mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 20:18:37 +00:00
Step C, with the previous WIP commit. - Resource::create_mirror_buffer: a slot-133 buffer over title memory the mirror holds in one chunk is bound to that chunk at heap_base + its title address: no import, no shadow, no per-submission walk or guard of its own. Its GPU VA is the title address; V#s and root views naming it keep that address. Title windows and stream-pool extents are not made for mirrored memory (resolve_title_memory answers the mirror first, since step B). - Write-back (DeviceState::page_mirror_write_back, from write_back_titles after the reaching submission retired): the 256-byte pieces whose home differs from the title's page are written back through kernel_writes, on pages nothing wrote since they were last armed (TitleMirrorTracking::unwritten); a page written on both sides keeps the title's bytes, which are uploaded over the home before the next submission. Ranges are noted (note_title_write, with its caps) where a V#, a writable geometry range, a title-memory resolution or a BVH range is answered by the mirror. - The mirror's tracking is HotColdTracking (src/title_mirror_hot_cold.h); it now answers unwritten() for cold pages from the version its guard was armed at (an additive change to that file and to the interface, default false). - Homes go to device-local memory while VK_EXT_memory_budget keeps 1.5 GiB (or an eighth of the budget) for the title's textures and render targets after a unit, to host memory otherwise, decided per 32 MiB unit and logged when it switches; the mirror's line and the mem-probe item count units of each kind. - The mirror's line adds hot and cold pages, write faults, hot and cold copies, and KiB uploaded a submission and a frame. Not moved: the XMemAlloc heap (HeapCreate places it). RtlCreateHeap over a reservation in the mirror's range faults under Wine (it writes the reserved base), so it stays outside the mirror; slot-133 buffers there keep their shadows. Tests: d3d12_page_mirror_test (Wine, iGPU): a slot-133 buffer over mirrored memory has the title's address as its GPU VA and is no shadow; a GPU write into its home goes back to the title's page (one 256-byte piece), a page written on both sides keeps the title's write. Linux ctest (llvmpipe) 122/122; xmem_test OK. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
126 lines
5.5 KiB
C++
126 lines
5.5 KiB
C++
// leonida::memory_breakdown (src/memory_breakdown.h): the mem probe's breakdown line. The tally of synthetic
|
|
// allocations by consumer adds up to the allocated total, an Owner scope names the allocations made under it and
|
|
// nests, an image's usage picks its kind, and the text names every part. No GPU, no device.
|
|
#include "../src/memory_breakdown.h"
|
|
|
|
#include <cstdint>
|
|
#include <cstdio>
|
|
#include <string>
|
|
#include <thread>
|
|
|
|
#define EXPECT(condition) do { \
|
|
if (!(condition)) { std::fprintf(stderr, "line %d: %s\n", __LINE__, #condition); return 1; } \
|
|
} while (0)
|
|
|
|
namespace {
|
|
|
|
namespace mb = leonida::memory_breakdown;
|
|
constexpr std::uint64_t kMiB = 1 << 20;
|
|
|
|
bool contains(const std::string& text, const char* part) { return text.find(part) != std::string::npos; }
|
|
|
|
// The parts add up to the total, imported memory stays out of it, and an unknown consumer counts as other.
|
|
int tally() {
|
|
mb::Tally t;
|
|
t.add(mb::Consumer::arena, 4095 * kMiB, false);
|
|
t.add(mb::Consumer::geometry, 4 * kMiB, false);
|
|
t.add(mb::Consumer::geometry, 4 * kMiB, false);
|
|
t.add(mb::Consumer::sampled, 3 * kMiB, false);
|
|
t.add(mb::Consumer::render_target, 8 * kMiB, false);
|
|
t.add(mb::Consumer::other, 1 * kMiB, false);
|
|
t.add(mb::Consumer::count, 2 * kMiB, false); // out of range: other
|
|
t.add(mb::Consumer::windows, 6 * kMiB, true); // imported: not allocated by us
|
|
EXPECT(t.total() == (4095 + 8 + 3 + 8 + 1 + 2) * kMiB);
|
|
EXPECT(t.bytes[std::size_t(mb::Consumer::geometry)] == 8 * kMiB);
|
|
EXPECT(t.count[std::size_t(mb::Consumer::geometry)] == 2);
|
|
EXPECT(t.bytes[std::size_t(mb::Consumer::other)] == 3 * kMiB);
|
|
EXPECT(t.count[std::size_t(mb::Consumer::other)] == 2);
|
|
EXPECT(t.bytes[std::size_t(mb::Consumer::windows)] == 0);
|
|
EXPECT(t.imported_bytes == 6 * kMiB && t.imported_count == 1);
|
|
std::uint64_t sum = 0;
|
|
for (std::size_t i = 0; i < mb::kConsumers; ++i) sum += t.bytes[i];
|
|
EXPECT(sum == t.total());
|
|
return 0;
|
|
}
|
|
|
|
// An Owner scope names this thread's allocations, nests, restores the outer name, and does not leak to other threads.
|
|
int owner_scopes() {
|
|
EXPECT(mb::current_owner() == mb::Consumer::other);
|
|
{
|
|
mb::Owner outer(mb::Consumer::geometry);
|
|
EXPECT(mb::current_owner() == mb::Consumer::geometry);
|
|
{
|
|
mb::Owner inner(mb::Consumer::staging);
|
|
EXPECT(mb::current_owner() == mb::Consumer::staging);
|
|
mb::Consumer seen = mb::Consumer::count;
|
|
std::thread([&] { seen = mb::current_owner(); }).join();
|
|
EXPECT(seen == mb::Consumer::other);
|
|
}
|
|
EXPECT(mb::current_owner() == mb::Consumer::geometry);
|
|
}
|
|
EXPECT(mb::current_owner() == mb::Consumer::other);
|
|
return 0;
|
|
}
|
|
|
|
// Colour and depth/stencil attachments are render targets; anything else bound alone is a sampled texture.
|
|
int image_kinds() {
|
|
const std::uint32_t sampled = 0x4, transfer_dst = 0x2, storage = 0x8;
|
|
EXPECT(mb::image_consumer(sampled | transfer_dst) == mb::Consumer::sampled);
|
|
EXPECT(mb::image_consumer(storage) == mb::Consumer::sampled);
|
|
EXPECT(mb::image_consumer(sampled | mb::kColorAttachment) == mb::Consumer::render_target);
|
|
EXPECT(mb::image_consumer(mb::kDepthStencilAttachment) == mb::Consumer::render_target);
|
|
return 0;
|
|
}
|
|
|
|
// The text: the total first, every consumer with its MiB and count, the arena's reserved/bound/in-use bytes, other
|
|
// last, and what lies outside the total.
|
|
int text() {
|
|
mb::Tally t;
|
|
t.add(mb::Consumer::arena, 4095 * kMiB, false);
|
|
t.add(mb::Consumer::geometry, 3867 * kMiB, false);
|
|
t.add(mb::Consumer::render_target, 1180 * kMiB, false);
|
|
t.add(mb::Consumer::other, 819 * kMiB, false);
|
|
t.add(mb::Consumer::other, 206 * kMiB, true);
|
|
mb::DeviceShare share;
|
|
share.arena = true;
|
|
share.arena_reserved = share.arena_bound = 4095 * kMiB;
|
|
share.arena_in_use = 757 * kMiB;
|
|
share.arena_use = {500 * kMiB, 171 * kMiB, 80 * kMiB, 6 * kMiB};
|
|
share.bindless_table_bytes = 2 * kMiB;
|
|
share.mirror = true;
|
|
share.mirror_homes = 32;
|
|
share.mirror_pages = 40;
|
|
share.mirror_chunks = 1;
|
|
const std::string line = mb::format(t, share, 12 * kMiB);
|
|
std::fprintf(stderr, "%s\n", line.c_str());
|
|
EXPECT(line.rfind("allocated 9961 MiB = arena 4095 (1) [reserved 4095, bound 4095, in use 757: buffers 500, "
|
|
"descriptor heaps 171, shader code 80, heaps 6]",
|
|
0) == 0);
|
|
EXPECT(contains(line, " + geometry copies 3867 (1)"));
|
|
EXPECT(contains(line, " + title windows and shadows 0 (0)"));
|
|
EXPECT(contains(line, " + upload heaps 0 (0) + staging 0 (0) + texture blocks 0 (0) + sampled textures 0 (0)"));
|
|
EXPECT(contains(line, " + render targets and depth 1180 (1) + page mirror 0 (0)"));
|
|
EXPECT(contains(line, " 0 host] + other 819 (1);"));
|
|
EXPECT(contains(line, "page mirror 0 (0) [homes bound 2 MiB (32), pages mirrored 40, chunks 1, units 0 "
|
|
"device-local 0 host]"));
|
|
EXPECT(contains(line, "imported 206 MiB (1)"));
|
|
EXPECT(contains(line, "bindless tables 2 MiB"));
|
|
EXPECT(contains(line, "shader modules 12 MiB SPIR-V"));
|
|
// Without an arena the bracket is left out.
|
|
const std::string bare = mb::format(t, mb::DeviceShare{}, 0);
|
|
EXPECT(!contains(bare, "reserved"));
|
|
EXPECT(contains(bare, "arena 4095 (1) + geometry copies"));
|
|
return 0;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
int main() {
|
|
if (int r = tally()) return r;
|
|
if (int r = owner_scopes()) return r;
|
|
if (int r = image_kinds()) return r;
|
|
if (int r = text()) return r;
|
|
std::fprintf(stderr, "memory_breakdown: ok\n");
|
|
return 0;
|
|
}
|