/** * A `rust` JOB ON A 16 GB MAC MUST NOT SWAP. * * `rust` used to name one machine and two constants in the job's `env:` were * the whole answer: `CARGO_BUILD_JOBS: 3`, `NEXTEST_TEST_THREADS: 8`, both * measured on the Linux CI host (8 cores / 16 threads, 125 GB). Since the job * became a `cotton-rust` job it can land on a Mac instead, and the Macs are * 16 GB and 24 GB machines running a desktop and Xcode besides. The wrong half * of that pair is not a slow build, it is a laptop paging. * * AND A `rust` JOB ON AN IDLE CI HOST MUST NOT BE SIZED FOR A BUSY ONE. The * Linux arm's `3` and `8` were the lane's worst case — four `rust` jobs at once * — handed out unconditionally, and run 181 measured what that costs on an idle * box: 550 s of nextest wall for 4,475 test-SECONDS, which is eight threads * doing exactly as much as eight threads can. The arm now reads `nproc`, the * one-minute load and `MemAvailable` at step time, and the old constants are * its FLOOR, so a busy host still gets the pair it was measured with. * * `scripts/ci/rust-job-sizing.sh` decides both numbers from the machine it is * on and writes them to `$GITHUB_ENV`. This holds it to the shapes that * actually exist, by running it — the five env overrides are there precisely * so the Mac answers can be proven on the Linux lane and the Linux answers on a * Mac, and so that "an idle host" and "a host with three neighbours" are both * testable on whichever machine happens to run this file. A guard that could * only run on the machine it describes is a guard nobody runs. */import { spawnSync } from"node:child_process";import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from"node:fs";import { tmpdir } from"node:os";import { join } from"node:path";import { afterAll, describe, expect, it } from"vitest";const ROOT = join(__dirname, "..", "..");const SCRIPT = join(ROOT, "scripts", "ci", "rust-job-sizing.sh");const temps: string[] = [];afterAll(() => {for (const dir of temps.splice(0)) rmSync(dir, { recursive: true, force: true });});interfaceSizing { jobs: number; threads: number; stdout: string; status: number | null;}/** What the Linux arm reads off the machine at step time, on top of the above. */interfaceNow {/** The one-minute load average. */ load?: number;/** MemAvailable, in GB. */ freeGb?: number;}function size(os: string, cores: number, memGb: number | "unreadable", now: Now = {}): Sizing {const dir = mkdtempSync(join(tmpdir(), "rust-sizing-")); temps.push(dir);const env = join(dir, "github-env"); writeFileSync(env, "");const result = spawnSync("bash", [SCRIPT], { encoding: "utf8", env: { ...process.env, GITHUB_ENV: env, COTTON_RUST_SIZING_OS: os, COTTON_RUST_SIZING_CORES: String(cores), COTTON_RUST_SIZING_MEM_GB: memGb === "unreadable" ? "0" : String(memGb),// Left unset the script reads the machine it is actually on, which is a// different answer on every developer's Mac and on the lane. Every test// below that cares about the Linux arm names both. ...(now.load === undefined ? {} : { COTTON_RUST_SIZING_LOAD: String(now.load) }), ...(now.freeGb === undefined ? {} : { COTTON_RUST_SIZING_FREE_GB: String(now.freeGb) }), }, });const wrote = readFileSync(env, "utf8");const read = (name: string) => {const line = wrote.split("\n").find((l) => l.startsWith(`${name}=`)); expect(line, `${name} was not written — every cargo step after this reads it`).toBeDefined();returnNumber(line!.slice(name.length + 1)); };return { jobs: read("CARGO_BUILD_JOBS"), threads: read("NEXTEST_TEST_THREADS"), stdout: result.stdout, status: result.status };}describe("rust-job-sizing.sh", () => { it("is present and executable — the workflow runs it by path", () => { expect(existsSync(SCRIPT), `${SCRIPT} is missing`).toBe(true); });// ── THE LINUX HOST: what the machine is doing, not a constant ────────────//// The CI host is 16 hardware threads and 125 GB. `3` and `8` were the pair it// was handed unconditionally, sized for the lane's worst case — four `rust`// jobs at once. They are now the FLOOR, and an idle host gets the rest. it("gives an IDLE host the machine, not the four-jobs-at-once share", () => {const linux = size("Linux", 16, 125, { load: 0.5, freeGb: 100 });// headroom 15.5 → round(15.5 * 0.75) = 12 (at the cap), round(15.5) = 16. expect(linux.jobs).toBe(12); expect(linux.threads).toBe(16); }); it("hands a host with three other jobs on it exactly the OLD pair", () => {// Four `rust` jobs on 16 threads is a load around 12 — the case the// constants were chosen for. The floors reproduce them, so this change can// only ever widen a job and never narrow one.const busy = size("Linux", 16, 125, { load: 12, freeGb: 100 }); expect(busy.jobs).toBe(3); expect(busy.threads).toBe(8); }); it("never goes under the floors, however hammered the host is", () => {const hammered = size("Linux", 16, 125, { load: 64, freeGb: 100 }); expect(hammered.jobs).toBe(3); expect(hammered.threads).toBe(8); }); it("scales in between rather than stepping between two constants", () => {// One neighbour: headroom 8 → 6 jobs, 8 threads.const one = size("Linux", 16, 125, { load: 8, freeGb: 100 }); expect(one.jobs).toBe(6); expect(one.threads).toBe(8);// Two thirds free: headroom 11 → round(8.25) = 8 jobs, 11 threads.const two = size("Linux", 16, 125, { load: 5, freeGb: 100 }); expect(two.jobs).toBe(8); expect(two.threads).toBe(11); }); it("prints the inputs it decided from, so a log says WHY it chose them", () => {const linux = size("Linux", 16, 125, { load: 2.5, freeGb: 100 }); expect(linux.stdout).toMatch(/16 cores/); expect(linux.stdout).toMatch(/load 2\.5/); expect(linux.stdout).toMatch(/100 GB available/); }); it("treats a host whose load it cannot read as a FULL one", () => {// An unreadable machine gets the conservative old answer, never the wide// one: guessing idle here is the swap the Darwin arm exists to prevent.const blind = size("Linux", 16, 125, { load: 16, freeGb: 100 }); expect(blind.jobs).toBe(3); expect(blind.threads).toBe(8); }); it("is bounded by the memory that is AVAILABLE, not by the cores alone", () => {// An idle 16-thread host with 12 GB left: 3 GB a rustc, 1.5 GB a test// process. 12/3 = 4 jobs, 12*2/3 = 8 threads — under what the cores alone// would have sanctioned (12 and 16).const tight = size("Linux", 16, 125, { load: 0, freeGb: 12 }); expect(tight.jobs).toBe(4); expect(tight.threads).toBe(8); expect(tight.jobs * 3).toBeLessThanOrEqual(12); }); it("sizes a 16 GB Mac so the whole job fits in what is left after an 8 GB reserve", () => {const mac = size("Darwin", 10, 16);// 8 GB usable: 3 GB a build job, 1.5 GB a test thread. expect(mac.jobs).toBe(2); expect(mac.threads).toBe(5);// The load this actually sanctions has to fit, or the reserve is a fiction. expect(mac.jobs * 3 + 8).toBeLessThanOrEqual(16); }); it("gives a 24 GB Mac more, and stops at the caps rather than filling the machine", () => {const mac = size("Darwin", 10, 24); expect(mac.jobs).toBe(4); expect(mac.threads).toBe(8); }); it("never exceeds the caps, however large the machine", () => {const huge = size("Darwin", 64, 512); expect(huge.jobs).toBe(4); expect(huge.threads).toBe(8); }); it("is bounded by the CORES too, not by memory alone", () => {// A 64 GB Mac with two cores must not be told to run four rustc.const narrow = size("Darwin", 2, 64); expect(narrow.jobs).toBeLessThanOrEqual(2); expect(narrow.threads).toBeLessThanOrEqual(2); }); it("sizes a machine whose memory it cannot read as the SMALLEST one supported", () => {// Guessing high on an unreadable machine is the swap this file exists to// prevent, so an unreadable answer is treated as 16 GB.const blind = size("Darwin", 10, "unreadable");const small = size("Darwin", 10, 16); expect(blind.jobs).toBe(small.jobs); expect(blind.threads).toBe(small.threads); }); it("always writes both names, on every platform", () => {for (const os of ["Darwin", "Linux"]) {const out = size(os, 8, 16, { load: 1, freeGb: 8 }); expect(out.status, `${os} exited non-zero`).toBe(0); expect(out.jobs).toBeGreaterThanOrEqual(1); expect(out.threads).toBeGreaterThanOrEqual(2); } });});