mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-20 01:49:59 -04:00
Gather the log lines of a quarter of a second and write them at once.
The log thread wrote as soon as it woke, which outside a burst is once for every line: each line woke it and was a write of its own to the log file and the console. It now waits a quarter of a second from the first line it finds, or until the queue is half full, and writes what was queued by then at once, so the log is up to that much behind the game. What is being gathered stays in the queue, so a fatal error, a crash report and an exit write it out before their own lines as before; a game ended from outside loses those last lines. The queue's test checks the gathering, the half-full write and that a process which ends at once, by an exit or as a fatal error does, leaves every line in its file. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
@@ -11,6 +11,15 @@
|
||||
// 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.
|
||||
//
|
||||
// The log thread gathers before it writes (log_main): from the first line it finds it waits LOGQ_GATHER_NS, or until
|
||||
// the queue is half full (LOGQ_GATHER_BYTES), and then writes everything queued by then in one write. It used to
|
||||
// write as soon as it woke, which outside a burst is once for every line: the thread that logged woke the log thread
|
||||
// for each, and each was a write of its own to every destination. The price is that the log file and the console are
|
||||
// up to LOGQ_GATHER_NS behind the game. The lines being gathered are in the queue like any others, so logq_sync
|
||||
// writes them too, and a fatal error, a crash report and an exit still lose none. A process that is ended from
|
||||
// outside runs no code of ours and loses what was gathered at that moment; it used to lose only the lines the log
|
||||
// thread had not reached yet.
|
||||
//
|
||||
// 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 <stdlib.h>
|
||||
@@ -21,6 +30,8 @@
|
||||
#include "platform/platform.h"
|
||||
|
||||
#define LOGQ_CAP (1u << 20)
|
||||
#define LOGQ_GATHER_NS 250000000ull // log_main: a batch is written this long after its first line was found ...
|
||||
#define LOGQ_GATHER_BYTES (LOGQ_CAP / 2) // ... or as soon as this much is queued
|
||||
#define LOGQ_SLOW_NS 2000000ll
|
||||
#define LOGQ_SAY_NS 10000000000ull
|
||||
|
||||
@@ -97,11 +108,12 @@ static int put(const char *p, size_t n) {
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
int first = !Q.n;
|
||||
// the log thread sleeps until the first line of a batch, and then until the batch fills half the queue (log_main)
|
||||
int wake = !Q.n || (Q.n < LOGQ_GATHER_BYTES && Q.n + n >= LOGQ_GATHER_BYTES);
|
||||
memcpy(Q.buf + Q.n, p, n);
|
||||
Q.n += n;
|
||||
plat_mutex_unlock(&Q.q);
|
||||
if (first) plat_cond_signal(&Q.more);
|
||||
if (wake) plat_cond_signal(&Q.more);
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -111,11 +123,18 @@ void logq_write(const char *p, size_t n) {
|
||||
say_slow();
|
||||
}
|
||||
|
||||
// The log thread: it sleeps until a line is queued, gathers, and writes the batch (drain). While it gathers it holds
|
||||
// no lock, so neither the threads that log nor logq_sync wait for it. The gathering ends when its time is up, when
|
||||
// the queue is half full (put signals that), and when the queue is empty again because another thread wrote it: a
|
||||
// line that found no room (put), or logq_sync, after which nothing is queued any more and this thread sleeps for good.
|
||||
static void log_main(void *arg) {
|
||||
(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;
|
||||
spent = plat_now_ns() - t0)
|
||||
plat_cond_wait_ns(&Q.more, &Q.q, LOGQ_GATHER_NS - spent);
|
||||
plat_mutex_unlock(&Q.q);
|
||||
drain();
|
||||
say_slow();
|
||||
|
||||
Reference in New Issue
Block a user