From 91c5312932bfea56f9d9d3362e671cfe38305adb Mon Sep 17 00:00:00 2001 From: Lucas-blip-png <177052392+Lucas-blip-png@users.noreply.github.com> Date: Sat, 31 Oct 2026 04:51:14 -0400 Subject: [PATCH] Size thread pools by the CPUs of the process's affinity mask Eight places sized a pool (pipeline workers, library prebuild, translator prewarm and seeding, the multithreaded ASTC decoder) by GetActiveProcessorCount, every logical processor of the machine. A process limited with start /affinity, Task Manager or a launcher then started a pool for 16 CPUs on 4. plat_cpu_usable counts the affinity mask (all active processors for a process that spans processor groups); plat_cpu_count, the machine's count, stays for the system info. Idea from MMadmer's UsableCpuCount in a fork of the Shadow of the Colossus PC port. --- runtime/boot.c | 3 ++- runtime/kernel_probe.c | 2 +- runtime/platform/platform.h | 3 ++- runtime/platform/win32.c | 12 ++++++++++++ runtime/tex_astc_mt.c | 3 ++- runtime/vk_pipeline.c | 10 +++++----- 6 files changed, 24 insertions(+), 9 deletions(-) diff --git a/runtime/boot.c b/runtime/boot.c index 382ac850..dc1aac69 100644 --- a/runtime/boot.c +++ b/runtime/boot.c @@ -18,6 +18,7 @@ #include "fs.h" #include "gfx.h" #include "nvn_api.h" +#include "platform/platform.h" #include "vk.h" #define SYM_INIT_START "_ZN2nn4init5StartEmmPFvvES2_S2_" @@ -326,7 +327,7 @@ static DWORD WINAPI prewarm_main(void *arg) { vk_pipeline_prewarm(); const char *prep = cfg_get("TOTK_TEST_PREPARE_SHADERS"); // Task 2.3 test hook; Task 2.6 builds the preparation if (prep && !strcmp(prep, "1")) { - int cpus = (int)GetActiveProcessorCount(ALL_PROCESSOR_GROUPS); + int cpus = (int)plat_cpu_usable(); gfx_shader_prepare_recorded(cpus, cpus / 2 > 16 ? 16 : cpus / 2 < 1 ? 1 : cpus / 2); } return 0; diff --git a/runtime/kernel_probe.c b/runtime/kernel_probe.c index 06b2b771..411242ec 100644 --- a/runtime/kernel_probe.c +++ b/runtime/kernel_probe.c @@ -416,7 +416,7 @@ static void test_cores(Cpu *c) { for (int i = 0; i < CORE_WORKERS; i++) join(c, h[i]); int now, held_max; kern_core_stats(&now, &held_max); - DWORD cpus = GetActiveProcessorCount(ALL_PROCESSOR_GROUPS); + DWORD cpus = plat_cpu_usable(); int gate = !cfg_bool("TOTK_FREE_CORES"); // the kernel's own switch int ok = g_run_max <= 3 && held_max <= 3 && !g_core_clash && (cpus < 4 || g_run_max == 3); printf("virtual cores: %d threads x %d runs on %lu host CPUs: at most %ld running guest code at once, at most %d " diff --git a/runtime/platform/platform.h b/runtime/platform/platform.h index c5761651..6f7bfe2e 100644 --- a/runtime/platform/platform.h +++ b/runtime/platform/platform.h @@ -19,7 +19,8 @@ void plat_thread_detach(PlatThread *t); // frees t; the thread runs on // Restricts t (NULL: the calling thread) to the logical processors in mask (bit n = processor n of the first group). // Returns the previous mask, 0 on failure. uint64_t plat_thread_affinity(PlatThread *t, uint64_t mask); -uint32_t plat_cpu_count(void); // logical processors +uint32_t plat_cpu_count(void); // logical processors of the machine (system info) +uint32_t plat_cpu_usable(void); // the ones this process may run on (its affinity mask): size thread pools by it // ---- time uint64_t plat_now_ns(void); // monotonic, high resolution, arbitrary epoch diff --git a/runtime/platform/win32.c b/runtime/platform/win32.c index 6ab2fbdb..9a0d4687 100644 --- a/runtime/platform/win32.c +++ b/runtime/platform/win32.c @@ -66,6 +66,18 @@ uint32_t plat_cpu_count(void) { return n ? (uint32_t)n : 1; } +// The processors of the process's affinity mask: what `start /affinity`, Task Manager's "Set affinity" or a launcher +// leaves this process. A process that spans processor groups (more than 64 logical processors) gets no mask from +// GetProcessAffinityMask (both masks 0): every active processor counts then. +uint32_t plat_cpu_usable(void) { + DWORD_PTR proc = 0, sys = 0; + if (GetProcessAffinityMask(GetCurrentProcess(), &proc, &sys) && proc && sys) { + uint32_t n = (uint32_t)__builtin_popcountll((unsigned long long)proc); + if (n) return n; + } + return plat_cpu_count(); +} + // ---- time static uint64_t qpc_freq(void) { diff --git a/runtime/tex_astc_mt.c b/runtime/tex_astc_mt.c index cf809926..ac08117a 100644 --- a/runtime/tex_astc_mt.c +++ b/runtime/tex_astc_mt.c @@ -6,6 +6,7 @@ #include #include "host.h" +#include "platform/platform.h" #include "tex.h" #define MT_MIN_BLOCKS 1024 // below this, waking the pool costs about as much as the decode @@ -73,7 +74,7 @@ static DWORD WINAPI worker(void *arg) { static BOOL CALLBACK pool_init(PINIT_ONCE once, PVOID param, PVOID *ctx) { (void)once, (void)param, (void)ctx; InitializeCriticalSection(&P.busy); - int cpus = (int)GetActiveProcessorCount(ALL_PROCESSOR_GROUPS) - 2; + int cpus = (int)plat_cpu_usable() - 2; int n = (cpus < 1 ? 1 : cpus > MT_MAX_THREADS ? MT_MAX_THREADS : cpus) - 1; for (int i = 0; i < n; i++) { HANDLE t = CreateThread(NULL, 0, worker, NULL, 0, NULL); diff --git a/runtime/vk_pipeline.c b/runtime/vk_pipeline.c index 15758686..7d6b03d3 100644 --- a/runtime/vk_pipeline.c +++ b/runtime/vk_pipeline.c @@ -2084,7 +2084,7 @@ static DWORD WINAPI pipe_worker(void *arg) { // Starts the pool on first use: a quarter of the logical processors, 2 to 4. Caller holds P.lock. static int workers_start(void) { if (A.threads) return 1; - DWORD cpus = GetActiveProcessorCount(ALL_PROCESSOR_GROUPS); + DWORD cpus = plat_cpu_usable(); int n = cpus / 4 < 2 ? 2 : cpus / 4 > MAX_WORKERS ? MAX_WORKERS : (int)(cpus / 4); for (int i = 0; i < n; i++) { HANDLE t = CreateThread(NULL, 0, pipe_worker, (void *)(intptr_t)A.threads, 0, NULL); @@ -2433,7 +2433,7 @@ void vk_pipeline_prewarm(void) { size_t u = 0; for (size_t i = 0; i < m; i++) if (!u || memcmp(ids[u - 1], ids[i], 32)) memcpy(ids[u++], ids[i], 32); - int cpus = (int)GetActiveProcessorCount(ALL_PROCESSOR_GROUPS); + int cpus = (int)plat_cpu_usable(); const char *pe = cfg_get("TOTK_PREWARM_XLATE"); // no fixed default: half the CPUs, at most 16, when unset int procs = pe && *pe ? atoi(pe) : cpus / 2 > 16 ? 16 : cpus / 2; gfx_shader_prewarm((const uint8_t(*)[32])ids, u, sh, cpus, procs < 1 ? 1 : procs); @@ -2819,7 +2819,7 @@ static void lb_start(const uint32_t pr_hist[16], const uint32_t fs_hist[4]) { LB.n = n; lb_state(pr_hist, fs_hist); LB.idle = 1; - int cpus = (int)GetActiveProcessorCount(ALL_PROCESSOR_GROUPS); + int cpus = (int)plat_cpu_usable(); int t = te && *te ? atoi(te) : cpus / 4; t = t < 1 ? 1 : t > LB_MAX_THREADS ? LB_MAX_THREADS : t; if ((size_t)t > n) t = (int)n; @@ -2892,7 +2892,7 @@ static void lb_warm_prepared(const char *dir) { LB.n = n, LB.idle = 0, LB.t0 = now_ms(); static const uint32_t no_pr[16], no_fs[4]; lb_state(no_pr, no_fs); - int cpus = (int)GetActiveProcessorCount(ALL_PROCESSOR_GROUPS), k = 0; + int cpus = (int)plat_cpu_usable(), k = 0; HANDLE t[64]; for (int i = 0; i < cpus && i < 64; i++) if ((t[k] = CreateThread(NULL, 0, lb_warm_worker, NULL, 0, NULL))) k++; @@ -2918,7 +2918,7 @@ int vk_pipeline_prepare_shaders(void) { char *cut = strrchr(dir, '\\'); if (!cut) return 1; *cut = 0; - int cpus = (int)GetActiveProcessorCount(ALL_PROCESSOR_GROUPS); + int cpus = (int)plat_cpu_usable(); const char *pe = cfg_get("TOTK_PREWARM_XLATE"); // no fixed default: half the CPUs, at most 16, when unset int procs = pe && *pe ? atoi(pe) : cpus / 2 > 16 ? 16 : cpus / 2; int rc = gfx_shader_prepare_seed(dir, cpus, procs < 1 ? 1 : procs);