okami.mom/strata

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

Niko1221Merge f-0215-energy: monitor: Energy since start counts the GPUs that report power (#6012)09cfbffed

main
1.7 KiB28 linesraw
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', () => {
9  const rtx = { id: 'GPU-6f6f', powerW: 40.5, powerLimitW: 450, source: 'sensor' };
10  const igpu = { id: 'PCI\\VEN_1002&DEV_164E', powerW: null, powerLimitW: null, source: 'sensor' };
11  assert.equal(reportedPowerW([rtx, igpu]), 40.5);
12  assert.equal(reportedPowerW([rtx, { ...rtx, id: 'GPU-2', powerW: 59.5 }]), 100);
13  assert.equal(reportedPowerW([igpu]), null);
14  assert.equal(reportedPowerW([]), null);
15  assert.equal(reportedPowerW(undefined), null);
16  assert.equal(reportedPowerW([{ id: 'x', powerW: Number.NaN }, { id: 'y', powerW: -1 }]), null);
17
18  const before = energyWh();
19  addEnergy(reportedPowerW([rtx, igpu]), 20_000);
20  addEnergy(reportedPowerW([{ ...rtx, powerW: 360 }, igpu]), 21_000);   // 360 W for 1 s = 0.1 Wh, the iGPU adds nothing
21  assert.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', () => {
25  const src = readFileSync(new URL('../server/monitor.mjs', import.meta.url), 'utf8');
26  assert.match(src, /addEnergy\(reportedPowerW\(hardwareSnapshot\(\)\.gpus\),/);
27  assert.doesNotMatch(src, /addEnergy\(hardwareSnapshot\(\)\.powerW/);
28});