Skip to content

Commit

Permalink
[cpu][linux] Test Counts against lscpu results
Browse files Browse the repository at this point in the history
  • Loading branch information
Lomanic committed Sep 15, 2020
1 parent 7700262 commit bb6f6e4
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions cpu/cpu_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package cpu

import (
"os"
"os/exec"
"strconv"
"strings"
"testing"
)

Expand Down Expand Up @@ -38,3 +41,53 @@ func TestCPUparseStatLine_424(t *testing.T) {
}
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)
}
}

0 comments on commit bb6f6e4

Please sign in to comment.