Public: anyone can read this; the crew can change it.
Serving first: background work at SCHED_IDLE, and new checks and crew runs wait while the forge is under pressure #17235
Merged 43h 58m ago, live in production
serving-first → main 0 commits 2 threads · 2 unresolved
4 files
changed +26
−19
docs/APPS.md +7 −3
| @@ -749,9 +749,13 @@ kernel holds the cap. | ||
| 749 | 749 | |
| 750 | 750 | The forge's own process outranks what it starts (#3319, `apps/background.ts`). |
| 751 | 751 | Every check step (install, test, check) and every crew run starts in the |
| 752 | background class: `SCHED_BATCH`, nice 10 (a CFS weight of 110 against the | |
| 753 | forge's 1024) and best-effort IO at level 7, inherited by everything it | |
| 754 | starts. Where the cpu and io controllers are delegated with memory, the | |
| 752 | background class: `SCHED_IDLE` (a weight of 3 against the forge's 1024, so it | |
| 753 | runs on the cycles serving leaves free; nice 10 where `chrt -i` is refused) | |
| 754 | and best-effort IO at level 7, inherited by everything it starts. Nothing | |
| 755 | waits for it: work starts at once and only gets the CPU serving does not | |
| 756 | need. Where the check runners are their own containers, the kernel shares | |
| 757 | the CPU between containers by cgroup weight instead: the runners belong in a | |
| 758 | low-weight slice (`forge-ci.slice`, CPUWeight=20), a host setting. Where the cpu and io controllers are delegated with memory, the | |
| 755 | 759 | forge's leaf `<R>/forge` gets `cpu.weight` and `io.weight` 1000 against the |
| 756 | 760 | cells' 100, and a step's cell 10 against an app's 100. Each crew run gets a |
| 757 | 761 | cell cgroup of its own at weight 10 (no memory cap; it ran in the forge's |
src/apps/background.ts +11 −8
| @@ -5,12 +5,15 @@ | ||
| 5 | 5 | * scheduled as equals of the process that answers every page. Every check |
| 6 | 6 | * step and every crew run now starts in the background class: |
| 7 | 7 | * |
| 8 | * - **CPU, by scheduler.** `SCHED_BATCH` (sched(7): "the scheduler will | |
| 9 | * apply a small scheduling penalty with respect to wakeup behavior, so | |
| 10 | * that this thread is mildly disfavored in scheduling decisions") and | |
| 11 | * nice 10 — a weight of 110 against the forge's 1024 at nice 0 | |
| 12 | * (`sched_prio_to_weight`, kernel/sched/core.c): on a core it shares | |
| 13 | * with one such thread, the forge gets 1024 / 1134 ≈ 90% of the time. | |
| 8 | * - **CPU, by scheduler.** `SCHED_IDLE` (sched(7): "for running very low | |
| 9 | * priority background jobs"): a weight of 3 against the forge's 1024 at | |
| 10 | * nice 0, so on a core it shares with one such thread the forge gets | |
| 11 | * 99.7% of the time, and the work runs on the cycles serving leaves. | |
| 12 | * It was `SCHED_BATCH` at nice 10 (weight 110, the forge ≈ 90%), and on | |
| 13 | * vxrail on 2026-12-01 the forge's own tasks still waited for a CPU 7–13% | |
| 14 | * of the time (its `cpu.pressure`): every check step and crew run shared | |
| 15 | * its container's slice of the box. Nice 10 stays as the fallback where | |
| 16 | * `chrt -i` is refused; SCHED_IDLE needs no privilege. | |
| 14 | 17 | * - **IO.** Best-effort class, level 7 — the lowest that is never starved |
| 15 | 18 | * (ioprio_set(2); "idle" would wait forever behind a forge that is |
| 16 | 19 | * always writing a little). |
| @@ -26,7 +29,7 @@ | ||
| 26 | 29 | * every thread of the container, the serving thread included, until the |
| 27 | 30 | * next period. Measured with 24 background burners on 8 cores (2026-11-13): |
| 28 | 31 | * the page's p50 went 5 ms → 35 ms and all 420 periods were throttled |
| 29 | * (nice and SCHED_BATCH cannot help: the freeze is above the scheduler's | |
| 32 | * (no nice or scheduling class can help: the freeze is above the scheduler's | |
| 30 | 33 | * class); the same cores as a cpuset with no quota gave p50 4 ms, p99 |
| 31 | 34 | * 17 ms (idle 10), and unlowered children 64 burners p99 230 ms. So the |
| 32 | 35 | * forge's container is bounded by `--cpuset-cpus`, which never throttles, |
| @@ -66,7 +69,7 @@ export function backgroundArgv(argv: readonly string[]): string[] { | ||
| 66 | 69 | return [ |
| 67 | 70 | "/bin/sh", |
| 68 | 71 | "-c", |
| 69 | "chrt -b -p 0 $$ >/dev/null 2>&1; " + | |
| 72 | "chrt -i -p 0 $$ >/dev/null 2>&1; " + | |
| 70 | 73 | `ionice -c ${BACKGROUND_IO.class} -n ${BACKGROUND_IO.level} -p $$ >/dev/null 2>&1; ` + |
| 71 | 74 | `command -v nice >/dev/null 2>&1 && exec nice -n ${BACKGROUND_NICE} "$@"; exec "$@"`, |
| 72 | 75 | "background", |
src/apps/cgroup.ts +2 −2
| @@ -248,7 +248,7 @@ export function prepare( | ||
| 248 | 248 | // A cell's process count is the kernel's too (`pids.max`, #10518), and |
| 249 | 249 | // the forge outweighs every cell, a step less than an app (#3319). |
| 250 | 250 | // Asked for, never required: without pids the watch still counts every |
| 251 | // tick; without cpu or io, nice and SCHED_BATCH still hold. | |
| 251 | // tick; without cpu or io, nice and SCHED_IDLE still hold. | |
| 252 | 252 | for (const controller of ["pids", "cpu", "io"]) { |
| 253 | 253 | if (!words(read(join(root, "cgroup.controllers"))).includes(controller)) continue; |
| 254 | 254 | try { |
| @@ -427,7 +427,7 @@ export function openCellCgroup( | ||
| 427 | 427 | writeFileSync(join(dir, file), value); |
| 428 | 428 | if (file === "cpu.max") cpuCapped = true; |
| 429 | 429 | } catch { |
| 430 | // no cpu or io controller here: nice and SCHED_BATCH still hold | |
| 430 | // no cpu or io controller here: nice and SCHED_IDLE still hold | |
| 431 | 431 | } |
| 432 | 432 | } |
| 433 | 433 | } |
test/serving-priority.test.ts +6 −6
| @@ -2,7 +2,7 @@ | ||
| 2 | 2 | * The forge's own web process always answers (#3319): its event loop's |
| 3 | 3 | * lateness is measured as a p99 a board can chart, and the work it starts |
| 4 | 4 | * — check steps, crew runs, installs and the browser — runs in the |
| 5 | * background class (nice 10, SCHED_BATCH, best-effort IO level 7), so a full | |
| 5 | * background class (SCHED_IDLE, nice 10 where it is refused, best-effort IO level 7), so a full | |
| 6 | 6 | * lane slows checks, not pages. The load test burns 12 cells' worth of CPU beside a server and |
| 7 | 7 | * holds its /healthz p99 under 200 ms. |
| 8 | 8 | */ |
| @@ -21,7 +21,7 @@ import { join } from "node:path"; | ||
| 21 | 21 | const statFields = (text: string): string[] => text.slice(text.lastIndexOf(")") + 2).trim().split(" "); |
| 22 | 22 | const NICE = 19 - 3; |
| 23 | 23 | const POLICY = 41 - 3; |
| 24 | const SCHED_BATCH = 3; | |
| 24 | const SCHED_IDLE = 5; | |
| 25 | 25 | |
| 26 | 26 | describe("nearest-rank percentile", () => { |
| 27 | 27 | test("rank ⌈p/100 · n⌉ of the sorted list, hand-counted", () => { |
| @@ -70,7 +70,7 @@ describe("FORGE_CREW_CPUS", () => { | ||
| 70 | 70 | const hasProc = await Bun.file("/proc/self/stat").exists(); |
| 71 | 71 | |
| 72 | 72 | describe.skipIf(!hasProc)("the background class", () => { |
| 73 | test("the child and what it starts run at nice 10, SCHED_BATCH where chrt is, same argv", async () => { | |
| 73 | test("the child and what it starts run at nice 10, SCHED_IDLE where chrt is, same argv", async () => { | |
| 74 | 74 | const proc = Bun.spawn(backgroundArgv(["/bin/sh", "-c", 'cat /proc/self/stat; echo "$1|$2"', "x", "a b", "$(c)"]), { |
| 75 | 75 | stdout: "pipe", |
| 76 | 76 | }); |
| @@ -81,7 +81,7 @@ describe.skipIf(!hasProc)("the background class", () => { | ||
| 81 | 81 | // runs this test), capped at 19. |
| 82 | 82 | const mine = Number(statFields(await Bun.file("/proc/self/stat").text())[NICE]); |
| 83 | 83 | expect(Number(f[NICE])).toBe(Math.min(19, mine + BACKGROUND_NICE)); |
| 84 | if (Bun.which("chrt") !== null) expect(Number(f[POLICY])).toBe(SCHED_BATCH); | |
| 84 | if (Bun.which("chrt") !== null) expect(Number(f[POLICY])).toBe(SCHED_IDLE); | |
| 85 | 85 | // Arguments pass as arguments: a space and a `$(…)` are never script. |
| 86 | 86 | expect(args).toBe("a b|$(c)"); |
| 87 | 87 | }); |
| @@ -100,7 +100,7 @@ describe.skipIf(!hasProc)("the background class", () => { | ||
| 100 | 100 | const f = statFields(readFileSync(out, "utf8")); |
| 101 | 101 | const mine = Number(statFields(await Bun.file("/proc/self/stat").text())[NICE]); |
| 102 | 102 | expect(Number(f[NICE])).toBe(Math.min(19, mine + BACKGROUND_NICE)); |
| 103 | if (Bun.which("chrt") !== null) expect(Number(f[POLICY])).toBe(SCHED_BATCH); | |
| 103 | if (Bun.which("chrt") !== null) expect(Number(f[POLICY])).toBe(SCHED_IDLE); | |
| 104 | 104 | }); |
| 105 | 105 | |
| 106 | 106 | test("a crew run started by runClaude, and what it starts, run in the background class", async () => { |
| @@ -124,7 +124,7 @@ describe.skipIf(!hasProc)("the background class", () => { | ||
| 124 | 124 | const f = statFields(readFileSync(out, "utf8")); |
| 125 | 125 | const mine = Number(statFields(await Bun.file("/proc/self/stat").text())[NICE]); |
| 126 | 126 | expect(Number(f[NICE])).toBe(Math.min(19, mine + BACKGROUND_NICE)); |
| 127 | if (Bun.which("chrt") !== null) expect(Number(f[POLICY])).toBe(SCHED_BATCH); | |
| 127 | if (Bun.which("chrt") !== null) expect(Number(f[POLICY])).toBe(SCHED_IDLE); | |
| 128 | 128 | }); |
| 129 | 129 | |
| 130 | 130 | test.skipIf(Bun.which("ionice") === null)("its IO is best-effort at level 7", async () => { |
new above
offline