diff --git a/pkg/sysfs/system.go b/pkg/sysfs/system.go index 7dfe23181..7381a6460 100644 --- a/pkg/sysfs/system.go +++ b/pkg/sysfs/system.go @@ -123,6 +123,8 @@ type System interface { Offlined() cpuset.CPUSet Isolated() cpuset.CPUSet + + NodeHintToCPUs(string) string } // System devices @@ -742,6 +744,24 @@ func (sys *system) Isolated() cpuset.CPUSet { return sys.IsolatedCPUs() } +// Resolve given node topology hints to CPUs. +func (sys *system) NodeHintToCPUs(nodes string) string { + mset, err := cpuset.Parse(nodes) + if err != nil { + log.Error("failed to resolve nodes %q to CPUs: %v", nodes, err) + return "" + } + + cset := cpuset.New() + for _, id := range mset.List() { + if n, ok := sys.nodes[id]; ok { + cset = cset.Union(n.CPUSet()) + } + } + + return cset.Intersection(sys.OnlineCPUs()).String() +} + // Discover Cpus present in the system. func (sys *system) discoverCPUs() error { if sys.cpus != nil { diff --git a/pkg/topology/topology.go b/pkg/topology/topology.go index 6eeb116f4..d3957a1c8 100644 --- a/pkg/topology/topology.go +++ b/pkg/topology/topology.go @@ -227,6 +227,17 @@ func MergeTopologyHints(org, hints Hints) (res Hints) { return } +// ResolvePartialHints resolves NUMA-only hints to CPU hints using the given function. +func (hints Hints) ResolvePartialHints(resolve func(NUMAs string) string) { + for k, h := range hints { + if h.CPUs == "" && h.NUMAs != "" { + h.CPUs = resolve(h.NUMAs) + log.Debugf("partial NUMA hint %q resolved to CPUs %q", h.NUMAs, h.CPUs) + hints[k] = h + } + } +} + // String returns the hints as a string. func (h *Hint) String() string { cpus, nodes, sockets, sep := "", "", "", ""