mirror of
https://github.com/valentderah/totk-pc-recomp.git
synced 2026-11-21 21:05:56 +00:00
Give the virtual core up in nn::fs host functions and in NVN memory pool setup, as the Switch blocks those threads in IPC: the game's loader threads held a core through CloseFile, CreateCacheStorage and nvnMemoryPoolInitialize (up to 20 ms) while job workers waited. Core diagnostics now also report waits on an owner in a host call, with its module, offset, latest SVC and guest lr, and the AllocateMemoryPages and AllocateAddressRegion lines need TOTK_TRACE=host
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
5b783728e5
commit
101f2c8b5e
4 files changed
+43
-10
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];
|
||||
|
||||
@@ -267,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);
|
||||
@@ -297,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);
|
||||
@@ -372,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
|
||||
|
||||
Reference in new issue
Block a user