Pin the worker pool's thread count across repeated decodes
Browse files at this commit44460dbbd528d13b402017cc0fd63d943998b65bParent: 2636658- test
- pin the worker pool's thread count across repeated decodes
2 changed files +53 -0
- crates/ec-av1/src/par.rs +1 -0
- crates/ec-av1/src/stream.rs +52 -0
crates/ec-av1/src/par.rsmodified+1 -0
| @@ -297,6 +297,7 @@ fn pool_push(inner: &std::sync::Arc<PoolInner>, job: Job, class: &'static str) { | |||
| 297 | /// asserts on, and what a profiler counts as `ec-av1-*`. | 297 | /// asserts on, and what a profiler counts as `ec-av1-*`. |
| 298 | static POOL_THREADS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); | 298 | static POOL_THREADS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); |
| 299 | 299 | ||
| 300 | +#[cfg(test)] | ||
| 300 | pub(crate) fn pool_threads() -> usize { | 301 | pub(crate) fn pool_threads() -> usize { |
| 301 | POOL_THREADS.load(std::sync::atomic::Ordering::Relaxed) | 302 | POOL_THREADS.load(std::sync::atomic::Ordering::Relaxed) |
| 302 | } | 303 | } |
crates/ec-av1/src/stream.rsmodified+52 -0
| @@ -6364,6 +6364,58 @@ mod tests { | |||
| 6364 | } | 6364 | } |
| 6365 | } | 6365 | } |
| 6366 | 6366 | ||
| 6367 | + /// lane-memfix: the pool grows to the peak of `busy + queued` over the | ||
| 6368 | + /// job tree and stops there. Before, a job running on a pool worker | ||
| 6369 | + /// opened a SECOND pool on that worker's own thread-local -- one level | ||
| 6370 | + /// per pipeline stage, none of them ever shrinking -- which is how a 4K | ||
| 6371 | + /// decode at `(16, 4, 2)` reached 1038 live threads. Two decodes of the | ||
| 6372 | + /// same stream: the count stays inside the composed setting and the | ||
| 6373 | + /// second decode adds nothing (the ratchet is what cost the memory). | ||
| 6374 | + #[test] | ||
| 6375 | + fn the_worker_pool_stays_bounded_across_repeated_threaded_decodes() { | ||
| 6376 | + const NAME: &str = "the_worker_pool_stays_bounded_across_repeated_threaded_decodes"; | ||
| 6377 | + let _gate_lock = lock_gate_counters(); | ||
| 6378 | + let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR")) | ||
| 6379 | + .join("fixtures") | ||
| 6380 | + .join("superres_alltools_sb128_320x180.obu"); | ||
| 6381 | + let stream = std::fs::read(&path) | ||
| 6382 | + .unwrap_or_else(|e| panic!("{NAME}: reading {}: {e}", path.display())); | ||
| 6383 | + crate::par::set_filter_threads(4); | ||
| 6384 | + crate::par::set_recon_threads(2); | ||
| 6385 | + let mut counts = [0usize; 2]; | ||
| 6386 | + let mut frames = [0usize; 2]; | ||
| 6387 | + for (i, slot) in counts.iter_mut().enumerate() { | ||
| 6388 | + let mut n = 0usize; | ||
| 6389 | + let r = decode_stream_with_threads(&stream, 4, |_, _, _| { | ||
| 6390 | + n += 1; | ||
| 6391 | + Ok(()) | ||
| 6392 | + }); | ||
| 6393 | + if let Err(e) = r { | ||
| 6394 | + crate::par::set_filter_threads(1); | ||
| 6395 | + crate::par::set_recon_threads(1); | ||
| 6396 | + panic!("{NAME}: refused on decode {i}: {e}"); | ||
| 6397 | + } | ||
| 6398 | + frames[i] = n; | ||
| 6399 | + *slot = crate::par::pool_threads(); | ||
| 6400 | + } | ||
| 6401 | + crate::par::set_filter_threads(1); | ||
| 6402 | + crate::par::set_recon_threads(1); | ||
| 6403 | + assert!(frames[0] > 1, "{NAME}: the fixture decoded {} frames, so nothing was threaded", frames[0]); | ||
| 6404 | + // 4 frame workers x (recon 2 + filter 4) plus the two in-flight | ||
| 6405 | + // slots `stream.rs` allows over the thread count, with room for the | ||
| 6406 | + // show jobs -- 1038 was three orders past this. | ||
| 6407 | + assert!( | ||
| 6408 | + counts[1] <= 4 * (2 + 4) + 2 * 4, | ||
| 6409 | + "{NAME}: the pool grew to {} threads at 4 frame / 4 filter / 2 recon", | ||
| 6410 | + counts[1] | ||
| 6411 | + ); | ||
| 6412 | + assert_eq!( | ||
| 6413 | + counts[0], counts[1], | ||
| 6414 | + "{NAME}: a second decode of the same stream grew the pool from {} to {} threads", | ||
| 6415 | + counts[0], counts[1] | ||
| 6416 | + ); | ||
| 6417 | + } | ||
| 6418 | + | ||
| 6367 | #[test] | 6419 | #[test] |
| 6368 | fn a_real_stream_filters_identically_with_one_and_four_filter_threads() { | 6420 | fn a_real_stream_filters_identically_with_one_and_four_filter_threads() { |
| 6369 | const NAME: &str = "a_real_stream_filters_identically_with_one_and_four_filter_threads"; | 6421 | const NAME: &str = "a_real_stream_filters_identically_with_one_and_four_filter_threads"; |