From 2b44d260968d218e30b07be35ff6ef339836c8bf Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Sat, 7 Nov 2026 19:03:09 -0800 Subject: [PATCH] In-place check: its work counts as the device's, since its thread carries the program's batches tests::freed_memory::the_in_place_check_s_buffers_delay_no_deallocator_behind_batches_in_flight failed 2 of 2 on msi lavapipe at development 73f73a3bd3 (m3-max-6379) and f09bdd0f84 (6380): "the batch was never submitted" after about 75 s. Since 09253899ac a vkQueueSubmit (and a making or release) on any check's thread counted under WorkCounts::device_checks. The in-place check makes its buffers, textures and command buffers through the program's paths, and a pass of the scheduler its waits take on its thread submits the program's ready batches too: the test's fills were submitted there, so queue_submissions never rose, and the counts gave the program's work to the checks. A check's thread now says whose its counts are (step_aside::Counted): Apart for the submission and queue-switch checks, whose every Vulkan call is their own (the device's functions directly, no pass of the scheduler), AsTheDevice for the in-place check, whose makings, releases and submissions count as the device's again. The step-aside's own-activity rule is unchanged for all three. DeviceCheckWork's, in_place_check.rs's and step_aside.rs's docs say so. Tests: churn_counts::tests::a_check_s_makings_through_the_program_s_paths_are_the_process_s (new); the freed_memory test above on msi lavapipe, against development 9696d4b3dd as its negative control, follows. (cherry picked from commit 957c192c5bf3dd0d44c8a33412520147c29cb6d1) --- .../metal2vulkan-runtime/src/churn_counts.rs | 34 ++++++++++++-- .../src/in_place_check.rs | 7 +-- .../src/queue_switch_check.rs | 2 +- crates/metal2vulkan-runtime/src/step_aside.rs | 44 +++++++++++++++---- .../metal2vulkan-runtime/src/submit_check.rs | 2 +- .../metal2vulkan-runtime/src/work_counts.rs | 36 +++++++-------- 6 files changed, 89 insertions(+), 36 deletions(-) diff --git a/crates/metal2vulkan-runtime/src/churn_counts.rs b/crates/metal2vulkan-runtime/src/churn_counts.rs index 3d18c65fd..2ec73cede 100644 --- a/crates/metal2vulkan-runtime/src/churn_counts.rs +++ b/crates/metal2vulkan-runtime/src/churn_counts.rs @@ -20,7 +20,7 @@ //! planned them (a render pass's attachments, a compute dispatch's bindings, a blit), cut to a //! rectangle or of every texel, and the texels they copy. -use crate::step_aside::on_a_check_s_thread; +use crate::step_aside::counted_apart; use crate::work_counts::{CopySite, CopySites, COPY_SITES}; use std::panic::Location; use std::sync::atomic::{AtomicU64, Ordering}; @@ -71,7 +71,7 @@ pub(crate) enum VkMaking { /// A release of `what` (`release::object`), called `at`: a check's where a device's check made /// it ([`CHECK_RELEASES`]). pub(crate) fn vk_released(what: VkRelease, at: &Location<'static>) { - if on_a_check_s_thread() { + if counted_apart() { CHECK_RELEASES[what as usize].fetch_add(1, Ordering::Relaxed); return; } @@ -88,7 +88,7 @@ pub(crate) fn vk_released(what: VkRelease, at: &Location<'static>) { /// A making of `what` (`creation.rs`), called `at`: a check's where a device's check made it /// ([`CHECK_MAKINGS`]). pub(crate) fn vk_made(what: VkMaking, at: &Location<'static>) { - if on_a_check_s_thread() { + if counted_apart() { CHECK_MAKINGS[what as usize].fetch_add(1, Ordering::Relaxed); return; } @@ -394,7 +394,7 @@ mod tests { ) }; let (before, before_checks) = (process(), checks()); - crate::step_aside::mark_a_check_s_thread(); + crate::step_aside::mark_a_check_s_thread(crate::step_aside::Counted::Apart); vk_made(VkMaking::Memory, Location::caller()); vk_released(VkRelease::Memory, Location::caller()); assert_eq!( @@ -404,4 +404,30 @@ mod tests { ); assert_eq!(checks(), (before_checks.0 + 1, before_checks.1 + 1)); } + + /// The in-place check's thread, whose paths carry the program's work too, counts its makings + /// and releases as the process's (`step_aside::Counted::AsTheDevice`). + #[test] + fn a_check_s_makings_through_the_program_s_paths_are_the_process_s() { + let process = || { + let churn = ChurnCounters::default().snapshot().0; + (churn.vk_memory_allocations, churn.vk_memory_freed) + }; + let checks = || { + ( + checks_made(VkMaking::Memory), + checks_released(VkRelease::Memory), + ) + }; + let (before, before_checks) = (process(), checks()); + crate::step_aside::mark_a_check_s_thread(crate::step_aside::Counted::AsTheDevice); + vk_made(VkMaking::Memory, Location::caller()); + vk_released(VkRelease::Memory, Location::caller()); + assert_eq!(process(), (before.0 + 1, before.1 + 1)); + assert_eq!( + checks(), + before_checks, + "the checks' counts took the device's work" + ); + } } diff --git a/crates/metal2vulkan-runtime/src/in_place_check.rs b/crates/metal2vulkan-runtime/src/in_place_check.rs index da8dc223e..e0f2ba883 100644 --- a/crates/metal2vulkan-runtime/src/in_place_check.rs +++ b/crates/metal2vulkan-runtime/src/in_place_check.rs @@ -76,8 +76,9 @@ //! allocations and frees on other threads (`step_aside.rs`): it goes on once none is in //! progress and none ended for `step_aside::QUIET`, at once while a call waits for its verdicts //! (`Device::settle_in_place_checks`). Its own buffers and textures, made through the product's -//! paths on its thread, count for nothing there, and their allocations, releases and -//! submissions on its thread are counted as the checks' (`WorkCounts::device_checks`). +//! paths on its thread, count for nothing there. Its Vulkan work is counted as the device's +//! (`step_aside::Counted::AsTheDevice`): those paths, and the scheduler's passes its waits take +//! on its thread, carry the program's batches beside its own. //! - **Its thread.** Each round's Vulkan work (the buffer, the texture, the submission and its //! wait, the releases) runs inside a section of the exit guard (`exit_guard.rs`), as every //! runtime thread's does. It holds its device through the registry of the runtime's own holds @@ -276,7 +277,7 @@ fn step_aside(device: &Device) { /// known, then the record. fn run(weak: crate::device::WeakDevice) { mark_checking(); - crate::step_aside::mark_a_check_s_thread(); + crate::step_aside::mark_a_check_s_thread(crate::step_aside::Counted::AsTheDevice); let started = Instant::now(); let mut stopped = false; for class in PagesClass::ALL { diff --git a/crates/metal2vulkan-runtime/src/queue_switch_check.rs b/crates/metal2vulkan-runtime/src/queue_switch_check.rs index 146766fc6..d6f5e3be4 100644 --- a/crates/metal2vulkan-runtime/src/queue_switch_check.rs +++ b/crates/metal2vulkan-runtime/src/queue_switch_check.rs @@ -123,7 +123,7 @@ pub(crate) fn start(device: &Device) -> bool { let switch = Arc::clone(&parts.switch); #[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))] crate::loader_word::on_a_check_s_thread(); - crate::step_aside::mark_a_check_s_thread(); + crate::step_aside::mark_a_check_s_thread(crate::step_aside::Counted::Apart); run(&parts, made); // Its shares go before the teardown that waits for it goes on. #[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))] diff --git a/crates/metal2vulkan-runtime/src/step_aside.rs b/crates/metal2vulkan-runtime/src/step_aside.rs index ac9dfc1bb..a45dfe1e4 100644 --- a/crates/metal2vulkan-runtime/src/step_aside.rs +++ b/crates/metal2vulkan-runtime/src/step_aside.rs @@ -29,9 +29,15 @@ //! - **What it costs.** A check's verdicts come later while the device's work goes on: until then //! the device takes the forms a device without them takes (each check's module docs). Counted: //! `WorkCounts::device_checks` (`steps_aside`, `aside_nanos`). -//! - **The checks' work is theirs.** A check's thread's makings, releases and submissions are -//! counted among its checks' (`WorkCounts::device_checks`), never the program's, so a program -//! or a case that meets a device first does not carry them. +//! - **The checks' work is theirs.** The makings, releases and submissions of a thread whose every +//! Vulkan call is its check's own (the submission checks' and the queue-switch check's, which +//! reach the device's functions directly: [`Counted::Apart`]) are counted among the checks' +//! (`WorkCounts::device_checks`), never the program's, so a program or a case that meets a +//! device first does not carry them. The in-place check's are the device's +//! ([`Counted::AsTheDevice`]): it makes its buffers, textures and command buffers through the +//! program's paths, and a pass of the scheduler its waits take on its thread submits and +//! releases the program's batches beside its own (`freed_memory.rs`'s in-place check test: the +//! program's fills, submitted there, were never counted among `queue_submissions`). use std::cell::Cell; use std::sync::atomic::{AtomicU32, AtomicU64, Ordering}; @@ -52,19 +58,39 @@ thread_local! { /// This thread is one of the device's checks' threads, whose own allocations and frees count /// for nothing here. static CHECKS: Cell = const { Cell::new(false) }; + /// Its makings, releases and submissions are counted as the checks' ([`Counted::Apart`]). + static APART: Cell = const { Cell::new(false) }; } -/// Mark this thread as a check's, for good: it is a thread of its own. -pub(crate) fn mark_a_check_s_thread() { +/// Whose counts a check's thread's makings, releases and submissions are. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(crate) enum Counted { + /// The checks' (`WorkCounts::device_checks`): every Vulkan call of the thread is its check's + /// own. + Apart, + /// The device's: the thread makes its check's objects through the program's paths, which may + /// carry the program's work on it too. + AsTheDevice, +} + +/// Mark this thread as a check's, for good: it is a thread of its own, its work counted as +/// `counted` says. +pub(crate) fn mark_a_check_s_thread(counted: Counted) { CHECKS.with(|checks| checks.set(true)); + APART.with(|apart| apart.set(counted == Counted::Apart)); } -/// Whether this thread is a check's ([`mark_a_check_s_thread`]): its makings, releases and -/// submissions are counted as the checks' (`WorkCounts::device_checks`). -pub(crate) fn on_a_check_s_thread() -> bool { +/// Whether this thread is a check's ([`mark_a_check_s_thread`]). +fn on_a_check_s_thread() -> bool { CHECKS.with(Cell::get) } +/// Whether this thread's makings, releases and submissions are counted as the checks' +/// ([`Counted::Apart`]). +pub(crate) fn counted_apart() -> bool { + APART.with(Cell::get) +} + /// One device's allocations and frees in progress on threads not its checks', and when the last /// ended. pub(crate) struct Activity { @@ -178,7 +204,7 @@ mod tests { #[test] fn a_check_s_own_allocations_count_for_nothing() { let activity = Activity::new(); - mark_a_check_s_thread(); + mark_a_check_s_thread(Counted::Apart); assert!(!activity.begin()); assert_eq!(activity.left(), Duration::ZERO); } diff --git a/crates/metal2vulkan-runtime/src/submit_check.rs b/crates/metal2vulkan-runtime/src/submit_check.rs index ea128e322..8101cb5ef 100644 --- a/crates/metal2vulkan-runtime/src/submit_check.rs +++ b/crates/metal2vulkan-runtime/src/submit_check.rs @@ -687,7 +687,7 @@ impl Device { let _ends = Ends(Arc::clone(&parts.checks)); #[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))] crate::loader_word::on_a_check_s_thread(); - crate::step_aside::mark_a_check_s_thread(); + crate::step_aside::mark_a_check_s_thread(crate::step_aside::Counted::Apart); let parts = parts; run(&parts, made); }); diff --git a/crates/metal2vulkan-runtime/src/work_counts.rs b/crates/metal2vulkan-runtime/src/work_counts.rs index 109614c6b..13a95779d 100644 --- a/crates/metal2vulkan-runtime/src/work_counts.rs +++ b/crates/metal2vulkan-runtime/src/work_counts.rs @@ -1909,10 +1909,10 @@ impl Counters { /// A `vkQueueSubmit` or `vkQueueBindSparse` call was made with `infos` `VkSubmitInfo`s /// (none for a sparse bind) and `command_buffers` `VkCommandBuffer`s among them. - /// Made on a thread of the device's checks (`step_aside.rs`), it is the checks' work and - /// counted as theirs alone ([`DeviceCheckWork::queue_submissions`]). + /// Made on a thread whose every call is its check's (`step_aside::Counted::Apart`), it is the + /// checks' work and counted as theirs alone ([`DeviceCheckWork::queue_submissions`]). pub(crate) fn queue_submitted(&self, infos: usize, command_buffers: usize) { - if crate::step_aside::on_a_check_s_thread() { + if crate::step_aside::counted_apart() { self.check_queue_submissions.fetch_add(1, Ordering::Relaxed); return; } @@ -3770,18 +3770,19 @@ pub struct MemoryFrees { pub blocks: u64, } -/// The Vulkan work of the device's own checks (its submission, queue-switch and in-place checks: -/// `submit_check.rs`, `queue_switch_check.rs`, `in_place_check.rs`), made on their threads -/// (`step_aside.rs`) and counted here alone: the makings and releases of the whole process's -/// checks, as `ObjectChurn`'s are the whole process's, and the device's checks' submissions. A -/// check runs once per driver and release with no record of its verdicts, beside whatever the -/// program does then, so none of this is the program's. A field of `WorkCounts`, for the lanes: -/// no Metal counterpart, not part of the API. +/// The Vulkan work of the device's submission and queue-switch checks (`submit_check.rs`, +/// `queue_switch_check.rs`), made on their threads, which reach the device's functions directly +/// and make no call but their check's (`step_aside::Counted::Apart`), and counted here alone: the +/// makings and releases of the whole process's such checks, as `ObjectChurn`'s are the whole +/// process's, and the device's queue-switch check's submissions. A check runs once per driver and +/// release with no record of its verdicts, beside whatever the program does then, so none of this +/// is the program's. A field of `WorkCounts`, for the lanes: no Metal counterpart, not part of +/// the API. /// -/// The in-place check makes its buffers, textures and command buffers through the program's -/// paths, which count them as they count the program's (`buffers_made`, `commands_recorded` and -/// the like); its `vkAllocateMemory`, release and submission calls on its thread are here, and a -/// free the device's free thread makes for it is the device's. +/// The in-place check's work is the device's, counted as the program's paths count it +/// (`step_aside::Counted::AsTheDevice`): it makes its buffers, textures and command buffers +/// through those paths, and a pass of the scheduler its waits take on its thread submits and +/// releases the program's batches beside its own. #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] pub struct DeviceCheckWork { /// `vkAllocateMemory` calls (`ObjectChurn::vk_memory_allocations` leaves them out). @@ -3804,10 +3805,9 @@ pub struct DeviceCheckWork { pub vk_image_bindings: u64, /// `vkBindBufferMemory` calls. pub vk_buffer_bindings: u64, - /// `vkQueueSubmit` and `vkQueueBindSparse` calls the device's queue-switch and in-place - /// checks made, and the relay's marks after their uses of a queue - /// (`WorkCounts::queue_submissions` leaves them out); the submission checks' are - /// `WorkCounts::submit_check_calls`. + /// `vkQueueSubmit` calls the device's queue-switch check made, and the relay's marks after its + /// uses of a queue (`WorkCounts::queue_submissions` leaves them out); the submission checks' + /// are `WorkCounts::submit_check_calls`. pub queue_submissions: u64, /// Steps of the device's checks that waited for the device's allocations and frees on other /// threads before they went on (`step_aside.rs`).