diff --git a/runtime/flight.c b/runtime/flight.c
index 85f33204..ef31d670 100644
--- a/runtime/flight.c
+++ b/runtime/flight.c
@@ -1,7 +1,7 @@
// NX2NAVI_FLIGHT=1 flight recorder: a ring of the last NX2NAVI_FLIGHT_FRAMES (600) presents, one record each (interval,
// render-thread blocked time per perf kind, event counts) and a 320x180 thumbnail each (gfx_present.c, blitted on the
-// GPU by the present thread). A dump goes to
/-f/: frames.tsv and f.png per
-// thumbnail, written by a background thread. Triggers: F8 while the game window has focus, a frame interval over
+// GPU by the present thread). A dump goes to /-f/: events.tsv (the texture events of its
+// frames, flight_tex), f.png per thumbnail and frames.tsv, written in that order by a background thread. Triggers: F8 while the game window has focus, a frame interval over
// NX2NAVI_FLIGHT_SPIKE_MS (100; 0 off; at most one dump per 10 s), NX2NAVI_FLIGHT_AT=[,...] and
// NX2NAVI_FLIGHT_EVERY= (every n-th frame: with n under the ring's length the dumps of a run hold every frame of it
// but the last n). A dump holds the frames up to FLIGHT_POST presents after the trigger, so the aftermath is in it
@@ -40,11 +40,22 @@ typedef struct {
uint32_t interval_us, blocked_us[PERF_N], n[C_N];
} Rec;
+// A texture event (flight_tex). seq is the event's index + 1 once it is written, 0 while a writer fills the slot, so
+// the copy start_dump takes leaves out a slot that is being written or was reused meanwhile.
+typedef struct {
+ uint64_t seq, frame, addr;
+ uint32_t kind, format, w, h, layers, flags, detail, us;
+} TexEv;
+#define TEX_RING (1u << 18) // events kept: 14 MiB; an area load makes a few thousand in one frame
+
typedef struct {
int n; // records, oldest first
uint64_t trig_frame, trig_interval_us;
const char *why;
char dir[PLAT_PATH_MAX];
+ TexEv *ev; // the texture events of the records' frames, oldest first
+ size_t nev;
+ uint64_t ev_from; // the first frame with all its events kept, when the ring had dropped earlier ones; 0 all
Rec r[];
} DumpJob;
@@ -61,6 +72,8 @@ static char base_dir[PLAT_PATH_MAX];
static uint64_t prev_cnt[PERF_CNT_N], prev_xlated;
static VkExecFrame prev_exec;
static volatile uint64_t cost_ns, cost_max_ns; // render-thread time in flight_frame, for the dump log line
+static TexEv *tex_ring;
+static volatile uint64_t tex_n; // texture events written so far
static unsigned env_u(const char *name, unsigned def) {
const char *e = cfg_get(name);
@@ -93,6 +106,7 @@ void flight_init(void) {
if (!plat_full_path(tmp, base_dir, sizeof base_dir)) snprintf(base_dir, sizeof base_dir, "%s", tmp);
thumbs_on = env_u("NX2NAVI_FLIGHT_THUMBS", 1) != 0;
if (thumbs_on) vk_thumbs_enable(ring_n);
+ if (!(tex_ring = calloc(TEX_RING, sizeof *tex_ring))) rt_log("flight: no memory for the texture events, none kept");
flight_on = 1;
rt_log("flight: on: last %d frames, %s; dump on F8, on a frame over %llu ms (one per 10 s)%s, to %s", ring_n,
thumbs_on ? "thumbnails 320x180" : "no thumbnails", (unsigned long long)(spike_ns / 1000000),
@@ -103,6 +117,80 @@ void flight_init(void) {
(unsigned long long)((uint64_t)ring_n * FLIGHT_THUMB_W * FLIGHT_THUMB_H * 4 >> 20));
}
+void flight_tex(int kind, uint64_t addr, uint32_t format, uint32_t w, uint32_t h, uint32_t layers, uint32_t flags,
+ uint32_t detail, uint32_t us) {
+ if (!tex_ring) return;
+ uint64_t i = __atomic_fetch_add(&tex_n, 1, __ATOMIC_RELAXED);
+ TexEv *v = &tex_ring[i % TEX_RING];
+ __atomic_store_n(&v->seq, 0, __ATOMIC_RELAXED);
+ __atomic_thread_fence(__ATOMIC_RELEASE); // a reader that sees the fields below being written sees seq 0 first
+ // The frame flight_frame records next: gfx_counters.frames counts the presents so far, and the counts of this
+ // present's record (frames.tsv) gather until the next one.
+ v->frame = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED) + 1;
+ v->addr = addr;
+ v->kind = (uint32_t)kind, v->format = format, v->w = w, v->h = h, v->layers = layers, v->flags = flags;
+ v->detail = detail, v->us = us;
+ __atomic_store_n(&v->seq, i + 1, __ATOMIC_RELEASE);
+}
+
+// The texture events from frame `first` on, oldest first, into j (render thread, at the dump's start).
+static void copy_events(DumpJob *j, uint64_t first) {
+ j->ev = NULL, j->nev = 0, j->ev_from = 0;
+ if (!tex_ring) return;
+ uint64_t end = __atomic_load_n(&tex_n, __ATOMIC_ACQUIRE), beg = end > TEX_RING ? end - TEX_RING : 0;
+ if (end == beg || !(j->ev = malloc(sizeof *j->ev * (size_t)(end - beg)))) return;
+ int dropped = beg > 0; // the ring has wrapped: events older than the oldest kept one are gone
+ for (uint64_t i = beg; i < end; i++) {
+ const TexEv *v = &tex_ring[i % TEX_RING];
+ if (__atomic_load_n(&v->seq, __ATOMIC_ACQUIRE) != i + 1) continue;
+ TexEv c = *v;
+ __atomic_thread_fence(__ATOMIC_ACQUIRE);
+ if (__atomic_load_n(&v->seq, __ATOMIC_RELAXED) != i + 1) continue; // reused while it was copied
+ if (dropped) { // the oldest kept event's frame may have lost earlier events of its own: from the next one on
+ if (c.frame >= first) j->ev_from = c.frame + 1;
+ dropped = 0;
+ }
+ if (c.frame >= first) j->ev[j->nev++] = c;
+ }
+}
+
+static const char *const tex_kind_names[FLIGHT_TEX_N] = {"new", "up", "reup", "release", "takeback", "drop"};
+
+// /events.tsv: one line per texture event of the dump's frames (flight_tex).
+static void write_events(const DumpJob *j) {
+ char path[PLAT_PATH_MAX + 32];
+ snprintf(path, sizeof path, "%s/events.tsv", j->dir);
+ FILE *f = fopen(path, "w");
+ if (!f) {
+ rt_log("flight: cannot write %s", path);
+ return;
+ }
+ fprintf(f, "# nx2navi flight dump texture events: frames %llu-%llu",
+ (unsigned long long)j->r[0].frame, (unsigned long long)j->r[j->n - 1].frame);
+ if (j->ev_from) fprintf(f, "; the ring kept all events from frame %llu on only", (unsigned long long)j->ev_from);
+ fprintf(f, "\nframe\tevent\taddr\tformat\twidth\theight\tlayers\tflags\tdetail\tus\n");
+ for (size_t i = 0; i < j->nev; i++) {
+ const TexEv *e = &j->ev[i];
+ if (e->frame > j->r[j->n - 1].frame) break;
+ char fl[64];
+ int k = 0;
+ fl[0] = 0;
+ static const char *const flag_names[] = {"depth", "stencil", "mirrored", "bound", "astc", "gpu"};
+ for (int b = 0; b < 6; b++)
+ if (e->flags & 1u << b) k += snprintf(fl + k, sizeof fl - (size_t)k, "%s%s", k ? "," : "", flag_names[b]);
+ char detail[160];
+ if (e->kind == FLIGHT_TEX_NEW) snprintf(detail, sizeof detail, "%s", vk_texture_start_name((int)e->detail));
+ else if (e->kind == FLIGHT_TEX_DROP) snprintf(detail, sizeof detail, "%s", vk_texture_drop_name((int)e->detail));
+ else if (e->kind == FLIGHT_TEX_UP || e->kind == FLIGHT_TEX_REUP)
+ snprintf(detail, sizeof detail, "%u WriteTexels", e->detail);
+ else detail[0] = 0;
+ fprintf(f, "%llu\t%s\t%#llx\t%#x\t%u\t%u\t%u\t%s\t%s\t%u\n", (unsigned long long)e->frame,
+ e->kind < FLIGHT_TEX_N ? tex_kind_names[e->kind] : "?", (unsigned long long)e->addr, e->format, e->w,
+ e->h, e->layers, fl, detail, e->us);
+ }
+ fclose(f);
+}
+
// ---------------------------------------------------------------- PNG (RGB, Paeth rows, fixed-Huffman deflate)
typedef struct { uint8_t *p; size_t n; uint64_t bits; int nb; } Bits;
@@ -235,9 +323,10 @@ static void dump_main(void *arg) {
plat_file_mkdir_p(j->dir);
if (!plat_dir_exists(j->dir)) {
rt_log("flight: cannot create %s", j->dir);
- free(j);
+ free(j->ev), free(j);
return;
}
+ write_events(j); // first: it takes a moment, the thumbnails seconds, and a game closed meanwhile keeps it
uint8_t *have = calloc((size_t)j->n, 1);
int n_thumbs = 0;
if (thumbs_on && have) {
@@ -268,7 +357,7 @@ static void dump_main(void *arg) {
FILE *f = fopen(path, "w");
if (!f) {
rt_log("flight: cannot write %s", path);
- free(have), free(j);
+ free(have), free(j->ev), free(j);
return;
}
fprintf(f, "# nx2navi flight dump: %s at frame %llu, interval %.1f ms\n", j->why, (unsigned long long)j->trig_frame,
@@ -285,12 +374,13 @@ static void dump_main(void *arg) {
fprintf(f, "\t%d\n", have ? have[i] : 0);
}
fclose(f);
- rt_log("flight: dump %s: %s at frame %llu (%.1f ms), frames %llu-%llu, %d thumbnails, written in %llu ms; "
- "recorder %.1f us per frame (max %.0f us)", j->dir,
+ rt_log("flight: dump %s: %s at frame %llu (%.1f ms), frames %llu-%llu, %d thumbnails, %zu texture events, written "
+ "in %llu ms; recorder %.1f us per frame (max %.0f us)", j->dir,
j->why, (unsigned long long)j->trig_frame, j->trig_interval_us / 1000.0, (unsigned long long)j->r[0].frame,
- (unsigned long long)j->r[j->n - 1].frame, n_thumbs, (unsigned long long)(plat_now_ms() - t_start),
+ (unsigned long long)j->r[j->n - 1].frame, n_thumbs, j->nev, (unsigned long long)(plat_now_ms() - t_start),
recorded ? cost_ns / 1e3 / (double)recorded : 0.0, cost_max_ns / 1e3);
free(have);
+ free(j->ev);
free(j);
return;
}
@@ -304,11 +394,12 @@ static void start_dump(void) {
j->trig_interval_us = trig_interval_us;
j->why = trig_why;
for (int i = 0; i < n; i++) j->r[i] = ring[(recorded - (uint64_t)n + (uint64_t)i) % (uint64_t)ring_n];
+ copy_events(j, j->r[0].frame);
struct tm st;
plat_local_time(&st);
snprintf(j->dir, sizeof j->dir, "%s/%04d%02d%02d-%02d%02d%02d-f%llu", base_dir, st.tm_year + 1900, st.tm_mon + 1,
st.tm_mday, st.tm_hour, st.tm_min, st.tm_sec, (unsigned long long)trig_frame);
- if (!plat_thread_spawn(dump_main, j, "flight dump", 0)) rt_log("flight: cannot start the dump thread"), free(j);
+ if (!plat_thread_spawn(dump_main, j, "flight dump", 0)) rt_log("flight: cannot start the dump thread"), free(j->ev), free(j);
}
static void trigger(uint64_t frame, uint64_t interval_ns, const char *why) {
diff --git a/runtime/flight.h b/runtime/flight.h
index 9ed9d476..33eb5c46 100644
--- a/runtime/flight.h
+++ b/runtime/flight.h
@@ -15,6 +15,23 @@ void flight_init(void); // prof_start calls it
void flight_frame(uint64_t frame, uint64_t now_ns, uint64_t interval_ns, const uint64_t blocked_ns[],
const volatile int64_t counts[]);
+// Texture events of the recorded frames (vk_texture.c), written with each dump as events.tsv: a texture made (with how
+// its content started), uploaded, uploaded again, released by the game, taken back after a release, or destroyed. Each
+// carries the frame whose counts in frames.tsv it is part of. Any thread: the ring takes no lock. A no-op until
+// flight_init turned the recorder on.
+enum { FLIGHT_TEX_NEW, FLIGHT_TEX_UP, FLIGHT_TEX_REUP, FLIGHT_TEX_RELEASE, FLIGHT_TEX_TAKEBACK, FLIGHT_TEX_DROP,
+ FLIGHT_TEX_N };
+// The texture's flags: a depth or stencil image, mirrored (the CPU writes its storage), bound into pool memory, an ASTC
+// texture, written by the GPU (VkTexImage.gpu_owned).
+enum { FLIGHT_TF_DEPTH = 1, FLIGHT_TF_STENCIL = 2, FLIGHT_TF_MIRRORED = 4, FLIGHT_TF_BOUND = 8, FLIGHT_TF_ASTC = 16,
+ FLIGHT_TF_GPU = 32 };
+// detail: for NEW how the content started (vk_texture_start_name), for UP and REUP the WriteTexels calls since the last
+// upload, for DROP why (vk_texture_drop_name). us: an upload's time, else 0.
+void flight_tex(int kind, uint64_t addr, uint32_t format, uint32_t w, uint32_t h, uint32_t layers, uint32_t flags,
+ uint32_t detail, uint32_t us);
+const char *vk_texture_start_name(int start); // vk_texture.c: the names of Tex.og_start and of drop_tex's reasons
+const char *vk_texture_drop_name(int why);
+
// gfx_present.c: the present thread blits every presented frame into a host-visible ring of n thumbnails.
void vk_thumbs_enable(int n); // before the first present; n slots of FLIGHT_THUMB_W x FLIGHT_THUMB_H
void vk_thumbs_frame(uint64_t frame); // the present thread names the frame of the vk_present_image call that follows
diff --git a/runtime/vk_texture.c b/runtime/vk_texture.c
index 9b7414a8..52769c3e 100644
--- a/runtime/vk_texture.c
+++ b/runtime/vk_texture.c
@@ -16,6 +16,7 @@
#include
#include "cfg.h"
+#include "flight.h"
#include "gfx.h"
#include "host.h"
#include "nvn_api.h"
@@ -1609,6 +1610,17 @@ static const char *const start_names[START_N] = {
"started on its own", "filled from storage over a live entry", "content copied from a live entry",
"content copied from a released entry", "started empty over a live entry", "started empty over a released entry",
"content copied from the released CPU-written entry it replaced (split fill)", "filled from its storage (split fill)"};
+const char *vk_texture_start_name(int start) { return start >= 0 && start < START_N ? start_names[start] : "?"; }
+const char *vk_texture_drop_name(int why) { return why >= 0 && why < DROP_N ? drop_names[why] : "?"; }
+
+// An entry's event for the flight recorder (flight.h flight_tex, FLIGHT_TF_*): a no-op while the recorder is off.
+static void flight_ev(const Tex *e, int kind, uint32_t detail, uint32_t us) {
+ if (!flight_on) return;
+ uint32_t fl = (e->f.depth ? FLIGHT_TF_DEPTH : 0) | (e->f.stencil ? FLIGHT_TF_STENCIL : 0) |
+ (e->pub.mirrored ? FLIGHT_TF_MIRRORED : 0) | (e->bound_to ? FLIGHT_TF_BOUND : 0) |
+ (e->f.astc_bw ? FLIGHT_TF_ASTC : 0) | (e->pub.gpu_owned ? FLIGHT_TF_GPU : 0);
+ flight_tex(kind, e->key.addr, (uint32_t)e->t.d.format, e->pub.width, e->pub.height, e->pub.layers, fl, detail, us);
+}
static uint64_t frames_now(void) { return __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED); }
@@ -1695,6 +1707,7 @@ static void write_back_n(Tex **v, int n);
static uint64_t n_drop_wb; // entries written back before their drop since the last perf line
static void drop_tex(Tex *e, int why) {
+ flight_ev(e, FLIGHT_TEX_DROP, (uint32_t)why, 0);
if (why == DROP_COMMIT || why == DROP_IDLE_COMMIT) commit_drop_note(e);
if (e->gpu_dirty && why != DROP_OVERWRITTEN && why != DROP_SHUTDOWN) {
uint32_t now;
@@ -3181,6 +3194,7 @@ static void upload(Tex *e) {
} else {
n_uploads++, perf_count(PERF_CNT_TEX_UP);
}
+ flight_ev(e, e->uploaded ? FLIGHT_TEX_REUP : FLIGHT_TEX_UP, e->wt_n, (uint32_t)(dt / 1000));
e->wt_n = 0;
e->uploaded = 1;
e->n_up++;
@@ -3652,6 +3666,7 @@ static Tex *entry_of(const tex_t *t) {
origin_made(e);
perf_count(PERF_CNT_TEX_NEW);
start_at(e, &k, 0);
+ flight_ev(e, FLIGHT_TEX_NEW, e->og_start, 0);
e->next_at = nvn_hm_get(&by_addr, &k.addr);
nvn_hm_put(&by_addr, &k.addr, e);
nvn_hm_put(&by_key, &k, e);
@@ -3661,6 +3676,7 @@ static Tex *entry_of(const tex_t *t) {
e->samp_frame = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED); // new: not disused before it is drawn
}
if (e->released) {
+ flight_ev(e, FLIGHT_TEX_TAKEBACK, 0, 0);
e->samp_frame = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED); // in use again, as a new one
uint64_t gap = e->samp_frame - e->rel_frame;
if (gap > e->back_gap) e->back_gap = gap > UINT32_MAX ? UINT32_MAX : (uint32_t)gap;
@@ -3725,6 +3741,7 @@ static void wb_cfg_init(void) {
static int wb_wanted(const Tex *e) { return e->t.L.size <= wb_limit || e->run < WB_RUN; }
static void mark_released(Tex *e) { // tex_lock held; e is not released
+ flight_ev(e, FLIGHT_TEX_RELEASE, 0, 0);
e->released = 1;
e->rel_epoch = submit_epoch;
e->rel_frame = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED);