mirror of
https://github.com/valentderah/totk-pc-recomp.git
synced 2026-11-24 13:45:57 +00:00
Merge task/pr1369: heap_lazy = 1 commits the game heap on first use on Windows, off by default; the Vulkan import also commits the chunks its alignment widens into, and the commit census shows the heap's faults (ShannoonRuaaa)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
4f5a9d4754
11 files changed
+175
-8
No files matched your search
@@ -221,6 +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 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 |
|
||||
|
||||
+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", "LIGHTNING_FIX", "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",
|
||||
|
||||
@@ -1024,6 +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_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);
|
||||
|
||||
@@ -177,6 +177,14 @@ 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
|
||||
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
|
||||
// module or the committed heap answered from the layout,
|
||||
// anything else by plat_mem_readable (an OS query)
|
||||
|
||||
+5
-1
@@ -66,7 +66,11 @@ 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); }
|
||||
// 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;
|
||||
|
||||
+125
-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,105 @@ 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. File reads and Vulkan imports call mem_thaw first, so mem_thaw commits the reserved chunks of its
|
||||
// range too (heap_commit_range); save writes only need the pages to exist and call mem_heap_commit. 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;
|
||||
}
|
||||
|
||||
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);
|
||||
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 +355,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 +378,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 +415,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.
|
||||
|
||||
+5
-1
@@ -31,7 +31,11 @@ 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); }
|
||||
// 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];
|
||||
|
||||
@@ -128,6 +128,9 @@ static int import_pool(VkCtx *c, PoolRec *r, uint64_t addr, uint64_t size) {
|
||||
r->import_lo = addr & ~(al - 1);
|
||||
r->import_hi = (addr + size + al - 1) & ~(al - 1);
|
||||
uint64_t n = r->import_hi - r->import_lo;
|
||||
// TOTK_HEAP_LAZY: the caller's mem_thaw committed the pool's own chunks; an alignment above 4 KiB can widen the
|
||||
// import into a neighbouring chunk the game never touched, and the driver refuses reserved pages.
|
||||
mem_heap_commit(r->import_lo, n);
|
||||
VkMemoryHostPointerPropertiesEXT hp = {.sType = VK_STRUCTURE_TYPE_MEMORY_HOST_POINTER_PROPERTIES_EXT};
|
||||
VkResult res = p_host_props ? p_host_props(c->device, VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT,
|
||||
(void *)(uintptr_t)r->import_lo, &hp)
|
||||
@@ -582,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();
|
||||
@@ -643,6 +653,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