upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/src/mem_probe.h
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/src/mem_probe.h')
-rw-r--r--runtime/src/mem_probe.h65
1 files changed, 63 insertions, 2 deletions
diff --git a/runtime/src/mem_probe.h b/runtime/src/mem_probe.h
index a36d38bfe..f8c573da4 100644
--- a/runtime/src/mem_probe.h
+++ b/runtime/src/mem_probe.h
@@ -9,12 +9,18 @@
9// other PE private memory (runtime + title heaps), PE images, PE sections, memory the Vulkan 9// other PE private memory (runtime + title heaps), PE images, PE sections, memory the Vulkan
10// driver mapped for us, /dev/nvidia* mappings, other native (Unix-side) anonymous memory 10// driver mapped for us, /dev/nvidia* mappings, other native (Unix-side) anonymous memory
11// (driver heaps, glibc arenas), and files. 11// (driver heaps, glibc arenas), and files.
12// [rt] mem-probe breakdown: the same allocations by consumer (src/memory_breakdown.h): the arena with its reserved,
13// bound and in-use bytes, geometry copies, title windows and shadows, upload heaps, staging,
14// texture blocks, sampled textures, render targets and depth, and other, which add up to the
15// allocated bytes of the t= line.
12// The Vulkan counts come from wrapping the runtime's own function table (install()); helpers that load their own 16// The Vulkan counts come from wrapping the runtime's own function table (install()); helpers that load their own
13// pointers (depth24, predicate, xdxr, swapchain) are not counted: they create a handful of objects. 17// pointers (depth24, predicate, xdxr, swapchain) are not counted: they create a handful of objects.
14 18
15#include <windows.h> 19#include <windows.h>
16#include <vulkan/vulkan.h> 20#include <vulkan/vulkan.h>
17 21
22#include "memory_breakdown.h"
23
18#include <algorithm> 24#include <algorithm>
19#include <atomic> 25#include <atomic>
20#include <cctype> 26#include <cctype>
@@ -40,6 +46,8 @@ struct Allocation {
40 bool imported = false; 46 bool imported = false;
41 std::uintptr_t mapped = 0; 47 std::uintptr_t mapped = 0;
42 std::uint64_t mapped_bytes = 0; 48 std::uint64_t mapped_bytes = 0;
49 // Who asked for it (an Owner scope at the allocation), or what was bound to it later (bind_image_memory).
50 memory_breakdown::Consumer consumer = memory_breakdown::Consumer::other;
43}; 51};
44 52
45struct State { 53struct State {
@@ -52,11 +60,15 @@ struct State {
52 PFN_vkCreateGraphicsPipelines create_graphics = nullptr; 60 PFN_vkCreateGraphicsPipelines create_graphics = nullptr;
53 PFN_vkCreateComputePipelines create_compute = nullptr; 61 PFN_vkCreateComputePipelines create_compute = nullptr;
54 PFN_vkDestroyPipeline destroy_pipeline = nullptr; 62 PFN_vkDestroyPipeline destroy_pipeline = nullptr;
63 PFN_vkCreateImage create_image = nullptr;
64 PFN_vkDestroyImage destroy_image = nullptr;
65 PFN_vkBindImageMemory bind_image = nullptr;
55 66
56 std::mutex mutex; 67 std::mutex mutex;
57 std::unordered_map<VkDeviceMemory, Allocation> allocations; 68 std::unordered_map<VkDeviceMemory, Allocation> allocations;
58 std::unordered_map<VkShaderModule, std::uint64_t> modules; 69 std::unordered_map<VkShaderModule, std::uint64_t> modules;
59 std::unordered_map<VkPipeline, std::uint8_t> pipelines; // 0 graphics, 1 compute 70 std::unordered_map<VkPipeline, std::uint8_t> pipelines; // 0 graphics, 1 compute
71 std::unordered_map<VkImage, std::uint32_t> image_usage; // VkImageUsageFlags of each live image
60 std::uint64_t module_bytes = 0, modules_made = 0, module_bytes_made = 0; 72 std::uint64_t module_bytes = 0, modules_made = 0, module_bytes_made = 0;
61 std::uint64_t graphics_live = 0, compute_live = 0, graphics_made = 0, compute_made = 0; 73 std::uint64_t graphics_live = 0, compute_live = 0, graphics_made = 0, compute_made = 0;
62 std::uint64_t graphics_ns = 0, compute_ns = 0; // time inside vkCreate*Pipelines 74 std::uint64_t graphics_ns = 0, compute_ns = 0; // time inside vkCreate*Pipelines
@@ -94,7 +106,8 @@ inline VKAPI_ATTR VkResult VKAPI_CALL allocate_memory(VkDevice device, const VkM
94 const VkResult result = s.allocate(device, info, callbacks, out); 106 const VkResult result = s.allocate(device, info, callbacks, out);
95 if (result == VK_SUCCESS && out && *out) { 107 if (result == VK_SUCCESS && out && *out) {
96 std::lock_guard lock(s.mutex); 108 std::lock_guard lock(s.mutex);
97 s.allocations[*out] = {info->memoryTypeIndex, info->allocationSize, imported_memory(info), 0, 0}; 109 s.allocations[*out] = {info->memoryTypeIndex, info->allocationSize, imported_memory(info), 0, 0,
110 memory_breakdown::current_owner()};
98 } 111 }
99 return result; 112 return result;
100} 113}
@@ -132,6 +145,43 @@ inline VKAPI_ATTR void VKAPI_CALL unmap_memory(VkDevice device, VkDeviceMemory m
132 s.unmap(device, memory); 145 s.unmap(device, memory);
133} 146}
134 147
148inline VKAPI_ATTR VkResult VKAPI_CALL create_image(VkDevice device, const VkImageCreateInfo* info,
149 const VkAllocationCallbacks* callbacks, VkImage* out) {
150 State& s = state();
151 const VkResult result = s.create_image(device, info, callbacks, out);
152 if (result == VK_SUCCESS && out && *out) {
153 std::lock_guard lock(s.mutex);
154 s.image_usage[*out] = info->usage;
155 }
156 return result;
157}
158
159inline VKAPI_ATTR void VKAPI_CALL destroy_image(VkDevice device, VkImage image, const VkAllocationCallbacks* callbacks) {
160 State& s = state();
161 if (image) {
162 std::lock_guard lock(s.mutex);
163 s.image_usage.erase(image);
164 }
165 s.destroy_image(device, image, callbacks);
166}
167
168// Memory nobody named that an image is bound to is that image's: a render target or depth buffer, or a sampled
169// texture. A named block (the texture blocks) keeps its name, whatever is bound into it.
170inline VKAPI_ATTR VkResult VKAPI_CALL bind_image_memory(VkDevice device, VkImage image, VkDeviceMemory memory,
171 VkDeviceSize offset) {
172 State& s = state();
173 const VkResult result = s.bind_image(device, image, memory, offset);
174 if (result == VK_SUCCESS) {
175 std::lock_guard lock(s.mutex);
176 const auto a = s.allocations.find(memory);
177 const auto u = s.image_usage.find(image);
178 if (a != s.allocations.end() && u != s.image_usage.end() &&
179 a->second.consumer == memory_breakdown::Consumer::other)
180 a->second.consumer = memory_breakdown::image_consumer(u->second);
181 }
182 return result;
183}
184
135inline VKAPI_ATTR VkResult VKAPI_CALL create_shader_module(VkDevice device, const VkShaderModuleCreateInfo* info, 185inline VKAPI_ATTR VkResult VKAPI_CALL create_shader_module(VkDevice device, const VkShaderModuleCreateInfo* info,
136 const VkAllocationCallbacks* callbacks, 186 const VkAllocationCallbacks* callbacks,
137 VkShaderModule* out) { 187 VkShaderModule* out) {
@@ -246,7 +296,8 @@ void install(Functions& vk) {
246 if (s.installed++) return; 296 if (s.installed++) return;
247 if (!vk.vkAllocateMemory || !vk.vkFreeMemory || !vk.vkMapMemory || !vk.vkUnmapMemory || 297 if (!vk.vkAllocateMemory || !vk.vkFreeMemory || !vk.vkMapMemory || !vk.vkUnmapMemory ||
248 !vk.vkCreateShaderModule || !vk.vkDestroyShaderModule || !vk.vkCreateGraphicsPipelines || 298 !vk.vkCreateShaderModule || !vk.vkDestroyShaderModule || !vk.vkCreateGraphicsPipelines ||
249 !vk.vkCreateComputePipelines || !vk.vkDestroyPipeline) 299 !vk.vkCreateComputePipelines || !vk.vkDestroyPipeline || !vk.vkCreateImage || !vk.vkDestroyImage ||
300 !vk.vkBindImageMemory)
250 return; 301 return;
251 s.started = GetTickCount64(); 302 s.started = GetTickCount64();
252 s.allocate = vk.vkAllocateMemory, vk.vkAllocateMemory = allocate_memory; 303 s.allocate = vk.vkAllocateMemory, vk.vkAllocateMemory = allocate_memory;
@@ -258,6 +309,9 @@ void install(Functions& vk) {
258 s.create_graphics = vk.vkCreateGraphicsPipelines, vk.vkCreateGraphicsPipelines = create_graphics_pipelines; 309 s.create_graphics = vk.vkCreateGraphicsPipelines, vk.vkCreateGraphicsPipelines = create_graphics_pipelines;
259 s.create_compute = vk.vkCreateComputePipelines, vk.vkCreateComputePipelines = create_compute_pipelines; 310 s.create_compute = vk.vkCreateComputePipelines, vk.vkCreateComputePipelines = create_compute_pipelines;
260 s.destroy_pipeline = vk.vkDestroyPipeline, vk.vkDestroyPipeline = destroy_pipeline; 311 s.destroy_pipeline = vk.vkDestroyPipeline, vk.vkDestroyPipeline = destroy_pipeline;
312 s.create_image = vk.vkCreateImage, vk.vkCreateImage = create_image;
313 s.destroy_image = vk.vkDestroyImage, vk.vkDestroyImage = destroy_image;
314 s.bind_image = vk.vkBindImageMemory, vk.vkBindImageMemory = bind_image_memory;
261 std::fprintf(stderr, "[rt] mem-probe: installed, every %u s\n", interval_seconds()); 315 std::fprintf(stderr, "[rt] mem-probe: installed, every %u s\n", interval_seconds());
262} 316}
263 317
@@ -451,6 +505,7 @@ inline std::string read_rollup() {
451struct DeviceFacts { 505struct DeviceFacts {
452 std::function<std::string()> describe; // one text fragment 506 std::function<std::string()> describe; // one text fragment
453 std::function<Ranges()> title_ranges; // the title's memory 507 std::function<Ranges()> title_ranges; // the title's memory
508 std::function<memory_breakdown::DeviceShare()> share; // the arena and the bindless tables, for the breakdown
454 VkPhysicalDeviceMemoryProperties memory{}; 509 VkPhysicalDeviceMemoryProperties memory{};
455}; 510};
456 511
@@ -477,6 +532,7 @@ inline void sample(const DeviceFacts& facts) {
477 std::uint64_t type_count[VK_MAX_MEMORY_TYPES] = {}, type_bytes[VK_MAX_MEMORY_TYPES] = {}; 532 std::uint64_t type_count[VK_MAX_MEMORY_TYPES] = {}, type_bytes[VK_MAX_MEMORY_TYPES] = {};
478 std::uint64_t import_count[VK_MAX_MEMORY_TYPES] = {}, import_bytes[VK_MAX_MEMORY_TYPES] = {}; 533 std::uint64_t import_count[VK_MAX_MEMORY_TYPES] = {}, import_bytes[VK_MAX_MEMORY_TYPES] = {};
479 Ranges vk_mapped; 534 Ranges vk_mapped;
535 memory_breakdown::Tally tally;
480 std::uint64_t modules = 0, module_bytes = 0, modules_made = 0, module_bytes_made = 0; 536 std::uint64_t modules = 0, module_bytes = 0, modules_made = 0, module_bytes_made = 0;
481 std::uint64_t graphics = 0, compute = 0, graphics_made = 0, compute_made = 0, graphics_ns = 0, compute_ns = 0; 537 std::uint64_t graphics = 0, compute = 0, graphics_made = 0, compute_made = 0, graphics_ns = 0, compute_ns = 0;
482 { 538 {
@@ -491,6 +547,7 @@ inline void sample(const DeviceFacts& facts) {
491 type_bytes[i] += a.bytes; 547 type_bytes[i] += a.bytes;
492 } 548 }
493 if (a.mapped && !a.imported) vk_mapped.push_back({a.mapped, a.mapped + a.mapped_bytes}); 549 if (a.mapped && !a.imported) vk_mapped.push_back({a.mapped, a.mapped + a.mapped_bytes});
550 tally.add(a.consumer, a.bytes, a.imported);
494 } 551 }
495 modules = s.modules.size(); 552 modules = s.modules.size();
496 module_bytes = s.module_bytes; 553 module_bytes = s.module_bytes;
@@ -527,6 +584,10 @@ inline void sample(const DeviceFacts& facts) {
527 (unsigned long long)compute_made, double(graphics_ns) / 1e9, double(compute_ns) / 1e9, 584 (unsigned long long)compute_made, double(graphics_ns) / 1e9, double(compute_ns) / 1e9,
528 (unsigned long long)modules, mib(module_bytes), (unsigned long long)modules_made, 585 (unsigned long long)modules, mib(module_bytes), (unsigned long long)modules_made,
529 mib(module_bytes_made), memory_text.c_str(), facts.describe ? facts.describe().c_str() : ""); 586 mib(module_bytes_made), memory_text.c_str(), facts.describe ? facts.describe().c_str() : "");
587 std::fprintf(stderr, "[rt] mem-probe breakdown t=%.0fs: %s\n", t,
588 memory_breakdown::format(tally, facts.share ? facts.share() : memory_breakdown::DeviceShare{},
589 module_bytes)
590 .c_str());
530 Ranges title = facts.title_ranges ? facts.title_ranges() : Ranges{}; 591 Ranges title = facts.title_ranges ? facts.title_ranges() : Ranges{};
531 std::sort(title.begin(), title.end()); 592 std::sort(title.begin(), title.end());
532 Usage usage[b_count]; 593 Usage usage[b_count];