import assert from "node:assert/strict"; import { describe, it } from "node:test"; import { estimateVramMb } from "@/lib/deploy-command"; import { aggregateFdinfo, drmUtilization, parseFdinfo, powerBetween, readAmdUsage, readHwmon } from "./gpu-sysfs"; import type { SysFs } from "./hardware"; import { parseGpuLine, summarizeSamples } from "./resources"; import { parseDriGroups } from "./runtimes"; /** An in-memory sysfs: files plus directory links. */ function memFs(files: Record, links: Record = {}): SysFs { const resolve = (p: string) => { const hit = Object.keys(links).find((l) => p === l || p.startsWith(`${l}/`)); return hit ? links[hit] + p.slice(hit.length) : p; }; return { read: async (p) => files[resolve(p)]?.trim() ?? null, resolve: async (p) => resolve(p), list: async (d) => { const dir = `${resolve(d)}/`; return [...new Set(Object.keys(files).filter((k) => k.startsWith(dir)).map((k) => k.slice(dir.length).split("/")[0]))]; }, }; } const xe = (client: number, vramKiB: number, rcs: number, ccs: number, total: number) => `pos:\t0 flags:\t02100002 drm-driver:\txe drm-client-id:\t${client} drm-pdev:\t0000:03:00.0 drm-total-system:\t0 drm-resident-system:\t0 drm-total-gtt:\t12 KiB drm-total-vram0:\t${vramKiB + 1024} KiB drm-resident-vram0:\t${vramKiB} KiB drm-cycles-rcs:\t${rcs} drm-total-cycles-rcs:\t${total} drm-cycles-ccs:\t${ccs} drm-total-cycles-ccs:\t${total} `; describe("DRM fdinfo", () => { it("parses xe: resident VRAM, cycles per engine class", () => { const c = parseFdinfo(xe(42, 9_437_184, 1000, 30_000, 50_000))!; assert.equal(c.pdev, "0000:03:00.0"); assert.equal(c.clientId, "42"); assert.equal(c.vramBytes, 9_437_184 * 1024); assert.deepEqual(c.cycles, { rcs: 1000, ccs: 30_000 }); assert.deepEqual(c.totalCycles, { rcs: 50_000, ccs: 50_000 }); }); it("parses i915 (local memory, nanoseconds) and amdgpu's legacy keys", () => { const i915 = parseFdinfo("drm-driver: i915\ndrm-pdev: 0000:00:02.0\ndrm-client-id: 7\ndrm-engine-render: 1000000 ns\ndrm-engine-capacity-video: 2\ndrm-total-local0: 1024 KiB\ndrm-resident-local0: 512 KiB\n")!; assert.equal(i915.vramBytes, 512 * 1024); assert.deepEqual(i915.ns, { render: 1_000_000 }); const amd = parseFdinfo("drm-driver:\tamdgpu\ndrm-pdev:\t0000:0a:00.0\ndrm-client-id:\t3\ndrm-memory-vram:\t2048 KiB\ndrm-memory-gtt:\t16 KiB\ndrm-engine-gfx:\t5000 ns\n")!; assert.equal(amd.vramBytes, 2048 * 1024); assert.deepEqual(amd.ns, { gfx: 5000 }); assert.equal(parseFdinfo("pos: 0\nflags: 02\n"), null); }); it("counts each client once and takes the GPU clock as is", () => { const s = aggregateFdinfo(1000, [xe(1, 1024, 10, 20, 100), xe(1, 1024, 10, 20, 100), xe(2, 2048, 5, 0, 110), "pos: 0\n"]); const g = s.gpus["0000:03:00.0"]; assert.equal(g.vramBytes, 3072 * 1024); assert.deepEqual(g.cycles, { rcs: 15, ccs: 20 }); assert.deepEqual(g.totalCycles, { rcs: 110, ccs: 110 }); }); it("reports the busiest engine class between two snapshots", () => { const a = aggregateFdinfo(0, [xe(1, 1024, 0, 0, 1000)]).gpus["0000:03:00.0"]; const b = aggregateFdinfo(2000, [xe(1, 1024, 100, 800, 2000)]).gpus["0000:03:00.0"]; assert.equal(drmUtilization(a, b, 2), 80); assert.equal(drmUtilization(undefined, b, 2), null); // A client that exited makes the sum drop: idle, not negative. const c = aggregateFdinfo(4000, [xe(2, 0, 0, 0, 3000)]).gpus["0000:03:00.0"]; assert.equal(drmUtilization(b, c, 2), 0); // Nanosecond engines: busy time over wall time. assert.equal(drmUtilization({ vramBytes: 0, cycles: {}, totalCycles: {}, ns: { gfx: 0 } }, { vramBytes: 0, cycles: {}, totalCycles: {}, ns: { gfx: 1e9 } }, 2), 50); }); }); describe("hwmon and amdgpu sysfs", () => { const dev = "/sys/devices/pci0000:00/0000:00:01.0/0000:03:00.0"; const fs = memFs( { [`${dev}/hwmon/hwmon3/name`]: "xe", [`${dev}/hwmon/hwmon3/energy1_input`]: "5000000", [`${dev}/hwmon/hwmon3/power1_max`]: "200000000", [`${dev}/hwmon/hwmon3/temp2_input`]: "61000", [`${dev}/hwmon/hwmon3/temp2_label`]: "pkg", [`${dev}/hwmon/hwmon3/temp3_input`]: "70000", [`${dev}/hwmon/hwmon3/temp3_label`]: "vram", "/sys/devices/pci0000:00/0000:0a:00.0/hwmon/hwmon1/power1_average": "150000000", "/sys/devices/pci0000:00/0000:0a:00.0/hwmon/hwmon1/temp1_input": "55000", "/sys/devices/pci0000:00/0000:0a:00.0/mem_info_vram_used": String(4 * 2 ** 30), "/sys/devices/pci0000:00/0000:0a:00.0/gpu_busy_percent": "87", }, { "/sys/bus/pci/devices/0000:03:00.0": dev, "/sys/bus/pci/devices/0000:0a:00.0": "/sys/devices/pci0000:00/0000:0a:00.0" }, ); it("reads xe's energy counter and package temperature", async () => { assert.deepEqual(await readHwmon(fs, "0000:03:00.0"), { energyUj: 5_000_000, powerW: null, tempC: 61 }); }); it("reads amdgpu's power, temperature, VRAM in use and busy percent", async () => { assert.deepEqual(await readHwmon(fs, "0000:0a:00.0"), { energyUj: null, powerW: 150, tempC: 55 }); assert.deepEqual(await readAmdUsage(fs, "0000:0a:00.0"), { vramUsedMb: 4096, util: 87 }); }); it("turns energy into power between readings", () => { assert.equal(powerBetween({ energyUj: 1_000_000, powerW: null, tempC: null }, { energyUj: 401_000_000, powerW: null, tempC: null }, 2), 200); assert.equal(powerBetween(null, { energyUj: 5, powerW: null, tempC: null }, 0), null); assert.equal(powerBetween(null, { energyUj: null, powerW: 150, tempC: null }, 0), 150); }); }); describe("GPU summaries", () => { it("keeps unknown figures unknown and adds temperature", () => { const s = summarizeSamples( [], [ { index: 0, name: "Intel Arc Pro B65", pciAddress: "0000:03:00.0", vramTotalMb: 32768, vramUsedMb: null, util: null, powerW: 120, tempC: 60 }, { index: 0, name: "Intel Arc Pro B65", pciAddress: "0000:03:00.0", vramTotalMb: 32768, vramUsedMb: null, util: null, powerW: 180, tempC: 70 }, ], 4, ); assert.deepEqual(s.gpus[0], { index: 0, name: "Intel Arc Pro B65", pciAddress: "0000:03:00.0", vramTotalMb: 32768, vramPeakMb: null, vramAvgMb: null, utilAvg: null, utilPeak: null, powerAvgW: 150, powerPeakW: 180, tempAvgC: 65, tempPeakC: 70, }); assert.equal(parseGpuLine("0, NVIDIA GeForce RTX 3080, 10240, 812, 3, 21.5, 4, 16, 47")?.tempC, 47); }); it("reads /dev/dri group IDs", () => { assert.deepEqual(parseDriGroups(["/dev/dri/card0 44", "/dev/dri/renderD128 993", "/dev/dri/renderD129 993", "/dev/dri/by-path 0", "stat: error"]), [44, 993]); }); }); describe("VRAM estimate", () => { const GiB = 2 ** 30; it("adds weights, KV cache and buffers for llama.cpp with all layers on the GPU", () => { const f16 = estimateVramMb(5 * GiB, 32768, "llama.cpp", {}, 32768)!; const q8 = estimateVramMb(5 * GiB, 32768, "llama.cpp", { cacheTypeK: "q8_0", cacheTypeV: "q8_0" }, 32768)!; assert.ok(f16 > 5120 + 1024 && f16 < 5120 + 1024 + 8192, String(f16)); assert.ok(q8 < f16); assert.equal(estimateVramMb(5 * GiB, 4096, "llama.cpp", { gpuLayers: "all" }, null), estimateVramMb(5 * GiB, 4096, "llama.cpp", {}, null)); }); it("can't tell partial offload", () => { assert.equal(estimateVramMb(5 * GiB, 4096, "llama.cpp", { gpuLayers: "20" }, 32768), null); assert.equal(estimateVramMb(5 * GiB, 4096, "llama.cpp", { cpuMoe: true }, 32768), null); }); it("uses vLLM's memory share of the VRAM", () => { assert.equal(estimateVramMb(5 * GiB, 4096, "vllm", {}, 32768), Math.ceil(32768 * 0.9)); assert.equal(estimateVramMb(5 * GiB, 4096, "vllm", { gpuMemoryUtilization: 0.5 }, 32768), 16384); assert.equal(estimateVramMb(5 * GiB, 4096, "vllm", {}, null), null); }); });