diff --git a/crates/metal2vulkan-rt/README.md b/crates/metal2vulkan-rt/README.md index 21a554c1a..c84c59109 100644 --- a/crates/metal2vulkan-rt/README.md +++ b/crates/metal2vulkan-rt/README.md @@ -163,7 +163,11 @@ and a call that needs a runtime thread after `Device::quiesce` (`Error::Quiesced `Device::counters` returns a handle that stays readable after the device is gone. `metrics()` are cumulative counts (submissions, passes, barriers, bytes copied, pipelines created, blocks made, first-use waits, kernels lowered, kernels read from the cache directory, upload waits, pushes, -and the draws and dispatches whose push bytes the command buffer already held); +and the draws and dispatches whose push bytes the command buffer already held), and times in +nanoseconds, in total and longest: among them each `vkQueueSubmit2` call's time inside the +driver (`submit_call_nanos`, with the calls over 100 ms counted) and the submit thread's wait +for the queue's lock before a submission's calls (`queue_lock_wait_nanos`), which on a family of +one queue is also the wait behind the consumer's presents (`Device::with_present_queue`); `gauges()` are live counts per kind of resource, every one zero after teardown, and `handles_out`, 1 while a handle of the device is out (Teardown). They are relaxed atomics, always on. What an encoder's commands count arrives when the encoder ends. diff --git a/crates/metal2vulkan-rt/src/counters.rs b/crates/metal2vulkan-rt/src/counters.rs index b288e1e15..e77c317ca 100644 --- a/crates/metal2vulkan-rt/src/counters.rs +++ b/crates/metal2vulkan-rt/src/counters.rs @@ -26,6 +26,10 @@ fn bucket(bounds: &[u64], nanos: u64) -> usize { /// The bounds of each [`Timed`]'s buckets, in nanoseconds: 1, 5 and 20 ms. const OVER_NANOS: [u64; 3] = [1_000_000, 5_000_000, 20_000_000]; +/// A `vkQueueSubmit2` call longer than this, in nanoseconds (100 ms), is counted in +/// [`Metrics::submit_calls_over_100_ms`]. +const LONG_SUBMIT_CALL: u64 = 100_000_000; + /// A part of the consumer's takes, buffer makes and exported block makes the runtime times, /// counted on its own in [`Metrics`]. #[derive(Clone, Copy)] @@ -357,6 +361,11 @@ struct Atomics { threads: [AtomicU64; THREADS], submissions: AtomicU64, submit_infos: AtomicU64, + submit_call_nanos: AtomicU64, + submit_call_max_nanos: AtomicU64, + submit_calls_over_100_ms: AtomicU64, + queue_lock_wait_nanos: AtomicU64, + queue_lock_wait_max_nanos: AtomicU64, passes: AtomicU64, barriers: AtomicU64, bytes_copied: AtomicU64, @@ -642,6 +651,24 @@ impl Counters { self.atomics.submit_infos.fetch_add(infos, Relaxed); } + /// A `vkQueueSubmit2` call that took `nanos` nanoseconds ([`Metrics::submit_call_nanos`]). + pub(crate) fn submit_call(&self, nanos: u64) { + let a = &self.atomics; + a.submit_call_nanos.fetch_add(nanos, Relaxed); + a.submit_call_max_nanos.fetch_max(nanos, Relaxed); + if nanos > LONG_SUBMIT_CALL { + a.submit_calls_over_100_ms.fetch_add(1, Relaxed); + } + } + + /// A submission that waited `nanos` nanoseconds for the runtime's queue lock before its + /// calls ([`Metrics::queue_lock_wait_nanos`]). + pub(crate) fn queue_lock_wait(&self, nanos: u64) { + let a = &self.atomics; + a.queue_lock_wait_nanos.fetch_add(nanos, Relaxed); + a.queue_lock_wait_max_nanos.fetch_max(nanos, Relaxed); + } + /// An allocation of `bytes` made for blocks ([`Metrics::block_bytes_made`]). pub(crate) fn allocated(&self, bytes: u64) { self.atomics.block_bytes_made.fetch_add(bytes, Relaxed); @@ -1079,6 +1106,11 @@ impl Counters { Metrics { submissions: a.submissions.load(Relaxed), submit_infos: a.submit_infos.load(Relaxed), + submit_call_nanos: a.submit_call_nanos.load(Relaxed), + submit_call_max_nanos: a.submit_call_max_nanos.load(Relaxed), + submit_calls_over_100_ms: a.submit_calls_over_100_ms.load(Relaxed), + queue_lock_wait_nanos: a.queue_lock_wait_nanos.load(Relaxed), + queue_lock_wait_max_nanos: a.queue_lock_wait_max_nanos.load(Relaxed), passes: a.passes.load(Relaxed), barriers: a.barriers.load(Relaxed), bytes_copied: a.bytes_copied.load(Relaxed), @@ -1275,6 +1307,27 @@ pub struct Metrics { /// The `VkSubmitInfo2`s those calls passed: the command buffers of a submission that no event /// wait or signal separates share one, and the upload shares the first. pub submit_infos: u64, + /// The nanoseconds of [`host_time`](crate::host_time) those calls took, from each call to + /// its return, in total: the time inside the driver, which a driver may spend holding the call + /// while another thread is inside a memory call + /// ([`submissions_during_frees`](Self::submissions_during_frees)). The wait for the queue's + /// lock before them is [`queue_lock_wait_nanos`](Self::queue_lock_wait_nanos). + pub submit_call_nanos: u64, + /// The longest such call, in nanoseconds; a maximum since the device was made, so the + /// difference of two reads does not apply to it. + pub submit_call_max_nanos: u64, + /// Those calls longer than 100 ms. + pub submit_calls_over_100_ms: u64, + /// The nanoseconds of [`host_time`](crate::host_time) the submit thread waited for the + /// runtime's queue lock before a submission's `vkQueueSubmit2` calls, in total: one wait a + /// submission, whose calls then hold the lock. A sparse bind of an import takes the lock too. + /// Where the queue family has one queue, the lock is also + /// [`Device::with_present_queue`](crate::Device::with_present_queue)'s, so a wait here is + /// also the submit thread's wait behind the consumer's presents. + pub queue_lock_wait_nanos: u64, + /// The longest such wait, in nanoseconds; a maximum since the device was made, so the + /// difference of two reads does not apply to it. + pub queue_lock_wait_max_nanos: u64, /// Encoders recorded: compute, blit and render. pub passes: u64, /// Pipeline-barrier commands recorded into command buffers' work: one at the end of each diff --git a/crates/metal2vulkan-rt/src/submit.rs b/crates/metal2vulkan-rt/src/submit.rs index 3eabeaf78..de2858378 100644 --- a/crates/metal2vulkan-rt/src/submit.rs +++ b/crates/metal2vulkan-rt/src/submit.rs @@ -179,6 +179,10 @@ pub(crate) struct Work { /// For the device tests: a commit began its count of the wait for the work lock. #[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))] pub(crate) counting: AtomicBool, + /// For the device tests: a submission began its count of the wait for the runtime's queue + /// lock. + #[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))] + pub(crate) asking: AtomicBool, } pub(crate) struct WorkState { @@ -530,6 +534,8 @@ impl Work { released: AtomicU64::new(0), #[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))] counting: AtomicBool::new(false), + #[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))] + asking: AtomicBool::new(false), } } @@ -2138,7 +2144,11 @@ fn submit(core: &Core, value: u64, uploads: Vec, batch: Batch) { }]), } if ok { + let asked = times::host_time(); + #[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))] + (core.work.asking).store(true, Ordering::SeqCst); let queue = lock(&core.queues.runtime); + (core.counters).queue_lock_wait(times::host_time().saturating_sub(asked)); if core.submit_calls.freeing() { core.counters.count(Metric::SubmissionsDuringFrees); } @@ -2174,10 +2184,12 @@ fn submit(core: &Core, value: u64, uploads: Vec, batch: Batch) { // Inside the driver from here until the call returns, for the block-maker thread's // wait after its memory calls (`crate::submit_calls`). let call = core.submit_calls.enter(); + let began = times::host_time(); // SAFETY: every command buffer is executable and in no other pending submission; // every wait is on a signal submitted earlier, and every signal raises its semaphore; // the queue is held. ok = unsafe { device.queue_submit2(*queue, &infos, vk::Fence::null()) }.is_ok(); + (core.counters).submit_call(times::host_time().saturating_sub(began)); drop(call); if !ok { break; diff --git a/crates/metal2vulkan-rt/src/tests/command.rs b/crates/metal2vulkan-rt/src/tests/command.rs index 49ad19c23..d0e5b4f7b 100644 --- a/crates/metal2vulkan-rt/src/tests/command.rs +++ b/crates/metal2vulkan-rt/src/tests/command.rs @@ -1619,6 +1619,61 @@ pub(crate) mod on_device { }); } + /// A submission's wait for the runtime's queue lock and its `vkQueueSubmit2` calls are timed: + /// one made while the test holds the lock counts at least the time from the start of its + /// count to the lock's release, here 20 ms or more, and the calls count a time of their own, + /// whose longest is at least their mean. + #[test] + fn every_device_times_its_submit_calls_and_their_wait_for_the_queue_lock() { + each_device("command_queue_lock_wait", |name, _, device| { + let mut arena = Arena::new(&device); + let buffer = arena.buffer(); + let queue = queue(&device); + let counters = device.counters(); + let before = counters.metrics(); + let command_buffer = queue.command_buffer().expect("a command buffer"); + fill(&command_buffer, &buffer, 3); + let asking = || (device.core().work.asking).load(std::sync::atomic::Ordering::SeqCst); + (device.core().work.asking).store(false, std::sync::atomic::Ordering::SeqCst); + let (waiting, released) = device.with_runtime_queue(|_| { + command_buffer.commit().expect("a commit"); + within("the submission's count", asking); + let waiting = host_time(); + thread::sleep(Duration::from_millis(20)); + (waiting, host_time()) + }); + finished(name, &command_buffer); + within("the call's time", || { + counters.metrics().submit_call_nanos > before.submit_call_nanos + }); + let after = counters.metrics(); + assert!( + after.queue_lock_wait_max_nanos >= released - waiting, + "{name}: {} counted, the lock held {} past the start of the submission's count", + after.queue_lock_wait_max_nanos, + released - waiting + ); + assert!( + after.queue_lock_wait_nanos - before.queue_lock_wait_nanos >= released - waiting, + "{name}: the waits' total" + ); + assert!(after.queue_lock_wait_nanos >= after.queue_lock_wait_max_nanos); + let calls = after.submissions; + assert!(calls > before.submissions, "{name}: a submission"); + assert!( + after.submit_call_max_nanos > 0 + && after.submit_call_max_nanos >= after.submit_call_nanos / calls + && after.submit_call_nanos >= after.submit_call_max_nanos, + "{name}: the longest call {} against {} over {calls} calls", + after.submit_call_max_nanos, + after.submit_call_nanos + ); + assert!(after.submit_calls_over_100_ms <= calls, "{name}"); + assert_eq!(command_buffer.error(), None, "{name}"); + drop((command_buffer, buffer, arena, queue, device)); + }); + } + /// Commits a command buffer of one fill of `buffer` with `word` on `queue`. fn queue_fill(queue: &CommandQueue, buffer: &Buffer, word: u32) -> CommandBuffer { let command_buffer = queue.command_buffer().expect("a command buffer"); diff --git a/crates/metal2vulkan/tests/api_signatures.tsv b/crates/metal2vulkan/tests/api_signatures.tsv index 384be5e1c..51dd5687a 100644 --- a/crates/metal2vulkan/tests/api_signatures.tsv +++ b/crates/metal2vulkan/tests/api_signatures.tsv @@ -2454,6 +2454,8 @@ field metal2vulkan-rt::Metrics.private_long_reuses pub private_long_reuses: u64 field metal2vulkan-rt::Metrics.private_take_allocations pub private_take_allocations: SiteAllocations field metal2vulkan-rt::Metrics.pushes pub pushes: u64 field metal2vulkan-rt::Metrics.pushes_held pub pushes_held: u64 +field metal2vulkan-rt::Metrics.queue_lock_wait_max_nanos pub queue_lock_wait_max_nanos: u64 +field metal2vulkan-rt::Metrics.queue_lock_wait_nanos pub queue_lock_wait_nanos: u64 field metal2vulkan-rt::Metrics.render_records_read pub render_records_read: u64 field metal2vulkan-rt::Metrics.render_records_stale pub render_records_stale: u64 field metal2vulkan-rt::Metrics.resolves pub resolves: u64 @@ -2469,6 +2471,9 @@ field metal2vulkan-rt::Metrics.staging_sleeps pub staging_sleeps: u64 field metal2vulkan-rt::Metrics.stamps_not_available pub stamps_not_available: u64 field metal2vulkan-rt::Metrics.submissions pub submissions: u64 field metal2vulkan-rt::Metrics.submissions_during_frees pub submissions_during_frees: u64 +field metal2vulkan-rt::Metrics.submit_call_max_nanos pub submit_call_max_nanos: u64 +field metal2vulkan-rt::Metrics.submit_call_nanos pub submit_call_nanos: u64 +field metal2vulkan-rt::Metrics.submit_calls_over_100_ms pub submit_calls_over_100_ms: u64 field metal2vulkan-rt::Metrics.submit_infos pub submit_infos: u64 field metal2vulkan-rt::Metrics.surface_wait_max_nanos pub surface_wait_max_nanos: u64 field metal2vulkan-rt::Metrics.surface_wait_nanos pub surface_wait_nanos: u64