diff --git a/docs/settings.md b/docs/settings.md index b8e23c26..19ff6519 100644 --- a/docs/settings.md +++ b/docs/settings.md @@ -221,7 +221,7 @@ Many rows below are developer tools that only act together with another setting | --- | --- | --- | --- | | `app_pool` | Size in bytes of the guest application memory pool; accepts decimal or `0x` hex. | The default depends on the resolution settings; `0` stops the start with a message, and too small a pool leaves the game without memory. | Risky | | `audio_off` | Any value mixes audio without opening an output device. | The game runs in silence and reports two channels; same as `audio = none`. | Caution | -| `heap_lazy` (default `0`) | The game's heap is committed a 64 KiB chunk at a time when the game first touches it, instead of all at once when the game sets its size (about 2.1 GiB at boot on 1.4.3). File reads, save writes and Vulkan imports commit their pages first. Windows only; Linux already backs pages on first use. | `1` lowers the game's committed memory (RAM plus page file) by the heap the game never touches (sky island: peak commit about 6.4 to 5.8 GiB), which helps PCs whose commit limit is near the game's needs; each first touch of a 1 MiB chunk costs a fault and a commit (about 600 a run, 8 ms in all). | Caution | +| `heap_lazy` (default `0`) | The game's heap is committed a 1 MiB chunk at a time when the game first touches it, instead of all at once when the game sets its size (about 2.1 GiB at boot on 1.4.3). File reads, save writes and Vulkan imports commit their pages first. Windows only; Linux already backs pages on first use. | `1` lowers the game's committed memory (RAM plus page file) by the heap the game never touches (sky island: peak commit about 6.4 to 5.8 GiB), which helps PCs whose commit limit is near the game's needs; each first touch of a 1 MiB chunk costs a fault and a commit (about 600 a run, 8 ms in all). | Caution | | `kspin_us` (default `20`) | Microseconds a guest wait or a wait for a virtual core polls before it sleeps; `0` turns the poll off; `ab` polls only in the B blocks of `ab`. Off with fewer than 6 logical processors. | A wake within the poll costs no OS wake-up; a longer poll takes more CPU time from other threads. | Caution | | `mem_reserve_mib` (default `1536`) | MiB of system memory kept free for Windows, other programs and the game's own heap when the renderer sizes what it keeps in device memory, where device memory takes system memory (integrated GPUs, and discrete GPUs whose driver charges it to the process). | A larger value keeps fewer released textures and spare blocks when the system runs short; a smaller one lets the game use more before it holds back. | Caution | | `one_per_core` (default `0`) | `1` keeps the process on the first logical processor of each physical core; `ab` switches it with the `ab` blocks. | Busy threads stop sharing a core with a sibling thread, which can help or hurt on the CPU; it only applies when the CPU has hyperthreads. | Caution | diff --git a/runtime/fs_host.c b/runtime/fs_host.c index 69c2883a..adc75160 100644 --- a/runtime/fs_host.c +++ b/runtime/fs_host.c @@ -1024,7 +1024,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 + mem_heap_commit((uintptr_t)buffer, size); // TOTK_HEAP_LAZY: the kernel reads the buffer, so its chunks must exist 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); diff --git a/runtime/host.h b/runtime/host.h index 6d6aef81..359b376f 100644 --- a/runtime/host.h +++ b/runtime/host.h @@ -177,6 +177,9 @@ 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 +int mem_heap_lazy(void); // TOTK_HEAP_LAZY is on (Windows): heap pages commit on first use +void mem_heap_commit(uint64_t a, uint64_t n); // commits the not yet committed heap chunks of [a, a+n) before + // the kernel reads them; nothing when mem_heap_lazy is 0 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 diff --git a/runtime/ipc.c b/runtime/ipc.c index 77bcbe30..e52ef09d 100644 --- a/runtime/ipc.c +++ b/runtime/ipc.c @@ -66,8 +66,11 @@ void ipc_handle_closed(int kind, void *obj) { } // ---- helpers for services -// 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); } +// With TOTK_HEAP_LAZY the heap by the layout (its pages may not be committed before first use), anything else by +// the OS; without it the OS for everything, as before. +static int readable(uint64_t a, uint64_t n) { + return mem_heap_lazy() ? mem_guest_readable(a, n) : plat_mem_readable(a, n); +} void ipc_backtrace(void) { const Cpu *c = t_req; diff --git a/runtime/memory.c b/runtime/memory.c index fdb3a6b9..5399432e 100644 --- a/runtime/memory.c +++ b/runtime/memory.c @@ -282,6 +282,10 @@ static int heap_fault_raced(uint64_t a) { return plat_mem_state(a & ~(PAGE - 1), (a & ~(PAGE - 1)) + PAGE, &end) == PLAT_PROT_RW; } +int mem_heap_lazy(void) { return heap_lazy; } + +void mem_heap_commit(uint64_t a, uint64_t n) { heap_commit_range(a, n); } + 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); diff --git a/runtime/svc.c b/runtime/svc.c index ff6503a2..25d41abe 100644 --- a/runtime/svc.c +++ b/runtime/svc.c @@ -31,8 +31,11 @@ RtStats rt_stats(void) { return (RtStats){__atomic_load_n(&g_svcs, __ATOMIC_RELAXED), kern_threads_created(), hostfn_calls()}; } -// 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); } +// With TOTK_HEAP_LAZY the heap by the layout (its pages may not be committed before first use), anything else by +// the OS; without it the OS for everything, as before. +static int readable(uint64_t a, uint64_t n) { + return mem_heap_lazy() ? mem_guest_readable(a, n) : plat_mem_readable(a, n); +} void kern_backtrace(const Cpu *c) { char d[160];