diff --git a/README.md b/README.md index 2f04c6f4..3d0d0e82 100644 --- a/README.md +++ b/README.md @@ -80,6 +80,15 @@ information about the CPUs on the host system. the host system contains * `ghw.CPUInfo.Processors` is an array of `ghw.Processor` structs, one for each physical processor package contained in the host +* `ghw.CPUInfo.LogicalProcessors` (Linux only) is an array of pointers to + `cpu.LogicalProcessor` structs, one for each online logical processor, + ascending by ID +* `ghw.CPUInfo.OfflineLogicalProcessors` (Linux only) is an array of the IDs of + the logical processors that are present but offline (for example with SMT + turned off), ascending +* `ghw.CPUInfo.Caches` (Linux only) is an array of pointers to + `ghw.MemoryCache` structs, one for each cache instance of the online logical + processors, with the logical processors sharing it Each `ghw.Processor` struct contains a number of fields: @@ -101,7 +110,8 @@ A `ghw.ProcessorCore` has the following fields: * `ghw.ProcessorCore.ID` is the `uint32` identifier that the host gave this core. Note that this does *not* necessarily equate to a zero-based index of the core within a physical package. For example, the core IDs for an Intel Core - i7 are 0, 1, 2, 8, 9, and 10 + i7 are 0, 1, 2, 8, 9, and 10. On Linux it is -1 where the core cannot be told, + and each such core holds a single logical processor * `ghw.ProcessorCore.TotalHardwareThreads` is the number of hardware threads associated with the core * `ghw.ProcessorCore.LogicalProcessors` is an array of ints representing the @@ -110,6 +120,31 @@ A `ghw.ProcessorCore` has the following fields: *zero-based* index of the processor on the host and are *not* related to the core ID. +A `cpu.LogicalProcessor` (Linux only) describes one online logical processor +(hardware thread) and where it sits in the host's topology. An ID that could +not be read is -1, and a frequency the kernel does not report is 0: + +* `cpu.LogicalProcessor.ID` is the logical processor ID the kernel and CPU + affinities use +* `cpu.LogicalProcessor.ProcessorID` is the physical package (socket) it + belongs to: the ID of its `ghw.Processor` +* `cpu.LogicalProcessor.CoreID` is the physical core it runs on: the ID of its + `ghw.ProcessorCore`, unique within the package. Logical processors with the + same known `ProcessorID` and `CoreID` are SMT siblings +* `cpu.LogicalProcessor.NodeID` is the NUMA node it belongs to; 0 on a kernel + without NUMA +* `cpu.LogicalProcessor.CoreType` is the class of core it runs on, a + `cpu.CoreType`: `cpu.CoreTypePerformance` or `cpu.CoreTypeEfficiency` on a + heterogeneous part (Intel P- and E-cores, ARM big.LITTLE), + `cpu.CoreTypeUniform` on a homogeneous one, or `cpu.CoreTypeUnknown` where the + host does not tell. Intel hybrid parts list their cores under the `cpu_core`, + `cpu_atom` and `cpu_lowpower` PMUs; elsewhere each CPU's `cpu_capacity` ranks + it, the highest capacity being a performance core. The ranking sees only + online CPUs and ranks by capacity alone, so on a part with three classes of + core the middle one reads as efficiency +* `cpu.LogicalProcessor.BaseFrequencyKHz` is its base (non-turbo) frequency +* `cpu.LogicalProcessor.MaxFrequencyKHz` is its maximum (turbo) frequency + ```go package main @@ -458,6 +493,9 @@ Each `ghw.MemoryCache` struct contains the following fields: cache can contain * `ghw.MemoryCache.LogicalProcessors` is an array of integers representing the logical processors that use the cache +* `ghw.MemoryCache.ID` (Linux only) is the kernel's identifier of this cache + instance, unique among the caches of the same level and type, or -1 where the + kernel does not number it (older kernels did not number device-tree caches) ```go package main @@ -700,6 +738,10 @@ The `ghw.PCIDevice` struct has the following fields: information is not available. If the information is not available, this does not mean the device is not functioning, but rather that `ghw` was not able to retrieve driver information. +* `ghw.PCIDevice.LocalCPUs` (Linux only) is an array of the logical processors + local to the device: those of the NUMA node its DMA and interrupts are local + to, or every CPU when the platform gives it no NUMA affinity. Nil when the + kernel does not report them. The `ghw.PCIAddress` (which is an alias for the `ghw.pci.address.Address` struct) contains the PCI address fields. It has a `ghw.PCIAddress.String()` diff --git a/internal/sysfsfixture/sysfsfixture.go b/internal/sysfsfixture/sysfsfixture.go new file mode 100644 index 00000000..8fe4dd29 --- /dev/null +++ b/internal/sysfsfixture/sysfsfixture.go @@ -0,0 +1,156 @@ +// +// Use and distribution licensed under the Apache license version 2. +// +// See the COPYING file in the root project directory for full text. +// + +// Package sysfsfixture writes small fake sysfs and procfs trees describing a +// CPU topology, for tests that point ghw at them with a chroot. +package sysfsfixture + +import ( + "fmt" + "os" + "path/filepath" + "sort" + "strconv" + "strings" + "testing" + + "github.com/zededa/ghw/pkg/util" +) + +// CPU describes one logical processor. An empty string field means the +// corresponding file does not exist. +type CPU struct { + ID int + Package string // topology/physical_package_id + Core string // topology/core_id + Siblings string // topology/thread_siblings_list + // Offline CPUs are present but not online: cpuN/online is 0 and they have + // no topology, caches or frequencies, as on a real kernel. + Offline bool + Caches []Cache + BaseKHz string // cpufreq/base_frequency + MaxKHz string // cpufreq/cpuinfo_max_freq + Capacity string // cpu_capacity +} + +// Cache describes one cache/indexN directory of a CPU. +type Cache struct { + Index int + Level string + Type string + ID string + Size string + Shared string // shared_cpu_list +} + +// Tree describes the whole fixture. +type Tree struct { + CPUs []CPU + // Nodes maps a NUMA node to its cpulist. Nil means no node directory at + // all, as on a kernel without NUMA. + Nodes map[int]string + // PMUCore, PMUAtom and PMULowPower are the cpus files of the Intel hybrid + // core PMUs; empty means the PMU is absent. + PMUCore string + PMUAtom string + PMULowPower string + // CorePMU creates /sys/devices/cpu, the core PMU of a homogeneous part. + CorePMU bool +} + +// Write writes the tree under a new temporary directory and returns it, to be +// used as the chroot. +func Write(t testing.TB, tree Tree) string { + t.Helper() + root := t.TempDir() + write := func(path, content string) { + t.Helper() + full := filepath.Join(root, path) + if err := os.MkdirAll(filepath.Dir(full), 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(full, []byte(content+"\n"), 0o644); err != nil { + t.Fatal(err) + } + } + optional := func(path, content string) { + if content != "" { + write(path, content) + } + } + + cpuDir := "sys/devices/system/cpu" + var online, present []int + var cpuinfo strings.Builder + for _, c := range tree.CPUs { + dir := fmt.Sprintf("%s/cpu%d", cpuDir, c.ID) + present = append(present, c.ID) + if c.Offline { + write(dir+"/online", "0") + continue + } + online = append(online, c.ID) + write(dir+"/online", "1") + optional(dir+"/topology/physical_package_id", c.Package) + optional(dir+"/topology/core_id", c.Core) + optional(dir+"/topology/thread_siblings_list", c.Siblings) + optional(dir+"/cpufreq/base_frequency", c.BaseKHz) + optional(dir+"/cpufreq/cpuinfo_max_freq", c.MaxKHz) + optional(dir+"/cpu_capacity", c.Capacity) + for _, cache := range c.Caches { + idx := fmt.Sprintf("%s/cache/index%d", dir, cache.Index) + optional(idx+"/level", cache.Level) + optional(idx+"/type", cache.Type) + optional(idx+"/id", cache.ID) + optional(idx+"/size", cache.Size) + optional(idx+"/shared_cpu_list", cache.Shared) + } + fmt.Fprintf(&cpuinfo, "processor\t: %d\nvendor_id\t: GenuineTest\nmodel name\t: Test CPU\nflags\t\t: fpu\n\n", c.ID) + } + write(cpuDir+"/online", List(online)) + write(cpuDir+"/present", List(present)) + write(cpuDir+"/possible", List(present)) + write("proc/cpuinfo", cpuinfo.String()) + + if tree.Nodes != nil { + nodes := make([]int, 0, len(tree.Nodes)) + for n := range tree.Nodes { + nodes = append(nodes, n) + } + sort.Ints(nodes) + for _, n := range nodes { + nodeDir := fmt.Sprintf("sys/devices/system/node/node%d", n) + write(nodeDir+"/cpulist", tree.Nodes[n]) + // Like the kernel, link each of the node's CPUs into its directory. + cpus, err := util.ParseCPUList(tree.Nodes[n]) + if err != nil { + t.Fatal(err) + } + for _, c := range cpus { + link := filepath.Join(root, nodeDir, fmt.Sprintf("cpu%d", c)) + if err := os.Symlink(fmt.Sprintf("../../cpu/cpu%d", c), link); err != nil { + t.Fatal(err) + } + } + } + } + optional("sys/devices/cpu_core/cpus", tree.PMUCore) + optional("sys/devices/cpu_atom/cpus", tree.PMUAtom) + optional("sys/devices/cpu_lowpower/cpus", tree.PMULowPower) + if tree.CorePMU { + write("sys/devices/cpu/type", "4") + } + return root +} + +// List formats logical processor IDs as a comma-separated CPU list. +func List(ids []int) string { + s := make([]string, len(ids)) + for i, id := range ids { + s[i] = strconv.Itoa(id) + } + return strings.Join(s, ",") +} diff --git a/pkg/cpu/cpu.go b/pkg/cpu/cpu.go index 48d4da84..916f6ed7 100644 --- a/pkg/cpu/cpu.go +++ b/pkg/cpu/cpu.go @@ -7,10 +7,14 @@ package cpu import ( + "encoding/json" "fmt" + "strconv" + "strings" "github.com/zededa/ghw/internal/config" "github.com/zededa/ghw/pkg/marshal" + "github.com/zededa/ghw/pkg/memory" ) // ProcessorCore describes a physical host processor core. A processor core is @@ -20,7 +24,8 @@ type ProcessorCore struct { // ID is the `uint32` identifier that the host gave this core. Note that // this does *not* necessarily equate to a zero-based index of the core // within a physical package. For example, the core IDs for an Intel Core - // i7 are 0, 1, 2, 8, 9, and 10 + // i7 are 0, 1, 2, 8, 9, and 10. It is -1 where the core cannot be told; + // each such core holds a single logical processor. ID int `json:"id"` // TotalHardwareThreads is the number of hardware threads associated with // the core @@ -134,6 +139,40 @@ type Info struct { // Processors is a slice of Processor struct pointers, one for each // physical processor package contained in the host Processors []*Processor `json:"processors"` + // LogicalProcessors describes each online logical processor, ascending by + // ID. + LogicalProcessors []*LogicalProcessor `json:"logical_processors,omitempty"` + // OfflineLogicalProcessors are the IDs of the logical processors that are + // present but offline (for example SMT turned off), ascending. + OfflineLogicalProcessors []int `json:"offline_logical_processors,omitempty"` + // Caches lists every cache instance of the online logical processors, + // with the logical processors sharing it. + Caches []*memory.Cache `json:"caches,omitempty"` +} + +// LogicalProcessor describes one online logical processor (hardware thread) +// and where it sits in the host's topology. An ID that could not be read is -1, +// and a frequency the kernel does not report is 0. +type LogicalProcessor struct { + // ID is the logical processor ID the kernel and CPU affinities use. + ID int `json:"id"` + // ProcessorID is the physical package (socket) it belongs to, the ID of + // its Processor in Processors (unless /proc/cpuinfo does not describe the + // logical processor, which leaves it out of Processors). + ProcessorID int `json:"processor_id"` + // CoreID is the physical core it runs on, the ID of its ProcessorCore, + // unique within the package. Logical processors with the same known + // ProcessorID and CoreID are SMT siblings; two with an ID of -1 are not + // known to be. + CoreID int `json:"core_id"` + // NodeID is the NUMA node it belongs to; 0 on a kernel without NUMA. + NodeID int `json:"node_id"` + // CoreType is the class of core it runs on. + CoreType CoreType `json:"core_type"` + // BaseFrequencyKHz is its base (non-turbo) frequency. + BaseFrequencyKHz uint64 `json:"base_frequency_khz,omitempty"` + // MaxFrequencyKHz is its maximum (turbo) frequency. + MaxFrequencyKHz uint64 `json:"max_frequency_khz,omitempty"` } // New returns a pointer to an Info struct that contains information about the @@ -189,3 +228,59 @@ func (i *Info) YAMLString() string { func (i *Info) JSONString(indent bool) string { return marshal.SafeJSON(cpuPrinter{i}, indent) } + +// CoreType is the class of core a logical processor runs on. +type CoreType int + +const ( + // CoreTypeUnknown means the host does not tell the class of the core. + CoreTypeUnknown CoreType = iota + // CoreTypePerformance is a performance core of a heterogeneous part, such + // as an Intel P-core or an ARM "big" core. + CoreTypePerformance + // CoreTypeEfficiency is an efficiency core of a heterogeneous part, such + // as an Intel E-core or an ARM "LITTLE" core. + CoreTypeEfficiency + // CoreTypeUniform is a core of a homogeneous part, where every core is of + // the same class. + CoreTypeUniform +) + +var ( + coreTypeString = map[CoreType]string{ + CoreTypeUnknown: "unknown", + CoreTypePerformance: "performance", + CoreTypeEfficiency: "efficiency", + CoreTypeUniform: "uniform", + } + stringCoreType = map[string]CoreType{ + "unknown": CoreTypeUnknown, + "performance": CoreTypePerformance, + "efficiency": CoreTypeEfficiency, + "uniform": CoreTypeUniform, + } +) + +// String returns the core type's name, as it appears in JSON and YAML. +func (t CoreType) String() string { + return coreTypeString[t] +} + +// MarshalJSON writes the core type as its name. +func (t CoreType) MarshalJSON() ([]byte, error) { + return []byte(strconv.Quote(t.String())), nil +} + +// UnmarshalJSON reads a core type from its name. +func (t *CoreType) UnmarshalJSON(b []byte) error { + var s string + if err := json.Unmarshal(b, &s); err != nil { + return err + } + val, ok := stringCoreType[strings.ToLower(s)] + if !ok { + return fmt.Errorf("unknown core type: %q", s) + } + *t = val + return nil +} diff --git a/pkg/cpu/cpu_linux.go b/pkg/cpu/cpu_linux.go index 6c4e8d59..57629d0e 100644 --- a/pkg/cpu/cpu_linux.go +++ b/pkg/cpu/cpu_linux.go @@ -10,9 +10,11 @@ import ( "context" "errors" "fmt" + "math" "os" "path/filepath" "regexp" + "slices" "sort" "strconv" "strings" @@ -20,6 +22,7 @@ import ( "github.com/zededa/ghw/internal/log" "github.com/zededa/ghw/pkg/linuxdt" "github.com/zededa/ghw/pkg/linuxpath" + "github.com/zededa/ghw/pkg/memory" "github.com/zededa/ghw/pkg/util" ) @@ -29,7 +32,9 @@ var ( ) func (i *Info) load(ctx context.Context) error { - i.Processors = processorsGet(ctx) + online, offline := logicalProcessorIDs(ctx) + procIDs, coreIDs := topologyOf(ctx, online) + i.Processors = processorsGet(ctx, online, procIDs, coreIDs) var totCores uint32 var totThreads uint32 for _, p := range i.Processors { @@ -40,23 +45,29 @@ func (i *Info) load(ctx context.Context) error { i.TotalHardwareThreads = totThreads // TODO(jaypipes): Remove TotalThreads before v1.0 i.TotalThreads = totThreads + ascending := slices.Clone(online) + slices.Sort(ascending) + i.LogicalProcessors = logicalProcessorsGet(ctx, ascending, procIDs, coreIDs) + i.OfflineLogicalProcessors = offline + caches, err := memory.Caches(ctx) + if err != nil { + log.Warn(ctx, "failed to read CPU caches: %s", err) + } + i.Caches = caches return nil } -func processorsGet(ctx context.Context) []*Processor { +// logicalProcessorIDs returns the IDs of the online logical processors, in the +// order /sys/devices/system/cpu lists them, and of the offline ones, ascending. +func logicalProcessorIDs(ctx context.Context) (online, offline []int) { paths := linuxpath.New(ctx) - - lps := logicalProcessorsFromProcCPUInfo(ctx) - // keyed by processor ID (physical_package_id) - procs := map[int]*Processor{} - // /sys/devices/system/cpu pseudodir contains N number of pseudodirs with // information about the logical processors on the host. These logical // processor pseudodirs are of the pattern /sys/devices/system/cpu/cpu{N} fnames, err := os.ReadDir(paths.SysDevicesSystemCPU) if err != nil { log.Warn(ctx, "failed to read /sys/devices/system/cpu: %s", err) - return []*Processor{} + return nil, nil } for _, fname := range fnames { matches := regexForCpulCore.FindStringSubmatch(fname.Name()) @@ -73,13 +84,26 @@ func processorsGet(ctx context.Context) []*Processor { onlineFilePath := filepath.Join(paths.SysDevicesSystemCPU, fmt.Sprintf("cpu%d", lpID), onlineFile) if _, err := os.Stat(onlineFilePath); err == nil { if util.SafeIntFromFile(ctx, onlineFilePath) == 0 { + offline = append(offline, lpID) continue } } else if errors.Is(err, os.ErrNotExist) { // Assume the CPU is online if the online state file doesn't exist // (as is the case with older snapshots) } - procID := processorIDFromLogicalProcessorID(ctx, lpID) + online = append(online, lpID) + } + sort.Ints(offline) + return online, offline +} + +func processorsGet(ctx context.Context, onlineLPs []int, procIDs, coreIDs map[int]int) []*Processor { + lps := logicalProcessorsFromProcCPUInfo(ctx) + // keyed by processor ID (physical_package_id) + procs := map[int]*Processor{} + + for _, lpID := range onlineLPs { + procID := procIDs[lpID] proc, found := procs[procID] if !found { proc = &Processor{ID: procID} @@ -138,8 +162,12 @@ func processorsGet(ctx context.Context) []*Processor { procs[procID] = proc } - coreID := coreIDFromLogicalProcessorID(ctx, lpID) - core := proc.CoreByID(coreID) + coreID := coreIDs[lpID] + var core *ProcessorCore + // An unknown core (-1) is not known to be shared: a core of its own. + if coreID != -1 { + core = proc.CoreByID(coreID) + } if core == nil { core = &ProcessorCore{ ID: coreID, @@ -174,19 +202,6 @@ func processorsGet(ctx context.Context) []*Processor { return res } -// processorIDFromLogicalProcessorID returns the processor physical package ID -// for the supplied logical processor ID -func processorIDFromLogicalProcessorID(ctx context.Context, lpID int) int { - paths := linuxpath.New(ctx) - // Fetch CPU ID - path := filepath.Join( - paths.SysDevicesSystemCPU, - fmt.Sprintf("cpu%d", lpID), - "topology", "physical_package_id", - ) - return util.SafeIntFromFile(ctx, path) -} - // coreIDFromLogicalProcessorID returns the core ID for the supplied logical // processor ID func coreIDFromLogicalProcessorID(ctx context.Context, lpID int) int { @@ -200,38 +215,91 @@ func coreIDFromLogicalProcessorID(ctx context.Context, lpID int) int { return util.SafeIntFromFile(ctx, path) } +// topologyOf returns the physical package and the core ID of each of the given +// logical processors, the core ID unique within its package. The kernel's +// core_id is that on x86, but an ARM device tree numbers cores per cluster, so +// core_id repeats within a package for cores that are not SMT siblings. Where +// one core_id covers more than one set of siblings (thread_siblings_list) in a +// package, every core of that package is numbered by the lowest logical +// processor among its siblings instead, and a logical processor whose siblings +// cannot be read gets -1. +func topologyOf(ctx context.Context, lpIDs []int) (procIDs, coreIDs map[int]int) { + paths := linuxpath.New(ctx) + type key struct{ proc, core int } + procIDs = make(map[int]int, len(lpIDs)) + coreIDs = make(map[int]int, len(lpIDs)) + firstSibling := map[int]int{} + allMinusOne := len(lpIDs) > 0 + for _, lpID := range lpIDs { + procIDs[lpID] = -1 + pkgPath := filepath.Join(paths.SysDevicesSystemCPU, fmt.Sprintf("cpu%d", lpID), + "topology", "physical_package_id") + if pkg, err := util.ReadInt(pkgPath); err != nil { + log.Warn(ctx, "failed to read the package of logical processor %d: %s", lpID, err) + allMinusOne = false + } else { + procIDs[lpID] = pkg + allMinusOne = allMinusOne && pkg == -1 + } + coreIDs[lpID] = coreIDFromLogicalProcessorID(ctx, lpID) + siblings, err := util.ReadCPUList(filepath.Join(paths.SysDevicesSystemCPU, + fmt.Sprintf("cpu%d", lpID), "topology", "thread_siblings_list")) + if err != nil || len(siblings) == 0 { + continue + } + firstSibling[lpID] = slices.Min(siblings) + } + // physical_package_id -1 from every CPU is the kernel saying it has no + // package information: one package. Next to CPUs with a real package it + // is unknown. + if allMinusOne { + for _, lpID := range lpIDs { + procIDs[lpID] = 0 + } + } + // Clusters are found per package, so only once the package IDs are final. + siblingSets := map[key]map[int]struct{}{} + for lpID, first := range firstSibling { + k := key{procIDs[lpID], coreIDs[lpID]} + if siblingSets[k] == nil { + siblingSets[k] = map[int]struct{}{} + } + siblingSets[k][first] = struct{}{} + } + clustered := map[int]bool{} + for k, sets := range siblingSets { + if len(sets) > 1 { + clustered[k.proc] = true + } + } + for _, lpID := range lpIDs { + if !clustered[procIDs[lpID]] { + continue + } + if first, ok := firstSibling[lpID]; ok { + coreIDs[lpID] = first + } else { + coreIDs[lpID] = -1 + } + } + return procIDs, coreIDs +} + func CoresForNode(ctx context.Context, nodeID int) ([]*ProcessorCore, error) { // The /sys/devices/system/node/nodeX directory contains a subdirectory - // called 'cpuX' for each logical processor assigned to the node. Each of - // those subdirectories contains a topology subdirectory which has a - // core_id file that indicates the 0-based identifier of the physical core - // the logical processor (hardware thread) is on. + // called 'cpuX' for each logical processor assigned to the node. Cores are + // identified as for Info: see topologyOf. paths := linuxpath.New(ctx) path := filepath.Join( paths.SysDevicesSystemNode, fmt.Sprintf("node%d", nodeID), ) - cores := make([]*ProcessorCore, 0) - - findCoreByID := func(coreID int) *ProcessorCore { - for _, c := range cores { - if c.ID == coreID { - return c - } - } - - c := &ProcessorCore{ - ID: coreID, - LogicalProcessors: []int{}, - } - cores = append(cores, c) - return c - } files, err := os.ReadDir(path) if err != nil { return nil, err } + var lpIDs []int for _, file := range files { filename := file.Name() if !strings.HasPrefix(filename, "cpu") { @@ -246,7 +314,7 @@ func CoresForNode(ctx context.Context, nodeID int) ([]*ProcessorCore, error) { // Grab the logical processor ID by cutting the integer from the // /sys/devices/system/node/nodeX/cpuX filename cpuPath := filepath.Join(path, filename) - procID, err := strconv.Atoi(filename[3:]) + lpID, err := strconv.Atoi(filename[3:]) if err != nil { log.Warn(ctx, "failed to determine procID from %s. Expected integer after 3rd char.", @@ -263,13 +331,32 @@ func CoresForNode(ctx context.Context, nodeID int) ([]*ProcessorCore, error) { // Assume the CPU is online if the online state file doesn't exist // (as is the case with older snapshots) } - coreIDPath := filepath.Join(cpuPath, "topology", "core_id") - coreID := util.SafeIntFromFile(ctx, coreIDPath) - core := findCoreByID(coreID) - core.LogicalProcessors = append( - core.LogicalProcessors, - procID, - ) + lpIDs = append(lpIDs, lpID) + } + + // A node can hold more than one package, so a core is a package and a + // core ID. Whether a package is clustered depends on all its CPUs, not + // only the node's, or the node's cores would be numbered unlike Info's. + online, _ := logicalProcessorIDs(ctx) + procIDs, coreIDs := topologyOf(ctx, online) + type key struct{ proc, core int } + byKey := map[key]*ProcessorCore{} + cores := make([]*ProcessorCore, 0) + for _, lpID := range lpIDs { + k := key{procIDs[lpID], coreIDs[lpID]} + core, ok := byKey[k] + if !ok { + core = &ProcessorCore{ + ID: k.core, + LogicalProcessors: []int{}, + } + // As in Info, an unknown core is a core of its own. + if k.core != -1 { + byKey[k] = core + } + cores = append(cores, core) + } + core.LogicalProcessors = append(core.LogicalProcessors, lpID) } for _, c := range cores { @@ -419,3 +506,140 @@ func logicalProcessorsFromProcCPUInfo( } return lps } + +// logicalProcessorsGet describes each of the given online logical processors. +func logicalProcessorsGet(ctx context.Context, lpIDs []int, procIDs, coreIDs map[int]int) []*LogicalProcessor { + paths := linuxpath.New(ctx) + nodeOf := nodesOf(ctx) + types := coreTypesOf(ctx, lpIDs) + lps := make([]*LogicalProcessor, 0, len(lpIDs)) + for _, id := range lpIDs { + dir := filepath.Join(paths.SysDevicesSystemCPU, fmt.Sprintf("cpu%d", id)) + lp := &LogicalProcessor{ + ID: id, + ProcessorID: procIDs[id], + CoreID: coreIDs[id], + NodeID: -1, + CoreType: types[id], + } + // A frequency the kernel does not report stays 0. + if khz, err := util.ReadInt(filepath.Join(dir, "cpufreq", "base_frequency")); err == nil && khz > 0 { + lp.BaseFrequencyKHz = uint64(khz) + } + if khz, err := util.ReadInt(filepath.Join(dir, "cpufreq", "cpuinfo_max_freq")); err == nil && khz > 0 { + lp.MaxFrequencyKHz = uint64(khz) + } + if nodeOf == nil { + lp.NodeID = 0 + } else if node, ok := nodeOf[id]; ok { + lp.NodeID = node + } + lps = append(lps, lp) + } + return lps +} + +// nodesOf maps each logical processor to its NUMA node, from the nodes' +// cpulist files. It is nil when the kernel has no NUMA support (no node +// directory); a logical processor no node lists is missing from the map. +func nodesOf(ctx context.Context) map[int]int { + paths := linuxpath.New(ctx) + entries, err := os.ReadDir(paths.SysDevicesSystemNode) + if err != nil { + if !errors.Is(err, os.ErrNotExist) { + log.Warn(ctx, "failed to read %s: %s", paths.SysDevicesSystemNode, err) + return map[int]int{} + } + return nil + } + nodeOf := map[int]int{} + for _, entry := range entries { + node, err := strconv.Atoi(strings.TrimPrefix(entry.Name(), "node")) + if err != nil || !strings.HasPrefix(entry.Name(), "node") { + continue + } + cpus, err := util.ReadCPUList(filepath.Join(paths.SysDevicesSystemNode, entry.Name(), "cpulist")) + if err != nil { + log.Warn(ctx, "failed to read the CPUs of NUMA node %d: %s", node, err) + continue + } + for _, cpu := range cpus { + nodeOf[cpu] = node + } + } + return nodeOf +} + +// coreTypesOf tells each logical processor's core type. Intel hybrid parts list +// their performance cores under the cpu_core PMU and their efficiency cores +// under cpu_atom and, for the low-power ones, cpu_lowpower. Elsewhere, ARM +// big.LITTLE among them, cpu_capacity ranks the logical processors: the highest +// capacity is a performance core, any lower an efficiency core, and one +// capacity for all of them a homogeneous part (x86 reports 1024 for every CPU +// of a homogeneous part since Linux 6.16). Before that, a homogeneous x86 part +// is told by its single core PMU, /sys/devices/cpu. When none of these tells, +// or only some CPUs report a capacity, the type is unknown, never guessed. +// +// The ranking sees only the online CPUs: with every big core offline, the +// LITTLE ones read as performance or uniform. And it ranks by capacity alone, +// so on a part with three classes of core (prime, big, LITTLE) the middle one +// reads as efficiency. +func coreTypesOf(ctx context.Context, lpIDs []int) map[int]CoreType { + paths := linuxpath.New(ctx) + devices := filepath.Join(paths.SysRoot, "devices") + types := map[int]CoreType{} + + var hybrid bool + for pmu, coreType := range map[string]CoreType{ + "cpu_core": CoreTypePerformance, + "cpu_atom": CoreTypeEfficiency, + "cpu_lowpower": CoreTypeEfficiency, + } { + cpus, err := util.ReadCPUList(filepath.Join(devices, pmu, "cpus")) + if err != nil { + continue + } + hybrid = true + for _, id := range cpus { + types[id] = coreType + } + } + if hybrid { + return types + } + + capacities := map[int]int{} + for _, id := range lpIDs { + capacity, err := util.ReadInt(filepath.Join(paths.SysDevicesSystemCPU, fmt.Sprintf("cpu%d", id), "cpu_capacity")) + if err == nil { + capacities[id] = capacity + } + } + if len(capacities) > 0 && len(capacities) < len(lpIDs) { + return types + } + if len(capacities) > 0 { + lowest, highest := math.MaxInt, math.MinInt + for _, capacity := range capacities { + lowest, highest = min(lowest, capacity), max(highest, capacity) + } + for id, capacity := range capacities { + switch { + case lowest == highest: + types[id] = CoreTypeUniform + case capacity == highest: + types[id] = CoreTypePerformance + default: + types[id] = CoreTypeEfficiency + } + } + return types + } + + if fi, err := os.Stat(filepath.Join(devices, "cpu")); err == nil && fi.IsDir() { + for _, id := range lpIDs { + types[id] = CoreTypeUniform + } + } + return types +} diff --git a/pkg/cpu/topology_linux_test.go b/pkg/cpu/topology_linux_test.go new file mode 100644 index 00000000..f72fbc2f --- /dev/null +++ b/pkg/cpu/topology_linux_test.go @@ -0,0 +1,747 @@ +// +// Use and distribution licensed under the Apache license version 2. +// +// See the COPYING file in the root project directory for full text. +// + +package cpu_test + +import ( + "encoding/json" + "fmt" + "os" + "path/filepath" + "reflect" + "slices" + "sort" + "strconv" + "strings" + "testing" + + "github.com/zededa/ghw/internal/config" + "github.com/zededa/ghw/internal/sysfsfixture" + "github.com/zededa/ghw/pkg/cpu" + "github.com/zededa/ghw/pkg/memory" + "github.com/zededa/ghw/pkg/snapshot" + "github.com/zededa/ghw/testdata" +) + +// coresOf returns each core's logical processors, keyed by core ID, for the +// processor with the given ID. +func coresOf(t *testing.T, info *cpu.Info, procID int) map[int][]int { + t.Helper() + for _, p := range info.Processors { + if p.ID != procID { + continue + } + cores := map[int][]int{} + for _, c := range p.Cores { + if _, dup := cores[c.ID]; dup { + t.Fatalf("processor %d has two cores with ID %d", procID, c.ID) + } + cores[c.ID] = c.LogicalProcessors + } + return cores + } + t.Fatalf("no processor %d in %v", procID, info.Processors) + return nil +} + +// An ARM device tree numbers cores per cluster, so core_id repeats within one +// package; the sibling lists tell the cores apart. +func TestCoresAcrossClusters(t *testing.T) { + var cpus []sysfsfixture.CPU + for id := 0; id < 8; id++ { + cpus = append(cpus, sysfsfixture.CPU{ + ID: id, + Package: "0", + Core: strconv.Itoa(id % 4), + Siblings: strconv.Itoa(id), + }) + } + root := sysfsfixture.Write(t, sysfsfixture.Tree{CPUs: cpus}) + + info, err := cpu.New(config.WithChroot(root)) + if err != nil { + t.Fatal(err) + } + want := map[int][]int{0: {0}, 1: {1}, 2: {2}, 3: {3}, 4: {4}, 5: {5}, 6: {6}, 7: {7}} + if got := coresOf(t, info, 0); !reflect.DeepEqual(got, want) { + t.Errorf("cores = %v, want %v", got, want) + } + if info.TotalCores != 8 { + t.Errorf("TotalCores = %d, want 8", info.TotalCores) + } +} + +// Where core_id is unique within the package, as on x86, it is kept. +func TestCoreIDsKeptOnX86(t *testing.T) { + root := sysfsfixture.Write(t, sysfsfixture.Tree{CPUs: []sysfsfixture.CPU{ + {ID: 0, Package: "0", Core: "0", Siblings: "0,2"}, + {ID: 1, Package: "0", Core: "1", Siblings: "1,3"}, + {ID: 2, Package: "0", Core: "0", Siblings: "0,2"}, + {ID: 3, Package: "0", Core: "1", Siblings: "1,3"}, + {ID: 4, Package: "1", Core: "0", Siblings: "4"}, + }}) + + info, err := cpu.New(config.WithChroot(root)) + if err != nil { + t.Fatal(err) + } + if got, want := coresOf(t, info, 0), map[int][]int{0: {0, 2}, 1: {1, 3}}; !reflect.DeepEqual(got, want) { + t.Errorf("package 0 cores = %v, want %v", got, want) + } + if got, want := coresOf(t, info, 1), map[int][]int{0: {4}}; !reflect.DeepEqual(got, want) { + t.Errorf("package 1 cores = %v, want %v", got, want) + } +} + +// In a renumbered package, a CPU whose siblings cannot be read has an unknown +// core rather than its kernel core_id, which another core now uses. Unknown +// cores are not known to be shared, so each is a core of its own. +func TestClusteredPackageCPUsWithoutSiblings(t *testing.T) { + var cpus []sysfsfixture.CPU + for id := 0; id < 8; id++ { + cpus = append(cpus, sysfsfixture.CPU{ + ID: id, Package: "0", Core: strconv.Itoa(id % 4), Siblings: strconv.Itoa(id), + }) + } + cpus[5].Siblings, cpus[6].Siblings = "", "" + root := sysfsfixture.Write(t, sysfsfixture.Tree{CPUs: cpus, Nodes: map[int]string{0: "0-7"}}) + info, err := cpu.New(config.WithChroot(root)) + if err != nil { + t.Fatal(err) + } + cores, err := cpu.CoresForNode(config.ContextFromArgs(config.WithChroot(root)), 0) + if err != nil { + t.Fatal(err) + } + want := []string{"0:[0]", "1:[1]", "2:[2]", "3:[3]", "4:[4]", "-1:[5]", "-1:[6]", "7:[7]"} + if got := coreList(info.Processors[0].Cores); !reflect.DeepEqual(got, want) { + t.Errorf("Info cores = %v, want %v", got, want) + } + if got := coreList(cores); !reflect.DeepEqual(got, want) { + t.Errorf("CoresForNode = %v, want %v", got, want) + } +} + +// CPUs whose core_id and siblings cannot be read are not merged into one core +// either. +func TestCPUsWithoutCoreID(t *testing.T) { + cpus := []sysfsfixture.CPU{ + {ID: 0, Package: "0", Core: "0", Siblings: "0"}, + {ID: 1, Package: "0"}, + {ID: 2, Package: "0"}, + } + info, err := cpu.New(config.WithChroot(sysfsfixture.Write(t, sysfsfixture.Tree{CPUs: cpus}))) + if err != nil { + t.Fatal(err) + } + want := []string{"0:[0]", "-1:[1]", "-1:[2]"} + if got := coreList(info.Processors[0].Cores); !reflect.DeepEqual(got, want) { + t.Errorf("cores = %v, want %v", got, want) + } +} + +// coreList lists cores as "ID:[logical processors]", by first logical processor. +func coreList(cores []*cpu.ProcessorCore) []string { + sorted := slices.Clone(cores) + slices.SortFunc(sorted, func(a, b *cpu.ProcessorCore) int { + return a.LogicalProcessors[0] - b.LogicalProcessors[0] + }) + var out []string + for _, c := range sorted { + out = append(out, fmt.Sprintf("%d:%v", c.ID, c.LogicalProcessors)) + } + return out +} + +// Renumbering is per package: a package whose core_id is unique keeps it. +func TestOneOfTwoPackagesClustered(t *testing.T) { + root := sysfsfixture.Write(t, sysfsfixture.Tree{CPUs: []sysfsfixture.CPU{ + {ID: 0, Package: "0", Core: "0", Siblings: "0"}, + {ID: 1, Package: "0", Core: "1", Siblings: "1"}, + {ID: 2, Package: "0", Core: "0", Siblings: "2"}, + {ID: 3, Package: "0", Core: "1", Siblings: "3"}, + {ID: 4, Package: "1", Core: "0", Siblings: "4-5"}, + {ID: 5, Package: "1", Core: "0", Siblings: "4-5"}, + {ID: 6, Package: "1", Core: "1", Siblings: "6-7"}, + {ID: 7, Package: "1", Core: "1", Siblings: "6-7"}, + }}) + info, err := cpu.New(config.WithChroot(root)) + if err != nil { + t.Fatal(err) + } + if got, want := coresOf(t, info, 0), map[int][]int{0: {0}, 1: {1}, 2: {2}, 3: {3}}; !reflect.DeepEqual(got, want) { + t.Errorf("package 0 cores = %v, want %v", got, want) + } + if got, want := coresOf(t, info, 1), map[int][]int{0: {4, 5}, 1: {6, 7}}; !reflect.DeepEqual(got, want) { + t.Errorf("package 1 cores = %v, want %v", got, want) + } +} + +// CoresForNode identifies cores as Info does: clusters are told apart. +func TestCoresForNodeAcrossClusters(t *testing.T) { + var cpus []sysfsfixture.CPU + for id := 0; id < 8; id++ { + cpus = append(cpus, sysfsfixture.CPU{ + ID: id, Package: "0", Core: strconv.Itoa(id % 4), Siblings: strconv.Itoa(id), + }) + } + ctx := config.ContextFromArgs(config.WithChroot(sysfsfixture.Write(t, + sysfsfixture.Tree{CPUs: cpus, Nodes: map[int]string{0: "0-7"}}))) + cores, err := cpu.CoresForNode(ctx, 0) + if err != nil { + t.Fatal(err) + } + if len(cores) != 8 { + t.Errorf("CoresForNode = %v, want 8 single-thread cores", cores) + } +} + +// When a package's clusters are separate NUMA nodes, CoresForNode numbers each +// node's cores as Info does: the package is clustered whichever subset of its +// CPUs is asked for. +func TestCoresForNodeClustersAsNodes(t *testing.T) { + var cpus []sysfsfixture.CPU + for id := 0; id < 8; id++ { + cpus = append(cpus, sysfsfixture.CPU{ + ID: id, Package: "0", Core: strconv.Itoa(id % 4), Siblings: strconv.Itoa(id), + }) + } + root := sysfsfixture.Write(t, sysfsfixture.Tree{CPUs: cpus, Nodes: map[int]string{0: "0-3", 1: "4-7"}}) + info, err := cpu.New(config.WithChroot(root)) + if err != nil { + t.Fatal(err) + } + infoCores := map[int][]int{} + for _, p := range info.Processors { + for _, c := range p.Cores { + infoCores[c.ID] = c.LogicalProcessors + } + } + ctx := config.ContextFromArgs(config.WithChroot(root)) + for _, node := range []int{0, 1} { + cores, err := cpu.CoresForNode(ctx, node) + if err != nil { + t.Fatal(err) + } + for _, c := range cores { + if !reflect.DeepEqual(infoCores[c.ID], c.LogicalProcessors) { + t.Errorf("node %d: core %d has %v, Info's core %d has %v", + node, c.ID, c.LogicalProcessors, c.ID, infoCores[c.ID]) + } + } + } +} + +// Two packages in one NUMA node reuse core IDs; their cores stay apart. +func TestCoresForNodeTwoPackages(t *testing.T) { + tree := sysfsfixture.Tree{ + CPUs: []sysfsfixture.CPU{ + {ID: 0, Package: "0", Core: "0", Siblings: "0"}, + {ID: 1, Package: "1", Core: "0", Siblings: "1"}, + }, + Nodes: map[int]string{0: "0-1"}, + } + ctx := config.ContextFromArgs(config.WithChroot(sysfsfixture.Write(t, tree))) + cores, err := cpu.CoresForNode(ctx, 0) + if err != nil { + t.Fatal(err) + } + if len(cores) != 2 { + t.Errorf("CoresForNode = %v, want 2 cores", cores) + } +} + +// A 13th-gen Core i5-1335UE: two P-cores with two threads each (CPUs 0-3, +// under the cpu_core PMU) and eight E-cores (CPUs 4-11, under cpu_atom), each +// module of four E-cores sharing an L2. Turbo is off on that machine, so each +// CPU's maximum frequency is its base frequency. +func TestHybridPMUSnapshot(t *testing.T) { + info, err := cpu.New(config.WithChroot(unpackSnapshot(t, "linux-amd64-intel-i5-1335UE.tar.gz"))) + if err != nil { + t.Fatal(err) + } + if info.TotalCores != 10 || info.TotalHardwareThreads != 12 { + t.Errorf("TotalCores, TotalHardwareThreads = %d, %d; want 10, 12", + info.TotalCores, info.TotalHardwareThreads) + } + var lps []string + for _, lp := range info.LogicalProcessors { + lps = append(lps, fmt.Sprintf("%d:%d:%s:%d", lp.ID, lp.CoreID, lp.CoreType, lp.MaxFrequencyKHz)) + } + wantLPs := []string{ + "0:0:performance:800000", "1:0:performance:800000", + "2:4:performance:800000", "3:4:performance:800000", + "4:8:efficiency:600000", "5:9:efficiency:600000", + "6:10:efficiency:600000", "7:11:efficiency:600000", + "8:12:efficiency:600000", "9:13:efficiency:600000", + "10:14:efficiency:600000", "11:15:efficiency:600000", + } + if !reflect.DeepEqual(lps, wantLPs) { + t.Errorf("logical processors (ID:core:type:max kHz) =\n%v\nwant\n%v", lps, wantLPs) + } + var caches []string + for _, c := range info.Caches { + if c.Level == 2 || c.Type == memory.CacheTypeData { + caches = append(caches, fmt.Sprintf("L%d %s %dK %v", c.Level, c.Type, c.SizeBytes/1024, c.LogicalProcessors)) + } + } + wantCaches := []string{ + "L1 Data 48K [0 1]", "L1 Data 48K [2 3]", + "L1 Data 32K [4]", "L1 Data 32K [5]", "L1 Data 32K [6]", "L1 Data 32K [7]", + "L1 Data 32K [8]", "L1 Data 32K [9]", "L1 Data 32K [10]", "L1 Data 32K [11]", + "L2 Unified 1280K [0 1]", "L2 Unified 1280K [2 3]", + "L2 Unified 2048K [4 5 6 7]", "L2 Unified 2048K [8 9 10 11]", + } + if !reflect.DeepEqual(caches, wantCaches) { + t.Errorf("L1d and L2 caches =\n%v\nwant\n%v", caches, wantCaches) + } +} + +// An NVIDIA Jetson AGX Orin: its kernel reports each cluster of four cores as +// a package, so core IDs repeat across packages but not within one and are +// kept. Every CPU has capacity 1024: a homogeneous part. The kernel numbers no +// cache, so cache instances are told apart by the CPUs sharing them. The CPU +// model comes from the DeviceTree. +func TestARMClusterPackagesSnapshot(t *testing.T) { + info, err := cpu.New(config.WithChroot(unpackSnapshot(t, "linux-arm64-nvidia-jetson-agx-orin.tar.gz"))) + if err != nil { + t.Fatal(err) + } + if len(info.Processors) != 3 { + t.Fatalf("Processors = %v, want 3", info.Processors) + } + for _, p := range info.Processors { + if p.Model != "nvidia,tegra234" { + t.Errorf("package %d model = %q, want the SoC from the DeviceTree", p.ID, p.Model) + } + first := p.ID * 4 + want := []string{ + fmt.Sprintf("0:[%d]", first), fmt.Sprintf("1:[%d]", first+1), + fmt.Sprintf("2:[%d]", first+2), fmt.Sprintf("3:[%d]", first+3), + } + if got := coreList(p.Cores); !reflect.DeepEqual(got, want) { + t.Errorf("package %d cores = %v, want %v", p.ID, got, want) + } + } + for _, lp := range info.LogicalProcessors { + if lp.ProcessorID != lp.ID/4 || lp.CoreID != lp.ID%4 || lp.CoreType != cpu.CoreTypeUniform || lp.MaxFrequencyKHz != 2201600 { + t.Errorf("logical processor %+v", lp) + } + } + var l3 []string + for _, c := range info.Caches { + if c.ID != -1 { + t.Errorf("cache %+v has a kernel ID", c) + } + if c.Level == 3 { + l3 = append(l3, fmt.Sprintf("%dK %v", c.SizeBytes/1024, c.LogicalProcessors)) + } + } + if want := []string{"2048K [0 1 2 3]", "2048K [4 5 6 7]", "2048K [8 9 10 11]"}; !reflect.DeepEqual(l3, want) { + t.Errorf("L3 caches = %v, want %v", l3, want) + } + if len(info.Caches) != 39 { + t.Errorf("%d caches, want 39: L1i, L1d and L2 per CPU and an L3 per cluster", len(info.Caches)) + } +} + +// On the i7-1270P the E-cores 8-11 share an L2 but are four cores of their own. +func TestHybridSnapshotCores(t *testing.T) { + info, err := cpu.New(config.WithChroot(unpackSnapshot(t, "linux-amd64-intel-i7-1270P.tar.gz"))) + if err != nil { + t.Fatal(err) + } + cores := map[int][]int{} + for _, p := range info.Processors { + for _, c := range p.Cores { + cores[c.ID] = c.LogicalProcessors + } + } + for _, lp := range []int{8, 9, 10, 11} { + found := false + for _, lps := range cores { + if reflect.DeepEqual(lps, []int{lp}) { + found = true + } + } + if !found { + t.Errorf("E-core CPU %d is not a core of its own: %v", lp, cores) + } + } + if info.TotalCores != 12 || info.TotalHardwareThreads != 16 { + t.Errorf("TotalCores, TotalHardwareThreads = %d, %d; want 12, 16", + info.TotalCores, info.TotalHardwareThreads) + } +} + +func unpackSnapshot(t *testing.T, name string) string { + t.Helper() + dir, err := testdata.SnapshotsDirectory() + if err != nil { + t.Fatal(err) + } + root := t.TempDir() + if err := snapshot.UnpackInto(filepath.Join(dir, name), root); err != nil { + t.Fatal(err) + } + return root +} + +// twoSocketTree is 2 packages x 2 cores x 2 threads, one NUMA node per +// package, with frequencies on CPU 0 only. +func twoSocketTree() sysfsfixture.Tree { + var cpus []sysfsfixture.CPU + for id := 0; id < 8; id++ { + pkg, core := id/4, (id%4)/2 + first := pkg*4 + core*2 + c := sysfsfixture.CPU{ + ID: id, + Package: strconv.Itoa(pkg), + Core: strconv.Itoa(core), + Siblings: strconv.Itoa(first) + "-" + strconv.Itoa(first+1), + } + if id == 0 { + c.BaseKHz, c.MaxKHz = "2100000", "3500000" + } + cpus = append(cpus, c) + } + return sysfsfixture.Tree{CPUs: cpus, Nodes: map[int]string{0: "0-3", 1: "4-7"}} +} + +func logicalProcessors(t *testing.T, tree sysfsfixture.Tree) *cpu.Info { + t.Helper() + info, err := cpu.New(config.WithChroot(sysfsfixture.Write(t, tree))) + if err != nil { + t.Fatal(err) + } + return info +} + +func TestLogicalProcessors(t *testing.T) { + info := logicalProcessors(t, twoSocketTree()) + var want []*cpu.LogicalProcessor + for id := 0; id < 8; id++ { + lp := &cpu.LogicalProcessor{ID: id, ProcessorID: id / 4, CoreID: (id % 4) / 2, NodeID: id / 4} + if id == 0 { + lp.BaseFrequencyKHz, lp.MaxFrequencyKHz = 2100000, 3500000 + } + want = append(want, lp) + } + if !reflect.DeepEqual(info.LogicalProcessors, want) { + t.Errorf("LogicalProcessors =\n%v\nwant\n%v", info.LogicalProcessors, want) + } + if info.OfflineLogicalProcessors != nil { + t.Errorf("OfflineLogicalProcessors = %v, want none", info.OfflineLogicalProcessors) + } +} + +// A kernel without NUMA has no node directory: every CPU is on node 0. +func TestLogicalProcessorsNoNUMA(t *testing.T) { + tree := twoSocketTree() + tree.Nodes = nil + for _, lp := range logicalProcessors(t, tree).LogicalProcessors { + if lp.NodeID != 0 { + t.Errorf("CPU %d NodeID = %d, want 0", lp.ID, lp.NodeID) + } + } +} + +// A CPU no node lists is on an unknown node, not on node 0. +func TestLogicalProcessorsNUMAHole(t *testing.T) { + tree := twoSocketTree() + tree.Nodes = map[int]string{0: "0-3", 1: "4-6"} + for _, lp := range logicalProcessors(t, tree).LogicalProcessors { + want := lp.ID / 4 + if lp.ID == 7 { + want = -1 + } + if lp.NodeID != want { + t.Errorf("CPU %d NodeID = %d, want %d", lp.ID, lp.NodeID, want) + } + } +} + +// A CPU whose topology cannot be read has unknown package and core. +func TestLogicalProcessorsUnreadableTopology(t *testing.T) { + tree := twoSocketTree() + tree.CPUs[2].Package, tree.CPUs[2].Core, tree.CPUs[2].Siblings = "", "", "" + for _, lp := range logicalProcessors(t, tree).LogicalProcessors { + wantPkg, wantCore := lp.ID/4, (lp.ID%4)/2 + if lp.ID == 2 { + wantPkg, wantCore = -1, -1 + } + if lp.ProcessorID != wantPkg || lp.CoreID != wantCore { + t.Errorf("CPU %d = package %d core %d, want package %d core %d", + lp.ID, lp.ProcessorID, lp.CoreID, wantPkg, wantCore) + } + } +} + +// physical_package_id -1 is the kernel saying there is no package +// information: one package, 0. +func TestPackageIDMinusOne(t *testing.T) { + tree := twoSocketTree() + for i := range tree.CPUs { + tree.CPUs[i].Package = "-1" + } + tree.CPUs = tree.CPUs[:4] + info := logicalProcessors(t, tree) + for _, lp := range info.LogicalProcessors { + if lp.ProcessorID != 0 { + t.Errorf("CPU %d ProcessorID = %d, want 0", lp.ID, lp.ProcessorID) + } + } + if len(info.Processors) != 1 || info.Processors[0].ID != 0 { + t.Errorf("Processors = %v, want one with ID 0", info.Processors) + } +} + +// A device-tree board with no package information and core_id repeating per +// cluster is still renumbered: its one package, 0, is clustered. +func TestPackageIDMinusOneClusters(t *testing.T) { + var cpus []sysfsfixture.CPU + for id := 0; id < 8; id++ { + cpus = append(cpus, sysfsfixture.CPU{ + ID: id, Package: "-1", Core: strconv.Itoa(id % 4), Siblings: strconv.Itoa(id), + }) + } + info := logicalProcessors(t, sysfsfixture.Tree{CPUs: cpus}) + want := []string{"0:[0]", "1:[1]", "2:[2]", "3:[3]", "4:[4]", "5:[5]", "6:[6]", "7:[7]"} + if len(info.Processors) != 1 || info.Processors[0].ID != 0 { + t.Fatalf("Processors = %v, want one with ID 0", info.Processors) + } + if got := coreList(info.Processors[0].Cores); !reflect.DeepEqual(got, want) { + t.Errorf("cores = %v, want %v", got, want) + } +} + +func TestOfflineLogicalProcessors(t *testing.T) { + tree := twoSocketTree() + tree.CPUs = tree.CPUs[:4] + tree.CPUs[2].Offline, tree.CPUs[3].Offline = true, true + info := logicalProcessors(t, tree) + if want := []int{2, 3}; !reflect.DeepEqual(info.OfflineLogicalProcessors, want) { + t.Errorf("OfflineLogicalProcessors = %v, want %v", info.OfflineLogicalProcessors, want) + } + var ids []int + for _, lp := range info.LogicalProcessors { + ids = append(ids, lp.ID) + } + if want := []int{0, 1}; !reflect.DeepEqual(ids, want) { + t.Errorf("LogicalProcessors = %v, want %v", ids, want) + } +} + +func TestInfoCaches(t *testing.T) { + tree := twoSocketTree() + for i := range tree.CPUs { + tree.CPUs[i].Caches = []sysfsfixture.Cache{ + {Index: 3, Level: "3", Type: "Unified", ID: strconv.Itoa(i / 4), Size: "16384K"}, + } + } + info := logicalProcessors(t, tree) + if len(info.Caches) != 2 { + t.Fatalf("Caches = %v, want two L3 instances", info.Caches) + } + for p, c := range info.Caches { + want := []uint32{uint32(p * 4), uint32(p*4 + 1), uint32(p*4 + 2), uint32(p*4 + 3)} + if c.Level != 3 || c.ID != p || !reflect.DeepEqual(c.LogicalProcessors, want) { + t.Errorf("Caches[%d] = %+v, want L3 %d of %v", p, c, p, want) + } + } +} + +// The new fields come in addition to the existing ones, which keep their +// values. +func TestInfoJSONKeepsOldFields(t *testing.T) { + info := logicalProcessors(t, twoSocketTree()) + var got map[string]any + if err := json.Unmarshal([]byte(info.JSONString(false)), &got); err != nil { + t.Fatal(err) + } + fields, _ := got["cpu"].(map[string]any) + for key, want := range map[string]float64{"total_cores": 4, "total_hardware_threads": 8} { + if fields[key] != want { + t.Errorf("%s = %v, want %v", key, fields[key], want) + } + } + for _, key := range []string{"processors", "logical_processors"} { + if _, ok := fields[key]; !ok { + t.Errorf("JSON has no %q: %v", key, fields) + } + } +} + +// physical_package_id -1 means "no package information" only when every CPU +// says so; next to CPUs with a real package it is unknown. +func TestPackageIDMixedMinusOne(t *testing.T) { + tree := twoSocketTree() + tree.CPUs = tree.CPUs[:4] + tree.CPUs[0].Package, tree.CPUs[1].Package = "-1", "-1" + tree.CPUs[2].Package, tree.CPUs[3].Package = "1", "1" + for _, lp := range logicalProcessors(t, tree).LogicalProcessors { + want := 1 + if lp.ID < 2 { + want = -1 + } + if lp.ProcessorID != want { + t.Errorf("CPU %d ProcessorID = %d, want %d", lp.ID, lp.ProcessorID, want) + } + } +} + +// On the x86 snapshots, Processors and their Cores are what they were before +// this package learned about clusters: cores grouped by (package, core_id), +// in the order the CPU directories are listed. +func TestSnapshotProcessorsUnchanged(t *testing.T) { + dir, err := testdata.SnapshotsDirectory() + if err != nil { + t.Fatal(err) + } + names, err := filepath.Glob(filepath.Join(dir, "linux-amd64-*.tar.gz")) + if err != nil { + t.Fatal(err) + } + for _, name := range names { + t.Run(filepath.Base(name), func(t *testing.T) { + root := unpackSnapshot(t, filepath.Base(name)) + cpuDir := filepath.Join(root, "sys", "devices", "system", "cpu") + want := legacyProcessors(t, cpuDir) + if want == nil { + t.Skip("no CPU topology in this snapshot") + } + info, err := cpu.New(config.WithChroot(root)) + if err != nil { + t.Fatal(err) + } + got := map[int][][2]any{} + for _, p := range info.Processors { + for _, c := range p.Cores { + got[p.ID] = append(got[p.ID], [2]any{c.ID, c.LogicalProcessors}) + } + } + if !reflect.DeepEqual(got, want) { + t.Errorf("processors =\n%v\nwant\n%v", got, want) + } + }) + } +} + +func coreTypes(t *testing.T, tree sysfsfixture.Tree) []cpu.CoreType { + t.Helper() + var types []cpu.CoreType + for _, lp := range logicalProcessors(t, tree).LogicalProcessors { + types = append(types, lp.CoreType) + } + return types +} + +func fourCPUs(capacities ...string) sysfsfixture.Tree { + tree := twoSocketTree() + tree.CPUs = tree.CPUs[:4] + for i, c := range capacities { + tree.CPUs[i].Capacity = c + } + return tree +} + +func TestCoreType(t *testing.T) { + P, E, U, X := cpu.CoreTypePerformance, cpu.CoreTypeEfficiency, cpu.CoreTypeUniform, cpu.CoreTypeUnknown + hybrid := fourCPUs() + hybrid.PMUCore, hybrid.PMUAtom = "0-1", "2" + pmuOnly := fourCPUs() + pmuOnly.CorePMU = true + partialPMU := fourCPUs("1024") + partialPMU.CorePMU = true + lowPower := fourCPUs() + lowPower.PMUCore, lowPower.PMULowPower = "0-1", "2-3" + for name, tc := range map[string]struct { + tree sysfsfixture.Tree + want []cpu.CoreType + }{ + // CPU 3 is in neither list: not guessed. + "Intel hybrid PMUs": {hybrid, []cpu.CoreType{P, P, E, X}}, + "Intel low-power": {lowPower, []cpu.CoreType{P, P, E, E}}, + "big.LITTLE capacity": {fourCPUs("1024", "1024", "446", "446"), []cpu.CoreType{P, P, E, E}}, + "uniform capacity": {fourCPUs("1024", "1024", "1024", "1024"), []cpu.CoreType{U, U, U, U}}, + "core PMU only": {pmuOnly, []cpu.CoreType{U, U, U, U}}, + "nothing to tell": {fourCPUs(), []cpu.CoreType{X, X, X, X}}, + "partial capacity": {fourCPUs("1024"), []cpu.CoreType{X, X, X, X}}, + // Some CPUs report a capacity: the core PMU does not make them uniform. + "partial capacity with core PMU": {partialPMU, []cpu.CoreType{X, X, X, X}}, + } { + t.Run(name, func(t *testing.T) { + if got := coreTypes(t, tc.tree); !reflect.DeepEqual(got, tc.want) { + t.Errorf("core types = %v, want %v", got, tc.want) + } + }) + } +} + +// legacyProcessors groups the online CPUs of a sysfs cpu directory by their +// raw (physical_package_id, core_id), cores in directory order, as ghw did +// before. +func legacyProcessors(t *testing.T, cpuDir string) map[int][][2]any { + t.Helper() + entries, err := os.ReadDir(cpuDir) + if err != nil { + return nil + } + type core struct { + id int + lps []int + } + cores := map[int][]*core{} + for _, e := range entries { + lp, err := strconv.Atoi(strings.TrimPrefix(e.Name(), "cpu")) + if err != nil || !strings.HasPrefix(e.Name(), "cpu") { + continue + } + if online, err := os.ReadFile(filepath.Join(cpuDir, e.Name(), "online")); err == nil && strings.TrimSpace(string(online)) == "0" { + continue + } + pkg, err1 := os.ReadFile(filepath.Join(cpuDir, e.Name(), "topology", "physical_package_id")) + id, err2 := os.ReadFile(filepath.Join(cpuDir, e.Name(), "topology", "core_id")) + if err1 != nil || err2 != nil { + return nil + } + p, _ := strconv.Atoi(strings.TrimSpace(string(pkg))) + c, _ := strconv.Atoi(strings.TrimSpace(string(id))) + var found *core + for _, existing := range cores[p] { + if existing.id == c { + found = existing + } + } + if found == nil { + found = &core{id: c} + cores[p] = append(cores[p], found) + } + found.lps = append(found.lps, lp) + } + if len(cores) == 0 { + return nil + } + out := map[int][][2]any{} + for p, list := range cores { + for _, c := range list { + sort.Ints(c.lps) + out[p] = append(out[p], [2]any{c.id, c.lps}) + } + } + return out +} + +func TestCoreTypeJSON(t *testing.T) { + data, err := json.Marshal(cpu.CoreTypeEfficiency) + if err != nil || string(data) != `"efficiency"` { + t.Fatalf("json.Marshal(CoreTypeEfficiency) = %s, %v", data, err) + } + var back cpu.CoreType + if err := json.Unmarshal(data, &back); err != nil || back != cpu.CoreTypeEfficiency { + t.Errorf("json.Unmarshal(%s) = %v, %v", data, back, err) + } +} diff --git a/pkg/memory/memory_cache.go b/pkg/memory/memory_cache.go index fcfeda5c..6805323d 100644 --- a/pkg/memory/memory_cache.go +++ b/pkg/memory/memory_cache.go @@ -122,6 +122,10 @@ type Cache struct { // Type indicates what type of memory is stored in the cache. Can be // instruction (executable bytecodes), data or both. Type CacheType `json:"type"` + // ID is the kernel's identifier of this cache instance, unique among the + // caches of the same level and type, or -1 where the kernel does not + // number it (older kernels did not number device-tree caches). + ID int `json:"id"` // SizeBytes indicates the size of the cache in bytes. SizeBytes uint64 `json:"size_bytes"` // The set of logical processors (hardware threads) that have access to diff --git a/pkg/memory/memory_cache_linux.go b/pkg/memory/memory_cache_linux.go index 8896fd5b..d829c76f 100644 --- a/pkg/memory/memory_cache_linux.go +++ b/pkg/memory/memory_cache_linux.go @@ -9,6 +9,7 @@ import ( "context" "errors" "fmt" + "math" "os" "path/filepath" "sort" @@ -18,6 +19,7 @@ import ( "github.com/zededa/ghw/internal/log" "github.com/zededa/ghw/pkg/linuxpath" "github.com/zededa/ghw/pkg/unitutil" + "github.com/zededa/ghw/pkg/util" ) func CachesForNode(ctx context.Context, nodeID int) ([]*Cache, error) { @@ -75,38 +77,23 @@ func CachesForNode(ctx context.Context, nodeID int) ([]*Cache, error) { if !strings.HasPrefix(cacheDirFileName, "index") { continue } - cacheIndex, _ := strconv.Atoi(cacheDirFileName[5:]) - - // The cache information is repeated for each node, so here, we - // just ensure that we only have a one Cache object for each - // unique combination of level, type and processor map - level := memoryCacheLevel(ctx, paths, nodeID, lpID, cacheIndex) - cacheType := memoryCacheType(ctx, paths, nodeID, lpID, cacheIndex) - cacheID := memoryCacheID(ctx, paths, nodeID, lpID, cacheIndex) - - // Generate cache key for deduplication. - // Use cache ID if available (modern kernels), otherwise fall back - // to shared_cpu_map for older kernels. This ensures that caches - // shared across NUMA nodes (e.g., L3 in Sub-NUMA Clustering) are - // correctly merged into a single cache object. - var cacheKey string - if cacheID != -1 { - cacheKey = fmt.Sprintf("%d-%d-%d", level, cacheType, cacheID) - } else { - sharedCpuMap := memoryCacheSharedCPUMap(ctx, paths, nodeID, lpID, cacheIndex) - cacheKey = fmt.Sprintf("%d-%d-%s", level, cacheType, sharedCpuMap) + dir := filepath.Join(cachePath, cacheDirFileName) + c, err := readCacheIndex(dir) + if err != nil { + log.Warn(ctx, "cache %s: %s", dir, err) + continue } - cache, exists := caches[cacheKey] + // The cache information is repeated for each logical processor + // sharing the cache, so keep one Cache object per instance. Keying + // by the kernel's cache ID also merges caches shared across NUMA + // nodes (e.g., L3 in Sub-NUMA Clustering) into one. + key := cacheKey(c, dir, lpID) + cache, exists := caches[key] if !exists { - size := memoryCacheSize(ctx, paths, nodeID, lpID, level) - cache = &Cache{ - Level: uint8(level), - Type: cacheType, - SizeBytes: uint64(size) * uint64(unitutil.KB), - LogicalProcessors: make([]uint32, 0), - } - caches[cacheKey] = cache + c.LogicalProcessors = make([]uint32, 0) + cache = c + caches[key] = cache } cache.LogicalProcessors = append( cache.LogicalProcessors, @@ -127,122 +114,134 @@ func CachesForNode(ctx context.Context, nodeID int) ([]*Cache, error) { return cacheVals, nil } -func memoryCacheLevel( - ctx context.Context, - paths *linuxpath.Paths, - nodeID int, - lpID int, - cacheIndex int, -) int { - levelPath := filepath.Join( - paths.NodeCPUCacheIndex(nodeID, lpID, cacheIndex), - "level", - ) - levelContents, err := os.ReadFile(levelPath) - if err != nil { - log.Warn(ctx, "%s", err) - return -1 +// cacheTypeFromSysfs maps the content of a cache's sysfs "type" file. +func cacheTypeFromSysfs(s string) CacheType { + switch strings.TrimSpace(s) { + case "Data": + return CacheTypeData + case "Instruction": + return CacheTypeInstruction + default: + return CacheTypeUnified } - // levelContents is now a []byte with the last byte being a newline - // character. Trim that off and convert the contents to an integer. - level, err := strconv.Atoi(string(levelContents[:len(levelContents)-1])) +} + +// Caches returns every cache instance of the host's online logical processors, +// read from /sys/devices/system/cpu/cpuN/cache: one entry per instance with +// all the logical processors sharing it, sorted by level, type and first +// logical processor. Instances are told apart by their kernel ID, or, where +// the kernel does not number them, by the set of logical processors sharing +// them. The error is only for a CPU directory that cannot be listed. +func Caches(ctx context.Context) ([]*Cache, error) { + paths := linuxpath.New(ctx) + cpuDirs, err := os.ReadDir(paths.SysDevicesSystemCPU) if err != nil { - log.Warn(ctx, "Unable to parse int from %s", levelContents) - return -1 + return nil, err + } + caches := map[string]*Cache{} + for _, cpuDir := range cpuDirs { + name := cpuDir.Name() + id, err := strconv.ParseUint(strings.TrimPrefix(name, "cpu"), 10, 31) + if err != nil || !strings.HasPrefix(name, "cpu") { + continue + } + lpID := int(id) + if online, err := util.ReadInt(filepath.Join(paths.SysDevicesSystemCPU, name, "online")); err == nil && online == 0 { + continue + } + cacheDir := filepath.Join(paths.SysDevicesSystemCPU, name, "cache") + indexes, err := os.ReadDir(cacheDir) + if err != nil { + continue + } + for _, index := range indexes { + if !strings.HasPrefix(index.Name(), "index") { + continue + } + dir := filepath.Join(cacheDir, index.Name()) + c, err := readCacheIndex(dir) + if err != nil { + log.Warn(ctx, "cache %s: %s", dir, err) + continue + } + key := cacheKey(c, dir, lpID) + cache, ok := caches[key] + if !ok { + cache = c + caches[key] = cache + } + cache.LogicalProcessors = append(cache.LogicalProcessors, uint32(id)) + } } - return level + + result := make([]*Cache, 0, len(caches)) + for _, c := range caches { + sort.Sort(SortByLogicalProcessorId(c.LogicalProcessors)) + result = append(result, c) + } + sort.Sort(SortByCacheLevelTypeFirstProcessor(result)) + return result, nil } -func memoryCacheID( - ctx context.Context, - paths *linuxpath.Paths, - nodeID int, - lpID int, - cacheIndex int, -) int { - idPath := filepath.Join( - paths.NodeCPUCacheIndex(nodeID, lpID, cacheIndex), - "id", - ) - idContents, err := os.ReadFile(idPath) - if err != nil { - // The id file may not exist in early kernel versions, so we don't warn - return -1 +// cacheKey tells cache instances apart: by the kernel's ID, or, where the kernel +// does not number them, by the logical processors sharing the cache, read from +// dir. A cache whose sharing list is blank is logical processor lpID's own. +func cacheKey(c *Cache, dir string, lpID int) string { + if c.ID >= 0 { + return fmt.Sprintf("%d-%d-id%d", c.Level, c.Type, c.ID) } - id, err := strconv.Atoi(string(idContents[:len(idContents)-1])) - if err != nil { - log.Warn(ctx, "Unable to parse int from %s", idContents) - return -1 + shared, _ := os.ReadFile(filepath.Join(dir, "shared_cpu_list")) + cpus := strings.TrimSpace(string(shared)) + if cpus == "" { + cpus = fmt.Sprintf("own%d", lpID) } - return id + return fmt.Sprintf("%d-%d-cpus%s", c.Level, c.Type, cpus) } -func memoryCacheSize( - ctx context.Context, - paths *linuxpath.Paths, - nodeID int, - lpID int, - cacheIndex int, -) int { - sizePath := filepath.Join( - paths.NodeCPUCacheIndex(nodeID, lpID, cacheIndex), - "size", - ) - sizeContents, err := os.ReadFile(sizePath) +// readCacheIndex reads the cache a cache/indexN directory describes: its level, +// type, kernel ID (-1 when the kernel does not number it) and size. The error +// is for a level that cannot be read or does not fit a Cache. +func readCacheIndex(dir string) (*Cache, error) { + level, err := util.ReadInt(filepath.Join(dir, "level")) if err != nil { - log.Warn(ctx, "%s", err) - return -1 + return nil, err } - // size comes as XK\n, so we trim off the K and the newline. - size, err := strconv.Atoi(string(sizeContents[:len(sizeContents)-2])) + if level < 0 || level > math.MaxUint8 { + return nil, fmt.Errorf("cache level %d is out of range", level) + } + typeContents, _ := os.ReadFile(filepath.Join(dir, "type")) + id, err := util.ReadInt(filepath.Join(dir, "id")) if err != nil { - log.Warn(ctx, "Unable to parse int from %s", sizeContents) - return -1 + id = -1 } - return size + return &Cache{ + Level: uint8(level), + Type: cacheTypeFromSysfs(string(typeContents)), + ID: id, + SizeBytes: readCacheSize(dir), + }, nil } -func memoryCacheType( - ctx context.Context, - paths *linuxpath.Paths, - nodeID int, - lpID int, - cacheIndex int, -) CacheType { - typePath := filepath.Join( - paths.NodeCPUCacheIndex(nodeID, lpID, cacheIndex), - "type", - ) - cacheTypeContents, err := os.ReadFile(typePath) +// readCacheSize reads a cache's size, such as "32K", in bytes; 0 when it is +// not reported. +func readCacheSize(dir string) uint64 { + data, err := os.ReadFile(filepath.Join(dir, "size")) if err != nil { - log.Warn(ctx, "%s", err) - return CacheTypeUnified + return 0 } - switch string(cacheTypeContents[:len(cacheTypeContents)-1]) { - case "Data": - return CacheTypeData - case "Instruction": - return CacheTypeInstruction - default: - return CacheTypeUnified + s := strings.TrimSpace(string(data)) + multiplier := uint64(1) + switch { + case strings.HasSuffix(s, "K"): + multiplier = uint64(unitutil.KB) + case strings.HasSuffix(s, "M"): + multiplier = uint64(unitutil.MB) + case strings.HasSuffix(s, "G"): + multiplier = uint64(unitutil.GB) } -} - -func memoryCacheSharedCPUMap( - ctx context.Context, - paths *linuxpath.Paths, - nodeID int, - lpID int, - cacheIndex int, -) string { - scpuPath := filepath.Join( - paths.NodeCPUCacheIndex(nodeID, lpID, cacheIndex), - "shared_cpu_map", - ) - sharedCpuMap, err := os.ReadFile(scpuPath) + n, err := strconv.ParseUint(strings.TrimRight(s, "KMG"), 10, 64) if err != nil { - log.Warn(ctx, "%s", err) - return "" + return 0 } - return string(sharedCpuMap[:len(sharedCpuMap)-1]) + return n * multiplier } diff --git a/pkg/memory/memory_cache_linux_test.go b/pkg/memory/memory_cache_linux_test.go new file mode 100644 index 00000000..f46e8848 --- /dev/null +++ b/pkg/memory/memory_cache_linux_test.go @@ -0,0 +1,243 @@ +// +// Use and distribution licensed under the Apache license version 2. +// +// See the COPYING file in the root project directory for full text. +// + +package memory_test + +import ( + "os" + "path/filepath" + "reflect" + "strconv" + "testing" + + "github.com/zededa/ghw/internal/config" + "github.com/zededa/ghw/internal/sysfsfixture" + "github.com/zededa/ghw/pkg/memory" + "github.com/zededa/ghw/pkg/snapshot" + "github.com/zededa/ghw/testdata" +) + +// cachesFixture builds four CPUs with private L1 caches, an L2 per pair of +// CPUs and one L3. withID decides, per cache level, whether the kernel numbers +// that level's instances. +func cachesFixture(t *testing.T, withID func(level, cpu int) bool) string { + t.Helper() + id := func(level, cpu int, value int) string { + if withID(level, cpu) { + return strconv.Itoa(value) + } + return "" + } + var cpus []sysfsfixture.CPU + for c := 0; c < 4; c++ { + pair := "0-1" + if c >= 2 { + pair = "2-3" + } + cpus = append(cpus, sysfsfixture.CPU{ID: c, Caches: []sysfsfixture.Cache{ + {Index: 0, Level: "1", Type: "Data", ID: id(1, c, c), Size: "48K", Shared: strconv.Itoa(c)}, + {Index: 1, Level: "1", Type: "Instruction", ID: id(1, c, c), Size: "32K", Shared: strconv.Itoa(c)}, + {Index: 2, Level: "2", Type: "Unified", ID: id(2, c, c/2), Size: "2048K", Shared: pair}, + {Index: 3, Level: "3", Type: "Unified", ID: id(3, c, 0), Size: "30720K", Shared: "0-3"}, + }}) + } + return sysfsfixture.Write(t, sysfsfixture.Tree{CPUs: cpus}) +} + +type cacheSummary struct { + Level uint8 + Type memory.CacheType + ID int + Size uint64 + CPUs []uint32 +} + +func summarize(caches []*memory.Cache) []cacheSummary { + out := make([]cacheSummary, len(caches)) + for i, c := range caches { + out[i] = cacheSummary{c.Level, c.Type, c.ID, c.SizeBytes, c.LogicalProcessors} + } + return out +} + +func wantCaches(id func(level, value int) int) []cacheSummary { + var want []cacheSummary + for c := 0; c < 4; c++ { + want = append(want, cacheSummary{1, memory.CacheTypeInstruction, id(1, c), 32 * 1024, []uint32{uint32(c)}}) + } + for c := 0; c < 4; c++ { + want = append(want, cacheSummary{1, memory.CacheTypeData, id(1, c), 48 * 1024, []uint32{uint32(c)}}) + } + want = append(want, + cacheSummary{2, memory.CacheTypeUnified, id(2, 0), 2048 * 1024, []uint32{0, 1}}, + cacheSummary{2, memory.CacheTypeUnified, id(2, 1), 2048 * 1024, []uint32{2, 3}}, + cacheSummary{3, memory.CacheTypeUnified, id(3, 0), 30720 * 1024, []uint32{0, 1, 2, 3}}, + ) + return want +} + +func caches(t *testing.T, root string) []cacheSummary { + t.Helper() + got, err := memory.Caches(config.ContextFromArgs(config.WithChroot(root))) + if err != nil { + t.Fatal(err) + } + return summarize(got) +} + +func TestCaches(t *testing.T) { + root := cachesFixture(t, func(int, int) bool { return true }) + want := wantCaches(func(_, value int) int { return value }) + if got := caches(t, root); !reflect.DeepEqual(got, want) { + t.Errorf("Caches() =\n%v\nwant\n%v", got, want) + } +} + +// ARM device trees give caches no id: instances are told apart by the CPUs +// sharing them, and their ID is -1. +func TestCachesWithoutIDs(t *testing.T) { + root := cachesFixture(t, func(int, int) bool { return false }) + want := wantCaches(func(int, int) int { return -1 }) + if got := caches(t, root); !reflect.DeepEqual(got, want) { + t.Errorf("Caches() =\n%v\nwant\n%v", got, want) + } +} + +// One L2 numbered by the kernel and one not must stay two instances. +func TestCachesMixedIDs(t *testing.T) { + root := cachesFixture(t, func(level, cpu int) bool { return level != 2 || cpu < 2 }) + var l2 []cacheSummary + for _, c := range caches(t, root) { + if c.Level == 2 { + l2 = append(l2, c) + } + } + want := []cacheSummary{ + {2, memory.CacheTypeUnified, 0, 2048 * 1024, []uint32{0, 1}}, + {2, memory.CacheTypeUnified, -1, 2048 * 1024, []uint32{2, 3}}, + } + if !reflect.DeepEqual(l2, want) { + t.Errorf("L2 caches = %v, want %v", l2, want) + } +} + +// A cache index whose level does not fit a Cache is skipped, not wrapped around. +func TestCachesLevelOutOfRange(t *testing.T) { + root := sysfsfixture.Write(t, sysfsfixture.Tree{CPUs: []sysfsfixture.CPU{{ID: 0, Caches: []sysfsfixture.Cache{ + {Index: 0, Level: "1", Type: "Data", ID: "0", Size: "48K", Shared: "0"}, + {Index: 2, Level: "258", Type: "Unified", ID: "0", Size: "2048K", Shared: "0"}, + }}}}) + got := caches(t, root) + if len(got) != 1 || got[0].Level != 1 { + t.Errorf("caches = %+v, want only the L1", got) + } +} + +// An offline CPU is not counted even if its cache directory is still there. +func TestCachesSkipOfflineCPU(t *testing.T) { + root := cachesFixture(t, func(int, int) bool { return true }) + if err := os.WriteFile(filepath.Join(root, "sys/devices/system/cpu/cpu3/online"), []byte("0\n"), 0o644); err != nil { + t.Fatal(err) + } + for _, c := range caches(t, root) { + for _, lp := range c.CPUs { + if lp == 3 { + t.Errorf("cache %+v lists offline CPU 3", c) + } + } + } +} + +// A blank shared_cpu_list must not merge private caches without ids. +func TestCachesBlankSharedList(t *testing.T) { + var cpus []sysfsfixture.CPU + for c := 0; c < 2; c++ { + cpus = append(cpus, sysfsfixture.CPU{ID: c, Caches: []sysfsfixture.Cache{ + {Index: 2, Level: "2", Type: "Unified", Size: "1024K", Shared: " "}, + }}) + } + got := caches(t, sysfsfixture.Write(t, sysfsfixture.Tree{CPUs: cpus})) + if len(got) != 2 { + t.Errorf("Caches() = %v, want two private L2 caches", got) + } +} + +// On the i7-1270P each 4-core E-module shares one L2. +func TestHybridSnapshotCaches(t *testing.T) { + dir, err := testdata.SnapshotsDirectory() + if err != nil { + t.Fatal(err) + } + root := t.TempDir() + if err := snapshot.UnpackInto(filepath.Join(dir, "linux-amd64-intel-i7-1270P.tar.gz"), root); err != nil { + t.Fatal(err) + } + found := false + for _, c := range caches(t, root) { + if c.Level == 2 && reflect.DeepEqual(c.CPUs, []uint32{8, 9, 10, 11}) { + found = true + if c.ID != 4 { + t.Errorf("E-module L2 ID = %d, want 4", c.ID) + } + } + } + if !found { + t.Error("no L2 shared by CPUs 8-11") + } +} + +func cachesForNode(t *testing.T, root string, node int) []cacheSummary { + t.Helper() + got, err := memory.CachesForNode(config.ContextFromArgs(config.WithChroot(root)), node) + if err != nil { + t.Fatal(err) + } + return summarize(got) +} + +// Each cache reports its own size: on the i7-1270P a P-core has a 48K L1d and +// a 32K L1i, an E-core a 32K L1d and a 64K L1i. +func TestCachesForNodeSizes(t *testing.T) { + dir, err := testdata.SnapshotsDirectory() + if err != nil { + t.Fatal(err) + } + root := t.TempDir() + if err := snapshot.UnpackInto(filepath.Join(dir, "linux-amd64-intel-i7-1270P.tar.gz"), root); err != nil { + t.Fatal(err) + } + want := map[[2]uint32]uint64{ // {CPU, type} -> size + {0, uint32(memory.CacheTypeData)}: 48 * 1024, + {0, uint32(memory.CacheTypeInstruction)}: 32 * 1024, + {8, uint32(memory.CacheTypeData)}: 32 * 1024, + {8, uint32(memory.CacheTypeInstruction)}: 64 * 1024, + } + for _, c := range cachesForNode(t, root, 0) { + if c.Level != 1 { + continue + } + for _, cpu := range c.CPUs { + if size, ok := want[[2]uint32{cpu, uint32(c.Type)}]; ok && c.Size != size { + t.Errorf("CPU %d L1 %v size = %d, want %d", cpu, c.Type, c.Size, size) + } + } + } +} + +// A cache with kernel id 3 and one without an id whose CPU list reads "3" are +// two caches. +func TestCachesForNodeIDAndCPUListApart(t *testing.T) { + root := sysfsfixture.Write(t, sysfsfixture.Tree{ + CPUs: []sysfsfixture.CPU{ + {ID: 0, Caches: []sysfsfixture.Cache{{Index: 2, Level: "2", Type: "Unified", ID: "3", Size: "1024K", Shared: "0"}}}, + {ID: 1, Caches: []sysfsfixture.Cache{{Index: 2, Level: "2", Type: "Unified", Size: "1024K", Shared: "3"}}}, + }, + Nodes: map[int]string{0: "0-1"}, + }) + if got := cachesForNode(t, root, 0); len(got) != 2 { + t.Errorf("CachesForNode = %v, want two L2 caches", got) + } +} diff --git a/pkg/pci/local_cpus_linux_test.go b/pkg/pci/local_cpus_linux_test.go new file mode 100644 index 00000000..3a15bb87 --- /dev/null +++ b/pkg/pci/local_cpus_linux_test.go @@ -0,0 +1,72 @@ +// +// Use and distribution licensed under the Apache license version 2. +// +// See the COPYING file in the root project directory for full text. +// + +package pci + +import ( + "encoding/json" + "os" + "path/filepath" + "reflect" + "strings" + "testing" + + "github.com/jaypipes/pcidb" + + "github.com/zededa/ghw/internal/config" + pciaddr "github.com/zededa/ghw/pkg/pci/address" +) + +func TestDeviceLocalCPUs(t *testing.T) { + root := t.TempDir() + devices := filepath.Join(root, "sys", "bus", "pci", "devices") + if err := os.MkdirAll(filepath.Join(devices, "0000:00:1f.0"), 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(devices, "0000:00:1f.0", "local_cpulist"), []byte("0-3\n"), 0o644); err != nil { + t.Fatal(err) + } + if err := os.MkdirAll(filepath.Join(devices, "0000:00:1f.1"), 0o755); err != nil { + t.Fatal(err) + } + if err := os.MkdirAll(filepath.Join(devices, "0000:00:1f.2"), 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(devices, "0000:00:1f.2", "local_cpulist"), []byte("0-x\n"), 0o644); err != nil { + t.Fatal(err) + } + ctx := config.ContextFromArgs(config.WithChroot(root)) + + if got, want := getDeviceLocalCPUs(ctx, pciaddr.FromString("0000:00:1f.0")), []int{0, 1, 2, 3}; !reflect.DeepEqual(got, want) { + t.Errorf("local CPUs = %v, want %v", got, want) + } + if got := getDeviceLocalCPUs(ctx, pciaddr.FromString("0000:00:1f.1")); got != nil { + t.Errorf("local CPUs without local_cpulist = %v, want nil", got) + } + if got := getDeviceLocalCPUs(ctx, pciaddr.FromString("0000:00:1f.2")); got != nil { + t.Errorf("local CPUs from a malformed local_cpulist = %v, want nil", got) + } +} + +func TestDeviceLocalCPUsJSON(t *testing.T) { + dev := &Device{ + Address: "0000:00:1f.0", + Vendor: &pcidb.Vendor{}, + Product: &pcidb.Product{}, + Subsystem: &pcidb.Product{}, + Class: &pcidb.Class{}, + Subclass: &pcidb.Subclass{}, + ProgrammingInterface: &pcidb.ProgrammingInterface{}, + LocalCPUs: []int{0, 1}, + } + data, err := json.Marshal(dev) + if err != nil { + t.Fatal(err) + } + if !strings.Contains(string(data), `"local_cpus":[0,1]`) { + t.Errorf("JSON %s has no local_cpus", data) + } +} diff --git a/pkg/pci/pci.go b/pkg/pci/pci.go index 28bdd682..a863e1a9 100644 --- a/pkg/pci/pci.go +++ b/pkg/pci/pci.go @@ -45,6 +45,11 @@ type Device struct { // Useful for callers that want to do custom vendor/device/class matching // beyond what the pcidb lookup provides. Modalias string `json:"modalias,omitempty"` + // LocalCPUs are the logical processors local to the device: those of the + // NUMA node its DMA and interrupts are local to, or every CPU when the + // platform gives it no NUMA affinity. Nil when the kernel does not report + // them. + LocalCPUs []int `json:"local_cpus,omitempty"` // Parent is the resolved parent Device pointer for this device (the // PCIe upstream port or root complex device). It is nil for root @@ -75,6 +80,7 @@ type devMarshallable struct { Interface devIdent `json:"programming_interface"` IOMMUGroup string `json:"iommu_group"` Modalias string `json:"modalias,omitempty"` + LocalCPUs []int `json:"local_cpus,omitempty"` } // NOTE(jaypipes) Device has a custom JSON marshaller because we don't want @@ -113,6 +119,7 @@ func (d *Device) MarshalJSON() ([]byte, error) { }, IOMMUGroup: d.IOMMUGroup, Modalias: d.Modalias, + LocalCPUs: d.LocalCPUs, } return json.Marshal(dm) } diff --git a/pkg/pci/pci_linux.go b/pkg/pci/pci_linux.go index 52052b06..e9487040 100644 --- a/pkg/pci/pci_linux.go +++ b/pkg/pci/pci_linux.go @@ -7,6 +7,7 @@ package pci import ( "context" + "errors" "os" "path/filepath" "regexp" @@ -95,6 +96,20 @@ func getDeviceIommuGroup(paths *linuxpath.Paths, pciAddr *pciaddr.Address) strin return filepath.Base(dest) } +// getDeviceLocalCPUs reads the logical processors local to the device; nil +// when the kernel does not report them. +func getDeviceLocalCPUs(ctx context.Context, pciAddr *pciaddr.Address) []int { + paths := linuxpath.New(ctx) + cpus, err := util.ReadCPUList(filepath.Join(paths.SysBusPciDevices, pciAddr.String(), "local_cpulist")) + if err != nil { + if !errors.Is(err, os.ErrNotExist) { + log.Warn(ctx, "failed to read the local CPUs of device %q: %v", pciAddr, err) + } + return nil + } + return cpus +} + func getDeviceModalias(paths *linuxpath.Paths, pciAddr *pciaddr.Address) string { fp := getDeviceModaliasPath(paths, pciAddr) data, err := os.ReadFile(fp) @@ -412,6 +427,7 @@ func (info *Info) getDevices(ctx context.Context) []*Device { device.ParentAddress = getDeviceParentAddress(paths, pciAddr) device.IOMMUGroup = getDeviceIommuGroup(paths, pciAddr) device.Modalias = modalias + device.LocalCPUs = getDeviceLocalCPUs(ctx, pciAddr) devs = append(devs, device) } linkDeviceTree(devs) diff --git a/pkg/pci/pci_linux_test.go b/pkg/pci/pci_linux_test.go index 0327a8fa..ed12c156 100644 --- a/pkg/pci/pci_linux_test.go +++ b/pkg/pci/pci_linux_test.go @@ -12,6 +12,7 @@ import ( "fmt" "os" "path/filepath" + "reflect" "testing" "github.com/jaypipes/pcidb" @@ -298,6 +299,33 @@ func TestPCIMalformedModalias(t *testing.T) { } } +// The dual-socket Xeon's devices behind 0000:05:00.0 and .1 are local to one +// socket each; the others list every CPU. +func TestPCIDeviceLocalCPUsXeon(t *testing.T) { + info := pciTestSetupXeon(t) + var socket0, socket1 []int + for cpu := 0; cpu < 24; cpu += 2 { + socket0, socket1 = append(socket0, cpu), append(socket1, cpu+1) + } + all := make([]int, 24) + for cpu := range all { + all[cpu] = cpu + } + for address, want := range map[string][]int{ + "0000:05:00.0": socket0, + "0000:05:00.1": socket1, + "0000:00:1a.1": all, + } { + dev := info.GetDevice(address) + if dev == nil { + t.Fatalf("no device %s", address) + } + if !reflect.DeepEqual(dev.LocalCPUs, want) { + t.Errorf("%s LocalCPUs = %v, want %v", address, dev.LocalCPUs, want) + } + } +} + func pciTestSetupXeon(t *testing.T) *pci.Info { const snapshotFilename = "linux-amd64-intel-xeon-L5640.tar.gz" return pciTestSetup(t, snapshotFilename) diff --git a/pkg/snapshot/clonetree_linux.go b/pkg/snapshot/clonetree_linux.go index 88bff08f..59c8b35e 100644 --- a/pkg/snapshot/clonetree_linux.go +++ b/pkg/snapshot/clonetree_linux.go @@ -37,8 +37,16 @@ func ExpectedCloneStaticContent() []string { "/proc/cpuinfo", "/proc/meminfo", "/proc/self/mounts", + "/sys/devices/system/cpu/cpu*/online", "/sys/devices/system/cpu/cpu*/cache/index*/*", "/sys/devices/system/cpu/cpu*/topology/*", + "/sys/devices/system/cpu/cpu*/cpufreq/base_frequency", + "/sys/devices/system/cpu/cpu*/cpufreq/cpuinfo_max_freq", + "/sys/devices/system/cpu/cpu*/cpu_capacity", + "/sys/devices/cpu_core/cpus", + "/sys/devices/cpu_atom/cpus", + "/sys/devices/cpu_lowpower/cpus", + "/sys/devices/cpu/type", "/sys/devices/system/memory/block_size_bytes", "/sys/devices/system/memory/memory*/online", "/sys/devices/system/memory/memory*/state", diff --git a/pkg/snapshot/clonetree_linux_test.go b/pkg/snapshot/clonetree_linux_test.go index 3cc6e38d..72ed2b70 100644 --- a/pkg/snapshot/clonetree_linux_test.go +++ b/pkg/snapshot/clonetree_linux_test.go @@ -12,10 +12,13 @@ import ( "path/filepath" "reflect" "sort" + "strconv" "strings" "testing" "github.com/zededa/ghw/internal/config" + "github.com/zededa/ghw/internal/sysfsfixture" + ghwcpu "github.com/zededa/ghw/pkg/cpu" "github.com/zededa/ghw/pkg/linuxdt" "github.com/zededa/ghw/pkg/snapshot" "github.com/zededa/ghw/pkg/util" @@ -207,3 +210,64 @@ func TestCloneDeviceTree(t *testing.T) { t.Errorf("snapshot carries the serial number %q", serial) } } + +// A snapshot reproduces the CPU topology ghw reads from the host: online and +// offline CPUs, packages, cores, NUMA nodes, frequencies, caches and core types. +func TestCloneStaticContentCPUTopology(t *testing.T) { + cpu := func(id int, core string, siblings string) sysfsfixture.CPU { + return sysfsfixture.CPU{ + ID: id, Package: "0", Core: core, Siblings: siblings, + BaseKHz: "2100000", MaxKHz: "4800000", + Caches: []sysfsfixture.Cache{ + {Index: 0, Level: "1", Type: "Data", ID: core, Size: "48K", Shared: siblings}, + {Index: 2, Level: "2", Type: "Unified", ID: "0", Size: "2048K", Shared: "0-3"}, + }, + } + } + hybrid := sysfsfixture.Tree{ + CPUs: []sysfsfixture.CPU{ + cpu(0, "0", "0-1"), cpu(1, "0", "0-1"), cpu(2, "8", "2"), cpu(3, "9", "3"), + {ID: 4, Offline: true}, + }, + Nodes: map[int]string{0: "0-3"}, + PMUCore: "0-1", PMUAtom: "2", PMULowPower: "3", + } + bigLittle := sysfsfixture.Tree{Nodes: map[int]string{0: "0-3"}} + for id, capacity := range []string{"1024", "1024", "446", "446"} { + c := cpu(id, strconv.Itoa(id), strconv.Itoa(id)) + c.Capacity = capacity + bigLittle.CPUs = append(bigLittle.CPUs, c) + } + homogeneous := sysfsfixture.Tree{ + CPUs: []sysfsfixture.CPU{cpu(0, "0", "0-1"), cpu(1, "0", "0-1")}, + CorePMU: true, + } + for name, tree := range map[string]sysfsfixture.Tree{ + "Intel hybrid": hybrid, + "big.LITTLE": bigLittle, + "homogeneous x86": homogeneous, + } { + t.Run(name, func(t *testing.T) { + host := sysfsfixture.Write(t, tree) + var specs []string + for _, spec := range snapshot.ExpectedCloneStaticContent() { + specs = append(specs, filepath.Join(host, spec)) + } + clone := t.TempDir() + if err := snapshot.CopyFilesInto(context.Background(), specs, clone, nil); err != nil { + t.Fatal(err) + } + want, err := ghwcpu.New(config.WithChroot(host)) + if err != nil { + t.Fatal(err) + } + got, err := ghwcpu.New(config.WithChroot(filepath.Join(clone, host))) + if err != nil { + t.Fatal(err) + } + if got.JSONString(true) != want.JSONString(true) { + t.Errorf("from the snapshot:\n%s\nfrom the host:\n%s", got.JSONString(true), want.JSONString(true)) + } + }) + } +} diff --git a/pkg/util/util.go b/pkg/util/util.go index a9955b32..262b24b4 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -83,3 +83,65 @@ func ParseBool(str string) (bool, error) { } } } + +// maxCPUID bounds the logical processor IDs ParseCPUList accepts, far above any +// kernel's CPU limit, so that a corrupt list, from a snapshot say, cannot make it +// allocate gigabytes. +const maxCPUID = 1 << 16 + +// ParseCPUList parses a list of logical processor IDs in the kernel's CPU-list +// format, such as "0-3,7", the format of sysfs files like +// /sys/devices/system/cpu/online. Surrounding whitespace is ignored, and an +// empty list or "(null)", which the kernel prints for a mask it never set, is +// nil. An ID of maxCPUID or more is an error. +func ParseCPUList(s string) ([]int, error) { + s = strings.TrimSpace(s) + if s == "" || s == "(null)" { + return nil, nil + } + var ids []int + for _, part := range strings.Split(s, ",") { + lo, hi, isRange := strings.Cut(part, "-") + first, err := strconv.ParseUint(lo, 10, 31) + if err != nil { + return nil, fmt.Errorf("invalid CPU list %q: %w", s, err) + } + last := first + if isRange { + if last, err = strconv.ParseUint(hi, 10, 31); err != nil { + return nil, fmt.Errorf("invalid CPU list %q: %w", s, err) + } + if last < first { + return nil, fmt.Errorf("invalid CPU list %q: range %s is reversed", s, part) + } + } + if last >= maxCPUID { + return nil, fmt.Errorf("invalid CPU list %q: CPU %d is out of range", s, last) + } + for id := first; id <= last; id++ { + ids = append(ids, int(id)) + } + } + return ids, nil +} + +// ReadCPUList reads a sysfs file holding a CPU list (see ParseCPUList). The +// error wraps os.ErrNotExist when the file does not exist. +func ReadCPUList(path string) ([]int, error) { + data, err := os.ReadFile(path) + if err != nil { + return nil, err + } + return ParseCPUList(string(data)) +} + +// ReadInt reads a sysfs file holding one integer. Unlike SafeIntFromFile it +// tells a missing or malformed file (an error, wrapping os.ErrNotExist when the +// file does not exist) from a value of -1, and it does not warn. +func ReadInt(path string) (int, error) { + data, err := os.ReadFile(path) + if err != nil { + return 0, err + } + return strconv.Atoi(strings.TrimSpace(string(data))) +} diff --git a/pkg/util/util_test.go b/pkg/util/util_test.go index 20258e58..34bbceba 100644 --- a/pkg/util/util_test.go +++ b/pkg/util/util_test.go @@ -7,6 +7,10 @@ package util_test import ( + "errors" + "os" + "path/filepath" + "reflect" "testing" "github.com/zededa/ghw/pkg/util" @@ -109,3 +113,66 @@ func TestParseBool(t *testing.T) { }) } } + +func TestParseCPUList(t *testing.T) { + testCases := []struct { + in string + want []int + wantErr bool + }{ + {in: "0-3,7\n", want: []int{0, 1, 2, 3, 7}}, + {in: "5", want: []int{5}}, + {in: "0,2-3", want: []int{0, 2, 3}}, + {in: "", want: nil}, + {in: " \n", want: nil}, + {in: "(null)\n", want: nil}, + {in: "a", wantErr: true}, + {in: "3-1", wantErr: true}, + {in: "1-", wantErr: true}, + {in: "-1", wantErr: true}, + {in: "0,,1", wantErr: true}, + {in: "65535", want: []int{65535}}, + {in: "65536", wantErr: true}, + {in: "0-2147483647", wantErr: true}, + } + for _, tc := range testCases { + got, err := util.ParseCPUList(tc.in) + if (err != nil) != tc.wantErr { + t.Errorf("ParseCPUList(%q) error = %v, want error %v", tc.in, err, tc.wantErr) + continue + } + if !reflect.DeepEqual(got, tc.want) { + t.Errorf("ParseCPUList(%q) = %v, want %v", tc.in, got, tc.want) + } + } +} + +func TestReadCPUListAndInt(t *testing.T) { + dir := t.TempDir() + write := func(name, content string) string { + t.Helper() + path := filepath.Join(dir, name) + if err := os.WriteFile(path, []byte(content), 0o644); err != nil { + t.Fatal(err) + } + return path + } + if got, err := util.ReadCPUList(write("list", "0-2,8\n")); err != nil || !reflect.DeepEqual(got, []int{0, 1, 2, 8}) { + t.Errorf("ReadCPUList = %v, %v; want [0 1 2 8]", got, err) + } + if _, err := util.ReadCPUList(filepath.Join(dir, "missing")); !errors.Is(err, os.ErrNotExist) { + t.Errorf("ReadCPUList(missing) error = %v, want os.ErrNotExist", err) + } + if _, err := util.ReadCPUList(write("bad", "x\n")); err == nil { + t.Error("ReadCPUList(malformed) = no error") + } + if got, err := util.ReadInt(write("int", " -1\n")); err != nil || got != -1 { + t.Errorf("ReadInt = %d, %v; want -1", got, err) + } + if _, err := util.ReadInt(filepath.Join(dir, "missing")); !errors.Is(err, os.ErrNotExist) { + t.Errorf("ReadInt(missing) error = %v, want os.ErrNotExist", err) + } + if _, err := util.ReadInt(write("nan", "abc")); err == nil { + t.Error("ReadInt(abc) = no error") + } +} diff --git a/testdata/snapshots/linux-amd64-intel-i5-1335UE.tar.gz b/testdata/snapshots/linux-amd64-intel-i5-1335UE.tar.gz new file mode 100644 index 00000000..f700ad85 Binary files /dev/null and b/testdata/snapshots/linux-amd64-intel-i5-1335UE.tar.gz differ diff --git a/testdata/snapshots/linux-arm64-nvidia-jetson-agx-orin.tar.gz b/testdata/snapshots/linux-arm64-nvidia-jetson-agx-orin.tar.gz new file mode 100644 index 00000000..a19ee7f1 Binary files /dev/null and b/testdata/snapshots/linux-arm64-nvidia-jetson-agx-orin.tar.gz differ