Created
December 22, 2022 14:38
-
-
Save rometsch/2f86de38ba2a49f2692f2c1aa79375b0 to your computer and use it in GitHub Desktop.
Revisions
-
rometsch created this gist
Dec 22, 2022 .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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,41 @@ 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())