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
CUDApp / tests / test_omega_gpu_session_platform.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | /* test_omega_gpu_session_platform: the portable GPU session (native v1 P07 cut C, session part; aienos#286) on a |
| 3 | * FAKE OmegaGpuPlatform. Host program; links the FREESTANDING partial-link object (build/gpu_fs/ |
| 4 | * omega_gpu_freestanding.o: session, matmul and attention APIs, serving, wait, codegen, platform contract), so what |
| 5 | * runs here is the code the native build would run. Opens no device, touches no GPU. |
| 6 | * |
| 7 | * The fake device "completes" a launch at submit time by writing the intermediate semaphore DONE and both marker |
| 8 | * payloads, or (hang mode) writes nothing. Its clock advances only when asked (plus 1 us per read), so timeouts are |
| 9 | * exact and instant. Each case runs in its own child process because an uncertain completion latches the process |
| 10 | * for good (that is the rule under test). |
| 11 | * |
| 12 | * Cases: platform install refusals; freestanding build has no default platform; open/launch/close with typed |
| 13 | * buffers, explicit rights and GPU-uncached policy (host-polled marker); polling only (irq never called, mode is |
| 14 | * POLLING); cancel before submit (nothing submitted, not latched); cancel after submit (latched, parked); timeout |
| 15 | * (latched, session buffers parked, later frees park, close/reopen/install refused, nothing released); stale handle |
| 16 | * refusal after close and reopen; close/reopen (new device generation, opens 2); serving reservation refusal (no |
| 17 | * platform allocation); matmul API code buffer is a CODE buffer encoded in place; wait helper refuses without a clock; |
| 18 | * the bounded formatter matches snprintf and never writes past its cap. |
| 19 | */ |
| 20 | #include "omega_gpu_session.h" |
| 21 | #include "omega_gpu_matmul_api.h" |
| 22 | #include "omega_gpu_wait.h" |
| 23 | #include "omega_gpu_fmt.h" |
| 24 | #include "omega_blackwell_submit.h" |
| 25 | #include "omega_blackwell_qmd.h" |
| 26 | #include "omega_gpu_cmdlist.h" |
| 27 | |
| 28 | #include <stdio.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <string.h> |
| 31 | #include <sys/wait.h> |
| 32 | #include <unistd.h> |
| 33 | |
| 34 | static int case_ok; |
| 35 | #define EXPECT(c) do { if (!(c)) { case_ok = 0; printf(" expect failed at line %d: %s\n", __LINE__, #c); } } while (0) |
| 36 | |
| 37 | /* ---- the fake platform ---- */ |
| 38 | #define NB OMEGA_GPU_HANDLE_SLOTS |
| 39 | enum { MODE_COMPLETE = 0, MODE_HANG = 1 }; |
| 40 | static struct { |
| 41 | OmegaGpuHandleTable t; |
| 42 | void *mem[NB]; |
| 43 | OmegaGpuBufferDesc desc[NB]; |
| 44 | uint32_t dev_of[NB]; /* device generation a buffer belongs to */ |
| 45 | OmegaGpuDeviceHandle dev; |
| 46 | OmegaGpuQueueHandle q; |
| 47 | OmegaGpuBufferHandle ring; |
| 48 | OmegaGpuBufferHandle queue_ring[NB]; |
| 49 | uint64_t gpu_sync_off[NB], gpu_sync_len[NB]; |
| 50 | unsigned gpu_sync_tick[NB], require_qmd_after, drop_publication; |
| 51 | unsigned qmd_checks, qmd_refusals, qmd_seen[2], qmd_seen_count; |
| 52 | int mode; |
| 53 | int cancel_on_poll; /* poll_word calls omega_gpu_session_cancel() on its first call */ |
| 54 | uint64_t ns; |
| 55 | uint64_t submitted_ns, marker_delay_ns[2]; |
| 56 | unsigned allocs, frees, free_refusals, submits, polls, retires, syncs, irq_calls, dev_opens, dev_closes; |
| 57 | unsigned locks_held; |
| 58 | void (*before_lock)(void); /* one contender completes before the fake lock is granted */ |
| 59 | unsigned cached_requests; /* buffers asked GPU-cached (must stay 0) */ |
| 60 | unsigned allocs_unlocked; /* allocations without the session lock (must stay 0) */ |
| 61 | int fail_open; |
| 62 | int fail_close; /* device_close closes, then reports a driver teardown fault (C5) */ |
| 63 | } fk; |
| 64 | |
| 65 | static uint64_t f_now_ns(void *c) { (void)c; fk.ns += 1000u; return fk.ns; } |
| 66 | static uint64_t f_now_ms(void *c) { return f_now_ns(c) / 1000000u; } |
| 67 | static int f_wait(void *c, uint64_t ms) { (void)c; fk.ns += ms * 1000000u; return 0; } |
| 68 | static int f_lock(void *c, uint32_t id) { |
| 69 | (void)c; |
| 70 | if (id != OMEGA_GPU_SESSION_LOCK_ID) return OMEGA_GPU_PLAT_REFUSED_ARGS; |
| 71 | void (*before_lock)(void) = fk.before_lock; |
| 72 | fk.before_lock = NULL; |
| 73 | if (before_lock) before_lock(); |
| 74 | fk.locks_held++; |
| 75 | return 0; |
| 76 | } |
| 77 | static int f_unlock(void *c, uint32_t id) { (void)c; if (id != OMEGA_GPU_SESSION_LOCK_ID || !fk.locks_held) return OMEGA_GPU_PLAT_REFUSED_ARGS; fk.locks_held--; return 0; } |
| 78 | static const char *f_diag(void *c) { (void)c; return fk.fail_open ? "fake device: no GB10 here" : ""; } |
| 79 | |
| 80 | static int f_dev_open(void *c, uint32_t ord, OmegaGpuDeviceHandle *out) { |
| 81 | (void)c; |
| 82 | if (fk.fail_open) return OMEGA_GPU_PLAT_REFUSED_DEVICE; |
| 83 | if (ord != 0 || fk.dev.generation) return OMEGA_GPU_PLAT_REFUSED_LIMIT; |
| 84 | int rc = omega_gpu_handles_open(&fk.t, OMEGA_GPU_HANDLE_DEVICE, 0, 1, 0, &out->index, &out->generation); |
| 85 | if (rc == 0) { fk.dev = *out; fk.dev_opens++; } |
| 86 | return rc; |
| 87 | } |
| 88 | static int f_dev_close(void *c, OmegaGpuDeviceHandle d) { |
| 89 | (void)c; |
| 90 | int rc = omega_gpu_handles_check(&fk.t, d.index, d.generation, OMEGA_GPU_HANDLE_DEVICE); |
| 91 | if (rc) return rc; |
| 92 | for (uint32_t i = 0; i < NB; i++) /* every handle of the device goes stale */ |
| 93 | if (fk.t.live[i] && i != d.index) { (void)omega_gpu_handles_close(&fk.t, i, fk.t.generation[i], fk.t.kind[i]); free(fk.mem[i]); fk.mem[i] = NULL; } |
| 94 | (void)omega_gpu_handles_close(&fk.t, d.index, d.generation, OMEGA_GPU_HANDLE_DEVICE); |
| 95 | memset(&fk.dev, 0, sizeof fk.dev); memset(&fk.q, 0, sizeof fk.q); |
| 96 | fk.dev_closes++; |
| 97 | return fk.fail_close ? OMEGA_GPU_PLAT_REFUSED_DEVICE : 0; |
| 98 | } |
| 99 | |
| 100 | static OmegaGpuPlatformCaps g_caps = { |
| 101 | .abi_major = OMEGA_GPU_PLATFORM_ABI_MAJOR, .abi_minor = OMEGA_GPU_PLATFORM_ABI_MINOR, |
| 102 | .services = OMEGA_GPU_SVC_REQUIRED_SESSION, .max_devices = 1, .max_buffers = 1000, .max_queues = 1, .max_inflight = 1, |
| 103 | .max_buffer_bytes = 1ull << 30, .max_total_bytes = 1ull << 32, |
| 104 | }; |
| 105 | |
| 106 | static int f_alloc(void *c, OmegaGpuDeviceHandle d, const OmegaGpuBufferDesc *desc, OmegaGpuBufferHandle *o, |
| 107 | OmegaCpuView *cpu, OmegaGpuVa *g, OmegaGpuBusAddr *b) { |
| 108 | (void)c; |
| 109 | int rc = omega_gpu_handles_check(&fk.t, d.index, d.generation, OMEGA_GPU_HANDLE_DEVICE); |
| 110 | if (!rc) rc = omega_gpu_platform_check_buffer(desc, &g_caps); |
| 111 | if (rc) return rc; |
| 112 | if (desc->gpu_cache != OMEGA_GPU_CACHE_GPU_UNCACHED) fk.cached_requests++; |
| 113 | if (!fk.locks_held) fk.allocs_unlocked++; |
| 114 | rc = omega_gpu_handles_open(&fk.t, desc->kind, desc->bytes, 1000, 1ull << 32, &o->index, &o->generation); |
| 115 | if (rc) return rc; |
| 116 | void *m = calloc(1, desc->bytes); |
| 117 | fk.mem[o->index] = m; fk.desc[o->index] = *desc; fk.dev_of[o->index] = d.generation; |
| 118 | cpu->ptr = m; cpu->len = desc->bytes; g->va = 0x100000000ull + (uint64_t)o->index * 0x1000000ull; b->iova = 0; |
| 119 | fk.allocs++; |
| 120 | return 0; |
| 121 | } |
| 122 | static int f_free(void *c, OmegaGpuBufferHandle h) { |
| 123 | (void)c; |
| 124 | if (h.index >= NB) { fk.free_refusals++; return OMEGA_GPU_PLAT_REFUSED_BAD_HANDLE; } |
| 125 | int rc = omega_gpu_handles_close(&fk.t, h.index, h.generation, fk.t.kind[h.index]); |
| 126 | if (rc) { fk.free_refusals++; return rc; } |
| 127 | free(fk.mem[h.index]); fk.mem[h.index] = NULL; |
| 128 | fk.frees++; |
| 129 | return 0; |
| 130 | } |
| 131 | static int buf_live(OmegaGpuBufferHandle h) { |
| 132 | return h.index < NB && fk.t.live[h.index] && fk.t.generation[h.index] == h.generation; |
| 133 | } |
| 134 | static int f_sync(void *c, OmegaGpuBufferHandle h, uint64_t off, uint64_t len) { |
| 135 | (void)c; |
| 136 | if (!buf_live(h)) return OMEGA_GPU_PLAT_REFUSED_STALE_HANDLE; |
| 137 | if (len == 0 || off > fk.desc[h.index].bytes || len > fk.desc[h.index].bytes - off) return OMEGA_GPU_PLAT_REFUSED_BAD_LENGTH; |
| 138 | fk.syncs++; |
| 139 | return 0; |
| 140 | } |
| 141 | /* Publication is per buffer and directional; a global barrier cannot satisfy |
| 142 | * this independent fake transport's queue-visible QMD range contract. */ |
| 143 | static int f_sync_to_gpu(void *c, OmegaGpuBufferHandle h, uint64_t off, uint64_t len) { |
| 144 | const int rc = f_sync(c, h, off, len); |
| 145 | if (!rc && fk.drop_publication != h.index + 1u) { |
| 146 | fk.gpu_sync_off[h.index] = off; |
| 147 | fk.gpu_sync_len[h.index] = len; |
| 148 | fk.gpu_sync_tick[h.index] = fk.syncs; |
| 149 | } |
| 150 | return rc; |
| 151 | } |
| 152 | static int f_poll(void *c, OmegaGpuBufferHandle h, uint64_t off, uint32_t want, uint64_t ms, uint32_t *seen) { |
| 153 | (void)c; |
| 154 | if (!buf_live(h)) return OMEGA_GPU_PLAT_REFUSED_STALE_HANDLE; |
| 155 | if (!(fk.desc[h.index].rights & OMEGA_GPU_RIGHT_HOST_POLLED)) return OMEGA_GPU_PLAT_REFUSED_BAD_RIGHTS; |
| 156 | fk.polls++; |
| 157 | if (fk.cancel_on_poll) { fk.cancel_on_poll = 0; omega_gpu_session_cancel(); } |
| 158 | volatile uint32_t *w = (volatile uint32_t *)((uint8_t *)fk.mem[h.index] + off); |
| 159 | *seen = *w; |
| 160 | if (fk.marker_delay_ns[0] && fk.submits && off <= 0x10 && (off == 0 || off == 0x10)) { |
| 161 | const uint64_t due = fk.submitted_ns + fk.marker_delay_ns[off == 0x10]; |
| 162 | const uint64_t available = ms * 1000000u; |
| 163 | if (fk.ns < due && due - fk.ns > available) { |
| 164 | fk.ns += available; |
| 165 | return OMEGA_GPU_POLL_TIMED_OUT; |
| 166 | } |
| 167 | if (fk.ns < due) fk.ns = due; |
| 168 | *w = want; *seen = want; |
| 169 | return OMEGA_GPU_PLAT_OK; |
| 170 | } |
| 171 | if ((int32_t)(*w - want) >= 0) return 0; |
| 172 | fk.ns += ms * 1000000u; |
| 173 | return OMEGA_GPU_POLL_TIMED_OUT; |
| 174 | } |
| 175 | static int f_irq(void *c, OmegaGpuDeviceHandle d, uint64_t t) { (void)c; (void)d; (void)t; fk.irq_calls++; return 0; } |
| 176 | static int f_qopen(void *c, OmegaGpuDeviceHandle d, OmegaGpuBufferHandle ring, OmegaGpuQueueHandle *o) { |
| 177 | (void)c; |
| 178 | int rc = omega_gpu_handles_check(&fk.t, d.index, d.generation, OMEGA_GPU_HANDLE_DEVICE); |
| 179 | if (!rc && (!buf_live(ring) || fk.t.kind[ring.index] != OMEGA_GPU_HANDLE_BUF_QUEUE)) rc = OMEGA_GPU_PLAT_REFUSED_BAD_KIND; |
| 180 | if (!rc) rc = omega_gpu_handles_open(&fk.t, OMEGA_GPU_HANDLE_QUEUE, 0, g_caps.max_queues, 0, &o->index, &o->generation); |
| 181 | if (!rc) { fk.q = *o; fk.ring = ring; fk.queue_ring[o->index] = ring; } |
| 182 | return rc; |
| 183 | } |
| 184 | static int f_qclose(void *c, OmegaGpuQueueHandle q) { |
| 185 | (void)c; |
| 186 | int rc = omega_gpu_handles_close(&fk.t, q.index, q.generation, OMEGA_GPU_HANDLE_QUEUE); |
| 187 | if (!rc) { memset(&fk.q, 0, sizeof fk.q); memset(&fk.queue_ring[q.index], 0, sizeof fk.queue_ring[q.index]); } |
| 188 | return rc; |
| 189 | } |
| 190 | /* The fake device: the launch completes at submit (both markers and the semaphore), or never (hang). The marker and |
| 191 | * semaphore addresses are read back from the method words: the QMD page is the buffer whose VA the QMD0 header |
| 192 | * names, the marker page the one whose VA the first release names. */ |
| 193 | static void *va_to_cpu(uint64_t va) { |
| 194 | for (uint32_t i = 0; i < NB; i++) { |
| 195 | if (!fk.mem[i] || !fk.t.live[i]) continue; |
| 196 | uint64_t base = 0x100000000ull + (uint64_t)i * 0x1000000ull; |
| 197 | if (va >= base && va < base + fk.desc[i].bytes) return (uint8_t *)fk.mem[i] + (va - base); |
| 198 | } |
| 199 | return NULL; |
| 200 | } |
| 201 | static int f_submit(void *c, OmegaGpuQueueHandle q, const uint32_t *w, uint32_t n) { |
| 202 | (void)c; |
| 203 | int rc = omega_gpu_handles_check(&fk.t, q.index, q.generation, OMEGA_GPU_HANDLE_QUEUE); |
| 204 | if (rc) return rc; |
| 205 | const OmegaGpuBufferHandle ring = fk.queue_ring[q.index]; |
| 206 | if (!buf_live(ring) || (uint64_t)n * 4u > fk.desc[ring.index].bytes) return OMEGA_GPU_PLAT_REFUSED_BAD_LENGTH; |
| 207 | memcpy(fk.mem[ring.index], w, (size_t)n * 4u); |
| 208 | fk.submits++; |
| 209 | fk.submitted_ns = fk.ns; |
| 210 | if (fk.mode != MODE_COMPLETE) return 0; |
| 211 | for (uint32_t i = 0; i + 5 < n; i++) { |
| 212 | if (w[i] == ((2u << 28) | (5u << 16) | (0u << 13) | (0x005cu >> 2))) { /* release: va lo, va hi, payload */ |
| 213 | uint64_t va = (uint64_t)w[i + 1] | ((uint64_t)w[i + 2] << 32); |
| 214 | uint32_t *m = va_to_cpu(va); |
| 215 | if (m && !fk.marker_delay_ns[0]) *m = w[i + 3]; |
| 216 | } |
| 217 | if (w[i] == ((2u << 28) | (2u << 16) | (1u << 13) | (0x0188u >> 2)) && w[i + 3] == ((2u << 28) | (2u << 16) | (1u << 13) | (0x0180u >> 2)) && |
| 218 | w[i + 4] == 4u) { /* the semaphore init upload: the kernel ran, the semaphore says DONE */ |
| 219 | uint32_t *s = va_to_cpu(((uint64_t)w[i + 1] << 32) | w[i + 2]); |
| 220 | if (s) *s = OMEGA_BW_SEMAPHORE_INTERMEDIATE_DONE; |
| 221 | } |
| 222 | /* INLINE oracle: compare the actual method payload to the mode-final |
| 223 | * owner slot before modeling its release. This never executes a kernel. */ |
| 224 | if (i + 98u < n && |
| 225 | w[i] == ((2u << 28) | (98u << 16) | (1u << 13) | (0x0318u >> 2))) { |
| 226 | uint64_t va = ((uint64_t)(w[i + 1] & 0x1ffu) << 40) | |
| 227 | ((uint64_t)w[i + 2] << 8); |
| 228 | uint32_t *qmd = va_to_cpu(va); |
| 229 | if (!qmd || memcmp(qmd,w+i+3,OMEGA_BW_QMD_BYTES)!=0) |
| 230 | return OMEGA_GPU_PLAT_REFUSED_BAD_LENGTH; |
| 231 | if (omega_blackwell_verify_qmd_invariants(qmd)==0) { |
| 232 | uint32_t *sem=va_to_cpu((uint64_t)qmd[15]|((uint64_t)qmd[16]<<32)); |
| 233 | if (sem) *sem=qmd[17]; |
| 234 | } |
| 235 | i += 98u; |
| 236 | continue; |
| 237 | } |
| 238 | /* Independent host completion model for a recorded standalone PCAS grid. |
| 239 | * Decode the actual emitted QMD and release address; never execute or |
| 240 | * claim qualification of the encoded kernel. */ |
| 241 | if (w[i] == ((2u << 28) | (1u << 16) | (1u << 13) | (0x02b4u >> 2)) && |
| 242 | w[i + 2] == ((2u << 28) | (1u << 16) | (1u << 13) | (0x02c0u >> 2)) && |
| 243 | w[i + 3] == 3u) { |
| 244 | if (fk.require_qmd_after) { |
| 245 | const uint64_t va = (uint64_t)w[i + 1] << 8; |
| 246 | int published = 0; |
| 247 | for (uint32_t j = 0; j < NB; j++) { |
| 248 | const uint64_t base = 0x100000000ull + (uint64_t)j * 0x1000000ull; |
| 249 | if (!fk.mem[j] || !fk.t.live[j] || va < base || va - base >= fk.desc[j].bytes) continue; |
| 250 | const uint64_t off = va - base, len = OMEGA_GPU_CMDLIST_QMD_BYTES; |
| 251 | if (fk.gpu_sync_tick[j] >= fk.require_qmd_after && |
| 252 | off >= fk.gpu_sync_off[j] && len <= fk.gpu_sync_len[j] && |
| 253 | off - fk.gpu_sync_off[j] <= fk.gpu_sync_len[j] - len) { |
| 254 | published = 1; |
| 255 | if (fk.qmd_seen_count < 2u) fk.qmd_seen[fk.qmd_seen_count++] = j; |
| 256 | fk.qmd_checks++; |
| 257 | } |
| 258 | break; |
| 259 | } |
| 260 | if (!published) { fk.qmd_refusals++; return OMEGA_GPU_PLAT_REFUSED_BAD_LENGTH; } |
| 261 | } |
| 262 | uint32_t *qmd = va_to_cpu((uint64_t)w[i + 1] << 8); |
| 263 | if (qmd && omega_blackwell_verify_qmd_invariants(qmd) == 0) { |
| 264 | uint32_t *sem = va_to_cpu((uint64_t)qmd[15] | ((uint64_t)qmd[16] << 32)); |
| 265 | if (sem) *sem = qmd[17]; |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | return 0; |
| 270 | } |
| 271 | static int f_retire(void *c, OmegaGpuQueueHandle q) { (void)c; (void)q; fk.retires++; return 0; } |
| 272 | static void *f_halloc(void *c, size_t b) { (void)c; return malloc(b); } |
| 273 | static void f_hfree(void *c, void *p) { (void)c; free(p); } |
| 274 | |
| 275 | static OmegaGpuPlatform g_plat = { |
| 276 | .ctx = &fk, .time_now_ms = f_now_ms, .wait_ms = f_wait, .lock = f_lock, .unlock = f_unlock, |
| 277 | .dma_alloc = f_alloc, .dma_free = f_free, .sync_cpu_to_gpu = f_sync_to_gpu, .sync_gpu_to_cpu = f_sync, .poll_word = f_poll, |
| 278 | .device_open = f_dev_open, .device_close = f_dev_close, .diag_text = f_diag, |
| 279 | .queue_open = f_qopen, .queue_close = f_qclose, .queue_submit = f_submit, .queue_retire = f_retire, |
| 280 | .time_now_ns = f_now_ns, .host_alloc = f_halloc, .host_free = f_hfree, |
| 281 | }; |
| 282 | |
| 283 | static void fk_reset(void) { memset(&fk, 0, sizeof fk); omega_gpu_handles_init(&fk.t); } |
| 284 | |
| 285 | static int install(void) { return omega_gpu_session_set_platform(&g_plat, &g_caps); } |
| 286 | |
| 287 | static const uint32_t ARGS[4] = {1, 2, 3, 4}; |
| 288 | static OmegaGpuLaunch launch_desc(uint64_t timeout_ms) { |
| 289 | OmegaGpuLaunch L = { .code_va = 0x200000000ull, .gpr_count = 64, .threads_x = 32, .grid_x = 1, .grid_y = 1, |
| 290 | .num_elements = 32, .args = ARGS, .n_args = 4, .timeout_ms = timeout_ms }; |
| 291 | return L; |
| 292 | } |
| 293 | |
| 294 | /* ---- cases ---- */ |
| 295 | |
| 296 | static void c_install_refusals(void) { |
| 297 | fk_reset(); |
| 298 | OmegaGpuPlatform p = g_plat; |
| 299 | OmegaGpuPlatformCaps caps = g_caps; |
| 300 | caps.services &= ~OMEGA_GPU_SVC_CLOCK_NS; p.time_now_ns = NULL; |
| 301 | EXPECT(omega_gpu_session_set_platform(&p, &caps) == OMEGA_GPU_PLAT_REFUSED_MISSING_SERVICE); |
| 302 | p = g_plat; caps = g_caps; |
| 303 | p.irq_wait = f_irq; /* a function for a service it does not claim: refused, never used */ |
| 304 | const int rc_irq = omega_gpu_session_set_platform(&p, &caps); |
| 305 | EXPECT(rc_irq != OMEGA_GPU_PLAT_OK && omega_gpu_session_platform() == NULL); |
| 306 | caps = g_caps; caps.abi_minor = 1; /* a 1.1 platform predates caps.submit_prefix_words: refused */ |
| 307 | EXPECT(omega_gpu_session_set_platform(&g_plat, &caps) == OMEGA_GPU_PLAT_REFUSED_ABI); |
| 308 | EXPECT(omega_gpu_session_platform() == NULL); /* nothing was installed */ |
| 309 | EXPECT(install() == OMEGA_GPU_PLAT_OK); |
| 310 | EXPECT(omega_gpu_session_platform() == &g_plat); |
| 311 | EXPECT(omega_gpu_session_completion_mode() == OMEGA_GPU_COMPLETION_POLLING); |
| 312 | } |
| 313 | |
| 314 | static void c_no_default_platform(void) { |
| 315 | fk_reset(); |
| 316 | /* the freestanding build has no Linux fallback: nothing installed means refused, not emulated */ |
| 317 | omega_gpu_session_lock(); |
| 318 | EXPECT(omega_gpu_session_open() == NULL); |
| 319 | EXPECT(strstr(omega_gpu_session_last_error(), "no GPU platform installed") != NULL); |
| 320 | omega_gpu_session_unlock(); |
| 321 | EXPECT(omega_gpu_host_alloc(16) == NULL); |
| 322 | EXPECT(omega_gpu_session_now_ns() == 0); |
| 323 | } |
| 324 | |
| 325 | static void c_open_launch_close(void) { |
| 326 | fk_reset(); |
| 327 | EXPECT(install() == 0); |
| 328 | omega_gpu_session_lock(); |
| 329 | EXPECT(fk.locks_held == 1); |
| 330 | OmegaGpuSession *s = omega_gpu_session_open(); |
| 331 | EXPECT(s != NULL); |
| 332 | if (!s) { omega_gpu_session_unlock(); return; } |
| 333 | EXPECT(fk.allocs == 4 && fk.dev_opens == 1); |
| 334 | /* typed buffers, explicit rights, all GPU-uncached; the polled marker is HOST_POLLED */ |
| 335 | EXPECT(fk.t.kind[s->pb.handle.index] == OMEGA_GPU_HANDLE_BUF_QUEUE); |
| 336 | EXPECT(fk.desc[s->marker.handle.index].rights & OMEGA_GPU_RIGHT_HOST_POLLED); |
| 337 | EXPECT(!(fk.desc[s->cbank.handle.index].rights & OMEGA_GPU_RIGHT_HOST_POLLED)); |
| 338 | EXPECT(fk.cached_requests == 0 && fk.allocs_unlocked == 0); |
| 339 | EXPECT(s->pb.va != s->cbank.va && s->marker.size >= 0x1000 && s->qmd.size >= 0x10000); |
| 340 | OmegaGpuLaunch L = launch_desc(100); |
| 341 | uint64_t el = 0; uint32_t mk = 0; |
| 342 | EXPECT(omega_gpu_session_launch(&L, &el, &mk) == 0); |
| 343 | EXPECT(mk == OMEGA_BW_MARKER_COMPLETION_PAYLOAD && fk.submits == 1 && fk.retires == 1 && el > 0); |
| 344 | EXPECT(fk.syncs >= 3); /* explicit CPU->GPU and GPU->CPU visibility, never implicit */ |
| 345 | EXPECT(fk.irq_calls == 0); /* polling only */ |
| 346 | EXPECT(!omega_gpu_session_is_blocked()); |
| 347 | /* a CODE buffer: CPU writes, GPU executes, never GPU-written, prefetch tail zeroed */ |
| 348 | OmegaGpuMem code; |
| 349 | EXPECT(omega_gpu_session_alloc_code(100, &code) == 0); |
| 350 | EXPECT(fk.t.kind[code.handle.index] == OMEGA_GPU_HANDLE_BUF_CODE); |
| 351 | EXPECT((fk.desc[code.handle.index].rights & OMEGA_GPU_RIGHT_GPU_EXEC) && !(fk.desc[code.handle.index].rights & OMEGA_GPU_RIGHT_GPU_WRITE)); |
| 352 | omega_gpu_session_free(&code); |
| 353 | EXPECT(code.cpu == NULL && fk.frees == 1); |
| 354 | omega_gpu_session_unlock(); |
| 355 | EXPECT(fk.locks_held == 0); |
| 356 | omega_gpu_session_close(); |
| 357 | EXPECT(fk.dev_closes == 1 && fk.frees == 5 && fk.t.nlive == 0); |
| 358 | EXPECT(!omega_gpu_session_is_open()); |
| 359 | } |
| 360 | |
| 361 | static void c_cancel_before_submit(void) { |
| 362 | fk_reset(); |
| 363 | EXPECT(install() == 0); |
| 364 | omega_gpu_session_lock(); |
| 365 | EXPECT(omega_gpu_session_open() != NULL); |
| 366 | omega_gpu_session_cancel(); |
| 367 | OmegaGpuLaunch L = launch_desc(100); |
| 368 | EXPECT(omega_gpu_session_launch(&L, NULL, NULL) == OMEGA_GPU_SESSION_CANCELLED); |
| 369 | EXPECT(fk.submits == 0); /* nothing went to the device */ |
| 370 | EXPECT(!omega_gpu_session_is_blocked()); /* so nothing is uncertain */ |
| 371 | OmegaGpuParkStats ps; omega_gpu_session_parked(&ps); |
| 372 | EXPECT(ps.buffers == 0); |
| 373 | EXPECT(omega_gpu_session_launch(&L, NULL, NULL) == 0); /* the request was consumed; the session still works */ |
| 374 | omega_gpu_session_unlock(); |
| 375 | omega_gpu_session_close(); |
| 376 | EXPECT(fk.dev_closes == 1); |
| 377 | } |
| 378 | |
| 379 | static void check_latched(unsigned frees_before) { |
| 380 | EXPECT(omega_gpu_session_is_blocked()); |
| 381 | OmegaGpuParkStats ps; omega_gpu_session_parked(&ps); |
| 382 | EXPECT(ps.buffers == 4 && ps.untracked == 0 && ps.bytes >= 0x10000 + 0x1000 + 0x1000 + 0x10000); |
| 383 | /* a buffer the caller frees now is parked too, never released: the device may still be using it */ |
| 384 | OmegaGpuMem m = {0}; |
| 385 | omega_gpu_session_lock(); |
| 386 | EXPECT(omega_gpu_session_alloc(64, &m) != 0); /* no new allocation after a latch */ |
| 387 | omega_gpu_session_unlock(); |
| 388 | EXPECT(fk.frees == frees_before); |
| 389 | omega_gpu_session_close(); |
| 390 | EXPECT(fk.dev_closes == 0 && fk.frees == frees_before); /* close refused: nothing released */ |
| 391 | omega_gpu_session_lock(); |
| 392 | EXPECT(omega_gpu_session_open() == NULL && strstr(omega_gpu_session_last_error(), "latched") != NULL); |
| 393 | OmegaGpuLaunch L = launch_desc(10); |
| 394 | EXPECT(omega_gpu_session_launch(&L, NULL, NULL) == -1); |
| 395 | EXPECT(strstr(omega_gpu_session_last_error(), "process latched") != NULL); |
| 396 | omega_gpu_session_unlock(); |
| 397 | EXPECT(omega_gpu_session_set_platform(&g_plat, &g_caps) == OMEGA_GPU_PLAT_REFUSED_BUSY); |
| 398 | EXPECT(fk.irq_calls == 0); |
| 399 | } |
| 400 | |
| 401 | static void c_timeout_parks(void) { |
| 402 | fk_reset(); |
| 403 | EXPECT(install() == 0); |
| 404 | omega_gpu_session_lock(); |
| 405 | OmegaGpuSession *s = omega_gpu_session_open(); |
| 406 | EXPECT(s != NULL); |
| 407 | OmegaGpuMem user; |
| 408 | EXPECT(omega_gpu_session_alloc(4096, &user) == 0); |
| 409 | fk.mode = MODE_HANG; |
| 410 | OmegaGpuLaunch L = launch_desc(25); |
| 411 | const uint64_t t0 = fk.ns; |
| 412 | EXPECT(omega_gpu_session_launch(&L, NULL, NULL) == -1); |
| 413 | EXPECT(strstr(omega_gpu_session_last_error(), "timed out") != NULL); |
| 414 | EXPECT(fk.polls >= 25 && fk.ns - t0 >= 25ull * 1000000u); /* polled in 1 ms slices for the whole timeout */ |
| 415 | EXPECT(fk.retires == 0); /* never retired: the entry may still be in flight */ |
| 416 | OmegaGpuBufferHandle uh = user.handle; |
| 417 | omega_gpu_session_free(&user); |
| 418 | EXPECT(user.cpu == NULL && omega_gpu_session_is_parked(uh)); |
| 419 | EXPECT(omega_gpu_session_is_parked(s->marker.handle) && omega_gpu_session_is_parked(s->pb.handle)); |
| 420 | omega_gpu_session_unlock(); |
| 421 | OmegaGpuParkStats ps; omega_gpu_session_parked(&ps); |
| 422 | EXPECT(ps.buffers == 5); |
| 423 | EXPECT(fk.frees == 0 && fk.t.nlive == 1 + 1 + 5); /* device, queue, 5 buffers: all still allocated */ |
| 424 | /* the generic latch checks expect the four session buffers; the user buffer made it five */ |
| 425 | EXPECT(omega_gpu_session_is_blocked()); |
| 426 | omega_gpu_session_close(); |
| 427 | EXPECT(fk.dev_closes == 0 && fk.frees == 0); |
| 428 | } |
| 429 | |
| 430 | /* Two releases at 3 ms and 6 ms are individually inside 5 ms, but the |
| 431 | * submission is not. The legacy per-marker oracle incorrectly succeeds. */ |
| 432 | static void c_submission_deadline(void) { |
| 433 | fk_reset(); |
| 434 | EXPECT(install() == 0); |
| 435 | omega_gpu_session_lock(); |
| 436 | EXPECT(omega_gpu_session_open() != NULL); |
| 437 | fk.marker_delay_ns[0] = 3000000u; |
| 438 | fk.marker_delay_ns[1] = 6000000u; |
| 439 | OmegaGpuLaunch L = launch_desc(5); |
| 440 | EXPECT(omega_gpu_session_launch(&L, NULL, NULL) == -1); |
| 441 | EXPECT(omega_gpu_session_is_blocked() && fk.retires == 0); |
| 442 | EXPECT(strstr(omega_gpu_session_last_error(), "timed out") != NULL); |
| 443 | EXPECT(fk.ns - fk.submitted_ns >= 5000000u); |
| 444 | EXPECT(fk.ns - fk.submitted_ns < 6000000u); |
| 445 | omega_gpu_session_unlock(); |
| 446 | EXPECT(omega_gpu_session_recover() == 0 && fk.dev_closes == 1); |
| 447 | fk.marker_delay_ns[0] = fk.marker_delay_ns[1] = 0; |
| 448 | omega_gpu_session_lock(); |
| 449 | EXPECT(omega_gpu_session_open() != NULL); |
| 450 | EXPECT(omega_gpu_session_launch(&L, NULL, NULL) == 0); |
| 451 | EXPECT(fk.dev_opens == 2); |
| 452 | omega_gpu_session_unlock(); |
| 453 | omega_gpu_session_close(); |
| 454 | } |
| 455 | |
| 456 | static void c_deadline_overflow_before_submit(void) { |
| 457 | fk_reset(); |
| 458 | EXPECT(install() == 0); |
| 459 | omega_gpu_session_lock(); |
| 460 | EXPECT(omega_gpu_session_open() != NULL); |
| 461 | OmegaGpuLaunch L = launch_desc(UINT64_MAX); |
| 462 | EXPECT(omega_gpu_session_launch(&L, NULL, NULL) == -1); |
| 463 | EXPECT(fk.submits == 0 && !omega_gpu_session_is_blocked()); |
| 464 | EXPECT(strstr(omega_gpu_session_last_error(), "deadline overflow") != NULL); |
| 465 | omega_gpu_session_unlock(); |
| 466 | omega_gpu_session_close(); |
| 467 | } |
| 468 | |
| 469 | static void c_cancel_after_submit(void) { |
| 470 | fk_reset(); |
| 471 | EXPECT(install() == 0); |
| 472 | omega_gpu_session_lock(); |
| 473 | EXPECT(omega_gpu_session_open() != NULL); |
| 474 | fk.mode = MODE_HANG; |
| 475 | fk.cancel_on_poll = 1; /* the cancel arrives while the host is polling */ |
| 476 | OmegaGpuLaunch L = launch_desc(60000); |
| 477 | EXPECT(omega_gpu_session_launch(&L, NULL, NULL) == OMEGA_GPU_SESSION_CANCELLED); |
| 478 | EXPECT(fk.submits == 1 && fk.polls <= 2); /* seen within one poll slice, not after the 60 s timeout */ |
| 479 | EXPECT(strstr(omega_gpu_session_last_error(), "cancelled after submit") != NULL); |
| 480 | omega_gpu_session_unlock(); |
| 481 | check_latched(0); |
| 482 | } |
| 483 | |
| 484 | static void c_timeout_latch_rules(void) { |
| 485 | fk_reset(); |
| 486 | EXPECT(install() == 0); |
| 487 | omega_gpu_session_lock(); |
| 488 | EXPECT(omega_gpu_session_open() != NULL); |
| 489 | fk.mode = MODE_HANG; |
| 490 | OmegaGpuLaunch L = launch_desc(3); |
| 491 | EXPECT(omega_gpu_session_launch(&L, NULL, NULL) == -1); |
| 492 | omega_gpu_session_unlock(); |
| 493 | check_latched(0); |
| 494 | } |
| 495 | |
| 496 | /* C5: recover lifts the latch in the same process. Refused when nothing is latched; after a timeout it closes the device |
| 497 | * once (the platform releases every buffer, parked ones included; the session itself frees nothing), clears the parked |
| 498 | * list, and the next open is a new device on which a launch completes. */ |
| 499 | static void c_timeout_recover(void) { |
| 500 | fk_reset(); |
| 501 | EXPECT(install() == 0); |
| 502 | EXPECT(omega_gpu_session_recover() == -1 && strstr(omega_gpu_session_last_error(), "not latched") != NULL); |
| 503 | omega_gpu_session_lock(); |
| 504 | OmegaGpuSession *s = omega_gpu_session_open(); |
| 505 | EXPECT(s != NULL); |
| 506 | const OmegaGpuDeviceHandle old_dev = s ? s->dev : (OmegaGpuDeviceHandle){0}; |
| 507 | OmegaGpuLaunch L = launch_desc(5); |
| 508 | EXPECT(omega_gpu_session_launch(&L, NULL, NULL) == 0); /* a working launch first */ |
| 509 | omega_gpu_session_unlock(); |
| 510 | EXPECT(omega_gpu_session_recover() == -1 && fk.dev_closes == 0); /* open but not latched: refused, device kept */ |
| 511 | omega_gpu_session_lock(); |
| 512 | OmegaGpuMem user; |
| 513 | EXPECT(omega_gpu_session_alloc(4096, &user) == 0); |
| 514 | fk.mode = MODE_HANG; |
| 515 | EXPECT(omega_gpu_session_launch(&L, NULL, NULL) == -1 && omega_gpu_session_is_blocked()); |
| 516 | omega_gpu_session_free(&user); /* parked */ |
| 517 | omega_gpu_session_unlock(); |
| 518 | EXPECT(omega_gpu_session_recoveries() == 0); |
| 519 | EXPECT(omega_gpu_session_recover() == 0); |
| 520 | EXPECT(!omega_gpu_session_is_blocked() && omega_gpu_session_recoveries() == 1); |
| 521 | EXPECT(fk.dev_closes == 1 && fk.frees == 0 && fk.t.nlive == 0); /* the platform close released all of it */ |
| 522 | OmegaGpuParkStats ps; omega_gpu_session_parked(&ps); |
| 523 | EXPECT(ps.buffers == 0 && ps.bytes == 0); |
| 524 | EXPECT(omega_gpu_session_recover() == -1); /* nothing latched any more */ |
| 525 | fk.mode = MODE_COMPLETE; |
| 526 | omega_gpu_session_lock(); |
| 527 | s = omega_gpu_session_open(); |
| 528 | EXPECT(s != NULL && s->dev.generation != old_dev.generation && fk.dev_opens == 2); |
| 529 | EXPECT(omega_gpu_session_launch(&L, NULL, NULL) == 0); |
| 530 | omega_gpu_session_unlock(); |
| 531 | omega_gpu_session_close(); |
| 532 | EXPECT(fk.dev_closes == 2 && fk.t.nlive == 0); |
| 533 | } |
| 534 | |
| 535 | /* C5: a close that reports a driver teardown fault leaves the process latched for good (the old grid may still run). */ |
| 536 | static void c_recover_teardown_fault(void) { |
| 537 | fk_reset(); |
| 538 | EXPECT(install() == 0); |
| 539 | omega_gpu_session_lock(); |
| 540 | EXPECT(omega_gpu_session_open() != NULL); |
| 541 | fk.mode = MODE_HANG; |
| 542 | OmegaGpuLaunch L = launch_desc(5); |
| 543 | EXPECT(omega_gpu_session_launch(&L, NULL, NULL) == -1 && omega_gpu_session_is_blocked()); |
| 544 | omega_gpu_session_unlock(); |
| 545 | fk.fail_close = 1; |
| 546 | EXPECT(omega_gpu_session_recover() == -1 && strstr(omega_gpu_session_last_error(), "teardown fault") != NULL); |
| 547 | EXPECT(omega_gpu_session_is_blocked() && omega_gpu_session_recoveries() == 0 && fk.dev_closes == 1); |
| 548 | fk.fail_close = 0; |
| 549 | EXPECT(omega_gpu_session_recover() == -1 && omega_gpu_session_is_blocked()); /* the handle is gone: no second try */ |
| 550 | omega_gpu_session_lock(); |
| 551 | EXPECT(omega_gpu_session_open() == NULL); |
| 552 | omega_gpu_session_unlock(); |
| 553 | } |
| 554 | |
| 555 | /* C5: a module latch (the command lists after a timeout whose check launch completed) is recoverable too */ |
| 556 | static void c_recover_module_latch(void) { |
| 557 | fk_reset(); |
| 558 | EXPECT(install() == 0); |
| 559 | omega_gpu_session_lock(); |
| 560 | EXPECT(omega_gpu_session_open() != NULL); |
| 561 | omega_gpu_session_unlock(); |
| 562 | omega_gpu_session_note_module_latch(); |
| 563 | EXPECT(!omega_gpu_session_is_blocked()); /* other APIs keep working */ |
| 564 | EXPECT(omega_gpu_session_recover() == 0 && fk.dev_closes == 1 && omega_gpu_session_recoveries() == 1); |
| 565 | EXPECT(omega_gpu_session_recover() == -1); /* lifted */ |
| 566 | } |
| 567 | |
| 568 | static void c_stale_after_reopen(void) { |
| 569 | fk_reset(); |
| 570 | EXPECT(install() == 0); |
| 571 | omega_gpu_session_lock(); |
| 572 | OmegaGpuSession *s = omega_gpu_session_open(); |
| 573 | EXPECT(s != NULL); |
| 574 | const OmegaGpuDeviceHandle d1 = s->dev; |
| 575 | OmegaGpuMem old; |
| 576 | EXPECT(omega_gpu_session_alloc(8192, &old) == 0); |
| 577 | omega_gpu_session_unlock(); |
| 578 | omega_gpu_session_close(); /* the caller kept `old` past the close */ |
| 579 | EXPECT(fk.dev_closes == 1); |
| 580 | omega_gpu_session_lock(); |
| 581 | s = omega_gpu_session_open(); |
| 582 | EXPECT(s != NULL && omega_gpu_session_open_count() == 2); |
| 583 | EXPECT(s->dev.index != d1.index || s->dev.generation != d1.generation); /* a new device generation */ |
| 584 | const unsigned frees = fk.frees; |
| 585 | OmegaGpuAllocStats st0, st1; |
| 586 | omega_gpu_session_alloc_stats(&st0); |
| 587 | omega_gpu_session_free(&old); /* a handle of the closed device: refused, nothing released */ |
| 588 | omega_gpu_session_alloc_stats(&st1); |
| 589 | EXPECT(st1.stale_refusals == st0.stale_refusals + 1 && st1.frees == st0.frees); |
| 590 | EXPECT(fk.frees == frees && fk.free_refusals == 1); |
| 591 | EXPECT(strstr(omega_gpu_session_last_error(), "STALE") != NULL || strstr(omega_gpu_session_last_error(), "stale") != NULL); |
| 592 | EXPECT(old.cpu == NULL); |
| 593 | OmegaGpuLaunch L = launch_desc(100); |
| 594 | EXPECT(omega_gpu_session_launch(&L, NULL, NULL) == 0); /* the reopened session works */ |
| 595 | omega_gpu_session_unlock(); |
| 596 | omega_gpu_session_close(); |
| 597 | EXPECT(fk.dev_closes == 2 && fk.t.nlive == 0); |
| 598 | } |
| 599 | |
| 600 | static void c_open_failure_text(void) { |
| 601 | fk_reset(); |
| 602 | EXPECT(install() == 0); |
| 603 | fk.fail_open = 1; |
| 604 | omega_gpu_session_lock(); |
| 605 | EXPECT(omega_gpu_session_open() == NULL); |
| 606 | EXPECT(strstr(omega_gpu_session_last_error(), "fake device: no GB10 here") != NULL); /* the driver text survives */ |
| 607 | EXPECT(!omega_gpu_session_is_blocked() && fk.allocs == 0); |
| 608 | fk.fail_open = 0; |
| 609 | EXPECT(omega_gpu_session_open() != NULL); /* a failed open is not a latch */ |
| 610 | omega_gpu_session_unlock(); |
| 611 | omega_gpu_session_close(); |
| 612 | } |
| 613 | |
| 614 | static void c_reservation_refusal(void) { |
| 615 | fk_reset(); |
| 616 | EXPECT(install() == 0); |
| 617 | omega_gpu_session_lock(); |
| 618 | EXPECT(omega_gpu_session_open() != NULL); |
| 619 | OmegaGpuScratch sc; memset(&sc, 0, sizeof sc); |
| 620 | EXPECT(omega_gpu_session_scratch_reserve(&sc, 10000) == 0); |
| 621 | const unsigned allocs = fk.allocs; |
| 622 | EXPECT(omega_gpu_session_scratch(&sc, 9000) == 0); |
| 623 | EXPECT(omega_gpu_session_scratch(&sc, sc.cap + 1) == OMEGA_GPU_SESSION_SCRATCH_RESERVED); |
| 624 | EXPECT(fk.allocs == allocs); /* refused loudly, nothing allocated */ |
| 625 | EXPECT(strstr(omega_gpu_session_last_error(), "serving reservation exceeded") != NULL); |
| 626 | omega_gpu_session_scratch_free(&sc); |
| 627 | omega_gpu_session_unlock(); |
| 628 | omega_gpu_session_close(); |
| 629 | EXPECT(fk.t.nlive == 0); |
| 630 | } |
| 631 | |
| 632 | static void c_matmul_code_buffer(void) { |
| 633 | fk_reset(); |
| 634 | EXPECT(install() == 0); |
| 635 | uint16_t b[32 * 16]; |
| 636 | memset(b, 0, sizeof b); |
| 637 | OmegaGpuTensor *t = NULL; |
| 638 | EXPECT(omega_gpu_tensor_upload_bf16(32, 16, b, &t) == OMEGA_GPU_MATMUL_OK); |
| 639 | uint16_t a[32]; memset(a, 0, sizeof a); |
| 640 | float c[16]; |
| 641 | OmegaGpuMatmulInfo info; |
| 642 | /* the fake device runs no kernel, so the result sentinels survive: CHIP_FAIL is the expected verdict here */ |
| 643 | int rc = omega_gpu_matmul_resident_bf16(1, a, t, c, &info); |
| 644 | EXPECT(rc == OMEGA_GPU_MATMUL_CHIP_FAIL && fk.submits >= 1); |
| 645 | unsigned code_bufs = 0; |
| 646 | for (uint32_t i = 0; i < NB; i++) |
| 647 | if (fk.t.live[i] && fk.t.kind[i] == OMEGA_GPU_HANDLE_BUF_CODE) { |
| 648 | code_bufs++; |
| 649 | const uint8_t *p = fk.mem[i]; |
| 650 | unsigned nz = 0; |
| 651 | for (uint32_t j = 0; j < 64; j++) nz += p[j] != 0; |
| 652 | EXPECT(nz > 0); /* the kernel was encoded in place into the device code buffer */ |
| 653 | EXPECT((fk.desc[i].rights & OMEGA_GPU_RIGHT_GPU_EXEC) && !(fk.desc[i].rights & OMEGA_GPU_RIGHT_GPU_WRITE)); |
| 654 | } |
| 655 | EXPECT(code_bufs == 1); |
| 656 | EXPECT(fk.cached_requests == 0 && fk.allocs_unlocked == 0 && fk.irq_calls == 0); |
| 657 | omega_gpu_tensor_free(t); |
| 658 | omega_gpu_session_close(); |
| 659 | EXPECT(fk.t.nlive == 0); |
| 660 | } |
| 661 | |
| 662 | static void c_wait_needs_clock(void) { |
| 663 | volatile uint32_t word = 0; |
| 664 | omega_gpu_wait_report_t r; |
| 665 | EXPECT(!omega_gpu_wait_fixed(&word, 1, &r, NULL)); |
| 666 | EXPECT(r.result == OMEGA_GPU_WAIT_INVALID_STATE); /* no clock in the freestanding build: refused, not unbounded */ |
| 667 | } |
| 668 | |
| 669 | static void c_fmt_matches_snprintf(void) { |
| 670 | char a[96], b[96]; |
| 671 | const long long big = -9223372036854775807LL - 1; |
| 672 | omega_gpu_fmt(a, sizeof a, "%s:%c:%d:%u:%x:%ld:%lu:%lld:%llu:%zu:%%", "stage", 'x', -42, 4000000000u, 0xbeefu, |
| 673 | -7L, 9UL, big, 18446744073709551615ULL, (size_t)12345); |
| 674 | snprintf(b, sizeof b, "%s:%c:%d:%u:%x:%ld:%lu:%lld:%llu:%zu:%%", "stage", 'x', -42, 4000000000u, 0xbeefu, |
| 675 | -7L, 9UL, big, 18446744073709551615ULL, (size_t)12345); |
| 676 | EXPECT(strcmp(a, b) == 0); |
| 677 | char s[8]; |
| 678 | memset(s, 'Z', sizeof s); |
| 679 | omega_gpu_fmt(s, 6, "%s", "truncate me"); |
| 680 | EXPECT(strcmp(s, "trunc") == 0 && s[6] == 'Z' && s[7] == 'Z'); /* bounded: NUL inside cap, nothing past it */ |
| 681 | omega_gpu_fmt(s, 1, "%d", 123); |
| 682 | EXPECT(s[0] == '\0'); |
| 683 | } |
| 684 | |
| 685 | #ifdef OMEGA_NATIVE_RUNTIME_TEST |
| 686 | #include "native_runtime_controls.h" |
| 687 | |
| 688 | static void recover_before_cmdlist_lock(void) { |
| 689 | omega_gpu_session_note_module_latch(); |
| 690 | EXPECT(omega_gpu_session_recover() == 0); |
| 691 | } |
| 692 | |
| 693 | static void cmdlist_create_lifetime(void (*before_lock)(void)) { |
| 694 | fk_reset(); |
| 695 | EXPECT(install() == 0); |
| 696 | omega_gpu_session_lock(); |
| 697 | EXPECT(omega_gpu_session_open() != NULL); |
| 698 | const uint64_t previous_epoch = omega_gpu_session_epoch(); |
| 699 | omega_gpu_session_unlock(); |
| 700 | |
| 701 | /* Model a close/recovery winning the lock immediately before create. |
| 702 | * The hook runs real session teardown, not a fabricated epoch value. */ |
| 703 | fk.before_lock = before_lock; |
| 704 | OmegaGpuCmdList *cl = NULL; |
| 705 | EXPECT(omega_gpu_cmdlist_create(1, &cl) == 0); |
| 706 | if (!cl) return; |
| 707 | EXPECT(fk.before_lock == NULL); |
| 708 | EXPECT(fk.dev_closes == 1 && fk.dev_opens == 2); |
| 709 | EXPECT(omega_gpu_session_epoch() != previous_epoch); |
| 710 | omega_gpu_session_lock(); |
| 711 | OmegaGpuCmdListOwner owner; |
| 712 | EXPECT(omega_gpu_cmdlist_owner(cl, &owner) == 0); |
| 713 | OmegaGpuLaunch launch = launch_desc(100); |
| 714 | EXPECT(omega_gpu_cmdlist_add(cl, &launch) == 0); |
| 715 | omega_gpu_session_unlock(); |
| 716 | omega_gpu_cmdlist_free(cl); |
| 717 | omega_gpu_session_close(); |
| 718 | } |
| 719 | |
| 720 | static void c_cmdlist_create_after_close(void) { |
| 721 | cmdlist_create_lifetime(omega_gpu_session_close); |
| 722 | } |
| 723 | |
| 724 | static void c_cmdlist_create_after_recovery(void) { |
| 725 | cmdlist_create_lifetime(recover_before_cmdlist_lock); |
| 726 | } |
| 727 | |
| 728 | static void c_cmdlist_close_after_create_stale(void) { |
| 729 | fk_reset(); |
| 730 | EXPECT(install() == 0); |
| 731 | OmegaGpuCmdList *cl = NULL; |
| 732 | EXPECT(omega_gpu_cmdlist_create(1, &cl) == 0); |
| 733 | if (!cl) return; |
| 734 | omega_gpu_session_close(); |
| 735 | omega_gpu_session_lock(); |
| 736 | OmegaGpuCmdListOwner owner; |
| 737 | EXPECT(omega_gpu_cmdlist_owner(cl, &owner) != 0); |
| 738 | OmegaGpuLaunch launch = launch_desc(100); |
| 739 | EXPECT(omega_gpu_cmdlist_add(cl, &launch) != 0); |
| 740 | omega_gpu_session_unlock(); |
| 741 | omega_gpu_cmdlist_free(cl); |
| 742 | } |
| 743 | |
| 744 | static void native_pair_visibility(unsigned missing) { |
| 745 | fk_reset(); |
| 746 | g_caps.max_queues = 2; |
| 747 | EXPECT(install() == 0); |
| 748 | OmegaGpuCmdList *a = NULL, *b = NULL; |
| 749 | EXPECT(omega_gpu_cmdlist_create(1, &a) == 0); |
| 750 | EXPECT(omega_gpu_cmdlist_create(1, &b) == 0); |
| 751 | if (!a || !b) return; |
| 752 | omega_gpu_session_lock(); |
| 753 | OmegaGpuLaunch launch = launch_desc(100); |
| 754 | EXPECT(omega_gpu_cmdlist_add(a, &launch) == 0); |
| 755 | EXPECT(omega_gpu_cmdlist_add(b, &launch) == 0); |
| 756 | OmegaGpuCmdListOpts opts = { .mode = OMEGA_GPU_CMDLIST_PCAS, .timeout_ms = 100 }; |
| 757 | OmegaGpuCmdListStats sa, sb; |
| 758 | fk.require_qmd_after = fk.syncs + 1u; |
| 759 | EXPECT(omega_gpu_cmdlist_submit_pair(a, b, &opts, &sa, &sb) == 0); |
| 760 | EXPECT(fk.qmd_checks == 2u && fk.qmd_seen_count == 2u && fk.qmd_refusals == 0u); |
| 761 | EXPECT(fk.submits == 2u && sa.sems_done == 1u && sb.sems_done == 1u); |
| 762 | if (missing && fk.qmd_seen_count == 2u) { |
| 763 | fk.drop_publication = fk.qmd_seen[missing - 1u] + 1u; |
| 764 | fk.require_qmd_after = fk.syncs + 1u; |
| 765 | EXPECT(omega_gpu_cmdlist_submit_pair(a, b, &opts, &sa, &sb) != 0); |
| 766 | EXPECT(fk.qmd_refusals == 1u); |
| 767 | EXPECT(strstr(omega_gpu_session_last_error(), "queue_submit") != NULL); |
| 768 | } |
| 769 | omega_gpu_session_unlock(); |
| 770 | omega_gpu_cmdlist_free(a); |
| 771 | omega_gpu_cmdlist_free(b); |
| 772 | omega_gpu_session_close(); |
| 773 | } |
| 774 | static void c_native_pair_visible(void) { native_pair_visibility(0); } |
| 775 | static void c_native_pair_missing_a(void) { native_pair_visibility(1); } |
| 776 | static void c_native_pair_missing_b(void) { native_pair_visibility(2); } |
| 777 | #endif |
| 778 | |
| 779 | typedef struct { const char *id; void (*fn)(void); } Case; |
| 780 | static const Case CASES[] = { |
| 781 | #ifdef OMEGA_NATIVE_RUNTIME_TEST |
| 782 | {"CMDLIST_CREATE_AFTER_LOCK_PRECEDING_CLOSE_IS_USABLE", c_cmdlist_create_after_close}, |
| 783 | {"CMDLIST_CREATE_AFTER_LOCK_PRECEDING_RECOVERY_IS_USABLE", c_cmdlist_create_after_recovery}, |
| 784 | {"CMDLIST_CLOSE_AFTER_CREATE_STILL_REFUSES_STALE_LIST", c_cmdlist_close_after_create_stale}, |
| 785 | {"NATIVE_SYNTHETIC_QUANT_COMPOSE_ENCODE_CACHE_SELECT_RECORD", c_native_chain}, |
| 786 | {"NATIVE_REAL_COMPOSE_CAPACITY_REFUSES_WITHOUT_TRAP", c_native_compose_capacity}, |
| 787 | {"NATIVE_TIMEOUT_RETAINS_CODE_UNTIL_CONFIRMED_TEARDOWN", c_native_timeout_refs}, |
| 788 | {"NATIVE_NORMAL_CLOSE_INVALIDATES_EXACT_LEASE", c_native_close_epoch}, |
| 789 | {"NATIVE_ACTUAL_OWNER_MODEFINAL_OBSERVER_BINDING", c_native_observer_modes}, |
| 790 | {"NATIVE_PAIR_REQUIRES_BOTH_QMD_PUBLICATIONS", c_native_pair_visible}, |
| 791 | {"NATIVE_PAIR_REFUSES_MISSING_FIRST_QMD_PUBLICATION", c_native_pair_missing_a}, |
| 792 | {"NATIVE_PAIR_REFUSES_MISSING_SECOND_QMD_PUBLICATION", c_native_pair_missing_b}, |
| 793 | #endif |
| 794 | {"INSTALL_REFUSALS_ABI_1_1", c_install_refusals}, |
| 795 | {"FREESTANDING_NO_DEFAULT_PLATFORM", c_no_default_platform}, |
| 796 | {"OPEN_LAUNCH_CLOSE_TYPED_UNCACHED_POLLING", c_open_launch_close}, |
| 797 | {"OPEN_FAILURE_KEEPS_DRIVER_TEXT", c_open_failure_text}, |
| 798 | {"CANCEL_BEFORE_SUBMIT_NOT_LATCHED", c_cancel_before_submit}, |
| 799 | {"CANCEL_AFTER_SUBMIT_LATCHES_AND_PARKS", c_cancel_after_submit}, |
| 800 | {"TIMEOUT_PARKS_NEVER_RELEASES", c_timeout_parks}, |
| 801 | {"TIMEOUT_LATCH_REFUSES_CLOSE_REOPEN_INSTALL", c_timeout_latch_rules}, |
| 802 | {"TIMEOUT_RECOVER_SAME_PROCESS", c_timeout_recover}, |
| 803 | {"SUBMISSION_DEADLINE_SHARED_BY_BOTH_MARKERS", c_submission_deadline}, |
| 804 | {"DEADLINE_OVERFLOW_REFUSED_BEFORE_SUBMIT", c_deadline_overflow_before_submit}, |
| 805 | {"RECOVER_TEARDOWN_FAULT_STAYS_LATCHED", c_recover_teardown_fault}, |
| 806 | {"RECOVER_MODULE_LATCH", c_recover_module_latch}, |
| 807 | {"STALE_HANDLE_REFUSED_AFTER_CLOSE_REOPEN", c_stale_after_reopen}, |
| 808 | {"SERVING_RESERVATION_REFUSES_WITHOUT_ALLOC", c_reservation_refusal}, |
| 809 | {"MATMUL_CODE_BUFFER_ENCODED_IN_PLACE", c_matmul_code_buffer}, |
| 810 | {"WAIT_REFUSES_WITHOUT_CLOCK", c_wait_needs_clock}, |
| 811 | {"FMT_BOUNDED_MATCHES_SNPRINTF", c_fmt_matches_snprintf}, |
| 812 | }; |
| 813 | |
| 814 | int main(void) { |
| 815 | int pass = 0, fail = 0; |
| 816 | for (size_t i = 0; i < sizeof CASES / sizeof CASES[0]; i++) { |
| 817 | fflush(stdout); |
| 818 | pid_t pid = fork(); |
| 819 | if (pid == 0) { case_ok = 1; CASES[i].fn(); fflush(stdout); _exit(case_ok ? 0 : 1); } |
| 820 | int st = 0; |
| 821 | waitpid(pid, &st, 0); |
| 822 | const int ok = WIFEXITED(st) && WEXITSTATUS(st) == 0; |
| 823 | printf("%s %s\n", ok ? "PASS" : "FAIL", CASES[i].id); |
| 824 | if (ok) pass++; else fail++; |
| 825 | } |
| 826 | printf("test_omega_gpu_session_platform: %d passed, %d failed\n", pass, fail); |
| 827 | return fail ? 1 : 0; |
| 828 | } |