Open the log's end reserve only when the process is ending, not at a bug capture's log sync in play.

The review of the log tail change found that main.c log_sink opened the 4 MiB reserve past the cap on g_log_final as
well as on g_log_ending. bugcap.c write_log_tail calls rt_log_sync in play, which sets g_log_final, so after a bug
capture in a trace = perf session past the cap, ordinary play lines filled the reserve (about 6 minutes at 12 KB/s),
the end's lines were dropped again, and the "end of the run follows" line came mid-run.

The reserve is now keyed on g_log_ending alone, as E-10's design had it. The ends that relied on rt_log_sync now call
rt_log_ending first: dispatch.c fatal_end and main.c crash_handler right after their guard, vk_device.c
lost_wait_exit's watchdog before its sync, and plat_exit through the new main.c log_sync_ending (plat_exit_hook in a
game run), so every path that ends through plat_exit marks the end too. rt_exit_begin and perf_summary_flush call it
as before.

Not reproduced in the game before the change. tests/log_cap_check.py gains a capture case (rt_log_sync in play, 1000
more play lines, then the end): on the code before this change 58 play lines took the reserve, the end line came at
the sync and no end line reached the file; now no play line passes the cap and the end's lines fill the reserve. A
fatal case runs rt_log_ending and then rt_log_sync, and static checks hold crash_handler, log_sync_ending, fatal_end
and lost_wait_exit to calling rt_log_ending before rt_log_sync. log_cap_check, fatal_exit_check (with a stand-in for
rt_log_ending) and the compile checks of main.c, dispatch.c and vk_device.c pass, with no new -Wall warnings.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
chrg6
2026-11-13 18:26:35 -05:00
parent b8b57cfd53
commit 313ef09081
5 changed files with 78 additions and 21 deletions

View File

@@ -103,6 +103,7 @@ int rt_ending_enter(void) {
static void fatal_end(int code, const char *fmt, va_list ap) __attribute__((noreturn));
static void fatal_end(int code, const char *fmt, va_list ap) {
if (!rt_ending_enter()) plat_terminate(code); // a fatal error inside this thread's own report: no second one
rt_log_ending(); // the report reaches the log file past its cap (main.c log_sink)
rt_log_sync(); // main.c: the lines queued for the log thread, then these, written here
rt_log_raw("FATAL: ", 7);
rt_vlogf(fmt, ap);

View File

@@ -63,11 +63,13 @@ unsigned rt_trace;
// (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
// orderly end, gfx_present.c rt_exit_begin, of the frame-time summary, perf.c perf_summary_flush, of a fatal error,
// dispatch.c fatal_end, of the crash report, crash_handler, of the lost device's watchdog, vk_device.c lost_wait_exit,
// and in plat_exit, log_sync_ending), 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.
// Explorer does not keep. rt_log_sync alone does not open the reserve: a bug capture calls it in play
// (bugcap.c write_log_tail), and the play's lines after it would fill the reserve before the end came.
#define LOG_KEEP 5
#define LOG_CAP (64ull << 20)
#define LOG_END_RESERVE (4ull << 20)
@@ -79,8 +81,8 @@ static char g_log_early[32768];
static size_t g_log_early_n;
static uint64_t g_log_bytes;
// stderr's writes (log_sink). g_log_final: rt_log_sync has run, so the process is ending: an exit, a fatal error or a
// crash report.
// stderr's writes (log_sink). g_log_final: rt_log_sync has run: an exit, a fatal error, a crash report or a bug
// capture (bugcap.c).
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
@@ -106,7 +108,7 @@ static void log_sink(const char *p, size_t n) {
"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)) {
if (__atomic_load_n(&g_log_ending, __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 "
@@ -132,6 +134,12 @@ void rt_log_sync(void) {
logq_sync();
}
// plat_exit's first step in a game run (plat_exit_hook): the process ends here, so its last lines reach the file.
static void log_sync_ending(void) {
rt_log_ending();
rt_log_sync();
}
// The console window was closed, or Ctrl+C or Ctrl+Break pressed in it (platform.h plat_console_setup): the end of a
// closed game window, which writes the settings, the caches and the saves and logs the exit (gfx_present.c
// gfx_window_closed). Left to Windows, a closed console ended the process with nothing saved.
@@ -227,7 +235,7 @@ static void log_open(int game) {
// A game run writes its log from the log thread (logq.c), whether the lines go to a log file or to stderr alone.
const char *e = cfg_get("NX2NAVI_LOG_ASYNC");
if (!game || (e && !strcmp(e, "0"))) return;
plat_exit_hook = rt_log_sync;
plat_exit_hook = log_sync_ending;
if (logq_async()) rt_log("log: lines are written by a thread of their own (NX2NAVI_LOG_ASYNC)");
else rt_log("log: no log thread; every line is written by the thread that logs it");
}
@@ -366,6 +374,7 @@ static void crash_handler(const PlatCrash *x) {
// shared with the fatal error's end (dispatch.c rt_ending_enter): another thread's fault or fatal error waits there
// while this report is written; a fault inside this thread's own report ends the process at once
if (!rt_ending_enter()) plat_exit(71);
rt_log_ending(); // the report reaches the log file past its cap (log_sink)
if (__atomic_load_n(&mode_done, __ATOMIC_ACQUIRE) && x->module) {
rt_log("exit: %s (%#x) in %s while the process was ending, after its work was done and saved%s: the run ends "
"with its own code %d", x->what, x->code, *x->module ? x->module : "code outside every module",

View File

@@ -880,6 +880,7 @@ static void lost_wait_exit(void *arg) {
int watchdog = arg != NULL;
for (int t = 0; t < LOST_EXIT_MS + (watchdog ? 0 : 2000); t += 50) plat_sleep_ms(50);
if (watchdog) {
rt_log_ending(); // its line reaches the log file past its cap (main.c log_sink)
rt_log_sync(); // the queue's locks only tried (bounded): their holder may be the thread that hangs
rt_log("vk: the end of the run did not finish in %d s; ending it", LOST_EXIT_MS / 1000);
}