~ubuntu-branches/ubuntu/trusty/mysql-5.6/trusty

« back to all changes in this revision

Viewing changes to mysql-test/lib/My/SysInfo.pm

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2014-02-12 11:54:27 UTC
  • Revision ID: package-import@ubuntu.com-20140212115427-oq6tfsqxl1wuwehi
Tags: upstream-5.6.15
ImportĀ upstreamĀ versionĀ 5.6.15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- cperl -*-
 
2
# Copyright (c) 2013 MySQL AB, 2008 Sun Microsystems, Inc.
 
3
# Use is subject to license terms.
 
4
#
 
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.
 
8
#
 
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.
 
13
#
 
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
 
17
 
 
18
 
 
19
package My::SysInfo;
 
20
 
 
21
use strict;
 
22
use Carp;
 
23
use My::Platform;
 
24
 
 
25
use constant DEFAULT_BOGO_MIPS => 2000;
 
26
 
 
27
sub _cpuinfo {
 
28
  my ($self)= @_;
 
29
 
 
30
  my $info_file= "/proc/cpuinfo";
 
31
  if ( !(  -e $info_file and -f $info_file) ) {
 
32
    return undef;
 
33
  }
 
34
 
 
35
  my $F= IO::File->new($info_file) or return undef;
 
36
 
 
37
  # Set input separator to blank line
 
38
  local $/ = '';
 
39
 
 
40
  while ( my $cpu_chunk= <$F>) {
 
41
    chomp($cpu_chunk);
 
42
 
 
43
    my $cpuinfo = {};
 
44
 
 
45
    foreach my $cpuline ( split(/\n/, $cpu_chunk) ) {
 
46
      my ( $attribute, $value ) = split(/\s*:\s*/, $cpuline);
 
47
 
 
48
      $attribute =~ s/\s+/_/;
 
49
      $attribute = lc($attribute);
 
50
 
 
51
      if ( $value =~ /^(no|not available|yes)$/ ) {
 
52
        $value = $value eq 'yes' ? 1 : 0;
 
53
      }
 
54
 
 
55
      if ( $attribute eq 'flags' ) {
 
56
        @{ $cpuinfo->{flags} } = split / /, $value;
 
57
      } else {
 
58
        $cpuinfo->{$attribute} = $value;
 
59
      }
 
60
    }
 
61
 
 
62
    # Make sure bogomips is set to some value
 
63
    $cpuinfo->{bogomips} ||= DEFAULT_BOGO_MIPS;
 
64
 
 
65
    # Cpus reported once, but with 'cpu_count' set to the actual number
 
66
    my $cpu_count= $cpuinfo->{cpu_count} || 1;
 
67
    for(1..$cpu_count){
 
68
      push(@{$self->{cpus}}, $cpuinfo);
 
69
    }
 
70
  }
 
71
  $F= undef; # Close file
 
72
  return $self;
 
73
}
 
74
 
 
75
 
 
76
sub _kstat {
 
77
  my ($self)= @_;
 
78
  while (1){
 
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
 
82
 
 
83
    my $cpuinfo= {};
 
84
    foreach my $line (@lines)
 
85
    {
 
86
      my ($module, $instance, $name, $statistic, $value)=
 
87
        $line=~ /(\w*):(\w*):(\w*):(\w*)\t(.*)/;
 
88
 
 
89
      $cpuinfo->{$statistic}= $value;
 
90
    }
 
91
 
 
92
    # Default value, the actual cpu values can be used to decrease this
 
93
    # on slower cpus
 
94
    $cpuinfo->{bogomips}= DEFAULT_BOGO_MIPS;
 
95
 
 
96
    push(@{$self->{cpus}}, $cpuinfo);
 
97
  }
 
98
 
 
99
  # At least one cpu should have been found
 
100
  # if this method worked
 
101
  if ( $self->{cpus} ) {
 
102
    return $self;
 
103
  }
 
104
  return undef;
 
105
}
 
106
 
 
107
 
 
108
sub _unamex {
 
109
  my ($self)= @_;
 
110
  # TODO
 
111
  return undef;
 
112
}
 
113
 
 
114
 
 
115
sub new {
 
116
  my ($class)= @_;
 
117
 
 
118
 
 
119
  my $self= bless {
 
120
                   cpus => (),
 
121
                  }, $class;
 
122
 
 
123
  my @info_methods =
 
124
    (
 
125
     \&_cpuinfo,
 
126
     \&_kstat,
 
127
     \&_unamex,
 
128
   );
 
129
 
 
130
  # Detect virtual machines
 
131
  my $isvm= 0;
 
132
 
 
133
  if (IS_WINDOWS) {
 
134
    # Detect vmware service
 
135
    $isvm= `tasklist` =~ /vmwareservice/i;
 
136
  }
 
137
  $self->{isvm}= $isvm;
 
138
 
 
139
  foreach my $method (@info_methods){
 
140
    if ($method->($self)){
 
141
      return $self;
 
142
    }
 
143
  }
 
144
 
 
145
  # Push a dummy cpu
 
146
  push(@{$self->{cpus}},
 
147
     {
 
148
      bogomips => DEFAULT_BOGO_MIPS,
 
149
      model_name => "unknown",
 
150
     });
 
151
 
 
152
  return $self;
 
153
}
 
154
 
 
155
 
 
156
# Return the list of cpus found
 
157
sub cpus {
 
158
  my ($self)= @_;
 
159
  return @{$self->{cpus}} or
 
160
    confess "INTERNAL ERROR: No cpus in list";
 
161
}
 
162
 
 
163
 
 
164
# Return the number of cpus found
 
165
sub num_cpus {
 
166
  my ($self)= @_;
 
167
  return int(@{$self->{cpus}}) or
 
168
    confess "INTERNAL ERROR: No cpus in list";
 
169
}
 
170
 
 
171
 
 
172
# Return the smallest bogomips value amongst the processors
 
173
sub min_bogomips {
 
174
  my ($self)= @_;
 
175
 
 
176
  my $bogomips;
 
177
 
 
178
  foreach my $cpu (@{$self->{cpus}}) {
 
179
    if (!defined $bogomips or $bogomips > $cpu->{bogomips}) {
 
180
      $bogomips= $cpu->{bogomips};
 
181
    }
 
182
  }
 
183
 
 
184
  return $bogomips;
 
185
}
 
186
 
 
187
sub isvm {
 
188
  my ($self)= @_;
 
189
 
 
190
  return $self->{isvm};
 
191
}
 
192
 
 
193
 
 
194
# Prit the cpuinfo
 
195
sub print_info {
 
196
  my ($self)= @_;
 
197
 
 
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), "]";
 
203
      } else {
 
204
        print $value;
 
205
      }
 
206
      print "\n";
 
207
    }
 
208
    print "\n";
 
209
  }
 
210
}
 
211
 
 
212
1;