diff --git a/runtime/perf.c b/runtime/perf.c index b6c1d3c1..0459a09e 100644 --- a/runtime/perf.c +++ b/runtime/perf.c @@ -22,6 +22,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; } // 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. @@ -440,6 +441,10 @@ static void perf_gpu_line(uint64_t frame, double interval_ms, double pace_share, rt_log("perf: frame %llu: %s; fence wait per frame: render thread %.2f ms, other threads %.2f ms; %s; ram working set " "%.0f MiB, private %.0f MiB; bound: %s", (unsigned long long)frame, gs, rw, ow, vs, ws, priv, bound); + // Where the committed memory lies, every 3600 frames and whenever it grew by 1 GiB since the last census: the process + // has run out of commit in long sessions while the game's own pools stayed flat. + static uint64_t census_prv; + if (frame % 3600 == 0 || prv >= census_prv + (1ull << 30)) prof_commit_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", diff --git a/runtime/prof.h b/runtime/prof.h index a11cc84a..1619b4e3 100644 --- a/runtime/prof.h +++ b/runtime/prof.h @@ -10,3 +10,4 @@ extern volatile uint32_t prof_replay_tid; // the asynchronous replay (perf_rep int prof_sampler_start(void); // NX2NAVI_PROFILE: starts sampling; 1 when it runs void prof_sampler_write(void); // writes the profile once (prof_write) void prof_sampler_frame(uint64_t frame, uint64_t now, uint64_t d); // every present; d: the interval, 0 on the first +void prof_commit_census(uint64_t frame); // NX2NAVI_TRACE=perf: logs where the process's committed memory lies diff --git a/runtime/prof_win.c b/runtime/prof_win.c index c5047ca1..f8e7157c 100644 --- a/runtime/prof_win.c +++ b/runtime/prof_win.c @@ -394,6 +394,51 @@ int prof_sampler_start(void) { // perf_present, every present: the frame the sampler's NX2NAVI_PROFILE_FRAMES window counts, and a NX2NAVI_PROFILE_SLOW // dump of a present at least slow_ns after the previous one (d 0 on the first present). +// NX2NAVI_TRACE=perf (perf.c): the process's committed memory by kind, and outside the guest's address range the +// allocations holding the most private memory, so a log shows what grows: guest memory, a heap, the graphics driver's +// mappings or loaded images. A walk of the address space with VirtualQuery: tens of milliseconds, so only now and then. +#define CENSUS_TOP 12 +typedef struct { uint64_t base, bytes; DWORD protect; } CensusRun; + +static void census_keep(CensusRun *top, int *nt, const CensusRun *r) { // top[] stays sorted, largest first + if (!r->bytes || (*nt == CENSUS_TOP && r->bytes <= top[*nt - 1].bytes)) return; + int k = *nt < CENSUS_TOP ? (*nt)++ : *nt - 1; + for (; k > 0 && top[k - 1].bytes < r->bytes; k--) top[k] = top[k - 1]; + top[k] = *r; +} + +void prof_commit_census(uint64_t frame) { + uint64_t t0 = GetTickCount64(), guest = 0, priv = 0, mapped = 0, image = 0, regions = 0; + CensusRun top[CENSUS_TOP], cur = {0}; + int nt = 0; + MEMORY_BASIC_INFORMATION mi; + for (uint8_t *a = NULL; VirtualQuery(a, &mi, sizeof mi) == sizeof mi; a = (uint8_t *)mi.BaseAddress + mi.RegionSize) { + regions++; + uint64_t base = (uint64_t)(uintptr_t)mi.AllocationBase, at = (uint64_t)(uintptr_t)mi.BaseAddress; + int committed = mi.State == MEM_COMMIT, outside = committed && mi.Type == MEM_PRIVATE && at >= RT_ADDRESS_LIMIT; + if (committed) { + if (mi.Type == MEM_IMAGE) image += mi.RegionSize; + else if (mi.Type == MEM_MAPPED) mapped += mi.RegionSize; + else if (at < RT_ADDRESS_LIMIT) guest += mi.RegionSize; + else priv += mi.RegionSize; + } + if (cur.bytes && (!outside || base != cur.base)) census_keep(top, &nt, &cur), cur.bytes = 0; // its run ended + if (outside) { + if (!cur.bytes) cur.base = base, cur.protect = mi.AllocationProtect; + cur.bytes += mi.RegionSize; + } + if (at + mi.RegionSize < at) break; // the top of the address space + } + census_keep(top, &nt, &cur); + rt_log("mem: commit census at frame %llu: guest %.0f MiB, other private %.0f MiB, mapped %.0f MiB, images %.0f MiB " + "(%llu regions walked in %llu ms); the largest private allocations outside the guest:", + (unsigned long long)frame, guest / 1048576.0, priv / 1048576.0, mapped / 1048576.0, image / 1048576.0, + (unsigned long long)regions, (unsigned long long)(GetTickCount64() - t0)); + for (int i = 0; i < nt; i++) + rt_log("mem: %#llx %.1f MiB committed (protect %#lx)", (unsigned long long)top[i].base, top[i].bytes / 1048576.0, + (unsigned long)top[i].protect); +} + void prof_sampler_frame(uint64_t frame, uint64_t now, uint64_t d) { cur_frame = frame; // the end of a NX2NAVI_PROFILE_FRAMES window writes the profile then: a test run ended by a timeout never reaches