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 / live-cpu.om

440 lines · 6625 bytes Raw Blame History
1note seed/node/live-cpu.om -- the DRA-399 core 8/9 contamination sampler.
2note live-cpu STOP WATCH OWNDIR OLLM (absolute paths): every 10 s, until
3note the file STOP
4note exists, read /proc/*/task/*/stat and print one line
5note CPU <realns> cwo <c> run89 <r> own <o> foreign <f> [ pid/tid:comm]...
6note run89 counts the threads in state R whose processor (stat field 39)
7note is 8 or 9. own counts those whose process argv0 starts with OWNDIR
8note or is exactly OLLM (the pinned ollm); foreign counts the
9note rest, and each foreign thread is named. cwo counts the lines of WATCH
10note that start with "CWO " (the objective boundaries; 0 while WATCH is
11note absent). Only read-only /proc and WATCH reads. Exit 0 once STOP
12note appears, 2 on bad usage.
13
14text kproc "/proc/"
15text ktask "/task/"
16text kstat "/stat"
17text kcmd "/cmdline"
18text kcwo "CWO "
19text kcpu "CPU"
20text kcwow "cwo"
21text krun "run89"
22text kownw "own"
23text kfor "foreign"
24text kdone "CPU done samples"
25text kuse "CPU usage: live-cpu STOP WATCH OWNDIR OLLM (absolute paths)"
26
27note ca: argument k at 1024 (k - 1), lengths in cl.
28global ca uchar 4096
29global cl ulong 4
30global cp uchar 1024
31global cpn ulong 1
32global co uchar 16384
33global con ulong 1
34global cs ulong 4
35
36fn c_dig v ulong d edit uchar 24 to ulong
37k = ulong 0
38x = v
39for i ulong 24
40put d k uchar add 48 rem x 10
41k = add k 1
42x = div x 10
43if eq x 0
44break
45end
46end
47ret k
48end
49
50fn co_c c ulong to void
51n = get con 0
52if or lt n 16300 and eq c 10 lt n 16384
53put co n uchar c
54put con 0 add n 1
55end
56end
57
58fn co_t a view uchar * to void
59for i ulong len a
60co_c ulong get a i
61end
62end
63
64fn co_n v ulong to void
65d = new uchar 24 0
66k = c_dig v d
67for j ulong k
68co_c ulong get d sub sub k j 1
69end
70end
71
72fn co_kv k view uchar * v ulong to void
73co_c 32
74co_t k
75co_c 32
76co_n v
77end
78
79fn co_e to void
80co_c 10
81n = get con 0
82p = ulong 0
83for it ulong 4096
84if ge p n
85break
86end
87r = write 1 co p sub n p
88if le r 0
89break
90end
91p = add p ulong r
92end
93put con 0 0
94end
95
96fn cp_c c ulong to void
97n = get cpn 0
98if lt n 1000
99put cp n uchar c
100put cpn 0 add n 1
101end
102end
103
104fn cp_t a view uchar * to void
105for i ulong len a
106cp_c ulong get a i
107end
108end
109
110fn cp_n v ulong to void
111d = new uchar 24 0
112k = c_dig v d
113for j ulong k
114cp_c ulong get d sub sub k j 1
115end
116end
117
118note Field k (0 = the state) of a stat line, counted from offset s.
119fn c_fld a view uchar * s ulong k ulong to ulong
120t = ulong 0
121v = ulong 0
122for i ulong sub len a s
123ch = ulong get a add s i
124if eq ch 32
125if eq t k
126ret v
127end
128t = add t 1
129else
130if eq t k
131v = add mul v 10 sub ch 48
132end
133end
134end
135ret v
136end
137
138note Is argv0 a (NUL-terminated in /proc cmdline) under OWNDIR (argument
139note 3) or exactly OLLM (argument 4)?
140fn c_cmdm a view uchar * to bool
141if c_pfa a 2
142ret true
143end
144if c_pfa a 3
145n = get cl 3
146if gt len a n
147ret eq get a n 0
148end
149end
150ret false
151end
152
153note Does a start with argument k + 1 (ca from 1024 k, length cl k)?
154fn c_pfa a view uchar * k ulong to bool
155n = get cl k
156if lt len a n
157ret false
158end
159o = mul k 1024
160for i ulong n bound 1024
161if ne get a i get ca add o i
162ret false
163end
164end
165ret true
166end
167
168fn c_own pid ulong to bool
169b = new uchar 512 0
170put cpn 0 0
171cp_t kproc
172cp_n pid
173cp_t kcmd
174fd = fopen cp 0 get cpn 0 0 0
175if lt fd 0
176ret false
177end
178n = read int fd b 0 512
179r = fclose int fd
180if le n 0
181ret false
182end
183ret c_cmdm view part b 0 ulong n
184end
185
186fn c_comm a view uchar * q ulong to void
187s = ulong 0
188for i ulong q
189if and eq s 0 eq get a i 40
190s = add i 1
191end
192end
193for i ulong sub q s
194co_c ulong get a add s i
195end
196end
197
198fn c_thr pid ulong tid ulong a view uchar * to void
199q = len a
200for i ulong len a
201if eq get a i 41
202q = i
203end
204end
205if ge add q 3 len a
206ret
207end
208if ne get a add q 2 82
209ret
210end
211cpu = c_fld a add q 2 36
212if and ne cpu 8 ne cpu 9
213ret
214end
215put cs 0 add get cs 0 1
216if c_own pid
217put cs 1 add get cs 1 1
218ret
219end
220put cs 2 add get cs 2 1
221co_c 32
222co_n pid
223co_c 47
224co_n tid
225co_c 58
226c_comm a q
227end
228
229fn c_tid pid ulong tid ulong to void
230b = new uchar 1024 0
231put cpn 0 0
232cp_t kproc
233cp_n pid
234cp_t ktask
235cp_n tid
236cp_t kstat
237fd = fopen cp 0 get cpn 0 0 0
238if lt fd 0
239ret
240end
241n = read int fd b 0 1024
242r = fclose int fd
243if le n 0
244ret
245end
246c_thr pid tid view part b 0 ulong n
247end
248
249note The decimal name of the dirent at p, or 0.
250fn c_num d view uchar 32768 p ulong to ulong
251v = ulong 0
252k = ulong 0
253for i ulong 24
254ch = ulong get d add add p 19 i
255if eq ch 0
256break
257end
258if or lt ch 48 gt ch 57
259ret 0
260end
261v = add mul v 10 sub ch 48
262k = add k 1
263end
264if eq k 0
265ret 0
266end
267ret v
268end
269
270fn c_task pid ulong to void
271d = new uchar 32768 0
272put cpn 0 0
273cp_t kproc
274cp_n pid
275cp_t ktask
276fd = fopen cp 0 get cpn 0 16384 0
277if lt fd 0
278ret
279end
280for it ulong 10000
281n = getdnts int fd d 0 32768
282if le n 0
283break
284end
285p = ulong 0
286while lt p ulong n bound 100000
287w = or ulong get d add p 16 shl ulong get d add p 17 8
288if eq w 0
289break
290end
291t = c_num d p
292if gt t 0
293c_tid pid t
294end
295p = add p w
296end
297end
298r = fclose int fd
299end
300
301fn c_scan to void
302d = new uchar 32768 0
303put cpn 0 0
304cp_t kproc
305fd = fopen cp 0 get cpn 0 16384 0
306if lt fd 0
307ret
308end
309for it ulong 10000
310n = getdnts int fd d 0 32768
311if le n 0
312break
313end
314p = ulong 0
315while lt p ulong n bound 100000
316w = or ulong get d add p 16 shl ulong get d add p 17 8
317if eq w 0
318break
319end
320t = c_num d p
321if gt t 0
322c_task t
323end
324p = add p w
325end
326end
327r = fclose int fd
328end
329
330note Lines of WATCH that start with "CWO ".
331fn c_cwo to ulong
332b = new uchar 65536 0
333fd = fopen ca 1024 get cl 1 0 0
334if lt fd 0
335ret 0
336end
337c = ulong 0
338m = ulong 0
339for it ulong 100000
340n = read int fd b 0 65536
341if le n 0
342break
343end
344for i ulong ulong n
345ch = ulong get b i
346if eq ch 10
347m = 0
348else
349if lt m 4
350if eq ch ulong get kcwo m
351m = add m 1
352if eq m 4
353c = add c 1
354end
355else
356m = 99
357end
358end
359end
360end
361end
362r = fclose int fd
363ret c
364end
365
366fn c_stop to bool
367fd = fopen ca 0 get cl 0 0 0
368if lt fd 0
369ret false
370end
371r = fclose int fd
372ret true
373end
374
375fn c_arg k ulong to bool
376n = argread k ca mul sub k 1 1024 1023
377if or lt n 2 gt n 1022
378ret false
379end
380put cl sub k 1 ulong n
381ret eq get ca mul sub k 1 1024 47
382end
383
384note c_scan has left the foreign thread names in co; print the counts
385note first, then those names, as one line.
386fn c_line to void
387s = new uchar 16384 0
388n = get con 0
389for i ulong n
390put s i get co i
391end
392put con 0 0
393co_t kcpu
394co_c 32
395co_n ulong realns
396co_kv kcwow c_cwo
397co_kv krun get cs 0
398co_kv kownw get cs 1
399co_kv kfor get cs 2
400for i ulong n
401co_c ulong get s i
402end
403co_e
404end
405
406fn c_once to void
407put cs 0 0
408put cs 1 0
409put cs 2 0
410put con 0 0
411c_scan
412c_line
413end
414
415fn main to int
416c = ulong argcnt
417ok = eq c 5
418if ok
419ok = and and c_arg 1 c_arg 2 and c_arg 3 c_arg 4
420end
421if not ok
422co_t kuse
423co_e
424ret 2
425end
426k = ulong 0
427for it ulong 100000
428c_once
429k = add k 1
430if c_stop
431break
432end
433q = sleepns 10000000000
434end
435co_t kdone
436co_c 32
437co_n k
438co_e
439ret 0
440end