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