From b23acff3015aa9ef9adb6f9c8165dd9177e2ff49 Mon Sep 17 00:00:00 2001 From: chrg6 Date: Fri, 13 Nov 2026 17:53:30 -0500 Subject: [PATCH] Leave a line in the log when a module ends the process with ExitProcess, and write the gathered lines at a focus loss. Two runs of 2026-11-13 (the 0.10.0 fresh install's nx2navi.1.log, 7,974,927 bytes, and the 0.9.2 test's nx2navi.3.log) ended seconds after "window lost the focus" with exit code 0, no WER event and no line of their own: an ExitProcess or exit() called by a module in the process and a kill from outside could not be told apart, and both lose the up to 250 ms of lines the log thread was gathering. A direct ExitProcess skips perf.c's atexit sync. (a) ExitProcess stops every other thread and then calls each module's detach, the executable's TLS callbacks included; TerminateProcess calls none. win32.c exit_note_tls, in the .CRT$XLB list beside _tls_used, writes on DLL_PROCESS_DETACH what the log queue holds (logq.c logq_peek: the bytes as they stand, no lock) and one line, "exit: the process is ending through ExitProcess, not one of the game's own ends", with the return addresses that led there. It runs under the loader lock with the other threads gone, so it uses no C runtime (hex written by hand) and does not ask the loader: the addresses are named from a module list noted at start and at each focus loss (plat_exit_note_modules: EnumProcessModules, two tables and a writers' lock the callback never takes), and RtlCaptureStackBackTrace and WriteFile take no lock of the process. plat_exit, plat_terminate and plat_reexec set the orderly flag (plat_exit_orderly), and main does once main_run has returned, so a tool mode's exit() leaves no line; a library's exit() during a game run does, naming ucrtbase and its caller. main.c log_open sets it up for a game run that writes a log file. Linux has empty stand-ins. (b) gfx_window_focus_changed calls the new main.c rt_log_focus_lost at a focus loss: logq_nudge makes the log thread write what it gathers at once (a flag log_main's gathering loop checks) and the module list is noted again. Not reproduced before the change (the two ends are unexplained). The new tests/exit_tls_check.py links win32.c and logq.c with a queued line still being gathered: ExitProcess from the program and exit() write the line and the note naming exit_tls_check.exe; TerminateProcess, plat_exit and a return after plat_exit_orderly write no note. tests/logq_check.c gains a nudge case, which fails with the flag left unset (252 ms) and passes with it. thread_end_check, fatal_exit_check, crash_stack_check, exit_order_check, save_share_check and the compile checks of every runtime file pass. Co-Authored-By: Claude Opus 5.5 --- runtime/gfx_present.c | 5 +- runtime/host.h | 1 + runtime/logq.c | 23 ++++++++- runtime/logq.h | 2 + runtime/main.c | 13 +++++ runtime/platform/linux.c | 5 ++ runtime/platform/platform.h | 10 ++++ runtime/platform/win32.c | 98 +++++++++++++++++++++++++++++++++++++ tests/exit_tls_check.c | 45 +++++++++++++++++ tests/exit_tls_check.py | 79 ++++++++++++++++++++++++++++++ tests/logq_check.c | 18 +++++++ 11 files changed, 296 insertions(+), 3 deletions(-) create mode 100644 tests/exit_tls_check.c create mode 100644 tests/exit_tls_check.py diff --git a/runtime/gfx_present.c b/runtime/gfx_present.c index 2db2407a..cd38f6dc 100644 --- a/runtime/gfx_present.c +++ b/runtime/gfx_present.c @@ -104,7 +104,10 @@ 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) + if (!in) { // the session's reports and the log now, should the process be ended from outside next + perf_focus_lost(); // perf.c + rt_log_focus_lost(); // main.c + } } // ---- the orderly end: the window's close (gfx_window_closed), the guest's ExitProcess (svc.c) and the end of a diff --git a/runtime/host.h b/runtime/host.h index ee1ceef9..ee8200be 100644 --- a/runtime/host.h +++ b/runtime/host.h @@ -25,6 +25,7 @@ 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 +void rt_log_focus_lost(void); // main.c: the gathered lines written now, the module list noted again void hang_watch_start(void); // hang_watch.c: NX2NAVI_HANG_WATCH_S's thread, in a game run void kern_wait_report(void); // kernel.c: the guest threads' lock and thread-end waits, for a hang report void vk_sync_progress(uint64_t *issued, uint64_t *done); // vk_sync.c: submissions issued and done, no lock or device call diff --git a/runtime/logq.c b/runtime/logq.c index 565815ac..36930797 100644 --- a/runtime/logq.c +++ b/runtime/logq.c @@ -41,7 +41,7 @@ static struct { char *buf, *out; // LOGQ_CAP bytes each: the queue, and what the log thread is writing size_t n; LogqSink sink; - volatile int32_t on, saying; + volatile int32_t on, saying, now; // now: logq_nudge asked for the gathered lines at once volatile int64_t slow, said, worst_ns, full; // slow writes, those reported, the longest; lines that found no room volatile uint64_t said_at; } Q = {.q = PLAT_MUTEX_INIT, .io = PLAT_MUTEX_INIT, .more = PLAT_COND_INIT}; @@ -135,9 +135,10 @@ static void log_main(void *arg) { for (;;) { plat_mutex_lock(&Q.q); while (!Q.n) plat_cond_wait(&Q.more, &Q.q); - for (uint64_t t0 = plat_now_ns(), spent = 0; Q.n && Q.n < LOGQ_GATHER_BYTES && spent < LOGQ_GATHER_NS; + for (uint64_t t0 = plat_now_ns(), spent = 0; Q.n && Q.n < LOGQ_GATHER_BYTES && spent < LOGQ_GATHER_NS && !Q.now; spent = plat_now_ns() - t0) plat_cond_wait_ns(&Q.more, &Q.q, LOGQ_GATHER_NS - spent); + Q.now = 0; plat_mutex_unlock(&Q.q); drain(); say_slow(); @@ -174,6 +175,24 @@ void logq_flush(void) { plat_mutex_unlock(&Q.io); } +// The log thread writes what it gathers now instead of when the gathering time is up: a focus loss, after which the +// process may be ended from outside (main.c rt_log_focus_lost); such an end loses what was being gathered. +void logq_nudge(void) { + if (!__atomic_load_n(&Q.on, __ATOMIC_ACQUIRE)) return; + plat_mutex_lock(&Q.q); + Q.now = 1; + plat_mutex_unlock(&Q.q); + plat_cond_signal(&Q.more); +} + +// What is queued, as it stands, with no lock: for the note of an end nobody asked for, written with every other thread +// stopped (platform.h plat_exit_note_setup). Nothing once the queue is off. +void logq_peek(const char **p, size_t *n) { + size_t k = __atomic_load_n(&Q.n, __ATOMIC_RELAXED); + *p = __atomic_load_n(&Q.buf, __ATOMIC_RELAXED); + *n = __atomic_load_n(&Q.on, __ATOMIC_RELAXED) && k <= LOGQ_CAP ? k : 0; +} + 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 eabc48ab..e159cb78 100644 --- a/runtime/logq.h +++ b/runtime/logq.h @@ -9,4 +9,6 @@ int logq_async(void); // from now on the log thread writes 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 +void logq_nudge(void); // the log thread writes what it gathers now (a focus loss) +void logq_peek(const char **p, size_t *n); // what is queued as it stands, no lock (the end nobody asked for) size_t logq_bytes(void); // the queue's buffers, for the memory report (vk_memory.c system_log) diff --git a/runtime/main.c b/runtime/main.c index b513a1db..b8996ea4 100644 --- a/runtime/main.c +++ b/runtime/main.c @@ -140,6 +140,14 @@ static void console_closed(const char *what) { gfx_window_closed(); } +// The window lost the focus (gfx_present.c gfx_window_focus_changed): a process ended from outside soon after lost the +// lines the log thread was still gathering, and both unexplained ends of 2026-11-13 came seconds after one. The log +// thread writes them at once, and the module list for the note of an ExitProcess is brought up to date. +void rt_log_focus_lost(void) { + logq_nudge(); + plat_exit_note_modules(); +} + void rt_log_raw(const char *p, size_t n) { if (__atomic_load_n(&g_log_state, __ATOMIC_ACQUIRE) == LOG_EARLY) { fwrite(p, 1, n, stderr); @@ -212,6 +220,10 @@ static void log_open(int game) { __atomic_store_n(&g_log_state, LOG_FILE, __ATOMIC_RELEASE); rt_log("log: %s", g_log_path); } + if (game && g_log_file) { // an ExitProcess nobody of ours asked for leaves a line in the file (platform.h) + plat_exit_note_setup(g_log_file, logq_peek); + plat_exit_note_modules(); + } // 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; @@ -690,6 +702,7 @@ int main(int argc, char **argv) { plat_stack_guarantee(); // a stack overflow on the main thread still gets its crash report (platform.h) mode_rc = main_run(argc, argv); __atomic_store_n(&mode_done, 1, __ATOMIC_RELEASE); // from here on only the exit runs (mode_done, above) + plat_exit_orderly(); // the C runtime's exit ends the process with ExitProcess: no note of an end nobody asked for return mode_rc; } diff --git a/runtime/platform/linux.c b/runtime/platform/linux.c index c0355e29..ed835bb4 100644 --- a/runtime/platform/linux.c +++ b/runtime/platform/linux.c @@ -1933,6 +1933,11 @@ void plat_exit(int code) { void plat_terminate(int code) { _exit(code); } +// The note of an end the game did not ask for is the Windows executable's TLS callback (win32.c); nothing here. +void plat_exit_note_setup(PlatAppend *log, void (*queued)(const char **p, size_t *n)) { (void)log, (void)queued; } +void plat_exit_note_modules(void) {} +void plat_exit_orderly(void) {} + void plat_stack_guarantee(void) {} // the crash report runs on the signal stack crash handling sets up int plat_setenv(const char *name, const char *value) { diff --git a/runtime/platform/platform.h b/runtime/platform/platform.h index 4199b0ef..4870e730 100644 --- a/runtime/platform/platform.h +++ b/runtime/platform/platform.h @@ -403,6 +403,16 @@ int plat_file_private(const char *path); // only this account m typedef struct PlatAppend PlatAppend; PlatAppend *plat_append_open(const char *path); // creates or truncates path; NULL on failure int plat_append_write(PlatAppend *f, const void *data, size_t n); // 1 when all n bytes landed +// An end the game did not ask for: ExitProcess (or exit()) called by a library, or by the runtime outside plat_exit and +// plat_terminate. Windows calls the executable's TLS callback for it, with every other thread already stopped, and the +// callback then writes what the log queue held (queued(&p, &n): the bytes as they stand, no lock) and one line with +// the return addresses that led to the end into log. Nothing for plat_exit, plat_terminate, TerminateProcess from +// outside, or after plat_exit_orderly (main returning from a tool mode). plat_exit_note_modules notes the loaded +// modules, whose names the line gives the addresses (the callback may not ask the loader): at start and at a focus +// loss. Windows only; elsewhere they do nothing. +void plat_exit_note_setup(PlatAppend *log, void (*queued)(const char **p, size_t *n)); +void plat_exit_note_modules(void); +void plat_exit_orderly(void); // Calls cb(name, ctx) for each file in dir whose name ends in ext (".in"); name has no directory part. cb returns 0 to // stop. Returns 0 when dir cannot be listed. int plat_file_list(const char *dir, const char *ext, int (*cb)(const char *name, void *ctx), void *ctx); diff --git a/runtime/platform/win32.c b/runtime/platform/win32.c index 92336626..e6fa01e7 100644 --- a/runtime/platform/win32.c +++ b/runtime/platform/win32.c @@ -1270,6 +1270,7 @@ int plat_crash_filter_reassert(char *who, size_t n) { // A fresh process gets a fresh layout: Windows places DLLs, heaps and thread stacks at random addresses. The parent // waits and exits with the child's code. void plat_reexec(void) { + plat_exit_orderly(); // this process ends with ExitProcess below, on purpose fprintf(stderr, "[host] memory: a fixed region is taken; re-running the process\n"); fflush(stdout); fflush(stderr); @@ -1390,6 +1391,7 @@ int plat_stderr_write(const void *p, size_t n) { void (*plat_exit_hook)(void); void plat_exit(int code) { + plat_exit_orderly(); if (plat_exit_hook) plat_exit_hook(); #ifdef NX2NAVI_PGO_GEN __llvm_profile_dump(); @@ -1409,10 +1411,106 @@ void plat_exit(int code) { } void plat_terminate(int code) { + plat_exit_orderly(); TerminateProcess(GetCurrentProcess(), (UINT)code); ExitProcess((UINT)code); // not reached } +// ---- an end the game did not ask for (plat_exit_note_setup) +// Two runs of 2026-11-13 ended seconds after a focus loss with exit code 0, no crash event and no line of their own: an +// ExitProcess by a module in the process, or a kill from outside, could not be told apart. ExitProcess stops every +// other thread and then calls each module's detach, the executable's TLS callbacks included; TerminateProcess calls +// none. The callback runs under the loader lock with the other threads gone, so any lock they held stays held: it +// uses no C runtime (no formatting, allocation or streams: their locks), does not ask the loader (RtlPcToFileHeader +// takes its module lock) and names addresses from the module list noted earlier. RtlCaptureStackBackTrace and +// WriteFile take no lock of the process. +#define EN_MODS 384 +typedef struct { uintptr_t base, size; char name[40]; } EnMod; +static EnMod en_mods[2][EN_MODS]; +static int en_n[2]; +static volatile LONG en_cur = -1; // the table the callback reads, -1 none yet +static PlatMutex en_lock = PLAT_MUTEX_INIT; // the writers of the tables (start, focus losses) +static PlatAppend *volatile en_log; +static void (*volatile en_queued)(const char **p, size_t *n); +static volatile LONG en_orderly; + +void plat_exit_orderly(void) { InterlockedExchange(&en_orderly, 1); } + +void plat_exit_note_setup(PlatAppend *log, void (*queued)(const char **p, size_t *n)) { + en_queued = queued; + en_log = log; +} + +void plat_exit_note_modules(void) { + HMODULE m[EN_MODS]; + DWORD need = 0; + if (!EnumProcessModules(GetCurrentProcess(), m, sizeof m, &need)) return; + int n = (int)(need / sizeof m[0] < EN_MODS ? need / sizeof m[0] : EN_MODS), k = 0; + plat_mutex_lock(&en_lock); + int t = InterlockedCompareExchange(&en_cur, 0, 0) == 0 ? 1 : 0; // the table the callback does not read + for (int i = 0; i < n; i++) { + MODULEINFO mi; + if (!GetModuleInformation(GetCurrentProcess(), m[i], &mi, sizeof mi)) continue; + EnMod *e = &en_mods[t][k++]; + e->base = (uintptr_t)mi.lpBaseOfDll, e->size = mi.SizeOfImage; + if (!GetModuleBaseNameA(GetCurrentProcess(), m[i], e->name, sizeof e->name)) e->name[0] = 0; + } + en_n[t] = k; + InterlockedExchange(&en_cur, t); + plat_mutex_unlock(&en_lock); +} + +static size_t en_put(char *o, size_t at, size_t cap, const char *s) { + while (*s && at < cap) o[at++] = *s++; + return at; +} + +static size_t en_hex(char *o, size_t at, size_t cap, uintptr_t v) { + char d[18]; + int n = 0; + do d[n++] = "0123456789abcdef"[v & 15], v >>= 4; while (v); + at = en_put(o, at, cap, "0x"); + while (n && at < cap) o[at++] = d[--n]; + return at; +} + +// An executable gets DLL_PROCESS_DETACH only as the process ends (its reserved argument is NULL there all the same). +static void NTAPI exit_note_tls(PVOID h, DWORD reason, PVOID reserved) { + (void)h, (void)reserved; + PlatAppend *log = en_log; + if (reason != DLL_PROCESS_DETACH || en_orderly || !log) return; + const char *q = NULL; + size_t qn = 0; + void (*queued)(const char **, size_t *) = en_queued; + if (queued) queued(&q, &qn); + if (q && qn) plat_append_write(log, q, qn); + char line[2048]; + size_t at = en_put(line, 0, sizeof line - 1, "[host] exit: the process is ending through ExitProcess, not one of " + "the game's own ends; the calls that led to it:"); + void *f[32]; + USHORT n = RtlCaptureStackBackTrace(1, 32, f, NULL); + LONG t = en_cur; + for (USHORT i = 0; i < n; i++) { + uintptr_t a = (uintptr_t)f[i]; + const EnMod *m = NULL; + for (int k = 0; t >= 0 && k < en_n[t] && !m; k++) + if (a - en_mods[t][k].base < en_mods[t][k].size) m = &en_mods[t][k]; + at = en_put(line, at, sizeof line - 1, " "); + if (m && m->name[0]) { + at = en_put(line, en_put(line, at, sizeof line - 1, m->name), sizeof line - 1, "+"); + a -= m->base; + } + at = en_hex(line, at, sizeof line - 1, a); + } + line[at++] = '\n'; + plat_append_write(log, line, at); +} +// The callback in the executable's TLS callback list (the .CRT$XL* sections), which the TLS directory the image has +// for its thread-local variables points to; _tls_used is that directory. +__attribute__((section(".CRT$XLB"), used)) PIMAGE_TLS_CALLBACK plat_exit_note_cb = exit_note_tls; +extern const IMAGE_TLS_DIRECTORY _tls_used; +__attribute__((used)) static const void *volatile exit_note_tls_dir = &_tls_used; + // _putenv_s, not SetEnvironmentVariableA: getenv reads the C runtime's copy of the environment, which only the CRT's // own calls update. An empty value removes the variable. int plat_setenv(const char *name, const char *value) { return _putenv_s(name, value ? value : "") == 0; } diff --git a/tests/exit_tls_check.c b/tests/exit_tls_check.c new file mode 100644 index 00000000..374e3029 --- /dev/null +++ b/tests/exit_tls_check.c @@ -0,0 +1,45 @@ +// The note of an end nobody of ours asked for (runtime/platform/win32.c exit_note_tls, plat_exit_note_setup) with the +// platform layer and runtime/logq.c linked: the process logs a line through the log queue, which the log thread is +// still gathering, and ends. Built and run by tests/exit_tls_check.py, which reads the log file each mode leaves. +// exit_tls_check , mode one of: +// exitprocess ExitProcess from a function of this program: the queued line, then the note naming this program +// exit exit(): the C runtime's ExitProcess, the same note +// terminate TerminateProcess: no note +// platexit plat_exit: the queued line (its hook writes the queue), no note +// return main returns after plat_exit_orderly, as main.c does after a tool mode: no note +#include +#include +#include +#include + +#include "logq.h" +#include "platform/platform.h" + +void rt_log(const char *fmt, ...) { (void)fmt; } // logq.c's line about slow writes + +static PlatAppend *file; +static void sink(const char *p, size_t n) { plat_append_write(file, p, n); } + +__attribute__((noinline)) static void end_here(void) { + ExitProcess(0); +} + +int main(int argc, char **argv) { + if (argc < 3 || !(file = plat_append_open(argv[2]))) return 2; + logq_init(sink); + if (!logq_async()) return 3; + plat_exit_hook = logq_sync; + plat_exit_note_setup(file, logq_peek); + plat_exit_note_modules(); + logq_write("queued line\n", 12); // the log thread gathers it for LOGQ_GATHER_NS: still queued below + const char *m = argv[1]; + if (!strcmp(m, "exitprocess")) end_here(); + if (!strcmp(m, "exit")) exit(0); + if (!strcmp(m, "terminate")) TerminateProcess(GetCurrentProcess(), 0); + if (!strcmp(m, "platexit")) plat_exit(0); + if (!strcmp(m, "return")) { + plat_exit_orderly(); + return 0; + } + return 2; +} diff --git a/tests/exit_tls_check.py b/tests/exit_tls_check.py new file mode 100644 index 00000000..b1739f5b --- /dev/null +++ b/tests/exit_tls_check.py @@ -0,0 +1,79 @@ +"""Build and run tests/exit_tls_check.c: an ExitProcess or exit() that none of the game's own ends asked for leaves the +log queue's lines and one note in the log file, naming the calls that led to it by module and offset +(runtime/platform/win32.c exit_note_tls); TerminateProcess, plat_exit and a return from main after plat_exit_orderly +leave none. Windows only; no game files. + +Usage: exit_tls_check.py +""" +import os +import re +import shutil +import subprocess +import sys +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] +MSYS_BIN = Path(r'C:\msys64\ucrt64\bin') + +LAYOUT = '''// A stand-in for the generated layout.h (tests/exit_tls_check.py). +#pragma once +#define MOD_MAIN_BASE 0x7000000000ull +#define MOD_MAIN_SIZE 0x4ea0000ull +''' +NOTE = 'exit: the process is ending through ExitProcess' + +CASES = [ # mode, the queued line in the file, a note in the file + ('exitprocess', True, True), + ('exit', True, True), + ('terminate', None, False), # the queued line may or may not have been written: the kill came while it gathered + ('platexit', True, False), + ('return', None, False), +] + + +def compiler(): + 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 main(): + if os.name != 'nt': + print('exit_tls_check: Windows only') + return 0 + work = ROOT / 'build' / 'test' / 'exit_tls' + work.mkdir(parents=True, exist_ok=True) + (work / 'layout.h').write_text(LAYOUT) + exe = work / 'exit_tls_check.exe' + env = dict(os.environ) + if MSYS_BIN.exists(): + env['PATH'] = str(MSYS_BIN) + os.pathsep + env.get('PATH', '') + rt = ROOT / 'runtime' + plat = [rt / 'platform' / 'win32.c', rt / 'platform' / 'common.c', rt / 'logq.c'] + libs = ['-lbcrypt', '-lws2_32', '-lpsapi', '-lwinmm', '-ldbghelp', '-lshell32', '-lole32', '-luser32', '-ladvapi32'] + subprocess.check_call([compiler(), '-O1', '-g', '-Wall', '-Wno-unused-function', '-fno-strict-aliasing', + '-march=x86-64-v3', '-I', str(rt), '-I', str(work), str(ROOT / 'tests' / 'exit_tls_check.c'), + *map(str, plat), '-o', str(exe), '-lm', *libs], env=env) + bad = 0 + for mode, queued, note in CASES: + log = work / f'{mode}.log' + log.unlink(missing_ok=True) + rc = subprocess.run([str(exe), mode, str(log)], env=env, timeout=60).returncode + text = log.read_text(errors='replace') if log.exists() else '' + has_q, has_note = 'queued line' in text, NOTE in text + named = bool(re.search(r'exit_tls_check\.exe\+0x[0-9a-f]+', text)) + ok = rc == 0 and (queued is None or has_q == queued) and has_note == note and (not note or named) + print(f'{mode}: exit code {rc}, the queued line {"in" if has_q else "not in"} the file, ' + f'{"a note" + (" naming this program" if named else " NAMING NOTHING") if has_note else "no note"}' + f'{"" if ok else ": FAIL"}', flush=True) + if not ok: + print(text[-2000:]) + bad += not ok + print(f'exit_tls_check: {"FAILED" if bad else "ok"}') + return 1 if bad else 0 + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/tests/logq_check.c b/tests/logq_check.c index 1d35b596..ea23a955 100644 --- a/tests/logq_check.c +++ b/tests/logq_check.c @@ -326,6 +326,24 @@ int main(int argc, char **argv) { at = out_n; for (int i = 0; i < THREADS; i++) next[i] = 0; + // 9b. logq_nudge (a focus loss): the log thread writes what it gathers at once, not when the gathering time is up. + // On a machine too busy to log and look within half that time the timing check is left out. + uint64_t n0 = plat_now_ns(); + for (int i = 0; i < 10; i++) { + char line[64]; + int n = snprintf(line, sizeof line, "t 4 %d \n", i); + logq_write(line, (size_t)n); + } + logq_nudge(); + in_time = plat_now_ns() - n0 < LOGQ_GATHER_NS / 4; + for (int i = 0; i < 60000 && out_n == at; i++) plat_sleep_ms(1); + uint64_t nudged = plat_now_ns() - n0; + settle(); + if (in_time) CHECK(nudged < LOGQ_GATHER_NS * 3 / 4, "the nudged batch came %.1f ms after its first line", nudged / 1e6); + CHECK(check_lines(at, next, ¬es) == 10, "lines lost from a nudged batch"); + 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