Public: anyone can read this; the crew can change it.
The memory line says what is held: RSS and its anonymous part, and the allocator's churn as a rate, never "committed 126 GiB" #17667
Merged 20h 10m ago, live in production
memory-line-true → main 0 commits 2 threads · 2 unresolved
7 files
changed +128
−49
docs/features/memory-growth.md +2 −2
| @@ -8,14 +8,14 @@ Since Bun 1.4.4 the serving process has held several GiB after hours. This chang | ||
| 8 | 8 | |
| 9 | 9 | ## Where it shows |
| 10 | 10 | |
| 11 | - **The minute line.** Each minute the log carries `memory main: rss …, heap …, process mimalloc committed … (peak …), up … min`. Every worker VM writes `memory worker <name>: heap …, process rss …` on its own minute (#12925). Only the heap is one VM's own: Bun's `memoryUsage()` is the whole process's, and its `current` is the RSS itself, not malloc (#14264). RSS rising while the main heap stays flat is native memory; a worker line rising is that worker's. Read them in `/system`'s log or the process log. | |
| 11 | - **The minute line.** Each minute the log carries `memory main: rss … (anonymous …), heap …, allocator churn … a minute, database caches up to …, up … min`. Bun's mimalloc total (`currentCommit`) only grows on Linux (126,739 MiB 22 minutes after a boot, beside a VmSize of 22 GiB), so it is said only as churn, a rate; what is held is RSS and its anonymous part. Every worker VM writes `memory worker <name>: heap …, process rss …` on its own minute (#12925). Only the heap is one VM's own: Bun's `memoryUsage()` is the whole process's, and its `current` is the RSS itself, not malloc (#14264). RSS rising while the main heap stays flat is native memory; a worker line rising is that worker's. Read them in `/system`'s log or the process log. | |
| 12 | 12 | - **What is held.** Beside the minute line the log carries `memory holds: <name> <count>, …`: the forge's long-lived Maps, Sets and lists by name with how many entries each holds now, largest first, twelve at most (`src/perf/held.ts`). A worker whose thread holds anything writes `memory worker <name> holds: …`. A name whose count only rises from line to line is the holder to read. |
| 13 | 13 | - **The stall line.** Each loop-stall line ends with `heap X (was Y N s ago; …)`, the heap now against the last minute's sample. |
| 14 | 14 | - `fell: a collection ran` means the heap shrank by more than a tenth since the sample, so the collector ran inside that stall. Bun 1.4.4 reports no collector time, so this is inferred, not measured. |
| 15 | 15 | - `did not fall: no collection seen` means the stall was not the collector's. |
| 16 | 16 | - **The need.** When RSS is at least twice the mean of the process's first hour (at least ten samples), `/system`, the board's forge strip and the terminal's home each list one need: the figure, the first-hour figure, which VM's part grew most and since when (#12951), for example "The forge's own memory is 4200 MiB, over twice its first-hour 2000 MiB; worker pages's JS heap grew most (100 MiB to 2410 MiB) since 2026-11-15T04:00:00.000Z." |
| 17 | 17 | - **What fills by design is not growth (#14343).** A database handle may fill its page cache (`FORGE_DB_CACHE_MIB`, 1 GiB on the serving handle, 128 MiB on the engine's) and its 512 MiB map as pages are read, each capped by the file's size. Two handles are counted: the serving VM's `app.db` (`src/boards/memory.ts`) and the engine worker's `app.db` (`src/engine/worker.ts`). Each reads its ceiling from the handle's own pragmas with its minute reading (`ceilingOf` in `src/kernel/db_ceiling.ts`: `min(cache, file) + min(map, file)`); nothing is registered, and a closed handle reads 0. On production's 4.5 GB database the two come to 2176 MiB, most of it filled after the first hour. The line says `database caches up to …`; the engine's crosses on the channel as `ceiling`. The need speaks only when RSS is past twice the first hour *and* past the first hour plus the ceilings of the VMs heard from in the last five minutes; the sentence then names that sum. The read-only handles of the `pages`, `lanes`, `pipeline` and `map` workers are not counted: their caches can only make the need fire sooner, never hide it. Production's evidence on #14343 held 3.0 to 3.3 GiB for twelve hours against a 1420 MiB first hour: a plateau under 1420 + 2176 = 3596 MiB, the shape of a warm cache, where a leak keeps climbing. The ceiling is an upper bound, so a leak smaller than the part of the two caches still cold is not said until it passes it. |
| 18 | - **The parts.** Each owner (the main VM, and each worker by name) keeps its own first-hour mean of its `JS heap` (`heapSize()`); main also keeps the process's `memory outside every JS heap` (RSS less main's heap and every reporting worker's, floored at 0: malloc, SQLite's pages, native buffers). That part is the process's, not one VM's, and the sentence says so; mimalloc's commit is printed on the line but is never a part, since it overlaps the heaps. A worker's hour starts at its first reading, so one started on its first ask has its own. The part named is the one that grew the most bytes past its baseline. A worker silent five minutes is not named, and its heap stops counting against the outside. When no part is above its own hour the sentence says so. | |
| 18 | - **The parts.** Each owner (the main VM, and each worker by name) keeps its own first-hour mean of its `JS heap` (`heapSize()`); main also keeps the process's `memory outside every JS heap` (RSS less main's heap and every reporting worker's, floored at 0: malloc, SQLite's pages, native buffers). That part is the process's, not one VM's, and the sentence says so; the allocator's churn is a rate on the line and never a part. A worker's hour starts at its first reading, so one started on its first ask has its own. The part named is the one that grew the most bytes past its baseline. A worker silent five minutes is not named, and its heap stops counting against the outside. When no part is above its own hour the sentence says so. | |
| 19 | 19 | - **The worker names.** `engine` (src/engine/worker.ts); `disk`, `tree` (src/apps); `lanes`, `pages`, `pipeline` (src/flow); `scan`, `du`, `leak` (the `answerAsks` workers of src/kernel/worker_ask.ts); `thumb` (src/looks); `log-writer` (src/runs); `mirror`, `vacuum` (src/snapshot); `code-rows`, `map`, `raster` (src/web). A one-shot worker (`mirror`, `vacuum`) that ends inside its first minute writes no line; the timer is unref'd, so it never keeps a worker alive. |
| 20 | 20 | |
| 21 | 21 | ## What to do when the need shows |
src/boards/memory.ts +17 −6
| @@ -21,7 +21,7 @@ import { heldLine, heldNow } from "../perf/held.ts"; | ||
| 21 | 21 | import { allocationLine } from "../perf/job_alloc.ts"; |
| 22 | 22 | import { ceilingOf } from "../kernel/db_ceiling.ts"; |
| 23 | 23 | import { keepIfGrown } from "../perf/memory_kept.ts"; |
| 24 | import { hearWorkers, memoryLine, memoryWatch, readNow, type MemoryWatch } from "../perf/memory_watch.ts"; | |
| 24 | import { churnPerMinute, hearWorkers, memoryLine, memoryWatch, readNow, type MemoryWatch, type Reading } from "../perf/memory_watch.ts"; | |
| 25 | 25 | |
| 26 | 26 | export const MEMORY_EVERY_MS = 60_000; |
| 27 | 27 | export const HOUR_MS = 3_600_000; |
| @@ -74,12 +74,23 @@ export type MemorySampler = { tick(now?: number): void; stop(): void }; | ||
| 74 | 74 | |
| 75 | 75 | /** |
| 76 | 76 | * Sample now and every minute until stopped. A failed tick is logged and the next one tries again. |
| 77 | * `watch` is the process's own; a test hands it one already grown (#16872). | |
| 77 | * `watch` is the process's own; a test hands it one already grown (#16872), and `read` its own | |
| 78 | * readings, so the churn between two ticks is known. | |
| 78 | 79 | */ |
| 79 | export function startMemorySampler(app: App, everyMs = MEMORY_EVERY_MS, watch: MemoryWatch = memoryWatch): MemorySampler { | |
| 80 | export function startMemorySampler( | |
| 81 | app: App, | |
| 82 | everyMs = MEMORY_EVERY_MS, | |
| 83 | watch: MemoryWatch = memoryWatch, | |
| 84 | read: (ceiling: number) => Reading = (ceiling) => readNow(undefined, ceiling), | |
| 85 | ): MemorySampler { | |
| 86 | // The last reading's allocator total, for its rate (`churnPerMinute`): the total itself is no | |
| 87 | // size. | |
| 88 | let before: { committedSoFar: number; at: number } | undefined; | |
| 80 | 89 | const tick = (now = Date.now()) => { |
| 81 | 90 | try { |
| 82 | const reading = readNow(undefined, ceilingOf(app.db)); | |
| 91 | const reading = read(ceilingOf(app.db)); | |
| 92 | const churn = churnPerMinute(before, { committedSoFar: reading.committedSoFar, at: now }); | |
| 93 | before = { committedSoFar: reading.committedSoFar, at: now }; | |
| 83 | 94 | recordMemory(app.db, reading.rss, reading.heap, now); |
| 84 | 95 | // The held counts are read once: the line writes them, the watch keeps them for a finding |
| 85 | 96 | // (#14362). |
| @@ -90,12 +101,12 @@ export function startMemorySampler(app: App, everyMs = MEMORY_EVERY_MS, watch: M | ||
| 90 | 101 | // The jobs open now: a trip keeps them, so native memory held by work that ended names it |
| 91 | 102 | // (#17083). |
| 92 | 103 | watch.note(reading.rss, reading.heap, now, { |
| 93 | commit: reading.commit, held, ceiling: reading.dbCeiling, allocated: jobs.jobs, skipped: jobs.skipped, running: runningOf(app), | |
| 104 | ...(reading.anon === undefined ? {} : { anon: reading.anon }), held, ceiling: reading.dbCeiling, allocated: jobs.jobs, skipped: jobs.skipped, running: runningOf(app), | |
| 94 | 105 | }); |
| 95 | 106 | // A grown minute's evidence outlives this process (#16872): a restart would leave the |
| 96 | 107 | // sentinel quoting a fresh watch that never tripped. |
| 97 | 108 | keepIfGrown(app.config.dataDir, watch, now); |
| 98 | app.log.info(memoryLine(reading, process.uptime() * 1000)); | |
| 109 | app.log.info(memoryLine(reading, process.uptime() * 1000, churn)); | |
| 99 | 110 | app.log.info(heldLine(held)); |
| 100 | 111 | const allocated = allocationLine(jobs); |
| 101 | 112 | if (allocated !== undefined) app.log.info(allocated); |
src/perf/memory_watch.ts +51 −27
| @@ -12,9 +12,11 @@ | ||
| 12 | 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 | * byte). Its `currentCommit` is mimalloc's commit for every thread at once, and overlaps the | |
| 16 | * heaps (the heap counts the buffers its objects own), so it is printed on the line and | |
| 17 | * is never a part. What is outside every heap belongs to the process, not to one VM. A worker's | |
| 15 | * byte). Its `currentCommit` is not memory held: on Linux it only grows (126,739 MiB 22 minutes | |
| 16 | * after a boot on 2026-12-02, beside a VmSize of 22 GiB and an RSS of 1.8 GiB), so the line says | |
| 17 | * it only as the allocator's churn, its rise a minute, never as a size; what is held is RSS and | |
| 18 | * its anonymous part. Churn overlaps the heaps (the heap counts the buffers its objects own), so it | |
| 19 | * is never a part either. What is outside every heap belongs to the process, not to one VM. A worker's | |
| 18 | 20 | * reading reaches the serving VM on a `BroadcastChannel` the worker posts to from the same minute |
| 19 | 21 | * timer that writes its line (`tellReading`, `hearWorkers`), so no ask and no hot-path message |
| 20 | 22 | * carries it; a worker silent for five minutes is not blamed. |
| @@ -45,6 +47,7 @@ | ||
| 45 | 47 | * need; a warm cache at its ceiling, which levels off where a leak keeps climbing, does not. |
| 46 | 48 | */ |
| 47 | 49 | import { memoryUsage as jscMemory } from "bun:jsc"; |
| 50 | import { readFileSync } from "node:fs"; | |
| 48 | 51 | import { createLog } from "../kernel/log.ts"; |
| 49 | 52 | import { heldLine, threadHeld, type HeldCount } from "./held.ts"; |
| 50 | 53 | import type { Allocation } from "./job_alloc.ts"; |
| @@ -71,9 +74,13 @@ export interface Reading { | ||
| 71 | 74 | readonly rss: number; |
| 72 | 75 | /** This VM's own JS heap (`memoryUsage().heapUsed`). */ |
| 73 | 76 | readonly heap: number; |
| 74 | /** `jsc.memoryUsage().currentCommit` and its peak: mimalloc's, for the whole process. */ | |
| 75 | readonly commit: number; | |
| 76 | readonly peakCommit: number; | |
| 77 | /** The anonymous part of RSS (`RssAnon` in /proc/self/status): undefined where there is none. */ | |
| 78 | readonly anon: number | undefined; | |
| 79 | /** | |
| 80 | * `jsc.memoryUsage().currentCommit`: what mimalloc has committed since the process began, as | |
| 81 | * Bun reports it. It only grows, so only its rate is said (`churnPerMinute`), never its size. | |
| 82 | */ | |
| 83 | readonly committedSoFar: number; | |
| 77 | 84 | /** What this VM's open databases may fill by design (`kernel/db_ceiling.ts`). */ |
| 78 | 85 | readonly dbCeiling: number; |
| 79 | 86 | } |
| @@ -92,13 +99,30 @@ function vmHeap(): number { | ||
| 92 | 99 | * database handle may fill (`ceilingOf` in `kernel/db_ceiling.ts`), 0 for a VM with none. |
| 93 | 100 | */ |
| 94 | 101 | export function readNow(rss: number = process.memoryUsage().rss, dbCeiling = 0): Reading { |
| 95 | const m = jscMemory(); | |
| 96 | return { rss, heap: vmHeap(), commit: m.currentCommit, peakCommit: m.peakCommit, dbCeiling }; | |
| 102 | return { rss, anon: rssAnon(), heap: vmHeap(), committedSoFar: jscMemory().currentCommit, dbCeiling }; | |
| 97 | 103 | } |
| 98 | 104 | |
| 99 | /** The serving VM's minute line: `memory main: rss 5.1 GiB, heap …`. */ | |
| 100 | export function memoryLine(r: Reading, uptimeMs: number): string { | |
| 101 | return `memory main: rss ${mib(r.rss)}, heap ${mib(r.heap)}, process mimalloc committed ${mib(r.commit)} (peak ${mib(r.peakCommit)}), database caches up to ${mib(r.dbCeiling)}, up ${Math.round(uptimeMs / 60000)} min`; | |
| 105 | /** The process's anonymous RSS from /proc/self/status, in bytes; undefined off Linux. */ | |
| 106 | export function rssAnon(status: () => string = () => readFileSync("/proc/self/status", "utf8")): number | undefined { | |
| 107 | try { | |
| 108 | const kb = /^RssAnon:\s+(\d+) kB$/m.exec(status())?.[1]; | |
| 109 | return kb === undefined ? undefined : Number(kb) * 1024; | |
| 110 | } catch { | |
| 111 | return undefined; | |
| 112 | } | |
| 113 | } | |
| 114 | ||
| 115 | /** The allocator's churn between two readings, bytes a minute; undefined for a first or a reset. */ | |
| 116 | export function churnPerMinute(before: { committedSoFar: number; at: number } | undefined, now: { committedSoFar: number; at: number }): number | undefined { | |
| 117 | if (before === undefined || now.at <= before.at || now.committedSoFar < before.committedSoFar) return undefined; | |
| 118 | return ((now.committedSoFar - before.committedSoFar) * 60_000) / (now.at - before.at); | |
| 119 | } | |
| 120 | ||
| 121 | /** The serving VM's minute line: `memory main: rss 1844 MiB (anonymous 1662 MiB), heap …`. */ | |
| 122 | export function memoryLine(r: Reading, uptimeMs: number, churn?: number): string { | |
| 123 | const anon = r.anon === undefined ? "" : ` (anonymous ${mib(r.anon)})`; | |
| 124 | const turned = churn === undefined ? "" : `, allocator churn ${mib(churn)} a minute`; | |
| 125 | return `memory main: rss ${mib(r.rss)}${anon}, heap ${mib(r.heap)}${turned}, database caches up to ${mib(r.dbCeiling)}, up ${Math.round(uptimeMs / 60000)} min`; | |
| 102 | 126 | } |
| 103 | 127 | |
| 104 | 128 | /** A worker VM's line: its heap is its own; RSS is the process's. */ |
| @@ -220,12 +244,12 @@ function mostGrown(owners: Iterable<readonly [string, Owner]>, now: number): Gro | ||
| 220 | 244 | } |
| 221 | 245 | |
| 222 | 246 | /** |
| 223 | * What the serving VM's minute tick also hands the watch (#14362): mimalloc's commit for the | |
| 224 | * process and the held-by-name counts it already read for its line. Both are kept as the | |
| 225 | * latest and, at the trip, as they stood then; nothing here is read by the watch itself. | |
| 247 | * What the serving VM's minute tick also hands the watch (#14362): the process's anonymous RSS | |
| 248 | * and the held-by-name counts it already read for its line. Both are kept as the latest and, at | |
| 249 | * the trip, as they stood then; nothing here is read by the watch itself. | |
| 226 | 250 | */ |
| 227 | 251 | export interface NoteExtra { |
| 228 | readonly commit?: number; | |
| 252 | readonly anon?: number; | |
| 229 | 253 | readonly held?: readonly HeldCount[]; |
| 230 | 254 | /** What this VM's database handle may fill by design (`kernel/db_ceiling.ts`, #14343). */ |
| 231 | 255 | readonly ceiling?: number; |
| @@ -244,7 +268,7 @@ export interface NoteExtra { | ||
| 244 | 268 | interface Trip { |
| 245 | 269 | readonly at: number; |
| 246 | 270 | readonly rss: number; |
| 247 | readonly commit: number | undefined; | |
| 271 | readonly anon: number | undefined; | |
| 248 | 272 | readonly outside: number | undefined; |
| 249 | 273 | /** Each fresh owner's JS heap then, by name. */ |
| 250 | 274 | readonly heaps: ReadonlyMap<string, number>; |
| @@ -292,8 +316,8 @@ export interface MemoryWatch { | ||
| 292 | 316 | grown(): Grown | undefined; |
| 293 | 317 | /** |
| 294 | 318 | * The lines a memory-growth finding quotes (#14362): each reporting VM's JS heap and the |
| 295 | * process's RSS outside every heap at the first-hour mean, at the trip and now; mimalloc's | |
| 296 | * commit; the largest held-by-name counts at the trip and now. Read from what the minute | |
| 319 | * process's RSS outside every heap at the first-hour mean, at the trip and now; its anonymous | |
| 320 | * RSS; the largest held-by-name counts at the trip and now. Read from what the minute | |
| 297 | 321 | * tick kept: nothing is measured here. Empty before any reading. |
| 298 | 322 | */ |
| 299 | 323 | evidence(): string[]; |
| @@ -313,9 +337,9 @@ export function createMemoryWatch(startedAt: number = Date.now()): MemoryWatch { | ||
| 313 | 337 | /** The last minute RSS was not above 1.25x its first hour (every first-hour minute is one). */ |
| 314 | 338 | let below: number | undefined; |
| 315 | 339 | let latest: number | undefined; |
| 316 | let commitSum = 0; | |
| 317 | let commitCount = 0; | |
| 318 | let commitNow: number | undefined; | |
| 340 | let anonSum = 0; | |
| 341 | let anonCount = 0; | |
| 342 | let anonNow: number | undefined; | |
| 319 | 343 | let heldNow: readonly HeldCount[] = []; |
| 320 | 344 | let runningNow: readonly Running[] | undefined; |
| 321 | 345 | let trip: Trip | undefined; |
| @@ -346,10 +370,10 @@ export function createMemoryWatch(startedAt: number = Date.now()): MemoryWatch { | ||
| 346 | 370 | main.ceiling = bytes(extra.ceiling) ? extra.ceiling : 0; |
| 347 | 371 | last = { heap, rss, at: now }; |
| 348 | 372 | latest = rss; |
| 349 | commitNow = bytes(extra.commit) ? extra.commit : undefined; | |
| 350 | if (commitNow !== undefined && now - startedAt < FIRST_HOUR_MS) { | |
| 351 | commitSum += commitNow; | |
| 352 | commitCount++; | |
| 373 | anonNow = bytes(extra.anon) ? extra.anon : undefined; | |
| 374 | if (anonNow !== undefined && now - startedAt < FIRST_HOUR_MS) { | |
| 375 | anonSum += anonNow; | |
| 376 | anonCount++; | |
| 353 | 377 | } |
| 354 | 378 | if (extra.held !== undefined) heldNow = extra.held.slice(); |
| 355 | 379 | runningNow = extra.running === undefined ? undefined : extra.running.filter(isRunning); |
| @@ -390,7 +414,7 @@ export function createMemoryWatch(startedAt: number = Date.now()): MemoryWatch { | ||
| 390 | 414 | for (const [name, w] of workers) if (now - w.at <= WORKER_STALE_MS) heaps.set(name, w.heap); |
| 391 | 415 | const jobs = [...runJobs.values()].sort((a, b) => b.bytes - a.bytes).slice(0, EVIDENCE_JOBS); |
| 392 | 416 | trip = { |
| 393 | at: now, rss, commit: commitNow, outside: main.latest.outside, heaps, held: heldNow, jobs, skipped: runSkipped, | |
| 417 | at: now, rss, anon: anonNow, outside: main.latest.outside, heaps, held: heldNow, jobs, skipped: runSkipped, | |
| 394 | 418 | runStart: below ?? above, running: runningNow ?? [], |
| 395 | 419 | }; |
| 396 | 420 | } |
| @@ -430,7 +454,7 @@ export function createMemoryWatch(startedAt: number = Date.now()): MemoryWatch { | ||
| 430 | 454 | if (trip !== undefined) out.push(`memory allocated by jobs from the run's start to the trip: ${jobsWords(trip.jobs, trip.skipped)}`); |
| 431 | 455 | if (trip !== undefined) out.push(`memory jobs open at the trip: ${openWords(trip.running, trip.runStart)}`); |
| 432 | 456 | if (runningNow !== undefined) out.push(`memory jobs open now: ${openWords(runningNow)}`); |
| 433 | out.push(`memory mimalloc committed (process): ${three(commitCount > 0 ? commitSum / commitCount : undefined, trip?.commit, commitNow)}`); | |
| 457 | out.push(`memory anonymous rss (process): ${three(anonCount > 0 ? anonSum / anonCount : undefined, trip?.anon, anonNow)}`); | |
| 434 | 458 | const then = new Map((trip?.held ?? []).map((c) => [c.name, c.size])); |
| 435 | 459 | const named = heldNow.filter((c) => c.size !== 0).slice(0, EVIDENCE_HELD); |
| 436 | 460 | if (named.length === 0) out.push("memory held: nothing held by name now"); |
test/boot-idle-memory.test.ts +2 −2
| @@ -1,7 +1,7 @@ | ||
| 1 | 1 | /** |
| 2 | 2 | * A fresh forge on empty data, booted as production boots it, settles under `IDLE_RSS_BUDGET` |
| 3 | * (#16851). When it does not, the failure prints the forge's own memory line (JS heap, mimalloc | |
| 4 | * committed, RSS) so the cause is named, not only the total. | |
| 3 | * (#16851). When it does not, the failure prints the forge's own memory line (JS heap, anonymous | |
| 4 | * RSS, RSS) so the cause is named, not only the total. | |
| 5 | 5 | * |
| 6 | 6 | * Linux only (`IDLE_SKIP_REASON`). |
| 7 | 7 | * |
test/held.test.ts +31
| @@ -106,4 +106,35 @@ describe("the memory sampler", () => { | ||
| 106 | 106 | await rm(dir, { recursive: true, force: true }); |
| 107 | 107 | } |
| 108 | 108 | }); |
| 109 | ||
| 110 | test("the allocator's churn is on the line from the second tick, as the rise since the first (#17667)", async () => { | |
| 111 | const dir = await mkdtemp(join(tmpdir(), "forge-churn-")); | |
| 112 | const app = createApp({ dataDir: dir, port: 0 }); | |
| 113 | const info = spyOn(app.log, "info").mockImplementation(() => {}); | |
| 114 | const MIB = 1024 * 1024; | |
| 115 | const totals = [100 * MIB, 400 * MIB, 400 * MIB]; | |
| 116 | const read = (dbCeiling: number) => ({ rss: 900 * MIB, anon: 700 * MIB, heap: 50 * MIB, committedSoFar: totals.shift()!, dbCeiling }); | |
| 117 | const t0 = Date.now(); | |
| 118 | // The first tick is the sampler's own, as it starts; an hour apart, so no timer tick lands between. | |
| 119 | const sampler = startMemorySampler(app, 3_600_000, undefined, read); | |
| 120 | try { | |
| 121 | sampler.tick(t0 + 30_000); | |
| 122 | sampler.tick(t0 + 90_000); | |
| 123 | const mains = info.mock.calls.map((call) => String(call[0])).filter((l) => l.startsWith("memory main:")); | |
| 124 | expect(mains).toHaveLength(3); | |
| 125 | expect(mains[0]).toContain("rss 900 MiB (anonymous 700 MiB)"); | |
| 126 | expect(mains[0]).not.toContain("churn"); | |
| 127 | // 300 MiB risen in the 30 s or so since the first tick: some 600 MiB a minute. | |
| 128 | expect(mains[1]).toMatch(/allocator churn \d+ MiB a minute/); | |
| 129 | expect(Number(/allocator churn (\d+) MiB/.exec(mains[1]!)![1])).toBeGreaterThan(300); | |
| 130 | // Nothing risen in the next minute: churn 0, said as a rate, never the total. | |
| 131 | expect(mains[2]).toContain("allocator churn 0.0 MiB a minute"); | |
| 132 | for (const l of mains) expect(l).not.toContain("400 MiB"); | |
| 133 | } finally { | |
| 134 | sampler.stop(); | |
| 135 | info.mockRestore(); | |
| 136 | app.close(); | |
| 137 | await rm(dir, { recursive: true, force: true }); | |
| 138 | } | |
| 139 | }); | |
| 109 | 140 | }); |
test/memory-watch.test.ts +18 −5
| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | import { expect, test } from "bun:test"; |
| 2 | 2 | import { |
| 3 | createMemoryWatch, grownSentence, hearWorkers, memoryLine, tellReading, workerMemoryLine, workerReadingOf, | |
| 3 | churnPerMinute, createMemoryWatch, grownSentence, hearWorkers, memoryLine, rssAnon, tellReading, workerMemoryLine, workerReadingOf, | |
| 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"; |
| @@ -91,8 +91,11 @@ postMessage(readNow().heap);`); | ||
| 91 | 91 | }); |
| 92 | 92 | |
| 93 | 93 | test("the minute lines name the owner and the figures", () => { |
| 94 | const r = { rss: 5 * 1024 * MIB, heap: 1024 * MIB, commit: 512 * MIB, peakCommit: 700 * MIB, dbCeiling: 1536 * MIB }; | |
| 95 | expect(memoryLine(r, 3_600_000)).toBe("memory main: rss 5120 MiB, heap 1024 MiB, process mimalloc committed 512 MiB (peak 700 MiB), database caches up to 1536 MiB, up 60 min"); | |
| 94 | const r = { rss: 5 * 1024 * MIB, anon: 4 * 1024 * MIB, heap: 1024 * MIB, committedSoFar: 126 * 1024 * MIB, dbCeiling: 1536 * MIB }; | |
| 95 | expect(memoryLine(r, 3_600_000, 5 * 1024 * MIB)).toBe("memory main: rss 5120 MiB (anonymous 4096 MiB), heap 1024 MiB, allocator churn 5120 MiB a minute, database caches up to 1536 MiB, up 60 min"); | |
| 96 | // The allocator's total is never said as a size: on Linux it only grows (#17629). | |
| 97 | expect(memoryLine(r, 3_600_000)).not.toContain("126"); | |
| 98 | expect(memoryLine({ ...r, anon: undefined }, 60_000)).toBe("memory main: rss 5120 MiB, heap 1024 MiB, database caches up to 1536 MiB, up 1 min"); | |
| 96 | 99 | expect(workerMemoryLine("engine", r)).toBe("memory worker engine: heap 1024 MiB, process rss 5120 MiB"); |
| 97 | 100 | expect(readNow().heap).toBeGreaterThan(0); |
| 98 | 101 | }); |
| @@ -100,7 +103,17 @@ test("the minute lines name the owner and the figures", () => { | ||
| 100 | 103 | // #14264: `jsc.memoryUsage().current` is the RSS, not malloc. Read as "malloc", RSS less |
| 101 | 104 | // main's heap blamed the main VM for every worker's heap and every native byte. |
| 102 | 105 | test("no part is read from Bun's memoryUsage().current", () => { |
| 103 | expect(Object.keys(readNow()).sort()).toEqual(["commit", "dbCeiling", "heap", "peakCommit", "rss"]); | |
| 106 | expect(Object.keys(readNow()).sort()).toEqual(["anon", "committedSoFar", "dbCeiling", "heap", "rss"]); | |
| 107 | }); | |
| 108 | ||
| 109 | test("the allocator's total is said as a rate a minute, and anonymous RSS from the kernel's own line", () => { | |
| 110 | expect(churnPerMinute(undefined, { committedSoFar: 9, at: 1 })).toBeUndefined(); | |
| 111 | expect(churnPerMinute({ committedSoFar: 100 * MIB, at: 0 }, { committedSoFar: 400 * MIB, at: 30_000 })).toBe(600 * MIB); | |
| 112 | // A total that went down is a new process's, not a negative churn. | |
| 113 | expect(churnPerMinute({ committedSoFar: 400 * MIB, at: 0 }, { committedSoFar: 100 * MIB, at: 60_000 })).toBeUndefined(); | |
| 114 | expect(rssAnon(() => "Name:\tbun\nVmRSS:\t 1849696 kB\nRssAnon:\t 1701708 kB\nRssFile:\t 147988 kB\n")).toBe(1701708 * 1024); | |
| 115 | expect(rssAnon(() => "Name:\tbun\n")).toBeUndefined(); | |
| 116 | expect(rssAnon(() => { throw new Error("no /proc here"); })).toBeUndefined(); | |
| 104 | 117 | }); |
| 105 | 118 | |
| 106 | 119 | // #12951: which VM grew, and which part. First hour: rss 2000, main heap 500, worker pages |
| @@ -227,7 +240,7 @@ test("a worker's reading crosses on the memory channel to the watch that hears i | ||
| 227 | 240 | const { promise: two, resolve } = Promise.withResolvers<void>(); |
| 228 | 241 | const stop = hearWorkers({ noteWorker: (r) => { got.push(r); if (got.length === 2) resolve(); } }); |
| 229 | 242 | try { |
| 230 | const r = { rss: 9, heap: 3 * MIB, commit: 7 * MIB, peakCommit: 9, dbCeiling: 640 * MIB }; | |
| 243 | const r = { rss: 9, anon: undefined, heap: 3 * MIB, committedSoFar: 7 * MIB, dbCeiling: 640 * MIB }; | |
| 231 | 244 | expect(tellReading("pages", r)).toBe(r); |
| 232 | 245 | const other = new BroadcastChannel(MEMORY_CHANNEL); |
| 233 | 246 | other.postMessage({ name: "../etc", heap: 1 }); // malformed: dropped |
test/sentinel-memory-evidence.test.ts +7 −7
| @@ -38,16 +38,16 @@ function grownWatch(start: number) { | ||
| 38 | 38 | ]; |
| 39 | 39 | for (let i = 0; i < 12; i++) { |
| 40 | 40 | w.noteWorker({ name: "pages", heap: 100 * MIB }, start + i * 5 * MIN); |
| 41 | w.note(2000 * MIB, 500 * MIB, start + i * 5 * MIN, { commit: 1200 * MIB, held: held(10) }); | |
| 41 | w.note(2000 * MIB, 500 * MIB, start + i * 5 * MIN, { anon: 1200 * MIB, held: held(10) }); | |
| 42 | 42 | } |
| 43 | 43 | // The hour is over: the baseline is struck, then the trip (twice the first hour), then now. |
| 44 | 44 | const trip = start + FIRST_HOUR_MS + MIN; |
| 45 | 45 | w.noteWorker({ name: "pages", heap: 100 * MIB }, start + FIRST_HOUR_MS); |
| 46 | w.note(2000 * MIB, 500 * MIB, start + FIRST_HOUR_MS, { commit: 1200 * MIB, held: held(10) }); | |
| 46 | w.note(2000 * MIB, 500 * MIB, start + FIRST_HOUR_MS, { anon: 1200 * MIB, held: held(10) }); | |
| 47 | 47 | w.noteWorker({ name: "pages", heap: 2000 * MIB }, trip); |
| 48 | w.note(4100 * MIB, 500 * MIB, trip, { commit: 3000 * MIB, held: held(400) }); | |
| 48 | w.note(4100 * MIB, 500 * MIB, trip, { anon: 3000 * MIB, held: held(400) }); | |
| 49 | 49 | w.noteWorker({ name: "pages", heap: 2410 * MIB }, trip + MIN); |
| 50 | w.note(4200 * MIB, 500 * MIB, trip + MIN, { commit: 3100 * MIB, held: held(900) }); | |
| 50 | w.note(4200 * MIB, 500 * MIB, trip + MIN, { anon: 3100 * MIB, held: held(900) }); | |
| 51 | 51 | return { w, trip }; |
| 52 | 52 | } |
| 53 | 53 | |
| @@ -69,7 +69,7 @@ test("a memory-growth finding names the part that grew, each VM's heap and the h | ||
| 69 | 69 | expect(text).toContain("memory heap main: first hour 500 MiB, trip 500 MiB, now 500 MiB"); |
| 70 | 70 | // Outside every heap: RSS less main's and the fresh worker's heap. |
| 71 | 71 | expect(text).toContain("memory outside every heap (process): first hour 1400 MiB, trip 1600 MiB, now 1290 MiB"); |
| 72 | expect(text).toContain("memory mimalloc committed (process): first hour 1200 MiB, trip 3000 MiB, now 3100 MiB"); | |
| 72 | expect(text).toContain("memory anonymous rss (process): first hour 1200 MiB, trip 3000 MiB, now 3100 MiB"); | |
| 73 | 73 | expect(text).toContain("memory held sse: trip 400, now 900 (+500)"); |
| 74 | 74 | expect(lines.filter((l) => l.startsWith("memory held ")).length).toBe(EVIDENCE_HELD); |
| 75 | 75 | expect(lines.length).toBeLessThan(40); |
| @@ -90,7 +90,7 @@ test("another cause's finding carries no memory lines", () => { | ||
| 90 | 90 | test("a watch that never tripped says so, and lists no trip figures", () => { |
| 91 | 91 | const start = Date.now() - 2 * FIRST_HOUR_MS; |
| 92 | 92 | const w = createMemoryWatch(start); |
| 93 | for (let i = 0; i < 12; i++) w.note(1000 * MIB, 200 * MIB, start + i * 5 * MIN, { commit: 300 * MIB, held: [{ name: "sse", size: 4 }] }); | |
| 93 | for (let i = 0; i < 12; i++) w.note(1000 * MIB, 200 * MIB, start + i * 5 * MIN, { anon: 300 * MIB, held: [{ name: "sse", size: 4 }] }); | |
| 94 | 94 | const lines = w.evidence(); |
| 95 | 95 | expect(lines[0]).toBe("memory rss: first hour 1000 MiB, trip none, now 1000 MiB"); |
| 96 | 96 | expect(lines).toContain("memory held sse: trip none, now 4"); |
| @@ -103,7 +103,7 @@ test("a watch that never tripped says so, and lists no trip figures", () => { | ||
| 103 | 103 | */ |
| 104 | 104 | function settledWatch(start: number) { |
| 105 | 105 | const w = createMemoryWatch(start); |
| 106 | for (let i = 0; i <= 12; i++) w.note(1000 * MIB, 200 * MIB, start + i * 5 * MIN, { commit: 300 * MIB }); | |
| 106 | for (let i = 0; i <= 12; i++) w.note(1000 * MIB, 200 * MIB, start + i * 5 * MIN, { anon: 300 * MIB }); | |
| 107 | 107 | return w; |
| 108 | 108 | } |
| 109 | 109 |
new above
offline
The
beforestate is the new behaviour and nothing tests it. Tick the sampler twice with a fakenowand assert that the second line hasallocator churnand the first does not.