diff --git a/crates/metal2vulkan-runtime/src/tests/held_step_order.rs b/crates/metal2vulkan-runtime/src/tests/held_step_order.rs index b36f2d0f0..a8390aaef 100644 --- a/crates/metal2vulkan-runtime/src/tests/held_step_order.rs +++ b/crates/metal2vulkan-runtime/src/tests/held_step_order.rs @@ -30,8 +30,12 @@ use metal2vulkan::metal::{PixelFormat, TextureUsage}; use std::sync::{Arc, Mutex}; use std::time::{Duration, Instant}; -/// The bytes one fill of the long work writes. -const FILL_BYTES: u64 = 64 << 20; +/// The bytes one fill of the long work writes: few fills make it on a fast GPU, so the host's +/// share of the long command buffer (its fills' encoding, planning and submission, before its +/// pass's batch reaches its gate) stays small whatever the GPU's speed. At 64 MiB the RX 7900 XTX's +/// calibration took about 1400 fills, and on that loaded suite leg the long one read `Scheduled` +/// 12.6 and 17.1 ms after its commit, its follower 0.35 and 2.2 ms after its own. +const FILL_BYTES: u64 = 256 << 20; /// The long work's length the fills are calibrated to, in seconds. const LONG: f64 = 0.1; @@ -77,7 +81,7 @@ impl Run { /// The fills that take about [`LONG`] of `queue`'s GPU. fn long_fills(queue: &CommandQueue, target: &Buffer) -> usize { - let mut fills = 64; + let mut fills = 16; for _ in 0..3 { let command_buffer = queue.command_buffer().unwrap(); let mut blit = command_buffer.blit_command_encoder().unwrap(); @@ -88,7 +92,7 @@ fn long_fills(queue: &CommandQueue, target: &Buffer) -> usize { command_buffer.commit().unwrap(); command_buffer.wait_until_completed_for(WAIT).unwrap(); let gpu = command_buffer.gpu_end_time().unwrap() - command_buffer.gpu_start_time().unwrap(); - fills = ((fills as f64 * LONG / gpu.max(1e-6)) as usize).clamp(8, 4096); + fills = ((fills as f64 * LONG / gpu.max(1e-6)) as usize).clamp(2, 4096); } fills } @@ -126,10 +130,12 @@ fn long(work: &Workload, big: &Buffer, fills: usize, counted: bool) -> CommandBu command_buffer } -/// Commit `long`, then wait until its pass's batch waits at its gate. -fn commit_to_its_gate(work: &Workload, long: &CommandBuffer) { +/// Commit `long`, then wait until its pass's batch waits at its gate: the host time its commit +/// returned. +fn commit_to_its_gate(work: &Workload, long: &CommandBuffer) -> f64 { let before = work.device.work_counts(); long.commit().unwrap(); + let returned = crate::host_time(); let began = Instant::now(); while work.device.work_counts().heads_waiting_at_their_gate == before.heads_waiting_at_their_gate @@ -140,6 +146,7 @@ fn commit_to_its_gate(work: &Workload, long: &CommandBuffer) { ); std::thread::sleep(Duration::from_millis(1)); } + returned } /// Commit the long command buffer, wait until its pass's batch waits at its gate, then commit the @@ -228,8 +235,9 @@ fn an_unshared_queue_s_fill_does_not_wait_for_another_queue_s_host_step() { /// One run of [`command_buffers_behind_a_waiting_step_read_scheduled`]'s: host times, in seconds /// of `host_time`'s clock. struct Early { - /// The long command buffer's commit was called, and its kernel end (made `Scheduled`). - long: (f64, f64), + /// The long command buffer's commit was called, returned, and its kernel end (made + /// `Scheduled`). + long: (f64, f64, f64), /// The follower's commit was called, its submission handler ran, and its /// `wait_until_scheduled` returned. follower: (f64, f64, f64), @@ -243,15 +251,18 @@ struct Early { } impl Early { - /// Whether the follower was committed at least half the long work before it ended: else the - /// test's thread came late (a loaded host), and the run shows nothing. + /// Whether the follower was committed at least twice [`SCHEDULED`] before the long work + /// ended, by this run's own times: a command buffer that read `Scheduled` only once the long + /// work ended would then read it past the bound, so the run tells the two apart. Else the + /// test's thread came late (a loaded host), or the long work ran short of its calibration, + /// and the run shows nothing. fn early(&self) -> bool { - self.long_end - self.follower.0 > LONG / 2.0 + self.long_end - self.follower.0 > 2.0 * SCHEDULED } /// The time from the long one's commit to its `Scheduled`. fn long_scheduled(&self) -> f64 { - self.long.1 - self.long.0 + self.long.2 - self.long.0 } /// The time from the follower's commit to its `Scheduled`. @@ -271,7 +282,7 @@ fn early_run(work: &Workload, (big, small): (&Buffer, &Buffer), fills: usize) -> let long = long(work, big, fills, false); let before = work.device.work_counts(); let long_committed = crate::host_time(); - commit_to_its_gate(work, &long); + let long_returned = commit_to_its_gate(work, &long); let follower = work.queue.command_buffer().unwrap(); let mut blit = follower.blit_command_encoder().unwrap(); blit.fill_buffer(small, 0..256, 1).unwrap(); @@ -291,7 +302,7 @@ fn early_run(work: &Workload, (big, small): (&Buffer, &Buffer), fills: usize) -> let after = work.device.work_counts(); let handler = (*handled.lock().unwrap()).expect("the follower's submission handler ran"); Early { - long: (long_committed, long.kernel_end_time()), + long: (long_committed, long_returned, long.kernel_end_time()), follower: (committed, handler, scheduled), times: [ follower.kernel_start_time(), @@ -311,8 +322,9 @@ fn early_run(work: &Workload, (big, small): (&Buffer, &Buffer), fills: usize) -> /// behind a GPU hang so, `command_buffer/hang.rs`): Metal's are `Scheduled` once their /// dependencies are resolved. Before, each read so with its last batch, after the long work. The /// follower's times stay in its life's order: its commit, kernel start and end, GPU start and end -/// (`gpu_time.rs`). Of [`RUNS`] runs, the one whose later of the two read `Scheduled` soonest: on -/// lavapipe the first run's long one read it 25.5 ms after its commit, the later runs' 1.9 ms. +/// (`gpu_time.rs`). Of [`RUNS`] runs that each tell the two apart ([`Early::early`]), the one +/// whose later of the two read `Scheduled` soonest: on lavapipe the first run's long one read it +/// 25.5 ms after its commit, the later runs' 1.9 ms. #[test] fn command_buffers_behind_a_waiting_step_read_scheduled() { let scratch = Scratch::new("held-step-scheduled"); @@ -326,9 +338,11 @@ fn command_buffers_behind_a_waiting_step_read_scheduled() { .inspect(|run| { let since = |at: f64| (at - run.follower.0) * 1e3; println!( - "the long one Scheduled {:.2} ms after its commit; the follower committed at 0, \ - its handler at {:.2} ms, Scheduled at {:.2} ms, its kernel end {:.2} ms; the \ - long work's end {:.2} ms; {} made Scheduled behind the step", + "{fills} fills: the long one's commit returned {:.2} ms after its call, Scheduled \ + {:.2} ms after it; the follower committed at 0, its handler at {:.2} ms, \ + Scheduled at {:.2} ms, its kernel end {:.2} ms; the long work's end {:.2} ms; {} \ + made Scheduled behind the step", + (run.long.1 - run.long.0) * 1e3, run.long_scheduled() * 1e3, since(run.follower.1), since(run.follower.2), @@ -339,7 +353,7 @@ fn command_buffers_behind_a_waiting_step_read_scheduled() { }) .filter(Early::early) .min_by(|a, b| a.scheduled().total_cmp(&b.scheduled())) - .expect("a run committed the follower before half the long work ran"); + .expect("a run committed the follower twice the bound before the long work ended"); assert!( run.long_scheduled() < SCHEDULED, "the long one read Scheduled while its step waited" diff --git a/crates/metal2vulkan-validation/src/runtime_lane/command_queues.rs b/crates/metal2vulkan-validation/src/runtime_lane/command_queues.rs index 0ecd4cc94..48be81581 100644 --- a/crates/metal2vulkan-validation/src/runtime_lane/command_queues.rs +++ b/crates/metal2vulkan-validation/src/runtime_lane/command_queues.rs @@ -450,6 +450,14 @@ fn a_presenting_queue_s_lock_holds_no_other_queue_where_the_device_reserved_one( /// ([`a_queue_joining_another_s_busy_vk_queue_is_announced_once`]). pub(super) const QUEUE_SHARING: [Degradation; 1] = [Degradation::QueueSharing]; +/// The rounds [`a_queue_joining_another_s_busy_vk_queue_is_announced_once`] makes at least, each a +/// joiner of its own, so that more than one queue may join. +const JOINERS: usize = 3; + +/// The rounds [`a_queue_joining_another_s_busy_vk_queue_is_announced_once`] makes at most, where +/// the device shares and no queue joined yet. +const JOIN_ROUNDS: usize = 16; + /// A command queue that makes a command buffer while it holds no other uncompleted one, beside a /// queue whose short command buffers keep its `VkQueue` busy, joins that `VkQueue` where the device /// shares (`QueueSharingPolicy::Adaptive`, set here over the lane's `exact_answers()`, on a device @@ -457,6 +465,15 @@ pub(super) const QUEUE_SHARING: [Degradation; 1] = [Degradation::QueueSharing]; /// `queue-sharing` once however many queues join: each notice a join (`WorkCounts::queue_joins`, /// above 0 where the device shares), never one without. On a device that shares no `VkQueue` /// (lavapipe, RADV, MoltenVK) nothing joins and nothing is announced, and the test says why. +/// +/// A joiner joins only where it looks while the busy `VkQueue` still has a batch in flight whose +/// oldest has run under half a millisecond (`queue_share.rs`), and the busy queue's 24 fills are +/// about that much of an RTX 3090's work: on a loaded host the test's thread or the completions' +/// can miss that window (`WorkCounts::picks_with_none_busy`, +/// `WorkCounts::joins_withheld_from_old_heads`), or a fill sliced between other processes' work +/// can mark the busy queue long for good (`WorkCounts::queues_gone_long`). So each of +/// [`JOIN_ROUNDS`] rounds, at least [`JOINERS`] of them, takes a busy queue and a joiner of its own, +/// until a queue joined; the failure names what each look found. #[test] fn a_queue_joining_another_s_busy_vk_queue_is_announced_once() { use metal2vulkan_runtime::ResourceOptions; @@ -498,26 +515,37 @@ fn a_queue_joining_another_s_busy_vk_queue_is_announced_once() { command_buffer.commit().unwrap(); command_buffer }; - let busy = device.new_command_queue().unwrap(); - let joiners: Vec = (0..3) - .map(|_| device.new_command_queue().unwrap()) - .collect(); - for joiner in &joiners { + let mut rounds = 0; + while rounds < JOINERS + || (shares && rounds < JOIN_ROUNDS && device.work_counts().queue_joins == 0) + { + rounds += 1; + let (busy, joiner) = ( + device.new_command_queue().unwrap(), + device.new_command_queue().unwrap(), + ); let kept: Vec = (0..24).map(|_| fill(&busy)).collect(); // Submitted, so the busy `VkQueue` has work in flight as the joiner makes its own. kept.last().unwrap().wait_until_scheduled().unwrap(); - let joined = fill(joiner); + let joined = fill(&joiner); joined.wait_until_completed_for(WAIT).unwrap(); for command_buffer in kept { command_buffer.wait_until_completed_for(WAIT).unwrap(); } } - let joins = device.work_counts().queue_joins; - assert_eq!( - joins > 0, - shares, - "{joins} joins where the device shares: {shares}" + let counts = device.work_counts(); + let joins = counts.queue_joins; + let found = format!( + "{joins} joins in {rounds} rounds where the device shares: {shares}; looks that found \ + no other VkQueue busy {}, that passed over a busy one's old oldest batch {}, or an \ + unshared queue's {}; queues gone long {}", + counts.picks_with_none_busy, + counts.joins_withheld_from_old_heads, + counts.joins_withheld_from_unshared, + counts.queues_gone_long ); + println!("{}: {found}", device.name()); + assert_eq!(joins > 0, shares, "{found}"); assert_eq!( sharing_kinds(), usize::from(joins > 0),