Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(memory): the memory gate counts each engine's share of memory, not shared pages once per process (#3792)

Milind Sonid8a0cd6553ac
diff --git a/server/engine-memory.test.ts b/server/engine-memory.test.ts
index 246af44b61..30bd22fafd 100644
--- a/server/engine-memory.test.ts
+++ b/server/engine-memory.test.ts
@@ -37,6 +37,19 @@ describe("reading the host", () => {
expect(groups.get(200)).toBe(50_000 * 4096);
});
+ it("counts each process's own share of what it shares, as the kernel splits it, and its resident pages only without one", () => {
+ const stat = (pid: number, pgrp: number, rssPages: number) => `${pid} (node) S 1 ${pgrp} ${pgrp} 0 -1 4194560 ${Array(14).fill(0).join(" ")} ${rssPages} 18446744073709551615`;
+ // Two MCP servers on one runtime: 100 MB resident each, of which 60 MB is the runtime's code they share.
+ const files: Record<string, string> = {
+ "/proc/100/stat": stat(100, 100, 75_000), "/proc/100/smaps_rollup": "00400000-7fff [rollup]\nRss: 300000 kB\nPss: 240000 kB\n",
+ "/proc/101/stat": stat(101, 100, 25_600), "/proc/101/smaps_rollup": "Rss: 102400 kB\nPss: 71680 kB\n",
+ "/proc/102/stat": stat(102, 100, 25_600), "/proc/102/smaps_rollup": "Rss: 102400 kB\nPss: 71680 kB\n",
+ "/proc/103/stat": stat(103, 100, 1_000),
+ };
+ const groups = processGroupBytes(() => ["100", "101", "102", "103"], (path) => files[path] ?? (() => { throw new Error("EACCES"); })());
+ expect(groups.get(100)).toBe((240_000 + 71_680 * 2) * 1024 + 1_000 * 4096);
+ });
+
it("starts a process with an OOM score of its own and puts this process's back, whatever the spawn does", () => {
const writes: string[] = [];
let own = "-800";
diff --git a/server/engine-memory.ts b/server/engine-memory.ts
index 3245aa6c42..9551d86783 100644
--- a/server/engine-memory.ts
+++ b/server/engine-memory.ts
@@ -40,8 +40,20 @@ export function readHostMemory(read: (path: string) => string = procText): HostM
oomKills: number("/proc/vmstat", /^oom_kill (\d+)$/m), fullStall10: number("/proc/pressure/memory", /^full avg10=([\d.]+)/m) };
}
-/** Resident bytes per process group (an engine and the MCP servers it started). Shared pages count once per process, so it
- * errs high. */
+/** A process's proportional set size: each page it shares counts as its share among the processes sharing it. Null where
+ * /proc/<pid>/smaps_rollup can't be read (a kernel before 4.14, another user's process). */
+function pssBytes(pid: string, read: (path: string) => string): number | null {
+ try {
+ const found = /^Pss:\s+(\d+) kB$/m.exec(read(`/proc/${pid}/smaps_rollup`));
+ return found ? Number(found[1]) * 1024 : null;
+ } catch {
+ return null;
+ }
+}
+
+/** What each process group takes (an engine and the MCP servers it started): the sum of its processes' proportional set
+ * sizes, so engines of one kind, or MCP servers on one runtime, count the code they share once between them rather than
+ * once each, as resident pages would. A process whose share can't be read counts its resident pages, which err high. */
export function processGroupBytes(list: () => string[] = () => readdirSync("/proc"), read: (path: string) => string = procText): Map<number, number> {
const groups = new Map<number, number>();
let pids: string[];
@@ -53,7 +65,7 @@ export function processGroupBytes(list: () => string[] = () => readdirSync("/pro
// pid (comm) state ppid pgrp …, rss the 24th: comm may hold spaces and parentheses.
const fields = stat.slice(stat.lastIndexOf(")") + 2).split(" ");
const group = Number(fields[2]);
- groups.set(group, (groups.get(group) ?? 0) + (Number(fields[21]) * PAGE_BYTES || 0));
+ groups.set(group, (groups.get(group) ?? 0) + (pssBytes(pid, read) ?? (Number(fields[21]) * PAGE_BYTES || 0)));
}
return groups;
}