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