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/CUDApp/blob/main/tests/smi/run.sh # 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 Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `CUDApp / tests / smi / run.sh` 308 lines · 14978 bytes Raw Blame History 1 #!/bin/sh 2 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 3 # cudapp-smi host tier: fixture parsing, refusals, the --rm path against a host fake, the devcall 4 # bound probe, and mutants. 5 # Device-free: the shipped binary only ever reads a fixture root; --rm runs only in the fake build, 6 # whose OS effects are rewritten out of private copies of native/nvrm/context.om and native/smi/util.om. 7 # Usage: tests/smi/run.sh [OMC [BASE_OMC]] (default $CUDAPP_SMI_OMC) 8 # OMC is the aien-dev/omega-mv#1048 dedicated compiler (landed via #1050; devcall bound `1048576`): util.om issues a 9 # 55888-byte control. BASE_OMC, when given, is the omega-mv `e5b32567` pin; the bound probe must trap 10 # under it (the negative control). 11 set -u 12 if [ "$(uname -s)" != Linux ] || [ "$(uname -m)" != aarch64 ]; then 13 echo 'NOT_RUN: cudapp-smi host tier needs Linux AArch64 (the pinned omc emits AArch64 Linux code)' >&2 14 exit 77 15 fi 16 omc=${1:-${CUDAPP_SMI_OMC:-}} 17 if [ -z "$omc" ] || [ ! -x "$omc" ]; then 18 echo 'NOT_RUN: set CUDAPP_SMI_OMC to the dedicated aien-dev/omega-mv#1048 omc (landed via #1050)' >&2 19 exit 77 20 fi 21 pin=`390629fd22590aaefca852045f8b0ace86c62cefb3b1bc203ce400773c8abc2f` 22 got=$(sha256sum "$omc") || exit 74 23 if [ "${got%% *}" != "$pin" ]; then 24 echo "REFUSED: omc digest ${got%% *} is not the dedicated omega-mv#1048 pin (`e9b63587`)" >&2 25 exit 69 26 fi 27 base=${2:-} 28 basepin=`2c82480596bbdc50cda9626866209ef662d5834f278f839ff4ce0250c495029f` 29 if [ -n "$base" ]; then 30 got=$(sha256sum "$base") || exit 74 31 if [ "${got%% *}" != "$basepin" ]; then 32 echo "REFUSED: base omc digest ${got%% *} is not the omega-mv `e5b32567` pin" >&2 33 exit 69 34 fi 35 fi 36 root=$(CDPATH= cd -- "$(dirname -- "$0")/../.." && pwd) || exit 74 37 t=$root/tests/smi 38 out=$(mktemp -d "${TMPDIR:-/tmp}/cudapp-smi.XXXXXX") || exit 74 39 trap 'rm -rf "$out"' EXIT 40 fails=0 41 fail() { echo "FAIL: $*" >&2; fails=$((fails + 1)); } 42 43 # Rewrites OS effects in context.om or util.om into the rmfake.om words; the request number is kept. 44 fakectx() { 45 awk ' 46 $1 == "note" { print; next } 47 $2 == "=" && ($3 == "devcall" || $3 == "devctl" || $3 == "uvmctl") { 48 q = ""; for (i = 4; i < NF; i++) if ($i == "uint") { q = $(i + 1); break } 49 if (q == "") { print "REWRITE_FAILED"; next } 50 print $1 " = smdrv " q; next } 51 $2 == "=" && $3 == "fopen" { print $1 " = smfopen"; next } 52 $2 == "=" && ($3 == "mapdrop" || $3 == "mapnew" || $3 == "mapset" || $3 == "mapread" || $3 == "mapwrite" || $3 == "maphold" || $3 == "mapbind") { print $1 " = smbad"; next } 53 { s = "" 54 for (i = 1; i <= NF; i++) { 55 w = $i 56 if (w == "fclose") w = "smclose" 57 if (w == "mapget") w = "smget" 58 if (w == "mapaddr") w = "smaddr" 59 s = (s == "" ? w : s " " w) } 60 print s } 61 ' "$1" 62 } 63 64 # Device words: none may survive in the fake context; rm.om and smi.om may never hold any; util.om 65 # holds exactly one (its devcall). 66 dev='devcall|devctl|uvmctl|mapnew|mapdrop|mapget|mapset|mapread|mapwrite|maphold|mapbind|mapaddr' 67 guard() { 68 awk -v re="^($dev$2)\$" '$1 != "note" { for (i = 1; i <= NF; i++) if ($i ~ re || $i == "REWRITE_FAILED") { print FILENAME ":" NR ": " $i; bad = 1 } } END { exit bad }' "$1" 69 } 70 71 build() { # smi.om rm.om out [fake] 72 if [ "${4:-}" = fake ]; then 73 fakectx "$root/native/nvrm/context.om" > "$out/ctx-fake.om" || return 1 74 fakectx "$root/native/smi/util.om" > "$out/util-fake.om" || return 1 75 guard "$out/ctx-fake.om" '|fopen|fclose' || { echo 'REFUSED: unbound OS effect in fake context' >&2; exit 69; } 76 guard "$out/util-fake.om" '|fopen|fclose' || { echo 'REFUSED: unbound OS effect in fake util' >&2; exit 69; } 77 cat "$1" "$out/ctx-fake.om" "$out/util-fake.om" "$t/rmfake.om" "$2" > "$3.om" 78 else 79 cat "$1" "$root/native/nvrm/context.om" "$root/native/smi/util.om" "$2" > "$3.om" 80 fi 81 "$omc" < "$3.om" > "$3" 2> "$3.log" && [ -s "$3" ] && chmod 700 "$3" 82 } 83 84 # Fixture root: recorded GB10 files plus a synthetic /proc with one holder, a duplicate fd, 85 # a control-node-only fd and a non-GPU process. 86 mkroot() { 87 rm -rf "$1" 88 cp -R "$t/fixtures/gb10" "$1" && chmod -R u+w "$1" || exit 74 89 mkdir -p "$1/proc/4242/fd" "$1/proc/77/fd" "$1/proc/self" || exit 74 90 printf '/opt/x/server\000--port\0008\000' > "$1/proc/4242/cmdline" 91 ln -s /dev/nvidia0 "$1/proc/4242/fd/3" 92 ln -s /dev/nvidiactl "$1/proc/4242/fd/4" 93 ln -s /dev/nvidia0 "$1/proc/4242/fd/5" 94 ln -s /dev/null "$1/proc/77/fd/1" 95 ln -s /dev/nvidia01 "$1/proc/77/fd/2" 96 printf 'idle\n' > "$1/proc/77/comm" 97 } 98 99 gi=proc/driver/nvidia/gpus/000f:01:00.0/information 100 sy=sys/bus/pci/devices/000f:01:00.0 101 # name|expected rc|edit command run inside the variant root 102 variants=' 103 noversion|4|rm proc/driver/nvidia/version 104 nonvrm|10|printf "Driver: 580.173.02\n" > proc/driver/nvidia/version 105 nover|10|printf "NVRM version: NVIDIA module 580. build\n" > proc/driver/nvidia/version 106 nobus|11|grep -v "^Bus Location" GI > x && mv x GI 107 nouuid|11|grep -v "^GPU UUID" GI > x && mv x GI 108 busdiff|12|sed "s/000f:01:00.0/000f:02:00.0/" GI > x && mv x GI 109 baduuid|12|sed "s/GPU-`0123abcd`/GPU-0123abcZ/" GI > x && mv x GI 110 badminor|12|sed "s/^Device Minor:.*/Device Minor: \t 256/" GI > x && mv x GI 111 wrapminor|12|sed "s/^Device Minor:.*/Device Minor: \t `18446744073709551616`/" GI > x && mv x GI 112 uuidpfx|12|sed "s/GPU-`0123abcd`/GPX-`0123abcd`/" GI > x && mv x GI 113 baddir|12|mkdir proc/driver/nvidia/gpus/bogus 114 vendor|13|printf "0x1002\n" > SY/vendor 115 uphex|14|printf "0x2E12\n" > SY/device 116 nohex|14|printf "2e12\n" > SY/device 117 widthx|14|printf "x16\n" > SY/max_link_width 118 speed|15|printf "3.0 GT/s PCIe\n" > SY/current_link_speed 119 ' 120 121 check_fixture() { # bin -> 0 when every fixture expectation holds 122 b=$1; ok=0 123 mkroot "$out/r" 124 "$b" --root "$out/r" > "$out/fx.out" 2> "$out/fx.err"; rc=$? 125 if [ "$rc" -ne 0 ] || ! cmp -s "$out/fx.out" "$t/expected-gb10.txt" || [ -s "$out/fx.err" ]; then 126 echo " fixture: rc=$rc" >&2; ok=1 127 fi 128 # Absent optional files become [N/A]; "Unknown" link speed too. 129 mkroot "$out/r" 130 rm "$out/r/$sy/class" "$out/r/$sy/subsystem_device" 131 printf 'Unknown\n' > "$out/r/$sy/max_link_speed" 132 "$b" --root "$out/r" > "$out/na.out" 2>&1; rc=$? 133 if [ "$rc" -ne 0 ] || ! grep -qx 'gpu.0.pci.base_classcode=\[N/A\]' "$out/na.out" \ 134 || ! grep -qx 'gpu.0.pci.sub_device_id=\[N/A\]' "$out/na.out" \ 135 || ! grep -qx 'gpu.0.pcie.link.gen.max=\[N/A\]' "$out/na.out" \ 136 || ! grep -qx 'gpu.0.pcie.link.gen.current=1' "$out/na.out"; then 137 echo " optional-absent: rc=$rc" >&2; ok=1 138 fi 139 # No GPU directory at all: count=0, no gpu keys. 140 mkroot "$out/r" 141 rm -rf "$out/r/proc/driver/nvidia/gpus" 142 "$b" --root "$out/r" > "$out/ng.out" 2>&1; rc=$? 143 if [ "$rc" -ne 0 ] || ! grep -qx 'count=0' "$out/ng.out" || grep -q '^gpu\.' "$out/ng.out"; then 144 echo " no-gpus: rc=$rc" >&2; ok=1 145 fi 146 printf '%s\n' "$variants" | while IFS='|' read -r name want cmd; do 147 [ -n "$name" ] || continue 148 mkroot "$out/v" 149 c=$(printf '%s' "$cmd" | sed "s#GI#$gi#g; s#SY#$sy#g") 150 (cd "$out/v" && sh -c "$c") || { echo " variant $name: setup failed" >&2; exit 1; } 151 "$b" --root "$out/v" > "$out/v.out" 2> "$out/v.err"; rc=$? 152 if [ "$rc" -ne "$want" ] || [ -s "$out/v.out" ] || ! grep -q '^SMI REFUSE ' "$out/v.err"; then 153 echo " variant $name: rc=$rc want $want" >&2; exit 1 154 fi 155 done || ok=1 156 for a in '--bogus' '--root' '--rm --rm' "--root $out/r --root $out/r"; do 157 # shellcheck disable=SC2086 158 "$b" $a > "$out/u.out" 2> "$out/u.err"; rc=$? 159 if [ "$rc" -ne 2 ] || [ -s "$out/u.out" ]; then echo " usage '$a': rc=$rc" >&2; ok=1; fi 160 done 161 return $ok 162 } 163 164 cnt() { grep -cx "FAKE $1" "$2"; } 165 166 check_rm() { # fake-built bin -> 0 when the --rm expectations hold 167 b=$1; ok=0 168 mkroot "$out/r" 169 env -u SMIFAULT "$b" --root "$out/r" --rm > "$out/rm.out" 2> "$out/rm.err"; rc=$? 170 if [ "$rc" -ne 0 ] || ! cmp -s "$out/rm.out" "$t/expected-gb10-rm.txt" \ 171 || [ "$(cnt free "$out/rm.err")" != 3 ] || [ "$(cnt close "$out/rm.err")" != 2 ] \ 172 || [ "$(cnt open "$out/rm.err")" != 2 ] || grep -q 'UNKNOWN\|UNEXPECTED' "$out/rm.err"; then 173 echo " rm: rc=$rc" >&2; ok=1 174 fi 175 # Unsupported pstate is not an error: the report differs from the default one only in that line. 176 SMIFAULT=u "$b" --root "$out/r" --rm > "$out/u.out" 2> "$out/u.err"; rc=$? 177 sed 's/^gpu\.0\.pstate=P8$/gpu.0.pstate=[N\/A]/' "$t/expected-gb10-rm.txt" > "$out/u.want" 178 if [ "$rc" -ne 0 ] || ! cmp -s "$out/u.out" "$out/u.want" || cmp -s "$out/u.want" "$t/expected-gb10-rm.txt" \ 179 || [ "$(cnt free "$out/u.err")" != 3 ] || grep -q 'UNKNOWN\|UNEXPECTED' "$out/u.err"; then 180 echo " rm pstate unsupported: rc=$rc" >&2; ok=1 181 fi 182 # Unsupported utilization, an empty ring and a zero newest timestamp print [N/A] for both fields. 183 sed -e 's/^gpu\.0\.utilization\.gpu=42$/gpu.0.utilization.gpu=[N\/A]/' \ 184 -e 's/^gpu\.0\.utilization\.memory=13$/gpu.0.utilization.memory=[N\/A]/' "$t/expected-gb10-rm.txt" > "$out/w.want" 185 if cmp -s "$out/w.want" "$t/expected-gb10-rm.txt"; then 186 echo " rm utilization [N/A] expectation equals the default report" >&2; ok=1 187 fi 188 for l in w e z; do 189 SMIFAULT=$l "$b" --root "$out/r" --rm > "$out/w.out" 2> "$out/w.err"; rc=$? 190 if [ "$rc" -ne 0 ] || ! cmp -s "$out/w.out" "$out/w.want" \ 191 || [ "$(cnt free "$out/w.err")" != 3 ] || grep -q 'UNKNOWN\|UNEXPECTED' "$out/w.err"; then 192 echo " rm utilization fault $l: rc=$rc" >&2; ok=1 193 fi 194 done 195 # letter|rc|frees|closes|opens 196 for f in 'o|30|0|0|1' 's|30|3|2|2' 'c|31|3|2|2' 'q|31|3|2|2' 'y|31|3|2|2' 'n|32|3|2|2' 't|32|3|2|2' 'm|32|3|2|2' 'p|32|3|2|2' 'r|32|3|2|2' 'x|32|3|2|2' 'f|33|3|2|2'; do 197 IFS='|' read -r l want fr cl op < "$out/f.out" 2> "$out/f.err"; rc=$? 201 if [ "$rc" -ne "$want" ] || [ -s "$out/f.out" ] || ! grep -q '^SMI REFUSE ' "$out/f.err" \ 202 || [ "$(cnt free "$out/f.err")" != "$fr" ] || [ "$(cnt close "$out/f.err")" != "$cl" ] \ 203 || [ "$(cnt open "$out/f.err")" != "$op" ] || grep -q 'UNEXPECTED' "$out/f.err"; then 204 echo " rm fault $l: rc=$rc want $want" >&2; ok=1 205 fi 206 done 207 # --rm needs exactly one GPU; refused before any device effect. 208 mkroot "$out/r" 209 rm -rf "$out/r/proc/driver/nvidia/gpus" 210 "$b" --root "$out/r" --rm > "$out/f.out" 2> "$out/f.err"; rc=$? 211 if [ "$rc" -ne 34 ] || [ -s "$out/f.out" ] || grep -q '^FAKE' "$out/f.err"; then 212 echo " rm no-gpu: rc=$rc" >&2; ok=1 213 fi 214 return $ok 215 } 216 217 smi=$root/native/smi/smi.om 218 rmu=$root/native/smi/rm.om 219 build "$smi" "$rmu" "$out/cudapp-smi" || { cat "$out/cudapp-smi.log" >&2; fail 'cudapp-smi did not compile'; exit 1; } 220 build "$smi" "$rmu" "$out/smi-fake" fake || { cat "$out/smi-fake.log" >&2; fail 'fake build did not compile'; exit 1; } 221 guard "$rmu" '|fopen|fclose' || fail 'rm.om holds an OS effect word' 222 guard "$smi" '' || fail 'smi.om holds a device word' 223 nu=$(awk -v re="^($dev|fopen|fclose)\$" '$1 != "note" { for (i = 1; i <= NF; i++) if ($i ~ re) n++ } END { print n + 0 }' "$root/native/smi/util.om") 224 [ "$nu" = 1 ] || fail "util.om holds $nu OS effect words, not exactly its one devcall" 225 if guard "$root/native/nvrm/context.om" '|fopen|fclose' > /dev/null; then 226 fail 'guard negative control: unrewritten context.om was not refused' 227 else 228 echo 'PASS guard negative control (unrewritten context.om refused)' 229 fi 230 231 if check_fixture "$out/cudapp-smi"; then echo 'PASS fixture tier (gb10, optional-absent, no-gpus, 16 refusals, usage)'; else fail 'fixture tier'; fi 232 if check_rm "$out/smi-fake"; then echo 'PASS rm fake tier (report, pstate and utilization [N/A], 12 faults, teardown counts, one-GPU rule)'; else fail 'rm fake tier'; fi 233 234 # Devcall bound probe: a 55888-byte control on /dev/null (not a GPU device) returns ENOTTY under OMC; 235 # under BASE_OMC it must trap 3 (exit 103), proving the bound is what this branch depends on. 236 "$omc" < "$t/capprobe.om" > "$out/cap" 2> "$out/cap.log" && chmod 700 "$out/cap" || fail 'capprobe did not compile' 237 "$out/cap"; rc=$? 238 if [ "$rc" -eq 0 ]; then echo 'PASS devcall bound probe (55888-byte devcall made, ENOTTY)'; else fail "capprobe rc=$rc under OMC"; fi 239 if [ -n "$base" ]; then 240 "$base" < "$t/capprobe.om" > "$out/cap0" 2> "$out/cap0.log" && chmod 700 "$out/cap0" || fail 'capprobe did not compile under BASE_OMC' 241 "$out/cap0"; rc=$? 242 if [ "$rc" -eq 103 ]; then echo 'PASS devcall bound negative control (BASE_OMC traps 3, exit 103)'; else fail "capprobe rc=$rc under BASE_OMC, want 103"; fi 243 else 244 echo 'NOT_RUN devcall bound negative control (no BASE_OMC)' 245 fi 246 247 # Checker negative control: one altered expected byte must be rejected. 248 mkroot "$out/r" 249 "$out/cudapp-smi" --root "$out/r" > "$out/nc.out" 2>/dev/null 250 sed 's/^count=1$/count=2/' "$out/nc.out" > "$out/nc.alt" 251 if cmp -s "$out/nc.alt" "$t/expected-gb10.txt" || ! cmp -s "$out/nc.out" "$t/expected-gb10.txt"; then 252 fail 'checker negative control: altered report was not distinguished' 253 else 254 echo 'PASS checker negative control (altered count rejected)' 255 fi 256 257 # Mutants: each edits one line that must occur exactly once, and must be caught. 258 # unit|search|replacement 259 mutants=' 260 smi|if and ge ven 0 ne ven 0x10de|if false 261 smi|if lnpre 0 sg1|if lnpre 0 sg2 262 smi|chkbus g|note chkbus g 263 smi|put gseen g 1|note gseen 264 smi|if not lnpre o tgpup|if false 265 smi|ofileu get gst 4 get gst 5|ofile get gst 4 get gst 5 266 smi|if or eq n 0 gt n 3|if false 267 rm|if or eq n 0 eq n 64|if eq n 0 268 rm|if or ne npget add o 4 1 ne npget o 0|if false 269 rm|if gt n 200|if false 270 rm|odec shr m 20|odec m 271 rm|refuse code msg|exit 0 272 rm|if ne nhget 28 86|if false 273 rm|if eq v shl uint 1 k|if ne v 0 274 rm|o = add 16 mul rem add ulong sbget 12 71 72 776|o = add 16 mul rem ulong sbget 12 72 776 275 rm|if or gt sbget 8 72 ge sbget 12 72|if false 276 rm|if or eq sbget 8 0 eq sbg64 o 0|if eq sbget 8 0 277 rm|if or gt g 10000 gt m 10000|if false 278 rm|odec ulong div g 100|odec ulong g 279 rm|m = sbget add o 8|m = sbget add o 136 280 rm|if not eq nhget 28 86|if false 281 rm|sbput 4 55872|sbput 4 55888 282 ' 283 mk=0; caught=0 284 printf '%s\n' "$mutants" | { while IFS='|' read -r unit from to; do 285 [ -n "$unit" ] || continue 286 mk=$((mk + 1)) 287 if [ "$unit" = smi ]; then src=$smi; else src=$rmu; fi 288 n=$(grep -cxF "$from" "$src") 289 if [ "$n" != 1 ]; then echo "FAIL: mutant site '$from' found $n times" >&2; exit 1; fi 290 awk -v f="$from" -v r="$to" '$0 == f { print r; next } { print }' "$src" > "$out/mut.om" 291 if [ "$unit" = smi ]; then s=$out/mut.om; r=$rmu; else s=$smi; r=$out/mut.om; fi 292 if ! build "$s" "$r" "$out/m-real" || ! build "$s" "$r" "$out/m-fake" fake; then 293 echo "FAIL: mutant '$from' did not compile; it proves nothing" >&2; continue 294 fi 295 if check_fixture "$out/m-real" 2>/dev/null && check_rm "$out/m-fake" 2>/dev/null; then 296 echo "FAIL: mutant survived: $from -> $to" >&2 297 else 298 caught=$((caught + 1)); echo " mutant '$from': caught" 299 fi 300 done 301 echo "mutants caught $caught/$mk" 302 [ "$caught" -eq "$mk" ]; } || fail 'mutants' 303 304 if [ "$fails" -ne 0 ]; then 305 echo "CUDAPP-SMI HOST FAIL ($fails)" 306 exit 1 307 fi 308 echo 'CUDAPP-SMI HOST PASS'