A session counts its own workers, not the whole house
Browse files at this commitf490585a1beb021f113217e669171f7bf61930daParent: 07f2cff- fix
- a session counts its own workers, not the whole house
2 changed files +55 -32
- crates/engine/src/playback.rs +44 -12
- crates/engine/tests/playback.rs +11 -20
crates/engine/src/playback.rsmodified+44 -12
| @@ -12,7 +12,7 @@ | |||
| 12 | //! the renderer decides which frame that means. | 12 | //! the renderer decides which frame that means. |
| 13 | 13 | ||
| 14 | use std::path::Path; | 14 | use std::path::Path; |
| 15 | -use std::sync::atomic::{AtomicBool, AtomicI64, AtomicU64, Ordering}; | 15 | +use std::sync::atomic::{AtomicBool, AtomicI64, AtomicU64, AtomicUsize, Ordering}; |
| 16 | use std::sync::mpsc::{Receiver, TryRecvError}; | 16 | use std::sync::mpsc::{Receiver, TryRecvError}; |
| 17 | use std::sync::{Arc, Mutex}; | 17 | use std::sync::{Arc, Mutex}; |
| 18 | use std::thread; | 18 | use std::thread; |
| @@ -116,6 +116,11 @@ struct Audio { | |||
| 116 | /// is not the device's own callback -- the plugin's RT thread never sees | 116 | /// is not the device's own callback -- the plugin's RT thread never sees |
| 117 | /// this lock, and the feeder holds it for one memcpy per write. | 117 | /// this lock, and the feeder holds it for one memcpy per write. |
| 118 | tap: Arc<Mutex<Vec<f32>>>, | 118 | tap: Arc<Mutex<Vec<f32>>>, |
| 119 | + /// Feeder threads started and not yet returned. They are detached, so there | ||
| 120 | + /// is no handle to count instead, and a scrub leaves dozens of them exiting | ||
| 121 | + /// at once -- which is precisely what | ||
| 122 | + /// [`live_workers`](PlaybackSession::live_workers) is asked about. | ||
| 123 | + feeders: Arc<AtomicUsize>, | ||
| 119 | } | 124 | } |
| 120 | 125 | ||
| 121 | /// How much of the played signal the tap keeps. A power of two, because what | 126 | /// How much of the played signal the tap keeps. A power of two, because what |
| @@ -174,24 +179,35 @@ impl Audio { | |||
| 174 | ) -> bool { | 179 | ) -> bool { |
| 175 | let me = self.clone(); | 180 | let me = self.clone(); |
| 176 | let epoch = self.epoch.load(Ordering::Acquire); | 181 | let epoch = self.epoch.load(Ordering::Acquire); |
| 177 | - thread::Builder::new() | 182 | + // Counted before the spawn, so the count is never behind the thread. |
| 183 | + self.feeders.fetch_add(1, Ordering::Release); | ||
| 184 | + let started = thread::Builder::new() | ||
| 178 | .name("audio-feed".into()) | 185 | .name("audio-feed".into()) |
| 179 | .spawn(move || { | 186 | .spawn(move || { |
| 180 | // Superseded before this thread was even scheduled: do not open | 187 | // Superseded before this thread was even scheduled: do not open |
| 181 | // anything at all. A scrub leaves all but the last one here. | 188 | // anything at all. A scrub leaves all but the last one here. |
| 182 | - if me.epoch.load(Ordering::Acquire) != epoch { | ||
| 183 | - return; | ||
| 184 | - } | ||
| 185 | - if let Some(rx) = open() { | ||
| 186 | - feed(rx, &me, epoch); | ||
| 187 | - } | ||
| 188 | - // Only the current feeder gets to declare the end of the audio: | ||
| 189 | - // a superseded one is finished, the stream is not. | ||
| 190 | if me.epoch.load(Ordering::Acquire) == epoch { | 189 | if me.epoch.load(Ordering::Acquire) == epoch { |
| 191 | - me.fed_all.store(true, Ordering::Release); | 190 | + if let Some(rx) = open() { |
| 191 | + feed(rx, &me, epoch); | ||
| 192 | + } | ||
| 193 | + // Only the current feeder gets to declare the end of the | ||
| 194 | + // audio: a superseded one is finished, the stream is not. | ||
| 195 | + if me.epoch.load(Ordering::Acquire) == epoch { | ||
| 196 | + me.fed_all.store(true, Ordering::Release); | ||
| 197 | + } | ||
| 192 | } | 198 | } |
| 199 | + me.feeders.fetch_sub(1, Ordering::Release); | ||
| 193 | }) | 200 | }) |
| 194 | - .is_ok() | 201 | + .is_ok(); |
| 202 | + if !started { | ||
| 203 | + self.feeders.fetch_sub(1, Ordering::Release); | ||
| 204 | + } | ||
| 205 | + started | ||
| 206 | + } | ||
| 207 | + | ||
| 208 | + /// Feeder threads still running; see [`Self::feeders`]. | ||
| 209 | + fn live_feeders(&self) -> usize { | ||
| 210 | + self.feeders.load(Ordering::Acquire) | ||
| 195 | } | 211 | } |
| 196 | } | 212 | } |
| 197 | 213 | ||
| @@ -1219,6 +1235,21 @@ impl PlaybackSession { | |||
| 1219 | self.retired.retain(|w| !w.is_finished()); | 1235 | self.retired.retain(|w| !w.is_finished()); |
| 1220 | } | 1236 | } |
| 1221 | 1237 | ||
| 1238 | + /// How many threads this session still has running of its own: the decode | ||
| 1239 | + /// worker, every retired one that has not returned yet, and the audio | ||
| 1240 | + /// feeders still in the air. One for a session playing along quietly. | ||
| 1241 | + /// | ||
| 1242 | + /// The oracle a seek storm is judged by. `/proc/self/status` counts the | ||
| 1243 | + /// *process*, and a suite decoding twenty files at once moves that number | ||
| 1244 | + /// under a test that never touched them -- this counts only what the | ||
| 1245 | + /// session in hand owns, so it says the same thing alone and in a crowd. | ||
| 1246 | + #[doc(hidden)] | ||
| 1247 | + pub fn live_workers(&self) -> usize { | ||
| 1248 | + usize::from(!self.worker.is_finished()) | ||
| 1249 | + + self.retired.iter().filter(|w| !w.is_finished()).count() | ||
| 1250 | + + self.audio.as_ref().map_or(0, Audio::live_feeders) | ||
| 1251 | + } | ||
| 1252 | + | ||
| 1222 | /// Starts feeding whatever follows the current span on the timeline -- | 1253 | /// Starts feeding whatever follows the current span on the timeline -- |
| 1223 | /// another clip, or a gap, which is black frames from a worker that opens no | 1254 | /// another clip, or a gap, which is black frames from a worker that opens no |
| 1224 | /// file. `false` past the end. The next timeline frame is derived rather | 1255 | /// file. `false` past the end. The next timeline frame is derived rather |
| @@ -2942,6 +2973,7 @@ fn open_audio(path: &Path, stream: usize) -> (Option<Audio>, Option<String>) { | |||
| 2942 | epoch: Arc::new(AtomicU64::new(0)), | 2973 | epoch: Arc::new(AtomicU64::new(0)), |
| 2943 | content_at: Arc::new(AtomicI64::new(-1)), | 2974 | content_at: Arc::new(AtomicI64::new(-1)), |
| 2944 | tap: Arc::new(Mutex::new(Vec::with_capacity(TAP_SAMPLES))), | 2975 | tap: Arc::new(Mutex::new(Vec::with_capacity(TAP_SAMPLES))), |
| 2976 | + feeders: Arc::new(AtomicUsize::new(0)), | ||
| 2945 | }; | 2977 | }; |
| 2946 | (audio.spawn_feeder(rx).then_some(audio), None) | 2978 | (audio.spawn_feeder(rx).then_some(audio), None) |
| 2947 | } | 2979 | } |
crates/engine/tests/playback.rsmodified+11 -20
| @@ -169,15 +169,6 @@ fn source_frame(path: &Path, index: u32) -> Vec<u8> { | |||
| 169 | frame.bgra | 169 | frame.bgra |
| 170 | } | 170 | } |
| 171 | 171 | ||
| 172 | -fn threads() -> usize { | ||
| 173 | - std::fs::read_to_string("/proc/self/status") | ||
| 174 | - .unwrap() | ||
| 175 | - .lines() | ||
| 176 | - .find_map(|l| l.strip_prefix("Threads:")) | ||
| 177 | - .and_then(|n| n.trim().parse().ok()) | ||
| 178 | - .expect("Threads: in /proc/self/status") | ||
| 179 | -} | ||
| 180 | - | ||
| 181 | /// Wall-clock seek: no audio track, so this runs anywhere. | 172 | /// Wall-clock seek: no audio track, so this runs anywhere. |
| 182 | #[test] | 173 | #[test] |
| 183 | fn seek_repositions_video_and_clock() { | 174 | fn seek_repositions_video_and_clock() { |
| @@ -250,7 +241,7 @@ fn rapid_seeks_settle_on_the_last_one() { | |||
| 250 | 241 | ||
| 251 | session.play(); | 242 | session.play(); |
| 252 | run_for(&mut session, &mut last_index, Duration::from_millis(300)); | 243 | run_for(&mut session, &mut last_index, Duration::from_millis(300)); |
| 253 | - let before = threads(); | 244 | + let before = session.live_workers(); |
| 254 | 245 | ||
| 255 | for t in [4.0, 1.0, 3.5, 0.5, 2.0] { | 246 | for t in [4.0, 1.0, 3.5, 0.5, 2.0] { |
| 256 | session.seek(t); | 247 | session.seek(t); |
| @@ -261,11 +252,11 @@ fn rapid_seeks_settle_on_the_last_one() { | |||
| 261 | ); | 252 | ); |
| 262 | 253 | ||
| 263 | sleep(Duration::from_millis(500)); // abandoned workers exit within an AU | 254 | sleep(Duration::from_millis(500)); // abandoned workers exit within an AU |
| 264 | - let after = threads(); | 255 | + let after = session.live_workers(); |
| 265 | - eprintln!("rapid seeks: {before} threads -> {after}"); | 256 | + eprintln!("rapid seeks: {before} workers -> {after}"); |
| 266 | assert!( | 257 | assert!( |
| 267 | after <= before + 3, | 258 | after <= before + 3, |
| 268 | - "workers piled up: {before} threads -> {after}" | 259 | + "workers piled up: {before} workers -> {after}" |
| 269 | ); | 260 | ); |
| 270 | } | 261 | } |
| 271 | 262 | ||
| @@ -273,8 +264,8 @@ fn rapid_seeks_settle_on_the_last_one() { | |||
| 273 | /// Each one abandons a worker and starts another, and since the open moved onto | 264 | /// Each one abandons a worker and starts another, and since the open moved onto |
| 274 | /// the worker (`DecodeSession::open_worker_deferred`) not one of them reads the | 265 | /// the worker (`DecodeSession::open_worker_deferred`) not one of them reads the |
| 275 | /// file on this thread -- so the whole storm is 40 thread spawns, the parked | 266 | /// file on this thread -- so the whole storm is 40 thread spawns, the parked |
| 276 | -/// workers are still reaped as they go (the thread count says so), and the last | 267 | +/// workers are still reaped as they go (the session's own count says so), and |
| 277 | -/// seek still decides what is on screen. | 268 | +/// the last seek still decides what is on screen. |
| 278 | #[test] | 269 | #[test] |
| 279 | fn a_storm_of_seeks_stays_bounded_and_the_last_one_wins() { | 270 | fn a_storm_of_seeks_stays_bounded_and_the_last_one_wins() { |
| 280 | let mut session = open(asset("test_av.mp4")); | 271 | let mut session = open(asset("test_av.mp4")); |
| @@ -283,7 +274,7 @@ fn a_storm_of_seeks_stays_bounded_and_the_last_one_wins() { | |||
| 283 | 274 | ||
| 284 | session.play(); | 275 | session.play(); |
| 285 | run_for(&mut session, &mut last_index, Duration::from_millis(300)); | 276 | run_for(&mut session, &mut last_index, Duration::from_millis(300)); |
| 286 | - let before = threads(); | 277 | + let before = session.live_workers(); |
| 287 | 278 | ||
| 288 | let storm = Instant::now(); | 279 | let storm = Instant::now(); |
| 289 | for i in 0..39 { | 280 | for i in 0..39 { |
| @@ -291,7 +282,7 @@ fn a_storm_of_seeks_stays_bounded_and_the_last_one_wins() { | |||
| 291 | } | 282 | } |
| 292 | session.seek(2.0); | 283 | session.seek(2.0); |
| 293 | let issued = storm.elapsed(); | 284 | let issued = storm.elapsed(); |
| 294 | - eprintln!("40 seeks issued in {issued:?} ({before} threads before)"); | 285 | + eprintln!("40 seeks issued in {issued:?} ({before} workers before)"); |
| 295 | assert!( | 286 | assert!( |
| 296 | issued < Duration::from_secs(1), | 287 | issued < Duration::from_secs(1), |
| 297 | "40 seeks cost the caller {issued:?} -- a seek is opening a file again" | 288 | "40 seeks cost the caller {issued:?} -- a seek is opening a file again" |
| @@ -303,11 +294,11 @@ fn a_storm_of_seeks_stays_bounded_and_the_last_one_wins() { | |||
| 303 | ); | 294 | ); |
| 304 | 295 | ||
| 305 | sleep(Duration::from_millis(500)); // abandoned workers exit within an AU | 296 | sleep(Duration::from_millis(500)); // abandoned workers exit within an AU |
| 306 | - let after = threads(); | 297 | + let after = session.live_workers(); |
| 307 | - eprintln!("seek storm: {before} threads -> {after}"); | 298 | + eprintln!("seek storm: {before} workers -> {after}"); |
| 308 | assert!( | 299 | assert!( |
| 309 | after <= before + 3, | 300 | after <= before + 3, |
| 310 | - "the retired list grew with the storm: {before} threads -> {after}" | 301 | + "the retired list grew with the storm: {before} workers -> {after}" |
| 311 | ); | 302 | ); |
| 312 | } | 303 | } |
| 313 | 304 | ||