sys-cpu 1.0.2 → 1.0.3

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: c9b8cf15d0019738569df3847184952320df08960cc039e52ed819ae7a649d3f
4
- data.tar.gz: b38dc203121fa564f8c6d156e99821eb33ea1b7db7070e261262458531b7393a
3
+ metadata.gz: 8f9a7931b9cc69f3f9725734f85a34fd5ca077226a52f22bdef4fd9f298813c7
4
+ data.tar.gz: b7fc906ea6420ed2dde2c9bc546cd5a782e6dc4f3527eb60ac7e5ee9b2cc002e
5
5
  SHA512:
6
- metadata.gz: 7bbce2f399977b559cbbd799b0b2fb9c5343627e316d8d8c1a58fd61878452491ffd3bf4dc18851c1a8c76d383b7b9aea2e9f7b8bae03e465fd94cd602f1e802
7
- data.tar.gz: 1ac9b193bf154c24b884ac0638a6bc2ff7ee959adaf0dac0d05eb8e47a4f4eadbc5e0f019cd50100cc5e30c0ac02ec8db6f10c89b1acf3404f30f64ea5341818
6
+ metadata.gz: 07a5729274191c98a35515d26b04906222504a8f9a27d551a604998fad5cf40596a240d90434ec0706e54e7ac27d211d222cac122ce101b9d72e29657eda1990
7
+ data.tar.gz: 44068c3fa47b3b9af47485133cd52e8d5bfef6638fbfd54a1e21822b1f8617343c4156d2ce17f3f2a46987cdbc2309fecfbacc33f582bb74ffe553229bd7d0ba
Binary file
data.tar.gz.sig CHANGED
Binary file
data/CHANGES.md CHANGED
@@ -1,3 +1,9 @@
1
+ ## 1.0.3 - 28-Jan-2021
2
+ * The code for OSX was split out into its own source file. This was partly for
3
+ ease of maintenance, but also because there was confusion with the
4
+ processor_info function. The original function was only aimed at Solaris, but
5
+ it turns out OSX has its own, different implementation. This caused segfaults.
6
+
1
7
  ## 1.0.2 - 25-Jan-2021
2
8
  * Fixed issues where things that were meant to be private weren't actually private.
3
9
 
@@ -17,6 +17,7 @@
17
17
  * examples/example_sys_cpu_sunos.rb
18
18
  * examples/example_sys_cpu_windows.rb
19
19
  * lib/sys/cpu.rb
20
+ * lib/sys/darwin/sys/cpu.rb
20
21
  * lib/sys/linux/sys/cpu.rb
21
22
  * lib/sys/unix/sys/cpu.rb
22
23
  * lib/sys/windows/sys/cpu.rb
@@ -5,7 +5,7 @@ require 'rbconfig'
5
5
  module Sys
6
6
  class CPU
7
7
  # The version of the sys-cpu gem.
8
- VERSION = '1.0.2'.freeze
8
+ VERSION = '1.0.3'.freeze
9
9
  end
10
10
  end
11
11
 
@@ -14,6 +14,8 @@ case RbConfig::CONFIG['host_os']
14
14
  require_relative('linux/sys/cpu')
15
15
  when /windows|mswin|mingw|cygwin|dos/i
16
16
  require_relative('windows/sys/cpu')
17
+ when /darwin|mach|osx/i
18
+ require_relative('darwin/sys/cpu')
17
19
  else
18
20
  require_relative('unix/sys/cpu')
19
21
  end
@@ -0,0 +1,167 @@
1
+ require 'ffi'
2
+ require 'rbconfig'
3
+
4
+ module Sys
5
+ class CPU
6
+ extend FFI::Library
7
+ ffi_lib FFI::Library::LIBC
8
+
9
+ # Error raised if any of the CPU methods fail.
10
+ class Error < StandardError; end
11
+
12
+ CTL_HW = 6 # Generic hardware/cpu
13
+
14
+ HW_MACHINE = 1 # Machine class
15
+ HW_MODEL = 2 # Specific machine model
16
+ HW_NCPU = 3 # Number of CPU's
17
+ HW_CPU_FREQ = 15 # CPU frequency
18
+ HW_MACHINE_ARCH = 12 # Machine architecture
19
+
20
+ SI_MACHINE = 5
21
+ SI_ARCHITECTURE = 6
22
+ SC_NPROCESSORS_ONLN = 15
23
+
24
+ P_OFFLINE = 1
25
+ P_ONLINE = 2
26
+ P_FAULTED = 4
27
+ P_POWEROFF = 5
28
+ P_NOINTR = 6
29
+ P_SPARE = 7
30
+
31
+ CPU_ARCH_ABI64 = 0x01000000
32
+ CPU_TYPE_X86 = 7
33
+ CPU_TYPE_X86_64 = (CPU_TYPE_X86 | CPU_ARCH_ABI64)
34
+ CPU_TYPE_SPARC = 14
35
+ CPU_TYPE_POWERPC = 18
36
+ CPU_TYPE_POWERPC64 = CPU_TYPE_POWERPC | CPU_ARCH_ABI64
37
+
38
+ attach_function(
39
+ :sysctl,
40
+ [:pointer, :uint, :pointer, :pointer, :pointer, :size_t],
41
+ :int
42
+ )
43
+
44
+ private_class_method :sysctl
45
+
46
+ attach_function(
47
+ :sysctlbyname,
48
+ [:string, :pointer, :pointer, :pointer, :size_t],
49
+ :int
50
+ )
51
+
52
+ private_class_method :sysctlbyname
53
+
54
+ attach_function :getloadavg, [:pointer, :int], :int
55
+ attach_function :processor_info, [:int, :int, :string, :pointer, :pointer], :int
56
+ attach_function :sysconf, [:int], :long
57
+
58
+ private_class_method :getloadavg
59
+ private_class_method :processor_info
60
+ private_class_method :sysconf
61
+
62
+ # Returns the cpu's architecture. On most systems this will be identical
63
+ # to the CPU.machine method. On OpenBSD it will be identical to the CPU.model
64
+ # method.
65
+ #
66
+ def self.architecture
67
+ optr = FFI::MemoryPointer.new(:char, 256)
68
+ size = FFI::MemoryPointer.new(:size_t)
69
+
70
+ size.write_int(optr.size)
71
+
72
+ if sysctlbyname('hw.machine', optr, size, nil, 0) < 0
73
+ raise Error, 'sysctlbyname function failed'
74
+ end
75
+
76
+ optr.read_string
77
+ end
78
+
79
+ # Returns the number of cpu's on your system. Note that each core on
80
+ # multi-core systems are counted as a cpu, e.g. one dual core cpu would
81
+ # return 2, not 1.
82
+ #
83
+ def self.num_cpu
84
+ optr = FFI::MemoryPointer.new(:long)
85
+ size = FFI::MemoryPointer.new(:size_t)
86
+
87
+ size.write_long(optr.size)
88
+
89
+ if sysctlbyname('hw.ncpu', optr, size, nil, 0) < 0
90
+ raise Error, 'sysctlbyname failed'
91
+ end
92
+
93
+ optr.read_long
94
+ end
95
+
96
+ # Returns the cpu's class type. On most systems this will be identical
97
+ # to the CPU.architecture method. On OpenBSD it will be identical to the
98
+ # CPU.model method.
99
+ #
100
+ def self.machine
101
+ buf = 0.chr * 32
102
+ mib = FFI::MemoryPointer.new(:int, 2)
103
+ size = FFI::MemoryPointer.new(:long, 1)
104
+
105
+ mib.write_array_of_int([CTL_HW, HW_MACHINE])
106
+ size.write_int(buf.size)
107
+
108
+ if sysctl(mib, 2, buf, size, nil, 0) < 0
109
+ raise Error, 'sysctl function failed'
110
+ end
111
+
112
+ buf.strip
113
+ end
114
+
115
+ # Returns a string indicating the cpu model.
116
+ #
117
+ def self.model
118
+ ptr = FFI::MemoryPointer.new(:long)
119
+ size = FFI::MemoryPointer.new(:size_t)
120
+
121
+ size.write_long(ptr.size)
122
+
123
+ if sysctlbyname('hw.cputype', ptr, size, nil, 0) < 0
124
+ raise 'sysctlbyname function failed'
125
+ end
126
+
127
+ case ptr.read_long
128
+ when CPU_TYPE_X86, CPU_TYPE_X86_64
129
+ 'Intel'
130
+ when CPU_TYPE_SPARC
131
+ 'Sparc'
132
+ when CPU_TYPE_POWERPC, CPU_TYPE_POWERPC64
133
+ 'PowerPC'
134
+ else
135
+ 'Unknown'
136
+ end
137
+ end
138
+
139
+ # Returns an integer indicating the speed of the CPU.
140
+ #
141
+ def self.freq
142
+ optr = FFI::MemoryPointer.new(:long)
143
+ size = FFI::MemoryPointer.new(:size_t)
144
+
145
+ size.write_long(optr.size)
146
+
147
+ if sysctlbyname('hw.cpufrequency', optr, size, nil, 0) < 0
148
+ raise Error, 'sysctlbyname failed'
149
+ end
150
+
151
+ optr.read_long / 1000000
152
+ end
153
+
154
+ # Returns an array of three floats indicating the 1, 5 and 15 minute load
155
+ # average.
156
+ #
157
+ def self.load_avg
158
+ loadavg = FFI::MemoryPointer.new(:double, 3)
159
+
160
+ if getloadavg(loadavg, loadavg.size) < 0
161
+ raise Error, 'getloadavg function failed'
162
+ end
163
+
164
+ loadavg.get_array_of_double(0, 3)
165
+ end
166
+ end
167
+ end
@@ -105,13 +105,7 @@ module Sys
105
105
 
106
106
  size.write_int(optr.size)
107
107
 
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
108
+ if sysctlbyname('hw.machine_arch', optr, size, nil, 0) < 0
115
109
  raise Error, 'sysctlbyname function failed'
116
110
  end
117
111
 
@@ -204,52 +198,30 @@ module Sys
204
198
  # Returns a string indicating the cpu model.
205
199
  #
206
200
  def self.model
207
- if RbConfig::CONFIG['host_os'] =~ /darwin/i
208
- ptr = FFI::MemoryPointer.new(:long)
209
- size = FFI::MemoryPointer.new(:size_t)
201
+ if respond_to?(:sysctl, true)
202
+ buf = 0.chr * 64
203
+ mib = FFI::MemoryPointer.new(:int, 2)
204
+ size = FFI::MemoryPointer.new(:long, 1)
210
205
 
211
- size.write_long(ptr.size)
206
+ mib.write_array_of_int([CTL_HW, HW_MODEL])
207
+ size.write_int(buf.size)
212
208
 
213
- if sysctlbyname('hw.cputype', ptr, size, nil, 0) < 0
214
- raise 'sysctlbyname function failed'
209
+ if sysctl(mib, 2, buf, size, nil, 0) < 0
210
+ raise Error, 'sysctl function failed'
215
211
  end
216
212
 
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
213
+ buf.strip
227
214
  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
215
+ pinfo = ProcInfo.new
243
216
 
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
217
+ # 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'
249
221
  end
250
-
251
- pinfo[:pi_processor_type].to_s
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)
@@ -9,7 +9,7 @@ require 'rspec'
9
9
 
10
10
  RSpec.describe Sys::CPU::VERSION do
11
11
  example "version number is set to the expected value" do
12
- expect(Sys::CPU::VERSION).to eq('1.0.2')
12
+ expect(Sys::CPU::VERSION).to eq('1.0.3')
13
13
  end
14
14
 
15
15
  example "version number is frozen" do
@@ -2,7 +2,7 @@ require 'rubygems'
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = 'sys-cpu'
5
- spec.version = '1.0.2'
5
+ spec.version = '1.0.3'
6
6
  spec.author = 'Daniel J. Berger'
7
7
  spec.email = 'djberg96@gmail.com'
8
8
  spec.license = 'Apache-2.0'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sys-cpu
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel J. Berger
@@ -35,7 +35,7 @@ cert_chain:
35
35
  ORVCZpRuCPpmC8qmqxUnARDArzucjaclkxjLWvCVHeFa9UP7K3Nl9oTjJNv+7/jM
36
36
  WZs4eecIcUc4tKdHxcAJ0MO/Dkqq7hGaiHpwKY76wQ1+8xAh
37
37
  -----END CERTIFICATE-----
38
- date: 2021-01-25 00:00:00.000000000 Z
38
+ date: 2021-01-28 00:00:00.000000000 Z
39
39
  dependencies:
40
40
  - !ruby/object:Gem::Dependency
41
41
  name: ffi
@@ -108,6 +108,7 @@ files:
108
108
  - install.rb
109
109
  - lib/sys-cpu.rb
110
110
  - lib/sys/cpu.rb
111
+ - lib/sys/darwin/sys/cpu.rb
111
112
  - lib/sys/linux/sys/cpu.rb
112
113
  - lib/sys/unix/sys/cpu.rb
113
114
  - lib/sys/windows/sys/cpu.rb
metadata.gz.sig CHANGED
Binary file