t/zbd: Fix test target size of test case #48
[fio.git] / t / zbd / test-zbd-support
1 #!/bin/bash
2 #
3 # Copyright (C) 2018 Western Digital Corporation or its affiliates.
4 #
5 # This file is released under the GPL.
6
7 usage() {
8         echo "Usage: $(basename "$0") [OPTIONS] <test target device file>"
9         echo "Options:"
10         echo -e "\t-d Run fio with valgrind using DRD tool"
11         echo -e "\t-e Run fio with valgrind using helgrind tool"
12         echo -e "\t-v Run fio with valgrind --read-var-info option"
13         echo -e "\t-l Test with libzbc ioengine"
14         echo -e "\t-r Reset all zones before test start"
15         echo -e "\t-o <max_open_zones> Run fio with max_open_zones limit"
16         echo -e "\t-t <test #> Run only a single test case with specified number"
17         echo -e "\t-z Run fio with debug=zbd option"
18 }
19
20 max() {
21     if [ "$1" -gt "$2" ]; then
22         echo "$1"
23     else
24         echo "$2"
25     fi
26 }
27
28 min() {
29     if [ "$1" -lt "$2" ]; then
30         echo "$1"
31     else
32         echo "$2"
33     fi
34 }
35
36 ioengine() {
37         if [ -n "$use_libzbc" ]; then
38                 echo -n "--ioengine=libzbc"
39         else
40                 echo -n "--ioengine=$1"
41         fi
42 }
43
44 set_io_scheduler() {
45     local dev=$1 sched=$2
46
47     [ -e "/sys/block/$dev" ] || return $?
48     if [ -e "/sys/block/$dev/mq" ]; then
49         case "$sched" in
50             noop)        sched=none;;
51             deadline)    sched=mq-deadline;;
52         esac
53     else
54         case "$sched" in
55             none)        sched=noop;;
56             mq-deadline) sched=deadline;;
57         esac
58     fi
59
60     echo "$sched" >"/sys/block/$dev/queue/scheduler"
61 }
62
63 check_read() {
64     local read
65
66     read=$(fio_read <"${logfile}.${test_number}")
67     echo "read: $read <> $1" >> "${logfile}.${test_number}"
68     [ "$read" = "$1" ]
69 }
70
71 check_written() {
72     local written
73
74     written=$(fio_written <"${logfile}.${test_number}")
75     echo "written: $written <> $1" >> "${logfile}.${test_number}"
76     [ "$written" = "$1" ]
77 }
78
79 # Compare the reset count from the log file with reset count $2 using operator
80 # $1 (=, -ge, -gt, -le, -lt).
81 check_reset_count() {
82     local reset_count
83
84     reset_count=$(fio_reset_count <"${logfile}.${test_number}")
85     echo "reset_count: test $reset_count $1 $2" >> "${logfile}.${test_number}"
86     eval "[ '$reset_count' '$1' '$2' ]"
87 }
88
89 # Check log for failed assertions and crashes. Without these checks,
90 # a test can succeed even when these events happen, but it must fail.
91 check_log() {
92      [ ! -f "${logfile}.${1}" ] && return 0
93      ! grep -q -e "Assertion " -e "Aborted " "${logfile}.${1}"
94 }
95
96 # Whether or not $1 (/dev/...) is a SCSI device.
97 is_scsi_device() {
98     local d f
99
100     d=$(basename "$dev")
101     for f in /sys/class/scsi_device/*/device/block/"$d"; do
102         [ -e "$f" ] && return 0
103     done
104     return 1
105 }
106
107 job_var_opts_exclude() {
108         local o
109         local ex_key="${1}"
110
111         for o in "${job_var_opts[@]}"; do
112                 if [[ ${o} =~ "${ex_key}" ]]; then
113                         continue
114                 fi
115                 echo -n "${o}"
116         done
117 }
118
119 has_max_open_zones() {
120         while (($# > 1)); do
121                 if [[ ${1} =~ "--max_open_zones" ]]; then
122                         return 0
123                 fi
124                 shift
125         done
126         return 1
127 }
128
129 run_fio() {
130     local fio opts
131
132     fio=$(dirname "$0")/../../fio
133
134     opts=(${global_var_opts[@]})
135     opts+=("--max-jobs=16" "--aux-path=/tmp" "--allow_file_create=0" \
136                            "--significant_figures=10" "$@")
137     # When max_open_zones option is specified to this test script, add
138     # max_open_zones option to fio command unless the test case already add it.
139     if [[ -n ${max_open_zones_opt} ]] && ! has_max_open_zones "${opts[@]}"; then
140             opts+=("--max_open_zones=${max_open_zones_opt}")
141     fi
142     { echo; echo "fio ${opts[*]}"; echo; } >>"${logfile}.${test_number}"
143
144     "${dynamic_analyzer[@]}" "$fio" "${opts[@]}"
145 }
146
147 run_one_fio_job() {
148     local r
149
150     r=$(((RANDOM << 16) | RANDOM))
151     run_fio --name="$dev" --filename="$dev" "$@" --randseed="$r"        \
152             --thread=1 --direct=1
153 }
154
155 write_and_run_one_fio_job() {
156     local r
157     local write_offset="${1}"
158     local write_size="${2}"
159     local -a write_opts
160
161     shift 2
162     r=$(((RANDOM << 16) | RANDOM))
163     write_opts=(--name="write_job" --rw=write "$(ioengine "psync")" \
164                       --bs="${logical_block_size}" --zonemode=zbd \
165                       --zonesize="${zone_size}" --thread=1 --direct=1 \
166                       --offset="${write_offset}" --size="${write_size}")
167     write_opts+=("${job_var_opts[@]}")
168     run_fio --filename="$dev" --randseed="$r" "${write_opts[@]}" \
169             --name="$dev" --wait_for="write_job" "$@" --thread=1 --direct=1
170 }
171
172 # Run fio on the first four sequential zones of the disk.
173 run_fio_on_seq() {
174     local opts=()
175
176     opts+=("--offset=$((first_sequential_zone_sector * 512))")
177     opts+=("--size=$((4 * zone_size))" "--zonemode=zbd")
178     if [ -z "$is_zbd" ]; then
179         opts+=("--zonesize=${zone_size}")
180     fi
181     run_one_fio_job "${opts[@]}" "$@"
182 }
183
184 # Prepare for write test by resetting zones. When max_open_zones option is
185 # specified, reset all zones of the test target to ensure that zones out of the
186 # test target range do not have open zones. This allows the write test to the
187 # target range to be able to open zones up to max_open_zones.
188 prep_write() {
189         [[ -n "${max_open_zones_opt}" && -n "${is_zbd}" ]] &&
190                 reset_zone "${dev}" -1
191 }
192
193 # Check whether buffered writes are refused.
194 test1() {
195     run_fio --name=job1 --filename="$dev" --rw=write --direct=0 --bs=4K \
196             "$(ioengine "psync")" --size="${zone_size}" --thread=1      \
197             --zonemode=zbd --zonesize="${zone_size}" 2>&1 |
198         tee -a "${logfile}.${test_number}" |
199         grep -q 'Using direct I/O is mandatory for writing to ZBD drives'
200     local fio_rc=${PIPESTATUS[0]} grep_rc=${PIPESTATUS[2]}
201     case "$fio_rc" in
202         0|1) ;;
203         *)   return "$fio_rc"
204     esac
205     if [ -n "$is_zbd" ]; then
206         [ "$grep_rc" = 0 ]
207     else
208         [ "$grep_rc" != 0 ]
209     fi
210 }
211
212 # Block size exceeds zone size.
213 test2() {
214     local bs off opts=() rc
215
216     off=$(((first_sequential_zone_sector + 2 * sectors_per_zone) * 512))
217     bs=$((2 * zone_size))
218     opts+=("$(ioengine "psync")")
219     opts+=("--name=job1" "--filename=$dev" "--rw=write" "--direct=1")
220     opts+=("--zonemode=zbd" "--offset=$off" "--bs=$bs" "--size=$bs")
221     if [ -z "$is_zbd" ]; then
222         opts+=("--zonesize=${zone_size}")
223     fi
224     run_fio "${opts[@]}" >> "${logfile}.${test_number}" 2>&1 || return $?
225     ! grep -q 'WRITE:' "${logfile}.${test_number}"
226 }
227
228 # Run fio against an empty zone. This causes fio to report "No I/O performed".
229 test3() {
230     local off opts=() rc
231
232     off=$((first_sequential_zone_sector * 512 + 128 * zone_size))
233     size=$((zone_size))
234     [ -n "$is_zbd" ] && reset_zone "$dev" $((off / 512))
235     opts+=("--name=$dev" "--filename=$dev" "--offset=$off" "--bs=4K")
236     opts+=("--size=$size" "--zonemode=zbd")
237     opts+=("$(ioengine "psync")" "--rw=read" "--direct=1" "--thread=1")
238     if [ -z "$is_zbd" ]; then
239         opts+=("--zonesize=${zone_size}")
240     fi
241     run_fio "${opts[@]}" >> "${logfile}.${test_number}" 2>&1 || return $?
242     ! grep -q 'READ:' "${logfile}.${test_number}"
243 }
244
245 # Run fio with --read_beyond_wp=1 against an empty zone.
246 test4() {
247     local off opts=()
248
249     off=$((first_sequential_zone_sector * 512 + 129 * zone_size))
250     size=$((zone_size))
251     [ -n "$is_zbd" ] && reset_zone "$dev" $((off / 512))
252     opts+=("--name=$dev" "--filename=$dev" "--offset=$off" "--bs=$size")
253     opts+=("--size=$size" "--thread=1" "--read_beyond_wp=1")
254     opts+=("$(ioengine "psync")" "--rw=read" "--direct=1" "--disable_lat=1")
255     opts+=("--zonemode=zbd" "--zonesize=${zone_size}")
256     run_fio "${opts[@]}" >> "${logfile}.${test_number}" 2>&1 || return $?
257     check_read $size || return $?
258 }
259
260 # Sequential write to sequential zones.
261 test5() {
262     local size off capacity
263
264     prep_write
265     off=$((first_sequential_zone_sector * 512))
266     capacity=$(total_zone_capacity 4 $off $dev)
267     size=$((4 * zone_size))
268     run_fio_on_seq "$(ioengine "psync")" --iodepth=1 --rw=write \
269                    --bs="$(max $((zone_size / 64)) "$logical_block_size")"\
270                    --do_verify=1 --verify=md5                           \
271                    >>"${logfile}.${test_number}" 2>&1 || return $?
272     check_written $capacity || return $?
273     check_read $capacity || return $?
274 }
275
276 # Sequential read from sequential zones.
277 test6() {
278     local size off capacity
279
280     prep_write
281     off=$((first_sequential_zone_sector * 512))
282     capacity=$(total_zone_capacity 4 $off $dev)
283     size=$((4 * zone_size))
284     write_and_run_one_fio_job \
285             $((first_sequential_zone_sector * 512)) "${size}" \
286             --offset="${off}" \
287             --size="${size}" --zonemode=zbd --zonesize="${zone_size}" \
288             "$(ioengine "psync")" --iodepth=1 --rw=read \
289             --bs="$(max $((zone_size / 64)) "$logical_block_size")" \
290             >>"${logfile}.${test_number}" 2>&1 || return $?
291     check_read $capacity || return $?
292 }
293
294 # Random write to sequential zones, libaio, queue depth 1.
295 test7() {
296     local size=$((zone_size))
297     local off capacity
298
299     prep_write
300     off=$((first_sequential_zone_sector * 512))
301     capacity=$(total_zone_capacity 1 $off $dev)
302     run_fio_on_seq "$(ioengine "libaio")" --iodepth=1 --rw=randwrite    \
303                    --bs="$(min 16384 "${zone_size}")"                   \
304                    --do_verify=1 --verify=md5 --size="$size"            \
305                    >>"${logfile}.${test_number}" 2>&1 || return $?
306     check_written $capacity || return $?
307     check_read $capacity || return $?
308 }
309
310 # Random write to sequential zones, libaio, queue depth 64.
311 test8() {
312     local size off capacity
313
314     prep_write
315     size=$((4 * zone_size))
316     off=$((first_sequential_zone_sector * 512))
317     capacity=$(total_zone_capacity 4 $off $dev)
318     run_fio_on_seq "$(ioengine "libaio")" --iodepth=64 --rw=randwrite   \
319                    --bs="$(min 16384 "${zone_size}")"                   \
320                    --do_verify=1 --verify=md5                           \
321                    >>"${logfile}.${test_number}" 2>&1 || return $?
322     check_written $capacity || return $?
323     check_read $capacity || return $?
324 }
325
326 # Random write to sequential zones, sg, queue depth 1.
327 test9() {
328     local size
329
330     if ! is_scsi_device "$dev"; then
331         echo "$dev is not a SCSI device" >>"${logfile}.${test_number}"
332         return 0
333     fi
334
335     prep_write
336     size=$((4 * zone_size))
337     run_fio_on_seq --ioengine=sg                                        \
338                    --iodepth=1 --rw=randwrite --bs=16K                  \
339                    --do_verify=1 --verify=md5                           \
340                    >>"${logfile}.${test_number}" 2>&1 || return $?
341     check_written $size || return $?
342     check_read $size || return $?
343 }
344
345 # Random write to sequential zones, sg, queue depth 64.
346 test10() {
347     local size
348
349     if ! is_scsi_device "$dev"; then
350         echo "$dev is not a SCSI device" >>"${logfile}.${test_number}"
351         return 0
352     fi
353
354     prep_write
355     size=$((4 * zone_size))
356     run_fio_on_seq --ioengine=sg                                        \
357                    --iodepth=64 --rw=randwrite --bs=16K                 \
358                    --do_verify=1 --verify=md5                           \
359                    >>"${logfile}.${test_number}" 2>&1 || return $?
360     check_written $size || return $?
361     check_read $size || return $?
362 }
363
364 # Random write to sequential zones, libaio, queue depth 64, random block size.
365 test11() {
366     local size off capacity
367
368     prep_write
369     size=$((4 * zone_size))
370     off=$((first_sequential_zone_sector * 512))
371     capacity=$(total_zone_capacity 4 $off $dev)
372     run_fio_on_seq "$(ioengine "libaio")" --iodepth=64 --rw=randwrite   \
373                    --bsrange=4K-64K --do_verify=1 --verify=md5          \
374                    --debug=zbd >>"${logfile}.${test_number}" 2>&1 || return $?
375     check_written $capacity || return $?
376     check_read $capacity || return $?
377 }
378
379 # Random write to sequential zones, libaio, queue depth 64, max 1 open zone.
380 test12() {
381     local size off capacity
382
383     prep_write
384     size=$((8 * zone_size))
385     off=$((first_sequential_zone_sector * 512))
386     capacity=$(total_zone_capacity 8 $off $dev)
387     run_fio_on_seq "$(ioengine "libaio")" --iodepth=64 --rw=randwrite --bs=16K \
388                    --max_open_zones=1 --size=$size --do_verify=1 --verify=md5 \
389                    --debug=zbd >>"${logfile}.${test_number}" 2>&1 || return $?
390     check_written $capacity || return $?
391     check_read $capacity || return $?
392 }
393
394 # Random write to sequential zones, libaio, queue depth 64, max 4 open zones.
395 test13() {
396     local size off capacity
397
398     prep_write
399     size=$((8 * zone_size))
400     off=$((first_sequential_zone_sector * 512))
401     capacity=$(total_zone_capacity 8 $off $dev)
402     run_fio_on_seq "$(ioengine "libaio")" --iodepth=64 --rw=randwrite --bs=16K \
403                    --max_open_zones=4 --size=$size --do_verify=1 --verify=md5 \
404                    --debug=zbd                                                \
405                    >>"${logfile}.${test_number}" 2>&1 || return $?
406     check_written $capacity || return $?
407     check_read $capacity || return $?
408 }
409
410 # Random write to conventional zones.
411 test14() {
412     local size
413
414     prep_write
415     size=$((16 * 2**20)) # 20 MB
416     if [ $size -gt $((first_sequential_zone_sector * 512)) ]; then
417         echo "$dev does not have enough sequential zones" \
418              >>"${logfile}.${test_number}"
419         return 0
420     fi
421     run_one_fio_job "$(ioengine "libaio")" --iodepth=64 --rw=randwrite --bs=16K \
422                     --zonemode=zbd --zonesize="${zone_size}" --do_verify=1 \
423                     --verify=md5 --size=$size                              \
424                     >>"${logfile}.${test_number}" 2>&1 || return $?
425     check_written $((size)) || return $?
426     check_read $((size)) || return $?
427 }
428
429 # Sequential read on a mix of empty and full zones.
430 test15() {
431     local i off size
432     local w_off w_size w_capacity
433
434     for ((i=0;i<4;i++)); do
435         [ -n "$is_zbd" ] &&
436             reset_zone "$dev" $((first_sequential_zone_sector +
437                                  i*sectors_per_zone))
438     done
439     prep_write
440     w_off=$(((first_sequential_zone_sector + 2 * sectors_per_zone) * 512))
441     w_size=$((2 * zone_size))
442     w_capacity=$(total_zone_capacity 2 $w_off $dev)
443     off=$((first_sequential_zone_sector * 512))
444     size=$((4 * zone_size))
445     write_and_run_one_fio_job "${w_off}" "${w_size}" \
446                     "$(ioengine "psync")" --rw=read --bs=$((zone_size / 16)) \
447                     --zonemode=zbd --zonesize="${zone_size}" --offset=$off \
448                     --size=$((size)) >>"${logfile}.${test_number}" 2>&1 ||
449         return $?
450     check_written $((w_capacity)) || return $?
451     check_read $((w_capacity))
452 }
453
454 # Random read on a mix of empty and full zones.
455 test16() {
456     local off size
457     local i w_off w_size w_capacity
458
459     for ((i=0;i<4;i++)); do
460         [ -n "$is_zbd" ] &&
461             reset_zone "$dev" $((first_sequential_zone_sector +
462                                  i*sectors_per_zone))
463     done
464     prep_write
465     w_off=$(((first_sequential_zone_sector + 2 * sectors_per_zone) * 512))
466     w_size=$((2 * zone_size))
467     w_capacity=$(total_zone_capacity 2 $w_off $dev)
468     off=$((first_sequential_zone_sector * 512))
469     size=$((4 * zone_size))
470     write_and_run_one_fio_job "${w_off}" "${w_size}" \
471                     "$(ioengine "libaio")" --iodepth=64 --rw=randread --bs=16K \
472                     --zonemode=zbd --zonesize="${zone_size}" --offset=$off \
473                     --size=$size >>"${logfile}.${test_number}" 2>&1 || return $?
474     check_written $w_capacity || return $?
475     check_read $size || return $?
476 }
477
478 # Random reads and writes in the last zone.
479 test17() {
480     local io off read size written
481
482     off=$(((disk_size / zone_size - 1) * zone_size))
483     size=$((disk_size - off))
484     if [ -n "$is_zbd" ]; then
485         reset_zone "$dev" $((off / 512)) || return $?
486     fi
487     prep_write
488     run_one_fio_job "$(ioengine "libaio")" --iodepth=8 --rw=randrw --bs=4K \
489                     --zonemode=zbd --zonesize="${zone_size}"            \
490                     --offset=$off --loops=2 --norandommap=1\
491                     >>"${logfile}.${test_number}" 2>&1 || return $?
492     written=$(fio_written <"${logfile}.${test_number}")
493     read=$(fio_read <"${logfile}.${test_number}")
494     io=$((written + read))
495     echo "Total number of bytes read and written: $io <> $size" \
496          >>"${logfile}.${test_number}"
497     [ $io = $((size * 2)) ];
498 }
499
500 # Out-of-range zone reset threshold and frequency parameters.
501 test18() {
502     run_fio_on_seq --zone_reset_threshold=-1 |&
503         tee -a "${logfile}.${test_number}"   |
504             grep -q 'value out of range' || return $?
505 }
506
507 test19() {
508     run_fio_on_seq --zone_reset_threshold=2  |&
509         tee -a "${logfile}.${test_number}"   |
510         grep -q 'value out of range' || return $?
511 }
512
513 test20() {
514     run_fio_on_seq --zone_reset_threshold=.4:.6 |&
515         tee -a "${logfile}.${test_number}"   |
516         grep -q 'the list exceeding max length' || return $?
517 }
518
519 test21() {
520     run_fio_on_seq --zone_reset_frequency=-1 |&
521         tee -a "${logfile}.${test_number}"   |
522         grep -q 'value out of range' || return $?
523 }
524
525 test22() {
526     run_fio_on_seq --zone_reset_frequency=2  |&
527         tee -a "${logfile}.${test_number}"   |
528         grep -q 'value out of range' || return $?
529 }
530
531 test23() {
532     run_fio_on_seq --zone_reset_frequency=.4:.6  |&
533         tee -a "${logfile}.${test_number}"   |
534         grep -q 'the list exceeding max length' || return $?
535 }
536
537 test24() {
538     local bs loops=9 size=$((zone_size))
539     local off capacity
540
541     prep_write
542     off=$((first_sequential_zone_sector * 512))
543     capacity=$(total_zone_capacity 1 $off $dev)
544
545     bs=$(min $((256*1024)) "$zone_size")
546     run_fio_on_seq "$(ioengine "psync")" --rw=write --bs="$bs"          \
547                    --size=$size --loops=$loops                          \
548                    --zone_reset_frequency=.01 --zone_reset_threshold=.90 \
549                    >> "${logfile}.${test_number}" 2>&1 || return $?
550     check_written $((capacity * loops)) || return $?
551     check_reset_count -eq 8 ||
552         check_reset_count -eq 9 ||
553         check_reset_count -eq 10 || return $?
554 }
555
556 # Multiple non-overlapping sequential write jobs for the same drive.
557 test25() {
558     local i opts=()
559
560     for ((i=0;i<16;i++)); do
561         [ -n "$is_zbd" ] &&
562             reset_zone "$dev" $((first_sequential_zone_sector + i*sectors_per_zone))
563     done
564     prep_write
565     for ((i=0;i<16;i++)); do
566         opts+=("--name=job$i" "--filename=$dev" "--thread=1" "--direct=1")
567         opts+=("--offset=$((first_sequential_zone_sector*512 + zone_size*i))")
568         opts+=("--size=$zone_size" "$(ioengine "psync")" "--rw=write" "--bs=16K")
569         opts+=("--zonemode=zbd" "--zonesize=${zone_size}" "--group_reporting=1")
570         opts+=(${job_var_opts[@]})
571     done
572     run_fio "${opts[@]}" >> "${logfile}.${test_number}" 2>&1 || return $?
573 }
574
575 write_to_first_seq_zone() {
576     local loops=4 r
577     local off capacity
578
579     prep_write
580     off=$((first_sequential_zone_sector * 512))
581     capacity=$(total_zone_capacity 1 $off $dev)
582
583     r=$(((RANDOM << 16) | RANDOM))
584     run_fio --name="$dev" --filename="$dev" "$(ioengine "psync")" --rw="$1" \
585             --thread=1 --do_verify=1 --verify=md5 --direct=1 --bs=4K    \
586             --offset=$off                                               \
587             --size=$zone_size --loops=$loops --randseed="$r"            \
588             --zonemode=zbd --zonesize="${zone_size}" --group_reporting=1        \
589             --gtod_reduce=1 >> "${logfile}.${test_number}" 2>&1 || return $?
590     check_written $((loops * capacity)) || return $?
591 }
592
593 # Overwrite the first sequential zone four times sequentially.
594 test26() {
595     write_to_first_seq_zone write
596 }
597
598 # Overwrite the first sequential zone four times using random writes.
599 test27() {
600     write_to_first_seq_zone randwrite
601 }
602
603 # Multiple overlapping random write jobs for the same drive.
604 test28() {
605     local i jobs=16 off opts
606
607     off=$((first_sequential_zone_sector * 512 + 64 * zone_size))
608     [ -n "$is_zbd" ] && reset_zone "$dev" $((off / 512))
609     prep_write
610     opts=("--debug=zbd")
611     capacity=$(total_zone_capacity 1 $off $dev)
612     for ((i=0;i<jobs;i++)); do
613         opts+=("--name=job$i" "--filename=$dev" "--offset=$off" "--bs=16K")
614         opts+=("--size=$zone_size" "--io_size=$capacity" "$(ioengine "psync")" "--rw=randwrite")
615         opts+=("--thread=1" "--direct=1" "--zonemode=zbd")
616         opts+=("--zonesize=${zone_size}" "--group_reporting=1")
617         opts+=(${job_var_opts[@]})
618     done
619     run_fio "${opts[@]}" >> "${logfile}.${test_number}" 2>&1 || return $?
620     check_written $((jobs * $capacity)) || return $?
621     check_reset_count -eq $jobs ||
622         check_reset_count -eq $((jobs - 1)) ||
623         return $?
624 }
625
626 # Multiple overlapping random write jobs for the same drive and with a limited
627 # number of open zones.
628 test29() {
629     local i jobs=16 off opts=()
630
631     off=$((first_sequential_zone_sector * 512 + 64 * zone_size))
632     size=$((16*zone_size))
633     prep_write
634     opts=("--debug=zbd")
635     for ((i=0;i<jobs;i++)); do
636         opts+=("--name=job$i" "--filename=$dev" "--offset=$off" "--bs=16K")
637         opts+=("--size=$size" "--io_size=$zone_size" "--thread=1")
638         opts+=("$(ioengine "psync")" "--rw=randwrite" "--direct=1")
639         opts+=("--max_open_zones=4" "--group_reporting=1")
640         opts+=("--zonemode=zbd" "--zonesize=${zone_size}")
641         # max_open_zones is already specified
642         opts+=($(job_var_opts_exclude "--max_open_zones"))
643     done
644     run_fio "${opts[@]}" >> "${logfile}.${test_number}" 2>&1 || return $?
645     check_written $((jobs * zone_size)) || return $?
646 }
647
648 # Random reads and writes across the entire disk for 30s.
649 test30() {
650     local off
651
652     prep_write
653     off=$((first_sequential_zone_sector * 512))
654     run_one_fio_job "$(ioengine "libaio")" --iodepth=8 --rw=randrw      \
655                     --bs="$(max $((zone_size / 128)) "$logical_block_size")"\
656                     --zonemode=zbd --zonesize="${zone_size}" --offset=$off\
657                     --loops=2 --time_based --runtime=30s --norandommap=1\
658                     >>"${logfile}.${test_number}" 2>&1
659 }
660
661 # Random reads across all sequential zones for 30s. This is not only a fio
662 # test but also allows to verify the performance of a drive.
663 test31() {
664     local bs inc nz off opts size
665
666     prep_write
667     # Start with writing 128 KB to 128 sequential zones.
668     bs=128K
669     nz=128
670     # shellcheck disable=SC2017
671     inc=$(((disk_size - (first_sequential_zone_sector * 512)) / (nz * zone_size)
672            * zone_size))
673     opts=()
674     for ((off = first_sequential_zone_sector * 512; off < disk_size;
675           off += inc)); do
676         opts+=("--name=$dev" "--filename=$dev" "--offset=$off" "--io_size=$bs")
677         opts+=("--bs=$bs" "--size=$zone_size" "$(ioengine "libaio")")
678         opts+=("--rw=write" "--direct=1" "--thread=1" "--stats=0")
679         opts+=("--zonemode=zbd" "--zonesize=${zone_size}")
680         opts+=(${job_var_opts[@]})
681     done
682     "$(dirname "$0")/../../fio" "${opts[@]}" >> "${logfile}.${test_number}" 2>&1
683     # Next, run the test.
684     off=$((first_sequential_zone_sector * 512))
685     size=$((disk_size - off))
686     opts=("--name=$dev" "--filename=$dev" "--offset=$off" "--size=$size")
687     opts+=("--bs=$bs" "$(ioengine "psync")" "--rw=randread" "--direct=1")
688     opts+=("--thread=1" "--time_based" "--runtime=30" "--zonemode=zbd")
689     opts+=("--zonesize=${zone_size}")
690     opts+=(${job_var_opts[@]})
691     run_fio "${opts[@]}" >> "${logfile}.${test_number}" 2>&1 || return $?
692 }
693
694 # Random writes across all sequential zones. This is not only a fio test but
695 # also allows to verify the performance of a drive.
696 test32() {
697     local off opts=() size
698
699     prep_write
700     off=$((first_sequential_zone_sector * 512))
701     size=$((disk_size - off))
702     opts+=("--name=$dev" "--filename=$dev" "--offset=$off" "--size=$size")
703     opts+=("--bs=128K" "$(ioengine "psync")" "--rw=randwrite" "--direct=1")
704     opts+=("--thread=1" "--time_based" "--runtime=30")
705     opts+=("--max_open_zones=$max_open_zones" "--zonemode=zbd")
706     opts+=("--zonesize=${zone_size}")
707     run_fio "${opts[@]}" >> "${logfile}.${test_number}" 2>&1 || return $?
708 }
709
710 # Write to sequential zones with a block size that is not a divisor of the
711 # zone size.
712 test33() {
713     local bs io_size size
714     local off capacity=0;
715
716     prep_write
717     off=$((first_sequential_zone_sector * 512))
718     capacity=$(total_zone_capacity 1 $off $dev)
719     size=$((2 * zone_size))
720     io_size=$((5 * capacity))
721     bs=$((3 * capacity / 4))
722     run_fio_on_seq "$(ioengine "psync")" --iodepth=1 --rw=write \
723                    --size=$size --io_size=$io_size --bs=$bs     \
724                    >> "${logfile}.${test_number}" 2>&1 || return $?
725     check_written $(((io_size + bs - 1) / bs * bs)) || return $?
726 }
727
728 # Write to sequential zones with a block size that is not a divisor of the
729 # zone size and with data verification enabled.
730 test34() {
731     local size
732
733     prep_write
734     size=$((2 * zone_size))
735     run_fio_on_seq "$(ioengine "psync")" --iodepth=1 --rw=write --size=$size \
736                    --do_verify=1 --verify=md5 --bs=$((3 * zone_size / 4)) \
737                    >> "${logfile}.${test_number}" 2>&1 && return 1
738     grep -q 'not a divisor of' "${logfile}.${test_number}"
739 }
740
741 # Test 1/4 for the I/O boundary rounding code: $size < $zone_size.
742 test35() {
743     local bs off io_size size
744
745     prep_write
746     off=$(((first_sequential_zone_sector + 1) * 512))
747     size=$((zone_size - 2 * 512))
748     bs=$((zone_size / 4))
749     run_one_fio_job --offset=$off --size=$size "$(ioengine "psync")"    \
750                     --iodepth=1 --rw=write --do_verify=1 --verify=md5   \
751                     --bs=$bs --zonemode=zbd --zonesize="${zone_size}"   \
752                     >> "${logfile}.${test_number}" 2>&1 && return 1
753     grep -q 'io_size must be at least one zone' "${logfile}.${test_number}"
754 }
755
756 # Test 2/4 for the I/O boundary rounding code: $size < $zone_size.
757 test36() {
758     local bs off io_size size
759
760     prep_write
761     off=$(((first_sequential_zone_sector) * 512))
762     size=$((zone_size - 512))
763     bs=$((zone_size / 4))
764     run_one_fio_job --offset=$off --size=$size "$(ioengine "psync")"    \
765                     --iodepth=1 --rw=write --do_verify=1 --verify=md5   \
766                     --bs=$bs --zonemode=zbd --zonesize="${zone_size}"   \
767                     >> "${logfile}.${test_number}" 2>&1 && return 1
768     grep -q 'io_size must be at least one zone' "${logfile}.${test_number}"
769 }
770
771 # Test 3/4 for the I/O boundary rounding code: $size > $zone_size.
772 test37() {
773     local bs off size capacity
774
775     prep_write
776     capacity=$(total_zone_capacity 1 $first_sequential_zone_sector $dev)
777     if [ "$first_sequential_zone_sector" = 0 ]; then
778         off=0
779     else
780         off=$(((first_sequential_zone_sector - 1) * 512))
781     fi
782     size=$((zone_size + 2 * 512))
783     bs=$((zone_size / 4))
784     run_one_fio_job --offset=$off --size=$size "$(ioengine "psync")"    \
785                     --iodepth=1 --rw=write --do_verify=1 --verify=md5   \
786                     --bs=$bs --zonemode=zbd --zonesize="${zone_size}"   \
787                     >> "${logfile}.${test_number}" 2>&1
788     check_written $capacity || return $?
789 }
790
791 # Test 4/4 for the I/O boundary rounding code: $offset > $disk_size - $zone_size
792 test38() {
793     local bs off size
794
795     prep_write
796     size=$((logical_block_size))
797     off=$((disk_size - logical_block_size))
798     bs=$((logical_block_size))
799     run_one_fio_job --offset=$off --size=$size "$(ioengine "psync")"    \
800                     --iodepth=1 --rw=write --do_verify=1 --verify=md5   \
801                     --bs=$bs --zonemode=zbd --zonesize="${zone_size}"   \
802                     >> "${logfile}.${test_number}" 2>&1 && return 1
803     grep -q 'io_size must be at least one zone' "${logfile}.${test_number}"
804 }
805
806 # Read one block from a block device.
807 read_one_block() {
808     local bs
809
810     bs=$((logical_block_size))
811     run_one_fio_job --rw=read "$(ioengine "psync")" --bs=$bs --size=$bs "$@" 2>&1 |
812         tee -a "${logfile}.${test_number}"
813 }
814
815 # Check whether fio accepts --zonemode=none for zoned block devices.
816 test39() {
817     [ -n "$is_zbd" ] || return 0
818     read_one_block --zonemode=none >/dev/null || return $?
819     check_read $((logical_block_size)) || return $?
820 }
821
822 # Check whether fio accepts --zonemode=strided for zoned block devices.
823 test40() {
824     local bs
825
826     bs=$((logical_block_size))
827     [ -n "$is_zbd" ] || return 0
828     read_one_block --zonemode=strided |
829         grep -q 'fio: --zonesize must be specified when using --zonemode=strided' ||
830         return $?
831     read_one_block --zonemode=strided --zonesize=$bs >/dev/null || return $?
832     check_read $bs || return $?
833 }
834
835 # Check whether fio checks the zone size for zoned block devices.
836 test41() {
837     [ -n "$is_zbd" ] || return 0
838     read_one_block --zonemode=zbd --zonesize=$((2 * zone_size)) |
839         grep -q 'job parameter zonesize.*does not match disk zone size'
840 }
841
842 # Check whether fio handles --zonesize=0 correctly for regular block devices.
843 test42() {
844     [ -n "$is_zbd" ] && return 0
845     read_one_block --zonemode=zbd --zonesize=0 |
846         grep -q 'Specifying the zone size is mandatory for regular block devices with --zonemode=zbd'
847 }
848
849 # Check whether fio handles --zonesize=1 correctly for regular block devices.
850 test43() {
851     [ -n "$is_zbd" ] && return 0
852     read_one_block --zonemode=zbd --zonesize=1 |
853         grep -q 'zone size must be at least 512 bytes for --zonemode=zbd'
854 }
855
856 # Check whether fio handles --zonemode=none --zonesize=1 correctly.
857 test44() {
858     read_one_block --zonemode=none --zonesize=1 |
859         grep -q 'fio: --zonemode=none and --zonesize are not compatible'
860 }
861
862 test45() {
863     local bs i
864
865     [ -z "$is_zbd" ] && return 0
866     prep_write
867     bs=$((logical_block_size))
868     run_one_fio_job "$(ioengine "psync")" --iodepth=1 --rw=randwrite --bs=$bs\
869                     --offset=$((first_sequential_zone_sector * 512)) \
870                     --size="$zone_size" --do_verify=1 --verify=md5 2>&1 |
871         tee -a "${logfile}.${test_number}" |
872         grep -q "fio: first I/O failed. If .* is a zoned block device, consider --zonemode=zbd"
873 }
874
875 # Random write to sequential zones, libaio, 8 jobs, queue depth 64 per job
876 test46() {
877     local size
878
879     prep_write
880     size=$((4 * zone_size))
881     run_fio_on_seq "$(ioengine "libaio")" --iodepth=64 --rw=randwrite --bs=4K \
882                    --group_reporting=1 --numjobs=8 \
883                    >> "${logfile}.${test_number}" 2>&1 || return $?
884     check_written $((size * 8)) || return $?
885 }
886
887 # Check whether fio handles --zonemode=zbd --zoneskip=1 correctly.
888 test47() {
889     local bs
890
891     prep_write
892     bs=$((logical_block_size))
893     run_fio_on_seq "$(ioengine "psync")" --rw=write --bs=$bs --zoneskip=1 \
894                     >> "${logfile}.${test_number}" 2>&1 && return 1
895     grep -q 'zoneskip 1 is not a multiple of the device zone size' "${logfile}.${test_number}"
896 }
897
898 # Multiple overlapping random write jobs for the same drive and with a
899 # limited number of open zones. This is similar to test29, but uses libaio
900 # to stress test zone locking.
901 test48() {
902     local i jobs=16 off opts=()
903
904     off=$((first_sequential_zone_sector * 512 + 64 * zone_size))
905     size=$((16*zone_size))
906     prep_write
907     opts=("--aux-path=/tmp" "--allow_file_create=0" "--significant_figures=10")
908     opts+=("--debug=zbd")
909     opts+=("$(ioengine "libaio")" "--rw=randwrite" "--direct=1")
910     opts+=("--time_based" "--runtime=30")
911     opts+=("--zonemode=zbd" "--zonesize=${zone_size}")
912     opts+=("--max_open_zones=4")
913     for ((i=0;i<jobs;i++)); do
914         opts+=("--name=job$i" "--filename=$dev" "--offset=$off" "--bs=16K")
915         opts+=("--io_size=$zone_size" "--iodepth=256" "--thread=1")
916         opts+=("--size=$size" "--group_reporting=1")
917         # max_open_zones is already specified
918         opts+=($(job_var_opts_exclude "--max_open_zones"))
919     done
920
921     fio=$(dirname "$0")/../../fio
922
923     { echo; echo "fio ${opts[*]}"; echo; } >>"${logfile}.${test_number}"
924
925     timeout -v -s KILL 45s \
926             "${dynamic_analyzer[@]}" "$fio" "${opts[@]}" \
927             >> "${logfile}.${test_number}" 2>&1 || return $?
928 }
929
930 # Check if fio handles --zonecapacity on a normal block device correctly
931 test49() {
932
933     if [ -n "$is_zbd" ]; then
934         echo "$dev is not a regular block device" \
935              >>"${logfile}.${test_number}"
936         return 0
937     fi
938
939     size=$((2 * zone_size))
940     capacity=$((zone_size * 3 / 4))
941
942     run_one_fio_job "$(ioengine "psync")" --rw=write \
943                     --zonemode=zbd --zonesize="${zone_size}" \
944                     --zonecapacity=${capacity} \
945                     --verify=md5  --size=${size} >>"${logfile}.${test_number}" 2>&1 ||
946         return $?
947     check_written $((capacity * 2)) || return $?
948     check_read $((capacity * 2)) || return $?
949 }
950
951 tests=()
952 dynamic_analyzer=()
953 reset_all_zones=
954 use_libzbc=
955 zbd_debug=
956 max_open_zones_opt=
957
958 while [ "${1#-}" != "$1" ]; do
959   case "$1" in
960     -d) dynamic_analyzer=(valgrind "--read-var-info=yes" "--tool=drd"
961                           "--show-confl-seg=no");
962         shift;;
963     -e) dynamic_analyzer=(valgrind "--read-var-info=yes" "--tool=helgrind");
964         shift;;
965     -l) use_libzbc=1; shift;;
966     -r) reset_all_zones=1; shift;;
967     -t) tests+=("$2"); shift; shift;;
968     -o) max_open_zones_opt="${2}"; shift; shift;;
969     -v) dynamic_analyzer=(valgrind "--read-var-info=yes");
970         shift;;
971     -z) zbd_debug=1; shift;;
972     --) shift; break;;
973   esac
974 done
975
976 if [ $# != 1 ]; then
977     usage
978     exit 1
979 fi
980
981 # shellcheck source=functions
982 source "$(dirname "$0")/functions" || exit $?
983
984 global_var_opts=()
985 job_var_opts=()
986 if [ -n "$zbd_debug" ]; then
987     global_var_opts+=("--debug=zbd")
988 fi
989 dev=$1
990 realdev=$(readlink -f "$dev")
991 basename=$(basename "$realdev")
992
993 if [[ -b "$realdev" ]]; then
994         major=$((0x$(stat -L -c '%t' "$realdev"))) || exit $?
995         minor=$((0x$(stat -L -c '%T' "$realdev"))) || exit $?
996         disk_size=$(($(<"/sys/dev/block/$major:$minor/size")*512))
997
998         # When the target is a partition device, get basename of its
999         # holder device to access sysfs path of the holder device
1000         if [[ -r "/sys/dev/block/$major:$minor/partition" ]]; then
1001                 realsysfs=$(readlink "/sys/dev/block/$major:$minor")
1002                 basename=$(basename "${realsysfs%/*}")
1003         fi
1004         logical_block_size=$(<"/sys/block/$basename/queue/logical_block_size")
1005         case "$(<"/sys/class/block/$basename/queue/zoned")" in
1006         host-managed|host-aware)
1007                 is_zbd=true
1008                 if ! check_blkzone "${dev}"; then
1009                         exit 1
1010                 fi
1011                 if ! result=($(first_sequential_zone "$dev")); then
1012                         echo "Failed to determine first sequential zone"
1013                         exit 1
1014                 fi
1015                 first_sequential_zone_sector=${result[0]}
1016                 sectors_per_zone=${result[1]}
1017                 zone_size=$((sectors_per_zone * 512))
1018                 if ! max_open_zones=$(max_open_zones "$dev"); then
1019                         echo "Failed to determine maximum number of open zones"
1020                         exit 1
1021                 fi
1022                 set_io_scheduler "$basename" deadline || exit $?
1023                 if [ -n "$reset_all_zones" ]; then
1024                         reset_zone "$dev" -1
1025                 fi
1026                 ;;
1027         *)
1028                 first_sequential_zone_sector=$(((disk_size / 2) &
1029                                                 (logical_block_size - 1)))
1030                 zone_size=$(max 65536 "$logical_block_size")
1031                 sectors_per_zone=$((zone_size / 512))
1032                 max_open_zones=128
1033                 set_io_scheduler "$basename" none || exit $?
1034                 ;;
1035         esac
1036 elif [[ -c "$realdev" ]]; then
1037         # For an SG node, we must have libzbc option specified
1038         if [[ ! -n "$use_libzbc" ]]; then
1039                 echo "Character device files can only be used with -l (libzbc) option"
1040                 exit 1
1041         fi
1042
1043         if ! $(is_zbc "$dev"); then
1044                 echo "Device is not a ZBC disk"
1045                 exit 1
1046         fi
1047         is_zbd=true
1048
1049         if ! disk_size=($(( $(zbc_disk_sectors "$dev") * 512))); then
1050                 echo "Failed to determine disk size"
1051                 exit 1
1052         fi
1053         if ! logical_block_size=($(zbc_logical_block_size "$dev")); then
1054                 echo "Failed to determine logical block size"
1055                 exit 1
1056         fi
1057         if ! result=($(first_sequential_zone "$dev")); then
1058                 echo "Failed to determine first sequential zone"
1059                 exit 1
1060         fi
1061         first_sequential_zone_sector=${result[0]}
1062         sectors_per_zone=${result[1]}
1063         zone_size=$((sectors_per_zone * 512))
1064         if ! max_open_zones=$(max_open_zones "$dev"); then
1065                 echo "Failed to determine maximum number of open zones"
1066                 exit 1
1067         fi
1068         if [ -n "$reset_all_zones" ]; then
1069                 reset_zone "$dev" -1
1070         fi
1071 fi
1072
1073 if [[ -n ${max_open_zones_opt} ]]; then
1074         # Override max_open_zones with the script option value
1075         max_open_zones="${max_open_zones_opt}"
1076         job_var_opts+=("--max_open_zones=${max_open_zones_opt}")
1077 fi
1078
1079 echo -n "First sequential zone starts at sector $first_sequential_zone_sector;"
1080 echo " zone size: $((zone_size >> 20)) MB"
1081
1082 if [ "${#tests[@]}" = 0 ]; then
1083     readarray -t tests < <(declare -F | grep "test[0-9]*" | \
1084                                    tr -c -d "[:digit:]\n" | sort -n)
1085 fi
1086
1087 logfile=$0.log
1088
1089 passed=0
1090 failed=0
1091 if [ -t 1 ]; then
1092     red="\e[1;31m"
1093     green="\e[1;32m"
1094     end="\e[m"
1095 else
1096     red=""
1097     green=""
1098     end=""
1099 fi
1100 rc=0
1101
1102 intr=0
1103 trap 'intr=1' SIGINT
1104
1105 for test_number in "${tests[@]}"; do
1106     rm -f "${logfile}.${test_number}"
1107     echo -n "Running test $(printf "%02d" $test_number) ... "
1108     if eval "test$test_number" && check_log $test_number; then
1109         status="PASS"
1110         cc_status="${green}${status}${end}"
1111         ((passed++))
1112     else
1113         status="FAIL"
1114         cc_status="${red}${status}${end}"
1115         ((failed++))
1116         rc=1
1117     fi
1118     echo -e "$cc_status"
1119     echo "$status" >> "${logfile}.${test_number}"
1120     [ $intr -ne 0 ] && exit 1
1121 done
1122
1123 echo "$passed tests passed"
1124 if [ $failed -gt 0 ]; then
1125     echo " and $failed tests failed"
1126 fi
1127 exit $rc