Files
nvdriver/tools/check_system_time_source.py
morebrackets 7818fd7a18 tools: make UVM/TLB/time C oracles portable and harden control compare/capture
- tools/check_uvm_ioctl_layout.py: resolve NVIDIA 580.178.04 sources via
  NV_RS_KERNEL_SRC / NV_RS_ANALYSIS_ROOT / /usr/src/nvidia-580.178.04 /
  /usr/src/nvidia-srv-580.178.04 instead of failing when /home/geodd is
  absent; replace -O-disabled asserts with explicit errors and argparse.
- tools/check_gv100_tlb_oracle.py and tools/check_system_time_source.py:
  resolve kernel sources portably, wrap execution in main(), create
  report parent dirs, and replace -O-disabled asserts.
- tools/compare_software_controls.py: wrap in main(), guard unknown
  ioctl kinds instead of KeyError, create output parent dirs, and use
  single-pass boundary-aware hex handle normalization so chained or
  prefix-overlapping handle mappings do not corrupt labels.
- tools/ioctl_capture/cuda_basic.py: set explicit 64-bit ctypes argtypes
  for all CUDA Driver API calls, fail non-zero on H2D/D2H roundtrip
  mismatch, and clean up allocations/context in a try/finally inside
  main().
- tests/offline/test_oracle_portability_and_capture.py: offline unit
  tests covering path resolution, CLI validation, single-pass handle
  normalization, and 64-bit ctypes signatures.
2026-12-02 04:16:27 -08:00

116 lines
3.8 KiB
Python

#!/usr/bin/env python3
"""Compare unchanged shipped OS time conversion with the Linux adapter body."""
import hashlib
import json
import os
import subprocess
from pathlib import Path
R = Path(__file__).resolve().parents[1]
REPLACEMENT = R / "kernel-open/nvidia/clock_adapter.c"
def resolve_original_os_interface():
candidates = []
if os.environ.get("NV_RS_KERNEL_SRC"):
candidates.append(Path(os.environ["NV_RS_KERNEL_SRC"]) / "nvidia/os-interface.c")
if os.environ.get("NV_RS_ANALYSIS_ROOT"):
candidates.append(
Path(os.environ["NV_RS_ANALYSIS_ROOT"]) / "extracted/kernel/nvidia/os-interface.c"
)
candidates.extend([
Path("/home/geodd/nvidia-driver/analysis-580.178.04/extracted/kernel/nvidia/os-interface.c"),
Path("/usr/src/nvidia-580.178.04/nvidia/os-interface.c"),
Path("/usr/src/nvidia-srv-580.178.04/nvidia/os-interface.c"),
])
for candidate in candidates:
if candidate.is_file():
return candidate
raise SystemExit(
"Unable to locate nvidia/os-interface.c; set NV_RS_KERNEL_SRC or NV_RS_ANALYSIS_ROOT."
)
def body(text, signature):
start = text.index(signature)
pos = text.index("{", start)
depth = 0
for end in range(pos, len(text)):
depth += (text[end] == "{") - (text[end] == "}")
if depth == 0:
return text[start:end + 1]
raise ValueError(signature)
PROLOGUE = '''#include <stdint.h>
#include <stdio.h>
#include <assert.h>
typedef uint32_t u32;
typedef uint32_t NvU32;
typedef uint32_t NV_STATUS;
#define NV_API_CALL
#define NV_OK 0
#define NSEC_PER_USEC 1000
struct timespec64 { int64_t tv_sec; long tv_nsec; };
static struct timespec64 clock_value;
static void ktime_get_real_ts64(struct timespec64 *out) { *out=clock_value; }
'''
EPILOGUE = '''
int main(void) {
int64_t seconds[]={0,1,-1,INT32_MAX,UINT32_MAX,INT64_C(4294967296),INT64_MAX};
long nanoseconds[]={0,1,999,1000,999999999};
unsigned count=0;
for(unsigned s=0;s<sizeof(seconds)/sizeof(seconds[0]);s++) {
for(unsigned n=0;n<sizeof(nanoseconds)/sizeof(nanoseconds[0]);n++) {
uint32_t old_s=0,old_u=0,new_s=0,new_u=0;
clock_value=(struct timespec64){seconds[s],nanoseconds[n]};
assert(os_get_system_time(&old_s,&old_u)==0);
nv_rs_clock_system_time(&new_s,&new_u);
assert(old_s==new_s && old_u==new_u);
count++;
}
}
printf("%u\\n",count);
}
'''
def main():
original = resolve_original_os_interface()
out = R / "target/system-time-source"
out.mkdir(parents=True, exist_ok=True)
c = out / "original-and-replacement.c"
exe = out / "comparison"
c.write_text(
PROLOGUE
+ body(original.read_text(), "NV_STATUS NV_API_CALL os_get_system_time(")
+ "\n"
+ body(REPLACEMENT.read_text(), "void nv_rs_clock_system_time(")
+ EPILOGUE
)
subprocess.run(["cc", "-std=c11", "-Wall", "-Wextra", "-Werror", str(c), "-o", str(exe)], check=True)
cases = int(subprocess.check_output([str(exe)], text=True))
if cases != 35:
raise SystemExit(f"expected 35 system-time comparison cases, got {cases}")
report = {
"passed": True,
"cases": cases,
"source_sha256": {
str(p): hashlib.sha256(p.read_bytes()).hexdigest() for p in [original, REPLACEMENT]
},
"scope": (
"Unchanged original and adapter conversion bodies compiled with the same scripted "
"ktime_get_real_ts64. Includes negative/wrapping seconds and microsecond truncation. "
"Does not compare simultaneous live wall-clock samples."
),
}
report_path = R / "reports/system-time-source.json"
report_path.parent.mkdir(parents=True, exist_ok=True)
report_path.write_text(json.dumps(report, indent=2) + "\n")
print(cases, "original/adapter system-time conversion cases")
if __name__ == "__main__":
main()