mirror of
https://github.com/valentderah/totk-pc-recomp.git
synced 2026-11-24 21:55:57 +00:00
heap_lazy = 1 commits the game's heap a 1 MiB chunk at a time on first use (Windows, opt-in)
SetHeapSize takes the game's whole heap at boot (2.1 GiB on 1.4.3) and Windows charges all of it against the commit limit, although a sky-island run touched about two thirds. With TOTK_HEAP_LAZY=1 the heap stays reserved: the first read or write of a chunk faults and memory.c heap_fault commits it; read faults reach the hook only in this mode (plat_mem_fault_reads). A byte per chunk (lazy_map) answers without asking the OS, so frozen-region write faults pay nothing for it, and only clear chunks are committed, never pages a freeze made read-only. mem_thaw, which file reads, save writes and Vulkan imports call before the kernel or driver touches guest memory, commits its range's chunks first; ipc.c and svc.c check readability by the layout; vk_mem_host_room counts the uncommitted heap as taken. Default off: the heap path is unchanged unless set. Sky island, 120 s, this laptop (Iris Xe): peak commit 6345-6536 MiB on dev, 5736-5821 MiB with heap_lazy = 1; about 600 first-use faults, 8 ms in all.
This commit is contained in:
1 parent
afae80c908
commit
6370c7c0b6
11 files changed
+142
-8
No files matched your search
+1
-1
@@ -52,7 +52,7 @@ static const char *const known[] = {
|
||||
"GAMEPLAY_WEAPON_WHEEL",
|
||||
"GPL", "GPL_DRAW_BUILD", "GPU", "GPU_INDEX", "GPU_NAME", "GRASS_RANGE",
|
||||
"GYRO_AUTO_BIAS", "GYRO_BIAS_PAD", "GYRO_BIAS_X", "GYRO_BIAS_Y", "GYRO_BIAS_Z", "GYRO_CALIBRATION", "HDR", "HDR_GAME_NITS", "HDR_HUE",
|
||||
"HDR_PEAK_NITS", "HDR_UI_NITS", "HIGHFPS_FIX", "HUD_WIDTH", "INPUT", "INPUT_RECORD", "INPUT_SCRIPT", "KSPIN_US", "LANG", "LIB_PREBUILD",
|
||||
"HDR_PEAK_NITS", "HDR_UI_NITS", "HEAP_LAZY", "HIGHFPS_FIX", "HUD_WIDTH", "INPUT", "INPUT_RECORD", "INPUT_SCRIPT", "KSPIN_US", "LANG", "LIB_PREBUILD",
|
||||
"LIB_PREBUILD_KEEP", "LIB_PREBUILD_THREADS", "LOCALE", "LOD_GUARD", "LOD_STEADY", "LSTICK_DEADZONE", "MAP_CLICK", "MEMORY", "MEMX", "MENU_LANG", "MENU_TEST", "MENU_FONT_FONTCONFIG",
|
||||
"MEM_RESERVE_MIB", "MENU_CURSOR", "MENU_CURSOR_LOG", "MENU_TEST_DIR", "MENU_TEST_SET", "MINIMAL_LAYOUT", "MIRROR_GAP", "MODS_DIR", "MODS_DISABLED", "MODS_NO_CODE", "MODS_ORDER", "MODS_REBUILD", "MODS_WITHOUT_CODE", "MOTION", "MOUSE_AIM_SCALE", "MOUSE_MODE", "MOUSE_DIRECT_PATCH", "MOUSE_SENS", "MOVIE_ASPECT", "MOVIE_HOST", "MOVIE_PACE",
|
||||
"MVOBJ_ALL", "MVOBJ_BANKS", "MVOBJ_EQUAL", "MVOBJ_EXACT", "MVOBJ_EXEC", "MVOBJ_NOC4", "MVOBJ_ONLY", "MVOBJ_TIMES", "MVPREV_CHECK", "MVPREV_GROW", "MVPREV_SKIP", "MVPREV_TRUST", "MV_CHECK",
|
||||
|
||||
@@ -1013,6 +1013,7 @@ static uint32_t write_file(uint64_t h, int64_t offset, const void *buffer, uint6
|
||||
if (!plat_file_id(f, &id)) rt_fatal("fs: host save size");
|
||||
if ((uint64_t)offset + size > id.size && !(mode & FS_OPEN_APPEND)) return FS_R_APPEND_REQUIRED;
|
||||
if (file->save) save_touched();
|
||||
mem_thaw((uintptr_t)buffer, size); // the kernel reads the buffer: heap pages never used yet must be committed
|
||||
for (uint64_t done = 0; done < size;) {
|
||||
uint32_t chunk = size - done > 0x40000000 ? 0x40000000 : (uint32_t)(size - done);
|
||||
int64_t put = plat_file_pwrite(f, (const uint8_t *)buffer + done, chunk, (uint64_t)offset + done);
|
||||
|
||||
@@ -177,6 +177,8 @@ void mem_thaw(uint64_t a, uint64_t n); // makes the frozen r
|
||||
void mem_freeze_stats(uint64_t *frozen, uint64_t *freezes, uint64_t *fault_thaws, uint64_t *other_thaws);
|
||||
// regions frozen now; freezes and thaws since the last call
|
||||
void mem_kernel_free(void *p, uint64_t size); // TLR/TLS reuse after thread exit
|
||||
uint64_t mem_heap_uncommitted(void); // heap bytes set by SetHeapSize but not committed yet
|
||||
// (TOTK_HEAP_LAZY): commit the game can still claim
|
||||
int mem_guest_readable(uint64_t a, uint64_t n); // [a, a+n) can be read without a fault: inside a loaded
|
||||
// module or the committed heap answered from the layout,
|
||||
// anything else by plat_mem_readable (an OS query)
|
||||
|
||||
+2
-1
@@ -66,7 +66,8 @@ void ipc_handle_closed(int kind, void *obj) {
|
||||
}
|
||||
|
||||
// ---- helpers for services
|
||||
static int readable(uint64_t a, uint64_t n) { return plat_mem_readable(a, n); }
|
||||
// The heap by the layout (its pages may not be committed before first use), anything else by the OS.
|
||||
static int readable(uint64_t a, uint64_t n) { return mem_guest_readable(a, n); }
|
||||
|
||||
void ipc_backtrace(void) {
|
||||
const Cpu *c = t_req;
|
||||
|
||||
+114
-3
@@ -108,10 +108,13 @@ static void thaw_range_locked(uint64_t a, uint64_t n, int why) {
|
||||
for (uint64_t r = r0; r <= r1; r++) thaw_locked(r, why);
|
||||
}
|
||||
|
||||
static void heap_commit_range(uint64_t a, uint64_t n);
|
||||
|
||||
void mem_thaw(uint64_t a, uint64_t n) {
|
||||
plat_mutex_lock(&frz_lock); // also when nothing is frozen: the hold keeps a read's buffer writable
|
||||
thaw_range_locked(a, n, 2);
|
||||
plat_mutex_unlock(&frz_lock);
|
||||
heap_commit_range(a, n);
|
||||
}
|
||||
|
||||
// frz_lock held exclusively. Makes the committed pages of region r read-only when every one of them is read-write;
|
||||
@@ -200,6 +203,94 @@ static void commit(uint64_t a, uint64_t n, const char *what) {
|
||||
rt_fatal("memory: commit %s %#llx+%#llx failed", what, (unsigned long long)a, (unsigned long long)n);
|
||||
}
|
||||
|
||||
// ---- heap pages committed on first use (Windows)
|
||||
// SetHeapSize asks for the game's whole heap at once (1.4.3: about 2.1 GiB at boot), and Windows charges every
|
||||
// committed page against the system's commit limit, backed by RAM or the page file, whether the game touches it or not.
|
||||
// In a sky-island scene it had touched about two thirds. With TOTK_HEAP_LAZY=1 (Windows, off by default) the part
|
||||
// SetHeapSize adds stays reserved: the first read or write of a reserved page faults, and heap_fault commits its
|
||||
// LAZY_CHUNK and retries. Freshly committed pages read as zero, as the Switch's heap does. Access by the kernel or the GPU
|
||||
// driver does not fault: a file read into the heap, a save written from it and a Vulkan import of it would fail on a
|
||||
// reserved page. All of them call mem_thaw first, so mem_thaw commits the reserved chunks of its range too
|
||||
// (heap_commit_range). Linux backs mprotect'ed pages on first use by itself and keeps committing eagerly.
|
||||
//
|
||||
// lazy_map holds one byte per chunk, set once the chunk is committed, so neither path asks the OS for the page state:
|
||||
// a VirtualQuery costs tens of microseconds here, and the write faults of frozen pool regions (thaw_fault) come through
|
||||
// heap_fault first. Only whole chunks are committed and only when their byte is clear: VirtualAlloc(MEM_COMMIT) over
|
||||
// committed pages would set them read-write again and undo a freeze.
|
||||
#ifndef LAZY_CHUNK
|
||||
#define LAZY_CHUNK 0x100000ull // divides the 2 MiB granularity of SetHeapSize
|
||||
#endif
|
||||
static int heap_lazy;
|
||||
static uint8_t *lazy_map; // per chunk of L.heap: 1 committed; written under lazy_lock
|
||||
static PlatMutex lazy_lock = PLAT_MUTEX_INIT; // commits and the heap's shrink; never held across a guest access
|
||||
static volatile uint64_t heap_committed; // bytes of committed chunks (lazy mode), exact
|
||||
static volatile int64_t lazy_faults, lazy_ns;
|
||||
|
||||
// Commits the clear chunks among [i0, i1) of the heap's first `size` bytes, runs of them in one call.
|
||||
static void heap_commit_chunks(uint64_t i0, uint64_t i1, uint64_t size, const char *what) {
|
||||
uint64_t base = L.heap.base, top = base + size;
|
||||
plat_mutex_lock(&lazy_lock);
|
||||
for (uint64_t i = i0; i < i1;) {
|
||||
if (lazy_map[i]) { i++; continue; }
|
||||
uint64_t j = i;
|
||||
while (j < i1 && !lazy_map[j]) j++;
|
||||
uint64_t lo = base + i * LAZY_CHUNK, hi = base + j * LAZY_CHUNK;
|
||||
if (hi > top) hi = top;
|
||||
commit(lo, hi - lo, what);
|
||||
__atomic_add_fetch(&heap_committed, hi - lo, __ATOMIC_RELAXED);
|
||||
for (uint64_t k = i; k < j; k++) __atomic_store_n(&lazy_map[k], 1, __ATOMIC_RELEASE);
|
||||
i = j;
|
||||
}
|
||||
plat_mutex_unlock(&lazy_lock);
|
||||
}
|
||||
|
||||
static void heap_commit_range(uint64_t a, uint64_t n) {
|
||||
if (!heap_lazy || !n) return;
|
||||
uint64_t base = L.heap.base, size = __atomic_load_n(&heap_size, __ATOMIC_ACQUIRE), top = base + size;
|
||||
uint64_t lo = a > base ? a : base, hi = a + n < a || a + n > top ? top : a + n;
|
||||
if (lo >= hi) return;
|
||||
uint64_t i0 = (lo - base) / LAZY_CHUNK, i1 = (hi - base + LAZY_CHUNK - 1) / LAZY_CHUNK, i = i0;
|
||||
while (i < i1 && __atomic_load_n(&lazy_map[i], __ATOMIC_ACQUIRE)) i++; // the usual case: all committed, no lock
|
||||
if (i < i1) heap_commit_chunks(i, i1, size, "heap pages for a file, save or import");
|
||||
}
|
||||
|
||||
// The platform's fault hook, ahead of thaw_fault: 1 retries the access. A committed chunk's fault is not this one's
|
||||
// (a frozen page, or a guest error); heap_fault_raced catches a chunk committed by another thread in between.
|
||||
static int heap_fault(uint64_t a) {
|
||||
if (!heap_lazy) return 0;
|
||||
uint64_t base = L.heap.base, size = __atomic_load_n(&heap_size, __ATOMIC_ACQUIRE);
|
||||
if (a < base || a - base >= size) return 0;
|
||||
uint64_t i = (a - base) / LAZY_CHUNK;
|
||||
if (__atomic_load_n(&lazy_map[i], __ATOMIC_ACQUIRE)) return 0;
|
||||
uint64_t t0 = plat_now_ns();
|
||||
heap_commit_chunks(i, i + 1, size, "heap on first use");
|
||||
int64_t ns = (int64_t)__atomic_add_fetch(&lazy_ns, (int64_t)(plat_now_ns() - t0), __ATOMIC_RELAXED);
|
||||
int64_t n = (int64_t)plat_inc64(&lazy_faults);
|
||||
if (!(n % (int64_t)((512ull << 20) / LAZY_CHUNK))) // every 512 MiB of chunks: how often and at what cost
|
||||
rt_log("memory: heap committed on first use: %lld faults, %.1f ms in all (%.1f us each); heap %llu of %llu MiB "
|
||||
"committed", (long long)n, (double)ns / 1e6, (double)ns / 1e3 / (double)n,
|
||||
(unsigned long long)(__atomic_load_n(&heap_committed, __ATOMIC_RELAXED) >> 20),
|
||||
(unsigned long long)(size >> 20));
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Last, after thaw_fault declined: a chunk another thread committed between this thread's fault and heap_fault's read
|
||||
// of lazy_map. Asks the OS, which only the faults no hook takes (a crash on its way) pay for.
|
||||
static int heap_fault_raced(uint64_t a) {
|
||||
uint64_t base = L.heap.base, size = __atomic_load_n(&heap_size, __ATOMIC_ACQUIRE), end;
|
||||
if (!heap_lazy || a < base || a - base >= size) return 0;
|
||||
return plat_mem_state(a & ~(PAGE - 1), (a & ~(PAGE - 1)) + PAGE, &end) == PLAT_PROT_RW;
|
||||
}
|
||||
|
||||
uint64_t mem_heap_uncommitted(void) {
|
||||
if (!heap_lazy) return 0;
|
||||
uint64_t size = __atomic_load_n(&heap_size, __ATOMIC_ACQUIRE), done = __atomic_load_n(&heap_committed, __ATOMIC_RELAXED);
|
||||
return size > done ? size - done : 0;
|
||||
}
|
||||
|
||||
static int thaw_fault(uint64_t a);
|
||||
static int mem_fault(uint64_t a) { return heap_fault(a) || thaw_fault(a) || heap_fault_raced(a); }
|
||||
|
||||
static void decommit(uint64_t a, uint64_t n, const char *what) {
|
||||
plat_mutex_lock(&frz_lock);
|
||||
thaw_range_locked(a, n, 3);
|
||||
@@ -253,12 +344,17 @@ void mem_init(void) {
|
||||
if (reserve_at(at, ALIAS_SIZE, 1)) alias = at;
|
||||
if (!alias) reexec_or_fail("a 64 GiB alias region below 2^39");
|
||||
|
||||
plat_mem_fault_hook(thaw_fault); // before the crash handler
|
||||
plat_mem_fault_hook(mem_fault); // before the crash handler
|
||||
alias_pages = calloc(ALIAS_PAGES, 1);
|
||||
if (!alias_pages) rt_fatal("memory: cannot allocate the alias page map");
|
||||
// totk.ini is read here, once the address space is claimed and before the first setting: the pool size below
|
||||
// depends on TOTK_APP_POOL and on the resolution settings (resolution.c decides them once, on this first call).
|
||||
cfg_init();
|
||||
#ifdef _WIN32
|
||||
const char *lazy = cfg_get("TOTK_HEAP_LAZY");
|
||||
heap_lazy = lazy && atoi(lazy) != 0; // opt-in: an untouched heap page then faults on its first read too
|
||||
plat_mem_fault_reads(heap_lazy);
|
||||
#endif
|
||||
const char *pool = cfg_get("TOTK_APP_POOL");
|
||||
L = (RtGuestLayout){
|
||||
.aslr = {low, RT_ADDRESS_LIMIT - low},
|
||||
@@ -271,6 +367,8 @@ void mem_init(void) {
|
||||
: DEFAULT_POOL + res_heap_extra_bytes() + res_tex_heap_extra_bytes() + res_grass_heap_extra_bytes(),
|
||||
};
|
||||
if (!L.pool) rt_fatal("memory: TOTK_APP_POOL is 0");
|
||||
if (heap_lazy && !(lazy_map = calloc(L.heap.size / LAZY_CHUNK + 1, 1)))
|
||||
rt_fatal("memory: cannot allocate the heap chunk map");
|
||||
}
|
||||
|
||||
const RtGuestLayout *mem_layout(void) { return &L; }
|
||||
@@ -306,8 +404,21 @@ uint32_t mem_set_heap_size(uint64_t size, uint64_t *addr) {
|
||||
plat_mutex_unlock(&lock);
|
||||
return R_INVALID_STATE;
|
||||
}
|
||||
if (size > heap_size) commit(L.heap.base + heap_size, size - heap_size, "heap");
|
||||
if (size < heap_size) decommit(L.heap.base + size, heap_size - size, "heap");
|
||||
if (size > heap_size && !heap_lazy) commit(L.heap.base + heap_size, size - heap_size, "heap"); // else heap_fault
|
||||
if (size < heap_size && heap_lazy) { // the chunks above the new size go back to reserved
|
||||
plat_mutex_lock(&lazy_lock);
|
||||
for (uint64_t i = size / LAZY_CHUNK, end = (heap_size + LAZY_CHUNK - 1) / LAZY_CHUNK; i < end; i++)
|
||||
if (lazy_map[i]) {
|
||||
uint64_t lo = L.heap.base + i * LAZY_CHUNK, hi = lo + LAZY_CHUNK;
|
||||
if (hi > L.heap.base + heap_size) hi = L.heap.base + heap_size;
|
||||
decommit(lo, hi - lo, "heap");
|
||||
__atomic_sub_fetch(&heap_committed, hi - lo, __ATOMIC_RELAXED);
|
||||
__atomic_store_n(&lazy_map[i], 0, __ATOMIC_RELEASE);
|
||||
}
|
||||
plat_mutex_unlock(&lazy_lock);
|
||||
} else if (size < heap_size) {
|
||||
decommit(L.heap.base + size, heap_size - size, "heap");
|
||||
}
|
||||
heap_size = size;
|
||||
plat_mutex_unlock(&lock);
|
||||
if (addr) *addr = L.heap.base;
|
||||
|
||||
@@ -985,6 +985,8 @@ void plat_mem_fault_hook(int (*fn)(uint64_t addr)) {
|
||||
fault_install();
|
||||
}
|
||||
|
||||
void plat_mem_fault_reads(int on) { (void)on; }
|
||||
|
||||
void plat_crash_hook(void (*fn)(const PlatCrash *c)) {
|
||||
crash_fn = fn;
|
||||
fault_install();
|
||||
|
||||
@@ -220,6 +220,9 @@ uint64_t plat_mem_written(uint64_t addr, uint64_t n, uint64_t *pages, uint64_t m
|
||||
// A write that faulted on a read-only page at addr goes to fn first; fn returns 1 when it made the page writable and
|
||||
// the write should run again, 0 to let the fault go on (to the crash handler). One hook per process.
|
||||
void plat_mem_fault_hook(int (*fn)(uint64_t addr));
|
||||
// Read faults go to the hook too (memory.c heap_fault, TOTK_HEAP_LAZY): Windows only, before the game starts; a no-op
|
||||
// on Linux, which commits on first use by itself.
|
||||
void plat_mem_fault_reads(int on);
|
||||
// Starts this program again with the same arguments and ends this process with the new one's exit code (Windows), or
|
||||
// replaces this process with it (Linux). Environment changes made before the call are passed on.
|
||||
_Noreturn void plat_reexec(void);
|
||||
|
||||
@@ -721,10 +721,16 @@ uint64_t plat_mem_written(uint64_t addr, uint64_t n, uint64_t *pages, uint64_t m
|
||||
|
||||
static int (*fault_hook)(uint64_t addr);
|
||||
|
||||
// A write access violation (ExceptionInformation[0] == 1) goes to the hook; registered first, before the crash handler.
|
||||
static ULONG_PTR fault_kinds = 1; // highest ExceptionInformation[0] the hook takes: 1 writes, 0 also reads
|
||||
|
||||
// A write access violation (ExceptionInformation[0] == 1) goes to the hook, and a read (0) too once
|
||||
// plat_mem_fault_reads asked for it; registered first, before the crash handler. Writes thaw frozen regions; with
|
||||
// TOTK_HEAP_LAZY reads and writes both commit heap pages on first use (memory.c, heap_fault). An execute fault (8) is
|
||||
// never the hook's.
|
||||
static LONG CALLBACK fault_veh(EXCEPTION_POINTERS *ep) {
|
||||
const EXCEPTION_RECORD *er = ep->ExceptionRecord;
|
||||
if (er->ExceptionCode != EXCEPTION_ACCESS_VIOLATION || er->NumberParameters < 2 || er->ExceptionInformation[0] != 1)
|
||||
ULONG_PTR kind = er->NumberParameters >= 2 ? er->ExceptionInformation[0] : 8;
|
||||
if (er->ExceptionCode != EXCEPTION_ACCESS_VIOLATION || kind > 1 || (kind == 0 && fault_kinds != 0))
|
||||
return EXCEPTION_CONTINUE_SEARCH;
|
||||
return fault_hook((uint64_t)er->ExceptionInformation[1]) ? EXCEPTION_CONTINUE_EXECUTION : EXCEPTION_CONTINUE_SEARCH;
|
||||
}
|
||||
@@ -734,6 +740,8 @@ void plat_mem_fault_hook(int (*fn)(uint64_t addr)) {
|
||||
AddVectoredExceptionHandler(1, fault_veh);
|
||||
}
|
||||
|
||||
void plat_mem_fault_reads(int on) { fault_kinds = on ? 0 : 1; }
|
||||
|
||||
static void (*crash_fn)(const PlatCrash *c);
|
||||
|
||||
// The faulting thread's general registers, for the report to tie the faulting instruction to its operands.
|
||||
|
||||
+2
-1
@@ -31,7 +31,8 @@ RtStats rt_stats(void) {
|
||||
return (RtStats){__atomic_load_n(&g_svcs, __ATOMIC_RELAXED), kern_threads_created(), hostfn_calls()};
|
||||
}
|
||||
|
||||
static int readable(uint64_t a, uint64_t n) { return plat_mem_readable(a, n); }
|
||||
// The heap by the layout (its pages may not be committed before first use), anything else by the OS.
|
||||
static int readable(uint64_t a, uint64_t n) { return mem_guest_readable(a, n); }
|
||||
|
||||
void kern_backtrace(const Cpu *c) {
|
||||
char d[160];
|
||||
|
||||
@@ -643,6 +643,10 @@ uint64_t vk_mem_host_room(void) {
|
||||
if (at && now - at < 250) return __atomic_load_n(&room_cached, __ATOMIC_RELAXED); // a query costs a system call
|
||||
uint64_t commit, phys;
|
||||
plat_mem_room(&commit, &phys);
|
||||
// The game's heap commits on first use (memory.c, heap_fault): the part it has not touched yet is commit it can
|
||||
// still claim, which an eagerly committed heap had already taken. Its physical memory was never taken either way.
|
||||
uint64_t later = mem_heap_uncommitted();
|
||||
if (commit != UINT64_MAX) commit = commit > later ? commit - later : 0;
|
||||
uint64_t r = host_backed == HB_UMA && phys < commit ? phys : commit;
|
||||
r = r == UINT64_MAX ? r : r > room_reserve ? r - room_reserve : 0;
|
||||
__atomic_store_n(&room_cached, r, __ATOMIC_RELAXED);
|
||||
|
||||
Reference in new issue
Block a user