fix(header): the CPU reading shows CPU use, not the load average per core

The header's CPU figure was the 1-minute load average divided by the core
count, capped at 100. Load counts runnable tasks, not busy time, so a host
with bursty processes, or a VM whose vCPU threads count as runnable, read
100% while most cores were idle. It is now busy time across all cores
since the previous reading, from os.cpus(), with a one-second minimum
window on the monotonic clock.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ludo FourrageandClaude Opus 5.5 committed 2026-11-15 14:56:33 -08:00
1 parent e99b42c387
commit 9304f98efb
4 files changed
+141 -3

No files matched your search

+52
View File
@@ -0,0 +1,52 @@
/**
* @fileoverview The header CPU reading: CPU use over all cores since the previous reading, from each core's
* busy and idle time as `os.cpus()` reports it (the kernel's per-core counters on Linux, macOS and Windows).
*
* It replaces the 1-minute load average divided by the core count. Load counts runnable tasks, not busy time,
* so a host with bursty short-lived processes, or a VM whose vCPU threads count as runnable, read 100% while
* most of its cores were idle.
*/
import type { CpuInfo } from 'node:os';
/** Readings closer together than this reuse the last value, so several open browsers never shrink the window. */
export const CPU_USAGE_MIN_WINDOW_MS = 1000;
interface CpuTimes {
busy: number;
total: number;
at: number;
}
/**
* A meter that answers the CPU use, 0 to 100, between this reading and the previous one. Without usable core
* times (none reported, or counters that went backwards, as when a core goes offline) it answers the last
* reading, else `fallback()`. The first reading covers the time since the meter was created.
*/
export function createCpuUsageMeter(
readCores: () => Pick<CpuInfo, 'times'>[],
clock: () => number,
fallback: () => number
): () => number {
const sample = (): CpuTimes => {
let busy = 0;
let total = 0;
for (const { times } of readCores()) {
const all = times.user + times.nice + times.sys + times.idle + times.irq;
total += all;
busy += all - times.idle;
}
return { busy, total, at: clock() };
};
let base = sample();
let last: number | undefined;
return () => {
const now = sample();
if (last !== undefined && now.at - base.at < CPU_USAGE_MIN_WINDOW_MS) return last;
const busy = now.busy - base.busy;
const total = now.total - base.total;
base = now;
if (!(total > 0) || busy < 0) return last ?? fallback();
last = Math.min(100, Math.max(0, Math.round((busy / total) * 100)));
return last;
};
}
+7 -3
View File
@@ -14,6 +14,7 @@ import { execSync, spawn } from 'node:child_process';
import { randomBytes } from 'node:crypto';
import { dataPath } from '../../config/instance.js';
import { readHostSensors } from '../../host-sensors.js';
import { createCpuUsageMeter } from '../../cpu-usage.js';
import { ApiErrorCode, createErrorResponse, getErrorMessage, type NiceConfig } from '../../types.js';
import { isExplicitlyEnabled, isUnauthenticatedTunnelAcknowledged } from '../network-auth-policy.js';
import { isMultiUserMode } from '../../config/multiuser.js';
@@ -93,6 +94,11 @@ const SCREENSHOTS_DIR = dataPath('screenshots');
/** Cached CPU count — doesn't change at runtime */
const CPU_COUNT = cpus().length;
/** Header CPU use; the 1-minute load average per core only when the platform reports no core times. */
const cpuUsagePercent = createCpuUsageMeter(cpus, () => performance.now(), () =>
Math.min(100, Math.round((loadavg()[0] / CPU_COUNT) * 100))
);
function parseOptionalNumber(value: string | undefined): number | undefined {
if (value === undefined || value.trim() === '') return undefined;
const parsed = Number(value);
@@ -125,9 +131,7 @@ function getSystemStats(): {
usedMem = totalMem - freemem();
}
// CPU load average (1 min) as percentage (rough approximation)
const load = loadavg()[0];
const cpuPercent = Math.min(100, Math.round((load / CPU_COUNT) * 100));
const cpuPercent = cpuUsagePercent();
return {
cpu: cpuPercent,