Close a frame's replay state under the submission lock: at the present when the lock is free, else first thing in the next submission, so another thread's replay never sees it reset halfway.

present_image ran the frame's bookkeeping on the presenting thread without a lock: vk_exec_frame_log (the trace counts
and vk_exec_cond_frame_end's frame count, which the conditional draws go by), dynres_frame, vk_exec_barrier_log,
vk_dlaa_frame (the DLSS mode and its LOD bias, the frame's points, the swap image, the idle releases), vk_mvsurvey_frame,
vk_mvprev_frame (which swaps the two motion tables the draws insert into) and vk_gpu_passes_frame. All of it is state the
replay reads and writes as it records, under sub_lock. With the replay on the submitting threads the game presents on
one thread while another may be replaying (the overlap lines of the 0.10.0 fresh session, at area changes), so the
present reset it in the middle of that replay: a DLSS frame reset halfway, the motion tables swapped halfway (with
MVPREV_CHECK=1 an index out of bounds), the conditional draws counted against the next frame.

Now the bookkeeping is frame_end, and runtime/vk_frame_owed.h decides where it runs. present_image marks its frame owed
and runs it itself only when it gets sub_lock at once (vk_sync.c vk_sync_try_locked); otherwise vk_sync.c submit runs
every owed frame's, in frame order, right after it takes the lock and before its replay (vk_present_frames_owed), on
whichever thread submits next, the present thread's blit submission included. The presenting thread never waits for
another thread's replay for it: no new wait on a drawing thread; the thread that takes the lock next does the work it
would have done at the present, tens of microseconds normally. The hand-off, vk_texture_get with its upload flush and
vk_present_drain stay outside the lock, as frame_end must not wait for the present thread or submit; its callees were
checked for that. vk_sampler_aniso_live and vk_mem_census keep their place in present_image (their own locks). In the
asynchronous replay the other holder of sub_lock is the present thread's blit, and the same applies.

NX2NAVI_PRESENT_ORDER (present_order in [diagnostics], default 1, read at start) puts it back at the present with 0, as
in 0.10.1: metas, known and sections rows and an nx2navi.ini.example entry.

Not reproduced before the change. Checks that passed: the new tests/frame_owed_check (one presenting and three
replaying threads, some replays holding the lock 40 ms, over 20,000 frames: every frame's bookkeeping once, in order,
never during a replay and before any replay that took the lock after its present; the longest present under 1 ms with
19,000 presents made during a long hold), cfg_rows_check with 0 problems, pass_cc_check on every runtime file (headers
changed), no new -Wall -Wextra warnings in vk_backend.c, vk_sync.c, vk_exec.c and cfg.c.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
chrg6
2026-11-13 19:07:06 -05:00
parent a789bd8fb0
commit 51c9261db3
10 changed files with 301 additions and 8 deletions

View File

@@ -774,6 +774,13 @@ each reason a held present goes out seen; step by step what keeps and what puts
held elsewhere a submitting thread gives up at once while the presenting thread waits and counts its wait. No GPU, no
game files.
A change to where a frame's bookkeeping at its present runs (`runtime/vk_frame_owed.h`, `runtime/vk_backend.c`
`present_image` and `frame_end`, `runtime/vk_sync.c` `submit` and `vk_sync_try_locked`) passes
`python tests\frame_owed_check.py`, which builds the header with the platform layer: one presenting and three
replaying threads, some replays holding the lock 40 ms, over 20,000 frames; every frame's bookkeeping runs once, in
order, never during a replay and before any replay that took the lock after its present, and no present waits for a
replay. No GPU, no game files.
A change to how `runtime/memory.c` `mem_copy_readable` reads the lazy heap (`NX2NAVI_HEAP_LAZY=1`) passes
`python tests\heap_lazy_copy_check.py`, which builds `memory.c` with the Windows platform layer and its fault handler:
every copy returns, a heap page nothing touched yet reads zero and stays reserved, and pages past the heap's end are

View File

@@ -1011,6 +1011,11 @@
# it waits for and what that one is doing (at most 24 lines a run): a game that stops then leaves the chain in its log.
# Off by default: its thread takes the game's thread lock every 2 seconds. 10 is a good value for a hang report.
# wait_watch_s = 0
# 1: what the game's present closes for the frame (the DLSS state, the motion tables, the frame count conditional
# draws go by) is closed between two replays: when another game thread is replaying at the present, the next
# submission does it first thing, and the present goes on at once. 0: at the present, as in 0.10.1, whatever another
# thread replays (a rare one-frame fault at area changes). Read at start.
# present_order = 1
# A report in the log when nothing moves for this many seconds: no frame shown while the window is in front, or a GPU
# submission not finished. It names who holds the present and queue locks and what the game's threads wait for, at
# most three times a run; the game goes on. 0 turns it off. Read at start.

View File

@@ -64,6 +64,7 @@ static const char *const known[] = {
"MV_SURVEY",
"HANDLE_CENSUS",
"PIPE_SEED_BOOT", "ONE_PER_CORE", "OPERATION_MODE", "OUTPUT_RES", "PAD_LAYOUT", "PACE_HZ", "PASS_CRUMBS", "PATCH_OUTPUT", "PATCH_RES_FIXED", "PATCH_SCENE", "PATCH_WIDE_ASPECT", "PATCH_WIDE_HUD", "PATCH_WIDE_MOVIE", "PATCH_WIDE_OUT", "PATCH_FREECAM", "PATCH_OCCLUSION_OFF", "PATCH_WIDE_SAVE", "PATCH_WIDE_SCENE", "PCSS", "PCSS_K", "PERF_OVERLAY", "PERF_SLOW_MS", "PIPELINE_CACHE", "PIPELINE_CACHE_MAX_MB", "PIPELINE_CACHE_PLAY", "PIPELINE_CACHE_PLAY_MB", "PIPE_BINARIES", "PIPE_BINARIES_MAX_MB", "PIPE_FEEDBACK", "PIPE_MEMO", "PIPE_PREBUILD", "PIPE_SEED", "PIPE_SKIP", "PIPE_SYNC", "PIPE_WAIT", "POWER_FULL", "TEX_SAME", "TEX_SAME_BIG", "TEX_FLAT3D", "TEX_KEEP_TAKEN", "TEX_HARD_ORDER", "TEX_STALE_KEPT", "ASTC_SCRATCH_TRIM", "PRESENT_HELD", "HELD_SYNC",
"PRESENT_ORDER",
"PREPARE_PIPELINES", "PREWARM_FULL", "PREWARM_FULL_S", "PREWARM_WAIT_MS", "PREWARM_XLATE", "PROBE_MOVIE", "PROBE_MOVIE_SHOT", "PROFILE", "PROFILE_CHAINS", "PROFILE_DIR", "PROFILE_FRAMES", "PROFILE_REPLAY", "PROFILE_SLOW",
"POOL_FAST", "POOL_GROUP", "POOL_GROUP_CHECK", "POOL_PRESNAP", "POOL_PRESNAP_RUNS",
"SUBMIT_FINISH_GPU", "SVC_WAITS", "SNAP_WIDEN", "SUBMIT_FINISH_RESULTS", "SUBMIT_FINISH_KINDS", "FOLIAGE", "MIRROR_STAGE", "FLUSH_SKIP_CLEAN", "MIRROR_HOT", "MESH_EMULATION", "MESH_EMU_MB", "MESH_EMU_TASKS", "MESH_EMU_BATCH", "GUEST_OPT", "LOG_TEX_NEW", "DUMP_KEY", "VEC_INDEX_FIX", "PASS_TIMES",
@@ -177,6 +178,7 @@ static const struct { const char *key, *section; } sections[] = {
{"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"}, {"HEAP_LAZY", "performance"}, {"BARRIER_GAME", "performance"}, {"MESH_AHEAD", "performance"}, {"MESH_FALLBACK", "performance"}, {"MESH_FALLBACK_MB", "performance"}, {"MESH_FALLBACK_WORKERS", "performance"}, {"WB_LAZY_TOUCH", "diagnostics"}, {"WB_LAZY_UNLOCKED", "diagnostics"}, {"WB_LAZY_CHUNK", "performance"}, {"FLUSH_UPLOAD", "diagnostics"}, {"GPA_TWIN_MAX_KB", "diagnostics"}, {"MIRROR_CAPTURE_KB", "diagnostics"}, {"WB_SKIP_MB", "diagnostics"},
{"WB_MERGE_MAX", "diagnostics"}, {"MV_SURVEY", "diagnostics"}, {"TEXUP_LOG_MS", "diagnostics"},
{"WAIT_WATCH_S", "diagnostics"},
{"PRESENT_ORDER", "diagnostics"},
{"HANDLE_CENSUS", "diagnostics"},
{"HANG_WATCH_S", "diagnostics"},
{"VMA_BLOCK_MB", "diagnostics"}, {"DEVICE_LOST_AT", "diagnostics"}, {"STAGING_FAIL_AT", "diagnostics"}, {"VRAM_BALLAST_MB", "diagnostics"},
@@ -442,6 +444,8 @@ static const CfgMeta metas[] = {
{"MIRROR_CAPTURE_KB", "0", "0..1048576", RESTART}, {"WB_SKIP_MB", "0", "0..4096", RESTART}, // vk_memory.c capture_cap, wb_skip
{"WB_MERGE_MAX", "32", "1..256", RESTART}, {"TEXUP_LOG_MS", "0", "0..60000", RESTART}, // vk_texture.c vk_texstat_frame
{"WAIT_WATCH_S", "0", "0..3600", RESTART}, // kernel.c wait_watch: a lock or thread-end wait this long is logged; 0 off
// vk_backend.c present_order_on: a frame's bookkeeping between two replays (vk_frame_owed.h); 0 at the present
{"PRESENT_ORDER", "1", "0|1", RESTART},
// perf.c hc_main: with NX2NAVI_TRACE=perf, the process's handles by kind when their count grew
{"HANDLE_CENSUS", "0", "0|1", RESTART},
// hang_watch.c: a report when no frame is shown with the window in front, or the GPU does not finish, this long; 0 off

View File

@@ -895,6 +895,12 @@ uint64_t vk_sync_submit(const uint64_t *handles, int n); // records with vk_ex
// The probes (--probe-vkmem's table parts, the lost-device probe): one submission whose commands the caller records
// itself where the replay would, with the submission lock, the memory prologue and the fence of any other.
uint64_t vk_sync_submit_own(void (*record)(VkCommandBuffer, void *), void *arg);
// Runs fn() under the submission lock when it is free now and returns 1; returns 0 at once, without running it, when
// another thread holds the lock (vk_backend.c present_image: a frame's bookkeeping never waits for another replay).
int vk_sync_try_locked(void (*fn)(void));
// vk_backend.c (vk_frame_owed.h): the bookkeeping of the frames presented while another thread held the submission
// lock. vk_sync.c submit calls it right after taking the lock, before its replay; a no-op without the back end.
void vk_present_frames_owed(void);
uint64_t vk_sync_reserve(void); // asynchronous replay: the value the next queued submission signals
int vk_sync_fence_wait(uint64_t value, uint64_t timeout_ns); // 1 when done; 0 on timeout or a value never submitted
// The executed timeline (vk_sync.c): a submission's own commands are through the GPU, its write-backs maybe not yet.

View File

@@ -37,6 +37,7 @@
#include "vk_dlssg.h"
#include "vk_fg.h"
#include "vk_lowlat.h"
#include "vk_frame_owed.h"
#include "vk_present_held.h"
#define WEAK __attribute__((weak))
@@ -266,6 +267,61 @@ static int frame_hold(uint32_t skipped, int resized, uint64_t frame) {
return hold;
}
// ---- a frame's bookkeeping in replay order (vk_frame_owed.h)
// A present closes the frame for the replay (frame_end): the trace and conditional-render frame counts
// (vk_exec_frame_log, whose vk_exec_cond_frame_end moves the frame count the conditional draws go by), dynamic
// resolution's step, the DLSS state of the frame (vk_dlaa_frame: the mode and its LOD bias, the frame's points and
// the swap image, the idle releases), the motion tables (vk_mvsurvey_frame, vk_mvprev_frame swaps its two) and the GPU
// pass timing. The replay reads and writes all of it as it records, under the submission lock. With the replay on the
// submitting threads the game presents on one thread while another may be replaying, and running it at the present
// then reset it in the middle of that replay: a DLSS frame reset halfway (a shake, a frame without the upscaler), the
// motion tables swapped halfway (a ghost; with NX2NAVI_MVPREV_CHECK=1 a write out of bounds), the conditional draws
// counted against the next frame. So it runs under the submission lock: at the present when the lock is free, else
// first thing in the next submission, on whichever thread makes it (vk_sync.c submit, vk_present_frames_owed), the
// present thread's blit submission included. The present never waits for another thread's replay for it. Nothing in
// frame_end may wait for the present thread, submit or flush with a wait: the hand-off, vk_texture_get with its upload
// flush and vk_present_drain stay in present_image, outside the lock. NX2NAVI_PRESENT_ORDER=0: at the present, whatever
// another thread replays, as in 0.10.1.
static void frame_end(uint64_t frame, const tex_t *t) { // t: the presented texture, for the trace = gfx line
vk_exec_frame_log(t, frame);
dynres_frame(); // before vk_dlaa_frame reads the DLSS mode it may override
vk_exec_barrier_log(frame);
vk_dlaa_frame();
vk_mvsurvey_frame();
vk_mvprev_frame();
vk_gpu_passes_frame(frame);
}
#define FO_TEX 4
static FrameOwed FO;
static const tex_t *fo_tex[FO_TEX]; // the newest frames' presented textures, by frame % FO_TEX (atomics)
static uint64_t fo_owed_n, fo_late_n; // presents that left it to a submission; frames a submission caught up (atomics)
static int present_order_on(void) { // read once; present_image runs one at a time
static int on = -1;
if (on < 0) {
on = cfg_bool("NX2NAVI_PRESENT_ORDER");
if (!on) rt_log("vk: NX2NAVI_PRESENT_ORDER=0: a frame's bookkeeping runs at the present, whatever another thread "
"replays");
}
return on;
}
static void fo_end(uint64_t frame, void *arg) {
(void)arg;
frame_end(frame, __atomic_load_n(&fo_tex[frame % FO_TEX], __ATOMIC_RELAXED));
}
static void fo_here(void) { fo_run(&FO, fo_end, NULL); }
static int fo_try(void *arg) {
(void)arg;
return vk_sync_try_locked(fo_here);
}
// vk_sync.c submit, sub_lock held, before its replay: the frames presented while another thread held the lock.
void vk_present_frames_owed(void) {
if (fo_waiting(&FO)) __atomic_add_fetch(&fo_late_n, fo_run(&FO, fo_end, NULL), __ATOMIC_RELAXED);
}
// The present after the frame count: resolves the image and hands the blit to the present thread. queued: when the game
// presented; value: the timeline value queued for this present (asynchronous replay), 0 for the next one (synchronous).
static void present_image(const tex_t *t, const int crop[4], uint64_t at_ns, uint64_t queued, uint64_t frame,
@@ -273,15 +329,14 @@ static void present_image(const tex_t *t, const int crop[4], uint64_t at_ns, uin
static volatile int32_t n_skipped;
static int vram_probe;
if (!vram_probe) vram_probe = 1, perfmon_set_vram_game(vk_mem_device_used_mb); // the overlay's "game" beside VRAM
vk_exec_frame_log(t, frame);
dynres_frame(); // before vk_dlaa_frame reads the DLSS mode it may override
vk_exec_barrier_log(frame);
vk_dlaa_frame();
if (present_order_on()) {
__atomic_store_n(&fo_tex[frame % FO_TEX], t, __ATOMIC_RELAXED);
if (!fo_present(&FO, frame, fo_try, NULL)) __atomic_add_fetch(&fo_owed_n, 1, __ATOMIC_RELAXED);
} else {
frame_end(frame, t);
}
vk_sampler_aniso_live(); // NX2NAVI_ANISO changed in the menu: the surface textures' samplers take the new level
vk_mvsurvey_frame();
vk_mvprev_frame();
vk_mem_census(frame);
vk_gpu_passes_frame(frame);
if (vk_dump_armed) vk_dump_present(frame);
__atomic_store_n(&gfx_counters.validation_errors, (uint64_t)vk_validation_errors_total(), __ATOMIC_RELAXED);
const VkTexImage *img = vk_texture_get(t);

View File

@@ -195,7 +195,9 @@ static void runs_frame(uint64_t frame) {
uint64_t vk_exec_submit_seq;
void vk_exec_frame_log(const tex_t *p, uint64_t frame) {
vk_exec_cond_frame_end(frame); // a frame boundary in replay order (present_image runs on the replay thread)
// a frame boundary in replay order: vk_backend.c present_image runs it under the submission lock, or the next
// submission does (vk_frame_owed.h)
vk_exec_cond_frame_end(frame);
runs_frame(frame);
if (!(rt_trace & RT_TRACE_GFX)) return;
VkExecFrame *f = &vk_exec_frame;

37
runtime/vk_frame_owed.h Normal file
View File

@@ -0,0 +1,37 @@
// A frame's bookkeeping at its present (vk_backend.c frame_end: the replay's per-frame state that a present closes), in
// replay order. Header only, so that tests/frame_owed_check.c checks it without a device.
//
// The replay reads and writes that state as it records, under the submission lock (vk_sync.c sub_lock). With the
// replay on the submitting threads the game presents on one thread while another may be replaying, and running the
// bookkeeping then reset the state in the middle of that replay. The presenting thread does not wait for the replay:
// it marks its frame owed and runs the bookkeeping itself only when it gets the lock at once; otherwise the next thread
// to take the lock (vk_sync.c submit) runs every owed frame's bookkeeping, in frame order, before its own replay. A frame
// caught up behind a later present runs as two presents with no submission between them always did.
#pragma once
#include <stdint.h>
typedef struct {
uint64_t owed; // the newest presented frame (atomic: the presenting thread raises it, the lock's holder reads it)
uint64_t done; // the newest frame whose bookkeeping ran (the lock's holder only)
} FrameOwed;
typedef void (*FrameEnd)(uint64_t frame, void *arg);
// The lock's holder: whether a frame waits for its bookkeeping (one atomic load, for every submission).
static inline int fo_waiting(FrameOwed *o) { return __atomic_load_n(&o->owed, __ATOMIC_ACQUIRE) != o->done; }
// The lock's holder: the bookkeeping of every frame presented since the last run, in frame order; returns how many.
static inline uint64_t fo_run(FrameOwed *o, FrameEnd end, void *arg) {
uint64_t owed = __atomic_load_n(&o->owed, __ATOMIC_ACQUIRE), n = 0;
while (o->done < owed) end(++o->done, arg), n++;
return n;
}
// The presenting thread, frame numbers rising (a smaller one than owed leaves it: that frame's bookkeeping ran or is
// owed already). try_run(arg) takes the lock without waiting, runs fo_run under it and returns 1, or returns 0 when
// another thread holds it. Returns whether the bookkeeping ran here.
static inline int fo_present(FrameOwed *o, uint64_t frame, int (*try_run)(void *), void *arg) {
uint64_t was = __atomic_load_n(&o->owed, __ATOMIC_RELAXED);
while (was < frame && !__atomic_compare_exchange_n(&o->owed, &was, frame, 1, __ATOMIC_RELEASE, __ATOMIC_RELAXED)) {}
return try_run(arg);
}

View File

@@ -1482,6 +1482,7 @@ static uint64_t submit(const uint64_t *handles, int n, const OwnCommands *own) {
if (vk_device_is_lost()) return __atomic_load_n(&issued, __ATOMIC_ACQUIRE);
VkCtx *c = vk_ctx();
plat_mutex_lock(&sub_lock);
vk_present_frames_owed(); // frames presented while another thread replayed close before this replay begins
vk_mem_dirty_submit(); // NX2NAVI_TRACE=perf: what a mirror would upload before this submit
uint64_t fence = __atomic_load_n(&issued, __ATOMIC_ACQUIRE) + 1;
cur_fence = fence;
@@ -1573,6 +1574,16 @@ uint64_t vk_sync_submit_own(void (*record)(VkCommandBuffer, void *), void *arg)
return submit(NULL, 0, &own);
}
int vk_sync_try_locked(void (*fn)(void)) {
if (!plat_mutex_trylock(&sub_lock)) return 0;
fn();
plat_mutex_unlock(&sub_lock);
return 1;
}
// vk_backend.c's, which runs the frames' bookkeeping (vk_frame_owed.h); builds without the back end have none.
WEAK void vk_present_frames_owed(void) {}
// st_lock held: p at the end of the queue *q.
static void pending_push(Pending **q, int *head, int *n, int *cap, Pending p) {
if (*head + *n == *cap) {

111
tests/frame_owed_check.c Normal file
View File

@@ -0,0 +1,111 @@
// tests/frame_owed_check.py builds this: runtime/vk_frame_owed.h, a frame's bookkeeping in replay order, with one
// presenting thread and three replaying threads that hold the lock for their replays, a few of them for HOLD_MS. Every
// frame's bookkeeping must run once and in frame order, never during a replay, and before the body of any replay that
// took the lock after the frame was presented; the presenting thread must never wait for a replay.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "platform/platform.h"
#include "vk_frame_owed.h"
#define FRAMES 20000
#define REPLAYERS 3
#define HOLD_MS 40
#define PRESENT_MAX_NS 20000000ull // a present that waited for a long hold would take up to HOLD_MS
static int fails;
#define CHECK(c, ...) do { if (!(c)) { printf("frame_owed_check: FAIL: " __VA_ARGS__); printf("\n"); fails++; } } while (0)
static PlatMutex sub_lock = PLAT_MUTEX_INIT;
static FrameOwed FO;
static volatile int32_t in_replay, in_long, stop;
static volatile uint64_t presented, ends, bad_order, during_replay, late, ran_here, long_holds, during_long;
static uint64_t last_end, present_max_ns;
// vk_backend.c frame_end's stand-in, run with the lock held.
static void frame_end(uint64_t frame, void *arg) {
(void)arg;
if (__atomic_load_n(&in_replay, __ATOMIC_ACQUIRE)) __atomic_add_fetch(&during_replay, 1, __ATOMIC_RELAXED);
if (frame != last_end + 1) __atomic_add_fetch(&bad_order, 1, __ATOMIC_RELAXED);
last_end = frame;
__atomic_add_fetch(&ends, 1, __ATOMIC_RELAXED);
for (volatile int i = 0; i < 300; i++) {}
}
// vk_sync.c vk_sync_try_locked with vk_backend.c fo_here
static int try_run(void *arg) {
(void)arg;
if (!plat_mutex_trylock(&sub_lock)) return 0;
fo_run(&FO, frame_end, NULL);
plat_mutex_unlock(&sub_lock);
return 1;
}
// vk_sync.c submit: the lock, the owed frames (vk_present_frames_owed), then the replay's body
static void replayer(void *arg) {
uint64_t r = (uint64_t)(uintptr_t)arg * 0x2545F4914F6CDD1Dull + 3;
while (!__atomic_load_n(&stop, __ATOMIC_ACQUIRE)) {
r ^= r << 13, r ^= r >> 7, r ^= r << 17;
uint64_t p0 = __atomic_load_n(&presented, __ATOMIC_ACQUIRE);
plat_mutex_lock(&sub_lock);
if (fo_waiting(&FO)) fo_run(&FO, frame_end, NULL);
if (FO.done < p0) __atomic_add_fetch(&late, 1, __ATOMIC_RELAXED);
__atomic_store_n(&in_replay, 1, __ATOMIC_RELEASE);
if (r % 997 == 0) {
__atomic_store_n(&in_long, 1, __ATOMIC_RELEASE);
plat_sleep_ms(HOLD_MS);
__atomic_store_n(&in_long, 0, __ATOMIC_RELEASE);
__atomic_add_fetch(&long_holds, 1, __ATOMIC_RELAXED);
} else {
for (volatile uint32_t i = 0; i < r % 20000; i++) {}
}
__atomic_store_n(&in_replay, 0, __ATOMIC_RELEASE);
plat_mutex_unlock(&sub_lock);
for (volatile uint32_t i = 0; i < r % 5000; i++) {}
}
}
int main(void) {
// one thread first: the present runs its own frame's bookkeeping when the lock is free, and two presents with the
// lock held elsewhere leave both frames to the next holder, which runs them in order
int here = fo_present(&FO, 1, try_run, NULL);
CHECK(here && ends == 1 && FO.done == 1, "a present with the lock free did not run its own bookkeeping");
plat_mutex_lock(&sub_lock);
int a = fo_present(&FO, 2, try_run, NULL), b = fo_present(&FO, 3, try_run, NULL);
CHECK(!a && !b && ends == 1 && fo_waiting(&FO), "presents past a held lock ran or lost their bookkeeping");
CHECK(fo_run(&FO, frame_end, NULL) == 2 && ends == 3 && !bad_order && !fo_waiting(&FO),
"the next holder did not catch up both frames in order");
CHECK(fo_present(&FO, 2, try_run, NULL) == 0 && ends == 3 && !fo_waiting(&FO), "an older frame number ran again");
plat_mutex_unlock(&sub_lock);
last_end = 3, ends = 0;
PlatThread *t[REPLAYERS];
for (int i = 0; i < REPLAYERS; i++) t[i] = plat_thread_new(replayer, (void *)(uintptr_t)(i + 1), "replay");
for (uint64_t f = 4; f < 4 + FRAMES; f++) {
int lng = __atomic_load_n(&in_long, __ATOMIC_ACQUIRE);
uint64_t t0 = plat_now_ns();
if (fo_present(&FO, f, try_run, NULL)) ran_here++;
uint64_t d = plat_now_ns() - t0;
if (d > present_max_ns) present_max_ns = d;
if (lng) during_long++;
__atomic_store_n(&presented, f, __ATOMIC_RELEASE);
for (volatile int i = 0; i < 20000; i++) {}
}
__atomic_store_n(&stop, 1, __ATOMIC_RELEASE);
for (int i = 0; i < REPLAYERS; i++) plat_thread_join(t[i]);
plat_mutex_lock(&sub_lock);
fo_run(&FO, frame_end, NULL);
plat_mutex_unlock(&sub_lock);
CHECK(ends == FRAMES, "%llu frame ends for %d frames", (unsigned long long)ends, FRAMES);
CHECK(!bad_order, "%llu frame ends out of order", (unsigned long long)bad_order);
CHECK(!during_replay, "%llu frame ends ran during a replay", (unsigned long long)during_replay);
CHECK(!late, "%llu replays began before the bookkeeping of a frame presented earlier", (unsigned long long)late);
CHECK(present_max_ns < PRESENT_MAX_NS, "a present took %.2f ms", present_max_ns / 1e6);
CHECK(ran_here < FRAMES, "no present found the lock held, so nothing was owed");
printf("frame_owed_check: %d frames: %llu ends ran at the present, %llu owed to a replay; %llu replays held the lock "
"%d ms, %llu presents came during one; the longest present %.3f ms; %s\n", FRAMES,
(unsigned long long)ran_here, (unsigned long long)(FRAMES - ran_here), (unsigned long long)long_holds,
HOLD_MS, (unsigned long long)during_long, present_max_ns / 1e6, fails ? "FAILED" : "passed");
return fails ? 1 : 0;
}

55
tests/frame_owed_check.py Normal file
View File

@@ -0,0 +1,55 @@
"""Build and run tests/frame_owed_check.c: runtime/vk_frame_owed.h, a frame's bookkeeping at its present in replay order
(vk_backend.c "a frame's bookkeeping in replay order", vk_sync.c submit). One presenting thread and three replaying
threads that hold the lock for their replays, a few of them for 40 ms, over 20,000 frames: every frame's bookkeeping
runs once and in frame order, never during a replay, and before the body of any replay that took the lock after the
frame was presented, and no present takes 20 ms, so the presenting thread never waits for a replay. Step by step: a
present with the lock free runs its own frame's, presents past a held lock leave theirs to the next holder, which
runs them in order, and an older frame number runs nothing again.
No GPU, no game files, any Python.
Usage: frame_owed_check.py
"""
import os
import shutil
import subprocess
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
MSYS_BIN = Path(r'C:\msys64\ucrt64\bin')
def compiler():
"""$CC, else MSYS2 UCRT64's clang, else clang on PATH."""
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 main():
work = ROOT / 'build' / 'test' / 'frame_owed'
work.mkdir(parents=True, exist_ok=True)
win = os.name == 'nt'
exe = work / ('frame_owed_check.exe' if win else 'frame_owed_check')
env = dict(os.environ)
if win and MSYS_BIN.exists():
env['PATH'] = str(MSYS_BIN) + os.pathsep + env.get('PATH', '')
rt = ROOT / 'runtime'
plat = [rt / 'platform' / ('win32.c' if win else 'linux.c'), rt / 'platform' / 'common.c']
libs = ['-lbcrypt', '-lws2_32', '-lpsapi', '-lwinmm', '-ldbghelp', '-lshell32', '-lole32', '-luser32', '-ladvapi32'] if win \
else ['-lpthread']
subprocess.check_call([compiler(), '-std=gnu11', '-O1', '-g', '-Wall', '-Wno-unused-function', '-fno-strict-aliasing',
'-march=x86-64-v3', '-I', str(rt), str(ROOT / 'tests' / 'frame_owed_check.c'),
*map(str, plat), '-o', str(exe), '-lm', *libs], env=env)
try:
return subprocess.run([str(exe)], env=env, timeout=300).returncode
except subprocess.TimeoutExpired:
print('frame_owed_check: FAIL: the check did not end within 300 s')
return 1
if __name__ == '__main__':
sys.exit(main())