1
0
mirror of https://github.com/shirou/gopsutil.git synced 2025-04-29 13:49:21 +08:00
shirou_gopsutil/load/load_darwin.go

77 lines
1.5 KiB
Go
Raw Normal View History

// +build darwin
2014-12-30 22:09:05 +09:00
package load
import (
2017-12-31 15:25:49 +09:00
"context"
"os/exec"
"strconv"
"strings"
2015-10-20 00:04:57 +09:00
"github.com/shirou/gopsutil/internal/common"
)
func Avg() (*AvgStat, error) {
2017-12-31 15:25:49 +09:00
return AvgWithContext(context.Background())
}
func AvgWithContext(ctx context.Context) (*AvgStat, error) {
2018-03-31 21:35:53 +09:00
values, err := common.DoSysctrlWithContext(ctx, "vm.loadavg")
if err != nil {
return nil, err
}
load1, err := strconv.ParseFloat(values[0], 64)
if err != nil {
return nil, err
}
load5, err := strconv.ParseFloat(values[1], 64)
if err != nil {
return nil, err
}
load15, err := strconv.ParseFloat(values[2], 64)
if err != nil {
return nil, err
}
ret := &AvgStat{
Load1: float64(load1),
Load5: float64(load5),
Load15: float64(load15),
}
return ret, nil
}
2016-02-20 23:17:20 +09:00
// Misc returnes miscellaneous host-wide statistics.
// darwin use ps command to get process running/blocked count.
// Almost same as FreeBSD implementation, but state is different.
// U means 'Uninterruptible Sleep'.
func Misc() (*MiscStat, error) {
2017-12-31 15:25:49 +09:00
return MiscWithContext(context.Background())
}
func MiscWithContext(ctx context.Context) (*MiscStat, error) {
bin, err := exec.LookPath("ps")
if err != nil {
return nil, err
}
2018-03-31 21:35:53 +09:00
out, err := invoke.CommandWithContext(ctx, bin, "axo", "state")
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++
} else if strings.Contains(l, "U") {
// uninterruptible sleep == blocked
2016-04-01 21:34:39 +09:00
ret.ProcsBlocked++
}
}
return &ret, nil
}