diff options
author | Sean E. Russell <ser@ser1.net> | 2020-06-26 11:50:15 -0500 |
---|---|---|
committer | Sean E. Russell <ser@ser1.net> | 2020-06-26 11:50:15 -0500 |
commit | 9b176e678e8535bc72bede4f4b95318ce207644a (patch) | |
tree | 8b3f6595ffe0074587b5bf5f4b64189c8dc0b7fb /devices | |
parent | 050b62a20a6250bac7bf1549e7cfeaf2bb7c5494 (diff) |
Work-around for bug shirou/gopsutil#849, addressing #135
Diffstat (limited to 'devices')
-rw-r--r-- | devices/cpu_cpu.go | 2 | ||||
-rw-r--r-- | devices/cpu_linux.go | 23 | ||||
-rw-r--r-- | devices/cpu_other.go | 9 |
3 files changed, 33 insertions, 1 deletions
diff --git a/devices/cpu_cpu.go b/devices/cpu_cpu.go index 0c1f9c1..bd2f891 100644 --- a/devices/cpu_cpu.go +++ b/devices/cpu_cpu.go @@ -9,7 +9,7 @@ import ( // FIXME: broken % under Linux. Doesn't reflect reality *at all*. func init() { f := func(cpus map[string]int, l bool) map[string]error { - cpuCount, err := psCpu.Counts(l) + cpuCount, err := CpuCount() if err != nil { return nil } diff --git a/devices/cpu_linux.go b/devices/cpu_linux.go new file mode 100644 index 0000000..4e836d5 --- /dev/null +++ b/devices/cpu_linux.go @@ -0,0 +1,23 @@ +// +build linux + +package devices + +import "github.com/shirou/gopsutil/cpu" + +func CpuCount() (int, error) { + cpuCount, err := cpu.Counts(false) + if err != nil { + return 0, err + } + if cpuCount == 0 { + is, err := cpu.Info() + if err != nil { + return 0, err + } + if is[0].Cores > 0 { + return len(is) / 2, nil + } + return len(is), nil + } + return cpuCount, nil +} diff --git a/devices/cpu_other.go b/devices/cpu_other.go new file mode 100644 index 0000000..95d183f --- /dev/null +++ b/devices/cpu_other.go @@ -0,0 +1,9 @@ +// +build !linux + +package devices + +import "github.com/shirou/gopsutil/cpu" + +func CpuCount() (int, error) { + return cpu.Counts(false) +} |