Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aien-train/blob/main/tools/omegallm/ctlsamp/ctlsamp.om # aien-train AIEN training programme: language-model history, GB10 constraints and the route to training on the project engine. Plans and runnable checks are distinguished. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aien-train / tools / omegallm / ctlsamp / ctlsamp.om` 1432 lines ยท 23122 bytes Raw Blame History 1 om2 2 note ctlsamp: wsp controller telemetry sampler. A measurement instrument, not product. 3 note Reads world-readable kernel files and runs one-shot nvidia-smi and upsc. No root, 4 note no GPU work. Appends one JSON object per line to OUT, keys sorted, in the row format 5 note of evidence/OMEGALLM/wsp-ctl-traces-`20261107`/trace-001.jsonl plus the five UPS keys. 6 note Usage: ctlsamp OUT [interval_s] [count] [nvsmi_path] [upsc_path] [raw_log] 7 note Exit: 0 done, 2 usage or open error, 3 a kernel file could not be read. 8 text dnv "/usr/bin/nvidia-smi" 9 text dups "/usr/bin/upsc" 10 text nvq "--query-gpu=utilization.gpu,utilization.memory,power.draw,temperature.gpu,clocks.sm,pstate" 11 text nvf "--format=csv,noheader,nounits" 12 text upsn "spark" 13 text nul "/dev/null" 14 text pst "/proc/stat" 15 text pla "/proc/loadavg" 16 text pme "/proc/meminfo" 17 text ppc "/proc/pressure/cpu" 18 text ppm "/proc/pressure/memory" 19 text ppi "/proc/pressure/io" 20 text tzpre "/sys/class/thermal/thermal_zone" 21 text tzsuf "/temp" 22 text usage "usage: ctlsamp OUT [interval_s>=5] [count] [nvsmi_path] [upsc_path] [raw_log]\n" 23 text eopen "ctlsamp: cannot open output\n" 24 text eread "ctlsamp: cannot read a kernel file\n" 25 text ewrite "ctlsamp: write to output failed or was short\n" 26 text sep ", " 27 text knull "null" 28 text ktz "tz" 29 text kc "c" 30 text kbatt "\"battery_runtime_s\": " 31 text kcore "\"core_busy_pct\": {" 32 text kgerr "\"gpu_error\": " 33 text kvolt "\"input_voltage\": " 34 text kload "\"loadavg\": [" 35 text krun "\"runnable\": " 36 text kmem "\"mem\": {" 37 text kmav "\"MemAvailable_kb\": " 38 text kmto "\"MemTotal_kb\": " 39 text kmsf "\"SwapFree_kb\": " 40 text kmst "\"SwapTotal_kb\": " 41 text kpc "\"psi_cpu\": {" 42 text kpi "\"psi_io\": {" 43 text kpm "\"psi_mem\": {" 44 text kcost "\"sample_cost_us\": " 45 text kseq "\"seq\": " 46 text kthm "\"thermal_c\": {" 47 text kunix "\"ts_unix\": " 48 text kutc "\"ts_utc\": " 49 text kuerr "\"ups_error\": " 50 text kuload "\"ups_load_pct\": " 51 text kunom "\"ups_realpower_nominal_w\": " 52 text kustat "\"ups_status\": " 53 text kg0 "\"gpu_util_pct\": " 54 text kg1 "\"gpu_mem_util_pct\": " 55 text kg2 "\"gpu_power_w\": " 56 text kg3 "\"gpu_temp_c\": " 57 text kg4 "\"gpu_sm_mhz\": " 58 text kg5 "\"gpu_pstate\": " 59 text kfull "\"full_" 60 text ksome "\"some_" 61 text ka10 "avg10\": " 62 text ka300 "avg300\": " 63 text ka60 "avg60\": " 64 text ktot "total\": " 65 text wsome "some" 66 text wa10 "avg10" 67 text wa60 "avg60" 68 text wa300 "avg300" 69 text wtot "total" 70 text wmto "MemTotal" 71 text wmav "MemAvailable" 72 text wmst "SwapTotal" 73 text wmsf "SwapFree" 74 text wuload "ups.load" 75 text wunom "ups.realpower.nominal" 76 text wustat "ups.status" 77 text wvolt "input.voltage" 78 text wbatt "battery.runtime" 79 text rtimeo "timeout" 80 text rrc "rc=" 81 text rtrunc "truncated" 82 text rcomma "," 83 text rhdr "== " 84 text tgpu "gpu" 85 text tups "ups" 86 global ob uchar 65536 87 global op ulong 1 88 global fb uchar 65536 89 global fl ulong 1 90 global pb uchar 4096 91 global cb uchar 16384 92 global cl ulong 2 93 global cs long 2 94 global tr uchar 2 95 global sk uchar 65536 96 global xp uchar 4096 97 global xl ulong 1 98 global fds uchar 8 99 global wst uchar 4 100 global db uchar 32 101 global ab uchar 4096 102 global gpth uchar 4096 103 global gpl ulong 1 104 global upth uchar 4096 105 global upl ulong 1 106 global ct ulong 64 107 global ci ulong 64 108 global pt ulong 64 109 global pi ulong 64 110 global pm long 24 111 global pd ulong 24 112 global pp uchar 24 113 global tz long 64 114 global ntz ulong 1 115 global mv ulong 4 116 global mp uchar 4 117 global lb uchar 512 118 global ll ulong 1 119 global nm long 1 120 global nd ulong 1 121 global fs ulong 16 122 global fe ulong 16 123 global nf ulong 1 124 global us ulong 8 125 global ue ulong 8 126 global uf uchar 8 127 global ofd long 1 128 global rfd long 1 129 note fd that oc spills a full ob into: ofd while a row is built, rfd while a raw record is 130 global odst long 1 131 global rseq ulong 1 132 global dlg long 1 133 global lgp long 1 134 global ivg long 1 135 136 note ---- output buffer ---- 137 note one output byte; a full ob is written to odst first, so a long row (control bytes escape to 138 note 6 bytes each) goes out in more than one write instead of running past the buffer 139 fn oc c uchar to void 140 i = get op 0 141 if ge i 65536 142 r = write int get odst 0 ob 0 i 143 wchk r i 144 i = ulong 0 145 end 146 put ob i c 147 put op 0 add i 1 148 end 149 fn ot t view uchar * to void 150 for i ulong len t 151 oc get t i 152 end 153 end 154 fn osep to void 155 i = get op 0 156 if gt i 0 157 c = get ob sub i 1 158 if and ne c 123 ne c 91 159 ot sep 160 end 161 end 162 end 163 fn ou v ulong to void 164 x = v 165 n = ulong 0 166 go = true 167 while go bound 24 168 put db n uchar add 48 rem x 10 169 n = add n 1 170 x = div x 10 171 go = ne x 0 172 end 173 while gt n 0 bound 24 174 n = sub n 1 175 oc get db n 176 end 177 end 178 note print exactly k decimal digits of f, zero padded 179 fn ofix f ulong k ulong to void 180 n = ulong 0 181 x = f 182 while lt n k bound 24 183 put db n uchar add 48 rem x 10 184 x = div x 10 185 n = add n 1 186 end 187 while gt n 0 bound 24 188 n = sub n 1 189 oc get db n 190 end 191 end 192 fn pw10 d ulong to ulong 193 p = ulong 1 194 for i ulong d 195 p = mul p 10 196 end 197 ret p 198 end 199 note a JSON float equal to m / 10^d in Python repr form: trailing fraction zeros 200 note dropped, at least one fraction digit (93 -> 93.0, 44.60 -> 44.6) 201 fn odec m long d ulong to void 202 x = ulong 0 203 if lt m 0 204 oc uchar 45 205 x = ulong neg m 206 else 207 x = ulong m 208 end 209 p = pw10 d 210 ou div x p 211 oc uchar 46 212 if eq d 0 213 oc uchar 48 214 ret 215 end 216 f = rem x p 217 k = d 218 while and gt k 1 eq rem f 10 0 bound 24 219 f = div f 10 220 k = sub k 1 221 end 222 ofix f k 223 end 224 note awk "%.6g"-style number of the shell UPS sampler: integer when the fraction is zero 225 fn oawk m long d ulong to void 226 p = long pw10 d 227 if eq rem m p 0 228 if lt m 0 229 oc uchar 45 230 ou ulong neg div m p 231 else 232 ou ulong div m p 233 end 234 ret 235 end 236 odec m d 237 end 238 note one byte of a JSON string, escaped the way Python json.dumps escapes ASCII: quote, 239 note backslash, \b \t \n \f \r, other bytes below 32 as \u00XX 240 fn ochr c uchar to void 241 if or eq c 34 eq c 92 242 oc uchar 92 243 oc c 244 ret 245 end 246 if ge c 32 247 oc c 248 ret 249 end 250 oc uchar 92 251 if eq c 8 252 oc uchar 98 253 ret 254 end 255 if eq c 9 256 oc uchar 116 257 ret 258 end 259 if eq c 10 260 oc uchar 110 261 ret 262 end 263 if eq c 12 264 oc uchar 102 265 ret 266 end 267 if eq c 13 268 oc uchar 114 269 ret 270 end 271 oc uchar 117 272 oc uchar 48 273 oc uchar 48 274 oc add 48 shr c 4 275 h = and c 15 276 if lt h 10 277 oc add 48 h 278 else 279 oc add 87 h 280 end 281 end 282 note a JSON string from fb[s..e) (ostr) or cb[s..e) (ostrg) 283 fn ostr s ulong e ulong to void 284 oc uchar 34 285 i = s 286 while lt i e bound 65536 287 ochr get fb i 288 i = add i 1 289 end 290 oc uchar 34 291 end 292 fn ostrg base ulong e ulong to void 293 oc uchar 34 294 i = base 295 while lt i e bound 16384 296 ochr get cb i 297 i = add i 1 298 end 299 oc uchar 34 300 end 301 302 note ---- parsing ---- 303 fn isdig c uchar to bool 304 ret and ge c 48 le c 57 305 end 306 note parse cb[s..e) as [-]digits[.digits] into nm (mantissa) and nd (fraction digits) 307 fn pnumc s ulong e ulong to bool 308 i = s 309 neg_ = false 310 if lt i e 311 if eq get cb i 45 312 neg_ = true 313 i = add i 1 314 end 315 end 316 m = long 0 317 d = ulong 0 318 dig = ulong 0 319 dot = false 320 ok = true 321 while lt i e bound 16384 322 c = get cb i 323 if isdig c 324 m = add mul m 10 long sub c 48 325 dig = add dig 1 326 if dot 327 d = add d 1 328 end 329 else 330 if and eq c 46 not dot 331 dot = true 332 else 333 ok = false 334 end 335 end 336 i = add i 1 337 end 338 if neg_ 339 m = neg m 340 end 341 put nm 0 m 342 put nd 0 d 343 ret and ok gt dig 0 344 end 345 note parse fb[s..e) the same way 346 fn pnumf s ulong e ulong to bool 347 i = s 348 m = long 0 349 d = ulong 0 350 dig = ulong 0 351 dot = false 352 ok = true 353 while lt i e bound 65536 354 c = get fb i 355 if isdig c 356 m = add mul m 10 long sub c 48 357 dig = add dig 1 358 if dot 359 d = add d 1 360 end 361 else 362 if and eq c 46 not dot 363 dot = true 364 else 365 ok = false 366 end 367 end 368 i = add i 1 369 end 370 put nm 0 m 371 put nd 0 d 372 ret and ok gt dig 0 373 end 374 note does fb[s..e) equal t 375 fn eqf s ulong e ulong t view uchar * to bool 376 if ne sub e s len t 377 ret false 378 end 379 for i ulong len t 380 if ne get fb add s i get t i 381 ret false 382 end 383 end 384 ret true 385 end 386 fn eqc s ulong e ulong t view uchar * to bool 387 if ne sub e s len t 388 ret false 389 end 390 for i ulong len t 391 if ne get cb add s i get t i 392 ret false 393 end 394 end 395 ret true 396 end 397 note end of the token in fb starting at s (space, tab, newline, '=' or ':' stop it) 398 fn tokend s ulong n ulong to ulong 399 i = s 400 while lt i n bound 65536 401 c = get fb i 402 if or or eq c 32 eq c 10 or eq c 9 or eq c 61 eq c 58 403 ret i 404 end 405 i = add i 1 406 end 407 ret i 408 end 409 fn skipsp s ulong n ulong to ulong 410 i = s 411 while lt i n bound 65536 412 c = get fb i 413 if and ne c 32 ne c 9 414 ret i 415 end 416 i = add i 1 417 end 418 ret i 419 end 420 fn lineend s ulong n ulong to ulong 421 i = s 422 while lt i n bound 65536 423 if eq get fb i 10 424 ret i 425 end 426 i = add i 1 427 end 428 ret i 429 end 430 431 note ---- files ---- 432 fn setp t view uchar * to ulong 433 for i ulong len t 434 put pb i get t i 435 end 436 ret len t 437 end 438 fn rawlog tag view uchar * n ulong src ulong to void 439 note src 0: fb[0..n), 1: cb[0..n), 2: cb[8192..8192+n) 440 if lt get rfd 0 0 441 ret 442 end 443 put odst 0 get rfd 0 444 put op 0 0 445 ot rhdr 446 ou get rseq 0 447 oc uchar 32 448 ot tag 449 rawend n src 450 end 451 note the same record, tagged with the path pb[0..pn) (thermal zones) 452 fn rawlogp pn ulong n ulong to void 453 if lt get rfd 0 0 454 ret 455 end 456 put odst 0 get rfd 0 457 put op 0 0 458 ot rhdr 459 ou get rseq 0 460 oc uchar 32 461 for i ulong pn 462 oc get pb i 463 end 464 rawend n 0 465 end 466 fn rawend n ulong src ulong to void 467 oc uchar 32 468 ou n 469 oc uchar 10 470 fd = int get rfd 0 471 r = write fd ob 0 get op 0 472 wchk r get op 0 473 if eq src 0 474 r = write fd fb 0 n 475 wchk r n 476 end 477 if eq src 1 478 r = write fd cb 0 n 479 wchk r n 480 end 481 if eq src 2 482 r = write fd cb 8192 n 483 wchk r n 484 end 485 put op 0 0 486 oc uchar 10 487 r = write fd ob 0 1 488 wchk r 1 489 put op 0 0 490 end 491 note read the path pb[0..pn) into fb; return byte count or negative errno 492 fn rdf pn ulong to long 493 fd = fopen pb 0 pn 524288 0 494 if lt fd 0 495 ret fd 496 end 497 n = ulong 0 498 go = true 499 while go bound 70000 500 k = read int fd fb n sub len fb n 501 if lt k 0 502 r = fclose int fd 503 ret k 504 end 505 if eq k 0 506 go = false 507 else 508 n = add n ulong k 509 if eq n len fb 510 go = false 511 end 512 end 513 end 514 r = fclose int fd 515 put fl 0 n 516 ret long n 517 end 518 note a write to OUT or raw_log that fails or comes back short stops the sampler (exit 4) instead 519 note of losing part of a row while it carries on with exit 0 520 fn wchk r long n ulong to void 521 if ne r long n 522 k = write 2 ewrite 0 len ewrite 523 exit 4 524 end 525 end 526 fn mustrd t view uchar * to ulong 527 pn = setp t 528 r = rdf pn 529 if lt r 0 530 k = write 2 eread 0 len eread 531 exit 3 532 end 533 rawlog t ulong r 0 534 ret ulong r 535 end 536 537 note ---- /proc/stat per-core totals ---- 538 fn iscpu s ulong to bool 539 if ne get fb s 99 540 ret false 541 end 542 if ne get fb add s 1 112 543 ret false 544 end 545 ret isdig get fb add s 3 546 end 547 fn readstat to void 548 n = mustrd pst 549 s = ulong 0 550 while lt s n bound 4096 551 e = lineend s n 552 if gt sub e s 3 553 if iscpu s 554 statln s e 555 end 556 end 557 s = add e 1 558 end 559 end 560 fn statln s ulong e ulong to void 561 i = add s 3 562 core = ulong 0 563 while isdig get fb i bound 8 564 core = add mul core 10 ulong sub get fb i 48 565 i = add i 1 566 end 567 tot = ulong 0 568 idl = ulong 0 569 for f ulong 8 570 i = skipsp i e 571 j = tokend i e 572 ok = pnumf i j 573 v = ulong get nm 0 574 tot = add tot v 575 if or eq f 3 eq f 4 576 idl = add idl v 577 end 578 i = j 579 end 580 if lt core 64 581 put ct core tot 582 put ci core idl 583 end 584 end 585 fn savestat to void 586 for c ulong 64 587 put pt c get ct c 588 put pi c get ci c 589 end 590 end 591 note round(100*(dt-di)/dt, 2) as hundredths, half up; -1 when dt is 0 592 fn busy c ulong to long 593 dt = sub get ct c get pt c 594 di = sub get ci c get pi c 595 if eq dt 0 596 ret long -1 597 end 598 q = mul 10000 sub dt di 599 ret long div add mul q 2 dt mul dt 2 600 end 601 fn ocore c ulong to void 602 osep 603 oc uchar 34 604 ot kc 605 ou c 606 oc uchar 34 607 oc uchar 58 608 oc uchar 32 609 b = busy c 610 if lt b 0 611 ot knull 612 else 613 odec b 2 614 end 615 end 616 fn ocores to void 617 osep 618 ot kcore 619 ocore 0 620 ocore 1 621 for i ulong 10 622 ocore add 10 i 623 end 624 for i ulong 8 625 ocore add 2 i 626 end 627 oc uchar 125 628 end 629 630 note ---- pressure ---- 631 fn readpsi k ulong t view uchar * to void 632 for j ulong 8 633 put pp add mul k 8 j 0 634 end 635 n = mustrd t 636 s = ulong 0 637 while lt s n bound 64 638 e = lineend s n 639 psiln k s e 640 s = add e 1 641 end 642 end 643 fn psiln k ulong s ulong e ulong to void 644 j = tokend s e 645 base = mul k 8 646 if eqf s j wsome 647 base = add base 4 648 end 649 i = j 650 while lt i e bound 16 651 i = skipsp i e 652 w = tokend i e 653 v = add w 1 654 x = tokend v e 655 ok = pnumf v x 656 slot = ulong 99 657 if eqf i w wa10 658 slot = 0 659 end 660 if eqf i w wa300 661 slot = 1 662 end 663 if eqf i w wa60 664 slot = 2 665 end 666 if eqf i w wtot 667 slot = 3 668 end 669 if and ok lt slot 4 670 put pm add base slot get nm 0 671 put pd add base slot get nd 0 672 put pp add base slot 1 673 end 674 i = x 675 end 676 end 677 fn opsi1 k ulong j ulong to void 678 x = add mul k 8 j 679 if eq get pp x 0 680 ret 681 end 682 osep 683 if lt j 4 684 ot kfull 685 else 686 ot ksome 687 end 688 r = rem j 4 689 if eq r 0 690 ot ka10 691 end 692 if eq r 1 693 ot ka300 694 end 695 if eq r 2 696 ot ka60 697 end 698 if eq r 3 699 ot ktot 700 ou ulong get pm x 701 else 702 odec get pm x get pd x 703 end 704 end 705 fn opsi k ulong t view uchar * to void 706 osep 707 ot t 708 for j ulong 8 709 opsi1 k j 710 end 711 oc uchar 125 712 end 713 714 note ---- thermal ---- 715 fn readtz to void 716 put ntz 0 0 717 i = ulong 0 718 go = true 719 while go bound 64 720 pn = setp tzpre 721 pn = tzpath pn i 722 r = rdf pn 723 if lt r 0 724 go = false 725 else 726 rawlogp pn ulong r 727 e = tokend 0 ulong r 728 ok = pnumf 0 e 729 put tz i get nm 0 730 i = add i 1 731 put ntz 0 i 732 end 733 end 734 end 735 fn tzpath pn ulong i ulong to ulong 736 p = pn 737 if ge i 10 738 put pb p uchar add 48 div i 10 739 p = add p 1 740 end 741 put pb p uchar add 48 rem i 10 742 p = add p 1 743 for j ulong len tzsuf 744 put pb p get tzsuf j 745 p = add p 1 746 end 747 ret p 748 end 749 fn othm to void 750 osep 751 ot kthm 752 for i ulong get ntz 0 753 osep 754 oc uchar 34 755 ot ktz 756 ou i 757 oc uchar 34 758 oc uchar 58 759 oc uchar 32 760 odec get tz i 3 761 end 762 oc uchar 125 763 end 764 765 note ---- meminfo and loadavg ---- 766 fn readmem to void 767 for j ulong 4 768 put mp j 0 769 end 770 n = mustrd pme 771 s = ulong 0 772 while lt s n bound 4096 773 e = lineend s n 774 k = tokend s e 775 v = skipsp add k 1 e 776 w = tokend v e 777 ok = pnumf v w 778 slot = ulong 99 779 if eqf s k wmav 780 slot = 0 781 end 782 if eqf s k wmto 783 slot = 1 784 end 785 if eqf s k wmsf 786 slot = 2 787 end 788 if eqf s k wmst 789 slot = 3 790 end 791 if and ok lt slot 4 792 put mv slot ulong get nm 0 793 put mp slot 1 794 end 795 s = add e 1 796 end 797 end 798 fn omem1 j ulong t view uchar * to void 799 if eq get mp j 0 800 ret 801 end 802 osep 803 ot t 804 ou get mv j 805 end 806 fn omem to void 807 osep 808 ot kmem 809 omem1 0 kmav 810 omem1 1 kmto 811 omem1 2 kmsf 812 omem1 3 kmst 813 oc uchar 125 814 end 815 fn readload to void 816 n = mustrd pla 817 if gt n len lb 818 n = len lb 819 end 820 for i ulong n 821 put lb i get fb i 822 end 823 put ll 0 n 824 end 825 fn oload to void 826 n = get ll 0 827 for i ulong n 828 put fb i get lb i 829 end 830 osep 831 ot kload 832 s = ulong 0 833 for f ulong 3 834 s = skipsp s n 835 e = tokend s n 836 ok = pnumf s e 837 osep 838 odec get nm 0 get nd 0 839 s = e 840 end 841 oc uchar 93 842 end 843 fn orun to void 844 n = get ll 0 845 for i ulong n 846 put fb i get lb i 847 end 848 s = ulong 0 849 for f ulong 3 850 s = skipsp s n 851 s = tokend s n 852 end 853 osep 854 ot krun 855 s = skipsp s n 856 e = tokend s n 857 ostr s e 858 end 859 860 note ---- commands ---- 861 fn pkb c uchar to void 862 i = get xl 0 863 put xp i c 864 put xl 0 add i 1 865 end 866 fn pkhdr n ulong to void 867 pkb uchar and n 255 868 pkb uchar shr n 8 869 end 870 fn pkt t view uchar * to void 871 pkhdr len t 872 for i ulong len t 873 pkb get t i 874 end 875 end 876 fn pkgpu to void 877 put xl 0 0 878 pkhdr 3 879 n = get gpl 0 880 pkhdr n 881 for i ulong n 882 pkb get gpth i 883 end 884 pkt nvq 885 pkt nvf 886 end 887 fn pkups to void 888 put xl 0 0 889 pkhdr 2 890 n = get upl 0 891 pkhdr n 892 for i ulong n 893 pkb get upth i 894 end 895 pkt upsn 896 end 897 fn fdword off ulong to int 898 x = ulong 0 899 for i ulong 4 900 v = ulong get fds add off i 901 x = or x shl v mul i 8 902 end 903 ret int x 904 end 905 note run the packet in xp with stdout into cb[base..base+8192), stderr to /dev/null, 906 note 4 s deadline. Store length in cl[slot] and status in cs[slot]: exit code, 907 note minus the signal, or -1000 for timeout, -1001 for a pipe or fork failure. 908 note tr[slot] is 1 when the child wrote more than 8192 bytes (the rest is read and dropped). 909 fn runcmd slot ulong base ulong to void 910 put cl slot 0 911 put cs slot -1001 912 put tr slot 0 913 r = pipefds fds 0 8 524288 914 if ne r 0 915 ret 916 end 917 rd = fdword 0 918 wr = fdword 4 919 pid = fork 920 if lt pid 0 921 r = fclose rd 922 r = fclose wr 923 ret 924 end 925 if eq pid 0 926 childx wr 927 end 928 r = fclose wr 929 n = drain slot rd base 930 put cl slot n 931 r = fclose rd 932 reap slot pid 933 end 934 fn childx wr int to void 935 r = dupfd wr 1 0 936 fd = fopen nul 0 len nul 1 0 937 if ge fd 0 938 r = dupfd int fd 2 0 939 end 940 r = execprog xp 0 get xl 0 941 exit 127 942 end 943 note read until end of file or the 4 s deadline; return bytes kept, or 16384 + bytes kept 944 note when the deadline passed. The first 8192 bytes go to cb[base..); later bytes are read 945 note into sk and dropped, so a child that writes more never blocks on a full pipe. The loop 946 note count is capped below the compiler bound; reaching it counts as the deadline. 947 fn drain slot ulong rd int base ulong to ulong 948 dl = add monons `4000000000` 949 put dlg 0 dl 950 n = ulong 0 951 it = ulong 0 952 while lt it 999998 bound 999999 953 it = add it 1 954 left = sub dl monons 955 if le left 0 956 ret add n 16384 957 end 958 w = waitio rd 1 left 959 if le w 0 960 ret add n 16384 961 end 962 if lt n 8192 963 k = read rd cb add base n sub 8192 n 964 if le k 0 965 ret n 966 end 967 n = add n ulong k 968 else 969 k = read rd sk 0 len sk 970 if le k 0 971 ret n 972 end 973 put tr slot 1 974 end 975 end 976 ret add n 16384 977 end 978 note after end of file the child may still run (it closed stdout and lingers): poll 979 note waitpid without blocking until the drain deadline, then kill it like a timeout. 980 fn reap slot ulong pid long to void 981 n = get cl slot 982 tmo = ge n 16384 983 if tmo 984 put cl slot sub n 16384 985 else 986 w = long 0 987 while eq w 0 bound 999999 988 w = waitpid int pid wst 0 4 1 989 if eq w 0 990 if le sub get dlg 0 monons 0 991 tmo = true 992 w = long 1 993 else 994 r = sleepns `1000000` 995 end 996 end 997 end 998 end 999 if tmo 1000 r = killpid int pid 9 1001 r = waitpid int pid wst 0 4 0 1002 put cs slot -1000 1003 ret 1004 end 1005 sig = and get wst 0 127 1006 if ne sig 0 1007 put cs slot neg long sig 1008 else 1009 put cs slot long get wst 1 1010 end 1011 end 1012 note error text: "timeout", "rc=N" or "truncated", with ",truncated" added when both apply 1013 fn oerr slot ulong to void 1014 oc uchar 34 1015 s = get cs slot 1016 if eq s -1000 1017 ot rtimeo 1018 else 1019 if ne s 0 1020 ot rrc 1021 if lt s 0 1022 oc uchar 45 1023 ou ulong neg s 1024 else 1025 ou ulong s 1026 end 1027 end 1028 end 1029 if ne get tr slot 0 1030 if ne s 0 1031 ot rcomma 1032 end 1033 ot rtrunc 1034 end 1035 oc uchar 34 1036 end 1037 note the command exited 0 and its whole output fitted the buffer 1038 fn cmdok slot ulong to bool 1039 if ne get cs slot 0 1040 ret false 1041 end 1042 ret eq get tr slot 0 1043 end 1044 1045 note ---- nvidia-smi fields ---- 1046 fn cbfind s ulong e ulong c uchar to ulong 1047 i = s 1048 while lt i e bound 8192 1049 if eq get cb i c 1050 ret i 1051 end 1052 i = add i 1 1053 end 1054 ret i 1055 end 1056 fn triml a ulong j ulong to ulong 1057 i = a 1058 while lt i j bound 8192 1059 if ne get cb i 32 1060 ret i 1061 end 1062 i = add i 1 1063 end 1064 ret i 1065 end 1066 fn trimr a ulong b ulong to ulong 1067 i = b 1068 while gt i a bound 8192 1069 if ne get cb sub i 1 32 1070 ret i 1071 end 1072 i = sub i 1 1073 end 1074 ret i 1075 end 1076 fn splitgpu to void 1077 e = cbfind 0 get cl 0 10 1078 k = ulong 0 1079 s = ulong 0 1080 go = true 1081 while go bound 16 1082 j = cbfind s e 44 1083 a = triml s j 1084 put fs k a 1085 put fe k trimr a j 1086 k = add k 1 1087 s = add j 1 1088 go = and lt j e lt k 16 1089 end 1090 put nf 0 k 1091 end 1092 fn ogpu1 j ulong t view uchar * to void 1093 if ge j get nf 0 1094 ret 1095 end 1096 osep 1097 ot t 1098 a = get fs j 1099 b = get fe j 1100 if pnumc a b 1101 odec get nm 0 get nd 0 1102 else 1103 ostrg a b 1104 end 1105 end 1106 fn ogpu to void 1107 if not cmdok 0 1108 osep 1109 ot kgerr 1110 oerr 0 1111 ret 1112 end 1113 splitgpu 1114 ogpu1 1 kg1 1115 ogpu1 2 kg2 1116 ogpu1 5 kg5 1117 ogpu1 4 kg4 1118 ogpu1 3 kg3 1119 ogpu1 0 kg0 1120 end 1121 1122 note ---- upsc fields: "key: value" lines, exact key match like the awk ---- 1123 fn splitups to void 1124 for j ulong 5 1125 put uf j 0 1126 end 1127 n = add 8192 get cl 1 1128 s = ulong 8192 1129 while lt s n bound 8192 1130 e = s 1131 while lt e n bound 8192 1132 if eq get cb e 10 1133 break 1134 end 1135 e = add e 1 1136 end 1137 k = s 1138 while lt add k 1 e bound 8192 1139 if and eq get cb k 58 eq get cb add k 1 32 1140 break 1141 end 1142 k = add k 1 1143 end 1144 if lt add k 1 e 1145 upskey s k add k 2 e 1146 end 1147 s = add e 1 1148 end 1149 end 1150 fn upskey s ulong k ulong v ulong e ulong to void 1151 slot = ulong 99 1152 if eqc s k wuload 1153 slot = 0 1154 end 1155 if eqc s k wunom 1156 slot = 1 1157 end 1158 if eqc s k wustat 1159 slot = 2 1160 end 1161 if eqc s k wvolt 1162 slot = 3 1163 end 1164 if eqc s k wbatt 1165 slot = 4 1166 end 1167 if lt slot 5 1168 put us slot v 1169 put ue slot e 1170 put uf slot 1 1171 end 1172 end 1173 note awk "$2+0": a missing or non-numeric field is 0 1174 fn oupsn j ulong t view uchar * to void 1175 osep 1176 ot t 1177 if eq get uf j 0 1178 oc uchar 48 1179 ret 1180 end 1181 if pnumc get us j get ue j 1182 oawk get nm 0 get nd 0 1183 else 1184 oc uchar 48 1185 end 1186 end 1187 fn oupsst to void 1188 osep 1189 ot kustat 1190 if eq get uf 2 0 1191 oc uchar 34 1192 oc uchar 34 1193 ret 1194 end 1195 ostrg get us 2 get ue 2 1196 end 1197 fn upsok to bool 1198 ret cmdok 1 1199 end 1200 1201 note ---- time ---- 1202 note civil date from days since 1970-01-01 (proleptic Gregorian), printed YYYY-MM-DD 1203 fn odate days ulong to void 1204 z = add days 719468 1205 era = div z 146097 1206 doe = sub z mul era 146097 1207 yoe = div sub add sub doe div doe 1460 div doe 36524 div doe 146096 365 1208 doy = sub doe sub add mul 365 yoe div yoe 4 div yoe 100 1209 mo = div add mul 5 doy 2 153 1210 d = add sub doy div add mul 153 mo 2 5 1 1211 m = ulong 0 1212 if lt mo 10 1213 m = add mo 3 1214 else 1215 m = sub mo 9 1216 end 1217 y = add yoe mul era 400 1218 if le m 2 1219 y = add y 1 1220 end 1221 ofix y 4 1222 oc uchar 45 1223 ofix m 2 1224 oc uchar 45 1225 ofix d 2 1226 end 1227 fn outc ns long to void 1228 s = div ulong ns `1000000000` 1229 osep 1230 ot kutc 1231 oc uchar 34 1232 odate div s 86400 1233 oc uchar 84 1234 r = rem s 86400 1235 ofix div r 3600 2 1236 oc uchar 58 1237 ofix div rem r 3600 60 2 1238 oc uchar 58 1239 ofix rem r 60 2 1240 oc uchar 90 1241 oc uchar 34 1242 end 1243 1244 note ---- one row ---- 1245 fn row seq ulong t0 long ns long to void 1246 put odst 0 get ofd 0 1247 put op 0 0 1248 oc uchar 123 1249 if upsok 1250 oupsn 4 kbatt 1251 end 1252 ocores 1253 ogpu 1254 if upsok 1255 oupsn 3 kvolt 1256 end 1257 oload 1258 omem 1259 opsi 0 kpc 1260 opsi 2 kpi 1261 opsi 1 kpm 1262 orun 1263 osep 1264 ot kcost 1265 ou div ulong sub monons t0 1000 1266 osep 1267 ot kseq 1268 ou seq 1269 othm 1270 osep 1271 ot kunix 1272 odec div add ns 500000 `1000000` 3 1273 outc ns 1274 if upsok 1275 oupsn 0 kuload 1276 oupsn 1 kunom 1277 oupsst 1278 else 1279 osep 1280 ot kuerr 1281 oerr 1 1282 end 1283 oc uchar 125 1284 oc uchar 10 1285 end 1286 1287 note ---- arguments and main loop ---- 1288 fn argnum i long dflt long to long 1289 if ge i argcnt 1290 ret dflt 1291 end 1292 n = argread ulong i ab 0 len ab 1293 if le n 0 1294 ret long -1 1295 end 1296 note at most 12 digits, so the decimal value below cannot overflow long (trap 1) 1297 if gt n 12 1298 ret long -1 1299 end 1300 v = long 0 1301 for j ulong ulong n 1302 c = get ab j 1303 if not isdig c 1304 ret long -1 1305 end 1306 v = add mul v 10 long sub c 48 1307 end 1308 ret v 1309 end 1310 fn argpath i long which ulong to void 1311 n = ulong 0 1312 if lt i argcnt 1313 k = argread ulong i ab 0 len ab 1314 if gt k 0 1315 n = ulong k 1316 end 1317 end 1318 if eq n 0 1319 if eq which 0 1320 for j ulong len dnv 1321 put ab j get dnv j 1322 end 1323 n = len dnv 1324 else 1325 for j ulong len dups 1326 put ab j get dups j 1327 end 1328 n = len dups 1329 end 1330 end 1331 for j ulong n 1332 if eq which 0 1333 put gpth j get ab j 1334 else 1335 put upth j get ab j 1336 end 1337 end 1338 if eq which 0 1339 put gpl 0 n 1340 else 1341 put upl 0 n 1342 end 1343 end 1344 fn openout i long flags int to long 1345 n = argread ulong i ab 0 len ab 1346 if le n 0 1347 ret long -1 1348 end 1349 ret fopen ab 0 ulong n flags 420 1350 end 1351 fn sample seq ulong to void 1352 t0 = monons 1353 put rseq 0 seq 1354 readstat 1355 ns = realns 1356 readload 1357 readpsi 0 ppc 1358 readpsi 1 ppm 1359 readpsi 2 ppi 1360 readtz 1361 readmem 1362 pkgpu 1363 note never two nvidia-smi launches closer than interval_s, whatever the /proc reads above cost: 1364 note wait here until one interval has passed since the previous launch, then record this one. 1365 g = get lgp 0 1366 if gt g 0 1367 w = sub add g get ivg 0 monons 1368 if gt w 0 1369 r = sleepns w 1370 end 1371 end 1372 put lgp 0 monons 1373 runcmd 0 0 1374 rawlog tgpu get cl 0 1 1375 pkups 1376 runcmd 1 8192 1377 rawlog tups get cl 1 2 1378 splitups 1379 row seq t0 ns 1380 r = write int get ofd 0 ob 0 get op 0 1381 wchk r get op 0 1382 savestat 1383 end 1384 fn main to int 1385 na = argcnt 1386 ival = argnum 2 5 1387 cnt = argnum 3 360 1388 note interval 5..86400 s (nvidia-smi read rate rule), count 0..999999 (the largest 1389 note while bound this omc accepts; bound `1000000` is refused with error 8) 1390 if or or lt na 2 or lt ival 5 gt ival 86400 or lt cnt 0 gt cnt 999999 1391 k = write 2 usage 0 len usage 1392 ret 2 1393 end 1394 fd = openout 1 525377 1395 if lt fd 0 1396 k = write 2 eopen 0 len eopen 1397 ret 2 1398 end 1399 put ofd 0 fd 1400 put rfd 0 -1 1401 if gt na 6 1402 rf = openout 6 525377 1403 if lt rf 0 1404 k = write 2 eopen 0 len eopen 1405 ret 2 1406 end 1407 put rfd 0 rf 1408 end 1409 argpath 4 0 1410 argpath 5 1 1411 put rseq 0 0 1412 readstat 1413 savestat 1414 iv = mul ival `1000000000` 1415 put ivg 0 iv 1416 nx = add monons iv 1417 seq = ulong 0 1418 note The next sample starts one interval after this one started, never sooner. A slow sample 1419 note (children at their 4 s deadline) is not followed by catch-up samples, so two nvidia-smi 1420 note reads are always at least interval_s apart. A fixed grid would fire late samples back to 1421 note back (measured 2026-11-09: gaps of 0.101 s and 0.131 s after a 10.5 s sample). 1422 while lt seq ulong cnt bound 999999 1423 left = sub nx monons 1424 if gt left 0 1425 r = sleepns left 1426 end 1427 nx = add monons iv 1428 sample seq 1429 seq = add seq 1 1430 end 1431 ret 0 1432 end