diff --git a/crates/metal2vulkan-runtime/src/command_buffer/mod.rs b/crates/metal2vulkan-runtime/src/command_buffer/mod.rs index e98e4cdd8..529e58e54 100644 --- a/crates/metal2vulkan-runtime/src/command_buffer/mod.rs +++ b/crates/metal2vulkan-runtime/src/command_buffer/mod.rs @@ -5023,18 +5023,23 @@ impl CommandBufferShared { } /// The index of its `VkQueue` where its queue is unshared: its commit counts there as posted - /// to the scheduler's inbox, then as taken into the order (`SlotShare::unshared_commits`). + /// to the scheduler's inbox, then as taken into the order (`SlotShare::unshared_commits`), and + /// its batches carry its place among the commits posted there + /// (`SlotShare::posted_unshared`). pub(crate) fn unshared_vk_queue(&self) -> Option { self.queue.unshared().then(|| self.queue.vk_queue()) } /// Where a batch of it is held back from its `VkQueue` for an unshared queue there - /// ([`held_for_unshared`](Self::held_for_unshared)'s conditions), that `VkQueue`'s unshared - /// commits posted to the scheduler's inbox and taken into its order so far + /// ([`held_for_unshared`](Self::held_for_unshared)'s conditions), that `VkQueue`'s index, with + /// its unshared commits posted to the scheduler's inbox and taken into its order so far /// (`unshared_hold::behind_commits`); `None` where none is held back. - pub(crate) fn unshared_commits(&self) -> Option<(u64, u64)> { + pub(crate) fn unshared_commits(&self) -> Option<(usize, (u64, u64))> { let index = self.held_back_on()?; - Some(self.device.shared().queues[index].share.unshared_commits()) + Some(( + index, + self.device.shared().queues[index].share.unshared_commits(), + )) } /// The index of its `VkQueue` where its batches are held back there for an unshared queue diff --git a/crates/metal2vulkan-runtime/src/queue_share.rs b/crates/metal2vulkan-runtime/src/queue_share.rs index afb576f0f..7db726f7e 100644 --- a/crates/metal2vulkan-runtime/src/queue_share.rs +++ b/crates/metal2vulkan-runtime/src/queue_share.rs @@ -220,9 +220,10 @@ impl SlotShare { } /// An unshared command queue it holds posted a commit to the scheduler's inbox, before the - /// commit is there. - pub(crate) fn posted_unshared(&self) { - self.unshared_posted.fetch_add(1, Ordering::SeqCst); + /// commit is there: the commit's place among those posted so far, counted from 1, which the + /// commit's batches carry (`unshared_hold.rs`, "Commits on their way"). + pub(crate) fn posted_unshared(&self) -> u64 { + self.unshared_posted.fetch_add(1, Ordering::SeqCst) + 1 } /// A pass took a commit [`posted_unshared`](Self::posted_unshared) counted into the diff --git a/crates/metal2vulkan-runtime/src/scheduler/mod.rs b/crates/metal2vulkan-runtime/src/scheduler/mod.rs index 62d346e74..e2e0b01b3 100644 --- a/crates/metal2vulkan-runtime/src/scheduler/mod.rs +++ b/crates/metal2vulkan-runtime/src/scheduler/mod.rs @@ -713,12 +713,13 @@ impl Sleep { /// A commit not yet taken into the order: the command buffer, its logical queue, its batches, and, /// where its queue is unshared, the index of the `VkQueue` whose unshared commits its post counted -/// (`SlotShare::unshared_commits`), which its taking in counts too. +/// (`SlotShare::unshared_commits`), which its taking in counts too, with its place among those +/// posted there (`SlotShare::posted_unshared`). struct Committed { buffer: Hold, queue: u64, batches: Vec, - unshared: Option, + unshared: Option<(usize, u64)>, } /// A committed batch not yet submitted. @@ -754,9 +755,13 @@ struct Held { /// ([`held_for`](Self::held_for)), until it goes ([`hold_ended`](Self::hold_ended)). held_since: Option, /// Where its `VkQueue` holds other queues' work back for an unshared queue, the unshared - /// commits posted there at the first look that found its room: those not yet taken in hold it - /// (`unshared_hold::behind_commits`). + /// commits posted there at the first look that found its room: those not yet taken in, or + /// leading their lines on their way, hold it (`unshared_hold::behind_commits`). unshared_noted: Option, + /// Where its queue is unshared, its commit's place among the unshared commits posted on its + /// `VkQueue` (`SlotShare::posted_unshared`): it holds another queue's batch there whose room + /// came after it was posted while it is on its way ([`unshared_on_their_way`]). + unshared_place: Option, } impl Held { @@ -910,12 +915,10 @@ impl Scheduler { // An unshared queue's commit counts as posted before it is in the inbox, so a pass that // finds another queue's batch's room meanwhile holds it until a pass took this one in // (`unshared_hold.rs`, "Commits not yet taken in"). - let unshared = buffer.unshared_vk_queue(); - if let Some(index) = unshared { - buffer.device().shared().queues[index] - .share - .posted_unshared(); - } + let unshared = (buffer.unshared_vk_queue()).map(|index| { + let share = &buffer.device().shared().queues[index].share; + (index, share.posted_unshared()) + }); let committed = Committed { buffer: buffer.hold(), queue, @@ -1559,7 +1562,7 @@ fn take_in(state: &mut State, commits: &mut Vec, spent: &mut Spent) - unshared, } in commits.drain(..) { - if let Some(index) = unshared { + if let Some((index, _)) = unshared { buffer.device().shared().queues[index] .share .took_in_unshared(); @@ -1581,6 +1584,7 @@ fn take_in(state: &mut State, commits: &mut Vec, spent: &mut Spent) - counted_held: false, held_since: None, unshared_noted: None, + unshared_place: unshared.map(|(_, place)| place), }); // Where the device notes queue times, a command buffer committed to an empty line is // its head as it is taken in. @@ -1782,6 +1786,11 @@ fn submit_ready( // of the oldest batch in flight on its `VkQueue` ([`await_unshared_room`]), and whether // one was. let (mut unshared_wake, mut isolating) = (None, false); + // The unshared queues' heads on their way, each with its `VkQueue` and its commit's place: + // another queue's batch there whose room came after it was posted waits for it + // (`unshared_hold.rs`, "Commits on their way"). + let decoding = (decodes, untracked_decodes); + let on_their_way = unshared_on_their_way(&state.order, binary, decoding, &mut memo); let behind = |order: &Order, sequence| is_behind(order, binary, &mut memo, sequence); let next = state.order.next(goes_first, behind, |held| { if let Some(reason) = holds_back(held, binary, decodes, untracked_decodes) { @@ -1802,10 +1811,15 @@ fn submit_ready( return None; } // Its room came, but an unshared queue's commit there posted before it did is not in - // the order yet: the pass that takes it in, which the passer makes before it sleeps, - // sends that one first (`unshared_hold.rs`, "Commits not yet taken in"). - if let Some(commits) = held.buffer.unshared_commits() { - if crate::unshared_hold::behind_commits(&mut held.unshared_noted, commits) { + // the order yet, or leads its line on its way: the pass that takes it in, which the + // passer makes before it sleeps, or the one its step, checks or signal's landing wake + // the thread for, sends that one first (`unshared_hold.rs`, "Commits not yet taken + // in", "Commits on their way"). + if let Some((index, commits)) = held.buffer.unshared_commits() { + let ahead = (on_their_way.iter()) + .filter(|&&(at, _)| at == index) + .map(|&(_, place)| place); + if crate::unshared_hold::behind_commits(&mut held.unshared_noted, commits, ahead) { held.held_for(HeldFor::UnsharedIsolation); (held.buffer.device().shared().counts).head_held_for_unshared_commits(); return None; @@ -2132,11 +2146,12 @@ impl AtCommit { /// /// **It sends no batch ahead of an unshared queue's head that the thread would send first.** The /// order puts an unshared queue's head first ([`goes_first`]) only where `ready` takes it; where -/// this pass leaves such a head that the thread's own look would find ready -/// ([`first_for_the_thread`]), it leaves the other queues' batches held back on that head's -/// `VkQueue` to the thread too ([`left_behind_unshared`]), whose next pass, which the caller -/// wakes it for, sends the unshared one first and them next (`unshared_hold.rs`, "Passes of other -/// threads"). +/// this pass leaves such a head that the thread's own look would find ready, or would hold other +/// queues' batches behind as it is on its way ([`first_for_the_thread`]), it leaves the other +/// queues' batches held back on that head's `VkQueue` to the thread too +/// ([`left_behind_unshared`]), whose next pass, which the caller wakes it for, sends the unshared +/// one first and them next, or holds them while it is on its way (`unshared_hold.rs`, "Passes of +/// other threads"). /// /// **It touches none of the thread's own state.** [`Awaits`] is what the thread's last pass left /// it asleep on, its `vkWaitSemaphores` among them, and [`Left`] is the work that pass handed it: @@ -2291,19 +2306,80 @@ fn submit_plain_ready( } /// Whether `held`, a line's head that [`submit_plain_ready`] leaves, is an unshared queue's that -/// the scheduler's thread's own look would send first ([`goes_first`]): nothing holds it back -/// from that look ([`holds_back`]) and every event value it waits for is reached, though the -/// pass of a thread other than the scheduler's leaves it (its submission or scheduled handlers -/// for a thread about to block, its host step recorded, a switch of storage forms riding on it). +/// the scheduler's thread's own look would send first ([`goes_first`]), or hold another queue's +/// batch behind ([`on_its_way`]): nothing holds it back from that look ([`holds_back`]) and every +/// event value it waits for is reached, though the pass of a thread other than the scheduler's +/// leaves it (its submission or scheduled handlers for a thread about to block, its host step +/// recorded, a switch of storage forms riding on it); or it is on its way (its host step or +/// checks to come, a submitted signal to land). Whether it waits behind an earlier commit that +/// thread's look asks. fn first_for_the_thread( held: &Held, binary: &BinarySignals, decodes: &BTreeMap, untracked_decodes: usize, ) -> bool { - goes_first(held) - && holds_back(held, binary, decodes, untracked_decodes).is_none() - && events_reachable(held) + goes_first(held) && on_its_way(held, binary, decodes, untracked_decodes) +} + +/// Whether `held`, a line's head, waits for nothing but the runtime and work already submitted +/// (`unshared_hold.rs`, "Commits on their way"): its binary semaphores' signals are submitted, +/// every event value it waits for is reached, or reached by a submitted signal that has not +/// landed ([`events_land`]), and nothing holds it back from the scheduler's look ([`holds_back`]) +/// but its host step (its gate, its recording), where no pipeline it records is being made, or its +/// submission checks. Not for a rebind's encoding or a CPU decode: neither is bounded by work +/// already submitted. A head ready for that look is on its way too. +fn on_its_way( + held: &Held, + binary: &BinarySignals, + decodes: &BTreeMap, + untracked_decodes: usize, +) -> bool { + if !events_land(held) { + return false; + } + match holds_back(held, binary, decodes, untracked_decodes) { + None | Some(HeldFor::Check) => true, + Some(HeldFor::HostStep) => { + let (buffer, index) = (&held.buffer, held.index); + buffer.stepping(index) || !buffer.step_waits(index).pipelines + } + Some(_) => false, + } +} + +/// Whether every event value `held` waits for is reached, or reached by a GPU signal already +/// submitted (`EventShared::landing`), a shared event's whose wait is held on the host until it +/// lands included: the value comes without anything not yet submitted. +fn events_land(held: &Held) -> bool { + (held.waits.iter()).all(|(event, value)| event.landing(*value).is_some()) +} + +/// The unshared queues' line heads on their way ([`goes_first`], [`on_its_way`]) that wait behind +/// no earlier commit ([`is_behind`], `memo` this look's), each with its `VkQueue` and its commit's +/// place among the unshared commits posted there: another queue's batch there whose room came +/// after that commit was posted waits for it (`unshared_hold.rs`, "Commits on their way"). One +/// that waits behind an earlier commit holds nothing, so no batch waits for one that waits for it: +/// each look asks again. +fn unshared_on_their_way( + order: &Order, + binary: &BinarySignals, + (decodes, untracked_decodes): (&BTreeMap, usize), + memo: &mut Behind>>>, +) -> Vec<(usize, u64)> { + let heads: Vec<(u64, usize, u64)> = (order.heads()) + .filter(|(_, held)| { + goes_first(held) && on_its_way(held, binary, decodes, untracked_decodes) + }) + .filter_map(|(sequence, held)| { + let index = held.buffer.unshared_vk_queue()?; + Some((sequence, index, held.unshared_place?)) + }) + .collect(); + (heads.into_iter()) + .filter(|&(sequence, ..)| !is_behind(order, binary, memo, sequence)) + .map(|(_, index, place)| (index, place)) + .collect() } /// Whether [`submit_plain_ready`] leaves `held` to the scheduler's thread for an unshared queue's @@ -2424,7 +2500,7 @@ fn plain( // (its passes note the commits it waits behind). theirs && held.buffer.held_for_unshared().is_none() - && (held.buffer.unshared_commits()).is_none_or(|(posted, taken)| taken >= posted) + && (held.buffer.unshared_commits()).is_none_or(|(_, (posted, taken))| taken >= posted) } /// The checks of the first line head in commit order whose checks a committing thread may run diff --git a/crates/metal2vulkan-runtime/src/scheduler/order.rs b/crates/metal2vulkan-runtime/src/scheduler/order.rs index c2e935833..b8efba095 100644 --- a/crates/metal2vulkan-runtime/src/scheduler/order.rs +++ b/crates/metal2vulkan-runtime/src/scheduler/order.rs @@ -229,6 +229,13 @@ impl Order { false } + /// Each line's head, with its commit sequence number. + pub(crate) fn heads(&self) -> impl Iterator + '_ { + (self.lines.iter()) + .filter_map(|(_, line)| line.front()) + .map(|(sequence, head)| (*sequence, head)) + } + /// The commit sequence numbers of the line heads `select` holds for. pub(crate) fn heads_where(&self, mut select: impl FnMut(&B) -> bool) -> Vec { self.lines diff --git a/crates/metal2vulkan-runtime/src/tests/queue_share_device.rs b/crates/metal2vulkan-runtime/src/tests/queue_share_device.rs index 9f71ba88b..4f7abc24a 100644 --- a/crates/metal2vulkan-runtime/src/tests/queue_share_device.rs +++ b/crates/metal2vulkan-runtime/src/tests/queue_share_device.rs @@ -55,6 +55,13 @@ //! head, which that pass may not submit (a scheduled handler; a host step recorded, the steps' //! thread held at trace points): the unshared one goes first, as the scheduler's own pass sends //! it. +//! - **Commits on their way** (`unshared_hold.rs`). The unshared queue's head committed and taken +//! in, held at its host step (the steps' thread held at a trace point) or waiting for a shared +//! event's value whose signal, at the end of a long command buffer, was submitted: an +//! application's command buffer committed then, whose room comes at once, goes after it, by the +//! scheduler's own pass and by a committing thread's. One that waits behind an earlier commit of +//! the held queue (a fill of the same range) holds nothing, nor one waiting for a value nothing +//! submitted reaches: neither the held batch nor the unshared one waits for the other. //! //! Where the device does not share (`PerQueue`, its check found no idling, or it has no two //! `VkQueue`s of one family: MoltenVK, RADV, lavapipe) each test asserts that no queue moves, and @@ -1668,7 +1675,9 @@ fn a_waiting_thread_s_pass_leaves_work_behind_an_unshared_head_it_cannot_send() /// (a dispatch whose pipeline's making the device's pool held, `pool::held`), which such a pass /// never submits, where the scheduler's own pass would send it first: the steps' thread is held /// before the step (`trace::Point::Step`) while the scheduler's thread is held in a submission -/// handler, its pass finding the dispatch stepping, then let go to record it. So the committing +/// handler, its pass finding the dispatch stepping, then let go to record it. The holding command +/// buffer is another unshared queue's, held back for none: an application's would wait behind the +/// dispatch on its way (`unshared_hold.rs`, "Commits on their way"). So the committing /// thread's pass leaves the application's command buffer to the scheduler's thread too /// (`unshared_hold.rs`, "Passes of other threads"). Under the default `CompletionPolicy`: where /// the device's batches group, the committing thread's pass sends the application's batch alone, @@ -1693,7 +1702,8 @@ fn a_committing_thread_s_pass_leaves_work_behind_an_unshared_head_with_its_step_ trace::reached(trace::Point::Step, WAIT), "the dispatch's host step was never handed over" ); - let held = HeldThread::new(&app, &holder); + let holding = device.new_command_queue_unshared(64).unwrap(); + let held = HeldThread::new(&holding, &holder); trace::hold_at(trace::Point::Stepped); trace::release(trace::Point::Step); assert!( @@ -1717,3 +1727,211 @@ fn a_committing_thread_s_pass_leaves_work_behind_an_unshared_head_with_its_step_ "the dispatch did not write what Metal's writes" ); } + +/// How many long command buffers' fills of [`long_fills`] the signalling command buffer of +/// [`an_unshared_head_waiting_for_a_signal_to_land_goes_before_work_whose_room_came_after`] takes: +/// about 90 ms of GPU time, which the test's commits and the scheduler's look come well within. +const LANDING_RUNS: usize = 3; + +/// Wait until a look of the scheduler's held `other` behind an unshared commit +/// (`heads_held_for_unshared_commits` rose from `before`) or its submission began: what that look +/// did with it. +fn looked(device: &Device, before: &WorkCounts, other: &CommandBuffer, leg: &str) { + let start = Instant::now(); + while device.work_counts().heads_held_for_unshared_commits + == before.heads_held_for_unshared_commits + && !other.submission_began() + { + assert!( + start.elapsed() < WAIT, + "{leg}: no look held the other queue's command buffer or submitted it" + ); + std::thread::sleep(Duration::from_millis(1)); + } +} + +/// Commit `command_buffer`, with `commit_and_submit` where `at_commit`. +fn committed(command_buffer: &CommandBuffer, at_commit: bool) { + match at_commit { + true => command_buffer.commit_and_submit().unwrap(), + false => command_buffer.commit().unwrap(), + } +} + +/// The legs of the commits-on-their-way tests: the application's command buffer committed with +/// `commit` (the scheduler's own pass looks at it) and with `commit_and_submit` (the committing +/// thread's pass first). +const LEGS: [(&str, bool); 2] = [ + ("the scheduler's pass", false), + ("a committing thread's pass", true), +]; + +/// The unshared queue's head, committed and taken in, waits for its host step, which the steps' +/// thread holds at its trace point (`trace::Point::Step`; a dispatch whose pipeline's making the +/// device's pool held, so the step records it): an application's command buffer committed then, +/// whose room comes at once (each queue's expected run a small fill's), is held behind it, the +/// scheduler's pass finding its room after the unshared commit was posted (`unshared_hold.rs`, +/// "Commits on their way"), and a committing thread's pass leaving it to that thread +/// ("Passes of other threads"). Once the step is let go, the unshared one goes first. Without the +/// rule the application's goes as the step records, ahead of the unshared one. Under +/// `CompletionPolicy::Exact`, where every pass submits any queue's batch alone. +#[test] +fn an_unshared_head_at_its_host_step_goes_before_work_whose_room_came_after_its_commit() { + for (leg, at_commit) in LEGS { + let scratch = Scratch::new(&format!("unshared-hold-stepping-{at_commit}")); + let (device, app, unshared) = beside_unshared(&scratch, CompletionPolicy::Exact); + let state = dispatch_pending::state(&device); + let buffers = Buffers::new(&device); + let target = (device.new_buffer(4096, ResourceOptions::private())).unwrap(); + let let_go = StepsLetGo; + let making = crate::pipeline::pool::held::hold(); + let grid = Grid::One(Size::new(STEP_THREADS, 1, 1)); + let drawn = dispatch_pending::dispatch(&unshared, &state, &buffers, grid); + drawn.commit().unwrap(); + trace::hold_at(trace::Point::Step); + drop(making); + assert!( + trace::reached(trace::Point::Step, WAIT), + "{leg}: the dispatch's host step was never handed over" + ); + let before = device.work_counts(); + let other = small_fill(&app, &target); + committed(&other, at_commit); + looked(&device, &before, &other, leg); + trace::release(trace::Point::Step); + drop(let_go); + unshared_went_first(leg, &drawn, &other); + let held = device.work_counts().heads_held_for_unshared_commits + - before.heads_held_for_unshared_commits; + println!("{leg}: {held} looks held it behind the unshared head at its host step"); + assert!(held > 0, "{leg}: a look held it behind the unshared commit"); + assert!( + buffers.written(STEP_THREADS), + "{leg}: the dispatch did not write what Metal's writes" + ); + } +} + +/// The unshared queue's head waits for a shared event's value, which a long command buffer of +/// the application's queue signals at its end, submitted and running (a shared event's wait is +/// held on the host until the signal lands, `event_timeline.rs`): an application's command buffer +/// committed then, whose room comes at once (the application's expected run a small fill's, the +/// long one's length measured on a queue of its own), is held behind it until the signal landed +/// and the unshared one went (`unshared_hold.rs`, "Commits on their way"). Without the rule the +/// application's goes at once, about the long one's length ahead of the unshared one. +#[test] +fn an_unshared_head_waiting_for_a_signal_to_land_goes_before_work_whose_room_came_after() { + for (leg, at_commit) in LEGS { + let scratch = Scratch::new(&format!("unshared-hold-landing-{at_commit}")); + let (device, app, unshared) = beside_unshared(&scratch, CompletionPolicy::Exact); + let big = (device.new_buffer(FILL_BYTES, ResourceOptions::private())).unwrap(); + let [target, small] = + [(); 2].map(|()| (device.new_buffer(4096, ResourceOptions::private())).unwrap()); + let fills = long_fills(&device.new_command_queue().unwrap(), &big) * LANDING_RUNS; + let event = device.new_shared_event().unwrap(); + let signalling = long(&app, &big, fills); + signalling.encode_signal_event((&event).into(), 1).unwrap(); + signalling.commit().unwrap(); + signalling.wait_until_scheduled().unwrap(); + let drawn = unshared.command_buffer().unwrap(); + drawn.encode_wait_for_event((&event).into(), 1).unwrap(); + let mut blit = drawn.blit_command_encoder().unwrap(); + blit.fill_buffer(&small, 0..256, 1).unwrap(); + blit.end_encoding().unwrap(); + drawn.commit().unwrap(); + let before = device.work_counts(); + let other = small_fill(&app, &target); + committed(&other, at_commit); + looked(&device, &before, &other, leg); + unshared_went_first(leg, &drawn, &other); + signalling.wait_until_completed_for(WAIT).unwrap(); + let held = device.work_counts().heads_held_for_unshared_commits + - before.heads_held_for_unshared_commits; + let after = (other.kernel_start_time() - signalling.gpu_end_time().unwrap()) * 1e3; + println!( + "{leg}: {held} looks held it behind the unshared head waiting for the signal; it began \ + {after:.3} ms after the signalling one's end" + ); + assert!(held > 0, "{leg}: a look held it behind the unshared commit"); + } +} + +/// An unshared queue's head that waits behind an earlier commit of the queue a batch is held back +/// for holds nothing (`unshared_hold.rs`, "Commits on their way"), or neither would go: the +/// application's `earlier`, a fill of the range the unshared queue's `drawn` fills next, is held +/// back behind its long command buffer (the application's expected run that long), and `drawn`, +/// committed after it, waits behind it in the tracked order (`order.rs`) though ready otherwise. +/// Once the long one has less than the budget left, `earlier` goes, and `drawn` after it. +#[test] +fn an_unshared_head_behind_an_earlier_commit_of_the_held_queue_holds_nothing() { + let scratch = Scratch::new("unshared-hold-behind"); + let (device, app, unshared) = beside_unshared(&scratch, CompletionPolicy::Exact); + let big = (device.new_buffer(FILL_BYTES, ResourceOptions::private())).unwrap(); + let shared = (device.new_buffer(4096, ResourceOptions::private())).unwrap(); + let fills = long_fills(&app, &big); + let before = device.work_counts(); + let running = long(&app, &big, fills); + running.commit().unwrap(); + running.wait_until_scheduled().unwrap(); + let earlier = small_fill(&app, &shared); + earlier.commit().unwrap(); + let drawn = small_fill(&unshared, &shared); + drawn.commit().unwrap(); + for command_buffer in [&running, &earlier, &drawn] { + command_buffer + .wait_until_completed_for(WAIT) + .expect("each completes: the held batch and the unshared one wait for no other"); + assert_eq!(command_buffer.status(), CommandBufferStatus::Completed); + } + let after = device.work_counts(); + let behind = (drawn.kernel_start_time() - earlier.kernel_start_time()) * 1e3; + let held = after.heads_held_for_unshared_isolation - before.heads_held_for_unshared_isolation; + let waited = after.heads_held_for_earlier_commits - before.heads_held_for_earlier_commits; + println!( + "the unshared one began {behind:.3} ms after the earlier one; looks held for isolation \ + {held}, behind an earlier commit {waited}" + ); + assert!( + held > 0, + "the earlier one was held back for the unshared queue" + ); + assert!(waited > 0, "the unshared one waited behind the earlier one"); + assert!( + behind > 0.0, + "the earlier one went first, in the tracked order" + ); +} + +/// An unshared queue's head that waits for a shared event's value nothing submitted reaches holds +/// nothing (`unshared_hold.rs`, "Commits on their way"): an application's command buffer committed +/// after it completes while it waits, by the scheduler's pass and by a committing thread's, and it +/// goes once the host sets the value. +#[test] +fn an_unshared_head_waiting_for_a_value_nothing_submitted_reaches_holds_nothing() { + for (leg, at_commit) in LEGS { + let scratch = Scratch::new(&format!("unshared-hold-unreached-{at_commit}")); + let (device, app, unshared) = beside_unshared(&scratch, CompletionPolicy::Exact); + let [target, small] = + [(); 2].map(|()| (device.new_buffer(4096, ResourceOptions::private())).unwrap()); + let event = device.new_shared_event().unwrap(); + let drawn = unshared.command_buffer().unwrap(); + drawn.encode_wait_for_event((&event).into(), 1).unwrap(); + let mut blit = drawn.blit_command_encoder().unwrap(); + blit.fill_buffer(&small, 0..256, 1).unwrap(); + blit.end_encoding().unwrap(); + drawn.commit().unwrap(); + let other = small_fill(&app, &target); + committed(&other, at_commit); + other + .wait_until_completed_for(WAIT) + .expect("the other queue's command buffer completes while the unshared one waits"); + assert_eq!(other.status(), CommandBufferStatus::Completed, "{leg}"); + assert!( + !drawn.submission_began(), + "{leg}: the unshared one waits for the value" + ); + event.set_signaled_value(1); + drawn.wait_until_completed_for(WAIT).unwrap(); + assert_eq!(drawn.status(), CommandBufferStatus::Completed, "{leg}"); + } +} diff --git a/crates/metal2vulkan-runtime/src/tests/unshared_hold.rs b/crates/metal2vulkan-runtime/src/tests/unshared_hold.rs index 1b355861d..ec882fb2a 100644 --- a/crates/metal2vulkan-runtime/src/tests/unshared_hold.rs +++ b/crates/metal2vulkan-runtime/src/tests/unshared_hold.rs @@ -22,6 +22,8 @@ //! - **Commits not yet taken in.** A batch whose room came is held while an unshared queue's commit //! posted before that is not taken into the scheduler's order, and goes once it is, whatever //! was posted since: a compositor posting during every pass holds it for one pass at most. +//! - **Commits on their way.** It is held too while an unshared commit posted before its room came +//! leads its line on its way, and goes once none does, one posted since on its way aside. use crate::unshared_hold::{ active, behind_commits, Expected, Flies, Flight, Room, ACTIVE, BUDGET, IDLE_BUDGET, @@ -373,26 +375,60 @@ fn a_quiet_compositor_s_first_command_buffer_waits_behind_the_idle_budget_and_on #[test] fn a_batch_waits_for_the_unshared_commits_posted_before_its_room_came_alone() { let mut noted = None; - assert!(!behind_commits(&mut noted, (2, 2)), "every commit taken in"); + assert!( + !behind_commits(&mut noted, (2, 2), none()), + "every commit taken in" + ); assert_eq!(noted, Some(2)); assert!( - !behind_commits(&mut noted, (5, 2)), + !behind_commits(&mut noted, (5, 2), none()), "commits posted after its room came never hold it" ); let mut noted = None; assert!( - behind_commits(&mut noted, (3, 2)), + behind_commits(&mut noted, (3, 2), none()), "a commit posted before its room came is not taken in" ); assert_eq!(noted, Some(3)); assert!( - behind_commits(&mut noted, (4, 2)), + behind_commits(&mut noted, (4, 2), none()), "still not taken in, another posted meanwhile" ); assert!( - !behind_commits(&mut noted, (5, 3)), + !behind_commits(&mut noted, (5, 3), none()), "the pass that took it in lets it go, the commits posted during that pass aside" ); assert_eq!(noted, Some(3), "noted once"); } + +/// No unshared commit on its way. +fn none() -> std::iter::Empty { + std::iter::empty() +} + +/// A batch whose room came with every unshared commit taken in waits while one posted before its +/// room came leads its line on its way (its place at or below the commits posted then), and goes +/// once none does; one posted after its room came holds it on its way or not. +#[test] +fn a_batch_waits_for_the_unshared_commits_on_their_way_posted_before_its_room_came() { + let mut noted = None; + assert!( + behind_commits(&mut noted, (3, 3), [3].into_iter()), + "the third commit, posted before its room came, is on its way" + ); + assert_eq!(noted, Some(3)); + assert!( + behind_commits(&mut noted, (4, 4), [2, 4].into_iter()), + "the second is on its way too, whatever came after" + ); + assert!( + !behind_commits(&mut noted, (5, 5), [4, 5].into_iter()), + "commits posted after its room came never hold it" + ); + assert!( + !behind_commits(&mut noted, (5, 5), none()), + "none on its way: it goes" + ); + assert_eq!(noted, Some(3), "noted once"); +} diff --git a/crates/metal2vulkan-runtime/src/unshared_hold.rs b/crates/metal2vulkan-runtime/src/unshared_hold.rs index a4c70c40d..335097e43 100644 --- a/crates/metal2vulkan-runtime/src/unshared_hold.rs +++ b/crates/metal2vulkan-runtime/src/unshared_hold.rs @@ -72,18 +72,40 @@ //! frame's command buffers in 106 to 129 of about 611 frames a 12 s walk, and those were 7 to 23 //! of the 9 to 27 frames answered more than a refresh after their take. Counted: //! `WorkCounts::heads_held_for_unshared_commits`. +//! - **Commits on their way.** A commit taken in may still wait for the runtime before its batch +//! goes: for its host step (its gate, work already submitted, then its recording on a steps' +//! thread), for its submission checks, or for an event value that a signal already submitted +//! reaches and that has not landed (a shared event's, whose wait is held on the host). A pass +//! that finds another queue's batch's room meanwhile would send that batch ahead of it, and the +//! unshared queue's command buffer would then wait for the whole batch. So a batch whose room +//! came is held too while an unshared queue's commit on its `VkQueue`, posted before its room +//! came, leads its line on its way so ([`behind_commits`]; each commit carries its place among +//! those posted there, `SlotShare::posted_unshared`). The step's end, the checks' end or the +//! signal's landing wakes the scheduler's thread, whose next pass sends the unshared one first +//! and the held batch next. Each look asks again, and such a commit holds nothing while it waits +//! for anything else: an event value no submitted signal reaches, a binary semaphore's signal, a +//! pipeline's making, a CPU decode, a rebind's encoding, or a batch committed before it that it +//! waits behind (which may be the held batch itself: neither then waits for the other). The +//! bound: the steps, checks and landings of the unshared commits posted before the room came. +//! Beside a guest compositor on the Radeon 780M (reims's traced walk ld91, 12 s), another queue's +//! batch went in while a compositor's command buffer was so on its way ahead of 33 of the 46 +//! frames answered more than a refresh after their take: 20 at its host step, 13 waiting for +//! the landing of its other unshared queue's signal. Counted: +//! `WorkCounts::heads_held_for_unshared_commits`. //! - **Passes of other threads.** A thread about to block, and a thread that just committed with //! `CommandBuffer::commit_and_submit`, pass for themselves, and submit only the batches that //! need nothing of the scheduler's thread (`scheduler/mod.rs`'s `submit_plain_ready`). Where //! such a pass leaves an unshared queue's head that the scheduler's own look would find ready //! and send first (one with a submission or scheduled handler, for a thread about to block; one -//! whose host step is recorded; one a switch of storage forms rides on), it leaves the other -//! queues' batches held back on that head's `VkQueue` to the scheduler's thread too, which it -//! wakes: that thread's next pass sends the unshared one first and them next. They wait for that -//! wake and pass, no more. An unshared head the scheduler's thread would not find ready either -//! (an event value not reached, a host step not recorded, its checks) leaves them to the pass, -//! as that thread's own pass lets them go then. Without the rule such a pass sent another -//! queue's batch ahead of the unshared one (`tests::queue_share_device`). +//! whose host step is recorded; one a switch of storage forms rides on), or one on its way +//! (above), it leaves the other queues' batches held back on that head's `VkQueue` to the +//! scheduler's thread too, which it wakes: that thread's next pass sends the unshared one first +//! and them next, or holds them while it is on its way. They wait for that wake and pass, or the +//! hold above, no more. An unshared head that waits for anything else (an event value no +//! submitted signal reaches, a binary semaphore's signal, a pipeline's making, a decode, a +//! rebind) leaves them to the pass, as that thread's own pass lets them go then. Without the +//! rule such a pass sent another queue's batch ahead of the unshared one +//! (`tests::queue_share_device`). //! - **Deferred commits** (a known limit). Under `RenderLoweringPolicy::Worker`, an unshared //! queue's commit left to the device's lowering thread (`render/queued_commits.rs`) counts as //! posted only once its own work ran and its batches reach the scheduler, so a batch whose room @@ -130,12 +152,20 @@ pub(crate) const IDLE_BUDGET: u64 = 16_000_000; /// unshared queue's presenting is the program's. pub(crate) const ACTIVE: u64 = 50_000_000; -/// Whether a batch whose room came is held behind an unshared queue's commit not yet in the -/// scheduler's order (the module docs), its `VkQueue`'s unshared commits posted and taken in so -/// far being `posted` and `taken`: `noted` keeps the commits posted at the first look that found -/// its room, those it waits behind; any posted later never hold it. -pub(crate) fn behind_commits(noted: &mut Option, (posted, taken): (u64, u64)) -> bool { - taken < *noted.get_or_insert(posted) +/// Whether a batch whose room came is held behind an unshared queue's commit (the module docs, +/// "Commits not yet taken in" and "Commits on their way"), its `VkQueue`'s unshared commits posted +/// and taken in so far being `posted` and `taken`, and `on_their_way` the places among those +/// posted (`SlotShare::posted_unshared`) of the unshared commits leading their lines there on +/// their way: `noted` keeps the commits posted at the first look that found its room, those it +/// waits behind, while one of them is not yet taken in or is on its way; any posted later never +/// hold it. +pub(crate) fn behind_commits( + noted: &mut Option, + (posted, taken): (u64, u64), + mut on_their_way: impl Iterator, +) -> bool { + let noted = *noted.get_or_insert(posted); + taken < noted || on_their_way.any(|place| place <= noted) } /// A command queue's expected run of one batch (the module docs), in nanoseconds; 0 before the diff --git a/crates/metal2vulkan-runtime/src/work_counts.rs b/crates/metal2vulkan-runtime/src/work_counts.rs index 640e147d3..96b0a57c3 100644 --- a/crates/metal2vulkan-runtime/src/work_counts.rs +++ b/crates/metal2vulkan-runtime/src/work_counts.rs @@ -3505,7 +3505,8 @@ impl Counters { } /// A look held a head whose room had come behind an unshared queue's commit on its `VkQueue` - /// not yet taken into the scheduler's order (`unshared_hold.rs`, "Commits not yet taken in"). + /// not yet taken into the scheduler's order, or leading its line on its way + /// (`unshared_hold.rs`, "Commits not yet taken in", "Commits on their way"). pub(crate) fn head_held_for_unshared_commits(&self) { self.heads_held_for_unshared_commits .fetch_add(1, Ordering::Relaxed); @@ -7365,7 +7366,7 @@ pub struct WorkCounts { /// behind about one batch of theirs. While no unshared queue there committed a command /// buffer within the last 50 ms, 16 ms or more (`heads_let_go_unshared_idle`). With them, the /// looks that held one behind an unshared queue's commit there not yet taken into the - /// scheduler's order (`heads_held_for_unshared_commits`). + /// scheduler's order, or on its way (`heads_held_for_unshared_commits`). pub heads_held_for_unshared_isolation: u64, /// Heads a look let go with 2 ms or more of other queues' expected GPU work in flight on /// their `VkQueue`, which would hold them for an unshared queue there @@ -7376,11 +7377,14 @@ pub struct WorkCounts { /// batch, and the 2 ms bound applies from its commit on. pub heads_let_go_unshared_idle: u64, /// Of `heads_held_for_unshared_isolation`, the looks that held a head whose room on its - /// `VkQueue` had come behind an unshared queue's commit there not yet taken into the - /// scheduler's order, posted before that room came (`unshared_hold.rs`): a scheduler pass - /// takes its commits in at its start, so one posted during the pass waits for the next, which - /// sends the unshared queue's command buffer first and the held head next. Commits posted - /// after its room came never hold it. + /// `VkQueue` had come behind an unshared queue's commit there posted before that room came + /// (`unshared_hold.rs`): one not yet taken into the scheduler's order (a scheduler pass takes + /// its commits in at its start, so one posted during the pass waits for the next), or one + /// leading its line on its way, waiting only for its host step, its submission checks or a + /// signal already submitted to land. The pass that takes it in, or that its step, checks or + /// landing wake the scheduler's thread for, sends the unshared queue's command buffer first + /// and the held head next. Commits posted after its room came never hold it, nor one that + /// waits for anything else. pub heads_held_for_unshared_commits: u64, /// Times a look left a head that needs a host step waiting at its gate: the step reads what /// the GPU may write, so it waits until the batches that last wrote what it reads completed diff --git a/crates/metal2vulkan/src/cache/mod.rs b/crates/metal2vulkan/src/cache/mod.rs index 290eb7b2a..87b506812 100644 --- a/crates/metal2vulkan/src/cache/mod.rs +++ b/crates/metal2vulkan/src/cache/mod.rs @@ -94,7 +94,7 @@ pub const ARTIFACT_FORMAT_VERSION: u32 = 522; /// changes no behaviour, and then updates that hash alone with the commit saying why, or changes /// behaviour and also bumps this version. The commit that bumps it says what changed; this comment /// keeps no history. -pub const RUNTIME_VERSION: u32 = 2309; +pub const RUNTIME_VERSION: u32 = 2310; /// The digest of the sources this build of the crate was compiled from: SHA-256, as lowercase hex, /// of the manifest, the build script, and exactly the files the crate roots' module tree compiles diff --git a/docs/API.md b/docs/API.md index 2876976e6..81451eea9 100644 --- a/docs/API.md +++ b/docs/API.md @@ -283,9 +283,10 @@ compile does. `DevicePolicy::default()` is `DevicePolicy::UNTRUSTED`: whole of one, however long, as a submission runs to its end. A scheduler pass that finds a held batch let go and the unshared queue's command buffer ready together (a pass the host ran late) sends the unshared one first, and a batch whose room comes during a pass waits while the - unshared queue's commit, posted before that, is not yet taken in by a pass + unshared queue's commit, posted before that, is not yet taken in by a pass, or is taken in and + waits only for its host step, its submission checks or a signal already submitted to land (`WorkCounts::heads_held_for_unshared_commits`); commits posted after its room came never hold - it. The 2 ms holds while an unshared queue there committed a command + it, nor one that waits for anything else. The 2 ms holds while an unshared queue there committed a command buffer within the last 50 ms, so a compositor drawing 20 frames a second or more (a 24-frame video's, a 30-frame game's) keeps it; while none has, the budget is 16 ms: an application keeping less queued beside a quiet compositor is not held, and the compositor's first command diff --git a/docs/FAST-PATHS.md b/docs/FAST-PATHS.md index b1d062ce6..708739523 100644 --- a/docs/FAST-PATHS.md +++ b/docs/FAST-PATHS.md @@ -135,12 +135,21 @@ known. The rest of the differences are docs/API.md section 9's precision notes. batch next, and commits posted after its room came never hold it (`WorkCounts::heads_held_for_unshared_commits`). Without that, beside a guest compositor on the Radeon 780M, 7 to 23 of the 9 to 27 frames a 12 s walk answered more than a refresh late had a - batch let go during such a pass ahead of them. A thread about to block, or one that just + batch let go during such a pass ahead of them. Such a batch waits too while a compositor's + commit posted before its room came is taken in and waits only for its host step, its + submission checks, or a signal already submitted to land (a shared event's value from its other + unshared queue): the step's end, the checks' or the landing wakes the scheduler's thread, which + sends the compositor's first; the bound is those steps, checks and landings, and a commit + waiting for anything else, or behind an earlier commit, holds nothing. Without that, in + reims's traced walk ld91 on the Radeon 780M, 33 of the 46 frames answered more than a refresh + late had another queue's batch go in while a compositor's command buffer so waited (20 at its + host step, 13 for a landing). A thread about to block, or one that just committed with `commit_and_submit`, passes for itself and submits only batches that need nothing of the scheduler's thread; where it leaves a compositor's command buffer that the scheduler's thread would send first (a scheduled or submission handler for a thread about to - block, a host step recorded), it leaves the other queues' batches on that `VkQueue` to that - thread too, which then sends the compositor's first: they wait for one wake and pass of it. + block, a host step recorded) or hold the batches behind (above), it leaves the other queues' + batches on that `VkQueue` to that thread too, which then sends the compositor's first: they + wait for one wake and pass of it, or that hold. Under `RenderLoweringPolicy::Worker` a compositor's commit left to the lowering thread holds no batch until its work ran (`unshared_hold.rs`, "Deferred commits"). Two budgets (`unshared_hold.rs`): `BUDGET`, 2 ms, while an unshared queue there committed a command buffer within the last 50 ms (`ACTIVE`: a compositor