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

opened this

Merged 43h 58m ago, live in production

serving-first → main 0 commits 2 threads · 2 unresolved

  • Merged. serving-first was merged into main as b68a708.

Review

Sign in to review this change.
4 files changed +26 −19
docs/APPS.md +7 −3
@@ -749,9 +749,13 @@ kernel holds the cap.
749749
750750 The forge's own process outranks what it starts (#3319, `apps/background.ts`).
751751 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
755759 forge's leaf `<R>/forge` gets `cpu.weight` and `io.weight` 1000 against the
756760 cells' 100, and a step's cell 10 against an app's 100. Each crew run gets a
757761 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 @@
55 * scheduled as equals of the process that answers every page. Every check
66 * step and every crew run now starts in the background class:
77 *
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.
1417 * - **IO.** Best-effort class, level 7 — the lowest that is never starved
1518 * (ioprio_set(2); "idle" would wait forever behind a forge that is
1619 * always writing a little).
@@ -26,7 +29,7 @@
2629 * every thread of the container, the serving thread included, until the
2730 * next period. Measured with 24 background burners on 8 cores (2026-11-13):
2831 * 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
3033 * class); the same cores as a cpuset with no quota gave p50 4 ms, p99
3134 * 17 ms (idle 10), and unlowered children 64 burners p99 230 ms. So the
3235 * forge's container is bounded by `--cpuset-cpus`, which never throttles,
@@ -66,7 +69,7 @@ export function backgroundArgv(argv: readonly string[]): string[] {
6669 return [
6770 "/bin/sh",
6871 "-c",
69 "chrt -b -p 0 $$ >/dev/null 2>&1; " +
72 "chrt -i -p 0 $$ >/dev/null 2>&1; " +
7073 `ionice -c ${BACKGROUND_IO.class} -n ${BACKGROUND_IO.level} -p $$ >/dev/null 2>&1; ` +
7174 `command -v nice >/dev/null 2>&1 && exec nice -n ${BACKGROUND_NICE} "$@"; exec "$@"`,
7275 "background",
src/apps/cgroup.ts +2 −2
@@ -248,7 +248,7 @@ export function prepare(
248248 // A cell's process count is the kernel's too (`pids.max`, #10518), and
249249 // the forge outweighs every cell, a step less than an app (#3319).
250250 // 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.
252252 for (const controller of ["pids", "cpu", "io"]) {
253253 if (!words(read(join(root, "cgroup.controllers"))).includes(controller)) continue;
254254 try {
@@ -427,7 +427,7 @@ export function openCellCgroup(
427427 writeFileSync(join(dir, file), value);
428428 if (file === "cpu.max") cpuCapped = true;
429429 } 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
431431 }
432432 }
433433 }
test/serving-priority.test.ts +6 −6
@@ -2,7 +2,7 @@
22 * The forge's own web process always answers (#3319): its event loop's
33 * lateness is measured as a p99 a board can chart, and the work it starts
44 * — 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
66 * lane slows checks, not pages. The load test burns 12 cells' worth of CPU beside a server and
77 * holds its /healthz p99 under 200 ms.
88 */
@@ -21,7 +21,7 @@ import { join } from "node:path";
2121 const statFields = (text: string): string[] => text.slice(text.lastIndexOf(")") + 2).trim().split(" ");
2222 const NICE = 19 - 3;
2323 const POLICY = 41 - 3;
24 const SCHED_BATCH = 3;
24 const SCHED_IDLE = 5;
2525
2626 describe("nearest-rank percentile", () => {
2727 test("rank ⌈p/100 · n⌉ of the sorted list, hand-counted", () => {
@@ -70,7 +70,7 @@ describe("FORGE_CREW_CPUS", () => {
7070 const hasProc = await Bun.file("/proc/self/stat").exists();
7171
7272 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 () => {
7474 const proc = Bun.spawn(backgroundArgv(["/bin/sh", "-c", 'cat /proc/self/stat; echo "$1|$2"', "x", "a b", "$(c)"]), {
7575 stdout: "pipe",
7676 });
@@ -81,7 +81,7 @@ describe.skipIf(!hasProc)("the background class", () => {
8181 // runs this test), capped at 19.
8282 const mine = Number(statFields(await Bun.file("/proc/self/stat").text())[NICE]);
8383 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);
8585 // Arguments pass as arguments: a space and a `$(…)` are never script.
8686 expect(args).toBe("a b|$(c)");
8787 });
@@ -100,7 +100,7 @@ describe.skipIf(!hasProc)("the background class", () => {
100100 const f = statFields(readFileSync(out, "utf8"));
101101 const mine = Number(statFields(await Bun.file("/proc/self/stat").text())[NICE]);
102102 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);
104104 });
105105
106106 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", () => {
124124 const f = statFields(readFileSync(out, "utf8"));
125125 const mine = Number(statFields(await Bun.file("/proc/self/stat").text())[NICE]);
126126 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);
128128 });
129129
130130 test.skipIf(Bun.which("ionice") === null)("its IO is best-effort at level 7", async () => {

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs