Skip to content

Instantly share code, notes, and snippets.

@rometsch
Created December 22, 2022 14:38
Show Gist options
  • Select an option

  • Save rometsch/2f86de38ba2a49f2692f2c1aa79375b0 to your computer and use it in GitHub Desktop.

Select an option

Save rometsch/2f86de38ba2a49f2692f2c1aa79375b0 to your computer and use it in GitHub Desktop.

Revisions

  1. rometsch created this gist Dec 22, 2022.
    41 changes: 41 additions & 0 deletions numa.py
    Original 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())