Public: anyone can read this; the crew can change it.
Where the kernel cannot hold a child forge's cores, the forge pauses and resumes its processes to hold them #16244
scout/forge:scout/11559-where-the-kernel-cannot-hold-a-child-for → main 7 commits fork
5 files
changed +434
−11
docs/features/child-cpu-pausing.md added +32
| @@ -0,0 +1,32 @@ | ||
| 1 | # A child forge's cores, held by pausing where the kernel cannot | |
| 2 | ||
| 3 | A child forge's cell asks for its cores to be held (`cpuQuota`). Where the | |
| 4 | forge can write `cpu.max` in the cell's cgroup, the kernel holds them. Where it | |
| 5 | cannot (no delegated cpu controller), the forge's cell watch holds them: each | |
| 6 | tick it reads the cores the cell's process tree used, and if that is over the | |
| 7 | limit it sends the tree SIGSTOP and SIGCONT later in the same second. It | |
| 8 | never kills a cell for its cores this way (#11559). | |
| 9 | ||
| 10 | - **The boot line.** The first quota cell after the forge starts logs who holds | |
| 11 | the cores, once, and again only if that changes: | |
| 12 | `cpu: a child forge's cores are held by the kernel` or | |
| 13 | `cpu: a child forge's cores are held by the forge, by pausing`. | |
| 14 | The second line means this host gave the forge no `cpu.max`. | |
| 15 | - **What pausing means for a child's speed.** A tree that wants 3 cores under a | |
| 16 | limit of 1 runs about a third of each second and is stopped for the rest. On | |
| 17 | average it gets its cores, as it would under `cpu.max`. The difference is the | |
| 18 | shape: under the kernel the tree runs slowly but without a break, and under | |
| 19 | pausing it runs at full speed in bursts with gaps of up to about a second. A | |
| 20 | request that arrives during a gap waits for the gap to end. A tree within its | |
| 21 | limit is never paused. A tree is never stopped for the whole second, so the | |
| 22 | watch can still read it. | |
| 23 | - **When it ends.** Closing a cell continues a paused tree. A clean exit of the | |
| 24 | forge continues every tree it stopped. If the forge is killed, its cells go | |
| 25 | with it through `--die-with-parent`: SIGKILL also ends a stopped process. The | |
| 26 | forge adopts no cells when it restarts. It starts them again, and they start | |
| 27 | unpaused. | |
| 28 | - **Where.** The schedule is in `src/apps/cpu_duty.ts`. The wiring (the duty | |
| 29 | set up in `watchTree`, driven by the watch tick) is in `src/apps/contain.ts`. | |
| 30 | - Tests: `test/cpu-duty.test.ts`. It covers the schedule, a fake clock, and a | |
| 31 | `watchTree` driven tick by tick with a recording signaller: stopped and | |
| 32 | continued, never stopped for CPU, and continued on close. |
src/apps/contain.ts +66 −11
| @@ -51,6 +51,7 @@ import { | ||
| 51 | 51 | import { during } from "../perf/jobs.ts"; |
| 52 | 52 | import { isSize, SIZES, type Size } from "../spawn/sizes.ts"; |
| 53 | 53 | import { cpuWatch } from "./cpu_rule.ts"; |
| 54 | import { dutyTick, newDuty, resumeNow, type Duty, type DutyLater, type Signaller } from "./cpu_duty.ts"; | |
| 54 | 55 | |
| 55 | 56 | /** `ceiling`: a declaration was cut to the machine's maximum; `declared`: forge.json's own; `default`: neither. */ |
| 56 | 57 | export type CeilingFrom = "env" | "pod" | "default" | "size"; |
| @@ -313,6 +314,8 @@ export interface CellSpec { | ||
| 313 | 314 | * it is busy rather than stopped. |
| 314 | 315 | */ |
| 315 | 316 | readonly cpuQuota?: boolean | undefined; |
| 317 | /** Where the signals of the pausing fallback go; tests only. */ | |
| 318 | readonly signal?: Signaller | undefined; | |
| 316 | 319 | /** |
| 317 | 320 | * Disk bandwidth the kernel holds the cell to (`io.max`) on the disk |
| 318 | 321 | * under `dir` — the forge's data volume — where io is delegated. A child |
| @@ -326,8 +329,12 @@ export interface CellSpec { | ||
| 326 | 329 | * does not already hold them. A cell under `cpu.max` cannot run over its |
| 327 | 330 | * cores, and a sampled 4.1 of 4 is the sampling, not the cell. |
| 328 | 331 | */ |
| 329 | export function watchedLimits(limits: Limits, cgroup: { readonly cpuCapped: boolean } | undefined): Limits { | |
| 330 | return cgroup?.cpuCapped === true ? { ...limits, cpus: Number.POSITIVE_INFINITY } : limits; | |
| 332 | export function watchedLimits( | |
| 333 | limits: Limits, | |
| 334 | cgroup: { readonly cpuCapped: boolean } | undefined, | |
| 335 | pausing = false, | |
| 336 | ): Limits { | |
| 337 | return cgroup?.cpuCapped === true || pausing ? { ...limits, cpus: Number.POSITIVE_INFINITY } : limits; | |
| 331 | 338 | } |
| 332 | 339 | |
| 333 | 340 | export interface Cell { |
| @@ -633,6 +640,22 @@ export function cellCgroup( | ||
| 633 | 640 | ); |
| 634 | 641 | } |
| 635 | 642 | |
| 643 | const cpuHolder: { said: string | undefined } = { said: undefined }; | |
| 644 | ||
| 645 | /** | |
| 646 | * Who holds a quota cell's cores: said once per process, and again only if | |
| 647 | * it changes. `forget` starts over (tests). | |
| 648 | */ | |
| 649 | export function noteCpuHolder(kernel: boolean, log: (line: string) => void, forget = false): string { | |
| 650 | if (forget) cpuHolder.said = undefined; | |
| 651 | const who = kernel ? "the kernel" : "the forge, by pausing"; | |
| 652 | if (cpuHolder.said !== who) { | |
| 653 | cpuHolder.said = who; | |
| 654 | log(`cpu: a child forge's cores are held by ${who}`); | |
| 655 | } | |
| 656 | return who; | |
| 657 | } | |
| 658 | ||
| 636 | 659 | /** Where the next pinned cell's cores begin; `affinityArgv` advances it. */ |
| 637 | 660 | export interface CoreRotor { |
| 638 | 661 | next: number; |
| @@ -730,6 +753,7 @@ export function openCell( | ||
| 730 | 753 | const SMALL = String(1 << 20); |
| 731 | 754 | |
| 732 | 755 | const cgroup = cellCgroup(spec); |
| 756 | if (spec.cpuQuota === true) noteCpuHolder(cgroup?.cpuCapped === true, log); | |
| 733 | 757 | const argv = (command: string): string[] => cellArgv(bwrapArgv(command), spec, cgroup); |
| 734 | 758 | const rlimits = cellRlimits(spec.limits); |
| 735 | 759 | const bwrapArgv = (command: string): string[] => [ |
| @@ -795,12 +819,21 @@ export function openCell( | ||
| 795 | 819 | cgroup === undefined |
| 796 | 820 | ? { bytes: Math.floor(spec.limits.memoryMiB * 1024 * 1024), by: "watch" } |
| 797 | 821 | : { bytes: cgroup.maxBytes, by: "kernel" }; |
| 798 | unwatch = watchTree(pid, watchedLimits(spec.limits, cgroup), cap, writable, spec.label, (why) => { | |
| 799 | if (breach !== undefined) return; | |
| 800 | breach = why; | |
| 801 | log(`stopped: ${why}`); | |
| 802 | stop(why); | |
| 803 | }); | |
| 822 | const paused = spec.cpuQuota === true && cgroup?.cpuCapped !== true; | |
| 823 | unwatch = watchTree( | |
| 824 | pid, | |
| 825 | watchedLimits(spec.limits, cgroup, paused), | |
| 826 | cap, | |
| 827 | writable, | |
| 828 | spec.label, | |
| 829 | (why) => { | |
| 830 | if (breach !== undefined) return; | |
| 831 | breach = why; | |
| 832 | log(`stopped: ${why}`); | |
| 833 | stop(why); | |
| 834 | }, | |
| 835 | paused ? { cores: spec.limits.cpus, ...(spec.signal === undefined ? {} : { signal: spec.signal }) } : undefined, | |
| 836 | ); | |
| 804 | 837 | }; |
| 805 | 838 | return { |
| 806 | 839 | argv, |
| @@ -957,6 +990,11 @@ interface Watched { | ||
| 957 | 990 | overSince: number | null; |
| 958 | 991 | readonly key: number; |
| 959 | 992 | readonly label: string | undefined; |
| 993 | /** Set where the watch holds the cores by pausing the tree (`apps/cpu_duty.ts`). */ | |
| 994 | readonly duty?: Duty | undefined; | |
| 995 | readonly signal?: Signaller | undefined; | |
| 996 | /** How the resume is scheduled; the real timer when unset (tests step a clock). */ | |
| 997 | readonly later?: DutyLater | undefined; | |
| 960 | 998 | /** The memory cap, and who holds it: the kernel (a cgroup) or this watch alone. */ |
| 961 | 999 | readonly cap: MemoryCap; |
| 962 | 1000 | /** The tree's CPU ticks so far and resident KiB, at the last tick. */ |
| @@ -1129,6 +1167,7 @@ export function watchTree( | ||
| 1129 | 1167 | writable: readonly string[], |
| 1130 | 1168 | label: string | undefined, |
| 1131 | 1169 | breach: (why: string) => void, |
| 1170 | pause?: { readonly cores: number; readonly signal?: Signaller; readonly later?: DutyLater }, | |
| 1132 | 1171 | ): () => void { |
| 1133 | 1172 | const entry: Watched = { |
| 1134 | 1173 | root, |
| @@ -1142,10 +1181,14 @@ export function watchTree( | ||
| 1142 | 1181 | key: nextKey++, |
| 1143 | 1182 | label, |
| 1144 | 1183 | cap, |
| 1184 | ...(pause === undefined ? {} : { duty: newDuty(pause.cores), signal: pause.signal, later: pause.later }), | |
| 1145 | 1185 | }; |
| 1186 | const signal = pause?.signal ?? signalTree; | |
| 1146 | 1187 | watched.add(entry); |
| 1147 | 1188 | timer ??= setInterval(() => during("cell-watch", tick), WATCH.tickMs); |
| 1148 | 1189 | return () => { |
| 1190 | // A paused tree is always resumed when the watch ends. | |
| 1191 | if (entry.duty !== undefined) resumeNow(entry.duty, root, signal); | |
| 1149 | 1192 | watched.delete(entry); |
| 1150 | 1193 | if (watched.size === 0 && timer !== undefined) { |
| 1151 | 1194 | clearInterval(timer); |
| @@ -1164,7 +1207,7 @@ export function watchLabelled(pid: number, label: string): () => void { | ||
| 1164 | 1207 | return watchTree(pid, unlimited, { bytes: Infinity, by: "watch" }, [], label, () => {}); |
| 1165 | 1208 | } |
| 1166 | 1209 | |
| 1167 | interface ProcessRow { | |
| 1210 | export interface ProcessRow { | |
| 1168 | 1211 | ppid: number; |
| 1169 | 1212 | ticks: number; |
| 1170 | 1213 | } |
| @@ -1334,9 +1377,20 @@ export function cpuSample( | ||
| 1334 | 1377 | } |
| 1335 | 1378 | |
| 1336 | 1379 | function tick(): void { |
| 1380 | watchTick(); | |
| 1381 | } | |
| 1382 | ||
| 1383 | /** | |
| 1384 | * One tick of the watch over every watched cell, at `now` against `table`. | |
| 1385 | * The interval calls it with the clock and `/proc`; tests drive it with a | |
| 1386 | * table and a clock of their own. | |
| 1387 | */ | |
| 1388 | export function watchTick( | |
| 1389 | now = Date.now(), | |
| 1390 | table?: { rows: Map<number, ProcessRow>; children: Map<number, number[]> }, | |
| 1391 | ): void { | |
| 1337 | 1392 | if (watched.size === 0) return; |
| 1338 | const { rows, children } = processTable(); | |
| 1339 | const now = Date.now(); | |
| 1393 | const { rows, children } = table ?? processTable(); | |
| 1340 | 1394 | for (const entry of [...watched]) { |
| 1341 | 1395 | const tree: number[] = []; |
| 1342 | 1396 | const walk = (pid: number): void => { |
| @@ -1360,6 +1414,7 @@ function tick(): void { | ||
| 1360 | 1414 | ticks += rows.get(pid)?.ticks ?? 0; |
| 1361 | 1415 | } |
| 1362 | 1416 | entry.last = { ticks, kib }; |
| 1417 | if (entry.duty !== undefined) dutyTick(entry.duty, entry.root, ticks, now, CLK_TCK, entry.signal ?? signalTree, entry.later); | |
| 1363 | 1418 | if (kib > limits.memoryMiB * 1024) { |
| 1364 | 1419 | entry.breach( |
| 1365 | 1420 | `used ${mib(kib * 1024)} of memory, over its ${limits.memoryMiB} MiB limit (${memorySource(limits.memorySource, limits.ceilingFrom)})`, |
src/apps/cpu_duty.ts added +139
| @@ -0,0 +1,139 @@ | ||
| 1 | /** | |
| 2 | * Holding a cell's cores without the kernel: pause its process tree for part | |
| 3 | * of each second. Plain data in (the cores the tree used over the last | |
| 4 | * second, the limit), plain data out (how long to pause it this second). The | |
| 5 | * watch in `contain.ts` sends the signals (SIGSTOP, then SIGCONT, never a | |
| 6 | * kill) where a cell asked for `cpuQuota` and `cpu.max` was not ours to write. | |
| 7 | */ | |
| 8 | ||
| 9 | import { during } from "../perf/jobs.ts"; | |
| 10 | ||
| 11 | export const DUTY_SECOND_MS = 1_000; | |
| 12 | ||
| 13 | /** | |
| 14 | * Milliseconds to pause the tree this second. `cores` is what it would use | |
| 15 | * running the whole second (its demand): a tree that wants 3 cores under a | |
| 16 | * limit of 1 runs a third of the time, and so is paused for two thirds. | |
| 17 | * Never the whole second: the tree always runs a little, so it can be read. | |
| 18 | */ | |
| 19 | export function pauseMs(cores: number, limit: number, second = DUTY_SECOND_MS): number { | |
| 20 | if (!Number.isFinite(cores) || !Number.isFinite(limit) || limit <= 0 || cores <= limit) return 0; | |
| 21 | const run = Math.max(MIN_RUN_FRACTION, limit / cores); | |
| 22 | return Math.round(second * (1 - run)); | |
| 23 | } | |
| 24 | ||
| 25 | const MIN_RUN_FRACTION = 0.02; | |
| 26 | ||
| 27 | /** A stalled tick never earns a pause longer than this. */ | |
| 28 | const MAX_PAUSE_SPAN_MS = 2_000; | |
| 29 | ||
| 30 | /** | |
| 31 | * The tree's demand from what it used: cores measured over a second in which | |
| 32 | * it was paused for `pausedMs` ran only for the rest, so its demand is the | |
| 33 | * reading over that running share. | |
| 34 | */ | |
| 35 | export function demandCores(usedCores: number, pausedMs: number, second = DUTY_SECOND_MS): number { | |
| 36 | const running = Math.max(MIN_RUN_FRACTION, 1 - pausedMs / second); | |
| 37 | return Math.max(0, usedCores) / running; | |
| 38 | } | |
| 39 | ||
| 40 | /** How a resume is scheduled: `fn` after `ms`. */ | |
| 41 | export type DutyLater = (fn: () => void, ms: number) => ReturnType<typeof setTimeout>; | |
| 42 | ||
| 43 | /** What the watch sends: to the cell's tree, a signal. */ | |
| 44 | export type Signaller = (root: number, signal: "SIGSTOP" | "SIGCONT") => void; | |
| 45 | ||
| 46 | /** | |
| 47 | * One cell's pause state: its limit, how long it was paused last interval | |
| 48 | * (of `span` ms), and the resume that is owed. | |
| 49 | */ | |
| 50 | export interface Duty { | |
| 51 | readonly limit: number; | |
| 52 | paused: number; | |
| 53 | span: number; | |
| 54 | resume: ReturnType<typeof setTimeout> | undefined; | |
| 55 | /** The tree's CPU ticks and the time (ms) at the last reading. */ | |
| 56 | last: { ticks: number; at: number } | undefined; | |
| 57 | } | |
| 58 | ||
| 59 | export function newDuty(limit: number): Duty { | |
| 60 | return { limit, paused: 0, span: DUTY_SECOND_MS, resume: undefined, last: undefined }; | |
| 61 | } | |
| 62 | ||
| 63 | /** Every tree stopped right now, so a clean exit of the forge can continue them. */ | |
| 64 | const stopped = new Map<Duty, { root: number; signal: Signaller }>(); | |
| 65 | let exitHooked = false; | |
| 66 | ||
| 67 | /** | |
| 68 | * Continue every tree this process stopped. Runs on exit; a killed forge is | |
| 69 | * covered by the cell's `--die-with-parent`. | |
| 70 | */ | |
| 71 | export function resumeAllPaused(): void { | |
| 72 | for (const [duty, { root, signal }] of [...stopped]) resumeNow(duty, root, signal); | |
| 73 | } | |
| 74 | ||
| 75 | /** | |
| 76 | * One step: from the cores used over the last `span` ms (the measured | |
| 77 | * interval, not an assumed second), pause the tree now and resume it after | |
| 78 | * `pauseMs`. Returns the pause chosen. | |
| 79 | */ | |
| 80 | export function dutyStep( | |
| 81 | duty: Duty, | |
| 82 | root: number, | |
| 83 | usedCores: number, | |
| 84 | signal: Signaller, | |
| 85 | later: DutyLater = (fn, ms) => | |
| 86 | setTimeout(() => during("cell-duty-resume", fn), ms), | |
| 87 | span = DUTY_SECOND_MS, | |
| 88 | ): number { | |
| 89 | resumeNow(duty, root, signal); | |
| 90 | const ms = pauseMs(demandCores(usedCores, duty.paused, duty.span), duty.limit, span); | |
| 91 | duty.paused = ms; | |
| 92 | duty.span = span; | |
| 93 | if (ms > 0) { | |
| 94 | signal(root, "SIGSTOP"); | |
| 95 | stopped.set(duty, { root, signal }); | |
| 96 | if (!exitHooked) { | |
| 97 | exitHooked = true; | |
| 98 | process.once("exit", resumeAllPaused); | |
| 99 | } | |
| 100 | duty.resume = later(() => { | |
| 101 | duty.resume = undefined; | |
| 102 | stopped.delete(duty); | |
| 103 | signal(root, "SIGCONT"); | |
| 104 | }, ms); | |
| 105 | (duty.resume as { unref?: () => void }).unref?.(); | |
| 106 | } | |
| 107 | return ms; | |
| 108 | } | |
| 109 | ||
| 110 | /** | |
| 111 | * A watch tick: the tree's CPU `ticks` (of `clkTck` a second) at `now` ms. | |
| 112 | * The first reading only sets the baseline; each later one steps the duty | |
| 113 | * over the interval between them. Returns the pause chosen, or undefined. | |
| 114 | */ | |
| 115 | export function dutyTick( | |
| 116 | duty: Duty, | |
| 117 | root: number, | |
| 118 | ticks: number, | |
| 119 | now: number, | |
| 120 | clkTck: number, | |
| 121 | signal: Signaller, | |
| 122 | later?: DutyLater, | |
| 123 | ): number | undefined { | |
| 124 | const before = duty.last; | |
| 125 | duty.last = { ticks, at: now }; | |
| 126 | if (before === undefined || now <= before.at) return undefined; | |
| 127 | const span = now - before.at; | |
| 128 | const used = (ticks - before.ticks) / clkTck / (span / 1000); | |
| 129 | return dutyStep(duty, root, used, signal, later, Math.min(span, MAX_PAUSE_SPAN_MS)); | |
| 130 | } | |
| 131 | ||
| 132 | /** Resume the tree if it is paused: the close of a cell, and the start of every step. */ | |
| 133 | export function resumeNow(duty: Duty, root: number, signal: Signaller): void { | |
| 134 | if (duty.resume === undefined) return; | |
| 135 | clearTimeout(duty.resume); | |
| 136 | duty.resume = undefined; | |
| 137 | stopped.delete(duty); | |
| 138 | signal(root, "SIGCONT"); | |
| 139 | } |
test/cpu-duty.test.ts added +196
| @@ -0,0 +1,196 @@ | ||
| 1 | import { expect, test } from "bun:test"; | |
| 2 | import { dutyStep, dutyTick, demandCores, newDuty, pauseMs, resumeAllPaused, resumeNow } from "../src/apps/cpu_duty.ts"; | |
| 3 | import { noteCpuHolder, watchTick, watchTree, watchedLimits, type ProcessRow } from "../src/apps/contain.ts"; | |
| 4 | ||
| 5 | test("under the limit is never paused", () => { | |
| 6 | expect(pauseMs(0.5, 1)).toBe(0); | |
| 7 | expect(pauseMs(1, 1)).toBe(0); | |
| 8 | expect(pauseMs(Number.NaN, 1)).toBe(0); | |
| 9 | }); | |
| 10 | ||
| 11 | test("a 3-core load under a 1-core limit is held near 1 core", () => { | |
| 12 | let paused = 0; | |
| 13 | const used: number[] = []; | |
| 14 | for (let second = 0; second < 10; second++) { | |
| 15 | // The tree wants 3 cores while it runs; it runs for what is not paused. | |
| 16 | const cores = 3 * (1 - paused / 1000); | |
| 17 | used.push(cores); | |
| 18 | paused = pauseMs(demandCores(cores, paused), 1); | |
| 19 | } | |
| 20 | for (const c of used.slice(2)) expect(Math.abs(c - 1)).toBeLessThan(0.05); | |
| 21 | }); | |
| 22 | ||
| 23 | test("it always leaves the tree some time to run", () => { | |
| 24 | expect(pauseMs(10_000, 1)).toBeLessThan(1000); | |
| 25 | }); | |
| 26 | ||
| 27 | test("a step pauses with SIGSTOP and resumes with SIGCONT, and close resumes a paused tree", () => { | |
| 28 | const sent: string[] = []; | |
| 29 | const duty = newDuty(1); | |
| 30 | const timers: (() => void)[] = []; | |
| 31 | const later = (fn: () => void) => (timers.push(fn), setTimeout(() => {}, 0)); | |
| 32 | const ms = dutyStep(duty, 7, 3, (_r, s) => sent.push(s), later); | |
| 33 | expect(ms).toBe(667); | |
| 34 | expect(sent).toEqual(["SIGSTOP"]); | |
| 35 | resumeNow(duty, 7, (_r, s) => sent.push(s)); | |
| 36 | expect(sent).toEqual(["SIGSTOP", "SIGCONT"]); | |
| 37 | resumeNow(duty, 7, (_r, s) => sent.push(s)); | |
| 38 | expect(sent).toEqual(["SIGSTOP", "SIGCONT"]); | |
| 39 | }); | |
| 40 | ||
| 41 | /** A fake clock: timers fire only when the test advances time. */ | |
| 42 | function fakeClock() { | |
| 43 | let now = 0; | |
| 44 | const due: { at: number; fn: () => void }[] = []; | |
| 45 | const later = (fn: () => void, ms: number) => { | |
| 46 | const h = { at: now + ms, fn }; | |
| 47 | due.push(h); | |
| 48 | return h as unknown as ReturnType<typeof setTimeout>; | |
| 49 | }; | |
| 50 | const advance = (ms: number) => { | |
| 51 | now += ms; | |
| 52 | for (const h of due.filter((d) => d.at <= now)) { | |
| 53 | due.splice(due.indexOf(h), 1); | |
| 54 | h.fn(); | |
| 55 | } | |
| 56 | }; | |
| 57 | return { later, advance, now: () => now }; | |
| 58 | } | |
| 59 | ||
| 60 | test("ticks drive pause and resume: a busy tree is stopped, continued, and ends continued", () => { | |
| 61 | const clock = fakeClock(); | |
| 62 | const sent: string[] = []; | |
| 63 | const duty = newDuty(0.1); | |
| 64 | const signal = (_r: number, s: string) => sent.push(s); | |
| 65 | const CLK = 100; | |
| 66 | let ticks = 0; | |
| 67 | expect(dutyTick(duty, 1, ticks, clock.now(), CLK, signal, clock.later)).toBeUndefined(); | |
| 68 | for (let i = 0; i < 3; i++) { | |
| 69 | clock.advance(1000); | |
| 70 | ticks += 100; // one core while running | |
| 71 | dutyTick(duty, 1, ticks, clock.now(), CLK, signal, clock.later); | |
| 72 | expect(sent.at(-1)).toBe("SIGSTOP"); | |
| 73 | clock.advance(duty.paused); | |
| 74 | expect(sent.at(-1)).toBe("SIGCONT"); | |
| 75 | } | |
| 76 | expect(sent.filter((s) => s === "SIGSTOP").length).toBe(3); | |
| 77 | }); | |
| 78 | ||
| 79 | test("the second is the measured interval, not an assumed 1000 ms", () => { | |
| 80 | const duty = newDuty(1); | |
| 81 | const clock = fakeClock(); | |
| 82 | // 3 cores for 500 ms: paused for two thirds of 500 ms, not of 1000. | |
| 83 | dutyTick(duty, 1, 0, 0, 100, () => {}, clock.later); | |
| 84 | expect(dutyTick(duty, 1, 150, 500, 100, () => {}, clock.later)).toBe(333); | |
| 85 | }); | |
| 86 | ||
| 87 | test("a stalled tick never earns a pause longer than two seconds", () => { | |
| 88 | const duty = newDuty(1); | |
| 89 | const clock = fakeClock(); | |
| 90 | dutyTick(duty, 1, 0, 0, 100, () => {}, clock.later); | |
| 91 | expect(dutyTick(duty, 1, 3000, 10_000, 100, () => {}, clock.later)!).toBeLessThanOrEqual(2000); | |
| 92 | }); | |
| 93 | ||
| 94 | test("a clean exit continues every tree still stopped", () => { | |
| 95 | const clock = fakeClock(); | |
| 96 | const sent: string[] = []; | |
| 97 | const duty = newDuty(1); | |
| 98 | dutyStep(duty, 9, 3, (_r, s) => sent.push(s), clock.later); | |
| 99 | expect(sent).toEqual(["SIGSTOP"]); | |
| 100 | resumeAllPaused(); | |
| 101 | expect(sent).toEqual(["SIGSTOP", "SIGCONT"]); | |
| 102 | resumeAllPaused(); | |
| 103 | expect(sent).toEqual(["SIGSTOP", "SIGCONT"]); | |
| 104 | }); | |
| 105 | ||
| 106 | test("a paused cell is not stopped for CPU; a plain one over its cores still is", () => { | |
| 107 | const limits = { memoryMiB: 512, cpus: 1, diskMiB: 100 }; | |
| 108 | expect(watchedLimits(limits, undefined, true).cpus).toBe(Number.POSITIVE_INFINITY); | |
| 109 | expect(watchedLimits(limits, undefined, false).cpus).toBe(1); | |
| 110 | expect(watchedLimits(limits, { cpuCapped: false }).cpus).toBe(1); | |
| 111 | }); | |
| 112 | ||
| 113 | test("the cpu holder is said once, and again only when it changes", () => { | |
| 114 | const lines: string[] = []; | |
| 115 | const say = (l: string) => lines.push(l); | |
| 116 | noteCpuHolder(false, say, true); | |
| 117 | noteCpuHolder(false, say); | |
| 118 | noteCpuHolder(false, say); | |
| 119 | expect(lines).toEqual(["cpu: a child forge's cores are held by the forge, by pausing"]); | |
| 120 | noteCpuHolder(true, say); | |
| 121 | noteCpuHolder(true, say); | |
| 122 | expect(lines.length).toBe(2); | |
| 123 | expect(lines[1]).toContain("the kernel"); | |
| 124 | }); | |
| 125 | ||
| 126 | /** A pid no kernel hands out (over `pid_max`), so only the test's table holds it. */ | |
| 127 | const FAKE_ROOT = 9_999_999; | |
| 128 | ||
| 129 | /** A table with one process, the root, at `ticks` CPU ticks. */ | |
| 130 | function table(ticks: number): { rows: Map<number, ProcessRow>; children: Map<number, number[]> } { | |
| 131 | return { rows: new Map([[FAKE_ROOT, { ppid: 1, ticks }]]), children: new Map() }; | |
| 132 | } | |
| 133 | ||
| 134 | /** | |
| 135 | * Drive `watchTree` through `watchTick` for `minutes` of a tree that runs 3 | |
| 136 | * cores flat out (the recording signaller never really stops it), stepping | |
| 137 | * a stepped clock. Returns the signals sent and the breaches. | |
| 138 | */ | |
| 139 | function busyWatch(paused: boolean) { | |
| 140 | const clock = fakeClock(); | |
| 141 | const sent: string[] = []; | |
| 142 | const breaches: string[] = []; | |
| 143 | const base = { memoryMiB: 512, cpus: 1, diskMiB: 100 }; | |
| 144 | const limits = watchedLimits(base, undefined, paused); | |
| 145 | const cap = { bytes: 512 * 1024 * 1024, by: "watch" as const }; | |
| 146 | const unwatch = watchTree( | |
| 147 | FAKE_ROOT, | |
| 148 | limits, | |
| 149 | cap, | |
| 150 | [], | |
| 151 | undefined, | |
| 152 | (why) => breaches.push(why), | |
| 153 | paused ? { cores: 1, signal: (_r, s) => sent.push(s), later: clock.later } : undefined, | |
| 154 | ); | |
| 155 | const start = Date.now(); | |
| 156 | let at = start; | |
| 157 | let ticks = 0; | |
| 158 | watchTick(at, table(ticks)); | |
| 159 | // A short first step, so the resume timer itself is seen to fire. | |
| 160 | at += 200; | |
| 161 | ticks += 3 * 20; | |
| 162 | watchTick(at, table(ticks)); | |
| 163 | if (paused) { | |
| 164 | expect(sent).toEqual(["SIGSTOP"]); | |
| 165 | clock.advance(250); | |
| 166 | expect(sent).toEqual(["SIGSTOP", "SIGCONT"]); | |
| 167 | } | |
| 168 | // Past the grace and the hold (`apps/cpu_rule.ts`): 16 minutes at 3 cores. | |
| 169 | while (at - start < 16 * 60_000 && breaches.length === 0) { | |
| 170 | at += 2_000; | |
| 171 | ticks += 3 * 200; | |
| 172 | watchTick(at, table(ticks)); | |
| 173 | } | |
| 174 | const beforeClose = [...sent]; | |
| 175 | unwatch(); | |
| 176 | return { sent, beforeClose, breaches }; | |
| 177 | } | |
| 178 | ||
| 179 | test("watchTree with pause: ticks stop and continue the tree, never stop it for CPU, and close continues it", () => { | |
| 180 | const { sent, beforeClose, breaches } = busyWatch(true); | |
| 181 | expect(breaches).toEqual([]); | |
| 182 | const stops = sent.filter((s) => s === "SIGSTOP").length; | |
| 183 | expect(stops).toBeGreaterThan(400); | |
| 184 | // Every stop is followed by its continue; the last one is sent by the close. | |
| 185 | expect(sent.filter((s) => s === "SIGCONT").length).toBe(stops); | |
| 186 | expect(beforeClose.at(-1)).toBe("SIGSTOP"); | |
| 187 | expect(sent.at(-1)).toBe("SIGCONT"); | |
| 188 | for (let i = 0; i < sent.length; i++) expect(sent[i]).toBe(i % 2 === 0 ? "SIGSTOP" : "SIGCONT"); | |
| 189 | }); | |
| 190 | ||
| 191 | test("watchTree without pause: the same load is stopped for CPU and never signalled", () => { | |
| 192 | const { sent, breaches } = busyWatch(false); | |
| 193 | expect(sent).toEqual([]); | |
| 194 | expect(breaches.length).toBe(1); | |
| 195 | expect(breaches[0]).toContain("core"); | |
| 196 | }); |
test/job-slices.test.ts +1
| @@ -251,6 +251,7 @@ const UNTIMED: Record<string, string> = { | ||
| 251 | 251 | "cell-watch": "watches running app cells' cgroups; needs cells, skipped in this sandbox mode", |
| 252 | 252 | "cell-probe-deadline": "a cell's start probe running out of time: needs cells, skipped in this sandbox mode", |
| 253 | 253 | "cell-kill-root": "kills a cell's root process after its grace: needs cells, skipped in this sandbox mode", |
| 254 | "cell-duty-resume": "sends SIGCONT to a cell tree the duty pause stopped: needs cells, skipped in this sandbox mode", | |
| 254 | 255 | "apps-restart": "restarts an app process after it exits; the fixture has no apps", |
| 255 | 256 | "app-tty-idle": "closes an app terminal left idle; the fixture has no app terminals", |
| 256 | 257 | "route-timing-flush": "a debounced write of route timings: runs after requests, the jobs here make none", |
new above
offline