mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-15 23:39:57 -04:00
vk_memory.c handed out page write generations from a 32-bit counter (gen_now) and every consumer compared them as 32-bit values, with 0 meaning "never stamped". At 124 to 336 generations a frame (a 0.10.0 log with trace = perf) the counter wrapped after about 66 h of play at 60 fps, 27.6 h at 144 and 15 to 17 h at 240. Past the wrap a stamp made before it compared newer than every later check and one made after it older: textures and VRAM mirrors missed the CPU's writes (stale textures, and the lazy fetch copying older mirror bytes over a CPU write), pages nothing wrote read as written at every check (a hash or upload every time, and every such page of a mirrored pool copied up at each sync), and the generation handed out at the wrap was 0. At 64 bits nothing wraps: 2^64 lasts millions of years at 336 a frame and 240 fps. Below 2^32 every value and every compare is the same as before. What changed: gen_now, both tables (gen_chunk, cgen_chunk), reg_max and creg_max, PoolRec's synced_gen, mgen and sgen, dirty_gen and DIRTY_UNSYNCED, VSample.mgen, every local and parameter that holds a generation in vk_memory.c, vk_texture.c (Tex.wgen and gpu_gen, PEnt.since, storage_rewritten and its callers, sweep_hard's write-back check included) and tex_probe.c, and vk.h's vk_mem_changed, vk_mem_changed_snap and vk_mem_unchanged(_why)_locked. The verify mode's mismatch line prints the generations with %llu. The readers that take no gen_lock (lazy_clean, cpu_wrote_since, verify_cb, vk_mem_sizes) stay lock-free at 64 bits: the slots, mgen, gen_now and a new chunk pointer are now stored atomically (relaxed, or release where a reader acquires), the same instructions on x86-64, and a static assertion holds that 64-bit atomics are lock-free. No lock, wait or thread changes. gen_next, which hands out every generation, no longer warns at 0xC0000000 and up or at the wrap, which cannot come; it logs one line when the count reaches 2^32, where earlier versions ran out, with the frame, the hours and the rate. Cost: the tables take 8 bytes a page instead of 4 (512 KiB more per 256 MiB of guest address space stamped, both tables together, allocated as before when a chunk is first stamped), reg_max and creg_max 2 MiB each instead of 1, Tex and PoolRec 8 bytes more, PEnt 8 more. The page walks read 8 bytes a slot; a micro-benchmark of the walk measured no difference. Not reproduced in the game before the change: the wrong answers were reproduced by compiling vk_memory.c's generation code with the counter seeded below 2^32. tests/gen_wrap_check now runs that code, with the readers outside its section, across 2^32, 2^40 and 2^63 and checks that the compares answer as below the boundary; on the code before the change 19 of its checks fail at 2^32 and the other two seeds do not fit the counter. The new tests/gen_width_check compiles vk_memory.c, vk_texture.c and tex_probe.c with -Wshorten-64-to-32, -Wformat and -Wincompatible-pointer-types as errors, so a 32-bit local left behind is found (shown by reverting single locals, fields and formats). Checks that passed: gen_wrap_check, gen_width_check, pass_cc_check of every runtime file, no new -Wall -Wextra warnings in the three files, tex_release_check, tex_compact_check, wb_lazy_check, gpa_check, mem_growth_check, mem_budget_check. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
112 lines
4.5 KiB
Python
112 lines
4.5 KiB
Python
"""Build and run tests/gen_wrap_check.c: runtime/vk_memory.c's page write generations across 2^32, 2^40 and 2^63.
|
|
|
|
From runtime/vk_memory.c it takes, verbatim, into build/test/gen_wrap/gen_section.inc:
|
|
- the declarations of PoolRec's mgen and sgen (the generations a VRAM mirror and a shadow copy hold), so the stand-in
|
|
PoolRec has their real width;
|
|
- the section from the "page write generations" banner to the "dirty census" banner;
|
|
- the readers of the tables outside that section: cpu_wrote_since, shadow_pool_clean, lazy_clean and page_dirty.
|
|
The program starts the generations just below each seed's boundary and checks that every compare answers across it as
|
|
below it (texture checks, the snapshot path, the region check, the CPU-side tables, mirror and shadow cleanliness),
|
|
and that gen_next logs one line when the count reaches 2^32 and no other.
|
|
|
|
No GPU, no game files, any Python.
|
|
|
|
Usage: gen_wrap_check.py [--cc <clang>]
|
|
"""
|
|
import argparse
|
|
import os
|
|
import re
|
|
import shutil
|
|
import subprocess
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
MSYS_BIN = Path(r'C:\msys64\ucrt64\bin')
|
|
BEGIN = '// ---------------------------------------------------------------- page write generations'
|
|
END = '// ---------------------------------------------------------------- dirty census'
|
|
READERS = ('static int cpu_wrote_since(', 'static int shadow_pool_clean(', 'static int lazy_clean(',
|
|
'static int page_dirty(')
|
|
SEEDS = ((1 << 32) - 8, (1 << 40) - 8, (1 << 63) - 8) # the old wrap; low 32 bits through 0 again; near 2^63
|
|
|
|
|
|
def compiler(cc):
|
|
if cc:
|
|
return cc
|
|
if os.environ.get('CC'):
|
|
return os.environ['CC']
|
|
if os.name == 'nt' and (MSYS_BIN / 'clang.exe').exists():
|
|
return str(MSYS_BIN / 'clang.exe')
|
|
return shutil.which('clang') or 'clang'
|
|
|
|
|
|
def definition(src, start):
|
|
"""The definition that begins with start (not a prototype), up to its closing brace at column 0."""
|
|
i = 0
|
|
while True:
|
|
i = src.find(start, i)
|
|
if i < 0:
|
|
return None
|
|
eol = src.index('\n', i)
|
|
if src[eol - 1] == '{':
|
|
return src[i:src.index('\n}\n', i) + 3]
|
|
i = eol
|
|
|
|
|
|
def extract(src):
|
|
"""gen_section.inc's text, or None and the reason."""
|
|
a, b = src.find(BEGIN), src.find(END)
|
|
if a < 0 or b < a:
|
|
return None, 'the section banners are not in runtime/vk_memory.c'
|
|
s0 = src.find('typedef struct PoolRec {')
|
|
s1 = src.find('} PoolRec;', s0)
|
|
if s0 < 0 or s1 < 0:
|
|
return None, 'PoolRec is not in runtime/vk_memory.c'
|
|
fields = []
|
|
for name in ('mgen', 'sgen'):
|
|
m = re.search(r'^\s*(u?int\d+_t)\s+' + name + r';', src[s0:s1], re.M)
|
|
if not m:
|
|
return None, f'PoolRec.{name} is not declared as an integer of fixed width'
|
|
fields.append(f'{m.group(1)} {name};')
|
|
out = ['// Extracted from runtime/vk_memory.c by tests/gen_wrap_check.py.\n',
|
|
'typedef struct PoolRec { int kind; void *buf; uint64_t import_lo, import_hi; ' + ' '.join(fields) +
|
|
' } PoolRec;\n', src[a:b]]
|
|
for start in READERS:
|
|
d = definition(src, start)
|
|
if not d:
|
|
return None, f'{start.split()[-1]}...) is not in runtime/vk_memory.c'
|
|
out.append(d)
|
|
return '\n'.join(out), None
|
|
|
|
|
|
def main():
|
|
ap = argparse.ArgumentParser()
|
|
ap.add_argument('--cc', help='the C compiler (default: $CC, MSYS2 UCRT64 clang, clang on PATH)')
|
|
args = ap.parse_args()
|
|
inc, why = extract((ROOT / 'runtime' / 'vk_memory.c').read_text(encoding='utf-8-sig'))
|
|
if not inc:
|
|
print(f'gen_wrap_check: FAIL: {why}')
|
|
return 1
|
|
work = ROOT / 'build' / 'test' / 'gen_wrap'
|
|
work.mkdir(parents=True, exist_ok=True)
|
|
(work / 'gen_section.inc').write_text(inc, encoding='utf-8')
|
|
exe = work / ('gen_wrap_check.exe' if os.name == 'nt' else 'gen_wrap_check')
|
|
env = dict(os.environ)
|
|
if os.name == 'nt' and MSYS_BIN.exists():
|
|
env['PATH'] = str(MSYS_BIN) + os.pathsep + env.get('PATH', '')
|
|
subprocess.check_call([compiler(args.cc), '-std=gnu11', '-O1', '-Wall', '-Wno-unused-function',
|
|
'-Wno-unused-variable', '-I', str(work), str(ROOT / 'tests' / 'gen_wrap_check.c'), '-o',
|
|
str(exe)], env=env)
|
|
bad = 0
|
|
for seed in SEEDS:
|
|
bad += subprocess.run([str(exe), str(seed)], env=env, timeout=120).returncode != 0
|
|
if bad:
|
|
print(f'gen_wrap_check: {bad} of {len(SEEDS)} runs failed')
|
|
return 1
|
|
print('gen_wrap_check: all checks passed')
|
|
return 0
|
|
|
|
|
|
if __name__ == '__main__':
|
|
sys.exit(main())
|