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