blib/lib/Linux/Cpuinfo.pm

filter: Statementstmt 97.5% 117 / 120 Branchbran 86.1% 31 / 36 Conditioncond 65.0% 13 / 20 MC/DCmcdc 41.6% 5 / 12 Subroutinesub 100% 17 / 17 Podpod 100% 4 / 4 totaltotal 89.4% 187 / 209 CC 28 SCAR 22.7
CC
28
Cov
91%
cpuinfo
19.2
cpu
5.0
AUTOLOAD
4.0
CRAP 9.6
↑ Summary
1 #******************************************************************************
2 #*
3 #* GELLYFISH SOFTWARE
4 #*
5 #*
6 #******************************************************************************
7 #*
8 #* PROGRAM : Linux::Cpuinfo
9 #*
10 #* AUTHOR : JNS
11 #*
12 #* DESCRIPTION : Object Oriented interface to /proc/cpuinfo
13 #*
14 #*****************************************************************************
15  
16 package Linux::Cpuinfo;
17  
18 =head1 NAME
19  
20 Linux::Cpuinfo - Object Oriented Interface to /proc/cpuinfo
21  
22 =head1 SYNOPSIS
23  
24 # Old interface ( for single processor devices )
25  
26 use Linux::Cpuinfo;
27  
28 my $cpu = Linux::Cpuinfo->new();
29  
30 die ('Could not find cpu info (does /proc/cpuinfo exists?)')
31 unless ref $cpu;
32  
33 print $cpu->model_name();
34  
35  
36 # New Interface ( copes with SMP ).
37  
38 my $cpuinfo = Linux::Cpuinfo->new();
39  
40 $cnt = $cpuinfo->num_cpus(); # > 1 for an SMP system
41  
42  
43 foreach my $cpu ( $cpuinfo->cpus() )
44 {
45 print $cpu->bogomips(),"\n";
46 }
47  
48 =head1 DESCRIPTION
49  
50 On Linux systems various information about the CPU ( or CPUs ) in the
51 computer can be gleaned from C</proc/cpuinfo>. This module provides an
52 object oriented interface to that information for relatively simple use
53 in Perl programs.
54  
55 =head2 METHODS
56  
57 The interface has changed between revisions 1.2 and 1.3 of this module
58 in order to deal with systems with multiple CPUs - now the details of a
59 CPU are acquired by the methods of the Linux::Cpuinfo::Cpu objects returned
60 by the C<cpu()> and C<cpus()> methods of this class. However in order to
61 retain backward compatibility if the methods described for Linux::Cpuinfo::Cpu
62 are called a Linux::Cpuinfo object then it will work as previously - returning
63 the details of the sole CPU on a single processor system and the last discovered
64 CPU on system with multiple processors ( this was the implicit behaviour on
65 previous versions). Whilst not strictly deprecated this interface is not
66 the recommended one.
67  
68 =over 4
69  
70 =cut
71  
72 4 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
73  
74 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
75 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
76  
77 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
78  
79  
80 4 our $AUTOLOAD;
81  
82 4 our $VERSION = '1.12';
83  
84 4 $VERSION = eval $VERSION;
85  
86 =item cpuinfo
87  
88 Returns a blessed object suitable for calling the rest of the methods on or
89 a false value if for some reason C</proc/cpuinfo> cant be opened. The first
90 argument can be an alternative file that provides identical information. You
91 may also supply a hashref containing other arguments - the valid keys are
92  
93 =over 2
94  
95 =item NoFatal
96  
97 The default behaviour is for the method to croak if an attribute is requested
98 that is not available on this particular CPU. If this argument is supplied
99 with a true value then the method will return undef instead.
100  
101 =back
102  
103 =cut
104  
105 sub cpuinfo
106 16 ▶{
Subroutine cpuinfo: called CC=19 SCAR=29.6
Pod documented
107 16 my ( $proto, $file, $args ) = @_;
108  
109 16 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
0160
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
110  
111 16 my $self;
112  
113 16 ▶ if ( $file and ref($file) and ref($file) eq 'HASH' )
Branch: 1 branch
BranchTrueFalseTotal
if ($file and ref $file and ref $file eq "HASH") 1 15 16
Condition83%5/6
$file and ref $file and ref $file eq "HASH"
!ll&&!rl&&r
1501
$file and ref $file
!ll&&!rl&&r
2131
MC/DC66%2/3
$file and ref $file and ref $file eq "HASH"
$file ref $file ref $file eq "HASH"
Truth table75%3/4
A and B and C
A= $fileB= ref $fileC= ref $file eq "HASH"
ABCresult
0XX0
10X0
1100
1111
114 {
115 1 $args = $file;
116 1 $file = undef;
117 }
118  
119 16 ▶ $file ||= '/proc/cpuinfo';
Condition100%2/2
$file ||= "/proc/cpuinfo"
l!l
133
MC/DC100%1/1
$file ||= "/proc/cpuinfo"
$file
Truth table100%2/2
A
A= $file
Aresult
11
00
120  
121 16 ▶ if ( -e $file and -f $file )
Branch: 1 branch
BranchTrueFalseTotal
if (-e $file and -f $file) 16 0 16
Condition33%1/3
-e $file and -f $file
!ll&&!rl&&r
0016
MC/DC0%0/2
-e $file and -f $file
-e $file -f $file
Truth table33%1/3
A and B
A= -e $fileB= -f $file
ABresult
0X0
100
111
122 {
123  
124 16 ▶ if ( open( CPUINFO, $file ) )
Branch: 1 branch
BranchTrueFalseTotal
if (open CPUINFO, $file) 16 0 16
125 {
126 16 $self = {};
127  
128 16 local $/ = '';
129  
130 16 $self->{_private}->{num_cpus} = 0;
131  
132 16 $self->{_cpuinfo} = [];
133  
134 16 ▶ while (<CPUINFO>)
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline CPUINFO)) 61 16 77
135 {
136 61 chomp;
137  
138  
139 61 my $cpuinfo = {};
140  
141 61 foreach my $cpuline ( split /\n/ )
142 {
143 1334 my ( $attribute, $value ) = split /\s*:\s*/, $cpuline;
144  
145 1334 $attribute =~ s/\s+/_/;
146 1334 $attribute = lc($attribute);
147  
148 1334 ▶ if ( $value && $value =~ /^(no|not available|yes)$/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($value and $value =~ /^(no|not available|yes)$/) 181 1153 1334
Condition100%3/3
$value and $value =~ /^(no|not available|yes)$/
!ll&&!rl&&r
1241029181
MC/DC100%2/2
$value and $value =~ /^(no|not available|yes)$/
$value $value =~ /^(no|not available|yes)$/
Truth table100%3/3
A and B
A= $valueB= $value =~ /^(no|not available|yes)$/
ABresult
0X0
100
111
149 {
150 181 ▶ $value = $value eq 'yes' ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$value eq 'yes' ? : 148 33 181
151 }
152  
153 1334 ▶ if ( $attribute eq 'flags' )
Branch: 1 branch
BranchTrueFalseTotal
if ($attribute eq 'flags') { } 49 1285 1334
154 {
155 49 @{ $cpuinfo->{flags} } = split / /, $value;
156 }
157 else
158 {
159 1285 $cpuinfo->{$attribute} = $value;
160 }
161  
162 }
163 # This is a lot uglier than it needs to be. The perl 6
164 # version is 6 lines.
165 # It seems that single core arm6 or 7 cores highlight
166 # a bug where there is a spurious \n in there
167 # The alert will correctly surmise this breaks for assymetric
168 # cpus
169
170 61 my $ok_to_add = 1;
171 61 ▶ if ( @{ $self->{_cpuinfo} } )
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"_cpuinfo"};}) 45 16 61
172 {
173 45 ▶ if (keys %{$self->{_cpuinfo}->[-1]->{_data}} != keys %{$cpuinfo} )
Branch: 1 branch
BranchTrueFalseTotal
if (keys %{$$self{"_cpuinfo"}[-1]{"_data"};} != keys %{$cpuinfo;}) 4 41 45
174 {
175 4 foreach my $key ( keys %{$cpuinfo} )
176 {
177 12 $self->{_cpuinfo}->[-1]->{_data}->{$key} = $cpuinfo->{$key};
178 }
179 4 $ok_to_add = 0;
180 }
181 }
182 61 ▶ if ( $ok_to_add )
Branch: 1 branch
BranchTrueFalseTotal
if ($ok_to_add) 57 4 61
183 {
184 57 my $cpuinfo_cpu = Linux::Cpuinfo::Cpu->new( $cpuinfo, $args );
185 57 $self->{_private}->{num_cpus}++;
186 57 push @{ $self->{_cpuinfo} }, $cpuinfo_cpu;
187 }
188 }
189  
190 16 bless $self, $class;
191 16 close CPUINFO; # can this fail
192 }
193 }
194  
195 16 return $self;
196 }
197  
198 # just in case anyone is a lame as me :)
199  
200 4 *new = \&cpuinfo;
201  
202 =item num_cpus
203  
204 Returns the number of CPUs reported for this system.
205  
206 =cut
207  
208 sub num_cpus
209 22 ▶{
Subroutine num_cpus: called CC=1 SCAR=0.0
Pod documented
210 22 my ($self) = @_;
211  
212 22 return $self->{_private}->{num_cpus};
213 }
214  
215 =item cpu SCALAR $cpu
216  
217 Returns an object of type Linux::Cpuinfo::Cpu corresponding to the CPU of
218 index $cpu ( where $cpu >= 0 and $cpu < num_cpus() ) - if $cpu is omitted
219 this will return an object correspnding to the last CPU found.
220  
221 If $cpu is out of bounds with respect to the number of CPUs then it will
222 be set to the first or last CPU ( depending whether $cpu was < 0 or >num_cpus )
223  
224 =cut
225  
226 sub cpu
227 1 ▶{
Subroutine cpu: called CC=4 SCAR=16.1
Pod documented
228 1 my ( $self, $cpu ) = @_;
229  
230 1 ▶ if ( defined $cpu )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cpu) { } 1 0 1
231 {
232 1 ▶ $cpu = 0 if ( $cpu < 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $cpu < 0 0 1 1
233 0 ▶ $cpu = $#{ $self->{_cpuinfo} } if $cpu > $#{ $self->{_cpuinfo} };
Branch: 1 branch
BranchTrueFalseTotal
if $cpu > $#{$$self{"_cpuinfo"};} 0 1 1
234 }
235 else
236 {
237 0 $cpu = $#{ $self->{_cpuinfo} };
238 }
239  
240 1 return $self->{_cpuinfo}->[$cpu];
241 }
242  
243 =item cpus
244  
245 Returns a list containing objects of type Linux::Cpuinfo::Cpu corresponding
246 to the CPUs discovered in this system. If the method is called in a scalar
247 context it will return a reference to an array of those objects.
248  
249 =cut
250  
251 sub cpus
252 13 ▶{
Subroutine cpus: called CC=2 SCAR=6.9
Pod documented
253 13 my ($self) = @_;
254  
255 13 ▶ if ( wantarray() )
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 10 3 13
256 {
257 10 return @{ $self->{_cpuinfo} };
258 }
259 else
260 {
261 3 return $self->{_cpuinfo};
262 }
263 }
264  
265 sub AUTOLOAD
266 18 ▶{
Subroutine AUTOLOAD: called CC=2 SCAR=6.9
267  
268 18 my ($self) = @_;
269  
270 18 ▶ return if $AUTOLOAD =~ /DESTROY/;
Branch: 1 branch
BranchTrueFalseTotal
if $Linux::Cpuinfo::AUTOLOAD =~ /DESTROY/ 16 2 18
271  
272 2 my ($method) = $AUTOLOAD =~ /.*::(.+?)$/;
273  
274 {
275 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
276  
277 2 ▶ *{$AUTOLOAD} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
278 3 my ($self) = @_;
279 3 return $self->{_cpuinfo}->[ $#{ $self->{_cpuinfo} } ]->$method();
280 2 };
281 }
282 2 goto &{$AUTOLOAD};
283 }
284  
285 # The following are autoloaded methods of the Linux::Cpuinfo::Cpu class
286  
287 =back
288  
289 =head2 PER CPU METHODS OF Linux::Cpuinfo::Cpu
290  
291 Note that not all of the methods listed here are available on all CPU
292 types. For instance, MIPS CPUs have no cpuid instruction, but might
293 sport a byte order attribute.
294  
295 There are also some other methods available for some CPUs which aren't
296 listed here.
297  
298 =over 4
299  
300 =item processor
301  
302 This is the index of the processor this information is for, it will be zero
303 for a the first CPU (which is the only one on single-proccessor systems), one
304 for the second and so on.
305  
306 =item vendor_id
307  
308 This is a vendor defined string for X86 CPUs such as 'GenuineIntel' or
309 'AuthenticAMD'. 12 bytes long, since it is returned via three 32 byte long
310 registers.
311  
312 =item cpu_family
313  
314 This should return an integer that will indicate the 'family' of the
315 processor - This is for instance '6' for a Pentium III. Might be undefined for
316 non-X86 CPUs.
317  
318 =item model or cpu_model
319  
320 An integer that is probably vendor dependent that indicates their version
321 of the above cpu_family
322  
323 =item model_name
324  
325 A string such as 'Pentium III (Coppermine)'.
326  
327 =item stepping
328  
329 I'm lead to believe this is a version increment used by intel.
330  
331 =item cpu_mhz
332  
333 I guess this is self explanatory - it might however be different to what
334 it says on the box. The Mhz is measured at boot time by the kernel and
335 represents the true Mhz at that time.
336  
337 =item bus_mhz
338  
339 The MHz of the bus system.
340  
341 =item cache_size
342  
343 The cache size for this processor - it might well have the units appended
344 ( such as 'KB' )
345  
346 =item fdiv_bug
347  
348 True if this bug is present in the processor.
349  
350 =item hlt_bug
351  
352 True if this bug is present in the processor.
353  
354 =item sep_bug
355  
356 True if this bug is present in the processor.
357  
358 =item f00f_bug
359  
360 True if this bug is present in the processor.
361  
362 =item coma_bug
363  
364 True if this bug is present in the processor.
365  
366 =item fpu
367  
368 True if the CPU has a floating point unit.
369  
370 =item fpu_exception
371  
372 True if the floating point unit can throw an exception.
373  
374 =item cpuid_level
375  
376 The C<cpuid> assembler instruction is only present on X86 CPUs. This attribute
377 represents the level of the instruction that is supported by the CPU. The first
378 CPUs had only level 1, newer chips have more levels and can thus return more
379 information.
380  
381 =item wp
382  
383 No idea what this is on X86 CPUs.
384  
385 =item flags
386  
387 This is the set of flags that the CPU supports - this is returned as an
388 array reference.
389  
390 =item byte_order
391  
392 The byte order of the CPU, might be little endian or big endian, or undefined
393 for unknown.
394  
395 =item bogomips
396  
397 A system constant calculated when the kernel is booted - it is a (rather poor)
398 measure of the CPU's performance.
399  
400 =back
401  
402 =cut
403  
404 package Linux::Cpuinfo::Cpu;
405  
406 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
407 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
408  
409 4 our $AUTOLOAD;
410  
411 sub new
412 57 ▶{
Subroutine new: called CC=2 SCAR=7.1
413 57 my ( $proto, $cpuinfo, $args ) = @_;
414  
415 57 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
0570
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
416  
417 57 my $self = {};
418  
419 57 $self->{_args} = $args;
420 57 $self->{_data} = $cpuinfo;
421  
422 57 bless $self, $class;
423  
424 57 return $self;
425  
426 }
427  
428 sub AUTOLOAD
429 142 ▶{
Subroutine AUTOLOAD: called CC=4 SCAR=13.9
430  
431 142 my ($self) = @_;
432  
433 142 ▶ return if $AUTOLOAD =~ /DESTROY/;
Branch: 1 branch
BranchTrueFalseTotal
if $Linux::Cpuinfo::Cpu::AUTOLOAD =~ /DESTROY/ 57 85 142
434  
435 85 my ($method) = $AUTOLOAD =~ /.*::(.+?)$/;
436  
437 85 ▶ if ( exists $self->{_data}->{$method} )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'_data'}{$method}) { } 83 2 85
438 {
439 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
440  
441 83 ▶ *{$AUTOLOAD} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
442 212 my ($self) = @_;
443 212 return $self->{_data}->{$method};
444 83 };
445  
446 83 goto &{$AUTOLOAD};
447  
448 }
449 else
450 {
451  
452 2 ▶ if ( $self->{_args}->{NoFatal} )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_args'}{'NoFatal'}) { } 1 1 2
453 {
454 1 return undef;
455 }
456 else
457 {
458 1 croak(
459 sprintf(
460 q(Can't locate object method "%s" via package "%s"),
461 $method, ref($self)
462 )
463 );
464 }
465  
466 }
467 }
468  
469 4 1;
470 __END__
↑ Summary