From c62128b56410d840c6830f7011bcaf6a3e21ebc4 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Mon, 16 Nov 2026 16:33:21 -0800 Subject: [PATCH] RUNTIME 2493: Queue turns: a run several batches were seen complete in shares in proportion to their expected runs What: unshared_hold's flight split a completion's run equally among the other queues' batches seen complete in it. It now shares the run in proportion to each batch's expected run at its submission (unshared_hold::Shares): a light batch seen complete with a long one keeps a light run, the long one its own. Batches of queues with no run seen take the run beyond the others' expected runs, an equal part each, at least 1 ns; when every batch is of unknown length they share it equally, as before. Equal expected runs split as before, so the unshared hold's tests keep their numbers. Why: a REGRESSION of RUNTIME 2490's Fair default. Under QueueTurnPolicy::Fair a compositor's plain queue takes such a share: its light draw, seen complete together with the application's 30 ms command buffer it waited behind, got half the run, about 15 ms, as its queue's expected run. With 2 ms of its own work allowed in flight, one draw in flight then held the next until that draw completed, behind the application's next batch: one draw per application batch. On RADV PHOENIX (m3-max-38213, frames, a draw every 16 ms beside three 30 ms command buffers in flight) the compositor fell 2 s behind (p50 1192 ms, max 2062 ms, against CommitOrder's 77.8 and 106.3 ms). 2 ms stays the budget: 4 ms was worse for the compositor in both shapes measured. FAST-PATHS: the queue turns row says how a grouped run is shared, and that the Macs disagree on bursts (cross_queue_burst_turns: the M3 Max ran a second queue's small command buffer ahead of 56 to 62 of a first queue's 64; the UHD 630 after 60 to 63 of them; m3-max-38188, m3-max-38189). Tests: queue_turns::a_light_batch_seen_complete_with_another_queue_s_long_one_keeps_its_light_run fails under the equal split (the compositor's next draw is held); a_batch_of_unknown_length_takes_the_run_beyond_the_others_expected_ones. Verified (msi, at 7f6f9c4c6b): m3-max-38234 runtime clippy --all-targets --all-features 0, unshared_hold|queue_turns 24/24, fast_paths 36/36, rustdoc 0; m3-max-38235 clippy --cfg m2v_gpu 0. Lavapipe clippy and the queue_share_device / scheduler_held_wakes device tests (m3-max-38236) and the PHOENIX rerun of both shapes run after the merge. --- .../src/tests/queue_turns.rs | 57 +++++++++++++++++ .../metal2vulkan-runtime/src/unshared_hold.rs | 64 ++++++++++++++++--- crates/metal2vulkan/src/cache/mod.rs | 2 +- docs/FAST-PATHS.md | 41 ++++++------ 4 files changed, 136 insertions(+), 28 deletions(-) diff --git a/crates/metal2vulkan-runtime/src/tests/queue_turns.rs b/crates/metal2vulkan-runtime/src/tests/queue_turns.rs index ab346ac91..f08c42055 100644 --- a/crates/metal2vulkan-runtime/src/tests/queue_turns.rs +++ b/crates/metal2vulkan-runtime/src/tests/queue_turns.rs @@ -12,6 +12,9 @@ //! still in flight at once, where the unshared bound prices each at its submission's run. //! - **Unknown length.** A queue with no run seen keeps one batch in flight until it completes, //! and holds no other queue back. +//! - **Shares of a run.** A light batch seen complete together with another queue's long one keeps +//! a light run (`unshared_hold::Shares`), so a compositor's plain queue is never held behind its +//! own draw stuck behind an application's batch. //! - **The oldest's run.** What the oldest batch in flight ran counts against its own queue's work //! alone, and only while no completion past it was published; the hold waits for the oldest //! batch's completion, and, where that is its own queue's, for the time its run brings the work @@ -251,3 +254,57 @@ fn a_published_completion_counts_its_batches_as_left() { assert_eq!(turn(&flight, (A, &app), 0, T0), Room::Go); assert_eq!(flight.len(), 1); } + +/// A compositor's light draw seen complete together with the application's 30 ms batch it waited +/// behind keeps its 0.1 ms, and the application its 30 ms: the compositor's next draw goes with +/// one in flight. An equal share, 15 ms each, held each draw until the one before it completed, +/// behind the application's next batch: a draw for each of the application's (RADV PHOENIX: a +/// draw committed every 16 ms fell 2 s behind). +#[test] +fn a_light_batch_seen_complete_with_another_queue_s_long_one_keeps_its_light_run() { + let (app, compositor) = (queue(30 * MS), queue(MS / 10)); + let mut flight = Flight::default(); + flight.taken(1, Flies::Other(A, &app), T0); + flight.taken(2, Flies::Other(B, &compositor), T0); + flight.completed(2, 30 * MS + MS / 10); + assert_eq!(app.nanos(), Some(30 * MS)); + assert_eq!(compositor.nanos(), Some(MS / 10)); + flight.taken(3, Flies::Other(A, &app), T0); + flight.taken(4, Flies::Other(B, &compositor), T0); + assert_eq!( + turn(&flight, (B, &compositor), 0, T0), + Room::Go, + "0.1 ms of its own in flight" + ); +} + +/// A batch of a queue with no run seen, seen complete with another queue's, takes the run beyond +/// the other's expected one, and 1 ns where the run is shorter than that; where every one is of +/// unknown length, they share the run equally. +#[test] +fn a_batch_of_unknown_length_takes_the_run_beyond_the_others_expected_ones() { + let (app, new, newer) = ( + queue(30 * MS), + Arc::new(Expected::default()), + Arc::new(Expected::default()), + ); + let mut flight = Flight::default(); + flight.taken(1, Flies::Other(A, &app), T0); + flight.taken(2, Flies::Other(B, &new), T0); + flight.completed(2, 30 * MS + MS / 2); + assert_eq!(app.nanos(), Some(30 * MS)); + assert_eq!(new.nanos(), Some(MS / 2)); + flight.taken(3, Flies::Other(A, &app), T0); + flight.taken(4, Flies::Other(B, &newer), T0); + flight.completed(4, 20 * MS); + assert_eq!(app.nanos(), Some(25 * MS), "halved toward the whole run"); + assert_eq!(newer.nanos(), Some(1), "none of it left"); + let (first, second) = (Arc::new(Expected::default()), Arc::new(Expected::default())); + flight.taken(5, Flies::Other(A, &first), T0); + flight.taken(6, Flies::Other(B, &second), T0); + flight.completed(6, 8 * MS); + assert_eq!( + (first.nanos(), second.nanos()), + (Some(4 * MS), Some(4 * MS)) + ); +} diff --git a/crates/metal2vulkan-runtime/src/unshared_hold.rs b/crates/metal2vulkan-runtime/src/unshared_hold.rs index 3339d4324..2f30b9b26 100644 --- a/crates/metal2vulkan-runtime/src/unshared_hold.rs +++ b/crates/metal2vulkan-runtime/src/unshared_hold.rs @@ -41,8 +41,9 @@ //! recent batches ran as their `VkQueue`'s oldest in flight (`queue_wait.rs`'s runs), raised at //! once by a longer one and halved toward a shorter one, so a queue whose batches got shorter //! takes its new length within a few batches, while one long batch counts at once. A run several -//! batches complete in shares among the other queues' batches among them; an unshared queue's -//! takes no share. +//! batches were seen complete in shares among the other queues' batches among them in +//! proportion to their expected runs (`Shares`), so a light batch seen complete with a long one +//! keeps a light run; an unshared queue's takes no share. //! - **Unknown length.** A batch of a queue with no run seen yet may be of any length: while one //! is in flight, no other batch of the other queues goes there until it is seen complete, which //! gives its queue a run. So an application that commits several command buffers at once does @@ -224,6 +225,53 @@ struct InFlight { of: Option>, } +/// How a run several batches were seen complete in shares among the other queues' batches among +/// them ([`Flight::completed`]): in proportion to each one's expected run at its submission, so a +/// light batch seen complete with a long one of another queue (a compositor's draw behind an +/// application's command buffer, the two seen together) keeps a light run and the long one its +/// own. The batches of queues with no run seen take the run beyond the others' expected runs, an +/// equal part each (all of it, equally, where every one is of unknown length), the others then +/// sharing the rest. An unshared queue's batch takes no share. +#[derive(Clone, Copy, Debug)] +struct Shares { + /// The run. + run: u64, + /// The expected runs at their submission of the batches of known length, summed. + known: u64, + /// How many are of unknown length. + unseen: u64, +} + +impl Shares { + /// The shares of `run` among `batches`, which were seen complete together. + fn among<'a>(batches: impl Iterator, run: u64) -> Self { + let (known, unseen) = + (batches.filter(|batch| batch.of.is_some())).fold((0, 0), |(known, unseen), batch| { + match batch.unseen { + true => (known, unseen + 1), + false => (known + batch.expected, unseen), + } + }); + Self { run, known, unseen } + } + + /// `batch`'s share (the type's docs). + fn of(self, batch: &InFlight) -> u64 { + // What the batches of known length take: the whole run where none is of unknown length. + let known_run = match self.unseen { + 0 => self.run, + _ => self.run.min(self.known), + }; + match batch.unseen { + true => (self.run - known_run) / self.unseen.max(1), + false => { + let share = u128::from(known_run) * u128::from(batch.expected); + u64::try_from(share / u128::from(self.known.max(1))).unwrap_or(u64::MAX) + } + } + } +} + /// The batches taken on one `VkQueue` an unshared queue holds, or whose queues take turns /// (`queue_turns.rs`), and not seen complete (`queue_wait::Ahead::flight`), under that `VkQueue`'s /// waits' lock. @@ -295,22 +343,20 @@ impl Flight { /// The host saw the timeline reach `value`, `run` nanoseconds after the oldest batch in flight /// became so: each batch at or below `value` leaves, and each other queue's among them updates - /// its queue's expected run with an equal share of `run`. An unshared queue's batch takes no - /// share: a compositor's light batch is seen complete with the application's batch ahead of - /// it, and a share of that batch's run would halve the application's expected one. + /// its queue's expected run with its share of `run` ([`Shares`]). An unshared queue's batch + /// takes no share: a compositor's light batch is seen complete with the application's batch + /// ahead of it, and a share of that batch's run would cut the application's expected one. pub(crate) fn completed(&mut self, value: u64, run: u64) { let done = self .batches .iter() .take_while(|batch| batch.value <= value) .count(); - let others = (self.batches.iter().take(done)) - .filter(|batch| batch.of.is_some()) - .count(); - let share = run / others.max(1) as u64; + let shares = Shares::among(self.batches.iter().take(done), run); for batch in self.batches.drain(..done) { self.expected -= batch.expected; self.unseen -= usize::from(batch.unseen); + let share = shares.of(&batch); if let Some(of) = batch.of { of.observed(share); if let Some(count) = self.turns.get_mut(&batch.queue) { diff --git a/crates/metal2vulkan/src/cache/mod.rs b/crates/metal2vulkan/src/cache/mod.rs index b28387753..321bf572f 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 = 525; /// 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 = 2492; +pub const RUNTIME_VERSION: u32 = 2493; /// 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 2d9476776..df2b08ac1 100644 --- a/docs/FAST-PATHS.md +++ b/docs/FAST-PATHS.md @@ -205,27 +205,32 @@ known. The rest of the differences are docs/API.md section 9's precision notes. later commit of another queue that shares nothing with it goes ahead of it (one whose footprint runs in commit order with it still waits behind it). A batch's expected work is its queue's expected run now (`unshared_hold::Expected`: the host's view of how long its recent batches ran, - raised at once by a longer one and halved toward a shorter one), so a run longer than the - queue's recent ones prices every batch of it in flight at once, where the unshared bound above - prices each at the run its queue had at its submission; while a batch of a queue with no run - seen yet is in flight there, that queue's next batch waits for its completion, and no other - queue's does. So a command buffer another queue commits after one queue's burst (a long command - buffer cut into parts, a backlog of frames) goes in behind about 16 ms of it and one batch, not - the whole burst, and from its submission on, while that queue keeps submitting there, behind - about 2 ms and one batch of each busy queue. An unshared queue's batches are never held for - their turn, and count as another queue's. The next batch goes in before the one ahead ends (the - scheduler's thread woken at the oldest batch's completion and at the time its run brings the - work left under the budget), so the GPU idles for the hold only where that thread answers later - than the budget less one batch: a queue alone keeps about 16 ms queued, two queues submitting - keep 2 ms each. A committing or waiting thread's pass leaves a held batch to the scheduler's - thread (`WorkCounts::scheduler_at_commit_left_turns`). Counted: + raised at once by a longer one and halved toward a shorter one; a run several batches were seen + complete in is shared in proportion to their expected runs, so a compositor's light draw seen + complete with the application's long command buffer it waited behind keeps a light run), so a + run longer than the queue's recent ones prices every batch of it in flight at once, where the + unshared bound above prices each at the run its queue had at its submission; while a batch of + a queue with no run seen yet is in flight there, that queue's next batch waits for its + completion, and no other queue's does. So a command buffer another queue commits after one + queue's burst (a long command buffer cut into parts, a backlog of frames) goes in behind about + 16 ms of it and one batch, not the whole burst, and from its submission on, while that queue + keeps submitting there, behind about 2 ms and one batch of each busy queue. An unshared queue's + batches are never held for their turn, and count as another queue's. The next batch goes in + before the one ahead ends (the scheduler's thread woken at the oldest batch's completion and at + the time its run brings the work left under the budget), so the GPU idles for the hold only + where that thread answers later than the budget less one batch: a queue alone keeps about 16 ms + queued, two queues submitting keep 2 ms each. A committing or waiting thread's pass leaves a + held batch to the scheduler's thread (`WorkCounts::scheduler_at_commit_left_turns`). Counted: `WorkCounts::heads_held_for_queue_turns` and `heads_let_go_turn_idle` (heads 2 ms would have held). `queue_turns = QueueTurnPolicy::CommitOrder` submits every ready batch at once, in commit order: a later commit of another queue waits behind every batch submitted before it there. Not - an accuracy choice: it trades a queue's work submitted ahead for the other queues' waits; the - value nearer Metal's, whose second queue's small command buffer ran beside a first queue's - 200 ms one on the consumer's oracles (a UHD 630 and a Radeon Pro 5300M, macOS 26.7.1), and - `exact_answers()` keeps it. The runtime's choice: no pipeline differs. What stays is a batch + an accuracy choice: it trades a queue's work submitted ahead for the other queues' waits. A + second queue's small command buffer ran beside a first queue's 200 ms one on the consumer's + oracles (a UHD 630 and a Radeon Pro 5300M, macOS 26.7.1); behind a first queue's burst of 64 + command buffers of about 4 ms each, the Macs disagree: an M3 Max (macOS 27.0.1) ran it ahead of + 56 to 62 of them, the UHD 630 after 60 to 63 of them had ended + (`metal2vulkan-conformance`'s `probes/cross_queue_burst_turns.m`). `exact_answers()` keeps + `Fair`. The runtime's choice: no pipeline differs. What stays is a batch already submitted: it runs to its end (known gaps, below). On lavapipe, both values (`queue_share_device::a_later_queue_s_command_buffer_takes_its_turn_ahead_of_another_queue_s_burst`: twelve command buffers of one queue sized for 10 ms each and committed at once, a small one of