xmtop/devices/cpu_cpu.go
寻觅 9dcbd2a48e
Some checks failed
Build Go binaries / build (push) Has been cancelled
[修复] 更新依赖, 修复龙架构无法正常编译的问题
2024-09-13 15:08:35 +08:00

35 lines
583 B
Go

package devices
import (
"fmt"
psCpu "github.com/shirou/gopsutil/v3/cpu"
)
func init() {
f := func(cpus map[string]int, l bool) map[string]error {
cpuCount, err := CpuCount()
if err != nil {
return nil
}
formatString := "CPU%1d"
if cpuCount > 10 {
formatString = "CPU%02d"
}
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)
}