diff options
| author | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-25 18:16:25 -0500 |
|---|---|---|
| committer | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-25 18:18:57 -0500 |
| commit | 38385762bd400e8260c7b3c8a80bb0c03fcc44c4 (patch) | |
| tree | e0d983ce74468edfde736ab699a695b80702ddbf /runtime/src | |
| parent | d4fb292344088e7925ddeb3bbceb5a9ac46de8c2 (diff) | |
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 <noreply@anthropic.com>
Diffstat (limited to 'runtime/src')
| -rw-r--r-- | runtime/src/dev_overlay.h | 18 | ||||
| -rw-r--r-- | runtime/src/memory_figures.h | 147 | ||||
| -rw-r--r-- | runtime/src/vulkan_memory_budget.h | 18 |
3 files changed, 164 insertions, 19 deletions
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 @@ | |||
| 3 | // window that says how the boot is doing: frames and submissions a second, where a submission's CPU time goes and which | 3 | // window that says how the boot is doing: frames and submissions a second, where a submission's CPU time goes and which |
| 4 | // phase runs now, pipeline builds, indirect draws, CPU, memory. It is its own window with its own thread and GDI text, | 4 | // phase runs now, pipeline builds, indirect draws, CPU, memory. It is its own window with its own thread and GDI text, |
| 5 | // so it keeps counting while the title shows no frames for seconds (pipeline builds, a stalled submission), it is in no | 5 | // so it keeps counting while the title shows no frames for seconds (pipeline builds, a stalled submission), it is in no |
| 6 | // captured frame, and it changes nothing the GPU does. It only reads counters: submit_phases.h, indirect_draw_stats.h | 6 | // captured frame, and it changes nothing the GPU does. It only reads counters: submit_phases.h, indirect_draw_stats.h, |
| 7 | // and dev_status.h. LEONIDA_TITLE_HUD (shims/title_hud.h) is the other view: frame times, memory and compile time in the | 7 | // dev_status.h, and memory_figures.h for the private memory and the device's GPU memory, which LEONIDA_TITLE_HUD |
| 8 | // (shims/title_hud.h) shows with the same text. The HUD is the other view: frame times, memory and compile time in the | ||
| 8 | // window's title bar. | 9 | // window's title bar. |
| 9 | #include "dev_status.h" | 10 | #include "dev_status.h" |
| 10 | #include "indirect_draw_stats.h" | 11 | #include "indirect_draw_stats.h" |
| 12 | #include "memory_figures.h" | ||
| 11 | #include "submit_phases.h" | 13 | #include "submit_phases.h" |
| 12 | 14 | ||
| 13 | #ifdef _WIN32 | 15 | #ifdef _WIN32 |
| @@ -50,7 +52,9 @@ struct Sample { | |||
| 50 | std::uint64_t built = 0; | 52 | std::uint64_t built = 0; |
| 51 | std::string build; // the last one started | 53 | std::string build; // the last one started |
| 52 | unsigned long long drawn = 0, skipped = 0; // indirect draws so far | 54 | unsigned long long drawn = 0, skipped = 0; // indirect draws so far |
| 53 | double cores = 0, process_gb = 0, free_gb = 0; | 55 | double cores = 0, free_gb = 0; |
| 56 | memory_figures::ProcessMemory ram; // private memory and working set | ||
| 57 | VramBudget vram; // the device's VK_EXT_memory_budget; "n/a" without it | ||
| 54 | }; | 58 | }; |
| 55 | 59 | ||
| 56 | // The text of one sample: pure, for the test. | 60 | // The text of one sample: pure, for the test. |
| @@ -82,8 +86,9 @@ inline std::vector<std::string> lines(const Sample& s, double age) { | |||
| 82 | out.emplace_back(text); | 86 | out.emplace_back(text); |
| 83 | std::snprintf(text, sizeof text, "indirect draws: %llu drawn, %llu skipped", s.drawn, s.skipped); | 87 | std::snprintf(text, sizeof text, "indirect draws: %llu drawn, %llu skipped", s.drawn, s.skipped); |
| 84 | out.emplace_back(text); | 88 | out.emplace_back(text); |
| 85 | std::snprintf(text, sizeof text, "%.1f cores, %.1f GB used, %.1f GB RAM free", s.cores, s.process_gb, s.free_gb); | 89 | std::snprintf(text, sizeof text, "%.1f cores, %.1f GB RAM free", s.cores, s.free_gb); |
| 86 | out.emplace_back(text); | 90 | out.emplace_back(text); |
| 91 | out.emplace_back(memory_figures::ram_text(s.ram) + " " + memory_figures::vram_text(s.vram)); | ||
| 87 | return out; | 92 | return out; |
| 88 | } | 93 | } |
| 89 | 94 | ||
| @@ -206,9 +211,8 @@ private: | |||
| 206 | const std::uint64_t used = grown(ticks(kernel) + ticks(user), cpu_); | 211 | const std::uint64_t used = grown(ticks(kernel) + ticks(user), cpu_); |
| 207 | if (s.seconds > 0) s.cores = double(used) * 1e-7 / s.seconds; | 212 | if (s.seconds > 0) s.cores = double(used) * 1e-7 / s.seconds; |
| 208 | } | 213 | } |
| 209 | PROCESS_MEMORY_COUNTERS memory{sizeof memory}; | 214 | s.ram = memory_figures::process_memory(); |
| 210 | if (GetProcessMemoryInfo(GetCurrentProcess(), &memory, sizeof memory)) | 215 | s.vram = memory_figures::latest_vram(); // reads the device at most once per 500 ms |
| 211 | s.process_gb = double(memory.WorkingSetSize) / double(1u << 30); | ||
| 212 | MEMORYSTATUSEX status{sizeof status}; | 216 | MEMORYSTATUSEX status{sizeof status}; |
| 213 | if (GlobalMemoryStatusEx(&status)) s.free_gb = double(status.ullAvailPhys) / double(1u << 30); | 217 | if (GlobalMemoryStatusEx(&status)) s.free_gb = double(status.ullAvailPhys) / double(1u << 30); |
| 214 | lines_ = lines(s, sp::process_age()); | 218 | 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 @@ | |||
| 1 | #pragma once | ||
| 2 | // The memory figures that the title HUD (shims/title_hud.h, LEONIDA_TITLE_HUD) and the dev overlay (dev_overlay.h, | ||
| 3 | // LEONIDA_DEV_OVERLAY) both show, and the text for each. Both views use this header, so they show the same figures | ||
| 4 | // and the same "n/a" where there is no figure, never a false 0: | ||
| 5 | // GPU memory: in use and the budget over the device-local heaps, and in use in the other heaps, from the device's | ||
| 6 | // VK_EXT_memory_budget (vulkan_memory_budget.h reads it). The device publishes a reader here (publish_vram_reader) | ||
| 7 | // because the overlay runs outside the device, and latest_vram() calls it at most once per refresh_ms. | ||
| 8 | // Private memory: the process's commit charge. Wine fills only PagefileUsage, which on Windows 8 and later is the | ||
| 9 | // same figure as PrivateUsage. | ||
| 10 | // No Vulkan here, and Win32 only under _WIN32, so the overlay's test builds on every host. | ||
| 11 | #ifdef _WIN32 | ||
| 12 | #ifndef NOMINMAX | ||
| 13 | #define NOMINMAX | ||
| 14 | #endif | ||
| 15 | #include <windows.h> | ||
| 16 | #include <psapi.h> | ||
| 17 | #ifdef _MSC_VER | ||
| 18 | #pragma comment(lib, "psapi.lib") | ||
| 19 | #endif | ||
| 20 | #endif | ||
| 21 | |||
| 22 | #include <algorithm> | ||
| 23 | #include <chrono> | ||
| 24 | #include <cstdint> | ||
| 25 | #include <cstdio> | ||
| 26 | #include <mutex> | ||
| 27 | #include <string> | ||
| 28 | |||
| 29 | namespace leonida { | ||
| 30 | |||
| 31 | struct VramBudget { | ||
| 32 | // False when there is nothing to show: no VK_EXT_memory_budget on the device, no device, or a device that | ||
| 33 | // reports no budget for any DEVICE_LOCAL heap. The views show "n/a" then, not a 0. | ||
| 34 | bool available = false; | ||
| 35 | std::uint64_t local_usage = 0; // heapUsage summed over the DEVICE_LOCAL heaps | ||
| 36 | std::uint64_t local_budget = 0; // heapBudget summed over the DEVICE_LOCAL heaps | ||
| 37 | std::uint64_t shared_usage = 0; // heapUsage summed over the other heaps: system memory the GPU uses | ||
| 38 | }; | ||
| 39 | |||
| 40 | namespace memory_figures { | ||
| 41 | |||
| 42 | inline double gb(std::uint64_t bytes) { return double(bytes) / double(1ull << 30); } | ||
| 43 | |||
| 44 | // "VRAM 3.5/16.0 GB +0.5 shared", or "VRAM n/a". | ||
| 45 | inline std::string vram_text(const VramBudget& vram) { | ||
| 46 | if (!vram.available) return "VRAM n/a"; | ||
| 47 | char text[96]; | ||
| 48 | std::snprintf(text, sizeof text, "VRAM %.1f/%.1f GB +%.1f shared", gb(vram.local_usage), gb(vram.local_budget), | ||
| 49 | gb(vram.shared_usage)); | ||
| 50 | return text; | ||
| 51 | } | ||
| 52 | |||
| 53 | struct ProcessMemory { | ||
| 54 | bool read = false; // GetProcessMemoryInfo answered | ||
| 55 | std::uint64_t private_usage = 0, pagefile_usage = 0, working_set = 0; | ||
| 56 | }; | ||
| 57 | |||
| 58 | // The commit charge. PrivateUsage and PagefileUsage are the same figure on Windows 8 and later. Wine's | ||
| 59 | // GetProcessMemoryInfo writes only the PROCESS_MEMORY_COUNTERS part, so PrivateUsage reads 0 there. | ||
| 60 | inline std::uint64_t private_bytes(const ProcessMemory& memory) { | ||
| 61 | return (std::max)(memory.private_usage, memory.pagefile_usage); // parenthesised: windows.h may define max | ||
| 62 | } | ||
| 63 | |||
| 64 | // "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. | ||
| 65 | inline std::string ram_text(const ProcessMemory& memory) { | ||
| 66 | char text[96]; | ||
| 67 | if (!memory.read) | ||
| 68 | std::snprintf(text, sizeof text, "RAM n/a"); | ||
| 69 | else if (const std::uint64_t bytes = private_bytes(memory); !bytes) | ||
| 70 | std::snprintf(text, sizeof text, "RAM n/a (ws %.1f)", gb(memory.working_set)); | ||
| 71 | else | ||
| 72 | std::snprintf(text, sizeof text, "RAM %.1f GB (ws %.1f)", gb(bytes), gb(memory.working_set)); | ||
| 73 | return text; | ||
| 74 | } | ||
| 75 | |||
| 76 | #ifdef _WIN32 | ||
| 77 | // This process's figures now. | ||
| 78 | inline ProcessMemory process_memory() { | ||
| 79 | PROCESS_MEMORY_COUNTERS_EX counters{}; | ||
| 80 | counters.cb = sizeof counters; | ||
| 81 | ProcessMemory out; | ||
| 82 | if (!GetProcessMemoryInfo(GetCurrentProcess(), reinterpret_cast<PROCESS_MEMORY_COUNTERS*>(&counters), | ||
| 83 | sizeof counters)) | ||
| 84 | return out; | ||
| 85 | out.read = true; | ||
| 86 | out.private_usage = counters.PrivateUsage; | ||
| 87 | out.pagefile_usage = counters.PagefileUsage; | ||
| 88 | out.working_set = counters.WorkingSetSize; | ||
| 89 | return out; | ||
| 90 | } | ||
| 91 | #endif | ||
| 92 | |||
| 93 | // The device's figure, published for views outside the device. A device with VK_EXT_memory_budget publishes a reader | ||
| 94 | // when it is created and withdraws it first thing when it goes. The views call latest_vram(), which calls the reader at | ||
| 95 | // most once per refresh_ms and returns the last figure in between. The lock is held during a read, so withdrawing | ||
| 96 | // waits for a read that is in progress, and a reader is never called after its device is gone. | ||
| 97 | using VramReader = VramBudget (*)(const void* context); | ||
| 98 | inline constexpr std::chrono::milliseconds refresh_ms{500}; | ||
| 99 | |||
| 100 | class VramPublisher { | ||
| 101 | public: | ||
| 102 | void publish(VramReader reader, const void* context) { | ||
| 103 | std::lock_guard lock(mutex_); | ||
| 104 | reader_ = reader; | ||
| 105 | context_ = context; | ||
| 106 | last_ = {}; | ||
| 107 | read_ = false; | ||
| 108 | } | ||
| 109 | // Only the device that published the current reader withdraws it. Another device may have published since. | ||
| 110 | void withdraw(const void* context) { | ||
| 111 | std::lock_guard lock(mutex_); | ||
| 112 | if (context_ != context) return; | ||
| 113 | reader_ = nullptr; | ||
| 114 | context_ = nullptr; | ||
| 115 | last_ = {}; | ||
| 116 | read_ = false; | ||
| 117 | } | ||
| 118 | VramBudget latest(std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now()) { | ||
| 119 | std::lock_guard lock(mutex_); | ||
| 120 | if (!reader_) return {}; | ||
| 121 | if (!read_ || now - at_ >= refresh_ms) { | ||
| 122 | last_ = reader_(context_); | ||
| 123 | at_ = now; | ||
| 124 | read_ = true; | ||
| 125 | } | ||
| 126 | return last_; | ||
| 127 | } | ||
| 128 | |||
| 129 | private: | ||
| 130 | std::mutex mutex_; | ||
| 131 | VramReader reader_ = nullptr; | ||
| 132 | const void* context_ = nullptr; | ||
| 133 | VramBudget last_; | ||
| 134 | std::chrono::steady_clock::time_point at_{}; | ||
| 135 | bool read_ = false; | ||
| 136 | }; | ||
| 137 | |||
| 138 | inline VramPublisher& vram_publisher() { | ||
| 139 | static VramPublisher instance; | ||
| 140 | return instance; | ||
| 141 | } | ||
| 142 | inline void publish_vram_reader(VramReader reader, const void* context) { vram_publisher().publish(reader, context); } | ||
| 143 | inline void withdraw_vram_reader(const void* context) { vram_publisher().withdraw(context); } | ||
| 144 | inline VramBudget latest_vram() { return vram_publisher().latest(); } | ||
| 145 | |||
| 146 | } // namespace memory_figures | ||
| 147 | } // 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 @@ | |||
| 1 | #pragma once | 1 | #pragma once |
| 2 | 2 | ||
| 3 | // VK_EXT_memory_budget: how much GPU memory this process uses and may use, read from the Vulkan device itself. The | 3 | // VK_EXT_memory_budget: how much GPU memory this process uses and may use, read from the Vulkan device itself. The |
| 4 | // title HUD (shims/title_hud.h, LEONIDA_TITLE_HUD) shows it; DXGI's QueryVideoMemoryInfo, which it read before, is 0 | 4 | // device publishes it (memory_figures.h) for the title HUD (shims/title_hud.h, LEONIDA_TITLE_HUD) and the dev overlay |
| 5 | // under Wine. The sums depend only on the two Vulkan structures, so a test can feed them made-up heaps. | 5 | // (dev_overlay.h, LEONIDA_DEV_OVERLAY); DXGI's QueryVideoMemoryInfo, which the HUD read before, is 0 under Wine. The |
| 6 | // sums depend only on the two Vulkan structures, so a test can feed them made-up heaps. | ||
| 6 | 7 | ||
| 7 | #ifndef VK_NO_PROTOTYPES | 8 | #ifndef VK_NO_PROTOTYPES |
| 8 | #define VK_NO_PROTOTYPES | 9 | #define VK_NO_PROTOTYPES |
| @@ -11,16 +12,9 @@ | |||
| 11 | 12 | ||
| 12 | #include <cstdint> | 13 | #include <cstdint> |
| 13 | 14 | ||
| 14 | namespace leonida { | 15 | #include "memory_figures.h" // VramBudget |
| 15 | 16 | ||
| 16 | struct VramBudget { | 17 | namespace leonida { |
| 17 | // False when there is nothing to show: no VK_EXT_memory_budget on the device, no query, or a device that reports | ||
| 18 | // no budget for any DEVICE_LOCAL heap. Callers show "n/a" then, not a 0. | ||
| 19 | bool available = false; | ||
| 20 | std::uint64_t local_usage = 0; // heapUsage summed over the DEVICE_LOCAL heaps | ||
| 21 | std::uint64_t local_budget = 0; // heapBudget summed over the DEVICE_LOCAL heaps | ||
| 22 | std::uint64_t shared_usage = 0; // heapUsage summed over the other heaps: system memory the GPU uses | ||
| 23 | }; | ||
| 24 | 18 | ||
| 25 | inline VramBudget sum_vram_budget(const VkPhysicalDeviceMemoryProperties& memory, | 19 | inline VramBudget sum_vram_budget(const VkPhysicalDeviceMemoryProperties& memory, |
| 26 | const VkPhysicalDeviceMemoryBudgetPropertiesEXT& budget) { | 20 | const VkPhysicalDeviceMemoryBudgetPropertiesEXT& budget) { |
| @@ -39,7 +33,7 @@ inline VramBudget sum_vram_budget(const VkPhysicalDeviceMemoryProperties& memory | |||
| 39 | 33 | ||
| 40 | // Reads the budget now. `physical` must have VK_EXT_memory_budget (enabled on the logical device, as the runtime's | 34 | // Reads the budget now. `physical` must have VK_EXT_memory_budget (enabled on the logical device, as the runtime's |
| 41 | // device does). One vkGetPhysicalDeviceMemoryProperties2 call, which needs no external synchronization, so another | 35 | // device does). One vkGetPhysicalDeviceMemoryProperties2 call, which needs no external synchronization, so another |
| 42 | // thread (the HUD's) may call it while the device is in use. | 36 | // thread (the HUD's or the overlay's) may call it while the device is in use. |
| 43 | inline VramBudget query_vram_budget(PFN_vkGetPhysicalDeviceMemoryProperties2 get_memory_properties2, | 37 | inline VramBudget query_vram_budget(PFN_vkGetPhysicalDeviceMemoryProperties2 get_memory_properties2, |
| 44 | VkPhysicalDevice physical) { | 38 | VkPhysicalDevice physical) { |
| 45 | if (!get_memory_properties2 || physical == VK_NULL_HANDLE) return {}; | 39 | if (!get_memory_properties2 || physical == VK_NULL_HANDLE) return {}; |