fix(daemon): sample disk usage on the filesystems pi2d uses
The status tile read `—` on a fresh machine: the sampler ran `statfs` on the daemon's state dir, which is created lazily on the first instance log, so the call failed with ENOENT and `diskUsedBytes`/`diskTotalBytes`/`diskPercent` all came back null — `/info` then dropped `disk` altogether and the failure was indistinguishable from a full disk. `probeMount()` now walks up to the nearest existing ancestor and then to the filesystem boundary (`st_dev` change), so a sample describes a volume, not a directory. Candidates are pi2d's own write targets — `/` (the home volume on macOS, where `/` is the sealed read-only system volume), the pi session store and the visible workspace roots — never a mount-table dump: an unrelated or hung network mount must not stall the sampler. Rows are deduped by `st_dev`, sorted most-constrained first, capped at four, and reported as `system.disks[]` with the legacy scalars aliasing the worst row. A sample that yields nothing now carries `diskError` instead of silent nulls. The status tile shows the worst filesystem and, when several are in play, one quiet row per mount instead of a single percentage that hides the full volume.
This commit is contained in:
1 parent
1d907c08fa
commit
01fd7b9922
9 files changed
+474
-31
No files matched your search
+126
-4
@@ -4,13 +4,29 @@ import {
|
||||
cpuTimesFromCpus,
|
||||
createSysinfo,
|
||||
diskFromStatfs,
|
||||
fsTypeFromMagic,
|
||||
parseMeminfo,
|
||||
parseProcStat,
|
||||
probeMount,
|
||||
} from "../../src/core/sysinfo.js";
|
||||
|
||||
const mem = (available = "MemAvailable: 512 kB") => `MemTotal: 1024 kB\n${available}\nMemFree: 128 kB\nBuffers: 64 kB\nCached: 128 kB\n`;
|
||||
const cpus = (user: number, idle: number) => [{ times: { user, nice: 0, sys: 0, idle, irq: 0 } }];
|
||||
|
||||
/** `path -> st_dev` fake: anything absent throws ENOENT, like a real stat. */
|
||||
type FsTree = Record<string, number | bigint>;
|
||||
|
||||
function fakeStat(tree: FsTree) {
|
||||
return async (path: string) => {
|
||||
const dev = tree[path];
|
||||
if (dev === undefined) throw Object.assign(new Error(`ENOENT: ${path}`), { code: "ENOENT" });
|
||||
return { dev };
|
||||
};
|
||||
}
|
||||
|
||||
/** Every sampler call below injects `stat`+`statfs`, so no test touches the disk. */
|
||||
const oneFs: FsTree = { "/": 1, "/pi/sessions": 1 };
|
||||
|
||||
describe("sysinfo", () => {
|
||||
it("parses meminfo using MemAvailable and fallback fields", () => {
|
||||
expect(parseMeminfo(mem())).toEqual({ totalBytes: 1024 * 1024, availableBytes: 512 * 1024 });
|
||||
@@ -49,6 +65,9 @@ describe("sysinfo", () => {
|
||||
const result = await createSysinfo({
|
||||
now: () => 1,
|
||||
platform: "darwin",
|
||||
diskPath: "/",
|
||||
sessionsPath: "/pi/sessions",
|
||||
stat: fakeStat(oneFs),
|
||||
cpus: () => cpus(10, 10),
|
||||
totalmem: () => Number.NaN,
|
||||
freemem: () => Number.POSITIVE_INFINITY,
|
||||
@@ -69,8 +88,12 @@ describe("sysinfo", () => {
|
||||
now: () => clock,
|
||||
platform: "linux",
|
||||
diskPath: "/test",
|
||||
sessionsPath: "/pi/sessions",
|
||||
// `/test` does not exist: the probe must land on the root filesystem
|
||||
// instead of blanking the tile (the shipped bug).
|
||||
stat: fakeStat(oneFs),
|
||||
readFile: async (path) => { reads++; return path === "/proc/stat" ? "cpu 10 0 0 10 0 0 0" : mem(); },
|
||||
statfs: async () => { stats++; return { bsize: 100, blocks: 10, bfree: 4 }; },
|
||||
statfs: async () => { stats++; return { bsize: 100, blocks: 10, bfree: 4, type: 0xef53 }; },
|
||||
cpus: () => cpus(10, 10),
|
||||
totalmem: () => 999,
|
||||
freemem: () => 1,
|
||||
@@ -78,7 +101,20 @@ describe("sysinfo", () => {
|
||||
cacheMs: 100,
|
||||
});
|
||||
const first = await sampler.sample();
|
||||
expect(first).toEqual({ sampledAt: 1000, cpuPercent: null, cpuCores: 1, loadAvg: [1.23, 2.35, 3.46], memUsedBytes: 512 * 1024, memTotalBytes: 1024 * 1024, memPercent: 50, diskPath: "/test", diskUsedBytes: 600, diskTotalBytes: 1000, diskPercent: 60 });
|
||||
expect(first).toEqual({
|
||||
sampledAt: 1000,
|
||||
cpuPercent: null,
|
||||
cpuCores: 1,
|
||||
loadAvg: [1.23, 2.35, 3.46],
|
||||
memUsedBytes: 512 * 1024,
|
||||
memTotalBytes: 1024 * 1024,
|
||||
memPercent: 50,
|
||||
diskPath: "/",
|
||||
diskUsedBytes: 600,
|
||||
diskTotalBytes: 1000,
|
||||
diskPercent: 60,
|
||||
disks: [{ mountPoint: "/", fsType: "ext4", totalBytes: 1000, usedBytes: 600, percent: 60 }],
|
||||
});
|
||||
await sampler.sample();
|
||||
expect(reads).toBe(2);
|
||||
expect(stats).toBe(1);
|
||||
@@ -91,6 +127,9 @@ describe("sysinfo", () => {
|
||||
it("resolves when total memory probing throws", async () => {
|
||||
const sampler = createSysinfo({
|
||||
platform: "darwin",
|
||||
diskPath: "/",
|
||||
sessionsPath: "/pi/sessions",
|
||||
stat: fakeStat(oneFs),
|
||||
cpus: () => cpus(1, 1),
|
||||
totalmem: () => { throw new Error("totalmem unavailable"); },
|
||||
freemem: () => 25,
|
||||
@@ -100,12 +139,15 @@ describe("sysinfo", () => {
|
||||
await expect(sampler.sample()).resolves.toMatchObject({ memTotalBytes: 0, memUsedBytes: 0, memPercent: 0 });
|
||||
});
|
||||
|
||||
it("falls back and resolves I/O failures with null CPU and disk", async () => {
|
||||
it("reports a diskError instead of silent nulls when nothing can be sampled", async () => {
|
||||
const result = await createSysinfo({
|
||||
now: () => 1,
|
||||
platform: "linux",
|
||||
diskPath: "/test",
|
||||
sessionsPath: "/pi/sessions",
|
||||
readFile: async () => { throw new Error("read"); },
|
||||
statfs: async () => { throw new Error("statfs"); },
|
||||
stat: fakeStat(oneFs),
|
||||
statfs: async () => { throw Object.assign(new Error("statfs"), { code: "ENOENT" }); },
|
||||
cpus: () => { throw new Error("cpu"); },
|
||||
totalmem: () => 0,
|
||||
freemem: () => 0,
|
||||
@@ -116,7 +158,87 @@ describe("sysinfo", () => {
|
||||
expect(result.diskUsedBytes).toBeNull();
|
||||
expect(result.diskTotalBytes).toBeNull();
|
||||
expect(result.diskPercent).toBeNull();
|
||||
expect(result.disks).toEqual([]);
|
||||
expect(result.diskError).toBe("/: ENOENT");
|
||||
expect(result.diskPath).toBe("/test");
|
||||
expect(result.memTotalBytes).toBe(0);
|
||||
expect(result.memUsedBytes).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe("disk probes", () => {
|
||||
it("walks up to the nearest existing ancestor", async () => {
|
||||
const tree: FsTree = { "/": 10, "/home": 20, "/home/u": 20 };
|
||||
await expect(probeMount("/home/u/project", fakeStat(tree))).resolves.toEqual({
|
||||
path: "/home/u",
|
||||
mountPoint: "/home",
|
||||
dev: "20",
|
||||
});
|
||||
// Nothing exists → null, not a throw.
|
||||
await expect(probeMount("/nope", fakeStat({}))).resolves.toBeNull();
|
||||
});
|
||||
|
||||
it("stops at the filesystem boundary, not at the root", async () => {
|
||||
const tree: FsTree = { "/": 1, "/mnt": 1, "/mnt/data": 2, "/mnt/data/ws": 2 };
|
||||
await expect(probeMount("/mnt/data/ws/x", fakeStat(tree))).resolves.toMatchObject({ mountPoint: "/mnt/data" });
|
||||
await expect(probeMount("/mnt/data/ws/x", fakeStat(tree))).resolves.toMatchObject({ dev: "2" });
|
||||
await expect(probeMount("/usr/lib", fakeStat(tree))).resolves.toMatchObject({ mountPoint: "/" });
|
||||
});
|
||||
|
||||
it("labels known filesystem magics only", () => {
|
||||
expect(fsTypeFromMagic(0xef53)).toBe("ext4");
|
||||
expect(fsTypeFromMagic(0x9123683e)).toBe("btrfs");
|
||||
expect(fsTypeFromMagic(0xdeadbeef)).toBeUndefined();
|
||||
expect(fsTypeFromMagic(undefined)).toBeUndefined();
|
||||
expect(fsTypeFromMagic(Number.NaN)).toBeUndefined();
|
||||
});
|
||||
|
||||
it("dedupes by st_dev and reports the most constrained disk first", async () => {
|
||||
const tree: FsTree = { "/": 1, "/pi/sessions": 1, "/home": 2, "/mnt/data": 3 };
|
||||
const seen: string[] = [];
|
||||
const result = await createSysinfo({
|
||||
platform: "linux",
|
||||
diskPath: "/",
|
||||
sessionsPath: "/pi/sessions",
|
||||
// Three workspace roots, two of them on the same filesystem as /home.
|
||||
diskPaths: () => ["/home/a/one", "/home/a/two", "/mnt/data/ws"],
|
||||
stat: fakeStat(tree),
|
||||
statfs: async (path) => {
|
||||
seen.push(path);
|
||||
const freeBlocks = path === "/" ? 40 : path === "/home" ? 5 : 75;
|
||||
return { bsize: 100, blocks: 100, bfree: freeBlocks, type: 0xef53 };
|
||||
},
|
||||
cpus: () => cpus(1, 1),
|
||||
totalmem: () => 1,
|
||||
freemem: () => 0,
|
||||
loadavg: () => [0, 0, 0],
|
||||
}).sample();
|
||||
|
||||
// One statfs per distinct filesystem: /home/a/one and /home/a/two collapsed.
|
||||
expect(seen.sort()).toEqual(["/", "/home", "/mnt/data"]);
|
||||
expect(result.disks.map((disk) => disk.mountPoint)).toEqual(["/home", "/", "/mnt/data"]);
|
||||
expect(result.disks.map((disk) => disk.percent)).toEqual([95, 60, 25]);
|
||||
// Legacy scalars alias the worst row.
|
||||
expect(result.diskPath).toBe("/home");
|
||||
expect(result.diskPercent).toBe(95);
|
||||
expect(result.diskError).toBeUndefined();
|
||||
});
|
||||
|
||||
it("caps the filesystem list", async () => {
|
||||
const tree: FsTree = { "/": 1, "/pi/sessions": 1 };
|
||||
for (let i = 0; i < 6; i += 1) tree[`/vol${i}`] = 10 + i;
|
||||
const result = await createSysinfo({
|
||||
platform: "linux",
|
||||
diskPath: "/",
|
||||
sessionsPath: "/pi/sessions",
|
||||
diskPaths: () => [0, 1, 2, 3, 4, 5].map((i) => `/vol${i}/ws`),
|
||||
stat: fakeStat(tree),
|
||||
statfs: async () => ({ bsize: 100, blocks: 100, bfree: 5000 }),
|
||||
cpus: () => cpus(1, 1),
|
||||
totalmem: () => 1,
|
||||
freemem: () => 0,
|
||||
loadavg: () => [0, 0, 0],
|
||||
}).sample();
|
||||
expect(result.disks).toHaveLength(4);
|
||||
});
|
||||
});
|
||||
@@ -45,6 +45,9 @@ export const FAKE_SYSTEM_STATS: SystemStats = {
|
||||
diskUsedBytes: 50 * 1024 ** 3,
|
||||
diskTotalBytes: 200 * 1024 ** 3,
|
||||
diskPercent: 25,
|
||||
disks: [
|
||||
{ mountPoint: "/tmp/pi2-test", fsType: "ext4", usedBytes: 50 * 1024 ** 3, totalBytes: 200 * 1024 ** 3, percent: 25 },
|
||||
],
|
||||
};
|
||||
|
||||
export function makeFakeSysinfoSampler(stats: Partial<SystemStats> = {}): SysinfoSampler {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
||||
import * as os from "node:os";
|
||||
import { makeSessionState, makeWorkspaceInfo } from "./fakes.js";
|
||||
import { createHarness, TEST_LIMITS, type Harness } from "./harness.js";
|
||||
import { createHarness, makeFakeSysinfoSampler, TEST_LIMITS, type Harness } from "./harness.js";
|
||||
import { DaemonError } from "../../src/daemon/errors.js";
|
||||
import { ERROR_CODES } from "../../src/protocol/errors.js";
|
||||
import { WorkspaceError } from "../../src/workspaces/errors.js";
|
||||
@@ -486,6 +486,34 @@ describe("HTTP API", () => {
|
||||
}));
|
||||
});
|
||||
|
||||
it("relays the per-filesystem disk list on resources", async () => {
|
||||
const custom = await createHarness({
|
||||
sysinfo: makeFakeSysinfoSampler({
|
||||
disks: [
|
||||
{ mountPoint: "/home", fsType: "btrfs", usedBytes: 950, totalBytes: 1000, percent: 95 },
|
||||
{ mountPoint: "/", fsType: "ext4", usedBytes: 500, totalBytes: 1000, percent: 50 },
|
||||
],
|
||||
diskPath: "/home",
|
||||
diskPercent: 95,
|
||||
}),
|
||||
});
|
||||
try {
|
||||
const response = await fetch(`${custom.url}/api/v1/resources`, {
|
||||
headers: { Authorization: `Bearer ${custom.createToken()}` },
|
||||
});
|
||||
expect(response.status).toBe(200);
|
||||
const system = (await response.json()).system;
|
||||
expect(system.disks).toEqual([
|
||||
{ mountPoint: "/home", fsType: "btrfs", usedBytes: 950, totalBytes: 1000, percent: 95 },
|
||||
{ mountPoint: "/", fsType: "ext4", usedBytes: 500, totalBytes: 1000, percent: 50 },
|
||||
]);
|
||||
// The /info scalar stays the worst row, so old clients keep working.
|
||||
expect(system.diskPercent).toBe(95);
|
||||
} finally {
|
||||
await custom.cleanup();
|
||||
}
|
||||
});
|
||||
|
||||
it("resolves bare models without spawning when no instance is ready", async () => {
|
||||
const bare = await createHarness();
|
||||
try {
|
||||
|
||||
Reference in new issue
Block a user