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