def bind(*nodes): nodes = list(set(nodes)) res = ",".join(list(map(str, nodes))) c_string = bytes(res, "ascii") bitmask = LIBNUMA.numa_parse_nodestring(c_string) LIBNUMA.numa_run_on_node_mask(bitmask) LIBNUMA.numa_set_membind(bitmask)
def set_membind_nodes(*nodes) -> None: nodes = list(set(nodes)) res = ",".join(list(map(str, nodes))) c_string = bytes(res, "ascii") bitmask = LIBNUMA.numa_parse_nodestring(c_string) op_res = LIBNUMA.numa_set_membind(bitmask) if op_res == -1: raise Exception(f"set membind nodes {res} failed")
def node_to_cpus(node: int) -> List[int]: cpu_mask = LIBNUMA.numa_allocate_cpumask() LIBNUMA.numa_bitmask_clearall(cpu_mask) res = LIBNUMA.numa_node_to_cpus(node, cpu_mask) if res == 0: return numa_utils.get_bitset_list(cpu_mask) else: return []
def get_allocation_allowed_nodes() -> List[int]: result_nodes_pointer = LIBNUMA.numa_get_mems_allowed() try: result_nodes_pointer.contents except ValueError: raise Exception(f"get allocation allowed nodes info failed") return numa_utils.get_bitset_list(result_nodes_pointer)
def get_interleave_nodes() -> List[int]: result_nodes_pointer = LIBNUMA.numa_get_interleave_mask() try: result_nodes_pointer.contents except ValueError: raise Exception(f"get interleave nodes info failed") return numa_utils.get_bitset_list(result_nodes_pointer)
def get_affinitive_nodes() -> List[int]: result_mask_pointer = LIBNUMA.numa_get_run_node_mask() try: result_mask_pointer.contents except ValueError: raise Exception(f"get run nodes info failed") return numa_utils.get_bitset_list(result_mask_pointer)
def run_on_nodes(*nodes): """ :param nodes: numa node to run :return: """ nodes = list(set(nodes)) if len(nodes) == 0: bitmask = LIBNUMA.numa_parse_nodestring(b"all") op_res = LIBNUMA.numa_run_on_node_mask(bitmask) if op_res == -1: raise Exception("attempt to run on all nodes failed") else: res = ",".join(list(map(str, nodes))) c_string = bytes(res, "ascii") bitmask = LIBNUMA.numa_parse_nodestring(c_string) op_res = LIBNUMA.numa_run_on_node_mask(bitmask) if op_res == -1: raise Exception(f"attempt to run on {res} failed")
def run_on_cpus(pid: int, *cpus): """ :param cpus: cpu list :param pid: process id :return: """ cpus = list(set(cpus)) if len(cpus) == 0: bitmask = LIBNUMA.numa_parse_cpustring(b"all") op_res = LIBNUMA.numa_sched_setaffinity(pid, bitmask) if op_res == -1: raise Exception("attempt to run on all nodes failed") else: res = ",".join(list(map(str, cpus))) c_string = bytes(res, "ascii") bitmask = LIBNUMA.numa_parse_cpustring(c_string) op_res = LIBNUMA.numa_sched_setaffinity(pid, bitmask) if op_res == -1: raise Exception(f"attempt to run on {res} failed")
def set_preferred_node(node: int) -> None: LIBNUMA.numa_set_preferred(node)
def get_preferred_node() -> int: return LIBNUMA.numa_preferred()
def node_memory_info(node: int) -> tuple: free_size = c_longlong() total_size = LIBNUMA.numa_node_size64(node, free_size) return total_size, free_size.value
def get_affinitive_cpus(pid: int) -> List[int]: cpu_mask = LIBNUMA.numa_allocate_cpumask() LIBNUMA.numa_bitmask_clearall(cpu_mask) LIBNUMA.numa_sched_getaffinity(pid, cpu_mask) return numa_utils.get_bitset_list(cpu_mask)
def get_max_node() -> int: return LIBNUMA.numa_max_node()
def get_bitset_list(bitmask: bitmask_t) -> List[int]: return list( filter(lambda node: LIBNUMA.numa_bitmask_isbitset(bitmask, node) != 0, range(bitmask.contents.size)))
def set_local_alloc() -> None: LIBNUMA.numa_set_localalloc()
def cpu_to_node(cpu: int) -> int: return LIBNUMA.numa_node_of_cpu(cpu)
def numa_distance(node1: int, node2: int) -> int: return LIBNUMA.numa_distance(node1, node2)
def get_num_configured_cpus() -> int: return LIBNUMA.numa_num_configured_cpus()
def get_max_possible_node() -> int: return LIBNUMA.numa_max_possible_node()
def get_allowed_cpus_num() -> int: return LIBNUMA.numa_num_task_cpus()
def get_allowed_nodes_num() -> int: return LIBNUMA.numa_num_task_nodes()
def numa_available() -> bool: return LIBNUMA.numa_available() != -1