From dda12051f59948b651171fd2fa01a14546942c2c Mon Sep 17 00:00:00 2001 From: chrg6 Date: Tue, 10 Nov 2026 23:04:40 -0500 Subject: [PATCH] Add heap_lazy (off by default): on Windows the game's heap is committed where it is first used. Windows charges every committed page to the commit limit whether it is used or not, and the game sizes its whole heap up front: in Castle Town after Hyrule Field the heap held 6.8 GiB of commit of which 3.4 GiB had ever been used. With heap_lazy = 1 a change of the heap's size only moves its end, an access to a reserved heap page commits the reserved runs of the 64 KiB around it in the access-fault hook and runs again, and the paths that do not fault commit their range first: mem_thaw (file reads, the host-pointer import), mem_kernel_access (file writes, the frame-rate fix's read) and mem_host_readable (the host's pointer checks). A page another path committed is left as it is, so a frozen, probed or armed page keeps its protection. With trace = perf a line per window gives the faults, the bytes committed and the time they took. The contributor's runs on a 16 GB PC: 4.67 instead of 6.77 GiB of heap committed and 29.3 instead of 32.9 GiB of process commit in Castle Town, no "system commit low", 12,858 faults in 440 ms over four minutes, the longest 41 ms. With the setting off nothing is hooked and the heap is committed as before. Co-Authored-By: Claude Fable 5.2 --- nx2navi.ini.example | 5 ++ runtime/cfg.c | 6 +-- runtime/frame_rate.c | 9 +++- runtime/fs_host.c | 4 +- runtime/host.h | 8 +++- runtime/ipc.c | 2 +- runtime/memory.c | 106 ++++++++++++++++++++++++++++++++++++++++-- runtime/menu_cursor.c | 4 +- runtime/perf.c | 2 + runtime/svc.c | 2 +- 10 files changed, 133 insertions(+), 15 deletions(-) diff --git a/nx2navi.ini.example b/nx2navi.ini.example index b4150bda..284a6054 100644 --- a/nx2navi.ini.example +++ b/nx2navi.ini.example @@ -758,6 +758,11 @@ # first one, so their draw pass is not ended and begun again at each of them (about 40 passes fewer a frame in a # village). 0: each such draw ends its pass and prepares its own count, as before. # mesh_ahead = 1 +# Windows, 1 (experimental): the game's heap is committed a 64 KiB run at a time where it is first used, instead of +# all of it when the game sizes it. Windows counts committed memory against RAM plus the page file whether it is used +# or not, and in Castle Town after Hyrule Field about 3.4 of the heap's 6.8 GiB had never been used. 0: committed up +# front. Linux commits pages on first use anyway. +# heap_lazy = 0 # Linux, shadow storage: 1 clears the write watch's protection of a range in one call before the GPU's bytes are copied # back into the game's memory, instead of taking a fault at the first store into each page. # shadow_bulk_write = 0 diff --git a/runtime/cfg.c b/runtime/cfg.c index 47c4e859..d0ed5fd9 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -46,7 +46,7 @@ static const char *const known[] = { "FLIGHT_SPIKE_MS", "FLIGHT_THUMBS", "FLIGHT_EVERY", "FLUSH_SPAN", "FLUSH_UPLOAD", "FOG_CULL", "FOLIAGE_SHADOW_CULL", "FOLIAGE_SHADOW_VRS", "FOV", "FOV_PATCH", "FG_HUDLESS_SHOW", "FG_MULT", "FPS60", "FPS_LIMIT", "FRAMEGEN", "FRAMEGEN_MULTIPLIER", "FREEZE_IDLE", "FREE_CORES", "FULLSCREEN", "FXAA", "GAME_ASPECT", "GAME_RES", "GPA", "GPA_EMPTY", "GPA_TWIN", "GPA_TWIN_MAX_KB", "GPL", "GPL_DRAW_BUILD", "GPL_FEEDBACK", "PIPE_AHEAD", "PIPE_FAST_WORKERS", "PIPE_FRAME_WAIT_MS", "PIPE_LINK_DRAW_MS", "PIPE_SKIP_STEADY", "PROG_LEAD", "GPU", "GPU_INDEX", "GPU_PASSES", "GPU_NAME", "HDR", "HDR_BLOWOUT", "HDR_CONTRAST", "HDR_EXPOSURE", "HDR_FLARE", "HDR_GAMMA", "HDR_GAME_NITS", "HDR_GRADE", "HDR_HIGHLIGHT_SAT", "HDR_HIGHLIGHTS", "HDR_HUE", - "HDR_LIVE", + "HDR_LIVE", "HEAP_LAZY", "HDR_PEAK_NITS", "HDR_SATURATION", "HDR_SHADOWS", "HDR_TONEMAP", "HDR_UI_NITS", "HIGHFPS_FIX", "INPUT", "INPUT_RECORD", "INPUT_SCRIPT", "LANG", "LIB_PREBUILD", "BIG_BUILD_FREE_MB", "BIG_BUILD_MAX_MB", "LIB_PREBUILD_BIG", "LIB_PREBUILD_BIG_BG", "LIB_PREBUILD_KEEP", "LIB_PREBUILD_THREADS", "LIB_WARM", "PIPE_LINK_MS", "FRAME_HOLD_MS", "PACE_SMOOTH", "OQ_BATCH", "GATE_WAIT_MS", "LOCALE", "LOD_GUARD", "LSTICK_DEADZONE", "MEMORY", "MEM_CENSUS", "MENU_LANG", "MENU_SOUND_VOLUME", "MENU_TEST", "MENU_CURSOR", "MENU_CURSOR_LOG", "MENU_TEST_CLICK", "MENU_TEST_DIR", "MENU_TEST_SET", "MINIMAL_LAYOUT", "MICROPHONE", "MIRROR_GAP", "MODS_DIR", "MODS_DISABLED", "MODS_ORDER", "MODS_REBUILD", "MONITOR", "MOTION", "MOUSE_MODE", "MOUSE_DIRECT_PATCH", "MOUSE_SENS", "MOVIE_HOST", "MOVIE_PACE", @@ -127,7 +127,7 @@ static const struct { const char *key, *section; } sections[] = { {"VK_DYN_VERTEX", "performance"}, {"VK_DYN_A2C", "performance"}, {"VK_SUBGROUP_PIN", "performance"}, {"VK_RECONVERGE", "performance"}, {"VK_NAN_PRESERVE", "performance"}, {"VK_NAN_BITS", "performance"}, {"TRACE", "diagnostics"}, {"FLIGHT", "diagnostics"}, {"PASS_CRUMBS", "diagnostics"}, {"FLIGHT_SPIKE_MS", "diagnostics"}, - {"WB_FENCE_LIMIT", "diagnostics"}, {"WB_MERGE", "diagnostics"}, {"WB_EXACT", "diagnostics"}, {"MEM_CENSUS", "diagnostics"}, {"WB_NO_REUP", "diagnostics"}, {"WB_PROBE", "diagnostics"}, {"WB_SPLIT", "performance"}, {"SHADOW_BULK_WRITE", "performance"}, {"WB_LAZY", "performance"}, {"BARRIER_GAME", "performance"}, {"MESH_AHEAD", "performance"}, {"WB_LAZY_TOUCH", "diagnostics"}, {"WB_LAZY_CHUNK", "performance"}, {"FLUSH_UPLOAD", "diagnostics"}, {"GPA_TWIN_MAX_KB", "diagnostics"}, + {"WB_FENCE_LIMIT", "diagnostics"}, {"WB_MERGE", "diagnostics"}, {"WB_EXACT", "diagnostics"}, {"MEM_CENSUS", "diagnostics"}, {"WB_NO_REUP", "diagnostics"}, {"WB_PROBE", "diagnostics"}, {"WB_SPLIT", "performance"}, {"SHADOW_BULK_WRITE", "performance"}, {"WB_LAZY", "performance"}, {"HEAP_LAZY", "performance"}, {"BARRIER_GAME", "performance"}, {"MESH_AHEAD", "performance"}, {"WB_LAZY_TOUCH", "diagnostics"}, {"WB_LAZY_CHUNK", "performance"}, {"FLUSH_UPLOAD", "diagnostics"}, {"GPA_TWIN_MAX_KB", "diagnostics"}, {"WB_MERGE_MAX", "diagnostics"}, {"MV_SURVEY", "diagnostics"}, {"TEXUP_LOG_MS", "diagnostics"}, {"VA_REPORT", "diagnostics"}, {"VMA_BLOCK_MB", "diagnostics"}, {"ASTC_BC_PROBE", "diagnostics"}, }; @@ -274,7 +274,7 @@ static const CfgMeta metas[] = { {"VK_NOOPT", "0", "0|1", RESTART}, // vk_pipeline.c noopt_flag // diagnostics {"TRACE", "", NULL, RESTART}, {"FLIGHT", "0", "0|1", RESTART}, {"FLIGHT_SPIKE_MS", "100", "0..60000", RESTART}, - {"WB_FENCE_LIMIT", "65536", "0..1073741824", RESTART}, {"WB_MERGE", "1", "0|1", RESTART}, {"WB_EXACT", "1", "0|1", RESTART}, {"MEM_CENSUS", "0", "0|1", RESTART}, {"WB_NO_REUP", "1", "0|1", RESTART}, {"WB_PROBE", "0", "0|1", RESTART}, {"WB_SPLIT", "1", "0|1", RESTART}, {"WB_LAZY", WB_LAZY_DEFAULT, "0|1", RESTART}, {"WB_LAZY_TOUCH", "0", "0..16", RESTART}, {"WB_LAZY_CHUNK", "1", "0|1", RESTART}, {"FLUSH_UPLOAD", "0", "0|1", RESTART}, {"GPA_TWIN_MAX_KB", "0", "0..1048576", RESTART}, + {"WB_FENCE_LIMIT", "65536", "0..1073741824", RESTART}, {"WB_MERGE", "1", "0|1", RESTART}, {"WB_EXACT", "1", "0|1", RESTART}, {"MEM_CENSUS", "0", "0|1", RESTART}, {"WB_NO_REUP", "1", "0|1", RESTART}, {"WB_PROBE", "0", "0|1", RESTART}, {"WB_SPLIT", "1", "0|1", RESTART}, {"WB_LAZY", WB_LAZY_DEFAULT, "0|1", RESTART}, {"HEAP_LAZY", "0", "0|1", RESTART}, {"WB_LAZY_TOUCH", "0", "0..16", RESTART}, {"WB_LAZY_CHUNK", "1", "0|1", RESTART}, {"FLUSH_UPLOAD", "0", "0|1", RESTART}, {"GPA_TWIN_MAX_KB", "0", "0..1048576", RESTART}, {"WB_MERGE_MAX", "32", "1..256", RESTART}, {"TEXUP_LOG_MS", "0", "0..60000", RESTART}, // vk_texture.c vk_texstat_frame {"VMA_BLOCK_MB", "64", "16..256", RESTART}, // vk_headroom.c vk_vma_block_size {"ASTC_BC_PROBE", "0", "0|1", RESTART}, // vk_texture.c astc_probe_alpha diff --git a/runtime/frame_rate.c b/runtime/frame_rate.c index e7430c9d..d40bbf90 100644 --- a/runtime/frame_rate.c +++ b/runtime/frame_rate.c @@ -298,7 +298,12 @@ static int probe = -1; static float last_pos[3]; static uint64_t last_pos_ns; -static int read_guest(uint64_t a, void *out, size_t n) { return a && plat_mem_read(a, out, n); } +// plat_mem_read copies through the kernel, which fails on a heap page NX2NAVI_HEAP_LAZY has not committed yet +static int read_guest(uint64_t a, void *out, size_t n) { + if (!a) return 0; + mem_kernel_access(a, n); + return plat_mem_read(a, out, n); +} static uint64_t read_ptr(uint64_t a) { uint64_t p = 0; return read_guest(a, &p, 8) ? p : 0; @@ -447,7 +452,7 @@ static void ocarina_step_write(double dt) { return; } uint64_t slot = read_ptr(MOD_MAIN_BASE + 0x4c7e890), mgr = slot ? read_ptr(slot) : 0; - if (!mgr || !plat_mem_readable(mgr + 0x1c0, 4)) return; + if (!mgr || !mem_host_readable(mgr + 0x1c0, 4)) return; _Atomic uint32_t *word = (_Atomic uint32_t *)(uintptr_t)(mgr + 0x1c0); uint32_t now_bits = atomic_load_explicit(word, memory_order_relaxed); float now_v; diff --git a/runtime/fs_host.c b/runtime/fs_host.c index 59b573b7..1bee63b9 100644 --- a/runtime/fs_host.c +++ b/runtime/fs_host.c @@ -1152,8 +1152,10 @@ static uint32_t write_file(uint64_t h, int64_t offset, const void *buffer, uint6 if (file->save) save_touched(); for (uint64_t done = 0; done < size;) { uint32_t chunk = size - done > 0x40000000 ? 0x40000000 : (uint32_t)(size - done); - // NX2NAVI_WB_PROBE: the kernel reads the buffer, and a probed page would fail the write instead of faulting + // NX2NAVI_WB_PROBE: the kernel reads the buffer, and a probed page would fail the write instead of faulting; + // so would a heap page NX2NAVI_HEAP_LAZY has not committed yet if (mem_probing()) mem_thaw((uintptr_t)buffer + done, chunk); + mem_kernel_access((uintptr_t)buffer + done, chunk); int64_t put = plat_file_pwrite(f, (const uint8_t *)buffer + done, chunk, (uint64_t)offset + done); if (put <= 0) rt_fatal("fs: host save write failure"); done += (uint64_t)put; diff --git a/runtime/host.h b/runtime/host.h index 39ad79e4..cb5b3560 100644 --- a/runtime/host.h +++ b/runtime/host.h @@ -154,6 +154,7 @@ void mem_init(void); // reserve every regi // (up to 20 times, NX2NAVI_REEXEC), then fatal const RtGuestLayout *mem_layout(void); void mem_guest_census(uint64_t frame); // NX2NAVI_TRACE=perf: committed bytes per guest region +void mem_lazy_heap_report(uint64_t frame); // NX2NAVI_TRACE=perf: the lazy heap's faults since the last line uint32_t mem_set_heap_size(uint64_t size, uint64_t *addr); // 0x01 uint32_t mem_set_permission(uint64_t a, uint64_t n, uint32_t perm); // 0x02 uint32_t mem_set_attribute(uint64_t a, uint64_t n, uint32_t mask, uint32_t at); // 0x03 @@ -184,7 +185,12 @@ int mem_freeze(uint64_t a); // makes the 2 MiB re int mem_frozen(uint64_t a); // the 2 MiB region of a is frozen: no page in it changed // since its freeze void mem_thaw(uint64_t a, uint64_t n); // makes the frozen regions over [a, a+n) writable again; - // before a kernel write (file read) into guest memory + // before a kernel write (file read) into guest memory; + // also commits pages the lazy heap left reserved +void mem_kernel_access(uint64_t a, uint64_t n); // NX2NAVI_HEAP_LAZY: commits the reserved heap pages of + // [a, a+n) before the kernel reads them (a file write) +int mem_host_readable(uint64_t a, uint64_t n); // plat_mem_readable that first commits the untouched + // pages of the lazy heap in [a, a+n), which read zero void mem_freeze_stats(uint64_t *frozen, uint64_t *freezes, uint64_t *fault_thaws, uint64_t *other_thaws); // NX2NAVI_WB_PROBE=1 (memory.c, "Probed regions"): who reads what the GPU wrote back into guest memory. // NX2NAVI_WB_LAZY (memory.c, "Lazy write-back"): pages the GPU wrote that come down only when the CPU touches them. diff --git a/runtime/ipc.c b/runtime/ipc.c index 1cc4f113..4c4c7e65 100644 --- a/runtime/ipc.c +++ b/runtime/ipc.c @@ -243,7 +243,7 @@ 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); } +static int readable(uint64_t a, uint64_t n) { return mem_host_readable(a, n); } void ipc_backtrace(void) { const Cpu *c = t_req; diff --git a/runtime/memory.c b/runtime/memory.c index 1f31ee91..ff404b82 100644 --- a/runtime/memory.c +++ b/runtime/memory.c @@ -49,6 +49,9 @@ static RtGuestLayout L; static uint64_t mods_lo, mods_hi; // fixed module window static PlatMutex lock = PLAT_MUTEX_INIT; static uint64_t heap_size, alias_bytes; +static int heap_lazy; // NX2NAVI_HEAP_LAZY, Windows: the heap is committed page run by page run on first use +static uint64_t heap_lazy_faults, heap_lazy_bytes; // accesses that committed heap pages, and the bytes they committed +static uint64_t heap_lazy_ns, heap_lazy_max_ns; // time in those faults, lock wait included, since the last report static uint8_t *alias_pages; // 1 per mapped 4 KiB page of the alias region typedef struct { uint64_t dst, src, size; } StackMap; static StackMap stack_maps[MAX_STACK_MAPS]; @@ -615,8 +618,72 @@ 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); } +// Lazy heap (NX2NAVI_HEAP_LAZY, Windows only; off by default). Windows charges every committed page to the system's +// commit at once, and SetHeapSize asks for the whole application heap up front: in Castle Town after Hyrule Field the +// heap held 6.8 GiB of commit of which 3.4 GiB had ever been resident, with 0.45 GB in the page file. With it on, +// SetHeapSize only moves the heap's end, and a page of the heap is committed when something first reaches it: an access +// of the game's or the runtime's code faults into access_fault, which commits the reserved runs of the 64 KiB around +// it; a kernel read or write of a buffer (mem_thaw, mem_kernel_access) and a pointer check (mem_host_readable) +// commit their range first, as the kernel and VirtualQuery do not fault. On Linux pages are committed on first touch +// already. +// frz_lock held exclusively. Commits the reserved pages of [a, a + n) inside the heap; returns the bytes committed. A +// page another path committed is left alone: committing it again would make a read-only or inaccessible page (a +// freeze, a probe, an armed lazy write-back) read-write behind their backs. The regions it commits into are thawed +// first, as for any mapping change, so that no frozen region holds a read-write page. +static uint64_t heap_lazy_commit_locked(uint64_t a, uint64_t n) { + uint64_t lo = a > L.heap.base ? a : L.heap.base, top = L.heap.base + __atomic_load_n(&heap_size, __ATOMIC_ACQUIRE); + uint64_t end = a + n < a ? UINT64_MAX : a + n, hi = end < top ? end : top, got = 0; + if (lo >= hi) return 0; + lo &= ~(PAGE - 1), hi = align_up(hi, PAGE); + int thawed = 0; + for (uint64_t x = lo, y; x < hi; x = y) { + if (plat_mem_state(x, hi, &y) != PLAT_MEM_RESERVED) continue; + if (!thawed) thaw_range_locked(lo, hi - lo, 3), thawed = 1; + if (!plat_mem_commit(x, y - x)) + rt_fatal("memory: committing the heap's pages %#llx+%#llx on first use failed: the system's commit is " + "used up", (unsigned long long)x, (unsigned long long)(y - x)); + got += y - x; + } + heap_lazy_bytes += got; + return got; +} + +// Commits the heap pages of [a, a + n) that the lazy heap left reserved; nothing when it is off. +void mem_kernel_access(uint64_t a, uint64_t n) { + if (!heap_lazy || !n) return; + plat_mutex_lock(&frz_lock); + heap_lazy_commit_locked(a, n); + plat_mutex_unlock(&frz_lock); +} + +// plat_mem_readable for a pointer the host is about to read: untouched heap pages of a lazy heap count as readable +// (they are committed here, and read zero as they would have). +int mem_host_readable(uint64_t a, uint64_t n) { + mem_kernel_access(a, n); + return plat_mem_readable(a, n); +} + +// An access fault on a reserved page of the lazy heap: the 64 KiB around it is committed and the access runs again. +// 0 when nothing was reserved there, so that a frozen, probed or armed page goes on to its own hook (a page another +// thread committed meanwhile is retried by thaw_fault or the end of access_fault, as the heap is watched). +static int heap_lazy_fault(uint64_t a) { + uint64_t heap = L.heap.base; + if (a < heap || a - heap >= __atomic_load_n(&heap_size, __ATOMIC_ACQUIRE)) return 0; + uint64_t t0 = plat_now_ns(); + plat_mutex_lock(&frz_lock); + uint64_t got = heap_lazy_commit_locked(a & ~0xFFFFull, 0x10000); + if (got) { + uint64_t ns = plat_now_ns() - t0; + heap_lazy_faults++, heap_lazy_ns += ns; + if (ns > heap_lazy_max_ns) heap_lazy_max_ns = ns; + } + plat_mutex_unlock(&frz_lock); + return got != 0; +} + 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 + if (heap_lazy) heap_lazy_commit_locked(a, n); // the kernel's write would fail on a reserved page as well thaw_range_locked(a, n, 2); plat_mutex_unlock(&frz_lock); } @@ -718,9 +785,11 @@ static int probe_fault(uint64_t a, int write, uint64_t ip) { return plat_mem_state(a & ~(PAGE - 1), (a & ~(PAGE - 1)) + PAGE, &end) >= PLAT_PROT_READ; } -// The platform's access-fault hook: an armed page of the lazy write-back first, then the probe. A read that faulted on +// The platform's access-fault hook: a reserved page of the lazy heap first, then an armed page of the lazy write-back, +// then the probe. A read that faulted on // a page another thread gave back meanwhile runs again; a write goes on to thaw_fault. static int access_fault(uint64_t a, int write, uint64_t ip) { + if (heap_lazy && heap_lazy_fault(a)) return 1; if (__atomic_load_n(&lz_on, __ATOMIC_RELAXED) && lazy_fault(a, write)) return 1; if (__atomic_load_n(&prb_on, __ATOMIC_RELAXED)) return probe_fault(a, write, ip); if (write || a >= RT_ADDRESS_LIMIT || !watched(a, 1)) return 0; @@ -860,6 +929,13 @@ void mem_init(void) { .pool = pool ? strtoull(pool, NULL, 0) : DEFAULT_POOL + res_heap_extra_bytes(), // resolution.c }; if (!L.pool) rt_fatal("memory: NX2NAVI_APP_POOL is 0"); +#ifdef _WIN32 + if (cfg_bool("NX2NAVI_HEAP_LAZY")) { // set before the game's first SetHeapSize + plat_mem_access_hook(access_fault); + heap_lazy = 1; + rt_log("mem: the heap is committed on first use (NX2NAVI_HEAP_LAZY)"); + } +#endif } const RtGuestLayout *mem_layout(void) { return &L; } @@ -900,9 +976,10 @@ 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"); - heap_size = size; + uint64_t was = heap_size; + if (size > was && !heap_lazy) commit(L.heap.base + was, size - was, "heap"); + __atomic_store_n(&heap_size, size, __ATOMIC_RELEASE); // before a shrink: no first-use commit past the new end + if (size < was) decommit(L.heap.base + size, was - size, "heap"); // reserved pages in the range are fine __atomic_store_n(&map_gen, map_gen + 1, __ATOMIC_RELEASE); plat_mutex_unlock(&lock); if (addr) *addr = L.heap.base; @@ -1337,6 +1414,23 @@ void mem_kernel_free(void *p, uint64_t size) { // NX2NAVI_TRACE=perf: how many bytes of each guest region are committed, so a process that has grown says which region // holds the memory. A region is reserved whole at start-up and committed page by page, so the walk reads each run of // pages of the same state; the alias window is the largest and costs the most steps when it is full. +// NX2NAVI_TRACE=perf: what the lazy heap's first-use faults cost since the last line (perf.c, every perf window that had +// some), so that a hitch can be told from them: the time includes the wait for frz_lock. +void mem_lazy_heap_report(uint64_t frame) { + static uint64_t faults_prv, bytes_prv; + if (!heap_lazy) return; + plat_mutex_lock(&frz_lock); + uint64_t faults = heap_lazy_faults, bytes = heap_lazy_bytes, ns = heap_lazy_ns, max = heap_lazy_max_ns; + heap_lazy_ns = heap_lazy_max_ns = 0; + plat_mutex_unlock(&frz_lock); + if (faults == faults_prv && bytes == bytes_prv) return; + rt_log("mem: lazy heap at frame %llu: %llu faults and %.1f MiB committed since the last line, %.2f ms in the faults " + "(longest %.3f ms); %.0f of %.0f MiB committed", (unsigned long long)frame, + (unsigned long long)(faults - faults_prv), (bytes - bytes_prv) / 1048576.0, ns / 1e6, max / 1e6, + bytes / 1048576.0, __atomic_load_n(&heap_size, __ATOMIC_ACQUIRE) / 1048576.0); + faults_prv = faults, bytes_prv = bytes; +} + void mem_guest_census(uint64_t frame) { const RtGuestLayout *g = mem_layout(); const struct { const char *name; RtRange r; } reg[] = { @@ -1359,4 +1453,8 @@ void mem_guest_census(uint64_t frame) { o += snprintf(line + o, sizeof line - o, " %s %.0f", reg[i].name, committed / 1048576.0); } rt_log("%s; all %.0f", line, all / 1048576.0); + if (heap_lazy) + rt_log("mem: lazy heap at frame %llu: %.0f of %.0f MiB committed, by %llu faults and the rest by kernel " + "accesses and pointer checks", (unsigned long long)frame, heap_lazy_bytes / 1048576.0, + __atomic_load_n(&heap_size, __ATOMIC_ACQUIRE) / 1048576.0, (unsigned long long)heap_lazy_faults); } diff --git a/runtime/menu_cursor.c b/runtime/menu_cursor.c index f9a5c39b..9cf132e0 100644 --- a/runtime/menu_cursor.c +++ b/runtime/menu_cursor.c @@ -52,7 +52,7 @@ enum { PANE_PARENT = 0x18, PANE_SIZE = 0x50, PANE_FLAG = 0x58, PANE_ALPHA = 0x59 static uint64_t main_lo, main_hi; static int ok_at(uint64_t p, uint64_t n) { - return p >= 0x10000 && p < (1ull << 47) && (p & 7) == 0 && plat_mem_readable(p, n); + return p >= 0x10000 && p < (1ull << 47) && (p & 7) == 0 && mem_host_readable(p, n); } static uint64_t rd64(uint64_t a) { uint64_t v; memcpy(&v, (const void *)(uintptr_t)a, 8); return v; } static uint32_t rd32(uint64_t a) { uint32_t v; memcpy(&v, (const void *)(uintptr_t)a, 4); return v; } @@ -156,7 +156,7 @@ static Focus focus_now(void) { static void layout_name(uint64_t scr, char *out, size_t n) { snprintf(out, n, "?"); uint64_t lyt = rd64(scr + SCR_LAYOUT), name = ok_at(lyt, LYT_NAME + 8) ? rd64(lyt + LYT_NAME) : 0; - if (name < 0x10000 || name >= (1ull << 47) || !plat_mem_readable(name, n)) return; + if (name < 0x10000 || name >= (1ull << 47) || !mem_host_readable(name, n)) return; memcpy(out, (const void *)(uintptr_t)name, n - 1); out[n - 1] = 0; for (char *q = out; *q; q++) if (*q < 0x20 || *q > 0x7e) { *q = 0; break; } diff --git a/runtime/perf.c b/runtime/perf.c index f801ca97..2b1a7a39 100644 --- a/runtime/perf.c +++ b/runtime/perf.c @@ -24,6 +24,7 @@ __attribute__((weak)) void prof_sampler_write(void) {} __attribute__((weak)) void prof_sampler_frame(uint64_t frame, uint64_t now, uint64_t d) { (void)frame, (void)now, (void)d; } __attribute__((weak)) void prof_commit_census(uint64_t frame) { (void)frame; } __attribute__((weak)) void mem_guest_census(uint64_t frame) { (void)frame; } +__attribute__((weak)) void mem_lazy_heap_report(uint64_t frame) { (void)frame; } // NX2NAVI_TRACE=perf, NX2NAVI_FLIGHT=1 or the full performance overlay: event counts are taken. Wait times are always taken // (frame-time summary). perf_base: the part fixed at start. @@ -456,6 +457,7 @@ static void perf_gpu_line(uint64_t frame, double interval_ms, double pace_share, if (census_on < 0) census_on = cfg_bool("NX2NAVI_MEM_CENSUS"); if (census_on && (frame % 3600 == 0 || prv >= census_prv + (1ull << 30))) prof_commit_census(frame), mem_guest_census(frame), census_prv = prv; + mem_lazy_heap_report(frame); if (g.have_gpu && g.have_idle) rt_log("perf: frame %llu: gpu idle per frame %.2f ms between timed parts: %.2f ms waiting for the next submission, " "%.2f ms with it queued (untimed work or waits); %.1f gaps over 0.5 ms, longest %.2f ms; submit to start avg %.2f ms", diff --git a/runtime/svc.c b/runtime/svc.c index c980ae6d..bfd5a83c 100644 --- a/runtime/svc.c +++ b/runtime/svc.c @@ -24,7 +24,7 @@ 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); } +static int readable(uint64_t a, uint64_t n) { return mem_host_readable(a, n); } void kern_backtrace(const Cpu *c) { char d[160];