diff --git a/.github/workflows/host-suites.yml b/.github/workflows/host-suites.yml index 31ed326c6..f2fb70ccc 100644 --- a/.github/workflows/host-suites.yml +++ b/.github/workflows/host-suites.yml @@ -119,6 +119,8 @@ jobs: run: make PHYSICS_DIR=/nonexistent PHYSICS_LOCK_CHECK=0 test-numeric-qualify - name: E1 closure combiner (host-only, synthetic constituents, every hostile mutant refused) run: make PHYSICS_DIR=/nonexistent PHYSICS_LOCK_CHECK=0 test-e1-combine + - name: GPU completion wait primitive (host-only, fake memory and clock, mutants must die) + run: make PHYSICS_DIR=/nonexistent PHYSICS_LOCK_CHECK=0 test-gpu-wait train: runs-on: ubuntu-24.04-arm diff --git a/Makefile b/Makefile index bc5633353..a629086f6 100644 --- a/Makefile +++ b/Makefile @@ -19,7 +19,7 @@ SRCS = src/sha256.c src/omega_canonical.c src/omega_validate.c src/omega_core.c src/omega_library.c src/omega_discovery.c src/omega_machine.c src/omega_realize_synth.c \ src/omega_matvec.c src/omega_accelerator.c src/omega_accelerator_world.c \ src/omega_vector.c src/omega_blackwell_encoder.c src/omega_blackwell_qmd.c \ - src/omega_blackwell_realize.c src/omega_blackwell_submit.c src/omega_blackwell_engine.c src/omega_gpu_engine.c src/omega_blackwell_gates.c src/omega_blackwell_matmul.c src/omega_blackwell_codegen.c src/omega_gpu_matmul_api.c src/omega_world_gates.c \ + src/omega_blackwell_realize.c src/omega_blackwell_submit.c src/omega_blackwell_engine.c src/omega_gpu_engine.c src/omega_blackwell_gates.c src/omega_blackwell_matmul.c src/omega_blackwell_codegen.c src/omega_gpu_matmul_api.c src/omega_world_gates.c src/omega_gpu_wait.c \ src/omega_evidence.c \ $(PHYSICS_DIR)/m16/m16_native.c $(PHYSICS_DIR)/nvrm/nvrm.c \ tools/omegatool.c diff --git a/mk/omega-gpu-wait.mk b/mk/omega-gpu-wait.mk new file mode 100644 index 000000000..8d5eab94e --- /dev/null +++ b/mk/omega-gpu-wait.mk @@ -0,0 +1,10 @@ +# Shared GPU completion wait primitive (src/omega_gpu_wait.c): host unit test with +# fake memory and a fake clock, plus a naive wall-clock mutant that must be +# caught. Picked up by `-include mk/*.mk`. Host only: opens no device, needs no +# physics headers, takes no chip lock. +.PHONY: test-gpu-wait +build/test_omega_gpu_wait: tests/test_omega_gpu_wait.c src/omega_gpu_wait.c src/omega_gpu_wait.h + @mkdir -p build + gcc -std=gnu11 -O2 -Wall -Wextra -Werror -D_GNU_SOURCE -Isrc -o $@ tests/test_omega_gpu_wait.c src/omega_gpu_wait.c +test-gpu-wait: build/test_omega_gpu_wait + ./build/test_omega_gpu_wait diff --git a/src/omega_gpu_wait.c b/src/omega_gpu_wait.c new file mode 100644 index 000000000..4702d9be6 --- /dev/null +++ b/src/omega_gpu_wait.c @@ -0,0 +1,137 @@ +#include "omega_gpu_wait.h" + +#include +#include + +static uint64_t real_now_ns(void *ctx) { + (void)ctx; + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + return (uint64_t)ts.tv_sec * 1000000000ull + (uint64_t)ts.tv_nsec; +} + +static void real_barrier(void *ctx) { + (void)ctx; +#if defined(__aarch64__) + __asm__ volatile("dsb sy" ::: "memory"); +#else + __atomic_thread_fence(__ATOMIC_SEQ_CST); +#endif +} + +static void real_relax(void *ctx) { + (void)ctx; +#if defined(__aarch64__) + __asm__ volatile("yield"); +#else + __asm__ volatile("" ::: "memory"); +#endif +} + +const char *omega_gpu_wait_result_name(omega_gpu_wait_result_t r) { + switch (r) { + case OMEGA_GPU_WAIT_PASS: return "PASS"; + case OMEGA_GPU_WAIT_TIMEOUT: return "TIMEOUT"; + case OMEGA_GPU_WAIT_STALLED: return "STALLED"; + case OMEGA_GPU_WAIT_INVALID_STATE: return "INVALID_STATE"; + case OMEGA_GPU_WAIT_ABORTED: return "ABORTED"; + default: return "UNKNOWN"; + } +} + +const char *omega_gpu_wait_kind_name(omega_gpu_wait_kind_t k) { + return k == OMEGA_GPU_WAIT_FIXED ? "fixed" : k == OMEGA_GPU_WAIT_SEQUENCE ? "sequence" : "unknown"; +} + +static bool wait_core(omega_gpu_wait_kind_t kind, volatile uint32_t *word, uint32_t expected, + omega_gpu_wait_report_t *rpt, const omega_gpu_wait_cfg_t *cfg) { + omega_gpu_wait_report_t local; + omega_gpu_wait_cfg_t zero; + if (!rpt) rpt = &local; + memset(rpt, 0, sizeof(*rpt)); + rpt->wait_kind = kind; + rpt->expected = expected; + if (!cfg) { + memset(&zero, 0, sizeof(zero)); + cfg = &zero; + } + + uint64_t (*now_ns)(void *) = cfg->now_ns ? cfg->now_ns : real_now_ns; + void (*barrier)(void *) = cfg->barrier ? cfg->barrier : real_barrier; + void (*relax)(void *) = cfg->relax ? cfg->relax : real_relax; + uint64_t total_ns = 1000000ull * (cfg->total_timeout_ms ? cfg->total_timeout_ms : OMEGA_GPU_WAIT_DEFAULT_TOTAL_MS); + uint64_t stall_ns = 1000000ull * (cfg->progress_timeout_ms ? cfg->progress_timeout_ms : OMEGA_GPU_WAIT_DEFAULT_PROGRESS_MS); + + uint64_t start = now_ns(cfg->hook_ctx); + uint64_t hard_deadline = start + total_ns; + uint64_t stall_deadline = start + stall_ns; + rpt->start_ns = start; + rpt->last_progress_ns = start; + uint64_t now = start; + bool first = true; + + /* A null word is a caller bug; report it as an invalid state, not a hang. */ + if (!word) { + rpt->result = OMEGA_GPU_WAIT_INVALID_STATE; + return false; + } + + while (now < hard_deadline) { + uint32_t m, m2 = 0, snap[2] = {0, 0}; + if (cfg->abort && cfg->abort(cfg->hook_ctx)) { + rpt->result = OMEGA_GPU_WAIT_ABORTED; + rpt->elapsed_ns = now - start; + return false; + } + barrier(cfg->hook_ctx); + m = *word; + if (cfg->marker2) m2 = *cfg->marker2; + if (cfg->snapshot) cfg->snapshot(cfg->snapshot_ctx, snap); + rpt->polls++; + now = now_ns(cfg->hook_ctx); + + bool changed = m != rpt->last_observed || m2 != rpt->last_marker2 || + snap[0] != rpt->snap[0] || snap[1] != rpt->snap[1]; + rpt->last_observed = m; + rpt->last_marker2 = m2; + rpt->snap[0] = snap[0]; + rpt->snap[1] = snap[1]; + rpt->elapsed_ns = now - start; + + if (changed && !first) { + rpt->progress_count++; + rpt->last_progress_ns = now; + stall_deadline = now + stall_ns; + } + bool ok = kind == OMEGA_GPU_WAIT_FIXED ? (m == expected) : ((int32_t)(m - expected) >= 0); + if (ok && (!cfg->marker2 || m2 == cfg->marker2_want)) { + barrier(cfg->hook_ctx); /* acquire: later CPU loads are ordered after the marker load */ + rpt->result = OMEGA_GPU_WAIT_PASS; + return true; + } + if (cfg->valid && !cfg->valid(cfg->hook_ctx, m)) { + rpt->result = OMEGA_GPU_WAIT_INVALID_STATE; + return false; + } + first = false; + if (now >= stall_deadline) { + rpt->result = OMEGA_GPU_WAIT_STALLED; + return false; + } + relax(cfg->hook_ctx); + now = now_ns(cfg->hook_ctx); + } + rpt->elapsed_ns = now - start; + rpt->result = OMEGA_GPU_WAIT_TIMEOUT; + return false; +} + +bool omega_gpu_wait_fixed(volatile uint32_t *word, uint32_t expected, + omega_gpu_wait_report_t *rpt, const omega_gpu_wait_cfg_t *cfg) { + return wait_core(OMEGA_GPU_WAIT_FIXED, word, expected, rpt, cfg); +} + +bool omega_gpu_wait_sequence(volatile uint32_t *word, uint32_t target, + omega_gpu_wait_report_t *rpt, const omega_gpu_wait_cfg_t *cfg) { + return wait_core(OMEGA_GPU_WAIT_SEQUENCE, word, target, rpt, cfg); +} diff --git a/src/omega_gpu_wait.h b/src/omega_gpu_wait.h new file mode 100644 index 000000000..466881766 --- /dev/null +++ b/src/omega_gpu_wait.h @@ -0,0 +1,99 @@ +#ifndef OMEGA_GPU_WAIT_H +#define OMEGA_GPU_WAIT_H + +/* Shared completion wait for words the GPU writes to host-visible memory. + * + * Pure C, no NVRM dependency, so the host unit test can drive it with fake + * memory and a fake clock. + * + * Two wait kinds, never one ambiguous wait: + * omega_gpu_wait_fixed the word must equal `expected` exactly. A value + * that overshoots is NOT success. + * omega_gpu_wait_sequence the word is a free-running u32 counter (for + * example dispatch ids). Success when it has reached + * or passed `target` in serial-number order, i.e. + * (int32_t)(observed - target) >= 0, so it is + * correct across the 0xffffffff -> 0 wrap. Valid + * while the counter is less than 2^31 ahead. + * + * Two bounds, both on CLOCK_MONOTONIC in nanoseconds: + * total_timeout_ms hard deadline from the start; never extended. A device + * that advances occasionally still hits it (TIMEOUT). + * progress_timeout_ms stall deadline; restarts only when a watched word + * (word, marker2, snapshot words) changes (STALLED). + * + * Memory visibility: a barrier (dsb sy on AArch64, a full fence elsewhere) runs + * before every read of the watched words and once more, after the success + * observation, before returning PASS. After PASS, the CPU loads issued by the + * caller are ordered after the marker load. That is ordering only. It does not + * make a cached CPU mapping coherent with GPU writes and it does not flush the + * GPU L2: the marker page must still be GPU-uncached and the producing work must + * flush (the omega_numeric_*_gb10.c pattern). The timer does not fix visibility. + * + * If cfg->marker2 is set, PASS also requires *marker2 == marker2_want (read + * output only after both). */ + +#include +#include + +typedef enum { + OMEGA_GPU_WAIT_FIXED = 1, + OMEGA_GPU_WAIT_SEQUENCE = 2 +} omega_gpu_wait_kind_t; + +typedef enum { + OMEGA_GPU_WAIT_PASS = 0, + OMEGA_GPU_WAIT_TIMEOUT = 1, /* hard total deadline */ + OMEGA_GPU_WAIT_STALLED = 2, /* no progress for progress_timeout_ms */ + OMEGA_GPU_WAIT_INVALID_STATE = 3, /* cfg->valid rejected an observed value */ + OMEGA_GPU_WAIT_ABORTED = 4 /* cfg->abort asked to stop */ +} omega_gpu_wait_result_t; + +typedef struct { + omega_gpu_wait_kind_t wait_kind; + omega_gpu_wait_result_t result; + uint32_t expected; /* fixed value or sequence target */ + uint32_t last_observed; /* last value read from the watched word */ + uint32_t last_marker2; /* last value read from marker2 (0 when unused) */ + uint32_t snap[2]; /* last values from the snapshot callback */ + uint64_t start_ns; + uint64_t elapsed_ns; + uint64_t last_progress_ns; /* time of the last observed change (start if none) */ + uint64_t progress_count; /* observed changes after the first read */ + uint64_t polls; +} omega_gpu_wait_report_t; + +typedef struct { + uint32_t total_timeout_ms; /* 0 means the default, 600000 */ + uint32_t progress_timeout_ms; /* 0 means the default, 5000 */ + volatile uint32_t *marker2; + uint32_t marker2_want; + /* Optional: fill two extra words that count as progress when they change + * (for example GP_PUT and a semaphore). Called once per poll. */ + void (*snapshot)(void *ctx, uint32_t out[2]); + void *snapshot_ctx; + /* Optional: return false to declare the observed word out of order. */ + bool (*valid)(void *ctx, uint32_t observed); + /* Optional: return true to stop the wait (ABORTED). */ + bool (*abort)(void *ctx); + /* Test hooks. NULL means the real ones (CLOCK_MONOTONIC, dsb, yield). + * hook_ctx is passed to now_ns, barrier, relax, valid and abort. */ + uint64_t (*now_ns)(void *ctx); + void (*barrier)(void *ctx); + void (*relax)(void *ctx); + void *hook_ctx; +} omega_gpu_wait_cfg_t; + +#define OMEGA_GPU_WAIT_DEFAULT_TOTAL_MS 600000u +#define OMEGA_GPU_WAIT_DEFAULT_PROGRESS_MS 5000u + +/* Both return true on PASS. rpt and cfg may be NULL (cfg NULL means defaults). */ +bool omega_gpu_wait_fixed(volatile uint32_t *word, uint32_t expected, + omega_gpu_wait_report_t *rpt, const omega_gpu_wait_cfg_t *cfg); +bool omega_gpu_wait_sequence(volatile uint32_t *word, uint32_t target, + omega_gpu_wait_report_t *rpt, const omega_gpu_wait_cfg_t *cfg); + +const char *omega_gpu_wait_result_name(omega_gpu_wait_result_t r); +const char *omega_gpu_wait_kind_name(omega_gpu_wait_kind_t k); + +#endif diff --git a/src/omega_world_gates.c b/src/omega_world_gates.c index c5ab42846..622b9e3fb 100644 --- a/src/omega_world_gates.c +++ b/src/omega_world_gates.c @@ -10,6 +10,7 @@ #include "omega_vector.h" #include "sha256.h" #include "omega_evidence.h" +#include "omega_gpu_wait.h" #include #include #include @@ -514,16 +515,32 @@ static bool test_m19_gate6_mixed_workload(void) { return true; } +/* Every QUEUE_WRAP failure exit prints one named reason (init, ring_discovery, + * enqueue, completion_timeout, wrap_count, channel_state, output_parity) so a + * failed run says which exit fired. Pass/fail decisions are unchanged. */ +static void m19_wrap_exit(const char *reason, const char *detail) { + printf("QUEUE_WRAP_FAIL reason=%s %s\n", reason, detail); +} + /* Gate 7: OMEGA_ACCEL_RESIDENT_QUEUE_WRAP_PASS */ static bool test_m19_gate7_queue_wrap(void) { OmegaAcceleratorWorld world; - if (omega_world_init(&world) != OMEGA_WORLD_OK) return false; + char detail[256]; + if (omega_world_init(&world) != OMEGA_WORLD_OK) { + m19_wrap_exit("init", "omega_world_init failed"); + return false; + } uint32_t ring_cap = 0; - if (omega_world_discover_ring_capacity(&world, &ring_cap) != OMEGA_WORLD_OK || ring_cap == 0) { + int ring_rc = omega_world_discover_ring_capacity(&world, &ring_cap); + if (ring_rc != OMEGA_WORLD_OK || ring_cap == 0) { + snprintf(detail, sizeof(detail), "rc=%d ring_capacity=%u", ring_rc, ring_cap); + m19_wrap_exit("ring_discovery", detail); omega_world_destroy(&world); return false; } + /* Observed facts: a failure after discovery still reports the capacity. */ + m19_ring_capacity = ring_cap; uint32_t stress_count = 3 * ring_cap; /* 3072 dispatches */ @@ -671,7 +688,12 @@ static bool test_m19_gate7_queue_wrap(void) { uint32_t off = (submitted % pb_slots) * 0x1000; memcpy((uint8_t *)world.m16.pb_mem.cpu + off, pb, pb_len * 4); __asm__ volatile("dsb sy" ::: "memory"); - if (nvrm_enqueue(&world.m16.rm, &world.m16.pb_mem, off, (uint32_t)pb_len) != 0) { + int enq_rc = nvrm_enqueue(&world.m16.rm, &world.m16.pb_mem, off, (uint32_t)pb_len); + if (enq_rc != 0) { + snprintf(detail, sizeof(detail), + "dispatch_id=%u submitted=%u rc=%d rm.put=%u ring_capacity=%u", + dispatch_id, submitted, enq_rc, (unsigned)world.m16.rm.put, ring_cap); + m19_wrap_exit("enqueue", detail); omega_world_destroy(&world); return false; } @@ -680,29 +702,54 @@ static bool test_m19_gate7_queue_wrap(void) { *world.m16.rm.doorbell = world.m16.rm.token; __asm__ volatile("dsb sy" ::: "memory"); - struct timespec start_ts, cur_ts; - clock_gettime(CLOCK_MONOTONIC, &start_ts); - while (*world.completion.cpu_marker < dispatch_id) { - __asm__ volatile("yield"); - clock_gettime(CLOCK_MONOTONIC, &cur_ts); - if ((cur_ts.tv_sec - start_ts.tv_sec) > 5) { - omega_world_destroy(&world); - return false; - } + /* Same effective budget as the old spin (about 5 s with no progress), + * but the clock is monotonic nanoseconds and any marker change restarts + * it. The hard cap is generous so a slow-but-moving batch is not cut. */ + omega_gpu_wait_cfg_t wait_cfg; + memset(&wait_cfg, 0, sizeof(wait_cfg)); + wait_cfg.progress_timeout_ms = 5000; + wait_cfg.total_timeout_ms = 600000; + omega_gpu_wait_report_t wrpt; + /* Dispatch ids are a free-running counter: sequence semantics. */ + if (!omega_gpu_wait_sequence(world.completion.cpu_marker, dispatch_id, &wrpt, &wait_cfg)) { + snprintf(detail, sizeof(detail), + "wait_kind=%s result=%s dispatch_id=%u submitted=%u expected=%u observed=%u " + "rm.put=%u ring_capacity=%u channel_generation=%u channel_reconstructions=%u " + "elapsed_ns=%llu last_progress_ns=%llu progress_count=%llu polls=%llu", + omega_gpu_wait_kind_name(wrpt.wait_kind), omega_gpu_wait_result_name(wrpt.result), + dispatch_id, submitted, wrpt.expected, wrpt.last_observed, + (unsigned)world.m16.rm.put, ring_cap, + (unsigned)world.channel_generation, (unsigned)world.channel_reconstructions, + (unsigned long long)wrpt.elapsed_ns, + (unsigned long long)(wrpt.last_progress_ns - wrpt.start_ns), + (unsigned long long)wrpt.progress_count, (unsigned long long)wrpt.polls); + m19_wrap_exit("completion_timeout", detail); + omega_world_destroy(&world); + return false; } /* Only now are this batch's pushbuffer slots safe to reuse. */ nvrm_retire(&world.m16.rm, dispatch_id); + m19_wrap_dispatches = dispatch_id; /* observed so far; stress_count on success */ } if (world.m16.rm.put < stress_count || (world.m16.rm.put / ring_cap) < 3) { + snprintf(detail, sizeof(detail), "rm.put=%u stress_count=%u ring_capacity=%u", + (unsigned)world.m16.rm.put, stress_count, ring_cap); + m19_wrap_exit("wrap_count", detail); omega_world_destroy(&world); return false; } if (world.channel_generation != 1 || world.channel_reconstructions != 0) { + snprintf(detail, sizeof(detail), "channel_generation=%u channel_reconstructions=%u", + (unsigned)world.channel_generation, (unsigned)world.channel_reconstructions); + m19_wrap_exit("channel_state", detail); omega_world_destroy(&world); return false; } if (c_cpu[0] != 10 || c_cpu[n - 1] != (n - 1 + 10)) { + snprintf(detail, sizeof(detail), "c[0]=%u c[n-1]=%u want %u and %u", + c_cpu[0], c_cpu[n - 1], 10u, n - 1 + 10); + m19_wrap_exit("output_parity", detail); omega_world_destroy(&world); return false; } diff --git a/tests/test_omega_gpu_wait.c b/tests/test_omega_gpu_wait.c new file mode 100644 index 000000000..f02930e14 --- /dev/null +++ b/tests/test_omega_gpu_wait.c @@ -0,0 +1,262 @@ +/* Host test for src/omega_gpu_wait.c: fake memory, fake clock, no GPU. + * Every scenario runs against the real primitive (must behave) and against + * mutants (a naive wall-clock loop, a fixed wait that accepts overshoot, a + * sequence wait with an unsigned compare). Each mutant must be killed by the + * scenarios named for it, otherwise this test fails. */ +#include "omega_gpu_wait.h" + +#include +#include + +typedef struct { + uint64_t t_ns; + uint64_t step_ns; /* clock advance per relax */ + uint64_t relaxes; + uint64_t barriers; + uint64_t at[32]; + volatile uint32_t *target[32]; + uint32_t value[32]; + int nsched; + uint32_t invalid_value; /* valid() rejects this value when nonzero */ + int abort_at; /* abort() true once relaxes >= abort_at when nonzero */ +} Fake; + +static uint64_t fk_now(void *c) { return ((Fake *)c)->t_ns; } +static void fk_barrier(void *c) { ((Fake *)c)->barriers++; } +static void fk_relax(void *c) { + Fake *f = c; + f->relaxes++; + f->t_ns += f->step_ns; + for (int i = 0; i < f->nsched; i++) + if (f->at[i] == f->relaxes) *f->target[i] = f->value[i]; +} +static bool fk_valid(void *c, uint32_t v) { return !((Fake *)c)->invalid_value || v != ((Fake *)c)->invalid_value; } +static bool fk_abort(void *c) { Fake *f = c; return f->abort_at && f->relaxes >= (uint64_t)f->abort_at; } + +typedef bool (*WaitFn)(volatile uint32_t *, uint32_t, omega_gpu_wait_report_t *, const omega_gpu_wait_cfg_t *); + +/* Mutants. mode 0: naive wall clock (single deadline, unsigned <, no progress + * tracking, no marker2, no barrier, no report). mode 1: exact for sequence too + * is not needed; mode "fixed_ge": signed >= for a fixed wait. mode "seq_unsigned": + * unsigned >= for a sequence wait. The last two keep everything else correct. */ +static bool naive_wait(volatile uint32_t *word, uint32_t want, omega_gpu_wait_report_t *rpt, + const omega_gpu_wait_cfg_t *cfg) { + uint64_t start = cfg->now_ns(cfg->hook_ctx); + uint64_t lim = 1000000ull * cfg->progress_timeout_ms; + if (rpt) memset(rpt, 0, sizeof(*rpt)); + while (*word < want) { + if (cfg->now_ns(cfg->hook_ctx) - start >= lim) return false; + cfg->relax(cfg->hook_ctx); + } + return true; +} +static bool mut_loop(int cmp, volatile uint32_t *w, uint32_t want, omega_gpu_wait_report_t *rpt, + const omega_gpu_wait_cfg_t *cfg, omega_gpu_wait_kind_t kind) { + /* Correct kind-aware wait with one comparison replaced; delegate the rest + * to the real primitive by wrapping the watched value would be circular, so + * implement the minimal real loop here. */ + uint64_t start = cfg->now_ns(cfg->hook_ctx), last = start; + uint64_t stall = 1000000ull * cfg->progress_timeout_ms; + uint32_t prev = *w; + if (rpt) { memset(rpt, 0, sizeof(*rpt)); rpt->wait_kind = kind; rpt->expected = want; } + for (;;) { + cfg->barrier(cfg->hook_ctx); + uint32_t m = *w, m2 = cfg->marker2 ? *cfg->marker2 : 0; + bool ok = cmp == 0 ? m == want : cmp == 1 ? (int32_t)(m - want) >= 0 : m >= want; + if (rpt) { rpt->last_observed = m; rpt->last_marker2 = m2; } + if (ok && (!cfg->marker2 || m2 == cfg->marker2_want)) { + cfg->barrier(cfg->hook_ctx); + if (rpt) rpt->result = OMEGA_GPU_WAIT_PASS; + return true; + } + uint64_t now = cfg->now_ns(cfg->hook_ctx); + if (m != prev) { prev = m; last = now; if (rpt) rpt->progress_count++; } + if (now - last >= stall) { if (rpt) rpt->result = OMEGA_GPU_WAIT_STALLED; return false; } + cfg->relax(cfg->hook_ctx); + } +} +static bool mut_fixed_ge(volatile uint32_t *w, uint32_t v, omega_gpu_wait_report_t *r, const omega_gpu_wait_cfg_t *c) { + return mut_loop(1, w, v, r, c, OMEGA_GPU_WAIT_FIXED); +} +static bool mut_seq_unsigned(volatile uint32_t *w, uint32_t v, omega_gpu_wait_report_t *r, const omega_gpu_wait_cfg_t *c) { + return mut_loop(2, w, v, r, c, OMEGA_GPU_WAIT_SEQUENCE); +} + +static int g_fail, g_checks, g_quiet; +#define CHECK(c, ...) do { g_checks++; if (!(c)) { g_fail++; if (!g_quiet) { printf(" FAIL: " __VA_ARGS__); printf("\n"); } } } while (0) + +static void setup(Fake *f, omega_gpu_wait_cfg_t *cfg) { + memset(f, 0, sizeof(*f)); + f->step_ns = 1000000ull; /* 1 ms per poll */ + memset(cfg, 0, sizeof(*cfg)); + cfg->progress_timeout_ms = 100; + cfg->total_timeout_ms = 100000; + cfg->now_ns = fk_now; + cfg->barrier = fk_barrier; + cfg->relax = fk_relax; + cfg->valid = fk_valid; + cfg->abort = fk_abort; + cfg->hook_ctx = f; +} +static void sched(Fake *f, uint64_t at, volatile uint32_t *t, uint32_t v) { + f->at[f->nsched] = at; f->target[f->nsched] = t; f->value[f->nsched] = v; f->nsched++; +} + +enum { S_NEVER, S_LATE, S_MARKER2, S_SEQ_WRAP, S_PROGRESS_RESETS, S_BARRIER, S_HARD_DEADLINE, + S_FIXED_OVERSHOOT, S_INVALID, S_ABORT, S_COUNT }; +static const char *names[S_COUNT] = {"never_written", "written_late", "marker2_gate", "seq_wrap32", + "progress_resets_idle", "barrier_each_read", "hard_deadline_with_progress", + "fixed_overshoot", "invalid_state", "abort"}; + +/* Returns failed checks. fx and sq are the fixed and sequence implementations. */ +static int scenario(const char *name, WaitFn fx, WaitFn sq, int which) { + volatile uint32_t w = 0, w2 = 0; + Fake f; + omega_gpu_wait_cfg_t cfg; + omega_gpu_wait_report_t r; + int before = g_fail; + bool ok; + setup(&f, &cfg); + + switch (which) { + case S_NEVER: + ok = sq(&w, 5, &r, &cfg); + CHECK(!ok, "%s: never-written marker must not pass", name); + CHECK(r.result == OMEGA_GPU_WAIT_STALLED, "%s: result STALLED (got %d)", name, r.result); + CHECK(r.last_observed == 0 && r.expected == 5 && r.wait_kind == OMEGA_GPU_WAIT_SEQUENCE, + "%s: report carries kind/expected/last-seen 0", name); + CHECK(r.elapsed_ns >= 100000000ull && r.progress_count == 0, "%s: elapsed %llu progress %llu", name, + (unsigned long long)r.elapsed_ns, (unsigned long long)r.progress_count); + break; + case S_LATE: + sched(&f, 60, &w, 7); + ok = fx(&w, 7, &r, &cfg); + CHECK(ok && r.result == OMEGA_GPU_WAIT_PASS && r.last_observed == 7, "%s: late fixed marker seen", name); + CHECK(r.progress_count == 1 && r.last_progress_ns > r.start_ns, "%s: progress recorded", name); + break; + case S_MARKER2: + cfg.marker2 = &w2; + cfg.marker2_want = 0x46464646u; + sched(&f, 10, &w, 3); + ok = fx(&w, 3, &r, &cfg); + CHECK(!ok, "%s: marker without marker2 must not pass", name); + CHECK(r.last_observed == 3 && r.last_marker2 == 0, "%s: report shows marker 3, marker2 0", name); + w = 0; w2 = 0; setup(&f, &cfg); + cfg.marker2 = &w2; + cfg.marker2_want = 0x46464646u; + sched(&f, 10, &w, 3); + sched(&f, 20, &w2, 0x46464646u); + ok = fx(&w, 3, &r, &cfg); + CHECK(ok && r.last_marker2 == 0x46464646u, "%s: both markers pass", name); + break; + case S_SEQ_WRAP: { + static const uint32_t tg[4] = {0xfffffffeu, 0xffffffffu, 0u, 1u}; + for (int i = 0; i < 4; i++) { + w = tg[i] - 1u; /* one step behind, wrapping through 0 */ + setup(&f, &cfg); + sched(&f, 10, &w, tg[i]); + ok = sq(&w, tg[i], &r, &cfg); + CHECK(ok, "%s: reaches target %08x from %08x", name, tg[i], tg[i] - 1u); + } + w = 3; setup(&f, &cfg); + ok = sq(&w, 0xffffffffu, &r, &cfg); + CHECK(ok, "%s: 3 is past 0xffffffff", name); + w = 0; setup(&f, &cfg); + ok = sq(&w, 0xfffffffeu, &r, &cfg); + CHECK(ok, "%s: 0 is past 0xfffffffe", name); + w = 0xfffffff0u; setup(&f, &cfg); + ok = sq(&w, 0xffffffffu, &r, &cfg); + CHECK(!ok, "%s: 0xfffffff0 is before 0xffffffff", name); + w = 0xfffffffeu; setup(&f, &cfg); + ok = sq(&w, 1, &r, &cfg); + CHECK(!ok, "%s: 0xfffffffe is before 1", name); + break; + } + case S_PROGRESS_RESETS: + for (int i = 0; i < 4; i++) sched(&f, 60u * (i + 1), &w, (uint32_t)(i + 1)); + ok = sq(&w, 4, &r, &cfg); /* 240 ms total, 60 ms gaps, 100 ms stall bound */ + CHECK(ok, "%s: steady progress must not stall", name); + CHECK(r.progress_count == 4, "%s: progress_count %llu", name, (unsigned long long)r.progress_count); + break; + case S_BARRIER: + sched(&f, 5, &w, 1); + ok = sq(&w, 1, &r, &cfg); + CHECK(ok && r.polls > 0 && f.barriers >= r.polls + 1, "%s: barriers %llu polls %llu", name, + (unsigned long long)f.barriers, (unsigned long long)r.polls); + break; + case S_HARD_DEADLINE: + cfg.total_timeout_ms = 200; + for (int i = 0; i < 30; i++) sched(&f, (uint64_t)(20 * (i + 1)), &w, (uint32_t)(i + 1)); + ok = sq(&w, 99, &r, &cfg); /* moves every 20 ms, never arrives */ + CHECK(!ok && r.result == OMEGA_GPU_WAIT_TIMEOUT, "%s: hard deadline TIMEOUT (got %d)", name, r.result); + CHECK(r.progress_count > 3, "%s: progress was occurring (%llu)", name, (unsigned long long)r.progress_count); + break; + case S_FIXED_OVERSHOOT: + sched(&f, 10, &w, 5); /* jumps past the fixed value 3 */ + ok = fx(&w, 3, &r, &cfg); + CHECK(!ok, "%s: fixed wait must not accept overshoot 5 for 3", name); + CHECK(r.result == OMEGA_GPU_WAIT_STALLED && r.last_observed == 5, "%s: stalled, saw 5", name); + w = 0; setup(&f, &cfg); + sched(&f, 10, &w, 5); + ok = sq(&w, 3, &r, &cfg); + CHECK(ok, "%s: sequence wait accepts 5 for target 3", name); + break; + case S_INVALID: + f.invalid_value = 9; + sched(&f, 10, &w, 9); + ok = sq(&w, 20, &r, &cfg); + CHECK(!ok && r.result == OMEGA_GPU_WAIT_INVALID_STATE && r.last_observed == 9, + "%s: invalid value reported (got %d)", name, r.result); + break; + case S_ABORT: + f.abort_at = 7; + ok = sq(&w, 20, &r, &cfg); + CHECK(!ok && r.result == OMEGA_GPU_WAIT_ABORTED, "%s: abort reported (got %d)", name, r.result); + break; + } + return g_fail - before; +} + +typedef struct { + const char *label; + WaitFn fx, sq; + unsigned must_kill; /* bitmask of scenarios that must kill this mutant */ +} Mutant; + +int main(void) { + for (int s = 0; s < S_COUNT; s++) { + int bad = scenario(names[s], omega_gpu_wait_fixed, omega_gpu_wait_sequence, s); + printf("real %-28s %s\n", names[s], bad ? "FAIL" : "ok"); + } + int real_fail = g_fail; + + const Mutant muts[] = { + {"naive_wall_clock", naive_wait, naive_wait, (1u << S_COUNT) - 1u}, + {"fixed_accepts_overshoot", mut_fixed_ge, omega_gpu_wait_sequence, 1u << S_FIXED_OVERSHOOT}, + {"seq_unsigned_compare", omega_gpu_wait_fixed, mut_seq_unsigned, 1u << S_SEQ_WRAP}, + }; + int saved = g_fail, kill_total = 0, kill_need = 0; + g_quiet = 1; + for (unsigned m = 0; m < sizeof(muts) / sizeof(muts[0]); m++) { + unsigned killed = 0; + for (int s = 0; s < S_COUNT; s++) { + if (scenario(names[s], muts[m].fx, muts[m].sq, s) > 0) killed |= 1u << s; + } + g_fail = saved; + g_quiet = 0; + for (int s = 0; s < S_COUNT; s++) { + if (!(muts[m].must_kill & (1u << s))) continue; + kill_need++; + g_checks++; + if (killed & (1u << s)) kill_total++; + else { g_fail++; printf(" FAIL: mutant %s survived scenario %s\n", muts[m].label, names[s]); } + } + printf("mutant %-26s killed mask %03x (required %03x)\n", muts[m].label, killed, muts[m].must_kill); + saved = g_fail; + g_quiet = 1; + } + g_quiet = 0; + printf("gpu_wait: checks=%d real_failures=%d required_kills=%d/%d\n", g_checks, real_fail, kill_total, kill_need); + printf("%s\n", g_fail ? "OMEGA_GPU_WAIT_HOST FAIL" : "OMEGA_GPU_WAIT_HOST PASS"); + return g_fail ? 1 : 0; +}