Free released recording copies outside their lock, skip the yield's lock and scans when nobody waits for a core, and wait instead of spinning before a paced frame.

Found by profiling a play session and reading the kernel and the command recording:
- nvn_api_cmd.c: a released recording copy had its pins uncounted (a walk over every op) and was freed while copy_lock was held exclusively; the main-loop thread waited for that lock 2.8% of its time. The copies are now chained under the lock and freed after it. The walks over a recording's records test a mask of the five op kinds that name pins before calling op_pins.
- kernel.c: SleepThread(0) took the kernel lock and computed the caller's inherited priority, a scan over every blocked thread, also when no thread waited for a core, which is nearly always. It now yields at once then. A wait without a timeout no longer reads the clock.
- nvn_api_obj.c sleep_until: the wait for a paced frame's slot slept coarsely and left the last 1 to 2 ms to a yield loop; the high-resolution wait now takes all but its own margin.
- input.c: the keyboard scan (a system call per key) is reused for reads within a millisecond.
- nvn_api_core.c: the read-lock observer's commit-point address is 1.2.1's and is now only compared there.
- kernel_probe.c: the probe expected three virtual cores and wait type 3 to be refused; the kernel has six cores and implements the 64-bit wait. It now checks both as they are, counts a core number past the cores as a clash without unbalancing core 0, and --probe-kernel passes again.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
(cherry picked from commit a3dbd63878d7d6de096ceb4c25988192b4bce738)
This commit is contained in:
chrg6
2026-11-09 20:43:31 -05:00
committed by tangerineturtle1
parent 0703cfa944
commit 745d48bc8c
7 changed files with 88 additions and 22 deletions

View File

@@ -267,7 +267,7 @@ static uint32_t block_inner(RtThread *t, int kind, int64_t ns) {
t->wkind = W_NONE;
return R_TIMEOUT;
}
uint64_t now = kern_now_ns();
uint64_t now = ns < 0 ? 0 : kern_now_ns(); // a wait without a timeout reads no clock (G is held here)
int forever = ns < 0 || (uint64_t)ns > UINT64_MAX - now;
uint64_t deadline = forever ? 0 : now + (uint64_t)ns;
while (!t->woke) {
@@ -1187,6 +1187,8 @@ void kern_core_stats(int *held_now, int *held_max) {
unlock();
}
int kern_cores(void) { return g_active_cores; }
void kern_core_stats_reset(void) {
lock();
g_held_max = 0;
@@ -1747,12 +1749,21 @@ static int kern_svc_inner(Cpu *c, uint32_t imm) {
unlock();
return 1;
}
// Yield: hand the core to a waiting thread of equal or better priority.
// Yield: hand the core to a waiting thread of equal or better priority. Most yields find nobody waiting for a
// core: those take neither G nor the priority scan over every blocked thread (prio_of), which the game's yield
// loops paid thousands of times a second. The look is as racy as rt_spin_gate's: a thread that queues for a
// core right after it is found by the next yield, and kicks the owner itself in core_take meanwhile.
if (!__atomic_load_n(&g_core_wait, __ATOMIC_RELAXED)) {
plat_yield();
return 1;
}
lock();
int give = 0;
int tp = prio_of(t);
int give = 0, tp = -1;
for (RtThread *w = g_core_wait; w && !give; w = w->cnext)
give = t->cur_core >= 0 && core_allowed(w, t->cur_core) && prio_of(w) <= tp;
if (t->cur_core >= 0 && core_allowed(w, t->cur_core)) {
if (tp < 0) tp = prio_of(t); // only once a waiter may run on this core
give = prio_of(w) <= tp;
}
if (give) {
core_put(t);
core_take(t);