diff options
Diffstat (limited to 'runtime/src/thread_sampler.h')
| -rw-r--r-- | runtime/src/thread_sampler.h | 443 |
1 files changed, 443 insertions, 0 deletions
diff --git a/runtime/src/thread_sampler.h b/runtime/src/thread_sampler.h new file mode 100644 index 000000000..0c18949ce --- /dev/null +++ b/runtime/src/thread_sampler.h | |||
| @@ -0,0 +1,443 @@ | |||
| 1 | #pragma once | ||
| 2 | // LEONIDA_SAMPLE_MS=<ms> (diagnostic, off by default): where the process's busy threads spend their CPU, without a | ||
| 3 | // debugger or perf. A thread wakes every <ms>, and for each thread of the process that used CPU since its last look | ||
| 4 | // suspends it, copies its context and resumes it at once; the stack is then unwound outside the suspension (through | ||
| 5 | // the .pdata every module registers, the title's included), under __try, since the thread runs on meanwhile. Every | ||
| 6 | // LEONIDA_SAMPLE_REPORT_S seconds (10) one block lists the busiest threads (CPU share) and the most frequent call | ||
| 7 | // chains (up to eight frames, innermost first): title frames as t+RVA of the function, others as module+RVA. Nothing is | ||
| 8 | // allocated or logged while a thread is suspended, so a suspended thread's locks are never waited for. | ||
| 9 | #ifndef NOMINMAX | ||
| 10 | #define NOMINMAX | ||
| 11 | #endif | ||
| 12 | #include <windows.h> | ||
| 13 | #include <tlhelp32.h> | ||
| 14 | |||
| 15 | #include <algorithm> | ||
| 16 | #include <array> | ||
| 17 | #include <cstdint> | ||
| 18 | #include <cstdio> | ||
| 19 | #include <cstdlib> | ||
| 20 | #include <map> | ||
| 21 | #include <string> | ||
| 22 | #include <vector> | ||
| 23 | |||
| 24 | #include "submit_phases.h" | ||
| 25 | |||
| 26 | namespace leonida::thread_sampler { | ||
| 27 | |||
| 28 | constexpr int kFrames = 8; | ||
| 29 | using Chain = std::array<std::uint64_t, kFrames>; // function starts (or addresses), innermost first | ||
| 30 | // LEONIDA_SAMPLE_SHIMS=1 (with LEONIDA_SAMPLE_MS): each sample is unwound this deep to tell whether a title thread | ||
| 31 | // is inside our runtime (a frame of this executable called, at any depth, from title code), and where. | ||
| 32 | constexpr int kDeepFrames = 48; | ||
| 33 | using DeepChain = std::array<std::uint64_t, kDeepFrames>; | ||
| 34 | |||
| 35 | |||
| 36 | struct Sampler { | ||
| 37 | std::uint8_t* title = nullptr; | ||
| 38 | std::uint64_t title_size = 0; | ||
| 39 | DWORD period_ms = 10; | ||
| 40 | DWORD report_ms = 10000; | ||
| 41 | bool waits = false; // LEONIDA_SAMPLE_WAITS=1: every thread, busy or waiting (where the wall time goes) | ||
| 42 | bool shims = false; // LEONIDA_SAMPLE_SHIMS=1: per title thread, the share of its samples inside our runtime | ||
| 43 | std::uint64_t self_base = 0, self_size = 0; // this executable (the runtime) | ||
| 44 | // LEONIDA_SAMPLE_DETAIL=<id>[,<id>...]: for these threads, the 10 most frequent chains, 16 frames deep, each as a | ||
| 45 | // share of that thread's samples (thread ids are stable from boot to boot: the title's main, render and submit). | ||
| 46 | std::vector<DWORD> detail; | ||
| 47 | // LEONIDA_SAMPLE_FIND=<rva>[,<rva>...] (title function starts): which threads have the function on their stack | ||
| 48 | // (48 frames deep), how often, and their most frequent chain under it, every report. | ||
| 49 | std::vector<std::uint64_t> find; | ||
| 50 | }; | ||
| 51 | |||
| 52 | // Per title thread (one with a title frame in its chain): how many samples were inside our runtime, and through which | ||
| 53 | // entry (the runtime frame the title called) and which runtime function was innermost. | ||
| 54 | struct ShimCounts { | ||
| 55 | std::uint64_t title = 0; // samples with a title frame | ||
| 56 | std::uint64_t ours = 0; // ... of which inside our runtime | ||
| 57 | std::uint64_t ours_busy = 0; // ... while the thread used CPU since the previous look | ||
| 58 | std::uint64_t busy = 0; // samples with a title frame while the thread used CPU | ||
| 59 | std::map<std::pair<std::uint64_t, std::uint64_t>, std::uint64_t> entries; // (our entry, title caller) -> samples | ||
| 60 | std::map<std::uint64_t, std::uint64_t> inner; // innermost runtime function -> samples | ||
| 61 | std::map<std::uint64_t, std::uint64_t> leaves; // innermost frame (any module) -> samples | ||
| 62 | }; | ||
| 63 | |||
| 64 | // Classifies one deep chain (innermost first) for LEONIDA_SAMPLE_SHIMS. | ||
| 65 | inline void count_shims(const Sampler& s, const DeepChain& chain, int n, bool moved, ShimCounts& c) { | ||
| 66 | const auto title = reinterpret_cast<std::uint64_t>(s.title); | ||
| 67 | const auto in_title = [&](std::uint64_t a) { return a >= title && a < title + s.title_size; }; | ||
| 68 | const auto in_self = [&](std::uint64_t a) { return a >= s.self_base && a < s.self_base + s.self_size; }; | ||
| 69 | int first_title = -1; | ||
| 70 | for (int i = 0; i < n; ++i) | ||
| 71 | if (in_title(chain[i])) { | ||
| 72 | first_title = i; | ||
| 73 | break; | ||
| 74 | } | ||
| 75 | if (first_title < 0) return; | ||
| 76 | ++c.title; | ||
| 77 | c.busy += moved; | ||
| 78 | int entry = -1, innermost = -1; | ||
| 79 | for (int i = 0; i < first_title; ++i) | ||
| 80 | if (in_self(chain[i])) { | ||
| 81 | if (innermost < 0) innermost = i; | ||
| 82 | entry = i; // the outermost runtime frame below the title's | ||
| 83 | } | ||
| 84 | if (entry < 0) return; | ||
| 85 | ++c.ours; | ||
| 86 | c.ours_busy += moved; | ||
| 87 | ++c.entries[{chain[entry], chain[first_title]}]; | ||
| 88 | ++c.inner[chain[innermost]]; | ||
| 89 | ++c.leaves[chain[0]]; | ||
| 90 | } | ||
| 91 | |||
| 92 | // The chain of a context copied from a suspended thread: unwound under __try (its stack may be changing). | ||
| 93 | template <std::size_t N> | ||
| 94 | inline int unwind(CONTEXT context, std::array<std::uint64_t, N>& chain) { | ||
| 95 | int n = 0; | ||
| 96 | __try { | ||
| 97 | for (; n < int(N); ++n) { | ||
| 98 | const DWORD64 pc = context.Rip; | ||
| 99 | if (!pc) break; | ||
| 100 | DWORD64 image = 0; | ||
| 101 | PRUNTIME_FUNCTION entry = RtlLookupFunctionEntry(pc, &image, nullptr); | ||
| 102 | chain[n] = entry ? image + entry->BeginAddress : pc; | ||
| 103 | if (!entry) { // a leaf, or code with no unwind data: take the return address at the stack pointer | ||
| 104 | context.Rip = *reinterpret_cast<const DWORD64*>(context.Rsp); | ||
| 105 | context.Rsp += 8; | ||
| 106 | continue; | ||
| 107 | } | ||
| 108 | void* handler_data = nullptr; | ||
| 109 | DWORD64 frame = 0; | ||
| 110 | RtlVirtualUnwind(UNW_FLAG_NHANDLER, image, pc, entry, &context, &handler_data, &frame, nullptr); | ||
| 111 | } | ||
| 112 | } __except (EXCEPTION_EXECUTE_HANDLER) { | ||
| 113 | } | ||
| 114 | return n; | ||
| 115 | } | ||
| 116 | |||
| 117 | inline std::string name_of(const Sampler& s, std::uint64_t at) { | ||
| 118 | char text[96]; | ||
| 119 | const auto title = reinterpret_cast<std::uint64_t>(s.title); | ||
| 120 | if (at >= title && at < title + s.title_size) { | ||
| 121 | std::snprintf(text, sizeof(text), "t+%llx", static_cast<unsigned long long>(at - title)); | ||
| 122 | return text; | ||
| 123 | } | ||
| 124 | HMODULE module = nullptr; | ||
| 125 | if (GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, | ||
| 126 | reinterpret_cast<LPCWSTR>(at), &module) && | ||
| 127 | module) { | ||
| 128 | wchar_t path[MAX_PATH] = {}; | ||
| 129 | GetModuleFileNameW(module, path, MAX_PATH); | ||
| 130 | const wchar_t* base = path; | ||
| 131 | for (const wchar_t* p = path; *p; ++p) | ||
| 132 | if (*p == L'\\' || *p == L'/') base = p + 1; | ||
| 133 | std::snprintf(text, sizeof(text), "%ls+%llx", base, | ||
| 134 | static_cast<unsigned long long>(at - reinterpret_cast<std::uint64_t>(module))); | ||
| 135 | return text; | ||
| 136 | } | ||
| 137 | std::snprintf(text, sizeof(text), "0x%llx", static_cast<unsigned long long>(at)); | ||
| 138 | return text; | ||
| 139 | } | ||
| 140 | |||
| 141 | inline DWORD WINAPI run(LPVOID parameter) { | ||
| 142 | const Sampler s = *static_cast<Sampler*>(parameter); | ||
| 143 | SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL); | ||
| 144 | const DWORD self = GetCurrentThreadId(), pid = GetCurrentProcessId(); | ||
| 145 | struct Thread { | ||
| 146 | HANDLE handle = nullptr; | ||
| 147 | std::uint64_t cpu = 0; // 100 ns, at the last look | ||
| 148 | std::uint64_t used = 0; // since the last report | ||
| 149 | std::uint64_t samples = 0; | ||
| 150 | }; | ||
| 151 | std::map<DWORD, Thread> threads; | ||
| 152 | std::map<std::pair<DWORD, Chain>, std::uint64_t> chains; | ||
| 153 | std::map<DWORD, ShimCounts> shim_counts; | ||
| 154 | constexpr int kDetailFrames = 16; | ||
| 155 | using DetailChain = std::array<std::uint64_t, kDetailFrames>; | ||
| 156 | std::map<std::pair<DWORD, DetailChain>, std::uint64_t> detail_chains; | ||
| 157 | std::map<std::pair<std::uint64_t, DWORD>, std::map<DetailChain, std::uint64_t>> found; // (rva, thread) -> chains | ||
| 158 | std::uint64_t total = 0; | ||
| 159 | ULONGLONG listed = 0, reported = GetTickCount64(); | ||
| 160 | const auto cpu_of = [](HANDLE h) { | ||
| 161 | FILETIME created{}, exited{}, kernel{}, user{}; | ||
| 162 | if (!GetThreadTimes(h, &created, &exited, &kernel, &user)) return std::uint64_t(0); | ||
| 163 | const auto ticks = [](const FILETIME& f) { return (std::uint64_t(f.dwHighDateTime) << 32) | f.dwLowDateTime; }; | ||
| 164 | return ticks(kernel) + ticks(user); | ||
| 165 | }; | ||
| 166 | for (;;) { | ||
| 167 | Sleep(s.period_ms); | ||
| 168 | const ULONGLONG now = GetTickCount64(); | ||
| 169 | if (now - listed >= 2000) { // the thread list, every 2 s | ||
| 170 | listed = now; | ||
| 171 | const HANDLE snap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0); | ||
| 172 | if (snap != INVALID_HANDLE_VALUE) { | ||
| 173 | THREADENTRY32 e{sizeof(e)}; | ||
| 174 | for (BOOL ok = Thread32First(snap, &e); ok; ok = Thread32Next(snap, &e)) { | ||
| 175 | if (e.th32OwnerProcessID != pid || e.th32ThreadID == self || threads.count(e.th32ThreadID)) continue; | ||
| 176 | const HANDLE h = OpenThread(THREAD_SUSPEND_RESUME | THREAD_GET_CONTEXT | THREAD_QUERY_INFORMATION, | ||
| 177 | FALSE, e.th32ThreadID); | ||
| 178 | if (h) threads[e.th32ThreadID] = {h, cpu_of(h)}; | ||
| 179 | e.dwSize = sizeof(e); | ||
| 180 | } | ||
| 181 | CloseHandle(snap); | ||
| 182 | } | ||
| 183 | } | ||
| 184 | for (auto it = threads.begin(); it != threads.end();) { | ||
| 185 | Thread& t = it->second; | ||
| 186 | const std::uint64_t cpu = cpu_of(t.handle); | ||
| 187 | DWORD code = 0; | ||
| 188 | if (GetExitCodeThread(t.handle, &code) && code != STILL_ACTIVE) { | ||
| 189 | CloseHandle(t.handle); | ||
| 190 | it = threads.erase(it); | ||
| 191 | continue; | ||
| 192 | } | ||
| 193 | const bool moved = cpu != t.cpu; | ||
| 194 | const bool busy = moved || s.waits; | ||
| 195 | t.used += cpu - t.cpu; | ||
| 196 | t.cpu = cpu; | ||
| 197 | if (busy) { | ||
| 198 | CONTEXT context{}; | ||
| 199 | context.ContextFlags = CONTEXT_CONTROL | CONTEXT_INTEGER; | ||
| 200 | bool got = false; | ||
| 201 | if (SuspendThread(t.handle) != DWORD(-1)) { | ||
| 202 | got = GetThreadContext(t.handle, &context) != FALSE; | ||
| 203 | ResumeThread(t.handle); | ||
| 204 | } | ||
| 205 | if (got) { | ||
| 206 | Chain chain{}; | ||
| 207 | if (std::find(s.detail.begin(), s.detail.end(), it->first) != s.detail.end()) { | ||
| 208 | DetailChain deep{}; | ||
| 209 | unwind(context, deep); | ||
| 210 | ++detail_chains[{it->first, deep}]; | ||
| 211 | } | ||
| 212 | if (s.shims || !s.find.empty()) { | ||
| 213 | DeepChain deep{}; | ||
| 214 | const int n = unwind(context, deep); | ||
| 215 | std::copy_n(deep.begin(), kFrames, chain.begin()); | ||
| 216 | if (s.shims) count_shims(s, deep, n, moved, shim_counts[it->first]); | ||
| 217 | const auto title = reinterpret_cast<std::uint64_t>(s.title); | ||
| 218 | for (const std::uint64_t rva : s.find) | ||
| 219 | for (int f = 0; f < n; ++f) | ||
| 220 | if (deep[f] == title + rva) { | ||
| 221 | DetailChain inner{}; | ||
| 222 | std::copy_n(deep.begin(), (std::min)(kDetailFrames, n), inner.begin()); | ||
| 223 | ++found[{rva, it->first}][inner]; | ||
| 224 | break; | ||
| 225 | } | ||
| 226 | } else { | ||
| 227 | unwind(context, chain); | ||
| 228 | } | ||
| 229 | ++chains[{it->first, chain}]; | ||
| 230 | ++t.samples; | ||
| 231 | ++total; | ||
| 232 | } | ||
| 233 | } | ||
| 234 | ++it; | ||
| 235 | } | ||
| 236 | if (now - reported < s.report_ms || !total) continue; | ||
| 237 | const double seconds = double(now - reported) / 1000.0; | ||
| 238 | reported = now; | ||
| 239 | std::vector<std::pair<std::uint64_t, DWORD>> busiest; | ||
| 240 | for (auto& [id, t] : threads) | ||
| 241 | if (t.used) busiest.emplace_back(t.used, id); | ||
| 242 | std::sort(busiest.rbegin(), busiest.rend()); | ||
| 243 | std::string line; | ||
| 244 | for (std::size_t i = 0; i < busiest.size() && i < 8; ++i) { | ||
| 245 | char text[64]; | ||
| 246 | std::snprintf(text, sizeof(text), "%s%lu %.0f%%", i ? ", " : "", busiest[i].second, | ||
| 247 | double(busiest[i].first) * 1e-5 / seconds); | ||
| 248 | line += text; | ||
| 249 | } | ||
| 250 | std::fprintf(stderr, "[rt] sampler: %llu samples in %.0f s; CPU by thread: %s\n", | ||
| 251 | static_cast<unsigned long long>(total), seconds, line.c_str()); | ||
| 252 | std::vector<std::pair<std::uint64_t, const std::pair<DWORD, Chain>*>> top; | ||
| 253 | for (const auto& c : chains) top.emplace_back(c.second, &c.first); | ||
| 254 | std::sort(top.begin(), top.end(), [](const auto& a, const auto& b) { return a.first > b.first; }); | ||
| 255 | const auto print = [&](const auto& entry, std::uint64_t of, const char* what) { | ||
| 256 | std::string frames; | ||
| 257 | for (int f = 0; f < kFrames && entry.second->second[f]; ++f) | ||
| 258 | frames += (f ? " < " : "") + name_of(s, entry.second->second[f]); | ||
| 259 | std::fprintf(stderr, "[rt] sampler: %s %5.1f%% thread %lu: %s\n", what, 100.0 * double(entry.first) / double(of), | ||
| 260 | entry.second->first, frames.c_str()); | ||
| 261 | }; | ||
| 262 | for (std::size_t i = 0; i < top.size() && i < 16; ++i) print(top[i], total, " "); | ||
| 263 | // Apart: the submitting thread's chains, and the two busiest threads', each as a share of that thread's samples. | ||
| 264 | std::vector<DWORD> apart; | ||
| 265 | if (const DWORD submitter = submit_phases::totals().thread.load(std::memory_order_relaxed)) apart.push_back(submitter); | ||
| 266 | for (std::size_t i = 0; i < busiest.size() && i < 2; ++i) | ||
| 267 | if (std::find(apart.begin(), apart.end(), busiest[i].second) == apart.end()) apart.push_back(busiest[i].second); | ||
| 268 | // With waiting threads sampled: each thread's own most frequent chain, busiest first, so a thread that waits | ||
| 269 | // (the title's main thread behind its render thread, a render thread behind a lock) shows where. | ||
| 270 | if (s.waits) { | ||
| 271 | std::vector<std::pair<std::uint64_t, DWORD>> all; | ||
| 272 | for (auto& [id, t] : threads) | ||
| 273 | if (t.samples) all.emplace_back(t.used, id); | ||
| 274 | std::sort(all.rbegin(), all.rend()); | ||
| 275 | std::size_t listed = 0; | ||
| 276 | for (const auto& [used, id] : all) { | ||
| 277 | if (listed++ >= 24) break; | ||
| 278 | const auto it = threads.find(id); | ||
| 279 | if (it == threads.end() || !it->second.samples) continue; | ||
| 280 | for (const auto& entry : top) | ||
| 281 | if (entry.second->first == id) { | ||
| 282 | print(entry, it->second.samples, "thread"); | ||
| 283 | break; | ||
| 284 | } | ||
| 285 | } | ||
| 286 | } | ||
| 287 | for (const DWORD id : apart) { | ||
| 288 | const auto it = threads.find(id); | ||
| 289 | if (it == threads.end() || !it->second.samples) continue; | ||
| 290 | std::size_t shown = 0; | ||
| 291 | for (const auto& entry : top) | ||
| 292 | if (entry.second->first == id && shown++ < 8) | ||
| 293 | print(entry, it->second.samples, id == apart.front() && submit_phases::totals().thread.load() == id | ||
| 294 | ? "submit" | ||
| 295 | : "busy "); | ||
| 296 | } | ||
| 297 | for (const DWORD id : s.detail) { | ||
| 298 | std::vector<std::pair<std::uint64_t, const DetailChain*>> mine; | ||
| 299 | std::uint64_t all = 0; | ||
| 300 | for (const auto& [key, n] : detail_chains) | ||
| 301 | if (key.first == id) { | ||
| 302 | mine.emplace_back(n, &key.second); | ||
| 303 | all += n; | ||
| 304 | } | ||
| 305 | std::sort(mine.begin(), mine.end(), [](const auto& a, const auto& b) { return a.first > b.first; }); | ||
| 306 | for (std::size_t i = 0; i < mine.size() && i < 10; ++i) { | ||
| 307 | std::string frames; | ||
| 308 | for (int f = 0; f < kDetailFrames && (*mine[i].second)[f]; ++f) | ||
| 309 | frames += (f ? " < " : "") + name_of(s, (*mine[i].second)[f]); | ||
| 310 | std::fprintf(stderr, "[rt] sampler: detail thread %lu %5.1f%%: %s\n", id, | ||
| 311 | 100.0 * double(mine[i].first) / double(all), frames.c_str()); | ||
| 312 | } | ||
| 313 | } | ||
| 314 | detail_chains.clear(); | ||
| 315 | for (const auto& [key, chains_of] : found) { | ||
| 316 | std::uint64_t all = 0; | ||
| 317 | const DetailChain* top = nullptr; | ||
| 318 | std::uint64_t top_n = 0; | ||
| 319 | for (const auto& [c, n] : chains_of) { | ||
| 320 | all += n; | ||
| 321 | if (n > top_n) top_n = n, top = &c; | ||
| 322 | } | ||
| 323 | std::string frames; | ||
| 324 | for (int f = 0; top && f < kDetailFrames && (*top)[f]; ++f) frames += (f ? " < " : "") + name_of(s, (*top)[f]); | ||
| 325 | const auto t = threads.find(key.second); | ||
| 326 | std::fprintf(stderr, "[rt] sampler: find t+%llx: thread %lu %llu samples of %llu; top chain (%llu): %s\n", | ||
| 327 | static_cast<unsigned long long>(key.first), key.second, static_cast<unsigned long long>(all), | ||
| 328 | static_cast<unsigned long long>(t == threads.end() ? 0 : t->second.samples), | ||
| 329 | static_cast<unsigned long long>(top_n), frames.c_str()); | ||
| 330 | } | ||
| 331 | found.clear(); | ||
| 332 | if (s.shims) { | ||
| 333 | // Title threads by their samples inside our runtime: where the title's own threads (its main, render and | ||
| 334 | // Subrender workers, streaming) spend time in our shims, waiting or running. | ||
| 335 | const auto top_of = [&](const auto& counts, std::size_t limit, bool pair_key) { | ||
| 336 | std::vector<std::pair<std::uint64_t, std::string>> v; | ||
| 337 | for (const auto& [key, n] : counts) { | ||
| 338 | std::string name; | ||
| 339 | if constexpr (requires { key.first; }) { | ||
| 340 | name = name_of(s, key.first) + "<-" + name_of(s, key.second); | ||
| 341 | } else { | ||
| 342 | name = name_of(s, key); | ||
| 343 | } | ||
| 344 | v.emplace_back(n, std::move(name)); | ||
| 345 | } | ||
| 346 | (void)pair_key; | ||
| 347 | std::sort(v.begin(), v.end(), [](const auto& a, const auto& b) { return a.first > b.first; }); | ||
| 348 | std::string text; | ||
| 349 | for (std::size_t i = 0; i < v.size() && i < limit; ++i) { | ||
| 350 | char n[24]; | ||
| 351 | std::snprintf(n, sizeof(n), " %llu", static_cast<unsigned long long>(v[i].first)); | ||
| 352 | text += (i ? ", " : "") + v[i].second + n; | ||
| 353 | } | ||
| 354 | return text; | ||
| 355 | }; | ||
| 356 | std::vector<std::pair<std::uint64_t, DWORD>> order; | ||
| 357 | ShimCounts all; | ||
| 358 | for (const auto& [id, c] : shim_counts) { | ||
| 359 | if (!c.title) continue; | ||
| 360 | order.emplace_back(c.ours, id); | ||
| 361 | all.title += c.title; | ||
| 362 | all.ours += c.ours; | ||
| 363 | all.ours_busy += c.ours_busy; | ||
| 364 | all.busy += c.busy; | ||
| 365 | for (const auto& [k, n] : c.entries) all.entries[k] += n; | ||
| 366 | for (const auto& [k, n] : c.inner) all.inner[k] += n; | ||
| 367 | for (const auto& [k, n] : c.leaves) all.leaves[k] += n; | ||
| 368 | } | ||
| 369 | std::sort(order.rbegin(), order.rend()); | ||
| 370 | std::fprintf(stderr, "[rt] sampler: shims: title-thread samples inside our runtime %llu of %llu (%.1f%%), " | ||
| 371 | "%llu of them while the thread used CPU\n", | ||
| 372 | static_cast<unsigned long long>(all.ours), static_cast<unsigned long long>(all.title), | ||
| 373 | all.title ? 100.0 * double(all.ours) / double(all.title) : 0.0, | ||
| 374 | static_cast<unsigned long long>(all.ours_busy)); | ||
| 375 | for (std::size_t i = 0; i < order.size() && i < 12 && order[i].first; ++i) { | ||
| 376 | const ShimCounts& c = shim_counts[order[i].second]; | ||
| 377 | std::fprintf(stderr, "[rt] sampler: shims thread %lu: %llu of %llu samples ours (%.0f%%; %llu busy), " | ||
| 378 | "thread busy in %llu; entries: %s; innermost ours: %s; leaves: %s\n", | ||
| 379 | order[i].second, static_cast<unsigned long long>(c.ours), | ||
| 380 | static_cast<unsigned long long>(c.title), 100.0 * double(c.ours) / double(c.title), | ||
| 381 | static_cast<unsigned long long>(c.ours_busy), static_cast<unsigned long long>(c.busy), | ||
| 382 | top_of(c.entries, 5, true).c_str(), top_of(c.inner, 5, false).c_str(), | ||
| 383 | top_of(c.leaves, 3, false).c_str()); | ||
| 384 | } | ||
| 385 | std::fprintf(stderr, "[rt] sampler: shims all: entries: %s\n", top_of(all.entries, 16, true).c_str()); | ||
| 386 | std::fprintf(stderr, "[rt] sampler: shims all: innermost ours: %s\n", top_of(all.inner, 16, false).c_str()); | ||
| 387 | std::fprintf(stderr, "[rt] sampler: shims all: leaves: %s\n", top_of(all.leaves, 12, false).c_str()); | ||
| 388 | shim_counts.clear(); | ||
| 389 | } | ||
| 390 | chains.clear(); | ||
| 391 | total = 0; | ||
| 392 | for (auto& [id, t] : threads) t.used = t.samples = 0; | ||
| 393 | } | ||
| 394 | } | ||
| 395 | |||
| 396 | // From main, once the title is mapped. Does nothing without LEONIDA_SAMPLE_MS. | ||
| 397 | inline void start(std::uint8_t* title, std::uint64_t title_size) { | ||
| 398 | char text[16] = {}; | ||
| 399 | const DWORD n = GetEnvironmentVariableA("LEONIDA_SAMPLE_MS", text, sizeof(text)); | ||
| 400 | if (!n || n >= sizeof(text)) return; | ||
| 401 | static Sampler s; | ||
| 402 | s.title = title; | ||
| 403 | s.title_size = title_size; | ||
| 404 | s.period_ms = (std::max)(1ul, std::strtoul(text, nullptr, 10)); | ||
| 405 | char report[16] = {}; | ||
| 406 | if (GetEnvironmentVariableA("LEONIDA_SAMPLE_REPORT_S", report, sizeof(report)) > 0) | ||
| 407 | s.report_ms = (std::max)(1ul, std::strtoul(report, nullptr, 10)) * 1000; | ||
| 408 | char waits[4] = {}; | ||
| 409 | s.waits = GetEnvironmentVariableA("LEONIDA_SAMPLE_WAITS", waits, sizeof(waits)) == 1 && waits[0] == '1'; | ||
| 410 | char detail[128] = {}; | ||
| 411 | if (const DWORD n = GetEnvironmentVariableA("LEONIDA_SAMPLE_DETAIL", detail, sizeof(detail)); n && n < sizeof(detail)) | ||
| 412 | for (char* at = detail; *at;) { | ||
| 413 | char* end = nullptr; | ||
| 414 | const unsigned long id = std::strtoul(at, &end, 10); | ||
| 415 | if (end == at) break; | ||
| 416 | if (id) s.detail.push_back(DWORD(id)); | ||
| 417 | at = *end == ',' ? end + 1 : end; | ||
| 418 | } | ||
| 419 | char finds[256] = {}; | ||
| 420 | if (const DWORD n = GetEnvironmentVariableA("LEONIDA_SAMPLE_FIND", finds, sizeof(finds)); n && n < sizeof(finds)) | ||
| 421 | for (char* at = finds; *at;) { | ||
| 422 | char* end = nullptr; | ||
| 423 | const std::uint64_t rva = std::strtoull(at, &end, 16); | ||
| 424 | if (end == at) break; | ||
| 425 | if (rva) s.find.push_back(rva); | ||
| 426 | at = *end == ',' ? end + 1 : end; | ||
| 427 | } | ||
| 428 | char shims[4] = {}; | ||
| 429 | s.shims = GetEnvironmentVariableA("LEONIDA_SAMPLE_SHIMS", shims, sizeof(shims)) == 1 && shims[0] == '1'; | ||
| 430 | if (const auto self = reinterpret_cast<const std::uint8_t*>(GetModuleHandleW(nullptr))) { | ||
| 431 | const auto* dos = reinterpret_cast<const IMAGE_DOS_HEADER*>(self); | ||
| 432 | const auto* nt = reinterpret_cast<const IMAGE_NT_HEADERS64*>(self + dos->e_lfanew); | ||
| 433 | s.self_base = reinterpret_cast<std::uint64_t>(self); | ||
| 434 | s.self_size = nt->OptionalHeader.SizeOfImage; | ||
| 435 | } | ||
| 436 | if (const HANDLE h = CreateThread(nullptr, 0, &run, &s, 0, nullptr)) { | ||
| 437 | CloseHandle(h); | ||
| 438 | std::fprintf(stderr, "[rt] sampler: every %lu ms, report every %lu s%s%s\n", s.period_ms, s.report_ms / 1000, | ||
| 439 | s.waits ? ", waiting threads too" : "", s.shims ? ", title threads inside our runtime" : ""); | ||
| 440 | } | ||
| 441 | } | ||
| 442 | |||
| 443 | } // namespace leonida::thread_sampler | ||