upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/src/thread_sampler.h
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/src/thread_sampler.h')
-rw-r--r--runtime/src/thread_sampler.h43
1 files changed, 41 insertions, 2 deletions
diff --git a/runtime/src/thread_sampler.h b/runtime/src/thread_sampler.h
index 282eb20dd..0c18949ce 100644
--- a/runtime/src/thread_sampler.h
+++ b/runtime/src/thread_sampler.h
@@ -44,6 +44,9 @@ struct Sampler {
44 // LEONIDA_SAMPLE_DETAIL=<id>[,<id>...]: for these threads, the 10 most frequent chains, 16 frames deep, each as a 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). 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; 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;
47}; 50};
48 51
49// Per title thread (one with a title frame in its chain): how many samples were inside our runtime, and through which 52// Per title thread (one with a title frame in its chain): how many samples were inside our runtime, and through which
@@ -151,6 +154,7 @@ inline DWORD WINAPI run(LPVOID parameter) {
151 constexpr int kDetailFrames = 16; 154 constexpr int kDetailFrames = 16;
152 using DetailChain = std::array<std::uint64_t, kDetailFrames>; 155 using DetailChain = std::array<std::uint64_t, kDetailFrames>;
153 std::map<std::pair<DWORD, DetailChain>, std::uint64_t> detail_chains; 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
154 std::uint64_t total = 0; 158 std::uint64_t total = 0;
155 ULONGLONG listed = 0, reported = GetTickCount64(); 159 ULONGLONG listed = 0, reported = GetTickCount64();
156 const auto cpu_of = [](HANDLE h) { 160 const auto cpu_of = [](HANDLE h) {
@@ -205,11 +209,20 @@ inline DWORD WINAPI run(LPVOID parameter) {
205 unwind(context, deep); 209 unwind(context, deep);
206 ++detail_chains[{it->first, deep}]; 210 ++detail_chains[{it->first, deep}];
207 } 211 }
208 if (s.shims) { 212 if (s.shims || !s.find.empty()) {
209 DeepChain deep{}; 213 DeepChain deep{};
210 const int n = unwind(context, deep); 214 const int n = unwind(context, deep);
211 std::copy_n(deep.begin(), kFrames, chain.begin()); 215 std::copy_n(deep.begin(), kFrames, chain.begin());
212 count_shims(s, deep, n, moved, shim_counts[it->first]); 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 }
213 } else { 226 } else {
214 unwind(context, chain); 227 unwind(context, chain);
215 } 228 }
@@ -299,6 +312,23 @@ inline DWORD WINAPI run(LPVOID parameter) {
299 } 312 }
300 } 313 }
301 detail_chains.clear(); 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();
302 if (s.shims) { 332 if (s.shims) {
303 // Title threads by their samples inside our runtime: where the title's own threads (its main, render and 333 // Title threads by their samples inside our runtime: where the title's own threads (its main, render and
304 // Subrender workers, streaming) spend time in our shims, waiting or running. 334 // Subrender workers, streaming) spend time in our shims, waiting or running.
@@ -386,6 +416,15 @@ inline void start(std::uint8_t* title, std::uint64_t title_size) {
386 if (id) s.detail.push_back(DWORD(id)); 416 if (id) s.detail.push_back(DWORD(id));
387 at = *end == ',' ? end + 1 : end; 417 at = *end == ',' ? end + 1 : end;
388 } 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 }
389 char shims[4] = {}; 428 char shims[4] = {};
390 s.shims = GetEnvironmentVariableA("LEONIDA_SAMPLE_SHIMS", shims, sizeof(shims)) == 1 && shims[0] == '1'; 429 s.shims = GetEnvironmentVariableA("LEONIDA_SAMPLE_SHIMS", shims, sizeof(shims)) == 1 && shims[0] == '1';
391 if (const auto self = reinterpret_cast<const std::uint8_t*>(GetModuleHandleW(nullptr))) { 430 if (const auto self = reinterpret_cast<const std::uint8_t*>(GetModuleHandleW(nullptr))) {