Whamcloud - gitweb
5deabff79c7e303f91a3fae4a3d0f1b3cac32c27
[fs/lustre-release.git] / lustre-iokit / obdfilter-survey / obdfilter-survey
1 #!/bin/bash
2
3 ######################################################################
4 # customize per survey
5
6 # specify obd instances to exercise
7 # these can be either...
8 # obdfilter instances (set 'ost_names')
9 # ...or...
10 # echo_client instances (set 'client_names')
11 # ... use 'host:name' for obd instances on other nodes.
12
13 # allow these to be passed in via string...
14 ost_names_str=${ost_names_str:-""}
15 if [ -n "$ost_names_str" ]; then
16     declare -a ost_names
17     count=0
18     for name in $ost_names_str; do
19         ost_names[$count]=$name
20         count=$((count+1))
21     done
22 else
23     ost_names=(ost{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16})
24 fi
25
26 #client_names=(ns8:ECHO_ns8 ns9:ECHO_ns9)
27 client_names_str=${client_names_str:-""}
28 if [ -n "$client_names_str" ]; then
29     # make sure we unset ost_names so that our client_names get noticed... 
30     unset ost_names
31     declare -a client_names
32     count=0
33     for name in $client_names_str; do
34         client_names[$count]=$name
35         count=$((count+1))
36     done
37 fi
38
39 # result file prefix (date/time + hostname makes unique)
40 # NB ensure path to it exists
41 rslt=${rslt:-"/tmp/obdfilter_survey_`date +%F@%R`_`uname -n`"}
42
43 # lustre root (if running with own source tree)
44 lustre_root=${lustre_root:-"/home_nfs/eeb/lustre"}
45
46 # what tests to run (first must be write)
47 tests_str=${tests_str:-""}
48 if [ -n "$tests_str" ]; then
49     declare -a tests
50     count=0
51     for name in $tests_str; do
52         tests[$count]=$name
53         count=$((count+1))
54     done
55 else
56     #tests=(write rewrite read reread rewrite_again)
57     tests=(write rewrite read)
58 fi
59
60 # total size (MBytes) per obd instance
61 # large enough to avoid cache effects 
62 # and to make test startup/shutdown overhead insignificant
63 size=${size:-16384}
64
65 # record size (KBytes)
66 rszlo=${rszlo:-1024}
67 rszhi=${rszhi:-1024}
68
69 # number of objects per OST
70 nobjlo=${nobjlo:-1}
71 nobjhi=${nobjhi:-512}
72
73 # threads per OST (1024 max)
74 thrlo=${thrlo:-1}
75 thrhi=${thrhi:-64}
76
77 # restart from here iff all are defined
78 restart_rsz=
79 restart_thr=1
80 restart_nobj=1
81
82 # machine's page size (K)
83 if [ -z "$PAGE_SIZE" ]; then
84     if which python >/dev/null; then
85         PAGE_SIZE=`echo 'import resource; print resource.getpagesize()/1024;' |python`
86     fi
87 fi
88 PAGE_SIZE=${PAGE_SIZE:-4}
89
90 # max buffer_mem (total_threads * buffer size)
91 # (to avoid lctl ENOMEM problems)
92 max_buffer_mem=$((1024*1024))
93
94 # how to run commands on other nodes
95 # You need to make this work on your cluster if you have specified
96 # non-local obd instances above
97 custom_remote_shell () {
98     host=$1
99     shift
100     cmds="$*"
101     here=`pwd`
102     # Hop on to the remote node, chdir to 'here' and run the given
103     # commands. One of the following will probably work.
104     ssh $host "cd $here; $cmds"
105     #rsh $host "cd $here; $cmds"
106     # we have to remove the leading `uname -n`: from pdsh output lines
107     #pdsh -w $host "cd $here; $cmds" | sed 's/^[^:]*://'
108 }
109
110 #####################################################################
111 # leave the rest of this alone unless you know what you're doing...
112
113 # binaries
114 lsmod="/sbin/lsmod"
115 modprobe="/sbin/modprobe"
116 insmod="/sbin/insmod"
117 rmmod="/sbin/rmmod"
118
119 # lctl::test_brw bandwidth snapshot interval (seconds)
120 snap=1
121 # check file contents?
122 verify=1
123
124 if [ ${#tests[@]} -eq 0 -o "${tests[0]}" != "write" ]; then
125     echo "tests: ${tests[@]}"
126     echo "First test must be 'write'" 1>&2
127     exit 1
128 fi
129
130 rsltf="${rslt}.summary"
131 workf="${rslt}.detail"
132 cmdsf="${rslt}.script"
133 vmstatf="${rslt}.vmstat"
134 echo -n > $rsltf
135 echo -n > $workf
136
137 declare -a vmstatpids
138
139 # hide a little trick to unset this from the command line
140 if [ "$lustre_root" == " " ]; then
141     unset lustre_root
142 fi
143
144 if [ -z "$lustre_root" ]; then
145     lctl=$(which lctl)
146 else
147     lctl=${lustre_root}/utils/lctl
148 fi
149
150 remote_shell () {
151     host=$1
152     shift
153     cmds="$*"
154     if [ "$host" = "localhost" -o "$host" = `uname -n` ]; then
155         eval "$cmds"
156     else
157         custom_remote_shell $host "$cmds"
158     fi
159 }
160
161 obdecho_loaded() {
162     local host=$1
163     remote_shell $host $lsmod | grep obdecho > /dev/null 2>&1
164 }
165
166 load_obdecho () {
167     local host=$1
168     if [ -z "$lustre_root" ]; then
169         remote_shell $host $modprobe obdecho
170     elif [ -f ${lustre_root}/obdecho/obdecho.ko ]; then
171         remote_shell $host $insmod ${lustre_root}/obdecho/obdecho.ko
172     else
173         remote_shell $host $insmod ${lustre_root}/obdecho/obdecho.o
174     fi
175 }
176
177 unload_obdecho () {
178     local host=$1
179     remote_shell $host $rmmod obdecho
180 }
181
182 get_devno () {
183     local host=$1
184     local type=$2
185     local name=$3
186     remote_shell $host $lctl device_list | \
187         awk "{if (\$2 == \"UP\" && \$3 == \"$type\" && \$4 == \"$name\") {\
188                   print \$1; exit}}"
189 }
190
191 get_ec_devno () {
192     local host=$1
193     local client_name="$2"
194     local ost_name="$3"
195     if [ -z "$client_name" ]; then
196         if [ -z "$ost_name" ]; then
197             echo "client and ost name both null" 1>&2
198             return
199         fi
200         client_name=${ost_name}_echo_client
201     fi
202     ec=`get_devno $host echo_client $client_name`
203     if [ -n "$ec" ]; then
204         echo $ec $client_name 0
205         return
206     fi
207     if [ -z "$ost_name" ]; then
208         echo "no echo client and ost_name not set" 1>&2
209         return
210     fi
211     ost=`get_devno $host obdfilter $ost_name`
212     if [ -z "$ost" ]; then
213         echo "OST $ost_name not setup" 1>&2
214         return
215     fi
216     remote_shell $host "$lctl <<EOF
217         attach echo_client $client_name ${client_name}_UUID
218         setup $ost_name
219 EOF"
220     ec=`get_devno $host echo_client $client_name`
221     if [ -z "$ec" ]; then
222         echo "Can't setup echo client" 1>&2
223         return
224     fi
225     echo $ec $client_name 1
226 }
227
228 teardown_ec_devno () {
229     local host=$1
230     local client_name=$2
231     remote_shell $host "$lctl <<EOF
232         cfg $client_name
233         cleanup
234         detach
235 EOF"
236 }
237
238 create_objects () {
239     # create a set of objects, check there are 'n' contiguous ones and
240     # return the first or 'ERROR'
241     local host=$1
242     local devno=$2
243     local nobj=$3
244     local rfile=$4
245     remote_shell $host $lctl --device $devno create $nobj > $rfile 2>&1
246     first=0
247     prev=0
248     count=0
249     error=0
250     while read line; do
251         echo "$line" | grep -q 'is object id'
252         if [ $?  -ne 0 ]; then
253             continue
254         fi
255         if [ $first -eq 0 ]; then
256             first=$(echo $line | awk '{print $6}')
257             first=$(printf "%d" $first)
258             prev=$first
259             count=1
260         else
261             obj=$(echo $line | awk '{print $6}') 
262             obj=$(printf "%d" $obj) 
263             diff=$((obj - (prev+1))) 
264             if [ $diff -ne 0 ]; then 
265                 error=1 
266             fi 
267             prev=$obj 
268             count=$((count+1)) 
269         fi 
270     done < $rfile 
271     if [ $nobj -ne $count ]; then 
272         echo "ERROR: $nobj != $count" >&2 
273         cat $rfile >&2 
274         echo "ERROR" 
275     elif [ $error -ne 0 ]; then 
276         echo "ERROR: non contiguous objs found" >&2 
277         echo "ERROR" 
278     else 
279         echo $first 
280     fi
281 }
282
283 destroy_objects () {
284     local host=$1
285     local devno=$2
286     local obj0=$3
287     local nobj=$4
288     local rfile=$5
289     remote_shell $host $lctl --device $devno destroy $obj0 $nobj > $rfile 2>&1
290 }
291
292 get_stats () {
293     local rfile=$1
294     awk < $rfile \
295         '/^Selected device [0-9]+$/ {n = 0; next}\
296         /error/ {n = -1; exit}\
297         /^[0-9]+\/[0-9]+ Total: [0-9]+\.[0-9]+\/second$/ {n++; v=strtonum($3); \
298                                                           if (n == 1 || v < min) min = v;\
299                                                           if (n == 1 || v > max) max = v;\
300                                                           next}\
301         {if (n != 0) {n = -1; exit}}\
302         END {printf "%d %f %f\n", n, min, max}'
303 }
304
305 get_global_stats () {
306     local rfile=$1
307     awk < $rfile 'BEGIN {n = 0;}\
308                   {n++; if (n == 1) {err = $1; min = $2; max = $3} else\
309                                     {if ($1 < err) err = $1;\
310                                      if ($2 < min) min = $2;\
311                                      if ($3 > max) max = $3}}\
312                   END {if (n == 0) err = 0;\
313                        printf "%d %f %f\n", err, min, max}'
314 }
315
316 testname2type () {
317     # 'x' disables data check
318     if ((verify)); then
319         x=""
320     else
321         x="x"
322     fi
323     case $1 in
324         *write*)  echo "w$x";;
325         *)        echo "r$x";;
326     esac
327 }
328
329 print_summary () {
330     if [ "$1" = "-n" ]; then
331         minusn=$1; shift
332     else
333         minusn=""
334     fi
335     echo $minusn "$*" >> $rsltf
336     echo $minusn "$*"
337 }
338
339 unique () {
340     echo "$@" | xargs -n1 echo | sort -u
341 }
342
343 split_hostname () {
344     local name=$1
345     case $name in
346     *:*) host=`echo $name | sed 's/:.*$//'`
347          name=`echo $name | sed 's/[^:]*://'`
348          ;;
349     *)   host=localhost
350          ;;
351     esac
352     echo "$host $name"
353 }
354
355 # split out hostnames from client/ost names
356 ndevs=${#client_names[@]}
357 if ((ndevs != 0)); then
358     if ((${#ost_names[@]} != 0)); then
359         echo "Please specify client_names or ost_names, but not both" 1>&2
360         exit 1
361     fi
362     for ((i=0; i<ndevs;i++)); do
363         str=(`split_hostname ${client_names[$i]}`)
364         host_names[$i]=${str[0]}
365         client_names[$i]=${str[1]}
366     done
367 else
368     ndevs=${#ost_names[@]}
369     if ((ndevs == 0)); then
370         echo "Please specify either client_names or ost_names" 1>&2
371         exit 1
372     fi
373     for ((i=0; i<ndevs;i++)); do
374         str=(`split_hostname ${ost_names[$i]}`)
375         host_names[$i]=${str[0]}
376         ost_names[$i]=${str[1]}
377     done
378 fi
379
380 # get vmstat started
381 # disable portals debug and get obdecho loaded on all relevant hosts
382 unique_hosts=(`unique ${host_names[@]}`)
383 pidcount=0
384 for host in ${unique_hosts[@]}; do
385     remote_shell $host "echo 0 > /proc/sys/portals/debug"
386     host_vmstatf=${vmstatf}_${host}
387     echo -n > $host_vmstatf
388     remote_shell $host "vmstat 5 >> $host_vmstatf" &
389     pid=$!
390     vmstatpids[$pidcount]=$pid
391     pidcount=$((pidcount+1))
392     do_unload_obdecho[$host]=0
393     if obdecho_loaded $host; then
394         continue
395     fi
396     load_obdecho $host
397     if obdecho_loaded $host; then
398         do_unload_obdecho[$host]=1
399         continue
400         fi
401     echo "Can't load obdecho on $host" 1>&2
402     exit 1
403 done
404
405 # get all the echo_client device numbers and names
406 for ((i=0; i<ndevs; i++)); do
407     host=${host_names[$i]}
408     devno=(`get_ec_devno $host "${client_names[$i]}" "${ost_names[$i]}"`)
409     if ((${#devno[@]} != 3)); then
410         exit 1
411     fi
412     devnos[$i]=${devno[0]}
413     client_names[$i]=${devno[1]}
414     do_teardown_ec[$i]=${devno[2]}
415 done
416
417 for ((rsz=$rszlo;rsz<=$rszhi;rsz*=2)); do
418     for ((nobj=$nobjlo;nobj<=$nobjhi;nobj*=2)); do 
419         for ((thr=$thrlo;thr<=$thrhi;thr*=2)); do
420             if ((thr < nobj)); then
421                 continue
422             fi
423             # restart?
424             if [ -n "$restart_rsz" -a\
425                  -n "$restart_nobj" -a\
426                  -n "$restart_thr" ]; then
427                 if ((rsz < restart_rsz ||\
428                      (rsz == restart_rsz &&\
429                       (nobj < restart_nobj ||\
430                        (nobj == restart_nobj &&\
431                         thr < restart_thr))))); then
432                     continue;
433                 fi
434             fi
435             # compute parameters
436             total_thr=$((ndevs*thr))
437             total_nobj=$((ndevs*nobj))
438             pages=$((rsz/PAGE_SIZE))
439             actual_rsz=$((pages*PAGE_SIZE))
440             count=$((size*1024/(actual_rsz*thr)))
441             actual_size=$((actual_rsz*count*thr))
442             total_size=$((actual_size*ndevs))
443             # show computed parameters
444             str=`printf 'ost %2d sz %8dK rsz %4d obj %4d thr %4d ' \
445                      $ndevs $total_size $actual_rsz $total_nobj $total_thr`
446             echo "=======================> $str" >> $workf
447             print_summary -n "$str"
448             if ((total_thr * actual_rsz > max_buffer_mem)); then
449                 print_summary "Too much buffer space"
450                 continue
451             fi
452             # create the objects
453             tmpf="${workf}_tmp"
454             for ((idx=0; idx < ndevs; idx++)); do
455                 host=${host_names[$idx]}
456                 devno=${devnos[$idx]}
457                 client_name="${host}:${client_names[$idx]}"
458                 echo "=============> Create $nobj on $client_name" >> $workf
459                 first_obj=`create_objects $host $devno $nobj $tmpf`
460                 cat $tmpf >> $workf
461                 rm $tmpf
462                 if [ $first_obj = "ERROR" ]; then
463                     print_summary "created object #s on $client_name not contiguous"
464                     exit 1
465                 fi
466                 first_objs[$idx]=$first_obj
467             done
468             # run tests
469             for test in ${tests[@]}; do
470                 declare -a pidarray
471                 for host in ${unique_hosts[@]}; do
472                     echo "starting run for test: $test rsz: $rsz threads: $thr objects: $nobj" >> ${vmstatf}_${host}
473                 done
474                 print_summary -n "$test "
475                 # create per-host script files
476                 for host in ${unique_hosts[@]}; do
477                     echo -n > ${cmdsf}_${host}
478                 done
479                 for ((idx=0; idx < ndevs; idx++)); do
480                     host=${host_names[$idx]}
481                     devno=${devnos[$idx]}
482                     tmpfi="${tmpf}_$idx"
483                     first_obj=${first_objs[$idx]}
484                     echo >> ${cmdsf}_${host} \
485                         "$lctl > $tmpfi 2>&1 \\
486                          --threads $thr -$snap $devno \\
487                          test_brw $count `testname2type $test` q $pages ${thr}t${first_obj} &"
488                 done
489                 pidcount=0
490                 for host in ${unique_hosts[@]}; do
491                     echo "wait" >> ${cmdsf}_${host}
492                     pidarray[$pidcount]=0
493                     pidcount=$((pidcount+1))
494                 done
495                 # timed run of all the per-host script files
496                 t0=`date +%s.%N`
497                 pidcount=0
498                 for host in ${unique_hosts[@]}; do
499                     remote_shell $host bash ${cmdsf}_${host} &
500                     pidarray[$pidcount]=$!
501                     pidcount=$((pidcount+1))
502                 done
503                 pidcount=0
504                 for host in ${unique_hosts[@]}; do
505                     wait ${pidarray[$pidcount]}
506                     pidcount=$((pidcount+1))
507                 done
508                 #wait
509                 t1=`date +%s.%N`
510                 # clean up per-host script files
511                 for host in ${unique_hosts[@]}; do
512                     rm ${cmdsf}_${host}
513                 done
514                 # compute bandwidth from total data / elapsed time
515                 str=`awk "BEGIN {printf \"%7.2f \",\
516                          $total_size / (( $t1 - $t0 ) * 1024)}"`
517                 print_summary -n "$str"
518                 # collect/check individual OST stats
519                 echo -n > $tmpf
520                 for ((idx=0; idx < ndevs; idx++)); do
521                     client_name="${host_names[$idx]}:${client_names[$idx]}"
522                     tmpfi="${tmpf}_$idx"
523                     echo "=============> $test $client_name" >> $workf
524                     cat $tmpfi >> $workf
525                     get_stats $tmpfi >> $tmpf
526                     rm $tmpfi
527                 done
528                 # compute/display global min/max stats
529                 echo "=============> $test global" >> $workf
530                 cat $tmpf >> $workf
531                 stats=(`get_global_stats $tmpf`)
532                 rm $tmpf
533                 if ((stats[0] <= 0)); then
534                     if ((stats[0] < 0)); then
535                         str=`printf "%17s " ERROR`
536                     else
537                         str=`printf "%17s " SHORT`
538                     fi
539                 else
540                     str=`awk "BEGIN {printf \"[%7.2f,%7.2f] \",\
541                              (${stats[1]} * $actual_rsz)/1024,\
542                              (${stats[2]} * $actual_rsz)/1024; exit}"`
543                 fi
544                 print_summary -n "$str"
545             done
546             print_summary ""
547             # destroy objects we created
548             for ((idx=0; idx < ndevs; idx++)); do
549                 host=${host_names[$idx]}
550                 devno=${devnos[$idx]}
551                 client_name="${host}:${client_names[$idx]}"
552                 first_obj=${first_objs[$idx]}
553                 echo "=============> Destroy $nobj on $client_name" >> $workf
554                 destroy_objects $host $devno $first_obj $nobj $tmpf
555                 cat $tmpf >> $workf
556                 rm $tmpf
557             done
558         done
559     done
560 done
561
562 # tear down any echo clients we created
563 for ((i=0; i<ndevs; i++)); do
564     host=${host_names[$i]}
565     if ((${do_teardown_ec[$i]})); then
566         teardown_ec_devno $host ${client_names[$i]}
567     fi
568 done
569
570 # unload any obdecho modules we loaded
571 pidcount=0
572 for host in ${unique_hosts[@]}; do
573     remote_shell $host "killall vmstat" &
574     pid=$!
575     kill -term ${vmstatpids[$pidcount]}
576     kill -kill ${vmstatpids[$pidcount]} 2>/dev/null
577     wait $pid
578     pidcount=$((pidcount+1))
579     if ((${do_unload_obdecho[$host]})); then
580         unload_obdecho $host
581     fi
582 done
583
584 exit 0