mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-16 18:09:58 -04:00
Wake only the threads that wait for the event that was signalled.
A signalled kernel event, or a thread that ended, woke every thread blocked in WaitSynchronization. Each woken thread takes a virtual core before it looks at its handles, asking a running thread of lower priority for its core when all are held, finds nothing signalled and blocks again. The audio renderer signals its event 200 times a second, so every thread that waits for a message, a timer or another thread's end did this 200 times a second at the cost of the threads that were working. A wait now records its handle list, and a signal wakes the waits that hold a handle of the signalled event or thread. NX2NAVI_SYNC_WAKE_ALL=1 (or ab) wakes them all as before, and the sched line counts the waits woken and left asleep. tests/sync_wake_check.py builds kernel.c with the rest of the runtime stubbed and checks the wakes, the timeouts, the cancel and that none of 4,000 signals to eight looping waiters is lost, in both modes. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
@@ -57,6 +57,9 @@ keeps per-core data without locks and relies on that, so the host models it (`ru
|
||||
and, if its host rip is in lifted code at host depth 0, moves it to `kern_ap_stub`, which saves the volatile
|
||||
registers and gives the core up there. A holder of a guest mutex counts at the priority of its best waiter, and one
|
||||
inside an SDK spin lock is not preempted.
|
||||
- Wakes. A signalled event or a thread that ends wakes only the `WaitSynchronization` waits that hold a handle of it
|
||||
(`wake_sync_waiters`): a woken thread takes a core before it looks, so waking the others cost running threads
|
||||
their cores for nothing (`NX2NAVI_SYNC_WAKE_ALL=1` wakes them all).
|
||||
- Host work gives the core back. Queue submits, presents, GPU waits, shader translation, `nn::fs` calls and NVN memory
|
||||
pool setup run without a core (`NVN_BLOCKING`, `kern_core_away`).
|
||||
- Spin waits. Loops that only another thread can end send their back-edges through `rt_spin_gate`, which pauses the
|
||||
|
||||
@@ -278,6 +278,13 @@ A change to the step Ocarina is given each frame (`runtime/frame_rate.c` `ocarin
|
||||
display times: a held 60 fps limit gives exactly one frame every frame, a 240 fps limit never less than a quarter
|
||||
frame, uneven frame times one steady step, and game time follows the clock. No game files.
|
||||
|
||||
A change to which waits a signalled event or an ended thread wakes (`runtime/kernel.c` `wake_sync_waiters`, the
|
||||
`WaitSynchronization` service) passes `python tests\sync_wake_check.py`, which builds `kernel.c` with the platform
|
||||
layer and the rest of the runtime stubbed and runs guest threads whose entries are host functions: a signal wakes the
|
||||
waits on its event and leaves the others asleep, a wait on several handles reports the right one, a thread's end ends
|
||||
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 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`.
|
||||
|
||||
@@ -605,6 +605,11 @@
|
||||
# Log kernel services that block longer than this many ms, with the service number (0: off). A stalled frame shows up as
|
||||
# time in the replay; this says whether the guest was waiting in a service, and which one.
|
||||
# svc_slow_ms = 0
|
||||
# 1: every signalled event and every thread that ends wakes all the game threads that wait for any event, as before;
|
||||
# each takes one of the game's virtual cores to find that its own event is not the one, often from a thread that was
|
||||
# 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: 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
|
||||
|
||||
@@ -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",
|
||||
"VMA_BLOCK_MB", "ASTC_BC_PROBE", "SYNC_WAKE_ALL",
|
||||
};
|
||||
|
||||
// The nx2navi.ini section a key cfg_set may write goes to when the file lacks it (nx2navi.ini.example's sections).
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
221
tests/sync_wake_check.c
Normal file
221
tests/sync_wake_check.c
Normal file
@@ -0,0 +1,221 @@
|
||||
// runtime/kernel.c: which WaitSynchronization waits a signalled event or an ended thread wakes (wake_sync_waiters),
|
||||
// on guest threads whose entries are host functions, with the rest of the runtime stubbed. Built and run by
|
||||
// tests/sync_wake_check.py; prints each failed check and exits 1 on any. With the argument "all" it runs with
|
||||
// NX2NAVI_SYNC_WAKE_ALL=1 and checks that every wait is woken and blocks again.
|
||||
#include "kernel.c"
|
||||
|
||||
#include <stdarg.h>
|
||||
|
||||
static int fails, wake_all;
|
||||
#define CHECK(c, ...) do { if (!(c)) { fails++; printf("FAIL line %d: ", __LINE__); printf(__VA_ARGS__); printf("\n"); } } while (0)
|
||||
|
||||
// ---- the runtime kernel.c reaches
|
||||
unsigned rt_trace;
|
||||
uint64_t rt_guest_start_ns;
|
||||
volatile uint32_t prof_replay_tid;
|
||||
const char *cfg_get(const char *name) { return !strcmp(name, "NX2NAVI_SYNC_WAKE_ALL") && wake_all ? "1" : NULL; }
|
||||
const char *cfg_value(const char *name) { (void)name; return "0"; } // no asynchronous preemption, no slow-service log
|
||||
int cfg_bool(const char *name) { (void)name; return 0; }
|
||||
int rt_ab(void) { return 0; }
|
||||
int rt_knob(const char *name, const char *def) {
|
||||
char key[64];
|
||||
snprintf(key, sizeof key, "NX2NAVI_%s", name);
|
||||
const char *e = cfg_get(key);
|
||||
return strcmp(e && *e ? e : def, "0") != 0;
|
||||
}
|
||||
int64_t frame_step_world_us(void) { return -1; }
|
||||
void ipc_handle_closed(int kind, void *obj) { (void)kind, (void)obj; }
|
||||
const char *ldr_describe(uint64_t a, char *buf, int n) { snprintf(buf, (size_t)n, "%#llx", (unsigned long long)a); return buf; }
|
||||
uint64_t ldr_symbol(const char *name) { (void)name; return 0; }
|
||||
void perf_main_thread(void) {}
|
||||
int rt_linked_modules(RtModule *out, int max) { (void)out, (void)max; return 0; }
|
||||
const RtGuestLayout *mem_layout(void) { return NULL; }
|
||||
void *mem_kernel_alloc(uint64_t size, uint64_t align) {
|
||||
void *p = plat_aligned_alloc(size, align < 64 ? 64 : align);
|
||||
if (!p) rt_fatal("out of memory");
|
||||
memset(p, 0, size);
|
||||
return p;
|
||||
}
|
||||
void mem_kernel_free(void *p, uint64_t size) { (void)size; plat_aligned_free(p); }
|
||||
void rt_log(const char *fmt, ...) { (void)fmt; }
|
||||
void rt_logf(const char *fmt, ...) { (void)fmt; }
|
||||
void rt_fatal(const char *fmt, ...) {
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
printf("FATAL: ");
|
||||
vprintf(fmt, ap);
|
||||
printf("\n");
|
||||
va_end(ap);
|
||||
fflush(stdout);
|
||||
_Exit(3);
|
||||
}
|
||||
void rt_dispatch(Cpu *c) { (void)c; rt_fatal("rt_dispatch in the test"); }
|
||||
void rt_svc(Cpu *c, uint32_t imm, uint64_t pc) { (void)pc; kern_svc(c, imm); }
|
||||
// A guest entry here is a host function taking the thread's Cpu, with its argument in x0.
|
||||
uint64_t rt_call(Cpu *c, uint64_t fn, int n, ...) {
|
||||
va_list ap;
|
||||
va_start(ap, n);
|
||||
for (int i = 0; i < n; i++) c->x[i] = va_arg(ap, uint64_t);
|
||||
va_end(ap);
|
||||
((void (*)(Cpu *))(uintptr_t)fn)(c);
|
||||
return c->x[0];
|
||||
}
|
||||
|
||||
// ---- waiters
|
||||
typedef struct {
|
||||
uint32_t hs[2];
|
||||
int n, loop; // loop: clear the event and wait again after each wake, counting in `wakes`
|
||||
int64_t ns;
|
||||
volatile uint32_t res, thread;
|
||||
volatile int idx, done, wakes;
|
||||
} Wait;
|
||||
|
||||
static void waiter(Cpu *c) {
|
||||
Wait *w = (Wait *)(uintptr_t)c->x[0];
|
||||
do {
|
||||
c->x[1] = (uint64_t)(uintptr_t)w->hs, c->x[2] = (uint64_t)w->n, c->x[3] = (uint64_t)w->ns;
|
||||
kern_svc(c, 0x18);
|
||||
w->res = (uint32_t)c->x[0], w->idx = (int)c->x[1];
|
||||
if (w->loop && !w->res) {
|
||||
c->x[0] = w->hs[0];
|
||||
kern_svc(c, 0x12); // ClearEvent on the readable end
|
||||
__atomic_add_fetch(&w->wakes, 1, __ATOMIC_SEQ_CST);
|
||||
}
|
||||
} while (w->loop > 0 && !w->res);
|
||||
__atomic_store_n(&w->done, 1, __ATOMIC_SEQ_CST);
|
||||
kern_svc(c, 0x0A);
|
||||
rt_fatal("ExitThread returned");
|
||||
}
|
||||
|
||||
static uint32_t spawn(Wait *w) {
|
||||
Cpu c = {0};
|
||||
c.x[1] = (uint64_t)(uintptr_t)waiter, c.x[2] = (uint64_t)(uintptr_t)w;
|
||||
c.x[3] = (uint64_t)(uintptr_t)mem_kernel_alloc(0x4000, 16) + 0x4000;
|
||||
c.x[4] = 44, c.x[5] = (uint64_t)(int64_t)-2;
|
||||
kern_svc(&c, 0x08);
|
||||
if (c.x[0]) rt_fatal("CreateThread failed %#llx", (unsigned long long)c.x[0]);
|
||||
w->thread = (uint32_t)c.x[1];
|
||||
c.x[0] = w->thread;
|
||||
kern_svc(&c, 0x09);
|
||||
return w->thread;
|
||||
}
|
||||
|
||||
static int until(volatile int *flag, int want, int ms) {
|
||||
for (int i = 0; i < ms * 10; i++) {
|
||||
if (__atomic_load_n(flag, __ATOMIC_SEQ_CST) == want) return 1;
|
||||
plat_wait_ns(100000);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int in_sync(uint32_t thread, int ms) {
|
||||
for (int i = 0; i < ms * 10; i++) {
|
||||
if (kern_thread_state(thread) == KERN_WAIT_SYNC) return 1;
|
||||
plat_wait_ns(100000);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void counts(uint64_t *woken, uint64_t *spared, uint64_t *blocks) {
|
||||
lock();
|
||||
*woken = g_sync_woken, *spared = g_sync_spared, *blocks = g_sync_blocks;
|
||||
unlock();
|
||||
}
|
||||
|
||||
// Waits blocked now: those woken for nothing are back in their wait.
|
||||
static void settle(Wait **ws, int n) {
|
||||
plat_wait_ns(20000000);
|
||||
for (int i = 0; i < n; i++) CHECK(in_sync(ws[i]->thread, 2000), "waiter %d is not back in its wait", i);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
wake_all = argc > 1 && !strcmp(argv[1], "all");
|
||||
setvbuf(stdout, NULL, _IONBF, 0);
|
||||
uint32_t ea = kern_event_new(), eb = kern_event_new(), ec = kern_event_new(), ed = kern_event_new();
|
||||
uint32_t ra = kern_event_readable(ea), rb = kern_event_readable(eb), rc = kern_event_readable(ec);
|
||||
uint64_t wk, sp, bl;
|
||||
|
||||
// 1. A waits on a, B on b, C on b and c. A signal of a wakes A; B and C stay in their waits.
|
||||
Wait A = {.hs = {ra}, .n = 1, .ns = -1}, B = {.hs = {rb}, .n = 1, .ns = -1}, C = {.hs = {rb, rc}, .n = 2, .ns = -1};
|
||||
spawn(&A), spawn(&B), spawn(&C);
|
||||
CHECK(in_sync(A.thread, 2000) && in_sync(B.thread, 2000) && in_sync(C.thread, 2000), "the waiters did not block");
|
||||
kern_event_signal(ea);
|
||||
CHECK(until(&A.done, 1, 2000) && A.res == 0 && A.idx == 0, "A: result %#x index %d", A.res, A.idx);
|
||||
settle((Wait *[]){&B, &C}, 2);
|
||||
CHECK(!B.done && !C.done, "a signal of another event ended a wait");
|
||||
// Two wakes pass B and C by: the signal of a, and the end of A's thread.
|
||||
counts(&wk, &sp, &bl);
|
||||
if (wake_all) CHECK(wk == 5 && sp == 0 && bl == 7, "all: woken %llu left %llu blocks %llu, want 5 0 7",
|
||||
(unsigned long long)wk, (unsigned long long)sp, (unsigned long long)bl);
|
||||
else CHECK(wk == 1 && sp == 4 && bl == 3, "woken %llu left %llu blocks %llu, want 1 4 3",
|
||||
(unsigned long long)wk, (unsigned long long)sp, (unsigned long long)bl);
|
||||
|
||||
// 2. A wait on several handles is woken by its second one and reports its index.
|
||||
kern_event_signal(ec);
|
||||
CHECK(until(&C.done, 1, 2000) && C.res == 0 && C.idx == 1, "C: result %#x index %d", C.res, C.idx);
|
||||
CHECK(in_sync(B.thread, 2000) && !B.done, "B left its wait at a signal of c");
|
||||
|
||||
// 3. A thread that ends wakes the wait on its handle: E waits for B's thread, and b ends B.
|
||||
Wait E = {.hs = {B.thread}, .n = 1, .ns = -1};
|
||||
spawn(&E);
|
||||
CHECK(in_sync(E.thread, 2000), "E did not block");
|
||||
kern_event_signal(eb);
|
||||
CHECK(until(&B.done, 1, 2000) && B.res == 0 && B.idx == 0, "B: result %#x index %d", B.res, B.idx);
|
||||
CHECK(until(&E.done, 1, 2000) && E.res == 0 && E.idx == 0, "E: the end of B's thread did not end its wait (result %#x)", E.res);
|
||||
|
||||
// 4. A timed wait keeps its time across other events' signals; a wait without handles is a sleep.
|
||||
kern_event_clear(ea);
|
||||
Wait F = {.hs = {ra}, .n = 1, .ns = 150000000}, Z = {.n = 0, .ns = 150000000};
|
||||
uint64_t t0 = kern_now_ns();
|
||||
spawn(&F), spawn(&Z);
|
||||
CHECK(in_sync(F.thread, 2000) && in_sync(Z.thread, 2000), "F and Z did not block");
|
||||
for (int i = 0; i < 20; i++) kern_event_signal(ed), plat_wait_ns(1000000);
|
||||
CHECK(until(&F.done, 1, 2000) && F.res == R_TIMEOUT, "F: result %#x, want a timeout", F.res);
|
||||
CHECK(until(&Z.done, 1, 2000) && Z.res == R_TIMEOUT, "Z: result %#x, want a timeout", Z.res);
|
||||
CHECK(kern_now_ns() - t0 >= 150000000, "a timed wait ended after %.1f ms, before its 150 ms", (kern_now_ns() - t0) / 1e6);
|
||||
|
||||
// 5. CancelSynchronization ends a wait whatever it waits on.
|
||||
Wait G = {.hs = {ra}, .n = 1, .ns = -1};
|
||||
spawn(&G);
|
||||
CHECK(in_sync(G.thread, 2000), "G did not block");
|
||||
Cpu c = {0};
|
||||
c.x[0] = G.thread;
|
||||
kern_svc(&c, 0x19);
|
||||
CHECK(until(&G.done, 1, 2000) && G.res == R_CANCELLED, "G: result %#x, want cancelled", G.res);
|
||||
|
||||
// 6. No signal is lost: eight threads each wait on their own event in a loop; every signal is seen by its thread.
|
||||
enum { N = 8, ROUNDS = 4000 };
|
||||
static Wait L[N];
|
||||
uint32_t ew[N];
|
||||
for (int i = 0; i < N; i++) {
|
||||
ew[i] = kern_event_new();
|
||||
L[i] = (Wait){.hs = {kern_event_readable(ew[i])}, .n = 1, .ns = -1, .loop = 1};
|
||||
spawn(&L[i]);
|
||||
}
|
||||
unsigned seed = 12345;
|
||||
int lost = 0;
|
||||
for (int r = 0; r < ROUNDS && !lost; r++) {
|
||||
seed = seed * 1664525u + 1013904223u;
|
||||
int i = (int)(seed >> 24) % N;
|
||||
int before = __atomic_load_n(&L[i].wakes, __ATOMIC_SEQ_CST);
|
||||
kern_event_signal(ew[i]);
|
||||
uint64_t end = kern_now_ns() + 5000000000ull;
|
||||
while (__atomic_load_n(&L[i].wakes, __ATOMIC_SEQ_CST) == before && kern_now_ns() < end) plat_yield();
|
||||
if (__atomic_load_n(&L[i].wakes, __ATOMIC_SEQ_CST) == before) {
|
||||
lost = 1;
|
||||
CHECK(0, "round %d: thread %d never saw its signal", r, i);
|
||||
}
|
||||
}
|
||||
int total = 0;
|
||||
for (int i = 0; i < N; i++) total += L[i].wakes;
|
||||
CHECK(lost || total == ROUNDS, "%d wakes for %d signals", total, ROUNDS);
|
||||
for (int i = 0; i < N; i++) { // end them: the last wake is not followed by another wait
|
||||
L[i].loop = -1;
|
||||
kern_event_signal(ew[i]);
|
||||
CHECK(until(&L[i].done, 1, 5000), "looping waiter %d did not end", i);
|
||||
}
|
||||
counts(&wk, &sp, &bl);
|
||||
printf("sync_wake_check%s: %s (signals woke %llu waits and left %llu asleep; %llu blocks)\n", wake_all ? " all" : "",
|
||||
fails ? "FAILED" : "ok", (unsigned long long)wk, (unsigned long long)sp, (unsigned long long)bl);
|
||||
return fails ? 1 : 0;
|
||||
}
|
||||
53
tests/sync_wake_check.py
Normal file
53
tests/sync_wake_check.py
Normal file
@@ -0,0 +1,53 @@
|
||||
"""Build and run tests/sync_wake_check.c: which WaitSynchronization waits a signalled event or an ended thread wakes
|
||||
(runtime/kernel.c wake_sync_waiters), with the platform layer linked and the rest of the runtime stubbed. No game files:
|
||||
the module layout the runtime includes is a stand-in written to the test's build folder.
|
||||
|
||||
Usage: sync_wake_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/sync_wake_check.py).
|
||||
#pragma once
|
||||
#define MOD_MAIN_BASE 0x7000000000ull
|
||||
#define MOD_MAIN_SIZE 0x4ea0000ull
|
||||
'''
|
||||
|
||||
|
||||
def main():
|
||||
work = ROOT / 'build' / 'test' / 'sync_wake'
|
||||
work.mkdir(parents=True, exist_ok=True)
|
||||
(work / 'layout.h').write_text(LAYOUT)
|
||||
win = os.name == 'nt'
|
||||
exe = work / ('sync_wake_check.exe' if win else 'sync_wake_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' / 'sync_wake_check.c'), *map(str, plat), '-o', str(exe), '-lm', *libs],
|
||||
env=env)
|
||||
rc = subprocess.run([str(exe)], env=env, timeout=300).returncode
|
||||
return subprocess.run([str(exe), 'all'], env=env, timeout=300).returncode or rc
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main())
|
||||
Reference in New Issue
Block a user