Skip to content

Instantly share code, notes, and snippets.

@rometsch
Created December 22, 2022 14:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rometsch/2f86de38ba2a49f2692f2c1aa79375b0 to your computer and use it in GitHub Desktop.
Save rometsch/2f86de38ba2a49f2692f2c1aa79375b0 to your computer and use it in GitHub Desktop.
Obtain information about the numa topology of the system.
import os
import re
def get_numa_nodes():
"""Return the numa topology of the system.
Data is extracted from /sys/devices/system/cpu.
The numa nodes on the system are the keys of the dictionary.
Each value is a list of tuples which themselves represent physical cores.
Each tuples contains the id of the threads belonging to the physical core.
Without hyperthreading, each tuple contains one id, with hyperthreading each tuple contains two keys.
Returns:
dict: Topology of the numa nodes.
"""
path = "/sys/devices/system/cpu"
cpus = [d for d in os.listdir(path) if re.match("cpu\d+", d) is not None]
nodes = {}
for cpu in cpus:
cpu_path = os.path.join(path, cpu)
cpu_node = [s for s in os.listdir(cpu_path) if re.match("node\d+",s)][0]
with open(os.path.join(cpu_path, "topology", "thread_siblings_list"), "r") as infile:
threads = sorted(infile.read().strip().split(","))
threads = tuple(threads)
try:
nodes[cpu_node].add(threads)
except KeyError:
nodes[cpu_node] = set()
nodes[cpu_node].add(threads)
for key in nodes:
nodes[key] = sorted(nodes[key])
return nodes
if __name__=="__main__":
print(get_numa_nodes())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment