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 / journal-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 SPDX-License-Identifier: AGPL-3.0-only |
| 2371 | note OM-CONTROL-659 action contract v1. See ../README.md for the trusted-caller boundary. |
| 2372 | note No I/O, authority minting or persistence. Caller serializes and durably commits transitions. |
| 2373 | note Record words 0..7: version,state,attempt,limit,generation,epoch,expiry,reserved. |
| 2374 | note Words 8..31: operation,request,scope,provider,policy,grant identities, four words each. |
| 2375 | note State 1 ready,2 in-flight,3 unknown,4 performed. No timeout implies not performed. |
| 2376 | |
| 2377 | fn actvalid r view ulong 32 to ulong |
| 2378 | if ne get r 0 1 |
| 2379 | ret 11 |
| 2380 | end |
| 2381 | if or lt get r 1 1 gt get r 1 4 |
| 2382 | ret 12 |
| 2383 | end |
| 2384 | if or eq get r 3 0 gt get r 3 1024 |
| 2385 | ret 12 |
| 2386 | end |
| 2387 | if gt get r 2 get r 3 |
| 2388 | ret 12 |
| 2389 | end |
| 2390 | if and ne get r 1 1 eq get r 2 0 |
| 2391 | ret 12 |
| 2392 | end |
| 2393 | if or eq get r 4 0 eq get r 5 0 |
| 2394 | ret 12 |
| 2395 | end |
| 2396 | if or eq get r 6 0 ne get r 7 0 |
| 2397 | ret 12 |
| 2398 | end |
| 2399 | for g ulong 6 |
| 2400 | bits = ulong 0 |
| 2401 | for j ulong 4 |
| 2402 | bits = or bits get r add 8 add mul g 4 j |
| 2403 | end |
| 2404 | if eq bits 0 |
| 2405 | ret 12 |
| 2406 | end |
| 2407 | end |
| 2408 | ret 0 |
| 2409 | end |
| 2410 | |
| 2411 | fn actbind r view ulong 32 c view ulong 32 to ulong |
| 2412 | for g ulong 6 |
| 2413 | diff = ulong 0 |
| 2414 | for j ulong 4 |
| 2415 | ix = add 8 add mul g 4 j |
| 2416 | diff = or diff xor get r ix get c ix |
| 2417 | end |
| 2418 | if ne diff 0 |
| 2419 | ret add 40 g |
| 2420 | end |
| 2421 | end |
| 2422 | ret 0 |
| 2423 | end |
| 2424 | |
| 2425 | fn actauth r view ulong 32 c view ulong 32 to ulong |
| 2426 | if ne get c 0 1 |
| 2427 | ret 11 |
| 2428 | end |
| 2429 | if or ne get c 3 0 or ne get c 6 0 ne get c 7 0 |
| 2430 | ret 12 |
| 2431 | end |
| 2432 | if ne get c 1 1 |
| 2433 | ret 30 |
| 2434 | end |
| 2435 | if ne get r 4 get c 4 |
| 2436 | ret 31 |
| 2437 | end |
| 2438 | if ne get r 5 get c 5 |
| 2439 | ret 32 |
| 2440 | end |
| 2441 | if ge get c 2 get r 6 |
| 2442 | ret 33 |
| 2443 | end |
| 2444 | ret actbind r c |
| 2445 | end |
| 2446 | |
| 2447 | fn actstart r edit ulong 32 c view ulong 32 to ulong |
| 2448 | err = actvalid r |
| 2449 | if ne err 0 |
| 2450 | ret err |
| 2451 | end |
| 2452 | if eq get r 1 3 |
| 2453 | ret 21 |
| 2454 | end |
| 2455 | if eq get r 1 4 |
| 2456 | ret 22 |
| 2457 | end |
| 2458 | if ne get r 1 1 |
| 2459 | ret 20 |
| 2460 | end |
| 2461 | if ge get r 2 get r 3 |
| 2462 | ret 23 |
| 2463 | end |
| 2464 | err = actauth r c |
| 2465 | if ne err 0 |
| 2466 | ret err |
| 2467 | end |
| 2468 | put r 2 add get r 2 1 |
| 2469 | put r 1 2 |
| 2470 | ret 0 |
| 2471 | end |
| 2472 | |
| 2473 | fn actlost r edit ulong 32 to ulong |
| 2474 | err = actvalid r |
| 2475 | if ne err 0 |
| 2476 | ret err |
| 2477 | end |
| 2478 | if eq get r 1 3 |
| 2479 | ret 0 |
| 2480 | end |
| 2481 | if ne get r 1 2 |
| 2482 | ret 20 |
| 2483 | end |
| 2484 | put r 1 3 |
| 2485 | ret 0 |
| 2486 | end |
| 2487 | |
| 2488 | note A trusted receipt maps outcome 0 unknown,1 definitely not performed,2 performed. |
| 2489 | note Not-found or timeout is outcome 0 unless the provider proves absence and quiescence. |
| 2490 | note Receipt words 2/3: exact attempt and verification=1. Words 6/7 must be zero. |
| 2491 | note Recording an observed outcome is permitted after expiry; another attempt is not. |
| 2492 | fn actrecon r edit ulong 32 s view ulong 32 to ulong |
| 2493 | err = actvalid r |
| 2494 | if ne err 0 |
| 2495 | ret err |
| 2496 | end |
| 2497 | if not or eq get r 1 2 eq get r 1 3 |
| 2498 | ret 20 |
| 2499 | end |
| 2500 | if ne get s 0 1 |
| 2501 | ret 11 |
| 2502 | end |
| 2503 | if or gt get s 1 2 or ne get s 6 0 ne get s 7 0 |
| 2504 | ret 12 |
| 2505 | end |
| 2506 | if ne get s 3 1 |
| 2507 | ret 36 |
| 2508 | end |
| 2509 | if ne get s 2 get r 2 |
| 2510 | ret 35 |
| 2511 | end |
| 2512 | if ne get s 4 get r 4 |
| 2513 | ret 31 |
| 2514 | end |
| 2515 | if ne get s 5 get r 5 |
| 2516 | ret 32 |
| 2517 | end |
| 2518 | err = actbind r s |
| 2519 | if ne err 0 |
| 2520 | ret err |
| 2521 | end |
| 2522 | if eq get s 1 0 |
| 2523 | put r 1 3 |
| 2524 | ret 21 |
| 2525 | end |
| 2526 | if eq get s 1 1 |
| 2527 | put r 1 1 |
| 2528 | else |
| 2529 | put r 1 4 |
| 2530 | end |
| 2531 | ret 0 |
| 2532 | end |
| 2533 | note Omega Node durable journal and restart recovery (DRA-289). Concatenate |
| 2534 | note after transport.om, pool.om, super.om, sched.om and |
| 2535 | note seed/authority/action.om, before auth.om, which journals through it. |
| 2536 | note While no journal is open every append succeeds without writing, so the |
| 2537 | note authority layer also runs memory-only. |
| 2538 | note File DIR/journal: a 64-byte header, then 128-byte records. Header words: |
| 2539 | note 0 magic, 1 format version (2 for a file this build creates; 1 for one |
| 2540 | note a format-1 creator made, read and continued, see FORMAT), 2 record |
| 2541 | note size 128, 3..6 objective identity, |
| 2542 | note 7 FNV-1a 64 of words 0..6. Record words: 0 type, 1 seq (1, 2, 3, ... |
| 2543 | note with no gap), 2 fence, 3..14 payload, 15 FNV-1a 64 of words 0..14. |
| 2544 | note Types and payloads: |
| 2545 | note 1 OPEN 3 pid, 4 authority generation of the new creator, 5 the |
| 2546 | note scheduler policy version SPVER it was written under (0 |
| 2547 | note from a format-1 creator) |
| 2548 | note 2 ADMIT 3 job id, 4 dependency topics, 5 flags, 6 resource request, |
| 2549 | note 7 table slot |
| 2550 | note 3 START 3..6 operation identity, 7 class, 8 job id, 9 attempt, 10 gen |
| 2551 | note 4 DONE 3..6 operation identity, 7 outcome (0 UNKNOWN, 1 not |
| 2552 | note performed, 2 performed), 8 job id, 9 how (1 reported, |
| 2553 | note 2 worker lost, 3 in flight at restart, 4 key present, |
| 2554 | note 5 key absent, 6 no external effect) |
| 2555 | note 5 RELEASE 3 job id, 4 outcome (the job's outcome; 300 cancelled) |
| 2556 | note 6 REVOKE 3 new generation |
| 2557 | note 7 PAUSE 3 1 paused, 0 resumed |
| 2558 | note 8 CANCEL 3 job id |
| 2559 | note 9 OPER an operator command the creator committed (DRA-291): 3 |
| 2560 | note command id (never 0, never repeated), 4 op, 5 target, |
| 2561 | note 6..9 arguments, 10 view version it was acknowledged at. |
| 2562 | note Kept in order in a table of 128 for the creator to replay; |
| 2563 | note a reader without type 9 refuses it as unknown (24). |
| 2564 | note 10 RESULT the result an objective ended with (DRA-292): 3 objective |
| 2565 | note index (< 64), 4 result (1 PASS, else the failure; not 0), |
| 2566 | note 5 model turns performed. At most one per objective; a |
| 2567 | note restart reports it and never resumes that objective. A |
| 2568 | note reader without type 10 refuses it as unknown (24). |
| 2569 | note 11 POLICY 3 policy, 4 window, 5 dispatch budget, 6 bypass budget, |
| 2570 | note 7 ticket lifetime, 8 capacities (packed like a request): |
| 2571 | note the whole setting; the last one wins |
| 2572 | note 12 GOAL 3 job id, 4 topics, 5 estimate ms, 6 uncertainty ms |
| 2573 | note 13 FAIR 3 dispatch event, 4 assigned slot bitmap, 5 bypassed slot |
| 2574 | note bitmap, 6 cursor, 7 reserved slot + 1, 8 slots a |
| 2575 | note publication made ready (sched.om's ring) |
| 2576 | note 14 FORMAT 3 the format version the records after it follow (2). In |
| 2577 | note a version-1 file the first creator of this format writes |
| 2578 | note it once, before its OPEN, so a format-1 reader refuses |
| 2579 | note the file from then on (an unknown type, 24) instead of |
| 2580 | note continuing it. |
| 2581 | note A header of another version is refused (21). Records keep the format |
| 2582 | note of the incarnation that wrote them: in a version-1 file, an OPEN with |
| 2583 | note word 5 zero starts a format-1 incarnation, whose ADMITs carry no |
| 2584 | note request or slot (request 0, the lowest slot no unreleased job holds) |
| 2585 | note and which may hold no POLICY, GOAL or FAIR (25). An OPEN of another |
| 2586 | note SPVER, a format-1 OPEN after FORMAT or in a version-2 file, an OPEN of |
| 2587 | note this format before FORMAT in a version-1 file, and a FORMAT anywhere |
| 2588 | note else are refused at replay (33). |
| 2589 | note Order: a transition is appended and fdatasync'd before anything acts on |
| 2590 | note it or publishes it. An append whose write or sync fails marks the journal |
| 2591 | note failed and every later append is refused, so nothing is acted on that |
| 2592 | note might not be durable. |
| 2593 | note Fencing: DIR/lock is held with flock(LOCK_EX) for the creator's life. It |
| 2594 | note is opened close-on-exec, so tools never hold it, but forked workers do: |
| 2595 | note a new creator cannot open the journal until every worker of the old one |
| 2596 | note has exited. Each open appends OPEN with the next fence number, and every |
| 2597 | note record carries the fence of the creator that wrote it. |
| 2598 | note Torn tail: a partial last record, or a last full record whose checksum or |
| 2599 | note seq is wrong, was never acknowledged and is cut off. A bad record that is |
| 2600 | note not the last is corruption: the open is refused and nothing is changed. |
| 2601 | note Compatibility: a header whose magic, record size or checksum differs, |
| 2602 | note or whose version is neither 1 nor 2, is refused (21), never |
| 2603 | note reinterpreted; an objective identity that differs is refused (22). A |
| 2604 | note version-1 file keeps its header when this build continues it. |
| 2605 | text jnjour "/journal" |
| 2606 | text jnlock "/lock" |
| 2607 | text jnkey "/k." |
| 2608 | global jon ulong 1 |
| 2609 | global jbad ulong 1 |
| 2610 | global jfd int 1 |
| 2611 | global jlk int 1 |
| 2612 | global jseq ulong 1 |
| 2613 | global jsz ulong 1 |
| 2614 | global jfence ulong 1 |
| 2615 | global jgen ulong 1 |
| 2616 | global jcrash ulong 1 |
| 2617 | global jdir uchar 128 |
| 2618 | global jdn ulong 1 |
| 2619 | global jpath uchar 160 |
| 2620 | global jact ulong 1024 |
| 2621 | global jjob ulong 2048 |
| 2622 | global jstat ulong 8 |
| 2623 | note jfmt: 0 the open file's header version, 1 1 once its records follow |
| 2624 | note this format (a version-2 file, or FORMAT seen), 2 1 while replaying a |
| 2625 | note format-1 incarnation. |
| 2626 | global jfmt ulong 3 |
| 2627 | note Policy replay tables (DRA-398), per job entry k: jreq request, jslot |
| 2628 | note slot at admission, jbyp bypass count, jrdy 1 once a publication made |
| 2629 | note it ready; jsl slot -> job entry + 1 while admitted and not released; |
| 2630 | note jpol 0 seen, 1..5 policy words, 6 caps; jgoal per k: 0 seen, 1 |
| 2631 | note topics, 2 estimate, 3 uncertainty; jfair 0 last event, 1 cursor, 2 |
| 2632 | note reserved job entry + 1, 3 seen. |
| 2633 | global jreq ulong 256 |
| 2634 | global jslot ulong 256 |
| 2635 | global jbyp ulong 256 |
| 2636 | global jrdy ulong 256 |
| 2637 | global jsl ulong 64 |
| 2638 | global jpol ulong 8 |
| 2639 | global jgoal ulong 1024 |
| 2640 | global jfair ulong 4 |
| 2641 | global jopr ulong 1024 |
| 2642 | global jnop ulong 1 |
| 2643 | global jres ulong 128 |
| 2644 | |
| 2645 | note Fault injection for the recovery tests: exit 77 at point p when armed. |
| 2646 | fn jn_hit p ulong to void |
| 2647 | if and gt p 0 eq get jcrash 0 p |
| 2648 | exit 77 |
| 2649 | end |
| 2650 | end |
| 2651 | |
| 2652 | fn jn_sum r view ulong 16 n ulong to ulong |
| 2653 | h = ulong 14695981039346656037 |
| 2654 | for i ulong n bound 16 |
| 2655 | v = get r i |
| 2656 | for b ulong 8 |
| 2657 | h = mul xor h and shr v mul b 8 255 1099511628211 |
| 2658 | end |
| 2659 | end |
| 2660 | ret h |
| 2661 | end |
| 2662 | |
| 2663 | fn jn_enc r view ulong 16 b edit uchar 128 to void |
| 2664 | for i ulong 16 |
| 2665 | v = get r i |
| 2666 | for k ulong 8 |
| 2667 | put b add mul i 8 k uchar and shr v mul k 8 255 |
| 2668 | end |
| 2669 | end |
| 2670 | end |
| 2671 | |
| 2672 | fn jn_dec b view uchar 128 r edit ulong 16 to void |
| 2673 | for i ulong 16 |
| 2674 | v = ulong 0 |
| 2675 | for k ulong 8 |
| 2676 | v = or v shl ulong get b add mul i 8 k mul k 8 |
| 2677 | end |
| 2678 | put r i v |
| 2679 | end |
| 2680 | end |
| 2681 | |
| 2682 | note DIR plus name k (0 journal, 1 lock, 2 key prefix) in jpath; the length. |
| 2683 | fn jn_name k ulong to ulong |
| 2684 | n = get jdn 0 |
| 2685 | for i ulong n bound 128 |
| 2686 | put jpath i get jdir i |
| 2687 | end |
| 2688 | m = len jnjour |
| 2689 | if eq k 1 |
| 2690 | m = len jnlock |
| 2691 | end |
| 2692 | if eq k 2 |
| 2693 | m = len jnkey |
| 2694 | end |
| 2695 | for i ulong m bound 16 |
| 2696 | b = get jnkey 0 |
| 2697 | if eq k 0 |
| 2698 | b = get jnjour i |
| 2699 | end |
| 2700 | if eq k 1 |
| 2701 | b = get jnlock i |
| 2702 | end |
| 2703 | if eq k 2 |
| 2704 | b = get jnkey i |
| 2705 | end |
| 2706 | put jpath add n i b |
| 2707 | end |
| 2708 | ret add n m |
| 2709 | end |
| 2710 | |
| 2711 | note The key path DIR/k.<v> of an idempotent effect; the length. |
| 2712 | fn jn_keyp v ulong to ulong |
| 2713 | n = jn_name 2 |
| 2714 | d = new uchar 20 0 |
| 2715 | x = v |
| 2716 | k = ulong 20 |
| 2717 | for i ulong 20 |
| 2718 | k = sub k 1 |
| 2719 | put d k uchar add rem x 10 48 |
| 2720 | x = div x 10 |
| 2721 | if eq x 0 |
| 2722 | break |
| 2723 | end |
| 2724 | end |
| 2725 | for i ulong sub 20 k bound 20 |
| 2726 | put jpath add n i get d add k i |
| 2727 | end |
| 2728 | ret add n sub 20 k |
| 2729 | end |
| 2730 | |
| 2731 | fn jn_reset to void |
| 2732 | for i ulong 1024 |
| 2733 | put jact i 0 |
| 2734 | end |
| 2735 | for i ulong 2048 |
| 2736 | put jjob i 0 |
| 2737 | end |
| 2738 | for i ulong 1024 |
| 2739 | put jopr i 0 |
| 2740 | end |
| 2741 | put jnop 0 0 |
| 2742 | for i ulong 128 |
| 2743 | put jres i 0 |
| 2744 | end |
| 2745 | for i ulong 8 |
| 2746 | put jstat i 0 |
| 2747 | put jpol i 0 |
| 2748 | end |
| 2749 | for i ulong 256 |
| 2750 | put jreq i 0 |
| 2751 | put jslot i 0 |
| 2752 | put jbyp i 0 |
| 2753 | put jrdy i 0 |
| 2754 | end |
| 2755 | for i ulong 1024 |
| 2756 | put jgoal i 0 |
| 2757 | end |
| 2758 | for i ulong 64 |
| 2759 | put jsl i 0 |
| 2760 | end |
| 2761 | for i ulong 4 |
| 2762 | put jfair i 0 |
| 2763 | end |
| 2764 | for i ulong 3 |
| 2765 | put jfmt i 0 |
| 2766 | end |
| 2767 | put jon 0 0 |
| 2768 | put jbad 0 0 |
| 2769 | put jseq 0 0 |
| 2770 | put jsz 0 0 |
| 2771 | put jfence 0 0 |
| 2772 | put jgen 0 0 |
| 2773 | end |
| 2774 | |
| 2775 | note Action table, 64 entries of 16 words: 0 used, 1..4 operation identity, |
| 2776 | note 5 class, 6 state (as action.om: 1 ready, 2 in flight, 3 UNKNOWN, |
| 2777 | note 4 performed), 7 attempt, 8 gen, 9 job id. -1 when absent. |
| 2778 | fn jn_afind r view ulong 16 to long |
| 2779 | for k ulong 64 |
| 2780 | b = mul k 16 |
| 2781 | if ne get jact b 0 |
| 2782 | m = true |
| 2783 | for i ulong 4 |
| 2784 | if ne get jact add b add 1 i get r add 3 i |
| 2785 | m = false |
| 2786 | end |
| 2787 | end |
| 2788 | if m |
| 2789 | ret long k |
| 2790 | end |
| 2791 | end |
| 2792 | end |
| 2793 | ret neg long 1 |
| 2794 | end |
| 2795 | |
| 2796 | note Job table, 256 entries of 8 words: 0 used, 1 job id, 2 deps, 3 flags, |
| 2797 | note 4 released, 5 outcome, 6 cancelled, 7 action entry + 1 of its last START. |
| 2798 | fn jn_jfind id ulong to long |
| 2799 | for k ulong 256 |
| 2800 | if and ne get jjob mul k 8 0 eq get jjob add mul k 8 1 id |
| 2801 | ret long k |
| 2802 | end |
| 2803 | end |
| 2804 | ret neg long 1 |
| 2805 | end |
| 2806 | |
| 2807 | fn jn_jnew id ulong to long |
| 2808 | for k ulong 256 |
| 2809 | b = mul k 8 |
| 2810 | if eq get jjob b 0 |
| 2811 | for i ulong 8 |
| 2812 | put jjob add b i 0 |
| 2813 | end |
| 2814 | put jjob b 1 |
| 2815 | put jjob add b 1 id |
| 2816 | ret long k |
| 2817 | end |
| 2818 | end |
| 2819 | ret neg long 1 |
| 2820 | end |
| 2821 | |
| 2822 | fn jn_maxg g ulong to void |
| 2823 | if gt g get jgen 0 |
| 2824 | put jgen 0 g |
| 2825 | end |
| 2826 | end |
| 2827 | |
| 2828 | fn jn_astart r view ulong 16 to ulong |
| 2829 | k = jn_afind r |
| 2830 | if lt k 0 |
| 2831 | for i ulong 64 |
| 2832 | if eq get jact mul i 16 0 |
| 2833 | k = long i |
| 2834 | break |
| 2835 | end |
| 2836 | end |
| 2837 | end |
| 2838 | if lt k 0 |
| 2839 | ret 25 |
| 2840 | end |
| 2841 | b = mul ulong k 16 |
| 2842 | if ne get jact b 0 |
| 2843 | if ne get jact add b 6 1 |
| 2844 | ret 25 |
| 2845 | end |
| 2846 | end |
| 2847 | put jact b 1 |
| 2848 | for i ulong 4 |
| 2849 | put jact add b add 1 i get r add 3 i |
| 2850 | end |
| 2851 | put jact add b 5 get r 7 |
| 2852 | put jact add b 6 2 |
| 2853 | put jact add b 7 get r 9 |
| 2854 | put jact add b 8 get r 10 |
| 2855 | put jact add b 9 get r 8 |
| 2856 | jn_maxg get r 10 |
| 2857 | j = jn_jfind get r 8 |
| 2858 | if ge j 0 |
| 2859 | put jjob add mul ulong j 8 7 add ulong k 1 |
| 2860 | end |
| 2861 | ret 0 |
| 2862 | end |
| 2863 | |
| 2864 | fn jn_adone r view ulong 16 to ulong |
| 2865 | k = jn_afind r |
| 2866 | if lt k 0 |
| 2867 | ret 25 |
| 2868 | end |
| 2869 | b = mul ulong k 16 |
| 2870 | s = get jact add b 6 |
| 2871 | if not or eq s 2 eq s 3 |
| 2872 | ret 25 |
| 2873 | end |
| 2874 | o = get r 7 |
| 2875 | if gt o 2 |
| 2876 | ret 25 |
| 2877 | end |
| 2878 | v = ulong 3 |
| 2879 | if eq o 1 |
| 2880 | v = 1 |
| 2881 | end |
| 2882 | if eq o 2 |
| 2883 | v = 4 |
| 2884 | end |
| 2885 | put jact add b 6 v |
| 2886 | ret 0 |
| 2887 | end |
| 2888 | |
| 2889 | note Apply one durable record to the recovery tables; 0, 24 unknown type, |
| 2890 | note 25 a transition the tables refuse (START of a performed operation, a |
| 2891 | note DONE without a START, a second ADMIT or RELEASE, a policy record that |
| 2892 | note does not hold, a POLICY, GOAL or FAIR in a format-1 incarnation), 33 an |
| 2893 | note OPEN or FORMAT the file's format does not allow (see FORMAT above). |
| 2894 | fn jn_apply r view ulong 16 to ulong |
| 2895 | t = get r 0 |
| 2896 | if eq t 1 |
| 2897 | e = jn_aopen r |
| 2898 | if ne e 0 |
| 2899 | ret e |
| 2900 | end |
| 2901 | put jfence 0 get r 2 |
| 2902 | jn_maxg get r 4 |
| 2903 | ret 0 |
| 2904 | end |
| 2905 | if eq t 2 |
| 2906 | ret jn_aadmit r |
| 2907 | end |
| 2908 | if eq t 3 |
| 2909 | ret jn_astart r |
| 2910 | end |
| 2911 | if eq t 4 |
| 2912 | ret jn_adone r |
| 2913 | end |
| 2914 | if or eq t 5 eq t 8 |
| 2915 | j = jn_jfind get r 3 |
| 2916 | if lt j 0 |
| 2917 | ret 25 |
| 2918 | end |
| 2919 | b = mul ulong j 8 |
| 2920 | if eq t 8 |
| 2921 | put jjob add b 6 1 |
| 2922 | if eq get jfair 2 add ulong j 1 |
| 2923 | put jfair 2 0 |
| 2924 | end |
| 2925 | ret 0 |
| 2926 | end |
| 2927 | if ne get jjob add b 4 0 |
| 2928 | ret 25 |
| 2929 | end |
| 2930 | put jjob add b 4 1 |
| 2931 | put jjob add b 5 get r 4 |
| 2932 | s = get jslot ulong j |
| 2933 | if eq get jsl s add ulong j 1 |
| 2934 | put jsl s 0 |
| 2935 | end |
| 2936 | ret 0 |
| 2937 | end |
| 2938 | if eq t 6 |
| 2939 | jn_maxg get r 3 |
| 2940 | ret 0 |
| 2941 | end |
| 2942 | if eq t 7 |
| 2943 | put jstat 2 get r 3 |
| 2944 | ret 0 |
| 2945 | end |
| 2946 | if eq t 9 |
| 2947 | ret jn_oapply r |
| 2948 | end |
| 2949 | if eq t 10 |
| 2950 | ret jn_rapply r |
| 2951 | end |
| 2952 | if eq t 14 |
| 2953 | ret jn_afmt r |
| 2954 | end |
| 2955 | if gt t 10 |
| 2956 | if and le t 13 ne get jfmt 2 0 |
| 2957 | ret 25 |
| 2958 | end |
| 2959 | ret jn_apol r |
| 2960 | end |
| 2961 | ret 24 |
| 2962 | end |
| 2963 | |
| 2964 | note ADMIT into the tables: a new job entry with its request and slot; a |
| 2965 | note slot another unreleased job holds is refused (25). A format-1 ADMIT |
| 2966 | note records neither: request 0, the lowest slot no unreleased job holds |
| 2967 | note (25 when none is free). |
| 2968 | fn jn_aadmit r view ulong 16 to ulong |
| 2969 | if ge jn_jfind get r 3 0 |
| 2970 | ret 25 |
| 2971 | end |
| 2972 | s = get r 7 |
| 2973 | q = get r 6 |
| 2974 | if ne get jfmt 2 0 |
| 2975 | s = jn_lfree |
| 2976 | q = ulong 0 |
| 2977 | end |
| 2978 | if ge s 64 |
| 2979 | ret 25 |
| 2980 | end |
| 2981 | if ne get jsl s 0 |
| 2982 | ret 25 |
| 2983 | end |
| 2984 | j = jn_jnew get r 3 |
| 2985 | if lt j 0 |
| 2986 | ret 25 |
| 2987 | end |
| 2988 | k = ulong j |
| 2989 | put jjob add mul k 8 2 get r 4 |
| 2990 | put jjob add mul k 8 3 get r 5 |
| 2991 | put jreq k q |
| 2992 | put jslot k s |
| 2993 | put jbyp k 0 |
| 2994 | put jrdy k 0 |
| 2995 | put jsl s add k 1 |
| 2996 | ret 0 |
| 2997 | end |
| 2998 | |
| 2999 | note The lowest slot no unreleased job holds in the replay table; 64 none. |
| 3000 | fn jn_lfree to ulong |
| 3001 | for s ulong 64 |
| 3002 | if eq get jsl s 0 |
| 3003 | ret s |
| 3004 | end |
| 3005 | end |
| 3006 | ret 64 |
| 3007 | end |
| 3008 | |
| 3009 | note OPEN's format: word 5 SPVER once the file's records follow this format; |
| 3010 | note 0 (a format-1 creator) only in a version-1 file before FORMAT. 33 else. |
| 3011 | fn jn_aopen r view ulong 16 to ulong |
| 3012 | if eq get r 5 SPVER |
| 3013 | if eq get jfmt 1 0 |
| 3014 | ret 33 |
| 3015 | end |
| 3016 | put jfmt 2 0 |
| 3017 | ret 0 |
| 3018 | end |
| 3019 | if or ne get r 5 0 ne get jfmt 1 0 |
| 3020 | ret 33 |
| 3021 | end |
| 3022 | put jfmt 2 1 |
| 3023 | ret 0 |
| 3024 | end |
| 3025 | |
| 3026 | note FORMAT: only in a version-1 file, once, naming format 2. 33 else. |
| 3027 | fn jn_afmt r view ulong 16 to ulong |
| 3028 | if or ne get jfmt 0 1 ne get jfmt 1 0 |
| 3029 | ret 33 |
| 3030 | end |
| 3031 | if ne get r 3 2 |
| 3032 | ret 33 |
| 3033 | end |
| 3034 | put jfmt 1 1 |
| 3035 | ret 0 |
| 3036 | end |
| 3037 | |
| 3038 | note POLICY, GOAL and FAIR into the tables (types 11..13); 24 any other. |
| 3039 | fn jn_apol r view ulong 16 to ulong |
| 3040 | t = get r 0 |
| 3041 | if eq t 11 |
| 3042 | if not sched_polok get r 3 get r 4 get r 5 get r 6 get r 7 |
| 3043 | ret 25 |
| 3044 | end |
| 3045 | put jpol 0 1 |
| 3046 | for i ulong 6 |
| 3047 | put jpol add i 1 get r add i 3 |
| 3048 | end |
| 3049 | ret 0 |
| 3050 | end |
| 3051 | if eq t 12 |
| 3052 | j = jn_jfind get r 3 |
| 3053 | if lt j 0 |
| 3054 | ret 25 |
| 3055 | end |
| 3056 | b = mul ulong j 4 |
| 3057 | put jgoal b 1 |
| 3058 | for i ulong 3 |
| 3059 | put jgoal add b add i 1 get r add i 4 |
| 3060 | end |
| 3061 | ret 0 |
| 3062 | end |
| 3063 | if eq t 13 |
| 3064 | ret jn_afair r |
| 3065 | end |
| 3066 | ret 24 |
| 3067 | end |
| 3068 | |
| 3069 | note FAIR into the tables. A decision record (word 8 zero) must follow the |
| 3070 | note last event; a readiness record (word 8 the slots a publication made |
| 3071 | note ready, nothing assigned or bypassed) may share it, since publication |
| 3072 | note does not count as a dispatch event. The cursor must lie in the |
| 3073 | note table; assigned and bypassed slots must be disjoint and hold live |
| 3074 | note ready jobs (no deps, or made ready by an earlier readiness record); |
| 3075 | note slots made ready must hold live waiting jobs; the reserved slot a live |
| 3076 | note ready job this record did not assign. Live: admitted, not released, |
| 3077 | note not cancelled. Nothing changes otherwise (25). Assigned jobs' bypass |
| 3078 | note counts restart at 0, bypassed ones go up by one, jobs made ready are |
| 3079 | note marked ready. |
| 3080 | fn jn_afair r view ulong 16 to ulong |
| 3081 | am = get r 4 |
| 3082 | bm = get r 5 |
| 3083 | rd = get r 8 |
| 3084 | if not jn_fevt get r 3 or am bm rd |
| 3085 | ret 25 |
| 3086 | end |
| 3087 | if or ge get r 6 64 ne and am bm 0 |
| 3088 | ret 25 |
| 3089 | end |
| 3090 | if or not jn_fok or am bm 1 not jn_fok rd 2 |
| 3091 | ret 25 |
| 3092 | end |
| 3093 | rs = get r 7 |
| 3094 | if gt rs 64 |
| 3095 | ret 25 |
| 3096 | end |
| 3097 | rk = ulong 0 |
| 3098 | if gt rs 0 |
| 3099 | if ne and shr am sub rs 1 1 0 |
| 3100 | ret 25 |
| 3101 | end |
| 3102 | if not jn_fok shl ulong 1 sub rs 1 1 |
| 3103 | ret 25 |
| 3104 | end |
| 3105 | rk = get jsl sub rs 1 |
| 3106 | end |
| 3107 | for s ulong 64 |
| 3108 | if ne and shr am s 1 0 |
| 3109 | put jbyp sub get jsl s 1 0 |
| 3110 | end |
| 3111 | if ne and shr bm s 1 0 |
| 3112 | k = sub get jsl s 1 |
| 3113 | put jbyp k add get jbyp k 1 |
| 3114 | end |
| 3115 | if ne and shr rd s 1 0 |
| 3116 | put jrdy sub get jsl s 1 1 |
| 3117 | end |
| 3118 | end |
| 3119 | put jfair 0 get r 3 |
| 3120 | put jfair 1 get r 6 |
| 3121 | put jfair 2 rk |
| 3122 | put jfair 3 1 |
| 3123 | ret 0 |
| 3124 | end |
| 3125 | |
| 3126 | note Event order of a FAIR at event ev with assigned or bypassed slots ab |
| 3127 | note and slots made ready rd: a readiness record (ab empty) is never also a |
| 3128 | note decision, even as the first FAIR; decisions come strictly after the |
| 3129 | note last event, readiness at or after it. |
| 3130 | fn jn_fevt ev ulong ab ulong rd ulong to bool |
| 3131 | if and ne rd 0 ne ab 0 |
| 3132 | ret false |
| 3133 | end |
| 3134 | if eq get jfair 3 0 |
| 3135 | ret true |
| 3136 | end |
| 3137 | if ne rd 0 |
| 3138 | ret ge ev get jfair 0 |
| 3139 | end |
| 3140 | ret gt ev get jfair 0 |
| 3141 | end |
| 3142 | |
| 3143 | note Does every slot set in bitmap m hold a live job that is ready (kind 1: |
| 3144 | note no deps, or made ready) or waiting (kind 2: deps, not made ready)? |
| 3145 | fn jn_fok m ulong kind ulong to bool |
| 3146 | for s ulong 64 |
| 3147 | if ne and shr m s 1 0 |
| 3148 | x = get jsl s |
| 3149 | if eq x 0 |
| 3150 | ret false |
| 3151 | end |
| 3152 | k = sub x 1 |
| 3153 | b = mul k 8 |
| 3154 | if or ne get jjob add b 4 0 ne get jjob add b 6 0 |
| 3155 | ret false |
| 3156 | end |
| 3157 | w = and ne get jjob add b 2 0 eq get jrdy k 0 |
| 3158 | if and w eq kind 1 |
| 3159 | ret false |
| 3160 | end |
| 3161 | if and not w eq kind 2 |
| 3162 | ret false |
| 3163 | end |
| 3164 | end |
| 3165 | end |
| 3166 | ret true |
| 3167 | end |
| 3168 | |
| 3169 | note OPER into the operator table: 25 for command id 0, an id already |
| 3170 | note there, or a full table. Entry k: 0 id, 1 op, 2 target, 3..6 arguments, |
| 3171 | note 7 view version. |
| 3172 | fn jn_oapply r view ulong 16 to ulong |
| 3173 | n = get jnop 0 |
| 3174 | if or eq get r 3 0 ge n 128 |
| 3175 | ret 25 |
| 3176 | end |
| 3177 | if ge jn_ofind get r 3 0 |
| 3178 | ret 25 |
| 3179 | end |
| 3180 | for w ulong 8 |
| 3181 | put jopr add mul n 8 w get r add 3 w |
| 3182 | end |
| 3183 | put jnop 0 add n 1 |
| 3184 | ret 0 |
| 3185 | end |
| 3186 | |
| 3187 | note RESULT into the result table (2 words per objective: result, turns): |
| 3188 | note 25 for an index past 63, result 0, or an objective that has one. |
| 3189 | fn jn_rapply r view ulong 16 to ulong |
| 3190 | i = get r 3 |
| 3191 | if or ge i 64 eq get r 4 0 |
| 3192 | ret 25 |
| 3193 | end |
| 3194 | if ne get jres mul i 2 0 |
| 3195 | ret 25 |
| 3196 | end |
| 3197 | put jres mul i 2 get r 4 |
| 3198 | put jres add mul i 2 1 get r 5 |
| 3199 | ret 0 |
| 3200 | end |
| 3201 | |
| 3202 | note Operator table index of command id, -1 when absent. |
| 3203 | fn jn_ofind id ulong to long |
| 3204 | for k ulong get jnop 0 bound 128 |
| 3205 | if eq get jopr mul k 8 id |
| 3206 | ret long k |
| 3207 | end |
| 3208 | end |
| 3209 | ret neg long 1 |
| 3210 | end |
| 3211 | |
| 3212 | fn jn_le64 b view uchar 128 off ulong to ulong |
| 3213 | v = ulong 0 |
| 3214 | for k ulong 8 |
| 3215 | v = or v shl ulong get b add off k mul k 8 |
| 3216 | end |
| 3217 | ret v |
| 3218 | end |
| 3219 | |
| 3220 | note Make DIR's entry for a new journal durable. |
| 3221 | fn jn_dsync to ulong |
| 3222 | fd = fopen jdir 0 get jdn 0 540672 0 |
| 3223 | if lt fd 0 |
| 3224 | ret 27 |
| 3225 | end |
| 3226 | r = fsync int fd |
| 3227 | x = fclose int fd |
| 3228 | if ne r 0 |
| 3229 | ret 27 |
| 3230 | end |
| 3231 | ret 0 |
| 3232 | end |
| 3233 | |
| 3234 | note Create the header of an empty journal, or check an existing one. |
| 3235 | fn jn_head fd int obj view ulong 4 to ulong |
| 3236 | st = new uchar 128 0 |
| 3237 | h = new ulong 16 0 |
| 3238 | b = new uchar 128 0 |
| 3239 | if ne fstat fd st 0 128 0 |
| 3240 | ret 27 |
| 3241 | end |
| 3242 | size = jn_le64 st 48 |
| 3243 | if lt size 64 |
| 3244 | if ne ftrunc fd 0 0 |
| 3245 | ret 27 |
| 3246 | end |
| 3247 | put h 0 5786624599 |
| 3248 | put h 1 2 |
| 3249 | put jfmt 0 2 |
| 3250 | put jfmt 1 1 |
| 3251 | put h 2 128 |
| 3252 | for i ulong 4 |
| 3253 | put h add 3 i get obj i |
| 3254 | end |
| 3255 | put h 7 jn_sum h 7 |
| 3256 | jn_enc h b |
| 3257 | if ne pwrite fd b 0 64 0 64 |
| 3258 | ret 27 |
| 3259 | end |
| 3260 | if ne fsync fd 0 |
| 3261 | ret 27 |
| 3262 | end |
| 3263 | put jsz 0 64 |
| 3264 | ret jn_dsync |
| 3265 | end |
| 3266 | if ne pread fd b 0 64 0 64 |
| 3267 | ret 27 |
| 3268 | end |
| 3269 | jn_dec b h |
| 3270 | s = jn_sum h 7 |
| 3271 | if or ne get h 0 5786624599 ne get h 7 s |
| 3272 | ret 21 |
| 3273 | end |
| 3274 | if ne get h 2 128 |
| 3275 | ret 21 |
| 3276 | end |
| 3277 | if and ne get h 1 1 ne get h 1 2 |
| 3278 | ret 21 |
| 3279 | end |
| 3280 | put jfmt 0 get h 1 |
| 3281 | if eq get h 1 2 |
| 3282 | put jfmt 1 1 |
| 3283 | end |
| 3284 | for i ulong 4 |
| 3285 | if ne get h add 3 i get obj i |
| 3286 | ret 22 |
| 3287 | end |
| 3288 | end |
| 3289 | put jsz 0 size |
| 3290 | ret 0 |
| 3291 | end |
| 3292 | |
| 3293 | fn jn_cut fd int off ulong to ulong |
| 3294 | if ne ftrunc fd long off 0 |
| 3295 | ret 27 |
| 3296 | end |
| 3297 | if ne fdsync fd 0 |
| 3298 | ret 27 |
| 3299 | end |
| 3300 | put jstat 0 add get jstat 0 sub get jsz 0 off |
| 3301 | put jsz 0 off |
| 3302 | ret 0 |
| 3303 | end |
| 3304 | |
| 3305 | note Record i into r: 0 well formed (checksum and seq), 1 not, 27 I/O. |
| 3306 | fn jn_rd fd int i ulong r edit ulong 16 to ulong |
| 3307 | b = new uchar 128 0 |
| 3308 | off = long add 64 mul i 128 |
| 3309 | if ne pread fd b 0 128 off 128 |
| 3310 | ret 27 |
| 3311 | end |
| 3312 | jn_dec b r |
| 3313 | s = jn_sum r 15 |
| 3314 | if ne get r 15 s |
| 3315 | ret 1 |
| 3316 | end |
| 3317 | if ne get r 1 add i 1 |
| 3318 | ret 1 |
| 3319 | end |
| 3320 | ret 0 |
| 3321 | end |
| 3322 | |
| 3323 | note Read every record into the tables; cut a torn tail; refuse corruption. |
| 3324 | fn jn_scan fd int to ulong |
| 3325 | r = new ulong 16 0 |
| 3326 | n = div sub get jsz 0 64 128 |
| 3327 | rm = rem sub get jsz 0 64 128 |
| 3328 | for i ulong n bound 10000000 |
| 3329 | g = jn_rd fd i r |
| 3330 | if eq g 27 |
| 3331 | ret 27 |
| 3332 | end |
| 3333 | if eq g 1 |
| 3334 | if and eq add i 1 n eq rm 0 |
| 3335 | ret jn_cut fd add 64 mul i 128 |
| 3336 | end |
| 3337 | ret 23 |
| 3338 | end |
| 3339 | e = jn_apply r |
| 3340 | if ne e 0 |
| 3341 | ret e |
| 3342 | end |
| 3343 | put jseq 0 add i 1 |
| 3344 | put jstat 1 add i 1 |
| 3345 | end |
| 3346 | if gt rm 0 |
| 3347 | ret jn_cut fd add 64 mul n 128 |
| 3348 | end |
| 3349 | ret 0 |
| 3350 | end |
| 3351 | |
| 3352 | note Append record r (caller sets word 0 and the payload) and make it |
| 3353 | note durable, after the dispatch decisions waiting in sched.om's replay |
| 3354 | note ring (jn_drain), so no transition is durable before the scheduling |
| 3355 | note that came before it. The tables check and take the transition first |
| 3356 | note (jn_apply changes nothing when it refuses), so a refused transition is |
| 3357 | note never written. 0, a table refusal, 28 write, 29 sync, 30 failed |
| 3358 | note earlier. After 28 or 29 the journal is failed: the transition may or |
| 3359 | note may not be durable, so the caller must not act on it, and nothing more |
| 3360 | note is written. Without an open journal: 0. |
| 3361 | fn jn_append r edit ulong 16 to ulong |
| 3362 | e = jn_drain |
| 3363 | if ne e 0 |
| 3364 | ret e |
| 3365 | end |
| 3366 | ret jn_put r |
| 3367 | end |
| 3368 | |
| 3369 | note Write the replay ring as FAIR records and empty it; then, when the |
| 3370 | note cursor or the reservation moved without a record (a window scan that |
| 3371 | note assigned nothing), one FAIR with empty bitmaps. 0 or as jn_put; on |
| 3372 | note failure the ring keeps its records and dispatch stops when it fills. |
| 3373 | note The ring is attached by jn_recover, once the scheduler holds the |
| 3374 | note journaled state; before that, or without an open journal, it is |
| 3375 | note emptied and nothing is written. |
| 3376 | fn jn_drain to ulong |
| 3377 | if or eq get jon 0 0 not sched_jattached |
| 3378 | sched_frclear |
| 3379 | ret 0 |
| 3380 | end |
| 3381 | r = new ulong 16 0 |
| 3382 | n = sched_frn |
| 3383 | for k ulong n bound 64 |
| 3384 | for i ulong 16 |
| 3385 | put r i 0 |
| 3386 | end |
| 3387 | put r 0 13 |
| 3388 | for i ulong 6 |
| 3389 | put r add 3 i sched_frw k i |
| 3390 | end |
| 3391 | e = jn_put r |
| 3392 | if ne e 0 |
| 3393 | ret e |
| 3394 | end |
| 3395 | end |
| 3396 | sched_frclear |
| 3397 | rk = ulong 0 |
| 3398 | rs = sched_stat 29 |
| 3399 | if gt rs 0 |
| 3400 | rk = get jsl sub rs 1 |
| 3401 | end |
| 3402 | if and eq sched_stat 28 get jfair 1 eq rk get jfair 2 |
| 3403 | ret 0 |
| 3404 | end |
| 3405 | if eq sched_stat 2 get jfair 0 |
| 3406 | ret 0 |
| 3407 | end |
| 3408 | for i ulong 16 |
| 3409 | put r i 0 |
| 3410 | end |
| 3411 | put r 0 13 |
| 3412 | put r 3 sched_stat 2 |
| 3413 | put r 6 sched_stat 28 |
| 3414 | put r 7 rs |
| 3415 | ret jn_put r |
| 3416 | end |
| 3417 | |
| 3418 | note jn_append without the drain. |
| 3419 | fn jn_put r edit ulong 16 to ulong |
| 3420 | if eq get jon 0 0 |
| 3421 | ret 0 |
| 3422 | end |
| 3423 | if ne get jbad 0 0 |
| 3424 | ret 30 |
| 3425 | end |
| 3426 | b = new uchar 128 0 |
| 3427 | fd = get jfd 0 |
| 3428 | put r 1 add get jseq 0 1 |
| 3429 | put r 2 get jfence 0 |
| 3430 | put r 15 jn_sum r 15 |
| 3431 | e = jn_apply r |
| 3432 | if ne e 0 |
| 3433 | ret e |
| 3434 | end |
| 3435 | jn_enc r b |
| 3436 | off = long add 64 mul get jseq 0 128 |
| 3437 | if and eq get jcrash 0 6 eq get r 0 4 |
| 3438 | x = pwrite fd b 0 60 off |
| 3439 | exit 77 |
| 3440 | end |
| 3441 | if ne pwrite fd b 0 128 off 128 |
| 3442 | put jbad 0 1 |
| 3443 | ret 28 |
| 3444 | end |
| 3445 | if ne fdsync fd 0 |
| 3446 | put jbad 0 1 |
| 3447 | ret 29 |
| 3448 | end |
| 3449 | put jseq 0 add get jseq 0 1 |
| 3450 | put jsz 0 add get jsz 0 128 |
| 3451 | ret 0 |
| 3452 | end |
| 3453 | |
| 3454 | fn jn_rec t ulong a ulong b ulong c ulong to ulong |
| 3455 | r = new ulong 16 0 |
| 3456 | put r 0 t |
| 3457 | put r 3 a |
| 3458 | put r 4 b |
| 3459 | put r 5 c |
| 3460 | ret jn_append r |
| 3461 | end |
| 3462 | |
| 3463 | note Open (or create) the journal in directory dir (dn bytes) for objective |
| 3464 | note obj, waiting up to ms for the lock. 0, 20 fenced (another creator or |
| 3465 | note its workers hold the lock), 21 incompatible, 22 other objective, 23 |
| 3466 | note corrupt, 24/25 refused record, 26 path too long, 27 I/O error. |
| 3467 | fn jn_open dir view uchar 128 dn ulong obj view ulong 4 ms ulong to ulong |
| 3468 | if or eq dn 0 gt dn 100 |
| 3469 | ret 26 |
| 3470 | end |
| 3471 | jn_reset |
| 3472 | for i ulong dn bound 128 |
| 3473 | put jdir i get dir i |
| 3474 | end |
| 3475 | put jdn 0 dn |
| 3476 | r = makedir jdir 0 dn 448 |
| 3477 | if and lt r 0 ne r neg long 17 |
| 3478 | ret 27 |
| 3479 | end |
| 3480 | n = jn_name 1 |
| 3481 | lk = fopen jpath 0 n 524354 384 |
| 3482 | if lt lk 0 |
| 3483 | ret 27 |
| 3484 | end |
| 3485 | got = false |
| 3486 | for t ulong add ms 1 bound 100000 |
| 3487 | f = flock int lk 6 |
| 3488 | if eq f 0 |
| 3489 | got = true |
| 3490 | break |
| 3491 | end |
| 3492 | if ne f neg long 11 |
| 3493 | break |
| 3494 | end |
| 3495 | x = sleepns 1000000 |
| 3496 | end |
| 3497 | if not got |
| 3498 | x = fclose int lk |
| 3499 | ret 20 |
| 3500 | end |
| 3501 | put jlk 0 int lk |
| 3502 | e = jn_load obj |
| 3503 | if ne e 0 |
| 3504 | x = fclose int lk |
| 3505 | ret e |
| 3506 | end |
| 3507 | ret 0 |
| 3508 | end |
| 3509 | |
| 3510 | fn jn_load obj view ulong 4 to ulong |
| 3511 | n = jn_name 0 |
| 3512 | fd = fopen jpath 0 n 524354 384 |
| 3513 | if lt fd 0 |
| 3514 | ret 27 |
| 3515 | end |
| 3516 | e = jn_head int fd obj |
| 3517 | if eq e 0 |
| 3518 | e = jn_scan int fd |
| 3519 | end |
| 3520 | if ne e 0 |
| 3521 | x = fclose int fd |
| 3522 | ret e |
| 3523 | end |
| 3524 | put jfd 0 int fd |
| 3525 | put jon 0 1 |
| 3526 | if eq get jfmt 1 0 |
| 3527 | e = jn_rec 14 2 0 0 |
| 3528 | if ne e 0 |
| 3529 | ret e |
| 3530 | end |
| 3531 | end |
| 3532 | put jfence 0 add get jfence 0 1 |
| 3533 | put jgen 0 add get jgen 0 1 |
| 3534 | ret jn_rec 1 ulong getpid get jgen 0 SPVER |
| 3535 | end |
| 3536 | |
| 3537 | note Close the journal (after draining the replay ring) and release the |
| 3538 | note lock (other holders: forked workers). |
| 3539 | fn jn_close to void |
| 3540 | if ne get jon 0 0 |
| 3541 | dr = jn_drain |
| 3542 | x = fclose get jfd 0 |
| 3543 | x = fclose get jlk 0 |
| 3544 | end |
| 3545 | put jon 0 0 |
| 3546 | sched_jattach false |
| 3547 | end |
| 3548 | |
| 3549 | note Probe the key of an idempotent operation: 1 present, 0 absent, 2 error. |
| 3550 | fn jn_probe v ulong to ulong |
| 3551 | st = new uchar 128 0 |
| 3552 | n = jn_keyp v |
| 3553 | r = stat jpath 0 n st 0 128 |
| 3554 | if eq r 0 |
| 3555 | ret 1 |
| 3556 | end |
| 3557 | if eq r neg long 2 |
| 3558 | ret 0 |
| 3559 | end |
| 3560 | ret 2 |
| 3561 | end |
| 3562 | |
| 3563 | note Close every START left in flight by a crash with a DONE. Class 3 (an |
| 3564 | note idempotent effect whose key DIR/k.<op word 0> is created atomically by |
| 3565 | note the effect) is decided by its key; class 2 (no external effect) was not |
| 3566 | note performed; anything else is UNKNOWN and is never run again. |
| 3567 | fn jn_settle to ulong |
| 3568 | r = new ulong 16 0 |
| 3569 | for k ulong 64 |
| 3570 | b = mul k 16 |
| 3571 | if and ne get jact b 0 eq get jact add b 6 2 |
| 3572 | for i ulong 4 |
| 3573 | put r add 3 i get jact add b add 1 i |
| 3574 | end |
| 3575 | put r 0 4 |
| 3576 | put r 8 get jact add b 9 |
| 3577 | cl = get jact add b 5 |
| 3578 | put r 7 0 |
| 3579 | put r 9 3 |
| 3580 | if eq cl 2 |
| 3581 | put r 7 1 |
| 3582 | put r 9 6 |
| 3583 | end |
| 3584 | if eq cl 3 |
| 3585 | p = jn_probe get r 3 |
| 3586 | if eq p 2 |
| 3587 | ret 27 |
| 3588 | end |
| 3589 | put r 7 add 1 p |
| 3590 | put r 9 sub 5 p |
| 3591 | end |
| 3592 | e = jn_append r |
| 3593 | if ne e 0 |
| 3594 | ret e |
| 3595 | end |
| 3596 | put jstat 3 add get jstat 3 1 |
| 3597 | end |
| 3598 | end |
| 3599 | ret 0 |
| 3600 | end |
| 3601 | |
| 3602 | note Recovery: the incarnation (the fence), then the last journaled policy |
| 3603 | note and capacities. False when the scheduler refused one; a fence past the |
| 3604 | note incarnation range (2^24 - 1) is refused, never folded onto an earlier |
| 3605 | note incarnation. |
| 3606 | fn jn_rpol to bool |
| 3607 | if ne sched_incarnate get jfence 0 0 |
| 3608 | ret false |
| 3609 | end |
| 3610 | if eq get jpol 0 0 |
| 3611 | ret true |
| 3612 | end |
| 3613 | if ne sched_cap get jpol 6 0 |
| 3614 | ret false |
| 3615 | end |
| 3616 | p = get jpol 1 |
| 3617 | w = get jpol 2 |
| 3618 | d = get jpol 3 |
| 3619 | y = get jpol 4 |
| 3620 | ret eq sched_policy p w d y get jpol 5 0 |
| 3621 | end |
| 3622 | |
| 3623 | note Creator, after jn_open and sched_new: settle in-flight operations, |
| 3624 | note set the scheduler's incarnation to the fence (proposals made for an |
| 3625 | note earlier creator are stale), restore the last journaled policy and |
| 3626 | note capacities, then for each admitted job not yet released: a performed |
| 3627 | note or UNKNOWN last operation releases it with that outcome (it never runs |
| 3628 | note again); a cancelled job is released as cancelled; any other is |
| 3629 | note admitted again, in admission order, into its recorded slot, with its |
| 3630 | note bypass count and declared goal. Last, the cursor, the dispatch event |
| 3631 | note count and the reservation (on the job's slot) from the last FAIR. |
| 3632 | note 0 or an error (31 a restore the scheduler refused). |
| 3633 | fn jn_recover c long to ulong |
| 3634 | e = jn_settle |
| 3635 | if ne e 0 |
| 3636 | ret e |
| 3637 | end |
| 3638 | if not jn_rpol |
| 3639 | ret 31 |
| 3640 | end |
| 3641 | rs = ulong 0 |
| 3642 | for k ulong 256 |
| 3643 | b = mul k 8 |
| 3644 | if and ne get jjob b 0 eq get jjob add b 4 0 |
| 3645 | o = jn_rout k |
| 3646 | if eq o 999 |
| 3647 | x = jn_readmit k |
| 3648 | if lt x 0 |
| 3649 | ret 31 |
| 3650 | end |
| 3651 | if eq get jfair 2 add k 1 |
| 3652 | rs = add ulong x 1 |
| 3653 | end |
| 3654 | put jstat 5 add get jstat 5 1 |
| 3655 | else |
| 3656 | e = jn_rec 5 get jjob add b 1 o 0 |
| 3657 | if ne e 0 |
| 3658 | ret e |
| 3659 | end |
| 3660 | put jstat 4 add get jstat 4 1 |
| 3661 | end |
| 3662 | end |
| 3663 | end |
| 3664 | if ne get jfair 3 0 |
| 3665 | if ne sched_frestore get jfair 1 get jfair 0 rs 0 |
| 3666 | ret 31 |
| 3667 | end |
| 3668 | end |
| 3669 | sched_jattach true |
| 3670 | ret 0 |
| 3671 | end |
| 3672 | |
| 3673 | note Recovery: the outcome job entry k is released with (2 performed, 0 |
| 3674 | note UNKNOWN, 300 cancelled), or 999 when it is to be admitted again. |
| 3675 | fn jn_rout k ulong to ulong |
| 3676 | b = mul k 8 |
| 3677 | o = ulong 999 |
| 3678 | a = get jjob add b 7 |
| 3679 | if gt a 0 |
| 3680 | s = get jact add mul sub a 1 16 6 |
| 3681 | if eq s 4 |
| 3682 | o = 2 |
| 3683 | end |
| 3684 | if eq s 3 |
| 3685 | o = 0 |
| 3686 | end |
| 3687 | end |
| 3688 | if and eq o 999 ne get jjob add b 6 0 |
| 3689 | o = 300 |
| 3690 | end |
| 3691 | ret o |
| 3692 | end |
| 3693 | |
| 3694 | note Recovery: admit job entry k again into its recorded slot, ready again |
| 3695 | note when a publication had made it ready, then its bypass count and goal. |
| 3696 | note The slot, or < 0 when the scheduler refuses any of them (a recorded |
| 3697 | note slot that is taken, a bypass count on a job that is not ready). |
| 3698 | fn jn_readmit k ulong to long |
| 3699 | b = mul k 8 |
| 3700 | x = sched_admits get jjob add b 1 get jjob add b 2 get jjob add b 3 get jreq k get jslot k |
| 3701 | if lt x 0 |
| 3702 | ret x |
| 3703 | end |
| 3704 | j = ulong x |
| 3705 | if ne get jrdy k 0 |
| 3706 | if lt sched_setready j 0 |
| 3707 | ret neg long 3 |
| 3708 | end |
| 3709 | end |
| 3710 | if ne get jbyp k 0 |
| 3711 | if lt sched_setbyp j get jbyp k 0 |
| 3712 | ret neg long 3 |
| 3713 | end |
| 3714 | end |
| 3715 | g = mul k 4 |
| 3716 | if ne get jgoal g 0 |
| 3717 | if lt sched_goal j get jgoal add g 1 get jgoal add g 2 get jgoal add g 3 0 |
| 3718 | ret neg long 3 |
| 3719 | end |
| 3720 | end |
| 3721 | ret x |
| 3722 | end |
| 3723 | |
| 3724 | note Admit job id durably: ADMIT, with the request and the slot it will |
| 3725 | note take, is appended before sched_admits. -4 an id already in the journal |
| 3726 | note (checked first, so a duplicate never takes or refreshes an admission |
| 3727 | note ticket), -8 a request no capacity could ever hold, -2 when sched_room |
| 3728 | note refuses (admission limit, no free slot, or ticket holders ahead), -5 |
| 3729 | note the journal refused, else the slot. |
| 3730 | fn jn_admitr id ulong deps ulong flags ulong req ulong to long |
| 3731 | if ge jn_jfind id 0 |
| 3732 | ret neg long 4 |
| 3733 | end |
| 3734 | if not sched_feas req |
| 3735 | ret neg long 8 |
| 3736 | end |
| 3737 | if not sched_room id |
| 3738 | ret neg long 2 |
| 3739 | end |
| 3740 | s = sched_slot |
| 3741 | if ge s 64 |
| 3742 | ret neg long 2 |
| 3743 | end |
| 3744 | r = new ulong 16 0 |
| 3745 | put r 0 2 |
| 3746 | put r 3 id |
| 3747 | put r 4 deps |
| 3748 | put r 5 flags |
| 3749 | put r 6 req |
| 3750 | put r 7 s |
| 3751 | if ne jn_append r 0 |
| 3752 | ret neg long 5 |
| 3753 | end |
| 3754 | jn_hit 1 |
| 3755 | ret sched_admits id deps flags req s |
| 3756 | end |
| 3757 | |
| 3758 | note jn_admitr with no resource request. |
| 3759 | fn jn_admit id ulong deps ulong flags ulong to long |
| 3760 | ret jn_admitr id deps flags 0 |
| 3761 | end |
| 3762 | |
| 3763 | note Creator: sched_policy, journaled first (POLICY, with the current |
| 3764 | note capacities). -7 refused, -5 the journal refused, else 0. |
| 3765 | fn jn_policy p ulong win ulong db ulong by ulong ttl ulong to long |
| 3766 | if not sched_polok p win db by ttl |
| 3767 | ret neg long 7 |
| 3768 | end |
| 3769 | if ne jn_pol p win db by ttl sched_caps 0 |
| 3770 | ret neg long 5 |
| 3771 | end |
| 3772 | ret sched_policy p win db by ttl |
| 3773 | end |
| 3774 | |
| 3775 | note Creator: sched_cap, journaled first (POLICY, with the current policy). |
| 3776 | note -8 refused, -5 the journal refused, else 0. |
| 3777 | fn jn_cap caps ulong to long |
| 3778 | if not sched_capok caps |
| 3779 | ret neg long 8 |
| 3780 | end |
| 3781 | if ne jn_pol sched_stat 24 sched_stat 25 sched_stat 26 sched_stat 27 sched_stat 31 caps 0 |
| 3782 | ret neg long 5 |
| 3783 | end |
| 3784 | ret sched_cap caps |
| 3785 | end |
| 3786 | |
| 3787 | fn jn_pol p ulong win ulong db ulong by ulong ttl ulong caps ulong to ulong |
| 3788 | r = new ulong 16 0 |
| 3789 | put r 0 11 |
| 3790 | put r 3 p |
| 3791 | put r 4 win |
| 3792 | put r 5 db |
| 3793 | put r 6 by |
| 3794 | put r 7 ttl |
| 3795 | put r 8 caps |
| 3796 | ret jn_append r |
| 3797 | end |
| 3798 | |
| 3799 | note Creator: sched_goal for job slot j (waiting or ready), journaled first |
| 3800 | note (GOAL). -3 no such job, -5 the journal refused, else 0. |
| 3801 | fn jn_goal j ulong pro ulong est ulong unc ulong to long |
| 3802 | if ge j 64 |
| 3803 | ret neg long 3 |
| 3804 | end |
| 3805 | s = get sjst j |
| 3806 | if or eq s 0 gt s 2 |
| 3807 | ret neg long 3 |
| 3808 | end |
| 3809 | r = new ulong 16 0 |
| 3810 | put r 0 12 |
| 3811 | put r 3 get sjid j |
| 3812 | put r 4 pro |
| 3813 | put r 5 est |
| 3814 | put r 6 unc |
| 3815 | if ne jn_append r 0 |
| 3816 | ret neg long 5 |
| 3817 | end |
| 3818 | ret sched_goal j pro est unc |
| 3819 | end |
| 3820 | |
| 3821 | note Release reviewed job slot j durably: RELEASE before sched_review. -5 |
| 3822 | note while its operation's DONE is not yet durable or on refusal. |
| 3823 | fn jn_release j ulong to long |
| 3824 | id = get sjid j |
| 3825 | k = jn_jfind id |
| 3826 | if ge k 0 |
| 3827 | a = get jjob add mul ulong k 8 7 |
| 3828 | if gt a 0 |
| 3829 | if eq get jact add mul sub a 1 16 6 2 |
| 3830 | ret neg long 5 |
| 3831 | end |
| 3832 | end |
| 3833 | end |
| 3834 | if ne jn_rec 5 id get sjout j 0 0 |
| 3835 | ret neg long 5 |
| 3836 | end |
| 3837 | jn_hit 5 |
| 3838 | ret sched_review j |
| 3839 | end |
| 3840 | |
| 3841 | fn jn_start op view ulong 4 cl ulong id ulong att ulong g ulong to ulong |
| 3842 | r = new ulong 16 0 |
| 3843 | put r 0 3 |
| 3844 | for i ulong 4 |
| 3845 | put r add 3 i get op i |
| 3846 | end |
| 3847 | put r 7 cl |
| 3848 | put r 8 id |
| 3849 | put r 9 att |
| 3850 | put r 10 g |
| 3851 | ret jn_append r |
| 3852 | end |
| 3853 | |
| 3854 | fn jn_done op view ulong 4 o ulong id ulong how ulong to ulong |
| 3855 | r = new ulong 16 0 |
| 3856 | put r 0 4 |
| 3857 | for i ulong 4 |
| 3858 | put r add 3 i get op i |
| 3859 | end |
| 3860 | put r 7 o |
| 3861 | put r 8 id |
| 3862 | put r 9 how |
| 3863 | ret jn_append r |
| 3864 | end |
| 3865 | |
| 3866 | note Getters: whether job id is in the journal, released, its outcome; a |
| 3867 | note recovery counter (0 torn bytes cut, 1 records read, 2 paused, 3 settled, |
| 3868 | note 4 released at recovery, 5 admitted again); the fence; the generation. |
| 3869 | fn jn_known id ulong to bool |
| 3870 | ret ge jn_jfind id 0 |
| 3871 | end |
| 3872 | |
| 3873 | fn jn_done_j id ulong to bool |
| 3874 | k = jn_jfind id |
| 3875 | if lt k 0 |
| 3876 | ret false |
| 3877 | end |
| 3878 | ret ne get jjob add mul ulong k 8 4 0 |
| 3879 | end |
| 3880 | |
| 3881 | fn jn_jout id ulong to ulong |
| 3882 | k = jn_jfind id |
| 3883 | if lt k 0 |
| 3884 | ret 999 |
| 3885 | end |
| 3886 | ret get jjob add mul ulong k 8 5 |
| 3887 | end |
| 3888 | |
| 3889 | fn jn_stat i ulong to ulong |
| 3890 | ret get jstat and i 7 |
| 3891 | end |
| 3892 | |
| 3893 | fn jn_fence to ulong |
| 3894 | ret get jfence 0 |
| 3895 | end |
| 3896 | |
| 3897 | fn jn_gen to ulong |
| 3898 | ret get jgen 0 |
| 3899 | end |
| 3900 | |
| 3901 | note Action table entry k word i (k < 64, i < 16). |
| 3902 | fn jn_act k ulong i ulong to ulong |
| 3903 | ret get jact add mul and k 63 16 and i 15 |
| 3904 | end |
| 3905 | |
| 3906 | note Commit operator command id: OPER (op, target t, arguments a[0..3], |
| 3907 | note view version v) appended and synced. 0, 25 refused, or a write error. |
| 3908 | fn jn_oper id ulong op ulong t ulong a view ulong 4 v ulong to ulong |
| 3909 | r = new ulong 16 0 |
| 3910 | put r 0 9 |
| 3911 | put r 3 id |
| 3912 | put r 4 op |
| 3913 | put r 5 t |
| 3914 | for i ulong 4 |
| 3915 | put r add 6 i get a i |
| 3916 | end |
| 3917 | put r 10 v |
| 3918 | ret jn_append r |
| 3919 | end |
| 3920 | |
| 3921 | note Operator commands in the journal, and word w of the k-th (as jn_oapply). |
| 3922 | fn jn_nops to ulong |
| 3923 | ret get jnop 0 |
| 3924 | end |
| 3925 | |
| 3926 | fn jn_opw k ulong w ulong to ulong |
| 3927 | ret get jopr add mul and k 127 8 and w 7 |
| 3928 | end |
| 3929 | |
| 3930 | note Commit that objective i ended with result r after t model turns: RESULT |
| 3931 | note appended and synced. 0, 25 refused, or a write error. |
| 3932 | fn jn_result i ulong r ulong t ulong to ulong |
| 3933 | x = new ulong 16 0 |
| 3934 | put x 0 10 |
| 3935 | put x 3 i |
| 3936 | put x 4 r |
| 3937 | put x 5 t |
| 3938 | ret jn_append x |
| 3939 | end |
| 3940 | |
| 3941 | note The result objective i ended with in the journal (0 none), and its |
| 3942 | note model turns. |
| 3943 | fn jn_res i ulong to ulong |
| 3944 | ret get jres mul and i 63 2 |
| 3945 | end |
| 3946 | |
| 3947 | fn jn_rturn i ulong to ulong |
| 3948 | ret get jres add mul and i 63 2 1 |
| 3949 | end |
| 3950 | |
| 3951 | note True when job id has a durable START (an operation was granted to it). |
| 3952 | fn jn_started id ulong to bool |
| 3953 | k = jn_jfind id |
| 3954 | if lt k 0 |
| 3955 | ret false |
| 3956 | end |
| 3957 | ret ne get jjob add mul ulong k 8 7 0 |
| 3958 | end |
| 3959 | note Omega Node proposals, authority and cancellation (DRA-288). Concatenate |
| 3960 | note after transport.om, pool.om, super.om, sched.om and |
| 3961 | note seed/authority/action.om, before one program with fn main. |
| 3962 | note A worker proposes and the creator alone decides and commits. A proposal |
| 3963 | note is typed and bound to its operation identity, the job id and |
| 3964 | note assignment seq, the worker slot and incarnation, the input epoch and |
| 3965 | note version it read, and the authority generation it holds. Every effect is |
| 3966 | note one action record of seed/authority/action.om per operation identity: |
| 3967 | note started with actstart, closed with actrecon or actlost. A performed |
| 3968 | note operation is never started again and an UNKNOWN one is never retried; |
| 3969 | note one with no external effect (class 2) whose worker died is closed as |
| 3970 | note not performed, so it may run again. |
| 3971 | note Control page (16384 bytes), cell of worker w at 128*w. Creator writes |
| 3972 | note 0 control seq, 8 cancel target (assignment seq, 0 none), 16 decided |
| 3973 | note proposal seq, 24 verdict, 32 generation at the decision. Worker |
| 3974 | note writes 64 control ack seq, 72 proposal seq, 80 report seq, 88 report |
| 3975 | note outcome, 96 tool wait status, 104 paused as last seen, 112 running tool |
| 3976 | note pid (its process group), 120 tool output bytes held. Word 8192 mirrors |
| 3977 | note the authority generation and 8200 the paused flag. |
| 3978 | note Proposal page: 4096 bytes per worker at 4096*w. Words 0..15: 0 version |
| 3979 | note 1, 1 kind 1, 2 job id, 3 assignment seq, 4 slot, 5 incarnation, 6 input |
| 3980 | note epoch, 7 input version, 8 generation, 9..12 operation identity, 13 class |
| 3981 | note (1 external effect, 2 no external effect, 3 idempotent external effect |
| 3982 | note whose completion is visible as a key, see journal.om), 14 and 15 zero. |
| 3983 | note Bytes 128..4095 hold the running tool's output. |
| 3984 | note Verdicts: 0 granted, 1 malformed, 2 not the worker's assignment, 3 other |
| 3985 | note incarnation, 4 stale input, 5 stale authority, 6 cancelled, 7 already |
| 3986 | note performed or UNKNOWN, 8 attempt limit, 9 refused by the action contract. |
| 3987 | note Outcomes, as action.om receipts: 0 UNKNOWN, 1 not performed, 2 performed. |
| 3988 | text audevnul "/dev/null" |
| 3989 | global aumap long 2 |
| 3990 | global auctl int 64 |
| 3991 | global auinc ulong 64 |
| 3992 | global augen ulong 1 |
| 3993 | global aurec ulong 2048 |
| 3994 | global auuse ulong 64 |
| 3995 | global aujob ulong 64 |
| 3996 | global aured ulong 64 |
| 3997 | global aupf uchar 8 |
| 3998 | global aufd int 1 |
| 3999 | |
| 4000 | fn au_cell w ulong off ulong to ulong |
| 4001 | ret mapget get aumap 0 add mul w 128 off 8 |
| 4002 | end |
| 4003 | |
| 4004 | fn au_set w ulong off ulong v ulong to void |
| 4005 | r = mapset get aumap 0 add mul w 128 off 8 v |
| 4006 | end |
| 4007 | |
| 4008 | fn au_pw w ulong i ulong to ulong |
| 4009 | ret mapget get aumap 1 add mul w 4096 mul i 8 8 |
| 4010 | end |
| 4011 | |
| 4012 | fn au_spw w ulong i ulong v ulong to void |
| 4013 | r = mapset get aumap 1 add mul w 4096 mul i 8 8 v |
| 4014 | end |
| 4015 | |
| 4016 | fn au_pfd off ulong to int |
| 4017 | v = ulong get aupf off |
| 4018 | v = or v shl ulong get aupf add off 1 8 |
| 4019 | v = or v shl ulong get aupf add off 2 16 |
| 4020 | v = or v shl ulong get aupf add off 3 24 |
| 4021 | ret int v |
| 4022 | end |
| 4023 | |
| 4024 | note Poll record at off; a negative fd keeps its bit pattern, so the kernel |
| 4025 | note ignores that record. |
| 4026 | fn au_lpfd p edit uchar 40 off ulong fd int to void |
| 4027 | u = bitcast uint fd |
| 4028 | for i ulong 4 |
| 4029 | put p add off i uchar and shr u uint mul i 8 255 |
| 4030 | end |
| 4031 | put p add off 4 1 |
| 4032 | end |
| 4033 | |
| 4034 | note Creator, after sup_init and before any spawn: map the control and |
| 4035 | note proposal pages, one control eventfd per worker slot, generation 1. |
| 4036 | note 0, -6 not creator, or a negative errno. |
| 4037 | fn auth_new c long to long |
| 4038 | if not pool_mine c |
| 4039 | ret neg long 6 |
| 4040 | end |
| 4041 | a = mapnew neg int 1 0 16384 3 33 |
| 4042 | if le a 0 |
| 4043 | ret a |
| 4044 | end |
| 4045 | b = mapnew neg int 1 0 262144 3 33 |
| 4046 | if le b 0 |
| 4047 | r = mapdrop a |
| 4048 | ret b |
| 4049 | end |
| 4050 | put aumap 0 a |
| 4051 | put aumap 1 b |
| 4052 | for i ulong 64 |
| 4053 | put auctl i neg int 1 |
| 4054 | put auinc i 0 |
| 4055 | put auuse i 0 |
| 4056 | put aujob i 0 |
| 4057 | put aured i 0 |
| 4058 | end |
| 4059 | n = mapget c 48 8 |
| 4060 | for i ulong n bound 64 |
| 4061 | e = eventfd uint 0 |
| 4062 | if lt e 0 |
| 4063 | x = auth_free |
| 4064 | ret e |
| 4065 | end |
| 4066 | put auctl i int e |
| 4067 | end |
| 4068 | put augen 0 1 |
| 4069 | r = mapset a 8192 8 1 |
| 4070 | r = mapset a 8200 8 0 |
| 4071 | ret 0 |
| 4072 | end |
| 4073 | |
| 4074 | note Creator: close the control eventfds and drop both pages. |
| 4075 | fn auth_free to long |
| 4076 | for i ulong 64 |
| 4077 | if ge get auctl i 0 |
| 4078 | r = fclose get auctl i |
| 4079 | end |
| 4080 | put auctl i neg int 1 |
| 4081 | end |
| 4082 | r = mapdrop get aumap 1 |
| 4083 | if lt r 0 |
| 4084 | ret r |
| 4085 | end |
| 4086 | ret mapdrop get aumap 0 |
| 4087 | end |
| 4088 | |
| 4089 | note Creator: start a new incarnation of worker slot w (sup_spawn). The child |
| 4090 | note inherits its incarnation number. 0 in the child, else as sup_spawn. |
| 4091 | fn auth_spawn c long w ulong to long |
| 4092 | if ge w 64 |
| 4093 | ret neg long 7 |
| 4094 | end |
| 4095 | if ne get swpid w 0 |
| 4096 | ret neg long 16 |
| 4097 | end |
| 4098 | put auinc w add get auinc w 1 |
| 4099 | ret sup_spawn c w |
| 4100 | end |
| 4101 | |
| 4102 | note Creator: post control to worker w. A nonzero target asks it to cancel |
| 4103 | note that assignment. The state words go first, then the seq, then the hint. |
| 4104 | fn au_ctl w ulong target ulong to void |
| 4105 | if gt target 0 |
| 4106 | au_set w 8 target |
| 4107 | end |
| 4108 | au_set w 0 add au_cell w 0 1 |
| 4109 | r = node_notify get auctl w |
| 4110 | end |
| 4111 | |
| 4112 | note Worker w: bounded control handler. Records the paused flag it saw and |
| 4113 | note acknowledges the control seq; returns the cancel target (0 none). |
| 4114 | fn auth_wctl w ulong to ulong |
| 4115 | s = au_cell w 0 |
| 4116 | au_set w 104 mapget get aumap 0 8200 8 |
| 4117 | au_set w 64 s |
| 4118 | ret au_cell w 8 |
| 4119 | end |
| 4120 | |
| 4121 | note Worker w: wait up to interval ns. 3 creator gone, 2 stop, 4 control, |
| 4122 | note 5 descriptor x readable or hung up (x negative: none), 1 work hint, |
| 4123 | note 0 timeout, or a negative errno. Creator loss outranks stop, stop |
| 4124 | note outranks control, control outranks output and work. Control and work |
| 4125 | note hints are drained; reread the shared state after any return. |
| 4126 | fn auth_wwait c long w ulong x int interval long to long |
| 4127 | p = new uchar 40 0 |
| 4128 | if ge w 64 |
| 4129 | ret neg long 7 |
| 4130 | end |
| 4131 | iv = interval |
| 4132 | if pool_stopped c |
| 4133 | iv = long 0 |
| 4134 | end |
| 4135 | au_lpfd p 0 get swwake w |
| 4136 | au_lpfd p 8 get swstop w |
| 4137 | au_lpfd p 16 get swpar 0 |
| 4138 | au_lpfd p 24 get auctl w |
| 4139 | au_lpfd p 32 x |
| 4140 | r = pollfds p 0 40 iv |
| 4141 | if lt r 0 |
| 4142 | ret r |
| 4143 | end |
| 4144 | if ne get p 22 0 |
| 4145 | ret 3 |
| 4146 | end |
| 4147 | if pool_stopped c |
| 4148 | ret 2 |
| 4149 | end |
| 4150 | if ne and get p 14 1 0 |
| 4151 | r = node_drain get swstop w |
| 4152 | ret 2 |
| 4153 | end |
| 4154 | if ne get p 30 0 |
| 4155 | r = node_drain get auctl w |
| 4156 | ret 4 |
| 4157 | end |
| 4158 | if ne get p 38 0 |
| 4159 | ret 5 |
| 4160 | end |
| 4161 | if ne and get p 6 1 0 |
| 4162 | r = node_drain get swwake w |
| 4163 | ret 1 |
| 4164 | end |
| 4165 | ret 0 |
| 4166 | end |
| 4167 | |
| 4168 | note Worker w: propose the effect of assignment q with operation identity |
| 4169 | note op and class (1 external effect, 2 none, 3 keyed idempotent). The words go first, then the |
| 4170 | note proposal seq, then an ack to the creator. 0 or the ack refusal. |
| 4171 | fn auth_propose w ulong q ulong op view ulong 4 class ulong to long |
| 4172 | if ge w 64 |
| 4173 | ret neg long 7 |
| 4174 | end |
| 4175 | au_spw w 0 1 |
| 4176 | au_spw w 1 1 |
| 4177 | au_spw w 2 sc_cell w 8 |
| 4178 | au_spw w 3 q |
| 4179 | au_spw w 4 w |
| 4180 | au_spw w 5 get auinc w |
| 4181 | au_spw w 6 sc_cell w 16 |
| 4182 | au_spw w 7 sc_cell w 24 |
| 4183 | au_spw w 8 mapget get aumap 0 8192 8 |
| 4184 | for i ulong 4 |
| 4185 | au_spw w add 9 i get op i |
| 4186 | end |
| 4187 | au_spw w 13 class |
| 4188 | au_spw w 14 0 |
| 4189 | au_spw w 15 0 |
| 4190 | au_set w 72 q |
| 4191 | ret sup_ack w |
| 4192 | end |
| 4193 | |
| 4194 | note Worker w: wait up to interval ns for the decision on proposal q, |
| 4195 | note handling control meanwhile. The verdict (0 granted), 102 stop, 103 |
| 4196 | note creator gone, 110 no decision yet, or a negative errno. |
| 4197 | fn auth_wdecide c long w ulong q ulong interval long to long |
| 4198 | dl = add ulong monons ulong interval |
| 4199 | for it ulong 100000000 |
| 4200 | if eq au_cell w 16 q |
| 4201 | ret long au_cell w 24 |
| 4202 | end |
| 4203 | now = ulong monons |
| 4204 | if ge now dl |
| 4205 | ret 110 |
| 4206 | end |
| 4207 | r = auth_wwait c w neg int 1 long sub dl now |
| 4208 | if lt r 0 |
| 4209 | ret r |
| 4210 | end |
| 4211 | if or eq r 2 eq r 3 |
| 4212 | ret add 100 r |
| 4213 | end |
| 4214 | if eq r 4 |
| 4215 | t = auth_wctl w |
| 4216 | end |
| 4217 | end |
| 4218 | ret 110 |
| 4219 | end |
| 4220 | |
| 4221 | note Worker w: fork the packed argv pk (pn bytes) as the leader of a new |
| 4222 | note process group, stdin on /dev/null, stdout and stderr on a pipe whose |
| 4223 | note read end is left in aufd. Both sides call setpgid, so the group exists |
| 4224 | note before either side goes on. The pid, or a negative errno. |
| 4225 | fn au_fork w ulong pk view uchar 4096 pn ulong to long |
| 4226 | r = pipefds aupf 0 8 524288 |
| 4227 | if lt r 0 |
| 4228 | ret r |
| 4229 | end |
| 4230 | rfd = au_pfd 0 |
| 4231 | wfd = au_pfd 4 |
| 4232 | dn = fopen audevnul 0 len audevnul 524288 0 |
| 4233 | pid = fork |
| 4234 | if eq pid 0 |
| 4235 | r = setpgid 0 0 |
| 4236 | r = dupfd wfd 1 0 |
| 4237 | r = dupfd wfd 2 0 |
| 4238 | if ge dn 0 |
| 4239 | r = dupfd int dn 0 0 |
| 4240 | end |
| 4241 | r = execprog pk 0 pn |
| 4242 | exit 127 |
| 4243 | end |
| 4244 | r = fclose wfd |
| 4245 | if ge dn 0 |
| 4246 | r = fclose int dn |
| 4247 | end |
| 4248 | if lt pid 0 |
| 4249 | r = fclose rfd |
| 4250 | ret pid |
| 4251 | end |
| 4252 | r = setpgid int pid int pid |
| 4253 | au_set w 120 0 |
| 4254 | au_set w 112 ulong pid |
| 4255 | put aufd 0 rfd |
| 4256 | ret pid |
| 4257 | end |
| 4258 | |
| 4259 | note Worker w: move available tool output into its proposal page from byte |
| 4260 | note 128 (bytes past 4096 are dropped). false at end of file or on error. |
| 4261 | fn au_out w ulong rfd int to bool |
| 4262 | buf = new uchar 4096 0 |
| 4263 | n = read rfd buf 0 4096 |
| 4264 | if eq n 0 |
| 4265 | ret false |
| 4266 | end |
| 4267 | if lt n 0 |
| 4268 | ret or eq n neg long 4 eq n neg long 11 |
| 4269 | end |
| 4270 | k = au_cell w 120 |
| 4271 | m = ulong n |
| 4272 | if gt add k m 3968 |
| 4273 | m = sub 3968 k |
| 4274 | end |
| 4275 | if gt m 0 |
| 4276 | h = get aumap 1 |
| 4277 | off = add mul w 4096 add 128 k |
| 4278 | r = mapwrite h off buf 0 m |
| 4279 | au_set w 120 add k m |
| 4280 | end |
| 4281 | ret true |
| 4282 | end |
| 4283 | |
| 4284 | note Outcome of a reaped tool from its wait status: it exited, so it ran |
| 4285 | note (2); it was killed or crashed, so an external effect is UNKNOWN (0) and |
| 4286 | note a class 2 tool had none (1). |
| 4287 | fn au_outcome st ulong class ulong to long |
| 4288 | if eq and st 127 0 |
| 4289 | ret 2 |
| 4290 | end |
| 4291 | if eq class 2 |
| 4292 | ret 1 |
| 4293 | end |
| 4294 | ret 0 |
| 4295 | end |
| 4296 | |
| 4297 | note Worker w: finish tool pid of assignment q. Without kill, wait for its |
| 4298 | note exit while still obeying control. With kill, a tool that has already |
| 4299 | note exited is not cancelled; otherwise SIGKILL its whole group. Its wait |
| 4300 | note status alone decides the outcome, never the fact that a kill was sent. |
| 4301 | note Group members left behind are killed either way. |
| 4302 | fn au_end c long w ulong q ulong pid long class ulong kill bool to long |
| 4303 | ws = new uchar 4 0 |
| 4304 | k = kill |
| 4305 | g = long 0 |
| 4306 | if not k |
| 4307 | for it ulong 100000000 |
| 4308 | g = waitpid int pid ws 0 4 1 |
| 4309 | if ne g 0 |
| 4310 | break |
| 4311 | end |
| 4312 | r = auth_wwait c w neg int 1 5000000 |
| 4313 | if eq r 4 |
| 4314 | if eq auth_wctl w q |
| 4315 | k = true |
| 4316 | break |
| 4317 | end |
| 4318 | end |
| 4319 | if or eq r 2 eq r 3 |
| 4320 | k = true |
| 4321 | break |
| 4322 | end |
| 4323 | end |
| 4324 | end |
| 4325 | if k |
| 4326 | g = waitpid int pid ws 0 4 1 |
| 4327 | end |
| 4328 | if eq g 0 |
| 4329 | r = killpid neg int pid 9 |
| 4330 | if lt r 0 |
| 4331 | r = killpid int pid 9 |
| 4332 | end |
| 4333 | g = waitpid int pid ws 0 4 0 |
| 4334 | else |
| 4335 | r = killpid neg int pid 9 |
| 4336 | end |
| 4337 | r = fclose get aufd 0 |
| 4338 | put aufd 0 neg int 1 |
| 4339 | au_set w 112 0 |
| 4340 | if lt g 0 |
| 4341 | au_set w 96 65535 |
| 4342 | ret 0 |
| 4343 | end |
| 4344 | st = or ulong get ws 0 shl ulong get ws 1 8 |
| 4345 | au_set w 96 st |
| 4346 | ret au_outcome st class |
| 4347 | end |
| 4348 | |
| 4349 | note Worker w: run the granted tool pk of assignment q (see au_fork). Its |
| 4350 | note output goes to the proposal page; cancellation of q, stop or creator |
| 4351 | note loss kill its process group. Returns the outcome: 1 when q was |
| 4352 | note cancelled before the tool started or the fork failed, else from |
| 4353 | note au_end. The wait status is in cell word 96. |
| 4354 | fn auth_wtool c long w ulong q ulong pk view uchar 4096 pn ulong class ulong to long |
| 4355 | if eq auth_wctl w q |
| 4356 | ret 1 |
| 4357 | end |
| 4358 | pid = au_fork w pk pn |
| 4359 | if le pid 0 |
| 4360 | ret 1 |
| 4361 | end |
| 4362 | rfd = get aufd 0 |
| 4363 | for it ulong 100000000 |
| 4364 | r = auth_wwait c w rfd 1000000000 |
| 4365 | if eq r 5 |
| 4366 | if not au_out w rfd |
| 4367 | ret au_end c w q pid class false |
| 4368 | end |
| 4369 | end |
| 4370 | if eq r 4 |
| 4371 | if eq auth_wctl w q |
| 4372 | ret au_end c w q pid class true |
| 4373 | end |
| 4374 | end |
| 4375 | if or eq r 2 eq r 3 |
| 4376 | ret au_end c w q pid class true |
| 4377 | end |
| 4378 | if lt r 0 |
| 4379 | ret au_end c w q pid class true |
| 4380 | end |
| 4381 | end |
| 4382 | ret au_end c w q pid class true |
| 4383 | end |
| 4384 | |
| 4385 | note Worker w: report the outcome of the granted tool of assignment q. |
| 4386 | fn auth_wreport w ulong q ulong outcome ulong to long |
| 4387 | au_set w 88 outcome |
| 4388 | au_set w 80 q |
| 4389 | ret sup_ack w |
| 4390 | end |
| 4391 | |
| 4392 | note Creator: the action slot whose operation identity is op, or -1. |
| 4393 | fn au_find op view ulong 4 to long |
| 4394 | for k ulong 64 |
| 4395 | if ne get auuse k 0 |
| 4396 | m = true |
| 4397 | for i ulong 4 |
| 4398 | if ne get aurec add mul k 32 add 8 i get op i |
| 4399 | m = false |
| 4400 | end |
| 4401 | end |
| 4402 | if m |
| 4403 | ret long k |
| 4404 | end |
| 4405 | end |
| 4406 | end |
| 4407 | ret neg long 1 |
| 4408 | end |
| 4409 | |
| 4410 | note Creator: a new ready action record for op and class in a free slot, |
| 4411 | note bound to the current generation; -1 when all 64 are in use. |
| 4412 | note Identities: operation and request op, scope node, provider class, |
| 4413 | note policy 1, grant 1. Attempt limit 3, epoch 1, no expiry in practice. |
| 4414 | fn au_alloc op view ulong 4 class ulong to long |
| 4415 | for k ulong 64 |
| 4416 | if eq get auuse k 0 |
| 4417 | b = mul k 32 |
| 4418 | for i ulong 32 |
| 4419 | put aurec add b i 0 |
| 4420 | end |
| 4421 | put aurec b 1 |
| 4422 | put aurec add b 1 1 |
| 4423 | put aurec add b 3 3 |
| 4424 | put aurec add b 4 get augen 0 |
| 4425 | put aurec add b 5 1 |
| 4426 | put aurec add b 6 9223372036854775807 |
| 4427 | for i ulong 4 |
| 4428 | put aurec add b add 8 i get op i |
| 4429 | put aurec add b add 12 i get op i |
| 4430 | end |
| 4431 | put aurec add b 16 1313817669 |
| 4432 | put aurec add b 20 class |
| 4433 | put aurec add b 24 1 |
| 4434 | put aurec add b 28 1 |
| 4435 | put auuse k 1 |
| 4436 | ret long k |
| 4437 | end |
| 4438 | end |
| 4439 | ret neg long 1 |
| 4440 | end |
| 4441 | |
| 4442 | fn au_load k ulong r edit ulong 32 to void |
| 4443 | for i ulong 32 |
| 4444 | put r i get aurec add mul k 32 i |
| 4445 | end |
| 4446 | end |
| 4447 | |
| 4448 | fn au_save k ulong r view ulong 32 to void |
| 4449 | for i ulong 32 |
| 4450 | put aurec add mul k 32 i get r i |
| 4451 | end |
| 4452 | end |
| 4453 | |
| 4454 | note Creator: commit the start of the operation proposed by worker w. |
| 4455 | note A ready record moves to the current generation; actstart checks the |
| 4456 | note state, the attempt limit and the credential. The verdict. |
| 4457 | fn au_commit w ulong to ulong |
| 4458 | op = new ulong 4 0 |
| 4459 | r = new ulong 32 0 |
| 4460 | cr = new ulong 32 0 |
| 4461 | for i ulong 4 |
| 4462 | put op i au_pw w add 9 i |
| 4463 | end |
| 4464 | k = au_find op |
| 4465 | if lt k 0 |
| 4466 | k = au_alloc op au_pw w 13 |
| 4467 | if lt k 0 |
| 4468 | ret 9 |
| 4469 | end |
| 4470 | end |
| 4471 | au_load ulong k r |
| 4472 | if and eq get r 1 1 ne get r 4 get augen 0 |
| 4473 | put r 4 get augen 0 |
| 4474 | end |
| 4475 | for i ulong 32 |
| 4476 | put cr i get r i |
| 4477 | end |
| 4478 | put cr 1 1 |
| 4479 | put cr 2 1 |
| 4480 | put cr 3 0 |
| 4481 | put cr 4 get augen 0 |
| 4482 | put cr 6 0 |
| 4483 | put cr 7 0 |
| 4484 | e = actstart r cr |
| 4485 | if eq e 0 |
| 4486 | au_save ulong k r |
| 4487 | put aujob w add ulong k 1 |
| 4488 | ret 0 |
| 4489 | end |
| 4490 | if or eq e 21 eq e 22 |
| 4491 | ret 7 |
| 4492 | end |
| 4493 | if eq e 23 |
| 4494 | ret 8 |
| 4495 | end |
| 4496 | if eq e 31 |
| 4497 | ret 5 |
| 4498 | end |
| 4499 | ret 9 |
| 4500 | end |
| 4501 | |
| 4502 | note Creator: a proposal is stale when the input it read is not the |
| 4503 | note assignment's, its epoch is no longer current, or a newer version on the |
| 4504 | note job's dependency topics has been published since. |
| 4505 | fn au_stale c long w ulong j ulong to bool |
| 4506 | if ne au_pw w 6 sc_cell w 16 |
| 4507 | ret true |
| 4508 | end |
| 4509 | if ne au_pw w 7 sc_cell w 24 |
| 4510 | ret true |
| 4511 | end |
| 4512 | if ne au_pw w 6 pool_epoch c |
| 4513 | ret true |
| 4514 | end |
| 4515 | t = get sjdep j |
| 4516 | if eq t 0 |
| 4517 | ret false |
| 4518 | end |
| 4519 | h = pool_current c |
| 4520 | if le h 0 |
| 4521 | ret true |
| 4522 | end |
| 4523 | ret ne node_next h au_pw w 7 t 0 |
| 4524 | end |
| 4525 | |
| 4526 | note Creator: check the pending proposal of worker w; the verdict. |
| 4527 | fn au_valid c long w ulong to ulong |
| 4528 | q = au_cell w 72 |
| 4529 | if or ne au_pw w 0 1 ne au_pw w 1 1 |
| 4530 | ret 1 |
| 4531 | end |
| 4532 | cl = au_pw w 13 |
| 4533 | if or eq cl 0 gt cl 3 |
| 4534 | ret 1 |
| 4535 | end |
| 4536 | if or ne au_pw w 14 0 ne au_pw w 15 0 |
| 4537 | ret 1 |
| 4538 | end |
| 4539 | b = get scbusy w |
| 4540 | if eq b 0 |
| 4541 | ret 2 |
| 4542 | end |
| 4543 | j = sub b 1 |
| 4544 | if ne get sjst j 3 |
| 4545 | ret 2 |
| 4546 | end |
| 4547 | if or ne au_pw w 3 q ne q sc_cell w 0 |
| 4548 | ret 2 |
| 4549 | end |
| 4550 | if or ne au_pw w 2 get sjid j ne au_pw w 4 w |
| 4551 | ret 2 |
| 4552 | end |
| 4553 | if ne au_pw w 5 get auinc w |
| 4554 | ret 3 |
| 4555 | end |
| 4556 | if au_stale c w j |
| 4557 | ret 4 |
| 4558 | end |
| 4559 | if ne au_pw w 8 get augen 0 |
| 4560 | ret 5 |
| 4561 | end |
| 4562 | if eq au_cell w 8 q |
| 4563 | ret 6 |
| 4564 | end |
| 4565 | ret 0 |
| 4566 | end |
| 4567 | |
| 4568 | note Creator: decide worker w's pending proposal. A grant is journaled |
| 4569 | note (START, made durable) before the verdict is published; if the journal |
| 4570 | note refuses, the action is closed as not performed and the verdict is 9. |
| 4571 | fn au_decide c long w ulong to void |
| 4572 | q = au_cell w 72 |
| 4573 | v = au_valid c w |
| 4574 | if eq v 0 |
| 4575 | v = au_commit w |
| 4576 | end |
| 4577 | if eq v 0 |
| 4578 | if ne au_jstart w 0 |
| 4579 | e = au_recon sub get aujob w 1 1 |
| 4580 | put aujob w 0 |
| 4581 | v = 9 |
| 4582 | end |
| 4583 | end |
| 4584 | if eq v 0 |
| 4585 | jn_hit 2 |
| 4586 | end |
| 4587 | au_set w 24 v |
| 4588 | au_set w 32 get augen 0 |
| 4589 | au_set w 16 q |
| 4590 | r = sup_wake w |
| 4591 | end |
| 4592 | |
| 4593 | fn au_jstart w ulong to ulong |
| 4594 | op = new ulong 4 0 |
| 4595 | k = sub get aujob w 1 |
| 4596 | for i ulong 4 |
| 4597 | put op i au_pw w add 9 i |
| 4598 | end |
| 4599 | b = mul k 32 |
| 4600 | ret jn_start op au_pw w 13 au_pw w 2 get aurec add b 2 get aurec add b 4 |
| 4601 | end |
| 4602 | |
| 4603 | note Journal DONE for the action in slot b-1 of worker w's job. |
| 4604 | fn au_jdone b ulong o ulong w ulong how ulong to ulong |
| 4605 | op = new ulong 4 0 |
| 4606 | k = sub b 1 |
| 4607 | for i ulong 4 |
| 4608 | put op i get aurec add mul k 32 add 8 i |
| 4609 | end |
| 4610 | ret jn_done op o au_pw w 2 how |
| 4611 | end |
| 4612 | |
| 4613 | note Creator: apply receipt outcome o to action slot k (actrecon). An |
| 4614 | note outcome of 0 leaves the action UNKNOWN. |
| 4615 | fn au_recon k ulong o ulong to ulong |
| 4616 | r = new ulong 32 0 |
| 4617 | s = new ulong 32 0 |
| 4618 | au_load k r |
| 4619 | for i ulong 32 |
| 4620 | put s i get r i |
| 4621 | end |
| 4622 | put s 0 1 |
| 4623 | put s 1 o |
| 4624 | put s 3 1 |
| 4625 | put s 6 0 |
| 4626 | put s 7 0 |
| 4627 | e = actrecon r s |
| 4628 | au_save k r |
| 4629 | ret e |
| 4630 | end |
| 4631 | |
| 4632 | note Creator: record worker w's report. DONE is made durable before the |
| 4633 | note action record changes; if the journal refuses, the report stays |
| 4634 | note unrecorded and is tried again at the next step. |
| 4635 | fn au_record w ulong to void |
| 4636 | b = get aujob w |
| 4637 | if eq b 0 |
| 4638 | put aured w au_cell w 80 |
| 4639 | ret |
| 4640 | end |
| 4641 | jn_hit 3 |
| 4642 | if ne au_jdone b au_cell w 88 w 1 0 |
| 4643 | ret |
| 4644 | end |
| 4645 | jn_hit 4 |
| 4646 | put aured w au_cell w 80 |
| 4647 | put aujob w 0 |
| 4648 | e = au_recon sub b 1 au_cell w 88 |
| 4649 | end |
| 4650 | |
| 4651 | note Creator, after worker w was reaped: kill the process group of a tool |
| 4652 | note it left running and close its in-flight action. One of class 2 (no |
| 4653 | note external effect) is not performed (DONE how 6, as jn_settle closes it |
| 4654 | note at a restart), so its rerunnable job may start again; any other is |
| 4655 | note UNKNOWN (actlost, DONE how 2) and never runs again (DRA-292). |
| 4656 | fn au_lost w ulong to void |
| 4657 | r = new ulong 32 0 |
| 4658 | p = au_cell w 112 |
| 4659 | if gt p 0 |
| 4660 | x = killpid neg int p 9 |
| 4661 | au_set w 112 0 |
| 4662 | end |
| 4663 | b = get aujob w |
| 4664 | if eq b 0 |
| 4665 | ret |
| 4666 | end |
| 4667 | k = sub b 1 |
| 4668 | if eq get aurec add mul k 32 20 2 |
| 4669 | if ne au_jdone b 1 w 6 0 |
| 4670 | ret |
| 4671 | end |
| 4672 | put aujob w 0 |
| 4673 | e = au_recon k 1 |
| 4674 | ret |
| 4675 | end |
| 4676 | if ne au_jdone b 0 w 2 0 |
| 4677 | ret |
| 4678 | end |
| 4679 | put aujob w 0 |
| 4680 | au_load k r |
| 4681 | e = actlost r |
| 4682 | au_save k r |
| 4683 | end |
| 4684 | |
| 4685 | note Creator, after sched_step: make the dispatch decisions so far durable |
| 4686 | note (jn_drain), record tool reports, close the actions of reaped workers, |
| 4687 | note and decide pending proposals unless paused. Returns the number of |
| 4688 | note proposals decided. |
| 4689 | fn auth_step c long to ulong |
| 4690 | dr = jn_drain |
| 4691 | n = mapget c 48 8 |
| 4692 | k = ulong 0 |
| 4693 | for w ulong n bound 64 |
| 4694 | if gt au_cell w 80 get aured w |
| 4695 | au_record w |
| 4696 | end |
| 4697 | if gt get swpid w 0 |
| 4698 | if and eq get scpause 0 0 gt au_cell w 72 au_cell w 16 |
| 4699 | au_decide c w |
| 4700 | k = add k 1 |
| 4701 | end |
| 4702 | else |
| 4703 | au_lost w |
| 4704 | end |
| 4705 | end |
| 4706 | ret k |
| 4707 | end |
| 4708 | |
| 4709 | note Creator: revoke the current authority. The generation moves on, so a |
| 4710 | note pending or later proposal made under the old one is refused (5), and |
| 4711 | note every granted tool still running is told to cancel. Its outcome is |
| 4712 | note recorded as reported: performed if it had already exited, UNKNOWN if |
| 4713 | note it was killed. Returns the new generation, or 0 with nothing changed |
| 4714 | note when the journal refused REVOKE. |
| 4715 | fn auth_revoke c long to ulong |
| 4716 | g = add get augen 0 1 |
| 4717 | if ne jn_rec 6 g 0 0 0 |
| 4718 | ret 0 |
| 4719 | end |
| 4720 | put augen 0 g |
| 4721 | r = mapset get aumap 0 8192 8 g |
| 4722 | n = mapget c 48 8 |
| 4723 | for w ulong n bound 64 |
| 4724 | if and gt get swpid w 0 gt get aujob w 0 |
| 4725 | au_ctl w au_cell w 16 |
| 4726 | end |
| 4727 | end |
| 4728 | ret g |
| 4729 | end |
| 4730 | |
| 4731 | note Creator: pause (on true) or resume. While paused no job is assigned and |
| 4732 | note no proposal is decided; running tools go on. Every live worker gets a |
| 4733 | note control notification and acknowledges it. Returns sched_pause's count, |
| 4734 | note or -5 with nothing changed when the journal refused PAUSE. |
| 4735 | fn auth_pause c long on bool to long |
| 4736 | v = ulong 0 |
| 4737 | if on |
| 4738 | v = 1 |
| 4739 | end |
| 4740 | if ne jn_rec 7 v 0 0 0 |
| 4741 | ret neg long 5 |
| 4742 | end |
| 4743 | r = mapset get aumap 0 8200 8 v |
| 4744 | n = mapget c 48 8 |
| 4745 | for w ulong n bound 64 |
| 4746 | if gt get swpid w 0 |
| 4747 | au_ctl w 0 |
| 4748 | end |
| 4749 | end |
| 4750 | ret long sched_pause c on |
| 4751 | end |
| 4752 | |
| 4753 | note Creator: cancel job j. 1 freed before it started (no effect), 2 its |
| 4754 | note worker was told to cancel and the outcome comes from its report (a |
| 4755 | note pending proposal is refused with 6), 3 already finished, 4 UNKNOWN, |
| 4756 | note -3 no such job. CANCEL is journaled first; a job freed before it |
| 4757 | note started is also released (outcome 300). -5 when the journal refused |
| 4758 | note CANCEL (nothing changed) or that RELEASE (the job is freed, and the |
| 4759 | note journal, now failed, releases it as cancelled at recovery). |
| 4760 | fn auth_cancel c long j ulong to long |
| 4761 | if ge j 64 |
| 4762 | ret neg long 3 |
| 4763 | end |
| 4764 | id = get sjid j |
| 4765 | if ne jn_rec 8 id 0 0 0 |
| 4766 | ret neg long 5 |
| 4767 | end |
| 4768 | s = sched_cancel j |
| 4769 | if eq s 1 |
| 4770 | if ne jn_rec 5 id 300 0 0 |
| 4771 | ret neg long 5 |
| 4772 | end |
| 4773 | end |
| 4774 | if eq s 2 |
| 4775 | w = get sjw j |
| 4776 | au_ctl w sc_cell w 0 |
| 4777 | end |
| 4778 | ret s |
| 4779 | end |
| 4780 | |
| 4781 | note Creator, after auth_new, jn_open and sched_new, before any spawn: |
| 4782 | note settle and release from the journal (jn_recover), then install every |
| 4783 | note journaled action into the action records, move the generation past |
| 4784 | note every journaled one and restore the paused flag. 0 or an error. |
| 4785 | fn auth_recover c long to ulong |
| 4786 | e = jn_recover c |
| 4787 | if ne e 0 |
| 4788 | ret e |
| 4789 | end |
| 4790 | op = new ulong 4 0 |
| 4791 | for k ulong 64 |
| 4792 | if ne jn_act k 0 0 |
| 4793 | for i ulong 4 |
| 4794 | put op i jn_act k add 1 i |
| 4795 | end |
| 4796 | s = au_alloc op jn_act k 5 |
| 4797 | if lt s 0 |
| 4798 | ret 32 |
| 4799 | end |
| 4800 | b = mul ulong s 32 |
| 4801 | put aurec add b 1 jn_act k 6 |
| 4802 | put aurec add b 2 jn_act k 7 |
| 4803 | put aurec add b 4 jn_act k 8 |
| 4804 | end |
| 4805 | end |
| 4806 | put augen 0 jn_gen |
| 4807 | r = mapset get aumap 0 8192 8 jn_gen |
| 4808 | if ne jn_stat 2 0 |
| 4809 | r = mapset get aumap 0 8200 8 1 |
| 4810 | x = sched_pause c true |
| 4811 | end |
| 4812 | ret 0 |
| 4813 | end |
| 4814 | |
| 4815 | note Creator: word i (0..31) of the action record for op, or -1 if none. |
| 4816 | fn auth_act op view ulong 4 i ulong to long |
| 4817 | k = au_find op |
| 4818 | if or lt k 0 gt i 31 |
| 4819 | ret neg long 1 |
| 4820 | end |
| 4821 | ret long get aurec add mul ulong k 32 i |
| 4822 | end |
| 4823 | |
| 4824 | note Worker w's control or proposal cell word, for the creator's view. |
| 4825 | fn auth_cell w ulong off ulong to ulong |
| 4826 | ret au_cell w off |
| 4827 | end |
| 4828 | |
| 4829 | note Copy up to 3968 bytes of worker w's tool output into dst; the count. |
| 4830 | fn auth_wout w ulong dst edit uchar 4096 to ulong |
| 4831 | n = au_cell w 120 |
| 4832 | if gt n 3968 |
| 4833 | n = 3968 |
| 4834 | end |
| 4835 | if gt n 0 |
| 4836 | r = mapread get aumap 1 add mul w 4096 128 dst 0 n |
| 4837 | end |
| 4838 | ret n |
| 4839 | end |
| 4840 | note Concatenate after transport.om, pool.om, super.om, sched.om, |
| 4841 | note seed/authority/action.om, journal.om and auth.om. CPU-only acceptance of |
| 4842 | note DRA-289: a conductor forks a creator, makes it crash at each durable and |
| 4843 | note effect boundary (exit 77 at jcrash points 1..6, or SIGKILL while a tool |
| 4844 | note runs), restarts it, and then checks the journal, the effects and the |
| 4845 | note recovered outcomes with its own reader (jt_load, not jn_scan). |
| 4846 | note Journal directory build/node/jt, relative to the repository root. |
| 4847 | note Job id kind*1000 + op. Tools (/bin/sh -c), all writing under the |
| 4848 | note directory: kind 1 appends "<op>" to sink; kind 2 sleeps 1 s first; |
| 4849 | note kind 3 is idempotent: it creates key k.<op> with ln (never replaces) |
| 4850 | note and appends "<op>" only when it did; kind 4 appends "s<op>", sleeps |
| 4851 | note 1 s, then acts as kind 3. Kinds 1 and 2 are class 1 (external effect), |
| 4852 | note kinds 3 and 4 class 3 (idempotent, decided by the key at recovery). |
| 4853 | text jtsh "/bin/sh" |
| 4854 | text jtdc "-c" |
| 4855 | text jtt2 "echo " |
| 4856 | text jtt3 " >> build/node/jt/sink" |
| 4857 | text jtt4 "sleep 1; " |
| 4858 | text jtt5 " > build/node/jt/t." |
| 4859 | text jtt6 " && ln build/node/jt/t." |
| 4860 | text jtt7 " build/node/jt/k." |
| 4861 | text jtt8 " && echo " |
| 4862 | text jtt9 "; rm -f build/node/jt/t." |
| 4863 | text jtts "echo s" |
| 4864 | text jtdir "build/node/jt" |
| 4865 | text jtfj "build/node/jt/journal" |
| 4866 | text jtfl "build/node/jt/lock" |
| 4867 | text jtfs "build/node/jt/sink" |
| 4868 | text jtfk "build/node/jt/k." |
| 4869 | text jtft "build/node/jt/t." |
| 4870 | text jtok "PASS node journal\n" |
| 4871 | global jtm long 1 |
| 4872 | global jtr ulong 4096 |
| 4873 | global jtn ulong 1 |
| 4874 | global jtb uchar 65536 |
| 4875 | global jtp uchar 64 |
| 4876 | |
| 4877 | fn jt_ok ok bool code int to void |
| 4878 | if not ok |
| 4879 | exit code |
| 4880 | end |
| 4881 | end |
| 4882 | |
| 4883 | fn jt_tlen k ulong to ulong |
| 4884 | if eq k 0 |
| 4885 | ret len jtsh |
| 4886 | end |
| 4887 | if eq k 1 |
| 4888 | ret len jtdc |
| 4889 | end |
| 4890 | if eq k 2 |
| 4891 | ret len jtt2 |
| 4892 | end |
| 4893 | if eq k 3 |
| 4894 | ret len jtt3 |
| 4895 | end |
| 4896 | if eq k 4 |
| 4897 | ret len jtt4 |
| 4898 | end |
| 4899 | if eq k 5 |
| 4900 | ret len jtt5 |
| 4901 | end |
| 4902 | if eq k 6 |
| 4903 | ret len jtt6 |
| 4904 | end |
| 4905 | if eq k 7 |
| 4906 | ret len jtt7 |
| 4907 | end |
| 4908 | if eq k 8 |
| 4909 | ret len jtt8 |
| 4910 | end |
| 4911 | if eq k 9 |
| 4912 | ret len jtt9 |
| 4913 | end |
| 4914 | ret len jtts |
| 4915 | end |
| 4916 | |
| 4917 | fn jt_tb k ulong i ulong to uchar |
| 4918 | if eq k 0 |
| 4919 | ret get jtsh i |
| 4920 | end |
| 4921 | if eq k 1 |
| 4922 | ret get jtdc i |
| 4923 | end |
| 4924 | if eq k 2 |
| 4925 | ret get jtt2 i |
| 4926 | end |
| 4927 | if eq k 3 |
| 4928 | ret get jtt3 i |
| 4929 | end |
| 4930 | if eq k 4 |
| 4931 | ret get jtt4 i |
| 4932 | end |
| 4933 | if eq k 5 |
| 4934 | ret get jtt5 i |
| 4935 | end |
| 4936 | if eq k 6 |
| 4937 | ret get jtt6 i |
| 4938 | end |
| 4939 | if eq k 7 |
| 4940 | ret get jtt7 i |
| 4941 | end |
| 4942 | if eq k 8 |
| 4943 | ret get jtt8 i |
| 4944 | end |
| 4945 | if eq k 9 |
| 4946 | ret get jtt9 i |
| 4947 | end |
| 4948 | ret get jtts i |
| 4949 | end |
| 4950 | |
| 4951 | note Append text k to s at n; the new length. |
| 4952 | fn jt_add s edit uchar 1024 n ulong k ulong to ulong |
| 4953 | m = jt_tlen k |
| 4954 | for i ulong m bound 64 |
| 4955 | put s add n i jt_tb k i |
| 4956 | end |
| 4957 | ret add n m |
| 4958 | end |
| 4959 | |
| 4960 | note Append the decimal v to s at n; the new length. |
| 4961 | fn jt_num s edit uchar 1024 n ulong v ulong to ulong |
| 4962 | d = new uchar 20 0 |
| 4963 | x = v |
| 4964 | k = ulong 20 |
| 4965 | for i ulong 20 |
| 4966 | k = sub k 1 |
| 4967 | put d k uchar add rem x 10 48 |
| 4968 | x = div x 10 |
| 4969 | if eq x 0 |
| 4970 | break |
| 4971 | end |
| 4972 | end |
| 4973 | for i ulong sub 20 k bound 20 |
| 4974 | put s add n i get d add k i |
| 4975 | end |
| 4976 | ret add n sub 20 k |
| 4977 | end |
| 4978 | |
| 4979 | note The idempotent part: echo N > t.N && ln t.N k.N && echo N >> sink; rm -f t.N |
| 4980 | fn jt_keyed s edit uchar 1024 n ulong op ulong to ulong |
| 4981 | m = jt_add s n 2 |
| 4982 | m = jt_num s m op |
| 4983 | m = jt_add s m 5 |
| 4984 | m = jt_num s m op |
| 4985 | m = jt_add s m 6 |
| 4986 | m = jt_num s m op |
| 4987 | m = jt_add s m 7 |
| 4988 | m = jt_num s m op |
| 4989 | m = jt_add s m 8 |
| 4990 | m = jt_num s m op |
| 4991 | m = jt_add s m 3 |
| 4992 | m = jt_add s m 9 |
| 4993 | ret jt_num s m op |
| 4994 | end |
| 4995 | |
| 4996 | fn jt_script kind ulong op ulong s edit uchar 1024 to ulong |
| 4997 | n = ulong 0 |
| 4998 | if eq kind 4 |
| 4999 | n = jt_add s n 10 |
| 5000 | n = jt_num s n op |
| 5001 | n = jt_add s n 3 |
| 5002 | put s n 59 |
| 5003 | put s add n 1 32 |
| 5004 | n = add n 2 |
| 5005 | end |
| 5006 | if or eq kind 2 eq kind 4 |
| 5007 | n = jt_add s n 4 |
| 5008 | end |
| 5009 | if ge kind 3 |
| 5010 | ret jt_keyed s n op |
| 5011 | end |
| 5012 | n = jt_add s n 2 |
| 5013 | n = jt_num s n op |
| 5014 | ret jt_add s n 3 |
| 5015 | end |
| 5016 | |
| 5017 | note Packed argv: /bin/sh -c <script>. |
| 5018 | fn jt_pack kind ulong op ulong pk edit uchar 4096 to ulong |
| 5019 | s = new uchar 1024 0 |
| 5020 | m = jt_script kind op s |
| 5021 | put pk 0 3 |
| 5022 | put pk 1 0 |
| 5023 | w = ulong 2 |
| 5024 | for a ulong 2 |
| 5025 | k = jt_tlen a |
| 5026 | put pk w uchar k |
| 5027 | put pk add w 1 0 |
| 5028 | for i ulong k bound 16 |
| 5029 | put pk add add w 2 i jt_tb a i |
| 5030 | end |
| 5031 | w = add add w 2 k |
| 5032 | end |
| 5033 | put pk w uchar and m 255 |
| 5034 | put pk add w 1 uchar shr m 8 |
| 5035 | for i ulong m bound 1024 |
| 5036 | put pk add add w 2 i get s i |
| 5037 | end |
| 5038 | ret add add w 2 m |
| 5039 | end |
| 5040 | |
| 5041 | fn jt_job c long w ulong q ulong to void |
| 5042 | op = new ulong 4 0 |
| 5043 | pk = new uchar 4096 0 |
| 5044 | out = new uchar 4096 0 |
| 5045 | id = sc_cell w 8 |
| 5046 | kind = div id 1000 |
| 5047 | cl = ulong 1 |
| 5048 | if ge kind 3 |
| 5049 | cl = 3 |
| 5050 | end |
| 5051 | put op 0 rem id 1000 |
| 5052 | put op 1 7 |
| 5053 | put op 2 1 |
| 5054 | put op 3 1 |
| 5055 | r = auth_propose w q op cl |
| 5056 | v = auth_wdecide c w q 30000000000 |
| 5057 | if ne v 0 |
| 5058 | r = sched_wdone w q add 16 ulong v out 0 |
| 5059 | ret |
| 5060 | end |
| 5061 | pn = jt_pack kind rem id 1000 pk |
| 5062 | o = auth_wtool c w q pk pn cl |
| 5063 | r = auth_wreport w q ulong o |
| 5064 | r = sched_wdone w q ulong o out 0 |
| 5065 | end |
| 5066 | |
| 5067 | fn jt_worker c long w ulong to int |
| 5068 | for it ulong 1000000000 |
| 5069 | q = sched_wjob w |
| 5070 | if gt q 0 |
| 5071 | jt_job c w q |
| 5072 | end |
| 5073 | r = auth_wwait c w neg int 1 1000000000 |
| 5074 | if or eq r 2 eq r 3 |
| 5075 | ret 0 |
| 5076 | end |
| 5077 | if eq r 4 |
| 5078 | t = auth_wctl w |
| 5079 | end |
| 5080 | if lt r 0 |
| 5081 | ret 45 |
| 5082 | end |
| 5083 | end |
| 5084 | ret 46 |
| 5085 | end |
| 5086 | |
| 5087 | note Job set js: its i-th job id, 0 past the end. |
| 5088 | fn jt_jobid js ulong i ulong to ulong |
| 5089 | if eq js 1 |
| 5090 | if eq i 0 |
| 5091 | ret 1001 |
| 5092 | end |
| 5093 | end |
| 5094 | if eq js 2 |
| 5095 | if eq i 0 |
| 5096 | ret 3002 |
| 5097 | end |
| 5098 | end |
| 5099 | if eq js 3 |
| 5100 | if eq i 0 |
| 5101 | ret 1001 |
| 5102 | end |
| 5103 | if eq i 1 |
| 5104 | ret 3002 |
| 5105 | end |
| 5106 | end |
| 5107 | if eq js 4 |
| 5108 | if eq i 0 |
| 5109 | ret 4003 |
| 5110 | end |
| 5111 | end |
| 5112 | if eq js 5 |
| 5113 | if eq i 0 |
| 5114 | ret 2004 |
| 5115 | end |
| 5116 | end |
| 5117 | ret 0 |
| 5118 | end |
| 5119 | |
| 5120 | fn jt_open ob ulong ms ulong to ulong |
| 5121 | d = new uchar 128 0 |
| 5122 | o = new ulong 4 0 |
| 5123 | n = len jtdir |
| 5124 | for i ulong n bound 128 |
| 5125 | put d i get jtdir i |
| 5126 | end |
| 5127 | put o 0 ob |
| 5128 | put o 1 7 |
| 5129 | put o 2 7 |
| 5130 | put o 3 7 |
| 5131 | ret jn_open d n o ms |
| 5132 | end |
| 5133 | |
| 5134 | note Creator steps until every job of set js is released, up to 20 s. |
| 5135 | fn jt_drive c long js ulong to int |
| 5136 | dl = add ulong monons 20000000000 |
| 5137 | for t ulong 100000000 |
| 5138 | r = sched_step c 5000000 |
| 5139 | k = auth_step c |
| 5140 | for j ulong 64 |
| 5141 | if eq get sjst j 4 |
| 5142 | x = jn_release j |
| 5143 | end |
| 5144 | end |
| 5145 | all = true |
| 5146 | for i ulong 8 |
| 5147 | id = jt_jobid js i |
| 5148 | if and gt id 0 not jn_done_j id |
| 5149 | all = false |
| 5150 | end |
| 5151 | end |
| 5152 | if all |
| 5153 | ret 0 |
| 5154 | end |
| 5155 | if ge ulong monons dl |
| 5156 | ret 62 |
| 5157 | end |
| 5158 | end |
| 5159 | ret 62 |
| 5160 | end |
| 5161 | |
| 5162 | note Creator process: open the journal (lock wait 3 s, 100 ms when fl bit 4), |
| 5163 | note recover, spawn two workers, admit the jobs of set js the journal does |
| 5164 | note not know, run to completion. fl bit 1: pause, then exit cleanly. fl bit |
| 5165 | note 2: check that the journaled pause survived the restart, then resume. |
| 5166 | note Exit 77 at crash point cp; journal refusals exit with their code. |
| 5167 | fn jt_run cp ulong js ulong ob ulong fl ulong to int |
| 5168 | c = pool_new 4 2 |
| 5169 | jt_ok gt c 0 50 |
| 5170 | jt_ok eq sup_init c 0 51 |
| 5171 | jt_ok eq sched_new c 8 8 0 52 |
| 5172 | jt_ok eq auth_new c 0 53 |
| 5173 | ms = ulong 3000 |
| 5174 | if ne and fl 4 0 |
| 5175 | ms = 100 |
| 5176 | end |
| 5177 | e = jt_open ob ms |
| 5178 | if ne e 0 |
| 5179 | ret int e |
| 5180 | end |
| 5181 | put jcrash 0 cp |
| 5182 | e = auth_recover c |
| 5183 | if ne e 0 |
| 5184 | ret int add 33 e |
| 5185 | end |
| 5186 | for w ulong 2 |
| 5187 | p = auth_spawn c w |
| 5188 | if eq p 0 |
| 5189 | exit jt_worker c w |
| 5190 | end |
| 5191 | jt_ok gt p 0 54 |
| 5192 | end |
| 5193 | for i ulong 8 |
| 5194 | id = jt_jobid js i |
| 5195 | if and gt id 0 not jn_known id |
| 5196 | jt_ok ge jn_admit id 0 0 0 55 |
| 5197 | end |
| 5198 | end |
| 5199 | if ne and fl 1 0 |
| 5200 | k = auth_pause c true |
| 5201 | jn_close |
| 5202 | ret 0 |
| 5203 | end |
| 5204 | if ne and fl 2 0 |
| 5205 | for t ulong 60 |
| 5206 | r = sched_step c 5000000 |
| 5207 | k = auth_step c |
| 5208 | end |
| 5209 | jt_ok eq sched_count 3 3 0 61 |
| 5210 | jt_ok eq get scpause 0 1 61 |
| 5211 | k = auth_pause c false |
| 5212 | end |
| 5213 | e = ulong jt_drive c js |
| 5214 | if ne e 0 |
| 5215 | ret int e |
| 5216 | end |
| 5217 | r = sched_stop c 2000000000 |
| 5218 | sup_closeall |
| 5219 | jn_close |
| 5220 | ret 0 |
| 5221 | end |
| 5222 | |
| 5223 | fn jt_fork cp ulong js ulong ob ulong fl ulong to long |
| 5224 | p = fork |
| 5225 | if eq p 0 |
| 5226 | exit jt_run cp js ob fl |
| 5227 | end |
| 5228 | ret p |
| 5229 | end |
| 5230 | |
| 5231 | fn jt_reap p long to ulong |
| 5232 | ws = new uchar 4 0 |
| 5233 | g = waitpid int p ws 0 4 0 |
| 5234 | if le g 0 |
| 5235 | ret 999 |
| 5236 | end |
| 5237 | if ne and get ws 0 127 0 |
| 5238 | ret add 128 ulong and get ws 0 127 |
| 5239 | end |
| 5240 | ret ulong get ws 1 |
| 5241 | end |
| 5242 | |
| 5243 | fn jt_runc cp ulong js ulong ob ulong fl ulong to ulong |
| 5244 | ret jt_reap jt_fork cp js ob fl |
| 5245 | end |
| 5246 | |
| 5247 | note Path prefix k (0 journal, 1 lock, 2 sink, 3 key prefix, 4 temp |
| 5248 | note prefix), with the decimal v appended for 3 and 4, in jtp; the length. |
| 5249 | fn jt_path k ulong v ulong to ulong |
| 5250 | s = new uchar 1024 0 |
| 5251 | n = ulong 0 |
| 5252 | if eq k 0 |
| 5253 | n = len jtfj |
| 5254 | for i ulong n bound 64 |
| 5255 | put s i get jtfj i |
| 5256 | end |
| 5257 | end |
| 5258 | if eq k 1 |
| 5259 | n = len jtfl |
| 5260 | for i ulong n bound 64 |
| 5261 | put s i get jtfl i |
| 5262 | end |
| 5263 | end |
| 5264 | if eq k 2 |
| 5265 | n = len jtfs |
| 5266 | for i ulong n bound 64 |
| 5267 | put s i get jtfs i |
| 5268 | end |
| 5269 | end |
| 5270 | if eq k 3 |
| 5271 | n = len jtfk |
| 5272 | for i ulong n bound 64 |
| 5273 | put s i get jtfk i |
| 5274 | end |
| 5275 | end |
| 5276 | if eq k 4 |
| 5277 | n = len jtft |
| 5278 | for i ulong n bound 64 |
| 5279 | put s i get jtft i |
| 5280 | end |
| 5281 | end |
| 5282 | if ge k 3 |
| 5283 | n = jt_num s n v |
| 5284 | end |
| 5285 | for i ulong n bound 64 |
| 5286 | put jtp i get s i |
| 5287 | end |
| 5288 | ret n |
| 5289 | end |
| 5290 | |
| 5291 | fn jt_clean to void |
| 5292 | for k ulong 3 |
| 5293 | n = jt_path k 0 |
| 5294 | r = unlink jtp 0 n |
| 5295 | end |
| 5296 | for v ulong 5 |
| 5297 | for k ulong 2 |
| 5298 | n = jt_path add k 3 v |
| 5299 | r = unlink jtp 0 n |
| 5300 | end |
| 5301 | end |
| 5302 | end |
| 5303 | |
| 5304 | fn jt_exists k ulong v ulong to bool |
| 5305 | st = new uchar 128 0 |
| 5306 | n = jt_path k v |
| 5307 | ret eq stat jtp 0 n st 0 128 0 |
| 5308 | end |
| 5309 | |
| 5310 | note Read file k (0 journal, 2 sink) into jtb; the byte count. |
| 5311 | fn jt_slurp k ulong to ulong |
| 5312 | n = jt_path k 0 |
| 5313 | fd = fopen jtp 0 n 524288 0 |
| 5314 | if lt fd 0 |
| 5315 | ret 0 |
| 5316 | end |
| 5317 | t = ulong 0 |
| 5318 | for i ulong 4096 |
| 5319 | g = read int fd jtb t sub 65536 t |
| 5320 | if le g 0 |
| 5321 | break |
| 5322 | end |
| 5323 | t = add t ulong g |
| 5324 | end |
| 5325 | r = fclose int fd |
| 5326 | ret t |
| 5327 | end |
| 5328 | |
| 5329 | fn jt_w o ulong to ulong |
| 5330 | v = ulong 0 |
| 5331 | for k ulong 8 |
| 5332 | v = or v shl ulong get jtb add o k mul k 8 |
| 5333 | end |
| 5334 | ret v |
| 5335 | end |
| 5336 | |
| 5337 | note Independent reader: the journal is well formed for objective ob: exact |
| 5338 | note size, header, every record's checksum and seq, each OPEN raises the |
| 5339 | note fence by one and every other record carries the current fence. |
| 5340 | note Records go to jtr, count in jtn. |
| 5341 | fn jt_load ob ulong to bool |
| 5342 | r = new ulong 16 0 |
| 5343 | sz = jt_slurp 0 |
| 5344 | if or lt sz 64 ne rem sub sz 64 128 0 |
| 5345 | ret false |
| 5346 | end |
| 5347 | for i ulong 8 |
| 5348 | put r i jt_w mul i 8 |
| 5349 | end |
| 5350 | if or ne get r 0 5786624599 or ne get r 3 ob ne get r 7 jn_sum r 7 |
| 5351 | ret false |
| 5352 | end |
| 5353 | n = div sub sz 64 128 |
| 5354 | if gt n 256 |
| 5355 | ret false |
| 5356 | end |
| 5357 | fe = ulong 0 |
| 5358 | for i ulong n bound 256 |
| 5359 | for k ulong 16 |
| 5360 | put r k jt_w add add 64 mul i 128 mul k 8 |
| 5361 | put jtr add mul i 16 k get r k |
| 5362 | end |
| 5363 | s = jn_sum r 15 |
| 5364 | if or ne get r 15 s ne get r 1 add i 1 |
| 5365 | ret false |
| 5366 | end |
| 5367 | if eq get r 0 1 |
| 5368 | if ne get r 2 add fe 1 |
| 5369 | ret false |
| 5370 | end |
| 5371 | fe = get r 2 |
| 5372 | end |
| 5373 | if ne get r 2 fe |
| 5374 | ret false |
| 5375 | end |
| 5376 | end |
| 5377 | put jtn 0 n |
| 5378 | ret true |
| 5379 | end |
| 5380 | |
| 5381 | fn jt_rw i ulong k ulong to ulong |
| 5382 | ret get jtr add mul i 16 k |
| 5383 | end |
| 5384 | |
| 5385 | note Records of type t whose word 3 is v (all when v is 0). |
| 5386 | fn jt_cnt t ulong v ulong to ulong |
| 5387 | n = ulong 0 |
| 5388 | for i ulong get jtn 0 bound 256 |
| 5389 | if and eq jt_rw i 0 t or eq v 0 eq jt_rw i 3 v |
| 5390 | n = add n 1 |
| 5391 | end |
| 5392 | end |
| 5393 | ret n |
| 5394 | end |
| 5395 | |
| 5396 | note Per operation: a START only when none is open and no DONE said |
| 5397 | note performed or UNKNOWN; every START closed by exactly one DONE. |
| 5398 | fn jt_order op ulong to bool |
| 5399 | open = false |
| 5400 | shut = false |
| 5401 | for i ulong get jtn 0 bound 256 |
| 5402 | t = jt_rw i 0 |
| 5403 | if and eq jt_rw i 3 op or eq t 3 eq t 4 |
| 5404 | if eq t 3 |
| 5405 | if or open shut |
| 5406 | ret false |
| 5407 | end |
| 5408 | open = true |
| 5409 | else |
| 5410 | if not open |
| 5411 | ret false |
| 5412 | end |
| 5413 | open = false |
| 5414 | o = jt_rw i 7 |
| 5415 | if or eq o 0 eq o 2 |
| 5416 | shut = true |
| 5417 | end |
| 5418 | end |
| 5419 | end |
| 5420 | end |
| 5421 | ret not open |
| 5422 | end |
| 5423 | |
| 5424 | note Outcome of the only RELEASE of job id; 999 when there is none, 998 |
| 5425 | note when there are several. |
| 5426 | fn jt_rel id ulong to ulong |
| 5427 | o = ulong 999 |
| 5428 | for i ulong get jtn 0 bound 256 |
| 5429 | if and eq jt_rw i 0 5 eq jt_rw i 3 id |
| 5430 | if ne o 999 |
| 5431 | ret 998 |
| 5432 | end |
| 5433 | o = jt_rw i 4 |
| 5434 | end |
| 5435 | end |
| 5436 | ret o |
| 5437 | end |
| 5438 | |
| 5439 | fn jt_lineq st ulong m ulong t view uchar 1024 to bool |
| 5440 | for k ulong m bound 64 |
| 5441 | if ne get jtb add st k get t k |
| 5442 | ret false |
| 5443 | end |
| 5444 | end |
| 5445 | ret true |
| 5446 | end |
| 5447 | |
| 5448 | note Lines of the sink equal to the decimal v (prefixed "s" when s). |
| 5449 | fn jt_sink v ulong s bool to ulong |
| 5450 | t = new uchar 1024 0 |
| 5451 | m = ulong 0 |
| 5452 | if s |
| 5453 | put t 0 115 |
| 5454 | m = 1 |
| 5455 | end |
| 5456 | m = jt_num t m v |
| 5457 | sz = jt_slurp 2 |
| 5458 | n = ulong 0 |
| 5459 | st = ulong 0 |
| 5460 | for i ulong sz bound 65536 |
| 5461 | if eq get jtb i 10 |
| 5462 | if and eq sub i st m jt_lineq st m t |
| 5463 | n = add n 1 |
| 5464 | end |
| 5465 | st = add i 1 |
| 5466 | end |
| 5467 | end |
| 5468 | ret n |
| 5469 | end |
| 5470 | |
| 5471 | note Checks shared by every case for operation op of job id: a well-formed |
| 5472 | note journal for objective 1, ordered transitions, one RELEASE with outcome |
| 5473 | note o, s sink lines. Exit base+1..base+4. |
| 5474 | fn jt_common id ulong o ulong s ulong base int to void |
| 5475 | jt_ok jt_load 1 add base 1 |
| 5476 | jt_ok jt_order rem id 1000 add base 2 |
| 5477 | jt_ok eq jt_rel id o add base 3 |
| 5478 | jt_ok eq jt_sink rem id 1000 false s add base 4 |
| 5479 | end |
| 5480 | |
| 5481 | note Run once with crash point cp (exit 77), then restart to completion. |
| 5482 | fn jt_crash cp ulong js ulong base int to void |
| 5483 | jt_clean |
| 5484 | jt_ok eq jt_runc cp js 1 0 77 base |
| 5485 | jt_ok eq jt_runc 0 js 1 0 0 add base 5 |
| 5486 | end |
| 5487 | |
| 5488 | note Case 0, baseline: both jobs performed once; the key exists. |
| 5489 | fn jt_c0 to void |
| 5490 | jt_clean |
| 5491 | jt_ok eq jt_runc 0 3 1 0 0 10 |
| 5492 | jt_common 1001 2 1 10 |
| 5493 | jt_common 3002 2 1 15 |
| 5494 | jt_ok jt_exists 3 2 19 |
| 5495 | end |
| 5496 | |
| 5497 | note Case 1, crash after ADMIT is durable: the admitted job is admitted again |
| 5498 | note and the unadmitted one admitted; each performed once. |
| 5499 | fn jt_c1 to void |
| 5500 | jt_crash 1 3 20 |
| 5501 | jt_common 1001 2 1 20 |
| 5502 | jt_ok eq jt_cnt 2 1001 1 26 |
| 5503 | jt_ok eq jt_cnt 3 1 1 27 |
| 5504 | jt_ok eq jt_rel 3002 2 28 |
| 5505 | jt_ok eq jt_sink 2 false 1 29 |
| 5506 | end |
| 5507 | |
| 5508 | note Case 2, crash after START is durable, before the grant is published: |
| 5509 | note class 1 is UNKNOWN, never run, and two more restarts add no transition. |
| 5510 | fn jt_c2 to void |
| 5511 | jt_crash 2 1 30 |
| 5512 | jt_common 1001 0 0 30 |
| 5513 | x = get jtn 0 |
| 5514 | o = jt_cnt 1 0 |
| 5515 | jt_ok eq jt_runc 0 1 1 0 0 36 |
| 5516 | jt_ok eq jt_runc 0 1 1 0 0 36 |
| 5517 | jt_ok jt_load 1 37 |
| 5518 | jt_ok eq sub get jtn 0 jt_cnt 1 0 sub x o 38 |
| 5519 | jt_ok eq jt_cnt 1 0 add o 2 39 |
| 5520 | end |
| 5521 | |
| 5522 | note Case 3, the same crash for class 3: the key is absent, so not performed; |
| 5523 | note run again (attempt 2) and performed once. |
| 5524 | fn jt_c3 to void |
| 5525 | jt_crash 2 2 40 |
| 5526 | jt_common 3002 2 1 40 |
| 5527 | jt_ok eq jt_cnt 3 2 2 46 |
| 5528 | jt_ok jt_exists 3 2 47 |
| 5529 | end |
| 5530 | |
| 5531 | note Case 4, crash after the report, before DONE: class 1 performed but |
| 5532 | note UNKNOWN, never run again. |
| 5533 | fn jt_c4 to void |
| 5534 | jt_crash 3 1 50 |
| 5535 | jt_common 1001 0 1 50 |
| 5536 | jt_ok eq jt_cnt 3 1 1 56 |
| 5537 | end |
| 5538 | |
| 5539 | note Case 5, the same for class 3: the key is present, so performed. |
| 5540 | fn jt_c5 to void |
| 5541 | jt_crash 3 2 60 |
| 5542 | jt_common 3002 2 1 60 |
| 5543 | jt_ok eq jt_cnt 3 2 1 66 |
| 5544 | end |
| 5545 | |
| 5546 | note Case 6, crash after DONE, before RELEASE: released as performed. |
| 5547 | fn jt_c6 to void |
| 5548 | jt_crash 4 1 70 |
| 5549 | jt_common 1001 2 1 70 |
| 5550 | jt_ok eq jt_cnt 3 1 1 76 |
| 5551 | end |
| 5552 | |
| 5553 | note Case 7, crash after RELEASE: nothing left to do. |
| 5554 | fn jt_c7 to void |
| 5555 | jt_crash 5 1 80 |
| 5556 | jt_common 1001 2 1 80 |
| 5557 | jt_ok eq jt_cnt 4 1 1 86 |
| 5558 | end |
| 5559 | |
| 5560 | note Case 8, a torn DONE: 60 bytes of it on disk after the crash; the |
| 5561 | note restart cuts them and the operation is UNKNOWN. |
| 5562 | fn jt_c8 to void |
| 5563 | jt_clean |
| 5564 | jt_ok eq jt_runc 6 1 1 0 77 90 |
| 5565 | sz = jt_slurp 0 |
| 5566 | jt_ok eq rem sub sz 64 128 60 96 |
| 5567 | jt_ok eq jt_runc 0 1 1 0 0 95 |
| 5568 | jt_common 1001 0 1 90 |
| 5569 | end |
| 5570 | |
| 5571 | note Wait until the journal holds a START, up to ms. |
| 5572 | fn jt_started ms ulong to bool |
| 5573 | for t ulong ms bound 100000 |
| 5574 | if jt_load 1 |
| 5575 | if gt jt_cnt 3 0 0 |
| 5576 | ret true |
| 5577 | end |
| 5578 | end |
| 5579 | r = sleepns 1000000 |
| 5580 | end |
| 5581 | ret false |
| 5582 | end |
| 5583 | |
| 5584 | note Cases 9 and 10, SIGKILL of the creator while a slow tool runs. Its |
| 5585 | note workers kill the tool. Class 3: run again and performed once (two tool |
| 5586 | note starts). Class 1: UNKNOWN, no effect, never run again. |
| 5587 | fn jt_c9 to void |
| 5588 | jt_clean |
| 5589 | p = jt_fork 0 4 1 0 |
| 5590 | jt_ok jt_started 5000 120 |
| 5591 | r = sleepns 100000000 |
| 5592 | jt_ok eq killpid int p 9 0 120 |
| 5593 | jt_ok eq jt_reap p 137 120 |
| 5594 | jt_ok eq jt_runc 0 4 1 0 0 125 |
| 5595 | jt_common 4003 2 1 120 |
| 5596 | jt_ok eq jt_cnt 3 3 2 126 |
| 5597 | jt_ok eq jt_sink 3 true 2 127 |
| 5598 | jt_ok jt_exists 3 3 128 |
| 5599 | end |
| 5600 | |
| 5601 | fn jt_c10 to void |
| 5602 | jt_clean |
| 5603 | p = jt_fork 0 5 1 0 |
| 5604 | jt_ok jt_started 5000 130 |
| 5605 | r = sleepns 100000000 |
| 5606 | jt_ok eq killpid int p 9 0 130 |
| 5607 | jt_ok eq jt_reap p 137 130 |
| 5608 | jt_ok eq jt_runc 0 5 1 0 0 135 |
| 5609 | jt_common 2004 0 0 130 |
| 5610 | jt_ok eq jt_cnt 3 4 1 136 |
| 5611 | end |
| 5612 | |
| 5613 | note Case 11, fencing: while a creator runs a tool, a second creator on the |
| 5614 | note same directory is refused (20) and the journal is unchanged. |
| 5615 | fn jt_c11 to void |
| 5616 | jt_clean |
| 5617 | p = jt_fork 0 4 1 0 |
| 5618 | jt_ok jt_started 5000 140 |
| 5619 | a = jt_slurp 0 |
| 5620 | jt_ok eq jt_runc 0 4 1 4 20 141 |
| 5621 | jt_ok eq jt_slurp 0 a 142 |
| 5622 | jt_ok eq jt_reap p 0 143 |
| 5623 | jt_common 4003 2 1 140 |
| 5624 | end |
| 5625 | |
| 5626 | note Cases 12 to 14 on a finished baseline directory: another objective |
| 5627 | note (22), a header from another format version (21) and a corrupt record |
| 5628 | note before the last (23) are refused, and the journal is unchanged. |
| 5629 | fn jt_c12 to void |
| 5630 | b = new uchar 128 0 |
| 5631 | h = new ulong 16 0 |
| 5632 | jt_clean |
| 5633 | jt_ok eq jt_runc 0 1 1 0 0 150 |
| 5634 | a = jt_slurp 0 |
| 5635 | jt_ok eq jt_runc 0 1 2 0 22 151 |
| 5636 | jt_ok eq jt_slurp 0 a 152 |
| 5637 | n = jt_path 0 0 |
| 5638 | fd = fopen jtp 0 n 524290 0 |
| 5639 | jt_ok ge fd 0 153 |
| 5640 | jt_ok eq pread int fd b 0 64 0 64 153 |
| 5641 | jn_dec b h |
| 5642 | put h 1 3 |
| 5643 | put h 7 jn_sum h 7 |
| 5644 | jn_enc h b |
| 5645 | jt_ok eq pwrite int fd b 0 64 0 64 153 |
| 5646 | jt_ok eq jt_runc 0 1 1 0 21 154 |
| 5647 | jt_ok eq jt_slurp 0 a 155 |
| 5648 | put h 1 2 |
| 5649 | put h 7 jn_sum h 7 |
| 5650 | jn_enc h b |
| 5651 | jt_ok eq pwrite int fd b 0 64 0 64 156 |
| 5652 | jt_ok eq pread int fd b 0 1 212 1 156 |
| 5653 | put b 0 uchar xor get b 0 1 |
| 5654 | jt_ok eq pwrite int fd b 0 1 212 1 156 |
| 5655 | r = fclose int fd |
| 5656 | jt_ok eq jt_runc 0 1 1 0 23 157 |
| 5657 | jt_ok eq jt_slurp 0 a 158 |
| 5658 | end |
| 5659 | |
| 5660 | note Case 15, pause survives a restart: no job starts until resumed. |
| 5661 | fn jt_c15 to void |
| 5662 | jt_clean |
| 5663 | jt_ok eq jt_runc 0 1 1 1 0 160 |
| 5664 | jt_ok eq jt_runc 0 1 1 2 0 161 |
| 5665 | jt_common 1001 2 1 160 |
| 5666 | jt_ok eq jt_cnt 7 0 2 166 |
| 5667 | end |
| 5668 | |
| 5669 | note Case 16, RESULT (type 10, DRA-292): an objective's result survives a |
| 5670 | note reopen; a second result for it, result 0 and an index past 63 are |
| 5671 | note refused with 25 before any write, so the file holds exactly one RESULT. |
| 5672 | fn jt_c16 to void |
| 5673 | jt_clean |
| 5674 | jt_ok eq jt_open 9 0 0 170 |
| 5675 | jt_ok eq jn_result 3 15 4 0 171 |
| 5676 | jt_ok eq jn_res 3 15 172 |
| 5677 | jt_ok eq jn_result 3 1 2 25 173 |
| 5678 | jt_ok eq jn_result 5 0 2 25 174 |
| 5679 | jt_ok eq jn_result 64 1 2 25 175 |
| 5680 | jt_ok eq jn_res 5 0 176 |
| 5681 | jn_close |
| 5682 | jt_ok eq jt_open 9 0 0 177 |
| 5683 | jt_ok eq jn_res 3 15 178 |
| 5684 | jt_ok eq jn_rturn 3 4 179 |
| 5685 | jt_ok eq jn_result 3 15 4 25 180 |
| 5686 | jn_close |
| 5687 | jt_ok jt_load 9 181 |
| 5688 | jt_ok eq jt_cnt 10 0 1 182 |
| 5689 | jt_ok eq jt_cnt 10 3 1 183 |
| 5690 | jt_ok eq jt_cnt 1 0 2 184 |
| 5691 | end |
| 5692 | |
| 5693 | note Case 17, admission at the limit (P-DRA-14, DRA-396): with admission |
| 5694 | note limit 2 full, a duplicate id is refused as a duplicate (-4) and takes |
| 5695 | note no ticket (185, 186); a new id is refused and takes one (187); once a |
| 5696 | note slot frees (cancelled as auth_cancel does: CANCEL, then RELEASE), the |
| 5697 | note duplicate asking again does not hold it up: the ticket holder enters |
| 5698 | note (188). Nothing is spawned; the journal is real. |
| 5699 | fn jt_c17 to void |
| 5700 | jt_clean |
| 5701 | c = pool_new 2 2 |
| 5702 | jt_ok gt c 0 185 |
| 5703 | jt_ok eq sup_init c 0 185 |
| 5704 | jt_ok eq sched_new c 2 8 0 185 |
| 5705 | jt_ok eq jt_open 1 3000 0 185 |
| 5706 | a = jn_admit 7001 0 0 |
| 5707 | jt_ok ge a 0 185 |
| 5708 | jt_ok ge jn_admit 7002 0 0 0 185 |
| 5709 | jt_ok eq jn_admit 7001 0 0 neg long 4 185 |
| 5710 | jt_ok eq sched_stat 30 0 186 |
| 5711 | jt_ok eq jn_admit 7003 0 0 neg long 2 187 |
| 5712 | jt_ok eq sched_stat 30 1 187 |
| 5713 | jt_ok eq jn_rec 8 7001 0 0 0 188 |
| 5714 | jt_ok eq sched_cancel ulong a 1 188 |
| 5715 | jt_ok eq jn_rec 5 7001 300 0 0 188 |
| 5716 | jt_ok eq jn_admit 7001 0 0 neg long 4 188 |
| 5717 | jt_ok ge jn_admit 7003 0 0 0 188 |
| 5718 | jn_close |
| 5719 | jt_clean |
| 5720 | end |
| 5721 | |
| 5722 | note Case 18, a full table below the admission limit (P-DRA-14, DRA-396): |
| 5723 | note limit 64, 64 jobs admitted and then put in UNKNOWN directly, the state |
| 5724 | note a worker loss leaves a job not safe to rerun in (synthetic occupancy: |
| 5725 | note nothing is spawned, no recovery runs), so no job is active and no slot |
| 5726 | note is free. A new id is refused with a ticket, the journal does not grow |
| 5727 | note and no ADMIT for it reaches the journal (189, 190, the last checked by |
| 5728 | note the independent reader). Settling one job (its RELEASE journaled |
| 5729 | note first, so the journal frees the slot too) frees its slot and the same |
| 5730 | note id is admitted and journaled once (191, 192). |
| 5731 | fn jt_c18 to void |
| 5732 | jt_clean |
| 5733 | c = pool_new 2 2 |
| 5734 | jt_ok gt c 0 189 |
| 5735 | jt_ok eq sup_init c 0 189 |
| 5736 | jt_ok eq sched_new c 64 64 0 189 |
| 5737 | jt_ok eq jt_open 1 3000 0 189 |
| 5738 | for i ulong 64 |
| 5739 | jt_ok ge jn_admit add 8001 i 0 0 0 189 |
| 5740 | end |
| 5741 | for j ulong 64 |
| 5742 | put sjst j 6 |
| 5743 | end |
| 5744 | jt_ok eq sched_count 0 0 0 189 |
| 5745 | q = get jseq 0 |
| 5746 | jt_ok eq jn_admit 9001 0 0 neg long 2 189 |
| 5747 | jt_ok eq sched_stat 30 1 189 |
| 5748 | jt_ok eq get jseq 0 q 190 |
| 5749 | jt_ok not jn_known 9001 190 |
| 5750 | jt_ok jt_load 1 190 |
| 5751 | jt_ok eq jt_cnt 2 0 64 190 |
| 5752 | jt_ok eq jt_cnt 2 9001 0 190 |
| 5753 | jt_ok eq jn_rec 5 get sjid 5 0 0 0 191 |
| 5754 | jt_ok eq sched_settle 5 0 191 |
| 5755 | jt_ok ge jn_admit 9001 0 0 0 191 |
| 5756 | jt_ok jn_known 9001 192 |
| 5757 | jt_ok jt_load 1 192 |
| 5758 | jt_ok eq jt_cnt 2 9001 1 192 |
| 5759 | jt_ok eq jt_cnt 2 0 65 192 |
| 5760 | jn_close |
| 5761 | jt_clean |
| 5762 | end |
| 5763 | |
| 5764 | note Case 19's raw journals, written as a creator of that format would: |
| 5765 | note jt_rnew starts the file afresh with a header of version v for the |
| 5766 | note objective jt_open 1 names and keeps it open in jtrd; jt_raw writes |
| 5767 | note record i (seq i + 1) of type t under fence f with words 3..5; jt_rend |
| 5768 | note closes it and returns its size. |
| 5769 | global jtrd int 1 |
| 5770 | fn jt_rnew v ulong to void |
| 5771 | d = new uchar 128 0 |
| 5772 | n = len jtdir |
| 5773 | for i ulong n bound 128 |
| 5774 | put d i get jtdir i |
| 5775 | end |
| 5776 | x = makedir d 0 n 448 |
| 5777 | jt_clean |
| 5778 | m = jt_path 0 0 |
| 5779 | fd = fopen jtp 0 m 524354 384 |
| 5780 | jt_ok ge fd 0 193 |
| 5781 | put jtrd 0 int fd |
| 5782 | h = new ulong 16 0 |
| 5783 | b = new uchar 128 0 |
| 5784 | put h 0 5786624599 |
| 5785 | put h 1 v |
| 5786 | put h 2 128 |
| 5787 | put h 3 1 |
| 5788 | put h 4 7 |
| 5789 | put h 5 7 |
| 5790 | put h 6 7 |
| 5791 | put h 7 jn_sum h 7 |
| 5792 | jn_enc h b |
| 5793 | jt_ok eq pwrite get jtrd 0 b 0 64 0 64 193 |
| 5794 | end |
| 5795 | |
| 5796 | global jtrr ulong 16 |
| 5797 | fn jt_raw i ulong t ulong f ulong a ulong b ulong c ulong to void |
| 5798 | put jtrr 0 t |
| 5799 | put jtrr 1 add i 1 |
| 5800 | put jtrr 2 f |
| 5801 | put jtrr 3 a |
| 5802 | put jtrr 4 b |
| 5803 | put jtrr 5 c |
| 5804 | jt_rput i |
| 5805 | end |
| 5806 | |
| 5807 | fn jt_rput i ulong to void |
| 5808 | r = new ulong 16 0 |
| 5809 | x = new uchar 128 0 |
| 5810 | for k ulong 15 |
| 5811 | put r k get jtrr k |
| 5812 | end |
| 5813 | put r 15 jn_sum r 15 |
| 5814 | jn_enc r x |
| 5815 | o = long add 64 mul i 128 |
| 5816 | jt_ok eq pwrite get jtrd 0 x 0 128 o 128 193 |
| 5817 | end |
| 5818 | |
| 5819 | fn jt_rend to ulong |
| 5820 | x = fclose get jtrd 0 |
| 5821 | ret jt_slurp 0 |
| 5822 | end |
| 5823 | |
| 5824 | note Case 19, a format-1 journal continued (DRA-292's release acceptance |
| 5825 | note runs a format-1 build's journal forward): a version-1 file written as |
| 5826 | note the format-1 creator (79894168) writes it: OPEN (word 5 zero), ADMIT |
| 5827 | note 6001 and 6002, CANCEL 6001, ADMIT 6003, OPEN, RELEASE 6001 cancelled, |
| 5828 | note ADMIT 6004. This build opens it (193). The format-1 jobs replay with |
| 5829 | note request 0 into the lowest slot no unreleased job holds: 6002 in 1, |
| 5830 | note 6003 in 2 (6001, cancelled but not yet released, keeps 0), 6004 in 0 |
| 5831 | note (194). The header is unchanged; FORMAT under the last fence, then OPEN |
| 5832 | note with SPVER, are appended and the independent reader accepts the file |
| 5833 | note (195). A second open appends one more OPEN and no second FORMAT (196); |
| 5834 | note recovery admits the jobs into those slots (197). Refused with the file |
| 5835 | note unchanged: a format-1 OPEN after FORMAT, an OPEN with SPVER before |
| 5836 | note FORMAT in a version-1 file, a FORMAT or a format-1 OPEN in a version-2 |
| 5837 | note file (198, all 33); a GOAL in a format-1 incarnation (199, 25); a 65th |
| 5838 | note live format-1 job, no slot left (200, 25). |
| 5839 | fn jt_c19 to void |
| 5840 | jt_rnew 1 |
| 5841 | jt_raw 0 1 1 111 1 0 |
| 5842 | jt_raw 1 2 1 6001 0 0 |
| 5843 | jt_raw 2 2 1 6002 0 0 |
| 5844 | jt_raw 3 8 1 6001 0 0 |
| 5845 | jt_raw 4 2 1 6003 0 0 |
| 5846 | jt_raw 5 1 2 112 2 0 |
| 5847 | jt_raw 6 5 2 6001 300 0 |
| 5848 | jt_raw 7 2 2 6004 0 0 |
| 5849 | a = jt_rend |
| 5850 | hb = new uchar 64 0 |
| 5851 | for i ulong 64 |
| 5852 | put hb i get jtb i |
| 5853 | end |
| 5854 | jt_ok eq jt_open 1 3000 0 193 |
| 5855 | jt_ok jt_rslot 6002 1 194 |
| 5856 | jt_ok jt_rslot 6003 2 194 |
| 5857 | jt_ok jt_rslot 6004 0 194 |
| 5858 | jn_close |
| 5859 | jt_ok jt_load 1 195 |
| 5860 | jt_ok eq jt_slurp 0 add a 256 195 |
| 5861 | jt_ok jt_rsame hb 195 |
| 5862 | jt_ok eq jt_rw 8 0 14 195 |
| 5863 | jt_ok eq jt_rw 8 3 2 195 |
| 5864 | jt_ok eq jt_rw 8 2 2 195 |
| 5865 | jt_ok eq jt_rw 9 0 1 195 |
| 5866 | jt_ok eq jt_rw 9 5 SPVER 195 |
| 5867 | jt_ok eq jt_rw 9 2 3 195 |
| 5868 | jt_ok eq jt_open 1 3000 0 196 |
| 5869 | jn_close |
| 5870 | jt_ok jt_load 1 196 |
| 5871 | jt_ok eq jt_cnt 14 0 1 196 |
| 5872 | jt_ok eq jt_cnt 1 0 4 196 |
| 5873 | jt_c19r |
| 5874 | jt_c19x |
| 5875 | jt_clean |
| 5876 | end |
| 5877 | |
| 5878 | note Case 19: the replayed job id holds slot s with request 0. |
| 5879 | fn jt_rslot id ulong s ulong to bool |
| 5880 | k = jn_jfind id |
| 5881 | if lt k 0 |
| 5882 | ret false |
| 5883 | end |
| 5884 | ret and eq get jslot ulong k s eq get jreq ulong k 0 |
| 5885 | end |
| 5886 | |
| 5887 | note Case 19: the journal read last (jtb) starts with header hb. |
| 5888 | fn jt_rsame hb view uchar 64 to bool |
| 5889 | for i ulong 64 |
| 5890 | if ne get jtb i get hb i |
| 5891 | ret false |
| 5892 | end |
| 5893 | end |
| 5894 | ret true |
| 5895 | end |
| 5896 | |
| 5897 | note Case 19: recovery admits the format-1 jobs into their replay slots. |
| 5898 | fn jt_c19r to void |
| 5899 | c = pool_new 2 2 |
| 5900 | jt_ok gt c 0 197 |
| 5901 | jt_ok eq sup_init c 0 197 |
| 5902 | jt_ok eq sched_new c 64 64 0 197 |
| 5903 | jt_ok eq jt_open 1 3000 0 197 |
| 5904 | jt_ok eq jn_recover c 0 197 |
| 5905 | jt_ok eq get sjid 1 6002 197 |
| 5906 | jt_ok eq get sjid 2 6003 197 |
| 5907 | jt_ok eq get sjid 0 6004 197 |
| 5908 | jn_close |
| 5909 | end |
| 5910 | |
| 5911 | note Case 19: the raw file just written is refused with e and unchanged. |
| 5912 | fn jt_rref e ulong code int to void |
| 5913 | a = jt_rend |
| 5914 | jt_ok eq jt_open 1 3000 e code |
| 5915 | jt_ok eq jt_slurp 0 a code |
| 5916 | end |
| 5917 | |
| 5918 | note Case 19's refusals. |
| 5919 | fn jt_c19x to void |
| 5920 | jt_rnew 1 |
| 5921 | jt_raw 0 1 1 111 1 0 |
| 5922 | jt_raw 1 14 1 2 0 0 |
| 5923 | jt_raw 2 1 2 112 2 0 |
| 5924 | jt_rref 33 198 |
| 5925 | jt_rnew 1 |
| 5926 | jt_raw 0 1 1 111 1 0 |
| 5927 | jt_raw 1 1 2 112 2 SPVER |
| 5928 | jt_rref 33 198 |
| 5929 | jt_rnew 2 |
| 5930 | jt_raw 0 1 1 111 1 SPVER |
| 5931 | jt_raw 1 14 1 2 0 0 |
| 5932 | jt_rref 33 198 |
| 5933 | jt_rnew 2 |
| 5934 | jt_raw 0 1 1 111 1 0 |
| 5935 | jt_rref 33 198 |
| 5936 | jt_rnew 1 |
| 5937 | jt_raw 0 1 1 111 1 0 |
| 5938 | jt_raw 1 2 1 6001 0 0 |
| 5939 | jt_raw 2 12 1 6001 1 5 |
| 5940 | jt_rref 25 199 |
| 5941 | jt_rnew 1 |
| 5942 | jt_raw 0 1 1 111 1 0 |
| 5943 | for i ulong 65 |
| 5944 | jt_raw add i 1 2 1 add 7001 i 0 0 |
| 5945 | end |
| 5946 | jt_rref 25 200 |
| 5947 | end |
| 5948 | |
| 5949 | fn main to int |
| 5950 | m = mapnew neg int 1 0 4096 3 33 |
| 5951 | jt_ok gt m 0 9 |
| 5952 | put jtm 0 m |
| 5953 | jt_c0 |
| 5954 | jt_c1 |
| 5955 | jt_c2 |
| 5956 | jt_c3 |
| 5957 | jt_c4 |
| 5958 | jt_c5 |
| 5959 | jt_c6 |
| 5960 | jt_c7 |
| 5961 | jt_c8 |
| 5962 | jt_c9 |
| 5963 | jt_c10 |
| 5964 | jt_c11 |
| 5965 | jt_c12 |
| 5966 | jt_c15 |
| 5967 | jt_c16 |
| 5968 | jt_c17 |
| 5969 | jt_c18 |
| 5970 | jt_c19 |
| 5971 | if ne write 1 jtok 0 len jtok long len jtok |
| 5972 | ret 100 |
| 5973 | end |
| 5974 | ret 0 |
| 5975 | end |