Skip to content

Commit

Permalink
[cpu][linux] Fix 849 implement giampaolo/psutil/pull/1727
Browse files Browse the repository at this point in the history
  • Loading branch information
Lomanic committed Sep 14, 2020
1 parent 4545a21 commit 48b30f0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
19 changes: 18 additions & 1 deletion cpu/cpu_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package cpu
import (
"context"
"errors"
"path/filepath"
"fmt"
"os/exec"
"strconv"
Expand Down Expand Up @@ -311,7 +312,23 @@ func CountsWithContext(ctx context.Context, logical bool) (int, error) {
}
return ret, nil
}
// physical cores https://github.com/giampaolo/psutil/blob/d01a9eaa35a8aadf6c519839e987a49d8be2d891/psutil/_pslinux.py#L628
// physical cores
// https://github.com/giampaolo/psutil/blob/122174a10b75c9beebe15f6c07dcf3afbe3b120d/psutil/_pslinux.py#L621-L629
var threadSiblingsLists = make(map[string]bool)
if files, err := filepath.Glob(common.HostSys("devices/system/cpu/cpu[0-9]*/topology/thread_siblings_list")); err == nil {
for _, file := range files {
lines, err := common.ReadLines(file)
if err != nil || len(lines) != 1 {
continue
}
threadSiblingsLists[lines[0]] = true
}
ret := len(threadSiblingsLists)
if ret != 0 {
return ret, nil
}
}
// https://github.com/giampaolo/psutil/blob/122174a10b75c9beebe15f6c07dcf3afbe3b120d/psutil/_pslinux.py#L631-L652
filename := common.HostProc("cpuinfo")
lines, err := common.ReadLines(filename)
if err != nil {
Expand Down
10 changes: 10 additions & 0 deletions cpu/cpu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ func TestCpu_counts(t *testing.T) {
if v == 0 {
t.Errorf("could not get CPU counts: %v", v)
}
t.Logf("logical cores: %d", v)
v, err = Counts(false)
skipIfNotImplementedErr(t, err)
if err != nil {
t.Errorf("error %v", err)
}
if v == 0 {
t.Errorf("could not get CPU counts: %v", v)
}
t.Logf("physical cores: %d", v)
}

func TestCPUTimeStat_String(t *testing.T) {
Expand Down

0 comments on commit 48b30f0

Please sign in to comment.