upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/src/memory_figures.h
blob: 8bf6115531d8fd52d9e1d31989802082c439b7d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
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 <windows.h>
#include <psapi.h>
#ifdef _MSC_VER
#pragma comment(lib, "psapi.lib")
#endif
#endif

#include <algorithm>
#include <chrono>
#include <cstdint>
#include <cstdio>
#include <mutex>
#include <string>

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<PROCESS_MEMORY_COUNTERS*>(&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