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