Merge the audit: its map and ledger, the perf-trace markers and size lines, and the fixes of its Phase 4.

Verified by runs in the audit: the Market's periodic stall (a pool slot was listed once per refresh pass instead of once; the list fell from 160,385 entries to at most 3,192 and no frame over 20 ms remained while standing), orderly exits delete the pipeline prebuild's running marker (left behind, the next start wrote a skip file and every later start skipped the prebuild), the log and crash report name an area a save asks for that the game does not have, and the menu atlas hash no longer overflows a signed multiply. The other fixes compile and pass the regression route but their faults were never reproduced: see AUDIT.md section 13 for each.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
chrg6
2026-11-11 00:39:08 -05:00
36 changed files with 3108 additions and 44 deletions

View File

@@ -119,6 +119,236 @@ void perf_count(int kind) {
if (__atomic_load_n(&perf_on, __ATOMIC_RELAXED)) plat_inc64(&counts[kind]);
}
// ---- NX2NAVI_TRACE=perf work marks (host.h PERF_M_*): any thread adds; the present takes the frame's sums (mark_frame),
// names them on a slow frame's line and adds them to the 60-frame window's line.
static const char *const mark_name[PERF_M_N] = {"file reads", "zstd decodes", "pipeline creates", "guest memory maps",
"pool init/finalize", "device memory allocations",
"DLSS feature creates"};
static volatile int64_t mk_ns[PERF_M_N], mk_n[PERF_M_N], mk_bytes[PERF_M_N], mk_max[PERF_M_N]; // since the last present
typedef struct { uint64_t ns[PERF_M_N], n[PERF_M_N], bytes[PERF_M_N], max[PERF_M_N]; } MarkSums;
static MarkSums mk_win; // the 60-frame window's, present thread only
uint64_t perf_mark_begin(void) { return (rt_trace & RT_TRACE_PERF) ? kern_now_ns() : 0; }
void perf_mark_add(int kind, uint64_t ns, uint64_t bytes) {
if (!(rt_trace & RT_TRACE_PERF) || (unsigned)kind >= PERF_M_N) return;
plat_add64(&mk_ns[kind], (int64_t)ns);
plat_inc64(&mk_n[kind]);
plat_add64(&mk_bytes[kind], (int64_t)bytes);
int64_t m = __atomic_load_n(&mk_max[kind], __ATOMIC_RELAXED);
while ((int64_t)ns > m && !__atomic_compare_exchange_n(&mk_max[kind], &m, (int64_t)ns, 1, __ATOMIC_RELAXED, __ATOMIC_RELAXED)) {}
}
void perf_mark_end(int kind, uint64_t t0, uint64_t bytes) {
if (t0) perf_mark_add(kind, kern_now_ns() - t0, bytes);
}
// Takes the sums since the last present into *f and adds them to the window.
static void mark_frame(MarkSums *f) {
for (int k = 0; k < PERF_M_N; k++) {
f->ns[k] = (uint64_t)plat_xchg64(&mk_ns[k], 0), f->n[k] = (uint64_t)plat_xchg64(&mk_n[k], 0);
f->bytes[k] = (uint64_t)plat_xchg64(&mk_bytes[k], 0), f->max[k] = (uint64_t)plat_xchg64(&mk_max[k], 0);
mk_win.ns[k] += f->ns[k], mk_win.n[k] += f->n[k], mk_win.bytes[k] += f->bytes[k];
if (f->max[k] > mk_win.max[k]) mk_win.max[k] = f->max[k];
}
}
// "; on any thread: file reads 12.3 ms (40, 5.1 MiB, longest 2.0 ms), ..." for the kinds with work, appended at line + n.
// Device memory allocations carry no time.
static int mark_text(const MarkSums *s, char *line, int n, size_t cap) {
int any = 0;
for (int k = 0; k < PERF_M_N && n >= 0 && n < (int)cap - 100; k++) {
if (!s->n[k]) continue;
n += snprintf(line + n, cap - (size_t)n, "%s %s", any ? "," : "; on any thread:", mark_name[k]);
if (k == PERF_M_DEVMEM)
n += snprintf(line + n, cap - (size_t)n, " %llu (%.1f MiB)", (unsigned long long)s->n[k], s->bytes[k] / 1048576.0);
else
n += snprintf(line + n, cap - (size_t)n, " %.1f ms (%llu, %.1f MiB, longest %.1f ms)", s->ns[k] / 1e6,
(unsigned long long)s->n[k], s->bytes[k] / 1048576.0, s->max[k] / 1e6);
any = 1;
}
return n;
}
// ---- NX2NAVI_TRACE=perf segments (host.h perf_segment) and scenes (perf_scene). The present thread keeps the running
// segment's frame times in 50 us bins (SEG_BINS, up to 204.8 ms; slower frames land in the last bin, the worst is kept
// apart), so a segment's 1% low is exact to 50 us at any frame rate. perf_segment and perf_scene come from any thread
// (the input script's guest thread, the game's loading threads) and leave a request under seg_lock for the present.
#define SEG_BINS 4096
#define SEG_BIN_NS 50000ull
#define SCENE_SETTLE_NS 10000000000ull
static PlatMutex seg_lock = PLAT_MUTEX_INIT;
static char seg_next[96], scene_name[256]; // seg_lock: the requested segment's name; the areas loaded since the last snapshot
static int seg_asked; // seg_lock: a segment was asked for
static uint64_t scene_at; // seg_lock: the last area load (plat_now_ns), 0 none since the last snapshot
static struct {
char name[96];
uint64_t f0, t0, frames, sum, max, max_frame, over25, over50, over100;
int64_t w0;
uint32_t hist[SEG_BINS];
MarkSums marks;
int on;
} 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);
snprintf(seg_next, sizeof seg_next, "%s", name && *name ? name : "(unnamed)");
seg_asked = 1;
plat_mutex_unlock(&seg_lock);
}
void perf_scene(const char *name) {
if (!(rt_trace & RT_TRACE_PERF)) return;
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);
scene_at = plat_now_ns();
plat_mutex_unlock(&seg_lock);
}
// The process's memory and the VRAM in use, for the segment and scene lines.
static void mem_text(char *out, size_t cap, int vram) {
uint64_t res, prv, used, budget;
int exact;
plat_process_memory(&res, &prv);
int n = snprintf(out, cap, "ram working set %.0f MiB, private %.0f MiB", res / 1048576.0, prv / 1048576.0);
if (vram && vk_mem_vram(&used, &budget, &exact) && n > 0 && (size_t)n < cap)
snprintf(out + n, cap - (size_t)n, "; vram %.0f of %.0f MiB", used / 1048576.0, budget / 1048576.0);
}
static void seg_close(uint64_t frame, uint64_t now, int at_exit) {
if (!seg.on) return;
seg.on = 0;
char mem[160], marks[700];
mem_text(mem, sizeof mem, !at_exit);
marks[0] = 0;
mark_text(&seg.marks, marks, 0, sizeof marks);
if (!seg.frames || !seg.sum) {
rt_log("segment: '%s' ended at frame %llu with no frames timed; %s", seg.name, (unsigned long long)frame, mem);
return;
}
uint64_t cut = (seg.frames + 99) / 100, acc = 0, low = 0; // the slowest 1% of frames: the upper edge of its bin
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;
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, 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) mem_snapshot(frame);
}
static void seg_open(const char *name, uint64_t frame, uint64_t now) {
memset(&seg, 0, sizeof seg);
snprintf(seg.name, sizeof seg.name, "%s", name);
seg.f0 = frame, seg.t0 = now, seg.w0 = frame_step_world_us(), seg.on = 1;
}
// Each present while the trace is on: the frame into the running segment (d 0: the first present), then a requested
// segment change and a settled scene's snapshot.
static void seg_frame(uint64_t frame, uint64_t now, uint64_t d, const MarkSums *fm) {
if (!seg.on && seg.f0 == 0) seg_open("boot", frame, now); // the first present opens the boot segment
if (seg.on && d) {
seg.frames++, seg.sum += d;
uint64_t b = d / SEG_BIN_NS;
seg.hist[b < SEG_BINS ? b : SEG_BINS - 1]++;
if (d > seg.max) seg.max = d, seg.max_frame = frame;
seg.over25 += d > 25000000ull, seg.over50 += d > 50000000ull, seg.over100 += d > 100000000ull;
for (int k = 0; k < PERF_M_N; k++) {
seg.marks.ns[k] += fm->ns[k], seg.marks.n[k] += fm->n[k], seg.marks.bytes[k] += fm->bytes[k];
if (fm->max[k] > seg.marks.max[k]) seg.marks.max[k] = fm->max[k];
}
}
char name[96], scenes[256];
int asked = 0, settled = 0;
plat_mutex_lock(&seg_lock);
if (seg_asked) asked = 1, seg_asked = 0, memcpy(name, seg_next, sizeof name);
if (scene_at && now - scene_at >= SCENE_SETTLE_NS)
settled = 1, scene_at = 0, memcpy(scenes, scene_name, sizeof scenes), scene_name[0] = 0;
plat_mutex_unlock(&seg_lock);
if (asked) {
seg_close(frame, now, 0);
seg_open(name, frame, now);
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);
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);
}
}
// ---- Frame-time summary, always on: every 60 s of play and at exit one line of frame count, mean fps, 1% low and the
// frames over 25/50/100 ms, then the worst frames of the period with the render thread's waits in them. Per frame: a
// histogram increment, the compare against the worst list, a memset of the wait array. No allocation.
@@ -348,6 +578,7 @@ __attribute__((weak)) void gfx_exit_log(void) {}
__attribute__((weak)) void vk_texstat_frame(uint64_t frame, uint64_t d) { (void)frame, (void)d; }
void perf_summary_flush(void) { // at exit (prof_write)
if (rt_trace & RT_TRACE_PERF) seg_close(__atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED), kern_now_ns(), 1);
if (summary_t0) {
ft_report(kern_now_ns(), "exit");
gfx_exit_log();
@@ -588,15 +819,25 @@ void perf_present(uint64_t frame) {
const char *e = cfg_get("NX2NAVI_PERF_SLOW_MS");
slow_ns = (uint64_t)(e && atoi(e) > 0 ? atoi(e) : 50) * 1000000ull;
}
MarkSums fm;
mark_frame(&fm);
seg_frame(frame, now, last ? d : 0, &fm);
if (last) {
sum += d;
n++;
if (d > max) max = d, memcpy(worst_ns, frame_ns, sizeof worst_ns);
if (d >= slow_ns)
rt_log("perf: slow frame %llu: %.1f ms; blocked pipeline %.1f xlate %.1f texture %.1f astc %.1f fence %.1f pacing %.1f",
if (d >= slow_ns) {
char marks[700];
marks[0] = 0;
mark_text(&fm, marks, 0, sizeof marks);
// drain: the wait for the present thread's previous job; replay: the replays since the last present, the
// pipeline, translation and texture waits above included (the rest of a slow frame is the game's own work)
rt_log("perf: slow frame %llu: %.1f ms; blocked pipeline %.1f xlate %.1f texture %.1f astc %.1f fence %.1f pacing %.1f "
"drain %.1f; replay %.1f%s",
(unsigned long long)frame, d / 1e6, frame_ns[PERF_PIPE] / 1e6, frame_ns[PERF_XLATE] / 1e6,
frame_ns[PERF_TEX] / 1e6, frame_ns[PERF_ASTC] / 1e6, frame_ns[PERF_FENCE] / 1e6,
frame_ns[PERF_PACE] / 1e6);
frame_ns[PERF_PACE] / 1e6, frame_ns[PERF_DRAIN] / 1e6, rep / 1e6, marks);
}
}
memset(frame_ns, 0, sizeof frame_ns);
last = now;
@@ -631,7 +872,11 @@ void perf_present(uint64_t frame) {
plat_xchg64(&replay_ns, 0) / 1e6 / n, (unsigned long long)ab_b, (unsigned long long)n, frame_step_world_us() / 1e6,
(unsigned long long)dwin[0], dwin[1] / 1e6, (unsigned long long)dwin[2]);
if (n && span) perf_gpu_line(frame, sum / 1e6 / n, (double)wait_ns[PERF_PACE] / span, n);
char marks[700];
if (mark_text(&mk_win, marks, 0, sizeof marks) > 0) rt_log("perf: frame %llu: work in the window%s", (unsigned long long)frame, marks);
memset(&mk_win, 0, sizeof mk_win);
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
}