From e85a5ffc484c637d93bcf3187112c87ab7f794b2 Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Wed, 25 Nov 2026 15:57:38 +0300 Subject: [PATCH 1/3] Switches for the preemption timing of virtual cores: TOTK_AP_FIRST_US and TOTK_HOST_CHECKPOINT TOTK_AP_FIRST_US sets how long a waiter spins before it first interrupts a worse owner (default 250 as before). TOTK_HOST_CHECKPOINT=1 makes an owner with a pending preempt request give its core up when a host call or SVC returns to game code, instead of at its next SVC or indirect call. Both off by default; for timing on the islands bench (critical path: worse owners hold a better thread's core 0.70 ms a frame there). Co-Authored-By: Claude Opus 5.5 --- runtime/cfg.c | 4 ++-- runtime/kernel.c | 23 ++++++++++++++++++++--- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/runtime/cfg.c b/runtime/cfg.c index 45d66eea..8aa34f84 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -33,7 +33,7 @@ static int save_ok = 1; // the last cfg_save_to wrote (o // logged as unknown. A new variable goes here too, or its ini key logs a harmless "unknown key" line, and gets a // description in totk.ini.example or docs/settings.md (tests/settings_doc_check.py checks both). static const char *const known[] = { - "AB", "ABILITY_GUARD", "ADDR_REPORT", "AMIIBO_DIR", "ANISO", "ANV_VS_BARRIER", "APP_POOL", "ASPECT", "ASPECT_PATCH", "ASSUME_FOCUS", "ASTC", "ASTC_BC", "ASTC_TIME", "ASYNC_PREEMPT", "AUDIO", "AUDIO_CHANNELS", "AUDIO_DEVICE", "AUDIO_DRAIN_FIX", "AUDIO_OFF", "AUDIO_PREFILL_MS", + "AB", "ABILITY_GUARD", "ADDR_REPORT", "AMIIBO_DIR", "ANISO", "ANV_VS_BARRIER", "AP_FIRST_US", "APP_POOL", "ASPECT", "ASPECT_PATCH", "ASSUME_FOCUS", "ASTC", "ASTC_BC", "ASTC_TIME", "ASYNC_PREEMPT", "AUDIO", "AUDIO_CHANNELS", "AUDIO_DEVICE", "AUDIO_DRAIN_FIX", "AUDIO_OFF", "AUDIO_PREFILL_MS", "BACKGROUND_WINDOW", "BIND_A", "BIND_B", "BIND_DOWN", "BIND_L", "BIND_LCLICK", "BIND_LEFT", "BIND_LSTICK_DOWN", "BIND_LSTICK_LEFT", "BIND_LSTICK_RIGHT", "BIND_LSTICK_UP", "BIND_MINUS", "BIND_PLUS", "BIND_R", "BIND_RCLICK", "BIND_RIGHT", "BIND_RSTICK_DOWN", "BIND_RSTICK_LEFT", "BIND_RSTICK_RIGHT", "BIND_RSTICK_UP", @@ -52,7 +52,7 @@ static const char *const known[] = { "GAMEPLAY_WEAPON_WHEEL", "GPL", "GPL_DRAW_BUILD", "GPU", "GPU_INDEX", "GPU_NAME", "GRASS_RANGE", "GYRO_AUTO_BIAS", "GYRO_BIAS_PAD", "GYRO_BIAS_X", "GYRO_BIAS_Y", "GYRO_BIAS_Z", "GYRO_CALIBRATION", "HDR", "HDR_GAME_NITS", "HDR_HUE", - "HDR_PEAK_NITS", "HDR_UI_NITS", "HEAP_LAZY", "HIGHFPS_FIX", "HUD_WIDTH", "INPUT", "INPUT_RECORD", "INPUT_SCRIPT", "KSPIN_US", "LANG", "LIB_PREBUILD", + "HOST_CHECKPOINT", "HDR_PEAK_NITS", "HDR_UI_NITS", "HEAP_LAZY", "HIGHFPS_FIX", "HUD_WIDTH", "INPUT", "INPUT_RECORD", "INPUT_SCRIPT", "KSPIN_US", "LANG", "LIB_PREBUILD", "LIB_PREBUILD_KEEP", "LIB_PREBUILD_THREADS", "LIGHTNING_FIX", "LOCALE", "LOD_GUARD", "LOD_STEADY", "LSTICK_DEADZONE", "MAP_CLICK", "MEMORY", "MEMX", "MENU_LANG", "MENU_TEST", "MENU_FONT_FONTCONFIG", "MEM_RESERVE_MIB", "MENU_CURSOR", "MENU_CURSOR_LOG", "MENU_TEST_DIR", "MENU_TEST_SET", "MINIMAL_LAYOUT", "MIRROR_GAP", "MODS_DIR", "MODS_DISABLED", "MODS_NO_CODE", "MODS_ORDER", "MODS_REBUILD", "MODS_WITHOUT_CODE", "MOTION", "MOUSE_AIM_SCALE", "MOUSE_MODE", "MOUSE_DIRECT_PATCH", "MOUSE_SENS", "MOVIE_ASPECT", "MOVIE_HOST", "MOVIE_PACE", "MVOBJ_ALL", "MVOBJ_BANKS", "MVOBJ_EQUAL", "MVOBJ_EXACT", "MVOBJ_EXEC", "MVOBJ_NOC4", "MVOBJ_ONLY", "MVOBJ_TIMES", "MVPREV_CHECK", "MVPREV_GROW", "MVPREV_SKIP", "MVPREV_TRUST", "MV_CHECK", diff --git a/runtime/kernel.c b/runtime/kernel.c index eb16a323..323ee468 100644 --- a/runtime/kernel.c +++ b/runtime/kernel.c @@ -389,6 +389,11 @@ static int core_allowed(const RtThread *t, int c) { return (int)((t->mask >> c) // inheritance and the preemption disable without interrupting owners. #define AP_FIRST_NS 250000ull // the waiter spins this long before the first interruption (most waits end sooner) #define AP_GAP_NS 1000000ull // and interrupts the same owner at most this often +// TOTK_AP_FIRST_US replaces AP_FIRST_NS (10..1000 us). TOTK_HOST_CHECKPOINT=1: an owner with a pending preempt request +// that returns from a host call or SVC to game code gives its core up there (kern_host_left), as at its next SVC or +// indirect call, instead of running on until one or until a waiter interrupts it. +static uint64_t g_ap_first_ns = AP_FIRST_NS; +static int g_host_cp; static int g_ap = -1, g_ap_int; // g_ap_int: interrupt owners (TOTK_ASYNC_PREEMPT=2: inheritance and disable only) static int g_ap_log; // redirects logged so far (AP_LOG_N) #define AP_LOG_N 40 @@ -401,6 +406,13 @@ static void ap_init(void) { const char *e = cfg_value("TOTK_ASYNC_PREEMPT"); g_ap = strcmp(e, "0") != 0; g_ap_int = g_ap && strcmp(e, "2") != 0; + const char *f = cfg_get("TOTK_AP_FIRST_US"); + if (f && *f) { + long long us = atoll(f); + g_ap_first_ns = (uint64_t)(us < 10 ? 10 : us > 1000 ? 1000 : us) * 1000; + } + const char *hc = cfg_get("TOTK_HOST_CHECKPOINT"); + g_host_cp = hc && !strcmp(hc, "1"); if (g_ap) { RtModule m[16]; int n = rt_linked_modules(m, 16); @@ -425,8 +437,12 @@ static void ap_init(void) { kern_host_track = 1; } } - rt_log("kernel: asynchronous preemption of virtual cores %s, priority inheritance and preemption disable %s; lifted " - "code exe+%#llx-exe+%#llx", g_ap_int ? "on" : "off", g_ap ? "on (TOTK_ASYNC_PREEMPT=0 turns all off)" : "off", + if (g_ap && g_host_cp) kern_host_track = 1; + rt_log("kernel: asynchronous preemption of virtual cores %s (first after %llu us, TOTK_AP_FIRST_US), priority " + "inheritance and preemption disable %s; preempt requests honoured at host call returns %s " + "(TOTK_HOST_CHECKPOINT); lifted code exe+%#llx-exe+%#llx", + g_ap_int ? "on" : "off", (unsigned long long)(g_ap_first_ns / 1000), + g_ap ? "on (TOTK_ASYNC_PREEMPT=0 turns all off)" : "off", g_ap && g_host_cp ? "on" : "off", (unsigned long long)(g_gen_lo ? g_gen_lo - plat_exe_base() : 0), (unsigned long long)(g_gen_hi ? g_gen_hi - plat_exe_base() : 0)); } @@ -733,6 +749,7 @@ int kern_core_diag_on(void) { return g_diag > 0; } void kern_host_left(void *thread) { RtThread *t = thread; if (g_diag > 0 && t->dg_kick && t->dg_host_kick && !t->dg_host_end) t->dg_host_end = kern_now_ns(); + if (g_host_cp && g_ap > 0 && t == tcur) kern_checkpoint(); } static int dg_in_host(const RtThread *o) { return __atomic_load_n(&o->host_depth, __ATOMIC_RELAXED) > 0; } @@ -1026,7 +1043,7 @@ static void core_take(RtThread *t) { core_kick(); if (g_diag > 0) t->dg_cls = dg_class0(t); if (g_ap_int && t->target && t->cur_core < 0) { // most waits end within microseconds: spin before interrupting - uint64_t end = kern_now_ns() + AP_FIRST_NS; + uint64_t end = kern_now_ns() + g_ap_first_ns; unlock(); while (__atomic_load_n(&t->cur_core, __ATOMIC_RELAXED) < 0 && kern_now_ns() < end) plat_pause(); lock(); From 912adc29650ffe8ec485ef6b24a2b986219167cf Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Wed, 25 Nov 2026 16:16:33 +0300 Subject: [PATCH 2/3] Count the cores given up at host call returns in the asynchronous preemption line With TOTK_HOST_CHECKPOINT=1 the line logged every 600 frames ends with the number of cores kern_host_left gave up, so a log shows the switch at work. Co-Authored-By: Claude Opus 5.5 --- runtime/kernel.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/runtime/kernel.c b/runtime/kernel.c index 323ee468..3429a354 100644 --- a/runtime/kernel.c +++ b/runtime/kernel.c @@ -394,6 +394,8 @@ static int core_allowed(const RtThread *t, int c) { return (int)((t->mask >> c) // indirect call, instead of running on until one or until a waiter interrupts it. static uint64_t g_ap_first_ns = AP_FIRST_NS; static int g_host_cp; +static uint64_t g_hcp_taken; // cores given up at a host call or SVC return (atomic) +static int checkpoint(void); static int g_ap = -1, g_ap_int; // g_ap_int: interrupt owners (TOTK_ASYNC_PREEMPT=2: inheritance and disable only) static int g_ap_log; // redirects logged so far (AP_LOG_N) #define AP_LOG_N 40 @@ -749,7 +751,7 @@ int kern_core_diag_on(void) { return g_diag > 0; } void kern_host_left(void *thread) { RtThread *t = thread; if (g_diag > 0 && t->dg_kick && t->dg_host_kick && !t->dg_host_end) t->dg_host_end = kern_now_ns(); - if (g_host_cp && g_ap > 0 && t == tcur) kern_checkpoint(); + if (g_host_cp && g_ap > 0 && t == tcur && checkpoint()) __atomic_add_fetch(&g_hcp_taken, 1, __ATOMIC_RELAXED); } static int dg_in_host(const RtThread *o) { return __atomic_load_n(&o->host_depth, __ATOMIC_RELAXED) > 0; } @@ -850,10 +852,10 @@ void kern_core_diag_frame(uint64_t frame, uint64_t interval_ns) { unlock(); rt_log("kernel: frame %llu: asynchronous preemption: %llu interrupted, %llu cores given up there; refused: " "%llu in a host call, %llu outside game code, %llu preemption disabled, %llu failed; %llu waits ended " - "in the first spin; SVC 0x36 %llu", + "in the first spin; SVC 0x36 %llu; cores given up at host call returns %llu", (unsigned long long)frame, (unsigned long long)v[0], (unsigned long long)v[1], (unsigned long long)v[2], (unsigned long long)v[3], (unsigned long long)v[4], (unsigned long long)v[5], (unsigned long long)v[6], - (unsigned long long)v[7]); + (unsigned long long)v[7], (unsigned long long)__atomic_load_n(&g_hcp_taken, __ATOMIC_RELAXED)); } if (g_diag <= 0) return; DgWindow w; @@ -1155,9 +1157,10 @@ void rt_spin_gate(Cpu *c, uint64_t head) { // A pending preempt request, at an SVC or an indirect call. The core goes over only if a better thread still waits // for it, so core_put grants it to a waiter better than t; a stale request (that waiter got another core) is dropped. -void kern_checkpoint(void) { +// checkpoint: 1 if the core was given up (holding G inside). +static int checkpoint(void) { RtThread *t = tcur; - if (!t || !__atomic_load_n(&t->preempt, __ATOMIC_RELAXED)) return; + if (!t || !__atomic_load_n(&t->preempt, __ATOMIC_RELAXED)) return 0; lock(); if (g_diag > 0 && t->cur) { t->dg_pc = t->cur->pc; @@ -1179,8 +1182,11 @@ void kern_checkpoint(void) { g_pre_stale++; } unlock(); + return better; } +void kern_checkpoint(void) { checkpoint(); } + // kern_ap_stub, on the interrupted owner: where to jump back to, then the core given up as at a checkpoint. uint64_t kern_ap_rip(void) { return tcur->ap_rip; } From 59f14012c1e1a9ca73748ac35eb4781c77fb6a20 Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Wed, 25 Nov 2026 20:56:10 +0300 Subject: [PATCH 3/3] host_checkpoint is on by default: a thread asked to give its virtual core to a better one gives it up when it returns from a host call or SVC to game code, so an interrupt refused inside a port function no longer leaves the core held until the next SVC or interrupt; on machine A (7900X, 4 cores, islands) the frame went from 13.71 to 13.04 ms over 5 clean runs each Co-Authored-By: Claude Opus 5.5 --- runtime/cfg.c | 5 +++-- runtime/kernel.c | 5 ++--- totk.ini.example | 5 +++++ 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/runtime/cfg.c b/runtime/cfg.c index 152a484c..d4cbab15 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -107,7 +107,7 @@ static const struct { const char *key, *section; } sections[] = { {"BIND_RIGHT", "input"}, {"BIND_DOWN", "input"}, {"BIND_LSTICK_LEFT", "input"}, {"BIND_LSTICK_UP", "input"}, {"BIND_LSTICK_RIGHT", "input"}, {"BIND_LSTICK_DOWN", "input"}, {"BIND_RSTICK_LEFT", "input"}, {"BIND_RSTICK_UP", "input"}, {"BIND_RSTICK_RIGHT", "input"}, {"BIND_RSTICK_DOWN", "input"}, {"BIND_SHAKE", "input"}, - {"ASYNC_PREEMPT", "performance"}, {"REPLAY_ASYNC", "performance"}, {"REPLAY_WORKERS", "performance"}, {"FREE_CORES", "performance"}, + {"ASYNC_PREEMPT", "performance"}, {"HOST_CHECKPOINT", "performance"}, {"REPLAY_ASYNC", "performance"}, {"REPLAY_WORKERS", "performance"}, {"FREE_CORES", "performance"}, {"TIMER_1MS", "performance"}, {"PIPE_WAIT", "performance"}, {"PIPE_SKIP", "performance"}, {"BARRIER_MERGE", "performance"}, {"LIB_PREBUILD", "performance"}, {"LIB_PREBUILD_THREADS", "performance"}, {"PIPE_PREBUILD", "performance"}, {"PREWARM_XLATE", "performance"}, {"GPL", "performance"}, {"ASTC", "performance"}, {"MEMORY", "performance"}, @@ -216,7 +216,8 @@ static const CfgMeta metas[] = { {"DISCORD_APP_ID", "1567232599846293504", NULL, LIVE}, {"DISCORD_IMAGE", "totk_512x512", NULL, LIVE}, // Advanced: scheduling - {"ASYNC_PREEMPT", "1", "0|1|2", RESTART}, {"REPLAY_ASYNC", "0", "0|1", RESTART}, + {"ASYNC_PREEMPT", "1", "0|1|2", RESTART}, {"HOST_CHECKPOINT", "1", "0|1", RESTART}, + {"REPLAY_ASYNC", "0", "0|1", RESTART}, {"REPLAY_WORKERS", "0", "0..16", RESTART}, {"REPLAY_CHUNKS", "0", "0..64", RESTART}, {"REPLAY_CHUNK_DRAWS", "128", "1..100000", RESTART}, {"REPLAY_STATE_CHECK", "0", "0|1", RESTART}, {"REPLAY_SKEW_US", "0", "0..100000", RESTART}, diff --git a/runtime/kernel.c b/runtime/kernel.c index 3429a354..74d7a8bb 100644 --- a/runtime/kernel.c +++ b/runtime/kernel.c @@ -389,7 +389,7 @@ static int core_allowed(const RtThread *t, int c) { return (int)((t->mask >> c) // inheritance and the preemption disable without interrupting owners. #define AP_FIRST_NS 250000ull // the waiter spins this long before the first interruption (most waits end sooner) #define AP_GAP_NS 1000000ull // and interrupts the same owner at most this often -// TOTK_AP_FIRST_US replaces AP_FIRST_NS (10..1000 us). TOTK_HOST_CHECKPOINT=1: an owner with a pending preempt request +// TOTK_AP_FIRST_US replaces AP_FIRST_NS (10..1000 us). TOTK_HOST_CHECKPOINT (on by default): an owner with a pending preempt request // that returns from a host call or SVC to game code gives its core up there (kern_host_left), as at its next SVC or // indirect call, instead of running on until one or until a waiter interrupts it. static uint64_t g_ap_first_ns = AP_FIRST_NS; @@ -413,8 +413,7 @@ static void ap_init(void) { long long us = atoll(f); g_ap_first_ns = (uint64_t)(us < 10 ? 10 : us > 1000 ? 1000 : us) * 1000; } - const char *hc = cfg_get("TOTK_HOST_CHECKPOINT"); - g_host_cp = hc && !strcmp(hc, "1"); + g_host_cp = cfg_bool("TOTK_HOST_CHECKPOINT"); if (g_ap) { RtModule m[16]; int n = rt_linked_modules(m, 16); diff --git a/totk.ini.example b/totk.ini.example index 7b24020d..cbc9d607 100644 --- a/totk.ini.example +++ b/totk.ini.example @@ -53,6 +53,11 @@ # A thread waiting for a virtual core interrupts the owner when it runs game code: 1 = on, 2 = priority inheritance # without the interrupt, 0 = off (the old scheduling). # async_preempt = 1 +# With async_preempt on, a thread asked to give its virtual core to a better one gives it up as soon as it returns +# from a port function (graphics, memory, files) to game code, instead of at its next system call or interrupt; an +# interrupt that lands inside a port function is refused. 0 or 1. Default: on. On a Ryzen 9 7900X with 4 cores the +# frame on the sky islands was about 5% shorter. +# host_checkpoint = 1 # Replay NVN submissions on their own thread, at most one frame behind the game, 0 or 1. Default: off. Helps where the # game waits for the GPU; adds up to a frame of latency. # replay_async = 0