Whamcloud - gitweb
300c9d882bbc56ab548eb9124fcd6a19325b9d16
[fs/lustre-release.git] / lustre / utils / llobdstat
1 #!/usr/bin/perl
2 # llobdstat is a utility that parses obdfilter statistics files 
3 # found at proc/fs/lustre/<ostname>/stats.
4 # It is mainly useful to watch the statistics change over time.
5
6 my $pname = $0;
7
8 my $defaultpath = "/proc/fs/lustre";
9 my $obdstats = "stats";
10
11 sub usage()
12 {
13     print STDERR "Usage: $pname <ost_name> [<interval>]\n";
14     print STDERR "where  ost_name  : ost name under $defaultpath/obdfilter\n";
15     print STDERR "       interval  : sample interaval in seconds\n";
16     print STDERR "example: $pname lustre-OST0000 2\n";
17     print STDERR "Use CTRL + C to stop statistics printing\n";
18     exit 1;
19 }
20
21 my $statspath = "None";
22 my $interval = 0;
23
24 if (($#ARGV < 0) || ($#ARGV > 1)) {
25     usage();
26 } else {
27     if ( $ARGV[0] =~ /help$/ ) {
28         usage();
29     }
30     if ( -f $ARGV[0] ) {
31         $statspath = $ARGV[0];
32     } elsif ( -f "$ARGV[0]/$obdstats" ) {
33         $statspath = "$ARGV[0]/$obdstats";
34     } else {
35         my $st = `ls $defaultpath/*/$ARGV[0]/$obdstats 2> /dev/null`;
36         chop $st;
37         if ( -f "$st" ) {
38             $statspath = $st;
39         }
40     }
41     if ( $statspath =~ /^None$/ ) {
42         die "Cannot locate stat file for: $ARGV[0]\n";
43     }
44     if ($#ARGV == 1) {
45         $interval = $ARGV[1];
46     } 
47 }
48
49 print "$pname on $statspath\n";
50
51 my %cur;
52 my %last;
53 my $mhz = 0;
54
55 #Removed some statstics like open, close that obdfilter don't contains.
56 #To add statistics parameters one need to specify parameter names in below declarations in same sequence. 
57 my ($read_bytes, $write_bytes, $create, $destroy, $statfs, $punch, $snapshot_time) = 
58     ("read_bytes", "write_bytes", "create", "destroy", "statfs", "punch", "snapshot_time"); 
59
60 my @extinfo = ($create, $destroy, $statfs, $punch);
61 my %shortname = ($create => "cx", $destroy => "dx", $statfs => "st", $punch => "pu");
62
63 sub get_cpumhz()
64 {
65     my $cpu_freq;
66     my $itc_freq; # On Itanium systems use this
67     if (open(CPUINFO, "/proc/cpuinfo")==0) {
68         return;
69     }
70     while (<CPUINFO>) {
71         if (/^cpu MHz\s+:\s*([\d\.]+)/) { $cpu_freq=$1; }
72         elsif (/^itc MHz\s+:\s*([\d\.]+)/) { $itc_freq=$1; }
73     }
74     if (defined($itc_freq)) { $mhz = $itc_freq; }
75     elsif (defined($cpu_freq)) { $mhz = $cpu_freq; }
76     else { $mhz = 1; }
77     close CPUINFO;
78 }
79
80 get_cpumhz();
81 print "Processor counters run at $mhz MHz\n";
82
83 # readstats subroutine reads statistics from obdfilter stats file.
84 # This subroutine gets called after every interval specified by user.     
85 sub readstat()
86 {
87         my $prevcount;
88         my @iodata;
89
90         seek STATS, 0, 0;
91         while (<STATS>) {
92                 chop;
93 #               ($name, $cumulcount, $samples, $unit, $min, $max, $sum, $sumsquare) 
94                 @iodata = split(/\s+/, $_);
95                 my $name = $iodata[0];
96
97                 $prevcount = $cur{$name};
98                 if (defined($prevcount)) {
99                         $last{$name} = $prevcount; 
100                 } 
101                 if ($name =~ /^read_bytes$/ || $name =~ /^write_bytes$/) {
102                         $cur{$name} = $iodata[6];
103                 }
104                 elsif ($name =~ /^snapshot_time$/) {
105 #                       $cumulcount =~ /(\d+)/;
106                         $cur{$name} = $iodata[1];
107                 }
108                 else {
109                         $cur{$name} = $iodata[1];
110                 }
111         }
112 }
113 # process_stats subroutine processes stats information read from obdfilter stats file.
114 # This subroutine gets called after every interval specified by user.     
115 sub process_stats()
116 {
117         my $delta;
118         my $data;
119         my $last_time = $last{$snapshot_time};
120         if (!defined($last_time)) {
121                 printf "Read: %-g, Write: %-g, create/destroy: %-g/%-g, stat: %-g, punch: %-g\n",
122                 $cur{$read_bytes}, $cur{$write_bytes},  
123                 $cur{$create}, $cur{$destroy}, 
124                 $cur{$statfs}, $cur{$punch}; 
125                 if ($interval) {
126                         print "[NOTE: cx: create, dx: destroy, st: statfs, pu: punch ]\n\n";
127                         print "Timestamp   Read-delta  ReadRate  Write-delta  WriteRate\n";
128                         print "--------------------------------------------------------\n";
129                 }
130         }
131         else {
132                 my $timespan = $cur{$snapshot_time} - $last{$snapshot_time};
133                 my $rdelta = $cur{$read_bytes} - $last{$read_bytes};
134                 my $rrate = ($rdelta) / ($timespan * ( 1 << 20 ));
135                 my $wdelta = $cur{$write_bytes} - $last{$write_bytes};
136                 my $wrate = ($wdelta) / ($timespan * ( 1 << 20 ));
137                 $rdelta = ($rdelta) / (1024 * 1024);
138                 $wdelta = ($wdelta) / (1024 * 1024);
139                 # This print repeats after every interval.
140                 printf "%10lu  %6.2fMB  %6.2fMB/s   %6.2fMB  %6.2fMB/s",
141                         $cur{$snapshot_time}, $rdelta, $rrate, $wdelta, $wrate;
142
143                 $delta = $cur{$getattr} - $last{$getattr};
144                 if ( $delta != 0 ) {
145                         $rdelta = int ($delta/$timespan);
146                         print " ga:$delta,$rdelta/s";
147                 }
148                 
149                 for $data ( @extinfo ) {
150                         $delta = $cur{$data} - $last{$data};
151                         if ($delta != 0) {
152                                 print " $shortname{$data}:$delta";
153                         }
154                 }
155                 print "\n";
156                 $| = 1;
157         }
158 }
159 #Open the obdfilter stat file with STATS
160 open(STATS, $statspath) || die "Cannot open $statspath: $!\n";
161 do {
162         readstat();             # read the statistics from stat file.
163         process_stats();        
164         if ($interval) {        
165                 sleep($interval); 
166                 %last = %cur;
167         }
168 } while ($interval);    # Repeat the statistics printing after every "interval" specified in command line.
169 close STATS;
170 # llobdfilter.pl ends here.