Merge pull request #944 from shirou/issue849

[cpu][linux] Fix #849 implement giampaolo/psutil#1727 and test Counts against lscpu
pull/945/head
shirou 5 years ago committed by GitHub
commit c5b7357407
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -7,6 +7,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"os/exec" "os/exec"
"path/filepath"
"strconv" "strconv"
"strings" "strings"
@ -311,7 +312,23 @@ func CountsWithContext(ctx context.Context, logical bool) (int, error) {
} }
return ret, nil 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") filename := common.HostProc("cpuinfo")
lines, err := common.ReadLines(filename) lines, err := common.ReadLines(filename)
if err != nil { if err != nil {

@ -2,6 +2,9 @@ package cpu
import ( import (
"os" "os"
"os/exec"
"strconv"
"strings"
"testing" "testing"
) )
@ -38,3 +41,53 @@ func TestCPUparseStatLine_424(t *testing.T) {
} }
os.Setenv("HOST_PROC", orig) os.Setenv("HOST_PROC", orig)
} }
func TestCPUCountsAgainstLscpu(t *testing.T) {
lscpu, err := exec.LookPath("lscpu")
if err != nil {
t.Skip("no lscpu to compare with")
}
cmd := exec.Command(lscpu)
cmd.Env = []string{"LC_ALL=C"}
out, err := cmd.Output()
if err != nil {
t.Errorf("error executing lscpu: %v", err)
}
var threadsPerCore, coresPerSocket, sockets int
lines := strings.Split(string(out), "\n")
for _, line := range lines {
fields := strings.Split(line, ":")
if len(fields) < 2 {
continue
}
switch fields[0] {
case "Thread(s) per core":
threadsPerCore, _ = strconv.Atoi(strings.TrimSpace(fields[1]))
case "Core(s) per socket":
coresPerSocket, _ = strconv.Atoi(strings.TrimSpace(fields[1]))
case "Socket(s)":
sockets, _ = strconv.Atoi(strings.TrimSpace(fields[1]))
}
}
if threadsPerCore == 0 || coresPerSocket == 0 || sockets == 0 {
t.Errorf("missing info from lscpu: threadsPerCore=%d coresPerSocket=%d sockets=%d", threadsPerCore, coresPerSocket, sockets)
}
expectedPhysical := coresPerSocket * sockets
expectedLogical := expectedPhysical * threadsPerCore
physical, err := Counts(false)
skipIfNotImplementedErr(t, err)
if err != nil {
t.Errorf("error %v", err)
}
logical, err := Counts(true)
skipIfNotImplementedErr(t, err)
if err != nil {
t.Errorf("error %v", err)
}
if expectedPhysical != physical {
t.Errorf("expected %v, got %v", expectedPhysical, physical)
}
if expectedLogical != logical {
t.Errorf("expected %v, got %v", expectedLogical, logical)
}
}

@ -71,8 +71,18 @@ func TestCpu_counts(t *testing.T) {
t.Errorf("error %v", err) t.Errorf("error %v", err)
} }
if v == 0 { 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) { func TestCPUTimeStat_String(t *testing.T) {

Loading…
Cancel
Save