Files
Triforce-Source/gpu/vk/ram_census.h
ruckus7382sfgsgdf dad28e2d4b Triforce v0.9.1
The 16 GB update: idle memory unlocked and handed back to Windows while playing, empty pages under freed locks returned, and a page-file check at launch that warns before a too-small page file can crash the game.
2026-12-01 17:47:00 -06:00

108 lines
5.1 KiB
C++

// (Triforce, 12-01) The RAM census, RX_VK_RAM_CENSUS=1 (off unless set): what the GPU does with the game's memory that
// the renderer imports.
//
// An import (vk_memory.cpp import_pool) hands a range of the game's memory to the driver as host memory; the driver locks
// those pages, so Windows can never trim them out of RAM or page them out. In the 11-30 play log 8,256 MB of the game's
// 10,240 MB in RAM were imported. This census says, for every import, how much of it the GPU reaches and how:
// - kBindNarrow: a buffer binding of at most 16 MB reached the page (vertex, index, uniform, storage, indirect...)
// - kBindWide: only a wider binding did (a whole pool bound for a few bytes: an upper bound of what it reads)
// - kBindWrite: a binding the GPU writes through reached it
// - kTexSampled: the storage of a texture the renderer made to sample (the image in VRAM is made from these bytes)
// - kTexTarget: the storage of a render target or a texture the GPU draws into
// A page is 64 KB of guest address (addresses below 1 TB, the native pools'; a virtual pool's texture is marked page by page
// through its mapping). The bits are never cleared, so the census holds everything since the start of the run.
// Once in a while (the memory line's thread, every 600 presents) triforce_ram_census.txt beside the exe gets one line an import
// and the totals, and the log one summary line. Costs only with the switch on: a load of `on` at each binding otherwise.
#pragma once
#include <algorithm>
#include <atomic>
#include <cstdint>
#include <cstdlib>
#include <memory>
namespace ram_census {
enum Kind : int { kBindNarrow, kBindWide, kBindWrite, kTexSampled, kTexTarget, kKinds };
inline constexpr int kPageShift = 16;
inline constexpr uint64_t kPages = 1ull << (40 - kPageShift); // (2^24 pages: 2 MB of bits a kind)
inline constexpr uint64_t kWords = kPages / 64;
inline constexpr uint64_t kNarrowMax = 16ull << 20;
inline bool on() {
static const bool v = [] {
const char* e = getenv("RX_VK_RAM_CENSUS");
return e && atoi(e) != 0;
}();
return v;
}
struct Maps {
std::unique_ptr<std::atomic<uint64_t>[]> bits[kKinds];
Maps() {
for (auto& b : bits) {
b.reset(new std::atomic<uint64_t>[kWords]);
for (uint64_t i = 0; i < kWords; i++) b[i].store(0, std::memory_order_relaxed);
}
}
};
inline Maps& maps() {
static Maps m;
return m;
}
/// Mark the pages of guest [lo, lo + size) as `k` (a word whose bits are all set already is not written)
inline void mark(Kind k, uint64_t lo, uint64_t size) {
if (!size || lo >= (1ull << 40)) return;
uint64_t hi = lo + size;
if (hi > (1ull << 40)) hi = 1ull << 40;
const uint64_t p0 = lo >> kPageShift, p1 = (hi - 1) >> kPageShift;
std::atomic<uint64_t>* b = maps().bits[k].get();
for (uint64_t w = p0 / 64; w <= p1 / 64; w++) {
const uint64_t first = w * 64, last = first + 63;
const uint64_t a = p0 > first ? p0 - first : 0, z = p1 < last ? p1 - first : 63;
const uint64_t mask = (z == 63 ? ~0ull : ((1ull << (z + 1)) - 1)) & ~((1ull << a) - 1);
if ((b[w].load(std::memory_order_relaxed) & mask) != mask) b[w].fetch_or(mask, std::memory_order_relaxed);
}
}
inline bool test(Kind k, uint64_t page) {
return (maps().bits[k][page / 64].load(std::memory_order_relaxed) >> (page % 64)) & 1;
}
/// A buffer binding of guest [lo, lo + size)
inline void bind(uint64_t lo, uint64_t size, bool written) {
mark(size <= kNarrowMax ? kBindNarrow : kBindWide, lo, size);
if (written) mark(kBindWrite, lo, size);
}
/// The bytes of [lo, hi) by what reached them: the classes the census file and the line give
struct Count {
uint64_t total = 0;
uint64_t narrow = 0; // a narrow binding reached it (whatever else did)
uint64_t wide_only = 0; // only a wide binding reached it, no texture
uint64_t tex_only = 0; // a sampled texture's storage, no binding
uint64_t target_only = 0; // a render target's storage, no binding
uint64_t tex_and_wide = 0; // a texture's storage that only a wide binding also reached
uint64_t written = 0; // a binding the GPU writes through reached it
uint64_t none = 0; // nothing: no binding, no texture
};
inline void count(uint64_t lo, uint64_t hi, Count* c) {
if (lo >= (1ull << 40)) return;
if (hi > (1ull << 40)) hi = 1ull << 40;
for (uint64_t page = lo >> kPageShift; (page << kPageShift) < hi; page++) {
const uint64_t a = std::max(lo, page << kPageShift), z = std::min(hi, (page + 1) << kPageShift);
const uint64_t n = z - a;
c->total += n;
const bool nar = test(kBindNarrow, page), wide = test(kBindWide, page), ts = test(kTexSampled, page),
tt = test(kTexTarget, page);
if (test(kBindWrite, page)) c->written += n;
if (nar) c->narrow += n;
else if (wide && (ts || tt)) c->tex_and_wide += n;
else if (wide) c->wide_only += n;
else if (ts) c->tex_only += n;
else if (tt) c->target_only += n;
else c->none += n;
}
}
} // namespace ram_census