diff options
| author | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-27 11:37:59 -0500 |
|---|---|---|
| committer | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-27 11:37:59 -0500 |
| commit | 2e00ea6c13f455cd83c7a9ef8a76d449a68606e8 (patch) | |
| tree | 8a347d2d8c4a8703312cb258a5e22507c92bcacf /runtime/test/submit_frame_test.cpp | |
| parent | 5cbd3e557b4cd708784ff18f7212d9838dd60640 (diff) | |
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).
Diffstat (limited to 'runtime/test/submit_frame_test.cpp')
| -rw-r--r-- | runtime/test/submit_frame_test.cpp | 138 |
1 files changed, 138 insertions, 0 deletions
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 @@ | |||
| 7 | // the test still passes): the floor's phase sums must equal what the phase totals grew by, submission for | 7 | // the test still passes): the floor's phase sums must equal what the phase totals grew by, submission for |
| 8 | // submission, and every one of them is a floor submission. | 8 | // submission, and every one of them is a floor submission. |
| 9 | #include "vk_test_device.h" | 9 | #include "vk_test_device.h" |
| 10 | #include "../src/list_value_progress.h" | ||
| 10 | #include "../src/vulkan_backend.h" | 11 | #include "../src/vulkan_backend.h" |
| 11 | 12 | ||
| 12 | #include <atomic> | 13 | #include <atomic> |
| @@ -473,6 +474,143 @@ int queue_async_and_coalescing() { | |||
| 473 | std::printf("queue async (depth=4): inbox coalescing, Present barrier split, gate release ok\n"); | 474 | std::printf("queue async (depth=4): inbox coalescing, Present barrier split, gate release ok\n"); |
| 474 | } | 475 | } |
| 475 | 476 | ||
| 477 | // 4. Async queue progress tracking (LEONIDA_SUBMIT_ASYNC=4 + queued Present + busy producer worker + genuine | ||
| 478 | // deadlock): | ||
| 479 | // (a) While the direct producer async worker is busy inside before_execute_() with a WriteGroup Signal | ||
| 480 | // still queued in its inbox_, a consumer compute gate with a Present queued behind it must NOT be | ||
| 481 | // released unmet (queue_holds::any_progress() == true -> list_value_progress::Reason::hold). | ||
| 482 | // (b) Once all async workers finish draining into held_ and the GPU is idle, two async queues holding | ||
| 483 | // unsatisfied waits must NOT ping-pong release_ready(), queue_holds::any_progress() settles to false, | ||
| 484 | // and a genuine Present deadlock breaks cleanly. | ||
| 485 | // (c) In synchronous mode, executing_ == true (unlocked before_execute_()) also keeps any_progress() true. | ||
| 486 | { | ||
| 487 | namespace lvp = leonida::list_value_progress; | ||
| 488 | const auto met_ge = [](std::uint32_t, std::uint64_t v, std::uint64_t ref, bool) { return v >= ref; }; | ||
| 489 | |||
| 490 | VulkanContext context(d.device_proc, d.device, true); | ||
| 491 | EXPECT(context.loaded()); | ||
| 492 | VulkanQueue direct(context, d.queue, d.family, VulkanQueue::QueueKind::direct); | ||
| 493 | VulkanQueue compute(context, d.queue, d.family, VulkanQueue::QueueKind::compute); | ||
| 494 | EXPECT(direct.ok() && compute.ok()); | ||
| 495 | direct.set_async_depth(4); | ||
| 496 | compute.set_async_depth(4); | ||
| 497 | |||
| 498 | auto empty_list = std::make_shared<CommandList>(); | ||
| 499 | EXPECT(empty_list->close()); | ||
| 500 | std::shared_ptr<const CommandList> snap = empty_list; | ||
| 501 | |||
| 502 | std::atomic<bool> hold_before{true}; | ||
| 503 | std::atomic<bool> in_before{false}; | ||
| 504 | context.set_before_execute([&] { | ||
| 505 | if (hold_before.load(std::memory_order_acquire)) { | ||
| 506 | in_before.store(true, std::memory_order_release); | ||
| 507 | while (hold_before.load(std::memory_order_acquire)) std::this_thread::yield(); | ||
| 508 | } | ||
| 509 | }); | ||
| 510 | |||
| 511 | VulkanFence producer_written(context, 0), consumer_gate(context, 0), consumer_done(context, 0); | ||
| 512 | EXPECT(producer_written.semaphore() && consumer_gate.semaphore() && consumer_done.semaphore()); | ||
| 513 | |||
| 514 | // (a) Direct worker starts batch 0 and blocks inside before_execute_(). | ||
| 515 | EXPECT(direct.execute({&snap, 1}) == ReplayResult::success); | ||
| 516 | while (!in_before.load(std::memory_order_acquire)) std::this_thread::yield(); | ||
| 517 | |||
| 518 | // Compute queue holds a batch behind consumer_gate (simulating wait64 on title memory). | ||
| 519 | compute.wait(consumer_gate.semaphore(), 1); | ||
| 520 | EXPECT(compute.execute({&snap, 1}) == ReplayResult::success); | ||
| 521 | EXPECT(compute.signal(consumer_done.semaphore(), 1)); | ||
| 522 | |||
| 523 | // Direct queue queues the producer batch + Signal(producer_written, 1) behind the blocked batch in inbox_. | ||
| 524 | EXPECT(direct.execute({&snap, 1}) == ReplayResult::success); | ||
| 525 | EXPECT(direct.signal(producer_written.semaphore(), 1)); | ||
| 526 | |||
| 527 | // Title queues Present (after_held) and returns to idle (title_calls == 0). | ||
| 528 | std::atomic<bool> present_ran{false}; | ||
| 529 | int present_owner = 0; | ||
| 530 | context.holds().after_held(&present_owner, [&] { present_ran.store(true, std::memory_order_release); }); | ||
| 531 | |||
| 532 | // Wait briefly for compute's worker to move its batch into held_, while direct's worker stays busy. | ||
| 533 | std::this_thread::sleep_for(std::chrono::milliseconds(10)); | ||
| 534 | |||
| 535 | // Producer's Signal has NOT reached Vulkan yet (still in direct.inbox_!), so pending() is true — the exact | ||
| 536 | // condition where Pump::choose_release() sees wg.running == false and would falsely break at Present | ||
| 537 | // without QueueHolds progress tracking. | ||
| 538 | EXPECT(context.holds().pending(producer_written.semaphore(), 1)); | ||
| 539 | EXPECT(queue_holds::any_progress()); | ||
| 540 | |||
| 541 | const lvp::WaitPredicate wait_val[] = {{0x1000, 1, 7, true}}; | ||
| 542 | const lvp::PendingWrite write_val[] = {{0x1000, 1, 8, false}}; | ||
| 543 | const lvp::PendingGate gates[] = { | ||
| 544 | {&compute, 1, false, false, wait_val, /*presents_at_hold=*/0, /*held_ms=*/0}}; | ||
| 545 | const lvp::PendingWriteGroup writes[] = {{&direct, 0, /*running=*/false, write_val}}; | ||
| 546 | lvp::Activity at_present; | ||
| 547 | at_present.presents = 1; | ||
| 548 | at_present.now_ms = 250; | ||
| 549 | at_present.title_call_ms = 10; | ||
| 550 | at_present.title_submitting = queue_holds::any_progress(); | ||
| 551 | EXPECT(lvp::decide(writes, gates, at_present, met_ge).reason == lvp::Reason::hold); | ||
| 552 | EXPECT(!present_ran.load(std::memory_order_acquire)); | ||
| 553 | |||
| 554 | // Unblock direct's worker: producer executes and submits Signal(producer_written, 1) cleanly. | ||
| 555 | hold_before.store(false, std::memory_order_release); | ||
| 556 | EXPECT(producer_written.wait(1, 5'000'000'000ull)); | ||
| 557 | EXPECT(direct.idle()); | ||
| 558 | EXPECT(!context.holds().pending(producer_written.semaphore(), 1)); | ||
| 559 | |||
| 560 | // Now consumer_gate is met: signaling it wakes compute's worker, which drains consumer_batch and Present. | ||
| 561 | EXPECT(consumer_gate.signal(1)); | ||
| 562 | EXPECT(consumer_done.wait(1, 5'000'000'000ull)); | ||
| 563 | EXPECT(compute.idle()); | ||
| 564 | EXPECT(present_ran.load(std::memory_order_acquire)); | ||
| 565 | |||
| 566 | // (b) Genuine deadlock across BOTH async queues (no producer can ever signal): any_progress() must settle | ||
| 567 | // to false (zero cross-queue release_ready() ping-pong) so Present breaks the deadlock cleanly. | ||
| 568 | VulkanFence dead_gate_a(context, 0), dead_gate_b(context, 0), dead_done_a(context, 0), dead_done_b(context, 0); | ||
| 569 | EXPECT(dead_gate_a.semaphore() && dead_gate_b.semaphore() && dead_done_a.semaphore() && | ||
| 570 | dead_done_b.semaphore()); | ||
| 571 | direct.wait(dead_gate_a.semaphore(), 1); | ||
| 572 | EXPECT(direct.execute({&snap, 1}) == ReplayResult::success); | ||
| 573 | EXPECT(direct.signal(dead_done_a.semaphore(), 1)); | ||
| 574 | compute.wait(dead_gate_b.semaphore(), 1); | ||
| 575 | EXPECT(compute.execute({&snap, 1}) == ReplayResult::success); | ||
| 576 | EXPECT(compute.signal(dead_done_b.semaphore(), 1)); | ||
| 577 | |||
| 578 | EXPECT(direct.idle()); | ||
| 579 | EXPECT(compute.idle()); | ||
| 580 | // Wait up to 500 ms for any trailing pump_ready() to finish; must settle to false and stay false. | ||
| 581 | const auto deadline = std::chrono::steady_clock::now() + std::chrono::milliseconds(500); | ||
| 582 | while (queue_holds::any_progress() && std::chrono::steady_clock::now() < deadline) | ||
| 583 | std::this_thread::sleep_for(std::chrono::milliseconds(1)); | ||
| 584 | EXPECT(!queue_holds::any_progress()); | ||
| 585 | at_present.title_submitting = queue_holds::any_progress(); | ||
| 586 | const lvp::Decision broken = lvp::decide({}, gates, at_present, met_ge); | ||
| 587 | EXPECT(broken.reason == lvp::Reason::present && broken.gate == 0); | ||
| 588 | |||
| 589 | // Clean teardown of held deadlock ops. | ||
| 590 | EXPECT(dead_gate_a.signal(1)); | ||
| 591 | EXPECT(dead_gate_b.signal(1)); | ||
| 592 | EXPECT(dead_done_a.wait(1, 5'000'000'000ull)); | ||
| 593 | EXPECT(dead_done_b.wait(1, 5'000'000'000ull)); | ||
| 594 | EXPECT(direct.idle() && compute.idle()); | ||
| 595 | |||
| 596 | // (c) Synchronous executing_ == true (unlocked before_execute_()) also keeps any_progress() true. | ||
| 597 | VulkanQueue sync_q(context, d.queue, d.family, VulkanQueue::QueueKind::direct); | ||
| 598 | EXPECT(sync_q.ok()); | ||
| 599 | sync_q.set_async_depth(0); | ||
| 600 | hold_before.store(true, std::memory_order_release); | ||
| 601 | in_before.store(false, std::memory_order_release); | ||
| 602 | ReplayResult sync_res = ReplayResult::unsupported; | ||
| 603 | std::thread sync_t([&] { sync_res = sync_q.execute({&snap, 1}); }); | ||
| 604 | while (!in_before.load(std::memory_order_acquire)) std::this_thread::yield(); | ||
| 605 | EXPECT(queue_holds::any_progress()); | ||
| 606 | hold_before.store(false, std::memory_order_release); | ||
| 607 | sync_t.join(); | ||
| 608 | EXPECT(sync_res == ReplayResult::success); | ||
| 609 | EXPECT(sync_q.idle()); | ||
| 610 | EXPECT(!queue_holds::any_progress()); | ||
| 611 | std::printf("queue async progress: held gate protected during busy worker/executing_, genuine deadlock breaks ok\n"); | ||
| 612 | } | ||
| 613 | |||
| 476 | return 0; | 614 | return 0; |
| 477 | } | 615 | } |
| 478 | 616 | ||