From 5efe2f1d7399d09b8899ccc9bb85cf893cce7f59 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Sat, 14 Nov 2026 21:02:38 -0800 Subject: [PATCH] Test queue limits count the program's live command queue handles The runtime tests' default queue limits count the queues the program holds, not live ones The check that fails a test needing the queue plan counted live queues (QueueShared). A command buffer holds its queue's QueueShared, and the runtime's threads let a completed one go a moment after its wait returns. So a test that let a queue go and then made two more could fail or pass by timing. The check now counts the command queues the program holds a handle of: - each QueueLine counts, under cfg(test), the command buffers alive whose `commandQueue` it is; - a queue the program made is held where its strong references outnumber those command buffers plus the caller's upgrade (QueueLine::held_by_the_program, Line::program_queues_held); - a command buffer takes its count before its handle of the queue and drops it after, so a count in passing reads one fewer, never one more. The product's count of live queues (WorkCounts::command_queues_live_most) is unchanged. The device test a_default_test_device_fails_where_its_work_needs_the_plan now keeps a command buffer of a queue it let go before making the third. The live-queue count panicked there; the held count does not. The message reads "while the program holds 2 of its command queues". docs/VALIDATION.md and TEST_QUEUE_LIMITS' doc say the same. The command buffer's count among its queue's is a guard held for its drop: _counted Clippy (-D warnings) refused 3d6e4e5957's field `counted`, never read: the field exists for its Drop, which takes the command buffer's count off its queue's. Named _counted, as a guard held for its drop. The test limits' check runs where the program makes a queue, not where a recovery places one again With the held count, a recovery's queue that took its place on the new generation counted itself and the program's other queues. So device_recovery's a_loss_holds_the_hang_s_queue_until_the_recovery and work_held_unsubmitted_at_a_loss_fails_as_its_victims_in_queue_order (two queues each) failed on lavapipe at 577d649100. The rest of the suite passed: 2741 of 2743. The check now runs in Device::command_queue, for a new round-robin or unshared queue of the program's, before its place. A queue placed again after a recovery was counted where it was made. Verified: job 31249 at 1f69cb2e0c: clippy plain, --cfg m2v_gpu and --cfg m2v_cpu_icd; queue_limits, queue_limits_device and device_recovery on lavapipe three times (22/22 each); the runtime suite on lavapipe 2743/2743; a negative control (the old live count) fails at the third queue. Test-only (cfg(test)). --- .../src/command_buffer/mod.rs | 8 +++ .../metal2vulkan-runtime/src/device/create.rs | 2 +- .../metal2vulkan-runtime/src/device/line.rs | 16 ++++++ crates/metal2vulkan-runtime/src/device/mod.rs | 29 +++++------ crates/metal2vulkan-runtime/src/queue.rs | 52 +++++++++++++++++++ .../src/tests/queue_limits_device.rs | 16 ++++-- docs/VALIDATION.md | 6 +-- 7 files changed, 105 insertions(+), 24 deletions(-) diff --git a/crates/metal2vulkan-runtime/src/command_buffer/mod.rs b/crates/metal2vulkan-runtime/src/command_buffer/mod.rs index d73a4f168..b27e3a36c 100644 --- a/crates/metal2vulkan-runtime/src/command_buffer/mod.rs +++ b/crates/metal2vulkan-runtime/src/command_buffer/mod.rs @@ -266,6 +266,10 @@ pub(super) struct CommandBufferShared { queue: QueueGen, /// Its queue (`commandQueue`). root: CommandQueue, + /// Its count among `root`'s command buffers, which the runtime tests' default queue limits' + /// check takes apart from the program's handles (`queue.rs`). Declared after `root`. + #[cfg(test)] + _counted: crate::queue::CountedCommandBuffer, state: Mutex, /// The slot of its queue it holds until it completes, or is dropped uncommitted /// (`queue.rs`). Apart from the state, which a submission of its batches holds through @@ -2600,6 +2604,10 @@ impl CommandBuffer { generation: Arc::clone(queue.device().shared_arc()), device: queue.device().clone(), queue: queue.clone(), + // Counted before its handle is taken, as its handle goes before its count: the count + // reads one fewer of the program's handles for a moment, never one more. + #[cfg(test)] + _counted: crate::queue::CountedCommandBuffer::new(root), root: root.clone(), state: Mutex::new(State { status: CommandBufferStatus::NotEnqueued, diff --git a/crates/metal2vulkan-runtime/src/device/create.rs b/crates/metal2vulkan-runtime/src/device/create.rs index e677401c4..7d83e3fcb 100644 --- a/crates/metal2vulkan-runtime/src/device/create.rs +++ b/crates/metal2vulkan-runtime/src/device/create.rs @@ -530,7 +530,7 @@ fn default_queue_limits() -> Option { /// of 8.1 s and their `vkDestroyDevice` 2.8 s. A device of fewer queues costs less of both /// (`DeviceOptions::queue_limits`'s measurement). A test whose work needs more than these limits /// give on a device with queues to spare (a command queue of the program's, unshared or not, made -/// while two are live, which would share a `VkQueue` with one of them; a presenting queue, which +/// while it holds two others, which would share a `VkQueue` with one of them; a presenting queue, which /// would share the round robin's; reserved or extra queues, placed otherwise) panics where it /// asks for it, on every device (`needs_the_queue_plan`), and makes its device with /// `queue_limits: None`, the plan: as the runtime lane's tests make theirs past its diff --git a/crates/metal2vulkan-runtime/src/device/line.rs b/crates/metal2vulkan-runtime/src/device/line.rs index ecfe9bee1..b8c5d9d74 100644 --- a/crates/metal2vulkan-runtime/src/device/line.rs +++ b/crates/metal2vulkan-runtime/src/device/line.rs @@ -596,6 +596,22 @@ impl Line { queues.push(queue); } + /// How many of the command queues the program made it holds a handle of + /// (`QueueLine::held_by_the_program`), for the runtime tests' default queue limits' check + /// (`Device::test_program_queue`). The upgrades go after the lock, so no queue's last + /// reference drops under it. + #[cfg(test)] + pub(crate) fn program_queues_held(&self) -> u64 { + let queues: Vec<_> = lock(&self.queues) + .iter() + .filter_map(Weak::upgrade) + .collect(); + let held = queues + .iter() + .filter(|line| crate::queue::QueueLine::held_by_the_program(line)); + held.count() as u64 + } + /// The program's last handle went (`holds.rs`): the generation it serves goes once nothing /// else holds it, here or as the last handle drops. pub(super) fn release(&self) { diff --git a/crates/metal2vulkan-runtime/src/device/mod.rs b/crates/metal2vulkan-runtime/src/device/mod.rs index 9f6b0fdfa..03073ee72 100644 --- a/crates/metal2vulkan-runtime/src/device/mod.rs +++ b/crates/metal2vulkan-runtime/src/device/mod.rs @@ -1877,8 +1877,6 @@ impl Device { /// a queue the program made, `None` the runtime's own and a presenting queue's, which /// `Device::next_presenting_queue` notes ([`note_past_limits`](Self::note_past_limits)). pub(super) fn next_queue(&self, noted: Option<&str>) -> (u64, usize) { - #[cfg(test)] - self.test_program_queue(noted); let (id, made) = self.round_robin_turn(); let vk_queue = self.take_round_robin(made, false); self.note_past_limits(noted, vk_queue); @@ -1891,8 +1889,6 @@ impl Device { /// (`Device::take_round_robin`). A queue the program made, as /// [`next_queue`](Self::next_queue)'s. pub(super) fn next_unshared_queue(&self) -> (u64, usize) { - #[cfg(test)] - self.test_program_queue(Some("an unshared command queue")); let (id, made) = self.round_robin_turn(); let vk_queue = (self.place_unshared()).unwrap_or_else(|| self.take_round_robin(made, true)); self.note_past_limits(Some("an unshared command queue"), vk_queue); @@ -1950,19 +1946,22 @@ impl Device { self.shared().test_queue_limits.load(Ordering::Relaxed) } - /// On a device of the runtime tests' default queue limits, the program's command queue - /// `noted` names (`None` for the runtime's own, which no limit counts), before anything places - /// it: where as many of the program's queues as the limits' round robin has `VkQueue`s are - /// live, the test fails here (`create::needs_the_queue_plan`), as this one would share a - /// `VkQueue` with a live one where a plan of queues to spare gives it one of its own. + /// On a device of the runtime tests' default queue limits, a new command queue of the + /// program's, `queue` naming it, before anything places it (`queue.rs`; not the place a + /// recovery's queue takes again, counted where it was made): where the program holds handles + /// of as many of its queues as the limits' round robin has `VkQueue`s + /// (`Line::program_queues_held`: the handles its command buffers hold not counted, so the + /// answer follows the test's code, not when the runtime lets a completed command buffer go), + /// the test fails here (`create::needs_the_queue_plan`), as this one would share a `VkQueue` + /// with one of them where a plan of queues to spare gives it one of its own. #[cfg(test)] - fn test_program_queue(&self, noted: Option<&str>) { - let (Some(queue), true) = (noted, self.test_queue_limited()) else { + pub(crate) fn test_program_queue(&self, queue: &str) { + if !self.test_queue_limited() { return; - }; - let live = self.shared().program_queues_live.load(Ordering::Relaxed); - if live >= u64::from(create::TEST_QUEUE_LIMITS.round_robin) { - let what = format!("made {queue} while {live} of the program's are live"); + } + let held = self.line().program_queues_held(); + if held >= u64::from(create::TEST_QUEUE_LIMITS.round_robin) { + let what = format!("made {queue} while the program holds {held} of its command queues"); create::needs_the_queue_plan(&what); } } diff --git a/crates/metal2vulkan-runtime/src/queue.rs b/crates/metal2vulkan-runtime/src/queue.rs index dd9c18300..f4131de8c 100644 --- a/crates/metal2vulkan-runtime/src/queue.rs +++ b/crates/metal2vulkan-runtime/src/queue.rs @@ -140,6 +140,46 @@ pub(crate) struct QueueLine { count: usize, common: Arc, cell: Mutex, + /// The command buffers alive whose `commandQueue` it is, each holding a handle of it + /// (`CommandBuffer::command_queue`): the program holds the others + /// ([`QueueLine::held_by_the_program`]). + #[cfg(test)] + command_buffers: Arc, +} + +#[cfg(test)] +impl QueueLine { + /// Whether the program holds a handle of `line`, a command queue it made, the caller holding + /// one strong reference (an upgrade): a handle past those its command buffers hold. A count + /// of the program's code alone, whatever the runtime's threads still hold of a command buffer + /// that completed; one that drops reads one fewer for a moment, never one more + /// ([`CountedCommandBuffer`]). + pub(crate) fn held_by_the_program(line: &Arc) -> bool { + line.made_for == MadeFor::Program + && Arc::strong_count(line) > 1 + line.command_buffers.load(Ordering::Relaxed) + } +} + +/// A command buffer's count among those of its `commandQueue` ([`QueueLine::command_buffers`]), +/// from its making to its end: taken before the command buffer's handle of its queue, and +/// declared after it, so the handle goes first. +#[cfg(test)] +pub(crate) struct CountedCommandBuffer(Arc); + +#[cfg(test)] +impl CountedCommandBuffer { + /// One more command buffer of `root`. + pub(crate) fn new(root: &CommandQueue) -> CountedCommandBuffer { + root.0.command_buffers.fetch_add(1, Ordering::Relaxed); + CountedCommandBuffer(Arc::clone(&root.0.command_buffers)) + } +} + +#[cfg(test)] +impl Drop for CountedCommandBuffer { + fn drop(&mut self) { + self.0.fetch_sub(1, Ordering::Relaxed); + } } /// What every place of a command queue shares (the module docs). @@ -572,6 +612,16 @@ impl Device { /// `made_for`, with its place on this generation, known to the device's line, whose /// recoveries retire it (`device/line.rs`). fn command_queue(&self, count: usize, kind: Kind, made_for: MadeFor) -> CommandQueue { + // A new queue of the program's, not the place a recovery's takes again + // (`QueueGen::follow`): the runtime tests' default queue limits' check. + #[cfg(test)] + match (made_for, kind) { + (MadeFor::Program, Kind::RoundRobin) => self.test_program_queue("a command queue"), + (MadeFor::Program, Kind::Unshared) => { + self.test_program_queue("an unshared command queue") + } + _ => {} + } let common = Arc::new(QueueCommon { label: Mutex::new(None), errors: QueueErrors::default(), @@ -584,6 +634,8 @@ impl Device { count, common, cell: Mutex::new(Place::Serving(place)), + #[cfg(test)] + command_buffers: Arc::default(), })); self.line().queue_made(Arc::downgrade(&queue.0)); queue diff --git a/crates/metal2vulkan-runtime/src/tests/queue_limits_device.rs b/crates/metal2vulkan-runtime/src/tests/queue_limits_device.rs index d1adb80d4..6d8aafb20 100644 --- a/crates/metal2vulkan-runtime/src/tests/queue_limits_device.rs +++ b/crates/metal2vulkan-runtime/src/tests/queue_limits_device.rs @@ -270,9 +270,11 @@ fn panic_of(call: impl FnOnce()) -> Option { /// A device of the runtime tests' default queue limits (`DeviceOptions::default()` in the test /// build, `TEST_QUEUE_LIMITS`) fails its test where the test's work needs more than they give on a /// device with queues to spare, on every device, whatever its own plan: a command queue of the -/// program's, unshared or not, made while two are live, a presenting queue, reserved or extra -/// queues; and it makes two live command queues, a third once one of them went, and reads its -/// queue sharing. A device of the plan (`queue_limits: None`) makes each. +/// program's, unshared or not, made while the program holds two others, a presenting queue, +/// reserved or extra queues; and it makes two command queues, a third once the program let one of +/// them go though it keeps a command buffer of it (which holds that queue, so the old count of +/// live queues failed here), and reads its queue sharing. A device of the plan +/// (`queue_limits: None`) makes each. #[test] fn a_default_test_device_fails_where_its_work_needs_the_plan() { let scratch = Scratch::new("queue-limits-test-default"); @@ -306,11 +308,13 @@ fn a_default_test_device_fails_where_its_work_needs_the_plan() { let first = device.new_command_queue().unwrap(); let _second = device.new_command_queue_unshared(64).unwrap(); let third = panic_of(|| drop(device.new_command_queue())).unwrap_or_default(); - assert!(third.contains("while 2 of the program's are live") && third.contains(needs)); + let held = "while the program holds 2 of its command queues"; + assert!(third.contains(held) && third.contains(needs), "{third}"); let unshared = panic_of(|| drop(device.new_command_queue_unshared(64))).unwrap_or_default(); assert!(unshared.contains("an unshared command queue"), "{unshared}"); let presenting = panic_of(|| drop(device.new_command_queue_for_presenting())); assert!(presenting.unwrap_or_default().contains("presenting")); + let kept = first.command_buffer().unwrap(); drop(first); let made = panic_of(|| { let _third = device.new_command_queue().unwrap(); @@ -318,8 +322,10 @@ fn a_default_test_device_fails_where_its_work_needs_the_plan() { }); assert_eq!( made, None, - "a third once the first went, and a read of the sharing" + "a third once the program let the first go, a command buffer of it kept, and a read of \ + the sharing" ); + drop(kept); drop(device); let reserved = panic_of(|| { let asked = DeviceOptions { diff --git a/docs/VALIDATION.md b/docs/VALIDATION.md index feb1304c2..f404e1e83 100644 --- a/docs/VALIDATION.md +++ b/docs/VALIDATION.md @@ -97,9 +97,9 @@ with 16 CPU threads to itself, and 0.1 to 0.3 s on an RX 7900 XTX and a Radeon 7 `DeviceOptions::default()` holds queue limits of a round robin of two and none for presenting (`TEST_QUEUE_LIMITS`, `device/create.rs`), as the runtime lane's devices hold theirs (`LANE_QUEUE_LIMITS`): NVIDIA's driver makes and destroys one device at a time across processes, - each queue adding to both. A test whose work needs more (a command queue made while two are - live, a presenting, reserved or extra queue) panics where it asks for it, on every device, and - makes its device with `queue_limits: None`. + each queue adding to both. A test whose work needs more (a command queue made while the test + holds two others, a presenting, reserved or extra queue) panics where it asks for it, on every + device, and makes its device with `queue_limits: None`. - **Waits are bounded.** Every host wait on device work goes through `src/gpu/wait.rs` (10 seconds by default; `--gpu-wait-timeout SECONDS` raises it for one command). A test fails if an unbounded wait appears elsewhere.