Skip to content

Commit cdd4d85

Browse files
committed
fix(engine): worker, extraction and encoder sizing read the CPU quota, not the host core count
1 parent 5798f38 commit cdd4d85

12 files changed

Lines changed: 45 additions & 35 deletions

‎packages/engine/src/services/audioMixer.order.test.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ vi.mock("../utils/runFfmpeg.js", async (importOriginal) => ({
1313
// Enough cores that every preparation starts at once, so they can finish in reverse.
1414
vi.mock("node:os", async (importOriginal) => ({
1515
...(await importOriginal<typeof import("node:os")>()),
16-
cpus: () =>
17-
Array.from({ length: 16 }, () => ({}) as ReturnType<typeof import("node:os").cpus>[number]),
16+
availableParallelism: () => 16,
1817
}));
1918

2019
// The AAC encode is aacEncode.test.ts's subject; here the mix only has to hand it the float sum.

‎packages/engine/src/services/audioMixer.prepareConcurrency.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { spawnSync } from "node:child_process";
22
import { copyFileSync, mkdtempSync, rmSync } from "node:fs";
3-
import { cpus, tmpdir } from "node:os";
3+
import { availableParallelism, tmpdir } from "node:os";
44
import { join } from "node:path";
55
import { afterEach, describe, expect, it, vi } from "vitest";
66
import { getFfmpegBinary } from "../utils/ffmpegBinaries.js";
@@ -52,7 +52,7 @@ describe.skipIf(!HAS_FFMPEG)("preparing the clips of a long mix", () => {
5252
{ encoding: "utf-8" },
5353
);
5454
expect(tone.status).toBe(0);
55-
const slots = Math.max(2, Math.floor(cpus().length / 2));
55+
const slots = Math.max(2, Math.floor(availableParallelism() / 2));
5656
const count = Math.max(40, slots * 3);
5757
const clips = Array.from({ length: count }, (_, i) => {
5858
copyFileSync(join(projectDir, "a.wav"), join(projectDir, `a${i}.wav`));

‎packages/engine/src/services/audioMixer.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
} from "../utils/urlDownloader.js";
1818
import { DEFAULT_CONFIG, type EngineConfig } from "../config.js";
1919
import { describeFfmpegFailure, runFfmpeg, type RunFfmpegResult } from "../utils/runFfmpeg.js";
20-
import { cpus } from "node:os";
20+
import { availableParallelism } from "node:os";
2121
import { createConcurrencyLimit } from "../utils/concurrencyLimit.js";
2222
import { encodeAac } from "./aacEncode.js";
2323
import { audioSubprocessEvidence } from "./audioFailureDiagnostic.js";
@@ -194,7 +194,7 @@ function legacyFilterScriptOptionIsUnsupported(stderr: string): boolean {
194194
* than a piecewise-linear volume envelope needs.
195195
*/
196196
// Caps the ffmpeg runs that prepare clips; all of a long mix's clips at once starved the host (200 clips, 200 ffmpeg).
197-
const inPrepareSlot = createConcurrencyLimit(Math.max(2, Math.floor(cpus().length / 2)));
197+
const inPrepareSlot = createConcurrencyLimit(Math.max(2, Math.floor(availableParallelism() / 2)));
198198
// A clip read from a URL spends its time waiting on the network, so it needs no CPU-sized limit.
199199
const inRemotePrepareSlot = createConcurrencyLimit(16);
200200

‎packages/engine/src/services/audioMixer.urlRead.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "nod
33
import { createServer, type Server, type ServerResponse } from "node:http";
44
import { once } from "node:events";
55
import type { AddressInfo } from "node:net";
6-
import { cpus, tmpdir } from "node:os";
6+
import { availableParallelism, tmpdir } from "node:os";
77
import { join } from "node:path";
88
import { afterEach, beforeEach, describe, expect, it } from "vitest";
99
import { getFfmpegBinary } from "../utils/ffmpegBinaries.js";
@@ -280,6 +280,6 @@ describe.skipIf(!HAS_FFMPEG)("audio cut from a cloud-held file's URL", { timeout
280280
);
281281

282282
expect(result.success).toBe(true);
283-
expect(peak).toBeGreaterThan(Math.max(2, Math.floor(cpus().length / 2)));
283+
expect(peak).toBeGreaterThan(Math.max(2, Math.floor(availableParallelism() / 2)));
284284
});
285285
});

‎packages/engine/src/services/parallelCoordinator.test.ts‎

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { cpus, totalmem } from "os";
1+
import { availableParallelism, cpus, totalmem } from "os";
22
import { getHeapStatistics } from "v8";
33
import { afterEach, describe, it, expect, vi } from "vitest";
44
import {
@@ -132,7 +132,13 @@ describe("calculateOptimalWorkers", () => {
132132

133133
vi.mock("os", async (importOriginal) => {
134134
const os = await importOriginal<typeof import("os")>();
135-
return { ...os, default: os, cpus: vi.fn(os.cpus), totalmem: vi.fn(os.totalmem) };
135+
return {
136+
...os,
137+
default: os,
138+
availableParallelism: vi.fn(os.availableParallelism),
139+
cpus: vi.fn(os.cpus),
140+
totalmem: vi.fn(os.totalmem),
141+
};
136142
});
137143
vi.mock("v8", async (importOriginal) => {
138144
const v8 = await importOriginal<typeof import("v8")>();
@@ -141,16 +147,15 @@ vi.mock("v8", async (importOriginal) => {
141147

142148
describe("computeWorkerSizing", () => {
143149
afterEach(() => {
150+
vi.mocked(availableParallelism).mockRestore();
144151
vi.mocked(cpus).mockRestore();
145152
vi.mocked(totalmem).mockRestore();
146153
vi.mocked(getHeapStatistics).mockRestore();
147154
});
148155

149156
// The field case: a 24GB 14-core Mac, Node's default ~4GB heap, a long 1080x1920 project.
150157
it("never auto-picks more workers than the V8 heap can feed", () => {
151-
vi.mocked(cpus).mockReturnValue(
152-
Array.from({ length: 14 }, () => ({}) as ReturnType<typeof cpus>[number]),
153-
);
158+
vi.mocked(availableParallelism).mockReturnValue(14);
154159
vi.mocked(totalmem).mockReturnValue(24 * 1024 ** 3);
155160
vi.mocked(getHeapStatistics).mockReturnValue({
156161
heap_size_limit: 4192 * 1024 ** 2,
@@ -167,6 +172,14 @@ describe("computeWorkerSizing", () => {
167172
expect(computeWorkerSizing(1300, undefined, { concurrency: "auto" }).workers).toBe(5);
168173
});
169174

175+
it("sizes from the CPU quota, not the host's core count", () => {
176+
vi.mocked(cpus).mockReturnValue(Array.from({ length: 32 }, () => ({}) as never));
177+
vi.mocked(availableParallelism).mockReturnValue(6);
178+
const sizing = computeWorkerSizing(1300, undefined, { concurrency: "auto" });
179+
expect(sizing.cpuCount).toBe(6);
180+
expect(sizing.cpuBasedWorkers).toBe(4);
181+
});
182+
170183
it("matches calculateOptimalWorkers and reports every constraint", () => {
171184
const config = { concurrency: "auto" as const };
172185
const sizing = computeWorkerSizing(900, undefined, config);

‎packages/engine/src/services/parallelCoordinator.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Auto-detects optimal worker count based on CPU/memory.
66
*/
77

8-
import { cpus, freemem } from "os";
8+
import { availableParallelism, freemem } from "os";
99
import { existsSync, mkdirSync, readdirSync } from "fs";
1010
import { copyFile, readFile } from "fs/promises";
1111
import { renameWithRetryAsync } from "../utils/renameWithRetry.js";
@@ -167,7 +167,7 @@ const ABSOLUTE_MAX_WORKERS = 24;
167167
// worker's SwiftShader compositor + the shader-blend thread pool, both of
168168
// which are themselves CPU-heavy.
169169
function defaultSafeMaxWorkers(): number {
170-
return Math.max(6, Math.min(16, Math.floor(cpus().length / 8)));
170+
return Math.max(6, Math.min(16, Math.floor(availableParallelism() / 8)));
171171
}
172172
const MIN_FRAMES_PER_WORKER = 30;
173173

@@ -314,7 +314,7 @@ export function computeWorkerSizing(
314314
requested?: number,
315315
config?: WorkerSizingConfig,
316316
): WorkerSizing {
317-
const cpuCount = cpus().length;
317+
const cpuCount = availableParallelism();
318318
// Use total memory instead of free memory — macOS reports misleadingly low
319319
// freemem() because it aggressively caches files in "inactive" memory that
320320
// is immediately reclaimable.
@@ -1217,7 +1217,7 @@ export function getSystemResources(): {
12171217
recommendedWorkers: number;
12181218
} {
12191219
return {
1220-
cpuCores: cpus().length,
1220+
cpuCores: availableParallelism(),
12211221
totalMemoryMB: getSystemTotalMb(),
12221222
freeMemoryMB: Math.round(freemem() / (1024 * 1024)),
12231223
recommendedWorkers: calculateOptimalWorkers(1000),

‎packages/engine/src/services/videoFrameExtractor.concurrency.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { runFfmpeg } from "../utils/runFfmpeg.js";
1010
// Two CPUs: the extractor runs at most one ffmpeg at a time.
1111
vi.mock("os", async (importOriginal) => ({
1212
...(await importOriginal<typeof import("os")>()),
13-
cpus: () => [{}, {}],
13+
availableParallelism: () => 2,
1414
}));
1515

1616
const { extractAllVideoFrames, extractVideoFramesRange } = await import("./videoFrameExtractor.js");

‎packages/engine/src/services/videoFrameExtractor.remoteTail.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { spawnSync } from "node:child_process";
22
import { mkdtempSync, readFileSync, rmSync } from "node:fs";
33
import { createServer, type Server } from "node:http";
44
import type { AddressInfo } from "node:net";
5-
import { cpus, tmpdir } from "node:os";
5+
import { availableParallelism, tmpdir } from "node:os";
66
import { join } from "node:path";
77
import { afterAll, beforeAll, describe, expect, it } from "vitest";
88
import type { VideoElement } from "../types.js";
@@ -89,7 +89,7 @@ describe.skipIf(!HAS_FFMPEG)("deferred windows of a remote source", { timeout: 1
8989
const result = await defer((i) => 5 + i * 0.05);
9090

9191
expect(result.extracted.length).toBe(24);
92-
expect(peak).toBeGreaterThan(Math.max(1, Math.floor(cpus().length / 2)));
92+
expect(peak).toBeGreaterThan(Math.max(1, Math.floor(availableParallelism() / 2)));
9393
});
9494
});
9595

‎packages/engine/src/services/videoFrameExtractor.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import { audioGroupsById, isMemberGroupHidden, isSelfOrAncestorHidden } from "./mediaHidden.js";
1010
import { copyFileSync, existsSync, linkSync, mkdirSync, rmSync } from "fs";
1111
import { join } from "path";
12-
import { cpus } from "os";
12+
import { availableParallelism } from "os";
1313
import { createConcurrencyLimit } from "../utils/concurrencyLimit.js";
1414
import { parseHTML } from "linkedom";
1515
import { isPathInside } from "@hyperframes/parsers/asset-paths";
@@ -923,7 +923,7 @@ function undoTickRoundingOnTheSourceFrameGrid(seek: number, lead: number): strin
923923
}
924924

925925
// Caps extraction ffmpeg runs across all clips; uncapped, dozens of clips starved each other into timeouts.
926-
const EXTRACTION_SLOTS = Math.max(1, Math.floor(cpus().length / 2));
926+
const EXTRACTION_SLOTS = Math.max(1, Math.floor(availableParallelism() / 2));
927927
const inExtractionSlot = createConcurrencyLimit(EXTRACTION_SLOTS);
928928
const TAIL_PROBE_SLOTS = Math.max(1, Number(process.env.HF_TAIL_PROBE_SLOTS) || 16);
929929
const inTailProbeSlot = createConcurrencyLimit(TAIL_PROBE_SLOTS);

‎packages/producer/src/services/distributed/renderChunk.ts‎

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636

3737
import { randomBytes } from "node:crypto";
3838
import { existsSync, mkdirSync, readFileSync, readdirSync, rmSync, writeFileSync } from "node:fs";
39-
import { cpus } from "node:os";
39+
import { availableParallelism } from "node:os";
4040
import { join } from "node:path";
4141
import {
4242
assertSwiftShader,
@@ -221,15 +221,9 @@ export interface RenderChunkOptions {
221221
* this many, one browser per 20 frames; never lowers it.
222222
*/
223223
maxCaptureWorkers?: number;
224-
/**
225-
* Round-robin the chunk's frames across the capture browsers while the encoder runs, which
226-
* shortens wall time but costs CPU. Set it where wall time is billed (Lambda); the default is
227-
* contiguous ranges, which bill the least CPU.
228-
*/
229224
interleaveCapture?: boolean;
230225
}
231226

232-
/** Round-robin capture only feeds the overlapped encoder, and only for a caller that bills wall time. */
233227
export function interleavesCapture(overlapping: boolean, options?: RenderChunkOptions): boolean {
234228
return overlapping && options?.interleaveCapture === true;
235229
}
@@ -987,7 +981,11 @@ export async function renderChunk(
987981
// including error paths. Clear our defensive handle before await.
988982
const captureSession = session;
989983
session = null;
990-
overlap.current = canEncodeWhileCapturing(encodeInput, chunkWorkerCount, cpus().length)
984+
overlap.current = canEncodeWhileCapturing(
985+
encodeInput,
986+
chunkWorkerCount,
987+
availableParallelism(),
988+
)
991989
? encodeWhileCapturing(encodeInput, framesInChunk)
992990
: null;
993991
await runCaptureStage({

0 commit comments

Comments
 (0)