Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix(self-hosted): track memory of process trees instead of process groups

Steven Enamakel0e4c3a4c8eda
diff --git a/scripts/__tests__/self-hosted-lanes.test.mjs b/scripts/__tests__/self-hosted-lanes.test.mjs
index 25ecf349ea..581e7baae4 100644
--- a/scripts/__tests__/self-hosted-lanes.test.mjs
+++ b/scripts/__tests__/self-hosted-lanes.test.mjs
@@ -18,6 +18,8 @@ import {
} from "../ci/self-hosted/lanes-plan.mjs";
import {
PrioritySemaphore,
+ processTable,
+ treeRssMiB,
sccacheSummary,
Runner,
defaultHeavySlots,
@@ -464,3 +466,30 @@ test("runner: heavy lanes wait for a slot, light lanes never do", async () => {
);
fs.rmSync(out, { recursive: true, force: true });
});
+
+test("peak RSS follows the process tree, including setsid'd descendants", () => {
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), "oh-proc-"));
+ const stat = (pid, ppid, pages) => {
+ fs.mkdirSync(path.join(dir, String(pid)));
+ // comm with a space and parens, as real process names can have.
+ const fields = ["S", ppid, pid, ...Array(18).fill(0), pages];
+ fs.writeFileSync(
+ path.join(dir, String(pid), "stat"),
+ `${pid} (cargo (x) y) ${fields.join(" ")}\n`,
+ );
+ };
+ stat(100, 1, 256); // the check's bash: 1 MiB
+ stat(101, 100, 512); // ci-cancel-aware.sh, own session after setsid: 2 MiB
+ stat(102, 101, 1024 * 256); // rustc: 1 GiB
+ stat(200, 1, 1024 * 256); // someone else's process
+ fs.mkdirSync(path.join(dir, "self")); // non-numeric entries are skipped
+ try {
+ const table = processTable(dir);
+ assert.equal(table.size, 4);
+ assert.equal(table.get(102).ppid, 101);
+ assert.equal(treeRssMiB(table, 100), 1 + 2 + 1024);
+ assert.equal(treeRssMiB(table, 999), 0);
+ } finally {
+ fs.rmSync(dir, { recursive: true, force: true });
+ }
+});
diff --git a/scripts/ci/self-hosted/lanes.mjs b/scripts/ci/self-hosted/lanes.mjs
index c200f9990e..682a991b87 100644
--- a/scripts/ci/self-hosted/lanes.mjs
+++ b/scripts/ci/self-hosted/lanes.mjs
@@ -265,26 +265,54 @@ function memInfoMiB() {
}
}
-/** Resident MiB per process group, from /proc (Linux only; {} elsewhere). */
-function rssByProcessGroup() {
- const out = new Map();
+/**
+ * The process table from /proc (Linux only; empty elsewhere): each pid's
+ * parent and resident MiB.
+ */
+export function processTable(procDir = "/proc") {
+ const table = new Map();
let pids;
try {
- pids = readdirSync("/proc").filter((d) => /^\d+$/.test(d));
+ pids = readdirSync(procDir).filter((d) => /^\d+$/.test(d));
} catch {
- return out;
+ return table;
}
for (const pid of pids) {
try {
- const stat = readFileSync(`/proc/${pid}/stat`, "utf8");
+ const stat = readFileSync(`${procDir}/${pid}/stat`, "utf8");
const f = stat.slice(stat.lastIndexOf(")") + 2).split(" ");
- // After the comm field: [0]=state [2]=pgrp ... [21]=rss (pages)
- const pgrp = Number(f[2]);
- const rssMiB = (Number(f[21]) * 4096) / 1048576;
- out.set(pgrp, (out.get(pgrp) ?? 0) + rssMiB);
+ // After the comm field: [0]=state [1]=ppid ... [21]=rss (pages)
+ table.set(Number(pid), {
+ ppid: Number(f[1]),
+ rssMiB: (Number(f[21]) * 4096) / 1048576,
+ });
} catch {}
}
- return out;
+ return table;
+}
+
+/**
+ * Resident MiB of `root` and all its descendants. Follows parent links, not
+ * process groups: scripts/ci-cancel-aware.sh starts cargo under `setsid`, so a
+ * check's compilers and test binaries leave the group the runner spawned.
+ */
+export function treeRssMiB(table, root) {
+ const children = new Map();
+ for (const [pid, { ppid }] of table) {
+ if (!children.has(ppid)) children.set(ppid, []);
+ children.get(ppid).push(pid);
+ }
+ let total = 0;
+ const stack = [root];
+ const seen = new Set();
+ while (stack.length) {
+ const pid = stack.pop();
+ if (seen.has(pid)) continue;
+ seen.add(pid);
+ total += table.get(pid)?.rssMiB ?? 0;
+ for (const c of children.get(pid) ?? []) stack.push(c);
+ }
+ return total;
}
export class Runner {
@@ -341,9 +369,9 @@ export class Runner {
)
this.memory.minAvailableMiB = available;
if (this.running.size === 0) return;
- const rss = rssByProcessGroup();
+ const table = processTable();
for (const entry of this.running.values()) {
- const now = Math.round(rss.get(entry.pgid) ?? 0);
+ const now = Math.round(treeRssMiB(table, entry.pgid));
if (now > entry.record.peakRssMiB) entry.record.peakRssMiB = now;
}
}