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.om

1443 lines · 36647 bytes Raw Blame History
1note Omega Node cooperative scheduling (DRA-287). Concatenate after
2note transport.om, pool.om and super.om, before one program with fn main.
3note One creator admits, assigns, collects and reviews jobs. Workers never
4note write each other's state, and no step waits for every worker.
5note Scheduling page: one 128-byte cell per worker w at 128*w.
6note Creator writes 0 assigned seq, 8 job id, 16 input epoch, 24 input
7note version, 32 interest topics, 40 wake stamp (monons at the last post).
8note Worker writes 64 done seq, 72 outcome, 80 wakes, 88 last wake latency
9note ns, 96 largest wake latency ns.
10note Outbox: worker w's result bytes at 4096*w of a second mapping, written
11note before its done seq. Each word has one writer; the creator writes a
12note worker word only after it has reaped that worker.
13note Job states: 0 free, 1 waiting for deps, 2 ready, 3 running, 4 in review,
14note 6 UNKNOWN (its worker died mid-job and the job is not safe to rerun).
15note
16note Resources and selection policy (P-DRA-14, DRA-395). A request is one
17note ulong of four 16-bit counts, kind k at bits 16k..16k+15: kind 0 CPU
18note slots, 1 memory MiB, 2 inference slots, 3 caller-defined. scap holds
19note the configured capacity of each kind, suse the sum of the needs of the
20note jobs that hold resources (sjheld 1). A job takes its whole need when it
21note is assigned and gives it back when its worker reports done or is reaped,
22note never in part. Every job also needs one idle live worker.
23note sjarr: admission order (waiting age; kept across reruns). sjrdy: order
24note in which the job last became ready. sjbyp: times it was bypassed.
25note Decisions read only these logical counters, never the clock.
26note Policy 1, fair fitting (DRA-396): a pick first probes the reserved job;
27note otherwise it scans a window of win slots from a persistent cursor and
28note takes the oldest admitted ready job that fits. The cursor moves on by
29note win after every window scan, so every slot is examined within
30note ceil(64/win) window scans. A ready job older than a job assigned in
31note the same event was bypassed; once the oldest such job reaches the
32note bypass budget it is reserved: it is offered first, and no other job may
33note use, of any kind, the capacity it still lacks (others may use only
34note free capacity beyond its need). Neither the free capacity it can reach
35note nor its age ever shrinks, so it runs once running work releases enough
36note (liveness assumes: every running job ends, by completion or by its
37note lease, at least one worker stays live, results in review are reviewed,
38note and dispatch is not paused). Requests over capacity are refused at
39note admission.
40note Admission: room is the smaller of the admission headroom and the free
41note table slots. A refused caller holds a ticket; while tickets are held
42note only the first holder may enter, so holders enter in ticket order
43note before anyone new. A ticket not asked about for more than ttl dispatch
44note events lapses (asked at event e, it survives while e + ttl is at least
45note the current event count). An absent head blocks later holders until
46note its ticket lapses; a paused dispatch counts no events, so the block
47note lasts until dispatch resumes.
48note Leases: a running job with a lease is reported once by sched_overdue
49note after its lease passes; the authority then cancels or kills it, which
50note releases its resources.
51note
52note Policy 2, goal-aware fair fitting (DRA-397): policy 1 with a different
53note order among the examined jobs that fit: larger unblock value first (the
54note waiting jobs, to depth 2, that the job's declared topics would make
55note ready), then smaller declared estimate, then older admission. The
56note reservation, the hold-back, feasibility and the bypass budget are
57note those of policy 1 and come first, so ranking cannot starve old work.
58note Values are rescored only when the graph version moved (a job became
59note ready, a waiting job was admitted, a job was cancelled, a declaration
60note changed), once per pick however many changes came before it.
61note Completions, releases and lease expiry replan (every step dispatches)
62note without rescoring. A proposal names a job by id and the graph version
63note it was computed at, and is rejected when that version is no longer
64note current; the version carries the scheduler's incarnation, so a
65note proposal made for another scheduler is never current here.
66const SJCAP ulong 64
67const SPVER ulong 3
68global scmap long 2
69global sclim ulong 2
70global sjst ulong 64
71global sjdep ulong 64
72global sjid ulong 64
73global sjw ulong 64
74global sjatt ulong 64
75global sjfl ulong 64
76global sjout ulong 64
77global sjneed ulong 64
78global sjheld ulong 64
79global sjarr ulong 64
80global sjrdy ulong 64
81global sjbyp ulong 64
82global sjlse ulong 64
83global sjdue ulong 64
84global sttk ulong 64
85global stts ulong 64
86global scbusy ulong 64
87global scseq ulong 64
88global swcur ulong 2
89global scpause ulong 1
90global scap ulong 4
91global suse ulong 4
92note Policy words: 0 policy (0 FIFO head-of-line, 1 fair fitting), 1
93note candidate window in slots, 2 dispatch budget per event, 3 bypass
94note budget, 4 cursor, 5 reserved job slot + 1 (0 none), 6 tickets held,
95note 7 ticket lifetime in dispatch events.
96global scpol ulong 8
97note Counters (sched_stat): 0 admissions, 1 ready stamps, 2 dispatch events,
98note 3 candidate slot reads, 4 jobs assigned, 5 bypasses, 6 reservations
99note made, 7 impossible requests refused, 8 head-of-line stops, 9 reserved
100note jobs assigned, 10 tickets issued, 11 tickets lapsed, 12 leases overdue,
101note 13 maintenance slot reads, 14 window scans, 15 dispatch events refused
102note because the replay ring was full (DRA-398).
103global scst ulong 16
104note Goal-aware ranking (policy 2): sjpro the topics a job declares its
105note completion publishes, sjest its declared work estimate in ms, sjunc
106note that estimate's uncertainty in ms, sjval its unblock value in jobs as
107note last scored. scgv: 0 graph version, 1 version last scored, 2 proposed
108note job slot + 1 (0 none), 3 version the proposal named, 4 proposal state
109note (0 no incarnation yet: proposals refused; 1 accepting; 2 the version
110note is exhausted: proposals refused for the rest of this scheduler). The
111note graph version is the scheduler's incarnation (sched_incarnate, once)
112note times 2^40 plus the bumps since, never more than 2^40 - 1 of them: the
113note bump that would carry into the next incarnation's range exhausts the
114note version instead, so schedulers of different incarnations never share a
115note version. Within one scheduler every admission of a job, cancellation
116note of a waiting or ready one and readiness bumps it, so a proposal whose
117note version is current still names the job it was made for.
118global sjpro ulong 64
119global sjest ulong 64
120global sjunc ulong 64
121global sjval ulong 64
122global scgv ulong 5
123note Goal counters (sched_stat 32..39): 0 rescores, 1 jobs scored, 2 graph
124note version bumps, 3 proposals accepted, 4 proposals stale, 5 proposals
125note refused, 6 slots read by value traversal, 7 proposals used.
126global scgst ulong 8
127note Replay ring (DRA-398). While a journal is attached (sfj 0 is 1), each
128note dispatch event that assigned a job or changed the reservation leaves a
129note record of 6 words in sfr: event count, assigned slot bitmap, bypassed
130note slot bitmap (the slots whose bypass count went up), cursor, reserved
131note slot + 1, and 0. A publication that makes waiting jobs ready leaves a
132note readiness record: the current event count, no assigned or bypassed
133note slot, cursor, reserved slot + 1, and the bitmap of the slots it made
134note ready. sfj 1 counts the records waiting; sfj 2 and 3 are the
135note reservation at the start of the current event and the slots it has
136note assigned so far. The journal drains them before it appends any other
137note record, so a transition is never durable before the dispatch
138note decisions and readiness that came before it. With 64 waiting,
139note dispatch assigns nothing and publication is refused (counter 15)
140note until they are drained.
141global sfr ulong 384
142global sfj ulong 4
143
144fn sc_cell w ulong off ulong to ulong
145ret mapget get scmap 0 add mul w 128 off 8
146end
147
148fn sc_set w ulong off ulong v ulong to void
149r = mapset get scmap 0 add mul w 128 off 8 v
150end
151
152note Creator, before any fork: map the scheduling page and the outboxes and
153note set the limits on admitted (a) and in-review (rl) jobs, each 1..64.
154note Returns 0, -6 not creator, -7 bad limit, or a mapping errno.
155fn sched_new c long a ulong rl ulong to long
156if not pool_mine c
157ret neg long 6
158end
159if or eq a 0 gt a SJCAP
160ret neg long 7
161end
162if or eq rl 0 gt rl SJCAP
163ret neg long 7
164end
165s = mapnew neg int 1 0 8192 3 33
166if le s 0
167ret s
168end
169o = mapnew neg int 1 0 262144 3 33
170if le o 0
171r = mapdrop s
172ret o
173end
174put scmap 0 s
175put scmap 1 o
176put sclim 0 a
177put sclim 1 rl
178for j ulong 64
179put sjst j 0
180put sjheld j 0
181put sjlse j 0
182put sjdue j 0
183put sjpro j 0
184put sjest j 0
185put sjunc j 0
186put sjval j 0
187put scbusy j 0
188put scseq j 0
189end
190for k ulong 4
191put scap k 65535
192put suse k 0
193end
194put sfj 1 0
195for i ulong 16
196put scst i 0
197end
198for i ulong 8
199put scgst i 0
200end
201for i ulong 5
202put scgv i 0
203end
204put scpol 0 1
205put scpol 1 64
206put scpol 2 64
207put scpol 3 4
208put scpol 4 0
209put scpol 5 0
210put scpol 6 0
211put scpol 7 1000
212put scpause 0 0
213ret 0
214end
215
216note Count of kind k (0..3) in packed request req.
217fn sp_need req ulong k ulong to ulong
218ret and shr req mul k 16 65535
219end
220
221note Free capacity of kind k. sched_cap never sets a capacity below the
222note use, so this cannot underflow.
223fn sp_free k ulong to ulong
224ret sub get scap k get suse k
225end
226
227note Does req fit in the free capacity once the reserved request res has
228note what it still lacks? Of each kind, res keeps min(free, its need); res 0
229note is no reservation. This keeps the reserved job's attainable capacity
230note from ever shrinking (the drain).
231fn sp_fits req ulong res ulong to bool
232for k ulong 4
233f = sp_free k
234r = sp_need res k
235a = ulong 0
236if gt f r
237a = sub f r
238end
239if gt sp_need req k a
240ret false
241end
242end
243ret true
244end
245
246note Could req ever run, with the configured capacity all free?
247fn sp_feas req ulong to bool
248for k ulong 4
249if gt sp_need req k get scap k
250ret false
251end
252end
253ret true
254end
255
256note Job j takes its whole need.
257fn sp_take j ulong to void
258for k ulong 4
259put suse k add get suse k sp_need get sjneed j k
260end
261put sjheld j 1
262end
263
264note Job j gives back its need, if it holds it.
265fn sp_give j ulong to void
266if eq get sjheld j 0
267ret
268end
269for k ulong 4
270put suse k sub get suse k sp_need get sjneed j k
271end
272put sjheld j 0
273end
274
275note Job j becomes ready, stamped with the next ready order.
276fn sp_ready j ulong to void
277put sjst j 2
278put sjrdy j get scst 1
279put scst 1 add get scst 1 1
280sp_bump
281end
282
283note Creator: counter i of scst, capacity (i 16..19) or use (i 20..23) of a
284note kind, policy word (i 24..31), goal counter (i 32..39), graph version
285note (i 40), proposal state (i 41: 0 no incarnation, 1 accepting, 2
286note exhausted).
287fn sched_stat i ulong to ulong
288if lt i 16
289ret get scst i
290end
291if lt i 20
292ret get scap sub i 16
293end
294if lt i 24
295ret get suse sub i 20
296end
297if lt i 32
298ret get scpol sub i 24
299end
300if lt i 40
301ret get scgst sub i 32
302end
303if eq i 40
304ret get scgv 0
305end
306if eq i 41
307ret get scgv 4
308end
309ret 0
310end
311
312note Creator: would sched_cap caps be accepted? Not when the new capacity of
313note a kind is below its use by running jobs, or below the need of any one
314note admitted job.
315fn sched_capok caps ulong to bool
316for k ulong 4
317if lt sp_need caps k get suse k
318ret false
319end
320end
321for j ulong 64
322s = get sjst j
323if and ge s 1 le s 3
324for k ulong 4
325if gt sp_need get sjneed j k sp_need caps k
326ret false
327end
328end
329end
330end
331ret true
332end
333
334note Creator: set the capacity of every kind from caps (packed like a
335note request). 0, or -8 when sched_capok refuses (nothing changes).
336fn sched_cap caps ulong to long
337if not sched_capok caps
338ret neg long 8
339end
340for k ulong 4
341put scap k sp_need caps k
342end
343ret 0
344end
345
346note Creator: the configured capacities, packed like a request.
347fn sched_caps to ulong
348v = ulong 0
349for k ulong 4
350v = or v shl get scap k mul k 16
351end
352ret v
353end
354
355note Creator: would sched_policy accept these words? Policy p 0..2, window
356note win 1..64, dispatch budget db 1..64, bypass budget by 1..65535, ticket
357note lifetime ttl 1..1000000.
358fn sched_polok p ulong win ulong db ulong by ulong ttl ulong to bool
359if gt p 2
360ret false
361end
362if or eq win 0 gt win 64
363ret false
364end
365if or eq db 0 gt db 64
366ret false
367end
368if or eq by 0 gt by 65535
369ret false
370end
371ret and gt ttl 0 le ttl 1000000
372end
373
374note Creator: policy p (0 FIFO head-of-line, 1 fair fitting, 2 goal-aware
375note fair fitting), candidate window win slots, dispatch budget db jobs per
376note event, bypass budget by, ticket lifetime ttl dispatch events, in the
377note ranges of sched_polok. The cursor and any reservation are kept. 0 or
378note -7.
379fn sched_policy p ulong win ulong db ulong by ulong ttl ulong to long
380if not sched_polok p win db by ttl
381ret neg long 7
382end
383put scpol 0 p
384put scpol 1 win
385put scpol 2 db
386put scpol 3 by
387put scpol 7 ttl
388ret 0
389end
390
391note Creator: drop this process's scheduling mappings.
392fn sched_free to long
393r = mapdrop get scmap 1
394if lt r 0
395ret r
396end
397ret mapdrop get scmap 0
398end
399
400note Jobs whose state is in lo..hi.
401fn sched_count lo ulong hi ulong to ulong
402n = ulong 0
403for j ulong 64
404s = get sjst j
405if and ge s lo le s hi
406n = add n 1
407end
408end
409ret n
410end
411
412note Position of id's admission ticket, or 64.
413fn sp_tkpos id ulong to ulong
414for t ulong get scpol 6 bound 64
415if eq get sttk t id
416ret t
417end
418end
419ret 64
420end
421
422note Compact the tickets in order, dropping ticket d (64: none) and every
423note ticket not asked about for the ticket lifetime.
424fn sp_tkpack d ulong to void
425k = ulong 0
426for t ulong get scpol 6 bound 64
427if ne t d
428if lt add get stts t get scpol 7 get scst 2
429put scst 11 add get scst 11 1
430else
431put sttk k get sttk t
432put stts k get stts t
433k = add k 1
434end
435end
436end
437put scpol 6 k
438end
439
440note Refresh ticket t, or give id a ticket at the end when it has none and
441note the line has room (a caller refused with the line full holds none).
442fn sp_tkhold id ulong t ulong to void
443if lt t 64
444put stts t get scst 2
445ret
446end
447n = get scpol 6
448if lt n 64
449put sttk n id
450put stts n get scst 2
451put scpol 6 add n 1
452put scst 10 add get scst 10 1
453end
454end
455
456note Creator: may id be admitted now? Room is the smaller of the admission
457note headroom and the free table slots (a job in review or UNKNOWN holds
458note its slot). While tickets are held only the first holder may enter;
459note with none, anyone may while there is room. A refused caller gets or
460note keeps a ticket; every ask by a holder, refused or not, refreshes its
461note ticket's last-asked event, and a caller with room and no ticket gets
462note none. A true answer guarantees the slot for the next sched_admitr
463note (single creator), so a caller may ask before journaling (as jn_admit
464note does).
465fn sched_room id ulong to bool
466sp_tkpack 64
467t = sp_tkpos id
468a = ulong 0
469fs = ulong 0
470for j ulong 64
471s = get sjst j
472if eq s 0
473fs = add fs 1
474end
475if and ge s 1 le s 3
476a = add a 1
477end
478end
479room = ulong 0
480if lt a get sclim 0
481room = sub get sclim 0 a
482end
483if lt fs room
484room = fs
485end
486if gt room 0
487if eq get scpol 6 0
488ret true
489end
490if eq t 0
491put stts 0 get scst 2
492ret true
493end
494end
495sp_tkhold id t
496ret false
497end
498
499note Creator: admit job id needing req (packed resources). deps 0 makes it
500note ready at once; otherwise it waits for a publication whose topics meet
501note deps. flags bit 0: safe to rerun after its worker dies. Returns the job
502note slot; -8 when req exceeds the configured capacity of a kind (it could
503note never run); -2 when sched_room refuses: at the admission limit, or
504note ticket holders ahead; the caller keeps a ticket and asks again.
505fn sched_admitr id ulong deps ulong flags ulong req ulong to long
506ret sched_admits id deps flags req sched_slot
507end
508
509note Creator: the lowest free table slot (the one sched_admitr would give
510note the next job), or 64 when none is free.
511fn sched_slot to ulong
512for j ulong 64
513if eq get sjst j 0
514ret j
515end
516end
517ret 64
518end
519
520note Creator: could req ever run, with the configured capacity all free?
521fn sched_feas req ulong to bool
522ret sp_feas req
523end
524
525note Creator: sched_admitr into table slot j, which must be free: the
526note journal records the slot with the admission and recovery puts the job
527note back into it (DRA-398). Refusals as sched_admitr; -2 also when slot j
528note is not free.
529fn sched_admits id ulong deps ulong flags ulong req ulong j ulong to long
530if not sp_feas req
531put scst 7 add get scst 7 1
532ret neg long 8
533end
534if not sched_room id
535ret neg long 2
536end
537if ge j 64
538ret neg long 2
539end
540if ne get sjst j 0
541ret neg long 2
542end
543t = sp_tkpos id
544if lt t 64
545sp_tkpack t
546end
547put sjid j id
548put sjdep j deps
549put sjfl j flags
550put sjatt j 0
551put sjout j 0
552put sjw j 0
553put sjneed j req
554put sjheld j 0
555put sjbyp j 0
556put sjlse j 0
557put sjdue j 0
558put sjarr j get scst 0
559put scst 0 add get scst 0 1
560put sjpro j 0
561put sjest j 0
562put sjunc j 0
563put sjval j 0
564if eq deps 0
565sp_ready j
566else
567put sjst j 1
568sp_bump
569end
570ret long j
571end
572
573note Creator: job j (waiting, ready or running) gets a lease of ns from its
574note next assignment, or from now when it runs; 0 removes it. 0; -3 for a
575note slot that is not waiting, ready or running; -7 for ns above 2^50 (about
576note 13 days). Both refusals change nothing. The bound keeps every deadline
577note (the monotonic clock, below 2^63, plus ns) representable: it never
578note wraps into the past or onto 0, the no-deadline value.
579fn sched_lease j ulong ns ulong to long
580if ge j 64
581ret neg long 3
582end
583s = get sjst j
584if or lt s 1 gt s 3
585ret neg long 3
586end
587if gt ns 1125899906842624
588ret neg long 7
589end
590put sjlse j ns
591if eq s 3
592put sjdue j 0
593if gt ns 0
594put sjdue j add ulong monons ns
595end
596end
597ret 0
598end
599
600note Creator: a running job past its lease whose worker has not reported
601note done, reported once (its deadline is cleared): its slot, or 64. The
602note authority cancels or kills it; the release follows from the worker's
603note report or reap.
604fn sched_overdue to ulong
605now = ulong monons
606for j ulong 64
607if eq get sjst j 3
608d = get sjdue j
609if gt d 0
610if ge now d
611w = get sjw j
612if ne sc_cell w 64 get scseq w
613put sjdue j 0
614put scst 12 add get scst 12 1
615ret j
616end
617end
618end
619end
620end
621ret 64
622end
623
624note Creator: sched_admitr with no resource need beyond one worker.
625fn sched_admit id ulong deps ulong flags ulong to long
626ret sched_admitr id deps flags 0
627end
628
629note Creator: worker w is woken by publications whose topics meet topics.
630fn sched_want w ulong topics ulong to long
631if ge w 64
632ret neg long 7
633end
634sc_set w 32 topics
635ret 0
636end
637
638fn sc_wake w ulong to void
639sc_set w 40 ulong monons
640r = sup_wake w
641end
642
643note Lowest idle live worker below n, or 64 when none. A worker is idle when
644note the creator has collected its last assignment.
645fn sp_idle n ulong to ulong
646for w ulong n bound 64
647if and gt get swpid w 0 eq get scbusy w 0
648ret w
649end
650end
651ret 64
652end
653
654note Assign job j to idle worker w: it takes its need; the job id and the
655note input epoch and version are written before the seq, then the wake.
656fn sp_start c long j ulong w ulong to void
657q = add get scseq w 1
658put scseq w q
659put scbusy w add j 1
660put sjst j 3
661put sjw j w
662put sjatt j add get sjatt j 1
663put sjbyp j 0
664put sjdue j 0
665if gt get sjlse j 0
666put sjdue j add ulong monons get sjlse j
667end
668sp_take j
669if eq get scpol 5 add j 1
670put scpol 5 0
671put scst 9 add get scst 9 1
672end
673sc_set w 8 get sjid j
674sc_set w 16 pool_epoch c
675sc_set w 24 node_count pool_current c
676sc_set w 0 q
677sc_wake w
678put scst 4 add get scst 4 1
679end
680
681note Policy 0, FIFO head-of-line: the stop rule of the reference pump
682note (aien-sovereign-core-mv spark-harness scheduler/core.rs Core::pump,
683note 598-602), not its queue: the head here is the ready job that became
684note ready first (the pump batches newly eligible tasks in task-id order
685note into a bounded queue). The head is chosen when it fits, and when it
686note does not fit nothing is chosen, so a head that does
687note not fit holds back every later job. Returns the slot or 64.
688fn sp_fifo to ulong
689h = ulong 64
690for j ulong 64
691put scst 3 add get scst 3 1
692if eq get sjst j 2
693if eq h 64
694h = j
695else
696if lt get sjrdy j get sjrdy h
697h = j
698end
699end
700end
701end
702if eq h 64
703ret 64
704end
705if sp_fits get sjneed h 0
706ret h
707end
708put scst 8 add get scst 8 1
709ret 64
710end
711
712note Policy 1, fair fitting: the reserved job first when it fits; else the
713note oldest admitted ready job, among win slots from the cursor, that fits
714note without the capacity the reserved job still lacks; the cursor then
715note moves on by win. Returns the slot or 64. Every slot read counts
716note (counter 3: the reserved probe and the window), at most win + 1 per
717note pick, win + 2 under policy 2 (sp_proposal's probe counts too);
718note counter 14 counts window scans.
719fn sp_fair to ulong
720rq = ulong 0
721r = get scpol 5
722if gt r 0
723rj = sub r 1
724put scst 3 add get scst 3 1
725if eq get sjst rj 2
726if sp_fits get sjneed rj 0
727ret rj
728end
729rq = get sjneed rj
730else
731put scpol 5 0
732end
733end
734goal = eq get scpol 0 2
735if goal
736sp_rescore
737pj = sp_proposal rq
738if lt pj 64
739ret pj
740end
741end
742win = get scpol 1
743cur = get scpol 4
744b = ulong 64
745put scst 14 add get scst 14 1
746for i ulong win bound 64
747put scst 3 add get scst 3 1
748j = rem add cur i 64
749if eq get sjst j 2
750if sp_fits get sjneed j rq
751if eq b 64
752b = j
753else
754if sp_better goal j b
755b = j
756end
757end
758end
759end
760end
761put scpol 4 rem add cur win 64
762ret b
763end
764
765note Ranking key, both of ready jobs a and b: is a better? Policy 1: older
766note admission. Policy 2: larger unblock value, then smaller declared
767note estimate, then older admission (admission orders are unique, so there
768note is never a tie).
769fn sp_better goal bool a ulong b ulong to bool
770if goal
771va = get sjval a
772vb = get sjval b
773if ne va vb
774ret gt va vb
775end
776ea = get sjest a
777eb = get sjest b
778if ne ea eb
779ret lt ea eb
780end
781end
782ret lt get sjarr a get sjarr b
783end
784
785note A committed change that can alter an unblock value: a job becomes
786note ready, a waiting job is admitted, a waiting or ready job is cancelled,
787note or a declaration changes. Bumps the graph version, which makes any
788note proposal stale (rejected when next used). Nothing is rescored here:
789note the next policy-2 pick rescores once, however many bumps came before
790note it (coalesced). At 2^40 - 1 bumps the version stays and is exhausted
791note (scgv 4 = 2): from then on proposals are refused and every pick
792note rescores.
793fn sp_bump to void
794put scgst 2 add get scgst 2 1
795if eq and get scgv 0 1099511627775 1099511627775
796put scgv 4 2
797ret
798end
799put scgv 0 add get scgv 0 1
800end
801
802note Unblock value of ready job j, in jobs: the waiting jobs whose deps meet
803note the topics j declares it unblocks (depth 1), plus the waiting jobs whose
804note deps meet the topics those declare (depth 2), each counted once. Reads
805note at most 128 slots.
806fn sp_value j ulong to ulong
807pro = get sjpro j
808if eq pro 0
809ret 0
810end
811n = ulong 0
812m = ulong 0
813p2 = ulong 0
814for k ulong 64
815if eq get sjst k 1
816if ne and get sjdep k pro 0
817m = or m shl ulong 1 k
818p2 = or p2 get sjpro k
819n = add n 1
820end
821end
822end
823put scgst 6 add get scgst 6 64
824if eq p2 0
825ret n
826end
827for k ulong 64
828if eq get sjst k 1
829if eq and shr m k 1 0
830if ne and get sjdep k p2 0
831n = add n 1
832end
833end
834end
835end
836put scgst 6 add get scgst 6 64
837ret n
838end
839
840note Recompute the unblock value of every ready job when the graph version
841note moved since the last scoring, or always once it is exhausted;
842note otherwise nothing.
843fn sp_rescore to void
844v = get scgv 0
845if and eq get scgv 1 v ne get scgv 4 2
846ret
847end
848for j ulong 64
849if eq get sjst j 2
850put sjval j sp_value j
851put scgst 1 add get scgst 1 1
852end
853end
854put scgv 1 v
855put scgst 0 add get scgst 0 1
856put scgst 6 add get scgst 6 64
857end
858
859note The proposed job, when the proposal is for the current graph version
860note (and that version is not exhausted) and the job is still ready and fits
861note beside the reservation (rq); it is used once. Otherwise the proposal
862note is dropped as stale: 64.
863fn sp_proposal rq ulong to ulong
864p = get scgv 2
865if eq p 0
866ret 64
867end
868put scgv 2 0
869put scst 3 add get scst 3 1
870j = sub p 1
871if and eq get scgv 3 get scgv 0 eq get scgv 4 1
872if eq get sjst j 2
873if sp_fits get sjneed j rq
874put scgst 7 add get scgst 7 1
875ret j
876end
877end
878end
879put scgst 4 add get scgst 4 1
880ret 64
881end
882
883note Creator: declare what job j (waiting or ready) is expected to unblock
884note and cost: pro, the topics its completion will publish; est, the work
885note estimate in ms (declared, or derived by the caller from measurements);
886note unc, its uncertainty in ms (recorded, not used to rank). 0, or -3.
887fn sched_goal j ulong pro ulong est ulong unc ulong to long
888if ge j 64
889ret neg long 3
890end
891s = get sjst j
892if or eq s 0 gt s 2
893ret neg long 3
894end
895put sjpro j pro
896put sjest j est
897put sjunc j unc
898sp_bump
899ret 0
900end
901
902note Creator: the graph version that proposals must name.
903fn sched_version to ulong
904ret get scgv 0
905end
906
907note Creator, after sched_new: incarnation n (1..2^24 - 1), distinct
908note for every scheduler that may receive proposals meant for another (the
909note journal fence, DRA-398). The graph version becomes n * 2^40, which no
910note scheduler of another incarnation reaches, any proposal is dropped and
911note proposals are accepted from now on. The last scoring stays below n *
912note 2^40, so the next ranking pass rescores whatever was admitted or
913note declared before, whenever the incarnation comes. Once per
914note scheduler: 0, or -7 (n out of range, or already set or exhausted).
915fn sched_incarnate n ulong to long
916if or eq n 0 ge n 16777216
917ret neg long 7
918end
919if ne get scgv 4 0
920ret neg long 7
921end
922put scgv 0 shl n 40
923put scgv 2 0
924put scgv 4 1
925ret 0
926end
927
928note Creator, policy 2: propose the ready job whose id is id for the next
929note pick, computed at graph version v (an outside planner, or an
930note operator). The proposal names the job, not its slot. Reservations and
931note feasibility still come first: it is checked again when used. 0; -6 no
932note incarnation set or the version exhausted; -9 stale (v is not the
933note current version); -3 no ready job, or more than one, has that id; -8
934note it does not fit beside the reservation; -7 not policy 2.
935fn sched_propose id ulong v ulong to long
936if ne get scpol 0 2
937ret neg long 7
938end
939if ne get scgv 4 1
940put scgst 5 add get scgst 5 1
941ret neg long 6
942end
943if ne v get scgv 0
944put scgst 4 add get scgst 4 1
945ret neg long 9
946end
947j = ulong 64
948n = ulong 0
949for k ulong 64
950if and eq get sjst k 2 eq get sjid k id
951j = k
952n = add n 1
953end
954end
955if ne n 1
956put scgst 5 add get scgst 5 1
957ret neg long 3
958end
959rq = ulong 0
960r = get scpol 5
961if gt r 0
962rq = get sjneed sub r 1
963end
964if not sp_fits get sjneed j rq
965put scgst 5 add get scgst 5 1
966ret neg long 8
967end
968put scgv 2 add j 1
969put scgv 3 v
970put scgst 3 add get scgst 3 1
971ret 0
972end
973
974note After a fair dispatch event in which ya - 1 was the youngest admission
975note assigned (ya 0: none): every ready job admitted before it was bypassed
976note once; with no reservation, the oldest job at the bypass budget is
977note reserved. One pass over the table (64 maintenance reads, counter 13).
978note Returns the bitmap of the slots bypassed.
979fn sp_after ya ulong to ulong
980o = ulong 64
981bm = ulong 0
982nores = eq get scpol 5 0
983for j ulong 64
984if eq get sjst j 2
985if gt ya 0
986if lt add get sjarr j 1 ya
987put sjbyp j add get sjbyp j 1
988put scst 5 add get scst 5 1
989bm = or bm shl 1 j
990end
991end
992if nores
993if ge get sjbyp j get scpol 3
994if eq o 64
995o = j
996else
997if lt get sjarr j get sjarr o
998o = j
999end
1000end
1001end
1002end
1003end
1004end
1005put scst 13 add get scst 13 64
1006if lt o 64
1007put scpol 5 add o 1
1008put scst 6 add get scst 6 1
1009end
1010ret bm
1011end
1012
1013note Creator: assign ready jobs to idle live workers under the policy, at
1014note most the dispatch budget per call. Returns the number assigned. With
1015note a journal attached and the replay ring full, nothing is assigned
1016note (counter 15) until the journal drains it.
1017fn sched_dispatch c long to ulong
1018if ne get scpause 0 0
1019ret 0
1020end
1021if and ne get sfj 0 0 ge get sfj 1 64
1022put scst 15 add get scst 15 1
1023ret 0
1024end
1025n = mapget c 48 8
1026k = ulong 0
1027ya = ulong 0
1028put sfj 2 get scpol 5
1029put sfj 3 0
1030fair = ge get scpol 0 1
1031put scst 2 add get scst 2 1
1032for i ulong get scpol 2 bound 64
1033w = sp_idle n
1034if ge w 64
1035break
1036end
1037j = ulong 64
1038if fair
1039j = sp_fair
1040else
1041j = sp_fifo
1042end
1043if ge j 64
1044break
1045end
1046if gt add get sjarr j 1 ya
1047ya = add get sjarr j 1
1048end
1049sp_fmark j
1050sp_start c j w
1051k = add k 1
1052end
1053bm = ulong 0
1054if fair
1055bm = sp_after ya
1056end
1057sp_frend bm
1058ret k
1059end
1060
1061note End of a dispatch event with a journal attached: one ring record when
1062note it assigned a job or moved the reservation.
1063fn sp_frend bm ulong to void
1064if eq get sfj 0 0
1065ret
1066end
1067if or ne get sfj 3 0 ne get scpol 5 get sfj 2
1068sp_frpush get sfj 3 bm 0
1069end
1070end
1071
1072note The current dispatch event assigned slot j (sfj 3).
1073fn sp_fmark j ulong to void
1074put sfj 3 or get sfj 3 shl 1 j
1075end
1076
1077note Append one replay record (sfr): event, assigned and bypassed slot
1078note bitmaps, cursor, reserved slot + 1, slots made ready. The callers
1079note leave room.
1080fn sp_frpush am ulong bm ulong rd ulong to void
1081b = mul get sfj 1 6
1082put sfr b get scst 2
1083put sfr add b 1 am
1084put sfr add b 2 bm
1085put sfr add b 3 get scpol 4
1086put sfr add b 4 get scpol 5
1087put sfr add b 5 rd
1088put sfj 1 add get sfj 1 1
1089end
1090
1091note Journal: attach (on) or detach the replay ring. Attaching empties it.
1092note jn_recover attaches once the scheduler holds the journaled state.
1093fn sched_jattach on bool to void
1094put sfj 0 0
1095if on
1096put sfj 0 1
1097end
1098put sfj 1 0
1099end
1100
1101fn sched_jattached to bool
1102ret ne get sfj 0 0
1103end
1104
1105note Journal: records waiting in the replay ring, word i (0..5) of record k,
1106note and emptying it once they are durable.
1107fn sched_frn to ulong
1108ret get sfj 1
1109end
1110fn sched_frw k ulong i ulong to ulong
1111ret get sfr add mul k 6 i
1112end
1113fn sched_frclear to void
1114put sfj 1 0
1115end
1116
1117note Journal, recovery: the cursor and reservation after replay, and the
1118note dispatch event count. The reservation names slot res - 1, which must
1119note hold a ready job. -7 (nothing changed) for a cursor outside the table
1120note or any other reservation, else 0.
1121fn sched_frestore cur ulong ev ulong res ulong to long
1122if or ge cur 64 gt res 64
1123ret neg long 7
1124end
1125if gt res 0
1126if ne get sjst sub res 1 2
1127ret neg long 7
1128end
1129end
1130put scpol 4 cur
1131put scst 2 ev
1132put scpol 5 res
1133ret 0
1134end
1135
1136note Journal, recovery: the bypass count of ready job j. 0 or -3.
1137fn sched_setbyp j ulong n ulong to long
1138if ge j 64
1139ret neg long 3
1140end
1141if ne get sjst j 2
1142ret neg long 3
1143end
1144put sjbyp j n
1145ret 0
1146end
1147
1148note Journal, recovery: waiting job j was made ready by a publication
1149note before the restart. 0 or -3 (j not waiting).
1150fn sched_setready j ulong to long
1151if ge j 64
1152ret neg long 3
1153end
1154if ne get sjst j 1
1155ret neg long 3
1156end
1157sp_ready j
1158ret 0
1159end
1160
1161note Creator: move finished jobs into review while review has room. A worker
1162note whose result is waiting for room keeps it and gets no new job, so a
1163note full review queue stops assignment instead of dropping work. A job
1164note gives back its resources as soon as its worker reported done, even
1165note while its result waits for room.
1166fn sched_collect c long to ulong
1167n = mapget c 48 8
1168k = ulong 0
1169for w ulong n bound 64
1170b = get scbusy w
1171if gt b 0
1172if eq sc_cell w 64 get scseq w
1173j = sub b 1
1174sp_give j
1175if ge sched_count 4 4 get sclim 1
1176continue
1177end
1178put sjst j 4
1179put sjout j sc_cell w 72
1180put scbusy w 0
1181k = add k 1
1182end
1183end
1184end
1185ret k
1186end
1187
1188note Creator, after worker w was reaped: retire its unfinished assignment.
1189note A rerunnable job is ready again; any other becomes UNKNOWN (6), since
1190note it may have had effects. A finished result is left for sched_collect.
1191note Returns 1 when an assignment was retired, 0 when none, -16 still live.
1192fn sched_lost c long w ulong to long
1193if ge w 64
1194ret neg long 7
1195end
1196if gt get swpid w 0
1197ret neg long 16
1198end
1199b = get scbusy w
1200if eq b 0
1201ret 0
1202end
1203if eq sc_cell w 64 get scseq w
1204ret 0
1205end
1206j = sub b 1
1207sc_set w 64 get scseq w
1208put scbusy w 0
1209sp_give j
1210if ne and get sjfl j 1 0
1211sp_ready j
1212else
1213put sjst j 6
1214end
1215ret 1
1216end
1217
1218note Creator loop step: wait up to interval ns for an ack or a death, take
1219note acks, retire the jobs of reaped workers, collect, then dispatch.
1220note Returns the number of workers reaped, or a negative errno.
1221fn sched_step c long interval long to long
1222d = sup_poll c interval
1223if lt d 0
1224ret d
1225end
1226n = mapget c 48 8
1227for w ulong n bound 64
1228if gt get swpid w 0
1229r = sup_take get swack w
1230else
1231r = sched_lost c w
1232end
1233end
1234k = sched_collect c
1235k = sched_dispatch c
1236ret d
1237end
1238
1239note Creator: publish an artifact, make waiting jobs whose deps meet topics
1240note ready, wake each live worker whose interest meets topics, dispatch.
1241note With a journal attached the jobs made ready leave a readiness record
1242note in the replay ring; when the ring is full nothing is published, -105
1243note (counter 15). Returns the version, or the publish refusal with nothing
1244note woken.
1245fn sched_publish c long topics ulong src view uchar 4096 n ulong to long
1246if and ne get sfj 0 0 ge get sfj 1 64
1247put scst 15 add get scst 15 1
1248ret neg long 105
1249end
1250v = pool_publish c topics src n
1251if le v 0
1252ret v
1253end
1254rd = ulong 0
1255for j ulong 64
1256if eq get sjst j 1
1257if ne and get sjdep j topics 0
1258sp_ready j
1259rd = or rd shl 1 j
1260end
1261end
1262end
1263if and ne rd 0 ne get sfj 0 0
1264sp_frpush 0 0 rd
1265end
1266m = mapget c 48 8
1267for w ulong m bound 64
1268if gt get swpid w 0
1269if ne and sc_cell w 32 topics 0
1270sc_wake w
1271end
1272end
1273end
1274k = sched_dispatch c
1275ret v
1276end
1277
1278note Creator: accept the result of job j in review and free its slot.
1279note Returns its outcome, or -3 when j is not in review.
1280fn sched_review j ulong to long
1281if ge j 64
1282ret neg long 3
1283end
1284if ne get sjst j 4
1285ret neg long 3
1286end
1287put sjst j 0
1288ret long get sjout j
1289end
1290
1291note Creator: free an UNKNOWN job once its outcome has been settled
1292note elsewhere. Returns 0, or -3 when j is not UNKNOWN.
1293fn sched_settle j ulong to long
1294if ge j 64
1295ret neg long 3
1296end
1297if ne get sjst j 6
1298ret neg long 3
1299end
1300put sjst j 0
1301ret 0
1302end
1303
1304note Creator: while paused (on true) no job is assigned; running jobs go on
1305note and finished results are still collected. Resuming dispatches at once.
1306note Returns the number assigned on resume, else 0.
1307fn sched_pause c long on bool to ulong
1308if on
1309put scpause 0 1
1310ret 0
1311end
1312put scpause 0 0
1313ret sched_dispatch c
1314end
1315
1316note Creator: cancel job j. A waiting or ready job never started, so it is
1317note freed with no effect: 1. A running job needs its worker: 2, and the
1318note outcome comes from the worker. A job in review already finished: 3,
1319note nothing is cancelled. UNKNOWN stays UNKNOWN: 4. -3 free or bad slot.
1320fn sched_cancel j ulong to long
1321if ge j 64
1322ret neg long 3
1323end
1324s = get sjst j
1325if or eq s 1 eq s 2
1326put sjst j 0
1327sp_bump
1328if eq get scpol 5 add j 1
1329put scpol 5 0
1330end
1331ret 1
1332end
1333if eq s 3
1334ret 2
1335end
1336if eq s 4
1337ret 3
1338end
1339if eq s 6
1340ret 4
1341end
1342ret neg long 3
1343end
1344
1345note Creator: stop every worker within interval ns (sup_stop), retire the
1346note assignments of the reaped workers, collect finished results.
1347note Returns how many workers had to be killed, or a refusal.
1348fn sched_stop c long interval long to long
1349k = sup_stop c interval
1350if lt k 0
1351ret k
1352end
1353n = mapget c 48 8
1354for w ulong n bound 64
1355r = sched_lost c w
1356end
1357m = sched_collect c
1358ret k
1359end
1360
1361note Worker w: the assignment seq when a job is waiting, else 0. Job id,
1362note input epoch and input version are words 8, 16 and 24 of its cell.
1363fn sched_wjob w ulong to ulong
1364q = sc_cell w 0
1365if gt q sc_cell w 64
1366ret q
1367end
1368ret 0
1369end
1370
1371note Worker w: finish assignment q with outcome and the first n (at most
1372note 4096) bytes of src. Bytes and outcome are written before the done seq;
1373note then the creator gets an ack. 0, -7 bad n, or the ack refusal.
1374fn sched_wdone w ulong q ulong outcome ulong src view uchar 4096 n ulong to long
1375if gt n 4096
1376ret neg long 7
1377end
1378if gt n 0
1379r = mapwrite get scmap 1 mul w 4096 src 0 n
1380end
1381sc_set w 72 outcome
1382sc_set w 64 q
1383ret sup_ack w
1384end
1385
1386note Creator: copy worker w's last result bytes into dst.
1387fn sched_result w ulong dst edit uchar 4096 to long
1388ret mapread get scmap 1 mul w 4096 dst 0 4096
1389end
1390
1391note Worker w: sup_wwait that records its wakes and wake latency.
1392fn sched_wwait c long w ulong interval long to long
1393r = sup_wwait c w interval
1394if eq r 1
1395now = ulong monons
1396st = sc_cell w 40
1397lat = ulong 0
1398if gt now st
1399lat = sub now st
1400end
1401sc_set w 80 add sc_cell w 80 1
1402sc_set w 88 lat
1403if gt lat sc_cell w 96
1404sc_set w 96 lat
1405end
1406end
1407ret r
1408end
1409
1410note Worker w: count the versions of the current epoch that meet its
1411note interest and are newer than its cursor, moving the cursor past them.
1412note A new epoch restarts the cursor at 0. A worker with no interest scans
1413note nothing. Returns the count or a refusal.
1414fn sched_wscan c long w ulong to long
1415t = sc_cell w 32
1416if eq t 0
1417ret 0
1418end
1419h = pool_acquire c w
1420if le h 0
1421ret h
1422end
1423e = mapget c add 256 mul w 8 8
1424if ne get swcur 0 e
1425put swcur 0 e
1426put swcur 1 0
1427end
1428k = long 0
1429for i ulong 65
1430v = node_next h get swcur 1 t
1431if lt v 0
1432r = pool_release c w
1433ret v
1434end
1435if eq v 0
1436break
1437end
1438put swcur 1 ulong v
1439k = add k 1
1440end
1441r = pool_release c w
1442ret k
1443end