Whamcloud - gitweb
b=10959
[fs/lustre-release.git] / lustre-iokit / ost-survey / ost-survey.pl
1 #!/usr/bin/perl
2 # This script is to be run on a client machine and will test all the
3 # OSTs to determine which is the fastest and slowest
4 # The current test method is as follows:
5 #   -Create a directory for each OST
6 #   -Use 'lfs setstripe' to set the Lustre striping such that IO goes to
7 #     only one OST
8 #   -Use 'dd' to write and read a file of a specified size
9 #   -Compute the average, and Standard deviation 
10 #   -Find the slowest OST for read and write
11 #   -Find the Fastest OST for read and write
12
13 # GLOBALS
14 $pname = $0;                     # to hold program name
15 $OSTS = 0;                       # Number of OSTS we will loop over
16 $BSIZE = 1024 * 1024;            # Size of i/o block
17 $MNT = "/mnt/lustre";            # Location of Lustre file system
18 $FSIZE = 30;                     # Number of i/o blocks
19
20 # Usage
21 sub usage () {
22         print "Usage: $pname [-s <size>] [-h] <Lustre_Path>\n";
23         print "[OPTIONS]\n";
24         print "  -s: size of test file in MB (default $FSIZE MB)\n";
25         print "  -h: To display this help\n";
26         print "example : $pname /mnt/lustre\n";
27         exit 1;
28 }
29
30 # ost_count subroutine ets globle variable $OST with Number of OST's
31 # Also fills 1 for active OST indexes in ACTIVEOST_INX array.
32 sub ost_count () {
33         # numobd gives number of ost's and activeobd gives number of active ost's
34         my $tempfile = glob ("/proc/fs/lustre/lov/*-clilov-*/activeobd"); 
35         open(PTR, $tempfile) || die "Cannot open $tempfile: $!\n";    
36         $OSTS = <PTR>;
37         close PTR;
38         print "Number of Active OST devices : $OSTS";
39         my $tempfile = glob ("/proc/fs/lustre/lov/*-clilov-*/numobd"); 
40         open(PTR, $tempfile) || die "Cannot open $tempfile: $!\n";    
41         $numost = <PTR>;
42         close PTR;
43         if ( $numost != $OSTS ) {
44                 printf "Number of non active ots(s): %d\n", ( $numost - $OSTS );
45                 $OSTS = $numost;
46         }
47         my $tempfile = glob ("/proc/fs/lustre/lov/*-clilov-*/target_obd");
48         open(PTR, $tempfile) || die "Cannot open $tempfile: $!\n";
49         my $count = 0;
50         my $temp;
51         while (<PTR>) {
52                 chop;
53                 my ($ost_num, $ost_name, $ost_status) = split(/\s+/, $_);
54                 if ( $ost_status eq "ACTIVE" ) {
55                         $ACTIVEOST_INX[$count] = 1;
56                 }
57                 $count++;
58         }
59 }
60
61 # make_dummy subroutine creates a dummy file that will be used for read operation.
62 sub make_dummy () {
63         my $SIZE = $_[0];
64         my $tempfile = $_[1];
65         system ("dd of=$tempfile if=/dev/zero count=$SIZE bs=$BSIZE 2> /dev/null");
66 }
67
68 my $LoadTimeHiRes = "use Time::HiRes qw(gettimeofday)";
69 eval ($LoadTimeHiRes);
70
71 # run_test subroutine actually writes and reads data to/from dummy file
72 # and compute corresponding time taken for read and write operation and 
73 # byte transfer for the both operations.
74 # This subroutine also fill corresponding globle arrays with above information.
75 sub run_test () {
76         my $SIZE = $_[0];
77         my $INX=$_[1];
78         my $ACTION=$_[2];
79         my $tempfile = $_[3];
80
81         if ( !(-f $tempfile) && $ACTION eq "read" ) {
82                 &make_dummy($SIZE, $tempfile);
83         }
84         system("sync");
85         my ($ts0, $tu0) = gettimeofday();
86         $tu0 = $ts0 + ($tu0 / 1000000);
87         if ( $ACTION eq "write" ) {
88                 system("dd of=$tempfile if=/dev/zero count=$SIZE bs=$BSIZE 2> /dev/null");
89         } elsif ( $ACTION eq "read" ) {
90                 system("dd if=$tempfile of=/dev/null count=$SIZE bs=$BSIZE 2> /dev/null");
91         } else {
92                 print "Action is neither read nor write\n";
93                 exit 1;
94         }
95         system("sync");
96         my ($ts1, $tu1) = gettimeofday();
97         $tu1 = $ts1 + ($tu1/1000000);
98         my $tdelta = $tu1 - $tu0;
99         my $delta = ($SIZE * $BSIZE / ( $tu1 - $tu0 )) / (1024 * 1024);
100         if ( $ACTION eq "write" ) {
101                 $wTime[$INX] = $tdelta;
102                 $wMBs[$INX] = $delta;
103         } else {
104                 $rTime[$INX] = $tdelta;
105                 $rMBs[$INX] = $delta;
106         }
107 }
108
109 # calculate subroutine compute following things and displays them.
110 #  - Finds worst and best OST for both read and write operations.
111 #  - Compute average of read and write rate from all OSTS
112 #  - Compute Standard deviation for read and write form all OST's
113 sub calculate () {
114         my ($op, $MBs);
115         $op = $_[0];
116         @MBs = @_[1..$#_]; 
117         my $count = 0;
118         my $total = 0;
119         my $avg = 0;
120         my $sd = 0;
121         my $best_OST = 0;
122         my $worst_OST = 0;
123         my $max_mb = 0;
124         my $min_mb = 999999999;
125         while ($count < $OSTS ) {
126                 if ( $ACTIVEOST_INX[$count] ) {
127                         $total = $total + $MBs[$count];
128                         if ($max_mb < $MBs[$count] ) {
129                                 $max_mb = $MBs[$count];
130                                 $best_OST = $count; 
131                         }
132                         if ($min_mb > $MBs[$count] ) {
133                                 $min_mb = $MBs[$count];
134                                 $worst_OST = $count; 
135                         }
136                 }
137                 $count++;
138         }
139         $avg = $total/$OSTS;
140         $total = 0;
141         $count = 0;
142         while ($count < $OSTS ) {
143                 if ( $ACTIVEOST_INX[$count] ) {
144                         $total = $total + ($MBs[$count] - $avg) * ($MBs[$count] - $avg);
145                 }
146                 $count++;
147         }
148         $sd = sqrt($total/$OSTS);
149         printf "Worst  %s OST indx: %d speed: %f\n", $op, $worst_OST, $min_mb;
150         printf "Best   %s OST indx: %d speed: %f\n", $op, $best_OST, $max_mb;
151         printf "%s Average: %f +/- %f MB/s\n", $op, $avg, $sd;
152 }
153
154 # output_all_data subroutine displays speed and time information 
155 # for all OST's for both read and write operations.
156 sub output_all_data () {
157         my $count = 0;
158         print "Ost#  Read(MB/s)  Write(MB/s)  Read-time  Write-time\n";
159         print "----------------------------------------------------\n";
160         while ( $count < $OSTS ) {
161                 if ( $ACTIVEOST_INX[$count] ) { 
162                         printf "%d     %.3f       %.3f        %.3f      %.3f\n",$count, 
163                         $rMBs[$count], $wMBs[$count], $rTime[$count], $wTime[$count];
164                 } else {
165                         printf "%d     Inactive ost\n",$count; 
166                 }
167                 $count = $count + 1;
168         }
169 }
170
171 @rTime = ();
172 @wTime = ();
173 @rMBs = ();
174 @wMBs = ();
175 @ACTIVEOST_INX;
176
177 # Locals
178 my $filename = "";
179 my $dirpath = "";
180 my $flag = 0;
181
182 # Command line parameter parsing
183 use Getopt::Std;
184 getopts('s:h') or usage();
185 usage() if $opt_h;
186 $FSIZE = $opt_s if $opt_s;
187
188 my $i = 0;
189 foreach (@ARGV) {
190         $MNT = $_;
191         $i++;
192         if ($i > 1) {
193                 print "ERROR: extra argument $_\n";
194                 usage();
195         }       
196 }
197 #Check for Time::HiRes module 
198 my $CheckTimeHiRes = "require Time::HiRes";
199 eval ($CheckTimeHiRes) or die "You need to install the perl-Time-HiRes package to use this script\n";
200
201 use POSIX qw(strftime);
202 my $time_v = time();
203 my $hostname = `lctl list_nids | head -1` or die "You need to install lctl to use this script\n";
204 chop($hostname);
205 print "$pname: ", strftime("%D", localtime($time_v));
206 print " OST speed survey on $MNT from $hostname\n";
207
208 # get OST count
209 ost_count ();
210
211 use File::Path;
212 $CNT = 0;
213 while ($CNT < $OSTS) {
214         $dirpath = "$MNT/tmpdir$CNT";
215         eval { mkpath($dirpath) };
216         if ($@) {
217                 print "Couldn't create $dirpath: $@";
218                 exit 1;
219         }
220         $filename = "$dirpath/file$CNT";
221         if ( $ACTIVEOST_INX[$CNT] ) {
222                 # set stripe for OST number $CNT
223                 system ("lfs setstripe $filename 0 $CNT 1");
224                 # Perform write for OST number $CNT
225                 &run_test($FSIZE,$CNT,"write",$filename);
226                 # Perform read for OST number $CNT
227                 &run_test($FSIZE,$CNT,"read",$filename);
228                 $flag++;
229         }
230         eval { rmtree($dirpath) };
231         if ($@) {
232                 print "Warning: Couldn't  $dirpath: $@";
233         }
234         $CNT = $CNT + 1;
235 }
236 # if read or write performed on any OST then display information. 
237 if ( $flag ) {
238         if ( $flag > 1 ) {
239                 &calculate("Read",@rMBs);
240                 &calculate("Write",@wMBs);
241         }
242         output_all_data ();
243 } else {
244         print "There is no active OST's found\n";
245 }