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