From f6cb4aedfc01d8e8421122643ea668ae71a6abf5 Mon Sep 17 00:00:00 2001 From: Snooozs <0randale196@gmail.com> Date: Wed, 25 Nov 2026 23:28:38 +0100 Subject: runtime: LEONIDA_DEV_OVERLAY shows what the runtime is doing over the title's window (RT-DEV-OVERLAY) A click-through window of its own with its own thread: frame and submission rate, a submission's CPU time by phase, the phase running now, pipeline builds in flight, indirect draws, cores and memory. Off by default. submit_phases keeps per-phase counts of the threads inside a phase, so overlapping submissions of several queues leave none behind. Co-Authored-By: Claude Opus 5.5 --- runtime/test/dev_overlay_test.cpp | 98 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 runtime/test/dev_overlay_test.cpp (limited to 'runtime/test/dev_overlay_test.cpp') diff --git a/runtime/test/dev_overlay_test.cpp b/runtime/test/dev_overlay_test.cpp new file mode 100644 index 000000000..b366ec2ed --- /dev/null +++ b/runtime/test/dev_overlay_test.cpp @@ -0,0 +1,98 @@ +// src/dev_overlay.h: the text of a sample, the counter differences, the phase bookkeeping behind its "now" line +// (submit_phases.h) and the pipeline builds in flight (dev_status.h). No window, GPU or game file. +#include "dev_overlay.h" + +#include +#include +#include +#include + +#define CHECK(condition) do { \ + if (!(condition)) { \ + std::fprintf(stderr, "line %d: %s\n", __LINE__, #condition); \ + return 1; \ + } \ +} while (0) + +namespace sp = leonida::submit_phases; +namespace dov = leonida::dev_overlay; + +static bool has(const std::vector& lines, const char* part) { + for (const auto& line : lines) + if (line.find(part) != std::string::npos) return true; + return false; +} + +int main() { + // A total that went down was reset by its log line: what it holds is the new part. + std::uint64_t before = 0; + CHECK(dov::grown(10, before) == 10 && before == 10); + CHECK(dov::grown(25, before) == 15); + CHECK(dov::grown(4, before) == 4 && before == 4); + + // Nested scopes: the inner one is running, then the outer one again, then none. + CHECK(sp::running().phase == -1); + { + const sp::Scope replay(sp::replay); + CHECK(sp::running().phase == sp::replay); + { + const sp::Scope textures(sp::textures); + CHECK(sp::running().phase == sp::textures); + } + CHECK(sp::running().phase == sp::replay); + } + CHECK(sp::running().phase == -1); + + // Two threads whose scopes overlap and end in the order they began: no phase stays behind. + { + std::atomic step{0}; + std::thread a([&] { + const sp::Scope heaps(sp::heaps); + step = 1; + while (step.load() < 2) std::this_thread::yield(); + }); + std::thread b([&] { + while (step.load() < 1) std::this_thread::yield(); + const sp::Scope shadows(sp::shadows); + step = 2; + while (step.load() < 3) std::this_thread::yield(); + }); + a.join(); + CHECK(sp::running().phase == sp::shadows); + step = 3; + b.join(); + CHECK(sp::running().phase == -1); + } + + // The builds in flight and the last one's name, cut to its buffer. + auto& builds = leonida::dev_status::builds(); + const auto done = builds.done.load(); + { + const leonida::dev_status::Build one("one"); + const leonida::dev_status::Build two(std::string(500, 'x').c_str()); + CHECK(builds.active.load() == 2 && builds.last() == std::string(95, 'x')); + } + CHECK(builds.active.load() == 0 && builds.done.load() == done + 2); + + dov::Sample s; + auto text = dov::lines(s, 12); + CHECK(text.size() == 6); + CHECK(has(text, "12 s") && has(text, "now: the title's own code") && has(text, "pipelines: 0 built")); + CHECK(!has(text, "building")); + s.frames = 8.44; + s.phase = sp::textures; + s.phase_for = 3.9; + s.phase_ms[sp::shadows] = 2; + s.phase_ms[sp::geometry] = 1.5; + s.building = 3; + s.built = 853; + s.build = std::string(200, 'n'); // longer than the line's part for it + s.drawn = 3029; + s.skipped = 38363; + text = dov::lines(s, 147); + CHECK(has(text, "8.4 fps") && has(text, "now: refreshing textures for 3 s") && has(text, "copies 3.5")); + CHECK(has(text, "853 built, 3 building (nnn") && has(text, "3029 drawn, 38363 skipped")); + for (const auto& line : text) CHECK(line.size() < 160); + std::puts("dev_overlay_test: ok"); + return 0; +} -- cgit v1.2.3 From 38385762bd400e8260c7b3c8a80bb0c03fcc44c4 Mon Sep 17 00:00:00 2001 From: Jan Trencansky Date: Wed, 25 Nov 2026 18:16:25 -0500 Subject: dev overlay: the title HUD's VRAM and private memory figures LEONIDA_DEV_OVERLAY (#2137) showed the working set and free RAM, with no GPU memory and no private memory. It now shows both, from the same code as the title HUD: - src/memory_figures.h holds VramBudget, the private-bytes choice (the larger of PrivateUsage and PagefileUsage, since Wine fills only the latter), the "RAM ..." and "VRAM ..." text with "n/a" where there is no figure, and a publisher for the device's GPU memory figure. No Vulkan, and Win32 only under _WIN32, so the overlay's test still builds on every host. - A device with VK_EXT_memory_budget publishes a reader when it is created and withdraws it first thing when it is destroyed (the lock is held during a read, so a reader never outlives its device). latest_vram() calls the reader at most once per 500 ms for both views. The HUD reads it instead of its own per-device lambda. - The overlay's last line is now "RAM 2.5 GB (ws 1.0) VRAM 3.5/16.0 GB +0.5 shared"; cores and free RAM keep their own line. Tests: dev_overlay_test checks the text with and without figures, the private-bytes fallback, and the publisher (one read per refresh, only the publishing device withdraws). title_hud_test checks that a HUD without its own source shows the published figure. d3d12_vram_budget_test checks that the device publishes its figure while it lives and withdraws it on release. Co-Authored-By: Claude Opus 5.5 --- docs/boot.md | 7 +- runtime/README.md | 2 +- runtime/shims/d3d12_vulkan_device.cpp | 20 ++++- runtime/shims/d3d12_xs_device.cpp | 5 +- runtime/shims/title_hud.h | 60 ++++--------- runtime/src/dev_overlay.h | 18 ++-- runtime/src/memory_figures.h | 147 ++++++++++++++++++++++++++++++++ runtime/src/vulkan_memory_budget.h | 18 ++-- runtime/test/d3d12_vram_budget_test.cpp | 21 ++++- runtime/test/dev_overlay_test.cpp | 72 +++++++++++++++- runtime/test/title_hud_test.cpp | 62 ++++++++------ 11 files changed, 324 insertions(+), 108 deletions(-) create mode 100644 runtime/src/memory_figures.h (limited to 'runtime/test/dev_overlay_test.cpp') diff --git a/docs/boot.md b/docs/boot.md index 831787057..3ba791f88 100644 --- a/docs/boot.md +++ b/docs/boot.md @@ -126,7 +126,8 @@ Useful switches: and working set; - its GPU memory: in use and the budget over the device-local heaps, and in use in the other heaps, read from the Vulkan device through `VK_EXT_memory_budget`. Without that extension it falls back to DXGI's - `QueryVideoMemoryInfo`, which reads 0 under Wine; with neither the field reads `VRAM n/a`, not a false 0; + `QueryVideoMemoryInfo`, which reads 0 under Wine; with neither the field reads `VRAM n/a`, not a false 0. + Both memory figures and their text come from `runtime/src/memory_figures.h`, which the dev overlay shows too; - the graphics and compute pipelines created, and the seconds spent in `vkCreate*Pipelines` in the last half second. A pipeline compiled in the frame shows up there as a hitch. This needs `LEONIDA_MEM_PROBE`; without it the field reads `PSO -`. @@ -136,7 +137,9 @@ Useful switches: * `LEONIDA_DEV_OVERLAY=1` (Windows) shows what the runtime is doing in a small click-through window over the top left corner of the title's window, while that window is in front (`runtime/src/dev_overlay.h`): frames and submissions a second, where a submission's CPU time goes, the phase that is running now and for how long, pipeline builds in - flight, indirect draws emitted and skipped, cores, memory. The figures cover the last two seconds. It has its own + flight, indirect draws emitted and skipped, cores, free RAM, and the same private memory and GPU memory figures as + `LEONIDA_TITLE_HUD` (`VRAM n/a` without `VK_EXT_memory_budget`). The device publishes its GPU memory figure, and + the overlay and the HUD read it at most once per half second. The rates cover the last two seconds. It has its own thread, so it also counts while the title presents nothing, and it is in no capture. `LEONIDA_TITLE_HUD` shows frame times and compile times in the title bar instead; both can be on. * `DISABLE_RTSS_LAYER=1` and `DISABLE_VULKAN_OBS_CAPTURE=1` when MSI Afterburner or OBS is installed; their diff --git a/runtime/README.md b/runtime/README.md index 3424175e1..2af9dc215 100644 --- a/runtime/README.md +++ b/runtime/README.md @@ -398,7 +398,7 @@ ones marked *diagnostic* only add log lines or exist to compare a run with and w | `LEONIDA_PASS_TRACE` | unset | *Diagnostic.* `n`, with `LEONIDA_DEVICE_FAULT=1`: in the frame after every `n`-th present, each submission logs its compute programs in order, repeats folded (`src/vulkan_backend.h`), for the frame's pass order. | | `LEONIDA_GPU_LATENCY` | unset | *Diagnostic.* `1` logs, every 10 s for each queue kind, how long the GPU took from a submission's `vkQueueSubmit` to it being seen done, and the share of the wall time the queue had work (`src/vulkan_backend.h`). | | `LEONIDA_SUBMIT_PHASES_SECONDS` | `5` | *Diagnostic.* The period, in seconds, of the always-on line that says how many submissions there were and where their CPU time went, phase by phase (`src/submit_phases.h`). `0` turns the line off. | -| `LEONIDA_DEV_OVERLAY` | unset | *Diagnostic, Windows.* `1` draws what the runtime is doing over the top left corner of the title's window (`src/dev_overlay.h`): frame and submission rate, a submission's CPU time by phase, the phase running now, pipeline builds, indirect draws, cores and memory. | +| `LEONIDA_DEV_OVERLAY` | unset | *Diagnostic, Windows.* `1` draws what the runtime is doing over the top left corner of the title's window (`src/dev_overlay.h`): frame and submission rate, a submission's CPU time by phase, the phase running now, pipeline builds, indirect draws, cores, free RAM, and the private memory and GPU memory (`src/memory_figures.h`, shared with `LEONIDA_TITLE_HUD`; the GPU figure is the device's `VK_EXT_memory_budget`, `VRAM n/a` without it). | | `LEONIDA_SAMPLE_MS` | unset | *Diagnostic.* `ms` starts the thread sampler (`src/thread_sampler.h`): every `ms` milliseconds each thread of the process that used CPU since the last look is suspended just long enough to copy its context, its stack is unwound afterwards, and a periodic report lists the busiest threads and their most frequent call chains (eight frames, innermost first). | | `LEONIDA_SAMPLE_REPORT_S` | `10` | *Diagnostic.* Seconds between two reports of the thread sampler (`LEONIDA_SAMPLE_MS`). | | `LEONIDA_SAMPLE_WAITS` | unset | *Diagnostic.* `1` makes the thread sampler sample waiting threads too, not only those that used CPU, and list each thread's most frequent chain, so a thread that waits shows where. | diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index 7b45daddc..0b81e1446 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -10848,6 +10848,15 @@ std::timed_mutex exit_cache_mutex; std::vector exit_cache_devices; std::atomic exit_cache_saved{false}; +// VK_EXT_memory_budget read from a device's info: the reader a device publishes for the title HUD and the dev overlay +// (src/memory_figures.h), and vulkan_vram_budget. Not available without the extension. +leonida::VramBudget read_vram_budget(const VulkanDeviceInfo& info) { + if (!info.memory_budget || !info.get_instance_proc || !info.instance) return {}; + const auto get = reinterpret_cast( + info.get_instance_proc(info.instance, "vkGetPhysicalDeviceMemoryProperties2")); + return leonida::query_vram_budget(get, info.physical); +} + // The Vulkan objects create_vulkan_device_standalone made, destroyed with the device. struct OwnedRuntime { PFN_vkDestroyDevice destroy_device = nullptr; @@ -10878,6 +10887,11 @@ public: std::lock_guard lock(exit_cache_mutex); exit_cache_devices.push_back(&state_); } + // The GPU memory figure of the title HUD and the dev overlay: the newest device with VK_EXT_memory_budget. + if (info.memory_budget) + leonida::memory_figures::publish_vram_reader( + [](const void* context) { return read_vram_budget(static_cast(context)->info); }, + &state_); queue_users_.resize(info.command_queues.size(), 0); compute_users_.resize(info.compute_queues.size(), 0); copy_users_.resize(info.copy_queues.size(), 0); @@ -11733,6 +11747,7 @@ public: } ~Device() override { + leonida::memory_figures::withdraw_vram_reader(&state_); // waits for a read of this device in progress { // Before anything goes: an exit save in progress finishes with this device first. std::lock_guard lock(exit_cache_mutex); @@ -17381,10 +17396,7 @@ const VulkanDeviceInfo* vulkan_device_info(ID3D12Device* device) { leonida::VramBudget vulkan_vram_budget(ID3D12Device* device) { const VulkanDeviceInfo* info = vulkan_device_info(device); - if (!info || !info->memory_budget || !info->get_instance_proc || !info->instance) return {}; - const auto get = reinterpret_cast( - info->get_instance_proc(info->instance, "vkGetPhysicalDeviceMemoryProperties2")); - return leonida::query_vram_budget(get, info->physical); + return info ? read_vram_budget(*info) : leonida::VramBudget{}; } std::uint64_t title_shadow_refresh_queries(ID3D12Device* device) { diff --git a/runtime/shims/d3d12_xs_device.cpp b/runtime/shims/d3d12_xs_device.cpp index 48bed42b2..61e795d44 100644 --- a/runtime/shims/d3d12_xs_device.cpp +++ b/runtime/shims/d3d12_xs_device.cpp @@ -1741,10 +1741,7 @@ PresentState* present_state_for(DeviceProxy* device, bool splash_only = false) { std::fprintf(stderr, "[rt] d3d12_xs: frame interval %u us replayed (0x%08lx), origin event (0x%08lx)\n", interval, static_cast(set), static_cast(armed)); } - // The device outlives the HUD: proxy_release destroys the present state before it releases `real`. - if (TitleHud::enabled()) - state->hud = std::make_unique( - target, [real = device->real] { return rt::d3d12::vulkan_vram_budget(real); }); + if (TitleHud::enabled()) state->hud = std::make_unique(target); if (splash_only) device->present_state = pending.release(); std::fprintf(stderr, "[rt] d3d12_xs: PresentX ready (window %p: %s, client area %ldx%ld)\n", static_cast(target), diff --git a/runtime/shims/title_hud.h b/runtime/shims/title_hud.h index 339d07078..160d769c4 100644 --- a/runtime/shims/title_hud.h +++ b/runtime/shims/title_hud.h @@ -3,9 +3,10 @@ // frame rate and frame time from PresentX (average and worst frame of the last half second), or how long // since the last frame when there was none; // the process's private memory (its commit charge) and working set; -// its GPU memory: in use and the budget over the device-local heaps, and in use in the other heaps, from -// VK_EXT_memory_budget on the device the title presents on (rt::d3d12::vulkan_vram_budget); without it, DXGI's -// QueryVideoMemoryInfo (local and non-local segments), which Wine leaves at 0; "n/a" when neither has a figure; +// its GPU memory: in use and the budget over the device-local heaps, and in use in the other heaps, from the +// device's VK_EXT_memory_budget; without it, DXGI's QueryVideoMemoryInfo (local and non-local segments), which +// Wine leaves at 0; "n/a" when neither has a figure. Both memory figures and their text come from +// src/memory_figures.h, which the dev overlay (src/dev_overlay.h) shows as well; // graphics and compute pipelines created, and the seconds spent in vkCreate*Pipelines in the last interval, // which is where an inline compile shows up as a hitch (needs LEONIDA_MEM_PROBE, src/mem_probe.h; else "-"); // time since the first present. @@ -24,11 +25,12 @@ #include #include #include +#include #include #include #include "../src/mem_probe.h" -#include "../src/vulkan_memory_budget.h" +#include "../src/memory_figures.h" namespace rt::d3d12_xs { @@ -42,8 +44,8 @@ public: return on; } - // The VRAM figure's source, called once per refresh on the HUD's thread: VK_EXT_memory_budget of the device the - // title presents on. Empty, or returning a budget that is not available: DXGI's figures, else "n/a". + // The VRAM figure's source, called once per refresh on the HUD's thread. Empty: the figure the device publishes + // (memory_figures::latest_vram). A budget that is not available: DXGI's figures, else "n/a". Tests pass their own. using VramSource = std::function; explicit TitleHud(HWND window, VramSource vram = {}) : window_(window), vram_(std::move(vram)) { @@ -74,12 +76,6 @@ public: presents_.fetch_add(1, std::memory_order_relaxed); } - // The commit charge. PrivateUsage and PagefileUsage are the same figure on Windows 8 and later; Wine fills only - // PagefileUsage (its GetProcessMemoryInfo writes the PROCESS_MEMORY_COUNTERS part and leaves PrivateUsage 0). - static std::uint64_t private_bytes(const PROCESS_MEMORY_COUNTERS_EX& counters) { - return (std::max)(counters.PrivateUsage, counters.PagefileUsage); - } - // DXGI's figures as a budget. A local segment with no usage or no budget is how Wine's stub answers (a process // that presents through a GPU always has some memory there), so that is not available rather than a 0. static leonida::VramBudget dxgi_budget(const DXGI_QUERY_VIDEO_MEMORY_INFO& local, @@ -92,25 +88,6 @@ public: return out; } - // "RAM n/a" when the counters could not be read, and for the private figure when it reads 0. - static void format_ram(wchar_t* out, std::size_t size, const PROCESS_MEMORY_COUNTERS_EX* counters) { - const std::uint64_t bytes = counters ? private_bytes(*counters) : 0; - if (!counters) - std::swprintf(out, size, L"RAM n/a"); - else if (!bytes) - std::swprintf(out, size, L"RAM n/a (ws %.1f)", gb(counters->WorkingSetSize)); - else - std::swprintf(out, size, L"RAM %.1f GB (ws %.1f)", gb(bytes), gb(counters->WorkingSetSize)); - } - - static void format_vram(wchar_t* out, std::size_t size, const leonida::VramBudget& vram) { - if (!vram.available) - std::swprintf(out, size, L"VRAM n/a"); - else - std::swprintf(out, size, L"VRAM %.1f/%.1f GB +%.1f shared", gb(vram.local_usage), gb(vram.local_budget), - gb(vram.shared_usage)); - } - private: static constexpr DWORD interval_ms = 500; @@ -133,21 +110,15 @@ private: const double fps = seconds > 0 ? double(frames) / seconds : 0.0; const double frame_ms = frames ? seconds * 1000.0 / double(frames) : 0.0; - wchar_t memory[96]; - PROCESS_MEMORY_COUNTERS_EX counters{}; - counters.cb = sizeof counters; - const bool counted = GetProcessMemoryInfo( - GetCurrentProcess(), reinterpret_cast(&counters), sizeof counters); - format_ram(memory, 96, counted ? &counters : nullptr); - - wchar_t video[96]; - leonida::VramBudget vram = vram_ ? vram_() : leonida::VramBudget{}; + namespace mf = leonida::memory_figures; + const std::wstring memory = widen(mf::ram_text(mf::process_memory())); + leonida::VramBudget vram = vram_ ? vram_() : mf::latest_vram(); DXGI_QUERY_VIDEO_MEMORY_INFO local{}, shared{}; if (!vram.available && adapter && SUCCEEDED(adapter->QueryVideoMemoryInfo(0, DXGI_MEMORY_SEGMENT_GROUP_LOCAL, &local)) && SUCCEEDED(adapter->QueryVideoMemoryInfo(0, DXGI_MEMORY_SEGMENT_GROUP_NON_LOCAL, &shared))) vram = dxgi_budget(local, shared); - format_vram(video, 96, vram); + const std::wstring video = widen(mf::vram_text(vram)); wchar_t pipelines[128] = L"PSO -"; auto& probe = leonida::mem_probe::state(); @@ -177,15 +148,16 @@ private: std::swprintf(frame, 96, L"0 fps, no frame for %.1f s", double(now.QuadPart - last_present) / double(frequency_.QuadPart)); wchar_t text[512]; - std::swprintf(text, 512, L"Leonida | %ls | %ls | %ls | %ls | %u:%02u", frame, memory, video, pipelines, - up / 60, up % 60); + std::swprintf(text, 512, L"Leonida | %ls | %ls | %ls | %ls | %u:%02u", frame, memory.c_str(), + video.c_str(), pipelines, up / 60, up % 60); DWORD_PTR ignored = 0; SendMessageTimeoutW(window_, WM_SETTEXT, 0, reinterpret_cast(text), SMTO_ABORTIFHUNG, interval_ms, &ignored); } } - static double gb(std::uint64_t bytes) { return double(bytes) / double(1ull << 30); } + // memory_figures' text is ASCII. + static std::wstring widen(const std::string& text) { return std::wstring(text.begin(), text.end()); } // The hardware adapter with the most dedicated memory: the one LEONIDA_VK_DEVICE picks on a machine with a // discrete GPU. The host's own dxgi.dll, not the title's shim. diff --git a/runtime/src/dev_overlay.h b/runtime/src/dev_overlay.h index fe38031db..1eecfc366 100644 --- a/runtime/src/dev_overlay.h +++ b/runtime/src/dev_overlay.h @@ -3,11 +3,13 @@ // window that says how the boot is doing: frames and submissions a second, where a submission's CPU time goes and which // phase runs now, pipeline builds, indirect draws, CPU, memory. It is its own window with its own thread and GDI text, // so it keeps counting while the title shows no frames for seconds (pipeline builds, a stalled submission), it is in no -// captured frame, and it changes nothing the GPU does. It only reads counters: submit_phases.h, indirect_draw_stats.h -// and dev_status.h. LEONIDA_TITLE_HUD (shims/title_hud.h) is the other view: frame times, memory and compile time in the +// captured frame, and it changes nothing the GPU does. It only reads counters: submit_phases.h, indirect_draw_stats.h, +// dev_status.h, and memory_figures.h for the private memory and the device's GPU memory, which LEONIDA_TITLE_HUD +// (shims/title_hud.h) shows with the same text. The HUD is the other view: frame times, memory and compile time in the // window's title bar. #include "dev_status.h" #include "indirect_draw_stats.h" +#include "memory_figures.h" #include "submit_phases.h" #ifdef _WIN32 @@ -50,7 +52,9 @@ struct Sample { std::uint64_t built = 0; std::string build; // the last one started unsigned long long drawn = 0, skipped = 0; // indirect draws so far - double cores = 0, process_gb = 0, free_gb = 0; + double cores = 0, free_gb = 0; + memory_figures::ProcessMemory ram; // private memory and working set + VramBudget vram; // the device's VK_EXT_memory_budget; "n/a" without it }; // The text of one sample: pure, for the test. @@ -82,8 +86,9 @@ inline std::vector lines(const Sample& s, double age) { out.emplace_back(text); std::snprintf(text, sizeof text, "indirect draws: %llu drawn, %llu skipped", s.drawn, s.skipped); out.emplace_back(text); - std::snprintf(text, sizeof text, "%.1f cores, %.1f GB used, %.1f GB RAM free", s.cores, s.process_gb, s.free_gb); + std::snprintf(text, sizeof text, "%.1f cores, %.1f GB RAM free", s.cores, s.free_gb); out.emplace_back(text); + out.emplace_back(memory_figures::ram_text(s.ram) + " " + memory_figures::vram_text(s.vram)); return out; } @@ -206,9 +211,8 @@ private: const std::uint64_t used = grown(ticks(kernel) + ticks(user), cpu_); if (s.seconds > 0) s.cores = double(used) * 1e-7 / s.seconds; } - PROCESS_MEMORY_COUNTERS memory{sizeof memory}; - if (GetProcessMemoryInfo(GetCurrentProcess(), &memory, sizeof memory)) - s.process_gb = double(memory.WorkingSetSize) / double(1u << 30); + s.ram = memory_figures::process_memory(); + s.vram = memory_figures::latest_vram(); // reads the device at most once per 500 ms MEMORYSTATUSEX status{sizeof status}; if (GlobalMemoryStatusEx(&status)) s.free_gb = double(status.ullAvailPhys) / double(1u << 30); lines_ = lines(s, sp::process_age()); diff --git a/runtime/src/memory_figures.h b/runtime/src/memory_figures.h new file mode 100644 index 000000000..8bf611553 --- /dev/null +++ b/runtime/src/memory_figures.h @@ -0,0 +1,147 @@ +#pragma once +// The memory figures that the title HUD (shims/title_hud.h, LEONIDA_TITLE_HUD) and the dev overlay (dev_overlay.h, +// LEONIDA_DEV_OVERLAY) both show, and the text for each. Both views use this header, so they show the same figures +// and the same "n/a" where there is no figure, never a false 0: +// GPU memory: in use and the budget over the device-local heaps, and in use in the other heaps, from the device's +// VK_EXT_memory_budget (vulkan_memory_budget.h reads it). The device publishes a reader here (publish_vram_reader) +// because the overlay runs outside the device, and latest_vram() calls it at most once per refresh_ms. +// Private memory: the process's commit charge. Wine fills only PagefileUsage, which on Windows 8 and later is the +// same figure as PrivateUsage. +// No Vulkan here, and Win32 only under _WIN32, so the overlay's test builds on every host. +#ifdef _WIN32 +#ifndef NOMINMAX +#define NOMINMAX +#endif +#include +#include +#ifdef _MSC_VER +#pragma comment(lib, "psapi.lib") +#endif +#endif + +#include +#include +#include +#include +#include +#include + +namespace leonida { + +struct VramBudget { + // False when there is nothing to show: no VK_EXT_memory_budget on the device, no device, or a device that + // reports no budget for any DEVICE_LOCAL heap. The views show "n/a" then, not a 0. + bool available = false; + std::uint64_t local_usage = 0; // heapUsage summed over the DEVICE_LOCAL heaps + std::uint64_t local_budget = 0; // heapBudget summed over the DEVICE_LOCAL heaps + std::uint64_t shared_usage = 0; // heapUsage summed over the other heaps: system memory the GPU uses +}; + +namespace memory_figures { + +inline double gb(std::uint64_t bytes) { return double(bytes) / double(1ull << 30); } + +// "VRAM 3.5/16.0 GB +0.5 shared", or "VRAM n/a". +inline std::string vram_text(const VramBudget& vram) { + if (!vram.available) return "VRAM n/a"; + char text[96]; + std::snprintf(text, sizeof text, "VRAM %.1f/%.1f GB +%.1f shared", gb(vram.local_usage), gb(vram.local_budget), + gb(vram.shared_usage)); + return text; +} + +struct ProcessMemory { + bool read = false; // GetProcessMemoryInfo answered + std::uint64_t private_usage = 0, pagefile_usage = 0, working_set = 0; +}; + +// The commit charge. PrivateUsage and PagefileUsage are the same figure on Windows 8 and later. Wine's +// GetProcessMemoryInfo writes only the PROCESS_MEMORY_COUNTERS part, so PrivateUsage reads 0 there. +inline std::uint64_t private_bytes(const ProcessMemory& memory) { + return (std::max)(memory.private_usage, memory.pagefile_usage); // parenthesised: windows.h may define max +} + +// "RAM 2.5 GB (ws 1.0)". "RAM n/a (ws 1.0)" when the private bytes read 0, "RAM n/a" when nothing could be read. +inline std::string ram_text(const ProcessMemory& memory) { + char text[96]; + if (!memory.read) + std::snprintf(text, sizeof text, "RAM n/a"); + else if (const std::uint64_t bytes = private_bytes(memory); !bytes) + std::snprintf(text, sizeof text, "RAM n/a (ws %.1f)", gb(memory.working_set)); + else + std::snprintf(text, sizeof text, "RAM %.1f GB (ws %.1f)", gb(bytes), gb(memory.working_set)); + return text; +} + +#ifdef _WIN32 +// This process's figures now. +inline ProcessMemory process_memory() { + PROCESS_MEMORY_COUNTERS_EX counters{}; + counters.cb = sizeof counters; + ProcessMemory out; + if (!GetProcessMemoryInfo(GetCurrentProcess(), reinterpret_cast(&counters), + sizeof counters)) + return out; + out.read = true; + out.private_usage = counters.PrivateUsage; + out.pagefile_usage = counters.PagefileUsage; + out.working_set = counters.WorkingSetSize; + return out; +} +#endif + +// The device's figure, published for views outside the device. A device with VK_EXT_memory_budget publishes a reader +// when it is created and withdraws it first thing when it goes. The views call latest_vram(), which calls the reader at +// most once per refresh_ms and returns the last figure in between. The lock is held during a read, so withdrawing +// waits for a read that is in progress, and a reader is never called after its device is gone. +using VramReader = VramBudget (*)(const void* context); +inline constexpr std::chrono::milliseconds refresh_ms{500}; + +class VramPublisher { +public: + void publish(VramReader reader, const void* context) { + std::lock_guard lock(mutex_); + reader_ = reader; + context_ = context; + last_ = {}; + read_ = false; + } + // Only the device that published the current reader withdraws it. Another device may have published since. + void withdraw(const void* context) { + std::lock_guard lock(mutex_); + if (context_ != context) return; + reader_ = nullptr; + context_ = nullptr; + last_ = {}; + read_ = false; + } + VramBudget latest(std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now()) { + std::lock_guard lock(mutex_); + if (!reader_) return {}; + if (!read_ || now - at_ >= refresh_ms) { + last_ = reader_(context_); + at_ = now; + read_ = true; + } + return last_; + } + +private: + std::mutex mutex_; + VramReader reader_ = nullptr; + const void* context_ = nullptr; + VramBudget last_; + std::chrono::steady_clock::time_point at_{}; + bool read_ = false; +}; + +inline VramPublisher& vram_publisher() { + static VramPublisher instance; + return instance; +} +inline void publish_vram_reader(VramReader reader, const void* context) { vram_publisher().publish(reader, context); } +inline void withdraw_vram_reader(const void* context) { vram_publisher().withdraw(context); } +inline VramBudget latest_vram() { return vram_publisher().latest(); } + +} // namespace memory_figures +} // namespace leonida diff --git a/runtime/src/vulkan_memory_budget.h b/runtime/src/vulkan_memory_budget.h index 16042987c..e9bc549b1 100644 --- a/runtime/src/vulkan_memory_budget.h +++ b/runtime/src/vulkan_memory_budget.h @@ -1,8 +1,9 @@ #pragma once // VK_EXT_memory_budget: how much GPU memory this process uses and may use, read from the Vulkan device itself. The -// title HUD (shims/title_hud.h, LEONIDA_TITLE_HUD) shows it; DXGI's QueryVideoMemoryInfo, which it read before, is 0 -// under Wine. The sums depend only on the two Vulkan structures, so a test can feed them made-up heaps. +// device publishes it (memory_figures.h) for the title HUD (shims/title_hud.h, LEONIDA_TITLE_HUD) and the dev overlay +// (dev_overlay.h, LEONIDA_DEV_OVERLAY); DXGI's QueryVideoMemoryInfo, which the HUD read before, is 0 under Wine. The +// sums depend only on the two Vulkan structures, so a test can feed them made-up heaps. #ifndef VK_NO_PROTOTYPES #define VK_NO_PROTOTYPES @@ -11,16 +12,9 @@ #include -namespace leonida { +#include "memory_figures.h" // VramBudget -struct VramBudget { - // False when there is nothing to show: no VK_EXT_memory_budget on the device, no query, or a device that reports - // no budget for any DEVICE_LOCAL heap. Callers show "n/a" then, not a 0. - bool available = false; - std::uint64_t local_usage = 0; // heapUsage summed over the DEVICE_LOCAL heaps - std::uint64_t local_budget = 0; // heapBudget summed over the DEVICE_LOCAL heaps - std::uint64_t shared_usage = 0; // heapUsage summed over the other heaps: system memory the GPU uses -}; +namespace leonida { inline VramBudget sum_vram_budget(const VkPhysicalDeviceMemoryProperties& memory, const VkPhysicalDeviceMemoryBudgetPropertiesEXT& budget) { @@ -39,7 +33,7 @@ inline VramBudget sum_vram_budget(const VkPhysicalDeviceMemoryProperties& memory // Reads the budget now. `physical` must have VK_EXT_memory_budget (enabled on the logical device, as the runtime's // device does). One vkGetPhysicalDeviceMemoryProperties2 call, which needs no external synchronization, so another -// thread (the HUD's) may call it while the device is in use. +// thread (the HUD's or the overlay's) may call it while the device is in use. inline VramBudget query_vram_budget(PFN_vkGetPhysicalDeviceMemoryProperties2 get_memory_properties2, VkPhysicalDevice physical) { if (!get_memory_properties2 || physical == VK_NULL_HANDLE) return {}; diff --git a/runtime/test/d3d12_vram_budget_test.cpp b/runtime/test/d3d12_vram_budget_test.cpp index c05830079..0f69d7548 100644 --- a/runtime/test/d3d12_vram_budget_test.cpp +++ b/runtime/test/d3d12_vram_budget_test.cpp @@ -1,8 +1,9 @@ // rt::d3d12::vulkan_vram_budget on the standalone Vulkan-backed device: the GPU memory in use and the budget the // title HUD shows (shims/title_hud.h), from VK_EXT_memory_budget. The device enables the extension exactly when the // GPU has it; then, with a 64 MiB default-heap buffer alive, the device-local budget and the usage are non-zero. -// Without the extension the figure is not available, and the test skips (77) after checking that. Also skips without -// a Vulkan GPU. No shaders or game data. +// The device publishes that figure for the HUD and the dev overlay (src/memory_figures.h) and withdraws it when it +// is released. Without the extension the figure is not available, and the test skips (77) after checking that. +// Also skips without a Vulkan GPU. No shaders or game data. #include "../shims/d3d12_com.h" #include @@ -60,8 +61,10 @@ int main() { // Enabled exactly when the GPU has it. const bool supported = physical_has_memory_budget(*info); EXPECT(info->memory_budget == supported); + // What the title HUD and the dev overlay show (src/memory_figures.h): this device's figure once it has one. + EXPECT(leonida::memory_figures::latest_vram().available == supported); if (!supported) { - EXPECT(!rt::d3d12::vulkan_vram_budget(device.p).available); // the HUD shows "n/a" + EXPECT(!rt::d3d12::vulkan_vram_budget(device.p).available); // the HUD and the overlay show "n/a" std::printf("SKIP: the GPU has no VK_EXT_memory_budget; the figure is not available\n"); return 77; } @@ -85,6 +88,16 @@ int main() { EXPECT(after.available); EXPECT(after.local_budget > 0); EXPECT(after.local_usage + after.shared_usage > 0); - std::printf("d3d12_vram_budget: VK_EXT_memory_budget enabled; usage and budget read from the device\n"); + const leonida::VramBudget published = leonida::memory_figures::latest_vram(); + EXPECT(published.available && published.local_budget > 0); // may be the read before the buffer: cached + + // A released device withdraws its figure: nothing reads a device that is gone. + buffer.p->Release(); + buffer.p = nullptr; + device.p->Release(); + device.p = nullptr; + EXPECT(!leonida::memory_figures::latest_vram().available); + std::printf("d3d12_vram_budget: VK_EXT_memory_budget enabled; usage and budget read from the device and published" + " while it lives\n"); return 0; } diff --git a/runtime/test/dev_overlay_test.cpp b/runtime/test/dev_overlay_test.cpp index b366ec2ed..55a5a31cc 100644 --- a/runtime/test/dev_overlay_test.cpp +++ b/runtime/test/dev_overlay_test.cpp @@ -1,7 +1,10 @@ // src/dev_overlay.h: the text of a sample, the counter differences, the phase bookkeeping behind its "now" line -// (submit_phases.h) and the pipeline builds in flight (dev_status.h). No window, GPU or game file. +// (submit_phases.h), the pipeline builds in flight (dev_status.h) and the memory figures it shares with the title HUD +// (memory_figures.h). No window, GPU or game file. #include "dev_overlay.h" +#include +#include #include #include #include @@ -23,6 +26,68 @@ static bool has(const std::vector& lines, const char* part) { return false; } +// memory_figures.h, which the overlay and the title HUD share: the private bytes Wine reports, the text of both +// figures with and without one, and the device's published GPU memory, read at most once per refresh. +static int check_memory_figures() { + namespace mf = leonida::memory_figures; + constexpr std::uint64_t kGiB = 1ull << 30; + + // Wine fills PagefileUsage and leaves PrivateUsage 0; Windows fills both with the same value. + mf::ProcessMemory ram; + CHECK(mf::ram_text(ram) == "RAM n/a"); // GetProcessMemoryInfo failed + ram.read = true; + ram.working_set = kGiB; + CHECK(mf::ram_text(ram) == "RAM n/a (ws 1.0)"); + ram.pagefile_usage = 5 * kGiB / 2; + CHECK(mf::private_bytes(ram) == 5 * kGiB / 2 && mf::ram_text(ram) == "RAM 2.5 GB (ws 1.0)"); + ram.private_usage = 5 * kGiB / 2; + CHECK(mf::private_bytes(ram) == 5 * kGiB / 2 && mf::ram_text(ram) == "RAM 2.5 GB (ws 1.0)"); + + leonida::VramBudget vram; + CHECK(mf::vram_text(vram) == "VRAM n/a"); + vram.available = true; + vram.local_usage = 3 * kGiB + kGiB / 2; + vram.local_budget = 16 * kGiB; + vram.shared_usage = kGiB / 2; + CHECK(mf::vram_text(vram) == "VRAM 3.5/16.0 GB +0.5 shared"); + + // The overlay's line with both figures. + dov::Sample s; + s.ram = ram; + s.vram = vram; + CHECK(has(dov::lines(s, 1), "RAM 2.5 GB (ws 1.0) VRAM 3.5/16.0 GB +0.5 shared")); + + // The published figure: none until a device publishes a reader; then read at most once per refresh_ms; another + // device's withdrawal leaves it; its own takes it away. + struct Fake { + int reads = 0; + leonida::VramBudget figure; + }; + static Fake device; + static const int other = 0; + device.figure = vram; + auto& publisher = mf::vram_publisher(); + using clock = std::chrono::steady_clock; + const clock::time_point t0 = clock::now(); + CHECK(!publisher.latest(t0).available); + mf::publish_vram_reader( + [](const void* context) { + auto* fake = static_cast(const_cast(context)); + ++fake->reads; + return fake->figure; + }, + &device); + CHECK(publisher.latest(t0).local_usage == 3 * kGiB + kGiB / 2 && device.reads == 1); + device.figure.local_usage = 4 * kGiB; + CHECK(publisher.latest(t0 + mf::refresh_ms / 2).local_usage == 3 * kGiB + kGiB / 2 && device.reads == 1); + CHECK(publisher.latest(t0 + mf::refresh_ms).local_usage == 4 * kGiB && device.reads == 2); + mf::withdraw_vram_reader(&other); + CHECK(mf::latest_vram().available); + mf::withdraw_vram_reader(&device); + CHECK(!mf::latest_vram().available); + return 0; +} + int main() { // A total that went down was reset by its log line: what it holds is the new part. std::uint64_t before = 0; @@ -76,7 +141,9 @@ int main() { dov::Sample s; auto text = dov::lines(s, 12); - CHECK(text.size() == 6); + CHECK(text.size() == 7); + // No counters and no device figure: "n/a", never a false 0. + CHECK(has(text, "RAM n/a VRAM n/a")); CHECK(has(text, "12 s") && has(text, "now: the title's own code") && has(text, "pipelines: 0 built")); CHECK(!has(text, "building")); s.frames = 8.44; @@ -93,6 +160,7 @@ int main() { CHECK(has(text, "8.4 fps") && has(text, "now: refreshing textures for 3 s") && has(text, "copies 3.5")); CHECK(has(text, "853 built, 3 building (nnn") && has(text, "3029 drawn, 38363 skipped")); for (const auto& line : text) CHECK(line.size() < 160); + if (const int failed = check_memory_figures()) return failed; std::puts("dev_overlay_test: ok"); return 0; } diff --git a/runtime/test/title_hud_test.cpp b/runtime/test/title_hud_test.cpp index bdde69563..661f4d30c 100644 --- a/runtime/test/title_hud_test.cpp +++ b/runtime/test/title_hud_test.cpp @@ -3,6 +3,7 @@ // summed, DXGI's as the fallback, "n/a" where neither has one, and the private bytes Wine reports. A real Win32 // window and made-up budgets; no GPU needed (d3d12_vram_budget_test reads a real device's). #include "title_hud.h" +#include "../src/vulkan_memory_budget.h" #include @@ -40,11 +41,12 @@ static void pump(DWORD ms, rt::d3d12_xs::TitleHud* present) { constexpr std::uint64_t kGiB = 1ull << 30; -// The memory figures without a window: VK_EXT_memory_budget's heaps summed, DXGI's answer, the private bytes, and -// the text for each, "n/a" where there is no figure. Made-up values, so it is the same on every host. +// The memory figures without a window: VK_EXT_memory_budget's heaps summed, DXGI's answer as the fallback, and this +// process's own private bytes. Made-up values except the last. The text and the private-bytes choice are +// src/memory_figures.h's, shared with the dev overlay; dev_overlay_test checks them. static int check_figures() { using rt::d3d12_xs::TitleHud; - wchar_t text[96]; + namespace mf = leonida::memory_figures; // Two device-local heaps (VRAM and a BAR window) and system memory. VkPhysicalDeviceMemoryProperties heaps{}; @@ -65,15 +67,12 @@ static int check_figures() { CHECK(summed.available); CHECK(summed.local_usage == 3 * kGiB + kGiB / 2 && summed.local_budget == 16 * kGiB); CHECK(summed.shared_usage == kGiB / 2); - TitleHud::format_vram(text, 96, summed); - CHECK(std::wstring(text) == L"VRAM 3.5/16.0 GB +0.5 shared"); + CHECK(mf::vram_text(summed) == "VRAM 3.5/16.0 GB +0.5 shared"); // A device that reports no budget for its device-local heaps has no figure, not a 0. const VkPhysicalDeviceMemoryBudgetPropertiesEXT empty{ VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MEMORY_BUDGET_PROPERTIES_EXT}; CHECK(!leonida::sum_vram_budget(heaps, empty).available); - TitleHud::format_vram(text, 96, leonida::VramBudget{}); - CHECK(std::wstring(text) == L"VRAM n/a"); // DXGI: Wine's QueryVideoMemoryInfo answers S_OK with no usage, which is not a figure; Windows' is. DXGI_QUERY_VIDEO_MEMORY_INFO local{}, non_local{}; @@ -85,28 +84,13 @@ static int check_figures() { CHECK(dxgi.available && dxgi.local_usage == 2 * kGiB && dxgi.local_budget == 24 * kGiB && dxgi.shared_usage == kGiB); - // The private bytes: Wine fills PagefileUsage and leaves PrivateUsage 0; Windows fills both with the same value. - PROCESS_MEMORY_COUNTERS_EX counters{}; - counters.WorkingSetSize = kGiB; - TitleHud::format_ram(text, 96, &counters); - CHECK(std::wstring(text) == L"RAM n/a (ws 1.0)"); - counters.PagefileUsage = 5 * kGiB / 2; - CHECK(TitleHud::private_bytes(counters) == 5 * kGiB / 2); - TitleHud::format_ram(text, 96, &counters); - CHECK(std::wstring(text) == L"RAM 2.5 GB (ws 1.0)"); - counters.PrivateUsage = 5 * kGiB / 2; - CHECK(TitleHud::private_bytes(counters) == 5 * kGiB / 2); - TitleHud::format_ram(text, 96, nullptr); - CHECK(std::wstring(text) == L"RAM n/a"); - - // This process's own counters: its private bytes are never 0, on Windows or under Wine. - PROCESS_MEMORY_COUNTERS_EX own{}; - own.cb = sizeof own; - CHECK(GetProcessMemoryInfo(GetCurrentProcess(), reinterpret_cast(&own), sizeof own)); + // This process's own counters: its private bytes are never 0, on Windows or under Wine (where only PagefileUsage + // is filled). + const mf::ProcessMemory own = mf::process_memory(); std::fprintf(stderr, "own counters: PrivateUsage %llu, PagefileUsage %llu, WorkingSetSize %llu\n", - (unsigned long long)own.PrivateUsage, (unsigned long long)own.PagefileUsage, - (unsigned long long)own.WorkingSetSize); - CHECK(TitleHud::private_bytes(own) > 0); + (unsigned long long)own.private_usage, (unsigned long long)own.pagefile_usage, + (unsigned long long)own.working_set); + CHECK(own.read && mf::private_bytes(own) > 0); return 0; } @@ -164,6 +148,28 @@ int main() { CHECK(text.find(L"| VRAM 3.5/20.0 GB +0.5 shared |") != std::wstring::npos); CHECK(reads.load() >= 1 && reads.load() <= 3); // 1.3 s at one refresh per 500 ms } + { + // No source of its own: the HUD shows what a device published (src/memory_figures.h), the figure the dev + // overlay shows as well. + static const int device = 0; + leonida::memory_figures::publish_vram_reader( + [](const void*) { + leonida::VramBudget fake; + fake.available = true; + fake.local_usage = 5 * kGiB; + fake.local_budget = 12 * kGiB; + return fake; + }, + &device); + { + rt::d3d12_xs::TitleHud hud(window); + pump(700, &hud); + const std::wstring text = title_of(window); + std::fwprintf(stderr, L"title: %ls\n", text.c_str()); + CHECK(text.find(L"| VRAM 5.0/12.0 GB +0.0 shared |") != std::wstring::npos); + } + leonida::memory_figures::withdraw_vram_reader(&device); + } { // A source with no figure (a device without VK_EXT_memory_budget): DXGI's figure where it has one (Windows), // else "n/a" (Wine, whose DXGI answers 0; check_figures covers that choice). -- cgit v1.2.3