| 1 |
#!/bin/bash |
| 2 |
# SPDX-License-Identifier: GPL-2.0-or-later |
| 3 |
# |
| 4 |
# Copyright 2015 PMC-Sierra, Inc. |
| 5 |
# |
| 6 |
# This program is free software; you can redistribute it and/or |
| 7 |
# modify it under the terms of the GNU General Public License |
| 8 |
# as published by the Free Software Foundation; either version 2 |
| 9 |
# of the License, or (at your option) any later version. |
| 10 |
# |
| 11 |
# This program is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
# GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License |
| 17 |
# along with this program; if not, write to the Free Software |
| 18 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 19 |
# MA 02110-1301, USA. |
| 20 |
# |
| 21 |
# Author: Stephen Bates <[email protected]> |
| 22 |
# |
| 23 |
# Description: |
| 24 |
# A shell script that calls the NVMe CLI multiple times to gather |
| 25 |
# latency data. Consider this a poor man's iometer or fio for QD=1 |
| 26 |
# analysis. Of course this is below the file-system and block |
| 27 |
# layer so is a best case measurement. |
| 28 |
# |
| 29 |
|
| 30 |
DEVICE= |
| 31 |
WRITE=false |
| 32 |
COUNT=10 |
| 33 |
DATA_SIZE=4096 |
| 34 |
METADATA_SIZE=64 |
| 35 |
|
| 36 |
RAND_BASE=temp.rand |
| 37 |
RAND_WFILE=${RAND_BASE}.write |
| 38 |
RAND_RFILE=${RAND_BASE}.read |
| 39 |
OUTPUT=latency.dat |
| 40 |
|
| 41 |
green=$(tput bold)$(tput setaf 2) |
| 42 |
red=$(tput bold)$(tput setaf 1) |
| 43 |
rst=$(tput sgr0) |
| 44 |
|
| 45 |
while getopts ":d:n:w" opt; do |
| 46 |
case $opt in |
| 47 |
d) |
| 48 |
DEVICE=${OPTARG} |
| 49 |
;; |
| 50 |
n) |
| 51 |
COUNT=${OPTARG} |
| 52 |
;; |
| 53 |
w) |
| 54 |
echo "WARNING: Write mode enabled, this might trash your drive!" |
| 55 |
WRITE=true |
| 56 |
;; |
| 57 |
\?) |
| 58 |
echo "Invalid option: -$OPTARG" >&2 |
| 59 |
exit 1 |
| 60 |
;; |
| 61 |
:) |
| 62 |
echo "Option -$OPTARG requires an argument." >&2 |
| 63 |
exit 1 |
| 64 |
;; |
| 65 |
esac |
| 66 |
done |
| 67 |
|
| 68 |
if [ "$COUNT" == "0" ]; then |
| 69 |
echo "Count can not be 0" |
| 70 |
exit 1 |
| 71 |
fi |
| 72 |
|
| 73 |
if [ -z "$DEVICE" ]; then |
| 74 |
echo "regress: You must specify a NVMe device using -d" |
| 75 |
exit 1 |
| 76 |
fi |
| 77 |
|
| 78 |
function run_test { |
| 79 |
$* | grep -i latency >> ${OUTPUT} 2>&1 |
| 80 |
if (( $? )); then |
| 81 |
echo ${red}"FAILED!"${rst} |
| 82 |
echo "Failed running command: " |
| 83 |
echo " $*" |
| 84 |
exit 1 |
| 85 |
fi |
| 86 |
} |
| 87 |
|
| 88 |
rm -f ${OUTPUT} > /dev/null || exit -1 |
| 89 |
make clean > /dev/null || exit -1 |
| 90 |
make install > /dev/null || exit -1 |
| 91 |
|
| 92 |
for i in `seq 1 ${COUNT}`; |
| 93 |
do |
| 94 |
if $WRITE ; then |
| 95 |
dd if=/dev/urandom of=${RAND_WFILE} bs=${DATA_SIZE} count=1 |
| 96 |
run_test nvme write ${DEVICE} --start-block=0 --block-count=0 \ |
| 97 |
--metadata-size=${METADATA_SIZE} --data-size=${DATA_SIZE} \ |
| 98 |
--data ${RAND_WFILE} --latency |
| 99 |
rm ${RAND_WFILE} > /dev/null |
| 100 |
else |
| 101 |
run_test nvme read ${DEVICE} --start-block=0 --block-count=0 \ |
| 102 |
--metadata-size=${METADATA_SIZE} --data-size=${DATA_SIZE} \ |
| 103 |
--data ${RAND_RFILE} --latency |
| 104 |
rm ${RAND_RFILE} > /dev/null |
| 105 |
fi |
| 106 |
done |
| 107 |
|
| 108 |
# Calculate average latency |
| 109 |
SUM=0 |
| 110 |
for i in `cat ${OUTPUT} | awk '{print $3}' | xargs` |
| 111 |
do |
| 112 |
SUM=$(($SUM + $i)) |
| 113 |
done |
| 114 |
AVERAGE=$(echo "scale=2; $SUM/$COUNT" | bc -l) |
| 115 |
echo "Average Latency: $AVERAGE us" |