xmtop/devices/cpu_cpu.go

36 lines
652 B
Go
Raw Normal View History

2020-02-26 04:04:55 +08:00
package devices
import (
"fmt"
2020-02-26 04:04:55 +08:00
psCpu "github.com/shirou/gopsutil/cpu"
)
2020-05-07 09:00:13 +08:00
// FIXME: broken % under Linux. Doesn't reflect reality *at all*.
2020-02-26 04:04:55 +08:00
func init() {
2020-05-02 01:41:18 +08:00
f := func(cpus map[string]int, l bool) map[string]error {
cpuCount, err := psCpu.Counts(l)
if err != nil {
return nil
}
formatString := "CPU%1d"
if cpuCount > 10 {
formatString = "CPU%02d"
}
2020-05-02 01:41:18 +08:00
vals, err := psCpu.Percent(0, l)
if err != nil {
return map[string]error{"gopsutil": err}
}
for i := 0; i < len(vals); i++ {
key := fmt.Sprintf(formatString, i)
v := vals[i]
if v > 100 {
v = 100
}
cpus[key] = int(v)
}
return nil
}
RegisterCPU(f)
2020-02-26 04:04:55 +08:00
}