omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / src / runtime / rx_capq.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* |
| 18 | * rx_capq.c -- Omega capability query IR. See rx_capq.h. |
| 19 | * |
| 20 | * The catalog is indexed by (operation, source). A query compiles to the |
| 21 | * buckets of its operation and specializations, in the sources that may |
| 22 | * realize it; it never walks the rest of the catalog and never reads a |
| 23 | * description. What it returns is a bounded array of fixed-size records. |
| 24 | */ |
| 25 | #include "rx_capq.h" |
| 26 | |
| 27 | #include "omega_native.h" |
| 28 | #include "sha256.h" |
| 29 | |
| 30 | #include <stdlib.h> |
| 31 | #include <string.h> |
| 32 | |
| 33 | /* ---- small open-addressing maps (key 0 = empty) ---- */ |
| 34 | |
| 35 | static uint32_t pow2_at_least(uint32_t n) { |
| 36 | uint32_t p = 16; |
| 37 | while (p < n) p <<= 1; |
| 38 | return p; |
| 39 | } |
| 40 | |
| 41 | static uint32_t hslot(uint32_t key, uint32_t mask) { return (key * 0x9E3779B1u) & mask; } |
| 42 | |
| 43 | static int map_put(uint32_t *keys, uint32_t mask, uint32_t *count, uint32_t key, uint32_t *slot) { |
| 44 | if (key == 0) return CQ_E_ARG; |
| 45 | uint32_t i = hslot(key, mask); |
| 46 | for (uint32_t probe = 0; probe <= mask; probe++, i = (i + 1) & mask) { |
| 47 | if (keys[i] == key) { *slot = i; return CQ_OK; } |
| 48 | if (keys[i] == 0) { |
| 49 | if ((*count + 1) * 2 > mask + 1) return CQ_E_NOMEM; /* keep load <= 1/2 */ |
| 50 | keys[i] = key; |
| 51 | (*count)++; |
| 52 | *slot = i; |
| 53 | return CQ_OK; |
| 54 | } |
| 55 | } |
| 56 | return CQ_E_NOMEM; |
| 57 | } |
| 58 | |
| 59 | static int map_get(const uint32_t *keys, uint32_t mask, uint32_t key, uint32_t *slot) { |
| 60 | if (key == 0 || !keys) return 0; |
| 61 | uint32_t i = hslot(key, mask); |
| 62 | for (uint32_t probe = 0; probe <= mask; probe++, i = (i + 1) & mask) { |
| 63 | if (keys[i] == key) { *slot = i; return 1; } |
| 64 | if (keys[i] == 0) return 0; |
| 65 | } |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | /* ---- catalog ---- */ |
| 70 | |
| 71 | int cq_catalog_init(CqCatalog *c, uint32_t self_machine, uint32_t alias_capacity, |
| 72 | uint32_t machine_capacity) { |
| 73 | if (!c || self_machine == 0) return CQ_E_ARG; |
| 74 | memset(c, 0, sizeof *c); |
| 75 | c->self_machine = self_machine; |
| 76 | uint32_t an = pow2_at_least(alias_capacity * 2 + 2), mn = pow2_at_least(machine_capacity * 2 + 2); |
| 77 | c->alias_mask = an - 1; |
| 78 | c->mach_mask = mn - 1; |
| 79 | c->alias_key = calloc(an, sizeof *c->alias_key); |
| 80 | c->alias_op = calloc(an, sizeof *c->alias_op); |
| 81 | c->mach_key = calloc(mn, sizeof *c->mach_key); |
| 82 | c->mach_lease = calloc(mn, sizeof *c->mach_lease); |
| 83 | c->cap_ops = 64; |
| 84 | c->ops = calloc(c->cap_ops + 1, sizeof *c->ops); |
| 85 | c->key_mask = 255; |
| 86 | c->key_slot = calloc(c->key_mask + 1, sizeof *c->key_slot); |
| 87 | if (!c->alias_key || !c->alias_op || !c->mach_key || !c->mach_lease || !c->ops || !c->key_slot) { |
| 88 | cq_catalog_free(c); |
| 89 | return CQ_E_NOMEM; |
| 90 | } |
| 91 | return CQ_OK; |
| 92 | } |
| 93 | |
| 94 | int cq_catalog_init_canonical(CqCatalog *c, AienMachineIndex *machines, const AienMachineId *self, |
| 95 | uint32_t alias_capacity, uint32_t machine_capacity) { |
| 96 | if (!c || !machines || !self) return CQ_E_ARG; |
| 97 | uint32_t idx = aien_mid_index_bind(machines, self); |
| 98 | if (idx == 0) return CQ_E_NO_IDENTITY; |
| 99 | int rc = cq_catalog_init(c, idx, alias_capacity, machine_capacity); |
| 100 | if (rc == CQ_OK) c->machines = machines; |
| 101 | return rc; |
| 102 | } |
| 103 | |
| 104 | int cq_machine_identity(const CqCatalog *c, uint32_t machine, AienMachineId *out) { |
| 105 | if (!c || !out) return CQ_E_ARG; |
| 106 | if (!c->machines || aien_mid_index_get(c->machines, machine, out) != AIEN_MID_OK) |
| 107 | return CQ_E_NO_IDENTITY; |
| 108 | return CQ_OK; |
| 109 | } |
| 110 | |
| 111 | uint32_t cq_machine_index(const CqCatalog *c, const AienMachineId *m) { |
| 112 | return c && c->machines && m ? aien_mid_index_find(c->machines, m) : 0; |
| 113 | } |
| 114 | |
| 115 | void cq_catalog_free(CqCatalog *c) { |
| 116 | if (!c) return; |
| 117 | free(c->key_slot); |
| 118 | free(c->alias_key); |
| 119 | free(c->alias_op); |
| 120 | free(c->mach_key); |
| 121 | free(c->mach_lease); |
| 122 | free(c->ops); |
| 123 | free(c->e); |
| 124 | free(c->desc); |
| 125 | free(c->bucket_start); |
| 126 | free(c->bucket); |
| 127 | memset(c, 0, sizeof *c); |
| 128 | } |
| 129 | |
| 130 | int cq_op_define(CqCatalog *c, uint32_t op, uint32_t parent, uint32_t domains) { |
| 131 | if (!c || op != c->n_ops + 1 || parent >= op || (domains & ~CQ_SRC_ALL)) return CQ_E_ARG; |
| 132 | if (op > c->cap_ops) { |
| 133 | uint32_t nc = c->cap_ops * 2; |
| 134 | CqOp *n = realloc(c->ops, (size_t)(nc + 1) * sizeof *n); |
| 135 | if (!n) return CQ_E_NOMEM; |
| 136 | memset(n + c->cap_ops + 1, 0, (size_t)(nc - c->cap_ops) * sizeof *n); |
| 137 | c->ops = n; |
| 138 | c->cap_ops = nc; |
| 139 | } |
| 140 | c->ops[op] = (CqOp){ parent, domains, 0, 0 }; |
| 141 | if (parent) { |
| 142 | c->ops[op].next_sibling = c->ops[parent].first_child; |
| 143 | c->ops[parent].first_child = op; |
| 144 | } |
| 145 | c->n_ops = op; |
| 146 | c->built = 0; |
| 147 | return CQ_OK; |
| 148 | } |
| 149 | |
| 150 | int cq_alias(CqCatalog *c, uint32_t alias, uint32_t op) { |
| 151 | if (!c || op == 0 || op > c->n_ops || alias == op) return CQ_E_ARG; |
| 152 | uint32_t s; |
| 153 | int rc = map_put(c->alias_key, c->alias_mask, &c->n_alias, alias, &s); |
| 154 | if (rc != CQ_OK) return rc; |
| 155 | c->alias_op[s] = op; |
| 156 | return CQ_OK; |
| 157 | } |
| 158 | |
| 159 | int cq_machine_advertise(CqCatalog *c, uint32_t machine, uint64_t lease_until_us) { |
| 160 | if (!c || machine == 0 || machine == c->self_machine) return CQ_E_ARG; |
| 161 | uint32_t s; |
| 162 | int rc = map_put(c->mach_key, c->mach_mask, &c->n_mach, machine, &s); |
| 163 | if (rc != CQ_OK) return rc; |
| 164 | c->mach_lease[s] = lease_until_us; |
| 165 | return CQ_OK; |
| 166 | } |
| 167 | |
| 168 | int cq_machine_advertise_id(CqCatalog *c, const AienMachineId *m, uint64_t lease_until_us, |
| 169 | uint32_t *index_out) { |
| 170 | if (!c || !m) return CQ_E_ARG; |
| 171 | if (!c->machines) return CQ_E_NO_IDENTITY; |
| 172 | uint32_t idx = aien_mid_index_bind(c->machines, m); |
| 173 | if (idx == 0) return CQ_E_NO_IDENTITY; |
| 174 | int rc = cq_machine_advertise(c, idx, lease_until_us); |
| 175 | if (rc == CQ_OK && index_out) *index_out = idx; |
| 176 | return rc; |
| 177 | } |
| 178 | |
| 179 | /* ---- entry keys ---- */ |
| 180 | |
| 181 | CqKey cq_key_of(const CqEntry *e) { |
| 182 | return (CqKey){ e->capability_id, e->realization_id, e->machine_id, e->skill_id }; |
| 183 | } |
| 184 | |
| 185 | CqKey cq_key_of_candidate(const CqCandidate *x) { |
| 186 | return (CqKey){ x->capability_id, x->realization_id, x->machine_id, x->skill_id }; |
| 187 | } |
| 188 | |
| 189 | static uint32_t key_hash(const CqKey *k) { |
| 190 | uint64_t res = 0; |
| 191 | (void)omega2_rx_cq_key_hash(&res, (uint64_t)k->capability_id, (uint64_t)k->realization_id, |
| 192 | (uint64_t)k->machine_id, (uint64_t)k->skill_id); |
| 193 | return (uint32_t)res; |
| 194 | } |
| 195 | |
| 196 | static int key_eq(const CqEntry *e, const CqKey *k) { |
| 197 | return e->capability_id == k->capability_id && e->realization_id == k->realization_id && |
| 198 | e->machine_id == k->machine_id && e->skill_id == k->skill_id; |
| 199 | } |
| 200 | |
| 201 | /* Slot holding k, or the empty slot where it would go. */ |
| 202 | static uint32_t key_find(const CqCatalog *c, const CqKey *k) { |
| 203 | uint32_t i = key_hash(k) & c->key_mask; |
| 204 | while (c->key_slot[i] && !key_eq(&c->e[c->key_slot[i] - 1], k)) i = (i + 1) & c->key_mask; |
| 205 | return i; |
| 206 | } |
| 207 | |
| 208 | static int key_grow(CqCatalog *c) { |
| 209 | uint32_t nm = (c->key_mask + 1) * 2 - 1; |
| 210 | uint32_t *ns = calloc((size_t)nm + 1, sizeof *ns); |
| 211 | if (!ns) return CQ_E_NOMEM; |
| 212 | free(c->key_slot); |
| 213 | c->key_slot = ns; |
| 214 | c->key_mask = nm; |
| 215 | for (uint32_t i = 0; i < c->n; i++) { |
| 216 | CqKey k = cq_key_of(&c->e[i]); |
| 217 | c->key_slot[key_find(c, &k)] = i + 1; |
| 218 | } |
| 219 | return CQ_OK; |
| 220 | } |
| 221 | |
| 222 | const CqEntry *cq_lookup(const CqCatalog *c, const CqKey *k) { |
| 223 | if (!c || !k || !c->key_slot) return NULL; |
| 224 | uint32_t s = c->key_slot[key_find(c, k)]; |
| 225 | return s ? &c->e[s - 1] : NULL; |
| 226 | } |
| 227 | |
| 228 | int cq_set_availability(CqCatalog *c, const CqKey *k, uint32_t live) { |
| 229 | if (!c || !k || (live != CQ_LIVE_AVAILABLE && live != CQ_LIVE_UNAVAILABLE)) return CQ_E_ARG; |
| 230 | uint32_t s = c->key_slot[key_find(c, k)]; |
| 231 | if (!s) return CQ_E_NOT_FOUND; |
| 232 | if (c->e[s - 1].live == CQ_LIVE_WITHDRAWN) return CQ_E_WITHDRAWN; |
| 233 | c->e[s - 1].live = live; /* same (op, source) bucket: the index stays valid */ |
| 234 | return CQ_OK; |
| 235 | } |
| 236 | |
| 237 | int cq_withdraw(CqCatalog *c, const CqKey *k) { |
| 238 | if (!c || !k) return CQ_E_ARG; |
| 239 | uint32_t s = c->key_slot[key_find(c, k)]; |
| 240 | if (!s) return CQ_E_NOT_FOUND; |
| 241 | c->e[s - 1].live = CQ_LIVE_WITHDRAWN; |
| 242 | return CQ_OK; |
| 243 | } |
| 244 | |
| 245 | int cq_register(CqCatalog *c, const CqEntry *e, const char *desc, uint32_t desc_len) { |
| 246 | if (!c || !e || e->op == 0 || e->op > c->n_ops || e->source >= CQ_SOURCES || |
| 247 | e->machine_id == 0 || (desc_len && !desc) || e->live > CQ_LIVE_UNAVAILABLE) |
| 248 | return CQ_E_ARG; |
| 249 | if (!(c->ops[e->op].domains & CQ_SRC(e->source))) return CQ_E_ARG; |
| 250 | /* Only the Fabric speaks for another machine; the Fabric only for another machine. */ |
| 251 | if ((e->source == CQ_SRC_FABRIC) != (e->machine_id != c->self_machine)) return CQ_E_ARG; |
| 252 | CqKey k = cq_key_of(e); |
| 253 | uint32_t ks = key_find(c, &k); |
| 254 | if (c->key_slot[ks]) { |
| 255 | /* Update in place. */ |
| 256 | CqEntry *old = &c->e[c->key_slot[ks] - 1]; |
| 257 | uint64_t off = old->desc_off; |
| 258 | uint32_t len = old->desc_len; |
| 259 | int moved = old->op != e->op || old->source != e->source; |
| 260 | if (desc_len) { |
| 261 | if (c->desc_bytes + desc_len > c->desc_cap) { |
| 262 | uint64_t nc = c->desc_cap ? c->desc_cap : 65536; |
| 263 | while (nc < c->desc_bytes + desc_len) nc *= 2; |
| 264 | char *n = realloc(c->desc, nc); |
| 265 | if (!n) return CQ_E_NOMEM; |
| 266 | c->desc = n; |
| 267 | c->desc_cap = nc; |
| 268 | } |
| 269 | memcpy(c->desc + c->desc_bytes, desc, desc_len); |
| 270 | off = c->desc_bytes; |
| 271 | len = desc_len; |
| 272 | c->desc_bytes += desc_len; /* the old text stays as dead bytes */ |
| 273 | } |
| 274 | *old = *e; |
| 275 | old->desc_off = off; |
| 276 | old->desc_len = len; |
| 277 | if (moved) c->built = 0; |
| 278 | return CQ_OK; |
| 279 | } |
| 280 | if ((c->n + 1) * 2 > c->key_mask + 1) { |
| 281 | if (key_grow(c) != CQ_OK) return CQ_E_NOMEM; |
| 282 | ks = key_find(c, &k); |
| 283 | } |
| 284 | if (c->n == c->cap) { |
| 285 | uint32_t nc = c->cap ? c->cap * 2 : 256; |
| 286 | CqEntry *n = realloc(c->e, (size_t)nc * sizeof *n); |
| 287 | if (!n) return CQ_E_NOMEM; |
| 288 | c->e = n; |
| 289 | c->cap = nc; |
| 290 | } |
| 291 | if (c->desc_bytes + desc_len > c->desc_cap) { |
| 292 | uint64_t nc = c->desc_cap ? c->desc_cap : 65536; |
| 293 | while (nc < c->desc_bytes + desc_len) nc *= 2; |
| 294 | char *n = realloc(c->desc, nc); |
| 295 | if (!n) return CQ_E_NOMEM; |
| 296 | c->desc = n; |
| 297 | c->desc_cap = nc; |
| 298 | } |
| 299 | CqEntry x = *e; |
| 300 | x.desc_off = c->desc_bytes; |
| 301 | x.desc_len = desc_len; |
| 302 | if (desc_len) memcpy(c->desc + c->desc_bytes, desc, desc_len); |
| 303 | c->desc_bytes += desc_len; |
| 304 | c->e[c->n++] = x; |
| 305 | c->key_slot[ks] = c->n; |
| 306 | c->built = 0; |
| 307 | return CQ_OK; |
| 308 | } |
| 309 | |
| 310 | static uint32_t bucket_key(uint32_t op, uint32_t source) { return op * CQ_SOURCES + source; } |
| 311 | |
| 312 | int cq_catalog_build(CqCatalog *c) { |
| 313 | if (!c) return CQ_E_ARG; |
| 314 | free(c->bucket_start); |
| 315 | free(c->bucket); |
| 316 | uint32_t nb = (c->n_ops + 1) * CQ_SOURCES; |
| 317 | c->bucket_start = calloc((size_t)nb + 1, sizeof *c->bucket_start); |
| 318 | c->bucket = malloc((size_t)(c->n ? c->n : 1) * sizeof *c->bucket); |
| 319 | if (!c->bucket_start || !c->bucket) return CQ_E_NOMEM; |
| 320 | for (uint32_t i = 0; i < c->n; i++) c->bucket_start[bucket_key(c->e[i].op, c->e[i].source) + 1]++; |
| 321 | for (uint32_t b = 0; b < nb; b++) c->bucket_start[b + 1] += c->bucket_start[b]; |
| 322 | uint32_t *fill = malloc((size_t)nb * sizeof *fill); |
| 323 | if (!fill) return CQ_E_NOMEM; |
| 324 | memcpy(fill, c->bucket_start, (size_t)nb * sizeof *fill); |
| 325 | for (uint32_t i = 0; i < c->n; i++) c->bucket[fill[bucket_key(c->e[i].op, c->e[i].source)]++] = i; |
| 326 | free(fill); |
| 327 | c->built = 1; |
| 328 | return CQ_OK; |
| 329 | } |
| 330 | |
| 331 | uint32_t cq_resolve(const CqCatalog *c, uint32_t name) { |
| 332 | if (!c || name == 0) return 0; |
| 333 | uint32_t s; |
| 334 | if (map_get(c->alias_key, c->alias_mask, name, &s)) return c->alias_op[s]; |
| 335 | return name <= c->n_ops ? name : 0; |
| 336 | } |
| 337 | |
| 338 | uint64_t cq_catalog_bytes(const CqCatalog *c) { |
| 339 | uint64_t b = (uint64_t)c->cap * sizeof(CqEntry) + (uint64_t)(c->cap_ops + 1) * sizeof(CqOp) + |
| 340 | (uint64_t)(c->alias_mask + 1) * 8u + (uint64_t)(c->mach_mask + 1) * 12u + |
| 341 | (uint64_t)(c->key_mask + 1) * 4u + c->desc_cap; |
| 342 | if (c->built) b += (uint64_t)((c->n_ops + 1) * CQ_SOURCES + 1) * 4u + (uint64_t)c->n * 4u; |
| 343 | return b; |
| 344 | } |
| 345 | |
| 346 | /* ---- compile ---- */ |
| 347 | |
| 348 | static int cmp_u32(const void *a, const void *b) { |
| 349 | uint32_t x = *(const uint32_t *)a, y = *(const uint32_t *)b; |
| 350 | return x < y ? -1 : x > y; |
| 351 | } |
| 352 | |
| 353 | static void put_u32(sha256_ctx *h, uint32_t v) { |
| 354 | uint8_t b[4] = { (uint8_t)v, (uint8_t)(v >> 8), (uint8_t)(v >> 16), (uint8_t)(v >> 24) }; |
| 355 | sha256_update(h, b, 4); |
| 356 | } |
| 357 | |
| 358 | static void put_u64(sha256_ctx *h, uint64_t v) { |
| 359 | put_u32(h, (uint32_t)v); |
| 360 | put_u32(h, (uint32_t)(v >> 32)); |
| 361 | } |
| 362 | |
| 363 | static int cmp_u64_(const void *a, const void *b) { |
| 364 | uint64_t x = *(const uint64_t *)a, y = *(const uint64_t *)b; |
| 365 | return x < y ? -1 : x > y; |
| 366 | } |
| 367 | |
| 368 | void cq_ontology_digest(const CqCatalog *c, uint8_t out[32]) { |
| 369 | sha256_ctx h; |
| 370 | sha256_init(&h); |
| 371 | sha256_update(&h, (const uint8_t *)"OMEGA-CAPQ-ONTOLOGY-1", 21); |
| 372 | put_u32(&h, c->n_ops); |
| 373 | for (uint32_t op = 1; op <= c->n_ops; op++) { |
| 374 | put_u32(&h, c->ops[op].parent); |
| 375 | put_u32(&h, c->ops[op].domains); |
| 376 | } |
| 377 | /* Aliases in key order, not table order. */ |
| 378 | uint64_t *pairs = malloc((size_t)(c->n_alias ? c->n_alias : 1) * sizeof *pairs); |
| 379 | uint32_t np = 0; |
| 380 | for (uint32_t i = 0; pairs && i <= c->alias_mask; i++) |
| 381 | if (c->alias_key[i]) pairs[np++] = (uint64_t)c->alias_key[i] << 32 | c->alias_op[i]; |
| 382 | if (pairs) { |
| 383 | qsort(pairs, np, sizeof *pairs, cmp_u64_); |
| 384 | put_u32(&h, np); |
| 385 | for (uint32_t i = 0; i < np; i++) put_u64(&h, pairs[i]); |
| 386 | } else { |
| 387 | put_u32(&h, UINT32_MAX); /* no memory: a digest no peer can match */ |
| 388 | } |
| 389 | free(pairs); |
| 390 | sha256_final(&h, out); |
| 391 | } |
| 392 | |
| 393 | int cq_compile(const CqCatalog *c, const CqNeed *need, CqPlan *plan) { |
| 394 | if (!plan) return CQ_E_ARG; |
| 395 | memset(plan, 0, sizeof *plan); |
| 396 | if (!c || !need) return plan->verdict = CQ_E_ARG; |
| 397 | uint32_t op = cq_resolve(c, need->semantic_operation); |
| 398 | if (op == 0) return plan->verdict = CQ_E_NO_OP; |
| 399 | plan->op = op; |
| 400 | |
| 401 | /* The operation and every specialization of it (breadth first). */ |
| 402 | plan->ops[plan->n_ops++] = op; |
| 403 | uint32_t domains = 0; |
| 404 | for (uint32_t i = 0; i < plan->n_ops; i++) { |
| 405 | domains |= c->ops[plan->ops[i]].domains; |
| 406 | for (uint32_t ch = c->ops[plan->ops[i]].first_child; ch; ch = c->ops[ch].next_sibling) { |
| 407 | if (plan->n_ops == CQ_MAX_PLAN_OPS) return plan->verdict = CQ_E_PLAN_FULL; |
| 408 | plan->ops[plan->n_ops++] = ch; |
| 409 | } |
| 410 | } |
| 411 | qsort(plan->ops, plan->n_ops, sizeof plan->ops[0], cmp_u32); |
| 412 | |
| 413 | /* Sources: computed in pure OM-2 (om/runtime/rx_capq_core.om). */ |
| 414 | uint32_t loc = need->locality_constraints.allowed ? need->locality_constraints.allowed |
| 415 | : (CQ_LOC_LOCAL | CQ_LOC_FABRIC); |
| 416 | uint32_t pin = need->locality_constraints.machine; |
| 417 | uint64_t s64 = 0; |
| 418 | (void)omega2_rx_cq_plan_sources(&s64, (uint64_t)domains, (uint64_t)need->sources, |
| 419 | (uint64_t)loc, (uint64_t)pin, (uint64_t)c->self_machine); |
| 420 | uint32_t s = (uint32_t)s64; |
| 421 | plan->sources = s; |
| 422 | plan->n_probes = plan->n_ops * (uint32_t)__builtin_popcount(s); |
| 423 | |
| 424 | /* Identity: the resolved operation, not the name it was asked by. */ |
| 425 | sha256_ctx h; |
| 426 | sha256_init(&h); |
| 427 | sha256_update(&h, (const uint8_t *)"OMEGA-CAPQ-PLAN-1", 17); |
| 428 | put_u32(&h, plan->op); |
| 429 | put_u32(&h, plan->n_ops); |
| 430 | for (uint32_t i = 0; i < plan->n_ops; i++) put_u32(&h, plan->ops[i]); |
| 431 | put_u32(&h, plan->sources); |
| 432 | put_u64(&h, need->accepted_input_types); |
| 433 | put_u64(&h, need->required_output_types); |
| 434 | put_u32(&h, need->effect_class); |
| 435 | put_u64(&h, need->authority_ceiling.resource_lo); |
| 436 | put_u64(&h, need->authority_ceiling.resource_hi); |
| 437 | put_u32(&h, need->authority_ceiling.rights); |
| 438 | put_u32(&h, loc); |
| 439 | put_u32(&h, pin); |
| 440 | put_u64(&h, need->latency_budget); |
| 441 | put_u64(&h, need->energy_budget); |
| 442 | put_u32(&h, need->reliability_requirement); |
| 443 | put_u32(&h, need->evidence_requirement); |
| 444 | sha256_final(&h, plan->digest); |
| 445 | return plan->verdict = s ? CQ_OK : CQ_E_NO_SOURCE; |
| 446 | } |
| 447 | |
| 448 | /* ---- ranking ---- */ |
| 449 | |
| 450 | static void pack_cand_7(const CqCandidate *c, uint64_t out[7]) { |
| 451 | out[0] = c->expected_cost; |
| 452 | out[1] = c->expected_latency; |
| 453 | out[2] = c->expected_energy; |
| 454 | out[3] = c->confidence; |
| 455 | out[4] = c->reliability; |
| 456 | out[5] = c->required_authority.held ? 1ull : 0ull; |
| 457 | out[6] = c->local ? 1ull : 0ull; |
| 458 | } |
| 459 | |
| 460 | static void pack_cand_11(const CqCandidate *c, uint64_t out[11]) { |
| 461 | pack_cand_7(c, out); |
| 462 | out[7] = c->capability_id; |
| 463 | out[8] = c->realization_id; |
| 464 | out[9] = c->machine_id; |
| 465 | out[10] = c->skill_id; |
| 466 | } |
| 467 | |
| 468 | uint64_t cq_dim_value(const CqCandidate *c, uint32_t dim) { |
| 469 | uint64_t vals[7], res = 0; |
| 470 | pack_cand_7(c, vals); |
| 471 | (void)omega2_rx_cq_dim_value(&res, vals, 7, (uint64_t)dim); |
| 472 | return res; |
| 473 | } |
| 474 | |
| 475 | typedef struct { |
| 476 | const CqCandidate *c; |
| 477 | const uint8_t *dims; |
| 478 | uint32_t n_dims; |
| 479 | } RankCtx; |
| 480 | |
| 481 | /* Strict order: the named dimensions, then the ids. */ |
| 482 | static int cmp_full(const CqCandidate *a, const CqCandidate *b, const uint8_t *dims, uint32_t nd) { |
| 483 | uint64_t pa[11], pb[11], res = 0; |
| 484 | uint8_t d7[7] = { 0 }; |
| 485 | pack_cand_11(a, pa); |
| 486 | pack_cand_11(b, pb); |
| 487 | for (uint32_t i = 0; i < nd && i < 7; i++) d7[i] = dims[i]; |
| 488 | (void)omega2_rx_cq_cmp_full(&res, pa, 11, pb, 11, d7, 7, (uint64_t)nd); |
| 489 | return (int)(int64_t)res; |
| 490 | } |
| 491 | |
| 492 | static __thread RankCtx t_sort; /* qsort has no context argument */ |
| 493 | |
| 494 | static int cmp_idx(const void *a, const void *b) { |
| 495 | const RankCtx *r = &t_sort; |
| 496 | return cmp_full(&r->c[*(const uint32_t *)a], &r->c[*(const uint32_t *)b], r->dims, r->n_dims); |
| 497 | } |
| 498 | |
| 499 | static int dominates(const CqCandidate *a, const CqCandidate *b, const uint8_t *dims, uint32_t nd) { |
| 500 | uint64_t pa[7], pb[7], res = 0; |
| 501 | uint8_t d7[7] = { 0 }; |
| 502 | pack_cand_7(a, pa); |
| 503 | pack_cand_7(b, pb); |
| 504 | for (uint32_t i = 0; i < nd && i < 7; i++) d7[i] = dims[i]; |
| 505 | (void)omega2_rx_cq_dominates(&res, pa, 7, pb, 7, d7, 7, (uint64_t)nd); |
| 506 | return res != 0; |
| 507 | } |
| 508 | |
| 509 | static int within(uint64_t v, uint64_t best, uint32_t tol_ppm) { |
| 510 | uint64_t res = 0; |
| 511 | (void)omega2_rx_cq_within(&res, v, best, (uint64_t)tol_ppm); |
| 512 | return res != 0; |
| 513 | } |
| 514 | |
| 515 | int cq_rank(const CqCandidate *in, uint32_t n, const CqTradeoffs *t, CqCandidate *out, uint32_t k, |
| 516 | uint32_t *front) { |
| 517 | static const uint8_t all_dims[CQ_DIMS] = { 0, 1, 2, 3, 4, 5, 6 }; |
| 518 | const uint8_t *dims = t && t->n_order ? t->order : all_dims; |
| 519 | uint32_t nd = t && t->n_order ? t->n_order : CQ_DIMS; |
| 520 | if (nd > CQ_DIMS) return CQ_E_ARG; |
| 521 | for (uint32_t i = 0; i < nd; i++) |
| 522 | if (dims[i] >= CQ_DIMS) return CQ_E_ARG; |
| 523 | if (front) *front = 0; |
| 524 | if (n == 0 || k == 0) return 0; |
| 525 | |
| 526 | uint32_t *idx = malloc((size_t)n * sizeof *idx); |
| 527 | uint8_t *mark = calloc(n, 1); /* 1 in pool, 2 taken */ |
| 528 | if (!idx || !mark) { free(idx); free(mark); return CQ_E_NOMEM; } |
| 529 | for (uint32_t i = 0; i < n; i++) idx[i] = i; |
| 530 | t_sort = (RankCtx){ in, dims, nd }; |
| 531 | qsort(idx, n, sizeof *idx, cmp_idx); |
| 532 | |
| 533 | /* Pareto front: a dominator sorts earlier, and dominance is transitive, |
| 534 | * so checking against the front found so far is enough. */ |
| 535 | uint32_t nf = 0; |
| 536 | uint32_t *fr = malloc((size_t)n * sizeof *fr); |
| 537 | if (!fr) { free(idx); free(mark); return CQ_E_NOMEM; } |
| 538 | for (uint32_t i = 0; i < n; i++) { |
| 539 | const CqCandidate *x = &in[idx[i]]; |
| 540 | int dom = 0; |
| 541 | for (uint32_t j = 0; j < nf && !dom; j++) dom = dominates(&in[fr[j]], x, dims, nd); |
| 542 | if (!dom) fr[nf++] = idx[i]; |
| 543 | } |
| 544 | if (front) *front = nf; |
| 545 | int all = t && t->include_dominated; |
| 546 | if (all) for (uint32_t i = 0; i < n; i++) mark[i] = 1; |
| 547 | else for (uint32_t j = 0; j < nf; j++) mark[fr[j]] = 1; |
| 548 | uint32_t pool = all ? n : nf; |
| 549 | if (k > pool) k = pool; |
| 550 | |
| 551 | /* Caller's priorities: keep what is within the band of the best on the |
| 552 | * first dimension, let the next decide among those, and so on; the strict |
| 553 | * order (dimensions, then ids) breaks what remains. Pool members are |
| 554 | * visited in the strict order, so the first survivor wins. */ |
| 555 | uint8_t *live = malloc(n); |
| 556 | if (!live) { free(idx); free(mark); free(fr); return CQ_E_NOMEM; } |
| 557 | uint32_t got = 0; |
| 558 | while (got < k) { |
| 559 | for (uint32_t i = 0; i < n; i++) live[i] = mark[i] == 1; |
| 560 | for (uint32_t d = 0; d < nd; d++) { |
| 561 | uint64_t best = UINT64_MAX; |
| 562 | for (uint32_t i = 0; i < n; i++) |
| 563 | if (live[i]) { |
| 564 | uint64_t v = cq_dim_value(&in[i], dims[d]); |
| 565 | if (v < best) best = v; |
| 566 | } |
| 567 | uint32_t tol = t ? t->tolerance_ppm[dims[d]] : 0; |
| 568 | for (uint32_t i = 0; i < n; i++) |
| 569 | if (live[i] && !within(cq_dim_value(&in[i], dims[d]), best, tol)) live[i] = 0; |
| 570 | } |
| 571 | uint32_t pick = UINT32_MAX; |
| 572 | for (uint32_t i = 0; i < n && pick == UINT32_MAX; i++) |
| 573 | if (live[idx[i]]) pick = idx[i]; |
| 574 | if (pick == UINT32_MAX) break; |
| 575 | out[got++] = in[pick]; |
| 576 | mark[pick] = 2; |
| 577 | } |
| 578 | free(live); |
| 579 | free(fr); |
| 580 | free(mark); |
| 581 | free(idx); |
| 582 | return (int)got; |
| 583 | } |
| 584 | |
| 585 | /* ---- run ---- */ |
| 586 | |
| 587 | size_t cq_result_bytes(const CqResult *r) { |
| 588 | return offsetof(CqResult, cand) + (size_t)r->n * sizeof(CqCandidate); |
| 589 | } |
| 590 | |
| 591 | static int machine_live(const CqCatalog *c, uint32_t machine, uint64_t now_us) { |
| 592 | if (machine == c->self_machine) return 1; |
| 593 | uint32_t s; |
| 594 | return map_get(c->mach_key, c->mach_mask, machine, &s) && c->mach_lease[s] > now_us; |
| 595 | } |
| 596 | |
| 597 | /* Weak runtime hooks so Fabric/standalone capq links never require rx_world.o or rx_graph.o. */ |
| 598 | extern int rx_world_validate_cap(const RxWorld *w, RxCapRef ref, uint32_t subject, |
| 599 | uint64_t resource, uint32_t rights, RxCapEntry *out) |
| 600 | __attribute__((weak)); |
| 601 | extern int rx_graph_need_authority(AgGraph *g, uint32_t node, uint64_t resource, uint32_t rights) |
| 602 | __attribute__((weak)); |
| 603 | |
| 604 | static uint32_t held_now(const CqHeld *h, uint64_t resource, uint32_t rights) { |
| 605 | if (rights == 0) return 1; |
| 606 | if (!h || !h->w || !h->caps || !rx_world_validate_cap) return 0; |
| 607 | for (uint32_t i = 0; i < h->caps->n; i++) { |
| 608 | if (h->caps->cap[i].resource != resource || (rights & ~h->caps->cap[i].rights)) continue; |
| 609 | RxCapEntry ent; |
| 610 | if (rx_world_validate_cap(h->w, h->caps->cap[i].ref, h->caps->subject, resource, rights, |
| 611 | &ent) == RX_OK) |
| 612 | return 1; |
| 613 | } |
| 614 | return 0; |
| 615 | } |
| 616 | |
| 617 | int cq_query(const CqCatalog *c, const CqPlan *plan, const CqNeed *need, const CqTradeoffs *t, |
| 618 | const CqHeld *held, uint64_t now_us, CqResult *out, CqStats *stats) { |
| 619 | return cq_query_admit(c, plan, need, t, held, now_us, NULL, NULL, out, stats); |
| 620 | } |
| 621 | |
| 622 | int cq_query_admit(const CqCatalog *c, const CqPlan *plan, const CqNeed *need, |
| 623 | const CqTradeoffs *t, const CqHeld *held, uint64_t now_us, |
| 624 | CqAdmitFn admit, void *admit_ctx, CqResult *out, CqStats *stats) { |
| 625 | if (!out) return CQ_E_ARG; |
| 626 | memset(out, 0, offsetof(CqResult, cand)); |
| 627 | CqStats st; |
| 628 | memset(&st, 0, sizeof st); |
| 629 | if (!c || !plan || !need || !t) return out->verdict = CQ_E_ARG; |
| 630 | if (!c->built) return out->verdict = CQ_E_UNBUILT; |
| 631 | if (plan->verdict != CQ_OK) { |
| 632 | if (stats) *stats = st; |
| 633 | return out->verdict = plan->verdict; |
| 634 | } |
| 635 | |
| 636 | uint64_t upper = 0; |
| 637 | for (uint32_t i = 0; i < plan->n_ops; i++) |
| 638 | for (uint32_t s = 0; s < CQ_SOURCES; s++) |
| 639 | if (plan->sources & CQ_SRC(s)) { |
| 640 | uint32_t b = bucket_key(plan->ops[i], s); |
| 641 | upper += c->bucket_start[b + 1] - c->bucket_start[b]; |
| 642 | } |
| 643 | CqCandidate *feas = malloc((size_t)(upper ? upper : 1) * sizeof *feas); |
| 644 | if (!feas) return out->verdict = CQ_E_NOMEM; |
| 645 | |
| 646 | const uint32_t loc = need->locality_constraints.allowed ? need->locality_constraints.allowed |
| 647 | : (CQ_LOC_LOCAL | CQ_LOC_FABRIC); |
| 648 | const uint32_t pin = need->locality_constraints.machine; |
| 649 | uint64_t need_vec[14] = { |
| 650 | loc, pin, need->accepted_input_types, need->required_output_types, need->effect_class, |
| 651 | need->authority_ceiling.resource_lo, need->authority_ceiling.resource_hi, |
| 652 | need->authority_ceiling.rights, need->latency_budget, need->energy_budget, |
| 653 | need->reliability_requirement, need->evidence_requirement, t->max_cost, t->min_confidence |
| 654 | }; |
| 655 | uint32_t nf = 0; |
| 656 | for (uint32_t i = 0; i < plan->n_ops; i++) |
| 657 | for (uint32_t s = 0; s < CQ_SOURCES; s++) { |
| 658 | if (!(plan->sources & CQ_SRC(s))) continue; |
| 659 | uint32_t b = bucket_key(plan->ops[i], s); |
| 660 | for (uint32_t j = c->bucket_start[b]; j < c->bucket_start[b + 1]; j++) { |
| 661 | const CqEntry *e = &c->e[c->bucket[j]]; |
| 662 | st.probed++; |
| 663 | if (e->live == CQ_LIVE_WITHDRAWN || !machine_live(c, e->machine_id, now_us)) { |
| 664 | st.rejected_dead++; |
| 665 | continue; |
| 666 | } |
| 667 | if (e->live != CQ_LIVE_AVAILABLE) { st.rejected_unavailable++; continue; } |
| 668 | int local = e->machine_id == c->self_machine; |
| 669 | uint64_t ent_vec[14] = { |
| 670 | e->machine_id, e->in_types, e->out_types, e->effects, e->auth_resource, |
| 671 | e->auth_rights, e->latency_us, e->energy_uj, e->reliability_ppm, |
| 672 | e->evidence_level, e->cost, e->confidence_ppm, (uint64_t)(local ? 1 : 0), |
| 673 | e->live |
| 674 | }; |
| 675 | uint64_t ok = 0; |
| 676 | (void)omega2_rx_cq_filter_entry(&ok, ent_vec, 14, need_vec, 14); |
| 677 | if (!ok) continue; |
| 678 | CqCandidate *x = &feas[nf++]; |
| 679 | memset(x, 0, sizeof *x); |
| 680 | x->capability_id = e->capability_id; |
| 681 | x->skill_id = e->skill_id; |
| 682 | x->machine_id = e->machine_id; |
| 683 | x->realization_id = e->realization_id; |
| 684 | x->required_authority.resource = e->auth_resource; |
| 685 | x->required_authority.rights = e->auth_rights; |
| 686 | x->required_authority.held = held_now(held, e->auth_resource, e->auth_rights); |
| 687 | x->expected_cost = e->cost; |
| 688 | x->expected_latency = e->latency_us; |
| 689 | x->expected_energy = e->energy_uj; |
| 690 | x->confidence = e->confidence_ppm; |
| 691 | x->reliability = e->reliability_ppm; |
| 692 | x->evidence.ref = e->evidence_ref; |
| 693 | x->evidence.level = e->evidence_level; |
| 694 | x->source = e->source; |
| 695 | x->local = (uint8_t)local; |
| 696 | x->effects = e->effects; |
| 697 | if (t->require_held && !x->required_authority.held) { |
| 698 | st.rejected_authority++; |
| 699 | nf--; |
| 700 | continue; |
| 701 | } |
| 702 | if (admit && !admit(admit_ctx, e, x)) { |
| 703 | st.rejected_admission++; |
| 704 | nf--; |
| 705 | continue; |
| 706 | } |
| 707 | } |
| 708 | } |
| 709 | st.feasible = nf; |
| 710 | uint32_t k = t->k ? t->k : CQ_MAX_K; |
| 711 | if (k > CQ_MAX_K) k = CQ_MAX_K; |
| 712 | uint32_t front = 0; |
| 713 | int n = cq_rank(feas, nf, t, out->cand, k, &front); |
| 714 | free(feas); |
| 715 | if (n < 0) return out->verdict = n; |
| 716 | out->n = (uint32_t)n; |
| 717 | out->n_feasible = nf; |
| 718 | out->n_front = front; |
| 719 | if (stats) *stats = st; |
| 720 | return out->verdict = CQ_OK; |
| 721 | } |
| 722 | |
| 723 | /* ---- consumption ---- */ |
| 724 | |
| 725 | int cq_bind_skill_node(AgGraph *g, uint32_t node, const CqCandidate *c) { |
| 726 | if (!g || !c || node >= g->n_nodes || c->skill_id == 0) return CQ_E_ARG; |
| 727 | if (g->nodes[node].kind != AG_SKILL && g->nodes[node].kind != AG_RETRY) return CQ_E_ARG; |
| 728 | g->nodes[node].op = c->skill_id; |
| 729 | if (c->required_authority.rights) { |
| 730 | if (!rx_graph_need_authority || |
| 731 | rx_graph_need_authority(g, node, c->required_authority.resource, |
| 732 | c->required_authority.rights) < 0) |
| 733 | return CQ_E_ARG; |
| 734 | } |
| 735 | return CQ_OK; |
| 736 | } |
| 737 | |
| 738 | const char *cq_source_name(uint32_t s) { |
| 739 | static const char *n[CQ_SOURCES] = { "capability_graph", "skill_network", "mcp_registry", |
| 740 | "local_physical", "fabric" }; |
| 741 | return s < CQ_SOURCES ? n[s] : "?"; |
| 742 | } |
| 743 | |
| 744 | /* ---- wire form (pure OM-2 codec in om/runtime/rx_capq_core.om) ---- */ |
| 745 | |
| 746 | static void wire_check(const uint8_t *rec, uint8_t out[4]) { |
| 747 | uint8_t d[SHA256_DIGEST_SIZE]; |
| 748 | sha256_hash(rec, CQ_WIRE_BYTES - 4, d); |
| 749 | memcpy(out, d, 4); |
| 750 | } |
| 751 | |
| 752 | int cq_wire_encode(const CqCatalog *c, const CqEntry *e, uint32_t kind, uint8_t out[CQ_WIRE_BYTES]) { |
| 753 | if (!c || !e || !out || kind < CQ_WIRE_ADVERTISE || kind > CQ_WIRE_WITHDRAW) return CQ_E_ARG; |
| 754 | if (e->machine_id != c->self_machine || e->source == CQ_SRC_FABRIC || e->source >= CQ_SOURCES || |
| 755 | e->live > CQ_LIVE_UNAVAILABLE) |
| 756 | return CQ_E_WIRE; |
| 757 | AienMachineId m; |
| 758 | if (cq_machine_identity(c, e->machine_id, &m) != CQ_OK) return CQ_E_NO_IDENTITY; |
| 759 | uint8_t mid_buf[AIEN_MID_RECORD_BYTES]; |
| 760 | if (aien_mid_encode(&m, mid_buf) != AIEN_MID_OK) return CQ_E_NO_IDENTITY; |
| 761 | uint64_t hdr[4] = { kind, e->source, e->evidence_level, e->live }; |
| 762 | uint64_t fields[17] = { |
| 763 | e->capability_id, e->realization_id, e->skill_id, e->skill_version, |
| 764 | e->op, e->effects, e->in_types, e->out_types, e->auth_resource, |
| 765 | e->auth_rights, e->confidence_ppm, e->reliability_ppm, e->cost, |
| 766 | e->latency_us, e->energy_uj, e->evidence_ref, e->generation |
| 767 | }; |
| 768 | uint64_t ok = 0; |
| 769 | (void)omega2_rx_cq_wire_encode_body(&ok, hdr, 4, fields, 17, mid_buf, AIEN_MID_RECORD_BYTES, |
| 770 | e->skill_digest, 32, out, CQ_WIRE_BYTES); |
| 771 | if (!ok) return CQ_E_WIRE; |
| 772 | wire_check(out, out + 184); |
| 773 | return CQ_OK; |
| 774 | } |
| 775 | |
| 776 | int cq_wire_decode(const uint8_t *in, size_t len, uint32_t *kind, AienMachineId *machine, |
| 777 | CqEntry *out) { |
| 778 | if (!in || !kind || !machine || !out) return CQ_E_ARG; |
| 779 | if (len != CQ_WIRE_BYTES) return CQ_E_WIRE; |
| 780 | uint8_t chk[4]; |
| 781 | wire_check(in, chk); |
| 782 | if (memcmp(chk, in + 184, 4) != 0) return CQ_E_WIRE; |
| 783 | uint64_t hdr[4] = { 0 }, fields[17] = { 0 }, ok = 0; |
| 784 | uint8_t sdig[32] = { 0 }; |
| 785 | (void)omega2_rx_cq_wire_decode_body(&ok, in, CQ_WIRE_BYTES, hdr, 4, fields, 17, sdig, 32); |
| 786 | if (!ok) return CQ_E_WIRE; |
| 787 | if (aien_mid_decode(in + 8, AIEN_MID_RECORD_BYTES, machine) != AIEN_MID_OK) return CQ_E_WIRE; |
| 788 | memset(out, 0, sizeof *out); |
| 789 | out->source = (uint8_t)hdr[1]; |
| 790 | out->evidence_level = (uint8_t)hdr[2]; |
| 791 | out->live = (uint32_t)hdr[3]; |
| 792 | out->capability_id = (uint32_t)fields[0]; |
| 793 | out->realization_id = (uint32_t)fields[1]; |
| 794 | out->skill_id = (uint32_t)fields[2]; |
| 795 | out->skill_version = (uint32_t)fields[3]; |
| 796 | memcpy(out->skill_digest, sdig, 32); |
| 797 | out->op = (uint32_t)fields[4]; |
| 798 | out->effects = (uint16_t)fields[5]; |
| 799 | out->in_types = fields[6]; |
| 800 | out->out_types = fields[7]; |
| 801 | out->auth_resource = fields[8]; |
| 802 | out->auth_rights = (uint32_t)fields[9]; |
| 803 | out->confidence_ppm = (uint32_t)fields[10]; |
| 804 | out->reliability_ppm = (uint32_t)fields[11]; |
| 805 | out->cost = fields[12]; |
| 806 | out->latency_us = fields[13]; |
| 807 | out->energy_uj = fields[14]; |
| 808 | out->evidence_ref = fields[15]; |
| 809 | out->generation = fields[16]; |
| 810 | *kind = (uint32_t)hdr[0]; |
| 811 | return CQ_OK; |
| 812 | } |
| 813 | |
| 814 | int cq_wire_apply(CqCatalog *c, const uint8_t *in, size_t len, CqKey *key_out) { |
| 815 | if (!c) return CQ_E_ARG; |
| 816 | if (!c->machines) return CQ_E_NO_IDENTITY; |
| 817 | uint32_t kind; |
| 818 | AienMachineId m; |
| 819 | CqEntry e; |
| 820 | int rc = cq_wire_decode(in, len, &kind, &m, &e); |
| 821 | if (rc != CQ_OK) return rc; |
| 822 | uint32_t idx = aien_mid_index_find(c->machines, &m); |
| 823 | if (idx == c->self_machine) return CQ_E_WIRE; /* nobody speaks for us */ |
| 824 | if (idx == 0) { |
| 825 | idx = aien_mid_index_bind(c->machines, &m); |
| 826 | if (idx == 0) return CQ_E_NO_IDENTITY; |
| 827 | } |
| 828 | uint32_t op = cq_resolve(c, e.op); |
| 829 | if (op == 0) return CQ_E_NO_OP; |
| 830 | e.op = op; |
| 831 | e.machine_id = idx; |
| 832 | e.source = CQ_SRC_FABRIC; /* local source was provenance only */ |
| 833 | CqKey k = cq_key_of(&e); |
| 834 | const CqEntry *old = cq_lookup(c, &k); |
| 835 | uint64_t chk = 0; |
| 836 | (void)omega2_rx_cq_wire_apply_check(&chk, old ? 1ull : 0ull, old ? old->generation : 0ull, |
| 837 | old ? (uint64_t)old->live : 0ull, e.generation, |
| 838 | (uint64_t)kind); |
| 839 | if ((int64_t)chk != 0) return (int)(int64_t)chk; |
| 840 | if (kind == CQ_WIRE_ADVERTISE) { |
| 841 | rc = cq_register(c, &e, NULL, 0); |
| 842 | } else { |
| 843 | /* Availability and withdrawal change only `live` (and the revision): |
| 844 | * the rest of the held entry stays as advertised. */ |
| 845 | CqEntry x = *old; |
| 846 | x.live = e.live; |
| 847 | x.generation = e.generation; |
| 848 | rc = cq_register(c, &x, NULL, 0); |
| 849 | } |
| 850 | if (rc == CQ_OK && key_out) *key_out = k; |
| 851 | return rc; |
| 852 | } |