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/bench-gate.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 / bench-gate.om` 2964 lines ยท 67702 bytes Raw Blame History 1 note Omega Node transport, concatenate with a program-mode consumer. 2 note Trusted forked processes only. This is transport, not write authority. 3 note One publisher PID, 64 immutable records per epoch; reuse only via node_reset. 4 note Layout: 4096 byte header then 64 slots of 4160 bytes. 5 note Header u64: magic, ABI, published count, stop, publisher PID, epoch. 6 note Slot u64: version, length, topic mask; payload begins at offset 64. 7 const NCAP ulong 64 8 const NBYTES ulong 270336 9 const NMAGIC ulong `1313817669` 10 11 fn node_new to long 12 h = mapnew neg int 1 0 NBYTES 3 33 13 if le h 0 14 ret h 15 end 16 r = mapset h 8 8 1 17 r = mapset h 32 8 ulong getpid 18 r = mapset h 0 8 NMAGIC 19 ret h 20 end 21 22 fn node_valid h long to bool 23 ret and eq mapget h 0 8 NMAGIC eq mapget h 8 8 1 24 end 25 26 fn node_offset v ulong to ulong 27 ret add 4096 mul sub v 1 4160 28 end 29 30 note Return version, or -1 stopped, -2 full, -3 length, -4 topic, 31 note -5 invalid header, -6 wrong publisher. Refusals do not publish. 32 fn node_publish h long topics ulong src view uchar 4096 n ulong to long 33 if not node_valid h 34 ret neg long 5 35 end 36 if ne mapget h 32 8 ulong getpid 37 ret neg long 6 38 end 39 if ne mapget h 24 8 0 40 ret neg long 1 41 end 42 if gt n 4096 43 ret neg long 3 44 end 45 if eq topics 0 46 ret neg long 4 47 end 48 v = mapget h 16 8 49 if gt v NCAP 50 ret neg long 5 51 end 52 if eq v NCAP 53 ret neg long 2 54 end 55 v = add v 1 56 off = node_offset v 57 r = mapwrite h add off 64 src 0 n 58 r = mapset h add off 8 8 n 59 r = mapset h add off 16 8 topics 60 note mapset has barriers before and after its aligned store. 61 r = mapset h off 8 v 62 r = mapset h 16 8 v 63 ret long v 64 end 65 66 note Header and record checks precede copying. No partial publication is read. 67 note Returns payload length, -5 invalid header/record, -7 unavailable version. 68 fn node_read h long v ulong dst edit uchar 4096 to long 69 if not node_valid h 70 ret neg long 5 71 end 72 if or eq v 0 gt v NCAP 73 ret neg long 7 74 end 75 latest = mapget h 16 8 76 if gt latest NCAP 77 ret neg long 5 78 end 79 if gt v latest 80 ret neg long 7 81 end 82 off = node_offset v 83 if ne mapget h off 8 v 84 ret neg long 5 85 end 86 n = mapget h add off 8 8 87 if gt n 4096 88 ret neg long 5 89 end 90 ret mapread h add off 64 dst 0 n 91 end 92 93 note Return the next matching version after cursor, zero if caught up, 94 note or negative refusal. Caller advances its cursor through returned versions. 95 fn node_next h long cursor ulong topics ulong to long 96 if not node_valid h 97 ret neg long 5 98 end 99 if or gt cursor NCAP eq topics 0 100 ret neg long 7 101 end 102 latest = mapget h 16 8 103 if or gt latest NCAP gt cursor latest 104 ret neg long 5 105 end 106 v = cursor 107 note At most NCAP published slots can be examined. 108 while lt v latest bound 64 109 v = add v 1 110 off = node_offset v 111 if ne mapget h off 8 v 112 ret neg long 5 113 end 114 if ne and mapget h add off 16 8 topics 0 115 ret long v 116 end 117 end 118 ret 0 119 end 120 121 fn node_stop h long to long 122 if not node_valid h 123 ret neg long 5 124 end 125 if ne mapget h 32 8 ulong getpid 126 ret neg long 6 127 end 128 ret mapset h 24 8 1 129 end 130 131 note Creator only. Retire every record so a pool can reuse the arena for a new 132 note epoch. The caller must first prove no reader holds the old epoch (pool.om). 133 note The count drops to zero before any record version is cleared. 134 fn node_reset h long epoch ulong to long 135 if not node_valid h 136 ret neg long 5 137 end 138 if ne mapget h 32 8 ulong getpid 139 ret neg long 6 140 end 141 if eq epoch 0 142 ret neg long 7 143 end 144 r = mapset h 16 8 0 145 for i ulong NCAP 146 r = mapset h node_offset add i 1 8 0 147 end 148 r = mapset h 24 8 0 149 r = mapset h 40 8 epoch 150 ret 0 151 end 152 153 fn node_epoch h long to ulong 154 ret mapget h 40 8 155 end 156 157 note The latest published version of the arena's current epoch (0 when empty). 158 fn node_count h long to ulong 159 ret mapget h 16 8 160 end 161 162 note One nonblocking eventfd per consumer. Saturation means already readable. 163 note Interrupted operations retry at most 64 times, then return EINTR. 164 fn node_notify fd int to long 165 buf = new uchar 8 0 166 put buf 0 1 167 for i ulong 64 168 r = write fd buf 0 8 169 if eq r 8 170 ret 0 171 end 172 if eq r neg long 11 173 ret 0 174 end 175 if ne r neg long 4 176 if lt r 0 177 ret r 178 end 179 ret neg long 5 180 end 181 end 182 ret neg long 4 183 end 184 185 fn node_drain fd int to long 186 buf = new uchar 8 0 187 for i ulong 64 188 r = read fd buf 0 8 189 if or eq r 8 eq r neg long 11 190 ret 0 191 end 192 if ne r neg long 4 193 if lt r 0 194 ret r 195 end 196 ret neg long 5 197 end 198 end 199 ret neg long 4 200 end 201 202 fn node_pollfd p edit uchar 16 off ulong fd int to void 203 for i ulong 4 204 put p add off i uchar and shr uint fd uint mul i 8 255 205 end 206 put p add off 4 1 207 end 208 209 note Returns 2 stop, 1 wake, 0 timeout or negative errno. 210 note Control takes priority when both descriptors are ready. 211 note EINTR returns to caller so it can recompute its monotonic deadline. 212 fn node_wait h long wake int stop int interval long to long 213 p = new uchar 16 0 214 if not node_valid h 215 ret neg long 5 216 end 217 if or lt wake 0 lt stop 0 218 ret neg long 9 219 end 220 if eq wake stop 221 ret neg long 22 222 end 223 if ne mapget h 24 8 0 224 ret 2 225 end 226 node_pollfd p 0 wake 227 node_pollfd p 8 stop 228 r = pollfds p 0 16 interval 229 if lt r 0 230 ret r 231 end 232 if ne mapget h 24 8 0 233 ret 2 234 end 235 if ne and get p 14 56 0 236 ret neg long 5 237 end 238 if ne and get p 14 1 0 239 r = node_drain stop 240 if lt r 0 241 ret r 242 end 243 ret 2 244 end 245 if ne and get p 6 56 0 246 ret neg long 5 247 end 248 if ne and get p 6 1 0 249 r = node_drain wake 250 if lt r 0 251 ret r 252 end 253 ret 1 254 end 255 ret 0 256 end 257 note Omega Node epoch pool. Concatenate after transport.om, before super.om. 258 note K arenas (1..8) and one control page are mapped before any worker fork, 259 note so every forked worker inherits the same shared mappings. 260 note Single writer: the creator writes the current epoch, the arena table and 261 note every arena record. Worker w alone writes hold word w while it lives; 262 note the creator clears hold w only after it has reaped worker w. 263 note Control u64: 0 magic, 8 ABI, 16 current epoch, 24 stop, 32 creator PID, 264 note 40 arena count K, 48 worker slots W, 64+8k arena handle k, 265 note 128+8k epoch of arena k (0 free), 256+8w hold of worker w (0 none), 266 note 768+8w worker PID, 1280+8w worker state (0 empty, 1 live, 2 reaped). 267 const PMAGIC ulong `1347374924` 268 const PCAP ulong 8 269 const PWCAP ulong 64 270 const PBYTES ulong 4096 271 272 fn pool_valid c long to bool 273 ret and eq mapget c 0 8 PMAGIC eq mapget c 8 8 1 274 end 275 276 fn pool_mine c long to bool 277 ret eq mapget c 32 8 ulong getpid 278 end 279 280 fn pool_arena c long k ulong to long 281 ret long mapget c add 64 mul k 8 8 282 end 283 284 note Drop the first n arenas and the control page of a pool being refused. 285 fn pool_unwind c long n ulong to long 286 for i ulong n bound 8 287 r = mapdrop pool_arena c i 288 end 289 ret mapdrop c 290 end 291 292 note Return the control handle, or -7 bad size, or a negative mapping errno. 293 note Arena 0 starts as epoch 1. Call before forking any worker. 294 fn pool_new k ulong w ulong to long 295 if or eq k 0 gt k PCAP 296 ret neg long 7 297 end 298 if or eq w 0 gt w PWCAP 299 ret neg long 7 300 end 301 c = mapnew neg int 1 0 PBYTES 3 33 302 if le c 0 303 ret c 304 end 305 r = mapset c 32 8 ulong getpid 306 r = mapset c 40 8 k 307 r = mapset c 48 8 w 308 for i ulong k bound 8 309 h = node_new 310 if le h 0 311 r = pool_unwind c i 312 ret h 313 end 314 r = mapset c add 64 mul i 8 8 ulong h 315 end 316 r = node_reset pool_arena c 0 1 317 r = mapset c 128 8 1 318 r = mapset c 16 8 1 319 r = mapset c 8 8 1 320 r = mapset c 0 8 PMAGIC 321 ret c 322 end 323 324 note Arena index holding epoch e, or -5 when no arena holds it. 325 fn pool_slot c long e ulong to long 326 k = mapget c 40 8 327 if gt k PCAP 328 ret neg long 5 329 end 330 for i ulong k bound 8 331 if eq mapget c add 128 mul i 8 8 e 332 ret long i 333 end 334 end 335 ret neg long 5 336 end 337 338 note Creator: the arena of the current epoch, or a negative refusal. 339 fn pool_current c long to long 340 if not pool_valid c 341 ret neg long 5 342 end 343 k = pool_slot c mapget c 16 8 344 if lt k 0 345 ret k 346 end 347 ret pool_arena c ulong k 348 end 349 350 fn pool_epoch c long to ulong 351 ret mapget c 16 8 352 end 353 354 note Worker step: store hold e for worker w, then read the epoch again. mapset 355 note and mapget fence both sides, so a creator that moved the epoch before it 356 note scanned holds either sees this hold or this read sees the move. Returns 357 note the arena of e, -8 when the epoch moved, or -5 when no arena holds e. 358 note Every refusal clears the hold. 359 fn pool_hold c long w ulong e ulong to long 360 if ge w mapget c 48 8 361 ret neg long 7 362 end 363 hw = add 256 mul w 8 364 r = mapset c hw 8 e 365 if ne mapget c 16 8 e 366 r = mapset c hw 8 0 367 ret neg long 8 368 end 369 k = pool_slot c e 370 if lt k 0 371 r = mapset c hw 8 0 372 ret k 373 end 374 h = pool_arena c ulong k 375 if ne node_epoch h e 376 r = mapset c hw 8 0 377 ret neg long 5 378 end 379 ret h 380 end 381 382 note Worker w holds the current epoch and returns its arena. Returns -1 383 note stopped, -5 invalid, -7 bad worker, -8 the epoch kept moving. 384 fn pool_acquire c long w ulong to long 385 if not pool_valid c 386 ret neg long 5 387 end 388 if ge w mapget c 48 8 389 ret neg long 7 390 end 391 for t ulong 16 392 if ne mapget c 24 8 0 393 r = mapset c add 256 mul w 8 8 0 394 ret neg long 1 395 end 396 h = pool_hold c w mapget c 16 8 397 if ne h neg long 8 398 ret h 399 end 400 end 401 ret neg long 8 402 end 403 404 note Worker w gives up its hold. Its old arena must not be read afterwards. 405 fn pool_release c long w ulong to long 406 if ge w mapget c 48 8 407 ret neg long 7 408 end 409 ret mapset c add 256 mul w 8 8 0 410 end 411 412 fn pool_held c long e ulong to bool 413 n = mapget c 48 8 414 for w ulong n bound 64 415 if eq mapget c add 256 mul w 8 8 e 416 ret true 417 end 418 end 419 ret false 420 end 421 422 note Creator step: retire arena i and open epoch n in it, then publish n. 423 fn pool_reuse c long i ulong n ulong to long 424 off = add 128 mul i 8 425 r = mapset c off 8 0 426 r = node_reset pool_arena c i n 427 if lt r 0 428 ret r 429 end 430 r = mapset c off 8 n 431 r = mapset c 16 8 n 432 ret long n 433 end 434 435 note Creator step: an arena that is free or whose older epoch no worker holds, 436 note or -9. Call only after the current epoch has moved past that arena. 437 fn pool_pick c long to long 438 e = mapget c 16 8 439 k = mapget c 40 8 440 for i ulong k bound 8 441 ek = mapget c add 128 mul i 8 8 442 if ne ek e 443 if or eq ek 0 not pool_held c ek 444 ret long i 445 end 446 end 447 end 448 ret neg long 9 449 end 450 451 note Creator: open the next epoch in a free or unheld arena and make it current. 452 note Returns the new epoch, -1 stopped, -5 invalid, -6 not creator, or -9 when 453 note every other arena is still held: the pool fails closed, nothing changes. 454 fn pool_roll c long to long 455 if not pool_valid c 456 ret neg long 5 457 end 458 if not pool_mine c 459 ret neg long 6 460 end 461 if ne mapget c 24 8 0 462 ret neg long 1 463 end 464 k = pool_pick c 465 if lt k 0 466 ret k 467 end 468 ret pool_reuse c ulong k add mapget c 16 8 1 469 end 470 471 note Creator: publish to the current epoch; when it is full, roll once and retry. 472 note Returns the version, or the refusal of the publish or of the roll. 473 fn pool_publish c long topics ulong src view uchar 4096 n ulong to long 474 h = pool_current c 475 if lt h 0 476 ret h 477 end 478 v = node_publish h topics src n 479 if ne v neg long 2 480 ret v 481 end 482 r = pool_roll c 483 if lt r 0 484 ret r 485 end 486 h = pool_current c 487 if lt h 0 488 ret h 489 end 490 ret node_publish h topics src n 491 end 492 493 fn pool_stop c long to long 494 if not pool_valid c 495 ret neg long 5 496 end 497 if not pool_mine c 498 ret neg long 6 499 end 500 ret mapset c 24 8 1 501 end 502 503 fn pool_stopped c long to bool 504 ret ne mapget c 24 8 0 505 end 506 507 note Creator: record worker w's PID and state for observers. 508 fn pool_setw c long w ulong pid ulong st ulong to long 509 if not pool_mine c 510 ret neg long 6 511 end 512 if ge w mapget c 48 8 513 ret neg long 7 514 end 515 r = mapset c add 768 mul w 8 8 pid 516 ret mapset c add 1280 mul w 8 8 st 517 end 518 519 note Creator: clear hold w. Only after worker w has been reaped. 520 fn pool_clear c long w ulong to long 521 if not pool_mine c 522 ret neg long 6 523 end 524 if ge w mapget c 48 8 525 ret neg long 7 526 end 527 ret mapset c add 256 mul w 8 8 0 528 end 529 530 note Drop every mapping of the pool in this process. Other processes keep 531 note their own inherited mappings until they drop them or exit. 532 fn pool_free c long to long 533 ret pool_unwind c mapget c 40 8 534 end 535 note Omega Node worker supervision. Concatenate after transport.om and pool.om. 536 note The creator forks trusted workers from its own binary: sup_spawn returns 0 537 note in the child, which runs its worker function and exits with its result. 538 note Death is seen without polling PIDs. Worker w alone holds the write end of 539 note its close-on-exec life pipe, so its exit hangs up the creator's read end. 540 note Every worker holds the read end of the creator's life pipe, whose only 541 note writer is the creator, so creator death hangs up every worker. 542 note Per worker: wake, stop and ack eventfds, kept across respawns. 543 note Workers must not exec: exec closes the life pipe and reads as death. 544 global swpid long 64 545 global swwake int 64 546 global swstop int 64 547 global swack int 64 548 global swlife int 64 549 global swst ulong 64 550 global swpw ulong 64 551 global swpar int 2 552 global swme ulong 1 553 global swws uchar 4 554 global swpf uchar 8 555 global swpoll uchar 1024 556 557 fn sup_fd off ulong to int 558 v = ulong get swpf off 559 v = or v shl ulong get swpf add off 1 8 560 v = or v shl ulong get swpf add off 2 16 561 v = or v shl ulong get swpf add off 3 24 562 ret int v 563 end 564 565 note Poll record j at swpoll offset off; a negative fd is stored as itself 566 note (bit pattern) so the kernel ignores that record. 567 fn sup_gpfd off ulong fd int to void 568 u = bitcast uint fd 569 for i ulong 4 570 put swpoll add off i uchar and shr u uint mul i 8 255 571 end 572 put swpoll add off 4 1 573 put swpoll add off 5 0 574 put swpoll add off 6 0 575 put swpoll add off 7 0 576 end 577 578 fn sup_lpfd p edit uchar 24 off ulong fd int to void 579 for i ulong 4 580 put p add off i uchar and shr uint fd uint mul i 8 255 581 end 582 put p add off 4 1 583 end 584 585 note Read and reset an eventfd counter: the number of hints since the last 586 note take, 0 when none is pending, or a negative errno. 587 fn sup_take fd int to long 588 buf = new uchar 8 0 589 for i ulong 64 590 r = read fd buf 0 8 591 if eq r 8 592 v = ulong 0 593 for j ulong 8 594 v = or v shl ulong get buf j mul j 8 595 end 596 ret long and v `9223372036854775807` 597 end 598 if eq r neg long 11 599 ret 0 600 end 601 if ne r neg long 4 602 if lt r 0 603 ret r 604 end 605 ret neg long 5 606 end 607 end 608 ret neg long 4 609 end 610 611 fn sup_closeall to void 612 for i ulong 64 613 if ge get swwake i 0 614 r = fclose get swwake i 615 end 616 if ge get swstop i 0 617 r = fclose get swstop i 618 end 619 if ge get swack i 0 620 r = fclose get swack i 621 end 622 if ge get swlife i 0 623 r = fclose get swlife i 624 end 625 put swwake i neg int 1 626 put swstop i neg int 1 627 put swack i neg int 1 628 put swlife i neg int 1 629 end 630 for i ulong 2 631 if ge get swpar i 0 632 r = fclose get swpar i 633 end 634 put swpar i neg int 1 635 end 636 end 637 638 note Creator: create the creator life pipe and three eventfds per worker slot. 639 note Call once after pool_new and before sup_spawn. 0 or a negative errno; 640 note a failure closes everything this call opened. 641 fn sup_init c long to long 642 if not pool_mine c 643 ret neg long 6 644 end 645 for i ulong 64 646 put swpid i 0 647 put swst i 0 648 put swwake i neg int 1 649 put swstop i neg int 1 650 put swack i neg int 1 651 put swlife i neg int 1 652 end 653 put swme 0 0 654 put swpar 0 neg int 1 655 put swpar 1 neg int 1 656 r = pipefds swpf 0 8 524288 657 if lt r 0 658 ret r 659 end 660 put swpar 0 sup_fd 0 661 put swpar 1 sup_fd 4 662 n = mapget c 48 8 663 for i ulong n bound 64 664 a = eventfd uint 0 665 if lt a 0 666 sup_closeall 667 ret a 668 end 669 put swwake i int a 670 a = eventfd uint 0 671 if lt a 0 672 sup_closeall 673 ret a 674 end 675 put swstop i int a 676 a = eventfd uint 0 677 if lt a 0 678 sup_closeall 679 ret a 680 end 681 put swack i int a 682 end 683 ret 0 684 end 685 686 note Creator: fork worker w. Returns 0 in the child, the child PID in the 687 note creator, -7 bad slot, -16 slot still live, or a negative errno. The 688 note child keeps only its own life write end; the creator keeps the read end. 689 fn sup_spawn c long w ulong to long 690 if not pool_mine c 691 ret neg long 6 692 end 693 if ge w mapget c 48 8 694 ret neg long 7 695 end 696 if ne get swpid w 0 697 ret neg long 16 698 end 699 r = pipefds swpf 0 8 524288 700 if lt r 0 701 ret r 702 end 703 rd = sup_fd 0 704 wr = sup_fd 4 705 pid = fork 706 if eq pid 0 707 r = fclose get swpar 1 708 put swpar 1 neg int 1 709 r = fclose rd 710 put swlife w wr 711 put swme 0 add w 1 712 ret 0 713 end 714 r = fclose wr 715 if lt pid 0 716 r = fclose rd 717 ret pid 718 end 719 put swpid w pid 720 put swlife w rd 721 r = pool_setw c w ulong pid 1 722 ret pid 723 end 724 725 note Worker: 3 creator gone, 2 stop, 1 wake hint, 0 timeout, negative errno. 726 note Creator loss outranks stop, which outranks work. A set stop flag makes 727 note the poll nonblocking. Hints are drained; rescan shared state after 1. 728 fn sup_wwait c long w ulong interval long to long 729 p = new uchar 24 0 730 if ge w 64 731 ret neg long 7 732 end 733 iv = interval 734 if pool_stopped c 735 iv = 0 736 end 737 sup_lpfd p 0 get swwake w 738 sup_lpfd p 8 get swstop w 739 sup_lpfd p 16 get swpar 0 740 r = pollfds p 0 24 iv 741 if lt r 0 742 ret r 743 end 744 if ne get p 22 0 745 ret 3 746 end 747 if pool_stopped c 748 ret 2 749 end 750 if ne and get p 14 56 0 751 ret neg long 5 752 end 753 if ne and get p 14 1 0 754 r = node_drain get swstop w 755 ret 2 756 end 757 if ne and get p 6 56 0 758 ret neg long 5 759 end 760 if ne and get p 6 1 0 761 r = node_drain get swwake w 762 if lt r 0 763 ret r 764 end 765 ret 1 766 end 767 ret 0 768 end 769 770 note Worker or creator: post a hint on worker w's ack eventfd. 771 fn sup_ack w ulong to long 772 if ge w 64 773 ret neg long 7 774 end 775 ret node_notify get swack w 776 end 777 778 note Creator: wake worker w with a work hint. 779 fn sup_wake w ulong to long 780 if ge w 64 781 ret neg long 7 782 end 783 if le get swpid w 0 784 ret neg long 3 785 end 786 ret node_notify get swwake w 787 end 788 789 fn sup_live c long to ulong 790 n = mapget c 48 8 791 k = ulong 0 792 for w ulong n bound 64 793 if gt get swpid w 0 794 k = add k 1 795 end 796 end 797 ret k 798 end 799 800 note Creator: reap worker w, then release what it held. A worker that has not 801 note exited within about one second gets SIGKILL first. Only after the exact 802 note PID is reaped are its hold, PID and life pipe released. Returns the raw 803 note wait status, -3 no live worker, or the waitpid errno (nothing released). 804 fn sup_reap c long w ulong to long 805 if ge w 64 806 ret neg long 7 807 end 808 pid = get swpid w 809 if le pid 0 810 ret neg long 3 811 end 812 done = false 813 for t ulong 100 814 r = waitpid int pid swws 0 4 1 815 if eq r pid 816 done = true 817 break 818 end 819 if ne r 0 820 ret r 821 end 822 r = sleepns `10000000` 823 end 824 if not done 825 r = killpid int pid 9 826 r = waitpid int pid swws 0 4 0 827 if ne r pid 828 ret r 829 end 830 end 831 st = ulong get swws 0 832 st = or st shl ulong get swws 1 8 833 put swst w st 834 r = fclose get swlife w 835 put swlife w neg int 1 836 put swpid w 0 837 r = pool_clear c w 838 r = pool_setw c w 0 2 839 ret long st 840 end 841 842 note Creator: wait up to interval ns for deaths and, when acks is true, acks. 843 note Ack counts are left pending for sup_take. Every worker whose life pipe 844 note hung up is reaped. Returns the number reaped, or a negative errno. 845 fn sup_pollx c long interval long acks bool to long 846 n = mapget c 48 8 847 m = ulong 0 848 for w ulong n bound 64 849 if gt get swpid w 0 850 o = mul m 16 851 if acks 852 sup_gpfd o get swack w 853 else 854 sup_gpfd o neg int 1 855 end 856 sup_gpfd add o 8 get swlife w 857 put swpw m w 858 m = add m 1 859 end 860 end 861 if eq m 0 862 ret 0 863 end 864 r = pollfds swpoll 0 mul m 16 interval 865 if lt r 0 866 ret r 867 end 868 dead = long 0 869 for j ulong m bound 64 870 o = add mul j 16 14 871 if ne get swpoll o 0 872 r = sup_reap c get swpw j 873 if lt r 0 874 ret r 875 end 876 dead = add dead 1 877 end 878 end 879 ret dead 880 end 881 882 note Creator: wait up to interval ns for acks and deaths (see sup_pollx). 883 fn sup_poll c long interval long to long 884 ret sup_pollx c interval true 885 end 886 887 note Creator: stop admission, post every live worker's stop eventfd, then reap 888 note until all have exited or interval ns pass; survivors get SIGKILL and are 889 note reaped. Returns how many had to be killed, or a refusal. 890 fn sup_stop c long interval long to long 891 r = pool_stop c 892 if lt r 0 893 ret r 894 end 895 n = mapget c 48 8 896 for w ulong n bound 64 897 if gt get swpid w 0 898 r = node_notify get swstop w 899 end 900 end 901 dl = add monons interval 902 for t ulong 100000 903 if eq sup_live c 0 904 ret 0 905 end 906 now = monons 907 if ge now dl 908 break 909 end 910 r = sup_pollx c sub dl now false 911 if lt r 0 912 if ne r neg long 4 913 break 914 end 915 end 916 end 917 killed = long 0 918 for w ulong n bound 64 919 if gt get swpid w 0 920 r = killpid int get swpid w 9 921 r = sup_reap c w 922 killed = add killed 1 923 end 924 end 925 ret killed 926 end 927 note Omega Node cooperative scheduling (DRA-287). Concatenate after 928 note transport.om, pool.om and super.om, before one program with fn main. 929 note One creator admits, assigns, collects and reviews jobs. Workers never 930 note write each other's state, and no step waits for every worker. 931 note Scheduling page: one 128-byte cell per worker w at 128*w. 932 note Creator writes 0 assigned seq, 8 job id, 16 input epoch, 24 input 933 note version, 32 interest topics, 40 wake stamp (monons at the last post). 934 note Worker writes 64 done seq, 72 outcome, 80 wakes, 88 last wake latency 935 note ns, 96 largest wake latency ns. 936 note Outbox: worker w's result bytes at 4096*w of a second mapping, written 937 note before its done seq. Each word has one writer; the creator writes a 938 note worker word only after it has reaped that worker. 939 note Job states: 0 free, 1 waiting for deps, 2 ready, 3 running, 4 in review, 940 note 6 UNKNOWN (its worker died mid-job and the job is not safe to rerun). 941 note 942 note Resources and selection policy (P-DRA-14, DRA-395). A request is one 943 note ulong of four 16-bit counts, kind k at bits 16k..16k+15: kind 0 CPU 944 note slots, 1 memory MiB, 2 inference slots, 3 caller-defined. scap holds 945 note the configured capacity of each kind, suse the sum of the needs of the 946 note jobs that hold resources (sjheld 1). A job takes its whole need when it 947 note is assigned and gives it back when its worker reports done or is reaped, 948 note never in part. Every job also needs one idle live worker. 949 note sjarr: admission order (waiting age; kept across reruns). sjrdy: order 950 note in which the job last became ready. sjbyp: times it was bypassed. 951 note Decisions read only these logical counters, never the clock. 952 note Policy 1, fair fitting (DRA-396): a pick first probes the reserved job; 953 note otherwise it scans a window of win slots from a persistent cursor and 954 note takes the oldest admitted ready job that fits. The cursor moves on by 955 note win after every window scan, so every slot is examined within 956 note ceil(64/win) window scans. A ready job older than a job assigned in 957 note the same event was bypassed; once the oldest such job reaches the 958 note bypass budget it is reserved: it is offered first, and no other job may 959 note use, of any kind, the capacity it still lacks (others may use only 960 note free capacity beyond its need). Neither the free capacity it can reach 961 note nor its age ever shrinks, so it runs once running work releases enough 962 note (liveness assumes: every running job ends, by completion or by its 963 note lease, at least one worker stays live, results in review are reviewed, 964 note and dispatch is not paused). Requests over capacity are refused at 965 note admission. 966 note Admission: room is the smaller of the admission headroom and the free 967 note table slots. A refused caller holds a ticket; while tickets are held 968 note only the first holder may enter, so holders enter in ticket order 969 note before anyone new. A ticket not asked about for more than ttl dispatch 970 note events lapses (asked at event e, it survives while e + ttl is at least 971 note the current event count). An absent head blocks later holders until 972 note its ticket lapses; a paused dispatch counts no events, so the block 973 note lasts until dispatch resumes. 974 note Leases: a running job with a lease is reported once by sched_overdue 975 note after its lease passes; the authority then cancels or kills it, which 976 note releases its resources. 977 note 978 note Policy 2, goal-aware fair fitting (DRA-397): policy 1 with a different 979 note order among the examined jobs that fit: larger unblock value first (the 980 note waiting jobs, to depth 2, that the job's declared topics would make 981 note ready), then smaller declared estimate, then older admission. The 982 note reservation, the hold-back, feasibility and the bypass budget are 983 note those of policy 1 and come first, so ranking cannot starve old work. 984 note Values are rescored only when the graph version moved (a job became 985 note ready, a waiting job was admitted, a job was cancelled, a declaration 986 note changed), once per pick however many changes came before it. 987 note Completions, releases and lease expiry replan (every step dispatches) 988 note without rescoring. A proposal names a job by id and the graph version 989 note it was computed at, and is rejected when that version is no longer 990 note current; the version carries the scheduler's incarnation, so a 991 note proposal made for another scheduler is never current here. 992 const SJCAP ulong 64 993 const SPVER ulong 3 994 global scmap long 2 995 global sclim ulong 2 996 global sjst ulong 64 997 global sjdep ulong 64 998 global sjid ulong 64 999 global sjw ulong 64 1000 global sjatt ulong 64 1001 global sjfl ulong 64 1002 global sjout ulong 64 1003 global sjneed ulong 64 1004 global sjheld ulong 64 1005 global sjarr ulong 64 1006 global sjrdy ulong 64 1007 global sjbyp ulong 64 1008 global sjlse ulong 64 1009 global sjdue ulong 64 1010 global sttk ulong 64 1011 global stts ulong 64 1012 global scbusy ulong 64 1013 global scseq ulong 64 1014 global swcur ulong 2 1015 global scpause ulong 1 1016 global scap ulong 4 1017 global suse ulong 4 1018 note Policy words: 0 policy (0 FIFO head-of-line, 1 fair fitting), 1 1019 note candidate window in slots, 2 dispatch budget per event, 3 bypass 1020 note budget, 4 cursor, 5 reserved job slot + 1 (0 none), 6 tickets held, 1021 note 7 ticket lifetime in dispatch events. 1022 global scpol ulong 8 1023 note Counters (sched_stat): 0 admissions, 1 ready stamps, 2 dispatch events, 1024 note 3 candidate slot reads, 4 jobs assigned, 5 bypasses, 6 reservations 1025 note made, 7 impossible requests refused, 8 head-of-line stops, 9 reserved 1026 note jobs assigned, 10 tickets issued, 11 tickets lapsed, 12 leases overdue, 1027 note 13 maintenance slot reads, 14 window scans, 15 dispatch events refused 1028 note because the replay ring was full (DRA-398). 1029 global scst ulong 16 1030 note Goal-aware ranking (policy 2): sjpro the topics a job declares its 1031 note completion publishes, sjest its declared work estimate in ms, sjunc 1032 note that estimate's uncertainty in ms, sjval its unblock value in jobs as 1033 note last scored. scgv: 0 graph version, 1 version last scored, 2 proposed 1034 note job slot + 1 (0 none), 3 version the proposal named, 4 proposal state 1035 note (0 no incarnation yet: proposals refused; 1 accepting; 2 the version 1036 note is exhausted: proposals refused for the rest of this scheduler). The 1037 note graph version is the scheduler's incarnation (sched_incarnate, once) 1038 note times 2^40 plus the bumps since, never more than 2^40 - 1 of them: the 1039 note bump that would carry into the next incarnation's range exhausts the 1040 note version instead, so schedulers of different incarnations never share a 1041 note version. Within one scheduler every admission of a job, cancellation 1042 note of a waiting or ready one and readiness bumps it, so a proposal whose 1043 note version is current still names the job it was made for. 1044 global sjpro ulong 64 1045 global sjest ulong 64 1046 global sjunc ulong 64 1047 global sjval ulong 64 1048 global scgv ulong 5 1049 note Goal counters (sched_stat 32..39): 0 rescores, 1 jobs scored, 2 graph 1050 note version bumps, 3 proposals accepted, 4 proposals stale, 5 proposals 1051 note refused, 6 slots read by value traversal, 7 proposals used. 1052 global scgst ulong 8 1053 note Replay ring (DRA-398). While a journal is attached (sfj 0 is 1), each 1054 note dispatch event that assigned a job or changed the reservation leaves a 1055 note record of 6 words in sfr: event count, assigned slot bitmap, bypassed 1056 note slot bitmap (the slots whose bypass count went up), cursor, reserved 1057 note slot + 1, and 0. A publication that makes waiting jobs ready leaves a 1058 note readiness record: the current event count, no assigned or bypassed 1059 note slot, cursor, reserved slot + 1, and the bitmap of the slots it made 1060 note ready. sfj 1 counts the records waiting; sfj 2 and 3 are the 1061 note reservation at the start of the current event and the slots it has 1062 note assigned so far. The journal drains them before it appends any other 1063 note record, so a transition is never durable before the dispatch 1064 note decisions and readiness that came before it. With 64 waiting, 1065 note dispatch assigns nothing and publication is refused (counter 15) 1066 note until they are drained. 1067 global sfr ulong 384 1068 global sfj ulong 4 1069 1070 fn sc_cell w ulong off ulong to ulong 1071 ret mapget get scmap 0 add mul w 128 off 8 1072 end 1073 1074 fn sc_set w ulong off ulong v ulong to void 1075 r = mapset get scmap 0 add mul w 128 off 8 v 1076 end 1077 1078 note Creator, before any fork: map the scheduling page and the outboxes and 1079 note set the limits on admitted (a) and in-review (rl) jobs, each 1..64. 1080 note Returns 0, -6 not creator, -7 bad limit, or a mapping errno. 1081 fn sched_new c long a ulong rl ulong to long 1082 if not pool_mine c 1083 ret neg long 6 1084 end 1085 if or eq a 0 gt a SJCAP 1086 ret neg long 7 1087 end 1088 if or eq rl 0 gt rl SJCAP 1089 ret neg long 7 1090 end 1091 s = mapnew neg int 1 0 8192 3 33 1092 if le s 0 1093 ret s 1094 end 1095 o = mapnew neg int 1 0 262144 3 33 1096 if le o 0 1097 r = mapdrop s 1098 ret o 1099 end 1100 put scmap 0 s 1101 put scmap 1 o 1102 put sclim 0 a 1103 put sclim 1 rl 1104 for j ulong 64 1105 put sjst j 0 1106 put sjheld j 0 1107 put sjlse j 0 1108 put sjdue j 0 1109 put sjpro j 0 1110 put sjest j 0 1111 put sjunc j 0 1112 put sjval j 0 1113 put scbusy j 0 1114 put scseq j 0 1115 end 1116 for k ulong 4 1117 put scap k 65535 1118 put suse k 0 1119 end 1120 put sfj 1 0 1121 for i ulong 16 1122 put scst i 0 1123 end 1124 for i ulong 8 1125 put scgst i 0 1126 end 1127 for i ulong 5 1128 put scgv i 0 1129 end 1130 put scpol 0 1 1131 put scpol 1 64 1132 put scpol 2 64 1133 put scpol 3 4 1134 put scpol 4 0 1135 put scpol 5 0 1136 put scpol 6 0 1137 put scpol 7 1000 1138 put scpause 0 0 1139 ret 0 1140 end 1141 1142 note Count of kind k (0..3) in packed request req. 1143 fn sp_need req ulong k ulong to ulong 1144 ret and shr req mul k 16 65535 1145 end 1146 1147 note Free capacity of kind k. sched_cap never sets a capacity below the 1148 note use, so this cannot underflow. 1149 fn sp_free k ulong to ulong 1150 ret sub get scap k get suse k 1151 end 1152 1153 note Does req fit in the free capacity once the reserved request res has 1154 note what it still lacks? Of each kind, res keeps min(free, its need); res 0 1155 note is no reservation. This keeps the reserved job's attainable capacity 1156 note from ever shrinking (the drain). 1157 fn sp_fits req ulong res ulong to bool 1158 for k ulong 4 1159 f = sp_free k 1160 r = sp_need res k 1161 a = ulong 0 1162 if gt f r 1163 a = sub f r 1164 end 1165 if gt sp_need req k a 1166 ret false 1167 end 1168 end 1169 ret true 1170 end 1171 1172 note Could req ever run, with the configured capacity all free? 1173 fn sp_feas req ulong to bool 1174 for k ulong 4 1175 if gt sp_need req k get scap k 1176 ret false 1177 end 1178 end 1179 ret true 1180 end 1181 1182 note Job j takes its whole need. 1183 fn sp_take j ulong to void 1184 for k ulong 4 1185 put suse k add get suse k sp_need get sjneed j k 1186 end 1187 put sjheld j 1 1188 end 1189 1190 note Job j gives back its need, if it holds it. 1191 fn sp_give j ulong to void 1192 if eq get sjheld j 0 1193 ret 1194 end 1195 for k ulong 4 1196 put suse k sub get suse k sp_need get sjneed j k 1197 end 1198 put sjheld j 0 1199 end 1200 1201 note Job j becomes ready, stamped with the next ready order. 1202 fn sp_ready j ulong to void 1203 put sjst j 2 1204 put sjrdy j get scst 1 1205 put scst 1 add get scst 1 1 1206 sp_bump 1207 end 1208 1209 note Creator: counter i of scst, capacity (i 16..19) or use (i 20..23) of a 1210 note kind, policy word (i 24..31), goal counter (i 32..39), graph version 1211 note (i 40), proposal state (i 41: 0 no incarnation, 1 accepting, 2 1212 note exhausted). 1213 fn sched_stat i ulong to ulong 1214 if lt i 16 1215 ret get scst i 1216 end 1217 if lt i 20 1218 ret get scap sub i 16 1219 end 1220 if lt i 24 1221 ret get suse sub i 20 1222 end 1223 if lt i 32 1224 ret get scpol sub i 24 1225 end 1226 if lt i 40 1227 ret get scgst sub i 32 1228 end 1229 if eq i 40 1230 ret get scgv 0 1231 end 1232 if eq i 41 1233 ret get scgv 4 1234 end 1235 ret 0 1236 end 1237 1238 note Creator: would sched_cap caps be accepted? Not when the new capacity of 1239 note a kind is below its use by running jobs, or below the need of any one 1240 note admitted job. 1241 fn sched_capok caps ulong to bool 1242 for k ulong 4 1243 if lt sp_need caps k get suse k 1244 ret false 1245 end 1246 end 1247 for j ulong 64 1248 s = get sjst j 1249 if and ge s 1 le s 3 1250 for k ulong 4 1251 if gt sp_need get sjneed j k sp_need caps k 1252 ret false 1253 end 1254 end 1255 end 1256 end 1257 ret true 1258 end 1259 1260 note Creator: set the capacity of every kind from caps (packed like a 1261 note request). 0, or -8 when sched_capok refuses (nothing changes). 1262 fn sched_cap caps ulong to long 1263 if not sched_capok caps 1264 ret neg long 8 1265 end 1266 for k ulong 4 1267 put scap k sp_need caps k 1268 end 1269 ret 0 1270 end 1271 1272 note Creator: the configured capacities, packed like a request. 1273 fn sched_caps to ulong 1274 v = ulong 0 1275 for k ulong 4 1276 v = or v shl get scap k mul k 16 1277 end 1278 ret v 1279 end 1280 1281 note Creator: would sched_policy accept these words? Policy p 0..2, window 1282 note win 1..64, dispatch budget db 1..64, bypass budget by 1..65535, ticket 1283 note lifetime ttl 1..`1000000`. 1284 fn sched_polok p ulong win ulong db ulong by ulong ttl ulong to bool 1285 if gt p 2 1286 ret false 1287 end 1288 if or eq win 0 gt win 64 1289 ret false 1290 end 1291 if or eq db 0 gt db 64 1292 ret false 1293 end 1294 if or eq by 0 gt by 65535 1295 ret false 1296 end 1297 ret and gt ttl 0 le ttl `1000000` 1298 end 1299 1300 note Creator: policy p (0 FIFO head-of-line, 1 fair fitting, 2 goal-aware 1301 note fair fitting), candidate window win slots, dispatch budget db jobs per 1302 note event, bypass budget by, ticket lifetime ttl dispatch events, in the 1303 note ranges of sched_polok. The cursor and any reservation are kept. 0 or 1304 note -7. 1305 fn sched_policy p ulong win ulong db ulong by ulong ttl ulong to long 1306 if not sched_polok p win db by ttl 1307 ret neg long 7 1308 end 1309 put scpol 0 p 1310 put scpol 1 win 1311 put scpol 2 db 1312 put scpol 3 by 1313 put scpol 7 ttl 1314 ret 0 1315 end 1316 1317 note Creator: drop this process's scheduling mappings. 1318 fn sched_free to long 1319 r = mapdrop get scmap 1 1320 if lt r 0 1321 ret r 1322 end 1323 ret mapdrop get scmap 0 1324 end 1325 1326 note Jobs whose state is in lo..hi. 1327 fn sched_count lo ulong hi ulong to ulong 1328 n = ulong 0 1329 for j ulong 64 1330 s = get sjst j 1331 if and ge s lo le s hi 1332 n = add n 1 1333 end 1334 end 1335 ret n 1336 end 1337 1338 note Position of id's admission ticket, or 64. 1339 fn sp_tkpos id ulong to ulong 1340 for t ulong get scpol 6 bound 64 1341 if eq get sttk t id 1342 ret t 1343 end 1344 end 1345 ret 64 1346 end 1347 1348 note Compact the tickets in order, dropping ticket d (64: none) and every 1349 note ticket not asked about for the ticket lifetime. 1350 fn sp_tkpack d ulong to void 1351 k = ulong 0 1352 for t ulong get scpol 6 bound 64 1353 if ne t d 1354 if lt add get stts t get scpol 7 get scst 2 1355 put scst 11 add get scst 11 1 1356 else 1357 put sttk k get sttk t 1358 put stts k get stts t 1359 k = add k 1 1360 end 1361 end 1362 end 1363 put scpol 6 k 1364 end 1365 1366 note Refresh ticket t, or give id a ticket at the end when it has none and 1367 note the line has room (a caller refused with the line full holds none). 1368 fn sp_tkhold id ulong t ulong to void 1369 if lt t 64 1370 put stts t get scst 2 1371 ret 1372 end 1373 n = get scpol 6 1374 if lt n 64 1375 put sttk n id 1376 put stts n get scst 2 1377 put scpol 6 add n 1 1378 put scst 10 add get scst 10 1 1379 end 1380 end 1381 1382 note Creator: may id be admitted now? Room is the smaller of the admission 1383 note headroom and the free table slots (a job in review or UNKNOWN holds 1384 note its slot). While tickets are held only the first holder may enter; 1385 note with none, anyone may while there is room. A refused caller gets or 1386 note keeps a ticket; every ask by a holder, refused or not, refreshes its 1387 note ticket's last-asked event, and a caller with room and no ticket gets 1388 note none. A true answer guarantees the slot for the next sched_admitr 1389 note (single creator), so a caller may ask before journaling (as jn_admit 1390 note does). 1391 fn sched_room id ulong to bool 1392 sp_tkpack 64 1393 t = sp_tkpos id 1394 a = ulong 0 1395 fs = ulong 0 1396 for j ulong 64 1397 s = get sjst j 1398 if eq s 0 1399 fs = add fs 1 1400 end 1401 if and ge s 1 le s 3 1402 a = add a 1 1403 end 1404 end 1405 room = ulong 0 1406 if lt a get sclim 0 1407 room = sub get sclim 0 a 1408 end 1409 if lt fs room 1410 room = fs 1411 end 1412 if gt room 0 1413 if eq get scpol 6 0 1414 ret true 1415 end 1416 if eq t 0 1417 put stts 0 get scst 2 1418 ret true 1419 end 1420 end 1421 sp_tkhold id t 1422 ret false 1423 end 1424 1425 note Creator: admit job id needing req (packed resources). deps 0 makes it 1426 note ready at once; otherwise it waits for a publication whose topics meet 1427 note deps. flags bit 0: safe to rerun after its worker dies. Returns the job 1428 note slot; -8 when req exceeds the configured capacity of a kind (it could 1429 note never run); -2 when sched_room refuses: at the admission limit, or 1430 note ticket holders ahead; the caller keeps a ticket and asks again. 1431 fn sched_admitr id ulong deps ulong flags ulong req ulong to long 1432 ret sched_admits id deps flags req sched_slot 1433 end 1434 1435 note Creator: the lowest free table slot (the one sched_admitr would give 1436 note the next job), or 64 when none is free. 1437 fn sched_slot to ulong 1438 for j ulong 64 1439 if eq get sjst j 0 1440 ret j 1441 end 1442 end 1443 ret 64 1444 end 1445 1446 note Creator: could req ever run, with the configured capacity all free? 1447 fn sched_feas req ulong to bool 1448 ret sp_feas req 1449 end 1450 1451 note Creator: sched_admitr into table slot j, which must be free: the 1452 note journal records the slot with the admission and recovery puts the job 1453 note back into it (DRA-398). Refusals as sched_admitr; -2 also when slot j 1454 note is not free. 1455 fn sched_admits id ulong deps ulong flags ulong req ulong j ulong to long 1456 if not sp_feas req 1457 put scst 7 add get scst 7 1 1458 ret neg long 8 1459 end 1460 if not sched_room id 1461 ret neg long 2 1462 end 1463 if ge j 64 1464 ret neg long 2 1465 end 1466 if ne get sjst j 0 1467 ret neg long 2 1468 end 1469 t = sp_tkpos id 1470 if lt t 64 1471 sp_tkpack t 1472 end 1473 put sjid j id 1474 put sjdep j deps 1475 put sjfl j flags 1476 put sjatt j 0 1477 put sjout j 0 1478 put sjw j 0 1479 put sjneed j req 1480 put sjheld j 0 1481 put sjbyp j 0 1482 put sjlse j 0 1483 put sjdue j 0 1484 put sjarr j get scst 0 1485 put scst 0 add get scst 0 1 1486 put sjpro j 0 1487 put sjest j 0 1488 put sjunc j 0 1489 put sjval j 0 1490 if eq deps 0 1491 sp_ready j 1492 else 1493 put sjst j 1 1494 sp_bump 1495 end 1496 ret long j 1497 end 1498 1499 note Creator: job j (waiting, ready or running) gets a lease of ns from its 1500 note next assignment, or from now when it runs; 0 removes it. 0; -3 for a 1501 note slot that is not waiting, ready or running; -7 for ns above 2^50 (about 1502 note 13 days). Both refusals change nothing. The bound keeps every deadline 1503 note (the monotonic clock, below 2^63, plus ns) representable: it never 1504 note wraps into the past or onto 0, the no-deadline value. 1505 fn sched_lease j ulong ns ulong to long 1506 if ge j 64 1507 ret neg long 3 1508 end 1509 s = get sjst j 1510 if or lt s 1 gt s 3 1511 ret neg long 3 1512 end 1513 if gt ns `1125899906842624` 1514 ret neg long 7 1515 end 1516 put sjlse j ns 1517 if eq s 3 1518 put sjdue j 0 1519 if gt ns 0 1520 put sjdue j add ulong monons ns 1521 end 1522 end 1523 ret 0 1524 end 1525 1526 note Creator: a running job past its lease whose worker has not reported 1527 note done, reported once (its deadline is cleared): its slot, or 64. The 1528 note authority cancels or kills it; the release follows from the worker's 1529 note report or reap. 1530 fn sched_overdue to ulong 1531 now = ulong monons 1532 for j ulong 64 1533 if eq get sjst j 3 1534 d = get sjdue j 1535 if gt d 0 1536 if ge now d 1537 w = get sjw j 1538 if ne sc_cell w 64 get scseq w 1539 put sjdue j 0 1540 put scst 12 add get scst 12 1 1541 ret j 1542 end 1543 end 1544 end 1545 end 1546 end 1547 ret 64 1548 end 1549 1550 note Creator: sched_admitr with no resource need beyond one worker. 1551 fn sched_admit id ulong deps ulong flags ulong to long 1552 ret sched_admitr id deps flags 0 1553 end 1554 1555 note Creator: worker w is woken by publications whose topics meet topics. 1556 fn sched_want w ulong topics ulong to long 1557 if ge w 64 1558 ret neg long 7 1559 end 1560 sc_set w 32 topics 1561 ret 0 1562 end 1563 1564 fn sc_wake w ulong to void 1565 sc_set w 40 ulong monons 1566 r = sup_wake w 1567 end 1568 1569 note Lowest idle live worker below n, or 64 when none. A worker is idle when 1570 note the creator has collected its last assignment. 1571 fn sp_idle n ulong to ulong 1572 for w ulong n bound 64 1573 if and gt get swpid w 0 eq get scbusy w 0 1574 ret w 1575 end 1576 end 1577 ret 64 1578 end 1579 1580 note Assign job j to idle worker w: it takes its need; the job id and the 1581 note input epoch and version are written before the seq, then the wake. 1582 fn sp_start c long j ulong w ulong to void 1583 q = add get scseq w 1 1584 put scseq w q 1585 put scbusy w add j 1 1586 put sjst j 3 1587 put sjw j w 1588 put sjatt j add get sjatt j 1 1589 put sjbyp j 0 1590 put sjdue j 0 1591 if gt get sjlse j 0 1592 put sjdue j add ulong monons get sjlse j 1593 end 1594 sp_take j 1595 if eq get scpol 5 add j 1 1596 put scpol 5 0 1597 put scst 9 add get scst 9 1 1598 end 1599 sc_set w 8 get sjid j 1600 sc_set w 16 pool_epoch c 1601 sc_set w 24 node_count pool_current c 1602 sc_set w 0 q 1603 sc_wake w 1604 put scst 4 add get scst 4 1 1605 end 1606 1607 note Policy 0, FIFO head-of-line: the stop rule of the reference pump 1608 note (aien-sovereign-core-mv spark-harness scheduler/core.rs Core::pump, 1609 note 598-602), not its queue: the head here is the ready job that became 1610 note ready first (the pump batches newly eligible tasks in task-id order 1611 note into a bounded queue). The head is chosen when it fits, and when it 1612 note does not fit nothing is chosen, so a head that does 1613 note not fit holds back every later job. Returns the slot or 64. 1614 fn sp_fifo to ulong 1615 h = ulong 64 1616 for j ulong 64 1617 put scst 3 add get scst 3 1 1618 if eq get sjst j 2 1619 if eq h 64 1620 h = j 1621 else 1622 if lt get sjrdy j get sjrdy h 1623 h = j 1624 end 1625 end 1626 end 1627 end 1628 if eq h 64 1629 ret 64 1630 end 1631 if sp_fits get sjneed h 0 1632 ret h 1633 end 1634 put scst 8 add get scst 8 1 1635 ret 64 1636 end 1637 1638 note Policy 1, fair fitting: the reserved job first when it fits; else the 1639 note oldest admitted ready job, among win slots from the cursor, that fits 1640 note without the capacity the reserved job still lacks; the cursor then 1641 note moves on by win. Returns the slot or 64. Every slot read counts 1642 note (counter 3: the reserved probe and the window), at most win + 1 per 1643 note pick, win + 2 under policy 2 (sp_proposal's probe counts too); 1644 note counter 14 counts window scans. 1645 fn sp_fair to ulong 1646 rq = ulong 0 1647 r = get scpol 5 1648 if gt r 0 1649 rj = sub r 1 1650 put scst 3 add get scst 3 1 1651 if eq get sjst rj 2 1652 if sp_fits get sjneed rj 0 1653 ret rj 1654 end 1655 rq = get sjneed rj 1656 else 1657 put scpol 5 0 1658 end 1659 end 1660 goal = eq get scpol 0 2 1661 if goal 1662 sp_rescore 1663 pj = sp_proposal rq 1664 if lt pj 64 1665 ret pj 1666 end 1667 end 1668 win = get scpol 1 1669 cur = get scpol 4 1670 b = ulong 64 1671 put scst 14 add get scst 14 1 1672 for i ulong win bound 64 1673 put scst 3 add get scst 3 1 1674 j = rem add cur i 64 1675 if eq get sjst j 2 1676 if sp_fits get sjneed j rq 1677 if eq b 64 1678 b = j 1679 else 1680 if sp_better goal j b 1681 b = j 1682 end 1683 end 1684 end 1685 end 1686 end 1687 put scpol 4 rem add cur win 64 1688 ret b 1689 end 1690 1691 note Ranking key, both of ready jobs a and b: is a better? Policy 1: older 1692 note admission. Policy 2: larger unblock value, then smaller declared 1693 note estimate, then older admission (admission orders are unique, so there 1694 note is never a tie). 1695 fn sp_better goal bool a ulong b ulong to bool 1696 if goal 1697 va = get sjval a 1698 vb = get sjval b 1699 if ne va vb 1700 ret gt va vb 1701 end 1702 ea = get sjest a 1703 eb = get sjest b 1704 if ne ea eb 1705 ret lt ea eb 1706 end 1707 end 1708 ret lt get sjarr a get sjarr b 1709 end 1710 1711 note A committed change that can alter an unblock value: a job becomes 1712 note ready, a waiting job is admitted, a waiting or ready job is cancelled, 1713 note or a declaration changes. Bumps the graph version, which makes any 1714 note proposal stale (rejected when next used). Nothing is rescored here: 1715 note the next policy-2 pick rescores once, however many bumps came before 1716 note it (coalesced). At 2^40 - 1 bumps the version stays and is exhausted 1717 note (scgv 4 = 2): from then on proposals are refused and every pick 1718 note rescores. 1719 fn sp_bump to void 1720 put scgst 2 add get scgst 2 1 1721 if eq and get scgv 0 `1099511627775` `1099511627775` 1722 put scgv 4 2 1723 ret 1724 end 1725 put scgv 0 add get scgv 0 1 1726 end 1727 1728 note Unblock value of ready job j, in jobs: the waiting jobs whose deps meet 1729 note the topics j declares it unblocks (depth 1), plus the waiting jobs whose 1730 note deps meet the topics those declare (depth 2), each counted once. Reads 1731 note at most 128 slots. 1732 fn sp_value j ulong to ulong 1733 pro = get sjpro j 1734 if eq pro 0 1735 ret 0 1736 end 1737 n = ulong 0 1738 m = ulong 0 1739 p2 = ulong 0 1740 for k ulong 64 1741 if eq get sjst k 1 1742 if ne and get sjdep k pro 0 1743 m = or m shl ulong 1 k 1744 p2 = or p2 get sjpro k 1745 n = add n 1 1746 end 1747 end 1748 end 1749 put scgst 6 add get scgst 6 64 1750 if eq p2 0 1751 ret n 1752 end 1753 for k ulong 64 1754 if eq get sjst k 1 1755 if eq and shr m k 1 0 1756 if ne and get sjdep k p2 0 1757 n = add n 1 1758 end 1759 end 1760 end 1761 end 1762 put scgst 6 add get scgst 6 64 1763 ret n 1764 end 1765 1766 note Recompute the unblock value of every ready job when the graph version 1767 note moved since the last scoring, or always once it is exhausted; 1768 note otherwise nothing. 1769 fn sp_rescore to void 1770 v = get scgv 0 1771 if and eq get scgv 1 v ne get scgv 4 2 1772 ret 1773 end 1774 for j ulong 64 1775 if eq get sjst j 2 1776 put sjval j sp_value j 1777 put scgst 1 add get scgst 1 1 1778 end 1779 end 1780 put scgv 1 v 1781 put scgst 0 add get scgst 0 1 1782 put scgst 6 add get scgst 6 64 1783 end 1784 1785 note The proposed job, when the proposal is for the current graph version 1786 note (and that version is not exhausted) and the job is still ready and fits 1787 note beside the reservation (rq); it is used once. Otherwise the proposal 1788 note is dropped as stale: 64. 1789 fn sp_proposal rq ulong to ulong 1790 p = get scgv 2 1791 if eq p 0 1792 ret 64 1793 end 1794 put scgv 2 0 1795 put scst 3 add get scst 3 1 1796 j = sub p 1 1797 if and eq get scgv 3 get scgv 0 eq get scgv 4 1 1798 if eq get sjst j 2 1799 if sp_fits get sjneed j rq 1800 put scgst 7 add get scgst 7 1 1801 ret j 1802 end 1803 end 1804 end 1805 put scgst 4 add get scgst 4 1 1806 ret 64 1807 end 1808 1809 note Creator: declare what job j (waiting or ready) is expected to unblock 1810 note and cost: pro, the topics its completion will publish; est, the work 1811 note estimate in ms (declared, or derived by the caller from measurements); 1812 note unc, its uncertainty in ms (recorded, not used to rank). 0, or -3. 1813 fn sched_goal j ulong pro ulong est ulong unc ulong to long 1814 if ge j 64 1815 ret neg long 3 1816 end 1817 s = get sjst j 1818 if or eq s 0 gt s 2 1819 ret neg long 3 1820 end 1821 put sjpro j pro 1822 put sjest j est 1823 put sjunc j unc 1824 sp_bump 1825 ret 0 1826 end 1827 1828 note Creator: the graph version that proposals must name. 1829 fn sched_version to ulong 1830 ret get scgv 0 1831 end 1832 1833 note Creator, after sched_new: incarnation n (1..2^24 - 1), distinct 1834 note for every scheduler that may receive proposals meant for another (the 1835 note journal fence, DRA-398). The graph version becomes n * 2^40, which no 1836 note scheduler of another incarnation reaches, any proposal is dropped and 1837 note proposals are accepted from now on. The last scoring stays below n * 1838 note 2^40, so the next ranking pass rescores whatever was admitted or 1839 note declared before, whenever the incarnation comes. Once per 1840 note scheduler: 0, or -7 (n out of range, or already set or exhausted). 1841 fn sched_incarnate n ulong to long 1842 if or eq n 0 ge n `16777216` 1843 ret neg long 7 1844 end 1845 if ne get scgv 4 0 1846 ret neg long 7 1847 end 1848 put scgv 0 shl n 40 1849 put scgv 2 0 1850 put scgv 4 1 1851 ret 0 1852 end 1853 1854 note Creator, policy 2: propose the ready job whose id is id for the next 1855 note pick, computed at graph version v (an outside planner, or an 1856 note operator). The proposal names the job, not its slot. Reservations and 1857 note feasibility still come first: it is checked again when used. 0; -6 no 1858 note incarnation set or the version exhausted; -9 stale (v is not the 1859 note current version); -3 no ready job, or more than one, has that id; -8 1860 note it does not fit beside the reservation; -7 not policy 2. 1861 fn sched_propose id ulong v ulong to long 1862 if ne get scpol 0 2 1863 ret neg long 7 1864 end 1865 if ne get scgv 4 1 1866 put scgst 5 add get scgst 5 1 1867 ret neg long 6 1868 end 1869 if ne v get scgv 0 1870 put scgst 4 add get scgst 4 1 1871 ret neg long 9 1872 end 1873 j = ulong 64 1874 n = ulong 0 1875 for k ulong 64 1876 if and eq get sjst k 2 eq get sjid k id 1877 j = k 1878 n = add n 1 1879 end 1880 end 1881 if ne n 1 1882 put scgst 5 add get scgst 5 1 1883 ret neg long 3 1884 end 1885 rq = ulong 0 1886 r = get scpol 5 1887 if gt r 0 1888 rq = get sjneed sub r 1 1889 end 1890 if not sp_fits get sjneed j rq 1891 put scgst 5 add get scgst 5 1 1892 ret neg long 8 1893 end 1894 put scgv 2 add j 1 1895 put scgv 3 v 1896 put scgst 3 add get scgst 3 1 1897 ret 0 1898 end 1899 1900 note After a fair dispatch event in which ya - 1 was the youngest admission 1901 note assigned (ya 0: none): every ready job admitted before it was bypassed 1902 note once; with no reservation, the oldest job at the bypass budget is 1903 note reserved. One pass over the table (64 maintenance reads, counter 13). 1904 note Returns the bitmap of the slots bypassed. 1905 fn sp_after ya ulong to ulong 1906 o = ulong 64 1907 bm = ulong 0 1908 nores = eq get scpol 5 0 1909 for j ulong 64 1910 if eq get sjst j 2 1911 if gt ya 0 1912 if lt add get sjarr j 1 ya 1913 put sjbyp j add get sjbyp j 1 1914 put scst 5 add get scst 5 1 1915 bm = or bm shl 1 j 1916 end 1917 end 1918 if nores 1919 if ge get sjbyp j get scpol 3 1920 if eq o 64 1921 o = j 1922 else 1923 if lt get sjarr j get sjarr o 1924 o = j 1925 end 1926 end 1927 end 1928 end 1929 end 1930 end 1931 put scst 13 add get scst 13 64 1932 if lt o 64 1933 put scpol 5 add o 1 1934 put scst 6 add get scst 6 1 1935 end 1936 ret bm 1937 end 1938 1939 note Creator: assign ready jobs to idle live workers under the policy, at 1940 note most the dispatch budget per call. Returns the number assigned. With 1941 note a journal attached and the replay ring full, nothing is assigned 1942 note (counter 15) until the journal drains it. 1943 fn sched_dispatch c long to ulong 1944 if ne get scpause 0 0 1945 ret 0 1946 end 1947 if and ne get sfj 0 0 ge get sfj 1 64 1948 put scst 15 add get scst 15 1 1949 ret 0 1950 end 1951 n = mapget c 48 8 1952 k = ulong 0 1953 ya = ulong 0 1954 put sfj 2 get scpol 5 1955 put sfj 3 0 1956 fair = ge get scpol 0 1 1957 put scst 2 add get scst 2 1 1958 for i ulong get scpol 2 bound 64 1959 w = sp_idle n 1960 if ge w 64 1961 break 1962 end 1963 j = ulong 64 1964 if fair 1965 j = sp_fair 1966 else 1967 j = sp_fifo 1968 end 1969 if ge j 64 1970 break 1971 end 1972 if gt add get sjarr j 1 ya 1973 ya = add get sjarr j 1 1974 end 1975 sp_fmark j 1976 sp_start c j w 1977 k = add k 1 1978 end 1979 bm = ulong 0 1980 if fair 1981 bm = sp_after ya 1982 end 1983 sp_frend bm 1984 ret k 1985 end 1986 1987 note End of a dispatch event with a journal attached: one ring record when 1988 note it assigned a job or moved the reservation. 1989 fn sp_frend bm ulong to void 1990 if eq get sfj 0 0 1991 ret 1992 end 1993 if or ne get sfj 3 0 ne get scpol 5 get sfj 2 1994 sp_frpush get sfj 3 bm 0 1995 end 1996 end 1997 1998 note The current dispatch event assigned slot j (sfj 3). 1999 fn sp_fmark j ulong to void 2000 put sfj 3 or get sfj 3 shl 1 j 2001 end 2002 2003 note Append one replay record (sfr): event, assigned and bypassed slot 2004 note bitmaps, cursor, reserved slot + 1, slots made ready. The callers 2005 note leave room. 2006 fn sp_frpush am ulong bm ulong rd ulong to void 2007 b = mul get sfj 1 6 2008 put sfr b get scst 2 2009 put sfr add b 1 am 2010 put sfr add b 2 bm 2011 put sfr add b 3 get scpol 4 2012 put sfr add b 4 get scpol 5 2013 put sfr add b 5 rd 2014 put sfj 1 add get sfj 1 1 2015 end 2016 2017 note Journal: attach (on) or detach the replay ring. Attaching empties it. 2018 note jn_recover attaches once the scheduler holds the journaled state. 2019 fn sched_jattach on bool to void 2020 put sfj 0 0 2021 if on 2022 put sfj 0 1 2023 end 2024 put sfj 1 0 2025 end 2026 2027 fn sched_jattached to bool 2028 ret ne get sfj 0 0 2029 end 2030 2031 note Journal: records waiting in the replay ring, word i (0..5) of record k, 2032 note and emptying it once they are durable. 2033 fn sched_frn to ulong 2034 ret get sfj 1 2035 end 2036 fn sched_frw k ulong i ulong to ulong 2037 ret get sfr add mul k 6 i 2038 end 2039 fn sched_frclear to void 2040 put sfj 1 0 2041 end 2042 2043 note Journal, recovery: the cursor and reservation after replay, and the 2044 note dispatch event count. The reservation names slot res - 1, which must 2045 note hold a ready job. -7 (nothing changed) for a cursor outside the table 2046 note or any other reservation, else 0. 2047 fn sched_frestore cur ulong ev ulong res ulong to long 2048 if or ge cur 64 gt res 64 2049 ret neg long 7 2050 end 2051 if gt res 0 2052 if ne get sjst sub res 1 2 2053 ret neg long 7 2054 end 2055 end 2056 put scpol 4 cur 2057 put scst 2 ev 2058 put scpol 5 res 2059 ret 0 2060 end 2061 2062 note Journal, recovery: the bypass count of ready job j. 0 or -3. 2063 fn sched_setbyp j ulong n ulong to long 2064 if ge j 64 2065 ret neg long 3 2066 end 2067 if ne get sjst j 2 2068 ret neg long 3 2069 end 2070 put sjbyp j n 2071 ret 0 2072 end 2073 2074 note Journal, recovery: waiting job j was made ready by a publication 2075 note before the restart. 0 or -3 (j not waiting). 2076 fn sched_setready j ulong to long 2077 if ge j 64 2078 ret neg long 3 2079 end 2080 if ne get sjst j 1 2081 ret neg long 3 2082 end 2083 sp_ready j 2084 ret 0 2085 end 2086 2087 note Creator: move finished jobs into review while review has room. A worker 2088 note whose result is waiting for room keeps it and gets no new job, so a 2089 note full review queue stops assignment instead of dropping work. A job 2090 note gives back its resources as soon as its worker reported done, even 2091 note while its result waits for room. 2092 fn sched_collect c long to ulong 2093 n = mapget c 48 8 2094 k = ulong 0 2095 for w ulong n bound 64 2096 b = get scbusy w 2097 if gt b 0 2098 if eq sc_cell w 64 get scseq w 2099 j = sub b 1 2100 sp_give j 2101 if ge sched_count 4 4 get sclim 1 2102 continue 2103 end 2104 put sjst j 4 2105 put sjout j sc_cell w 72 2106 put scbusy w 0 2107 k = add k 1 2108 end 2109 end 2110 end 2111 ret k 2112 end 2113 2114 note Creator, after worker w was reaped: retire its unfinished assignment. 2115 note A rerunnable job is ready again; any other becomes UNKNOWN (6), since 2116 note it may have had effects. A finished result is left for sched_collect. 2117 note Returns 1 when an assignment was retired, 0 when none, -16 still live. 2118 fn sched_lost c long w ulong to long 2119 if ge w 64 2120 ret neg long 7 2121 end 2122 if gt get swpid w 0 2123 ret neg long 16 2124 end 2125 b = get scbusy w 2126 if eq b 0 2127 ret 0 2128 end 2129 if eq sc_cell w 64 get scseq w 2130 ret 0 2131 end 2132 j = sub b 1 2133 sc_set w 64 get scseq w 2134 put scbusy w 0 2135 sp_give j 2136 if ne and get sjfl j 1 0 2137 sp_ready j 2138 else 2139 put sjst j 6 2140 end 2141 ret 1 2142 end 2143 2144 note Creator loop step: wait up to interval ns for an ack or a death, take 2145 note acks, retire the jobs of reaped workers, collect, then dispatch. 2146 note Returns the number of workers reaped, or a negative errno. 2147 fn sched_step c long interval long to long 2148 d = sup_poll c interval 2149 if lt d 0 2150 ret d 2151 end 2152 n = mapget c 48 8 2153 for w ulong n bound 64 2154 if gt get swpid w 0 2155 r = sup_take get swack w 2156 else 2157 r = sched_lost c w 2158 end 2159 end 2160 k = sched_collect c 2161 k = sched_dispatch c 2162 ret d 2163 end 2164 2165 note Creator: publish an artifact, make waiting jobs whose deps meet topics 2166 note ready, wake each live worker whose interest meets topics, dispatch. 2167 note With a journal attached the jobs made ready leave a readiness record 2168 note in the replay ring; when the ring is full nothing is published, -105 2169 note (counter 15). Returns the version, or the publish refusal with nothing 2170 note woken. 2171 fn sched_publish c long topics ulong src view uchar 4096 n ulong to long 2172 if and ne get sfj 0 0 ge get sfj 1 64 2173 put scst 15 add get scst 15 1 2174 ret neg long 105 2175 end 2176 v = pool_publish c topics src n 2177 if le v 0 2178 ret v 2179 end 2180 rd = ulong 0 2181 for j ulong 64 2182 if eq get sjst j 1 2183 if ne and get sjdep j topics 0 2184 sp_ready j 2185 rd = or rd shl 1 j 2186 end 2187 end 2188 end 2189 if and ne rd 0 ne get sfj 0 0 2190 sp_frpush 0 0 rd 2191 end 2192 m = mapget c 48 8 2193 for w ulong m bound 64 2194 if gt get swpid w 0 2195 if ne and sc_cell w 32 topics 0 2196 sc_wake w 2197 end 2198 end 2199 end 2200 k = sched_dispatch c 2201 ret v 2202 end 2203 2204 note Creator: accept the result of job j in review and free its slot. 2205 note Returns its outcome, or -3 when j is not in review. 2206 fn sched_review j ulong to long 2207 if ge j 64 2208 ret neg long 3 2209 end 2210 if ne get sjst j 4 2211 ret neg long 3 2212 end 2213 put sjst j 0 2214 ret long get sjout j 2215 end 2216 2217 note Creator: free an UNKNOWN job once its outcome has been settled 2218 note elsewhere. Returns 0, or -3 when j is not UNKNOWN. 2219 fn sched_settle j ulong to long 2220 if ge j 64 2221 ret neg long 3 2222 end 2223 if ne get sjst j 6 2224 ret neg long 3 2225 end 2226 put sjst j 0 2227 ret 0 2228 end 2229 2230 note Creator: while paused (on true) no job is assigned; running jobs go on 2231 note and finished results are still collected. Resuming dispatches at once. 2232 note Returns the number assigned on resume, else 0. 2233 fn sched_pause c long on bool to ulong 2234 if on 2235 put scpause 0 1 2236 ret 0 2237 end 2238 put scpause 0 0 2239 ret sched_dispatch c 2240 end 2241 2242 note Creator: cancel job j. A waiting or ready job never started, so it is 2243 note freed with no effect: 1. A running job needs its worker: 2, and the 2244 note outcome comes from the worker. A job in review already finished: 3, 2245 note nothing is cancelled. UNKNOWN stays UNKNOWN: 4. -3 free or bad slot. 2246 fn sched_cancel j ulong to long 2247 if ge j 64 2248 ret neg long 3 2249 end 2250 s = get sjst j 2251 if or eq s 1 eq s 2 2252 put sjst j 0 2253 sp_bump 2254 if eq get scpol 5 add j 1 2255 put scpol 5 0 2256 end 2257 ret 1 2258 end 2259 if eq s 3 2260 ret 2 2261 end 2262 if eq s 4 2263 ret 3 2264 end 2265 if eq s 6 2266 ret 4 2267 end 2268 ret neg long 3 2269 end 2270 2271 note Creator: stop every worker within interval ns (sup_stop), retire the 2272 note assignments of the reaped workers, collect finished results. 2273 note Returns how many workers had to be killed, or a refusal. 2274 fn sched_stop c long interval long to long 2275 k = sup_stop c interval 2276 if lt k 0 2277 ret k 2278 end 2279 n = mapget c 48 8 2280 for w ulong n bound 64 2281 r = sched_lost c w 2282 end 2283 m = sched_collect c 2284 ret k 2285 end 2286 2287 note Worker w: the assignment seq when a job is waiting, else 0. Job id, 2288 note input epoch and input version are words 8, 16 and 24 of its cell. 2289 fn sched_wjob w ulong to ulong 2290 q = sc_cell w 0 2291 if gt q sc_cell w 64 2292 ret q 2293 end 2294 ret 0 2295 end 2296 2297 note Worker w: finish assignment q with outcome and the first n (at most 2298 note 4096) bytes of src. Bytes and outcome are written before the done seq; 2299 note then the creator gets an ack. 0, -7 bad n, or the ack refusal. 2300 fn sched_wdone w ulong q ulong outcome ulong src view uchar 4096 n ulong to long 2301 if gt n 4096 2302 ret neg long 7 2303 end 2304 if gt n 0 2305 r = mapwrite get scmap 1 mul w 4096 src 0 n 2306 end 2307 sc_set w 72 outcome 2308 sc_set w 64 q 2309 ret sup_ack w 2310 end 2311 2312 note Creator: copy worker w's last result bytes into dst. 2313 fn sched_result w ulong dst edit uchar 4096 to long 2314 ret mapread get scmap 1 mul w 4096 dst 0 4096 2315 end 2316 2317 note Worker w: sup_wwait that records its wakes and wake latency. 2318 fn sched_wwait c long w ulong interval long to long 2319 r = sup_wwait c w interval 2320 if eq r 1 2321 now = ulong monons 2322 st = sc_cell w 40 2323 lat = ulong 0 2324 if gt now st 2325 lat = sub now st 2326 end 2327 sc_set w 80 add sc_cell w 80 1 2328 sc_set w 88 lat 2329 if gt lat sc_cell w 96 2330 sc_set w 96 lat 2331 end 2332 end 2333 ret r 2334 end 2335 2336 note Worker w: count the versions of the current epoch that meet its 2337 note interest and are newer than its cursor, moving the cursor past them. 2338 note A new epoch restarts the cursor at 0. A worker with no interest scans 2339 note nothing. Returns the count or a refusal. 2340 fn sched_wscan c long w ulong to long 2341 t = sc_cell w 32 2342 if eq t 0 2343 ret 0 2344 end 2345 h = pool_acquire c w 2346 if le h 0 2347 ret h 2348 end 2349 e = mapget c add 256 mul w 8 8 2350 if ne get swcur 0 e 2351 put swcur 0 e 2352 put swcur 1 0 2353 end 2354 k = long 0 2355 for i ulong 65 2356 v = node_next h get swcur 1 t 2357 if lt v 0 2358 r = pool_release c w 2359 ret v 2360 end 2361 if eq v 0 2362 break 2363 end 2364 put swcur 1 ulong v 2365 k = add k 1 2366 end 2367 r = pool_release c w 2368 ret k 2369 end 2370 note Omega Node scheduling comparison (DRA-399). Concatenate after 2371 note transport.om, pool.om, super.om and sched.om. Runs pinned workloads 2372 note under arm 0 (FIFO), 1 (fair fitting), 2 (goal-aware, declared estimates 2373 note within a factor of 2 of the duration) and 3 (goal-aware given the true 2374 note durations: ORACLE, an upper bound, not a policy result) with real 2375 note forked workers that sleep for the milliseconds in the low four decimal 2376 note digits of their job id, on the host's wall clock. Usage: bench-gate 2377 note (five repetitions of every scenario and arm). Prints one line per run: 2378 note bench S P R mk_us idle_us dsp_ns dsp_n ev cand maint vread rework res cont ok hwm_kb : waits_us... 2379 note S scenario 1..6 (blocked head, long chain, fan-out, sustained arrivals, 2380 note queue pressure, worker failure), P arm (0..3), R repetition; mk makespan 2381 note (first admission to last review); idle the integral of min(idle live 2382 note workers, ready jobs that fit the free capacity) over time, in 2383 note worker-microseconds; dsp time inside sched_dispatch from the creator 2384 note loop and its calls; ev, cand, maint, vread the scheduler's counters 2385 note (events, candidate reads, maintenance reads, value reads); rework the 2386 note jobs that went back to ready after starting; res the reservations made 2387 note (sched_stat 6); cont the new reservations seen while another ready job 2388 note was also at the bypass budget (where check 71 can tell oldest from any); 2389 note ok the jobs reviewed with 2390 note the expected outcome; hwm_kb the creator's VmHWM; waits from ready to 2391 note started, per job, in microseconds (sampled after every creator step 2392 note and every publication). Every policy sees the same jobs, requests, 2393 note durations, declarations, workers and capacities. Checked in every run: 2394 note the capacity invariant (exit 70), a new reservation goes to the oldest 2395 note ready job at the bypass budget (71), every job is reviewed once with 2396 note its expected outcome before the deadline (72), setup (73..79). 2397 text bnok "PASS node bench\n" 2398 text bnhd "bench " 2399 text bnsp " " 2400 text bnco " :" 2401 text bnnl "\n" 2402 text bnst "/proc/self/status" 2403 text bnhw "VmHWM:" 2404 global bnc long 1 2405 note Run metrics: 0 t0, 1 last review, 2 idle sum ns, 3 last sample, 4 2406 note dispatch ns, 5 dispatch calls, 6 reviewed, 7 verified, 8 reworks, 9 2407 note jobs in the scenario, 10 next queue entry, 11 killed, 12 reservation 2408 note last seen + 1, 13 idle-eligible workers at the last sample, 14 policy, 2409 note 15 bypass budget, 16 contested reservations. 2410 global bnm ulong 20 2411 note Per slot: ready since (ns, 0 not ready), started flag. 2412 global bnr ulong 64 2413 global bns ulong 64 2414 note Waits of this run (us) and their count. 2415 global bnwt ulong 256 2416 global bnwn ulong 1 2417 note Scenario queue: id, deps, request, flags, arrival ms, topics published. 2418 global bnqi ulong 64 2419 global bnqd ulong 64 2420 global bnqr ulong 64 2421 global bnqf ulong 64 2422 global bnqt ulong 64 2423 global bnqp ulong 64 2424 global bnqn ulong 1 2425 note 1: arm 3, policy 2 given the true durations as estimates (ORACLE). 2426 global bne ulong 1 2427 2428 fn bn_ok ok bool code int to void 2429 if not ok 2430 exit code 2431 end 2432 end 2433 2434 fn bn_num v ulong to void 2435 d = new uchar 20 0 2436 x = v 2437 k = ulong 20 2438 for i ulong 20 2439 k = sub k 1 2440 put d k uchar add rem x 10 48 2441 x = div x 10 2442 if eq x 0 2443 break 2444 end 2445 end 2446 r = write 1 d k sub 20 k 2447 end 2448 2449 fn bn_sp v ulong to void 2450 r = write 1 bnsp 0 1 2451 bn_num v 2452 end 2453 2454 fn bn_req cpu ulong to ulong 2455 ret cpu 2456 end 2457 2458 fn bn_run c long w ulong q ulong to int 2459 buf = new uchar 4096 0 2460 id = sc_cell w 8 2461 dl = add ulong monons mul rem id 10000 `1000000` 2462 for t ulong `1000000` 2463 now = ulong monons 2464 if ge now dl 2465 break 2466 end 2467 r = sched_wwait c w long sub dl now 2468 if eq r 2 2469 ret 2 2470 end 2471 if eq r 3 2472 ret 3 2473 end 2474 if lt r 0 2475 ret 4 2476 end 2477 end 2478 if ne sched_wdone w q add id 1 buf 8 0 2479 ret 5 2480 end 2481 ret 0 2482 end 2483 2484 fn bn_worker w ulong to int 2485 c = get bnc 0 2486 for it ulong `1000000000` 2487 q = sched_wjob w 2488 if gt q 0 2489 r = bn_run c w q 2490 if eq r 2 2491 ret 0 2492 end 2493 if ne r 0 2494 ret add 60 r 2495 end 2496 continue 2497 end 2498 r = sched_wwait c w neg long 1 2499 if eq r 2 2500 ret 0 2501 end 2502 if eq r 3 2503 ret 41 2504 end 2505 if lt r 0 2506 ret 43 2507 end 2508 end 2509 ret 45 2510 end 2511 2512 note Queue entry: job id (duration in its low four digits), deps, CPU, 2513 note flags, arrival ms after the start, topics its completion publishes. 2514 fn bn_q id ulong deps ulong cpu ulong fl ulong at ulong pro ulong to void 2515 n = get bnqn 0 2516 put bnqi n id 2517 put bnqd n deps 2518 put bnqr n cpu 2519 put bnqf n fl 2520 put bnqt n at 2521 put bnqp n pro 2522 put bnqn 0 add n 1 2523 end 2524 2525 note Declared work estimate of job id in ms. Policy 2 is not told the 2526 note duration: the estimate is the duration times 1/2, 1, 3/2 or 2, chosen 2527 note by a multiplicative hash of the id (fixed, so every run and repetition 2528 note declares the same values). Arm 3 (ORACLE) declares the true duration. 2529 fn bn_est id ulong to ulong 2530 d = rem id 10000 2531 if ne get bne 0 0 2532 ret d 2533 end 2534 k = rem div mul id `2654435761` 65536 4 2535 ret div mul d add k 1 2 2536 end 2537 2538 note Scenario s into the queue; returns (workers, admission limit). 2539 fn bn_load s ulong to ulong 2540 put bnqn 0 0 2541 if eq s 1 2542 bn_q 10600 0 2 0 0 0 2543 bn_q 20300 0 4 0 0 0 2544 for i ulong 6 2545 bn_q add 30200 mul i 10000 0 1 0 0 0 2546 end 2547 ret 64 2548 end 2549 if eq s 2 2550 for i ulong 8 2551 bn_q add 100300 mul i 10000 0 1 0 0 0 2552 end 2553 bn_q 200150 0 1 0 0 2 2554 for i ulong 5 2555 bn_q add 210150 mul i 10000 shl ulong 2 i 1 0 0 shl ulong 4 i 2556 end 2557 ret 64 2558 end 2559 if eq s 3 2560 for i ulong 6 2561 bn_q add 300300 mul i 10000 0 1 0 0 0 2562 end 2563 bn_q 400100 0 1 0 0 1024 2564 for i ulong 6 2565 bn_q add 410200 mul i 10000 1024 1 0 0 0 2566 end 2567 ret 64 2568 end 2569 if eq s 4 2570 bn_s4 2571 ret 64 2572 end 2573 if eq s 5 2574 bn_s5 2575 ret 8 2576 end 2577 bn_s6 2578 ret 64 2579 end 2580 2581 note Sustained arrivals: 24 jobs of 100..400 ms needing 1..3 CPU, four 2582 note every 150 ms. 2583 fn bn_s4 to void 2584 for i ulong 24 2585 d = add 100 mul rem mul i 7 4 100 2586 id = add add 500000 mul i 10000 d 2587 at = mul div i 4 150 2588 bn_q id 0 add 1 rem i 3 0 at 0 2589 end 2590 end 2591 2592 note Queue pressure: 30 jobs of 100..300 ms needing 1..2 CPU offered at 2593 note once against an admission limit of 8. 2594 fn bn_s5 to void 2595 for i ulong 30 2596 d = add 100 mul rem mul i 5 3 100 2597 id = add add 600000 mul i 10000 d 2598 bn_q id 0 add 1 rem i 2 0 0 0 2599 end 2600 end 2601 2602 note Worker failure: 12 rerunnable jobs of 150..450 ms needing 1..3 CPU; 2603 note worker 0 is killed 250 ms in while busy. 2604 fn bn_s6 to void 2605 for i ulong 12 2606 d = add 150 mul rem mul i 7 4 100 2607 id = add add 700000 mul i 10000 d 2608 bn_q id 0 add 1 rem i 3 1 0 0 2609 end 2610 end 2611 2612 note Admit the queue entries that have arrived, in order; a refusal for 2613 note room (-2, a ticket) stops until the next call. 2614 fn bn_admit now ulong to void 2615 for k ulong 64 2616 i = get bnm 10 2617 if ge i get bnqn 0 2618 ret 2619 end 2620 if lt sub now get bnm 0 mul get bnqt i `1000000` 2621 ret 2622 end 2623 j = sched_admitr get bnqi i get bnqd i get bnqf i bn_req get bnqr i 2624 if eq j neg long 2 2625 ret 2626 end 2627 bn_ok ge j 0 74 2628 e = bn_est get bnqi i 2629 r = sched_goal ulong j get bnqp i e e 2630 put bnm 10 add i 1 2631 end 2632 end 2633 2634 note Topics job id publishes on completion. 2635 fn bn_pro id ulong to ulong 2636 for i ulong get bnqn 0 bound 64 2637 if eq get bnqi i id 2638 ret get bnqp i 2639 end 2640 end 2641 ret 0 2642 end 2643 2644 fn bn_inv to void 2645 for k ulong 4 2646 s = ulong 0 2647 for j ulong 64 2648 if eq get sjheld j 1 2649 s = add s sp_need get sjneed j k 2650 end 2651 end 2652 bn_ok eq get suse k s 70 2653 bn_ok le get suse k get scap k 70 2654 end 2655 end 2656 2657 note A reservation that appeared since the last sample goes to the oldest 2658 note ready job at the bypass budget: no older ready job is at the budget. 2659 note Another ready job at the budget makes it contested (bnm 16). 2660 fn bn_fair to void 2661 r = sched_stat 29 2662 if or eq r 0 eq r get bnm 12 2663 put bnm 12 r 2664 ret 2665 end 2666 put bnm 12 r 2667 a = get sjarr sub r 1 2668 n = ulong 0 2669 for j ulong 64 2670 if and eq get sjst j 2 ne j sub r 1 2671 if ge get sjbyp j get bnm 15 2672 n = add n 1 2673 end 2674 if lt get sjarr j a 2675 bn_ok lt get sjbyp j get bnm 15 71 2676 end 2677 end 2678 end 2679 if gt n 0 2680 put bnm 16 add get bnm 16 1 2681 end 2682 end 2683 2684 note Sample after a creator step or publication: waits, reworks, the idle 2685 note eligible integral, the invariants. 2686 fn bn_sample to void 2687 now = ulong monons 2688 put bnm 2 add get bnm 2 mul get bnm 13 sub now get bnm 3 2689 put bnm 3 now 2690 nf = ulong 0 2691 for j ulong 64 2692 s = get sjst j 2693 if eq s 2 2694 if ne get bns j 0 2695 put bns j 0 2696 put bnm 8 add get bnm 8 1 2697 end 2698 if eq get bnr j 0 2699 put bnr j now 2700 end 2701 if sp_fits get sjneed j 0 2702 nf = add nf 1 2703 end 2704 end 2705 if and ge s 3 ne get bnr j 0 2706 bn_wait sub now get bnr j 2707 put bnr j 0 2708 put bns j 1 2709 end 2710 end 2711 put bnm 13 bn_min nf bn_idle 2712 bn_inv 2713 if ge get bnm 14 1 2714 bn_fair 2715 end 2716 end 2717 2718 fn bn_wait ns ulong to void 2719 n = get bnwn 0 2720 if lt n 256 2721 put bnwt n div ns 1000 2722 put bnwn 0 add n 1 2723 end 2724 end 2725 2726 fn bn_min a ulong b ulong to ulong 2727 if lt a b 2728 ret a 2729 end 2730 ret b 2731 end 2732 2733 fn bn_idle to ulong 2734 c = get bnc 0 2735 n = mapget c 48 8 2736 k = ulong 0 2737 for w ulong n bound 64 2738 if and gt get swpid w 0 eq get scbusy w 0 2739 k = add k 1 2740 end 2741 end 2742 ret k 2743 end 2744 2745 note sched_step with the dispatch timed. 2746 fn bn_step c long to void 2747 d = sup_poll c `1000000` 2748 bn_ok ge d 0 75 2749 n = mapget c 48 8 2750 for w ulong n bound 64 2751 if gt get swpid w 0 2752 r = sup_take get swack w 2753 else 2754 r = sched_lost c w 2755 end 2756 end 2757 k = sched_collect c 2758 t = ulong monons 2759 k = sched_dispatch c 2760 put bnm 4 add get bnm 4 sub ulong monons t 2761 put bnm 5 add get bnm 5 1 2762 end 2763 2764 note Review finished jobs (outcome id + 1 is verified work); publish what 2765 note each declared. 2766 fn bn_review c long to void 2767 buf = new uchar 4096 0 2768 for j ulong 64 2769 if eq get sjst j 4 2770 id = get sjid j 2771 o = sched_review j 2772 if eq o long add id 1 2773 put bnm 7 add get bnm 7 1 2774 end 2775 put bnm 6 add get bnm 6 1 2776 put bnm 1 ulong monons 2777 put bns j 0 2778 put bnr j 0 2779 p = bn_pro id 2780 if ne p 0 2781 v = sched_publish c p buf 8 2782 bn_ok gt v 0 76 2783 bn_sample 2784 end 2785 end 2786 end 2787 end 2788 2789 fn bn_hwm to ulong 2790 b = new uchar 4096 0 2791 fd = fopen bnst 0 len bnst 0 0 2792 if lt fd 0 2793 ret 0 2794 end 2795 r = pread int fd b 0 4096 long 0 2796 x = fclose int fd 2797 if le r 0 2798 ret 0 2799 end 2800 n = ulong r 2801 h = len bnhw 2802 for i ulong 4000 2803 if gt add i h n 2804 ret 0 2805 end 2806 m = true 2807 for k ulong h bound 8 2808 if ne get b add i k get bnhw k 2809 m = false 2810 end 2811 end 2812 if m 2813 ret bn_digits b add i h n 2814 end 2815 end 2816 ret 0 2817 end 2818 2819 fn bn_digits b view uchar 4096 i ulong n ulong to ulong 2820 v = ulong 0 2821 for k ulong 32 2822 p = add i k 2823 if ge p n 2824 ret v 2825 end 2826 ch = get b p 2827 if and ge ch 48 le ch 57 2828 v = add mul v 10 sub ulong ch 48 2829 else 2830 if gt v 0 2831 ret v 2832 end 2833 end 2834 end 2835 ret v 2836 end 2837 2838 fn bn_reset pol ulong by ulong to void 2839 for i ulong 20 2840 put bnm i 0 2841 end 2842 for j ulong 64 2843 put bnr j 0 2844 put bns j 0 2845 end 2846 put bnwn 0 0 2847 put bnm 14 pol 2848 put bnm 15 by 2849 end 2850 2851 note One run of scenario s under arm a (policy 0, 1, 2; arm 3 is policy 2 2852 note with ORACLE estimates); prints its line. 2853 fn bn_one s ulong a ulong rep ulong to void 2854 pol = a 2855 put bne 0 0 2856 if eq a 3 2857 pol = 2 2858 put bne 0 1 2859 end 2860 lim = bn_load s 2861 c = pool_new 4 2 2862 bn_ok gt c 0 73 2863 put bnc 0 c 2864 bn_ok eq sup_init c 0 73 2865 bn_ok eq sched_new c lim 64 0 73 2866 bn_ok eq sched_cap bn_req 4 0 73 2867 bn_ok eq sched_policy pol 64 64 4 1000 0 73 2868 bn_reset a 4 2869 put bnm 9 get bnqn 0 2870 for w ulong 2 2871 p = sup_spawn c w 2872 if eq p 0 2873 exit bn_worker w 2874 end 2875 bn_ok gt p 0 73 2876 end 2877 k = sched_pause c true 2878 put bnm 0 ulong monons 2879 put bnm 3 get bnm 0 2880 bn_admit get bnm 0 2881 bn_sample 2882 k = sched_pause c false 2883 bn_loop c s 2884 bn_ok eq get bnm 6 get bnm 9 72 2885 bn_ok eq get bnm 7 get bnm 9 72 2886 bn_print s a rep 2887 bn_ok ge sched_stop c `2000000000` 0 77 2888 sup_closeall 2889 bn_ok eq sched_free 0 77 2890 bn_ok eq pool_free c 0 77 2891 end 2892 2893 fn bn_loop c long s ulong to void 2894 dl = add ulong monons `20000000000` 2895 for t ulong `100000000` 2896 bn_sample 2897 if ge get bnm 6 get bnm 9 2898 ret 2899 end 2900 bn_ok lt ulong monons dl 72 2901 if and eq s 6 eq get bnm 11 0 2902 if ge sub ulong monons get bnm 0 `250000000` 2903 bn_kill c 2904 end 2905 end 2906 bn_admit ulong monons 2907 bn_step c 2908 bn_sample 2909 bn_review c 2910 end 2911 end 2912 2913 note Scenario 6: SIGKILL the busy worker 0 once. 2914 fn bn_kill c long to void 2915 if eq get scbusy 0 0 2916 ret 2917 end 2918 r = killpid int get swpid 0 9 2919 put bnm 11 1 2920 end 2921 2922 fn bn_print s ulong pol ulong rep ulong to void 2923 r = write 1 bnhd 0 len bnhd 2924 bn_num s 2925 bn_sp pol 2926 bn_sp rep 2927 bn_sp div sub get bnm 1 get bnm 0 1000 2928 bn_sp div get bnm 2 1000 2929 bn_sp get bnm 4 2930 bn_sp get bnm 5 2931 bn_sp sched_stat 2 2932 bn_sp sched_stat 3 2933 bn_sp sched_stat 13 2934 bn_sp sched_stat 38 2935 bn_sp get bnm 8 2936 bn_sp sched_stat 6 2937 bn_sp get bnm 16 2938 bn_sp get bnm 7 2939 bn_sp bn_hwm 2940 r = write 1 bnco 0 len bnco 2941 for i ulong get bnwn 0 bound 256 2942 bn_sp get bnwt i 2943 end 2944 r = write 1 bnnl 0 1 2945 end 2946 2947 fn bn_reps to ulong 2948 ret 5 2949 end 2950 2951 fn main to int 2952 n = bn_reps 2953 for rep ulong n bound 100 2954 for s ulong 6 2955 for pol ulong 4 2956 bn_one add s 1 pol rep 2957 end 2958 end 2959 end 2960 if ne write 1 bnok 0 len bnok long len bnok 2961 ret 100 2962 end 2963 ret 0 2964 end