From de0b1f899fcf8cd06f03ede62e2d32c555b37908 Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Tue, 24 Nov 2026 08:44:45 +0300 Subject: [PATCH] heap_lazy adds the committed heap and the first-use faults with their total time to the commit census lines, so a log shows what the setting saved and cost Co-Authored-By: Claude Opus 5.5 --- runtime/host.h | 3 +++ runtime/memory.c | 7 +++++++ runtime/vk_memory.c | 7 +++++++ 3 files changed, 17 insertions(+) diff --git a/runtime/host.h b/runtime/host.h index 359b376f..ef721106 100644 --- a/runtime/host.h +++ b/runtime/host.h @@ -180,6 +180,9 @@ void mem_kernel_free(void *p, uint64_t size); // TLR/TLS reuse afte 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 +void mem_heap_lazy_stats(uint64_t *faults, uint64_t *ns, uint64_t *committed, uint64_t *size); + // TOTK_HEAP_LAZY: first-use faults and their time since + // the start, committed heap bytes, heap size 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/memory.c b/runtime/memory.c index 9f7c496a..12cc2c3d 100644 --- a/runtime/memory.c +++ b/runtime/memory.c @@ -286,6 +286,13 @@ int mem_heap_lazy(void) { return heap_lazy; } void mem_heap_commit(uint64_t a, uint64_t n) { heap_commit_range(a, n); } +void mem_heap_lazy_stats(uint64_t *faults, uint64_t *ns, uint64_t *committed, uint64_t *size) { + *faults = (uint64_t)__atomic_load_n(&lazy_faults, __ATOMIC_RELAXED); + *ns = (uint64_t)__atomic_load_n(&lazy_ns, __ATOMIC_RELAXED); + *committed = __atomic_load_n(&heap_committed, __ATOMIC_RELAXED); + *size = __atomic_load_n(&heap_size, __ATOMIC_ACQUIRE); +} + 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/vk_memory.c b/runtime/vk_memory.c index c21ae243..3376da2e 100644 --- a/runtime/vk_memory.c +++ b/runtime/vk_memory.c @@ -585,6 +585,13 @@ void vk_mem_census(uint64_t frame) { rt_log("mem: commit at frame %llu: this process %.0f MB, the system %.0f of %.0f MB; room for device memory %s%.0f MiB", (unsigned long long)frame, proc, used, limit, room == UINT64_MAX ? "not counted " : "", room == UINT64_MAX ? 0.0 : room / 1048576.0); + if (mem_heap_lazy()) { // what the heap's first-use commits (memory.c heap_fault) cost and saved so far + uint64_t nf, ns, hc, hs; + mem_heap_lazy_stats(&nf, &ns, &hc, &hs); + rt_log("mem: heap at frame %llu: %llu of %llu MiB committed, %llu first-use faults, %.1f ms in all", + (unsigned long long)frame, (unsigned long long)(hc >> 20), (unsigned long long)(hs >> 20), + (unsigned long long)nf, ns / 1e6); + } // Per heap, what VMA holds (blocks, and the allocations inside them) beside what the driver counts for the process: // usage above VMA's blocks is the driver's own memory (pipelines, command buffers, descriptor pools). VkCtx *vc = vk_ctx();