// HOW MUCH MEMORY A RENDER DECLARES to the heavy slot (scripts/queue-lock.mjs). // // The slot admits by memory: a job that declares an estimate runs BESIDE // others while MemAvailable minus every running job's estimate stays at or // above the floor (HEAVY_MIN_FREE_MB, 6000); a job that declares nothing runs // ALONE. A render used to declare nothing, so a capped site build (it declares // its 5 GB MemoryMax) waited out the whole render. Now every render declares: // // a take (takeRender.mjs, `queue-lock --heavy --estimate -- build-video …`), // build-video's own heavy runs (heavy-gate.mjs withRenderSlot), kind "render" // the encode ladder (variants.mjs), kind "ladder" // // MEASURED (2026-10-26, the process tree's PSS -- Chromium included -- sampled // every 3 s; the Huck cut, 1920x1080 30 fps, x264 slow, 68 segments): // // the crossfade pass, 23 segments open at once (one of 3 batches) 4.9 GB // the last join (3 batch files + 4 chrome layers) 1.4 GB // a 120 s window's one pass (4 segments, the chrome over them) 2.0 GB // one segment's encode 0.85 GB // the full deck's capture, 7 Chromium workers 2.2 GB // a window's capture, 4 / 7 workers 0.8 / 1.15 GB // the ladder's re-encode (x264 medium, one rung at a time) 0.76 GB // // The STACKED VERTICAL DECK (vertical-deck.mjs; 2026-11-10, the heavy scope's MemoryPeak, the quartering-diet // take's 3 entries -- a twocrop clip, a clip, a post item -- 1080x1920, the deck's full-frame page and the // dial/odometer/trail/extras layers, a preview): 2.6 GB // A 9:16 frame is a 16:9 frame's pixels on its side, so the formula below holds for it; its full-frame deck page // is one more 1080x1920 layer, which VERTICAL_DECK_MIN_MB (the measured peak + 25 %) floors. // // So a render's peak is its widest ffmpeg -- the crossfade pass holds every // segment of a batch open, about 200 MB each (build-video's xfadeBatches: // REPORT_VIDEO_XFADE_BATCH / render.xfadeBatch, default 24) -- or its chrome // capture, whichever is larger: the phases run one after another, so the peak // is the larger of the two, not their sum. // // render: max(ENCODE_BASE + ENCODE_PER_SEGMENT × min(segments, batch), // CHROME_BASE × layers + CHROME_PER_WORKER × workers) // // `segments` is what the run joins (a window's entries, a whole cut's; unknown: // a full batch); `workers` the capture's worker budget and `layers` its // renders at once (chromeWorkers / chromeLayers below). The constants carry // about 20 % over the measured line. // // UMTOOL_RENDER_ESTIMATE_MB / UMTOOL_LADDER_ESTIMATE_MB override a kind's // number; `0` declares nothing, and the job runs alone as before. // // The estimate is for ADMISSION; the cap is the hard limit. An admitted // `--heavy` command still runs in its capped scope (MemoryMax 8G, swap 2G, // queue-lock's heavyCapPlan; an estimate over the cap raises the cap to it). // // Pure and dependency-free: the umtool app bundles takeRender.mjs, which // imports this, and build-video.mjs imports it too (no cycle). // The vertical format's rules (common, pure and dependency-free as this file is). import { stackedDeckWanted } from "yt-dlp-transcript-common/lib/report/vertical.mjs"; /** The crossfade / overlay pass's own memory, with no segment open. */ export const RENDER_ENCODE_BASE_MB = 1700; /** Each segment the pass holds open (a decoder and its frames). */ export const RENDER_ENCODE_PER_SEGMENT_MB = 180; /** One chrome region's capture: hyperframes' node, the browser and GPU processes. */ export const RENDER_CHROME_BASE_MB = 400; /** Each Chromium renderer page a region captures with. */ export const RENDER_CHROME_PER_WORKER_MB = 320; /** The ladder's one x264 re-encode at a time. */ export const LADDER_ESTIMATE_MB = 1000; /** * A BASIC cut (render.mode "basic", basic.mjs): one span's encode at a time at * the source's own size, then a stream-copied concat -- no chrome capture, no * crossfade pass. One segment's encode measured 0.85 GB at 1080p30 (above); * a 1080p60 source decodes twice the frames, so this carries about 40 % over it. */ export const BASIC_ESTIMATE_MB = 1200; /** A stacked vertical deck's render declares at least this (its measured peak, 2.6 GB, + 25 %). */ export const VERTICAL_DECK_MIN_MB = 3300; /** build-video's XFADE_BATCH_DEFAULT: the segments one crossfade pass opens. */ export const XFADE_BATCH = 24; // THE CHROME CAPTURE'S SHAPE (capture-pool.mjs). A build's chrome layers // capture side by side from ONE budget of renderer workers, at most // `chromeLayers` renders at once, each a HyperFrames process with its own // browser: // // chrome: CHROME_BASE × layers + CHROME_PER_WORKER × workers // // The budget, absent UMTOOL_CHROME_WORKERS, is the machine's: one and a half // workers per core, at most CHROME_AUTO_MAX, and no more than the chrome's // share of the heavy scope's cap (CHROME_MEMORY_MB) holds. Nothing else runs // during the capture (the segments are built before it, the encode after), // and a worker spends much of each frame waiting on the browser: MEASURED // (2026-10-29, 8 cores, Ember's deck, 10,689 distinct frames by the frame // list) 6 workers 210 s, 8 114 s (the machine 66 % busy), 12 98 s (86 %), // 16 92 s, 24 100 s; 12 workers peaked at 3.4 GB PSS. Whole renders are // lighter still (8 workers left it ~45 % idle). UMTOOL_CHROME_WORKERS // sets it outright (1-16), as it always has; UMTOOL_CHROME_LAYERS sets the // renders at once (1 is one layer after another, as before). /** The most workers the automatic budget takes. */ export const CHROME_AUTO_MAX = 12; /** Renders at once, absent UMTOOL_CHROME_LAYERS. */ export const CHROME_LAYERS_DEFAULT = 3; /** The chrome capture's share of the heavy scope's 8 GB cap (the rest: the build's node, ffprobe, slack). */ export const CHROME_MEMORY_MB = 6144; /** This machine's cores, when it can be asked (a bundle without node:os says 4). */ function machineCores() { try { const os = globalThis.process?.getBuiltinModule?.("node:os"); const n = os?.availableParallelism?.() ?? os?.cpus?.().length; return Number.isInteger(n) && n > 0 ? n : 4; } catch { return 4; } } /** The renders a build's chrome capture runs at once: UMTOOL_CHROME_LAYERS (1-8), else 3. */ export function chromeLayers(env = process.env) { const n = Number(env.UMTOOL_CHROME_LAYERS); return Number.isInteger(n) && n >= 1 ? Math.min(n, 8) : CHROME_LAYERS_DEFAULT; } /** * The chrome workers a render's capture shares (build-video's capture pool): * UMTOOL_CHROME_WORKERS (1-16) when it is set; else one and a half per core, * at least 2 and at most CHROME_AUTO_MAX, within CHROME_MEMORY_MB beside * `chromeLayers` renders. */ export function chromeWorkers(env = process.env, cores = machineCores()) { const set = Number(env.UMTOOL_CHROME_WORKERS); if (set > 0) return Math.max(1, Math.min(16, Math.floor(set))); const byMemory = Math.floor((CHROME_MEMORY_MB - RENDER_CHROME_BASE_MB * chromeLayers(env)) / RENDER_CHROME_PER_WORKER_MB); return Math.max(2, Math.min(CHROME_AUTO_MAX, Math.ceil(1.5 * (Number(cores) || 4)), byMemory)); } /** The chrome capture's memory (MB): its renders' bases and its workers. */ export function chromeEstimateMb(env = process.env, cores = machineCores()) { const w = chromeWorkers(env, cores); return RENDER_CHROME_BASE_MB * Math.min(chromeLayers(env), w) + RENDER_CHROME_PER_WORKER_MB * w; } const OVERRIDE = { render: "UMTOOL_RENDER_ESTIMATE_MB", ladder: "UMTOOL_LADDER_ESTIMATE_MB", basic: "UMTOOL_RENDER_ESTIMATE_MB" }; /** * The segments one pass holds open: `segments` capped by the batch size * (`batch` 0 never batches); unknown segments, a full batch. */ export function openSegments(segments, batch = XFADE_BATCH) { const size = Number.isInteger(batch) && batch >= 0 ? batch : XFADE_BATCH; const n = Number.isFinite(segments) && segments > 0 ? Math.ceil(segments) : null; if (n == null) return size || XFADE_BATCH; return size ? Math.min(n, size) : n; } /** * The estimate (MB) a render of `kind` declares, or null to declare nothing * (an override of `0`: the job then runs alone). * * @param {"render" | "ladder" | "basic"} [kind] * @param {Record} [env] * @param {{ segments?: number | null, batch?: number, cores?: number }} [shape] what the run joins, the batch size, the machine's cores * @returns {number | null} */ export function renderEstimateMb(kind = "render", env = process.env, { segments = null, batch = XFADE_BATCH, cores = machineCores() } = {}) { const name = OVERRIDE[kind]; if (!name) throw new Error(`renderEstimateMb: no kind '${kind}' (render, ladder, basic)`); const raw = env[name]; if (raw != null && String(raw).trim() !== "") { const n = Number(raw); if (n === 0) return null; if (n > 0) return Math.round(n); // Not a size: fall through to the measured default rather than run alone. } if (kind === "ladder") return LADDER_ESTIMATE_MB; if (kind === "basic") return BASIC_ESTIMATE_MB; const encode = RENDER_ENCODE_BASE_MB + RENDER_ENCODE_PER_SEGMENT_MB * openSegments(segments, batch); return Math.max(encode, chromeEstimateMb(env, cores)); } /** * The segments a manifest's render joins: its timeline's entries, and the * brand intro's own segment when it has one. Null when it cannot tell. * * @param {any} manifest * @returns {number | null} */ export function manifestSegments(manifest) { const timeline = manifest?.timeline; if (!Array.isArray(timeline)) return null; return timeline.length + (manifest?.render?.intro ? 1 : 0); } /** * What a render of `manifest` declares (renderEstimateMb's "render"): its * segments joined in batches of `batch`; one segment when nothing is * crossfaded (`xfade` false: --no-xfade, a transition of 0 -- the concat * stream-copies and one pass lays the chrome over it) or when the run is one * clip (`single`: --clip-preview, --short). A manifest it cannot read: a full * batch. * * @param {any} manifest * @param {Record} [env] * @param {{ xfade?: boolean, single?: boolean, batch?: number }} [o] * @returns {number | null} */ export function manifestEstimateMb(manifest, env = process.env, { xfade = true, single = false, batch = XFADE_BATCH, vertical = null } = {}) { // A basic cut (render.mode, as resolved: a take's render manifest carries its preset laid) is one encode at a time. if (manifest?.render?.mode === "basic") return renderEstimateMb("basic", env); const crossfades = xfade && (manifest?.render?.transition ?? 0.5) !== 0; const segments = single || (manifest && !crossfades) ? 1 : manifestSegments(manifest); const mb = renderEstimateMb("render", env, { segments, batch }); // A 9:16 cut that keeps its deck (stackedDeckWanted: the manifest's render.format vertical, or a `--vertical` build // of it -- `vertical`: true or its knobs, as `umtool vertical` asks): floored at its measured peak + margin -- // unless an override says otherwise (0: declare nothing). const own = manifest?.render ?? {}; const r = vertical ? { ...own, format: "vertical", vertical: { ...(own.vertical ?? {}), ...(typeof vertical === "object" ? vertical : {}) } } : own; return stackedDeckWanted(r) && mb > 0 && env.UMTOOL_RENDER_ESTIMATE_MB === undefined ? Math.max(mb, VERTICAL_DECK_MIN_MB) : mb; }