From 2e00ea6c13f455cd83c7a9ef8a76d449a68606e8 Mon Sep 17 00:00:00 2001 From: Jan Trencansky Date: Fri, 27 Nov 2026 11:37:59 -0500 Subject: runtime: track active async, executing, and GPU queue progress for list-value gates Fixes premature unmet release of console list-value gates at Present under LEONIDA_SUBMIT_ASYNC and unlocked queue execution: - QueueHolds (runtime/src/queue_holds.h): add process-wide queue_holds::any_progress() backed by a lock-free active CPU/async work counter (WorkGuard / work_changed), signaled_unpumped_ tracking across signaled() -> pump_ready(), and per-holder lock-free has_pending_gpu_work() protected against detach() via holders_use_mutex_. - VulkanQueue (runtime/src/vulkan_backend.h): account for queued async inbox calls, active async_worker turns (through pump_ready()), pending release_requested_ flags (with async_stop_ guard), synchronous/draining executing_ passes (through pump_ready()), VulkanFence::signal() -> pump_ready(), and unretired GPU tickets (submitted_gpu_ vs progress_ counter). Stop cross-queue release_ready() ping-pong by waking only when held_front_seq_ != 0 and calling pump_ready() in async_worker() only when a call ran or a held op was released. - Pump::choose_release (runtime/shims/d3d12_xs_list_values.h): treat leonida::queue_holds::any_progress() as active queue progress so held waits are never broken unmet while a producer async worker, executing pass, or GPU submission is still progressing; advance last_progress_ms_ so kStalledMs measures genuine quiet time. Genuine deadlocks (cycles, idle queues + idle GPU at Present, stalled title) still break cleanly. - Tests: add CPU-only QueueHolds + list_value_progress tests in list_value_progress_test.cpp and real VulkanQueue async-depth=4 + Present + busy producer + genuine deadlock + synchronous executing_ tests in submit_frame_test.cpp. AI-assisted: Antigravity, Gemini 4 Neon. Tested on Linux (RADV RAPHAEL_MENDOCINO, MESA_VK_DEVICE_SELECT=1002:164e!): 8/8 passed (list_value_progress, submit_frame, vk_queue, vk_queue_query_sync, vk_queue_constructor_oom, vk_fence_dispatcher_sync, vk_semaphore_lifetime, vk_fence_lost_by_counter). --- runtime/test/submit_frame_test.cpp | 138 +++++++++++++++++++++++++++++++++++++ 1 file changed, 138 insertions(+) (limited to 'runtime/test/submit_frame_test.cpp') diff --git a/runtime/test/submit_frame_test.cpp b/runtime/test/submit_frame_test.cpp index ba5d275c7..edf690006 100644 --- a/runtime/test/submit_frame_test.cpp +++ b/runtime/test/submit_frame_test.cpp @@ -7,6 +7,7 @@ // the test still passes): the floor's phase sums must equal what the phase totals grew by, submission for // submission, and every one of them is a floor submission. #include "vk_test_device.h" +#include "../src/list_value_progress.h" #include "../src/vulkan_backend.h" #include @@ -473,6 +474,143 @@ int queue_async_and_coalescing() { std::printf("queue async (depth=4): inbox coalescing, Present barrier split, gate release ok\n"); } + // 4. Async queue progress tracking (LEONIDA_SUBMIT_ASYNC=4 + queued Present + busy producer worker + genuine + // deadlock): + // (a) While the direct producer async worker is busy inside before_execute_() with a WriteGroup Signal + // still queued in its inbox_, a consumer compute gate with a Present queued behind it must NOT be + // released unmet (queue_holds::any_progress() == true -> list_value_progress::Reason::hold). + // (b) Once all async workers finish draining into held_ and the GPU is idle, two async queues holding + // unsatisfied waits must NOT ping-pong release_ready(), queue_holds::any_progress() settles to false, + // and a genuine Present deadlock breaks cleanly. + // (c) In synchronous mode, executing_ == true (unlocked before_execute_()) also keeps any_progress() true. + { + namespace lvp = leonida::list_value_progress; + const auto met_ge = [](std::uint32_t, std::uint64_t v, std::uint64_t ref, bool) { return v >= ref; }; + + VulkanContext context(d.device_proc, d.device, true); + EXPECT(context.loaded()); + VulkanQueue direct(context, d.queue, d.family, VulkanQueue::QueueKind::direct); + VulkanQueue compute(context, d.queue, d.family, VulkanQueue::QueueKind::compute); + EXPECT(direct.ok() && compute.ok()); + direct.set_async_depth(4); + compute.set_async_depth(4); + + auto empty_list = std::make_shared(); + EXPECT(empty_list->close()); + std::shared_ptr snap = empty_list; + + std::atomic hold_before{true}; + std::atomic in_before{false}; + context.set_before_execute([&] { + if (hold_before.load(std::memory_order_acquire)) { + in_before.store(true, std::memory_order_release); + while (hold_before.load(std::memory_order_acquire)) std::this_thread::yield(); + } + }); + + VulkanFence producer_written(context, 0), consumer_gate(context, 0), consumer_done(context, 0); + EXPECT(producer_written.semaphore() && consumer_gate.semaphore() && consumer_done.semaphore()); + + // (a) Direct worker starts batch 0 and blocks inside before_execute_(). + EXPECT(direct.execute({&snap, 1}) == ReplayResult::success); + while (!in_before.load(std::memory_order_acquire)) std::this_thread::yield(); + + // Compute queue holds a batch behind consumer_gate (simulating wait64 on title memory). + compute.wait(consumer_gate.semaphore(), 1); + EXPECT(compute.execute({&snap, 1}) == ReplayResult::success); + EXPECT(compute.signal(consumer_done.semaphore(), 1)); + + // Direct queue queues the producer batch + Signal(producer_written, 1) behind the blocked batch in inbox_. + EXPECT(direct.execute({&snap, 1}) == ReplayResult::success); + EXPECT(direct.signal(producer_written.semaphore(), 1)); + + // Title queues Present (after_held) and returns to idle (title_calls == 0). + std::atomic present_ran{false}; + int present_owner = 0; + context.holds().after_held(&present_owner, [&] { present_ran.store(true, std::memory_order_release); }); + + // Wait briefly for compute's worker to move its batch into held_, while direct's worker stays busy. + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + + // Producer's Signal has NOT reached Vulkan yet (still in direct.inbox_!), so pending() is true — the exact + // condition where Pump::choose_release() sees wg.running == false and would falsely break at Present + // without QueueHolds progress tracking. + EXPECT(context.holds().pending(producer_written.semaphore(), 1)); + EXPECT(queue_holds::any_progress()); + + const lvp::WaitPredicate wait_val[] = {{0x1000, 1, 7, true}}; + const lvp::PendingWrite write_val[] = {{0x1000, 1, 8, false}}; + const lvp::PendingGate gates[] = { + {&compute, 1, false, false, wait_val, /*presents_at_hold=*/0, /*held_ms=*/0}}; + const lvp::PendingWriteGroup writes[] = {{&direct, 0, /*running=*/false, write_val}}; + lvp::Activity at_present; + at_present.presents = 1; + at_present.now_ms = 250; + at_present.title_call_ms = 10; + at_present.title_submitting = queue_holds::any_progress(); + EXPECT(lvp::decide(writes, gates, at_present, met_ge).reason == lvp::Reason::hold); + EXPECT(!present_ran.load(std::memory_order_acquire)); + + // Unblock direct's worker: producer executes and submits Signal(producer_written, 1) cleanly. + hold_before.store(false, std::memory_order_release); + EXPECT(producer_written.wait(1, 5'000'000'000ull)); + EXPECT(direct.idle()); + EXPECT(!context.holds().pending(producer_written.semaphore(), 1)); + + // Now consumer_gate is met: signaling it wakes compute's worker, which drains consumer_batch and Present. + EXPECT(consumer_gate.signal(1)); + EXPECT(consumer_done.wait(1, 5'000'000'000ull)); + EXPECT(compute.idle()); + EXPECT(present_ran.load(std::memory_order_acquire)); + + // (b) Genuine deadlock across BOTH async queues (no producer can ever signal): any_progress() must settle + // to false (zero cross-queue release_ready() ping-pong) so Present breaks the deadlock cleanly. + VulkanFence dead_gate_a(context, 0), dead_gate_b(context, 0), dead_done_a(context, 0), dead_done_b(context, 0); + EXPECT(dead_gate_a.semaphore() && dead_gate_b.semaphore() && dead_done_a.semaphore() && + dead_done_b.semaphore()); + direct.wait(dead_gate_a.semaphore(), 1); + EXPECT(direct.execute({&snap, 1}) == ReplayResult::success); + EXPECT(direct.signal(dead_done_a.semaphore(), 1)); + compute.wait(dead_gate_b.semaphore(), 1); + EXPECT(compute.execute({&snap, 1}) == ReplayResult::success); + EXPECT(compute.signal(dead_done_b.semaphore(), 1)); + + EXPECT(direct.idle()); + EXPECT(compute.idle()); + // Wait up to 500 ms for any trailing pump_ready() to finish; must settle to false and stay false. + const auto deadline = std::chrono::steady_clock::now() + std::chrono::milliseconds(500); + while (queue_holds::any_progress() && std::chrono::steady_clock::now() < deadline) + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + EXPECT(!queue_holds::any_progress()); + at_present.title_submitting = queue_holds::any_progress(); + const lvp::Decision broken = lvp::decide({}, gates, at_present, met_ge); + EXPECT(broken.reason == lvp::Reason::present && broken.gate == 0); + + // Clean teardown of held deadlock ops. + EXPECT(dead_gate_a.signal(1)); + EXPECT(dead_gate_b.signal(1)); + EXPECT(dead_done_a.wait(1, 5'000'000'000ull)); + EXPECT(dead_done_b.wait(1, 5'000'000'000ull)); + EXPECT(direct.idle() && compute.idle()); + + // (c) Synchronous executing_ == true (unlocked before_execute_()) also keeps any_progress() true. + VulkanQueue sync_q(context, d.queue, d.family, VulkanQueue::QueueKind::direct); + EXPECT(sync_q.ok()); + sync_q.set_async_depth(0); + hold_before.store(true, std::memory_order_release); + in_before.store(false, std::memory_order_release); + ReplayResult sync_res = ReplayResult::unsupported; + std::thread sync_t([&] { sync_res = sync_q.execute({&snap, 1}); }); + while (!in_before.load(std::memory_order_acquire)) std::this_thread::yield(); + EXPECT(queue_holds::any_progress()); + hold_before.store(false, std::memory_order_release); + sync_t.join(); + EXPECT(sync_res == ReplayResult::success); + EXPECT(sync_q.idle()); + EXPECT(!queue_holds::any_progress()); + std::printf("queue async progress: held gate protected during busy worker/executing_, genuine deadlock breaks ok\n"); + } + return 0; } -- cgit v1.2.3