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

2964 lines · 67702 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 Omega Node scheduling comparison (DRA-399). Concatenate after
2371note transport.om, pool.om, super.om and sched.om. Runs pinned workloads
2372note under arm 0 (FIFO), 1 (fair fitting), 2 (goal-aware, declared estimates
2373note within a factor of 2 of the duration) and 3 (goal-aware given the true
2374note durations: ORACLE, an upper bound, not a policy result) with real
2375note forked workers that sleep for the milliseconds in the low four decimal
2376note digits of their job id, on the host's wall clock. Usage: bench-gate
2377note (five repetitions of every scenario and arm). Prints one line per run:
2378note bench S P R mk_us idle_us dsp_ns dsp_n ev cand maint vread rework res cont ok hwm_kb : waits_us...
2379note S scenario 1..6 (blocked head, long chain, fan-out, sustained arrivals,
2380note queue pressure, worker failure), P arm (0..3), R repetition; mk makespan
2381note (first admission to last review); idle the integral of min(idle live
2382note workers, ready jobs that fit the free capacity) over time, in
2383note worker-microseconds; dsp time inside sched_dispatch from the creator
2384note loop and its calls; ev, cand, maint, vread the scheduler's counters
2385note (events, candidate reads, maintenance reads, value reads); rework the
2386note jobs that went back to ready after starting; res the reservations made
2387note (sched_stat 6); cont the new reservations seen while another ready job
2388note was also at the bypass budget (where check 71 can tell oldest from any);
2389note ok the jobs reviewed with
2390note the expected outcome; hwm_kb the creator's VmHWM; waits from ready to
2391note started, per job, in microseconds (sampled after every creator step
2392note and every publication). Every policy sees the same jobs, requests,
2393note durations, declarations, workers and capacities. Checked in every run:
2394note the capacity invariant (exit 70), a new reservation goes to the oldest
2395note ready job at the bypass budget (71), every job is reviewed once with
2396note its expected outcome before the deadline (72), setup (73..79).
2397text bnok "PASS node bench\n"
2398text bnhd "bench "
2399text bnsp " "
2400text bnco " :"
2401text bnnl "\n"
2402text bnst "/proc/self/status"
2403text bnhw "VmHWM:"
2404global bnc long 1
2405note Run metrics: 0 t0, 1 last review, 2 idle sum ns, 3 last sample, 4
2406note dispatch ns, 5 dispatch calls, 6 reviewed, 7 verified, 8 reworks, 9
2407note jobs in the scenario, 10 next queue entry, 11 killed, 12 reservation
2408note last seen + 1, 13 idle-eligible workers at the last sample, 14 policy,
2409note 15 bypass budget, 16 contested reservations.
2410global bnm ulong 20
2411note Per slot: ready since (ns, 0 not ready), started flag.
2412global bnr ulong 64
2413global bns ulong 64
2414note Waits of this run (us) and their count.
2415global bnwt ulong 256
2416global bnwn ulong 1
2417note Scenario queue: id, deps, request, flags, arrival ms, topics published.
2418global bnqi ulong 64
2419global bnqd ulong 64
2420global bnqr ulong 64
2421global bnqf ulong 64
2422global bnqt ulong 64
2423global bnqp ulong 64
2424global bnqn ulong 1
2425note 1: arm 3, policy 2 given the true durations as estimates (ORACLE).
2426global bne ulong 1
2427
2428fn bn_ok ok bool code int to void
2429if not ok
2430exit code
2431end
2432end
2433
2434fn bn_num v ulong to void
2435d = new uchar 20 0
2436x = v
2437k = ulong 20
2438for i ulong 20
2439k = sub k 1
2440put d k uchar add rem x 10 48
2441x = div x 10
2442if eq x 0
2443break
2444end
2445end
2446r = write 1 d k sub 20 k
2447end
2448
2449fn bn_sp v ulong to void
2450r = write 1 bnsp 0 1
2451bn_num v
2452end
2453
2454fn bn_req cpu ulong to ulong
2455ret cpu
2456end
2457
2458fn bn_run c long w ulong q ulong to int
2459buf = new uchar 4096 0
2460id = sc_cell w 8
2461dl = add ulong monons mul rem id 10000 1000000
2462for t ulong 1000000
2463now = ulong monons
2464if ge now dl
2465break
2466end
2467r = sched_wwait c w long sub dl now
2468if eq r 2
2469ret 2
2470end
2471if eq r 3
2472ret 3
2473end
2474if lt r 0
2475ret 4
2476end
2477end
2478if ne sched_wdone w q add id 1 buf 8 0
2479ret 5
2480end
2481ret 0
2482end
2483
2484fn bn_worker w ulong to int
2485c = get bnc 0
2486for it ulong 1000000000
2487q = sched_wjob w
2488if gt q 0
2489r = bn_run c w q
2490if eq r 2
2491ret 0
2492end
2493if ne r 0
2494ret add 60 r
2495end
2496continue
2497end
2498r = sched_wwait c w neg long 1
2499if eq r 2
2500ret 0
2501end
2502if eq r 3
2503ret 41
2504end
2505if lt r 0
2506ret 43
2507end
2508end
2509ret 45
2510end
2511
2512note Queue entry: job id (duration in its low four digits), deps, CPU,
2513note flags, arrival ms after the start, topics its completion publishes.
2514fn bn_q id ulong deps ulong cpu ulong fl ulong at ulong pro ulong to void
2515n = get bnqn 0
2516put bnqi n id
2517put bnqd n deps
2518put bnqr n cpu
2519put bnqf n fl
2520put bnqt n at
2521put bnqp n pro
2522put bnqn 0 add n 1
2523end
2524
2525note Declared work estimate of job id in ms. Policy 2 is not told the
2526note duration: the estimate is the duration times 1/2, 1, 3/2 or 2, chosen
2527note by a multiplicative hash of the id (fixed, so every run and repetition
2528note declares the same values). Arm 3 (ORACLE) declares the true duration.
2529fn bn_est id ulong to ulong
2530d = rem id 10000
2531if ne get bne 0 0
2532ret d
2533end
2534k = rem div mul id 2654435761 65536 4
2535ret div mul d add k 1 2
2536end
2537
2538note Scenario s into the queue; returns (workers, admission limit).
2539fn bn_load s ulong to ulong
2540put bnqn 0 0
2541if eq s 1
2542bn_q 10600 0 2 0 0 0
2543bn_q 20300 0 4 0 0 0
2544for i ulong 6
2545bn_q add 30200 mul i 10000 0 1 0 0 0
2546end
2547ret 64
2548end
2549if eq s 2
2550for i ulong 8
2551bn_q add 100300 mul i 10000 0 1 0 0 0
2552end
2553bn_q 200150 0 1 0 0 2
2554for i ulong 5
2555bn_q add 210150 mul i 10000 shl ulong 2 i 1 0 0 shl ulong 4 i
2556end
2557ret 64
2558end
2559if eq s 3
2560for i ulong 6
2561bn_q add 300300 mul i 10000 0 1 0 0 0
2562end
2563bn_q 400100 0 1 0 0 1024
2564for i ulong 6
2565bn_q add 410200 mul i 10000 1024 1 0 0 0
2566end
2567ret 64
2568end
2569if eq s 4
2570bn_s4
2571ret 64
2572end
2573if eq s 5
2574bn_s5
2575ret 8
2576end
2577bn_s6
2578ret 64
2579end
2580
2581note Sustained arrivals: 24 jobs of 100..400 ms needing 1..3 CPU, four
2582note every 150 ms.
2583fn bn_s4 to void
2584for i ulong 24
2585d = add 100 mul rem mul i 7 4 100
2586id = add add 500000 mul i 10000 d
2587at = mul div i 4 150
2588bn_q id 0 add 1 rem i 3 0 at 0
2589end
2590end
2591
2592note Queue pressure: 30 jobs of 100..300 ms needing 1..2 CPU offered at
2593note once against an admission limit of 8.
2594fn bn_s5 to void
2595for i ulong 30
2596d = add 100 mul rem mul i 5 3 100
2597id = add add 600000 mul i 10000 d
2598bn_q id 0 add 1 rem i 2 0 0 0
2599end
2600end
2601
2602note Worker failure: 12 rerunnable jobs of 150..450 ms needing 1..3 CPU;
2603note worker 0 is killed 250 ms in while busy.
2604fn bn_s6 to void
2605for i ulong 12
2606d = add 150 mul rem mul i 7 4 100
2607id = add add 700000 mul i 10000 d
2608bn_q id 0 add 1 rem i 3 1 0 0
2609end
2610end
2611
2612note Admit the queue entries that have arrived, in order; a refusal for
2613note room (-2, a ticket) stops until the next call.
2614fn bn_admit now ulong to void
2615for k ulong 64
2616i = get bnm 10
2617if ge i get bnqn 0
2618ret
2619end
2620if lt sub now get bnm 0 mul get bnqt i 1000000
2621ret
2622end
2623j = sched_admitr get bnqi i get bnqd i get bnqf i bn_req get bnqr i
2624if eq j neg long 2
2625ret
2626end
2627bn_ok ge j 0 74
2628e = bn_est get bnqi i
2629r = sched_goal ulong j get bnqp i e e
2630put bnm 10 add i 1
2631end
2632end
2633
2634note Topics job id publishes on completion.
2635fn bn_pro id ulong to ulong
2636for i ulong get bnqn 0 bound 64
2637if eq get bnqi i id
2638ret get bnqp i
2639end
2640end
2641ret 0
2642end
2643
2644fn bn_inv to void
2645for k ulong 4
2646s = ulong 0
2647for j ulong 64
2648if eq get sjheld j 1
2649s = add s sp_need get sjneed j k
2650end
2651end
2652bn_ok eq get suse k s 70
2653bn_ok le get suse k get scap k 70
2654end
2655end
2656
2657note A reservation that appeared since the last sample goes to the oldest
2658note ready job at the bypass budget: no older ready job is at the budget.
2659note Another ready job at the budget makes it contested (bnm 16).
2660fn bn_fair to void
2661r = sched_stat 29
2662if or eq r 0 eq r get bnm 12
2663put bnm 12 r
2664ret
2665end
2666put bnm 12 r
2667a = get sjarr sub r 1
2668n = ulong 0
2669for j ulong 64
2670if and eq get sjst j 2 ne j sub r 1
2671if ge get sjbyp j get bnm 15
2672n = add n 1
2673end
2674if lt get sjarr j a
2675bn_ok lt get sjbyp j get bnm 15 71
2676end
2677end
2678end
2679if gt n 0
2680put bnm 16 add get bnm 16 1
2681end
2682end
2683
2684note Sample after a creator step or publication: waits, reworks, the idle
2685note eligible integral, the invariants.
2686fn bn_sample to void
2687now = ulong monons
2688put bnm 2 add get bnm 2 mul get bnm 13 sub now get bnm 3
2689put bnm 3 now
2690nf = ulong 0
2691for j ulong 64
2692s = get sjst j
2693if eq s 2
2694if ne get bns j 0
2695put bns j 0
2696put bnm 8 add get bnm 8 1
2697end
2698if eq get bnr j 0
2699put bnr j now
2700end
2701if sp_fits get sjneed j 0
2702nf = add nf 1
2703end
2704end
2705if and ge s 3 ne get bnr j 0
2706bn_wait sub now get bnr j
2707put bnr j 0
2708put bns j 1
2709end
2710end
2711put bnm 13 bn_min nf bn_idle
2712bn_inv
2713if ge get bnm 14 1
2714bn_fair
2715end
2716end
2717
2718fn bn_wait ns ulong to void
2719n = get bnwn 0
2720if lt n 256
2721put bnwt n div ns 1000
2722put bnwn 0 add n 1
2723end
2724end
2725
2726fn bn_min a ulong b ulong to ulong
2727if lt a b
2728ret a
2729end
2730ret b
2731end
2732
2733fn bn_idle to ulong
2734c = get bnc 0
2735n = mapget c 48 8
2736k = ulong 0
2737for w ulong n bound 64
2738if and gt get swpid w 0 eq get scbusy w 0
2739k = add k 1
2740end
2741end
2742ret k
2743end
2744
2745note sched_step with the dispatch timed.
2746fn bn_step c long to void
2747d = sup_poll c 1000000
2748bn_ok ge d 0 75
2749n = mapget c 48 8
2750for w ulong n bound 64
2751if gt get swpid w 0
2752r = sup_take get swack w
2753else
2754r = sched_lost c w
2755end
2756end
2757k = sched_collect c
2758t = ulong monons
2759k = sched_dispatch c
2760put bnm 4 add get bnm 4 sub ulong monons t
2761put bnm 5 add get bnm 5 1
2762end
2763
2764note Review finished jobs (outcome id + 1 is verified work); publish what
2765note each declared.
2766fn bn_review c long to void
2767buf = new uchar 4096 0
2768for j ulong 64
2769if eq get sjst j 4
2770id = get sjid j
2771o = sched_review j
2772if eq o long add id 1
2773put bnm 7 add get bnm 7 1
2774end
2775put bnm 6 add get bnm 6 1
2776put bnm 1 ulong monons
2777put bns j 0
2778put bnr j 0
2779p = bn_pro id
2780if ne p 0
2781v = sched_publish c p buf 8
2782bn_ok gt v 0 76
2783bn_sample
2784end
2785end
2786end
2787end
2788
2789fn bn_hwm to ulong
2790b = new uchar 4096 0
2791fd = fopen bnst 0 len bnst 0 0
2792if lt fd 0
2793ret 0
2794end
2795r = pread int fd b 0 4096 long 0
2796x = fclose int fd
2797if le r 0
2798ret 0
2799end
2800n = ulong r
2801h = len bnhw
2802for i ulong 4000
2803if gt add i h n
2804ret 0
2805end
2806m = true
2807for k ulong h bound 8
2808if ne get b add i k get bnhw k
2809m = false
2810end
2811end
2812if m
2813ret bn_digits b add i h n
2814end
2815end
2816ret 0
2817end
2818
2819fn bn_digits b view uchar 4096 i ulong n ulong to ulong
2820v = ulong 0
2821for k ulong 32
2822p = add i k
2823if ge p n
2824ret v
2825end
2826ch = get b p
2827if and ge ch 48 le ch 57
2828v = add mul v 10 sub ulong ch 48
2829else
2830if gt v 0
2831ret v
2832end
2833end
2834end
2835ret v
2836end
2837
2838fn bn_reset pol ulong by ulong to void
2839for i ulong 20
2840put bnm i 0
2841end
2842for j ulong 64
2843put bnr j 0
2844put bns j 0
2845end
2846put bnwn 0 0
2847put bnm 14 pol
2848put bnm 15 by
2849end
2850
2851note One run of scenario s under arm a (policy 0, 1, 2; arm 3 is policy 2
2852note with ORACLE estimates); prints its line.
2853fn bn_one s ulong a ulong rep ulong to void
2854pol = a
2855put bne 0 0
2856if eq a 3
2857pol = 2
2858put bne 0 1
2859end
2860lim = bn_load s
2861c = pool_new 4 2
2862bn_ok gt c 0 73
2863put bnc 0 c
2864bn_ok eq sup_init c 0 73
2865bn_ok eq sched_new c lim 64 0 73
2866bn_ok eq sched_cap bn_req 4 0 73
2867bn_ok eq sched_policy pol 64 64 4 1000 0 73
2868bn_reset a 4
2869put bnm 9 get bnqn 0
2870for w ulong 2
2871p = sup_spawn c w
2872if eq p 0
2873exit bn_worker w
2874end
2875bn_ok gt p 0 73
2876end
2877k = sched_pause c true
2878put bnm 0 ulong monons
2879put bnm 3 get bnm 0
2880bn_admit get bnm 0
2881bn_sample
2882k = sched_pause c false
2883bn_loop c s
2884bn_ok eq get bnm 6 get bnm 9 72
2885bn_ok eq get bnm 7 get bnm 9 72
2886bn_print s a rep
2887bn_ok ge sched_stop c 2000000000 0 77
2888sup_closeall
2889bn_ok eq sched_free 0 77
2890bn_ok eq pool_free c 0 77
2891end
2892
2893fn bn_loop c long s ulong to void
2894dl = add ulong monons 20000000000
2895for t ulong 100000000
2896bn_sample
2897if ge get bnm 6 get bnm 9
2898ret
2899end
2900bn_ok lt ulong monons dl 72
2901if and eq s 6 eq get bnm 11 0
2902if ge sub ulong monons get bnm 0 250000000
2903bn_kill c
2904end
2905end
2906bn_admit ulong monons
2907bn_step c
2908bn_sample
2909bn_review c
2910end
2911end
2912
2913note Scenario 6: SIGKILL the busy worker 0 once.
2914fn bn_kill c long to void
2915if eq get scbusy 0 0
2916ret
2917end
2918r = killpid int get swpid 0 9
2919put bnm 11 1
2920end
2921
2922fn bn_print s ulong pol ulong rep ulong to void
2923r = write 1 bnhd 0 len bnhd
2924bn_num s
2925bn_sp pol
2926bn_sp rep
2927bn_sp div sub get bnm 1 get bnm 0 1000
2928bn_sp div get bnm 2 1000
2929bn_sp get bnm 4
2930bn_sp get bnm 5
2931bn_sp sched_stat 2
2932bn_sp sched_stat 3
2933bn_sp sched_stat 13
2934bn_sp sched_stat 38
2935bn_sp get bnm 8
2936bn_sp sched_stat 6
2937bn_sp get bnm 16
2938bn_sp get bnm 7
2939bn_sp bn_hwm
2940r = write 1 bnco 0 len bnco
2941for i ulong get bnwn 0 bound 256
2942bn_sp get bnwt i
2943end
2944r = write 1 bnnl 0 1
2945end
2946
2947fn bn_reps to ulong
2948ret 5
2949end
2950
2951fn main to int
2952n = bn_reps
2953for rep ulong n bound 100
2954for s ulong 6
2955for pol ulong 4
2956bn_one add s 1 pol rep
2957end
2958end
2959end
2960if ne write 1 bnok 0 len bnok long len bnok
2961ret 100
2962end
2963ret 0
2964end