File Coverage

blib/lib/Sys/Info/Driver/Linux/Device/CPU.pm
Criterion Covered Total %
statement 65 74 87.8
branch 15 32 46.8
condition 5 20 25.0
subroutine 11 11 100.0
pod 3 3 100.0
total 99 140 70.7


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.7911';
3 2     2   224296 use strict;
  2         4  
  2         79  
4 2     2   10 use warnings;
  2         4  
  2         123  
5 2     2   13 use parent qw(Sys::Info::Base);
  2         4  
  2         15  
6              
7 2     2   21021 use Sys::Info::Driver::Linux;
  2         5  
  2         103  
8 2     2   1092 use Unix::Processors;
  2         3140  
  2         70  
9 2     2   872 use POSIX ();
  2         10810  
  2         67  
10 2     2   11 use Carp qw( croak );
  2         4  
  2         2151  
11              
12             sub identify {
13 9     9 1 1751 my $self = shift;
14              
15 9 100       32 if ( ! $self->{META_DATA} ) {
16 2         12 my $mach = $self->uname->{machine};
17 2 50       96 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         13 $self->trim( $self->slurp( proc->{cpuinfo} ) );
25 2         1501 $self->{META_DATA} = [];
26 2         5 my $device_model;
27 2         8 foreach my $e ( @raw ) {
28 26         59 my %i = $self->_parse_cpuinfo($e);
29 26 50       79 if ( $i{__meta_key} ) {
30 0         0 $device_model = $i{Model};
31 0         0 next;
32             }
33 26         29 push @{ $self->{META_DATA} },
  26         222  
34             { %i, architecture => $arch };
35             }
36              
37 2 50       11 if ( $device_model ) {
38 0         0 for my $e ( @{ $self->{META_DATA} } ) {
  0         0  
39 0         0 $e->{__device_model} = $device_model;
40 0   0     0 $e->{model} ||= $device_model;
41             }
42             }
43             }
44              
45 9         28 return $self->_serve_from_cache(wantarray);
46             }
47              
48             sub bitness {
49 1     1 1 236 my $self = shift;
50 1         3 my @cpu = $self->identify;
51 1         10 my $flags = $cpu[0]->{flags};
52 1 50       3 if ( $flags ) {
53 1         2 my $lm = grep { $_ eq 'lm' } @{$flags};
  62         81  
  1         2  
54 1 50       4 return '64' if $lm;
55             }
56 0 0       0 return $cpu[0]->{architecture} =~ m{64}xms ? '64' : '32';
57             }
58              
59             sub load {
60 5     5 1 490 my $self = shift;
61 5         5 my $level = shift;
62 5         12 my @loads = split /\s+/xms, $self->slurp( proc->{loadavg} );
63 5         482 return $loads[$level];
64             }
65              
66             sub _parse_cpuinfo {
67 26     26   33 my $self = shift;
68 26   33     48 my $raw = shift || croak 'Parser called without data';
69 26         51 my($k, $v);
70 26         0 my %cpu;
71 26         136 foreach my $line (split /\n/xms, $raw) {
72 676         1468 ($k, $v) = split /\s+:\s+/xms, $line;
73 676         980 $cpu{$k} = $v;
74             }
75              
76 26 0 33     78 if ( $cpu{Model} && $cpu{Revision} && $cpu{Serial} ) {
      0        
77 0         0 return %cpu, __meta_key => 1;
78             }
79              
80 26 50       233 my @flags = $cpu{flags} ? (split /\s+/xms, $cpu{flags}) : ();
81 26         50 my %flags = map { $_ => 1 } @flags;
  1636         2525  
82 26         223 my $up = Unix::Processors->new;
83 26         235 my $name = $cpu{'model name'};
84 26 50       92 $name =~ s[ \s{2,} ][ ]xms if $name;
85              
86 26         45 my $cpu_freq_file = proc->{scaling_cur_freq};
87              
88 26         34 my $speed = $cpu{'cpu MHz'};
89              
90 26 50 33     49 if ( ! $speed && -e $cpu_freq_file ) {
91 0         0 $speed = $self->trim( $self->slurp( $cpu_freq_file ) );
92             }
93              
94             return(
95             processor_id => $cpu{processor},
96             data_width => $flags{lm} ? '64' : '32', # guess
97             address_width => $flags{lm} ? '64' : '32', # guess
98             bus_speed => undef,
99             speed => $speed,
100             name => $name || q{},
101             family => $cpu{'cpu family'},
102             manufacturer => $cpu{vendor_id},
103             model => $cpu{model},
104             stepping => $cpu{stepping},
105             number_of_cores => $cpu{'cpu cores'} || $up->max_physical,
106             number_of_logical_processors => $up->max_online,
107 26 50 50     1048 L2_cache => {max_cache_size => $cpu{'cache size'}},
    50 33        
    50          
108             flags => @flags ? [ @flags ] : undef,
109             );
110             }
111              
112             1;
113              
114             __END__
115              
116             =pod
117              
118             =encoding UTF-8
119              
120             =head1 NAME
121              
122             Sys::Info::Driver::Linux::Device::CPU
123              
124             =head1 VERSION
125              
126             version 0.7911
127              
128             =head1 SYNOPSIS
129              
130             -
131              
132             =head1 DESCRIPTION
133              
134             Identifies the CPU with L<Unix::Processors>, L<POSIX> and C<< /proc >>.
135              
136             =head1 NAME
137              
138             Sys::Info::Driver::Linux::Device::CPU - Linux CPU Device Driver
139              
140             =head1 METHODS
141              
142             =head2 identify
143              
144             See identify in L<Sys::Info::Device::CPU>.
145              
146             =head2 load
147              
148             See load in L<Sys::Info::Device::CPU>.
149              
150             =head2 bitness
151              
152             See bitness in L<Sys::Info::Device::CPU>.
153              
154             =head1 SEE ALSO
155              
156             L<Sys::Info>,
157             L<Sys::Info::Device::CPU>,
158             L<Unix::Processors>, L<POSIX>,
159             proc filesystem.
160              
161             =head1 AUTHOR
162              
163             Burak Gursoy
164              
165             =head1 COPYRIGHT AND LICENSE
166              
167             This software is copyright (c) 2006 by Burak Gursoy.
168              
169             This is free software; you can redistribute it and/or modify it under
170             the same terms as the Perl 5 programming language system itself.
171              
172             =cut