Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion runtime/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ ones marked *diagnostic* only add log lines or exist to compare a run with and w
| `LEONIDA_INPUT_TRACE` | unset | `1` (diagnostic, the GameInput shim built without SDL3) logs the title's reading calls on the scripted pad: the first 128, then each one with a button down and every 1000th. Details in [docs/gameinput.md](../docs/gameinput.md#scripted-input-without-sdl3). |
| `LEONIDA_LOG_ALLOCINFO` | unset | *Diagnostic.* `1` logs every device slot 25 call (`GetResourceAllocationInfo`) with its thread, description and sizes (`shims/d3d12_xs_device.cpp`). |
| `LEONIDA_LOG_EXE_PIPELINES` | unset | *Diagnostic.* When set, one line per distinct graphics pipeline that uses a program translated from the executable's inline containers. |
| `LEONIDA_MEM_PROBE` | unset | *Diagnostic.* `<seconds>`: every that many seconds the memory probe (`src/mem_probe.h`) logs `[rt] mem-probe t=` (live pipelines, shader modules and `VkDeviceMemory` per memory type), `[rt] mem-probe breakdown t=` (the same allocated bytes by consumer: the arena with its reserved, bound and in-use MiB, geometry copies, title windows and shadows, upload heaps, staging, texture blocks, sampled textures, render targets and depth, and `other`, which add up to the allocated total; `src/memory_breakdown.h`) and `[rt] mem-probe smaps` (the process's resident memory by kind). |
| `LEONIDA_MEM_PROBE` | unset | *Diagnostic.* `<seconds>`: every that many seconds the memory probe (`src/mem_probe.h`) logs `[rt] mem-probe t=` (live pipelines, shader modules and `VkDeviceMemory` per memory type), `[rt] mem-probe breakdown t=` (the same allocated bytes by consumer: the arena with its reserved, bound and in-use MiB, geometry copies, title windows and shadows, upload heaps, staging, texture blocks, sampled textures, render targets and depth, and `other`, which add up to the allocated total, each with the part in host memory when it has one, e.g. `sampled textures 2300 (412; 870 in host memory)` (MiB, then the allocation count and the MiB of it in host memory), then `in host memory` for all of them with their allocation count: memory types without DEVICE_LOCAL, which the GPU reads over the bus, not the BAR window; each figure is rounded to a MiB on its own; `src/memory_breakdown.h`) and `[rt] mem-probe smaps` (the process's resident memory by kind). |
| `LEONIDA_MEM_PROBE_CACHE` | unset | *Diagnostic.* `1` adds the pipeline cache's size to the memory probe's report; the driver serializes the whole cache to answer. |
| `LEONIDA_TEXTURE_PROBE` | unset | *Diagnostic.* Logs a `[rt] texture-probe` line for every shader resource view of a texture in the title's memory. |
| `LEONIDA_LOCAL_DIAGNOSTICS` | unset | *Diagnostic.* `1` turns on the boot diagnostics that write to stderr from the draw, clear, present and descriptor paths (the lines tagged `present-probe`, `fsdraw`, `heap-probe`, `desc-probe` and `PresentX diag`; `src/local_diagnostics.h`). They follow a frame of the title from its draws to the window and cost a lock and a line per draw, so they are off by default. The full-screen draw targets that `LEONIDA_CAPTURE_SOURCE` reads back are only collected with it. |
Expand Down
4 changes: 3 additions & 1 deletion runtime/src/mem_probe.h
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,9 @@ inline void sample(const DeviceFacts& facts) {
type_bytes[i] += a.bytes;
}
if (a.mapped && !a.imported) vk_mapped.push_back({a.mapped, a.mapped + a.mapped_bytes});
tally.add(a.consumer, a.bytes, a.imported);
const bool host = memory_breakdown::in_host_memory(
i, facts.memory.memoryTypeCount, i < VK_MAX_MEMORY_TYPES ? facts.memory.memoryTypes[i].propertyFlags : 0);
tally.add(a.consumer, a.bytes, a.imported, host);
}
modules = s.modules.size();
module_bytes = s.module_bytes;
Expand Down
48 changes: 43 additions & 5 deletions runtime/src/memory_breakdown.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
// buffer when the image is an attachment, else a sampled texture (image_consumer()).
// Memory named neither way is "other", so the parts always sum to the total.
//
// Each part also says how much of it is in host memory: a memory type outside the device-local heaps, which the GPU
// reads over the bus (a texture that did not fit in the card's memory, the arena on a type without DEVICE_LOCAL).
//
// Pure standard library (the usage bits are passed in as plain integers), so the tally and the text are tested
// without a GPU or Windows (memory_breakdown_test).

Expand Down Expand Up @@ -57,6 +60,15 @@ inline const char* consumer_name(Consumer c) {
// VkImageUsageFlagBits values, as plain integers so this header needs no Vulkan headers.
constexpr std::uint32_t kColorAttachment = 0x10; // VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
constexpr std::uint32_t kDepthStencilAttachment = 0x20; // VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT
// VkMemoryPropertyFlagBits value, likewise.
constexpr std::uint32_t kDeviceLocal = 0x1; // VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT

// Whether an allocation of memory type `type` counts as in host memory: a type the device reports (type < type_count)
// whose flags lack DEVICE_LOCAL, so the GPU reads it over the bus. A device-local type that the CPU can also map (the
// BAR window, Resizable BAR) is the card's memory; a type index the device does not report is not counted as host.
inline bool in_host_memory(std::uint32_t type, std::uint32_t type_count, std::uint32_t property_flags) {
return type < type_count && !(property_flags & kDeviceLocal);
}

// The consumer of memory an image of `usage` is bound to, when nobody named the memory.
inline Consumer image_consumer(std::uint32_t usage) {
Expand All @@ -83,12 +95,14 @@ class Owner {
};

// Live allocations per consumer. Imported memory (host pointers, the title's own pages) is system memory the runtime
// did not allocate, so it is kept apart, as the probe's total keeps it apart.
// did not allocate, so it is kept apart, as the probe's total keeps it apart. `host`: the allocation's memory type is
// not DEVICE_LOCAL; host_bytes and host_count are the part of bytes and count in such types.
struct Tally {
std::array<std::uint64_t, kConsumers> bytes{}, count{};
std::array<std::uint64_t, kConsumers> host_bytes{}, host_count{};
std::uint64_t imported_bytes = 0, imported_count = 0;

void add(Consumer consumer, std::uint64_t size, bool imported) {
void add(Consumer consumer, std::uint64_t size, bool imported, bool host = false) {
if (imported) {
imported_bytes += size;
++imported_count;
Expand All @@ -97,12 +111,21 @@ struct Tally {
const std::size_t i = std::size_t(consumer) < kConsumers ? std::size_t(consumer) : 0;
bytes[i] += size;
++count[i];
if (host) {
host_bytes[i] += size;
++host_count[i];
}
}
std::uint64_t total() const {
std::uint64_t sum = 0;
for (const std::uint64_t b : bytes) sum += b;
return sum;
}
std::uint64_t host_total() const {
std::uint64_t sum = 0;
for (const std::uint64_t b : host_bytes) sum += b;
return sum;
}
};

// What the device knows about the arena and the tables outside the probe's wrapped allocations.
Expand All @@ -123,7 +146,12 @@ struct DeviceShare {
inline double mib(std::uint64_t bytes) { return double(bytes) / 1048576.0; }

// The breakdown line's text after "[rt] mem-probe breakdown t=<s>: ". Every consumer is printed, zero or not, so a
// reader can line up two ticks; the figures are whole MiB.
// reader can line up two ticks. The figures are MiB, each rounded on its own, so the parts can differ from the total
// by a MiB or so; a number in parentheses is a count of allocations. A consumer with memory in host memory adds how
// much to its count ("sampled textures 2300 (412; 870 in host memory)": 412 allocations, 870 of the 2300 MiB in host
// memory), and the total in host memory follows the parts ("in host memory 4997 MiB (4)": 4 allocations). The longest
// items (the arena's bracket, the closing note) stay near 200 characters even with ten-digit figures, inside the 256
// the formatting buffer holds.
inline std::string format(const Tally& tally, const DeviceShare& device, std::uint64_t module_bytes) {
std::string out;
char item[256];
Expand All @@ -132,8 +160,13 @@ inline std::string format(const Tally& tally, const DeviceShare& device, std::ui
bool first = true;
for (std::size_t i = 1; i <= kConsumers; ++i) {
const std::size_t c = i % kConsumers; // "other" last
std::snprintf(item, sizeof item, "%s %s %.0f (%llu)", first ? "" : " +", consumer_name(Consumer(c)),
mib(tally.bytes[c]), (unsigned long long)tally.count[c]);
if (tally.host_count[c])
std::snprintf(item, sizeof item, "%s %s %.0f (%llu; %.0f in host memory)", first ? "" : " +",
consumer_name(Consumer(c)), mib(tally.bytes[c]), (unsigned long long)tally.count[c],
mib(tally.host_bytes[c]));
else
std::snprintf(item, sizeof item, "%s %s %.0f (%llu)", first ? "" : " +", consumer_name(Consumer(c)),
mib(tally.bytes[c]), (unsigned long long)tally.count[c]);
out += item;
first = false;
if (Consumer(c) == Consumer::arena && device.arena) {
Expand Down Expand Up @@ -168,6 +201,11 @@ inline std::string format(const Tally& tally, const DeviceShare& device, std::ui
out += item;
}
}
std::uint64_t host_count = 0;
for (const std::uint64_t n : tally.host_count) host_count += n;
std::snprintf(item, sizeof item, "; in host memory %.0f MiB (%llu)", mib(tally.host_total()),
(unsigned long long)host_count);
out += item;
std::snprintf(item, sizeof item,
"; imported %.0f MiB (%llu); not in the total: bindless tables %.0f MiB (own function table), shader "
"modules %.0f MiB SPIR-V, pipelines and swapchain images (the driver's)",
Expand Down
54 changes: 52 additions & 2 deletions runtime/test/memory_breakdown_test.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// 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
// allocations by consumer adds up to the allocated total with the part in host memory, 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"

Expand Down Expand Up @@ -40,6 +40,54 @@ int tally() {
std::uint64_t sum = 0;
for (std::size_t i = 0; i < mb::kConsumers; ++i) sum += t.bytes[i];
EXPECT(sum == t.total());
EXPECT(t.host_total() == 0);
return 0;
}

// Which memory types count as host memory (the rule mem_probe::sample applies per allocation): a reported type without
// DEVICE_LOCAL. Flags as VkMemoryPropertyFlagBits: DEVICE_LOCAL 0x1, HOST_VISIBLE 0x2, HOST_COHERENT 0x4,
// HOST_CACHED 0x8.
int host_types() {
// A discrete GPU: VRAM, system memory, and the BAR window (DEVICE_LOCAL | HOST_VISIBLE | HOST_COHERENT).
EXPECT(!mb::in_host_memory(0, 3, 0x1));
EXPECT(mb::in_host_memory(1, 3, 0x2 | 0x4));
EXPECT(mb::in_host_memory(1, 3, 0x2 | 0x4 | 0x8));
EXPECT(!mb::in_host_memory(2, 3, 0x1 | 0x2 | 0x4));
EXPECT(mb::in_host_memory(1, 3, 0)); // a type with no flags at all is not the card's memory either
// An integrated GPU: every type device-local.
EXPECT(!mb::in_host_memory(0, 2, 0x1 | 0x2 | 0x4 | 0x8));
// A type index the device does not report: not counted as host, whatever flags came with it.
EXPECT(!mb::in_host_memory(3, 3, 0x2));
EXPECT(!mb::in_host_memory(32, 3, 0));
return 0;
}

// The part in host memory: counted in the consumer's bytes and count as well, never for imported memory, and an
// unknown consumer's goes to other.
int host_share() {
mb::Tally t;
t.add(mb::Consumer::sampled, 900 * kMiB, false, false);
t.add(mb::Consumer::sampled, 870 * kMiB, false, true);
t.add(mb::Consumer::sampled, 30 * kMiB, false, true);
t.add(mb::Consumer::arena, 4095 * kMiB, false, true);
t.add(mb::Consumer::render_target, 1180 * kMiB, false, false);
t.add(mb::Consumer::count, 2 * kMiB, false, true); // out of range: other
t.add(mb::Consumer::windows, 6 * kMiB, true, true); // imported: neither allocated nor in the host part
EXPECT(t.total() == (1800 + 4095 + 1180 + 2) * kMiB);
EXPECT(t.bytes[std::size_t(mb::Consumer::sampled)] == 1800 * kMiB);
EXPECT(t.count[std::size_t(mb::Consumer::sampled)] == 3);
EXPECT(t.host_bytes[std::size_t(mb::Consumer::sampled)] == 900 * kMiB);
EXPECT(t.host_count[std::size_t(mb::Consumer::sampled)] == 2);
EXPECT(t.host_bytes[std::size_t(mb::Consumer::render_target)] == 0);
EXPECT(t.host_bytes[std::size_t(mb::Consumer::other)] == 2 * kMiB);
EXPECT(t.host_bytes[std::size_t(mb::Consumer::windows)] == 0);
EXPECT(t.host_total() == (900 + 4095 + 2) * kMiB);
const std::string line = mb::format(t, mb::DeviceShare{}, 0);
std::fprintf(stderr, "%s\n", line.c_str());
EXPECT(contains(line, " + sampled textures 1800 (3; 900 in host memory)"));
EXPECT(contains(line, "allocated 7077 MiB = arena 4095 (1; 4095 in host memory) + geometry copies 0 (0)"));
EXPECT(contains(line, " + render targets and depth 1180 (1) + "));
EXPECT(contains(line, " + other 2 (1; 2 in host memory); in host memory 4997 MiB (4); imported 6 MiB (1)"));
return 0;
}

Expand Down Expand Up @@ -100,7 +148,7 @@ int text() {
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, " 0 host] + other 819 (1); in host memory 0 MiB (0); imported 206 MiB (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)"));
Expand All @@ -117,6 +165,8 @@ int text() {

int main() {
if (int r = tally()) return r;
if (int r = host_types()) return r;
if (int r = host_share()) return r;
if (int r = owner_scopes()) return r;
if (int r = image_kinds()) return r;
if (int r = text()) return r;
Expand Down
Loading