Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/physics-mv/blob/main/tests/test_m16_gpu_wait.c # physics-mv FORGE: request-bound machine realization, providers and verification for AienOS. Host, simulated and GB10 hardware paths have separate evidence. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `physics-mv / tests / test_m16_gpu_wait.c` 317 lines ยท 15135 bytes Raw Blame History 1 /* Host test for src/m16_gpu_wait.c: fake memory, fake clock, no GPU. 2 * Every scenario runs against the real primitive (must behave) and against 3 * mutants (a naive wall-clock loop, a fixed wait that accepts overshoot, a 4 * sequence wait with an unsigned compare). Each mutant must be killed by the 5 * scenarios named for it, otherwise this test fails. */ 6 #include "m16_gpu_wait.h" 7 8 #include 9 #include 10 11 typedef struct { 12 uint64_t t_ns; 13 uint64_t step_ns; /* clock advance per relax */ 14 uint64_t relaxes; 15 uint64_t barriers; 16 uint64_t at[32]; 17 volatile uint32_t *target[32]; 18 uint32_t value[32]; 19 int nsched; 20 uint32_t invalid_value; /* valid() rejects this value when nonzero */ 21 int abort_at; /* abort() true once relaxes >= abort_at when nonzero */ 22 uint64_t bar_at; /* the bar_at-th barrier call lands bar_value in bar_target (a GPU write that becomes visible at the fence) */ 23 volatile uint32_t *bar_target; 24 uint32_t bar_value; 25 } Fake; 26 27 static uint64_t fk_now(void *c) { return ((Fake *)c)->t_ns; } 28 static void fk_barrier(void *c) { 29 Fake *f = c; 30 f->barriers++; 31 if (f->bar_at && f->barriers == f->bar_at) *f->bar_target = f->bar_value; 32 } 33 static void fk_relax(void *c) { 34 Fake *f = c; 35 f->relaxes++; 36 f->t_ns += f->step_ns; 37 for (int i = 0; i < f->nsched; i++) 38 if (f->at[i] == f->relaxes) *f->target[i] = f->value[i]; 39 } 40 static bool fk_valid(void *c, uint32_t v) { return !((Fake *)c)->invalid_value || v != ((Fake *)c)->invalid_value; } 41 static bool fk_abort(void *c) { Fake *f = c; return f->abort_at && f->relaxes >= (uint64_t)f->abort_at; } 42 43 typedef bool (*WaitFn)(volatile uint32_t *, uint32_t, m16_gpu_wait_report_t *, const m16_gpu_wait_cfg_t *); 44 45 /* Mutants. mode 0: naive wall clock (single deadline, unsigned <, no progress 46 * tracking, no marker2, no barrier, no report). mode 1: exact for sequence too 47 * is not needed; mode "fixed_ge": signed >= for a fixed wait. mode "seq_unsigned": 48 * unsigned >= for a sequence wait. The last two keep everything else correct. */ 49 static bool naive_wait(volatile uint32_t *word, uint32_t want, m16_gpu_wait_report_t *rpt, 50 const m16_gpu_wait_cfg_t *cfg) { 51 uint64_t start = cfg->now_ns(cfg->hook_ctx); 52 uint64_t lim = 1000000ull * cfg->progress_timeout_ms; 53 if (rpt) memset(rpt, 0, sizeof(*rpt)); 54 while (*word < want) { 55 if (cfg->now_ns(cfg->hook_ctx) - start >= lim) return false; 56 cfg->relax(cfg->hook_ctx); 57 } 58 return true; 59 } 60 static bool mut_loop(int cmp, volatile uint32_t *w, uint32_t want, m16_gpu_wait_report_t *rpt, 61 const m16_gpu_wait_cfg_t *cfg, m16_gpu_wait_kind_t kind) { 62 /* Correct kind-aware wait with one comparison replaced; delegate the rest 63 * to the real primitive by wrapping the watched value would be circular, so 64 * implement the minimal real loop here. */ 65 uint64_t start = cfg->now_ns(cfg->hook_ctx), last = start; 66 uint64_t stall = 1000000ull * cfg->progress_timeout_ms; 67 uint32_t prev = *w; 68 if (rpt) { memset(rpt, 0, sizeof(*rpt)); rpt->wait_kind = kind; rpt->expected = want; } 69 for (;;) { 70 cfg->barrier(cfg->hook_ctx); 71 uint32_t m = *w, m2 = cfg->marker2 ? *cfg->marker2 : 0; 72 bool ok = cmp == 0 ? m == want : cmp == 1 ? (int32_t)(m - want) >= 0 : m >= want; 73 if (rpt) { rpt->last_observed = m; rpt->last_marker2 = m2; } 74 if (ok && (!cfg->marker2 || m2 == cfg->marker2_want)) { 75 cfg->barrier(cfg->hook_ctx); 76 if (rpt) rpt->result = M16_GPU_WAIT_PASS; 77 return true; 78 } 79 uint64_t now = cfg->now_ns(cfg->hook_ctx); 80 if (m != prev) { prev = m; last = now; if (rpt) rpt->progress_count++; } 81 if (now - last >= stall) { if (rpt) rpt->result = M16_GPU_WAIT_STALLED; return false; } 82 cfg->relax(cfg->hook_ctx); 83 } 84 } 85 static bool mut_fixed_ge(volatile uint32_t *w, uint32_t v, m16_gpu_wait_report_t *r, const m16_gpu_wait_cfg_t *c) { 86 return mut_loop(1, w, v, r, c, M16_GPU_WAIT_FIXED); 87 } 88 static bool mut_seq_unsigned(volatile uint32_t *w, uint32_t v, m16_gpu_wait_report_t *r, const m16_gpu_wait_cfg_t *c) { 89 return mut_loop(2, w, v, r, c, M16_GPU_WAIT_SEQUENCE); 90 } 91 92 static int g_fail, g_checks, g_quiet; 93 #define CHECK(c, ...) do { g_checks++; if (!(c)) { g_fail++; if (!g_quiet) { printf(" FAIL: " __VA_ARGS__); printf("\n"); } } } while (0) 94 95 static void setup(Fake *f, m16_gpu_wait_cfg_t *cfg) { 96 memset(f, 0, sizeof(*f)); 97 f->step_ns = 1000000ull; /* 1 ms per poll */ 98 memset(cfg, 0, sizeof(*cfg)); 99 cfg->progress_timeout_ms = 100; 100 cfg->total_timeout_ms = 100000; 101 cfg->now_ns = fk_now; 102 cfg->barrier = fk_barrier; 103 cfg->relax = fk_relax; 104 cfg->valid = fk_valid; 105 cfg->abort = fk_abort; 106 cfg->hook_ctx = f; 107 } 108 static void sched(Fake *f, uint64_t at, volatile uint32_t *t, uint32_t v) { 109 f->at[f->nsched] = at; f->target[f->nsched] = t; f->value[f->nsched] = v; f->nsched++; 110 } 111 112 enum { S_NEVER, S_LATE, S_MARKER2, S_SEQ_WRAP, S_PROGRESS_RESETS, S_BARRIER, S_HARD_DEADLINE, 113 S_FIXED_OVERSHOOT, S_INVALID, S_ABORT, S_CLASSES, S_COUNT }; 114 static const char *names[S_COUNT] = {"never_written", "written_late", "marker2_gate", "seq_wrap32", 115 "progress_resets_idle", "barrier_before_read", "hard_deadline_with_progress", 116 "fixed_overshoot", "invalid_state", "abort", "failure_classes"}; 117 118 /* Returns failed checks. fx and sq are the fixed and sequence implementations. */ 119 static int scenario(const char *name, WaitFn fx, WaitFn sq, int which) { 120 volatile uint32_t w = 0, w2 = 0; 121 Fake f; 122 m16_gpu_wait_cfg_t cfg; 123 m16_gpu_wait_report_t r; 124 int before = g_fail; 125 bool ok; 126 setup(&f, &cfg); 127 128 switch (which) { 129 case S_NEVER: 130 ok = sq(&w, 5, &r, &cfg); 131 CHECK(!ok, "%s: never-written marker must not pass", name); 132 CHECK(r.result == M16_GPU_WAIT_STALLED, "%s: result STALLED (got %d)", name, r.result); 133 CHECK(r.last_observed == 0 && r.expected == 5 && r.wait_kind == M16_GPU_WAIT_SEQUENCE, 134 "%s: report carries kind/expected/last-seen 0", name); 135 CHECK(r.elapsed_ns >= 100000000ull && r.progress_count == 0, "%s: elapsed %llu progress %llu", name, 136 (unsigned long long)r.elapsed_ns, (unsigned long long)r.progress_count); 137 break; 138 case S_LATE: 139 sched(&f, 60, &w, 7); 140 ok = fx(&w, 7, &r, &cfg); 141 CHECK(ok && r.result == M16_GPU_WAIT_PASS && r.last_observed == 7, "%s: late fixed marker seen", name); 142 CHECK(r.progress_count == 1 && r.last_progress_ns > r.start_ns, "%s: progress recorded", name); 143 break; 144 case S_MARKER2: 145 cfg.marker2 = &w2; 146 cfg.marker2_want = 0x46464646u; 147 sched(&f, 10, &w, 3); 148 ok = fx(&w, 3, &r, &cfg); 149 CHECK(!ok, "%s: marker without marker2 must not pass", name); 150 CHECK(r.last_observed == 3 && r.last_marker2 == 0, "%s: report shows marker 3, marker2 0", name); 151 w = 0; w2 = 0; setup(&f, &cfg); 152 cfg.marker2 = &w2; 153 cfg.marker2_want = 0x46464646u; 154 sched(&f, 10, &w, 3); 155 sched(&f, 20, &w2, 0x46464646u); 156 ok = fx(&w, 3, &r, &cfg); 157 CHECK(ok && r.last_marker2 == 0x46464646u, "%s: both markers pass", name); 158 break; 159 case S_SEQ_WRAP: { 160 static const uint32_t tg[4] = {0xfffffffeu, 0xffffffffu, 0u, 1u}; 161 for (int i = 0; i < 4; i++) { 162 w = tg[i] - 1u; /* one step behind, wrapping through 0 */ 163 setup(&f, &cfg); 164 sched(&f, 10, &w, tg[i]); 165 ok = sq(&w, tg[i], &r, &cfg); 166 CHECK(ok, "%s: reaches target %08x from %08x", name, tg[i], tg[i] - 1u); 167 } 168 w = 3; setup(&f, &cfg); 169 ok = sq(&w, 0xffffffffu, &r, &cfg); 170 CHECK(ok, "%s: 3 is past 0xffffffff", name); 171 w = 0; setup(&f, &cfg); 172 ok = sq(&w, 0xfffffffeu, &r, &cfg); 173 CHECK(ok, "%s: 0 is past 0xfffffffe", name); 174 w = 0xfffffff0u; setup(&f, &cfg); 175 ok = sq(&w, 0xffffffffu, &r, &cfg); 176 CHECK(!ok, "%s: 0xfffffff0 is before 0xffffffff", name); 177 w = 0xfffffffeu; setup(&f, &cfg); 178 ok = sq(&w, 1, &r, &cfg); 179 CHECK(!ok, "%s: 0xfffffffe is before 1", name); 180 break; 181 } 182 case S_PROGRESS_RESETS: 183 for (int i = 0; i < 4; i++) sched(&f, 60u * (i + 1), &w, (uint32_t)(i + 1)); 184 ok = sq(&w, 4, &r, &cfg); /* 240 ms total, 60 ms gaps, 100 ms stall bound */ 185 CHECK(ok, "%s: steady progress must not stall", name); 186 CHECK(r.progress_count == 4, "%s: progress_count %llu", name, (unsigned long long)r.progress_count); 187 break; 188 case S_BARRIER: 189 /* Order check: the value lands when the 3rd barrier runs. A barrier before 190 * every read means the read right after it sees the value: polls == 3. 191 * A barrier after the read (or none) cannot pass in 3 polls. The extra 192 * barrier after PASS (acquire) makes barriers == polls + 1. */ 193 f.bar_at = 3; f.bar_target = &w; f.bar_value = 1; 194 ok = sq(&w, 1, &r, &cfg); 195 CHECK(ok && r.polls == 3 && f.barriers == 4, "%s: barrier-before-read order: polls %llu barriers %llu (want 3, 4)", name, 196 (unsigned long long)r.polls, (unsigned long long)f.barriers); 197 break; 198 case S_HARD_DEADLINE: 199 cfg.total_timeout_ms = 200; 200 for (int i = 0; i < 30; i++) sched(&f, (uint64_t)(20 * (i + 1)), &w, (uint32_t)(i + 1)); 201 ok = sq(&w, 99, &r, &cfg); /* moves every 20 ms, never arrives */ 202 CHECK(!ok && r.result == M16_GPU_WAIT_TIMEOUT, "%s: hard deadline TIMEOUT (got %d)", name, r.result); 203 CHECK(r.progress_count > 3, "%s: progress was occurring (%llu)", name, (unsigned long long)r.progress_count); 204 break; 205 case S_FIXED_OVERSHOOT: 206 sched(&f, 10, &w, 5); /* jumps past the fixed value 3 */ 207 ok = fx(&w, 3, &r, &cfg); 208 CHECK(!ok, "%s: fixed wait must not accept overshoot 5 for 3", name); 209 CHECK(r.result == M16_GPU_WAIT_STALLED && r.last_observed == 5, "%s: stalled, saw 5", name); 210 w = 0; setup(&f, &cfg); 211 sched(&f, 10, &w, 5); 212 ok = sq(&w, 3, &r, &cfg); 213 CHECK(ok, "%s: sequence wait accepts 5 for target 3", name); 214 break; 215 case S_INVALID: 216 f.invalid_value = 9; 217 sched(&f, 10, &w, 9); 218 ok = sq(&w, 20, &r, &cfg); 219 CHECK(!ok && r.result == M16_GPU_WAIT_INVALID_STATE && r.last_observed == 9, 220 "%s: invalid value reported (got %d)", name, r.result); 221 break; 222 case S_ABORT: 223 f.abort_at = 7; 224 ok = sq(&w, 20, &r, &cfg); 225 CHECK(!ok && r.result == M16_GPU_WAIT_ABORTED, "%s: abort reported (got %d)", name, r.result); 226 break; 227 case S_CLASSES: { 228 const char *cl; 229 ok = fx(&w, 5, &r, &cfg); 230 cl = m16_gpu_wait_failure_class(&r, 0); 231 CHECK(!ok && !strcmp(cl, "no_progress"), "%s: never moved -> no_progress (got %s)", name, cl); 232 setup(&f, &cfg); w = 0; 233 cfg.total_timeout_ms = 200; 234 for (int i = 0; i < 30; i++) sched(&f, (uint64_t)(20 * (i + 1)), &w, (uint32_t)(i + 1)); 235 ok = sq(&w, 99, &r, &cfg); /* counter climbing toward 99 */ 236 cl = m16_gpu_wait_failure_class(&r, 0); 237 CHECK(!ok && r.result == M16_GPU_WAIT_TIMEOUT && !strcmp(cl, "late_progress"), "%s: counter moved, missed hard deadline -> late_progress (got %s)", name, cl); 238 setup(&f, &cfg); w = 0; 239 sched(&f, 10, &w, 5); 240 ok = fx(&w, 3, &r, &cfg); 241 cl = m16_gpu_wait_failure_class(&r, 0); 242 CHECK(!ok && !strcmp(cl, "wrong_marker"), "%s: unexpected value -> wrong_marker (got %s)", name, cl); 243 setup(&f, &cfg); w = 0; w2 = 0; 244 cfg.marker2 = &w2; cfg.marker2_want = 0x46464646u; 245 sched(&f, 10, &w, 3); 246 ok = fx(&w, 3, &r, &cfg); 247 cl = m16_gpu_wait_failure_class(&r, 0x46464646u); 248 CHECK(!ok && !strcmp(cl, "late_progress"), "%s: marker ok, marker2 missing -> late_progress (got %s)", name, cl); 249 setup(&f, &cfg); w = 0; w2 = 0; 250 cfg.marker2 = &w2; cfg.marker2_want = 0x46464646u; 251 sched(&f, 10, &w, 3); 252 sched(&f, 12, &w2, 0x11u); 253 ok = fx(&w, 3, &r, &cfg); 254 cl = m16_gpu_wait_failure_class(&r, 0x46464646u); 255 CHECK(!ok && !strcmp(cl, "wrong_marker"), "%s: marker2 wrong value -> wrong_marker (got %s)", name, cl); 256 setup(&f, &cfg); w = 0; w2 = 0; 257 cfg.total_timeout_ms = 200; cfg.progress_timeout_ms = 200; /* stall bound == hard bound, as in m16_concurrent */ 258 cfg.marker2 = &w2; cfg.marker2_want = 0x46464646u; 259 sched(&f, 150, &w, 3); 260 ok = fx(&w, 3, &r, &cfg); 261 cl = m16_gpu_wait_failure_class(&r, 0x46464646u); 262 CHECK(!ok && r.result == M16_GPU_WAIT_TIMEOUT && !strcmp(cl, "late_progress"), "%s: marker lands at 150 ms of 200, marker2 never -> TIMEOUT late_progress (got %s)", name, cl); 263 setup(&f, &cfg); w = 0; 264 cfg.total_timeout_ms = 200; cfg.progress_timeout_ms = 200; 265 sched(&f, 150, &w, 5); 266 ok = fx(&w, 3, &r, &cfg); 267 cl = m16_gpu_wait_failure_class(&r, 0); 268 CHECK(!ok && r.result == M16_GPU_WAIT_TIMEOUT && !strcmp(cl, "wrong_marker"), "%s: wrong value then hard TIMEOUT still wrong_marker (got %s)", name, cl); 269 break; 270 } 271 } 272 return g_fail - before; 273 } 274 275 typedef struct { 276 const char *label; 277 WaitFn fx, sq; 278 unsigned must_kill; /* bitmask of scenarios that must kill this mutant */ 279 } Mutant; 280 281 int main(void) { 282 for (int s = 0; s < S_COUNT; s++) { 283 int bad = scenario(names[s], m16_gpu_wait_fixed, m16_gpu_wait_sequence, s); 284 printf("real %-28s %s\n", names[s], bad ? "FAIL" : "ok"); 285 } 286 int real_fail = g_fail; 287 288 const Mutant muts[] = { 289 {"naive_wall_clock", naive_wait, naive_wait, (1u << S_COUNT) - 1u}, 290 {"fixed_accepts_overshoot", mut_fixed_ge, m16_gpu_wait_sequence, 1u << S_FIXED_OVERSHOOT}, 291 {"seq_unsigned_compare", m16_gpu_wait_fixed, mut_seq_unsigned, 1u << S_SEQ_WRAP}, 292 }; 293 int saved = g_fail, kill_total = 0, kill_need = 0; 294 g_quiet = 1; 295 for (unsigned m = 0; m < sizeof(muts) / sizeof(muts[0]); m++) { 296 unsigned killed = 0; 297 for (int s = 0; s < S_COUNT; s++) { 298 if (scenario(names[s], muts[m].fx, muts[m].sq, s) > 0) killed |= 1u << s; 299 } 300 g_fail = saved; 301 g_quiet = 0; 302 for (int s = 0; s < S_COUNT; s++) { 303 if (!(muts[m].must_kill & (1u << s))) continue; 304 kill_need++; 305 g_checks++; 306 if (killed & (1u << s)) kill_total++; 307 else { g_fail++; printf(" FAIL: mutant %s survived scenario %s\n", muts[m].label, names[s]); } 308 } 309 printf("mutant %-26s killed mask %03x (required %03x)\n", muts[m].label, killed, muts[m].must_kill); 310 saved = g_fail; 311 g_quiet = 1; 312 } 313 g_quiet = 0; 314 printf("m16_gpu_wait: checks=%d real_failures=%d required_kills=%d/%d\n", g_checks, real_fail, kill_total, kill_need); 315 printf("%s\n", g_fail ? "M16_GPU_WAIT_HOST FAIL" : "M16_GPU_WAIT_HOST PASS"); 316 return g_fail ? 1 : 0; 317 }