From 7b0dbeb18d65b7bde19864f9b922daa336651af1 Mon Sep 17 00:00:00 2001 From: chrg6 Date: Fri, 13 Nov 2026 12:41:44 -0500 Subject: [PATCH] Write the session's reports when the window loses the focus in play, more often in play, and log every end of the game in the launcher. The session of 2026-11-13 ended 8 s after its window lost the focus, after 7 minutes of play, with none of the game's exit lines: no frame-time summary of its last period, no stutter map of its last minute and no growth ranking at all, as the ranking came every 10 minutes. When the window loses the focus in play, the present thread now writes the frame-time summary so far (starting a new period), the stutter map and the growth ranking at its next present, at most once a minute; formatting only, nothing asks the device. The growth ranking comes every 2 minutes of play instead of 10, the stutter map every 5 instead of 10. A process that ends through exit() rather than plat_exit now writes the log thread's queue last, after the frame-time summary's exit handler, instead of losing it. The launcher logged a game's end only for a nonzero code, so the session's end with code 0 left no line anywhere. It now logs every end of a game it started, and when the game's log has no line of the game's own end (the window's close, the game's ExitProcess, a timed run's end, a FATAL or exit line), it notes the end there too. Co-Authored-By: Claude Opus 5.5 --- docs/architecture.md | 4 ++-- docs/launcher.md | 3 ++- launcher/ui/app.py | 37 +++++++++++++++++++++++++++++++++++++ runtime/gfx_present.c | 1 + runtime/host.h | 1 + runtime/perf.c | 34 ++++++++++++++++++++++++++++++++++ runtime/stutter_map.c | 5 +++-- runtime/stutter_map.h | 2 +- runtime/vk.h | 1 + runtime/vk_memory.c | 11 ++++++++++- tests/launcher_test.py | 24 ++++++++++++++++++++++++ 11 files changed, 116 insertions(+), 7 deletions(-) diff --git a/docs/architecture.md b/docs/architecture.md index 40b4c3fb..ea2a9ea1 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -283,8 +283,8 @@ keeps per-core data without locks and relies on that, so the host models it (`ru tables, the texture cache and its decode temporaries, the log queue, the flight recorder), the data of the driver's pipeline caches (asked once a minute on the autosave thread) and the rest; `mem: growth` names what grew by more than 64 MiB since the report before and since the first report in play. Every - ten minutes of play and at exit, `mem: growth ranking` ranks the categories by their growth since play began and - marks those that only climbed (`mem_growth.h`). + two minutes of play, when the window loses the focus in play and at exit, `mem: growth ranking` ranks the categories + by their growth since play began and marks those that only climbed (`mem_growth.h`). - Released textures by age: the game releases a texture when what uses it leaves the view and takes it back when that returns; the cache keeps the released image so that the take-back finds it. In play the take-backs came within a minute of the release or never, so a released texture goes once it has gone unused for a hold of three minutes, six diff --git a/docs/launcher.md b/docs/launcher.md index c3999d40..14debdc5 100644 --- a/docs/launcher.md +++ b/docs/launcher.md @@ -574,7 +574,8 @@ files), `start.pyw` on a version that cannot be imported (exit code 86 at once, back to), a cut RomFS image and the free space, the object cache written while the compile runs and kept when it is cut, a RomFS step that does not repeat the lift, the update's duration text and the warm-up's bar, a damaged dump named by its file and an unexpected error by its message, the file check's result in its row and on home, the -game's FATAL line after exit codes 70 and 71, the job count by free memory and the build's priority class, a line +game's FATAL line after exit codes 70 and 71, an end with code 0 noted in the game's log when it has no line of the +game's own end, the job count by free memory and the build's priority class, a line handler that raises and a log that cannot be written, one forced update check a minute and GitHub's reset time, a game folder with characters outside the Basic Multilingual Plane (its `modules.c` compiled when a C compiler is there), the file check at both piece sizes against the check as it was, the disk space of every step and the build diff --git a/launcher/ui/app.py b/launcher/ui/app.py index 4d447099..d483eb89 100644 --- a/launcher/ui/app.py +++ b/launcher/ui/app.py @@ -167,6 +167,33 @@ def last_fatal(game_dir): return '' +# The lines the game writes when it ends on its own: the window's close (runtime/gfx_present.c gfx_window_closed), the +# game's own exit (runtime/svc.c, ExitProcess), a timed run's end (runtime/boot.c), a fatal error and the crash report's +# line for a fault on the way out (runtime/main.c). +OWN_EXIT_LINES = ('gfx: window closed', 'guest ExitProcess', 'boot: frames', 'FATAL: ', 'exit: ') + + +def log_has_own_exit(game_dir): + """Whether the end of logs/nx2navi.log holds a line of the game's own end (OWN_EXIT_LINES) for its latest run; None + when the log cannot be read. A line this launcher added for an earlier run's end (note_exit_in_log) ends the + search.""" + if not game_dir: + return None + try: + with open(Path(game_dir) / 'logs' / 'nx2navi.log', 'rb') as f: + f.seek(max(0, f.seek(0, 2) - FATAL_TAIL)) + lines = f.read().decode('utf-8', 'replace').splitlines() + except OSError: + return None + for line in reversed(lines): + if line.startswith('[launcher] the game ended'): + return False + body = line.split('] ', 1)[1] if line.startswith('[') and '] ' in line else line + if body.startswith(OWN_EXIT_LINES): + return True + return False + + def note_exit_in_log(game_dir, text): """Adds the exit code to the end of the game's log, so a log a player sends shows how the run ended even when the process died before it could write a line itself.""" @@ -1910,6 +1937,7 @@ class App: self.root.after(1000, self.watch_game) return code = self.game_proc.returncode if self.game_proc else 0 + started = self.game_proc is not None self.game_proc = None self.root.deiconify() self.root.lift() @@ -1927,6 +1955,15 @@ class App: self.ctx.log(f'the game ended with code {text}') self.message = f'The game ended with code {text}. Its log is logs\\nx2navi.log in the game folder.' note_exit_in_log(self.game_dir(), text) + elif started: + # A run this launcher started that ended with code 0 is logged too, and when the game's log has no line of + # its own end, the end goes into that log: a session of 2026-11-13 stopped in play with code 0, no FATAL + # line and none of the game's exit lines, and nothing said how it had ended. + if log_has_own_exit(self.game_dir()) is False: + self.ctx.log('the game ended with code 0, and its log has no line of its own end') + note_exit_in_log(self.game_dir(), "0 (the log has no line of the game's own end before this one)") + else: + self.ctx.log('the game ended with code 0') if self.install_staged('the game exited'): return self.show('home', self.focus) diff --git a/runtime/gfx_present.c b/runtime/gfx_present.c index a214c266..4089021b 100644 --- a/runtime/gfx_present.c +++ b/runtime/gfx_present.c @@ -75,6 +75,7 @@ void gfx_window_focus_changed(int in) { plat_local_time(&t); rt_log("gfx: window %s the focus at %04d-%02d-%02d %02d:%02d:%02d local time", in ? "gained" : "lost", t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec); + if (!in) perf_focus_lost(); // the session's reports, in case the process is ended from outside next (perf.c) } // The window was closed: the process ends with it, once a save Commit still running in the background is diff --git a/runtime/host.h b/runtime/host.h index 06ee0c20..6ab82edc 100644 --- a/runtime/host.h +++ b/runtime/host.h @@ -102,6 +102,7 @@ void perf_present(uint64_t frame); void perf_cpu_start(void); // the process's logical processors (NX2NAVI_CACHE_DIE, NX2NAVI_ONE_PER_CORE), at start int perf_cpu_on_cache_die(void); // 1 while the process runs on the die with the largest L3 cache alone (perf.c cpu_placement) void perf_summary_flush(void); // prints the frame-time summary of the running period (exit); the 60 s lines come from perf_present +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 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) diff --git a/runtime/perf.c b/runtime/perf.c index 9dced1af..4bda4055 100644 --- a/runtime/perf.c +++ b/runtime/perf.c @@ -53,9 +53,15 @@ void perf_gpu_busy(uint64_t ns) { plat_add64(&gpu_busy_total, (int64_t)ns); } uint64_t perf_gpu_busy_total(void) { return (uint64_t)__atomic_load_n(&gpu_busy_total, __ATOMIC_RELAXED); } +// Registered before the handlers below, so it runs after them (atexit's order is the reverse): a process that ends +// through exit() rather than plat_exit, whose log sync this is, gets the lines of the frame-time summary, the stutter +// map and everything queued before them into the log instead of losing them with the log thread's queue. +static void log_sync_at_exit(void) { rt_log_sync(); } + void prof_start(void) { flight_init(); stutter_map_init(); + atexit(log_sync_at_exit); atexit(perf_summary_flush); // boot.c's _exit path goes through prof_write instead perf_base = (rt_trace & RT_TRACE_PERF) || flight_on; perf_on = perf_base || overlay == 2; @@ -586,6 +592,33 @@ static void ft_report(uint64_t now, const char *tag) { } __attribute__((weak)) void gfx_exit_log(void) {} +__attribute__((weak)) void vk_mem_rank_log(const char *when) { (void)when; } + +// The session's reports when the window loses the focus in play: the frame-time summary of the period so far (which +// starts a new period), the stutter map and the memory report's growth ranking, at most every SESSION_REPORT_GAP_NS. +// They are written at the exit too (perf_summary_flush, vk_mem_exit_log), but a process ended from outside, or by a +// path that skips them, leaves none: a session of 2026-11-13 ended 8 s after its window lost the focus, after 7 minutes +// of play, with no ranking (one every 10 minutes then) and no map of its last minute. gfx_present.c +// gfx_window_focus_changed asks for them; the present thread writes them at its next present (perf_present). Formatting +// only: no device call, no wait. +#define SESSION_REPORT_GAP_NS 60000000000ull +static volatile int32_t session_due; + +void perf_focus_lost(void) { + if (frame_step_world_us() >= 0) plat_xchg32(&session_due, 1); // in play: the loaded save's world clock runs +} + +static void session_report(uint64_t now) { + static uint64_t last; + if (last && now - last < SESSION_REPORT_GAP_NS) return; + last = now; + if (summary_t0) { + ft_report(now, "at focus loss"); + summary_t0 = now; + } + stutter_report("at focus loss"); + vk_mem_rank_log("at focus loss, after"); +} // The renderer's per-frame texture work (vk_texture.c): closes the frame's sums; d is the frame's interval. __attribute__((weak)) void vk_texstat_frame(uint64_t frame, uint64_t d) { (void)frame, (void)d; } @@ -891,6 +924,7 @@ void perf_present(uint64_t frame) { uint64_t now = kern_now_ns(), d = last ? now - last : 0; prof_sampler_frame(frame, now, last ? d : 0); 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); ab_b += rt_ab(); cpu_placement(); reserve_sweep(frame); diff --git a/runtime/stutter_map.c b/runtime/stutter_map.c index 0bbf3565..6c81c1ca 100644 --- a/runtime/stutter_map.c +++ b/runtime/stutter_map.c @@ -15,7 +15,8 @@ // What a hitch went to comes from the frame-time summary's split (perf.c ft_frame: pipeline, translation, texture, // fence, pacing, replay, 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 -// SM_LINES_PER_S a second and SM_LINES_MAX in all; the counts go on past that. Every SM_REPORT_NS of play and at exit, +// 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, // "stutter map" lines rank the areas by their hitches and the pipelines by the time draws lost to them, each with // whether this install had recorded it before (the pipeline list or an earlier run) and whether one of its shaders was // translated in play rather than prepared. @@ -49,7 +50,7 @@ int stutter_map_on; #define SM_RING 256 // draws that met something missing, between two presents #define SM_LINES_MAX 4000 #define SM_LINES_PER_S 5 -#define SM_REPORT_NS 600000000000ull // the map every 10 minutes of play +#define SM_REPORT_NS 300000000000ull // the map every 5 minutes of play #define SM_TOP_AREAS 40 #define SM_TOP_PIPES 40 diff --git a/runtime/stutter_map.h b/runtime/stutter_map.h index 2e3b6e6e..42584060 100644 --- a/runtime/stutter_map.h +++ b/runtime/stutter_map.h @@ -21,4 +21,4 @@ void stutter_ready(int mesh, const uint8_t *vi, const uint8_t *fi, double ms, in // the last one is the part nothing recorded explains), cause the one the frame is charged to. void stutter_frame(uint64_t frame, uint64_t now, uint64_t d, const uint64_t *part, const char *const *names, int n, int cause); -void stutter_report(const char *tag); // the map so far; every 10 minutes of play and at exit +void stutter_report(const char *tag); // the map so far; every 5 minutes of play, at a focus loss in play and at exit diff --git a/runtime/vk.h b/runtime/vk.h index ea37f7ad..9f3a041b 100644 --- a/runtime/vk.h +++ b/runtime/vk.h @@ -499,6 +499,7 @@ void vk_mem_lost_report(void); // a line with the video memor float vk_mem_device_used_mb(void); // device-local memory this process holds (the driver's budget // figure), MB; -1 without VK_EXT_memory_budget. Any thread. void vk_mem_stage_log(const char *stage); // one line after a boot stage: device memory, process commit +void vk_mem_rank_log(const char *when); // the growth ranking now, "when" leading its minutes of play void vk_mem_exit_log(void); // at the process's end: the memory report's growth ranking of the // session (vk_memory.c growth_take), once it reached play // VRAM in use and its budget, summed over the DEVICE_LOCAL heaps. exact 1: VK_EXT_memory_budget (the whole process), diff --git a/runtime/vk_memory.c b/runtime/vk_memory.c index 6e762570..11ad31c3 100644 --- a/runtime/vk_memory.c +++ b/runtime/vk_memory.c @@ -5415,7 +5415,9 @@ uint64_t vk_mem_give_back(void) { // their locks (a fraction of a millisecond, once a report), the C runtime heap's summary. Nothing is counted per frame. #define IDLE_MIN 10 // pipelines no lookup reached for this many minutes count as unused #define GROWTH_STEP (64ull << 20) // growth_take names a category that grew by more than this -#define RANK_EVERY_NS (600ull * 1000000000ull) // the ranking's period in play +// the ranking's period in play: every 2 minutes, as a session ended without its exit lines after 7 minutes of play had +// no ranking at all with one every 10 (2026-11-13); a ranking is one line of figures the book already holds +#define RANK_EVERY_NS (120ull * 1000000000ull) #define HEAP_SLOW_NS 2000000ull // a heap summary that took longer is not asked for again typedef struct { @@ -5608,6 +5610,13 @@ void vk_mem_exit_log(void) { plat_mutex_unlock(&grow_lock); } +// The same ranking at a moment of the caller's (perf.c session_report, when the window loses the focus). +void vk_mem_rank_log(const char *when) { + plat_mutex_lock(&grow_lock); + if (gbook.play) rank_log_locked(when, plat_now_ns()); + plat_mutex_unlock(&grow_lock); +} + static void beyond_log(VkCtx *c, uint64_t frame, const VkPhysicalDeviceMemoryBudgetPropertiesEXT *bp, const VmaBudget *vb, const ReportFig *f) { VkPipeMem pm; diff --git a/tests/launcher_test.py b/tests/launcher_test.py index d50ad5d8..1cc2ac4a 100644 --- a/tests/launcher_test.py +++ b/tests/launcher_test.py @@ -5153,6 +5153,30 @@ class WhilePlayingTest(Tmp): self.assertEqual(app.last_fatal(self.tmp), '') self.assertEqual(app.last_fatal(''), '') + def test_an_end_with_code_0_is_logged_and_noted_without_the_games_exit_line(self): + """A run ended with code 0 goes into launcher.log; when the game's log has no line of the game's own end (the + window's close, its ExitProcess, a FATAL line), the end is noted in that log too.""" + try: + from launcher.ui import app + except ImportError as e: + self.skipTest(f'no window toolkit: {e}') + a, log = self.ended(0, '[host] log: x\n[host] gfx: window closed; exiting\n[host] perf: after it\n') + a.ctx.log.assert_called_once_with('the game ended with code 0') + self.assertNotIn('[launcher]', log.read_text(encoding='utf-8')) + a, log = self.ended(0, '[host] log: x\n[host] perf: frame 39660: the last line of play\n') + a.ctx.log.assert_called_once_with('the game ended with code 0, and its log has no line of its own end') + text = log.read_text(encoding='utf-8') + self.assertIn("[launcher] the game ended with code 0 (the log has no line of the game's own end", text) + self.assertIs(app.log_has_own_exit(self.tmp), False) # the note ends the search for the next run + log.write_text('FATAL: x\n', encoding='utf-8') + self.assertIs(app.log_has_own_exit(self.tmp), True) + log.write_text('[host] guest ExitProcess at main+0x10; exiting\n', encoding='utf-8') + self.assertIs(app.log_has_own_exit(self.tmp), True) + log.unlink() + self.assertIsNone(app.log_has_own_exit(self.tmp)) + a, _ = self.ended(0, None) # no log: logged as an end with code 0, nothing noted + a.ctx.log.assert_called_once_with('the game ended with code 0') + def test_the_fatal_line_is_looked_for_in_the_logs_end(self): try: from launcher.ui import app