mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-18 23:59:59 -04:00
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:
189
tests/logq_check.c
Normal file
189
tests/logq_check.c
Normal file
@@ -0,0 +1,189 @@
|
||||
// runtime/logq.c: log lines queued for the log thread, with a sink of the test's own that can be slow or held.
|
||||
// Built and run by tests/logq_check.py; prints each failed check and exits 1 on any.
|
||||
#include "logq.c"
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
|
||||
static int fails;
|
||||
#define CHECK(c, ...) do { if (!(c)) { fails++; printf("FAIL line %d: ", __LINE__); printf(__VA_ARGS__); printf("\n"); } } while (0)
|
||||
|
||||
// ---- the sink: everything written, in the order it arrived; each call can take sink_ms, or wait while held is set
|
||||
#define OUT_CAP (64u << 20)
|
||||
static char *out;
|
||||
static volatile size_t out_n;
|
||||
static volatile int32_t sink_ms, held, sink_calls, in_sink;
|
||||
static void sink(const char *p, size_t n) {
|
||||
plat_xchg32(&in_sink, 1);
|
||||
while (__atomic_load_n(&held, __ATOMIC_ACQUIRE)) plat_sleep_ms(1);
|
||||
int ms = __atomic_load_n(&sink_ms, __ATOMIC_ACQUIRE);
|
||||
if (ms) plat_sleep_ms((uint32_t)ms);
|
||||
size_t at = __atomic_fetch_add(&out_n, n, __ATOMIC_SEQ_CST);
|
||||
if (at + n <= OUT_CAP) memcpy(out + at, p, n);
|
||||
__atomic_add_fetch(&sink_calls, 1, __ATOMIC_SEQ_CST);
|
||||
plat_xchg32(&in_sink, 0);
|
||||
}
|
||||
|
||||
// ---- the runtime logq.c reaches: its line about slow writes comes through here
|
||||
void rt_log(const char *fmt, ...) {
|
||||
char buf[512];
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
int n = vsnprintf(buf, sizeof buf - 1, fmt, ap);
|
||||
va_end(ap);
|
||||
if (n < 0) return;
|
||||
if (n > (int)sizeof buf - 2) n = (int)sizeof buf - 2;
|
||||
buf[n++] = '\n';
|
||||
logq_write(buf, (size_t)n);
|
||||
}
|
||||
|
||||
// ---- producers: thread t writes lines "t <t> <seq> <padding>\n"
|
||||
enum { THREADS = 8 };
|
||||
typedef struct { int id, lines, pad; uint64_t worst_ns; } Prod;
|
||||
static void producer(void *arg) {
|
||||
Prod *p = arg;
|
||||
char line[512];
|
||||
for (int i = 0; i < p->lines; i++) {
|
||||
int n = snprintf(line, sizeof line, "t %d %d %.*s\n", p->id, i, p->pad,
|
||||
"................................................................................................"
|
||||
"................................................................................................"
|
||||
"................................................................................................");
|
||||
uint64_t t0 = plat_now_ns();
|
||||
logq_write(line, (size_t)n);
|
||||
uint64_t d = plat_now_ns() - t0;
|
||||
if (d > p->worst_ns) p->worst_ns = d;
|
||||
}
|
||||
}
|
||||
|
||||
// Waits until the sink has taken everything written so far.
|
||||
static void settle(size_t want) {
|
||||
for (int i = 0; i < 20000 && __atomic_load_n(&out_n, __ATOMIC_SEQ_CST) < want; i++) plat_sleep_ms(1);
|
||||
}
|
||||
|
||||
// Every line of the output from at on is whole, and each thread's lines come in their order. Returns the lines seen;
|
||||
// the slow-write lines ("log: ...") are counted in *notes.
|
||||
static int check_lines(size_t at, int *next, int *notes) {
|
||||
int lines = 0;
|
||||
size_t end = __atomic_load_n(&out_n, __ATOMIC_SEQ_CST);
|
||||
while (at < end) {
|
||||
const char *nlp = memchr(out + at, '\n', end - at);
|
||||
if (!nlp) { CHECK(0, "the output ends inside a line"); break; }
|
||||
int t, seq;
|
||||
if (!strncmp(out + at, "log: ", 5)) {
|
||||
++*notes;
|
||||
} else if (sscanf(out + at, "t %d %d", &t, &seq) == 2 && t >= 0 && t < THREADS) {
|
||||
if (seq != next[t]) CHECK(0, "thread %d: line %d came where %d was due", t, seq, next[t]);
|
||||
next[t] = seq + 1;
|
||||
lines++;
|
||||
} else {
|
||||
CHECK(0, "a torn line at byte %zu: %.40s", at, out + at);
|
||||
}
|
||||
at = (size_t)(nlp - out) + 1;
|
||||
}
|
||||
return lines;
|
||||
}
|
||||
|
||||
static size_t run(Prod *pr, int n, int lines, int pad) {
|
||||
PlatThread *th[THREADS];
|
||||
size_t bytes = 0;
|
||||
for (int i = 0; i < n; i++) {
|
||||
pr[i] = (Prod){.id = i, .lines = lines, .pad = pad};
|
||||
th[i] = plat_thread_new(producer, &pr[i], "producer");
|
||||
if (!th[i]) { printf("FATAL: no thread\n"); exit(3); }
|
||||
}
|
||||
for (int i = 0; i < n; i++) plat_thread_join(th[i]);
|
||||
for (int i = 0; i < n; i++)
|
||||
for (int k = 0; k < lines; k++) bytes += (size_t)snprintf(NULL, 0, "t %d %d %.*s\n", i, k, pad, "");
|
||||
return bytes + (size_t)n * (size_t)lines * (size_t)pad;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
setvbuf(stdout, NULL, _IONBF, 0);
|
||||
out = malloc(OUT_CAP);
|
||||
if (!out) return 3;
|
||||
Prod pr[THREADS];
|
||||
int next[THREADS] = {0}, notes = 0;
|
||||
logq_init(sink);
|
||||
|
||||
// 1. Before the queue is on, a line is written by the thread that logs it, before logq_write returns.
|
||||
logq_write("t 0 0 \n", 7);
|
||||
CHECK(out_n == 7 && sink_calls == 1, "a line without the queue: %zu bytes in %d writes", (size_t)out_n, (int)sink_calls);
|
||||
size_t at = 0;
|
||||
CHECK(check_lines(at, next, ¬es) == 1, "the first line");
|
||||
at = out_n;
|
||||
for (int i = 0; i < THREADS; i++) next[i] = 0;
|
||||
|
||||
// 2. With the queue: eight threads, 20,000 lines each; every line arrives whole and each thread's in order.
|
||||
CHECK(logq_async(), "the log thread did not start");
|
||||
size_t want = at + run(pr, THREADS, 20000, 40);
|
||||
settle(want);
|
||||
CHECK(out_n == want, "%zu bytes written, %zu queued", (size_t)out_n - at, want - at);
|
||||
CHECK(check_lines(at, next, ¬es) == THREADS * 20000, "lines lost or doubled");
|
||||
CHECK(sink_calls < THREADS * 20000 / 2, "%d writes for %d lines: the queue did not batch", (int)sink_calls, THREADS * 20000);
|
||||
at = out_n;
|
||||
for (int i = 0; i < THREADS; i++) next[i] = 0;
|
||||
|
||||
// 3. A slow destination (30 ms a write) does not hold the threads that log: 100 lines, a millisecond apart.
|
||||
__atomic_store_n(&sink_ms, 30, __ATOMIC_RELEASE);
|
||||
uint64_t worst = 0;
|
||||
for (int i = 0; i < 100; i++) {
|
||||
char line[64];
|
||||
int n = snprintf(line, sizeof line, "t 0 %d \n", i);
|
||||
uint64_t t0 = plat_now_ns();
|
||||
logq_write(line, (size_t)n);
|
||||
uint64_t d = plat_now_ns() - t0;
|
||||
if (d > worst) worst = d;
|
||||
want += (size_t)n;
|
||||
plat_sleep_ms(1);
|
||||
}
|
||||
CHECK(worst < 15000000, "a line waited %.1f ms for a 30 ms write", worst / 1e6);
|
||||
settle(want);
|
||||
__atomic_store_n(&sink_ms, 0, __ATOMIC_RELEASE);
|
||||
settle(want);
|
||||
plat_sleep_ms(50); // the log thread's line about the slow writes
|
||||
CHECK(check_lines(at, next, ¬es) == 100, "lines lost behind a slow write");
|
||||
CHECK(notes == 1, "%d lines about slow writes within ten seconds, want 1", notes);
|
||||
want = at = out_n;
|
||||
for (int i = 0; i < THREADS; i++) next[i] = 0;
|
||||
|
||||
// 4. A destination that stops while more is logged than the queue holds: the threads wait, nothing is lost.
|
||||
__atomic_store_n(&held, 1, __ATOMIC_RELEASE);
|
||||
logq_write("t 0 0 \n", 7); // the log thread takes it and stops in the sink
|
||||
for (int i = 0; i < 5000 && !__atomic_load_n(&in_sink, __ATOMIC_ACQUIRE); i++) plat_sleep_ms(1);
|
||||
CHECK(in_sink, "the log thread did not reach the sink");
|
||||
PlatThread *th[THREADS];
|
||||
for (int i = 0; i < THREADS; i++) {
|
||||
pr[i] = (Prod){.id = i, .lines = 2000, .pad = 250}; // 8 x 2000 x 260 bytes: four times the queue
|
||||
th[i] = plat_thread_new(producer, &pr[i], "producer");
|
||||
}
|
||||
plat_sleep_ms(200);
|
||||
size_t stuck = out_n;
|
||||
__atomic_store_n(&held, 0, __ATOMIC_RELEASE);
|
||||
for (int i = 0; i < THREADS; i++) plat_thread_join(th[i]);
|
||||
plat_sleep_ms(200);
|
||||
CHECK(stuck == at, "%zu bytes were written while the destination was stopped", stuck - at);
|
||||
at += 7; // past the line that stopped the log thread
|
||||
CHECK(check_lines(at, next, ¬es) == THREADS * 2000, "lines lost at a full queue");
|
||||
at = out_n;
|
||||
for (int i = 0; i < THREADS; i++) next[i] = 0;
|
||||
|
||||
// 5. logq_sync: what is queued is written before it returns, and later lines are written by their own thread.
|
||||
__atomic_store_n(&sink_ms, 20, __ATOMIC_RELEASE);
|
||||
for (int i = 0; i < 50; i++) {
|
||||
char line[64];
|
||||
int n = snprintf(line, sizeof line, "t 1 %d \n", i);
|
||||
logq_write(line, (size_t)n);
|
||||
}
|
||||
logq_sync();
|
||||
__atomic_store_n(&sink_ms, 0, __ATOMIC_RELEASE);
|
||||
CHECK(check_lines(at, next, ¬es) == 50, "logq_sync returned before the queued lines were written");
|
||||
at = out_n;
|
||||
int calls = sink_calls;
|
||||
logq_write("t 2 0 \n", 7);
|
||||
CHECK(out_n == at + 7 && sink_calls == calls + 1, "a line after logq_sync was not written at once");
|
||||
logq_sync(); // a second call does nothing
|
||||
CHECK(check_lines(at, next, ¬es) == 1, "the line after logq_sync");
|
||||
|
||||
printf("logq_check: %s (%zu bytes in %d writes)\n", fails ? "FAILED" : "ok", (size_t)out_n, (int)sink_calls);
|
||||
return fails ? 1 : 0;
|
||||
}
|
||||
52
tests/logq_check.py
Normal file
52
tests/logq_check.py
Normal file
@@ -0,0 +1,52 @@
|
||||
"""Build and run tests/logq_check.c: the queue that hands log lines to the log thread (runtime/logq.c), with the
|
||||
platform layer linked and a destination of the test's own. No game files: the module layout the runtime includes is a
|
||||
stand-in written to the test's build folder.
|
||||
|
||||
Usage: logq_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/logq_check.py).
|
||||
#pragma once
|
||||
#define MOD_MAIN_BASE 0x7000000000ull
|
||||
#define MOD_MAIN_SIZE 0x4ea0000ull
|
||||
'''
|
||||
|
||||
|
||||
def main():
|
||||
work = ROOT / 'build' / 'test' / 'logq'
|
||||
work.mkdir(parents=True, exist_ok=True)
|
||||
(work / 'layout.h').write_text(LAYOUT)
|
||||
win = os.name == 'nt'
|
||||
exe = work / ('logq_check.exe' if win else 'logq_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' / 'logq_check.c'), *map(str, plat), '-o', str(exe), '-lm', *libs],
|
||||
env=env)
|
||||
return subprocess.run([str(exe)], env=env, timeout=300).returncode
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main())
|
||||
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