mirror of
https://git.triforceproject.com/triforce/source
synced 2026-12-02 05:48:50 -04:00
Triforce v0.9.1
The 16 GB update: idle memory unlocked and handed back to Windows while playing, empty pages under freed locks returned, and a page-file check at launch that warns before a too-small page file can crash the game.
This commit is contained in:
160
host/hos_mem.c
160
host/hos_mem.c
@@ -62,6 +62,12 @@ void hos_mem_init(uint64_t image_end) {
|
||||
hos_program_size = image_end - hos_image_base;
|
||||
hos_commit(HOS_SYSTEM_BASE, HOS_SYSTEM_END - HOS_SYSTEM_BASE);
|
||||
hos_mem_census_start(); /* (Triforce: the census line and the give-back, mkw_glue.c) */
|
||||
#if defined(_WIN32)
|
||||
{ /* ((Triforce, 12-01) the page file against the game's peak, a warning when it is too small: mkw_glue.c) */
|
||||
void hos_pagefile_check(void);
|
||||
hos_pagefile_check();
|
||||
}
|
||||
#endif
|
||||
low_hole[0][0] = 0x00100000ULL, low_hole[0][1] = HOS_SYSTEM_BASE;
|
||||
low_hole[1][0] = (image_end + 0xFFFFFULL) & ~0xFFFFFULL, low_hole[1][1] = HOS_HEAP_BASE;
|
||||
/* Retail Switch gives an application 3285 MB. HOS_MEMORY_MB overrides it: TOTK sizes its heap from this
|
||||
@@ -849,6 +855,160 @@ static int census_release(uint64_t lo, uint64_t hi) {
|
||||
CENSUS_UNLOCK();
|
||||
return ok;
|
||||
}
|
||||
/* ---- ((Triforce, 12-01) The zero pages of freed imports given back (HOS_MEM_ZERO_BACK, on; =0 off) -----------------------------
|
||||
An import (gpu/vk/vk_memory.cpp import_pool) commits its whole range and reads every page of it first, also the pages the game
|
||||
never had: its GPU pools lie in the heap (committed as it is touched) and in regions, and an import takes 4 to 16 MB chunks and
|
||||
whole pools around what a binding or a texture needs. After the import went those pages stayed committed, zeros, in RAM until
|
||||
Windows trimmed them and in the commit charge for good: in the 12-01 lab runs the game's window held 9.3-9.8 GB committed
|
||||
where the game had been handed ~5.0-5.2 GB (the census line's "handed out"), on a tour 25 GB of commit at the peak, past what a
|
||||
16 GB PC with its page file can give (#bug-tracker: crashes and stutter on 16 GB PCs).
|
||||
Here the census's thread takes the freed ranges from the renderer (rx_freed_ranges) and, a MB at a time with the renderer's
|
||||
import locks held (rx_guest_release: no import holds the MB, none can start meanwhile), gives back each 64 KB block that is
|
||||
committed read-write and all zeros. A zero page given back is the same page to the game: its next touch commits it again,
|
||||
zeroed (mkw_glue.c commit_on_touch), whoever had been handed it. The race with the game's own writes:
|
||||
- the block is read first without any lock (most blocks with data end there);
|
||||
- then marked busy (zero_busy_lo/hi), made read-only, read again, and decommitted only if still all zeros, else made
|
||||
read-write again. A write after the read-only flip faults: commit_on_touch waits until the block is no longer busy and
|
||||
has the write tried again (on a reserved page then: committed, zeroed; on a read-write one: done);
|
||||
- the kernel does not fault pages in, so the calls that hand guest memory to it (file reads and writes) hold kio_lock shared
|
||||
from their touch to their end (hos_guest_kio_begin/end), and a block is given back only when kio_lock is free at once
|
||||
(never waited for: the renderer's pool lock is held here).
|
||||
Below 4 GB and in large pages nothing is given back (as census_release). Only with the heap committed as it is touched
|
||||
(hos_heap_on_touch, Windows): the first touch is what brings a page back. */
|
||||
#if defined(_WIN32)
|
||||
static SRWLOCK kio_lock = SRWLOCK_INIT;
|
||||
static volatile uint64_t zero_busy_lo, zero_busy_hi;
|
||||
static uint64_t zero_given, zero_scanned, zero_busy_io, zero_had_data, zero_refused;
|
||||
void hos_guest_kio_begin(uint64_t addr, uint64_t size) {
|
||||
AcquireSRWLockShared(&kio_lock);
|
||||
hos_guest_touch(addr, size);
|
||||
}
|
||||
void hos_guest_kio_end(void) { ReleaseSRWLockShared(&kio_lock); }
|
||||
/* The handler's part (mkw_glue.c commit_on_touch): 1 when guest lies in the block being given back; it returns once the block
|
||||
is done (given back or read-write again), and the access is to be tried again */
|
||||
int hos_zero_wait(uint64_t guest) {
|
||||
if (!(guest >= __atomic_load_n(&zero_busy_lo, __ATOMIC_SEQ_CST) && guest < __atomic_load_n(&zero_busy_hi, __ATOMIC_SEQ_CST)))
|
||||
return 0;
|
||||
while (guest >= __atomic_load_n(&zero_busy_lo, __ATOMIC_SEQ_CST) && guest < __atomic_load_n(&zero_busy_hi, __ATOMIC_SEQ_CST))
|
||||
YieldProcessor();
|
||||
return 1;
|
||||
}
|
||||
static int block_zero(const uint8_t *p) {
|
||||
const uint64_t *q = (const uint64_t *)p;
|
||||
for (int i = 0; i < 0x10000 / 8; i += 8)
|
||||
if (q[i] | q[i + 1] | q[i + 2] | q[i + 3] | q[i + 4] | q[i + 5] | q[i + 6] | q[i + 7]) return 0;
|
||||
return 1;
|
||||
}
|
||||
/* rx_guest_release's callback for [lo, hi) (a MB at most): the zero blocks given back; 1 when any was */
|
||||
static int zero_release(uint64_t lo, uint64_t hi) {
|
||||
extern uint64_t hos_large_lo, hos_large_hi;
|
||||
if (hos_large_hi && lo < hos_large_hi && hi > hos_large_lo) return 0;
|
||||
if (lo < (1ULL << 32)) return 0;
|
||||
/* (which pages are in RAM and read-write: QueryWorkingSetEx, one call for the MB. Not VirtualQuery: it walks every page of
|
||||
the committed region it is asked in, and the game's are GBs: 132,681 asks of it made a pass of 57 s of kernel time in the
|
||||
first lab tour, the pools' lock and the address space's held through it, the replay and the game's frees stalled for
|
||||
seconds. A page not in RAM is left: it costs no RAM now) */
|
||||
typedef struct { void *va; ULONG_PTR attr; } Wsx;
|
||||
typedef BOOL(WINAPI * QueryWs)(HANDLE, PVOID, DWORD);
|
||||
static QueryWs query;
|
||||
if (!query) query = (QueryWs)(void *)GetProcAddress(GetModuleHandleA("kernel32.dll"), "K32QueryWorkingSetEx");
|
||||
lo &= ~0xFFFFULL;
|
||||
if (!query || hi <= lo || hi - lo > (1ULL << 20)) return 0;
|
||||
static Wsx q[256]; /* (the census's thread alone) */
|
||||
const int npages = (int)((hi - lo) >> 12);
|
||||
for (int i = 0; i < npages; i++) q[i].va = (void *)GP(lo + ((uint64_t)i << 12)), q[i].attr = 0;
|
||||
if (!query(GetCurrentProcess(), q, (DWORD)(npages * sizeof *q))) return 0;
|
||||
uint64_t given = 0;
|
||||
for (uint64_t b = lo; b + 0x10000 <= hi; b += 0x10000) {
|
||||
const int first = (int)((b - lo) >> 12);
|
||||
int usable = 1;
|
||||
for (int i = first; i < first + 16 && usable; i++) {
|
||||
const ULONG_PTR a = q[i].attr;
|
||||
/* (Valid bit 0, Win32Protection bits 4-14, Shared bit 15, Locked bit 22) */
|
||||
usable = (a & 1) && ((a >> 4) & 0x7FF) == PAGE_READWRITE && !(a & (1u << 15)) && !(a & (1u << 22));
|
||||
}
|
||||
if (!usable) continue;
|
||||
zero_scanned++;
|
||||
if (!block_zero(GP(b))) { zero_had_data++; continue; }
|
||||
if (!TryAcquireSRWLockExclusive(&kio_lock)) { zero_busy_io++; continue; }
|
||||
__atomic_store_n(&zero_busy_lo, b, __ATOMIC_SEQ_CST);
|
||||
__atomic_store_n(&zero_busy_hi, b + 0x10000, __ATOMIC_SEQ_CST);
|
||||
DWORD old = 0;
|
||||
int done = 0;
|
||||
if (VirtualProtect(GP(b), 0x10000, PAGE_READONLY, &old)) {
|
||||
if (block_zero(GP(b)) && VirtualFree(GP(b), 0x10000, MEM_DECOMMIT)) done = 1;
|
||||
else VirtualProtect(GP(b), 0x10000, PAGE_READWRITE, &old), zero_refused++;
|
||||
}
|
||||
__atomic_store_n(&zero_busy_hi, 0, __ATOMIC_SEQ_CST);
|
||||
__atomic_store_n(&zero_busy_lo, 0, __ATOMIC_SEQ_CST);
|
||||
ReleaseSRWLockExclusive(&kio_lock);
|
||||
if (done) {
|
||||
CENSUS_LOCK();
|
||||
gc_change(&census, GC_HELD, b, b + 0x10000, 0);
|
||||
gc_change(&census, GC_KEPT, b, b + 0x10000, 0);
|
||||
CENSUS_UNLOCK();
|
||||
given += 0x10000;
|
||||
}
|
||||
}
|
||||
zero_given += given;
|
||||
return given > 0;
|
||||
}
|
||||
/* A pass (the census's thread): the ranges freed since the last one taken in, then up to max_bytes of them looked at, a MB at a
|
||||
time. The bytes given back this pass */
|
||||
int64_t hos_mem_zero_give_back(uint64_t max_bytes) {
|
||||
static int on = -1;
|
||||
if (on < 0) {
|
||||
const char *e = getenv("HOS_MEM_ZERO_BACK");
|
||||
on = !(e && atoi(e) == 0) && hos_heap_on_touch && hos_mem_census_on();
|
||||
hos_log("mem: the zero pages of freed imports given back: %s (HOS_MEM_ZERO_BACK=0: off)",
|
||||
on ? "on" : hos_heap_on_touch ? "off" : "off (the heap is committed whole)");
|
||||
}
|
||||
if (!on) return 0;
|
||||
int rx_freed_ranges(uint64_t (*out)[2], int max); /* (rx_backend.c) */
|
||||
int rx_guest_release(uint64_t lo, uint64_t hi, int (*release)(uint64_t lo, uint64_t hi));
|
||||
static uint64_t (*todo)[2];
|
||||
static int ntodo, captodo;
|
||||
uint64_t got[256][2];
|
||||
for (int n; (n = rx_freed_ranges(got, 256)) > 0;) {
|
||||
if (ntodo + n > captodo) {
|
||||
const int cap = (ntodo + n) * 2;
|
||||
uint64_t(*grown)[2] = (uint64_t(*)[2])realloc(todo, (size_t)cap * sizeof *todo);
|
||||
if (!grown) break;
|
||||
todo = grown, captodo = cap;
|
||||
}
|
||||
for (int i = 0; i < n; i++) todo[ntodo][0] = got[i][0] & ~0xFFFFULL, todo[ntodo][1] = got[i][1], ntodo++;
|
||||
if (n < 256) break;
|
||||
}
|
||||
const uint64_t given0 = zero_given;
|
||||
uint64_t looked = 0;
|
||||
const int64_t stall0 = stall_host_begin();
|
||||
const int64_t t0 = hos_now_ns();
|
||||
while (ntodo > 0 && looked < max_bytes && hos_now_ns() - t0 < 40000000) { /* (40 ms a pass at most: the rest next pass) */
|
||||
uint64_t *r = todo[ntodo - 1];
|
||||
const uint64_t lo = r[0], hi = r[1] - r[0] > (1ULL << 20) ? r[0] + (1ULL << 20) : r[1];
|
||||
const int io = hos_guest_io_begin(GP(lo), hi - lo); /* (the lazy write-back's pages there paid first, as census_offer) */
|
||||
rx_guest_release(lo, hi, zero_release);
|
||||
hos_guest_io_end(io);
|
||||
looked += hi - lo;
|
||||
if (hi >= r[1]) ntodo--;
|
||||
else r[0] = hi;
|
||||
}
|
||||
stall_host_end(STALL_HOST_GIVE_BACK, stall0, zero_given - given0);
|
||||
const uint64_t given = zero_given - given0;
|
||||
static int said;
|
||||
if (given && (said++ < 40 || said % 64 == 0))
|
||||
hos_log("mem: %llu MB of zero pages under freed imports given back (%llu MB in all; %llu MB looked at, %d ranges left; blocks: "
|
||||
"%llu scanned, %llu with data, %llu skipped beside a file call, %llu written meanwhile)", (unsigned long long)(given >> 20),
|
||||
(unsigned long long)(zero_given >> 20), (unsigned long long)(looked >> 20), ntodo, (unsigned long long)zero_scanned,
|
||||
(unsigned long long)zero_had_data, (unsigned long long)zero_busy_io, (unsigned long long)zero_refused);
|
||||
return (int64_t)given;
|
||||
}
|
||||
#else
|
||||
void hos_guest_kio_begin(uint64_t addr, uint64_t size) { hos_guest_touch(addr, size); }
|
||||
void hos_guest_kio_end(void) {}
|
||||
int hos_zero_wait(uint64_t guest) { (void)guest; return 0; }
|
||||
int64_t hos_mem_zero_give_back(uint64_t max_bytes) { (void)max_bytes; return 0; }
|
||||
#endif
|
||||
/* One kept run offered: the lazy write-back's pages there paid first (as FreeMemoryPages does), then the renderer asked */
|
||||
static int census_offer(void *ctx, uint64_t lo, uint64_t hi) {
|
||||
(void)ctx;
|
||||
|
||||
Reference in New Issue
Block a user