Whamcloud - gitweb
*** empty log message ***
[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 my @GraphTitle;
35
36 #Subroutine to write .scr file that further used by gnuplot to plot the graph.
37 sub write_scr_file() {
38         my $op = $_[0];
39         print "generating plot $file-$rsz-$op.png\n";
40         open ( SCRFILE, "> $file-$rsz-$op.scr" ) or die "Can't open scr file for writing";
41         if ($op eq "rd") {
42                 $rwlabel = "Read";
43         }
44         if ($op eq "wr") {
45                 $rwlabel = "Write";
46         }
47         
48         if ($opt_rdtitle || $opt_wrtitle) {
49                 if ($op eq "rd") {
50                         print SCRFILE "set title \"@GraphTitle\\n$rwlabel, Rsize = $rsz KBytes, $opt_rdtitle\"\n";
51                 }
52                 if ($op eq "wr") {
53                         print SCRFILE "set title \"@GraphTitle\\n$rwlabel, Rsize = $rsz KBytes, $opt_wrtitle\"\n";
54                 }
55         } else {
56                 print SCRFILE "set title \"@GraphTitle\\n$rwlabel, Rsize = $rsz KBytes\"\n";
57         }
58         print SCRFILE "set xlabel \"Threads\"\n";
59         print SCRFILE "set ylabel \"Speeds(MB/s)\"\n";
60         print SCRFILE "set logscale x\n";
61         print SCRFILE "set grid\n";
62         print SCRFILE "set terminal png\n";
63         print SCRFILE "set output \"/dev/null\"\n";
64         if ($opt_y != 0) {
65                 print SCRFILE "set yrange [ 0:$opt_y ]\n";
66         } else {
67                 print SCRFILE "set yrange [ 0: ]\n";
68         }
69
70         my $plot = "plot";
71         $i = 2;
72         $xrange = 1;
73         # generate instructions for gnuplot, with adjusting X-axes ranges
74         for ($j = 1; $j <= $thread ; $j = $j + $j) {
75                 if ($op eq "wr") {
76                         printf SCRFILE "$plot \"$file-$rsz-$op.dat\" using 1:$i axes x%dy1 title \"write-obj$j\" with line\n", $xrange;
77                 }
78                 if ($op eq "rd") {
79                         printf SCRFILE "$plot \"$file-$rsz-$op.dat\" using 1:$i axes x%dy1 title \"read-obj$j\" with line\n", $xrange;
80                 }
81                 $i++;
82                 $plot = "replot";
83         }
84         print SCRFILE "set output \"$file-$rsz-$op.png\"\n";
85         print SCRFILE "replot\n";
86         close SCRFILE;
87         # invoke gnuplot to display graph.
88         system ("gnuplot $file-$rsz-$op.scr") == 0 or die "ERROR: while ploting graph";
89         system ("rm $file-$rsz-$op.scr");
90 }
91
92 #Subroutine to write .dat file that further used by gnuplot to plot the graph.
93 sub write_dat_file() {
94         my $op = $_[0];
95         print "writing data $file-$rsz-$op.dat\n";
96         # Open .csv/.dat file for writing required columns from log file.
97         open ( DATAFILE, "> $file-$rsz-$op.dat" ) or die "Can't open csv file for writing";
98         printf DATAFILE "%-6s", "0";
99         for ($j = 1; $j <= $thread ; $j = $j + $j) {
100                         printf DATAFILE "%-8s", "$op$j";
101         }
102         for ( $i = 1; $i <= $region; $i = $i + $i ) {
103                 printf DATAFILE "\n%-6s", $i;
104                 for ($j = 1; $j <= $thread ; $j = $j + $j) {
105                         if (($op eq "rd" && $rdwr) || ($op eq "wr" && $wrrd) || ($readop) || ($writeop)) {
106                                 if ( $out{$i}{$j} ) {
107                                         printf DATAFILE "%-8s", $out{$i}{$j};
108                                 } else {
109                                         printf DATAFILE "%-8s", "-";
110                                 }
111                         } else {
112                                 if (($j <= 1 && $out{$i}{$j - 1})) {
113                                         printf DATAFILE "%-8s", $out{$i}{$j - 1};
114                                 }elsif ($out{$i}{$j + 1} && $j > 1) {
115                                         printf DATAFILE "%-8s", $out{$i}{$j + 1};
116                                 } else {
117                                         printf DATAFILE "%-8s", "-";
118                                 }
119                         }
120                 }
121         }
122         close DATAFILE;
123 }
124
125 if ( !$ARGV[0] ) {
126         usage();
127 }
128 $region = 0;
129 $thread = 0;
130 $count = 0;
131 $wrrd = 0;
132 $rdwr = 0;
133 $writeop = 0;
134 $readop = 0;
135 $rsz = 0;
136 $opt_rdtitle = "";
137 $opt_wrtitle = "";
138 $opt_y = 0;
139 # Command line parameter parsing
140 use Getopt::Long;
141 GetOptions ('help' => \$opt_help, 'rt=s' => \$opt_rdtitle, 'wt=s' => \$opt_wrtitle, 'y=i' => \$opt_y) or usage(); 
142 if ($opt_help) {
143         usage();
144 }
145 $file = $ARGV[0];
146
147 open ( PFILE, "$file") or die "Can't open $file";
148 LABEL: while ( <PFILE> ) {
149         chomp;
150         @line = split( /\s+/ );
151         if ($line[27]) {
152                 print "invalid file format\n";
153                 exit 1; 
154         } 
155         if ($count == 0) {
156                 @GraphTitle = @line;
157                 $count++;
158                 next LABEL;
159         }
160         if ($line[8]) {
161                 if ($line[8] eq "ENOMEM") {
162                         next LABEL;
163                 }
164         }
165         if (!$rsz && $line[3]) {
166                 $rsz = $line[3];
167         }
168         if ($rsz != $line[3]) {
169                 if($readop) {
170                         &write_dat_file("rd");
171                         &write_scr_file("rd");
172                 }
173                 if($writeop) {
174                         &write_dat_file("wr");
175                         &write_scr_file("wr");
176                 }
177                 if ($wrrd || $rdwr) {
178                         &write_dat_file("rd");
179                         &write_scr_file("rd");
180                         &write_dat_file("wr");
181                         &write_scr_file("wr");
182                 }
183                 $rsz = $line[3];                
184                 $region = 0;
185                 $thread = 0;
186         }
187         #print "rg$line[5] th$line[7] w$line[9] r$line[$rindex]\n";
188         $rindex = 18;
189         if ($line[18]) {
190                 if ($line[10] eq "failed") {
191                         $rindex = 12;
192                 }
193                 if ($line[8] eq "write" && $line[17] eq "read") {
194                         $wrrd = 1;
195                 }
196                 if ($line[8] eq "read" && $line[17] eq "write") {
197                         $rdwr = 1;
198                 }
199         } else {
200                 if ($line[8] eq "write" && $line[9]) {
201                         $writeop = 1;
202                 }
203                 if ($line[8] eq "read" && $line[9]) {
204                         $readop = 1;
205                 }
206                 
207         }
208         if ($wrrd || $rdwr) {
209                 $out{$line[7]}{$line[5]} = $line[9];
210                 if ($line[$rindex+1]) {
211                         if (!($line[$rindex+1] eq "failed")) {  
212                                 goto LABEL2;    
213                         }
214                 } else {
215 LABEL2:                 if ($line[5] <= 1 ) {
216                                 $out{$line[7]}{$line[5] - 1} = $line[$rindex];
217                         } else {
218                                 $out{$line[7]}{$line[5] + 1} = $line[$rindex];
219                         }
220                 }
221         }
222         if ($writeop) {
223                 $out{$line[7]}{$line[5]} = $line[9];    
224         }
225         if ($readop) {
226                 $out{$line[7]}{$line[5]} = $line[9];    
227         }
228         if ( $region < $line[7] ) {
229                 $region = $line[7];
230         }
231         if ( $thread < $line[5] ) {
232                 $thread = $line[5];
233         }
234         $count++;
235 }
236 close PFILE;
237 if ($count > 1 && $rsz) {
238         if($readop) {
239                 &write_dat_file("rd");
240                 &write_scr_file("rd");
241         }
242         if($writeop) {
243                 &write_dat_file("wr");
244                 &write_scr_file("wr");
245         }
246         if ($wrrd || $rdwr) {
247                 &write_dat_file("rd");
248                 &write_scr_file("rd");
249                 &write_dat_file("wr");
250                 &write_scr_file("wr");
251         }
252 } else {
253         print "Invalid log file format\n";
254 }