2021-11-09 21:14:54 +08:00
|
|
|
// +build aix
|
|
|
|
|
|
|
|
package load
|
|
|
|
|
|
|
|
/*
|
|
|
|
#cgo LDFLAGS: -L/usr/lib -lperfstat
|
|
|
|
|
|
|
|
#include <libperfstat.h>
|
|
|
|
#include <procinfo.h>
|
|
|
|
*/
|
|
|
|
import "C"
|
|
|
|
|
|
|
|
import (
|
2021-12-23 05:46:33 +08:00
|
|
|
"context"
|
|
|
|
"unsafe"
|
2021-11-09 21:14:54 +08:00
|
|
|
|
2021-12-23 05:46:33 +08:00
|
|
|
"github.com/power-devops/perfstat"
|
2021-11-09 21:14:54 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func Avg() (*AvgStat, error) {
|
2021-12-23 05:46:33 +08:00
|
|
|
return AvgWithContext(context.Background())
|
2021-11-09 21:14:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func AvgWithContext(ctx context.Context) (*AvgStat, error) {
|
2021-12-23 05:46:33 +08:00
|
|
|
c, err := perfstat.CpuTotalStat()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ret := &AvgStat{
|
|
|
|
Load1: float64(c.LoadAvg1),
|
|
|
|
Load5: float64(c.LoadAvg5),
|
|
|
|
Load15: float64(c.LoadAvg15),
|
|
|
|
}
|
2021-11-09 21:14:54 +08:00
|
|
|
|
2021-12-23 05:46:33 +08:00
|
|
|
return ret, nil
|
2021-11-09 21:14:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Misc returns miscellaneous host-wide statistics.
|
|
|
|
// darwin use ps command to get process running/blocked count.
|
|
|
|
// Almost same as Darwin implementation, but state is different.
|
|
|
|
func Misc() (*MiscStat, error) {
|
2021-12-23 05:46:33 +08:00
|
|
|
return MiscWithContext(context.Background())
|
2021-11-09 21:14:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func MiscWithContext(ctx context.Context) (*MiscStat, error) {
|
2021-12-23 05:46:33 +08:00
|
|
|
info := C.struct_procentry64{}
|
|
|
|
cpid := C.pid_t(0)
|
2021-11-09 21:14:54 +08:00
|
|
|
|
2021-12-23 05:46:33 +08:00
|
|
|
ret := MiscStat{}
|
|
|
|
for {
|
|
|
|
// getprocs first argument is a void*
|
|
|
|
num, err := C.getprocs64(unsafe.Pointer(&info), C.sizeof_struct_procentry64, nil, 0, &cpid, 1)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-11-09 21:14:54 +08:00
|
|
|
|
2021-12-23 05:46:33 +08:00
|
|
|
ret.ProcsTotal++
|
|
|
|
switch info.pi_state {
|
|
|
|
case C.SACTIVE:
|
|
|
|
ret.ProcsRunning++
|
|
|
|
case C.SSTOP:
|
|
|
|
ret.ProcsBlocked++
|
|
|
|
}
|
2021-11-09 21:14:54 +08:00
|
|
|
|
2021-12-23 05:46:33 +08:00
|
|
|
if num == 0 {
|
|
|
|
break
|
2021-11-09 21:14:54 +08:00
|
|
|
}
|
2021-12-23 05:46:33 +08:00
|
|
|
}
|
|
|
|
return &ret, nil
|
2021-11-09 21:14:54 +08:00
|
|
|
}
|