mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-15 23:39: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:
@@ -62,8 +62,12 @@
|
||||
# free_cores = 0
|
||||
# Windows timer resolution of 1 ms for steadier frame pacing (0 = the Windows default).
|
||||
# timer_1ms = 1
|
||||
# The thread that records the GPU work keeps the highest scheduling priority (recorder_prio), and a core of its own that
|
||||
# the process's other threads stay off (recorder_core: auto = with 12 or more logical processors, 1 always, 0 never).
|
||||
# The thread that records the GPU work (the first to spend half a second doing it, not the one that submits first) keeps
|
||||
# the highest scheduling priority (recorder_prio), and a core of its own that the process's other threads stay off
|
||||
# (recorder_core: auto = with 12 or more logical processors, 1 always, 0 never). That is a fast core, with both of its
|
||||
# logical processors: on Windows the highest-numbered core of the fastest efficiency class; on Linux the performance
|
||||
# core that clocks highest on a hybrid CPU (else the core of the highest capacity or clock), and none when fewer than
|
||||
# four cores are that fast. The log names the thread and its logical processors.
|
||||
# recorder_prio = 1
|
||||
# recorder_core = auto
|
||||
# Submissions handed to the GPU queue together in one call, 1 to 16 (1 = each one at once).
|
||||
|
||||
@@ -207,7 +207,7 @@ static const CfgMeta metas[] = {
|
||||
// dynamic resolution (dynres.c): the scene follows the GPU time toward DYN_RES_TARGET milliseconds
|
||||
{"DYN_RES", "0", "0|1", LIVE}, {"DYN_RES_TARGET", "16.7", "1..100", LIVE}, {"DYN_RES_STEP", "45", "8..600", LIVE},
|
||||
// the recording thread: a core of its own (auto: with 12 or more logical processors) and the highest priority
|
||||
// (vk_exec.c vk_exec_record)
|
||||
// (vk_exec.c rec_claim)
|
||||
{"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},
|
||||
|
||||
@@ -540,6 +540,21 @@ static void crash_filter_guard(uint64_t frame) {
|
||||
}
|
||||
}
|
||||
|
||||
// A thread the recording thread starts through a library inherits its reserved core, and the core would stay reserved
|
||||
// after the recording thread ended (plat_cpu_reserve_sweep): every RESERVE_SWEEP presents both are put right.
|
||||
#define RESERVE_SWEEP 300
|
||||
static void reserve_sweep(uint64_t frame) {
|
||||
static int logged;
|
||||
if (frame % RESERVE_SWEEP) return;
|
||||
int n = plat_cpu_reserve_sweep();
|
||||
if (!n || logged >= 8) return;
|
||||
logged++;
|
||||
if (n < 0) rt_log("cpu: frame %llu: the recording thread has ended; its core is open to every thread again",
|
||||
(unsigned long long)frame);
|
||||
else rt_log("cpu: frame %llu: %d thread%s started from the recording thread had its core alone; now on the other "
|
||||
"processors", (unsigned long long)frame, n, n > 1 ? "s" : "");
|
||||
}
|
||||
|
||||
void perf_present(uint64_t frame) {
|
||||
static uint64_t last, sum, max, n, win_start, ab_b;
|
||||
crash_filter_guard(frame);
|
||||
@@ -551,6 +566,7 @@ void perf_present(uint64_t frame) {
|
||||
uint64_t rep = ft_frame(frame, now, d); // d is 0 on the first present: it only starts the period
|
||||
ab_b += rt_ab();
|
||||
cpu_placement();
|
||||
reserve_sweep(frame);
|
||||
if (__atomic_load_n(&overlay, __ATOMIC_RELAXED)) // the overlay's only cost while it is off: this branch
|
||||
perfmon_frame(now, d, frame_ns, rep, __atomic_load_n(&counts[PERF_CNT_TEX_UP], __ATOMIC_RELAXED));
|
||||
kern_core_diag_frame(frame, d);
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -1227,39 +1227,46 @@ static const op_hdr *walk(VkExec *ex, const op_hdr *h, int depth) {
|
||||
}
|
||||
}
|
||||
|
||||
// The replay records on the thread the game submitted from, and the kernel schedules that like any other guest
|
||||
// thread: a preemption in the middle of a part leaves the submission late for the frame it belongs to (Kokiri
|
||||
// Forest: 274 of 2,737 frames had a late submit). The recording thread therefore keeps the highest priority so the
|
||||
// scheduler prefers it over the guest's other cores (NX2NAVI_RECORDER_PRIO=0 leaves the guest's priorities alone) and
|
||||
// gets a core of its own (plat_cpu_reserve): every other thread of the process, the guest's cores, the audio, the
|
||||
// shader and pipeline workers, is kept off it, so nothing of this process preempts the recording. That core is then
|
||||
// lost to the rest, so NX2NAVI_RECORDER_CORE=auto (the default) reserves it only with 12 or more logical processors;
|
||||
// 1 always, 0 never. Linux reserves none with fewer than four fast cores.
|
||||
// The recording thread is not the first to call vk_exec_record: the game's main thread submits first and goes on
|
||||
// submitting a little at a time, while another guest thread does the work (Kokiri Forest: that thread spent 21.9 of
|
||||
// the 22.5 s the replay took in a minute and a half, the first one 0.5 s). The first thread to have spent
|
||||
// REC_CLAIM_NS in vk_exec_record is the one that keeps both.
|
||||
#define REC_CLAIM_NS 500000000ull
|
||||
static _Thread_local uint64_t rec_spent_ns; // time this thread has spent in vk_exec_record
|
||||
|
||||
static void rec_claim(void) {
|
||||
static int done;
|
||||
if (done) return;
|
||||
done = 1;
|
||||
rt_log("vk: thread %u is the recording thread (the first to spend %.1f s in it)", plat_thread_id(),
|
||||
REC_CLAIM_NS / 1e9);
|
||||
const char *e = cfg_get("NX2NAVI_RECORDER_PRIO");
|
||||
if (!(e && !strcmp(e, "0"))) {
|
||||
plat_thread_priority(PLAT_PRIO_HIGHEST);
|
||||
rt_log("vk: the recording thread keeps the highest scheduling priority (NX2NAVI_RECORDER_PRIO=0 turns it off)");
|
||||
}
|
||||
const char *rc = cfg_value("NX2NAVI_RECORDER_CORE");
|
||||
unsigned ncpu = plat_cpu_usable();
|
||||
if (!strcmp(rc, "1") || (!strcmp(rc, "auto") && ncpu >= 12)) {
|
||||
char which[96];
|
||||
int n = plat_cpu_reserve(which, sizeof which);
|
||||
if (n) rt_log("vk: the recording thread has a core of its own: logical processor%s %s (%d of %u; "
|
||||
"NX2NAVI_RECORDER_CORE=0 turns it off)", n > 1 ? "s" : "", which, n, ncpu);
|
||||
else rt_log("vk: no core was reserved for the recording thread (too few fast cores, or the system refused it; "
|
||||
"NX2NAVI_RECORDER_CORE=0 stops the attempt)");
|
||||
}
|
||||
}
|
||||
|
||||
int vk_exec_record(VkCommandBuffer cb, const uint64_t *handles, int n, uint64_t fence) {
|
||||
// The replay records on the thread the game submitted from, and the kernel schedules that like any other guest
|
||||
// thread: a preemption in the middle of a part leaves the submission late for the frame it belongs to (Kokiri
|
||||
// Forest: 274 of 2,737 frames had a late submit). Recording keeps the highest priority so the scheduler prefers it
|
||||
// over the guest's other cores; NX2NAVI_RECORDER_PRIO=0 leaves the guest's priorities alone.
|
||||
static int prio_done;
|
||||
static uint32_t rec_thread; // the thread that got the priority and the core
|
||||
if (!prio_done) {
|
||||
prio_done = 1;
|
||||
const char *e = cfg_get("NX2NAVI_RECORDER_PRIO");
|
||||
if (!(e && !strcmp(e, "0"))) {
|
||||
plat_thread_priority(PLAT_PRIO_HIGHEST);
|
||||
rt_log("vk: the recording thread keeps the highest scheduling priority (NX2NAVI_RECORDER_PRIO=0 turns it off)");
|
||||
}
|
||||
// The recorder also gets a core of its own (plat_cpu_reserve): every other thread of the process, the guest's
|
||||
// cores, the audio, the shader and pipeline workers, is kept off it, so nothing of this process preempts the
|
||||
// recording. That core is then lost to the rest, so NX2NAVI_RECORDER_CORE=auto (the default) reserves it only
|
||||
// with 12 or more logical processors; 1 always, 0 never.
|
||||
const char *rc = cfg_value("NX2NAVI_RECORDER_CORE");
|
||||
unsigned ncpu = plat_cpu_usable();
|
||||
if (!strcmp(rc, "1") || (!strcmp(rc, "auto") && ncpu >= 12)) {
|
||||
int n = plat_cpu_reserve();
|
||||
if (n) rt_log("vk: the recording thread has a core of its own (%d logical processor%s of %u; "
|
||||
"NX2NAVI_RECORDER_CORE=0 turns it off)", n, n > 1 ? "s" : "", ncpu);
|
||||
else rt_log("vk: could not reserve a core for the recording thread (NX2NAVI_RECORDER_CORE)");
|
||||
}
|
||||
rec_thread = plat_thread_id();
|
||||
}
|
||||
if (rec_thread && plat_thread_id() != rec_thread) { // the priority and the core stay with the first thread
|
||||
static int said;
|
||||
if (!said++) rt_log("vk: a submission is recorded on another thread than the first one; it runs without the "
|
||||
"recorder's priority and core");
|
||||
}
|
||||
uint64_t in_ns = kern_now_ns();
|
||||
perf_lock_timing_here(); // NX2NAVI_LOCK_TIME
|
||||
if (prof_replay_tid != plat_thread_id()) perf_replay_thread(); // the profiler follows the thread replaying now
|
||||
perf_rec_waits = rec_wait_totals;
|
||||
@@ -1352,5 +1359,7 @@ int vk_exec_record(VkCommandBuffer cb, const uint64_t *handles, int n, uint64_t
|
||||
evo_held = evo_resumed = evo_forced = evo_switches = 0;
|
||||
n_subs = n_parts = n_idle_parts = n_pass_splits = 0;
|
||||
}
|
||||
rec_spent_ns += kern_now_ns() - in_ns;
|
||||
if (rec_spent_ns >= REC_CLAIM_NS) rec_claim();
|
||||
return ex->recorded - part_start; // what the last part holds
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user