File Coverage

blib/lib/Sys/Info/Driver/Linux/Device/CPU.pm
Criterion Covered Total %
statement 57 58 98.2
branch 12 24 50.0
condition 3 8 37.5
subroutine 11 11 100.0
pod 3 3 100.0
total 86 104 82.6


line stmt bran cond sub pod time code
1             package Sys::Info::Driver::Linux::Device::CPU;
2             $Sys::Info::Driver::Linux::Device::CPU::VERSION = '0.7905';
3 1     1   978 use strict;
  1         2  
  1         25  
4 1     1   4 use warnings;
  1         2  
  1         26  
5 1     1   5 use base qw(Sys::Info::Base);
  1         1  
  1         132  
6              
7 1     1   8 use Sys::Info::Driver::Linux;
  1         2  
  1         40  
8 1     1   407 use Unix::Processors;
  1         1196  
  1         24  
9 1     1   11 use POSIX ();
  1         3  
  1         19  
10 1     1   5 use Carp qw( croak );
  1         2  
  1         647  
11              
12             sub identify {
13 8     8 1 1681 my $self = shift;
14              
15 8 100       24 if ( ! $self->{META_DATA} ) {
16 2         11 my $mach = $self->uname->{machine};
17 2 50       63 my $arch = $mach =~ m{ i [0-9] 86 }xmsi ? 'x86'
    50          
    50          
18             : $mach =~ m{ ia64 }xmsi ? 'IA64'
19             : $mach =~ m{ x86_64 }xmsi ? 'AMD-64'
20             : $mach
21             ;
22              
23             my @raw = split m{\n\n}xms,
24 2         11 $self->trim( $self->slurp( proc->{cpuinfo} ) );
25 2         1339 $self->{META_DATA} = [];
26 2         6 foreach my $e ( @raw ) {
27 32         53 push @{ $self->{META_DATA} },
  32         75  
28             { $self->_parse_cpuinfo($e), architecture => $arch };
29             }
30             }
31              
32 8         32 return $self->_serve_from_cache(wantarray);
33             }
34              
35             sub bitness {
36 1     1 1 264 my $self = shift;
37 1         5 my @cpu = $self->identify;
38 1         12 my $flags = $cpu[0]->{flags};
39 1 50       3 if ( $flags ) {
40 1         3 my $lm = grep { $_ eq 'lm' } @{$flags};
  31         42  
  1         2  
41 1 50       7 return '64' if $lm;
42             }
43 0 0       0 return $cpu[0]->{architecture} =~ m{64}xms ? '64' : '32';
44             }
45              
46             sub load {
47 5     5 1 553 my $self = shift;
48 5         8 my $level = shift;
49 5         14 my @loads = split /\s+/xms, $self->slurp( proc->{loadavg} );
50 5         632 return $loads[$level];
51             }
52              
53             sub _parse_cpuinfo {
54 32     32   46 my $self = shift;
55 32   33     62 my $raw = shift || croak 'Parser called without data';
56 32         59 my($k, $v);
57 32         0 my %cpu;
58 32         172 foreach my $line (split /\n/xms, $raw) {
59 832         2132 ($k, $v) = split /\s+:\s+/xms, $line;
60 832         1506 $cpu{$k} = $v;
61             }
62              
63 32 50       390 my @flags = $cpu{flags} ? (split /\s+/xms, $cpu{flags}) : ();
64 32         58 my %flags = map { $_ => 1 } @flags;
  992         1588  
65 32         142 my $up = Unix::Processors->new;
66 32         283 my $name = $cpu{'model name'};
67 32 50       93 $name =~ s[ \s{2,} ][ ]xms if $name;
68              
69             return(
70             processor_id => $cpu{processor},
71             data_width => $flags{lm} ? '64' : '32', # guess
72             address_width => $flags{lm} ? '64' : '32', # guess
73             bus_speed => undef,
74             speed => $cpu{'cpu MHz'},
75             name => $name || q{},
76             family => $cpu{'cpu family'},
77             manufacturer => $cpu{vendor_id},
78             model => $cpu{model},
79             stepping => $cpu{stepping},
80             number_of_cores => $cpu{'cpu cores'} || $up->max_physical,
81             number_of_logical_processors => $up->max_online,
82 32 50 50     876 L2_cache => {max_cache_size => $cpu{'cache size'}},
    50 33        
    50          
83             flags => @flags ? [ @flags ] : undef,
84             );
85             }
86              
87             1;
88              
89             __END__
90              
91             =pod
92              
93             =encoding UTF-8
94              
95             =head1 NAME
96              
97             Sys::Info::Driver::Linux::Device::CPU
98              
99             =head1 VERSION
100              
101             version 0.7905
102              
103             =head1 SYNOPSIS
104              
105             -
106              
107             =head1 DESCRIPTION
108              
109             Identifies the CPU with L<Unix::Processors>, L<POSIX> and C<< /proc >>.
110              
111             =head1 NAME
112              
113             Sys::Info::Driver::Linux::Device::CPU - Linux CPU Device Driver
114              
115             =head1 METHODS
116              
117             =head2 identify
118              
119             See identify in L<Sys::Info::Device::CPU>.
120              
121             =head2 load
122              
123             See load in L<Sys::Info::Device::CPU>.
124              
125             =head2 bitness
126              
127             See bitness in L<Sys::Info::Device::CPU>.
128              
129             =head1 SEE ALSO
130              
131             L<Sys::Info>,
132             L<Sys::Info::Device::CPU>,
133             L<Unix::Processors>, L<POSIX>,
134             proc filesystem.
135              
136             =head1 AUTHOR
137              
138             Burak Gursoy <burak@cpan.org>
139              
140             =head1 COPYRIGHT AND LICENSE
141              
142             This software is copyright (c) 2006 by Burak Gursoy.
143              
144             This is free software; you can redistribute it and/or modify it under
145             the same terms as the Perl 5 programming language system itself.
146              
147             =cut