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

A memory reading's heap is never 0, so test/memory-watch.test.ts is green on main (#17155) #17248

opened this

Merged 42h 49m ago, live in production

scout/forge:scout/17210-a-memory-reading-s-heap-is-never-0-so-te → main 0 commits fork

  • Merged. scout/17210-a-memory-reading-s-heap-is-never-0-so-te was merged into main as f52066e.

Review

Sign in to review this change.
2 files changed +46 −12
src/perf/memory_watch.ts +21 −12
@@ -1,19 +1,19 @@
11 /**
22 * What holds the forge's memory, by owner (#12901). Once a minute the serving VM's
3 * reading (`bun:jsc` `heapSize()` and `memoryUsage()`) and the process's RSS are one log line
4 * (`memoryLine`), and a worker VM writes its own (`workerMemoryLine`), so growth reads
5 * in the log by its owner. The watch also keeps the first-hour RSS figure and says when
6 * RSS passes twice it (`grown`), which the needs-you line reads (`kernel/vitals.ts`).
3 * reading (`process.memoryUsage().heapUsed` and `bun:jsc` `memoryUsage()`) and the process's RSS
4 * are one log line (`memoryLine`), and a worker VM writes its own (`workerMemoryLine`), so growth
5 * reads in the log by its owner. The watch also keeps the first-hour RSS figure and says when RSS
6 * passes twice it (`grown`), which the needs-you line reads (`kernel/vitals.ts`).
77 *
88 * It names what grew (#12951): each owner, the main VM and every worker by name, keeps its
99 * own first-hour mean of its JS heap, main also of the RSS outside every VM's heap, and
1010 * `grown` names the part that grew the most bytes past its baseline.
1111 *
12 * What Bun can tell apart (#14264): `heapSize()` is one VM's own; `jsc.memoryUsage()` is the
12 * What Bun can tell apart (#14264): `heapUsed` is one VM's own; `jsc.memoryUsage()` is the
1313 * whole process's, and its `current` is the RSS itself (not malloc: reading `current - heap`
1414 * as a VM's malloc blamed "the main VM's malloc" for every worker's heap and every native
1515 * byte). Its `currentCommit` is mimalloc's commit for every thread at once, and overlaps the
16 * heaps (`heapSize()` counts the buffers its objects own), so it is printed on the line and
16 * heaps (the heap counts the buffers its objects own), so it is printed on the line and
1717 * is never a part. What is outside every heap belongs to the process, not to one VM. A worker's
1818 * reading reaches the serving VM on a `BroadcastChannel` the worker posts to from the same minute
1919 * timer that writes its line (`tellReading`, `hearWorkers`), so no ask and no hot-path message
@@ -31,7 +31,7 @@
3131 * the growth began is the one to read.
3232 *
3333 * `heapStats()` counts every object on the heap by type and held the loop ~280 ms a
34 * minute on a 2M-object heap (#11118), so the minute line never calls it: `heapSize()`
34 * minute on a 2M-object heap (#11118), so the minute line never calls it: `heapUsed`
3535 * and `memoryUsage()` are constant-time. Bun 1.4.4 exposes no collector time (a
3636 * "gc" PerformanceObserver gets no entries), so a stall is attributed to the collector
3737 * by what the heap did across it (`heapAcross`): a heap that fell is a collection that
@@ -44,7 +44,7 @@
4444 * past the first hour plus every fresh VM's ceiling plus `BURST_SLACK`. A leak still raises the
4545 * need; a warm cache at its ceiling, which levels off where a leak keeps climbing, does not.
4646 */
47 import { heapSize, memoryUsage as jscMemory } from "bun:jsc";
47 import { memoryUsage as jscMemory } from "bun:jsc";
4848 import { createLog } from "../kernel/log.ts";
4949 import { heldLine, threadHeld, type HeldCount } from "./held.ts";
5050 import type { Allocation } from "./job_alloc.ts";
@@ -69,7 +69,7 @@ const mib = (bytes: number): string => `${(bytes / MIB).toFixed(bytes < 100 * MI
6969
7070 export interface Reading {
7171 readonly rss: number;
72 /** This VM's own JS heap (`heapSize()`). */
72 /** This VM's own JS heap (`memoryUsage().heapUsed`). */
7373 readonly heap: number;
7474 /** `jsc.memoryUsage().currentCommit` and its peak: mimalloc's, for the whole process. */
7575 readonly commit: number;
@@ -78,13 +78,22 @@ export interface Reading {
7878 readonly dbCeiling: number;
7979 }
8080
81 /**
82 * This VM's own JS heap in bytes (#17210): `memoryUsage().heapUsed`, which is per VM (a worker
83 * reads its own) and constant time. `bun:jsc` `heapSize()` read 0 on Bun 1.4.4 until the VM's
84 * first collection, so the line, /boards/memory and a stall's `heapAcross` said "heap 0 MiB".
85 */
86 function vmHeap(): number {
87 return process.memoryUsage().heapUsed;
88 }
89
8190 /**
8291 * One reading of this VM: constant time, never `heapStats()`. `dbCeiling` is what the caller's
8392 * database handle may fill (`ceilingOf` in `kernel/db_ceiling.ts`), 0 for a VM with none.
8493 */
8594 export function readNow(rss: number = process.memoryUsage().rss, dbCeiling = 0): Reading {
8695 const m = jscMemory();
87 return { rss, heap: heapSize(), commit: m.currentCommit, peakCommit: m.peakCommit, dbCeiling };
96 return { rss, heap: vmHeap(), commit: m.currentCommit, peakCommit: m.peakCommit, dbCeiling };
8897 }
8998
9099 /** The serving VM's minute line: `memory main: rss 5.1 GiB, heap …`. */
@@ -110,7 +119,7 @@ export interface Grown {
110119
111120 /**
112121 * The parts a reading splits into (#12951, #14264): `heap` is one VM's JS heap
113 * (`heapSize()`); `outside` is the process's RSS less every fresh VM's heap, floored at 0
122 * (`heapUsed`); `outside` is the process's RSS less every fresh VM's heap, floored at 0
114123 * (malloc, SQLite's pages, native buffers, code), kept on main since RSS is the process's.
115124 */
116125 export type Part = "heap" | "outside";
@@ -433,7 +442,7 @@ export function createMemoryWatch(startedAt: number = Date.now()): MemoryWatch {
433442 }
434443 return out;
435444 },
436 heapAcross(now = Date.now(), heapNow = heapSize()) {
445 heapAcross(now = Date.now(), heapNow = vmHeap()) {
437446 if (last === undefined) return `heap ${mib(heapNow)}`;
438447 const was = `${mib(last.heap)} ${Math.max(0, Math.round((now - last.at) / 1000))} s ago`;
439448 const verdict = heapNow < last.heap * 0.9 ? "fell: a collection ran" : "did not fall: no collection seen";
test/memory-watch.test.ts +25
@@ -4,6 +4,9 @@ import {
44 BURST_SLACK, FIRST_HOUR_MS, MAX_OWNERS, MEMORY_CHANNEL, WORKER_STALE_MS, type WorkerReading,
55 } from "../src/perf/memory_watch.ts";
66 import { readNow } from "../src/perf/memory_watch.ts";
7 import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
8 import { tmpdir } from "node:os";
9 import { join } from "node:path";
710
811 const MIB = 1048576;
912 const T0 = 1_000_000_000_000;
@@ -63,6 +66,28 @@ test("a stall's heap is read against the last minute: a fall is a collection", (
6366 test("a stall's heap with no argument is read from the VM", () => {
6467 const w = createMemoryWatch(T0);
6568 expect(w.heapAcross()).toMatch(/^heap \d/);
69 expect(w.heapAcross()).not.toBe("heap 0 MiB"); // #17210: heapSize() read 0 before a collection
70 });
71
72 // #17210: `bun:jsc` heapSize() is 0 in a VM no collection has measured yet, and a worker's
73 // minute line is read from a fresh VM. Its reading is its own heap, never 0.
74 test("a worker VM's reading has a heap above 0 from its first minute", async () => {
75 const dir = mkdtempSync(join(tmpdir(), "memory-watch-"));
76 const file = join(dir, "reader.ts");
77 writeFileSync(file, `import { readNow } from ${JSON.stringify(join(import.meta.dir, "../src/perf/memory_watch.ts"))};
78 postMessage(readNow().heap);`);
79 try {
80 const worker = new Worker(file);
81 const heap = await new Promise<unknown>((resolve, reject) => {
82 worker.onmessage = (e) => resolve(e.data);
83 worker.onerror = (e) => reject(e);
84 });
85 worker.terminate();
86 expect(typeof heap).toBe("number");
87 expect(heap as number).toBeGreaterThan(0);
88 } finally {
89 rmSync(dir, { recursive: true, force: true });
90 }
6691 });
6792
6893 test("the minute lines name the owner and the figures", () => {

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

Keyboard shortcuts

Docs