Run the game on the die with the larger L3 cache on processors whose dies differ (AMD's two-die X3D processors).

Windows places a game on the V-Cache die only when it recognizes the exe as a game, and it does not know this one, so the threads were spread over both dies by frequency preference. cache_die = 1 (the default) narrows the process to the logical processors that share the largest L3; it never widens the set the process was given, and does nothing where every L3 has the same size. The recorder's reserved core is then chosen inside that set.

Co-Authored-By: Claude Fable 5.2 <[EMAIL]>
This commit is contained in:
ordinarybob
2026-11-09 20:08:17 -06:00
committed by chrg6
parent c9942578e1
commit 662a6e88ee
8 changed files with 112 additions and 14 deletions

View File

@@ -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<k> of /sys/devices/system/cpu/cpu<n> 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) {

View File

@@ -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

View File

@@ -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) {