2017-09-20 14:58:30 +08:00
|
|
|
package cpu
|
|
|
|
|
|
|
|
import (
|
2022-03-05 00:18:03 +08:00
|
|
|
"errors"
|
2020-09-15 08:11:23 +08:00
|
|
|
"os/exec"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2017-09-20 14:58:30 +08:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestTimesEmpty(t *testing.T) {
|
2022-12-20 13:13:01 +08:00
|
|
|
t.Setenv("HOST_PROC", "testdata/linux/times_empty")
|
2017-09-20 14:58:30 +08:00
|
|
|
_, err := Times(true)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Times(true) failed")
|
|
|
|
}
|
|
|
|
_, err = Times(false)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Times(false) failed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCPUparseStatLine_424(t *testing.T) {
|
2022-12-20 13:13:01 +08:00
|
|
|
t.Setenv("HOST_PROC", "testdata/linux/424/proc")
|
2017-09-20 14:58:30 +08:00
|
|
|
{
|
|
|
|
l, err := Times(true)
|
|
|
|
if err != nil || len(l) == 0 {
|
|
|
|
t.Error("Times(true) failed")
|
|
|
|
}
|
|
|
|
t.Logf("Times(true): %#v", l)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
l, err := Times(false)
|
|
|
|
if err != nil || len(l) == 0 {
|
|
|
|
t.Error("Times(false) failed")
|
|
|
|
}
|
|
|
|
t.Logf("Times(false): %#v", l)
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:11:23 +08:00
|
|
|
|
|
|
|
func TestCPUCountsAgainstLscpu(t *testing.T) {
|
2022-03-05 00:18:03 +08:00
|
|
|
cmd := exec.Command("lscpu")
|
2020-09-15 08:11:23 +08:00
|
|
|
cmd.Env = []string{"LC_ALL=C"}
|
|
|
|
out, err := cmd.Output()
|
|
|
|
if err != nil {
|
2022-03-05 00:18:03 +08:00
|
|
|
if errors.Is(err, exec.ErrNotFound) {
|
|
|
|
t.Skip("no lscpu to compare with")
|
|
|
|
}
|
2020-09-15 08:11:23 +08:00
|
|
|
t.Errorf("error executing lscpu: %v", err)
|
|
|
|
}
|
2023-02-08 06:12:31 +08:00
|
|
|
var threadsPerCore, coresPerSocket, sockets, books, drawers int
|
|
|
|
books = 1
|
|
|
|
drawers = 1
|
2020-09-15 08:11:23 +08:00
|
|
|
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]))
|
2023-02-08 06:12:31 +08:00
|
|
|
case "Socket(s)", "Socket(s) per book":
|
2020-09-15 08:11:23 +08:00
|
|
|
sockets, _ = strconv.Atoi(strings.TrimSpace(fields[1]))
|
2023-02-08 06:12:31 +08:00
|
|
|
case "Book(s) per drawer":
|
|
|
|
books, _ = strconv.Atoi(strings.TrimSpace(fields[1]))
|
|
|
|
case "Drawer(s)":
|
|
|
|
drawers, _ = strconv.Atoi(strings.TrimSpace(fields[1]))
|
2020-09-15 08:11:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if threadsPerCore == 0 || coresPerSocket == 0 || sockets == 0 {
|
|
|
|
t.Errorf("missing info from lscpu: threadsPerCore=%d coresPerSocket=%d sockets=%d", threadsPerCore, coresPerSocket, sockets)
|
|
|
|
}
|
2023-02-08 06:12:31 +08:00
|
|
|
expectedPhysical := coresPerSocket * sockets * books * drawers
|
2020-09-15 08:11:23 +08:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2021-02-22 07:28:56 +08:00
|
|
|
|
|
|
|
func TestCPUCountsLogicalAndroid_1037(t *testing.T) { // https://github.com/shirou/gopsutil/issues/1037
|
2022-12-20 13:13:01 +08:00
|
|
|
t.Setenv("HOST_PROC", "testdata/linux/1037/proc")
|
2021-02-22 07:28:56 +08:00
|
|
|
|
|
|
|
count, err := Counts(true)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error %v", err)
|
|
|
|
}
|
|
|
|
expected := 8
|
|
|
|
if count != expected {
|
|
|
|
t.Errorf("expected %v, got %v", expected, count)
|
|
|
|
}
|
|
|
|
}
|