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 / crates / aienos-c1-tree / c1_tree.om

518 lines · 9991 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note aienux/crates/aienos-c1-tree/c1_tree.om: Pure-Omega C1 Copy-On-Write Prefix Tree engine.
3note Converted from aienux crates/aienos-c1-tree/src/{page,pool,metrics,tree,lib}.rs.
4note Compose with: si/directives/om2.om om/tls/sha2.om aienux/crates/aienos-c1-tree/c1_tree.om ...
5
6const C1_PAGE_CAP ulong 64
7const C1_KV_ELEMS_PER_TOK ulong 128
8const C1_PAGE_KV_BYTES ulong 32768
9const C1_MAX_PAGES ulong 64
10const C1_MAX_BRANCHES ulong 16
11const C1_MAX_BPAGES ulong 16
12
13const C1_OK int 0
14const C1_ERR_NOT_FOUND int -1
15const C1_ERR_EVICTED int -2
16const C1_ERR_EXISTS int -3
17const C1_ERR_CORRUPT int -4
18const C1_ERR_QUOTA int -5
19const C1_ERR_CAPACITY int -6
20
21note Physical page pool state
22global c1_next_pid ulong 1
23global c1_p_used uchar 64
24global c1_p_id ulong 64
25global c1_p_len ulong 64
26global c1_p_ref ulong 64
27global c1_p_toks uint 4096
28global c1_p_cksum uchar 2048
29
30note Branch table state
31global c1_b_used uchar 16
32global c1_b_id ulong 16
33global c1_b_evicted uchar 16
34global c1_b_npages ulong 16
35global c1_b_ntoks ulong 16
36global c1_b_pids ulong 256
37
38note Reservation receipt output: 0=branch_id, 1=tokens_reserved, 2=pages_reserved, 3=tick
39global c1_res_out ulong 4
40
41note Metrics output: 0=phys_pages, 1=phys_bytes, 2=virt_tokens, 3=phys_token_cap
42global c1_met_out ulong 4
43
44fn c1_init to void
45put c1_next_pid 0 1
46for i ulong C1_MAX_PAGES
47put c1_p_used i 0
48put c1_p_id i 0
49put c1_p_len i 0
50put c1_p_ref i 0
51end
52for i ulong C1_MAX_BRANCHES
53put c1_b_used i 0
54put c1_b_id i 0
55put c1_b_evicted i 0
56put c1_b_npages i 0
57put c1_b_ntoks i 0
58end
59end
60
61fn c1_find_page pid ulong to long
62for i ulong C1_MAX_PAGES
63if and eq get c1_p_used i 1 eq get c1_p_id i pid
64ret long i
65end
66end
67ret -1
68end
69
70fn c1_find_branch bid ulong to long
71for i ulong C1_MAX_BRANCHES
72if and eq get c1_b_used i 1 eq get c1_b_id i bid
73ret long i
74end
75end
76ret -1
77end
78
79fn c1_page_hash slot ulong out_ck edit uchar * to void
80ctx = new ulong 40 0
81sha256_init ctx
82pid = get c1_p_id slot
83hdr = new uchar 8 0
84for i ulong 8
85sh = mul sub 7 i 8
86put hdr i uchar and shr pid sh 0xff
87end
88sha256_update ctx hdr
89plen = get c1_p_len slot
90tbase = mul slot C1_PAGE_CAP
91tbuf = new uchar 4 0
92for i ulong plen bound 64
93tok = get c1_p_toks add tbase i
94put tbuf 0 uchar and shr tok 24 0xff
95put tbuf 1 uchar and shr tok 16 0xff
96put tbuf 2 uchar and shr tok 8 0xff
97put tbuf 3 uchar and tok 0xff
98sha256_update ctx tbuf
99end
100sha256_final ctx out_ck
101end
102
103fn c1_page_recompute slot ulong to void
104ck = new uchar 32 0
105c1_page_hash slot ck
106cbase = mul slot 32
107for i ulong 32
108put c1_p_cksum add cbase i get ck i
109end
110end
111
112fn c1_page_verify slot ulong to bool
113ck = new uchar 32 0
114c1_page_hash slot ck
115cbase = mul slot 32
116for i ulong 32
117if ne get c1_p_cksum add cbase i get ck i
118ret false
119end
120end
121ret true
122end
123
124fn c1_pool_alloc to ulong
125for i ulong C1_MAX_PAGES
126if eq get c1_p_used i 0
127pid = get c1_next_pid 0
128put c1_next_pid 0 add pid 1
129put c1_p_used i 1
130put c1_p_id i pid
131put c1_p_len i 0
132put c1_p_ref i 1
133c1_page_recompute i
134ret pid
135end
136end
137ret 0
138end
139
140fn c1_pool_retain pid ulong to void
141sl = c1_find_page pid
142if ge sl 0
143u = ulong sl
144put c1_p_ref u add get c1_p_ref u 1
145end
146end
147
148fn c1_pool_release pid ulong to void
149sl = c1_find_page pid
150if ge sl 0
151u = ulong sl
152r = get c1_p_ref u
153if gt r 1
154put c1_p_ref u sub r 1
155else
156put c1_p_ref u 0
157put c1_p_used u 0
158put c1_p_id u 0
159put c1_p_len u 0
160end
161end
162end
163
164fn c1_copy_page_tokens su ulong du ulong to void
165plen = get c1_p_len su
166put c1_p_len du plen
167sbase = mul su C1_PAGE_CAP
168dbase = mul du C1_PAGE_CAP
169for i ulong plen bound 64
170put c1_p_toks add dbase i get c1_p_toks add sbase i
171end
172c1_page_recompute du
173end
174
175fn c1_pool_cow src_pid ulong to ulong
176src_sl = c1_find_page src_pid
177if lt src_sl 0
178ret 0
179end
180new_pid = c1_pool_alloc
181if eq new_pid 0
182ret 0
183end
184dst_sl = c1_find_page new_pid
185if lt dst_sl 0
186ret 0
187end
188c1_copy_page_tokens ulong src_sl ulong dst_sl
189c1_pool_release src_pid
190ret new_pid
191end
192
193fn c1_pool_active_count to ulong
194cnt = ulong 0
195for i ulong C1_MAX_PAGES
196if eq get c1_p_used i 1
197cnt = add cnt 1
198end
199end
200ret cnt
201end
202
203fn c1_pool_memory_bytes to ulong
204ret mul c1_pool_active_count C1_PAGE_KV_BYTES
205end
206
207fn c1_page_append_slot slot ulong tok uint to bool
208plen = get c1_p_len slot
209if ge plen C1_PAGE_CAP
210ret false
211end
212tbase = mul slot C1_PAGE_CAP
213put c1_p_toks add tbase plen tok
214put c1_p_len slot add plen 1
215c1_page_recompute slot
216ret true
217end
218
219fn c1_append_single bslot ulong tok uint to int
220np = get c1_b_npages bslot
221bbase = mul bslot C1_MAX_BPAGES
222if eq np 0
223pid = c1_pool_alloc
224if eq pid 0
225ret C1_ERR_CAPACITY
226end
227psl = c1_find_page pid
228if lt psl 0
229ret C1_ERR_CORRUPT
230end
231ok = c1_page_append_slot ulong psl tok
232if not ok
233ret C1_ERR_CORRUPT
234end
235put c1_b_pids bbase pid
236put c1_b_npages bslot 1
237put c1_b_ntoks bslot add get c1_b_ntoks bslot 1
238ret C1_OK
239end
240
241last_idx = sub np 1
242last_pid = get c1_b_pids add bbase last_idx
243psl = c1_find_page last_pid
244if lt psl 0
245ret C1_ERR_CORRUPT
246end
247pu = ulong psl
248if not c1_page_verify pu
249ret C1_ERR_CORRUPT
250end
251if lt get c1_p_len pu C1_PAGE_CAP
252if eq get c1_p_ref pu 1
253ok = c1_page_append_slot pu tok
254if not ok
255ret C1_ERR_CORRUPT
256end
257else
258new_pid = c1_pool_cow last_pid
259if eq new_pid 0
260ret C1_ERR_CAPACITY
261end
262nsl = c1_find_page new_pid
263if lt nsl 0
264ret C1_ERR_CORRUPT
265end
266ok = c1_page_append_slot ulong nsl tok
267if not ok
268ret C1_ERR_CORRUPT
269end
270put c1_b_pids add bbase last_idx new_pid
271end
272else
273if ge np C1_MAX_BPAGES
274ret C1_ERR_CAPACITY
275end
276new_pid = c1_pool_alloc
277if eq new_pid 0
278ret C1_ERR_CAPACITY
279end
280nsl = c1_find_page new_pid
281if lt nsl 0
282ret C1_ERR_CORRUPT
283end
284ok = c1_page_append_slot ulong nsl tok
285if not ok
286ret C1_ERR_CORRUPT
287end
288put c1_b_pids add bbase np new_pid
289put c1_b_npages bslot add np 1
290end
291put c1_b_ntoks bslot add get c1_b_ntoks bslot 1
292ret C1_OK
293end
294
295fn c1_create_root bid ulong toks view uint * to int
296if ge c1_find_branch bid 0
297ret C1_ERR_EXISTS
298end
299bslot = ulong C1_MAX_BRANCHES
300for i ulong C1_MAX_BRANCHES
301if eq get c1_b_used i 0
302bslot = i
303break
304end
305end
306if eq bslot C1_MAX_BRANCHES
307ret C1_ERR_CAPACITY
308end
309put c1_b_used bslot 1
310put c1_b_id bslot bid
311put c1_b_evicted bslot 0
312put c1_b_npages bslot 0
313put c1_b_ntoks bslot 0
314for i ulong len toks bound 1024
315rc = c1_append_single bslot get toks i
316if ne rc C1_OK
317ret rc
318end
319end
320ret C1_OK
321end
322
323fn c1_fork_branch parent_id ulong child_id ulong to int
324psl = c1_find_branch parent_id
325if lt psl 0
326ret C1_ERR_NOT_FOUND
327end
328pu = ulong psl
329if eq get c1_b_evicted pu 1
330ret C1_ERR_EVICTED
331end
332if ge c1_find_branch child_id 0
333ret C1_ERR_EXISTS
334end
335cslot = ulong C1_MAX_BRANCHES
336for i ulong C1_MAX_BRANCHES
337if eq get c1_b_used i 0
338cslot = i
339break
340end
341end
342if eq cslot C1_MAX_BRANCHES
343ret C1_ERR_CAPACITY
344end
345np = get c1_b_npages pu
346pbase = mul pu C1_MAX_BPAGES
347cbase = mul cslot C1_MAX_BPAGES
348for i ulong np bound 16
349pid = get c1_b_pids add pbase i
350c1_pool_retain pid
351put c1_b_pids add cbase i pid
352end
353put c1_b_used cslot 1
354put c1_b_id cslot child_id
355put c1_b_evicted cslot 0
356put c1_b_npages cslot np
357put c1_b_ntoks cslot get c1_b_ntoks pu
358ret C1_OK
359end
360
361fn c1_reserve_step bid ulong num_toks ulong tick ulong max_pages ulong to int
362bsl = c1_find_branch bid
363if lt bsl 0
364ret C1_ERR_NOT_FOUND
365end
366bu = ulong bsl
367if eq get c1_b_evicted bu 1
368ret C1_ERR_EVICTED
369end
370needed = ulong 0
371if gt num_toks 0
372needed = div add num_toks sub C1_PAGE_CAP 1 C1_PAGE_CAP
373end
374alloc = c1_pool_active_count
375if gt add alloc needed max_pages
376ret C1_ERR_QUOTA
377end
378put c1_res_out 0 bid
379put c1_res_out 1 num_toks
380put c1_res_out 2 needed
381put c1_res_out 3 tick
382ret C1_OK
383end
384
385fn c1_append_tokens bid ulong toks view uint * to int
386bsl = c1_find_branch bid
387if lt bsl 0
388ret C1_ERR_NOT_FOUND
389end
390bu = ulong bsl
391if eq get c1_b_evicted bu 1
392ret C1_ERR_EVICTED
393end
394for i ulong len toks bound 1024
395rc = c1_append_single bu get toks i
396if ne rc C1_OK
397ret rc
398end
399end
400ret C1_OK
401end
402
403fn c1_copy_page_out pu ulong dst edit uint * pos ulong to ulong
404plen = get c1_p_len pu
405tbase = mul pu C1_PAGE_CAP
406for j ulong plen bound 64
407put dst add pos j get c1_p_toks add tbase j
408end
409ret add pos plen
410end
411
412fn c1_get_tokens bid ulong dst edit uint * to long
413bsl = c1_find_branch bid
414if lt bsl 0
415ret long C1_ERR_NOT_FOUND
416end
417bu = ulong bsl
418if eq get c1_b_evicted bu 1
419ret long C1_ERR_EVICTED
420end
421if gt get c1_b_ntoks bu len dst
422ret long C1_ERR_CAPACITY
423end
424np = get c1_b_npages bu
425bbase = mul bu C1_MAX_BPAGES
426pos = ulong 0
427for i ulong np bound 16
428psl = c1_find_page get c1_b_pids add bbase i
429if lt psl 0
430ret long C1_ERR_CORRUPT
431end
432if not c1_page_verify ulong psl
433ret long C1_ERR_CORRUPT
434end
435pos = c1_copy_page_out ulong psl dst pos
436end
437ret long pos
438end
439
440fn c1_evict_branch bid ulong to int
441bsl = c1_find_branch bid
442if lt bsl 0
443ret C1_ERR_NOT_FOUND
444end
445bu = ulong bsl
446if eq get c1_b_evicted bu 1
447ret C1_OK
448end
449np = get c1_b_npages bu
450bbase = mul bu C1_MAX_BPAGES
451for i ulong np bound 16
452c1_pool_release get c1_b_pids add bbase i
453end
454put c1_b_npages bu 0
455put c1_b_evicted bu 1
456ret C1_OK
457end
458
459fn c1_remove_branch bid ulong to int
460bsl = c1_find_branch bid
461if lt bsl 0
462ret C1_ERR_NOT_FOUND
463end
464bu = ulong bsl
465if eq get c1_b_evicted bu 0
466np = get c1_b_npages bu
467bbase = mul bu C1_MAX_BPAGES
468for i ulong np bound 16
469c1_pool_release get c1_b_pids add bbase i
470end
471end
472put c1_b_used bu 0
473put c1_b_id bu 0
474put c1_b_npages bu 0
475put c1_b_ntoks bu 0
476put c1_b_evicted bu 0
477ret C1_OK
478end
479
480fn c1_reconstruct_branch bid ulong toks view uint * to int
481bsl = c1_find_branch bid
482if lt bsl 0
483ret C1_ERR_NOT_FOUND
484end
485bu = ulong bsl
486if eq get c1_b_evicted bu 0
487np = get c1_b_npages bu
488bbase = mul bu C1_MAX_BPAGES
489for i ulong np bound 16
490c1_pool_release get c1_b_pids add bbase i
491end
492end
493put c1_b_npages bu 0
494put c1_b_ntoks bu 0
495put c1_b_evicted bu 0
496for i ulong len toks bound 1024
497rc = c1_append_single bu get toks i
498if ne rc C1_OK
499ret rc
500end
501end
502ret C1_OK
503end
504
505fn c1_compute_metrics to void
506pp = c1_pool_active_count
507pb = mul pp C1_PAGE_KV_BYTES
508vt = ulong 0
509for i ulong C1_MAX_BRANCHES
510if eq get c1_b_used i 1
511vt = add vt get c1_b_ntoks i
512end
513end
514put c1_met_out 0 pp
515put c1_met_out 1 pb
516put c1_met_out 2 vt
517put c1_met_out 3 mul pp C1_PAGE_CAP
518end