Repository navigation
fix(producer): cap the software encoder to the cores capture leaves free - #8019
Conversation
miguel-heygen
left a comment
There was a problem hiding this comment.
Clean, well-scoped fix for #7458.
Strengths
overlapEncoderThreads(encodeStage.ts) is pure and floors at 1.canEncodeWhileCapturingalready requiresworkerCount < cpuCount, so the cap is always at least one real free core.- The cap only applies when a
captureBudgetis passed, so the post-capture encode and every other caller keep the old-threadsvalue. - Distributed determinism holds.
chunkWorkerCountcomes fromcomputeWorkerSizing, which reads CPU count, cgroup quota and total memory, so it is stable per worker. Same-worker retries therefore get the same-threadsvalue and stay byte-identical. Cross-worker output was already PSNR-equivalent, not byte-equal.
Important (follow-up, not blocking)
-threadsis fixed for the whole ffmpeg process. If the encoder falls behind capture,finish()encodes the remaining backlog with the capped pool (e.g. 1 thread at 7/8 workers) while every core is idle. Worth checking in a render benchmark at high worker:core ratios that wall time doesn't regress against the oversubscribed baseline.- The two "byte-identical to the encode after capture" tests in
encodeWhileCapturing.test.tscallencodeWhileCapturingwithout a budget. They no longer cover the configuration production uses for the overlap. A capped overlap is expected to differ from the full-width post-capture encode, which is fine, but a short note in the tests would stop a later reader from assuming they're equivalent.
Nit
renderChunk.tscallsusableCpuCount()twice on the overlap line. Hoisting it into a local would guarantee that the gate and the budget see the same value.
Verdict: APPROVE
Reasoning: Correct, minimal and opt-in at the two overlap call sites. Distributed per-worker determinism is preserved. CI passed on this head.
`canEncodeWhileCapturing` started an H.264 export beside the capture workers whenever `workerCount < cpuCount`, but `appendSoftwareEncoderThreadsArg` gave the encoder every usable core, so the overlap spawned a full-width x264 pool next to the capture workers (heygen-com#7458). The overlap now reserves the cores capture is using: `softwareEncoderThreads = max(1, cpuCount - workerCount)` travels through the streaming encoder options to ffmpeg's `-threads`, while the post-capture encode keeps the old default.
Head branch was pushed to by a user without write access
502f128 to
79e5416
Compare
|
Fixed the comment-ratchet regression in The workflows on the new commit are |
miguel-heygen
left a comment
There was a problem hiding this comment.
Clean, well-scoped fix for #7458. Re-approving at 79e5416: the only delta from 502f128 (approved earlier) is the doc comments dropped for comment-ratchet; the code is identical.
Strengths
overlapEncoderThreads(encodeStage.ts) is pure and floors at 1.canEncodeWhileCapturingalready requiresworkerCount < cpuCount, so the cap is always at least one real free core.- The cap only applies when a
captureBudgetis passed, so the post-capture encode and every other caller keep the old-threadsvalue. - Distributed determinism holds.
chunkWorkerCountcomes fromcomputeWorkerSizing, which reads CPU count, cgroup quota and total memory, so it is stable per worker. Same-worker retries therefore get the same-threadsvalue and stay byte-identical. Cross-worker output was already PSNR-equivalent, not byte-equal.
Important (follow-up, not blocking)
-threadsis fixed for the whole ffmpeg process. If the encoder falls behind capture,finish()encodes the remaining backlog with the capped pool (e.g. 1 thread at 7/8 workers) while every core is idle. Worth checking in a render benchmark at high worker:core ratios that wall time doesn't regress against the oversubscribed baseline.- The two "byte-identical to the encode after capture" tests in
encodeWhileCapturing.test.tscallencodeWhileCapturingwithout a budget. They no longer cover the configuration production uses for the overlap. A capped overlap is expected to differ from the full-width post-capture encode, which is fine, but a short note in the tests would stop a later reader from assuming they're equivalent.
Nit
renderChunk.tscallsusableCpuCount()twice on the overlap line. Hoisting it into a local would guarantee that the gate and the budget see the same value.
Verdict: APPROVE
Reasoning: Correct, minimal and opt-in at the two overlap call sites. Distributed per-worker determinism is preserved. CI passed on this head.
…ing its threads heygen-com#8019 capped the software encoder that runs beside a multi-worker capture to `cpuCount - workerCount` threads. ffmpeg keeps that pool for its whole run, so once capture ends the encoder finishes the chunk on the reserved core alone. On a 10 GB AWS Lambda function (6 vCPU, 5 capture browsers) every chunk encoded with one x264 thread: a 150-frame chunk's encode went from about 4 s to 19 s and the whole render took 2.5 times as long. The overlapping encoder now keeps its usual thread count and runs below normal priority instead (os.setPriority, PRIORITY_BELOW_NORMAL; the priority a Windows user lowered ffmpeg to in heygen-com#7458). Capture, at normal priority, still gets the CPU first; the encoder takes the cores capture leaves idle and all of them once it ends. Its bitstream again matches the encode after capture, since both use the same thread count. A refused priority change leaves the encode at normal priority.
…ing its threads heygen-com#8019 capped the software encoder that runs beside a multi-worker capture to `cpuCount - workerCount` threads. ffmpeg keeps that pool for its whole run, so once capture ends the encoder finishes the chunk on the reserved core alone. On a 10 GB AWS Lambda function (6 vCPU, 5 capture browsers) every chunk encoded with one x264 thread: a 150-frame chunk's encode went from about 4 s to 19 s and the whole render took 2.5 times as long. The overlapping encoder now keeps its usual thread count and runs below normal priority instead (os.setPriority, PRIORITY_BELOW_NORMAL; the priority a Windows user lowered ffmpeg to in heygen-com#7458). Capture, at normal priority, still gets the CPU first; the encoder takes the cores capture leaves idle and all of them once it ends. Its bitstream again matches the encode after capture, since both use the same thread count. A refused priority change leaves the encode at normal priority.
…ing its threads heygen-com#8019 capped the software encoder that runs beside a multi-worker capture to `cpuCount - workerCount` threads. ffmpeg keeps that pool for its whole run, so once capture ends the encoder finishes the chunk on the reserved core alone. On a 10 GB AWS Lambda function (6 vCPU, 5 capture browsers) every chunk encoded with one x264 thread: a 150-frame chunk's encode went from about 4 s to 19 s and the whole render took 2.5 times as long. The overlapping encoder now keeps its usual thread count and runs below normal priority instead (os.setPriority, PRIORITY_BELOW_NORMAL; the priority a Windows user lowered ffmpeg to in heygen-com#7458). Capture, at normal priority, still gets the CPU first; the encoder takes the cores capture leaves idle and all of them once it ends. Its bitstream again matches the encode after capture, since both use the same thread count. A refused priority change leaves the encode at normal priority.
What
Cap the software encoder's thread pool to the cores capture still leaves free when an H.264 export overlaps a multi-worker capture. The encoder no longer spawns a full-width x264 pool next to the capture workers.
Why
canEncodeWhileCapturing()allows the overlap wheneverworkerCount < cpuCount, i.e. when at least one core is nominally free. ButappendSoftwareEncoderThreadsArg()gives the encoder every usable core by default, so the overlap that was meant to use the free core actually ran a full-width encoder pool beside the capture workers — the oversubscription described in #7458.Related work
Fixes #7458.
Refs #5334: that PR degrades multi-worker capture when headroom is insufficient; this one keeps the overlap but gives the encoder only the cores capture is not using. They are complementary, not duplicates.
How
overlapEncoderThreads(workerCount, cpuCount)=max(1, floor(cpuCount) - floor(workerCount)).encodeWhileCapturing()takes an optionalcaptureBudgetand passes the reserved count assoftwareEncoderThreadsthroughvideoEncoderOptions()→StreamingEncoderOptions→appendSoftwareEncoderThreadsArg().renderOrchestrator,renderChunk) pass their worker count and CPU count.Test plan
overlapEncoderThreads(4, 8) = 4,(6, 8) = 2,(7, 8) = 1, never 0.buildStreamingArgs()emits-threads 2whensoftwareEncoderThreads: 2.videoEncoderOptions()carries the field only when a budget is passed.@hyperframes/engineand@hyperframes/producerboth pass.encodeWhileCapturing.test.tscould not run locally; CI has ffmpeg.Commands run: