upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/src/thread_stacks.cpp
diff options
context:
space:
mode:
authorJan Trencansky <jan.trencansky@protonmail.com>2026-11-25 19:04:24 -0500
committerJan Trencansky <jan.trencansky@protonmail.com>2026-11-25 19:04:24 -0500
commit8d832fd10913064e1c8aec74191e3ba8f70211c8 (patch)
treee365199d7d86ebf592a5a0dc2f07cda4b74b58a7 /runtime/src/thread_stacks.cpp
parent5da3b6be3cefc1499452ba8c8370a50092674d61 (diff)
parentc50ed94b35b37b697ab9c5fd176f3738480b4db5 (diff)
Merge main into rt-title-every-budget (#2122)
#2098 split the every-pass loop of refresh_title_textures by mode: with LEONIDA_TEXTURE_REFRESH_ASYNC every persistent mapping is polled each pass and only stale unmapped textures are visited, without it all mapped or stale ones are. The async branch is kept as main has it. LEONIDA_TITLE_EVERY_BUDGET_US applies to the synchronous pass, the one before a submission on the submitting thread; under async the pass runs on the refresh worker and the limit does not apply (README says so). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/src/thread_stacks.cpp')
-rw-r--r--runtime/src/thread_stacks.cpp30
1 files changed, 23 insertions, 7 deletions
diff --git a/runtime/src/thread_stacks.cpp b/runtime/src/thread_stacks.cpp
index 67188d908..38d7cc2a4 100644
--- a/runtime/src/thread_stacks.cpp
+++ b/runtime/src/thread_stacks.cpp
@@ -121,10 +121,10 @@ struct ThreadTrace {
121 size_t count = 0; 121 size_t count = 0;
122}; 122};
123 123
124void print_traces(unsigned at, const std::vector<ThreadTrace>& traces) { 124void print_traces(const std::string& tag, const std::vector<ThreadTrace>& traces) {
125 std::string out = "[rt] stacks t=" + std::to_string(at) + ": " + std::to_string(traces.size()) + " threads\n"; 125 std::string out = "[rt] stacks " + tag + ": " + std::to_string(traces.size()) + " threads\n";
126 for (auto it = traces.begin(); it != traces.end(); ++it) { 126 for (auto it = traces.begin(); it != traces.end(); ++it) {
127 out += "[rt] stacks t=" + std::to_string(at) + " thread " + std::to_string(it->tid) + " (" + it->label + "):\n"; 127 out += "[rt] stacks " + tag + " thread " + std::to_string(it->tid) + " (" + it->label + "):\n";
128 const auto same = std::find_if(traces.begin(), it, [&](const ThreadTrace& earlier) { 128 const auto same = std::find_if(traces.begin(), it, [&](const ThreadTrace& earlier) {
129 return it->count && it->count == earlier.count && 129 return it->count && it->count == earlier.count &&
130 std::equal(it->frames.begin(), it->frames.begin() + it->count, earlier.frames.begin()); 130 std::equal(it->frames.begin(), it->frames.begin() + it->count, earlier.frames.begin());
@@ -142,10 +142,11 @@ void print_traces(unsigned at, const std::vector<ThreadTrace>& traces) {
142 std::fflush(stderr); 142 std::fflush(stderr);
143} 143}
144 144
145void snapshot_threads(unsigned at, StackSampler& sampler, GetDescription describe, QueryThread query) { 145void snapshot_threads(const std::string& tag, StackSampler& sampler, GetDescription describe, QueryThread query,
146 DWORD only = 0) {
146 const HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0); 147 const HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
147 if (snapshot == INVALID_HANDLE_VALUE) { 148 if (snapshot == INVALID_HANDLE_VALUE) {
148 std::fprintf(stderr, "[rt] stacks t=%u: thread list failed (error %lu)\n", at, GetLastError()); 149 std::fprintf(stderr, "[rt] stacks %s: thread list failed (error %lu)\n", tag.c_str(), GetLastError());
149 return; 150 return;
150 } 151 }
151 std::vector<ThreadTrace> traces; 152 std::vector<ThreadTrace> traces;
@@ -157,6 +158,7 @@ void snapshot_threads(unsigned at, StackSampler& sampler, GetDescription describ
157 for (BOOL ok = Thread32First(snapshot, &entry); ok; ok = Thread32Next(snapshot, &entry)) { 158 for (BOOL ok = Thread32First(snapshot, &entry); ok; ok = Thread32Next(snapshot, &entry)) {
158 entry.dwSize = sizeof(entry); 159 entry.dwSize = sizeof(entry);
159 if (entry.th32OwnerProcessID != pid || entry.th32ThreadID == self) continue; 160 if (entry.th32OwnerProcessID != pid || entry.th32ThreadID == self) continue;
161 if (only && entry.th32ThreadID != only) continue;
160 const HANDLE thread = OpenThread(THREAD_SUSPEND_RESUME | THREAD_GET_CONTEXT | THREAD_QUERY_INFORMATION, 162 const HANDLE thread = OpenThread(THREAD_SUSPEND_RESUME | THREAD_GET_CONTEXT | THREAD_QUERY_INFORMATION,
161 FALSE, entry.th32ThreadID); 163 FALSE, entry.th32ThreadID);
162 if (!thread) continue; 164 if (!thread) continue;
@@ -169,7 +171,7 @@ void snapshot_threads(unsigned at, StackSampler& sampler, GetDescription describ
169 CloseHandle(thread); 171 CloseHandle(thread);
170 } 172 }
171 CloseHandle(snapshot); 173 CloseHandle(snapshot);
172 print_traces(at, traces); 174 print_traces(tag, traces);
173} 175}
174 176
175} // namespace 177} // namespace
@@ -207,6 +209,20 @@ bool parse_stack_times(std::string_view text, std::vector<unsigned>& seconds) {
207 return true; 209 return true;
208} 210}
209 211
212void print_thread_stacks_now(std::uint32_t tid, const char* why) noexcept {
213 static std::mutex mutex;
214 try {
215 std::lock_guard lock(mutex);
216 static StackSampler sampler;
217 const auto describe = reinterpret_cast<GetDescription>(reinterpret_cast<void*>(
218 GetProcAddress(GetModuleHandleW(L"kernel32.dll"), "GetThreadDescription")));
219 const auto query = reinterpret_cast<QueryThread>(reinterpret_cast<void*>(
220 GetProcAddress(GetModuleHandleW(L"ntdll.dll"), "NtQueryInformationThread")));
221 snapshot_threads(why ? why : "now", sampler, describe, query, tid);
222 } catch (...) {
223 }
224}
225
210std::jthread start_thread_stacks() { 226std::jthread start_thread_stacks() {
211 const auto start = Clock::now(); 227 const auto start = Clock::now();
212 char spec[1024]; 228 char spec[1024];
@@ -229,7 +245,7 @@ std::jthread start_thread_stacks() {
229 for (const unsigned at : times) { 245 for (const unsigned at : times) {
230 wake.wait_until(lock, stop, start + std::chrono::seconds(at), [] { return false; }); 246 wake.wait_until(lock, stop, start + std::chrono::seconds(at), [] { return false; });
231 if (stop.stop_requested()) break; 247 if (stop.stop_requested()) break;
232 snapshot_threads(at, sampler, describe, query); 248 snapshot_threads("t=" + std::to_string(at), sampler, describe, query);
233 } 249 }
234 }); 250 });
235} 251}