diff --git a/runtime/dispatch.c b/runtime/dispatch.c index 76afb92b..82996542 100644 --- a/runtime/dispatch.c +++ b/runtime/dispatch.c @@ -57,6 +57,8 @@ void rt_fatal_notes(void) { for (int i = 0; i < n_fatal_notes; i++) fatal_notes[i](); } +__attribute__((weak)) void fs_save_crash_wait(void) {} // fs_host.c; builds without it (the unit checks) have no saves + void rt_fatal(const char *fmt, ...) { va_list ap; va_start(ap, fmt); @@ -72,6 +74,7 @@ void rt_fatal(const char *fmt, ...) { rt_log("fatal: memory commit: this process %.0f MB, the system %.0f of %.0f MB", proc, used, limit); rt_fatal_notes(); kern_free_cores_note(); + fs_save_crash_wait(); // a save the game already reported as done finishes first (bounded) prof_sampler_write(); // NX2NAVI_PROFILE: what was sampled until now, else a crash loses the whole profile // RivaTuner Statistics Server's profile as it was: left with the game's limit in it, the next run would take that // limit for the player's own (rtss_limit.c; it gives up after a second if the failing thread holds its lock) diff --git a/runtime/fs_host.c b/runtime/fs_host.c index 4d97db64..0a5f8d35 100644 --- a/runtime/fs_host.c +++ b/runtime/fs_host.c @@ -582,11 +582,30 @@ void fs_save_flush(void) { plat_mutex_unlock(&pend_lock); } +// The process is about to end on a crash or a fatal error (main.c crash_handler, dispatch.c rt_fatal). The game's +// Commit returns once its commit is queued, so the game may already have told the player the save is done while the +// commit thread still copies (95-135 ms): ending now would leave an unfinished copy, which the next start rolls back. +// The thread goes on while this one waits, for at most CRASH_WAIT_MS, without taking a lock (the failing thread may +// hold one). Not when the failing thread is the one committing: its commit cannot finish. +#define CRASH_WAIT_MS 2000 +static _Thread_local int committing; + +void fs_save_crash_wait(void) { + if (committing || !plat_cas32(&commit_pending, 0, 0)) return; + uint64_t t0 = plat_now_ms(); + while (plat_cas32(&commit_pending, 0, 0) && plat_now_ms() - t0 < CRASH_WAIT_MS) plat_sleep_ms(5); + rt_log("fs: a save commit was under way when the process failed: %s after %llu ms", + plat_cas32(&commit_pending, 0, 0) ? "not finished; the next start keeps the save before it" : "finished", + (unsigned long long)(plat_now_ms() - t0)); +} + static uint32_t commit_run(void) { + committing = 1; plat_mutex_lock(&tx_lock); uint32_t r = save_commit_locked(); plat_dec32(&save_writers); plat_mutex_unlock(&tx_lock); + committing = 0; plat_mutex_lock(&pend_lock); plat_dec32(&commit_pending); plat_mutex_unlock(&pend_lock); diff --git a/runtime/host.h b/runtime/host.h index f0bac455..9c0bb7b9 100644 --- a/runtime/host.h +++ b/runtime/host.h @@ -272,6 +272,7 @@ extern const char rt_port_version[]; // VERSION at the bui // ---- kernel (kernel.c) typedef struct RtThread RtThread; Cpu *kern_cpu(void); // current guest thread's Cpu, NULL on host threads +void fs_save_crash_wait(void); // fs_host.c: the crash report's and rt_fatal's bounded wait for a save Commit under way void kern_free_cores_note(void); // for crash reports: logs that free_cores = 1 lifted the // virtual core gate, nothing when the gate is on uint32_t kern_main_thread(Cpu **cpu); // main thread object and Cpu for the calling host thread diff --git a/runtime/main.c b/runtime/main.c index 529c5b14..cec414bb 100644 --- a/runtime/main.c +++ b/runtime/main.c @@ -313,6 +313,7 @@ static void crash_handler(const PlatCrash *x) { } rt_fatal_notes(); // the card, its driver and the memory settings in effect (vk_device.c fatal_note) kern_free_cores_note(); + fs_save_crash_wait(); // a save the game already reported as done finishes first (bounded; fs_host.c) rtss_limit_restore(); // after the report: RivaTuner Statistics Server's profile as it was fflush(stderr); prof_sampler_write(); // NX2NAVI_PROFILE: what was sampled until the crash (it gives up after 2 s if the sampler hangs)