From 01783b26f78d4caf153c987b58ed6019a31e55f9 Mon Sep 17 00:00:00 2001 From: longXboy Date: Sat, 12 Oct 2019 12:22:28 +0800 Subject: [PATCH 1/2] fix possible cpus --- go.mod | 1 + pkg/stat/sys/cpu/cgroup.go | 5 ++++- pkg/stat/sys/cpu/cgroupCPU.go | 19 +++++++++++++------ 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/go.mod b/go.mod index d32f668ce..de240ece7 100644 --- a/go.mod +++ b/go.mod @@ -23,6 +23,7 @@ require ( github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect github.com/leodido/go-urn v1.1.0 // indirect github.com/mattn/go-colorable v0.1.2 // indirect + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/montanaflynn/stats v0.5.0 github.com/openzipkin/zipkin-go v0.2.1 github.com/otokaze/mock v0.0.0-20190125081256-8282b7a7c7c3 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..4555a4bc1 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,10 +67,11 @@ func newCgroupCPU() (cpu *cgroupCPU, err error) { cpu = &cgroupCPU{ frequency: maxFreq, quota: quota, - cores: cores, + cores: uint64(cores), preSystem: preSystem, preTotal: preTotal, } + fmt.Printf("get cpu info:%+v \n", cpu) return } From 560bcd0c2a18d91d743fb346750fb5667b740e25 Mon Sep 17 00:00:00 2001 From: longXboy Date: Tue, 15 Oct 2019 16:16:30 +0800 Subject: [PATCH 2/2] fix lint --- go.mod | 1 - pkg/stat/sys/cpu/cgroupCPU.go | 1 - 2 files changed, 2 deletions(-) diff --git a/go.mod b/go.mod index 475ff1c43..ce078a8e7 100644 --- a/go.mod +++ b/go.mod @@ -26,7 +26,6 @@ require ( github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect github.com/leodido/go-urn v1.1.0 // indirect github.com/mattn/go-colorable v0.1.2 // indirect - github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/montanaflynn/stats v0.5.0 github.com/openzipkin/zipkin-go v0.2.1 github.com/otokaze/mock v0.0.0-20190125081256-8282b7a7c7c3 diff --git a/pkg/stat/sys/cpu/cgroupCPU.go b/pkg/stat/sys/cpu/cgroupCPU.go index 4555a4bc1..db1372ae9 100644 --- a/pkg/stat/sys/cpu/cgroupCPU.go +++ b/pkg/stat/sys/cpu/cgroupCPU.go @@ -71,7 +71,6 @@ func newCgroupCPU() (cpu *cgroupCPU, err error) { preSystem: preSystem, preTotal: preTotal, } - fmt.Printf("get cpu info:%+v \n", cpu) return }