feat(daemon): sample host cpu, memory and disk for /info and /resources

The status page had no host figures: `/api/v1/info` only carried identity and
`/api/v1/resources` only carried instance RSS, so the CPU/mem/disk tiles fell
back to "—" and the memory tile faked its percentage from RSS over physical
memory.

src/core/sysinfo.ts is a zero-dependency sampler: CPU from the /proc/stat delta
(null on the first read, since it needs two samples), load average from
os.loadavg(), memory from os.freemem/os.totalmem, and disk via statfs on the
daemon's state dir. One sample is cached for ~1 s and shared between /info and
/resources, so polling both does not double the syscalls. /info gains additive
`cpu`, `mem`, `disk` percentages plus `cpuCores` and `loadAvg`; /resources gains
a `system` object with the byte counts, the disk path and `sampledAt`.

Every field is optional on the client side: an older daemon simply omits them.
This commit is contained in:
yuez committed 2026-10-28 08:13:58 +08:00
1 parent 1664227c3b
commit 88354ec078
7 files changed
+492 -5

No files matched your search

+24
View File
@@ -4,6 +4,7 @@ import { join } from "node:path";
import { AuthStore } from "../../src/daemon/auth.js";
import { startServer, type StartedServer } from "../../src/daemon/server.js";
import type { Config, Limits } from "../../src/config.js";
import type { SysinfoSampler, SystemStats } from "../../src/core/sysinfo.js";
import type { WorkspaceInfo } from "../../src/protocol/types.js";
import { FakeBus, FakeInstanceManager, FakeWorkspaceStore } from "./fakes.js";
@@ -31,6 +32,26 @@ export function testConfig(overrides: Partial<Config> = {}): Config {
return { ...base, ...overrides, limits: { ...base.limits, ...(overrides.limits ?? {}) } };
}
/** Deterministic fake sampler returned when no override is provided. */
export const FAKE_SYSTEM_STATS: SystemStats = {
sampledAt: 1_700_000_000_000,
cpuPercent: 42,
cpuCores: 4,
loadAvg: [1.5, 1.2, 0.9],
memUsedBytes: 4 * 1024 ** 3,
memTotalBytes: 16 * 1024 ** 3,
memPercent: 25,
diskPath: "/tmp/pi2-test",
diskUsedBytes: 50 * 1024 ** 3,
diskTotalBytes: 200 * 1024 ** 3,
diskPercent: 25,
};
export function makeFakeSysinfoSampler(stats: Partial<SystemStats> = {}): SysinfoSampler {
const merged: SystemStats = { ...FAKE_SYSTEM_STATS, ...stats };
return { sample: async () => merged };
}
export interface HarnessOptions {
workspaces?: WorkspaceInfo[];
seedInstances?: string[];
@@ -45,6 +66,8 @@ export interface HarnessOptions {
snapshotWaitMs?: number;
measureBufferedAmount?: (socket: unknown) => number;
};
/** Override the sysinfo sampler; defaults to a deterministic fake (no real OS I/O). */
sysinfo?: SysinfoSampler;
}
export interface Harness {
@@ -107,6 +130,7 @@ export async function createHarness(options: HarnessOptions = {}): Promise<Harne
startedAt: Date.now(),
heartbeatMs: options.heartbeatMs,
ws: options.wsOverrides,
sysinfo: options.sysinfo ?? makeFakeSysinfoSampler(),
});
return {