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
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
|
@@ -162,7 +166,7 @@ module Sys
|
|
162
166
|
raise Error, 'sysctl function failed'
|
163
167
|
end
|
164
168
|
|
165
|
-
buf.strip.
|
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
|
-
|
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.
|
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
|
-
|
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
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
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
|
-
|
300
|
-
|
301
|
-
|
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, '
|
311
|
+
raise Error, 'processor_info function failed'
|
320
312
|
end
|
321
313
|
|
322
314
|
case pinfo[:pi_state].to_i
|