From 44460dbbd528d13b402017cc0fd63d943998b65b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Sun, 6 Sep 2026 02:49:00 +0300 Subject: [PATCH] test(av1): pin the worker pool's thread count across repeated decodes Two threaded decodes of one stream at 4 frame / 4 filter / 2 recon threads: the pool stays inside the composed setting and the second decode adds no thread. The nested-pool shape this replaces had no bound at all. --- crates/ec-av1/src/par.rs | 1 + crates/ec-av1/src/stream.rs | 52 +++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/crates/ec-av1/src/par.rs b/crates/ec-av1/src/par.rs index e048e264..e16ac1d1 100644 --- a/crates/ec-av1/src/par.rs +++ b/crates/ec-av1/src/par.rs @@ -297,6 +297,7 @@ fn pool_push(inner: &std::sync::Arc, job: Job, class: &'static str) { /// asserts on, and what a profiler counts as `ec-av1-*`. static POOL_THREADS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); +#[cfg(test)] pub(crate) fn pool_threads() -> usize { POOL_THREADS.load(std::sync::atomic::Ordering::Relaxed) } diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index 35aa354c..48ea523d 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -6364,6 +6364,58 @@ mod tests { } } + /// lane-memfix: the pool grows to the peak of `busy + queued` over the + /// job tree and stops there. Before, a job running on a pool worker + /// opened a SECOND pool on that worker's own thread-local -- one level + /// per pipeline stage, none of them ever shrinking -- which is how a 4K + /// decode at `(16, 4, 2)` reached 1038 live threads. Two decodes of the + /// same stream: the count stays inside the composed setting and the + /// second decode adds nothing (the ratchet is what cost the memory). + #[test] + fn the_worker_pool_stays_bounded_across_repeated_threaded_decodes() { + const NAME: &str = "the_worker_pool_stays_bounded_across_repeated_threaded_decodes"; + let _gate_lock = lock_gate_counters(); + let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR")) + .join("fixtures") + .join("superres_alltools_sb128_320x180.obu"); + let stream = std::fs::read(&path) + .unwrap_or_else(|e| panic!("{NAME}: reading {}: {e}", path.display())); + crate::par::set_filter_threads(4); + crate::par::set_recon_threads(2); + let mut counts = [0usize; 2]; + let mut frames = [0usize; 2]; + for (i, slot) in counts.iter_mut().enumerate() { + let mut n = 0usize; + let r = decode_stream_with_threads(&stream, 4, |_, _, _| { + n += 1; + Ok(()) + }); + if let Err(e) = r { + crate::par::set_filter_threads(1); + crate::par::set_recon_threads(1); + panic!("{NAME}: refused on decode {i}: {e}"); + } + frames[i] = n; + *slot = crate::par::pool_threads(); + } + crate::par::set_filter_threads(1); + crate::par::set_recon_threads(1); + assert!(frames[0] > 1, "{NAME}: the fixture decoded {} frames, so nothing was threaded", frames[0]); + // 4 frame workers x (recon 2 + filter 4) plus the two in-flight + // slots `stream.rs` allows over the thread count, with room for the + // show jobs -- 1038 was three orders past this. + assert!( + counts[1] <= 4 * (2 + 4) + 2 * 4, + "{NAME}: the pool grew to {} threads at 4 frame / 4 filter / 2 recon", + counts[1] + ); + assert_eq!( + counts[0], counts[1], + "{NAME}: a second decode of the same stream grew the pool from {} to {} threads", + counts[0], counts[1] + ); + } + #[test] fn a_real_stream_filters_identically_with_one_and_four_filter_threads() { const NAME: &str = "a_real_stream_filters_identically_with_one_and_four_filter_threads";