diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index 3084bf06b8..9d346a3d49 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -4920,7 +4920,12 @@ in that list when available. A checkpoint shows progress at a reported pipeline the faulting work. The loss report also lists the last 64 compute dispatches submitted across queues, including direct group dimensions or indirect argument-buffer locations, even without driver checkpoints. These entries show submission order. Set `LEONIDA_DEVICE_FAULT_ROOTS=1` to include each dispatch's 32 recorded root words in the local -diagnostic log. While this mode is enabled, a host thread samples the submission timelines once a second. If the +diagnostic log. In this mode each of the 64 recent submissions also keeps its own dispatches. A dispatch checkpoint the driver +reports is then followed by `checkpoint dispatch` with that exact dispatch: groups or indirect arguments, and the +root words. This works even when later submissions have pushed it out of the global history of 64, which is what a +busy compute queue does by the time a loss is reported. Each submission keeps up to its last 1,024 dispatches, so +a checkpoint early in a long compute submission is found as well; past that, the report says how many dispatches the +submission recorded and how many it kept. At about 250 bytes a dispatch this is at most 16 MiB, in this mode only. While this mode is enabled, a host thread samples the submission timelines once a second. If the retired ticket stays behind submitted work for 15 seconds, it prints the CPU dispatch history. It also reports when it cannot sample the submission lock for that long. A progress warning leaves D3D12 fence values and the device state intact. The driver checkpoint and fault-info queries run after Vulkan reports device loss. This mode diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index 9c433257d3..8aa97b8486 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -2030,6 +2030,8 @@ if(TARGET Vulkan::Headers) add_executable(vulkan_device_fault_test test/vulkan_device_fault_test.cpp) target_link_libraries(vulkan_device_fault_test PRIVATE Vulkan::Headers) add_test(NAME vulkan_device_fault COMMAND vulkan_device_fault_test) + add_test(NAME vulkan_device_fault_roots COMMAND vulkan_device_fault_test) + set_tests_properties(vulkan_device_fault_roots PROPERTIES ENVIRONMENT "LEONIDA_DEVICE_FAULT_ROOTS=1") # The AMD breadcrumbs (LEONIDA_DEVICE_MARKERS): seq numbering, marker writes and the likely-hang verdict, on the CPU. add_executable(vulkan_device_markers_test test/vulkan_device_markers_test.cpp) target_link_libraries(vulkan_device_markers_test PRIVATE Vulkan::Headers) diff --git a/runtime/src/vulkan_device_fault.h b/runtime/src/vulkan_device_fault.h index 3c49fce8d2..a39c4b5480 100644 --- a/runtime/src/vulkan_device_fault.h +++ b/runtime/src/vulkan_device_fault.h @@ -177,6 +177,11 @@ struct CheckpointSubmission { std::vector dispatches; std::size_t dispatch_count = 0; static constexpr std::size_t dispatch_limit = 64; + // With LEONIDA_DEVICE_FAULT_ROOTS a submission keeps up to this many of its own dispatches, so a checkpoint the + // driver reports early in a long compute submission still finds its dispatch. Diagnostic mode only: 64 recent + // submissions at about 250 bytes a dispatch is at most 16 MiB. + static constexpr std::size_t roots_dispatch_limit = 1024; + std::size_t keep = dispatch_limit; // how many of its most recent dispatches this submission keeps // Numbers every recorded dispatch, across queues and submissions (the report's seq). Null: no numbers. std::atomic* dispatch_seq = nullptr; // LEONIDA_DEVICE_MARKERS=1: this queue's marker slot, every dispatch recorded for the marker history and the seq @@ -186,8 +191,8 @@ struct CheckpointSubmission { std::uint64_t open_marker = 0; void append_dispatch(CheckpointDispatch d) { - if (dispatches.size() < dispatch_limit) dispatches.push_back(d); - else dispatches[dispatch_count % dispatch_limit] = d; + if (dispatches.size() < keep) dispatches.push_back(d); + else dispatches[dispatch_count % keep] = d; ++dispatch_count; } }; @@ -1344,6 +1349,7 @@ class DeviceFaultDiagnostics { CheckpointSubmission submission{sequence_, call, queue, kind, lists, std::vector(std::min(lists, std::size_t{CheckpointMarker::max_list} + 1))}; submission.dispatch_seq = &dispatch_seq_; + if (include_roots_) submission.keep = CheckpointSubmission::roots_dispatch_limit; if (markers_) { std::lock_guard lock(mutex_); submission.markers = markers_->target(queue); @@ -1353,9 +1359,13 @@ class DeviceFaultDiagnostics { void publish(CheckpointSubmission&& submission) { std::lock_guard lock(mutex_); - for (std::size_t i = submission.dispatch_count - submission.dispatches.size(); i < submission.dispatch_count; ++i) - dispatches_[dispatch_count_++ % dispatches_.size()] = submission.dispatches[i % submission.dispatch_limit]; - submission.dispatches.clear(); + // The global history keeps the last 64 across submissions, so only a submission's last 64 can stay in it. + const std::size_t copied = std::min(submission.dispatches.size(), dispatches_.size()); + for (std::size_t i = submission.dispatch_count - copied; i < submission.dispatch_count; ++i) + dispatches_[dispatch_count_++ % dispatches_.size()] = submission.dispatches[i % submission.keep]; + // With LEONIDA_DEVICE_FAULT_ROOTS each recent submission keeps its own dispatches: a later submission can + // push every one of them out of the global history before the driver reports a checkpoint in it. + if (!include_roots_) submission.dispatches.clear(); if (history_) for (const auto& d : submission.marked) history_->push(d); submission.marked.clear(); @@ -1480,25 +1490,34 @@ class DeviceFaultDiagnostics { const auto n = std::min(dispatch_count_, dispatches_.size()); for (std::size_t i = dispatch_count_ - n; i < dispatch_count_; ++i) { const auto& d = dispatches_[i % dispatches_.size()]; - std::fprintf(stderr, "[rt] device-fault: submitted dispatch seq=%" PRIu64 " execute=%" PRIu64 - " queue=%p (%s) list=%zu command=%zu shader=%s ", d.seq, d.execute_call, - static_cast(d.queue), d.queue_kind, d.list, d.command, d.shader ? d.shader : "-"); - if (d.indirect) - std::fprintf(stderr, "indirect args=0x%" PRIxPTR "+0x%" PRIx64 " count=0x%" PRIxPTR - "+0x%" PRIx64 " max=%u stride=%u", d.argument_buffer, d.argument_offset, - d.count_buffer, d.count_offset, d.max_count, d.stride); - else std::fprintf(stderr, "groups=%u,%u,%u", d.groups[0], d.groups[1], d.groups[2]); - if (include_roots_) { - std::fputs(" roots=", stderr); - for (auto word : d.roots) std::fprintf(stderr, "%08x ", word); - } - if (d.seq && std::find(hangs.begin(), hangs.end(), d.seq) != hangs.end()) - std::fputs(" <- likely hang (device-markers)", stderr); - std::fputc('\n', stderr); + const bool likely_hang = d.seq && std::find(hangs.begin(), hangs.end(), d.seq) != hangs.end(); + std::fprintf(stderr, "[rt] device-fault: submitted dispatch seq=%" PRIu64, d.seq); + print_dispatch(d, likely_hang); + } + } + + // The rest of a dispatch line: where it was submitted, its shader, its groups or indirect arguments, and with + // LEONIDA_DEVICE_FAULT_ROOTS its 32 root words. + void print_dispatch(const CheckpointDispatch& d, bool likely_hang = false) const { + std::fprintf(stderr, " execute=%" PRIu64 " queue=%p (%s) list=%zu command=%zu shader=%s ", d.execute_call, + static_cast(d.queue), d.queue_kind, d.list, d.command, d.shader ? d.shader : "-"); + if (d.indirect) + std::fprintf(stderr, "indirect args=0x%" PRIxPTR "+0x%" PRIx64 " count=0x%" PRIxPTR + "+0x%" PRIx64 " max=%u stride=%u", d.argument_buffer, d.argument_offset, + d.count_buffer, d.count_offset, d.max_count, d.stride); + else std::fprintf(stderr, "groups=%u,%u,%u", d.groups[0], d.groups[1], d.groups[2]); + if (include_roots_) { + std::fputs(" roots=", stderr); + for (auto word : d.roots) std::fprintf(stderr, "%08x ", word); } + if (likely_hang) std::fputs(" <- likely hang (device-markers)", stderr); + std::fputc('\n', stderr); } public: + // Checkpoints whose dispatch the report found in its submission's history (LEONIDA_DEVICE_FAULT_ROOTS); tests. + std::size_t checkpoint_dispatches_found() const { return checkpoint_dispatches_found_; } + // LOCAL DIAGNOSTIC: run once after a loss is reported (the compute input probe rechecks the descriptors it saw). void set_lost_hook(std::function hook) { std::lock_guard lock(mutex_); @@ -1621,6 +1640,22 @@ class DeviceFaultDiagnostics { pipeline.shaders[1] ? pipeline.shaders[1] : "-", pipeline.shaders[2] ? pipeline.shaders[2] : "-"); } + // The dispatch at the checkpoint, from the submission's own history (LEONIDA_DEVICE_FAULT_ROOTS). + if (include_roots_ && marker.kind == CheckpointKind::dispatch) { + bool found = false; + for (const auto& dispatch : entry.dispatches) { + if (dispatch.list != marker.list || dispatch.command != marker.command) continue; + std::fputs("[rt] device-fault: checkpoint dispatch", stderr); + print_dispatch(dispatch); + found = true; + } + if (found) ++checkpoint_dispatches_found_; + else if (entry.dispatch_count > entry.dispatches.size()) + std::fprintf(stderr, "[rt] device-fault: checkpoint dispatch not in its submission's history (it " + "recorded %zu dispatches and keeps the last %zu)\n", entry.dispatch_count, + entry.dispatches.size()); + else std::fputs("[rt] device-fault: checkpoint dispatch not in its submission's history\n", stderr); + } } // LOCAL DIAGNOSTIC (post-load GPU faults): the work in flight is between the bottom-of-pipe checkpoint (last // command finished) and the top-of-pipe one (last command started) of the same list. Every pipeline bound in @@ -1674,6 +1709,7 @@ class DeviceFaultDiagnostics { std::uint64_t stalled_ticket_ = 0; std::atomic reported_{false}; std::atomic execute_calls_{0}; + mutable std::size_t checkpoint_dispatches_found_ = 0; std::uint32_t sequence_ = 0; // Held while a report reads the counters of the semaphores in the ring, and by forget before one is destroyed. // Before mutex_; the submit path never takes it. diff --git a/runtime/test/vulkan_device_fault_test.cpp b/runtime/test/vulkan_device_fault_test.cpp index d6bfe13d74..0d7ed97d0d 100644 --- a/runtime/test/vulkan_device_fault_test.cpp +++ b/runtime/test/vulkan_device_fault_test.cpp @@ -123,13 +123,29 @@ int main() { for (unsigned i = 0; i < 70; ++i) cpu.dispatch(VK_NULL_HANDLE, i, VK_NULL_HANDLE, 4, roots, {i, 2, 3}); cpu.dispatch(VK_NULL_HANDLE, 70, VK_NULL_HANDLE, 4, roots, {}, true, 17, 32, 19, 64, 4, 12); - const auto& oldest = submission.dispatches[submission.dispatch_count % submission.dispatch_limit]; - const auto& latest = submission.dispatches[(submission.dispatch_count - 1) % submission.dispatch_limit]; - if (submission.dispatches.size() != 64 || oldest.command != 7 || oldest.groups[0] != 7 || + // The driver will report the checkpoint at an early dispatch (list 1, command 3): one of the 71 that the last 64 + // no longer hold, as in a long compute submission whose loss is reported well after its start. + last_marker = reinterpret_cast(CheckpointMarker::encode(submission.sequence, 1, 3, CheckpointKind::dispatch)); + const char* roots_env = std::getenv("LEONIDA_DEVICE_FAULT_ROOTS"); + const bool roots_on = roots_env && !std::strcmp(roots_env, "1"); + // Without LEONIDA_DEVICE_FAULT_ROOTS a submission keeps its last 64; with it, up to roots_dispatch_limit. + const std::size_t kept = roots_on ? 71 : 64, first_kept = 71 - kept; + if (submission.keep != (roots_on ? CheckpointSubmission::roots_dispatch_limit : CheckpointSubmission::dispatch_limit)) + return 11; + const auto& oldest = submission.dispatches[(submission.dispatch_count - kept) % submission.keep]; + const auto& latest = submission.dispatches[(submission.dispatch_count - 1) % submission.keep]; + if (submission.dispatches.size() != kept || oldest.command != first_kept || oldest.groups[0] != first_kept || oldest.roots[5] != 42 || std::strcmp(oldest.shader, "test_shader") || latest.list != 1 || !latest.indirect || latest.argument_buffer != 17 || latest.count_offset != 64 || latest.stride != 12 || latest.queue != queue) return 7; diagnostics.publish(std::move(submission)); + // A later submission can push every dispatch of that one out of the global history before the driver reports + // the checkpoint in it; with LEONIDA_DEVICE_FAULT_ROOTS the report still finds it in the submission's own. + auto later = diagnostics.begin_submission(diagnostics.next_execute_call(), queue, "direct", 1); + const CheckpointRecorder later_cpu{nullptr, later, diagnostics.pipeline_names, 0}; + for (unsigned i = 0; i < 64; ++i) + later_cpu.dispatch(VK_NULL_HANDLE, i, VK_NULL_HANDLE, 4, {}, {i, 1, 1}); + diagnostics.publish(std::move(later)); // A stopped timeline emits the CPU history once. Idle time and new completions reset the clock. using Watch = SubmissionProgressWatch; using namespace std::chrono_literals; @@ -150,4 +166,5 @@ int main() { DeviceFaultDiagnostics::observe(&diagnostics, VK_ERROR_DEVICE_LOST, "test repeat") != VK_ERROR_DEVICE_LOST || fault_queries != 2 || !fault_data_written || checkpoint_queries != 2) return 6; + if (diagnostics.checkpoint_dispatches_found() != (roots_on ? 1u : 0u)) return 10; }