diff --git a/crates/engine-audio/src/lib.rs b/crates/engine-audio/src/lib.rs index 7cf435f..559df2c 100644 --- a/crates/engine-audio/src/lib.rs +++ b/crates/engine-audio/src/lib.rs @@ -157,6 +157,11 @@ struct Shared { /// per callback on the RT thread, so it is an atomic rather than a lock. gain: AtomicU32, underruns: AtomicU64, + /// Device position (samples per channel) read at the most recent underrun, + /// `-1` until one happens: what says whether the count in hand is old news + /// or is still growing. Written on the RT thread, and only on the starved + /// path, so the ordinary callback pays nothing for it. + last_underrun: AtomicI64, } enum Ctl { @@ -251,6 +256,12 @@ fn run( // that silence, so it really is elapsed time. slice[got * 4..want * 4].fill(0); shared.underruns.fetch_add(1, Ordering::Relaxed); + // The position this callback is about to compute is + // not known yet; the previous one is a quantum + // (~23 ms) behind and needs no clock call here. + shared + .last_underrun + .store(shared.position.load(Ordering::Relaxed), Ordering::Relaxed); } // After the fill, so the silence stays silent whatever // the gain is, and the underrun count keeps meaning @@ -410,6 +421,7 @@ pub extern "C" fn ao_open(sample_rate: u32, channels: u32) -> *mut c_void { resumed: AtomicBool::new(false), gain: AtomicU32::new(1.0f32.to_bits()), underruns: AtomicU64::new(0), + last_underrun: AtomicI64::new(-1), }); let (ready_tx, ready_rx) = mpsc::channel(); let (ctl_tx, ctl_rx) = mpsc::channel(); @@ -479,6 +491,34 @@ pub unsafe extern "C" fn ao_position(session: *mut c_void) -> i64 { .unwrap_or(-1) } +/// Starved callbacks since the stream opened -- one per quantum the decoder was +/// late for -- and, through `last_at`, the device position the newest of them +/// was seen at (`-1` if there has been none). The same number the drop print +/// reports, asked for while the session is still running: a caller measuring a +/// seek reads it before and after and the difference is that seek's cost. +/// +/// Two relaxed atomic loads, nothing else: the RT thread writes both and must +/// never wait on a reader. +/// +/// # Safety +/// `session` must come from [`ao_open`]; `last_at` is either null or writable. +#[unsafe(no_mangle)] +pub unsafe extern "C" fn ao_underruns(session: *mut c_void, last_at: *mut i64) -> i64 { + catch_unwind(AssertUnwindSafe(|| { + if session.is_null() { + return -1; + } + // SAFETY: caller-guaranteed live session. + let session = unsafe { &*(session as *const Session) }; + if !last_at.is_null() { + // SAFETY: caller-guaranteed writable, and it is a plain i64. + unsafe { *last_at = session.shared.last_underrun.load(Ordering::Relaxed) }; + } + session.shared.underruns.load(Ordering::Relaxed) as i64 + })) + .unwrap_or(-1) +} + /// Pauses (0) or resumes (non-zero) playback. Paused freezes the position. /// 0 on success, negative on failure. /// diff --git a/crates/engine/examples/bench.rs b/crates/engine/examples/bench.rs index ae88085..7672226 100644 --- a/crates/engine/examples/bench.rs +++ b/crates/engine/examples/bench.rs @@ -1,5 +1,6 @@ //! The baseline every later decode/encode change is measured against: open, -//! seek, scrub and export, timed on the real library rather than on a fixture. +//! seek, seek storm, play loop, scrub and export, timed on the real library +//! rather than on a fixture. //! //! `cargo run --release -p engine --example bench -- [args]` //! @@ -9,7 +10,10 @@ //! failure down as data. //! //! Every row is one TSV line -- `file, metric, unit, n, median, min, max, note` -//! -- so the whole baseline is `cat`ted together and diffed later. +//! -- so the whole baseline is `cat`ted together and diffed later. The note is +//! where the tail percentile and the **decode backend** live: a row that does +//! not say `backend hw` was measured in software, and a baseline that does not +//! say which is not a baseline (see `scripts/bench.sh` on `LD_LIBRARY_PATH`). //! //! **Cold and warm.** Cold means the file's pages are dropped from the page //! cache immediately before the measurement, with `posix_fadvise(DONTNEED)` on @@ -49,6 +53,8 @@ fn main() { match metric.as_str() { "open" => open_bench(&path), "seek" => seek_bench(&path, arg_f64(args.next(), "seconds")), + "seekstorm" => seekstorm_bench(&path, arg_seed(args.next())), + "playloop" => playloop_bench(&path, arg_f64(args.next(), "seconds"), arg_seed(args.next())), "scrub" => scrub_bench(&path), "waveform" => waveform_bench(&path), "export" => { @@ -63,6 +69,8 @@ fn main() { fn usage() -> ! { eprintln!( "usage: bench open \n bench seek \n \ + bench seekstorm [seed]\n \ + bench playloop [seed]\n \ bench scrub \n bench waveform \n \ bench export " ); @@ -74,6 +82,12 @@ fn arg_f64(arg: Option, what: &str) -> f64 { .unwrap_or_else(|| panic!("missing {what}")) } +/// The seed the random metrics draw their positions from: same seed, same +/// seeks, so a before and an after are the same measurement. +fn arg_seed(arg: Option) -> u64 { + arg.and_then(|s| s.parse().ok()).unwrap_or(1) +} + // ---------------------------------------------------------------- reporting /// One TSV row: the median is what a comparison uses, the min and max are what @@ -125,6 +139,34 @@ fn ttff_timeout() -> Duration { Duration::from_secs_f64(env_f64("BENCH_TTFF_TIMEOUT", 180.0)) } +/// The p'th percentile of `samples`, nearest rank on the sorted copy. The +/// median is [`row`]'s business; this is for the tail, which is the number a +/// dropped frame and a starved ring both live in. +fn pct(samples: &[f64], p: f64) -> f64 { + if samples.is_empty() { + return 0.0; + } + let mut sorted = samples.to_vec(); + sorted.sort_by(f64::total_cmp); + sorted[((sorted.len() - 1) as f64 * p).round() as usize] +} + +/// A seeded LCG, so "random" seeks are the same random seeks tomorrow. Sixteen +/// lines of a crate would do the same thing and none of the quality a real +/// generator has matters to a list of positions in a film. +struct Lcg(u64); + +impl Lcg { + /// The next position, in `0.0..1.0`. + fn next(&mut self) -> f64 { + self.0 = self + .0 + .wrapping_mul(6_364_136_223_846_793_005) + .wrapping_add(1_442_695_040_888_963_407); + (self.0 >> 11) as f64 / (1u64 << 53) as f64 + } +} + fn env_f64(key: &str, default: f64) -> f64 { std::env::var(key) .ok() @@ -250,6 +292,220 @@ fn seek_bench(path: &Path, secs: f64) { row(path, &format!("seek_ttff_{secs:.0}s"), "ms", &samples, ¬e); } +// -------------------------------------------------------------- seek storm + +/// Seeks a film opens with none of: fifty positions nobody chose, drawn from a +/// seed so the same fifty come back tomorrow. `seek` measures one point five +/// times over; this measures the spread across the whole file, which is where +/// an index lookup, a keyframe distance and a decoder reopen all vary. +/// +/// Cold is the first pass over a freshly evicted file, warm the same fifty +/// again straight after -- the same two questions the other metrics ask, and +/// the audio underruns each pass cost are counted with it +/// ([`engine::PlaybackSession::audio_underruns`]), because a seek that lands +/// fast and starves the ring is not a seek that worked. +const STORM_SEEKS: usize = 50; + +fn seekstorm_bench(path: &Path, seed: u64) { + evict(path); + let mut session = match engine::PlaybackSession::open(path) { + Ok(session) => session, + Err(e) => { + row(path, "seekstorm_ttff_cold", "ms", &[], &format!("FAIL({e})")); + return; + } + }; + // Playing throughout, because the underruns are the point: a paused device + // runs no callbacks and can never starve, so a storm measured paused would + // report zero however badly the seeks fed it. This is the complaint's own + // shape -- a film that is playing, jumped around in. + session.play(); + let duration = session.timeline_duration(); + let mut rng = Lcg(seed); + // Short of the very end: a landing with no picture after it is a different + // measurement (end of stream), and every seek here is meant to be one. + let targets: Vec = (0..STORM_SEEKS) + .map(|_| rng.next() * duration * 0.95) + .collect(); + + for pass in ["cold", "warm"] { + let before = session.audio_underruns().map_or(0, |(n, _)| n); + let mut samples = Vec::new(); + let mut missed = Vec::new(); + let mut peak = threads(); + for &secs in &targets { + match seek_ttff(&mut session, secs, &mut peak) { + Ok(ms) => samples.push(ms), + Err(why) => missed.push(why), + } + } + let audio = session.audio_underruns(); + let backend = session.decode_backend().label(); + let mut note = format!( + "backend {backend}, p95 {:.2}ms, peak {peak} threads, seed {seed}", + pct(&samples, 0.95) + ); + if !missed.is_empty() { + note += &format!(", {}/{STORM_SEEKS} NO-FRAME({})", missed.len(), missed[0]); + } + row(path, &format!("seekstorm_ttff_{pass}"), "ms", &samples, ¬e); + row( + path, + &format!("seekstorm_underruns_{pass}"), + "count", + &[audio.map_or(0, |(n, _)| n).saturating_sub(before) as f64], + &match audio { + None => "no audio device".into(), + Some((total, last)) => format!( + "backend {backend}, {total} since open, {}", + last.map_or_else(|| "none yet".into(), |t| format!("last at {t:.1}s device")) + ), + }, + ); + } +} + +// --------------------------------------------------------------- play loop + +/// How long a picture waits between frames while the sound runs: the app's own +/// pump (`crates/app/src/player/transport.rs`), driven here for `secs` with a +/// seek to a random point every five seconds -- the shape of the complaint, +/// which is a film played and jumped around in rather than a seek measured on +/// its own. +/// +/// Three numbers come out: the gap between frames (p50 and p99, the tail being +/// the jump a person sees), the audio underruns over the whole run, and how +/// many times the picture worker was restarted -- by a seek, by a clip +/// boundary, or by the late-picture resync, which is counted separately in the +/// note. +/// +/// The resync rule is the app's, repeated here (`LATE_RESYNC`, `RESYNC_GAP` in +/// `crates/app/src/transport.rs`) because an engine example cannot depend on +/// the binary. Repeated only: nothing in this file changes how playback runs. +const PLAYLOOP_SEEK_GAP: Duration = Duration::from_secs(5); +const LATE_RESYNC: f64 = 0.4; +const RESYNC_GAP: Duration = Duration::from_secs(2); + +fn playloop_bench(path: &Path, secs: f64, seed: u64) { + evict(path); + let mut session = match engine::PlaybackSession::open(path) { + Ok(session) => session, + Err(e) => { + row(path, "playloop_frame_gap", "ms", &[], &format!("FAIL({e})")); + return; + } + }; + let fps = session.meta().frame_rate; + let duration = session.timeline_duration(); + let mut rng = Lcg(seed); + + let mut gaps = Vec::new(); + let mut held: Option = None; + let (mut shown, mut dropped, mut seeks, mut resyncs) = (0u64, 0u64, 0u64, 0u64); + let mut last_resync: Option = None; + // The first frame of the run is owed exactly as a seek's landing is. + let mut owed = true; + + session.play(); + let start = Instant::now(); + let deadline = start + Duration::from_secs_f64(secs); + let mut last_frame = start; + let mut next_seek = start + PLAYLOOP_SEEK_GAP; + while Instant::now() < deadline { + session.tick(); + let target = session.now() * fps; + let mut newest = None; + while session.is_playing() || owed { + let frame = match held.take() { + Some(frame) => frame, + None => match session.try_frame() { + // Timed here and only here: a frame taken back out of + // `held` cost no decode, and counting it would report a + // zero gap for a frame nobody waited for. + Some(frame) => { + let now = Instant::now(); + gaps.push((now - last_frame).as_secs_f64() * 1000.0); + last_frame = now; + frame + } + None => break, + }, + }; + if f64::from(frame.index) <= target { + dropped += u64::from(newest.is_some()); + newest = Some(frame); + } else { + held = Some(frame); + break; + } + } + let late = newest + .as_ref() + .map_or(0.0, |f| (target - f64::from(f.index)) / fps); + if newest.is_some() { + shown += 1; + owed = false; + } + if !owed + && session.is_playing() + && late > LATE_RESYNC + && last_resync.is_none_or(|t| t.elapsed() >= RESYNC_GAP) + { + session.resync_picture(); + held = None; + last_resync = Some(Instant::now()); + resyncs += 1; + } + // End of stream inside a five-second stretch is the same jump early: + // the loop is here to keep playing, not to stop at whatever it landed + // near. + if Instant::now() >= next_seek || session.is_eos() { + session.seek(rng.next() * duration * 0.95); + held = None; + owed = true; + seeks += 1; + next_seek = Instant::now() + PLAYLOOP_SEEK_GAP; + } + std::thread::sleep(Duration::from_millis(1)); + } + session.pause(); + + let elapsed = start.elapsed().as_secs_f64(); + let backend = session.decode_backend().label(); + let audio = session.audio_underruns(); + row( + path, + "playloop_frame_gap", + "ms", + &gaps, + &format!( + "backend {backend}, p99 {:.2}ms, {shown} shown, {dropped} dropped, \ + {seeks} seeks in {elapsed:.0}s, seed {seed}", + pct(&gaps, 0.99) + ), + ); + row( + path, + "playloop_underruns", + "count", + &[audio.map_or(0, |(n, _)| n) as f64], + &match audio { + None => "no audio device".into(), + Some((_, last)) => format!( + "backend {backend}, {}", + last.map_or_else(|| "none".into(), |t| format!("last at {t:.1}s device")) + ), + }, + ); + row( + path, + "playloop_restarts", + "count", + &[session.restarts() as f64], + &format!("backend {backend}, {resyncs} late-picture resyncs, {seeks} seeks"), + ); +} + // ---------------------------------------------------------------- waveform /// What a lane waits for before it has an envelope: one whole diff --git a/crates/engine/src/ao.rs b/crates/engine/src/ao.rs index 00bded0..872152c 100644 --- a/crates/engine/src/ao.rs +++ b/crates/engine/src/ao.rs @@ -20,6 +20,7 @@ struct Plugin { set_active: unsafe extern "C" fn(*mut c_void, u32) -> i32, set_volume: unsafe extern "C" fn(*mut c_void, f32) -> i32, flush: unsafe extern "C" fn(*mut c_void) -> i32, + underruns: unsafe extern "C" fn(*mut c_void, *mut i64) -> i64, close: unsafe extern "C" fn(*mut c_void), // Never dropped (lives in a static), so the fn pointers above stay valid. _lib: Library, @@ -63,6 +64,11 @@ fn load() -> Option { // with no sound at all, which at least says so. set_volume: *lib.get(b"ao_set_volume\0").ok()?, flush: *lib.get(b"ao_flush\0").ok()?, + // Required on the same terms as the two above: the plugin + // ships from this build, and a measurement that silently + // reads zero underruns is worse than a run that says the + // plugin is the wrong one. + underruns: *lib.get(b"ao_underruns\0").ok()?, close: *lib.get(b"ao_close\0").ok()?, _lib: lib, }) @@ -143,6 +149,17 @@ impl AoSession { // SAFETY: `handle` came from `ao_open` and is still open. unsafe { (self.plugin.flush)(self.handle) == 0 } } + + /// Starved callbacks so far, and the device position (samples per channel) + /// the last of them was seen at -- `None` for a stream that has never run + /// dry. Two atomic loads inside the plugin, so it is cheap enough to poll + /// and it never touches the RT thread's path. + pub fn underruns(&self) -> (u64, Option) { + let mut last = -1i64; + // SAFETY: `handle` came from `ao_open`, and `last` is a live i64 of ours. + let count = unsafe { (self.plugin.underruns)(self.handle, &raw mut last) }; + (count.max(0) as u64, (last >= 0).then_some(last)) + } } impl Drop for AoSession { diff --git a/crates/engine/src/playback.rs b/crates/engine/src/playback.rs index adc56de..75ac7a9 100644 --- a/crates/engine/src/playback.rs +++ b/crates/engine/src/playback.rs @@ -250,6 +250,12 @@ pub struct PlaybackSession { /// /// [`retire`]: PlaybackSession::retire retired: Vec, + /// Picture workers that replaced another one, i.e. every restart the + /// session's first worker did not cost: a seek, a resync, a clip boundary. + /// Each carries a demuxer reopen and a VA-API init, so this is what a + /// playback measurement counts to say how often the picture was thrown away + /// ([`restarts`](Self::restarts)). + restarts: u64, clock: PlaybackClock, audio: Option