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