sys-cpu 1.0.2 → 1.0.5

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
@@ -105,13 +109,7 @@ module Sys
105
109
 
106
110
  size.write_int(optr.size)
107
111
 
108
- if RbConfig::CONFIG['host_os'] =~ /darwin/i
109
- name = 'hw.machine'
110
- else
111
- name = 'hw.machine_arch'
112
- end
113
-
114
- if sysctlbyname(name, optr, size, nil, 0) < 0
112
+ if sysctlbyname('hw.machine_arch', optr, size, nil, 0) < 0
115
113
  raise Error, 'sysctlbyname function failed'
116
114
  end
117
115
 
@@ -168,7 +166,7 @@ module Sys
168
166
  raise Error, 'sysctl function failed'
169
167
  end
170
168
 
171
- buf.strip.unpack('C').first
169
+ buf.strip.unpack1('C')
172
170
  end
173
171
  end
174
172
 
@@ -188,68 +186,42 @@ module Sys
188
186
  if sysctl(mib, 2, buf, size, nil, 0) < 0
189
187
  raise Error, 'sysctl function failed'
190
188
  end
191
-
192
- buf.strip
193
189
  else
194
190
  buf = 0.chr * 257
195
191
 
196
192
  if sysinfo(SI_MACHINE, buf, buf.size) < 0
197
193
  raise Error, 'sysinfo function failed'
198
194
  end
199
-
200
- buf.strip
201
195
  end
196
+
197
+ buf.strip
202
198
  end
203
199
 
204
200
  # Returns a string indicating the cpu model.
205
201
  #
206
202
  def self.model
207
- if RbConfig::CONFIG['host_os'] =~ /darwin/i
208
- ptr = FFI::MemoryPointer.new(:long)
209
- size = FFI::MemoryPointer.new(:size_t)
203
+ if respond_to?(:sysctl, true)
204
+ buf = 0.chr * 64
205
+ mib = FFI::MemoryPointer.new(:int, 2)
206
+ size = FFI::MemoryPointer.new(:long, 1)
210
207
 
211
- size.write_long(ptr.size)
208
+ mib.write_array_of_int([CTL_HW, HW_MODEL])
209
+ size.write_int(buf.size)
212
210
 
213
- if sysctlbyname('hw.cputype', ptr, size, nil, 0) < 0
214
- raise 'sysctlbyname function failed'
211
+ if sysctl(mib, 2, buf, size, nil, 0) < 0
212
+ raise Error, 'sysctl function failed'
215
213
  end
216
214
 
217
- case ptr.read_long
218
- when CPU_TYPE_X86, CPU_TYPE_X86_64
219
- 'Intel'
220
- when CPU_TYPE_SPARC
221
- 'Sparc'
222
- when CPU_TYPE_POWERPC, CPU_TYPE_POWERPC64
223
- 'PowerPC'
224
- else
225
- 'Unknown'
226
- end
215
+ buf.strip
227
216
  else
228
- if respond_to?(:sysctl, true)
229
- buf = 0.chr * 64
230
- mib = FFI::MemoryPointer.new(:int, 2)
231
- size = FFI::MemoryPointer.new(:long, 1)
232
-
233
- mib.write_array_of_int([CTL_HW, HW_MODEL])
234
- size.write_int(buf.size)
235
-
236
- if sysctl(mib, 2, buf, size, nil, 0) < 0
237
- raise Error, 'sysctl function failed'
238
- end
239
-
240
- buf.strip
241
- else
242
- pinfo = ProcInfo.new
243
-
244
- # Some systems start at 0, some at 1
245
- if processor_info(0, pinfo) < 0
246
- if processor_info(1, pinfo) < 0
247
- raise Error, 'process_info function failed'
248
- end
249
- end
217
+ pinfo = ProcInfo.new
250
218
 
251
- pinfo[:pi_processor_type].to_s
219
+ # Some systems start at 0, some at 1
220
+ if processor_info(0, pinfo) < 0 && processor_info(1, pinfo) < 0
221
+ raise Error, 'processor_info function failed'
252
222
  end
223
+
224
+ pinfo[:pi_processor_type].to_s
253
225
  end
254
226
  end
255
227
 
@@ -272,11 +244,7 @@ module Sys
272
244
  raise Error, 'sysctlbyname failed'
273
245
  end
274
246
 
275
- if RbConfig::CONFIG['host_os'] =~ /darwin/i
276
- optr.read_long / 1000000
277
- else
278
- optr.read_long
279
- end
247
+ optr.read_long
280
248
  elsif respond_to?(:sysctl, true)
281
249
  buf = 0.chr * 16
282
250
  mib = FFI::MemoryPointer.new(:int, 2)
@@ -289,15 +257,13 @@ module Sys
289
257
  raise Error, 'sysctl function failed'
290
258
  end
291
259
 
292
- buf.unpack('I*').first / 1000000
260
+ buf.unpack1('I*') / 1_000_000
293
261
  else
294
262
  pinfo = ProcInfo.new
295
263
 
296
264
  # Some systems start at 0, some at 1
297
- if processor_info(0, pinfo) < 0
298
- if processor_info(1, pinfo) < 0
299
- raise Error, 'process_info function failed'
300
- end
265
+ if processor_info(0, pinfo) < 0 && processor_info(1, pinfo) < 0
266
+ raise Error, 'processor_info function failed'
301
267
  end
302
268
 
303
269
  pinfo[:pi_clock].to_i
@@ -308,15 +274,10 @@ module Sys
308
274
  # average.
309
275
  #
310
276
  def self.load_avg
311
- if respond_to?(:getloadavg, true)
312
- loadavg = FFI::MemoryPointer.new(:double, 3)
313
-
314
- if getloadavg(loadavg, loadavg.size) < 0
315
- raise Error, 'getloadavg function failed'
316
- end
317
-
318
- loadavg.get_array_of_double(0, 3)
319
- 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)
320
281
  end
321
282
 
322
283
  # Returns the floating point processor type.
@@ -328,10 +289,9 @@ module Sys
328
289
 
329
290
  pinfo = ProcInfo.new
330
291
 
331
- if processor_info(0, pinfo) < 0
332
- if processor_info(1, pinfo) < 0
333
- raise Error, 'process_info function failed'
334
- 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'
335
295
  end
336
296
 
337
297
  pinfo[:pi_fputypes].to_s
@@ -348,7 +308,7 @@ module Sys
348
308
  pinfo = ProcInfo.new
349
309
 
350
310
  if processor_info(num, pinfo) < 0
351
- raise Error, 'process_info function failed'
311
+ raise Error, 'processor_info function failed'
352
312
  end
353
313
 
354
314
  case pinfo[:pi_state].to_i