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 / seed / node / view.om

704 lines · 15955 bytes Raw Blame History
1note seed/node/view.om -- the Omega Node operator view (DRA-291). OM-2.
2note Compose after seed/net/http.om, transport.om, pool.om, super.om,
3note sched.om, seed/authority/action.om, journal.om and auth.om, before the
4note program that drives the creator (code.om).
5note One shared page (32768 bytes) carries the creator's state to a view
6note process it forks, and operator commands back. The view process only
7note reads state and posts commands; the creator alone decides them,
8note through the authority and the journal, and then acknowledges them.
9note Page words: 0 magic, 1 layout 1, 2 snapshot seqlock (odd while the
10note creator writes), 3 version (events so far, this incarnation), 4
11note incarnation (the journal fence), 5 creator pid, 6 stop, 7 view port,
12note 8 requests served, 9 view pid.
13note Snapshot words, written by the creator under the seqlock:
14note 16..21 authority: generation, paused, journal seq, journal failed,
15note fence, version of the last pause change; 32..39 queue: waiting,
16note ready, running, in review, UNKNOWN, admission limit, review limit,
17note admissions refused while full; 44..46 commands committed, refused,
18note last id; 48 workers, then 4 words each at 64+4w: pid (0 none),
19note incarnation, control seq acknowledged, paused as seen; 96 uncertain
20note jobs (outcome UNKNOWN), their ids at 97..112; 128 objectives, then 16
21note words each at 136+16i (set by the driver); 200..205 limits.
22note Events: a ring of 256 at word 1024, 4 words each: version, type |
23note target << 8 | x << 16, a, b. Types: 1 job (x what, a job id, b
24note value), 2 objective result (a result, b turns), 3 command (x op, a
25note id, b status), 4 pause (a 1 paused 0 resumed), 5 worker (x slot, a
26note pid, b 1 started 0 lost), 6 admission refused (a job id), 7 uncertain
27note (a job id).
28note Mailbox at word 2048: 0 state (0 free, 1 posted, 2 acknowledged), 1
29note command id, 2 op, 3 target, 4 view version the command was based on,
30note 5..8 arguments, 9 status, 10 version, 11 journal seq, 12 incarnation.
31note Ops: 1 pause, 2 resume, 3 cancel objective, 4 budget objective a
32note (model turns), 5 redirect objective to kind a, K b, N c, A d.
33note Status: 0 committed, 1 stale, 2 invalid, 3 refused, 4 duplicate (the
34note id was committed before; the version is the original one).
35const VWBYTES ulong 32768
36const VWMAG ulong 1330466390
37const VWRING ulong 1024
38const VWBOX ulong 2048
39text vwjson "application/json"
40text vwctype "Content-Type"
41text vwpview "/view"
42text vwpcmd "/cmd"
43text vwphlth "/health"
44text vwget "GET"
45text vwpost "POST"
46text vwkhd "{\"layout\":1,\"incarnation\":"
47text vwkver ",\"version\":"
48text vwksnap ",\"kind\":\"snapshot\""
49text vwkdel ",\"kind\":\"delta\",\"since\":"
50text vwkev ",\"events\":["
51text vwkauth ",\"authority\":"
52text vwnauth "generation,paused,journal_seq,journal_failed,fence,version,"
53text vwkq ",\"queue\":"
54text vwnq "waiting,ready,running,review,unknown,limit,review_limit,refused,"
55text vwkc ",\"commands\":"
56text vwnc "committed,refused,last_id,"
57text vwkw ",\"workers\":["
58text vwnw "pid,incarnation,ack,paused,"
59text vwku "],\"uncertain\":["
60text vwko "],\"objectives\":["
61text vwno "kind,k,n,a,state,turns,budget,phase,job,result,version,redirect,cancel,seq,"
62text vwkl "],\"limits\":"
63text vwnl "max_turns,deadline_s,max_tokens,max_objectives,action_slots,ring,"
64text vwkid "{\"id\":"
65text vwkst ",\"status\":\""
66text vwkst2 "\",\"version\":"
67text vwkjs ",\"journal_seq\":"
68text vwkinc ",\"incarnation\":"
69text vwsts "committed,stale,invalid,refused,duplicate,busy,pending,"
70text vwhlth "{\"ok\":true}"
71text vwqsin "since"
72text vwqinc "inc"
73text vwqid "id"
74text vwqop "op"
75text vwqtg "target"
76text vwqver "ver"
77text vwqa "a"
78text vwqb "b"
79text vwqc "c"
80text vwqd "d"
81global vwmap long 1
82global vwver ulong 1
83global vwcnt ulong 4
84
85note ======== the page (without vw_new every write is dropped and reads are 0,
86note so the driver also runs without a view)
87
88fn vw_g o ulong to ulong
89if le get vwmap 0 0
90ret 0
91end
92ret mapget get vwmap 0 mul o 8 8
93end
94
95fn vw_s o ulong v ulong to void
96if le get vwmap 0 0
97ret
98end
99r = mapset get vwmap 0 mul o 8 8 v
100end
101
102note Creator, before it forks anything: map the page for incarnation inc.
103note 0 or a mapping errno.
104fn vw_new inc ulong to long
105h = mapnew neg int 1 0 VWBYTES 3 33
106if le h 0
107ret h
108end
109put vwmap 0 h
110put vwver 0 0
111for i ulong 4
112put vwcnt i 0
113end
114vw_s 1 1
115vw_s 4 inc
116vw_s 5 ulong getpid
117vw_s 0 VWMAG
118ret 0
119end
120
121note Creator: append an event; its version. Words first, version last.
122fn vw_ev ty ulong t ulong x ulong a ulong b ulong to ulong
123v = add get vwver 0 1
124o = add VWRING mul and v 255 4
125vw_s add o 1 or or ty shl t 8 shl x 16
126vw_s add o 2 a
127vw_s add o 3 b
128vw_s o v
129put vwver 0 v
130vw_s 3 v
131ret v
132end
133
134fn vw_ver to ulong
135ret get vwver 0
136end
137
138note Creator: count c (0 admissions refused, 1 commands committed, 2
139note commands refused, 3 last command id).
140fn vw_cnt c ulong v ulong to void
141put vwcnt c v
142end
143
144fn vw_cget c ulong to ulong
145ret get vwcnt c
146end
147
148fn vw_begin to void
149vw_s 2 or vw_g 2 1
150end
151
152fn vw_end to void
153vw_s 2 add vw_g 2 1
154end
155
156note Creator, between vw_begin and vw_end: the generic sections.
157fn vw_gen c long to void
158vw_s 16 get augen 0
159vw_s 17 get scpause 0
160vw_s 18 get jseq 0
161vw_s 19 get jbad 0
162vw_s 20 get jfence 0
163for s ulong 4
164vw_s add 32 s sched_count add s 1 add s 1
165end
166vw_s 36 sched_count 6 6
167vw_s 37 get sclim 0
168vw_s 38 get sclim 1
169vw_s 39 get vwcnt 0
170vw_s 44 get vwcnt 1
171vw_s 45 get vwcnt 2
172vw_s 46 get vwcnt 3
173n = mapget c 48 8
174vw_s 48 n
175for w ulong n bound 8
176o = add 64 mul w 4
177vw_s o ulong get swpid w
178vw_s add o 1 get auinc w
179vw_s add o 2 auth_wctl w
180vw_s add o 3 au_cell w 104
181end
182vw_unc
183end
184
185note Uncertain jobs: UNKNOWN in the scheduler, or released by the journal
186note with outcome UNKNOWN (a class 1 effect whose fate nobody knows).
187fn vw_unc to void
188k = ulong 0
189for j ulong 64
190if and eq get sjst j 6 lt k 16
191vw_s add 97 k get sjid j
192k = add k 1
193end
194end
195for e ulong 256
196b = mul e 8
197if and and ne get jjob b 0 ne get jjob add b 4 0 lt k 16
198if eq and get jjob add b 5 255 0
199vw_s add 97 k get jjob add b 1
200k = add k 1
201end
202end
203end
204vw_s 96 k
205end
206
207note Creator: the posted command into d (id, op, target, version, a..d);
208note false when none is posted.
209fn vw_take d edit ulong 8 to bool
210if ne vw_g VWBOX 1
211ret false
212end
213for i ulong 8
214put d i vw_g add VWBOX add 1 i
215end
216ret true
217end
218
219note Creator: acknowledge the posted command.
220fn vw_ack st ulong v ulong js ulong to void
221vw_s add VWBOX 9 st
222vw_s add VWBOX 10 v
223vw_s add VWBOX 11 js
224vw_s add VWBOX 12 vw_g 4
225vw_s VWBOX 2
226end
227
228note Creator: fork the view process on 127.0.0.1:port. Its pid (it writes
229note the bound port to word 7 once listening), or a negative errno. The
230note child is not a worker: it drops the creator's life-pipe write end (so
231note workers still see the creator's death) and the journal descriptors.
232fn vw_spawn port ulong to long
233pid = fork
234if eq pid 0
235r = fclose get swpar 1
236r = fclose get jfd 0
237r = fclose get jlk 0
238exit vw_serve port
239end
240if gt pid 0
241vw_s 9 ulong pid
242end
243ret pid
244end
245
246note Creator: stop the view process and reap it.
247fn vw_stop to void
248pid = vw_g 9
249if eq pid 0
250ret
251end
252vw_s 6 1
253st = new uchar 8 0
254for t ulong 300
255r = waitpid int pid st 0 4 1
256if eq r long pid
257vw_s 9 0
258ret
259end
260x = sleepns 10000000
261end
262r = killpid int pid 9
263r = waitpid int pid st 0 4 0
264vw_s 9 0
265end
266
267note ======== the view process
268
269note Serve until the creator stops or is gone: 0, or 2 when the port
270note cannot be bound.
271fn vw_serve port ulong to int
272cs = new ulong 320 0
273io = new uchar 131072 0
274ob = new uchar 32768 0
275a = new uchar 16 0
276r = haddr4 a 2130706433 port
277lf = hlisten a 16
278if lt lf 0
279ret 2
280end
281vw_s 7 hport a
282me = vw_g 5
283for t ulong 1000000000
284if or ne vw_g 6 0 ne ulong getppid me
285break
286end
287c = haccept int lf 200000000
288if ge c 0
289vw_one int c cs io ob
290end
291end
292x = fclose int lf
293ret 0
294end
295
296note One request on connection fd, then close it.
297fn vw_one fd int cs edit ulong 320 io edit uchar 131072 ob edit uchar 32768 to void
298r = hinit cs io fd 1
299if lt r 0
300x = hclose cs
301ret
302end
303hsettmo cs 2000000000 2000000000
304hsetbdl cs 2000000000
305r = hreqrd cs io
306if le r 0
307if lt r 0
308x = hreject cs io r
309end
310x = hclose cs
311ret
312end
313vw_s 8 add vw_g 8 1
314r = vw_route cs io ob
315x = hclose cs
316end
317
318fn vw_route cs edit ulong 320 io edit uchar 131072 ob edit uchar 32768 to long
319if hispath cs io vwpview
320if hismeth cs io vwget
321n = vw_view cs io ob
322ret vw_send cs io ob n 200
323end
324ret hrsperr cs io 405
325end
326if hispath cs io vwpcmd
327if hismeth cs io vwpost
328ret vw_post cs io ob
329end
330ret hrsperr cs io 405
331end
332if hispath cs io vwphlth
333n = vw_cat ob 0 vwhlth
334ret vw_send cs io ob n 200
335end
336ret hrsperr cs io 404
337end
338
339fn vw_send cs edit ulong 320 io edit uchar 131072 ob view uchar 32768 n ulong code ulong to long
340put cs HKEEP 0
341r = hrspst cs io code
342r = hfe r hhname cs io vwctype
343r = hfe r hhval cs io vwjson
344r = hfe r hhend cs io 0 n
345r = hfe r hput cs io view part ob 0 n
346ret hfe r hwfin cs io
347end
348
349note Query parameter key of the request target as a decimal; all ones
350note when absent or not a number of 1..19 digits.
351fn vw_qnum cs edit ulong 320 io view uchar 131072 key view uchar * to ulong
352t = get cs HTOFF
353n = get cs HTLEN
354q = ulong 0
355for i ulong n
356if eq get io add t i 63
357q = add i 1
358break
359end
360end
361if eq q 0
362ret not ulong 0
363end
364p = q
365for it ulong 64
366if ge p n
367break
368end
369e = vw_seg io t p n
370if vw_keq io add t p key
371ret vw_qdec io add add t p add len key 1 sub sub e p add len key 1
372end
373p = add e 1
374end
375ret not ulong 0
376end
377
378note End of the query segment starting at p (index of '&' or n).
379fn vw_seg io view uchar 131072 t ulong p ulong n ulong to ulong
380for i ulong sub n p
381if eq get io add t add p i 38
382ret add p i
383end
384end
385ret n
386end
387
388note key= at io[o].
389fn vw_keq io view uchar 131072 o ulong key view uchar * to bool
390for i ulong len key
391if ne get io add o i get key i
392ret false
393end
394end
395ret eq get io add o len key 61
396end
397
398fn vw_qdec io view uchar 131072 o ulong m ulong to ulong
399if or eq m 0 gt m 19
400ret not ulong 0
401end
402v = ulong 0
403for i ulong m
404c = ulong get io add o i
405if or lt c 48 gt c 57
406ret not ulong 0
407end
408v = add mul v 10 sub c 48
409end
410ret v
411end
412
413note ======== JSON
414
415fn vw_cat o edit uchar 32768 p ulong a view uchar * to ulong
416for i ulong len a
417if lt add p i 32768
418put o add p i get a i
419end
420end
421ret hmin add p len a 32768
422end
423
424fn vw_dec o edit uchar 32768 p ulong v ulong to ulong
425d = new uchar 20 0
426k = ulong 20
427x = v
428for it ulong 20
429k = sub k 1
430put d k uchar add rem x 10 48
431x = div x 10
432if eq x 0
433break
434end
435end
436q = p
437for i ulong sub 20 k
438if lt q 32768
439put o q get d add k i
440end
441q = add q 1
442end
443ret hmin q 32768
444end
445
446fn vw_ch o edit uchar 32768 p ulong c ulong to ulong
447if lt p 32768
448put o p uchar c
449end
450ret hmin add p 1 32768
451end
452
453note "name": for the k-th comma-terminated name in names.
454fn vw_key o edit uchar 32768 p ulong names view uchar * k ulong to ulong
455s = ulong 0
456m = ulong 0
457for i ulong len names
458if eq get names i 44
459if eq m k
460q = vw_ch o p 34
461q = vw_cat o q view part names s sub i s
462q = vw_ch o q 34
463ret vw_ch o q 58
464end
465m = add m 1
466s = add i 1
467end
468end
469ret p
470end
471
472note {"name":s[b],...}, one member per comma-terminated name.
473fn vw_obj o edit uchar 32768 p ulong names view uchar * s view ulong 1024 b ulong to ulong
474n = ulong 0
475for i ulong len names
476if eq get names i 44
477n = add n 1
478end
479end
480q = vw_ch o p 123
481for k ulong n
482if gt k 0
483q = vw_ch o q 44
484end
485q = vw_key o q names k
486q = vw_dec o q get s add b k
487end
488ret vw_ch o q 125
489end
490
491note A stable copy of words 0..1023 (retries while the creator writes).
492fn vw_load s edit ulong 1024 to bool
493for t ulong 1000
494a = vw_g 2
495if eq and a 1 0
496for i ulong 1024
497put s i vw_g i
498end
499if eq vw_g 2 a
500ret true
501end
502end
503x = sleepns 100000
504end
505ret false
506end
507
508note The view: a delta since the client's version when the client holds
509note this incarnation and every event since it is still in the ring, else
510note a snapshot. The length.
511fn vw_view cs edit ulong 320 io view uchar 131072 ob edit uchar 32768 to ulong
512s = new ulong 1024 0
513sv = vw_qnum cs io vwqsin
514ic = vw_qnum cs io vwqinc
515x = vw_load s
516v = get s 3
517p = vw_cat ob 0 vwkhd
518p = vw_dec ob p get s 4
519p = vw_cat ob p vwkver
520p = vw_dec ob p v
521if and and eq ic get s 4 le sv v le sub v sv 255
522ret vw_delta ob p sv v
523end
524ret vw_snap ob p s
525end
526
527fn vw_delta ob edit uchar 32768 p0 ulong sv ulong v ulong to ulong
528p = vw_cat ob p0 vwkdel
529p = vw_dec ob p sv
530p = vw_cat ob p vwkev
531for k ulong sub v sv
532e = add sv add k 1
533o = add VWRING mul and e 255 4
534if gt k 0
535p = vw_ch ob p 44
536end
537p = vw_evj ob p o e
538end
539p = vw_ch ob p 93
540ret vw_ch ob p 125
541end
542
543note [version,type,target,x,a,b] of the event at word o.
544fn vw_evj ob edit uchar 32768 p0 ulong o ulong e ulong to ulong
545w = vw_g add o 1
546p = vw_ch ob p0 91
547p = vw_dec ob p vw_g o
548p = vw_ch ob p 44
549p = vw_dec ob p and w 255
550p = vw_ch ob p 44
551p = vw_dec ob p and shr w 8 255
552p = vw_ch ob p 44
553p = vw_dec ob p shr w 16
554p = vw_ch ob p 44
555p = vw_dec ob p vw_g add o 2
556p = vw_ch ob p 44
557p = vw_dec ob p vw_g add o 3
558ret vw_ch ob p 93
559end
560
561fn vw_snap ob edit uchar 32768 p0 ulong s view ulong 1024 to ulong
562p = vw_cat ob p0 vwksnap
563p = vw_cat ob p vwkauth
564p = vw_obj ob p vwnauth s 16
565p = vw_cat ob p vwkq
566p = vw_obj ob p vwnq s 32
567p = vw_cat ob p vwkc
568p = vw_obj ob p vwnc s 44
569p = vw_cat ob p vwkw
570for w ulong get s 48 bound 8
571if gt w 0
572p = vw_ch ob p 44
573end
574p = vw_obj ob p vwnw s add 64 mul w 4
575end
576p = vw_cat ob p vwku
577for k ulong get s 96 bound 16
578if gt k 0
579p = vw_ch ob p 44
580end
581p = vw_dec ob p get s add 97 k
582end
583p = vw_cat ob p vwko
584for i ulong get s 128 bound 4
585if gt i 0
586p = vw_ch ob p 44
587end
588p = vw_obj ob p vwno s add 136 mul i 16
589end
590p = vw_cat ob p vwkl
591p = vw_obj ob p vwnl s 200
592ret vw_ch ob p 125
593end
594
595note ======== commands
596
597note POST /cmd?id=&op=&target=&inc=&ver=&a=&b=&c=&d=: post it to the creator
598note and wait up to 5 s for the acknowledgement. A command made for another
599note incarnation (inc) is stale without reaching the creator, since its ver
600note counts another incarnation's events. 503 while another command is
601note posted and not yet taken, 504 when no acknowledgement came in time
602note (the command may still commit: post it again with the same id).
603fn vw_post cs edit ulong 320 io edit uchar 131072 ob edit uchar 32768 to long
604d = new ulong 8 0
605id = vw_qnum cs io vwqid
606put d 0 vw_qnum cs io vwqop
607put d 1 vw_qnum cs io vwqtg
608put d 2 vw_qnum cs io vwqver
609put d 3 vw_qnum cs io vwqa
610put d 4 vw_qnum cs io vwqb
611put d 5 vw_qnum cs io vwqc
612put d 6 vw_qnum cs io vwqd
613if or eq id 0 eq id not ulong 0
614ret hrsperr cs io 400
615end
616st = ulong 1
617bx = false
618if eq vw_qnum cs io vwqinc vw_g 4
619st = vw_box id d
620bx = lt st 5
621end
622n = vw_ackj ob id st bx
623ret vw_send cs io ob n vw_code st
624end
625
626note Post command id with d[0..6] = op, target, version, a..d; the status
627note (0..4 from the creator, 5 busy, 6 pending).
628fn vw_box id ulong d view ulong 8 to ulong
629b = vw_g VWBOX
630if eq b 1
631ret 5
632end
633vw_s add VWBOX 1 id
634for i ulong 7
635vw_s add VWBOX add 2 i get d i
636end
637vw_s VWBOX 1
638for t ulong 5000
639if eq vw_g VWBOX 2
640st = vw_g add VWBOX 9
641vw_s VWBOX 0
642ret st
643end
644x = sleepns 1000000
645end
646ret 6
647end
648
649fn vw_code st ulong to ulong
650if or eq st 0 eq st 4
651ret 200
652end
653if eq st 1
654ret 409
655end
656if eq st 2
657ret 400
658end
659if eq st 3
660ret 500
661end
662if eq st 5
663ret 503
664end
665ret 504
666end
667
668note The acknowledgement: the creator's version and journal seq when bx,
669note else the current version (the view's own verdict) and 0.
670fn vw_ackj ob edit uchar 32768 id ulong st ulong bx bool to ulong
671p = vw_cat ob 0 vwkid
672p = vw_dec ob p id
673p = vw_cat ob p vwkst
674p = vw_name ob p vwsts st
675p = vw_cat ob p vwkst2
676v = vw_g 3
677js = ulong 0
678if bx
679v = vw_g add VWBOX 10
680js = vw_g add VWBOX 11
681end
682p = vw_dec ob p v
683p = vw_cat ob p vwkjs
684p = vw_dec ob p js
685p = vw_cat ob p vwkinc
686p = vw_dec ob p vw_g 4
687ret vw_ch ob p 125
688end
689
690note The k-th comma-terminated name of names, unquoted.
691fn vw_name o edit uchar 32768 p ulong names view uchar * k ulong to ulong
692s = ulong 0
693m = ulong 0
694for i ulong len names
695if eq get names i 44
696if eq m k
697ret vw_cat o p view part names s sub i s
698end
699m = add m 1
700s = add i 1
701end
702end
703ret p
704end