upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/src/memory_figures.h
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/src/memory_figures.h')
-rw-r--r--runtime/src/memory_figures.h147
1 files changed, 147 insertions, 0 deletions
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
29namespace leonida {
30
31struct 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
40namespace memory_figures {
41
42inline 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".
45inline 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
53struct 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.
60inline 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.
65inline 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.
78inline 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.
97using VramReader = VramBudget (*)(const void* context);
98inline constexpr std::chrono::milliseconds refresh_ms{500};
99
100class VramPublisher {
101public:
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
129private:
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
138inline VramPublisher& vram_publisher() {
139 static VramPublisher instance;
140 return instance;
141}
142inline void publish_vram_reader(VramReader reader, const void* context) { vram_publisher().publish(reader, context); }
143inline void withdraw_vram_reader(const void* context) { vram_publisher().withdraw(context); }
144inline VramBudget latest_vram() { return vram_publisher().latest(); }
145
146} // namespace memory_figures
147} // namespace leonida