2
# Copyright (c) 2013 MySQL AB, 2008 Sun Microsystems, Inc.
3
# Use is subject to license terms.
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; version 2 of the License.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
use constant DEFAULT_BOGO_MIPS => 2000;
30
my $info_file= "/proc/cpuinfo";
31
if ( !( -e $info_file and -f $info_file) ) {
35
my $F= IO::File->new($info_file) or return undef;
37
# Set input separator to blank line
40
while ( my $cpu_chunk= <$F>) {
45
foreach my $cpuline ( split(/\n/, $cpu_chunk) ) {
46
my ( $attribute, $value ) = split(/\s*:\s*/, $cpuline);
48
$attribute =~ s/\s+/_/;
49
$attribute = lc($attribute);
51
if ( $value =~ /^(no|not available|yes)$/ ) {
52
$value = $value eq 'yes' ? 1 : 0;
55
if ( $attribute eq 'flags' ) {
56
@{ $cpuinfo->{flags} } = split / /, $value;
58
$cpuinfo->{$attribute} = $value;
62
# Make sure bogomips is set to some value
63
$cpuinfo->{bogomips} ||= DEFAULT_BOGO_MIPS;
65
# Cpus reported once, but with 'cpu_count' set to the actual number
66
my $cpu_count= $cpuinfo->{cpu_count} || 1;
68
push(@{$self->{cpus}}, $cpuinfo);
71
$F= undef; # Close file
79
my $instance_num= $self->{cpus} ? @{$self->{cpus}} : 0;
80
my $list= `kstat -p -m cpu_info -i $instance_num 2> /dev/null`;
81
my @lines= split('\n', $list) or last; # Break loop
84
foreach my $line (@lines)
86
my ($module, $instance, $name, $statistic, $value)=
87
$line=~ /(\w*):(\w*):(\w*):(\w*)\t(.*)/;
89
$cpuinfo->{$statistic}= $value;
92
# Default value, the actual cpu values can be used to decrease this
94
$cpuinfo->{bogomips}= DEFAULT_BOGO_MIPS;
96
push(@{$self->{cpus}}, $cpuinfo);
99
# At least one cpu should have been found
100
# if this method worked
101
if ( $self->{cpus} ) {
130
# Detect virtual machines
134
# Detect vmware service
135
$isvm= `tasklist` =~ /vmwareservice/i;
137
$self->{isvm}= $isvm;
139
foreach my $method (@info_methods){
140
if ($method->($self)){
146
push(@{$self->{cpus}},
148
bogomips => DEFAULT_BOGO_MIPS,
149
model_name => "unknown",
156
# Return the list of cpus found
159
return @{$self->{cpus}} or
160
confess "INTERNAL ERROR: No cpus in list";
164
# Return the number of cpus found
167
return int(@{$self->{cpus}}) or
168
confess "INTERNAL ERROR: No cpus in list";
172
# Return the smallest bogomips value amongst the processors
178
foreach my $cpu (@{$self->{cpus}}) {
179
if (!defined $bogomips or $bogomips > $cpu->{bogomips}) {
180
$bogomips= $cpu->{bogomips};
190
return $self->{isvm};
198
foreach my $cpu (@{$self->{cpus}}) {
199
while ((my ($key, $value)) = each(%$cpu)) {
200
print " ", $key, "= ";
201
if (ref $value eq "ARRAY") {
202
print "[", join(", ", @$value), "]";