From 3eb1be1a37c5e02412e9104286e536140aac68b6 Mon Sep 17 00:00:00 2001 From: Muhammad Adeel Date: Tue, 11 Jun 2024 15:31:30 +0200 Subject: [PATCH] Add s390/s390x CPU topology test --- machine/topology_test.go | 113 ++++++++++++++++++++++++++++++++-- utils/sysfs/fakesysfs/fake.go | 10 +++ utils/sysfs/sysfs.go | 4 +- 3 files changed, 120 insertions(+), 7 deletions(-) diff --git a/machine/topology_test.go b/machine/topology_test.go index bf72578ebe2..24618dc0cf5 100644 --- a/machine/topology_test.go +++ b/machine/topology_test.go @@ -19,6 +19,7 @@ import ( "os" "path/filepath" "reflect" + "runtime" "sort" "testing" @@ -426,11 +427,113 @@ func TestTopologyWithNodesWithoutCPU(t *testing.T) { } func TestTopologyOnSystemZ(t *testing.T) { - machineArch = "s390" // overwrite package variable - nodes, cores, err := GetTopology(&fakesysfs.FakeSysFs{}) - assert.Nil(t, err) - assert.Nil(t, nodes) - assert.NotNil(t, cores) + if runtime.GOARCH != "s390x" { + t.Skip("skipping TestTopologyOnSystemZ due to wrong architecture") + } else { + machineArch = "s390" // overwrite package variable + sysFs := &fakesysfs.FakeSysFs{} + + c := sysfs.CacheInfo{ + Id: 0, + Size: 128 * 1024, + Type: "Data", + Level: 0, + Cpus: 2, + } + sysFs.SetCacheInfo(c) + + nodesPaths := []string{} + sysFs.SetNodesPaths(nodesPaths, nil) + + cpusPaths := map[string][]string{ + "/sys/devices/system/cpu": { + "/sys/devices/system/cpu/cpu0", + "/sys/devices/system/cpu/cpu1", + "/sys/devices/system/cpu/cpu2", + "/sys/devices/system/cpu/cpu3", + }, + } + sysFs.SetCPUsPaths(cpusPaths, nil) + + coreThread := map[string]string{ + "/sys/devices/system/cpu/cpu0": "0", + "/sys/devices/system/cpu/cpu1": "1", + "/sys/devices/system/cpu/cpu2": "0", + "/sys/devices/system/cpu/cpu3": "1", + } + sysFs.SetCoreThreads(coreThread, nil) + + physicalPackageIDs := map[string]string{ + "/sys/devices/system/cpu/cpu0": "0", + "/sys/devices/system/cpu/cpu1": "1", + "/sys/devices/system/cpu/cpu2": "0", + "/sys/devices/system/cpu/cpu3": "1", + } + sysFs.SetPhysicalPackageIDs(physicalPackageIDs, nil) + + bookIDs := map[string]string{ + "/sys/devices/system/cpu/cpu0": "1", + "/sys/devices/system/cpu/cpu1": "1", + "/sys/devices/system/cpu/cpu2": "1", + "/sys/devices/system/cpu/cpu3": "1", + } + sysFs.SetBookIDs(bookIDs, nil) + + drawerIDs := map[string]string{ + "/sys/devices/system/cpu/cpu0": "0", + "/sys/devices/system/cpu/cpu1": "0", + "/sys/devices/system/cpu/cpu2": "0", + "/sys/devices/system/cpu/cpu3": "0", + } + sysFs.SetDrawerIDs(drawerIDs, nil) + + topology, numCores, err := GetTopology(sysFs) + assert.Nil(t, err) + assert.Equal(t, 2, len(topology)) + assert.Equal(t, 4, numCores) + + topologyJSON1, err := json.Marshal(topology[0]) + assert.Nil(t, err) + topologyJSON2, err := json.Marshal(topology[1]) + assert.Nil(t, err) + + expectedTopology1 := `{"node_id":0,"memory":0,"hugepages":null,"distances":null,"cores":[{"core_id":0,"thread_ids":[0,2],"caches":[{"id":0, "size":131072,"type":"Data","level":0}], "socket_id": 0, "book_id":"1", "drawer_id":"0", "uncore_caches":null}],"caches":null}` + expectedTopology2 := ` + { + "node_id":1, + "memory":0, + "hugepages":null, + "distances": null, + "cores":[ + { + "core_id":1, + "thread_ids":[ + 1, + 3 + ], + "caches":[ + { + "id": 0, + "size":131072, + "type":"Data", + "level":0 + } + ], + "socket_id": 1, + "book_id": "1", + "drawer_id": "0", + "uncore_caches": null + } + ], + "caches":null + }` + + json1 := string(topologyJSON1) + json2 := string(topologyJSON2) + + assert.JSONEq(t, expectedTopology1, json1) + assert.JSONEq(t, expectedTopology2, json2) + } } func TestMemoryInfo(t *testing.T) { diff --git a/utils/sysfs/fakesysfs/fake.go b/utils/sysfs/fakesysfs/fake.go index 1f004a11139..3ef4ca63297 100644 --- a/utils/sysfs/fakesysfs/fake.go +++ b/utils/sysfs/fakesysfs/fake.go @@ -199,6 +199,16 @@ func (fs *FakeSysFs) SetPhysicalPackageIDs(physicalPackageIDs map[string]string, fs.physicalPackageIDErr = physicalPackageIDErrors } +func (fs *FakeSysFs) SetBookIDs(bookIDs map[string]string, bookIDErrors map[string]error) { + fs.bookIDs = bookIDs + fs.bookIDErr = bookIDErrors +} + +func (fs *FakeSysFs) SetDrawerIDs(drawerIDs map[string]string, drawerIDErrors map[string]error) { + fs.drawerIDs = drawerIDs + fs.drawerIDErr = drawerIDErrors +} + func (fs *FakeSysFs) SetMemory(memTotal string, err error) { fs.memTotal = memTotal fs.memErr = err diff --git a/utils/sysfs/sysfs.go b/utils/sysfs/sysfs.go index 47ce5eb1ab4..dbf7e000e4f 100644 --- a/utils/sysfs/sysfs.go +++ b/utils/sysfs/sysfs.go @@ -177,7 +177,7 @@ func (fs *realSysFs) GetBookID(cpuPath string) (string, error) { if err != nil { return "", err } - return strings.TrimSpace(string(bookID)), err + return strings.TrimSpace(string(bookID)), nil } func (fs *realSysFs) GetDrawerID(cpuPath string) (string, error) { @@ -186,7 +186,7 @@ func (fs *realSysFs) GetDrawerID(cpuPath string) (string, error) { if err != nil { return "", err } - return strings.TrimSpace(string(drawerID)), err + return strings.TrimSpace(string(drawerID)), nil } func (fs *realSysFs) GetMemInfo(nodePath string) (string, error) {