diff --git a/runtime/boot.c b/runtime/boot.c index d3c1e754..a1288b48 100644 --- a/runtime/boot.c +++ b/runtime/boot.c @@ -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); } diff --git a/runtime/fs.h b/runtime/fs.h index 25a00c7c..4e81625c 100644 --- a/runtime/fs.h +++ b/runtime/fs.h @@ -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. diff --git a/runtime/fs_host.c b/runtime/fs_host.c index c2d9284b..8a995912 100644 --- a/runtime/fs_host.c +++ b/runtime/fs_host.c @@ -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) { diff --git a/runtime/gfx_present.c b/runtime/gfx_present.c index d7eb75f4..a8c1d78d 100644 --- a/runtime/gfx_present.c +++ b/runtime/gfx_present.c @@ -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"); diff --git a/runtime/host.h b/runtime/host.h index b9de4516..0b43c003 100644 --- a/runtime/host.h +++ b/runtime/host.h @@ -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. diff --git a/runtime/logq.c b/runtime/logq.c index b3edf835..565815ac 100644 --- a/runtime/logq.c +++ b/runtime/logq.c @@ -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 diff --git a/runtime/logq.h b/runtime/logq.h index 993bb34f..eabc48ab 100644 --- a/runtime/logq.h +++ b/runtime/logq.h @@ -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) diff --git a/runtime/perf.c b/runtime/perf.c index 8dd6f575..c75c9fe9 100644 --- a/runtime/perf.c +++ b/runtime/perf.c @@ -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); diff --git a/runtime/svc.c b/runtime/svc.c index cd29ecd1..61356f29 100644 --- a/runtime/svc.c +++ b/runtime/svc.c @@ -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); diff --git a/tests/exit_order_check.py b/tests/exit_order_check.py new file mode 100644 index 00000000..f11405ee --- /dev/null +++ b/tests/exit_order_check.py @@ -0,0 +1,195 @@ +"""The orderly end of the process (runtime/gfx_present.c rt_exit_begin and rt_exit_steps): the steps run in one order, +with what a kill of the process must not lose (a save Commit in flight, RivaTuner Statistics Server's profile, the +controller's rumble, the settings, the frame-time summary) before the slow pipeline save and the final save flush +last; it runs once, and a second caller on another thread stays where it is. The frame-time summary of the exit +(runtime/perf.c ft_stop) waits for a present thread's update under way and keeps later ones out. Builds those +functions into a test program against stand-ins, and checks statically that every orderly end goes through them. +No game files. + +Usage: python tests/exit_order_check.py [path to clang]""" +import os +import re +import shutil +import subprocess +import sys +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] +RT = ROOT / 'runtime' +MSYS_BIN = Path(r'C:\msys64\ucrt64\bin') + + +def compiler(): + if len(sys.argv) > 1: + return sys.argv[1] + if os.environ.get('CC'): + return os.environ['CC'] + if os.name == 'nt' and (MSYS_BIN / 'clang.exe').exists(): + return str(MSYS_BIN / 'clang.exe') + return shutil.which('clang') or 'clang' + + +def strip_comments(text): + return re.sub(r'//[^\n]*', '', text) + + +def function(source, signature): + start = source.index(signature) + brace = source.index('{', start) + depth, end = 1, brace + 1 + while depth: + depth += (source[end] == '{') - (source[end] == '}') + end += 1 + return source[start:end] + + +def calls_in_order(body, names): + """The position of each name's first call in body (comments left out), or None for a name that is missing.""" + code = strip_comments(body) + return [m.start() if (m := re.search(r'\b' + re.escape(n) + r'\s*\(', code)) else None for n in names] + + +STEPS = ['fs_save_wait', 'rtss_limit_restore', 'input_rumble_hd_stop', 'cfg_save', 'vk_pipeline_prewarm_marker_clear', + 'prof_write', 'vk_mem_exit_log', 'logq_flush', 'vk_pipeline_save_exit', 'fs_save_flush'] + +HARNESS = r''' +#include +#include +#include +#include +static uint32_t plat_thread_id(void) { return GetCurrentThreadId(); } +static void plat_sleep_ms(uint32_t ms) { Sleep(ms); } +enum { RT_EXIT_SETTINGS = 1 }; +static char seen[1024]; +#define STEP(name) static void name(void) { strcat(seen, #name " "); } +STEP(fs_save_wait) STEP(rtss_limit_restore) STEP(input_rumble_hd_stop) STEP(vk_pipeline_prewarm_marker_clear) +STEP(prof_write) STEP(vk_mem_exit_log) STEP(logq_flush) STEP(vk_pipeline_save_exit) STEP(fs_save_flush) +static int cfg_save(void) { strcat(seen, "cfg_save "); return 1; } +''' + +MAIN = r''' +static volatile LONG second_back = -1; +static DWORD WINAPI second(void *arg) { (void)arg; second_back = rt_exit_begin(); return 0; } + +static volatile LONG updating; +static DWORD WINAPI present(void *arg) { // one present thread update that takes ms milliseconds + int ms = (int)(intptr_t)arg; + if (!ft_enter()) { ft_leave(); return 0; } + updating = 1; + Sleep(ms); + updating = 0; + ft_leave(); + return 0; +} + +int main(int argc, char **argv) { + int fails = 0; + rt_exit_steps(RT_EXIT_SETTINGS); + printf("window close, guest ExitProcess: %s\n", seen); + if (strcmp(seen, argv[1])) printf("FAIL: want %s\n", argv[1]), fails++; + seen[0] = 0; + rt_exit_steps(0); + printf("--seconds: %s\n", seen); + if (strcmp(seen, argv[2])) printf("FAIL: want %s\n", argv[2]), fails++; + + int first = rt_exit_begin(), again = rt_exit_begin(); + HANDLE t = CreateThread(NULL, 0, second, NULL, 0, NULL); + int parked = WaitForSingleObject(t, 300) == WAIT_TIMEOUT && second_back == -1; + printf("once: first caller %d, its own thread again %d, another thread %s\n", first, again, + parked ? "stays in rt_exit_begin" : "came back"); + if (first != 1 || again != 0 || !parked) printf("FAIL: the end must run once\n"), fails++; + + // an update under way: ft_stop waits for it; after ft_stop no update starts + HANDLE p = CreateThread(NULL, 0, present, (void *)(intptr_t)20, 0, NULL); + while (!updating) Sleep(1); + ULONGLONG t0 = GetTickCount64(); + ft_stop(); + ULONGLONG waited = GetTickCount64() - t0; + int busy_after = updating; + WaitForSingleObject(p, INFINITE); + int later = ft_enter(); + ft_leave(); + printf("frame-time summary: ft_stop waited %llu ms for the update under way (%s), a later update %s\n", + (unsigned long long)waited, busy_after ? "still running: FAIL" : "done", + later ? "still added: FAIL" : "kept out"); + if (busy_after || later) fails++; + // an update that does not end: ft_stop gives up after about 50 ms + __atomic_store_n(&ft_ending, 0, __ATOMIC_SEQ_CST); + p = CreateThread(NULL, 0, present, (void *)(intptr_t)2000, 0, NULL); + while (!updating) Sleep(1); + t0 = GetTickCount64(); + ft_stop(); + waited = GetTickCount64() - t0; + printf("frame-time summary: ft_stop with an update that hangs came back after %llu ms\n", (unsigned long long)waited); + if (waited > 1000) printf("FAIL: ft_stop must be bounded\n"), fails++; + printf("exit_order_check: %s\n", fails ? "FAILED" : "ok"); + fflush(stdout); + ExitProcess(fails ? 1 : 0); // the parked thread never returns +} +''' + + +def static_checks(): + bad = 0 + gp = (RT / 'gfx_present.c').read_text(encoding='utf-8') + svc = (RT / 'svc.c').read_text(encoding='utf-8') + boot = (RT / 'boot.c').read_text(encoding='utf-8') + perf = (RT / 'perf.c').read_text(encoding='utf-8') + exit07 = svc[svc.index('case 0x07:'):] + exit07 = exit07[:exit07.index('svc_fatal(')] + paths = [('gfx_present.c gfx_window_closed', function(gp, 'void gfx_window_closed(void)'), 'rt_exit_steps(RT_EXIT_SETTINGS)'), + ('svc.c ExitProcess (0x07)', exit07, 'rt_exit_steps(RT_EXIT_SETTINGS)'), + ('boot.c rt_frame_presented (--seconds)', function(boot, 'void rt_frame_presented('), 'rt_exit_steps(0)')] + for name, body, steps in paths: + code = strip_comments(body) + a, b, c = code.find('rt_exit_begin()'), code.find(steps), code.rfind('plat_exit(') + own = [s for s in STEPS if re.search(r'\b' + s + r'\s*\(', code)] + ok = 0 <= a < b < c and not own + print(f'{name}: {"rt_exit_begin, then " + steps + ", then plat_exit" if ok else "FAIL"}' + + (f'; calls exit steps of its own: {", ".join(own)}' if own else ''), flush=True) + bad += not ok + steps = function(gp, 'void rt_exit_steps(unsigned what)') + pos = calls_in_order(steps, ['rtss_limit_restore', 'vk_pipeline_save_exit']) + ok = None not in pos and pos[0] < pos[1] + print(f'rt_exit_steps: RivaTuner\'s profile put back before the pipeline save: {"ok" if ok else "FAIL"}') + bad += not ok + flush = strip_comments(function(perf, 'void perf_summary_flush(void)')) + present = strip_comments(function(perf, 'void perf_present(uint64_t frame)')) + e, f, s, l = (present.find(x) for x in ('ft_enter()', 'ft_frame(', 'session_report(', 'ft_leave()')) + ok = flush[flush.index('{') + 1:].strip().startswith('ft_stop();') and 0 <= e < f < s < l + print(f'perf.c: perf_summary_flush starts with ft_stop, perf_present updates inside ft_enter/ft_leave: ' + f'{"ok" if ok else "FAIL"}') + bad += not ok + return bad + + +def main(): + sys.stdout.reconfigure(line_buffering=True) + bad = static_checks() + if os.name != 'nt': + print('exit_order_check: the program part runs on Windows only') + return 1 if bad else 0 + gp = (RT / 'gfx_present.c').read_text(encoding='utf-8') + perf = (RT / 'perf.c').read_text(encoding='utf-8') + tid = re.search(r'static volatile uint32_t exit_tid;[^\n]*', gp).group(0) + ftvars = re.search(r'static volatile int32_t ft_ending, ft_busy;', perf).group(0) + src = '\n'.join([HARNESS, tid, function(gp, 'int rt_exit_begin(void)'), function(gp, 'void rt_exit_steps(unsigned what)'), + ftvars, function(perf, 'static int ft_enter(void)'), function(perf, 'static void ft_leave(void)'), + function(perf, 'static void ft_stop(void)'), MAIN]) + work = ROOT / 'build' / 'test' / 'exit_order' + work.mkdir(parents=True, exist_ok=True) + (work / 'exit_order_check.c').write_text(src, encoding='utf-8') + exe = work / 'exit_order_check.exe' + env = dict(os.environ) + if MSYS_BIN.exists(): + env['PATH'] = str(MSYS_BIN) + os.pathsep + env.get('PATH', '') + subprocess.check_call([compiler(), '-O1', '-g', '-Wall', '-Wno-unused-function', str(work / 'exit_order_check.c'), + '-o', str(exe)], env=env) + want_all = ' '.join(STEPS) + ' ' + want_seconds = ' '.join(s for s in STEPS if s != 'cfg_save') + ' ' + rc = subprocess.run([str(exe), want_all, want_seconds], env=env, timeout=60).returncode + return 1 if bad or rc else 0 + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/tests/logq_check.c b/tests/logq_check.c index a11b89f8..1d35b596 100644 --- a/tests/logq_check.c +++ b/tests/logq_check.c @@ -296,9 +296,40 @@ int main(int argc, char **argv) { at = out_n; for (int i = 0; i < THREADS; i++) next[i] = 0; - // 9. logq_sync: what is queued is written before it returns, and later lines are written by their own thread. The - // lines are logged together just before it, so the log thread is still gathering them (the clock says whether - // this thread was in time for that): the batch a fatal error, a crash or the end of the process finds queued. + // 9. logq_flush: what is queued is written before it returns, inside the gathering time, and the queue stays on. + // With the log thread stuck in a write (the test holds its io lock) it gives up within its bound and writes + // nothing, and the line is written once the write is over. + uint64_t f0 = plat_now_ns(); + for (int i = 0; i < 20; i++) { + char line[64]; + int n = snprintf(line, sizeof line, "t 3 %d \n", i); + logq_write(line, (size_t)n); + } + early = out_n - at; + in_time = plat_now_ns() - f0 < LOGQ_GATHER_NS / 2; + logq_flush(); + if (in_time) CHECK(!early, "%zu bytes were written before logq_flush, inside the gathering time", early); + CHECK(check_lines(at, next, ¬es) == 20, "logq_flush returned before the queued lines were written"); + CHECK(__atomic_load_n(&Q.on, __ATOMIC_ACQUIRE), "logq_flush turned the queue off"); + at = out_n; + plat_mutex_lock(&Q.io); + logq_write("t 3 20 \n", 8); + f0 = plat_now_ns(); + logq_flush(); + uint64_t gave_up = plat_now_ns() - f0; + size_t through = out_n - at; + plat_mutex_unlock(&Q.io); + settle(); + CHECK(gave_up < 2000000000ull && !through, "logq_flush with the io lock taken: back after %.0f ms, %zu bytes written", + gave_up / 1e6, through); + CHECK(check_lines(at, next, ¬es) == 1, "the line queued while logq_flush gave up"); + at = out_n; + for (int i = 0; i < THREADS; i++) next[i] = 0; + + // 10. logq_sync: what is queued is written before it returns, and later lines are written by their own thread. The + // lines are logged together just before it, so the log thread is still gathering them (the clock says + // whether this thread was in time for that): the batch a fatal error, a crash or the end of the process finds + // queued. __atomic_store_n(&sink_ms, 20, __ATOMIC_RELEASE); uint64_t s0 = plat_now_ns(); for (int i = 0; i < 50; i++) {