diff options
| author | Snooozs <0randale196@gmail.com> | 2026-11-25 23:28:38 +0100 |
|---|---|---|
| committer | Snooozs <0randale196@gmail.com> | 2026-11-25 23:28:38 +0100 |
| commit | f6cb4aedfc01d8e8421122643ea668ae71a6abf5 (patch) | |
| tree | 642a065b7a806a1f241816b97770da8031c6a16e /runtime/src | |
| parent | d601736ced20708d5d2b0f60374d549701473405 (diff) | |
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 <noreply@anthropic.com>
Diffstat (limited to 'runtime/src')
| -rw-r--r-- | runtime/src/dev_overlay.h | 278 | ||||
| -rw-r--r-- | runtime/src/dev_status.h | 51 | ||||
| -rw-r--r-- | runtime/src/main.cpp | 2 | ||||
| -rw-r--r-- | runtime/src/submit_phases.h | 44 |
4 files changed, 373 insertions, 2 deletions
diff --git a/runtime/src/dev_overlay.h b/runtime/src/dev_overlay.h new file mode 100644 index 000000000..fe38031db --- /dev/null +++ b/runtime/src/dev_overlay.h | |||
| @@ -0,0 +1,278 @@ | |||
| 1 | #pragma once | ||
| 2 | // LEONIDA_DEV_OVERLAY=1 (off by default, Windows): a small click-through window over the top left corner of the title's | ||
| 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, | ||
| 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 | ||
| 7 | // and dev_status.h. LEONIDA_TITLE_HUD (shims/title_hud.h) is the other view: frame times, memory and compile time in the | ||
| 8 | // window's title bar. | ||
| 9 | #include "dev_status.h" | ||
| 10 | #include "indirect_draw_stats.h" | ||
| 11 | #include "submit_phases.h" | ||
| 12 | |||
| 13 | #ifdef _WIN32 | ||
| 14 | #ifndef NOMINMAX | ||
| 15 | #define NOMINMAX | ||
| 16 | #endif | ||
| 17 | #include <windows.h> | ||
| 18 | #include <psapi.h> | ||
| 19 | #ifdef _MSC_VER | ||
| 20 | #pragma comment(lib, "user32.lib") | ||
| 21 | #pragma comment(lib, "gdi32.lib") | ||
| 22 | #pragma comment(lib, "psapi.lib") | ||
| 23 | #endif | ||
| 24 | #endif | ||
| 25 | |||
| 26 | #include <algorithm> | ||
| 27 | #include <cstdint> | ||
| 28 | #include <cstdio> | ||
| 29 | #include <cstdlib> | ||
| 30 | #include <string> | ||
| 31 | #include <thread> | ||
| 32 | #include <vector> | ||
| 33 | |||
| 34 | namespace leonida::dev_overlay { | ||
| 35 | |||
| 36 | // The counters between two looks. A total that went down was reset by its own log line: what it holds is the new part. | ||
| 37 | inline std::uint64_t grown(std::uint64_t now, std::uint64_t& before) { | ||
| 38 | const std::uint64_t by = now >= before ? now - before : now; | ||
| 39 | before = now; | ||
| 40 | return by; | ||
| 41 | } | ||
| 42 | |||
| 43 | struct Sample { | ||
| 44 | double seconds = 0; | ||
| 45 | double frames = 0, submissions = 0; // a second | ||
| 46 | double phase_ms[submit_phases::kCount]{}; // a submission | ||
| 47 | int phase = -1; // running now | ||
| 48 | double phase_for = 0; // seconds it has been | ||
| 49 | unsigned building = 0; | ||
| 50 | std::uint64_t built = 0; | ||
| 51 | std::string build; // the last one started | ||
| 52 | unsigned long long drawn = 0, skipped = 0; // indirect draws so far | ||
| 53 | double cores = 0, process_gb = 0, free_gb = 0; | ||
| 54 | }; | ||
| 55 | |||
| 56 | // The text of one sample: pure, for the test. | ||
| 57 | inline std::vector<std::string> lines(const Sample& s, double age) { | ||
| 58 | char text[160]; | ||
| 59 | std::vector<std::string> out; | ||
| 60 | std::snprintf(text, sizeof text, "Leonida dev %.0f s %.1f fps %.1f submissions/s", age, s.frames, s.submissions); | ||
| 61 | out.emplace_back(text); | ||
| 62 | double total = 0; | ||
| 63 | for (const double ms : s.phase_ms) total += ms; | ||
| 64 | std::snprintf(text, sizeof text, "CPU/submission %.1f ms: descriptors %.1f, textures %.1f, copies %.1f, replay %.1f", total, | ||
| 65 | s.phase_ms[submit_phases::heaps], s.phase_ms[submit_phases::textures], | ||
| 66 | s.phase_ms[submit_phases::shadows] + s.phase_ms[submit_phases::geometry], | ||
| 67 | s.phase_ms[submit_phases::replay]); | ||
| 68 | out.emplace_back(text); | ||
| 69 | static const char* const now[submit_phases::kCount] = {"waiting for the GPU", "fixing descriptors", "copying title memory", | ||
| 70 | "copying geometry", "refreshing textures", "recording and submitting"}; | ||
| 71 | if (s.phase >= 0 && s.phase < int(submit_phases::kCount)) | ||
| 72 | std::snprintf(text, sizeof text, "now: %s%s", now[s.phase], | ||
| 73 | s.phase_for >= 1 ? (" for " + std::to_string(int(s.phase_for)) + " s").c_str() : ""); | ||
| 74 | else | ||
| 75 | std::snprintf(text, sizeof text, "now: the title's own code"); | ||
| 76 | out.emplace_back(text); | ||
| 77 | if (s.building) | ||
| 78 | std::snprintf(text, sizeof text, "pipelines: %llu built, %u building (%.50s)", | ||
| 79 | static_cast<unsigned long long>(s.built), s.building, s.build.c_str()); | ||
| 80 | else | ||
| 81 | std::snprintf(text, sizeof text, "pipelines: %llu built", static_cast<unsigned long long>(s.built)); | ||
| 82 | out.emplace_back(text); | ||
| 83 | std::snprintf(text, sizeof text, "indirect draws: %llu drawn, %llu skipped", s.drawn, s.skipped); | ||
| 84 | 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); | ||
| 86 | out.emplace_back(text); | ||
| 87 | return out; | ||
| 88 | } | ||
| 89 | |||
| 90 | inline bool enabled() { | ||
| 91 | static const bool on = [] { | ||
| 92 | #ifdef _WIN32 | ||
| 93 | char value[3]{}; | ||
| 94 | std::size_t length = 0; | ||
| 95 | return getenv_s(&length, value, sizeof value, "LEONIDA_DEV_OVERLAY") == 0 && length == 2 && value[0] == '1'; | ||
| 96 | #else | ||
| 97 | const char* value = std::getenv("LEONIDA_DEV_OVERLAY"); | ||
| 98 | return value && value[0] == '1' && !value[1]; | ||
| 99 | #endif | ||
| 100 | }(); | ||
| 101 | return on; | ||
| 102 | } | ||
| 103 | |||
| 104 | #ifdef _WIN32 | ||
| 105 | class Overlay { | ||
| 106 | public: | ||
| 107 | void run() { | ||
| 108 | WNDCLASSW type{}; | ||
| 109 | type.lpfnWndProc = &Overlay::procedure; | ||
| 110 | type.hInstance = GetModuleHandleW(nullptr); | ||
| 111 | type.lpszClassName = L"LeonidaDevOverlay"; | ||
| 112 | if (!RegisterClassW(&type)) return; | ||
| 113 | window_ = CreateWindowExW(WS_EX_LAYERED | WS_EX_TRANSPARENT | WS_EX_NOACTIVATE | WS_EX_TOPMOST | WS_EX_TOOLWINDOW, | ||
| 114 | type.lpszClassName, L"", WS_POPUP, 0, 0, 10, 10, nullptr, nullptr, type.hInstance, this); | ||
| 115 | if (!window_) return; | ||
| 116 | SetWindowLongPtrW(window_, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this)); | ||
| 117 | SetLayeredWindowAttributes(window_, 0, 205, LWA_ALPHA); | ||
| 118 | font_ = CreateFontW(-14, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, | ||
| 119 | CLIP_DEFAULT_PRECIS, CLEARTYPE_QUALITY, FIXED_PITCH | FF_MODERN, L"Consolas"); | ||
| 120 | look(); | ||
| 121 | SetTimer(window_, 1, 500, nullptr); | ||
| 122 | MSG message; | ||
| 123 | while (GetMessageW(&message, nullptr, 0, 0) > 0) DispatchMessageW(&message); | ||
| 124 | } | ||
| 125 | |||
| 126 | private: | ||
| 127 | static LRESULT CALLBACK procedure(HWND window, UINT message, WPARAM w, LPARAM l) { | ||
| 128 | auto* self = reinterpret_cast<Overlay*>(GetWindowLongPtrW(window, GWLP_USERDATA)); | ||
| 129 | if (self && message == WM_TIMER) { | ||
| 130 | self->look(); | ||
| 131 | return 0; | ||
| 132 | } | ||
| 133 | if (self && message == WM_PAINT) { | ||
| 134 | self->paint(); | ||
| 135 | return 0; | ||
| 136 | } | ||
| 137 | if (message == WM_NCHITTEST) return HTTRANSPARENT; | ||
| 138 | return DefWindowProcW(window, message, w, l); | ||
| 139 | } | ||
| 140 | |||
| 141 | // The process's largest visible top-level window is the title's. | ||
| 142 | struct Search { | ||
| 143 | HWND own, found; | ||
| 144 | std::int64_t area; | ||
| 145 | }; | ||
| 146 | static BOOL CALLBACK consider(HWND window, LPARAM parameter) { | ||
| 147 | auto& search = *reinterpret_cast<Search*>(parameter); | ||
| 148 | DWORD process = 0; | ||
| 149 | GetWindowThreadProcessId(window, &process); | ||
| 150 | RECT r{}; | ||
| 151 | if (process != GetCurrentProcessId() || window == search.own || !IsWindowVisible(window) || IsIconic(window) || | ||
| 152 | !GetClientRect(window, &r)) | ||
| 153 | return TRUE; | ||
| 154 | const std::int64_t area = std::int64_t(r.right) * r.bottom; | ||
| 155 | if (area > search.area) search = {search.own, window, area}; | ||
| 156 | return TRUE; | ||
| 157 | } | ||
| 158 | |||
| 159 | void look() { | ||
| 160 | namespace sp = submit_phases; | ||
| 161 | sp::Totals& totals = sp::totals(); | ||
| 162 | const std::int64_t now = sp::now_ns(); | ||
| 163 | Sample s; | ||
| 164 | s.seconds = before_ ? double(now - before_) * 1e-9 : 0; | ||
| 165 | const std::uint64_t frames = grown(totals.present_serial.load(std::memory_order_relaxed), frames_); | ||
| 166 | const std::uint64_t submissions = grown(totals.all_submissions.load(std::memory_order_relaxed), submissions_); | ||
| 167 | std::uint64_t ns[sp::kCount]; | ||
| 168 | for (unsigned p = 0; p < sp::kCount; ++p) ns[p] = grown(totals.ns[p].load(std::memory_order_relaxed), ns_[p]); | ||
| 169 | // A stall longer than a look leaves nothing to divide by: the figures then cover the last two seconds. | ||
| 170 | window_frames_ += frames; | ||
| 171 | window_submissions_ += submissions; | ||
| 172 | window_seconds_ += s.seconds; | ||
| 173 | for (unsigned p = 0; p < sp::kCount; ++p) window_ns_[p] += ns[p]; | ||
| 174 | if (window_seconds_ >= 2 || !before_) { | ||
| 175 | shown_ = {}; | ||
| 176 | if (window_seconds_ > 0) { | ||
| 177 | shown_.frames = double(window_frames_) / window_seconds_; | ||
| 178 | shown_.submissions = double(window_submissions_) / window_seconds_; | ||
| 179 | } | ||
| 180 | for (unsigned p = 0; p < sp::kCount; ++p) { | ||
| 181 | if (window_submissions_) shown_.phase_ms[p] = double(window_ns_[p]) * 1e-6 / double(window_submissions_); | ||
| 182 | window_ns_[p] = 0; | ||
| 183 | } | ||
| 184 | window_frames_ = window_submissions_ = 0; | ||
| 185 | window_seconds_ = 0; | ||
| 186 | } | ||
| 187 | s.frames = shown_.frames; | ||
| 188 | s.submissions = shown_.submissions; | ||
| 189 | std::copy(std::begin(shown_.phase_ms), std::end(shown_.phase_ms), std::begin(s.phase_ms)); | ||
| 190 | before_ = now; | ||
| 191 | |||
| 192 | const sp::Running running = sp::running(); | ||
| 193 | s.phase = running.phase; | ||
| 194 | if (s.phase >= 0) s.phase_for = std::max(0.0, double(now - running.since) * 1e-9); | ||
| 195 | dev_status::Builds& builds = dev_status::builds(); | ||
| 196 | s.building = builds.active.load(std::memory_order_relaxed); | ||
| 197 | s.built = builds.done.load(std::memory_order_relaxed); | ||
| 198 | s.build = builds.last(); | ||
| 199 | const auto& draws = indirect_stats::counts().values; | ||
| 200 | s.drawn = draws[std::size_t(indirect_stats::Event::emitted)].load(std::memory_order_relaxed); | ||
| 201 | s.skipped = draws[std::size_t(indirect_stats::Event::record_skip)].load(std::memory_order_relaxed) + | ||
| 202 | draws[std::size_t(indirect_stats::Event::replay_skip)].load(std::memory_order_relaxed); | ||
| 203 | FILETIME created{}, exited{}, kernel{}, user{}; | ||
| 204 | if (GetProcessTimes(GetCurrentProcess(), &created, &exited, &kernel, &user)) { | ||
| 205 | const auto ticks = [](const FILETIME& f) { return (std::uint64_t(f.dwHighDateTime) << 32) | f.dwLowDateTime; }; | ||
| 206 | const std::uint64_t used = grown(ticks(kernel) + ticks(user), cpu_); | ||
| 207 | if (s.seconds > 0) s.cores = double(used) * 1e-7 / s.seconds; | ||
| 208 | } | ||
| 209 | PROCESS_MEMORY_COUNTERS memory{sizeof memory}; | ||
| 210 | if (GetProcessMemoryInfo(GetCurrentProcess(), &memory, sizeof memory)) | ||
| 211 | s.process_gb = double(memory.WorkingSetSize) / double(1u << 30); | ||
| 212 | MEMORYSTATUSEX status{sizeof status}; | ||
| 213 | if (GlobalMemoryStatusEx(&status)) s.free_gb = double(status.ullAvailPhys) / double(1u << 30); | ||
| 214 | lines_ = lines(s, sp::process_age()); | ||
| 215 | place(); | ||
| 216 | } | ||
| 217 | |||
| 218 | void place() { | ||
| 219 | Search search{window_, nullptr, 0}; | ||
| 220 | EnumWindows(&Overlay::consider, reinterpret_cast<LPARAM>(&search)); | ||
| 221 | // Shown only over the title while it is the window in front, so the topmost text is never over anything else. | ||
| 222 | if (!search.found || GetForegroundWindow() != search.found) { | ||
| 223 | ShowWindow(window_, SW_HIDE); | ||
| 224 | return; | ||
| 225 | } | ||
| 226 | POINT corner{8, 8}; | ||
| 227 | ClientToScreen(search.found, &corner); | ||
| 228 | std::size_t longest = 0; | ||
| 229 | for (const auto& line : lines_) longest = std::max(longest, line.size()); | ||
| 230 | SetWindowPos(window_, HWND_TOPMOST, corner.x, corner.y, int(longest) * 8 + 12, int(lines_.size()) * 17 + 8, | ||
| 231 | SWP_NOACTIVATE | SWP_SHOWWINDOW); | ||
| 232 | InvalidateRect(window_, nullptr, FALSE); | ||
| 233 | } | ||
| 234 | |||
| 235 | void paint() { | ||
| 236 | PAINTSTRUCT ps; | ||
| 237 | HDC dc = BeginPaint(window_, &ps); | ||
| 238 | RECT area; | ||
| 239 | GetClientRect(window_, &area); | ||
| 240 | HDC memory = CreateCompatibleDC(dc); | ||
| 241 | HBITMAP bitmap = CreateCompatibleBitmap(dc, area.right, area.bottom); | ||
| 242 | HGDIOBJ old_bitmap = SelectObject(memory, bitmap), old_font = SelectObject(memory, font_); | ||
| 243 | FillRect(memory, &area, static_cast<HBRUSH>(GetStockObject(BLACK_BRUSH))); | ||
| 244 | SetBkMode(memory, TRANSPARENT); | ||
| 245 | int y = 4; | ||
| 246 | for (std::size_t i = 0; i < lines_.size(); ++i, y += 17) { | ||
| 247 | SetTextColor(memory, i == 0 ? RGB(120, 255, 140) : RGB(235, 235, 235)); | ||
| 248 | TextOutA(memory, 6, y, lines_[i].data(), int(lines_[i].size())); | ||
| 249 | } | ||
| 250 | BitBlt(dc, 0, 0, area.right, area.bottom, memory, 0, 0, SRCCOPY); | ||
| 251 | SelectObject(memory, old_bitmap); | ||
| 252 | SelectObject(memory, old_font); | ||
| 253 | DeleteObject(bitmap); | ||
| 254 | DeleteDC(memory); | ||
| 255 | EndPaint(window_, &ps); | ||
| 256 | } | ||
| 257 | |||
| 258 | HWND window_ = nullptr; | ||
| 259 | HFONT font_ = nullptr; | ||
| 260 | std::vector<std::string> lines_; | ||
| 261 | std::int64_t before_ = 0; | ||
| 262 | std::uint64_t frames_ = 0, submissions_ = 0, cpu_ = 0, ns_[submit_phases::kCount]{}; | ||
| 263 | std::uint64_t window_frames_ = 0, window_submissions_ = 0, window_ns_[submit_phases::kCount]{}; | ||
| 264 | double window_seconds_ = 0; | ||
| 265 | Sample shown_; | ||
| 266 | }; | ||
| 267 | #endif | ||
| 268 | |||
| 269 | // Once per process; nothing without LEONIDA_DEV_OVERLAY=1. | ||
| 270 | inline void start() { | ||
| 271 | #ifdef _WIN32 | ||
| 272 | if (!enabled()) return; | ||
| 273 | std::fprintf(stderr, "[rt] dev overlay on (LEONIDA_DEV_OVERLAY=1)\n"); | ||
| 274 | std::thread([] { static Overlay overlay; overlay.run(); }).detach(); | ||
| 275 | #endif | ||
| 276 | } | ||
| 277 | |||
| 278 | } // namespace leonida::dev_overlay | ||
diff --git a/runtime/src/dev_status.h b/runtime/src/dev_status.h new file mode 100644 index 000000000..3efc9bf4f --- /dev/null +++ b/runtime/src/dev_status.h | |||
| @@ -0,0 +1,51 @@ | |||
| 1 | #pragma once | ||
| 2 | // What the runtime is building now, for the dev overlay (dev_overlay.h). Always on: two relaxed atomics a build, and a | ||
| 3 | // short copy under a lock for the name. | ||
| 4 | #include <algorithm> | ||
| 5 | #include <atomic> | ||
| 6 | #include <cstdint> | ||
| 7 | #include <cstring> | ||
| 8 | #include <mutex> | ||
| 9 | #include <string> | ||
| 10 | |||
| 11 | namespace leonida::dev_status { | ||
| 12 | |||
| 13 | struct Builds { | ||
| 14 | std::atomic<unsigned> active{0}; | ||
| 15 | std::atomic<std::uint64_t> done{0}; | ||
| 16 | |||
| 17 | std::string last() { | ||
| 18 | std::lock_guard lock(mutex_); | ||
| 19 | return name_; | ||
| 20 | } | ||
| 21 | void name(const char* text) { | ||
| 22 | std::lock_guard lock(mutex_); | ||
| 23 | const std::size_t n = std::min(std::strlen(text), sizeof name_ - 1); | ||
| 24 | std::memcpy(name_, text, n); | ||
| 25 | name_[n] = 0; | ||
| 26 | } | ||
| 27 | |||
| 28 | private: | ||
| 29 | std::mutex mutex_; | ||
| 30 | char name_[96]{}; | ||
| 31 | }; | ||
| 32 | inline Builds& builds() { | ||
| 33 | static Builds instance; | ||
| 34 | return instance; | ||
| 35 | } | ||
| 36 | |||
| 37 | // One pipeline build, from construction to destruction. | ||
| 38 | struct Build { | ||
| 39 | explicit Build(const char* name) { | ||
| 40 | builds().name(name); | ||
| 41 | builds().active.fetch_add(1, std::memory_order_relaxed); | ||
| 42 | } | ||
| 43 | ~Build() { | ||
| 44 | builds().active.fetch_sub(1, std::memory_order_relaxed); | ||
| 45 | builds().done.fetch_add(1, std::memory_order_relaxed); | ||
| 46 | } | ||
| 47 | Build(const Build&) = delete; | ||
| 48 | Build& operator=(const Build&) = delete; | ||
| 49 | }; | ||
| 50 | |||
| 51 | } // namespace leonida::dev_status | ||
diff --git a/runtime/src/main.cpp b/runtime/src/main.cpp index 094aaa321..1adacab85 100644 --- a/runtime/src/main.cpp +++ b/runtime/src/main.cpp | |||
| @@ -149,6 +149,7 @@ void start_title_peek(std::uint8_t* base) { | |||
| 149 | #include "mods.h" | 149 | #include "mods.h" |
| 150 | #include "pe_loader.h" | 150 | #include "pe_loader.h" |
| 151 | #include "thread_stacks.h" | 151 | #include "thread_stacks.h" |
| 152 | #include "dev_overlay.h" | ||
| 152 | #include "thread_sampler.h" | 153 | #include "thread_sampler.h" |
| 153 | #include "title_page_watch.h" | 154 | #include "title_page_watch.h" |
| 154 | #include "title_geometry_watch.h" | 155 | #include "title_geometry_watch.h" |
| @@ -561,6 +562,7 @@ int wmain(int argc, wchar_t** argv) { | |||
| 561 | std::fflush(stdout); | 562 | std::fflush(stdout); |
| 562 | keep_title_watchdog_kicked(main_module.base); | 563 | keep_title_watchdog_kicked(main_module.base); |
| 563 | start_title_peek(main_module.base); | 564 | start_title_peek(main_module.base); |
| 565 | leonida::dev_overlay::start(); // LEONIDA_DEV_OVERLAY=1 | ||
| 564 | leonida::thread_sampler::start(main_module.base, main_module.size); // LEONIDA_SAMPLE_MS (diagnostic) | 566 | leonida::thread_sampler::start(main_module.base, main_module.size); // LEONIDA_SAMPLE_MS (diagnostic) |
| 565 | #ifdef LEONIDA_VULKAN_DEVICE | 567 | #ifdef LEONIDA_VULKAN_DEVICE |
| 566 | SetConsoleCtrlHandler(&stop_compiles_on_console_exit, TRUE); | 568 | SetConsoleCtrlHandler(&stop_compiles_on_console_exit, TRUE); |
diff --git a/runtime/src/submit_phases.h b/runtime/src/submit_phases.h index 84ea77b02..230240b57 100644 --- a/runtime/src/submit_phases.h +++ b/runtime/src/submit_phases.h | |||
| @@ -40,6 +40,10 @@ struct Totals { | |||
| 40 | std::atomic<std::int64_t> last_log{0}; | 40 | std::atomic<std::int64_t> last_log{0}; |
| 41 | std::atomic<std::uint64_t> presents{0}; // frames the presenter showed (VulkanPresenter), since the last line | 41 | std::atomic<std::uint64_t> presents{0}; // frames the presenter showed (VulkanPresenter), since the last line |
| 42 | std::atomic<std::uint64_t> present_serial{0}; // the same, never reset (LEONIDA_REFRESH_PER_FRAME) | 42 | std::atomic<std::uint64_t> present_serial{0}; // the same, never reset (LEONIDA_REFRESH_PER_FRAME) |
| 43 | // Threads whose innermost Scope is this phase now, and when the first of them entered it (steady_clock). Counted | ||
| 44 | // per phase, since queues submit from several threads: one value would go stale when their scopes overlap. | ||
| 45 | std::atomic<unsigned> active[kCount]{}; | ||
| 46 | std::atomic<std::int64_t> active_since[kCount]{}; | ||
| 43 | std::atomic<std::uint32_t> thread{0}; // the thread of the last submission (the thread sampler reports it apart) | 47 | std::atomic<std::uint32_t> thread{0}; // the thread of the last submission (the thread sampler reports it apart) |
| 44 | }; | 48 | }; |
| 45 | inline Totals& totals() { | 49 | inline Totals& totals() { |
| @@ -86,12 +90,48 @@ inline void add(Phase p, std::uint64_t ns) { | |||
| 86 | struct Scope { | 90 | struct Scope { |
| 87 | Phase phase; | 91 | Phase phase; |
| 88 | std::int64_t start = now_ns(); | 92 | std::int64_t start = now_ns(); |
| 89 | explicit Scope(Phase p) : phase(p) {} | 93 | int outer; // the phase of this thread's Scope around this one, -1 for none |
| 90 | ~Scope() { add(phase, std::uint64_t(now_ns() - start)); } | 94 | explicit Scope(Phase p) : phase(p), outer(innermost()) { |
| 95 | if (outer >= 0) leave(outer); | ||
| 96 | enter(int(p), start); | ||
| 97 | innermost() = int(p); | ||
| 98 | } | ||
| 99 | ~Scope() { | ||
| 100 | const std::int64_t end = now_ns(); | ||
| 101 | add(phase, std::uint64_t(end - start)); | ||
| 102 | leave(int(phase)); | ||
| 103 | if (outer >= 0) enter(outer, end); | ||
| 104 | innermost() = outer; | ||
| 105 | } | ||
| 106 | static int& innermost() { | ||
| 107 | thread_local int phase_of_this_thread = -1; | ||
| 108 | return phase_of_this_thread; | ||
| 109 | } | ||
| 110 | static void enter(int p, std::int64_t at) { | ||
| 111 | Totals& t = totals(); | ||
| 112 | if (t.active[p].fetch_add(1, std::memory_order_relaxed) == 0) t.active_since[p].store(at, std::memory_order_relaxed); | ||
| 113 | } | ||
| 114 | static void leave(int p) { totals().active[p].fetch_sub(1, std::memory_order_relaxed); } | ||
| 91 | Scope(const Scope&) = delete; | 115 | Scope(const Scope&) = delete; |
| 92 | Scope& operator=(const Scope&) = delete; | 116 | Scope& operator=(const Scope&) = delete; |
| 93 | }; | 117 | }; |
| 94 | 118 | ||
| 119 | // The phase that some thread has been in longest, -1 when none is in one. | ||
| 120 | struct Running { | ||
| 121 | int phase = -1; | ||
| 122 | std::int64_t since = 0; | ||
| 123 | }; | ||
| 124 | inline Running running() { | ||
| 125 | Totals& t = totals(); | ||
| 126 | Running r; | ||
| 127 | for (unsigned p = 0; p < kCount; ++p) { | ||
| 128 | if (!t.active[p].load(std::memory_order_relaxed)) continue; | ||
| 129 | const std::int64_t since = t.active_since[p].load(std::memory_order_relaxed); | ||
| 130 | if (r.phase < 0 || since < r.since) r = {int(p), since}; | ||
| 131 | } | ||
| 132 | return r; | ||
| 133 | } | ||
| 134 | |||
| 95 | // After each submission: counts it, and prints the line when the period has passed. One thread wins the line. | 135 | // After each submission: counts it, and prints the line when the period has passed. One thread wins the line. |
| 96 | inline void submitted() { | 136 | inline void submitted() { |
| 97 | Totals& t = totals(); | 137 | Totals& t = totals(); |