diff --git a/crates/metal2vulkan-rt/src/complete.rs b/crates/metal2vulkan-rt/src/complete.rs index b5db7153d..3058c9126 100644 --- a/crates/metal2vulkan-rt/src/complete.rs +++ b/crates/metal2vulkan-rt/src/complete.rs @@ -362,6 +362,12 @@ pub(crate) fn run(core: Arc) { let mut behind: Option = None; let mut state = done.lock(); loop { + #[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))] + if !state.flight.is_empty() && core.hold_completions.try_read().is_err() { + drop(state); + drop(core.hold_completions.read()); + state = done.lock(); + } let mut retire = std::mem::take(&mut state.ended); let fired = std::mem::take(&mut state.fire); let mut fire: Vec<(Listener, u64)> = Vec::new(); diff --git a/crates/metal2vulkan-rt/src/device.rs b/crates/metal2vulkan-rt/src/device.rs index 1e4ef074c..83af8d2b1 100644 --- a/crates/metal2vulkan-rt/src/device.rs +++ b/crates/metal2vulkan-rt/src/device.rs @@ -186,6 +186,10 @@ pub(crate) struct Core { /// the barrier twice before it allocates, once parked and once released. #[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))] pub(crate) park_region: Mutex>>, + /// For the device tests: while a test holds it for writing, the completion thread, with work + /// in flight, waits at the top of its round, so no completion is seen, as on a loaded host. + #[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))] + pub(crate) hold_completions: std::sync::RwLock<()>, pub(crate) pool: Pool, /// The blocks the consumer holds handles of, by number ([`Block`]): each claim goes once its /// last handle drops, or at teardown. @@ -520,6 +524,8 @@ impl Device { fail_private: std::sync::atomic::AtomicU32::new(0), #[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))] park_region: Mutex::new(None), + #[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))] + hold_completions: std::sync::RwLock::new(()), pool, claims: Mutex::new(HashMap::new()), places: Places::new(), diff --git a/crates/metal2vulkan-rt/src/submit.rs b/crates/metal2vulkan-rt/src/submit.rs index ca0b3196c..37379bcaa 100644 --- a/crates/metal2vulkan-rt/src/submit.rs +++ b/crates/metal2vulkan-rt/src/submit.rs @@ -650,6 +650,10 @@ impl Work { let mut captured = 0; let mut piece = 0; let mut at = pieces.first().map_or(0, |&(_, start, _)| start); + // The progress value known complete: what the completion thread has seen, or what this + // capture waited for. A wait frees the spans of the submission it waited for at once, + // before the completion thread has retired it, so the capture waits once for each. + let mut completed = 0; while let Some(&(index, _, end)) = pieces.get(piece) { if at == end { piece += 1; @@ -661,7 +665,7 @@ impl Work { let Some(region) = state.changes.region.as_mut() else { unreachable!("the first named range made the ring, freed only at the device's end"); }; - region.ring.reclaim(core.done.completed()); + region.ring.reclaim(completed.max(core.done.completed())); if let Some((number, offset)) = region.ring.take(len, false) { let (host, ring) = (region.host(offset), region.buffer); drop(state); @@ -704,7 +708,10 @@ impl Work { } else { Ok(()) }) { - Ok(()) => continue, + Ok(()) => { + completed = completed.max(value); + continue; + } Err(error) => { let spans = chunk.give_up(core); return Err(self.unwind(state, spans, &named, error)); @@ -777,6 +784,7 @@ impl Work { let spans = chunk.give_up(core); return Err(self.unwind(state, spans, &named, error)); } + completed = completed.max(value); continue; } // A command buffer committed before is held on the host: the rest takes staging of diff --git a/crates/metal2vulkan-rt/src/tests/managed.rs b/crates/metal2vulkan-rt/src/tests/managed.rs index a6e47dd1a..d3f9d956e 100644 --- a/crates/metal2vulkan-rt/src/tests/managed.rs +++ b/crates/metal2vulkan-rt/src/tests/managed.rs @@ -830,6 +830,53 @@ pub(crate) mod on_device { }); } + /// A commit naming three times the capture ring waits once for each ring-full it sends, though + /// the completion thread sees no completion meanwhile (as on a loaded host): the wait frees the + /// spans of the submission it waited for, so the capture never waits again for one that has + /// completed. The bytes arrive whole. + #[test] + fn every_device_waits_once_for_each_ring_full_while_completions_lag() { + each_device("one wait a ring-full", |name, _, device| { + if device.core().unified_memory() { + println!("one wait a ring-full: {name}: no device copy"); + return; + } + let counters = device.counters(); + let len = 3 * CAPTURE_LEN; + let bytes: Vec = (0..len).map(|i| (i % 251) as u8).collect(); + let (managed, into, blocks) = named_whole(&device, name, len, &bytes); + let queue = device.new_command_queue(4).expect("a queue"); + let command_buffer = queue.command_buffer().expect("a command buffer"); + copy_out(&command_buffer, &managed, &into, 0, len); + // Another thread holds the completion thread for a while; the commit runs inside it. + let core = Arc::clone(device.core()); + let (held, holding) = std::sync::mpsc::channel(); + let holder = thread::spawn(move || { + let hold = core.hold_completions.write().expect("the hold"); + held.send(()).expect("the test"); + thread::sleep(std::time::Duration::from_millis(300)); + drop(hold); + }); + holding.recv().expect("the hold taken"); + let before = counters.metrics(); + command_buffer.commit().expect("a commit"); + let metrics = counters.metrics(); + holder.join().expect("the holder"); + finished("the copy", &command_buffer); + assert_eq!(command_buffer.error(), None, "{name}"); + let delta = |of: fn(&crate::Metrics) -> u64| of(&metrics) - of(&before); + let (streams, waits) = (delta(|m| m.capture_streams), delta(|m| m.capture_waits)); + println!("one wait a ring-full: {name}: streams {streams}, waits {waits}"); + assert!(streams >= 2, "{name}: streamed"); + assert_eq!(waits, streams, "{name}: one wait a ring-full"); + assert!(read(&into, len as usize) == bytes, "{name}: the bytes"); + drop((managed, into, command_buffer, queue)); + for block in blocks { + (device.release_block(block)).unwrap_or_else(|error| panic!("{name}: {error}")); + } + }); + } + /// A commit naming more than the ring behind a command buffer that waits on an event the host /// has not signalled, committed before the bytes were named, cannot send its ring-full ahead: /// the rest takes staging of its own, the