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 { parseCpuTopology , parseNvidiaReadings , parsePcieThroughput , gpuMemoryTotals , pickGpuTemperature , isComputeAdapter } from '../server/device-probes.mjs' ; 4import { cpuUtilization , diskRate , parseLinuxDiskCounters , parseMacDiskCounters } from '../server/system-telemetry.mjs' ; 5import { parseEngineMetrics } from '../server/supervisor.mjs' ; 6import { originalDashboard , mergeOwnReading } from '../server/monitor-dashboard.mjs' ; 7import { gpuMetric , gpuCapacity , gpuReported , shortGpuName , defaultGpuSet , gpuId } from '../src/lib/monitor-hardware.ts' ; 8import { referencePaths } from '../src/lib/monitor-chart.ts' ; 9 10const gpu = ( id , index = 0 , unified = false ) => ({ id, index, unified, vramGb :24 , name :id , vendor :'nvidia' , backend :'cuda' }); 11const reading = ( id , utilizationPct , memoryUsedGb , temperatureC , powerW ) => ({ id, utilizationPct, memoryUsedGb, temperatureC, powerW}); 12 13test ( 'two GPUs use stable IDs, mean load, total memory/power, and hottest temperature' ,() => { 14const devices = [ gpu ( 'a' ), gpu ( 'b' , 1 )], readings = [ reading ( 'b' , 80 , 10 , 72 , 120 ), reading ( 'a' , 20 , 4 , 48 , 60 )]; 15assert . equal ( gpuMetric ( devices , readings , 'utilizationPct' , 'mean' ), 50 ); 16assert . equal ( gpuMetric ( devices , readings , 'memoryUsedGb' ), 14 ); 17assert . equal ( gpuMetric ( devices , readings , 'temperatureC' , 'max' ), 72 ); 18assert . equal ( gpuMetric ( devices , readings , 'powerW' ), 180 ); 19assert . equal ( gpuMetric ([ devices [ 1 ]], readings , 'utilizationPct' , 'mean' ), 80 ); 20assert . equal ( gpuCapacity ( devices ), 48 ); 21}); 22test ( 'missing sensors remain unavailable, including partial totals' ,() => { 23const devices = [ gpu ( 'a' ), gpu ( 'b' , 1 )]; 24assert . equal ( gpuMetric ( devices ,[ reading ( 'a' , 20 , 4 , 48 , 60 )], 'memoryUsedGb' ), null ); 25assert . equal ( gpuMetric ( devices ,[ reading ( 'a' , 20 , 4 , 48 , 60 ), reading ( 'b' , null , 10 , 72 , null )], 'utilizationPct' , 'mean' ), null ); 26assert . equal ( gpuMetric ([],[], 'powerW' ), null ); 27}); 28test ( 'unified memory capacity and readings do not count the same pool twice' ,() => { 29const devices = [ gpu ( 'a' , 0 , true ), gpu ( 'b' , 1 , true )]; 30assert . equal ( gpuCapacity ( devices ), 24 ); 31assert . equal ( gpuMetric ( devices ,[ reading ( 'a' , 20 , 4 , 48 , 60 ), reading ( 'b' , 80 , 4 , 72 , 120 )], 'memoryUsedGb' ), 4 ); 32assert . deepEqual ( gpuMemoryTotals ( devices ,[ reading ( 'a' , 20 , 4 , 48 , 60 ), reading ( 'b' , 80 , 4 , 72 , 120 )]),{ used :4 , total :24 }); 33assert . deepEqual ( gpuMemoryTotals ([ ...devices , gpu ( 'c' , 2 )],[ reading ( 'a' , 20 , 4 , 48 , 60 ), reading ( 'b' , 80 , 4 , 72 , 120 ), reading ( 'c' , 20 , 10 , 50 , 100 )]),{ used :14 , total :48 }); 34assert . deepEqual ( gpuMemoryTotals ( devices ,[ reading ( 'a' , 20 , 4 , 48 , 60 )]),{ used :null , total :24 }); 35}); 36test ( 'CPU topology keeps actual logical CPU IDs per socket' ,() => { 37const processors = parseCpuTopology ( '# CPU,Core,Socket\n0,0,0\n1,1,0\n2,0,1\n3,1,1' , 'Xeon' , 'x64' ); 38assert . deepEqual ( processors . map ( p => p . logicalIds ),[[ 0 , 1 ],[ 2 , 3 ]]); 39assert . deepEqual ( processors . map ( p => p . cores ),[ 2 , 2 ]); 40}); 41test ( 'per socket CPU utilization is a counter delta rather than a model or core count estimate' ,() => { 42const make = values => values . map (([ busy , idle ]) => ({ times :{ user :busy , nice :0 , sys :0 , idle, irq :0 }})); 43const before = make ([[ 0 , 0 ],[ 0 , 0 ],[ 0 , 0 ],[ 0 , 0 ]]), after = make ([[ 25 , 75 ],[ 25 , 75 ],[ 90 , 10 ],[ 90 , 10 ]]); 44assert . equal ( cpuUtilization ( before , after ,[ 0 , 1 ]), 25 ); 45assert . equal ( cpuUtilization ( before , after ,[ 2 , 3 ]), 90 ); 46assert . equal ( cpuUtilization ( null , after ,[ 0 ]), null ); 47assert . equal ( cpuUtilization ( before , after ,[]), null ); 48assert . equal ( cpuUtilization ( before , after ,[ 7 ]), null ); 49}); 50test ( 'disk counters exclude partitions to avoid double counting' ,() => { 51const row = ( name , read , write ) => `8 0 ${ name } 0 0 ${ read } 0 0 0 ${ write } 0 0 0 0` ; 52assert . deepEqual ( parseLinuxDiskCounters ([ row ( 'sda' , 10 , 20 ), row ( 'sda1' , 10 , 20 ), row ( 'sdb' , 5 , 10 )]. join ( '\n' ),[ 'sda' , 'sdb' ]),{ read :15 * 512 , write :30 * 512 }); 53assert . deepEqual ( parseMacDiskCounters ( '"Bytes (Read)"=100, "Bytes (Write)"=200\n"Bytes (Read)"=50, "Bytes (Write)"=20' ),{ read :150 , write :220 }); 54assert . deepEqual ( diskRate ({ time :1000 , counters :{ read :0 , write :0 }},{ read :2 * 1024 ** 2 , write :1024 ** 2 }, 3000 ),{ readMbs :1 , writeMbs :.5 }); 55assert . equal ( diskRate ( null ,{ read :4 , write :2 }, 3000 ). readMbs , null ); 56assert . equal ( diskRate ({ time :1000 , counters :{ read :10 , write :10 }},{ read :0 , write :0 }, 3000 ). readMbs , null ); 57}); 58test ( 'NVIDIA power and PCIe link readings survive unavailable driver fields' ,() => { 59const [ r ] = parseNvidiaReadings ( 'GPU-1, 60, 70, 4096, 123.5, 250, 3, 4, 16' ); 60assert . equal ( r . memoryUsedGb , 4 ); assert . equal ( r . powerW , 123.5 ); assert . equal ( r . pcieGenMax , 4 ); assert . equal ( r . pcieWidth , 16 ); 61const [ missing ] = parseNvidiaReadings ( 'GPU-1, N/A, 0, 0, [Not Supported], N/A, N/A, N/A, N/A' ); 62assert . equal ( missing . temperatureC , null ); assert . equal ( missing . powerW , null ); assert . equal ( missing . utilizationPct , 0 ); 63const transfer = parsePcieThroughput ( '# gpu rxpci txpci\n# Idx MB/s MB/s\n0 12.5 8\n1 200 0\n2 - -' ); 64assert . equal ( transfer . get ( 0 ), 12.5 ); assert . equal ( transfer . get ( 1 ), 200 ); assert . equal ( transfer . has ( 2 ), false ); 65}); 66test ( 'chart paths reuse the exact original 100×32 geometry' ,() => { 67const { paths} = referencePaths ([ 0 , 5 , 10 ]); 68assert . equal ( paths [ 0 ]. line , 'M0.00,30.00L50.00,17.00L100.00,4.00' ); 69assert . equal ( paths [ 0 ]. area , 'M0.00,30.00L50.00,17.00L100.00,4.00L100.00,32L0.00,32Z' ); 70}); 71test ( 'charts leave gaps for missing sensors and do not draw a made up zero sample' ,() => { 72assert . equal ( referencePaths ([ null , null ]). paths . length , 0 ); 73assert . equal ( referencePaths ([ 1 ]). paths . length , 0 ); 74const { paths} = referencePaths ([ 1 , 2 , null , 3 , 4 ]); 75assert . equal ( paths . length , 2 ); 76assert . ok ( paths [ 0 ]. line . endsWith ( '25.00,17.00' )); 77assert . ok ( paths [ 1 ]. line . startsWith ( 'M75.00' )); 78}); 79test ( 'both supported engine metric formats keep their actual telemetry' ,() => { 80const prom = parseEngineMetrics ( 'llamacpp:prompt_tokens_total 4\nllamacpp:predicted_tokens_seconds 42' ); 81assert . equal ( prom . outputTps , 42 ); 82const raw = { engine :{ max_context :32768 }, live :{ state :'generating' , tok_s :44 }, hardware :{}, requests :[]}; 83assert . equal ( parseEngineMetrics ( JSON . stringify ( raw )). dashboard . live . tok_s , 44 ); 84assert . throws (() => parseEngineMetrics ( '<html>error</html>' )); 85}); 86test ( 'original requests, cache, progress, hardware and MCP facts are translated without losing information' ,() => { 87const raw = { engine :{ max_context :32768 , expert_slots :48 , expert_cache_mib :8192 }, live :{ state :'generating' , generated :120 , max_tokens :600 , prompt_tokens :200 , tok_s :44 }, hardware :{ gpu_mem_used :4 * 1024 ** 3 , gpu_mem_total :24 * 1024 ** 3 }, history :{ tok_s :[ 42 , 44 ]}, requests :[{ time :1 , finish :'stop' , prompt_tokens :200 , reused :100 , output_tokens :120 , decode_tok_s :44 , hit_rate :.85 , pcie_share :.1 , duration_s :3 }], totals :{ requests :20 , prompt_tokens :4000 , output_tokens :2400 , reused :2000 , since :1 }, conversation_cache :{ enabled :true , budget_mib :8192 , parked :2 , slots :4 , bytes :2 * 1024 ** 3 , parks :3 , restores :1 , requests :20 , requests_reused :10 }, mcp :{ tools :1 , servers :[{ name :'files' , status :'ready' , transport :'stdio' , tools :[{ tool :'read_file' , description :'Read a file' }]}]}}; 88const mapped = originalDashboard ( raw ,{ gpus :[ gpu ( 'a' )]},{ context :{ maxTokens :32768 }, hardware :{}, history :[], totals :{}, telemetry :{}}); 89assert . equal ( mapped . requests [ 0 ]. cachedTokens , 100 ); assert . equal ( mapped . requests [ 0 ]. hitRate , .85 ); 90assert . equal ( mapped . conversationCache . memoryGb , 2 ); assert . equal ( mapped . totals . cachedTokens , 2000 ); 91assert . equal ( mapped . expertCache . memoryGb , 8 ); assert . equal ( mapped . hardware . vramUsedGb , 4 ); 92assert . equal ( mapped . generation . total , 600 ); assert . equal ( mapped . history [ 0 ]. gpuTempC , null ); 93assert . equal ( mapped . mcp . servers [ 0 ]. state , 'ready' ); assert . equal ( mapped . mcp . servers [ 0 ]. tools [ 0 ]. name , 'read_file' ); 94}); 95 96test ( 'All GPUs: a card with no temperature sensor is left out and the card that reports is named (#3727 follow-up)' ,() => { 97const devices = [{ ...gpu ( 'nv' , 0 ), name :'NVIDIA GeForce RTX 5070' },{ ...gpu ( 'igpu' , 1 ), name :'AMD Radeon(TM) Graphics' , vramGb :0.4 }]; 98const readings = [ reading ( 'nv' , 3 , 0.8 , 34 , 7 ), reading ( 'igpu' , 0 , 0.001 , null , null )]; 99const temp = gpuReported ( devices , readings , 'temperatureC' , 'max' ); 100assert . equal ( temp . value , 34 ); assert . equal ( temp . partial , true ); assert . deepEqual ( temp . devices . map ( d => shortGpuName ( d . name )),[ 'RTX 5070' ]); 101const load = gpuReported ( devices , readings , 'utilizationPct' , 'mean' ); 102assert . equal ( load . partial , false ); assert . equal ( load . value , 1.5 ); 103assert . equal ( gpuReported ( devices ,[ reading ( 'nv' , 3 , 0.8 , null , null ), reading ( 'igpu' , 0 , 0 , null , null )], 'temperatureC' , 'max' ). value , null , 'no card reports: still unavailable' ); 104assert . equal ( shortGpuName ( 'AMD Radeon RX 9070 XT' ), 'RX 9070 XT' ); 105assert . equal ( shortGpuName ( 'Intel(R) Arc(TM) A750 Graphics' ), 'A750 Graphics' ); 106}); 107 108test ( 'an RTX 5070 beside the unsupported Radeon iGPU: All GPUs is the RTX alone, the iGPU stays pickable' ,() => { 109const rtx = { id :'GPU-5070' , index :0 , vendor :'nvidia' , name :'NVIDIA GeForce RTX 5070' , vramGb :11.9 , unified :false , backend :'cuda' }; 110const igpu = { id :'PCI\VEN_1002&DEV_164E' , index :1 , vendor :'amd' , name :'AMD Radeon(TM) Graphics' , vramGb :0.5 , unified :false , backend :'vulkan' }; 111const set = defaultGpuSet ([ rtx , igpu ]); 112assert . deepEqual ( set . map ( g => g . id ),[ 'GPU-5070' ]); 113assert . equal ( gpuCapacity ( set ), 11.9 ); 114// the native engine report numbers each make from 0, so both cards say index 0: the ids stay different 115assert . notEqual ( gpuId ({ ...rtx , id :undefined }), gpuId ({ ...igpu , id :undefined , index :0 })); 116// alone, or a real AMD card beside it, nothing is dropped 117assert . deepEqual ( defaultGpuSet ([ igpu ]). map ( g => g . id ),[ igpu . id ]); 118assert . equal ( defaultGpuSet ([{ ...igpu , vramGb :96 , unified :true }, rtx ]). length , 2 ); 119}); 120 121// ---- 0.2.8: Monitor readings for AMD cards (#3727) ---- 122const amd = ( id , index ) => ({ id, index, unified :false , vramGb :16 , name :'AMD Radeon RX 9070 XT' , vendor :'amd' , backend :'vulkan' }); 123const ownReading = ( id , extra = {}) => ({ id, source :'sensor' , sampledAt :'t0' , temperatureC :null , utilizationPct :37 , memoryUsedGb :5.5 , powerW :null , powerLimitW :null , pcieGen :null , pcieGenMax :null , pcieWidth :null , pcieRxMbs :null , ...extra }); 124const shell = ( own ) => ({ context :{ maxTokens :32768 }, hardware :{ gpus :own , gpuLoad :null }, history :[], totals :{}, telemetry :{}}); 125const rawNoGpu = { engine :{ max_context :32768 }, live :{ state :'generating' }, hardware :{ cpu :12 }, history :{}}; 126 127test ( 'Windows AMD, one card, engine running: the engine has no GPU reader, the host load and memory stay (#3727)' ,() => { 128const mapped = originalDashboard ( rawNoGpu ,{ gpus :[ amd ( 'dx:1' , 0 )]}, shell ([ ownReading ( 'dx:1' )])); 129assert . equal ( mapped . hardware . gpus . length , 1 ); 130assert . equal ( mapped . hardware . gpus [ 0 ]. utilizationPct , 37 ); 131assert . equal ( mapped . hardware . gpus [ 0 ]. memoryUsedGb , 5.5 ); 132assert . equal ( mapped . hardware . gpus [ 0 ]. source , 'sensor' ); 133assert . equal ( mapped . hardware . gpus [ 0 ]. temperatureC , null , 'a value nobody read stays unavailable' ); 134}); 135test ( 'engine numbers win, the host fills only the gaps (fan and clock come from the host)' ,() => { 136const raw = { ...rawNoGpu , hardware :{ gpu_util :80 , gpu_mem_used :4 * 1024 ** 3 , gpu_temp :61 , gpu_power :200 }}; 137const mapped = originalDashboard ( raw ,{ gpus :[ amd ( 'a' , 0 )]}, shell ([ ownReading ( 'a' ,{ utilizationPct :10 , temperatureC :50 , fanPct :35 , clockMhz :2400 , powerLimitW :300 })])); 138const g = mapped . hardware . gpus [ 0 ]; 139assert . equal ( g . utilizationPct , 80 ); assert . equal ( g . temperatureC , 61 ); assert . equal ( g . powerW , 200 ); 140assert . equal ( g . powerLimitW , 300 ); assert . equal ( g . fanPct , 35 ); assert . equal ( g . clockMhz , 2400 ); assert . equal ( g . source , 'engine' ); 141}); 142test ( 'a card the host has no reading for keeps the engine reading unchanged' ,() => { 143const e = { id :'x' , source :'engine' , temperatureC :null , utilizationPct :5 }; 144assert . equal ( mergeOwnReading ( e , undefined ), e ); 145}); 146test ( 'the headline aggregates fall back to the host when the engine reports none' ,() => { 147const fb = shell ([ ownReading ( 'a' )]); fb . hardware . gpuTempC = 55 ; fb . hardware . vramUsedGb = 5.5 ; 148const mapped = originalDashboard ( rawNoGpu ,{ gpus :[ amd ( 'a' , 0 )]}, fb ); 149assert . equal ( mapped . hardware . gpuTempC , 55 ); assert . equal ( mapped . hardware . vramUsedGb , 5.5 ); 150}); 151test ( 'amdgpu temperature: edge first (what nvtop shows), junction when there is no edge, 0 is no reading' ,() => { 152assert . equal ( pickGpuTemperature ([{ label :'edge' , milli :36000 },{ label :'junction' , milli :38000 },{ label :'mem' , milli :56000 }]), 36 ); 153assert . equal ( pickGpuTemperature ([{ label :'mem' , milli :56000 },{ label :'junction' , milli :48000 }]), 48 ); 154assert . equal ( pickGpuTemperature ([{ label :'' , milli :41000 }]), 41 ); 155assert . equal ( pickGpuTemperature ([{ label :'edge' , milli :0 },{ label :'junction' , milli :44000 }]), 44 ); 156assert . equal ( pickGpuTemperature ([{ label :'edge' , milli :0 }]), null ); 157assert . equal ( pickGpuTemperature ([{ label :'mem' , milli :56000 }]), null , 'a memory sensor alone is not the GPU temperature' ); 158assert . equal ( pickGpuTemperature ([]), null ); 159}); 160test ( 'a remote-console chip with no memory is not a GPU; an unnamed card with memory and an Intel iGPU are' ,() => { 161assert . equal ( isComputeAdapter ( 'unknown' , null ), false ); 162assert . equal ( isComputeAdapter ( 'unknown' , 0 ), false ); 163assert . equal ( isComputeAdapter ( 'unknown' , 8 * 1024 ** 3 ), true ); 164assert . equal ( isComputeAdapter ( 'intel' , null ), true ); 165assert . equal ( isComputeAdapter ( 'amd' , 34208743424 ), true ); 166});