interplane.Get Omega
Read in your language

CUDApp

CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.

Brought by drakestapleton

branch main Branches Tags History

CUDApp / native / smi / smi.om

1275 lines · 21191 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2note smi.om: cudapp-smi, a read-only GPU status reporter (the CUDA++ successor of nvidia-smi).
3note Data sources, field table and gaps: native/smi/README.md.
4note
5note Build: this unit is OM-2. The compiled program is this file, the NVRM context library, the
6note large-control unit and the RM query unit, fed to omc in this order (omc has no forward global
7note references). native/smi/util.om needs the aien-dev/omega-mv#1048 candidate omc (devcall bound):
8note cat native/smi/smi.om native/nvrm/context.om native/smi/util.om native/smi/rm.om | omc > cudapp-smi
9note Usage: cudapp-smi [--root DIR] [--rm]
10note --root DIR read DIR/proc and DIR/sys instead of /proc and /sys (fixture trees)
11note --rm also query the resource manager through /dev/nvidiactl and /dev/nvidia0 (one GPU only);
12note the device nodes are always the real ones, whatever --root says
13note Without --rm the program opens only files under ROOT/proc and ROOT/sys, read-only, and reads
14note symbolic links under ROOT/proc/<pid>/fd. It never opens a device and never writes a file.
15note
16note Report (stdout, key=value lines, only after every check passed; keys follow nvidia-smi --query-gpu):
17note source=procfs+sysfs[+rm] driver_version=<v> count=<n>
18note per GPU g (sorted by PCI address): gpu.g.pci.bus_id .pci.domain .pci.bus .pci.device .uuid
19note .minor_number .vbios_version .gsp_firmware_version .pci.device_id .pci.sub_device_id
20note .pci.base_classcode .pci.sub_classcode .pcie.link.gen.current .pcie.link.gen.max
21note .pcie.link.width.current .pcie.link.width.max, and without --rm .name .temperature.gpu .pstate
22note .utilization.gpu .utilization.memory as [N/A]
23note then per GPU: gpu.g.holder.k.pid .holder.k.process_name, gpu.g.holders=<n>
24note then proc.scanned=<n> proc.uninspectable=<n>, then with --rm the rm.om keys.
25note [N/A] marks a field whose source file is absent or which this source cannot provide.
26note
27note Exit codes (a refusal writes one line "SMI REFUSE <reason>" to stderr and nothing to stdout):
28note 0 report written
29note 2 usage
30note 3 I/O: a present file or directory could not be read, or stdout write failed
31note 4 no NVIDIA driver: ROOT/proc/driver/nvidia/version is absent
32note 10 malformed version file: no line starting "NVRM version:" or no dotted numeric version token
33note 11 information file lacks GPU UUID, Bus Location or Device Minor
34note 12 malformed GPU entry: directory name not dddd:bb:dd.f, Bus Location differs from the directory
35note name, malformed GPU UUID, Device Minor not 0..255, Video BIOS or GPU Firmware malformed
36note 13 PCI vendor is not 0x10de
37note 14 malformed sysfs number (hex must be 0x + 1..8 lowercase digits; decimal 1..9 digits)
38note 15 unknown PCIe link speed
39note 19 capacity: path, file, GPU count (8), holder count (4096) or report (65536 bytes) exceeded
40note 30..34 resource-manager refusals, see rm.om
41note Runtime traps exit 100 + trap code (Omegese 9.1); none is expected on any input.
42om2
43
44text trf "SMI REFUSE "
45text tnl "\n"
46text tuse "usage: cudapp-smi [--root DIR] [--rm]"
47text tio "I/O error"
48text tnodrv "NVIDIA driver not loaded (no proc/driver/nvidia/version)"
49text tbver "malformed driver version file"
50text tmkey "information file lacks a required key"
51text tbgpu "malformed GPU entry"
52text tvend "PCI vendor is not NVIDIA"
53text tbnum "malformed sysfs number"
54text tbspd "unknown PCIe link speed"
55text tcap "capacity exceeded"
56text targr "--root"
57text targm "--rm"
58text tver "/proc/driver/nvidia/version"
59text tgpd "/proc/driver/nvidia/gpus"
60text tinfo "/information"
61text tsysd "/sys/bus/pci/devices/"
62text tproc0 "/proc"
63text tproc "/proc/"
64text tfd "/fd"
65text tcmd "/cmdline"
66text tcomm "/comm"
67text tdevn "/dev/nvidia"
68text tgpup "GPU-"
69text tnvrm "NVRM version:"
70text kuuid "GPU UUID:"
71text kbusl "Bus Location:"
72text kminor "Device Minor:"
73text kvbios "Video BIOS:"
74text kgspfw "GPU Firmware:"
75text fvend "/vendor"
76text fdevi "/device"
77text fsvnd "/subsystem_vendor"
78text fsdev "/subsystem_device"
79text fclass "/class"
80text fcls "/current_link_speed"
81text fclw "/current_link_width"
82text fmls "/max_link_speed"
83text fmlw "/max_link_width"
84text sunk "Unknown"
85text sg1 "2.5 GT/s"
86text sg2 "5.0 GT/s"
87text sg3 "8.0 GT/s"
88text sg4 "16.0 GT/s"
89text sg5 "32.0 GT/s"
90text sg6 "64.0 GT/s"
91text tna "[N/A]"
92text tsrc "source=procfs+sysfs"
93text tsrcrm "+rm"
94text kdrv "driver_version="
95text kcount "count="
96text kgpu "gpu."
97text kbusid "pci.bus_id="
98text kdom "pci.domain=0x"
99text kbus "pci.bus=0x"
100text kdevice "pci.device=0x"
101text kuuido "uuid="
102text kmino "minor_number="
103text kvbo "vbios_version="
104text kgspo "gsp_firmware_version="
105text kdevid "pci.device_id="
106text ksubid "pci.sub_device_id="
107text kbcls "pci.base_classcode="
108text kscls "pci.sub_classcode="
109text kgenc "pcie.link.gen.current="
110text kgenm "pcie.link.gen.max="
111text kwidc "pcie.link.width.current="
112text kwidm "pcie.link.width.max="
113text kname "name="
114text ktemp "temperature.gpu="
115text kpst "pstate="
116text kutg "utilization.gpu="
117text kutm "utilization.memory="
118text khold "holder."
119text kpid ".pid="
120text kpname ".process_name="
121text kholds "holders="
122text kscan "proc.scanned="
123text kunins "proc.uninspectable="
124
125global gob uchar 65536
126global gpath uchar 4096
127global groot uchar 2048
128global gfile uchar 16384
129note Directory buffers carry 256 bytes of slack past the getdnts capacity, so record fields read
130note at a record start below the returned length stay in bounds.
131global gdir uchar 33024
132global gdir2 uchar 16640
133global glnk uchar 64
134global ggpu uchar 128
135global gmin ulong 8
136global gseen uchar 8
137global gpg uchar 4096
138global gpp ulong 4096
139note gst: 0 output fill, 1 root length, 2 path length, 3 file length, 4 value offset, 5 value length,
140note 6 GPU count, 7 holder pair count, 8 uninspectable processes, 9 --rm given, 10 scanned processes,
141note 11 proc directory readable, 12 resource-manager refusal code (rm.om)
142global gst ulong 16
143
144fn refuse code int msg view uchar * to void
145n = write 2 trf 0 len trf
146n = write 2 msg 0 len msg
147n = write 2 tnl 0 len tnl
148exit code
149end
150
151note The report is held whole and written once, so a refusal leaves stdout empty.
152fn oflush to void
153n = get gst 0
154if gt n 0
155w = write 1 gob 0 n
156if ne w long n
157refuse 3 tio
158end
159put gst 0 0
160end
161end
162
163fn oc b uchar to void
164if ge get gst 0 len gob
165refuse 19 tcap
166end
167put gob get gst 0 b
168put gst 0 add get gst 0 1
169end
170
171fn ot t view uchar * to void
172for i ulong len t
173oc get t i
174end
175end
176
177fn onl to void
178oc 10
179end
180
181fn ona to void
182ot tna
183onl
184end
185
186fn odec v ulong to void
187d = new uchar 20 0
188n = ulong 0
189x = v
190while lt n 20 bound 21
191put d n uchar add rem x 10 48
192n = add n 1
193x = div x 10
194if eq x 0
195break
196end
197end
198while gt n 0 bound 21
199n = sub n 1
200oc get d n
201end
202end
203
204fn ohu n ulong to uchar
205if lt n 10
206ret uchar add n 48
207end
208ret uchar add n 55
209end
210
211note Uppercase hex without prefix, at least w digits (w 0: no leading zeros, at least one digit).
212fn ohxw v ulong w ulong to void
213on = false
214for i ulong 16
215nb = and shr v sub 60 mul i 4 15
216if or or ne nb 0 on or ge add i w 16 eq i 15
217oc ohu nb
218on = true
219end
220end
221end
222
223fn og g ulong t view uchar * to void
224ot kgpu
225odec g
226oc 46
227ot t
228end
229
230note Optional decimal value: negative means the source file was absent.
231fn odopt v long to void
232if lt v 0
233ona
234ret
235end
236odec ulong v
237onl
238end
239
240fn ofile o ulong n ulong to void
241for i ulong n
242oc get gfile add o i
243end
244end
245
246fn ofileu o ulong n ulong to void
247for i ulong n
248c = get gfile add o i
249if and ge c 97 le c 122
250c = sub c 32
251end
252oc c
253end
254end
255
256fn ogpu g ulong o ulong n ulong to void
257for i ulong n
258c = get ggpu add mul g 16 add o i
259if and ge c 97 le c 122
260c = sub c 32
261end
262oc c
263end
264end
265
266fn pclr to void
267n = get gst 1
268for i ulong n
269put gpath i get groot i
270end
271put gst 2 n
272end
273
274fn pc b uchar to void
275n = get gst 2
276if ge n sub len gpath 1
277refuse 19 tcap
278end
279put gpath n b
280put gst 2 add n 1
281end
282
283fn pt t view uchar * to void
284for i ulong len t
285pc get t i
286end
287end
288
289fn pdec v ulong to void
290d = new uchar 20 0
291n = ulong 0
292x = v
293while lt n 20 bound 21
294put d n uchar add rem x 10 48
295n = add n 1
296x = div x 10
297if eq x 0
298break
299end
300end
301while gt n 0 bound 21
302n = sub n 1
303pc get d n
304end
305end
306
307fn pgpu g ulong to void
308for i ulong 12
309pc get ggpu add mul g 16 i
310end
311end
312
313fn pdir2 o ulong n ulong to void
314for i ulong n
315pc get gdir2 add o i
316end
317end
318
319note Reads the file named by gpath into gfile; returns its length or minus errno. At most
320note len gfile - 1 bytes are read, so get gfile at the returned length stays in bounds.
321fn rdf to long
322fd = fopen gpath 0 get gst 2 0 0
323if lt fd 0
324put gst 3 0
325ret fd
326end
327t = ulong 0
328cap = sub len gfile 1
329while lt t cap bound 100000
330r = read int fd gfile t sub cap t
331if lt r 0
332c = fclose int fd
333put gst 3 0
334ret r
335end
336if eq r 0
337break
338end
339t = add t ulong r
340end
341c = fclose int fd
342put gst 3 t
343ret long t
344end
345
346note Like rdf, but a file that fills gfile is refused rather than read truncated.
347fn rdopt to long
348r = rdf
349if ge r long sub len gfile 1
350refuse 19 tcap
351end
352ret r
353end
354
355fn isws c uchar to bool
356ret or or eq c 32 eq c 9 or eq c 10 eq c 13
357end
358
359fn ishx c uchar to bool
360ret or and ge c 48 le c 57 and ge c 97 le c 102
361end
362
363fn hxv c uchar to ulong
364if le c 57
365ret ulong sub c 48
366end
367ret ulong sub c 87
368end
369
370fn lnpre o ulong t view uchar * to bool
371n = get gst 3
372if gt len t sub n o
373ret false
374end
375for k ulong len t
376if ne get gfile add o k get t k
377ret false
378end
379end
380ret true
381end
382
383note Finds the first line of gfile starting with t; sets gst 4 to the byte after t.
384fn findk t view uchar * to bool
385n = get gst 3
386o = ulong 0
387while lt o n bound 100000
388if lnpre o t
389put gst 4 add o len t
390ret true
391end
392while and lt o n ne get gfile o 10 bound 100000
393o = add o 1
394end
395o = add o 1
396end
397ret false
398end
399
400note From gst 4, skips blanks; sets gst 4 and gst 5 to the value up to the line end, trailing blanks trimmed.
401fn val to void
402n = get gst 3
403o = get gst 4
404while and lt o n or eq get gfile o 32 eq get gfile o 9 bound 100000
405o = add o 1
406end
407e = o
408while and lt e n ne get gfile e 10 bound 100000
409e = add e 1
410end
411while gt e o bound 100000
412c = get gfile sub e 1
413if not or or eq c 32 eq c 9 eq c 13
414break
415end
416e = sub e 1
417end
418put gst 4 o
419put gst 5 sub e o
420end
421
422fn isver o ulong n ulong to bool
423if or eq n 0 gt n 32
424ret false
425end
426dots = ulong 0
427prev = uchar 46
428for i ulong n
429c = get gfile add o i
430if eq c 46
431if eq prev 46
432ret false
433end
434dots = add dots 1
435else
436if or lt c 48 gt c 57
437ret false
438end
439end
440prev = c
441end
442ret and gt dots 0 ne prev 46
443end
444
445fn pver to void
446pclr
447pt tver
448r = rdopt
449if lt r 0
450if eq r neg long 2
451refuse 4 tnodrv
452end
453refuse 3 tio
454end
455if not findk tnvrm
456refuse 10 tbver
457end
458n = get gst 3
459o = get gst 4
460found = false
461while and lt o n ne get gfile o 10 bound 100000
462if isws get gfile o
463o = add o 1
464continue
465end
466e = o
467while and lt e n not isws get gfile e bound 100000
468e = add e 1
469end
470if isver o sub e o
471put gst 4 o
472put gst 5 sub e o
473found = true
474break
475end
476o = e
477end
478if not found
479refuse 10 tbver
480end
481ot kdrv
482ofile get gst 4 get gst 5
483onl
484end
485
486note Directory entry helpers over getdents64 records: d_reclen u16 at 16, d_name at 19.
487fn dlen o ulong to ulong
488ret or ulong get gdir add o 16 shl ulong get gdir add o 17 8
489end
490
491fn dlen2 o ulong to ulong
492ret or ulong get gdir2 add o 16 shl ulong get gdir2 add o 17 8
493end
494
495fn dnam o ulong e ulong to ulong
496n = ulong 0
497while and lt add o n e ne get gdir add o n 0 bound 300
498n = add n 1
499end
500ret n
501end
502
503fn dnam2 o ulong e ulong to ulong
504n = ulong 0
505while and lt add o n e ne get gdir2 add o n 0 bound 300
506n = add n 1
507end
508ret n
509end
510
511fn isdot o ulong n ulong to bool
512if or eq n 0 gt n 2
513ret false
514end
515if ne get gdir o 46
516ret false
517end
518ret or eq n 1 eq get gdir add o 1 46
519end
520
521fn isbus o ulong n ulong to bool
522if ne n 12
523ret false
524end
525for i ulong 12
526c = get gdir add o i
527if or eq i 4 eq i 7
528if ne c 58
529ret false
530end
531continue
532end
533if eq i 10
534if ne c 46
535ret false
536end
537continue
538end
539if not ishx c
540ret false
541end
542end
543ret true
544end
545
546fn addgpu o ulong n ulong to void
547if not isbus o n
548refuse 12 tbgpu
549end
550g = get gst 6
551if ge g 8
552refuse 19 tcap
553end
554for i ulong 12
555put ggpu add mul g 16 i get gdir add o i
556end
557put gst 6 add g 1
558end
559
560fn gpult a ulong b ulong to bool
561for i ulong 12
562x = get ggpu add mul a 16 i
563y = get ggpu add mul b 16 i
564if ne x y
565ret lt x y
566end
567end
568ret false
569end
570
571fn gpusw a ulong b ulong to void
572for i ulong 16
573x = get ggpu add mul a 16 i
574put ggpu add mul a 16 i get ggpu add mul b 16 i
575put ggpu add mul b 16 i x
576end
577end
578
579fn lsgpu to void
580put gst 6 0
581pclr
582pt tgpd
583fd = fopen gpath 0 get gst 2 16384 0
584if lt fd 0
585if eq fd neg long 2
586ret
587end
588refuse 3 tio
589end
590while true bound 10000
591r = getdnts int fd gdir 0 32768
592if lt r 0
593refuse 3 tio
594end
595if eq r 0
596break
597end
598o = ulong 0
599while lt o ulong r bound 100000
600rl = dlen o
601if lt rl 20
602refuse 3 tio
603end
604e = add o rl
605nm = add o 19
606l = dnam nm e
607if not isdot nm l
608addgpu nm l
609end
610o = e
611end
612end
613c = fclose int fd
614n = get gst 6
615for i ulong n
616for j ulong sub sub n i 1
617if gpult add j 1 j
618gpusw j add j 1
619end
620end
621end
622end
623
624note Bus Location must equal the directory name exactly.
625fn chkbus g ulong to void
626if ne get gst 5 12
627refuse 12 tbgpu
628end
629o = get gst 4
630for i ulong 12
631if ne get gfile add o i get ggpu add mul g 16 i
632refuse 12 tbgpu
633end
634end
635end
636
637fn chkuuid to void
638o = get gst 4
639if ne get gst 5 40
640refuse 12 tbgpu
641end
642if not lnpre o tgpup
643refuse 12 tbgpu
644end
645for i ulong 36
646c = get gfile add o add 4 i
647if or or eq i 8 eq i 13 or eq i 18 eq i 23
648if ne c 45
649refuse 12 tbgpu
650end
651continue
652end
653if not ishx c
654refuse 12 tbgpu
655end
656end
657end
658
659fn pminor to ulong
660o = get gst 4
661n = get gst 5
662if or eq n 0 gt n 3
663refuse 12 tbgpu
664end
665v = ulong 0
666for i ulong n
667c = get gfile add o i
668if or lt c 48 gt c 57
669refuse 12 tbgpu
670end
671v = add mul v 10 ulong sub c 48
672end
673if gt v 255
674refuse 12 tbgpu
675end
676ret v
677end
678
679fn chkvb to void
680o = get gst 4
681n = get gst 5
682if or eq n 0 gt n 32
683refuse 12 tbgpu
684end
685for i ulong n
686c = get gfile add o i
687if not or ishx c eq c 46
688refuse 12 tbgpu
689end
690end
691end
692
693fn chkprn to void
694o = get gst 4
695n = get gst 5
696if or eq n 0 gt n 63
697refuse 12 tbgpu
698end
699for i ulong n
700c = get gfile add o i
701if or lt c 33 gt c 126
702refuse 12 tbgpu
703end
704end
705end
706
707note Information file fields, validated and written in report order.
708fn rinfo g ulong to void
709pclr
710pt tgpd
711pc 47
712pgpu g
713pt tinfo
714r = rdopt
715if lt r 0
716refuse 3 tio
717end
718if not findk kbusl
719refuse 11 tmkey
720end
721val
722chkbus g
723og g kbusid
724oc 48
725oc 48
726oc 48
727oc 48
728ogpu g 0 4
729oc 58
730ogpu g 5 2
731oc 58
732ogpu g 8 4
733onl
734og g kdom
735ogpu g 0 4
736onl
737og g kbus
738ogpu g 5 2
739onl
740og g kdevice
741ogpu g 8 2
742onl
743if not findk kuuid
744refuse 11 tmkey
745end
746val
747chkuuid
748og g kuuido
749ofile get gst 4 get gst 5
750onl
751if not findk kminor
752refuse 11 tmkey
753end
754val
755put gmin g pminor
756og g kmino
757odec get gmin g
758onl
759og g kvbo
760if findk kvbios
761val
762chkvb
763ofileu get gst 4 get gst 5
764onl
765else
766ona
767end
768og g kgspo
769if findk kgspfw
770val
771chkprn
772ofile get gst 4 get gst 5
773onl
774else
775ona
776end
777end
778
779fn psys g ulong t view uchar * to void
780pclr
781pt tsysd
782pgpu g
783pt t
784end
785
786note sysfs "0x" + 1..8 lowercase hex digits, optional newline; minus one when the file is absent.
787fn rdhex to long
788r = rdopt
789if lt r 0
790ret neg long 1
791end
792n = get gst 3
793if gt n 0
794if eq get gfile sub n 1 10
795n = sub n 1
796end
797end
798if or or lt n 3 gt n 10 or ne get gfile 0 48 ne get gfile 1 120
799refuse 14 tbnum
800end
801v = ulong 0
802for i ulong sub n 2
803c = get gfile add 2 i
804if not ishx c
805refuse 14 tbnum
806end
807v = or shl v 4 hxv c
808end
809ret long v
810end
811
812fn rddec to long
813r = rdopt
814if lt r 0
815ret neg long 1
816end
817n = get gst 3
818if gt n 0
819if eq get gfile sub n 1 10
820n = sub n 1
821end
822end
823if or eq n 0 gt n 9
824refuse 14 tbnum
825end
826v = ulong 0
827for i ulong n
828c = get gfile i
829if or lt c 48 gt c 57
830refuse 14 tbnum
831end
832v = add mul v 10 ulong sub c 48
833end
834ret long v
835end
836
837note PCIe generation from the sysfs link speed; minus one when absent or "Unknown".
838fn rdgen to long
839r = rdopt
840if lt r 0
841ret neg long 1
842end
843if lnpre 0 sunk
844ret neg long 1
845end
846if lnpre 0 sg1
847ret 1
848end
849if lnpre 0 sg2
850ret 2
851end
852if lnpre 0 sg3
853ret 3
854end
855if lnpre 0 sg4
856ret 4
857end
858if lnpre 0 sg5
859ret 5
860end
861if lnpre 0 sg6
862ret 6
863end
864refuse 15 tbspd
865ret 0
866end
867
868fn ohexid hi long lo long to void
869if or lt hi 0 lt lo 0
870ona
871ret
872end
873oc 48
874oc 120
875ohxw or shl ulong hi 16 ulong lo 8
876onl
877end
878
879fn rsys g ulong to void
880psys g fvend
881ven = rdhex
882if and ge ven 0 ne ven 0x10de
883refuse 13 tvend
884end
885psys g fdevi
886dev = rdhex
887psys g fsvnd
888sven = rdhex
889psys g fsdev
890sdev = rdhex
891psys g fclass
892cls = rdhex
893og g kdevid
894ohexid dev ven
895og g ksubid
896ohexid sdev sven
897og g kbcls
898if lt cls 0
899ona
900else
901oc 48
902oc 120
903ohxw shr ulong cls 16 0
904onl
905end
906og g kscls
907if lt cls 0
908ona
909else
910oc 48
911oc 120
912ohxw and shr ulong cls 8 255 0
913onl
914end
915end
916
917fn rlink g ulong to void
918psys g fcls
919v = rdgen
920og g kgenc
921odopt v
922psys g fmls
923v = rdgen
924og g kgenm
925odopt v
926psys g fclw
927v = rddec
928og g kwidc
929odopt v
930psys g fmlw
931v = rddec
932og g kwidm
933odopt v
934end
935
936note Exact match "/dev/nvidia<1..3 digits>"; returns the minor or minus one.
937fn lnkmin n long to long
938if or lt n 12 gt n 14
939ret neg long 1
940end
941for i ulong len tdevn
942if ne get glnk i get tdevn i
943ret neg long 1
944end
945end
946v = long 0
947for i ulong sub ulong n len tdevn
948c = get glnk add len tdevn i
949if or lt c 48 gt c 57
950ret neg long 1
951end
952v = add mul v 10 long sub c 48
953end
954ret v
955end
956
957fn addpair g ulong pid ulong to void
958k = get gst 7
959if ge k len gpp
960refuse 19 tcap
961end
962put gpg k uchar g
963put gpp k pid
964put gst 7 add k 1
965end
966
967fn fdmin pid ulong o ulong n ulong to long
968pclr
969pt tproc
970pdec pid
971pt tfd
972pc 47
973pdir2 o n
974r = readlnk gpath 0 get gst 2 glnk 0 len glnk
975if le r 0
976ret neg long 1
977end
978ret lnkmin r
979end
980
981fn chkfd pid ulong o ulong n ulong to void
982m = fdmin pid o n
983if lt m 0
984ret
985end
986mu = ulong m
987ng = get gst 6
988for g ulong ng
989if ne get gseen g 0
990continue
991end
992if eq get gmin g mu
993put gseen g 1
994addpair g pid
995end
996end
997end
998
999note One process: every /dev/nvidia<minor> link in ROOT/proc/<pid>/fd marks it as a holder of that GPU.
1000fn scanpid pid ulong to void
1001for i ulong len gseen
1002put gseen i 0
1003end
1004pclr
1005pt tproc
1006pdec pid
1007pt tfd
1008fd = fopen gpath 0 get gst 2 16384 0
1009if lt fd 0
1010put gst 8 add get gst 8 1
1011ret
1012end
1013while true bound 10000
1014r = getdnts int fd gdir2 0 16384
1015if le r 0
1016break
1017end
1018o = ulong 0
1019while lt o ulong r bound 100000
1020rl = dlen2 o
1021if lt rl 20
1022break
1023end
1024e = add o rl
1025nm = add o 19
1026l = dnam2 nm e
1027if and gt l 0 ne get gdir2 nm 46
1028chkfd pid nm l
1029end
1030o = e
1031end
1032end
1033c = fclose int fd
1034end
1035
1036fn piddec o ulong n ulong to long
1037if or eq n 0 gt n 10
1038ret neg long 1
1039end
1040v = ulong 0
1041for i ulong n
1042c = get gdir add o i
1043if or lt c 48 gt c 57
1044ret neg long 1
1045end
1046v = add mul v 10 ulong sub c 48
1047end
1048ret long v
1049end
1050
1051fn pairlt a ulong b ulong to bool
1052if ne get gpg a get gpg b
1053ret lt get gpg a get gpg b
1054end
1055ret lt get gpp a get gpp b
1056end
1057
1058fn pairsw a ulong b ulong to void
1059x = get gpg a
1060put gpg a get gpg b
1061put gpg b x
1062y = get gpp a
1063put gpp a get gpp b
1064put gpp b y
1065end
1066
1067fn scan to void
1068put gst 7 0
1069put gst 8 0
1070put gst 10 0
1071put gst 11 0
1072pclr
1073pt tproc0
1074fd = fopen gpath 0 get gst 2 16384 0
1075if lt fd 0
1076ret
1077end
1078put gst 11 1
1079while true bound 10000
1080r = getdnts int fd gdir 0 32768
1081if lt r 0
1082refuse 3 tio
1083end
1084if eq r 0
1085break
1086end
1087o = ulong 0
1088while lt o ulong r bound 100000
1089rl = dlen o
1090if lt rl 20
1091refuse 3 tio
1092end
1093e = add o rl
1094nm = add o 19
1095pid = piddec nm dnam nm e
1096if ge pid 0
1097put gst 10 add get gst 10 1
1098scanpid ulong pid
1099end
1100o = e
1101end
1102end
1103c = fclose int fd
1104n = get gst 7
1105for i ulong n
1106for j ulong sub sub n i 1
1107if pairlt add j 1 j
1108pairsw j add j 1
1109end
1110end
1111end
1112end
1113
1114note Process name: argv[0] from cmdline, else comm; bytes outside 32..126 print as '?'.
1115fn oname pid ulong to void
1116pclr
1117pt tproc
1118pdec pid
1119pt tcmd
1120r = rdf
1121n = ulong 0
1122while and lt n ulong r ne get gfile n 0 bound 20000
1123n = add n 1
1124end
1125if eq n 0
1126pclr
1127pt tproc
1128pdec pid
1129pt tcomm
1130r2 = rdf
1131if gt r2 0
1132n = ulong r2
1133if eq get gfile sub n 1 10
1134n = sub n 1
1135end
1136end
1137end
1138if eq n 0
1139ona
1140ret
1141end
1142if gt n 1024
1143n = 1024
1144end
1145for i ulong n
1146c = get gfile i
1147if or lt c 32 gt c 126
1148c = 63
1149end
1150oc c
1151end
1152onl
1153end
1154
1155fn oholder g ulong to void
1156if eq get gst 11 0
1157og g kholds
1158ona
1159ret
1160end
1161k = ulong 0
1162for i ulong get gst 7
1163if eq ulong get gpg i g
1164p = get gpp i
1165og g khold
1166odec k
1167ot kpid
1168odec p
1169onl
1170og g khold
1171odec k
1172ot kpname
1173oname p
1174k = add k 1
1175end
1176end
1177og g kholds
1178odec k
1179onl
1180end
1181
1182note The whole /proc and /sys report; rm.om main calls it before any resource-manager query.
1183fn report to void
1184ot tsrc
1185if ne get gst 9 0
1186ot tsrcrm
1187end
1188onl
1189pver
1190lsgpu
1191ot kcount
1192odec get gst 6
1193onl
1194for g ulong get gst 6
1195rinfo g
1196rsys g
1197rlink g
1198if eq get gst 9 0
1199og g kname
1200ona
1201og g ktemp
1202ona
1203og g kpst
1204ona
1205og g kutg
1206ona
1207og g kutm
1208ona
1209end
1210end
1211scan
1212for g ulong get gst 6
1213oholder g
1214end
1215ot kscan
1216if eq get gst 11 0
1217ona
1218else
1219odec get gst 10
1220onl
1221end
1222ot kunins
1223if eq get gst 11 0
1224ona
1225else
1226odec get gst 8
1227onl
1228end
1229end
1230
1231fn argis n long t view uchar * to bool
1232if ne ulong n len t
1233ret false
1234end
1235for i ulong len t
1236if ne get gpath i get t i
1237ret false
1238end
1239end
1240ret true
1241end
1242
1243note Parses the command line into gst 1 (root length, groot) and gst 9 (--rm).
1244fn args to void
1245ac = argcnt
1246i = long 1
1247root = false
1248while lt i ac bound 16
1249n = argread ulong i gpath 0 len gpath
1250if le n 0
1251refuse 2 tuse
1252end
1253if argis n targm
1254if ne get gst 9 0
1255refuse 2 tuse
1256end
1257put gst 9 1
1258i = add i 1
1259continue
1260end
1261if not argis n targr
1262refuse 2 tuse
1263end
1264if or root ge add i 1 ac
1265refuse 2 tuse
1266end
1267root = true
1268m = argread ulong add i 1 groot 0 len groot
1269if or le m 0 ge m long len groot
1270refuse 2 tuse
1271end
1272put gst 1 ulong m
1273i = add i 2
1274end
1275end