Commit | Line | Data |
---|---|---|
191d1d1a BVA |
1 | #!/bin/bash |
2 | ||
68ecd85e SK |
3 | blkzone=$(type -p blkzone 2>/dev/null) |
4 | sg_inq=$(type -p sg_inq 2>/dev/null) | |
191d1d1a BVA |
5 | zbc_report_zones=$(type -p zbc_report_zones 2>/dev/null) |
6 | zbc_reset_zone=$(type -p zbc_reset_zone 2>/dev/null) | |
6dcb098d | 7 | zbc_info=$(type -p zbc_info 2>/dev/null) |
191d1d1a BVA |
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 | ||
6dcb098d DF |
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 | ||
552e214c SK |
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 | ||
1ae82d67 SK |
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 | ||
9ffe433d SK |
75 | # When blkzone command is neither available nor relevant to the |
76 | # test device, or when blkzone command does not report capacity, | |
1ae82d67 | 77 | # assume that zone capacity is same as zone size for all zones. |
9ffe433d SK |
78 | if [ -z "${blkzone}" ] || [ -z "$is_zbd" ] || [ -c "$dev" ] || |
79 | ! blkzone_reports_capacity "${dev}"; then | |
1ae82d67 SK |
80 | echo "$zone_size" |
81 | return | |
82 | fi | |
83 | ||
84 | while read -r -a line; do | |
85 | ((line[0] == line[1])) && continue | |
86 | cap=$((line[1] * 512)) | |
87 | while ((bs > 512 && cap % bs)); do | |
88 | bs=$((bs / 2)) | |
89 | done | |
90 | done < <(blkzone report "${dev}" | sed -n "${sed_str}") | |
91 | ||
92 | echo "$bs" | |
93 | } | |
94 | ||
191d1d1a BVA |
95 | # Reports the starting sector and length of the first sequential zone of device |
96 | # $1. | |
97 | first_sequential_zone() { | |
98 | local dev=$1 | |
99 | ||
6dcb098d | 100 | if [ -n "${blkzone}" ] && [ ! -n "${use_libzbc}" ]; then |
191d1d1a | 101 | ${blkzone} report "$dev" | |
c6950209 | 102 | 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' | |
191d1d1a BVA |
103 | { |
104 | read -r starting_sector length && | |
105 | # Convert from hex to decimal | |
106 | echo $((starting_sector)) $((length)) | |
107 | } | |
108 | else | |
109 | ${zbc_report_zones} "$dev" | | |
c6950209 | 110 | sed -n 's/^Zone [0-9]*: type 0x2 .*,[[:blank:]]cond[[:blank:]]0x[0-4e][[:blank:]].*, sector \([0-9]*\), \([0-9]*\) sectors.*$/\1 \2/p' | |
191d1d1a BVA |
111 | head -n1 |
112 | fi | |
113 | } | |
114 | ||
d7c7539f HH |
115 | # Reports the summed zone capacity of $1 number of zones starting from offset $2 |
116 | # on device $3. | |
117 | total_zone_capacity() { | |
118 | local nr_zones=$1 | |
119 | local sector=$(($2 / 512)) | |
120 | local dev=$3 | |
121 | local capacity=0 num | |
122 | local grep_str | |
123 | ||
124 | if [ -z "$is_zbd" ]; then | |
125 | # For regular block devices, handle zone size as zone capacity. | |
126 | echo $((zone_size * nr_zones)) | |
127 | return | |
128 | fi | |
129 | ||
130 | if [ -n "${blkzone}" ] && [ ! -n "${use_libzbc}" ]; then | |
131 | if blkzone_reports_capacity "${dev}"; then | |
132 | grep_str='cap \K[0-9a-zA-Z]*' | |
133 | else | |
134 | # If zone capacity is not reported, refer zone length. | |
135 | grep_str='len \K[0-9a-zA-Z]*' | |
136 | fi | |
137 | while read num; do | |
138 | capacity=$((capacity + num)) | |
139 | done < <(${blkzone} report -c "$nr_zones" -o "$sector" "$dev" | | |
140 | grep -Po "${grep_str}") | |
141 | else | |
142 | # ZBC devices do not have zone capacity. Use zone size. | |
143 | while read num; do | |
144 | capacity=$((capacity + num)) | |
145 | done < <(${zbc_report_zones} -nz "$nr_zones" -start "$sector" \ | |
146 | "$dev" | grep -Po 'sector [0-9]*, \K[0-9]*') | |
147 | fi | |
148 | ||
149 | echo $((capacity * 512)) | |
150 | } | |
151 | ||
c6950209 DF |
152 | # Reports the starting sector and length of the first zone of device $1 |
153 | # that is not in offline (or similar) condition. | |
154 | first_online_zone() { | |
155 | local dev=$1 | |
156 | ||
157 | if [ -z "$is_zbd" ]; then | |
158 | echo 0 | |
159 | return | |
160 | fi | |
161 | ||
162 | if [ -n "${blkzone}" ] && [ ! -n "${use_libzbc}" ]; then | |
163 | ${blkzone} report "$dev" | | |
164 | 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' | | |
165 | head -n1 | | |
166 | { | |
167 | read -r starting_sector && | |
168 | # Convert from hex to decimal | |
169 | echo $((starting_sector)) | |
170 | } | |
171 | else | |
172 | ${zbc_report_zones} "$dev" | | |
173 | 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' | | |
174 | head -n1 | |
175 | fi | |
176 | } | |
177 | ||
178 | # Reports the starting sector and length of the last zone of device $1 | |
179 | # that is not in offline (or similar) condition. | |
180 | last_online_zone() { | |
181 | local dev=$1 | |
182 | ||
183 | if [ -z "$is_zbd" ]; then | |
184 | echo 0 | |
185 | return | |
186 | fi | |
187 | ||
188 | if [ -n "${blkzone}" ] && [ ! -n "${use_libzbc}" ]; then | |
189 | ${blkzone} report "$dev" | | |
190 | 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' | | |
191 | tail -1 | | |
192 | { | |
193 | read -r starting_sector && | |
194 | # Convert from hex to decimal | |
195 | echo $((starting_sector)) | |
196 | } | |
197 | else | |
198 | ${zbc_report_zones} "$dev" | | |
199 | 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' | | |
200 | tail -1 | |
201 | fi | |
202 | } | |
203 | ||
e1315822 SK |
204 | # Get max_open_zones of SMR drives using sg_inq or libzbc tools. Two test cases |
205 | # 31 and 32 use this max_open_zones value. The test case 31 uses max_open_zones | |
206 | # to decide number of write target zones. The test case 32 passes max_open_zones | |
207 | # value to fio with --max_open_zones option. Of note is that fio itself has the | |
208 | # feature to get max_open_zones from the device through sysfs or ioengine | |
209 | # specific implementation. This max_open_zones fetch by test script is required | |
210 | # in case fio is running on an old Linux kernel version which lacks | |
211 | # max_open_zones in sysfs, or which lacks zoned block device support completely. | |
191d1d1a BVA |
212 | max_open_zones() { |
213 | local dev=$1 | |
214 | ||
6dcb098d | 215 | if [ -n "${sg_inq}" ] && [ ! -n "${use_libzbc}" ]; then |
e80190b4 SK |
216 | if ! ${sg_inq} -e --page=0xB6 --len=20 --hex "$dev" \ |
217 | > /dev/null 2>&1; then | |
e1315822 SK |
218 | # When sg_inq can not get max open zones, specify 0 which indicates |
219 | # fio to get max open zones limit from the device. | |
220 | echo 0 | |
68ecd85e SK |
221 | else |
222 | ${sg_inq} -e --page=0xB6 --len=20 --hex "$dev" | tail -1 | | |
223 | { | |
224 | read -r offset b0 b1 b2 b3 trailer || return $? | |
225 | # Convert from hex to decimal | |
226 | max_nr_open_zones=$((0x${b0})) | |
227 | max_nr_open_zones=$((max_nr_open_zones * 256 + 0x${b1})) | |
228 | max_nr_open_zones=$((max_nr_open_zones * 256 + 0x${b2})) | |
229 | max_nr_open_zones=$((max_nr_open_zones * 256 + 0x${b3})) | |
230 | echo ${max_nr_open_zones} | |
231 | } | |
232 | fi | |
842fb796 | 233 | elif [ -n "${use_libzbc}" ]; then |
191d1d1a BVA |
234 | ${zbc_report_zones} "$dev" | |
235 | sed -n 's/^[[:blank:]]*Maximum number of open sequential write required zones:[[:blank:]]*//p' | |
842fb796 DF |
236 | else |
237 | echo 0 | |
191d1d1a BVA |
238 | fi |
239 | } | |
240 | ||
557cfc51 SK |
241 | # Get minimum block size to write to seq zones. Refer the sysfs attribute |
242 | # zone_write_granularity which shows the valid minimum size regardless of zoned | |
243 | # block device type. If the sysfs attribute is not available, refer physical | |
244 | # block size for rotational SMR drives. For non-rotational devices such as ZNS | |
245 | # devices, refer logical block size. | |
246 | min_seq_write_size() { | |
247 | local sys_path="/sys/block/$1/queue" | |
248 | local -i size=0 | |
249 | ||
250 | if [[ -r "$sys_path/zone_write_granularity" ]]; then | |
251 | size=$(<"$sys_path/zone_write_granularity") | |
252 | fi | |
253 | ||
254 | if ((size)); then | |
255 | echo "$size" | |
256 | elif (($(<"$sys_path/rotational"))); then | |
257 | cat "$sys_path/physical_block_size" | |
258 | else | |
259 | cat "$sys_path/logical_block_size" | |
260 | fi | |
261 | } | |
262 | ||
6dcb098d DF |
263 | is_zbc() { |
264 | local dev=$1 | |
265 | ||
266 | [[ -z "$(${zbc_info} "$dev" | grep "is not a zoned block device")" ]] | |
267 | } | |
268 | ||
557cfc51 | 269 | zbc_physical_block_size() { |
6dcb098d DF |
270 | local dev=$1 |
271 | ||
272 | ${zbc_info} "$dev" | | |
557cfc51 SK |
273 | grep "physical blocks" | |
274 | sed -n 's/^[[:blank:]]*[0-9]* physical blocks of[[:blank:]]*//p' | | |
6dcb098d DF |
275 | sed 's/ B//' |
276 | } | |
277 | ||
278 | zbc_disk_sectors() { | |
279 | local dev=$1 | |
280 | ||
281 | zbc_info "$dev" | | |
282 | grep "512-bytes sectors" | | |
283 | sed -e 's/[[:blank:]]*\([0-9]*\)512-bytes sectors.*/\1/' | |
284 | } | |
285 | ||
191d1d1a BVA |
286 | # Reset the write pointer of one zone on device $1 at offset $2. The offset |
287 | # must be specified in units of 512 byte sectors. Offset -1 means reset all | |
288 | # zones. | |
289 | reset_zone() { | |
290 | local dev=$1 offset=$2 sectors | |
291 | ||
6dcb098d | 292 | if [ -n "${blkzone}" ] && [ ! -n "${use_libzbc}" ]; then |
191d1d1a | 293 | if [ "$offset" -lt 0 ]; then |
63a52199 | 294 | ${blkzone} reset "$dev" |
191d1d1a BVA |
295 | else |
296 | ${blkzone} reset -o "${offset}" -c 1 "$dev" | |
297 | fi | |
298 | else | |
299 | if [ "$offset" -lt 0 ]; then | |
3412afb7 | 300 | ${zbc_reset_zone} -all "$dev" >/dev/null |
191d1d1a BVA |
301 | else |
302 | ${zbc_reset_zone} -sector "$dev" "${offset}" >/dev/null | |
303 | fi | |
304 | fi | |
305 | } | |
306 | ||
307 | # Extract the number of bytes that have been transferred from a line like | |
308 | # READ: bw=6847KiB/s (7011kB/s), 6847KiB/s-6847KiB/s (7011kB/s-7011kB/s), io=257MiB (269MB), run=38406-38406msec | |
309 | fio_io() { | |
310 | sed -n 's/^[[:blank:]]*'"$1"'.*, io=\([^[:blank:]]*\).*/\1/p' | | |
311 | tail -n 1 | | |
312 | ( | |
313 | read -r io; | |
314 | # Parse <number>.<number><suffix> into n1, n2 and s. See also | |
315 | # num2str(). | |
316 | shopt -s extglob | |
317 | n1=${io%${io##*([0-9])}} | |
318 | s=${io#${io%%*([a-zA-Z])}} | |
319 | n2=${io#${n1}} | |
320 | n2=${n2#.} | |
321 | n2=${n2%$s}000 | |
322 | n2=${n2:0:3} | |
323 | case "$s" in | |
324 | KiB) m=10;; | |
325 | MiB) m=20;; | |
326 | GiB) m=30;; | |
327 | B) m=0;; | |
328 | *) return 1;; | |
329 | esac | |
330 | [ -n "$n1" ] || return 1 | |
331 | echo $(((n1 << m) + (n2 << m) / 1000)) | |
332 | ) | |
333 | } | |
334 | ||
335 | fio_read() { | |
336 | fio_io 'READ:' | |
337 | } | |
338 | ||
339 | fio_written() { | |
340 | fio_io 'WRITE:' | |
341 | } | |
342 | ||
343 | fio_reset_count() { | |
1b412cb4 BVA |
344 | local count |
345 | ||
346 | count=$(sed -n 's/^.*write:[^;]*; \([0-9]*\) zone resets$/\1/p') | |
347 | echo "${count:-0}" | |
191d1d1a | 348 | } |