okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
09cfbffed
main
1// #6012: "Energy since start" stayed at 0 on a PC whose second GPU (an AMD iGPU on Windows) has no power counter, because the 2// meter was fed the machine total, and that total is unknown when any one GPU gives no power reading. 3import test from 'node:test' ; 4import assert from 'node:assert/strict' ; 5import { readFileSync } from 'node:fs' ; 6import { addEnergy , energyWh , reportedPowerW } from '../server/energy-meter.mjs' ; 7 8test ( 'the energy meter counts the GPUs that report power and skips one that has no power counter' , () => { 9const rtx = { id :'GPU-6f6f' , powerW :40.5 , powerLimitW :450 , source :'sensor' }; 10const igpu = { id :'PCI\\VEN_1002&DEV_164E' , powerW :null , powerLimitW :null , source :'sensor' }; 11assert . equal ( reportedPowerW ([ rtx , igpu ]), 40.5 ); 12assert . equal ( reportedPowerW ([ rtx , { ...rtx , id :'GPU-2' , powerW :59.5 }]), 100 ); 13assert . equal ( reportedPowerW ([ igpu ]), null ); 14assert . equal ( reportedPowerW ([]), null ); 15assert . equal ( reportedPowerW ( undefined ), null ); 16assert . equal ( reportedPowerW ([{ id :'x' , powerW :Number . NaN }, { id :'y' , powerW :- 1 }]), null ); 17 18const before = energyWh (); 19addEnergy ( reportedPowerW ([ rtx , igpu ]), 20_000 ); 20addEnergy ( reportedPowerW ([{ ...rtx , powerW :360 }, igpu ]), 21_000 ); // 360 W for 1 s = 0.1 Wh, the iGPU adds nothing 21assert . ok ( Math . abs ( energyWh () - before - 0.1 ) < 1e-9 ); 22}); 23 24test ( 'the monitor feeds the energy meter from the per-GPU readings, not the all-or-nothing machine total' , () => { 25const src = readFileSync ( new URL ( '../server/monitor.mjs' , import . meta. url ), 'utf8' ); 26assert . match ( src , / addEnergy\(reportedPowerW\(hardwareSnapshot\(\)\.gpus\), / ); 27assert . doesNotMatch ( src , / addEnergy\(hardwareSnapshot\(\)\.powerW / ); 28});