From 48b30f07e58b953499f5e5a2149ebcd5bf7cc3d0 Mon Sep 17 00:00:00 2001 From: Lomanic Date: Tue, 15 Sep 2020 01:11:17 +0200 Subject: [PATCH] [cpu][linux] Fix 849 implement giampaolo/psutil/pull/1727 --- cpu/cpu_linux.go | 19 ++++++++++++++++++- cpu/cpu_test.go | 10 ++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/cpu/cpu_linux.go b/cpu/cpu_linux.go index 735bd29ed..214a31e9b 100644 --- a/cpu/cpu_linux.go +++ b/cpu/cpu_linux.go @@ -5,6 +5,7 @@ package cpu import ( "context" "errors" + "path/filepath" "fmt" "os/exec" "strconv" @@ -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 a2886f127..97654df70 100644 --- a/cpu/cpu_test.go +++ b/cpu/cpu_test.go @@ -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) {