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
12.5 KiB166 linesraw
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',()=>{
14  const devices=[gpu('a'),gpu('b',1)],readings=[reading('b',80,10,72,120),reading('a',20,4,48,60)];
15  assert.equal(gpuMetric(devices,readings,'utilizationPct','mean'),50);
16  assert.equal(gpuMetric(devices,readings,'memoryUsedGb'),14);
17  assert.equal(gpuMetric(devices,readings,'temperatureC','max'),72);
18  assert.equal(gpuMetric(devices,readings,'powerW'),180);
19  assert.equal(gpuMetric([devices[1]],readings,'utilizationPct','mean'),80);
20  assert.equal(gpuCapacity(devices),48);
21});
22test('missing sensors remain unavailable, including partial totals',()=>{
23  const devices=[gpu('a'),gpu('b',1)];
24  assert.equal(gpuMetric(devices,[reading('a',20,4,48,60)],'memoryUsedGb'),null);
25  assert.equal(gpuMetric(devices,[reading('a',20,4,48,60),reading('b',null,10,72,null)],'utilizationPct','mean'),null);
26  assert.equal(gpuMetric([],[],'powerW'),null);
27});
28test('unified memory capacity and readings do not count the same pool twice',()=>{
29  const devices=[gpu('a',0,true),gpu('b',1,true)];
30  assert.equal(gpuCapacity(devices),24);
31  assert.equal(gpuMetric(devices,[reading('a',20,4,48,60),reading('b',80,4,72,120)],'memoryUsedGb'),4);
32  assert.deepEqual(gpuMemoryTotals(devices,[reading('a',20,4,48,60),reading('b',80,4,72,120)]),{used:4,total:24});
33  assert.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});
34  assert.deepEqual(gpuMemoryTotals(devices,[reading('a',20,4,48,60)]),{used:null,total:24});
35});
36test('CPU topology keeps actual logical CPU IDs per socket',()=>{
37  const processors=parseCpuTopology('# CPU,Core,Socket\n0,0,0\n1,1,0\n2,0,1\n3,1,1','Xeon','x64');
38  assert.deepEqual(processors.map(p=>p.logicalIds),[[0,1],[2,3]]);
39  assert.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',()=>{
42  const make=values=>values.map(([busy,idle])=>({times:{user:busy,nice:0,sys:0,idle,irq:0}}));
43  const before=make([[0,0],[0,0],[0,0],[0,0]]),after=make([[25,75],[25,75],[90,10],[90,10]]);
44  assert.equal(cpuUtilization(before,after,[0,1]),25);
45  assert.equal(cpuUtilization(before,after,[2,3]),90);
46  assert.equal(cpuUtilization(null,after,[0]),null);
47  assert.equal(cpuUtilization(before,after,[]),null);
48  assert.equal(cpuUtilization(before,after,[7]),null);
49});
50test('disk counters exclude partitions to avoid double counting',()=>{
51  const row=(name,read,write)=>`8 0 ${name} 0 0 ${read} 0 0 0 ${write} 0 0 0 0`;
52  assert.deepEqual(parseLinuxDiskCounters([row('sda',10,20),row('sda1',10,20),row('sdb',5,10)].join('\n'),['sda','sdb']),{read:15*512,write:30*512});
53  assert.deepEqual(parseMacDiskCounters('"Bytes (Read)"=100, "Bytes (Write)"=200\n"Bytes (Read)"=50, "Bytes (Write)"=20'),{read:150,write:220});
54  assert.deepEqual(diskRate({time:1000,counters:{read:0,write:0}},{read:2*1024**2,write:1024**2},3000),{readMbs:1,writeMbs:.5});
55  assert.equal(diskRate(null,{read:4,write:2},3000).readMbs,null);
56  assert.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',()=>{
59  const [r]=parseNvidiaReadings('GPU-1, 60, 70, 4096, 123.5, 250, 3, 4, 16');
60  assert.equal(r.memoryUsedGb,4);assert.equal(r.powerW,123.5);assert.equal(r.pcieGenMax,4);assert.equal(r.pcieWidth,16);
61  const [missing]=parseNvidiaReadings('GPU-1, N/A, 0, 0, [Not Supported], N/A, N/A, N/A, N/A');
62  assert.equal(missing.temperatureC,null);assert.equal(missing.powerW,null);assert.equal(missing.utilizationPct,0);
63  const transfer=parsePcieThroughput('# gpu rxpci txpci\n# Idx MB/s MB/s\n0 12.5 8\n1 200 0\n2 - -');
64  assert.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',()=>{
67  const {paths}=referencePaths([0,5,10]);
68  assert.equal(paths[0].line,'M0.00,30.00L50.00,17.00L100.00,4.00');
69  assert.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',()=>{
72  assert.equal(referencePaths([null,null]).paths.length,0);
73  assert.equal(referencePaths([1]).paths.length,0);
74  const {paths}=referencePaths([1,2,null,3,4]);
75  assert.equal(paths.length,2);
76  assert.ok(paths[0].line.endsWith('25.00,17.00'));
77  assert.ok(paths[1].line.startsWith('M75.00'));
78});
79test('both supported engine metric formats keep their actual telemetry',()=>{
80  const prom=parseEngineMetrics('llamacpp:prompt_tokens_total 4\nllamacpp:predicted_tokens_seconds 42');
81  assert.equal(prom.outputTps,42);
82  const raw={engine:{max_context:32768},live:{state:'generating',tok_s:44},hardware:{},requests:[]};
83  assert.equal(parseEngineMetrics(JSON.stringify(raw)).dashboard.live.tok_s,44);
84  assert.throws(()=>parseEngineMetrics('<html>error</html>'));
85});
86test('original requests, cache, progress, hardware and MCP facts are translated without losing information',()=>{
87  const 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'}]}]}};
88  const mapped=originalDashboard(raw,{gpus:[gpu('a')]},{context:{maxTokens:32768},hardware:{},history:[],totals:{},telemetry:{}});
89  assert.equal(mapped.requests[0].cachedTokens,100);assert.equal(mapped.requests[0].hitRate,.85);
90  assert.equal(mapped.conversationCache.memoryGb,2);assert.equal(mapped.totals.cachedTokens,2000);
91  assert.equal(mapped.expertCache.memoryGb,8);assert.equal(mapped.hardware.vramUsedGb,4);
92  assert.equal(mapped.generation.total,600);assert.equal(mapped.history[0].gpuTempC,null);
93  assert.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)',()=>{
97  const devices=[{...gpu('nv',0),name:'NVIDIA GeForce RTX 5070'},{...gpu('igpu',1),name:'AMD Radeon(TM) Graphics',vramGb:0.4}];
98  const readings=[reading('nv',3,0.8,34,7),reading('igpu',0,0.001,null,null)];
99  const temp=gpuReported(devices,readings,'temperatureC','max');
100  assert.equal(temp.value,34); assert.equal(temp.partial,true); assert.deepEqual(temp.devices.map(d=>shortGpuName(d.name)),['RTX 5070']);
101  const load=gpuReported(devices,readings,'utilizationPct','mean');
102  assert.equal(load.partial,false); assert.equal(load.value,1.5);
103  assert.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');
104  assert.equal(shortGpuName('AMD Radeon RX 9070 XT'),'RX 9070 XT');
105  assert.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',()=>{
109  const rtx={id:'GPU-5070',index:0,vendor:'nvidia',name:'NVIDIA GeForce RTX 5070',vramGb:11.9,unified:false,backend:'cuda'};
110  const igpu={id:'PCI\VEN_1002&DEV_164E',index:1,vendor:'amd',name:'AMD Radeon(TM) Graphics',vramGb:0.5,unified:false,backend:'vulkan'};
111  const set=defaultGpuSet([rtx,igpu]);
112  assert.deepEqual(set.map(g=>g.id),['GPU-5070']);
113  assert.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
115  assert.notEqual(gpuId({...rtx,id:undefined}),gpuId({...igpu,id:undefined,index:0}));
116  // alone, or a real AMD card beside it, nothing is dropped
117  assert.deepEqual(defaultGpuSet([igpu]).map(g=>g.id),[igpu.id]);
118  assert.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)',()=>{
128  const mapped=originalDashboard(rawNoGpu,{gpus:[amd('dx:1',0)]},shell([ownReading('dx:1')]));
129  assert.equal(mapped.hardware.gpus.length,1);
130  assert.equal(mapped.hardware.gpus[0].utilizationPct,37);
131  assert.equal(mapped.hardware.gpus[0].memoryUsedGb,5.5);
132  assert.equal(mapped.hardware.gpus[0].source,'sensor');
133  assert.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)',()=>{
136  const raw={...rawNoGpu,hardware:{gpu_util:80,gpu_mem_used:4*1024**3,gpu_temp:61,gpu_power:200}};
137  const mapped=originalDashboard(raw,{gpus:[amd('a',0)]},shell([ownReading('a',{utilizationPct:10,temperatureC:50,fanPct:35,clockMhz:2400,powerLimitW:300})]));
138  const g=mapped.hardware.gpus[0];
139  assert.equal(g.utilizationPct,80); assert.equal(g.temperatureC,61); assert.equal(g.powerW,200);
140  assert.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',()=>{
143  const e={id:'x',source:'engine',temperatureC:null,utilizationPct:5};
144  assert.equal(mergeOwnReading(e,undefined),e);
145});
146test('the headline aggregates fall back to the host when the engine reports none',()=>{
147  const fb=shell([ownReading('a')]); fb.hardware.gpuTempC=55; fb.hardware.vramUsedGb=5.5;
148  const mapped=originalDashboard(rawNoGpu,{gpus:[amd('a',0)]},fb);
149  assert.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',()=>{
152  assert.equal(pickGpuTemperature([{label:'edge',milli:36000},{label:'junction',milli:38000},{label:'mem',milli:56000}]),36);
153  assert.equal(pickGpuTemperature([{label:'mem',milli:56000},{label:'junction',milli:48000}]),48);
154  assert.equal(pickGpuTemperature([{label:'',milli:41000}]),41);
155  assert.equal(pickGpuTemperature([{label:'edge',milli:0},{label:'junction',milli:44000}]),44);
156  assert.equal(pickGpuTemperature([{label:'edge',milli:0}]),null);
157  assert.equal(pickGpuTemperature([{label:'mem',milli:56000}]),null,'a memory sensor alone is not the GPU temperature');
158  assert.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',()=>{
161  assert.equal(isComputeAdapter('unknown',null),false);
162  assert.equal(isComputeAdapter('unknown',0),false);
163  assert.equal(isComputeAdapter('unknown',8*1024**3),true);
164  assert.equal(isComputeAdapter('intel',null),true);
165  assert.equal(isComputeAdapter('amd',34208743424),true);
166});