#!/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("