Repository navigation
cpu: per-CPU topology facts for workload placement #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
34b0b85
util: read the kernel's CPU-list format
rucoder 94b72cd
cpu: tell apart cores that share a core_id across clusters
rucoder f7646e3
memory: report every cache instance with its id
rucoder 28eab44
memory: read CachesForNode's caches the way Caches does
rucoder 6297a1e
cpu: report each logical processor's package, core, NUMA node and fre…
rucoder fcbd6e2
cpu: report each logical processor's core type
rucoder 2c79e09
pci: report the CPUs local to each device
rucoder d446797
snapshot: capture the files CPU topology reads
rucoder 6856e44
testdata: add a snapshot of an Intel hybrid with its PMU files
rucoder 58599f7
testdata: add a snapshot of an NVIDIA Jetson AGX Orin
rucoder File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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, ",") | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.