Whamcloud - gitweb
LU-1581 utils: introduce osd_init() wrapper
[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> [<count>}]\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 my $counter = undef;
24
25 if (($#ARGV < 0) || ($#ARGV > 2)) {
26     usage();
27 } else {
28     if ( $ARGV[0] =~ /help$/ ) {
29         usage();
30     }
31     if ( -f $ARGV[0] ) {
32         $statspath = $ARGV[0];
33     } elsif ( -f "$ARGV[0]/$obdstats" ) {
34         $statspath = "$ARGV[0]/$obdstats";
35     } else {
36         my $st = "$defaultpath/obdfilter/$ARGV[0]/$obdstats";
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 > 0) {
45         $interval = $ARGV[1];
46     }
47     if ($#ARGV > 1) {
48         $counter = $ARGV[2];
49     }
50 }
51
52 print "$pname on $statspath\n";
53
54 my %cur;
55 my %last;
56 my $mhz = 0;
57
58 # Removed some statstics like open, close that obdfilter doesn't contain.
59 # To add statistics parameters one needs to specify parameter names in the
60 # below declarations in the same sequence.
61 my ($read_bytes, $write_bytes, $create, $destroy, $statfs, $punch,
62     $snapshot_time) = ("read_bytes", "write_bytes", "create", "destroy",
63                        "statfs", "punch", "snapshot_time");
64
65 my @extinfo = ($create, $destroy, $statfs, $punch);
66 my %shortname = ($create => "cx", $destroy => "dx", $statfs => "st", $punch => "pu");
67
68 sub get_cpumhz()
69 {
70     my $cpu_freq;
71     my $itc_freq; # On Itanium systems use this
72     if (open(CPUINFO, "/proc/cpuinfo")==0) {
73         return;
74     }
75     while (<CPUINFO>) {
76         if (/^cpu MHz\s+:\s*([\d\.]+)/) { $cpu_freq=$1; }
77         elsif (/^itc MHz\s+:\s*([\d\.]+)/) { $itc_freq=$1; }
78     }
79     if (defined($itc_freq)) {
80         $mhz = $itc_freq;
81     } elsif (defined($cpu_freq)) {
82         $mhz = $cpu_freq;
83     } else {
84         $mhz = 1;
85     }
86     close CPUINFO;
87 }
88
89 get_cpumhz();
90 print "Processor counters run at $mhz MHz\n";
91
92 # read statistics from obdfilter stats file.
93 # This subroutine gets called after every interval specified by user.
94 sub readstat()
95 {
96     my $prevcount;
97     my @iodata;
98
99     seek STATS, 0, 0;
100     while (<STATS>) {
101         chop;
102 #        ($name, $cumulcount, $samples, $unit, $min, $max, $sum, $sumsquare)
103         @iodata = split(/\s+/, $_);
104         my $name = $iodata[0];
105
106         $prevcount = $cur{$name};
107         if (defined($prevcount)) {
108             $last{$name} = $prevcount;
109         }
110         if ($name =~ /^read_bytes$/ || $name =~ /^write_bytes$/) {
111             $cur{$name} = $iodata[6];
112         }
113         elsif ($name =~ /^snapshot_time$/) {
114 #            $cumulcount =~ /(\d+)/;
115             $cur{$name} = $iodata[1];
116         } else {
117             $cur{$name} = $iodata[1];
118         }
119     }
120 }
121
122 # process stats information read from obdfilter stats file.
123 # This subroutine gets called after every interval specified by user.
124 sub process_stats()
125 {
126     my $delta;
127     my $data;
128     my $last_time = $last{$snapshot_time};
129     if (!defined($last_time)) {
130         printf "Read: %-g, Write: %-g, create/destroy: %-g/%-g, stat: %-g, punch: %-g\n",
131         $cur{$read_bytes}, $cur{$write_bytes},
132         $cur{$create}, $cur{$destroy},
133         $cur{$statfs}, $cur{$punch};
134         if ($interval) {
135             print "[NOTE: cx: create, dx: destroy, st: statfs, pu: punch ]\n\n";
136             print "Timestamp   Read-delta  ReadRate  Write-delta  WriteRate\n";
137             print "--------------------------------------------------------\n";
138         }
139     } else {
140         my $timespan = $cur{$snapshot_time} - $last{$snapshot_time};
141         my $rdelta = $cur{$read_bytes} - $last{$read_bytes};
142         my $rrate = ($rdelta) / ($timespan * ( 1 << 20 ));
143         my $wdelta = $cur{$write_bytes} - $last{$write_bytes};
144         my $wrate = ($wdelta) / ($timespan * ( 1 << 20 ));
145         $rdelta = ($rdelta) / (1024 * 1024);
146         $wdelta = ($wdelta) / (1024 * 1024);
147         # This print repeats after every interval.
148         printf "%10lu  %6.2fMB  %6.2fMB/s   %6.2fMB  %6.2fMB/s",
149                $cur{$snapshot_time}, $rdelta, $rrate, $wdelta, $wrate;
150
151         $delta = $cur{$getattr} - $last{$getattr};
152         if ( $delta != 0 ) {
153             $rdelta = int ($delta/$timespan);
154             print " ga:$delta,$rdelta/s";
155         }
156
157         for $data ( @extinfo ) {
158             $delta = $cur{$data} - $last{$data};
159             if ($delta != 0) {
160                 print " $shortname{$data}:$delta";
161             }
162         }
163         print "\n";
164         $| = 1;
165     }
166 }
167
168 #Open the obdfilter stat file with STATS
169 open(STATS, $statspath) || die "Cannot open $statspath: $!\n";
170 do {
171     # read the statistics from stat file.
172     readstat();
173     process_stats();
174     if ($interval) {
175         sleep($interval);
176         %last = %cur;
177     }
178     # Repeat the statistics printing after every "interval" specified in
179     # command line, up to counter times, if specified
180 } while ($interval && (defined($counter) ? $counter-- > 0 : 1));
181 close STATS;
182 # llobdfilter.pl ends here.