Whamcloud - gitweb
LU-2730 mdt: fix erroneous LASSERT in mdt_reint_opcode
[fs/lustre-release.git] / lustre-iokit / sgpdd-survey / plot-sgpdd
1 #!/usr/bin/perl -w
2 # Report generation for plot-sgpdd
3 # ================================
4 #        The plot-sgpdd script is used to generate csv file and
5 # instructions files for gnuplot from the output of sgpdd-survey script.
6 #
7 #        The plot-sgpdd also creates .scr file that contains instructions
8 # for gnuplot to plot the graph. After generating .dat and .scr files this
9 # script invokes gnuplot to display graph.
10 #
11 # Syntax:
12 # $ sgpdd-survey > log_filename
13 # $ plot-sgpdd <log_filename>
14 # [Note: 1. This script may need modifications whenever there will be
15 #           modifications in output format of sgpdd-survey script.
16 #        2. Gnuplot version 4.0 or above is required.]
17
18 sub usage() 
19 {
20         print STDERR "Usage: $0 [options] <log_filename>\n";
21         print STDERR "  $0 parses and plots graphs from the output of sgpdd-survey\n";
22         print STDERR "  It generates text data files (.dat) and graphs (.png) using gnuplot.\n";
23         print STDERR "options:\n";
24         print STDERR "  --rt: Subtitle for read graphs\n";
25         print STDERR "  --wt: Subtitle for write graphs\n";
26         print STDERR "  --y: Y-axis scale\n";
27         print STDERR "e.g. # $0 --rt=\"no prefetch\" --wt=\"WB disabled\" --y=500 sgpdd.summary\n";
28         exit 1;
29 }
30
31 # check whether gnuplot exists?
32 system ("which gnuplot > /dev/null") == 0 or die "gnuplot does not exist, please install it and try again.\n";
33
34 # check whether gnuplot supports png
35 $pngsupport = "ldd `which gnuplot` | grep -q libpng";
36 system ("$pngsupport") == 0 or die "gnuplot installed does not support png.
37         Please install gnuplot to support png and try again.\n"; 
38
39 my @GraphTitle;
40
41 #Subroutine to write .scr file that further used by gnuplot to plot the graph.
42 sub write_scr_file() {
43         my $op = $_[0];
44         print "generating plot $file-$rsz-$op.png\n";
45         open ( SCRFILE, "> $file-$rsz-$op.scr" ) or die "Can't open scr file for writing";
46         if ($op eq "rd") {
47                 $rwlabel = "Read";
48         }
49         if ($op eq "wr") {
50                 $rwlabel = "Write";
51         }
52         
53         if ($opt_rdtitle || $opt_wrtitle) {
54                 if ($op eq "rd") {
55                         print SCRFILE "set title \"@GraphTitle\\n$rwlabel, Rsize = $rsz KBytes, $opt_rdtitle\"\n";
56                 }
57                 if ($op eq "wr") {
58                         print SCRFILE "set title \"@GraphTitle\\n$rwlabel, Rsize = $rsz KBytes, $opt_wrtitle\"\n";
59                 }
60         } else {
61                 print SCRFILE "set title \"@GraphTitle\\n$rwlabel, Rsize = $rsz KBytes\"\n";
62         }
63         print SCRFILE "set xlabel \"Threads\"\n";
64         print SCRFILE "set ylabel \"Speeds(MB/s)\"\n";
65         print SCRFILE "set logscale x\n";
66         print SCRFILE "set grid\n";
67         print SCRFILE "set terminal png\n";
68         print SCRFILE "set output \"/dev/null\"\n";
69         if ($opt_y != 0) {
70                 print SCRFILE "set yrange [ 0:$opt_y ]\n";
71         } else {
72                 print SCRFILE "set yrange [ 0: ]\n";
73         }
74
75         my $plot = "plot";
76         $i = 2;
77         my @numrgs = split " ", $regions;
78         $xrange = 1;
79         # generate instructions for gnuplot, with adjusting X-axes ranges
80         
81         foreach my $j (sort numerically split " ", $threads) {
82                 if ($op eq "wr") {
83                         $using = ( $i < $#numrgs ) ? $i : $#numrgs;
84                         printf SCRFILE "$plot \"$file-$rsz-$op.dat\" using 1:$using axes x%dy1 title \"write-obj$j\" with line\n", $xrange;
85                 }
86                 if ($op eq "rd") {
87                         $using = ( $i < $#numrgs ) ? $i : $#numrgs;
88                         printf SCRFILE "$plot \"$file-$rsz-$op.dat\" using 1:$using axes x%dy1 title \"read-obj$j\" with line\n", $xrange;
89                 }
90                 $i++;
91                 $plot = "replot";
92         }
93         print SCRFILE "set output \"$file-$rsz-$op.png\"\n";
94         print SCRFILE "replot\n";
95         close SCRFILE;
96         # invoke gnuplot to display graph.
97         system ("gnuplot $file-$rsz-$op.scr") == 0 or die "ERROR: while ploting graph";
98         system ("rm $file-$rsz-$op.scr");
99 }
100
101 sub check_data_file () {
102         my $file=shift;
103         my @values;
104         my @tmp;
105
106         open ( FILE, "< $file" ) or die "Can't open $file for reading";
107         while ( <FILE> ) {
108                 @tmp = split;
109                 push @values, [ @tmp ];
110         }
111         close FILE;
112
113         for ( $j = 0; $j <= $#tmp; $j++) {
114                 my $sum=0;
115                 for ($i = 2; $i <= $#values ; $i ++) {
116                         $values [$i][$j] =~ "-" or $sum = $sum + $values [$i][$j];
117                 }
118                 die "File: $file : $j column contains no data.\n" unless $sum != 0;
119         }
120 }
121
122 sub numerically { $a <=> $b; }
123
124 #Subroutine to write .dat file that further used by gnuplot to plot the graph.
125 sub write_dat_file() {
126         my $op = $_[0];
127         print "writing data $file-$rsz-$op.dat\n";
128         # Open .csv/.dat file for writing required columns from log file.
129         my $datafile = "$file-$rsz-$op.dat";
130         open ( DATAFILE, "> $datafile" ) or die "Can't open csv $datafile for writing";
131         printf DATAFILE "%-6s", "0";
132         
133         foreach my $j (sort numerically split " ", $regions) {
134                 printf DATAFILE "%-8s", "$op$j";
135         }
136
137         # threads, line [7], strings
138         foreach my $i (sort numerically split " ", $threads) {
139                 printf DATAFILE "\n%-6s", $i;
140
141                 # regions, line [5], column
142                 foreach my $j (sort numerically split " ", $regions) {
143                         if (($op eq "rd" && $rdwr) || ($op eq "wr" && $wrrd) || ($readop) || ($writeop)) {
144                                 if ( $out{$i}{$j} ) {
145                                         printf DATAFILE "%-8s", $out{$i}{$j};
146                                 } else {
147                                         printf DATAFILE "%-8s", "-";
148                                 }
149                         } else {
150                                 if (($j <= 1 && $out{$i}{$j - 1})) {
151                                         printf DATAFILE "%-8s", $out{$i}{$j - 1};
152                                 }elsif ($out{$i}{$j + 1} && $j > 1) {
153                                         printf DATAFILE "%-8s", $out{$i}{$j + 1};
154                                 } else {
155                                         printf DATAFILE "%-8s", "-";
156                                 }
157                         }
158                 }
159         }
160         close DATAFILE;
161         &check_data_file ( $datafile );         
162 }
163
164 if ( !$ARGV[0] ) {
165         usage();
166 }
167 $regions = "";
168 $threads = "";
169 $count = 0;
170 $wrrd = 0;
171 $rdwr = 0;
172 $writeop = 0;
173 $readop = 0;
174 $rsz = 0;
175 $opt_rdtitle = "";
176 $opt_wrtitle = "";
177 $opt_y = 0;
178 # Command line parameter parsing
179 use Getopt::Long;
180 GetOptions ('help' => \$opt_help, 'rt=s' => \$opt_rdtitle, 'wt=s' => \$opt_wrtitle, 'y=i' => \$opt_y) or usage(); 
181 if ($opt_help) {
182         usage();
183 }
184 $file = $ARGV[0];
185
186 open ( PFILE, "$file") or die "Can't open $file";
187 LABEL: while ( <PFILE> ) {
188         chomp;
189         @line = split( /\s+/ );
190         if ($line[27] && $count != 0) {
191                 print "invalid file format\n";
192                 exit 1; 
193         } 
194         if ($count == 0) {
195                 @GraphTitle = @line;
196                 $count++;
197                 next LABEL;
198         }
199         if ($line[8]) {
200                 if ($line[8] eq "ENOMEM") {
201                         next LABEL;
202                 }
203         }
204         if (!$rsz && $line[3]) {
205                 $rsz = $line[3];
206         }
207         if ($rsz != $line[3]) {
208                 if($readop) {
209                         &write_dat_file("rd");
210                         &write_scr_file("rd");
211                 }
212                 if($writeop) {
213                         &write_dat_file("wr");
214                         &write_scr_file("wr");
215                 }
216                 if ($wrrd || $rdwr) {
217                         &write_dat_file("rd");
218                         &write_scr_file("rd");
219                         &write_dat_file("wr");
220                         &write_scr_file("wr");
221                 }
222                 $rsz = $line[3];                
223                 $regions = "";
224                 $threads = "";
225         }
226         #print "rg$line[5] th$line[7] w$line[9] r$line[$rindex]\n";
227         $rindex = 18;
228         if ($line[18]) {
229                 if ($line[10] eq "failed") {
230                         $rindex = 12;
231                 }
232                 if ($line[8] eq "write" && $line[17] eq "read") {
233                         $wrrd = 1;
234                 }
235                 if ($line[8] eq "read" && $line[17] eq "write") {
236                         $rdwr = 1;
237                 }
238         } else {
239                 if ($line[8] eq "write" && $line[9]) {
240                         $writeop = 1;
241                 }
242                 if ($line[8] eq "read" && $line[9]) {
243                         $readop = 1;
244                 }
245                 
246         }
247         if ($wrrd || $rdwr) {
248                 $out{$line[7]}{$line[5]} = $line[9];
249                 if ($line[$rindex+1]) {
250                         if (!($line[$rindex+1] eq "failed")) {  
251                                 goto LABEL2;    
252                         }
253                 } else {
254 LABEL2:                 if ($line[5] <= 1 ) {
255                                 $out{$line[7]}{$line[5] - 1} = $line[$rindex];
256                         } else {
257                                 $out{$line[7]}{$line[5] + 1} = $line[$rindex];
258                         }
259                 }
260         }
261         if ($writeop) {
262                 $out{$line[7]}{$line[5]} = $line[9];    
263         }
264         if ($readop) {
265                 $out{$line[7]}{$line[5]} = $line[9];
266         }
267         $regions .= " $line[5]" unless $regions =~ $line[5];
268         $threads .= " $line[7]" unless $threads =~ $line[7];
269         $count++;
270 }
271 close PFILE;
272 if ($count > 1 && $rsz) {
273         if($readop) {
274                 &write_dat_file("rd");
275                 &write_scr_file("rd");
276         }
277         if($writeop) {
278                 &write_dat_file("wr");
279                 &write_scr_file("wr");
280         }
281         if ($wrrd || $rdwr) {
282                 &write_dat_file("rd");
283                 &write_scr_file("rd");
284                 &write_dat_file("wr");
285                 &write_scr_file("wr");
286         }
287 } else {
288         print "Invalid log file format\n";
289 }