From ded13958de53f0f9ec8419dbbeb0d9055c29dfe8 Mon Sep 17 00:00:00 2001 From: chrg6 Date: Tue, 10 Nov 2026 19:51:36 -0500 Subject: [PATCH] Log the sizes of the caches, pools and tables under the perf trace, every 600 presents and with each memory snapshot. Four "mem: sizes" lines give the entries and bytes of what no other line counted: the guest heap, physical maps, kernel region, lazily written ranges and the fixed kernel, setjmp, save-file, RomFS and zstd tables; pool records, mirrors, deferred destructions, upload and staging buffers, the device address table and the texture, sampler and slot caches; image views, descriptor spares, the completion queue, uniform snapshot arenas, recording copies and pins; and pipelines, layouts, stage libraries, job queues, the shader memory cache and the translator processes. Each subsystem reads its own counters under its lock; settings now count the replaced values they keep. Segment and scene lines say "-" for the world clock before it starts. Co-Authored-By: Claude Opus 5.5 --- runtime/cfg.c | 11 ++++++ runtime/fs_host.c | 11 ++++++ runtime/gfx_shader.c | 28 +++++++++++++++ runtime/guest_setjmp.c | 9 +++++ runtime/host.h | 23 +++++++++++++ runtime/kernel.c | 20 +++++++++++ runtime/memory.c | 21 ++++++++++++ runtime/nvn_api_cmd.c | 21 ++++++++++++ runtime/ov_zstd.c | 13 +++++++ runtime/perf.c | 77 +++++++++++++++++++++++++++++++++++++----- runtime/romfs.c | 9 +++++ runtime/vk_exec_draw.c | 16 +++++++++ runtime/vk_gpa.c | 11 ++++++ runtime/vk_memory.c | 30 ++++++++++++++++ runtime/vk_pipeline.c | 25 ++++++++++++++ runtime/vk_sync.c | 12 +++++++ runtime/vk_texture.c | 20 +++++++++++ runtime/vk_usnap.c | 11 ++++++ 18 files changed, 359 insertions(+), 9 deletions(-) diff --git a/runtime/cfg.c b/runtime/cfg.c index c7a5f74a..bce97d68 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -26,6 +26,7 @@ static CfgEntry entries[CFG_MAX_KEYS]; static int n_entries; static unsigned version; static PlatMutex set_lock = PLAT_MUTEX_INIT; // cfg_set, cfg_reset, cfg_save +static uint64_t kept_n, kept_bytes; // set_lock: values cfg_set replaced and kept for readers (cfg_sizes) static int save_ok = 1; // the last cfg_save_to wrote (or had nothing to write) // Every name the runtime reads (and the patch sets in generated/), without the prefix. A file key outside this list is @@ -534,6 +535,7 @@ void cfg_set(const char *name, const char *value) { while (i < n && strcmp(entries[i].key, key)) i++; if (i < n) { if (!entries[i].value || strcmp(entries[i].value, value)) { + if (entries[i].value) kept_n++, kept_bytes += strlen(entries[i].value) + 1; // cfg_sizes __atomic_store_n(&entries[i].value, strdup(value), __ATOMIC_RELEASE); // the old string stays for readers entries[i].dirty = 1; } @@ -1010,3 +1012,12 @@ void cfg_init(void) { "turns it off)"); start_values(); } + +// NX2NAVI_TRACE=perf "mem: sizes" (perf.c): settings entries, and the replaced values cfg_set keeps for readers. +void cfg_sizes(PerfSizes *s) { + plat_mutex_lock(&set_lock); + uint64_t n = (uint64_t)n_entries, kn = kept_n, kb = kept_bytes; + plat_mutex_unlock(&set_lock); + perf_size(s, "settings", n, CFG_MAX_KEYS, PERF_SIZE_NONE); + perf_size(s, "replaced setting values kept", kn, 0, kb); +} diff --git a/runtime/fs_host.c b/runtime/fs_host.c index 59b573b7..f441ce44 100644 --- a/runtime/fs_host.c +++ b/runtime/fs_host.c @@ -2033,3 +2033,14 @@ int probe_fs(void) { printf("probe-fs: passed (%llu files, %d sampled)\n", (unsigned long long)files, SAMPLES); return 0; } + +// NX2NAVI_TRACE=perf "mem: sizes" (perf.c): open save-data files and directory listings against their tables. +void fs_host_sizes(PerfSizes *s) { + uint64_t nf = 0, nd = 0; + plat_mutex_lock_shared(&lock); + for (int i = 0; i < NFILES; i++) nf += files[i].h != NULL; + for (int i = 0; i < NDIRS; i++) nd += dirs[i].used != 0; + plat_mutex_unlock_shared(&lock); + perf_size(s, "save files open", nf, NFILES, PERF_SIZE_NONE); + perf_size(s, "save listings open", nd, NDIRS, PERF_SIZE_NONE); +} diff --git a/runtime/gfx_shader.c b/runtime/gfx_shader.c index be532ff2..f35468cf 100644 --- a/runtime/gfx_shader.c +++ b/runtime/gfx_shader.c @@ -3304,3 +3304,31 @@ void gfx_shader_carry_start(void) { } if (!plat_thread_spawn(carry_main, NULL, "shader carry", 0)) rt_log("gfx_shader: carry: cannot start the background pass"); } + +// NX2NAVI_TRACE=perf "mem: sizes" (perf.c): the shader memory cache's entries with their SPIR-V and reflection bytes +// (kept for the life of the process), the remembered failures, the translation and load queues, and the translator +// processes. cache_lock is held shared: lookups go on, a translator's insert waits for the walk. +void gfx_shader_sizes(PerfSizes *s) { + uint64_t n = 0, spv = 0, refl = 0, failed = 0, xq = 0, lq = 0, procs = 0, busy = 0; + plat_mutex_lock_shared(&cache_lock); + for (int i = 0; i < BUCKETS; i++) + for (const GfxShader *g = buckets[i]; g; g = g->next) n++, spv += g->words * 4, refl += g->reflection_size; + plat_mutex_unlock_shared(&cache_lock); + plat_mutex_lock(&failed_lock); + for (int i = 0; i < FAILED_N; i++) failed += failed_used[i] != 0; + plat_mutex_unlock(&failed_lock); + plat_mutex_lock(&X.lock); + for (const XJob *j = X.head; j; j = j->next) xq++; + for (const XJob *j = X.lhead; j; j = j->next) lq++; + plat_mutex_unlock(&X.lock); + plat_mutex_lock(&S.lock); + for (int i = 0; i < MAX_SVC; i++) procs += S.s[i].child != NULL, busy += S.s[i].busy != 0; + plat_mutex_unlock(&S.lock); + perf_size(s, "shader memory cache", n, 0, spv + refl); + perf_size(s, "of it reflection", PERF_SIZE_NONE, 0, refl); + perf_size(s, "failed shader keys", failed, FAILED_N, PERF_SIZE_NONE); + perf_size(s, "translations queued", xq, 0, PERF_SIZE_NONE); + perf_size(s, "shader loads queued", lq, 0, PERF_SIZE_NONE); + perf_size(s, "translator processes", procs, MAX_SVC, PERF_SIZE_NONE); + perf_size(s, "busy", busy, 0, PERF_SIZE_NONE); +} diff --git a/runtime/guest_setjmp.c b/runtime/guest_setjmp.c index 742fdebb..c7f9ed8f 100644 --- a/runtime/guest_setjmp.c +++ b/runtime/guest_setjmp.c @@ -166,3 +166,12 @@ static void ov_longjmp(Cpu *c) { HOST_OVERRIDE("setjmp", ov_setjmp); HOST_OVERRIDE("longjmp", ov_longjmp); + +// NX2NAVI_TRACE=perf "mem: sizes" (perf.c): the jmp_buf table's entries (entries are never removed; fatal when full). +void setjmp_sizes(PerfSizes *s) { + uint64_t n = 0; + plat_mutex_lock(&jt_lock); + for (int k = 0; k < JMP_N; k++) n += jt[k].env != 0; + plat_mutex_unlock(&jt_lock); + perf_size(s, "setjmp buffers", n, JMP_N, PERF_SIZE_NONE); +} diff --git a/runtime/host.h b/runtime/host.h index caad3322..693c127c 100644 --- a/runtime/host.h +++ b/runtime/host.h @@ -63,6 +63,29 @@ void perf_mark_add(int kind, uint64_t ns, uint64_t bytes); // nothing while th // (romfs.c, the area's static placement file) and takes the memory snapshot once no area has loaded for 10 s. void perf_segment(const char *name); void perf_scene(const char *name); +// NX2NAVI_TRACE=perf "mem: sizes" lines (perf.c sizes_log): the entries and bytes of the caches, pools and tables no +// other line gives, every 600 presents and with each memory snapshot. Each subsystem's *_sizes function (weak no-ops +// in perf.c) adds its own with perf_size, taking its locks briefly on the present thread. count or bytes UINT64_MAX: +// not known; limit 0: none. +typedef struct { char line[1800]; int n; uint64_t frame; const char *group; } PerfSizes; +void perf_size(PerfSizes *s, const char *name, uint64_t count, uint64_t limit, uint64_t bytes); +#define PERF_SIZE_NONE UINT64_MAX +void mem_sizes(PerfSizes *s); // memory.c +void kern_sizes(PerfSizes *s); // kernel.c +void setjmp_sizes(PerfSizes *s); // guest_setjmp.c +void fs_host_sizes(PerfSizes *s); // fs_host.c +void romfs_sizes(PerfSizes *s); // romfs.c +void zstd_sizes(PerfSizes *s); // ov_zstd.c +void cfg_sizes(PerfSizes *s); // cfg.c +void vk_mem_sizes(PerfSizes *s); // vk_memory.c +void vk_gpa_sizes(PerfSizes *s); // vk_gpa.c +void vk_texture_sizes(PerfSizes *s); // vk_texture.c +void vk_exec_sizes(PerfSizes *s); // vk_exec_draw.c +void vk_sync_sizes(PerfSizes *s); // vk_sync.c +void vk_usnap_sizes(PerfSizes *s); // vk_usnap.c +void nvn_cmd_sizes(PerfSizes *s); // nvn_api_cmd.c +void vk_pipeline_sizes(PerfSizes *s); // vk_pipeline.c +void gfx_shader_sizes(PerfSizes *s); // gfx_shader.c // NX2NAVI_REPLAY_TIME=1: how long the replay spends in each kind of op (perf.c perf_replay_op reports it with the // frame-time summary). Draws and dispatches include their bindings and the vkCmd calls they record; binds are the state // and resource binds between them; xfer is copies and clears. diff --git a/runtime/kernel.c b/runtime/kernel.c index e3cb0dda..e61faccf 100644 --- a/runtime/kernel.c +++ b/runtime/kernel.c @@ -2306,3 +2306,23 @@ int kern_svc(Cpu *c, uint32_t imm) { } return r; } + +// NX2NAVI_TRACE=perf "mem: sizes" (perf.c): guest threads, the handle table, the system-event table and fibers, each +// against its fixed size (the tables are fatal when full). +void kern_sizes(PerfSizes *s) { + uint64_t threads = 0, handles = 0, fibers = 0, ses; + plat_mutex_lock_shared(&G); + for (RtThread *t = g_all; t; t = t->all) threads++; + for (int i = 0; i < MAX_H; i++) handles += g_h[i].type != 0; + plat_mutex_unlock_shared(&G); + plat_mutex_lock_shared(&g_se_lock); + ses = (uint64_t)g_nse; + plat_mutex_unlock_shared(&g_se_lock); + plat_mutex_lock_shared(&g_fiber_lock); + for (int i = 0; i < FIBER_MAX; i++) fibers += g_fibers[i] != NULL; + plat_mutex_unlock_shared(&g_fiber_lock); + perf_size(s, "guest threads", threads, 0, PERF_SIZE_NONE); + perf_size(s, "handles", handles, MAX_H, PERF_SIZE_NONE); + perf_size(s, "system events", ses, sizeof g_se / sizeof *g_se, PERF_SIZE_NONE); + perf_size(s, "fibers", fibers, FIBER_MAX, PERF_SIZE_NONE); +} diff --git a/runtime/memory.c b/runtime/memory.c index 3703e792..3acd2bd0 100644 --- a/runtime/memory.c +++ b/runtime/memory.c @@ -1364,3 +1364,24 @@ void mem_guest_census(uint64_t frame) { } rt_log("%s; all %.0f", line, all / 1048576.0); } + +// NX2NAVI_TRACE=perf "mem: sizes" (perf.c): the guest heap and physical maps, the kernel region's bump and commit, the +// moved stacks, and the lazily written ranges with their array's room. +void mem_sizes(PerfSizes *s) { + plat_mutex_lock_shared(&lock); + uint64_t heap = heap_size, alias = alias_bytes, stacks = nstack_maps; + plat_mutex_unlock_shared(&lock); + plat_mutex_lock_shared(&klock); + uint64_t kused = kbump ? kbump - RT_KERNEL_BASE : 0, kcom = kcommitted ? kcommitted - RT_KERNEL_BASE : 0; + plat_mutex_unlock_shared(&klock); + plat_mutex_lock_shared(&frz_lock); + uint64_t lz = (uint64_t)lz_n, lzc = (uint64_t)lz_cap; + plat_mutex_unlock_shared(&frz_lock); + perf_size(s, "guest heap", PERF_SIZE_NONE, 0, heap); + perf_size(s, "guest physical maps", PERF_SIZE_NONE, 0, alias); + perf_size(s, "kernel region used", PERF_SIZE_NONE, 0, kused); + perf_size(s, "kernel region committed", PERF_SIZE_NONE, 0, kcom); + perf_size(s, "moved stacks", stacks, sizeof stack_maps / sizeof *stack_maps, PERF_SIZE_NONE); + perf_size(s, "lazy write-back ranges", lz, 0, PERF_SIZE_NONE); + perf_size(s, "their array's room", lzc, 0, PERF_SIZE_NONE); +} diff --git a/runtime/nvn_api_cmd.c b/runtime/nvn_api_cmd.c index 714cdeb0..e3a11d6e 100644 --- a/runtime/nvn_api_cmd.c +++ b/runtime/nvn_api_cmd.c @@ -1315,3 +1315,24 @@ const char *nvn_op_name(uint32_t op) { _Static_assert(sizeof names / sizeof *names == OP_COUNT_, "one name per op"); return op < OP_COUNT_ ? names[op] : "?"; } + +// NX2NAVI_TRACE=perf "mem: sizes" (perf.c): live recording copies and the zombies waiting for their submissions, texture +// pins with their sweep queue, and the recording-buffer pool. +void nvn_cmd_sizes(PerfSizes *s) { + plat_mutex_lock_shared(©_lock); + uint64_t nc = ncopies, cb = copy_bytes, nz = nzombies, zb = 0; + for (uint32_t i = 0; i < nzombies; i++) zb += zombies[i]->bytes; + plat_mutex_unlock_shared(©_lock); + plat_mutex_lock(&pin_lock); + uint64_t pins = pins_live, pq = pin_qn - pin_qh; + plat_mutex_unlock(&pin_lock); + plat_mutex_lock(&pool_lock); + uint64_t np = npool, pb = 0; + for (uint32_t i = 0; i < npool; i++) pb += pool[i].size; + plat_mutex_unlock(&pool_lock); + perf_size(s, "recording copies", nc, 0, cb); + perf_size(s, "recording copy zombies", nz, 0, zb); + perf_size(s, "texture pins", pins, 0, PERF_SIZE_NONE); + perf_size(s, "pin sweep queue", pq, 0, PERF_SIZE_NONE); + perf_size(s, "recording-buffer pool", np, POOL_SLOTS, pb); +} diff --git a/runtime/ov_zstd.c b/runtime/ov_zstd.c index c7d0ca3d..554c51fd 100644 --- a/runtime/ov_zstd.c +++ b/runtime/ov_zstd.c @@ -739,3 +739,16 @@ int probe_zstd(void) { } #endif + +// NX2NAVI_TRACE=perf "mem: sizes" (perf.c): host dictionaries (never freed) and stream slots taken. +void zstd_sizes(PerfSizes *s) { + uint64_t d, st = 0; + plat_mutex_lock(&dict_lock); + d = (uint64_t)ndicts; + plat_mutex_unlock(&dict_lock); + plat_mutex_lock(&stream_lock); + for (int i = 0; i < NSTREAM; i++) st += streams[i].s != 0; + plat_mutex_unlock(&stream_lock); + perf_size(s, "zstd dictionaries", d, NDICT, PERF_SIZE_NONE); + perf_size(s, "zstd stream slots", st, NSTREAM, PERF_SIZE_NONE); +} diff --git a/runtime/perf.c b/runtime/perf.c index 23907cf5..b5532c20 100644 --- a/runtime/perf.c +++ b/runtime/perf.c @@ -189,11 +189,65 @@ static struct { } seg; __attribute__((weak)) void vk_mem_snapshot(uint64_t frame) { (void)frame; } + +// ---- "mem: sizes" lines (host.h PerfSizes): one line per group; a line that grows past the log's line size goes out +// and the group goes on in the next. +#define SIZES_WEAK(f) __attribute__((weak)) void f(PerfSizes *s) { (void)s; } +SIZES_WEAK(mem_sizes) SIZES_WEAK(kern_sizes) SIZES_WEAK(setjmp_sizes) SIZES_WEAK(fs_host_sizes) SIZES_WEAK(romfs_sizes) +SIZES_WEAK(zstd_sizes) SIZES_WEAK(cfg_sizes) SIZES_WEAK(vk_mem_sizes) SIZES_WEAK(vk_gpa_sizes) SIZES_WEAK(vk_texture_sizes) +SIZES_WEAK(vk_exec_sizes) SIZES_WEAK(vk_sync_sizes) SIZES_WEAK(vk_usnap_sizes) SIZES_WEAK(nvn_cmd_sizes) +SIZES_WEAK(vk_pipeline_sizes) SIZES_WEAK(gfx_shader_sizes) + +static void sizes_flush(PerfSizes *s) { + if (s->n > 0) rt_log("mem: sizes frame %llu, %s:%s", (unsigned long long)s->frame, s->group, s->line); + s->n = 0, s->line[0] = 0; +} + +void perf_size(PerfSizes *s, const char *name, uint64_t count, uint64_t limit, uint64_t bytes) { + if (s->n > (int)sizeof s->line - 120) sizes_flush(s); + int n = snprintf(s->line + s->n, sizeof s->line - (size_t)s->n, " %s", name); + if (count != PERF_SIZE_NONE) n += snprintf(s->line + s->n + n, sizeof s->line - (size_t)(s->n + n), " %llu", (unsigned long long)count); + if (limit) n += snprintf(s->line + s->n + n, sizeof s->line - (size_t)(s->n + n), "/%llu", (unsigned long long)limit); + if (bytes != PERF_SIZE_NONE) + n += snprintf(s->line + s->n + n, sizeof s->line - (size_t)(s->n + n), "%s%.1f MiB", count != PERF_SIZE_NONE ? ", " : " ", + bytes / 1048576.0); + n += snprintf(s->line + s->n + n, sizeof s->line - (size_t)(s->n + n), ";"); + if (n > 0 && s->n + n < (int)sizeof s->line) s->n += n; + else s->line[s->n] = 0; +} + +static void sizes_log(uint64_t frame) { + static const struct { const char *group; void (*fn[7])(PerfSizes *); } groups[] = { + {"guest memory, kernel, files and settings", {mem_sizes, kern_sizes, setjmp_sizes, fs_host_sizes, romfs_sizes, zstd_sizes, cfg_sizes}}, + {"GPU memory and textures", {vk_mem_sizes, vk_gpa_sizes, vk_texture_sizes}}, + {"replay and submission", {vk_exec_sizes, vk_sync_sizes, vk_usnap_sizes, nvn_cmd_sizes}}, + {"pipelines and shaders", {vk_pipeline_sizes, gfx_shader_sizes}}, + }; + PerfSizes s; + for (size_t g = 0; g < sizeof groups / sizeof *groups; g++) { + s.n = 0, s.line[0] = 0, s.frame = frame, s.group = groups[g].group; + for (int i = 0; i < 7 && groups[g].fn[i]; i++) groups[g].fn[i](&s); + sizes_flush(&s); + } +} + +// The memory lines of a segment's end or a settled scene: the back end's (vk_mem_snapshot), then the sizes. +static void mem_snapshot(uint64_t frame) { + vk_mem_snapshot(frame); + sizes_log(frame); +} __attribute__((weak)) int vk_mem_vram(uint64_t *used, uint64_t *budget, int *exact) { (void)used, (void)budget, (void)exact; return 0; } +// The world clock as the segment and scene lines give it: seconds, or "-" before the loaded save's first frame. +static const char *wtxt(int64_t w, char *b) { + if (w < 0) return "-"; + snprintf(b, 24, "%.1f", w / 1e6); + return b; +} + void perf_segment(const char *name) { if (!(rt_trace & RT_TRACE_PERF)) return; plat_mutex_lock(&seg_lock); @@ -204,8 +258,9 @@ void perf_segment(const char *name) { void perf_scene(const char *name) { if (!(rt_trace & RT_TRACE_PERF)) return; - rt_log("scene: %s loads (frame %llu, world %.1f s)", name, - (unsigned long long)__atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED), frame_step_world_us() / 1e6); + char wb[24]; + rt_log("scene: %s loads (frame %llu, world %s s)", name, + (unsigned long long)__atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED), wtxt(frame_step_world_us(), wb)); plat_mutex_lock(&seg_lock); size_t n = strlen(scene_name); if (n + strlen(name) + 2 < sizeof scene_name) snprintf(scene_name + n, sizeof scene_name - n, "%s%s", n ? ", " : "", name); @@ -238,13 +293,14 @@ static void seg_close(uint64_t frame, uint64_t now, int at_exit) { for (int b = SEG_BINS - 1; b >= 0; b--) if ((acc += seg.hist[b]) >= cut) { low = (uint64_t)(b + 1) * SEG_BIN_NS; break; } if (low > seg.max) low = seg.max; - rt_log("segment: '%s' frames %llu-%llu, world %.1f-%.1f s: %llu frames in %.1f s, mean %.2f ms (%.1f fps), 1%% low %.1f " + char w0[24], w1[24]; + rt_log("segment: '%s' frames %llu-%llu, world %s to %s s: %llu frames in %.1f s, mean %.2f ms (%.1f fps), 1%% low %.1f " "fps (%.2f ms), worst %.1f ms at frame %llu; over 25 ms %llu, 50 ms %llu, 100 ms %llu; %s%s", - seg.name, (unsigned long long)seg.f0, (unsigned long long)frame, seg.w0 / 1e6, frame_step_world_us() / 1e6, + seg.name, (unsigned long long)seg.f0, (unsigned long long)frame, wtxt(seg.w0, w0), wtxt(frame_step_world_us(), w1), (unsigned long long)seg.frames, (now - seg.t0) / 1e9, seg.sum / 1e6 / seg.frames, seg.frames * 1e9 / seg.sum, low ? 1e9 / low : 0.0, low / 1e6, seg.max / 1e6, (unsigned long long)seg.max_frame, (unsigned long long)seg.over25, (unsigned long long)seg.over50, (unsigned long long)seg.over100, mem, marks); - if (!at_exit) vk_mem_snapshot(frame); + if (!at_exit) mem_snapshot(frame); } static void seg_open(const char *name, uint64_t frame, uint64_t now) { @@ -278,14 +334,16 @@ static void seg_frame(uint64_t frame, uint64_t now, uint64_t d, const MarkSums * if (asked) { seg_close(frame, now, 0); seg_open(name, frame, now); - rt_log("segment: '%s' starts at frame %llu, world %.1f s", name, (unsigned long long)frame, seg.w0 / 1e6); + char wb[24]; + rt_log("segment: '%s' starts at frame %llu, world %s s", name, (unsigned long long)frame, wtxt(seg.w0, wb)); } if (settled) { char mem[160]; mem_text(mem, sizeof mem, 1); - rt_log("scene: %s settled (no area loaded for 10 s) at frame %llu, world %.1f s; %s", scenes, - (unsigned long long)frame, frame_step_world_us() / 1e6, mem); - vk_mem_snapshot(frame); + char wb[24]; + rt_log("scene: %s settled (no area loaded for 10 s) at frame %llu, world %s s; %s", scenes, + (unsigned long long)frame, wtxt(frame_step_world_us(), wb), mem); + mem_snapshot(frame); } } @@ -814,4 +872,5 @@ void perf_present(uint64_t frame) { sum = max = n = ab_b = 0; memset(wait_ns, 0, sizeof wait_ns); win_start = now; + if (frame % 600 == 0) sizes_log(frame); // beside vk_memory.c's 600-frame memory lines } diff --git a/runtime/romfs.c b/runtime/romfs.c index 512c8d4a..4ade575d 100644 --- a/runtime/romfs.c +++ b/runtime/romfs.c @@ -1341,3 +1341,12 @@ HOST_OVERRIDE("_ZN2nn2fs8MountRomEPKcPvm", ov_mount_rom); HOST_OVERRIDE("_ZN2nn2fs8MountRomEPKc", ov_mount_rom); HOST_OVERRIDE("_ZN2nn2fs22QueryMountRomCacheSizeEPm", ov_query_cache); HOST_OVERRIDE("_ZN2nn2fs24InitializeForApplicationEv", ov_initialize); + +// NX2NAVI_TRACE=perf "mem: sizes" (perf.c): open RomFS handles against the table. +void romfs_sizes(PerfSizes *s) { + uint64_t n = 0; + plat_mutex_lock_shared(&lock); + for (unsigned j = 1; j < NH; j++) n += handles[j] != 0; + plat_mutex_unlock_shared(&lock); + perf_size(s, "RomFS handles open", n, NH - 1, PERF_SIZE_NONE); +} diff --git a/runtime/vk_exec_draw.c b/runtime/vk_exec_draw.c index 98a5da9b..933ddd49 100644 --- a/runtime/vk_exec_draw.c +++ b/runtime/vk_exec_draw.c @@ -5808,3 +5808,19 @@ void vk_exec_op_draw(VkExec *ex, uint32_t op, const void *payload) { op_draw_impl(ex, op, payload); perf_replay_op_at(op_is_draw(op) ? PERF_RP_DRAW : PERF_RP_BIND, op, plat_now_ns() - t0); } + +// NX2NAVI_TRACE=perf "mem: sizes" (perf.c): the image view cache (under the texture lock), the spare descriptor pools +// and transient chunks, and the continuations held for event order. +void vk_exec_sizes(PerfSizes *s) { + vk_texture_lock(); + uint64_t views = view_of.n, imgs = views_by_image.n; + vk_texture_unlock(); + plat_mutex_lock(&spare_lock); + uint64_t sp = (uint64_t)nspare_pool, sc = (uint64_t)nspare_chunk; + plat_mutex_unlock(&spare_lock); + perf_size(s, "image views", views, 0, PERF_SIZE_NONE); + perf_size(s, "images with views", imgs, 0, PERF_SIZE_NONE); + perf_size(s, "spare descriptor pools", sp, SPARE_POOLS, PERF_SIZE_NONE); + perf_size(s, "spare transient chunks", sc, SPARE_CHUNKS, sc * CHUNK_BYTES); + perf_size(s, "held continuations", (uint64_t)vk_exec_held(), 36, PERF_SIZE_NONE); +} diff --git a/runtime/vk_gpa.c b/runtime/vk_gpa.c index 6bd4732a..66763af5 100644 --- a/runtime/vk_gpa.c +++ b/runtime/vk_gpa.c @@ -374,3 +374,14 @@ void vk_gpa_shutdown(void) { memset(&G, 0, sizeof G); G.state = -1; } + +// NX2NAVI_TRACE=perf "mem: sizes" (perf.c): device address table records and L2 blocks in use (those waiting for a +// fence included), against their fixed sizes (when they run out, shaders read zeros). +void vk_gpa_sizes(PerfSizes *s) { + plat_mutex_lock(&gpa_lock); + uint64_t recs = G.nrec > 1 + G.nfree ? G.nrec - 1 - G.nfree : 0; + uint64_t blks = G.nblocks > 1 + G.nfree_blk + G.blk_waiting ? G.nblocks - 1 - G.nfree_blk - G.blk_waiting : 0; + plat_mutex_unlock(&gpa_lock); + perf_size(s, "device address records", recs, RECS, PERF_SIZE_NONE); + perf_size(s, "device address blocks", blks, BLOCKS, PERF_SIZE_NONE); +} diff --git a/runtime/vk_memory.c b/runtime/vk_memory.c index c90255e8..fc70da19 100644 --- a/runtime/vk_memory.c +++ b/runtime/vk_memory.c @@ -3931,3 +3931,33 @@ void vk_mem_shutdown(void) { for (uint64_t i = 0; i < GEN_CHUNKS; i++) free(gen_chunk[i]), gen_chunk[i] = NULL, free(cgen_chunk[i]), cgen_chunk[i] = NULL; plat_mutex_unlock(&gen_lock); } + +// NX2NAVI_TRACE=perf "mem: sizes" (perf.c): pool records, VRAM mirrors, the deferred destruction list, upload batches, +// the staging cache and arena, the write-back merge snapshot pool and the shadow storage's base copies. Counters only: +// cmd_lock is taken by every upload and release, so nothing is walked under it. +void vk_mem_sizes(PerfSizes *s) { + plat_mutex_lock_shared(&pool_lock); + uint64_t pools = (uint64_t)pool_index.n, phys = (uint64_t)nphys; + plat_mutex_unlock_shared(&pool_lock); + plat_rmutex_lock(&cmd_lock); + uint64_t dead_n = (uint64_t)ndead, infl = (uint64_t)ninflight, spare_b = (uint64_t)nspare, stg_n = (uint64_t)stg_ncache, + stg_b = (uint64_t)stg_cache_bytes, arena_n = (uint64_t)narena_free; + plat_rmutex_unlock(&cmd_lock); + plat_mutex_lock(&snap_pool_lock); + uint64_t snap_n = (uint64_t)nsnap_pool, snap_b = snap_pool_bytes; + plat_mutex_unlock(&snap_pool_lock); + plat_mutex_lock(&shadow_lock); + uint64_t sb = s_sb_pages; + plat_mutex_unlock(&shadow_lock); + perf_size(s, "pool records", pools, 0, PERF_SIZE_NONE); + perf_size(s, "of them physical", phys, 0, PERF_SIZE_NONE); + perf_size(s, "VRAM mirrors", (uint64_t)__atomic_load_n(&m_live_n, __ATOMIC_RELAXED), 0, + (uint64_t)__atomic_load_n(&m_live_bytes, __ATOMIC_RELAXED)); + perf_size(s, "deferred destructions", dead_n, 0, PERF_SIZE_NONE); + perf_size(s, "upload batches in flight", infl, 0, PERF_SIZE_NONE); + perf_size(s, "spare upload batches", spare_b, 0, PERF_SIZE_NONE); + perf_size(s, "staging cache", stg_n, STG_CACHE, stg_b); + perf_size(s, "free staging arena chunks", arena_n, ARENA_KEEP, arena_n * ARENA_CHUNK); + perf_size(s, "write-back snapshot pool", snap_n, SNAP_POOL_N, snap_b); + perf_size(s, "shadow base copies", sb, 0, sb * SB_PAGE); +} diff --git a/runtime/vk_pipeline.c b/runtime/vk_pipeline.c index 645376fa..b39be85d 100644 --- a/runtime/vk_pipeline.c +++ b/runtime/vk_pipeline.c @@ -6669,3 +6669,28 @@ int vk_pipeline_selftest(void) { printf("vk_pipeline selftest: %s\n", ok ? "passed" : "FAILED"); return ok ? 0 : 1; } + +// NX2NAVI_TRACE=perf "mem: sizes" (perf.c): the pipeline table's entries with their pipelines and fast links (never +// evicted), layouts, stage libraries, the job queues and the pipeline-keys.bin set. A walk of the hash chains under P.lock +// held shared: the workers' inserts and a draw's memo miss wait for it (well under a millisecond). +void vk_pipeline_sizes(PerfSizes *s) { + uint64_t ent = 0, built = 0, fast = 0, libn = 0, libb = 0, lq = 0; + plat_mutex_lock_shared(&P.lock); + for (int i = 0; i < PIPE_BUCKETS; i++) + for (const Pipe *p = pipes[i]; p; p = p->next) ent++, built += p->info.pipeline != VK_NULL_HANDLE, fast += p->fast != VK_NULL_HANDLE; + for (int i = 0; i < LIB_BUCKETS; i++) + for (const Lib *b = libs[i]; b; b = b->next) libn++, libb += b->pipe != VK_NULL_HANDLE; + for (const LibJob *j = U.head; j; j = j->next) lq++; + uint64_t lay = P.stats.layouts, fails = P.stats.failures, jobs = A.inflight, seen = K.seen_n, seen_b = (uint64_t)K.seen_cap * 8; + plat_mutex_unlock_shared(&P.lock); + perf_size(s, "pipeline entries", ent, 0, PERF_SIZE_NONE); + perf_size(s, "with a pipeline", built, 0, PERF_SIZE_NONE); + perf_size(s, "fast links", fast, 0, PERF_SIZE_NONE); + perf_size(s, "pipeline failures", fails, 0, PERF_SIZE_NONE); + perf_size(s, "pipeline layouts", lay, 0, PERF_SIZE_NONE); + perf_size(s, "stage library entries", libn, 0, PERF_SIZE_NONE); + perf_size(s, "built stage libraries", libb, 0, PERF_SIZE_NONE); + perf_size(s, "library jobs queued", lq, 0, PERF_SIZE_NONE); + perf_size(s, "pipeline jobs queued or running", jobs, 0, PERF_SIZE_NONE); + perf_size(s, "pipeline keys seen", seen, 0, seen_b); +} diff --git a/runtime/vk_sync.c b/runtime/vk_sync.c index 9e7ee07a..201934e3 100644 --- a/runtime/vk_sync.c +++ b/runtime/vk_sync.c @@ -763,3 +763,15 @@ void vk_sync_shutdown(void) { } plat_mutex_unlock(&start_lock); } + +// NX2NAVI_TRACE=perf "mem: sizes" (perf.c): command buffer slots (read without sub_lock, which a whole replay holds), +// the completion queue and the submissions held back. +void vk_sync_sizes(PerfSizes *s) { + uint64_t slots = (uint64_t)__atomic_load_n(&nslots, __ATOMIC_RELAXED); + plat_mutex_lock(&st_lock); + uint64_t q = (uint64_t)qn; + plat_mutex_unlock(&st_lock); + perf_size(s, "command buffer slots", slots, 0, PERF_SIZE_NONE); + perf_size(s, "completion queue", q, 0, PERF_SIZE_NONE); + perf_size(s, "held submissions", (uint64_t)__atomic_load_n(&held_n, __ATOMIC_RELAXED), 0, PERF_SIZE_NONE); +} diff --git a/runtime/vk_texture.c b/runtime/vk_texture.c index 628d4a80..ff180206 100644 --- a/runtime/vk_texture.c +++ b/runtime/vk_texture.c @@ -4278,3 +4278,23 @@ void vk_texture_shutdown(void) { if (sinfo) memset(sinfo, 0, sizeof *sinfo << 20); // no slot is registered: tokens 0 unlock(); } + +// NX2NAVI_TRACE=perf "mem: sizes" (perf.c): the texture cache's entries (the bytes are in the "mem: vram" line), the +// released ones, those retired by disuse, the sampler cache, the image requirements cache, the disuse holds, the +// reuse keys, and the pool's texture and sampler slots. Counters, and a walk of at most 4096 sampler slots. +void vk_texture_sizes(PerfSizes *s) { + lock(); + uint64_t imgs = by_key.n, rel = (uint64_t)nreleased, relb = rel_bytes, ret = (uint64_t)nretiring, retb = retiring_bytes, + smp = samplers.n, reqs = img_reqs.n, hold = holds.n, reuse_keys = reuse.n, tslot = pool_slots, sslot = 0; + for (int i = 0; i < nsslots; i++) sslot += sslots[i].used != 0; + unlock(); + perf_size(s, "textures", imgs, 0, PERF_SIZE_NONE); + perf_size(s, "released textures", rel, 0, relb); + perf_size(s, "retiring textures", ret, 0, retb); + perf_size(s, "samplers", smp, 0, PERF_SIZE_NONE); + perf_size(s, "image requirements", reqs, 0, PERF_SIZE_NONE); + perf_size(s, "disuse holds", hold, HOLDS_MAX, PERF_SIZE_NONE); + perf_size(s, "reuse keys", reuse_keys, 0, PERF_SIZE_NONE); + perf_size(s, "texture pool slots", tslot, VK_POOL_TEX, PERF_SIZE_NONE); + perf_size(s, "sampler slots", sslot, VK_POOL_SAMP, PERF_SIZE_NONE); +} diff --git a/runtime/vk_usnap.c b/runtime/vk_usnap.c index 3781865c..29a89f3d 100644 --- a/runtime/vk_usnap.c +++ b/runtime/vk_usnap.c @@ -490,3 +490,14 @@ void vk_usnap_shutdown(void) { free_arenas = NULL; plat_mutex_unlock(&free_lock); } + +// NX2NAVI_TRACE=perf "mem: sizes" (perf.c): the free uniform snapshot arenas (the free list is never trimmed) and the +// GPU-write list (the replay thread's; read without a lock, a line may be one write behind). +void vk_usnap_sizes(PerfSizes *s) { + uint64_t n = 0, b = 0; + plat_mutex_lock(&free_lock); + for (const Arena *a = free_arenas; a; a = a->next) n++, b += a->size; + plat_mutex_unlock(&free_lock); + perf_size(s, "free uniform snapshot arenas", n, 0, b); + perf_size(s, "uniform snapshot GPU writes", (uint64_t)__atomic_load_n(&U.nwr, __ATOMIC_RELAXED), 0, PERF_SIZE_NONE); +}