diff --git a/runtime/host.h b/runtime/host.h index da198746..8d3372f6 100644 --- a/runtime/host.h +++ b/runtime/host.h @@ -151,6 +151,7 @@ typedef struct { uint64_t base, size; uint32_t state, attr, perm, ipc, device, p void mem_init(void); // reserve every region; on a collision re-run the process // (up to 20 times, NX2NAVI_REEXEC), then fatal const RtGuestLayout *mem_layout(void); +void mem_guest_census(uint64_t frame); // NX2NAVI_TRACE=perf: committed bytes per guest region uint32_t mem_set_heap_size(uint64_t size, uint64_t *addr); // 0x01 uint32_t mem_set_permission(uint64_t a, uint64_t n, uint32_t perm); // 0x02 uint32_t mem_set_attribute(uint64_t a, uint64_t n, uint32_t mask, uint32_t at); // 0x03 diff --git a/runtime/memory.c b/runtime/memory.c index 8ea70681..87a72b41 100644 --- a/runtime/memory.c +++ b/runtime/memory.c @@ -708,3 +708,30 @@ void mem_kernel_free(void *p, uint64_t size) { kfree_list[k] = f; plat_mutex_unlock(&klock); } + +// NX2NAVI_TRACE=perf: how many bytes of each guest region are committed, so a process that has grown says which region +// holds the memory. A region is reserved whole at start-up and committed page by page, so the walk reads each run of +// pages of the same state; the alias window is the largest and costs the most steps when it is full. +void mem_guest_census(uint64_t frame) { + const RtGuestLayout *g = mem_layout(); + const struct { const char *name; RtRange r; } reg[] = { + {"aslr", g->aslr}, {"stack", g->stack}, {"heap", g->heap}, + {"alias", g->alias}, {"kernel", g->kernel}, {"hostfn", g->hostfn}, + }; + char line[400]; + int o = snprintf(line, sizeof line, "mem: guest regions at frame %llu (MiB committed):", + (unsigned long long)frame); + uint64_t all = 0; + for (unsigned i = 0; i < sizeof reg / sizeof *reg && o < (int)sizeof line - 40; i++) { + uint64_t x = reg[i].r.base, end = reg[i].r.base + reg[i].r.size, committed = 0, b = 0; + while (x < end) { + int st = plat_mem_state(x, end, &b); + if (b <= x) break; + if (st >= 0) committed += b - x; // a PLAT_PROT_* state: these pages are committed + x = b; + } + all += committed; + o += snprintf(line + o, sizeof line - o, " %s %.0f", reg[i].name, committed / 1048576.0); + } + rt_log("%s; all %.0f", line, all / 1048576.0); +} diff --git a/runtime/perf.c b/runtime/perf.c index 2545c68d..1a27ffef 100644 --- a/runtime/perf.c +++ b/runtime/perf.c @@ -23,6 +23,7 @@ __attribute__((weak)) int prof_sampler_start(void) { __attribute__((weak)) void prof_sampler_write(void) {} __attribute__((weak)) void prof_sampler_frame(uint64_t frame, uint64_t now, uint64_t d) { (void)frame, (void)now, (void)d; } __attribute__((weak)) void prof_commit_census(uint64_t frame) { (void)frame; } +__attribute__((weak)) void mem_guest_census(uint64_t frame) { (void)frame; } // NX2NAVI_TRACE=perf, NX2NAVI_FLIGHT=1 or the full performance overlay: event counts are taken. Wait times are always taken // (frame-time summary). perf_base: the part fixed at start. @@ -448,7 +449,8 @@ static void perf_gpu_line(uint64_t frame, double interval_ms, double pace_share, static int census_on = -1; static uint64_t census_prv; if (census_on < 0) census_on = cfg_bool("NX2NAVI_MEM_CENSUS"); - if (census_on && (frame % 3600 == 0 || prv >= census_prv + (1ull << 30))) prof_commit_census(frame), census_prv = prv; + if (census_on && (frame % 3600 == 0 || prv >= census_prv + (1ull << 30))) + prof_commit_census(frame), mem_guest_census(frame), census_prv = prv; if (g.have_gpu && g.have_idle) rt_log("perf: frame %llu: gpu idle per frame %.2f ms between timed parts: %.2f ms waiting for the next submission, " "%.2f ms with it queued (untimed work or waits); %.1f gaps over 0.5 ms, longest %.2f ms; submit to start avg %.2f ms",