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