diff --git a/crates/metal2vulkan-runtime/README.md b/crates/metal2vulkan-runtime/README.md index ba04d1a2f..de0d652eb 100644 --- a/crates/metal2vulkan-runtime/README.md +++ b/crates/metal2vulkan-runtime/README.md @@ -697,7 +697,8 @@ Calls Metal does not have, for a consumer that emulates a Metal device: Metal submits an `MTLEvent` wait at once), and its own submission handlers after it, in submission order with every other command buffer's (those of the other command buffers its pass submits go to the device's handler threads as it goes on; where it has submission handlers, its - pass ends right after its own last batch, so they run before any later batch's submission). + pass ends right after its own last batch, so they run before any later batch's submission, and + it wakes the scheduler's thread for the rest before it runs them). What it leaves goes to the scheduler's thread, which it wakes, as it does after a batch it submitted alone where the batch may wait in a group. No wake and no thread hop lies on its way to the `VkQueue`, where `commit`'s submission is the scheduler thread's, as Metal's is its own thread's diff --git a/crates/metal2vulkan-runtime/src/command_buffer/mod.rs b/crates/metal2vulkan-runtime/src/command_buffer/mod.rs index 3deeeb25b..e14f2472d 100644 --- a/crates/metal2vulkan-runtime/src/command_buffer/mod.rs +++ b/crates/metal2vulkan-runtime/src/command_buffer/mod.rs @@ -4073,8 +4073,8 @@ impl CommandBuffer { /// device's handler threads as the pass goes on, as the scheduler thread's passes hand /// theirs, so none waits for the rest of the pass. Where this command buffer has submission /// handlers, its pass ends right after its last batch, so they run before any later batch's - /// submission; what else is ready goes to the scheduler's thread, which it wakes - /// (`WorkCounts::scheduler_passes_at_commit_ended_early`). + /// submission; what else is ready goes to the scheduler's thread, which it wakes before they + /// run (`WorkCounts::scheduler_passes_at_commit_ended_early`). /// - **Scheduled.** Where this call submitted its last batch and it has no scheduled and no /// submission handler, it reads `Scheduled` before this returns, unless its queue's handler /// thread holds an earlier transition. Otherwise its transition to `Scheduled` runs on its diff --git a/crates/metal2vulkan-runtime/src/scheduler/mod.rs b/crates/metal2vulkan-runtime/src/scheduler/mod.rs index 9422d4a71..5abf9031f 100644 --- a/crates/metal2vulkan-runtime/src/scheduler/mod.rs +++ b/crates/metal2vulkan-runtime/src/scheduler/mod.rs @@ -31,9 +31,10 @@ //! hand theirs (**Submission handlers**, below); their transitions to `Scheduled` run where //! their handlers ran, where plain, else go to their queues' contexts. Where its own command //! buffer has submission handlers, the pass ends right after that command buffer's last batch, -//! leaving the rest to this thread: they run once the lock is free, before any later batch's -//! submission, and no later command buffer's handlers wait in their turn for the rest of the -//! pass (`WorkCounts::scheduler_passes_at_commit_ended_early`). The first head in commit order ready but for its +//! leaving the rest to this thread, which it wakes as soon as the lock is free: they run then, +//! before any later batch's submission, no later command buffer's handlers wait in their turn +//! for the rest of the pass, and this thread's pass submits the rest meanwhile +//! (`WorkCounts::scheduler_passes_at_commit_ended_early`). The first head in commit order ready but for its //! submission checks, every value it waits for reached on the device or by the host, and behind //! no earlier commit, has its checks run there too, without the lock, the head marked so this //! thread leaves them ([`claim_check`]), and is submitted by a pass of the committing thread's @@ -1124,7 +1125,8 @@ impl Scheduler { /// commit ([`submit_plain_ready`], [`Passer::Committing`]), handing the submission handlers of /// every command buffer but its own to the device's handler threads as it goes on /// ([`HandOff`]), and ending right after its own command buffer's last batch where that one has - /// submission handlers; then, still counted and without the lock, it runs its own command + /// submission handlers, waking the thread for the rest as soon as the lock is free + /// ([`AtCommit::handed_over`]); then, still counted and without the lock, it runs its own command /// buffer's in their turn ([`Turns`]) and lets go of what the pass held, a command buffer's /// last reference on a handler thread. Where a pass that did not so end left a head that waits /// only for its submission checks, and this thread may run them ([`claim_check`]), it runs them @@ -1208,6 +1210,12 @@ impl Scheduler { submitted += pass.submitted; alone += pass.alone; let (left_over, left) = (pass.left_over, pass.left); + // A pass that ended at its own command buffer's handlers left the rest to the thread, + // which goes on with it now, while this one runs them ([`AtCommit::handed_over`]): its + // look reads the mark the pass left, so it passes whether it sleeps or not. + if pass.handed_over { + shared.wake_thread(Woken::Host, shared.nudge.get()); + } // The completion turn a command buffer the pass took with no submission left this // thread, before the handlers and the check, as the scheduler's thread makes it before // its handlers ([`round`]). @@ -2306,8 +2314,13 @@ struct AtCommit { alone: usize, /// The checks of a head it left, taken for the thread to run ([`claim_check`]). check: Option, - /// It left batches in the order or the group. + /// It left batches in the order or the group, for the thread to be woken for once the + /// committing thread's work is done. left_over: bool, + /// It ended at its own command buffer's submission handlers with batches left, and marked the + /// state for the thread's pass (`State::dirty`): the committing thread wakes it as soon as the + /// lock is free, before it runs those handlers, so those batches wait for no handler. + handed_over: bool, /// Where it submitted nothing, why the committing thread's command buffer's first batch was /// left ([`LeftAtCommit`]). left: Option, @@ -2322,7 +2335,8 @@ impl AtCommit { /// run inside its call; it hands those of every other command buffer it takes to the device's /// handler threads as it goes on (`hand_off`, [`submit_plain_ready`]). Where it kept them, it /// ended right after `ours`'s last batch, and takes no check: what is left is the scheduler's - /// thread's (counted where any is, `WorkCounts::scheduler_passes_at_commit_ended_early`). + /// thread's, the state marked for its pass (counted where any is, + /// `WorkCounts::scheduler_passes_at_commit_ended_early`; [`AtCommit::handed_over`]). fn pass( state: &mut State, mut spent: Spent, @@ -2349,7 +2363,9 @@ impl AtCommit { false => None, }; let left_over = !state.order.is_empty() || !state.grouped.is_empty(); - if submitted.ended && left_over { + let handed_over = submitted.ended && left_over; + if handed_over { + state.dirty = true; ours.device().shared().counts.pass_at_commit_ended_early(); } AtCommit { @@ -2359,7 +2375,8 @@ impl AtCommit { submitted: submitted.batches, alone: submitted.alone, check, - left_over, + left_over: left_over && !handed_over, + handed_over, left, } } diff --git a/crates/metal2vulkan-runtime/src/tests/submission_turns.rs b/crates/metal2vulkan-runtime/src/tests/submission_turns.rs index d6afa12ef..c71d8043c 100644 --- a/crates/metal2vulkan-runtime/src/tests/submission_turns.rs +++ b/crates/metal2vulkan-runtime/src/tests/submission_turns.rs @@ -46,7 +46,10 @@ //! (one on another queue, one behind it on its own, one behind the first on the other), and its //! pass takes all four in. It submits its own and ends: its handler runs inside its call before //! any later batch's submission, where it once ran after the pass submitted all four. The -//! scheduler's thread submits the three, each queue's in its commit order. +//! scheduler's thread, let go once that handler started, submits the three while it runs, at the +//! wake the committing thread gave it as its pass ended, each queue's in its commit order, where +//! they once waited for that handler's end and a wake, or for the thread's sleep slice +//! (`WorkCounts::scheduler_slice_rescues`). //! //! And that such a gated transition to `Completed` takes one look for a thread to run it //! (`Contexts::post_gated`): a command buffer's submission handler waits until its work completed @@ -78,6 +81,10 @@ const WAIT: Duration = Duration::from_secs(10); /// long. const PASS_PAUSE: Duration = Duration::from_millis(200); +/// How long the committing command buffer's handler in the check of a committing pass's end waits +/// for the batches that pass left to be submitted. +const LEFT_OVER: Duration = Duration::from_secs(2); + /// The threads of the handler pool, and the queues, of the check at the pool's cap. const POOL: usize = 2; const QUEUES: usize = 6; @@ -535,8 +542,40 @@ fn a_committing_pass_ends_at_its_own(completion: CompletionPolicy) { // inbox, and the committing thread's pass takes it in. let held = super::waiter_submits::Held::new(&side, &targets[2]); let ours = fill(&first, &targets[0], 1); - noting(&ours, "ours"); let before = device.work_counts(); + // Each command buffer's submission's return (`CommandBuffer::submit_returned_time`). + device.shared().timing.note_queue_times(); + // Its handler notes the kernel submissions made as it starts, has the test let the + // scheduler's thread go, and waits, up to a bound, for the three later command buffers' + // submissions (one `vkQueueSubmit` may carry two of a queue's), noting how many were made as + // it ends. + let (entered, enters) = mpsc::channel::<()>(); + let watched: Arc>> = Arc::default(); + let at_end: Arc>> = Arc::default(); + { + let (noted, watched, at_end) = ( + Arc::clone(¬ed), + Arc::clone(&watched), + Arc::clone(&at_end), + ); + let entered = Mutex::new(entered); + let handler = Box::new(move |command_buffer: &CommandBuffer| { + let submissions = command_buffer.device().work_counts().queue_submissions; + noted.lock().unwrap().push(("ours", submissions)); + let _ = entered.lock().unwrap().send(()); + let submitted = || { + (watched.lock().unwrap().iter()) + .filter(|later| later.submit_returned_time() > 0.0) + .count() + }; + let deadline = std::time::Instant::now() + LEFT_OVER; + while submitted() < 3 && std::time::Instant::now() < deadline { + std::thread::sleep(Duration::from_millis(1)); + } + *at_end.lock().unwrap() = Some(submitted()); + }); + ours.add_submission_handler(handler).unwrap(); + } trace::hold_at(trace::Point::AtCommitPass); let committing = std::thread::spawn(move || ours.commit_and_submit().map(|()| ours)); assert!( @@ -556,11 +595,15 @@ fn a_committing_pass_ends_at_its_own(completion: CompletionPolicy) { command_buffer.commit().unwrap(); command_buffer }); + watched.lock().unwrap().extend(later.iter().cloned()); trace::release(trace::Point::AtCommitPass); + // Its pass ended and its handler started: the scheduler's thread goes on. + let started = enters.recv_timeout(WAIT).is_ok(); + drop(held); let ours = committing.join().unwrap().unwrap(); let after = device.work_counts(); let at_return = noted.lock().unwrap().clone(); - drop(held); + let at_end = *at_end.lock().unwrap(); for command_buffer in std::iter::once(&ours).chain(&later) { completes(command_buffer); } @@ -570,15 +613,35 @@ fn a_committing_pass_ends_at_its_own(completion: CompletionPolicy) { .map(|&(name, submissions)| (name, submissions - before.queue_submissions)) .collect(); println!( - "SUBMISSION-TURNS {} {completion:?}: a committing pass submitted {} batches, ended early {}; handlers in their turn, with the kernel submissions made by then: {submitted:?}", + "SUBMISSION-TURNS {} {completion:?}: a committing pass submitted {} batches, ended early \ + {}; handlers in their turn, with the kernel submissions made by then: {submitted:?}; \ + of the three later command buffers, submitted by the committing one's end: {at_end:?}, \ + the scheduler's thread's passes after a sleep slice that let a batch go: {}", device.name(), moved(|c| c.batches_submitted_at_commit), moved(|c| c.scheduler_passes_at_commit_ended_early), + moved(|c| c.scheduler_slice_rescues), + ); + assert!( + started, + "the committing command buffer's handler never ran ({completion:?})" ); assert_eq!( - (at_return.len(), submitted.first().copied()), - (1, Some(("ours", 1))), - "the committing command buffer's own submission handler did not run inside its call before any later batch's submission ({completion:?}): {submitted:?}" + ( + at_return.first().map(|&(name, _)| name), + submitted.first().copied() + ), + (Some("ours"), Some(("ours", 1))), + "the committing command buffer's own submission handler did not run inside its call \ + before any later batch's submission ({completion:?}): {submitted:?}" + ); + // Submitted by a pass the committing thread woke the scheduler's thread for, not by one after + // a sleep slice (`WorkCounts::scheduler_slice_rescues`), as they went with no such wake. + assert_eq!( + (at_end, moved(|c| c.scheduler_slice_rescues)), + (Some(3), 0), + "the batches the committing pass left were not submitted at a wake while the \ + committing command buffer's handler ran ({completion:?})" ); assert_eq!( ( @@ -592,7 +655,8 @@ fn a_committing_pass_ends_at_its_own(completion: CompletionPolicy) { assert_eq!( order, ["ours", "second's first", "first's next", "second's next"], - "submission handlers out of submission order, or a queue's batches out of commit order ({completion:?})" + "submission handlers out of submission order, or a queue's batches out of commit order \ + ({completion:?})" ); } diff --git a/crates/metal2vulkan-runtime/src/work_counts.rs b/crates/metal2vulkan-runtime/src/work_counts.rs index d8f4b0a29..eb8241d22 100644 --- a/crates/metal2vulkan-runtime/src/work_counts.rs +++ b/crates/metal2vulkan-runtime/src/work_counts.rs @@ -8157,8 +8157,8 @@ pub struct WorkCounts { /// Of those passes, the ones that ended right after they submitted their own command /// buffer's last batch, which has submission handlers, with batches left in the order: the /// committing thread runs those handlers once the lock is free, before any later batch's - /// submission, and leaves the rest to the scheduler's thread, which it wakes. Each such - /// batch then waits for that wake and that thread's pass. + /// submission, and leaves the rest to the scheduler's thread, which it wakes first. Each such + /// batch then waits for that wake and that thread's pass, not for those handlers. pub scheduler_passes_at_commit_ended_early: u64, /// Commits of `CommandBuffer::commit_and_submit` whose committing thread made no pass of its /// own, as another pass had taken its commit in first (`scheduler/mod.rs`, `LeftAtCommit`): diff --git a/crates/metal2vulkan/src/cache/mod.rs b/crates/metal2vulkan/src/cache/mod.rs index 712731956..2cf35a695 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 = 523; /// 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 = 2444; +pub const RUNTIME_VERSION: u32 = 2445; /// 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/FAST-PATHS.md b/docs/FAST-PATHS.md index f9f341dc8..df077ceee 100644 --- a/docs/FAST-PATHS.md +++ b/docs/FAST-PATHS.md @@ -227,8 +227,9 @@ recorded here. what it submits and its own command buffer's submission handlers; it hands the other command buffers' to the device's handler threads as it goes on, and where its own command buffer has submission handlers, it ends its pass right after that command buffer's last batch, leaving - what else is ready to the scheduler's thread, so no later command buffer's handlers wait in - their turn for the rest of the pass. + what else is ready to the scheduler's thread, which it wakes before it runs them, so no later + command buffer's handlers wait in their turn for the rest of the pass, and no left batch waits + for those handlers. - **A present that samples a guest plane copies the whole plane in, and Metal requires it.** Where a window's sink draws a guest's 3840x2160 plane into its swapchain texture, it does so in its own command buffer, and Metal shows a command buffer any CPU write made since its commit -- probed on