mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-17 11:29:58 -04:00
Charge the frames a memory-census walk overlapped to census, the memory report's time to a report part, and walk once at frame 3600N.
With mem_census = 1 and trace = perf, vk_memory.c census_main walks the address space for 100-240 ms on its own
thread, holding the process's address-space lock. The replaying thread's write-watch queries wait for that lock
("mem: slow write-watch query: 20-34 ms ... 0 pages written"), and ft_frame charged the frame to "replay": in the
0.10.0 fresh session (nx2navi.1.log, 7,974,927 bytes) 63 of the stutter map's 66 replay hitches sat beside such a
walk, 49 of them at frames 600N+1..3. The presenting thread's own 600-frame memory report (10-17 ms, timed in
vk_mem_census since b81a9561) went to "unattributed". And at frame 3600N, census_check walked on the presenting thread
and the same frame's heap_log started census_main's second walk.
- perf.c perf_census_walk brackets census_main's walk (atomics); ft_frame moves the part of a frame over 25 ms (or a
stutter-map candidate) that overlapped the walk out of the replay part first, then out of the unattributed part,
into "census". The rule is runtime/perf_census.h census_charge, capped by the overlap and the frame.
- A new cause "report": vk_mem_census feeds the report's time (perf_report_part), and so do perf.c's memory
snapshots and its 600-frame sizes lines; the next frame takes it, so the overlap rule no longer has to infer it.
The cause list changes together: perf.c FT_C_* and ft_cause_name, stutter_map.c SM_CAUSES and cause_short,
tests/stutter_hitch_check.c names[].
- census_check records the frame it walked (perf_census_walked_frame); heap_log does not start census_main for that
frame, so frame 3600N walks once. A frame stamp, not a time window, since prof_commit_census can fail and leave
perf_note_wc uncalled.
- The "mem: process" line gives the walk's start and end on the run's clock and its length, so a play-test with
mem_census = 0 can tell the walk from the presenting thread's report as the cause of the 600N+1 hitches.
New tests/census_charge_check.py builds runtime/perf_census.h alone: no overlap, partial, contained and still-running
walks, the charge from replay first, capped by the overlap and the parts, and 100,000 random frames in which the census
part never passes the frame; docs/building.md describes it. Diagnostics only: NX2NAVI_MEM_CENSUS defaults to 0, and
nothing in gameplay, pacing or the save changes. Not reproduced before the change (it needs a game run). The pass's
known-issues table, which should say that hitch counts are taken with mem_census = 0 and trace off, is not in the
repository. census_charge_check, stutter_hitch_check and stutter_map_check pass; every runtime file compiles with the
dev flags, and perf.c, stutter_map.c and vk_memory.c add no -Wall -Wextra warnings.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -128,6 +128,14 @@ extern void (*perf_gpu_stats)(PerfGpu *out);
|
||||
// commit census). perf_wc_recent: the last such figure when it is at most max_age_ns old, else 0.
|
||||
void perf_note_wc(uint64_t wc, uint64_t vram_used);
|
||||
int perf_wc_recent(uint64_t max_age_ns, uint64_t *wc, uint64_t *vram_used);
|
||||
// The frame-time summary's "census" and "report" parts (perf.c ft_frame). perf_census_walk(1) and (0) bracket a walk of
|
||||
// the address space on another thread (vk_memory.c census_main): a frame that overlapped it has that much moved to
|
||||
// "census". perf_report_part adds the time the memory report took on the presenting thread (vk_memory.c
|
||||
// vk_mem_census), any thread, to the next present's "report". perf_census_walked_frame: the frame whose present walked
|
||||
// the address space itself (perf.c census_check), UINT64_MAX before any, so a report of that frame skips its own walk.
|
||||
void perf_census_walk(int begin);
|
||||
void perf_report_part(uint64_t ns);
|
||||
uint64_t perf_census_walked_frame(void);
|
||||
// The performance overlay (perfmon.c): mode 0 off, 1 fps, 2 full. While it is on, perf_present hands every frame to
|
||||
// perfmon_frame; full also takes the event counts (perf_count) and the GPU timestamps as NX2NAVI_TRACE=perf does.
|
||||
void perf_set_overlay(int mode);
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
#include "gfx.h"
|
||||
#include "host.h"
|
||||
#include "nvn_api.h"
|
||||
#include "perf_census.h"
|
||||
#include "perfmon.h"
|
||||
#include "prof.h"
|
||||
#include "stutter_map.h"
|
||||
@@ -329,10 +330,13 @@ static void hc_report(uint64_t frame) {
|
||||
plat_event_set(&hc_ev);
|
||||
}
|
||||
|
||||
// The memory lines of a segment's end or a settled scene: the back end's (vk_mem_snapshot), then the sizes.
|
||||
// The memory lines of a segment's end or a settled scene: the back end's (vk_mem_snapshot), then the sizes. Their
|
||||
// time on the presenting thread delays the next frame: its "report" part.
|
||||
static void mem_snapshot(uint64_t frame) {
|
||||
uint64_t t0 = kern_now_ns();
|
||||
vk_mem_snapshot(frame);
|
||||
sizes_log(frame);
|
||||
perf_report_part(kern_now_ns() - t0);
|
||||
}
|
||||
__attribute__((weak)) int vk_mem_vram(uint64_t *used, uint64_t *budget, int *exact) {
|
||||
(void)used, (void)budget, (void)exact;
|
||||
@@ -469,14 +473,34 @@ typedef struct {
|
||||
// The replay's part is its time without the pipeline, translation and texture waits, which all happen inside it: with
|
||||
// them in, every frame that waited 900 ms for a pipeline was charged to "replay", the larger number, and a log
|
||||
// could not tell a pipeline stall from a slow replay.
|
||||
// "census" is the address-space walk of NX2NAVI_MEM_CENSUS (census_check), which runs on the presenting thread after a
|
||||
// present: its time is charged to the next frame, which it delays. "hand-off" is the wait for the present thread to
|
||||
// take the previous picture (PERF_HANDOFF): its own pacing and its present call, an overlay's frame limiter included.
|
||||
// "census" is the address-space walk of NX2NAVI_MEM_CENSUS: census_check's on the presenting thread after a present,
|
||||
// whose time is charged to the next frame, which it delays, and the part of a frame vk_memory.c census_main's walk on
|
||||
// its own thread overlapped (perf_census.h census_charge). "report" is the memory report's time on the presenting
|
||||
// thread (vk_memory.c vk_mem_census every 600 frames with the perf trace, every 3600 without; the snapshots and sizes
|
||||
// lines here), charged to the next frame too. "hand-off" is the wait for the present thread to take the previous
|
||||
// picture (PERF_HANDOFF): its own pacing and its present call, an overlay's frame limiter included.
|
||||
// stutter_map.c cause_short and tools/stutter_map.py read these names, in this order.
|
||||
enum { FT_C_PIPE, FT_C_XLATE, FT_C_TEX, FT_C_FENCE, FT_C_PACE, FT_C_HANDOFF, FT_C_REPLAY, FT_C_CENSUS, FT_C_OTHER, FT_C_N };
|
||||
enum { FT_C_PIPE, FT_C_XLATE, FT_C_TEX, FT_C_FENCE, FT_C_PACE, FT_C_HANDOFF, FT_C_REPLAY, FT_C_CENSUS, FT_C_REPORT,
|
||||
FT_C_OTHER, FT_C_N };
|
||||
static const char *const ft_cause_name[FT_C_N] = { "pipeline", "translation", "texture", "fence", "pacing", "hand-off",
|
||||
"replay", "census", "unattributed" };
|
||||
"replay", "census", "report", "unattributed" };
|
||||
static uint64_t census_part_ns; // present thread: the census walks since the last present (census_check)
|
||||
static volatile int64_t report_part_ns; // the memory report's time since the last present (perf_report_part), any thread
|
||||
static volatile uint64_t walk_t0, walk_t1; // census_main's last walk (perf_census_walk); walk_t1 0 while it runs
|
||||
static volatile uint64_t census_walked = UINT64_MAX; // the frame census_check walked last
|
||||
|
||||
void perf_census_walk(int begin) {
|
||||
if (begin) {
|
||||
__atomic_store_n(&walk_t1, 0, __ATOMIC_RELAXED);
|
||||
__atomic_store_n(&walk_t0, kern_now_ns(), __ATOMIC_RELEASE);
|
||||
} else {
|
||||
__atomic_store_n(&walk_t1, kern_now_ns(), __ATOMIC_RELEASE);
|
||||
}
|
||||
}
|
||||
|
||||
void perf_report_part(uint64_t ns) { plat_add64(&report_part_ns, (int64_t)ns); }
|
||||
|
||||
uint64_t perf_census_walked_frame(void) { return __atomic_load_n(&census_walked, __ATOMIC_ACQUIRE); }
|
||||
static struct {
|
||||
uint64_t t0, frames, sum, over25, over50, over100;
|
||||
uint32_t hist[FT_BINS];
|
||||
@@ -753,8 +777,10 @@ static uint64_t ft_frame(uint64_t frame, uint64_t now, uint64_t d) {
|
||||
int best = FT_C_OTHER;
|
||||
uint64_t census_ns = census_part_ns;
|
||||
census_part_ns = 0;
|
||||
int64_t report_ns = __atomic_load_n(&report_part_ns, __ATOMIC_RELAXED) ? plat_xchg64(&report_part_ns, 0) : 0;
|
||||
if (d > 25000000ull || (stutter_map_on && d >= 12000000ull)) {
|
||||
part[FT_C_CENSUS] = census_ns < d ? census_ns : d;
|
||||
part[FT_C_REPORT] = report_ns <= 0 ? 0 : (uint64_t)report_ns < d ? (uint64_t)report_ns : d;
|
||||
part[FT_C_PIPE] = frame_ns[PERF_PIPE];
|
||||
part[FT_C_XLATE] = frame_ns[PERF_XLATE];
|
||||
part[FT_C_TEX] = frame_ns[PERF_TEX];
|
||||
@@ -767,6 +793,8 @@ static uint64_t ft_frame(uint64_t frame, uint64_t now, uint64_t d) {
|
||||
uint64_t known = 0;
|
||||
for (int c = 0; c < FT_C_OTHER; c++) known += part[c];
|
||||
part[FT_C_OTHER] = d > known ? d - known : 0;
|
||||
uint64_t w0 = __atomic_load_n(&walk_t0, __ATOMIC_ACQUIRE), w1 = __atomic_load_n(&walk_t1, __ATOMIC_ACQUIRE);
|
||||
census_charge(now, d, w0, w1, &part[FT_C_REPLAY], &part[FT_C_OTHER], &part[FT_C_CENSUS]);
|
||||
for (int c = 0; c < FT_C_N; c++)
|
||||
if (part[c] > part[best]) best = c;
|
||||
if (part[best] * 4 < d) best = FT_C_OTHER; // no recorded part explains a quarter of the frame
|
||||
@@ -853,6 +881,9 @@ static void census_check(uint64_t frame, uint64_t prv, const PerfGpu *g) {
|
||||
uint64_t host = host_commit(prv, g), t0 = kern_now_ns();
|
||||
int grown = host >= census_high + (1ull << 30) && (!census_ns || t0 - census_ns >= CENSUS_GAP_NS);
|
||||
if (frame % 3600 && !grown) return;
|
||||
// This frame's memory report (vk_memory.c heap_log, after this present) leaves out its own walk: one is enough,
|
||||
// and the second, 100-240 ms on census_main's thread, delayed the frames after it once more.
|
||||
__atomic_store_n(&census_walked, frame, __ATOMIC_RELEASE);
|
||||
uint64_t wc = 0;
|
||||
if (prof_commit_census(frame, &wc)) perf_note_wc(wc, g->have_vram ? g->vram_used : 0); // one walk for both lines
|
||||
else mem_guest_census(frame);
|
||||
@@ -1108,5 +1139,9 @@ 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), hc_report(frame); // beside vk_memory.c's 600-frame memory lines
|
||||
if (frame % 600 == 0) { // beside vk_memory.c's 600-frame memory lines, and like them the next frame's "report"
|
||||
uint64_t s0 = kern_now_ns();
|
||||
sizes_log(frame), hc_report(frame);
|
||||
perf_report_part(kern_now_ns() - s0);
|
||||
}
|
||||
}
|
||||
|
||||
25
runtime/perf_census.h
Normal file
25
runtime/perf_census.h
Normal file
@@ -0,0 +1,25 @@
|
||||
// The part of a frame that an address-space walk of NX2NAVI_MEM_CENSUS overlapped, charged to the census (perf.c
|
||||
// ft_frame). vk_memory.c census_main walks on a thread of its own, but the walk holds the process's address-space lock,
|
||||
// which the replaying thread's write-watch queries (vk_memory.c vk_mem_mirror_begin) and every page fault wait for: in
|
||||
// a session of 2026-11-13, 63 of the 66 hitches the stutter map charged to the replay sat beside such a walk, with a
|
||||
// 20-34 ms write-watch query in them. So the time the frame overlapped the walk is taken out of the frame's replay part
|
||||
// first, then out of its unattributed part, never more than the overlap and never so much that the census part grows
|
||||
// past the frame. Apart from perf.c so that tests/census_charge_check.py builds it alone.
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
|
||||
// The frame is [now - d, now]; the walk [w0, w1]: w0 0 when no walk was seen, w1 0 (or older than w0, the end of the
|
||||
// walk before) while it runs, then reaching now. Returns the time moved into *census.
|
||||
static inline uint64_t census_charge(uint64_t now, uint64_t d, uint64_t w0, uint64_t w1, uint64_t *replay,
|
||||
uint64_t *other, uint64_t *census) {
|
||||
if (!w0 || !d || d > now) return 0;
|
||||
if (w1 && w1 < w0) w1 = 0;
|
||||
uint64_t lo = now - d > w0 ? now - d : w0, hi = w1 && w1 < now ? w1 : now;
|
||||
if (hi <= lo) return 0;
|
||||
uint64_t ov = hi - lo, room = d > *census ? d - *census : 0;
|
||||
if (ov > room) ov = room;
|
||||
uint64_t r = ov < *replay ? ov : *replay;
|
||||
uint64_t o = ov - r < *other ? ov - r : *other;
|
||||
*replay -= r, *other -= o, *census += r + o;
|
||||
return r + o;
|
||||
}
|
||||
@@ -18,7 +18,7 @@
|
||||
// 96 ms after an area load (2026-11-13) each counted as a change of pace before, as each was followed by one as long.
|
||||
//
|
||||
// What a hitch went to comes from the frame-time summary's split (perf.c ft_frame: pipeline, translation, texture,
|
||||
// fence, pacing, hand-off, replay, census, unattributed), with the draws that met a pipeline or a shader missing between the previous
|
||||
// fence, pacing, hand-off, replay, census, report, unattributed), with the draws that met a pipeline or a shader missing between the previous
|
||||
// present and this one (stutter_miss, from the replay thread). Each hitch gets a "stutter:" line, at most
|
||||
// SM_LINES_PER_S a second and SM_LINES_MAX in all; the counts go on past that. Every SM_REPORT_NS of play, when the
|
||||
// window loses the focus in play (perf.c session_report) and at exit,
|
||||
@@ -42,7 +42,7 @@
|
||||
int stutter_map_on;
|
||||
|
||||
#define SM_AREAS 128 // areas kept by name; later ones share the last slot
|
||||
#define SM_CAUSES 9 // perf.c's FT_C_N
|
||||
#define SM_CAUSES 10 // perf.c's FT_C_N
|
||||
#define SM_LOAD_NS 3000000000ull // after an area load, frames count as the load's for this long
|
||||
#define SM_HITCH_MIN_NS 12000000.0 // a hitch is at least this long...
|
||||
#define SM_HITCH_ANY_NS 25000000ull // ...and any frame over this is one
|
||||
@@ -365,9 +365,9 @@ void stutter_frame(uint64_t frame, uint64_t now, uint64_t d, const uint64_t *par
|
||||
}
|
||||
|
||||
// In the order of perf.c's causes (FT_C_*); "hand-off" is the wait for the present thread to take the previous picture,
|
||||
// "census" NX2NAVI_MEM_CENSUS's walk of the address space.
|
||||
// "census" NX2NAVI_MEM_CENSUS's walk of the address space, "report" the memory report on the presenting thread.
|
||||
static const char *const cause_short[SM_CAUSES] = {"pipeline", "translation", "texture", "fence", "pacing", "hand-off",
|
||||
"replay", "census", "unattributed"};
|
||||
"replay", "census", "report", "unattributed"};
|
||||
|
||||
static int area_cmp(const void *x, const void *y) {
|
||||
const Area *a = &areas[*(const int *)x], *b = &areas[*(const int *)y];
|
||||
|
||||
@@ -1003,6 +1003,7 @@ void vk_mem_census(uint64_t frame) {
|
||||
rt_log("mem: the report of frame %llu took %.2f ms on the presenting thread: vram lines %.2f ms, holes %.2f ms, "
|
||||
"owners and the rest %.2f ms", (unsigned long long)frame, qpc_ms(h2 - r0), qpc_ms(rv), qpc_ms(h1 - h0),
|
||||
qpc_ms(h2 - h1));
|
||||
perf_report_part(h2 - r0); // the next frame's "report" part (perf.c ft_frame), not unattributed
|
||||
}
|
||||
if (vk_texture_compact_due()) compact_look(); // NX2NAVI_TEX_COMPACT: 0 at once with the setting off
|
||||
if ((rt_trace & RT_TRACE_PERF) && frame % 60 == 0)
|
||||
@@ -4558,10 +4559,17 @@ static void census_main(void *arg) {
|
||||
free(arg);
|
||||
const double M = 1048576.0;
|
||||
uint64_t priv = 0, wc = 0, wcn = 0, now;
|
||||
// perf.c ft_frame charges the frames the walk overlapped to "census"; the line gives the walk's span on the run's
|
||||
// clock (kern_now_ns) so that it can be set beside the frames it held up
|
||||
perf_census_walk(1);
|
||||
uint64_t w0 = kern_now_ns();
|
||||
int have = plat_mem_census(&priv, &wc, &wcn);
|
||||
uint64_t w1 = kern_now_ns();
|
||||
perf_census_walk(0);
|
||||
if (have) {
|
||||
rt_log("mem: process frame %llu: private %.0f MiB, write-combined %.0f MiB in %llu allocations",
|
||||
(unsigned long long)j.frame, priv / M, wc / M, (unsigned long long)wcn);
|
||||
rt_log("mem: process frame %llu: private %.0f MiB, write-combined %.0f MiB in %llu allocations; the walk %.3f s "
|
||||
"to %.3f s, %.1f ms", (unsigned long long)j.frame, priv / M, wc / M, (unsigned long long)wcn, w0 / 1e9,
|
||||
w1 / 1e9, (w1 - w0) / 1e6);
|
||||
uint64_t used = 0, budget = 0;
|
||||
int exact = 0;
|
||||
if (!vk_mem_vram(&used, &budget, &exact)) used = 0;
|
||||
@@ -4625,9 +4633,10 @@ static void heap_log(VkCtx *c, uint64_t frame, const VmaBudget *vb) {
|
||||
// The address-space walk of the census (plat_mem_census, 100-240 ms of VirtualQuery) runs on its own thread, but
|
||||
// it holds the process's address-space lock, which the game's threads take at every page fault and every
|
||||
// VirtualAlloc: the present after each walk arrived 65-120 ms late. NX2NAVI_MEM_CENSUS=1 asks for it.
|
||||
// Not when this frame's present walked already (perf.c census_check, every 3600 frames and on commit growth).
|
||||
static int census_on = -1;
|
||||
if (census_on < 0) census_on = cfg_bool("NX2NAVI_MEM_CENSUS");
|
||||
if (!census_on || plat_xchg32(&census_busy, 1)) return;
|
||||
if (!census_on || perf_census_walked_frame() == frame || plat_xchg32(&census_busy, 1)) return;
|
||||
CensusJob *j = malloc(sizeof *j);
|
||||
if (j) *j = (CensusJob){c, frame};
|
||||
if (!j || !plat_thread_spawn(census_main, j, "mem census", 0)) free(j), plat_xchg32(&census_busy, 0);
|
||||
|
||||
Reference in New Issue
Block a user