Write the end of the run to the log file past its 64 MiB cap, up to a 4 MiB reserve counted on its own.

main.c log_sink wrote nothing to the file once g_log_bytes passed LOG_CAP: every later line, the exit summary,
"frametime: exit", the stutter map's exit lines and FATAL and crash lines included, went to stderr only, which a
game started from the launcher or Explorer does not keep. A trace = perf session reaches the cap in about 90
minutes (the fresh 0.10.0 log grew 12 KB/s), and the play-test asks for an hour or more of exactly that. A reserve
compared against g_log_bytes would not help: that counter adds every line, dropped ones too, so 4 MiB of dropped
lines (under 6 minutes at that rate) would use it up before the end came.

The new rt_log_ending marks the process as ending; rt_exit_begin (the orderly ends) and perf_summary_flush (the
frame-time summary, also on the cheats rebuild exit) call it, and rt_log_sync (a fatal error, the crash report)
counts as ending too. The exit summary and the stutter map are written before rt_log_sync, so keying on
g_log_final alone would have missed them. While ending, a line past the cap goes to the file through
g_log_tail, a counter of the bytes written past the cap, up to LOG_END_RESERVE (4 MiB), after one line saying the
end of the run follows. The cut line says the end will still come. The file can end 4 MiB past the cap.

perf_summary_flush now calls rt_log_ending before ft_stop, so tests/exit_order_check.py's static check of it wants
ft_stop before ft_report rather than first. The check also prints one closing line for the static checks and the
program together: the program's own "ok" line had hidden a failing static check.

Not reproduced before the change (no log here reached the cap). The new tests/log_cap_check.py builds log_sink
with a 4 KiB cap and a 2 KiB reserve: past the cap ordinary lines are dropped with one cut line, and after
rt_log_ending or rt_log_sync the end's lines fill the reserve after one "end follows" line, however much was
dropped before. exit_order_check (rt_exit_begin marks the end once; exit code 0 at this commit), logq_check 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:25:40 -05:00
parent 8c1ec42406
commit bbea3ce2a0
6 changed files with 168 additions and 9 deletions

View File

@@ -118,7 +118,10 @@ static volatile uint32_t exit_tid; // the thread that runs the orderly end, 0
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 (__atomic_compare_exchange_n(&exit_tid, &was, me, 0, __ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE)) {
rt_log_ending(); // the end's lines reach the log file past its cap (main.c log_sink)
return 1;
}
if (was == me) return 0;
for (;;) plat_sleep_ms(1000);
}

View File

@@ -24,6 +24,7 @@ void rt_vlogf(const char *fmt, va_list ap);
void rt_log_raw(const char *p, size_t n);
const char *rt_log_path(void); // the log file, NULL when this run writes none
void rt_log_sync(void); // writes the queued log lines now; later lines are written as they are made
void rt_log_ending(void); // the process is ending: its lines reach the log file past its 64 MiB cap
enum { RT_TRACE_SVC = 1, RT_TRACE_HOSTCALL = 2, RT_TRACE_IPC = 4, RT_TRACE_FS = 8, RT_TRACE_GFX = 16, RT_TRACE_PERF = 32,
RT_TRACE_SCHED = 64, RT_TRACE_AUDIO = 128, RT_TRACE_PRESENT = 256 };
extern unsigned rt_trace; // from NX2NAVI_TRACE=svc,host,ipc,fs,gfx,perf,sched,audio,present (or all)

View File

@@ -62,8 +62,15 @@ unsigned rt_trace;
// `2> file` keep their own file. Each start moves the earlier logs to nx2navi.1.log .. nx2navi.4.log. A file stops at 64 MiB
// (NX2NAVI_TRACE can write that much). Lines from before log_open wait in a static buffer: mem_init runs first so that
// nothing else lands in the guest address space, and an earlier open would put the CRT's heap there.
// The end of the run still reaches a file past its cap: once the process is ending (rt_log_ending at the start of an
// orderly end, gfx_present.c rt_exit_begin, and of the frame-time summary, perf.c perf_summary_flush; rt_log_sync at a
// fatal error and in the crash report), up to LOG_END_RESERVE more bytes are written, counted on their own. A
// trace = perf session reaches the cap in about 90 minutes, and every line after it, the exit summary, "frametime:
// exit", the stutter map and FATAL lines included, went to stderr only, which a game started from the launcher or
// Explorer does not keep.
#define LOG_KEEP 5
#define LOG_CAP (64ull << 20)
#define LOG_END_RESERVE (4ull << 20)
enum { LOG_EARLY, LOG_FILE, LOG_OFF };
static int g_log_state;
static PlatAppend *g_log_file;
@@ -76,6 +83,10 @@ static uint64_t g_log_bytes;
// crash report.
static PlatMutex g_err_lock = PLAT_MUTEX_INIT;
static volatile int32_t g_log_final;
static volatile int32_t g_log_ending; // rt_log_ending ran: the process is ending
static uint64_t g_log_tail; // bytes of the end the file was given past LOG_CAP
void rt_log_ending(void) { __atomic_store_n(&g_log_ending, 1, __ATOMIC_RELEASE); }
// The log's destinations: the file first, then stderr. logq.c calls it: from the log thread in a game run, else from
// the thread that logs. A write to stderr can block for as long as its reader likes: a console window holds every write
@@ -89,9 +100,21 @@ static void log_sink(const char *p, size_t n) {
uint64_t at = __atomic_fetch_add(&g_log_bytes, n, __ATOMIC_RELAXED);
if (at + n <= LOG_CAP) {
plat_append_write(g_log_file, p, n);
} else if (at <= LOG_CAP) { // the first write past the cap says so, once
static const char cut[] = "[host] log: the file reached 64 MiB, the rest of this run goes to stderr only\n";
plat_append_write(g_log_file, cut, sizeof cut - 1);
} else {
if (at <= LOG_CAP) { // the first write past the cap says so, once
static const char cut[] = "[host] log: the file reached 64 MiB, the rest of this run goes to stderr only, "
"but for the lines of its end\n";
plat_append_write(g_log_file, cut, sizeof cut - 1);
}
if (__atomic_load_n(&g_log_ending, __ATOMIC_ACQUIRE) || __atomic_load_n(&g_log_final, __ATOMIC_ACQUIRE)) {
uint64_t t = __atomic_fetch_add(&g_log_tail, n, __ATOMIC_RELAXED);
if (!t) {
static const char end[] = "[host] log: the end of the run follows; the lines between the cap and it "
"were not written to this file\n";
plat_append_write(g_log_file, end, sizeof end - 1);
}
if (t + n <= LOG_END_RESERVE) plat_append_write(g_log_file, p, n);
}
}
}
// without the file stderr is the log (run_game.py, `2> file`), and it is waited for

View File

@@ -764,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)
rt_log_ending(); // the summary and the stutter map reach the log file past its cap (main.c log_sink)
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) {