Files
nvdriver/tools/extract_rmapi_memory_workload.py
nvidia-driver-rs 4526e6a816 Baseline: validated 30/30 tree with passing Level 7 hardware regression
- Updated tools/post_reboot.sh to check kernel oops taint bit and stuck driver tasks instead of <86400s uptime
- Level 7 hardware regression passed on 0000:b3:00.0 (module 37e6fa430835eee2b426019db3ad70eeb7a9e9070c09c84bd9b973cf56cbe529, 107 inventory words, 0 static mismatches, CUDA baseline verified before and after)
- Updated .gitignore to exclude build archives (*.a, *.rlib), __pycache__, and raw trace dumps
2026-11-23 23:48:38 +00:00

112 lines
4.9 KiB
Python

#!/usr/bin/env python3
"""Extract the memory-object requests of one GPU from a captured CUDA run of the original.
Input: the ioctl payload capture of the original driver on this machine
(`cuda-compute-exact-allocation-ioctl-payloads.jsonl`, one JSON object per ioctl with the
escape block before and after and the parameter block before and after). Output:
`tools/rmapi_memory_workload.json`, the ordered requests that concern the memory objects of
the device the context was created on (the last device allocated; the other seven GPUs are
released after `cuInit`), with the original's replies:
alloc NV_ESC_RM_ALLOC of classes 0x90f1, 0x503c, 0x50a0, 0x40, 0x3e under that device
osdesc NV_ESC_RM_ALLOC_MEMORY (class 0x71)
map NV_ESC_RM_MAP_MEMORY of one of those memory objects
unmap NV_ESC_RM_UNMAP_MEMORY
update NV_ESC_RM_UPDATE_DEVICE_MAPPING_INFO
control 0xd01, 0x503c0102, 0x503c0104, 0x503c0105 that name one of those objects
free NV_ESC_RM_FREE of one of those objects
Handles are the ones libcuda chose (0x5c0000xx); the client handle is replaced by the
marker 0xc1d00001 and the device and subdevice by 0xcaf00000 and 0xcaf00001 wherever they
occur as whole 32-bit words in a request or reply. User addresses stay as captured. The
checked-in JSON is what the oracle tool and the Rust replay test read; this script only
regenerates it (`--check` compares).
"""
import json
import struct
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
CAPTURE = Path("/home/geodd/nvidia-driver/analysis-580.178.04/reports/"
"cuda-compute-exact-allocation-ioctl-payloads.jsonl")
TARGET = ROOT / "tools/rmapi_memory_workload.json"
CLASSES = (0x90f1, 0x503c, 0x50a0, 0x40, 0x3e)
CONTROLS = (0xd01, 0x503c0102, 0x503c0104, 0x503c0105)
def main():
rows = [json.loads(line) for line in CAPTURE.read_text().splitlines()]
rows = [row for row in rows if "nvidia-uvm" not in row["device"]]
def number(row):
return int(row["request"], 16) & 0xff
def words(text):
data = bytes.fromhex(text)
return struct.unpack(f"<{len(data) // 4}I", data[:len(data) // 4 * 4])
devices, client = [], None
for row in rows:
if number(row) == 0x2b and words(row["after"])[3] == 0x80:
devices.append(words(row["after"])[2])
client = words(row["after"])[0]
device = devices[-1]
subdevice = next(words(row["after"])[2] for row in rows
if number(row) == 0x2b and words(row["after"])[3] == 0x2080
and words(row["after"])[1] == device)
rename = {client: 0xc1d00001, device: 0xcaf00000, subdevice: 0xcaf00001}
def canonical(text):
data = bytearray(bytes.fromhex(text))
for offset in range(0, len(data) - 3, 4):
value = struct.unpack_from("<I", data, offset)[0]
if value in rename:
struct.pack_into("<I", data, offset, rename[value])
return data.hex()
objects, operations = {device, subdevice}, []
for index, row in enumerate(rows):
escape = number(row)
before, after = words(row["before"]), words(row["after"])
kind = None
if escape == 0x2b and after[3] in CLASSES and after[1] in objects:
kind = "alloc"
objects.add(after[2])
elif escape == 0x27 and after[1] in objects:
kind = "osdesc"
objects.add(after[2])
elif escape in (0x4e, 0x4f, 0x5e) and after[2] in objects - {device, subdevice}:
kind = {0x4e: "map", 0x4f: "unmap", 0x5e: "update"}[escape]
elif escape == 0x2a and after[2] in CONTROLS:
named = words(row["params_before"])[0] if row["params_before"] else 0
if after[2] == 0xd01 and named not in objects - {device, subdevice}:
continue
if after[2] != 0xd01 and after[1] not in objects:
continue
kind = "control"
elif escape == 0x29 and after[2] in objects - {device, subdevice}:
kind = "free"
objects.discard(after[2])
if kind is None:
continue
operations.append(dict(
kind=kind, index=index, node="ctl" if row["device"].endswith("ctl") else "dev",
request=canonical(row["before"]), reply=canonical(row["after"]),
params=canonical(row["params_before"]) if row["params_before"] else None,
params_reply=canonical(row["params_after"]) if row["params_after"] else None))
text = json.dumps(dict(source=CAPTURE.name, client=0xc1d00001, device=0xcaf00000,
subdevice=0xcaf00001, operations=operations), indent=1) + "\n"
if "--check" in sys.argv:
assert TARGET.read_text() == text, "workload extract differs from the checked copy"
else:
TARGET.write_text(text)
counts = {}
for operation in operations:
counts[operation["kind"]] = counts.get(operation["kind"], 0) + 1
print(json.dumps(counts))
if __name__ == "__main__":
main()