Whamcloud - gitweb
LU-13090 utils: fix lfs_migrate -p for file with pool
[fs/lustre-release.git] / lustre-iokit / obdfilter-survey / iokit-plot-obdfilter
1 #!/usr/bin/perl -w
2 # GPL HEADER START
3 #
4 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License version 2 only,
8 # as published by the Free Software Foundation.
9 #
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # General Public License version 2 for more details (a copy is included
14 # in the LICENSE file that accompanied this code).
15 #
16 # You should have received a copy of the GNU General Public License
17 # version 2 along with this program; If not, see
18 # http://www.gnu.org/licenses/gpl-2.0.html
19 #
20 # GPL HEADER END
21 #
22 # Copyright  2008 Sun Microsystems, Inc. All rights reserved
23 # Use is subject to license terms.
24 #
25 # Copyright (c) 2013, 2014, Intel Corporation.
26 #
27 # This file is part of Lustre, http://www.lustre.org/
28 # Lustre is a trademark of Sun Microsystems, Inc.
29 #
30 # Author: Jitendra Pawar <jitendra@clusterfs.com>
31
32 # Report generation for iokit-plot-obdfilter
33 # ====================================
34 # The iokit-plot-obdfilter script is used to generate csv file and
35 # instructions files for gnuplot from the output of obdfilter-survey script.
36 #
37 # The iokit-plot-obdfilter also creates .scr file that contains instructions
38 # for gnuplot to plot the graph. After generating .dat and .scr files this
39 # script invokes gnuplot to display graph.
40 #
41 # Syntax:
42 # $ obdfilter-survey > log_filename
43 # $ iokit-plot-obdfilter <log_filename>
44 # [Note: 1. Please use the .summary file generated by obdfilter-survey as log_file.
45 #           It is generally available at /tmp/obdfilter_survey_<date_time_system>.summary
46 #        2. This script may need modifications whenever there will be
47 #           modifications in output format of obdfilter-survey script.
48 #        3. Gnuplot version 4.0 or above is required.]
49
50 my @GraphTitle;
51 sub usage() {
52         print "Usage: $0 <log_filename> [--st=<subtitle>] [--y0=<Y-axis start point>]\n";
53         print "       The $0 parses and plots graphs for output of obdfilter-survey using gnuplot.\n";
54         print "       It generates <log_filename>-<Rsize><rd/wr>.dat and\n";
55         print "       <log_filename>-<Rsize>-<rd/wr/rrd/rwr/rwa>.scr files.\n";
56         print "       Those will be used for graphing the results\n";
57         print "OPTIONS:\n";
58         print " --st: SubTitle for the graph\n";
59         print " --y0: Start point of Y-axis, Default value automatically taken based on Y-axis values ranges\n";
60         print " log_file: use the .summary file generated by obdfilter-survey as log_file.\n";
61         print "           It is generally available at /tmp/obdfilter_survey_<date_time_system>.summary\n";
62         print "e.g.   # $0 obdfilter-log --st=\"Sub-Title\" --y0=50\n";
63         exit 1;
64 }
65
66 # check whether gnuplot exists?
67 system ("which gnuplot > /dev/null") == 0 or die "gnuplot does not exist, please install it and try again.\n";
68
69 #Subroutine to write .scr file that further used by gnuplot to plot the graph.
70 sub write_scr_file() {
71         my $op = $_[0];
72         my $rwlabel = $_[1];
73         print "generating plot $file-$rsz-$op.png\n";
74         open ( SCRFILE, "> $file-$rsz-$op.scr" ) or die "Can't open scr file for writing";
75
76         if ($subtitle) {
77                 print SCRFILE "set title \"@GraphTitle\\n$rwlabel, Rsize = $rsz KBytes, $subtitle\"\n";
78         } else {
79                 print SCRFILE "set title \"@GraphTitle\\n$rwlabel, Rsize = $rsz KBytes\"\n";
80         }
81         print SCRFILE "set xlabel \"Threads\"\n";
82         print SCRFILE "set ylabel \"Speeds(MB/s)\"\n";
83         print SCRFILE "set logscale x\n";
84         print SCRFILE "set grid\n";
85         print SCRFILE "set terminal png\n";
86         print SCRFILE "set output \"/dev/null\"\n";
87         if ($opt_y0 != -9999) {
88                 print SCRFILE "set yrange [ $opt_y0: ]\n";
89         }
90         my $plot = "plot";
91         $i = 2;
92         $xrange = 1;
93         # generate instructions for gnuplot, with adjusting X-axes ranges
94         for ($j = $first_obj; $j <= $obj ; $j = $j + $j) {
95                         printf SCRFILE "$plot \"$file-$rsz-$op.dat\" using 1:$i axes x%dy1 title \"$rwlabel-obj$j\" with line\n", $xrange;
96                 $i++;
97                 $plot = "replot";
98         }
99         print SCRFILE "set output \"$file-$rsz-$op.png\"\n";
100         print SCRFILE "replot\n";
101         close SCRFILE;
102         $graphgen = 1;
103         # invoke gnuplot to display graph.
104         system ("gnuplot $file-$rsz-$op.scr") == 0 or die "ERROR: while ploting graph";
105         system ("rm $file-$rsz-$op.scr");
106 }
107
108 #Subroutine to write .dat file that further used by gnuplot to plot the graph.
109 sub write_dat_file() {
110         my $op = $_[0];
111         print "writing data $file-$rsz-$op.dat\n";
112         # Open .csv/.dat file for writing required columns from log file.
113         open ( DATAFILE, "> $file-$rsz-$op.dat" ) or die "Can't open csv file for writing";
114         printf DATAFILE "%-6s", "thrd";
115         for ($j = $first_obj; $j <= $obj; $j = $j + $j) {
116                 printf DATAFILE "%-10s", "$op-obj$j";
117         }
118         for ( $i = $first_thread; $i <= $thread; $i = $i + $i ) {
119                 printf DATAFILE "\n%-6s", $i;
120                 for ($j = $first_obj; $j <= $obj; $j = $j + $j) {
121                 # switch-case can be used instead if else
122                         if ($op eq "rd") {
123                                         if ( $ard{$i}{$j} ) {
124                                                 printf DATAFILE "%-10s", $ard{$i}{$j};
125                                         } else {
126                                                 printf DATAFILE "%-10s", "-";
127                                         }
128                         } elsif ($op eq "wr" ) {
129                                         if ( $awr{$i}{$j} ) {
130                                                 printf DATAFILE "%-10s", $awr{$i}{$j};
131                                         } else {
132                                                 printf DATAFILE "%-10s", "-";
133                                         }
134                         } elsif ($op eq "rwr" ) {
135                                         if ( $arwr{$i}{$j} ) {
136                                                 printf DATAFILE "%-10s", $arwr{$i}{$j};
137                                         } else {
138                                                 printf DATAFILE "%-10s", "-";
139                                         }
140                         } elsif ($op eq "rrd" ) {
141                                         if ( $arrd{$i}{$j} ) {
142                                                 printf DATAFILE "%-10s", $arrd{$i}{$j};
143                                         } else {
144                                                 printf DATAFILE "%-10s", "-";
145                                         }
146                         } elsif ($op eq "rwa" ) {
147                                         if ( $arwa{$i}{$j} ) {
148                                                 printf DATAFILE "%-10s", $arwa{$i}{$j};
149                                         } else {
150                                                 printf DATAFILE "%-10s", "-";
151                                         }
152                         }
153                 }
154         }
155         close DATAFILE;
156 }
157
158 #Subroutine to call .scr and .dat file write routines.
159 sub write_files() {
160         for ($cnt = 0; $cnt < @operations; $cnt = $cnt + 1) {
161                 # switch-case can be used instead if else
162                 if($operations[$cnt] eq "read") {
163                         &write_dat_file("rd");
164                         &write_scr_file("rd", "read");
165                 } elsif ($operations[$cnt] eq "write") {
166                         &write_dat_file("wr");
167                         &write_scr_file("wr", "write");
168                 } elsif ($operations[$cnt] eq "reread") {
169                         &write_dat_file("rrd");
170                         &write_scr_file("rrd", "reread");
171                 } elsif ($operations[$cnt] eq "rewrite") {
172                         &write_dat_file("rwr");
173                         &write_scr_file("rwr", "rewrite");
174                 } elsif ($operations[$cnt] eq "rewrite_again") {
175                         &write_dat_file("rwa");
176                         &write_scr_file("rwa", "rewrite_again");
177                 }
178         }
179 }
180
181 if ( !$ARGV[0] ) {
182         usage();
183 }
184 $file = $ARGV[0];
185 $obj = 0;
186 $thread = 0;
187 $first_obj = 1;
188 $first_thread = 1;
189 $count = 0;
190 $rsz = 0;
191 $subtitle = "";
192 $opt_y0 = -9999;
193 $cnt = 0;
194 @operations = ();
195 $graphgen = 0;
196 # Command line parameter parsing
197 use Getopt::Long;
198 GetOptions ('help' => \$opt_help, 'st=s' => \$subtitle, 'y0=i' => \$opt_y0) or usage();
199 if ($opt_help) {
200         usage();
201 }
202 open ( PFILE, "$file") or die "Can't open results";
203 LABEL: while ( <PFILE> ) {
204         chomp;
205         @line = split( /\s+/ );
206         if ($count == 0) {
207                 @GraphTitle = @line;
208                 $count++;
209                 next LABEL;
210         }
211         $linelen = @line;
212         if ($linelen > 26 || $linelen < 11) {
213                 print "invalid file format at line $count\n";
214                 exit 1;
215         }
216         if (!$rsz && $line[5]) {
217                 $cnt = 0;
218                 $rsz = $line[5];
219                 $first_obj = $line[7];
220                 $first_thread = $line[9];
221                 for ($i = 10; $i <= $linelen; $i = $i + 5) {
222                         if ($line[$i]) {
223                                 $operations[$cnt] = $line[$i];
224                                 $cnt++;
225                         }
226                 }
227         }
228         if ($rsz ne $line[5]) {
229                 &write_files();
230                 $rsz = $line[5];
231                 $first_obj = $line[7];
232                 $first_thread = $line[9];
233                 @operations = ();
234                 $cnt = 0;
235                 for ($i = 10; $i <= $linelen; $i = $i + 5) {
236                         if ($line[$i]) {
237                                 $operations[$cnt] = $line[$i];
238                                 $cnt++;
239                         }
240                 }
241                 $obj = 0;
242                 $thread = 0;
243         }
244         for ($i = 0; $i < @operations; $i++) {
245                 # switch-case can be used instead if else
246                 if($operations[$i] eq "read") {
247                         $ard{$line[9]}{$line[7]} = $line[$i * 5 + 11];
248                 } elsif ($operations[$i] eq "write") {
249                         $awr{$line[9]}{$line[7]} = $line[$i * 5 + 11];
250                 } elsif ($operations[$i] eq "reread") {
251                         $arrd{$line[9]}{$line[7]} = $line[$i * 5 + 11];
252                 } elsif ($operations[$i] eq "rewrite") {
253                         $arwr{$line[9]}{$line[7]} = $line[$i * 5 + 11];
254                 } elsif ($operations[$i] eq "rewrite_again") {
255                         $arwa{$line[9]}{$line[7]} = $line[$i * 5 + 11];
256                 }
257         }
258         if ( $obj < $line[9] ) {
259                 $obj = $line[9];
260         }
261         if ( $thread < $line[7] ) {
262                 $thread = $line[7];
263         }
264         $count++;
265 }
266 close PFILE;
267 if ($count > 1 && $rsz) {
268         &write_files();
269 }
270 if (!$graphgen) {
271         print "Invalid log file format\n";
272 }