From a441af732dacec1e8af4fa5b656adeb48cb07c9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 30 Sep 2026 10:05:18 +0300 Subject: [PATCH] lane-av1enchang: box the DPB CdfSnapshot (Av1Encoder 139960 -> 18168 bytes), gate the tight-stack requirement, and add a measured SLOW-not-hung backstop THE REAL DEFECT: Av1Encoder was 139,960 bytes BY VALUE (eight inline CdfSnapshot DPB slots) and is returned by value through three nested Result constructors (with_pyramid_and_rate_target -> with_pyramid -> new). Any caller on a normal 2 MiB thread stack CRASHES: reproduced twice on the pre-fix binary built from unmodified main, 'fatal runtime error: stack overflow, aborting' at Av1Encoder::new (encoder.rs:1519), from coredumpctl. The repo HID this behind '.cargo/config.toml [env] RUST_MIN_STACK = 67108864' -- a 64 MiB stack for the workspace's own cargo runs only, which is also why 'cargo test' wedges instead of aborting when the test binary is invoked directly. Box the per-slot snapshot: 139960 -> 18168 bytes (7.7x). The tables' VALUES are untouched. GATE: the_pyramid_constructors_fit_on_a_tight_stack spawns a 1 MiB thread and constructs both the flat and the pyramid encoder. Red/green in milliseconds instead of a 6m35s debug run. Verified by Main: with the Box removed the gate dies 'fatal runtime error: stack overflow, aborting' (SIGABRT); with it, green in 0.00s. Post-fix clean from 384 KiB (2.7x margin at 1 MiB), pre-fix overflowed up to 2 MiB. THE ~7 HOUR TEST IS NOT A HANG, and this lane measured it rather than guessing: it terminates and passes (old pin ea8fa18f, same test, byte-identical output, 275.53s RELEASE). evals per search are 61.3 / 64.5 / 64.9 across 384k / 1.5M / 2M bps -- FLAT, equal to the search's designed bound, so the search is not pathological. The cost is full search x 60 superblocks x 48 frames x 8 arms x ~13x debug penalty, and the HIGH-BITRATE arms dominate because the rate loop must walk q_idx DOWN from seed 100 into the 20s to spend an 8000-byte budget. 3 gdb backtraces 20s apart show 3 different functions, 3 different blocks and a changing base_q_idx (32/32/30) -- progress, not a spin. BACKSTOP, reported as a backstop and NOT as a fix for the cost: a per-CALL wall-clock ceiling on a worker thread (1800s default, EC_AV1_ENCODE_CALL_BUDGET_SECS) = ~16x the worst honest debug call (~110s, derived) and 1730x the worst optimized call (1.04s, measured). Red-proofed both ways: EC_AV1_ENCODE_CALL_BUDGET_SECS=1 -> FAILED, exit 101, message carries the test name, the arm name, the arm's elapsed seconds, the stuck call index and the text 'SLOW, NOT HUNG'; an injected spin_loop in encode_frames fails at 5m00s where pre-fix it hangs forever (injection reverted, 0 grep hits). BIT-EXACTNESS: round_trip_encode 108 passed 0 failed 40 ignored after the Box change; the bitrate gate 8/8 arms byte-identical to pre-fix; pyramid decode gates (display order through both decoders, three- and four-level) all ok. Main re-ran the round_trip family in release: 20 passed 0 failed. Lane report: lanes/av1enchang.report.md. --- crates/ec-av1/src/encoder.rs | 263 +++++++++++++++++++--- lanes/av1enchang.report.md | 410 +++++++++++++++++++++++++++++++++++ 2 files changed, 646 insertions(+), 27 deletions(-) create mode 100644 lanes/av1enchang.report.md diff --git a/crates/ec-av1/src/encoder.rs b/crates/ec-av1/src/encoder.rs index 816c00f2..db39bc65 100644 --- a/crates/ec-av1/src/encoder.rs +++ b/crates/ec-av1/src/encoder.rs @@ -1477,11 +1477,22 @@ pub struct Av1Encoder { /// One decoded-picture-buffer slot: what a `refresh_frame_flags` bit stores /// and what a `ref_frame_idx` entry reads back. +/// +/// lane-av1enchang: the `CdfSnapshot` is BOXED, not held by value. Eight +/// inline copies made [`Av1Encoder`] 139960 bytes, and that struct is +/// returned by value through three nested `Result` constructors +/// ([`Av1Encoder::with_pyramid_and_rate_target`] -> +/// [`Av1Encoder::with_pyramid`] -> [`Av1Encoder::new`]), so an unoptimized +/// build put several of those on the stack at once: the pyramid path +/// overflowed libtest's 2 MiB test thread and aborted the whole crate suite +/// with `fatal runtime error: stack overflow`. One heap box per DPB slot +/// brings the struct from 139960 bytes down to 18168 (measured). The tables' +/// VALUES are untouched. #[derive(Debug, Clone)] struct DpbSlot { picture: Picture, order_hint: u32, - cdfs: crate::encode::CdfSnapshot, + cdfs: Box, } /// The encoder stays `Send` now that it owns a `FrameCtx` (whose cells are @@ -1963,7 +1974,7 @@ impl Av1Encoder { let slot = DpbSlot { picture: encoded.reconstruction.clone(), order_hint, - cdfs: encoded.next_cdfs.clone(), + cdfs: Box::new(encoded.next_cdfs.clone()), }; for &s in slots { self.dpb[s as usize] = Some(slot.clone()); @@ -3379,6 +3390,35 @@ mod tests { ); } + /// One (bitrate, pyramid) arm's tally, built on the worker thread and + /// handed back through the progress channel + /// (`bitrate_target_lands_within_5_percent_over_48_frames`). Per LEVEL, + /// so a miss reads as WHICH level's model is off rather than as one + /// aggregate percentage. + struct ArmResult { + /// [`RateLoop::target`] as the arm's own encoder computed it. + targets: [f64; 3], + coded: usize, + bytes: [usize; 4], + count: [usize; 4], + } + + impl ArmResult { + fn take(&mut self, packets: Vec) { + for packet in packets { + self.coded += packet.data.len(); + let i = match packet.level { + Level::Key => 0, + Level::Arf => 1, + Level::Leaf => 2, + Level::ShowExisting => 3, + }; + self.bytes[i] += packet.data.len(); + self.count[i] += 1; + } + } + } + /// [`RateTarget::Bitrate`] lands the achieved bitrate within ±5% of the /// target over 48 frames of a real clip, flat and under a pyramid alike, /// across a 5x span of bitrates. This is the accuracy claim of the @@ -3394,6 +3434,103 @@ mod tests { /// frame are the same at 1.5 and 2 Mbit/s) and the leaves sit at 1 + 12 /// `leaf_q_offset`, so this clip cannot be made to spend much past /// 2.4 Mbit/s under the default pyramid however the budget is split. + /// + /// # Bounded + /// + /// Each arm's encode runs on a worker thread under a per-CALL + /// wall-clock ceiling, so this gate FAILS rather than hanging when an + /// encode stops terminating (lane-av1enchang: a 3h20m wedge on one host + /// and 30min on another, both here, both with the test thread spinning + /// and no child process for the crate's child timeout to reap). The cost + /// is real and is why the budget is not tight: the two high bitrate arms + /// walk `q_idx` from the seed 100 down into the 20s to spend an + /// 8000-byte budget on a 640x384 clip, and a frame at `q_idx` 25 costs + /// ~15s in a debug build (~1s optimized) against ~0.5s at `q_idx` 100. + /// lane-av1enchang: the PYRAMID CONSTRUCTORS fit on a tight stack. + /// + /// `Av1Encoder` used to be 139960 bytes BY VALUE, because each of the + /// eight `DpbSlot`s held a 15 KB `CdfSnapshot` inline. The pyramid + /// constructors nest three deep -- `with_pyramid_and_rate_target` -> + /// `with_pyramid` -> `new` -- and each level materialises the struct in + /// an unoptimized build, so ~840 KB of stack was live at the innermost + /// `Ok(Self { .. })`. On libtest's default 2 MiB test thread that aborts: + /// `fatal runtime error: stack overflow, aborting`, reproduced on the + /// pre-fix binary at `crates/ec-av1/src/encoder.rs:1519` via + /// `coredumpctl debug`. + /// + /// Boxing the snapshot brought the struct to 18168 bytes. This gate pins + /// that: it builds BOTH constructors on a thread with a deliberately + /// tight stack, so the old layout ABORTS THE PROCESS and the new one + /// returns. It runs in milliseconds, where proving the same thing through + /// the 48-frame gate takes 6m35s. + /// + /// Both thresholds were swept, in an unoptimized build: + /// + /// | stack | pre-fix (`CdfSnapshot` by value) | post-fix (boxed) | + /// |---|---|---| + /// | 256 KiB | overflow | overflow | + /// | 384 KiB | overflow | ok | + /// | 512 KiB | overflow | ok | + /// | 1 MiB | **overflow** | **ok** | + /// | 2 MiB | **overflow** (libtest's default) | ok | + /// | 3 MiB | ok | ok | + /// + /// 1 MiB is the gate: 2.7x what the boxed layout needs and 2x below what + /// the old one did, so it is nowhere near either threshold and cannot + /// flake. The pre-fix row at 1 MiB is the red -- which is also why + /// `.cargo/config.toml` carries `RUST_MIN_STACK = 67108864`, a + /// workspace-local band-aid this gate makes unnecessary for this struct. + /// + /// `EC_AV1_TIGHT_STACK_BYTES` overrides the size, which is how the sweep + /// above is reproduced. + #[test] + fn the_pyramid_constructors_fit_on_a_tight_stack() { + // 1 MiB: see the threshold table on this test. Red before the Box, + // green after, with 2.7x margin either way. + const STACK: usize = 1024 * 1024; + let stack: usize = std::env::var("EC_AV1_TIGHT_STACK_BYTES") + .ok() + .and_then(|v| v.parse().ok()) + .unwrap_or(STACK); + let config = EncoderConfig { + width: 640, + height: 384, + base_q_idx: 100, + gop: 48, + colour: Colour::Bt709Limited, + tile_cols_log2: 0, + tile_rows_log2: 0, + }; + let rate = RateTarget::Bitrate { + bits_per_second: 1_536_000, + frames_per_second: 24.0, + }; + let handle = std::thread::Builder::new() + .stack_size(stack) + .spawn(move || { + let flat = Av1Encoder::with_rate_target(config, rate) + .expect("with_rate_target") + .rate_loop + .is_some(); + let pyr = + Av1Encoder::with_pyramid_and_rate_target(config, Pyramid::default(), rate) + .expect("with_pyramid_and_rate_target") + .pyramid + .is_some(); + (flat, pyr) + }) + .expect("spawn tight-stack thread"); + let (flat, pyr) = handle + .join() + .unwrap_or_else(|_| panic!("the encoder constructors overflowed a {stack}-byte stack")); + assert!(flat, "with_rate_target lost its rate loop"); + assert!(pyr, "with_pyramid_and_rate_target lost its pyramid"); + eprintln!( + "TIGHTSTACK {stack} bytes: Av1Encoder={} bytes, both constructors returned", + std::mem::size_of::() + ); + } + #[test] fn bitrate_target_lands_within_5_percent_over_48_frames() { let _knobs = crate::speed::knob_read(); @@ -3407,6 +3544,7 @@ mod tests { return; }; let fps = 24.0; + let pictures = std::sync::Arc::new(pictures); for bits_per_second in [384_000u32, 768_000, 1_536_000, 2_000_000] { let config = EncoderConfig { width, @@ -3422,32 +3560,104 @@ mod tests { frames_per_second: fps, }; for pyramid in [None, Some(Pyramid::default())] { - let mut enc = match pyramid { - None => Av1Encoder::with_rate_target(config, rate).unwrap(), - Some(p) => Av1Encoder::with_pyramid_and_rate_target(config, p, rate).unwrap(), - }; - let targets = enc.rate_loop.as_ref().unwrap().target; - let mut coded = 0usize; - // Per level, so a miss reads as WHICH level's model is off - // rather than as one aggregate percentage. - let (mut bytes, mut count) = ([0usize; 4], [0usize; 4]); - let mut take = |packets: Vec| { - for packet in packets { - coded += packet.data.len(); - let i = match packet.level { - Level::Key => 0, - Level::Arf => 1, - Level::Leaf => 2, - Level::ShowExisting => 3, - }; - bytes[i] += packet.data.len(); - count[i] += 1; + let arm = format!("{bits_per_second} bps pyramid {}", pyramid.is_some()); + // lane-av1enchang: a BACKSTOP, not a fix. This gate is NOT + // hung and NOT regressed -- it is a ~7 hour test in an + // unoptimized build (measured: the whole `ec-av1` lib suite + // takes 24341s, and this test is the largest single item in + // it), and it PASSES. What it lacked was any way to FAIL: a + // future non-terminating encode would wedge the suite for + // hours with no diagnostic, which is exactly how wave3j and + // wave3h were lost. So the arm runs on a worker thread and + // THIS thread watches a per-CALL deadline, purely so that + // case reports itself. + // + // The budget is PER CALL, not per test, and it is valued from + // a measured run so it cannot turn a correct-but-slow build + // red. Measured on this gate's own workload (48 frames of + // 640x384): one `encode_frames` call is 0.78s..1.04s + // optimized and 8.6s..12.2s debug on the flat arms. A + // PYRAMID call codes a whole mini-GOP at once, so its worst + // single call is `Pyramid::default().mini_gop` frames + // (~9), i.e. ~110s debug. 1800s is therefore ~16x that + // worst honest call: no plausible host trips it, and a real + // non-terminating encode is reported in 30 minutes instead + // of never. `EC_AV1_ENCODE_CALL_BUDGET_SECS` overrides it, + // which is how the backstop is proven to bite. + let budget = std::time::Duration::from_secs( + std::env::var("EC_AV1_ENCODE_CALL_BUDGET_SECS") + .ok() + .and_then(|v| v.parse().ok()) + .unwrap_or(1800), + ); + let total = pictures.len() + 1; + // Shared, not cloned: the 48 decoded frames are ~35 MB of + // `u16` luma+chroma, and all eight arms want the same ones. + let pictures = std::sync::Arc::clone(&pictures); + // `None` is a progress ticket: THIS call returned. `Some` is + // the arm's result, sent after `flush` -- which is itself a + // call that can spin, so it needs a ticket too, not a bare + // `join` that would block forever. + let (tx, rx) = std::sync::mpsc::channel::>(); + let worker = std::thread::spawn(move || { + let mut enc = match pyramid { + None => Av1Encoder::with_rate_target(config, rate).unwrap(), + Some(p) => { + Av1Encoder::with_pyramid_and_rate_target(config, p, rate).unwrap() + } + }; + let targets = enc.rate_loop.as_ref().unwrap().target; + let mut out = ArmResult { + targets, + coded: 0, + bytes: [0; 4], + count: [0; 4], + }; + for picture in pictures.iter() { + out.take(enc.encode_frames(picture).unwrap()); + if tx.send(None).is_err() { + return; + } + } + out.take(enc.flush().unwrap()); + let _ = tx.send(Some(out)); + }); + let started = std::time::Instant::now(); + let mut calls = 0usize; + let out = loop { + match rx.recv_timeout(budget) { + // The result IS the last thing the worker sends, so + // receiving it means every call returned. A worker + // that panicked sends nothing and arrives as + // `Disconnected` below, which re-joins it and + // surfaces the real panic. + Ok(Some(out)) => break out, + Ok(None) => calls += 1, + Err(std::sync::mpsc::RecvTimeoutError::Timeout) => panic!( + "AV1 bitrate gate SLOW, NOT HUNG: the {arm} arm's encode did not \ + return within {}s (arm elapsed {:.0}s, stuck on call {} of {total} \ + -- the call that never came back). This gate is a ~7 hour test in \ + an unoptimized build and it PASSES, so a red here is NOT a slow \ + host: the budget is per CALL and the worst honest call measured \ + is ~1s optimized / ~110s debug on a pyramid arm, so reaching this \ + panic means an encode call that is NOT RETURNING. \ + EC_AV1_ENCODE_CALL_BUDGET_SECS raises the budget.", + budget.as_secs(), + started.elapsed().as_secs_f64(), + calls + 1, + ), + Err(std::sync::mpsc::RecvTimeoutError::Disconnected) => { + worker.join().expect("encode worker panicked"); + panic!("AV1 encode worker for {arm} died without a result"); + } } }; - for picture in &pictures { - take(enc.encode_frames(picture).unwrap()); - } - take(enc.flush().unwrap()); + let ArmResult { + targets, + coded, + bytes, + count, + } = out; let seconds = frames as f64 / fps; let achieved = coded as f64 * 8.0 / seconds; let error = achieved / f64::from(bits_per_second) - 1.0; @@ -3478,7 +3688,6 @@ mod tests { } } } - /// The `BytesPerFrame` controller's own windup bound: no frame's coded /// `base_q_idx` step exceeds [`RateLoop::STEP_CLAMP`], across a real /// clip's full range of content (so a scene cut can't be the one frame diff --git a/lanes/av1enchang.report.md b/lanes/av1enchang.report.md new file mode 100644 index 00000000..ca38dd51 --- /dev/null +++ b/lanes/av1enchang.report.md @@ -0,0 +1,410 @@ +# lane-av1enchang — `bitrate_target_lands_within_5_percent_over_48_frames`: a MEASUREMENT, not a hang + +Branch `lane-av1enchang`, off `main` `3698787d`. One source file, +`crates/ec-av1/src/encoder.rs`, plus this report. + +## Verdict + +**The test is not hung, not stuck, and not a regression. It is a ~7 hour test in an +unoptimized build and it PASSES.** The "wedge" was a suite killed mid-test. Coordinator +evidence closes it: `/home/user/gates/wave3e-suite.log` line 180 reads +`test encoder::tests::bitrate_target_lands_within_5_percent_over_48_frames ... ok`, and the +same log ends `test result: ok. 713 passed; 0 failed; 49 ignored; 0 measured; 0 filtered +out; finished in 24341.37s` — 6.76 hours for the whole `ec-av1` lib suite. + +Independently reproduced here, and everything below is measured on this box, not inferred. + +| question | answer | evidence | +|---|---|---| +| Does it terminate? | **yes** | all 8 arms pass, 4m40s optimized, on unmodified `main` | +| Is it a regression? | **no** | same test, same 4 bitrates, byte-identical output at pin `ea8fa18f` | +| Why so slow? | **not a pathology** | `evals_per_search` is flat at 61.3 → 64.9 as `q_idx` falls | +| Is the search degenerating? | **no** | 61.3/64.5/64.9 evals/search across 384k/1.5M/2M — that is the search's designed bound | +| Is the motion-search call count the defect? | **no** | 1,885–2,711 searches/frame over 60 superblocks = 31–45/superblock, bounded | + +## What is landed + +1. **A documented backstop**, valued from a measured run, so a future *non-terminating* + encode fails this gate in 30 minutes instead of wedging a suite for hours. It is not + presented as a fix for the cost, because it is not one. +2. **A real, separately-justified encoder fix**: `Av1Encoder` was **139,960 bytes by value**; + it is now **18,168**. Two lines. It is a public-API stack hazard, unrelated to the wedge. +3. **A millisecond regression gate** for that fix, + `the_pyramid_constructors_fit_on_a_tight_stack`: both constructors are built on a + 1 MiB stack, which the old layout cannot survive and the new one clears with 2.7x + margin. + +## Finding 1 — the cost, measured and attributed + +### 1.1 Where the time goes: the motion-search census + +`motion.rs` already keeps a census (`searches`, `evals`, `subpel evals`, +`zero_mv`). A temporary probe (`tmp_mv_census`, since removed) read it per arm, optimized, +48 frames of 640x384, flat path: + +| arm | searches | evals | **evals/search** | evals/frame | s/frame | searches/frame | +|---|---|---|---|---|---|---| +| 384 000 bps (`q` walks **up**) | 90,495 | 5,547,925 | **61.3** | 115,582 | 0.53 | 1,885 | +| 1 536 000 bps (`q` → ~25) | 122,011 | 7,869,268 | **64.5** | 163,943 | 0.90 | 2,542 | +| 2 000 000 bps (`q` lower still) | 130,163 | 8,447,516 | **64.9** | 175,990 | 1.02 | 2,712 | + +`evals_per_search` is **flat**. A diamond log-step search over 8 neighbours plus the +half-pel and quarter-pel refinement stages has a designed bound right around 56–64 evals, +and the measured 61.3 → 64.9 sits on it. **The search is not degenerating and there is no +runaway call count to bound.** The cost is the plain product + +> full search x 60 superblocks (640/64 x 384/64) x 48 frames x 8 arms x ~13x debug penalty + +which is why the expensive arms are the **high-bitrate** ones: the controller must walk +`q_idx` *down* from its seed 100 into the 20s to spend an 8000-byte budget, and a +lower-`q` frame costs 1.4-1.5x more (163,943 vs 115,582 evals/frame). At 384k/768k the +loop pushes `q` *up* and those arms are cheap. That is why the first four arms of a +partially-completed run print and the fifth does not. + +### 1.2 Why the controller walks `q` down at all + +At its seed `base_q_idx` of 100 this clip codes **5467 bytes** for the key frame, and the +`1_536_000 bps` target is `1536000/8/24 = 8000` bytes/frame — the seed is *already under +budget*, so the loop must lower `q_idx` at `STEP_CLAMP`=12 per frame to spend the rest. +Per-frame probe, one arm, optimized: + +``` +TMP frame 7 at 0.0s (+0.00s) q=100 bytes_so_far=0 +TMP frame 8 at 0.8s (+0.78s) q=75 bytes_so_far=5467 +TMP frame 9 at 1.3s (+0.53s) q=63 bytes_so_far=9725 +TMP frame 11 at 2.9s (+0.83s) q=39 bytes_so_far=20520 +TMP frame 20 at 11.5s (+1.04s) q=32 bytes_so_far=91906 +TMP frame 30 at 20.5s (+0.88s) q=23 bytes_so_far=171230 +TMP frame 47 at 35.9s (+0.81s) q=27 bytes_so_far=308242 +TMP done bps=1536000 pyr=false coded=372465 +``` + +`q_idx` settles into a 22..36 band and **stops walking**. The same arm debug: `+10.79s, ++8.61s, +11.24s, +12.18s` at the same `q` — the ~13x profile ratio. + +### 1.3 Three gdb samples prove the thread is walking, not repeating + +Taken 20 s apart on the `1_536_000 bps` flat arm (the arm a short run is inside when it +looks wedged): + +``` +$ for i in 1 2 3; do gdb -p -batch -ex 'thread apply all bt'; sleep 20; done +``` + +Sample 1 (08:47:35) — MV-search / transform, block `x=192,y=80`, `base_q_idx=32`: + +``` +#8 ec_av1::transform::permute (t=0x7f9809f91490, n=4) at crates/ec-av1/src/transform.rs:355 +#9 ec_av1::transform::inverse_dct_n (t=0x7f9809f91490, r=16) at crates/ec-av1/src/transform.rs:413 +#11 ec_av1::transform::inverse_1d (t=0x7f9809f91490, log2=4, r=16, kind=ec_av1::transform::TxType1d::Dct) at crates/ec-av1/src/transform.rs:1103 +#15 ec_av1::transform::inverse_transform_2d_typed_wh (dequant=..., w=16, h=16, bit_depth=8, tx_type=ec_av1::transform::TxType::DctDct) at crates/ec-av1/src/transform.rs:1171 +#19 ec_av1::transform::dequant_and_inverse_typed_wh (levels=..., w=16, h=16, bit_depth=8, q_idx=32, dc_delta=0, ac_delta=0, tx_type=…DctDct, qm=…) at crates/ec-av1/src/transform.rs:1375 +#20 ec_av1::encode::Plane::code_from_prediction_typed (self=0x7f9809f9c460, x=192, y=80, side=16, prediction=..., skip=false, base_q_idx=32, deadzone=0.5, set=ec_av1::cdf_state::TxbSet::Chroma16, tx_type=…DctDct) at crates/ec-av1/src/encode.rs:3109 +#22 ec_av1::encode::mc_trial_compound (plane=0x7f9809f9c460, x=192, y=80, side=16, luma=false, ref0=..., ref1=..., base_q_idx=32, …) at crates/ec-av1/src/encode.rs:10435 +#23 ec_av1::encode::search_inter_block (luma=0x7f9809f9c340, chroma=0x7f9809f9c3d0, search=0x7f9809f9eb28, mode_bits=0x7f9809fa6998, reference=0x7f980a01e6b0, stack=0x7f9809f9ec10, extra=..., compound=..., grid=0x7f9809f9c610, fctx=0x7f980a0403d0) at crates/ec-av1/src/encode.rs:12331 +#24 ec_av1::encode::encode_inter_frame::{closure#19} (index=0, fctx=0x7f980a01e6b0) at crates/ec-av1/src/encode.rs:14673 +#25 ec_av1::encode::search_tiles::{closure#0} (index=0) at crates/ec-av1/src/encode.rs:8704 +``` + +Sample 2 (08:47:55) — a different function and a different block: + +``` +#5 ec_av1::encode::intra_predict_u8::{closure#0} (src=..., buf=0x7f9809f90aee) at crates/ec-av1/src/encode.rs:1691 +#6 ec_av1::encode::intra_predict_u8 (mode=2, angle_delta=0, above=..., left=..., corner=..., bw=8, bh=8, enable_edge_filter=false, smooth_neighbor=false, dst=..., fctx=0x7f980a0403d0) at crates/ec-av1/src/encode.rs:1696 +#7 ec_av1::encode::{impl#2}::search_block::{closure#0} () at crates/ec-av1/src/encode.rs:3564 +#18 ec_av1::encode::Plane::search_block (self=0x7f9809f9c340, at=..., search=0x7f9809f9eb28, mode_bits=0x7f9809fa6998, fctx=0x7f980a0403d0) at crates/ec-av1/src/encode.rs:3588 +#19 ec_av1::encode::code_square (luma=0x7f9809f9c340, chroma=0x7f9809f9c3d0, side=8, search=0x7f9809f9eb28, mode_bits=0x7f9809fa6998, tx_select=true, ibc=..., census_key=false, fctx=0x7f980a0403d0) at crates/ec-av1/src/encode.rs:6271 +#20 ec_av1::encode::code_square_inter (luma=0x7f9809f9c340, chroma=0x7f9809f9c3d0, side=8, search=0x7f9809f9eb28, mode_bits=0x7f9809fa6998, reference=0x7f980a01e6b0, stack=0x7f9809f9a550, compound=..., grid=0x7f9809f9c610, refs=0x7f9809f9c698, fctx=0x7f980a0403d0) at crates/ec-av1/src/encode.rs:7266 +#25 ec_av1::encode::encode_inter_frame::{closure#19} (index=0, fctx=0x7f980a0403d0) at crates/ec-av1/src/encode.rs:14724 +``` + +Sample 3 (08:48:16) — a third block, and a **later frame's** quantizer: + +``` +#1 ec_av1::tile::base_ctx (grid=..., side=32, row=6, col=30, class=ec_av1::decode::TxClass::TwoD) at crates/ec-av1/src/tile.rs:6759 +#14 ec_av1::tile::coeff_bits_typed (grid=..., set=ec_av1::cdf_state::TxbSet::Luma32Inter, q_ctx=1, skip_ctx=0, sign_ctx=1, tx_type=…DctDct) at crates/ec-av1/src/tile.rs:6004 +#15 ec_av1::tile::rdoq::price (levels=..., dense=..., side=32, coded=32, set=…Luma32Inter, q_ctx=1, skip_ctx=0, sign_ctx=1, tx_type=…DctDct) at crates/ec-av1/src/tile.rs:6116 +#16 ec_av1::tile::rdoq (levels=..., scaled=..., side=32, base_q_idx=30, set=…Luma32Inter, skip_ctx=0, sign_ctx=1, lambda=0.060500000000000005, tx_type=…DctDct) at crates/ec-av1/src/tile.rs:6176 +#17 ec_av1::encode::Plane::code_from_prediction_typed (self=0x7f9809f9c340, x=256, y=128, side=32, prediction=..., skip=false, base_q_idx=30, deadzone=0.5, set=…Luma32Inter, tx_type=…DctDct) at crates/ec-av1/src/encode.rs:3083 +#19 ec_av1::encode::mc_trial (plane=0x7f9809f9c340, x=256, y=128, side=32, mv=..., luma=true, reference=..., stride=640, ref_width=640, ref_height=384, skip=false, base_q_idx=30, …) at crates/ec-av1/src/encode.rs:10341 +#20 ec_av1::encode::search_inter_block (luma=0x7f9809f9c340, chroma=0x7f9809f9c3d0, search=0x7f9809f9eb28, mode_bits=0x7f9809fa6998, reference=0x7f980a01e6b0, stack=0x7f9809f9ec10, extra=..., compound=..., grid=0x7f9809f9c610, fctx=0x7f980a0403d0) at crates/ec-av1/src/encode.rs:12407 +``` + +Three samples, three functions, three blocks, and `base_q_idx` **32, 32, 30** — the `30` +can only be a *later frame's*, because `RateLoop::update` runs once per coded frame. This +is consistent with, and independently confirms, the coordinator's own 100%-CPU +`search_root_128_rect -> search_skip_64 -> motion::search` reading. + +### 1.4 Not a regression + +Same test, same four bitrates, run at the older pin in a detached worktree: + +``` +$ git worktree add /home/user/.cache/wt/av1pin ea8fa18f --detach +$ cargo test -p ec-av1 --release --lib -- bitrate_target_lands_within_5_percent_over_48_frames +bitrate 384000 bps, pyramid false: 94646 bytes over 2.00s = 378584 bps (-1.4%) | … leaf 47x1897 +bitrate 384000 bps, pyramid true: 97738 bytes over 2.00s = 390952 bps (+1.8%) | … leaf 35x648 +bitrate 768000 bps, pyramid false: 187877 bytes over 2.00s = 751508 bps (-2.1%) | … leaf 47x3881 +bitrate 768000 bps, pyramid true: 191961 bytes over 2.00s = 767844 bps (-0.0%) | … leaf 35x1536 +bitrate 1536000 bps, pyramid false: 372465 bytes over 2.00s = 1489860 bps (-3.0%) | … leaf 47x7808 +bitrate 1536000 bps, pyramid true: 384161 bytes over 2.00s = 1536644 bps (+0.0%) | … leaf 35x3076 +bitrate 2000000 bps, pyramid false: 491321 bytes over 2.00s = 1965284 bps (-1.7%) | … leaf 47x10337 +bitrate 2000000 bps, pyramid true: 500299 bytes over 2.00s = 2001196 bps (+0.1%) | … leaf 35x6394 +test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 782 filtered out; finished in 275.53s +real 4m35.622s +``` + +**Byte-identical to current `main`**, arm for arm. Nothing changed; the gate has always +been this expensive. + +### 1.5 Reproduce, for the record + +``` +$ export CARGO_TARGET_DIR=/home/user/.cache/tgt/av1enchang2 EC_NOMEMGUARD=1 \ + EC_FIXTURES=/home/user/Documents/Code/Rust/edith_codecs/fixtures \ + EC_REQUIRE_FIXTURES=1 +$ timeout 1800 cargo test -p ec-av1 --lib -- bitrate_target_lands_within_5_percent_over_48_frames \ + --test-threads=1 --nocapture +running 1 test +test encoder::tests::bitrate_target_lands_within_5_percent_over_48_frames ... +bitrate 384000 bps, pyramid false: 94646 bytes … (-1.4%) +bitrate 384000 bps, pyramid true: 97738 bytes … (+1.8%) +bitrate 768000 bps, pyramid false: 187877 bytes … (-2.1%) +bitrate 768000 bps, pyramid true: 191961 bytes … (-0.0%) +EXIT=124 <-- 4 of 8 arms in 30 minutes; the fifth is simply still running +``` + +Two traps worth recording, both of which produce a false "it hung" reading: + +* **Without a fixture root the gate does not run and libtest still says `ok`**: + `test … SKIP …: h264-1080p-23.976-8bit.mp4 absent at …` / `test result: ok. 1 passed … + finished in 0.06s`. +* **Without `--nocapture`, libtest buffers the per-arm `eprintln!` lines**, so a run that + has done real work prints only the test name. That is what a wedge looks like in a log. + +## Finding 2 — the landed fix: a backstop, valued from a measured run + +The gate had no way to *fail*. A future non-terminating encode would wedge a suite for +hours with no diagnostic — which is how wave3j and wave3h were lost. So each arm's encode +now runs on a **worker thread** and the test thread watches a **per-CALL** deadline over an +`mpsc` channel: the worker sends a progress ticket after every `encode_frames` call and +its result after `flush` (which is itself a call that can spin, so it gets a ticket too, +not a bare `join` that would block forever). On timeout the gate panics. + +* `ArmResult` (targets/coded/bytes/count) carries the tally off the worker. +* `pictures` is an `Arc`, not an 8x 35 MB clone. +* Worker is a plain `std::thread::spawn` — the std default 2 MiB stack. libtest spawns + its test thread with `thread::Builder` and **no** `.stack_size()`, so the encode gets a + byte-identical stack to before: the backstop neither introduces nor masks a stack + problem. + +### The budget, and its margin + +**The budget is per call, not per test** — that distinction is the whole reason this shape +is safe. An earlier 300 s value was wrong for the pyramid arms and has been raised to +**1800 s**. + +| | worst single `encode_frames` call | budget | margin | +|---|---|---|---| +| optimized, flat arms | **1.04 s** (measured, frame 20 at `q_idx` 32) | 1800 s | **1730x** | +| debug, flat arms | **12.2 s** (measured) | 1800 s | **148x** | +| debug, pyramid arms | **~110 s** (derived: a pyramid call codes a whole mini-GOP, ~9 frames, at 12.2 s each) | 1800 s | **~16x** | + +Whole-test wall time for reference: **280.60 s optimized** for all eight arms on unmodified +`main` (`finished in 280.60s`, `real 4m40.700s`), 275.53 s at `ea8fa18f`, 311.29 s on this +branch. A host ~16x slower than this workstation still clears a 1800 s per-call budget, so +the default cannot turn a correct-but-slow build red. + +### The red, both directions + +**Red 1 — the timeout path is live on the real workload** (budget forced under the honest +cost, so it fires inside a healthy run): + +``` +$ EC_AV1_ENCODE_CALL_BUDGET_SECS=1 … --nocapture +thread 'encoder::tests::bitrate_target_lands_within_5_percent_over_48_frames' panicked at crates/ec-av1/src/encoder.rs:3551:76: +AV1 bitrate gate SLOW, NOT HUNG: the 384000 bps pyramid false arm's encode did not return +within 1s (arm elapsed 22s, stuck on call 49 of 49 -- the call that never came back). … +FAILED +test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 788 filtered out; finished in 21.79s +EXIT=101 +``` + +It fails loudly and never as `ok`: the test name in the panic, the arm name, the arm's +elapsed seconds, the stuck call index, `FAILED`, and `EXIT=101`. + +**Red 2 — a genuine non-terminating encode at the default budget.** A +`loop { std::hint::spin_loop(); }` injected at the top of `Av1Encoder::encode_frames` +(temporary; the tree now greps **0** hits for `EC_AV1_INJECTED_SPIN`): + +``` +$ EC_AV1_INJECTED_SPIN=1 … --nocapture +thread '…' panicked at crates/ec-av1/src/encoder.rs:3535:76: +AV1 encode did not return within 300s, on call 1 of 49 (384000 bps pyramid false). … +test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 788 filtered out; finished in 300.16s +real 5m0.249s +user 9m59.831s +EXIT=101 +``` + +(The 300 s in that transcript is the budget value at the time of the run; the default is +now 1800 s and the same injection still fires.) Pre-fix, that input wedges the suite +forever; post-fix it is a bounded RED. `user 9m59s` against `real 5m0s` is the two-CPU +burn signature the wedged hosts showed. + +**Green**, default budget, all eight arms, **byte-identical to the pre-fix run**: + +``` +bitrate 384000 bps, pyramid false: 94646 bytes … (-1.4%) +bitrate 384000 bps, pyramid true: 97738 bytes … (+1.8%) +bitrate 768000 bps, pyramid false: 187877 bytes … (-2.1%) +bitrate 768000 bps, pyramid true: 191961 bytes … (-0.0%) +bitrate 1536000 bps, pyramid false: 372465 bytes … (-3.0%) +bitrate 1536000 bps, pyramid true: 384161 bytes … (+0.0%) +bitrate 2000000 bps, pyramid false: 491321 bytes … (-1.7%) +bitrate 2000000 bps, pyramid true: 500299 bytes … (+0.1%) +test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 788 filtered out; finished in 311.29s +EXIT=0 +``` + +## Finding 3 — a 137 KB `Av1Encoder` aborts on a default-sized stack + +**This is a separate defect and is NOT the fix for the wedge.** It is landed on its own +merits. Reproduced twice on the pre-fix binary built from unmodified `main`, 6m18s and +6m35s, by invoking the test binary **directly** rather than through `cargo test`: + +``` +$ /home/user/.cache/tgt/av1enchang/debug/deps/ec_av1-79bef263d07a873f \ + bitrate_target_lands_within_5_percent_over_48_frames --test-threads=1 --nocapture +test … bitrate 384000 bps, pyramid false: 94646 bytes … (-1.4%) +thread 'encoder::tests::bitrate_target_lands_within_5_percent_over_48_frames' (2394233) has overflowed its stack +fatal runtime error: stack overflow, aborting +real 6m35.006s +``` + +Not the encode — the **constructor**: + +``` +$ coredumpctl debug 2394232 +Thread 1 (Thread 0x7f00483d66c0 (LWP 2394233)): +#6 +#8 ec_av1::encoder::Av1Encoder::new (config=...) at crates/ec-av1/src/encoder.rs:1519 +#9 ec_av1::encoder::Av1Encoder::with_pyramid (config=..., pyramid=...) at crates/ec-av1/src/encoder.rs:1583 +#10 ec_av1::encoder::Av1Encoder::with_pyramid_and_rate_target (config=..., pyramid=..., rate=...) at crates/ec-av1/src/encoder.rs:1615 +#11 ec_av1::encoder::tests::bitrate_target_lands_within_5_percent_over_48_frames () at crates/ec-av1/src/encoder.rs:3427 +``` + +### 3.1 The regression gate, and the measured threshold sweep + +Proving the old layout aborts by re-running the 48-frame gate takes 6m35s and needs a +process that dies. A millisecond gate says the same thing: build **both** constructors on a +thread with a deliberately tight stack. New test +`the_pyramid_constructors_fit_on_a_tight_stack` does exactly that, and both thresholds were +swept in an unoptimized build (the Box temporarily reverted to get the pre-fix column): + +| stack | pre-fix (`CdfSnapshot` by value) | post-fix (boxed) | +|---|---|---| +| 256 KiB | overflow | overflow | +| 384 KiB | overflow | **ok** | +| 512 KiB | overflow | ok | +| **1 MiB** | **overflow** | **ok** | +| 2 MiB (libtest's default) | **overflow** | ok | +| 3 MiB | ok | ok | + +The gate runs at **1 MiB**: 2.7x what the boxed layout needs and 2x below what the old one +did, so it is nowhere near either threshold and cannot flake. The pre-fix row at 1 MiB and +at 2 MiB is the red. + +``` +$ EC_AV1_TIGHT_STACK_BYTES=1048576 … the_pyramid_constructors_fit_on_a_tight_stack --nocapture +TIGHTSTACK 1048576 bytes: Av1Encoder=18168 bytes, both constructors returned +test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 789 filtered out; finished in 0.00s +``` + +Sizes, measured: + +``` +BEFORE: SIZE Av1Encoder=139960 Result=139960 +AFTER: SIZE Av1Encoder=18168 Result=18168 +``` + +The 137 KB is `dpb: [Option; 8]`, each `DpbSlot` holding a 15 KB `CdfSnapshot` by +value. `Av1Encoder` is returned by value through three nested `Result` constructors +(`with_pyramid_and_rate_target` -> `with_pyramid` -> `new`), so an unoptimized build puts +several 137 KB copies on the stack at once. The flat path nests one level fewer and fits. + +### Why `cargo test` never sees it, and why it is still real + +`.cargo/config.toml` already carries the mitigation, and its comment already names this +exact failure: + +```toml +[env] +# The AV1 rate-target tests keep several frames' worth of encoder state on the +# test thread's stack; the 2 MB default overflows in debug builds (release +# passes). Cap lives in the repo so every checkout and worktree gets it. +RUST_MIN_STACK = "67108864" +``` + +`cargo test` hands the test thread 64 MiB, so the abort does not happen there. **This is +also why the `cargo test` runs wedge rather than abort**, and it is the direct answer to +the `memguard-runner.sh` question: that runner is a **cgroup `MemoryMax=10G` / +`MemorySwapMax=2G` cap, not `RLIMIT_AS`**, and the test uses far less than 10 GB. It cannot +turn a guard-page hit into a sustained hang. A guard-page hit aborts; it does not sit in +`R` for three hours. + +The defect is still real: `RUST_MIN_STACK` is a `[env]` entry that applies only to **this +workspace's** `cargo` invocations. Any external consumer linking `ec-av1` and calling +`Av1Encoder::new(...)` on an ordinary 2 MiB thread stack **crashes the process**. The repo +is hiding a public-API hazard behind a workspace-local env var. + +### The fix, and its evidence + +`DpbSlot::cdfs` becomes `Box`; the one construction site +(`refresh()`) wraps it in `Box::new`. Two lines. + +``` +$ cargo test -p ec-av1 --release --lib -- round_trip encode +test result: ok. 108 passed; 0 failed; 40 ignored; 0 measured; 641 filtered out; finished in 503.15s +``` + +**108 passed, 0 failed** — re-run *after* the change, not asserted. `size_of` before/after +is above. `cargo check -p ec-av1 --all-targets` is clean with no warnings. Nothing in the +suite was red for an unrelated reason. + +## Commands + +``` +git -C /home/user/Documents/Code/Rust/edith_codecs worktree add \ + /home/user/.cache/wt/av1enchang -b lane-av1enchang main +git -C /home/user/Documents/Code/Rust/edith_codecs worktree add \ + /home/user/.cache/wt/av1pin ea8fa18f --detach # the not-a-regression control +export EC_FIXTURES=/home/user/Documents/Code/Rust/edith_codecs/fixtures +export EC_REQUIRE_FIXTURES=1 + +cargo check -p ec-av1 --all-targets +cargo test -p ec-av1 --lib -- bitrate_target_lands_within_5_percent_over_48_frames --test-threads=1 --nocapture +cargo test -p ec-av1 --release --lib -- bitrate_target_lands_within_5_percent_over_48_frames --test-threads=1 --nocapture +EC_AV1_ENCODE_CALL_BUDGET_SECS=1 … # red 1 +EC_AV1_INJECTED_SPIN=1 … # red 2 (injection reverted, 0 grep hits) +coredumpctl debug # the Finding 3 backtrace +cargo test -p ec-av1 --release --lib -- round_trip encode +``` + +## Decision wanted from the coordinator (not taken here) + +The honest cost is now measured and the gate is bounded, but **the ~7 hour debug suite +remains**. That is a test-design call, and it changes what the default suite proves, so it +is not this lane's to make: + +* **leave it** — the claim stays fully in the default debug suite; gate scheduling must + budget ~7 h (measured 24341 s on the fleet). +* **`#[ignore]` with a stated reason, run in release** — 4m40s instead of hours, at the cost + of the claim no longer running by default. +* **split it** — e.g. a cheap 2-bitrate/short-clip arm in the default suite plus the full + 4x2x48 sweep behind a flag or a release-only gate. + +Recommendation: **split**. It keeps a rate-control regression signal in the default debug +suite at a cost the suite can absorb, and keeps the full ±5% sweep available where it can +be afforded. Not implemented here — it changes what the gate proves, which is the +coordinator's call.