2014-08-08 22:09:28 +08:00
|
|
|
// +build darwin
|
|
|
|
|
2014-12-30 21:09:05 +08:00
|
|
|
package load
|
2014-08-08 22:09:28 +08:00
|
|
|
|
|
|
|
import (
|
2016-02-20 21:52:16 +08:00
|
|
|
"os/exec"
|
2014-08-08 22:09:28 +08:00
|
|
|
"strconv"
|
2016-02-20 21:52:16 +08:00
|
|
|
"strings"
|
2014-11-27 09:18:15 +08:00
|
|
|
|
2015-10-19 23:04:57 +08:00
|
|
|
"github.com/shirou/gopsutil/internal/common"
|
2014-08-08 22:09:28 +08:00
|
|
|
)
|
|
|
|
|
2016-03-22 22:09:12 +08:00
|
|
|
func Avg() (*AvgStat, error) {
|
2014-11-27 09:18:15 +08:00
|
|
|
values, err := common.DoSysctrl("vm.loadavg")
|
2014-08-08 22:09:28 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-03-22 22:09:12 +08:00
|
|
|
ret := &AvgStat{
|
2014-08-08 22:09:28 +08:00
|
|
|
Load1: float64(load1),
|
|
|
|
Load5: float64(load5),
|
|
|
|
Load15: float64(load15),
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
2016-02-20 21:52:16 +08:00
|
|
|
|
2016-02-20 22:17:20 +08: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'.
|
2016-02-20 21:52:16 +08:00
|
|
|
func Misc() (*MiscStat, error) {
|
|
|
|
bin, err := exec.LookPath("ps")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
out, err := invoke.Command(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") {
|
|
|
|
ret.ProcsRunning += 1
|
|
|
|
} else if strings.Contains(l, "U") {
|
|
|
|
// uninterruptible sleep == blocked
|
|
|
|
ret.ProcsBlocked += 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ret, nil
|
|
|
|
}
|