sys-cpu 1.0.3 → 1.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/CHANGES.md +21 -0
- data/Gemfile +2 -7
- data/README.md +21 -14
- data/Rakefile +4 -1
- data/doc/bsd.md +58 -0
- data/doc/linux.md +46 -0
- data/install.rb +2 -2
- data/lib/sys/cpu.rb +8 -1
- data/lib/sys/darwin/sys/cpu.rb +54 -8
- data/lib/sys/linux/sys/cpu.rb +33 -31
- data/lib/sys/unix/sys/cpu.rb +25 -33
- data/lib/sys/windows/sys/cpu.rb +244 -143
- data/lib/sys-cpu.rb +2 -0
- data/spec/spec_helper.rb +4 -0
- data/spec/sys_cpu_bsd_spec.rb +70 -51
- data/spec/sys_cpu_hpux_spec.rb +33 -28
- data/spec/sys_cpu_linux_spec.rb +32 -26
- data/spec/{sys_cpu_spec.rb → sys_cpu_shared.rb} +10 -4
- data/spec/sys_cpu_sunos_spec.rb +44 -42
- data/spec/sys_cpu_windows_spec.rb +39 -34
- data/sys-cpu.gemspec +10 -9
- data.tar.gz.sig +0 -0
- metadata +40 -11
- metadata.gz.sig +0 -0
- data/doc/bsd.txt +0 -49
- data/doc/linux.txt +0 -41
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 240d4766057d3b5e6099092e037a858a527598955f303a6b1e958e78dd55f10e
|
4
|
+
data.tar.gz: 9a1041ac81a63a1205d28d94c59373a5868f17eb9e46b1cf4226f3488f7dafd9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9bb252c92d2460f38fdf65656c2d3875c55b6d3d2ec73863be3fcdd878d3d66a49a33f9a9d1d49ae65aa4c8d3d5a405bac2aa3bdaaa4edbc5d6854e12e6a29af
|
7
|
+
data.tar.gz: 993afd63ee978e9bd24682309221f9f4fbddb177f8ed748b9cf512317bf72ea5243b529d114e9407fbf198f00c685fc1a6a0afc4646e2230acce9a5dce8d0d08
|
checksums.yaml.gz.sig
CHANGED
Binary file
|
data/CHANGES.md
CHANGED
@@ -1,3 +1,24 @@
|
|
1
|
+
## 1.0.6 - 12-Aug-2022
|
2
|
+
* The Sys::CPU.model method will now return "ARM" for machines using an
|
3
|
+
ARM-based processor instead of "Uknown".
|
4
|
+
|
5
|
+
## 1.0.5 - 10-Aug-2022
|
6
|
+
* Updated the cpu detection handling for Mac M1 systems. Thanks go to
|
7
|
+
Julien W for the spot.
|
8
|
+
|
9
|
+
## 1.0.4 - 10-Jun-2022
|
10
|
+
* The OSX code for the CPU.freq method was updated for arm64 systems.
|
11
|
+
* Some refactoring to the specs and uses shared examples now.
|
12
|
+
* Now makes the new method a private class method. The constructor was never
|
13
|
+
meant to be used with this library, so now it's explicitly forbidden.
|
14
|
+
* Added rubocop and rubocop-rspec as development dependencies, as well as
|
15
|
+
a rubocop rake task, and applied some suggested changes.
|
16
|
+
* Fixed the global Gemfile source issue. Just use the gemspec.
|
17
|
+
* Added some new cpu families for Windows.
|
18
|
+
* Added a respond_to_missing? method to the Linux version since it uses
|
19
|
+
method_missing.
|
20
|
+
* The MS Windows version now assumes Ruby 2.0 or later.
|
21
|
+
|
1
22
|
## 1.0.3 - 28-Jan-2021
|
2
23
|
* The code for OSX was split out into its own source file. This was partly for
|
3
24
|
ease of maintenance, but also because there was confusion with the
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,3 +1,9 @@
|
|
1
|
+
[![Ruby](https://github.com/djberg96/sys-cpu/actions/workflows/ruby.yml/badge.svg)](https://github.com/djberg96/sys-cpu/actions/workflows/ruby.yml)
|
2
|
+
|
3
|
+
* Linux
|
4
|
+
* Windows
|
5
|
+
* OSX
|
6
|
+
|
1
7
|
## Description
|
2
8
|
A Ruby interface for getting cpu information.
|
3
9
|
|
@@ -5,15 +11,16 @@ A Ruby interface for getting cpu information.
|
|
5
11
|
`gem install sys-cpu`
|
6
12
|
|
7
13
|
## Adding the trusted cert
|
8
|
-
`gem cert --add <(curl -Ls https://raw.githubusercontent.com/djberg96/sys-cpu/
|
14
|
+
`gem cert --add <(curl -Ls https://raw.githubusercontent.com/djberg96/sys-cpu/main/certs/djberg96_pub.pem)`
|
9
15
|
|
10
16
|
## Notes
|
11
17
|
### Solaris
|
12
|
-
|
13
|
-
|
18
|
+
There is no `processors` iterative method for multi-cpu systems. I was going to
|
19
|
+
add this originally, but since Solaris is basically dead at this point I've
|
20
|
+
dropped the idea.
|
14
21
|
|
15
22
|
### OS X
|
16
|
-
The CPU.model method returns very limited information. I do not yet know
|
23
|
+
The `CPU.model` method returns very limited information. I do not yet know
|
17
24
|
how to get more detailed information.
|
18
25
|
|
19
26
|
### Linux
|
@@ -21,14 +28,14 @@ This is pure Ruby. This version reads information out of /proc/cpuinfo and
|
|
21
28
|
/proc/loadavg, so if /proc isn't mounted it won't work.
|
22
29
|
|
23
30
|
The key-value information in /proc/cpuinfo is stored internally (i.e. in
|
24
|
-
memory) as an array of hashes when you first
|
31
|
+
memory) as an array of hashes when you first `require` this package. This
|
25
32
|
overhead is exceptionally minimal, given that your average cpuinfo file
|
26
33
|
contains less than 1k of text (and I don't store whitespace or newlines).
|
27
34
|
|
28
35
|
The text documentation for Linux is dynamically generated during the
|
29
36
|
build process because the fields vary depending on your setup. So, don't
|
30
|
-
look at it until *after* you've installed it.
|
31
|
-
file after you run
|
37
|
+
look at it until *after* you've installed it. You will see a doc/linux.txt
|
38
|
+
file after you run `rake install` (via install.rb).
|
32
39
|
|
33
40
|
### HP-UX
|
34
41
|
Unlike other platforms, you can get load averages for an individual cpu in
|
@@ -42,10 +49,10 @@ will work just fine.
|
|
42
49
|
This is a pure Ruby implementation using the win32ole package + WMI. The C
|
43
50
|
version has been scrapped.
|
44
51
|
|
45
|
-
As of version 0.5.0, the CPU.usage method has been removed in favor of the
|
46
|
-
CPU.load_avg method. This does not (currently) use a perf counter, so there
|
47
|
-
is no longer any delay. Also, the
|
48
|
-
|
52
|
+
As of version 0.5.0, the `CPU.usage` method has been removed in favor of the
|
53
|
+
`CPU.load_avg` method. This does not (currently) use a perf counter, so there
|
54
|
+
is no longer any delay. Also, the `processors` method has been added and the
|
55
|
+
`supported` method has been dropped. See the documentation for other changes.
|
49
56
|
|
50
57
|
## Acknowledgements
|
51
58
|
Thanks go to the MPlayer team for some source code that helped me on
|
@@ -57,14 +64,14 @@ None that I'm aware of. Please report bugs on the project page at:
|
|
57
64
|
https://github.com/djberg96/sys-cpu
|
58
65
|
|
59
66
|
## Future Plans
|
60
|
-
* Add iterative CPU.processors method.
|
61
|
-
* Add more information in general, such as what
|
67
|
+
* Add iterative `CPU.processors` method.
|
68
|
+
* Add more information in general, such as what `prtdiag` shows.
|
62
69
|
|
63
70
|
## License
|
64
71
|
Apache-2.0
|
65
72
|
|
66
73
|
## Copyright
|
67
|
-
(C) 2003-
|
74
|
+
(C) 2003-2022 Daniel J. Berger, All Rights Reserved
|
68
75
|
|
69
76
|
## Warranty
|
70
77
|
This package is provided "as is" and without any express or
|
data/Rakefile
CHANGED
@@ -2,6 +2,7 @@ require 'rake'
|
|
2
2
|
require 'rake/clean'
|
3
3
|
require 'rbconfig'
|
4
4
|
require 'rspec/core/rake_task'
|
5
|
+
require 'rubocop/rake_task'
|
5
6
|
include RbConfig
|
6
7
|
|
7
8
|
CLEAN.include('**/*.gem', '**/*.rbc', '**/*.rbx', '**/*.lock')
|
@@ -10,7 +11,7 @@ namespace 'gem' do
|
|
10
11
|
desc "Create the sys-cpu gem"
|
11
12
|
task :create => [:clean] do
|
12
13
|
require 'rubygems/package'
|
13
|
-
spec =
|
14
|
+
spec = Gem::Specification.load('sys-cpu.gemspec')
|
14
15
|
spec.signing_key = File.join(Dir.home, '.ssh', 'gem-private_key.pem')
|
15
16
|
Gem::Package.build(spec)
|
16
17
|
end
|
@@ -44,6 +45,8 @@ task :example => [:clean] do
|
|
44
45
|
|
45
46
|
end
|
46
47
|
|
48
|
+
RuboCop::RakeTask.new
|
49
|
+
|
47
50
|
desc "Run the test suite"
|
48
51
|
RSpec::Core::RakeTask.new(:spec)
|
49
52
|
|
data/doc/bsd.md
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
## Description
|
2
|
+
|
3
|
+
A Ruby interface for various cpu statistics
|
4
|
+
|
5
|
+
## Synopsis
|
6
|
+
```ruby
|
7
|
+
require 'sys/cpu' # or 'sys-cpu'
|
8
|
+
|
9
|
+
# BSD and OS X
|
10
|
+
puts "Architecture: " + Sys::CPU.architecture
|
11
|
+
puts "Machine: " + Sys::CPU.machine
|
12
|
+
puts "Mhz: " + Sys::CPU.cpu_freq.to_s
|
13
|
+
puts "Number of cpu's on this system: " + Sys::CPU.num_cpu.to_s
|
14
|
+
puts "CPU model: " + Sys::CPU.model
|
15
|
+
puts "Load averages: " + Sys::CPU.load_avg.join(", ")
|
16
|
+
```
|
17
|
+
|
18
|
+
## Constants
|
19
|
+
`VERSION`
|
20
|
+
|
21
|
+
Returns the current version number for this library.
|
22
|
+
|
23
|
+
## Singleton Methods
|
24
|
+
|
25
|
+
`CPU.architecture`
|
26
|
+
|
27
|
+
Returns the cpu's architecture, e.g. "x86_64".
|
28
|
+
|
29
|
+
`CPU.freq`
|
30
|
+
|
31
|
+
Returns an integer indicating the speed (i.e. frequency in Mhz) of
|
32
|
+
the cpu.
|
33
|
+
|
34
|
+
`CPU.load_avg`
|
35
|
+
|
36
|
+
Returns an array of three floats indicating the 1, 5 and 15 minute load
|
37
|
+
average.
|
38
|
+
|
39
|
+
`CPU.machine`
|
40
|
+
|
41
|
+
Returns the class of cpu (probably identical to the architecture).
|
42
|
+
|
43
|
+
`CPU.model`
|
44
|
+
|
45
|
+
Returns a string indicating the cpu model, e.g. "Intel".
|
46
|
+
|
47
|
+
`CPU.num_cpu`
|
48
|
+
|
49
|
+
Returns an integer indicating the number of cpu's on the system.
|
50
|
+
|
51
|
+
## Error Classes
|
52
|
+
`CPU::Error < StandardError`
|
53
|
+
|
54
|
+
Raised is response to internal function errors, usually relating to an
|
55
|
+
invalid cpu number.
|
56
|
+
|
57
|
+
## More Information
|
58
|
+
See the `README.md` file for more information.
|
data/doc/linux.md
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
## Description
|
2
|
+
Sys::CPU - An interface for various cpu statistics
|
3
|
+
|
4
|
+
## Synopsis
|
5
|
+
```ruby
|
6
|
+
require 'sys-cpu' # Or "sys/cpu"
|
7
|
+
|
8
|
+
Sys::CPU.processors{ |cs|
|
9
|
+
cs.members.each{ |m|
|
10
|
+
puts "#{m}: " + cs[m].to_s
|
11
|
+
}
|
12
|
+
}
|
13
|
+
|
14
|
+
Sys::CPU.bogomips(1) # -> returns bogomips for cpu #2
|
15
|
+
```
|
16
|
+
|
17
|
+
## Notes
|
18
|
+
|
19
|
+
Portions of this documentation were built dynamically.
|
20
|
+
|
21
|
+
## Constants
|
22
|
+
|
23
|
+
VERSION
|
24
|
+
|
25
|
+
Returns the current version number for this library as a string.
|
26
|
+
|
27
|
+
## Class Methods
|
28
|
+
`CPU.load_avg`
|
29
|
+
|
30
|
+
Returns an array of three floats indicating the 1, 5 and 15 minute load average.
|
31
|
+
|
32
|
+
`CPU.cpu_stats`
|
33
|
+
|
34
|
+
Returns a hash, with the cpu number as the key and an array as the value.
|
35
|
+
The array contains the number of seconds that the system spent in
|
36
|
+
user mode, user mode with low priority (nice), system mode, and the
|
37
|
+
idle task, respectively, for that cpu.
|
38
|
+
|
39
|
+
`CPU.processors{ |cpu_struct| ... }`
|
40
|
+
|
41
|
+
Calls the block for each processor on your system, yielding a `CPUStruct` to the block.
|
42
|
+
|
43
|
+
The exact members of the `CPUStruct` are the same as the singleton method names, except
|
44
|
+
for `Sys::CPU.processors` (although you may optionally omit the "?" when referring to a
|
45
|
+
struct member). These were determined when you installed this library because they
|
46
|
+
vary from one chip architecture to another.
|
data/install.rb
CHANGED
@@ -31,7 +31,7 @@ end
|
|
31
31
|
#######################################################################
|
32
32
|
if CONFIG['host_os'] =~ /linux/
|
33
33
|
cpu_file = "/proc/cpuinfo"
|
34
|
-
text_file = "doc/linux.
|
34
|
+
text_file = "doc/linux.md"
|
35
35
|
rb_file = "lib/linux/sys/cpu.rb"
|
36
36
|
|
37
37
|
if File.size(text_file) > 1400
|
@@ -44,7 +44,7 @@ if CONFIG['host_os'] =~ /linux/
|
|
44
44
|
IO.foreach(cpu_file){ |line|
|
45
45
|
next if line =~ /^$/
|
46
46
|
k,v = line.split(":")
|
47
|
-
|
47
|
+
|
48
48
|
v = v.strip.chomp
|
49
49
|
k = k.strip.gsub(/\s+/, '_').downcase
|
50
50
|
|
data/lib/sys/cpu.rb
CHANGED
@@ -1,11 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# This is just a stub file that requires the appropriate version
|
2
4
|
# depending on which platform you're on.
|
3
5
|
require 'rbconfig'
|
4
6
|
|
7
|
+
# The Sys module is a namespace only.
|
5
8
|
module Sys
|
9
|
+
# The CPU class encapsulates information about the physical cpu's on your system.
|
10
|
+
# This class is reopened for each of the supported platforms/operating systems.
|
6
11
|
class CPU
|
7
12
|
# The version of the sys-cpu gem.
|
8
|
-
VERSION = '1.0.
|
13
|
+
VERSION = '1.0.6'
|
14
|
+
|
15
|
+
private_class_method :new
|
9
16
|
end
|
10
17
|
end
|
11
18
|
|
data/lib/sys/darwin/sys/cpu.rb
CHANGED
@@ -1,7 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'ffi'
|
2
4
|
require 'rbconfig'
|
3
5
|
|
6
|
+
# The Sys module serves as a namespace only.
|
4
7
|
module Sys
|
8
|
+
# The CPU class encapsulates information about the physical cpu's on your system.
|
5
9
|
class CPU
|
6
10
|
extend FFI::Library
|
7
11
|
ffi_lib FFI::Library::LIBC
|
@@ -11,16 +15,22 @@ module Sys
|
|
11
15
|
|
12
16
|
CTL_HW = 6 # Generic hardware/cpu
|
13
17
|
|
18
|
+
private_constant :CTL_HW
|
19
|
+
|
14
20
|
HW_MACHINE = 1 # Machine class
|
15
21
|
HW_MODEL = 2 # Specific machine model
|
16
22
|
HW_NCPU = 3 # Number of CPU's
|
17
23
|
HW_CPU_FREQ = 15 # CPU frequency
|
18
24
|
HW_MACHINE_ARCH = 12 # Machine architecture
|
19
25
|
|
26
|
+
private_constant :HW_MACHINE, :HW_MODEL, :HW_NCPU, :HW_CPU_FREQ, :HW_MACHINE_ARCH
|
27
|
+
|
20
28
|
SI_MACHINE = 5
|
21
29
|
SI_ARCHITECTURE = 6
|
22
30
|
SC_NPROCESSORS_ONLN = 15
|
23
31
|
|
32
|
+
private_constant :SI_MACHINE, :SI_ARCHITECTURE, :SC_NPROCESSORS_ONLN
|
33
|
+
|
24
34
|
P_OFFLINE = 1
|
25
35
|
P_ONLINE = 2
|
26
36
|
P_FAULTED = 4
|
@@ -28,16 +38,23 @@ module Sys
|
|
28
38
|
P_NOINTR = 6
|
29
39
|
P_SPARE = 7
|
30
40
|
|
41
|
+
private_constant :P_OFFLINE, :P_ONLINE, :P_FAULTED, :P_POWEROFF, :P_NOINTR, :P_SPARE
|
42
|
+
|
31
43
|
CPU_ARCH_ABI64 = 0x01000000
|
32
44
|
CPU_TYPE_X86 = 7
|
33
45
|
CPU_TYPE_X86_64 = (CPU_TYPE_X86 | CPU_ARCH_ABI64)
|
46
|
+
CPU_TYPE_ARM = 12
|
34
47
|
CPU_TYPE_SPARC = 14
|
35
48
|
CPU_TYPE_POWERPC = 18
|
36
49
|
CPU_TYPE_POWERPC64 = CPU_TYPE_POWERPC | CPU_ARCH_ABI64
|
50
|
+
CPU_TYPE_ARM64 = CPU_TYPE_ARM | CPU_ARCH_ABI64
|
51
|
+
|
52
|
+
private_constant :CPU_ARCH_ABI64, :CPU_TYPE_X86, :CPU_TYPE_X86_64, :CPU_TYPE_ARM
|
53
|
+
private_constant :CPU_TYPE_SPARC, :CPU_TYPE_POWERPC, :CPU_TYPE_POWERPC64
|
37
54
|
|
38
55
|
attach_function(
|
39
56
|
:sysctl,
|
40
|
-
[
|
57
|
+
%i[pointer uint pointer pointer pointer size_t],
|
41
58
|
:int
|
42
59
|
)
|
43
60
|
|
@@ -45,20 +62,32 @@ module Sys
|
|
45
62
|
|
46
63
|
attach_function(
|
47
64
|
:sysctlbyname,
|
48
|
-
[
|
65
|
+
%i[string pointer pointer pointer size_t],
|
49
66
|
:int
|
50
67
|
)
|
51
68
|
|
52
69
|
private_class_method :sysctlbyname
|
53
70
|
|
54
|
-
attach_function :getloadavg, [
|
55
|
-
attach_function :processor_info, [
|
71
|
+
attach_function :getloadavg, %i[pointer int], :int
|
72
|
+
attach_function :processor_info, %i[int int string pointer pointer], :int
|
56
73
|
attach_function :sysconf, [:int], :long
|
57
74
|
|
58
75
|
private_class_method :getloadavg
|
59
76
|
private_class_method :processor_info
|
60
77
|
private_class_method :sysconf
|
61
78
|
|
79
|
+
class ClockInfo < FFI::Struct
|
80
|
+
layout(
|
81
|
+
:hz, :int,
|
82
|
+
:tick, :int,
|
83
|
+
:spare, :int,
|
84
|
+
:stathz, :int,
|
85
|
+
:profhz, :int
|
86
|
+
)
|
87
|
+
end
|
88
|
+
|
89
|
+
private_constant :ClockInfo
|
90
|
+
|
62
91
|
# Returns the cpu's architecture. On most systems this will be identical
|
63
92
|
# to the CPU.machine method. On OpenBSD it will be identical to the CPU.model
|
64
93
|
# method.
|
@@ -131,6 +160,8 @@ module Sys
|
|
131
160
|
'Sparc'
|
132
161
|
when CPU_TYPE_POWERPC, CPU_TYPE_POWERPC64
|
133
162
|
'PowerPC'
|
163
|
+
when CPU_TYPE_ARM, CPU_TYPE_ARM64
|
164
|
+
'ARM'
|
134
165
|
else
|
135
166
|
'Unknown'
|
136
167
|
end
|
@@ -144,11 +175,26 @@ module Sys
|
|
144
175
|
|
145
176
|
size.write_long(optr.size)
|
146
177
|
|
147
|
-
if
|
148
|
-
|
178
|
+
if RbConfig::CONFIG['host_cpu'] =~ /^arm|^aarch/i
|
179
|
+
if sysctlbyname('hw.tbfrequency', optr, size, nil, 0) < 0
|
180
|
+
raise Error, 'sysctlbyname failed on hw.tbfrequency'
|
181
|
+
end
|
182
|
+
|
183
|
+
size.clear
|
184
|
+
clock = ClockInfo.new
|
185
|
+
size.write_long(clock.size)
|
186
|
+
|
187
|
+
if sysctlbyname('kern.clockrate', clock, size, nil, 0) < 0
|
188
|
+
raise Error, 'sysctlbyname failed on kern.clockrate'
|
189
|
+
end
|
190
|
+
|
191
|
+
(optr.read_long * clock[:hz]) / 1_000_000
|
192
|
+
else
|
193
|
+
if sysctlbyname('hw.cpufrequency', optr, size, nil, 0) < 0
|
194
|
+
raise Error, 'sysctlbyname failed on hw.cpufrequency' if result < 0
|
195
|
+
end
|
196
|
+
optr.read_long / 1_000_000
|
149
197
|
end
|
150
|
-
|
151
|
-
optr.read_long / 1000000
|
152
198
|
end
|
153
199
|
|
154
200
|
# Returns an array of three floats indicating the 1, 5 and 15 minute load
|
data/lib/sys/linux/sys/cpu.rb
CHANGED
@@ -1,48 +1,48 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
##########################################################
|
2
4
|
# linux.rb (sys-cpu) - pure Ruby version for Linux
|
3
5
|
##########################################################
|
4
|
-
module Sys
|
5
6
|
|
7
|
+
# The Sys module is a namespace only.
|
8
|
+
module Sys
|
6
9
|
# :stopdoc:
|
7
10
|
|
8
|
-
cpu_file
|
9
|
-
cpu_hash
|
11
|
+
cpu_file = '/proc/cpuinfo'
|
12
|
+
cpu_hash = {}
|
10
13
|
CPU_ARRAY = []
|
11
14
|
|
12
15
|
private_constant :CPU_ARRAY
|
13
16
|
|
14
17
|
# Parse the info out of the /proc/cpuinfo file
|
15
|
-
|
18
|
+
File.foreach(cpu_file) do |line|
|
16
19
|
line.strip!
|
17
20
|
next if line.empty?
|
18
21
|
|
19
22
|
key, val = line.split(':')
|
20
23
|
key.strip!
|
21
|
-
key.gsub!(/\s+/,'_')
|
24
|
+
key.gsub!(/\s+/, '_')
|
22
25
|
key.downcase!
|
23
26
|
val.strip! if val
|
24
27
|
|
25
|
-
if cpu_hash.
|
28
|
+
if cpu_hash.key?(key)
|
26
29
|
CPU_ARRAY.push(cpu_hash.dup)
|
27
30
|
cpu_hash.clear
|
28
31
|
end
|
29
32
|
|
30
33
|
# Turn yes/no attributes into booleans
|
31
|
-
if val == 'yes'
|
32
|
-
|
33
|
-
elsif val == 'no'
|
34
|
-
val = false
|
35
|
-
end
|
34
|
+
val = true if val == 'yes'
|
35
|
+
val = false if val == 'no'
|
36
36
|
|
37
37
|
cpu_hash[key] = val
|
38
|
-
|
38
|
+
end
|
39
39
|
|
40
40
|
CPU_ARRAY.push(cpu_hash)
|
41
41
|
|
42
42
|
# :startdoc:
|
43
43
|
|
44
|
+
# The CPU class encapsulates information about physical CPUs on your system.
|
44
45
|
class CPU
|
45
|
-
|
46
46
|
# :stopdoc:
|
47
47
|
|
48
48
|
CPUStruct = Struct.new('CPUStruct', *CPU_ARRAY.first.keys)
|
@@ -58,15 +58,15 @@ module Sys
|
|
58
58
|
#
|
59
59
|
def self.processors
|
60
60
|
array = []
|
61
|
-
CPU_ARRAY.each
|
61
|
+
CPU_ARRAY.each do |hash|
|
62
62
|
struct = CPUStruct.new
|
63
|
-
struct.members.each{ |m| struct.send("#{m}=", hash[
|
63
|
+
struct.members.each{ |m| struct.send("#{m}=", hash[m.to_s]) }
|
64
64
|
if block_given?
|
65
65
|
yield struct
|
66
66
|
else
|
67
67
|
array << struct
|
68
68
|
end
|
69
|
-
|
69
|
+
end
|
70
70
|
array unless block_given?
|
71
71
|
end
|
72
72
|
|
@@ -80,12 +80,10 @@ module Sys
|
|
80
80
|
#
|
81
81
|
def self.architecture
|
82
82
|
case CPU_ARRAY.first['cpu_family']
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
else
|
88
|
-
nil
|
83
|
+
when '3'
|
84
|
+
'x86'
|
85
|
+
when '6'
|
86
|
+
'x86_64'
|
89
87
|
end
|
90
88
|
end
|
91
89
|
|
@@ -103,16 +101,20 @@ module Sys
|
|
103
101
|
|
104
102
|
# Create singleton methods for each of the attributes.
|
105
103
|
#
|
106
|
-
def self.method_missing(id, arg=0)
|
107
|
-
raise NoMethodError, "'#{id}'" unless CPU_ARRAY[arg].
|
104
|
+
def self.method_missing(id, arg = 0)
|
105
|
+
raise NoMethodError, "'#{id}'" unless CPU_ARRAY[arg].key?(id.to_s)
|
108
106
|
rv = CPU_ARRAY[arg][id.to_s]
|
109
107
|
if rv.nil?
|
110
|
-
id = id
|
108
|
+
id = "#{id}?"
|
111
109
|
rv = CPU_ARRAY[arg][id]
|
112
110
|
end
|
113
111
|
rv
|
114
112
|
end
|
115
113
|
|
114
|
+
def self.respond_to_missing?(method, _private_methods = false)
|
115
|
+
CPU_ARRAY.first.keys.include?(method.to_s)
|
116
|
+
end
|
117
|
+
|
116
118
|
private_class_method :method_missing
|
117
119
|
|
118
120
|
# Returns a 3 element Array corresponding to the 1, 5 and 15 minute
|
@@ -120,7 +122,7 @@ module Sys
|
|
120
122
|
#
|
121
123
|
def self.load_avg
|
122
124
|
load_avg_file = '/proc/loadavg'
|
123
|
-
|
125
|
+
File.readlines(load_avg_file).first.split[0..2].map(&:to_f)
|
124
126
|
end
|
125
127
|
|
126
128
|
# Returns a hash of arrays that contains an array of the following
|
@@ -143,21 +145,21 @@ module Sys
|
|
143
145
|
cpu_stat_file = '/proc/stat'
|
144
146
|
hash = {} # Hash needed for multi-cpu systems
|
145
147
|
|
146
|
-
lines =
|
148
|
+
lines = File.readlines(cpu_stat_file)
|
147
149
|
|
148
|
-
lines.each_with_index
|
150
|
+
lines.each_with_index do |line, i|
|
149
151
|
array = line.split
|
150
|
-
break unless array[0] =~ /cpu/
|
152
|
+
break unless array[0] =~ /cpu/ # 'cpu' entries always on top
|
151
153
|
|
152
154
|
# Some machines list a 'cpu' and a 'cpu0'. In this case only
|
153
155
|
# return values for the numbered cpu entry.
|
154
|
-
if lines[i].split[0] == 'cpu' && lines[i+1].split[0] =~ /cpu\d/
|
156
|
+
if lines[i].split[0] == 'cpu' && lines[i + 1].split[0] =~ /cpu\d/
|
155
157
|
next
|
156
158
|
end
|
157
159
|
|
158
160
|
vals = array[1..-1].map{ |e| e.to_i / 100 } # 100 jiffies/sec.
|
159
161
|
hash[array[0]] = vals
|
160
|
-
|
162
|
+
end
|
161
163
|
|
162
164
|
hash
|
163
165
|
end
|