mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-17 11:29:58 -04:00
Under the perf trace, report frame times per route segment and take a memory snapshot after each area load.
An input script line "<tick> MARK <name>" (or "@<ms> MARK <name>") ends the running segment with one line of its frame count, mean, 1% low and worst frame, the work marks on any thread, process memory and VRAM, then the 600-frame memory lines, and starts a segment of that name. RomFS opens of an area's static placement file give a "scene:" line, and once no area has loaded for 10 s the memory snapshot is taken with a "settled" line. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -57,6 +57,12 @@ enum { PERF_M_FILE, PERF_M_ZSTD, PERF_M_PIPE, PERF_M_GMEM, PERF_M_POOL, PERF_M_D
|
||||
uint64_t perf_mark_begin(void);
|
||||
void perf_mark_end(int kind, uint64_t t0, uint64_t bytes); // nothing when t0 is 0
|
||||
void perf_mark_add(int kind, uint64_t ns, uint64_t bytes); // nothing while the trace is off
|
||||
// NX2NAVI_TRACE=perf segments and scenes (perf.c): perf_segment ends the running segment of presents at the next present
|
||||
// with a line of its frame times (mean, 1% low, worst), the work marks above, process memory and VRAM, then the memory
|
||||
// snapshot, and starts one named name (the input script's MARK lines, input.c). perf_scene notes an area's load
|
||||
// (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_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.
|
||||
|
||||
@@ -66,9 +66,10 @@ static const SDL_GamepadButton pad_sdl[PAD_COUNT] = {
|
||||
// and its hold are milliseconds of the world clock (frame_step_world_us, game time since the loaded save's first
|
||||
// frame), so it lands at the same game time at any frame rate and load length. A timed event shorter than a frame is
|
||||
// applied once. In a script with timed lines (a recording) the untimed ones belong before the world clock: they stop
|
||||
// when it starts, as the load ends at another tick in each run.
|
||||
// when it starts, as the load ends at another tick in each run. "<frame> MARK <name>" presses nothing: with
|
||||
// NX2NAVI_TRACE=perf it ends the running segment of frame times and starts one called <name> (perf.c perf_segment).
|
||||
// at, hold: ticks, or microseconds of the world clock when timed.
|
||||
typedef struct { uint64_t at, hold; int bit, stick, timed; int32_t x, y; int logged; char text[48]; } ScriptEv;
|
||||
typedef struct { uint64_t at, hold; int bit, stick, timed, mark; int32_t x, y; int logged; char text[48]; } ScriptEv;
|
||||
static const struct { const char *name; int bit; } script_buttons[] = {
|
||||
{"A", B_A}, {"B", B_B}, {"X", B_X}, {"Y", B_Y}, {"LCLICK", B_STICK_L}, {"RCLICK", B_STICK_R}, {"L", B_L}, {"R", B_R},
|
||||
{"ZL", B_ZL}, {"ZR", B_ZR}, {"PLUS", B_PLUS}, {"MINUS", B_MINUS}, {"LEFT", B_LEFT}, {"UP", B_UP}, {"RIGHT", B_RIGHT}, {"DOWN", B_DOWN},
|
||||
@@ -99,7 +100,14 @@ static void script_load(void) {
|
||||
ScriptEv e = {.at = (uint64_t)(at * unit + 0.5), .hold = timed ? 66667 : 4, .bit = -1, .timed = timed};
|
||||
long a, b;
|
||||
double c;
|
||||
if (!strcmp(name, "MOUSE") || !strcmp(name, "LSTICK") || !strcmp(name, "RSTICK")) {
|
||||
if (!strcmp(name, "MARK")) {
|
||||
const char *s = line + off;
|
||||
while (*s == ' ' || *s == '\t') s++;
|
||||
size_t len = strcspn(s, "\r\n");
|
||||
while (len && (s[len - 1] == ' ' || s[len - 1] == '\t')) len--;
|
||||
e.mark = 1, e.hold = 1;
|
||||
snprintf(e.text, sizeof e.text, "%.*s", (int)len, s);
|
||||
} else if (!strcmp(name, "MOUSE") || !strcmp(name, "LSTICK") || !strcmp(name, "RSTICK")) {
|
||||
int n = sscanf(line + off, "%ld %ld %lf", &a, &b, &c);
|
||||
if (n < 2) { rt_log("input: script line %d: %s needs x y [hold]", ln, name); continue; }
|
||||
e.stick = name[0] == 'M' ? 3 : name[0] == 'L' ? 1 : 2; e.x = (int32_t)a; e.y = (int32_t)b;
|
||||
@@ -135,13 +143,14 @@ static void script_apply(uint64_t *b, int32_t sl[2], int32_t sr[2]) {
|
||||
if (e->timed ? w < 0 : script_mixed && w >= 0) continue;
|
||||
uint64_t now = e->timed ? (uint64_t)w : tick;
|
||||
if (now < e->at || (now >= e->at + e->hold && (e->logged || !e->timed))) continue;
|
||||
if (e->stick == 3 && e->logged) continue;
|
||||
if ((e->stick == 3 || e->mark) && e->logged) continue;
|
||||
if (!e->logged) {
|
||||
e->logged = 1;
|
||||
if (e->timed) rt_log("input: script frame %llu at %.1f ms: %s", (unsigned long long)fr, w / 1000.0, e->text);
|
||||
else rt_log("input: script frame %llu tick %llu: %s", (unsigned long long)fr, (unsigned long long)tick, e->text);
|
||||
}
|
||||
if (e->stick == 3) {
|
||||
if (e->mark) perf_segment(e->text);
|
||||
else if (e->stick == 3) {
|
||||
int64_t x = (int64_t)script_mouse[0] + e->x, y = (int64_t)script_mouse[1] + e->y;
|
||||
script_mouse[0] = (int32_t)(x < INT32_MIN ? INT32_MIN : x > INT32_MAX ? INT32_MAX : x);
|
||||
script_mouse[1] = (int32_t)(y < INT32_MIN ? INT32_MIN : y > INT32_MAX ? INT32_MAX : y);
|
||||
|
||||
123
runtime/perf.c
123
runtime/perf.c
@@ -168,6 +168,127 @@ static int mark_text(const MarkSums *s, char *line, int n, size_t cap) {
|
||||
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; }
|
||||
__attribute__((weak)) int vk_mem_vram(uint64_t *used, uint64_t *budget, int *exact) {
|
||||
(void)used, (void)budget, (void)exact;
|
||||
return 0;
|
||||
}
|
||||
|
||||
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;
|
||||
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);
|
||||
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;
|
||||
rt_log("segment: '%s' frames %llu-%llu, world %.1f-%.1f 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,
|
||||
(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);
|
||||
}
|
||||
|
||||
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);
|
||||
rt_log("segment: '%s' starts at frame %llu, world %.1f s", name, (unsigned long long)frame, seg.w0 / 1e6);
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
// ---- 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.
|
||||
@@ -397,6 +518,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();
|
||||
@@ -638,6 +760,7 @@ void perf_present(uint64_t frame) {
|
||||
}
|
||||
MarkSums fm;
|
||||
mark_frame(&fm);
|
||||
seg_frame(frame, now, last ? d : 0, &fm);
|
||||
if (last) {
|
||||
sum += d;
|
||||
n++;
|
||||
|
||||
@@ -1154,6 +1154,18 @@ static void open_note(uint64_t i, const char *path) {
|
||||
"(romfs-check.json lists the files found damaged)", path);
|
||||
}
|
||||
|
||||
// NX2NAVI_TRACE=perf: an area's static placement, Banc/<area>/<area>_<age>_Static.bcett.byml.zs, is read once when the
|
||||
// area loads (the game streams a neighbouring area the same way): perf_scene names it.
|
||||
static void scene_note(const char *p) {
|
||||
static const char tail[] = "_Static.bcett.byml";
|
||||
if (strncmp(p, "Banc/", 5)) return;
|
||||
const char *leaf = strrchr(p, '/'), *t = leaf ? strstr(leaf, tail) : NULL;
|
||||
if (!t || (t[sizeof tail - 1] && strcmp(t + sizeof tail - 1, ".zs"))) return;
|
||||
char name[96];
|
||||
snprintf(name, sizeof name, "%.*s", (int)(t - leaf - 1), leaf + 1);
|
||||
perf_scene(name);
|
||||
}
|
||||
|
||||
uint32_t romfs_open(uint64_t *out, const char *path, int mode) {
|
||||
fs_trace("rom", path);
|
||||
if (!out || mode != FS_OPEN_READ) return FS_R_UNSUPPORTED_OPEN;
|
||||
@@ -1168,6 +1180,7 @@ uint32_t romfs_open(uint64_t *out, const char *path, int mode) {
|
||||
uint64_t i = file_index(p);
|
||||
if (i == UINT64_MAX) return FS_R_PATH_NOT_FOUND;
|
||||
open_note(i, p);
|
||||
if (rt_trace & RT_TRACE_PERF) scene_note(p);
|
||||
if (!strcasecmp(p, CAMPACK)) cam_pack_opened(); // its values come into memory at the start's factor (camera_speed.c)
|
||||
uint32_t ov = overlay_slot(i);
|
||||
PlatFile *f = NULL;
|
||||
|
||||
@@ -614,6 +614,11 @@ static void outside_watch(uint64_t frame);
|
||||
|
||||
static volatile uint64_t mirror_frame; // the frame vk_mem_census last saw, for mirror_sync's log lines
|
||||
|
||||
// NX2NAVI_TRACE=perf: the 600-frame memory lines now, at a segment's end and once a scene has settled (perf.c).
|
||||
void vk_mem_snapshot(uint64_t frame) {
|
||||
if (rt_trace & RT_TRACE_PERF) vram_log(frame);
|
||||
}
|
||||
|
||||
void vk_mem_census(uint64_t frame) {
|
||||
mirror_frame = frame;
|
||||
static uint64_t window_start;
|
||||
|
||||
Reference in New Issue
Block a user