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 / aienux / argus / ring.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note aienux/argus/ring.om: Pure-Omega ARGUS Class-Gated Admission Ring & Drop Synthesizer. |
| 3 | note Converted from upstream aienux native/argus/argus_ring.c. |
| 4 | note Enforces 50%/75%/90%/100% per-class saturation watermarks, sticky critical_overflow, |
| 5 | note malformed push accounting (never drained as drops), and priority-ordered TELEMETRY_DROPPED synthesis. |
| 6 | |
| 7 | const ARGUS_RING_MIN_CAP uint 4 |
| 8 | const ARGUS_RING_MAX_CAP uint 256 |
| 9 | |
| 10 | note Ring metadata: 0=head, 1=tail, 2=capacity, 3=mask, 4=critical_overflow, 5=init |
| 11 | global argus_ring_meta ulong 8 |
| 12 | global argus_ring_refused ulong 5 |
| 13 | global argus_ring_pending ulong 5 |
| 14 | global argus_ring_limit ulong 5 |
| 15 | note 256 slots * 128 wire bytes = 32768 bytes |
| 16 | global argus_ring_slots uchar 32768 |
| 17 | |
| 18 | fn argus_is_pow2 v uint to bool |
| 19 | if eq v 0 |
| 20 | ret false |
| 21 | end |
| 22 | ret eq and v sub v 1 0 |
| 23 | end |
| 24 | |
| 25 | fn argus_ring_saturation_point cap_pow2 uint cls uchar to uint |
| 26 | if or lt cap_pow2 ARGUS_RING_MIN_CAP gt cap_pow2 ARGUS_RING_MAX_CAP |
| 27 | ret 0 |
| 28 | end |
| 29 | if not argus_is_pow2 cap_pow2 |
| 30 | ret 0 |
| 31 | end |
| 32 | if eq cls ARGUS_CLASS_CRITICAL |
| 33 | ret cap_pow2 |
| 34 | end |
| 35 | if eq cls ARGUS_CLASS_SECURITY |
| 36 | note 90% rounded up to a whole slot: (cap * 9 + 9) / 10 |
| 37 | ret uint div add mul ulong cap_pow2 9 9 10 |
| 38 | end |
| 39 | if eq cls ARGUS_CLASS_AUDIT |
| 40 | note 75% |
| 41 | ret uint div mul ulong cap_pow2 3 4 |
| 42 | end |
| 43 | if eq cls ARGUS_CLASS_INFORMATIONAL |
| 44 | note 50% |
| 45 | ret uint div ulong cap_pow2 2 |
| 46 | end |
| 47 | ret 0 |
| 48 | end |
| 49 | |
| 50 | fn argus_ring_init cap_pow2 uint to int |
| 51 | if or lt cap_pow2 ARGUS_RING_MIN_CAP gt cap_pow2 ARGUS_RING_MAX_CAP |
| 52 | ret ARGUS_ERR_ARG |
| 53 | end |
| 54 | if not argus_is_pow2 cap_pow2 |
| 55 | ret ARGUS_ERR_ARG |
| 56 | end |
| 57 | put argus_ring_meta 0 0 |
| 58 | put argus_ring_meta 1 0 |
| 59 | put argus_ring_meta 2 ulong cap_pow2 |
| 60 | put argus_ring_meta 3 ulong sub cap_pow2 1 |
| 61 | put argus_ring_meta 4 0 |
| 62 | put argus_ring_meta 5 1 |
| 63 | for c ulong 5 |
| 64 | put argus_ring_refused c 0 |
| 65 | put argus_ring_pending c 0 |
| 66 | put argus_ring_limit c ulong argus_ring_saturation_point cap_pow2 uchar c |
| 67 | end |
| 68 | ret ARGUS_OK |
| 69 | end |
| 70 | |
| 71 | fn argus_ring_admit ev view ArgusEvent to int |
| 72 | if eq get argus_ring_meta 5 0 |
| 73 | ret ARGUS_ERR_STATE |
| 74 | end |
| 75 | cls = get ev class_ |
| 76 | k = get ev kind |
| 77 | minc = argus_event_min_class k |
| 78 | bad_cls = or lt cls ARGUS_CLASS_CRITICAL gt cls ARGUS_CLASS_MAX |
| 79 | weaker = and ne minc 0 gt cls minc |
| 80 | is_prop = eq k ARGUS_EV_CONTAINMENT_PROPOSED |
| 81 | if or bad_cls or weaker is_prop |
| 82 | put argus_ring_refused 0 add get argus_ring_refused 0 1 |
| 83 | ret ARGUS_ERR_MALFORMED |
| 84 | end |
| 85 | h = get argus_ring_meta 0 |
| 86 | t = get argus_ring_meta 1 |
| 87 | lim = get argus_ring_limit ulong cls |
| 88 | if ge sub h t lim |
| 89 | if eq cls ARGUS_CLASS_CRITICAL |
| 90 | put argus_ring_meta 4 add get argus_ring_meta 4 1 |
| 91 | else |
| 92 | put argus_ring_refused ulong cls add get argus_ring_refused ulong cls 1 |
| 93 | end |
| 94 | put argus_ring_pending ulong cls add get argus_ring_pending ulong cls 1 |
| 95 | ret ARGUS_ERR_FULL |
| 96 | end |
| 97 | ret ARGUS_OK |
| 98 | end |
| 99 | |
| 100 | fn argus_ring_push ev view ArgusEvent mach view uchar 32 evdig view uchar 32 to int |
| 101 | rc = argus_ring_admit ev |
| 102 | if ne rc ARGUS_OK |
| 103 | ret rc |
| 104 | end |
| 105 | wire = new uchar 128 0 |
| 106 | rc = argus_event_encode ev mach evdig wire |
| 107 | if ne rc ARGUS_OK |
| 108 | ret rc |
| 109 | end |
| 110 | h = get argus_ring_meta 0 |
| 111 | slot = and h get argus_ring_meta 3 |
| 112 | base = mul slot 128 |
| 113 | for i ulong 128 |
| 114 | put argus_ring_slots add base i get wire i |
| 115 | end |
| 116 | put argus_ring_meta 0 add h 1 |
| 117 | ret ARGUS_OK |
| 118 | end |
| 119 | |
| 120 | fn argus_ring_pop dst edit ArgusEvent mach edit uchar 32 evdig edit uchar 32 to int |
| 121 | if eq get argus_ring_meta 5 0 |
| 122 | ret ARGUS_ERR_STATE |
| 123 | end |
| 124 | h = get argus_ring_meta 0 |
| 125 | t = get argus_ring_meta 1 |
| 126 | if eq h t |
| 127 | ret ARGUS_ERR_STATE |
| 128 | end |
| 129 | slot = and t get argus_ring_meta 3 |
| 130 | base = mul slot 128 |
| 131 | wire = new uchar 128 0 |
| 132 | for i ulong 128 |
| 133 | put wire i get argus_ring_slots add base i |
| 134 | end |
| 135 | put argus_ring_meta 1 add t 1 |
| 136 | argus_event_decode_fields wire dst |
| 137 | for i ulong 32 |
| 138 | put mach i get wire add 64 i |
| 139 | put evdig i get wire add 96 i |
| 140 | end |
| 141 | ret ARGUS_OK |
| 142 | end |
| 143 | |
| 144 | fn argus_ring_stats dst edit ArgusRingStats to void |
| 145 | h = get argus_ring_meta 0 |
| 146 | t = get argus_ring_meta 1 |
| 147 | cap = get argus_ring_meta 2 |
| 148 | put dst pushed h |
| 149 | put dst popped t |
| 150 | put dst critical_overflow get argus_ring_meta 4 |
| 151 | d = ulong 0 |
| 152 | if ge h t |
| 153 | d = sub h t |
| 154 | end |
| 155 | if gt d cap |
| 156 | d = cap |
| 157 | end |
| 158 | put dst depth uint d |
| 159 | put dst capacity uint cap |
| 160 | put dst ref_malformed get argus_ring_refused 0 |
| 161 | put dst ref_crit get argus_ring_refused 1 |
| 162 | put dst ref_sec get argus_ring_refused 2 |
| 163 | put dst ref_audit get argus_ring_refused 3 |
| 164 | put dst ref_info get argus_ring_refused 4 |
| 165 | end |
| 166 | |
| 167 | note Drain the highest-priority pending drop class (CRITICAL -> SECURITY -> AUDIT -> INFORMATIONAL). |
| 168 | note Returns ARGUS_OK and populates dst if a drop event was synthesized, else ARGUS_ERR_STATE. |
| 169 | fn argus_ring_drain_one dst edit ArgusEvent next_seq edit ulong 1 to int |
| 170 | if eq get argus_ring_meta 5 0 |
| 171 | ret ARGUS_ERR_STATE |
| 172 | end |
| 173 | for c ulong 4 |
| 174 | cls = add c 1 |
| 175 | cnt = get argus_ring_pending cls |
| 176 | if gt cnt 0 |
| 177 | put argus_ring_pending cls 0 |
| 178 | seq = get next_seq 0 |
| 179 | if eq seq 0 |
| 180 | seq = ulong 1 |
| 181 | end |
| 182 | put next_seq 0 add seq 1 |
| 183 | put dst version ARGUS_ABI_VERSION |
| 184 | put dst class_ ARGUS_CLASS_CRITICAL |
| 185 | put dst kind ARGUS_EV_TELEMETRY_DROPPED |
| 186 | put dst effect_class ARGUS_EFFECT_NONE |
| 187 | put dst outcome ARGUS_OUTCOME_ERROR |
| 188 | put dst flags ARGUS_FLAG_CONSUMER |
| 189 | put dst sequence seq |
| 190 | put dst tick 0 |
| 191 | put dst principal 0 |
| 192 | put dst code ARGUS_ERR_FULL |
| 193 | put dst cap_id ARGUS_CAP_NONE |
| 194 | put dst object_id uint cls |
| 195 | put dst cap_generation 0 |
| 196 | put dst world_generation 0 |
| 197 | put dst resource cnt |
| 198 | ret ARGUS_OK |
| 199 | end |
| 200 | end |
| 201 | ret ARGUS_ERR_STATE |
| 202 | end |