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
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
2 files
changed +46
−12
src/perf/memory_watch.ts +21 −12
| @@ -1,19 +1,19 @@ | ||
| 1 | 1 | /** |
| 2 | 2 | * 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`). | |
| 7 | 7 | * |
| 8 | 8 | * It names what grew (#12951): each owner, the main VM and every worker by name, keeps its |
| 9 | 9 | * own first-hour mean of its JS heap, main also of the RSS outside every VM's heap, and |
| 10 | 10 | * `grown` names the part that grew the most bytes past its baseline. |
| 11 | 11 | * |
| 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 | |
| 13 | 13 | * whole process's, and its `current` is the RSS itself (not malloc: reading `current - heap` |
| 14 | 14 | * as a VM's malloc blamed "the main VM's malloc" for every worker's heap and every native |
| 15 | 15 | * 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 | |
| 17 | 17 | * is never a part. What is outside every heap belongs to the process, not to one VM. A worker's |
| 18 | 18 | * reading reaches the serving VM on a `BroadcastChannel` the worker posts to from the same minute |
| 19 | 19 | * timer that writes its line (`tellReading`, `hearWorkers`), so no ask and no hot-path message |
| @@ -31,7 +31,7 @@ | ||
| 31 | 31 | * the growth began is the one to read. |
| 32 | 32 | * |
| 33 | 33 | * `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` | |
| 35 | 35 | * and `memoryUsage()` are constant-time. Bun 1.4.4 exposes no collector time (a |
| 36 | 36 | * "gc" PerformanceObserver gets no entries), so a stall is attributed to the collector |
| 37 | 37 | * by what the heap did across it (`heapAcross`): a heap that fell is a collection that |
| @@ -44,7 +44,7 @@ | ||
| 44 | 44 | * past the first hour plus every fresh VM's ceiling plus `BURST_SLACK`. A leak still raises the |
| 45 | 45 | * need; a warm cache at its ceiling, which levels off where a leak keeps climbing, does not. |
| 46 | 46 | */ |
| 47 | import { heapSize, memoryUsage as jscMemory } from "bun:jsc"; | |
| 47 | import { memoryUsage as jscMemory } from "bun:jsc"; | |
| 48 | 48 | import { createLog } from "../kernel/log.ts"; |
| 49 | 49 | import { heldLine, threadHeld, type HeldCount } from "./held.ts"; |
| 50 | 50 | import type { Allocation } from "./job_alloc.ts"; |
| @@ -69,7 +69,7 @@ const mib = (bytes: number): string => `${(bytes / MIB).toFixed(bytes < 100 * MI | ||
| 69 | 69 | |
| 70 | 70 | export interface Reading { |
| 71 | 71 | readonly rss: number; |
| 72 | /** This VM's own JS heap (`heapSize()`). */ | |
| 72 | /** This VM's own JS heap (`memoryUsage().heapUsed`). */ | |
| 73 | 73 | readonly heap: number; |
| 74 | 74 | /** `jsc.memoryUsage().currentCommit` and its peak: mimalloc's, for the whole process. */ |
| 75 | 75 | readonly commit: number; |
| @@ -78,13 +78,22 @@ export interface Reading { | ||
| 78 | 78 | readonly dbCeiling: number; |
| 79 | 79 | } |
| 80 | 80 | |
| 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 | ||
| 81 | 90 | /** |
| 82 | 91 | * One reading of this VM: constant time, never `heapStats()`. `dbCeiling` is what the caller's |
| 83 | 92 | * database handle may fill (`ceilingOf` in `kernel/db_ceiling.ts`), 0 for a VM with none. |
| 84 | 93 | */ |
| 85 | 94 | export function readNow(rss: number = process.memoryUsage().rss, dbCeiling = 0): Reading { |
| 86 | 95 | 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 }; | |
| 88 | 97 | } |
| 89 | 98 | |
| 90 | 99 | /** The serving VM's minute line: `memory main: rss 5.1 GiB, heap …`. */ |
| @@ -110,7 +119,7 @@ export interface Grown { | ||
| 110 | 119 | |
| 111 | 120 | /** |
| 112 | 121 | * 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 | |
| 114 | 123 | * (malloc, SQLite's pages, native buffers, code), kept on main since RSS is the process's. |
| 115 | 124 | */ |
| 116 | 125 | export type Part = "heap" | "outside"; |
| @@ -433,7 +442,7 @@ export function createMemoryWatch(startedAt: number = Date.now()): MemoryWatch { | ||
| 433 | 442 | } |
| 434 | 443 | return out; |
| 435 | 444 | }, |
| 436 | heapAcross(now = Date.now(), heapNow = heapSize()) { | |
| 445 | heapAcross(now = Date.now(), heapNow = vmHeap()) { | |
| 437 | 446 | if (last === undefined) return `heap ${mib(heapNow)}`; |
| 438 | 447 | const was = `${mib(last.heap)} ${Math.max(0, Math.round((now - last.at) / 1000))} s ago`; |
| 439 | 448 | 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 { | ||
| 4 | 4 | BURST_SLACK, FIRST_HOUR_MS, MAX_OWNERS, MEMORY_CHANNEL, WORKER_STALE_MS, type WorkerReading, |
| 5 | 5 | } from "../src/perf/memory_watch.ts"; |
| 6 | 6 | 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"; | |
| 7 | 10 | |
| 8 | 11 | const MIB = 1048576; |
| 9 | 12 | 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", ( | ||
| 63 | 66 | test("a stall's heap with no argument is read from the VM", () => { |
| 64 | 67 | const w = createMemoryWatch(T0); |
| 65 | 68 | 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 | } | |
| 66 | 91 | }); |
| 67 | 92 | |
| 68 | 93 | test("the minute lines name the owner and the figures", () => { |
new above
offline