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 / replay-gate.om

5548 lines · 124863 bytes Raw Blame History
1note Omega Node transport, concatenate with a program-mode consumer.
2note Trusted forked processes only. This is transport, not write authority.
3note One publisher PID, 64 immutable records per epoch; reuse only via node_reset.
4note Layout: 4096 byte header then 64 slots of 4160 bytes.
5note Header u64: magic, ABI, published count, stop, publisher PID, epoch.
6note Slot u64: version, length, topic mask; payload begins at offset 64.
7const NCAP ulong 64
8const NBYTES ulong 270336
9const NMAGIC ulong 1313817669
10
11fn node_new to long
12h = mapnew neg int 1 0 NBYTES 3 33
13if le h 0
14ret h
15end
16r = mapset h 8 8 1
17r = mapset h 32 8 ulong getpid
18r = mapset h 0 8 NMAGIC
19ret h
20end
21
22fn node_valid h long to bool
23ret and eq mapget h 0 8 NMAGIC eq mapget h 8 8 1
24end
25
26fn node_offset v ulong to ulong
27ret add 4096 mul sub v 1 4160
28end
29
30note Return version, or -1 stopped, -2 full, -3 length, -4 topic,
31note -5 invalid header, -6 wrong publisher. Refusals do not publish.
32fn node_publish h long topics ulong src view uchar 4096 n ulong to long
33if not node_valid h
34ret neg long 5
35end
36if ne mapget h 32 8 ulong getpid
37ret neg long 6
38end
39if ne mapget h 24 8 0
40ret neg long 1
41end
42if gt n 4096
43ret neg long 3
44end
45if eq topics 0
46ret neg long 4
47end
48v = mapget h 16 8
49if gt v NCAP
50ret neg long 5
51end
52if eq v NCAP
53ret neg long 2
54end
55v = add v 1
56off = node_offset v
57r = mapwrite h add off 64 src 0 n
58r = mapset h add off 8 8 n
59r = mapset h add off 16 8 topics
60note mapset has barriers before and after its aligned store.
61r = mapset h off 8 v
62r = mapset h 16 8 v
63ret long v
64end
65
66note Header and record checks precede copying. No partial publication is read.
67note Returns payload length, -5 invalid header/record, -7 unavailable version.
68fn node_read h long v ulong dst edit uchar 4096 to long
69if not node_valid h
70ret neg long 5
71end
72if or eq v 0 gt v NCAP
73ret neg long 7
74end
75latest = mapget h 16 8
76if gt latest NCAP
77ret neg long 5
78end
79if gt v latest
80ret neg long 7
81end
82off = node_offset v
83if ne mapget h off 8 v
84ret neg long 5
85end
86n = mapget h add off 8 8
87if gt n 4096
88ret neg long 5
89end
90ret mapread h add off 64 dst 0 n
91end
92
93note Return the next matching version after cursor, zero if caught up,
94note or negative refusal. Caller advances its cursor through returned versions.
95fn node_next h long cursor ulong topics ulong to long
96if not node_valid h
97ret neg long 5
98end
99if or gt cursor NCAP eq topics 0
100ret neg long 7
101end
102latest = mapget h 16 8
103if or gt latest NCAP gt cursor latest
104ret neg long 5
105end
106v = cursor
107note At most NCAP published slots can be examined.
108while lt v latest bound 64
109v = add v 1
110off = node_offset v
111if ne mapget h off 8 v
112ret neg long 5
113end
114if ne and mapget h add off 16 8 topics 0
115ret long v
116end
117end
118ret 0
119end
120
121fn node_stop h long to long
122if not node_valid h
123ret neg long 5
124end
125if ne mapget h 32 8 ulong getpid
126ret neg long 6
127end
128ret mapset h 24 8 1
129end
130
131note Creator only. Retire every record so a pool can reuse the arena for a new
132note epoch. The caller must first prove no reader holds the old epoch (pool.om).
133note The count drops to zero before any record version is cleared.
134fn node_reset h long epoch ulong to long
135if not node_valid h
136ret neg long 5
137end
138if ne mapget h 32 8 ulong getpid
139ret neg long 6
140end
141if eq epoch 0
142ret neg long 7
143end
144r = mapset h 16 8 0
145for i ulong NCAP
146r = mapset h node_offset add i 1 8 0
147end
148r = mapset h 24 8 0
149r = mapset h 40 8 epoch
150ret 0
151end
152
153fn node_epoch h long to ulong
154ret mapget h 40 8
155end
156
157note The latest published version of the arena's current epoch (0 when empty).
158fn node_count h long to ulong
159ret mapget h 16 8
160end
161
162note One nonblocking eventfd per consumer. Saturation means already readable.
163note Interrupted operations retry at most 64 times, then return EINTR.
164fn node_notify fd int to long
165buf = new uchar 8 0
166put buf 0 1
167for i ulong 64
168r = write fd buf 0 8
169if eq r 8
170ret 0
171end
172if eq r neg long 11
173ret 0
174end
175if ne r neg long 4
176if lt r 0
177ret r
178end
179ret neg long 5
180end
181end
182ret neg long 4
183end
184
185fn node_drain fd int to long
186buf = new uchar 8 0
187for i ulong 64
188r = read fd buf 0 8
189if or eq r 8 eq r neg long 11
190ret 0
191end
192if ne r neg long 4
193if lt r 0
194ret r
195end
196ret neg long 5
197end
198end
199ret neg long 4
200end
201
202fn node_pollfd p edit uchar 16 off ulong fd int to void
203for i ulong 4
204put p add off i uchar and shr uint fd uint mul i 8 255
205end
206put p add off 4 1
207end
208
209note Returns 2 stop, 1 wake, 0 timeout or negative errno.
210note Control takes priority when both descriptors are ready.
211note EINTR returns to caller so it can recompute its monotonic deadline.
212fn node_wait h long wake int stop int interval long to long
213p = new uchar 16 0
214if not node_valid h
215ret neg long 5
216end
217if or lt wake 0 lt stop 0
218ret neg long 9
219end
220if eq wake stop
221ret neg long 22
222end
223if ne mapget h 24 8 0
224ret 2
225end
226node_pollfd p 0 wake
227node_pollfd p 8 stop
228r = pollfds p 0 16 interval
229if lt r 0
230ret r
231end
232if ne mapget h 24 8 0
233ret 2
234end
235if ne and get p 14 56 0
236ret neg long 5
237end
238if ne and get p 14 1 0
239r = node_drain stop
240if lt r 0
241ret r
242end
243ret 2
244end
245if ne and get p 6 56 0
246ret neg long 5
247end
248if ne and get p 6 1 0
249r = node_drain wake
250if lt r 0
251ret r
252end
253ret 1
254end
255ret 0
256end
257note Omega Node epoch pool. Concatenate after transport.om, before super.om.
258note K arenas (1..8) and one control page are mapped before any worker fork,
259note so every forked worker inherits the same shared mappings.
260note Single writer: the creator writes the current epoch, the arena table and
261note every arena record. Worker w alone writes hold word w while it lives;
262note the creator clears hold w only after it has reaped worker w.
263note Control u64: 0 magic, 8 ABI, 16 current epoch, 24 stop, 32 creator PID,
264note 40 arena count K, 48 worker slots W, 64+8k arena handle k,
265note 128+8k epoch of arena k (0 free), 256+8w hold of worker w (0 none),
266note 768+8w worker PID, 1280+8w worker state (0 empty, 1 live, 2 reaped).
267const PMAGIC ulong 1347374924
268const PCAP ulong 8
269const PWCAP ulong 64
270const PBYTES ulong 4096
271
272fn pool_valid c long to bool
273ret and eq mapget c 0 8 PMAGIC eq mapget c 8 8 1
274end
275
276fn pool_mine c long to bool
277ret eq mapget c 32 8 ulong getpid
278end
279
280fn pool_arena c long k ulong to long
281ret long mapget c add 64 mul k 8 8
282end
283
284note Drop the first n arenas and the control page of a pool being refused.
285fn pool_unwind c long n ulong to long
286for i ulong n bound 8
287r = mapdrop pool_arena c i
288end
289ret mapdrop c
290end
291
292note Return the control handle, or -7 bad size, or a negative mapping errno.
293note Arena 0 starts as epoch 1. Call before forking any worker.
294fn pool_new k ulong w ulong to long
295if or eq k 0 gt k PCAP
296ret neg long 7
297end
298if or eq w 0 gt w PWCAP
299ret neg long 7
300end
301c = mapnew neg int 1 0 PBYTES 3 33
302if le c 0
303ret c
304end
305r = mapset c 32 8 ulong getpid
306r = mapset c 40 8 k
307r = mapset c 48 8 w
308for i ulong k bound 8
309h = node_new
310if le h 0
311r = pool_unwind c i
312ret h
313end
314r = mapset c add 64 mul i 8 8 ulong h
315end
316r = node_reset pool_arena c 0 1
317r = mapset c 128 8 1
318r = mapset c 16 8 1
319r = mapset c 8 8 1
320r = mapset c 0 8 PMAGIC
321ret c
322end
323
324note Arena index holding epoch e, or -5 when no arena holds it.
325fn pool_slot c long e ulong to long
326k = mapget c 40 8
327if gt k PCAP
328ret neg long 5
329end
330for i ulong k bound 8
331if eq mapget c add 128 mul i 8 8 e
332ret long i
333end
334end
335ret neg long 5
336end
337
338note Creator: the arena of the current epoch, or a negative refusal.
339fn pool_current c long to long
340if not pool_valid c
341ret neg long 5
342end
343k = pool_slot c mapget c 16 8
344if lt k 0
345ret k
346end
347ret pool_arena c ulong k
348end
349
350fn pool_epoch c long to ulong
351ret mapget c 16 8
352end
353
354note Worker step: store hold e for worker w, then read the epoch again. mapset
355note and mapget fence both sides, so a creator that moved the epoch before it
356note scanned holds either sees this hold or this read sees the move. Returns
357note the arena of e, -8 when the epoch moved, or -5 when no arena holds e.
358note Every refusal clears the hold.
359fn pool_hold c long w ulong e ulong to long
360if ge w mapget c 48 8
361ret neg long 7
362end
363hw = add 256 mul w 8
364r = mapset c hw 8 e
365if ne mapget c 16 8 e
366r = mapset c hw 8 0
367ret neg long 8
368end
369k = pool_slot c e
370if lt k 0
371r = mapset c hw 8 0
372ret k
373end
374h = pool_arena c ulong k
375if ne node_epoch h e
376r = mapset c hw 8 0
377ret neg long 5
378end
379ret h
380end
381
382note Worker w holds the current epoch and returns its arena. Returns -1
383note stopped, -5 invalid, -7 bad worker, -8 the epoch kept moving.
384fn pool_acquire c long w ulong to long
385if not pool_valid c
386ret neg long 5
387end
388if ge w mapget c 48 8
389ret neg long 7
390end
391for t ulong 16
392if ne mapget c 24 8 0
393r = mapset c add 256 mul w 8 8 0
394ret neg long 1
395end
396h = pool_hold c w mapget c 16 8
397if ne h neg long 8
398ret h
399end
400end
401ret neg long 8
402end
403
404note Worker w gives up its hold. Its old arena must not be read afterwards.
405fn pool_release c long w ulong to long
406if ge w mapget c 48 8
407ret neg long 7
408end
409ret mapset c add 256 mul w 8 8 0
410end
411
412fn pool_held c long e ulong to bool
413n = mapget c 48 8
414for w ulong n bound 64
415if eq mapget c add 256 mul w 8 8 e
416ret true
417end
418end
419ret false
420end
421
422note Creator step: retire arena i and open epoch n in it, then publish n.
423fn pool_reuse c long i ulong n ulong to long
424off = add 128 mul i 8
425r = mapset c off 8 0
426r = node_reset pool_arena c i n
427if lt r 0
428ret r
429end
430r = mapset c off 8 n
431r = mapset c 16 8 n
432ret long n
433end
434
435note Creator step: an arena that is free or whose older epoch no worker holds,
436note or -9. Call only after the current epoch has moved past that arena.
437fn pool_pick c long to long
438e = mapget c 16 8
439k = mapget c 40 8
440for i ulong k bound 8
441ek = mapget c add 128 mul i 8 8
442if ne ek e
443if or eq ek 0 not pool_held c ek
444ret long i
445end
446end
447end
448ret neg long 9
449end
450
451note Creator: open the next epoch in a free or unheld arena and make it current.
452note Returns the new epoch, -1 stopped, -5 invalid, -6 not creator, or -9 when
453note every other arena is still held: the pool fails closed, nothing changes.
454fn pool_roll c long to long
455if not pool_valid c
456ret neg long 5
457end
458if not pool_mine c
459ret neg long 6
460end
461if ne mapget c 24 8 0
462ret neg long 1
463end
464k = pool_pick c
465if lt k 0
466ret k
467end
468ret pool_reuse c ulong k add mapget c 16 8 1
469end
470
471note Creator: publish to the current epoch; when it is full, roll once and retry.
472note Returns the version, or the refusal of the publish or of the roll.
473fn pool_publish c long topics ulong src view uchar 4096 n ulong to long
474h = pool_current c
475if lt h 0
476ret h
477end
478v = node_publish h topics src n
479if ne v neg long 2
480ret v
481end
482r = pool_roll c
483if lt r 0
484ret r
485end
486h = pool_current c
487if lt h 0
488ret h
489end
490ret node_publish h topics src n
491end
492
493fn pool_stop c long to long
494if not pool_valid c
495ret neg long 5
496end
497if not pool_mine c
498ret neg long 6
499end
500ret mapset c 24 8 1
501end
502
503fn pool_stopped c long to bool
504ret ne mapget c 24 8 0
505end
506
507note Creator: record worker w's PID and state for observers.
508fn pool_setw c long w ulong pid ulong st ulong to long
509if not pool_mine c
510ret neg long 6
511end
512if ge w mapget c 48 8
513ret neg long 7
514end
515r = mapset c add 768 mul w 8 8 pid
516ret mapset c add 1280 mul w 8 8 st
517end
518
519note Creator: clear hold w. Only after worker w has been reaped.
520fn pool_clear c long w ulong to long
521if not pool_mine c
522ret neg long 6
523end
524if ge w mapget c 48 8
525ret neg long 7
526end
527ret mapset c add 256 mul w 8 8 0
528end
529
530note Drop every mapping of the pool in this process. Other processes keep
531note their own inherited mappings until they drop them or exit.
532fn pool_free c long to long
533ret pool_unwind c mapget c 40 8
534end
535note Omega Node worker supervision. Concatenate after transport.om and pool.om.
536note The creator forks trusted workers from its own binary: sup_spawn returns 0
537note in the child, which runs its worker function and exits with its result.
538note Death is seen without polling PIDs. Worker w alone holds the write end of
539note its close-on-exec life pipe, so its exit hangs up the creator's read end.
540note Every worker holds the read end of the creator's life pipe, whose only
541note writer is the creator, so creator death hangs up every worker.
542note Per worker: wake, stop and ack eventfds, kept across respawns.
543note Workers must not exec: exec closes the life pipe and reads as death.
544global swpid long 64
545global swwake int 64
546global swstop int 64
547global swack int 64
548global swlife int 64
549global swst ulong 64
550global swpw ulong 64
551global swpar int 2
552global swme ulong 1
553global swws uchar 4
554global swpf uchar 8
555global swpoll uchar 1024
556
557fn sup_fd off ulong to int
558v = ulong get swpf off
559v = or v shl ulong get swpf add off 1 8
560v = or v shl ulong get swpf add off 2 16
561v = or v shl ulong get swpf add off 3 24
562ret int v
563end
564
565note Poll record j at swpoll offset off; a negative fd is stored as itself
566note (bit pattern) so the kernel ignores that record.
567fn sup_gpfd off ulong fd int to void
568u = bitcast uint fd
569for i ulong 4
570put swpoll add off i uchar and shr u uint mul i 8 255
571end
572put swpoll add off 4 1
573put swpoll add off 5 0
574put swpoll add off 6 0
575put swpoll add off 7 0
576end
577
578fn sup_lpfd p edit uchar 24 off ulong fd int to void
579for i ulong 4
580put p add off i uchar and shr uint fd uint mul i 8 255
581end
582put p add off 4 1
583end
584
585note Read and reset an eventfd counter: the number of hints since the last
586note take, 0 when none is pending, or a negative errno.
587fn sup_take fd int to long
588buf = new uchar 8 0
589for i ulong 64
590r = read fd buf 0 8
591if eq r 8
592v = ulong 0
593for j ulong 8
594v = or v shl ulong get buf j mul j 8
595end
596ret long and v 9223372036854775807
597end
598if eq r neg long 11
599ret 0
600end
601if ne r neg long 4
602if lt r 0
603ret r
604end
605ret neg long 5
606end
607end
608ret neg long 4
609end
610
611fn sup_closeall to void
612for i ulong 64
613if ge get swwake i 0
614r = fclose get swwake i
615end
616if ge get swstop i 0
617r = fclose get swstop i
618end
619if ge get swack i 0
620r = fclose get swack i
621end
622if ge get swlife i 0
623r = fclose get swlife i
624end
625put swwake i neg int 1
626put swstop i neg int 1
627put swack i neg int 1
628put swlife i neg int 1
629end
630for i ulong 2
631if ge get swpar i 0
632r = fclose get swpar i
633end
634put swpar i neg int 1
635end
636end
637
638note Creator: create the creator life pipe and three eventfds per worker slot.
639note Call once after pool_new and before sup_spawn. 0 or a negative errno;
640note a failure closes everything this call opened.
641fn sup_init c long to long
642if not pool_mine c
643ret neg long 6
644end
645for i ulong 64
646put swpid i 0
647put swst i 0
648put swwake i neg int 1
649put swstop i neg int 1
650put swack i neg int 1
651put swlife i neg int 1
652end
653put swme 0 0
654put swpar 0 neg int 1
655put swpar 1 neg int 1
656r = pipefds swpf 0 8 524288
657if lt r 0
658ret r
659end
660put swpar 0 sup_fd 0
661put swpar 1 sup_fd 4
662n = mapget c 48 8
663for i ulong n bound 64
664a = eventfd uint 0
665if lt a 0
666sup_closeall
667ret a
668end
669put swwake i int a
670a = eventfd uint 0
671if lt a 0
672sup_closeall
673ret a
674end
675put swstop i int a
676a = eventfd uint 0
677if lt a 0
678sup_closeall
679ret a
680end
681put swack i int a
682end
683ret 0
684end
685
686note Creator: fork worker w. Returns 0 in the child, the child PID in the
687note creator, -7 bad slot, -16 slot still live, or a negative errno. The
688note child keeps only its own life write end; the creator keeps the read end.
689fn sup_spawn c long w ulong to long
690if not pool_mine c
691ret neg long 6
692end
693if ge w mapget c 48 8
694ret neg long 7
695end
696if ne get swpid w 0
697ret neg long 16
698end
699r = pipefds swpf 0 8 524288
700if lt r 0
701ret r
702end
703rd = sup_fd 0
704wr = sup_fd 4
705pid = fork
706if eq pid 0
707r = fclose get swpar 1
708put swpar 1 neg int 1
709r = fclose rd
710put swlife w wr
711put swme 0 add w 1
712ret 0
713end
714r = fclose wr
715if lt pid 0
716r = fclose rd
717ret pid
718end
719put swpid w pid
720put swlife w rd
721r = pool_setw c w ulong pid 1
722ret pid
723end
724
725note Worker: 3 creator gone, 2 stop, 1 wake hint, 0 timeout, negative errno.
726note Creator loss outranks stop, which outranks work. A set stop flag makes
727note the poll nonblocking. Hints are drained; rescan shared state after 1.
728fn sup_wwait c long w ulong interval long to long
729p = new uchar 24 0
730if ge w 64
731ret neg long 7
732end
733iv = interval
734if pool_stopped c
735iv = 0
736end
737sup_lpfd p 0 get swwake w
738sup_lpfd p 8 get swstop w
739sup_lpfd p 16 get swpar 0
740r = pollfds p 0 24 iv
741if lt r 0
742ret r
743end
744if ne get p 22 0
745ret 3
746end
747if pool_stopped c
748ret 2
749end
750if ne and get p 14 56 0
751ret neg long 5
752end
753if ne and get p 14 1 0
754r = node_drain get swstop w
755ret 2
756end
757if ne and get p 6 56 0
758ret neg long 5
759end
760if ne and get p 6 1 0
761r = node_drain get swwake w
762if lt r 0
763ret r
764end
765ret 1
766end
767ret 0
768end
769
770note Worker or creator: post a hint on worker w's ack eventfd.
771fn sup_ack w ulong to long
772if ge w 64
773ret neg long 7
774end
775ret node_notify get swack w
776end
777
778note Creator: wake worker w with a work hint.
779fn sup_wake w ulong to long
780if ge w 64
781ret neg long 7
782end
783if le get swpid w 0
784ret neg long 3
785end
786ret node_notify get swwake w
787end
788
789fn sup_live c long to ulong
790n = mapget c 48 8
791k = ulong 0
792for w ulong n bound 64
793if gt get swpid w 0
794k = add k 1
795end
796end
797ret k
798end
799
800note Creator: reap worker w, then release what it held. A worker that has not
801note exited within about one second gets SIGKILL first. Only after the exact
802note PID is reaped are its hold, PID and life pipe released. Returns the raw
803note wait status, -3 no live worker, or the waitpid errno (nothing released).
804fn sup_reap c long w ulong to long
805if ge w 64
806ret neg long 7
807end
808pid = get swpid w
809if le pid 0
810ret neg long 3
811end
812done = false
813for t ulong 100
814r = waitpid int pid swws 0 4 1
815if eq r pid
816done = true
817break
818end
819if ne r 0
820ret r
821end
822r = sleepns 10000000
823end
824if not done
825r = killpid int pid 9
826r = waitpid int pid swws 0 4 0
827if ne r pid
828ret r
829end
830end
831st = ulong get swws 0
832st = or st shl ulong get swws 1 8
833put swst w st
834r = fclose get swlife w
835put swlife w neg int 1
836put swpid w 0
837r = pool_clear c w
838r = pool_setw c w 0 2
839ret long st
840end
841
842note Creator: wait up to interval ns for deaths and, when acks is true, acks.
843note Ack counts are left pending for sup_take. Every worker whose life pipe
844note hung up is reaped. Returns the number reaped, or a negative errno.
845fn sup_pollx c long interval long acks bool to long
846n = mapget c 48 8
847m = ulong 0
848for w ulong n bound 64
849if gt get swpid w 0
850o = mul m 16
851if acks
852sup_gpfd o get swack w
853else
854sup_gpfd o neg int 1
855end
856sup_gpfd add o 8 get swlife w
857put swpw m w
858m = add m 1
859end
860end
861if eq m 0
862ret 0
863end
864r = pollfds swpoll 0 mul m 16 interval
865if lt r 0
866ret r
867end
868dead = long 0
869for j ulong m bound 64
870o = add mul j 16 14
871if ne get swpoll o 0
872r = sup_reap c get swpw j
873if lt r 0
874ret r
875end
876dead = add dead 1
877end
878end
879ret dead
880end
881
882note Creator: wait up to interval ns for acks and deaths (see sup_pollx).
883fn sup_poll c long interval long to long
884ret sup_pollx c interval true
885end
886
887note Creator: stop admission, post every live worker's stop eventfd, then reap
888note until all have exited or interval ns pass; survivors get SIGKILL and are
889note reaped. Returns how many had to be killed, or a refusal.
890fn sup_stop c long interval long to long
891r = pool_stop c
892if lt r 0
893ret r
894end
895n = mapget c 48 8
896for w ulong n bound 64
897if gt get swpid w 0
898r = node_notify get swstop w
899end
900end
901dl = add monons interval
902for t ulong 100000
903if eq sup_live c 0
904ret 0
905end
906now = monons
907if ge now dl
908break
909end
910r = sup_pollx c sub dl now false
911if lt r 0
912if ne r neg long 4
913break
914end
915end
916end
917killed = long 0
918for w ulong n bound 64
919if gt get swpid w 0
920r = killpid int get swpid w 9
921r = sup_reap c w
922killed = add killed 1
923end
924end
925ret killed
926end
927note Omega Node cooperative scheduling (DRA-287). Concatenate after
928note transport.om, pool.om and super.om, before one program with fn main.
929note One creator admits, assigns, collects and reviews jobs. Workers never
930note write each other's state, and no step waits for every worker.
931note Scheduling page: one 128-byte cell per worker w at 128*w.
932note Creator writes 0 assigned seq, 8 job id, 16 input epoch, 24 input
933note version, 32 interest topics, 40 wake stamp (monons at the last post).
934note Worker writes 64 done seq, 72 outcome, 80 wakes, 88 last wake latency
935note ns, 96 largest wake latency ns.
936note Outbox: worker w's result bytes at 4096*w of a second mapping, written
937note before its done seq. Each word has one writer; the creator writes a
938note worker word only after it has reaped that worker.
939note Job states: 0 free, 1 waiting for deps, 2 ready, 3 running, 4 in review,
940note 6 UNKNOWN (its worker died mid-job and the job is not safe to rerun).
941note
942note Resources and selection policy (P-DRA-14, DRA-395). A request is one
943note ulong of four 16-bit counts, kind k at bits 16k..16k+15: kind 0 CPU
944note slots, 1 memory MiB, 2 inference slots, 3 caller-defined. scap holds
945note the configured capacity of each kind, suse the sum of the needs of the
946note jobs that hold resources (sjheld 1). A job takes its whole need when it
947note is assigned and gives it back when its worker reports done or is reaped,
948note never in part. Every job also needs one idle live worker.
949note sjarr: admission order (waiting age; kept across reruns). sjrdy: order
950note in which the job last became ready. sjbyp: times it was bypassed.
951note Decisions read only these logical counters, never the clock.
952note Policy 1, fair fitting (DRA-396): a pick first probes the reserved job;
953note otherwise it scans a window of win slots from a persistent cursor and
954note takes the oldest admitted ready job that fits. The cursor moves on by
955note win after every window scan, so every slot is examined within
956note ceil(64/win) window scans. A ready job older than a job assigned in
957note the same event was bypassed; once the oldest such job reaches the
958note bypass budget it is reserved: it is offered first, and no other job may
959note use, of any kind, the capacity it still lacks (others may use only
960note free capacity beyond its need). Neither the free capacity it can reach
961note nor its age ever shrinks, so it runs once running work releases enough
962note (liveness assumes: every running job ends, by completion or by its
963note lease, at least one worker stays live, results in review are reviewed,
964note and dispatch is not paused). Requests over capacity are refused at
965note admission.
966note Admission: room is the smaller of the admission headroom and the free
967note table slots. A refused caller holds a ticket; while tickets are held
968note only the first holder may enter, so holders enter in ticket order
969note before anyone new. A ticket not asked about for more than ttl dispatch
970note events lapses (asked at event e, it survives while e + ttl is at least
971note the current event count). An absent head blocks later holders until
972note its ticket lapses; a paused dispatch counts no events, so the block
973note lasts until dispatch resumes.
974note Leases: a running job with a lease is reported once by sched_overdue
975note after its lease passes; the authority then cancels or kills it, which
976note releases its resources.
977note
978note Policy 2, goal-aware fair fitting (DRA-397): policy 1 with a different
979note order among the examined jobs that fit: larger unblock value first (the
980note waiting jobs, to depth 2, that the job's declared topics would make
981note ready), then smaller declared estimate, then older admission. The
982note reservation, the hold-back, feasibility and the bypass budget are
983note those of policy 1 and come first, so ranking cannot starve old work.
984note Values are rescored only when the graph version moved (a job became
985note ready, a waiting job was admitted, a job was cancelled, a declaration
986note changed), once per pick however many changes came before it.
987note Completions, releases and lease expiry replan (every step dispatches)
988note without rescoring. A proposal names a job by id and the graph version
989note it was computed at, and is rejected when that version is no longer
990note current; the version carries the scheduler's incarnation, so a
991note proposal made for another scheduler is never current here.
992const SJCAP ulong 64
993const SPVER ulong 3
994global scmap long 2
995global sclim ulong 2
996global sjst ulong 64
997global sjdep ulong 64
998global sjid ulong 64
999global sjw ulong 64
1000global sjatt ulong 64
1001global sjfl ulong 64
1002global sjout ulong 64
1003global sjneed ulong 64
1004global sjheld ulong 64
1005global sjarr ulong 64
1006global sjrdy ulong 64
1007global sjbyp ulong 64
1008global sjlse ulong 64
1009global sjdue ulong 64
1010global sttk ulong 64
1011global stts ulong 64
1012global scbusy ulong 64
1013global scseq ulong 64
1014global swcur ulong 2
1015global scpause ulong 1
1016global scap ulong 4
1017global suse ulong 4
1018note Policy words: 0 policy (0 FIFO head-of-line, 1 fair fitting), 1
1019note candidate window in slots, 2 dispatch budget per event, 3 bypass
1020note budget, 4 cursor, 5 reserved job slot + 1 (0 none), 6 tickets held,
1021note 7 ticket lifetime in dispatch events.
1022global scpol ulong 8
1023note Counters (sched_stat): 0 admissions, 1 ready stamps, 2 dispatch events,
1024note 3 candidate slot reads, 4 jobs assigned, 5 bypasses, 6 reservations
1025note made, 7 impossible requests refused, 8 head-of-line stops, 9 reserved
1026note jobs assigned, 10 tickets issued, 11 tickets lapsed, 12 leases overdue,
1027note 13 maintenance slot reads, 14 window scans, 15 dispatch events refused
1028note because the replay ring was full (DRA-398).
1029global scst ulong 16
1030note Goal-aware ranking (policy 2): sjpro the topics a job declares its
1031note completion publishes, sjest its declared work estimate in ms, sjunc
1032note that estimate's uncertainty in ms, sjval its unblock value in jobs as
1033note last scored. scgv: 0 graph version, 1 version last scored, 2 proposed
1034note job slot + 1 (0 none), 3 version the proposal named, 4 proposal state
1035note (0 no incarnation yet: proposals refused; 1 accepting; 2 the version
1036note is exhausted: proposals refused for the rest of this scheduler). The
1037note graph version is the scheduler's incarnation (sched_incarnate, once)
1038note times 2^40 plus the bumps since, never more than 2^40 - 1 of them: the
1039note bump that would carry into the next incarnation's range exhausts the
1040note version instead, so schedulers of different incarnations never share a
1041note version. Within one scheduler every admission of a job, cancellation
1042note of a waiting or ready one and readiness bumps it, so a proposal whose
1043note version is current still names the job it was made for.
1044global sjpro ulong 64
1045global sjest ulong 64
1046global sjunc ulong 64
1047global sjval ulong 64
1048global scgv ulong 5
1049note Goal counters (sched_stat 32..39): 0 rescores, 1 jobs scored, 2 graph
1050note version bumps, 3 proposals accepted, 4 proposals stale, 5 proposals
1051note refused, 6 slots read by value traversal, 7 proposals used.
1052global scgst ulong 8
1053note Replay ring (DRA-398). While a journal is attached (sfj 0 is 1), each
1054note dispatch event that assigned a job or changed the reservation leaves a
1055note record of 6 words in sfr: event count, assigned slot bitmap, bypassed
1056note slot bitmap (the slots whose bypass count went up), cursor, reserved
1057note slot + 1, and 0. A publication that makes waiting jobs ready leaves a
1058note readiness record: the current event count, no assigned or bypassed
1059note slot, cursor, reserved slot + 1, and the bitmap of the slots it made
1060note ready. sfj 1 counts the records waiting; sfj 2 and 3 are the
1061note reservation at the start of the current event and the slots it has
1062note assigned so far. The journal drains them before it appends any other
1063note record, so a transition is never durable before the dispatch
1064note decisions and readiness that came before it. With 64 waiting,
1065note dispatch assigns nothing and publication is refused (counter 15)
1066note until they are drained.
1067global sfr ulong 384
1068global sfj ulong 4
1069
1070fn sc_cell w ulong off ulong to ulong
1071ret mapget get scmap 0 add mul w 128 off 8
1072end
1073
1074fn sc_set w ulong off ulong v ulong to void
1075r = mapset get scmap 0 add mul w 128 off 8 v
1076end
1077
1078note Creator, before any fork: map the scheduling page and the outboxes and
1079note set the limits on admitted (a) and in-review (rl) jobs, each 1..64.
1080note Returns 0, -6 not creator, -7 bad limit, or a mapping errno.
1081fn sched_new c long a ulong rl ulong to long
1082if not pool_mine c
1083ret neg long 6
1084end
1085if or eq a 0 gt a SJCAP
1086ret neg long 7
1087end
1088if or eq rl 0 gt rl SJCAP
1089ret neg long 7
1090end
1091s = mapnew neg int 1 0 8192 3 33
1092if le s 0
1093ret s
1094end
1095o = mapnew neg int 1 0 262144 3 33
1096if le o 0
1097r = mapdrop s
1098ret o
1099end
1100put scmap 0 s
1101put scmap 1 o
1102put sclim 0 a
1103put sclim 1 rl
1104for j ulong 64
1105put sjst j 0
1106put sjheld j 0
1107put sjlse j 0
1108put sjdue j 0
1109put sjpro j 0
1110put sjest j 0
1111put sjunc j 0
1112put sjval j 0
1113put scbusy j 0
1114put scseq j 0
1115end
1116for k ulong 4
1117put scap k 65535
1118put suse k 0
1119end
1120put sfj 1 0
1121for i ulong 16
1122put scst i 0
1123end
1124for i ulong 8
1125put scgst i 0
1126end
1127for i ulong 5
1128put scgv i 0
1129end
1130put scpol 0 1
1131put scpol 1 64
1132put scpol 2 64
1133put scpol 3 4
1134put scpol 4 0
1135put scpol 5 0
1136put scpol 6 0
1137put scpol 7 1000
1138put scpause 0 0
1139ret 0
1140end
1141
1142note Count of kind k (0..3) in packed request req.
1143fn sp_need req ulong k ulong to ulong
1144ret and shr req mul k 16 65535
1145end
1146
1147note Free capacity of kind k. sched_cap never sets a capacity below the
1148note use, so this cannot underflow.
1149fn sp_free k ulong to ulong
1150ret sub get scap k get suse k
1151end
1152
1153note Does req fit in the free capacity once the reserved request res has
1154note what it still lacks? Of each kind, res keeps min(free, its need); res 0
1155note is no reservation. This keeps the reserved job's attainable capacity
1156note from ever shrinking (the drain).
1157fn sp_fits req ulong res ulong to bool
1158for k ulong 4
1159f = sp_free k
1160r = sp_need res k
1161a = ulong 0
1162if gt f r
1163a = sub f r
1164end
1165if gt sp_need req k a
1166ret false
1167end
1168end
1169ret true
1170end
1171
1172note Could req ever run, with the configured capacity all free?
1173fn sp_feas req ulong to bool
1174for k ulong 4
1175if gt sp_need req k get scap k
1176ret false
1177end
1178end
1179ret true
1180end
1181
1182note Job j takes its whole need.
1183fn sp_take j ulong to void
1184for k ulong 4
1185put suse k add get suse k sp_need get sjneed j k
1186end
1187put sjheld j 1
1188end
1189
1190note Job j gives back its need, if it holds it.
1191fn sp_give j ulong to void
1192if eq get sjheld j 0
1193ret
1194end
1195for k ulong 4
1196put suse k sub get suse k sp_need get sjneed j k
1197end
1198put sjheld j 0
1199end
1200
1201note Job j becomes ready, stamped with the next ready order.
1202fn sp_ready j ulong to void
1203put sjst j 2
1204put sjrdy j get scst 1
1205put scst 1 add get scst 1 1
1206sp_bump
1207end
1208
1209note Creator: counter i of scst, capacity (i 16..19) or use (i 20..23) of a
1210note kind, policy word (i 24..31), goal counter (i 32..39), graph version
1211note (i 40), proposal state (i 41: 0 no incarnation, 1 accepting, 2
1212note exhausted).
1213fn sched_stat i ulong to ulong
1214if lt i 16
1215ret get scst i
1216end
1217if lt i 20
1218ret get scap sub i 16
1219end
1220if lt i 24
1221ret get suse sub i 20
1222end
1223if lt i 32
1224ret get scpol sub i 24
1225end
1226if lt i 40
1227ret get scgst sub i 32
1228end
1229if eq i 40
1230ret get scgv 0
1231end
1232if eq i 41
1233ret get scgv 4
1234end
1235ret 0
1236end
1237
1238note Creator: would sched_cap caps be accepted? Not when the new capacity of
1239note a kind is below its use by running jobs, or below the need of any one
1240note admitted job.
1241fn sched_capok caps ulong to bool
1242for k ulong 4
1243if lt sp_need caps k get suse k
1244ret false
1245end
1246end
1247for j ulong 64
1248s = get sjst j
1249if and ge s 1 le s 3
1250for k ulong 4
1251if gt sp_need get sjneed j k sp_need caps k
1252ret false
1253end
1254end
1255end
1256end
1257ret true
1258end
1259
1260note Creator: set the capacity of every kind from caps (packed like a
1261note request). 0, or -8 when sched_capok refuses (nothing changes).
1262fn sched_cap caps ulong to long
1263if not sched_capok caps
1264ret neg long 8
1265end
1266for k ulong 4
1267put scap k sp_need caps k
1268end
1269ret 0
1270end
1271
1272note Creator: the configured capacities, packed like a request.
1273fn sched_caps to ulong
1274v = ulong 0
1275for k ulong 4
1276v = or v shl get scap k mul k 16
1277end
1278ret v
1279end
1280
1281note Creator: would sched_policy accept these words? Policy p 0..2, window
1282note win 1..64, dispatch budget db 1..64, bypass budget by 1..65535, ticket
1283note lifetime ttl 1..1000000.
1284fn sched_polok p ulong win ulong db ulong by ulong ttl ulong to bool
1285if gt p 2
1286ret false
1287end
1288if or eq win 0 gt win 64
1289ret false
1290end
1291if or eq db 0 gt db 64
1292ret false
1293end
1294if or eq by 0 gt by 65535
1295ret false
1296end
1297ret and gt ttl 0 le ttl 1000000
1298end
1299
1300note Creator: policy p (0 FIFO head-of-line, 1 fair fitting, 2 goal-aware
1301note fair fitting), candidate window win slots, dispatch budget db jobs per
1302note event, bypass budget by, ticket lifetime ttl dispatch events, in the
1303note ranges of sched_polok. The cursor and any reservation are kept. 0 or
1304note -7.
1305fn sched_policy p ulong win ulong db ulong by ulong ttl ulong to long
1306if not sched_polok p win db by ttl
1307ret neg long 7
1308end
1309put scpol 0 p
1310put scpol 1 win
1311put scpol 2 db
1312put scpol 3 by
1313put scpol 7 ttl
1314ret 0
1315end
1316
1317note Creator: drop this process's scheduling mappings.
1318fn sched_free to long
1319r = mapdrop get scmap 1
1320if lt r 0
1321ret r
1322end
1323ret mapdrop get scmap 0
1324end
1325
1326note Jobs whose state is in lo..hi.
1327fn sched_count lo ulong hi ulong to ulong
1328n = ulong 0
1329for j ulong 64
1330s = get sjst j
1331if and ge s lo le s hi
1332n = add n 1
1333end
1334end
1335ret n
1336end
1337
1338note Position of id's admission ticket, or 64.
1339fn sp_tkpos id ulong to ulong
1340for t ulong get scpol 6 bound 64
1341if eq get sttk t id
1342ret t
1343end
1344end
1345ret 64
1346end
1347
1348note Compact the tickets in order, dropping ticket d (64: none) and every
1349note ticket not asked about for the ticket lifetime.
1350fn sp_tkpack d ulong to void
1351k = ulong 0
1352for t ulong get scpol 6 bound 64
1353if ne t d
1354if lt add get stts t get scpol 7 get scst 2
1355put scst 11 add get scst 11 1
1356else
1357put sttk k get sttk t
1358put stts k get stts t
1359k = add k 1
1360end
1361end
1362end
1363put scpol 6 k
1364end
1365
1366note Refresh ticket t, or give id a ticket at the end when it has none and
1367note the line has room (a caller refused with the line full holds none).
1368fn sp_tkhold id ulong t ulong to void
1369if lt t 64
1370put stts t get scst 2
1371ret
1372end
1373n = get scpol 6
1374if lt n 64
1375put sttk n id
1376put stts n get scst 2
1377put scpol 6 add n 1
1378put scst 10 add get scst 10 1
1379end
1380end
1381
1382note Creator: may id be admitted now? Room is the smaller of the admission
1383note headroom and the free table slots (a job in review or UNKNOWN holds
1384note its slot). While tickets are held only the first holder may enter;
1385note with none, anyone may while there is room. A refused caller gets or
1386note keeps a ticket; every ask by a holder, refused or not, refreshes its
1387note ticket's last-asked event, and a caller with room and no ticket gets
1388note none. A true answer guarantees the slot for the next sched_admitr
1389note (single creator), so a caller may ask before journaling (as jn_admit
1390note does).
1391fn sched_room id ulong to bool
1392sp_tkpack 64
1393t = sp_tkpos id
1394a = ulong 0
1395fs = ulong 0
1396for j ulong 64
1397s = get sjst j
1398if eq s 0
1399fs = add fs 1
1400end
1401if and ge s 1 le s 3
1402a = add a 1
1403end
1404end
1405room = ulong 0
1406if lt a get sclim 0
1407room = sub get sclim 0 a
1408end
1409if lt fs room
1410room = fs
1411end
1412if gt room 0
1413if eq get scpol 6 0
1414ret true
1415end
1416if eq t 0
1417put stts 0 get scst 2
1418ret true
1419end
1420end
1421sp_tkhold id t
1422ret false
1423end
1424
1425note Creator: admit job id needing req (packed resources). deps 0 makes it
1426note ready at once; otherwise it waits for a publication whose topics meet
1427note deps. flags bit 0: safe to rerun after its worker dies. Returns the job
1428note slot; -8 when req exceeds the configured capacity of a kind (it could
1429note never run); -2 when sched_room refuses: at the admission limit, or
1430note ticket holders ahead; the caller keeps a ticket and asks again.
1431fn sched_admitr id ulong deps ulong flags ulong req ulong to long
1432ret sched_admits id deps flags req sched_slot
1433end
1434
1435note Creator: the lowest free table slot (the one sched_admitr would give
1436note the next job), or 64 when none is free.
1437fn sched_slot to ulong
1438for j ulong 64
1439if eq get sjst j 0
1440ret j
1441end
1442end
1443ret 64
1444end
1445
1446note Creator: could req ever run, with the configured capacity all free?
1447fn sched_feas req ulong to bool
1448ret sp_feas req
1449end
1450
1451note Creator: sched_admitr into table slot j, which must be free: the
1452note journal records the slot with the admission and recovery puts the job
1453note back into it (DRA-398). Refusals as sched_admitr; -2 also when slot j
1454note is not free.
1455fn sched_admits id ulong deps ulong flags ulong req ulong j ulong to long
1456if not sp_feas req
1457put scst 7 add get scst 7 1
1458ret neg long 8
1459end
1460if not sched_room id
1461ret neg long 2
1462end
1463if ge j 64
1464ret neg long 2
1465end
1466if ne get sjst j 0
1467ret neg long 2
1468end
1469t = sp_tkpos id
1470if lt t 64
1471sp_tkpack t
1472end
1473put sjid j id
1474put sjdep j deps
1475put sjfl j flags
1476put sjatt j 0
1477put sjout j 0
1478put sjw j 0
1479put sjneed j req
1480put sjheld j 0
1481put sjbyp j 0
1482put sjlse j 0
1483put sjdue j 0
1484put sjarr j get scst 0
1485put scst 0 add get scst 0 1
1486put sjpro j 0
1487put sjest j 0
1488put sjunc j 0
1489put sjval j 0
1490if eq deps 0
1491sp_ready j
1492else
1493put sjst j 1
1494sp_bump
1495end
1496ret long j
1497end
1498
1499note Creator: job j (waiting, ready or running) gets a lease of ns from its
1500note next assignment, or from now when it runs; 0 removes it. 0; -3 for a
1501note slot that is not waiting, ready or running; -7 for ns above 2^50 (about
1502note 13 days). Both refusals change nothing. The bound keeps every deadline
1503note (the monotonic clock, below 2^63, plus ns) representable: it never
1504note wraps into the past or onto 0, the no-deadline value.
1505fn sched_lease j ulong ns ulong to long
1506if ge j 64
1507ret neg long 3
1508end
1509s = get sjst j
1510if or lt s 1 gt s 3
1511ret neg long 3
1512end
1513if gt ns 1125899906842624
1514ret neg long 7
1515end
1516put sjlse j ns
1517if eq s 3
1518put sjdue j 0
1519if gt ns 0
1520put sjdue j add ulong monons ns
1521end
1522end
1523ret 0
1524end
1525
1526note Creator: a running job past its lease whose worker has not reported
1527note done, reported once (its deadline is cleared): its slot, or 64. The
1528note authority cancels or kills it; the release follows from the worker's
1529note report or reap.
1530fn sched_overdue to ulong
1531now = ulong monons
1532for j ulong 64
1533if eq get sjst j 3
1534d = get sjdue j
1535if gt d 0
1536if ge now d
1537w = get sjw j
1538if ne sc_cell w 64 get scseq w
1539put sjdue j 0
1540put scst 12 add get scst 12 1
1541ret j
1542end
1543end
1544end
1545end
1546end
1547ret 64
1548end
1549
1550note Creator: sched_admitr with no resource need beyond one worker.
1551fn sched_admit id ulong deps ulong flags ulong to long
1552ret sched_admitr id deps flags 0
1553end
1554
1555note Creator: worker w is woken by publications whose topics meet topics.
1556fn sched_want w ulong topics ulong to long
1557if ge w 64
1558ret neg long 7
1559end
1560sc_set w 32 topics
1561ret 0
1562end
1563
1564fn sc_wake w ulong to void
1565sc_set w 40 ulong monons
1566r = sup_wake w
1567end
1568
1569note Lowest idle live worker below n, or 64 when none. A worker is idle when
1570note the creator has collected its last assignment.
1571fn sp_idle n ulong to ulong
1572for w ulong n bound 64
1573if and gt get swpid w 0 eq get scbusy w 0
1574ret w
1575end
1576end
1577ret 64
1578end
1579
1580note Assign job j to idle worker w: it takes its need; the job id and the
1581note input epoch and version are written before the seq, then the wake.
1582fn sp_start c long j ulong w ulong to void
1583q = add get scseq w 1
1584put scseq w q
1585put scbusy w add j 1
1586put sjst j 3
1587put sjw j w
1588put sjatt j add get sjatt j 1
1589put sjbyp j 0
1590put sjdue j 0
1591if gt get sjlse j 0
1592put sjdue j add ulong monons get sjlse j
1593end
1594sp_take j
1595if eq get scpol 5 add j 1
1596put scpol 5 0
1597put scst 9 add get scst 9 1
1598end
1599sc_set w 8 get sjid j
1600sc_set w 16 pool_epoch c
1601sc_set w 24 node_count pool_current c
1602sc_set w 0 q
1603sc_wake w
1604put scst 4 add get scst 4 1
1605end
1606
1607note Policy 0, FIFO head-of-line: the stop rule of the reference pump
1608note (aien-sovereign-core-mv spark-harness scheduler/core.rs Core::pump,
1609note 598-602), not its queue: the head here is the ready job that became
1610note ready first (the pump batches newly eligible tasks in task-id order
1611note into a bounded queue). The head is chosen when it fits, and when it
1612note does not fit nothing is chosen, so a head that does
1613note not fit holds back every later job. Returns the slot or 64.
1614fn sp_fifo to ulong
1615h = ulong 64
1616for j ulong 64
1617put scst 3 add get scst 3 1
1618if eq get sjst j 2
1619if eq h 64
1620h = j
1621else
1622if lt get sjrdy j get sjrdy h
1623h = j
1624end
1625end
1626end
1627end
1628if eq h 64
1629ret 64
1630end
1631if sp_fits get sjneed h 0
1632ret h
1633end
1634put scst 8 add get scst 8 1
1635ret 64
1636end
1637
1638note Policy 1, fair fitting: the reserved job first when it fits; else the
1639note oldest admitted ready job, among win slots from the cursor, that fits
1640note without the capacity the reserved job still lacks; the cursor then
1641note moves on by win. Returns the slot or 64. Every slot read counts
1642note (counter 3: the reserved probe and the window), at most win + 1 per
1643note pick, win + 2 under policy 2 (sp_proposal's probe counts too);
1644note counter 14 counts window scans.
1645fn sp_fair to ulong
1646rq = ulong 0
1647r = get scpol 5
1648if gt r 0
1649rj = sub r 1
1650put scst 3 add get scst 3 1
1651if eq get sjst rj 2
1652if sp_fits get sjneed rj 0
1653ret rj
1654end
1655rq = get sjneed rj
1656else
1657put scpol 5 0
1658end
1659end
1660goal = eq get scpol 0 2
1661if goal
1662sp_rescore
1663pj = sp_proposal rq
1664if lt pj 64
1665ret pj
1666end
1667end
1668win = get scpol 1
1669cur = get scpol 4
1670b = ulong 64
1671put scst 14 add get scst 14 1
1672for i ulong win bound 64
1673put scst 3 add get scst 3 1
1674j = rem add cur i 64
1675if eq get sjst j 2
1676if sp_fits get sjneed j rq
1677if eq b 64
1678b = j
1679else
1680if sp_better goal j b
1681b = j
1682end
1683end
1684end
1685end
1686end
1687put scpol 4 rem add cur win 64
1688ret b
1689end
1690
1691note Ranking key, both of ready jobs a and b: is a better? Policy 1: older
1692note admission. Policy 2: larger unblock value, then smaller declared
1693note estimate, then older admission (admission orders are unique, so there
1694note is never a tie).
1695fn sp_better goal bool a ulong b ulong to bool
1696if goal
1697va = get sjval a
1698vb = get sjval b
1699if ne va vb
1700ret gt va vb
1701end
1702ea = get sjest a
1703eb = get sjest b
1704if ne ea eb
1705ret lt ea eb
1706end
1707end
1708ret lt get sjarr a get sjarr b
1709end
1710
1711note A committed change that can alter an unblock value: a job becomes
1712note ready, a waiting job is admitted, a waiting or ready job is cancelled,
1713note or a declaration changes. Bumps the graph version, which makes any
1714note proposal stale (rejected when next used). Nothing is rescored here:
1715note the next policy-2 pick rescores once, however many bumps came before
1716note it (coalesced). At 2^40 - 1 bumps the version stays and is exhausted
1717note (scgv 4 = 2): from then on proposals are refused and every pick
1718note rescores.
1719fn sp_bump to void
1720put scgst 2 add get scgst 2 1
1721if eq and get scgv 0 1099511627775 1099511627775
1722put scgv 4 2
1723ret
1724end
1725put scgv 0 add get scgv 0 1
1726end
1727
1728note Unblock value of ready job j, in jobs: the waiting jobs whose deps meet
1729note the topics j declares it unblocks (depth 1), plus the waiting jobs whose
1730note deps meet the topics those declare (depth 2), each counted once. Reads
1731note at most 128 slots.
1732fn sp_value j ulong to ulong
1733pro = get sjpro j
1734if eq pro 0
1735ret 0
1736end
1737n = ulong 0
1738m = ulong 0
1739p2 = ulong 0
1740for k ulong 64
1741if eq get sjst k 1
1742if ne and get sjdep k pro 0
1743m = or m shl ulong 1 k
1744p2 = or p2 get sjpro k
1745n = add n 1
1746end
1747end
1748end
1749put scgst 6 add get scgst 6 64
1750if eq p2 0
1751ret n
1752end
1753for k ulong 64
1754if eq get sjst k 1
1755if eq and shr m k 1 0
1756if ne and get sjdep k p2 0
1757n = add n 1
1758end
1759end
1760end
1761end
1762put scgst 6 add get scgst 6 64
1763ret n
1764end
1765
1766note Recompute the unblock value of every ready job when the graph version
1767note moved since the last scoring, or always once it is exhausted;
1768note otherwise nothing.
1769fn sp_rescore to void
1770v = get scgv 0
1771if and eq get scgv 1 v ne get scgv 4 2
1772ret
1773end
1774for j ulong 64
1775if eq get sjst j 2
1776put sjval j sp_value j
1777put scgst 1 add get scgst 1 1
1778end
1779end
1780put scgv 1 v
1781put scgst 0 add get scgst 0 1
1782put scgst 6 add get scgst 6 64
1783end
1784
1785note The proposed job, when the proposal is for the current graph version
1786note (and that version is not exhausted) and the job is still ready and fits
1787note beside the reservation (rq); it is used once. Otherwise the proposal
1788note is dropped as stale: 64.
1789fn sp_proposal rq ulong to ulong
1790p = get scgv 2
1791if eq p 0
1792ret 64
1793end
1794put scgv 2 0
1795put scst 3 add get scst 3 1
1796j = sub p 1
1797if and eq get scgv 3 get scgv 0 eq get scgv 4 1
1798if eq get sjst j 2
1799if sp_fits get sjneed j rq
1800put scgst 7 add get scgst 7 1
1801ret j
1802end
1803end
1804end
1805put scgst 4 add get scgst 4 1
1806ret 64
1807end
1808
1809note Creator: declare what job j (waiting or ready) is expected to unblock
1810note and cost: pro, the topics its completion will publish; est, the work
1811note estimate in ms (declared, or derived by the caller from measurements);
1812note unc, its uncertainty in ms (recorded, not used to rank). 0, or -3.
1813fn sched_goal j ulong pro ulong est ulong unc ulong to long
1814if ge j 64
1815ret neg long 3
1816end
1817s = get sjst j
1818if or eq s 0 gt s 2
1819ret neg long 3
1820end
1821put sjpro j pro
1822put sjest j est
1823put sjunc j unc
1824sp_bump
1825ret 0
1826end
1827
1828note Creator: the graph version that proposals must name.
1829fn sched_version to ulong
1830ret get scgv 0
1831end
1832
1833note Creator, after sched_new: incarnation n (1..2^24 - 1), distinct
1834note for every scheduler that may receive proposals meant for another (the
1835note journal fence, DRA-398). The graph version becomes n * 2^40, which no
1836note scheduler of another incarnation reaches, any proposal is dropped and
1837note proposals are accepted from now on. The last scoring stays below n *
1838note 2^40, so the next ranking pass rescores whatever was admitted or
1839note declared before, whenever the incarnation comes. Once per
1840note scheduler: 0, or -7 (n out of range, or already set or exhausted).
1841fn sched_incarnate n ulong to long
1842if or eq n 0 ge n 16777216
1843ret neg long 7
1844end
1845if ne get scgv 4 0
1846ret neg long 7
1847end
1848put scgv 0 shl n 40
1849put scgv 2 0
1850put scgv 4 1
1851ret 0
1852end
1853
1854note Creator, policy 2: propose the ready job whose id is id for the next
1855note pick, computed at graph version v (an outside planner, or an
1856note operator). The proposal names the job, not its slot. Reservations and
1857note feasibility still come first: it is checked again when used. 0; -6 no
1858note incarnation set or the version exhausted; -9 stale (v is not the
1859note current version); -3 no ready job, or more than one, has that id; -8
1860note it does not fit beside the reservation; -7 not policy 2.
1861fn sched_propose id ulong v ulong to long
1862if ne get scpol 0 2
1863ret neg long 7
1864end
1865if ne get scgv 4 1
1866put scgst 5 add get scgst 5 1
1867ret neg long 6
1868end
1869if ne v get scgv 0
1870put scgst 4 add get scgst 4 1
1871ret neg long 9
1872end
1873j = ulong 64
1874n = ulong 0
1875for k ulong 64
1876if and eq get sjst k 2 eq get sjid k id
1877j = k
1878n = add n 1
1879end
1880end
1881if ne n 1
1882put scgst 5 add get scgst 5 1
1883ret neg long 3
1884end
1885rq = ulong 0
1886r = get scpol 5
1887if gt r 0
1888rq = get sjneed sub r 1
1889end
1890if not sp_fits get sjneed j rq
1891put scgst 5 add get scgst 5 1
1892ret neg long 8
1893end
1894put scgv 2 add j 1
1895put scgv 3 v
1896put scgst 3 add get scgst 3 1
1897ret 0
1898end
1899
1900note After a fair dispatch event in which ya - 1 was the youngest admission
1901note assigned (ya 0: none): every ready job admitted before it was bypassed
1902note once; with no reservation, the oldest job at the bypass budget is
1903note reserved. One pass over the table (64 maintenance reads, counter 13).
1904note Returns the bitmap of the slots bypassed.
1905fn sp_after ya ulong to ulong
1906o = ulong 64
1907bm = ulong 0
1908nores = eq get scpol 5 0
1909for j ulong 64
1910if eq get sjst j 2
1911if gt ya 0
1912if lt add get sjarr j 1 ya
1913put sjbyp j add get sjbyp j 1
1914put scst 5 add get scst 5 1
1915bm = or bm shl 1 j
1916end
1917end
1918if nores
1919if ge get sjbyp j get scpol 3
1920if eq o 64
1921o = j
1922else
1923if lt get sjarr j get sjarr o
1924o = j
1925end
1926end
1927end
1928end
1929end
1930end
1931put scst 13 add get scst 13 64
1932if lt o 64
1933put scpol 5 add o 1
1934put scst 6 add get scst 6 1
1935end
1936ret bm
1937end
1938
1939note Creator: assign ready jobs to idle live workers under the policy, at
1940note most the dispatch budget per call. Returns the number assigned. With
1941note a journal attached and the replay ring full, nothing is assigned
1942note (counter 15) until the journal drains it.
1943fn sched_dispatch c long to ulong
1944if ne get scpause 0 0
1945ret 0
1946end
1947if and ne get sfj 0 0 ge get sfj 1 64
1948put scst 15 add get scst 15 1
1949ret 0
1950end
1951n = mapget c 48 8
1952k = ulong 0
1953ya = ulong 0
1954put sfj 2 get scpol 5
1955put sfj 3 0
1956fair = ge get scpol 0 1
1957put scst 2 add get scst 2 1
1958for i ulong get scpol 2 bound 64
1959w = sp_idle n
1960if ge w 64
1961break
1962end
1963j = ulong 64
1964if fair
1965j = sp_fair
1966else
1967j = sp_fifo
1968end
1969if ge j 64
1970break
1971end
1972if gt add get sjarr j 1 ya
1973ya = add get sjarr j 1
1974end
1975sp_fmark j
1976sp_start c j w
1977k = add k 1
1978end
1979bm = ulong 0
1980if fair
1981bm = sp_after ya
1982end
1983sp_frend bm
1984ret k
1985end
1986
1987note End of a dispatch event with a journal attached: one ring record when
1988note it assigned a job or moved the reservation.
1989fn sp_frend bm ulong to void
1990if eq get sfj 0 0
1991ret
1992end
1993if or ne get sfj 3 0 ne get scpol 5 get sfj 2
1994sp_frpush get sfj 3 bm 0
1995end
1996end
1997
1998note The current dispatch event assigned slot j (sfj 3).
1999fn sp_fmark j ulong to void
2000put sfj 3 or get sfj 3 shl 1 j
2001end
2002
2003note Append one replay record (sfr): event, assigned and bypassed slot
2004note bitmaps, cursor, reserved slot + 1, slots made ready. The callers
2005note leave room.
2006fn sp_frpush am ulong bm ulong rd ulong to void
2007b = mul get sfj 1 6
2008put sfr b get scst 2
2009put sfr add b 1 am
2010put sfr add b 2 bm
2011put sfr add b 3 get scpol 4
2012put sfr add b 4 get scpol 5
2013put sfr add b 5 rd
2014put sfj 1 add get sfj 1 1
2015end
2016
2017note Journal: attach (on) or detach the replay ring. Attaching empties it.
2018note jn_recover attaches once the scheduler holds the journaled state.
2019fn sched_jattach on bool to void
2020put sfj 0 0
2021if on
2022put sfj 0 1
2023end
2024put sfj 1 0
2025end
2026
2027fn sched_jattached to bool
2028ret ne get sfj 0 0
2029end
2030
2031note Journal: records waiting in the replay ring, word i (0..5) of record k,
2032note and emptying it once they are durable.
2033fn sched_frn to ulong
2034ret get sfj 1
2035end
2036fn sched_frw k ulong i ulong to ulong
2037ret get sfr add mul k 6 i
2038end
2039fn sched_frclear to void
2040put sfj 1 0
2041end
2042
2043note Journal, recovery: the cursor and reservation after replay, and the
2044note dispatch event count. The reservation names slot res - 1, which must
2045note hold a ready job. -7 (nothing changed) for a cursor outside the table
2046note or any other reservation, else 0.
2047fn sched_frestore cur ulong ev ulong res ulong to long
2048if or ge cur 64 gt res 64
2049ret neg long 7
2050end
2051if gt res 0
2052if ne get sjst sub res 1 2
2053ret neg long 7
2054end
2055end
2056put scpol 4 cur
2057put scst 2 ev
2058put scpol 5 res
2059ret 0
2060end
2061
2062note Journal, recovery: the bypass count of ready job j. 0 or -3.
2063fn sched_setbyp j ulong n ulong to long
2064if ge j 64
2065ret neg long 3
2066end
2067if ne get sjst j 2
2068ret neg long 3
2069end
2070put sjbyp j n
2071ret 0
2072end
2073
2074note Journal, recovery: waiting job j was made ready by a publication
2075note before the restart. 0 or -3 (j not waiting).
2076fn sched_setready j ulong to long
2077if ge j 64
2078ret neg long 3
2079end
2080if ne get sjst j 1
2081ret neg long 3
2082end
2083sp_ready j
2084ret 0
2085end
2086
2087note Creator: move finished jobs into review while review has room. A worker
2088note whose result is waiting for room keeps it and gets no new job, so a
2089note full review queue stops assignment instead of dropping work. A job
2090note gives back its resources as soon as its worker reported done, even
2091note while its result waits for room.
2092fn sched_collect c long to ulong
2093n = mapget c 48 8
2094k = ulong 0
2095for w ulong n bound 64
2096b = get scbusy w
2097if gt b 0
2098if eq sc_cell w 64 get scseq w
2099j = sub b 1
2100sp_give j
2101if ge sched_count 4 4 get sclim 1
2102continue
2103end
2104put sjst j 4
2105put sjout j sc_cell w 72
2106put scbusy w 0
2107k = add k 1
2108end
2109end
2110end
2111ret k
2112end
2113
2114note Creator, after worker w was reaped: retire its unfinished assignment.
2115note A rerunnable job is ready again; any other becomes UNKNOWN (6), since
2116note it may have had effects. A finished result is left for sched_collect.
2117note Returns 1 when an assignment was retired, 0 when none, -16 still live.
2118fn sched_lost c long w ulong to long
2119if ge w 64
2120ret neg long 7
2121end
2122if gt get swpid w 0
2123ret neg long 16
2124end
2125b = get scbusy w
2126if eq b 0
2127ret 0
2128end
2129if eq sc_cell w 64 get scseq w
2130ret 0
2131end
2132j = sub b 1
2133sc_set w 64 get scseq w
2134put scbusy w 0
2135sp_give j
2136if ne and get sjfl j 1 0
2137sp_ready j
2138else
2139put sjst j 6
2140end
2141ret 1
2142end
2143
2144note Creator loop step: wait up to interval ns for an ack or a death, take
2145note acks, retire the jobs of reaped workers, collect, then dispatch.
2146note Returns the number of workers reaped, or a negative errno.
2147fn sched_step c long interval long to long
2148d = sup_poll c interval
2149if lt d 0
2150ret d
2151end
2152n = mapget c 48 8
2153for w ulong n bound 64
2154if gt get swpid w 0
2155r = sup_take get swack w
2156else
2157r = sched_lost c w
2158end
2159end
2160k = sched_collect c
2161k = sched_dispatch c
2162ret d
2163end
2164
2165note Creator: publish an artifact, make waiting jobs whose deps meet topics
2166note ready, wake each live worker whose interest meets topics, dispatch.
2167note With a journal attached the jobs made ready leave a readiness record
2168note in the replay ring; when the ring is full nothing is published, -105
2169note (counter 15). Returns the version, or the publish refusal with nothing
2170note woken.
2171fn sched_publish c long topics ulong src view uchar 4096 n ulong to long
2172if and ne get sfj 0 0 ge get sfj 1 64
2173put scst 15 add get scst 15 1
2174ret neg long 105
2175end
2176v = pool_publish c topics src n
2177if le v 0
2178ret v
2179end
2180rd = ulong 0
2181for j ulong 64
2182if eq get sjst j 1
2183if ne and get sjdep j topics 0
2184sp_ready j
2185rd = or rd shl 1 j
2186end
2187end
2188end
2189if and ne rd 0 ne get sfj 0 0
2190sp_frpush 0 0 rd
2191end
2192m = mapget c 48 8
2193for w ulong m bound 64
2194if gt get swpid w 0
2195if ne and sc_cell w 32 topics 0
2196sc_wake w
2197end
2198end
2199end
2200k = sched_dispatch c
2201ret v
2202end
2203
2204note Creator: accept the result of job j in review and free its slot.
2205note Returns its outcome, or -3 when j is not in review.
2206fn sched_review j ulong to long
2207if ge j 64
2208ret neg long 3
2209end
2210if ne get sjst j 4
2211ret neg long 3
2212end
2213put sjst j 0
2214ret long get sjout j
2215end
2216
2217note Creator: free an UNKNOWN job once its outcome has been settled
2218note elsewhere. Returns 0, or -3 when j is not UNKNOWN.
2219fn sched_settle j ulong to long
2220if ge j 64
2221ret neg long 3
2222end
2223if ne get sjst j 6
2224ret neg long 3
2225end
2226put sjst j 0
2227ret 0
2228end
2229
2230note Creator: while paused (on true) no job is assigned; running jobs go on
2231note and finished results are still collected. Resuming dispatches at once.
2232note Returns the number assigned on resume, else 0.
2233fn sched_pause c long on bool to ulong
2234if on
2235put scpause 0 1
2236ret 0
2237end
2238put scpause 0 0
2239ret sched_dispatch c
2240end
2241
2242note Creator: cancel job j. A waiting or ready job never started, so it is
2243note freed with no effect: 1. A running job needs its worker: 2, and the
2244note outcome comes from the worker. A job in review already finished: 3,
2245note nothing is cancelled. UNKNOWN stays UNKNOWN: 4. -3 free or bad slot.
2246fn sched_cancel j ulong to long
2247if ge j 64
2248ret neg long 3
2249end
2250s = get sjst j
2251if or eq s 1 eq s 2
2252put sjst j 0
2253sp_bump
2254if eq get scpol 5 add j 1
2255put scpol 5 0
2256end
2257ret 1
2258end
2259if eq s 3
2260ret 2
2261end
2262if eq s 4
2263ret 3
2264end
2265if eq s 6
2266ret 4
2267end
2268ret neg long 3
2269end
2270
2271note Creator: stop every worker within interval ns (sup_stop), retire the
2272note assignments of the reaped workers, collect finished results.
2273note Returns how many workers had to be killed, or a refusal.
2274fn sched_stop c long interval long to long
2275k = sup_stop c interval
2276if lt k 0
2277ret k
2278end
2279n = mapget c 48 8
2280for w ulong n bound 64
2281r = sched_lost c w
2282end
2283m = sched_collect c
2284ret k
2285end
2286
2287note Worker w: the assignment seq when a job is waiting, else 0. Job id,
2288note input epoch and input version are words 8, 16 and 24 of its cell.
2289fn sched_wjob w ulong to ulong
2290q = sc_cell w 0
2291if gt q sc_cell w 64
2292ret q
2293end
2294ret 0
2295end
2296
2297note Worker w: finish assignment q with outcome and the first n (at most
2298note 4096) bytes of src. Bytes and outcome are written before the done seq;
2299note then the creator gets an ack. 0, -7 bad n, or the ack refusal.
2300fn sched_wdone w ulong q ulong outcome ulong src view uchar 4096 n ulong to long
2301if gt n 4096
2302ret neg long 7
2303end
2304if gt n 0
2305r = mapwrite get scmap 1 mul w 4096 src 0 n
2306end
2307sc_set w 72 outcome
2308sc_set w 64 q
2309ret sup_ack w
2310end
2311
2312note Creator: copy worker w's last result bytes into dst.
2313fn sched_result w ulong dst edit uchar 4096 to long
2314ret mapread get scmap 1 mul w 4096 dst 0 4096
2315end
2316
2317note Worker w: sup_wwait that records its wakes and wake latency.
2318fn sched_wwait c long w ulong interval long to long
2319r = sup_wwait c w interval
2320if eq r 1
2321now = ulong monons
2322st = sc_cell w 40
2323lat = ulong 0
2324if gt now st
2325lat = sub now st
2326end
2327sc_set w 80 add sc_cell w 80 1
2328sc_set w 88 lat
2329if gt lat sc_cell w 96
2330sc_set w 96 lat
2331end
2332end
2333ret r
2334end
2335
2336note Worker w: count the versions of the current epoch that meet its
2337note interest and are newer than its cursor, moving the cursor past them.
2338note A new epoch restarts the cursor at 0. A worker with no interest scans
2339note nothing. Returns the count or a refusal.
2340fn sched_wscan c long w ulong to long
2341t = sc_cell w 32
2342if eq t 0
2343ret 0
2344end
2345h = pool_acquire c w
2346if le h 0
2347ret h
2348end
2349e = mapget c add 256 mul w 8 8
2350if ne get swcur 0 e
2351put swcur 0 e
2352put swcur 1 0
2353end
2354k = long 0
2355for i ulong 65
2356v = node_next h get swcur 1 t
2357if lt v 0
2358r = pool_release c w
2359ret v
2360end
2361if eq v 0
2362break
2363end
2364put swcur 1 ulong v
2365k = add k 1
2366end
2367r = pool_release c w
2368ret k
2369end
2370note SPDX-License-Identifier: AGPL-3.0-only
2371note OM-CONTROL-659 action contract v1. See ../README.md for the trusted-caller boundary.
2372note No I/O, authority minting or persistence. Caller serializes and durably commits transitions.
2373note Record words 0..7: version,state,attempt,limit,generation,epoch,expiry,reserved.
2374note Words 8..31: operation,request,scope,provider,policy,grant identities, four words each.
2375note State 1 ready,2 in-flight,3 unknown,4 performed. No timeout implies not performed.
2376
2377fn actvalid r view ulong 32 to ulong
2378if ne get r 0 1
2379ret 11
2380end
2381if or lt get r 1 1 gt get r 1 4
2382ret 12
2383end
2384if or eq get r 3 0 gt get r 3 1024
2385ret 12
2386end
2387if gt get r 2 get r 3
2388ret 12
2389end
2390if and ne get r 1 1 eq get r 2 0
2391ret 12
2392end
2393if or eq get r 4 0 eq get r 5 0
2394ret 12
2395end
2396if or eq get r 6 0 ne get r 7 0
2397ret 12
2398end
2399for g ulong 6
2400bits = ulong 0
2401for j ulong 4
2402bits = or bits get r add 8 add mul g 4 j
2403end
2404if eq bits 0
2405ret 12
2406end
2407end
2408ret 0
2409end
2410
2411fn actbind r view ulong 32 c view ulong 32 to ulong
2412for g ulong 6
2413diff = ulong 0
2414for j ulong 4
2415ix = add 8 add mul g 4 j
2416diff = or diff xor get r ix get c ix
2417end
2418if ne diff 0
2419ret add 40 g
2420end
2421end
2422ret 0
2423end
2424
2425fn actauth r view ulong 32 c view ulong 32 to ulong
2426if ne get c 0 1
2427ret 11
2428end
2429if or ne get c 3 0 or ne get c 6 0 ne get c 7 0
2430ret 12
2431end
2432if ne get c 1 1
2433ret 30
2434end
2435if ne get r 4 get c 4
2436ret 31
2437end
2438if ne get r 5 get c 5
2439ret 32
2440end
2441if ge get c 2 get r 6
2442ret 33
2443end
2444ret actbind r c
2445end
2446
2447fn actstart r edit ulong 32 c view ulong 32 to ulong
2448err = actvalid r
2449if ne err 0
2450ret err
2451end
2452if eq get r 1 3
2453ret 21
2454end
2455if eq get r 1 4
2456ret 22
2457end
2458if ne get r 1 1
2459ret 20
2460end
2461if ge get r 2 get r 3
2462ret 23
2463end
2464err = actauth r c
2465if ne err 0
2466ret err
2467end
2468put r 2 add get r 2 1
2469put r 1 2
2470ret 0
2471end
2472
2473fn actlost r edit ulong 32 to ulong
2474err = actvalid r
2475if ne err 0
2476ret err
2477end
2478if eq get r 1 3
2479ret 0
2480end
2481if ne get r 1 2
2482ret 20
2483end
2484put r 1 3
2485ret 0
2486end
2487
2488note A trusted receipt maps outcome 0 unknown,1 definitely not performed,2 performed.
2489note Not-found or timeout is outcome 0 unless the provider proves absence and quiescence.
2490note Receipt words 2/3: exact attempt and verification=1. Words 6/7 must be zero.
2491note Recording an observed outcome is permitted after expiry; another attempt is not.
2492fn actrecon r edit ulong 32 s view ulong 32 to ulong
2493err = actvalid r
2494if ne err 0
2495ret err
2496end
2497if not or eq get r 1 2 eq get r 1 3
2498ret 20
2499end
2500if ne get s 0 1
2501ret 11
2502end
2503if or gt get s 1 2 or ne get s 6 0 ne get s 7 0
2504ret 12
2505end
2506if ne get s 3 1
2507ret 36
2508end
2509if ne get s 2 get r 2
2510ret 35
2511end
2512if ne get s 4 get r 4
2513ret 31
2514end
2515if ne get s 5 get r 5
2516ret 32
2517end
2518err = actbind r s
2519if ne err 0
2520ret err
2521end
2522if eq get s 1 0
2523put r 1 3
2524ret 21
2525end
2526if eq get s 1 1
2527put r 1 1
2528else
2529put r 1 4
2530end
2531ret 0
2532end
2533note Omega Node durable journal and restart recovery (DRA-289). Concatenate
2534note after transport.om, pool.om, super.om, sched.om and
2535note seed/authority/action.om, before auth.om, which journals through it.
2536note While no journal is open every append succeeds without writing, so the
2537note authority layer also runs memory-only.
2538note File DIR/journal: a 64-byte header, then 128-byte records. Header words:
2539note 0 magic, 1 format version (2 for a file this build creates; 1 for one
2540note a format-1 creator made, read and continued, see FORMAT), 2 record
2541note size 128, 3..6 objective identity,
2542note 7 FNV-1a 64 of words 0..6. Record words: 0 type, 1 seq (1, 2, 3, ...
2543note with no gap), 2 fence, 3..14 payload, 15 FNV-1a 64 of words 0..14.
2544note Types and payloads:
2545note 1 OPEN 3 pid, 4 authority generation of the new creator, 5 the
2546note scheduler policy version SPVER it was written under (0
2547note from a format-1 creator)
2548note 2 ADMIT 3 job id, 4 dependency topics, 5 flags, 6 resource request,
2549note 7 table slot
2550note 3 START 3..6 operation identity, 7 class, 8 job id, 9 attempt, 10 gen
2551note 4 DONE 3..6 operation identity, 7 outcome (0 UNKNOWN, 1 not
2552note performed, 2 performed), 8 job id, 9 how (1 reported,
2553note 2 worker lost, 3 in flight at restart, 4 key present,
2554note 5 key absent, 6 no external effect)
2555note 5 RELEASE 3 job id, 4 outcome (the job's outcome; 300 cancelled)
2556note 6 REVOKE 3 new generation
2557note 7 PAUSE 3 1 paused, 0 resumed
2558note 8 CANCEL 3 job id
2559note 9 OPER an operator command the creator committed (DRA-291): 3
2560note command id (never 0, never repeated), 4 op, 5 target,
2561note 6..9 arguments, 10 view version it was acknowledged at.
2562note Kept in order in a table of 128 for the creator to replay;
2563note a reader without type 9 refuses it as unknown (24).
2564note 10 RESULT the result an objective ended with (DRA-292): 3 objective
2565note index (< 64), 4 result (1 PASS, else the failure; not 0),
2566note 5 model turns performed. At most one per objective; a
2567note restart reports it and never resumes that objective. A
2568note reader without type 10 refuses it as unknown (24).
2569note 11 POLICY 3 policy, 4 window, 5 dispatch budget, 6 bypass budget,
2570note 7 ticket lifetime, 8 capacities (packed like a request):
2571note the whole setting; the last one wins
2572note 12 GOAL 3 job id, 4 topics, 5 estimate ms, 6 uncertainty ms
2573note 13 FAIR 3 dispatch event, 4 assigned slot bitmap, 5 bypassed slot
2574note bitmap, 6 cursor, 7 reserved slot + 1, 8 slots a
2575note publication made ready (sched.om's ring)
2576note 14 FORMAT 3 the format version the records after it follow (2). In
2577note a version-1 file the first creator of this format writes
2578note it once, before its OPEN, so a format-1 reader refuses
2579note the file from then on (an unknown type, 24) instead of
2580note continuing it.
2581note A header of another version is refused (21). Records keep the format
2582note of the incarnation that wrote them: in a version-1 file, an OPEN with
2583note word 5 zero starts a format-1 incarnation, whose ADMITs carry no
2584note request or slot (request 0, the lowest slot no unreleased job holds)
2585note and which may hold no POLICY, GOAL or FAIR (25). An OPEN of another
2586note SPVER, a format-1 OPEN after FORMAT or in a version-2 file, an OPEN of
2587note this format before FORMAT in a version-1 file, and a FORMAT anywhere
2588note else are refused at replay (33).
2589note Order: a transition is appended and fdatasync'd before anything acts on
2590note it or publishes it. An append whose write or sync fails marks the journal
2591note failed and every later append is refused, so nothing is acted on that
2592note might not be durable.
2593note Fencing: DIR/lock is held with flock(LOCK_EX) for the creator's life. It
2594note is opened close-on-exec, so tools never hold it, but forked workers do:
2595note a new creator cannot open the journal until every worker of the old one
2596note has exited. Each open appends OPEN with the next fence number, and every
2597note record carries the fence of the creator that wrote it.
2598note Torn tail: a partial last record, or a last full record whose checksum or
2599note seq is wrong, was never acknowledged and is cut off. A bad record that is
2600note not the last is corruption: the open is refused and nothing is changed.
2601note Compatibility: a header whose magic, record size or checksum differs,
2602note or whose version is neither 1 nor 2, is refused (21), never
2603note reinterpreted; an objective identity that differs is refused (22). A
2604note version-1 file keeps its header when this build continues it.
2605text jnjour "/journal"
2606text jnlock "/lock"
2607text jnkey "/k."
2608global jon ulong 1
2609global jbad ulong 1
2610global jfd int 1
2611global jlk int 1
2612global jseq ulong 1
2613global jsz ulong 1
2614global jfence ulong 1
2615global jgen ulong 1
2616global jcrash ulong 1
2617global jdir uchar 128
2618global jdn ulong 1
2619global jpath uchar 160
2620global jact ulong 1024
2621global jjob ulong 2048
2622global jstat ulong 8
2623note jfmt: 0 the open file's header version, 1 1 once its records follow
2624note this format (a version-2 file, or FORMAT seen), 2 1 while replaying a
2625note format-1 incarnation.
2626global jfmt ulong 3
2627note Policy replay tables (DRA-398), per job entry k: jreq request, jslot
2628note slot at admission, jbyp bypass count, jrdy 1 once a publication made
2629note it ready; jsl slot -> job entry + 1 while admitted and not released;
2630note jpol 0 seen, 1..5 policy words, 6 caps; jgoal per k: 0 seen, 1
2631note topics, 2 estimate, 3 uncertainty; jfair 0 last event, 1 cursor, 2
2632note reserved job entry + 1, 3 seen.
2633global jreq ulong 256
2634global jslot ulong 256
2635global jbyp ulong 256
2636global jrdy ulong 256
2637global jsl ulong 64
2638global jpol ulong 8
2639global jgoal ulong 1024
2640global jfair ulong 4
2641global jopr ulong 1024
2642global jnop ulong 1
2643global jres ulong 128
2644
2645note Fault injection for the recovery tests: exit 77 at point p when armed.
2646fn jn_hit p ulong to void
2647if and gt p 0 eq get jcrash 0 p
2648exit 77
2649end
2650end
2651
2652fn jn_sum r view ulong 16 n ulong to ulong
2653h = ulong 14695981039346656037
2654for i ulong n bound 16
2655v = get r i
2656for b ulong 8
2657h = mul xor h and shr v mul b 8 255 1099511628211
2658end
2659end
2660ret h
2661end
2662
2663fn jn_enc r view ulong 16 b edit uchar 128 to void
2664for i ulong 16
2665v = get r i
2666for k ulong 8
2667put b add mul i 8 k uchar and shr v mul k 8 255
2668end
2669end
2670end
2671
2672fn jn_dec b view uchar 128 r edit ulong 16 to void
2673for i ulong 16
2674v = ulong 0
2675for k ulong 8
2676v = or v shl ulong get b add mul i 8 k mul k 8
2677end
2678put r i v
2679end
2680end
2681
2682note DIR plus name k (0 journal, 1 lock, 2 key prefix) in jpath; the length.
2683fn jn_name k ulong to ulong
2684n = get jdn 0
2685for i ulong n bound 128
2686put jpath i get jdir i
2687end
2688m = len jnjour
2689if eq k 1
2690m = len jnlock
2691end
2692if eq k 2
2693m = len jnkey
2694end
2695for i ulong m bound 16
2696b = get jnkey 0
2697if eq k 0
2698b = get jnjour i
2699end
2700if eq k 1
2701b = get jnlock i
2702end
2703if eq k 2
2704b = get jnkey i
2705end
2706put jpath add n i b
2707end
2708ret add n m
2709end
2710
2711note The key path DIR/k.<v> of an idempotent effect; the length.
2712fn jn_keyp v ulong to ulong
2713n = jn_name 2
2714d = new uchar 20 0
2715x = v
2716k = ulong 20
2717for i ulong 20
2718k = sub k 1
2719put d k uchar add rem x 10 48
2720x = div x 10
2721if eq x 0
2722break
2723end
2724end
2725for i ulong sub 20 k bound 20
2726put jpath add n i get d add k i
2727end
2728ret add n sub 20 k
2729end
2730
2731fn jn_reset to void
2732for i ulong 1024
2733put jact i 0
2734end
2735for i ulong 2048
2736put jjob i 0
2737end
2738for i ulong 1024
2739put jopr i 0
2740end
2741put jnop 0 0
2742for i ulong 128
2743put jres i 0
2744end
2745for i ulong 8
2746put jstat i 0
2747put jpol i 0
2748end
2749for i ulong 256
2750put jreq i 0
2751put jslot i 0
2752put jbyp i 0
2753put jrdy i 0
2754end
2755for i ulong 1024
2756put jgoal i 0
2757end
2758for i ulong 64
2759put jsl i 0
2760end
2761for i ulong 4
2762put jfair i 0
2763end
2764for i ulong 3
2765put jfmt i 0
2766end
2767put jon 0 0
2768put jbad 0 0
2769put jseq 0 0
2770put jsz 0 0
2771put jfence 0 0
2772put jgen 0 0
2773end
2774
2775note Action table, 64 entries of 16 words: 0 used, 1..4 operation identity,
2776note 5 class, 6 state (as action.om: 1 ready, 2 in flight, 3 UNKNOWN,
2777note 4 performed), 7 attempt, 8 gen, 9 job id. -1 when absent.
2778fn jn_afind r view ulong 16 to long
2779for k ulong 64
2780b = mul k 16
2781if ne get jact b 0
2782m = true
2783for i ulong 4
2784if ne get jact add b add 1 i get r add 3 i
2785m = false
2786end
2787end
2788if m
2789ret long k
2790end
2791end
2792end
2793ret neg long 1
2794end
2795
2796note Job table, 256 entries of 8 words: 0 used, 1 job id, 2 deps, 3 flags,
2797note 4 released, 5 outcome, 6 cancelled, 7 action entry + 1 of its last START.
2798fn jn_jfind id ulong to long
2799for k ulong 256
2800if and ne get jjob mul k 8 0 eq get jjob add mul k 8 1 id
2801ret long k
2802end
2803end
2804ret neg long 1
2805end
2806
2807fn jn_jnew id ulong to long
2808for k ulong 256
2809b = mul k 8
2810if eq get jjob b 0
2811for i ulong 8
2812put jjob add b i 0
2813end
2814put jjob b 1
2815put jjob add b 1 id
2816ret long k
2817end
2818end
2819ret neg long 1
2820end
2821
2822fn jn_maxg g ulong to void
2823if gt g get jgen 0
2824put jgen 0 g
2825end
2826end
2827
2828fn jn_astart r view ulong 16 to ulong
2829k = jn_afind r
2830if lt k 0
2831for i ulong 64
2832if eq get jact mul i 16 0
2833k = long i
2834break
2835end
2836end
2837end
2838if lt k 0
2839ret 25
2840end
2841b = mul ulong k 16
2842if ne get jact b 0
2843if ne get jact add b 6 1
2844ret 25
2845end
2846end
2847put jact b 1
2848for i ulong 4
2849put jact add b add 1 i get r add 3 i
2850end
2851put jact add b 5 get r 7
2852put jact add b 6 2
2853put jact add b 7 get r 9
2854put jact add b 8 get r 10
2855put jact add b 9 get r 8
2856jn_maxg get r 10
2857j = jn_jfind get r 8
2858if ge j 0
2859put jjob add mul ulong j 8 7 add ulong k 1
2860end
2861ret 0
2862end
2863
2864fn jn_adone r view ulong 16 to ulong
2865k = jn_afind r
2866if lt k 0
2867ret 25
2868end
2869b = mul ulong k 16
2870s = get jact add b 6
2871if not or eq s 2 eq s 3
2872ret 25
2873end
2874o = get r 7
2875if gt o 2
2876ret 25
2877end
2878v = ulong 3
2879if eq o 1
2880v = 1
2881end
2882if eq o 2
2883v = 4
2884end
2885put jact add b 6 v
2886ret 0
2887end
2888
2889note Apply one durable record to the recovery tables; 0, 24 unknown type,
2890note 25 a transition the tables refuse (START of a performed operation, a
2891note DONE without a START, a second ADMIT or RELEASE, a policy record that
2892note does not hold, a POLICY, GOAL or FAIR in a format-1 incarnation), 33 an
2893note OPEN or FORMAT the file's format does not allow (see FORMAT above).
2894fn jn_apply r view ulong 16 to ulong
2895t = get r 0
2896if eq t 1
2897e = jn_aopen r
2898if ne e 0
2899ret e
2900end
2901put jfence 0 get r 2
2902jn_maxg get r 4
2903ret 0
2904end
2905if eq t 2
2906ret jn_aadmit r
2907end
2908if eq t 3
2909ret jn_astart r
2910end
2911if eq t 4
2912ret jn_adone r
2913end
2914if or eq t 5 eq t 8
2915j = jn_jfind get r 3
2916if lt j 0
2917ret 25
2918end
2919b = mul ulong j 8
2920if eq t 8
2921put jjob add b 6 1
2922if eq get jfair 2 add ulong j 1
2923put jfair 2 0
2924end
2925ret 0
2926end
2927if ne get jjob add b 4 0
2928ret 25
2929end
2930put jjob add b 4 1
2931put jjob add b 5 get r 4
2932s = get jslot ulong j
2933if eq get jsl s add ulong j 1
2934put jsl s 0
2935end
2936ret 0
2937end
2938if eq t 6
2939jn_maxg get r 3
2940ret 0
2941end
2942if eq t 7
2943put jstat 2 get r 3
2944ret 0
2945end
2946if eq t 9
2947ret jn_oapply r
2948end
2949if eq t 10
2950ret jn_rapply r
2951end
2952if eq t 14
2953ret jn_afmt r
2954end
2955if gt t 10
2956if and le t 13 ne get jfmt 2 0
2957ret 25
2958end
2959ret jn_apol r
2960end
2961ret 24
2962end
2963
2964note ADMIT into the tables: a new job entry with its request and slot; a
2965note slot another unreleased job holds is refused (25). A format-1 ADMIT
2966note records neither: request 0, the lowest slot no unreleased job holds
2967note (25 when none is free).
2968fn jn_aadmit r view ulong 16 to ulong
2969if ge jn_jfind get r 3 0
2970ret 25
2971end
2972s = get r 7
2973q = get r 6
2974if ne get jfmt 2 0
2975s = jn_lfree
2976q = ulong 0
2977end
2978if ge s 64
2979ret 25
2980end
2981if ne get jsl s 0
2982ret 25
2983end
2984j = jn_jnew get r 3
2985if lt j 0
2986ret 25
2987end
2988k = ulong j
2989put jjob add mul k 8 2 get r 4
2990put jjob add mul k 8 3 get r 5
2991put jreq k q
2992put jslot k s
2993put jbyp k 0
2994put jrdy k 0
2995put jsl s add k 1
2996ret 0
2997end
2998
2999note The lowest slot no unreleased job holds in the replay table; 64 none.
3000fn jn_lfree to ulong
3001for s ulong 64
3002if eq get jsl s 0
3003ret s
3004end
3005end
3006ret 64
3007end
3008
3009note OPEN's format: word 5 SPVER once the file's records follow this format;
3010note 0 (a format-1 creator) only in a version-1 file before FORMAT. 33 else.
3011fn jn_aopen r view ulong 16 to ulong
3012if eq get r 5 SPVER
3013if eq get jfmt 1 0
3014ret 33
3015end
3016put jfmt 2 0
3017ret 0
3018end
3019if or ne get r 5 0 ne get jfmt 1 0
3020ret 33
3021end
3022put jfmt 2 1
3023ret 0
3024end
3025
3026note FORMAT: only in a version-1 file, once, naming format 2. 33 else.
3027fn jn_afmt r view ulong 16 to ulong
3028if or ne get jfmt 0 1 ne get jfmt 1 0
3029ret 33
3030end
3031if ne get r 3 2
3032ret 33
3033end
3034put jfmt 1 1
3035ret 0
3036end
3037
3038note POLICY, GOAL and FAIR into the tables (types 11..13); 24 any other.
3039fn jn_apol r view ulong 16 to ulong
3040t = get r 0
3041if eq t 11
3042if not sched_polok get r 3 get r 4 get r 5 get r 6 get r 7
3043ret 25
3044end
3045put jpol 0 1
3046for i ulong 6
3047put jpol add i 1 get r add i 3
3048end
3049ret 0
3050end
3051if eq t 12
3052j = jn_jfind get r 3
3053if lt j 0
3054ret 25
3055end
3056b = mul ulong j 4
3057put jgoal b 1
3058for i ulong 3
3059put jgoal add b add i 1 get r add i 4
3060end
3061ret 0
3062end
3063if eq t 13
3064ret jn_afair r
3065end
3066ret 24
3067end
3068
3069note FAIR into the tables. A decision record (word 8 zero) must follow the
3070note last event; a readiness record (word 8 the slots a publication made
3071note ready, nothing assigned or bypassed) may share it, since publication
3072note does not count as a dispatch event. The cursor must lie in the
3073note table; assigned and bypassed slots must be disjoint and hold live
3074note ready jobs (no deps, or made ready by an earlier readiness record);
3075note slots made ready must hold live waiting jobs; the reserved slot a live
3076note ready job this record did not assign. Live: admitted, not released,
3077note not cancelled. Nothing changes otherwise (25). Assigned jobs' bypass
3078note counts restart at 0, bypassed ones go up by one, jobs made ready are
3079note marked ready.
3080fn jn_afair r view ulong 16 to ulong
3081am = get r 4
3082bm = get r 5
3083rd = get r 8
3084if not jn_fevt get r 3 or am bm rd
3085ret 25
3086end
3087if or ge get r 6 64 ne and am bm 0
3088ret 25
3089end
3090if or not jn_fok or am bm 1 not jn_fok rd 2
3091ret 25
3092end
3093rs = get r 7
3094if gt rs 64
3095ret 25
3096end
3097rk = ulong 0
3098if gt rs 0
3099if ne and shr am sub rs 1 1 0
3100ret 25
3101end
3102if not jn_fok shl ulong 1 sub rs 1 1
3103ret 25
3104end
3105rk = get jsl sub rs 1
3106end
3107for s ulong 64
3108if ne and shr am s 1 0
3109put jbyp sub get jsl s 1 0
3110end
3111if ne and shr bm s 1 0
3112k = sub get jsl s 1
3113put jbyp k add get jbyp k 1
3114end
3115if ne and shr rd s 1 0
3116put jrdy sub get jsl s 1 1
3117end
3118end
3119put jfair 0 get r 3
3120put jfair 1 get r 6
3121put jfair 2 rk
3122put jfair 3 1
3123ret 0
3124end
3125
3126note Event order of a FAIR at event ev with assigned or bypassed slots ab
3127note and slots made ready rd: a readiness record (ab empty) is never also a
3128note decision, even as the first FAIR; decisions come strictly after the
3129note last event, readiness at or after it.
3130fn jn_fevt ev ulong ab ulong rd ulong to bool
3131if and ne rd 0 ne ab 0
3132ret false
3133end
3134if eq get jfair 3 0
3135ret true
3136end
3137if ne rd 0
3138ret ge ev get jfair 0
3139end
3140ret gt ev get jfair 0
3141end
3142
3143note Does every slot set in bitmap m hold a live job that is ready (kind 1:
3144note no deps, or made ready) or waiting (kind 2: deps, not made ready)?
3145fn jn_fok m ulong kind ulong to bool
3146for s ulong 64
3147if ne and shr m s 1 0
3148x = get jsl s
3149if eq x 0
3150ret false
3151end
3152k = sub x 1
3153b = mul k 8
3154if or ne get jjob add b 4 0 ne get jjob add b 6 0
3155ret false
3156end
3157w = and ne get jjob add b 2 0 eq get jrdy k 0
3158if and w eq kind 1
3159ret false
3160end
3161if and not w eq kind 2
3162ret false
3163end
3164end
3165end
3166ret true
3167end
3168
3169note OPER into the operator table: 25 for command id 0, an id already
3170note there, or a full table. Entry k: 0 id, 1 op, 2 target, 3..6 arguments,
3171note 7 view version.
3172fn jn_oapply r view ulong 16 to ulong
3173n = get jnop 0
3174if or eq get r 3 0 ge n 128
3175ret 25
3176end
3177if ge jn_ofind get r 3 0
3178ret 25
3179end
3180for w ulong 8
3181put jopr add mul n 8 w get r add 3 w
3182end
3183put jnop 0 add n 1
3184ret 0
3185end
3186
3187note RESULT into the result table (2 words per objective: result, turns):
3188note 25 for an index past 63, result 0, or an objective that has one.
3189fn jn_rapply r view ulong 16 to ulong
3190i = get r 3
3191if or ge i 64 eq get r 4 0
3192ret 25
3193end
3194if ne get jres mul i 2 0
3195ret 25
3196end
3197put jres mul i 2 get r 4
3198put jres add mul i 2 1 get r 5
3199ret 0
3200end
3201
3202note Operator table index of command id, -1 when absent.
3203fn jn_ofind id ulong to long
3204for k ulong get jnop 0 bound 128
3205if eq get jopr mul k 8 id
3206ret long k
3207end
3208end
3209ret neg long 1
3210end
3211
3212fn jn_le64 b view uchar 128 off ulong to ulong
3213v = ulong 0
3214for k ulong 8
3215v = or v shl ulong get b add off k mul k 8
3216end
3217ret v
3218end
3219
3220note Make DIR's entry for a new journal durable.
3221fn jn_dsync to ulong
3222fd = fopen jdir 0 get jdn 0 540672 0
3223if lt fd 0
3224ret 27
3225end
3226r = fsync int fd
3227x = fclose int fd
3228if ne r 0
3229ret 27
3230end
3231ret 0
3232end
3233
3234note Create the header of an empty journal, or check an existing one.
3235fn jn_head fd int obj view ulong 4 to ulong
3236st = new uchar 128 0
3237h = new ulong 16 0
3238b = new uchar 128 0
3239if ne fstat fd st 0 128 0
3240ret 27
3241end
3242size = jn_le64 st 48
3243if lt size 64
3244if ne ftrunc fd 0 0
3245ret 27
3246end
3247put h 0 5786624599
3248put h 1 2
3249put jfmt 0 2
3250put jfmt 1 1
3251put h 2 128
3252for i ulong 4
3253put h add 3 i get obj i
3254end
3255put h 7 jn_sum h 7
3256jn_enc h b
3257if ne pwrite fd b 0 64 0 64
3258ret 27
3259end
3260if ne fsync fd 0
3261ret 27
3262end
3263put jsz 0 64
3264ret jn_dsync
3265end
3266if ne pread fd b 0 64 0 64
3267ret 27
3268end
3269jn_dec b h
3270s = jn_sum h 7
3271if or ne get h 0 5786624599 ne get h 7 s
3272ret 21
3273end
3274if ne get h 2 128
3275ret 21
3276end
3277if and ne get h 1 1 ne get h 1 2
3278ret 21
3279end
3280put jfmt 0 get h 1
3281if eq get h 1 2
3282put jfmt 1 1
3283end
3284for i ulong 4
3285if ne get h add 3 i get obj i
3286ret 22
3287end
3288end
3289put jsz 0 size
3290ret 0
3291end
3292
3293fn jn_cut fd int off ulong to ulong
3294if ne ftrunc fd long off 0
3295ret 27
3296end
3297if ne fdsync fd 0
3298ret 27
3299end
3300put jstat 0 add get jstat 0 sub get jsz 0 off
3301put jsz 0 off
3302ret 0
3303end
3304
3305note Record i into r: 0 well formed (checksum and seq), 1 not, 27 I/O.
3306fn jn_rd fd int i ulong r edit ulong 16 to ulong
3307b = new uchar 128 0
3308off = long add 64 mul i 128
3309if ne pread fd b 0 128 off 128
3310ret 27
3311end
3312jn_dec b r
3313s = jn_sum r 15
3314if ne get r 15 s
3315ret 1
3316end
3317if ne get r 1 add i 1
3318ret 1
3319end
3320ret 0
3321end
3322
3323note Read every record into the tables; cut a torn tail; refuse corruption.
3324fn jn_scan fd int to ulong
3325r = new ulong 16 0
3326n = div sub get jsz 0 64 128
3327rm = rem sub get jsz 0 64 128
3328for i ulong n bound 10000000
3329g = jn_rd fd i r
3330if eq g 27
3331ret 27
3332end
3333if eq g 1
3334if and eq add i 1 n eq rm 0
3335ret jn_cut fd add 64 mul i 128
3336end
3337ret 23
3338end
3339e = jn_apply r
3340if ne e 0
3341ret e
3342end
3343put jseq 0 add i 1
3344put jstat 1 add i 1
3345end
3346if gt rm 0
3347ret jn_cut fd add 64 mul n 128
3348end
3349ret 0
3350end
3351
3352note Append record r (caller sets word 0 and the payload) and make it
3353note durable, after the dispatch decisions waiting in sched.om's replay
3354note ring (jn_drain), so no transition is durable before the scheduling
3355note that came before it. The tables check and take the transition first
3356note (jn_apply changes nothing when it refuses), so a refused transition is
3357note never written. 0, a table refusal, 28 write, 29 sync, 30 failed
3358note earlier. After 28 or 29 the journal is failed: the transition may or
3359note may not be durable, so the caller must not act on it, and nothing more
3360note is written. Without an open journal: 0.
3361fn jn_append r edit ulong 16 to ulong
3362e = jn_drain
3363if ne e 0
3364ret e
3365end
3366ret jn_put r
3367end
3368
3369note Write the replay ring as FAIR records and empty it; then, when the
3370note cursor or the reservation moved without a record (a window scan that
3371note assigned nothing), one FAIR with empty bitmaps. 0 or as jn_put; on
3372note failure the ring keeps its records and dispatch stops when it fills.
3373note The ring is attached by jn_recover, once the scheduler holds the
3374note journaled state; before that, or without an open journal, it is
3375note emptied and nothing is written.
3376fn jn_drain to ulong
3377if or eq get jon 0 0 not sched_jattached
3378sched_frclear
3379ret 0
3380end
3381r = new ulong 16 0
3382n = sched_frn
3383for k ulong n bound 64
3384for i ulong 16
3385put r i 0
3386end
3387put r 0 13
3388for i ulong 6
3389put r add 3 i sched_frw k i
3390end
3391e = jn_put r
3392if ne e 0
3393ret e
3394end
3395end
3396sched_frclear
3397rk = ulong 0
3398rs = sched_stat 29
3399if gt rs 0
3400rk = get jsl sub rs 1
3401end
3402if and eq sched_stat 28 get jfair 1 eq rk get jfair 2
3403ret 0
3404end
3405if eq sched_stat 2 get jfair 0
3406ret 0
3407end
3408for i ulong 16
3409put r i 0
3410end
3411put r 0 13
3412put r 3 sched_stat 2
3413put r 6 sched_stat 28
3414put r 7 rs
3415ret jn_put r
3416end
3417
3418note jn_append without the drain.
3419fn jn_put r edit ulong 16 to ulong
3420if eq get jon 0 0
3421ret 0
3422end
3423if ne get jbad 0 0
3424ret 30
3425end
3426b = new uchar 128 0
3427fd = get jfd 0
3428put r 1 add get jseq 0 1
3429put r 2 get jfence 0
3430put r 15 jn_sum r 15
3431e = jn_apply r
3432if ne e 0
3433ret e
3434end
3435jn_enc r b
3436off = long add 64 mul get jseq 0 128
3437if and eq get jcrash 0 6 eq get r 0 4
3438x = pwrite fd b 0 60 off
3439exit 77
3440end
3441if ne pwrite fd b 0 128 off 128
3442put jbad 0 1
3443ret 28
3444end
3445if ne fdsync fd 0
3446put jbad 0 1
3447ret 29
3448end
3449put jseq 0 add get jseq 0 1
3450put jsz 0 add get jsz 0 128
3451ret 0
3452end
3453
3454fn jn_rec t ulong a ulong b ulong c ulong to ulong
3455r = new ulong 16 0
3456put r 0 t
3457put r 3 a
3458put r 4 b
3459put r 5 c
3460ret jn_append r
3461end
3462
3463note Open (or create) the journal in directory dir (dn bytes) for objective
3464note obj, waiting up to ms for the lock. 0, 20 fenced (another creator or
3465note its workers hold the lock), 21 incompatible, 22 other objective, 23
3466note corrupt, 24/25 refused record, 26 path too long, 27 I/O error.
3467fn jn_open dir view uchar 128 dn ulong obj view ulong 4 ms ulong to ulong
3468if or eq dn 0 gt dn 100
3469ret 26
3470end
3471jn_reset
3472for i ulong dn bound 128
3473put jdir i get dir i
3474end
3475put jdn 0 dn
3476r = makedir jdir 0 dn 448
3477if and lt r 0 ne r neg long 17
3478ret 27
3479end
3480n = jn_name 1
3481lk = fopen jpath 0 n 524354 384
3482if lt lk 0
3483ret 27
3484end
3485got = false
3486for t ulong add ms 1 bound 100000
3487f = flock int lk 6
3488if eq f 0
3489got = true
3490break
3491end
3492if ne f neg long 11
3493break
3494end
3495x = sleepns 1000000
3496end
3497if not got
3498x = fclose int lk
3499ret 20
3500end
3501put jlk 0 int lk
3502e = jn_load obj
3503if ne e 0
3504x = fclose int lk
3505ret e
3506end
3507ret 0
3508end
3509
3510fn jn_load obj view ulong 4 to ulong
3511n = jn_name 0
3512fd = fopen jpath 0 n 524354 384
3513if lt fd 0
3514ret 27
3515end
3516e = jn_head int fd obj
3517if eq e 0
3518e = jn_scan int fd
3519end
3520if ne e 0
3521x = fclose int fd
3522ret e
3523end
3524put jfd 0 int fd
3525put jon 0 1
3526if eq get jfmt 1 0
3527e = jn_rec 14 2 0 0
3528if ne e 0
3529ret e
3530end
3531end
3532put jfence 0 add get jfence 0 1
3533put jgen 0 add get jgen 0 1
3534ret jn_rec 1 ulong getpid get jgen 0 SPVER
3535end
3536
3537note Close the journal (after draining the replay ring) and release the
3538note lock (other holders: forked workers).
3539fn jn_close to void
3540if ne get jon 0 0
3541dr = jn_drain
3542x = fclose get jfd 0
3543x = fclose get jlk 0
3544end
3545put jon 0 0
3546sched_jattach false
3547end
3548
3549note Probe the key of an idempotent operation: 1 present, 0 absent, 2 error.
3550fn jn_probe v ulong to ulong
3551st = new uchar 128 0
3552n = jn_keyp v
3553r = stat jpath 0 n st 0 128
3554if eq r 0
3555ret 1
3556end
3557if eq r neg long 2
3558ret 0
3559end
3560ret 2
3561end
3562
3563note Close every START left in flight by a crash with a DONE. Class 3 (an
3564note idempotent effect whose key DIR/k.<op word 0> is created atomically by
3565note the effect) is decided by its key; class 2 (no external effect) was not
3566note performed; anything else is UNKNOWN and is never run again.
3567fn jn_settle to ulong
3568r = new ulong 16 0
3569for k ulong 64
3570b = mul k 16
3571if and ne get jact b 0 eq get jact add b 6 2
3572for i ulong 4
3573put r add 3 i get jact add b add 1 i
3574end
3575put r 0 4
3576put r 8 get jact add b 9
3577cl = get jact add b 5
3578put r 7 0
3579put r 9 3
3580if eq cl 2
3581put r 7 1
3582put r 9 6
3583end
3584if eq cl 3
3585p = jn_probe get r 3
3586if eq p 2
3587ret 27
3588end
3589put r 7 add 1 p
3590put r 9 sub 5 p
3591end
3592e = jn_append r
3593if ne e 0
3594ret e
3595end
3596put jstat 3 add get jstat 3 1
3597end
3598end
3599ret 0
3600end
3601
3602note Recovery: the incarnation (the fence), then the last journaled policy
3603note and capacities. False when the scheduler refused one; a fence past the
3604note incarnation range (2^24 - 1) is refused, never folded onto an earlier
3605note incarnation.
3606fn jn_rpol to bool
3607if ne sched_incarnate get jfence 0 0
3608ret false
3609end
3610if eq get jpol 0 0
3611ret true
3612end
3613if ne sched_cap get jpol 6 0
3614ret false
3615end
3616p = get jpol 1
3617w = get jpol 2
3618d = get jpol 3
3619y = get jpol 4
3620ret eq sched_policy p w d y get jpol 5 0
3621end
3622
3623note Creator, after jn_open and sched_new: settle in-flight operations,
3624note set the scheduler's incarnation to the fence (proposals made for an
3625note earlier creator are stale), restore the last journaled policy and
3626note capacities, then for each admitted job not yet released: a performed
3627note or UNKNOWN last operation releases it with that outcome (it never runs
3628note again); a cancelled job is released as cancelled; any other is
3629note admitted again, in admission order, into its recorded slot, with its
3630note bypass count and declared goal. Last, the cursor, the dispatch event
3631note count and the reservation (on the job's slot) from the last FAIR.
3632note 0 or an error (31 a restore the scheduler refused).
3633fn jn_recover c long to ulong
3634e = jn_settle
3635if ne e 0
3636ret e
3637end
3638if not jn_rpol
3639ret 31
3640end
3641rs = ulong 0
3642for k ulong 256
3643b = mul k 8
3644if and ne get jjob b 0 eq get jjob add b 4 0
3645o = jn_rout k
3646if eq o 999
3647x = jn_readmit k
3648if lt x 0
3649ret 31
3650end
3651if eq get jfair 2 add k 1
3652rs = add ulong x 1
3653end
3654put jstat 5 add get jstat 5 1
3655else
3656e = jn_rec 5 get jjob add b 1 o 0
3657if ne e 0
3658ret e
3659end
3660put jstat 4 add get jstat 4 1
3661end
3662end
3663end
3664if ne get jfair 3 0
3665if ne sched_frestore get jfair 1 get jfair 0 rs 0
3666ret 31
3667end
3668end
3669sched_jattach true
3670ret 0
3671end
3672
3673note Recovery: the outcome job entry k is released with (2 performed, 0
3674note UNKNOWN, 300 cancelled), or 999 when it is to be admitted again.
3675fn jn_rout k ulong to ulong
3676b = mul k 8
3677o = ulong 999
3678a = get jjob add b 7
3679if gt a 0
3680s = get jact add mul sub a 1 16 6
3681if eq s 4
3682o = 2
3683end
3684if eq s 3
3685o = 0
3686end
3687end
3688if and eq o 999 ne get jjob add b 6 0
3689o = 300
3690end
3691ret o
3692end
3693
3694note Recovery: admit job entry k again into its recorded slot, ready again
3695note when a publication had made it ready, then its bypass count and goal.
3696note The slot, or < 0 when the scheduler refuses any of them (a recorded
3697note slot that is taken, a bypass count on a job that is not ready).
3698fn jn_readmit k ulong to long
3699b = mul k 8
3700x = sched_admits get jjob add b 1 get jjob add b 2 get jjob add b 3 get jreq k get jslot k
3701if lt x 0
3702ret x
3703end
3704j = ulong x
3705if ne get jrdy k 0
3706if lt sched_setready j 0
3707ret neg long 3
3708end
3709end
3710if ne get jbyp k 0
3711if lt sched_setbyp j get jbyp k 0
3712ret neg long 3
3713end
3714end
3715g = mul k 4
3716if ne get jgoal g 0
3717if lt sched_goal j get jgoal add g 1 get jgoal add g 2 get jgoal add g 3 0
3718ret neg long 3
3719end
3720end
3721ret x
3722end
3723
3724note Admit job id durably: ADMIT, with the request and the slot it will
3725note take, is appended before sched_admits. -4 an id already in the journal
3726note (checked first, so a duplicate never takes or refreshes an admission
3727note ticket), -8 a request no capacity could ever hold, -2 when sched_room
3728note refuses (admission limit, no free slot, or ticket holders ahead), -5
3729note the journal refused, else the slot.
3730fn jn_admitr id ulong deps ulong flags ulong req ulong to long
3731if ge jn_jfind id 0
3732ret neg long 4
3733end
3734if not sched_feas req
3735ret neg long 8
3736end
3737if not sched_room id
3738ret neg long 2
3739end
3740s = sched_slot
3741if ge s 64
3742ret neg long 2
3743end
3744r = new ulong 16 0
3745put r 0 2
3746put r 3 id
3747put r 4 deps
3748put r 5 flags
3749put r 6 req
3750put r 7 s
3751if ne jn_append r 0
3752ret neg long 5
3753end
3754jn_hit 1
3755ret sched_admits id deps flags req s
3756end
3757
3758note jn_admitr with no resource request.
3759fn jn_admit id ulong deps ulong flags ulong to long
3760ret jn_admitr id deps flags 0
3761end
3762
3763note Creator: sched_policy, journaled first (POLICY, with the current
3764note capacities). -7 refused, -5 the journal refused, else 0.
3765fn jn_policy p ulong win ulong db ulong by ulong ttl ulong to long
3766if not sched_polok p win db by ttl
3767ret neg long 7
3768end
3769if ne jn_pol p win db by ttl sched_caps 0
3770ret neg long 5
3771end
3772ret sched_policy p win db by ttl
3773end
3774
3775note Creator: sched_cap, journaled first (POLICY, with the current policy).
3776note -8 refused, -5 the journal refused, else 0.
3777fn jn_cap caps ulong to long
3778if not sched_capok caps
3779ret neg long 8
3780end
3781if ne jn_pol sched_stat 24 sched_stat 25 sched_stat 26 sched_stat 27 sched_stat 31 caps 0
3782ret neg long 5
3783end
3784ret sched_cap caps
3785end
3786
3787fn jn_pol p ulong win ulong db ulong by ulong ttl ulong caps ulong to ulong
3788r = new ulong 16 0
3789put r 0 11
3790put r 3 p
3791put r 4 win
3792put r 5 db
3793put r 6 by
3794put r 7 ttl
3795put r 8 caps
3796ret jn_append r
3797end
3798
3799note Creator: sched_goal for job slot j (waiting or ready), journaled first
3800note (GOAL). -3 no such job, -5 the journal refused, else 0.
3801fn jn_goal j ulong pro ulong est ulong unc ulong to long
3802if ge j 64
3803ret neg long 3
3804end
3805s = get sjst j
3806if or eq s 0 gt s 2
3807ret neg long 3
3808end
3809r = new ulong 16 0
3810put r 0 12
3811put r 3 get sjid j
3812put r 4 pro
3813put r 5 est
3814put r 6 unc
3815if ne jn_append r 0
3816ret neg long 5
3817end
3818ret sched_goal j pro est unc
3819end
3820
3821note Release reviewed job slot j durably: RELEASE before sched_review. -5
3822note while its operation's DONE is not yet durable or on refusal.
3823fn jn_release j ulong to long
3824id = get sjid j
3825k = jn_jfind id
3826if ge k 0
3827a = get jjob add mul ulong k 8 7
3828if gt a 0
3829if eq get jact add mul sub a 1 16 6 2
3830ret neg long 5
3831end
3832end
3833end
3834if ne jn_rec 5 id get sjout j 0 0
3835ret neg long 5
3836end
3837jn_hit 5
3838ret sched_review j
3839end
3840
3841fn jn_start op view ulong 4 cl ulong id ulong att ulong g ulong to ulong
3842r = new ulong 16 0
3843put r 0 3
3844for i ulong 4
3845put r add 3 i get op i
3846end
3847put r 7 cl
3848put r 8 id
3849put r 9 att
3850put r 10 g
3851ret jn_append r
3852end
3853
3854fn jn_done op view ulong 4 o ulong id ulong how ulong to ulong
3855r = new ulong 16 0
3856put r 0 4
3857for i ulong 4
3858put r add 3 i get op i
3859end
3860put r 7 o
3861put r 8 id
3862put r 9 how
3863ret jn_append r
3864end
3865
3866note Getters: whether job id is in the journal, released, its outcome; a
3867note recovery counter (0 torn bytes cut, 1 records read, 2 paused, 3 settled,
3868note 4 released at recovery, 5 admitted again); the fence; the generation.
3869fn jn_known id ulong to bool
3870ret ge jn_jfind id 0
3871end
3872
3873fn jn_done_j id ulong to bool
3874k = jn_jfind id
3875if lt k 0
3876ret false
3877end
3878ret ne get jjob add mul ulong k 8 4 0
3879end
3880
3881fn jn_jout id ulong to ulong
3882k = jn_jfind id
3883if lt k 0
3884ret 999
3885end
3886ret get jjob add mul ulong k 8 5
3887end
3888
3889fn jn_stat i ulong to ulong
3890ret get jstat and i 7
3891end
3892
3893fn jn_fence to ulong
3894ret get jfence 0
3895end
3896
3897fn jn_gen to ulong
3898ret get jgen 0
3899end
3900
3901note Action table entry k word i (k < 64, i < 16).
3902fn jn_act k ulong i ulong to ulong
3903ret get jact add mul and k 63 16 and i 15
3904end
3905
3906note Commit operator command id: OPER (op, target t, arguments a[0..3],
3907note view version v) appended and synced. 0, 25 refused, or a write error.
3908fn jn_oper id ulong op ulong t ulong a view ulong 4 v ulong to ulong
3909r = new ulong 16 0
3910put r 0 9
3911put r 3 id
3912put r 4 op
3913put r 5 t
3914for i ulong 4
3915put r add 6 i get a i
3916end
3917put r 10 v
3918ret jn_append r
3919end
3920
3921note Operator commands in the journal, and word w of the k-th (as jn_oapply).
3922fn jn_nops to ulong
3923ret get jnop 0
3924end
3925
3926fn jn_opw k ulong w ulong to ulong
3927ret get jopr add mul and k 127 8 and w 7
3928end
3929
3930note Commit that objective i ended with result r after t model turns: RESULT
3931note appended and synced. 0, 25 refused, or a write error.
3932fn jn_result i ulong r ulong t ulong to ulong
3933x = new ulong 16 0
3934put x 0 10
3935put x 3 i
3936put x 4 r
3937put x 5 t
3938ret jn_append x
3939end
3940
3941note The result objective i ended with in the journal (0 none), and its
3942note model turns.
3943fn jn_res i ulong to ulong
3944ret get jres mul and i 63 2
3945end
3946
3947fn jn_rturn i ulong to ulong
3948ret get jres add mul and i 63 2 1
3949end
3950
3951note True when job id has a durable START (an operation was granted to it).
3952fn jn_started id ulong to bool
3953k = jn_jfind id
3954if lt k 0
3955ret false
3956end
3957ret ne get jjob add mul ulong k 8 7 0
3958end
3959note Omega Node proposals, authority and cancellation (DRA-288). Concatenate
3960note after transport.om, pool.om, super.om, sched.om and
3961note seed/authority/action.om, before one program with fn main.
3962note A worker proposes and the creator alone decides and commits. A proposal
3963note is typed and bound to its operation identity, the job id and
3964note assignment seq, the worker slot and incarnation, the input epoch and
3965note version it read, and the authority generation it holds. Every effect is
3966note one action record of seed/authority/action.om per operation identity:
3967note started with actstart, closed with actrecon or actlost. A performed
3968note operation is never started again and an UNKNOWN one is never retried;
3969note one with no external effect (class 2) whose worker died is closed as
3970note not performed, so it may run again.
3971note Control page (16384 bytes), cell of worker w at 128*w. Creator writes
3972note 0 control seq, 8 cancel target (assignment seq, 0 none), 16 decided
3973note proposal seq, 24 verdict, 32 generation at the decision. Worker
3974note writes 64 control ack seq, 72 proposal seq, 80 report seq, 88 report
3975note outcome, 96 tool wait status, 104 paused as last seen, 112 running tool
3976note pid (its process group), 120 tool output bytes held. Word 8192 mirrors
3977note the authority generation and 8200 the paused flag.
3978note Proposal page: 4096 bytes per worker at 4096*w. Words 0..15: 0 version
3979note 1, 1 kind 1, 2 job id, 3 assignment seq, 4 slot, 5 incarnation, 6 input
3980note epoch, 7 input version, 8 generation, 9..12 operation identity, 13 class
3981note (1 external effect, 2 no external effect, 3 idempotent external effect
3982note whose completion is visible as a key, see journal.om), 14 and 15 zero.
3983note Bytes 128..4095 hold the running tool's output.
3984note Verdicts: 0 granted, 1 malformed, 2 not the worker's assignment, 3 other
3985note incarnation, 4 stale input, 5 stale authority, 6 cancelled, 7 already
3986note performed or UNKNOWN, 8 attempt limit, 9 refused by the action contract.
3987note Outcomes, as action.om receipts: 0 UNKNOWN, 1 not performed, 2 performed.
3988text audevnul "/dev/null"
3989global aumap long 2
3990global auctl int 64
3991global auinc ulong 64
3992global augen ulong 1
3993global aurec ulong 2048
3994global auuse ulong 64
3995global aujob ulong 64
3996global aured ulong 64
3997global aupf uchar 8
3998global aufd int 1
3999
4000fn au_cell w ulong off ulong to ulong
4001ret mapget get aumap 0 add mul w 128 off 8
4002end
4003
4004fn au_set w ulong off ulong v ulong to void
4005r = mapset get aumap 0 add mul w 128 off 8 v
4006end
4007
4008fn au_pw w ulong i ulong to ulong
4009ret mapget get aumap 1 add mul w 4096 mul i 8 8
4010end
4011
4012fn au_spw w ulong i ulong v ulong to void
4013r = mapset get aumap 1 add mul w 4096 mul i 8 8 v
4014end
4015
4016fn au_pfd off ulong to int
4017v = ulong get aupf off
4018v = or v shl ulong get aupf add off 1 8
4019v = or v shl ulong get aupf add off 2 16
4020v = or v shl ulong get aupf add off 3 24
4021ret int v
4022end
4023
4024note Poll record at off; a negative fd keeps its bit pattern, so the kernel
4025note ignores that record.
4026fn au_lpfd p edit uchar 40 off ulong fd int to void
4027u = bitcast uint fd
4028for i ulong 4
4029put p add off i uchar and shr u uint mul i 8 255
4030end
4031put p add off 4 1
4032end
4033
4034note Creator, after sup_init and before any spawn: map the control and
4035note proposal pages, one control eventfd per worker slot, generation 1.
4036note 0, -6 not creator, or a negative errno.
4037fn auth_new c long to long
4038if not pool_mine c
4039ret neg long 6
4040end
4041a = mapnew neg int 1 0 16384 3 33
4042if le a 0
4043ret a
4044end
4045b = mapnew neg int 1 0 262144 3 33
4046if le b 0
4047r = mapdrop a
4048ret b
4049end
4050put aumap 0 a
4051put aumap 1 b
4052for i ulong 64
4053put auctl i neg int 1
4054put auinc i 0
4055put auuse i 0
4056put aujob i 0
4057put aured i 0
4058end
4059n = mapget c 48 8
4060for i ulong n bound 64
4061e = eventfd uint 0
4062if lt e 0
4063x = auth_free
4064ret e
4065end
4066put auctl i int e
4067end
4068put augen 0 1
4069r = mapset a 8192 8 1
4070r = mapset a 8200 8 0
4071ret 0
4072end
4073
4074note Creator: close the control eventfds and drop both pages.
4075fn auth_free to long
4076for i ulong 64
4077if ge get auctl i 0
4078r = fclose get auctl i
4079end
4080put auctl i neg int 1
4081end
4082r = mapdrop get aumap 1
4083if lt r 0
4084ret r
4085end
4086ret mapdrop get aumap 0
4087end
4088
4089note Creator: start a new incarnation of worker slot w (sup_spawn). The child
4090note inherits its incarnation number. 0 in the child, else as sup_spawn.
4091fn auth_spawn c long w ulong to long
4092if ge w 64
4093ret neg long 7
4094end
4095if ne get swpid w 0
4096ret neg long 16
4097end
4098put auinc w add get auinc w 1
4099ret sup_spawn c w
4100end
4101
4102note Creator: post control to worker w. A nonzero target asks it to cancel
4103note that assignment. The state words go first, then the seq, then the hint.
4104fn au_ctl w ulong target ulong to void
4105if gt target 0
4106au_set w 8 target
4107end
4108au_set w 0 add au_cell w 0 1
4109r = node_notify get auctl w
4110end
4111
4112note Worker w: bounded control handler. Records the paused flag it saw and
4113note acknowledges the control seq; returns the cancel target (0 none).
4114fn auth_wctl w ulong to ulong
4115s = au_cell w 0
4116au_set w 104 mapget get aumap 0 8200 8
4117au_set w 64 s
4118ret au_cell w 8
4119end
4120
4121note Worker w: wait up to interval ns. 3 creator gone, 2 stop, 4 control,
4122note 5 descriptor x readable or hung up (x negative: none), 1 work hint,
4123note 0 timeout, or a negative errno. Creator loss outranks stop, stop
4124note outranks control, control outranks output and work. Control and work
4125note hints are drained; reread the shared state after any return.
4126fn auth_wwait c long w ulong x int interval long to long
4127p = new uchar 40 0
4128if ge w 64
4129ret neg long 7
4130end
4131iv = interval
4132if pool_stopped c
4133iv = long 0
4134end
4135au_lpfd p 0 get swwake w
4136au_lpfd p 8 get swstop w
4137au_lpfd p 16 get swpar 0
4138au_lpfd p 24 get auctl w
4139au_lpfd p 32 x
4140r = pollfds p 0 40 iv
4141if lt r 0
4142ret r
4143end
4144if ne get p 22 0
4145ret 3
4146end
4147if pool_stopped c
4148ret 2
4149end
4150if ne and get p 14 1 0
4151r = node_drain get swstop w
4152ret 2
4153end
4154if ne get p 30 0
4155r = node_drain get auctl w
4156ret 4
4157end
4158if ne get p 38 0
4159ret 5
4160end
4161if ne and get p 6 1 0
4162r = node_drain get swwake w
4163ret 1
4164end
4165ret 0
4166end
4167
4168note Worker w: propose the effect of assignment q with operation identity
4169note op and class (1 external effect, 2 none, 3 keyed idempotent). The words go first, then the
4170note proposal seq, then an ack to the creator. 0 or the ack refusal.
4171fn auth_propose w ulong q ulong op view ulong 4 class ulong to long
4172if ge w 64
4173ret neg long 7
4174end
4175au_spw w 0 1
4176au_spw w 1 1
4177au_spw w 2 sc_cell w 8
4178au_spw w 3 q
4179au_spw w 4 w
4180au_spw w 5 get auinc w
4181au_spw w 6 sc_cell w 16
4182au_spw w 7 sc_cell w 24
4183au_spw w 8 mapget get aumap 0 8192 8
4184for i ulong 4
4185au_spw w add 9 i get op i
4186end
4187au_spw w 13 class
4188au_spw w 14 0
4189au_spw w 15 0
4190au_set w 72 q
4191ret sup_ack w
4192end
4193
4194note Worker w: wait up to interval ns for the decision on proposal q,
4195note handling control meanwhile. The verdict (0 granted), 102 stop, 103
4196note creator gone, 110 no decision yet, or a negative errno.
4197fn auth_wdecide c long w ulong q ulong interval long to long
4198dl = add ulong monons ulong interval
4199for it ulong 100000000
4200if eq au_cell w 16 q
4201ret long au_cell w 24
4202end
4203now = ulong monons
4204if ge now dl
4205ret 110
4206end
4207r = auth_wwait c w neg int 1 long sub dl now
4208if lt r 0
4209ret r
4210end
4211if or eq r 2 eq r 3
4212ret add 100 r
4213end
4214if eq r 4
4215t = auth_wctl w
4216end
4217end
4218ret 110
4219end
4220
4221note Worker w: fork the packed argv pk (pn bytes) as the leader of a new
4222note process group, stdin on /dev/null, stdout and stderr on a pipe whose
4223note read end is left in aufd. Both sides call setpgid, so the group exists
4224note before either side goes on. The pid, or a negative errno.
4225fn au_fork w ulong pk view uchar 4096 pn ulong to long
4226r = pipefds aupf 0 8 524288
4227if lt r 0
4228ret r
4229end
4230rfd = au_pfd 0
4231wfd = au_pfd 4
4232dn = fopen audevnul 0 len audevnul 524288 0
4233pid = fork
4234if eq pid 0
4235r = setpgid 0 0
4236r = dupfd wfd 1 0
4237r = dupfd wfd 2 0
4238if ge dn 0
4239r = dupfd int dn 0 0
4240end
4241r = execprog pk 0 pn
4242exit 127
4243end
4244r = fclose wfd
4245if ge dn 0
4246r = fclose int dn
4247end
4248if lt pid 0
4249r = fclose rfd
4250ret pid
4251end
4252r = setpgid int pid int pid
4253au_set w 120 0
4254au_set w 112 ulong pid
4255put aufd 0 rfd
4256ret pid
4257end
4258
4259note Worker w: move available tool output into its proposal page from byte
4260note 128 (bytes past 4096 are dropped). false at end of file or on error.
4261fn au_out w ulong rfd int to bool
4262buf = new uchar 4096 0
4263n = read rfd buf 0 4096
4264if eq n 0
4265ret false
4266end
4267if lt n 0
4268ret or eq n neg long 4 eq n neg long 11
4269end
4270k = au_cell w 120
4271m = ulong n
4272if gt add k m 3968
4273m = sub 3968 k
4274end
4275if gt m 0
4276h = get aumap 1
4277off = add mul w 4096 add 128 k
4278r = mapwrite h off buf 0 m
4279au_set w 120 add k m
4280end
4281ret true
4282end
4283
4284note Outcome of a reaped tool from its wait status: it exited, so it ran
4285note (2); it was killed or crashed, so an external effect is UNKNOWN (0) and
4286note a class 2 tool had none (1).
4287fn au_outcome st ulong class ulong to long
4288if eq and st 127 0
4289ret 2
4290end
4291if eq class 2
4292ret 1
4293end
4294ret 0
4295end
4296
4297note Worker w: finish tool pid of assignment q. Without kill, wait for its
4298note exit while still obeying control. With kill, a tool that has already
4299note exited is not cancelled; otherwise SIGKILL its whole group. Its wait
4300note status alone decides the outcome, never the fact that a kill was sent.
4301note Group members left behind are killed either way.
4302fn au_end c long w ulong q ulong pid long class ulong kill bool to long
4303ws = new uchar 4 0
4304k = kill
4305g = long 0
4306if not k
4307for it ulong 100000000
4308g = waitpid int pid ws 0 4 1
4309if ne g 0
4310break
4311end
4312r = auth_wwait c w neg int 1 5000000
4313if eq r 4
4314if eq auth_wctl w q
4315k = true
4316break
4317end
4318end
4319if or eq r 2 eq r 3
4320k = true
4321break
4322end
4323end
4324end
4325if k
4326g = waitpid int pid ws 0 4 1
4327end
4328if eq g 0
4329r = killpid neg int pid 9
4330if lt r 0
4331r = killpid int pid 9
4332end
4333g = waitpid int pid ws 0 4 0
4334else
4335r = killpid neg int pid 9
4336end
4337r = fclose get aufd 0
4338put aufd 0 neg int 1
4339au_set w 112 0
4340if lt g 0
4341au_set w 96 65535
4342ret 0
4343end
4344st = or ulong get ws 0 shl ulong get ws 1 8
4345au_set w 96 st
4346ret au_outcome st class
4347end
4348
4349note Worker w: run the granted tool pk of assignment q (see au_fork). Its
4350note output goes to the proposal page; cancellation of q, stop or creator
4351note loss kill its process group. Returns the outcome: 1 when q was
4352note cancelled before the tool started or the fork failed, else from
4353note au_end. The wait status is in cell word 96.
4354fn auth_wtool c long w ulong q ulong pk view uchar 4096 pn ulong class ulong to long
4355if eq auth_wctl w q
4356ret 1
4357end
4358pid = au_fork w pk pn
4359if le pid 0
4360ret 1
4361end
4362rfd = get aufd 0
4363for it ulong 100000000
4364r = auth_wwait c w rfd 1000000000
4365if eq r 5
4366if not au_out w rfd
4367ret au_end c w q pid class false
4368end
4369end
4370if eq r 4
4371if eq auth_wctl w q
4372ret au_end c w q pid class true
4373end
4374end
4375if or eq r 2 eq r 3
4376ret au_end c w q pid class true
4377end
4378if lt r 0
4379ret au_end c w q pid class true
4380end
4381end
4382ret au_end c w q pid class true
4383end
4384
4385note Worker w: report the outcome of the granted tool of assignment q.
4386fn auth_wreport w ulong q ulong outcome ulong to long
4387au_set w 88 outcome
4388au_set w 80 q
4389ret sup_ack w
4390end
4391
4392note Creator: the action slot whose operation identity is op, or -1.
4393fn au_find op view ulong 4 to long
4394for k ulong 64
4395if ne get auuse k 0
4396m = true
4397for i ulong 4
4398if ne get aurec add mul k 32 add 8 i get op i
4399m = false
4400end
4401end
4402if m
4403ret long k
4404end
4405end
4406end
4407ret neg long 1
4408end
4409
4410note Creator: a new ready action record for op and class in a free slot,
4411note bound to the current generation; -1 when all 64 are in use.
4412note Identities: operation and request op, scope node, provider class,
4413note policy 1, grant 1. Attempt limit 3, epoch 1, no expiry in practice.
4414fn au_alloc op view ulong 4 class ulong to long
4415for k ulong 64
4416if eq get auuse k 0
4417b = mul k 32
4418for i ulong 32
4419put aurec add b i 0
4420end
4421put aurec b 1
4422put aurec add b 1 1
4423put aurec add b 3 3
4424put aurec add b 4 get augen 0
4425put aurec add b 5 1
4426put aurec add b 6 9223372036854775807
4427for i ulong 4
4428put aurec add b add 8 i get op i
4429put aurec add b add 12 i get op i
4430end
4431put aurec add b 16 1313817669
4432put aurec add b 20 class
4433put aurec add b 24 1
4434put aurec add b 28 1
4435put auuse k 1
4436ret long k
4437end
4438end
4439ret neg long 1
4440end
4441
4442fn au_load k ulong r edit ulong 32 to void
4443for i ulong 32
4444put r i get aurec add mul k 32 i
4445end
4446end
4447
4448fn au_save k ulong r view ulong 32 to void
4449for i ulong 32
4450put aurec add mul k 32 i get r i
4451end
4452end
4453
4454note Creator: commit the start of the operation proposed by worker w.
4455note A ready record moves to the current generation; actstart checks the
4456note state, the attempt limit and the credential. The verdict.
4457fn au_commit w ulong to ulong
4458op = new ulong 4 0
4459r = new ulong 32 0
4460cr = new ulong 32 0
4461for i ulong 4
4462put op i au_pw w add 9 i
4463end
4464k = au_find op
4465if lt k 0
4466k = au_alloc op au_pw w 13
4467if lt k 0
4468ret 9
4469end
4470end
4471au_load ulong k r
4472if and eq get r 1 1 ne get r 4 get augen 0
4473put r 4 get augen 0
4474end
4475for i ulong 32
4476put cr i get r i
4477end
4478put cr 1 1
4479put cr 2 1
4480put cr 3 0
4481put cr 4 get augen 0
4482put cr 6 0
4483put cr 7 0
4484e = actstart r cr
4485if eq e 0
4486au_save ulong k r
4487put aujob w add ulong k 1
4488ret 0
4489end
4490if or eq e 21 eq e 22
4491ret 7
4492end
4493if eq e 23
4494ret 8
4495end
4496if eq e 31
4497ret 5
4498end
4499ret 9
4500end
4501
4502note Creator: a proposal is stale when the input it read is not the
4503note assignment's, its epoch is no longer current, or a newer version on the
4504note job's dependency topics has been published since.
4505fn au_stale c long w ulong j ulong to bool
4506if ne au_pw w 6 sc_cell w 16
4507ret true
4508end
4509if ne au_pw w 7 sc_cell w 24
4510ret true
4511end
4512if ne au_pw w 6 pool_epoch c
4513ret true
4514end
4515t = get sjdep j
4516if eq t 0
4517ret false
4518end
4519h = pool_current c
4520if le h 0
4521ret true
4522end
4523ret ne node_next h au_pw w 7 t 0
4524end
4525
4526note Creator: check the pending proposal of worker w; the verdict.
4527fn au_valid c long w ulong to ulong
4528q = au_cell w 72
4529if or ne au_pw w 0 1 ne au_pw w 1 1
4530ret 1
4531end
4532cl = au_pw w 13
4533if or eq cl 0 gt cl 3
4534ret 1
4535end
4536if or ne au_pw w 14 0 ne au_pw w 15 0
4537ret 1
4538end
4539b = get scbusy w
4540if eq b 0
4541ret 2
4542end
4543j = sub b 1
4544if ne get sjst j 3
4545ret 2
4546end
4547if or ne au_pw w 3 q ne q sc_cell w 0
4548ret 2
4549end
4550if or ne au_pw w 2 get sjid j ne au_pw w 4 w
4551ret 2
4552end
4553if ne au_pw w 5 get auinc w
4554ret 3
4555end
4556if au_stale c w j
4557ret 4
4558end
4559if ne au_pw w 8 get augen 0
4560ret 5
4561end
4562if eq au_cell w 8 q
4563ret 6
4564end
4565ret 0
4566end
4567
4568note Creator: decide worker w's pending proposal. A grant is journaled
4569note (START, made durable) before the verdict is published; if the journal
4570note refuses, the action is closed as not performed and the verdict is 9.
4571fn au_decide c long w ulong to void
4572q = au_cell w 72
4573v = au_valid c w
4574if eq v 0
4575v = au_commit w
4576end
4577if eq v 0
4578if ne au_jstart w 0
4579e = au_recon sub get aujob w 1 1
4580put aujob w 0
4581v = 9
4582end
4583end
4584if eq v 0
4585jn_hit 2
4586end
4587au_set w 24 v
4588au_set w 32 get augen 0
4589au_set w 16 q
4590r = sup_wake w
4591end
4592
4593fn au_jstart w ulong to ulong
4594op = new ulong 4 0
4595k = sub get aujob w 1
4596for i ulong 4
4597put op i au_pw w add 9 i
4598end
4599b = mul k 32
4600ret jn_start op au_pw w 13 au_pw w 2 get aurec add b 2 get aurec add b 4
4601end
4602
4603note Journal DONE for the action in slot b-1 of worker w's job.
4604fn au_jdone b ulong o ulong w ulong how ulong to ulong
4605op = new ulong 4 0
4606k = sub b 1
4607for i ulong 4
4608put op i get aurec add mul k 32 add 8 i
4609end
4610ret jn_done op o au_pw w 2 how
4611end
4612
4613note Creator: apply receipt outcome o to action slot k (actrecon). An
4614note outcome of 0 leaves the action UNKNOWN.
4615fn au_recon k ulong o ulong to ulong
4616r = new ulong 32 0
4617s = new ulong 32 0
4618au_load k r
4619for i ulong 32
4620put s i get r i
4621end
4622put s 0 1
4623put s 1 o
4624put s 3 1
4625put s 6 0
4626put s 7 0
4627e = actrecon r s
4628au_save k r
4629ret e
4630end
4631
4632note Creator: record worker w's report. DONE is made durable before the
4633note action record changes; if the journal refuses, the report stays
4634note unrecorded and is tried again at the next step.
4635fn au_record w ulong to void
4636b = get aujob w
4637if eq b 0
4638put aured w au_cell w 80
4639ret
4640end
4641jn_hit 3
4642if ne au_jdone b au_cell w 88 w 1 0
4643ret
4644end
4645jn_hit 4
4646put aured w au_cell w 80
4647put aujob w 0
4648e = au_recon sub b 1 au_cell w 88
4649end
4650
4651note Creator, after worker w was reaped: kill the process group of a tool
4652note it left running and close its in-flight action. One of class 2 (no
4653note external effect) is not performed (DONE how 6, as jn_settle closes it
4654note at a restart), so its rerunnable job may start again; any other is
4655note UNKNOWN (actlost, DONE how 2) and never runs again (DRA-292).
4656fn au_lost w ulong to void
4657r = new ulong 32 0
4658p = au_cell w 112
4659if gt p 0
4660x = killpid neg int p 9
4661au_set w 112 0
4662end
4663b = get aujob w
4664if eq b 0
4665ret
4666end
4667k = sub b 1
4668if eq get aurec add mul k 32 20 2
4669if ne au_jdone b 1 w 6 0
4670ret
4671end
4672put aujob w 0
4673e = au_recon k 1
4674ret
4675end
4676if ne au_jdone b 0 w 2 0
4677ret
4678end
4679put aujob w 0
4680au_load k r
4681e = actlost r
4682au_save k r
4683end
4684
4685note Creator, after sched_step: make the dispatch decisions so far durable
4686note (jn_drain), record tool reports, close the actions of reaped workers,
4687note and decide pending proposals unless paused. Returns the number of
4688note proposals decided.
4689fn auth_step c long to ulong
4690dr = jn_drain
4691n = mapget c 48 8
4692k = ulong 0
4693for w ulong n bound 64
4694if gt au_cell w 80 get aured w
4695au_record w
4696end
4697if gt get swpid w 0
4698if and eq get scpause 0 0 gt au_cell w 72 au_cell w 16
4699au_decide c w
4700k = add k 1
4701end
4702else
4703au_lost w
4704end
4705end
4706ret k
4707end
4708
4709note Creator: revoke the current authority. The generation moves on, so a
4710note pending or later proposal made under the old one is refused (5), and
4711note every granted tool still running is told to cancel. Its outcome is
4712note recorded as reported: performed if it had already exited, UNKNOWN if
4713note it was killed. Returns the new generation, or 0 with nothing changed
4714note when the journal refused REVOKE.
4715fn auth_revoke c long to ulong
4716g = add get augen 0 1
4717if ne jn_rec 6 g 0 0 0
4718ret 0
4719end
4720put augen 0 g
4721r = mapset get aumap 0 8192 8 g
4722n = mapget c 48 8
4723for w ulong n bound 64
4724if and gt get swpid w 0 gt get aujob w 0
4725au_ctl w au_cell w 16
4726end
4727end
4728ret g
4729end
4730
4731note Creator: pause (on true) or resume. While paused no job is assigned and
4732note no proposal is decided; running tools go on. Every live worker gets a
4733note control notification and acknowledges it. Returns sched_pause's count,
4734note or -5 with nothing changed when the journal refused PAUSE.
4735fn auth_pause c long on bool to long
4736v = ulong 0
4737if on
4738v = 1
4739end
4740if ne jn_rec 7 v 0 0 0
4741ret neg long 5
4742end
4743r = mapset get aumap 0 8200 8 v
4744n = mapget c 48 8
4745for w ulong n bound 64
4746if gt get swpid w 0
4747au_ctl w 0
4748end
4749end
4750ret long sched_pause c on
4751end
4752
4753note Creator: cancel job j. 1 freed before it started (no effect), 2 its
4754note worker was told to cancel and the outcome comes from its report (a
4755note pending proposal is refused with 6), 3 already finished, 4 UNKNOWN,
4756note -3 no such job. CANCEL is journaled first; a job freed before it
4757note started is also released (outcome 300). -5 when the journal refused
4758note CANCEL (nothing changed) or that RELEASE (the job is freed, and the
4759note journal, now failed, releases it as cancelled at recovery).
4760fn auth_cancel c long j ulong to long
4761if ge j 64
4762ret neg long 3
4763end
4764id = get sjid j
4765if ne jn_rec 8 id 0 0 0
4766ret neg long 5
4767end
4768s = sched_cancel j
4769if eq s 1
4770if ne jn_rec 5 id 300 0 0
4771ret neg long 5
4772end
4773end
4774if eq s 2
4775w = get sjw j
4776au_ctl w sc_cell w 0
4777end
4778ret s
4779end
4780
4781note Creator, after auth_new, jn_open and sched_new, before any spawn:
4782note settle and release from the journal (jn_recover), then install every
4783note journaled action into the action records, move the generation past
4784note every journaled one and restore the paused flag. 0 or an error.
4785fn auth_recover c long to ulong
4786e = jn_recover c
4787if ne e 0
4788ret e
4789end
4790op = new ulong 4 0
4791for k ulong 64
4792if ne jn_act k 0 0
4793for i ulong 4
4794put op i jn_act k add 1 i
4795end
4796s = au_alloc op jn_act k 5
4797if lt s 0
4798ret 32
4799end
4800b = mul ulong s 32
4801put aurec add b 1 jn_act k 6
4802put aurec add b 2 jn_act k 7
4803put aurec add b 4 jn_act k 8
4804end
4805end
4806put augen 0 jn_gen
4807r = mapset get aumap 0 8192 8 jn_gen
4808if ne jn_stat 2 0
4809r = mapset get aumap 0 8200 8 1
4810x = sched_pause c true
4811end
4812ret 0
4813end
4814
4815note Creator: word i (0..31) of the action record for op, or -1 if none.
4816fn auth_act op view ulong 4 i ulong to long
4817k = au_find op
4818if or lt k 0 gt i 31
4819ret neg long 1
4820end
4821ret long get aurec add mul ulong k 32 i
4822end
4823
4824note Worker w's control or proposal cell word, for the creator's view.
4825fn auth_cell w ulong off ulong to ulong
4826ret au_cell w off
4827end
4828
4829note Copy up to 3968 bytes of worker w's tool output into dst; the count.
4830fn auth_wout w ulong dst edit uchar 4096 to ulong
4831n = au_cell w 120
4832if gt n 3968
4833n = 3968
4834end
4835if gt n 0
4836r = mapread get aumap 1 add mul w 4096 128 dst 0 n
4837end
4838ret n
4839end
4840note Omega Node durable scheduling-policy replay (DRA-398). Concatenate
4841note after transport.om, pool.om, super.om, sched.om,
4842note seed/authority/action.om, journal.om and auth.om.
4843note A reference creator R runs 10 dispatch events on journal a. A creator
4844note C runs the same first k events on journal b and dies (77); C2
4845note recovers journal b and runs the remaining events. Each event resumes
4846note dispatch once (auth_pause false, then true), then lets every assigned
4847note job finish and releases them in slot order, so each event ends at a
4848note quiescent point. Trace record per event (10 words): 0, 1 the ids
4849note assigned, 2 cursor, 3 reserved job id, 4 fold of (slot, id, bypass
4850note count) over ready jobs, 5 policy words (with the ticket lifetime), 6
4851note fold of (id, need, estimate, uncertainty, topics) over waiting and
4852note ready jobs, 7 capacities, 8 sum of the ready jobs' bypass counts, 9
4853note state and bypass count of job 7001. C2's state right after recovery
4854note must equal R's after event k (words 2..9), and the C and C2 trace
4855note must equal R's.
4856note Jobs: 14 ready jobs needing 1..3 of 4 CPU, two workers, window 4,
4857note dispatch budget 2, bypass budget 2, ticket lifetime 777, and job 7001
4858note (3 CPU) waiting on topic 8, which the creator publishes before event
4859note 1 (39 if refused), so it is ready, by publication, across the crash.
4860note Policy 2 adds three waiting jobs, declared topics, estimates and
4861note uncertainties (one on a waiting job).
4862note Exits: 10..22 creator checks; per case base b: b R failed, b+1 C did
4863note not stop at 77, b+2 C2 failed, b+3 recovered state differs, b+4 traces
4864note differ, b+5 the case would not test replay (no bypass count, a zero
4865note cursor or job 7001 not ready at the boundary, or no reservation in
4866note the run), b+6 C3, which
4867note recovers journal b again after C2, differs from R's final state (or R's
4868note final cursor equals its boundary cursor, so the check would not see
4869note C2's FAIR records missing). Mid-event crash
4870note (90): C' dies right after a dispatch and its PAUSE, before the creator
4871note collects or releases the jobs it assigned; C2' must see the cursor, reservation and bypass counts of the
4872note jobs that were not assigned (93; 95 nothing assigned or bypassed, or
4873note the event moved neither cursor nor reservation, so losing its FAIR
4874note would go unseen). Records refused (96 a FAIR that repeats the last
4875note event, 97 a FAIR naming a free slot, 98 an OPEN of another SPVER, 23
4876note an ADMIT into a held slot, 24 a FAIR assigning and bypassing one slot,
4877note 26 a FAIR assigning a waiting job, 27 a readiness record for a ready
4878note job (and a valid one at the last event refused), 25 a FAIR reserving
4879note a cancelled job, 28 a first FAIR that both assigns and readies).
4880note Failed journal (40..43): revoke, pause, cancel,
4881note admission, policy and goal change nothing. 44: a full replay ring
4882note stops dispatch; 47: and refuses publication. 29: recovery refuses a
4883note fence past the incarnation range.
4884text rpja "build/node/rpa/journal"
4885text rpla "build/node/rpa/lock"
4886text rpjb "build/node/rpb/journal"
4887text rplb "build/node/rpb/lock"
4888text rpda "build/node/rpa"
4889text rpdb "build/node/rpb"
4890text rpok "PASS node replay\n"
4891global rpm long 1
4892global rpp uchar 128
4893
4894fn rp_ok ok bool code int to void
4895if not ok
4896exit code
4897end
4898end
4899
4900note Path k (0 journal a, 1 lock a, 2 journal b, 3 lock b, 4 dir a, 5 dir
4901note b) into rpp; its length.
4902fn rp_path k ulong to ulong
4903n = ulong 0
4904if eq k 0
4905n = len rpja
4906for i ulong n bound 64
4907put rpp i get rpja i
4908end
4909end
4910if eq k 1
4911n = len rpla
4912for i ulong n bound 64
4913put rpp i get rpla i
4914end
4915end
4916if eq k 2
4917n = len rpjb
4918for i ulong n bound 64
4919put rpp i get rpjb i
4920end
4921end
4922if eq k 3
4923n = len rplb
4924for i ulong n bound 64
4925put rpp i get rplb i
4926end
4927end
4928if eq k 4
4929n = len rpda
4930for i ulong n bound 64
4931put rpp i get rpda i
4932end
4933end
4934if eq k 5
4935n = len rpdb
4936for i ulong n bound 64
4937put rpp i get rpdb i
4938end
4939end
4940ret n
4941end
4942
4943fn rp_clean to void
4944for k ulong 4
4945n = rp_path k
4946r = unlink rpp 0 n
4947end
4948end
4949
4950fn rp_open d ulong ms ulong to ulong
4951o = new ulong 4 0
4952put o 0 398
4953put o 1 7
4954put o 2 7
4955put o 3 7
4956n = rp_path add d 4
4957p = new uchar 128 0
4958for i ulong n bound 128
4959put p i get rpp i
4960end
4961ret jn_open p n o ms
4962end
4963
4964note The CPU job i (0..13) needs.
4965fn rp_cpu i ulong to ulong
4966ret add 1 rem mul i 7 3
4967end
4968
4969fn rp_worker c long w ulong to int
4970out = new uchar 4096 0
4971for it ulong 1000000000
4972q = sched_wjob w
4973if gt q 0
4974d = sched_wdone w q 2 out 0
4975end
4976r = auth_wwait c w neg int 1 1000000000
4977if or eq r 2 eq r 3
4978ret 0
4979end
4980if eq r 4
4981t = auth_wctl w
4982end
4983if lt r 0
4984ret 45
4985end
4986end
4987ret 46
4988end
4989
4990note Creator on journal d (0 a, 1 b): open it, recover, spawn two workers.
4991fn rp_setup d ulong to long
4992c = pool_new 4 2
4993rp_ok gt c 0 10
4994rp_ok eq sup_init c 0 11
4995rp_ok eq sched_new c 32 8 0 12
4996rp_ok eq auth_new c 0 13
4997rp_ok eq rp_open d 3000 0 14
4998rp_ok eq auth_recover c 0 15
4999for w ulong 2
5000p = auth_spawn c w
5001if eq p 0
5002exit rp_worker c w
5003end
5004rp_ok gt p 0 16
5005end
5006ret c
5007end
5008
5009fn rp_slot id ulong to ulong
5010for j ulong 64
5011s = get sjst j
5012if and eq get sjid j id and ge s 1 le s 2
5013ret j
5014end
5015end
5016ret 64
5017end
5018
5019note A fresh creator: paused, capacities, policy, the jobs and (policy 2)
5020note the waiting jobs and declarations, all journaled.
5021fn rp_load c long pol ulong to void
5022rp_ok ge auth_pause c true 0 17
5023rp_ok eq jn_cap 4 0 17
5024rp_ok eq jn_policy pol 4 2 2 777 0 17
5025for i ulong 14
5026rp_ok ge jn_admitr add 5000 i 0 0 rp_cpu i 0 18
5027end
5028rp_ok ge jn_admitr 7001 8 0 3 0 18
5029if eq pol 2
5030for i ulong 3
5031rp_ok ge jn_admitr add 6000 i shl 1 i 0 0 0 18
5032end
5033rp_ok eq jn_goal rp_slot 5003 3 40 5 0 19
5034rp_ok eq jn_goal rp_slot 5008 4 60 7 0 19
5035rp_ok eq jn_goal rp_slot 5005 0 10 1 0 19
5036rp_ok eq jn_goal rp_slot 5011 0 20 3 0 19
5037rp_ok eq jn_goal rp_slot 6000 0 30 9 0 19
5038end
5039end
5040
5041fn rp_put base ulong e ulong i ulong v ulong to void
5042r = mapset get rpm 0 mul add add base mul e 10 i 8 8 v
5043end
5044
5045fn rp_get base ulong e ulong i ulong to ulong
5046ret mapget get rpm 0 mul add add base mul e 10 i 8 8
5047end
5048
5049note Step until no job runs, then release every finished one, slot order.
5050fn rp_drive c long to void
5051dl = add ulong monons 20000000000
5052for t ulong 100000000
5053r = sched_step c 5000000
5054k = auth_step c
5055busy = false
5056for j ulong 64
5057if eq get sjst j 3
5058busy = true
5059end
5060end
5061if not busy
5062for j ulong 64
5063if eq get sjst j 4
5064rp_ok ge jn_release j 0 21
5065end
5066end
5067ret
5068end
5069rp_ok lt ulong monons dl 22
5070end
5071end
5072
5073note Fold of (slot, id, bypass count) over ready jobs other than ids x, y;
5074note with sum true the sum of those bypass counts instead.
5075fn rp_fbyp x ulong y ulong sum bool to ulong
5076h = ulong 1469598103934665603
5077n = ulong 0
5078for j ulong 64
5079id = get sjid j
5080if and eq get sjst j 2 and ne id x ne id y
5081n = add n get sjbyp j
5082h = mul xor h j 1099511628211
5083h = mul xor h id 1099511628211
5084h = mul xor h get sjbyp j 1099511628211
5085end
5086end
5087if sum
5088ret n
5089end
5090ret h
5091end
5092
5093note Fold of (id, need, estimate, uncertainty, topics) over waiting and
5094note ready jobs.
5095fn rp_fjob to ulong
5096h = ulong 1469598103934665603
5097for j ulong 64
5098s = get sjst j
5099if and ge s 1 le s 4
5100h = mul xor h get sjid j 1099511628211
5101h = mul xor h get sjneed j 1099511628211
5102h = mul xor h get sjest j 1099511628211
5103h = mul xor h get sjunc j 1099511628211
5104h = mul xor h get sjpro j 1099511628211
5105end
5106end
5107ret h
5108end
5109
5110note Job 7001's state and bypass count, 0 when x or y is 7001 or it is not
5111note in the table.
5112fn rp_dep x ulong y ulong to ulong
5113if or eq x 7001 eq y 7001
5114ret 0
5115end
5116for j ulong 64
5117if and eq get sjid j 7001 ne get sjst j 0
5118ret or get sjst j shl get sjbyp j 8
5119end
5120end
5121ret 0
5122end
5123
5124fn rp_rid to ulong
5125r = sched_stat 29
5126if eq r 0
5127ret 0
5128end
5129ret get sjid sub r 1
5130end
5131
5132note Words 2..9 of trace record e (base), x and y excluded from 4, 8, 9.
5133fn rp_state base ulong e ulong x ulong y ulong to void
5134rp_put base e 2 sched_stat 28
5135rp_put base e 3 rp_rid
5136rp_put base e 4 rp_fbyp x y false
5137p = or or sched_stat 24 shl sched_stat 25 8 or shl sched_stat 26 16 shl sched_stat 27 24
5138rp_put base e 5 or p shl sched_stat 31 32
5139rp_put base e 6 rp_fjob
5140rp_put base e 7 sched_caps
5141rp_put base e 8 rp_fbyp x y true
5142rp_put base e 9 rp_dep x y
5143end
5144
5145note Resume dispatch once, pause; record the ids assigned (words 0, 1).
5146fn rp_dispatch c long e ulong base ulong to void
5147rp_ok ge auth_pause c false 0 20
5148rp_ok ge auth_pause c true 0 20
5149a0 = ulong 0
5150a1 = ulong 0
5151for j ulong 64
5152s = get sjst j
5153if and ge s 3 le s 4
5154if eq a0 0
5155a0 = get sjid j
5156else
5157a1 = get sjid j
5158end
5159end
5160end
5161rp_put base e 0 a0
5162rp_put base e 1 a1
5163end
5164
5165note Event e; before event 1 the creator publishes topic 8 (job 7001).
5166fn rp_event c long e ulong base ulong to void
5167pb = new uchar 4096 0
5168if eq e 1
5169rp_ok gt sched_publish c 8 pb 16 0 39
5170end
5171rp_dispatch c e base
5172rp_drive c
5173rp_state base e 0 0
5174end
5175
5176fn rp_down c long to void
5177r = sched_stop c 2000000000
5178sup_closeall
5179jn_close
5180end
5181
5182fn rp_ref pol ulong en ulong to int
5183c = rp_setup 0
5184rp_load c pol
5185for e ulong en
5186rp_event c e 0
5187end
5188rp_down c
5189ret 0
5190end
5191
5192fn rp_crash pol ulong k ulong to int
5193c = rp_setup 1
5194rp_load c pol
5195for e ulong k
5196rp_event c e 1024
5197end
5198ret 77
5199end
5200
5201fn rp_rec k ulong en ulong to int
5202c = rp_setup 1
5203rp_state 2048 0 0 0
5204for e ulong sub en k
5205rp_event c add k e 1024
5206end
5207rp_down c
5208ret 0
5209end
5210
5211note C3: recover journal b again, state at 2048 + 10.
5212fn rp_again to int
5213c = rp_setup 1
5214rp_state 2048 1 0 0
5215rp_down c
5216ret 0
5217end
5218
5219note C': k events, then one dispatch and its PAUSE, state at 3072, die.
5220fn rp_mcrash pol ulong k ulong to int
5221c = rp_setup 1
5222rp_load c pol
5223for e ulong k
5224rp_event c e 1024
5225end
5226rp_dispatch c 0 3072
5227rp_state 3072 0 rp_get 3072 0 0 rp_get 3072 0 1
5228ret 77
5229end
5230
5231note C2': recover, state at 3072 + 10 without the jobs C' assigned.
5232fn rp_mrec to int
5233c = rp_setup 1
5234rp_state 3072 1 rp_get 3072 0 0 rp_get 3072 0 1
5235rp_down c
5236ret 0
5237end
5238
5239fn rp_reap p long to ulong
5240ws = new uchar 4 0
5241g = waitpid int p ws 0 4 0
5242if le g 0
5243ret 999
5244end
5245if ne and get ws 0 127 0
5246ret add 128 ulong and get ws 0 127
5247end
5248ret ulong get ws 1
5249end
5250
5251note A child that should exit want: anything else ends the test with the
5252note child's own check code (10..48, 90..99), or code.
5253fn rp_child x ulong want ulong code int to void
5254if ne x want
5255if or and ge x 10 le x 48 and ge x 90 le x 99
5256exit int x
5257end
5258exit code
5259end
5260end
5261
5262fn rp_zero to void
5263for i ulong 4096
5264r = mapset get rpm 0 mul i 8 8 0
5265end
5266end
5267
5268fn rp_case pol ulong k ulong b int to void
5269en = ulong 10
5270rp_zero
5271rp_clean
5272p = fork
5273if eq p 0
5274exit rp_ref pol en
5275end
5276rp_child rp_reap p 0 b
5277p = fork
5278if eq p 0
5279exit rp_crash pol k
5280end
5281rp_child rp_reap p 77 add b 1
5282p = fork
5283if eq p 0
5284exit rp_rec k en
5285end
5286rp_child rp_reap p 0 add b 2
5287p = fork
5288if eq p 0
5289exit rp_again
5290end
5291rp_child rp_reap p 0 add b 6
5292rp_cmp k en b
5293end
5294
5295fn rp_cmp k ulong en ulong b int to void
5296for i ulong 8
5297rp_ok eq rp_get 2048 0 add i 2 rp_get 0 sub k 1 add i 2 add b 3
5298end
5299seen = false
5300for e ulong en
5301for i ulong 10
5302rp_ok eq rp_get 0 e i rp_get 1024 e i add b 4
5303end
5304if gt rp_get 0 e 3 0
5305seen = true
5306end
5307end
5308rp_ok seen add b 5
5309rp_ok gt rp_get 0 sub k 1 8 0 add b 5
5310rp_ok ne rp_get 0 sub k 1 2 0 add b 5
5311rp_ok eq and rp_get 0 sub k 1 9 255 2 add b 5
5312for i ulong 8
5313rp_ok eq rp_get 2048 1 add i 2 rp_get 0 sub en 1 add i 2 add b 6
5314end
5315rp_ok ne rp_get 0 sub en 1 2 rp_get 0 sub k 1 2 add b 6
5316end
5317
5318fn rp_mid pol ulong k ulong to void
5319rp_zero
5320rp_clean
5321p = fork
5322if eq p 0
5323exit rp_mcrash pol k
5324end
5325rp_child rp_reap p 77 91
5326p = fork
5327if eq p 0
5328exit rp_mrec
5329end
5330rp_child rp_reap p 0 92
5331for i ulong 8
5332rp_ok eq rp_get 3072 1 add i 2 rp_get 3072 0 add i 2 93
5333end
5334rp_ok gt rp_get 3072 0 0 0 95
5335rp_ok gt rp_get 3072 0 8 0 95
5336w2 = rp_get 1024 sub k 1 2
5337w3 = rp_get 1024 sub k 1 3
5338rp_ok or ne rp_get 3072 0 2 w2 ne rp_get 3072 0 3 w3 95
5339end
5340
5341note Journal b as left by the mid-event case: a FAIR repeating the last
5342note event is refused (96), and one assigning a free slot (97).
5343fn rp_dup to int
5344rp_ok eq rp_open 1 3000 0 96
5345r = new ulong 16 0
5346put r 0 13
5347put r 3 get jfair 0
5348put r 6 get jfair 1
5349rp_ok eq jn_put r 25 96
5350for i ulong 16
5351put r i 0
5352end
5353s = ulong 64
5354for j ulong 64
5355if eq get jsl j 0
5356s = j
5357end
5358end
5359rp_ok lt s 64 97
5360put r 0 13
5361put r 3 add get jfair 0 1
5362put r 4 shl 1 s
5363rp_ok eq jn_put r 25 97
5364for i ulong 16
5365put r i 0
5366end
5367put r 0 1
5368put r 3 1
5369put r 5 add SPVER 1
5370rp_ok eq jn_put r 33 98
5371rp_dupv
5372jn_close
5373ret 0
5374end
5375
5376note A live ready job's slot (no deps, not cancelled), or 64.
5377fn rp_rslot to ulong
5378for s ulong 64
5379x = get jsl s
5380if ne x 0
5381b = mul sub x 1 8
5382if and eq get jjob add b 2 0 eq get jjob add b 6 0
5383ret s
5384end
5385end
5386end
5387ret 64
5388end
5389
5390fn rp_fair r edit ulong 16 ev ulong am ulong bm ulong rs ulong rd ulong to void
5391for i ulong 16
5392put r i 0
5393end
5394put r 0 13
5395put r 3 ev
5396put r 4 am
5397put r 5 bm
5398put r 6 get jfair 1
5399put r 7 rs
5400put r 8 rd
5401end
5402
5403note Contradictory records are refused (23..27); valid ones still pass.
5404fn rp_dupv to void
5405r = new ulong 16 0
5406s = rp_rslot
5407rp_ok lt s 64 23
5408f = ulong 64
5409for j ulong 64
5410if eq get jsl j 0
5411f = j
5412end
5413end
5414rp_ok lt f 64 23
5415put r 0 2
5416put r 3 9100
5417put r 7 s
5418rp_ok eq jn_put r 25 23
5419e = add get jfair 0 1
5420rp_fair r e shl 1 s shl 1 s 0 0
5421rp_ok eq jn_put r 25 24
5422for i ulong 16
5423put r i 0
5424end
5425put r 0 2
5426put r 3 9101
5427put r 4 16
5428put r 7 f
5429rp_ok eq jn_put r 0 26
5430rp_fair r e shl 1 f 0 0 0
5431rp_ok eq jn_put r 25 26
5432rp_fair r get jfair 0 0 0 0 shl 1 s
5433rp_ok eq jn_put r 25 27
5434put jfair 3 0
5435rp_fair r e shl 1 s 0 0 shl 1 f
5436rp_ok eq jn_put r 25 28
5437rp_ok eq get jfair 3 0 28
5438rp_ok eq get jrdy sub get jsl f 1 0 28
5439put jfair 3 1
5440rp_fair r get jfair 0 0 0 0 shl 1 f
5441rp_ok eq jn_put r 0 27
5442for i ulong 16
5443put r i 0
5444end
5445put r 0 8
5446put r 3 get jjob add mul sub get jsl s 1 8 1
5447rp_ok eq jn_put r 0 25
5448rp_fair r add get jfair 0 1 0 0 add s 1 0
5449rp_ok eq jn_put r 25 25
5450end
5451
5452note A full replay ring (64 records not yet durable) refuses dispatch (44)
5453note and publication (47: job 7001 stays waiting).
5454fn rp_ring to int
5455c = rp_setup 0
5456rp_load c 1
5457put sfj 1 64
5458put scpause 0 0
5459x = sched_dispatch c
5460rp_ok eq x 0 44
5461rp_ok eq sched_stat 15 1 44
5462rp_ok eq sched_stat 4 0 44
5463pb = new uchar 4096 0
5464rp_ok eq sched_publish c 8 pb 16 neg long 105 47
5465rp_ok eq sched_stat 15 2 47
5466rp_ok eq get sjst rp_slot 7001 1 47
5467put sfj 1 0
5468put scpause 0 1
5469rp_down c
5470ret 0
5471end
5472
5473note A failed journal: nothing it refuses changes the creator (40..43).
5474fn rp_fail to int
5475c = rp_setup 0
5476rp_load c 2
5477j = rp_slot 5000
5478g = get augen 0
5479put jbad 0 1
5480rp_ok eq auth_revoke c 0 40
5481rp_ok eq get augen 0 g 40
5482rp_ok eq auth_pause c false neg long 5 41
5483rp_ok eq get scpause 0 1 41
5484rp_ok eq auth_cancel c j neg long 5 42
5485rp_ok eq get sjst j 2 42
5486rp_ok eq jn_admitr 7000 0 0 1 neg long 5 43
5487rp_ok eq jn_policy 1 8 2 2 1000 neg long 5 43
5488rp_ok eq sched_stat 25 4 43
5489rp_ok eq jn_goal j 1 99 1 neg long 5 43
5490rp_ok eq get sjest j 0 43
5491rp_ok eq jn_cap 8 neg long 5 43
5492rp_ok eq sched_caps 4 43
5493rp_down c
5494ret 0
5495end
5496
5497note Recovery fails closed past the incarnation range (29): fence 2^24 + 1,
5498note which a 24-bit mask would fold onto incarnation 1, is refused and
5499note leaves the scheduler unincarnated, so 2^24 - 1 is then accepted.
5500fn rp_fence to int
5501c = pool_new 4 2
5502rp_ok gt c 0 29
5503rp_ok eq sched_new c 32 8 0 29
5504put jpol 0 0
5505put jfence 0 16777217
5506rp_ok not jn_rpol 29
5507put jfence 0 16777215
5508rp_ok jn_rpol 29
5509ret 0
5510end
5511
5512fn main to int
5513m = mapnew neg int 1 0 32768 3 33
5514rp_ok gt m 0 9
5515put rpm 0 m
5516rp_case 1 3 50
5517rp_case 1 6 60
5518rp_case 2 3 70
5519rp_case 2 6 80
5520rp_mid 1 3
5521p = fork
5522if eq p 0
5523exit rp_dup
5524end
5525rp_child rp_reap p 0 96
5526rp_clean
5527p = fork
5528if eq p 0
5529exit rp_fail
5530end
5531rp_child rp_reap p 0 40
5532rp_clean
5533p = fork
5534if eq p 0
5535exit rp_ring
5536end
5537rp_child rp_reap p 0 44
5538rp_clean
5539p = fork
5540if eq p 0
5541exit rp_fence
5542end
5543rp_child rp_reap p 0 29
5544if ne write 1 rpok 0 len rpok long len rpok
5545ret 100
5546end
5547ret 0
5548end