diff --git a/pkg/stat/sys/cpu/cgroup.go b/pkg/stat/sys/cpu/cgroup.go index a3edd52f4..b297f611f 100644 --- a/pkg/stat/sys/cpu/cgroup.go +++ b/pkg/stat/sys/cpu/cgroup.go @@ -56,7 +56,10 @@ func (c *cgroup) CPUAcctUsagePerCPU() ([]uint64, error) { if u, err = parseUint(v); err != nil { return nil, err } - usage = append(usage, u) + // fix possible_cpu:https://www.ibm.com/support/knowledgecenter/en/linuxonibm/com.ibm.linux.z.lgdd/lgdd_r_posscpusparm.html + if u != 0 { + usage = append(usage, u) + } } return usage, nil } diff --git a/pkg/stat/sys/cpu/cgroupCPU.go b/pkg/stat/sys/cpu/cgroupCPU.go index dda467ee7..db1372ae9 100644 --- a/pkg/stat/sys/cpu/cgroupCPU.go +++ b/pkg/stat/sys/cpu/cgroupCPU.go @@ -8,6 +8,7 @@ import ( "strings" "github.com/pkg/errors" + pscpu "github.com/shirou/gopsutil/cpu" ) type cgroupCPU struct { @@ -21,12 +22,17 @@ type cgroupCPU struct { } func newCgroupCPU() (cpu *cgroupCPU, err error) { - cpus, err := perCPUUsage() - if err != nil { - err = errors.Errorf("perCPUUsage() failed!err:=%v", err) - return + var cores int + cores, err = pscpu.Counts(true) + if err != nil || cores == 0 { + var cpus []uint64 + cpus, err = perCPUUsage() + if err != nil { + err = errors.Errorf("perCPUUsage() failed!err:=%v", err) + return + } + cores = len(cpus) } - cores := uint64(len(cpus)) sets, err := cpuSets() if err != nil { @@ -61,7 +67,7 @@ func newCgroupCPU() (cpu *cgroupCPU, err error) { cpu = &cgroupCPU{ frequency: maxFreq, quota: quota, - cores: cores, + cores: uint64(cores), preSystem: preSystem, preTotal: preTotal, }