cpuinfo 1.0.0 → 1.0.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cd3fd6b47290969e1e19c3496854d3c342a0e56fa8e1fcbea7507c5a85ea8d35
4
- data.tar.gz: 9571570022400c4c331d586258a586a1641d204ca843a223d496a542f675d07a
3
+ metadata.gz: c2e0becf188c44619dc2023bde1d90e102d48135ef3a88900c9dcfa8024cbf9f
4
+ data.tar.gz: 9f73b1d9eaaa16850be89b44b4ac5526f0a6ad9505807db42b2f31f82d00e5c3
5
5
  SHA512:
6
- metadata.gz: c0e005b310cb5bd98a833a889f7d9d5fe8858eae76635498b5be429062b2c6a9f83da019e3326982ad2991d3d3128565729794a288f6506ba23695eefe258b85
7
- data.tar.gz: 3008f847eae9563e727ef684240a44466f6b445e844790c0b4952853857751fb2a1785c5adfcb69774c735048bd05ae0d6a35672037bb965d2783401291eeb1f
6
+ metadata.gz: c4875d9dac27412bc40e44440976e5b66e4d213bb48ed247e8acff979c9822bfe03edf503718cc3b205b496f6bfffa1341997fca60424ba9c2e82238360f9786
7
+ data.tar.gz: ca1e96a896eb5f5ba301dbfc87f51a344c70f92a1ac5c77c3b6f192cfa34a626932ba61af153d9f44bdbf2c04634370ab900e5849a80a198da1144cba5db8329
File without changes
File without changes
File without changes
@@ -2,7 +2,7 @@ require 'rubygems'
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = 'cpuinfo'
5
- spec.version = '1.0.0'
5
+ spec.version = '1.0.1'
6
6
  spec.author = 'Matthias Winkelmann'
7
7
  spec.email = 'm@matthi.coffee'
8
8
  spec.license = 'MIT'
File without changes
File without changes
File without changes
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cpuinfo
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthias Winkelmann
@@ -44,23 +44,13 @@ executables: []
44
44
  extensions: []
45
45
  extra_rdoc_files: []
46
46
  files:
47
- - cpuinfo/LICENSE
48
- - cpuinfo/README.md
49
- - cpuinfo/Rakefile
50
- - cpuinfo/cpuinfo.gemspec
51
- - cpuinfo/lib/cpuinfo.rb
52
- - cpuinfo/test/fixtures/cpuinfo
53
- - cpuinfo/test/test.rb
54
- - lib/cp/cpu.rb
55
- - lib/cp/unix/sys/cpu.rb
56
- - lib/cp/windows/sys/cpu.rb
57
- - lib/sys-cpu.rb
58
- - test/test_sys_cpu_bsd.rb
59
- - test/test_sys_cpu_hpux.rb
60
- - test/test_sys_cpu_linux.rb
61
- - test/test_sys_cpu_sunos.rb
62
- - test/test_sys_cpu_version.rb
63
- - test/test_sys_cpu_windows.rb
47
+ - LICENSE
48
+ - README.md
49
+ - Rakefile
50
+ - cpuinfo.gemspec
51
+ - lib/cpuinfo.rb
52
+ - test/fixtures/cpuinfo
53
+ - test/test.rb
64
54
  homepage: https://github.com/MatthiasWinkelmann/cpuinfo
65
55
  licenses:
66
56
  - MIT
@@ -1,19 +0,0 @@
1
- # This is just a stub file that requires the appropriate version
2
- # depending on which platform you're on.
3
- require 'rbconfig'
4
-
5
- module Sys
6
- class CPU
7
- # The version of the sys-cpu gem.
8
- VERSION = '0.7.2'
9
- end
10
- end
11
-
12
- case RbConfig::CONFIG['host_os']
13
- when /linux/i
14
- require File.join(File.dirname(__FILE__), 'linux', 'sys', 'cpu')
15
- when /windows|mswin|mingw|cygwin|dos/i
16
- require File.join(File.dirname(__FILE__), 'windows', 'sys', 'cpu')
17
- else
18
- require File.join(File.dirname(__FILE__), 'unix', 'sys', 'cpu')
19
- end
@@ -1,372 +0,0 @@
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
-
19
- if RbConfig::CONFIG['host_os'] =~ /bsd/
20
- HW_MACHINE_ARCH = 11 # Machine architecture
21
- else
22
- HW_MACHINE_ARCH = 12 # Machine architecture
23
- end
24
-
25
- SI_MACHINE = 5
26
- SI_ARCHITECTURE = 6
27
- SC_NPROCESSORS_ONLN = 15
28
-
29
- P_OFFLINE = 1
30
- P_ONLINE = 2
31
- P_FAULTED = 4
32
- P_POWEROFF = 5
33
- P_NOINTR = 6
34
- P_SPARE = 7
35
-
36
- CPU_ARCH_ABI64 = 0x01000000
37
- CPU_TYPE_X86 = 7
38
- CPU_TYPE_X86_64 = (CPU_TYPE_X86 | CPU_ARCH_ABI64)
39
- CPU_TYPE_SPARC = 14
40
- CPU_TYPE_POWERPC = 18
41
- CPU_TYPE_POWERPC64 = CPU_TYPE_POWERPC | CPU_ARCH_ABI64
42
-
43
- begin
44
- attach_function(
45
- :sysctl,
46
- [:pointer, :uint, :pointer, :pointer, :pointer, :size_t],
47
- :int
48
- )
49
- private_class_method :sysctl
50
- rescue FFI::NotFoundError
51
- # Do nothing, not supported on this platform.
52
- end
53
-
54
- begin
55
- attach_function(
56
- :sysctlbyname,
57
- [:string, :pointer, :pointer, :pointer, :size_t],
58
- :int
59
- )
60
- private_class_method :sysctlbyname
61
- rescue FFI::NotFoundError
62
- # Do nothing, not supported on this platform.
63
- end
64
-
65
- # Solaris
66
- begin
67
- attach_function :getloadavg, [:pointer, :int], :int
68
- attach_function :processor_info, [:int, :pointer], :int
69
- attach_function :sysconf, [:int], :long
70
- attach_function :sysinfo, [:int, :pointer, :long], :int
71
-
72
- private_class_method :getloadavg
73
- private_class_method :processor_info
74
- private_class_method :sysconf
75
- private_class_method :sysinfo
76
- rescue FFI::NotFoundError
77
- # Do nothing, not supported on this platform.
78
- end
79
-
80
- class ProcInfo < FFI::Struct
81
- layout(
82
- :pi_state, :int,
83
- :pi_processor_type, [:char, 16],
84
- :pi_fputypes, [:char, 32],
85
- :pi_clock, :int
86
- )
87
- end
88
-
89
- # Returns the cpu's architecture. On most systems this will be identical
90
- # to the CPU.machine method. On OpenBSD it will be identical to the CPU.model
91
- # method.
92
- #
93
- def self.architecture
94
- if respond_to?(:sysinfo, true)
95
- buf = 0.chr * 257
96
-
97
- if sysinfo(SI_ARCHITECTURE, buf, buf.size) < 0
98
- raise Error, "sysinfo function failed"
99
- end
100
-
101
- buf.strip
102
- elsif respond_to?(:sysctlbyname, true)
103
- optr = FFI::MemoryPointer.new(:char, 256)
104
- size = FFI::MemoryPointer.new(:size_t)
105
-
106
- size.write_int(optr.size)
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
115
- raise Error, "sysctlbyname function failed"
116
- end
117
-
118
- optr.read_string
119
- else
120
- buf = 0.chr * 64
121
- mib = FFI::MemoryPointer.new(:int, 2)
122
- size = FFI::MemoryPointer.new(:long, 1)
123
-
124
- mib.write_array_of_int([CTL_HW, HW_MACHINE_ARCH])
125
- size.write_int(buf.size)
126
-
127
- if sysctl(mib, 2, buf, size, nil, 0) < 0
128
- raise Error, "sysctl function failed"
129
- end
130
-
131
- buf.strip
132
- end
133
- end
134
-
135
- # Returns the number of cpu's on your system. Note that each core on
136
- # multi-core systems are counted as a cpu, e.g. one dual core cpu would
137
- # return 2, not 1.
138
- #
139
- def self.num_cpu
140
- if respond_to?(:sysctlbyname, true)
141
- optr = FFI::MemoryPointer.new(:long)
142
- size = FFI::MemoryPointer.new(:size_t)
143
-
144
- size.write_long(optr.size)
145
-
146
- if sysctlbyname('hw.ncpu', optr, size, nil, 0) < 0
147
- raise Error, "sysctlbyname failed"
148
- end
149
-
150
- optr.read_long
151
- elsif respond_to?(:sysconf, true)
152
- num = sysconf(SC_NPROCESSORS_ONLN)
153
-
154
- if num < 0
155
- raise Error, "sysconf function failed"
156
- end
157
-
158
- num
159
- else
160
- buf = 0.chr * 4
161
- mib = FFI::MemoryPointer.new(:int, 2)
162
- size = FFI::MemoryPointer.new(:long, 1)
163
-
164
- mib.write_array_of_int([CTL_HW, HW_NCPU])
165
- size.write_int(buf.size)
166
-
167
- if sysctl(mib, 2, buf, size, nil, 0) < 0
168
- raise Error, "sysctl function failed"
169
- end
170
-
171
- buf.strip.unpack("C").first
172
- end
173
- end
174
-
175
- # Returns the cpu's class type. On most systems this will be identical
176
- # to the CPU.architecture method. On OpenBSD it will be identical to the
177
- # CPU.model method.
178
- #
179
- def self.machine
180
- if respond_to?(:sysctl, true)
181
- buf = 0.chr * 32
182
- mib = FFI::MemoryPointer.new(:int, 2)
183
- size = FFI::MemoryPointer.new(:long, 1)
184
-
185
- mib.write_array_of_int([CTL_HW, HW_MACHINE])
186
- size.write_int(buf.size)
187
-
188
- if sysctl(mib, 2, buf, size, nil, 0) < 0
189
- raise Error, "sysctl function failed"
190
- end
191
-
192
- buf.strip
193
- else
194
- buf = 0.chr * 257
195
-
196
- if sysinfo(SI_MACHINE, buf, buf.size) < 0
197
- raise Error, "sysinfo function failed"
198
- end
199
-
200
- buf.strip
201
- end
202
- end
203
-
204
- # Returns a string indicating the cpu model.
205
- #
206
- def self.model
207
- if RbConfig::CONFIG['host_os'] =~ /darwin/i
208
- ptr = FFI::MemoryPointer.new(:long)
209
- size = FFI::MemoryPointer.new(:size_t)
210
-
211
- size.write_long(ptr.size)
212
-
213
- if sysctlbyname("hw.cputype", ptr, size, nil, 0) < 0
214
- raise "sysctlbyname function failed"
215
- end
216
-
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
227
- 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
250
-
251
- pinfo[:pi_processor_type].to_s
252
- end
253
- end
254
- end
255
-
256
- # Returns an integer indicating the speed of the CPU.
257
- #
258
- def self.freq
259
- if respond_to?(:sysctlbyname, true)
260
- optr = FFI::MemoryPointer.new(:long)
261
- size = FFI::MemoryPointer.new(:size_t)
262
-
263
- size.write_long(optr.size)
264
-
265
- if RbConfig::CONFIG['host_os'] =~ /bsd/i
266
- name = 'hw.clockrate'
267
- else
268
- name = 'hw.cpufrequency'
269
- end
270
-
271
- if sysctlbyname(name, optr, size, nil, 0) < 0
272
- raise Error, "sysctlbyname failed"
273
- end
274
-
275
- if RbConfig::CONFIG['host_os'] =~ /darwin/i
276
- optr.read_long / 1000000
277
- else
278
- optr.read_long
279
- end
280
- elsif respond_to?(:sysctl, true)
281
- buf = 0.chr * 16
282
- mib = FFI::MemoryPointer.new(:int, 2)
283
- size = FFI::MemoryPointer.new(:long, 1)
284
-
285
- mib.write_array_of_int([CTL_HW, HW_CPU_FREQ])
286
- size.write_int(buf.size)
287
-
288
- if sysctl(mib, 2, buf, size, nil, 0) < 0
289
- raise Error, "sysctl function failed"
290
- end
291
-
292
- buf.unpack("I*").first / 1000000
293
- else
294
- pinfo = ProcInfo.new
295
-
296
- # 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
301
- end
302
-
303
- pinfo[:pi_clock].to_i
304
- end
305
- end
306
-
307
- # Returns an array of three floats indicating the 1, 5 and 15 minute load
308
- # average.
309
- #
310
- 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
320
- end
321
-
322
- # Returns the floating point processor type.
323
- #
324
- # Not supported on all platforms.
325
- #
326
- def self.fpu_type
327
- raise NoMethodError unless respond_to?(:processor_info, true)
328
-
329
- pinfo = ProcInfo.new
330
-
331
- if processor_info(0, pinfo) < 0
332
- if processor_info(1, pinfo) < 0
333
- raise Error, "process_info function failed"
334
- end
335
- end
336
-
337
- pinfo[:pi_fputypes].to_s
338
- end
339
-
340
- # Returns the current state of processor +num+, or 0 if no number is
341
- # specified.
342
- #
343
- # Not supported on all platforms.
344
- #
345
- def self.state(num = 0)
346
- raise NoMethodError unless respond_to?(:processor_info, true)
347
-
348
- pinfo = ProcInfo.new
349
-
350
- if processor_info(num, pinfo) < 0
351
- raise Error, "process_info function failed"
352
- end
353
-
354
- case pinfo[:pi_state].to_i
355
- when P_ONLINE
356
- "online"
357
- when P_OFFLINE
358
- "offline"
359
- when P_POWEROFF
360
- "poweroff"
361
- when P_FAULTED
362
- "faulted"
363
- when P_NOINTR
364
- "nointr"
365
- when P_SPARE
366
- "spare"
367
- else
368
- "unknown"
369
- end
370
- end
371
- end
372
- end