Files
nvdriver/tools/run_live_control.py
morebrackets 1ee1639002 Address CodeRabbit follow-up findings on #276
- tools/run_live_control.py (4778363104): fail closed when /sys/bus/pci/devices
  is unavailable instead of returning an empty inventory.
- tools/summarize_original_dispatch.py (4778363107): validate required string
  'stage' field when loading sites.json.
- tools/reconcile_native_fifo_live_bootstrap.py (4778363084, 4778363095):
  include index.json and _nv028064rm.rea.json in inputs_sha256 / existence
  checks and require len(groups) == 8 before reporting eight validated groups.
- tests/offline/test_live_control_and_summary_tools.py: add unit tests for
  missing PCI sysfs dir and malformed sites.json.
2026-12-02 17:05:52 -08:00

101 lines
4.3 KiB
Python

#!/usr/bin/env python3
"""Load only the isolated empty-target control module and record actual exits."""
from pathlib import Path
import subprocess, json, hashlib, os, sys
R = Path(__file__).resolve().parent.parent
def bindings():
d = {}
pci_root = Path('/sys/bus/pci/devices')
if not pci_root.is_dir():
raise RuntimeError(f'PCI device inventory unavailable: {pci_root} is not a directory')
for f in pci_root.iterdir():
try:
if (f / 'vendor').read_text(encoding='utf-8').strip() == '0x10de' and int((f / 'class').read_text(encoding='utf-8'), 16) >> 16 == 3:
d[f.name] = {
'driver': (f / 'driver').resolve().name if (f / 'driver').exists() else None,
'device': (f / 'device').read_text(encoding='utf-8').strip(),
}
except (OSError, ValueError):
pass
return d
def main():
if os.geteuid() != 0:
raise SystemExit('Run with sudo; test node is root-only')
module = R / 'kernel-open/nvidia_rs.ko'
module_sys = Path('/sys/module/nvidia_rs')
if not module.is_file():
raise SystemExit(f'Missing built kernel module: {module}')
if module_sys.exists():
raise SystemExit('Refusing to replace an already loaded module')
compat_bin = R / 'target/compat32-rust-driver'
if not compat_bin.is_file():
raise SystemExit(f'Missing compat32 test binary: {compat_bin}')
before = bindings()
runs = []
loaded = False
failure = None
report = {
'scope': 'Live isolated control tests with empty pci_target. No GPU binding or DMA activation.',
'module_sha256': hashlib.sha256(module.read_bytes()).hexdigest(),
'pci_before': before,
}
try:
r = subprocess.run(['insmod', str(module), 'pci_target='], capture_output=True, text=True, timeout=30)
runs.append({'command': 'insmod', 'exit_code': r.returncode, 'stderr': r.stderr})
r.check_returncode()
loaded = True
pci_target = (module_sys / 'parameters/pci_target').read_text(encoding='utf-8').strip()
report['pci_target'] = pci_target
if pci_target != '':
raise RuntimeError(f'expected empty pci_target parameter, got {pci_target!r}')
py = sys.executable
for command in [
[str(compat_bin)],
[py, str(R / 'tools/live_control_test.py')],
[py, str(R / 'tools/probe_original_clients.py'), '/dev/nvidia-rsctl'],
[py, str(R / 'tools/probe_original_software_controls_replay.py')],
]:
r = subprocess.run(
command,
capture_output=True,
text=True,
timeout=30,
env={**os.environ, 'NV_RS_NODE': '/dev/nvidia-rsctl'},
)
runs.append({'command': command, 'exit_code': r.returncode, 'stdout': r.stdout, 'stderr': r.stderr})
if command[-1].endswith('probe_original_software_controls_replay.py') and r.returncode == 0:
payload = json.loads(r.stdout)
out_path = R / 'reports/software-controls-rust-live.json'
out_path.parent.mkdir(parents=True, exist_ok=True)
out_path.write_text(json.dumps(payload, indent=2) + '\n', encoding='utf-8')
r.check_returncode()
except BaseException as e:
failure = str(e)
finally:
if loaded:
try:
r = subprocess.run(['rmmod', 'nvidia_rs'], capture_output=True, text=True, timeout=30)
runs.append({'command': 'rmmod', 'exit_code': r.returncode, 'stderr': r.stderr})
except (OSError, subprocess.TimeoutExpired) as e:
runs.append({'command': 'rmmod', 'exit_code': -1, 'stderr': str(e)})
failure = f'{failure}; rmmod failed: {e}' if failure else f'rmmod failed: {e}'
report.update(runs=runs, error=failure, pci_after=bindings(), module_unloaded=not module_sys.exists())
report['passed'] = (
failure is None
and all(r['exit_code'] == 0 for r in runs)
and report['module_unloaded']
and report['pci_before'] == report['pci_after']
)
print(json.dumps(report, indent=2))
return 0 if report['passed'] else 1
if __name__ == '__main__':
raise SystemExit(main())