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