From 48dfe0cfdf113b342cdbba725cadafe0fee17a03 Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Wed, 28 Oct 2026 16:41:46 +0300 Subject: [PATCH] Measure a change within one launch: TOTK_AB= alternates A and B blocks (A B B A), a switch set to ab is on in the B blocks, and tools/ab_summary.py compares the perf windows of each side; TOTK_ONE_PER_CORE=1 keeps one logical processor per physical core (off: no gain on a 7900X) Co-Authored-By: Claude Opus 5.5 --- runtime/cfg.c | 4 +-- runtime/host.h | 8 ++++++ runtime/prof_win.c | 63 ++++++++++++++++++++++++++++++++++++++++++--- tools/ab_summary.py | 43 +++++++++++++++++++++++++++++++ 4 files changed, 112 insertions(+), 6 deletions(-) create mode 100644 tools/ab_summary.py diff --git a/runtime/cfg.c b/runtime/cfg.c index 79ea9378..bee6a0ff 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -30,7 +30,7 @@ static int save_ok = 1; // the last cfg_save_to wrote (o // Every name the runtime reads (and the patch sets in generated/), without the prefix. A file key outside this list is // logged as unknown. A new variable goes here too, or its ini key logs a harmless "unknown key" line. static const char *const known[] = { - "APP_POOL", "ASSUME_FOCUS", "ASTC", "ASTC_TIME", "ASYNC_PREEMPT", "AUDIO", "AUDIO_OFF", "AUDIO_PREFILL_MS", + "AB", "APP_POOL", "ASSUME_FOCUS", "ASTC", "ASTC_TIME", "ASYNC_PREEMPT", "AUDIO", "AUDIO_OFF", "AUDIO_PREFILL_MS", "BACKGROUND_WINDOW", "BIND_A", "BIND_B", "BIND_DOWN", "BIND_L", "BIND_LCLICK", "BIND_LEFT", "BIND_MINUS", "BIND_PLUS", "BIND_R", "BIND_RCLICK", "BIND_RIGHT", "BIND_SKIP", "BIND_STATS", "BIND_UP", "BIND_X", "BIND_Y", "BIND_ZL", "BIND_ZR", "CLOTH_REALTIME", "CORE_DIAG", "CRIT", "CRIT_OUT", "DESC_TEMPLATE", "DUMP", "DUMP_BUF", "DUMP_DIR", "DUMP_LOG", @@ -39,7 +39,7 @@ static const char *const known[] = { "GPU_INDEX", "GPU_NAME", "HIGHFPS_FIX", "INPUT", "INPUT_RECORD", "INPUT_SCRIPT", "LANG", "LIB_PREBUILD", "LIB_PREBUILD_KEEP", "LIB_PREBUILD_THREADS", "LOCALE", "LOD_GUARD", "MEMORY", "MENU_LANG", "MENU_TEST", "MENU_TEST_DIR", "MENU_TEST_SET", "MINIMAL_LAYOUT", "MIRROR_GAP", "MOTION", "MOUSE_SENS", "MOVIE_HOST", "MOVIE_PACE", - "OPERATION_MODE", "OUTPUT_RES", "PATCH_OUTPUT", "PATCH_SCENE", "PERF_OVERLAY", "PERF_SLOW_MS", "PIPELINE_CACHE", "PIPE_MEMO", "PIPE_PREBUILD", "PIPE_SKIP", "PIPE_SYNC", "PIPE_WAIT", + "ONE_PER_CORE", "OPERATION_MODE", "OUTPUT_RES", "PATCH_OUTPUT", "PATCH_SCENE", "PERF_OVERLAY", "PERF_SLOW_MS", "PIPELINE_CACHE", "PIPE_MEMO", "PIPE_PREBUILD", "PIPE_SKIP", "PIPE_SYNC", "PIPE_WAIT", "PREWARM_XLATE", "PROBE_MOVIE", "PROBE_MOVIE_SHOT", "PROFILE", "PROFILE_CHAINS", "PROFILE_DIR", "PROFILE_FRAMES", "PROFILE_MAIN", "PROFILE_RENDER", "PUSH_SKIP", "PUSH_STAGE", "RDOC", "RDOC_DIR", "RDOC_DLL", "REALTIME", "REEXEC", "RENDER_SCALE", "REPLAY_ASYNC", diff --git a/runtime/host.h b/runtime/host.h index 72de7c81..26606b04 100644 --- a/runtime/host.h +++ b/runtime/host.h @@ -250,6 +250,14 @@ double frame_step_limit_hz(void); // the frame rate the int64_t frame_step_world_us(void); // game time since the loaded save's first frame, -1 before // (TOTK_FPS_LIMIT, live) uint64_t frame_step_ticks(void); // game time since the first present in 60 Hz ticks (input scripts) +// In-launch A/B measurement: TOTK_AB= splits the run into blocks of that many presents in the order A B B A +// (repeated); rt_ab() is 1 in the B blocks, 0 in A blocks and without TOTK_AB. rt_knob(name, def) reads the switch +// TOTK_ once per call (def when unset): 0 off, 2 for "ab" (on in the B blocks only), 1 for anything else; +// callers keep it and test it with RT_KNOB_ON. The perf line counts the B frames of its window (tools/ab_summary.py +// compares the windows that are wholly A or wholly B). +int rt_ab(void); +int rt_knob(const char *name, const char *def); +#define RT_KNOB_ON(mode) ((mode) == 2 ? rt_ab() : (mode)) void frame_step_present(uint64_t display_ns, int interval); // nvnQueuePresentTexture: the frame's display time and // present interval (60 Hz ticks) // ---- boot (boot.c, main.c) diff --git a/runtime/prof_win.c b/runtime/prof_win.c index 36e9c575..9a749fbb 100644 --- a/runtime/prof_win.c +++ b/runtime/prof_win.c @@ -527,8 +527,61 @@ static void perf_gpu_line(uint64_t frame, double interval_ms, double pace_share, (unsigned long long)frame, gs, rw, ow, vs, ws, priv, bound); } +int rt_ab(void) { + static uint64_t period = UINT64_MAX; // 0: off + if (period == UINT64_MAX) { + const char *e = cfg_get("TOTK_AB"); + period = e && *e ? strtoull(e, NULL, 10) : 0; + } + if (!period) return 0; + uint64_t k = cur_frame / period % 4; + return k == 1 || k == 2; +} + +int rt_knob(const char *name, const char *def) { + char key[64]; + snprintf(key, sizeof key, "TOTK_%s", name); + const char *e = cfg_get(key); + if (!e || !*e) e = def; + return !strcmp(e, "ab") ? 2 : strcmp(e, "0") != 0; +} + +// TOTK_ONE_PER_CORE=1: the process keeps the first logical processor of each physical core, so busy threads (the +// render thread, the guest cores, the job workers) never share a core's execution units with another; "ab" switches +// it with the A/B blocks. Off by default until measured (Savage Wind Recomp #200: +4-10% on an 8-core SMT CPU). +static void cpu_placement(void) { + static int mode = -1, applied = -1; + static DWORD_PTR all, one; + if (mode < 0) { + mode = rt_knob("ONE_PER_CORE", "0"); + DWORD_PTR sys; + GetProcessAffinityMask(GetCurrentProcess(), &all, &sys); + DWORD len = 0; + GetLogicalProcessorInformationEx(RelationProcessorCore, NULL, &len); + SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *buf = len ? malloc(len) : NULL; + int cores = 0; + if (buf && GetLogicalProcessorInformationEx(RelationProcessorCore, buf, &len)) + for (DWORD off = 0; off < len;) { + SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *e = (void *)((char *)buf + off); + KAFFINITY m = e->Processor.GroupMask[0].Mask & all; + if (e->Processor.GroupMask[0].Group == 0 && m) one |= m & -m, cores++; // the lowest of the core's + off += e->Size; + } + free(buf); + if (mode && one && one != all) + rt_log("cpu: %d cores, %d logical processors: one per core%s", cores, __builtin_popcountll(all), + mode == 2 ? " in the B blocks" : ""); + else + mode = 0; + } + int on = RT_KNOB_ON(mode); + if (on == applied) return; + applied = on; + SetProcessAffinityMask(GetCurrentProcess(), on ? one : all); +} + void perf_present(uint64_t frame) { - static uint64_t last, sum, max, n, win_start; + static uint64_t last, sum, max, n, win_start, ab_b; static LONG64 win_prev[PERF_CNT_N]; render_tid = GetCurrentThreadId(); cur_frame = frame; @@ -540,6 +593,8 @@ void perf_present(uint64_t frame) { InterlockedExchange64(&dump_hi, (LONG64)now); } uint64_t rep = ft_frame(frame, now, d); // d is 0 on the first present: it only starts the period + ab_b += rt_ab(); + cpu_placement(); if (__atomic_load_n(&overlay, __ATOMIC_RELAXED)) // the overlay's only cost while it is off: this branch perfmon_frame(now, d, frame_ns, rep, __atomic_load_n(&counts[PERF_CNT_TEX_UP], __ATOMIC_RELAXED)); kern_core_diag_frame(frame, d); @@ -581,7 +636,7 @@ void perf_present(uint64_t frame) { uint64_t shader_keys, shader_xlated; gfx_shader_counts(&shader_keys, &shader_xlated); if (n && span) - rt_log("perf: frame %llu: interval avg %.2f ms max %.2f ms (%.1f fps); render thread blocked: pacing %.0f%% fence %.0f%% drain %.0f%% pipeline build %.0f%% shader xlate %.0f%% texture %.0f%% (astc %.0f%%); pipelines built in background %lld, draws skipped pending %lld, waited %lld, waiting for a shader %lld, one-shot waited %lld; textures uploaded %lld re-uploaded %lld; slowest frame: xlate %.1f ms texture %.1f ms (astc %.1f) pipeline %.1f ms fence %.1f ms; shaders: %llu keys, %llu translated; render busy avg %.2f ms; replay avg %.2f ms", + rt_log("perf: frame %llu: interval avg %.2f ms max %.2f ms (%.1f fps); render thread blocked: pacing %.0f%% fence %.0f%% drain %.0f%% pipeline build %.0f%% shader xlate %.0f%% texture %.0f%% (astc %.0f%%); pipelines built in background %lld, draws skipped pending %lld, waited %lld, waiting for a shader %lld, one-shot waited %lld; textures uploaded %lld re-uploaded %lld; slowest frame: xlate %.1f ms texture %.1f ms (astc %.1f) pipeline %.1f ms fence %.1f ms; shaders: %llu keys, %llu translated; render busy avg %.2f ms; replay avg %.2f ms; ab %llu/%llu", (unsigned long long)frame, sum / 1e6 / n, max / 1e6, n * 1e9 / sum, 100.0 * wait_ns[PERF_PACE] / span, 100.0 * wait_ns[PERF_FENCE] / span, 100.0 * wait_ns[PERF_DRAIN] / span, 100.0 * wait_ns[PERF_PIPE] / span, 100.0 * wait_ns[PERF_XLATE] / span, @@ -590,9 +645,9 @@ void perf_present(uint64_t frame) { worst_ns[PERF_ASTC] / 1e6, worst_ns[PERF_PIPE] / 1e6, worst_ns[PERF_FENCE] / 1e6, (unsigned long long)shader_keys, (unsigned long long)shader_xlated, (span > wait_ns[PERF_PACE] ? span - wait_ns[PERF_PACE] : 0) / 1e6 / n, - InterlockedExchange64(&replay_ns, 0) / 1e6 / n); + InterlockedExchange64(&replay_ns, 0) / 1e6 / n, (unsigned long long)ab_b, (unsigned long long)n); if (n && span) perf_gpu_line(frame, sum / 1e6 / n, (double)wait_ns[PERF_PACE] / span, n); - sum = max = n = 0; + sum = max = n = ab_b = 0; memset(wait_ns, 0, sizeof wait_ns); win_start = now; } diff --git a/tools/ab_summary.py b/tools/ab_summary.py new file mode 100644 index 00000000..9851dd33 --- /dev/null +++ b/tools/ab_summary.py @@ -0,0 +1,43 @@ +"""In-launch A/B summary of a TOTK_AB run (runtime/prof_win.c rt_ab): per perf window of 60 presents wholly in an A or a +B block, the frame interval and the replay time; their means per side, and the B - A difference per A B B A cycle. + +usage: ab_summary.py [--from ] +""" +import argparse +import re +import statistics +import sys + +LINE = re.compile(r'perf: frame (\d+): interval avg ([0-9.]+) ms .*replay avg ([0-9.]+) ms; ab (\d+)/(\d+)') + + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument('log') + ap.add_argument('--from', dest='lo', type=int, default=0, help='first frame counted (after the load)') + a = ap.parse_args() + side = {0: [], 1: []} + for line in open(a.log, encoding='utf-8', errors='replace'): + m = LINE.search(line) + if not m or int(m.group(1)) < a.lo: + continue + b, n = int(m.group(4)), int(m.group(5)) + if b and b != n: + continue # a window across a block edge + side[1 if b else 0].append((int(m.group(1)), float(m.group(2)), float(m.group(3)))) + if not side[0] or not side[1]: + print('no whole A and B windows (TOTK_AB unset, or the run too short)') + return 1 + for k, name in ((0, 'A'), (1, 'B')): + w = side[k] + iv = statistics.mean(x[1] for x in w) + rp = statistics.mean(x[2] for x in w) + print(f'{name}: {len(w):3d} windows, interval {iv:6.2f} ms ({1000 / iv:5.1f} fps), replay {rp:5.2f} ms') + ia, ib = statistics.mean(x[1] for x in side[0]), statistics.mean(x[1] for x in side[1]) + ra, rb = statistics.mean(x[2] for x in side[0]), statistics.mean(x[2] for x in side[1]) + print(f'B - A: interval {ib - ia:+.2f} ms ({100 * (ib - ia) / ia:+.1f}%), replay {rb - ra:+.2f} ms ({100 * (rb - ra) / ra:+.1f}%)') + return 0 + + +if __name__ == '__main__': + sys.exit(main())