Files
mac_linuxgpu/hsa/tests/test_allocation_census.cpp
geramyloveless 604239afb9 hsa: a census of the buffers each connection holds, and who asked
The driver says how much VRAM the process holds; nothing said what holds
it. Each connection now records every VRAM and shared buffer it gets
from the driver with the return addresses of the allocating call, and
reports buffer counts and bytes, a VRAM size histogram and the callers
holding the most VRAM (frames named by dladdr where exported, otherwise
image+offset for atos).

The report is printed with every refused allocation and is available
as mac_hsa_memory_report(); MAC_HSA_VRAM_TRACE=1 also logs each buffer
as it is allocated and freed.
2026-10-03 09:29:01 -07:00

47 lines
2.2 KiB
C++

// Host unit test: the allocation census (src/allocation_census.h) groups live
// buffers by the call that allocated them, sizes them, and forgets them when
// they are freed.
#include "allocation_census.h"
#include <cstdio>
#include <string>
static int failures = 0;
#define CHECK(cond, msg) do { \
if (!(cond)) { std::fprintf(stderr, "FAIL: %s (line %d)\n", msg, __LINE__); ++failures; } \
else { std::fprintf(stderr, " ok: %s\n", msg); } \
} while (0)
using mac_hsa::AllocationCensus;
__attribute__((noinline)) void weightsSite(AllocationCensus &c, uint64_t handle) {
c.add(handle, AllocationCensus::Kind::VRAM, 512ull << 20, 0);
}
__attribute__((noinline)) void slabSite(AllocationCensus &c, uint64_t handle) {
c.add(handle, AllocationCensus::Kind::VRAM, 64ull << 20, 0);
}
int main() {
AllocationCensus c;
for (uint64_t h = 1; h <= 3; ++h) weightsSite(c, h);
for (uint64_t h = 10; h < 20; ++h) slabSite(c, h);
c.add(100, AllocationCensus::Kind::Shared, 16384);
CHECK(c.count(AllocationCensus::Kind::VRAM) == 13, "13 VRAM buffers");
CHECK(c.bytes(AllocationCensus::Kind::VRAM) == (3 * 512ull + 10 * 64ull) << 20, "VRAM bytes");
CHECK(c.count(AllocationCensus::Kind::Shared) == 1 && c.bytes(AllocationCensus::Kind::Shared) == 16384,
"shared counted apart");
const std::string report = c.report();
std::fputs(report.c_str(), stderr);
CHECK(report.find("13 VRAM buffers") != std::string::npos, "report totals");
CHECK(report.find("=64M 10 (640.0 MiB)") != std::string::npos, "64 MiB slabs bucketed");
CHECK(report.find(">=512M 3 (1536.0 MiB)") != std::string::npos, "large buffers bucketed");
CHECK(report.find("3 VRAM buffers, 1536.0 MiB, from") != std::string::npos &&
report.find("10 VRAM buffers, 640.0 MiB, from") != std::string::npos,
"grouped by allocating call, largest first");
CHECK(report.find("1536.0 MiB, from") < report.find("640.0 MiB, from"), "largest group first");
for (uint64_t h = 10; h < 20; ++h) c.remove(h);
c.remove(999);
CHECK(c.count(AllocationCensus::Kind::VRAM) == 3, "freed buffers leave the census");
std::fprintf(stderr, failures ? "%d FAILURE(S)\n" : "all census checks passed\n", failures);
return failures ? 1 : 0;
}