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.0 KiB23 linesraw
1/**
2 * GPU energy since the host started, in Wh (#3628): the power reading held for the time since the last tick, added up.
3 * A long gap (the PC slept, the host was paused) counts for at most 5 s, so a sleep never adds a made-up hour of use.
4 */
5const meter = { wh: 0, at: 0 };
6
7export function addEnergy(powerW, at) {
8  const dt = meter.at ? Math.min((at - meter.at) / 1000, 5) : 0;
9  meter.at = at;
10  if (typeof powerW === 'number' && Number.isFinite(powerW) && powerW >= 0 && dt > 0) meter.wh += powerW * dt / 3600;
11  return meter.wh;
12}
13
14export const energyWh = () => meter.wh;
15
16/**
17 * The power the meter adds up (#6012): the sum over the GPUs that report one. A GPU with no power counter (an AMD iGPU
18 * on Windows, a unified-memory GPU) adds nothing instead of making the whole sum unknown; with no reading at all, null.
19 */
20export function reportedPowerW(readings) {
21  const values = (readings ?? []).map(g => g?.powerW).filter(v => typeof v === 'number' && Number.isFinite(v) && v >= 0);
22  return values.length ? values.reduce((n, v) => n + v, 0) : null;
23}