From 1ab3856fdd94f6ee5fa8d534132ee6de50ad8771 Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Sat, 21 Nov 2026 09:19:54 +0300 Subject: [PATCH] TOTK_SYNC_AHEAD=check measures what a one-frame-late frame wait without shadow storage would race, with the wait left exact The replay notes each GPU-timeline access of guest memory with its submission's value (in-place bindings of imported pools without a mirror, mirror sync copies, GPU deswizzle sources, mirror write-backs); at the frame wait the notes of the frame the late wait would leave running form its read set, snapped with the write watch and checked again at the next frame-wait entry. Invalidates and small-target write-backs of GPU results from that frame count as stale readbacks. One summary line per second and a run summary with the largest offenders by pool (runtime/vk_ahead.c). --probe-vkmem checks the detector with a synthetic write after the wait; tests/sync_ahead_check.py checks that check mode keeps the wait exact. Co-Authored-By: Claude Opus 5.5 --- docs/architecture.md | 1 + runtime/cfg.c | 2 +- runtime/nvn_api_core.c | 35 ++- runtime/tex_probe.c | 4 + runtime/vk.h | 16 +- runtime/vk_ahead.c | 561 ++++++++++++++++++++++++++++++++++++++ runtime/vk_ahead.h | 30 ++ runtime/vk_backend.c | 1 + runtime/vk_exec_draw.c | 9 + runtime/vk_exec_xfer.c | 6 + runtime/vk_fg.cpp | 2 +- runtime/vk_memory.c | 67 ++++- runtime/vk_sync.c | 2 +- runtime/vk_texture.c | 3 + tests/sync_ahead_check.py | 40 ++- 15 files changed, 752 insertions(+), 27 deletions(-) create mode 100644 runtime/vk_ahead.c create mode 100644 runtime/vk_ahead.h diff --git a/docs/architecture.md b/docs/architecture.md index 70d5ddad..a7ffb229 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -91,6 +91,7 @@ Every guest thread is a host thread. TotK assumes Horizon's scheduler: three app - Replay thread. `TOTK_REPLAY_ASYNC=1` moves the replay off the submitting thread onto a queue at most one frame behind the game; reads of GPU results wait for the newest writer instead of draining the queue (`vk_backend.c`). It is off by default. Where the game waits for the GPU it overlaps the replay with the game threads (a GTX 1650 in the Depths, issue #88), a gain measured only there; while it was the default, a GTX 1660 Ti at the 60 fps cap with the GPU at 26 % reported uneven motion (#659). It adds up to a frame of latency; `replay_async = 1` turns it on. - Chunked replay. A large submission is recorded in chunks by several threads (`TOTK_REPLAY_WORKERS`, 0 by default: the sequential replay; 4 is a good value from 8 logical processors) and the chunks go to the queue in order (`vk_exec.c`). A sequential scan keeps a copy of the queue state where each chunk starts and lists what a chunk would otherwise learn from the chunks before it: inline uniform updates, conditions, contact shadow marches, uniform drops. It also makes render targets live in replay order and notes the op, since a chunk can sample a storage before the chunk that renders it got there or after the scan went past a later target there: a chunk's texture slots resolve as of its own op, and the chunks after a texture with no entry yet wait for the chunk that makes it. DLSS's decisions (the G-buffer, the light target, the end of the scene, the scaling pass) are the scan's too, and its copy of the lit scene and its evaluation are records of the chunk that holds the op; a chunk's draws are jittered while they come before the scan's end of the scene. The evaluation waits until the chunks before it are recorded, as it reads what their draws noted (the camera block, the jitter, the per-object motion list, which chunks add to under one lock), and the chunks after it wait for it, as they sample its output. `TOTK_REPLAY_STATE_CHECK=1` compares the state after the last chunk with the scan's. - Frame ahead. `TOTK_SYNC_AHEAD=1` (`sync_ahead`, off by default) makes the one `nvnSyncWait` per frame that waits for the frame presented last wait for the frame before it, so the game builds the next frame while the GPU still runs the previous one (`nvn_api_core.c`, `nvnSyncWait`). The game then writes memory that in-flight work still reads. This is safe only with shadow storage (`vk_memory.c`, the CPU-visible pools are host-visible copies of guest memory, on by default on Linux and Wine, `shadow = 1` (`TOTK_SHADOW=1`) on native Windows; without it the switch is ignored): a CPU write reaches the GPU only through the shadow copy made before each submit (`vk_mem_shadow_sync`), and a copy that would overwrite bytes an unfinished submission reads is queued as a GPU copy in the prologue command buffer of the part submitted next (`vk_mem_shadow_prologue`, `TOTK_SHADOW_DEFER=1`, the default with the feature) or, with `=0`, waited for on the replay thread (`shadow_reader_wait`). Only that one wait lags (identified by its call site, known for 1.1.0, 1.2.1 and 1.4.3). GPU writes into pool storage come back with their fence, so a buffer the game reads back every frame after that wait holds the result of the frame before; a buffer it has not read lately holds none, and its `nvnMemoryPoolInvalidateMappedRange` waits for the write (`vk_mem_shadow_read`): TotK reads single pixels through a buffer per request, and in the Depths zeros in one of them put Link in gloom. The exit line `mem: the game waited for a GPU write it reads` counts these waits and their time. On 1.4.3 the patch set `qstamp` (`recomp/patches-v143.txt`) lets the game take its depth query results one frame older; it follows the switch and is off when the switch is refused. The fence write-back of small render targets (`vk_texture.c`, `write_back_n`) does not wait for the GPU with `TOTK_WB_EAGER=1` (`wb_eager`, the default, but only while frame-ahead is in effect: with `sync_ahead` off or refused the setting does nothing and the write-back reads the image when it runs): `vk_sync_submit` has `vk_texture_eager_record` copy the targets that value rendered into staging buffers inside the same command buffer, so once the value has passed the write-back only copies from staging into the target's storage. Only a target that was written back within the last 64 timeline values is copied (the first write-back of a target takes the blocking path), and the staging of a target past that window, no longer wanted or released goes through the deferred free (`vk_defer_buffer_until`). A snapshot is used only when its value is the one that last rendered the target, nothing recorded a write to the image since (`wr_fence`) and the GPU has finished it; otherwise the write-back reads the image as before, and the exit line `vk: wb_eager:` counts the reasons. With frame-ahead in effect the exit line `vk: fence write-backs waited for the GPU` counts the write-backs that waited for a copy and the time they waited, for every value of the setting. `check` also reads the old way and compares. +- Frame-ahead check. `TOTK_SYNC_AHEAD=check` keeps the frame wait exact and measures what a one-frame-late wait without shadow storage would race (`runtime/vk_ahead.c`). The replay notes, with each submission's value, every access of guest memory the GPU makes after the recording: in-place bindings of imported pools without a VRAM mirror (`VK_AHEAD_READ`), the mirror's sync copies (`vk_mem_mirror_prologue`), GPU deswizzle sources (`vk_texture.c` `upload_gpu`) and the mirror write-backs (`vk_mem_mirror_end`). At the frame wait for value A, whose late form would have waited for L, the notes of values in (L, A] form the frame's read set: merged per class, gap-joined into at most 512 write-watch queries that stamp the writes made so far into the shared page generations (`vk_mem_ahead_observe`). At the next frame-wait entry the queries run again, and every page of the set stamped since is a CPU write the late wait would have raced; memory without a write watch is compared by page hashes. GPU writes into CPU-visible memory and reports are listed with their value: an invalidate of a range written by a value in (L, A] of the last frame wait, and the fence write-back of a small target rendered by one, count as stale readbacks. One line `sync-ahead check:` per second of host time and a run summary with the largest offenders by pool at exit. `--probe-vkmem` checks the detector on a watched pool (`vk_ahead_selftest`); `tests/sync_ahead_check.py` checks that the wait stays exact. - Recorder thread. The Vulkan calls the replay makes into its command buffer are made by a thread of their own (`vk_rec.c`, after Savage Wind Recomp and LAS Recomp): each `vkCmd*` copies its arguments into a packet of a ring and the recorder makes the calls in order; calls into other command buffers are made at once; the recorder also ends the command buffer, and the submit waits until it has. In the town on an RX 9070 the replay takes 17% less time on the submitting thread. It is off by default on AMD GPUs, where the driver loses the device or hangs the GPU at startup with it (`TOTK_VK_RECORDER=1` forces it on, `=0` makes every call on the submitting thread). - Uniform snapshot. The game rewrites per-object uniform blocks on other threads soon after it submits the frame that reads them, while the replay still records it. At the start of a submission's replay every uniform range it diff --git a/runtime/cfg.c b/runtime/cfg.c index 9349ac04..16abeaba 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -217,7 +217,7 @@ static const CfgMeta metas[] = { {"REPLAY_WORKERS", "0", "0..16", RESTART}, {"REPLAY_CHUNKS", "0", "0..64", RESTART}, {"REPLAY_CHUNK_DRAWS", "128", "1..100000", RESTART}, {"REPLAY_STATE_CHECK", "0", "0|1", RESTART}, {"REPLAY_SKEW_US", "0", "0..100000", RESTART}, - {"SYNC_AHEAD", "0", "0|1", RESTART}, {"SHADOW_DEFER", "1", "0|1", RESTART}, + {"SYNC_AHEAD", "0", "0|1|check", RESTART}, {"SHADOW_DEFER", "1", "0|1", RESTART}, {"FREE_CORES", "0", "0|1", RESTART}, {"TIMER_1MS", "1", "0|1", RESTART}, // shaders and pipelines {"PIPE_WAIT", "default", "default|draw", RESTART}, {"PIPE_SKIP", "fullscreen", "strict|any|fullscreen", RESTART}, diff --git a/runtime/nvn_api_core.c b/runtime/nvn_api_core.c index 96c65e2b..187bee54 100644 --- a/runtime/nvn_api_core.c +++ b/runtime/nvn_api_core.c @@ -15,6 +15,7 @@ #include "host.h" #include "nvn_api.h" #include "nvn_pool_index.h" +#include "vk_ahead.h" #include "platform/platform.h" #define DEV_MAGIC MAGIC('D', 'E', 'V', 0) @@ -494,10 +495,13 @@ static uint64_t frame_wait_lr(void) { if (rt_game_version == 110) return 0x7000b39aa4ull; return rt_game_version == 121 ? 0x7000b23dfcull : rt_game_version == 143 ? 0x70002db99cull : 0; } -static int sync_ahead(void) { - static int on = -1; - if (on < 0) { - on = cfg_bool("TOTK_SYNC_AHEAD"); +// TOTK_SYNC_AHEAD=check (vk_ahead.c): the wait stays exact; the presents and the wait note what the one-frame-late +// wait would have waited for, and the back end counts what that would have raced. sync_mode: 0 off, 1 on, 2 check. +static int sync_mode(void) { + static int mode = -1; + if (mode < 0) { + const char *v = cfg_value("TOTK_SYNC_AHEAD"); + int on = strcmp(v, "0") != 0 && strcmp(v, "check") != 0, check = !strcmp(v, "check"); // Without shadow storage the GPU reads the game's own memory, which the game writes ahead: nothing guards // that. The Vulkan back end sets the mode while it starts, before the game's first present or wait; the null // back end never does. @@ -506,20 +510,22 @@ static int sync_ahead(void) { "the game waits for the GPU as before"); on = 0; } - if (on && !frame_wait_lr()) { + if ((on || check) && !frame_wait_lr()) { rt_log("nvn: TOTK_SYNC_AHEAD: no frame wait site is known for this game version; the game waits for the " "GPU as before"); - on = 0; + on = check = 0; } // The depth query patch (set qstamp, patches-v143.txt) is switched by TOTK_SYNC_AHEAD too: a refused switch // leaves the game's own frame number check, as with the switch off. if (!on) rt_patch_switch(rt_patch_set("qstamp"), 0); if (on) rt_log("nvn: TOTK_SYNC_AHEAD: a wait for the frame presented last waits for the frame before it"); + mode = on ? 1 : check ? 2 : 0; } - return on; + return mode; } +static int sync_ahead(void) { return sync_mode() == 1; } void nvn_sync_ahead_present(void) { - if (!sync_ahead()) return; + if (!sync_mode()) return; __atomic_store_n(&pa_fence[1], __atomic_load_n(&pa_fence[0], __ATOMIC_RELAXED), __ATOMIC_RELEASE); __atomic_store_n(&pa_fence[0], nvn_fence_last(), __ATOMIC_RELEASE); } @@ -533,12 +539,15 @@ NVN_API(nvnSyncWait) { uint64_t timeout = arg_x(c, 1); if (!s->fenced) { ret_x(c, NVN_SYNC_WAIT_RESULT_TIMEOUT_EXPIRED); return; } const GfxBackend *b = gfx_backend(); - uint64_t want = s->fence; + uint64_t want = s->fence, lag = s->fence; // Only the game's per-frame wait lags, and not a poll (timeout 0). - if (timeout && c->x[30] == frame_wait_lr() && sync_ahead() && b->fence_done() < want) { + int mode = timeout && c->x[30] == frame_wait_lr() ? sync_mode() : 0; + if (mode == 2) vk_ahead_frame_entry(); + if (mode && b->fence_done() < want) { uint64_t p0 = __atomic_load_n(&pa_fence[0], __ATOMIC_ACQUIRE); uint64_t p1 = __atomic_load_n(&pa_fence[1], __ATOMIC_ACQUIRE); - if (want <= p0 && want > p1) want = p1; + if (want <= p0 && want > p1) lag = p1; + if (mode == 1) want = lag; } if (want != s->fence) { // the replay of the asked value is done first: the guard then covers every copy of it { @@ -551,7 +560,9 @@ NVN_API(nvnSyncWait) { vk_mem_shadow_fence(); // a shadow sync that read the old guard finishes before the game writes again } if (b->fence_done() >= want) { + if (mode == 2) vk_ahead_frame_lag(s->fence, lag); b->cpu_synced(want == s->fence ? s->fence : b->fence_done()); + if (mode == 2) vk_ahead_frame_done(1); ret_x(c, NVN_SYNC_WAIT_RESULT_ALREADY_SIGNALED); return; } @@ -560,7 +571,9 @@ NVN_API(nvnSyncWait) { NVN_BLOCKING(); ok = b->fence_wait(want, timeout); } + if (mode == 2) vk_ahead_frame_lag(s->fence, lag); if (ok) b->cpu_synced(want == s->fence ? s->fence : b->fence_done()); + if (mode == 2) vk_ahead_frame_done(ok); ret_x(c, ok ? NVN_SYNC_WAIT_RESULT_CONDITION_SATISFIED : NVN_SYNC_WAIT_RESULT_TIMEOUT_EXPIRED); } // void nvnEventBuilderSetDefaults(NVNeventBuilder *b) diff --git a/runtime/tex_probe.c b/runtime/tex_probe.c index 80eb91c5..06396205 100644 --- a/runtime/tex_probe.c +++ b/runtime/tex_probe.c @@ -372,6 +372,10 @@ static int test_import(pool_t *pool) { GCHECK(vk_mem_changed(RT_KERNEL_BASE, 0x1000, 0, &g3) == -1, "the kernel region is reported as tracked"); printf("probe-vkmem: write watch: CPU writes reported page-exact; generations see CPU and explicit writes\n"); g_gpu_checks += 7; + int ah = vk_ahead_selftest(a, pool->size); + GCHECK(!ah, "sync-ahead check self test failed at check %d", ah); + printf("probe-vkmem: sync-ahead check: a write after the frame wait into the read set is flagged, one before it is not\n"); + g_gpu_checks++; return 0; } diff --git a/runtime/vk.h b/runtime/vk.h index 55094c1d..9600b528 100644 --- a/runtime/vk.h +++ b/runtime/vk.h @@ -8,6 +8,7 @@ #pragma clang diagnostic pop #include "nvn_obj.h" // pool_t, tex_t, view_t, samp_par, NVNcopyRegion (memory and textures block) #include "gfx.h" // GfxShader, GfxShaderRes, GfxShaderAnswers (pipelines block) +#include "vk_ahead.h" // TOTK_SYNC_AHEAD=check hooks (vk_ahead_on) // Required (device choice fails without them): Vulkan 1.3; VK_KHR_swapchain, VK_KHR_push_descriptor, // VK_EXT_depth_clip_control, VK_EXT_external_memory_host; features timelineSemaphore, dynamicRendering, @@ -265,6 +266,19 @@ void vk_mem_use(VkMemSpan *sp); void vk_mem_pick(VkMemSpan *sp); #define VK_MEM_USE(sp) do { if (vk_mirror_on && (sp).pool) vk_mem_use(&(sp)); } while (0) #define VK_MEM_PICK(sp) do { if (vk_mirror_on && (sp).pool) vk_mem_pick(&(sp)); } while (0) +// TOTK_SYNC_AHEAD=check (vk_ahead.c): VK_AHEAD_READ after VK_MEM_USE / VK_MEM_PICK notes a binding or copy of +// [addr, addr+size) that the GPU reads (use: VK_MUSE_*; GPU_WRITE for a write) in place in guest memory: an imported +// pool without a mirror or shadow. One test of vk_ahead_on when the mode is off. +void vk_ahead_span(const VkMemSpan *sp, uint64_t addr, uint64_t size, int use); +#define VK_AHEAD_READ(sp, addr, size, use) \ + do { if (__builtin_expect(vk_ahead_on, 0)) vk_ahead_span(&(sp), (addr), (size), (use)); } while (0) +int vk_mem_span_inplace(const VkMemSpan *sp); // the span is guest memory itself (no mirror, no shadow, no device memory) +// The write watch over the ranges: every write made so far stamped into the page generations; untracked[i] 1 where +// range i has no write watch. Returns the newest generation. +uint32_t vk_mem_ahead_observe(const uint64_t (*iv)[2], int n, uint8_t *untracked); +// Bytes of [lo, hi) in pages stamped after generation since; *first: the first such byte. +uint64_t vk_mem_ahead_written(uint64_t lo, uint64_t hi, uint32_t since, uint64_t *first); +int vk_mem_pool_info(uint64_t addr, uint64_t *lo, uint64_t *size, uint32_t *flags, int *mirrored); // 0: no pool void vk_mem_gpu_wrote(const VkMemSpan *sp, uint64_t size); // A shader's storage-buffer write: goes back through the write-back merge (vk_memory.c), which keeps the words the GPU // did not change. @@ -303,7 +317,7 @@ void vk_mem_mirror_begin(void); int vk_mem_mirror_end(VkCommandBuffer cb, uint64_t fence); void vk_mem_mirror_seal(void); // before vk_mem_shadow_sync: the copies the next vk_mem_mirror_prologue takes int vk_mem_mirror_pending(void); -void vk_mem_mirror_prologue(VkCommandBuffer cb); +void vk_mem_mirror_prologue(VkCommandBuffer cb, uint64_t fence); // fence: the value of the part it precedes // Shadow storage (vk_memory.c): vk_sync.c calls vk_mem_shadow_sync right before each submit; the replay notes every GPU // write into CPU-visible memory at record time (vk_mem_gpu_pending) and reports it at the fence with vk_gpu_written, // which copies the bytes back into guest memory before telling the texture cache (vk_guest_written). diff --git a/runtime/vk_ahead.c b/runtime/vk_ahead.c new file mode 100644 index 00000000..d11e7a0b --- /dev/null +++ b/runtime/vk_ahead.c @@ -0,0 +1,561 @@ +// TOTK_SYNC_AHEAD=check: what answering the game's frame wait one frame late without shadow storage would race, with +// the wait left exact. In that form (Savage Wind's overlap) the game is told frame N is done when only N-1 is, and the +// GPU runs N while the game builds N+1, so every byte of guest memory N's GPU work reads after its recording (or writes +// back into guest memory) must not change between the wait for N and the next frame wait, which then waits for N; and +// every GPU result the game reads back after the wait for N must not come from N. +// - Read set: the replay notes each GPU-timeline access of guest memory with its submission's value (vk_ahead_read): +// in-place bindings of imported pools without a VRAM mirror, the mirror's sync copies (the pages and flush ranges +// copied in the prologue), GPU deswizzle sources and the mirror write-backs. Uniform snapshots and CPU texture uploads +// copy at record time, before the wait: not noted. +// - At the frame wait for value A whose one-frame-late form would have waited for L < A, the notes of values in (L, A] +// are N's read set: merged per class and over all classes, and gap-joined into at most IV_MAX write-watch queries. The +// queries stamp every write made so far into the shared page generations (vk_memory.c vk_mem_ahead_observe); G is the +// newest generation then. At the next frame-wait entry the same queries run again, and every page of the set stamped +// after G is a CPU write after the wait that the late form would have raced: counted in bytes by class, and the first +// offenders logged by pool. A range in memory without a write watch is hashed by page instead. +// - Readbacks: GPU writes into CPU-visible memory and reports are listed with their value (vk_ahead_gpu_write). An +// invalidate of a range a GPU write of a value in (L, A] of the last frame wait lands in would have read stale data; +// so would the fence write-back of a small target last rendered by such a value (vk_ahead_small). +// Bounded: fixed tables, a note list that is compacted when full, one frame set at a time. One log line per second of +// host time and a summary at exit, prefixed "sync-ahead check:". +#include +#include +#include + +#include "cfg.h" +#include "host.h" +#include "nvn_api.h" +#include "vk.h" +#include "vk_ahead.h" +#include "vk_exec.h" +#include "platform/platform.h" + +int vk_ahead_on; + +static const char *const cls_name[AH_N + 1] = {"vb", "ib", "ubo", "ssbo", "ind", "copy", "msync", "deswz", "wb", "wbm", + "all"}; + +#define NOTE_MAX 65536 // notes not yet taken by a frame wait +#define SET_MAX 8192 // merged ranges of one frame set, per class and the union together +#define IV_MAX 512 // write-watch queries per set (as Savage Wind's gsnap list) +#define IV_GAP 0x10000ull // ranges closer than this share a query +#define HASH_MAX 4096 // pages hashed per set (memory without a write watch) +#define GW_MAX 8192 // listed GPU writes and reports +#define RD_N 64 // invalidated ranges remembered (as vk_memory.c sread) +#define RD_AGE 64 // ... known when invalidated within this many values +#define SMALL_N 64 // small targets remembered (Savage Wind: one-off = not read back in 16 frames, 64 entries) +#define SMALL_FRAMES 16 +#define OFF_N 32 // offender table +#define OFF_LOG 24 // first-sighting lines + +typedef struct { uint64_t lo, hi, fence; uint8_t cls; } Note; +typedef struct { uint64_t lo, hi; uint8_t cls, untracked; uint32_t hash0, nhash; } SetR; +typedef struct { uint64_t lo, hi, fence; uint8_t kind; } GWrite; + +static PlatMutex ah_lock = PLAT_MUTEX_INIT; // notes, gw, rd, small, lag context +static Note *notes; +static int nnotes; +static uint64_t n_compact; +static GWrite gw[GW_MAX]; +static int ngw; +static uint64_t gw_over; +static uint64_t win_lo, win_hi; // (L, A] of the last frame wait; win_lo == win_hi: none +static int lag_live; // inside cpu_synced of a frame wait (vk_ahead_frame_lag .. done) +static uint64_t cur_asked, cur_lagged; +static uint64_t frame_no; +static struct { uint64_t addr, size, at; } rd[RD_N]; +static struct { uint64_t addr, frame; } small_t[SMALL_N]; + +// Game thread only (the frame wait): +static Note *take; +static SetR set[SET_MAX]; +static int nset; +static uint64_t iv[IV_MAX][2]; +static uint8_t iv_untr[IV_MAX]; +static int niv, armed; +static uint32_t set_gen; +static uint64_t hashes[HASH_MAX]; +static int nhash; + +typedef struct { + uint64_t frames, lagged, read[AH_N + 1], hit[AH_N + 1], hit_frames[AH_N + 1], hit_max[AH_N + 1]; + uint64_t inval, inval_stale, inval_stale_unknown, inval_stale_bytes, late[AHW_N], small, small_stale, small_oneoff; + uint64_t cost_ns, set_joined, set_lost, untracked, hashed, hash_skipped, ivq; +} Stat; +static Stat win, tot; // ah_lock for the readback counters, game thread for the rest (summed under ah_lock) + +typedef struct { uint64_t pool, size, bytes, frames, first; uint32_t flags; int cls, mirrored; } Offender; +static Offender off[OFF_N]; +static int noff, nofflog; +static uint64_t last_log_ns; + +void vk_ahead_init(void) { + vk_ahead_on = !strcmp(cfg_value("TOTK_SYNC_AHEAD"), "check"); + if (!vk_ahead_on) return; + notes = malloc(sizeof *notes * NOTE_MAX); + take = malloc(sizeof *take * NOTE_MAX); + if (!notes || !take) rt_fatal("vk: out of memory (sync-ahead check)"); + rt_log("sync-ahead check: the frame wait stays exact; logging what a one-frame-late wait without shadow storage " + "would race (%s)", vk_mem_shadow_on() ? "shadow storage is on: the counts describe the in-place form only " + "approximately" : "in place"); +} + +static uint64_t note_fence(uint64_t fence) { + if (fence) return fence; + uint64_t f = vk_exec_recording_fence(); + return f ? f : vk_sync_issued() + 1; +} + +static int note_cmp(const void *x, const void *y) { + const Note *a = x, *b = y; + if (a->cls != b->cls) return a->cls < b->cls ? -1 : 1; + return a->lo < b->lo ? -1 : a->lo > b->lo; +} + +// ah_lock held, list full: overlapping and touching notes of one class merge (the newest value kept). +static void compact(void) { + qsort(notes, (size_t)nnotes, sizeof *notes, note_cmp); + int o = 0; + for (int i = 1; i < nnotes; i++) { + Note *p = ¬es[o]; + if (notes[i].cls == p->cls && notes[i].lo <= p->hi) { + if (notes[i].hi > p->hi) p->hi = notes[i].hi; + if (notes[i].fence > p->fence) p->fence = notes[i].fence; + } else notes[++o] = notes[i]; + } + nnotes = o + 1; + n_compact++; + if (nnotes > NOTE_MAX * 3 / 4) { // still full: join each class's neighbours (coarser, never smaller) + o = 0; + for (int i = 1; i < nnotes; i++) { + Note *p = ¬es[o]; + if (notes[i].cls == p->cls && notes[i].lo - p->hi <= IV_GAP * 16) { + if (notes[i].hi > p->hi) p->hi = notes[i].hi; + if (notes[i].fence > p->fence) p->fence = notes[i].fence; + } else notes[++o] = notes[i]; + } + nnotes = o + 1; + } +} + +void vk_ahead_read(uint64_t addr, uint64_t size, int cls, uint64_t fence) { + if (!vk_ahead_on || !size || (unsigned)cls >= AH_N || addr + size < addr) return; + fence = note_fence(fence); + plat_mutex_lock(&ah_lock); + Note *l = nnotes ? ¬es[nnotes - 1] : NULL; + if (l && l->cls == cls && l->fence == fence && addr <= l->hi && addr + size >= l->lo) { + if (addr < l->lo) l->lo = addr; + if (addr + size > l->hi) l->hi = addr + size; + } else { + if (nnotes == NOTE_MAX) compact(); + notes[nnotes++] = (Note){addr, addr + size, fence, (uint8_t)cls}; + } + plat_mutex_unlock(&ah_lock); +} + +void vk_ahead_span(const VkMemSpan *sp, uint64_t addr, uint64_t size, int use) { + if (!vk_mem_span_inplace(sp)) return; + int cls = use == VK_MUSE_VB ? AH_VB : use == VK_MUSE_IB ? AH_IB : use == VK_MUSE_UBO ? AH_UBO + : use == VK_MUSE_IND ? AH_IND : use == VK_MUSE_COPY_SRC ? AH_COPY : use == VK_MUSE_GPU_WRITE ? AH_WB : AH_SSBO; + vk_ahead_read(addr, size, cls, 0); +} + +void vk_ahead_gpu_write(uint64_t addr, uint64_t size, int kind, uint64_t fence) { + if (!vk_ahead_on || !size) return; + fence = note_fence(fence); + plat_mutex_lock(&ah_lock); + int i = ngw; + while (i > 0 && i > ngw - 8 && !(gw[i - 1].lo == addr && gw[i - 1].hi == addr + size && gw[i - 1].kind == kind)) i--; + if (i > 0 && i > ngw - 8) { + if (fence > gw[i - 1].fence) gw[i - 1].fence = fence; + } else { + if (ngw == GW_MAX) { // the oldest goes + memmove(gw, gw + 1, sizeof *gw * (GW_MAX - 1)); + ngw--, gw_over++; + } + gw[ngw++] = (GWrite){addr, addr + size, fence, (uint8_t)kind}; + } + plat_mutex_unlock(&ah_lock); +} + +void vk_ahead_invalidate(uint64_t addr, uint64_t size) { + if (!vk_ahead_on || !size) return; + plat_mutex_lock(&ah_lock); + uint64_t now = vk_sync_issued(), stale = 0; + for (int i = 0; i < ngw; i++) + if (gw[i].fence > win_lo && gw[i].fence <= win_hi && gw[i].lo < addr + size && gw[i].hi > addr) { + uint64_t a = gw[i].lo > addr ? gw[i].lo : addr, b = gw[i].hi < addr + size ? gw[i].hi : addr + size; + stale += b - a; + } + int e = 0; // the range's entry, else the oldest + for (int i = 0; i < RD_N; i++) { + if (rd[i].addr == addr && rd[i].size == size) { e = i; break; } + if (rd[i].at < rd[e].at) e = i; + } + int known = rd[e].addr == addr && rd[e].size == size && rd[e].at + RD_AGE >= now; + rd[e].addr = addr, rd[e].size = size, rd[e].at = now; + win.inval++; + if (stale) win.inval_stale++, win.inval_stale_bytes += stale, win.inval_stale_unknown += !known; + plat_mutex_unlock(&ah_lock); +} + +void vk_ahead_small(uint64_t addr, uint64_t gpu_fence) { + if (!vk_ahead_on) return; + plat_mutex_lock(&ah_lock); + if (lag_live) { + int e = 0; + for (int i = 0; i < SMALL_N; i++) { + if (small_t[i].addr == addr) { e = i; break; } + if (small_t[i].frame < small_t[e].frame) e = i; + } + int oneoff = small_t[e].addr != addr || small_t[e].frame + SMALL_FRAMES < frame_no; + small_t[e].addr = addr, small_t[e].frame = frame_no; + win.small++; + if (gpu_fence > cur_lagged) win.small_stale++, win.small_oneoff += oneoff; + } + plat_mutex_unlock(&ah_lock); +} + +// ---- the frame set + +static int lo_cmp(const void *x, const void *y) { + const SetR *a = x, *b = y; + return a->lo < b->lo ? -1 : a->lo > b->lo; +} + +static uint64_t hash_bytes(uint64_t lo, uint64_t hi) { + uint64_t h = 0x9E3779B97F4A7C15ull ^ (hi - lo); + const uint8_t *p = (const uint8_t *)(uintptr_t)lo; + uint64_t n = hi - lo, i = 0; + for (; i + 8 <= n; i += 8) { + uint64_t w; + memcpy(&w, p + i, 8); + h = (h ^ w) * 0x100000001B3ull, h ^= h >> 29; + } + for (; i < n; i++) h = (h ^ p[i]) * 0x100000001B3ull; + return h; +} + +// Readable now (committed, not freed by the game since): hashing a page that is gone would fault. +static int readable(uint64_t lo, uint64_t hi) { + for (uint64_t a = lo, b; a < hi; a = b) { + int st = plat_mem_state(a, hi, &b); + if (st != PLAT_PROT_READ && st != PLAT_PROT_RW) return 0; + if (b <= a) return 0; + } + return 1; +} + +// The pages of set[i] (in memory without a write watch) hashed into hashes[], or compared with them (check). +static uint64_t hash_pass(SetR *s, int check) { + uint64_t changed = 0; + if (!check) s->hash0 = (uint32_t)nhash, s->nhash = 0; + uint32_t k = s->hash0; + for (uint64_t a = s->lo; a < s->hi;) { + uint64_t b = (a & ~0xfffull) + 0x1000; + if (b > s->hi) b = s->hi; + if (!check) { // 0: the page is not compared (the table is full or the page not readable) + if (nhash == HASH_MAX) { win.hash_skipped += s->hi - a; break; } + int ok = readable(a, b); + hashes[nhash++] = ok ? hash_bytes(a, b) | 1 : 0; + s->nhash++; + if (ok) win.hashed += b - a; + else win.hash_skipped += b - a; + } else { + if (k >= s->hash0 + s->nhash) break; + if (hashes[k] && readable(a, b) && hashes[k] != (hash_bytes(a, b) | 1)) changed += b - a; + k++; + } + a = b; + } + return changed; +} + +static void offender(int cls, uint64_t addr, uint64_t bytes) { + uint64_t lo = 0, size = 0; + uint32_t flags = 0; + int mirrored = 0; + vk_mem_pool_info(addr, &lo, &size, &flags, &mirrored); + int i = 0; + while (i < noff && !(off[i].cls == cls && off[i].pool == lo)) i++; + if (i == noff) { + if (noff == OFF_N) { // the smallest goes + int m = 0; + for (int k = 1; k < noff; k++) if (off[k].bytes < off[m].bytes) m = k; + i = m; + } else noff++; + off[i] = (Offender){lo, size, 0, 0, addr - lo, flags, cls, mirrored}; + if (nofflog++ < OFF_LOG) + rt_log("sync-ahead check: frame %llu: %s range written after the wait: pool %#llx (%.1f MiB, flags %#x, %s) " + "+%#llx, %llu bytes", (unsigned long long)frame_no, cls_name[cls], (unsigned long long)lo, + size / 1048576.0, flags, mirrored ? "mirrored" : "in place", (unsigned long long)(addr - lo), + (unsigned long long)bytes); + } + off[i].bytes += bytes, off[i].frames++; +} + +// The armed set against the writes since its snap. +static void check_set(void) { + vk_mem_ahead_observe((const uint64_t(*)[2])iv, niv, iv_untr); + win.ivq += (uint64_t)niv; + uint64_t hit[AH_N + 1] = {0}; + for (int i = 0; i < nset; i++) { + SetR *s = &set[i]; + uint64_t first = s->lo, b = s->untracked ? hash_pass(s, 1) : vk_mem_ahead_written(s->lo, s->hi, set_gen, &first); + if (!b) continue; + hit[s->cls] += b; + if (s->cls != AH_N) offender(s->cls, first, b); + } + for (int c = 0; c <= AH_N; c++) { + win.hit[c] += hit[c]; + if (hit[c]) win.hit_frames[c]++; + if (hit[c] > win.hit_max[c]) win.hit_max[c] = hit[c]; + } + armed = 0; +} + +// take[0..n): notes of values in (L, A]. Merged per class, then over all classes (class AH_N), then the queries. +static void build_set(int n) { + qsort(take, (size_t)n, sizeof *take, note_cmp); + nset = 0; + for (int i = 0; i < n; i++) { + SetR *p = nset ? &set[nset - 1] : NULL; + if (p && p->cls == take[i].cls && take[i].lo <= p->hi) { + if (take[i].hi > p->hi) p->hi = take[i].hi; + continue; + } + if (nset == SET_MAX / 2) { // full: the rest joins the last range of its class, or is left out + if (p && p->cls == take[i].cls) { + win.set_joined += take[i].lo - p->hi; + if (take[i].hi > p->hi) p->hi = take[i].hi; + } else win.set_lost += take[i].hi - take[i].lo; + continue; + } + set[nset++] = (SetR){take[i].lo, take[i].hi, take[i].cls, 0, 0, 0}; + } + int nc = nset; + for (int i = 0; i < nc; i++) win.read[set[i].cls] += set[i].hi - set[i].lo; + // The union over all classes, after them. + SetR *u = set + nc; + memcpy(u, set, sizeof *set * (size_t)nc); + qsort(u, (size_t)nc, sizeof *u, lo_cmp); + int nu = 0; + for (int i = 0; i < nc; i++) { + if (nu && u[i].lo <= u[nu - 1].hi) { + if (u[i].hi > u[nu - 1].hi) u[nu - 1].hi = u[i].hi; + } else u[nu++] = (SetR){u[i].lo, u[i].hi, AH_N, 0, 0, 0}; + } + nset = nc + nu; + for (int i = 0; i < nu; i++) win.read[AH_N] += u[i].hi - u[i].lo; + // Queries: the union gap-joined; a gap that does not fit doubles until it does. + uint64_t gap = IV_GAP; + for (;;) { + niv = 0; + for (int i = 0; i < nu && niv <= IV_MAX; i++) { + if (niv && u[i].lo - iv[niv - 1][1] <= gap) { + if (u[i].hi > iv[niv - 1][1]) iv[niv - 1][1] = u[i].hi; + } else if (niv < IV_MAX) iv[niv][0] = u[i].lo, iv[niv][1] = u[i].hi, niv++; + else { niv = IV_MAX + 1; break; } + } + if (niv <= IV_MAX) break; + gap *= 2; + } +} + +// Whether [lo, hi) lies in a query the write watch does not cover. +static int untracked_at(uint64_t lo) { + int a = 0, b = niv; + while (a < b) { + int m = (a + b) / 2; + if (iv[m][1] <= lo) a = m + 1; + else b = m; + } + return a < niv && iv[a][0] <= lo && iv_untr[a]; +} + +static void snap_set(void) { + set_gen = vk_mem_ahead_observe((const uint64_t(*)[2])iv, niv, iv_untr); + win.ivq += (uint64_t)niv; + nhash = 0; + for (int i = 0; i < nset; i++) { + SetR *s = &set[i]; + s->untracked = (uint8_t)untracked_at(s->lo); + if (!s->untracked) continue; + win.untracked += s->cls == AH_N ? s->hi - s->lo : 0; + hash_pass(s, 0); + } + armed = 1; +} + +static void log_line(const Stat *s, const char *what) { + char b[2048]; // about 1,200 bytes with every class + int k = 0; + double f = s->frames ? (double)s->frames : 1; + k += snprintf(b + k, sizeof b - (size_t)k, "sync-ahead check: %s %llu frames (%llu lagged): written after the wait " + "KiB/frame", what, (unsigned long long)s->frames, (unsigned long long)s->lagged); + for (int c = 0; c <= AH_N; c++) + if (s->hit[c] || c == AH_N) + k += snprintf(b + k, sizeof b - (size_t)k, " %s %.1f (in %llu frames, max %.0f)", cls_name[c], + s->hit[c] / 1024.0 / f, (unsigned long long)s->hit_frames[c], s->hit_max[c] / 1024.0); + k += snprintf(b + k, sizeof b - (size_t)k, "; read KiB/frame"); + for (int c = 0; c <= AH_N; c++) + if (s->read[c] || c == AH_N) k += snprintf(b + k, sizeof b - (size_t)k, " %s %.1f", cls_name[c], s->read[c] / 1024.0 / f); + k += snprintf(b + k, sizeof b - (size_t)k, + "; stale: invalidates %llu of %llu (%llu not read lately, %.1f KiB), late reports counter %llu samples " + "%llu event %llu, writes %llu, small write-backs %llu of %llu (%llu one-off)", + (unsigned long long)s->inval_stale, (unsigned long long)s->inval, (unsigned long long)s->inval_stale_unknown, + s->inval_stale_bytes / 1024.0, (unsigned long long)s->late[AHW_COUNTER], (unsigned long long)s->late[AHW_SAMPLES], + (unsigned long long)s->late[AHW_EVENT], (unsigned long long)s->late[AHW_WRITE], + (unsigned long long)s->small_stale, (unsigned long long)s->small, (unsigned long long)s->small_oneoff); + snprintf(b + k, sizeof b - (size_t)k, "; game-thread cost %.3f ms/frame, %.1f queries/frame, untracked %.1f " + "KiB/frame (hashed %.1f, skipped %.1f), set over its size: %.1f KiB joined, %.1f KiB left out; note compactions %llu, gpu-write list " + "overflow %llu", s->cost_ns / 1e6 / f, s->ivq / f, s->untracked / 1024.0 / f, s->hashed / 1024.0 / f, + s->hash_skipped / 1024.0 / f, s->set_joined / 1024.0, s->set_lost / 1024.0, (unsigned long long)n_compact, + (unsigned long long)gw_over); + rt_log("%s", b); +} + +static void stat_add(Stat *d, const Stat *s) { + const uint64_t *x = (const uint64_t *)s; + uint64_t *y = (uint64_t *)d; + for (size_t i = 0; i < sizeof *s / sizeof(uint64_t); i++) y[i] += x[i]; + for (int c = 0; c <= AH_N; c++) { // maxima, not sums + d->hit_max[c] -= s->hit_max[c]; + if (s->hit_max[c] > d->hit_max[c]) d->hit_max[c] = s->hit_max[c]; + } +} + +void vk_ahead_frame_entry(void) { + if (!vk_ahead_on || !armed) return; + uint64_t t0 = plat_now_ns(); + check_set(); + win.cost_ns += plat_now_ns() - t0; +} + +void vk_ahead_frame_lag(uint64_t asked, uint64_t lagged) { + if (!vk_ahead_on) return; + plat_mutex_lock(&ah_lock); + frame_no++; + cur_asked = asked, cur_lagged = lagged, lag_live = 1; + plat_mutex_unlock(&ah_lock); +} + +void vk_ahead_frame_done(int ok) { + if (!vk_ahead_on) return; + uint64_t t0 = plat_now_ns(); + plat_mutex_lock(&ah_lock); + lag_live = 0; + uint64_t a = cur_asked, l = cur_lagged; + int n = 0, k = 0; + if (ok) { + for (int i = 0; i < nnotes; i++) { + if (notes[i].fence > a) notes[k++] = notes[i]; + else if (notes[i].fence > l) take[n++] = notes[i]; + } + nnotes = k; + k = 0; + for (int i = 0; i < ngw; i++) { // writes done even with the lag go; the late ones are counted + if (gw[i].fence > l && gw[i].fence <= a) win.late[gw[i].kind]++; + if (gw[i].fence > l) gw[k++] = gw[i]; + } + ngw = k; + win_lo = l, win_hi = a; + } + plat_mutex_unlock(&ah_lock); + if (ok) { + win.frames++; + if (l < a) win.lagged++; + build_set(n); + snap_set(); + } + uint64_t now = plat_now_ns(); + win.cost_ns += now - t0; + if (now - last_log_ns >= 1000000000ull && win.frames) { + plat_mutex_lock(&ah_lock); + Stat w = win; + memset(&win, 0, sizeof win); + plat_mutex_unlock(&ah_lock); + if (last_log_ns) log_line(&w, "1 s:"); + stat_add(&tot, &w); + last_log_ns = now; + } +} + +static int off_cmp(const void *x, const void *y) { + const Offender *a = x, *b = y; + return a->bytes < b->bytes ? 1 : a->bytes > b->bytes ? -1 : 0; +} + +void vk_ahead_log_final(void) { + static int32_t done; + if (!vk_ahead_on || plat_xchg32(&done, 1)) return; + plat_mutex_lock(&ah_lock); + stat_add(&tot, &win); + memset(&win, 0, sizeof win); + plat_mutex_unlock(&ah_lock); + log_line(&tot, "run:"); + qsort(off, (size_t)noff, sizeof *off, off_cmp); + for (int i = 0; i < noff && i < 12; i++) + rt_log("sync-ahead check: offender %d: %s pool %#llx (%.1f MiB, flags %#x, %s) first at +%#llx: %.1f KiB in %llu " + "frames", i + 1, cls_name[off[i].cls], (unsigned long long)off[i].pool, off[i].size / 1048576.0, off[i].flags, + off[i].mirrored ? "mirrored" : "in place", (unsigned long long)off[i].first, off[i].bytes / 1024.0, + (unsigned long long)off[i].frames); +} + +// --probe-vkmem (tex_probe.c test_import): [a, a+size) is write-watched guest memory of an imported pool, at least +// 0x40000 bytes, that nothing else touches meanwhile. A frame's read set gets a CPU write before its wait (not counted) +// and one after it (counted in its class and the union, not in another class); a frame the late wait would not have +// lagged keeps no set; an invalidate of a GPU write of the lagged frame counts as stale. 0: passed, else the failed check. +int vk_ahead_selftest(uint64_t a, uint64_t size) { + if (size < 0x40000) return 1; + int was = vk_ahead_on; + Note *n0 = notes, *t0 = take; + Note nbuf[64], tbuf[64]; + if (!notes) notes = nbuf, take = tbuf; // the checks add far fewer than 64 notes + vk_ahead_on = 1; + plat_mutex_lock(&ah_lock); + nnotes = ngw = 0; + plat_mutex_unlock(&ah_lock); + armed = 0; + Stat saved = win; + uint64_t log_was = last_log_ns; + last_log_ns = plat_now_ns(); // no summary line (it would reset the counters) while the checks run + memset(&win, 0, sizeof win); + volatile uint8_t *m = (volatile uint8_t *)(uintptr_t)a; + uint64_t f = vk_sync_issued() + 1000; // values no real submission has + int bad = 0; + vk_ahead_read(a + 0x10000, 0x8000, AH_MSYNC, f); + vk_ahead_read(a + 0x20000, 0x1000, AH_VB, f); + vk_ahead_read(a + 0x28000, 0x1000, AH_VB, f + 1); // a later frame's: stays in the list + vk_ahead_gpu_write(a + 0x30000, 16, AHW_COUNTER, f); + m[0x10010] = (uint8_t)(m[0x10010] + 1); // before the wait: the frame wrote it before the GPU read it + vk_ahead_frame_lag(f, f - 1); + vk_ahead_frame_done(1); + if (!armed || win.read[AH_MSYNC] != 0x8000 || win.read[AH_VB] != 0x1000 || win.read[AH_N] != 0x9000) bad = 2; + else if (win.late[AHW_COUNTER] != 1) bad = 3; + vk_ahead_invalidate(a + 0x30000, 0x100); + if (!bad && (win.inval != 1 || win.inval_stale != 1)) bad = 4; + m[0x12345] = (uint8_t)(m[0x12345] + 1); // after the wait, in the msync range: the late wait would race it + m[0x3a000] = (uint8_t)(m[0x3a000] + 1); // after the wait, outside the set + vk_ahead_frame_entry(); + if (!bad && (armed || win.hit[AH_MSYNC] != 0x1000 || win.hit[AH_VB] || win.hit[AH_N] != 0x1000)) bad = 5; + plat_mutex_lock(&ah_lock); + int left = nnotes; + plat_mutex_unlock(&ah_lock); + if (!bad && left != 1) bad = 6; + vk_ahead_frame_lag(f + 1, f + 1); // not lagged: nothing at risk + vk_ahead_frame_done(1); + if (!bad && win.read[AH_N] != 0x9000) bad = 7; + m[0x28010] = (uint8_t)(m[0x28010] + 1); + vk_ahead_frame_entry(); + if (!bad && win.hit[AH_N] != 0x1000) bad = 8; + plat_mutex_lock(&ah_lock); + nnotes = ngw = 0, win_lo = win_hi = 0; + plat_mutex_unlock(&ah_lock); + armed = 0, noff = 0, nofflog = 0; + win = saved; + last_log_ns = log_was; + notes = n0, take = t0; + vk_ahead_on = was; + return bad; +} diff --git a/runtime/vk_ahead.h b/runtime/vk_ahead.h new file mode 100644 index 00000000..48149cb0 --- /dev/null +++ b/runtime/vk_ahead.h @@ -0,0 +1,30 @@ +// TOTK_SYNC_AHEAD=check (vk_ahead.c): measures what answering the frame wait one frame late without shadow storage +// would race, while the wait stays exact. Off: vk_ahead_on is 0 and every hook is one test of it. +#pragma once +#include + +// Classes of GPU-timeline accesses of guest memory a submission makes after its recording: in-place bindings of an +// imported pool without a mirror (vertex, index, uniform, storage, indirect, copy source), the VRAM mirror's sync copies, +// the GPU deswizzle of a texture's storage, and the mirror write-backs (plain copies, word merges). +enum { AH_VB, AH_IB, AH_UBO, AH_SSBO, AH_IND, AH_COPY, AH_MSYNC, AH_DESWZ, AH_WB, AH_WBM, AH_N }; +// Kinds of GPU results the CPU may read back: buffer writes, counter reports, samples-passed reports, event signals. +enum { AHW_WRITE, AHW_COUNTER, AHW_SAMPLES, AHW_EVENT, AHW_N }; + +extern int vk_ahead_on; +void vk_ahead_init(void); // vk_memory.c mirror_mode, once at device start: reads TOTK_SYNC_AHEAD +// [addr, addr+size) of guest memory is read (or, for WB and WBM, written) on the GPU timeline by the submission of value +// fence (0: the one being recorded, else the next one). +void vk_ahead_read(uint64_t addr, uint64_t size, int cls, uint64_t fence); +// The GPU writes [addr, addr+size) of CPU-visible memory, or a report lands there, at the completion of value fence +// (0: as above). +void vk_ahead_gpu_write(uint64_t addr, uint64_t size, int kind, uint64_t fence); +void vk_ahead_invalidate(uint64_t addr, uint64_t size); // nvnMemoryPoolInvalidateMappedRange +void vk_ahead_small(uint64_t addr, uint64_t gpu_fence); // the fence write-back of a small target (vk_texture.c) +// The game's per-frame wait (nvn_api_core.c nvnSyncWait at frame_wait_lr): entry checks the previous frame's read set; +// lag before cpu_synced gives the value a one-frame-late wait would have waited for (lagged == asked: no lag); done after +// it records this frame's read set. ok 0: the wait timed out. +void vk_ahead_frame_entry(void); +void vk_ahead_frame_lag(uint64_t asked, uint64_t lagged); +void vk_ahead_frame_done(int ok); +void vk_ahead_log_final(void); // at exit +int vk_ahead_selftest(uint64_t addr, uint64_t size); // --probe-vkmem: 0 passed diff --git a/runtime/vk_backend.c b/runtime/vk_backend.c index 64b61ed2..bde23fbe 100644 --- a/runtime/vk_backend.c +++ b/runtime/vk_backend.c @@ -478,6 +478,7 @@ void gfx_exit_log(void) { if (!plat_xchg32(&fs_done, 1)) fs_log(&fs_run, "over the run"); vk_texture_eager_log(); vk_mem_shadow_read_log(); + vk_ahead_log_final(); if (gfx_replay_async() && r_started && !plat_xchg32(&done, 1)) replay_log(game_frames, 1); } diff --git a/runtime/vk_exec_draw.c b/runtime/vk_exec_draw.c index 305d67a3..e7f27738 100644 --- a/runtime/vk_exec_draw.c +++ b/runtime/vk_exec_draw.c @@ -1277,6 +1277,7 @@ static int bind_range(VkExec *ex, uint64_t addr, uint64_t size, uint64_t align, } if (sp.offset % align == 0) { VK_MEM_USE(sp); + VK_AHEAD_READ(sp, addr, n, use); if (written) ub_written(ex, addr, n); *out = (VkDescriptorBufferInfo){sp.buffer, sp.offset, n}; if (written) @@ -2966,6 +2967,7 @@ static void bind_vertex_buffers(VkExec *ex) { if (r->address && vk_mem_resolve(r->address, 1, &sp)) { VK_MEM_USE(sp); nb = sp.buffer, no = sp.offset, nsz = r->size && r->size < sp.avail ? r->size : sp.avail; + VK_AHEAD_READ(sp, r->address, nsz, VK_MUSE_VB); ncls = sp.cls, npool = sp.pool; } D->B.vin[b] = *r, D->B.vres |= 1u << b; @@ -3788,6 +3790,7 @@ static uint32_t mo_vertex_buffers(VkExec *ex) { if (!r->address || !vk_mem_resolve(r->address, 1, &sp)) continue; VK_MEM_USE(sp); v->b[b] = sp.buffer, v->o[b] = sp.offset, v->sz[b] = r->size && r->size < sp.avail ? r->size : sp.avail; + VK_AHEAD_READ(sp, r->address, v->sz[b], VK_MUSE_VB); } memcpy(MQ.vin, ex->vbuf, sizeof MQ.vin); MQ.vgen = gen; @@ -4422,6 +4425,7 @@ static void op_draw_idx(VkExec *ex, const p_draw_idx *d) { return warn_once(W_INDEX, "index buffer %#llx is not aligned to its index size: draw skipped", (unsigned long long)d->ibuf); } else { VK_MEM_USE(sp); + VK_AHEAD_READ(sp, d->ibuf, bytes, VK_MUSE_IB); vkCmdBindIndexBuffer(ex->cb, sp.buffer, sp.offset, d->itype == NVN_INDEX_TYPE_UNSIGNED_SHORT ? VK_INDEX_TYPE_UINT16 : VK_INDEX_TYPE_UINT32); ex->recorded++; vk_mem_count(sp.cls, VK_MUSE_IB, bytes); @@ -4450,6 +4454,8 @@ static void op_draw_idx_ind(VkExec *ex, const p_draw_idx_ind *d) { ex->fr.draws++; VK_MEM_USE(ip); VK_MEM_USE(ib); + VK_AHEAD_READ(ip, d->ind, sizeof(VkDrawIndexedIndirectCommand), VK_MUSE_IND); + VK_AHEAD_READ(ib, d->ibuf, ib.avail < 0x10000 ? ib.avail : 0x10000, VK_MUSE_IB); // the count is the GPU's: 64 KiB vkCmdBindIndexBuffer(ex->cb, ib.buffer, ib.offset, d->itype == NVN_INDEX_TYPE_UNSIGNED_SHORT ? VK_INDEX_TYPE_UINT16 : VK_INDEX_TYPE_UINT32); vkCmdDrawIndexedIndirect(ex->cb, ip.buffer, ip.offset, 1, sizeof(VkDrawIndexedIndirectCommand)); ex->recorded += 2; @@ -4504,6 +4510,7 @@ static void op_dispatch_ind(VkExec *ex, const p_dispatch_ind *d) { if (!p || !info) return warn_once(W_PIPELINE, "a compute pipeline could not be created: dispatches with it are skipped"); vk_exec_barrier(ex); VK_MEM_USE(ip); + VK_AHEAD_READ(ip, d->ind, sizeof(VkDispatchIndirectCommand), VK_MUSE_IND); bind_pipeline(ex, VK_PIPELINE_BIND_POINT_COMPUTE, p); D->c0_dim[0] = D->c0_dim[1] = D->c0_dim[2] = 0; D->B.c0_valid[2] = 0; // a fresh c0 range: the copy below must not change one an earlier dispatch reads @@ -4759,6 +4766,7 @@ static void op_update_ub(VkExec *ex, const p_update_ub *u) { uint8_t *d = scratch(ex, u->size, &b, &o); MX_CPY(d, u->data, u->size); VK_MEM_PICK(sp); + VK_AHEAD_READ(sp, u->dst, u->size, VK_MUSE_GPU_WRITE); ov_add(u->dst, u->dst + u->size, b, o, &sp); vk_mem_gpu_wrote(&sp, u->size); vk_usnap_drop(u->dst, u->size); @@ -4776,6 +4784,7 @@ static void op_update_ub(VkExec *ex, const p_update_ub *u) { vk_exec_barrier(ex); // earlier draws finished with the old contents; overlaid updates' copies go first MX_CPY(scratch(ex, u->size, &b, &o), u->data, u->size); VK_MEM_PICK(sp); + VK_AHEAD_READ(sp, u->dst, u->size, VK_MUSE_GPU_WRITE); VkBufferCopy cp = {o, sp.offset, u->size}; vkCmdCopyBuffer(ex->cb, b, sp.buffer, 1, &cp); vk_mem_gpu_wrote(&sp, u->size); diff --git a/runtime/vk_exec_xfer.c b/runtime/vk_exec_xfer.c index d3b35b19..bb8723ac 100644 --- a/runtime/vk_exec_xfer.c +++ b/runtime/vk_exec_xfer.c @@ -277,6 +277,7 @@ static void signal_event(VkExec *ex, const p_signal_event *p) { EventWrite *e = malloc(sizeof *e); if (!e) rt_fatal("vk: out of memory"); *e = (EventWrite){p->val, p->mode, p->value}; + if (vk_ahead_on) vk_ahead_gpu_write((uintptr_t)p->val, sizeof *p->val, AHW_EVENT, ex->fence); vk_completion_add(ex->fence, event_cb, e); } @@ -290,6 +291,7 @@ static void report_counter(VkExec *ex, const p_report_counter *p) { if (!vk_mem_resolve(p->addr, sizeof(NVNcounterData), &m)) rt_fatal("vk: counter report at %#llx is in no memory pool", (unsigned long long)p->addr); vk_mem_count(m.cls, VK_MUSE_GPU_WRITE, sizeof(NVNcounterData)); + if (vk_ahead_on) vk_ahead_gpu_write(p->addr, sizeof(NVNcounterData), samples ? AHW_SAMPLES : AHW_COUNTER, ex->fence); if (samples) oq_report(ex, p->addr); else { uint64_t *a = malloc(sizeof *a); @@ -432,6 +434,7 @@ static void copy_buffer_image(VkExec *ex, const p_copy_b2t *p, int to_image) { VkBufferImageCopy c; VkMemSpan m; if (!buffer_side(ex, &s, p->src, &size, &c, &m)) return; + VK_AHEAD_READ(m, p->src, size, to_image ? VK_MUSE_COPY_SRC : VK_MUSE_GPU_WRITE); vk_mem_count(m.cls, to_image ? VK_MUSE_COPY_SRC : VK_MUSE_GPU_WRITE, size); vk_exec_pass_end(ex); vk_exec_barrier(ex); @@ -497,6 +500,8 @@ static void copy_b2b(VkExec *ex, const p_copy_b2b *p) { if (n > d.avail) n = d.avail; VK_MEM_PICK(s); VK_MEM_PICK(d); + VK_AHEAD_READ(s, src, n, VK_MUSE_COPY_SRC); + VK_AHEAD_READ(d, dst, n, VK_MUSE_GPU_WRITE); if (s.buffer == d.buffer && s.offset < d.offset + n && d.offset < s.offset + n) { warn_once(W_OVERLAP, "buffer copy %#llx -> %#llx overlaps within one buffer: skipped", (unsigned long long)p->src, (unsigned long long)p->dst); @@ -537,6 +542,7 @@ static void clear_buf(VkExec *ex, const p_clear_buf *p) { if (!n) break; if (!started) vk_exec_pass_end(ex), vk_exec_barrier(ex), started = 1; VK_MEM_PICK(d); + VK_AHEAD_READ(d, dst, n, VK_MUSE_GPU_WRITE); vkCmdFillBuffer(ex->cb, d.buffer, d.offset, n, p->value); vk_mem_gpu_wrote(&d, n); ex->recorded++; diff --git a/runtime/vk_fg.cpp b/runtime/vk_fg.cpp index 7ac91fca..c74216e0 100644 --- a/runtime/vk_fg.cpp +++ b/runtime/vk_fg.cpp @@ -295,7 +295,7 @@ static void start(void) { } g.win_fmt = VK_FORMAT_R8G8B8A8_UNORM; g.workers = cfg_int("TOTK_REPLAY_WORKERS"); - g.sync_ahead = cfg_bool("TOTK_SYNC_AHEAD"); + g.sync_ahead = cfg_bool("TOTK_SYNC_AHEAD") && strcmp(cfg_value("TOTK_SYNC_AHEAD"), "check") != 0; // check: exact g.hdr = vk_hdr_wanted(); g.test = knob_int("TOTK_FG_TEST"); g.debug = knob_int("TOTK_FG_DEBUG"); diff --git a/runtime/vk_memory.c b/runtime/vk_memory.c index baaabef5..cfc10d78 100644 --- a/runtime/vk_memory.c +++ b/runtime/vk_memory.c @@ -982,6 +982,56 @@ static int query_stamp(uint64_t lo, uint64_t hi, uint32_t g) { } } +// ---------------------------------------------------------------- TOTK_SYNC_AHEAD=check (vk_ahead.c) +int vk_mem_span_inplace(const VkMemSpan *sp) { + const PoolRec *r = sp->pool; + return r && !sp->alloc && r->kind == POOL_IMPORT && !r->smap && sp->buffer == r->buf; +} + +uint32_t vk_mem_ahead_observe(const uint64_t (*iv)[2], int n, uint8_t *untracked) { + plat_mutex_lock(&gen_lock); + uint32_t g = ++gen_now; + for (int i = 0; i < n; i++) { + uint64_t lo = iv[i][0] & ~0xfffull, hi = (iv[i][1] + 0xfff) & ~0xfffull; + int frozen = hi > lo && hi <= RT_ADDRESS_LIMIT; // a frozen region changed in no page since its freeze + for (uint64_t a = lo & ~((1ull << REG_SHIFT) - 1); a < hi && frozen; a += 1ull << REG_SHIFT) frozen = mem_frozen(a); + untracked[i] = (uint8_t)(!frozen && (hi <= lo || hi > RT_ADDRESS_LIMIT || !query_stamp(lo, hi, g))); + } + g = gen_now; + plat_mutex_unlock(&gen_lock); + return g; +} + +uint64_t vk_mem_ahead_written(uint64_t lo, uint64_t hi, uint32_t since, uint64_t *first) { + uint64_t bytes = 0; + if (hi <= lo || hi > RT_ADDRESS_LIMIT) return 0; + plat_mutex_lock_shared(&gen_lock); + for (uint64_t r = lo >> REG_SHIFT; r <= (hi - 1) >> REG_SHIFT; r++) { + if (reg_max[r] <= since) continue; + uint64_t a = r << REG_SHIFT > lo ? r << REG_SHIFT : lo & ~0xfffull; + uint64_t b = (r + 1) << REG_SHIFT < hi ? (r + 1) << REG_SHIFT : hi; + const uint32_t *ch = gen_chunk[a >> GEN_CHUNK_SHIFT]; // a 2 MiB region lies in one chunk + if (!ch) continue; + for (uint64_t x = a; x < b; x += 1ull << GEN_PAGE_SHIFT) { + if (ch[(x >> GEN_PAGE_SHIFT) & (GEN_PER_CHUNK - 1)] <= since) continue; + uint64_t s = x > lo ? x : lo, e = x + (1ull << GEN_PAGE_SHIFT) < hi ? x + (1ull << GEN_PAGE_SHIFT) : hi; + if (!bytes && first) *first = s; + bytes += e - s; + } + } + plat_mutex_unlock_shared(&gen_lock); + return bytes; +} + +int vk_mem_pool_info(uint64_t addr, uint64_t *lo, uint64_t *size, uint32_t *flags, int *mirrored) { + plat_mutex_lock_shared(&pool_lock); + const pidx_ent *e = pidx_find(&pool_index, (uintptr_t)addr, 1); + const PoolRec *r = e ? e->buf : NULL; + if (r) *lo = r->lo, *size = r->size, *flags = r->flags, *mirrored = r->mstate == MS_ON; + plat_mutex_unlock_shared(&pool_lock); + return r != NULL; +} + // ---------------------------------------------------------------- shadow storage // Where importing guest memory costs too much (Linux: re-protecting pages that amdgpu imported as a userptr // invalidates the buffer, about 1.3 ms per 16 MiB at the next submit, and the import's write pinning clears the @@ -1322,6 +1372,7 @@ static uint64_t s_read_waits, s_read_ticks; // shadow_lock: since the last log static uint64_t r_read_waits, r_read_ticks, r_read_max; // shadow_lock: over the run (vk_mem_shadow_read_log) void vk_mem_shadow_read(uint64_t addr, uint64_t size) { + if (vk_ahead_on) vk_ahead_invalidate(addr, size); if (!shadow_on || !size || !nvn_sync_ahead_guard()) return; plat_mutex_lock(&shadow_lock); uint64_t now = vk_sync_issued(); @@ -1396,6 +1447,7 @@ int vk_mem_shadow_deferred(void) { return ndq; } // Record time: the range's base is the shadow's bytes, except where an overlapping pending range has one: the GPU may // have written those shadow bytes since. void vk_mem_gpu_pending(uint64_t addr, uint64_t size) { + if (vk_ahead_on) vk_ahead_gpu_write(addr, size, AHW_WRITE, 0); if (!shadow_on || !size) return; uint64_t lo = addr, hi = addr + size; plat_mutex_lock_shared(&pool_lock); @@ -1689,12 +1741,14 @@ static void mirror_mode(int discrete) { #else shadow_on = !sh || !*sh || strcmp(sh, "0"); #endif - defer_on = shadow_on && cfg_bool("TOTK_SYNC_AHEAD") && cfg_bool("TOTK_SHADOW_DEFER"); + defer_on = shadow_on && cfg_bool("TOTK_SYNC_AHEAD") && strcmp(cfg_value("TOTK_SYNC_AHEAD"), "check") && + cfg_bool("TOTK_SHADOW_DEFER"); // check: the wait stays exact, nothing is deferred if (defer_on) rt_log("mem: TOTK_SHADOW_DEFER: shadow updates that wait for a reader are queued for the GPU instead"); if (shadow_on) rt_log("mem: shadow storage: CPU-visible pools are host-visible copies of guest memory, not imports%s", sh && *sh ? " (TOTK_SHADOW)" : ""); rt_log("mem: mode %s (%s): %s", mode == 2 ? "verify" : mode ? "mirror" : "import", why, mode ? "pools used as buffers get a device-local mirror" : "every CPU-visible pool is imported host memory"); + vk_ahead_init(); // Import is the integrated-GPU mode. On a discrete card every draw reads the pools over PCIe (the comment above // this section has the measurements), which is the difference between playable and 5 fps when it is not intended. if (!mode && discrete) @@ -2309,6 +2363,9 @@ static void freeze_pass(void) { void vk_mem_mirror_begin(void) { if (!vk_mirror_on) { // import mode: the texture checks' snapshots still skip frozen regions + // TOTK_SYNC_AHEAD=check notes in-place bindings where the replay resolves them: a new generation per submission + // makes it resolve them again in each, as mirror mode does. + if (vk_ahead_on) __atomic_add_fetch(&mirror_gen, 1, __ATOMIC_RELEASE); if (!freeze_idle) return; plat_mutex_lock(&gen_lock); ww_ep++; @@ -2370,12 +2427,14 @@ int vk_mem_mirror_pending(void) { return n; } -void vk_mem_mirror_prologue(VkCommandBuffer cb) { +void vk_mem_mirror_prologue(VkCommandBuffer cb, uint64_t fence) { plat_mutex_lock(&mirror_lock); if (!pend_seal && snl_copied == snl_seal) { plat_mutex_unlock(&mirror_lock); return; } + if (vk_ahead_on) // the copies read guest memory when the GPU runs them + for (int i = 0; i < pend_seal; i++) vk_ahead_read(pend[i].r->import_lo + pend[i].off, pend[i].size, AH_MSYNC, fence); mbarrier(cb, VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT, VK_ACCESS_2_MEMORY_WRITE_BIT, VK_PIPELINE_STAGE_2_COPY_BIT, VK_ACCESS_2_TRANSFER_READ_BIT | VK_ACCESS_2_TRANSFER_WRITE_BIT); copy_ranges(cb, pend, pend_seal, 1, &mrep.upload_bytes, &mrep.upload_copies); @@ -2489,6 +2548,8 @@ int vk_mem_mirror_end(VkCommandBuffer cb, uint64_t fence) { mbarrier(cb, VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT, VK_ACCESS_2_MEMORY_WRITE_BIT, VK_PIPELINE_STAGE_2_COMPUTE_SHADER_BIT, VK_ACCESS_2_SHADER_STORAGE_READ_BIT); int m = wb_merge_record(cb); + if (m && vk_ahead_on) // the merge writes guest memory when the GPU runs it + for (int i = 0; i < nsnl; i++) vk_ahead_read(snl[i].r->import_lo + snl[i].off, snl[i].size, AH_WBM, fence); if (!m) { // no range list: copy them plainly for (int i = 0; i < nsnl; i++) range_add(&wbl, &nwbl, &cwbl, 0, snl[i].r, snl[i].off, snl[i].size); } @@ -2508,6 +2569,8 @@ int vk_mem_mirror_end(VkCommandBuffer cb, uint64_t fence) { mbarrier(cb, VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT, VK_ACCESS_2_MEMORY_WRITE_BIT, VK_PIPELINE_STAGE_2_COPY_BIT, VK_ACCESS_2_TRANSFER_READ_BIT); for (int i = 0; i < nwbl; i++) wbl[i].r->wb_sub = msub; + if (vk_ahead_on) + for (int i = 0; i < nwbl; i++) vk_ahead_read(wbl[i].r->import_lo + wbl[i].off, wbl[i].size, AH_WB, fence); mrep.wb_ranges += (uint64_t)nwbl; rec += copy_ranges(cb, wbl, nwbl, 0, &mrep.wb_bytes, &mrep.wb_copies) + 2; mbarrier(cb, VK_PIPELINE_STAGE_2_COPY_BIT, VK_ACCESS_2_TRANSFER_WRITE_BIT, diff --git a/runtime/vk_sync.c b/runtime/vk_sync.c index f48e237f..a5ad6e3d 100644 --- a/runtime/vk_sync.c +++ b/runtime/vk_sync.c @@ -385,7 +385,7 @@ static VkCommandBuffer prologue(VkCtx *c, uint64_t fence, int direct) { } int pair = ts_open(cb); vk_mem_shadow_prologue(cb, fence); // before the mirror copies, which read the shadow these bring up to date - vk_mem_mirror_prologue(cb); + vk_mem_mirror_prologue(cb, fence); ts_close(cb, pair, fence); if (direct || !rec_end_cb(cb, 0, 0)) vkEndCommandBuffer(cb); return cb; diff --git a/runtime/vk_texture.c b/runtime/vk_texture.c index daad15bd..b7761fac 100644 --- a/runtime/vk_texture.c +++ b/runtime/vk_texture.c @@ -1203,6 +1203,7 @@ static int upload_gpu(Tex *e) { vk_upload_end(); if (vk_upload_held() > UPLOAD_HELD_MAX) vk_upload_flush(0); free(dr), free(rg), free(ar); + if (vk_ahead_on) vk_ahead_read(e->pub.addr, t->L.size, AH_DESWZ, 0); // the batch reads the pool when it runs return 1; } @@ -2584,6 +2585,8 @@ void vk_texture_write_back_small(uint64_t fence) { } write_back_n(hit, k); for (int i = 0; i < k; i++) perf_count(PERF_CNT_WB); + if (vk_ahead_on) + for (int i = 0; i < k; i++) vk_ahead_small(hit[i]->pub.addr, hit[i]->gpu_fence); for (int i = k; i < n; i++) perf_count(PERF_CNT_WB_SKIP); free(hit); unlock(); diff --git a/tests/sync_ahead_check.py b/tests/sync_ahead_check.py index ef6d0571..a8499799 100644 --- a/tests/sync_ahead_check.py +++ b/tests/sync_ahead_check.py @@ -1,4 +1,4 @@ -"""Exercise the production sync-ahead wait selection with a deterministic back end. +"""Exercise the production sync-ahead wait selection with a deterministic back end (TOTK_SYNC_AHEAD 0, 1, check). python tests/sync_ahead_check.py [--cc path/to/clang] """ @@ -35,7 +35,19 @@ static int wait_for(uint64_t value, uint64_t timeout) { typedef struct { uint64_t (*fence_done)(void); void (*cpu_synced)(uint64_t); int (*fence_wait)(uint64_t, uint64_t); } GfxBackend; static const GfxBackend backend = {done, cpu_synced, wait_for}; static const GfxBackend *gfx_backend(void) { return &backend; } -static int cfg_bool(const char *name) { assert(!strcmp(name, "TOTK_SYNC_AHEAD")); return enabled; } +// enabled: 0 off, 1 on, 2 check (the wait stays exact; vk_ahead.c is told what the late wait would have waited for) +static const char *cfg_value(const char *name) { + assert(!strcmp(name, "TOTK_SYNC_AHEAD")); + return enabled == 2 ? "check" : enabled ? "1" : "0"; +} +static int entry_calls, lag_calls, done_calls, done_ok; +static uint64_t lag_asked, lag_value; +static void vk_ahead_frame_entry(void) { assert(!lag_calls && !done_calls); entry_calls++; } +static void vk_ahead_frame_lag(uint64_t asked, uint64_t lagged) { + assert(entry_calls == 1 && !sync_calls); // before cpu_synced: the small write-backs see the lag + lag_calls++, lag_asked = asked, lag_value = lagged; +} +static void vk_ahead_frame_done(int ok) { assert(lag_calls == 1); done_calls++, done_ok = ok; } static int vk_mem_shadow_on(void) { return shadow; } static uint64_t nvn_fence_last(void) { return last; } static void rt_log(const char *format, ...) { (void)format; } @@ -66,9 +78,10 @@ int main(int argc, char **argv) { qstamp_on = rt_game_version == 143 && enabled; last = 20; nvn_sync_ahead_present(); last = 40; nvn_sync_ahead_present(); - assert(pa_fence[0] == (enabled && shadow && supported ? 40 : 0)); - assert(pa_fence[1] == (enabled && shadow && supported ? 20 : 0)); - assert(qstamp_on == (rt_game_version == 143 && enabled && shadow)); // a refused switch leaves the game's check + int on = enabled == 1 && shadow && supported, check = enabled == 2 && supported; + assert(pa_fence[0] == (on || check ? 40 : 0)); + assert(pa_fence[1] == (on || check ? 20 : 0)); + assert(qstamp_on == (rt_game_version == 143 && on)); // a refused switch and check leave the game's check int relaxed_cases = 0, polls = 0, other_waits = 0; for (int i = 0; i < 100000; i++) { sync_t sync = {.fenced = random_word() % 2, .fence = random_word() % 65}; @@ -77,16 +90,23 @@ int main(int argc, char **argv) { completed = random_word() % 65; uint64_t initial_done = completed, target = sync.fence; succeed = random_word() % 2; - relaxed = sync.fenced && enabled && shadow && supported && timeout && lr == site && - completed < target && target > 20 && target <= 40; + int lagcase = sync.fenced && timeout && lr == site && completed < target && target > 20 && target <= 40; + relaxed = on && lagcase; + int checked = check && sync.fenced && timeout && lr == site; relaxed_cases += relaxed; polls += timeout == 0; other_waits += lr != site; if (relaxed) target = 20; replay_calls = shadow_calls = sync_calls = wait_calls = 0; + entry_calls = lag_calls = done_calls = 0; synced = waited = waited_timeout = replay_value = UINT64_MAX; pa_guard = 0; Cpu cpu = {0}; cpu.x[0] = (uintptr_t)&sync; cpu.x[1] = timeout; cpu.x[30] = lr; nvnSyncWait(&cpu); assert(replay_calls == relaxed && shadow_calls == relaxed); + assert(entry_calls == checked && lag_calls == checked && done_calls == checked); + if (checked) { // exact wait, the late one's value told to the check + assert(lag_asked == sync.fence && lag_value == (lagcase ? 20 : sync.fence)); + assert(done_ok == (initial_done >= target || succeed)); + } assert(pa_guard == (relaxed ? sync.fence : 0)); if (relaxed) assert(replay_value == sync.fence); if (!sync.fenced) { @@ -102,9 +122,9 @@ int main(int argc, char **argv) { } } assert(polls > 0 && other_waits > 0); - assert((relaxed_cases > 0) == (enabled && shadow && supported)); + assert((relaxed_cases > 0) == on); // Raising the guard for an older request must not lower a guard already published by another waiter. - if (enabled && shadow && supported) { + if (on) { sync_t sync = {.fenced = 1, .fence = 35}; completed = 25; pa_guard = 39; relaxed = 1; Cpu cpu = {0}; cpu.x[0] = (uintptr_t)&sync; cpu.x[1] = UINT64_MAX; cpu.x[30] = site; @@ -130,7 +150,7 @@ def main(): path.write_text(HARNESS.replace("PRODUCTION", source[start:end])) subprocess.run([args.cc, "-std=c11", "-O2", "-Wall", "-Werror", str(path), "-o", str(exe)], check=True) for version in (110, 121, 143, 999): - for enabled in (0, 1): + for enabled in (0, 1, 2): for shadow in (0, 1): subprocess.run([str(exe), str(version), str(enabled), str(shadow)], check=True)