1
0
mirror of https://github.com/shirou/gopsutil.git synced 2025-04-24 13:48:56 +08:00
shirou_gopsutil/load/load_bsd.go

77 lines
1.6 KiB
Go
Raw Normal View History

// SPDX-License-Identifier: BSD-3-Clause
//go:build freebsd || openbsd
2014-04-18 16:34:47 +09:00
2014-12-30 22:09:05 +09:00
package load
2014-04-18 16:34:47 +09:00
import (
2017-12-31 15:25:49 +09:00
"context"
"strings"
"unsafe"
"golang.org/x/sys/unix"
2014-04-18 16:34:47 +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) {
// This SysctlRaw method borrowed from
// https://github.com/prometheus/node_exporter/blob/master/collector/loadavg_freebsd.go
type loadavg struct {
load [3]uint32
scale int
2014-04-18 16:34:47 +09:00
}
b, err := unix.SysctlRaw("vm.loadavg")
2014-04-18 16:34:47 +09:00
if err != nil {
2014-05-20 19:29:41 +09:00
return nil, err
2014-04-18 16:34:47 +09:00
}
load := *(*loadavg)(unsafe.Pointer((&b[0])))
scale := float64(load.scale)
ret := &AvgStat{
Load1: float64(load.load[0]) / scale,
Load5: float64(load.load[1]) / scale,
Load15: float64(load.load[2]) / scale,
2014-04-18 16:34:47 +09:00
}
return ret, nil
}
2020-09-12 12:35:21 +10:00
type forkstat struct {
forks int
vforks int
__tforks int
}
// 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 Darwin implementation, but state is different.
func Misc() (*MiscStat, error) {
2017-12-31 15:25:49 +09:00
return MiscWithContext(context.Background())
}
func MiscWithContext(ctx context.Context) (*MiscStat, error) {
out, err := invoke.CommandWithContext(ctx, "ps", "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, "D") {
2016-04-01 21:34:39 +09:00
ret.ProcsBlocked++
}
}
2020-09-12 12:35:21 +10:00
f, err := getForkStat()
if err != nil {
return nil, err
}
ret.ProcsCreated = f.forks
return &ret, nil
}