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 / super.om
| 1 | note Omega Node worker supervision. Concatenate after transport.om and pool.om. |
| 2 | note The creator forks trusted workers from its own binary: sup_spawn returns 0 |
| 3 | note in the child, which runs its worker function and exits with its result. |
| 4 | note Death is seen without polling PIDs. Worker w alone holds the write end of |
| 5 | note its close-on-exec life pipe, so its exit hangs up the creator's read end. |
| 6 | note Every worker holds the read end of the creator's life pipe, whose only |
| 7 | note writer is the creator, so creator death hangs up every worker. |
| 8 | note Per worker: wake, stop and ack eventfds, kept across respawns. |
| 9 | note Workers must not exec: exec closes the life pipe and reads as death. |
| 10 | global swpid long 64 |
| 11 | global swwake int 64 |
| 12 | global swstop int 64 |
| 13 | global swack int 64 |
| 14 | global swlife int 64 |
| 15 | global swst ulong 64 |
| 16 | global swpw ulong 64 |
| 17 | global swpar int 2 |
| 18 | global swme ulong 1 |
| 19 | global swws uchar 4 |
| 20 | global swpf uchar 8 |
| 21 | global swpoll uchar 1024 |
| 22 | |
| 23 | fn sup_fd off ulong to int |
| 24 | v = ulong get swpf off |
| 25 | v = or v shl ulong get swpf add off 1 8 |
| 26 | v = or v shl ulong get swpf add off 2 16 |
| 27 | v = or v shl ulong get swpf add off 3 24 |
| 28 | ret int v |
| 29 | end |
| 30 | |
| 31 | note Poll record j at swpoll offset off; a negative fd is stored as itself |
| 32 | note (bit pattern) so the kernel ignores that record. |
| 33 | fn sup_gpfd off ulong fd int to void |
| 34 | u = bitcast uint fd |
| 35 | for i ulong 4 |
| 36 | put swpoll add off i uchar and shr u uint mul i 8 255 |
| 37 | end |
| 38 | put swpoll add off 4 1 |
| 39 | put swpoll add off 5 0 |
| 40 | put swpoll add off 6 0 |
| 41 | put swpoll add off 7 0 |
| 42 | end |
| 43 | |
| 44 | fn sup_lpfd p edit uchar 24 off ulong fd int to void |
| 45 | for i ulong 4 |
| 46 | put p add off i uchar and shr uint fd uint mul i 8 255 |
| 47 | end |
| 48 | put p add off 4 1 |
| 49 | end |
| 50 | |
| 51 | note Read and reset an eventfd counter: the number of hints since the last |
| 52 | note take, 0 when none is pending, or a negative errno. |
| 53 | fn sup_take fd int to long |
| 54 | buf = new uchar 8 0 |
| 55 | for i ulong 64 |
| 56 | r = read fd buf 0 8 |
| 57 | if eq r 8 |
| 58 | v = ulong 0 |
| 59 | for j ulong 8 |
| 60 | v = or v shl ulong get buf j mul j 8 |
| 61 | end |
| 62 | ret long and v 9223372036854775807 |
| 63 | end |
| 64 | if eq r neg long 11 |
| 65 | ret 0 |
| 66 | end |
| 67 | if ne r neg long 4 |
| 68 | if lt r 0 |
| 69 | ret r |
| 70 | end |
| 71 | ret neg long 5 |
| 72 | end |
| 73 | end |
| 74 | ret neg long 4 |
| 75 | end |
| 76 | |
| 77 | fn sup_closeall to void |
| 78 | for i ulong 64 |
| 79 | if ge get swwake i 0 |
| 80 | r = fclose get swwake i |
| 81 | end |
| 82 | if ge get swstop i 0 |
| 83 | r = fclose get swstop i |
| 84 | end |
| 85 | if ge get swack i 0 |
| 86 | r = fclose get swack i |
| 87 | end |
| 88 | if ge get swlife i 0 |
| 89 | r = fclose get swlife i |
| 90 | end |
| 91 | put swwake i neg int 1 |
| 92 | put swstop i neg int 1 |
| 93 | put swack i neg int 1 |
| 94 | put swlife i neg int 1 |
| 95 | end |
| 96 | for i ulong 2 |
| 97 | if ge get swpar i 0 |
| 98 | r = fclose get swpar i |
| 99 | end |
| 100 | put swpar i neg int 1 |
| 101 | end |
| 102 | end |
| 103 | |
| 104 | note Creator: create the creator life pipe and three eventfds per worker slot. |
| 105 | note Call once after pool_new and before sup_spawn. 0 or a negative errno; |
| 106 | note a failure closes everything this call opened. |
| 107 | fn sup_init c long to long |
| 108 | if not pool_mine c |
| 109 | ret neg long 6 |
| 110 | end |
| 111 | for i ulong 64 |
| 112 | put swpid i 0 |
| 113 | put swst i 0 |
| 114 | put swwake i neg int 1 |
| 115 | put swstop i neg int 1 |
| 116 | put swack i neg int 1 |
| 117 | put swlife i neg int 1 |
| 118 | end |
| 119 | put swme 0 0 |
| 120 | put swpar 0 neg int 1 |
| 121 | put swpar 1 neg int 1 |
| 122 | r = pipefds swpf 0 8 524288 |
| 123 | if lt r 0 |
| 124 | ret r |
| 125 | end |
| 126 | put swpar 0 sup_fd 0 |
| 127 | put swpar 1 sup_fd 4 |
| 128 | n = mapget c 48 8 |
| 129 | for i ulong n bound 64 |
| 130 | a = eventfd uint 0 |
| 131 | if lt a 0 |
| 132 | sup_closeall |
| 133 | ret a |
| 134 | end |
| 135 | put swwake i int a |
| 136 | a = eventfd uint 0 |
| 137 | if lt a 0 |
| 138 | sup_closeall |
| 139 | ret a |
| 140 | end |
| 141 | put swstop i int a |
| 142 | a = eventfd uint 0 |
| 143 | if lt a 0 |
| 144 | sup_closeall |
| 145 | ret a |
| 146 | end |
| 147 | put swack i int a |
| 148 | end |
| 149 | ret 0 |
| 150 | end |
| 151 | |
| 152 | note Creator: fork worker w. Returns 0 in the child, the child PID in the |
| 153 | note creator, -7 bad slot, -16 slot still live, or a negative errno. The |
| 154 | note child keeps only its own life write end; the creator keeps the read end. |
| 155 | fn sup_spawn c long w ulong to long |
| 156 | if not pool_mine c |
| 157 | ret neg long 6 |
| 158 | end |
| 159 | if ge w mapget c 48 8 |
| 160 | ret neg long 7 |
| 161 | end |
| 162 | if ne get swpid w 0 |
| 163 | ret neg long 16 |
| 164 | end |
| 165 | r = pipefds swpf 0 8 524288 |
| 166 | if lt r 0 |
| 167 | ret r |
| 168 | end |
| 169 | rd = sup_fd 0 |
| 170 | wr = sup_fd 4 |
| 171 | pid = fork |
| 172 | if eq pid 0 |
| 173 | r = fclose get swpar 1 |
| 174 | put swpar 1 neg int 1 |
| 175 | r = fclose rd |
| 176 | put swlife w wr |
| 177 | put swme 0 add w 1 |
| 178 | ret 0 |
| 179 | end |
| 180 | r = fclose wr |
| 181 | if lt pid 0 |
| 182 | r = fclose rd |
| 183 | ret pid |
| 184 | end |
| 185 | put swpid w pid |
| 186 | put swlife w rd |
| 187 | r = pool_setw c w ulong pid 1 |
| 188 | ret pid |
| 189 | end |
| 190 | |
| 191 | note Worker: 3 creator gone, 2 stop, 1 wake hint, 0 timeout, negative errno. |
| 192 | note Creator loss outranks stop, which outranks work. A set stop flag makes |
| 193 | note the poll nonblocking. Hints are drained; rescan shared state after 1. |
| 194 | fn sup_wwait c long w ulong interval long to long |
| 195 | p = new uchar 24 0 |
| 196 | if ge w 64 |
| 197 | ret neg long 7 |
| 198 | end |
| 199 | iv = interval |
| 200 | if pool_stopped c |
| 201 | iv = 0 |
| 202 | end |
| 203 | sup_lpfd p 0 get swwake w |
| 204 | sup_lpfd p 8 get swstop w |
| 205 | sup_lpfd p 16 get swpar 0 |
| 206 | r = pollfds p 0 24 iv |
| 207 | if lt r 0 |
| 208 | ret r |
| 209 | end |
| 210 | if ne get p 22 0 |
| 211 | ret 3 |
| 212 | end |
| 213 | if pool_stopped c |
| 214 | ret 2 |
| 215 | end |
| 216 | if ne and get p 14 56 0 |
| 217 | ret neg long 5 |
| 218 | end |
| 219 | if ne and get p 14 1 0 |
| 220 | r = node_drain get swstop w |
| 221 | ret 2 |
| 222 | end |
| 223 | if ne and get p 6 56 0 |
| 224 | ret neg long 5 |
| 225 | end |
| 226 | if ne and get p 6 1 0 |
| 227 | r = node_drain get swwake w |
| 228 | if lt r 0 |
| 229 | ret r |
| 230 | end |
| 231 | ret 1 |
| 232 | end |
| 233 | ret 0 |
| 234 | end |
| 235 | |
| 236 | note Worker or creator: post a hint on worker w's ack eventfd. |
| 237 | fn sup_ack w ulong to long |
| 238 | if ge w 64 |
| 239 | ret neg long 7 |
| 240 | end |
| 241 | ret node_notify get swack w |
| 242 | end |
| 243 | |
| 244 | note Creator: wake worker w with a work hint. |
| 245 | fn sup_wake w ulong to long |
| 246 | if ge w 64 |
| 247 | ret neg long 7 |
| 248 | end |
| 249 | if le get swpid w 0 |
| 250 | ret neg long 3 |
| 251 | end |
| 252 | ret node_notify get swwake w |
| 253 | end |
| 254 | |
| 255 | fn sup_live c long to ulong |
| 256 | n = mapget c 48 8 |
| 257 | k = ulong 0 |
| 258 | for w ulong n bound 64 |
| 259 | if gt get swpid w 0 |
| 260 | k = add k 1 |
| 261 | end |
| 262 | end |
| 263 | ret k |
| 264 | end |
| 265 | |
| 266 | note Creator: reap worker w, then release what it held. A worker that has not |
| 267 | note exited within about one second gets SIGKILL first. Only after the exact |
| 268 | note PID is reaped are its hold, PID and life pipe released. Returns the raw |
| 269 | note wait status, -3 no live worker, or the waitpid errno (nothing released). |
| 270 | fn sup_reap c long w ulong to long |
| 271 | if ge w 64 |
| 272 | ret neg long 7 |
| 273 | end |
| 274 | pid = get swpid w |
| 275 | if le pid 0 |
| 276 | ret neg long 3 |
| 277 | end |
| 278 | done = false |
| 279 | for t ulong 100 |
| 280 | r = waitpid int pid swws 0 4 1 |
| 281 | if eq r pid |
| 282 | done = true |
| 283 | break |
| 284 | end |
| 285 | if ne r 0 |
| 286 | ret r |
| 287 | end |
| 288 | r = sleepns 10000000 |
| 289 | end |
| 290 | if not done |
| 291 | r = killpid int pid 9 |
| 292 | r = waitpid int pid swws 0 4 0 |
| 293 | if ne r pid |
| 294 | ret r |
| 295 | end |
| 296 | end |
| 297 | st = ulong get swws 0 |
| 298 | st = or st shl ulong get swws 1 8 |
| 299 | put swst w st |
| 300 | r = fclose get swlife w |
| 301 | put swlife w neg int 1 |
| 302 | put swpid w 0 |
| 303 | r = pool_clear c w |
| 304 | r = pool_setw c w 0 2 |
| 305 | ret long st |
| 306 | end |
| 307 | |
| 308 | note Creator: wait up to interval ns for deaths and, when acks is true, acks. |
| 309 | note Ack counts are left pending for sup_take. Every worker whose life pipe |
| 310 | note hung up is reaped. Returns the number reaped, or a negative errno. |
| 311 | fn sup_pollx c long interval long acks bool to long |
| 312 | n = mapget c 48 8 |
| 313 | m = ulong 0 |
| 314 | for w ulong n bound 64 |
| 315 | if gt get swpid w 0 |
| 316 | o = mul m 16 |
| 317 | if acks |
| 318 | sup_gpfd o get swack w |
| 319 | else |
| 320 | sup_gpfd o neg int 1 |
| 321 | end |
| 322 | sup_gpfd add o 8 get swlife w |
| 323 | put swpw m w |
| 324 | m = add m 1 |
| 325 | end |
| 326 | end |
| 327 | if eq m 0 |
| 328 | ret 0 |
| 329 | end |
| 330 | r = pollfds swpoll 0 mul m 16 interval |
| 331 | if lt r 0 |
| 332 | ret r |
| 333 | end |
| 334 | dead = long 0 |
| 335 | for j ulong m bound 64 |
| 336 | o = add mul j 16 14 |
| 337 | if ne get swpoll o 0 |
| 338 | r = sup_reap c get swpw j |
| 339 | if lt r 0 |
| 340 | ret r |
| 341 | end |
| 342 | dead = add dead 1 |
| 343 | end |
| 344 | end |
| 345 | ret dead |
| 346 | end |
| 347 | |
| 348 | note Creator: wait up to interval ns for acks and deaths (see sup_pollx). |
| 349 | fn sup_poll c long interval long to long |
| 350 | ret sup_pollx c interval true |
| 351 | end |
| 352 | |
| 353 | note Creator: stop admission, post every live worker's stop eventfd, then reap |
| 354 | note until all have exited or interval ns pass; survivors get SIGKILL and are |
| 355 | note reaped. Returns how many had to be killed, or a refusal. |
| 356 | fn sup_stop c long interval long to long |
| 357 | r = pool_stop c |
| 358 | if lt r 0 |
| 359 | ret r |
| 360 | end |
| 361 | n = mapget c 48 8 |
| 362 | for w ulong n bound 64 |
| 363 | if gt get swpid w 0 |
| 364 | r = node_notify get swstop w |
| 365 | end |
| 366 | end |
| 367 | dl = add monons interval |
| 368 | for t ulong 100000 |
| 369 | if eq sup_live c 0 |
| 370 | ret 0 |
| 371 | end |
| 372 | now = monons |
| 373 | if ge now dl |
| 374 | break |
| 375 | end |
| 376 | r = sup_pollx c sub dl now false |
| 377 | if lt r 0 |
| 378 | if ne r neg long 4 |
| 379 | break |
| 380 | end |
| 381 | end |
| 382 | end |
| 383 | killed = long 0 |
| 384 | for w ulong n bound 64 |
| 385 | if gt get swpid w 0 |
| 386 | r = killpid int get swpid w 9 |
| 387 | r = sup_reap c w |
| 388 | killed = add killed 1 |
| 389 | end |
| 390 | end |
| 391 | ret killed |
| 392 | end |