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

Closed opened this in answer to
Closed without merging

scout/forge:scout/2243-a-check-never-trips-its-own-cpu-guard-te → main 1 commit fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
4 files changed +37 −1
scripts/shards.ts +14
@@ -53,6 +53,20 @@ export function shardCount(
5353 return { ok: true, value: Math.max(1, Math.min(n, files)) };
5454 }
5555
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
5670 /**
5771 * The memory a process may use: the least of the machine's and each
5872 * cgroup limit it can read (`memory.max` under v2, `memory.limit_in_bytes`
scripts/test-shards.ts +2 −1
@@ -53,6 +53,7 @@ import {
5353 SHARDS_MAX,
5454 WORKER_MEMORY_BYTES,
5555 shardCount,
56 shardCpus,
5657 sliceFiles,
5758 splitArgs,
5859 splitFiles,
@@ -339,7 +340,7 @@ const readLimit = (path: string): string | undefined => {
339340 return undefined;
340341 }
341342 };
342 const cpus = availableParallelism();
343 const cpus = shardCpus(availableParallelism(), process.env["FORGE_CELL_CPUS"]);
343344 const shardsOverride = process.env["FORGE_CHECK_SHARDS"];
344345 const memory = memoryLimit(totalmem(), [
345346 readLimit("/sys/fs/cgroup/memory.max"),
src/checks/service.ts +2
@@ -792,6 +792,8 @@ export function startChecks(app: App, options: ChecksOptions): ChecksHandle {
792792 FORGE_CHECK: run.id,
793793 FORGE_COMMIT: x.commitOid,
794794 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),
795797 ...(base === undefined
796798 ? {}
797799 : {
test/shards.test.ts +19
@@ -19,6 +19,7 @@ import {
1919 refreshTimes,
2020 parseShardSpec,
2121 shardCount,
22 shardCpus,
2223 sliceFiles,
2324 splitArgs,
2425 splitFiles,
@@ -611,3 +612,21 @@ describe("one file per worker, timed", () => {
611612 );
612613 });
613614 });
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 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs