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
omega-mv / seed / node / schedpol-gate.om
| 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 Concatenate after transport.om, pool.om, super.om and sched.om. CPU-only |
| 2371 | note acceptance of the resource-aware selection policy (P-DRA-14) with real |
| 2372 | note forked workers: impossible requests, the capacity invariant under churn, |
| 2373 | note ready order, the head-of-line scenarios of the reference pump |
| 2374 | note (aien-sovereign-core-mv spark-harness scheduler/core.rs Core::pump), |
| 2375 | note starvation under repeated small arrivals, admission tickets, lease |
| 2376 | note expiry and cancellation of a reserved job. |
| 2377 | note After every creator step the test checks that no kind is used beyond its |
| 2378 | note capacity and that the use equals the needs of the jobs holding them. |
| 2379 | note Argument "fifo" runs policy 0, "noreserve" policy 1 with the bypass |
| 2380 | note budget out of reach: the negative controls. No GPU, model or network. |
| 2381 | text pok "PASS node schedpol\n" |
| 2382 | text pst "schedpol events=" |
| 2383 | text pex " examined=" |
| 2384 | text pas " assigned=" |
| 2385 | text phol " hol_stops=" |
| 2386 | text pbyp " bypasses=" |
| 2387 | text pres " reservations=" |
| 2388 | text pnl "\n" |
| 2389 | text ptr "schedpol goal trace " |
| 2390 | text psp " " |
| 2391 | text pw1 "schedpol hol witness head=" |
| 2392 | text pw2 " fits_behind=" |
| 2393 | text pw3 " free_cpu=" |
| 2394 | text pw4 " idle_worker=" |
| 2395 | global ptc long 1 |
| 2396 | note Mode: 0 default policy, 1 fifo, 2 noreserve, 3 goal (policy 2). |
| 2397 | global ptm ulong 1 |
| 2398 | note Step-bound snapshot (pt_snap): event count, candidate reads, |
| 2399 | note maintenance reads, assigned, cursor, window scans. |
| 2400 | global ptb ulong 6 |
| 2401 | note Phase G trace: ids in review order. |
| 2402 | global ptt ulong 16 |
| 2403 | global ptn ulong 1 |
| 2404 | note Value-read bound baseline: stats 38, 32, 33 at the last check. |
| 2405 | global ptv ulong 3 |
| 2406 | |
| 2407 | fn pt_ok ok bool code int to void |
| 2408 | if not ok |
| 2409 | exit code |
| 2410 | end |
| 2411 | end |
| 2412 | |
| 2413 | fn pt_num v ulong to void |
| 2414 | d = new uchar 20 0 |
| 2415 | x = v |
| 2416 | k = ulong 20 |
| 2417 | for i ulong 20 |
| 2418 | k = sub k 1 |
| 2419 | put d k uchar add rem x 10 48 |
| 2420 | x = div x 10 |
| 2421 | if eq x 0 |
| 2422 | break |
| 2423 | end |
| 2424 | end |
| 2425 | r = write 1 d k sub 20 k |
| 2426 | end |
| 2427 | |
| 2428 | note Packed request: cpu slots, memory MiB, inference slots, kind 3. |
| 2429 | fn pt_req cpu ulong mem ulong inf ulong x ulong to ulong |
| 2430 | ret or or cpu shl mem 16 or shl inf 32 shl x 48 |
| 2431 | end |
| 2432 | |
| 2433 | note Worker: run assignment q for the milliseconds in the low four decimal |
| 2434 | note digits of its job id, then report outcome id+1 and 8 bytes. 0 done, |
| 2435 | note 2 stopped, 3 creator gone, 4 wait error, 5 report refused. |
| 2436 | fn pt_run c long w ulong q ulong to int |
| 2437 | buf = new uchar 4096 0 |
| 2438 | id = sc_cell w 8 |
| 2439 | dl = add ulong monons mul rem id 10000 1000000 |
| 2440 | for t ulong 1000000 |
| 2441 | now = ulong monons |
| 2442 | if ge now dl |
| 2443 | break |
| 2444 | end |
| 2445 | r = sched_wwait c w long sub dl now |
| 2446 | if eq r 2 |
| 2447 | ret 2 |
| 2448 | end |
| 2449 | if eq r 3 |
| 2450 | ret 3 |
| 2451 | end |
| 2452 | if lt r 0 |
| 2453 | ret 4 |
| 2454 | end |
| 2455 | end |
| 2456 | if ne sched_wdone w q add id 1 buf 8 0 |
| 2457 | ret 5 |
| 2458 | end |
| 2459 | ret 0 |
| 2460 | end |
| 2461 | |
| 2462 | fn pt_worker w ulong to int |
| 2463 | c = get ptc 0 |
| 2464 | for it ulong 1000000000 |
| 2465 | q = sched_wjob w |
| 2466 | if gt q 0 |
| 2467 | r = pt_run c w q |
| 2468 | if eq r 2 |
| 2469 | ret 0 |
| 2470 | end |
| 2471 | if ne r 0 |
| 2472 | ret add 60 r |
| 2473 | end |
| 2474 | continue |
| 2475 | end |
| 2476 | r = sched_wwait c w neg long 1 |
| 2477 | if eq r 2 |
| 2478 | ret 0 |
| 2479 | end |
| 2480 | if eq r 3 |
| 2481 | ret 41 |
| 2482 | end |
| 2483 | if lt r 0 |
| 2484 | ret 43 |
| 2485 | end |
| 2486 | end |
| 2487 | ret 45 |
| 2488 | end |
| 2489 | |
| 2490 | fn pt_spawn c long w ulong to bool |
| 2491 | p = sup_spawn c w |
| 2492 | if eq p 0 |
| 2493 | exit pt_worker w |
| 2494 | end |
| 2495 | ret gt p 0 |
| 2496 | end |
| 2497 | |
| 2498 | note Fresh pool for w workers (spawned), admission limit a, review limit rl, |
| 2499 | note capacities caps. Exits with code on any refusal. |
| 2500 | fn pt_setup w ulong a ulong rl ulong caps ulong code int to long |
| 2501 | c = pool_new 4 w |
| 2502 | pt_ok gt c 0 code |
| 2503 | put ptc 0 c |
| 2504 | pt_ok eq sup_init c 0 code |
| 2505 | pt_ok eq sched_new c a rl 0 code |
| 2506 | pt_ok eq sched_cap caps 0 code |
| 2507 | put ptv 0 sched_stat 38 |
| 2508 | put ptv 1 sched_stat 32 |
| 2509 | put ptv 2 sched_stat 33 |
| 2510 | if eq get ptm 0 1 |
| 2511 | pt_ok eq sched_policy 0 64 64 4 1000 0 code |
| 2512 | end |
| 2513 | if eq get ptm 0 2 |
| 2514 | pt_ok eq sched_policy 1 64 64 65535 1000 0 code |
| 2515 | end |
| 2516 | if eq get ptm 0 3 |
| 2517 | pt_ok eq sched_policy 2 64 64 4 1000 0 code |
| 2518 | end |
| 2519 | for i ulong w bound 64 |
| 2520 | pt_ok pt_spawn c i code |
| 2521 | end |
| 2522 | ret c |
| 2523 | end |
| 2524 | |
| 2525 | fn pt_down c long code int to void |
| 2526 | pt_ok ge sched_stop c 2000000000 0 code |
| 2527 | sup_closeall |
| 2528 | pt_ok eq sched_free 0 code |
| 2529 | pt_ok eq pool_free c 0 code |
| 2530 | end |
| 2531 | |
| 2532 | note The capacity invariant: of each kind the use is at most the capacity |
| 2533 | note and equals the sum of the needs of the jobs that hold resources; only |
| 2534 | note a running job holds them, and every running job whose worker has not |
| 2535 | note reported done holds them. |
| 2536 | fn pt_inv code int to void |
| 2537 | for j ulong 64 |
| 2538 | if eq get sjst j 3 |
| 2539 | w = get sjw j |
| 2540 | if ne sc_cell w 64 get scseq w |
| 2541 | pt_ok eq get sjheld j 1 code |
| 2542 | end |
| 2543 | end |
| 2544 | end |
| 2545 | for k ulong 4 |
| 2546 | s = ulong 0 |
| 2547 | for j ulong 64 |
| 2548 | if eq get sjheld j 1 |
| 2549 | pt_ok eq get sjst j 3 code |
| 2550 | s = add s sp_need get sjneed j k |
| 2551 | end |
| 2552 | end |
| 2553 | pt_ok eq get suse k s code |
| 2554 | pt_ok le get suse k get scap k code |
| 2555 | end |
| 2556 | end |
| 2557 | |
| 2558 | note One creator step of up to 10 ms, then the invariant and the scan |
| 2559 | note bounds since the last step. An event makes at most one pick more than |
| 2560 | note it assigns, so candidate reads are at most (assigned + events) x |
| 2561 | note (window + 1), the reserved probe included, plus one proposal probe per |
| 2562 | note pick under policy 2, or x 64 under FIFO (policy 0), 9; maintenance reads |
| 2563 | note at most 64 per event, 14; the cursor moved by exactly the window per |
| 2564 | note window scan, mod 64, 15 (the traversal witness: window scans cover |
| 2565 | note consecutive windows, so ceil(64/win) of them examine every slot); value |
| 2566 | note reads at most 64 per rescoring plus 128 per job scored, 8. |
| 2567 | fn pt_step c long code int to void |
| 2568 | pt_snap |
| 2569 | r = sched_step c 10000000 |
| 2570 | pt_ok ge r 0 code |
| 2571 | pt_inv code |
| 2572 | pt_check |
| 2573 | end |
| 2574 | |
| 2575 | note Snapshot the counters the step bounds compare against (ptb). |
| 2576 | fn pt_snap to void |
| 2577 | put ptb 0 sched_stat 2 |
| 2578 | put ptb 1 sched_stat 3 |
| 2579 | put ptb 2 sched_stat 13 |
| 2580 | put ptb 3 sched_stat 4 |
| 2581 | put ptb 4 sched_stat 28 |
| 2582 | put ptb 5 sched_stat 14 |
| 2583 | end |
| 2584 | |
| 2585 | note The bounds since pt_snap (exits 9, 14, 15, 8). |
| 2586 | fn pt_check to void |
| 2587 | w = add sched_stat 25 1 |
| 2588 | if eq sched_stat 24 0 |
| 2589 | w = ulong 64 |
| 2590 | end |
| 2591 | if eq sched_stat 24 2 |
| 2592 | w = add w 1 |
| 2593 | end |
| 2594 | ev = sub sched_stat 2 get ptb 0 |
| 2595 | lim = mul add sub sched_stat 4 get ptb 3 ev w |
| 2596 | pt_ok le sub sched_stat 3 get ptb 1 lim 9 |
| 2597 | pt_ok le sub sched_stat 13 get ptb 2 mul ev 64 14 |
| 2598 | pt_cursor get ptb 4 get ptb 5 |
| 2599 | pt_vbound |
| 2600 | end |
| 2601 | |
| 2602 | note Resume dispatch (it dispatches at once) inside the step bounds. |
| 2603 | fn pt_resume c long to void |
| 2604 | pt_snap |
| 2605 | k = sched_pause c false |
| 2606 | pt_check |
| 2607 | end |
| 2608 | |
| 2609 | note The cursor moved by the window once per window scan since (u0, s0). |
| 2610 | fn pt_cursor u0 ulong s0 ulong to void |
| 2611 | d = mul sched_stat 25 sub sched_stat 14 s0 |
| 2612 | pt_ok eq sched_stat 28 rem add u0 d 64 15 |
| 2613 | end |
| 2614 | |
| 2615 | note The value-read bound since the last call (phase checks, exit 8). |
| 2616 | fn pt_vbound to void |
| 2617 | v = sched_stat 38 |
| 2618 | lim = add mul sub sched_stat 32 get ptv 1 64 mul sub sched_stat 33 get ptv 2 128 |
| 2619 | pt_ok le sub v get ptv 0 lim 8 |
| 2620 | put ptv 0 v |
| 2621 | put ptv 1 sched_stat 32 |
| 2622 | put ptv 2 sched_stat 33 |
| 2623 | end |
| 2624 | |
| 2625 | note Step until job j is in a state in lo..hi, up to ms. True when reached. |
| 2626 | fn pt_until c long j ulong lo ulong hi ulong ms ulong code int to bool |
| 2627 | dl = add ulong monons mul ms 1000000 |
| 2628 | for t ulong 1000000 |
| 2629 | s = get sjst j |
| 2630 | if and ge s lo le s hi |
| 2631 | ret true |
| 2632 | end |
| 2633 | if ge ulong monons dl |
| 2634 | ret false |
| 2635 | end |
| 2636 | pt_step c code |
| 2637 | end |
| 2638 | ret false |
| 2639 | end |
| 2640 | |
| 2641 | note Review every job in review whose outcome is its id + 1. |
| 2642 | fn pt_review code int to ulong |
| 2643 | n = ulong 0 |
| 2644 | for j ulong 64 |
| 2645 | if eq get sjst j 4 |
| 2646 | pt_ok eq sched_review j long add get sjid j 1 code |
| 2647 | n = add n 1 |
| 2648 | end |
| 2649 | end |
| 2650 | ret n |
| 2651 | end |
| 2652 | |
| 2653 | note Step and review until n jobs were reviewed, up to ms. |
| 2654 | fn pt_drain c long n ulong ms ulong code int to void |
| 2655 | dl = add ulong monons mul ms 1000000 |
| 2656 | k = ulong 0 |
| 2657 | for t ulong 1000000 |
| 2658 | k = add k pt_review code |
| 2659 | if ge k n |
| 2660 | ret |
| 2661 | end |
| 2662 | pt_ok lt ulong monons dl code |
| 2663 | pt_step c code |
| 2664 | end |
| 2665 | exit code |
| 2666 | end |
| 2667 | |
| 2668 | fn pt_admit id ulong deps ulong req ulong code int to ulong |
| 2669 | j = sched_admitr id deps 0 req |
| 2670 | pt_ok ge j 0 code |
| 2671 | ret ulong j |
| 2672 | end |
| 2673 | |
| 2674 | note Phase C: a request beyond the capacity of any kind is refused (-8) and |
| 2675 | note counted; lowering a capacity below an admitted job's need, or below |
| 2676 | note the use of two running jobs that each fit it alone, is refused and |
| 2677 | note changes nothing. |
| 2678 | fn pt_impossible to void |
| 2679 | c = pt_setup 2 8 8 pt_req 4 1024 1 8 30 |
| 2680 | pt_ok eq sched_admitr 1 0 0 pt_req 5 0 0 0 neg long 8 31 |
| 2681 | pt_ok eq sched_admitr 2 0 0 pt_req 1 1025 0 0 neg long 8 32 |
| 2682 | pt_ok eq sched_admitr 3 0 0 pt_req 0 0 2 0 neg long 8 33 |
| 2683 | pt_ok eq sched_stat 7 3 34 |
| 2684 | a = pt_admit 4 0 pt_req 3 0 0 0 35 |
| 2685 | pt_ok eq sched_cap pt_req 2 1024 1 8 neg long 8 36 |
| 2686 | pt_ok eq sched_stat 16 4 37 |
| 2687 | pt_ok eq sched_cancel a 1 38 |
| 2688 | pt_ok eq sched_cap pt_req 2 1024 1 8 0 39 |
| 2689 | pt_ok eq sched_stat 16 2 40 |
| 2690 | pt_ok eq sched_cap pt_req 4 1024 1 8 0 130 |
| 2691 | p = pt_admit 130400 0 pt_req 2 0 0 0 130 |
| 2692 | q = pt_admit 140400 0 pt_req 2 0 0 0 130 |
| 2693 | pt_ok pt_until c p 3 3 2000 130 130 |
| 2694 | pt_ok pt_until c q 3 3 2000 130 130 |
| 2695 | pt_ok eq sched_cap pt_req 3 1024 1 8 neg long 8 131 |
| 2696 | pt_ok eq sched_stat 16 4 132 |
| 2697 | pt_drain c 2 3000 133 |
| 2698 | pt_ok eq sched_cap pt_req 3 1024 1 8 0 134 |
| 2699 | pt_down c 41 |
| 2700 | end |
| 2701 | |
| 2702 | note Phase D: 12 jobs with mixed CPU, memory and inference needs on three |
| 2703 | note workers, capacities 4 CPU, 1024 MiB, 1 inference slot. Every job runs |
| 2704 | note once and returns its outcome; the invariant holds at every step and |
| 2705 | note every resource is free at the end. |
| 2706 | fn pt_churn to void |
| 2707 | c = pt_setup 3 16 8 pt_req 4 1024 1 0 42 |
| 2708 | for i ulong 12 |
| 2709 | cpu = add rem i 3 1 |
| 2710 | mem = mul add rem i 6 1 100 |
| 2711 | inf = rem i 2 |
| 2712 | id = add mul add i 1 10000 add 30 mul rem i 4 15 |
| 2713 | j = pt_admit id 0 pt_req cpu mem inf 0 43 |
| 2714 | end |
| 2715 | pt_drain c 12 10000 44 |
| 2716 | for k ulong 4 |
| 2717 | pt_ok eq get suse k 0 45 |
| 2718 | end |
| 2719 | pt_ok eq sched_count 1 6 0 46 |
| 2720 | pt_ok eq sched_stat 4 12 47 |
| 2721 | pt_down c 48 |
| 2722 | end |
| 2723 | |
| 2724 | note Some running job whose worker has reported done (its result waits |
| 2725 | note for review room). |
| 2726 | fn pt_held to bool |
| 2727 | for j ulong 64 |
| 2728 | if eq get sjst j 3 |
| 2729 | w = get sjw j |
| 2730 | if eq sc_cell w 64 get scseq w |
| 2731 | ret true |
| 2732 | end |
| 2733 | end |
| 2734 | end |
| 2735 | ret false |
| 2736 | end |
| 2737 | |
| 2738 | note Phase V: release while review is full. Review limit 1, two workers, |
| 2739 | note 2 CPU: j1 and j2 (1 CPU, 50 ms) both finish; one enters review, the |
| 2740 | note other's result waits for room, yet both CPUs are free at once and j3 |
| 2741 | note (2 CPU) runs before anything is reviewed. |
| 2742 | fn pt_release to void |
| 2743 | c = pt_setup 2 8 1 pt_req 2 1024 1 8 120 |
| 2744 | j1 = pt_admit 150050 0 pt_req 1 0 0 0 120 |
| 2745 | j2 = pt_admit 160050 0 pt_req 1 0 0 0 120 |
| 2746 | dl = add ulong monons 2000000000 |
| 2747 | for t ulong 1000000 |
| 2748 | if eq sched_count 4 4 1 |
| 2749 | if pt_held |
| 2750 | break |
| 2751 | end |
| 2752 | end |
| 2753 | pt_ok lt ulong monons dl 121 |
| 2754 | pt_step c 121 |
| 2755 | end |
| 2756 | note The done report may land after the last collect: one more step |
| 2757 | note collects it (review stays full, so nothing else changes). |
| 2758 | pt_step c 121 |
| 2759 | pt_ok eq get suse 0 0 122 |
| 2760 | j3 = pt_admit 170050 0 pt_req 2 0 0 0 123 |
| 2761 | pt_ok pt_until c j3 3 4 2000 123 124 |
| 2762 | pt_drain c 3 3000 125 |
| 2763 | pt_down c 126 |
| 2764 | end |
| 2765 | |
| 2766 | note Phase F: ready order. With one worker, x is admitted first waiting on |
| 2767 | note topic 8 and y second ready at once; then topic 8 is published. FIFO |
| 2768 | note (policy 0) serves the job that became ready first, y; fair fitting |
| 2769 | note serves the job admitted first, x (waiting age counts from admission). |
| 2770 | fn pt_order to void |
| 2771 | buf = new uchar 4096 0 |
| 2772 | c = pt_setup 1 8 8 pt_req 4 1024 1 8 50 |
| 2773 | x = pt_admit 60050 8 0 51 |
| 2774 | y = pt_admit 70050 0 0 52 |
| 2775 | pt_snap |
| 2776 | pt_ok gt sched_publish c 8 buf 16 0 53 |
| 2777 | pt_check |
| 2778 | if eq sched_stat 24 0 |
| 2779 | pt_ok eq get sjst y 3 54 |
| 2780 | pt_ok eq get sjst x 2 55 |
| 2781 | else |
| 2782 | pt_ok eq get sjst x 3 54 |
| 2783 | pt_ok eq get sjst y 2 55 |
| 2784 | end |
| 2785 | pt_drain c 2 3000 56 |
| 2786 | pt_down c 57 |
| 2787 | end |
| 2788 | |
| 2789 | note Witness of a head-of-line block, read with no step in between: a runs |
| 2790 | note unfinished holding its need, the head h is ready and does not fit, x |
| 2791 | note is ready and fits, a live worker is idle, and dispatch stopped at the |
| 2792 | note head at least once since h0 (codes code..code+4 when not). |
| 2793 | fn pt_witness c long a ulong h ulong x ulong h0 ulong code int to void |
| 2794 | w = get sjw a |
| 2795 | pt_ok eq get sjst a 3 code |
| 2796 | pt_ok ne sc_cell w 64 get scseq w code |
| 2797 | pt_ok eq get sjheld a 1 code |
| 2798 | pt_ok eq get sjst h 2 add code 1 |
| 2799 | pt_ok not sp_fits get sjneed h 0 add code 1 |
| 2800 | pt_ok eq get sjst x 2 add code 2 |
| 2801 | pt_ok sp_fits get sjneed x 0 add code 2 |
| 2802 | i = sp_idle mapget c 48 8 |
| 2803 | pt_ok lt i 64 add code 3 |
| 2804 | pt_ok gt sched_stat 8 h0 add code 4 |
| 2805 | r = write 1 pw1 0 len pw1 |
| 2806 | pt_num get sjid h |
| 2807 | r = write 1 pw2 0 len pw2 |
| 2808 | pt_num get sjid x |
| 2809 | r = write 1 pw3 0 len pw3 |
| 2810 | pt_num sp_free 0 |
| 2811 | r = write 1 pw4 0 len pw4 |
| 2812 | pt_num i |
| 2813 | r = write 1 phol 0 len phol |
| 2814 | pt_num sub sched_stat 8 h0 |
| 2815 | r = write 1 pnl 0 1 |
| 2816 | end |
| 2817 | |
| 2818 | note Phase A: the blocked head. a (3 CPU, 800 ms) runs; then b (2 CPU, |
| 2819 | note cannot fit) and c (1 CPU, fits) arrive with no dependency. The target: |
| 2820 | note c runs while a still runs and b waits (0). When c has not run within |
| 2821 | note 400 ms, the witness is checked and printed and the phase returns 11: |
| 2822 | note c stayed idle behind b although a CPU and a worker were free. |
| 2823 | fn pt_blocked to int |
| 2824 | c = pt_setup 3 8 8 pt_req 4 1024 1 8 10 |
| 2825 | a = pt_admit 10800 0 pt_req 3 0 0 0 10 |
| 2826 | pt_ok pt_until c a 3 3 2000 10 10 |
| 2827 | b = pt_admit 20050 0 pt_req 2 0 0 0 10 |
| 2828 | x = pt_admit 30050 0 pt_req 1 0 0 0 10 |
| 2829 | h0 = sched_stat 8 |
| 2830 | v = int 0 |
| 2831 | if pt_until c x 3 4 400 10 |
| 2832 | pt_ok eq get sjst a 3 12 |
| 2833 | pt_ok eq get sjst b 2 13 |
| 2834 | else |
| 2835 | pt_witness c a b x h0 12 |
| 2836 | v = int 11 |
| 2837 | end |
| 2838 | pt_drain c 3 5000 17 |
| 2839 | pt_down c 18 |
| 2840 | ret v |
| 2841 | end |
| 2842 | |
| 2843 | note Phase B: a second head-of-line case, after the pump's queue-of-two |
| 2844 | note scenario (the bounded queue itself is not modelled: every admitted |
| 2845 | note job is in the table). a (3 CPU, 800 ms) runs, b and d (2 CPU) cannot |
| 2846 | note fit, e (1 CPU) fits. Target: e runs while a still runs (0); else the |
| 2847 | note witness and 21. |
| 2848 | fn pt_exclude to int |
| 2849 | c = pt_setup 3 8 8 pt_req 4 1024 1 8 20 |
| 2850 | a = pt_admit 10800 0 pt_req 3 0 0 0 20 |
| 2851 | pt_ok pt_until c a 3 3 2000 20 20 |
| 2852 | b = pt_admit 20050 0 pt_req 2 0 0 0 20 |
| 2853 | d = pt_admit 40050 0 pt_req 2 0 0 0 20 |
| 2854 | e = pt_admit 50050 0 pt_req 1 0 0 0 20 |
| 2855 | h0 = sched_stat 8 |
| 2856 | v = int 0 |
| 2857 | if pt_until c e 3 4 400 20 |
| 2858 | pt_ok eq get sjst a 3 22 |
| 2859 | else |
| 2860 | pt_witness c a b e h0 22 |
| 2861 | v = int 21 |
| 2862 | end |
| 2863 | pt_drain c 4 5000 27 |
| 2864 | pt_down c 28 |
| 2865 | ret v |
| 2866 | end |
| 2867 | |
| 2868 | note Phase S: starvation under repeated small arrivals. Three workers, 4 |
| 2869 | note CPU. Small jobs (1 CPU) start staggered (100, 200, 300 ms), and each |
| 2870 | note that finishes is replaced by one of 300 ms, so the small jobs never |
| 2871 | note all end together; big (4 CPU, 50 ms) needs every CPU free. The target: |
| 2872 | note big runs within 4 s. 61 means it never did: the small jobs kept the |
| 2873 | note CPU partly busy (the "noreserve" control). |
| 2874 | fn pt_starve to void |
| 2875 | c = pt_setup 3 16 8 pt_req 4 1024 1 8 60 |
| 2876 | for i ulong 3 |
| 2877 | j = pt_admit mul add i 1 100100 0 pt_req 1 0 0 0 60 |
| 2878 | end |
| 2879 | s = ulong 4 |
| 2880 | g = pt_admit 90050 0 pt_req 4 0 0 0 60 |
| 2881 | dl = add ulong monons 4000000000 |
| 2882 | ok = false |
| 2883 | for t ulong 1000000 |
| 2884 | if ge get sjst g 3 |
| 2885 | ok = true |
| 2886 | break |
| 2887 | end |
| 2888 | if ge ulong monons dl |
| 2889 | break |
| 2890 | end |
| 2891 | if lt sched_count 1 3 4 |
| 2892 | j = pt_admit add mul s 100000 300 0 pt_req 1 0 0 0 60 |
| 2893 | s = add s 1 |
| 2894 | end |
| 2895 | pt_step c 60 |
| 2896 | n = pt_review 60 |
| 2897 | end |
| 2898 | pt_ok ok 61 |
| 2899 | pt_ok ge sched_stat 6 1 62 |
| 2900 | pt_ok ge sched_stat 9 1 63 |
| 2901 | pt_drain c sched_count 1 4 5000 64 |
| 2902 | pt_stats |
| 2903 | pt_ok eq get suse 0 0 65 |
| 2904 | pt_down c 66 |
| 2905 | end |
| 2906 | |
| 2907 | note Phase Q: admission tickets. Limit 2: p1 (400 ms) and p2 (900 ms) fill |
| 2908 | note it; X then Y are refused and hold tickets in that order. When p1 is |
| 2909 | note reviewed there is room for one: Y asking first is refused (75), X is |
| 2910 | note admitted (76); Y enters once more room returns (78). A ticket not |
| 2911 | note asked about for the lifetime (20 events) lapses, so a new caller is |
| 2912 | note admitted although the old holder never came back (80). The wait for |
| 2913 | note the lapse reviews whatever finishes, 140900 included when it outlasts |
| 2914 | note that job's 900 ms, so the last drain waits for the jobs still |
| 2915 | note outstanding, not a fixed two: a fixed two timed out (80) whenever the |
| 2916 | note loop had already reviewed 140900. |
| 2917 | fn pt_tickets to void |
| 2918 | c = pt_setup 2 2 8 pt_req 4 1024 1 8 70 |
| 2919 | pt_ok eq sched_policy sched_stat 24 64 64 sched_stat 27 1000 0 70 |
| 2920 | p1 = pt_admit 100400 0 0 70 |
| 2921 | p2 = pt_admit 110900 0 0 70 |
| 2922 | pt_ok eq sched_admitr 120050 0 0 0 neg long 2 71 |
| 2923 | pt_ok eq sched_admitr 130050 0 0 0 neg long 2 72 |
| 2924 | pt_ok eq sched_stat 30 2 73 |
| 2925 | pt_drain c 1 3000 74 |
| 2926 | pt_ok eq sched_admitr 130050 0 0 0 neg long 2 75 |
| 2927 | pt_ok ge sched_admitr 120050 0 0 0 0 76 |
| 2928 | pt_drain c 2 3000 77 |
| 2929 | pt_ok ge sched_admitr 130050 0 0 0 0 78 |
| 2930 | pt_ok eq sched_policy sched_stat 24 64 64 sched_stat 27 20 0 79 |
| 2931 | f = pt_admit 140900 0 0 79 |
| 2932 | pt_ok eq sched_admitr 150050 0 0 0 neg long 2 79 |
| 2933 | e0 = sched_stat 2 |
| 2934 | dl = add ulong monons 3000000000 |
| 2935 | for t ulong 1000000 |
| 2936 | if le sched_count 1 3 1 |
| 2937 | if gt sched_stat 2 add e0 21 |
| 2938 | break |
| 2939 | end |
| 2940 | end |
| 2941 | pt_ok lt ulong monons dl 79 |
| 2942 | pt_step c 79 |
| 2943 | n = pt_review 79 |
| 2944 | end |
| 2945 | pt_ok ge sched_admitr 160050 0 0 0 0 80 |
| 2946 | pt_ok ge sched_stat 11 1 80 |
| 2947 | pt_drain c sched_count 1 4 3000 80 |
| 2948 | pt_down c 80 |
| 2949 | end |
| 2950 | |
| 2951 | note Step until sched_overdue reports a job, up to 2 s: its slot, or 64. |
| 2952 | fn pt_waitod c long code int to ulong |
| 2953 | t0 = ulong monons |
| 2954 | for t ulong 1000000 |
| 2955 | o = sched_overdue |
| 2956 | if lt o 64 |
| 2957 | ret o |
| 2958 | end |
| 2959 | if gt sub ulong monons t0 2000000000 |
| 2960 | ret 64 |
| 2961 | end |
| 2962 | pt_step c code |
| 2963 | end |
| 2964 | ret 64 |
| 2965 | end |
| 2966 | |
| 2967 | note Phase L: lease expiry. h (2 of 2 CPU, 3 s, not safe to rerun) has a |
| 2968 | note 200 ms lease: its deadline is at least 200 ms after h was admitted, and |
| 2969 | note sched_overdue reports it once, at or after that deadline (83); the |
| 2970 | note creator kills its worker; h becomes UNKNOWN and its CPU is free. |
| 2971 | note h2 (2 CPU, 400 ms, safe to rerun, 100 ms lease) is killed the same |
| 2972 | note way: it is ready or running again with its admission age kept, and |
| 2973 | note completes. Then a new 2-CPU job runs. |
| 2974 | fn pt_lease to void |
| 2975 | c = pt_setup 3 8 8 pt_req 2 1024 1 8 81 |
| 2976 | t0 = ulong monons |
| 2977 | h = pt_admit 173000 0 pt_req 2 0 0 0 81 |
| 2978 | pt_ok eq sched_lease h 200000000 0 81 |
| 2979 | pt_ok pt_until c h 3 3 2000 81 81 |
| 2980 | due = get sjdue h |
| 2981 | pt_ok ge due add t0 200000000 83 |
| 2982 | o = pt_waitod c 82 |
| 2983 | pt_ok eq o h 82 |
| 2984 | pt_ok ge ulong monons due 83 |
| 2985 | pt_ok eq sched_overdue 64 84 |
| 2986 | r = killpid int get swpid get sjw h 9 |
| 2987 | pt_ok pt_until c h 6 6 2000 85 85 |
| 2988 | pt_ok eq get suse 0 0 86 |
| 2989 | pt_ok eq sched_settle h 0 87 |
| 2990 | x = sched_admitr 180400 0 1 pt_req 2 0 0 0 |
| 2991 | pt_ok ge x 0 140 |
| 2992 | h2 = ulong x |
| 2993 | pt_ok eq sched_lease h2 100000000 0 140 |
| 2994 | pt_ok pt_until c h2 3 3 2000 140 140 |
| 2995 | ar = get sjarr h2 |
| 2996 | pt_ok eq pt_waitod c 141 h2 141 |
| 2997 | w = get sjw h2 |
| 2998 | r = killpid int get swpid w 9 |
| 2999 | dl = add ulong monons 2000000000 |
| 3000 | for t ulong 1000000 |
| 3001 | if eq get swpid w 0 |
| 3002 | break |
| 3003 | end |
| 3004 | pt_ok lt ulong monons dl 142 |
| 3005 | pt_step c 142 |
| 3006 | end |
| 3007 | pt_ok eq get sjarr h2 ar 143 |
| 3008 | s = get sjst h2 |
| 3009 | pt_ok and ge s 2 le s 3 144 |
| 3010 | pt_drain c 1 3000 145 |
| 3011 | z = pt_admit 190050 0 pt_req 2 0 0 0 88 |
| 3012 | pt_drain c 1 3000 88 |
| 3013 | pt_down c 89 |
| 3014 | end |
| 3015 | |
| 3016 | note Phase R: cancellation of a reserved job. Bypass budget 1: a (1 of 2 |
| 3017 | note CPU) runs, b (2 CPU) waits, x (1 CPU) runs past it, so b is reserved. |
| 3018 | note Cancelling b frees it with no effect and clears the reservation. |
| 3019 | fn pt_rcancel to void |
| 3020 | c = pt_setup 2 8 8 pt_req 2 1024 1 8 90 |
| 3021 | pt_ok eq sched_policy sched_stat 24 64 64 1 1000 0 90 |
| 3022 | a = pt_admit 190600 0 pt_req 1 0 0 0 90 |
| 3023 | pt_ok pt_until c a 3 3 2000 90 90 |
| 3024 | b = pt_admit 200050 0 pt_req 2 0 0 0 90 |
| 3025 | x = pt_admit 210050 0 pt_req 1 0 0 0 90 |
| 3026 | pt_ok pt_until c x 3 4 400 90 91 |
| 3027 | pt_ok eq sched_stat 29 add b 1 92 |
| 3028 | pt_ok eq sched_cancel b 1 93 |
| 3029 | pt_ok eq sched_stat 29 0 94 |
| 3030 | pt_drain c 2 3000 95 |
| 3031 | pt_ok eq get suse 0 0 96 |
| 3032 | pt_down c 97 |
| 3033 | end |
| 3034 | |
| 3035 | note Phase W: bounded candidate work. Window 4 slots, dispatch budget 2, |
| 3036 | note two workers, so an event may pick twice: 12 ready jobs fill slots |
| 3037 | note 0..11, beyond any one window. Each pick reads at most 5 slots (the |
| 3038 | note reserved probe and the window, exit 9), every window scan moves the |
| 3039 | note cursor on by 4 (15), and all 12 run. 101 means some never did. |
| 3040 | fn pt_window to void |
| 3041 | c = pt_setup 2 16 16 pt_req 4 1024 1 8 101 |
| 3042 | pt_ok eq sched_policy sched_stat 24 4 2 sched_stat 27 1000 0 101 |
| 3043 | for i ulong 12 |
| 3044 | j = pt_admit add mul add i 1 10000 30 0 0 101 |
| 3045 | end |
| 3046 | pt_drain c 12 5000 101 |
| 3047 | pt_ok eq sched_stat 4 12 102 |
| 3048 | pt_down c 103 |
| 3049 | end |
| 3050 | |
| 3051 | note Phase T: ticket order with several openings. Limit 3 held by p1, p2 |
| 3052 | note (400 ms) and p3, which waits on a topic nobody publishes; X, Y, Z are |
| 3053 | note refused in that order and hold tickets (104). p1 and p2 are reviewed, |
| 3054 | note so there are exactly two openings: Y asking first is refused (105), and |
| 3055 | note so is a new caller W, who takes a ticket behind Z (106); X enters |
| 3056 | note (107), then Y (108). p3 is cancelled and X and Y are reviewed, so there |
| 3057 | note are three openings: W asking first is refused, then Z and W enter (109). |
| 3058 | fn pt_torder to void |
| 3059 | c = pt_setup 3 3 8 pt_req 4 1024 1 8 104 |
| 3060 | p = pt_admit 400400 0 0 104 |
| 3061 | p = pt_admit 410400 0 0 104 |
| 3062 | p3 = pt_admit 420050 4096 0 104 |
| 3063 | pt_ok eq sched_admitr 430050 0 0 0 neg long 2 104 |
| 3064 | pt_ok eq sched_admitr 440050 0 0 0 neg long 2 104 |
| 3065 | pt_ok eq sched_admitr 450050 0 0 0 neg long 2 104 |
| 3066 | pt_ok eq sched_stat 30 3 104 |
| 3067 | pt_drain c 2 3000 104 |
| 3068 | pt_ok eq sched_admitr 440050 0 0 0 neg long 2 105 |
| 3069 | pt_ok eq sched_admitr 460050 0 0 0 neg long 2 106 |
| 3070 | pt_ok eq sched_stat 30 4 106 |
| 3071 | pt_ok ge sched_admitr 430050 0 0 0 0 107 |
| 3072 | pt_ok ge sched_admitr 440050 0 0 0 0 108 |
| 3073 | pt_ok eq sched_cancel p3 1 108 |
| 3074 | pt_drain c 2 3000 108 |
| 3075 | pt_ok eq sched_admitr 460050 0 0 0 neg long 2 109 |
| 3076 | pt_ok ge sched_admitr 450050 0 0 0 0 109 |
| 3077 | pt_ok ge sched_admitr 460050 0 0 0 0 109 |
| 3078 | pt_drain c 2 3000 109 |
| 3079 | pt_down c 109 |
| 3080 | end |
| 3081 | |
| 3082 | note Phase X: the table, not the admission limit, is full. Admission limit |
| 3083 | note and review limit 64; 64 jobs finish and wait in review, so none is |
| 3084 | note admitted, waiting, ready or running. A new caller is refused and takes |
| 3085 | note a ticket (190, 191), as is a second (192). One review frees one slot: |
| 3086 | note the second holder is refused, the first enters (193, 194). |
| 3087 | fn pt_slots to void |
| 3088 | c = pt_setup 3 64 64 pt_req 4 1024 1 8 190 |
| 3089 | for i ulong 64 |
| 3090 | j = pt_admit add 500001 mul i 10000 0 0 190 |
| 3091 | end |
| 3092 | dl = add ulong monons 5000000000 |
| 3093 | for t ulong 1000000 |
| 3094 | if eq sched_count 4 4 64 |
| 3095 | break |
| 3096 | end |
| 3097 | pt_ok lt ulong monons dl 190 |
| 3098 | pt_step c 190 |
| 3099 | end |
| 3100 | pt_ok not sched_room 600050 191 |
| 3101 | pt_ok eq sched_stat 30 1 191 |
| 3102 | pt_ok eq sched_admitr 600050 0 0 0 neg long 2 192 |
| 3103 | pt_ok eq sched_admitr 610050 0 0 0 neg long 2 192 |
| 3104 | pt_ok eq sched_stat 30 2 192 |
| 3105 | pt_ok eq sched_review 0 long add get sjid 0 1 193 |
| 3106 | pt_ok eq sched_admitr 610050 0 0 0 neg long 2 193 |
| 3107 | pt_ok ge sched_admitr 600050 0 0 0 0 194 |
| 3108 | n = pt_review 194 |
| 3109 | pt_ok ge sched_admitr 610050 0 0 0 0 194 |
| 3110 | pt_drain c 2 3000 194 |
| 3111 | pt_down c 194 |
| 3112 | end |
| 3113 | |
| 3114 | note Phase Z: reserved progress needs review progress. Review limit 1, two |
| 3115 | note workers, 2 CPU, bypass budget 1. p finishes and waits in review, |
| 3116 | note unreviewed. a (1 CPU) and x (1 CPU) run past b (2 CPU), so b is |
| 3117 | note reserved (196). a and x finish; their workers keep the results, since |
| 3118 | note review is full, so b is ready at every step for 600 ms (197) and all |
| 3119 | note CPU is free at the end of that interval (198). Reviewing p lets a into |
| 3120 | note review, frees its worker, and b runs (199). |
| 3121 | fn pt_live to void |
| 3122 | c = pt_setup 2 8 1 pt_req 2 1024 1 8 195 |
| 3123 | pt_ok eq sched_policy sched_stat 24 64 64 1 1000 0 195 |
| 3124 | p = pt_admit 470050 0 pt_req 1 0 0 0 195 |
| 3125 | pt_ok pt_until c p 4 4 2000 195 195 |
| 3126 | k = sched_pause c true |
| 3127 | a = pt_admit 480050 0 pt_req 1 0 0 0 195 |
| 3128 | b = pt_admit 490050 0 pt_req 2 0 0 0 195 |
| 3129 | x = pt_admit 510050 0 pt_req 1 0 0 0 195 |
| 3130 | pt_resume c |
| 3131 | pt_ok eq sched_stat 29 add b 1 196 |
| 3132 | dl = add ulong monons 600000000 |
| 3133 | for t ulong 1000000 |
| 3134 | if ge ulong monons dl |
| 3135 | break |
| 3136 | end |
| 3137 | pt_step c 197 |
| 3138 | pt_ok eq get sjst b 2 197 |
| 3139 | end |
| 3140 | pt_ok eq get suse 0 0 198 |
| 3141 | pt_ok eq sched_review p long add get sjid p 1 199 |
| 3142 | pt_ok pt_until c b 3 4 2000 199 199 |
| 3143 | pt_drain c 3 3000 199 |
| 3144 | pt_down c 199 |
| 3145 | end |
| 3146 | |
| 3147 | note Phase M: lease bounds (2^50 ns, about 13 days). Waiting job w refuses |
| 3148 | note 2^50 + 1 and 2^64 - 1 (-7) with its lease unchanged (200, 201) and |
| 3149 | note accepts 2^50 (202). Job r, leased 2^50 before it is assigned, runs with |
| 3150 | note a deadline ahead of the clock and nothing overdue (203); running, it |
| 3151 | note refuses both too large values with lease and deadline unchanged (204, |
| 3152 | note 205) and accepts a renewal of 2^49 measured from now: the new deadline |
| 3153 | note lies between clock + 2^49 read just before and just after the call |
| 3154 | note (206), and nothing is overdue; it then completes (207). |
| 3155 | fn pt_lbound to void |
| 3156 | c = pt_setup 1 8 8 pt_req 2 1024 1 8 200 |
| 3157 | mx = ulong 1125899906842624 |
| 3158 | w = pt_admit 220050 1 0 200 |
| 3159 | pt_ok eq sched_lease w add mx 1 neg long 7 200 |
| 3160 | pt_ok eq get sjlse w 0 200 |
| 3161 | pt_ok eq sched_lease w sub ulong 0 1 neg long 7 201 |
| 3162 | pt_ok eq get sjlse w 0 201 |
| 3163 | pt_ok eq sched_lease w mx 0 202 |
| 3164 | pt_ok eq get sjlse w mx 202 |
| 3165 | r = pt_admit 230600 0 pt_req 1 0 0 0 203 |
| 3166 | pt_ok eq sched_lease r mx 0 203 |
| 3167 | pt_ok pt_until c r 3 3 2000 203 203 |
| 3168 | d = get sjdue r |
| 3169 | pt_ok gt d ulong monons 203 |
| 3170 | pt_ok eq sched_overdue 64 203 |
| 3171 | pt_ok eq sched_lease r sub ulong 0 1 neg long 7 204 |
| 3172 | pt_ok and eq get sjdue r d eq get sjlse r mx 204 |
| 3173 | pt_ok eq sched_lease r add mx 1 neg long 7 205 |
| 3174 | pt_ok and eq get sjdue r d eq get sjlse r mx 205 |
| 3175 | w = ulong monons |
| 3176 | pt_ok eq sched_lease r div mx 2 0 206 |
| 3177 | t2 = ulong monons |
| 3178 | pt_ok ge get sjdue r add w div mx 2 206 |
| 3179 | pt_ok le get sjdue r add t2 div mx 2 206 |
| 3180 | pt_ok eq sched_overdue 64 206 |
| 3181 | pt_drain c 1 3000 207 |
| 3182 | pt_down c 207 |
| 3183 | end |
| 3184 | |
| 3185 | note Phase N: every ask renews a ticket, a successful one too. Admission |
| 3186 | note limit 1, ticket lifetime 2. o waits on a topic nobody publishes and |
| 3187 | note fills the limit; X is refused at event e and holds a ticket. o is |
| 3188 | note cancelled; at event e + 1 X asks and is told yes, which renews its |
| 3189 | note ticket (210). At e + 3 the ticket still holds (asked at e + 1, lifetime |
| 3190 | note 2), so newcomer Y is refused (211); at e + 4 it lapses, exactly one |
| 3191 | note lapse is counted and Y, now first, is admitted (212). |
| 3192 | fn pt_renew to void |
| 3193 | c = pt_setup 1 1 8 pt_req 2 1024 1 8 210 |
| 3194 | pt_ok eq sched_policy sched_stat 24 64 64 sched_stat 27 2 0 210 |
| 3195 | o = pt_admit 240050 1 0 210 |
| 3196 | pt_ok eq sched_admitr 250050 0 0 0 neg long 2 210 |
| 3197 | pt_ok eq sched_cancel o 1 210 |
| 3198 | k = sched_dispatch c |
| 3199 | pt_ok sched_room 250050 210 |
| 3200 | k = sched_dispatch c |
| 3201 | k = sched_dispatch c |
| 3202 | pt_ok eq sched_admitr 260050 0 0 0 neg long 2 211 |
| 3203 | l0 = sched_stat 11 |
| 3204 | k = sched_dispatch c |
| 3205 | pt_ok ge sched_admitr 260050 0 0 0 0 212 |
| 3206 | pt_ok eq sched_stat 11 add l0 1 212 |
| 3207 | pt_drain c 1 3000 212 |
| 3208 | pt_down c 212 |
| 3209 | end |
| 3210 | |
| 3211 | note Phase U: the dispatch budget binds. Four idle workers, dispatch budget |
| 3212 | note 2, six ready 1-CPU jobs and 4 CPU: one dispatch assigns exactly 2 |
| 3213 | note (220); all six then complete (221). |
| 3214 | fn pt_budget to void |
| 3215 | c = pt_setup 4 16 16 pt_req 4 1024 1 8 220 |
| 3216 | pt_ok eq sched_policy sched_stat 24 64 2 sched_stat 27 1000 0 220 |
| 3217 | for i ulong 6 |
| 3218 | j = pt_admit add 270100 mul i 10000 0 pt_req 1 0 0 0 220 |
| 3219 | end |
| 3220 | a0 = sched_stat 4 |
| 3221 | k = sched_dispatch c |
| 3222 | pt_ok and eq k 2 eq sub sched_stat 4 a0 2 220 |
| 3223 | pt_drain c 6 5000 221 |
| 3224 | pt_down c 221 |
| 3225 | end |
| 3226 | |
| 3227 | fn pt_mode to void |
| 3228 | a = new uchar 16 0 |
| 3229 | if ne argcnt 2 |
| 3230 | ret |
| 3231 | end |
| 3232 | n = argread 1 a 0 16 |
| 3233 | if eq n 4 |
| 3234 | if eq get a 0 102 |
| 3235 | put ptm 0 1 |
| 3236 | end |
| 3237 | if eq get a 0 103 |
| 3238 | put ptm 0 3 |
| 3239 | end |
| 3240 | end |
| 3241 | if eq n 9 |
| 3242 | put ptm 0 2 |
| 3243 | end |
| 3244 | end |
| 3245 | |
| 3246 | note Step, then review every job in review in slot order, appending its id |
| 3247 | note to the trace and publishing the topics it declared (as the workflow |
| 3248 | note does after accepting a result). |
| 3249 | fn pt_gstep c long buf edit uchar 4096 code int to void |
| 3250 | pt_step c code |
| 3251 | for j ulong 64 |
| 3252 | if eq get sjst j 4 |
| 3253 | p = get sjpro j |
| 3254 | n = get ptn 0 |
| 3255 | if lt n 16 |
| 3256 | put ptt n get sjid j |
| 3257 | end |
| 3258 | put ptn 0 add n 1 |
| 3259 | pt_ok eq sched_review j long add get sjid j 1 code |
| 3260 | if ne p 0 |
| 3261 | pt_snap |
| 3262 | pt_ok gt sched_publish c p buf 16 0 code |
| 3263 | pt_check |
| 3264 | end |
| 3265 | end |
| 3266 | end |
| 3267 | end |
| 3268 | |
| 3269 | fn pt_gadmit id ulong deps ulong pro ulong code int to ulong |
| 3270 | j = pt_admit id deps pt_req 1 0 0 0 code |
| 3271 | if ne pro 0 |
| 3272 | pt_ok eq sched_goal j pro 50 10 0 code |
| 3273 | end |
| 3274 | ret j |
| 3275 | end |
| 3276 | |
| 3277 | note One run of the goal workload, one worker, policy 2, bypass budget 4, |
| 3278 | note admitted while paused: x (no value), z (unblocks d: value 1), a |
| 3279 | note (unblocks b, which unblocks c: value 2), f (unblocks g1..g3: value 3). |
| 3280 | note A decision is made when the worker frees, before the finished job's |
| 3281 | note topics are published. Order: f a z b x d c g1 g2 g3. f and a first |
| 3282 | note (useful work); x, bypassed in 4 events, is reserved and runs 5th. |
| 3283 | fn pt_grun code int to void |
| 3284 | buf = new uchar 4096 0 |
| 3285 | c = pt_setup 1 16 16 pt_req 1 1024 1 8 code |
| 3286 | pt_ok eq sched_policy 2 64 64 4 1000 0 code |
| 3287 | k = sched_pause c true |
| 3288 | put ptn 0 0 |
| 3289 | j = pt_gadmit 220050 0 0 code |
| 3290 | j = pt_gadmit 230050 0 128 code |
| 3291 | j = pt_gadmit 240050 128 0 code |
| 3292 | j = pt_gadmit 250050 0 32 code |
| 3293 | j = pt_gadmit 260050 32 64 code |
| 3294 | j = pt_gadmit 270050 64 0 code |
| 3295 | j = pt_gadmit 280050 0 256 code |
| 3296 | for i ulong 3 |
| 3297 | j = pt_gadmit add 290050 mul i 10000 256 0 code |
| 3298 | end |
| 3299 | pt_resume c |
| 3300 | dl = add ulong monons 5000000000 |
| 3301 | for t ulong 1000000 |
| 3302 | if ge get ptn 0 10 |
| 3303 | break |
| 3304 | end |
| 3305 | pt_ok lt ulong monons dl code |
| 3306 | pt_gstep c buf code |
| 3307 | end |
| 3308 | pt_down c code |
| 3309 | end |
| 3310 | |
| 3311 | note Phase G: goal-aware ranking. The goal workload runs twice; both traces |
| 3312 | note must equal the expected order (151 first run, 152 second): repeated |
| 3313 | note identical inputs, identical decisions. |
| 3314 | fn pt_goal to void |
| 3315 | ex = new ulong 10 0 |
| 3316 | put ex 0 280050 |
| 3317 | put ex 1 250050 |
| 3318 | put ex 2 230050 |
| 3319 | put ex 3 260050 |
| 3320 | put ex 4 220050 |
| 3321 | put ex 5 240050 |
| 3322 | put ex 6 270050 |
| 3323 | put ex 7 290050 |
| 3324 | put ex 8 300050 |
| 3325 | put ex 9 310050 |
| 3326 | for r ulong 2 |
| 3327 | code = int add 151 r |
| 3328 | pt_grun 150 |
| 3329 | pt_ok eq get ptn 0 10 code |
| 3330 | for i ulong 10 |
| 3331 | if ne get ptt i get ex i |
| 3332 | pt_trace |
| 3333 | exit code |
| 3334 | end |
| 3335 | end |
| 3336 | end |
| 3337 | end |
| 3338 | |
| 3339 | fn pt_trace to void |
| 3340 | r = write 1 ptr 0 len ptr |
| 3341 | for i ulong get ptn 0 bound 16 |
| 3342 | pt_num get ptt i |
| 3343 | r = write 1 psp 0 1 |
| 3344 | end |
| 3345 | r = write 1 pnl 0 1 |
| 3346 | end |
| 3347 | |
| 3348 | note Phase H: rescoring. Policy 2, paused: one ready job r and two jobs |
| 3349 | note waiting on topic 512, then r declares topic 512: four graph changes, |
| 3350 | note no rescoring (160). The first pick rescores once for all four (161). |
| 3351 | note r completing and being reviewed with nothing published changes no |
| 3352 | note value: no rescoring (162). Publishing 512 readies both waiting jobs: |
| 3353 | note two changes, one more rescoring at the next pick (163). The work of |
| 3354 | note every rescoring is bounded (checked at each step, exit 8). Cancelling |
| 3355 | note a waiting job is one more change (165). |
| 3356 | fn pt_rescores to void |
| 3357 | buf = new uchar 4096 0 |
| 3358 | c = pt_setup 1 8 8 pt_req 4 1024 1 8 160 |
| 3359 | pt_ok eq sched_policy 2 64 64 4 1000 0 160 |
| 3360 | k = sched_pause c true |
| 3361 | r = pt_admit 320050 0 pt_req 1 0 0 0 160 |
| 3362 | w1 = pt_admit 330050 512 pt_req 1 0 0 0 160 |
| 3363 | w2 = pt_admit 340050 512 pt_req 1 0 0 0 160 |
| 3364 | pt_ok eq sched_goal r 512 50 10 0 160 |
| 3365 | pt_ok eq sched_stat 34 4 160 |
| 3366 | pt_ok eq sched_stat 32 0 160 |
| 3367 | pt_resume c |
| 3368 | pt_ok eq sched_stat 32 1 161 |
| 3369 | pt_ok eq get sjval r 2 161 |
| 3370 | pt_drain c 1 2000 162 |
| 3371 | pt_ok eq sched_stat 32 1 162 |
| 3372 | pt_ok eq sched_stat 34 4 162 |
| 3373 | pt_snap |
| 3374 | pt_ok gt sched_publish c 512 buf 16 0 163 |
| 3375 | pt_check |
| 3376 | pt_ok eq sched_stat 34 6 163 |
| 3377 | pt_ok eq sched_stat 32 2 163 |
| 3378 | pt_drain c 2 2000 164 |
| 3379 | pt_ok eq sched_stat 32 2 165 |
| 3380 | w3 = pt_admit 345050 4096 pt_req 1 0 0 0 165 |
| 3381 | b0 = sched_stat 34 |
| 3382 | pt_ok eq sched_cancel w3 1 165 |
| 3383 | pt_ok eq sched_stat 34 add b0 1 165 |
| 3384 | pt_down c 166 |
| 3385 | end |
| 3386 | |
| 3387 | note Phase K: the policy-2 key on two free slots, a and b. Larger unblock |
| 3388 | note value first, even when b is older and cheaper (180); equal values: |
| 3389 | note smaller estimate first, even when b is older (181); equal values and |
| 3390 | note estimates: older admission first (182); outside policy 2 only the |
| 3391 | note admission age counts (183). |
| 3392 | fn pt_key to void |
| 3393 | a = ulong 60 |
| 3394 | b = ulong 61 |
| 3395 | put sjval a 2 |
| 3396 | put sjval b 1 |
| 3397 | put sjest a 90 |
| 3398 | put sjest b 10 |
| 3399 | put sjarr a 9 |
| 3400 | put sjarr b 8 |
| 3401 | pt_ok sp_better true a b 180 |
| 3402 | pt_ok not sp_better true b a 180 |
| 3403 | put sjval b 2 |
| 3404 | put sjest a 10 |
| 3405 | put sjest b 90 |
| 3406 | pt_ok sp_better true a b 181 |
| 3407 | pt_ok not sp_better true b a 181 |
| 3408 | put sjest b 10 |
| 3409 | pt_ok sp_better true b a 182 |
| 3410 | pt_ok not sp_better true a b 182 |
| 3411 | put sjval a 5 |
| 3412 | put sjest b 1 |
| 3413 | pt_ok sp_better false b a 183 |
| 3414 | pt_ok not sp_better false a b 183 |
| 3415 | for i ulong 2 |
| 3416 | put sjval add a i 0 |
| 3417 | put sjest add a i 0 |
| 3418 | put sjarr add a i 0 |
| 3419 | end |
| 3420 | end |
| 3421 | |
| 3422 | note Phase P: proposals. Policy 2, paused. p1 (value 0) and p2 (value 1) |
| 3423 | note are ready. A proposal naming an older graph version is refused (170) |
| 3424 | note and counted (171); one naming an id with no ready job is refused |
| 3425 | note (172); p1 at the current version is accepted (173) and used over the |
| 3426 | note higher ranked p2 (174). A proposal made stale by a later change is |
| 3427 | note rejected when next used and counted: p3 runs by rank, after p2 (175, |
| 3428 | note 176). Outside policy 2: -7 (177). |
| 3429 | fn pt_proposals to void |
| 3430 | c = pt_setup 1 8 8 pt_req 4 1024 1 8 170 |
| 3431 | pt_ok eq sched_incarnate 1 0 170 |
| 3432 | pt_ok eq sched_policy 2 64 64 4 1000 0 170 |
| 3433 | k = sched_pause c true |
| 3434 | p1 = pt_admit 350050 0 pt_req 1 0 0 0 170 |
| 3435 | p2 = pt_admit 360050 0 pt_req 1 0 0 0 170 |
| 3436 | q = pt_admit 370050 1024 pt_req 1 0 0 0 170 |
| 3437 | pt_ok eq sched_goal p2 1024 50 10 0 170 |
| 3438 | v = sched_version |
| 3439 | p3 = pt_admit 380050 0 pt_req 1 0 0 0 170 |
| 3440 | pt_ok eq sched_propose 350050 v neg long 9 170 |
| 3441 | pt_ok eq sched_stat 36 1 171 |
| 3442 | v = sched_version |
| 3443 | pt_ok eq sched_propose 999999 v neg long 3 172 |
| 3444 | pt_ok eq sched_propose 350050 v 0 173 |
| 3445 | pt_resume c |
| 3446 | pt_ok eq get sjst p1 3 174 |
| 3447 | pt_ok eq sched_stat 39 1 174 |
| 3448 | k = sched_pause c true |
| 3449 | v = sched_version |
| 3450 | pt_ok eq sched_propose 380050 v 0 175 |
| 3451 | p4 = pt_admit 390050 2048 pt_req 1 0 0 0 175 |
| 3452 | pt_ok eq sched_goal p3 0 10 1 0 175 |
| 3453 | pt_resume c |
| 3454 | pt_ok pt_until c p2 3 4 2000 175 175 |
| 3455 | pt_ok eq get sjst p3 2 176 |
| 3456 | pt_ok eq sched_stat 39 1 176 |
| 3457 | pt_ok eq sched_stat 36 2 176 |
| 3458 | pt_ok eq sched_cancel p4 1 176 |
| 3459 | pt_ok eq sched_cancel q 1 176 |
| 3460 | pt_drain c 3 3000 176 |
| 3461 | pt_ok eq sched_policy 1 64 64 4 1000 0 177 |
| 3462 | pt_ok eq sched_propose 350050 sched_version neg long 7 177 |
| 3463 | pt_down c 178 |
| 3464 | end |
| 3465 | |
| 3466 | note Phase I: a proposal from an earlier scheduler. Scheduler 1 |
| 3467 | note (incarnation 1) admits a into slot 0 and hands out the version; |
| 3468 | note scheduler 2 (incarnation 2) admits b into slot 0 with as many bumps. |
| 3469 | note There the old version is stale (184); a's id names no job even at the |
| 3470 | note current version, b's is accepted, and incarnation 0 is refused (185). |
| 3471 | fn pt_incarn to void |
| 3472 | c = pt_setup 1 8 8 pt_req 4 1024 1 8 184 |
| 3473 | pt_ok eq sched_incarnate 1 0 184 |
| 3474 | pt_ok eq sched_policy 2 64 64 4 1000 0 184 |
| 3475 | k = sched_pause c true |
| 3476 | a = pt_admit 670050 0 pt_req 1 0 0 0 184 |
| 3477 | pt_ok eq a 0 184 |
| 3478 | v1 = sched_version |
| 3479 | pt_ok eq sched_cancel a 1 184 |
| 3480 | pt_down c 184 |
| 3481 | c = pt_setup 1 8 8 pt_req 4 1024 1 8 184 |
| 3482 | pt_ok eq sched_incarnate 2 0 184 |
| 3483 | pt_ok eq sched_policy 2 64 64 4 1000 0 184 |
| 3484 | k = sched_pause c true |
| 3485 | b = pt_admit 680050 0 pt_req 1 0 0 0 184 |
| 3486 | pt_ok eq b 0 184 |
| 3487 | pt_ok eq sched_propose 670050 v1 neg long 9 184 |
| 3488 | pt_ok eq sched_propose 670050 sched_version neg long 3 185 |
| 3489 | pt_ok eq sched_propose 680050 sched_version 0 185 |
| 3490 | pt_ok eq sched_incarnate 0 neg long 7 185 |
| 3491 | pt_ok eq sched_cancel b 1 185 |
| 3492 | pt_down c 185 |
| 3493 | end |
| 3494 | |
| 3495 | note Phase R2: a proposal beside a reservation. Policy 2, bypass budget 1, |
| 3496 | note two workers, 5 CPU. h (2 CPU, 5 s) runs; then B and B2 (4 CPU each) |
| 3497 | note and s1 (1 CPU, 1.5 s) are admitted: s1 runs, B and B2 are bypassed and |
| 3498 | note B, the older, is reserved. x (2 CPU) is refused beside it and counted |
| 3499 | note (186). Cancelling B clears the reservation and x is accepted (187). |
| 3500 | note An event with no idle worker reserves B2 and leaves the proposal |
| 3501 | note pending (188). Once s1 is done, the next pick finds x no longer fits |
| 3502 | note beside B2: the proposal is dropped and counted stale, and x does not |
| 3503 | note run (189). Once h is done B2 fits; y (1 CPU) is accepted beside it, |
| 3504 | note and at the next event B2 runs first, then y is used; x (2 CPU, 0 |
| 3505 | note left) stays ready and, bypassed by y, is now the reservation (179). |
| 3506 | fn pt_propres to void |
| 3507 | c = pt_setup 2 8 8 pt_req 5 1024 2 8 186 |
| 3508 | pt_ok eq sched_incarnate 1 0 186 |
| 3509 | pt_ok eq sched_policy 2 64 64 1 1000 0 186 |
| 3510 | k = sched_pause c true |
| 3511 | h = pt_admit 625000 0 pt_req 2 0 0 0 186 |
| 3512 | pt_resume c |
| 3513 | k = sched_pause c true |
| 3514 | pt_ok eq get sjst h 3 186 |
| 3515 | b = pt_admit 630050 0 pt_req 4 0 0 0 186 |
| 3516 | b2 = pt_admit 640050 0 pt_req 4 0 0 0 186 |
| 3517 | s1 = pt_admit 651500 0 pt_req 1 0 0 0 186 |
| 3518 | pt_resume c |
| 3519 | k = sched_pause c true |
| 3520 | pt_ok eq get sjst s1 3 186 |
| 3521 | pt_ok eq sched_stat 29 add b 1 186 |
| 3522 | x = pt_admit 660050 0 pt_req 2 0 0 0 186 |
| 3523 | r0 = sched_stat 37 |
| 3524 | pt_ok eq sched_propose 660050 sched_version neg long 8 186 |
| 3525 | pt_ok eq sched_stat 37 add r0 1 186 |
| 3526 | pt_ok eq sched_cancel b 1 187 |
| 3527 | pt_ok eq sched_stat 29 0 187 |
| 3528 | pt_ok eq sched_propose 660050 sched_version 0 187 |
| 3529 | pt_resume c |
| 3530 | k = sched_pause c true |
| 3531 | pt_ok eq sched_stat 29 add b2 1 188 |
| 3532 | pt_ok gt get scgv 2 0 188 |
| 3533 | pt_ok pt_until c s1 4 4 3000 188 188 |
| 3534 | s0 = sched_stat 36 |
| 3535 | pt_resume c |
| 3536 | k = sched_pause c true |
| 3537 | pt_ok eq sched_stat 36 add s0 1 189 |
| 3538 | pt_ok eq get sjst x 2 189 |
| 3539 | pt_ok eq get scgv 2 0 189 |
| 3540 | y = pt_admit 690050 0 pt_req 1 0 0 0 179 |
| 3541 | pt_ok pt_until c h 4 4 6000 179 179 |
| 3542 | pt_ok eq sched_propose 690050 sched_version 0 179 |
| 3543 | s0 = sched_stat 39 |
| 3544 | pt_resume c |
| 3545 | k = sched_pause c true |
| 3546 | pt_ok eq get sjst b2 3 179 |
| 3547 | pt_ok eq get sjst y 3 179 |
| 3548 | pt_ok eq sched_stat 39 add s0 1 179 |
| 3549 | pt_ok eq sched_stat 29 add x 1 179 |
| 3550 | pt_ok eq get sjst x 2 179 |
| 3551 | pt_ok eq sched_cancel x 1 179 |
| 3552 | pt_drain c 4 8000 179 |
| 3553 | pt_down c 189 |
| 3554 | end |
| 3555 | |
| 3556 | fn pt_stats to void |
| 3557 | r = write 1 pst 0 len pst |
| 3558 | pt_num sched_stat 2 |
| 3559 | r = write 1 pex 0 len pex |
| 3560 | pt_num sched_stat 3 |
| 3561 | r = write 1 pas 0 len pas |
| 3562 | pt_num sched_stat 4 |
| 3563 | r = write 1 phol 0 len phol |
| 3564 | pt_num sched_stat 8 |
| 3565 | r = write 1 pbyp 0 len pbyp |
| 3566 | pt_num sched_stat 5 |
| 3567 | r = write 1 pres 0 len pres |
| 3568 | pt_num sched_stat 6 |
| 3569 | r = write 1 pnl 0 1 |
| 3570 | end |
| 3571 | |
| 3572 | note Phase X: proposal guards. Without an incarnation a proposal is refused |
| 3573 | note (120); a second incarnation is refused (121); after the first one a |
| 3574 | note proposal is accepted and used (122). The version is then set to the |
| 3575 | note last revision of incarnation 5 (2^40 - 1 bumps): a proposal made there |
| 3576 | note is accepted, and the next bump exhausts the version instead of |
| 3577 | note carrying it into incarnation 6's range (123); proposals are refused |
| 3578 | note (124), the pending one is dropped at use (125) and every pick rescores |
| 3579 | note (126). In a new scheduler, two ready jobs share an id: a proposal |
| 3580 | note naming it is refused (127). |
| 3581 | fn pt_incx to void |
| 3582 | c = pt_setup 1 8 8 pt_req 4 1024 1 8 120 |
| 3583 | pt_ok eq sched_policy 2 64 64 4 1000 0 120 |
| 3584 | k = sched_pause c true |
| 3585 | a = pt_admit 710050 0 pt_req 1 0 0 0 120 |
| 3586 | pt_ok eq sched_propose 710050 sched_version neg long 6 120 |
| 3587 | pt_ok eq sched_stat 41 0 120 |
| 3588 | pt_ok eq sched_incarnate 5 0 121 |
| 3589 | pt_ok eq sched_incarnate 6 neg long 7 121 |
| 3590 | pt_ok eq sched_propose 710050 sched_version 0 122 |
| 3591 | pt_ok eq sp_proposal 0 a 122 |
| 3592 | put scgv 0 add shl ulong 5 40 1099511627775 |
| 3593 | v = sched_version |
| 3594 | pt_ok eq sched_propose 710050 v 0 123 |
| 3595 | pt_ok eq sched_goal a 0 10 1 0 123 |
| 3596 | pt_ok eq sched_version v 123 |
| 3597 | pt_ok eq shr sched_version 40 5 123 |
| 3598 | pt_ok eq sched_stat 41 2 123 |
| 3599 | pt_ok eq sched_propose 710050 v neg long 6 124 |
| 3600 | pt_ok eq sp_proposal 0 64 125 |
| 3601 | r0 = sched_stat 32 |
| 3602 | sp_rescore |
| 3603 | sp_rescore |
| 3604 | pt_ok eq sched_stat 32 add r0 2 126 |
| 3605 | pt_ok eq sched_cancel a 1 126 |
| 3606 | pt_down c 126 |
| 3607 | c = pt_setup 1 8 8 pt_req 4 1024 1 8 127 |
| 3608 | pt_ok eq sched_incarnate 7 0 127 |
| 3609 | pt_ok eq sched_policy 2 64 64 4 1000 0 127 |
| 3610 | k = sched_pause c true |
| 3611 | b1 = pt_admit 720050 0 pt_req 1 0 0 0 127 |
| 3612 | b2 = pt_admit 720050 0 pt_req 1 0 0 0 127 |
| 3613 | pt_ok ne b1 b2 127 |
| 3614 | pt_ok eq sched_propose 720050 sched_version neg long 3 127 |
| 3615 | pt_ok eq sched_cancel b1 1 127 |
| 3616 | pt_ok eq sched_cancel b2 1 127 |
| 3617 | pt_down c 127 |
| 3618 | end |
| 3619 | |
| 3620 | note Phase L: a late first incarnation must not mark stale values current. |
| 3621 | note Paused, one worker, policy 2: an older unrelated job u, a younger |
| 3622 | note producer p declaring topic 64 and a job d waiting on it are admitted |
| 3623 | note and declared, then the scheduler incarnates. At resume the one pick |
| 3624 | note rescores and takes p (value 1); with the values left unscored they all |
| 3625 | note read 0 and u (older, no estimate) would run instead (128). |
| 3626 | fn pt_incl to void |
| 3627 | c = pt_setup 1 8 8 pt_req 4 1024 1 8 128 |
| 3628 | pt_ok eq sched_policy 2 64 64 4 1000 0 128 |
| 3629 | k = sched_pause c true |
| 3630 | u = pt_admit 730300 0 pt_req 1 0 0 0 128 |
| 3631 | p = pt_admit 740300 0 pt_req 1 0 0 0 128 |
| 3632 | d = pt_admit 750300 64 pt_req 1 0 0 0 128 |
| 3633 | pt_ok eq sched_goal p 64 300 0 0 128 |
| 3634 | pt_ok eq sched_incarnate 8 0 128 |
| 3635 | pt_resume c |
| 3636 | pt_ok eq get sjst p 3 128 |
| 3637 | pt_ok eq get sjst u 2 128 |
| 3638 | pt_ok eq sched_cancel u 1 128 |
| 3639 | pt_ok eq sched_cancel d 1 128 |
| 3640 | pt_drain c 1 3000 128 |
| 3641 | pt_down c 128 |
| 3642 | end |
| 3643 | |
| 3644 | note The regression phases A and B return their verdict, so both run and |
| 3645 | note print their witness; the first failing one is the exit status. |
| 3646 | fn main to int |
| 3647 | pt_mode |
| 3648 | pt_impossible |
| 3649 | pt_churn |
| 3650 | pt_release |
| 3651 | pt_order |
| 3652 | ra = pt_blocked |
| 3653 | rb = pt_exclude |
| 3654 | if ne ra 0 |
| 3655 | ret ra |
| 3656 | end |
| 3657 | if ne rb 0 |
| 3658 | ret rb |
| 3659 | end |
| 3660 | pt_starve |
| 3661 | pt_tickets |
| 3662 | pt_lease |
| 3663 | pt_rcancel |
| 3664 | pt_window |
| 3665 | pt_torder |
| 3666 | pt_slots |
| 3667 | pt_live |
| 3668 | pt_goal |
| 3669 | pt_rescores |
| 3670 | pt_proposals |
| 3671 | pt_incarn |
| 3672 | pt_propres |
| 3673 | pt_incx |
| 3674 | pt_incl |
| 3675 | pt_key |
| 3676 | pt_lbound |
| 3677 | pt_renew |
| 3678 | pt_budget |
| 3679 | if ne write 1 pok 0 len pok long len pok |
| 3680 | ret 100 |
| 3681 | end |
| 3682 | ret 0 |
| 3683 | end |