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.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/CHANGES.md +23 -0
- data/Gemfile +2 -7
- data/MANIFEST.md +1 -0
- 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 +10 -1
- data/lib/sys/darwin/sys/cpu.rb +196 -0
- data/lib/sys/linux/sys/cpu.rb +33 -31
- data/lib/sys/unix/sys/cpu.rb +40 -80
- 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 +53 -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 +38 -8
- metadata.gz.sig +0 -0
- data/doc/bsd.txt +0 -49
- data/doc/linux.txt +0 -41
data/lib/sys/unix/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 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
|
-
[
|
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
|
-
[
|
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, [
|
68
|
-
attach_function :processor_info, [
|
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, [
|
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
|
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.
|
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
|
208
|
-
|
209
|
-
|
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
|
-
|
208
|
+
mib.write_array_of_int([CTL_HW, HW_MODEL])
|
209
|
+
size.write_int(buf.size)
|
212
210
|
|
213
|
-
if
|
214
|
-
raise '
|
211
|
+
if sysctl(mib, 2, buf, size, nil, 0) < 0
|
212
|
+
raise Error, 'sysctl function failed'
|
215
213
|
end
|
216
214
|
|
217
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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.
|
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
|
-
|
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
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
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
|
-
|
332
|
-
|
333
|
-
|
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, '
|
311
|
+
raise Error, 'processor_info function failed'
|
352
312
|
end
|
353
313
|
|
354
314
|
case pinfo[:pi_state].to_i
|