okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
1bf946eaf
main
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' , () => { 12assert . equal ( luidName ( '90370' ), 'luid_0x00000000_0x00016102' ); 13assert . equal ( luidName ( String ( 0x12n * 2n ** 32n + 5n )), 'luid_0x00000012_0x00000005' ); 14assert . 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)' , () => { 18const dx = parseDxAdapters ( dxRaw ); 19const 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 ); 24assert . equal ( cards . length , 1 ); 25assert . equal ( cards [ 0 ]. vendor , 'amd' ); 26assert . equal ( cards [ 0 ]. vramGb , 15.9 ); 27assert . equal ( cards [ 0 ]. luid , 'luid_0x00000000_0x00016102' ); 28}); 29 30test ( 'without the registry list an AMD card is still listed, with no size' , () => { 31const cards = windowsOtherGpus ([{ Name :'AMD Radeon RX 7900 XT' , PNPDeviceID :'PCI-B' }], parseDxAdapters ( 'garbage' )); 32assert . 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' , () => { 36assert . equal ( parseGpuCounterLine ( 'nonsense' ), null ); 37const line = parseGpuCounterLine ( 'G luid_0x00000000_0x00016102 37 4294967296' ); 38assert . equal ( line . utilizationPct , 37 ); 39assert . equal ( line . memoryUsedGb , 4 ); 40const procs = []; 41const reader = createWindowsGpuReader ({ spawnFn :() => { const p = new EventEmitter (); p . stdout = new EventEmitter (); p . kill = () => {}; procs . push ( p ); return p ; }, now :() => 5 }); 42assert . equal ( reader . read ( 'luid_0x00000000_0x00016102' ), null , 'nothing before the first line' ); 43procs [ 0 ]. stdout . emit ( 'data' , 'G luid_0x00000000_0x00016102 37 4294967296\nG luid_0x00000000_0x00000001 0 0\n' ); 44assert . equal ( reader . read ( 'LUID_0x00000000_0x00016102' ). utilizationPct , 37 ); 45assert . equal ( reader . read ( 'luid_0x00000000_0x00099999' ), null ); 46assert . 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' , () => { 53assert . equal ( d3dkmtTemperatureC ( 300 ), 30 ); 54assert . equal ( d3dkmtTemperatureC ( 437 ), 44 ); 55assert . equal ( d3dkmtTemperatureC ( - 1 ), null ); 56assert . equal ( d3dkmtTemperatureC ( 0 ), null ); 57assert . equal ( d3dkmtTemperatureC ( 4294967295 ), null ); 58assert . equal ( d3dkmtTemperatureC ( undefined ), null ); 59assert . equal ( d3dkmtTemperatureC ( 'abc' ), null ); 60}); 61 62test ( 'a counter line carries the temperature; the older three-field line and a failed query still parse' , () => { 63assert . equal ( parseGpuCounterLine ( 'G luid_0x00000000_0x00016102 37 4294967296 430' ). temperatureC , 43 ); 64assert . equal ( parseGpuCounterLine ( 'G luid_0x00000000_0x00016102 37 4294967296 -1' ). temperatureC , null ); 65const old = parseGpuCounterLine ( 'G luid_0x00000000_0x00016102 37 4294967296' ); 66assert . equal ( old . temperatureC , null ); assert . equal ( old . utilizationPct , 37 ); 67}); 68 69test ( 'the temperature reaches the reader by LUID next to load and memory' , () => { 70const procs = []; 71const reader = createWindowsGpuReader ({ spawnFn :() => { const p = new EventEmitter (); p . stdout = new EventEmitter (); p . kill = () => {}; procs . push ( p ); return p ; }, now :() => 5 }); 72reader . read ( 'luid_0x00000000_0x00016102' ); 73procs [ 0 ]. stdout . emit ( 'data' , 'G luid_0x00000000_0x00016102 90 17000000000 465\n' ); 74const r = reader . read ( 'luid_0x00000000_0x00016102' ); 75assert . 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' , () => { 79assert . ok ( ! GPU_TEMP_CSHARP . includes ( "'" ), 'it sits inside a single-quoted PowerShell string' ); 80assert . match ( GPU_TEMP_CSHARP , / i\.T=62 / ); 81assert . match ( GPU_TEMP_CSHARP , / ReadInt32\(buf,56\) / ); 82assert . match ( GPU_TEMP_CSHARP , / AllocHGlobal\(64\) / ); 83let script = '' ; 84createWindowsGpuReader ({ spawnFn :( _cmd , args ) => { script = args . at ( - 1 ); const p = new EventEmitter (); p . stdout = new EventEmitter (); p . kill = () => {}; return p ; } }). read ( 'x' ); 85assert . ok ( script . includes ( 'Add-Type -TypeDefinition' ), 'compiled once, before the loop' ); 86assert . ok ( script . indexOf ( 'Add-Type' ) < script . indexOf ( 'while($true)' )); 87assert . match ( script , / try\{Add-Type [ ^ ] * \}catch\{\} / , 'a failed compile must not stop load and memory' ); 88});