mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-17 04:59:57 -04:00
Give the recording thread a fast core and its sibling on Linux, and pick the thread that does the recording.
On Linux recorder_core reserved the last logical processor of the mask (an efficiency core on a hybrid CPU) without its sibling, for the first thread to call vk_exec_record, and the first process-wide affinity call took it back. plat_cpu_reserve there now takes the fast core that clocks highest with both of its logical processors (none when fewer than four cores are fast), threads the holder starts get the other processors, plat_process_affinity leaves the core with its holder, and a sweep every 300 presents moves threads that inherited the core and gives it back once the holder has ended. Shared code, so Windows changes too: the priority and the core go to the first thread that has spent half a second in vk_exec_record (vk_exec.c rec_claim) instead of the first to call it, and the log names the logical processors. Pull request #358 by mooki000, taken as it is. The Windows files compile; nothing was run, and the Linux code is not compiled here. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
@@ -101,6 +101,11 @@ static void alt_stack(void) {
|
||||
sigaltstack(&ss, NULL);
|
||||
}
|
||||
|
||||
// Set in the thread that took a core in plat_cpu_reserve: the processors the threads it starts run on (not that core).
|
||||
// Threads inherit the mask of the thread that starts them, which for those would be the reserved core alone.
|
||||
static __thread cpu_set_t cpu_rest;
|
||||
static __thread int cpu_rest_set;
|
||||
|
||||
static void *thread_main(void *p) {
|
||||
PlatThread *t = p;
|
||||
alt_stack();
|
||||
@@ -124,6 +129,7 @@ PlatThread *plat_thread_new_ex(PlatThreadFn fn, void *arg, const char *name, siz
|
||||
pthread_attr_init(&a);
|
||||
// The stack is mapped lazily, as Windows reserves it: only the pages a thread touches cost memory.
|
||||
pthread_attr_setstacksize(&a, stack ? stack : PLAT_STACK_DEFAULT);
|
||||
if (cpu_rest_set) pthread_attr_setaffinity_np(&a, sizeof cpu_rest, &cpu_rest);
|
||||
int e = pthread_create(&t->t, &a, thread_main, t);
|
||||
pthread_attr_destroy(&a);
|
||||
if (e) { free(t); return NULL; }
|
||||
@@ -190,19 +196,37 @@ uint64_t plat_cpu_cache_die(uint32_t *l3_kb) {
|
||||
return die & in_set;
|
||||
}
|
||||
|
||||
// The core plat_cpu_reserve took: its processors and the thread that holds them (0 before the call), and the
|
||||
// processors every other thread got then or at the last plat_process_affinity. cpu_lock orders the functions that set
|
||||
// the masks of every thread.
|
||||
static PlatMutex cpu_lock = PLAT_MUTEX_INIT;
|
||||
static cpu_set_t cpu_taken, cpu_others;
|
||||
static pid_t cpu_taker;
|
||||
|
||||
static void cpu_clear(cpu_set_t *set, const cpu_set_t *drop) {
|
||||
for (int i = 0; i < CPU_SETSIZE; i++)
|
||||
if (CPU_ISSET(i, drop)) CPU_CLR(i, 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.
|
||||
// it from the thread that starts them. The core plat_cpu_reserve took stays out of the mask of every thread but its
|
||||
// holder, which keeps the mask it has.
|
||||
int plat_process_affinity(uint64_t mask) {
|
||||
cpu_set_t set;
|
||||
CPU_ZERO(&set);
|
||||
for (int i = 0; i < 64; i++)
|
||||
if (mask >> i & 1) CPU_SET(i, &set);
|
||||
DIR *d = opendir("/proc/self/task");
|
||||
if (!d) return 0;
|
||||
int ok = 1;
|
||||
for (struct dirent *e; (e = readdir(d));)
|
||||
if (e->d_name[0] != '.' && sched_setaffinity((pid_t)atoi(e->d_name), sizeof set, &set)) ok = 0;
|
||||
closedir(d);
|
||||
plat_mutex_lock(&cpu_lock);
|
||||
if (cpu_taker) cpu_clear(&set, &cpu_taken);
|
||||
DIR *d = CPU_COUNT(&set) ? opendir("/proc/self/task") : NULL;
|
||||
int ok = d != NULL;
|
||||
if (ok && cpu_taker) cpu_others = set;
|
||||
for (struct dirent *e; d && (e = readdir(d));) {
|
||||
pid_t tid = (pid_t)atoi(e->d_name);
|
||||
if (tid > 0 && tid != cpu_taker && sched_setaffinity(tid, sizeof set, &set)) ok = 0;
|
||||
}
|
||||
if (d) closedir(d);
|
||||
plat_mutex_unlock(&cpu_lock);
|
||||
return ok;
|
||||
}
|
||||
|
||||
@@ -427,27 +451,185 @@ int plat_thread_join_ns(PlatThread *t, uint64_t ns) {
|
||||
// The thread keeps using t->fn and t->arg, so the struct stays allocated (a few bytes per detached thread).
|
||||
void plat_thread_detach(PlatThread *t) { pthread_detach(t->t); }
|
||||
|
||||
// The calling thread gets the last processor of its affinity mask to itself: every other thread of the process now
|
||||
// running loses that processor (threads they start later inherit their masks), and the caller keeps only it. Returns
|
||||
// 1, or 0 when the mask has fewer than three processors or the caller's mask cannot be set.
|
||||
int plat_cpu_reserve(void) {
|
||||
// ---- a core for the recording thread (plat_cpu_reserve)
|
||||
|
||||
// The processors a sysfs CPU list ("0-15", "0,2,4-7") names, added to *set; 0 when s is not such a list.
|
||||
static int cpu_list_parse(const char *s, cpu_set_t *set) {
|
||||
for (;;) {
|
||||
char *e;
|
||||
long a = strtol(s, &e, 10), b = a;
|
||||
if (e == s || a < 0 || a >= CPU_SETSIZE) return 0;
|
||||
if (*e == '-') {
|
||||
s = e + 1;
|
||||
b = strtol(s, &e, 10);
|
||||
if (e == s || b < a || b >= CPU_SETSIZE) return 0;
|
||||
}
|
||||
for (long i = a; i <= b; i++) CPU_SET((int)i, set);
|
||||
if (*e != ',') return *e == '\n' || !*e;
|
||||
s = e + 1;
|
||||
}
|
||||
}
|
||||
|
||||
static int cpu_list_file(const char *path, cpu_set_t *set) {
|
||||
char text[512];
|
||||
FILE *f = fopen(path, "r");
|
||||
if (!f) return 0;
|
||||
size_t n = fread(text, 1, sizeof text - 1, f);
|
||||
fclose(f);
|
||||
text[n] = 0;
|
||||
CPU_ZERO(set);
|
||||
return cpu_list_parse(text, set);
|
||||
}
|
||||
|
||||
// cpu<n>/<file> under root/system/cpu holds one number per processor (cpu_capacity, cpufreq/cpuinfo_max_freq). When
|
||||
// the processors of `in` differ in it by more than a tenth, *out gets the ones within a tenth of the highest and the
|
||||
// result is 1; otherwise 0 and *out stays as it was. A processor without the file counts as the lowest.
|
||||
static int cpus_highest_by(const char *root, const char *file, const cpu_set_t *in, cpu_set_t *out) {
|
||||
uint64_t v[CPU_SETSIZE], top = 0, low = UINT64_MAX;
|
||||
for (int i = 0; i < CPU_SETSIZE; i++) {
|
||||
char path[160];
|
||||
v[i] = 0;
|
||||
if (!CPU_ISSET(i, in)) continue;
|
||||
snprintf(path, sizeof path, "%s/system/cpu/cpu%d/%s", root, i, file);
|
||||
if (!read_u64_file(path, &v[i])) v[i] = 0;
|
||||
if (v[i] > top) top = v[i];
|
||||
if (v[i] < low) low = v[i];
|
||||
}
|
||||
if (low * 10 >= top * 9) return 0;
|
||||
CPU_ZERO(out);
|
||||
for (int i = 0; i < CPU_SETSIZE; i++)
|
||||
if (CPU_ISSET(i, in) && v[i] * 10 >= top * 9) CPU_SET(i, out);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// The processors of `all` on the fastest kind of core, and what makes them so. A hybrid Intel CPU lists its performance
|
||||
// cores in root/cpu_core/cpus; for the others, cores of different capacity or top clock (a tenth apart or less counts
|
||||
// as the same) keep the highest; a CPU with one kind of core keeps all of `all`.
|
||||
static const char *cpus_fast(const char *root, const cpu_set_t *all, cpu_set_t *fast) {
|
||||
cpu_set_t perf;
|
||||
char path[160];
|
||||
snprintf(path, sizeof path, "%s/cpu_core/cpus", root);
|
||||
if (cpu_list_file(path, &perf)) {
|
||||
CPU_AND(fast, &perf, all);
|
||||
if (CPU_COUNT(fast)) return "a performance core";
|
||||
}
|
||||
if (cpus_highest_by(root, "cpu_capacity", all, fast)) return "a core of the highest capacity";
|
||||
if (cpus_highest_by(root, "cpufreq/cpuinfo_max_freq", all, fast)) return "a core of the highest clock";
|
||||
*fast = *all;
|
||||
return "a core";
|
||||
}
|
||||
|
||||
// The processors plat_cpu_reserve takes into *mine: every processor of `all` on the fast core (cpus_fast) that clocks
|
||||
// highest, the highest-numbered one when several do. A core with a processor outside the fast ones is no fast core.
|
||||
// Returns how many processors, 0 when fewer than four cores are fast: one of them is then too much to lose. *kind
|
||||
// names what makes the core fast.
|
||||
static int cpu_pick_core(const char *root, const cpu_set_t *all, cpu_set_t *mine, const char **kind) {
|
||||
cpu_set_t fast;
|
||||
*kind = cpus_fast(root, all, &fast);
|
||||
int cores = 0;
|
||||
uint64_t best = 0;
|
||||
for (int i = 0; i < CPU_SETSIZE; i++) {
|
||||
char path[160];
|
||||
cpu_set_t sib, in;
|
||||
uint64_t freq = 0;
|
||||
if (!CPU_ISSET(i, &fast)) continue;
|
||||
snprintf(path, sizeof path, "%s/system/cpu/cpu%d/topology/thread_siblings_list", root, i);
|
||||
if (!cpu_list_file(path, &sib)) CPU_ZERO(&sib);
|
||||
CPU_SET(i, &sib);
|
||||
CPU_AND(&sib, &sib, all);
|
||||
int first = 0;
|
||||
while (!CPU_ISSET(first, &sib)) first++;
|
||||
CPU_AND(&in, &sib, &fast);
|
||||
if (first != i || !CPU_EQUAL(&in, &sib)) continue; // counted at its first processor, or not fast
|
||||
snprintf(path, sizeof path, "%s/system/cpu/cpu%d/cpufreq/cpuinfo_max_freq", root, i);
|
||||
read_u64_file(path, &freq);
|
||||
if (++cores == 1 || freq >= best) best = freq, *mine = sib;
|
||||
}
|
||||
return cores < 4 ? 0 : CPU_COUNT(mine);
|
||||
}
|
||||
|
||||
// The calling thread gets a fast core to itself (cpu_pick_core): every other thread of the process now running loses
|
||||
// the core's processors, and the caller keeps only them. Threads the caller starts later through plat_thread_new_ex
|
||||
// get the others; a thread started by another one inherits that one's mask, which no longer has the core.
|
||||
// plat_process_affinity leaves the core with the caller. Returns the processors reserved, 0 when no core is fast
|
||||
// enough or the caller's mask cannot be set; *which (when not NULL) receives them as text, such as "10,11 of a
|
||||
// performance core".
|
||||
int plat_cpu_reserve(char *which, size_t size) {
|
||||
cpu_set_t all, mine, rest;
|
||||
if (sched_getaffinity(0, sizeof all, &all) || CPU_COUNT(&all) < 3) return 0;
|
||||
int last = -1;
|
||||
for (int i = CPU_SETSIZE - 1; i >= 0 && last < 0; i--)
|
||||
if (CPU_ISSET(i, &all)) last = i;
|
||||
rest = all;
|
||||
CPU_CLR(last, &rest);
|
||||
CPU_ZERO(&mine);
|
||||
CPU_SET(last, &mine);
|
||||
const char *kind;
|
||||
pid_t me = (pid_t)syscall(SYS_gettid);
|
||||
if (which && size) which[0] = 0;
|
||||
if (sched_getaffinity(0, sizeof all, &all) || !cpu_pick_core("/sys/devices", &all, &mine, &kind)) return 0;
|
||||
CPU_XOR(&rest, &all, &mine);
|
||||
plat_mutex_lock(&cpu_lock);
|
||||
if (sched_setaffinity(0, sizeof mine, &mine)) {
|
||||
plat_mutex_unlock(&cpu_lock);
|
||||
return 0;
|
||||
}
|
||||
cpu_rest = rest, cpu_rest_set = 1;
|
||||
cpu_taken = mine, cpu_taker = me, cpu_others = rest;
|
||||
// A thread started during a pass by one it has not reached yet is found by the next.
|
||||
for (int pass = 0, moved = 1; moved && pass < 3; pass++) {
|
||||
DIR *d = opendir("/proc/self/task");
|
||||
moved = 0;
|
||||
for (struct dirent *e; d && (e = readdir(d));) {
|
||||
pid_t tid = (pid_t)atoi(e->d_name);
|
||||
cpu_set_t cur, now;
|
||||
if (tid <= 0 || tid == me || sched_getaffinity(tid, sizeof cur, &cur)) continue;
|
||||
now = cur;
|
||||
cpu_clear(&now, &mine);
|
||||
if (CPU_EQUAL(&now, &cur)) continue;
|
||||
if (!CPU_COUNT(&now)) now = rest; // it was pinned to the core alone
|
||||
if (!sched_setaffinity(tid, sizeof now, &now)) moved = 1;
|
||||
}
|
||||
if (d) closedir(d);
|
||||
}
|
||||
plat_mutex_unlock(&cpu_lock);
|
||||
if (which && size) {
|
||||
size_t n = 0;
|
||||
for (int i = 0; i < CPU_SETSIZE && n < size; i++)
|
||||
if (CPU_ISSET(i, &mine)) n += (size_t)snprintf(which + n, size - n, "%s%d", n ? "," : "", i);
|
||||
if (n < size) snprintf(which + n, size - n, " of %s", kind);
|
||||
}
|
||||
return CPU_COUNT(&mine);
|
||||
}
|
||||
|
||||
// A thread the holder starts by other means than plat_thread_new_ex (a library's audio thread, a driver's worker)
|
||||
// inherits its mask, the core alone, after plat_cpu_reserve's sweep ran, and plat_process_affinity seldom runs again:
|
||||
// each thread whose mask lies within the core, the holder aside, gets the others' processors here. A thread with
|
||||
// anything beside the core in its mask keeps it. Once the holder has ended, nothing would use the core, so it goes back:
|
||||
// the threads with the others' mask get its processors again and the reservation ends. Returns the threads moved, -1
|
||||
// when the core went back.
|
||||
int plat_cpu_reserve_sweep(void) {
|
||||
plat_mutex_lock(&cpu_lock);
|
||||
if (!cpu_taker) {
|
||||
plat_mutex_unlock(&cpu_lock);
|
||||
return 0;
|
||||
}
|
||||
char path[48];
|
||||
snprintf(path, sizeof path, "/proc/self/task/%d", (int)cpu_taker);
|
||||
int ended = access(path, F_OK) != 0, moved = 0;
|
||||
cpu_set_t back;
|
||||
CPU_OR(&back, &cpu_others, &cpu_taken);
|
||||
DIR *d = opendir("/proc/self/task");
|
||||
for (struct dirent *e; d && (e = readdir(d));) {
|
||||
pid_t tid = (pid_t)atoi(e->d_name);
|
||||
if (tid > 0 && tid != me) sched_setaffinity(tid, sizeof rest, &rest);
|
||||
cpu_set_t cur, in;
|
||||
if (tid <= 0 || tid == cpu_taker || sched_getaffinity(tid, sizeof cur, &cur)) continue;
|
||||
if (ended) {
|
||||
if (CPU_EQUAL(&cur, &cpu_others) && !sched_setaffinity(tid, sizeof back, &back)) moved++;
|
||||
continue;
|
||||
}
|
||||
CPU_AND(&in, &cur, &cpu_taken);
|
||||
if (CPU_COUNT(&cur) && CPU_EQUAL(&in, &cur) && !sched_setaffinity(tid, sizeof cpu_others, &cpu_others)) moved++;
|
||||
}
|
||||
if (d) closedir(d);
|
||||
return sched_setaffinity(0, sizeof mine, &mine) == 0;
|
||||
if (ended) {
|
||||
cpu_taker = 0;
|
||||
CPU_ZERO(&cpu_taken);
|
||||
}
|
||||
plat_mutex_unlock(&cpu_lock);
|
||||
return ended ? -1 : moved;
|
||||
}
|
||||
|
||||
uint64_t plat_thread_affinity(PlatThread *t, uint64_t mask) {
|
||||
|
||||
@@ -31,10 +31,18 @@ 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);
|
||||
// The calling thread gets a core to itself: the process's other threads are kept off it and the caller on it (Windows:
|
||||
// CPU sets, a performance core; Linux: the last processor of the affinity mask). Returns the logical processors
|
||||
// reserved, 0 when it could not.
|
||||
int plat_cpu_reserve(void);
|
||||
// The calling thread gets a fast core to itself: the process's other threads are kept off it and the caller on it, with
|
||||
// both logical processors of the core when it has two; threads the caller starts later run with the others. Windows
|
||||
// takes the highest-numbered core of the fastest efficiency class (CPU sets). Linux takes the core of a hybrid CPU's
|
||||
// performance cores that clocks highest, else of the highest capacity or clock, and none when fewer than four cores
|
||||
// are that fast. Returns the logical processors reserved, 0 when it could not; *which (when not NULL, size bytes)
|
||||
// receives them as text, such as "10,11" (Linux adds what makes the core fast: "10,11 of a performance core").
|
||||
int plat_cpu_reserve(char *which, size_t size);
|
||||
// Keeps the reserved core reserved: on Linux a thread the holder starts through a library inherits its mask, the core
|
||||
// alone, and gets the others' processors here; once the holder has ended, the core goes back to every thread. Returns
|
||||
// the threads moved, -1 when the core went back. Windows needs none of it (threads without CPU sets of their own run
|
||||
// on the process's default sets, which leave the core out) and returns 0.
|
||||
int plat_cpu_reserve_sweep(void);
|
||||
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
|
||||
uint64_t plat_thread_cpu_ns(uint32_t tid); // CPU time of the thread with id tid (plat_thread_id) so far; 0 unknown
|
||||
@@ -45,7 +53,9 @@ int plat_cpu_cores(uint64_t *all, uint64_t *first);
|
||||
// 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
|
||||
// Every thread of the process to the processors in mask; 1 on success. On Linux the core plat_cpu_reserve took stays
|
||||
// out of the mask for every thread but its holder, which keeps the processors it has.
|
||||
int plat_process_affinity(uint64_t mask);
|
||||
|
||||
// Interrupting a thread (kernel.c's asynchronous preemption). A thread makes its own handle; another thread stops it
|
||||
// wherever it runs, reads where it is, may send it elsewhere, and lets it go on. While it is stopped the caller must not
|
||||
|
||||
@@ -331,8 +331,9 @@ uint64_t plat_thread_affinity(PlatThread *t, uint64_t mask) {
|
||||
// core. The core is the highest-numbered one of the fastest efficiency class (a performance core on a hybrid CPU),
|
||||
// with both its logical processors when it has two; only processors in group 0 and in the process's affinity mask
|
||||
// count. CPU sets are soft: the scheduler may still use the core when nothing else wants it. Returns the logical
|
||||
// processors reserved, 0 when Windows has no CPU sets (before Windows 10) or fewer than two cores qualify.
|
||||
int plat_cpu_reserve(void) {
|
||||
// processors reserved, 0 when Windows has no CPU sets (before Windows 10) or fewer than two cores qualify; *which
|
||||
// (when not NULL, size bytes) receives their numbers, such as "10,11".
|
||||
int plat_cpu_reserve(char *which, size_t size) {
|
||||
typedef BOOL(WINAPI * GetInfoFn)(PSYSTEM_CPU_SET_INFORMATION, ULONG, PULONG, HANDLE, ULONG);
|
||||
typedef BOOL(WINAPI * SetProcFn)(HANDLE, const ULONG *, ULONG);
|
||||
typedef BOOL(WINAPI * SetThreadFn)(HANDLE, const ULONG *, ULONG);
|
||||
@@ -362,6 +363,7 @@ int plat_cpu_reserve(void) {
|
||||
o += s->Size;
|
||||
}
|
||||
ULONG rest[64], mine[8];
|
||||
unsigned mine_cpu[8]; // the logical processor numbers of mine
|
||||
int nrest = 0, nmine = 0;
|
||||
for (ULONG o = 0; o + sizeof(DWORD) * 2 <= len;) {
|
||||
const SYSTEM_CPU_SET_INFORMATION *s = (const void *)(buf + o);
|
||||
@@ -369,7 +371,7 @@ int plat_cpu_reserve(void) {
|
||||
if (s->Type == CpuSetInformation && s->CpuSet.Group == 0 && s->CpuSet.LogicalProcessorIndex < 64 &&
|
||||
(proc >> s->CpuSet.LogicalProcessorIndex & 1)) {
|
||||
if (s->CpuSet.EfficiencyClass == best_class && s->CpuSet.CoreIndex == best_core) {
|
||||
if (nmine < 8) mine[nmine++] = s->CpuSet.Id;
|
||||
if (nmine < 8) mine_cpu[nmine] = s->CpuSet.LogicalProcessorIndex, mine[nmine++] = s->CpuSet.Id;
|
||||
} else if (nrest < 64) {
|
||||
rest[nrest++] = s->CpuSet.Id;
|
||||
}
|
||||
@@ -383,9 +385,17 @@ int plat_cpu_reserve(void) {
|
||||
set_proc(GetCurrentProcess(), NULL, 0); // back to every processor
|
||||
return 0;
|
||||
}
|
||||
if (which && size) {
|
||||
size_t n = 0;
|
||||
which[0] = 0;
|
||||
for (int i = 0; i < nmine && n < size; i++)
|
||||
n += (size_t)snprintf(which + n, size - n, "%s%u", n ? "," : "", mine_cpu[i]);
|
||||
}
|
||||
return nmine;
|
||||
}
|
||||
|
||||
int plat_cpu_reserve_sweep(void) { return 0; }
|
||||
|
||||
uint32_t plat_cpu_count(void) {
|
||||
DWORD n = GetActiveProcessorCount(ALL_PROCESSOR_GROUPS);
|
||||
return n ? (uint32_t)n : 1;
|
||||
|
||||
Reference in New Issue
Block a user