From e85a5ffc484c637d93bcf3187112c87ab7f794b2 Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Wed, 25 Nov 2026 15:57:38 +0300 Subject: [PATCH] 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();