mirror of
https://github.com/valentderah/totk-pc-recomp.git
synced 2026-11-21 12:55:57 +00:00
Merge the host cost fixes: the performance counter is scaled without a 128-bit divide, NVN calls are counted per thread, and nn::fs host functions and NVN memory pool setup give the virtual core up; core diagnostics report waits on owners in host calls
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
6c14dd5937
7 files changed
+77
-20
No files matched your search
+16
-1
@@ -25,6 +25,7 @@ typedef struct {
|
||||
GuestFn fn;
|
||||
uint64_t original; // observers: the guest definition rt_original calls
|
||||
uint64_t calls;
|
||||
int blocking; // runs with the virtual core given up (hostfn_blocking)
|
||||
} Slot;
|
||||
static Slot slots[MAX_HOSTFN];
|
||||
static int nslots;
|
||||
@@ -82,7 +83,7 @@ uint64_t hostfn_register(const char *name, GuestFn fn) {
|
||||
if (nslots == MAX_HOSTFN) rt_fatal("hostfn_register: more than %d host functions", MAX_HOSTFN);
|
||||
uint64_t a = slot_addr(nslots);
|
||||
if (a >= RT_RET_HOST) rt_fatal("hostfn_register: host-function window full");
|
||||
slots[nslots] = (Slot){.name = name, .fn = fn};
|
||||
slots[nslots] = (Slot){.name = name, .fn = fn, .blocking = !strncmp(name, "_ZN2nn2fs", 9)};
|
||||
rt_register_host(a, hostfn_entry);
|
||||
nslots++;
|
||||
return a;
|
||||
@@ -120,6 +121,16 @@ __attribute__((noinline)) static void hostfn_tracked(Cpu *c, GuestFn fn) {
|
||||
if (!n && kern_preempt_pending(t)) kern_host_left(t);
|
||||
}
|
||||
|
||||
// nn::fs: the call waits for the disk and the OS, and on the Switch it is an IPC request that blocks its thread, so
|
||||
// the thread gives its virtual core up for it. TOTK_CORE_DIAG found the game's loader threads (priority 58) holding
|
||||
// a core through CloseFile and CreateCacheStorage while job workers and better threads waited 2-6 ms for it.
|
||||
__attribute__((noinline)) static void hostfn_blocking(Cpu *c, GuestFn fn) {
|
||||
int away = kern_core_away();
|
||||
if (kern_host_track && c->thread) hostfn_tracked(c, fn);
|
||||
else fn(c);
|
||||
kern_core_back(&away);
|
||||
}
|
||||
|
||||
static void hostfn_entry(Cpu *c) {
|
||||
uint64_t i = (c->pc - RT_HOSTFN_BASE) >> 4;
|
||||
if (__builtin_expect((c->pc & 15) || i >= (uint64_t)nslots, 0)) bad_entry(c);
|
||||
@@ -129,6 +140,10 @@ static void hostfn_entry(Cpu *c) {
|
||||
char lr[96];
|
||||
rt_log("host call %s from %s", s->name, ldr_describe(c->x[30] - 4, lr, sizeof lr));
|
||||
}
|
||||
if (s->blocking) {
|
||||
hostfn_blocking(c, s->fn);
|
||||
return;
|
||||
}
|
||||
if (__builtin_expect(kern_host_track && c->thread, 0)) {
|
||||
hostfn_tracked(c, s->fn);
|
||||
return;
|
||||
|
||||
+17
-7
@@ -104,6 +104,7 @@ struct RtThread {
|
||||
uint64_t dg_host_end; // as an owner: the return from the host call or SVC it was in at the request
|
||||
uint64_t dg_pc, dg_lr; // as an owner: guest pc and lr at the checkpoint that gave the core up
|
||||
int dg_host_kick; // as an owner: in a host call or SVC at the request
|
||||
uint32_t dg_svc; // the thread's latest SVC (TOTK_CORE_DIAG: what an owner in a host call is in)
|
||||
struct DgRel { uint64_t kick, host_end, pc, lr; int host, pre, prio; uint32_t h; } dg_rel; // as a waiter: the
|
||||
// owner that released
|
||||
};
|
||||
@@ -627,7 +628,7 @@ static void dg_done(RtThread *t) {
|
||||
while (b < DG_NB - 1 && w >= dg_edge[b]) b++;
|
||||
g_dgw.n[top]++;
|
||||
g_dgw.hist[top][b]++;
|
||||
if (part[DG_GAME] >= 2000000 && g_dg_lines < DG_LONG_LINES) {
|
||||
if ((part[DG_GAME] >= 2000000 || part[DG_HOST] >= 2000000) && g_dg_lines < DG_LONG_LINES) {
|
||||
g_dg_lines++;
|
||||
char a[128], l[128];
|
||||
rt_log("core-diag: wait %.2f ms (game %.2f host %.2f): %#x p%d mask %llx; released by %#x p%d%s at %s lr %s",
|
||||
@@ -639,8 +640,8 @@ static void dg_done(RtThread *t) {
|
||||
memset(&t->dg_rel, 0, sizeof t->dg_rel);
|
||||
}
|
||||
|
||||
// Once per long DG_GAME wait: where the owner's host thread runs (exe RVA). Holding G; the owner is resumed before
|
||||
// anything that could take a lock it holds.
|
||||
// Once per long DG_GAME or DG_HOST wait: where the owner's host thread runs (module and offset), its latest SVC and
|
||||
// its guest lr. Holding G; the owner is resumed before anything that could take a lock it holds.
|
||||
static void dg_sample(RtThread *t) {
|
||||
RtThread *o = t->target;
|
||||
if (!o || o->cur_core < 0 || !o->tid || g_dg_samples >= DG_SAMPLES) return;
|
||||
@@ -658,9 +659,17 @@ static void dg_sample(RtThread *t) {
|
||||
CloseHandle(h);
|
||||
if (!ok) return;
|
||||
g_dg_samples++;
|
||||
uintptr_t base = (uintptr_t)GetModuleHandleW(NULL);
|
||||
rt_log("core-diag: sample owner %#x p%d depth %d rip exe+%#llx (waiter %#x p%d, %.2f ms)", o->handle, o->prio, depth,
|
||||
(unsigned long long)(ctx.Rip - base), t->handle, t->prio, (kern_now_ns() - t->dg_since) / 1e6);
|
||||
HMODULE m = NULL;
|
||||
wchar_t mw[MAX_PATH] = L"?";
|
||||
if (GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
|
||||
(LPCWSTR)(uintptr_t)ctx.Rip, &m) && m)
|
||||
GetModuleFileNameW(m, mw, MAX_PATH);
|
||||
const wchar_t *mn = wcsrchr(mw, L'\\') ? wcsrchr(mw, L'\\') + 1 : mw;
|
||||
char lr[128];
|
||||
rt_log("core-diag: sample owner %#x p%d depth %d rip %ls+%#llx, last svc %#x, guest lr %s (waiter %#x p%d, %s, "
|
||||
"%.2f ms)", o->handle, o->prio, depth, mn, (unsigned long long)(ctx.Rip - (uintptr_t)m), o->dg_svc,
|
||||
o->cur ? ldr_describe(o->cur->x[30], lr, sizeof lr) : "-", t->handle, t->prio,
|
||||
t->dg_cls == DG_HOST ? "host" : "game", (kern_now_ns() - t->dg_since) / 1e6);
|
||||
}
|
||||
|
||||
|
||||
@@ -875,7 +884,7 @@ static void core_take(RtThread *t) {
|
||||
while (t->cur_core < 0) {
|
||||
DWORD ms = INFINITE;
|
||||
if (g_ap_int && ap_try(t) != AP_NONE) ms = 1; // the owner keeps its core: again in a millisecond
|
||||
int sample = g_diag > 0 && !sampled && t->dg_cls == DG_GAME;
|
||||
int sample = g_diag > 0 && !sampled && (t->dg_cls == DG_GAME || t->dg_cls == DG_HOST);
|
||||
if (sample && ms == INFINITE) ms = 2;
|
||||
SleepConditionVariableSRW(&t->cv, &G, ms, 0);
|
||||
if (sample && t->cur_core < 0 && kern_now_ns() - t->dg_since >= 2000000) {
|
||||
@@ -1426,6 +1435,7 @@ static void cv_hook(void) {
|
||||
|
||||
int kern_svc(Cpu *c, uint32_t imm) {
|
||||
uint64_t *x = c->x;
|
||||
if (g_diag > 0 && tcur) tcur->dg_svc = imm;
|
||||
switch (imm) {
|
||||
case 0x08: { // CreateThread(-> w1 handle; x1 entry, x2 arg, x3 stack top, w4 priority, w5 core)
|
||||
int prio = (int)x[4], core = (int)x[5];
|
||||
|
||||
+11
-8
@@ -27,6 +27,7 @@
|
||||
|
||||
#include "cfg.h"
|
||||
#include "host.h"
|
||||
#include "platform/platform.h"
|
||||
|
||||
#define MAX_MODULES 16
|
||||
|
||||
@@ -105,17 +106,19 @@ void rt_udf(Cpu *c, uint64_t pc) {
|
||||
ldr_describe(c->x[30], lr, sizeof lr));
|
||||
}
|
||||
|
||||
// CNTPCT_EL0: the Switch counter runs at 19.2 MHz.
|
||||
// CNTPCT_EL0: the Switch counter runs at 19.2 MHz. The scale is set before main, so no thread reads it half written.
|
||||
static PlatScale tick_scale;
|
||||
|
||||
__attribute__((constructor)) static void tick_scale_init(void) {
|
||||
LARGE_INTEGER f;
|
||||
QueryPerformanceFrequency(&f);
|
||||
tick_scale = plat_scale(19200000u, (uint64_t)f.QuadPart);
|
||||
}
|
||||
|
||||
uint64_t rt_ticks(void) {
|
||||
static uint64_t freq;
|
||||
LARGE_INTEGER t;
|
||||
if (!freq) {
|
||||
LARGE_INTEGER f;
|
||||
QueryPerformanceFrequency(&f);
|
||||
freq = (uint64_t)f.QuadPart;
|
||||
}
|
||||
QueryPerformanceCounter(&t);
|
||||
return (uint64_t)((unsigned __int128)(uint64_t)t.QuadPart * 19200000u / freq);
|
||||
return plat_scaled(tick_scale, (uint64_t)t.QuadPart);
|
||||
}
|
||||
|
||||
// ---- crash reporter
|
||||
|
||||
+14
-1
@@ -24,8 +24,15 @@
|
||||
#define EVENT_MAGIC MAGIC('E', 'V', 'N', 'T')
|
||||
|
||||
// ---------- shared helpers (nvn_api.h)
|
||||
// The call count goes to the shared counter 1024 at a time: the main thread and the job workers all record command
|
||||
// buffers, and one locked add per call kept the counter's cache line moving between their cores (0.8k samples a
|
||||
// fight). The count lags by at most 1023 calls per thread.
|
||||
void nvn_called(uint8_t *seen, const char *name) {
|
||||
__atomic_fetch_add(&gfx_counters.nvn_calls, 1, __ATOMIC_RELAXED);
|
||||
static _Thread_local uint32_t calls;
|
||||
if (++calls == 1024) {
|
||||
__atomic_fetch_add(&gfx_counters.nvn_calls, calls, __ATOMIC_RELAXED);
|
||||
calls = 0;
|
||||
}
|
||||
if (__atomic_load_n(seen, __ATOMIC_RELAXED) || __atomic_exchange_n(seen, 1, __ATOMIC_RELAXED)) return;
|
||||
uint64_t n = __atomic_add_fetch(&gfx_counters.nvn_distinct, 1, __ATOMIC_RELAXED);
|
||||
NVN_LOG("first call %llu: %s", (unsigned long long)n, name);
|
||||
@@ -260,7 +267,11 @@ NVN_API(nvnMemoryPoolBuilderSetStorage) {
|
||||
// void nvnMemoryPoolBuilderSetFlags(NVNmemoryPoolBuilder *b, int flags)
|
||||
NVN_API(nvnMemoryPoolBuilderSetFlags) { ((pool_builder_t *)arg_p(c, 0))->flags = (uint32_t)arg_x(c, 1); }
|
||||
// NVNboolean nvnMemoryPoolInitialize(NVNmemoryPool *p, const NVNmemoryPoolBuilder *b)
|
||||
// Pool setup imports, mirrors and maps memory in the back end (the driver, VirtualAlloc): up to 20 ms on the game's
|
||||
// loader threads, which held a virtual core through it while job workers waited (TOTK_CORE_DIAG). The thread gives
|
||||
// the core up, as for any blocking host call.
|
||||
NVN_API(nvnMemoryPoolInitialize) {
|
||||
NVN_BLOCKING();
|
||||
pool_t *o = arg_p(c, 0);
|
||||
const pool_builder_t *s = arg_p(c, 1);
|
||||
memset(o, 0, sizeof *o);
|
||||
@@ -290,6 +301,7 @@ NVN_API(nvnMemoryPoolInitialize) {
|
||||
}
|
||||
// void nvnMemoryPoolFinalize(NVNmemoryPool *p)
|
||||
NVN_API(nvnMemoryPoolFinalize) {
|
||||
NVN_BLOCKING(); // as nvnMemoryPoolInitialize
|
||||
pool_t *o = arg_p(c, 0);
|
||||
if (o->magic != NVN_POOL_MAGIC) return;
|
||||
if (pool_gpu_visible(o)) index_del(o);
|
||||
@@ -365,6 +377,7 @@ static int check_request(const pool_t *v, const nvn_mapping_t *r, int i) {
|
||||
}
|
||||
// NVNboolean nvnMemoryPoolMapVirtual(NVNmemoryPool *p, int n, const NVNmappingRequest *requests)
|
||||
NVN_API(nvnMemoryPoolMapVirtual) {
|
||||
NVN_BLOCKING(); // as nvnMemoryPoolInitialize
|
||||
pool_t *v = arg_p(c, 0);
|
||||
int n = (int)arg_x(c, 1);
|
||||
const nvn_mapping_t *r = arg_p(c, 2);
|
||||
|
||||
+4
-2
@@ -327,7 +327,8 @@ static void obs_main(Cpu *c) { observe(c, "nnMain"); }
|
||||
static void obs_address_region(Cpu *c) {
|
||||
uint64_t *out = arg_p(c, 0), size = arg_x(c, 1);
|
||||
uint32_t r = (uint32_t)rt_original(c);
|
||||
rt_log("AllocateAddressRegion size %#llx result %#x address %#llx", (unsigned long long)size, r,
|
||||
if (rt_trace & RT_TRACE_HOSTCALL) // 20,000 lines a session otherwise, each a write on a game thread
|
||||
rt_log("AllocateAddressRegion size %#llx result %#x address %#llx", (unsigned long long)size, r,
|
||||
(unsigned long long)(r || !out ? 0 : *out));
|
||||
}
|
||||
|
||||
@@ -335,7 +336,8 @@ static void obs_address_region(Cpu *c) {
|
||||
static void obs_memory_pages(Cpu *c) {
|
||||
uint64_t a = arg_x(c, 0), size = arg_x(c, 1);
|
||||
uint32_t r = (uint32_t)rt_original(c);
|
||||
rt_log("AllocateMemoryPages address %#llx size %#llx result %#x", (unsigned long long)a, (unsigned long long)size, r);
|
||||
if (rt_trace & RT_TRACE_HOSTCALL)
|
||||
rt_log("AllocateMemoryPages address %#llx size %#llx result %#x", (unsigned long long)a, (unsigned long long)size, r);
|
||||
}
|
||||
|
||||
// ==== nn::account (account.c): one offline local user whose Uid survives restarts in <profile>/account.uid
|
||||
|
||||
@@ -23,6 +23,16 @@ uint32_t plat_cpu_count(void); // logical processors
|
||||
|
||||
// ---- time
|
||||
uint64_t plat_now_ns(void); // monotonic, high resolution, arbitrary epoch
|
||||
// t * num / den for a fixed num / den without a 128-bit division per call (__udivti3 took 5% of the game's running
|
||||
// samples through plat_now_ns and rt_ticks): t * q + (t * f >> 64) with q = num / den and f = (num % den) * 2^64 / den.
|
||||
// Monotonic in t and at most one below the exact quotient while t * (num % den) / den fits 64 bits.
|
||||
typedef struct { uint64_t q, f; } PlatScale;
|
||||
static inline PlatScale plat_scale(uint64_t num, uint64_t den) {
|
||||
return (PlatScale){num / den, (uint64_t)(((unsigned __int128)(num % den) << 64) / den)};
|
||||
}
|
||||
static inline uint64_t plat_scaled(PlatScale s, uint64_t t) {
|
||||
return t * s.q + (uint64_t)(((unsigned __int128)t * s.f) >> 64);
|
||||
}
|
||||
void plat_sleep_ns(uint64_t ns); // returns no earlier than ns later; overshoot well under 1 ms
|
||||
|
||||
// ---- locks (not recursive) and condition variables; zero-initialised storage is a valid unlocked lock and an empty
|
||||
|
||||
@@ -78,10 +78,14 @@ static uint64_t qpc_freq(void) {
|
||||
return freq;
|
||||
}
|
||||
|
||||
static PlatScale qpc_ns; // set before main, so no thread reads it half written
|
||||
|
||||
__attribute__((constructor)) static void qpc_ns_init(void) { qpc_ns = plat_scale(1000000000u, qpc_freq()); }
|
||||
|
||||
uint64_t plat_now_ns(void) {
|
||||
LARGE_INTEGER t;
|
||||
QueryPerformanceCounter(&t);
|
||||
return (uint64_t)((unsigned __int128)(uint64_t)t.QuadPart * 1000000000u / qpc_freq());
|
||||
return plat_scaled(qpc_ns, (uint64_t)t.QuadPart);
|
||||
}
|
||||
|
||||
// A high-resolution waitable timer per sleeping thread (Windows 10 1803+; one handle per thread, kept until exit)
|
||||
|
||||
Reference in new issue
Block a user