diff --git a/nx2navi.ini.example b/nx2navi.ini.example index 13450974..98ebd431 100644 --- a/nx2navi.ini.example +++ b/nx2navi.ini.example @@ -66,6 +66,11 @@ # dyn_res = 0 # dyn_res_target = 16.7 # dyn_res_step = 45 +# Processors. On a processor whose dies have L3 caches of different sizes (AMD's two-die X3D processors, with 3D +# V-Cache on one die) the game runs on the die with the larger cache: 1 = on, 0 = Windows places the threads. +# cache_die = 1 +# One logical processor of each physical core, so no two busy threads share a core: 1 = on, 0 = all of them. +# one_per_core = 0 # Shaders and pipelines. # Draws whose pipeline is still building: default waits on a reserved worker; draw is the previous scheduling. diff --git a/runtime/cfg.c b/runtime/cfg.c index 761f485b..725d3a0d 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -38,7 +38,7 @@ static const char *const known[] = { "BIND_PLUS", "BIND_R", "BIND_RCLICK", "BIND_RIGHT", "BIND_RSTICK_DOWN", "BIND_RSTICK_LEFT", "BIND_RSTICK_RIGHT", "BIND_RSTICK_UP", "BIND_SHAKE", "BIND_SKIP", "BIND_STATS", "BIND_UP", "BIND_X", "BIND_Y", - "BIND_ZL", "BIND_ZR", "BLOCK_VIEWS_3D", "CLOTH_REALTIME", "COLOR_BRIGHTNESS", "COLOR_CONTRAST", "COLOR_GAMMA", + "BIND_ZL", "BIND_ZR", "BLOCK_VIEWS_3D", "CACHE_DIE", "CLOTH_REALTIME", "COLOR_BRIGHTNESS", "COLOR_CONTRAST", "COLOR_GAMMA", "COLOR_PRESET", "COLOR_SATURATION", "CONTACT_DEBUG", "CONTACT_SHADOWS", "CORE_DIAG", "CRIT", "CRIT_OUT", "DESC_TEMPLATE", "DEVICE_PROFILE", "DISCORD_APP_ID", "DISCORD_IMAGE", "DISCORD_RPC", "DLAA", "DLAA_CAM_KEEP", "DLAA_JITTER", "DLAA_MV", "DLAA_MASKDEBUG", "DLAA_MVDEBUG", "DLAA_OBJMV", "DLAA_SURVEY", "DLAA_TIMES", "DLSS", "DLSS_CHAR_MASK", "DLSS_PRESET", "DLSS_REACTIVE", "DUMP", "DUMP_BUF", "DUMP_DIR", "DUMP_LOG", "DUMP_FILE", "DS3", "LOG_CS_BINDS", "LOG_TEX_FLAGS", "LOG_MESH", "MESH", "POOL", "NATIVE_TENSOR", "LOG_RCOND", "LOG_RT_SIZES", "DUMP_WHITE", "DUMP_AFTER", "DUMP_MOVIE", "SUBMIT_FINISH", "ACQUIRE_WAIT", "CRASH_FILTER_GUARD", "CS_FALLBACK", "EXEFS", "FEEDBACK", "FIX_C0", "FIX_ZERO", "FLIGHT", "FLIGHT_AT", "FLIGHT_DIR", "FLIGHT_FRAMES", @@ -111,6 +111,7 @@ static const struct { const char *key, *section; } sections[] = { {"ASTC_GPU_DESWIZZLE", "performance"}, {"ASTC_SCRATCH_KEEP", "performance"}, {"ASTC_ARENA", "performance"}, {"POOL_CREATE_BUDGET", "performance"}, {"POOL_CREATE_MS", "performance"}, {"TIMER_1MS", "performance"}, {"PIPE_WAIT", "performance"}, {"PIPE_SKIP", "performance"}, {"ONESHOT_BUDGET", "performance"}, + {"CACHE_DIE", "performance"}, {"ONE_PER_CORE", "performance"}, {"LIB_PREBUILD", "performance"}, {"LIB_PREBUILD_THREADS", "performance"}, {"PIPE_PREBUILD", "performance"}, {"PREWARM_XLATE", "performance"}, {"GPL", "performance"}, {"ASTC", "performance"}, {"MEMORY", "performance"}, {"TEX_ALIAS", "performance"}, {"TEX_DISUSE", "performance"}, {"TEX_GEN_UNDO", "performance"}, {"TEX_REUSE", "performance"}, {"TEX_VRAM_PRESSURE", "performance"}, {"ASTC_BC", "performance"}, {"LOD_GUARD", "performance"}, {"FIX_C0", "performance"}, {"FIX_ZERO", "performance"}, @@ -204,6 +205,7 @@ static const CfgMeta metas[] = { {"RECORDER_CORE", "auto", "auto|0|1", RESTART}, {"RECORDER_PRIO", "1", "0|1", RESTART}, {"REPLAY_TIME", "0", "0|1", RESTART}, {"LOCK_TIME", "0", "0|1", RESTART}, {"POOL_CREATE_BUDGET", "16", "1..4096", RESTART}, {"POOL_CREATE_MS", "6", "0..100", RESTART}, // held submissions handed to the queue together (vk_sync.c, submit batching) {"SUBMIT_BATCH", "4", "1..16", RESTART}, + {"CACHE_DIE", "1", "0|1|ab", RESTART}, {"ONE_PER_CORE", "0", "0|1|ab", RESTART}, // perf.c cpu_placement // shaders and pipelines {"PIPE_WAIT", "default", "default|draw", RESTART}, {"PIPE_SKIP", "fullscreen", "strict|any|fullscreen", RESTART}, {"LIB_PREBUILD", "1", "0|1", RESTART}, {"LIB_PREBUILD_THREADS", "", "1..64", RESTART}, diff --git a/runtime/host.h b/runtime/host.h index f6c1efee..67ab7022 100644 --- a/runtime/host.h +++ b/runtime/host.h @@ -59,6 +59,7 @@ int perf_lock_holds_on(void); // NX2NAVI_LOCK_TIME void perf_lock_hold(void *site, uint64_t ns); // a lock held ns from site (vk_texture.c, holds over 1 ms) // NX2NAVI_LOCK_TIME: this thread's lock waits go into the frame-time summary extern void (*perf_rec_waits)(uint64_t *full_ns, uint64_t *drain_ns); // set by the back end (vk_rec.c totals) void perf_present(uint64_t frame); +void perf_cpu_start(void); // the process's logical processors (NX2NAVI_CACHE_DIE, NX2NAVI_ONE_PER_CORE), at start void perf_summary_flush(void); // prints the frame-time summary of the running period (exit); the 60 s lines come from perf_present void gfx_exit_log(void); // back end's lines for the exit summary (weak no-op in prof_win.c) void perf_gpu_wait(uint64_t t0); // a wait for a GPU fence ended (any thread; the render thread's also counts as PERF_FENCE) diff --git a/runtime/main.c b/runtime/main.c index dc27fc34..d59f6a15 100644 --- a/runtime/main.c +++ b/runtime/main.c @@ -547,6 +547,7 @@ int main(int argc, char **argv) { // Timed guest waits (kernel.c block_inner) sleep in whole milliseconds; at the default 15.6 ms tick a 1 ms wait // costs a full tick. NX2NAVI_TIMER_1MS=0 keeps the default for comparison. if (cfg_bool("NX2NAVI_TIMER_1MS")) plat_timer_1ms(); + perf_cpu_start(); // before any thread pool counts the usable processors { // NX2NAVI_POWER_FULL=0 leaves Windows' power throttling of an unfocused window in place (A/B runs) const char *pf = cfg_get("NX2NAVI_POWER_FULL"); if (!(pf && !strcmp(pf, "0"))) { diff --git a/runtime/perf.c b/runtime/perf.c index f0fd0f34..01390fa0 100644 --- a/runtime/perf.c +++ b/runtime/perf.c @@ -482,24 +482,49 @@ int rt_knob(const char *name, const char *def) { // render thread, the guest cores, the job workers) never share a core's execution units with another; "ab" switches // it with the A/B blocks. Off by default until measured here; the same rule // is reported to give +4-10% on an 8-core SMT CPU. +// +// NX2NAVI_CACHE_DIE=1, the default: on a processor whose dies have L3 caches of different sizes (AMD's two-die +// X3D processors, 3D V-Cache on one die) the process runs on the die with the larger cache, so the guest cores, the +// render thread and the recorder share one L3 and never cross dies. Windows' own placement of games on that die +// (game mode with AMD's driver) depends on recognizing the exe as a game. 0 leaves the placement to the OS; "ab" +// switches it with the A/B blocks. No change on one-die and Intel processors. +// +// The placement is set at start (perf_cpu_start, before the thread pools count the usable processors) and checked at +// every present for the A/B switches. The rules narrow the processors the process was given (start /affinity, a +// launcher's or a benchmark's pin), never widen them; with both off nothing is set. +static int one_mode = -1, die_mode; +static uint64_t cpu_all, cpu_one, cpu_die, cpu_applied; // cpu_applied: the mask set last, 0 none + static void cpu_placement(void) { - static int mode = -1, applied = -1; - static uint64_t all, one; - if (mode < 0) { - mode = rt_knob("ONE_PER_CORE", "0"); - int cores = plat_cpu_cores(&all, &one); - if (mode && one && one != all) - rt_log("cpu: %d cores, %d logical processors: one per core%s", cores, __builtin_popcountll(all), - mode == 2 ? " in the B blocks" : ""); + if (one_mode < 0) { + one_mode = rt_knob("ONE_PER_CORE", "0"); + die_mode = rt_knob("CACHE_DIE", "1"); + int cores = plat_cpu_cores(&cpu_all, &cpu_one); + uint32_t kb = 0; + cpu_die = plat_cpu_cache_die(&kb); + if (one_mode && cpu_one && cpu_one != cpu_all) + rt_log("cpu: %d cores, %d logical processors: one per core%s", cores, __builtin_popcountll(cpu_all), + one_mode == 2 ? " in the B blocks" : ""); else - mode = 0; + one_mode = 0; + if (cpu_die && cpu_die != cpu_all) + rt_log("cpu: logical processors %#llx of %#llx share the largest L3 cache (%u MiB)%s", + (unsigned long long)cpu_die, (unsigned long long)cpu_all, kb >> 10, + !die_mode ? "; cache_die = 0: not used" : die_mode == 2 ? ": the process runs there in the B blocks" + : ": the process runs there"); + if (!cpu_die || cpu_die == cpu_all) die_mode = 0; } - int on = RT_KNOB_ON(mode); - if (on == applied) return; - applied = on; - plat_process_affinity(on ? one : all); + uint64_t want = cpu_all; + if (RT_KNOB_ON(die_mode)) want &= cpu_die; + if (RT_KNOB_ON(one_mode)) want &= cpu_one; + if (!want) want = cpu_all; + if (want == (cpu_applied ? cpu_applied : cpu_all)) return; // nothing new; never restricted: as it was given + cpu_applied = want; + if (!plat_process_affinity(want)) rt_log("cpu: could not move the process to logical processors %#llx", (unsigned long long)want); } +void perf_cpu_start(void) { cpu_placement(); } + // NX2NAVI_CRASH_FILTER_GUARD (default 1; 0 off): once a second, the crash report's last-chance exception filter is put // back if a DLL loaded after the start (a GPU driver, an overlay) set its own in its place. Without it, a fault in such // a DLL ends the process with no FATAL line in the log. The first eight replacements are logged with the module. diff --git a/runtime/platform/linux.c b/runtime/platform/linux.c index 8c1ed45a..60cf2c6d 100644 --- a/runtime/platform/linux.c +++ b/runtime/platform/linux.c @@ -155,6 +155,40 @@ int plat_cpu_cores(uint64_t *all, uint64_t *first) { return cores; } +// The L3 cache of each logical processor: the cache/index of /sys/devices/system/cpu/cpu whose level is 3, its +// size in KiB ("98304K"). +uint64_t plat_cpu_cache_die(uint32_t *l3_kb) { + cpu_set_t set; + *l3_kb = 0; + if (sched_getaffinity(0, sizeof set, &set)) return 0; + uint64_t die = 0, in_set = 0; + uint32_t big = 0, small = ~0u; + for (int i = 0; i < 64; i++) { + uint32_t kb = 0; + for (int k = 0; k < 8 && !kb; k++) { + char path[96], text[32] = ""; + uint64_t level = 0; + snprintf(path, sizeof path, "/sys/devices/system/cpu/cpu%d/cache/index%d/level", i, k); + if (!read_u64_file(path, &level)) break; + if (level != 3) continue; + snprintf(path, sizeof path, "/sys/devices/system/cpu/cpu%d/cache/index%d/size", i, k); + FILE *f = fopen(path, "r"); + if (f) { + if (fgets(text, sizeof text, f)) kb = (uint32_t)strtoul(text, NULL, 10) * (strchr(text, 'M') ? 1024 : 1); + fclose(f); + } + } + if (!kb) continue; + if (kb > big) big = kb, die = 0; + if (kb == big) die |= 1ull << i; + if (kb < small) small = kb; + if (CPU_ISSET(i, &set)) in_set |= 1ull << i; + } + if (!die || big == small) return 0; + *l3_kb = big; + return die & in_set; +} + // Linux has no process-wide affinity: each thread of /proc/self/task gets the mask, and threads started later inherit // it from the thread that starts them. int plat_process_affinity(uint64_t mask) { diff --git a/runtime/platform/platform.h b/runtime/platform/platform.h index 8ad23a8e..09e9dad3 100644 --- a/runtime/platform/platform.h +++ b/runtime/platform/platform.h @@ -41,6 +41,10 @@ uint64_t plat_thread_cpu_ns(uint32_t tid); // CPU time of the thread with id ti // The logical processors the process may use (bit n = processor n, the first 64), into *all, and the lowest of each // physical core among them into *first; returns the number of physical cores, 0 when unknown. int plat_cpu_cores(uint64_t *all, uint64_t *first); +// The logical processors of the process's set (the first 64) that share the largest L3 cache, when the L3 caches +// are not all the same size: the die with AMD's 3D V-Cache on a two-die X3D processor. 0 when they are (one die, or +// equal dies) or the topology is unknown. *l3_kb: that cache's size. +uint64_t plat_cpu_cache_die(uint32_t *l3_kb); int plat_process_affinity(uint64_t mask); // every thread of the process to the processors in mask; 1 on success // Interrupting a thread (kernel.c's asynchronous preemption). A thread makes its own handle; another thread stops it diff --git a/runtime/platform/win32.c b/runtime/platform/win32.c index 340fbec7..26f23de1 100644 --- a/runtime/platform/win32.c +++ b/runtime/platform/win32.c @@ -95,6 +95,32 @@ int plat_cpu_cores(uint64_t *all, uint64_t *first) { return cores; } +uint64_t plat_cpu_cache_die(uint32_t *l3_kb) { + DWORD_PTR proc, sys; + *l3_kb = 0; + if (!GetProcessAffinityMask(GetCurrentProcess(), &proc, &sys)) return 0; + DWORD len = 0; + GetLogicalProcessorInformationEx(RelationCache, NULL, &len); + SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *buf = len ? malloc(len) : NULL; + uint64_t die = 0; + DWORD big = 0, small = ~0u; + if (buf && GetLogicalProcessorInformationEx(RelationCache, buf, &len)) + for (DWORD off = 0; off < len;) { + SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *e = (void *)((char *)buf + off); + const CACHE_RELATIONSHIP *c = &e->Cache; + if (c->Level == 3 && c->GroupMask.Group == 0) { // every L3, also those outside the process's set + if (c->CacheSize > big) big = c->CacheSize, die = 0; + if (c->CacheSize == big) die |= c->GroupMask.Mask; + if (c->CacheSize < small) small = c->CacheSize; + } + off += e->Size; + } + free(buf); + if (!die || big == small) return 0; + *l3_kb = big >> 10; + return die & proc; +} + int plat_process_affinity(uint64_t mask) { return SetProcessAffinityMask(GetCurrentProcess(), (DWORD_PTR)mask) != 0; } uint64_t plat_thread_cpu_ns(uint32_t tid) {