diff --git a/runtime/shims/d3d12_com.cpp b/runtime/shims/d3d12_com.cpp index 4a61ffadbc..12b646c3ca 100644 --- a/runtime/shims/d3d12_com.cpp +++ b/runtime/shims/d3d12_com.cpp @@ -2281,6 +2281,7 @@ class Fence final VkSemaphoreSignalInfo info{VK_STRUCTURE_TYPE_SEMAPHORE_SIGNAL_INFO}; info.semaphore = semaphore; info.value = value; + context->holds().asking(semaphore, value); // before the call, as VulkanFence::signal signaled = context->observe_result(context->vk.vkSignalSemaphore(context->device(), &info), "vkSignalSemaphore (fence callback)") == VK_SUCCESS; } diff --git a/runtime/src/queue_holds.h b/runtime/src/queue_holds.h index 40cc331170..d34f833ed2 100644 --- a/runtime/src/queue_holds.h +++ b/runtime/src/queue_holds.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include #include @@ -62,6 +63,7 @@ class QueueHolds { { std::lock_guard lock(mutex_); fences_.erase(semaphore); + asked_.erase(semaphore); } if (held_.load(std::memory_order_acquire) == 0 || in_pump()) return; { @@ -100,6 +102,25 @@ class QueueHolds { return it == fences_.end() ? 0 : it->second; } + // A Signal of `value` is about to reach Vulkan (vkSignalSemaphore or a vkQueueSubmit that signals the fence). + // Called before the Vulkan call, so a counter read on another thread that already sees the value also finds it + // here, which signaled() records only afterwards. Kept even when the call then fails: asked_value() only answers + // whether a counter value can have come from a Signal. + void asking(Semaphore semaphore, std::uint64_t value) { + std::lock_guard lock(mutex_); + std::uint64_t& asked = asked_[semaphore]; + if (asked < value) asked = value; + } + + // The highest value anyone asked of the fence: its initial value, a Signal recorded by signaled(), or one + // announced by asking() whose Vulkan call may still be in flight. 0 for an unknown semaphore. + std::uint64_t asked_value(Semaphore semaphore) const { + std::lock_guard lock(mutex_); + const auto it = fences_.find(semaphore); + const auto at = asked_.find(semaphore); + return std::max(it == fences_.end() ? 0 : it->second, at == asked_.end() ? 0 : at->second); + } + // true when no submitted Signal reaches `value` yet. Unknown semaphores are satisfied. bool pending(Semaphore semaphore, std::uint64_t value) const { std::lock_guard lock(mutex_); @@ -228,9 +249,10 @@ class QueueHolds { } } - mutable std::mutex mutex_; // leaf: fences_, holders_ and barriers_ + mutable std::mutex mutex_; // leaf: fences_, asked_, holders_ and barriers_ std::mutex pump_mutex_; // one releasing thread; held across release_ready(), so detach() waits for it std::unordered_map fences_; + std::unordered_map asked_; // asking(): Signals announced before their Vulkan call std::vector holders_; std::deque barriers_; std::atomic held_{0}; diff --git a/runtime/src/vulkan_backend.h b/runtime/src/vulkan_backend.h index 9fdb296405..7a1befc9e6 100644 --- a/runtime/src/vulkan_backend.h +++ b/runtime/src/vulkan_backend.h @@ -2492,18 +2492,24 @@ class VulkanFence { if (value == UINT64_MAX) { // A lost device can show itself here first: measured on NVIDIA 621.24 and reported on 617.14, the counter // query succeeds with UINT64_MAX (and vkWaitSemaphores with a zero timeout succeeds too) before any call - // has returned VK_ERROR_DEVICE_LOST. vkDeviceWaitIdle is what then says so, and it is also what a driver - // needs to have returned before vkGetDeviceFaultInfoEXT answers (LEONIDA_DEVICE_FAULT=1). When - // vkDeviceWaitIdle succeeds, nothing is recorded and the value is returned as the driver gave it. - VkResult idle = VK_SUCCESS; - { + // has returned VK_ERROR_DEVICE_LOST. The fence's record says whether anyone asked for that value: its + // initial value, a CPU Signal or a submitted one, including one whose Vulkan call is still returning + // (QueueHolds::asked_value: asking() runs before the call). Asked for, the reading is the value and + // nothing is recorded. Not asked for, no Signal can have produced it, so the device is lost. + // That is decided without vkDeviceWaitIdle first: under the submit lock it can block for minutes on such + // a device (measured 270 s on NVIDIA 616.56), and meanwhile the releaser cannot submit the held batch a + // queue's Wait may be waiting for, so the title froze and the loss was reported minutes late. + if (context_.holds().asked_value(semaphore_) == UINT64_MAX) return value; + const bool first = !context_.is_device_lost(); + context_.mark_device_lost("a fence's counter read UINT64_MAX, a value no Signal asked for"); + if (first && context_.device_fault()) { + // With the fault diagnostics on, vkGetDeviceFaultInfoEXT answered on NVIDIA 621.24 only once a call + // had returned VK_ERROR_DEVICE_LOST; vkDeviceWaitIdle is that call when nothing submits afterwards. + // The context is lost already, so a long wait here stalls only a diagnostic run. std::lock_guard lock(context_.submit_mutex()); - idle = context_.observe_result(context_.vk.vkDeviceWaitIdle(context_.device()), - "vkDeviceWaitIdle (a fence counter of UINT64_MAX)"); + context_.observe_result(context_.vk.vkDeviceWaitIdle(context_.device()), + "vkDeviceWaitIdle (a fence counter of UINT64_MAX)"); } - if (idle == VK_ERROR_DEVICE_LOST) - context_.mark_device_lost( - "a fence's counter read UINT64_MAX and vkDeviceWaitIdle then returned VK_ERROR_DEVICE_LOST"); } return value; } @@ -2522,6 +2528,7 @@ class VulkanFence { VkSemaphoreSignalInfo info{VK_STRUCTURE_TYPE_SEMAPHORE_SIGNAL_INFO}; info.semaphore = semaphore_; info.value = value; + context_.holds().asking(semaphore_, value); // before the call: a concurrent completed() may see the value const VkResult result = context_.observe_result(context_.vk.vkSignalSemaphore(context_.device(), &info), "vkSignalSemaphore"); if (result == VK_ERROR_DEVICE_LOST) context_.mark_device_lost(); @@ -4473,6 +4480,7 @@ class VulkanQueue : private QueueHolds::Holder { } ring_at = ring->begin_submit(record); } + if (signals_fence) context_.holds().asking(fence, value); // before the call, as VulkanFence::signal res = context_.observe_result(context_.queue_submit(queue_, 1, &submit, context_.gpu_labels_.submit_fence(command_buffer)), "vkQueueSubmit"); context_.gpu_labels_.submitted(command_buffer, res == VK_SUCCESS); diff --git a/runtime/test/vk_fence_lost_by_counter_test.cpp b/runtime/test/vk_fence_lost_by_counter_test.cpp index ceb154b3a3..5597f53334 100644 --- a/runtime/test/vk_fence_lost_by_counter_test.cpp +++ b/runtime/test/vk_fence_lost_by_counter_test.cpp @@ -1,9 +1,13 @@ // A device loss the driver shows only through a timeline counter (runtime/src/vulkan_backend.h, VulkanFence::completed): // vkGetSemaphoreCounterValue succeeds with UINT64_MAX and no call has returned VK_ERROR_DEVICE_LOST yet. -// 1. With vkDeviceWaitIdle then returning VK_ERROR_DEVICE_LOST: the context is marked lost and the loss is said once. +// 1. UINT64_MAX on a fence no Signal asked that value of: the context is marked lost and the loss is said once, +// without vkDeviceWaitIdle (which can block for minutes under the submit lock on such a device). // 2. A second read on the lost device asks the driver nothing and says nothing more. -// 3. With vkDeviceWaitIdle returning VK_SUCCESS, the value alone records no loss and nothing is said. +// 3. UINT64_MAX on a fence a Signal asked that value of is the value: no loss, nothing said, no vkDeviceWaitIdle. // 4. An ordinary counter value never reaches vkDeviceWaitIdle. +// 5. The reading does not take the submit lock: a thread holding it (the releaser, mid-submission) cannot stall it. +// 6. A Signal(UINT64_MAX) announced with QueueHolds::asking but not yet recorded (its Vulkan call still returning +// on another thread) makes the reading the value, not a loss. // The driver functions are interposed on a real device's table. Skips with exit code 77 when no Vulkan 1.3 device is // available. No game data. #include "vk_test_device.h" @@ -14,6 +18,7 @@ #include #include #include +#include #define EXPECT(condition) do { \ if (!(condition)) { std::fprintf(stderr, "line %d: %s\n", __LINE__, #condition); std::_Exit(1); } \ @@ -70,7 +75,7 @@ int main() { } driver::install(d.device_proc, d.device); - { // 4, then 3: an ordinary value, then UINT64_MAX on a device that is not lost + { // 4, then 3: an ordinary value, then UINT64_MAX on a fence a Signal asked that value of VulkanContext context(driver::get_device, d.device, true); EXPECT(context.loaded()); VulkanFence fence(context, 7); @@ -78,37 +83,70 @@ int main() { EXPECT(fence.completed() == 7); EXPECT(driver::idle_calls.load() == 0); + context.holds().signaled(fence.semaphore(), UINT64_MAX); // what a queue's Signal(UINT64_MAX) records driver::counter_reads_max.store(true, std::memory_order_release); - EXPECT(fence.completed() == UINT64_MAX); // what the driver said; the layer adds nothing to it + EXPECT(fence.completed() == UINT64_MAX); // the value asked for; the layer adds nothing to it driver::counter_reads_max.store(false, std::memory_order_release); - EXPECT(driver::idle_calls.load() == 1); + EXPECT(driver::idle_calls.load() == 0); EXPECT(!context.is_device_lost()); EXPECT(device_lost_reports.load() == lost_before); - EXPECT(fence.completed() == 7); } - { // 1 and 2 + { // 1 and 2: UINT64_MAX that no Signal asked for is a loss, decided without vkDeviceWaitIdle VulkanContext context(driver::get_device, d.device, true); EXPECT(context.loaded()); VulkanFence fence(context, 0); const unsigned lost_before = device_lost_reports.load(); - const unsigned idle_before = driver::idle_calls.load(); + context.holds().signaled(fence.semaphore(), 5); // a Signal below UINT64_MAX was submitted driver::counter_reads_max.store(true, std::memory_order_release); driver::idle_reports_loss.store(true, std::memory_order_release); EXPECT(fence.completed() == UINT64_MAX); EXPECT(context.is_device_lost()); - EXPECT(driver::idle_calls.load() == idle_before + 1); + EXPECT(driver::idle_calls.load() == 0); EXPECT(device_lost_reports.load() == lost_before + 1); const unsigned counter_before = driver::counter_calls.load(); EXPECT(fence.completed() == UINT64_MAX); // lost: answered without asking the driver again context.mark_device_lost(); EXPECT(driver::counter_calls.load() == counter_before); - EXPECT(driver::idle_calls.load() == idle_before + 1); + EXPECT(driver::idle_calls.load() == 0); EXPECT(device_lost_reports.load() == lost_before + 1); driver::counter_reads_max.store(false, std::memory_order_release); driver::idle_reports_loss.store(false, std::memory_order_release); } + { // 5: with the submit lock held by another thread, the reading still answers and marks the loss + VulkanContext context(driver::get_device, d.device, true); + EXPECT(context.loaded()); + VulkanFence fence(context, 3); + std::atomic locked{false}, release{false}; + std::thread holder([&] { + std::lock_guard lock(context.submit_mutex()); + locked.store(true, std::memory_order_release); + while (!release.load(std::memory_order_acquire)) std::this_thread::yield(); + }); + while (!locked.load(std::memory_order_acquire)) std::this_thread::yield(); + driver::counter_reads_max.store(true, std::memory_order_release); + EXPECT(fence.completed() == UINT64_MAX); + driver::counter_reads_max.store(false, std::memory_order_release); + EXPECT(context.is_device_lost()); + EXPECT(driver::idle_calls.load() == 0); + release.store(true, std::memory_order_release); + holder.join(); + } + { // 6: a Signal(UINT64_MAX) in flight: announced before its Vulkan call, not recorded yet + VulkanContext context(driver::get_device, d.device, true); + EXPECT(context.loaded()); + VulkanFence fence(context, UINT64_MAX - 1); + const unsigned lost_before = device_lost_reports.load(); + context.holds().asking(fence.semaphore(), UINT64_MAX); + EXPECT(context.holds().submitted_value(fence.semaphore()) == UINT64_MAX - 1); // signaled() not called yet + driver::counter_reads_max.store(true, std::memory_order_release); + EXPECT(fence.completed() == UINT64_MAX); + driver::counter_reads_max.store(false, std::memory_order_release); + EXPECT(!context.is_device_lost()); + EXPECT(device_lost_reports.load() == lost_before); + EXPECT(driver::idle_calls.load() == 0); + } std::puts("vk_fence_lost_by_counter: ok"); return 0; }