t/zbd: fix null_blk configuration in run-tests-against-nullb
[fio.git] / t / zbd / run-tests-against-nullb
1 #!/bin/bash
2 #
3 # Copyright (C) 2020 Western Digital Corporation or its affiliates.
4 #
5 # This file is released under the GPL.
6 #
7 # Run t/zbd/test-zbd-support script against a variety of conventional,
8 # zoned and mixed zone configurations.
9 #
10
11 usage()
12 {
13         echo "This script runs the tests from t/zbd/test-zbd-support script"
14         echo "against a nullb device in a variety of conventional and zoned"
15         echo "configurations."
16         echo "Usage: ${0} [OPTIONS]"
17         echo "Options:"
18         echo -e "\t-h Show this message."
19         echo -e "\t-L List the device layouts for every section without running"
20         echo -e "\t   tests."
21         echo -e "\t-s <#section> Only run the section with the given number."
22         echo -e "\t-t <#test> Only run the test with the given number in every section."
23         echo -e "\t-o <max_open_zones> Specify MaxOpen value, (${set_max_open} by default)."
24         echo -e "\t-n <#number of runs> Set the number of times to run the entire suite "
25         echo -e "\t   or an individual section/test."
26         echo -e "\t-q Quit t/zbd/test-zbd-support run after any failed test."
27         echo -e "\t-r Remove the /dev/nullb0 device that may still exist after"
28         echo -e "\t   running this script."
29         exit 1
30 }
31
32 cleanup_nullb()
33 {
34         for d in /sys/kernel/config/nullb/*; do [ -d "$d" ] && rmdir "$d"; done
35         modprobe -r null_blk
36         modprobe null_blk nr_devices=0 || exit $?
37         for d in /sys/kernel/config/nullb/*; do
38                 [ -d "$d" ] && rmdir "$d"
39         done
40         modprobe -r null_blk
41         [ -e /sys/module/null_blk ] && exit $?
42 }
43
44 create_nullb()
45 {
46         modprobe null_blk nr_devices=0 &&
47         cd /sys/kernel/config/nullb &&
48         mkdir nullb0 &&
49         cd nullb0 || return $?
50 }
51
52 configure_nullb()
53 {
54         echo 0 > completion_nsec &&
55                 echo ${dev_blocksize} > blocksize &&
56                 echo ${dev_size} > size &&
57                 echo 1 > memory_backed || return $?
58
59         if ((conv_pcnt < 100)); then
60                 echo 1 > zoned &&
61                         echo "${zone_size}" > zone_size || return $?
62
63                 if ((zone_capacity < zone_size)); then
64                         if ((!zcap_supported)); then
65                                 echo "null_blk does not support zone capacity"
66                                 return 2
67                         fi
68                         echo "${zone_capacity}" > zone_capacity
69                 fi
70
71                 if ((conv_pcnt)); then
72                         if ((!conv_supported)); then
73                                 echo "null_blk does not support conventional zones"
74                                 return 2
75                         fi
76                         nr_conv=$((dev_size/zone_size*conv_pcnt/100))
77                 else
78                         nr_conv=0
79                 fi
80                 echo "${nr_conv}" > zone_nr_conv
81
82                 if ((max_open)); then
83                         echo "${max_open}" > zone_max_open
84                 fi
85         fi
86
87         echo 1 > power || return $?
88         return 0
89 }
90
91 show_nullb_config()
92 {
93         if ((conv_pcnt < 100)); then
94                 echo "    $(printf "Zoned Device, %d%% Conventional Zones (%d)" \
95                           ${conv_pcnt} ${nr_conv})"
96                 echo "    $(printf "Zone Size: %d MB" ${zone_size})"
97                 echo "    $(printf "Zone Capacity: %d MB" ${zone_capacity})"
98                 if ((max_open)); then
99                         echo "    $(printf "Max Open: %d Zones" ${max_open})"
100                 else
101                         echo "    Max Open: Unlimited Zones"
102                 fi
103         else
104                 echo "    Non-zoned Device"
105         fi
106 }
107
108 #
109 # Test sections.
110 #
111 # Fully conventional device.
112 section1()
113 {
114         conv_pcnt=100
115         max_open=0
116 }
117
118 # Zoned device with no conventional zones, ZCAP == ZSIZE, unlimited MaxOpen.
119 section2()
120 {
121         conv_pcnt=0
122         zone_size=1
123         zone_capacity=1
124         max_open=0
125 }
126
127 # Zoned device with no conventional zones, ZCAP < ZSIZE, unlimited MaxOpen.
128 section3()
129 {
130         conv_pcnt=0
131         zone_size=4
132         zone_capacity=3
133         max_open=0
134 }
135
136 # Zoned device with mostly sequential zones, ZCAP == ZSIZE, unlimited MaxOpen.
137 section4()
138 {
139         conv_pcnt=10
140         zone_size=1
141         zone_capacity=1
142         max_open=0
143 }
144
145 # Zoned device with mostly sequential zones, ZCAP < ZSIZE, unlimited MaxOpen.
146 section5()
147 {
148         conv_pcnt=10
149         zone_size=4
150         zone_capacity=3
151         max_open=0
152 }
153
154 # Zoned device with mostly conventional zones, ZCAP == ZSIZE, unlimited MaxOpen.
155 section6()
156 {
157         conv_pcnt=66
158         zone_size=1
159         zone_capacity=1
160         max_open=0
161 }
162
163 # Zoned device with mostly conventional zones, ZCAP < ZSIZE, unlimited MaxOpen.
164 section7()
165 {
166         dev_size=2048
167         conv_pcnt=66
168         zone_size=4
169         zone_capacity=3
170         max_open=0
171 }
172
173 # Zoned device with no conventional zones, ZCAP == ZSIZE, limited MaxOpen.
174 section8()
175 {
176         dev_size=1024
177         conv_pcnt=0
178         zone_size=1
179         zone_capacity=1
180         max_open=${set_max_open}
181         zbd_test_opts+=("-o ${max_open}")
182 }
183
184 # Zoned device with no conventional zones, ZCAP < ZSIZE, limited MaxOpen.
185 section9()
186 {
187         conv_pcnt=0
188         zone_size=4
189         zone_capacity=3
190         max_open=${set_max_open}
191         zbd_test_opts+=("-o ${max_open}")
192 }
193
194 # Zoned device with mostly sequential zones, ZCAP == ZSIZE, limited MaxOpen.
195 section10()
196 {
197         conv_pcnt=10
198         zone_size=1
199         zone_capacity=1
200         max_open=${set_max_open}
201         zbd_test_opts+=("-o ${max_open}")
202 }
203
204 # Zoned device with mostly sequential zones, ZCAP < ZSIZE, limited MaxOpen.
205 section11()
206 {
207         conv_pcnt=10
208         zone_size=4
209         zone_capacity=3
210         max_open=${set_max_open}
211         zbd_test_opts+=("-o ${max_open}")
212 }
213
214 # Zoned device with mostly conventional zones, ZCAP == ZSIZE, limited MaxOpen.
215 section12()
216 {
217         conv_pcnt=66
218         zone_size=1
219         zone_capacity=1
220         max_open=${set_max_open}
221         zbd_test_opts+=("-o ${max_open}")
222 }
223
224 # Zoned device with mostly conventional zones, ZCAP < ZSIZE, limited MaxOpen.
225 section13()
226 {
227         dev_size=2048
228         conv_pcnt=66
229         zone_size=4
230         zone_capacity=3
231         max_open=${set_max_open}
232         zbd_test_opts+=("-o ${max_open}")
233 }
234
235 #
236 # Entry point.
237 #
238 SECONDS=0
239 scriptdir="$(cd "$(dirname "$0")" && pwd)"
240 sections=()
241 zcap_supported=1
242 conv_supported=1
243 list_only=0
244 dev_size=1024
245 dev_blocksize=4096
246 set_max_open=8
247 zbd_test_opts=()
248 num_of_runs=1
249 test_case=0
250 quit_on_err=0
251
252 while (($#)); do
253         case "$1" in
254                 -s) sections+=("$2"); shift; shift;;
255                 -o) set_max_open="${2}"; shift; shift;;
256                 -L) list_only=1; shift;;
257                 -r) cleanup_nullb; exit 0;;
258                 -n) num_of_runs="${2}"; shift; shift;;
259                 -t) test_case="${2}"; shift; shift;;
260                 -q) quit_on_err=1; shift;;
261                 -h) usage; break;;
262                 --) shift; break;;
263                  *) usage; exit 1;;
264         esac
265 done
266
267 if [ "${#sections[@]}" = 0 ]; then
268         readarray -t sections < <(declare -F | grep "section[0-9]*" |  tr -c -d "[:digit:]\n" | sort -n)
269 fi
270
271 cleanup_nullb
272
273 #
274 # Test creating null_blk device and check if newer features are supported
275 #
276 if ! eval "create_nullb"; then
277         echo "can't create nullb"
278         exit 1
279 fi
280 if ! cat /sys/kernel/config/nullb/features | grep -q zone_capacity; then
281         zcap_supported=0
282 fi
283 if ! cat /sys/kernel/config/nullb/features | grep -q zone_nr_conv; then
284         conv_supported=0
285 fi
286
287 rc=0
288 test_rc=0
289 intr=0
290 run_nr=1
291 trap 'kill ${zbd_test_pid}; intr=1' SIGINT
292
293 while ((run_nr <= $num_of_runs)); do
294         echo -e "\nRun #$run_nr:"
295         for section_number in "${sections[@]}"; do
296                 cleanup_nullb
297                 echo "---------- Section $(printf "%02d" $section_number) ----------"
298                 if ! eval "create_nullb"; then
299                         echo "error creating nullb"
300                         exit 1
301                 fi
302                 zbd_test_opts=()
303                 if ((test_case)); then
304                         zbd_test_opts+=("-t" "${test_case}")
305                 fi
306                 if ((quit_on_err)); then
307                         zbd_test_opts+=("-q")
308                 fi
309                 section$section_number
310                 configure_nullb
311                 rc=$?
312                 ((rc == 2)) && continue
313                 if ((rc)); then
314                         echo "can't set up nullb for section $(printf "%02d" $section_number)"
315                         exit 1
316                 fi
317                 show_nullb_config
318                 cd "${scriptdir}"
319                 ((intr)) && exit 1
320                 ((list_only)) && continue
321
322                 ./test-zbd-support ${zbd_test_opts[@]} /dev/nullb0 &
323                 zbd_test_pid=$!
324                 if kill -0 "${zbd_test_pid}"; then
325                         wait "${zbd_test_pid}"
326                         test_rc=$?
327                 else
328                         echo "can't run ZBD tests"
329                         exit 1
330                 fi
331                 ((intr)) && exit 1
332                 if (($test_rc)); then
333                         rc=1
334                         ((quit_on_err)) && break
335                 fi
336         done
337
338         ((rc && quit_on_err)) && break
339         run_nr=$((run_nr + 1))
340 done
341
342 if ((!list_only)); then
343         echo "--------------------------------"
344         echo "Total run time: $(TZ=UTC0 printf "%(%H:%M:%S)T\n" $(( SECONDS )) )"
345 fi
346
347 exit $rc