Graph/Mpstat.pm untested

filter: Statementstmt 0.0% 0 / 151 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 63 MC/DCmcdc 0.0% 0 / 37 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 301 CC 18 SCAR 35.3
CC
18
Cov
0%
read_ncpu
72.0
read_1cpu
42.0
new
12.0
CRAP 34.1
1 ################################################################################
2  
3 use strict;
4 use POSIX qw(mktime);
5 use StatsView::Graph;
6 package StatsView::Graph::Mpstat;
7 @StatsView::Graph::Mpstat::ISA = qw(StatsView::Graph);
8  
9 use vars qw(@Category);
10 @Category = ( "Average Values", "Total Values", "Per CPU Values" );
11
12 ################################################################################
13  
14 sub getline($$)
15 {
16 my ($self, $fh) = @_;
17 my $line;
18 while (defined($line = $fh->getline()) &&
19 index($line, "<<State change>>") != -1)
20 { }
21 return($line);
22 }
23  
24 ################################################################################
25  
26 sub new($$$)
27 {
28 my ($class, $file, $fh) = @_;
29 $class = ref($class) || $class;
30  
31 # Look for the start/interval line
32 my $line;
33 while (defined($line = $class->getline($fh)) && $line =~ /^\s*$/) { }
34 $line =~ /start:/i && $line =~ /interval:/i || return(undef);
35  
36 # Look for the first header line
37 while (defined($line = $class->getline($fh)) && $line =~ /^\s*$/) { }
38 $line =~ /^\s*CPU\s+minf\s+mjf\s+/i || return(undef);
39  
40 # Get the next 2 lines. If the CPU numbers are the same, it's a single CPU
41 # machine, otherwise it's multi-cpu (the output formats differ)
42 $line = $class->getline($fh);
43 my $line1 = $class->getline($fh);
44 return(undef) if (! (defined($line) && defined($line1)));
45 $line = (split(' ', $line, 2))[0];
46 $line1 = (split(' ', $line1, 2))[0];
47 my $self = $class->SUPER::init($file);
48 if ($line eq $line1)
49 {
50 $self->{category} = undef;
51 $self->{reader} = sub { shift(@_)->read_1cpu(@_); };
52 }
53 else
54 {
55 $self->{category} = \@Category;
56 $self->{reader} = sub { shift(@_)->read_ncpu(@_); };
57 }
58  
59 return($self);
60 }
61  
62 ################################################################################
63  
64 sub read_1cpu($$)
65 {
66 my ($self, $category) = @_;
67 $self->SUPER::read();
68  
69 # Open the file
70 my $mpstat = IO::File->new($self->{file}, "r")
71 || die("Can't open $self->{file}: $!\n");
72 $self->{title} = "Mpstat";
73  
74 # Look for the start/interval line
75 my $line;
76 while (defined($line = $self->getline($mpstat)) && $line =~ /^\s*$/) { }
77 $line =~ /start:/i && $line =~ /interval:/i
78 || die("$self->{file} is not a mpstat file (1)\n");
79 $line =~ m!interval:\s+(\d+)!i;
80 $self->{interval} = $1;
81 $line =~ m!start:\s+(\d\d/\d\d/\d\d(?:\d\d)?)\s+(\d\d:\d\d:\d\d)!i;
82 my ($D, $M, $Y) = split(/\//, $1);
83 my ($h, $m, $s) = split(/:/, $2);
84 $M--;
85 if ($Y >= 100) { $Y -= 1900; }
86 elsif ($Y <= 50) { $Y += 100; }
87 $self->{start} = POSIX::mktime($s, $m, $h, $D, $M, $Y, 0, 0, -1)
88 + $self->{interval};
89  
90 # Look for the first header lines
91 while (defined ($line = $self->getline($mpstat))
92 && $line !~ /^\s*CPU\s+minf\s+mjf\s+/i) { }
93 die("$self->{file} is not a mpstat file (2)\n") if (! $line);
94  
95 # Register the column types
96 my (@colname, @coltype);
97 @colname = split(' ', $line);
98 shift(@colname); # lose CPU column
99 my $cpu_end = $#colname;
100 my $cpu_begin = $cpu_end - 3;
101 @coltype = (('N') x $cpu_begin, ("%") x 4);
102 $self->define_cols(\@colname, \@coltype);
103  
104 # Skip the first data segment, which is the values since the last reboot
105 $self->getline($mpstat);
106 die("$self->{file} is not a mpstat file (3)\n") if (! $line);
107  
108 # Read the data
109 my $tstamp = $self->{start};
110 my $interval = $self->{interval};
111  
112 # Only 1 CPU - read the data
113 while (defined($line = $self->getline($mpstat)))
114 {
115 next if ($line =~ /^CPU\s+minf\smjf/i);
116 my (@value) = split(' ', $line);
117 shift(@value); # Lose the CPU number
118 push(@{$self->{data}}, { tstamp => $tstamp, value => [ @value ] });
119 $tstamp += $interval;
120 }
121  
122 $self->{finish} = $tstamp - $interval;
123 $mpstat->close();
124 }
125  
126 ################################################################################
127  
128 sub read_ncpu($$)
129 {
130 my ($self, $category) = @_;
131 $self->SUPER::read();
132  
133 # Open the file
134 my $mpstat = IO::File->new($self->{file}, "r")
135 || die("Can't open $self->{file}: $!\n");
136 $self->{title} = "Mpstat $category";
137  
138 # Look for the start/interval line
139 my $line;
140 while (defined($line = $self->getline($mpstat)) && $line =~ /^\s*$/) { }
141 $line =~ /start:/i && $line =~ /interval:/i
142 || die("$self->{file} is not a mpstat file (1)\n");
143 $line =~ m!interval:\s+(\d+)!i;
144 $self->{interval} = $1;
145 $line =~ m!start:\s+(\d\d/\d\d/\d\d(?:\d\d)?)\s+(\d\d:\d\d:\d\d)!i;
146 my ($D, $M, $Y) = split(/\//, $1);
147 my ($h, $m, $s) = split(/:/, $2);
148 $M--;
149 if ($Y >= 100) { $Y -= 1900; }
150 elsif ($Y <= 50) { $Y += 100; }
151 $self->{start} = POSIX::mktime($s, $m, $h, $D, $M, $Y, 0, 0, -1)
152 + $self->{interval};
153  
154 # Look for the first header lines
155 while (defined ($line = $self->getline($mpstat))
156 && $line !~ /^\s*CPU\s+minf\s+mjf\s+/i) { }
157 die("$self->{file} is not a mpstat file (2)\n") if (! $line);
158  
159 # Register the column types
160 my (@colname, @coltype);
161 @colname = split(' ', $line);
162 shift(@colname); # lose CPU column
163 my $cpu_end = $#colname;
164 my $cpu_begin = $cpu_end - 3;
165 @coltype = (('N') x $cpu_begin, ("%") x 4);
166 $self->define_cols(\@colname, \@coltype);
167  
168 # Skip the first data segment, which is the values since the last reboot
169 while (defined ($line = $self->getline($mpstat))
170 && $line !~ /^CPU\s+minf\s+mjf/i) { }
171 die("$self->{file} is not a mpstat file (3)\n") if (! $line);
172  
173 # Read the data
174 my $tstamp = $self->{start};
175 my $interval = $self->{interval};
176  
177 if ($category eq "Average Values" || $category eq "Total Values")
178 {
179 my $want_avg = $category eq "Average Values" ? 1 : 0;
180 while (defined($line))
181 {
182 # Read the data
183 my $ncpu = 0;
184 my @value = (0) x scalar(@colname);
185 while (defined($line = $self->getline($mpstat))
186 && $line !~ /^CPU\s+minf\smjf/i)
187 {
188 my (@v) = split(' ', $line);
189 shift(@v); # lose CPU number
190 @value = map { $_ += shift(@v) } @value;
191 $ncpu++;
192 }
193 if ($want_avg)
194 { @value = map { $_ /= $ncpu } @value; }
195 else # Always have to average the CPU averages anyway
196 { @value[$cpu_begin .. $cpu_end] =
197 map { $_ /= $ncpu } @value[$cpu_begin .. $cpu_end]; }
198 push(@{$self->{data}}, { tstamp => $tstamp, value => [ @value ] });
199 $tstamp += $interval;
200 }
201 }
202  
203 # Per CPU Values
204 else
205 {
206 while (defined($line))
207 {
208 # Read the data
209 while (defined($line = $self->getline($mpstat))
210 && $line !~ /^CPU\s+minf\smjf/i)
211 {
212 my (@value) = split(' ', $line);
213 my $inst = "CPU " . shift(@value);
214 $self->define_inst($inst);
215 push(@{$self->{data}{$inst}}, { tstamp => $tstamp,
216 value => [ @value ] });
217 }
218 $tstamp += $interval;
219 }
220 }
221  
222 $self->{finish} = $tstamp - $interval;
223 $mpstat->close();
224 }
225  
226  
227 ################################################################################
228  
229 sub read($$)
230 {
231 my ($self, $category) = @_;
232 die("Illegal category type $category\n")
233 if (defined($category) && ! grep($category, @Category));
234 $self->SUPER::read($category);
235 return(&{$self->{reader}}($self, $category));
236 }
237  
238 ################################################################################
239  
240 sub get_data_type($;$)
241 {
242 my ($self, $category) = @_;
243 if (! defined($self->{category})) { return("2d"); }
244 else { return($category eq "Per CPU Values" ? "3d" : "2d"); };
245 }
246  
247 ################################################################################
248 1;