diff options
| author | quartz-heron-58 <245169279+quartz-heron-58@users.noreply.github.com> | 2026-11-25 00:42:48 +0800 |
|---|---|---|
| committer | quartz-heron-58 <245169279+quartz-heron-58@users.noreply.github.com> | 2026-11-25 00:42:48 +0800 |
| commit | da74b96810eba31eb15d1cfd576fe4c69f159912 (patch) | |
| tree | 421a3c01b4205212d28a02e5ebb812d1b2d22d01 /runtime/src | |
| parent | 5c1f77e1d4bafb3cba8c3e2037de28cb376f633e (diff) | |
vulkan: print the driver's fault report when the device is lost (RT-DEVICE-FAULT-INFO)
Where the device has VK_EXT_device_fault, enable it and, the first time the
device is lost, query vkGetDeviceFaultInfoEXT and print the description, the
address faults and the vendor faults after the "device was lost" line.
Nothing is printed and nothing changes without the extension.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Diffstat (limited to 'runtime/src')
| -rw-r--r-- | runtime/src/vulkan_backend.h | 30 | ||||
| -rw-r--r-- | runtime/src/vulkan_device_fault.h | 106 |
2 files changed, 131 insertions, 5 deletions
diff --git a/runtime/src/vulkan_backend.h b/runtime/src/vulkan_backend.h index dcf89476c..7f0723483 100644 --- a/runtime/src/vulkan_backend.h +++ b/runtime/src/vulkan_backend.h | |||
| @@ -17,6 +17,7 @@ | |||
| 17 | #include "statistics_sum_spirv.h" | 17 | #include "statistics_sum_spirv.h" |
| 18 | #include "vulkan13_features.h" | 18 | #include "vulkan13_features.h" |
| 19 | #include "vulkan_depth24.h" | 19 | #include "vulkan_depth24.h" |
| 20 | #include "vulkan_device_fault.h" | ||
| 20 | #include "vulkan_indirect_dispatch_count.h" | 21 | #include "vulkan_indirect_dispatch_count.h" |
| 21 | #include "vulkan_scratch.h" | 22 | #include "vulkan_scratch.h" |
| 22 | #include "vulkan_replay.h" | 23 | #include "vulkan_replay.h" |
| @@ -107,7 +108,7 @@ struct VulkanDeviceFunctions { | |||
| 107 | X(vkCreatePipelineCache) X(vkDestroyPipelineCache) \ | 108 | X(vkCreatePipelineCache) X(vkDestroyPipelineCache) \ |
| 108 | X(vkCreateEvent) X(vkDestroyEvent) X(vkResetEvent) X(vkCmdSetEvent) X(vkCmdWaitEvents) X(vkCmdDrawMeshTasksEXT) \ | 109 | X(vkCreateEvent) X(vkDestroyEvent) X(vkResetEvent) X(vkCmdSetEvent) X(vkCmdWaitEvents) X(vkCmdDrawMeshTasksEXT) \ |
| 109 | X(vkCreateRenderPass2) X(vkDestroyRenderPass) X(vkCreateFramebuffer) X(vkDestroyFramebuffer) \ | 110 | X(vkCreateRenderPass2) X(vkDestroyRenderPass) X(vkCreateFramebuffer) X(vkDestroyFramebuffer) \ |
| 110 | X(vkCmdBeginRenderPass) X(vkCmdEndRenderPass) | 111 | X(vkCmdBeginRenderPass) X(vkCmdEndRenderPass) X(vkGetDeviceFaultInfoEXT) |
| 111 | #define LEONIDA_VK_MEMBER(name) PFN_##name name = nullptr; | 112 | #define LEONIDA_VK_MEMBER(name) PFN_##name name = nullptr; |
| 112 | LEONIDA_VK_DEVICE_FUNCTIONS(LEONIDA_VK_MEMBER) | 113 | LEONIDA_VK_DEVICE_FUNCTIONS(LEONIDA_VK_MEMBER) |
| 113 | #undef LEONIDA_VK_MEMBER | 114 | #undef LEONIDA_VK_MEMBER |
| @@ -565,6 +566,22 @@ inline bool set_device_lost(std::atomic<bool>& flag, const char* where) noexcept | |||
| 565 | return true; | 566 | return true; |
| 566 | } | 567 | } |
| 567 | 568 | ||
| 569 | // What the driver says about the loss, printed after the line above by the caller whose set_device_lost returned true, | ||
| 570 | // so once per device. Only where VK_EXT_device_fault is enabled: the entry point is null otherwise and nothing is | ||
| 571 | // printed (src/vulkan_device_fault.h). device_fault_reports counts the reports printed in this process (for tests). | ||
| 572 | inline std::atomic<unsigned> device_fault_reports{0}; | ||
| 573 | inline void report_device_fault(const VulkanDeviceFunctions& vk, VkDevice device) noexcept { | ||
| 574 | if (!vk.vkGetDeviceFaultInfoEXT || !device) return; | ||
| 575 | try { | ||
| 576 | const auto lines = device_fault_lines([&](VkDeviceFaultCountsEXT* counts, VkDeviceFaultInfoEXT* info) { | ||
| 577 | return vk.vkGetDeviceFaultInfoEXT(device, counts, info); | ||
| 578 | }); | ||
| 579 | for (const std::string& line : lines) std::fprintf(stderr, "[rt] vulkan: %s\n", line.c_str()); | ||
| 580 | device_fault_reports.fetch_add(1, std::memory_order_relaxed); | ||
| 581 | } catch (...) { // out of memory while the device is going down: the loss itself is already said | ||
| 582 | } | ||
| 583 | } | ||
| 584 | |||
| 568 | // Centralized single-thread fence dispatcher using Vulkan 1.2 vkWaitSemaphores(VK_SEMAPHORE_WAIT_ANY_BIT). | 585 | // Centralized single-thread fence dispatcher using Vulkan 1.2 vkWaitSemaphores(VK_SEMAPHORE_WAIT_ANY_BIT). |
| 569 | // Eliminates per-fence OS threads and 10ms polling: a synthetic timeline semaphore (wakeup_sem_) at index 0 | 586 | // Eliminates per-fence OS threads and 10ms polling: a synthetic timeline semaphore (wakeup_sem_) at index 0 |
| 570 | // wakes vkWaitSemaphores immediately whenever a new wait, a released fence, or shutdown arrives while waiting_ == true. | 587 | // wakes vkWaitSemaphores immediately whenever a new wait, a released fence, or shutdown arrives while waiting_ == true. |
| @@ -837,7 +854,9 @@ private: | |||
| 837 | epoch_cv_.notify_all(); | 854 | epoch_cv_.notify_all(); |
| 838 | 855 | ||
| 839 | if (res == VK_ERROR_DEVICE_LOST || device_lost_.load(std::memory_order_acquire)) { | 856 | if (res == VK_ERROR_DEVICE_LOST || device_lost_.load(std::memory_order_acquire)) { |
| 840 | set_device_lost(device_lost_, "vkWaitSemaphores in the fence dispatcher returned VK_ERROR_DEVICE_LOST"); | 857 | if (set_device_lost(device_lost_, |
| 858 | "vkWaitSemaphores in the fence dispatcher returned VK_ERROR_DEVICE_LOST")) | ||
| 859 | report_device_fault(vk_, device_); | ||
| 841 | std::vector<Entry> drained = std::move(pending_); | 860 | std::vector<Entry> drained = std::move(pending_); |
| 842 | pending_.clear(); | 861 | pending_.clear(); |
| 843 | dispatch_outside_lock(lock, drained, signal_event_); | 862 | dispatch_outside_lock(lock, drained, signal_event_); |
| @@ -867,8 +886,9 @@ private: | |||
| 867 | pending_.resize(write); | 886 | pending_.resize(write); |
| 868 | for (Entry& e : pending_) ready.push_back(std::move(e)); | 887 | for (Entry& e : pending_) ready.push_back(std::move(e)); |
| 869 | pending_.clear(); | 888 | pending_.clear(); |
| 870 | set_device_lost(device_lost_, | 889 | if (set_device_lost(device_lost_, |
| 871 | "vkGetSemaphoreCounterValue in the fence dispatcher returned VK_ERROR_DEVICE_LOST"); | 890 | "vkGetSemaphoreCounterValue in the fence dispatcher returned VK_ERROR_DEVICE_LOST")) |
| 891 | report_device_fault(vk_, device_); | ||
| 872 | dispatch_outside_lock(lock, ready, signal_event_); | 892 | dispatch_outside_lock(lock, ready, signal_event_); |
| 873 | return; | 893 | return; |
| 874 | } | 894 | } |
| @@ -1556,7 +1576,7 @@ public: | |||
| 1556 | std::mutex& submit_mutex() { return submit_mutex_; } | 1576 | std::mutex& submit_mutex() { return submit_mutex_; } |
| 1557 | 1577 | ||
| 1558 | void mark_device_lost(const char* where = "a Vulkan call returned VK_ERROR_DEVICE_LOST") noexcept { | 1578 | void mark_device_lost(const char* where = "a Vulkan call returned VK_ERROR_DEVICE_LOST") noexcept { |
| 1559 | set_device_lost(device_lost_, where); | 1579 | if (set_device_lost(device_lost_, where)) report_device_fault(vk, device_); |
| 1560 | if (fence_dispatcher_) { | 1580 | if (fence_dispatcher_) { |
| 1561 | try { | 1581 | try { |
| 1562 | fence_dispatcher_->notify_device_lost(); | 1582 | fence_dispatcher_->notify_device_lost(); |
diff --git a/runtime/src/vulkan_device_fault.h b/runtime/src/vulkan_device_fault.h new file mode 100644 index 000000000..297280b83 --- /dev/null +++ b/runtime/src/vulkan_device_fault.h | |||
| @@ -0,0 +1,106 @@ | |||
| 1 | // Why a lost Vulkan device was lost, as the driver tells it through VK_EXT_device_fault. | ||
| 2 | // | ||
| 3 | // A lost device says only VK_ERROR_DEVICE_LOST. With the extension enabled the driver also keeps a short report: a | ||
| 4 | // description, the addresses of the faults it saw (a shader's read or write of memory that is not there, for one) and | ||
| 5 | // its own vendor codes. device_fault_lines() turns that report into the lines the layer prints after its "the device | ||
| 6 | // was lost" line (vulkan_backend.h, set_device_lost). It takes the query as a callable so a test can stand in for the | ||
| 7 | // driver; the layer passes vkGetDeviceFaultInfoEXT bound to its device. | ||
| 8 | // | ||
| 9 | // The vendor binary (a crash dump for the vendor's own tools) is not asked for: its size is set to 0 in the second | ||
| 10 | // call, and the deviceFaultVendorBinary feature is left off at device creation. | ||
| 11 | #pragma once | ||
| 12 | |||
| 13 | #include <vulkan/vulkan.h> | ||
| 14 | |||
| 15 | #include <algorithm> | ||
| 16 | #include <cstdint> | ||
| 17 | #include <cstdio> | ||
| 18 | #include <string> | ||
| 19 | #include <vector> | ||
| 20 | |||
| 21 | namespace leonida { | ||
| 22 | |||
| 23 | #ifdef VK_EXT_device_fault | ||
| 24 | |||
| 25 | // More entries than this are not printed; the line that follows the last one says how many were left out. | ||
| 26 | constexpr std::uint32_t kDeviceFaultPrintLimit = 16; | ||
| 27 | |||
| 28 | inline const char* device_fault_address_type(VkDeviceFaultAddressTypeEXT type) { | ||
| 29 | switch (type) { | ||
| 30 | case VK_DEVICE_FAULT_ADDRESS_TYPE_NONE_EXT: return "no address"; | ||
| 31 | case VK_DEVICE_FAULT_ADDRESS_TYPE_READ_INVALID_EXT: return "read of an invalid address"; | ||
| 32 | case VK_DEVICE_FAULT_ADDRESS_TYPE_WRITE_INVALID_EXT: return "write to an invalid address"; | ||
| 33 | case VK_DEVICE_FAULT_ADDRESS_TYPE_EXECUTE_INVALID_EXT: return "execution of an invalid address"; | ||
| 34 | case VK_DEVICE_FAULT_ADDRESS_TYPE_INSTRUCTION_POINTER_UNKNOWN_EXT: return "instruction pointer, cause unknown"; | ||
| 35 | case VK_DEVICE_FAULT_ADDRESS_TYPE_INSTRUCTION_POINTER_INVALID_EXT: return "invalid instruction pointer"; | ||
| 36 | case VK_DEVICE_FAULT_ADDRESS_TYPE_INSTRUCTION_POINTER_FAULT_EXT: return "instruction pointer of a fault"; | ||
| 37 | default: return "address fault of an unknown type"; | ||
| 38 | } | ||
| 39 | } | ||
| 40 | |||
| 41 | // The lines of a driver's fault report, without a prefix or a newline. `query(counts, info)` is | ||
| 42 | // vkGetDeviceFaultInfoEXT for one device: called once with a null info for the counts and once more for the report. | ||
| 43 | // Always at least one line: a query that fails, or a report with nothing in it, is said as such. | ||
| 44 | template <typename Query> | ||
| 45 | std::vector<std::string> device_fault_lines(Query&& query) { | ||
| 46 | std::vector<std::string> lines; | ||
| 47 | char text[512]; | ||
| 48 | VkDeviceFaultCountsEXT counts{VK_STRUCTURE_TYPE_DEVICE_FAULT_COUNTS_EXT}; | ||
| 49 | VkResult result = query(&counts, nullptr); | ||
| 50 | if (result != VK_SUCCESS) { | ||
| 51 | std::snprintf(text, sizeof text, "the driver gave no fault report (vkGetDeviceFaultInfoEXT returned %d)", | ||
| 52 | static_cast<int>(result)); | ||
| 53 | lines.emplace_back(text); | ||
| 54 | return lines; | ||
| 55 | } | ||
| 56 | const std::uint32_t reported_addresses = counts.addressInfoCount, reported_vendors = counts.vendorInfoCount; | ||
| 57 | counts.addressInfoCount = std::min(reported_addresses, kDeviceFaultPrintLimit); | ||
| 58 | counts.vendorInfoCount = std::min(reported_vendors, kDeviceFaultPrintLimit); | ||
| 59 | counts.vendorBinarySize = 0; | ||
| 60 | std::vector<VkDeviceFaultAddressInfoEXT> addresses(counts.addressInfoCount); | ||
| 61 | std::vector<VkDeviceFaultVendorInfoEXT> vendors(counts.vendorInfoCount); | ||
| 62 | VkDeviceFaultInfoEXT info{VK_STRUCTURE_TYPE_DEVICE_FAULT_INFO_EXT}; | ||
| 63 | info.pAddressInfos = addresses.empty() ? nullptr : addresses.data(); | ||
| 64 | info.pVendorInfos = vendors.empty() ? nullptr : vendors.data(); | ||
| 65 | result = query(&counts, &info); | ||
| 66 | if (result != VK_SUCCESS && result != VK_INCOMPLETE) { // INCOMPLETE: more than was asked for, by design here | ||
| 67 | std::snprintf(text, sizeof text, "the driver gave no fault report (vkGetDeviceFaultInfoEXT returned %d)", | ||
| 68 | static_cast<int>(result)); | ||
| 69 | lines.emplace_back(text); | ||
| 70 | return lines; | ||
| 71 | } | ||
| 72 | // The driver writes back how many entries it filled; never read past what was allocated. | ||
| 73 | const std::uint32_t address_count = std::min<std::uint32_t>(counts.addressInfoCount, std::uint32_t(addresses.size())); | ||
| 74 | const std::uint32_t vendor_count = std::min<std::uint32_t>(counts.vendorInfoCount, std::uint32_t(vendors.size())); | ||
| 75 | info.description[VK_MAX_DESCRIPTION_SIZE - 1] = '\0'; | ||
| 76 | std::snprintf(text, sizeof text, "the driver's fault report (VK_EXT_device_fault): %s", | ||
| 77 | info.description[0] ? info.description : "no description"); | ||
| 78 | lines.emplace_back(text); | ||
| 79 | for (std::uint32_t i = 0; i < address_count; ++i) { | ||
| 80 | // The fault is somewhere in [reportedAddress & ~(precision - 1), that + precision): precision is a power of two. | ||
| 81 | std::snprintf(text, sizeof text, " address fault %u: %s, address 0x%llx, precision 0x%llx", i, | ||
| 82 | device_fault_address_type(addresses[i].addressType), | ||
| 83 | static_cast<unsigned long long>(addresses[i].reportedAddress), | ||
| 84 | static_cast<unsigned long long>(addresses[i].addressPrecision)); | ||
| 85 | lines.emplace_back(text); | ||
| 86 | } | ||
| 87 | for (std::uint32_t i = 0; i < vendor_count; ++i) { | ||
| 88 | vendors[i].description[VK_MAX_DESCRIPTION_SIZE - 1] = '\0'; | ||
| 89 | std::snprintf(text, sizeof text, " vendor fault %u: %s, code 0x%llx, data 0x%llx", i, | ||
| 90 | vendors[i].description[0] ? vendors[i].description : "no description", | ||
| 91 | static_cast<unsigned long long>(vendors[i].vendorFaultCode), | ||
| 92 | static_cast<unsigned long long>(vendors[i].vendorFaultData)); | ||
| 93 | lines.emplace_back(text); | ||
| 94 | } | ||
| 95 | if (!address_count && !vendor_count) lines.emplace_back(" the report names no address and no vendor fault"); | ||
| 96 | if (reported_addresses > address_count || reported_vendors > vendor_count) { | ||
| 97 | std::snprintf(text, sizeof text, " not printed: %u more address fault(s), %u more vendor fault(s)", | ||
| 98 | reported_addresses - address_count, reported_vendors - vendor_count); | ||
| 99 | lines.emplace_back(text); | ||
| 100 | } | ||
| 101 | return lines; | ||
| 102 | } | ||
| 103 | |||
| 104 | #endif // VK_EXT_device_fault | ||
| 105 | |||
| 106 | } // namespace leonida | ||