okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-028-monitor: the Monitor keeps the app's own GPU readings where the engine has none (AMD on Windows), reads Windows AMD temperature, and drops non-GPU display chips on Linux (#3727)1bf946eaf

main
5.0 KiB88 linesraw
1import test from 'node:test';
2import assert from 'node:assert/strict';
3import { EventEmitter } from 'node:events';
4import { luidName, parseDxAdapters, windowsOtherGpus, parseGpuCounterLine, createWindowsGpuReader } from '../server/windows-gpu.mjs';
5
6const dxRaw = JSON.stringify([
7  { Description: 'AMD Radeon RX 9070 XT', VendorId: '4098', Luid: '90370', Dedicated: '17094934528', Software: '0' },
8  { Description: 'Microsoft Basic Render Driver', VendorId: '5140', Luid: '1', Dedicated: '0', Software: '1' },
9]);
10
11test('the LUID is spelled as the performance counters spell it', () => {
12  assert.equal(luidName('90370'), 'luid_0x00000000_0x00016102');
13  assert.equal(luidName(String(0x12n * 2n ** 32n + 5n)), 'luid_0x00000012_0x00000005');
14  assert.equal(luidName('nope'), null);
15});
16
17test('an AMD card on Windows gets its real memory size and a LUID, virtual monitors are not cards (#3727, #3836)', () => {
18  const dx = parseDxAdapters(dxRaw);
19  const cards = windowsOtherGpus([
20    { Name: 'AMD Radeon RX 9070 XT', PNPDeviceID: 'PCI-A' },
21    { Name: 'Meta Virtual Monitor', PNPDeviceID: 'ROOT-1' },
22    { Name: 'Virtual Desktop Monitor', PNPDeviceID: 'ROOT-2' },
23  ], dx);
24  assert.equal(cards.length, 1);
25  assert.equal(cards[0].vendor, 'amd');
26  assert.equal(cards[0].vramGb, 15.9);
27  assert.equal(cards[0].luid, 'luid_0x00000000_0x00016102');
28});
29
30test('without the registry list an AMD card is still listed, with no size', () => {
31  const cards = windowsOtherGpus([{ Name: 'AMD Radeon RX 7900 XT', PNPDeviceID: 'PCI-B' }], parseDxAdapters('garbage'));
32  assert.deepEqual(cards.map(c => [c.vendor, c.vramGb, c.luid]), [['amd', 0, null]]);
33});
34
35test('counter lines are read, and the load and VRAM in use reach the card by its LUID', () => {
36  assert.equal(parseGpuCounterLine('nonsense'), null);
37  const line = parseGpuCounterLine('G luid_0x00000000_0x00016102 37 4294967296');
38  assert.equal(line.utilizationPct, 37);
39  assert.equal(line.memoryUsedGb, 4);
40  const procs = [];
41  const reader = createWindowsGpuReader({ spawnFn: () => { const p = new EventEmitter(); p.stdout = new EventEmitter(); p.kill = () => {}; procs.push(p); return p; }, now: () => 5 });
42  assert.equal(reader.read('luid_0x00000000_0x00016102'), null, 'nothing before the first line');
43  procs[0].stdout.emit('data', 'G luid_0x00000000_0x00016102 37 4294967296\nG luid_0x00000000_0x00000001 0 0\n');
44  assert.equal(reader.read('LUID_0x00000000_0x00016102').utilizationPct, 37);
45  assert.equal(reader.read('luid_0x00000000_0x00099999'), null);
46  assert.equal(procs.length, 1, 'one process for every read');
47});
48
49// ---- 0.2.8: the card's temperature from the display driver (D3DKMT), #3727 ----
50import { d3dkmtTemperatureC, GPU_TEMP_CSHARP } from '../server/windows-gpu.mjs';
51
52test('temperature comes in tenths of a degree; no answer, zero and nonsense are not a reading', () => {
53  assert.equal(d3dkmtTemperatureC(300), 30);
54  assert.equal(d3dkmtTemperatureC(437), 44);
55  assert.equal(d3dkmtTemperatureC(-1), null);
56  assert.equal(d3dkmtTemperatureC(0), null);
57  assert.equal(d3dkmtTemperatureC(4294967295), null);
58  assert.equal(d3dkmtTemperatureC(undefined), null);
59  assert.equal(d3dkmtTemperatureC('abc'), null);
60});
61
62test('a counter line carries the temperature; the older three-field line and a failed query still parse', () => {
63  assert.equal(parseGpuCounterLine('G luid_0x00000000_0x00016102 37 4294967296 430').temperatureC, 43);
64  assert.equal(parseGpuCounterLine('G luid_0x00000000_0x00016102 37 4294967296 -1').temperatureC, null);
65  const old = parseGpuCounterLine('G luid_0x00000000_0x00016102 37 4294967296');
66  assert.equal(old.temperatureC, null); assert.equal(old.utilizationPct, 37);
67});
68
69test('the temperature reaches the reader by LUID next to load and memory', () => {
70  const procs = [];
71  const reader = createWindowsGpuReader({ spawnFn: () => { const p = new EventEmitter(); p.stdout = new EventEmitter(); p.kill = () => {}; procs.push(p); return p; }, now: () => 5 });
72  reader.read('luid_0x00000000_0x00016102');
73  procs[0].stdout.emit('data', 'G luid_0x00000000_0x00016102 90 17000000000 465\n');
74  const r = reader.read('luid_0x00000000_0x00016102');
75  assert.equal(r.temperatureC, 47); assert.equal(r.utilizationPct, 90);
76});
77
78test('the PowerShell script that asks D3DKMT is single-quote safe and uses adapter performance data type 62 with the temperature at byte 56', () => {
79  assert.ok(!GPU_TEMP_CSHARP.includes("'"), 'it sits inside a single-quoted PowerShell string');
80  assert.match(GPU_TEMP_CSHARP, /i\.T=62/);
81  assert.match(GPU_TEMP_CSHARP, /ReadInt32\(buf,56\)/);
82  assert.match(GPU_TEMP_CSHARP, /AllocHGlobal\(64\)/);
83  let script = '';
84  createWindowsGpuReader({ spawnFn: (_cmd, args) => { script = args.at(-1); const p = new EventEmitter(); p.stdout = new EventEmitter(); p.kill = () => {}; return p; } }).read('x');
85  assert.ok(script.includes('Add-Type -TypeDefinition'), 'compiled once, before the loop');
86  assert.ok(script.indexOf('Add-Type') < script.indexOf('while($true)'));
87  assert.match(script, /try\{Add-Type[^]*\}catch\{\}/, 'a failed compile must not stop load and memory');
88});