|
| 1 | +// Verify buffer memory requirements alignments match between trace and replay paths |
| 2 | + |
| 3 | +#include "tests/common.h" |
| 4 | +#include "memory.h" |
| 5 | +#include "packfile.h" |
| 6 | +#include "vk_wrapper_auto.h" |
| 7 | +#include <stdio.h> |
| 8 | +#include <assert.h> |
| 9 | +#include <unordered_map> |
| 10 | +#include <vector> |
| 11 | + |
| 12 | +#define TEST_NAME "tracing_alignment" |
| 13 | + |
| 14 | +static std::unordered_map<uint32_t, VkDeviceSize> trace_alignments; |
| 15 | +static std::unordered_map<uint32_t, VkDeviceSize> replay_alignments; |
| 16 | +static std::vector<uint32_t> buffer_indices; |
| 17 | +static size_t replay_call_index = 0; |
| 18 | + |
| 19 | +static trackedbuffer make_trackedbuffer(const VkBufferCreateInfo& info, uint32_t idx) |
| 20 | +{ |
| 21 | + trackedbuffer t; |
| 22 | + t.index = idx; |
| 23 | + t.size = info.size; |
| 24 | + t.flags = info.flags; |
| 25 | + t.sharingMode = info.sharingMode; |
| 26 | + t.usage = info.usage; |
| 27 | + t.object_type = VK_OBJECT_TYPE_BUFFER; |
| 28 | + t.enter_initialized(); |
| 29 | + return t; |
| 30 | +} |
| 31 | + |
| 32 | +static void trace() |
| 33 | +{ |
| 34 | + vulkan_req_t reqs; |
| 35 | + reqs.apiVersion = VK_API_VERSION_1_3; |
| 36 | + vulkan_setup_t vulkan = test_init(TEST_NAME, reqs); |
| 37 | + VkResult result; |
| 38 | + |
| 39 | + trace_alignments.clear(); |
| 40 | + replay_alignments.clear(); |
| 41 | + buffer_indices.clear(); |
| 42 | + |
| 43 | + const VkDeviceSize sizes[] = { 64, 4096 + 13, 1024 * 1024 + 7 }; |
| 44 | + VkBufferCreateInfo info = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, nullptr }; |
| 45 | + info.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT; |
| 46 | + info.sharingMode = VK_SHARING_MODE_EXCLUSIVE; |
| 47 | + |
| 48 | + for (unsigned i = 0; i < sizeof(sizes) / sizeof(sizes[0]); i++) |
| 49 | + { |
| 50 | + info.size = sizes[i]; |
| 51 | + VkBuffer buffer = VK_NULL_HANDLE; |
| 52 | + result = trace_vkCreateBuffer(vulkan.device, &info, nullptr, &buffer); |
| 53 | + check(result); |
| 54 | + char name[32]; |
| 55 | + snprintf(name, sizeof(name), "alignment-buffer-%u", i); |
| 56 | + test_set_name(vulkan.device, VK_OBJECT_TYPE_BUFFER, (uint64_t)buffer, name); |
| 57 | + |
| 58 | + VkMemoryRequirements raw_req = {}; |
| 59 | + wrap_vkGetBufferMemoryRequirements(vulkan.device, buffer, &raw_req); |
| 60 | + assert(raw_req.alignment != 0); |
| 61 | + |
| 62 | + const uint32_t index = trace_vkGetDeviceTracingObjectPropertyTRACETOOLTEST(vulkan.device, VK_OBJECT_TYPE_BUFFER, (uint64_t)buffer, VK_TRACING_OBJECT_PROPERTY_INDEX_TRACETOOLTEST); |
| 63 | + |
| 64 | + VkMemoryRequirements traced_req = {}; |
| 65 | + trace_vkGetBufferMemoryRequirements(vulkan.device, buffer, &traced_req); |
| 66 | + assert(traced_req.alignment == raw_req.alignment); |
| 67 | + |
| 68 | + trackedbuffer tracked = make_trackedbuffer(info, index); |
| 69 | + memory_requirements replay_req = get_trackedbuffer_memory_requirements(vulkan.device, tracked); |
| 70 | + assert(replay_req.requirements.alignment == raw_req.alignment); |
| 71 | + |
| 72 | + trace_alignments[index] = traced_req.alignment; |
| 73 | + buffer_indices.push_back(index); |
| 74 | + trace_vkDestroyBuffer(vulkan.device, buffer, nullptr); |
| 75 | + ILOG("Trace %d align=%d buffer=%lu", (int)index, (int)raw_req.alignment, (unsigned long)buffer); |
| 76 | + } |
| 77 | + test_done(vulkan); |
| 78 | +} |
| 79 | + |
| 80 | +static bool getnext(lava_file_reader& t) |
| 81 | +{ |
| 82 | + bool done = false; |
| 83 | + const uint8_t instrtype = t.read_uint8_t(); |
| 84 | + if (instrtype == PACKET_VULKAN_API_CALL) |
| 85 | + { |
| 86 | + const uint16_t apicall = t.read_apicall(); |
| 87 | + if (apicall == 1) done = true; // vkDestroyInstance |
| 88 | + } |
| 89 | + else if (instrtype == PACKET_THREAD_BARRIER) |
| 90 | + { |
| 91 | + t.read_barrier(); |
| 92 | + } |
| 93 | + else if (instrtype == PACKET_IMAGE_UPDATE || instrtype == PACKET_IMAGE_UPDATE2) |
| 94 | + { |
| 95 | + update_image_packet(instrtype, t); |
| 96 | + } |
| 97 | + else if (instrtype == PACKET_BUFFER_UPDATE || instrtype == PACKET_BUFFER_UPDATE2) |
| 98 | + { |
| 99 | + update_buffer_packet(instrtype, t); |
| 100 | + } |
| 101 | + else if (instrtype == PACKET_TENSOR_UPDATE) |
| 102 | + { |
| 103 | + update_tensor_packet(instrtype, t); |
| 104 | + } |
| 105 | + else assert(false); |
| 106 | + t.parent->allocator.self_test(); |
| 107 | + return !done; |
| 108 | +} |
| 109 | + |
| 110 | +static void record_vkGetBufferMemoryRequirements(VkDevice device, VkBuffer buffer, VkMemoryRequirements* pMemoryRequirements) |
| 111 | +{ |
| 112 | + (void)device; |
| 113 | + (void)buffer; |
| 114 | + assert(pMemoryRequirements != nullptr); |
| 115 | + assert(replay_call_index < buffer_indices.size()); |
| 116 | + const uint32_t idx = buffer_indices.at(replay_call_index++); |
| 117 | + replay_alignments[idx] = pMemoryRequirements->alignment; |
| 118 | + ILOG("Found replay alignment idx=%d align=%d", (int)idx, (int)pMemoryRequirements->alignment); |
| 119 | +} |
| 120 | + |
| 121 | +static void retrace() |
| 122 | +{ |
| 123 | + // Verify trace file itself |
| 124 | + Json::Value tracking = packed_json("tracking.json", TEST_NAME ".vk"); |
| 125 | + unsigned matched = 0; |
| 126 | + for (const auto& entry : tracking["VkBuffer"]) |
| 127 | + { |
| 128 | + const uint32_t index = entry["index"].asUInt(); |
| 129 | + assert(entry.isMember("req_alignment")); |
| 130 | + const VkDeviceSize recorded = entry["req_alignment"].asUInt(); |
| 131 | + assert(recorded == trace_alignments.at(index)); |
| 132 | + matched++; |
| 133 | + } |
| 134 | + assert(matched == trace_alignments.size()); |
| 135 | + |
| 136 | + // Replay it |
| 137 | + lava_reader r(TEST_NAME ".vk"); |
| 138 | + replay_call_index = 0; |
| 139 | + replay_alignments.clear(); |
| 140 | + vkGetBufferMemoryRequirements_callbacks.push_back(record_vkGetBufferMemoryRequirements); |
| 141 | + lava_file_reader& t = r.file_reader(0); |
| 142 | + int remaining = r.allocator.self_test(); |
| 143 | + assert(remaining == 0); |
| 144 | + while (getnext(t)) {} |
| 145 | + remaining = r.allocator.self_test(); |
| 146 | + assert(remaining == 0); |
| 147 | + assert(replay_call_index == buffer_indices.size()); |
| 148 | + assert(replay_alignments.size() == trace_alignments.size()); |
| 149 | + |
| 150 | + // Check data we collected during run |
| 151 | + for (const auto& it : trace_alignments) |
| 152 | + { |
| 153 | + assert(replay_alignments.count(it.first) > 0); |
| 154 | + assert(replay_alignments.at(it.first) == it.second); |
| 155 | + } |
| 156 | + for (const auto& entry : tracking["VkBuffer"]) |
| 157 | + { |
| 158 | + const uint32_t idx = entry["index"].asUInt(); |
| 159 | + assert(replay_alignments.count(idx) > 0); |
| 160 | + assert(replay_alignments.at(idx) == trace_alignments.at(idx)); |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +int main() |
| 165 | +{ |
| 166 | + trace(); |
| 167 | + retrace(); |
| 168 | + return 0; |
| 169 | +} |
0 commit comments