Merge pull request #196: Size thread pools by the CPUs of the process's affinity mask (Lucas-blip-png)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Valiantsin DzerakhandClaude Opus 5.5 committed 2026-10-31 12:51:48 +03:00
commit 5f1a94c5e3
6 files changed
+24 -9

No files matched your search

+2 -1
View File
@@ -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;
+1 -1
View File
@@ -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 "
+2 -1
View File
@@ -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
+12
View File
@@ -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) {
+2 -1
View File
@@ -6,6 +6,7 @@
#include <windows.h>
#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);
+5 -5
View File
@@ -2095,7 +2095,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);
@@ -2444,7 +2444,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);
@@ -2830,7 +2830,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;
@@ -2903,7 +2903,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++;
@@ -2929,7 +2929,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);