Whamcloud - gitweb
LU-515 canonicalize the devices names
[fs/lustre-release.git] / lustre-iokit / sgpdd-survey / sgpdd-survey
1 #!/bin/bash
2
3 ######################################################################
4 # customize per survey
5
6 # CHOOSE EITHER scsidevs or rawdevs
7 # the SCSI devices to measure - WARNING: will be erased.
8 # The raw devices to use
9 # rawdevs=${rawdevs:-"/dev/raw/raw1"}
10 # scsidevs=`ls /dev/sd[a-z] /dev/sd[a-z][a-z]` # all devices, if you use udev
11
12 # result file prefix.  date/time+hostname makes unique
13 # NB ensure the path exists if it includes subdirs
14 rslt_loc=${rslt_loc:-"/tmp"}
15 rslt=${rslt:-"$rslt_loc/sgpdd_survey_`date +%F@%R`_`uname -n`"}
16
17 # what to do (read or write)
18 actions=${actions:-"write read"}
19
20 # total size per device (MBytes)
21 # NB bigger than device cache is good
22 size=${size:-8192}
23
24 # record size (KBytes)
25 rszlo=${rszlo:-1024}
26 rszhi=${rszhi:-1024}
27
28 # Concurrent regions per device
29 crglo=${crglo:-1}
30 crghi=${crghi:-256}
31
32 # threads to share between concurrent regions per device
33 # multiple threads per region simulates a deeper request queue
34 # NB survey skips over #thr < #regions and #thr/#regions > SG_MAX_QUEUE
35 thrlo=${thrlo:-1}
36 thrhi=${thrhi:-4096}
37
38 #####################################################################
39 # leave the rest of this alone unless you know what you're doing...
40
41 # and max # threads one instance will spawn
42 SG_MAX_QUEUE=16
43
44 # is the sg module loaded?
45 sg_is_loaded=$(grep -q "^sg " /proc/modules && echo true || echo false)
46
47 # did we load it?
48 sg_was_loaded=false
49
50 # map given device names into SG device names
51 i=0
52 devs=()
53 if [ "$scsidevs" ]; then
54         # we will test for a LUN, the test for a partition
55         # if the partition number is > 9 this will fail
56
57     # make sure sg kernel module is loaded
58     if ! $sg_is_loaded; then
59         echo "loading the sg kernel module"
60         modprobe sg && sg_was_loaded=true
61         sg_is_loaded=true
62     fi
63
64     for d in $scsidevs; do
65         if [[ -L "$d" ]]; then
66             echo "Device $d specified by alias. Will 'readlink' for device name"
67             d=$(readlink -f $d)
68         fi
69         devs[$i]=`sg_map | awk "{if (\\\$2 == \"$d\") print \\\$1}"`
70         if [ -z "${devs[i]}" ]; then
71             echo "Can't find SG device for $d, testing for partition"
72             pt=`echo $d | sed 's/[0-9]*$//'`
73             # Try again
74             devs[$i]=`sg_map | awk "{if (\\\$2 == \"$pt\") print \\\$1}"`
75             if [ -z "${devs[i]}" ]; then
76                 echo -e "Can't find SG device $pt.\nDo you have the sg module configured for your kernel?"
77                 exit 1
78            fi
79         fi
80         i=$((i+1))
81     done
82 elif [ "$rawdevs" ]; then
83     for r in $rawdevs; do
84         RES=`raw -q $r`
85         if [ $? -eq 0 ];then
86             devs[$i]=$r
87             i=$((i+1))
88         else
89             echo "Raw device $r not set up"
90             exit 1
91         fi
92     done
93 else
94     echo "Must specify scsidevs or rawdevs"
95     exit 1
96 fi
97
98 ndevs=${#devs[@]}
99
100 # determine block size. This should also work for raw devices
101 # If it fails, set to 512
102 bs=$((`sg_readcap -b ${devs[0]} | awk '{print $2}'`))
103 if [ $bs == 0  ];then
104         echo "sg_readcap failed, setting block size to 512"
105         bs=512
106 fi
107 rsltf=${rslt}.summary
108 workf=${rslt}.detail
109 echo -n > $rsltf
110 echo -n > $workf
111
112 print_summary () {
113     if [ "$1" = "-n" ]; then
114         minusn=$1; shift
115     else
116         minusn=""
117     fi
118     echo $minusn "$*" >> $rsltf
119     echo $minusn "$*"
120 }
121
122 print_summary "$(date) sgpdd-survey on $rawdevs$scsidevs from $(hostname)"
123
124 for ((rsz=$rszlo;rsz<=$rszhi;rsz*=2)); do
125     for ((crg=$crglo;crg<=$crghi;crg*=2)); do 
126         for ((thr=$thrlo;thr<=$thrhi;thr*=2)); do
127             if ((thr < crg || thr/crg > SG_MAX_QUEUE)); then
128                 continue
129             fi
130             # compute parameters
131             bpt=$((rsz*1024/bs))
132             blocks=$((size*((1024*1024)/bs)/crg))
133             count=$blocks
134             # show computed parameters
135             actual_rsz=$((bpt*bs/1024))
136             actual_size=$((bs*count*crg/1024))
137             str=`printf 'total_size %8dK rsz %4d crg %5d thr %5d ' \
138                          $((actual_size*ndevs)) $actual_rsz $((crg*ndevs)) $((thr*ndevs))`
139             echo "==============> $str" >> $workf
140             print_summary -n "$str"
141             freemem=`awk < /proc/meminfo '/^MemTotal:/ {printf "%d\n", $2}'`
142             if (((actual_rsz*thr/crg + 64)*crg*ndevs > freemem)); then
143                 print_summary "ENOMEM"
144                 continue
145             fi
146             # run tests
147             for action in $actions; do
148                 print_summary -n "$action "
149                 echo "=====> $action" >> $workf
150                 tmpf=${workf}_tmp
151                 # start test
152                 t0=`date +%s.%N`
153                 for ((i=0;i<ndevs;i++)); do
154                     dev=${devs[i]}
155                     devsize=$((bs*`sg_readcap -b ${dev} | awk '{print $1}'`/1024))
156                     if [ $devsize -lt $actual_size ]; then
157                         _dev=$(sg_map | grep $dev | awk '{ print $2; }')
158                         echo -e "device $_dev not big enough: $devsize <" \
159                                 "$actual_size.\nConsider reducing \$size"
160                         exit 1
161                     fi
162                     if [ $action = read ]; then
163                         inf="if=$dev"
164                         outf="of=/dev/null"
165                         skip=skip
166                     else
167                         inf="if=/dev/zero"
168                         outf="of=$dev"
169                         skip=seek
170                     fi
171                     for ((j=0;j<crg;j++)); do 
172                         sgp_dd 2> ${tmpf}_${i}_${j} \
173                             $inf $outf ${skip}=$((1024+j*blocks)) \
174                             thr=$((thr/crg)) count=$count bs=$bs bpt=$bpt time=1&
175                     done
176                 done 
177                 wait
178                 t1=`date +%s.%N`
179                 # collect/check individual stats
180                 echo > $tmpf
181                 ok=0
182                 for ((i=0;i<ndevs;i++)); do
183                     for ((j=0;j<crg;j++)); do
184                         rtmp=${tmpf}_${i}_${j}
185                         if grep 'error' $rtmp > /dev/null 2>&1; then
186                                 echo "Error found in $rtmp"
187                         elif grep 'time to transfer data' $rtmp > /dev/null 2>&1; then
188                             ok=$((ok + 1))
189                         fi
190                         cat ${rtmp} >> $tmpf
191                         cat ${rtmp} >> $workf
192                         rm  ${rtmp}
193                     done
194                 done
195                 if ((ok != ndevs*crg)); then
196                     print_summary -n "$((ndevs*crg - ok)) failed "
197                 else
198                     # compute MB/sec from elapsed
199                     bw=`awk "BEGIN {printf \"%7.2f MB/s\", $actual_size * $ndevs / (( $t1 - $t0 ) * 1024); exit}"`
200                     # compute MB/sec from nregions*slowest
201                     check=`awk < $tmpf \
202                         '/time to transfer data/ {mb=$8/1.048576; if (n == 0 || mb < min) min = mb; n++}\
203                         END {printf "%5d x %6.2f = %7.2f MB/s", n, min, min * n}'`
204                     print_summary -n "$bw $check "
205                 fi
206                 rm $tmpf
207             done
208             print_summary ""
209         done
210     done
211 done
212
213 if $sg_was_loaded; then
214     echo "unloading sg module"
215     rmmod sg
216 fi