upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/test/memory_breakdown_test.cpp
diff options
context:
space:
mode:
authorJan Trencansky <jan.trencansky@protonmail.com>2026-11-26 14:31:34 -0500
committerJan Trencansky <jan.trencansky@protonmail.com>2026-11-26 14:31:34 -0500
commitaaff77b831a5167b4b255fe92b7cde4add593b98 (patch)
treede62c97ea795078ad9e199fdfacb53092d3eff8c /runtime/test/memory_breakdown_test.cpp
parent2d43417d49fdd8f2d8241f75ea8fd0cdd8811137 (diff)
mem probe: a breakdown line of the runtime's Vulkan memory by consumer (RT-MEMORY-BREAKDOWN-AND-SPARSE-ARENA)
The `mem-probe t=` line sums our vkAllocateMemory bytes per memory type, but not who holds them: in a late world run 7.5 GB of device-local memory and up to 1.2 GB of the Resizable BAR type could not be attributed. Every tick now also prints [rt] mem-probe breakdown t=<s>: allocated N MiB = arena A (n) [reserved, bound, in use: buffers, descriptor heaps, shader code, heaps] + geometry copies + title windows and shadows + upload heaps + staging + texture blocks + sampled textures + render targets and depth + other; imported M MiB; not in the total: bindless tables, shader modules, pipelines and swapchain images whose parts add up to the allocated bytes of the t= line. Diagnostic only: nothing changes what is allocated or where, and with LEONIDA_MEM_PROBE unset nothing is installed. - src/memory_breakdown.h (new, standard library only): the consumers, an Owner scope (thread-local) that names the allocations a thread makes while it lives, the tally and the line's text. - src/mem_probe.h: the vkAllocateMemory wrapper records the thread's owner; vkCreateImage/vkDestroyImage/ vkBindImageMemory are wrapped too, so memory nobody named that a dedicated image is bound to counts as a render target or depth buffer (attachment usage) or a sampled texture. Named blocks keep their name. - Owner scopes at the allocation sites: the arena (create_arena), geometry copies (GeometryViews' pool), title windows outside the arena and slot-133 shadow buffers with their own memory, upload/readback heap buffers, texture staging and copy scratch, and the texture blocks (ImageMemory, TitleImagePool). - The device adds the arena's reserved, bound and in-use bytes (by kind) and the bindless table bytes, read under the arena's and the descriptor locks; BindlessSet allocates through its own function table, so its bytes are shown beside the total, not in it. - runtime/README.md: LEONIDA_MEM_PROBE's row names the three lines. Test: memory_breakdown_test (CPU): the tally sums to the total with imported memory kept apart, owner scopes nest and stay on their thread, image usage picks the kind, and the text lists every part. It does not build on main (the header is new). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/test/memory_breakdown_test.cpp')
-rw-r--r--runtime/test/memory_breakdown_test.cpp118
1 files changed, 118 insertions, 0 deletions
diff --git a/runtime/test/memory_breakdown_test.cpp b/runtime/test/memory_breakdown_test.cpp
new file mode 100644
index 000000000..d27a1166d
--- /dev/null
+++ b/runtime/test/memory_breakdown_test.cpp
@@ -0,0 +1,118 @@
1// leonida::memory_breakdown (src/memory_breakdown.h): the mem probe's breakdown line. The tally of synthetic
2// allocations by consumer adds up to the allocated total, an Owner scope names the allocations made under it and
3// nests, an image's usage picks its kind, and the text names every part. No GPU, no device.
4#include "../src/memory_breakdown.h"
5
6#include <cstdint>
7#include <cstdio>
8#include <string>
9#include <thread>
10
11#define EXPECT(condition) do { \
12 if (!(condition)) { std::fprintf(stderr, "line %d: %s\n", __LINE__, #condition); return 1; } \
13} while (0)
14
15namespace {
16
17namespace mb = leonida::memory_breakdown;
18constexpr std::uint64_t kMiB = 1 << 20;
19
20bool contains(const std::string& text, const char* part) { return text.find(part) != std::string::npos; }
21
22// The parts add up to the total, imported memory stays out of it, and an unknown consumer counts as other.
23int tally() {
24 mb::Tally t;
25 t.add(mb::Consumer::arena, 4095 * kMiB, false);
26 t.add(mb::Consumer::geometry, 4 * kMiB, false);
27 t.add(mb::Consumer::geometry, 4 * kMiB, false);
28 t.add(mb::Consumer::sampled, 3 * kMiB, false);
29 t.add(mb::Consumer::render_target, 8 * kMiB, false);
30 t.add(mb::Consumer::other, 1 * kMiB, false);
31 t.add(mb::Consumer::count, 2 * kMiB, false); // out of range: other
32 t.add(mb::Consumer::windows, 6 * kMiB, true); // imported: not allocated by us
33 EXPECT(t.total() == (4095 + 8 + 3 + 8 + 1 + 2) * kMiB);
34 EXPECT(t.bytes[std::size_t(mb::Consumer::geometry)] == 8 * kMiB);
35 EXPECT(t.count[std::size_t(mb::Consumer::geometry)] == 2);
36 EXPECT(t.bytes[std::size_t(mb::Consumer::other)] == 3 * kMiB);
37 EXPECT(t.count[std::size_t(mb::Consumer::other)] == 2);
38 EXPECT(t.bytes[std::size_t(mb::Consumer::windows)] == 0);
39 EXPECT(t.imported_bytes == 6 * kMiB && t.imported_count == 1);
40 std::uint64_t sum = 0;
41 for (std::size_t i = 0; i < mb::kConsumers; ++i) sum += t.bytes[i];
42 EXPECT(sum == t.total());
43 return 0;
44}
45
46// An Owner scope names this thread's allocations, nests, restores the outer name, and does not leak to other threads.
47int owner_scopes() {
48 EXPECT(mb::current_owner() == mb::Consumer::other);
49 {
50 mb::Owner outer(mb::Consumer::geometry);
51 EXPECT(mb::current_owner() == mb::Consumer::geometry);
52 {
53 mb::Owner inner(mb::Consumer::staging);
54 EXPECT(mb::current_owner() == mb::Consumer::staging);
55 mb::Consumer seen = mb::Consumer::count;
56 std::thread([&] { seen = mb::current_owner(); }).join();
57 EXPECT(seen == mb::Consumer::other);
58 }
59 EXPECT(mb::current_owner() == mb::Consumer::geometry);
60 }
61 EXPECT(mb::current_owner() == mb::Consumer::other);
62 return 0;
63}
64
65// Colour and depth/stencil attachments are render targets; anything else bound alone is a sampled texture.
66int image_kinds() {
67 const std::uint32_t sampled = 0x4, transfer_dst = 0x2, storage = 0x8;
68 EXPECT(mb::image_consumer(sampled | transfer_dst) == mb::Consumer::sampled);
69 EXPECT(mb::image_consumer(storage) == mb::Consumer::sampled);
70 EXPECT(mb::image_consumer(sampled | mb::kColorAttachment) == mb::Consumer::render_target);
71 EXPECT(mb::image_consumer(mb::kDepthStencilAttachment) == mb::Consumer::render_target);
72 return 0;
73}
74
75// The text: the total first, every consumer with its MiB and count, the arena's reserved/bound/in-use bytes, other
76// last, and what lies outside the total.
77int text() {
78 mb::Tally t;
79 t.add(mb::Consumer::arena, 4095 * kMiB, false);
80 t.add(mb::Consumer::geometry, 3867 * kMiB, false);
81 t.add(mb::Consumer::render_target, 1180 * kMiB, false);
82 t.add(mb::Consumer::other, 819 * kMiB, false);
83 t.add(mb::Consumer::other, 206 * kMiB, true);
84 mb::DeviceShare share;
85 share.arena = true;
86 share.arena_reserved = share.arena_bound = 4095 * kMiB;
87 share.arena_in_use = 757 * kMiB;
88 share.arena_use = {500 * kMiB, 171 * kMiB, 80 * kMiB, 6 * kMiB};
89 share.bindless_table_bytes = 2 * kMiB;
90 const std::string line = mb::format(t, share, 12 * kMiB);
91 std::fprintf(stderr, "%s\n", line.c_str());
92 EXPECT(line.rfind("allocated 9961 MiB = arena 4095 (1) [reserved 4095, bound 4095, in use 757: buffers 500, "
93 "descriptor heaps 171, shader code 80, heaps 6]",
94 0) == 0);
95 EXPECT(contains(line, " + geometry copies 3867 (1)"));
96 EXPECT(contains(line, " + title windows and shadows 0 (0)"));
97 EXPECT(contains(line, " + upload heaps 0 (0) + staging 0 (0) + texture blocks 0 (0) + sampled textures 0 (0)"));
98 EXPECT(contains(line, " + render targets and depth 1180 (1) + other 819 (1);"));
99 EXPECT(contains(line, "imported 206 MiB (1)"));
100 EXPECT(contains(line, "bindless tables 2 MiB"));
101 EXPECT(contains(line, "shader modules 12 MiB SPIR-V"));
102 // Without an arena the bracket is left out.
103 const std::string bare = mb::format(t, mb::DeviceShare{}, 0);
104 EXPECT(!contains(bare, "reserved"));
105 EXPECT(contains(bare, "arena 4095 (1) + geometry copies"));
106 return 0;
107}
108
109} // namespace
110
111int main() {
112 if (int r = tally()) return r;
113 if (int r = owner_scopes()) return r;
114 if (int r = image_kinds()) return r;
115 if (int r = text()) return r;
116 std::fprintf(stderr, "memory_breakdown: ok\n");
117 return 0;
118}