diff --git a/crates/metal2vulkan-validation/src/runtime_lane/busy_premise.rs b/crates/metal2vulkan-validation/src/runtime_lane/busy_premise.rs index a3e776b71..722a153b3 100644 --- a/crates/metal2vulkan-validation/src/runtime_lane/busy_premise.rs +++ b/crates/metal2vulkan-validation/src/runtime_lane/busy_premise.rs @@ -1,14 +1,17 @@ //! A step that needs the GPU busy past its calls, run again where the busy work ended first (lane //! tooling). //! -//! A step of `device_copies`' `did_modify_range` oracle commits busy work (`first_use`'s -//! `Busy::commit`, about 2 s of GPU time at its measured rate) and then makes its calls. Those -//! return in milliseconds: 5 to 47 ms from the busy work's first commit to the last call's return -//! on an RX 7900 XTX (RADV), alone and in a lane leg. The step's premise is that the GPU has not +//! A step of `device_copies`' `did_modify_range` oracle, and each storage mode of `first_use`'s +//! host copies of a new texture, commits busy work (`first_use`'s `Busy::commit`, about 2 s of GPU +//! time at its measured rate) and then makes its calls. Those return in milliseconds: 5 to 47 ms +//! from the busy work's first commit to the last call's return on an RX 7900 XTX (RADV), alone and +//! in a lane leg; the texture copies 20 to 670 us each on a Radeon 780M. The step's premise is that the GPU has not //! run its command buffer when its calls return: the end mark the busy work writes last still //! reads 0. A host that keeps the test off the CPU past the busy window breaks the premise with //! nothing wrong in the runtime. One step on the XTX missed it so, in a lane leg whose node's 16 -//! CPUs also ran an RTX 2080 suite, lavapipe and a replay. The window cannot be stretched to fit +//! CPUs also ran an RTX 2080 suite, lavapipe and a replay; and one texture copy on the Radeon 780M, +//! in a leg at its memory limit beside a virtual machine's boot, whose calls took 140 and 667 us +//! and whose end mark turned within a millisecond of reading 0. The window cannot be stretched to fit //! any host: its length rests on a rate measured once, and Vulkan does not promise that a running //! shader sees the host's release. //! diff --git a/crates/metal2vulkan-validation/src/runtime_lane/first_use.rs b/crates/metal2vulkan-validation/src/runtime_lane/first_use.rs index 9bb1ce0ed..a45adc12c 100644 --- a/crates/metal2vulkan-validation/src/runtime_lane/first_use.rs +++ b/crates/metal2vulkan-validation/src/runtime_lane/first_use.rs @@ -83,6 +83,7 @@ //! zero through a pass that loads them. use super::astc_textures::decodes_astc; +use super::busy_premise::until_premise_held; use super::commit_work::relay_marks; use super::degrade_notices::{ assert_once_allowing, forced, made_compute_kinds, shared_in_system_memory, @@ -1743,76 +1744,90 @@ fn shared_textures_copy_on_the_host_while_the_gpu_is_busy() { let region = Region::new_2d(0, 0, SIDE, SIDE); for options in [ResourceOptions::shared(), managed] { let mode = options.storage_mode(); - let before = device.work_counts(); - let texture = device.new_texture(&descriptor(options)).unwrap(); - let made = device.work_counts(); - let hosted = made.images_started_on_the_host > before.images_started_on_the_host; - let staged = made.staging_moves_queued > before.staging_moves_queued; - println!( - "HOST-COPY {mode:?} texture on {name}: {}", - match (hosted, staged) { - (true, _) => "moved and zeroed on the host", - (false, true) => "its move and zeros queued for the staging queue", - (false, false) => "copied through submissions", - } - ); - let (busy_work, mark) = busy.commit(device, &queue); - busy_work.scheduled(); - assert_eq!(read_mark(&mark), 0, "the busy work ended before the calls"); - let mut read = vec![0xee; BYTES]; - let at = Instant::now(); - texture.get_bytes(&mut read, ROW, 0, region, 0, 0).unwrap(); - let (read_took, read_mark_then) = (at.elapsed(), read_mark(&mark)); - assert!( - read.iter().all(|&b| b == 0), - "{mode:?}: a new texture reads zero" - ); let written = pattern(6); - let at = Instant::now(); - texture - .replace_region(region, 0, 0, &written, ROW, 0) - .unwrap(); - let (write_took, write_mark_then) = (at.elapsed(), read_mark(&mark)); - texture.get_bytes(&mut read, ROW, 0, region, 0, 0).unwrap(); - assert_eq!(read, written, "{mode:?}: the bytes written read back"); - let copied = device.work_counts(); - println!( - "HOST-COPY {mode:?} on {name}: get_bytes {read_took:?}, replace_region \ - {write_took:?} while the GPU was busy" - ); - if hosted { - assert_eq!( - (read_mark_then, write_mark_then), - (0, 0), - "{mode:?} on {name}: a host copy waited for the busy work (get_bytes \ - {read_took:?}, replace_region {write_took:?})" + let mut read = vec![0xee; BYTES]; + let mut kept = None; + // A try whose calls the busy work did not outlast answers nothing of the premise + // (`busy_premise.rs`): its bytes and counts are checked, and it is run again with a + // new texture and new busy work. A copy that waits for the busy work misses every try. + until_premise_held(&format!("{mode:?} on {name}"), |_| { + let before = device.work_counts(); + let texture = device.new_texture(&descriptor(options)).unwrap(); + let made = device.work_counts(); + let hosted = made.images_started_on_the_host > before.images_started_on_the_host; + let staged = made.staging_moves_queued > before.staging_moves_queued; + println!( + "HOST-COPY {mode:?} texture on {name}: {}", + match (hosted, staged) { + (true, _) => "moved and zeroed on the host", + (false, true) => "its move and zeros queued for the staging queue", + (false, false) => "copied through submissions", + } ); - assert_eq!( - copied.internal_submissions, before.internal_submissions, - "{mode:?} on {name}: a host copy made a submission" + let begun = Instant::now(); + let (busy_work, mark) = busy.commit(device, &queue); + busy_work.scheduled(); + let (scheduled_took, mark_before) = (begun.elapsed(), read_mark(&mark)); + read.fill(0xee); + let at = Instant::now(); + texture.get_bytes(&mut read, ROW, 0, region, 0, 0).unwrap(); + let (read_took, read_mark_then) = (at.elapsed(), read_mark(&mark)); + assert!( + read.iter().all(|&b| b == 0), + "{mode:?}: a new texture reads zero" ); - assert_eq!(copied.host_texel_copies - before.host_texel_copies, 3); - } - // On the staging queue each read waits for its own submission alone: the busy work's - // batch carried the new texture's queued move there before its own submission, and - // the second read carries the write queued before it. - if staged { - assert_eq!( - (read_mark_then, write_mark_then), - (0, 0), - "{mode:?} on {name}: a staging copy waited for the busy work (get_bytes \ - {read_took:?}, replace_region {write_took:?})" + let at = Instant::now(); + texture + .replace_region(region, 0, 0, &written, ROW, 0) + .unwrap(); + let (write_took, write_mark_then) = (at.elapsed(), read_mark(&mark)); + let calls = begun.elapsed(); + texture.get_bytes(&mut read, ROW, 0, region, 0, 0).unwrap(); + assert_eq!(read, written, "{mode:?}: the bytes written read back"); + let copied = device.work_counts(); + println!( + "HOST-COPY {mode:?} on {name}: get_bytes {read_took:?}, replace_region \ + {write_took:?} while the GPU was busy" ); - assert_eq!( - copied.internal_submissions, before.internal_submissions, - "{mode:?} on {name}: a staging copy made an internal submission" - ); - assert_eq!(copied.staging_submissions - made.staging_submissions, 3); - assert_eq!(copied.staging_copies_queued - made.staging_copies_queued, 1); - assert_eq!(copied.staging_flushes - made.staging_flushes, 2); - } - release(&mark); - busy_work.wait_until_completed_for(WAIT).unwrap(); + if hosted { + assert_eq!( + copied.internal_submissions, before.internal_submissions, + "{mode:?} on {name}: a host copy made a submission" + ); + assert_eq!(copied.host_texel_copies - before.host_texel_copies, 3); + } + // On the staging queue each read waits for its own submission alone: the busy + // work's batch carried the new texture's queued move there before its own + // submission, and the second read carries the write queued before it. + if staged { + assert_eq!( + copied.internal_submissions, before.internal_submissions, + "{mode:?} on {name}: a staging copy made an internal submission" + ); + assert_eq!(copied.staging_submissions - made.staging_submissions, 3); + assert_eq!(copied.staging_copies_queued - made.staging_copies_queued, 1); + assert_eq!(copied.staging_flushes - made.staging_flushes, 2); + } + // The premise: the busy work still runs as the calls that copy on the host or the + // staging queue return, and before any call. Copies through submissions wait for + // it by design. + let ended_first = mark_before != 0 + || ((hosted || staged) && (read_mark_then, write_mark_then) != (0, 0)); + release(&mark); + busy_work.wait_until_completed_for(WAIT).unwrap(); + kept = Some(texture); + ended_first.then(|| { + format!( + "the busy work ended first, on the GPU {}; from its first commit: \ + scheduled {scheduled_took:?}, the calls returned {calls:?} (get_bytes \ + {read_took:?}, replace_region {write_took:?}); end mark before the \ + calls {mark_before}, after get_bytes {read_mark_then}, after \ + replace_region {write_mark_then}", + busy_work.gpu_span() + ) + }) + }); + let texture = kept.expect("a try's texture"); // A command buffer committed after the call reads the bytes it wrote. let command_buffer = queue.command_buffer().unwrap(); let out = copy_out(&command_buffer, &texture); diff --git a/crates/metal2vulkan-validation/src/runtime_lane/grouped_submissions.rs b/crates/metal2vulkan-validation/src/runtime_lane/grouped_submissions.rs index c8f68308e..bf1713cb9 100644 --- a/crates/metal2vulkan-validation/src/runtime_lane/grouped_submissions.rs +++ b/crates/metal2vulkan-validation/src/runtime_lane/grouped_submissions.rs @@ -58,6 +58,7 @@ //! handler's start on the host's clock, over [`DELAY_ROUNDS`] rounds after [`DELAY_WARM`]. use super::degrade_notices::counts; +use super::first_use::gpu_took; use super::{ at_plan, layer_reported_nothing_at_all, loader, open_device, validated_device_with, Messages, ScratchCache, WAIT, @@ -808,17 +809,32 @@ const RUNNING_FILL: u64 = 128 << 20; /// The command buffers committed behind `A` in [`behind_running_work`]. const BEHIND: usize = 7; -/// The most runs [`behind_while_a_runs`] makes, `A` twice as long each time. +/// The most runs [`behind_while_a_runs`] makes, `A` twice as long each time, up to [`A_MOST`]. const BEHIND_RUNS: u32 = 4; -/// The fills of [`RUNNING_FILL`] bytes that keep the device's GPU busy for at least `least`: a -/// command buffer of fills doubled until one takes `least` from its commit to its completion -/// (the GPU's time then outweighs the submission's), at most 1 << 14 of them, then thrice that. +/// The longest one command buffer of fills of these runs (`A`, and `B` where it is long) may run +/// on the GPU, at the rate its calibration measured ([`RunningFills::stretched`]): half a kernel +/// driver's job timeout. A command buffer goes to the kernel as one job, and the kernel ends a job +/// that runs past its timeout: amdgpu's is 2 s by default (Linux 7.3.3, `modinfo amdgpu`: +/// `lockup_timeout`, "default: 2000"), as Windows' is (`TdrDelay`). On a Radeon 780M (RADV, Mesa +/// 26.2.6) a lane leg's run of [`a_held_group_goes_before_another_queue_s_submission_handler_runs`] +/// doubled `A` to 8 times its calibrated fills, about 3.2 s at the 2.09 ms a fill measured there +/// (192 fills took 0.39 to 0.45 s, 384 took 0.80 s). The kernel logged a gfx ring timeout and +/// reset at about that run's time, naming a process of the leg's, and `B`, sent behind `A`, read +/// `Completed` with no error and its fill not run. +const A_MOST: std::time::Duration = std::time::Duration::from_secs(1); + +/// The fills of [`RUNNING_FILL`] bytes that keep the device's GPU busy for at least `least`, and +/// one fill's GPU time: a command buffer of fills doubled until one takes `least` on the GPU +/// ([`gpu_took`]: its GPU times, else its commit to its completion), at most 1 << 14 of them, +/// then thrice that. The GPU's time, not the wall's: the first command buffer also carries the +/// device's first work and the host's latency, and by the wall's measure it stopped at 4 fills +/// (28 ms of GPU work) in 2 of 10 quiet runs on a Radeon 780M, where the others took 192. fn running_fills( queue: &metal2vulkan_runtime::CommandQueue, large: &Buffer, least: std::time::Duration, -) -> usize { +) -> RunningFills { let mut fills = 4; loop { let command_buffer = queue.command_buffer().unwrap(); @@ -832,13 +848,83 @@ fn running_fills( command_buffer.commit().unwrap(); command_buffer.wait_until_completed_for(WAIT).unwrap(); assert_eq!(command_buffer.error(), None, "{fills} fills"); - if start.elapsed() >= least || fills >= 1 << 14 { - return fills * 3; + let took = gpu_took(&command_buffer, start.elapsed()); + if took >= least || fills >= 1 << 14 { + return RunningFills { + fills: fills * 3, + each: took / fills as u32, + }; } fills *= 2; } } +/// What [`running_fills`] measured. +#[derive(Clone, Copy, Debug)] +struct RunningFills { + /// The fills that run its `least` thrice over. + fills: usize, + /// One fill's GPU time. + each: std::time::Duration, +} + +impl RunningFills { + /// `stretch` times the fills, at most as many as run [`A_MOST`] at the measured rate, and at + /// least one. + fn stretched(self, stretch: usize) -> usize { + let most = A_MOST.as_nanos() / self.each.as_nanos().max(1); + (self.fills * stretch).min(most as usize).max(1) + } + + /// The GPU time `fills` take at the measured rate. + fn take(self, fills: usize) -> std::time::Duration { + self.each * fills as u32 + } +} + +/// Write `A`'s fills and GPU time to standard error (`GROUPED-A`), once it completed: `fills` +/// at `stretch`, `rate` the calibration's. +fn a_took(stretch: usize, rate: RunningFills, fills: usize, running: &CommandBuffer) { + let took = match (running.gpu_start_time(), running.gpu_end_time()) { + (Ok(start), Ok(end)) => format!("{:.1} ms", (end - start) * 1e3), + _ => "?".to_owned(), + }; + eprintln!( + "GROUPED-A stretch {stretch}: {fills} fills ({:?} at the calibration's rate, at most \ + {A_MOST:?}), {took} on the GPU", + rate.take(fills) + ); +} + +/// Every stretch the runs reach keeps one command buffer of fills within [`A_MOST`] at the +/// measured rate, and a stretch the bound leaves room for keeps every fill: at the Radeon 780M's +/// 2.09 ms a fill and 192 fills (0.4 s), 8 times as many would run about 3.2 s, past amdgpu's 2 s +/// job timeout. +#[test] +fn a_stretched_command_buffer_stays_within_its_job_bound() { + let leg = RunningFills { + fills: 192, + each: std::time::Duration::from_micros(2090), + }; + assert_eq!(leg.stretched(1), 192); + assert_eq!(leg.stretched(2), 384); + for stretch in (0..BEHIND_RUNS).map(|run| 1 << run) { + let fills = leg.stretched(stretch); + assert!( + leg.take(fills) <= A_MOST, + "stretch {stretch}: {fills} fills run {:?}", + leg.take(fills) + ); + } + assert!(leg.take(192 * 8) > 2 * A_MOST, "the leg's eighth stretch"); + // A calibration whose own fills pass the bound is cut to it. + let slow = RunningFills { + fills: 300, + each: std::time::Duration::from_millis(10), + }; + assert_eq!(slow.stretched(1), 100); +} + /// What each command buffer behind `A` carries in [`behind_running_work`]. #[derive(Clone, Copy, Debug, PartialEq, Eq)] enum Carried { @@ -901,8 +987,10 @@ fn behind_running_work(completion: CompletionPolicy, stretch: usize, carried: Ca let large = device .new_buffer(RUNNING_FILL, ResourceOptions::private()) .unwrap(); - // Its first fill alone (first work goes at once), then enough to run 300 ms or more. - let fills = running_fills(&queue, &large, std::time::Duration::from_millis(100)) * stretch; + // Its first fill alone (first work goes at once), then enough to run 300 ms or more, at + // most `A_MOST`. + let rate = running_fills(&queue, &large, std::time::Duration::from_millis(100)); + let fills = rate.stretched(stretch); let buffers: Vec = (0..BEHIND) .map(|_| device.new_buffer(BYTES, ResourceOptions::shared()).unwrap()) .collect(); @@ -1003,6 +1091,7 @@ fn behind_running_work(completion: CompletionPolicy, stretch: usize, carried: Ca }); running.wait_until_completed_for(WAIT).unwrap(); assert_eq!(running.error(), None, "A"); + a_took(stretch, rate, fills, &running); for (at, command_buffer) in committed.iter().enumerate() { command_buffer.wait_until_completed_for(WAIT).unwrap(); assert_eq!(command_buffer.error(), None, "B{}", at + 1); @@ -1038,7 +1127,8 @@ fn behind_running_work(completion: CompletionPolicy, stretch: usize, carried: Ca /// [`behind_running_work`] under the default until, on a driver that merges, `A` outlasted every /// command buffer behind it and the wait that sends the last group: a loaded CPU device (lavapipe /// beside other runs) may run `A` sooner than its calibration measured. `A` is twice as long at -/// each run, at most [`BEHIND_RUNS`] runs; the last run is returned whatever it saw. On a driver +/// each run, up to [`A_MOST`] of GPU time, at most [`BEHIND_RUNS`] runs; the last run is returned +/// whatever it saw. On a driver /// that does not merge nothing is held, and one run serves. fn behind_while_a_runs(carried: Carried) -> Behind { let mut stretch = 1; @@ -1259,7 +1349,8 @@ fn behind_another_queue_s_handler(stretch: usize) -> HandlerWait { let warm = fill(&second, 2); warm.commit().unwrap(); warm.wait_until_completed_for(WAIT).unwrap(); - let fills = running_fills(&first, &large, std::time::Duration::from_millis(100)) * stretch; + let rate = running_fills(&first, &large, std::time::Duration::from_millis(100)); + let fills = rate.stretched(stretch); let running = first.command_buffer().unwrap(); let mut blit = running.blit_command_encoder().unwrap(); for at in 0..fills { @@ -1323,6 +1414,7 @@ fn behind_another_queue_s_handler(stretch: usize) -> HandlerWait { command_buffer.wait_until_completed_for(WAIT).unwrap(); assert_eq!(command_buffer.error(), None, "{name}"); } + a_took(stretch, rate, fills, &running); for (at, buffer) in buffers.iter().enumerate() { let wrong = (bytes_of(buffer).iter()) .filter(|&&byte| byte != at as u8 + 1) @@ -1483,7 +1575,8 @@ fn behind_a_kept_signal(stretch: usize) -> KeptSignalWait { blit.end_encoding().unwrap(); warm.commit().unwrap(); warm.wait_until_completed_for(WAIT).unwrap(); - let fills = running_fills(&first, &large, std::time::Duration::from_millis(100)) * stretch; + let rate = running_fills(&first, &large, std::time::Duration::from_millis(100)); + let fills = rate.stretched(stretch); let long = || { let command_buffer = first.command_buffer().unwrap(); let mut blit = command_buffer.blit_command_encoder().unwrap(); @@ -1559,6 +1652,7 @@ fn behind_a_kept_signal(stretch: usize) -> KeptSignalWait { "{name}" ); } + a_took(stretch, rate, fills, &running); for (at, buffer) in buffers.iter().enumerate() { let wrong = (bytes_of(buffer).iter()) .filter(|&&byte| byte != at as u8 + 1) diff --git a/docs/VALIDATION.md b/docs/VALIDATION.md index f8a2d4fd7..518fa19d1 100644 --- a/docs/VALIDATION.md +++ b/docs/VALIDATION.md @@ -101,8 +101,9 @@ with 16 CPU threads to itself, and 0.1 to 0.3 s on an RX 7900 XTX and a Radeon 7 few-channels` runs three device tests at a time (`.config/nextest.toml` has the measurement), leaving room for other programs' devices on the same GPU. - **A busy window the host outlasted is asked again.** The `did_modify_range` oracle's steps - (`runtime_lane/device_copies.rs`) need the GPU busy past their calls, about 2 s of busy work - against calls of 5 to 47 ms on an RX 7900 XTX. A try whose busy work ended before its calls + (`runtime_lane/device_copies.rs`) and the host copies of new `Shared` and `Managed` textures + (`runtime_lane/first_use.rs`) need the GPU busy past their calls, about 2 s of busy work + against calls of 5 to 47 ms on an RX 7900 XTX and of 20 to 670 us on a Radeon 780M. A try whose busy work ended before its calls returned still checks its bytes, writes a `LANE-BUSY-MISS` line on stderr (each call's time and the busy work's GPU time), and runs again, at most 3 tries a step (`runtime_lane/busy_premise.rs`); a step that misses every try fails, as a call that waits for