Skip to content

Commit

Permalink
Work-around for bug shirou/gopsutil#849, addressing #135
Browse files Browse the repository at this point in the history
  • Loading branch information
xxxserxxx committed Jun 26, 2020
1 parent 050b62a commit 9b176e6
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 4 deletions.
2 changes: 1 addition & 1 deletion devices/cpu_cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
23 changes: 23 additions & 0 deletions devices/cpu_linux.go
Original file line number Diff line number Diff line change
@@ -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
}
9 changes: 9 additions & 0 deletions devices/cpu_other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// +build !linux

package devices

import "github.com/shirou/gopsutil/cpu"

func CpuCount() (int, error) {
return cpu.Counts(false)
}
5 changes: 2 additions & 3 deletions widgets/proc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
}
Expand Down
1 change: 1 addition & 0 deletions widgets/proc_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 9b176e6

Please sign in to comment.