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 / dagsim / dagsim_unit.c
| 1 | /* |
| 2 | * dagsim_unit -- analytical tests for the offline resource-DAG evaluator |
| 3 | * (omega-mv#583 acceptance, first box). Any host, no GPU. |
| 4 | * |
| 5 | * Every case is a DAG whose answer is known by hand. The evaluator must |
| 6 | * produce exactly that number (to 1 ns), the same number twice, and never a |
| 7 | * makespan below its own bounds. |
| 8 | */ |
| 9 | #include "dagsim.h" |
| 10 | #include "runtime/rx_costmodel.h" |
| 11 | |
| 12 | #include <math.h> |
| 13 | #include <stdio.h> |
| 14 | #include <stdlib.h> |
| 15 | #include <string.h> |
| 16 | |
| 17 | static int g_checks, g_fail; |
| 18 | #define CHECK(cond, ...) do { \ |
| 19 | g_checks++; \ |
| 20 | if (!(cond)) { \ |
| 21 | g_fail++; \ |
| 22 | fprintf(stderr, " FAIL %s:%d ", __FILE__, __LINE__); \ |
| 23 | fprintf(stderr, __VA_ARGS__); \ |
| 24 | fputc('\n', stderr); \ |
| 25 | } \ |
| 26 | } while (0) |
| 27 | #define NEAR(a, b, tol) (fabs((a) - (b)) <= (tol)) |
| 28 | |
| 29 | /* The real predict never sets known on a refusal. These cases do, so a caller |
| 30 | * that only looks at p.known will certify a time. 0 calls the real function. |
| 31 | * 1: RX_CM_ERR_UNSUPPORTED with known set and refused clear. |
| 32 | * 2: RX_CM_OK with refused set and known set. */ |
| 33 | static int g_predict_fault; |
| 34 | |
| 35 | extern int __real_rx_cm_predict(const RxCostModel *m, const RxCmFeatures *f, |
| 36 | uint32_t arm, RxCmPrediction *out); |
| 37 | |
| 38 | int __wrap_rx_cm_predict(const RxCostModel *m, const RxCmFeatures *f, |
| 39 | uint32_t arm, RxCmPrediction *out) { |
| 40 | if (g_predict_fault == 0) return __real_rx_cm_predict(m, f, arm, out); |
| 41 | memset(out, 0, sizeof *out); |
| 42 | out->known = 1; |
| 43 | out->n = 32; |
| 44 | out->mean_log2_ps = 20.0; |
| 45 | out->sd_log2 = 0.0; |
| 46 | out->dof = 1.0; |
| 47 | if (g_predict_fault == 1) return RX_CM_ERR_UNSUPPORTED; |
| 48 | out->refused = 1; |
| 49 | return RX_CM_OK; |
| 50 | } |
| 51 | |
| 52 | static const char *HDR = |
| 53 | "oev version=1\n" |
| 54 | "machine id=t soc=t cores=4 mem_bytes=1000 prov=specified src=test\n" |
| 55 | "build repo=omega-mv sha=0000000000000000000000000000000000000000\n" |
| 56 | "calibration id=c1 taken=2026-10-19 valid_days=30 load1=1.0 src=test\n" |
| 57 | "resource id=mem kind=capacity cap=1000 prov=specified src=test\n" |
| 58 | "energy instrument=none\n"; |
| 59 | |
| 60 | static int eval_text(const char *body, ds_model *m, ds_result *r, const ds_opts *o) { |
| 61 | char text[8192], err[256]; |
| 62 | snprintf(text, sizeof text, "%s%s", HDR, body); |
| 63 | int rc = ds_parse_text(text, m, err, sizeof err); |
| 64 | if (rc != DS_OK) { fprintf(stderr, " parse: %s\n", err); r->status = rc; return rc; } |
| 65 | ds_opts def; memset(&def, 0, sizeof def); |
| 66 | def.asof_day = ds_day_from_date("2026-10-20"); |
| 67 | def.interval_q = 0.9; |
| 68 | rc = ds_eval(m, o ? o : &def, r); |
| 69 | /* invariants every OK result must satisfy */ |
| 70 | if (rc == DS_OK) { |
| 71 | CHECK(r->makespan_ns + 1e-6 >= r->bound_ns, "makespan %.1f below bound %.1f", r->makespan_ns, r->bound_ns); |
| 72 | CHECK(r->makespan_ns + 1e-6 >= r->bound_critical_path_ns, "below critical path bound"); |
| 73 | double s = 0; for (int i = 0; i < DS_ATTR_N; i++) s += r->attr_ns[i]; |
| 74 | CHECK(NEAR(s, r->makespan_ns, 1e-6), "attribution %.3f does not sum to makespan %.3f", s, r->makespan_ns); |
| 75 | ds_result r2; ds_eval(m, o ? o : &def, &r2); |
| 76 | CHECK(memcmp(&r2, r, sizeof r2) == 0, "not deterministic"); |
| 77 | } |
| 78 | return rc; |
| 79 | } |
| 80 | |
| 81 | static void test_serial_chain(void) { |
| 82 | ds_model m; ds_result r; |
| 83 | int rc = eval_text( |
| 84 | "resource id=cpu kind=compute slots=1 prov=specified src=test\n" |
| 85 | "op id=A kind=compute engine=cpu t_ns=10 prov=measured src=t\n" |
| 86 | "op id=B kind=compute engine=cpu t_ns=20 prov=measured src=t\n" |
| 87 | "op id=C kind=compute engine=cpu t_ns=30 prov=measured src=t\n" |
| 88 | "dep A B\ndep B C\n", &m, &r, NULL); |
| 89 | CHECK(rc == DS_OK, "serial chain status %s: %s", ds_status_name(r.status), r.reason); |
| 90 | CHECK(NEAR(r.makespan_ns, 60, 1e-9), "serial makespan %.3f", r.makespan_ns); |
| 91 | CHECK(NEAR(r.bound_critical_path_ns, 60, 1e-9), "critical path bound %.3f", r.bound_critical_path_ns); |
| 92 | CHECK(r.n_critical == 3, "critical path length %d", r.n_critical); |
| 93 | CHECK(NEAR(r.attr_ns[DS_ATTR_COMPUTE], 60, 1e-9), "compute attribution %.3f", r.attr_ns[DS_ATTR_COMPUTE]); |
| 94 | CHECK(NEAR(r.attr_ns[DS_ATTR_RESIDUAL], 0, 1e-9), "residual %.3f", r.attr_ns[DS_ATTR_RESIDUAL]); |
| 95 | CHECK(NEAR(r.start_ns[2], 30, 1e-9) && NEAR(r.end_ns[2], 60, 1e-9), "C window"); |
| 96 | CHECK(r.prov_mask == (1u << DS_PROV_MEASURED | 1u << DS_PROV_SPECIFIED), "prov mask %x", r.prov_mask); |
| 97 | CHECK(r.n_ops == 3 && NEAR(r.total_busy_ns, 60, 1e-9), "conservation"); |
| 98 | } |
| 99 | |
| 100 | static void test_perfect_overlap(void) { |
| 101 | ds_model m; ds_result r; |
| 102 | int rc = eval_text( |
| 103 | "resource id=cpu kind=compute slots=4 prov=specified src=test\n" |
| 104 | "op id=A kind=compute engine=cpu t_ns=10 prov=measured src=t\n" |
| 105 | "op id=B kind=compute engine=cpu t_ns=20 prov=measured src=t\n" |
| 106 | "op id=C kind=compute engine=cpu t_ns=30 prov=measured src=t\n", &m, &r, NULL); |
| 107 | CHECK(rc == DS_OK, "overlap status %s", ds_status_name(r.status)); |
| 108 | CHECK(NEAR(r.makespan_ns, 30, 1e-9), "independent overlap makespan %.3f", r.makespan_ns); |
| 109 | CHECK(NEAR(r.attr_ns[DS_ATTR_SERIALIZATION], 0, 1e-9), "no serialization when slots suffice"); |
| 110 | /* same DAG, one slot: work bound 60 binds and the wait is serialization */ |
| 111 | rc = eval_text( |
| 112 | "resource id=cpu kind=compute slots=1 prov=specified src=test\n" |
| 113 | "op id=A kind=compute engine=cpu t_ns=10 prov=measured src=t\n" |
| 114 | "op id=B kind=compute engine=cpu t_ns=20 prov=measured src=t\n" |
| 115 | "op id=C kind=compute engine=cpu t_ns=30 prov=measured src=t\n", &m, &r, NULL); |
| 116 | CHECK(rc == DS_OK, "one slot status"); |
| 117 | CHECK(NEAR(r.makespan_ns, 60, 1e-9), "one-slot makespan %.3f", r.makespan_ns); |
| 118 | CHECK(NEAR(r.bound_critical_path_ns, 30, 1e-9), "critical path stays 30"); |
| 119 | CHECK(NEAR(r.bound_ns, 60, 1e-9), "resource bound 60, got %.3f", r.bound_ns); |
| 120 | CHECK(NEAR(r.attr_ns[DS_ATTR_SERIALIZATION], 30, 1e-9), "serialization %.3f", r.attr_ns[DS_ATTR_SERIALIZATION]); |
| 121 | } |
| 122 | |
| 123 | static void test_shared_bandwidth(void) { |
| 124 | ds_model m; ds_result r; |
| 125 | /* two 1 GB copies on a 1 GB/s pipe, both slots free: each gets half, both end at 2 s */ |
| 126 | int rc = eval_text( |
| 127 | "resource id=cpu kind=compute slots=2 prov=specified src=test\n" |
| 128 | "resource id=pipe kind=bandwidth cap=1e9 prov=measured src=t\n" |
| 129 | "op id=A kind=copy engine=cpu bytes=1e9 bw=pipe prov=measured src=t\n" |
| 130 | "op id=B kind=copy engine=cpu bytes=1e9 bw=pipe prov=measured src=t\n", &m, &r, NULL); |
| 131 | CHECK(rc == DS_OK, "bw status %s: %s", ds_status_name(r.status), r.reason); |
| 132 | CHECK(NEAR(r.makespan_ns, 2e9, 1e-3), "two concurrent full-bandwidth claims must not sum: %.0f", r.makespan_ns); |
| 133 | CHECK(NEAR(r.end_ns[0], 2e9, 1e-3) && NEAR(r.end_ns[1], 2e9, 1e-3), "both end at 2 s"); |
| 134 | CHECK(NEAR(r.op_contention_ns[0], 1e9, 1e-3), "A contention %.0f", r.op_contention_ns[0]); |
| 135 | CHECK(NEAR(r.bound_critical_path_ns, 1e9, 1e-3), "cp bound 1 s"); |
| 136 | CHECK(NEAR(r.bound_ns, 2e9, 1e-3), "pipe work bound 2 s, got %.0f", r.bound_ns); |
| 137 | CHECK(NEAR(r.attr_ns[DS_ATTR_BANDWIDTH] + r.attr_ns[DS_ATTR_CONTENTION], 2e9, 1e-3), "bw+contention = 2 s"); |
| 138 | CHECK(NEAR(r.attr_ns[DS_ATTR_CONTENTION], 1e9, 1e-3), "contention attributed 1 s, got %.0f", r.attr_ns[DS_ATTR_CONTENTION]); |
| 139 | /* unequal: B is half the size; it finishes at 1 s, A at 1.5 s */ |
| 140 | rc = eval_text( |
| 141 | "resource id=cpu kind=compute slots=2 prov=specified src=test\n" |
| 142 | "resource id=pipe kind=bandwidth cap=1e9 prov=measured src=t\n" |
| 143 | "op id=A kind=copy engine=cpu bytes=1e9 bw=pipe prov=measured src=t\n" |
| 144 | "op id=B kind=copy engine=cpu bytes=5e8 bw=pipe prov=measured src=t\n", &m, &r, NULL); |
| 145 | CHECK(rc == DS_OK, "bw2 status"); |
| 146 | CHECK(NEAR(r.end_ns[1], 1e9, 1e-3), "B ends at 1 s, got %.0f", r.end_ns[1]); |
| 147 | CHECK(NEAR(r.end_ns[0], 1.5e9, 1e-3), "A ends at 1.5 s, got %.0f", r.end_ns[0]); |
| 148 | /* compute and traffic inside one op overlap: floor is max(F/P, Q/B) */ |
| 149 | rc = eval_text( |
| 150 | "resource id=cpu kind=compute slots=1 prov=specified src=test\n" |
| 151 | "resource id=pipe kind=bandwidth cap=1e9 prov=measured src=t\n" |
| 152 | "op id=A kind=compute engine=cpu t_ns=5e8 bytes=1e9 bw=pipe prov=measured src=t\n", &m, &r, NULL); |
| 153 | CHECK(rc == DS_OK && NEAR(r.makespan_ns, 1e9, 1e-3), "max(F/P,Q/B) floor, got %.0f", r.makespan_ns); |
| 154 | CHECK(NEAR(r.op_floor_ns[0], 1e9, 1e-3), "op floor"); |
| 155 | /* a compute-bound op streaming alone has no contention, whatever its busy time */ |
| 156 | rc = eval_text( |
| 157 | "resource id=cpu kind=compute slots=1 prov=specified src=test\n" |
| 158 | "resource id=pipe kind=bandwidth cap=1e9 prov=measured src=t\n" |
| 159 | "op id=A kind=compute engine=cpu t_ns=4e9 bytes=1e9 bw=pipe prov=measured src=t\n", &m, &r, NULL); |
| 160 | CHECK(rc == DS_OK && NEAR(r.makespan_ns, 4e9, 1e-3) && NEAR(r.op_contention_ns[0], 0, 1e-3), |
| 161 | "compute-bound op alone: contention %.0f (want 0)", r.op_contention_ns[0]); |
| 162 | CHECK(NEAR(r.attr_ns[DS_ATTR_COMPUTE], 4e9, 1e-3), "attributed as compute"); |
| 163 | /* serialized sharing: one slot, so no contention and makespan = sum */ |
| 164 | rc = eval_text( |
| 165 | "resource id=cpu kind=compute slots=1 prov=specified src=test\n" |
| 166 | "resource id=pipe kind=bandwidth cap=1e9 prov=measured src=t\n" |
| 167 | "op id=A kind=copy engine=cpu bytes=1e9 bw=pipe prov=measured src=t\n" |
| 168 | "op id=B kind=copy engine=cpu bytes=1e9 bw=pipe prov=measured src=t\n", &m, &r, NULL); |
| 169 | CHECK(rc == DS_OK && NEAR(r.makespan_ns, 2e9, 1e-3) && NEAR(r.attr_ns[DS_ATTR_CONTENTION], 0, 1e-3), |
| 170 | "serialized copies: no contention, makespan %.0f contention %.0f", r.makespan_ns, r.attr_ns[DS_ATTR_CONTENTION]); |
| 171 | } |
| 172 | |
| 173 | static void test_capacity_refusal(void) { |
| 174 | ds_model m; ds_result r; |
| 175 | int rc = eval_text( |
| 176 | "resource id=cpu kind=compute slots=2 prov=specified src=test\n" |
| 177 | "op id=A kind=compute engine=cpu t_ns=10 prov=measured src=t\n" |
| 178 | "buffer id=x bytes=600 cap=mem first=A last=A\n" |
| 179 | "buffer id=y bytes=600 cap=mem first=A last=A\n", &m, &r, NULL); |
| 180 | CHECK(rc == DS_REFUSED_CAPACITY, "1200 live bytes in 1000 must refuse, got %s", ds_status_name(rc)); |
| 181 | CHECK(strstr(r.reason, "x") && strstr(r.reason, "y"), "reason names the buffers: %s", r.reason); |
| 182 | /* same buffers, disjoint lifetimes: fits */ |
| 183 | rc = eval_text( |
| 184 | "resource id=cpu kind=compute slots=2 prov=specified src=test\n" |
| 185 | "op id=A kind=compute engine=cpu t_ns=10 prov=measured src=t\n" |
| 186 | "op id=B kind=compute engine=cpu t_ns=10 prov=measured src=t\n" |
| 187 | "dep A B\n" |
| 188 | "buffer id=x bytes=600 cap=mem first=A last=A\n" |
| 189 | "buffer id=y bytes=600 cap=mem first=B last=B\n", &m, &r, NULL); |
| 190 | CHECK(rc == DS_OK, "disjoint lifetimes fit, got %s: %s", ds_status_name(rc), r.reason); |
| 191 | CHECK(NEAR(r.peak_live_bytes[0], 600, 1e-9), "peak 600, got %.0f", r.peak_live_bytes[0]); |
| 192 | /* a single buffer larger than the ledger refuses before any scheduling */ |
| 193 | rc = eval_text( |
| 194 | "resource id=cpu kind=compute slots=2 prov=specified src=test\n" |
| 195 | "op id=A kind=compute engine=cpu t_ns=10 prov=measured src=t\n" |
| 196 | "buffer id=big bytes=2000 cap=mem\n", &m, &r, NULL); |
| 197 | CHECK(rc == DS_REFUSED_CAPACITY, "oversized resident buffer refuses"); |
| 198 | } |
| 199 | |
| 200 | static void test_lifetimes(void) { |
| 201 | ds_model m; ds_result r; |
| 202 | int rc = eval_text( |
| 203 | "resource id=cpu kind=compute slots=1 prov=specified src=test\n" |
| 204 | "op id=A kind=compute engine=cpu t_ns=10 prov=measured src=t\n" |
| 205 | "op id=B kind=compute engine=cpu t_ns=10 prov=measured src=t\n" |
| 206 | "op id=C kind=compute engine=cpu t_ns=10 prov=measured src=t\n" |
| 207 | "dep A B\ndep B C\n" |
| 208 | "buffer id=resident bytes=100 cap=mem\n" |
| 209 | "buffer id=x bytes=300 cap=mem first=A last=B\n" |
| 210 | "buffer id=y bytes=400 cap=mem first=B last=C\n", &m, &r, NULL); |
| 211 | CHECK(rc == DS_OK, "lifetimes status %s: %s", ds_status_name(rc), r.reason); |
| 212 | CHECK(NEAR(r.peak_live_bytes[0], 800, 1e-9), "peak = resident + x + y during B, got %.0f", r.peak_live_bytes[0]); |
| 213 | CHECK(NEAR(r.peak_live_at_ns[0], 10, 1e-9), "peak reached when B starts, got %.1f", r.peak_live_at_ns[0]); |
| 214 | } |
| 215 | |
| 216 | static void test_sync(void) { |
| 217 | ds_model m; ds_result r; |
| 218 | int rc = eval_text( |
| 219 | "resource id=cpu kind=compute slots=2 prov=specified src=test\n" |
| 220 | "op id=A kind=compute engine=cpu t_ns=10 prov=measured src=t\n" |
| 221 | "op id=B kind=compute engine=cpu t_ns=30 prov=measured src=t\n" |
| 222 | "op id=C kind=compute engine=cpu t_ns=5 prov=measured src=t\n" |
| 223 | "sync id=s ops=A,B\n" |
| 224 | "dep s C\n", &m, &r, NULL); |
| 225 | CHECK(rc == DS_OK, "sync status %s: %s", ds_status_name(rc), r.reason); |
| 226 | int c = ds_find_op(&m, "C"); |
| 227 | CHECK(c >= 0 && NEAR(r.start_ns[c], 30, 1e-9), "C waits for the slower of A,B: start %.1f", c >= 0 ? r.start_ns[c] : -1); |
| 228 | CHECK(NEAR(r.makespan_ns, 35, 1e-9), "sync makespan %.1f", r.makespan_ns); |
| 229 | /* a barrier with a declared cost is attributed as sync */ |
| 230 | rc = eval_text( |
| 231 | "resource id=cpu kind=compute slots=2 prov=specified src=test\n" |
| 232 | "op id=A kind=compute engine=cpu t_ns=10 prov=measured src=t\n" |
| 233 | "op id=B kind=compute engine=cpu t_ns=30 prov=measured src=t\n" |
| 234 | "op id=fence kind=barrier t_ns=7 prov=measured src=t\n" |
| 235 | "op id=C kind=compute engine=cpu t_ns=5 prov=measured src=t\n" |
| 236 | "dep A fence\ndep B fence\ndep fence C\n", &m, &r, NULL); |
| 237 | CHECK(rc == DS_OK && NEAR(r.makespan_ns, 42, 1e-9), "costed barrier makespan %.1f", r.makespan_ns); |
| 238 | CHECK(NEAR(r.attr_ns[DS_ATTR_SYNC], 7, 1e-9), "sync attribution %.1f", r.attr_ns[DS_ATTR_SYNC]); |
| 239 | } |
| 240 | |
| 241 | static void test_launch(void) { |
| 242 | ds_model m; ds_result r; |
| 243 | /* two jobs, one launcher, one slot: launches serialize, 80 ms */ |
| 244 | int rc = eval_text( |
| 245 | "resource id=gpu kind=compute slots=1 prov=specified src=test\n" |
| 246 | "resource id=L kind=launch prov=measured src=t\n" |
| 247 | "op id=A kind=compute engine=gpu t_ns=0 launch_ns=40e6 launcher=L prov=measured src=t\n" |
| 248 | "op id=B kind=compute engine=gpu t_ns=0 launch_ns=40e6 launcher=L prov=measured src=t\n", &m, &r, NULL); |
| 249 | CHECK(rc == DS_OK && NEAR(r.makespan_ns, 80e6, 1e-3), "serialized launches add: %.0f", r.makespan_ns); |
| 250 | CHECK(NEAR(r.attr_ns[DS_ATTR_LAUNCH] + r.attr_ns[DS_ATTR_SERIALIZATION], 80e6, 1e-3), "launch+serialization"); |
| 251 | /* two engines, two launchers: launches overlap, 40 ms */ |
| 252 | rc = eval_text( |
| 253 | "resource id=gpu kind=compute slots=1 prov=specified src=test\n" |
| 254 | "resource id=cpu kind=compute slots=1 prov=specified src=test\n" |
| 255 | "resource id=L1 kind=launch prov=measured src=t\n" |
| 256 | "resource id=L2 kind=launch prov=measured src=t\n" |
| 257 | "op id=A kind=compute engine=gpu t_ns=0 launch_ns=40e6 launcher=L1 prov=measured src=t\n" |
| 258 | "op id=B kind=compute engine=cpu t_ns=0 launch_ns=40e6 launcher=L2 prov=measured src=t\n", &m, &r, NULL); |
| 259 | CHECK(rc == DS_OK && NEAR(r.makespan_ns, 40e6, 1e-3), "independent launches overlap: %.0f", r.makespan_ns); |
| 260 | } |
| 261 | |
| 262 | static void test_calibration(void) { |
| 263 | ds_model m; ds_result r; char text[4096], err[256]; |
| 264 | const char *body = |
| 265 | "resource id=cpu kind=compute slots=1 prov=specified src=test\n" |
| 266 | "op id=A kind=compute engine=cpu t_ns=10 prov=measured src=t\n"; |
| 267 | /* stale: asof past taken + valid_days */ |
| 268 | ds_opts o; memset(&o, 0, sizeof o); o.interval_q = 0.9; |
| 269 | o.asof_day = ds_day_from_date("2026-11-19"); |
| 270 | int rc = eval_text(body, &m, &r, &o); |
| 271 | CHECK(rc == DS_REFUSED_STALE, "31 days after a 30-day calibration refuses, got %s", ds_status_name(rc)); |
| 272 | o.asof_day = ds_day_from_date("2026-11-18"); |
| 273 | rc = eval_text(body, &m, &r, &o); |
| 274 | CHECK(rc == DS_OK, "last valid day is fine, got %s", ds_status_name(rc)); |
| 275 | /* missing: measured value without a calibration record */ |
| 276 | snprintf(text, sizeof text, |
| 277 | "oev version=1\n" |
| 278 | "machine id=t soc=t cores=4 mem_bytes=1000 prov=specified src=test\n" |
| 279 | "build repo=omega-mv sha=0000000000000000000000000000000000000000\n" |
| 280 | "energy instrument=none\n%s", body); |
| 281 | rc = ds_parse_text(text, &m, err, sizeof err); |
| 282 | CHECK(rc == DS_OK, "parse without calibration: %s", err); |
| 283 | o.asof_day = ds_day_from_date("2026-10-20"); |
| 284 | rc = ds_eval(&m, &o, &r); |
| 285 | CHECK(rc == DS_REFUSED_NO_CALIBRATION, "measured without calibration refuses, got %s", ds_status_name(rc)); |
| 286 | /* unknown provenance never becomes a number */ |
| 287 | rc = eval_text( |
| 288 | "resource id=cpu kind=compute slots=1 prov=specified src=test\n" |
| 289 | "op id=A kind=compute engine=cpu t_ns=10 prov=measured src=t\n" |
| 290 | "op id=B kind=compute engine=cpu prov=unknown src=arch-244-not-run\n" |
| 291 | "dep A B\n", &m, &r, NULL); |
| 292 | CHECK(rc == DS_REFUSED_UNKNOWN, "unknown op refuses, got %s", ds_status_name(rc)); |
| 293 | CHECK(strstr(r.reason, "B") != NULL, "reason names the op: %s", r.reason); |
| 294 | CHECK(r.makespan_ns == 0, "no makespan on refusal"); |
| 295 | /* synthetic input marks the result */ |
| 296 | rc = eval_text( |
| 297 | "resource id=cpu kind=compute slots=1 prov=specified src=test\n" |
| 298 | "op id=A kind=compute engine=cpu t_ns=10 prov=synthetic src=t\n", &m, &r, NULL); |
| 299 | CHECK(rc == DS_OK && r.synthetic == 1, "synthetic flag"); |
| 300 | } |
| 301 | |
| 302 | static void test_intervals(void) { |
| 303 | ds_model m; ds_result r; double lo, hi; |
| 304 | char text[8192]; |
| 305 | snprintf(text, sizeof text, "%s%s", HDR, |
| 306 | "resource id=cpu kind=compute slots=2 prov=specified src=test\n" |
| 307 | "resource id=pipe kind=bandwidth cap=1e9 lo=0.5e9 hi=2e9 prov=measured src=t\n" |
| 308 | "op id=A kind=compute engine=cpu t_ns=100 lo=80 hi=150 prov=measured src=t\n" |
| 309 | "op id=B kind=copy engine=cpu bytes=1e9 bw=pipe prov=measured src=t\n" |
| 310 | "dep A B\n"); |
| 311 | char err[256]; |
| 312 | CHECK(ds_parse_text(text, &m, err, sizeof err) == DS_OK, "parse: %s", err); |
| 313 | ds_opts o; memset(&o, 0, sizeof o); o.asof_day = ds_day_from_date("2026-10-20"); o.interval_q = 0.9; |
| 314 | int rc = ds_eval_interval(&m, &o, &r, &lo, &hi); |
| 315 | CHECK(rc == DS_OK, "interval status"); |
| 316 | CHECK(NEAR(r.makespan_ns, 100 + 1e9, 1e-3), "nominal %.0f", r.makespan_ns); |
| 317 | CHECK(NEAR(lo, 80 + 0.5e9, 1e-3), "optimistic %.0f", lo); |
| 318 | CHECK(NEAR(hi, 150 + 2e9, 1e-3), "pessimistic %.0f", hi); |
| 319 | } |
| 320 | |
| 321 | static void test_energy(void) { |
| 322 | ds_model m; ds_result r; char text[4096], err[256]; |
| 323 | int rc = eval_text( |
| 324 | "resource id=cpu kind=compute slots=1 prov=specified src=test\n" |
| 325 | "op id=A kind=compute engine=cpu t_ns=1e9 prov=measured src=t\n", &m, &r, NULL); |
| 326 | CHECK(rc == DS_OK && !r.energy_measured && !r.energy_synthetic, "instrument=none means UNMEASURED"); |
| 327 | snprintf(text, sizeof text, |
| 328 | "oev version=1\n" |
| 329 | "machine id=t soc=t cores=4 mem_bytes=1000 prov=specified src=test\n" |
| 330 | "build repo=omega-mv sha=0000000000000000000000000000000000000000\n" |
| 331 | "calibration id=c1 taken=2026-10-19 valid_days=30 load1=1.0 src=test\n" |
| 332 | "resource id=cpu kind=compute slots=1 prov=specified src=test\n" |
| 333 | "op id=A kind=compute engine=cpu t_ns=1e9 prov=measured src=t\n" |
| 334 | "energy instrument=tdp-guess boundary=soc power_w=100 prov=synthetic\n"); |
| 335 | CHECK(ds_parse_text(text, &m, err, sizeof err) == DS_OK, "parse: %s", err); |
| 336 | ds_opts o; memset(&o, 0, sizeof o); o.asof_day = ds_day_from_date("2026-10-20"); o.interval_q = 0.9; |
| 337 | rc = ds_eval(&m, &o, &r); |
| 338 | CHECK(rc == DS_OK && r.energy_synthetic == 1 && !r.energy_measured && NEAR(r.energy_j, 100.0, 1e-9) && r.synthetic, |
| 339 | "synthetic power gives a labelled E=P*T (100 J), got %.3f syn=%d", r.energy_j, r.energy_synthetic); |
| 340 | snprintf(text, sizeof text, |
| 341 | "oev version=1\n" |
| 342 | "machine id=t soc=t cores=4 mem_bytes=1000 prov=specified src=test\n" |
| 343 | "build repo=omega-mv sha=0000000000000000000000000000000000000000\n" |
| 344 | "calibration id=c1 taken=2026-10-19 valid_days=30 load1=1.0 src=test\n" |
| 345 | "resource id=cpu kind=compute slots=1 prov=specified src=test\n" |
| 346 | "op id=A kind=compute engine=cpu t_ns=1e9 prov=measured src=t\n" |
| 347 | "energy instrument=cyberpower-ups-nut boundary=wall joules=42.5 prov=measured src=receipt.json\n"); |
| 348 | CHECK(ds_parse_text(text, &m, err, sizeof err) == DS_OK, "parse: %s", err); |
| 349 | rc = ds_eval(&m, &o, &r); |
| 350 | CHECK(rc == DS_OK && r.energy_measured == 1 && NEAR(r.energy_j, 42.5, 1e-9) && !r.synthetic, "measured joules pass through"); |
| 351 | /* a measured joules figure with no instrument name is a parse error */ |
| 352 | snprintf(text, sizeof text, |
| 353 | "oev version=1\n" |
| 354 | "machine id=t soc=t cores=4 mem_bytes=1000 prov=specified src=test\n" |
| 355 | "build repo=omega-mv sha=0000000000000000000000000000000000000000\n" |
| 356 | "energy instrument=none joules=42.5 prov=measured src=x\n"); |
| 357 | CHECK(ds_parse_text(text, &m, err, sizeof err) == DS_PARSE_ERROR, "joules without an instrument is refused at parse"); |
| 358 | } |
| 359 | |
| 360 | static void test_parse_and_cycles(void) { |
| 361 | ds_model m; ds_result r; char text[4096], err[256]; |
| 362 | snprintf(text, sizeof text, "%s%s", HDR, "resource id=cpu kind=compute slots=1 prov=specified src=test\nbogus x=1\n"); |
| 363 | int rc = ds_parse_text(text, &m, err, sizeof err); |
| 364 | CHECK(rc == DS_PARSE_ERROR && strstr(err, "line 8"), "unknown keyword is an error with its line: %s", err); |
| 365 | snprintf(text, sizeof text, "%s%s", HDR, "resource id=cpu kind=compute slots=1 prov=specified src=test\nop id=A kind=compute engine=cpu t_ns=1 prov=measured src=t typo=3\n"); |
| 366 | rc = ds_parse_text(text, &m, err, sizeof err); |
| 367 | CHECK(rc == DS_PARSE_ERROR, "unknown field is an error: %s", err); |
| 368 | snprintf(text, sizeof text, "%s%s", HDR, "resource id=cpu kind=compute slots=1 prov=specified src=test\nop id=A kind=compute engine=cpu t_ns=1 src=t\n"); |
| 369 | rc = ds_parse_text(text, &m, err, sizeof err); |
| 370 | CHECK(rc == DS_PARSE_ERROR, "a number without prov is an error: %s", err); |
| 371 | rc = eval_text( |
| 372 | "resource id=cpu kind=compute slots=1 prov=specified src=test\n" |
| 373 | "op id=A kind=compute engine=cpu t_ns=1 prov=measured src=t\n" |
| 374 | "op id=B kind=compute engine=cpu t_ns=1 prov=measured src=t\n" |
| 375 | "dep A B\ndep B A\n", &m, &r, NULL); |
| 376 | CHECK(rc == DS_REFUSED_CYCLE, "cycle refuses, got %s", ds_status_name(rc)); |
| 377 | /* the pinned fixture parses and evaluates */ |
| 378 | rc = ds_parse_file("tests/dagsim/fixtures/m1_spark_subset.oev", &m, err, sizeof err); |
| 379 | CHECK(rc == DS_OK, "fixture parses: %s", err); |
| 380 | if (rc == DS_OK) { |
| 381 | ds_opts o; memset(&o, 0, sizeof o); o.asof_day = ds_day_from_date("2026-10-20"); o.interval_q = 0.9; |
| 382 | rc = ds_eval(&m, &o, &r); |
| 383 | CHECK(rc == DS_OK, "fixture evaluates: %s", r.reason); |
| 384 | CHECK(NEAR(r.makespan_ns, 20000 + 40736700 + 111000000, 1e-3), "fixture serial makespan %.0f", r.makespan_ns); |
| 385 | CHECK(!r.energy_measured && !r.energy_synthetic, "fixture energy UNMEASURED"); |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | /* rx_costmodel integration: a matvec op with a fitted blob gets its time from |
| 390 | * rx_cm_predict; an unknown cell refuses. Synthetic observations, so the blob is |
| 391 | * labelled fitted on synthetic data and the result is marked synthetic. */ |
| 392 | static void test_costmodel_reuse(void) { |
| 393 | RxCostModel cm; rx_cm_init(&cm, 2); |
| 394 | uint64_t rng = 0x9E3779B97F4A7C15ull; |
| 395 | for (int i = 0; i < 200; i++) { |
| 396 | rng ^= rng << 13; rng ^= rng >> 7; rng ^= rng << 17; |
| 397 | uint32_t M = 8 + (uint32_t)(rng % 120), N = 4 + (uint32_t)((rng >> 20) % 60); |
| 398 | RxCmFeatures f; memset(&f, 0, sizeof f); |
| 399 | f.op = RX_CM_OP_MATVEC; f.M = M; f.N = N; f.state_bytes = ((uint64_t)M * N + M + N) * 8u; |
| 400 | f.cache_bytes = 1u << 20; f.core = RX_CM_CORE_X925; f.eligible = 3; |
| 401 | double l2 = 10.0 + 0.95 * (log2((double)M) + log2((double)N)); |
| 402 | RxCmObservation o; o.failed = 0; o.ps_per_call = (uint64_t)pow(2.0, l2); |
| 403 | rx_cm_observe(&cm, &f, 0, &o); |
| 404 | } |
| 405 | ds_model m; ds_result r; char text[4096], err[256]; |
| 406 | snprintf(text, sizeof text, "%s%s", HDR, |
| 407 | "resource id=cpu kind=compute slots=1 prov=specified src=test\n" |
| 408 | "op id=mv kind=matvec engine=cpu M=64 N=16 core=0 arm=0 prov=fitted src=rx_costmodel\n"); |
| 409 | CHECK(ds_parse_text(text, &m, err, sizeof err) == DS_OK, "parse: %s", err); |
| 410 | ds_opts o; memset(&o, 0, sizeof o); o.asof_day = ds_day_from_date("2026-10-20"); o.interval_q = 0.9; |
| 411 | int rc = ds_eval(&m, &o, &r); |
| 412 | CHECK(rc == DS_REFUSED_UNKNOWN, "matvec with no model refuses, got %s", ds_status_name(rc)); |
| 413 | o.costmodel = &cm; |
| 414 | rc = ds_eval(&m, &o, &r); |
| 415 | CHECK(rc == DS_OK, "matvec with model: %s %s", ds_status_name(rc), r.reason); |
| 416 | RxCmFeatures f; memset(&f, 0, sizeof f); |
| 417 | f.op = RX_CM_OP_MATVEC; f.M = 64; f.N = 16; f.state_bytes = (64u * 16u + 64u + 16u) * 8u; f.cache_bytes = 1u << 20; f.core = 0; |
| 418 | RxCmPrediction p; rx_cm_predict(&cm, &f, 0, &p); |
| 419 | CHECK(p.known, "cell known"); |
| 420 | CHECK(NEAR(r.makespan_ns, pow(2.0, p.mean_log2_ps) / 1000.0, 1e-6), "time comes from rx_cm_predict: %.3f vs %.3f", r.makespan_ns, pow(2.0, p.mean_log2_ps) / 1000.0); |
| 421 | CHECK(r.prov_mask & (1u << DS_PROV_FITTED), "fitted provenance recorded"); |
| 422 | /* arm 1 was never observed: unknown, refuse */ |
| 423 | snprintf(text, sizeof text, "%s%s", HDR, |
| 424 | "resource id=cpu kind=compute slots=1 prov=specified src=test\n" |
| 425 | "op id=mv kind=matvec engine=cpu M=64 N=16 core=0 arm=1 prov=fitted src=rx_costmodel\n"); |
| 426 | CHECK(ds_parse_text(text, &m, err, sizeof err) == DS_OK, "parse: %s", err); |
| 427 | rc = ds_eval(&m, &o, &r); |
| 428 | CHECK(rc == DS_REFUSED_UNKNOWN, "unobserved arm refuses, got %s", ds_status_name(rc)); |
| 429 | } |
| 430 | |
| 431 | /* #934: an unsupported id is refused, and that refusal is not a known cell. |
| 432 | * The two fault cases set known anyway. They must still refuse. */ |
| 433 | static void test_predict_refusal(void) { |
| 434 | RxCostModel cm; rx_cm_init(&cm, 1); |
| 435 | RxCmFeatures f; memset(&f, 0, sizeof f); |
| 436 | f.op = RX_CM_OPS; f.M = 64; f.N = 16; f.core = 0; |
| 437 | RxCmPrediction p; |
| 438 | int st = rx_cm_predict(&cm, &f, 0, &p); |
| 439 | CHECK(st == RX_CM_ERR_UNSUPPORTED && p.refused && !p.known && p.n == 0, |
| 440 | "real predict refuses an unsupported op, st=%d refused=%d known=%d n=%u", |
| 441 | st, p.refused, p.known, p.n); |
| 442 | |
| 443 | ds_model m; ds_result r; char text[4096], err[256]; |
| 444 | snprintf(text, sizeof text, "%s%s", HDR, |
| 445 | "resource id=cpu kind=compute slots=1 prov=specified src=test\n" |
| 446 | "op id=mv kind=matvec engine=cpu M=64 N=16 core=0 arm=0 prov=fitted src=rx_costmodel\n"); |
| 447 | CHECK(ds_parse_text(text, &m, err, sizeof err) == DS_OK, "parse: %s", err); |
| 448 | ds_opts o; memset(&o, 0, sizeof o); |
| 449 | o.asof_day = ds_day_from_date("2026-10-20"); o.interval_q = 0.9; |
| 450 | o.costmodel = &cm; |
| 451 | |
| 452 | g_predict_fault = 1; |
| 453 | int rc = ds_eval(&m, &o, &r); |
| 454 | g_predict_fault = 0; |
| 455 | CHECK(rc == DS_REFUSED_UNKNOWN && r.makespan_ns == 0.0, |
| 456 | "predict_unsupported_despite_known: got %s makespan %.3f (%s)", |
| 457 | ds_status_name(rc), r.makespan_ns, r.reason); |
| 458 | |
| 459 | g_predict_fault = 2; |
| 460 | rc = ds_eval(&m, &o, &r); |
| 461 | g_predict_fault = 0; |
| 462 | CHECK(rc == DS_REFUSED_UNKNOWN && r.makespan_ns == 0.0, |
| 463 | "predict_refused_despite_known: got %s makespan %.3f (%s)", |
| 464 | ds_status_name(rc), r.makespan_ns, r.reason); |
| 465 | } |
| 466 | |
| 467 | static void test_counterfactual(void) { |
| 468 | ds_model m; ds_result r; |
| 469 | int rc = eval_text( |
| 470 | "resource id=cpu kind=compute slots=2 prov=specified src=test\n" |
| 471 | "resource id=pipe kind=bandwidth cap=1e9 prov=measured src=t\n" |
| 472 | "op id=A kind=copy engine=cpu bytes=1e9 bw=pipe prov=measured src=t\n" |
| 473 | "op id=B kind=copy engine=cpu bytes=1e9 bw=pipe prov=measured src=t\n", &m, &r, NULL); |
| 474 | CHECK(rc == DS_OK, "cf status"); |
| 475 | ds_opts o; memset(&o, 0, sizeof o); o.asof_day = ds_day_from_date("2026-10-20"); o.interval_q = 0.9; |
| 476 | double d = ds_counterfactual(&m, &o, ds_find_res(&m, "pipe")); |
| 477 | CHECK(NEAR(d, 1e9, 1e-3), "doubling the pipe halves the bandwidth-bound DAG: %.0f", d); |
| 478 | d = ds_counterfactual(&m, &o, ds_find_res(&m, "cpu")); |
| 479 | CHECK(NEAR(d, 2e9, 1e-3), "doubling slots changes nothing when the pipe binds: %.0f", d); |
| 480 | } |
| 481 | |
| 482 | static void test_minimum_work_diff(void) { |
| 483 | ds_model base, tr; ds_diff d; char text[8192], err[256]; |
| 484 | const char *base_body = |
| 485 | "resource id=cpu kind=compute slots=1 prov=specified src=test\n" |
| 486 | "op id=A kind=compute engine=cpu t_ns=10 prov=measured src=t\n" |
| 487 | "op id=B kind=compute engine=cpu t_ns=20 prov=measured src=t\n" |
| 488 | "op id=C kind=compute engine=cpu t_ns=30 prov=measured src=t\n" |
| 489 | "dep A B\ndep B C\n"; |
| 490 | snprintf(text, sizeof text, "%s%s", HDR, base_body); |
| 491 | CHECK(ds_parse_text(text, &base, err, sizeof err) == DS_OK, "base: %s", err); |
| 492 | ds_opts o; memset(&o, 0, sizeof o); o.asof_day = ds_day_from_date("2026-10-20"); o.interval_q = 0.9; |
| 493 | /* no change: identical transformed DAG adds no nodes and removes none */ |
| 494 | CHECK(ds_parse_text(text, &tr, err, sizeof err) == DS_OK, "tr: %s", err); |
| 495 | CHECK(ds_diff_models(&base, &tr, &o, &d) == DS_OK && d.n_removed == 0 && d.n_added == 0 && d.n_kept == 3, "no-change fixture: removed %d added %d kept %d", d.n_removed, d.n_added, d.n_kept); |
| 496 | /* B reused from a lookup: valid, 20 ns of work moves to the reused ledger */ |
| 497 | snprintf(text, sizeof text, "%s%s", HDR, |
| 498 | "resource id=cpu kind=compute slots=1 prov=specified src=test\n" |
| 499 | "op id=A kind=compute engine=cpu t_ns=10 prov=measured src=t\n" |
| 500 | "op id=B_lookup kind=compute engine=cpu t_ns=2 prov=measured src=t\n" |
| 501 | "op id=C kind=compute engine=cpu t_ns=30 prov=measured src=t\n" |
| 502 | "dep A B_lookup\ndep B_lookup C\n" |
| 503 | "removed op=B reason=reused ref=B_lookup proof=rx_plan:digest\n"); |
| 504 | CHECK(ds_parse_text(text, &tr, err, sizeof err) == DS_OK, "tr2: %s", err); |
| 505 | int rc = ds_diff_models(&base, &tr, &o, &d); |
| 506 | CHECK(rc == DS_OK && NEAR(d.removed_ns[DS_RM_REUSED], 20, 1e-9) && d.n_added == 1, "reuse accounted: %s rc=%s", d.reason, ds_status_name(rc)); |
| 507 | /* eliminated with no proof while C still consumed it: required work cannot vanish by assertion */ |
| 508 | snprintf(text, sizeof text, "%s%s", HDR, |
| 509 | "resource id=cpu kind=compute slots=1 prov=specified src=test\n" |
| 510 | "op id=A kind=compute engine=cpu t_ns=10 prov=measured src=t\n" |
| 511 | "op id=C kind=compute engine=cpu t_ns=30 prov=measured src=t\n" |
| 512 | "dep A C\n" |
| 513 | "removed op=B reason=eliminated\n"); |
| 514 | CHECK(ds_parse_text(text, &tr, err, sizeof err) == DS_OK, "tr3: %s", err); |
| 515 | rc = ds_diff_models(&base, &tr, &o, &d); |
| 516 | CHECK(rc == DS_REFUSED_REMOVED_REQUIRED && d.required_removed, "unproven elimination of consumed work refuses, got %s", ds_status_name(rc)); |
| 517 | /* unchanged_dep on A, but A changed: the consumer is invalidated */ |
| 518 | snprintf(text, sizeof text, "%s%s", HDR, |
| 519 | "resource id=cpu kind=compute slots=1 prov=specified src=test\n" |
| 520 | "op id=A kind=compute engine=cpu t_ns=10 prov=measured src=t\n" |
| 521 | "op id=C kind=compute engine=cpu t_ns=30 prov=measured src=t\n" |
| 522 | "dep A C\n" |
| 523 | "removed op=B reason=unchanged_dep ref=A\n"); |
| 524 | CHECK(ds_parse_text(text, &tr, err, sizeof err) == DS_OK, "tr4: %s", err); |
| 525 | rc = ds_diff_models(&base, &tr, &o, &d); |
| 526 | CHECK(rc == DS_OK && NEAR(d.removed_ns[DS_RM_UNCHANGED_DEP], 20, 1e-9), "unchanged dependency allows skipping B"); |
| 527 | o.changed = "A"; |
| 528 | rc = ds_diff_models(&base, &tr, &o, &d); |
| 529 | CHECK(rc == DS_REFUSED_REMOVED_REQUIRED, "changed A invalidates the skip of B, got %s", ds_status_name(rc)); |
| 530 | o.changed = NULL; |
| 531 | /* shared work counted twice is detected */ |
| 532 | snprintf(text, sizeof text, "%s%s", HDR, |
| 533 | "resource id=cpu kind=compute slots=1 prov=specified src=test\n" |
| 534 | "op id=A kind=compute engine=cpu t_ns=10 prov=measured src=t\n" |
| 535 | "op id=C kind=compute engine=cpu t_ns=30 prov=measured src=t\n" |
| 536 | "dep A C\n" |
| 537 | "removed op=B reason=shared ref=A\n" |
| 538 | "removed op=B reason=shared ref=C\n"); |
| 539 | CHECK(ds_parse_text(text, &tr, err, sizeof err) == DS_OK, "tr5: %s", err); |
| 540 | rc = ds_diff_models(&base, &tr, &o, &d); |
| 541 | CHECK(rc != DS_OK && d.double_counted, "double-counted shared work detected"); |
| 542 | } |
| 543 | |
| 544 | static int eval_file(const char *path, ds_model *m, ds_result *r) { |
| 545 | char err[256]; |
| 546 | int rc = ds_parse_file(path, m, err, sizeof err); |
| 547 | if (rc != DS_OK) { |
| 548 | memset(r, 0, sizeof *r); |
| 549 | r->status = rc; |
| 550 | snprintf(r->reason, sizeof r->reason, "%s", err); |
| 551 | return rc; |
| 552 | } |
| 553 | ds_opts def; memset(&def, 0, sizeof def); |
| 554 | def.asof_day = ds_day_from_date("2026-10-24"); |
| 555 | def.interval_q = 0.9; |
| 556 | return ds_eval(m, &def, r); |
| 557 | } |
| 558 | |
| 559 | static double peak_of(const ds_model *m, const ds_result *r, const char *cap) { |
| 560 | int i = ds_find_res(m, cap); |
| 561 | return i < 0 ? -1.0 : r->peak_live_bytes[i]; |
| 562 | } |
| 563 | |
| 564 | /* Issue #705. An inverted lifetime must not contribute negative live bytes. */ |
| 565 | static void test_inverted_lifetimes(void) { |
| 566 | ds_model m; ds_result r, r2; |
| 567 | int rc = eval_file("tests/dagsim/fixtures/inverted_masks_overflow.oev", &m, &r); |
| 568 | CHECK(rc == DS_REFUSED_LIFETIME |
| 569 | && strstr(r.reason, "inverted lifetime for buffer inverted:") |
| 570 | && strstr(r.reason, "first=C") && strstr(r.reason, "start_ns=20") |
| 571 | && strstr(r.reason, "last=A") && strstr(r.reason, "end_ns=10") |
| 572 | && !strstr(r.reason, "went negative"), |
| 573 | "inverted_lifetime_masks_overflow: got %s (%s)", ds_status_name(rc), r.reason); |
| 574 | CHECK(r.makespan_ns == 0 && r.peak_live_bytes[0] == 0, "refusal carries no capacity certificate"); |
| 575 | rc = eval_file("tests/dagsim/fixtures/inverted_masks_overflow.oev", &m, &r2); |
| 576 | CHECK(rc == r2.status && strcmp(r.reason, r2.reason) == 0, "rejection text is deterministic"); |
| 577 | |
| 578 | rc = eval_file("tests/dagsim/fixtures/inverted_lone.oev", &m, &r); |
| 579 | CHECK(rc == DS_REFUSED_LIFETIME && strstr(r.reason, "first=C") && strstr(r.reason, "last=A"), |
| 580 | "lone inverted lifetime refused, got %s (%s)", ds_status_name(rc), r.reason); |
| 581 | |
| 582 | rc = eval_file("tests/dagsim/fixtures/capacity_two_80.oev", &m, &r); |
| 583 | CHECK(rc == DS_REFUSED_CAPACITY && strstr(r.reason, "real1") && strstr(r.reason, "real2"), |
| 584 | "two valid 80 B buffers refuse capacity, got %s (%s)", ds_status_name(rc), r.reason); |
| 585 | |
| 586 | rc = eval_file("tests/dagsim/fixtures/valid_disjoint_80.oev", &m, &r); |
| 587 | CHECK(rc == DS_OK, "disjoint 80 B pair fits, got %s (%s)", ds_status_name(rc), r.reason); |
| 588 | CHECK(NEAR(peak_of(&m, &r, "mem"), 80, 1e-9), "disjoint peak 80, got %.1f", peak_of(&m, &r, "mem")); |
| 589 | CHECK(NEAR(r.makespan_ns, 30, 1e-9), "disjoint makespan 30, got %.1f", r.makespan_ns); |
| 590 | |
| 591 | rc = eval_file("tests/dagsim/fixtures/valid_tie_80.oev", &m, &r); |
| 592 | CHECK(rc == DS_OK, "completion/allocation tie fits, got %s (%s)", ds_status_name(rc), r.reason); |
| 593 | CHECK(NEAR(peak_of(&m, &r, "mem"), 80, 1e-9), "tie peak 80 (not 160), got %.1f", peak_of(&m, &r, "mem")); |
| 594 | |
| 595 | /* zero-duration: empty [start, end), peak 0, and it cannot hide an overlap */ |
| 596 | rc = eval_text( |
| 597 | "resource id=cpu kind=compute slots=1 prov=specified src=test\n" |
| 598 | "op id=Z kind=compute engine=cpu t_ns=0 prov=measured src=t\n" |
| 599 | "buffer id=z bytes=80 cap=mem first=Z last=Z\n", &m, &r, NULL); |
| 600 | CHECK(rc == DS_OK, "zero-duration buffer status %s (%s)", ds_status_name(rc), r.reason); |
| 601 | CHECK(NEAR(r.peak_live_bytes[0], 0, 1e-9), "zero-duration peak 0, got %.1f", r.peak_live_bytes[0]); |
| 602 | rc = eval_text( |
| 603 | "resource id=cpu kind=compute slots=1 prov=specified src=test\n" |
| 604 | "op id=Z kind=compute engine=cpu t_ns=0 prov=measured src=t\n" |
| 605 | "op id=B kind=compute engine=cpu t_ns=10 prov=measured src=t\n" |
| 606 | "buffer id=z bytes=400 cap=mem first=Z last=Z\n" |
| 607 | "buffer id=real1 bytes=600 cap=mem first=B last=B\n" |
| 608 | "buffer id=real2 bytes=600 cap=mem first=B last=B\n", &m, &r, NULL); |
| 609 | CHECK(rc == DS_REFUSED_CAPACITY, "zero-duration must not hide 1200 B, got %s (%s)", ds_status_name(rc), r.reason); |
| 610 | |
| 611 | /* omitted endpoints stay valid: resident from 0, open end, and both omitted */ |
| 612 | rc = eval_text( |
| 613 | "resource id=cpu kind=compute slots=1 prov=specified src=test\n" |
| 614 | "op id=A kind=compute engine=cpu t_ns=10 prov=measured src=t\n" |
| 615 | "buffer id=no_first bytes=100 cap=mem last=A\n" |
| 616 | "buffer id=no_last bytes=50 cap=mem first=A\n" |
| 617 | "buffer id=resident bytes=25 cap=mem\n", &m, &r, NULL); |
| 618 | CHECK(rc == DS_OK, "omitted endpoints status %s (%s)", ds_status_name(rc), r.reason); |
| 619 | CHECK(NEAR(r.peak_live_bytes[0], 175, 1e-9), "omitted endpoints peak 175, got %.1f", r.peak_live_bytes[0]); |
| 620 | |
| 621 | /* The went-negative refusal is reachable without an inverted window: the |
| 622 | * ledger sums doubles, and 1e20 + 1 rounds to 1e20 (ulp 16384). Events |
| 623 | * +1e20@0, +1@10, -1e20@20, -1@30 end the sweep at -1. A certificate |
| 624 | * built on that ledger would be wrong, so it is refused. */ |
| 625 | rc = eval_text( |
| 626 | "resource id=cpu kind=compute slots=1 prov=specified src=test\n" |
| 627 | "resource id=hbm kind=capacity cap=1e30 prov=specified src=test\n" |
| 628 | "op id=A kind=compute engine=cpu t_ns=10 prov=measured src=t\n" |
| 629 | "op id=B kind=compute engine=cpu t_ns=10 prov=measured src=t\n" |
| 630 | "op id=C kind=compute engine=cpu t_ns=10 prov=measured src=t\n" |
| 631 | "dep A B\ndep B C\n" |
| 632 | "buffer id=huge bytes=1e20 cap=hbm first=A last=B\n" |
| 633 | "buffer id=tiny bytes=1 cap=hbm first=B last=C\n", &m, &r, NULL); |
| 634 | CHECK(rc == DS_REFUSED_LIFETIME && strstr(r.reason, "went negative"), |
| 635 | "rounding_drives_live_negative: got %s (%s)", ds_status_name(rc), r.reason); |
| 636 | CHECK(peak_of(&m, &r, "hbm") == 0 && r.makespan_ns == 0, |
| 637 | "negative-ledger refusal carries no capacity certificate"); |
| 638 | |
| 639 | char text[2048], err[256]; |
| 640 | snprintf(text, sizeof text, "%s%s", HDR, "buffer id=neg bytes=-5 cap=mem\n"); |
| 641 | CHECK(ds_parse_text(text, &m, err, sizeof err) == DS_PARSE_ERROR && strstr(err, "finite number >= 0"), |
| 642 | "negative bytes refused at parse: %s", err); |
| 643 | snprintf(text, sizeof text, "%s%s", HDR, "buffer id=inf bytes=inf cap=mem\n"); |
| 644 | CHECK(ds_parse_text(text, &m, err, sizeof err) == DS_PARSE_ERROR && strstr(err, "finite number >= 0"), |
| 645 | "non-finite bytes refused at parse: %s", err); |
| 646 | snprintf(text, sizeof text, "%s%s", HDR, |
| 647 | "resource id=cpu kind=compute slots=1 prov=specified src=test\n" |
| 648 | "op id=A kind=compute engine=cpu t_ns=10 prov=measured src=t\n" |
| 649 | "buffer id=ghost bytes=10 cap=mem first=nope last=A\n"); |
| 650 | CHECK(ds_parse_text(text, &m, err, sizeof err) == DS_PARSE_ERROR && strstr(err, "not an op"), |
| 651 | "unknown endpoint refused at parse: %s", err); |
| 652 | } |
| 653 | |
| 654 | int main(void) { |
| 655 | test_serial_chain(); |
| 656 | test_perfect_overlap(); |
| 657 | test_shared_bandwidth(); |
| 658 | test_capacity_refusal(); |
| 659 | test_lifetimes(); |
| 660 | test_sync(); |
| 661 | test_launch(); |
| 662 | test_calibration(); |
| 663 | test_intervals(); |
| 664 | test_energy(); |
| 665 | test_parse_and_cycles(); |
| 666 | test_costmodel_reuse(); |
| 667 | test_predict_refusal(); |
| 668 | test_counterfactual(); |
| 669 | test_minimum_work_diff(); |
| 670 | test_inverted_lifetimes(); |
| 671 | printf("dagsim_unit: %d checks, %d failed\n", g_checks, g_fail); |
| 672 | return g_fail ? 1 : 0; |
| 673 | } |