Run every orderly exit through one sequence that puts RivaTuner's profile back before the slow pipeline save.

The window's close, the guest's ExitProcess and the end of a --seconds run each ran their own list. All three
waited for the pipeline save (up to 5 s for running builds, then the write) before they put RivaTuner Statistics
Server's profile back, stopped HD rumble, wrote the frame-time summary and flushed the save, so a console close,
a session end or an "End task" that killed the process in that wait left the game's limit in RTSS's profile (the
next run takes it for the player's own), a buzzing controller and no exit summary. ExitProcess also lacked
cfg_save and the rumble stop, and it and the window's close had no shared guard, so both could run at once and
write the summary twice while the present thread still added to it.

gfx_present.c rt_exit_begin is the one guard (the first caller runs; another thread waits for good; the same
thread again returns, and svc.c ends the process then rather than resume guest code). rt_exit_steps runs, in
order: fs_save_wait (a Commit under way finishes, none is refused), rtss_limit_restore, input_rumble_hd_stop,
cfg_save (not for --seconds), the prewarm marker clear, prof_write, vk_mem_exit_log, a non-final log flush
(the new logq.c logq_flush, bounded trylocks, queue left on), vk_pipeline_save_exit, and fs_save_flush last.
perf.c ft_stop keeps the present thread's frame-time updates out of the exit's summary and waits at most 50 ms,
on the exiting thread, for one under way. The restore relies on rtss_limit.c's closed flag to stay restored
while frames go on during the pipeline save. The log order changes: "frametime: exit" and the stutter-map exit
lines now come before the pipeline save's lines.

Not reproduced before the change. tests/exit_order_check.py (new: the step order, the once-guard with a second
thread, ft_stop's wait and bound, and statically that the three exits use the helper with the RTSS restore
before the pipeline save), tests/logq_check.py (with a logq_flush case), tests/save_share_check.py and the
compile checks pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
chrg6
2026-11-13 17:09:13 -05:00
parent 2c59addd62
commit e39f71df39
11 changed files with 344 additions and 36 deletions

View File

@@ -324,13 +324,10 @@ void rt_frame_presented(const tex_t *t, uint64_t n) {
stats_line();
fflush(stdout);
fflush(stderr);
input_rumble_hd_stop(); // a controller on HD rumble gets the neutral value before the exit
fs_save_flush();
vk_pipeline_prewarm_marker_clear(); // an orderly end, not a driver crash in the boot's builds (vk_pipeline.c pm_skipped)
vk_mem_exit_log(); // the memory report's ranking of the session
vk_pipeline_save_exit(); // no-op without the Vulkan back end
prof_write();
rtss_limit_restore();
// the orderly end the window's close takes (gfx_present.c rt_exit_steps), without nx2navi.ini: a scripted run's
// end is no player's; a close already ending the process keeps this thread in rt_exit_begin
if (!rt_exit_begin()) plat_exit(0);
rt_exit_steps(0);
fflush(stderr);
plat_exit(0);
}

View File

@@ -66,6 +66,8 @@ int fs_save_busy(void); // a save is being wr
// or save data changed in the last 3 s and not committed
void fs_save_flush(void); // wait for a save Commit still running in the background;
// every exit path calls it before the process ends
void fs_save_wait(void); // wait for a save Commit under way, refusing none later
// (the orderly end's first step, gfx_present.c rt_exit_steps)
// A shared save (save_share.c). fs_save_export: visit(ctx, path below the save with '/', bytes, size) for every file of
// the save the game would load next, under the commit lock; visit returns 0 to fail. The number of files (0: no save
// yet), -1 when one could not be read or visit failed.

View File

@@ -573,6 +573,10 @@ static void save_wait_commit(void) {
plat_mutex_unlock(&pend_lock);
}
// The orderly end's first step (gfx_present.c rt_exit_steps): a Commit under way finishes before the quick put-backs and
// the slow pipeline save, and a later one is still taken; fs_save_flush, the end's last step, refuses those.
void fs_save_wait(void) { save_wait_commit(); }
// The window closed: a Commit already started finishes first, and none starts afterwards, as the process exit would
// cut it off halfway (the startup recovery then keeps the previous one; here it is kept outright).
void fs_save_flush(void) {

View File

@@ -23,6 +23,7 @@
#include "gfx.h"
#include "gfx_wsys.h"
#include "input.h"
#include "logq.h"
#include "nvn_api.h"
#include "ov.h"
#include "platform/platform.h"
@@ -106,27 +107,55 @@ void gfx_window_focus_changed(int in) {
if (!in) perf_focus_lost(); // the session's reports, in case the process is ended from outside next (perf.c)
}
// ---- the orderly end: the window's close (gfx_window_closed), the guest's ExitProcess (svc.c) and the end of a
// --seconds run (boot.c rt_frame_presented)
// Once per process, whichever comes first. The window's close and the console's (main.c console_closed) can come
// together, or the guest's ExitProcess beside either: each ran its own sequence, so two pipeline saves and two
// frame-time summaries were written while both raced. 1 for the first caller; a caller on another thread waits here
// for good while the first ends the process; 0 for the first caller's own thread coming back (a message the window
// thread handles inside the sequence), which goes on with the sequence it is in.
static volatile uint32_t exit_tid; // the thread that runs the orderly end, 0 before one started
int rt_exit_begin(void) {
uint32_t me = plat_thread_id(), was = 0;
if (__atomic_compare_exchange_n(&exit_tid, &was, me, 0, __ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE)) return 1;
if (was == me) return 0;
for (;;) plat_sleep_ms(1000);
}
// What every orderly end saves and puts back, in this order. What a kill of the process must not lose and what is quick
// comes first: Windows ends a closed console about 5 s after the close, a session's end and an "End task" sooner, and
// the pipeline save alone can take that long (vk_pipeline_save_exit waits up to 5 s for the builds that run). So a
// Commit in flight is finished, RivaTuner Statistics Server's profile and the controller's rumble are put back, the
// settings, the boot prebuild's marker, the frame-time summary and the memory ranking are written, and the log reaches
// the disk before the pipeline save starts. The final save flush comes last: the game's threads run until plat_exit,
// and a Commit the game starts during the pipeline save is still taken and waited for. RT_EXIT_SETTINGS: nx2navi.ini
// gets the menu's changes (not in a scripted --seconds run, whose end is no player's).
void rt_exit_steps(unsigned what) {
fs_save_wait(); // a Commit already under way: finished, nothing refused
// the game's frame limit out of the profile; rtss_limit.c keeps it out while frames go on during the pipeline save
rtss_limit_restore();
input_rumble_hd_stop(); // a controller on HD rumble gets the neutral value: nothing renews or stops it after the exit
if (what & RT_EXIT_SETTINGS) cfg_save(); // a menu change saves 400 ms later on the render thread (option_rows.cpp)
// The game ended while the boot still built its pipelines: not the driver crash the marker is there to catch. Left
// in place, the next start took it for one and every later start skipped the prebuild (vk_pipeline.c pm_skipped).
vk_pipeline_prewarm_marker_clear();
prof_write(); // "frametime: exit" and the stutter map; plat_exit runs no atexit handlers
vk_mem_exit_log(); // the memory report's ranking of the session
logq_flush(); // the lines so far on the disk before the slow part; the queue stays on
vk_pipeline_save_exit();
fs_save_flush(); // last: a Commit started since fs_save_wait finishes, and one after this is refused
}
// The window was closed: the process ends with it, once a save Commit still running in the background is
// done. After gfx_window_request_restart the executable then starts again with the same command line and environment
// (plat_restart), and the new process waits until this one is gone (main.c) before it moves the log and opens the
// RomFS and the saves.
void gfx_window_closed(void) {
// Once. The console's close (main.c console_closed) can come beside the window's: the later caller waits here
// while the first one saves and ends the process.
static volatile int32_t closing;
if (plat_xchg32(&closing, 1)) for (;;) plat_sleep_ms(1000);
if (!rt_exit_begin()) return;
int restart = plat_cas32(&g_restart, 0, 0) != 0;
rt_log("gfx: window closed; %s", restart ? "restarting" : "exiting");
cfg_save(); // a menu change saves 400 ms later on the render thread (option_rows.cpp); closing sooner kept the old file
// The window closed while the boot still built its pipelines: not the driver crash the marker is there to catch.
// Left in place, the next start took it for one and every later start skipped the prebuild (vk_pipeline.c pm_skipped).
vk_pipeline_prewarm_marker_clear();
vk_mem_exit_log(); // the memory report's ranking of the session
vk_pipeline_save_exit();
prof_write(); // plat_exit runs no atexit handlers
rtss_limit_restore(); // RivaTuner Statistics Server's profile as it was
input_rumble_hd_stop(); // a controller on HD rumble gets the neutral value: nothing renews or stops it after the exit
fs_save_flush(); // last: the game's threads still run until plat_exit, and a Commit after this one is refused
rt_exit_steps(RT_EXIT_SETTINGS);
if (restart) {
cfg_setenv("NX2NAVI_REEXEC", NULL); // the new process counts its own address-space retries (memory.c)
if (!plat_restart()) rt_log("gfx: the restart could not start the game again");

View File

@@ -111,6 +111,12 @@ void perf_summary_flush(void); // prints the frame-time summary
void perf_focus_lost(void); // the window lost the focus: the session's reports at the next present, in play
void gfx_exit_log(void); // back end's lines for the exit summary (weak no-op in prof_win.c)
void gfx_window_closed(void); // gfx_present.c (gfx_wsys.h): the end of a closed window; does not return
// gfx_present.c: the orderly end every exit the game asks for shares (the window's close, svc.c ExitProcess, boot.c
// --seconds). rt_exit_begin: 1 for the first caller; another thread waits there for good; 0 for that caller's own
// thread again. rt_exit_steps: the saves and the put-backs, the slow pipeline save late and the final save flush last.
int rt_exit_begin(void);
enum { RT_EXIT_SETTINGS = 1 }; // rt_exit_steps also writes the menu's changes to nx2navi.ini
void rt_exit_steps(unsigned what);
void perf_gpu_wait(uint64_t t0); // a wait for a GPU fence ended (any thread; the render thread's also counts as PERF_FENCE)
// GPU side of the 60-frame perf line, filled by the graphics back end when it set perf_gpu_stats. gpu_ns: GPU busy time
// since the previous call (timestamp queries, overlaps counted once), have_gpu 0 when the device has no timestamps.

View File

@@ -152,6 +152,28 @@ int logq_async(void) {
return 1;
}
// What is queued, written now on the calling thread, with the queue left on: a point the log is to have reached on the
// disk before something slow comes (the orderly end before its pipeline save, gfx_present.c rt_exit_steps). Each lock
// is tried for at most 100 ms: the log thread may be stuck in a write and hold io. Without both locks nothing is
// written here, since other threads still queue and write, and the log thread writes the lines when it can.
void logq_flush(void) {
if (!__atomic_load_n(&Q.on, __ATOMIC_ACQUIRE)) return;
int io = 0, q = 0;
for (int i = 0; i < 100 && !(io = plat_mutex_trylock(&Q.io)); i++) plat_sleep_ms(1);
if (!io) return;
for (int i = 0; i < 100 && !(q = plat_mutex_trylock(&Q.q)); i++) plat_sleep_ms(1);
if (!q) {
plat_mutex_unlock(&Q.io);
return;
}
char *b = Q.buf;
size_t n = Q.n;
Q.buf = Q.out, Q.out = b, Q.n = 0;
plat_mutex_unlock(&Q.q);
if (n) put_out(b, n);
plat_mutex_unlock(&Q.io);
}
void logq_sync(void) {
if (!plat_xchg32(&Q.on, 0)) return;
// Each lock is tried for half a second: the thread that holds it may be the one that crashed, or be stuck in the

View File

@@ -8,4 +8,5 @@ void logq_init(LogqSink sink); // before the first logq_write; line
int logq_async(void); // from now on the log thread writes them; 0 when it could not be started
void logq_write(const char *p, size_t n); // queues the bytes, or writes them when the queue is off or has no room
void logq_sync(void); // writes what is queued, on the calling thread, and turns the queue off
void logq_flush(void); // writes what is queued, on the calling thread, and leaves the queue on
size_t logq_bytes(void); // the queue's buffers, for the memory report (vk_memory.c system_log)

View File

@@ -507,6 +507,25 @@ static struct {
} ft;
static uint64_t summary_t0; // start of the current period (0: not started)
// The exit's summary (perf_summary_flush) runs on the exiting thread while the present thread may still add frames to
// `ft` and write its own reports (ft_frame, session_report), with no lock between them: the exit lines came out torn,
// or twice. ft_stop marks the end, after which the present thread adds nothing, and waits a little for an update
// already under way; it is the exiting thread that waits, never the present thread. Both sides are sequentially
// consistent, so either the present thread sees ft_ending or ft_stop sees it counted in ft_busy.
static volatile int32_t ft_ending, ft_busy;
static int ft_enter(void) {
__atomic_add_fetch(&ft_busy, 1, __ATOMIC_SEQ_CST);
return !__atomic_load_n(&ft_ending, __ATOMIC_SEQ_CST);
}
static void ft_leave(void) { __atomic_sub_fetch(&ft_busy, 1, __ATOMIC_SEQ_CST); }
static void ft_stop(void) {
__atomic_store_n(&ft_ending, 1, __ATOMIC_SEQ_CST);
for (int i = 0; i < 50 && __atomic_load_n(&ft_busy, __ATOMIC_SEQ_CST); i++) plat_sleep_ms(1);
}
// NX2NAVI_REPLAY_TIME=1: how the replay splits between the kinds of op it walks, reported with the frame-time summary.
// The replay is recorded on one thread, so these need no atomics. The kinds are PERF_RP_* in host.h.
// NX2NAVI_LOCK_TIME=1 (default 0): the replay threads time every lock they have to wait for (platform.h
@@ -745,6 +764,7 @@ static void session_report(uint64_t now) {
__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)
ft_stop();
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");
@@ -754,8 +774,9 @@ void perf_summary_flush(void) { // at exit (prof_write)
summary_t0 = 0;
}
// Returns the replay time since the previous present.
static uint64_t ft_frame(uint64_t frame, uint64_t now, uint64_t d) {
// Returns the replay time since the previous present. add 0: the exit's summary is written (ft_stop), so the frame
// is not added to it.
static uint64_t ft_frame(uint64_t frame, uint64_t now, uint64_t d, int add) {
int64_t rep = plat_xchg64(&frame_replay_ns, 0);
vk_texstat_frame(frame, d);
uint64_t f_cpu = (uint64_t)plat_xchg64(&frame_replay_cpu_ns, 0);
@@ -764,6 +785,7 @@ static uint64_t ft_frame(uint64_t frame, uint64_t now, uint64_t d) {
uint64_t f_hold = (uint64_t)plat_xchg64(&fr_hold_ns, 0);
uint32_t f_op = (uint32_t)fr_max_op;
for (int c = 0; c < PERF_RP_N; c++) f_ns[c] = (uint64_t)plat_xchg64(&fr_ns[c], 0), f_n[c] = (uint64_t)plat_xchg64(&fr_n[c], 0);
if (!add) return (uint64_t)rep;
if (!summary_t0) { // the first present starts the period; there is no interval yet
summary_t0 = now;
ft.t0 = now;
@@ -1080,8 +1102,10 @@ void perf_present(uint64_t frame) {
__atomic_fetch_add(&wait_ns[k], (uint64_t)a, __ATOMIC_RELAXED);
if (k == PERF_PACE) pace_any += (uint64_t)a;
}
uint64_t rep = ft_frame(frame, now, d); // d is 0 on the first present: it only starts the period
if (__atomic_load_n(&session_due, __ATOMIC_RELAXED) && plat_xchg32(&session_due, 0)) session_report(now);
int ft_add = ft_enter(); // 0 once the exit's summary is written
uint64_t rep = ft_frame(frame, now, d, ft_add); // d is 0 on the first present: it only starts the period
if (ft_add && __atomic_load_n(&session_due, __ATOMIC_RELAXED) && plat_xchg32(&session_due, 0)) session_report(now);
ft_leave();
ab_b += rt_ab();
cpu_placement();
reserve_sweep(frame);

View File

@@ -239,16 +239,13 @@ static void svc_body(Cpu *c, uint32_t imm, uint64_t pc) {
return;
}
case 0x29: get_info(c, pc); return;
case 0x07: { // ExitProcess, as closing the window (gfx_window_closed): pipeline cache, shader records and
// profile saved, logs flushed, exit code 0
case 0x07: { // ExitProcess, as closing the window: the orderly end (gfx_present.c rt_exit_steps), exit code 0
char at[160];
rt_log("guest ExitProcess at %s; exiting", ldr_describe(pc, at, sizeof at));
fs_save_flush();
vk_pipeline_prewarm_marker_clear(); // as the window's close (gfx_present.c gfx_window_closed)
vk_mem_exit_log();
vk_pipeline_save_exit();
prof_write();
rtss_limit_restore();
// once, shared with the window's close: a close already ending the process keeps this thread there; this
// thread coming back would resume guest code after its ExitProcess, so it ends the process instead
if (!rt_exit_begin()) plat_exit(0);
rt_exit_steps(RT_EXIT_SETTINGS);
fflush(NULL);
if (svc_test_exit_hook) return svc_test_exit_hook(0);
plat_exit(0);