[CPU] Create Proper Numa topology for s390x (#40714)

Signed-off-by: Rehan Khan <Rehan.Khan7@ibm.com>
Co-authored-by: Li, Jiang <jiang1.li@intel.com>
This commit is contained in:
Rehan Khan
2026-07-13 12:58:43 +08:00
committed by GitHub
co-authored by Li, Jiang <jiang1.li@intel.com>
parent 8c5dafcd09
commit 9e57de7197
7 changed files with 84 additions and 105 deletions
+49 -4
View File
@@ -3,6 +3,7 @@
import json
import os
import platform
import subprocess
import sys
from dataclasses import dataclass
@@ -216,9 +217,14 @@ def _get_cpu_list() -> list[LogicalCPUInfo]:
# For MacOS, no user-level CPU affinity and SMT, return all CPUs
return _synthesize_cpu_list()
lscpu_output = subprocess.check_output(
"lscpu --json --extended=CPU,CORE,NODE --online", shell=True, text=True
)
if platform.machine() == "s390x":
lscpu_output = subprocess.check_output(
"lscpu -J -e=CPU,CORE,NODE,SOCKET,BOOK", shell=True, text=True
)
else:
lscpu_output = subprocess.check_output(
"lscpu --json --extended=CPU,CORE,NODE --online", shell=True, text=True
)
# For platforms without NUMA, map bare `-` node to 0 so non-NUMA
# systems keep the existing behavior from #39781.
@@ -233,11 +239,50 @@ def _get_cpu_list() -> list[LogicalCPUInfo]:
lscpu_output,
)
if platform.machine() == "s390x":
# On s390x, use the best topology level as the abstract CPU group
# key via numa_node. The hierarchy is book > socket > core; prefer
# book, fall back to socket if only one book exists.
lscpu_output = re.sub(r'"book":\s*-\s*(,|\n|\})', r'"book": 0\1', lscpu_output)
lscpu_output = re.sub(
r'"socket":\s*-\s*(,|\n|\})', r'"socket": 0\1', lscpu_output
)
raw_cpus = json.loads(lscpu_output)["cpus"]
book_values = set()
socket_values = set()
for entry in raw_cpus:
book_values.add(LogicalCPUInfo._int(str(entry.get("book", "-1"))))
socket_values.add(LogicalCPUInfo._int(str(entry.get("socket", "-1"))))
# Pick the best partitioning level: book first, then socket
if len(book_values - {-1}) > 1:
group_key = "book"
elif len(socket_values - {-1}) > 1:
group_key = "socket"
else:
group_key = None
if group_key is not None:
logical_book_socket_list: list[LogicalCPUInfo] = []
for entry in raw_cpus:
cpu_id = LogicalCPUInfo._int(str(entry.get("cpu", "-1")))
core = LogicalCPUInfo._int(str(entry.get("core", "-1")))
group = LogicalCPUInfo._int(str(entry.get(group_key, "-1")))
if -1 not in (cpu_id, core, group):
logical_book_socket_list.append(
LogicalCPUInfo(id=cpu_id, physical_core=core, numa_node=group)
)
if not logical_book_socket_list:
return _synthesize_cpu_list()
return logical_book_socket_list
logical_cpu_list: list[LogicalCPUInfo] = json.loads(
lscpu_output, object_hook=LogicalCPUInfo.json_decoder
)["cpus"]
# Filter CPUs with invalid attributes
# Filter CPUs with invalid attributes (only require id, core, node)
logical_cpu_list = [
x for x in logical_cpu_list if -1 not in (x.id, x.physical_core, x.numa_node)
]