import assert from "node:assert/strict"; import { before, describe, it } from "node:test"; import { fetchEngineInfo, parseLlamaProps, parseVllm, versionFromLog } from "./engine-info"; import { currentLinks, detectHardware, gpuDisplayName, type SysFs, largestBarMb, mountDevice, normalizePciAddress, parseCpu, parseP2p, pciName } from "./hardware"; import { fastestLinks, parseGpuLine, summarizeSamples } from "./resources"; import { parseNvidiaSmi, repoDigest } from "./runtimes"; const GiB = 2 ** 30; const hex = (n: number) => `0x${n.toString(16).padStart(16, "0")}`; /** A `resource` line: start, end, flags (0x0014220c = 64-bit prefetchable memory). */ const bar = (start: number, size: number) => `${hex(start)} ${hex(start + size - 1)} 0x000000000014220c`; const EMPTY = `${hex(0)} ${hex(0)} 0x0000000000000000`; const PCI_IDS = `# test pci.ids 10de NVIDIA Corporation \t2684 AD102 [GeForce RTX 4090] 8086 Intel Corporation \te211 Battlemage G31 [Arc Pro B65] \t\t8086 1234 Arc Pro B65 32GB \te20b Battlemage G21 [Arc B580] 1af4 Red Hat, Inc. `; const CPUINFO = [0, 1, 2, 3] .map((p) => `processor\t: ${p}\nmodel name\t: AMD EPYC 9124 16-Core Processor\nphysical id\t: 0\ncore id\t\t: ${p % 2}\nflags\t\t: fpu avx2 hypervisor\n`) .join("\n"); const files = new Map(); const links = new Map(); /** An in-memory sysfs: files plus directory symlinks (sysfs names contain ":", which Windows can't store). */ function memFs(): SysFs { const resolve = (p: string): string => { for (let i = 0; i < 10; i++) { const hit = [...links.keys()].find((l) => p === l || p.startsWith(`${l}/`)); if (!hit) return p; p = links.get(hit)! + p.slice(hit.length); } return p; }; return { read: async (p) => files.get(resolve(p))?.trim() ?? null, resolve: async (p) => resolve(p), list: async (d) => { const dir = `${resolve(d)}/`; const names = [...files.keys(), ...links.keys()].filter((k) => k.startsWith(dir)).map((k) => k.slice(dir.length).split("/")[0]); return [...new Set(names)]; }, }; } const file = (path: string, content: string) => files.set(`/${path}`, content); const link = (path: string, target: string) => links.set(`/${path}`, `/${target}`); before(() => { // Intel Arc Pro B65: root port 00:01.0 → on-card switch (01:00.0 up, 02:01.0 down) → GPU 03:00.0. const rp = "sys/devices/pci0000:00/0000:00:01.0"; const up = `${rp}/0000:01:00.0`; const down = `${up}/0000:02:01.0`; const gpu = `${down}/0000:03:00.0`; file(`${rp}/vendor`, "0x1022"); file(`${up}/vendor`, "0x8086"); file(`${up}/current_link_speed`, "16.0 GT/s PCIe"); file(`${up}/current_link_width`, "8"); file(`${up}/max_link_speed`, "32.0 GT/s PCIe"); file(`${up}/max_link_width`, "16"); file(`${down}/vendor`, "0x8086"); file(`${gpu}/vendor`, "0x8086"); file(`${gpu}/device`, "0xe211"); file(`${gpu}/subsystem_vendor`, "0x8086"); file(`${gpu}/subsystem_device`, "0x1234"); file(`${gpu}/class`, "0x030000"); file(`${gpu}/uevent`, "DRIVER=xe\nPCI_CLASS=30000\nPCI_SLOT_NAME=0000:03:00.0"); // What the GPU function itself reports: the switch-internal link. file(`${gpu}/current_link_speed`, "2.5 GT/s PCIe"); file(`${gpu}/current_link_width`, "1"); file(`${gpu}/resource`, [bar(0xa0000000, 16 * 2 ** 20), EMPTY, bar(0x4000000000, 32 * GiB)].join("\n")); file(`${gpu}/tile0/physical_vram_size_bytes`, String(32 * GiB)); link("sys/bus/pci/devices/0000:03:00.0", gpu); // NVIDIA card directly on a root port, 256 MiB BAR (no ReBAR). const nv = "sys/devices/pci0000:00/0000:00:03.0/0000:05:00.0"; file("sys/devices/pci0000:00/0000:00:03.0/vendor", "0x1022"); file(`${nv}/vendor`, "0x10de"); file(`${nv}/device`, "0x2684"); file(`${nv}/class`, "0x030000"); file(`${nv}/uevent`, "DRIVER=nvidia"); file(`${nv}/current_link_speed`, "16.0 GT/s PCIe"); file(`${nv}/current_link_width`, "16"); file(`${nv}/max_link_speed`, "16.0 GT/s PCIe"); file(`${nv}/max_link_width`, "16"); file(`${nv}/resource`, [bar(0xb0000000, 16 * 2 ** 20), bar(0x6000000000, 256 * 2 ** 20)].join("\n")); link("sys/bus/pci/devices/0000:05:00.0", nv); file("sys/module/nvidia/version", "580.82.07"); // A virtio NIC: not a GPU. file("sys/devices/pci0000:00/0000:00:12.0/vendor", "0x1af4"); file("sys/devices/pci0000:00/0000:00:12.0/class", "0x020000"); link("sys/bus/pci/devices/0000:00:12.0", "sys/devices/pci0000:00/0000:00:12.0"); file("usr/share/misc/pci.ids", PCI_IDS); file("proc/cpuinfo", CPUINFO); file("proc/meminfo", "MemTotal: 65536000 kB\nMemFree: 1 kB\n"); file("sys/class/dmi/id/sys_vendor", "QEMU"); file("sys/class/dmi/id/product_name", "Standard PC (Q35 + ICH9, 2009)"); // Models on /dev/nvme0n1p2 (259:2). file("proc/self/mountinfo", ["22 1 259:1 / / rw - ext4 /dev/nvme0n1p1 rw", "1521 1471 259:2 /srv/models /models rw,relatime - ext4 /dev/nvme0n1p2 rw"].join("\n")); const disk = "sys/devices/pci0000:00/0000:00:06.0/nvme/nvme0/nvme0n1"; file(`${disk}/device/model`, "Samsung SSD 990 PRO 2TB "); file(`${disk}/queue/rotational`, "0"); file(`${disk}/size`, String(3_907_029_168)); file(`${disk}/nvme0n1p2/partition`, "2"); link("sys/dev/block/259:2", `${disk}/nvme0n1p2`); }); describe("hardware detection (sysfs fixture)", () => { it("finds GPUs, the card's upstream link behind an on-card switch, ReBAR and names", async () => { const hw = await detectHardware({ fs: memFs(), modelsDir: "/models", kernel: "6.17.1-pve", nvidia: [{ vendor: "nvidia", index: 0, uuid: "GPU-x", name: "NVIDIA GeForce RTX 4090", memoryMb: 24564, driver: "580.82.07", pciAddress: "00000000:05:00.0" }], }); assert.equal(hw.gpus.length, 2); const [intel, nv] = hw.gpus; assert.deepEqual( { ...intel }, { vendor: "intel", pciAddress: "0000:03:00.0", name: "Intel Arc Pro B65 32GB", vramMb: 32768, driver: "xe", driverVersion: "kernel 6.17.1-pve", linkCurrent: { gen: 4, width: 8 }, linkMax: { gen: 5, width: 16 }, barMb: 32768, rebar: true, }, ); assert.equal(nv.name, "NVIDIA GeForce RTX 4090"); assert.equal(nv.vramMb, 24564); assert.equal(nv.driverVersion, "580.82.07"); assert.deepEqual(nv.linkCurrent, { gen: 4, width: 16 }); assert.equal(nv.barMb, 256); assert.equal(nv.rebar, false); assert.equal(hw.p2p, null); }); it("reads CPU, RAM, VM and the models disk", async () => { const hw = await detectHardware({ fs: memFs(), modelsDir: "/models/", kernel: "6.17.1-pve" }); assert.deepEqual(hw.cpu, { model: "AMD EPYC 9124 16-Core Processor", cores: 2, threads: 4 }); assert.equal(hw.memoryBytes, 65536000 * 1024); assert.deepEqual(hw.virtualization, { vm: true, hypervisor: "KVM/QEMU" }); assert.deepEqual(hw.storage, { model: "Samsung SSD 990 PRO 2TB", kind: "nvme", sizeBytes: 3_907_029_168 * 512 }); }); it("doesn't fail on a host without any of it", async () => { const empty: SysFs = { read: async () => null, list: async () => [], resolve: async (p) => p }; const hw = await detectHardware({ fs: empty, modelsDir: "/models", kernel: null }); assert.deepEqual(hw.gpus, []); assert.equal(hw.storage, null); assert.deepEqual(hw.cpu, { model: null, cores: null, threads: null }); assert.deepEqual(hw.virtualization, { vm: null, hypervisor: null }); }); }); describe("hardware parsers", () => { it("normalizes nvidia-smi bus IDs and reads them from the probe CSV", () => { assert.equal(normalizePciAddress("00000000:0A:00.0"), "0000:0a:00.0"); assert.deepEqual(parseNvidiaSmi("0, GPU-abc, NVIDIA RTX A6000, 49140, 580.82.07, 00000000:01:00.0")[0].pciAddress, "0000:01:00.0"); assert.equal(parseNvidiaSmi("0, GPU-abc, NVIDIA RTX A6000, 49140, 580.82.07")[0].pciAddress, null); }); it("takes the largest memory BAR", () => { assert.equal(largestBarMb([bar(0x1000, 2 ** 20), bar(0x100000000, 8 * GiB), "0x0000000000001000 0x000000000000107f 0x0000000000040101"].join("\n")), 8192); assert.equal(largestBarMb(EMPTY), null); assert.equal(largestBarMb(null), null); }); it("names GPUs from pci.ids, board name first", () => { assert.equal(pciName(PCI_IDS, "0x8086", "0xe20b"), "Battlemage G21 [Arc B580]"); assert.equal(pciName(PCI_IDS, "0x8086", "0xe211", "0x8086", "0x1234"), "Arc Pro B65 32GB"); assert.equal(pciName(PCI_IDS, "0x8086", "0xffff"), null); assert.equal(gpuDisplayName("intel", "Battlemage G21 [Arc B580]"), "Intel Arc B580"); assert.equal(gpuDisplayName("nvidia", "AD102 [GeForce RTX 4090]"), "NVIDIA GeForce RTX 4090"); }); it("reads nvidia-smi's P2P matrix", () => { const m = (cells: string) => `\tGPU0\tGPU1\n GPU0\tX\t${cells}\n GPU1\t${cells}\tX\n\nLegend:\n X = Self\n OK = Status Ok`; assert.equal(parseP2p(m("OK")), "on"); assert.equal(parseP2p(m("CNS")), "off"); assert.equal(parseP2p("\tGPU0\tGPU1\tGPU2\n GPU0\tX\tOK\tNS\n GPU1\tOK\tX\tNS\n GPU2\tNS\tNS\tX"), "partial"); assert.equal(parseP2p("garbage"), null); }); it("counts cores per socket and sees the hypervisor flag", () => { const c = parseCpu(CPUINFO); assert.equal(c.cores, 2); assert.equal(c.threads, 4); assert.ok(c.hypervisor); assert.equal(parseCpu(null).model, null); }); it("finds the mount holding a folder", () => { const mi = "1 0 8:1 / / rw - ext4 /dev/sda1 rw\n2 1 8:16 / /models rw - ext4 /dev/sdb rw\n3 1 8:32 / /models-old rw - ext4 /dev/sdc rw"; assert.equal(mountDevice(mi, "/models"), "8:16"); assert.equal(mountDevice(mi, "/models/unsloth"), "8:16"); assert.equal(mountDevice(mi, "/models-old"), "8:32"); assert.equal(mountDevice(mi, "/data"), "8:1"); }); it("picks the digest of the image's own repository", () => { assert.equal(repoDigest("ghcr.io/ggml-org/llama.cpp:server", ["ghcr.io/ggml-org/llama.cpp@sha256:aaa", "mirror.local/llama.cpp@sha256:bbb"]), "sha256:aaa"); assert.equal(repoDigest("vllm/vllm-openai:v0.31.0", ["vllm/vllm-openai@sha256:ccc"]), "sha256:ccc"); assert.equal(repoDigest("local:dev", []), null); }); }); describe("engine info", () => { it("reads llama-server /props (build 11429)", () => { assert.deepEqual(parseLlamaProps({ build_info: "b11429-d81235049", total_slots: 4, default_generation_settings: { n_ctx: 4096 } }), { version: "b11429-d81235049", slots: 4, contextSize: 4096, }); assert.deepEqual(parseLlamaProps(null), { version: null, slots: null, contextSize: null }); }); it("reads vLLM /version and /v1/models", () => { assert.deepEqual(parseVllm({ version: "0.5.7" }, { data: [{ id: "x", max_model_len: 32768 }] }), { version: "0.5.7", slots: null, contextSize: 32768 }); }); it("falls back to startup log lines", () => { assert.equal(versionFromLog("llama.cpp", ["main: build: 11429 (d8123504) with gcc"]), "b11429-d8123504"); assert.equal(versionFromLog("vllm", ["INFO 10-20 api_server.py:1 vLLM API server version 0.31.0"]), "0.31.0"); assert.equal(versionFromLog("vllm", ["nothing here"]), null); }); it("returns nulls when the server can't be reached", async () => { const info = await fetchEngineInfo("llama.cpp", "http://127.0.0.1:9", null); assert.deepEqual(info, { version: null, slots: null, contextSize: null }); }); }); describe("PCIe link under load", () => { it("reads each card's current link from sysfs", async () => { assert.deepEqual(await currentLinks(memFs(), ["0000:03:00.0", "0000:05:00.0", "0000:09:00.0"]), { "0000:03:00.0": { gen: 4, width: 8 }, "0000:05:00.0": { gen: 4, width: 16 }, }); }); it("keeps the fastest link seen and reads nvidia-smi's link columns", () => { const l = (gen: number, width: number) => ({ gen, width }); assert.deepEqual( fastestLinks([ { pciAddress: "a", link: l(1, 16) }, { pciAddress: "a", link: l(4, 16) }, { pciAddress: "a", link: l(4, 8) }, { pciAddress: "b", link: l(3, 4) }, ]), { a: l(4, 16), b: l(3, 4) }, ); assert.deepEqual(parseGpuLine("0, NVIDIA GeForce RTX 3080, 10240, 812, 3, 21.5, 4, 16")?.link, l(4, 16)); assert.equal(parseGpuLine("0, NVIDIA GeForce RTX 3080, 10240, 812, 3, 21.5")?.link, null); const stats = summarizeSamples([], [], 10, [{ pciAddress: "a", link: l(1, 16) }, { pciAddress: "a", link: l(4, 16) }]); assert.deepEqual(stats.links, { a: l(4, 16) }); }); it("lists NVIDIA GPUs that aren't on the visible PCI bus (WSL2), with nvidia-smi's link", async () => { const empty: SysFs = { read: async () => null, list: async () => [], resolve: async (p) => p }; const hw = await detectHardware({ fs: empty, modelsDir: "/models", kernel: null, nvidia: [{ vendor: "nvidia", index: 0, uuid: "GPU-x", name: "NVIDIA GeForce RTX 3080", memoryMb: 10240, driver: "617.14", pciAddress: "00000000:01:00.0", linkMax: { gen: 4, width: 16 } }], }); assert.equal(hw.gpus.length, 1); assert.equal(hw.gpus[0].pciAddress, "0000:01:00.0"); assert.deepEqual(hw.gpus[0].linkMax, { gen: 4, width: 16 }); assert.equal(hw.gpus[0].rebar, null); }); });