Write the log from a thread of its own, so a stalled write no longer holds the thread that logged.

Every log line was two writes on the thread that logged it, to stderr and to the log file. Such a write can stall for
tens or hundreds of milliseconds (a busy disk, a virus scanner, a console or a pipe that is slow to take it), and the
render thread logs too: a frame it spent in a write was a hitch with nothing else to show for it.

In a game run the lines now go into a queue that a log thread writes out in order (runtime/logq.c). A fatal error,
the crash report and the end of the process write the queue out on their own thread first and write their own lines
directly, so nothing is lost to an exit. A thread that finds the queue's lock held for long, or the queue full, writes
its line itself. NX2NAVI_LOG_ASYNC=0 writes every line where it is logged, as before.

Every write is timed in both modes: a "log: ... writes of the log took over 2 ms" line counts the slow ones, gives
the longest and says whose time they took.

tests/logq_check.py checks order and completeness with eight threads, a slow destination, a stopped destination with
the queue full, and the synchronous flush.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
chrg6
2026-11-10 07:22:41 -05:00
parent 094573bd26
commit a03e059d2b
14 changed files with 452 additions and 19 deletions

View File

@@ -610,6 +610,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