Merge branch 'master' of https://github.com/albert-chang0/fio
[fio.git] / t / zbd / functions
index 95f9bf456b9a190ebde9461aebf378457bf316cd..1a64a21572ae6f988e7be9b306c16953e9f559a4 100644 (file)
@@ -1,22 +1,75 @@
 #!/bin/bash
 
-# To do: switch to blkzone once blkzone reset works correctly.
-blkzone=
-#blkzone=$(type -p blkzone 2>/dev/null)
+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_info=$(type -p zbc_info 2>/dev/null)
 if [ -z "${blkzone}" ] &&
        { [ -z "${zbc_report_zones}" ] || [ -z "${zbc_reset_zone}" ]; }; then
     echo "Error: neither blkzone nor zbc_report_zones is available"
     exit 1
 fi
 
+if [ -n "${use_libzbc}" ] &&
+       { [ -z "${zbc_report_zones}" ] || [ -z "${zbc_reset_zone}" ] ||
+         [ -z "${zbc_info}" ]; }; then
+    echo "Error: zbc_report_zones, or zbc_reset_zone or zbc_info is not available"
+    echo "Error: reinstall libzbc tools"
+    exit 1
+fi
+
+blkzone_reports_capacity() {
+       local dev="${1}"
+
+       [[ -n "${blkzone}" ]] &&
+               "${blkzone}" report -c 1 -o 0 "${dev}" | grep -q 'cap '
+}
+
+# Whether or not $1 (/dev/...) is a NVME ZNS device.
+is_nvme_zns() {
+       local s
+
+       s=/sys/block/$(basename "${1}")/device/subsystem
+
+       if [[ ! -h "${s}" || $(realpath "${s}") != /sys/class/nvme ]]; then
+               return 1
+       fi
+
+       [[ $(</sys/block/$(basename "${1}")/queue/zoned) == host-managed ]]
+}
+
+# Whether or not $1 (/dev/...) is a null_blk device with zone capacity smaller
+# than zone size.
+is_nullb_with_zone_cap() {
+       local f
+
+       f=/sys/kernel/config/nullb/$(basename "${1}")
+       [[ -r "${f}/zone_capacity" &&
+                  $(<"${f}/zone_capacity") -lt $(<"${f}/zone_size") ]]
+}
+
+# Check if blkzone is available and suitable for the test target device. If not
+# available, print error message and return 1. Otherwise return 0.
+check_blkzone() {
+       local dev="${1}"
+
+       # If the device supports zone capacity, mandate zone capacity report by
+       # blkzone.
+       if (is_nvme_zns "${dev}" || is_nullb_with_zone_cap "${dev}") &&
+                               ! blkzone_reports_capacity "${dev}"; then
+               echo "Error: blkzone does not report zone capacity"
+               echo "Error: install latest util-linux with blkzone"
+               return 1
+       fi
+}
+
 # Reports the starting sector and length of the first sequential zone of device
 # $1.
 first_sequential_zone() {
     local dev=$1
 
-    if [ -n "${blkzone}" ]; then
+    if [ -n "${blkzone}" ] && [ ! -n "${use_libzbc}" ]; then
        ${blkzone} report "$dev" |
            sed -n 's/^[[:blank:]]*start:[[:blank:]]\([0-9a-zA-Z]*\),[[:blank:]]len[[:blank:]]\([0-9a-zA-Z]*\),.*type:[[:blank:]]2(.*/\1 \2/p' |
            {
@@ -31,34 +84,108 @@ first_sequential_zone() {
     fi
 }
 
+# Reports the summed zone capacity of $1 number of zones starting from offset $2
+# on device $3.
+total_zone_capacity() {
+       local nr_zones=$1
+       local sector=$(($2 / 512))
+       local dev=$3
+       local capacity=0 num
+       local grep_str
+
+       if [ -z "$is_zbd" ]; then
+               # For regular block devices, handle zone size as zone capacity.
+               echo $((zone_size * nr_zones))
+               return
+       fi
+
+       if [ -n "${blkzone}" ] && [ ! -n "${use_libzbc}" ]; then
+               if blkzone_reports_capacity "${dev}"; then
+                       grep_str='cap \K[0-9a-zA-Z]*'
+               else
+                       # If zone capacity is not reported, refer zone length.
+                       grep_str='len \K[0-9a-zA-Z]*'
+               fi
+               while read num; do
+                       capacity=$((capacity + num))
+               done < <(${blkzone} report -c "$nr_zones" -o "$sector" "$dev" |
+                               grep -Po "${grep_str}")
+       else
+               # ZBC devices do not have zone capacity. Use zone size.
+               while read num; do
+                       capacity=$((capacity + num))
+               done < <(${zbc_report_zones} -nz "$nr_zones" -start "$sector" \
+                               "$dev" | grep -Po 'sector [0-9]*, \K[0-9]*')
+       fi
+
+       echo $((capacity * 512))
+}
+
 max_open_zones() {
     local dev=$1
 
-    if [ -n "${blkzone}" ]; then
-       # To do: query the maximum number of open zones using sg_raw
-       return 1
+    if [ -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
+       else
+           ${sg_inq} -e --page=0xB6 --len=20 --hex "$dev" | tail -1 |
+               {
+                   read -r offset b0 b1 b2 b3 trailer || return $?
+                   # Convert from hex to decimal
+                   max_nr_open_zones=$((0x${b0}))
+                   max_nr_open_zones=$((max_nr_open_zones * 256 + 0x${b1}))
+                   max_nr_open_zones=$((max_nr_open_zones * 256 + 0x${b2}))
+                   max_nr_open_zones=$((max_nr_open_zones * 256 + 0x${b3}))
+                   echo ${max_nr_open_zones}
+               }
+       fi
     else
        ${zbc_report_zones} "$dev" |
            sed -n 's/^[[:blank:]]*Maximum number of open sequential write required zones:[[:blank:]]*//p'
     fi
 }
 
+is_zbc() {
+       local dev=$1
+
+       [[ -z "$(${zbc_info} "$dev" | grep "is not a zoned block device")" ]]
+}
+
+zbc_logical_block_size() {
+       local dev=$1
+
+       ${zbc_info} "$dev" |
+               grep "logical blocks" |
+               sed -n 's/^[[:blank:]]*[0-9]* logical blocks of[[:blank:]]*//p' |
+               sed 's/ B//'
+}
+
+zbc_disk_sectors() {
+        local dev=$1
+
+       zbc_info "$dev" |
+               grep "512-bytes sectors" |
+               sed -e 's/[[:blank:]]*\([0-9]*\)512-bytes sectors.*/\1/'
+}
+
 # Reset the write pointer of one zone on device $1 at offset $2. The offset
 # must be specified in units of 512 byte sectors. Offset -1 means reset all
 # zones.
 reset_zone() {
     local dev=$1 offset=$2 sectors
 
-    if [ -n "${blkzone}" ]; then
+    if [ -n "${blkzone}" ] && [ ! -n "${use_libzbc}" ]; then
        if [ "$offset" -lt 0 ]; then
-           sectors=$(<"/sys/class/block/${dev#/dev/}/size")
-           ${blkzone} reset -o "${offset}" -l "$sectors" "$dev"
+           ${blkzone} reset "$dev"
        else
            ${blkzone} reset -o "${offset}" -c 1 "$dev"
        fi
     else
        if [ "$offset" -lt 0 ]; then
-           ${zbc_reset_zone} -all "$dev" "${offset}" >/dev/null
+           ${zbc_reset_zone} -all "$dev" >/dev/null
        else
            ${zbc_reset_zone} -sector "$dev" "${offset}" >/dev/null
        fi
@@ -102,5 +229,8 @@ fio_written() {
 }
 
 fio_reset_count() {
-    sed -n 's/^.*write:[^;]*; \([0-9]*\) zone resets$/\1/p'
+    local count
+
+    count=$(sed -n 's/^.*write:[^;]*; \([0-9]*\) zone resets$/\1/p')
+    echo "${count:-0}"
 }