Files
Triforce-Source/host/mkw_glue.c
ruckus7382sfgsgdf e6f2f248f2 Triforce v0.7.0 "Sage Of Courage"
The 0.6 crashes after warps and area changes fixed (no write-back into memory the capture leaves out), invisible objects fixed (occlusion gate), DLSS frame generation flicker fixed, achievements, the renderer built with a play profile, faster translated game code, helper threads judged on settled scenes, memory, shader preparation, audio effects, launcher, controls and Linux fixes.
2026-11-22 11:59:06 -06:00

1106 lines
59 KiB
C

/* mkw-recomp's pieces of the standalone runtime that TOTKPort's hos/ keeps elsewhere: the 64-bit guest window's
commit (TOTK commits its whole 4 GB window up front; MKW's 512 GB window is only reserved), and stand-ins for the
hooks of files not taken into host/ (the renderer and display, the profiler, mods, TOTK's own patches). */
#include "hos.h"
#include "heap_commit.h" /* ((Triforce, 11-21, MEM-09) the heap committed as it is touched: its rules) */
#include "stall_host.h" /* ((Triforce, 11-22, OS-51) the host's part of a long frame, counted where it is spent) */
#include "host_alloc.h" /* ((Triforce, 11-22, OS-04) the wait at Windows' commit limit: its length, and the host's own allocations) */
#include <stdio.h>
#include <stdlib.h>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#ifndef _WIN32
#include <dlfcn.h>
#include <execinfo.h>
#endif
/* ---- the guest window ---------------------------------------------------------------------------------------
The runtime commits what it hands out (image, system region, heap, address regions: hos_commit), because host
APIs given guest buffers (ReadFile into a game buffer) fail on reserved pages without raising an exception. A
guest or host access to a page nothing committed is committed on first touch, 64 KB at a time, by the handler. */
/* ((Triforce, 11-21, the performance audit's MEM-09, after Source A's) A commit refused at Windows' commit limit (1455: RAM
and page file full) is tried again for up to 5 s, 100 times 50 ms apart: the renderer gives its textures' commit back
when the system's runs low (vk_texture.cpp, RX_VK_TEX_COMMIT_FREE_MB), and a page file set to grow does. Before, the
commit failed at once and the game's next write into those pages ended it. With the heap committed as it is touched
(heap_commit.h) a refusal can come at any first touch. 1: committed after waiting; 0: not */
/* ((Triforce, 11-22, the performance audit's OS-04) HOS_COMMIT_WAIT_MS, read once: how long a refusal at the commit limit
is waited out, here and in hos_xmalloc below: 5000 ms unless set, 0 for no wait at all (host_alloc.h) */
int hos_commit_wait_ms(void) {
static int ms = -1;
if (ms < 0) ms = host_alloc_wait_ms(getenv("HOS_COMMIT_WAIT_MS"));
return ms;
}
static int commit_wait(void *p, SIZE_T size) {
static volatile LONG made, given_up;
const int tries = host_alloc_tries(hos_commit_wait_ms()); /* ((Triforce, OS-04) 100 for the 5 s; 0: the refusal stands at once) */
for (int i = 0; i < tries; i++) {
Sleep(50);
if (VirtualAlloc(p, size, MEM_COMMIT, PAGE_READWRITE)) {
const LONG n = InterlockedIncrement(&made);
if (n <= 8 || (n & 255) == 0)
hos_log("mem: commit of %#llx bytes refused at the commit limit, made after %d ms (%ld so far)",
(unsigned long long)size, (i + 1) * 50, (long)n);
return 1;
}
if (GetLastError() != ERROR_COMMITMENT_LIMIT) return 0;
}
if (tries && InterlockedIncrement(&given_up) <= 8)
hos_log("mem: commit of %#llx bytes still refused after %d ms at the commit limit", (unsigned long long)size,
tries * HOST_ALLOC_STEP_MS);
return 0;
}
/* ((Triforce, 11-22, the performance audit's OS-04, after Source A's) The host's own tables at the commit limit: malloc,
calloc and realloc that are tried again every 50 ms for up to HOS_COMMIT_WAIT_MS before they answer NULL, on which
their callers end the game as before (host_alloc.h has why). The thread keeps its virtual core while it waits: some of
the callers hold a lock of the host's, and a thread that queued for its core again under one could wait on a thread
that holds a core and waits for that lock. The first 8 rescues and the first 8 refusals are logged, and a refusal
that stands brings the out-of-memory notice, once */
typedef struct { int kind; void *p; size_t count, n; } HostAllocAsk;
static void *host_alloc_try(void *ctx) {
const HostAllocAsk *a = (const HostAllocAsk *)ctx;
return a->kind == 0 ? malloc(a->n) : a->kind == 1 ? calloc(a->count, a->n) : realloc(a->p, a->n);
}
static void host_alloc_sleep(int ms) { Sleep((DWORD)ms); }
DWORD WINAPI hos_out_of_memory_box(LPVOID);
static void *host_alloc(HostAllocAsk *a) {
void *p = host_alloc_try(a);
if (p || !a->n || (a->kind == 1 && !a->count)) return p; /* (a size of 0: the C library's own answer) */
static volatile LONG made, given_up, said;
int waited = 0;
p = host_alloc_retry(host_alloc_try, a, host_alloc_tries(hos_commit_wait_ms()), host_alloc_sleep, &waited);
const unsigned long long bytes = (unsigned long long)a->n * (a->kind == 1 ? a->count : 1);
if (p) {
const LONG n = InterlockedIncrement(&made);
if (n <= 8 || (n & 255) == 0)
hos_log("mem: a host allocation of %llu bytes refused, made after %d ms (%ld so far)", bytes, waited, (long)n);
return p;
}
if (InterlockedIncrement(&given_up) <= 8)
hos_log("mem: a host allocation of %llu bytes still refused after %d ms", bytes, waited);
if (InterlockedExchange(&said, 1) == 0) {
hos_log("mem: Windows is out of memory (RAM and page file): close other programs, or let Windows manage the page file size");
CloseHandle(CreateThread(NULL, 0, hos_out_of_memory_box, NULL, 0, NULL));
}
return NULL;
}
void *hos_xmalloc(size_t n) {
HostAllocAsk a = {0, NULL, 1, n};
return host_alloc(&a);
}
void *hos_xcalloc(size_t count, size_t size) {
HostAllocAsk a = {1, NULL, count, size};
return host_alloc(&a);
}
void *hos_xrealloc(void *p, size_t n) {
HostAllocAsk a = {2, p, 1, n};
return host_alloc(&a);
}
void *hos_mem_commit_begin(uint64_t addr, uint64_t size); /* ((Triforce) hos_mem.c: the census's notes around the commit) */
void hos_mem_commit_end(void *had, uint64_t addr, uint64_t size, int committed);
int hos_commit(uint64_t addr, uint64_t size) {
if (!size) return 1;
if (!hos_guest_range(addr, size)) { /* (hookshot: guest pages only; the posix layer's commit changes any page's protection) */
hos_log("mem: commit of %#llx bytes at %#llx: outside the guest window", (unsigned long long)size, (unsigned long long)addr);
return -1;
}
uint64_t lo = addr & ~0xFFFFULL, hi = (addr + size + 0xFFFF) & ~0xFFFFULL;
{ /* (HOS_LARGE_PAGES: the heap's large pages are a reservation of their own, committed whole; what lies below or
above them is committed by itself) */
extern uint64_t hos_large_lo, hos_large_hi;
if (hos_large_hi && lo < hos_large_hi && hi > hos_large_lo) {
const uint64_t llo = lo > hos_large_lo ? lo : hos_large_lo, lhi = hi < hos_large_hi ? hi : hos_large_hi;
hos_mem_note(HOS_MEM_COMMITTED, llo, lhi - llo); /* ((Triforce) the census: committed already, handed out now) */
const int below = lo < hos_large_lo ? hos_commit(lo, hos_large_lo - lo) : 1;
if (hi <= hos_large_hi) return below;
lo = hos_large_hi;
}
}
/* ((Triforce) the census: held before the commit, under the lock the give-back's decommit checks under; no longer kept once
it is made, and as before if it fails: hos_mem.c hos_mem_commit_begin) */
void *const census_had = hos_mem_commit_begin(lo, hi - lo);
/* (Triforce: a commit of committed pages resets their protection, the lazy write-back's too: rx_backend.c) */
const int io = hos_guest_io_begin(GP(lo), hi - lo);
int committed = VirtualAlloc(GP(lo), (SIZE_T)(hi - lo), MEM_COMMIT, PAGE_READWRITE) != NULL;
DWORD commit_err = committed ? 0 : GetLastError();
/* ((Triforce, 11-21, MEM-09) refused at the commit limit: tried again for up to 5 s, commit_wait above) */
if (!committed && commit_err == ERROR_COMMITMENT_LIMIT && commit_wait(GP(lo), (SIZE_T)(hi - lo))) committed = 1, commit_err = 0;
hos_guest_io_end(io);
hos_mem_commit_end(census_had, lo, hi - lo, committed);
if (!committed) {
const DWORD err = commit_err;
hos_log("mem: commit of %#llx bytes at %#llx failed (error %lu)", (unsigned long long)(hi - lo),
(unsigned long long)lo, err);
if (err == ERROR_COMMITMENT_LIMIT) { /* (1455: RAM and page file full; the game crashes soon after. Said once, on its own thread) */
static volatile LONG said;
if (InterlockedExchange(&said, 1) == 0) {
hos_log("mem: Windows is out of memory (RAM and page file): close other programs, or let Windows manage the page file size");
extern DWORD WINAPI hos_out_of_memory_box(LPVOID);
CloseHandle(CreateThread(NULL, 0, hos_out_of_memory_box, NULL, 0, NULL));
}
}
return 0;
}
return 1;
}
/* ((Triforce, 11-21, MEM-09) Guest memory about to go to the kernel (a file read into it, a write from it): with the heap
committed as it is touched (hos_heap_on_touch, hos_mem.c), each page of [addr, addr + size) is read once first, so that
one still only reserved is committed by the handler below, as the CPU's own first touch of it would. The kernel does
not fault a page in: ReadFile into heap pages nothing touched yet failed with no exception. A byte read a 4 KB page,
nothing written: 30 microseconds for a 100 MB buffer in use. Not VirtualQuery for which pages are reserved: it walks
every page of the committed region it is asked in, GBs of them, and the title ran 105 to 141 fps for 240 with it in
each read. With the heap committed whole (HOS_HEAP_COMMIT=1, large pages, Linux) nothing is done, as before */
int hos_heap_on_touch;
void hos_guest_touch(uint64_t addr, uint64_t size) {
if (!hos_heap_on_touch || !size || !hos_guest_range(addr, size)) return;
const uint64_t end = addr + size;
for (uint64_t a = addr; a < end; a = (a | 0xFFF) + 1) (void)*(volatile const uint8_t *)GP(a);
}
DWORD WINAPI hos_out_of_memory_box(LPVOID unused) {
(void)unused;
MessageBoxA(NULL,
"Windows has run out of memory (RAM plus the page file), so Triforce may close.\n\n"
"Close other programs, and in Windows' System > About > Advanced system settings > Performance > Advanced > Virtual "
"memory, choose \"Automatically manage paging file size\" (or give it at least 16 GB). A lower Quality preset in the "
"game's Options (the Graphics tab) also uses less memory.",
"Triforce: out of memory", MB_OK | MB_ICONWARNING | MB_TOPMOST);
return 0;
}
static LONG CALLBACK commit_on_touch(EXCEPTION_POINTERS *info) {
EXCEPTION_RECORD *rec = info->ExceptionRecord;
if (rec->ExceptionCode != EXCEPTION_ACCESS_VIOLATION || rec->NumberParameters < 2) return EXCEPTION_CONTINUE_SEARCH;
const uint8_t *p = (const uint8_t *)rec->ExceptionInformation[1];
if (p < A64_MEM || p >= A64_MEM + HOS_GUEST_SPACE) return EXCEPTION_CONTINUE_SEARCH;
const uint64_t guest = (uint64_t)(p - A64_MEM);
if (guest < 0x100000) return EXCEPTION_CONTINUE_SEARCH; /* (a null pointer: the game's own fault) */
MEMORY_BASIC_INFORMATION mbi;
if (!VirtualQuery(p, &mbi, sizeof mbi)) return EXCEPTION_CONTINUE_SEARCH;
#ifdef _WIN32
/* ((Triforce, 11-21, MEM-09) another thread committed this 64 KB between the fault and the look: the access is tried
again. With the heap committed as it is touched, two threads touching one new chunk at once is common, and the second
went on to the crash report. Read-write only: the lazy write-back's pages and the watches' are their own handlers'.
Windows only: Linux's VirtualQuery stand-in calls every guest page committed and read-write, and every fault in the
window would be tried again for ever) */
if (heap_touch_again(mbi.State == MEM_COMMIT, mbi.Protect == PAGE_READWRITE, rec->ExceptionInformation[0]))
return EXCEPTION_CONTINUE_EXECUTION;
#endif
if (mbi.State != MEM_RESERVE) return EXCEPTION_CONTINUE_SEARCH;
/* ((Triforce, 11-21, MEM-09) the heap's own first touches are how it is committed now (hos_mem.c SetMemoryHeapSize):
counted apart, and not among the 16 lines for pages nothing handed out) */
const int heap = guest >= HOS_HEAP_BASE && guest - HOS_HEAP_BASE < hos_heap_size;
static volatile LONG touched, heap_touched;
if (!heap && InterlockedIncrement(&touched) <= 16)
hos_log("mem: %#llx committed on first touch (nothing handed it out)", (unsigned long long)guest);
/* (only the reserved pages of its 64 KB: a commit over committed ones resets their protection, heap_commit.h) */
const uint64_t run = (uint64_t)((const uint8_t *)mbi.BaseAddress - A64_MEM);
uint64_t lo, hi;
heap_touch_range(guest, run, run + mbi.RegionSize, &lo, &hi);
if (!VirtualAlloc(GP(lo), (SIZE_T)(hi - lo), MEM_COMMIT, PAGE_READWRITE) &&
!(GetLastError() == ERROR_COMMITMENT_LIMIT && commit_wait(GP(lo), (SIZE_T)(hi - lo)))) /* (at the limit: waited for) */
return EXCEPTION_CONTINUE_SEARCH;
hos_mem_note(HOS_MEM_TOUCHED, lo, hi - lo); /* ((Triforce) the census: atomics only, no lock) */
if (heap) {
const LONG n = InterlockedIncrement(&heap_touched);
if (heap_touch_said(n))
hos_log("mem: the heap is committed as it is touched: %ld first touches so far, 64 KB each at most (%ld MB of its "
"%llu; HOS_HEAP_COMMIT=1: all of it at the start, as before)", (long)n, (long)(n >> 4),
(unsigned long long)(hos_heap_size >> 20));
}
return EXCEPTION_CONTINUE_EXECUTION;
}
/* A crash nothing handles: what, where (mkw.exe+offset: nx2/dllmap.py reads mkw.map the same way), and the guest
state of the thread, as of its last call out of lifted code, with its frame chain from x29. */
static uint64_t peek_guest(uint64_t a) {
MEMORY_BASIC_INFORMATION mbi;
if (a >= HOS_GUEST_SPACE - 8 || !VirtualQuery(GP(a), &mbi, sizeof mbi) || mbi.State != MEM_COMMIT) return 0;
return G64(a);
}
static LONG WINAPI report_crash(EXCEPTION_POINTERS *info) {
/* (Triforce) one report for a failed run (hos_thread.c hos_ending_enter): another thread that crashes or hits a fatal
error while this one reports waits instead of ending the process under it, and a crash inside this thread's own
report, or the same crash handed here twice (report_fatal below, then the unhandled filter), is not reported again */
int hos_ending_enter(void);
if (!hos_ending_enter()) return EXCEPTION_CONTINUE_SEARCH;
/* (Triforce) first of all, what the other threads are doing now: taken on a helper thread while this one writes its
report, written at the report's end (host/crash_threads.h) */
void hos_crash_threads_begin(void), hos_crash_threads_end(void);
hos_crash_threads_begin();
EXCEPTION_RECORD *rec = info->ExceptionRecord;
const uint64_t rip = info->ContextRecord->Rip;
HMODULE mod = NULL;
char name[MAX_PATH] = "?";
if (GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
(LPCSTR)rip, &mod))
GetModuleFileNameA(mod, name, MAX_PATH);
const char *base = strrchr(name, '\\');
const uint64_t addr = rec->NumberParameters > 1 ? rec->ExceptionInformation[1] : 0;
hos_log("CRASH: exception %#lx (%s %#llx, guest %#llx) at %s+%#llx", rec->ExceptionCode,
rec->NumberParameters > 1 && rec->ExceptionInformation[0] == 1 ? "write" : "read",
(unsigned long long)addr, (unsigned long long)(addr - (uint64_t)A64_MEM), base ? base + 1 : name,
(unsigned long long)(rip - (uint64_t)mod));
#ifdef _WIN32
{ /* the host stack from the faulting frame (x64 unwind data), as module+offset (nx2/samap.py maps mkw.exe's) */
CONTEXT ctx = *info->ContextRecord;
char hb[1024];
int n = snprintf(hb, sizeof hb, "CRASH: host backtrace:");
for (int i = 0; i < 20 && ctx.Rip && n < (int)sizeof hb - 48; i++) {
HMODULE m = NULL;
char mn[MAX_PATH] = "?";
if (GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
(LPCSTR)ctx.Rip, &m))
GetModuleFileNameA(m, mn, MAX_PATH);
const char *mb = strrchr(mn, '\\');
n += snprintf(hb + n, sizeof hb - n, " %s+%#llx", mb ? mb + 1 : mn, (unsigned long long)(ctx.Rip - (uint64_t)m));
DWORD64 image = 0;
PRUNTIME_FUNCTION f = RtlLookupFunctionEntry(ctx.Rip, &image, NULL);
if (!f) break;
void *handler_data;
DWORD64 frame;
RtlVirtualUnwind(UNW_FLAG_NHANDLER, image, ctx.Rip, f, &ctx, &handler_data, &frame, NULL);
}
hos_log("%s", hb);
}
#else
{ /* (hookshot) Linux: the host stack as module+offset, from glibc (the frames above the signal's included): the
x64 unwind data is Windows' */
void *pcs[24];
const int got = backtrace(pcs, 24);
char hb[1024];
int n = snprintf(hb, sizeof hb, "CRASH: host backtrace:");
for (int i = 0; i < got && n < (int)sizeof hb - 48; i++) {
Dl_info d;
const char *mn = "?", *mb;
uint64_t off = (uint64_t)(uintptr_t)pcs[i];
if (dladdr(pcs[i], &d) && d.dli_fname) mn = d.dli_fname, off -= (uint64_t)(uintptr_t)d.dli_fbase;
mb = strrchr(mn, '/');
n += snprintf(hb + n, sizeof hb - n, " %s+%#llx", mb ? mb + 1 : mn, (unsigned long long)off);
}
hos_log("%s", hb);
}
#endif
Ctx *c = hos_ctx();
if (c) {
hos_log("CRASH: thread %s, sp %#llx", hos_thread_name(hos_self()), (unsigned long long)c->x[31]);
for (int i = 0; i < 31; i += 4)
hos_log("CRASH: x%-2d %016llx x%-2d %016llx x%-2d %016llx x%-2d %016llx", i,
(unsigned long long)c->x[i], i + 1, (unsigned long long)c->x[i + 1], i + 2,
(unsigned long long)c->x[i + 2], i + 3, i + 3 < 31 ? (unsigned long long)c->x[i + 3] : 0ULL);
char bt[1024];
int n = snprintf(bt, sizeof bt, "CRASH: guest backtrace: lr %#llx", (unsigned long long)c->x[30]);
uint64_t fp = c->x[29];
for (int i = 0; i < 40 && fp && !(fp & 15) && n < (int)sizeof bt - 24; i++) {
const uint64_t lr = peek_guest(fp + 8), next = peek_guest(fp);
n += snprintf(bt + n, sizeof bt - n, " %#llx", (unsigned long long)lr);
if (next <= fp) break;
fp = next;
}
hos_log("%s", bt);
/* the memory x0, x1 and x19 point to (the object a crash is usually about) */
static const int dumped[] = {0, 1, 19};
for (int k = 0; k < 3; k++) {
const int r = dumped[k];
const uint64_t base = c->x[r];
if (base < 0x100000 || !peek_guest(base) && !peek_guest(base + 8) && !peek_guest(base + 0x100)) continue;
for (int row = 0; row < 0x120; row += 32)
hos_log("CRASH: [x%d+%#05x] %016llx %016llx %016llx %016llx", r, row,
(unsigned long long)peek_guest(base + row), (unsigned long long)peek_guest(base + row + 8),
(unsigned long long)peek_guest(base + row + 16), (unsigned long long)peek_guest(base + row + 24));
}
}
hos_log_flush(); /* (the report so far is written before the wait for the other threads' stacks) */
hos_crash_threads_end();
hos_log_flush();
void hos_hostcall_stats(void);
hos_hostcall_stats(); /* (HOS_HOSTCALL_STATS=1: the host calls made, and the last 256 of them) */
return EXCEPTION_CONTINUE_SEARCH;
}
/* (hos_prof.c's, for hos_fatal) the current thread's guest frame chain from x29 */
void hos_backtrace(void) {
Ctx *c = hos_ctx();
if (!c) return;
char bt[1024];
int n = snprintf(bt, sizeof bt, "guest backtrace: lr %#llx", (unsigned long long)c->x[30]);
uint64_t fp = c->x[29];
for (int i = 0; i < 40 && fp && !(fp & 15) && n < (int)sizeof bt - 24; i++) {
const uint64_t lr = peek_guest(fp + 8), next = peek_guest(fp);
n += snprintf(bt + n, sizeof bt - n, " %#llx", (unsigned long long)lr);
if (next <= fp) break;
fp = next;
}
fprintf(stderr, "[hos] %s\n", bt);
fflush(stderr);
}
/* (Triforce) HOS_CRASH_TEST_S=<seconds>, a lab switch: a host thread reads address 8 that long after the start, so that a
crash report (and its other threads' lines, host/crash_threads.h) can be looked at from a run in the game */
static DWORD WINAPI crash_test_after(void *p) {
Sleep((DWORD)(uintptr_t)p * 1000);
hos_log("crash test: a read of address 8 now (HOS_CRASH_TEST_S)");
hos_log_flush();
volatile int *bad = (volatile int *)(uintptr_t)8;
return (DWORD)*bad;
}
/* HOS_STATS_AFTER=<seconds> (with HOS_HOSTCALL_STATS=1): the host call counts and the last calls at that time, for
runs that are stopped rather than exit (what a quiet game is doing) */
static DWORD WINAPI stats_after(void *p) {
Sleep((DWORD)(uintptr_t)p * 1000);
void hos_hostcall_stats(void);
hos_log("stats: after %u s", (unsigned)(uintptr_t)p);
void hos_thread_dump(void);
hos_thread_dump();
hos_log_flush();
hos_hostcall_stats();
return 0;
}
/* heap corruption and fail-fast reports go to the process's end without reaching the unhandled filter: reported here
first (the backtrace is where the heap noticed, after the bad write) */
static LONG CALLBACK report_fatal(EXCEPTION_POINTERS *info) {
const DWORD code = info->ExceptionRecord->ExceptionCode;
if (code == 0xC0000374 || code == 0xC0000409) report_crash(info);
return EXCEPTION_CONTINUE_SEARCH;
}
void hos_commit_init(void) {
AddVectoredExceptionHandler(1, commit_on_touch);
AddVectoredExceptionHandler(0, report_fatal);
SetUnhandledExceptionFilter(report_crash);
const char *after = getenv("HOS_STATS_AFTER");
if (after && atoi(after) > 0) CreateThread(NULL, 0, stats_after, (void *)(uintptr_t)atoi(after), 0, NULL);
const char *crash = getenv("HOS_CRASH_TEST_S");
if (crash && atoi(crash) > 0) CreateThread(NULL, 0, crash_test_after, (void *)(uintptr_t)atoi(crash), 0, NULL);
}
/* ---- stand-ins --------------------------------------------------------------------------------------------- */
/* HOS_PROFILE=<seconds> (in place of TOTKPort's hos_prof.c): every interval, the process's busiest threads and their
share of a core (guest threads by their guest names, host threads by their descriptions): which one a frame rate
waits for */
#ifdef _WIN32
#include <tlhelp32.h>
int hos_thread_handles(HANDLE *out, const char **names, int max); /* hos_thread.c */
static DWORD WINAPI prof_thread(void *p) {
const DWORD ms = (DWORD)(uintptr_t)p * 1000;
enum { MAXT = 512 };
static DWORD ids[MAXT];
static uint64_t was[MAXT];
int nwas = 0;
for (;;) {
Sleep(ms);
static HANDLE gh[1024];
static const char *gn[1024];
const int ng = hos_thread_handles(gh, gn, 1024);
struct { DWORD id; uint64_t cpu, delta; char name[64]; } cur[MAXT];
int n = 0;
HANDLE snap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
THREADENTRY32 te = {sizeof te};
for (BOOL ok = Thread32First(snap, &te); ok && n < MAXT; ok = Thread32Next(snap, &te)) {
if (te.th32OwnerProcessID != GetCurrentProcessId()) continue;
HANDLE h = OpenThread(THREAD_QUERY_LIMITED_INFORMATION, FALSE, te.th32ThreadID);
if (!h) continue;
FILETIME c0, e0, k, u;
if (GetThreadTimes(h, &c0, &e0, &k, &u)) {
cur[n].id = te.th32ThreadID;
cur[n].cpu = ((uint64_t)k.dwHighDateTime << 32 | k.dwLowDateTime) + ((uint64_t)u.dwHighDateTime << 32 | u.dwLowDateTime);
cur[n].delta = cur[n].cpu;
for (int i = 0; i < nwas; i++)
if (ids[i] == te.th32ThreadID) { cur[n].delta = cur[n].cpu - was[i]; break; }
snprintf(cur[n].name, sizeof cur[n].name, "tid %lu", te.th32ThreadID);
for (int i = 0; i < ng; i++)
if (GetThreadId(gh[i]) == te.th32ThreadID) { snprintf(cur[n].name, sizeof cur[n].name, "%s", gn[i]); break; }
if (!strncmp(cur[n].name, "tid ", 4)) {
PWSTR d = NULL;
if (SUCCEEDED(GetThreadDescription(h, &d)) && d && d[0])
WideCharToMultiByte(CP_UTF8, 0, d, -1, cur[n].name, sizeof cur[n].name, NULL, NULL);
if (d) LocalFree(d);
}
n++;
}
CloseHandle(h);
}
CloseHandle(snap);
for (int i = 0; i < n && i < MAXT; i++) ids[i] = cur[i].id, was[i] = cur[i].cpu;
nwas = n;
for (int i = 0; i < n; i++) /* (busiest first) */
for (int j = i + 1; j < n; j++)
if (cur[j].delta > cur[i].delta) { char t[sizeof cur[0]]; memcpy(t, &cur[i], sizeof t); memcpy(&cur[i], &cur[j], sizeof t); memcpy(&cur[j], t, sizeof t); }
char line[1024];
int len = snprintf(line, sizeof line, "prof: busiest threads (%% of a core):");
uint64_t total = 0;
for (int i = 0; i < n; i++) total += cur[i].delta;
for (int i = 0; i < n && i < 10 && len < (int)sizeof line - 80; i++)
len += snprintf(line + len, sizeof line - len, " %s %.0f,", cur[i].name, cur[i].delta / (ms * 100.0));
snprintf(line + len, sizeof line - len, " all %.0f", total / (ms * 100.0));
hos_log("%s", line);
}
return 0;
}
/* HOS_SAMPLE=<thread name> (with HOS_PROFILE=<seconds>): that thread's instruction pointer sampled every millisecond;
every interval the most frequent places, as module+offset of the leaf and of the first frame in mkw.exe under it
(what of ours called into wgpu or the system). nx2/samap.py maps the mkw.exe offsets to functions. */
static DWORD WINAPI sample_thread(void *p) {
const char *want = (const char *)p;
const DWORD ms = (DWORD)atoi(getenv("HOS_PROFILE")) * 1000;
HANDLE th = NULL;
while (!th) { /* (the renderer's threads start late) */
Sleep(1000);
HANDLE snap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
THREADENTRY32 te = {sizeof te};
for (BOOL ok = Thread32First(snap, &te); ok && !th; ok = Thread32Next(snap, &te)) {
if (te.th32OwnerProcessID != GetCurrentProcessId()) continue;
HANDLE h = OpenThread(THREAD_ALL_ACCESS, FALSE, te.th32ThreadID);
if (!h) continue;
PWSTR d = NULL;
char name[64] = "";
if (SUCCEEDED(GetThreadDescription(h, &d)) && d) WideCharToMultiByte(CP_UTF8, 0, d, -1, name, sizeof name, NULL, NULL);
if (d) LocalFree(d);
if (strstr(name, want)) th = h;
else CloseHandle(h);
}
CloseHandle(snap);
}
hos_log("prof: sampling thread \"%s\"", want);
const HMODULE exe = GetModuleHandleA(NULL);
enum { SLOTS = 4096 };
static struct { uint64_t key; uint32_t n; } leaf[SLOTS], ours[SLOTS];
static char modname[64][64];
static HMODULE mods[64];
int nmods = 0;
uint32_t total = 0;
DWORD last = GetTickCount();
/* HOS_SAMPLE_FILE=<path>: every sample's whole stack too, one line of module+offset frames (leaf first), for
an offline profile (work/prof.py maps them to functions with the linker maps) */
FILE *stacks = getenv("HOS_SAMPLE_FILE") ? fopen(getenv("HOS_SAMPLE_FILE"), "w") : NULL;
for (;;) {
Sleep(1);
if (SuspendThread(th) == (DWORD)-1) return 0; /* (the thread ended) */
CONTEXT ctx = {0};
ctx.ContextFlags = CONTEXT_CONTROL | CONTEXT_INTEGER;
const BOOL got = GetThreadContext(th, &ctx);
uint64_t keys[2] = {0, 0};
char stack_line[1024];
int stack_len = 0;
if (got) {
for (int depth = 0; depth < 48 && ctx.Rip; depth++) {
HMODULE m = NULL;
GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
(LPCSTR)ctx.Rip, &m);
int mi = -1;
for (int i = 0; i < nmods; i++) if (mods[i] == m) mi = i;
if (mi < 0 && nmods < 64) {
mi = nmods++;
mods[mi] = m;
char path[MAX_PATH] = "?";
if (m) GetModuleFileNameA(m, path, MAX_PATH);
const char *b = strrchr(path, '\\');
snprintf(modname[mi], sizeof modname[mi], "%s", b ? b + 1 : path);
}
const uint64_t key = (uint64_t)(mi < 0 ? 63 : mi) << 56 | (ctx.Rip - (uint64_t)m);
if (stacks && stack_len < (int)sizeof stack_line - 96)
stack_len += snprintf(stack_line + stack_len, sizeof stack_line - stack_len, "%s%s+%llx",
depth ? ";" : "", mi < 0 ? "?" : modname[mi],
(unsigned long long)(ctx.Rip - (uint64_t)m));
if (depth == 0) keys[0] = key;
if (m == exe && !keys[1]) keys[1] = key;
if (m == exe && !stacks) break;
DWORD64 image = 0;
PRUNTIME_FUNCTION f = RtlLookupFunctionEntry(ctx.Rip, &image, NULL);
if (!f) break;
void *hd;
DWORD64 frame;
RtlVirtualUnwind(UNW_FLAG_NHANDLER, image, ctx.Rip, f, &ctx, &hd, &frame, NULL);
}
}
ResumeThread(th);
if (stacks && got && stack_len) fprintf(stacks, "%s\n", stack_line);
if (got) {
total++;
for (int t = 0; t < 2; t++) {
if (!keys[t]) continue;
typeof(leaf[0]) *tab = t ? ours : leaf;
for (uint32_t s = (uint32_t)(keys[t] * 0x9E3779B97F4A7C15ull >> 52), k = 0; k < SLOTS; k++, s = (s + 1) % SLOTS) {
if (tab[s].key == keys[t]) { tab[s].n++; break; }
if (!tab[s].key) { tab[s].key = keys[t], tab[s].n = 1; break; }
}
}
}
if (GetTickCount() - last >= ms) {
last = GetTickCount();
for (int t = 0; t < 2; t++) {
typeof(leaf[0]) *tab = t ? ours : leaf;
char line[2048];
int len = snprintf(line, sizeof line, "prof-sample %s (%u samples):", t ? "ours" : "leaf", total);
for (int r = 0; r < 30 && len < (int)sizeof line - 64; r++) {
int best = -1;
for (int s = 0; s < SLOTS; s++) if (tab[s].n && (best < 0 || tab[s].n > tab[best].n)) best = s;
if (best < 0) break;
len += snprintf(line + len, sizeof line - len, " %s+%llx %u,", modname[tab[best].key >> 56],
(unsigned long long)(tab[best].key & ((1ull << 56) - 1)), tab[best].n);
tab[best].n = 0;
}
hos_log("%s", line);
memset(tab, 0, sizeof leaf);
}
total = 0;
if (stacks) fflush(stacks);
}
}
return 0;
}
/* HOS_THREAD_TIMES=<seconds>: every interval, the process's threads that used over 5% of a core, by name, with
their share of one core (which thread holds the frame back) */
static DWORD WINAPI thread_times_thread(void *p) {
const DWORD ms = (DWORD)(uintptr_t)p * 1000;
enum { MAX = 512 };
static struct { DWORD tid; uint64_t cpu; } last[MAX];
int nlast = 0;
for (;;) {
Sleep(ms);
HANDLE snap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
THREADENTRY32 te = {sizeof te};
char line[4096];
int n = snprintf(line, sizeof line, "threads in %u s:", (unsigned)(ms / 1000));
static struct { DWORD tid; uint64_t cpu; } now[MAX];
int nnow = 0;
for (BOOL ok = Thread32First(snap, &te); ok; ok = Thread32Next(snap, &te)) {
if (te.th32OwnerProcessID != GetCurrentProcessId() || nnow >= MAX) continue;
HANDLE h = OpenThread(THREAD_QUERY_LIMITED_INFORMATION, FALSE, te.th32ThreadID);
if (!h) continue;
FILETIME c, x, k, u;
uint64_t cpu = 0;
if (GetThreadTimes(h, &c, &x, &k, &u))
cpu = (((uint64_t)k.dwHighDateTime << 32) | k.dwLowDateTime) + (((uint64_t)u.dwHighDateTime << 32) | u.dwLowDateTime);
uint64_t before = 0;
int seen = 0;
for (int i = 0; i < nlast; i++)
if (last[i].tid == te.th32ThreadID) before = last[i].cpu, seen = 1;
now[nnow].tid = te.th32ThreadID, now[nnow].cpu = cpu, nnow++;
const double pct = seen ? (double)(cpu - before) / (ms * 10000.0) * 100.0 : 0;
if (pct >= 5.0 && n < (int)sizeof line - 96) {
PWSTR d = NULL;
char name[64] = "?";
if (SUCCEEDED(GetThreadDescription(h, &d)) && d && d[0]) WideCharToMultiByte(CP_UTF8, 0, d, -1, name, sizeof name, NULL, NULL);
if (d) LocalFree(d);
n += snprintf(line + n, sizeof line - n, " %s %.0f%%;", name, pct);
}
CloseHandle(h);
}
CloseHandle(snap);
memcpy(last, now, sizeof(now[0]) * (size_t)nnow);
nlast = nnow;
hos_log("prof: %s", line);
}
return 0;
}
/* (hookshot) HOS_SAMPLE_ALL=<path> (with HOS_PROFILE): while <path>.go exists, every 2 ms every guest thread and the
renderer's threads (descriptions starting "mkw_vk") are sampled at the same instant: one line per thread,
"<ms> <name>|<frames>", the frames module+offset leaf first (16 at most). What each thread does while another waits:
the frame's critical path (scripts/crit.py) */
/* HOS_WATCH=<file> (diagnostic): when <file> exists (up to 4 guest addresses in hex), CPU data breakpoints on them on
every guest thread: 4 s on reads and writes, then 4 s on writes; <file>.out gets, by address and the host place of
the access, how many; then <file> is renamed <file>.done and the next one may come. */
static uint64_t watch_keys[8192];
static volatile LONG watch_counts[8192];
static volatile LONG watch_phase;
static LONG CALLBACK watch_handler(EXCEPTION_POINTERS *e) {
if (e->ExceptionRecord->ExceptionCode != EXCEPTION_SINGLE_STEP) return EXCEPTION_CONTINUE_SEARCH;
const DWORD64 hit = e->ContextRecord->Dr6 & 15;
if (!hit) return EXCEPTION_CONTINUE_SEARCH;
int idx = 0;
while (!(hit >> idx & 1)) idx++;
const uint64_t key = ((uint64_t)(watch_phase & 1) << 62) | ((uint64_t)idx << 58) | (uint64_t)e->ExceptionRecord->ExceptionAddress;
for (uint32_t i = (uint32_t)((key >> 2) * 2654435761u) & 8191, n = 0; n < 8192; n++, i = (i + 1) & 8191) {
uint64_t k = __atomic_load_n(&watch_keys[i], __ATOMIC_RELAXED);
if (!k) {
uint64_t expect = 0;
if (__atomic_compare_exchange_n(&watch_keys[i], &expect, key, 0, __ATOMIC_RELAXED, __ATOMIC_RELAXED)) k = key;
else k = expect;
}
if (k == key) { InterlockedIncrement(&watch_counts[i]); break; }
}
e->ContextRecord->Dr6 = 0;
return EXCEPTION_CONTINUE_EXECUTION;
}
static void watch_arm(const uint64_t *addr, int n, int rw) {
static HANDLE gh[1024];
static const char *gn[1024];
const int ng = hos_thread_handles(gh, gn, 1024);
for (int i = 0; i < ng; i++) {
if (GetThreadId(gh[i]) == GetCurrentThreadId()) continue;
if (SuspendThread(gh[i]) == (DWORD)-1) continue;
CONTEXT c;
memset(&c, 0, sizeof c);
c.ContextFlags = CONTEXT_DEBUG_REGISTERS;
if (GetThreadContext(gh[i], &c)) {
DWORD64 *dr[4] = {&c.Dr0, &c.Dr1, &c.Dr2, &c.Dr3};
c.Dr7 = 0;
for (int k = 0; k < n && rw; k++) {
*dr[k] = (DWORD64)GP(addr[k]);
c.Dr7 |= (DWORD64)1 << (k * 2); /* (local enable) */
c.Dr7 |= (DWORD64)((rw & 3) | (3 << 2)) << (16 + k * 4); /* (rw, 4 bytes) */
}
c.Dr6 = 0;
SetThreadContext(gh[i], &c);
}
ResumeThread(gh[i]);
}
}
static DWORD WINAPI watch_thread(void *p) {
const char *path = (const char *)p;
AddVectoredExceptionHandler(1, watch_handler);
for (;;) {
Sleep(300);
FILE *f = fopen(path, "r");
if (!f) continue;
uint64_t addr[4];
int n = 0;
unsigned long long v;
while (n < 4 && fscanf(f, "%llx", &v) == 1) addr[n++] = v;
fclose(f);
memset(watch_keys, 0, sizeof watch_keys);
memset((void *)watch_counts, 0, sizeof watch_counts);
for (int k = 0; k < n; k++) hos_log("watch: %llx holds %08x", (unsigned long long)addr[k], *(uint32_t *)GP(addr[k]));
for (int ph = 0; ph < 2; ph++) {
watch_phase = ph;
for (int t = 0; t < 4; t++) { /* (threads that started since) */
watch_arm(addr, n, ph ? 1 : 3);
Sleep(1000);
}
}
watch_arm(addr, 0, 0);
char out[MAX_PATH], done[MAX_PATH];
snprintf(out, sizeof out, "%s.out", path);
snprintf(done, sizeof done, "%s.done", path);
f = fopen(out, "a");
if (f) {
for (int i = 0; i < 8192; i++)
if (watch_keys[i]) {
const uint64_t k = watch_keys[i], rip = k & ((1ULL << 58) - 1);
HMODULE m = NULL;
char full[MAX_PATH] = "?";
GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, (LPCSTR)rip, &m);
if (m) GetModuleFileNameA(m, full, sizeof full);
const char *name = strrchr(full, 92) ? strrchr(full, 92) + 1 : full;
fprintf(f, "%llx %s %s+%llx %ld\n", (unsigned long long)addr[(k >> 58) & 3], k >> 62 & 1 ? "w" : "rw", name,
(unsigned long long)(rip - (uint64_t)m), watch_counts[i]);
}
fclose(f);
}
DeleteFileA(done);
MoveFileA(path, done);
hos_log("watch: done");
}
}
static DWORD WINAPI sample_all_thread(void *p) {
const char *path = (const char *)p;
char go[MAX_PATH];
snprintf(go, sizeof go, "%s.go", path);
FILE *out = NULL;
static HMODULE mods[64];
static char modname[64][64];
int nmods = 0;
enum { MAXT = 128 };
static HANDLE th[MAXT];
static char tn[MAXT][64];
int nt = 0;
int64_t t0 = 0;
for (;;) {
if (GetFileAttributesA(go) == INVALID_FILE_ATTRIBUTES) {
if (out) { fclose(out); out = NULL; hos_log("prof: all-thread sampling stopped"); }
Sleep(200);
continue;
}
if (!out) {
out = fopen(path, "a");
if (!out) { Sleep(1000); continue; }
for (int i = 0; i < nt; i++) CloseHandle(th[i]);
nt = 0;
static HANDLE gh[1024];
static const char *gn[1024];
const int ng = hos_thread_handles(gh, gn, 1024);
HANDLE snap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
THREADENTRY32 te = {sizeof te};
for (BOOL ok = Thread32First(snap, &te); ok && nt < MAXT; ok = Thread32Next(snap, &te)) {
if (te.th32OwnerProcessID != GetCurrentProcessId() || te.th32ThreadID == GetCurrentThreadId()) continue;
char name[64] = "";
for (int i = 0; i < ng; i++)
if (GetThreadId(gh[i]) == te.th32ThreadID) { snprintf(name, sizeof name, "%s", gn[i]); break; }
HANDLE h = OpenThread(THREAD_ALL_ACCESS, FALSE, te.th32ThreadID);
if (!h) continue;
if (!name[0]) {
PWSTR d = NULL;
char desc[64] = "";
if (SUCCEEDED(GetThreadDescription(h, &d)) && d) WideCharToMultiByte(CP_UTF8, 0, d, -1, desc, sizeof desc, NULL, NULL);
if (d) LocalFree(d);
if (!strncmp(desc, "mkw_vk", 6)) snprintf(name, sizeof name, "%s", desc);
}
if (!name[0]) { CloseHandle(h); continue; }
for (char *q = name; *q; q++) if (*q == ' ' || *q == '|') *q = '_';
th[nt] = h;
snprintf(tn[nt], sizeof tn[nt], "%s", name);
nt++;
}
CloseHandle(snap);
t0 = hos_now_ns();
hos_log("prof: all-thread sampling of %d threads into %s", nt, path);
}
Sleep(2);
const long ms = (long)((hos_now_ns() - t0) / 1000000);
for (int i = 0; i < nt; i++) {
if (SuspendThread(th[i]) == (DWORD)-1) continue;
CONTEXT ctx = {0};
ctx.ContextFlags = CONTEXT_CONTROL | CONTEXT_INTEGER;
const BOOL got = GetThreadContext(th[i], &ctx);
char line[1024];
int len = snprintf(line, sizeof line, "%ld %s|", ms, tn[i]);
for (int depth = 0; got && depth < 16 && ctx.Rip && len < (int)sizeof line - 96; depth++) {
HMODULE m = NULL;
GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
(LPCSTR)ctx.Rip, &m);
int mi = -1;
for (int k = 0; k < nmods; k++) if (mods[k] == m) mi = k;
if (mi < 0 && nmods < 64) {
mi = nmods++;
mods[mi] = m;
char mp[MAX_PATH] = "?";
if (m) GetModuleFileNameA(m, mp, MAX_PATH);
const char *b = strrchr(mp, '\\');
snprintf(modname[mi], sizeof modname[mi], "%s", b ? b + 1 : mp);
}
len += snprintf(line + len, sizeof line - len, "%s%s+%llx", depth ? ";" : "", mi < 0 ? "?" : modname[mi],
(unsigned long long)(ctx.Rip - (uint64_t)m));
DWORD64 image = 0;
PRUNTIME_FUNCTION f = RtlLookupFunctionEntry(ctx.Rip, &image, NULL);
if (!f) break;
void *hd;
DWORD64 frame;
RtlVirtualUnwind(UNW_FLAG_NHANDLER, image, ctx.Rip, f, &ctx, &hd, &frame, NULL);
}
ResumeThread(th[i]);
fprintf(out, "%s\n", line);
}
}
return 0;
}
void hos_prof_start(void) {
const char *tt = getenv("HOS_THREAD_TIMES");
if (tt && atoi(tt) > 0) CreateThread(NULL, 0, thread_times_thread, (void *)(uintptr_t)atoi(tt), 0, NULL);
const char *e = getenv("HOS_PROFILE");
if (e && atoi(e) > 0) CreateThread(NULL, 0, prof_thread, (void *)(uintptr_t)atoi(e), 0, NULL);
const char *s = getenv("HOS_SAMPLE");
if (e && atoi(e) > 0 && s && *s) CreateThread(NULL, 0, sample_thread, (void *)s, 0, NULL);
const char *all = getenv("HOS_SAMPLE_ALL");
if (all && *all) CreateThread(NULL, 0, sample_all_thread, (void *)all, 0, NULL);
const char *watch = getenv("HOS_WATCH");
if (watch && *watch) CreateThread(NULL, 0, watch_thread, (void *)watch, 0, NULL);
}
#endif /* (linux) host/posix/prof.c */
void hos_mem_checkpoint(const char *what) { (void)what; } /* (hos_prof.c: HOS_MEM_TIMELINE) */
int hos_game_cores(void) { return 0; } /* (totk_cores.c) */
int hos_blank_cached_icon(const char *guest, const char *host) { (void)guest, (void)host; return 0; } /* (TOTK) */
int hos_ultrawide_redirect(const char *guest, char *out, size_t cap) { (void)guest, (void)out, (void)cap; return 0; }
uint64_t hos_title_input(uint64_t buttons) { return buttons; } /* (totk_title.c) */
uint64_t hos_frame_step_buttons(uint64_t buttons) { return buttons; } /* (nn_vi.c) */
/* (hookshot) hos_sdl_pads, hos_sdl_rumble: host/pad_sdl.c (from TOTKPort; SDL2.dll beside the exe) */
/* TOTK's emulated GPU (gpu.c, its Maxwell command processor), which nvdrv.c and the debug commands call into: MKW
has none (its NVN2 calls go to nvn_native.c), so a submission is only counted. */
int hos_gpu_async(void) { return 0; }
void hos_gpu_submit(int channel_fd, const uint64_t *entries, uint32_t n) {
static volatile LONG seen;
if (InterlockedIncrement(&seen) <= 4) hos_log("gpu: submit of %u entries on channel %d (no GPU yet)", n, channel_fd);
(void)entries;
}
void hos_gpu_queue_submit(int channel_fd, const uint64_t *entries, uint32_t n, int incr_id) {
(void)incr_id;
hos_gpu_submit(channel_fd, entries, n);
}
/* The loop-header interrupt polls (nx2/poll.py): the virtual cores (HOS_VCORES) ask a thread holding a core a
waiter should have to give it up there, so guest code with no nn::os call or SVC in it (a long job) is preempted
as Horizon would preempt it. */
/* (nx2/rtgen.py) the loop-header poll: the lifted code has cleared c->poll and calls this with no argument, so that the
compiler keeps guest registers in host registers across the loop header (a call handed the context could change any
of it). The thread's context is the running one's (hos_ctx: a fiber's while it runs one), which is where the kick
(hos_thread.c) set the flag; nothing here reads it */
void a64_poll_cur(void) {
void hos_vcore_checkpoint(void);
hos_vcore_checkpoint();
}
void a64_poll(Ctx *c) { /* (the old form, A64_POLL_CTX builds) */
c->poll = 0;
a64_poll_cur();
}
/* CNTPCT_EL0 at the Switch 2's 31.25 MHz (rt/a64_rt.c's default counts the Switch 1's 19.2 MHz). The SDK checks
nn::os::GetSystemTickFrequency against 31250000 at init and aborts otherwise. */
uint64_t a64_cntpct_host(void) {
static LARGE_INTEGER base;
static uint64_t mult;
LARGE_INTEGER now;
if (!__atomic_load_n(&mult, __ATOMIC_ACQUIRE)) {
LARGE_INTEGER f, b;
QueryPerformanceFrequency(&f);
QueryPerformanceCounter(&b);
base = b;
__atomic_store_n(&mult, (HOS_TICK_HZ << 32) / (uint64_t)f.QuadPart, __ATOMIC_RELEASE);
}
QueryPerformanceCounter(&now);
return (uint64_t)(((unsigned __int128)(uint64_t)(now.QuadPart - base.QuadPart) * mult) >> 32);
}
/* ---- what the renderer (gpu/render, TOTKPort's) asks of TOTKPort's hos_prof.c and totk_ultrawide.c ---- */
int hos_ab(void) { return 0; } /* (hos_prof.c: A/B measurement phases) */
/* (with the renderer's mem: lines) HOS_MEM_MAP=1: how much of the guest window is committed, and its resident part */
void hos_memory_map_log(void) {
static int on = -1;
if (on < 0) on = getenv("HOS_MEM_MAP") != NULL;
if (!on) return;
uint64_t committed = 0, regions = 0;
for (uint64_t a = 0; a < HOS_GUEST_SPACE;) {
MEMORY_BASIC_INFORMATION m;
if (!VirtualQuery(GP(a), &m, sizeof m)) break;
const uint64_t end = (uint64_t)((uint8_t *)m.BaseAddress + m.RegionSize - (uint8_t *)GP(0));
if (m.State == MEM_COMMIT) committed += m.RegionSize, regions++;
a = end > a ? end : a + 0x1000;
}
hos_log("mem: guest window %llu MB committed in %llu regions", (unsigned long long)(committed >> 20),
(unsigned long long)regions);
/* the rest of the address space: private memory by allocation, the largest */
struct { uint64_t base, bytes; } top[8] = {{0}};
uint64_t other = 0, cur_base = 0, cur = 0;
const uint64_t g0 = (uint64_t)(uintptr_t)GP(0), g1 = g0 + HOS_GUEST_SPACE;
for (uint64_t a = 0x10000; a < 0x7FFFFFFF0000ull;) {
MEMORY_BASIC_INFORMATION m;
if (!VirtualQuery((void *)(uintptr_t)a, &m, sizeof m)) break;
const uint64_t base = (uint64_t)(uintptr_t)m.AllocationBase, end = (uint64_t)(uintptr_t)m.BaseAddress + m.RegionSize;
if (base != cur_base) {
for (int k = 0; k < 8 && cur; k++)
if (cur > top[k].bytes) {
memmove(&top[k + 1], &top[k], (size_t)(7 - k) * sizeof top[0]);
top[k].base = cur_base, top[k].bytes = cur;
break;
}
cur_base = base, cur = 0;
}
if (m.State == MEM_COMMIT && m.Type == MEM_PRIVATE && (a < g0 || a >= g1)) other += m.RegionSize, cur += m.RegionSize;
a = end > a ? end : a + 0x1000;
}
hos_log("mem: private outside the guest window %llu MB; largest: %llx %llu MB, %llx %llu MB, %llx %llu MB, %llx %llu "
"MB, %llx %llu MB, %llx %llu MB", (unsigned long long)(other >> 20), (unsigned long long)top[0].base,
(unsigned long long)(top[0].bytes >> 20), (unsigned long long)top[1].base, (unsigned long long)(top[1].bytes >> 20),
(unsigned long long)top[2].base, (unsigned long long)(top[2].bytes >> 20), (unsigned long long)top[3].base,
(unsigned long long)(top[3].bytes >> 20), (unsigned long long)top[4].base, (unsigned long long)(top[4].bytes >> 20),
(unsigned long long)top[5].base, (unsigned long long)(top[5].bytes >> 20));
}
void hos_ultrawide_apply(void) {} /* (totk_ultrawide.c) */
float hos_view_aspect; /* (totk_ultrawide.c: 0, the game's own 16:9) */
void hos_host_thread_register_self(const char *name) { /* (hos_prof.c: names for its stats; here the thread's description) */
wchar_t w[64];
if (MultiByteToWideChar(CP_UTF8, 0, name, -1, w, 64)) SetThreadDescription(GetCurrentThread(), w);
}
typedef struct {
DWORD cb, PageFaultCount;
SIZE_T PeakWorkingSetSize, WorkingSetSize, QuotaPeakPagedPoolUsage, QuotaPagedPoolUsage,
QuotaPeakNonPagedPoolUsage, QuotaNonPagedPoolUsage, PagefileUsage, PeakPagefileUsage, PrivateUsage;
} ProcessCounters;
/* (the process's memory counters as Windows keeps them; all zero when they cannot be read) */
static void process_counters(ProcessCounters *m) {
typedef BOOL(WINAPI * Fn)(HANDLE, ProcessCounters *, DWORD);
static Fn fn;
if (!fn) fn = (Fn)(void *)GetProcAddress(GetModuleHandleA("kernel32.dll"), "K32GetProcessMemoryInfo");
memset(m, 0, sizeof *m);
m->cb = sizeof *m;
if (!fn || !fn(GetCurrentProcess(), m, sizeof *m)) memset(m, 0, sizeof *m);
}
void hos_process_memory(uint64_t *private_bytes, uint64_t *working_set) {
ProcessCounters m;
process_counters(&m);
*private_bytes = m.PrivateUsage;
*working_set = m.WorkingSetSize;
}
/* ---- (Triforce, 11-22, the performance audit's OS-51, after Source B's) the host's part of a long frame --------------------
host/stall_host.h has what is counted and the line. The counters are here, and the look the present makes
(nvn_native.c nvnQueuePresentTexture, on the presenting thread): every present takes what was counted since the last
one, with the process's page faults and CPU time (one K32GetProcessMemoryInfo and one GetProcessTimes), and a present
that comes the stall line's threshold or more after the last one logs it. HOS_STALL_HOST=0, or RX_VK_STALL_LOG=0: none
of it, and neither system call */
StallHostCause hos_stall_host[STALL_HOST_CAUSES];
int64_t hos_stall_host_us = -1;
/* (the process's page faults so far, soft and hard together, and its CPU time in the kernel and in its own code, in ns;
zero where they cannot be read) */
static void process_work(uint64_t *page_faults, int64_t *kernel_ns, int64_t *user_ns) {
ProcessCounters m;
process_counters(&m);
*page_faults = m.PageFaultCount;
*kernel_ns = *user_ns = 0;
#if defined(_WIN32)
FILETIME made, ended, kernel, user;
if (GetProcessTimes(GetCurrentProcess(), &made, &ended, &kernel, &user)) {
*kernel_ns = (int64_t)(((uint64_t)kernel.dwHighDateTime << 32) | kernel.dwLowDateTime) * 100;
*user_ns = (int64_t)(((uint64_t)user.dwHighDateTime << 32) | user.dwLowDateTime) * 100;
}
#endif
}
void hos_stall_host_presented(void) {
void hos_vcore_waits_take(int64_t *ns, int64_t *count, int64_t *longest);
extern volatile LONG64 hos_present_count; /* display.c */
static int64_t last_ns, last_kernel, last_user;
static uint64_t last_faults;
static StallHostBound bound = STALL_HOST_BOUND;
const int64_t threshold_us = stall_host_us();
if (!threshold_us) return;
const int64_t now = hos_now_ns();
StallHostFrame f;
uint64_t faults;
int64_t kernel, user;
process_work(&faults, &kernel, &user);
for (int i = 0; i < STALL_HOST_CAUSES; i++) f.cause[i] = stall_host_take(&hos_stall_host[i]);
{ /* (the waits for a virtual core are counted where they end, with the virtual cores' lock held: hos_thread.c) */
StallHostCause *w = &f.cause[STALL_HOST_CORE_WAIT];
hos_vcore_waits_take(&w->ns, &w->count, &w->longest);
w->bytes = 0;
}
if (last_ns && now - last_ns >= threshold_us * 1000 && stall_host_bound_take(&bound, now)) {
char line[1024];
f.frame = (int64_t)hos_present_count, f.gap_ns = now - last_ns;
f.page_faults = (uint32_t)(faults - last_faults); /* (Windows' count is 32 bits and wraps) */
f.kernel_ns = kernel - last_kernel, f.user_ns = user - last_user;
f.held = bound.held, bound.held = 0;
stall_host_line(&f, line, sizeof line);
hos_log("%s", line);
}
last_ns = now, last_faults = faults, last_kernel = kernel, last_user = user;
}
/* ---- (Triforce) the guest memory census's thread (host/guest_census.h has why) ---------------------------------------------
HOS_MEM_CENSUS=<seconds> (5 by default; 0: none): every that many seconds a "mem: census" line: what the runtime handed out
of the guest window by kind (the program and the system region, the heap, the address regions and the game's pages in them,
what lies outside them: the physical maps and first touches), what the game counts against the memory it is told it has,
the pages it let go that stay committed and what the give-back gave back, the process's commit and working set, and the
PC's commit and RAM; then a "mem: census, the renderer" line with the renderer's big holders (gpu/vk/vk_memory.cpp
mkwvk_memory_holders). A count of the bits of the gigabytes in use (guest_census.h GC_WALK steps over the rest) and a few
system calls, on a thread of its own.
HOS_MEM_GIVE_BACK=1 (off by default, not yet run in the game): each pass first gives back up to 256 MB of the pages the game
let go that stay committed, where no import holds them (hos_mem.c hos_mem_give_back); =2: only while the PC is short, its
commit past 85% of its limit or under 1.5 GB of RAM available */
#include "guest_census.h"
void hos_mem_census(GcCensus *c); /* (hos_mem.c) */
int rx_memory_holders(char *out, size_t n); /* (rx_backend.c: 0 when there is no renderer's part) */
static int census_every = 5, census_line = 1, census_give_back;
/* The process's and the PC's figures, -1 where not known */
static void census_figures(GcCensus *c) {
c->proc_commit = c->proc_peak = c->working_set = c->sys_commit = c->sys_limit = c->ram_avail = c->ram_total = -1;
#ifdef _WIN32
typedef struct {
DWORD cb, PageFaultCount;
SIZE_T PeakWorkingSetSize, WorkingSetSize, QuotaPeakPagedPoolUsage, QuotaPagedPoolUsage,
QuotaPeakNonPagedPoolUsage, QuotaNonPagedPoolUsage, PagefileUsage, PeakPagefileUsage, PrivateUsage;
} Counters;
typedef struct { /* (PERFORMANCE_INFORMATION: pages) */
DWORD cb;
SIZE_T CommitTotal, CommitLimit, CommitPeak, PhysicalTotal, PhysicalAvailable, SystemCache, KernelTotal, KernelPaged,
KernelNonpaged, PageSize;
DWORD HandleCount, ProcessCount, ThreadCount;
} Perf;
typedef BOOL(WINAPI * ProcFn)(HANDLE, Counters *, DWORD);
typedef BOOL(WINAPI * PerfFn)(Perf *, DWORD);
static ProcFn proc;
static PerfFn perf;
static int looked;
if (!looked) {
looked = 1;
HMODULE k = GetModuleHandleA("kernel32.dll");
proc = (ProcFn)(void *)GetProcAddress(k, "K32GetProcessMemoryInfo");
perf = (PerfFn)(void *)GetProcAddress(k, "K32GetPerformanceInfo");
}
Counters m;
memset(&m, 0, sizeof m);
m.cb = sizeof m;
if (proc && proc(GetCurrentProcess(), &m, sizeof m))
c->proc_commit = (int64_t)m.PrivateUsage, c->proc_peak = (int64_t)m.PeakPagefileUsage, c->working_set = (int64_t)m.WorkingSetSize;
Perf p;
memset(&p, 0, sizeof p);
p.cb = sizeof p;
if (perf && perf(&p, sizeof p) && p.PageSize) {
c->sys_commit = (int64_t)(p.CommitTotal * p.PageSize), c->sys_limit = (int64_t)(p.CommitLimit * p.PageSize);
c->ram_avail = (int64_t)(p.PhysicalAvailable * p.PageSize), c->ram_total = (int64_t)(p.PhysicalTotal * p.PageSize);
}
#else
/* (linux: the PC's committed memory and its limit, /proc/meminfo; the process's resident set, /proc/self/status) */
FILE *f = fopen("/proc/meminfo", "r");
if (f) {
char line[128];
unsigned long long kb;
while (fgets(line, sizeof line, f)) {
if (sscanf(line, "MemTotal: %llu kB", &kb) == 1) c->ram_total = (int64_t)kb << 10;
else if (sscanf(line, "MemAvailable: %llu kB", &kb) == 1) c->ram_avail = (int64_t)kb << 10;
else if (sscanf(line, "Committed_AS: %llu kB", &kb) == 1) c->sys_commit = (int64_t)kb << 10;
else if (sscanf(line, "CommitLimit: %llu kB", &kb) == 1) c->sys_limit = (int64_t)kb << 10;
}
fclose(f);
}
if ((f = fopen("/proc/self/status", "r"))) {
char line[128];
unsigned long long kb;
while (fgets(line, sizeof line, f)) {
if (sscanf(line, "VmRSS: %llu kB", &kb) == 1) c->working_set = (int64_t)kb << 10;
else if (sscanf(line, "VmHWM: %llu kB", &kb) == 1) c->proc_peak = (int64_t)kb << 10;
}
fclose(f);
}
#endif
}
static DWORD WINAPI census_main(void *unused) {
(void)unused;
hos_host_thread_register_self("hos memory census");
for (;;) {
Sleep((DWORD)census_every * 1000);
GcCensus c;
memset(&c, 0, sizeof c);
census_figures(&c);
if (census_give_back > 0) { /* (first, so that the line says what it did) */
const int short_now = (c.sys_limit > 0 && c.sys_commit > c.sys_limit / 100 * 85) ||
(c.ram_avail >= 0 && c.ram_avail < (1536LL << 20));
if (census_give_back == 1 || short_now) {
if (hos_mem_give_back(256ULL << 20) < 0) {
hos_log("mem: the give-back is off: the renderer cannot say which pages its imports hold (no mkwvk_guest_release)");
census_give_back = 0;
}
}
}
if (!census_line) continue;
hos_mem_census(&c);
char line[1024];
gc_line(line, sizeof line, &c);
hos_log("mem: %s", line);
char holders[1000];
if (rx_memory_holders(holders, sizeof holders) > 0) hos_log("mem: census, the renderer (MB): %s", holders);
}
return 0;
}
void hos_mem_census_start(void) {
const char *e = getenv("HOS_MEM_CENSUS"), *g = getenv("HOS_MEM_GIVE_BACK");
census_line = !(e && atoi(e) <= 0);
census_every = e && atoi(e) > 0 ? atoi(e) : 5;
census_give_back = g ? atoi(g) : 0;
if (!census_line && census_give_back <= 0) return;
char every[32] = "off";
if (census_line) snprintf(every, sizeof every, "every %d s", census_every);
hos_log("mem: census %s; give-back %s (HOS_MEM_CENSUS, HOS_MEM_GIVE_BACK)", every,
census_give_back == 1 ? "on" : census_give_back >= 2 ? "while the PC is short" : "off");
CloseHandle(CreateThread(NULL, 0, census_main, NULL, 0, NULL));
}