diff --git a/cpu/cpu_linux.go b/cpu/cpu_linux.go index 735bd29..a660dd4 100644 --- a/cpu/cpu_linux.go +++ b/cpu/cpu_linux.go @@ -7,6 +7,7 @@ import ( "errors" "fmt" "os/exec" + "path/filepath" "strconv" "strings" @@ -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 { diff --git a/cpu/cpu_test.go b/cpu/cpu_test.go index a2886f1..8ae9a82 100644 --- a/cpu/cpu_test.go +++ b/cpu/cpu_test.go @@ -71,8 +71,18 @@ func TestCpu_counts(t *testing.T) { t.Errorf("error %v", err) } if v == 0 { - t.Errorf("could not get CPU counts: %v", v) + t.Errorf("could not get logical 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 physical CPU counts: %v", v) + } + t.Logf("physical cores: %d", v) } func TestCPUTimeStat_String(t *testing.T) {