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:
ruckus7382sfgsgdf
2026-12-01 17:47:00 -06:00
parent 822ee913bc
commit dad28e2d4b
11 changed files with 802 additions and 7 deletions

View File

@@ -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;

View File

@@ -188,6 +188,89 @@ DWORD WINAPI hos_out_of_memory_box(LPVOID unused) {
return 0;
}
/* ---- ((Triforce, 12-01) The page file checked at the start (pagefile_rule.h has why and the rule) ---------------------------
A line in the log every start; and when the commit limit can never reach what the game needs, a warning that says what to
change, once for the page file's settings as they are (triforce_pagefile_warned.txt beside the game: shown again only when
they change). On a thread of its own: the start waits for nothing. HOS_PAGEFILE_CHECK=0: neither; =2: the warning whatever the
rule says (to see it) */
#if defined(_WIN32)
#include "pagefile_rule.h"
static char pagefile_box_text[1400];
static DWORD WINAPI pagefile_box(LPVOID unused) {
(void)unused;
MessageBoxA(NULL, pagefile_box_text, "Triforce: your page file is too small", MB_OK | MB_ICONWARNING | MB_TOPMOST);
return 0;
}
static DWORD WINAPI pagefile_check_main(LPVOID unused) {
(void)unused;
const char *e = getenv("HOS_PAGEFILE_CHECK");
const int mode = e ? atoi(e) : 1;
if (mode == 0) return 0;
char multi[2048];
DWORD size = sizeof multi - 2;
memset(multi, 0, sizeof multi);
const LONG r = RegGetValueA(HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Memory Management",
"PagingFiles", RRF_RT_REG_MULTI_SZ, NULL, multi, &size);
const PfSettings s = pf_parse(r == ERROR_SUCCESS ? multi : "", r == ERROR_SUCCESS ? (size_t)size : 0);
MEMORYSTATUSEX ms;
ms.dwLength = sizeof ms;
if (!GlobalMemoryStatusEx(&ms)) return 0;
const double gb = 1024.0 * 1024.0 * 1024.0;
const double ram = (double)ms.ullTotalPhys / gb, limit = (double)ms.ullTotalPageFile / gb,
in_use = (double)(ms.ullTotalPageFile - ms.ullAvailPageFile) / gb;
double disk = 0;
if (s.kind == PF_KIND_MANAGED && s.drive) {
char root[4] = {s.drive, ':', '\\', 0};
ULARGE_INTEGER avail;
if (GetDiskFreeSpaceExA(root, &avail, NULL, NULL)) disk = (double)avail.QuadPart / gb;
}
const PfVerdict v = pf_decide(&s, ram, limit, in_use, disk);
static const char *kinds[] = {"none", "managed by Windows", "a fixed size"};
hos_log("mem: page file: %s%s%c%s (%s %.1f GB); RAM %.1f GB, commit limit %.1f GB now, %.1f GB in use before the game; the "
"limit can reach %.1f GB, the game needs %.1f GB: %s (HOS_PAGEFILE_CHECK)", kinds[s.kind], s.drive ? " on " : "",
s.drive ? s.drive : ' ', s.drive ? ":" : "", s.kind == PF_KIND_MANAGED ? "its drive has free" : "at most",
s.kind == PF_KIND_FIXED ? s.fixed_max_gb : s.kind == PF_KIND_MANAGED ? disk : 0.0,
ram, limit, in_use, v.reach_gb, v.need_gb,
v.verdict == PF_OK ? "enough" : v.verdict == PF_SHORT_NONE ? "SHORT, no page file" : v.verdict == PF_SHORT_FIXED ?
"SHORT, the fixed page file is too small" : "SHORT, the page file's drive is nearly full");
if (v.verdict == PF_OK && mode != 2) return 0;
/* (once for these settings: the kind, the drive, the fixed size and the RAM) */
char sig[96], path[MAX_PATH + 40], had[96] = "";
snprintf(sig, sizeof sig, "%d %c %.0f %.0f %d", s.kind, s.drive ? s.drive : '-', s.fixed_max_gb, ram, v.verdict);
const DWORD n = hos_game_path_a(path, MAX_PATH);
if (n && n < MAX_PATH) {
char *slash = strrchr(path, '\\');
if (slash) strcpy(slash + 1, "triforce_pagefile_warned.txt");
FILE *f = fopen(path, "rb");
if (f) {
const size_t k = fread(had, 1, sizeof had - 1, f);
had[k] = 0;
fclose(f);
}
if (!strcmp(had, sig) && mode != 2) {
hos_log("mem: page file: warned before for these settings (delete triforce_pagefile_warned.txt to see it again)");
return 0;
}
if ((f = fopen(path, "wb"))) fputs(sig, f), fclose(f);
}
const char *what = v.verdict == PF_SHORT_NONE ? "Windows has no page file"
: v.verdict == PF_SHORT_FIXED ? "Windows' page file is set to a fixed size that is too small"
: "the drive that holds Windows' page file is nearly full, so the page file cannot grow";
snprintf(pagefile_box_text, sizeof pagefile_box_text,
"Triforce can use up to about %.0f GB of memory (RAM plus the page file) at its peak, and %s: this PC can reach "
"%.0f GB, it needs about %.0f GB. When it runs out, the game closes.\n\n"
"To fix it: open Windows' Settings > System > About > Advanced system settings > Performance: Settings > "
"Advanced > Virtual memory: Change, tick \"Automatically manage paging file size for all drives\", then restart "
"the PC.%s\n\n"
"This warning is shown once for these settings.",
24.0, what, v.reach_gb, v.need_gb,
v.verdict == PF_SHORT_DISK ? " Also free up space on that drive (at least 20 GB)." : "");
CloseHandle(CreateThread(NULL, 0, pagefile_box, NULL, 0, NULL));
return 0;
}
void hos_pagefile_check(void) { CloseHandle(CreateThread(NULL, 0, pagefile_check_main, NULL, 0, NULL)); }
#endif
static LONG CALLBACK commit_on_touch(EXCEPTION_POINTERS *info) {
EXCEPTION_RECORD *rec = info->ExceptionRecord;
if (rec->ExceptionCode != EXCEPTION_ACCESS_VIOLATION || rec->NumberParameters < 2) return EXCEPTION_CONTINUE_SEARCH;
@@ -195,6 +278,12 @@ static LONG CALLBACK commit_on_touch(EXCEPTION_POINTERS *info) {
if (p < A64_MEM || p >= A64_MEM + HOS_GUEST_SPACE) return EXCEPTION_CONTINUE_SEARCH;
const uint64_t guest = (uint64_t)(p - A64_MEM);
if (guest < 0x100000) return EXCEPTION_CONTINUE_SEARCH; /* (a null pointer: the game's own fault) */
/* ((Triforce, 12-01) a block of zeros being given back (hos_mem.c zero_release) is read-only for a moment: the write waits
for it, then is tried again */
{
int hos_zero_wait(uint64_t guest);
if (hos_zero_wait(guest)) return EXCEPTION_CONTINUE_EXECUTION;
}
MEMORY_BASIC_INFORMATION mbi;
if (!VirtualQuery(p, &mbi, sizeof mbi)) return EXCEPTION_CONTINUE_SEARCH;
#ifdef _WIN32
@@ -1335,6 +1424,10 @@ static DWORD WINAPI census_main(void *unused) {
}
}
}
{ /* ((Triforce, 12-01) the zero pages under freed imports (hos_mem.c), every pass whatever the RAM */
int64_t hos_mem_zero_give_back(uint64_t max_bytes);
hos_mem_zero_give_back(512ULL << 20);
}
if (!census_line) continue;
hos_mem_census(&c);
char line[1024];
@@ -1351,7 +1444,7 @@ void hos_mem_census_start(void) {
census_line = !(e && atoi(e) <= 0);
census_every = e && atoi(e) > 0 ? atoi(e) : 5;
census_give_back = g ? atoi(g) : 0;
if (!census_line && census_give_back <= 0) return;
if (!census_line && census_give_back <= 0) return; /* ((Triforce, 12-01) the zero give-back runs on this thread too) */
char every[32] = "off";
if (census_line) snprintf(every, sizeof every, "every %d s", census_every);
hos_log("mem: census %s; give-back %s (HOS_MEM_CENSUS, HOS_MEM_GIVE_BACK)", every,

View File

@@ -2341,10 +2341,12 @@ HLE(_ZN2nn5audio16AppendWaveBufferEPNS0_9VoiceTypeEPKNS0_10WaveBufferE) {
FILE *f = fopen(path, "wb");
if (f) {
fwrite(v->coefs, 1, 32, f);
hos_guest_touch(w.buffer, (uint64_t)w.size); /* ((Triforce, 11-21, MEM-09) fwrite hands it to the kernel) */
void hos_guest_kio_begin(uint64_t addr, uint64_t size), hos_guest_kio_end(void);
hos_guest_kio_begin(w.buffer, (uint64_t)w.size); /* ((Triforce, 11-21, MEM-09; 12-01) fwrite hands it to the kernel) */
const int io = hos_guest_io_begin(GP(w.buffer), (uint64_t)w.size); /* (Triforce: rx_backend.c) */
fwrite(GP(w.buffer), 1, (size_t)w.size, f);
hos_guest_io_end(io);
hos_guest_kio_end();
fclose(f);
}
}

View File

@@ -740,7 +740,8 @@ static uint64_t read_at(Ctx *c, uint64_t id, int64_t off, uint64_t buf, uint64_t
const int64_t stall0 = stall_host_begin(); /* ((Triforce, OS-51) the read itself, not the wait for the core after it) */
/* ((Triforce, 11-21, MEM-09) the kernel's ReadFile does not fault pages in: heap pages nothing touched yet (committed
as they are touched now, heap_commit.h) are touched for it first) */
hos_guest_touch(buf, size);
void hos_guest_kio_begin(uint64_t addr, uint64_t size), hos_guest_kio_end(void);
hos_guest_kio_begin(buf, size); /* ((Triforce, 12-01) and no zero block given back under the read: hos_mem.c) */
const int io = hos_guest_io_begin(GP(buf), size); /* (Triforce: the kernel writes there: rx_backend.c) */
while (done < size) {
OVERLAPPED ov = {0};
@@ -752,6 +753,7 @@ static uint64_t read_at(Ctx *c, uint64_t id, int64_t off, uint64_t buf, uint64_t
done += got;
}
hos_guest_io_end(io);
hos_guest_kio_end();
stall_host_end(STALL_HOST_READ, stall0, done);
if (away) hos_vcore_back();
HOS_WRITTEN(GP(buf), (size_t)done); /* (into guest memory: the GPU may read it) */
@@ -808,10 +810,12 @@ HLE(_ZN2nn2fs9WriteFileENS0_10FileHandleElPKvmRKNS0_11WriteOptionE) {
it. HOS_WRITE_KEEP_CORE=1: the core held through writes and flushes, as before */
const int away = fs_write_keep_core() ? 0 : hos_vcore_away();
const int64_t stall0 = stall_host_begin(); /* ((Triforce, OS-51) the host's part of a long frame: stall_host.h) */
hos_guest_touch(ARG(2), ARG(3)); /* ((Triforce, 11-21, MEM-09) as the read: touched before the kernel reads them) */
void hos_guest_kio_begin(uint64_t addr, uint64_t size), hos_guest_kio_end(void);
hos_guest_kio_begin(ARG(2), ARG(3)); /* ((Triforce, 11-21, MEM-09) as the read: touched before the kernel reads them) */
const int io = hos_guest_io_begin(GP(ARG(2)), ARG(3)); /* (Triforce: the kernel reads there: rx_backend.c) */
WriteFile(fh->h, GP(ARG(2)), (DWORD)ARG(3), &put, &ov);
hos_guest_io_end(io);
hos_guest_kio_end();
stall_host_end(STALL_HOST_WRITE, stall0, put);
if (away) hos_vcore_back();
}

97
host/pagefile_rule.h Normal file
View File

@@ -0,0 +1,97 @@
/* (Triforce, 12-01) Whether this PC's commit limit (RAM + page file) can ever hold the game: the page file's settings read at
the start, and a warning when they cannot (mkw_glue.c hos_pagefile_check). Apart from Windows so that
tests/test_pagefile_rule.py runs it on many made-up PCs.
Why: 16 GB players crash when Windows runs out of commit ("Windows is out of memory", mkw_glue.c hos_out_of_memory_box, shown
only once it has happened). Eleven 16 GB players' logs of 11-19 had commit limits of 29 to 58 GB, set by their page files,
and five of them were within 1.5 GB of theirs. The game itself takes some 23.5-24.5 GB of commit at its peak on a lab tour of
eleven areas (12-01, v0.9.0 + the RAM work: the game's own memory ~9.4 GB, the graphics memory's charge ~8.4, the renderer's
~5.6), and none of that could be cut without hitches. A page file Windows manages grows as it is needed, up to three times
the RAM, while its drive has room; a fixed one never passes its maximum; none at all leaves the RAM alone.
The rule: the most the limit can reach (RAM + the page file at its largest: a fixed one's maximum; a managed one's size now
grown by what its drive has free, keeping 2 GB, up to three times the RAM, or 4 GB on a small PC) against what is needed (the
commit in use before the game, plus the game's peak and a margin: 26 GB; never under 36 GB). Short: a warning, which names
what to change. */
#ifndef PAGEFILE_RULE_H
#define PAGEFILE_RULE_H
#include <stdlib.h>
#include <string.h>
enum { PF_KIND_NONE = 0, PF_KIND_MANAGED = 1, PF_KIND_FIXED = 2 };
enum { PF_OK = 0, PF_SHORT_NONE = 1, PF_SHORT_FIXED = 2, PF_SHORT_DISK = 3 };
typedef struct {
int kind; /* PF_KIND_*: managed wins when one file of several is managed */
char drive; /* the managed file's drive letter ('C'), or the first fixed file's; 0 when none */
double fixed_max_gb; /* the fixed files' maximum sizes, summed */
} PfSettings;
/* The registry's PagingFiles value (REG_MULTI_SZ: strings one after another, each ended by a 0, an empty one at the end), n
bytes: "?:\pagefile.sys" (every drive, Windows manages it), "C:\pagefile.sys 0 0" (managed, on C:), "C:\pagefile.sys 4096 8192"
(fixed: initial and maximum in MB), or nothing (no page file) */
static inline PfSettings pf_parse(const char *multi, size_t n) {
PfSettings s = {PF_KIND_NONE, 0, 0.0};
size_t at = 0;
while (multi && at < n && multi[at]) {
const char *e = multi + at;
size_t len = 0;
while (at + len < n && e[len]) len++;
char one[300];
const size_t k = len < sizeof one - 1 ? len : sizeof one - 1;
memcpy(one, e, k);
one[k] = 0;
at += len + 1;
if (k < 2) continue;
char *sp = strchr(one, ' ');
long long init = 0, max = 0;
int sizes = 0;
if (sp) {
char *end = NULL;
init = strtoll(sp + 1, &end, 10);
if (end && *end == ' ') max = strtoll(end + 1, NULL, 10), sizes = 1;
}
const char d = one[0] >= 'a' && one[0] <= 'z' ? (char)(one[0] - 32) : one[0];
if (one[0] == '?' || !sizes || (init == 0 && max == 0)) { /* (managed: every drive, or a drive with no sizes) */
s.kind = PF_KIND_MANAGED;
s.drive = one[0] == '?' ? 'C' : d;
} else if (s.kind != PF_KIND_MANAGED) {
if (s.kind == PF_KIND_NONE) s.drive = d;
s.kind = PF_KIND_FIXED;
s.fixed_max_gb += (double)(max > init ? max : init) / 1024.0;
}
}
return s;
}
typedef struct {
double reach_gb; /* the most the commit limit can reach */
double need_gb; /* what the game needs it to reach */
int verdict; /* PF_OK, or why it is short */
} PfVerdict;
/* ram_gb: the PC's RAM; limit_gb: the commit limit now (RAM + the page file as it is now); in_use_gb: the commit in use now,
before the game's own; disk_free_gb: free space on the managed file's drive (unused otherwise) */
static inline PfVerdict pf_decide(const PfSettings *s, double ram_gb, double limit_gb, double in_use_gb, double disk_free_gb) {
PfVerdict v;
const double now_file = limit_gb > ram_gb ? limit_gb - ram_gb : 0.0;
if (s->kind == PF_KIND_MANAGED) {
const double cap = ram_gb * 3 > 4 ? ram_gb * 3 : 4;
double grown = now_file + (disk_free_gb > 2 ? disk_free_gb - 2 : 0);
if (grown > cap) grown = cap;
if (grown < now_file) grown = now_file;
v.reach_gb = ram_gb + grown;
} else if (s->kind == PF_KIND_FIXED) {
v.reach_gb = ram_gb + (s->fixed_max_gb > now_file ? s->fixed_max_gb : now_file);
} else {
v.reach_gb = limit_gb > ram_gb ? limit_gb : ram_gb;
}
v.need_gb = in_use_gb + 26 > 36 ? in_use_gb + 26 : 36;
v.verdict = v.reach_gb >= v.need_gb ? PF_OK
: s->kind == PF_KIND_NONE ? PF_SHORT_NONE
: s->kind == PF_KIND_FIXED ? PF_SHORT_FIXED : PF_SHORT_DISK;
return v;
}
#endif

View File

@@ -766,6 +766,19 @@ int rx_guest_release(uint64_t lo, uint64_t hi, int (*release)(uint64_t lo, uint6
if (away) hos_vcore_back();
return r == 1 ? 1 : 0;
}
/* ((Triforce, 12-01) The ranges of guest memory whose Vulkan imports were freed since the last ask (the back end's
mkwvk_freed_ranges, found by name): hos_mem.c hos_mem_zero_give_back looks there for pages to give back. The count, 0 when
there is no such back end */
int rx_freed_ranges(uint64_t (*out)[2], int max) {
if (!vk || max <= 0) return 0;
static int (*volatile fn)(uint64_t (*)[2], int);
static volatile int looked;
if (!looked) {
fn = (int (*)(uint64_t (*)[2], int))(void *)GetProcAddress(vk_module, "mkwvk_freed_ranges");
looked = 1;
}
return fn ? fn(out, max) : 0;
}
/* (Triforce) The census's second line (mkw_glue.c census_main): the Vulkan back end's big holders of memory, as text (its
mkwvk_memory_holders, found by name). Its length, 0 when there is none */
int rx_memory_holders(char *out, size_t n) {