From 2790f7d0e2a6a0dbc40d3c764f4bc3dd232d26b4 Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Sun, 25 Oct 2026 12:40:49 +0300 Subject: [PATCH 1/2] Keep render-thread call chains in the sampling profile and report inclusive time and driver callers from them Co-Authored-By: Claude Opus 5.5 --- docs/agent-playbook.md | 5 ++- runtime/prof_win.c | 88 +++++++++++++++++++++++++++++++++++++++++- tools/prof_report.py | 57 ++++++++++++++++++++++++++- 3 files changed, 144 insertions(+), 6 deletions(-) diff --git a/docs/agent-playbook.md b/docs/agent-playbook.md index 38634c62..b18f0ffe 100644 --- a/docs/agent-playbook.md +++ b/docs/agent-playbook.md @@ -35,8 +35,9 @@ other `totk.exe` or `ninja.exe` runs. The log's first and last lines record the - With `perf`, `mem: dirty` every 60 frames: what a VRAM mirror of the buffer-bound pools would upload (pages the CPU wrote per frame and per submit, eager and lazy, by pool class), FlushMappedRange calls and bytes, and the mirror size. The census costs about 1.1 ms of CPU per frame on the submitting thread; `perf_summary.py` summarises it. -- `TOTK_PROFILE=1 TOTK_PROFILE_FRAMES=lo-hi`: sampling profiler (`runtime/prof_win.c`). Resolve `exe+0x...` RVAs - with the build's `totk.map`. +- `TOTK_PROFILE= TOTK_PROFILE_FRAMES=lo-hi`: sampling profiler (`runtime/prof_win.c`), read with + `tools/prof_report.py `. With `TOTK_PROFILE_RENDER=1` it samples the render thread only and keeps + call chains: the report adds inclusive time per function and the exe callers of the driver and system DLLs. - `tools/regress.py --base build/v121-m4 [--routes fight,rock,island,field] [--repeat 3] [--cold] [--validation] [--out dir]`: runs the routes through `run_game.py --quiet`, scores logs and `TOTK_SHOTS` frames (`tools/regress_score.py`, tested by `tests/regress_score_test.py`) and prints median and worst per metric. Run the candidate alone and judge it against diff --git a/runtime/prof_win.c b/runtime/prof_win.c index fea5af3a..f87c30d1 100644 --- a/runtime/prof_win.c +++ b/runtime/prof_win.c @@ -2,7 +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) +// 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 +// 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 #include #include @@ -18,11 +22,20 @@ #define PROF_SLOTS (1u << 16) // Rip hash table, open addressing; a full table drops new Rips #define PROF_TOP 4000 // more than the 200 asked for: the report sums per function #define PROF_THREADS 512 +#define PROF_DEPTH 24 // frames kept per call chain, the sampled Rip first +#define STK_SLOTS (1u << 15) // call-chain hash table, open addressing; a full table drops new chains +#define PROF_STACKS 6000 +#define STK_COPY (64u << 10) // stack bytes copied per sample +#define STK_SLACK (64u << 10) // zeros after the copy: a frame reaching past it reads zeros, and a zero Rip ends the chain typedef struct { uint64_t rip, n; } RipCount; typedef struct { DWORD tid; HANDLE h; uint64_t n, in_exe; } ThreadSlot; static RipCount table[PROF_SLOTS]; +typedef struct { uint64_t hash, n; uint32_t depth; uint64_t rip[PROF_DEPTH]; } StackCount; +static StackCount stacks[STK_SLOTS]; +static uint64_t stacks_dropped; +static uint8_t stk_buf[STK_COPY + STK_SLACK] __attribute__((aligned(16))); // the slack is never written static ThreadSlot threads[PROF_THREADS]; static int n_threads; static uint64_t total, dropped; @@ -47,6 +60,50 @@ static void count_rip(uint64_t rip) { dropped++; } +static void count_stack(const uint64_t *rip, uint32_t depth) { + uint64_t h = 0xcbf29ce484222325ull; + for (uint32_t d = 0; d < depth; d++) h = (h ^ rip[d]) * 0x100000001b3ull; + h |= 1; // 0 marks a free slot + uint32_t i = (uint32_t)(h >> 49); + for (uint32_t k = 0; k < 64; k++, i = (i + 1) & (STK_SLOTS - 1)) { + StackCount *s = &stacks[i]; + if (!s->hash) { + s->hash = h, s->depth = depth; + memcpy(s->rip, rip, depth * sizeof *rip); + } else if (s->hash != h || s->depth != depth || memcmp(s->rip, rip, depth * sizeof *rip)) { + continue; + } + s->n++; + return; + } + stacks_dropped++; +} + +// Unwinds the stack copied to stk_buf from real address lo (n bytes). Registers that point into the copied range are +// moved into the copy, so frame-pointer frames read the copy too. Returns the number of frames. +static uint32_t unwind_copy(CONTEXT *c, uintptr_t lo, size_t n, uint64_t *rip) { + uintptr_t buf = (uintptr_t)stk_buf, end = buf + n; + DWORD64 *regs[] = {&c->Rbx, &c->Rbp, &c->Rsi, &c->Rdi, &c->R12, &c->R13, &c->R14, &c->R15}; + for (unsigned i = 0; i < sizeof regs / sizeof *regs; i++) + if (*regs[i] >= lo && *regs[i] < lo + n) *regs[i] = *regs[i] - lo + buf; + c->Rsp = buf; + uint32_t d = 0; + while (d < PROF_DEPTH && c->Rip && c->Rsp >= buf && c->Rsp + 8 <= end) { + rip[d++] = c->Rip; + DWORD64 base; + PRUNTIME_FUNCTION f = RtlLookupFunctionEntry(c->Rip, &base, NULL); + if (f) { + void *hd; + DWORD64 est; + RtlVirtualUnwind(UNW_FLAG_NHANDLER, base, c->Rip, f, c, &hd, &est, NULL); + } else { // a leaf function: the return address is on top + c->Rip = *(DWORD64 *)c->Rsp; + c->Rsp += 8; + } + } + return d; +} + // Re-lists the process's threads: keeps the handle of a thread seen before, opens new ones, closes the gone ones. static void refresh_threads(DWORD self) { HANDLE snap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0); @@ -94,12 +151,24 @@ 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 = CONTEXT_CONTROL; + ctx.ContextFlags = only_render ? 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) { + 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); + } ResumeThread(t->h); if (!ok) continue; count_rip(ctx.Rip); + if (n) { + uint64_t chain[PROF_DEPTH]; + uint32_t d = unwind_copy(&ctx, ctx.Rsp, n, chain); + if (d) count_stack(chain, d); + } total++; t->n++; if (ctx.Rip >= exe_lo && ctx.Rip < exe_hi) t->in_exe++; @@ -113,6 +182,11 @@ static int cmp_desc(const void *a, const void *b) { return (x < y) - (x > y); } +static int cmp_stack_desc(const void *a, const void *b) { + uint64_t x = ((const StackCount *)a)->n, y = ((const StackCount *)b)->n; + return (x < y) - (x > y); +} + void prof_write(void) { perf_summary_flush(); if (!out_path || InterlockedExchange(&written, 1)) return; @@ -142,6 +216,16 @@ void prof_write(void) { if (table[i].rip) table[cnt++] = table[i]; qsort(table, cnt, sizeof *table, cmp_desc); for (int i = 0; i < cnt && i < PROF_TOP; i++) fprintf(f, "rip %llx %llu\n", (unsigned long long)table[i].rip, (unsigned long long)table[i].n); + int ns = 0; + for (uint32_t i = 0; i < STK_SLOTS; i++) + if (stacks[i].hash) stacks[ns++] = stacks[i]; + qsort(stacks, ns, sizeof *stacks, cmp_stack_desc); + for (int i = 0; i < ns && i < PROF_STACKS; i++) { + fprintf(f, "stack %llu", (unsigned long long)stacks[i].n); + for (uint32_t d = 0; d < stacks[i].depth; d++) fprintf(f, " %llx", (unsigned long long)stacks[i].rip[d]); + fputc('\n', f); + } + if (stacks_dropped) fprintf(f, "stacks_dropped %llu\n", (unsigned long long)stacks_dropped); fclose(f); rt_log("prof: %llu samples, %d Rips -> %s", (unsigned long long)total, cnt, out_path); } diff --git a/tools/prof_report.py b/tools/prof_report.py index aa416445..684d730f 100644 --- a/tools/prof_report.py +++ b/tools/prof_report.py @@ -1,10 +1,14 @@ """Report for a TOTK_PROFILE sampling profile (runtime/prof_win.c): top functions and the guest/runtime/outside split. -usage: prof_report.py [--top 40] +usage: prof_report.py [--top 40] [--chains 30] The map is the lld one from the link (build//totk.map); its addresses are RVAs, the profile header carries the 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 +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. """ import argparse import bisect @@ -28,7 +32,7 @@ def load_map(path): def load_profile(path): - prof = {'rips': [], 'modules': [], 'threads': [], 'samples': 0, 'base': 0} + prof = {'rips': [], 'modules': [], 'threads': [], 'stacks': [], 'samples': 0, 'base': 0} for line in Path(path).read_text().splitlines(): p = line.split(None, 3) if not p: @@ -43,20 +47,67 @@ def load_profile(path): prof['modules'].append((int(p[1], 16), int(p[2], 16), p[3])) elif p[0] == 'rip': prof['rips'].append((int(p[1], 16), int(p[2]))) + elif p[0] == 'stack': + f = line.split() + prof['stacks'].append((int(f[1]), [int(x, 16) for x in f[2:]])) return prof +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)') + incl = collections.Counter() + for n, chain in stacks: + for f in set(name_of(r) for r in chain): + incl[f] += n + print(f'\ninclusive, top {top} (% of chain samples)') + for f, n in incl.most_common(top): + print(f' {100 * n / total:6.2f}% {n:8d} {f}') + # Per outside module: the innermost exe frame of each chain whose leaf is in that module + by_mod = collections.defaultdict(collections.Counter) + paths = collections.defaultdict(collections.Counter) + for n, chain in stacks: + if in_exe(chain[0]): + continue + mod = name_of(chain[0]).split('!')[0] + i = next((k for k, r in enumerate(chain) if in_exe(r)), None) + if i is None: + by_mod[mod]['(no exe frame in the chain)'] += n + continue + by_mod[mod][name_of(chain[i])] += n + paths[mod][' < '.join(name_of(r) for r in chain[i:i + 4])] += n + for mod, c in sorted(by_mod.items(), key=lambda kv: -sum(kv[1].values())): + m = sum(c.values()) + print(f'\n{mod}: {100 * m / total:.2f}% of chain samples; exe callers') + for f, n in c.most_common(12): + print(f' {100 * n / total:6.2f}% {n:8d} {f}') + for pth, n in paths[mod].most_common(6): + print(f' {100 * n / total:6.2f}% {pth}') + + def main(): ap = argparse.ArgumentParser() ap.add_argument('profile') ap.add_argument('map') ap.add_argument('--top', type=int, default=40) + ap.add_argument('--chains', type=int, default=30, help='inclusive functions listed from the call chains') a = ap.parse_args() prof = load_profile(a.profile) addrs, names = load_map(a.map) base = prof['base'] exe = next((m for m in prof['modules'] if m[0] == base), None) exe_end = base + exe[1] if exe else base + + def in_exe(rip): + return base <= rip < exe_end + + def name_of(rip): + if in_exe(rip): + i = bisect.bisect_right(addrs, rip - base) - 1 + return names[i] if i >= 0 else '?' + m = next((m for m in prof['modules'] if m[0] <= rip < m[0] + m[1]), None) + return f'{Path(m[2]).name}!+{rip - m[0]:#x}' if m else 'unknown' total = sum(n for _, n in prof['rips']) or 1 funcs = collections.Counter() cats = collections.Counter() @@ -83,6 +134,8 @@ def main(): print('\nthreads (samples, % of them inside the exe)') for tid, n, ie in sorted(prof['threads'], key=lambda t: -t[1])[:12]: print(f' tid {tid:6d} {n:8d} {100 * ie / max(n, 1):5.1f}% in exe') + if prof['stacks']: + report_chains(prof, name_of, in_exe, a.chains) if __name__ == '__main__': From bf41b9cfefb743e5b298ab43d8970c85353b1a97 Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Sun, 25 Oct 2026 13:02:21 +0300 Subject: [PATCH 2/2] Make the program-to-shader cache 4-way set-associative so conflict misses stop hashing whole shaders with SHA-256 every frame Co-Authored-By: Claude Opus 5.5 --- runtime/vk_exec_draw.c | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/runtime/vk_exec_draw.c b/runtime/vk_exec_draw.c index aca46658..5c4d3b11 100644 --- a/runtime/vk_exec_draw.c +++ b/runtime/vk_exec_draw.c @@ -475,8 +475,13 @@ static VkImageView get_view(VkExec *ex, const VkTexImage *img, const view_t *v, // ---- program to shaders +// (program, stage, answers) -> shader, 4-way set-associative, most recently used first. A miss costs a SHA-256 of the +// whole shader code (gfx_shader.c req_init), slow on CPUs without SHA instructions: a field frame looks up ~3000 and +// the 2048-slot direct-mapped table this replaced missed 3% of them on conflicts. +#define SHADER_SETS 2048 +#define SHADER_WAYS 4 typedef struct { uint64_t serial; int stage; int used; GfxShaderAnswers a; const GfxShader *s; } ShaderEnt; -static ShaderEnt shader_tab[2048]; +static ShaderEnt shader_tab[SHADER_SETS][SHADER_WAYS]; static int find_stage(const prog_t *p, int nvn_stage, const uint8_t **ctl, const uint8_t **code, uint32_t *len) { for (uint32_t i = 0; i < p->nstage && i < 6; i++) { @@ -542,11 +547,20 @@ static const GfxShader *shader_for(VkExec *ex, int st, int *present, int *pendin GfxShaderAnswers a; answers_for(ex, st, ctl, &a); uint64_t serial = ex->prog->serial; - ShaderEnt *e = NULL; + ShaderEnt *set = NULL; if (serial) { uint64_t h = (serial * 0x9E3779B97F4A7C15ull ^ (uint64_t)st) ^ nvn_hm_hash(&a, sizeof a); // a is zero-padded - e = &shader_tab[(h ^ h >> 29) & 2047]; - if (e->used && e->serial == serial && e->stage == st && !memcmp(&e->a, &a, sizeof a)) return e->s; + set = shader_tab[(h ^ h >> 29) & (SHADER_SETS - 1)]; + for (int w = 0; w < SHADER_WAYS && set[w].used; w++) { + ShaderEnt *e = &set[w]; + if (e->serial != serial || e->stage != st || memcmp(&e->a, &a, sizeof a)) continue; + if (w) { // to the front + ShaderEnt hit = *e; + memmove(&set[1], &set[0], w * sizeof *set); + set[0] = hit; + } + return set[0].s; + } } int gs = st == NVN_STAGE_VS ? GFX_STAGE_VS : st == NVN_STAGE_FS ? GFX_STAGE_FS : GFX_STAGE_CS; const GfxShader *s = await ? gfx_shader_await(gs, ctl, code, len, &a) @@ -555,7 +569,10 @@ static const GfxShader *shader_for(VkExec *ex, int st, int *present, int *pendin if (pending && *pending) return NULL; if (!s) warn_once(W_SHADER, "a stage %d shader of program %llu failed to translate: draws with it are skipped", st, (unsigned long long)serial); - else if (e) e->used = 1, e->serial = serial, e->stage = st, e->a = a, e->s = s; + else if (set) { // in front, the least recently used way drops out + memmove(&set[1], &set[0], (SHADER_WAYS - 1) * sizeof *set); + set[0] = (ShaderEnt){.serial = serial, .stage = st, .used = 1, .a = a, .s = s}; + } return s; }