sys-cpu 1.0.3 → 1.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -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