Debian Codegraph @

debian/tests/gpuisol-test-launcher

raw symbols SCIP
1 #!/bin/bash
2 # Helper for running tests on GPUs
3 #
4 # Author: Christian Kastner <[email protected]>
5 # License: MIT
6
7 usage() {
8 cat >&2 <<"EOF"
9 usage: gpuisol-test-launcher [options] VENDOR CMD [ARGS ...]
10
11 Checks for availability of and access to a GPU from VENDOR, runs the test, and
12 exits with the exit code of the test, or with exit code 77 (which autopkgtest
13 interprets as "skipped") if no GPU was found. Optionally exports some system
14 data as autopkgtest artifacts.
15
16 VENDOR can currently be either 'amd' or 'nvidia'.
17
18 Use this helper to skip tests on ci.debian.net (which doesn't support GPUs) but
19 have them run on ci.ai.debian.net (which supports various GPUs from AMD and
20 NVIDIA), and on ci.rocm.debian.net (which as numerous AMD GPUs).
21
22 To run the autopkgtests on your own system, in a QEMU VM or a rootless podman
23 container, you will need the utilities provided by package gpuisol-qemu
24 resp. gpuisol-podman.
25
26 Supported options:
27 -h, --help
28 Print this help
29 --cd-tmp
30 Change directory to AUTOPKGTEST_TMP before executing the test.
31
32 Supported environment variables:
33 GPUISOL_TEST_LANCHER_WITH_DMESG
34 If set, export dmesg before and after the test as an autopkgtest artifact.
35 The user in the testbed must have access to dmesg, so either the user needs
36 to be privileged, or dmesg must not be restricted. Restriction can be
37 lifted with sudo `sysctl kernel.dmesg_restrict=0`.
38 GPUISOL_TEST_LAUNCHER_WITH_AMD_DRI[=PATH]
39 If set, export firmware and possibly other GPU-specific information as an
40 autopkgtest artifact. The user in the testbed must have access to
41 "/sys/kernel/debug/dri/", which requires privileges. Alternatively, one
42 can bind-mount that directory to some user-readable path, eg:
43 `mount --bind /sys/kernel/debug/dri /tmp/foo`, and pass that path as
44 GPUISOL_TEST_LAUNCHER_WITH_AMD_DRI=/tmp/foo.
45 GPUISOL_TEST_LAUNCHER_WITH_AMD_ROCMINFO
46 If set, export the output of `rocminfo` as an autopkgtest artifact.
47
48 Examples for d/tests/control, for a test with an AMD GPU:
49 Simple:
50 Test-Command: gpuisol-test-launcher amd testRunner --verbose --skip fooTest
51 Depends: @, gpuisol-test-launcher
52 Restrictions: skippable
53 Architecture: amd64 arm64 ppc64el
54
55 Write your own test runner/wrapper, have gpuisol-test-launcher call it:
56 Test-Command: gpuisol-test-launcher amd debian/tests/my-runner
57 Depends: @, gpuisol-test-launcher
58 Restrictions: skippable
59 Architecture: amd64 arm64 ppc64el
60 EOF
61 }
62
63 vendor=
64 opt_cd_tmp=0
65 opt_with_dmesg=0
66 opt_with_amd_dri=0
67 opt_with_amd_rocminfo=0
68 dri_path=
69
70 # Can't use getopt because it won't stop parsing, but any options after the
71 # first positional argument (the test command) aren't for us, they are for
72 # the test command
73 while [[ $# -gt 0 ]]; do
74 case "$1" in
75 -h | --help)
76 usage
77 exit 0
78 ;;
79 --cd-tmp)
80 opt_cd_tmp=1
81 shift
82 ;;
83 --)
84 shift
85 break
86 ;;
87 -*)
88 echo "$0: unknown option: $1" >&2
89 usage
90 exit 1
91 ;;
92 *)
93 break
94 ;;
95 esac
96 done
97
98 if [ -z "$1" ]; then
99 echo "Not enough arguments." >&2
100 exit 1
101 fi
102 case "$1" in
103 amd)
104 vendor=amd
105 ;;
106 nvidia)
107 vendor=nvidia
108 ;;
109 *)
110 echo "Unsupported vendor: $1" >&2
111 exit 1
112 ;;
113 esac
114 shift
115
116 # Test that each variable is actually set (null or not)
117 if [ -n "${ROCM_TEST_LAUNCHER_WITH_DMESG+x}" ]; then
118 opt_with_dmesg=1
119 fi
120 if [ -n "${ROCM_TEST_LAUNCHER_WITH_AMD_DRI+x}" ]; then
121 opt_with_amd_dri=1
122 dri_path="${ROCM_TEST_LAUNCHER_WITH_AMD_DRI}"
123 fi
124 if [ -n "${ROCM_TEST_LAUNCHER_WITH_AMD_ROCMINFO+x}" ]; then
125 opt_with_amd_rocminfo=1
126 fi
127
128 if [ "$vendor" = "amd" ]; then
129 if [ ! -e /dev/kfd ]; then
130 echo "/dev/kfd not present, system either lacks AMD GPU or 'amdgpu' driver is not loaded."
131 echo "Skipping tests."
132 # Magic number to signal 'skipped'
133 exit 77
134 elif [ "$(id -u)" != "0" ] && [ ! -r /dev/kfd ]; then
135 echo "/dev/kfd present but no read permission."
136 echo "Skipping tests."
137 exit 77
138 fi
139 elif [ "$vendor" = "nvidia" ]; then
140 nvidia_found=0
141 if [ -e /dev/nvidiactl ]; then
142 nvidia_found=1
143 elif [ -x /usr/bin/nvidia-modprobe ] && /usr/bin/nvidia-modprobe -c 0 -u &>/dev/null; then
144 nvidia_found=1
145 elif [ -x /usr/bin/lsmod ] && lsmod | grep -Eq '^nvidia[[:space:]]+'; then
146 nvidia_found=1
147 fi
148 if [ "$nvidia_found" -ne 1 ]; then
149 echo "Either no NVIDIA GPU, or 'nvidia' driver is not loaded."
150 echo "Skipping tests."
151 # Magic number to signal 'skipped'
152 exit 77
153 fi
154 fi
155
156 # So that we can sort files by creation time
157 tstamp() {
158 echo "$(date '+%s.%N')"
159 }
160
161 check_for_sudo() {
162 local msg
163 msg="$1"
164
165 if ! [ -x /usr/bin/sudo ]; then
166 if [ -n "$msg" ]; then
167 echo "$0: sudo not available; $msg" >&2
168 else
169 echo "$0: sudo not available." >&2
170 fi
171 return 1
172 else
173 return 0
174 fi
175 }
176
177 save_dmesg() {
178 local phase
179 local outfile
180
181 phase="$1"
182 if [ "$phase" != "before" ] && [ "$phase" != "after" ]; then
183 echo "save_dmesg: unknown phase $phase" >&2
184 exit 2
185 fi
186 outfile="$AUTOPKGTEST_ARTIFACTS/$(tstamp).dmesg.$phase"
187
188 # First, try regular dmesg, which works for root and all systems with
189 # kernel.dmesg_restrict=0
190 dmesg >"$outfile" && return
191
192 check_for_sudo "could not save dmesg" || return 0
193 # shellcheck disable=SC2024 # we don't need privileged write
194 if ! sudo -n dmesg >"$outfile"; then
195 echo "$0: failed to save dmesg." >&2
196 fi
197 }
198
199 save_amd_firmware() {
200 local dripath
201 local fwinfo
202 local outfile
203 local fwfound
204
205 dripath="${1:-/sys/kernel/debug/dri}"
206
207 fwfound=0
208 if [ -d "$dripath" ]; then
209 for subpath in "$dripath"/*; do
210 index="${subpath##*/}"
211 fwinfo="$subpath/amdgpu_firmware_info"
212 outfile="$AUTOPKGTEST_ARTIFACTS/$(tstamp).amdgpu_firmware_info.$index"
213 if [ -f "$fwinfo" ]; then
214 cat "$fwinfo" >"$outfile"
215 fwfound=1
216 fi
217 done
218 else
219 # directory might be there, we just might not have permission
220 check_for_sudo "could not read firmware info" || return 0
221 if sudo -n [ -d "$dripath" ]; then
222 for subpath in $(sudo -n ls "$dripath"); do
223 index="${subpath##*/}"
224 fwinfo="$subpath/amdgpu_firmware_info"
225 outfile="$AUTOPKGTEST_ARTIFACTS/$(tstamp).amdgpu_firmware_info.$index"
226 if sudo -n [ -f "$fwinfo" ]; then
227 # shellcheck disable=SC2024 # we don't need privileged write
228 sudo -n cat "$fwinfo" >"$outfile"
229 fwfound=1
230 fi
231 done
232 else
233 echo "$0: Cannot access $dripath, cannot query firmware info." >&2
234 return
235 fi
236 fi
237 if [ "$fwfound" -eq 0 ]; then
238 echo "$0: No firmware info found. Is $dripath populated?" >&2
239 fi
240 }
241
242 save_rocminfo() {
243 # No need to check for sudo here, as we've already verified access to
244 # /dev/kfd, which should be all we need
245 if ! [ -x /usr/bin/rocminfo ]; then
246 echo "$0: rocminfo not available, not saving info." >&2
247 exit 1
248 fi
249 if ! rocminfo >"$AUTOPKGTEST_ARTIFACTS/$(tstamp).rocminfo.txt"; then
250 echo "$0: Could not save rocminfo." >&2
251 fi
252 }
253
254 ### Pre-test ###
255
256 # 16 = testbed failure
257 if ([ "$opt_with_dmesg" -eq 1 ] \
258 || [ "$opt_with_amd_dri" -eq 1 ] \
259 || [ "$opt_with_amd_rocminfo" -eq 1 ]) && [ -z "$AUTOPKGTEST_ARTIFACTS" ]; then
260 echo "AUTOPKGTEST_ARTIFACTS not set, cannot save requested artifacts." >&2
261 exit 16
262 fi
263 [ "$opt_cd_tmp" -eq 1 ] && { cd "$AUTOPKGTEST_TMP" || exit 16; }
264 [ "$opt_with_dmesg" -eq 1 ] && save_dmesg "before"
265 [ "$opt_with_amd_dri" -eq 1 ] && save_amd_firmware "$dri_path"
266 [ "$opt_with_amd_rocminfo" -eq 1 ] && save_rocminfo
267
268 ### Test ###
269
270 "$@"
271 exitcode=$?
272
273 ### Post-test ###
274
275 [ "$opt_with_dmesg" -eq 1 ] && save_dmesg "after"
276 exit $exitcode