2024-02-17 03:48:29 +00:00
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2021-12-22 21:54:41 +00:00
|
|
|
//go:build darwin
|
2014-08-08 23:09:28 +09:00
|
|
|
|
2014-12-30 22:09:05 +09:00
|
|
|
package load
|
2014-08-08 23:09:28 +09:00
|
|
|
|
|
|
|
import (
|
2017-12-31 15:25:49 +09:00
|
|
|
"context"
|
2016-02-20 22:52:16 +09:00
|
|
|
"strings"
|
2019-03-02 18:47:41 +01:00
|
|
|
"unsafe"
|
2014-11-27 10:18:15 +09:00
|
|
|
|
2019-03-02 18:47:41 +01:00
|
|
|
"golang.org/x/sys/unix"
|
2014-08-08 23:09:28 +09:00
|
|
|
)
|
|
|
|
|
2016-03-22 23:09:12 +09:00
|
|
|
func Avg() (*AvgStat, error) {
|
2017-12-31 15:25:49 +09:00
|
|
|
return AvgWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func AvgWithContext(ctx context.Context) (*AvgStat, error) {
|
2019-03-02 18:47:41 +01:00
|
|
|
// This SysctlRaw method borrowed from
|
|
|
|
// https://github.com/prometheus/node_exporter/blob/master/collector/loadavg_freebsd.go
|
|
|
|
// this implementation is common with BSDs
|
|
|
|
type loadavg struct {
|
|
|
|
load [3]uint32
|
|
|
|
scale int
|
2014-08-08 23:09:28 +09:00
|
|
|
}
|
2019-03-02 18:47:41 +01:00
|
|
|
b, err := unix.SysctlRaw("vm.loadavg")
|
2014-08-08 23:09:28 +09:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-03-02 18:47:41 +01:00
|
|
|
load := *(*loadavg)(unsafe.Pointer((&b[0])))
|
|
|
|
scale := float64(load.scale)
|
2016-03-22 23:09:12 +09:00
|
|
|
ret := &AvgStat{
|
2019-03-02 18:47:41 +01:00
|
|
|
Load1: float64(load.load[0]) / scale,
|
|
|
|
Load5: float64(load.load[1]) / scale,
|
|
|
|
Load15: float64(load.load[2]) / scale,
|
2014-08-08 23:09:28 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
2016-02-20 22:52:16 +09:00
|
|
|
|
2022-01-30 22:48:09 +02:00
|
|
|
// Misc returns miscellaneous host-wide statistics.
|
2016-02-20 23:17:20 +09:00
|
|
|
// darwin use ps command to get process running/blocked count.
|
|
|
|
// Almost same as FreeBSD implementation, but state is different.
|
|
|
|
// U means 'Uninterruptible Sleep'.
|
2016-02-20 22:52:16 +09:00
|
|
|
func Misc() (*MiscStat, error) {
|
2017-12-31 15:25:49 +09:00
|
|
|
return MiscWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func MiscWithContext(ctx context.Context) (*MiscStat, error) {
|
2022-03-04 18:18:03 +02:00
|
|
|
out, err := invoke.CommandWithContext(ctx, "ps", "axo", "state")
|
2016-02-20 22:52:16 +09:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
lines := strings.Split(string(out), "\n")
|
|
|
|
|
|
|
|
ret := MiscStat{}
|
|
|
|
for _, l := range lines {
|
|
|
|
if strings.Contains(l, "R") {
|
2016-04-01 21:34:39 +09:00
|
|
|
ret.ProcsRunning++
|
2016-02-20 22:52:16 +09:00
|
|
|
} else if strings.Contains(l, "U") {
|
|
|
|
// uninterruptible sleep == blocked
|
2016-04-01 21:34:39 +09:00
|
|
|
ret.ProcsBlocked++
|
2016-02-20 22:52:16 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ret, nil
|
|
|
|
}
|