sys-cpu 0.8.3 → 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: 5c889d0a79359903abbe62cbd38095a88335386cc3c930b61ad94c9000350956
4
- data.tar.gz: b050390fc7fccbc4ad842f348e5dd702432f221c037a533bb4df2138d198d70c
3
+ metadata.gz: 8f9a7931b9cc69f3f9725734f85a34fd5ca077226a52f22bdef4fd9f298813c7
4
+ data.tar.gz: b7fc906ea6420ed2dde2c9bc546cd5a782e6dc4f3527eb60ac7e5ee9b2cc002e
5
5
  SHA512:
6
- metadata.gz: 780e4ee7023069eb9c0d6dc0beb296edacda8db250f84cabada5961d2ab6728acd311a7eab41c292703feec6904a3caee6927b676454db46f4e6c1a248a06d12
7
- data.tar.gz: 310b45098badac319002989cd86009cb85fe4924300fb737f8de1b5c62fd2846f104c46a87233f2644dbc6527df1579cf5aa1eb78faea965eecd1648ddc46d15
6
+ metadata.gz: 07a5729274191c98a35515d26b04906222504a8f9a27d551a604998fad5cf40596a240d90434ec0706e54e7ac27d211d222cac122ce101b9d72e29657eda1990
7
+ data.tar.gz: 44068c3fa47b3b9af47485133cd52e8d5bfef6638fbfd54a1e21822b1f8617343c4156d2ce17f3f2a46987cdbc2309fecfbacc33f582bb74ffe553229bd7d0ba
Binary file
data.tar.gz.sig CHANGED
Binary file
@@ -1,34 +1,59 @@
1
- == 0.8.3 - 18-Mar-2020
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
+
7
+ ## 1.0.2 - 25-Jan-2021
8
+ * Fixed issues where things that were meant to be private weren't actually private.
9
+
10
+ ## 1.0.1 - 20-Dec-2020
11
+ * Switched from rdoc to markdown.
12
+
13
+ ## 1.0.0 - 23-Oct-2020
14
+ * Fixed a bug in the Windows version where the attempt to convert the ConfigManagerErrorCode
15
+ to a string was busted. Coincidentally, this also exposed a core bug in JRuby (see PR #6443).
16
+ Thanks go to G. Gibson for reporting the issue.
17
+ * Switched the tests from test-unit to rspec.
18
+
19
+ ## 0.9.0 - 12-May-2020
20
+ * Added explicit freq, architecture, num_cpu and model methods to the Linux
21
+ version in an effort to create a common interface across platforms.
22
+ * Modified the Linux version so that method_missing will raise a NoMethodError
23
+ if you try to call access an attribute that doesn't exist.
24
+ * Some updates to the Linux tests.
25
+
26
+ ## 0.8.3 - 18-Mar-2020
2
27
  * Properly include a LICENSE file as per the Apache-2.0 license.
3
28
 
4
- == 0.8.2 - 14-Jan-2020
29
+ ## 0.8.2 - 14-Jan-2020
5
30
  * Added explicit .rdoc extension to README, CHANGES and MANIFEST files.
6
31
  * Fixed license name in README.
7
32
 
8
- == 0.8.1 - 4-Nov-2018
33
+ ## 0.8.1 - 4-Nov-2018
9
34
  * Added metadata to the gemspec.
10
35
  * Fixed missing hyphen in license name.
11
36
 
12
- == 0.8.0 - 17-Oct-2018
37
+ ## 0.8.0 - 17-Oct-2018
13
38
  * Switched license to Apache 2.0.
14
39
  * Updated documentation for the cpu_stats method on Linux.
15
40
  * The VERSION constant is now frozen.
16
41
  * Now uses require_relative internally where needed.
17
42
 
18
- == 0.7.2 - 5-Sep-2015
43
+ ## 0.7.2 - 5-Sep-2015
19
44
  * Replaced a global array with a constant in the Linux version. Thanks go
20
45
  to Yorick Peterse for the patch.
21
46
  * Added method comments back to the Unix version.
22
47
  * Added a sys-cpu.rb stub file for your convenience.
23
48
  * This gem is now signed.
24
49
 
25
- == 0.7.1 - 2-May-2013
50
+ ## 0.7.1 - 2-May-2013
26
51
  * Added a workaround for a win32ole bug in the Windows version.
27
52
  * Reorganized code so that there is now a single gem rather than three
28
53
  separate platform gems.
29
54
  * Updated test-unit dependency which let me simplify the test files a bit.
30
55
 
31
- == 0.7.0 - 14-Dec-2011
56
+ ## 0.7.0 - 14-Dec-2011
32
57
  * Code base now uses FFI. However, HP-UX is not currently supported.
33
58
  HP-UX users should continue to use the 0.6.x version. Note that
34
59
  the code base remains unchanged on Linux and Windows.
@@ -37,7 +62,7 @@
37
62
  * The type method has been changed to cpu_type on Windows.
38
63
  * Some Rakefile and test suite updates.
39
64
 
40
- == 0.6.4 - 27-Sep-2011
65
+ ## 0.6.4 - 27-Sep-2011
41
66
  * The CPU.freq method now works on OSX.
42
67
  * The CPU.model method on OSX has been altered. Previously it
43
68
  returned the machine model. However, the information is limited.
@@ -45,7 +70,7 @@
45
70
  * The Linux and Windows gems now have a 'universal' architecture.
46
71
  * Refactored the clean task in the Rakefile.
47
72
 
48
- == 0.6.3 - 9-Oct-2010
73
+ ## 0.6.3 - 9-Oct-2010
49
74
  * Fixed a bug in the install.rb file and refactored it a bit. Thanks go
50
75
  to Di-an Jan for the spot. Note, however, that this file will eventually
51
76
  be removed and/or integrated into the Linux source file.
@@ -53,7 +78,7 @@
53
78
  * Fixed and updated the CPU.architecture method on MS Windows to handle IA64
54
79
  and x64 architectures.
55
80
 
56
- == 0.6.2 - 1-Jan-2010
81
+ ## 0.6.2 - 1-Jan-2010
57
82
  * Fixed a bug in the cpu_freq function (which would only be noticed on
58
83
  certain platforms in cases where the CPU.freq method failed). Thanks
59
84
  go to Edho P Arief for the spot.
@@ -66,13 +91,13 @@
66
91
  * Test files renamed.
67
92
  * Added test-unit 2.x as a development dependency.
68
93
 
69
- == 0.6.1 - 4-Jan-2009
94
+ ## 0.6.1 - 4-Jan-2009
70
95
  * Fix for OS X 10.5.x. Thanks go to Victor Costan for the spot and the patch.
71
96
  * Updated the gemspec and some other minor changes.
72
97
  * On MS Windows the impersonation level is now explicitly set to 'impersonate'
73
98
  to avoid issues where systems might be using an older version of WMI.
74
99
 
75
- == 0.6.0 - 26-Apr-2007
100
+ ## 0.6.0 - 26-Apr-2007
76
101
  * Added support for most BSD flavors, including OS X. The freebsd.c file is
77
102
  now just bsd.c.
78
103
  * The CPU.type method for Solaris has been changed to CPU.cpu_type to avoid
@@ -84,7 +109,7 @@
84
109
  * Improved RDoc comments in the C source files.
85
110
  * Changed CPUError to CPU::Error.
86
111
 
87
- == 0.5.5 - 17-Nov-2006
112
+ ## 0.5.5 - 17-Nov-2006
88
113
  * Fixed a bug in the Linux version where there could be a key but no
89
114
  associated value, causing a String#strip! call to fail. Now the value is
90
115
  simply left at nil.
@@ -94,14 +119,14 @@
94
119
  * Modified the gemspec so that it sets the platform properly for Linux
95
120
  and Windows.
96
121
 
97
- == 0.5.4 - 12-Jul-2006
122
+ ## 0.5.4 - 12-Jul-2006
98
123
  * Added a gemspec (and a gem on RubyForge).
99
124
  * The CPU.architecture method on HP-UX now returns nil if it cannot be
100
125
  determined instead of "Unknown" for consistency with the other
101
126
  platforms.
102
127
  * Inlined the RDoc and made some minor cosmetic source code changes.
103
128
 
104
- == 0.5.3 - 4-May-2006
129
+ ## 0.5.3 - 4-May-2006
105
130
  * Fixed in a bug in the Solaris version where CPU.load_avg returned bad values
106
131
  when compiled in 64 bit mode. Thanks go to James Hranicky for the spot and
107
132
  both James Hranicky and Peter Tribble (via comp.unix.solaris) for patches.
@@ -109,7 +134,7 @@
109
134
  which test suite to run. All platforms now use 'ts_all.rb', which will run
110
135
  the appropriate test case behind the scenes.
111
136
 
112
- == 0.5.2 - 24-Jun-2005
137
+ ## 0.5.2 - 24-Jun-2005
113
138
  * Bug fixed on Linux where empty lines could cause problems. This affected
114
139
  both the install.rb and linux.rb files, though it only seems to have been
115
140
  an issue on the 2.6+ kernel.
@@ -118,7 +143,7 @@
118
143
  is the only entry.
119
144
  * Added a sample program for Linux under the 'examples' directory.
120
145
 
121
- == 0.5.1 - 5-May-2005
146
+ ## 0.5.1 - 5-May-2005
122
147
  * Fixed a potential bug in the FreeBSD version of CPU.model.
123
148
  * Eliminated some warnings in the FreeBSD version.
124
149
  * Moved examples directory to the toplevel package directory.
@@ -131,7 +156,7 @@
131
156
  doc/linux.txt.
132
157
  * Some $LOAD_PATH setup changes in the unit tests.
133
158
 
134
- == 0.5.0 - 26-Jun-2004
159
+ ## 0.5.0 - 26-Jun-2004
135
160
  * Now requires Ruby 1.8.0 or later.
136
161
  * FreeBSD support added.
137
162
  * Replaced 'CPUException' with 'CPUError'
@@ -145,7 +170,7 @@
145
170
  * The .html files have been removed. You can generate the html on your own
146
171
  if you like.
147
172
 
148
- == 0.4.0 - 18-Sep-2003
173
+ ## 0.4.0 - 18-Sep-2003
149
174
  * Added MS Windows support
150
175
  * Changed some method names. The "cpu" has been dropped from most method
151
176
  names. See documentation for details.
@@ -157,14 +182,14 @@
157
182
  * Minor API change for the load_avg() method on HP-UX. It now accepts a
158
183
  CPU number as an argument.
159
184
 
160
- == 0.3.1 - 16-Jul-2003
185
+ ## 0.3.1 - 16-Jul-2003
161
186
  * Fixed a bug in the Solaris version that was using up and not
162
187
  freeing file descriptors.
163
188
  * Added html doc for Solaris under doc directory.
164
189
  * Minor changes to test_hpux.rb and test_sunos.rb
165
190
  * Minor README changes.
166
191
 
167
- == 0.3.0 - 30-Jun-2003
192
+ ## 0.3.0 - 30-Jun-2003
168
193
  * Added HP-UX support
169
194
  * Removed the VERSION class method. Use the constant instead
170
195
  * Changed license to "Artistic"
@@ -173,7 +198,7 @@
173
198
  * Modified extconf.rb, moving some of the dynamic test generation
174
199
  into separate files
175
200
 
176
- == 0.2.2 - 25-Mar-2003
201
+ ## 0.2.2 - 25-Mar-2003
177
202
  * fpu_type and cpu_type now return nil if not found (Solaris)
178
203
  * CPUException is now a direct subclass of StandardError
179
204
  * Modified extconf.rb script
@@ -181,19 +206,19 @@
181
206
  * Added another test to the solaris test suite
182
207
  * Important note added to INSTALL file
183
208
 
184
- == 0.2.1 - 12-Mar-2003
209
+ ## 0.2.1 - 12-Mar-2003
185
210
  * Added the cpu_stats() class method for Linux, which contains the
186
211
  data from the 'cpu' lines of /proc/stat
187
212
  * Minor fix for extconf.rb (thanks Michael Granger)
188
213
  * Some tests added to Linux test suite
189
214
  * MANIFEST correction
190
215
 
191
- == 0.2.0 - 13-Feb-2003
216
+ ## 0.2.0 - 13-Feb-2003
192
217
  * Linux support added (pure Ruby only)
193
218
  * Many changes to extconf.rb to support Linux version
194
219
  * sys-uname prerequisite dropped
195
220
  * rd2 documentation now kept separate from source
196
221
 
197
- == 0.1.0 - 3-Feb-2003
222
+ ## 0.1.0 - 3-Feb-2003
198
223
  * Initial release
199
224
  * Currently supports Solaris (only)
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org' do
2
+ gem 'ffi', '~> 1.1'
3
+ group 'test' do
4
+ gem 'rake'
5
+ gem 'rspec', '~> 3.9'
6
+ end
7
+ end
@@ -1,8 +1,9 @@
1
1
  * install.rb
2
- * MANIFEST.rdoc
3
- * CHANGES.rdoc
2
+ * LICENSE
3
+ * MANIFEST.md
4
+ * CHANGES.md
4
5
  * Rakefile
5
- * README.rdoc
6
+ * README.md
6
7
  * sys-cpu.gemspec
7
8
  * certs/djberg96_pub.pem
8
9
  * doc/bsd.txt
@@ -16,12 +17,14 @@
16
17
  * examples/example_sys_cpu_sunos.rb
17
18
  * examples/example_sys_cpu_windows.rb
18
19
  * lib/sys/cpu.rb
20
+ * lib/sys/darwin/sys/cpu.rb
19
21
  * lib/sys/linux/sys/cpu.rb
20
22
  * lib/sys/unix/sys/cpu.rb
21
23
  * lib/sys/windows/sys/cpu.rb
22
- * test/test_sys_cpu.rb
23
- * test/test_sys_cpu_bsd.rb
24
- * test/test_sys_cpu_hpux.rb
25
- * test/test_sys_cpu_sunos.rb
26
- * test/test_sys_cpu_linux.rb
27
- * test/test_sys_cpu_windows.rb
24
+ * spec/spec_helper.rb
25
+ * spec/sys_cpu_spec.rb
26
+ * spec/sys_cpu_bsd_spec.rb
27
+ * spec/sys_cpu_hpux_spec.rb
28
+ * spec/sys_cpu_linux_spec.rb
29
+ * spec/sys_cpu_sunos_spec.rb
30
+ * spec/sys_cpu_windows_spec.rb
@@ -1,22 +1,22 @@
1
- = Description
1
+ ## Description
2
2
  A Ruby interface for getting cpu information.
3
3
 
4
- = Installation
5
- gem install sys-cpu
4
+ ## Installation
5
+ `gem install sys-cpu`
6
6
 
7
- = Adding the trusted cert
8
- gem cert --add <(curl -Ls https://raw.githubusercontent.com/djberg96/sys-cpu/ffi/certs/djberg96_pub.pem)
7
+ ## Adding the trusted cert
8
+ `gem cert --add <(curl -Ls https://raw.githubusercontent.com/djberg96/sys-cpu/ffi/certs/djberg96_pub.pem)`
9
9
 
10
- = Notes
11
- == Solaris
10
+ ## Notes
11
+ ### Solaris
12
12
  Currently there is no +processors+ iterative method for multi-cpu systems.
13
13
  I plan to add it this in a future release.
14
14
 
15
- == OS X
15
+ ### OS X
16
16
  The CPU.model method returns very limited information. I do not yet know
17
17
  how to get more detailed information.
18
18
 
19
- == Linux
19
+ ### Linux
20
20
  This is pure Ruby. This version reads information out of /proc/cpuinfo and
21
21
  /proc/loadavg, so if /proc isn't mounted it won't work.
22
22
 
@@ -30,7 +30,7 @@ build process because the fields vary depending on your setup. So, don't
30
30
  look at it until *after* you've installed it. You will see a doc/linux.txt
31
31
  file after you run +rake install+ (via install.rb).
32
32
 
33
- == HP-UX
33
+ ### HP-UX
34
34
  Unlike other platforms, you can get load averages for an individual cpu in
35
35
  multi-cpu systems. See documentation for more details.
36
36
 
@@ -38,38 +38,38 @@ Note that version 0.7.x and later will not work on HP-UX because of the
38
38
  switch to FFI and the lack of a testing platform. However, version 0.6.x
39
39
  will work just fine.
40
40
 
41
- == MS Windows
41
+ ### MS Windows
42
42
  This is a pure Ruby implementation using the win32ole package + WMI. The C
43
43
  version has been scrapped.
44
44
 
45
45
  As of version 0.5.0, the CPU.usage method has been removed in favor of the
46
46
  CPU.load_avg method. This does not (currently) use a perf counter, so there
47
- is no longer any delay. Also, the +processors+ method has been added and the
48
- +supported+ method has been dropped. See the documentation for other changes.
47
+ is no longer any delay. Also, the +processors+ method has been added and the
48
+ +supported+ method has been dropped. See the documentation for other changes.
49
49
 
50
- = Acknowledgements
50
+ ## Acknowledgements
51
51
  Thanks go to the MPlayer team for some source code that helped me on
52
52
  certain versions of FreeBSD in the original C version.
53
53
 
54
- = Known Bugs
54
+ ## Known Bugs
55
55
  None that I'm aware of. Please report bugs on the project page at:
56
56
 
57
57
  https://github.com/djberg96/sys-cpu
58
58
 
59
- = Future Plans
59
+ ## Future Plans
60
60
  * Add iterative CPU.processors method.
61
61
  * Add more information in general, such as what +prtdiag+ shows.
62
62
 
63
- = License
63
+ ## License
64
64
  Apache-2.0
65
65
 
66
- = Copyright
67
- (C) 2003-2020 Daniel J. Berger, All Rights Reserved
66
+ ## Copyright
67
+ (C) 2003-2021 Daniel J. Berger, All Rights Reserved
68
68
 
69
- = Warranty
69
+ ## Warranty
70
70
  This package is provided "as is" and without any express or
71
71
  implied warranties, including, without limitation, the implied
72
72
  warranties of merchantability and fitness for a particular purpose.
73
73
 
74
- = Author
74
+ ## Author
75
75
  Daniel J. Berger
data/Rakefile CHANGED
@@ -1,10 +1,10 @@
1
1
  require 'rake'
2
2
  require 'rake/clean'
3
- require 'rake/testtask'
4
3
  require 'rbconfig'
4
+ require 'rspec/core/rake_task'
5
5
  include RbConfig
6
6
 
7
- CLEAN.include('**/*.gem', '**/*.rbc', '**/*.rbx')
7
+ CLEAN.include('**/*.gem', '**/*.rbc', '**/*.rbx', '**/*.lock')
8
8
 
9
9
  namespace 'gem' do
10
10
  desc "Create the sys-cpu gem"
@@ -12,7 +12,7 @@ namespace 'gem' do
12
12
  require 'rubygems/package'
13
13
  spec = eval(IO.read('sys-cpu.gemspec'))
14
14
  spec.signing_key = File.join(Dir.home, '.ssh', 'gem-private_key.pem')
15
- Gem::Package.build(spec, true)
15
+ Gem::Package.build(spec)
16
16
  end
17
17
 
18
18
  desc "Install the sys-cpu gem"
@@ -44,9 +44,7 @@ task :example => [:clean] do
44
44
 
45
45
  end
46
46
 
47
- Rake::TestTask.new do |t|
48
- t.libs << 'test'
49
- t.test_files = FileList['test/test_sys_cpu.rb']
50
- end
47
+ desc "Run the test suite"
48
+ RSpec::Core::RakeTask.new(:spec)
51
49
 
52
- task :default => :test
50
+ task :default => :spec
@@ -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 = '0.8.3'.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