Public: anyone can read this; the crew can change it.

Sentinel: memory-growth was degraded 12 minutes today: The forge's own memory is 4183 MiB, over twice its first-hour 1629 MiB and past the 2176 MiB more its database caches m… #16828

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

mason/forge:mason/16801-sentinel-memory-growth-was-degraded-12-m → main 0 commits fork

  • Merged. mason/16801-sentinel-memory-growth-was-degraded-12-m was merged into main as 814939d.

Review

Sign in to review this change.
4 files changed +137 −7
docs/features/memory-growth.md +2
@@ -25,6 +25,8 @@ Since Bun 1.4.4 the serving process has held several GiB after hours. This chang
2525 2. Check the stall lines for `fell: a collection ran`: stalls that are collections point at the heap.
2626 3. Restart the forge to release it, and report the lines with the issue.
2727
28 The sentinel's memory-growth issue quotes the watch's evidence: each VM's heap, the memory outside every heap and the held counts at the first hour, at the trip and now. The trip is kept after RSS falls back (#16801), so a short spike filed an hour later still shows what it saw, and it adds `memory allocated by jobs from the run's start to the trip`: the background jobs that grew main's heap from the first minute past 1.25x to the trip, most first (`perf/job_alloc.ts`). The line ends `(sampled: N slices not counted)` when the tally skipped slices over its read budget, as the minute line does. `none counted` there means the heap grew in a request or after an await, not in a job, and it is said only when no slice was skipped; with some skipped and none measured the line says `none measured … so a job is not ruled out`. The timestamp on the `memory rss` line is the trip's own time, not the filing's: a finding filed hours after a spike quotes that spike. A new run that trips replaces the last trip.
29
2830 The need clears by itself once RSS falls under twice the baseline. A process that slept through its first hour has no baseline and raises none.
2931
3032 ## How it is built
src/boards/memory.ts +5 −2
@@ -80,10 +80,13 @@ export function startMemorySampler(app: App, everyMs = MEMORY_EVERY_MS): MemoryS
8080 // The held counts are read once: the line writes them, the watch keeps them for a finding
8181 // (#14362).
8282 const held = heldNow(app);
83 memoryWatch.note(reading.rss, reading.heap, now, { commit: reading.commit, held, ceiling: reading.dbCeiling });
83 // Read once (it empties the tally): the line writes it, the watch sums it up to a trip
84 // (#16801).
85 const jobs = allocationsOf(app);
86 memoryWatch.note(reading.rss, reading.heap, now, { commit: reading.commit, held, ceiling: reading.dbCeiling, allocated: jobs.jobs, skipped: jobs.skipped });
8487 app.log.info(memoryLine(reading, process.uptime() * 1000));
8588 app.log.info(heldLine(held));
86 const allocated = allocationLine(allocationsOf(app));
89 const allocated = allocationLine(jobs);
8790 if (allocated !== undefined) app.log.info(allocated);
8891 } catch (error) {
8992 app.log.warn("memory sample failed", { error: String(error) });
src/perf/memory_watch.ts +57 −5
@@ -22,6 +22,11 @@
2222 * Beside each line, what is held by name (`perf/held.ts`): the serving forge's holders
2323 * and the thread's on `memory holds:`, a worker's own thread's on its second line.
2424 *
25 * The trip (the minute RSS first reached twice its first hour) is kept after RSS falls back
26 * (#16801): a twelve-minute spike has passed by the time the sentinel files it, and the trip is
27 * the only reading that saw it. It also holds what the jobs grew main's heap by from the run's
28 * start (the first minute past 1.25x) to the trip, by name. A new run that trips replaces it.
29 *
2530 * `heapStats()` counts every object on the heap by type and held the loop ~280 ms a
2631 * minute on a 2M-object heap (#11118), so the minute line never calls it: `heapSize()`
2732 * and `memoryUsage()` are constant-time. Bun 1.4.4 exposes no collector time (a
@@ -39,6 +44,7 @@
3944 import { heapSize, memoryUsage as jscMemory } from "bun:jsc";
4045 import { createLog } from "../kernel/log.ts";
4146 import { heldLine, threadHeld, type HeldCount } from "./held.ts";
47 import type { Allocation } from "./job_alloc.ts";
4248
4349 const MIB = 1048576;
4450 export const FIRST_HOUR_MS = 3_600_000;
@@ -210,6 +216,13 @@ export interface NoteExtra {
210216 readonly held?: readonly HeldCount[];
211217 /** What this VM's database handle may fill by design (`kernel/db_ceiling.ts`, #14343). */
212218 readonly ceiling?: number;
219 /**
220 * The jobs that grew main's heap since the last minute (`allocationsOf` in `perf/jobs.ts`):
221 * summed from the start of a run above 1.25x to its trip, and kept with it (#16801).
222 */
223 readonly allocated?: readonly Allocation[];
224 /** The slices `allocated` did not measure (`Taken.skipped`): summed and kept beside it. */
225 readonly skipped?: number;
213226 }
214227
215228 /** The watch as it stood at the trip: the minute RSS first reached twice its first hour. */
@@ -221,8 +234,17 @@ interface Trip {
221234 /** Each fresh owner's JS heap then, by name. */
222235 readonly heaps: ReadonlyMap<string, number>;
223236 readonly held: readonly HeldCount[];
237 /** The jobs that grew main's heap from the run's start to the trip, most first. */
238 readonly jobs: readonly Allocation[];
239 /** The job slices not measured from the run's start to the trip: jobs are a floor unless 0. */
240 readonly skipped: number;
224241 }
225242
243 /** How many job names one run above 1.25x sums before the trip; a name past it is not kept. */
244 export const RUN_JOBS = 64;
245 /** How many of them the evidence names. */
246 export const EVIDENCE_JOBS = 5;
247
226248 /**
227249 * How many held-by-name counts the evidence names, and how many workers at most (#14362: under 40
228250 * lines).
@@ -268,6 +290,10 @@ export function createMemoryWatch(startedAt: number = Date.now()): MemoryWatch {
268290 let commitNow: number | undefined;
269291 let heldNow: readonly HeldCount[] = [];
270292 let trip: Trip | undefined;
293 /** What the jobs allocated since the current run above 1.25x began, by name. */
294 const runJobs = new Map<string, Allocation>();
295 /** The slices the tally did not measure in the current run (`Taken.skipped`). */
296 let runSkipped = 0;
271297 const main = owner(startedAt);
272298 const workers = new Map<string, Owner>();
273299 const grown = (): Grown | undefined => {
@@ -311,11 +337,24 @@ export function createMemoryWatch(startedAt: number = Date.now()): MemoryWatch {
311337 }
312338 if (rss > base * GROWING_RATIO) above ??= now;
313339 else above = undefined;
314 if (above === undefined) trip = undefined;
315 else if (trip === undefined && rss >= base * GROWN_RATIO) {
316 const heaps = new Map<string, number>([["main", heap]]);
317 for (const [name, w] of workers) if (now - w.at <= WORKER_STALE_MS) heaps.set(name, w.heap);
318 trip = { at: now, rss, commit: commitNow, outside: main.latest.outside, heaps, held: heldNow };
340 // The last trip outlives its run (#16801): a short spike has fallen back by the time the
341 // sentinel files it, and the trip is the only reading that saw it. A new run replaces it.
342 if (above === undefined) {
343 runJobs.clear();
344 runSkipped = 0;
345 } else {
346 if (bytes(extra.skipped)) runSkipped += extra.skipped;
347 for (const a of extra.allocated ?? []) {
348 const at = runJobs.get(a.name);
349 if (at !== undefined) runJobs.set(a.name, { name: a.name, bytes: at.bytes + a.bytes, runs: at.runs + a.runs });
350 else if (runJobs.size < RUN_JOBS) runJobs.set(a.name, { ...a });
351 }
352 if ((trip === undefined || trip.at < above) && rss >= base * GROWN_RATIO) {
353 const heaps = new Map<string, number>([["main", heap]]);
354 for (const [name, w] of workers) if (now - w.at <= WORKER_STALE_MS) heaps.set(name, w.heap);
355 const jobs = [...runJobs.values()].sort((a, b) => b.bytes - a.bytes).slice(0, EVIDENCE_JOBS);
356 trip = { at: now, rss, commit: commitNow, outside: main.latest.outside, heaps, held: heldNow, jobs, skipped: runSkipped };
357 }
319358 }
320359 },
321360 noteWorker(r, now = Date.now()) {
@@ -349,6 +388,7 @@ export function createMemoryWatch(startedAt: number = Date.now()): MemoryWatch {
349388 out.push(`memory heap worker ${name}: ${three(first(w, "heap"), trip?.heaps.get(name), w.latest.heap)}`);
350389 if (fresh.length > EVIDENCE_WORKERS) out.push(`memory heap: ${fresh.length - EVIDENCE_WORKERS} more workers not listed`);
351390 out.push(`memory outside every heap (process): ${three(first(main, "outside"), trip?.outside, main.latest.outside)}`);
391 if (trip !== undefined) out.push(`memory allocated by jobs from the run's start to the trip: ${jobsWords(trip.jobs, trip.skipped)}`);
352392 out.push(`memory mimalloc committed (process): ${three(commitCount > 0 ? commitSum / commitCount : undefined, trip?.commit, commitNow)}`);
353393 const then = new Map((trip?.held ?? []).map((c) => [c.name, c.size]));
354394 const named = heldNow.filter((c) => c.size !== 0).slice(0, EVIDENCE_HELD);
@@ -382,6 +422,18 @@ const grewWords = (g: Growth): string =>
382422 ? "the process's memory outside every JS heap (malloc, SQLite, native buffers)"
383423 : `${g.owner === "main" ? "the main VM" : `worker ${g.owner}`}'s JS heap`;
384424
425 /**
426 * `timers 2100 MiB in 9 slices, maintain 3.0 MiB in 1 slices`, ended with `(sampled: N slices not
427 * counted)` when the tally skipped any, as `allocationLine` does. With no job counted and none
428 * skipped, the growth came from a request, an await's continuation, or a worker; with some
429 * skipped, a job is not ruled out, and the line does not say it is.
430 */
431 const jobsWords = (jobs: readonly Allocation[], skipped: number): string => {
432 const sampled = skipped > 0 ? ` (sampled: ${skipped} slices not counted)` : "";
433 if (jobs.length > 0) return `${jobs.map((j) => `${j.name} ${mib(j.bytes)} in ${j.runs} slices`).join(", ")}${sampled}`;
434 return skipped > 0 ? `none measured${sampled}, so a job is not ruled out` : "none counted (a request, or work after an await, is not a job)";
435 };
436
385437 /** The needs-you sentence for a grown process: the figure, and whose part grew most (#12951). */
386438 export function grownSentence(g: Grown): string {
387439 const past = g.ceilingBytes === undefined ? "" : ` and past the ${mib(g.ceilingBytes)} more its database caches may fill`;
test/sentinel-memory-evidence.test.ts +73
@@ -93,3 +93,76 @@ test("a watch that never tripped says so, and lists no trip figures", () => {
9393 expect(lines).toContain("memory held sse: trip none, now 4");
9494 expect(createMemoryWatch().evidence()).toEqual([]);
9595 });
96
97 /**
98 * A watch with a 1000 MiB first hour (main heap 200 MiB), its hour struck at
99 * `start + FIRST_HOUR_MS`.
100 */
101 function settledWatch(start: number) {
102 const w = createMemoryWatch(start);
103 for (let i = 0; i <= 12; i++) w.note(1000 * MIB, 200 * MIB, start + i * 5 * MIN, { commit: 300 * MIB });
104 return w;
105 }
106
107 test("a spike that fell back before the finding was filed still names its trip, and the jobs that grew the heap to it (#16801)", () => {
108 const start = Date.now() - 3 * FIRST_HOUR_MS;
109 const w = settledWatch(start);
110 const t = start + FIRST_HOUR_MS + MIN;
111 // The run starts at 1.3x with a job allocating, trips at 2.4x, then falls back to the first hour.
112 w.note(1300 * MIB, 500 * MIB, t, { allocated: [{ name: "timers", bytes: 300 * MIB, runs: 4 }, { name: "maintain", bytes: 2 * MIB, runs: 1 }] });
113 w.note(2400 * MIB, 1500 * MIB, t + MIN, { allocated: [{ name: "timers", bytes: 1000 * MIB, runs: 5 }] });
114 w.note(2500 * MIB, 1600 * MIB, t + 2 * MIN, { allocated: [{ name: "late", bytes: 900 * MIB, runs: 1 }] });
115 w.note(1000 * MIB, 210 * MIB, t + 10 * MIN, { allocated: [{ name: "after", bytes: 50 * MIB, runs: 1 }] });
116
117 const text = w.evidence().join("\n");
118 expect(text).toContain(`memory rss: first hour 1000 MiB, trip 2400 MiB at ${new Date(t + MIN).toISOString()}, now 1000 MiB`);
119 expect(text).toContain("memory heap main: first hour 200 MiB, trip 1500 MiB, now 210 MiB");
120 // Summed from the run's start to the trip, most first; nothing after the trip is in it.
121 expect(text).toContain("memory allocated by jobs from the run's start to the trip: timers 1300 MiB in 9 slices, maintain 2.0 MiB in 1 slices");
122 expect(text).not.toContain("late");
123 expect(text).not.toContain("after");
124 });
125
126 test("a new run that trips replaces the last trip; a run that never trips leaves it", () => {
127 const start = Date.now() - 3 * FIRST_HOUR_MS;
128 const w = settledWatch(start);
129 const t = start + FIRST_HOUR_MS + MIN;
130 w.note(2400 * MIB, 1500 * MIB, t);
131 w.note(1000 * MIB, 200 * MIB, t + MIN);
132 // A run past 1.25x that never reaches twice: the first trip stands.
133 w.note(1500 * MIB, 300 * MIB, t + 2 * MIN);
134 w.note(1000 * MIB, 200 * MIB, t + 3 * MIN);
135 expect(w.evidence()[0]).toContain(`trip 2400 MiB at ${new Date(t).toISOString()}`);
136 // A second run that trips: its own figures, and no job counted in it says so.
137 w.note(2600 * MIB, 1700 * MIB, t + 4 * MIN);
138 const text = w.evidence().join("\n");
139 expect(text).toContain(`trip 2600 MiB at ${new Date(t + 4 * MIN).toISOString()}`);
140 expect(text).toContain("memory allocated by jobs from the run's start to the trip: none counted");
141 });
142
143 test("a trip whose job slices were sampled out does not rule jobs out, and says how many were not counted (#16801)", () => {
144 const start = Date.now() - 3 * FIRST_HOUR_MS;
145 const w = settledWatch(start);
146 const t = start + FIRST_HOUR_MS + MIN;
147 w.note(1300 * MIB, 500 * MIB, t, { allocated: [], skipped: 3 });
148 w.note(2400 * MIB, 1500 * MIB, t + MIN, { allocated: [], skipped: 4 });
149 // After the trip: not in its count.
150 w.note(2500 * MIB, 1600 * MIB, t + 2 * MIN, { allocated: [], skipped: 50 });
151 const line = w.evidence().find((l) => l.startsWith("memory allocated by jobs")) ?? "";
152 expect(line).toContain("(sampled: 7 slices not counted)");
153 expect(line).not.toContain("none counted");
154 expect(line).not.toContain("is not a job");
155 });
156
157 test("jobs counted beside sampled-out slices name both", () => {
158 const start = Date.now() - 3 * FIRST_HOUR_MS;
159 const w = settledWatch(start);
160 const t = start + FIRST_HOUR_MS + MIN;
161 w.note(2400 * MIB, 1500 * MIB, t, { allocated: [{ name: "timers", bytes: 300 * MIB, runs: 4 }], skipped: 2 });
162 expect(w.evidence().join("\n")).toContain("memory allocated by jobs from the run's start to the trip: timers 300 MiB in 4 slices (sampled: 2 slices not counted)");
163 });
164
165 test("a watch that never tripped names no jobs", () => {
166 const w = settledWatch(Date.now() - 3 * FIRST_HOUR_MS);
167 expect(w.evidence().some((l) => l.startsWith("memory allocated by jobs"))).toBe(false);
168 });

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs