t/zbd: get max_open_zones from sysfs
[fio.git] / t / zbd / functions
index 40ffe1deebe9ae23e2459082059a3db080b7e7a8..4faa45a92df7831776acc752c7ae81e7440d9fb5 100644 (file)
@@ -4,6 +4,7 @@ blkzone=$(type -p blkzone 2>/dev/null)
 sg_inq=$(type -p sg_inq 2>/dev/null)
 zbc_report_zones=$(type -p zbc_report_zones 2>/dev/null)
 zbc_reset_zone=$(type -p zbc_reset_zone 2>/dev/null)
+zbc_close_zone=$(type -p zbc_close_zone 2>/dev/null)
 zbc_info=$(type -p zbc_info 2>/dev/null)
 if [ -z "${blkzone}" ] &&
        { [ -z "${zbc_report_zones}" ] || [ -z "${zbc_reset_zone}" ]; }; then
@@ -64,6 +65,34 @@ check_blkzone() {
        fi
 }
 
+# Check zone capacity of each zone and report block size aligned to the zone
+# capacities. If zone capacity is same as zone size for zones, report zone size.
+zone_cap_bs() {
+       local dev="${1}"
+       local zone_size="${2}"
+       local sed_str='s/.*len \([0-9A-Za-z]*\), cap \([0-9A-Za-z]*\).*/\1 \2/p'
+       local cap bs="$zone_size"
+
+       # When blkzone command is neither available nor relevant to the
+       # test device, or when blkzone command does not report capacity,
+       # assume that zone capacity is same as zone size for all zones.
+       if [ -z "${blkzone}" ] || [ -z "$is_zbd" ] || [ -c "$dev" ] ||
+                  ! blkzone_reports_capacity "${dev}"; then
+               echo "$zone_size"
+               return
+       fi
+
+       while read -r -a line; do
+               ((line[0] == line[1])) && continue
+               cap=$((line[1] * 512))
+               while ((bs > 512 && cap % bs)); do
+                       bs=$((bs / 2))
+               done
+       done < <(blkzone report "${dev}" | sed -n "${sed_str}")
+
+       echo "$bs"
+}
+
 # Reports the starting sector and length of the first sequential zone of device
 # $1.
 first_sequential_zone() {
@@ -173,15 +202,29 @@ last_online_zone() {
     fi
 }
 
+# Get max_open_zones of SMR drives using sg_inq or libzbc tools. Two test cases
+# 31 and 32 use this max_open_zones value. The test case 31 uses max_open_zones
+# to decide number of write target zones. The test case 32 passes max_open_zones
+# value to fio with --max_open_zones option. Of note is that fio itself has the
+# feature to get max_open_zones from the device through sysfs or ioengine
+# specific implementation. This max_open_zones fetch by test script is required
+# in case fio is running on an old Linux kernel version which lacks
+# max_open_zones in sysfs, or which lacks zoned block device support completely.
 max_open_zones() {
     local dev=$1
+    local realdev syspath
+
+    realdev=$(readlink -f "$dev")
+    syspath=/sys/block/${realdev##*/}/queue/max_open_zones
 
-    if [ -n "${sg_inq}" ] && [ ! -n "${use_libzbc}" ]; then
+    if [ -b "${realdev}" ] && [ -r "${syspath}" ]; then
+       cat ${syspath}
+    elif [ -n "${sg_inq}" ] && [ ! -n "${use_libzbc}" ]; then
        if ! ${sg_inq} -e --page=0xB6 --len=20 --hex "$dev" \
                 > /dev/null 2>&1; then
-           # Non scsi device such as null_blk can not return max open zones.
-           # Use default value.
-           echo 128
+           # When sg_inq can not get max open zones, specify 0 which indicates
+           # fio to get max open zones limit from the device.
+           echo 0
        else
            ${sg_inq} -e --page=0xB6 --len=20 --hex "$dev" | tail -1 |
                {
@@ -194,24 +237,60 @@ max_open_zones() {
                    echo ${max_nr_open_zones}
                }
        fi
-    else
+    elif [ -n "${use_libzbc}" ]; then
        ${zbc_report_zones} "$dev" |
            sed -n 's/^[[:blank:]]*Maximum number of open sequential write required zones:[[:blank:]]*//p'
+    else
+       echo 0
     fi
 }
 
+# If sysfs provides, get max_active_zones limit of the zoned block device.
+max_active_zones() {
+       local dev=$1
+       local sys_queue="/sys/block/${dev##*/}/queue/"
+
+       if [[ -e "$sys_queue/max_active_zones" ]]; then
+               cat "$sys_queue/max_active_zones"
+               return
+       fi
+       echo 0
+}
+
+# Get minimum block size to write to seq zones. Refer the sysfs attribute
+# zone_write_granularity which shows the valid minimum size regardless of zoned
+# block device type. If the sysfs attribute is not available, refer physical
+# block size for rotational SMR drives. For non-rotational devices such as ZNS
+# devices, refer logical block size.
+min_seq_write_size() {
+       local sys_path="/sys/block/$1/queue"
+       local -i size=0
+
+       if [[ -r "$sys_path/zone_write_granularity" ]]; then
+               size=$(<"$sys_path/zone_write_granularity")
+       fi
+
+       if ((size)); then
+               echo "$size"
+       elif (($(<"$sys_path/rotational"))); then
+               cat "$sys_path/physical_block_size"
+       else
+               cat "$sys_path/logical_block_size"
+       fi
+}
+
 is_zbc() {
        local dev=$1
 
        [[ -z "$(${zbc_info} "$dev" | grep "is not a zoned block device")" ]]
 }
 
-zbc_logical_block_size() {
+zbc_physical_block_size() {
        local dev=$1
 
        ${zbc_info} "$dev" |
-               grep "logical blocks" |
-               sed -n 's/^[[:blank:]]*[0-9]* logical blocks of[[:blank:]]*//p' |
+               grep "physical blocks" |
+               sed -n 's/^[[:blank:]]*[0-9]* physical blocks of[[:blank:]]*//p' |
                sed 's/ B//'
 }
 
@@ -244,6 +323,18 @@ reset_zone() {
     fi
 }
 
+# Close the zone on device $1 at offset $2. The offset must be specified in
+# units of 512 byte sectors.
+close_zone() {
+       local dev=$1 offset=$2
+
+       if [ -n "${blkzone}" ] && [ -z "${use_libzbc}" ]; then
+               ${blkzone} close -o "${offset}" -c 1 "$dev"
+       else
+               ${zbc_close_zone} -sector "$dev" "${offset}" >/dev/null
+       fi
+}
+
 # Extract the number of bytes that have been transferred from a line like
 # READ: bw=6847KiB/s (7011kB/s), 6847KiB/s-6847KiB/s (7011kB/s-7011kB/s), io=257MiB (269MB), run=38406-38406msec
 fio_io() {