diff --git a/docs/architecture.md b/docs/architecture.md index b6db81de..961d645e 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -40,6 +40,10 @@ from other projects, with its license, is listed in [THIRD_PARTY_NOTICES.md](../ - Settings and menu: every key with its default is in one table (`cfg.c`), read from `nx2navi.ini` and `NX2NAVI_*` variables and written back when the in-game menu changes it. The menu and the performance overlay are Dear ImGui, drawn into the swapchain image (`runtime/ui/`); their text is in `ui_text.c`. +- Log: in a game run the lines go through a queue to a thread that writes them (`runtime/logq.c`), so a stalled + write to the log file or the console does not hold the thread that logged; a fatal error, the crash report and the + end of the process write the queue out first (`rt_log_sync`). `NX2NAVI_LOG_ASYNC=0` writes each line where it is + logged. - OS calls the host makes go through `runtime/platform/` (`platform.h`, `win32.c`). #### Threads and cores diff --git a/docs/building.md b/docs/building.md index 3703593a..ecb84d6e 100644 --- a/docs/building.md +++ b/docs/building.md @@ -285,6 +285,11 @@ waits on its event and leaves the others asleep, a wait on several handles repor the wait on it, timed waits keep their time, a cancel ends any wait, and 4,000 signals to eight looping waiters are each seen. It runs a second time with `NX2NAVI_SYNC_WAKE_ALL=1`. No game files. +A change to the log queue (`runtime/logq.c`) passes `python tests\logq_check.py`, which builds it with the platform +layer and a destination of its own: 160,000 lines from eight threads arrive whole and in each thread's order, a +destination that takes 30 ms a write does not hold the threads that log, one that stops while four times the queue's +size is logged loses nothing, and `logq_sync` returns with everything written. No game files. + A change to the button prompts (`tools/make_romfs_patches.py --prompts`, `tools/button_icons.py`) passes `.venv-py314\Scripts\python tests\romfs_prompts_test.py`, which runs the tool on a RomFS it makes itself (no game files); a change to the settings passes `nx2navi.exe --probe-cfg`. diff --git a/nx2navi.ini.example b/nx2navi.ini.example index 66ba5570..633531df 100644 --- a/nx2navi.ini.example +++ b/nx2navi.ini.example @@ -611,6 +611,12 @@ # working. 0 (default): only the threads that wait for that event or thread are woken. With trace = sched the # "sched:" line counts both ("signals woke ..., left ... asleep"). # sync_wake_all = 0 +# 1 (default): a thread of its own writes the log lines, to the log file and the console, so a write that stalls (a +# busy disk, a virus scanner, a console that scrolls) no longer holds the thread that logged, which is often the one +# that draws the frame. A crash, a fatal error and the end of the game write out what is still queued first. 0: each +# line is written by the thread that logs it, as before. Either way a "log: ... writes of the log took over 2 ms" line +# counts the slow writes and says whose time they took. +# log_async = 1 # 1: the frame-time summary also splits the replay by op kind (draw, bind, transfer), which says where a stalled frame # went. The clock reads cost a fraction of a millisecond a frame, so it is off by default. # replay_time = 0 diff --git a/runtime/cfg.c b/runtime/cfg.c index 351aa990..77cc34d7 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -69,7 +69,7 @@ static const char *const known[] = { "BIND_REUSE", "DYN_DELTA", "STG_ARENA", "SUBMIT_BATCH", "ASTC_GPU_DESWIZZLE", "ASTC_SCRATCH_KEEP", "ASTC_ARENA", "TEXUP_LOG_MS", "DYN_RES", "DYN_RES_TARGET", "DYN_RES_STEP", "RECORDER_CORE", "RECORDER_PRIO", "REPLAY_TIME", "LOCK_TIME", "POOL_CREATE_BUDGET", "POOL_CREATE_MS", - "VMA_BLOCK_MB", "ASTC_BC_PROBE", "SYNC_WAKE_ALL", + "VMA_BLOCK_MB", "ASTC_BC_PROBE", "SYNC_WAKE_ALL", "LOG_ASYNC", }; // The nx2navi.ini section a key cfg_set may write goes to when the file lacks it (nx2navi.ini.example's sections). diff --git a/runtime/dispatch.c b/runtime/dispatch.c index 1e651f61..75d28e01 100644 --- a/runtime/dispatch.c +++ b/runtime/dispatch.c @@ -44,6 +44,7 @@ static uint64_t g_nhost; void rt_fatal(const char *fmt, ...) { va_list ap; va_start(ap, fmt); + 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); rt_log_raw("\n", 1); diff --git a/runtime/host.h b/runtime/host.h index f86f7a6e..2223209c 100644 --- a/runtime/host.h +++ b/runtime/host.h @@ -23,6 +23,7 @@ void rt_logf(const char *fmt, ...) __attribute__((format(printf, 1, 2))); void rt_vlogf(const char *fmt, va_list ap); 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 enum { RT_TRACE_SVC = 1, RT_TRACE_HOSTCALL = 2, RT_TRACE_IPC = 4, RT_TRACE_FS = 8, RT_TRACE_GFX = 16, RT_TRACE_PERF = 32, RT_TRACE_SCHED = 64, RT_TRACE_AUDIO = 128, RT_TRACE_PRESENT = 256 }; extern unsigned rt_trace; // from NX2NAVI_TRACE=svc,host,ipc,fs,gfx,perf,sched,audio,present (or all) diff --git a/runtime/logq.c b/runtime/logq.c new file mode 100644 index 00000000..7c3448c8 --- /dev/null +++ b/runtime/logq.c @@ -0,0 +1,141 @@ +// Log writes on a thread of their own. +// +// A log line was written by the thread that logged it: to stderr and to the log file (main.c log_sink). A write can +// stall: a console that scrolls, a pipe whose reader is busy, a virus scanner, a disk that flushes; hundreds of +// milliseconds at worst. The render thread logs too, and a frame it spends in such a write is a hitch with nothing +// else to show for it. +// +// With the queue on (logq_async) a line is copied into a buffer under a lock that is held for the copy alone, and the +// "log" thread writes the buffer out, so lines keep the order they were queued in. logq_sync writes what is queued on +// the calling thread and turns the queue off for the rest of the run: a fatal error, the crash report and the end of +// the process call it (rt_log_sync), so no line is lost to an exit and their own lines are written as they are made. +// +// Every write is timed. One that took LOGQ_SLOW_NS or longer is counted, and a line says so at most every ten +// seconds: with the queue that time was the log thread's, without it (NX2NAVI_LOG_ASYNC=0) the logging thread's. +#include +#include + +#include "host.h" +#include "logq.h" +#include "platform/platform.h" + +#define LOGQ_CAP (1u << 20) +#define LOGQ_SLOW_NS 2000000ll +#define LOGQ_SAY_NS 10000000000ull + +static struct { + PlatMutex q, io; // q: buf and n; io: the queue's writes and out. io is taken first + PlatCond more; + 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 int64_t slow, said, worst_ns, direct; // slow writes, those reported, the longest; lines written past a busy queue + volatile uint64_t said_at; +} Q = {.q = PLAT_MUTEX_INIT, .io = PLAT_MUTEX_INIT, .more = PLAT_COND_INIT}; + +void logq_init(LogqSink sink) { Q.sink = sink; } + +// The write itself, timed. +static void put_out(const char *p, size_t n) { + uint64_t t0 = plat_now_ns(); + Q.sink(p, n); + int64_t d = (int64_t)(plat_now_ns() - t0); + if (d < LOGQ_SLOW_NS) return; + __atomic_add_fetch(&Q.slow, 1, __ATOMIC_RELAXED); + int64_t w = __atomic_load_n(&Q.worst_ns, __ATOMIC_RELAXED); + while (d > w && !__atomic_compare_exchange_n(&Q.worst_ns, &w, d, 0, __ATOMIC_RELAXED, __ATOMIC_RELAXED)) {} +} + +// The line about slow writes, when there are new ones and the last line is ten seconds old. Called with no lock held: +// the line goes through logq_write like any other. +static void say_slow(void) { + int64_t slow = __atomic_load_n(&Q.slow, __ATOMIC_RELAXED); + if (slow == __atomic_load_n(&Q.said, __ATOMIC_RELAXED)) return; + uint64_t now = plat_now_ns(), at = __atomic_load_n(&Q.said_at, __ATOMIC_RELAXED); + if ((at && now - at < LOGQ_SAY_NS) || plat_xchg32(&Q.saying, 1)) return; + __atomic_store_n(&Q.said, slow, __ATOMIC_RELAXED); + __atomic_store_n(&Q.said_at, now ? now : 1, __ATOMIC_RELAXED); + rt_log("log: %lld writes of the log took over %d ms so far, the longest %.1f ms, %s (NX2NAVI_LOG_ASYNC); %lld lines " + "were written past a busy queue", (long long)slow, (int)(LOGQ_SLOW_NS / 1000000), + __atomic_load_n(&Q.worst_ns, __ATOMIC_RELAXED) / 1e6, + __atomic_load_n(&Q.on, __ATOMIC_ACQUIRE) ? "on the log thread" : "on the threads that logged", + (long long)__atomic_load_n(&Q.direct, __ATOMIC_RELAXED)); + plat_xchg32(&Q.saying, 0); +} + +// Writes out what is queued, in order. Any thread. +static void drain(void) { + plat_mutex_lock(&Q.io); + plat_mutex_lock(&Q.q); + 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); +} + +// 1 when the bytes were queued, 0 when the caller is to write them. The queue's lock is only tried: a thread that logs +// while the lock's holder cannot run (stopped for a profile sample or a core hand-over) writes its line itself. +static int put(const char *p, size_t n) { + if (!__atomic_load_n(&Q.on, __ATOMIC_ACQUIRE) || n > LOGQ_CAP / 4) return 0; + for (int tries = 0; !plat_mutex_trylock(&Q.q); tries++) { + if (tries == 4000) { + __atomic_add_fetch(&Q.direct, 1, __ATOMIC_RELAXED); + return 0; + } + plat_pause(); + } + int on = __atomic_load_n(&Q.on, __ATOMIC_ACQUIRE); + if (!on || Q.n + n > LOGQ_CAP) { // full: the log thread is held up in a write. What is queued goes out first + plat_mutex_unlock(&Q.q); + if (on) drain(); + return 0; + } + int first = !Q.n; + memcpy(Q.buf + Q.n, p, n); + Q.n += n; + plat_mutex_unlock(&Q.q); + if (first) plat_cond_signal(&Q.more); + return 1; +} + +void logq_write(const char *p, size_t n) { + if (put(p, n)) return; + put_out(p, n); + say_slow(); +} + +static void log_main(void *arg) { + (void)arg; + for (;;) { + plat_mutex_lock(&Q.q); + while (!Q.n) plat_cond_wait(&Q.more, &Q.q); + plat_mutex_unlock(&Q.q); + drain(); + say_slow(); + } +} + +int logq_async(void) { + if (!Q.sink || __atomic_load_n(&Q.on, __ATOMIC_ACQUIRE)) return 0; + if (!Q.buf) Q.buf = malloc(LOGQ_CAP), Q.out = malloc(LOGQ_CAP); + if (!Q.buf || !Q.out || !plat_thread_spawn(log_main, NULL, "log", 0)) return 0; + __atomic_store_n(&Q.on, 1, __ATOMIC_RELEASE); + return 1; +} + +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 + // write this call is there to get past. Without the lock the queue is written as it stands. + int io = 0, q = 0; + for (int i = 0; i < 500 && !(io = plat_mutex_trylock(&Q.io)); i++) plat_sleep_ms(1); + for (int i = 0; i < 500 && !(q = plat_mutex_trylock(&Q.q)); i++) plat_sleep_ms(1); + size_t n = Q.n; + Q.n = 0; + if (n && n <= LOGQ_CAP) put_out(Q.buf, n); + if (q) plat_mutex_unlock(&Q.q); + if (io) plat_mutex_unlock(&Q.io); +} diff --git a/runtime/logq.h b/runtime/logq.h new file mode 100644 index 00000000..995182d1 --- /dev/null +++ b/runtime/logq.h @@ -0,0 +1,10 @@ +// Log writes on a thread of their own (logq.c). main.c owns the log's destinations and hands every line here. +#pragma once +#include + +typedef void (*LogqSink)(const char *p, size_t n); // writes the bytes to the log's destinations; may block + +void logq_init(LogqSink sink); // before the first logq_write; lines are written by the thread that logs them +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, full or busy +void logq_sync(void); // writes what is queued, on the calling thread, and turns the queue off diff --git a/runtime/main.c b/runtime/main.c index 9f1e6d1d..72262350 100644 --- a/runtime/main.c +++ b/runtime/main.c @@ -42,6 +42,7 @@ #include "cfg.h" #include "host.h" +#include "logq.h" #include "prof.h" #include "platform/platform.h" #include "sha256.h" @@ -66,21 +67,29 @@ static char g_log_early[32768]; static size_t g_log_early_n; static uint64_t g_log_bytes; -void rt_log_raw(const char *p, size_t n) { +// The log's destinations. logq.c calls it: from the log thread in a game run, else from the thread that logs. +static void log_sink(const char *p, size_t n) { fwrite(p, 1, n, stderr); - int state = __atomic_load_n(&g_log_state, __ATOMIC_ACQUIRE); - if (state == LOG_EARLY) { + if (__atomic_load_n(&g_log_state, __ATOMIC_ACQUIRE) != LOG_FILE) return; + uint64_t at = __atomic_fetch_add(&g_log_bytes, n, __ATOMIC_RELAXED); + if (at + n <= LOG_CAP) { + plat_append_write(g_log_file, p, n); + } else if (at <= LOG_CAP) { // the first write past the cap says so, once + static const char cut[] = "[host] log: the file reached 64 MiB, the rest of this run goes to stderr only\n"; + plat_append_write(g_log_file, cut, sizeof cut - 1); + } +} + +void rt_log_sync(void) { logq_sync(); } + +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); size_t at = __atomic_fetch_add(&g_log_early_n, n, __ATOMIC_RELAXED); if (at + n <= sizeof g_log_early) memcpy(g_log_early + at, p, n); - } else if (state == LOG_FILE) { - uint64_t at = __atomic_fetch_add(&g_log_bytes, n, __ATOMIC_RELAXED); - if (at + n <= LOG_CAP) { - plat_append_write(g_log_file, p, n); - } else if (at <= LOG_CAP) { // the first write past the cap says so, once - static const char cut[] = "[host] log: the file reached 64 MiB, the rest of this run goes to stderr only\n"; - plat_append_write(g_log_file, cut, sizeof cut - 1); - } + return; } + logq_write(p, n); } void rt_vlogf(const char *fmt, va_list ap) { @@ -115,6 +124,7 @@ const char *rt_log_path(void) { // Opens the log file for a game run (game = 1) and writes the buffered lines to it; anything else drops them. Runs on // the main thread before the boot starts other threads. static void log_open(int game) { + logq_init(log_sink); char dir[PLAT_PATH_MAX * 3], from[PLAT_PATH_MAX * 3 + 16], to[PLAT_PATH_MAX * 3 + 16]; char *cut = NULL; if (game && !plat_stderr_path(dir, sizeof dir) && plat_exe_path(dir, sizeof dir)) { @@ -136,14 +146,20 @@ static void log_open(int game) { } if (!g_log_file) { __atomic_store_n(&g_log_state, LOG_OFF, __ATOMIC_RELEASE); - return; + } else { + size_t n = __atomic_load_n(&g_log_early_n, __ATOMIC_RELAXED); + if (n > sizeof g_log_early) n = sizeof g_log_early; + plat_append_write(g_log_file, g_log_early, n); + g_log_bytes = n; + __atomic_store_n(&g_log_state, LOG_FILE, __ATOMIC_RELEASE); + rt_log("log: %s", g_log_path); } - size_t n = __atomic_load_n(&g_log_early_n, __ATOMIC_RELAXED); - if (n > sizeof g_log_early) n = sizeof g_log_early; - plat_append_write(g_log_file, g_log_early, n); - g_log_bytes = n; - __atomic_store_n(&g_log_state, LOG_FILE, __ATOMIC_RELEASE); - rt_log("log: %s", g_log_path); + // 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; + 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"); } static void parse_trace(void) { @@ -243,6 +259,7 @@ static int readable(uint64_t a, uint64_t n) { return plat_mem_readable(a, n); } static void crash_handler(const PlatCrash *x) { static volatile int32_t busy; if (plat_xchg32(&busy, 1)) plat_exit(71); + rt_log_sync(); // the lines before the crash, then the report, each written as it is made char d[160]; uintptr_t rip = x->rip; rt_logf("FATAL: %s (%#x) at host %p (", x->what, x->code, (void *)rip); diff --git a/runtime/platform/linux.c b/runtime/platform/linux.c index 60cf2c6d..caa58b76 100644 --- a/runtime/platform/linux.c +++ b/runtime/platform/linux.c @@ -1481,7 +1481,10 @@ int plat_stderr_path(char *out, size_t cap) { int __llvm_profile_dump(void); #endif +void (*plat_exit_hook)(void); + void plat_exit(int code) { + if (plat_exit_hook) plat_exit_hook(); #ifdef NX2NAVI_PGO_GEN __llvm_profile_dump(); #endif diff --git a/runtime/platform/platform.h b/runtime/platform/platform.h index 09e9dad3..259c51a1 100644 --- a/runtime/platform/platform.h +++ b/runtime/platform/platform.h @@ -289,6 +289,7 @@ void plat_aligned_free(void *p); // ---- process _Noreturn void plat_exit(int code); // ends the process at once: other threads stop, no atexit handlers run +extern void (*plat_exit_hook)(void); // called first by plat_exit when set (main.c: the queued log lines) int plat_setenv(const char *name, const char *value); // value NULL removes it; 1 on success uint32_t plat_process_id(void); // CPU time of the process since it started (user and kernel), and wall time since it started, in ns; 0 when unknown. diff --git a/runtime/platform/win32.c b/runtime/platform/win32.c index d33dac69..b73ffe75 100644 --- a/runtime/platform/win32.c +++ b/runtime/platform/win32.c @@ -1120,7 +1120,10 @@ __attribute__((constructor)) static void pgo_flush_start(void) { } #endif +void (*plat_exit_hook)(void); + void plat_exit(int code) { + if (plat_exit_hook) plat_exit_hook(); #ifdef NX2NAVI_PGO_GEN __llvm_profile_dump(); #endif diff --git a/tests/logq_check.c b/tests/logq_check.c new file mode 100644 index 00000000..c4ae1dbb --- /dev/null +++ b/tests/logq_check.c @@ -0,0 +1,189 @@ +// runtime/logq.c: log lines queued for the log thread, with a sink of the test's own that can be slow or held. +// Built and run by tests/logq_check.py; prints each failed check and exits 1 on any. +#include "logq.c" + +#include +#include + +static int fails; +#define CHECK(c, ...) do { if (!(c)) { fails++; printf("FAIL line %d: ", __LINE__); printf(__VA_ARGS__); printf("\n"); } } while (0) + +// ---- the sink: everything written, in the order it arrived; each call can take sink_ms, or wait while held is set +#define OUT_CAP (64u << 20) +static char *out; +static volatile size_t out_n; +static volatile int32_t sink_ms, held, sink_calls, in_sink; +static void sink(const char *p, size_t n) { + plat_xchg32(&in_sink, 1); + while (__atomic_load_n(&held, __ATOMIC_ACQUIRE)) plat_sleep_ms(1); + int ms = __atomic_load_n(&sink_ms, __ATOMIC_ACQUIRE); + if (ms) plat_sleep_ms((uint32_t)ms); + size_t at = __atomic_fetch_add(&out_n, n, __ATOMIC_SEQ_CST); + if (at + n <= OUT_CAP) memcpy(out + at, p, n); + __atomic_add_fetch(&sink_calls, 1, __ATOMIC_SEQ_CST); + plat_xchg32(&in_sink, 0); +} + +// ---- the runtime logq.c reaches: its line about slow writes comes through here +void rt_log(const char *fmt, ...) { + char buf[512]; + va_list ap; + va_start(ap, fmt); + int n = vsnprintf(buf, sizeof buf - 1, fmt, ap); + va_end(ap); + if (n < 0) return; + if (n > (int)sizeof buf - 2) n = (int)sizeof buf - 2; + buf[n++] = '\n'; + logq_write(buf, (size_t)n); +} + +// ---- producers: thread t writes lines "t \n" +enum { THREADS = 8 }; +typedef struct { int id, lines, pad; uint64_t worst_ns; } Prod; +static void producer(void *arg) { + Prod *p = arg; + char line[512]; + for (int i = 0; i < p->lines; i++) { + int n = snprintf(line, sizeof line, "t %d %d %.*s\n", p->id, i, p->pad, + "................................................................................................" + "................................................................................................" + "................................................................................................"); + uint64_t t0 = plat_now_ns(); + logq_write(line, (size_t)n); + uint64_t d = plat_now_ns() - t0; + if (d > p->worst_ns) p->worst_ns = d; + } +} + +// Waits until the sink has taken everything written so far. +static void settle(size_t want) { + for (int i = 0; i < 20000 && __atomic_load_n(&out_n, __ATOMIC_SEQ_CST) < want; i++) plat_sleep_ms(1); +} + +// Every line of the output from at on is whole, and each thread's lines come in their order. Returns the lines seen; +// the slow-write lines ("log: ...") are counted in *notes. +static int check_lines(size_t at, int *next, int *notes) { + int lines = 0; + size_t end = __atomic_load_n(&out_n, __ATOMIC_SEQ_CST); + while (at < end) { + const char *nlp = memchr(out + at, '\n', end - at); + if (!nlp) { CHECK(0, "the output ends inside a line"); break; } + int t, seq; + if (!strncmp(out + at, "log: ", 5)) { + ++*notes; + } else if (sscanf(out + at, "t %d %d", &t, &seq) == 2 && t >= 0 && t < THREADS) { + if (seq != next[t]) CHECK(0, "thread %d: line %d came where %d was due", t, seq, next[t]); + next[t] = seq + 1; + lines++; + } else { + CHECK(0, "a torn line at byte %zu: %.40s", at, out + at); + } + at = (size_t)(nlp - out) + 1; + } + return lines; +} + +static size_t run(Prod *pr, int n, int lines, int pad) { + PlatThread *th[THREADS]; + size_t bytes = 0; + for (int i = 0; i < n; i++) { + pr[i] = (Prod){.id = i, .lines = lines, .pad = pad}; + th[i] = plat_thread_new(producer, &pr[i], "producer"); + if (!th[i]) { printf("FATAL: no thread\n"); exit(3); } + } + for (int i = 0; i < n; i++) plat_thread_join(th[i]); + for (int i = 0; i < n; i++) + for (int k = 0; k < lines; k++) bytes += (size_t)snprintf(NULL, 0, "t %d %d %.*s\n", i, k, pad, ""); + return bytes + (size_t)n * (size_t)lines * (size_t)pad; +} + +int main(void) { + setvbuf(stdout, NULL, _IONBF, 0); + out = malloc(OUT_CAP); + if (!out) return 3; + Prod pr[THREADS]; + int next[THREADS] = {0}, notes = 0; + logq_init(sink); + + // 1. Before the queue is on, a line is written by the thread that logs it, before logq_write returns. + logq_write("t 0 0 \n", 7); + CHECK(out_n == 7 && sink_calls == 1, "a line without the queue: %zu bytes in %d writes", (size_t)out_n, (int)sink_calls); + size_t at = 0; + CHECK(check_lines(at, next, ¬es) == 1, "the first line"); + at = out_n; + for (int i = 0; i < THREADS; i++) next[i] = 0; + + // 2. With the queue: eight threads, 20,000 lines each; every line arrives whole and each thread's in order. + CHECK(logq_async(), "the log thread did not start"); + size_t want = at + run(pr, THREADS, 20000, 40); + settle(want); + CHECK(out_n == want, "%zu bytes written, %zu queued", (size_t)out_n - at, want - at); + CHECK(check_lines(at, next, ¬es) == THREADS * 20000, "lines lost or doubled"); + CHECK(sink_calls < THREADS * 20000 / 2, "%d writes for %d lines: the queue did not batch", (int)sink_calls, THREADS * 20000); + at = out_n; + for (int i = 0; i < THREADS; i++) next[i] = 0; + + // 3. A slow destination (30 ms a write) does not hold the threads that log: 100 lines, a millisecond apart. + __atomic_store_n(&sink_ms, 30, __ATOMIC_RELEASE); + uint64_t worst = 0; + for (int i = 0; i < 100; i++) { + char line[64]; + int n = snprintf(line, sizeof line, "t 0 %d \n", i); + uint64_t t0 = plat_now_ns(); + logq_write(line, (size_t)n); + uint64_t d = plat_now_ns() - t0; + if (d > worst) worst = d; + want += (size_t)n; + plat_sleep_ms(1); + } + CHECK(worst < 15000000, "a line waited %.1f ms for a 30 ms write", worst / 1e6); + settle(want); + __atomic_store_n(&sink_ms, 0, __ATOMIC_RELEASE); + settle(want); + plat_sleep_ms(50); // the log thread's line about the slow writes + CHECK(check_lines(at, next, ¬es) == 100, "lines lost behind a slow write"); + CHECK(notes == 1, "%d lines about slow writes within ten seconds, want 1", notes); + want = at = out_n; + for (int i = 0; i < THREADS; i++) next[i] = 0; + + // 4. A destination that stops while more is logged than the queue holds: the threads wait, nothing is lost. + __atomic_store_n(&held, 1, __ATOMIC_RELEASE); + logq_write("t 0 0 \n", 7); // the log thread takes it and stops in the sink + for (int i = 0; i < 5000 && !__atomic_load_n(&in_sink, __ATOMIC_ACQUIRE); i++) plat_sleep_ms(1); + CHECK(in_sink, "the log thread did not reach the sink"); + PlatThread *th[THREADS]; + for (int i = 0; i < THREADS; i++) { + pr[i] = (Prod){.id = i, .lines = 2000, .pad = 250}; // 8 x 2000 x 260 bytes: four times the queue + th[i] = plat_thread_new(producer, &pr[i], "producer"); + } + plat_sleep_ms(200); + size_t stuck = out_n; + __atomic_store_n(&held, 0, __ATOMIC_RELEASE); + for (int i = 0; i < THREADS; i++) plat_thread_join(th[i]); + plat_sleep_ms(200); + CHECK(stuck == at, "%zu bytes were written while the destination was stopped", stuck - at); + at += 7; // past the line that stopped the log thread + CHECK(check_lines(at, next, ¬es) == THREADS * 2000, "lines lost at a full queue"); + at = out_n; + for (int i = 0; i < THREADS; i++) next[i] = 0; + + // 5. logq_sync: what is queued is written before it returns, and later lines are written by their own thread. + __atomic_store_n(&sink_ms, 20, __ATOMIC_RELEASE); + for (int i = 0; i < 50; i++) { + char line[64]; + int n = snprintf(line, sizeof line, "t 1 %d \n", i); + logq_write(line, (size_t)n); + } + logq_sync(); + __atomic_store_n(&sink_ms, 0, __ATOMIC_RELEASE); + CHECK(check_lines(at, next, ¬es) == 50, "logq_sync returned before the queued lines were written"); + at = out_n; + int calls = sink_calls; + logq_write("t 2 0 \n", 7); + CHECK(out_n == at + 7 && sink_calls == calls + 1, "a line after logq_sync was not written at once"); + logq_sync(); // a second call does nothing + CHECK(check_lines(at, next, ¬es) == 1, "the line after logq_sync"); + + printf("logq_check: %s (%zu bytes in %d writes)\n", fails ? "FAILED" : "ok", (size_t)out_n, (int)sink_calls); + return fails ? 1 : 0; +} diff --git a/tests/logq_check.py b/tests/logq_check.py new file mode 100644 index 00000000..8af0e2b3 --- /dev/null +++ b/tests/logq_check.py @@ -0,0 +1,52 @@ +"""Build and run tests/logq_check.c: the queue that hands log lines to the log thread (runtime/logq.c), with the +platform layer linked and a destination of the test's own. No game files: the module layout the runtime includes is a +stand-in written to the test's build folder. + +Usage: logq_check.py +""" +import os +import shutil +import subprocess +import sys +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] +MSYS_BIN = Path(r'C:\msys64\ucrt64\bin') + + +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' + +LAYOUT = '''// A stand-in for the generated layout.h (tests/logq_check.py). +#pragma once +#define MOD_MAIN_BASE 0x7000000000ull +#define MOD_MAIN_SIZE 0x4ea0000ull +''' + + +def main(): + work = ROOT / 'build' / 'test' / 'logq' + work.mkdir(parents=True, exist_ok=True) + (work / 'layout.h').write_text(LAYOUT) + win = os.name == 'nt' + exe = work / ('logq_check.exe' if win else 'logq_check') + env = dict(os.environ) + if win and MSYS_BIN.exists(): + env['PATH'] = str(MSYS_BIN) + os.pathsep + env.get('PATH', '') + rt = ROOT / 'runtime' + plat = [rt / 'platform' / ('win32.c' if win else 'linux.c'), rt / 'platform' / 'common.c'] + libs = ['-lbcrypt', '-lpsapi', '-lwinmm', '-ldbghelp', '-lshell32', '-lole32', '-luser32', '-ladvapi32'] if win \ + else ['-lpthread'] + 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' / 'logq_check.c'), *map(str, plat), '-o', str(exe), '-lm', *libs], + env=env) + return subprocess.run([str(exe)], env=env, timeout=300).returncode + + +if __name__ == '__main__': + sys.exit(main())