Public: anyone can read this; the crew can change it.
Disk limits: walk a cell as often as its use and growth need, from 15 s up to 120 s #16715
Check · Review · Land · Live done — Landed and live in production.
disk-cadence → main 0 commits 1 thread · 1 unresolved
3 files
changed +173
−5
scripts/unused-exports-allow.json +1
src/apps/contain.ts +51 −5
| @@ -1271,8 +1271,12 @@ function readMountinfo(): string { | ||
| 1271 | 1271 | |
| 1272 | 1272 | // ---- the watch ---- |
| 1273 | 1273 | |
| 1274 | /** How often the process table is read, and over how long CPU is averaged. */ | |
| 1275 | export const WATCH = { tickMs: 1_000, cpuWindowMs: 30_000, diskEveryTicks: 15 }; | |
| 1274 | /** | |
| 1275 | * How often the process table is read, and over how long CPU is averaged. A cell's disk is walked | |
| 1276 | * every `diskEveryTicks` while it is near its limit and as seldom as `diskMostTicks` far below it | |
| 1277 | * (`diskWalkAfter`). | |
| 1278 | */ | |
| 1279 | export const WATCH = { tickMs: 1_000, cpuWindowMs: 30_000, diskEveryTicks: 15, diskMostTicks: 120 }; | |
| 1276 | 1280 | /** |
| 1277 | 1281 | * A cell's process limit: the watch's every tick, and the kernel's `pids.max` where pids is |
| 1278 | 1282 | * delegated. |
| @@ -1289,6 +1293,9 @@ interface Watched { | ||
| 1289 | 1293 | ticks: number; |
| 1290 | 1294 | /** A disk walk is out for this cell. */ |
| 1291 | 1295 | measuring?: boolean; |
| 1296 | /** The tick the next disk walk is due at (`diskWalkAfter`), and the bytes the last walk found. */ | |
| 1297 | diskAt: number; | |
| 1298 | diskBytes?: number; | |
| 1292 | 1299 | /** |
| 1293 | 1300 | * When the cell began to be watched, and since when its cores have held over the stop line |
| 1294 | 1301 | * without a break (#11657). |
| @@ -1501,6 +1508,7 @@ export function watchTree( | ||
| 1501 | 1508 | breach, |
| 1502 | 1509 | samples: [], |
| 1503 | 1510 | ticks: 0, |
| 1511 | diskAt: WATCH.diskEveryTicks, | |
| 1504 | 1512 | startedAt: Date.now(), |
| 1505 | 1513 | overSince: null, |
| 1506 | 1514 | key: nextKey++, |
| @@ -1597,6 +1605,29 @@ export function diskBreach(walk: DiskWalk, diskMiB: number): string | undefined | ||
| 1597 | 1605 | return walk.bytes > diskMiB * 1024 * 1024 ? `wrote ${mib(walk.bytes)} to disk, over its ${diskMiB} MiB limit` : undefined; |
| 1598 | 1606 | } |
| 1599 | 1607 | |
| 1608 | /** | |
| 1609 | * Ticks until a cell's next disk walk. A walk `lstat`s every file the cell can write, and the | |
| 1610 | * watch walked each cell every 15 s whatever it held: on production 142 cells, 3.7 s of CPU a | |
| 1611 | * pass (two thirds of it in the kernel), a quarter of a core all day on the disk worker | |
| 1612 | * (2026-11-30). A cell near its limit is still walked every `diskEveryTicks`; one far below it | |
| 1613 | * waits longer, up to `diskMostTicks`, doubling with each halving of what it uses: at least half | |
| 1614 | * the limit, every 15; a quarter, 30; an eighth, 60; less, 120. A walk that hit its entry cap, | |
| 1615 | * or a cell without a limit in bytes, is walked at the floor. | |
| 1616 | * | |
| 1617 | * A cell that `grew` by an eighth of its limit or more since its last walk is walked at the floor | |
| 1618 | * too, whatever share it now holds: a small cell that has started writing fast (an install, a | |
| 1619 | * build cache, a runaway log) is watched closely until its growth stops. | |
| 1620 | */ | |
| 1621 | export function diskWalkAfter(walk: DiskWalk, diskMiB: number, grew = 0): number { | |
| 1622 | const floor = WATCH.diskEveryTicks; | |
| 1623 | const most = WATCH.diskMostTicks; | |
| 1624 | const limit = diskMiB * 1024 * 1024; | |
| 1625 | if (walk.capped || !(limit > 0) || !Number.isFinite(limit)) return floor; | |
| 1626 | if (grew >= limit / 8) return floor; | |
| 1627 | const used = walk.bytes / limit; | |
| 1628 | return used >= 1 / 2 ? floor : used >= 1 / 4 ? 2 * floor : used >= 1 / 8 ? 4 * floor : most; | |
| 1629 | } | |
| 1630 | ||
| 1600 | 1631 | /** |
| 1601 | 1632 | * `diskWalk` taken by a worker: a cell's checkout and its `node_modules` run |
| 1602 | 1633 | * to a couple of hundred thousand `lstat`s, which held the forge's one thread |
| @@ -1714,6 +1745,17 @@ export const readTreesOff: TreeReader = (asks) => | ||
| 1714 | 1745 | } |
| 1715 | 1746 | }); |
| 1716 | 1747 | |
| 1748 | /** How a cell's disk is walked: the worker's walk, unless a test hands over its own. */ | |
| 1749 | export type DiskWalker = (dirs: readonly string[], max: number) => Promise<DiskWalk>; | |
| 1750 | let diskWalker: DiskWalker | undefined; | |
| 1751 | /** | |
| 1752 | * Swap how a cell's disk is walked (a test counts the walks and when they come); undefined: the | |
| 1753 | * default. | |
| 1754 | */ | |
| 1755 | export function useDiskWalker(next: DiskWalker | undefined): void { | |
| 1756 | diskWalker = next; | |
| 1757 | } | |
| 1758 | ||
| 1717 | 1759 | let treeReader: TreeReader | undefined; |
| 1718 | 1760 | /** Swap how the watched trees are read (a test counts the readings); undefined: the default. */ |
| 1719 | 1761 | export function useTreeReader(next: TreeReader | undefined): void { |
| @@ -1806,22 +1848,26 @@ function decide(now: number, trees: readonly TreeReading[]): void { | ||
| 1806 | 1848 | continue; |
| 1807 | 1849 | } |
| 1808 | 1850 | entry.ticks++; |
| 1809 | if (entry.ticks % WATCH.diskEveryTicks === 0) { | |
| 1851 | if (entry.ticks >= entry.diskAt) { | |
| 1810 | 1852 | const over = (walk: DiskWalk): void => { |
| 1853 | const grew = entry.diskBytes === undefined ? 0 : Math.max(0, walk.bytes - entry.diskBytes); | |
| 1854 | entry.diskBytes = walk.bytes; | |
| 1855 | entry.diskAt = entry.ticks + diskWalkAfter(walk, limits.diskMiB, grew); | |
| 1811 | 1856 | const why = diskBreach(walk, limits.diskMiB); |
| 1812 | 1857 | if (why !== undefined) entry.breach(why); |
| 1813 | 1858 | }; |
| 1814 | 1859 | const most = diskEntriesMax(limits.diskMiB); |
| 1815 | if (process.env["NODE_ENV"] === "test") over(diskWalk(entry.writable, most)); | |
| 1860 | if (diskWalker === undefined && process.env["NODE_ENV"] === "test") over(diskWalk(entry.writable, most)); | |
| 1816 | 1861 | else if (entry.measuring !== true) { |
| 1817 | 1862 | entry.measuring = true; |
| 1818 | diskWalkOff(entry.writable, most).then( | |
| 1863 | (diskWalker ?? diskWalkOff)(entry.writable, most).then( | |
| 1819 | 1864 | (walk) => { |
| 1820 | 1865 | entry.measuring = false; |
| 1821 | 1866 | if (watched.has(entry)) over(walk); |
| 1822 | 1867 | }, |
| 1823 | 1868 | () => { |
| 1824 | 1869 | entry.measuring = false; |
| 1870 | entry.diskAt = entry.ticks + WATCH.diskEveryTicks; | |
| 1825 | 1871 | }, |
| 1826 | 1872 | ); |
| 1827 | 1873 | } |
test/disk-cadence.test.ts added +121
| @@ -0,0 +1,121 @@ | ||
| 1 | /** | |
| 2 | * How often a cell's disk is walked (`diskWalkAfter`): every 15 ticks near its limit, as seldom as | |
| 3 | * 120 far below it. On production 142 cells were walked every 15 s whatever they held: a quarter | |
| 4 | * of a core on the disk worker. The watch is driven here tick by tick with a stubbed walk, so the | |
| 5 | * schedule it keeps is what is tested, not only the function that picks the wait. | |
| 6 | */ | |
| 7 | import { afterEach, expect, test } from "bun:test"; | |
| 8 | import { | |
| 9 | WATCH, | |
| 10 | diskWalkAfter, | |
| 11 | useDiskWalker, | |
| 12 | useTreeReader, | |
| 13 | watchTick, | |
| 14 | watchTree, | |
| 15 | type DiskWalker, | |
| 16 | type Limits, | |
| 17 | } from "../src/apps/contain.ts"; | |
| 18 | import type { ProcessTable } from "../src/apps/tree_read.ts"; | |
| 19 | ||
| 20 | const MiB = 1024 * 1024; | |
| 21 | const walk = (mib: number, capped = false) => ({ bytes: mib * MiB, capped }); | |
| 22 | ||
| 23 | test("the floor and the ceiling are the watch's own", () => { | |
| 24 | expect(WATCH.diskEveryTicks).toBe(15); | |
| 25 | expect(WATCH.diskMostTicks).toBe(120); | |
| 26 | }); | |
| 27 | ||
| 28 | test("the wait doubles with each halving of what the cell uses", () => { | |
| 29 | expect(diskWalkAfter(walk(1024), 1024)).toBe(15); | |
| 30 | expect(diskWalkAfter(walk(512), 1024)).toBe(15); | |
| 31 | expect(diskWalkAfter(walk(511), 1024)).toBe(30); | |
| 32 | expect(diskWalkAfter(walk(256), 1024)).toBe(30); | |
| 33 | expect(diskWalkAfter(walk(255), 1024)).toBe(60); | |
| 34 | expect(diskWalkAfter(walk(128), 1024)).toBe(60); | |
| 35 | expect(diskWalkAfter(walk(127), 1024)).toBe(120); | |
| 36 | expect(diskWalkAfter(walk(0), 1024)).toBe(120); | |
| 37 | }); | |
| 38 | ||
| 39 | test("growth of an eighth of the limit or more since the last walk is walked at the floor", () => { | |
| 40 | expect(diskWalkAfter(walk(320), 1024, 310 * MiB)).toBe(15); | |
| 41 | expect(diskWalkAfter(walk(140), 1024, 128 * MiB)).toBe(15); | |
| 42 | expect(diskWalkAfter(walk(140), 1024, 127 * MiB)).toBe(60); | |
| 43 | expect(diskWalkAfter(walk(10), 1024, 0)).toBe(120); | |
| 44 | }); | |
| 45 | ||
| 46 | test("a walk that hit its entry cap, or a cell with no limit in bytes, is walked at the floor", () => { | |
| 47 | expect(diskWalkAfter(walk(1, true), 1024)).toBe(15); | |
| 48 | expect(diskWalkAfter(walk(1), 0)).toBe(15); | |
| 49 | expect(diskWalkAfter(walk(1), Number.POSITIVE_INFINITY)).toBe(15); | |
| 50 | expect(diskWalkAfter(walk(1), Number.NaN)).toBe(15); | |
| 51 | }); | |
| 52 | ||
| 53 | // ---- the watch's own schedule ---- | |
| 54 | ||
| 55 | /** A pid no kernel hands out: the watch reads it only from the table a test writes. */ | |
| 56 | const ROOT = 9_300_000; | |
| 57 | const table: ProcessTable = { rows: new Map([[ROOT, { ppid: 1, ticks: 0, kib: 1024 }]]), children: new Map() }; | |
| 58 | const limits: Limits = { memoryMiB: 512, cpus: 1, diskMiB: 1024 }; | |
| 59 | const cap = { bytes: 512 * MiB, by: "watch" as const }; | |
| 60 | const saved = { ...WATCH }; | |
| 61 | const open: (() => void)[] = []; | |
| 62 | ||
| 63 | afterEach(() => { | |
| 64 | for (const unwatch of open.splice(0)) unwatch(); | |
| 65 | useDiskWalker(undefined); | |
| 66 | useTreeReader(undefined); | |
| 67 | Object.assign(WATCH, saved); | |
| 68 | }); | |
| 69 | ||
| 70 | /** | |
| 71 | * Watch one cell whose walks answer `answer(n)` for the n-th walk (a number of MiB, or an error), | |
| 72 | * drive `ticks` ticks of the watch, and return the ticks a walk was asked at. | |
| 73 | */ | |
| 74 | async function walkedAt(ticks: number, answer: (n: number) => number | Error): Promise<number[]> { | |
| 75 | // The interval's own tick never fires inside the test: only `watchTick` drives the watch. | |
| 76 | WATCH.tickMs = 3_600_000; | |
| 77 | useTreeReader(() => new Promise(() => {})); | |
| 78 | const asked: number[] = []; | |
| 79 | let tick = 0; | |
| 80 | let pending: Promise<unknown> = Promise.resolve(); | |
| 81 | const walker: DiskWalker = () => { | |
| 82 | asked.push(tick); | |
| 83 | const said = answer(asked.length); | |
| 84 | const out = said instanceof Error ? Promise.reject(said) : Promise.resolve(walk(said)); | |
| 85 | pending = out.catch(() => {}); | |
| 86 | return out; | |
| 87 | }; | |
| 88 | useDiskWalker(walker); | |
| 89 | open.push(watchTree(ROOT, limits, cap, ["/nowhere"], undefined, () => {})); | |
| 90 | const start = Date.now(); | |
| 91 | for (tick = 1; tick <= ticks; tick++) { | |
| 92 | watchTick(start + tick * 1_000, table); | |
| 93 | // The walk's answer is settled before the next tick, as a worker's would be between ticks. | |
| 94 | await pending; | |
| 95 | } | |
| 96 | return asked; | |
| 97 | } | |
| 98 | ||
| 99 | test("a cell far under its limit is walked at tick 15, then not again until 120 ticks later", async () => { | |
| 100 | expect(await walkedAt(300, () => 10)).toEqual([15, 135, 255]); | |
| 101 | }); | |
| 102 | ||
| 103 | test("a cell whose walk finds it past half its limit is walked again 15 ticks later", async () => { | |
| 104 | // First walk: 10 MiB of 1,024, so 120 ticks; it grew to 600 MiB by the second, so 15. | |
| 105 | expect(await walkedAt(170, (n) => (n === 1 ? 10 : 600))).toEqual([15, 135, 150, 165]); | |
| 106 | }); | |
| 107 | ||
| 108 | test("a growing cell is walked more often at each halving it crosses", async () => { | |
| 109 | // 100 MiB: 120 ticks. 200 MiB (over an eighth): 60. 300 MiB (over a quarter): 30. | |
| 110 | expect(await walkedAt(230, (n) => [100, 200, 300, 300][n - 1] ?? 300)).toEqual([15, 135, 195, 225]); | |
| 111 | }); | |
| 112 | ||
| 113 | test("a cell at 1% that grows by 30% of its limit between two walks is walked again at 15 ticks, not 120", async () => { | |
| 114 | // 10 MiB of 1,024 at tick 15: 120 ticks. 317 MiB at tick 135, a burst of 307: 15, where its | |
| 115 | // share alone (31%) would say 30. At tick 150 it has stopped growing: its share's 30 from then. | |
| 116 | expect(await walkedAt(230, (n) => (n === 1 ? 10 : 317))).toEqual([15, 135, 150, 180, 210]); | |
| 117 | }); | |
| 118 | ||
| 119 | test("a walk that fails is asked again 15 ticks later, not at the far cadence", async () => { | |
| 120 | expect(await walkedAt(160, (n) => (n === 2 ? new Error("the disk worker failed") : 10))).toEqual([15, 135, 150]); | |
| 121 | }); |
new above
offline