mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-18 13:09:57 -04:00
Let a shadow write-back clear the write watch's protection of its range in one call on Linux (shadow_bulk_write = 1).
On Linux the write watch protects pages, so a write-back from shadow storage into guest memory took a fault at the first store into every page. plat_mem_prepare_write clears the protection of a range with one UFFDIO_WRITEPROTECT call; the pages count as written, as they would after the faults. Off by default; Windows has nothing to clear. Pull request #327 by theurbanninja1993-sketch (a draft on GitHub). The platform function and its Linux test are taken as they are. The call moved: the write-back it was written for is now shadow_write_back in runtime/vk_memory.c (a file other sessions are changing), where it covers the runs of pages a submission changed, and the setting is registered in cfg.c and described in nx2navi.ini.example, which also says that the lazy write-back stays off with shadow storage. The Windows files compile; the Linux code and its test are not compiled or run here. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
@@ -649,8 +649,12 @@
|
||||
# wb_split = 1
|
||||
# 1: what the game's shaders write into its memory is copied back from the video card once, and after that only when
|
||||
# the game's CPU code touches it (most of it never is: 66 MiB a frame in Hyrule Field). 0: copied back at the end of
|
||||
# every GPU submission, as before. On Linux it is off unless set here.
|
||||
# every GPU submission, as before. On Linux it is off unless set here, and with shadow storage (Linux, Wine) it stays
|
||||
# off.
|
||||
# wb_lazy = 1
|
||||
# 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
|
||||
# 1: the game's flushes of mirrored memory are copied into the mirrors even where the write watch already sees every
|
||||
# page the game wrote; 0 (default) copies only the written pages there, which saved 47-57 MiB of copies a frame in
|
||||
# Kokiri Forest.
|
||||
|
||||
@@ -58,7 +58,7 @@ static const char *const known[] = {
|
||||
"PRESNAP_PLACE",
|
||||
"REPLAY_CHECK", "RESOLVE_CHECK", "RESTART_WAIT", "ROMFS", "ROMFS_INDEX", "ROMFS_MMAP", "ROMFS_MODS", "ROMFS_PATCHES",
|
||||
"ROMFS_PROOF_DIR", "ROMFS_VERIFY", "RSTICK_DEADZONE", "RUMBLE", "RUMBLE_CURVE", "RUMBLE_STRENGTH", "RUMBLE_TRIGGERS", "SHADER_CACHE",
|
||||
"SHADER_CACHE_FLUSH", "SHADER_CARRY", "SHADER_MEMO", "SHADOW_COMPARE", "SHADOW_RANGE", "SHADOW_RES", "SHADOW_SCALE", "SHARED_FONTS", "SHARPNESS", "SHOTS", "SHOTS_DIR",
|
||||
"SHADER_CACHE_FLUSH", "SHADER_CARRY", "SHADER_MEMO", "SHADOW_BULK_WRITE", "SHADOW_COMPARE", "SHADOW_RANGE", "SHADOW_RES", "SHADOW_SCALE", "SHARED_FONTS", "SHARPNESS", "SHOTS", "SHOTS_DIR",
|
||||
"SVC_SLOW_MS",
|
||||
"SHOTS_FULL", "SPEED_PROBE", "SPIN", "SPIN_HANDOFF_US", "SPLIT_DRAWS", "SPLIT_IDLE", "SPLIT_IN_PASS", "SPLIT_US", "SPV_MARK_TEST", "SPV_OVERRIDE", "STICK_DEADZONE", "STICK_SENS",
|
||||
"SWKBD_ANSWER",
|
||||
@@ -123,7 +123,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"},
|
||||
{"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"}, {"WB_LAZY", "performance"}, {"WB_LAZY_TOUCH", "diagnostics"}, {"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"}, {"WB_LAZY_TOUCH", "diagnostics"}, {"FLUSH_UPLOAD", "diagnostics"}, {"GPA_TWIN_MAX_KB", "diagnostics"},
|
||||
{"WB_MERGE_MAX", "diagnostics"}, {"MV_SURVEY", "diagnostics"}, {"TEXUP_LOG_MS", "diagnostics"},
|
||||
{"VMA_BLOCK_MB", "diagnostics"}, {"ASTC_BC_PROBE", "diagnostics"},
|
||||
};
|
||||
@@ -169,6 +169,7 @@ static const CfgMeta metas[] = {
|
||||
{"DEVICE_PROFILE", "auto", "auto|desktop|deck", RESTART},
|
||||
{"SHADOW_RES", "game", "game|256|512|768|1024|1536|2048", RESTART},
|
||||
{"SHADOW_SCALE", "1", "1..4", RESTART}, {"SHADOW_COMPARE", "1", "0|1", RESTART}, // vk_texture.c
|
||||
{"SHADOW_BULK_WRITE", "0", "0|1", RESTART}, // vk_memory.c shadow_write_back
|
||||
{"SVC_SLOW_MS", "0", "0..60000", RESTART}, // kernel.c kern_svc: log services slower than this
|
||||
{"SHADOW_RANGE", "100", "50..400", RESTART}, {"CONTACT_SHADOWS", "0", "0..100", RESTART},
|
||||
{"PCSS", "0", "0..200", RESTART},
|
||||
|
||||
@@ -1191,6 +1191,14 @@ int plat_mem_census(uint64_t *priv, uint64_t *wc, uint64_t *wc_allocs) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int plat_mem_prepare_write(uint64_t addr, uint64_t n) {
|
||||
if (ww_fd < 0 || !n || addr >= PS_LIMIT || n > PS_LIMIT - addr) return 0;
|
||||
uint64_t lo = addr & ~(PG - 1), hi = (addr + n + PG - 1) & ~(PG - 1);
|
||||
struct uffdio_writeprotect wp = {.range = {lo, hi - lo}, .mode = 0};
|
||||
// PAGEMAP_SCAN observes cleared protection as written, just as after an asynchronous write fault.
|
||||
return ioctl(ww_fd, UFFDIO_WRITEPROTECT, &wp) == 0;
|
||||
}
|
||||
|
||||
uint64_t plat_mem_written(uint64_t addr, uint64_t n, uint64_t *pages, uint64_t max, int reset) {
|
||||
if (!max || !n) return 0;
|
||||
if (ww_fd < 0) return UINT64_MAX;
|
||||
|
||||
@@ -254,6 +254,10 @@ int plat_mem_census(uint64_t *priv, uint64_t *wc, uint64_t *wc_allocs);
|
||||
// The pages of a watched reservation written since the last reset, up to max page addresses into pages, in address
|
||||
// order; reset: those reported start over as unwritten. UINT64_MAX when writes to the range are not tracked.
|
||||
uint64_t plat_mem_written(uint64_t addr, uint64_t n, uint64_t *pages, uint64_t max, int reset);
|
||||
// Before the host itself writes [addr, addr+n) of a watched reservation: clears the write watch's protection of those
|
||||
// pages in one call, where the watch works by protecting pages (Linux), so the stores do not fault page by page. The
|
||||
// pages count as written. Returns 0 when there is nothing to clear or it cannot be done; the write is valid either way.
|
||||
int plat_mem_prepare_write(uint64_t addr, uint64_t n);
|
||||
// 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));
|
||||
|
||||
@@ -829,6 +829,11 @@ int plat_mem_census(uint64_t *priv, uint64_t *wc, uint64_t *wc_allocs) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
int plat_mem_prepare_write(uint64_t addr, uint64_t n) {
|
||||
(void)addr; (void)n;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Contains code Copyright (c) 2026 Peiikai (MIT; see THIRD_PARTY_NOTICES.md).
|
||||
// GetWriteWatch with the reset flag.
|
||||
uint64_t plat_mem_written(uint64_t addr, uint64_t n, uint64_t *pages, uint64_t max, int reset) {
|
||||
|
||||
@@ -1283,10 +1283,24 @@ static int wb_no_reup(void) {
|
||||
return wb_reup_off;
|
||||
}
|
||||
|
||||
// NX2NAVI_SHADOW_BULK_WRITE=1 (default 0): before a write-back stores into guest memory, the write watch's protection
|
||||
// of the whole range is cleared in one call (plat_mem_prepare_write). Where the watch protects pages (Linux), the
|
||||
// first store into each page otherwise takes a fault of its own; the pages count as written either way.
|
||||
static int shadow_bulk_write(void) {
|
||||
static int on = -1;
|
||||
if (on < 0) {
|
||||
on = cfg_bool("NX2NAVI_SHADOW_BULK_WRITE");
|
||||
if (on) rt_log("mem: shadow write-backs clear the write watch's protection of their range in one call "
|
||||
"(NX2NAVI_SHADOW_BULK_WRITE)");
|
||||
}
|
||||
return on;
|
||||
}
|
||||
|
||||
// Completion thread: the GPU's bytes of [addr, addr+size) from the shadow into guest memory (merge_page against the
|
||||
// base copies; a page without one whole). Returns the bytes moved.
|
||||
static uint64_t shadow_write_back(uint64_t addr, uint64_t size) {
|
||||
uint64_t moved = 0;
|
||||
int bulk = shadow_bulk_write(); // before the locks: its first call logs
|
||||
plat_mutex_lock_shared(&pool_lock);
|
||||
const pidx_ent *e = pidx_find(&pool_index, addr, size);
|
||||
PoolRec *r = e ? e->buf : NULL;
|
||||
@@ -1294,6 +1308,7 @@ static uint64_t shadow_write_back(uint64_t addr, uint64_t size) {
|
||||
if (r && r->smap && addr >= r->import_lo && addr + size <= r->import_hi) {
|
||||
uint64_t t0 = qpc();
|
||||
mem_thaw(addr, size);
|
||||
if (bulk) plat_mem_prepare_write(addr, size);
|
||||
for (uint64_t a = addr, end = addr + size; a < end;) {
|
||||
uint64_t pg = a & ~(SB_PAGE - 1), pe = pg + SB_PAGE < end ? pg + SB_PAGE : end;
|
||||
uint8_t *base = sbase_find(r, pg), *s = r->smap + (a - r->import_lo);
|
||||
|
||||
32
tests/platform_bulk_write_test.c
Normal file
32
tests/platform_bulk_write_test.c
Normal file
@@ -0,0 +1,32 @@
|
||||
// plat_mem_prepare_write on Linux (runtime/platform/linux.c): every page of a prepared range counts as written, a
|
||||
// reset leaves it clean, and a later store to one page is still seen. No game files. Build and run from the
|
||||
// repository root:
|
||||
// clang -O2 -ffunction-sections -fdata-sections -Iruntime -c runtime/platform/linux.c -o /tmp/plat.o
|
||||
// clang -O2 -Wall -Iruntime tests/platform_bulk_write_test.c /tmp/plat.o -Wl,--gc-sections -lpthread -ldl -o /tmp/bulk
|
||||
// /tmp/bulk (exit 77: the write watch is not available on this kernel)
|
||||
#include <assert.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "platform/platform.h"
|
||||
int main(void) {
|
||||
const uint64_t a = 0x6000000000ull, n = 4 * 4096;
|
||||
uint64_t pages[4];
|
||||
assert(plat_mem_reserve(a, n, 1));
|
||||
assert(plat_mem_commit(a, n));
|
||||
memset((void *)(uintptr_t)a, 0, n);
|
||||
uint64_t count = plat_mem_written(a, n, pages, 4, 1);
|
||||
if (count == UINT64_MAX) { puts("SKIP: asynchronous write watch unavailable"); return 77; }
|
||||
assert(plat_mem_written(a, n, pages, 4, 0) == 0);
|
||||
assert(plat_mem_prepare_write(a + 7, n - 14));
|
||||
memset((void *)(uintptr_t)(a + 7), 0x5a, n - 14);
|
||||
assert(plat_mem_written(a, n, pages, 4, 1) == 4);
|
||||
for (unsigned i = 0; i < 4; i++) assert(pages[i] == a + i * 4096);
|
||||
assert(plat_mem_written(a, n, pages, 4, 0) == 0);
|
||||
*(volatile unsigned char *)(uintptr_t)(a + 2 * 4096) = 0xa5;
|
||||
assert(plat_mem_written(a, n, pages, 4, 1) == 1 && pages[0] == a + 2 * 4096);
|
||||
assert(!plat_mem_prepare_write(a, 0));
|
||||
assert(!plat_mem_prepare_write(UINT64_MAX - 7, 16));
|
||||
puts("bulk write watch: full range tracked, reset clean, subsequent single-page write tracked");
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user