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

2911 lines · 67368 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 Concatenate after transport.om, pool.om, super.om and sched.om. CPU-only
2371note acceptance of DRA-287 with real forked workers: irrelevant changes,
2372note coalesced hints, no lost wake-up, no barrier behind a slow worker, full
2373note admission and review queues, dead workers, stop under load, idle CPU and
2374note wake latency. No GPU, model or network.
2375text sok "PASS node sched\n"
2376text sidle "idle ticks workers="
2377text screat " creator="
2378text swake " wake rounds="
2379text smean " mean_us="
2380text smax " max_us="
2381text snl "\n"
2382text sproc "/proc/"
2383text sstat "/stat"
2384global stc long 1
2385global stm long 1
2386
2387fn st_ok ok bool code int to void
2388if not ok
2389exit code
2390end
2391end
2392
2393fn st_num v ulong to void
2394d = new uchar 20 0
2395x = v
2396k = ulong 20
2397for i ulong 20
2398k = sub k 1
2399put d k uchar add rem x 10 48
2400x = div x 10
2401if eq x 0
2402break
2403end
2404end
2405r = write 1 d k sub 20 k
2406end
2407
2408note Write "/proc/<pid>/stat" into p; returns its length.
2409fn st_path pid ulong p edit uchar 64 to ulong
2410d = new uchar 20 0
2411for i ulong 6
2412put p i get sproc i
2413end
2414x = pid
2415k = ulong 20
2416for i ulong 20
2417k = sub k 1
2418put d k uchar add rem x 10 48
2419x = div x 10
2420if eq x 0
2421break
2422end
2423end
2424n = ulong 6
2425for i ulong sub 20 k bound 20
2426put p n get d add k i
2427n = add n 1
2428end
2429for i ulong 5
2430put p n get sstat i
2431n = add n 1
2432end
2433ret n
2434end
2435
2436note Sum of fields 14 and 15 (utime, stime) of a /proc stat line of got bytes.
2437fn st_parse b view uchar 1024 got ulong to ulong
2438rp = ulong 0
2439for i ulong got bound 1023
2440if eq get b i 41
2441rp = i
2442end
2443end
2444field = ulong 2
2445t = ulong 0
2446v = ulong 0
2447for i ulong sub got rp bound 1023
2448c = get b add rp i
2449if eq c 32
2450if or eq field 14 eq field 15
2451t = add t v
2452end
2453field = add field 1
2454v = 0
2455else
2456if and ge c 48 le c 57
2457v = add mul v 10 sub ulong c 48
2458end
2459end
2460end
2461ret t
2462end
2463
2464note utime + stime of process pid in clock ticks, from /proc/pid/stat.
2465fn st_ticks pid ulong to ulong
2466p = new uchar 64 0
2467b = new uchar 1024 0
2468n = st_path pid p
2469fd = fopen p 0 n 524288 0
2470if lt fd 0
2471exit 150
2472end
2473got = read int fd b 0 1023
2474r = fclose int fd
2475if le got 0
2476exit 151
2477end
2478ret st_parse b ulong got
2479end
2480
2481note Worker: run the job of assignment q cooperatively for the number of
2482note milliseconds in the low four decimal digits of its id, then report
2483note outcome id+1 and 64 bytes of the id's low byte. 0 done, 2 stopped,
2484note 3 creator gone, 4 wait error, 5 report refused.
2485fn st_run c long w ulong q ulong to int
2486buf = new uchar 4096 0
2487id = sc_cell w 8
2488dl = add ulong monons mul rem id 10000 1000000
2489for t ulong 1000000
2490now = ulong monons
2491if ge now dl
2492break
2493end
2494r = sched_wwait c w long sub dl now
2495if eq r 2
2496ret 2
2497end
2498if eq r 3
2499ret 3
2500end
2501if lt r 0
2502ret 4
2503end
2504end
2505for j ulong 64
2506put buf j uchar and id 255
2507end
2508if ne sched_wdone w q add id 1 buf 64 0
2509ret 5
2510end
2511ret 0
2512end
2513
2514note Worker loop: run an assigned job, else scan the subscribed versions,
2515note record how many it has seen, and wait without a deadline. A worker with
2516note its lazy flag set sleeps 300 ms before each wait, so hints pile up.
2517fn st_worker w ulong to int
2518c = get stc 0
2519m = get stm 0
2520seen = ulong 0
2521for it ulong 1000000000
2522q = sched_wjob w
2523if gt q 0
2524r = st_run c w q
2525if eq r 2
2526ret 0
2527end
2528if ne r 0
2529ret add 60 r
2530end
2531continue
2532end
2533k = sched_wscan c w
2534if eq k neg long 1
2535ret 0
2536end
2537if lt k 0
2538ret 59
2539end
2540seen = add seen ulong k
2541r = mapset m add 64 mul w 8 8 seen
2542if ne mapget m add 600 mul w 8 8 0
2543r = sleepns 300000000
2544end
2545r = sched_wwait c w neg long 1
2546if eq r 2
2547ret 0
2548end
2549if eq r 3
2550ret 41
2551end
2552if lt r 0
2553ret 43
2554end
2555end
2556ret 45
2557end
2558
2559fn st_spawn c long w ulong to bool
2560p = sup_spawn c w
2561if eq p 0
2562exit st_worker w
2563end
2564ret gt p 0
2565end
2566
2567note Fresh pool of 4 arenas for w workers with limits a and rl, plus the test
2568note page. Exits with code on any refusal.
2569fn st_setup w ulong a ulong rl ulong code int to long
2570c = pool_new 4 w
2571st_ok gt c 0 code
2572put stc 0 c
2573m = mapnew neg int 1 0 4096 3 33
2574st_ok gt m 0 code
2575put stm 0 m
2576st_ok eq sup_init c 0 code
2577st_ok eq sched_new c a rl 0 code
2578ret c
2579end
2580
2581fn st_down c long code int to void
2582st_ok ge sched_stop c 2000000000 0 code
2583sup_closeall
2584st_ok eq sched_free 0 code
2585st_ok eq pool_free c 0 code
2586st_ok eq mapdrop get stm 0 0 code
2587end
2588
2589note Step the creator until at least n jobs are in state st, up to ms.
2590fn st_until c long st ulong n ulong ms ulong to bool
2591dl = add ulong monons mul ms 1000000
2592for t ulong 1000000
2593if ge sched_count st st n
2594ret true
2595end
2596if ge ulong monons dl
2597ret false
2598end
2599r = sched_step c 10000000
2600end
2601ret false
2602end
2603
2604note Wait up to ms for worker w's seen count to reach n.
2605fn st_seen w ulong n ulong ms ulong to bool
2606dl = add ulong monons mul ms 1000000
2607for t ulong 1000000
2608if ge mapget get stm 0 add 64 mul w 8 8 n
2609ret true
2610end
2611if ge ulong monons dl
2612ret false
2613end
2614r = sleepns 1000000
2615end
2616ret false
2617end
2618
2619note Phase A: interests w0 topic 1, w1 topic 2, w2 topic 1 and lazy. Topic 4
2620note publications wake nobody. One topic 1 publication wakes w2; the next 29
2621note land while it sleeps and reach it in exactly one more wake. w0 sees all
2622note 30, w1 is never woken.
2623fn st_hints to void
2624buf = new uchar 4096 0
2625c = st_setup 3 8 8 10
2626m = get stm 0
2627st_ok eq sched_want 0 1 0 11
2628st_ok eq sched_want 1 2 0 12
2629st_ok eq sched_want 2 1 0 13
2630r = mapset m 616 8 1
2631for w ulong 3
2632st_ok st_spawn c w 14
2633end
2634r = sleepns 100000000
2635for i ulong 20
2636st_ok gt sched_publish c 4 buf 16 0 15
2637end
2638r = sleepns 500000000
2639for w ulong 3
2640st_ok eq sc_cell w 80 0 16
2641st_ok eq mapget m add 64 mul w 8 8 0 17
2642end
2643st_ok gt sched_publish c 1 buf 16 0 18
2644st_ok st_seen 2 1 3000 19
2645for i ulong 29
2646st_ok gt sched_publish c 1 buf 16 0 29
2647end
2648st_ok st_seen 0 30 3000 44
2649st_ok st_seen 2 30 3000 20
2650r = sleepns 400000000
2651st_ok eq mapget m 64 8 30 21
2652st_ok eq mapget m 80 8 30 22
2653st_ok eq mapget m 72 8 0 23
2654st_ok eq sc_cell 1 80 0 24
2655st_ok eq sc_cell 2 80 2 25
2656st_ok ge sc_cell 0 80 1 26
2657st_ok le sc_cell 0 80 30 27
2658st_down c 28
2659end
2660
2661fn st_admit id ulong deps ulong fl ulong code int to ulong
2662j = sched_admit id deps fl
2663st_ok ge j 0 code
2664ret ulong j
2665end
2666
2667note Phase B: one slow job and six fast ones on three workers. The fast jobs
2668note finish and are reviewed while the slow one still runs (no barrier).
2669fn st_nobar to void
2670buf = new uchar 4096 0
2671c = st_setup 3 8 8 30
2672for w ulong 3
2673st_ok st_spawn c w 31
2674end
2675s = st_admit 11500 0 0 32
2676for i ulong 6
2677j = st_admit add mul add i 2 10000 20 0 0 33
2678end
2679st_ok st_until c 4 6 5000 34
2680st_ok eq get sjst s 3 35
2681for j ulong 64
2682if and eq get sjst j 4 ne j s
2683st_ok eq sched_review j long add get sjid j 1 36
2684end
2685end
2686st_ok eq sched_count 1 4 1 37
2687st_ok st_until c 4 1 5000 38
2688w = get sjw s
2689st_ok eq sched_review s 11501 39
2690st_ok eq sched_result w buf 4096 40
2691st_ok eq get buf 63 236 41
2692st_ok eq sched_count 1 6 0 42
2693st_down c 43
2694end
2695
2696note Phase C: admission refuses past 8 and review holds at 2. Workers keep
2697note finished results instead of taking more work; reviewing drains it all.
2698fn st_full to void
2699c = st_setup 3 8 2 45
2700for w ulong 3
2701st_ok st_spawn c w 46
2702end
2703for i ulong 8
2704j = st_admit add mul add i 1 10000 20 0 0 47
2705end
2706st_ok eq sched_admit 99020 0 0 neg long 2 48
2707st_ok st_until c 4 2 5000 49
2708r = sleepns 300000000
2709for t ulong 20
2710r = sched_step c 10000000
2711end
2712st_ok eq sched_count 4 4 2 50
2713st_ok eq sched_count 3 3 3 51
2714st_ok eq sched_count 2 2 3 52
2715for w ulong 3
2716st_ok eq sc_cell w 64 get scseq w 53
2717end
2718note Jobs in review no longer count as admitted: 3 held results + 3 ready.
2719st_ok eq sched_count 1 3 6 54
2720n = ulong 0
2721for t ulong 400
2722for j ulong 64
2723if eq get sjst j 4
2724st_ok eq sched_review j long add get sjid j 1 55
2725n = add n 1
2726end
2727end
2728if eq n 8
2729break
2730end
2731r = sched_step c 10000000
2732end
2733st_ok eq n 8 56
2734st_ok ge sched_admit 99020 0 0 0 57
2735st_down c 58
2736end
2737
2738note Phase D: a job that waits on topic 8 starts only after a topic 8
2739note publication; topic 16 does not start it.
2740fn st_deps to void
2741buf = new uchar 4096 0
2742c = st_setup 2 8 8 65
2743for w ulong 2
2744st_ok st_spawn c w 66
2745end
2746j = st_admit 10020 8 0 67
2747st_ok eq get sjst j 1 68
2748st_ok gt sched_publish c 16 buf 16 0 69
2749st_ok not st_until c 4 1 300 70
2750st_ok eq get sjst j 1 71
2751st_ok gt sched_publish c 8 buf 16 0 72
2752st_ok st_until c 4 1 3000 73
2753st_ok eq sched_review j 10021 74
2754st_down c 75
2755end
2756
2757note Phase E: kill two workers mid-job. The rerunnable job runs again on the
2758note third worker; the other becomes UNKNOWN and is never run again.
2759note Respawned workers find their old assignment retired.
2760fn st_dead to void
2761c = st_setup 3 8 8 76
2762for w ulong 3
2763st_ok st_spawn c w 77
2764end
2765x = st_admit 11500 0 0 78
2766y = st_admit 21500 0 1 79
2767st_ok st_until c 3 2 2000 80
2768wx = get sjw x
2769wy = get sjw y
2770st_ok eq killpid int get swpid wx 9 0 81
2771st_ok eq killpid int get swpid wy 9 0 82
2772st_ok st_until c 6 1 3000 83
2773st_ok eq get sjatt x 1 84
2774st_ok st_until c 4 1 5000 85
2775st_ok eq get sjatt y 2 86
2776st_ok eq sched_review y 21501 87
2777st_ok eq get sjst x 6 88
2778st_ok eq sched_review x neg long 3 89
2779st_ok st_spawn c wx 90
2780st_ok st_spawn c wy 91
2781st_ok eq sc_cell wx 64 get scseq wx 92
2782st_ok eq sc_cell wy 64 get scseq wy 93
2783for t ulong 30
2784r = sched_step c 10000000
2785end
2786st_ok eq get sjst x 6 94
2787st_ok eq sched_settle x 0 95
2788st_ok eq sched_count 1 6 0 96
2789st_down c 97
2790end
2791
2792note Phase F: stop with eight long jobs admitted and three running. Every
2793note worker leaves cooperatively well inside the deadline; the three running
2794note jobs become UNKNOWN; the five never started stay ready.
2795fn st_stop to void
2796c = st_setup 3 8 8 115
2797for w ulong 3
2798st_ok st_spawn c w 116
2799end
2800for i ulong 8
2801j = st_admit add mul add i 1 10000 5000 0 0 117
2802end
2803st_ok st_until c 3 3 2000 118
2804t0 = ulong monons
2805st_ok eq sched_stop c 2000000000 0 119
2806st_ok lt sub ulong monons t0 1000000000 120
2807st_ok eq sup_live c 0 121
2808st_ok eq sched_count 6 6 3 122
2809st_ok eq sched_count 2 2 5 123
2810for w ulong 3
2811st_ok eq get swst w 0 124
2812end
2813sup_closeall
2814st_ok eq sched_free 0 125
2815st_ok eq pool_free c 0 126
2816st_ok eq mapdrop get stm 0 0 127
2817end
2818
2819note CPU ticks used by both workers (word 1000) and the creator (1008) of the
2820note test page while the creator waits 2 s with nothing to do.
2821fn st_idlecpu c long to void
2822m = get stm 0
2823p0 = ulong get swpid 0
2824p1 = ulong get swpid 1
2825me = ulong getpid
2826a0 = add st_ticks p0 st_ticks p1
2827a1 = st_ticks me
2828r = sched_step c 2000000000
2829b0 = add st_ticks p0 st_ticks p1
2830b1 = st_ticks me
2831r = mapset m 1000 8 sub b0 a0
2832r = mapset m 1008 8 sub b1 a1
2833end
2834
2835note 200 rounds of one topic 1 publication, each seen by both workers before
2836note the next. Returns the sum of the per-round wake latencies in ns.
2837fn st_rounds c long to ulong
2838buf = new uchar 4096 0
2839sum = ulong 0
2840for i ulong 200
2841st_ok gt sched_publish c 1 buf 16 0 134
2842for w ulong 2
2843st_ok st_seen w add i 1 2000 135
2844sum = add sum sc_cell w 88
2845end
2846end
2847ret sum
2848end
2849
2850note Phase G: two idle workers block while the creator idles 2 s; then 200
2851note publication rounds wake each worker exactly once per round (no lost and
2852note no extra wake-ups).
2853fn st_idle to void
2854c = st_setup 2 8 8 130
2855m = get stm 0
2856st_ok eq sched_want 0 1 0 131
2857st_ok eq sched_want 1 1 0 132
2858for w ulong 2
2859st_ok st_spawn c w 133
2860end
2861r = sleepns 200000000
2862st_idlecpu c
2863st_ok le mapget m 1000 8 2 138
2864st_ok le mapget m 1008 8 2 139
2865sum = st_rounds c
2866st_ok eq sc_cell 0 80 200 136
2867st_ok eq sc_cell 1 80 200 137
2868r = write 1 sidle 0 len sidle
2869st_num mapget m 1000 8
2870r = write 1 screat 0 len screat
2871st_num mapget m 1008 8
2872r = write 1 swake 0 len swake
2873st_num 200
2874r = write 1 smean 0 len smean
2875st_num div sum 400000
2876r = write 1 smax 0 len smax
2877mx = sc_cell 0 96
2878if gt sc_cell 1 96 mx
2879mx = sc_cell 1 96
2880end
2881st_num div mx 1000
2882r = write 1 snl 0 1
2883st_down c 140
2884end
2885
2886fn st_isarg to bool
2887a = new uchar 16 0
2888if ne argcnt 2
2889ret false
2890end
2891ret eq argread 1 a 0 16 4
2892end
2893
2894note With one 4-byte argument ("idle"), run only phase G.
2895fn main to int
2896if st_isarg
2897st_idle
2898ret 0
2899end
2900st_hints
2901st_nobar
2902st_full
2903st_deps
2904st_dead
2905st_stop
2906st_idle
2907if ne write 1 sok 0 len sok long len sok
2908ret 100
2909end
2910ret 0
2911end