t/zbd: Support testing zone capacity smaller than zone size
[fio.git] / t / zbd / functions
CommitLineData
191d1d1a
BVA
1#!/bin/bash
2
68ecd85e
SK
3blkzone=$(type -p blkzone 2>/dev/null)
4sg_inq=$(type -p sg_inq 2>/dev/null)
191d1d1a
BVA
5zbc_report_zones=$(type -p zbc_report_zones 2>/dev/null)
6zbc_reset_zone=$(type -p zbc_reset_zone 2>/dev/null)
6dcb098d 7zbc_info=$(type -p zbc_info 2>/dev/null)
191d1d1a
BVA
8if [ -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
12fi
13
6dcb098d
DF
14if [ -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
20fi
21
552e214c
SK
22blkzone_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.
30is_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.
44is_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.
54check_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
191d1d1a
BVA
67# Reports the starting sector and length of the first sequential zone of device
68# $1.
69first_sequential_zone() {
70 local dev=$1
71
6dcb098d 72 if [ -n "${blkzone}" ] && [ ! -n "${use_libzbc}" ]; then
191d1d1a
BVA
73 ${blkzone} report "$dev" |
74 sed -n 's/^[[:blank:]]*start:[[:blank:]]\([0-9a-zA-Z]*\),[[:blank:]]len[[:blank:]]\([0-9a-zA-Z]*\),.*type:[[:blank:]]2(.*/\1 \2/p' |
75 {
76 read -r starting_sector length &&
77 # Convert from hex to decimal
78 echo $((starting_sector)) $((length))
79 }
80 else
81 ${zbc_report_zones} "$dev" |
82 sed -n 's/^Zone [0-9]*: type 0x2 .*, sector \([0-9]*\), \([0-9]*\) sectors,.*$/\1 \2/p' |
83 head -n1
84 fi
85}
86
d7c7539f
HH
87# Reports the summed zone capacity of $1 number of zones starting from offset $2
88# on device $3.
89total_zone_capacity() {
90 local nr_zones=$1
91 local sector=$(($2 / 512))
92 local dev=$3
93 local capacity=0 num
94 local grep_str
95
96 if [ -z "$is_zbd" ]; then
97 # For regular block devices, handle zone size as zone capacity.
98 echo $((zone_size * nr_zones))
99 return
100 fi
101
102 if [ -n "${blkzone}" ] && [ ! -n "${use_libzbc}" ]; then
103 if blkzone_reports_capacity "${dev}"; then
104 grep_str='cap \K[0-9a-zA-Z]*'
105 else
106 # If zone capacity is not reported, refer zone length.
107 grep_str='len \K[0-9a-zA-Z]*'
108 fi
109 while read num; do
110 capacity=$((capacity + num))
111 done < <(${blkzone} report -c "$nr_zones" -o "$sector" "$dev" |
112 grep -Po "${grep_str}")
113 else
114 # ZBC devices do not have zone capacity. Use zone size.
115 while read num; do
116 capacity=$((capacity + num))
117 done < <(${zbc_report_zones} -nz "$nr_zones" -start "$sector" \
118 "$dev" | grep -Po 'sector [0-9]*, \K[0-9]*')
119 fi
120
121 echo $((capacity * 512))
122}
123
191d1d1a
BVA
124max_open_zones() {
125 local dev=$1
126
6dcb098d 127 if [ -n "${sg_inq}" ] && [ ! -n "${use_libzbc}" ]; then
e80190b4
SK
128 if ! ${sg_inq} -e --page=0xB6 --len=20 --hex "$dev" \
129 > /dev/null 2>&1; then
68ecd85e
SK
130 # Non scsi device such as null_blk can not return max open zones.
131 # Use default value.
132 echo 128
133 else
134 ${sg_inq} -e --page=0xB6 --len=20 --hex "$dev" | tail -1 |
135 {
136 read -r offset b0 b1 b2 b3 trailer || return $?
137 # Convert from hex to decimal
138 max_nr_open_zones=$((0x${b0}))
139 max_nr_open_zones=$((max_nr_open_zones * 256 + 0x${b1}))
140 max_nr_open_zones=$((max_nr_open_zones * 256 + 0x${b2}))
141 max_nr_open_zones=$((max_nr_open_zones * 256 + 0x${b3}))
142 echo ${max_nr_open_zones}
143 }
144 fi
191d1d1a
BVA
145 else
146 ${zbc_report_zones} "$dev" |
147 sed -n 's/^[[:blank:]]*Maximum number of open sequential write required zones:[[:blank:]]*//p'
148 fi
149}
150
6dcb098d
DF
151is_zbc() {
152 local dev=$1
153
154 [[ -z "$(${zbc_info} "$dev" | grep "is not a zoned block device")" ]]
155}
156
157zbc_logical_block_size() {
158 local dev=$1
159
160 ${zbc_info} "$dev" |
161 grep "logical blocks" |
162 sed -n 's/^[[:blank:]]*[0-9]* logical blocks of[[:blank:]]*//p' |
163 sed 's/ B//'
164}
165
166zbc_disk_sectors() {
167 local dev=$1
168
169 zbc_info "$dev" |
170 grep "512-bytes sectors" |
171 sed -e 's/[[:blank:]]*\([0-9]*\)512-bytes sectors.*/\1/'
172}
173
191d1d1a
BVA
174# Reset the write pointer of one zone on device $1 at offset $2. The offset
175# must be specified in units of 512 byte sectors. Offset -1 means reset all
176# zones.
177reset_zone() {
178 local dev=$1 offset=$2 sectors
179
6dcb098d 180 if [ -n "${blkzone}" ] && [ ! -n "${use_libzbc}" ]; then
191d1d1a 181 if [ "$offset" -lt 0 ]; then
63a52199 182 ${blkzone} reset "$dev"
191d1d1a
BVA
183 else
184 ${blkzone} reset -o "${offset}" -c 1 "$dev"
185 fi
186 else
187 if [ "$offset" -lt 0 ]; then
188 ${zbc_reset_zone} -all "$dev" "${offset}" >/dev/null
189 else
190 ${zbc_reset_zone} -sector "$dev" "${offset}" >/dev/null
191 fi
192 fi
193}
194
195# Extract the number of bytes that have been transferred from a line like
196# READ: bw=6847KiB/s (7011kB/s), 6847KiB/s-6847KiB/s (7011kB/s-7011kB/s), io=257MiB (269MB), run=38406-38406msec
197fio_io() {
198 sed -n 's/^[[:blank:]]*'"$1"'.*, io=\([^[:blank:]]*\).*/\1/p' |
199 tail -n 1 |
200 (
201 read -r io;
202 # Parse <number>.<number><suffix> into n1, n2 and s. See also
203 # num2str().
204 shopt -s extglob
205 n1=${io%${io##*([0-9])}}
206 s=${io#${io%%*([a-zA-Z])}}
207 n2=${io#${n1}}
208 n2=${n2#.}
209 n2=${n2%$s}000
210 n2=${n2:0:3}
211 case "$s" in
212 KiB) m=10;;
213 MiB) m=20;;
214 GiB) m=30;;
215 B) m=0;;
216 *) return 1;;
217 esac
218 [ -n "$n1" ] || return 1
219 echo $(((n1 << m) + (n2 << m) / 1000))
220 )
221}
222
223fio_read() {
224 fio_io 'READ:'
225}
226
227fio_written() {
228 fio_io 'WRITE:'
229}
230
231fio_reset_count() {
1b412cb4
BVA
232 local count
233
234 count=$(sed -n 's/^.*write:[^;]*; \([0-9]*\) zone resets$/\1/p')
235 echo "${count:-0}"
191d1d1a 236}