From 9b176e678e8535bc72bede4f4b95318ce207644a Mon Sep 17 00:00:00 2001 From: "Sean E. Russell" Date: Fri, 26 Jun 2020 11:50:15 -0500 Subject: [PATCH] Work-around for bug shirou/gopsutil#849, addressing #135 --- devices/cpu_cpu.go | 2 +- devices/cpu_linux.go | 23 +++++++++++++++++++++++ devices/cpu_other.go | 9 +++++++++ widgets/proc.go | 5 ++--- widgets/proc_linux.go | 1 + 5 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 devices/cpu_linux.go create mode 100644 devices/cpu_other.go 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) +} diff --git a/widgets/proc.go b/widgets/proc.go index f47f646..218ba63 100644 --- a/widgets/proc.go +++ b/widgets/proc.go @@ -9,9 +9,8 @@ import ( "strings" "time" - psCPU "github.com/shirou/gopsutil/cpu" - tui "github.com/gizak/termui/v3" + "github.com/xxxserxxx/gotop/v4/devices" ui "github.com/xxxserxxx/gotop/v4/termui" "github.com/xxxserxxx/gotop/v4/utils" ) @@ -49,7 +48,7 @@ type ProcWidget struct { } func NewProcWidget() *ProcWidget { - cpuCount, err := psCPU.Counts(false) + cpuCount, err := devices.CpuCount() if err != nil { log.Printf("failed to get CPU count from gopsutil: %v", err) } diff --git a/widgets/proc_linux.go b/widgets/proc_linux.go index 164319d..8fb53a0 100644 --- a/widgets/proc_linux.go +++ b/widgets/proc_linux.go @@ -19,6 +19,7 @@ func getProcs() ([]Proc, error) { procs := []Proc{} for _, line := range linesOfProcStrings { + log.Printf("line is '%s', pid is '%s', cpu is '%s', mem is '%s'", line, strings.TrimSpace(line[0:10]), strings.TrimSpace(line[63:68]), strings.TrimSpace(line[69:74])) pid, err := strconv.Atoi(strings.TrimSpace(line[0:10])) if err != nil { log.Printf("failed to convert PID to int: %v. line: %v", err, line)