Merge the two performance changes into the 0.7.0 release line: a signalled event wakes only its own waiters, and a thread of its own writes the log.

One conflict, the list of setting names, which keeps both sides' additions.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
chrg6
2026-11-10 07:32:34 -05:00
17 changed files with 778 additions and 28 deletions

View File

@@ -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", "POOL_LOAD_BACKLOG", "POOL_LOAD_MS",
"VMA_BLOCK_MB", "ASTC_BC_PROBE",
"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).

View File

@@ -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);

View File

@@ -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)

View File

@@ -87,6 +87,8 @@ struct RtThread {
int st; // S_* state since st_since
uint64_t st_since, st_ns[S_N], nblk, npre, pre_since, cpu_last;
uint64_t wobj; // what the current wait is on: owner handle, key, address or first handle
const uint32_t *whs; // a WaitSynchronization that blocks: its handles, in the waiter's own memory (holding G)
int wn;
uint32_t waker; // the thread whose signal ended the last wait (0: a host thread or a timeout)
uint64_t wake_at, grant_at; // when a wait was signalled and a core granted, until the thread runs again
uint64_t lw_ns, lw_n, lw_max, lg_ns, lg_n, lg_max, sl_n, sl_req, sl_act; // those latencies; SleepThread asked/took
@@ -153,6 +155,7 @@ typedef struct { int st; uint32_t waker; uint64_t obj, ns; } WaitRec;
static WaitRec g_rw[RW_N], g_mw[RW_N]; // the render and main thread's waits by object and waker
static uint64_t g_pre_req, g_pre_n, g_pre_ns, g_pre_max, g_pre_stale; // preempt requests; releases after one and their latency
static uint64_t g_sync_calls, g_sync_blocks; // WaitSynchronization calls and the blocks inside them
static uint64_t g_sync_woken, g_sync_spared; // such waits a signal woke, and those it left asleep
static void wait_add(WaitRec *w, int st, uint64_t obj, uint32_t waker, uint64_t ns) {
for (int i = 0; i < RW_N; i++)
@@ -246,10 +249,31 @@ static void wake(RtThread *t, uint32_t res) {
plat_cond_signal(&t->cv);
}
static void wake_sync_waiters(void) {
// 1 when one of the handles w waits on names obj, an event (either a readable handle of it) or a thread.
static int sync_watches(const RtThread *w, const void *obj) {
for (int i = 0; i < w->wn; i++) {
unsigned k = w->whs[i] - 0x1000;
if (k > 0 && k < MAX_H && g_h[k].obj == obj && (g_h[k].type == O_EVENT_R || g_h[k].type == O_THREAD)) return 1;
}
return 0;
}
// obj became signalled: an event (its Event) or a thread that exited. The WaitSynchronization waits that hold a handle
// of it are woken to look again. Every other such wait stays asleep: woken, it took a virtual core (block: core_take,
// which asks a running thread of lower priority for its core when every core is held), found nothing signalled and
// blocked again, and the audio renderer alone signals its event 200 times a second, each time waking every thread
// that waits for a message, a timer or the end of another thread. NX2NAVI_SYNC_WAKE_ALL=1 wakes them all, as before
// ("ab": in the B blocks of NX2NAVI_AB); the sched line counts the waits woken and left asleep.
static void wake_sync_waiters(const void *obj) {
static int mode = -1;
if (mode < 0) mode = rt_knob("SYNC_WAKE_ALL", "0");
int all = RT_KNOB_ON(mode);
for (RtThread *w = g_wait; w;) {
RtThread *n = w->next;
if (w->wkind == W_SYNC) wake(w, (uint32_t)-1);
if (w->wkind == W_SYNC) {
if (all || sync_watches(w, obj)) g_sync_woken++, wake(w, (uint32_t)-1);
else g_sync_spared++;
}
w = n;
}
}
@@ -1184,7 +1208,7 @@ void kern_sched_report(uint64_t frame) {
}
}
uint64_t pre_stale = g_pre_stale, pre_req = g_pre_req, pre_n = g_pre_n, pre_ns = g_pre_ns, pre_max = g_pre_max;
uint64_t sc = g_sync_calls, sb = g_sync_blocks;
uint64_t sc = g_sync_calls, sb = g_sync_blocks, sw = g_sync_woken, ss = g_sync_spared;
for (RtThread *t = g_all; t; t = t->all) {
memset(t->st_ns, 0, sizeof t->st_ns);
t->nblk = t->npre = 0;
@@ -1194,14 +1218,16 @@ void kern_sched_report(uint64_t frame) {
memset(g_rw, 0, sizeof g_rw);
memset(g_mw, 0, sizeof g_mw);
g_pre_req = g_pre_n = g_pre_ns = g_pre_max = g_pre_stale = 0;
g_sync_calls = g_sync_blocks = 0;
g_sync_calls = g_sync_blocks = g_sync_woken = g_sync_spared = 0;
unlock();
if (!span) return;
rt_log("sched: frame %llu: %.2f s, cores busy %.2f of %d, core wait %.2f thread-s, preempt req %llu released %llu "
"avg %.2f ms max %.2f ms, WaitSynchronization %llu calls %llu blocks, stale preempt %llu%s",
"avg %.2f ms max %.2f ms, WaitSynchronization %llu calls %llu blocks (signals woke %llu, left %llu asleep), "
"stale preempt %llu%s",
(unsigned long long)frame, span / 1e9, (double)all_run / span, NCORE, all_core / 1e9,
(unsigned long long)pre_req, (unsigned long long)pre_n, pre_n ? pre_ns / 1e6 / pre_n : 0.0, pre_max / 1e6,
(unsigned long long)sc, (unsigned long long)sb, (unsigned long long)pre_stale, self ? "" : " (render thread is not a guest thread)");
(unsigned long long)sc, (unsigned long long)sb, (unsigned long long)sw, (unsigned long long)ss,
(unsigned long long)pre_stale, self ? "" : " (render thread is not a guest thread)");
for (int i = 0; i < nr; i++) {
Row *r = &rows[i];
char d[128], s[256];
@@ -1336,7 +1362,7 @@ static void thread_finish(RtThread *t) {
if (t->hthr) plat_intr_free(t->hthr); // no waiter interrupts a thread without a core
t->hthr = NULL;
t->exited = 1;
wake_sync_waiters();
wake_sync_waiters(t);
if (t->closed) thr_free(t);
unlock();
}
@@ -1858,7 +1884,7 @@ static int kern_svc_inner(Cpu *c, uint32_t imm) {
if (r == R_BAD_HANDLE) rt_log("kernel: event svc %#x: invalid handle %#x", imm, h);
} else if (imm == 0x11) {
e->signaled = 1;
wake_sync_waiters();
wake_sync_waiters(e);
} else if (imm == 0x17 && !e->signaled) {
r = R_BAD_STATE;
} else {
@@ -1939,7 +1965,9 @@ static int kern_svc_inner(Cpu *c, uint32_t imm) {
}
t->wobj = n ? hs[0] | (uint64_t)n << 32 : 0;
g_sync_blocks++;
t->whs = hs, t->wn = n;
woke = block(t, W_SYNC, left);
t->whs = NULL, t->wn = 0;
if (woke == R_TIMEOUT) {
r = R_TIMEOUT;
break;
@@ -2010,7 +2038,7 @@ void kern_event_signal(uint32_t hw) {
Event *e = h_peek(hw, O_EVENT_W);
if (!e) rt_fatal("kern_event_signal: %#x is not a writable event handle", hw);
e->signaled = 1;
wake_sync_waiters();
wake_sync_waiters(e);
unlock();
}

141
runtime/logq.c Normal file
View File

@@ -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 <stdlib.h>
#include <string.h>
#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);
}

10
runtime/logq.h Normal file
View File

@@ -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 <stddef.h>
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

View File

@@ -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);

View File

@@ -1672,7 +1672,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

View File

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

View File

@@ -1135,7 +1135,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