diff --git a/runtime/vk.h b/runtime/vk.h index 68096ff4..63e417fd 100644 --- a/runtime/vk.h +++ b/runtime/vk.h @@ -474,6 +474,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_census_log(uint64_t frame); // vk_memory.c vram_log: the live entries' memory by kind and format 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 a34f3c4b..e01515fc 100644 --- a/runtime/vk_memory.c +++ b/runtime/vk_memory.c @@ -2182,6 +2182,32 @@ static void heap_log(VkCtx *c, uint64_t frame, const VmaBudget *vb) { if (!j || !plat_thread_spawn(census_main, j, "mem census", 0)) free(j), plat_xchg32(&census_busy, 0); } +// After the vram line: the imported pools the GPU reached through a resolve (a binding, copy or texture placement) and +// those it never did, which hold host memory the driver counts in the host heap all the same (the 4.3 GiB imported +// at the title screen exceed the 3.9 GiB host heap of an integrated GPU with 16 GB of RAM); then the VRAM mirrors no +// submission synced for MIRROR_IDLE submissions, which a scene that needs the room could take back. +#define MIRROR_IDLE 120 +static void pool_census_log(uint64_t frame) { + uint64_t un = 0, ub = 0, nn = 0, nb = 0, mn = 0, mb = 0, in = 0, ib = 0; + uint32_t sub = __atomic_load_n(&msub, __ATOMIC_RELAXED); // the replay's; a log line may read it a submission late + plat_mutex_lock_shared(&pool_lock); + for (int i = 0; i < pool_index.n; i++) { + const PoolRec *r = pool_index.e[i].buf; + if (r->kind != POOL_IMPORT || !r->buf) continue; + uint64_t n = r->import_hi - r->import_lo; + if (__atomic_load_n(&r->used, __ATOMIC_RELAXED)) un++, ub += n; + else nn++, nb += n; + if (r->mstate != MS_ON) continue; + mn++, mb += n; + if (sub - r->acc_sub >= MIRROR_IDLE) in++, ib += n; + } + plat_mutex_unlock_shared(&pool_lock); + const double M = 1048576.0; + rt_log("mem: pools frame %llu: imported %llu %.0f MiB resolved by the GPU, %llu %.0f MiB never; mirrors %llu %.0f MiB, " + "idle for %d submissions %llu %.0f MiB", (unsigned long long)frame, (unsigned long long)un, ub / M, + (unsigned long long)nn, nb / M, (unsigned long long)mn, mb / M, MIRROR_IDLE, (unsigned long long)in, ib / M); +} + // With NX2NAVI_TRACE=perf every 600 frames: what holds device-local memory, to see what grows over a long session. // VMA's blocks and allocations on the device-local heaps, texture entries with their own memory, the VRAM mirrors and // the objects waiting for their last submission to retire. @@ -2204,6 +2230,8 @@ 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_census_log(frame); + pool_census_log(frame); heap_log(c, frame, vb); } diff --git a/runtime/vk_texture.c b/runtime/vk_texture.c index f4def2df..5d84b1c0 100644 --- a/runtime/vk_texture.c +++ b/runtime/vk_texture.c @@ -43,6 +43,8 @@ typedef struct Tex { VmaAllocation bound_to; // the pool memory it is bound into uint32_t wgen; // page generation of the last upload check uint32_t checked; // submit_epoch of the last check from a draw binding + uint32_t use_ep; // submit_epoch when a lookup last reached it (touch) + uint64_t use_frame; // gfx_counters.frames then int uploaded, released; uint32_t rel_serial; // reg_serial when it was released: slots registered before then are stale (slot_resolve) uint64_t rel_frame; // gfx_counters.frames when it was released (drop_stale) @@ -676,6 +678,17 @@ static uint64_t n_pressure_drops; static int comes_back_intact(const Tex *e) { return e->pub.mirrored && !e->pub.gpu_owned && !e->gpu_dirty; } +// When a lookup last reached an entry: entry_of (every texture get and slot resolution) and the per-submit check of a +// slot lookup (vk_texture_by_id_locked). The texture pool refresh looks every registered slot up once per submission +// (vk_exec_draw.c pool_sync_entries), so an entry no lookup reached for IDLE_SUBMITS submissions and STALE_FRAMES +// presents (below) has no live descriptor naming it: the game keeps the texture object, nothing samples it. +#define IDLE_SUBMITS 240 +static void touch(Tex *e) { + e->use_ep = submit_epoch; + e->use_frame = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED); +} +static int idle(const Tex *e, uint64_t now); + // NX2NAVI_TEX_RELEASED_MB (default a sixteenth of the installed memory, 1 to 4 GiB; 0 = no limit) bounds the own memory // of the released entries: RELEASED_MAX alone kept 5-7 GiB of released render targets and decoded textures after an // hour of play at a 2160p game resolution, device memory the driver also charges to the system's commit (the process's @@ -735,6 +748,10 @@ static int commit_is_low(void) { // young ones destroyed 2160p render targets about every frame for whole sessions. Frame generation also reads a frame's // depth target at the host present (vk_dlssg.c vk_dlssg_frame tags it), after the blit deferred destruction waits for. #define STALE_FRAMES 120 +static int idle(const Tex *e, uint64_t now) { + return submit_epoch - e->use_ep >= IDLE_SUBMITS && now - e->use_frame >= STALE_FRAMES; +} + static void drop_stale(uint64_t keep, uint64_t *count) { uint64_t now = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED); int dropped = 0; @@ -1249,6 +1266,7 @@ static Tex *entry_of(const tex_t *t) { } if (e->released) e->released = 0, nreleased--, rel_unlink(e), n_site[2]++, tex_gen_bump(k.addr); e->t = *t; + touch(e); return e; } @@ -1569,6 +1587,7 @@ const VkTexImage *vk_texture_by_id_locked(int id, view_t *view, int *has_view) { if (!e->uploaded || e->checked != submit_epoch) { make_fresh_snap(e); e->checked = submit_epoch; + touch(e); } s->cepoch = submit_epoch; } @@ -2350,6 +2369,71 @@ void vk_texture_census(VkTexCensus *s) { unlock(); } +// With NX2NAVI_TRACE=perf every 600 frames, after vk_memory.c's vram line: what the live entries' own memory holds. By +// kind: decoded ASTC (as BC3 or RGBA8) and other textures whose content the CPU provides, all of which a re-upload +// brings back (comes_back_intact), and the GPU-written ones (render targets, textures the GPU drew or copied into), +// whose content lives only in the image. "idle" is the part no lookup reached for IDLE_SUBMITS submissions and +// STALE_FRAMES presents (touch). Then the entries that share their storage address with another live entry (each +// descriptor of the same storage gets an image of its own), the formats holding the most, and the largest entries. +void vk_texture_census_log(uint64_t frame) { + enum { K_BC3, K_RGBA8, K_CPU, K_GPU, K_N }; + static const char *const kn[K_N] = {"ASTC as BC3", "ASTC as RGBA8", "other CPU-written", "GPU-written"}; + uint64_t n[K_N] = {0}, b[K_N] = {0}, in[K_N] = {0}, ib[K_N] = {0}, bound = 0, dup_n = 0, dup_b = 0; + struct { int fmt; uint64_t n, b; } fm[48]; + int nfm = 0; + struct { uint64_t b; uint32_t w, h, d, levels; int fmt, gpu; } big[4] = {{0}}; // copied: read after unlock + const double M = 1048576.0; + lock(); + uint64_t now = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED); + for (uint32_t i = 0; i < by_key.cap; i++) { + const Tex *e = by_key.vals[i]; + if (!e || e->released) continue; + if (!e->bytes) { bound += e->bound_to != NULL; continue; } + int k = !comes_back_intact(e) ? K_GPU : e->f.astc_bw ? (e->f.astc_bc ? K_BC3 : K_RGBA8) : K_CPU; + n[k]++, b[k] += e->bytes; + if (idle(e, now)) in[k]++, ib[k] += e->bytes; + int j = 0; + while (j < nfm && fm[j].fmt != (int)e->f.vk) j++; + if (j == nfm && nfm < 48) fm[nfm++] = (typeof(fm[0])){(int)e->f.vk, 0, 0}; + if (j < nfm) fm[j].n++, fm[j].b += e->bytes; + for (int q = 0; q < 4; q++) + if (e->bytes > big[q].b) { + memmove(big + q + 1, big + q, sizeof *big * (size_t)(3 - q)); + big[q] = (typeof(big[0])){e->bytes, e->pub.width, e->pub.height, e->pub.layers * e->pub.depth, + e->pub.levels, (int)e->f.vk, k == K_GPU}; + break; + } + } + for (uint32_t i = 0; i < by_addr.cap; i++) { // live entries with own memory at one address, beyond the largest + uint64_t cnt = 0, sum = 0, most = 0; + for (const Tex *p = by_addr.vals[i]; p; p = p->next_at) + if (!p->released && p->bytes) cnt++, sum += p->bytes, most = p->bytes > most ? p->bytes : most; + if (cnt > 1) dup_n += cnt - 1, dup_b += sum - most; + } + unlock(); + char line[1024]; + int o = snprintf(line, sizeof line, "mem: textures frame %llu: live with own memory by kind, idle part in brackets:", + (unsigned long long)frame); + for (int k = 0; k < K_N; k++) + o += snprintf(line + o, sizeof line - (size_t)o, " %s %llu %.0f MiB (%llu %.0f MiB);", kn[k], (unsigned long long)n[k], + b[k] / M, (unsigned long long)in[k], ib[k] / M); + snprintf(line + o, sizeof line - (size_t)o, " bound into pool memory %llu; sharing an address with another %llu %.0f MiB", + (unsigned long long)bound, (unsigned long long)dup_n, dup_b / M); + rt_log("%s", line); + for (int x = 0; x < nfm; x++) // by bytes, largest first + for (int y = x + 1; y < nfm; y++) + if (fm[y].b > fm[x].b) { typeof(fm[0]) t = fm[x]; fm[x] = fm[y], fm[y] = t; } + o = snprintf(line, sizeof line, "mem: textures frame %llu: VkFormats holding the most:", (unsigned long long)frame); + for (int x = 0; x < nfm && x < 8; x++) + o += snprintf(line + o, sizeof line - (size_t)o, " %d: %llu %.0f MiB;", fm[x].fmt, (unsigned long long)fm[x].n, + fm[x].b / M); + o += snprintf(line + o, sizeof line - (size_t)o, " largest:"); + for (int q = 0; q < 4 && big[q].b; q++) + o += snprintf(line + o, sizeof line - (size_t)o, " %ux%ux%u fmt %d %u levels %.0f MiB%s;", big[q].w, big[q].h, + big[q].d, big[q].fmt, big[q].levels, big[q].b / M, big[q].gpu ? " (GPU-written)" : ""); + rt_log("%s", line); +} + void vk_texture_shutdown(void) { VkCtx *c = vk_ctx(); lock();