t/zbd: Fix a bug in max_open_zones()
[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 # Reports the starting sector and length of the first sequential zone of device
23 # $1.
24 first_sequential_zone() {
25     local dev=$1
26
27     if [ -n "${blkzone}" ] && [ ! -n "${use_libzbc}" ]; then
28         ${blkzone} report "$dev" |
29             sed -n 's/^[[:blank:]]*start:[[:blank:]]\([0-9a-zA-Z]*\),[[:blank:]]len[[:blank:]]\([0-9a-zA-Z]*\),.*type:[[:blank:]]2(.*/\1 \2/p' |
30             {
31                 read -r starting_sector length &&
32                     # Convert from hex to decimal
33                     echo $((starting_sector)) $((length))
34             }
35     else
36         ${zbc_report_zones} "$dev" |
37             sed -n 's/^Zone [0-9]*: type 0x2 .*, sector \([0-9]*\), \([0-9]*\) sectors,.*$/\1 \2/p' |
38             head -n1
39     fi
40 }
41
42 max_open_zones() {
43     local dev=$1
44
45     if [ -n "${sg_inq}" ] && [ ! -n "${use_libzbc}" ]; then
46         if ! ${sg_inq} -e --page=0xB6 --len=20 --hex "$dev" \
47                  > /dev/null 2>&1; then
48             # Non scsi device such as null_blk can not return max open zones.
49             # Use default value.
50             echo 128
51         else
52             ${sg_inq} -e --page=0xB6 --len=20 --hex "$dev" | tail -1 |
53                 {
54                     read -r offset b0 b1 b2 b3 trailer || return $?
55                     # Convert from hex to decimal
56                     max_nr_open_zones=$((0x${b0}))
57                     max_nr_open_zones=$((max_nr_open_zones * 256 + 0x${b1}))
58                     max_nr_open_zones=$((max_nr_open_zones * 256 + 0x${b2}))
59                     max_nr_open_zones=$((max_nr_open_zones * 256 + 0x${b3}))
60                     echo ${max_nr_open_zones}
61                 }
62         fi
63     else
64         ${zbc_report_zones} "$dev" |
65             sed -n 's/^[[:blank:]]*Maximum number of open sequential write required zones:[[:blank:]]*//p'
66     fi
67 }
68
69 is_zbc() {
70         local dev=$1
71
72         [[ -z "$(${zbc_info} "$dev" | grep "is not a zoned block device")" ]]
73 }
74
75 zbc_logical_block_size() {
76         local dev=$1
77
78         ${zbc_info} "$dev" |
79                 grep "logical blocks" |
80                 sed -n 's/^[[:blank:]]*[0-9]* logical blocks of[[:blank:]]*//p' |
81                 sed 's/ B//'
82 }
83
84 zbc_disk_sectors() {
85         local dev=$1
86
87         zbc_info "$dev" |
88                 grep "512-bytes sectors" |
89                 sed -e 's/[[:blank:]]*\([0-9]*\)512-bytes sectors.*/\1/'
90 }
91
92 # Reset the write pointer of one zone on device $1 at offset $2. The offset
93 # must be specified in units of 512 byte sectors. Offset -1 means reset all
94 # zones.
95 reset_zone() {
96     local dev=$1 offset=$2 sectors
97
98     if [ -n "${blkzone}" ] && [ ! -n "${use_libzbc}" ]; then
99         if [ "$offset" -lt 0 ]; then
100             sectors=$(<"/sys/class/block/${dev#/dev/}/size")
101             ${blkzone} reset -o "${offset}" -l "$sectors" "$dev"
102         else
103             ${blkzone} reset -o "${offset}" -c 1 "$dev"
104         fi
105     else
106         if [ "$offset" -lt 0 ]; then
107             ${zbc_reset_zone} -all "$dev" "${offset}" >/dev/null
108         else
109             ${zbc_reset_zone} -sector "$dev" "${offset}" >/dev/null
110         fi
111     fi
112 }
113
114 # Extract the number of bytes that have been transferred from a line like
115 # READ: bw=6847KiB/s (7011kB/s), 6847KiB/s-6847KiB/s (7011kB/s-7011kB/s), io=257MiB (269MB), run=38406-38406msec
116 fio_io() {
117     sed -n 's/^[[:blank:]]*'"$1"'.*, io=\([^[:blank:]]*\).*/\1/p' |
118         tail -n 1 |
119         (
120             read -r io;
121             # Parse <number>.<number><suffix> into n1, n2 and s. See also
122             # num2str().
123             shopt -s extglob
124             n1=${io%${io##*([0-9])}}
125             s=${io#${io%%*([a-zA-Z])}}
126             n2=${io#${n1}}
127             n2=${n2#.}
128             n2=${n2%$s}000
129             n2=${n2:0:3}
130             case "$s" in
131                 KiB) m=10;;
132                 MiB) m=20;;
133                 GiB) m=30;;
134                 B)   m=0;;
135                 *)   return 1;;
136             esac
137             [ -n "$n1" ] || return 1
138             echo $(((n1 << m) + (n2 << m) / 1000))
139         )
140 }
141
142 fio_read() {
143     fio_io 'READ:'
144 }
145
146 fio_written() {
147     fio_io 'WRITE:'
148 }
149
150 fio_reset_count() {
151     local count
152
153     count=$(sed -n 's/^.*write:[^;]*; \([0-9]*\) zone resets$/\1/p')
154     echo "${count:-0}"
155 }