diff options
Diffstat (limited to 'runtime/test/gpu_label_memory_test.cpp')
| -rw-r--r-- | runtime/test/gpu_label_memory_test.cpp | 194 |
1 files changed, 194 insertions, 0 deletions
diff --git a/runtime/test/gpu_label_memory_test.cpp b/runtime/test/gpu_label_memory_test.cpp new file mode 100644 index 000000000..db485ba3d --- /dev/null +++ b/runtime/test/gpu_label_memory_test.cpp | |||
| @@ -0,0 +1,194 @@ | |||
| 1 | // Portable unit and concurrency tests for leonida::label::Memory (src/gpu_label_memory.h). | ||
| 2 | // Covers in-place non-heap region expansion (preserving in-flight GPU completion value writes), | ||
| 3 | // partial vs entire page release generation invalidation, heap vs non-heap isolation, base-0 GPU arena | ||
| 4 | // tracking, alignment/overflow bounds, and concurrent track/capture/publish. No Windows/Vulkan/game data. | ||
| 5 | #include "../src/gpu_label_memory.h" | ||
| 6 | |||
| 7 | #include <atomic> | ||
| 8 | #include <cstdint> | ||
| 9 | #include <cstdio> | ||
| 10 | #include <limits> | ||
| 11 | #include <thread> | ||
| 12 | #include <vector> | ||
| 13 | |||
| 14 | #define EXPECT(condition) do { \ | ||
| 15 | if (!(condition)) { std::fprintf(stderr, "line %d: %s\n", __LINE__, #condition); return 1; } \ | ||
| 16 | } while (0) | ||
| 17 | |||
| 18 | namespace { | ||
| 19 | |||
| 20 | using leonida::label::Memory; | ||
| 21 | using leonida::label::Target; | ||
| 22 | |||
| 23 | bool publish_ok(Memory& mem, const Target& target, std::uintptr_t expected_addr, unsigned expected_bytes, | ||
| 24 | std::uintptr_t expected_alloc) { | ||
| 25 | bool called = false; | ||
| 26 | const bool ok = mem.publish(target, [&](std::uintptr_t addr, unsigned bytes, std::uintptr_t alloc) { | ||
| 27 | called = true; | ||
| 28 | return addr == expected_addr && bytes == expected_bytes && alloc == expected_alloc; | ||
| 29 | }); | ||
| 30 | return ok && called; | ||
| 31 | } | ||
| 32 | |||
| 33 | bool publish_rejected(Memory& mem, const Target& target) { | ||
| 34 | bool called = false; | ||
| 35 | const bool ok = mem.publish(target, [&](std::uintptr_t, unsigned, std::uintptr_t) { | ||
| 36 | called = true; | ||
| 37 | return true; | ||
| 38 | }); | ||
| 39 | return !ok && !called; | ||
| 40 | } | ||
| 41 | |||
| 42 | int test_expand_preserves_inflight_targets() { | ||
| 43 | Memory mem; | ||
| 44 | constexpr std::uintptr_t kBase = 0x100000; | ||
| 45 | constexpr std::uintptr_t kAlloc = 0x100000; | ||
| 46 | mem.track(kBase, 0x1000, kAlloc, false); | ||
| 47 | |||
| 48 | const Target t4 = mem.capture(kBase + 0x10, 4); | ||
| 49 | const Target t8 = mem.capture(kBase + 0x20, 8); | ||
| 50 | EXPECT(t4.owner.lock() != nullptr && t8.owner.lock() != nullptr); | ||
| 51 | EXPECT(mem.capture(kBase + 0x1000, 8).owner.lock() == nullptr); | ||
| 52 | |||
| 53 | // Growing the same non-heap region at the same base and allocation_base must update Region::end in place | ||
| 54 | // so in-flight Targets remain valid instead of being silently dropped. | ||
| 55 | mem.track(kBase, 0x4000, kAlloc, false); | ||
| 56 | EXPECT(publish_ok(mem, t4, kBase + 0x10, 4, kAlloc)); | ||
| 57 | EXPECT(publish_ok(mem, t8, kBase + 0x20, 8, kAlloc)); | ||
| 58 | |||
| 59 | // The newly covered tail is immediately capturable and publishable too. | ||
| 60 | const Target t_tail = mem.capture(kBase + 0x3ff8, 8); | ||
| 61 | EXPECT(publish_ok(mem, t_tail, kBase + 0x3ff8, 8, kAlloc)); | ||
| 62 | |||
| 63 | // Partial release bumps generation: pre-release targets become stale, post-release targets survive further | ||
| 64 | // in-place expansion while the pre-release stale target remains rejected. | ||
| 65 | EXPECT(mem.release_pages(kBase, 0x1000, false, [] { return true; })); | ||
| 66 | EXPECT(publish_rejected(mem, t4)); | ||
| 67 | EXPECT(publish_rejected(mem, t_tail)); | ||
| 68 | |||
| 69 | const Target t_gen2 = mem.capture(kBase + 0x10, 8); | ||
| 70 | EXPECT(t_gen2.generation == t4.generation + 1); | ||
| 71 | mem.track(kBase, 0x8000, kAlloc, false); | ||
| 72 | EXPECT(publish_rejected(mem, t4)); | ||
| 73 | EXPECT(publish_ok(mem, t_gen2, kBase + 0x10, 8, kAlloc)); | ||
| 74 | return 0; | ||
| 75 | } | ||
| 76 | |||
| 77 | int test_subrange_and_adjacent_page_isolation() { | ||
| 78 | Memory mem; | ||
| 79 | constexpr std::uintptr_t kBase = 0x200000; | ||
| 80 | constexpr std::uintptr_t kAlloc = 0x200000; | ||
| 81 | mem.track(kBase, 0x2000, kAlloc, false); | ||
| 82 | const Target t0 = mem.capture(kBase + 0x100, 8); | ||
| 83 | |||
| 84 | // Re-tracking a covered subrange does not disturb existing targets. | ||
| 85 | mem.track(kBase + 0x1000, 0x1000, kAlloc, false); | ||
| 86 | EXPECT(publish_ok(mem, t0, kBase + 0x100, 8, kAlloc)); | ||
| 87 | |||
| 88 | // An adjacent region at kBase + 0x2000 stays separate so partially releasing page 2 does not falsely | ||
| 89 | // invalidate live targets in [kBase, kBase + 0x2000). | ||
| 90 | mem.track(kBase + 0x2000, 0x1000, kAlloc, false); | ||
| 91 | const Target t_adj = mem.capture(kBase + 0x2008, 8); | ||
| 92 | EXPECT(publish_ok(mem, t_adj, kBase + 0x2008, 8, kAlloc)); | ||
| 93 | |||
| 94 | EXPECT(mem.release_pages(kBase + 0x2000, 0x1000, false, [] { return true; })); | ||
| 95 | EXPECT(publish_rejected(mem, t_adj)); | ||
| 96 | EXPECT(publish_ok(mem, t0, kBase + 0x100, 8, kAlloc)); | ||
| 97 | return 0; | ||
| 98 | } | ||
| 99 | |||
| 100 | int test_allocation_base_and_heap_invalidation() { | ||
| 101 | Memory mem; | ||
| 102 | constexpr std::uintptr_t kBase = 0x300000; | ||
| 103 | mem.track(kBase, 0x1000, kBase, false); | ||
| 104 | const Target t_old = mem.capture(kBase + 8, 8); | ||
| 105 | |||
| 106 | // Different allocation_base at the same base represents a new reservation and invalidates prior targets. | ||
| 107 | mem.track(kBase, 0x2000, kBase + 0x80000, false); | ||
| 108 | EXPECT(publish_rejected(mem, t_old)); | ||
| 109 | const Target t_new = mem.capture(kBase + 8, 8); | ||
| 110 | EXPECT(publish_ok(mem, t_new, kBase + 8, 8, kBase + 0x80000)); | ||
| 111 | |||
| 112 | // Failed release callback must leave regions and generations untouched. | ||
| 113 | EXPECT(!mem.release_pages(kBase + 0x80000, 0, true, [] { return false; })); | ||
| 114 | EXPECT(publish_ok(mem, t_new, kBase + 8, 8, kBase + 0x80000)); | ||
| 115 | |||
| 116 | // Heap regions are isolated from release_pages and invalidated by release_heap or re-track. | ||
| 117 | constexpr std::uintptr_t kHeap = 0x400000; | ||
| 118 | mem.track(kHeap, 0x1000, kBase + 0x80000, true); | ||
| 119 | const Target t_heap = mem.capture(kHeap + 16, 4); | ||
| 120 | EXPECT(mem.release_pages(kBase + 0x80000, 0, true, [] { return true; })); | ||
| 121 | EXPECT(publish_rejected(mem, t_new)); | ||
| 122 | EXPECT(publish_ok(mem, t_heap, kHeap + 16, 4, kBase + 0x80000)); | ||
| 123 | |||
| 124 | mem.release_heap(kHeap); | ||
| 125 | EXPECT(publish_rejected(mem, t_heap)); | ||
| 126 | return 0; | ||
| 127 | } | ||
| 128 | |||
| 129 | int test_arena_base_zero_and_bounds() { | ||
| 130 | Memory mem; | ||
| 131 | constexpr std::uintptr_t kArenaHost = 0x500000; | ||
| 132 | mem.track(0, 0x10000, kArenaHost, false); | ||
| 133 | const Target t0 = mem.capture(0x100, 8); | ||
| 134 | EXPECT(publish_ok(mem, t0, 0x100, 8, kArenaHost)); | ||
| 135 | |||
| 136 | // Expanding base-0 GPU arena preserves in-flight targets. | ||
| 137 | mem.track(0, 0x20000, kArenaHost, false); | ||
| 138 | EXPECT(publish_ok(mem, t0, 0x100, 8, kArenaHost)); | ||
| 139 | const Target t_high = mem.capture(0x1fff8, 8); | ||
| 140 | EXPECT(publish_ok(mem, t_high, 0x1fff8, 8, kArenaHost)); | ||
| 141 | |||
| 142 | // Invalid track and capture calls are rejected cleanly. | ||
| 143 | mem.track(0x900000, 0, 0x900000, false); | ||
| 144 | mem.track(0x900000, 0x1000, 0, false); | ||
| 145 | mem.track(std::numeric_limits<std::uintptr_t>::max() - 0x10, 0x100, 0x900000, false); | ||
| 146 | EXPECT(mem.capture(0x900000, 4).owner.lock() == nullptr); | ||
| 147 | EXPECT(mem.capture(0x100, 2).owner.lock() == nullptr); | ||
| 148 | EXPECT(mem.capture(0x100, 16).owner.lock() == nullptr); | ||
| 149 | EXPECT(mem.capture(0x102, 4).owner.lock() == nullptr); | ||
| 150 | EXPECT(mem.capture(0x104, 8).owner.lock() == nullptr); | ||
| 151 | EXPECT(mem.capture(0x1fffc, 8).owner.lock() == nullptr); // straddles end (0x20000) | ||
| 152 | return 0; | ||
| 153 | } | ||
| 154 | |||
| 155 | int test_concurrent_expand_and_publish() { | ||
| 156 | Memory mem; | ||
| 157 | constexpr std::uintptr_t kBase = 0x600000; | ||
| 158 | constexpr std::uintptr_t kAlloc = 0x600000; | ||
| 159 | mem.track(kBase, 0x1000, kAlloc, false); | ||
| 160 | |||
| 161 | std::atomic<bool> failed{false}; | ||
| 162 | std::thread expander([&] { | ||
| 163 | for (std::uintptr_t pages = 1; pages <= 256; ++pages) mem.track(kBase, pages * 0x1000, kAlloc, false); | ||
| 164 | }); | ||
| 165 | std::vector<std::thread> workers; | ||
| 166 | for (int w = 0; w < 3; ++w) { | ||
| 167 | workers.emplace_back([&, w] { | ||
| 168 | const std::uintptr_t addr = kBase + static_cast<std::uintptr_t>(w) * 16; | ||
| 169 | for (int i = 0; i < 2000; ++i) { | ||
| 170 | const Target t = mem.capture(addr, 8); | ||
| 171 | if (!publish_ok(mem, t, addr, 8, kAlloc)) { | ||
| 172 | failed.store(true, std::memory_order_relaxed); | ||
| 173 | return; | ||
| 174 | } | ||
| 175 | } | ||
| 176 | }); | ||
| 177 | } | ||
| 178 | expander.join(); | ||
| 179 | for (auto& worker : workers) worker.join(); | ||
| 180 | EXPECT(!failed.load()); | ||
| 181 | return 0; | ||
| 182 | } | ||
| 183 | |||
| 184 | } // namespace | ||
| 185 | |||
| 186 | int main() { | ||
| 187 | if (int r = test_expand_preserves_inflight_targets()) return r; | ||
| 188 | if (int r = test_subrange_and_adjacent_page_isolation()) return r; | ||
| 189 | if (int r = test_allocation_base_and_heap_invalidation()) return r; | ||
| 190 | if (int r = test_arena_base_zero_and_bounds()) return r; | ||
| 191 | if (int r = test_concurrent_expand_and_publish()) return r; | ||
| 192 | std::printf("gpu_label_memory_test: ok\n"); | ||
| 193 | return 0; | ||
| 194 | } | ||