// GPU breadcrumbs for a lost device (vk.h vk_crumb). VK_AMD_buffer_marker writes a 32-bit code into a buffer from // the command stream: at TOP_OF_PIPE once the command processor reaches the marker, at BOTTOM_OF_PIPE once every // command before it has finished. Each crumb writes both, into two words of one host-coherent buffer, so after a // device loss word 0 names the newest point the GPU reached and word 1 the newest point it had finished: the hang lies // between them. A code is the crumb kind in the top 4 bits and its number in the low 28. The markers cost two small // writes per crumb, and crumbs mark submissions, upload batches, ASTC chunks and presents, not draws. #include #include #include "host.h" #include "vk.h" static PFN_vkCmdWriteBufferMarkerAMD write_marker; static VkBuffer buf; static VmaAllocation alloc; static volatile uint32_t *words; void vk_crumbs_init(VkCtx *c) { if (!c->has_buffer_marker) return; write_marker = (PFN_vkCmdWriteBufferMarkerAMD)vkGetDeviceProcAddr(c->device, "vkCmdWriteBufferMarkerAMD"); VkBufferCreateInfo bi = {.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, .size = 16, .usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT}; VmaAllocationCreateInfo ai = {.usage = VMA_MEMORY_USAGE_AUTO_PREFER_HOST, .flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_RANDOM_BIT | VMA_ALLOCATION_CREATE_MAPPED_BIT, .requiredFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT}; VmaAllocationInfo info; if (!write_marker || vmaCreateBuffer(c->vma, &bi, &ai, &buf, &alloc, &info) != VK_SUCCESS) { rt_log("vk: GPU breadcrumbs unavailable"); write_marker = NULL; return; } words = info.pMappedData; words[0] = words[1] = 0; rt_log("vk: GPU breadcrumbs on (VK_AMD_buffer_marker)"); } void vk_crumbs_release(void) { VkCtx *c = vk_ctx(); if (buf && c) vmaDestroyBuffer(c->vma, buf, alloc); buf = VK_NULL_HANDLE, alloc = NULL, words = NULL, write_marker = NULL; } void vk_crumb(VkCommandBuffer cb, int kind, uint64_t n) { if (!write_marker || !cb) return; uint32_t code = (uint32_t)kind << 28 | (uint32_t)(n & 0x0fffffff); write_marker(cb, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, buf, 0, code); write_marker(cb, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, buf, 4, code); } static void describe(uint32_t code, char *o, size_t size) { static const char *const kinds[] = {"nothing", "the start of a part of submission", "the end of submission", "the start of upload batch", "the end of upload batch", "ASTC decode chunk", "the start of present", "the end of present"}; unsigned k = code >> 28; if (!code) snprintf(o, size, "nothing yet"); else if (k < sizeof kinds / sizeof *kinds) snprintf(o, size, "%s %u", kinds[k], code & 0x0fffffff); else snprintf(o, size, "unknown code %#x", code); } void vk_crumbs_report(void) { if (!words) return; char reached[96], finished[96]; describe(words[0], reached, sizeof reached); describe(words[1], finished, sizeof finished); rt_log("vk: GPU breadcrumbs: the GPU last reached %s; everything before %s had finished", reached, finished); }