diff options
Diffstat (limited to 'runtime/src')
| -rw-r--r-- | runtime/src/address_binding.h | 303 | ||||
| -rw-r--r-- | runtime/src/vulkan_device_fault.h | 21 |
2 files changed, 324 insertions, 0 deletions
diff --git a/runtime/src/address_binding.h b/runtime/src/address_binding.h new file mode 100644 index 000000000..b8c54e18b --- /dev/null +++ b/runtime/src/address_binding.h | |||
| @@ -0,0 +1,303 @@ | |||
| 1 | #pragma once | ||
| 2 | // LEONIDA_ADDRESS_BINDINGS=1, with LEONIDA_DEVICE_FAULT=1: names what the driver had bound at a device-fault address | ||
| 3 | // (RT-FAULT-ADDRESS-OWNER). VK_EXT_device_address_binding_report has the driver report the GPU virtual address range | ||
| 4 | // of every buffer, image and allocation as it is bound and unbound, to the instance's debug messengers. This keeps | ||
| 5 | // the live ranges and the most recent unbound ones, so the device-fault report (vulkan_device_fault.h) can say which | ||
| 6 | // object contains a faulting address, which objects end right below it and start right above it, and which objects | ||
| 7 | // held it before. An address that none of them covers is a GPU address no object of this device was ever given. | ||
| 8 | // | ||
| 9 | // The record and the report are pure (record_binding, report); the Vulkan side is install_messenger before | ||
| 10 | // vkCreateDevice's bindings start and enable_device on the device's create chain. | ||
| 11 | |||
| 12 | #include <vulkan/vulkan.h> | ||
| 13 | |||
| 14 | #include <algorithm> | ||
| 15 | #include <atomic> | ||
| 16 | #include <chrono> | ||
| 17 | #include <cinttypes> | ||
| 18 | #include <cstdint> | ||
| 19 | #include <cstdio> | ||
| 20 | #include <cstdlib> | ||
| 21 | #include <cstring> | ||
| 22 | #include <deque> | ||
| 23 | #include <map> | ||
| 24 | #include <mutex> | ||
| 25 | #include <span> | ||
| 26 | #include <string> | ||
| 27 | #include <vector> | ||
| 28 | |||
| 29 | namespace leonida::address_binding { | ||
| 30 | |||
| 31 | struct Entry { | ||
| 32 | std::uint64_t base = 0, size = 0; | ||
| 33 | VkObjectType type = VK_OBJECT_TYPE_UNKNOWN; | ||
| 34 | std::uint64_t handle = 0; | ||
| 35 | bool internal = false; // VK_DEVICE_ADDRESS_BINDING_INTERNAL_OBJECT_BIT_EXT | ||
| 36 | double bound_at = 0, unbound_at = 0; // seconds since the record started; unbound_at 0 while live | ||
| 37 | }; | ||
| 38 | |||
| 39 | class Record { | ||
| 40 | public: | ||
| 41 | static constexpr std::size_t kept_unbound = 16384; | ||
| 42 | |||
| 43 | // One report: bound, or unbound (matched to its live entry by base, handle and size). | ||
| 44 | void record(Entry e, bool bind, double now) { | ||
| 45 | std::lock_guard lock(mutex_); | ||
| 46 | if (bind) { | ||
| 47 | e.bound_at = now; | ||
| 48 | live_.emplace(e.base, e); | ||
| 49 | ++binds_; | ||
| 50 | return; | ||
| 51 | } | ||
| 52 | ++unbinds_; | ||
| 53 | auto [first, last] = live_.equal_range(e.base); | ||
| 54 | for (auto it = first; it != last; ++it) { | ||
| 55 | if (it->second.handle != e.handle || it->second.size != e.size) continue; | ||
| 56 | e = it->second; | ||
| 57 | live_.erase(it); | ||
| 58 | break; | ||
| 59 | } | ||
| 60 | e.unbound_at = now; | ||
| 61 | unbound_.push_back(e); | ||
| 62 | if (unbound_.size() > kept_unbound) unbound_.pop_front(); | ||
| 63 | } | ||
| 64 | |||
| 65 | // What was bound at and around the `precision`-sized page of `address`, one line per finding. | ||
| 66 | std::string report(std::uint64_t address, std::uint64_t precision) const { | ||
| 67 | std::lock_guard lock(mutex_); | ||
| 68 | std::string out; | ||
| 69 | char line[160]; | ||
| 70 | std::snprintf(line, sizeof line, "0x%" PRIx64 " (precision 0x%" PRIx64 "): %" PRIu64 " binds, %" PRIu64 | ||
| 71 | " unbinds, %zu live\n", | ||
| 72 | address, precision, binds_, unbinds_, live_.size()); | ||
| 73 | out += line; | ||
| 74 | const std::uint64_t lo = precision > 1 ? address & ~(precision - 1) : address; | ||
| 75 | const std::uint64_t hi = lo + (precision > 1 ? precision : 1); | ||
| 76 | const auto overlaps = [&](const Entry& e) { return e.base < hi && e.base + e.size > lo; }; | ||
| 77 | int containing = 0; | ||
| 78 | const Entry* below = nullptr; | ||
| 79 | const Entry* above = nullptr; | ||
| 80 | // "containing" only for an object the address itself is in; one that shares only the reported page with it is | ||
| 81 | // "on the page", since the driver gives the fault address no closer than `precision`. | ||
| 82 | const auto inside = [&](const Entry& e) { return address >= e.base && address - e.base < e.size; }; | ||
| 83 | for (const auto& [base, e] : live_) { | ||
| 84 | if (overlaps(e)) { | ||
| 85 | if (containing++ < 16) out += describe(inside(e) ? "live, containing" : "live, on the page", e, address); | ||
| 86 | continue; | ||
| 87 | } | ||
| 88 | if (base + e.size <= lo && (!below || base + e.size > below->base + below->size)) below = &e; | ||
| 89 | if (base >= hi && (!above || base < above->base)) above = &e; | ||
| 90 | } | ||
| 91 | if (below) out += describe("live, nearest below", *below, address); | ||
| 92 | if (above) out += describe("live, nearest above", *above, address); | ||
| 93 | int held = 0; | ||
| 94 | for (auto it = unbound_.rbegin(); it != unbound_.rend() && held < 16; ++it) | ||
| 95 | if (overlaps(*it)) { | ||
| 96 | out += describe(inside(*it) ? "unbound, held it" : "unbound, held the page", *it, address); | ||
| 97 | ++held; | ||
| 98 | } | ||
| 99 | if (!containing && !held) out += " nothing live or recently unbound there\n"; | ||
| 100 | return out; | ||
| 101 | } | ||
| 102 | |||
| 103 | private: | ||
| 104 | static const char* type_name(VkObjectType t) { | ||
| 105 | switch (t) { | ||
| 106 | case VK_OBJECT_TYPE_BUFFER: return "buffer"; | ||
| 107 | case VK_OBJECT_TYPE_IMAGE: return "image"; | ||
| 108 | case VK_OBJECT_TYPE_DEVICE_MEMORY: return "memory"; | ||
| 109 | case VK_OBJECT_TYPE_ACCELERATION_STRUCTURE_KHR: return "acceleration structure"; | ||
| 110 | case VK_OBJECT_TYPE_COMMAND_BUFFER: return "command buffer"; | ||
| 111 | case VK_OBJECT_TYPE_PIPELINE: return "pipeline"; | ||
| 112 | case VK_OBJECT_TYPE_QUERY_POOL: return "query pool"; | ||
| 113 | case VK_OBJECT_TYPE_DESCRIPTOR_POOL: return "descriptor pool"; | ||
| 114 | default: return "object"; | ||
| 115 | } | ||
| 116 | } | ||
| 117 | |||
| 118 | static std::string describe(const char* what, const Entry& e, std::uint64_t address) { | ||
| 119 | const std::uint64_t end = e.base + e.size; | ||
| 120 | const char* relation; | ||
| 121 | std::uint64_t distance; | ||
| 122 | if (address >= e.base && address < end) { | ||
| 123 | relation = "into"; | ||
| 124 | distance = address - e.base; | ||
| 125 | } else if (address >= end) { | ||
| 126 | relation = "past the end of"; | ||
| 127 | distance = address - end; | ||
| 128 | } else { | ||
| 129 | relation = "before"; | ||
| 130 | distance = e.base - address; | ||
| 131 | } | ||
| 132 | char line[320]; | ||
| 133 | std::snprintf(line, sizeof line, | ||
| 134 | " %s: %s%s 0x%" PRIx64 "..0x%" PRIx64 " (%" PRIu64 " KiB) handle 0x%" PRIx64 | ||
| 135 | " bound at %.1f s%s; the address is 0x%" PRIx64 " %s it\n", | ||
| 136 | what, type_name(e.type), e.internal ? " (driver-internal)" : "", e.base, end, e.size >> 10, | ||
| 137 | e.handle, e.bound_at, e.unbound_at ? (", unbound at " + std::to_string(e.unbound_at) + " s").c_str() : "", | ||
| 138 | distance, relation); | ||
| 139 | return line; | ||
| 140 | } | ||
| 141 | |||
| 142 | mutable std::mutex mutex_; | ||
| 143 | std::multimap<std::uint64_t, Entry> live_; // by base address | ||
| 144 | std::deque<Entry> unbound_; // the most recent last | ||
| 145 | std::uint64_t binds_ = 0, unbinds_ = 0; | ||
| 146 | }; | ||
| 147 | |||
| 148 | struct State { | ||
| 149 | std::mutex install; // the messenger fields below; the record has a lock of its own | ||
| 150 | Record record; | ||
| 151 | std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now(); | ||
| 152 | VkPhysicalDeviceAddressBindingReportFeaturesEXT feature{ | ||
| 153 | VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ADDRESS_BINDING_REPORT_FEATURES_EXT}; | ||
| 154 | VkDebugUtilsMessengerEXT messenger = VK_NULL_HANDLE; | ||
| 155 | VkInstance messenger_instance = VK_NULL_HANDLE; | ||
| 156 | PFN_vkDestroyDebugUtilsMessengerEXT destroy_messenger = nullptr; | ||
| 157 | // A device enabled the report and this process's messenger records it. Read at a device fault on whatever thread | ||
| 158 | // notices it, so atomic. | ||
| 159 | std::atomic<bool> on{false}; | ||
| 160 | }; | ||
| 161 | |||
| 162 | inline State& state() { | ||
| 163 | static State s; | ||
| 164 | return s; | ||
| 165 | } | ||
| 166 | |||
| 167 | inline bool requested() { | ||
| 168 | const char* v = std::getenv("LEONIDA_ADDRESS_BINDINGS"); | ||
| 169 | return v && v[0] == '1' && !v[1]; | ||
| 170 | } | ||
| 171 | |||
| 172 | inline VKAPI_ATTR VkBool32 VKAPI_CALL on_message(VkDebugUtilsMessageSeverityFlagBitsEXT, | ||
| 173 | VkDebugUtilsMessageTypeFlagsEXT types, | ||
| 174 | const VkDebugUtilsMessengerCallbackDataEXT* data, void*) { | ||
| 175 | if (!(types & VK_DEBUG_UTILS_MESSAGE_TYPE_DEVICE_ADDRESS_BINDING_BIT_EXT) || !data) return VK_FALSE; | ||
| 176 | const VkDeviceAddressBindingCallbackDataEXT* binding = nullptr; | ||
| 177 | for (auto* p = static_cast<const VkBaseInStructure*>(data->pNext); p; p = p->pNext) | ||
| 178 | if (p->sType == VK_STRUCTURE_TYPE_DEVICE_ADDRESS_BINDING_CALLBACK_DATA_EXT) | ||
| 179 | binding = reinterpret_cast<const VkDeviceAddressBindingCallbackDataEXT*>(p); | ||
| 180 | if (!binding) return VK_FALSE; | ||
| 181 | State& s = state(); | ||
| 182 | Entry e; | ||
| 183 | e.base = binding->baseAddress; | ||
| 184 | e.size = binding->size; | ||
| 185 | e.internal = (binding->flags & VK_DEVICE_ADDRESS_BINDING_INTERNAL_OBJECT_BIT_EXT) != 0; | ||
| 186 | if (data->objectCount && data->pObjects) { | ||
| 187 | e.type = data->pObjects[0].objectType; | ||
| 188 | e.handle = data->pObjects[0].objectHandle; | ||
| 189 | } | ||
| 190 | const double now = std::chrono::duration<double>(std::chrono::steady_clock::now() - s.start).count(); | ||
| 191 | s.record.record(e, binding->bindingType == VK_DEVICE_ADDRESS_BINDING_TYPE_BIND_EXT, now); | ||
| 192 | return VK_FALSE; | ||
| 193 | } | ||
| 194 | |||
| 195 | // Before vkCreateDevice: adds the extension and its feature to `device`'s chain when the device has them and | ||
| 196 | // `instance` has this process's messenger. Without the messenger nothing would record the reports, so the device is | ||
| 197 | // left as it is and a fault report says the bindings were not recorded. | ||
| 198 | inline bool enable_device(VkInstance instance, VkPhysicalDevice physical, PFN_vkGetPhysicalDeviceFeatures2 query, | ||
| 199 | std::span<const VkExtensionProperties> advertised, std::vector<const char*>& extensions, | ||
| 200 | VkDeviceCreateInfo& device) { | ||
| 201 | if (!requested() || !query) return false; | ||
| 202 | { | ||
| 203 | State& s = state(); | ||
| 204 | std::lock_guard lock(s.install); | ||
| 205 | if (!s.messenger || s.messenger_instance != instance) { | ||
| 206 | std::fprintf(stderr, "[rt] address-binding: no messenger on this device's instance; bindings are not " | ||
| 207 | "recorded\n"); | ||
| 208 | return false; | ||
| 209 | } | ||
| 210 | } | ||
| 211 | const bool has = std::any_of(advertised.begin(), advertised.end(), [](const auto& e) { | ||
| 212 | return std::strcmp(e.extensionName, VK_EXT_DEVICE_ADDRESS_BINDING_REPORT_EXTENSION_NAME) == 0; | ||
| 213 | }); | ||
| 214 | State& s = state(); | ||
| 215 | if (!has) { | ||
| 216 | std::fprintf(stderr, "[rt] address-binding: the device has no VK_EXT_device_address_binding_report\n"); | ||
| 217 | return false; | ||
| 218 | } | ||
| 219 | s.feature.pNext = nullptr; | ||
| 220 | VkPhysicalDeviceFeatures2 features{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2, &s.feature}; | ||
| 221 | query(physical, &features); | ||
| 222 | if (!s.feature.reportAddressBinding) { | ||
| 223 | std::fprintf(stderr, "[rt] address-binding: reportAddressBinding is not supported\n"); | ||
| 224 | return false; | ||
| 225 | } | ||
| 226 | extensions.push_back(VK_EXT_DEVICE_ADDRESS_BINDING_REPORT_EXTENSION_NAME); | ||
| 227 | s.feature.pNext = const_cast<void*>(device.pNext); | ||
| 228 | device.pNext = &s.feature; | ||
| 229 | s.on = true; | ||
| 230 | return true; | ||
| 231 | } | ||
| 232 | |||
| 233 | // After vkCreateInstance with VK_EXT_debug_utils and before vkCreateDevice: bindings reach the instance's messengers | ||
| 234 | // as they happen. | ||
| 235 | inline void install_messenger(VkInstance instance, PFN_vkGetInstanceProcAddr get) { | ||
| 236 | if (!requested() || !instance || !get) return; | ||
| 237 | const auto create = | ||
| 238 | reinterpret_cast<PFN_vkCreateDebugUtilsMessengerEXT>(get(instance, "vkCreateDebugUtilsMessengerEXT")); | ||
| 239 | if (!create) { | ||
| 240 | std::fprintf(stderr, "[rt] address-binding: no vkCreateDebugUtilsMessengerEXT\n"); | ||
| 241 | return; | ||
| 242 | } | ||
| 243 | VkDebugUtilsMessengerCreateInfoEXT info{VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT}; | ||
| 244 | info.messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT | | ||
| 245 | VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT; | ||
| 246 | info.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_DEVICE_ADDRESS_BINDING_BIT_EXT; | ||
| 247 | info.pfnUserCallback = &on_message; | ||
| 248 | State& s = state(); | ||
| 249 | std::lock_guard lock(s.install); | ||
| 250 | // One record per process: a second instance (a second device in one process) would mix its GPU addresses with | ||
| 251 | // the first's, and overwriting the handle would leave the first messenger alive past its instance. So the | ||
| 252 | // second one is refused, and its device is not enabled. | ||
| 253 | if (s.messenger) { | ||
| 254 | std::fprintf(stderr, "[rt] address-binding: a messenger is already installed for another instance; this " | ||
| 255 | "instance's bindings are not recorded\n"); | ||
| 256 | return; | ||
| 257 | } | ||
| 258 | VkDebugUtilsMessengerEXT messenger = VK_NULL_HANDLE; | ||
| 259 | const VkResult r = create(instance, &info, nullptr, &messenger); | ||
| 260 | if (r == VK_SUCCESS) { | ||
| 261 | s.messenger = messenger; | ||
| 262 | s.messenger_instance = instance; | ||
| 263 | s.destroy_messenger = reinterpret_cast<PFN_vkDestroyDebugUtilsMessengerEXT>( | ||
| 264 | get(instance, "vkDestroyDebugUtilsMessengerEXT")); | ||
| 265 | } | ||
| 266 | std::fprintf(stderr, "[rt] address-binding: messenger %s\n", r == VK_SUCCESS ? "installed" : "failed"); | ||
| 267 | } | ||
| 268 | |||
| 269 | // Before vkDestroyInstance: a messenger may not outlive its instance. What it recorded stays. | ||
| 270 | inline void uninstall_messenger(VkInstance instance) { | ||
| 271 | State& s = state(); | ||
| 272 | std::lock_guard lock(s.install); | ||
| 273 | if (!s.messenger || s.messenger_instance != instance) return; | ||
| 274 | if (s.destroy_messenger) s.destroy_messenger(instance, s.messenger, nullptr); | ||
| 275 | s.messenger = VK_NULL_HANDLE; | ||
| 276 | s.messenger_instance = VK_NULL_HANDLE; | ||
| 277 | s.destroy_messenger = nullptr; | ||
| 278 | s.on = false; | ||
| 279 | } | ||
| 280 | |||
| 281 | // At a device fault: what the driver had bound at and around `address`, on stderr. | ||
| 282 | inline void describe(std::uint64_t address, std::uint64_t precision) { | ||
| 283 | State& s = state(); | ||
| 284 | if (!s.on) { | ||
| 285 | // Asked for but not recording: say so, rather than leave a report that reads as "no object there". | ||
| 286 | if (requested()) | ||
| 287 | std::fprintf(stderr, "[rt] address-binding: 0x%" PRIx64 ": bindings were not recorded for this device (no " | ||
| 288 | "messenger or no VK_EXT_device_address_binding_report; see the address-binding " | ||
| 289 | "lines at startup)\n", address); | ||
| 290 | return; | ||
| 291 | } | ||
| 292 | const std::string text = s.record.report(address, precision); | ||
| 293 | std::size_t at = 0; | ||
| 294 | while (at < text.size()) { | ||
| 295 | const std::size_t end = text.find('\n', at); | ||
| 296 | std::fprintf(stderr, "[rt] address-binding: %.*s\n", int((end == std::string::npos ? text.size() : end) - at), | ||
| 297 | text.c_str() + at); | ||
| 298 | if (end == std::string::npos) break; | ||
| 299 | at = end + 1; | ||
| 300 | } | ||
| 301 | } | ||
| 302 | |||
| 303 | } // namespace leonida::address_binding | ||
diff --git a/runtime/src/vulkan_device_fault.h b/runtime/src/vulkan_device_fault.h index 3c49fce8d..9b55ef268 100644 --- a/runtime/src/vulkan_device_fault.h +++ b/runtime/src/vulkan_device_fault.h | |||
| @@ -1,6 +1,14 @@ | |||
| 1 | #pragma once | 1 | #pragma once |
| 2 | 2 | ||
| 3 | #include <vulkan/vulkan.h> | 3 | #include <vulkan/vulkan.h> |
| 4 | #ifdef _WIN32 | ||
| 5 | #ifndef NOMINMAX | ||
| 6 | #define NOMINMAX | ||
| 7 | #endif | ||
| 8 | #include <windows.h> // VirtualQuery: what the process has at a fault address | ||
| 9 | #endif | ||
| 10 | |||
| 11 | #include "address_binding.h" // LEONIDA_ADDRESS_BINDINGS: the bound object at a fault address | ||
| 4 | 12 | ||
| 5 | #include "compile_gate_policy.h" | 13 | #include "compile_gate_policy.h" |
| 6 | #include "vulkan_device_markers.h" | 14 | #include "vulkan_device_markers.h" |
| @@ -1578,6 +1586,19 @@ private: | |||
| 1578 | std::fprintf(stderr, "[rt] device-fault: address[%zu] type=%d address=0x%" PRIx64 | 1586 | std::fprintf(stderr, "[rt] device-fault: address[%zu] type=%d address=0x%" PRIx64 |
| 1579 | " precision=%" PRIu64 "\n", | 1587 | " precision=%" PRIu64 "\n", |
| 1580 | i, int(address.addressType), address.reportedAddress, address.addressPrecision); | 1588 | i, int(address.addressType), address.reportedAddress, address.addressPrecision); |
| 1589 | #ifdef _WIN32 | ||
| 1590 | // The page in this process at the same number. A GPU address is not a CPU one, but a host import, a title | ||
| 1591 | // range or a raw CPU pointer that reached the GPU shares it, so the region says which of those it can be. | ||
| 1592 | MEMORY_BASIC_INFORMATION page{}; | ||
| 1593 | if (VirtualQuery(reinterpret_cast<const void*>(static_cast<std::uintptr_t>(address.reportedAddress)), &page, | ||
| 1594 | sizeof page)) | ||
| 1595 | std::fprintf(stderr, "[rt] device-fault: address[%zu] in this process: base %p allocation %p size 0x%zx " | ||
| 1596 | "state 0x%lx protect 0x%lx type 0x%lx\n", | ||
| 1597 | i, page.BaseAddress, page.AllocationBase, page.RegionSize, | ||
| 1598 | static_cast<unsigned long>(page.State), static_cast<unsigned long>(page.Protect), | ||
| 1599 | static_cast<unsigned long>(page.Type)); | ||
| 1600 | #endif | ||
| 1601 | leonida::address_binding::describe(address.reportedAddress, address.addressPrecision); | ||
| 1581 | } | 1602 | } |
| 1582 | for (std::size_t i = 0; i < counts.vendorInfoCount; ++i) { | 1603 | for (std::size_t i = 0; i < counts.vendorInfoCount; ++i) { |
| 1583 | const auto& vendor = vendors[i]; | 1604 | const auto& vendor = vendors[i]; |