Skip to content

Commit 293bf14

Browse files
committed
Read APIC IDs from /proc/cpuinfo instead of sysfs
Not all systems expose /sys/devices/system/cpu/*/topology/apic_id. The apicid field in /proc/cpuinfo is more universally available. Signed-off-by: Cong Wang <cwang@multikernel.io>
1 parent 2aedb23 commit 293bf14

1 file changed

Lines changed: 15 additions & 17 deletions

File tree

src/kerf/init/main.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -348,30 +348,28 @@ def get_total_cpus_from_system() -> Optional[int]:
348348

349349
def get_valid_apic_ids_from_system() -> Optional[set]:
350350
"""
351-
Get set of valid APIC IDs from the system via sysfs.
352-
Reads /sys/devices/system/cpu/cpuN/topology/apic_id for each CPU.
351+
Get set of valid APIC IDs from the system via /proc/cpuinfo.
353352
Returns set of valid APIC IDs or None if not available.
354353
"""
355354
try:
356-
cpu_dir = Path('/sys/devices/system/cpu')
357-
if not cpu_dir.exists():
355+
cpuinfo_path = Path('/proc/cpuinfo')
356+
if not cpuinfo_path.exists():
358357
return None
359358

360359
apic_ids = set()
361-
cpu_files = [f for f in cpu_dir.iterdir() if f.name.startswith('cpu') and f.name[3:].isdigit()]
362-
363-
for cpu_path in cpu_files:
364-
apic_id_file = cpu_path / 'topology' / 'apic_id'
365-
if apic_id_file.exists():
366-
try:
367-
with open(apic_id_file, 'r', encoding='utf-8') as f:
368-
apic_id = int(f.read().strip())
369-
apic_ids.add(apic_id)
370-
except (ValueError, IOError):
371-
pass
360+
with open(cpuinfo_path, 'r', encoding='utf-8') as f:
361+
for line in f:
362+
if line.startswith('apicid'):
363+
parts = line.split(':')
364+
if len(parts) == 2:
365+
try:
366+
apic_id = int(parts[1].strip())
367+
apic_ids.add(apic_id)
368+
except ValueError:
369+
pass
372370

373371
return apic_ids if apic_ids else None
374-
except (OSError, ValueError):
372+
except (OSError, IOError):
375373
pass
376374

377375
return None
@@ -406,7 +404,7 @@ def build_baseline_from_cmdline(
406404
valid_apic_ids = get_valid_apic_ids_from_system()
407405
if valid_apic_ids is None:
408406
raise KernelInterfaceError(
409-
"Could not read APIC IDs from /sys/devices/system/cpu/*/topology/apic_id. "
407+
"Could not read APIC IDs from /proc/cpuinfo. "
410408
"Ensure the system exposes CPU topology information."
411409
)
412410

0 commit comments

Comments
 (0)