From 8a41e3a7ef75b10d73cb005eba7ee21e1c41a953 Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Sun, 25 Oct 2026 14:12:43 +0300 Subject: [PATCH] Keep call chains of every thread in the sampling profile with TOTK_PROFILE_CHAINS=1 Render-thread chains could not show which guest thread held a virtual core during a stall; with every thread's chains over a short frame range the running samples name it. Co-Authored-By: Claude Opus 5.5 --- runtime/cfg.c | 2 +- runtime/prof_win.c | 15 ++++++++++----- tools/prof_report.py | 4 ++-- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/runtime/cfg.c b/runtime/cfg.c index 324fa4f4..118c0d43 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -32,7 +32,7 @@ static const char *const known[] = { "GPU", "GPU_INDEX", "GPU_NAME", "HIGHFPS_FIX", "INPUT", "INPUT_RECORD", "INPUT_SCRIPT", "LANG", "LIB_PREBUILD", "LIB_PREBUILD_KEEP", "LIB_PREBUILD_THREADS", "LOCALE", "LOD_GUARD", "MEMORY", "MINIMAL_LAYOUT", "MIRROR_GAP", "MOTION", "MOUSE_SENS", "MOVIE_HOST", "MOVIE_PACE", "OPERATION_MODE", "PERF_SLOW_MS", "PIPELINE_CACHE", "PIPE_PREBUILD", "PIPE_SYNC", "PREWARM_XLATE", - "PROBE_MOVIE", "PROBE_MOVIE_SHOT", "PROFILE", "PROFILE_DIR", "PROFILE_FRAMES", "PROFILE_RENDER", "PUSH_SKIP", "PUSH_STAGE", "RDOC", + "PROBE_MOVIE", "PROBE_MOVIE_SHOT", "PROFILE", "PROFILE_CHAINS", "PROFILE_DIR", "PROFILE_FRAMES", "PROFILE_RENDER", "PUSH_SKIP", "PUSH_STAGE", "RDOC", "RDOC_DIR", "RDOC_DLL", "REALTIME", "REEXEC", "RESOLVE_CHECK", "ROMFS", "ROMFS_INDEX", "ROMFS_MMAP", "ROMFS_MODS", "ROMFS_PATCHES", "ROMFS_PROOF_DIR", "ROMFS_VERIFY", "RUMBLE", "RUMBLE_CURVE", "RUMBLE_STRENGTH", "RUMBLE_TRIGGERS", "SHADER_CACHE", "SHADER_MEMO", "SHARED_FONTS", "SHOTS", "SHOTS_DIR", "SHOTS_FULL", diff --git a/runtime/prof_win.c b/runtime/prof_win.c index f87c30d1..d81954ba 100644 --- a/runtime/prof_win.c +++ b/runtime/prof_win.c @@ -2,9 +2,11 @@ // A host thread wakes every 1 ms and, for every other thread, suspends it, reads Rip and resumes it. The sampler never // allocates or takes a lock between SuspendThread and ResumeThread and never runs guest code. The profile is text: // totk-prof 1 / base / period_us / samples N / thread / module / -// rip (the top PROF_TOP Rips) / stack ... (TOTK_PROFILE_RENDER only, the top -// PROF_STACKS call chains) -// With TOTK_PROFILE_RENDER the sampler also copies up to STK_COPY bytes of the suspended thread's stack and, after +// rip (the top PROF_TOP Rips) / stack ... (TOTK_PROFILE_RENDER or +// TOTK_PROFILE_CHAINS only, the top PROF_STACKS call chains) +// With TOTK_PROFILE_RENDER (the render thread) or TOTK_PROFILE_CHAINS=1 (every thread, chains of all threads summed: for +// short frame ranges, as each sample copies a stack) the sampler also copies up to STK_COPY bytes of the suspended +// thread's stack and, after // ResumeThread, unwinds the copy with the .pdata tables (RtlVirtualUnwind), so it takes no lock while the thread is // suspended. The unwind stops at the first frame outside the copy; deeper callers are cut. #include @@ -45,6 +47,7 @@ static volatile LONG stop, written; static uintptr_t exe_lo, exe_hi; static volatile DWORD render_tid; // the thread that presents (perf_present) static int only_render; // TOTK_PROFILE_RENDER=1: sample the render thread only +static int chains; // call chains are kept: TOTK_PROFILE_RENDER or TOTK_PROFILE_CHAINS static volatile uint64_t cur_frame; // last presented frame static uint64_t frame_lo, frame_hi; // TOTK_PROFILE_FRAMES=-: sample only while presents lo..hi-1 are pending @@ -151,12 +154,12 @@ static DWORD WINAPI sampler_main(void *arg) { for (int i = 0; i < n_threads; i++) { ThreadSlot *t = &threads[i]; if (only_render && t->tid != render_tid) continue; - ctx.ContextFlags = only_render ? CONTEXT_CONTROL | CONTEXT_INTEGER : CONTEXT_CONTROL; + ctx.ContextFlags = chains ? CONTEXT_CONTROL | CONTEXT_INTEGER : CONTEXT_CONTROL; if (SuspendThread(t->h) == (DWORD)-1) continue; BOOL ok = GetThreadContext(t->h, &ctx); size_t n = 0; MEMORY_BASIC_INFORMATION mb; - if (ok && only_render && VirtualQuery((void *)ctx.Rsp, &mb, sizeof mb) && mb.State == MEM_COMMIT) { + if (ok && chains && VirtualQuery((void *)ctx.Rsp, &mb, sizeof mb) && mb.State == MEM_COMMIT) { n = (uintptr_t)mb.BaseAddress + mb.RegionSize - ctx.Rsp; // the committed stack above Rsp if (n > STK_COPY) n = STK_COPY; memcpy(stk_buf, (void *)ctx.Rsp, n); @@ -242,6 +245,8 @@ void prof_start(void) { return; } only_render = cfg_get("TOTK_PROFILE_RENDER") != NULL; + const char *ch = cfg_get("TOTK_PROFILE_CHAINS"); + chains = only_render || (ch && *ch && strcmp(ch, "0")); const char *fr = cfg_get("TOTK_PROFILE_FRAMES"); if (fr && sscanf(fr, "%llu-%llu", (unsigned long long *)&frame_lo, (unsigned long long *)&frame_hi) != 2) frame_hi = 0; MODULEINFO mi; diff --git a/tools/prof_report.py b/tools/prof_report.py index 684d730f..9c5ad45d 100644 --- a/tools/prof_report.py +++ b/tools/prof_report.py @@ -6,7 +6,7 @@ The map is the lld one from the link (build//totk.map); its addresses a runtime image base, so ASLR does not matter. Symbols in the map are labels without sizes: a Rip belongs to the nearest symbol at or below it. -A profile taken with TOTK_PROFILE_RENDER=1 also has call chains: the report then adds the inclusive time per +A profile taken with TOTK_PROFILE_RENDER=1 or TOTK_PROFILE_CHAINS=1 also has call chains: the report then adds the inclusive time per function (a function counted once per chain), and for each module outside the exe the exe functions that call into it and the longest common chains down to it. """ @@ -56,7 +56,7 @@ def load_profile(path): def report_chains(prof, name_of, in_exe, top): stacks = prof['stacks'] total = sum(n for n, _ in stacks) or 1 - print(f'\ncall chains: {total} samples in {len(stacks)} chains (render thread)') + print(f'\ncall chains: {total} samples in {len(stacks)} chains (render thread, or every thread with TOTK_PROFILE_CHAINS)') incl = collections.Counter() for n, chain in stacks: for f in set(name_of(r) for r in chain):