t/zbd: set mq-deadline scheduler to device-mapper destination devices
[fio.git] / t / zbd / functions
1 #!/bin/bash
2
3 blkzone=$(type -p blkzone 2>/dev/null)
4 sg_inq=$(type -p sg_inq 2>/dev/null)
5 zbc_report_zones=$(type -p zbc_report_zones 2>/dev/null)
6 zbc_reset_zone=$(type -p zbc_reset_zone 2>/dev/null)
7 zbc_close_zone=$(type -p zbc_close_zone 2>/dev/null)
8 zbc_info=$(type -p zbc_info 2>/dev/null)
9 if [ -z "${blkzone}" ] &&
10        { [ -z "${zbc_report_zones}" ] || [ -z "${zbc_reset_zone}" ]; }; then
11     echo "Error: neither blkzone nor zbc_report_zones is available"
12     exit 1
13 fi
14
15 if [ -n "${use_libzbc}" ] &&
16        { [ -z "${zbc_report_zones}" ] || [ -z "${zbc_reset_zone}" ] ||
17          [ -z "${zbc_info}" ]; }; then
18     echo "Error: zbc_report_zones, or zbc_reset_zone or zbc_info is not available"
19     echo "Error: reinstall libzbc tools"
20     exit 1
21 fi
22
23 blkzone_reports_capacity() {
24         local dev="${1}"
25
26         [[ -n "${blkzone}" ]] &&
27                 "${blkzone}" report -c 1 -o 0 "${dev}" | grep -q 'cap '
28 }
29
30 has_command() {
31         local cmd="${1}"
32
33         cmd_path=$(type -p "${cmd}" 2>/dev/null)
34         if [ -z "${cmd_path}" ]; then
35                 echo "${cmd} is not available"
36                 return 1
37         fi
38         return 0
39 }
40
41 # Whether or not $1 (/dev/...) is a NVME ZNS device.
42 is_nvme_zns() {
43         local s
44
45         s=/sys/block/$(basename "${1}")/device/subsystem
46
47         if [[ ! -h "${s}" || $(realpath "${s}") != /sys/class/nvme ]]; then
48                 return 1
49         fi
50
51         [[ $(</sys/block/$(basename "${1}")/queue/zoned) == host-managed ]]
52 }
53
54 # Whether or not $1 (/dev/...) is a null_blk device with zone capacity smaller
55 # than zone size.
56 is_nullb_with_zone_cap() {
57         local f
58
59         f=/sys/kernel/config/nullb/$(basename "${1}")
60         [[ -r "${f}/zone_capacity" &&
61                    $(<"${f}/zone_capacity") -lt $(<"${f}/zone_size") ]]
62 }
63
64 # Check if blkzone is available and suitable for the test target device. If not
65 # available, print error message and return 1. Otherwise return 0.
66 check_blkzone() {
67         local dev="${1}"
68
69         # If the device supports zone capacity, mandate zone capacity report by
70         # blkzone.
71         if (is_nvme_zns "${dev}" || is_nullb_with_zone_cap "${dev}") &&
72                                 ! blkzone_reports_capacity "${dev}"; then
73                 echo "Error: blkzone does not report zone capacity"
74                 echo "Error: install latest util-linux with blkzone"
75                 return 1
76         fi
77 }
78
79 # Check zone capacity of each zone and report block size aligned to the zone
80 # capacities. If zone capacity is same as zone size for zones, report zone size.
81 zone_cap_bs() {
82         local dev="${1}"
83         local zone_size="${2}"
84         local sed_str='s/.*len \([0-9A-Za-z]*\), cap \([0-9A-Za-z]*\).*/\1 \2/p'
85         local cap bs="$zone_size"
86
87         # When blkzone command is neither available nor relevant to the
88         # test device, or when blkzone command does not report capacity,
89         # assume that zone capacity is same as zone size for all zones.
90         if [ -z "${blkzone}" ] || [ -z "$is_zbd" ] || [ -c "$dev" ] ||
91                    ! blkzone_reports_capacity "${dev}"; then
92                 echo "$zone_size"
93                 return
94         fi
95
96         while read -r -a line; do
97                 ((line[0] == line[1])) && continue
98                 cap=$((line[1] * 512))
99                 while ((bs > 512 && cap % bs)); do
100                         bs=$((bs / 2))
101                 done
102         done < <(blkzone report "${dev}" | sed -n "${sed_str}")
103
104         echo "$bs"
105 }
106
107 # Reports the starting sector and length of the first sequential zone of device
108 # $1.
109 first_sequential_zone() {
110     local dev=$1
111
112     if [ -n "${blkzone}" ] && [ ! -n "${use_libzbc}" ]; then
113         ${blkzone} report "$dev" |
114             sed -n 's/^[[:blank:]]*start:[[:blank:]]\([0-9a-zA-Z]*\),[[:blank:]]len[[:blank:]]\([0-9a-zA-Z]*\),.*zcond:\(14\|[[:blank:]][0-4]\)(.*type:[[:blank:]]\([2]\)(.*/\1 \2/p' |
115             {
116                 read -r starting_sector length &&
117                     # Convert from hex to decimal
118                     echo $((starting_sector)) $((length))
119             }
120     else
121         ${zbc_report_zones} "$dev" |
122             sed -n 's/^Zone [0-9]*: type 0x2 .*,[[:blank:]]cond[[:blank:]]0x[0-4e][[:blank:]].*, sector \([0-9]*\), \([0-9]*\) sectors.*$/\1 \2/p' |
123             head -n1
124     fi
125 }
126
127 # Reports the summed zone capacity of $1 number of zones starting from offset $2
128 # on device $3.
129 total_zone_capacity() {
130         local nr_zones=$1
131         local sector=$(($2 / 512))
132         local dev=$3
133         local capacity=0 num
134         local grep_str
135
136         if [ -z "$is_zbd" ]; then
137                 # For regular block devices, handle zone size as zone capacity.
138                 echo $((zone_size * nr_zones))
139                 return
140         fi
141
142         if [ -n "${blkzone}" ] && [ ! -n "${use_libzbc}" ]; then
143                 if blkzone_reports_capacity "${dev}"; then
144                         grep_str='cap \K[0-9a-zA-Z]*'
145                 else
146                         # If zone capacity is not reported, refer zone length.
147                         grep_str='len \K[0-9a-zA-Z]*'
148                 fi
149                 while read num; do
150                         capacity=$((capacity + num))
151                 done < <(${blkzone} report -c "$nr_zones" -o "$sector" "$dev" |
152                                 grep -Po "${grep_str}")
153         else
154                 # ZBC devices do not have zone capacity. Use zone size.
155                 while read num; do
156                         capacity=$((capacity + num))
157                 done < <(${zbc_report_zones} -nz "$nr_zones" -start "$sector" \
158                                 "$dev" | grep -Po 'sector [0-9]*, \K[0-9]*')
159         fi
160
161         echo $((capacity * 512))
162 }
163
164 # Reports the starting sector and length of the first zone of device $1
165 # that is not in offline (or similar) condition.
166 first_online_zone() {
167     local dev=$1
168
169     if [ -z "$is_zbd" ]; then
170         echo 0
171         return
172     fi
173
174     if [ -n "${blkzone}" ] && [ ! -n "${use_libzbc}" ]; then
175         ${blkzone} report "$dev" |
176             sed -n 's/^[[:blank:]]*start:[[:blank:]]\([0-9a-zA-Z]*\),[[:blank:]]len[[:blank:]]\([0-9a-zA-Z]*\),.*zcond:\(14\|[[:blank:]][0-4]\)(.*type:[[:blank:]][12](.*/\1/p' |
177             head -n1 |
178             {
179                 read -r starting_sector &&
180                     # Convert from hex to decimal
181                     echo $((starting_sector))
182             }
183     else
184         ${zbc_report_zones} "$dev" |
185             sed -n 's/^Zone[[:blank:]][0-9]*:[[:blank:]]type[[:blank:]]0x[12][[:blank:]].*,[[:blank:]]cond[[:blank:]]0x[0-4e][[:blank:]].*,[[:blank:]]sector[[:blank:]]\([0-9]*\),.*$/\1/p' |
186             head -n1
187     fi
188 }
189
190 # Reports the starting sector and length of the last zone of device $1
191 # that is not in offline (or similar) condition.
192 last_online_zone() {
193     local dev=$1
194
195     if [ -z "$is_zbd" ]; then
196         echo 0
197         return
198     fi
199
200     if [ -n "${blkzone}" ] && [ ! -n "${use_libzbc}" ]; then
201         ${blkzone} report "$dev" |
202             sed -n 's/^[[:blank:]]*start:[[:blank:]]\([0-9a-zA-Z]*\),[[:blank:]]len[[:blank:]]\([0-9a-zA-Z]*\),.*zcond:\(14\|[[:blank:]][0-4]\)(.*type:[[:blank:]][12](.*/\1/p' |
203             tail -1 |
204             {
205                 read -r starting_sector &&
206                     # Convert from hex to decimal
207                     echo $((starting_sector))
208             }
209     else
210         ${zbc_report_zones} "$dev" |
211             sed -n 's/^Zone[[:blank:]][0-9]*:[[:blank:]]type[[:blank:]]0x[12][[:blank:]].*,[[:blank:]]cond[[:blank:]]0x[0-4e][[:blank:]].*,[[:blank:]]sector[[:blank:]]\([0-9]*\),.*$/\1/p' |
212             tail -1
213     fi
214 }
215
216 # Get max_open_zones of SMR drives using sg_inq or libzbc tools. Two test cases
217 # 31 and 32 use this max_open_zones value. The test case 31 uses max_open_zones
218 # to decide number of write target zones. The test case 32 passes max_open_zones
219 # value to fio with --max_open_zones option. Of note is that fio itself has the
220 # feature to get max_open_zones from the device through sysfs or ioengine
221 # specific implementation. This max_open_zones fetch by test script is required
222 # in case fio is running on an old Linux kernel version which lacks
223 # max_open_zones in sysfs, or which lacks zoned block device support completely.
224 max_open_zones() {
225     local dev=$1
226     local realdev syspath
227
228     realdev=$(readlink -f "$dev")
229     syspath=/sys/block/${realdev##*/}/queue/max_open_zones
230
231     if [ -b "${realdev}" ] && [ -r "${syspath}" ]; then
232         cat ${syspath}
233     elif [ -n "${sg_inq}" ] && [ ! -n "${use_libzbc}" ]; then
234         if ! ${sg_inq} -e --page=0xB6 --len=20 --hex "$dev" \
235                  > /dev/null 2>&1; then
236             # When sg_inq can not get max open zones, specify 0 which indicates
237             # fio to get max open zones limit from the device.
238             echo 0
239         else
240             ${sg_inq} -e --page=0xB6 --len=20 --hex "$dev" | tail -1 |
241                 {
242                     read -r offset b0 b1 b2 b3 trailer || return $?
243                     # Convert from hex to decimal
244                     max_nr_open_zones=$((0x${b0}))
245                     max_nr_open_zones=$((max_nr_open_zones * 256 + 0x${b1}))
246                     max_nr_open_zones=$((max_nr_open_zones * 256 + 0x${b2}))
247                     max_nr_open_zones=$((max_nr_open_zones * 256 + 0x${b3}))
248                     echo ${max_nr_open_zones}
249                 }
250         fi
251     elif [ -n "${use_libzbc}" ]; then
252         ${zbc_report_zones} "$dev" |
253             sed -n 's/^[[:blank:]]*Maximum number of open sequential write required zones:[[:blank:]]*//p'
254     else
255         echo 0
256     fi
257 }
258
259 # If sysfs provides, get max_active_zones limit of the zoned block device.
260 max_active_zones() {
261         local dev=$1
262         local sys_queue="/sys/block/${dev##*/}/queue/"
263
264         if [[ -e "$sys_queue/max_active_zones" ]]; then
265                 cat "$sys_queue/max_active_zones"
266                 return
267         fi
268         echo 0
269 }
270
271 # Get minimum block size to write to seq zones. Refer the sysfs attribute
272 # zone_write_granularity which shows the valid minimum size regardless of zoned
273 # block device type. If the sysfs attribute is not available, refer physical
274 # block size for rotational SMR drives. For non-rotational devices such as ZNS
275 # devices, refer logical block size.
276 min_seq_write_size() {
277         local sys_path="/sys/block/$1/queue"
278         local -i size=0
279
280         if [[ -r "$sys_path/zone_write_granularity" ]]; then
281                 size=$(<"$sys_path/zone_write_granularity")
282         fi
283
284         if ((size)); then
285                 echo "$size"
286         elif (($(<"$sys_path/rotational"))); then
287                 cat "$sys_path/physical_block_size"
288         else
289                 cat "$sys_path/logical_block_size"
290         fi
291 }
292
293 is_zbc() {
294         local dev=$1
295
296         [[ -z "$(${zbc_info} "$dev" | grep "is not a zoned block device")" ]]
297 }
298
299 zbc_physical_block_size() {
300         local dev=$1
301
302         ${zbc_info} "$dev" |
303                 grep "physical blocks" |
304                 sed -n 's/^[[:blank:]]*[0-9]* physical blocks of[[:blank:]]*//p' |
305                 sed 's/ B//'
306 }
307
308 zbc_disk_sectors() {
309         local dev=$1
310
311         zbc_info "$dev" |
312                 grep "512-bytes sectors" |
313                 sed -e 's/[[:blank:]]*\([0-9]*\)512-bytes sectors.*/\1/'
314 }
315
316 # Reset the write pointer of one zone on device $1 at offset $2. The offset
317 # must be specified in units of 512 byte sectors. Offset -1 means reset all
318 # zones.
319 reset_zone() {
320     local dev=$1 offset=$2 sectors
321
322     if [ -n "${blkzone}" ] && [ ! -n "${use_libzbc}" ]; then
323         if [ "$offset" -lt 0 ]; then
324             ${blkzone} reset "$dev"
325         else
326             ${blkzone} reset -o "${offset}" -c 1 "$dev"
327         fi
328     else
329         if [ "$offset" -lt 0 ]; then
330             ${zbc_reset_zone} -all "$dev" >/dev/null
331         else
332             ${zbc_reset_zone} -sector "$dev" "${offset}" >/dev/null
333         fi
334     fi
335 }
336
337 # Close the zone on device $1 at offset $2. The offset must be specified in
338 # units of 512 byte sectors.
339 close_zone() {
340         local dev=$1 offset=$2
341
342         if [ -n "${blkzone}" ] && [ -z "${use_libzbc}" ]; then
343                 ${blkzone} close -o "${offset}" -c 1 "$dev"
344         else
345                 ${zbc_close_zone} -sector "$dev" "${offset}" >/dev/null
346         fi
347 }
348
349 # Extract the number of bytes that have been transferred from a line like
350 # READ: bw=6847KiB/s (7011kB/s), 6847KiB/s-6847KiB/s (7011kB/s-7011kB/s), io=257MiB (269MB), run=38406-38406msec
351 fio_io() {
352     sed -n 's/^[[:blank:]]*'"$1"'.*, io=\([^[:blank:]]*\).*/\1/p' |
353         tail -n 1 |
354         (
355             read -r io;
356             # Parse <number>.<number><suffix> into n1, n2 and s. See also
357             # num2str().
358             shopt -s extglob
359             n1=${io%${io##*([0-9])}}
360             s=${io#${io%%*([a-zA-Z])}}
361             n2=${io#${n1}}
362             n2=${n2#.}
363             n2=${n2%$s}000
364             n2=${n2:0:3}
365             case "$s" in
366                 KiB) m=10;;
367                 MiB) m=20;;
368                 GiB) m=30;;
369                 B)   m=0;;
370                 *)   return 1;;
371             esac
372             [ -n "$n1" ] || return 1
373             echo $(((n1 << m) + (n2 << m) / 1000))
374         )
375 }
376
377 fio_read() {
378     fio_io 'READ:'
379 }
380
381 fio_written() {
382     fio_io 'WRITE:'
383 }
384
385 fio_reset_count() {
386     local count
387
388     count=$(sed -n 's/^.*write:[^;]*; \([0-9]*\) zone resets$/\1/p')
389     echo "${count:-0}"
390 }