From beda188776e2f2191830457db9c3b0d60871f9d7 Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Thu, 5 Nov 2026 12:53:17 +0300 Subject: [PATCH 1/5] Drop released textures the game has overwritten when VRAM runs short. Under pressure only clean mirrored entries could go, so once those ran out the render targets stayed: 1.7-2.0 GiB of released images with the use still over the line. Most of them sit on storage the game has since given to another texture, and they are never taken back. The pressure path now drops those first, 30 s after their release and when no finalized slot has bound them for 600 submits. Clean entries wait 5 s, which ends the re-creation churn in fights, and both kinds stop once the use is back under the line. TOTK_TRACE=perf gains mem: released lines that split released memory by kind, age, overlap and dedicated memory, and count take-backs and re-creations. TOTK_VRAM_LIMIT_MIB caps the budget the port sees, so the path can be driven on a large card. Co-Authored-By: Claude Opus 5.5 --- docs/settings.md | 3 +- runtime/cfg.c | 2 +- runtime/vk.h | 1 + runtime/vk_memory.c | 16 +++ runtime/vk_texture.c | 248 ++++++++++++++++++++++++++++++++++++++++--- 5 files changed, 256 insertions(+), 14 deletions(-) diff --git a/docs/settings.md b/docs/settings.md index e51c339c..d6ffbb4e 100644 --- a/docs/settings.md +++ b/docs/settings.md @@ -64,6 +64,7 @@ Many rows below are developer tools that only act together with another setting | Setting | What it does | Effect of changing it | Safety | | --- | --- | --- | --- | +| `vram_limit_mib` | Pretends the device-local memory budget is at most this many MiB, for the paths that react to the budget (`tex_vram_pressure`, mirror refusal); the driver knows nothing of it. | Lets a large card drive those paths; a low value drops released textures and refuses mirrors that would have fit. | Caution | | `background_window` | `1` keeps the window behind the user's windows, never fullscreen at start, and F11 keeps its place; SDL windows ignore it. | The window does not come to the front or flash, so it is easy to miss; meant for unattended runs. | Caution | | `gpu` | `vulkan` or `null`: the graphics back end; `null` has no GPU, completes every submit at once and returns zeros for texture reads. | `null` shows no picture; any other value stops the start with an error. | Caution | | `input` | `none` reports no input from the keyboard, mouse or controllers. | The game gets no controller input and the menu cursor stops following the mouse; `input_script` still applies. | Caution | @@ -132,7 +133,7 @@ Many rows below are developer tools that only act together with another setting | `pipe_sync` | Any value other than `0` or empty stops graphics pipelines from being built in the background. | Pipelines are built on the render path, which causes stutter when new effects appear. | Caution | | `push_stage` | `fs` pushes the descriptor set from the fragment stage instead of the vertex stage. | The log names the stage in use; the code does not say what changes in the picture. | Unknown | | `rec_end` (default `1`) | `0` lets the submitting thread end a recorded part again, as an older version did. | On some AMD cards the GPU hangs or the scene turns green and then white when a submission is split. | Risky | -| `tex_vram_pressure` (default `90`) | Percent of the device-local memory budget above which unused released textures are dropped, 0 to 100; `0` is off. | A low value drops textures sooner and re-uploads them; `0` keeps them and lets a 4 GiB card spill into system memory. Also a slider in the menu. | Caution | +| `tex_vram_pressure` (default `90`) | Percent of the device-local memory budget above which released textures are dropped, 0 to 100; `0` is off. First those the game has overwritten with a later texture and left alone for 30 s, then those that can be uploaded again. | A low value drops textures sooner and re-uploads them; `0` keeps them and lets the card spill into system memory. Also a slider in the menu. | Caution | | `txd_transpose` (default `1`) | `0` turns off the swap of gradient components for gradient texture samples. | Terrain textures turned across the screen show crawling patches again. | Caution | | `ubo_snapshot` (default `1`) | `0` binds live memory for uniform blocks; `ab` alternates with the `ab` blocks. | Without snapshots late draws read another object's matrix, so objects can be drawn in the wrong place. | Risky | | `vk_no_float64` (default `0`) | `1` clears the 64-bit float feature, so the translator lowers FP64 to FP32. | Shaders change, so the shader cache entries for it are made again; precision of FP64 shaders drops. | Caution | diff --git a/runtime/cfg.c b/runtime/cfg.c index 36795a01..d49bc43c 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -58,7 +58,7 @@ static const char *const known[] = { "SHOTS_FULL", "SPEED_PROBE", "SPIN", "SPIN_HANDOFF_US", "SPLIT_DRAWS", "SPLIT_US", "STICK_DEADZONE", "STICK_SENS", "SWKBD_ANSWER", "SWKBD_TEST", "TEST_PAD", "TEST_PREPARE_SHADERS", "TEX_ALIAS", "TEX_MEMO", "TEX_VRAM_PRESSURE", "TIMER_1MS", "TRACE", "TXD_TRANSPOSE", "UBO_SNAPSHOT", "UPSCALER", "VK_DYN_A2C", "VK_DYN_VERTEX", "VK_NO_FLOAT64", - "VK_PRESENT", "VK_RECORDER", "VK_SSBO_ALIGN", "VK_SUBGROUP", "VK_SUBGROUP_PIN", "VK_VALIDATION", "VOLUME", "VRAM_HEADROOM", "VS_OUTS", + "VK_PRESENT", "VK_RECORDER", "VK_SSBO_ALIGN", "VK_SUBGROUP", "VK_SUBGROUP_PIN", "VK_VALIDATION", "VOLUME", "VRAM_HEADROOM", "VRAM_LIMIT_MIB", "VS_OUTS", "WALK_KEY", "WALK_SCALE", "WB_EXACT", "WB_FENCE_LIMIT", "WB_MERGE", "WINDOW_SIZE", "XLATE", "XLATE_ID", "XLATE_KEEP", "XLATE_PROCS", "ZSTD_CHECK", "ZSTD_NATIVE", }; diff --git a/runtime/vk.h b/runtime/vk.h index 91f127fa..39cfb9a4 100644 --- a/runtime/vk.h +++ b/runtime/vk.h @@ -428,6 +428,7 @@ void vk_texture_stats(uint64_t *images, uint64_t *uploads, uint64_t *reuploads); // (images not bound into pool memory). typedef struct { uint64_t live, released, own_live, own_released, bytes_live, bytes_released; } VkTexCensus; void vk_texture_census(VkTexCensus *s); +void vk_texture_released_log(uint64_t frame); // mem: released lines of vram_log void vk_texture_shutdown(void); // vk_mem_shutdown calls it void vk_texture_new_submit(void); // a queue submit starts: draw bindings check storage again uint32_t vk_texture_epoch(void); // the write-watch snapshot epoch vk_texture_new_submit started diff --git a/runtime/vk_memory.c b/runtime/vk_memory.c index 7ee7acb8..d40119b6 100644 --- a/runtime/vk_memory.c +++ b/runtime/vk_memory.c @@ -565,10 +565,23 @@ void vk_mem_census(uint64_t frame) { window_start = frame; } +// TOTK_VRAM_LIMIT_MIB: pretend the device-local budget is at most this many MiB, so the paths that react to the budget +// can be driven on a card larger than the one a report came from. The driver knows nothing of it. +static uint64_t vram_limit(void) { + static int64_t limit = -1; + if (limit < 0) { + const char *s = cfg_get("TOTK_VRAM_LIMIT_MIB"); + limit = s && *s ? (int64_t)strtoull(s, NULL, 10) << 20 : 0; + if (limit) rt_log("mem: device-local budget limited to %lld MiB (TOTK_VRAM_LIMIT_MIB)", (long long)(limit >> 20)); + } + return (uint64_t)limit; +} + int vk_mem_vram(uint64_t *used, uint64_t *budget, int *exact) { VkCtx *c = vk_ctx(); *used = *budget = 0, *exact = 0; if (!c) return 0; + uint64_t limit = vram_limit(); const VkPhysicalDeviceMemoryProperties *mp = &c->mem_props; if (c->has_memory_budget) { VkPhysicalDeviceMemoryBudgetPropertiesEXT b = {.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MEMORY_BUDGET_PROPERTIES_EXT}; @@ -578,6 +591,7 @@ int vk_mem_vram(uint64_t *used, uint64_t *budget, int *exact) { if (p2.memoryProperties.memoryHeaps[h].flags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT) *used += b.heapUsage[h], *budget += b.heapBudget[h]; *exact = 1; + if (limit && *budget > limit) *budget = limit; return *budget != 0; } VmaBudget vb[VK_MAX_MEMORY_HEAPS]; @@ -585,6 +599,7 @@ int vk_mem_vram(uint64_t *used, uint64_t *budget, int *exact) { for (uint32_t h = 0; h < mp->memoryHeapCount && h < VK_MAX_MEMORY_HEAPS; h++) if (mp->memoryHeaps[h].flags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT) *used += vb[h].statistics.blockBytes, *budget += mp->memoryHeaps[h].size; + if (limit && *budget > limit) *budget = limit; return *budget != 0; } @@ -2050,6 +2065,7 @@ static void vram_log(uint64_t frame) { alloc_bytes / M, (unsigned long long)t.live, (unsigned long long)t.own_live, t.bytes_live / M, (unsigned long long)t.released, (unsigned long long)t.own_released, t.bytes_released / M, (long long)m_live_n, m_live_bytes / M, dead_pending()); + vk_texture_released_log(frame); } // Device class: INTEGRATED -> uma; a discrete GPU with a DEVICE_LOCAL heap of 1 GiB or more beside a diff --git a/runtime/vk_texture.c b/runtime/vk_texture.c index bd0c5be8..142b01f7 100644 --- a/runtime/vk_texture.c +++ b/runtime/vk_texture.c @@ -61,6 +61,10 @@ typedef struct Tex { uint8_t *snap; // a texture that fills no page: its storage bytes when the GPU write was recorded uint32_t wt_n; // WriteTexels into the storage since the last upload struct Tex *rel_prev, *rel_next; // while released: the list of released entries, oldest first + uint64_t rel_ms; // while released: when (coarse_ms) + uint64_t use_seq; // order of the last creation or take-back among all entries + uint64_t rel_seq; // while released: use_seq of the newest entry placed before this release; an entry + // with a later use_seq on overlapping storage replaced this one's bytes } Tex; // A slot keeps what vk_texture_by_id resolved for it, so a draw binding skips the key lookup and the address chain: the @@ -529,6 +533,46 @@ static void rel_unlink(Tex *e) { e->rel_prev = e->rel_next = NULL; } +// A clock read per submit, for stamps on paths that run many times a frame. +static uint64_t coarse_ms; +static uint64_t use_counter; + +// How long released entries wait until the game takes them back, split into those holding GPU-written content and the +// others (TOTK_TRACE=perf, vk_texture_released_log): where transient textures end and the tail that can go begins. +enum { AGE_1S, AGE_5S, AGE_30S, AGE_120S, AGE_LONG, AGE_N }; +static const char *const age_name[AGE_N] = {"<1s", "<5s", "<30s", "<120s", ">=120s"}; +static uint64_t takeback_n[2][AGE_N]; +static int age_bucket(uint64_t ms) { + return ms < 1000 ? AGE_1S : ms < 5000 ? AGE_5S : ms < 30000 ? AGE_30S : ms < 120000 ? AGE_120S : AGE_LONG; +} + +// Keys of entries the pressure path dropped, to count the ones the game asks for again soon after: a drop that buys +// nothing and, for GPU-written content, loses it. Direct-mapped, a lost record only undercounts. +#define DROPPED_SLOTS 4096u +static struct { uint64_t hash, ms; } dropped[DROPPED_SLOTS]; +static uint64_t n_recreated[2], n_recreated_written[2]; // index: within 10 s, later +static uint64_t key_hash(const TexKey *k) { + uint64_t h = 0xcbf29ce484222325ull; + const uint8_t *b = (const uint8_t *)k; + for (size_t i = 0; i < sizeof *k; i++) h = (h ^ b[i]) * 0x100000001b3ull; + return h | 1; +} +static void dropped_note(const Tex *e) { + uint64_t h = key_hash(&e->key); + dropped[h >> 3 & (DROPPED_SLOTS - 1)].hash = h ^ (uint64_t)(e->pub.gpu_owned != 0) << 1; + dropped[h >> 3 & (DROPPED_SLOTS - 1)].ms = coarse_ms; +} +static void dropped_check(const TexKey *k) { + uint64_t h = key_hash(k); + unsigned i = h >> 3 & (DROPPED_SLOTS - 1); + uint64_t was = dropped[i].hash; + if (!was || (was & ~2ull) != (h & ~2ull)) return; + int later = coarse_ms - dropped[i].ms >= 10000; + n_recreated[later]++; + if (was != h) n_recreated_written[later]++; + dropped[i].hash = 0; +} + static void destroy_tex(Tex *e) { own_remove(e); tex_gen_bump(e->key.addr); @@ -558,15 +602,112 @@ static void sweep_released(void) { while (nreleased > RELEASED_MAX && rel_head) destroy_tex(rel_head); } -// TOTK_TEX_VRAM_PRESSURE (percent of the device-local budget, default 90; 0 = off): above it, each submit drops up to -// PRESSURE_STEP of the oldest released entries, so a 4 GiB card does not keep RELEASED_MAX released images (each ASTC -// one decoded to RGBA8) while the heap spills into system memory. A few per submit, for the reason above. Only entries -// that come back intact are dropped: mirrored ones the GPU never wrote, re-uploaded from their storage. A render target -// lives only in its image; dropping it brought the texture back empty (black flashes in a Depths boss fight, ~170 +// TOTK_TEX_VRAM_PRESSURE (percent of the device-local budget, default 90; 0 = off): above it, each budget query drops up +// to PRESSURE_STEP released entries, oldest first, until the use is back under the line; a few at a time, for the reason +// above. Two kinds come back without loss: +// +// Overwritten: the game placed another texture on the entry's storage after releasing it, so on the Switch its bytes are +// gone too, and it has waited DEAD_AGE_MS. Transient render targets are taken back within a second or two (a stand-still +// field window: about 19000 take-backs under 1 s, 2 later than that, none after 30 s), while their leftovers at +// addresses the game's allocator moved on from stay released for good: 1.9 GiB of 2.0 GiB, nearly all of it render targets +// no storage can refill. Not bound through a finalized slot for DEAD_QUIET_SUBMITS either: such a slot still samples the +// entry (slot_resolve). +// +// Clean: mirrored ones the GPU never wrote, re-uploaded from their storage, at the cost of the upload, so after +// CLEAN_AGE_MS: without the wait a fight re-created about 500 of them per 10 s. Any other render target lives only in its +// image: dropping one that was merely old brought the texture back empty (black flashes in a Depths boss fight, ~170 // "left uninitialised" aliases a minute). #define PRESSURE_STEP 32 +#define CLEAN_AGE_MS 5000u +#define DEAD_AGE_MS 30000u +#define DEAD_QUIET_SUBMITS 600u +#define DEAD_RETRY_QUERIES 8 // budget queries to skip after a search that found nothing static int vram_pressure = -1; -static uint64_t n_pressure_drops; +static uint64_t n_pressure_drops, n_pressure_overwritten; + +static int tex_by_addr(const void *a, const void *b) { + const Tex *x = *(Tex *const *)a, *y = *(Tex *const *)b; + return x->pub.addr < y->pub.addr ? -1 : x->pub.addr > y->pub.addr; +} + +// All entries sorted by storage address, with the highest storage end up to each one: the input of storage_taken. +typedef struct { Tex **e; uint64_t *end; int n; } AddrIndex; + +static int addr_index(AddrIndex *x) { + x->n = 0; + x->e = malloc(sizeof *x->e * (size_t)(by_key.n + 1)); + x->end = malloc(sizeof *x->end * (size_t)(by_key.n + 1)); + if (!x->e || !x->end) { free(x->e), free(x->end); return 0; } + for (uint32_t i = 0; i < by_key.cap; i++) + if (by_key.vals[i]) x->e[x->n++] = by_key.vals[i]; + qsort(x->e, (size_t)x->n, sizeof *x->e, tex_by_addr); + uint64_t top = 0; + for (int i = 0; i < x->n; i++) { + uint64_t end = x->e[i]->pub.addr + x->e[i]->t.L.size; + x->end[i] = top = end > top ? end : top; + } + return 1; +} + +static void addr_index_free(AddrIndex *x) { free(x->e), free(x->end); } + +// A texture placed after the release of x->e[i] overlaps its storage. +static int storage_taken(const AddrIndex *x, int i) { + const Tex *e = x->e[i]; + for (int j = i; --j >= 0 && x->end[j] > e->pub.addr;) + if (x->e[j]->pub.addr + x->e[j]->t.L.size > e->pub.addr && x->e[j]->use_seq > e->rel_seq) return 1; + for (int j = i; ++j < x->n && x->e[j]->pub.addr < e->pub.addr + e->t.L.size;) + if (x->e[j]->use_seq > e->rel_seq) return 1; + return 0; +} + +static uint64_t own_bytes(const Tex *e) { + VkCtx *c = vk_ctx(); + if (!e->alloc || !c) return 0; + VmaAllocationInfo ai; + vmaGetAllocationInfo(c->vma, e->alloc, &ai); + return ai.size; +} + +static int dead_candidate(const Tex *e) { + return e->alloc && (!e->checked || submit_epoch - e->checked >= DEAD_QUIET_SUBMITS); +} + +// Drops overwritten entries until `need` bytes are free or the step is used up. The sort runs only under pressure, while +// an old enough entry exists, and rests for a while when it finds nothing to drop. Returns the bytes freed. +static uint64_t pressure_search_ns; // longest search since the last mem: released line +static uint64_t drop_overwritten(uint64_t need, int *dropped) { + static int rest; + if (rest > 0) { rest--; return 0; } + if (!rel_head || coarse_ms - rel_head->rel_ms < DEAD_AGE_MS) return 0; + uint64_t t0 = plat_now_ns(), freed = 0; + AddrIndex x; + if (!addr_index(&x)) return 0; + Tex *cand[PRESSURE_STEP]; + int nc = 0; + for (Tex *e = rel_head; e && nc < PRESSURE_STEP && coarse_ms - e->rel_ms >= DEAD_AGE_MS; e = e->rel_next) { + if (!dead_candidate(e)) continue; + int lo = 0, hi = x.n; // the first entry at e's address, then e among those + while (lo < hi) { + int m = (lo + hi) / 2; + if (x.e[m]->pub.addr < e->pub.addr) lo = m + 1; + else hi = m; + } + while (lo < x.n && x.e[lo] != e) lo++; + if (lo < x.n && storage_taken(&x, lo)) cand[nc++] = e; + } + addr_index_free(&x); + for (int i = 0; i < nc && freed < need; i++) { + freed += own_bytes(cand[i]); + dropped_note(cand[i]); + destroy_tex(cand[i]); + (*dropped)++, n_pressure_drops++, n_pressure_overwritten++; + } + if (!nc) rest = DEAD_RETRY_QUERIES; + uint64_t ns = plat_now_ns() - t0; + if (ns > pressure_search_ns) pressure_search_ns = ns; + return freed; +} static void sweep_pressure(void) { // tex_lock held if (vram_pressure < 0) { @@ -578,11 +719,16 @@ static void sweep_pressure(void) { // tex_lock held if (!vram_pressure || !rel_head || submit_epoch & 7) return; // the budget query every 8 submits uint64_t used, budget; int exact; - if (!vk_mem_vram(&used, &budget, &exact) || used <= budget / 100 * (uint64_t)vram_pressure) return; + if (!vk_mem_vram(&used, &budget, &exact)) return; + uint64_t line = budget / 100 * (uint64_t)vram_pressure; + if (used <= line) return; int dropped = 0; - for (Tex *e = rel_head, *next; e && dropped < PRESSURE_STEP; e = next) { + uint64_t need = used - line, freed = drop_overwritten(need, &dropped); + for (Tex *e = rel_head, *next; e && freed < need && dropped < PRESSURE_STEP && coarse_ms - e->rel_ms >= CLEAN_AGE_MS; e = next) { next = e->rel_next; if (!e->pub.mirrored || e->pub.gpu_owned || e->gpu_dirty) continue; + freed += own_bytes(e); + dropped_note(e); destroy_tex(e); dropped++, n_pressure_drops++; } @@ -936,6 +1082,8 @@ static Tex *entry_of(const tex_t *t) { } if (!(e = create_tex(t, &k, tex_alias))) return NULL; perf_count(PERF_CNT_TEX_NEW); + dropped_check(&k); + e->use_seq = ++use_counter; // released entries of another descriptor at this address are replaced by the new one; a live one holding // GPU-written content is the alias source, preferring the one that differs only in swizzle (same_but_swizzle) int alias = 0; @@ -971,7 +1119,11 @@ static Tex *entry_of(const tex_t *t) { nvn_hm_put(&by_key, &k, e); tex_gen_bump(k.addr); } - if (e->released) e->released = 0, nreleased--, rel_unlink(e), tex_gen_bump(k.addr); + if (e->released) { + takeback_n[e->pub.gpu_owned != 0][age_bucket(coarse_ms - e->rel_ms)]++; + e->use_seq = ++use_counter; + e->released = 0, nreleased--, rel_unlink(e), tex_gen_bump(k.addr); + } e->t = *t; return e; } @@ -1062,6 +1214,8 @@ void vk_texture_release(const tex_t *t) { Tex *e = nvn_hm_get(&by_key, &k); if (e && !e->released) { e->released = 1; + e->rel_ms = coarse_ms; + e->rel_seq = use_counter; nreleased++; rel_append(e); tex_gen_bump(k.addr); @@ -1219,6 +1373,7 @@ uint32_t vk_texture_epoch(void) { return __atomic_load_n(&submit_epoch, __ATOMIC void vk_texture_new_submit(void) { lock(); if (!++submit_epoch) submit_epoch = 1; + coarse_ms = plat_now_ms(); sweep_pressure(); static uint64_t log_at; uint64_t f = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED) / 60; @@ -1238,9 +1393,10 @@ void vk_texture_new_submit(void) { (unsigned long long)f * 60, (unsigned long long)ck, (unsigned long long)n_checks, (unsigned long long)calls, (unsigned long long)untr); n_checks = 0; - rt_log("perf: frame %llu: released textures kept %d, dropped under VRAM pressure %llu", (unsigned long long)f * 60, - nreleased, (unsigned long long)n_pressure_drops); - n_pressure_drops = 0; + rt_log("perf: frame %llu: released textures kept %d, dropped under VRAM pressure %llu (%llu overwritten)", + (unsigned long long)f * 60, nreleased, (unsigned long long)n_pressure_drops, + (unsigned long long)n_pressure_overwritten); + n_pressure_drops = n_pressure_overwritten = 0; } unlock(); } @@ -1946,6 +2102,74 @@ void vk_texture_census(VkTexCensus *s) { unlock(); } +// TOTK_TRACE=perf, with mem: vram: what the released entries in own memory hold and how long they waited. +void vk_texture_released_log(uint64_t frame) { + VkCtx *c = vk_ctx(); + if (!c) return; + enum { CL_CLEAN, CL_BACKED, CL_DIRTY, CL_POOLS, CL_N }; + uint64_t cl_n[CL_N] = {0}, cl_b[CL_N] = {0}, age_b[AGE_N] = {0}, dead_n = 0, dead_b = 0, dead_dirty_b = 0; + uint64_t bound_n = 0, bound_b = 0, ded_n = 0, ded_b = 0; + typedef struct { uint32_t w, h, fmt, n; uint64_t b; } Group; + Group gr[256]; + int ng = 0; + lock(); + AddrIndex x = {0}; + int indexed = addr_index(&x); + for (int i = 0; indexed && i < x.n; i++) { + Tex *e = x.e[i]; + if (!e->released || !e->alloc) continue; + VmaAllocationInfo2 ai2; + vmaGetAllocationInfo2(c->vma, e->alloc, &ai2); + const VmaAllocationInfo ai = ai2.allocationInfo; + if (ai2.dedicatedMemory) ded_n++, ded_b += ai.size; + int cl = !e->pub.mirrored ? CL_POOLS : e->gpu_dirty ? CL_DIRTY : e->pub.gpu_owned ? CL_BACKED : CL_CLEAN; + cl_n[cl]++, cl_b[cl] += ai.size; + if (cl != CL_CLEAN) age_b[age_bucket(coarse_ms - e->rel_ms)] += ai.size; + if (e->checked && submit_epoch - e->checked < 600) bound_n++, bound_b += ai.size; + if (storage_taken(&x, i)) dead_n++, dead_b += ai.size, dead_dirty_b += cl != CL_CLEAN ? ai.size : 0; + int g = 0; + while (g < ng && (gr[g].w != e->t.d.width || gr[g].h != e->t.d.height || gr[g].fmt != (uint32_t)e->t.d.format)) g++; + if (g == ng && ng < 256) gr[ng++] = (Group){e->t.d.width, e->t.d.height, (uint32_t)e->t.d.format, 0, 0}; + if (g < ng) gr[g].n++, gr[g].b += ai.size; + } + if (indexed) addr_index_free(&x); + const double M = 1048576.0; + rt_log("mem: released frame %llu: own memory: clean %llu %.0f MiB, GPU-written with storage current %llu %.0f MiB, " + "GPU-written image only %llu %.0f MiB, not mirrored %llu %.0f MiB; all but clean by age <1s %.0f <5s %.0f <30s %.0f " + "<120s %.0f older %.0f MiB; storage taken by a later texture %llu %.0f MiB (%.0f not clean); bound through a slot " + "in the last 600 submits %llu %.0f MiB; in dedicated memory, returned to the driver when dropped, %llu %.0f MiB; " + "longest search %.2f ms", + (unsigned long long)frame, (unsigned long long)cl_n[CL_CLEAN], cl_b[CL_CLEAN] / M, (unsigned long long)cl_n[CL_BACKED], + cl_b[CL_BACKED] / M, (unsigned long long)cl_n[CL_DIRTY], cl_b[CL_DIRTY] / M, (unsigned long long)cl_n[CL_POOLS], + cl_b[CL_POOLS] / M, age_b[AGE_1S] / M, age_b[AGE_5S] / M, age_b[AGE_30S] / M, age_b[AGE_120S] / M, + age_b[AGE_LONG] / M, (unsigned long long)dead_n, dead_b / M, dead_dirty_b / M, (unsigned long long)bound_n, + bound_b / M, (unsigned long long)ded_n, ded_b / M, pressure_search_ns / 1e6); + pressure_search_ns = 0; + for (int k = 0; k < 2; k++) { + char line[256]; + size_t o = 0; + for (int a = 0; a < AGE_N; a++) { + o += (size_t)snprintf(line + o, sizeof line - o, " %s %llu", age_name[a], (unsigned long long)takeback_n[k][a]); + takeback_n[k][a] = 0; + } + rt_log("mem: released frame %llu: taken back after,%s:%s", (unsigned long long)frame, k ? " GPU-written" : " others", line); + } + rt_log("mem: released frame %llu: dropped under pressure and created again: within 10 s %llu (%llu GPU-written), later %llu (%llu)", + (unsigned long long)frame, (unsigned long long)n_recreated[0], (unsigned long long)n_recreated_written[0], + (unsigned long long)n_recreated[1], (unsigned long long)n_recreated_written[1]); + n_recreated[0] = n_recreated[1] = n_recreated_written[0] = n_recreated_written[1] = 0; + for (int top = 0; top < 4; top++) { + int best = -1; + for (int g = 0; g < ng; g++) + if (gr[g].n && (best < 0 || gr[g].b > gr[best].b)) best = g; + if (best < 0) break; + rt_log("mem: released frame %llu: largest %d: %ux%u fmt %#x: %u entries %.0f MiB", (unsigned long long)frame, top + 1, + gr[best].w, gr[best].h, gr[best].fmt, gr[best].n, gr[best].b / M); + gr[best].n = 0; + } + unlock(); +} + void vk_texture_shutdown(void) { VkCtx *c = vk_ctx(); lock(); From 9a81e3e2f15bcb7b897034bfda012da27adccd8c Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Fri, 6 Nov 2026 10:07:38 +0300 Subject: [PATCH 2/5] Keep regions the GPU writes from being frozen read-only, and thaw a frozen one before a GPU write. Storage-buffer stores, copies and fills land in imported guest memory without a fault or a write-watch bit, so a region only the GPU wrote looked idle and was frozen while the GPU kept writing it. LAS Recomp never changes the protection of imported memory because of NVIDIA. A possible cause of the NVIDIA "write invalid" device faults (#788, #802), not proven. Co-Authored-By: Claude Opus 5.5 --- runtime/vk_memory.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/runtime/vk_memory.c b/runtime/vk_memory.c index 7ee7acb8..f4ac80ce 100644 --- a/runtime/vk_memory.c +++ b/runtime/vk_memory.c @@ -633,6 +633,17 @@ static uint32_t reg_tried[RT_ADDRESS_LIMIT >> REG_SHIFT]; // gen_lock: ww_ep o static uint32_t *fcand; // gen_lock: regions to try at the next freeze pass static int nfcand, cfcand; static uint64_t n_frz_refused; // gen_lock: candidates mem_freeze refused +// Regions the GPU writes. Storage-buffer stores, buffer copies and fills land in imported guest memory, directly (import +// mode) or through a pool's mirror that vk_mem_mirror_end copies or merges into the imported buffer. They are not CPU +// writes: no fault thaws the region and no write-watch bit is set, so a region only the GPU writes looked idle, was +// frozen, and the GPU went on writing pages the CPU side had made read-only. LAS Recomp never changes the protection of +// imported memory: on NVIDIA, VirtualProtect on pages imported with VK_EXT_external_memory_host left the GPU reading +// wrong data (runtime/nvn/vulkan/exec.c, the comment on its write watch); a GPU write there is the likely cause of the +// device faults "write invalid" at a page of guest memory on NVIDIA (#788, #802). reg_gpu holds the timeline value of +// the newest submission that writes into the region (gpu_writes): freeze_pass leaves the region alone until the GPU is +// past that value, and a frozen region is thawed when a write into it is recorded. +static uint64_t reg_gpu[RT_ADDRESS_LIMIT >> REG_SHIFT]; // replay thread +static volatile int64_t n_frz_gpu, n_thaw_gpu; // freeze candidates kept for a GPU write; thaws for one (mirror_log) static uint32_t *gen_slot(uint64_t page_addr, int create) { uint64_t ci = page_addr >> GEN_CHUNK_SHIFT; @@ -1693,8 +1704,26 @@ static void snap_free(void *arg) { // completion thread: the submission is don free(f); } +// Record time: the submission being recorded writes [off, off+size) of r's guest range, directly into the imported +// buffer or into the mirror whose bytes go back at its end. Keeps the regions unfrozen until the GPU is past it +// (reg_gpu). Shadow storage needs nothing: the GPU writes the shadow and the CPU copies it back, thawing as it goes. +static void gpu_writes(const PoolRec *r, uint64_t off, uint64_t size) { + if (!freeze_idle || r->smap) return; + uint64_t a = r->import_lo + off, b = a + size; + if (b <= a || b > RT_ADDRESS_LIMIT) return; + uint64_t f = vk_exec_recording_fence(); + if (!f) f = vk_sync_issued() + 1; + int frozen = 0; + for (uint64_t j = a >> REG_SHIFT; j <= (b - 1) >> REG_SHIFT; j++) { + if (__atomic_load_n(®_gpu[j], __ATOMIC_RELAXED) < f) __atomic_store_n(®_gpu[j], f, __ATOMIC_RELAXED); + frozen |= mem_frozen(j << REG_SHIFT); + } + if (frozen) mem_thaw(a, b - a), plat_inc64(&n_thaw_gpu); +} + static void gpu_wrote(const VkMemSpan *sp, uint64_t size, int shader) { PoolRec *r = sp->pool; + if (r && size) gpu_writes(r, sp->offset, size); if (!r || r->mstate != MS_ON || sp->buffer != r->mbuf || !size) return; // A binding to the end of its pool (no size) can span hundreds of MiB: those stay copies. if (shader && !(sp->offset & 3) && !(size & 3) && size <= MERGE_MAX && merging() && snap_add(r, sp->offset, size)) @@ -1778,9 +1807,11 @@ void nvn_pool_flushed(const pool_t *p, uint64_t off, uint64_t size) { // gen_lock exclusive: freezes up to FREEZE_PER_SUB candidate regions still idle. The query after the // protection collects the writes made before it took effect. static void freeze_pass(void) { + uint64_t gpu_done = vk_sync_fence_done(); for (int done = 0; nfcand && done < FREEZE_PER_SUB;) { uint64_t j = fcand[--nfcand], a = j << REG_SHIFT; if (ww_ep - reg_last[j] < (uint32_t)freeze_idle || mem_frozen(a)) continue; + if (__atomic_load_n(®_gpu[j], __ATOMIC_RELAXED) > gpu_done) { plat_inc64(&n_frz_gpu); continue; } // gpu_writes done++; if (!mem_freeze(a)) { n_frz_refused++; continue; } query_stamp(a, a + (1ull << REG_SHIFT), ++gen_now); @@ -1998,6 +2029,10 @@ static void mirror_log(uint64_t frame) { "or mapping change %llu, refused %llu", (unsigned long long)frame, wc / wf, wb / wf / 1048576.0, qpc_ms(wt) / wf, dc / wf, wp / wf, qpc_ms(dt) / wf, fz * 2.0, (unsigned long long)ff, (unsigned long long)ft, (unsigned long long)fo, (unsigned long long)nr); + int64_t kg = plat_xchg64(&n_frz_gpu, 0), tg = plat_xchg64(&n_thaw_gpu, 0); + if (kg || tg) // gpu_writes: before this, every count here was a region frozen under a GPU write + rt_log("mem: frame %llu: regions the GPU writes, since the last line: %lld kept from freezing, %lld frozen ones " + "thawed", (unsigned long long)frame, (long long)kg, (long long)tg); if (!vk_mirror_on) return; plat_mutex_lock(&mirror_stat_lock); MirrorAcc a = macc; From 895d202ccefe973ee76b5481953402d84c9ebc5b Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Fri, 6 Nov 2026 10:07:45 +0300 Subject: [PATCH 3/5] Let the recorder thread begin, end and submit the parts it records. The part command buffers and their pool were used by two threads. The submitting thread still waits for the submission, so the queue order is unchanged. TOTK_REC_END=0 brings the old split back. Co-Authored-By: Claude Opus 5.5 --- docs/settings.md | 2 +- runtime/vk_rec.c | 120 ++++++++++++++++++++++++++++++++++++++-------- runtime/vk_rec.h | 28 ++++++----- runtime/vk_sync.c | 27 +++-------- 4 files changed, 124 insertions(+), 53 deletions(-) diff --git a/docs/settings.md b/docs/settings.md index e51c339c..8f8992b5 100644 --- a/docs/settings.md +++ b/docs/settings.md @@ -131,7 +131,7 @@ Many rows below are developer tools that only act together with another setting | `pcss_k` (default `0.5`) | Depth-to-penumbra factor of soft shadows at 100 percent, for tuning. | Changes how wide soft shadows blur; has no effect while `pcss` is `0`. | Caution | | `pipe_sync` | Any value other than `0` or empty stops graphics pipelines from being built in the background. | Pipelines are built on the render path, which causes stutter when new effects appear. | Caution | | `push_stage` | `fs` pushes the descriptor set from the fragment stage instead of the vertex stage. | The log names the stage in use; the code does not say what changes in the picture. | Unknown | -| `rec_end` (default `1`) | `0` lets the submitting thread end a recorded part again, as an older version did. | On some AMD cards the GPU hangs or the scene turns green and then white when a submission is split. | Risky | +| `rec_end` (default `1`) | `0` lets the submitting thread begin, end and submit a recorded part again, as older versions did. | On some AMD cards the GPU hangs or the scene turns green and then white when a submission is split. | Risky | | `tex_vram_pressure` (default `90`) | Percent of the device-local memory budget above which unused released textures are dropped, 0 to 100; `0` is off. | A low value drops textures sooner and re-uploads them; `0` keeps them and lets a 4 GiB card spill into system memory. Also a slider in the menu. | Caution | | `txd_transpose` (default `1`) | `0` turns off the swap of gradient components for gradient texture samples. | Terrain textures turned across the screen show crawling patches again. | Caution | | `ubo_snapshot` (default `1`) | `0` binds live memory for uniform blocks; `ab` alternates with the `ab` blocks. | Without snapshots late draws read another object's matrix, so objects can be drawn in the wrong place. | Risky | diff --git a/runtime/vk_rec.c b/runtime/vk_rec.c index ffa2c72e..8df8444f 100644 --- a/runtime/vk_rec.c +++ b/runtime/vk_rec.c @@ -3,7 +3,8 @@ // call's arguments; its pointers point into the packet itself, so the recorder passes the payload to the driver as // it is. A packet never wraps: one that does not fit before the end is preceded by a pad. The producer publishes head // after each packet; the recorder makes the calls and advances tail, which the producer waits on when the ring is -// full and in rec_drain. While a part is recorded the recorder spins on head; idle, it sleeps on an event. +// full and in rec_drain. While a part is recorded the recorder spins on head; idle, it sleeps on an event. Packets also +// begin, end and submit the parts (rec_begin, rec_end_cb, rec_submit), so each part stays on the recorder throughout. // Design: Savage Wind Recomp #189 and LAS Recomp runtime/nvn/vulkan/record.c (https://github.com/peiikai/LASRecomp // @ a4b53a3, MIT, Copyright (c) 2026 Peiikai); the code is ours. #define VK_REC_IMPL @@ -22,8 +23,12 @@ static volatile uint64_t head, tail; // words published by the producer / cons static volatile int32_t sleeping; // the recorder waits for wake_ev static PlatEvent wake_ev; // auto-reset static uint64_t put_h; // producer: words written (published up to head) -static VkCommandBuffer rec_cb; // the part being recorded (set while the recorder is idle) +static VkCommandBuffer rec_cb; // producer: the part being recorded, whose vkCmd* become packets +static VkCommandBuffer r_cb; // recorder: the command buffer its packets go into (set by a BeginCb packet) static int rec_live; // producer: rec_begin started the recorder for rec_cb +static int end_mode = -1; // TOTK_REC_END (rec_own) +static int sub_pending; // producer: the recorder ended a part that rec_submit has not handed over yet +static volatile VkResult r_sub_res; // recorder: the result of its last vkQueueSubmit2, read after the drain static PlatThread *rec_thread; static int rec_prio; // the submitting thread's priority, which the recorder takes static int rec_chk; // TOTK_REC_CHECK (rec_check_handle) @@ -32,7 +37,7 @@ static volatile uint64_t part_h0; // put_h at rec_begin: where the packets static volatile uint64_t part_fence; // the value of the part being recorded (rec_begin) // Producer statistics, logged by rec_stats_log. -static struct { uint64_t packets, words, drains, fallbacks, drain_ns, full_waits, parts, parts_on; } RS_; +static struct { uint64_t packets, words, drains, fallbacks, drain_ns, full_waits, parts, parts_on, submits; } RS_; enum { R_PAD, R_BeginQuery, R_BeginRendering, R_BindDescriptorSets, R_BindIndexBuffer, R_BindPipeline, R_BindVertexBuffers2, @@ -44,7 +49,7 @@ enum { R_SetRasterizerDiscardEnable, R_SetScissorWithCount, R_SetStencilCompareMask, R_SetStencilOp, R_SetStencilReference, R_SetStencilTestEnable, R_SetStencilWriteMask, R_SetViewportWithCount, R_WriteTimestamp, R_WriteTimestamp2, R_PushTpl, R_Push, R_VertexInput, R_Bool, R_PolygonMode, R_BlendEnable, R_BlendEquation, R_WriteMask, R_EndCb, - R_PassMark, + R_PassMark, R_BeginCb, R_Submit, }; static uint64_t now_ns(void) { return plat_now_ns(); } @@ -135,7 +140,23 @@ void rec_drain(void) { RS_.drains++, RS_.drain_ns += now_ns() - t0; } -void rec_begin(VkCommandBuffer cb, uint64_t fence) { +static int rec_own(void) { + if (end_mode < 0) end_mode = rt_knob("REC_END", "1"); + return end_mode; +} + +// vkBeginCommandBuffer (one submission) and the breadcrumb, on the thread that records cb. +static void begin_now(VkCommandBuffer cb, int crumb, uint64_t n) { + VkCommandBufferBeginInfo bi = {.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, + .flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT}; + vkBeginCommandBuffer(cb, &bi); + if (crumb) vk_crumb(cb, crumb, n); +} + +// The command buffer the following packets go into; begin: the recorder begins it (TOTK_REC_END=1). +typedef struct { VkCommandBuffer cb; int begin, crumb; uint64_t n; } A_BeginCb; + +void rec_begin(VkCommandBuffer cb, uint64_t fence, int crumb, uint64_t n) { static int mode = -1; if (mode < 0) { // Default off on AMD GPUs. With the recorder those drivers lost the device or hung the GPU at startup on @@ -168,11 +189,21 @@ void rec_begin(VkCommandBuffer cb, uint64_t fence) { } } RS_.parts++; - if (!RT_KNOB_ON(mode)) return; + int on = RT_KNOB_ON(mode), own = on && rec_own(); + if (!own) { + // The submitting thread begins cb. The part pool is the recorder's while it has packets (an A/B switch + // between parts): it finishes them first. + rec_drain(); + begin_now(cb, crumb, n); + if (!on) return; + } RS_.parts_on++; - rec_cb = cb; // the recorder is idle: rec_end drained it __atomic_store_n(&part_h0, put_h, __ATOMIC_RELEASE); __atomic_store_n(&part_fence, fence, __ATOMIC_RELEASE); + A_BeginCb *a = put(R_BeginCb, sizeof *a); + *a = (A_BeginCb){cb, own, crumb, n}; + pub(); + rec_cb = cb; rec_live = 1; } @@ -182,23 +213,28 @@ void rec_end(void) { rec_live = 0; } -// TOTK_REC_END (default 1): the thread that recorded the part also ends it, after the breadcrumb crumb (kind 0: none) -// that vk_sync.c writes at the end of a submission. On an RX 6700 XT (driver 26.10.1, issue #5) parts the recorder -// recorded and the submitting thread ended broke when a submission was split (vk_sync.c vk_sync_split): the GPU hung -// or the scene turned green, then white. The handoff in rec_drain orders the two threads, so Vulkan allows the old -// split; the driver did not cope with it. TOTK_REC_END=0 brings the old split back. +// TOTK_REC_END (default 1): a part the recorder records is the recorder's from its begin to its submission: it begins +// it, makes every command, ends it after the breadcrumb crumb (kind 0: none) that vk_sync.c writes at the end of a +// submission, and submits it. The submitting thread touches the part pool only while the recorder is idle. That is +// LAS Recomp's and Savage Wind Recomp's model (runtime/nvn/vulkan/record.h: command buffers allocated once, then only +// the recorder begins, records, ends and submits them). Before, the submitting thread began and submitted parts the +// recorder recorded and, before that, also ended them. The handoff in rec_drain orders the two threads, so Vulkan +// allows that split, but on an RX 6700 XT (driver 26.10.1, issue #5) parts ended by the other thread broke when a +// submission was split (vk_sync.c vk_sync_split): the GPU hung or the scene turned green, then white. Moving the end +// fixed that; begin and submit followed. TOTK_REC_END=0 brings the old split back: the submitting thread begins, +// ends and submits every part. // The line is written only when a part was recorded by the recorder: with the recorder off (the default on AMD) the // switch changes nothing, and reports read an unconditional line as proof that the recorder was running (#315). typedef struct { int kind; uint64_t n; } A_EndCb; int rec_end_cb(VkCommandBuffer cb, int crumb, uint64_t n) { - static int mode = -1, said; - if (mode < 0) mode = rt_knob("REC_END", "1"); + static int said; + int own = rec_own(); if (rec_live && cb == rec_cb && !said) { said = 1; - rt_log("vk_rec: a part the recorder records is ended by %s (TOTK_REC_END=%d)", - mode ? "the recorder" : "the submitting thread", mode); + rt_log("vk_rec: a part the recorder records is begun, ended and submitted by %s (TOTK_REC_END=%d)", + own ? "the recorder" : "the submitting thread", own); } - if (!rec_live || cb != rec_cb || !mode) { + if (!rec_live || cb != rec_cb || !own) { rec_end(); if (crumb) vk_crumb(cb, crumb, n); return 0; @@ -206,18 +242,53 @@ int rec_end_cb(VkCommandBuffer cb, int crumb, uint64_t n) { A_EndCb *a = put(R_EndCb, sizeof *a); *a = (A_EndCb){crumb, n}; pub(); - rec_drain(); rec_live = 0; + sub_pending = 1; return 1; } +// At most two command buffers and one signal: the shape vk_sync.c submits. +typedef struct { uint32_t ncb, nsig; VkCommandBufferSubmitInfo cbi[2]; VkSemaphoreSubmitInfo sig; } A_Submit; +VkResult rec_submit(const VkSubmitInfo2 *si) { + if (!sub_pending) { + vk_queue_lock(); + VkResult r = vkQueueSubmit2(vk_ctx()->gfx_queue, 1, si, VK_NULL_HANDLE); + vk_queue_unlock(); + return r; + } + sub_pending = 0; + if (si->commandBufferInfoCount > 2 || si->signalSemaphoreInfoCount > 1 || si->waitSemaphoreInfoCount || si->pNext) + rt_fatal("vk_rec: a submission of %u command buffers, %u waits and %u signals the recorder cannot carry", + si->commandBufferInfoCount, si->waitSemaphoreInfoCount, si->signalSemaphoreInfoCount); + A_Submit *a = put(R_Submit, sizeof *a); + memset(a, 0, sizeof *a); + a->ncb = si->commandBufferInfoCount, a->nsig = si->signalSemaphoreInfoCount; + for (uint32_t i = 0; i < a->ncb; i++) a->cbi[i] = si->pCommandBufferInfos[i]; + if (a->nsig) a->sig = si->pSignalSemaphoreInfos[0]; + pub(); + // Waited for here, so the queue holds every earlier submission when the caller goes on, as with the recorder off: + // the present, uploads, readbacks and device waits on other threads rely on that order. + rec_drain(); + RS_.submits++; + return r_sub_res; +} + +static void submit_run(const A_Submit *a) { + VkSubmitInfo2 si = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO_2, .commandBufferInfoCount = a->ncb, + .pCommandBufferInfos = a->cbi, .signalSemaphoreInfoCount = a->nsig, + .pSignalSemaphoreInfos = a->nsig ? &a->sig : NULL}; + vk_queue_lock(); + r_sub_res = vkQueueSubmit2(vk_ctx()->gfx_queue, 1, &si, VK_NULL_HANDLE); + vk_queue_unlock(); +} + void rec_stats_log(void) { if (!(rt_trace & RT_TRACE_PERF) || !RS_.parts) return; rt_log("vk_rec: %llu of %llu parts recorded by the recorder: %llu packets (%.1f MiB), %llu drains (%llu for a call " - "without a packet) waited %.2f ms in all, ring full %llu times", + "without a packet) waited %.2f ms in all, ring full %llu times, %llu submissions made by the recorder", (unsigned long long)RS_.parts_on, (unsigned long long)RS_.parts, (unsigned long long)RS_.packets, RS_.words * 8 / 1048576.0, (unsigned long long)RS_.drains, (unsigned long long)RS_.fallbacks, - RS_.drain_ns / 1e6, (unsigned long long)RS_.full_waits); + RS_.drain_ns / 1e6, (unsigned long long)RS_.full_waits, (unsigned long long)RS_.submits); memset(&RS_, 0, sizeof RS_); } @@ -642,7 +713,14 @@ TARGET_CMD(rec_write_mask, R_WriteMask, PFN_vkCmdSetColorWriteMaskEXT, VkColorCo // ---- the recorder's side static void exec_packet(uint32_t op, void *p) { - VkCommandBuffer cb = rec_cb; + if (op == R_BeginCb) { + A_BeginCb *a = p; + r_cb = a->cb; + if (a->begin) begin_now(r_cb, a->crumb, a->n); + return; + } + if (op == R_Submit) { submit_run(p); return; } + VkCommandBuffer cb = r_cb; switch (op) { #define F(name, ...) case R_##name: { A_##name *a = p; vkCmd##name(cb, __VA_ARGS__); break; } F(BeginQuery, a->p, a->q, a->f) diff --git a/runtime/vk_rec.h b/runtime/vk_rec.h index feddc707..bbceff90 100644 --- a/runtime/vk_rec.h +++ b/runtime/vk_rec.h @@ -1,11 +1,12 @@ -// The recorder thread (vk_rec.c): the Vulkan calls the replay makes into its command buffer are made by a thread of -// their own. vk_sync.c brackets each part of a submission with rec_begin (after vkBeginCommandBuffer) and rec_end -// (before vkEndCommandBuffer). In between, every vkCmd* on that command buffer copies its arguments into a packet of a -// ring, in call order, and the recorder makes the real call in that order; calls into any other command buffer (uploads, -// the window, probes) are made at once. A call the ring has no packet for (a structure with a pNext chain) waits for -// the recorder to empty the ring and is then made at once on the calling thread. rec_end returns once the recorder made -// every call. vk_sync.c ends a part through rec_end_cb: the recorder makes the last calls into a part it recorded, -// vkEndCommandBuffer included; submitting and the command pool stay on the submitting thread. +// The recorder thread (vk_rec.c): the Vulkan calls the replay makes into its command buffers are made by a thread of +// their own. vk_sync.c begins each part of a submission (and the mirror prologue) with rec_begin, ends it with +// rec_end_cb and submits it with rec_submit. In between, every vkCmd* on that command buffer copies its arguments +// into a packet of a ring, in call order, and the recorder makes the real call in that order; calls into any other +// command buffer (uploads, the window, probes) are made at once. A call the ring has no packet for (a structure with a +// pNext chain) waits for the recorder to empty the ring and is then made at once on the calling thread. With +// TOTK_REC_END=1 (default) the recorder also begins, ends and submits the parts it records, so a part's command buffer +// never changes threads; rec_submit returns once the recorder submitted it. The submitting thread allocates from the +// part pool only while the recorder is idle (vk_sync.c claim_slot). // // Every vkCmd* the runtime calls through vk.h goes through the rc_* wrappers below (the macros at the end); the // extension commands called through function pointers have rec_* wrappers of their own. TOTK_VK_RECORDER=0 makes every @@ -15,11 +16,16 @@ // MIT, Copyright (c) 2026 Peiikai): the driver's work behind the commands depends on nothing the replay decides next. #pragma once -void rec_begin(VkCommandBuffer cb, uint64_t fence); // the submitting thread, sub_lock held; fence: the part's value +// The submitting thread, sub_lock held: vkBeginCommandBuffer (one submission) on cb and the breadcrumb crumb (0: none, +// vk_crumbs.c) with number n, made by the recorder when it records the part, else at once; fence: the part's value. +void rec_begin(VkCommandBuffer cb, uint64_t fence, int crumb, uint64_t n); void rec_end(void); // waits for the recorder; no-op when rec_begin did not start it -// The end of part cb: the breadcrumb crumb (0: none, vk_crumbs.c), then vkEndCommandBuffer, both made by the recorder -// when it records cb, and the wait for it (1); otherwise rec_end and the breadcrumb, and the caller ends cb (0). +// The end of part cb: the breadcrumb crumb, then vkEndCommandBuffer, both queued for the recorder when it records cb +// (1: rec_submit hands the part over); otherwise rec_end and the breadcrumb, and the caller ends cb (0). int rec_end_cb(VkCommandBuffer cb, int crumb, uint64_t n); +// vkQueueSubmit2 of si (at most two command buffers, one signal, no wait, no fence) under vk_queue_lock: by the +// recorder after a part it ended, waited for, else at once. +VkResult rec_submit(const VkSubmitInfo2 *si); void rec_drain(void); // waits until the recorder made every call queued so far void rec_stats_log(void); // TOTK_TRACE=perf: packets, bytes, drains and waits since the last call // TOTK_REC_CHECK=1 (debug, recorder on): vk_memory.c reports each object it defers (destroy 0, stamped tl) or destroys diff --git a/runtime/vk_sync.c b/runtime/vk_sync.c index ae9ee33b..e3162d12 100644 --- a/runtime/vk_sync.c +++ b/runtime/vk_sync.c @@ -328,6 +328,7 @@ static int claim_slot(VkCtx *c, uint64_t fence) { slots[i].fence = fence; return i; } + rec_drain(); // the part pool is the recorder's while it has packets (vk_rec.h): it is idle for the allocation CmdSlot *n = realloc(slots, sizeof *n * (size_t)(nslots + 1)); if (!n) rt_fatal("vk: out of memory (command buffers)"); slots = n; @@ -353,13 +354,11 @@ static VkCommandBuffer prologue(VkCtx *c, uint64_t fence) { if (!vk_mem_mirror_pending()) return VK_NULL_HANDLE; int i = claim_slot(c, fence); // before reading slots: the claim may move it VkCommandBuffer cb = slots[i].cb; - VkCommandBufferBeginInfo bi = {.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, - .flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT}; - vkBeginCommandBuffer(cb, &bi); + rec_begin(cb, fence, 0, 0); // a command buffer of the part pool: the recorder's like the parts int pair = ts_open(cb); vk_mem_mirror_prologue(cb); ts_close(cb, pair, fence); - vkEndCommandBuffer(cb); + if (!rec_end_cb(cb, 0, 0)) vkEndCommandBuffer(cb); return cb; } @@ -375,21 +374,15 @@ VkCommandBuffer vk_sync_split(void) { VkSubmitInfo2 si = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO_2, .commandBufferInfoCount = pro ? 2 : 1, .pCommandBufferInfos = pro ? cbi : cbi + 1}; ts_submitted(); - vk_queue_lock(); - VkResult r = vkQueueSubmit2(c->gfx_queue, 1, &si, VK_NULL_HANDLE); - vk_queue_unlock(); + VkResult r = rec_submit(&si); // by the recorder when it recorded the part if (r != VK_SUCCESS) { vk_device_fault_report(r); rt_fatal("vk: vkQueueSubmit2 of a part of value %llu failed (%d)", (unsigned long long)cur_fence, (int)r); } vk_mem_note_submit(cur_fence); // objects destroyed from here on may be in use by the part just submitted CmdSlot *s = take_slot(c, cur_fence); // the submitted part keeps its slot until the value is reached - VkCommandBufferBeginInfo bi = {.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, - .flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT}; - vkBeginCommandBuffer(s->cb, &bi); - vk_crumb(s->cb, VK_CRUMB_SUBMIT, cur_fence); + rec_begin(s->cb, cur_fence, VK_CRUMB_SUBMIT, cur_fence); ts_begin(s->cb); - rec_begin(s->cb, cur_fence); return s->cb; } @@ -412,13 +405,9 @@ uint64_t vk_sync_submit(const uint64_t *handles, int n) { uint64_t fence = __atomic_load_n(&issued, __ATOMIC_ACQUIRE) + 1; cur_fence = fence; CmdSlot *s = take_slot(c, fence); - VkCommandBufferBeginInfo bi = {.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, - .flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT}; uint64_t t0 = perf_begin(); - vkBeginCommandBuffer(s->cb, &bi); - vk_crumb(s->cb, VK_CRUMB_SUBMIT, fence); + rec_begin(s->cb, fence, VK_CRUMB_SUBMIT, fence); // begun by the recorder when it records the part ts_begin(s->cb); - rec_begin(s->cb, fence); int recorded = vk_exec_record(s->cb, handles, n, fence); s = &slots[cur_slot]; // the last part (vk_sync_split) if (recorded) ts_end(s->cb, fence); @@ -439,9 +428,7 @@ uint64_t vk_sync_submit(const uint64_t *handles, int n) { .pCommandBufferInfos = pro ? cbi : cbi + 1, .signalSemaphoreInfoCount = 1, .pSignalSemaphoreInfos = &sig}; ts_submitted(); - vk_queue_lock(); - VkResult r = vkQueueSubmit2(c->gfx_queue, 1, &si, VK_NULL_HANDLE); - vk_queue_unlock(); + VkResult r = rec_submit(&si); // by the recorder when it recorded the part if (r != VK_SUCCESS) { vk_device_fault_report(r); rt_fatal("vk: vkQueueSubmit2 of value %llu failed (%d)", (unsigned long long)fence, (int)r); From d850c2116db9fb94e736174c9bf6c9bd05a73eb5 Mon Sep 17 00:00:00 2001 From: itaskaev2 <165683571+itaskaev2@users.noreply.github.com> Date: Wed, 4 Nov 2026 21:55:12 +0100 Subject: [PATCH 4/5] Count play time across runs and show it on the Game tab A host thread adds each second in which the window has the focus and the game presents frames, keeps the total in /playtime.txt (every 30 s and at exit) and the Game tab of the settings menu shows it with this run's share. Texts in English, Russian, Brazilian Portuguese and Simplified Chinese. Closes #720. Co-Authored-By: Claude Opus 5.5 --- README.md | 1 + runtime/boot.c | 1 + runtime/gfx_present.c | 1 + runtime/host.h | 3 ++ runtime/main.c | 1 + runtime/playtime.c | 121 ++++++++++++++++++++++++++++++++++++++++++ runtime/svc.c | 1 + runtime/ui/menu.cpp | 15 ++++++ runtime/ui_text.c | 16 ++++++ runtime/ui_text.h | 3 ++ 10 files changed, 163 insertions(+) create mode 100644 runtime/playtime.c diff --git a/README.md b/README.md index 9b0c3f9d..c0ffc147 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,7 @@ Report problems as issues, following [CONTRIBUTING.md](CONTRIBUTING.md). - 16:10 (Steam Deck, handhelds), 3:2 (Surface, Framework), 21:9 and 32:9 (ultrawide) game resolutions, with the camera and the interface in that shape - Anisotropic texture filtering up to 16x, for sharp ground into the distance - In-game settings menu (F1) and performance overlay (F10) +- Play time across runs on the Game tab of the settings menu, kept in `playtime.txt` next to the saves - Discord Rich Presence ("Playing TotK Recomp" with the play time), off by default, own application id supported - Setup and in-game menu in English, Russian, Brazilian Portuguese and Simplified Chinese - Keyboard, mouse and gamepad with remapping, rumble and gyro diff --git a/runtime/boot.c b/runtime/boot.c index 992bf33b..d1c1c1b1 100644 --- a/runtime/boot.c +++ b/runtime/boot.c @@ -307,6 +307,7 @@ void rt_frame_presented(const tex_t *t, uint64_t n) { fflush(stderr); fs_save_flush(); vk_pipeline_save(); // no-op without the Vulkan back end + playtime_save(); prof_write(); fflush(stderr); plat_exit(0); diff --git a/runtime/gfx_present.c b/runtime/gfx_present.c index ea0c72c9..472acb41 100644 --- a/runtime/gfx_present.c +++ b/runtime/gfx_present.c @@ -72,6 +72,7 @@ void gfx_window_closed(void) { rt_log("gfx: window closed; %s", restart ? "restarting" : "exiting"); fs_save_flush(); vk_pipeline_save(); + playtime_save(); prof_write(); // plat_exit runs no atexit handlers if (restart) { plat_setenv("TOTK_REEXEC", NULL); // the new process counts its own address-space retries (memory.c) diff --git a/runtime/host.h b/runtime/host.h index 065256a2..1cf399a3 100644 --- a/runtime/host.h +++ b/runtime/host.h @@ -316,6 +316,9 @@ int probe_cfg(void); // cfg_probe.c int probe_dpi(void); // dpi_probe_win.c (Windows only) int probe_mods(void); // mods_probe.c void discord_init(void); // discord_rpc.c: the Rich Presence thread, game runs only +void playtime_init(void); // playtime.c: the play time thread, game runs only +void playtime_save(void); // playtime.c: writes /playtime.txt; at exit +int playtime_get(uint64_t *total_s, uint64_t *session_s); // playtime.c: all runs and this one; 0 while not counted int probe_discord(void); // discord_rpc.c int probe_loader(void), probe_kernel(void), probe_ipc(void), probe_services(void), probe_fs(void); int probe_gfx_abi(void); // gfx_abi_probe.c diff --git a/runtime/main.c b/runtime/main.c index 720896f8..3cf47de3 100644 --- a/runtime/main.c +++ b/runtime/main.c @@ -553,6 +553,7 @@ int main(int argc, char **argv) { if (!mode) { cfg.boot = cfg.seconds > 0; // --seconds starts the game like --boot, with its checkpoints discord_init(); // the Rich Presence thread (discord_rpc.c); it reads totk.ini itself and idles while off + playtime_init(); // the play time thread (playtime.c): the Game tab's play time across runs rt_boot(); return 0; } diff --git a/runtime/playtime.c b/runtime/playtime.c new file mode 100644 index 00000000..3d11d043 --- /dev/null +++ b/runtime/playtime.c @@ -0,0 +1,121 @@ +// Play time: how long the player has spent in the game, across runs, shown on the Game tab of the settings menu +// (ui/menu.cpp tab_game). The game itself keeps no play time a player can see, and the Switch's own play log is not +// there to read, so the port counts it. +// +// One host thread (it never holds a virtual core, as discord_rpc.c's) wakes every second and adds the time since its +// last wake when the window has the focus (gfx_window_focused) and the game presented at least one frame meanwhile +// (gfx_stats). So the shader prewarm before the first frame, a window left in the background and a hung game do not +// count; loading screens and the game's own menus do, as they present frames. One wake adds at most TICK_CAP_MS, so a +// machine that slept with the game open adds two seconds, not the night. +// +// The total lives in /playtime.txt, next to account.uid and the saves, so it stays with them when the +// install is updated or moved. It is plain text, "total_seconds ", written atomically (plat_file_write_atomic) +// every SAVE_EVERY_MS while it grows and once more when the game exits (playtime_save from boot.c, svc.c and +// gfx_present.c, next to prof_write); a crash loses at most SAVE_EVERY_MS. A file that does not read as that is kept +// as playtime.txt.bad and the count starts again from zero. +#include +#include +#include +#include +#include + +#include "gfx.h" +#include "host.h" +#include "platform/platform.h" + +#define TICK_MS 1000 +#define TICK_CAP_MS 2000 +#define SAVE_EVERY_MS 30000 +#define TOTAL_CAP_S (100ull * 365 * 24 * 3600) // a hundred years: anything larger is a damaged file + +static PlatMutex lock = PLAT_MUTEX_INIT; // the totals and the file +static uint64_t total_ms, session_ms, saved_ms; +static int loaded; // 1 once the file was read (or found missing), -1 when there is no profile +static char path[1100]; + +// Holding lock. +static void load(void) { + if (loaded) return; + const char *dir = rt_config()->profile; + if (!dir || !*dir || snprintf(path, sizeof path, "%s/playtime.txt", dir) >= (int)sizeof path) { + loaded = -1; + rt_log("playtime: no profile directory; play time is not counted"); + return; + } + loaded = 1; + uint64_t n = 0; + uint8_t *b = plat_file_read(path, &n); + if (!b) { + rt_log("playtime: no %s yet; counting from zero", path); + return; + } + char text[64] = {0}; + memcpy(text, b, n < sizeof text - 1 ? n : sizeof text - 1); + free(b); + unsigned long long s = 0; + char tail = 0; + if (sscanf(text, "total_seconds %llu%c", &s, &tail) >= 1 && (!tail || tail == '\n' || tail == '\r') && + s <= TOTAL_CAP_S) { + total_ms = saved_ms = (uint64_t)s * 1000; + rt_log("playtime: %llu h %02llu min played before this run (%s)", s / 3600, s / 60 % 60, path); + return; + } + char bad[1110]; + snprintf(bad, sizeof bad, "%s.bad", path); + int kept = plat_file_write_atomic(bad, text, strlen(text)); + rt_log("playtime: %s does not read as \"total_seconds \"; %s, counting from zero", path, + kept ? "kept as playtime.txt.bad" : "could not keep a copy"); +} + +// Holding lock. +static void save_locked(void) { + if (loaded != 1 || total_ms / 1000 == saved_ms / 1000) return; + char text[48]; + int n = snprintf(text, sizeof text, "total_seconds %" PRIu64 "\n", total_ms / 1000); + if (plat_file_write_atomic(path, text, (size_t)n)) saved_ms = total_ms; + else rt_log("playtime: cannot write %s", path); +} + +void playtime_save(void) { + plat_mutex_lock(&lock); + save_locked(); + plat_mutex_unlock(&lock); +} + +int playtime_get(uint64_t *total_s, uint64_t *session_s) { + plat_mutex_lock(&lock); + int ok = loaded == 1; + *total_s = total_ms / 1000; + *session_s = session_ms / 1000; + plat_mutex_unlock(&lock); + return ok; +} + +static void playtime_main(void *arg) { + (void)arg; + plat_thread_priority(PLAT_PRIO_BELOW_NORMAL); + plat_mutex_lock(&lock); + load(); + int on = loaded == 1; + plat_mutex_unlock(&lock); + if (!on) return; + uint64_t last = plat_now_ms(), last_save = last, frames = gfx_stats().frames; + for (;;) { + plat_sleep_ms(TICK_MS); + uint64_t now = plat_now_ms(), f = gfx_stats().frames, d = now - last; + int playing = f != frames && gfx_window_focused(); + last = now, frames = f; + if (d > TICK_CAP_MS) d = TICK_CAP_MS; + plat_mutex_lock(&lock); + if (playing) total_ms += d, session_ms += d; + if (now - last_save >= SAVE_EVERY_MS) save_locked(), last_save = now; + plat_mutex_unlock(&lock); + } +} + +void playtime_init(void) { + static int32_t started; + if (plat_xchg32(&started, 1)) return; + if (!plat_thread_spawn(playtime_main, NULL, "playtime", 0)) + rt_log("playtime: cannot start its thread; play time is not counted"); +} diff --git a/runtime/svc.c b/runtime/svc.c index f257dd0a..27cdad19 100644 --- a/runtime/svc.c +++ b/runtime/svc.c @@ -216,6 +216,7 @@ static void svc_body(Cpu *c, uint32_t imm, uint64_t pc) { rt_log("guest ExitProcess at %s; exiting", ldr_describe(pc, at, sizeof at)); fs_save_flush(); vk_pipeline_save(); + playtime_save(); prof_write(); fflush(NULL); if (svc_test_exit_hook) return svc_test_exit_hook(0); diff --git a/runtime/ui/menu.cpp b/runtime/ui/menu.cpp index d4b60442..bd500fa3 100644 --- a/runtime/ui/menu.cpp +++ b/runtime/ui/menu.cpp @@ -57,6 +57,7 @@ extern const int rt_game_version; extern const char rt_port_version[]; void cheats_frame(uint64_t frame); // cheats.c bool tab_cheats(float scale); // ui/cheats_tab.cpp; true: restart into the rebuild +int playtime_get(uint64_t *total_s, uint64_t *session_s); // playtime.c } // Roboto Medium (Apache-2.0, ui/fonts/LICENSE-Roboto.txt) with Latin and Cyrillic glyphs. @@ -698,6 +699,19 @@ static void tab_screen(float scale) { opt_end(); } +// Play time across runs (playtime.c), read only. Hidden while it is not counted (no profile directory). +static void row_play_time(void) { + uint64_t total, session; + if (!playtime_get(&total, &session)) return; + char value[64], run[64], note[96]; + snprintf(value, sizeof value, menu_text(UI_MENU_PLAY_TIME_HM), (unsigned long long)(total / 3600), + (unsigned)(total / 60 % 60)); + snprintf(run, sizeof run, menu_text(UI_MENU_PLAY_TIME_HM), (unsigned long long)(session / 3600), + (unsigned)(session / 60 % 60)); + snprintf(note, sizeof note, menu_text(UI_MENU_PLAY_TIME_SESSION), run); + opt_text_row("##play_time", menu_text(UI_MENU_PLAY_TIME), value, note, menu_text(UI_MENU_PLAY_TIME_DESC), false); +} + static void tab_game(float scale) { opt_begin("##game", scale); row_fps_limit(); @@ -706,6 +720,7 @@ static void tab_game(float scale) { static const UiTextId mode[] = {UI_MENU_MODE_DOCKED, UI_MENU_MODE_HANDHELD}; opt_choice("TOTK_OPERATION_MODE", UI_MENU_OPERATION_MODE, mode, 2, UI_MENU_OPERATION_MODE_DESC); opt_toggle("TOTK_ASSUME_FOCUS", UI_MENU_ASSUME_FOCUS, UI_MENU_ASSUME_FOCUS_DESC); + row_play_time(); opt_end(); } diff --git a/runtime/ui_text.c b/runtime/ui_text.c index ccb91d03..f4e033c5 100644 --- a/runtime/ui_text.c +++ b/runtime/ui_text.c @@ -192,6 +192,10 @@ static const char *const text_en[UI_TEXT_COUNT] = { "window loses focus, as on the Switch when Home is pressed; a load interrupted that " "way can stop for good.", + [UI_MENU_PLAY_TIME] = "Play time", + [UI_MENU_PLAY_TIME_DESC] = "Time in the game over all runs, counted while the game window has the focus and the game " + "draws frames. Kept in playtime.txt in the profile folder, next to the saves.", + [UI_MENU_PLAY_TIME_HM] = "%llu h %02u min", [UI_MENU_PLAY_TIME_SESSION] = "this run: %s", [UI_MENU_MODS_BUILTIN] = "Built into the port", [UI_MENU_MODS_INSTALLED] = "Installed mods", [UI_MENU_MODS_NONE] = "No mods yet. Unpack a mod into the mods folder, one folder per mod with its romfs folder " "inside, and restart the game. TKMM can export its merged mods into a folder there too.", @@ -662,6 +666,10 @@ static const char *const text_ru[UI_TEXT_COUNT] = { "когда окно теряет фокус, как на Switch при нажатии Home; прерванная так загрузка " "может остановиться насовсем.", + [UI_MENU_PLAY_TIME] = "Время в игре", + [UI_MENU_PLAY_TIME_DESC] = "Время в игре за все запуски. Считается, пока окно игры в фокусе и игра рисует кадры. " + "Хранится в playtime.txt в папке профиля, рядом с сохранениями.", + [UI_MENU_PLAY_TIME_HM] = "%llu ч %02u мин", [UI_MENU_PLAY_TIME_SESSION] = "в этом запуске: %s", [UI_MENU_MODS_BUILTIN] = "Встроенные в порт", [UI_MENU_MODS_INSTALLED] = "Установленные моды", [UI_MENU_MODS_NONE] = "Модов пока нет. Распакуйте мод в папку mods, каждый мод в свою папку с папкой romfs внутри, и " "перезапустите игру. TKMM тоже может выгрузить туда объединённые моды отдельной папкой.", @@ -983,6 +991,10 @@ static const char *const text_pt_br[UI_TEXT_COUNT] = { [UI_MENU_TAB_SCREEN] = "Tela", [UI_MENU_TAB_GAME] = "Jogo", [UI_MENU_TAB_SOUND] = "Som", [UI_MENU_TAB_CONTROLS] = "Controles", [UI_MENU_TAB_AMIIBO] = "Amiibo", [UI_MENU_TAB_MENU] = "Menu", [UI_MENU_TAB_ADVANCED] = "Avançado", [UI_MENU_TAB_MODS] = "Mods", + [UI_MENU_PLAY_TIME] = "Tempo de jogo", + [UI_MENU_PLAY_TIME_DESC] = "Tempo no jogo somando todas as sessões, contado enquanto a janela do jogo está em foco e o " + "jogo desenha quadros. Fica em playtime.txt na pasta do perfil, junto dos saves.", + [UI_MENU_PLAY_TIME_HM] = "%llu h %02u min", [UI_MENU_PLAY_TIME_SESSION] = "nesta sessão: %s", [UI_MENU_MODS_BUILTIN] = "Embutidos no port", [UI_MENU_MODS_INSTALLED] = "Mods instalados", [UI_MENU_MODS_NONE] = "Nenhum mod ainda. Descompacte um mod na pasta mods, uma pasta por mod com a pasta romfs dentro, " "e reinicie o jogo. O TKMM também pode exportar os mods mesclados para uma pasta ali.", @@ -1492,6 +1504,10 @@ static const char *const text_zh_hans[UI_TEXT_COUNT] = { [UI_MENU_TAB_MENU] = "菜单", [UI_MENU_TAB_ADVANCED] = "高级", [UI_MENU_TAB_MODS] = "模组", + [UI_MENU_PLAY_TIME] = "游戏时间", + [UI_MENU_PLAY_TIME_DESC] = "所有运行累计的游戏时间,仅在游戏窗口处于焦点且游戏正在绘制画面时计时。" + "保存在配置文件夹的 playtime.txt 中,与存档放在一起。", + [UI_MENU_PLAY_TIME_HM] = "%llu 小时 %02u 分钟", [UI_MENU_PLAY_TIME_SESSION] = "本次运行:%s", [UI_MENU_MODS_BUILTIN] = "移植版内置", [UI_MENU_MODS_INSTALLED] = "已安装的模组", [UI_MENU_MODS_NONE] = "还没有模组。把模组解压到 mods 文件夹,每个模组一个文件夹,里面放 romfs 文件夹,然后重启游戏。" "TKMM 也可以把合并后的模组导出到那里的一个文件夹。", diff --git a/runtime/ui_text.h b/runtime/ui_text.h index deca0f7f..59e277b4 100644 --- a/runtime/ui_text.h +++ b/runtime/ui_text.h @@ -70,6 +70,9 @@ typedef enum { UI_MENU_GAME_LANG, UI_MENU_GAME_LANG_DESC, UI_MENU_GAME_LANG_AUTO, UI_MENU_OPERATION_MODE, UI_MENU_OPERATION_MODE_DESC, UI_MENU_MODE_DOCKED, UI_MENU_MODE_HANDHELD, UI_MENU_ASSUME_FOCUS, UI_MENU_ASSUME_FOCUS_DESC, + UI_MENU_PLAY_TIME, UI_MENU_PLAY_TIME_DESC, + UI_MENU_PLAY_TIME_HM, // hours and minutes: "%llu h %02u min" (unsigned long long hours, unsigned minutes) + UI_MENU_PLAY_TIME_SESSION, // note under the total: "this run: %s" (the hours and minutes above) // Mods: the port's own patches and the player's mods folder (mods.c) UI_MENU_MODS_BUILTIN, UI_MENU_MODS_INSTALLED, From 4645b8a0d067b2dc59c2806ce47de470a8a660a8 Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Fri, 6 Nov 2026 10:55:42 +0300 Subject: [PATCH 5/5] Leave scripted runs out of the play time, keep a damaged file whole, and test the file with --probe-playtime. A --seconds run (benches, menu shots, input scripts) reads the total so the Game tab shows it, but starts no counting thread. The file is now read once at start instead of on the thread. A playtime.txt with a UTF-8 byte order mark reads as valid, and a damaged one is kept whole as playtime.txt.bad instead of its first 63 bytes. --probe-playtime checks the parser, a missing, valid and damaged file, the 30 s save and the 2 s cap of one tick. Co-Authored-By: Claude Opus 5.5 --- runtime/host.h | 3 +- runtime/main.c | 4 +- runtime/playtime.c | 143 +++++++++++++++++++++++++++++++++++++-------- 3 files changed, 123 insertions(+), 27 deletions(-) diff --git a/runtime/host.h b/runtime/host.h index 1cf399a3..21d0534f 100644 --- a/runtime/host.h +++ b/runtime/host.h @@ -318,7 +318,8 @@ int probe_mods(void); // mods_probe.c void discord_init(void); // discord_rpc.c: the Rich Presence thread, game runs only void playtime_init(void); // playtime.c: the play time thread, game runs only void playtime_save(void); // playtime.c: writes /playtime.txt; at exit -int playtime_get(uint64_t *total_s, uint64_t *session_s); // playtime.c: all runs and this one; 0 while not counted +int playtime_get(uint64_t *total_s, uint64_t *session_s); // playtime.c: all runs and this one; 0 without a profile +int probe_playtime(void); // playtime.c: the file, the ticks and the save int probe_discord(void); // discord_rpc.c int probe_loader(void), probe_kernel(void), probe_ipc(void), probe_services(void), probe_fs(void); int probe_gfx_abi(void); // gfx_abi_probe.c diff --git a/runtime/main.c b/runtime/main.c index 3cf47de3..49d227a0 100644 --- a/runtime/main.c +++ b/runtime/main.c @@ -7,6 +7,7 @@ // --probe-loader load and relocate the modules, check them against the measured 1.2.1 values // --probe-kernel, --probe-ipc, --probe-services, --probe-fs host probes // --probe-discord the Rich Presence IPC exchange; skips without a Discord client; no game files +// --probe-playtime the play time file, its ticks and saves (playtime.c); needs no game files // --probe-dpi the process DPI awareness (per-monitor v2) and the primary monitor's size; no game files // --probe-gfx-abi NVN object layouts against nvn.h's reserved sizes; needs no game files // --probe-nvn, --probe-nvn-tex, --probe-window, --probe-shader, --probe-vkmem, --probe-vkexec graphics probes @@ -463,7 +464,7 @@ static int probe_crash(int null) { static int usage(const char *argv0) { fprintf(stderr, "usage: %s [--exefs DIR] [--romfs DIR] [--profile DIR] [--selftest | --probe-loader | " - "--probe-cfg | --probe-dpi | --probe-mods | --probe-kernel | --probe-ipc | --probe-services | --probe-fs | --probe-gfx-abi | " + "--probe-cfg | --probe-dpi | --probe-mods | --probe-playtime | --probe-kernel | --probe-ipc | --probe-services | --probe-fs | --probe-gfx-abi | " "--probe-nvn | --probe-nvn-tex | --probe-window | --probe-shader | --probe-vkmem | " "--probe-vkexec | --probe-astc | --probe-movie | --probe-crash-call | --probe-crash-null | --probe-discord | " "--prepare-shaders | --warm-shaders | --boot |--seconds S [--shot PNG]]\n", argv0); @@ -567,6 +568,7 @@ int main(int argc, char **argv) { #endif if (!strcmp(mode, "--probe-mods")) return probe_mods(); if (!strcmp(mode, "--probe-discord")) return probe_discord(); + if (!strcmp(mode, "--probe-playtime")) return probe_playtime(); if (!strcmp(mode, "--probe-kernel")) return probe_kernel(); if (!strcmp(mode, "--probe-ipc")) return probe_ipc(); if (!strcmp(mode, "--probe-services")) return probe_services(); diff --git a/runtime/playtime.c b/runtime/playtime.c index 3d11d043..5b53700f 100644 --- a/runtime/playtime.c +++ b/runtime/playtime.c @@ -12,7 +12,7 @@ // install is updated or moved. It is plain text, "total_seconds ", written atomically (plat_file_write_atomic) // every SAVE_EVERY_MS while it grows and once more when the game exits (playtime_save from boot.c, svc.c and // gfx_present.c, next to prof_write); a crash loses at most SAVE_EVERY_MS. A file that does not read as that is kept -// as playtime.txt.bad and the count starts again from zero. +// as playtime.txt.bad and the count starts again from zero. A scripted run (--seconds) reads the total and adds nothing. #include #include #include @@ -33,36 +33,47 @@ static uint64_t total_ms, session_ms, saved_ms; static int loaded; // 1 once the file was read (or found missing), -1 when there is no profile static char path[1100]; -// Holding lock. -static void load(void) { +// The seconds of a playtime.txt: "total_seconds ", a line end or nothing after it, a UTF-8 byte order mark before it +// (an editor may add one). 1 when the file reads so and n is at most TOTAL_CAP_S. +static int parse(const uint8_t *b, uint64_t n, uint64_t *seconds) { + if (n >= 3 && !memcmp(b, "\xEF\xBB\xBF", 3)) b += 3, n -= 3; + char text[64] = {0}; + memcpy(text, b, n < sizeof text - 1 ? n : sizeof text - 1); + unsigned long long s = 0; + char tail = 0; + if (sscanf(text, "total_seconds %llu%c", &s, &tail) < 1 || (tail && tail != '\n' && tail != '\r') || + s > TOTAL_CAP_S) + return 0; + *seconds = s; + return 1; +} + +// Holding lock. dir: the profile directory. +static void load(const char *dir) { if (loaded) return; - const char *dir = rt_config()->profile; if (!dir || !*dir || snprintf(path, sizeof path, "%s/playtime.txt", dir) >= (int)sizeof path) { loaded = -1; rt_log("playtime: no profile directory; play time is not counted"); return; } loaded = 1; - uint64_t n = 0; + uint64_t n = 0, s = 0; uint8_t *b = plat_file_read(path, &n); if (!b) { rt_log("playtime: no %s yet; counting from zero", path); return; } - char text[64] = {0}; - memcpy(text, b, n < sizeof text - 1 ? n : sizeof text - 1); - free(b); - unsigned long long s = 0; - char tail = 0; - if (sscanf(text, "total_seconds %llu%c", &s, &tail) >= 1 && (!tail || tail == '\n' || tail == '\r') && - s <= TOTAL_CAP_S) { - total_ms = saved_ms = (uint64_t)s * 1000; - rt_log("playtime: %llu h %02llu min played before this run (%s)", s / 3600, s / 60 % 60, path); + if (parse(b, n, &s)) { + free(b); + total_ms = saved_ms = s * 1000; + rt_log("playtime: %llu h %02llu min played before this run (%s)", (unsigned long long)(s / 3600), + (unsigned long long)(s / 60 % 60), path); return; } char bad[1110]; snprintf(bad, sizeof bad, "%s.bad", path); - int kept = plat_file_write_atomic(bad, text, strlen(text)); + int kept = plat_file_write_atomic(bad, b, (size_t)n); // the whole file, for the player to recover the number + free(b); rt_log("playtime: %s does not read as \"total_seconds \"; %s, counting from zero", path, kept ? "kept as playtime.txt.bad" : "could not keep a copy"); } @@ -91,31 +102,113 @@ int playtime_get(uint64_t *total_s, uint64_t *session_s) { return ok; } +// Holding lock: the time since the last tick, d ms, when the game was played meanwhile; the file every SAVE_EVERY_MS. +static void tick_locked(uint64_t now, uint64_t d, int playing, uint64_t *last_save) { + if (d > TICK_CAP_MS) d = TICK_CAP_MS; + if (playing) total_ms += d, session_ms += d; + if (now - *last_save >= SAVE_EVERY_MS) save_locked(), *last_save = now; +} + static void playtime_main(void *arg) { (void)arg; plat_thread_priority(PLAT_PRIO_BELOW_NORMAL); - plat_mutex_lock(&lock); - load(); - int on = loaded == 1; - plat_mutex_unlock(&lock); - if (!on) return; uint64_t last = plat_now_ms(), last_save = last, frames = gfx_stats().frames; for (;;) { plat_sleep_ms(TICK_MS); - uint64_t now = plat_now_ms(), f = gfx_stats().frames, d = now - last; + uint64_t now = plat_now_ms(), f = gfx_stats().frames; int playing = f != frames && gfx_window_focused(); - last = now, frames = f; - if (d > TICK_CAP_MS) d = TICK_CAP_MS; plat_mutex_lock(&lock); - if (playing) total_ms += d, session_ms += d; - if (now - last_save >= SAVE_EVERY_MS) save_locked(), last_save = now; + tick_locked(now, now - last, playing, &last_save); plat_mutex_unlock(&lock); + last = now, frames = f; } } +// The file is read at once, so the Game tab shows the total from the first frame. A scripted run (--seconds: benches, +// menu shots, input scripts) shows it but adds nothing: those runs are not play. void playtime_init(void) { static int32_t started; if (plat_xchg32(&started, 1)) return; + plat_mutex_lock(&lock); + load(rt_config()->profile); + int on = loaded == 1; + plat_mutex_unlock(&lock); + if (!on) return; + if (rt_config()->seconds > 0) { + rt_log("playtime: a scripted run (--seconds); play time is shown, not counted"); + return; + } if (!plat_thread_spawn(playtime_main, NULL, "playtime", 0)) rt_log("playtime: cannot start its thread; play time is not counted"); } + +// --probe-playtime: the file's parsing, a missing, a valid and a damaged file, the periodic save and the tick's cap, in +// a directory next to totk.exe. Needs no game files. +static int fails; +#define CHECK(c, what) do { if (!(c)) { fails++; fprintf(stderr, "FAIL playtime: %s\n", what); } } while (0) + +static void reset(void) { total_ms = session_ms = saved_ms = 0, loaded = 0, path[0] = 0; } + +static int file_is(const char *p, const char *want) { + uint64_t n = 0; + uint8_t *b = plat_file_read(p, &n); + int ok = b && n == strlen(want) && !memcmp(b, want, n); + free(b); + return ok; +} + +int probe_playtime(void) { + uint64_t s = 0; + CHECK(parse((const uint8_t *)"total_seconds 42\n", 17, &s) && s == 42, "a line with LF"); + CHECK(parse((const uint8_t *)"total_seconds 42\r\n", 18, &s) && s == 42, "a line with CRLF"); + CHECK(parse((const uint8_t *)"total_seconds 42", 16, &s) && s == 42, "no line end"); + CHECK(parse((const uint8_t *)"\xEF\xBB\xBFtotal_seconds 7\n", 19, &s) && s == 7, "a byte order mark"); + CHECK(!parse((const uint8_t *)"total_seconds 42x", 17, &s), "a character after the number"); + CHECK(!parse((const uint8_t *)"total_seconds -1\n", 17, &s), "a negative number"); + CHECK(!parse((const uint8_t *)"total_seconds 3153600001\n", 25, &s), "over a hundred years"); + CHECK(!parse((const uint8_t *)"seconds 42\n", 11, &s), "another key"); + CHECK(!parse((const uint8_t *)"", 0, &s), "an empty file"); + + char dir[1024]; + if (!plat_exe_path(dir, sizeof dir)) return 1; + char *cut = plat_last_sep(dir); + if (!cut) return 1; + strcpy(cut + 1, "playtime-probe"); + plat_file_mkdir_p(dir); + char file[1100], bad[1110]; + snprintf(file, sizeof file, "%s/playtime.txt", dir); + snprintf(bad, sizeof bad, "%s.bad", file); + plat_file_delete(file); + plat_file_delete(bad); + + uint64_t total, session, last_save = 0; + reset(); + load(dir); + CHECK(playtime_get(&total, &session) && total == 0, "a missing file counts from zero"); + tick_locked(1000, 1000, 1, &last_save); + tick_locked(2000, 1000, 0, &last_save); + CHECK(plat_file_size(file) < 0, "nothing is written before SAVE_EVERY_MS"); + tick_locked(2000 + 3600000, 3600000, 1, &last_save); // a machine that slept: one tick adds TICK_CAP_MS, and saves + playtime_get(&total, &session); + CHECK(total == 3 && session == 3, "played ticks count, an idle one does not, a long one is capped"); + CHECK(file_is(file, "total_seconds 3\n"), "the save after SAVE_EVERY_MS writes the total"); + + reset(); + load(dir); + playtime_get(&total, &session); + CHECK(total == 3 && session == 0, "the next run reads the total back"); + + static const char damaged[] = "total_seconds twelve\n"; + plat_file_write_atomic(file, damaged, sizeof damaged - 1); + reset(); + load(dir); + playtime_get(&total, &session); + CHECK(total == 0, "a damaged file counts from zero"); + CHECK(file_is(bad, damaged), "a damaged file is kept whole as playtime.txt.bad"); + + plat_file_delete(file); + plat_file_delete(bad); + reset(); + fprintf(stderr, "playtime: %s (%d failures)\n", fails ? "FAILED" : "ok", fails); + return fails ? 1 : 0; +}