Cancel stops the read, not only the landing
Browse files at this commit4d0dd2b9a2819a1de8131993b4e232210f71cb7aParent: 290699e- fix
- Cancel stops the read, not only the landing
7 changed files +255 -18
- crates/app/src/importing.rs +10 -6
- crates/app/src/player/library.rs +13 -5
- crates/app/src/ui/overlays.rs +2 -2
- crates/engine/src/audio.rs +20 -0
- crates/engine/src/demux.rs +95 -0
- crates/engine/src/playback.rs +45 -5
- crates/engine/tests/playback.rs +70 -0
crates/app/src/importing.rsmodified+10 -6
| @@ -81,12 +81,16 @@ pub(crate) struct Import { | |||
| 81 | /// the landing: what the worker read is dropped instead of joining the | 81 | /// the landing: what the worker read is dropped instead of joining the |
| 82 | /// timeline. | 82 | /// timeline. |
| 83 | /// | 83 | /// |
| 84 | - /// corner-cut: the *read* is not stopped -- a demuxer walk polls nothing, so | 84 | + /// ...and the read with it: the worker installs this as its thread's cancel |
| 85 | - /// the worker finishes into a result nobody takes, and the window is given | 85 | + /// ([`engine::demux::with_cancel`]) and every Matroska cluster walk under it |
| 86 | - /// back at the click either way. Ceiling: a cancelled cold 24 GB import | 86 | + /// -- the header index and the subtitle pass, the twenty seconds of a cold |
| 87 | - /// still costs the disk its twenty seconds. Upgrade: a flag | 87 | + /// 24 GB remux -- gives up at the next element. |
| 88 | - /// `engine::demux::Demuxer::open` polls between clusters, which is where an | 88 | + /// |
| 89 | - /// export's own cancel already lives ([`engine::ExportHandle::cancel`]). | 89 | + /// corner-cut: an mp4's header is one read inside `mp4::Mp4Reader`, which |
| 90 | + /// has no flag to poll, so a cancelled mp4 import still finishes its own | ||
| 91 | + /// header before the worker returns. Ceiling: the disk stays busy for that | ||
| 92 | + /// read, though nothing waits on it. Upgrade path is our own `moov` parse, | ||
| 93 | + /// which is a demuxer rewrite rather than a cancel. | ||
| 90 | pub(crate) cancelled: Arc<std::sync::atomic::AtomicBool>, | 94 | pub(crate) cancelled: Arc<std::sync::atomic::AtomicBool>, |
| 91 | } | 95 | } |
| 92 | 96 | ||
crates/app/src/player/library.rsmodified+13 -5
| @@ -257,7 +257,14 @@ impl Player { | |||
| 257 | let gate = self.session.as_ref().map(PlaybackSession::import_gate); | 257 | let gate = self.session.as_ref().map(PlaybackSession::import_gate); |
| 258 | let read = cx.background_executor().spawn({ | 258 | let read = cx.background_executor().spawn({ |
| 259 | let (path, stage) = (path.clone(), Arc::clone(&stage)); | 259 | let (path, stage) = (path.clone(), Arc::clone(&stage)); |
| 260 | - async move { open_ahead(what, &path, &stage, gate) } | 260 | + let cancelled = Arc::clone(&cancelled); |
| 261 | + // The Cancel beside the line reaches the *walk* this way: every | ||
| 262 | + // cluster the reads below step over checks this flag, so a stopped | ||
| 263 | + // import stops the disk rather than only the landing | ||
| 264 | + // ([`engine::demux::with_cancel`]). | ||
| 265 | + async move { | ||
| 266 | + engine::demux::with_cancel(&cancelled, || open_ahead(what, &path, &stage, gate)) | ||
| 267 | + } | ||
| 261 | }); | 268 | }); |
| 262 | let now = Instant::now(); | 269 | let now = Instant::now(); |
| 263 | self.importing = Some(Import { | 270 | self.importing = Some(Import { |
| @@ -311,9 +318,10 @@ impl Player { | |||
| 311 | /// leaving five to start themselves would be the same wait under another | 318 | /// leaving five to start themselves would be the same wait under another |
| 312 | /// name. | 319 | /// name. |
| 313 | /// | 320 | /// |
| 314 | - /// The read in flight is *not* stopped, for the reason [`Import::cancelled`] | 321 | + /// The read in flight is stopped too, where the container lets it be: a |
| 315 | - /// gives, and the notice says as much rather than promising the disk went | 322 | + /// Matroska walk checks this flag between clusters and gives up there |
| 316 | - /// quiet. | 323 | + /// ([`engine::demux::with_cancel`]). See [`Import::cancelled`] for what an |
| 324 | + /// mp4 header still costs. | ||
| 317 | pub(crate) fn cancel_import(&mut self, cx: &mut Context<Self>) { | 325 | pub(crate) fn cancel_import(&mut self, cx: &mut Context<Self>) { |
| 318 | let Some(import) = self.importing.take() else { | 326 | let Some(import) = self.importing.take() else { |
| 319 | return; | 327 | return; |
| @@ -328,7 +336,7 @@ impl Player { | |||
| 328 | n => format!(" — {n} more dropped from the queue"), | 336 | n => format!(" — {n} more dropped from the queue"), |
| 329 | }; | 337 | }; |
| 330 | let text = format!( | 338 | let text = format!( |
| 331 | - "IMPORT CANCELLED: {}{tail} — the read already running finishes unheeded", | 339 | + "IMPORT CANCELLED: {}{tail} — the read stops where the container lets it", |
| 332 | file_name(&import.path) | 340 | file_name(&import.path) |
| 333 | ); | 341 | ); |
| 334 | eprintln!("{text}"); | 342 | eprintln!("{text}"); |
crates/app/src/ui/overlays.rsmodified+2 -2
| @@ -58,8 +58,8 @@ impl Player { | |||
| 58 | BG_RAISED(), | 58 | BG_RAISED(), |
| 59 | None, | 59 | None, |
| 60 | "Cancel", | 60 | "Cancel", |
| 61 | - "stops this file and anything queued behind it; the read already \ | 61 | + "stops this file and anything queued behind it, and the read with \ |
| 62 | - running finishes unheeded" | 62 | + them where the container lets it stop" |
| 63 | .to_string(), | 63 | .to_string(), |
| 64 | true, | 64 | true, |
| 65 | cx.listener(|this, _: &ClickEvent, _, cx| this.cancel_import(cx)), | 65 | cx.listener(|this, _: &ClickEvent, _, cx| this.cancel_import(cx)), |
crates/engine/src/audio.rsmodified+20 -0
| @@ -201,6 +201,14 @@ pub struct MixControls { | |||
| 201 | state: Mutex<(Vec<f32>, Limiter)>, | 201 | state: Mutex<(Vec<f32>, Limiter)>, |
| 202 | /// A mixer thread has these; see the type docs. | 202 | /// A mixer thread has these; see the type docs. |
| 203 | live: AtomicBool, | 203 | live: AtomicBool, |
| 204 | + /// The open these were handed to finished without a mixer: a silent | ||
| 205 | + /// timeline, or a source that would not open. Set by whoever ran that open | ||
| 206 | + /// ([`PlaybackSession::start_audio`] does it from the feeder), because | ||
| 207 | + /// [`Self::live`] alone cannot tell "no mixer will ever read this" from | ||
| 208 | + /// "the mixer is still being opened" -- and a caller that cannot tell those | ||
| 209 | + /// apart either rebuilds the sound under a cold open, once per fader nudge, | ||
| 210 | + /// or hands a mix to nobody and never notices. | ||
| 211 | + detached: AtomicBool, | ||
| 204 | } | 212 | } |
| 205 | 213 | ||
| 206 | impl MixControls { | 214 | impl MixControls { |
| @@ -209,6 +217,7 @@ impl MixControls { | |||
| 209 | version: AtomicU64::new(0), | 217 | version: AtomicU64::new(0), |
| 210 | state: Mutex::new((gains, limiter)), | 218 | state: Mutex::new((gains, limiter)), |
| 211 | live: AtomicBool::new(false), | 219 | live: AtomicBool::new(false), |
| 220 | + detached: AtomicBool::new(false), | ||
| 212 | }) | 221 | }) |
| 213 | } | 222 | } |
| 214 | 223 | ||
| @@ -224,6 +233,17 @@ impl MixControls { | |||
| 224 | self.live.load(Ordering::Acquire) | 233 | self.live.load(Ordering::Acquire) |
| 225 | } | 234 | } |
| 226 | 235 | ||
| 236 | + /// Says the open is over and left no mixer behind; see [`Self::detached`]. | ||
| 237 | + pub fn detach(&self) { | ||
| 238 | + self.detached.store(true, Ordering::Release); | ||
| 239 | + } | ||
| 240 | + | ||
| 241 | + /// Whether anything can still hear a [`set`](Self::set): a mixer reading | ||
| 242 | + /// these, or an open still on its way to being one. | ||
| 243 | + pub fn is_reachable(&self) -> bool { | ||
| 244 | + !self.detached.load(Ordering::Acquire) | ||
| 245 | + } | ||
| 246 | + | ||
| 227 | /// The mix, if it has moved since `seen`. `None` -- the every-block case -- | 247 | /// The mix, if it has moved since `seen`. `None` -- the every-block case -- |
| 228 | /// takes no lock. | 248 | /// takes no lock. |
| 229 | fn changed(&self, seen: &mut u64) -> Option<(Vec<f32>, Limiter)> { | 249 | fn changed(&self, seen: &mut u64) -> Option<(Vec<f32>, Limiter)> { |
crates/engine/src/demux.rsmodified+95 -0
| @@ -26,6 +26,57 @@ use crate::colorspace::{ColorDescription, ContentLight, Tags, bitstream_tags}; | |||
| 26 | 26 | ||
| 27 | const START_CODE: [u8; 4] = [0, 0, 0, 1]; | 27 | const START_CODE: [u8; 4] = [0, 0, 0, 1]; |
| 28 | 28 | ||
| 29 | +/// What a cancelled read fails with. A message rather than a variant because | ||
| 30 | +/// [`crate::Error`] is a boxed error and always was; [`is_cancelled`] is what | ||
| 31 | +/// tells this apart from a file that really would not open, so an abandoned | ||
| 32 | +/// read is never reported to anyone as a broken file. | ||
| 33 | +const CANCELLED: &str = "read cancelled"; | ||
| 34 | + | ||
| 35 | +thread_local! { | ||
| 36 | + /// The flag the read running on *this* thread is abandoned by, installed by | ||
| 37 | + /// [`with_cancel`]. | ||
| 38 | + /// | ||
| 39 | + /// Thread-scoped rather than passed down, and deliberately: a read is a | ||
| 40 | + /// thread here -- an import worker, an audio feeder, a decode worker -- and | ||
| 41 | + /// the walk that has to be stopped sits five frames under an `open` with a | ||
| 42 | + /// dozen callers, none of which have anything to cancel. This way the two | ||
| 43 | + /// that do say so at the top of their own thread and every loop below them | ||
| 44 | + /// obeys, at the price of one thread-local load per cluster. | ||
| 45 | + static CANCEL: std::cell::RefCell<Option<std::sync::Arc<std::sync::atomic::AtomicBool>>> = | ||
| 46 | + const { std::cell::RefCell::new(None) }; | ||
| 47 | +} | ||
| 48 | + | ||
| 49 | +/// Runs `read` with `flag` as this thread's cancel: setting it abandons the | ||
| 50 | +/// walks inside at their next element, with [`CANCELLED`] rather than a verdict | ||
| 51 | +/// about the file. Restores whatever was installed before, so a nested read is | ||
| 52 | +/// not silently un-cancelled. | ||
| 53 | +pub fn with_cancel<T>( | ||
| 54 | + flag: &std::sync::Arc<std::sync::atomic::AtomicBool>, | ||
| 55 | + read: impl FnOnce() -> T, | ||
| 56 | +) -> T { | ||
| 57 | + let previous = CANCEL.with(|cell| cell.borrow_mut().replace(flag.clone())); | ||
| 58 | + let out = read(); | ||
| 59 | + CANCEL.with(|cell| *cell.borrow_mut() = previous); | ||
| 60 | + out | ||
| 61 | +} | ||
| 62 | + | ||
| 63 | +/// Whether the read on this thread has been abandoned; `false` where nobody | ||
| 64 | +/// installed a flag, which is every caller that has nothing to cancel. | ||
| 65 | +fn cancelled() -> bool { | ||
| 66 | + CANCEL.with(|cell| { | ||
| 67 | + cell.borrow() | ||
| 68 | + .as_ref() | ||
| 69 | + .is_some_and(|flag| flag.load(Ordering::Acquire)) | ||
| 70 | + }) | ||
| 71 | +} | ||
| 72 | + | ||
| 73 | +/// Whether `error` is a read that was abandoned rather than a file that could | ||
| 74 | +/// not be read: what keeps a cancelled import off the notice bar and a | ||
| 75 | +/// superseded audio open off stderr. | ||
| 76 | +pub fn is_cancelled(error: &crate::Error) -> bool { | ||
| 77 | + error.to_string() == CANCELLED | ||
| 78 | +} | ||
| 79 | + | ||
| 29 | /// What the video track is coded with. Not a decoder choice by itself: only | 80 | /// What the video track is coded with. Not a decoder choice by itself: only |
| 30 | /// H.264 has a software decoder here, which is what [`Codec::needs_plugin`] | 81 | /// H.264 has a software decoder here, which is what [`Codec::needs_plugin`] |
| 31 | /// says for the others. | 82 | /// says for the others. |
| @@ -2641,6 +2692,13 @@ fn mkv_walk_blocks(file: &mut File, segment: (u64, u64), number: u64) -> crate:: | |||
| 2641 | let mut laced: Vec<(usize, usize)> = Vec::new(); | 2692 | let mut laced: Vec<(usize, usize)> = Vec::new(); |
| 2642 | let mut at = segment.0; | 2693 | let mut at = segment.0; |
| 2643 | while let Some((id, body, stop)) = ebml_element(file, at, segment.1)? { | 2694 | while let Some((id, body, stop)) = ebml_element(file, at, segment.1)? { |
| 2695 | + // The whole segment is the walk a cancel exists for -- twenty seconds of | ||
| 2696 | + // disk on a cold 25 GB remux -- and this is where a Cancel beside an | ||
| 2697 | + // import, or a scrub step that superseded this open two hundred steps | ||
| 2698 | + // ago, stops the *read* instead of dropping its answer. | ||
| 2699 | + if cancelled() { | ||
| 2700 | + return Err(CANCELLED.into()); | ||
| 2701 | + } | ||
| 2644 | at = stop; | 2702 | at = stop; |
| 2645 | if id != CLUSTER { | 2703 | if id != CLUSTER { |
| 2646 | continue; | 2704 | continue; |
| @@ -3929,6 +3987,10 @@ fn mkv_subtitle_blocks( | |||
| 3929 | let us = |ticks: i64| ticks * timestamp_scale as i64 / 1_000; | 3987 | let us = |ticks: i64| ticks * timestamp_scale as i64 / 1_000; |
| 3930 | let mut at = segment.0; | 3988 | let mut at = segment.0; |
| 3931 | while let Some((id, body, stop)) = ebml_element(file, at, segment.1)? { | 3989 | while let Some((id, body, stop)) = ebml_element(file, at, segment.1)? { |
| 3990 | + // The import's second walk, stopped by the same Cancel as its first. | ||
| 3991 | + if cancelled() { | ||
| 3992 | + return Err(CANCELLED.into()); | ||
| 3993 | + } | ||
| 3932 | at = stop; | 3994 | at = stop; |
| 3933 | if id != CLUSTER { | 3995 | if id != CLUSTER { |
| 3934 | continue; | 3996 | continue; |
| @@ -4027,6 +4089,39 @@ mod tests { | |||
| 4027 | found | 4089 | found |
| 4028 | } | 4090 | } |
| 4029 | 4091 | ||
| 4092 | + /// **A cancelled walk stops walking.** The flag the import's Cancel and a | ||
| 4093 | + /// superseded audio open both set ([`with_cancel`]) is read by the segment | ||
| 4094 | + /// walk itself, so the answer is an abandoned read rather than a verdict | ||
| 4095 | + /// about the file -- and the same walk without it still opens. | ||
| 4096 | + #[test] | ||
| 4097 | + fn a_cancelled_walk_gives_up_and_says_so() { | ||
| 4098 | + let Some(path) = matroska_fixtures().into_iter().next() else { | ||
| 4099 | + eprintln!("no Matroska fixture: the cancel walk is not measured"); | ||
| 4100 | + return; | ||
| 4101 | + }; | ||
| 4102 | + let name = path.file_name().unwrap().to_string_lossy().into_owned(); | ||
| 4103 | + let (_, lazy) = MkvDemuxer::open(&path).expect(&name); | ||
| 4104 | + let (segment, number) = (lazy.segment, lazy.number); | ||
| 4105 | + let mut file = File::open(&path).expect(&name); | ||
| 4106 | + | ||
| 4107 | + let flag = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(true)); | ||
| 4108 | + let stopped = with_cancel(&flag, || mkv_walk_blocks(&mut file, segment, number)); | ||
| 4109 | + let error = stopped.err().expect("a cancelled walk must not answer"); | ||
| 4110 | + assert!( | ||
| 4111 | + is_cancelled(&error), | ||
| 4112 | + "{name}: a cancelled walk reported {error} -- a verdict about the file" | ||
| 4113 | + ); | ||
| 4114 | + | ||
| 4115 | + // ...and nothing is cancelled by having been near a flag: the walk that | ||
| 4116 | + // was not asked to stop is the walk it always was. | ||
| 4117 | + flag.store(false, Ordering::Release); | ||
| 4118 | + let walked = with_cancel(&flag, || mkv_walk_blocks(&mut file, segment, number)); | ||
| 4119 | + assert!( | ||
| 4120 | + walked.is_ok_and(|(blocks, ..)| !blocks.is_empty()), | ||
| 4121 | + "{name}: the uncancelled walk found nothing" | ||
| 4122 | + ); | ||
| 4123 | + } | ||
| 4124 | + | ||
| 4030 | /// **The lazy index is the same index.** For every Matroska fixture, the | 4125 | /// **The lazy index is the same index.** For every Matroska fixture, the |
| 4031 | /// window built off the file's `Cues` and the whole-segment walk it replaced | 4126 | /// window built off the file's `Cues` and the whole-segment walk it replaced |
| 4032 | /// agree on: the block list, the frame count, and where every one of the | 4127 | /// agree on: the block list, the frame count, and where every one of the |
crates/engine/src/playback.rsmodified+45 -5
| @@ -205,6 +205,20 @@ impl Audio { | |||
| 205 | /// The epoch is captured *here*, at the seek, not when the open returns: a | 205 | /// The epoch is captured *here*, at the seek, not when the open returns: a |
| 206 | /// scrub abandons opens by the dozen and every one of them must still be | 206 | /// scrub abandons opens by the dozen and every one of them must still be |
| 207 | /// the stale stream it was started as, however late it lands. | 207 | /// the stale stream it was started as, however late it lands. |
| 208 | + /// | ||
| 209 | + /// corner-cut: an abandoned open is not *cancelled*, though the machinery | ||
| 210 | + /// for it exists ([`crate::demux::with_cancel`], which the import's Cancel | ||
| 211 | + /// uses): it runs to the end and its answer is dropped. Tried, and left | ||
| 212 | + /// out for want of evidence -- 200 cold scrub steps over the 25 GB HEVC | ||
| 213 | + /// remux measured 101, 137 and 14 live workers on three runs of the *same* | ||
| 214 | + /// build, so the machine decides that number and not this. There is a | ||
| 215 | + /// reason to expect it to hurt, too: a Matroska walk writes its sidecar | ||
| 216 | + /// index only when it finishes (`demux::mkv_blocks`), so a drag that | ||
| 217 | + /// cancels every step's walk would never write one and every step would | ||
| 218 | + /// walk the segment again from the top. Ceiling: an abandoned open keeps | ||
| 219 | + /// reading, and the decoder threads it started live until it does. Upgrade | ||
| 220 | + /// path is an index the walk publishes as it goes -- then cancelling costs | ||
| 221 | + /// nothing and can be measured on an idle machine. | ||
| 208 | fn spawn_feeder_deferred( | 222 | fn spawn_feeder_deferred( |
| 209 | &self, | 223 | &self, |
| 210 | open: impl FnOnce() -> Option<Receiver<AudioChunk>> + Send + 'static, | 224 | open: impl FnOnce() -> Option<Receiver<AudioChunk>> + Send + 'static, |
| @@ -1603,9 +1617,22 @@ impl PlaybackSession { | |||
| 1603 | fn push_mix(&mut self) { | 1617 | fn push_mix(&mut self) { |
| 1604 | let gains = self.project.audio_gains(); | 1618 | let gains = self.project.audio_gains(); |
| 1605 | let limiter = self.project.limiter(); | 1619 | let limiter = self.project.limiter(); |
| 1606 | - match &self.mix { | 1620 | + // A mixer reading these, or an open on its way to being one: either way |
| 1607 | - Some(mix) => mix.set(gains, limiter), | 1621 | + // the number is heard without a rebuild, and *that* is what a handle |
| 1608 | - None => self.reseek_audio(), | 1622 | + // left over from an open that found nothing cannot do |
| 1623 | + // ([`crate::audio::MixControls::is_reachable`]). | ||
| 1624 | + if let Some(mix) = self.mix.as_ref().filter(|mix| mix.is_reachable()) { | ||
| 1625 | + mix.set(gains, limiter); | ||
| 1626 | + return; | ||
| 1627 | + } | ||
| 1628 | + // Nothing to hand it to. Only a setting that *needs* a mixer is worth | ||
| 1629 | + // the flush and the re-open: a ceiling dragged with the limiter off, or | ||
| 1630 | + // a fader put back to unity, changes not one sample of the flat | ||
| 1631 | + // single-stream path the timeline is already playing | ||
| 1632 | + // ([`AudioSession::is_mixed`]), and reseeking per nudge was a hole in | ||
| 1633 | + // the sound for a change nobody could hear. | ||
| 1634 | + if crate::audio::AudioSession::is_mixed(gains.len(), &gains, limiter) { | ||
| 1635 | + self.reseek_audio(); | ||
| 1609 | } | 1636 | } |
| 1610 | } | 1637 | } |
| 1611 | 1638 | ||
| @@ -2668,9 +2695,22 @@ impl PlaybackSession { | |||
| 2668 | Ok(Some((_, rx))) => Some(rx), | 2695 | Ok(Some((_, rx))) => Some(rx), |
| 2669 | // A silent timeline, and a source that will not open: both | 2696 | // A silent timeline, and a source that will not open: both |
| 2670 | // feed nothing, which the feeder itself reports by ending. | 2697 | // feed nothing, which the feeder itself reports by ending. |
| 2671 | - Ok(None) => None, | 2698 | + // The mix goes with them -- no mixer was started, so a fader |
| 2699 | + // moved later must rebuild the sound rather than hand its | ||
| 2700 | + // number to a thread that does not exist | ||
| 2701 | + // ([`crate::audio::MixControls::detach`]). | ||
| 2702 | + Ok(None) => { | ||
| 2703 | + worker_controls.detach(); | ||
| 2704 | + None | ||
| 2705 | + } | ||
| 2672 | Err(e) => { | 2706 | Err(e) => { |
| 2673 | - eprintln!("timeline audio open failed: {e}"); | 2707 | + worker_controls.detach(); |
| 2708 | + // A read this session abandoned on purpose -- the seek | ||
| 2709 | + // after it, or the session going away -- is not a file | ||
| 2710 | + // that would not open, and says nothing. | ||
| 2711 | + if !crate::demux::is_cancelled(&e) { | ||
| 2712 | + eprintln!("timeline audio open failed: {e}"); | ||
| 2713 | + } | ||
| 2674 | None | 2714 | None |
| 2675 | } | 2715 | } |
| 2676 | } | 2716 | } |
crates/engine/tests/playback.rsmodified+70 -0
| @@ -530,6 +530,76 @@ fn a_cold_seek_storm_starves_no_quantum() { | |||
| 530 | ); | 530 | ); |
| 531 | } | 531 | } |
| 532 | 532 | ||
| 533 | +/// What a *drag* costs in threads: 200 scrub steps across a cold film with no | ||
| 534 | +/// wait between them, which is a ruler dragged from end to end. Every step | ||
| 535 | +/// abandons an audio open and starts another, and an abandoned one used to run | ||
| 536 | +/// its read to the end anyway -- on a cold film that is seconds each, so the | ||
| 537 | +/// threads pile up behind the drag rather than following it. | ||
| 538 | +/// | ||
| 539 | +/// The session's own count is the oracle (`live_workers`), and the process | ||
| 540 | +/// thread count beside it says what the OS saw. | ||
| 541 | +#[test] | ||
| 542 | +#[ignore = "needs a running PipeWire daemon"] | ||
| 543 | +fn a_cold_scrub_does_not_pile_up_threads() { | ||
| 544 | + let path = std::env::var_os("EDITH_COLD_FILM") | ||
| 545 | + .map(PathBuf::from) | ||
| 546 | + .unwrap_or_else(|| asset("test_av.mp4")); | ||
| 547 | + let steps: u32 = std::env::var("EDITH_STEPS") | ||
| 548 | + .ok() | ||
| 549 | + .and_then(|n| n.parse().ok()) | ||
| 550 | + .unwrap_or(200); | ||
| 551 | + let mut session = open(&path); | ||
| 552 | + let duration = session.timeline_duration(); | ||
| 553 | + let mut last_index = None; | ||
| 554 | + session.play(); | ||
| 555 | + run_for(&mut session, &mut last_index, Duration::from_millis(300)); | ||
| 556 | + evict(&path); | ||
| 557 | + | ||
| 558 | + let (mut peak, mut peak_os) = (0, 0); | ||
| 559 | + let scrub = Instant::now(); | ||
| 560 | + for step in 0..steps { | ||
| 561 | + session.seek(duration * f64::from(step) / f64::from(steps)); | ||
| 562 | + session.tick(); | ||
| 563 | + while session.try_frame().is_some() {} | ||
| 564 | + peak = peak.max(session.live_workers()); | ||
| 565 | + peak_os = peak_os.max(os_threads()); | ||
| 566 | + sleep(Duration::from_millis(16)); // a drag reports at ~60 Hz | ||
| 567 | + } | ||
| 568 | + eprintln!( | ||
| 569 | + "{steps} cold scrub steps in {:?}: peak {peak} session workers, {peak_os} process threads", | ||
| 570 | + scrub.elapsed() | ||
| 571 | + ); | ||
| 572 | + // The peak is *reported*, not asserted: three runs of one build over the | ||
| 573 | + // same film measured 14, 101 and 137, because what decides it is how much | ||
| 574 | + // disk the rest of the machine is using while the opens run. What the | ||
| 575 | + // session owes is a different question and a real one -- every worker the | ||
| 576 | + // drag started has to end, or a drag is a leak. | ||
| 577 | + let deadline = Instant::now() + Duration::from_secs(120); | ||
| 578 | + while session.live_workers() > 2 { | ||
| 579 | + assert!( | ||
| 580 | + Instant::now() < deadline, | ||
| 581 | + "the scrub leaked workers: {} still running two minutes on", | ||
| 582 | + session.live_workers() | ||
| 583 | + ); | ||
| 584 | + sleep(Duration::from_millis(50)); | ||
| 585 | + } | ||
| 586 | + eprintln!("all but {} of them returned", session.live_workers()); | ||
| 587 | +} | ||
| 588 | + | ||
| 589 | +/// Threads in this process, out of `/proc/self/status`. Only ever a report | ||
| 590 | +/// beside the session's own count -- a suite decoding other files at the same | ||
| 591 | +/// time moves this number without touching the session under test. | ||
| 592 | +fn os_threads() -> usize { | ||
| 593 | + std::fs::read_to_string("/proc/self/status") | ||
| 594 | + .ok() | ||
| 595 | + .and_then(|status| { | ||
| 596 | + status | ||
| 597 | + .lines() | ||
| 598 | + .find_map(|line| line.strip_prefix("Threads:")?.trim().parse().ok()) | ||
| 599 | + }) | ||
| 600 | + .unwrap_or(0) | ||
| 601 | +} | ||
| 602 | + | ||
| 533 | /// The edit list end to end: two cuts, drop the middle clip, play the result. | 603 | /// The edit list end to end: two cuts, drop the middle clip, play the result. |
| 534 | /// What comes out is one contiguous timeline whose *pictures* skip the hole -- | 604 | /// What comes out is one contiguous timeline whose *pictures* skip the hole -- |
| 535 | /// no audio needed, so this runs anywhere. | 605 | /// no audio needed, so this runs anywhere. |