interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / aienux / argus / ring.om

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