Public: anyone can read this; the crew can change it.
A check never trips its own CPU guard: test workers follow the cell's CPU limit, not the host's #2255
scout/forge:scout/2243-a-check-never-trips-its-own-cpu-guard-te → main 1 commit fork
4 files
changed +37
−1
scripts/shards.ts +14
| @@ -53,6 +53,20 @@ export function shardCount( | ||
| 53 | 53 | return { ok: true, value: Math.max(1, Math.min(n, files)) }; |
| 54 | 54 | } |
| 55 | 55 | |
| 56 | /** | |
| 57 | * The CPUs the shards may use: the host's, or inside a cell the cell's limit | |
| 58 | * (`FORGE_CELL_CPUS`, set by the check from forge.json `check.limits.cpus`) | |
| 59 | * when that is lower, minus one for the runner itself. `availableParallelism()` | |
| 60 | * inside a cell can report the host's CPUs, and more busy workers than the | |
| 61 | * cell's limit gets the check killed by its CPU guard (#2243). Not a whole | |
| 62 | * number from 1 is no limit; at least one. | |
| 63 | */ | |
| 64 | export function shardCpus(available: number, cellLimit?: string): number { | |
| 65 | const text = cellLimit?.trim() ?? ""; | |
| 66 | if (!/^[1-9][0-9]{0,4}$/.test(text)) return Math.max(1, Math.floor(available)); | |
| 67 | return Math.max(1, Math.min(Math.floor(available), Number(text) - 1)); | |
| 68 | } | |
| 69 | ||
| 56 | 70 | /** |
| 57 | 71 | * The memory a process may use: the least of the machine's and each |
| 58 | 72 | * cgroup limit it can read (`memory.max` under v2, `memory.limit_in_bytes` |
scripts/test-shards.ts +2 −1
| @@ -53,6 +53,7 @@ import { | ||
| 53 | 53 | SHARDS_MAX, |
| 54 | 54 | WORKER_MEMORY_BYTES, |
| 55 | 55 | shardCount, |
| 56 | shardCpus, | |
| 56 | 57 | sliceFiles, |
| 57 | 58 | splitArgs, |
| 58 | 59 | splitFiles, |
| @@ -339,7 +340,7 @@ const readLimit = (path: string): string | undefined => { | ||
| 339 | 340 | return undefined; |
| 340 | 341 | } |
| 341 | 342 | }; |
| 342 | const cpus = availableParallelism(); | |
| 343 | const cpus = shardCpus(availableParallelism(), process.env["FORGE_CELL_CPUS"]); | |
| 343 | 344 | const shardsOverride = process.env["FORGE_CHECK_SHARDS"]; |
| 344 | 345 | const memory = memoryLimit(totalmem(), [ |
| 345 | 346 | readLimit("/sys/fs/cgroup/memory.max"), |
src/checks/service.ts +2
| @@ -792,6 +792,8 @@ export function startChecks(app: App, options: ChecksOptions): ChecksHandle { | ||
| 792 | 792 | FORGE_CHECK: run.id, |
| 793 | 793 | FORGE_COMMIT: x.commitOid, |
| 794 | 794 | FORGE_CHECK_IMPACT: IMPACT_FILE, |
| 795 | // The cell's CPU limit, so test workers follow it, not the host's (#2243). | |
| 796 | FORGE_CELL_CPUS: String(resolveLimits("step", manifest.limits).cpus), | |
| 795 | 797 | ...(base === undefined |
| 796 | 798 | ? {} |
| 797 | 799 | : { |
test/shards.test.ts +19
| @@ -19,6 +19,7 @@ import { | ||
| 19 | 19 | refreshTimes, |
| 20 | 20 | parseShardSpec, |
| 21 | 21 | shardCount, |
| 22 | shardCpus, | |
| 22 | 23 | sliceFiles, |
| 23 | 24 | splitArgs, |
| 24 | 25 | splitFiles, |
| @@ -611,3 +612,21 @@ describe("one file per worker, timed", () => { | ||
| 611 | 612 | ); |
| 612 | 613 | }); |
| 613 | 614 | }); |
| 615 | ||
| 616 | describe("shardCpus", () => { | |
| 617 | test("a cell's CPU limit bounds the workers on a many-core host, one left for the runner", () => { | |
| 618 | expect(shardCpus(80, "4")).toBe(3); | |
| 619 | const n = shardCount(shardCpus(80, "4"), 400); | |
| 620 | expect(n).toEqual({ ok: true, value: 3 }); | |
| 621 | }); | |
| 622 | test("no limit, or one that is not a whole number, is the host's", () => { | |
| 623 | expect(shardCpus(8)).toBe(8); | |
| 624 | expect(shardCpus(8, "")).toBe(8); | |
| 625 | expect(shardCpus(8, "abc")).toBe(8); | |
| 626 | expect(shardCpus(8, "0")).toBe(8); | |
| 627 | }); | |
| 628 | test("never fewer than one, never more than the host", () => { | |
| 629 | expect(shardCpus(80, "1")).toBe(1); | |
| 630 | expect(shardCpus(2, "16")).toBe(2); | |
| 631 | }); | |
| 632 | }); |
new above
offline