sys-cpu 1.0.3 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8f9a7931b9cc69f3f9725734f85a34fd5ca077226a52f22bdef4fd9f298813c7
4
- data.tar.gz: b7fc906ea6420ed2dde2c9bc546cd5a782e6dc4f3527eb60ac7e5ee9b2cc002e
3
+ metadata.gz: 7624e670375df3b2fd1055c43ac391b592c9288462fbaf2687b3c94d149434e2
4
+ data.tar.gz: d9b4acf3a644bed915050950bbdd19136700ddc544f7b2de06a1a54af3910411
5
5
  SHA512:
6
- metadata.gz: 07a5729274191c98a35515d26b04906222504a8f9a27d551a604998fad5cf40596a240d90434ec0706e54e7ac27d211d222cac122ce101b9d72e29657eda1990
7
- data.tar.gz: 44068c3fa47b3b9af47485133cd52e8d5bfef6638fbfd54a1e21822b1f8617343c4156d2ce17f3f2a46987cdbc2309fecfbacc33f582bb74ffe553229bd7d0ba
6
+ metadata.gz: f4ff44d8420830dd6cc1da28a8c016587226a38870612c3858dc4fee9f9c2038615c2cc7f24160a00467f9bde67ac45a174d5dff80d7b22682dd7399969ce061
7
+ data.tar.gz: a2c59fd8f0243d97fdddcb8ea333a27a71028c19218fc4ebedde57b209d5f6f970f1e21c3798fbcaacc387becfd1895cf78b2b3e035f8f2c50778e31a7a8d808
checksums.yaml.gz.sig CHANGED
Binary file
data/CHANGES.md CHANGED
@@ -1,3 +1,16 @@
1
+ ## 1.0.4 - 10-Jun-2022
2
+ * The OSX code for the CPU.freq method was updated for arm64 systems.
3
+ * Some refactoring to the specs and uses shared examples now.
4
+ * Now makes the new method a private class method. The constructor was never
5
+ meant to be used with this library, so now it's explicitly forbidden.
6
+ * Added rubocop and rubocop-rspec as development dependencies, as well as
7
+ a rubocop rake task, and applied some suggested changes.
8
+ * Fixed the global Gemfile source issue. Just use the gemspec.
9
+ * Added some new cpu families for Windows.
10
+ * Added a respond_to_missing? method to the Linux version since it uses
11
+ method_missing.
12
+ * The MS Windows version now assumes Ruby 2.0 or later.
13
+
1
14
  ## 1.0.3 - 28-Jan-2021
2
15
  * The code for OSX was split out into its own source file. This was partly for
3
16
  ease of maintenance, but also because there was confusion with the
data/Gemfile CHANGED
@@ -1,7 +1,2 @@
1
- source 'https://rubygems.org' do
2
- gem 'ffi', '~> 1.1'
3
- group 'test' do
4
- gem 'rake'
5
- gem 'rspec', '~> 3.9'
6
- end
7
- end
1
+ source 'https://rubygems.org'
2
+ gemspec
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/ffi/certs/djberg96_pub.pem)`
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
- Currently there is no +processors+ iterative method for multi-cpu systems.
13
- I plan to add it this in a future release.
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 +require+ this package. This
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. You will see a doc/linux.txt
31
- file after you run +rake install+ (via install.rb).
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 +processors+ method has been added and the
48
- +supported+ method has been dropped. See the documentation for other changes.
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,8 +64,8 @@ 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 +prtdiag+ shows.
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
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 = eval(IO.read('sys-cpu.gemspec'))
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.txt"
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.3'.freeze
13
+ VERSION = '1.0.4'
14
+
15
+ private_class_method :new
9
16
  end
10
17
  end
11
18
 
@@ -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
@@ -37,7 +41,7 @@ module Sys
37
41
 
38
42
  attach_function(
39
43
  :sysctl,
40
- [:pointer, :uint, :pointer, :pointer, :pointer, :size_t],
44
+ %i[pointer uint pointer pointer pointer size_t],
41
45
  :int
42
46
  )
43
47
 
@@ -45,20 +49,30 @@ module Sys
45
49
 
46
50
  attach_function(
47
51
  :sysctlbyname,
48
- [:string, :pointer, :pointer, :pointer, :size_t],
52
+ %i[string pointer pointer pointer size_t],
49
53
  :int
50
54
  )
51
55
 
52
56
  private_class_method :sysctlbyname
53
57
 
54
- attach_function :getloadavg, [:pointer, :int], :int
55
- attach_function :processor_info, [:int, :int, :string, :pointer, :pointer], :int
58
+ attach_function :getloadavg, %i[pointer int], :int
59
+ attach_function :processor_info, %i[int int string pointer pointer], :int
56
60
  attach_function :sysconf, [:int], :long
57
61
 
58
62
  private_class_method :getloadavg
59
63
  private_class_method :processor_info
60
64
  private_class_method :sysconf
61
65
 
66
+ class ClockInfo < FFI::Struct
67
+ layout(
68
+ :hz, :int,
69
+ :tick, :int,
70
+ :spare, :int,
71
+ :stathz, :int,
72
+ :profhz, :int
73
+ )
74
+ end
75
+
62
76
  # Returns the cpu's architecture. On most systems this will be identical
63
77
  # to the CPU.machine method. On OpenBSD it will be identical to the CPU.model
64
78
  # method.
@@ -144,11 +158,26 @@ module Sys
144
158
 
145
159
  size.write_long(optr.size)
146
160
 
147
- if sysctlbyname('hw.cpufrequency', optr, size, nil, 0) < 0
148
- raise Error, 'sysctlbyname failed'
161
+ if RbConfig::CONFIG['host_cpu'].downcase == 'arm64'
162
+ if sysctlbyname('hw.tbfrequency', optr, size, nil, 0) < 0
163
+ raise Error, 'sysctlbyname failed on hw.tbfrequency'
164
+ end
165
+
166
+ size.clear
167
+ clock = ClockInfo.new
168
+ size.write_long(clock.size)
169
+
170
+ if sysctlbyname('kern.clockrate', clock, size, nil, 0) < 0
171
+ raise Error, 'sysctlbyname failed on kern.clockrate'
172
+ end
173
+
174
+ (optr.read_long * clock[:hz]) / 1_000_000
175
+ else
176
+ if sysctlbyname('hw.cpufrequency', optr, size, nil, 0) < 0
177
+ raise Error, 'sysctlbyname failed on hw.cpufrequency' if result < 0
178
+ end
179
+ optr.read_long / 1_000_000
149
180
  end
150
-
151
- optr.read_long / 1000000
152
181
  end
153
182
 
154
183
  # Returns an array of three floats indicating the 1, 5 and 15 minute load
@@ -165,3 +194,5 @@ module Sys
165
194
  end
166
195
  end
167
196
  end
197
+
198
+ p Sys::CPU.freq
@@ -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 = '/proc/cpuinfo'
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
- IO.foreach(cpu_file){ |line|
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.has_key?(key)
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
- val = true
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{ |hash|
61
+ CPU_ARRAY.each do |hash|
62
62
  struct = CPUStruct.new
63
- struct.members.each{ |m| struct.send("#{m}=", hash["#{m}"]) }
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
- when '3'
84
- 'x86'
85
- when '6'
86
- 'x86_64'
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].has_key?(id.to_s)
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.to_s + '?'
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
- IO.readlines(load_avg_file).first.split[0..2].map{ |e| e.to_f }
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 = IO.readlines(cpu_stat_file)
148
+ lines = File.readlines(cpu_stat_file)
147
149
 
148
- lines.each_with_index{ |line, i|
150
+ lines.each_with_index do |line, i|
149
151
  array = line.split
150
- break unless array[0] =~ /cpu/ # 'cpu' entries always on top
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
@@ -1,7 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'ffi'
2
4
  require 'rbconfig'
3
5
 
6
+ # The Sys module is a name space 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
@@ -43,7 +47,7 @@ module Sys
43
47
  begin
44
48
  attach_function(
45
49
  :sysctl,
46
- [:pointer, :uint, :pointer, :pointer, :pointer, :size_t],
50
+ %i[pointer uint pointer pointer pointer size_t],
47
51
  :int
48
52
  )
49
53
  private_class_method :sysctl
@@ -54,7 +58,7 @@ module Sys
54
58
  begin
55
59
  attach_function(
56
60
  :sysctlbyname,
57
- [:string, :pointer, :pointer, :pointer, :size_t],
61
+ %i[string pointer pointer pointer size_t],
58
62
  :int
59
63
  )
60
64
  private_class_method :sysctlbyname
@@ -64,10 +68,10 @@ module Sys
64
68
 
65
69
  # Solaris
66
70
  begin
67
- attach_function :getloadavg, [:pointer, :int], :int
68
- attach_function :processor_info, [:int, :pointer], :int
71
+ attach_function :getloadavg, %i[pointer int], :int
72
+ attach_function :processor_info, %i[int pointer], :int
69
73
  attach_function :sysconf, [:int], :long
70
- attach_function :sysinfo, [:int, :pointer, :long], :int
74
+ attach_function :sysinfo, %i[int pointer long], :int
71
75
 
72
76
  private_class_method :getloadavg
73
77
  private_class_method :processor_info
@@ -162,7 +166,7 @@ module Sys
162
166
  raise Error, 'sysctl function failed'
163
167
  end
164
168
 
165
- buf.strip.unpack('C').first
169
+ buf.strip.unpack1('C')
166
170
  end
167
171
  end
168
172
 
@@ -182,17 +186,15 @@ module Sys
182
186
  if sysctl(mib, 2, buf, size, nil, 0) < 0
183
187
  raise Error, 'sysctl function failed'
184
188
  end
185
-
186
- buf.strip
187
189
  else
188
190
  buf = 0.chr * 257
189
191
 
190
192
  if sysinfo(SI_MACHINE, buf, buf.size) < 0
191
193
  raise Error, 'sysinfo function failed'
192
194
  end
193
-
194
- buf.strip
195
195
  end
196
+
197
+ buf.strip
196
198
  end
197
199
 
198
200
  # Returns a string indicating the cpu model.
@@ -215,10 +217,8 @@ module Sys
215
217
  pinfo = ProcInfo.new
216
218
 
217
219
  # Some systems start at 0, some at 1
218
- if processor_info(0, pinfo) < 0
219
- if processor_info(1, pinfo) < 0
220
- raise Error, 'process_info function failed'
221
- end
220
+ if processor_info(0, pinfo) < 0 && processor_info(1, pinfo) < 0
221
+ raise Error, 'processor_info function failed'
222
222
  end
223
223
 
224
224
  pinfo[:pi_processor_type].to_s
@@ -257,15 +257,13 @@ module Sys
257
257
  raise Error, 'sysctl function failed'
258
258
  end
259
259
 
260
- buf.unpack('I*').first / 1000000
260
+ buf.unpack1('I*') / 1_000_000
261
261
  else
262
262
  pinfo = ProcInfo.new
263
263
 
264
264
  # Some systems start at 0, some at 1
265
- if processor_info(0, pinfo) < 0
266
- if processor_info(1, pinfo) < 0
267
- raise Error, 'process_info function failed'
268
- end
265
+ if processor_info(0, pinfo) < 0 && processor_info(1, pinfo) < 0
266
+ raise Error, 'processor_info function failed'
269
267
  end
270
268
 
271
269
  pinfo[:pi_clock].to_i
@@ -276,15 +274,10 @@ module Sys
276
274
  # average.
277
275
  #
278
276
  def self.load_avg
279
- if respond_to?(:getloadavg, true)
280
- loadavg = FFI::MemoryPointer.new(:double, 3)
281
-
282
- if getloadavg(loadavg, loadavg.size) < 0
283
- raise Error, 'getloadavg function failed'
284
- end
285
-
286
- loadavg.get_array_of_double(0, 3)
287
- end
277
+ return unless respond_to?(:getloadavg, true)
278
+ loadavg = FFI::MemoryPointer.new(:double, 3)
279
+ raise Error, 'getloadavg function failed' if getloadavg(loadavg, loadavg.size) < 0
280
+ loadavg.get_array_of_double(0, 3)
288
281
  end
289
282
 
290
283
  # Returns the floating point processor type.
@@ -296,10 +289,9 @@ module Sys
296
289
 
297
290
  pinfo = ProcInfo.new
298
291
 
299
- if processor_info(0, pinfo) < 0
300
- if processor_info(1, pinfo) < 0
301
- raise Error, 'process_info function failed'
302
- end
292
+ # Some start at 0, some start at 1
293
+ if processor_info(0, pinfo) < 0 && processor_info(1, pinfo) < 0
294
+ raise Error, 'processor_info function failed'
303
295
  end
304
296
 
305
297
  pinfo[:pi_fputypes].to_s
@@ -316,7 +308,7 @@ module Sys
316
308
  pinfo = ProcInfo.new
317
309
 
318
310
  if processor_info(num, pinfo) < 0
319
- raise Error, 'process_info function failed'
311
+ raise Error, 'processor_info function failed'
320
312
  end
321
313
 
322
314
  case pinfo[:pi_state].to_i