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:
chrg6
2026-11-13 17:20:25 -05:00
parent 0472734008
commit af8e2a323b
9 changed files with 216 additions and 15 deletions

View File

@@ -715,6 +715,12 @@ of `runtime/perf.c` `perf_present`) or to how `tools/perf_summary.py` reads them
with them and checks the present percentiles, the present thread's averages and the hand-off share the tool prints, with them and checks the present percentiles, the present thread's averages and the hand-off share the tool prints,
and that the lines of older logs, without those fields, still parse. Any Python, no game files. and that the lines of older logs, without those fields, still parse. Any Python, no game files.
A change to what the frame-time summary charges to an address-space walk of `NX2NAVI_MEM_CENSUS`
(`runtime/perf_census.h` `census_charge`, `runtime/perf.c` `ft_frame`) passes `python tests\census_charge_check.py`,
which builds the header alone: a walk before, after or around a frame, one still running, the charge taken from the
replay part first, capped by the overlap and the parts, and 100,000 random frames in which the census part never passes
the frame and the parts keep their sum. No GPU, no game files.
The lifter (`recomp/`) and the CPU headers (`runtime/cpu.h`, `runtime/simd.h`) are checked against Unicorn. Every lifter The lifter (`recomp/`) and the CPU headers (`runtime/cpu.h`, `runtime/simd.h`) are checked against Unicorn. Every lifter
change passes these, all run with `.venv`: change passes these, all run with `.venv`:

View File

@@ -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. // 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); 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); 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 // 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. // 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); void perf_set_overlay(int mode);

View File

@@ -11,6 +11,7 @@
#include "gfx.h" #include "gfx.h"
#include "host.h" #include "host.h"
#include "nvn_api.h" #include "nvn_api.h"
#include "perf_census.h"
#include "perfmon.h" #include "perfmon.h"
#include "prof.h" #include "prof.h"
#include "stutter_map.h" #include "stutter_map.h"
@@ -329,10 +330,13 @@ static void hc_report(uint64_t frame) {
plat_event_set(&hc_ev); 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) { static void mem_snapshot(uint64_t frame) {
uint64_t t0 = kern_now_ns();
vk_mem_snapshot(frame); vk_mem_snapshot(frame);
sizes_log(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) { __attribute__((weak)) int vk_mem_vram(uint64_t *used, uint64_t *budget, int *exact) {
(void)used, (void)budget, (void)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 // 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 // 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. // 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 // "census" is the address-space walk of NX2NAVI_MEM_CENSUS: census_check's on the presenting thread after a present,
// present: its time is charged to the next frame, which it delays. "hand-off" is the wait for the present thread to // 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
// take the previous picture (PERF_HANDOFF): its own pacing and its present call, an overlay's frame limiter included. // 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. // 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", 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 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 { static struct {
uint64_t t0, frames, sum, over25, over50, over100; uint64_t t0, frames, sum, over25, over50, over100;
uint32_t hist[FT_BINS]; 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; int best = FT_C_OTHER;
uint64_t census_ns = census_part_ns; uint64_t census_ns = census_part_ns;
census_part_ns = 0; 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)) { if (d > 25000000ull || (stutter_map_on && d >= 12000000ull)) {
part[FT_C_CENSUS] = census_ns < d ? census_ns : d; 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_PIPE] = frame_ns[PERF_PIPE];
part[FT_C_XLATE] = frame_ns[PERF_XLATE]; part[FT_C_XLATE] = frame_ns[PERF_XLATE];
part[FT_C_TEX] = frame_ns[PERF_TEX]; 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; uint64_t known = 0;
for (int c = 0; c < FT_C_OTHER; c++) known += part[c]; for (int c = 0; c < FT_C_OTHER; c++) known += part[c];
part[FT_C_OTHER] = d > known ? d - known : 0; 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++) for (int c = 0; c < FT_C_N; c++)
if (part[c] > part[best]) best = 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 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(); 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); int grown = host >= census_high + (1ull << 30) && (!census_ns || t0 - census_ns >= CENSUS_GAP_NS);
if (frame % 3600 && !grown) return; 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; 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 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); else mem_guest_census(frame);
@@ -1108,5 +1139,9 @@ void perf_present(uint64_t frame) {
sum = max = n = ab_b = 0; sum = max = n = ab_b = 0;
memset(wait_ns, 0, sizeof wait_ns); memset(wait_ns, 0, sizeof wait_ns);
win_start = now; 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
View 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;
}

View File

@@ -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. // 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, // 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 // 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 // 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, // window loses the focus in play (perf.c session_report) and at exit,
@@ -42,7 +42,7 @@
int stutter_map_on; int stutter_map_on;
#define SM_AREAS 128 // areas kept by name; later ones share the last slot #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_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_MIN_NS 12000000.0 // a hitch is at least this long...
#define SM_HITCH_ANY_NS 25000000ull // ...and any frame over this is one #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, // 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", 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) { static int area_cmp(const void *x, const void *y) {
const Area *a = &areas[*(const int *)x], *b = &areas[*(const int *)y]; const Area *a = &areas[*(const int *)x], *b = &areas[*(const int *)y];

View File

@@ -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, " 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), "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)); 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 (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) if ((rt_trace & RT_TRACE_PERF) && frame % 60 == 0)
@@ -4558,10 +4559,17 @@ static void census_main(void *arg) {
free(arg); free(arg);
const double M = 1048576.0; const double M = 1048576.0;
uint64_t priv = 0, wc = 0, wcn = 0, now; 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); int have = plat_mem_census(&priv, &wc, &wcn);
uint64_t w1 = kern_now_ns();
perf_census_walk(0);
if (have) { if (have) {
rt_log("mem: process frame %llu: private %.0f MiB, write-combined %.0f MiB in %llu allocations", rt_log("mem: process frame %llu: private %.0f MiB, write-combined %.0f MiB in %llu allocations; the walk %.3f s "
(unsigned long long)j.frame, priv / M, wc / M, (unsigned long long)wcn); "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; uint64_t used = 0, budget = 0;
int exact = 0; int exact = 0;
if (!vk_mem_vram(&used, &budget, &exact)) used = 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 // 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 // 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. // 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; static int census_on = -1;
if (census_on < 0) census_on = cfg_bool("NX2NAVI_MEM_CENSUS"); 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); CensusJob *j = malloc(sizeof *j);
if (j) *j = (CensusJob){c, frame}; if (j) *j = (CensusJob){c, frame};
if (!j || !plat_thread_spawn(census_main, j, "mem census", 0)) free(j), plat_xchg32(&census_busy, 0); if (!j || !plat_thread_spawn(census_main, j, "mem census", 0)) free(j), plat_xchg32(&census_busy, 0);

View File

@@ -0,0 +1,77 @@
// runtime/perf_census.h: the part of a frame an address-space walk overlapped moves to the frame's census part, from
// its replay part first and then from its unattributed part, never more than the overlap and never past the frame.
// Built and run by tests/census_charge_check.py; prints each failed check and exits 1 on any failure.
#include <stdio.h>
#include <stdlib.h>
#include "perf_census.h"
static int fails;
#define CHECK(c, ...) do { if (!(c)) { fails++; printf("FAIL line %d: ", __LINE__); printf(__VA_ARGS__); printf("\n"); } } while (0)
#define MS 1000000ull
typedef struct { uint64_t replay, other, census, moved; } Parts;
// A frame of d ms ending at now ms, with replay / unattributed / census parts as given, and a walk w0..w1 ms (0: none
// seen; w1 0: still running).
static Parts charge(uint64_t now, uint64_t d, uint64_t w0, uint64_t w1, uint64_t replay, uint64_t other,
uint64_t census) {
Parts p = {replay * MS, other * MS, census * MS, 0};
p.moved = census_charge(now * MS, d * MS, w0 * MS, w1 * MS, &p.replay, &p.other, &p.census);
return p;
}
int main(void) {
// no walk seen, and a walk that ended before the frame or started after it
Parts p = charge(1000, 40, 0, 0, 30, 10, 0);
CHECK(p.moved == 0 && p.replay == 30 * MS && p.other == 10 * MS, "no walk: moved %llu", (unsigned long long)p.moved);
p = charge(1000, 40, 800, 950, 30, 10, 0);
CHECK(p.moved == 0 && p.census == 0, "a walk before the frame: moved %llu", (unsigned long long)p.moved);
p = charge(1000, 40, 1000, 0, 30, 10, 0);
CHECK(p.moved == 0, "a walk that starts as the frame ends: moved %llu", (unsigned long long)p.moved);
// a walk that ended 10 ms into the frame: 10 ms from the replay
p = charge(1000, 40, 900, 970, 30, 10, 0);
CHECK(p.moved == 10 * MS && p.replay == 20 * MS && p.other == 10 * MS && p.census == 10 * MS,
"partial overlap: replay %llu other %llu census %llu", (unsigned long long)p.replay, (unsigned long long)p.other,
(unsigned long long)p.census);
// a walk inside the frame, longer than the replay part: the replay's 15 ms, then 10 ms of the 20 unattributed
p = charge(1000, 60, 950, 975, 15, 20, 0);
CHECK(p.moved == 25 * MS && p.replay == 0 && p.other == 10 * MS && p.census == 25 * MS,
"a walk inside the frame: replay %llu other %llu census %llu", (unsigned long long)p.replay,
(unsigned long long)p.other, (unsigned long long)p.census);
// a walk still running (w1 0, or the end of the walk before it): it reaches now
p = charge(1000, 40, 985, 0, 30, 10, 0);
CHECK(p.moved == 15 * MS && p.replay == 15 * MS, "a running walk: moved %llu", (unsigned long long)p.moved);
p = charge(1000, 40, 985, 700, 30, 10, 0);
CHECK(p.moved == 15 * MS && p.replay == 15 * MS, "a running walk with the last one's end: moved %llu",
(unsigned long long)p.moved);
// the overlap caps the charge; the parts cap it too (nothing taken from the other parts of the frame)
p = charge(1000, 100, 900, 0, 5, 3, 0);
CHECK(p.moved == 8 * MS && p.replay == 0 && p.other == 0 && p.census == 8 * MS, "parts smaller than the overlap: "
"moved %llu", (unsigned long long)p.moved);
// never past the frame: census_check's own walk already took 35 of its 40 ms
p = charge(1000, 40, 900, 0, 30, 10, 35);
CHECK(p.moved == 5 * MS && p.census == 40 * MS && p.replay == 25 * MS, "census at the frame's length: moved %llu, "
"census %llu", (unsigned long long)p.moved, (unsigned long long)p.census);
// random frames: the census part never passes the frame, the moved time never passes the overlap, and the sum of
// the three parts is kept
srand(7);
for (int i = 0; i < 100000; i++) {
uint64_t now = 1000 + (uint64_t)(rand() % 1000), d = 1 + (uint64_t)(rand() % 300);
uint64_t w0 = (uint64_t)(rand() % 2000), w1 = rand() % 3 ? w0 + (uint64_t)(rand() % 400) : 0;
uint64_t census = (uint64_t)(rand() % (int)d), replay = (uint64_t)(rand() % 300), other = (uint64_t)(rand() % 300);
p = charge(now, d, w0, w1, replay, other, census);
uint64_t lo = now - d > w0 ? now - d : w0, hi = w1 && w1 < now ? w1 : now;
uint64_t ov = w0 && hi > lo ? (hi - lo) * MS : 0;
int ok = p.census <= d * MS && p.moved <= ov && p.replay + p.other + p.census == (replay + other + census) * MS &&
p.census == census * MS + p.moved && (p.other == other * MS || p.replay == 0);
CHECK(ok, "case %d: now %llu d %llu walk %llu-%llu: moved %llu of overlap %llu, census %llu", i,
(unsigned long long)now, (unsigned long long)d, (unsigned long long)w0, (unsigned long long)w1,
(unsigned long long)p.moved, (unsigned long long)ov, (unsigned long long)p.census);
if (!ok) break;
}
if (fails) return 1;
printf("census_charge_check: all checks passed\n");
return 0;
}

View File

@@ -0,0 +1,41 @@
"""Build and run tests/census_charge_check.c: runtime/perf_census.h, which charges the part of a frame that an
address-space walk of NX2NAVI_MEM_CENSUS overlapped to the frame's census part (perf.c ft_frame): from the replay part
first, then from the unattributed part, never more than the overlap and never past the frame.
No GPU, no game files, any Python.
Usage: census_charge_check.py
"""
import os
import shutil
import subprocess
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
MSYS_BIN = Path(r'C:\msys64\ucrt64\bin')
def compiler():
"""$CC, else MSYS2 UCRT64's clang, else clang on PATH (as tests/diff/common.py picks it, without its Unicorn)."""
if os.environ.get('CC'):
return os.environ['CC']
if os.name == 'nt' and (MSYS_BIN / 'clang.exe').exists():
return str(MSYS_BIN / 'clang.exe')
return shutil.which('clang') or 'clang'
def main():
work = ROOT / 'build' / 'test' / 'census_charge'
work.mkdir(parents=True, exist_ok=True)
exe = work / ('census_charge_check.exe' if os.name == 'nt' else 'census_charge_check')
env = dict(os.environ)
if os.name == 'nt' and MSYS_BIN.exists():
env['PATH'] = str(MSYS_BIN) + os.pathsep + env.get('PATH', '')
subprocess.check_call([compiler(), '-std=gnu11', '-O1', '-Wall', '-Werror', '-I', str(ROOT / 'runtime'),
str(ROOT / 'tests' / 'census_charge_check.c'), '-o', str(exe)], env=env)
return subprocess.run([str(exe)], env=env).returncode
if __name__ == '__main__':
sys.exit(main())

View File

@@ -19,7 +19,7 @@ void rt_log(const char *fmt, ...) {
// perf.c's causes, in its order (FT_C_*); the frames below are charged to the last, unattributed // perf.c's causes, in its order (FT_C_*); the frames below are charged to the last, unattributed
static const char *const names[] = {"pipeline", "translation", "texture", "fence", "pacing", "hand-off", "replay", static const char *const names[] = {"pipeline", "translation", "texture", "fence", "pacing", "hand-off", "replay",
"census", "unattributed"}; "census", "report", "unattributed"};
#define OTHER (SM_CAUSES - 1) #define OTHER (SM_CAUSES - 1)
_Static_assert(sizeof names / sizeof *names == SM_CAUSES, "names follows perf.c's FT_C_* list"); _Static_assert(sizeof names / sizeof *names == SM_CAUSES, "names follows perf.c's FT_C_* list");
static uint64_t frame_no; static uint64_t frame_no;