From 95ae91687cdf0b6f1624f839af6c27e276c117e7 Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Tue, 27 Oct 2026 10:17:13 +0300 Subject: [PATCH 1/7] Spin short SleepThread calls on the performance counter: the high-resolution waitable timer ended the SDK's 20 us polling sleeps after 460-515 us on the main thread and the job workers; the sched trace now reports wake-to-run and core-grant-to-run latency and asked versus taken sleep time per thread Co-Authored-By: Claude Opus 5.5 --- runtime/kernel.c | 47 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/runtime/kernel.c b/runtime/kernel.c index 5dd3087c..23c61766 100644 --- a/runtime/kernel.c +++ b/runtime/kernel.c @@ -89,6 +89,8 @@ struct RtThread { uint64_t st_since, st_ns[S_N], nblk, npre, pre_since, cpu_last; uint64_t wobj; // what the current wait is on: owner handle, key, address or first handle uint32_t waker; // the thread whose signal ended the last wait (0: a host thread or a timeout) + uint64_t wake_at, grant_at; // when a wait was signalled and a core granted, until the thread runs again + uint64_t lw_ns, lw_n, lw_max, lg_ns, lg_n, lg_max, sl_n, sl_req, sl_act; // those latencies; SleepThread asked/took RtThread *target; // as a core waiter: the owner core_kick asked to give its core up for it (holding G) // TOTK_ASYNC_PREEMPT (holding G) HANDLE hthr; // the host thread, for SuspendThread / Get/SetThreadContext; open while it may own a core @@ -214,6 +216,7 @@ static void wake(RtThread *t, uint32_t res) { t->woke = 1; t->wres = res; t->waker = tcur ? tcur->handle : 0; + if (rt_trace & RT_TRACE_SCHED) t->wake_at = kern_now_ns(); WakeConditionVariable(&t->cv); } @@ -255,6 +258,13 @@ static uint32_t block_inner(RtThread *t, int kind, int64_t ns) { uint64_t ms = (deadline - now + 999999) / 1000000; SleepConditionVariableSRW(&t->cv, &G, ms > 0xFFFFFFF0u ? 0xFFFFFFF0u : (DWORD)ms, 0); } + if (t->wake_at) { + uint64_t d = kern_now_ns() - t->wake_at; + t->lw_ns += d; + t->lw_n++; + if (d > t->lw_max) t->lw_max = d; + t->wake_at = 0; + } return t->wres; } @@ -747,6 +757,7 @@ static void core_grant(int c) { g_core_owner[c] = b; core_count(); if (g_diag > 0) b->dg_rel = g_dg_rel; + if (rt_trace & RT_TRACE_SCHED) b->grant_at = kern_now_ns(); WakeConditionVariable(&b->cv); } @@ -873,6 +884,13 @@ static void core_take(RtThread *t) { } } if (g_diag > 0) dg_done(t); + if (t->grant_at) { + uint64_t d = kern_now_ns() - t->grant_at; + t->lg_ns += d; + t->lg_n++; + if (d > t->lg_max) t->lg_max = d; + t->grant_at = 0; + } t->target = NULL; acct(t, S_RUN); } @@ -988,7 +1006,11 @@ void kern_core_stats_reset(void) { #define REP_N 8 void kern_sched_report(uint64_t frame) { static uint64_t last; - typedef struct { uint32_t h; int prio, render; uint64_t mask, entry, ns[S_N], nblk, npre, cpu; } Row; + typedef struct { + uint32_t h; + int prio, render; + uint64_t mask, entry, ns[S_N], nblk, npre, cpu, lw_ns, lw_n, lw_max, lg_ns, lg_n, lg_max, sl_n, sl_req, sl_act; + } Row; Row rows[REP_N + 1]; WaitRec rw[2][4] = {0}; // the render and the main thread's longest waits int nr = 0; @@ -1036,6 +1058,9 @@ void kern_sched_report(uint64_t frame) { memcpy(r->ns, t->st_ns, sizeof r->ns); r->nblk = t->nblk; r->npre = t->npre; + r->lw_ns = t->lw_ns, r->lw_n = t->lw_n, r->lw_max = t->lw_max; + r->lg_ns = t->lg_ns, r->lg_n = t->lg_n, r->lg_max = t->lg_max; + r->sl_n = t->sl_n, r->sl_req = t->sl_req, r->sl_act = t->sl_act; r->cpu = 0; HANDLE th = t->tid ? OpenThread(THREAD_QUERY_LIMITED_INFORMATION, FALSE, t->tid) : NULL; FILETIME a, b, k, u; @@ -1062,6 +1087,7 @@ void kern_sched_report(uint64_t frame) { for (RtThread *t = g_all; t; t = t->all) { memset(t->st_ns, 0, sizeof t->st_ns); t->nblk = t->npre = 0; + t->lw_ns = t->lw_n = t->lw_max = t->lg_ns = t->lg_n = t->lg_max = t->sl_n = t->sl_req = t->sl_act = 0; } memset(g_rw, 0, sizeof g_rw); memset(g_mw, 0, sizeof g_mw); @@ -1084,6 +1110,11 @@ void kern_sched_report(uint64_t frame) { rt_log("sched: %s%#x p%d m%llx%s cpu %.0f%% blocks %llu preempted %llu (%s)", r->render ? "render " : "", r->h, r->prio, (unsigned long long)r->mask, s, 100.0 * r->cpu / span, (unsigned long long)r->nblk, (unsigned long long)r->npre, r->entry ? d : "main"); + rt_log("sched: %#x wake to run avg %.1f us max %.0f us (%llu), core grant to run avg %.1f us max %.0f us " + "(%llu), sleep %llu asked avg %.1f us took avg %.1f us", r->h, r->lw_n ? r->lw_ns / 1e3 / r->lw_n : 0.0, + r->lw_max / 1e3, (unsigned long long)r->lw_n, r->lg_n ? r->lg_ns / 1e3 / r->lg_n : 0.0, r->lg_max / 1e3, + (unsigned long long)r->lg_n, (unsigned long long)r->sl_n, r->sl_n ? r->sl_req / 1e3 / r->sl_n : 0.0, + r->sl_n ? r->sl_act / 1e3 / r->sl_n : 0.0); } for (int k = 0; k < 2; k++) for (int j = 0; j < 4 && rw[k][j].ns; j++) { @@ -1300,7 +1331,16 @@ void kern_run_on_guest_main(void (*fn)(Cpu *c)) { CloseHandle(h); } +// The high-resolution waitable timer ends a wait 0.3-0.5 ms late at best: the SDK's 20 us polling sleeps (main thread +// and job workers, about 1.5 per frame each) took 460-515 us. Sleeps up to SLEEP_SPIN_NS spin on the performance +// counter instead; the virtual core is already given up. +#define SLEEP_SPIN_NS 1000000 static void sleep_ns(RtThread *t, int64_t ns) { + if (ns <= SLEEP_SPIN_NS) { + uint64_t end = kern_now_ns() + (uint64_t)ns; + while (kern_now_ns() < end) YieldProcessor(); + return; + } if (!t->timer) { t->timer = CreateWaitableTimerExW(NULL, NULL, CREATE_WAITABLE_TIMER_HIGH_RESOLUTION, TIMER_ALL_ACCESS); if (!t->timer) t->timer = CreateWaitableTimerExW(NULL, NULL, 0, TIMER_ALL_ACCESS); @@ -1444,8 +1484,13 @@ int kern_svc(Cpu *c, uint32_t imm) { t->wobj = (uint64_t)ns; t->nblk++; unlock(); + uint64_t t0 = kern_now_ns(); sleep_ns(t, ns); + uint64_t took = kern_now_ns() - t0; lock(); + t->sl_n++; + t->sl_req += (uint64_t)ns; + t->sl_act += took; core_take(t); unlock(); return 1; From dc69797746ee330d4478e53d6b80f19daf641baa Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Tue, 27 Oct 2026 10:17:14 +0300 Subject: [PATCH 2/7] Index recording copies by 64 KiB block instead of 4 KiB page: AddControlMemory walked about 500 pages per call under the exclusive copy lock (53 calls a frame, 2 MiB ranges on the 1.4.3 field), and the main thread and job workers recording command buffers waited on it; field walk 72-77 fps to 78-80 Co-Authored-By: Claude Opus 5.5 --- runtime/nvn_api_cmd.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/runtime/nvn_api_cmd.c b/runtime/nvn_api_cmd.c index 054f3794..b05b1da2 100644 --- a/runtime/nvn_api_cmd.c +++ b/runtime/nvn_api_cmd.c @@ -29,7 +29,7 @@ #define MIN_COMMAND_SIZE 512 // NVN_DEVICE_INFO_COMMAND_BUFFER_MIN_COMMAND_SIZE (nvn_api_core.c devinfo) #define MIN_CONTROL_SIZE 256 // NVN_DEVICE_INFO_COMMAND_BUFFER_MIN_CONTROL_SIZE #define CTL_PER_RECORDING 64 // control memory one recording is reported to use -#define COPY_PAGE_SHIFT 12 +#define COPY_PAGE_SHIFT 16 // recording copy index granularity (release_copies) static cmd_t *cmd_of(const Cpu *c) { return arg_p(c, 0); } @@ -158,8 +158,10 @@ static const tex_t *tex_pin(const NVNtexture *nt) { } // ---------- recording copies -// A handle points just past a copy_prefix. Copies are indexed by the 4 KiB page of their control-memory position, so -// adding control memory again releases the copies recorded there without walking every live copy. +// A handle points just past a copy_prefix. Copies are indexed by the 64 KiB block of their control-memory position, so +// adding control memory again releases the copies recorded there without walking every live copy. 4 KiB pages cost +// about 500 lookups per AddControlMemory under the exclusive lock (1.4.3 field: 53 calls a frame, 2 MiB ranges, 3.7 +// copies per page), and the recording threads waited on it. typedef struct { uint64_t magic, bytes, made_at, used_at, deferred; } copy_prefix; // deferred: 0 unknown, 1 none, 2 some _Static_assert(sizeof(copy_prefix) % 8 == 0, "records stay 8-byte aligned"); typedef struct { uintptr_t lo, hi; uint8_t *copy; } cmd_copy; From 650c69bcb5246064f2e09f024f6b42153736b6e7 Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Tue, 27 Oct 2026 10:45:53 +0300 Subject: [PATCH 3/7] Save the pipeline cache and append pipeline keys on an autosave thread, without P.lock: the save held P.lock through vkGetPipelineCacheData and the file write (about 60 ms) and ran on the present thread, so every 20 s the replay stopped at its next pipeline lookup or waited for the present thread (60-80 ms freezes on the 1.4.3 fight); shader record files are written outside RC.lock for the same reason Co-Authored-By: Claude Opus 5.5 --- runtime/gfx_shader.c | 11 ++++++++--- runtime/vk_backend.c | 2 +- runtime/vk_pipeline.c | 34 +++++++++++++++++++++++++++++++--- 3 files changed, 40 insertions(+), 7 deletions(-) diff --git a/runtime/gfx_shader.c b/runtime/gfx_shader.c index 099218db..8cafa3ea 100644 --- a/runtime/gfx_shader.c +++ b/runtime/gfx_shader.c @@ -1626,18 +1626,23 @@ static int rec_append(const char *path, const char *magic, uint32_t version, uin return !fclose(f) && ok; } +static SRWLOCK rec_io = SRWLOCK_INIT; // the record files: appends in order, no read during an append + void gfx_shader_record_flush(int force) { static volatile LONG64 last; LONG64 now = (LONG64)GetTickCount64(); if (!RC.on || (!force && now - last < 2000)) return; last = now; + // The files are written outside RC.lock, which the render thread takes to record a new answer. + AcquireSRWLockExclusive(&rec_io); AcquireSRWLockExclusive(&RC.lock); Bytes ans = RC.ans, prg = RC.prg; RC.ans = (Bytes){0}, RC.prg = (Bytes){0}; uint64_t total = RC.pairs.n, added = RC.pairs_new; + ReleaseSRWLockExclusive(&RC.lock); if (prg.n && !rec_append(RC.prg_path, "TOTKSPRG", PRG_VERSION, 0, &prg)) rt_log("gfx_shader: cannot write %s", RC.prg_path); if (ans.n && !rec_append(RC.ans_path, "TOTKSANS", ANS_VERSION, ANS_REC, &ans)) rt_log("gfx_shader: cannot write %s", RC.ans_path); - ReleaseSRWLockExclusive(&RC.lock); + ReleaseSRWLockExclusive(&rec_io); if (ans.n) rt_log("gfx_shader: answers: %zu new records appended (%llu new this run, %llu recorded)", ans.n / ANS_REC, (unsigned long long)added, (unsigned long long)total); free(ans.p), free(prg.p); @@ -1801,9 +1806,9 @@ size_t gfx_shader_warm_list(GfxWarmEnt **out) { RC.ans_path); uint8_t *ab = NULL, *pb = NULL; size_t an = 0, pn = 0; - AcquireSRWLockExclusive(&RC.lock); // no append meanwhile + AcquireSRWLockExclusive(&rec_io); // no append meanwhile rec_read(RC.ans_path, "TOTKSANS", ANS_VERSION, ANS_REC, &ab, &an); - ReleaseSRWLockExclusive(&RC.lock); + ReleaseSRWLockExclusive(&rec_io); rec_read(prp, "TOTKSPRP", PRP_VERSION, PRP_REC, &pb, &pn); size_t na = an / ANS_REC, np = pn / PRP_REC, n = 0; GfxWarmEnt *e = calloc(na + np + 1, sizeof *e); diff --git a/runtime/vk_backend.c b/runtime/vk_backend.c index ec04b300..c67dc4df 100644 --- a/runtime/vk_backend.c +++ b/runtime/vk_backend.c @@ -84,7 +84,7 @@ static DWORD WINAPI present_main(void *arg) { j.at && t1 > j.at ? (t1 - j.at) / 1e6 : 0.0); last = t1; last_queued = j.queued; - vk_pipeline_autosave(); // off the render thread, outside the present lock + vk_pipeline_autosave(); // starts the autosave thread once AcquireSRWLockExclusive(&pl); } return 0; diff --git a/runtime/vk_pipeline.c b/runtime/vk_pipeline.c index 39aaf2a5..4b87bfab 100644 --- a/runtime/vk_pipeline.c +++ b/runtime/vk_pipeline.c @@ -3009,6 +3009,8 @@ void vk_pipeline_stats(VkPipeStats *s) { ReleaseSRWLockShared(&P.lock); } +static SRWLOCK save_lock = SRWLOCK_INIT; // one cache save at a time; shutdown takes it to destroy the cache + void vk_pipeline_save(void) { if (P.ready) keys_flush(); if (P.ready && P.gpl) { @@ -3036,7 +3038,13 @@ void vk_pipeline_save(void) { } gfx_shader_record_flush(1); if (!P.ready || !P.cache || !P.cache_path[0]) return; - AcquireSRWLockExclusive(&P.lock); + // Not under P.lock: the driver synchronises P.cache, and holding P.lock through the copy and the file write (about + // 60 ms on the 1.4.3 fight) stopped every pipeline lookup of the replay, a freeze at each 20 s autosave. + AcquireSRWLockExclusive(&save_lock); + if (!P.cache) { // vk_pipeline_shutdown destroyed it + ReleaseSRWLockExclusive(&save_lock); + return; + } size_t size = 0; void *data = NULL; if (vkGetPipelineCacheData(P.device, P.cache, &size, NULL) == VK_SUCCESS && size && (data = malloc(size)) && @@ -3044,12 +3052,12 @@ void vk_pipeline_save(void) { if (!plat_file_write_atomic(P.cache_path, data, size)) rt_log("vk_pipeline: cannot write %s", P.cache_path); } free(data); - ReleaseSRWLockExclusive(&P.lock); + ReleaseSRWLockExclusive(&save_lock); } // The game never shuts the device down (boot.c and the window end the process), so the cache is saved while it // runs: at most every 20 s, and only after new pipelines were built. New pipeline keys are appended every 2 s. -void vk_pipeline_autosave(void) { +static void autosave_once(void) { static uint64_t last_n, last_ms, last_keys; if (!P.ready) return; uint64_t now = GetTickCount64(); @@ -3063,6 +3071,24 @@ void vk_pipeline_autosave(void) { vk_pipeline_save(); } +// On its own thread: the save (vkGetPipelineCacheData and the file write, about 60 ms) and the key appends (a flush to +// the disk each) ran on the present thread, and the render thread waited for it to take the next frame. +static DWORD WINAPI autosave_main(void *arg) { + (void)arg; + for (;;) { + Sleep(500); + autosave_once(); + } +} + +void vk_pipeline_autosave(void) { + static LONG started; + if (!P.ready || InterlockedExchange(&started, 1)) return; + HANDLE t = CreateThread(NULL, 0, autosave_main, NULL, 0, NULL); + if (t) SetThreadPriority(t, THREAD_PRIORITY_BELOW_NORMAL), CloseHandle(t); + else rt_log("vk_pipeline: cannot start the autosave thread; the cache is saved at exit only"); +} + void vk_pipeline_shutdown(void) { if (!P.ready) return; lb_stop(); @@ -3094,8 +3120,10 @@ void vk_pipeline_shutdown(void) { } layouts[i] = NULL; } + AcquireSRWLockExclusive(&save_lock); // not while the autosave thread copies it if (P.cache) vkDestroyPipelineCache(P.device, P.cache, NULL); P.cache = VK_NULL_HANDLE; + ReleaseSRWLockExclusive(&save_lock); memset(&P.stats, 0, sizeof P.stats); InterlockedExchange(&P.ready, 0); ReleaseSRWLockExclusive(&P.lock); From 6fca9576704b939d607664d9b78c270b76c48163 Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Tue, 27 Oct 2026 11:01:54 +0300 Subject: [PATCH 4/7] Run the save Commit on a background thread: the copy, flushes and renames held the game thread 95-135 ms at every game save in the 1.4.3 fight save The game's Commit returns at once. Every later change to the working copy, Unmount and the next Commit wait for the thread, so no write lands in the snapshot, and the window close, guest ExitProcess and --frames exits wait through fs_save_flush. A crash during the commit loses that commit only; the transaction and save_recover are unchanged. probe-fs waits for each commit and checks a write and an Unmount right after Commit. Co-Authored-By: Claude Opus 5.5 --- runtime/boot.c | 2 + runtime/fs.h | 2 + runtime/fs_host.c | 98 ++++++++++++++++++++++++++++++++++++-------- runtime/gfx_window.c | 4 +- runtime/svc.c | 2 + 5 files changed, 90 insertions(+), 18 deletions(-) diff --git a/runtime/boot.c b/runtime/boot.c index d83b04fc..66f23e81 100644 --- a/runtime/boot.c +++ b/runtime/boot.c @@ -15,6 +15,7 @@ #include #include "cfg.h" +#include "fs.h" #include "gfx.h" #include "nvn_api.h" #include "vk.h" @@ -283,6 +284,7 @@ void rt_frame_presented(const tex_t *t, uint64_t n) { stats_line(); fflush(stdout); fflush(stderr); + fs_save_flush(); vk_pipeline_save(); // no-op without the Vulkan back end prof_write(); fflush(stderr); diff --git a/runtime/fs.h b/runtime/fs.h index ac2e6240..07cfe7a0 100644 --- a/runtime/fs.h +++ b/runtime/fs.h @@ -48,3 +48,5 @@ void fs_host_set_profile(const char *dir); // profile directory; // (default rt_config()->profile) int fs_save_busy(void); // a save is being written: a save file open for writing, // or save data changed in the last 3 s and not committed +void fs_save_flush(void); // wait for a save Commit still running in the background; + // every exit path calls it before the process ends diff --git a/runtime/fs_host.c b/runtime/fs_host.c index 5d1ccfd8..69c88f8e 100644 --- a/runtime/fs_host.c +++ b/runtime/fs_host.c @@ -13,8 +13,9 @@ // /cache// cache storage // // The save mount is journaled as on Horizon: changes go to a working copy and become the committed save atomically at -// Commit; a crash or an Unmount before Commit leaves the previous committed save. fs_save_busy still tells a window -// close to wait until a save in progress is committed. Paths are UTF-8 and go to the W APIs as UTF-16. +// Commit; a crash or an Unmount before Commit leaves the previous committed save. Commit returns at once and the copy +// runs on a background thread (section "save transactions"); fs_save_flush lets the exit paths wait for it. Paths are +// UTF-8 and go to the W APIs as UTF-16. #include #include #include @@ -70,7 +71,9 @@ static DWORD attributes(const char *path) { // (TotK writes a slot's files back to back and then commits; between the files nothing is open for a moment). static volatile LONG save_writers, save_dirty; static volatile LONG64 save_changed_ns; +static void save_wait_commit(void); static void save_touched(void) { + save_wait_commit(); // every change to save data comes here first: none lands in a commit's snapshot InterlockedExchange64(&save_changed_ns, (LONG64)plat_now_ns()); InterlockedExchange(&save_dirty, 1); } @@ -158,7 +161,10 @@ static int remove_tree(const char *path) { // Commit: (1) copy work to new and flush every file (FlushFileBuffers) and directory, a file equal to the committed // one as a hard link to it; (2) rename save/ to old; // (3) rename new to save/, the commit point; (4) move old to save-backup/ and keep the newest three. -// Both renames are MoveFileExW with MOVEFILE_WRITE_THROUGH. save/ is renamed away only after new is complete +// Both renames are MoveFileExW with MOVEFILE_WRITE_THROUGH. The game's Commit returns 0 once steps (1) to (4) are +// queued; a thread runs them (95-135 ms on the game thread before, in the 1.4.3 fight save) while every change to the +// working copy waits in save_touched, and Unmount and the next Commit wait too. A crash in that time loses this Commit +// only, and a failed one is logged instead of returned. save/ is renamed away only after new is complete // on disk, so the recovery rule at start (save_recover) is: // save/ present it is the committed save (the old one if the crash came before (3), the new one after); // new, if present, is an unfinished copy and is deleted (rollback); @@ -425,18 +431,56 @@ static uint32_t save_commit_locked(void) { return 0; } -static uint32_t save_commit(void) { +// The background commit: commit_pending from the game's Commit until the thread has run save_commit_locked. +static SRWLOCK pend_lock = SRWLOCK_INIT; +static CONDITION_VARIABLE pend_cv = CONDITION_VARIABLE_INIT; +static volatile LONG commit_pending; + +static void save_wait_commit(void) { + if (!InterlockedCompareExchange(&commit_pending, 0, 0)) return; + AcquireSRWLockExclusive(&pend_lock); + while (commit_pending) SleepConditionVariableSRW(&pend_cv, &pend_lock, INFINITE, 0); + ReleaseSRWLockExclusive(&pend_lock); +} + +void fs_save_flush(void) { save_wait_commit(); } + +static uint32_t commit_run(void) { AcquireSRWLockExclusive(&tx_lock); - InterlockedIncrement(&save_writers); // fs_save_busy for the duration - static int quiet; - uint32_t r = 0; - if (InterlockedCompareExchange(&save_dirty, 0, 0)) r = save_commit_locked(); - else if (!quiet++) rt_log("fs commit save: nothing changed (logged once)"); + uint32_t r = save_commit_locked(); InterlockedDecrement(&save_writers); ReleaseSRWLockExclusive(&tx_lock); + AcquireSRWLockExclusive(&pend_lock); + commit_pending = 0; + ReleaseSRWLockExclusive(&pend_lock); + WakeAllConditionVariable(&pend_cv); return r; } +static DWORD WINAPI commit_main(void *arg) { + (void)arg; + commit_run(); + return 0; +} + +static uint32_t save_commit(void) { + save_wait_commit(); // the previous Commit + static int quiet; + if (!InterlockedCompareExchange(&save_dirty, 0, 0)) { + if (!quiet++) rt_log("fs commit save: nothing changed (logged once)"); + return 0; + } + InterlockedIncrement(&save_writers); // fs_save_busy until the thread is done + InterlockedExchange(&commit_pending, 1); + HANDLE t = CreateThread(NULL, 0, commit_main, NULL, 0, NULL); + if (t) { + CloseHandle(t); + return 0; + } + rt_log("fs: cannot start the save commit thread (error %lu); committing on the game thread", GetLastError()); + return commit_run(); +} + // ---- storage roots static BOOL CALLBACK storage_init(PINIT_ONCE once, PVOID param, PVOID *ctx) { (void)once, (void)param, (void)ctx; @@ -678,6 +722,7 @@ static uint32_t read_cache_list(int *count, CacheStorageInfo *out, uint64_t h, i static void close_cache_list(uint64_t h) { free((void *)(uintptr_t)h); } static void unmount(const char *name) { + save_wait_commit(); // else the working copy could go before a Commit queued just now has copied it AcquireSRWLockExclusive(&tx_lock); AcquireSRWLockExclusive(&lock); for (int i = 0; i < NMOUNTS; i++) @@ -1335,6 +1380,13 @@ static void probe_restart(const uint64_t *uid) { CHECK(RES(S_MOUNT_SAVE, U("save"), U(uid)) == 0, "MountSaveData after a restart"); } +// Commit through the override, then wait for its background thread, as the checks read the disk next. +static uint32_t tx_commit(void) { + uint32_t r = RES(S_COMMIT, U("save")); + fs_save_flush(); + return r; +} + #define TX_SIZE 4096 static uint8_t tx_buf[TX_SIZE]; @@ -1405,7 +1457,7 @@ static int count_dirs(const wchar_t *dir) { static void probe_save_tx(const uint64_t *uid) { const char *a = "save:/tx_a.sav", *b = "save:/tx_b.sav"; uint64_t h = 0; - CHECK(tx_write(a, 1) && tx_write(b, 1) && RES(S_COMMIT, U("save")) == 0 && tx_read_committed("tx_a.sav") == 1, + CHECK(tx_write(a, 1) && tx_write(b, 1) && tx_commit() == 0 && tx_read_committed("tx_a.sav") == 1, "Commit of generation 1"); // uncommitted changes: the game reads them; the committed save and the next start do not see them @@ -1417,7 +1469,7 @@ static void probe_save_tx(const uint64_t *uid) { CHECK(tx_read(a) == 1 && tx_read("save:/tx_c.sav") == -1, "an uncommitted write survived a restart"); // committed changes survive a restart; an Unmount without Commit drops changes - CHECK(tx_write(a, 3) && tx_write(b, 3) && RES(S_COMMIT, U("save")) == 0, "Commit of generation 3"); + CHECK(tx_write(a, 3) && tx_write(b, 3) && tx_commit() == 0, "Commit of generation 3"); probe_restart(uid); CHECK(tx_read(a) == 3 && tx_read(b) == 3, "a commit lost in a restart"); CHECK(tx_write(a, 4), "write before Unmount"); @@ -1430,7 +1482,7 @@ static void probe_save_tx(const uint64_t *uid) { for (int k = 1; k < 64; k++) { int next = ++gen, ok = tx_write(a, next) && tx_write(b, next); tx_crash_at = k; - uint32_t r = RES(S_COMMIT, U("save")); + uint32_t r = tx_commit(); int died = tx_dead; tx_crash_at = 0; if (!died) { @@ -1456,19 +1508,31 @@ static void probe_save_tx(const uint64_t *uid) { // a Commit writes the changed file and links the unchanged one to the committed save it replaces int kept = committed; - CHECK(tx_write(a, ++gen) && RES(S_COMMIT, U("save")) == 0 && tx_read_committed("tx_a.sav") == gen && + CHECK(tx_write(a, ++gen) && tx_commit() == 0 && tx_read_committed("tx_a.sav") == gen && tx_read_committed("tx_b.sav") == kept && tx_links_committed("tx_b.sav") >= 2 && tx_links_committed("tx_a.sav") == 1, "Commit of one changed file: tx_a %d links, tx_b %d links", tx_links_committed("tx_a.sav"), tx_links_committed("tx_b.sav")); committed = gen; + // the background commit: a write right after Commit waits for it and stays out of it; an Unmount right after + // Commit waits for it and keeps it + int queued = ++gen; + CHECK(tx_write(a, queued) && RES(S_COMMIT, U("save")) == 0 && tx_write(a, ++gen) && tx_read(a) == gen && + tx_read_committed("tx_a.sav") == queued, "a write right after Commit: committed tx_a %d, expected %d", + tx_read_committed("tx_a.sav"), queued); + CHECK(RES(S_COMMIT, U("save")) == 0, "Commit before Unmount"); + CALL(S_UNMOUNT, U("save")); + CHECK(tx_read_committed("tx_a.sav") == gen && RES(S_MOUNT_SAVE, U("save"), U(uid)) == 0 && tx_read(a) == gen, + "an Unmount right after Commit lost it"); + committed = gen; + // the read-only mount shows the committed save and refuses changes CHECK(tx_write(a, 99) && RES(S_MOUNT_SAVE_RO, U("ro"), TOTK_APPLICATION_ID, U(uid)) == 0 && tx_read("ro:/tx_a.sav") == committed && RES(S_OPEN_FILE, U(&h), U("ro:/tx_a.sav"), FS_OPEN_WRITE) == FS_R_UNSUPPORTED_OPEN && RES(S_CREATE_FILE, U("ro:/x"), 0) == FS_R_UNSUPPORTED_OPEN && RES(S_RENAME_FILE, U("save:/tx_a.sav"), U("ro:/y")) == FS_R_INVALID_PATH, "MountSaveDataReadOnly"); CALL(S_UNMOUNT, U("ro")); - CHECK(RES(S_DELETE_FILE, U(a)) == 0 && RES(S_DELETE_FILE, U(b)) == 0 && RES(S_COMMIT, U("save")) == 0 && + CHECK(RES(S_DELETE_FILE, U(a)) == 0 && RES(S_DELETE_FILE, U(b)) == 0 && tx_commit() == 0 && tx_read_committed("tx_a.sav") == -1, "Commit of deletions"); printf("save tx: uncommitted writes visible to the game and dropped by a restart and by Unmount, commits kept; a " "crash at each of %d commit steps recovers to the old save %d times, the new one %d times, mixed %d " @@ -1518,7 +1582,7 @@ static void probe_storage(void) { CHECK(fs_save_busy(), "a save file open for writing does not count as a save in progress"); CALL(S_CLOSE_FILE, h); CHECK(fs_save_busy(), "closed but uncommitted save data does not count as a save in progress"); - CHECK(RES(S_COMMIT, U("save")) == 0 && !fs_save_busy(), "Commit"); + CHECK(tx_commit() == 0 && !fs_save_busy(), "Commit"); CHECK(RES(S_COMMIT, U("nomount")) == FS_R_PATH_NOT_FOUND, "Commit of an unknown mount"); // paths @@ -1549,7 +1613,7 @@ static void probe_storage(void) { count == 1 && ents[0].type == FS_ENTRY_FILE, "ReadDirectory, files only"); CALL(S_CLOSE_DIRECTORY, d); - CHECK(RES(S_COMMIT, U("save")) == 0, "Commit of the directories"); + CHECK(tx_commit() == 0, "Commit of the directories"); // reopen after unmount: the bytes come back through the overrides and are on disk CALL(S_UNMOUNT, U("save")); @@ -1588,7 +1652,7 @@ static void probe_storage(void) { RES(S_DELETE_FILE, U("save:/renamed.sav")) == FS_R_PATH_NOT_FOUND, "DeleteFile"); CHECK(RES(S_DELETE_RECURSIVE, U("save:/dir")) == 0 && RES(S_ENTRY_TYPE, U(&type), U("save:/dir")) == FS_R_PATH_NOT_FOUND, "DeleteDirectoryRecursively"); - CHECK(RES(S_COMMIT, U("save")) == 0 && !fs_save_busy(), "Commit after the changes"); + CHECK(tx_commit() == 0 && !fs_save_busy(), "Commit after the changes"); probe_save_tx(uid); // temporary and cache storage diff --git a/runtime/gfx_window.c b/runtime/gfx_window.c index eb01ea28..3da9d1eb 100644 --- a/runtime/gfx_window.c +++ b/runtime/gfx_window.c @@ -19,6 +19,7 @@ #include "cfg.h" #include "flight.h" +#include "fs.h" #include "gfx.h" #include "input.h" #include "nvn_api.h" @@ -392,8 +393,9 @@ static LRESULT CALLBACK wnd_proc(HWND h, UINT m, WPARAM w, LPARAM l) { return 0; } case WM_CLOSE: - // The process ends with the window, as in MNG. Save writes go straight to files (runtime/fs_host.c). + // The process ends with the window, as in MNG, once a save Commit still running in the background is done. rt_log("gfx: window closed; exiting"); + fs_save_flush(); vk_pipeline_save(); prof_write(); // ExitProcess runs no atexit handlers ExitProcess(0); diff --git a/runtime/svc.c b/runtime/svc.c index 4dc9b451..604af0a1 100644 --- a/runtime/svc.c +++ b/runtime/svc.c @@ -8,6 +8,7 @@ #include #include +#include "fs.h" #include "host.h" #include "kernel.h" #include "vk.h" @@ -220,6 +221,7 @@ static void svc_body(Cpu *c, uint32_t imm, uint64_t pc) { // profile saved, logs flushed, exit code 0 char at[160]; rt_log("guest ExitProcess at %s; exiting", ldr_describe(pc, at, sizeof at)); + fs_save_flush(); vk_pipeline_save(); prof_write(); fflush(NULL); From adc4d7aa9c2b637dd819cce7a89df46e756bfe92 Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Tue, 27 Oct 2026 11:40:32 +0300 Subject: [PATCH 5/7] Dump every thread's call chains of a slow present (TOTK_PROFILE_SLOW=), and never copy a stack whose Rsp sits in its guard page The sampler keeps the last half second of every thread's chains in a ring and writes the samples of a present interval over the threshold to .f, per thread and time (tstack), with the samples that took over 2 ms to stop their thread (tstall: the thread was in a long kernel call). A thread that ran no cycle since its last sample repeats its chain without being suspended. A thread stopped between its sub rsp and the stack probe has Rsp in the guard page; copying from there raised STATUS_GUARD_PAGE_VIOLATION and ended a run. Co-Authored-By: Claude Opus 5.5 --- runtime/prof_win.c | 135 +++++++++++++++++++++++++++++++++++++-------- 1 file changed, 113 insertions(+), 22 deletions(-) diff --git a/runtime/prof_win.c b/runtime/prof_win.c index afbe799c..36e9c575 100644 --- a/runtime/prof_win.c +++ b/runtime/prof_win.c @@ -9,6 +9,13 @@ // 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. +// TOTK_PROFILE_SLOW= keeps every thread's chains of the last half second in a ring (a thread outside the exe, mostly +// waiting, copies 8 KiB of stack, one in it STK_COPY; a thread that ran no cycle since its previous sample is not +// suspended and repeats its previous chain; a sample that took over 2 ms to stop its thread, which happens while the +// thread is in a long kernel call, is written as `tstall ...` with the +// chain where the thread stopped) and, after a present at least after the previous one, writes +// that interval's samples (plus the 20 ms before) to .f: the profile format, one `stack 1` line per +// sample, and `tstack ...` lines that keep the thread and the time. At most 64 dumps. #include #include #include @@ -25,14 +32,15 @@ #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; +#define PROF_DEPTH 24 // frames kept per call chain, the sampled Rip first +// cycles, depth, last: TOTK_PROFILE_SLOW only, the thread's cycle count and chain at its previous sample +typedef struct { DWORD tid; HANDLE h; uint64_t n, in_exe, cycles; uint32_t depth; uint64_t last[PROF_DEPTH]; } ThreadSlot; static RipCount table[PROF_SLOTS]; typedef struct { uint64_t hash, n; uint32_t depth; uint64_t rip[PROF_DEPTH]; } StackCount; @@ -54,6 +62,18 @@ static int chains; // call chains are kept: TOTK_PROFILE_RENDER 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 +#define SLOW_RING (1u << 15) // TOTK_PROFILE_SLOW: about 0.5 s of samples of every thread +#define SLOW_DUMPS 64 +typedef struct { uint64_t t; DWORD tid; uint32_t depth; uint64_t rip[PROF_DEPTH]; } Sample; +static Sample ring[SLOW_RING]; +static uint64_t ring_n; +#define STALL_RING 1024 +static Sample stalls[STALL_RING]; // t: when the stop began; rip[PROF_DEPTH - 1] holds the stop's ns +static uint64_t stall_n; +static uint64_t slow_ns; // TOTK_PROFILE_SLOW in ns, 0 off +static volatile LONG64 dump_lo, dump_hi, dump_frame; // the render thread asks for a window, the sampler writes it +static volatile DWORD replay_tid; // asynchronous replay (vk_backend.c): its waits hold up the frame too + static void count_rip(uint64_t rip) { uint32_t i = (uint32_t)((rip * 0x9E3779B97F4A7C15ull) >> 48); for (uint32_t k = 0; k < 64; k++, i = (i + 1) & (PROF_SLOTS - 1)) { @@ -138,6 +158,54 @@ static void refresh_threads(DWORD self) { n_threads = n; } +static void write_modules(FILE *f) { + HMODULE mods[1024]; + DWORD need = 0; + if (!EnumProcessModules(GetCurrentProcess(), mods, sizeof mods, &need)) return; + for (DWORD i = 0; i < need / sizeof(HMODULE) && i < 1024; i++) { + MODULEINFO mi; + char path[MAX_PATH]; + if (GetModuleInformation(GetCurrentProcess(), mods[i], &mi, sizeof mi) && GetModuleFileNameA(mods[i], path, sizeof path)) + fprintf(f, "module %llx %lx %s\n", (unsigned long long)(uintptr_t)mi.lpBaseOfDll, mi.SizeOfImage, path); + } +} + +// The sampler, between ticks: the samples of the window the render thread asked for. +static void slow_dump(void) { + uint64_t lo = (uint64_t)dump_lo, hi = (uint64_t)dump_hi, frame = (uint64_t)dump_frame; + static int dumps; + char path[1100]; + snprintf(path, sizeof path, "%s.f%llu", out_path, (unsigned long long)frame); + FILE *f = dumps < SLOW_DUMPS ? fopen(path, "w") : NULL; + if (f) { + dumps++; + fprintf(f, "totk-prof 1\nbase %llx\nperiod_us 1000\nframe %llu interval_ms %.1f\nmain %lu render %lu replay %lu\n", + (unsigned long long)(uintptr_t)GetModuleHandleW(NULL), (unsigned long long)frame, (hi - lo) / 1e6 - 20, + main_tid, render_tid, replay_tid); + write_modules(f); + uint64_t first = ring_n > SLOW_RING ? ring_n - SLOW_RING : 0; + for (int pass = 0; pass < 2; pass++) + for (uint64_t k = first; k < ring_n; k++) { + const Sample *s = &ring[k & (SLOW_RING - 1)]; + if (s->t < lo || s->t > hi) continue; + if (pass) fprintf(f, "tstack %lu %.1f", s->tid, (s->t - lo) / 1e6); + else fprintf(f, "stack 1"); + for (uint32_t d = 0; d < s->depth; d++) fprintf(f, " %llx", (unsigned long long)s->rip[d]); + fputc('\n', f); + } + for (uint64_t k = stall_n > STALL_RING ? stall_n - STALL_RING : 0; k < stall_n; k++) { + const Sample *s = &stalls[k & (STALL_RING - 1)]; + if (s->t + s->rip[PROF_DEPTH - 1] < lo || s->t > hi) continue; + fprintf(f, "tstall %lu %.1f %.1f", s->tid, ((double)s->t - (double)lo) / 1e6, s->rip[PROF_DEPTH - 1] / 1e6); + for (uint32_t d = 0; d < s->depth; d++) fprintf(f, " %llx", (unsigned long long)s->rip[d]); + fputc('\n', f); + } + fclose(f); + rt_log("prof: slow frame %llu (%.1f ms) -> %s", (unsigned long long)frame, (hi - lo) / 1e6 - 20, path); + } + InterlockedExchange64(&dump_hi, 0); +} + static DWORD WINAPI sampler_main(void *arg) { (void)arg; DWORD self = GetCurrentThreadId(); @@ -153,27 +221,54 @@ static DWORD WINAPI sampler_main(void *arg) { Sleep(1); } if (tick % 64 == 0) refresh_threads(self); + if (dump_hi) slow_dump(); if (frame_hi && (cur_frame < frame_lo || cur_frame >= frame_hi)) continue; + uint64_t now = slow_ns ? kern_now_ns() : 0; for (int i = 0; i < n_threads; i++) { ThreadSlot *t = &threads[i]; if ((only_render && t->tid != render_tid) || (only_main && t->tid != main_tid)) continue; + uint64_t cyc = 0; + if (slow_ns && t->depth && QueryThreadCycleTime(t->h, &cyc) && cyc == t->cycles) { // still waiting + Sample *s = &ring[ring_n++ & (SLOW_RING - 1)]; + s->t = now, s->tid = t->tid, s->depth = t->depth; + memcpy(s->rip, t->last, t->depth * sizeof *t->last); + continue; + } ctx.ContextFlags = chains ? CONTEXT_CONTROL | CONTEXT_INTEGER : CONTEXT_CONTROL; + uint64_t stop0 = slow_ns ? kern_now_ns() : 0; if (SuspendThread(t->h) == (DWORD)-1) continue; - BOOL ok = GetThreadContext(t->h, &ctx); + BOOL ok = GetThreadContext(t->h, &ctx); // returns once the thread has stopped in user mode + uint64_t stop_ns = slow_ns ? kern_now_ns() - stop0 : 0; size_t n = 0; MEMORY_BASIC_INFORMATION mb; - if (ok && chains && VirtualQuery((void *)ctx.Rsp, &mb, sizeof mb) && mb.State == MEM_COMMIT) { + // a thread stopped between its sub rsp and the stack probe has Rsp in the guard page: touching it would fault + if (ok && chains && VirtualQuery((void *)ctx.Rsp, &mb, sizeof mb) && mb.State == MEM_COMMIT && + !(mb.Protect & (PAGE_GUARD | PAGE_NOACCESS))) { n = (uintptr_t)mb.BaseAddress + mb.RegionSize - ctx.Rsp; // the committed stack above Rsp - if (n > STK_COPY) n = STK_COPY; + size_t cap = slow_ns && (ctx.Rip < exe_lo || ctx.Rip >= exe_hi) ? 8u << 10 : STK_COPY; + if (n > cap) n = cap; 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); + uint64_t chain[PROF_DEPTH] = {ctx.Rip}; + uint32_t d = n ? unwind_copy(&ctx, ctx.Rsp, n, chain) : 0; + if (d) count_stack(chain, d); + if (slow_ns) { + Sample *s = &ring[ring_n++ & (SLOW_RING - 1)]; + s->t = now, s->tid = t->tid, s->depth = d ? d : 1; + memcpy(s->rip, chain, s->depth * sizeof *chain); + if (stop_ns > 2000000) { + Sample *w = &stalls[stall_n++ & (STALL_RING - 1)]; + *w = *s; + w->t = stop0; + if (w->depth > PROF_DEPTH - 1) w->depth = PROF_DEPTH - 1; + w->rip[PROF_DEPTH - 1] = stop_ns; + } + QueryThreadCycleTime(t->h, &t->cycles); // after ResumeThread: the cycles the sample itself cost + t->depth = s->depth; + memcpy(t->last, chain, s->depth * sizeof *chain); } total++; t->n++; @@ -207,16 +302,7 @@ void prof_write(void) { (unsigned long long)total, (unsigned long long)dropped); for (int i = 0; i < n_threads; i++) fprintf(f, "thread %lu %llu %llu\n", threads[i].tid, (unsigned long long)threads[i].n, (unsigned long long)threads[i].in_exe); - HMODULE mods[1024]; - DWORD need = 0; - if (EnumProcessModules(GetCurrentProcess(), mods, sizeof mods, &need)) { - for (DWORD i = 0; i < need / sizeof(HMODULE) && i < 1024; i++) { - MODULEINFO mi; - char path[MAX_PATH]; - if (GetModuleInformation(GetCurrentProcess(), mods[i], &mi, sizeof mi) && GetModuleFileNameA(mods[i], path, sizeof path)) - fprintf(f, "module %llx %lx %s\n", (unsigned long long)(uintptr_t)mi.lpBaseOfDll, mi.SizeOfImage, path); - } - } + write_modules(f); int cnt = 0; for (uint32_t i = 0; i < PROF_SLOTS; i++) if (table[i].rip) table[cnt++] = table[i]; @@ -267,7 +353,9 @@ void prof_start(void) { only_render = cfg_get("TOTK_PROFILE_RENDER") != NULL; only_main = !only_render && cfg_get("TOTK_PROFILE_MAIN") != NULL; const char *ch = cfg_get("TOTK_PROFILE_CHAINS"); - chains = only_render || only_main || (ch && *ch && strcmp(ch, "0")); + const char *sl = cfg_get("TOTK_PROFILE_SLOW"); + slow_ns = sl && atoi(sl) > 0 ? (uint64_t)atoi(sl) * 1000000ull : 0; + chains = only_render || only_main || slow_ns || (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; @@ -290,8 +378,6 @@ static uint64_t wait_ns[PERF_N], frame_ns[PERF_N], worst_ns[PERF_N]; // window uint64_t perf_begin(void) { return kern_now_ns(); } -static volatile DWORD replay_tid; // asynchronous replay (vk_backend.c): its waits hold up the frame too - void perf_replay_thread(void) { replay_tid = GetCurrentThreadId(); } void perf_main_thread(void) { main_tid = GetCurrentThreadId(); } @@ -448,6 +534,11 @@ void perf_present(uint64_t frame) { cur_frame = frame; if (rt_trace & RT_TRACE_SCHED) kern_sched_report(frame); uint64_t now = kern_now_ns(), d = last ? now - last : 0; + if (slow_ns && last && d >= slow_ns && !dump_hi) { + dump_lo = (LONG64)(now - d - 20000000ull); + dump_frame = (LONG64)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 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)); From ecc90b56789657d00a550262b01f2d40f45288db Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Tue, 27 Oct 2026 11:40:33 +0300 Subject: [PATCH 6/7] Record and replay input on a world clock: game seconds stepped since the player first stands away from the title screen's origin With the real-time frame step, a frame advances the game by its own length, so a recording keyed by presents replayed at another frame rate pressed at other game times, and every load of another length shifted the rest. Lines after the first save load are now written as "@ " and replayed on that clock; menu input before it stays in presents. Two replays of one recording whose loads ended at presents 1887 and 2094 kept the player within 2 m for 15 game seconds, until the fight's own nondeterminism took over. Co-Authored-By: Claude Opus 5.5 --- runtime/frame_rate.c | 29 ++++++++++++++-- runtime/host.h | 1 + runtime/input_win.c | 78 +++++++++++++++++++++++++++++--------------- 3 files changed, 79 insertions(+), 29 deletions(-) diff --git a/runtime/frame_rate.c b/runtime/frame_rate.c index b5d57c88..17ad1aed 100644 --- a/runtime/frame_rate.c +++ b/runtime/frame_rate.c @@ -22,6 +22,11 @@ // One 64-bit and one 32-bit store per present; the guest reads the floats on its own threads, and each stays finite // and in range whichever store it sees. // +// The world clock (frame_step_world_us) sums the steps from the first present that finds the player actor away from +// the origin, where the title screen keeps it: the end of the first save load. Recorded and scripted input +// (input_win.c) runs on it, so a replay presses at the same game time at any frame rate and after a first load of any +// length; a later load (fast travel) still shifts what follows by its length. +// // TOTK_TRACE=perf adds a line per 120 presents: fps, the game seconds stepped against wall seconds, and with // TOTK_SPEED_PROBE=1 the player's position and ground speed (an independent check: Link runs at the same speed at // any frame rate when game time follows wall time). @@ -201,11 +206,30 @@ static void speed_report(uint64_t now, double step, int interval) { memset(win_hist, 0, sizeof win_hist); } +static _Atomic int64_t world_us = -1; +static uint64_t presents; + +int64_t frame_step_world_us(void) { return atomic_load_explicit(&world_us, memory_order_relaxed); } + +static void world_clock(double dt, double step) { + presents++; + int64_t w = atomic_load_explicit(&world_us, memory_order_relaxed); + if (w < 0) { + float p[3]; + if (!player_pos(p) || fabsf(p[0]) + fabsf(p[2]) < 1.0f) return; // a few guest reads, until the load ends + rt_log("frame step: world clock starts at present %llu (the player at %.1f %.1f %.1f)", + (unsigned long long)presents, p[0], p[1], p[2]); + atomic_store_explicit(&world_us, 0, memory_order_relaxed); + return; + } + if (!step_word) step = dt > STEP_MAX_S ? STEP_MAX_S : dt; // whole-vsync steps: the display time stands in + atomic_store_explicit(&world_us, w + (int64_t)(step * 1e6 + 0.5), memory_order_relaxed); +} + void frame_step_present(uint64_t display_ns, int interval) { frame_limit_live(); - double step = 0; + double step = 0, dt = last_ns ? (double)(display_ns - last_ns) * 1e-9 : 0; if (step_word && last_ns) { - double dt = (double)(display_ns - last_ns) * 1e-9; if (dt > STEP_RESET_S || dt < 0.001) { sample_count = sample_next = 0; } else { @@ -219,5 +243,6 @@ void frame_step_present(uint64_t display_ns, int interval) { } } last_ns = display_ns; + world_clock(dt, step); if (rt_trace & RT_TRACE_PERF) speed_report(display_ns, step, interval); } diff --git a/runtime/host.h b/runtime/host.h index 43057521..0b38715d 100644 --- a/runtime/host.h +++ b/runtime/host.h @@ -237,6 +237,7 @@ void *rt_patch_page(void); // the page the patch void frame_step_init(void); // after rt_patches_init: the real-time step's first value int frame_step_realtime(void); // the real-time step is on: presents ignore the interval uint64_t frame_step_cap_ns(void); // with the real-time step: the shortest frame in ns +int64_t frame_step_world_us(void); // game time since the loaded save's first frame, -1 before // (TOTK_FPS_LIMIT, live) void frame_step_present(uint64_t display_ns, int interval); // nvnQueuePresentTexture: the frame's display time and // present interval (60 Hz ticks) diff --git a/runtime/input_win.c b/runtime/input_win.c index f0c37d20..c2a2553f 100644 --- a/runtime/input_win.c +++ b/runtime/input_win.c @@ -41,7 +41,11 @@ static const SDL_GamepadButton pad_sdl[PAD_COUNT] = { }; // TOTK_INPUT_SCRIPT=: lines "