interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / seed / node / super.om

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