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 / pool-test.om
| 1 | note Concatenate after transport.om, pool.om and super.om. CPU-only acceptance |
| 2 | note of epoch turnover, held old versions, worker death, respawn, orphaning and |
| 3 | note bounded teardown with real forked processes. No GPU, model or network. |
| 4 | text pok "PASS node pool\n" |
| 5 | global ptc long 1 |
| 6 | global ptm long 1 |
| 7 | global pth long 1 |
| 8 | global ptws uchar 4 |
| 9 | |
| 10 | fn pt_ok ok bool code int to void |
| 11 | if not ok |
| 12 | exit code |
| 13 | end |
| 14 | end |
| 15 | |
| 16 | fn pt_fd4 p view uchar 8 off ulong to int |
| 17 | v = ulong get p off |
| 18 | v = or v shl ulong get p add off 1 8 |
| 19 | v = or v shl ulong get p add off 2 16 |
| 20 | v = or v shl ulong get p add off 3 24 |
| 21 | ret int v |
| 22 | end |
| 23 | |
| 24 | fn pt_byte e ulong v ulong to uchar |
| 25 | ret uchar and add mul e 16 v 255 |
| 26 | end |
| 27 | |
| 28 | note Creator: publish n full records into the current epoch, bytes f(epoch, v). |
| 29 | fn pt_fill c long n ulong to bool |
| 30 | buf = new uchar 4096 0 |
| 31 | e = pool_epoch c |
| 32 | h = pool_current c |
| 33 | if lt h 0 |
| 34 | ret false |
| 35 | end |
| 36 | for k ulong n bound 64 |
| 37 | v = add mapget h 16 8 1 |
| 38 | b = pt_byte e v |
| 39 | for j ulong 4096 |
| 40 | put buf j b |
| 41 | end |
| 42 | if ne pool_publish c 1 buf 4096 long v |
| 43 | ret false |
| 44 | end |
| 45 | end |
| 46 | ret true |
| 47 | end |
| 48 | |
| 49 | note 0 when arena h holds exactly n complete records of its own epoch, else a code. |
| 50 | fn pt_verify h long n ulong to int |
| 51 | buf = new uchar 4096 0 |
| 52 | e = node_epoch h |
| 53 | cur = ulong 0 |
| 54 | cnt = ulong 0 |
| 55 | for k ulong 65 |
| 56 | v = node_next h cur 1 |
| 57 | if eq v 0 |
| 58 | break |
| 59 | end |
| 60 | if lt v 0 |
| 61 | ret 1 |
| 62 | end |
| 63 | cur = ulong v |
| 64 | if ne node_read h cur buf 4096 |
| 65 | ret 2 |
| 66 | end |
| 67 | b = pt_byte e cur |
| 68 | for j ulong 4096 |
| 69 | if ne get buf j b |
| 70 | ret 3 |
| 71 | end |
| 72 | end |
| 73 | cnt = add cnt 1 |
| 74 | end |
| 75 | if ne cnt n |
| 76 | ret 4 |
| 77 | end |
| 78 | ret 0 |
| 79 | end |
| 80 | |
| 81 | fn pt_refusals to void |
| 82 | pt_ok eq pool_new 0 4 neg long 7 10 |
| 83 | pt_ok eq pool_new 9 4 neg long 7 11 |
| 84 | pt_ok eq pool_new 2 0 neg long 7 12 |
| 85 | pt_ok eq pool_new 2 65 neg long 7 13 |
| 86 | c = pool_new 2 4 |
| 87 | pt_ok gt c 0 14 |
| 88 | pt_ok eq pool_epoch c 1 15 |
| 89 | pt_ok eq pool_acquire c 4 neg long 7 16 |
| 90 | pt_ok eq pool_release c 4 neg long 7 17 |
| 91 | pt_ok eq node_epoch pool_current c 1 18 |
| 92 | pt_ok eq node_reset pool_current c 0 neg long 7 19 |
| 93 | pt_ok eq pool_free c 0 20 |
| 94 | end |
| 95 | |
| 96 | note One process: the creator also plays worker slots 0 and 1, whose holds are |
| 97 | note plain words. Epoch numbers never repeat; arenas are reused only unheld. |
| 98 | fn pt_epochs to void |
| 99 | dst = new uchar 4096 0 |
| 100 | c = pool_new 2 4 |
| 101 | pt_ok gt c 0 21 |
| 102 | pt_ok pt_fill c 1 22 |
| 103 | h1 = pool_acquire c 0 |
| 104 | pt_ok eq h1 pool_current c 23 |
| 105 | pt_ok eq mapget c 256 8 1 24 |
| 106 | pt_ok eq pool_roll c 2 25 |
| 107 | pt_ok ne pool_current c h1 26 |
| 108 | pt_ok eq node_next pool_current c 0 1 0 27 |
| 109 | pt_ok eq pool_roll c neg long 9 28 |
| 110 | pt_ok eq pool_epoch c 2 29 |
| 111 | pt_ok eq pt_verify h1 1 0 30 |
| 112 | pt_ok eq pool_release c 0 0 31 |
| 113 | pt_ok eq pool_roll c 3 32 |
| 114 | pt_ok eq pool_slot c 3 0 33 |
| 115 | pt_ok eq node_read h1 1 dst neg long 7 34 |
| 116 | pt_ok eq node_epoch h1 3 35 |
| 117 | pt_ok pt_fill c 64 36 |
| 118 | for j ulong 4096 |
| 119 | put dst j pt_byte 4 1 |
| 120 | end |
| 121 | pt_ok eq pool_publish c 1 dst 4096 1 37 |
| 122 | pt_ok eq pool_epoch c 4 38 |
| 123 | h4 = pool_acquire c 1 |
| 124 | pt_ok eq node_epoch h4 4 39 |
| 125 | pt_ok pt_fill c 63 40 |
| 126 | pt_ok eq pool_epoch c 4 41 |
| 127 | pt_ok eq pool_roll c 5 42 |
| 128 | pt_ok pt_fill c 64 43 |
| 129 | pt_ok eq pool_publish c 1 dst 1 neg long 9 44 |
| 130 | pt_ok eq pool_epoch c 5 45 |
| 131 | pt_ok eq pt_verify h4 64 0 46 |
| 132 | pt_ok eq pool_stop c 0 47 |
| 133 | pt_ok eq pool_roll c neg long 1 48 |
| 134 | pt_ok eq pool_acquire c 2 neg long 1 49 |
| 135 | pt_ok eq mapget c 272 8 0 50 |
| 136 | pt_ok eq pool_free c 0 51 |
| 137 | end |
| 138 | |
| 139 | note The announce race, made deterministic by running the protocol steps in |
| 140 | note the order that breaks a hold without its recheck: worker 2 read epoch e, |
| 141 | note the creator moved past e and picked e's arena, then worker 2 stores its |
| 142 | note hold. The recheck must refuse; reuse of the arena must then proceed. |
| 143 | fn pt_race to void |
| 144 | c = pool_new 2 4 |
| 145 | pt_ok gt c 0 140 |
| 146 | pt_ok pt_fill c 1 141 |
| 147 | e = pool_epoch c |
| 148 | pt_ok eq pool_roll c 2 142 |
| 149 | pt_ok eq pool_pick c 0 143 |
| 150 | pt_ok eq pool_hold c 2 e neg long 8 144 |
| 151 | pt_ok eq mapget c 272 8 0 145 |
| 152 | pt_ok eq pool_reuse c 0 3 3 146 |
| 153 | pt_ok eq pool_acquire c 2 pool_current c 147 |
| 154 | pt_ok eq pool_hold c 4 3 neg long 7 148 |
| 155 | pt_ok eq pool_free c 0 149 |
| 156 | end |
| 157 | |
| 158 | note Worker phase: 1 acquire and verify, 2 verify the held epoch again, |
| 159 | note 3 release, acquire the current epoch and verify, 4 stall ignoring stop. |
| 160 | fn pt_step c long w ulong ph ulong to int |
| 161 | buf = new uchar 4096 0 |
| 162 | m = get ptm 0 |
| 163 | n = mapget m 8 8 |
| 164 | if eq ph 1 |
| 165 | if ne pool_roll c neg long 6 |
| 166 | ret 11 |
| 167 | end |
| 168 | if ne pool_stop c neg long 6 |
| 169 | ret 12 |
| 170 | end |
| 171 | if ne pool_clear c w neg long 6 |
| 172 | ret 13 |
| 173 | end |
| 174 | h = pool_acquire c w |
| 175 | if le h 0 |
| 176 | ret 14 |
| 177 | end |
| 178 | put pth 0 h |
| 179 | if ne node_publish h 1 buf 1 neg long 6 |
| 180 | ret 15 |
| 181 | end |
| 182 | ret pt_verify h n |
| 183 | end |
| 184 | if eq ph 2 |
| 185 | ret pt_verify get pth 0 mapget m 16 8 |
| 186 | end |
| 187 | if eq ph 3 |
| 188 | r = pool_release c w |
| 189 | h = pool_acquire c w |
| 190 | if le h 0 |
| 191 | ret 16 |
| 192 | end |
| 193 | put pth 0 h |
| 194 | ret pt_verify h n |
| 195 | end |
| 196 | if eq ph 4 |
| 197 | r = sleepns 30000000000 |
| 198 | ret 17 |
| 199 | end |
| 200 | ret 18 |
| 201 | end |
| 202 | |
| 203 | fn pt_worker w ulong to int |
| 204 | c = get ptc 0 |
| 205 | m = get ptm 0 |
| 206 | put pth 0 0 |
| 207 | for it ulong 64 |
| 208 | r = sup_wwait c w 5000000000 |
| 209 | if eq r 2 |
| 210 | if gt get pth 0 0 |
| 211 | r = pool_release c w |
| 212 | end |
| 213 | ret 0 |
| 214 | end |
| 215 | if eq r 3 |
| 216 | ret 41 |
| 217 | end |
| 218 | if eq r 0 |
| 219 | ret 42 |
| 220 | end |
| 221 | if ne r 1 |
| 222 | ret 43 |
| 223 | end |
| 224 | res = pt_step c w mapget m 0 8 |
| 225 | r = mapset m add 64 mul w 8 8 ulong res |
| 226 | if ne sup_ack w 0 |
| 227 | ret 44 |
| 228 | end |
| 229 | end |
| 230 | ret 45 |
| 231 | end |
| 232 | |
| 233 | note Creator: wake worker w for phase ph and wait up to about 5 s for its ack. |
| 234 | note Returns the worker's phase result, or 90 no ack, 91 the worker died. |
| 235 | fn pt_phase c long w ulong ph ulong to int |
| 236 | m = get ptm 0 |
| 237 | r = mapset m 0 8 ph |
| 238 | r = mapset m add 64 mul w 8 8 99 |
| 239 | if ne sup_wake w 0 |
| 240 | ret 92 |
| 241 | end |
| 242 | for t ulong 500 |
| 243 | if gt sup_take get swack w 0 |
| 244 | ret int mapget m add 64 mul w 8 8 |
| 245 | end |
| 246 | if le get swpid w 0 |
| 247 | ret 91 |
| 248 | end |
| 249 | r = sup_poll c 10000000 |
| 250 | end |
| 251 | ret 90 |
| 252 | end |
| 253 | |
| 254 | fn pt_spawn c long w ulong to bool |
| 255 | p = sup_spawn c w |
| 256 | if eq p 0 |
| 257 | exit pt_worker w |
| 258 | end |
| 259 | ret gt p 0 |
| 260 | end |
| 261 | |
| 262 | fn pt_procs to void |
| 263 | c = pool_new 2 4 |
| 264 | pt_ok gt c 0 52 |
| 265 | put ptc 0 c |
| 266 | m = mapnew neg int 1 0 4096 3 33 |
| 267 | pt_ok gt m 0 53 |
| 268 | put ptm 0 m |
| 269 | pt_ok eq sup_init c 0 54 |
| 270 | pt_ok pt_fill c 8 55 |
| 271 | r = mapset m 8 8 8 |
| 272 | r = mapset m 16 8 8 |
| 273 | pt_ok pt_spawn c 0 56 |
| 274 | pt_ok pt_spawn c 1 57 |
| 275 | pt_ok pt_spawn c 2 58 |
| 276 | pt_ok eq sup_spawn c 2 neg long 16 59 |
| 277 | for w ulong 3 |
| 278 | pt_ok eq pt_phase c w 1 0 60 |
| 279 | end |
| 280 | pt_ok eq mapget c 272 8 1 61 |
| 281 | pt_ok eq mapget c 1288 8 1 62 |
| 282 | pt_ok eq pool_roll c 2 63 |
| 283 | pt_ok pt_fill c 5 64 |
| 284 | pt_ok eq pool_roll c neg long 9 65 |
| 285 | note An older epoch stays complete for its holder after newer publication. |
| 286 | pt_ok eq pt_phase c 0 2 0 66 |
| 287 | r = mapset m 8 8 5 |
| 288 | pt_ok eq pt_phase c 0 3 0 67 |
| 289 | pt_ok eq pt_phase c 1 3 0 68 |
| 290 | pt_ok eq mapget c 256 8 2 69 |
| 291 | pt_ok eq pool_roll c neg long 9 70 |
| 292 | note Kill the last holder of epoch 1. Its death is seen through its life pipe. |
| 293 | pt_ok eq killpid int get swpid 2 9 0 71 |
| 294 | dead = long 0 |
| 295 | for t ulong 500 |
| 296 | dead = add dead sup_poll c 10000000 |
| 297 | if gt dead 0 |
| 298 | break |
| 299 | end |
| 300 | end |
| 301 | pt_ok eq dead 1 72 |
| 302 | pt_ok eq and get swst 2 127 9 73 |
| 303 | pt_ok eq mapget c 272 8 0 74 |
| 304 | pt_ok eq mapget c 1296 8 2 75 |
| 305 | pt_ok eq pool_roll c 3 76 |
| 306 | pt_ok pt_fill c 3 77 |
| 307 | r = mapset m 8 8 3 |
| 308 | pt_ok pt_spawn c 2 78 |
| 309 | pt_ok eq pt_phase c 2 3 0 79 |
| 310 | r = mapset m 0 8 4 |
| 311 | pt_ok eq sup_wake 1 0 80 |
| 312 | r = sleepns 100000000 |
| 313 | note A pending ack from the stalled worker must not turn the stop wait into a |
| 314 | note spin that kills it before the 2 s deadline. |
| 315 | pt_ok eq sup_ack 1 0 89 |
| 316 | t0 = monons |
| 317 | k = sup_stop c 2000000000 |
| 318 | pt_ok ge sub monons t0 1900000000 90 |
| 319 | pt_ok eq k 1 81 |
| 320 | pt_ok eq get swst 0 0 82 |
| 321 | pt_ok eq get swst 2 0 83 |
| 322 | pt_ok eq and get swst 1 127 9 84 |
| 323 | pt_ok eq waitpid neg int 1 ptws 0 4 1 neg long 10 85 |
| 324 | pt_ok eq sup_live c 0 86 |
| 325 | sup_closeall |
| 326 | pt_ok eq pool_free c 0 87 |
| 327 | pt_ok eq mapdrop m 0 88 |
| 328 | end |
| 329 | |
| 330 | note A creator that exits without stopping its worker must not leave the worker |
| 331 | note waiting: the worker sees the creator's life pipe hang up and reports 3. |
| 332 | fn pt_orphan to void |
| 333 | pf = new uchar 8 0 |
| 334 | b = new uchar 1 0 |
| 335 | pt_ok ge pipefds pf 0 8 524288 0 93 |
| 336 | rd = pt_fd4 pf 0 |
| 337 | wr = pt_fd4 pf 4 |
| 338 | s = fork |
| 339 | if eq s 0 |
| 340 | c = pool_new 1 1 |
| 341 | if le c 0 |
| 342 | exit 50 |
| 343 | end |
| 344 | if ne sup_init c 0 |
| 345 | exit 51 |
| 346 | end |
| 347 | put ptc 0 c |
| 348 | p = sup_spawn c 0 |
| 349 | if eq p 0 |
| 350 | r = sup_wwait c 0 5000000000 |
| 351 | put b 0 uchar and ulong add r 128 255 |
| 352 | r = write wr b 0 1 |
| 353 | exit 0 |
| 354 | end |
| 355 | exit 0 |
| 356 | end |
| 357 | pt_ok gt s 0 94 |
| 358 | r = fclose wr |
| 359 | pt_ok eq waitpid int s ptws 0 4 0 s 95 |
| 360 | pt_ok eq get ptws 0 0 96 |
| 361 | pt_ok gt waitio rd 1 6000000000 0 97 |
| 362 | pt_ok eq read rd b 0 1 1 98 |
| 363 | pt_ok eq get b 0 131 99 |
| 364 | r = fclose rd |
| 365 | end |
| 366 | |
| 367 | note Stress: 0 when arena h shows only complete records of epoch e from start |
| 368 | note to end of one hold. A reclaimed held arena changes its epoch, drops its |
| 369 | note count or rewrites bytes, so a missing hold check shows up here. |
| 370 | fn pt_scan h long e ulong to int |
| 371 | buf = new uchar 4096 0 |
| 372 | if ne node_epoch h e |
| 373 | ret 1 |
| 374 | end |
| 375 | cur = ulong 0 |
| 376 | for k ulong 65 |
| 377 | v = node_next h cur 1 |
| 378 | if eq v 0 |
| 379 | break |
| 380 | end |
| 381 | if lt v 0 |
| 382 | ret 2 |
| 383 | end |
| 384 | cur = ulong v |
| 385 | if ne node_read h cur buf 4096 |
| 386 | ret 3 |
| 387 | end |
| 388 | b = pt_byte e cur |
| 389 | for j ulong 4096 |
| 390 | if ne get buf j b |
| 391 | ret 4 |
| 392 | end |
| 393 | end |
| 394 | end |
| 395 | if ne node_epoch h e |
| 396 | ret 5 |
| 397 | end |
| 398 | ret 0 |
| 399 | end |
| 400 | |
| 401 | fn pt_sworker w ulong to int |
| 402 | c = get ptc 0 |
| 403 | m = get ptm 0 |
| 404 | n = ulong 0 |
| 405 | for it ulong 10000000 |
| 406 | if pool_stopped c |
| 407 | r = mapset m add 64 mul w 8 8 n |
| 408 | ret 0 |
| 409 | end |
| 410 | h = pool_acquire c w |
| 411 | if eq h neg long 1 |
| 412 | r = mapset m add 64 mul w 8 8 n |
| 413 | ret 0 |
| 414 | end |
| 415 | if gt h 0 |
| 416 | r = pt_scan h mapget c add 256 mul w 8 8 |
| 417 | if ne r 0 |
| 418 | ret add 20 r |
| 419 | end |
| 420 | q = pool_release c w |
| 421 | n = add n 1 |
| 422 | end |
| 423 | end |
| 424 | ret 29 |
| 425 | end |
| 426 | |
| 427 | fn pt_num v ulong to void |
| 428 | d = new uchar 20 0 |
| 429 | x = v |
| 430 | k = ulong 20 |
| 431 | for i ulong 20 |
| 432 | k = sub k 1 |
| 433 | put d k uchar add rem x 10 48 |
| 434 | x = div x 10 |
| 435 | if eq x 0 |
| 436 | break |
| 437 | end |
| 438 | end |
| 439 | r = write 1 d k sub 20 k |
| 440 | end |
| 441 | |
| 442 | text pstress "stress rolls=" |
| 443 | text pbusy " busy=" |
| 444 | text preads " reads=" |
| 445 | text pnl "\n" |
| 446 | text psp " " |
| 447 | |
| 448 | note Two workers acquire, scan and release in a tight loop while the creator |
| 449 | note rolls 2000 epochs over two arenas, publishing four records per epoch. |
| 450 | fn pt_stress to void |
| 451 | c = pool_new 2 2 |
| 452 | pt_ok gt c 0 120 |
| 453 | put ptc 0 c |
| 454 | m = mapnew neg int 1 0 4096 3 33 |
| 455 | pt_ok gt m 0 121 |
| 456 | put ptm 0 m |
| 457 | pt_ok eq sup_init c 0 122 |
| 458 | pt_ok pt_fill c 4 123 |
| 459 | for w ulong 2 |
| 460 | p = sup_spawn c w |
| 461 | if eq p 0 |
| 462 | exit pt_sworker w |
| 463 | end |
| 464 | pt_ok gt p 0 124 |
| 465 | end |
| 466 | rolls = ulong 0 |
| 467 | busy = ulong 0 |
| 468 | for i ulong 100000000 |
| 469 | if ge rolls 2000 |
| 470 | break |
| 471 | end |
| 472 | r = pool_roll c |
| 473 | if eq r neg long 9 |
| 474 | busy = add busy 1 |
| 475 | if eq and busy 1023 0 |
| 476 | pt_ok eq sup_poll c 0 0 133 |
| 477 | end |
| 478 | cpurelax |
| 479 | continue |
| 480 | end |
| 481 | pt_ok gt r 0 125 |
| 482 | rolls = add rolls 1 |
| 483 | pt_ok pt_fill c 4 126 |
| 484 | end |
| 485 | pt_ok eq rolls 2000 127 |
| 486 | pt_ok eq sup_stop c 5000000000 0 128 |
| 487 | pt_ok eq get swst 0 0 129 |
| 488 | pt_ok eq get swst 1 0 130 |
| 489 | r = write 1 pstress 0 len pstress |
| 490 | pt_num rolls |
| 491 | r = write 1 pbusy 0 len pbusy |
| 492 | pt_num busy |
| 493 | r = write 1 preads 0 len preads |
| 494 | pt_num mapget m 64 8 |
| 495 | r = write 1 psp 0 1 |
| 496 | pt_num mapget m 72 8 |
| 497 | r = write 1 pnl 0 1 |
| 498 | sup_closeall |
| 499 | pt_ok eq pool_free c 0 131 |
| 500 | pt_ok eq mapdrop m 0 132 |
| 501 | end |
| 502 | |
| 503 | fn pt_isarg to bool |
| 504 | a = new uchar 16 0 |
| 505 | if ne argcnt 2 |
| 506 | ret false |
| 507 | end |
| 508 | ret eq argread 1 a 0 16 6 |
| 509 | end |
| 510 | |
| 511 | note With one argument (any 6 bytes, "stress"), run only the stress phase. |
| 512 | fn main to int |
| 513 | if pt_isarg |
| 514 | pt_stress |
| 515 | ret 0 |
| 516 | end |
| 517 | pt_refusals |
| 518 | pt_epochs |
| 519 | pt_race |
| 520 | pt_orphan |
| 521 | pt_procs |
| 522 | pt_stress |
| 523 | if ne write 1 pok 0 len pok long len pok |
| 524 | ret 100 |
| 525 | end |
| 526 | ret 0 |
| 527 | end |