sys-cpu 0.6.4 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sys-cpu
3
3
  version: !ruby/object:Gem::Version
4
- hash: 15
4
+ hash: 3
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 6
9
- - 4
10
- version: 0.6.4
8
+ - 7
9
+ - 0
10
+ version: 0.7.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Daniel J. Berger
@@ -15,12 +15,28 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-09-27 00:00:00 Z
18
+ date: 2011-12-14 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
- name: test-unit
21
+ name: ffi
22
22
  prerelease: false
23
23
  requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 23
29
+ segments:
30
+ - 1
31
+ - 0
32
+ - 0
33
+ version: 1.0.0
34
+ type: :runtime
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: test-unit
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
24
40
  none: false
25
41
  requirements:
26
42
  - - ">="
@@ -32,18 +48,18 @@ dependencies:
32
48
  - 2
33
49
  version: 2.1.2
34
50
  type: :development
35
- version_requirements: *id001
51
+ version_requirements: *id002
36
52
  description: " The sys-cpu library provides an interface for gathering information\n about your system's processor(s). Information includes speed, type,\n and load average.\n"
37
53
  email: djberg96 at nospam at gmail dot com
38
54
  executables: []
39
55
 
40
- extensions:
41
- - ext/extconf.rb
56
+ extensions: []
57
+
42
58
  extra_rdoc_files:
43
59
  - CHANGES
44
60
  - README
45
61
  - MANIFEST
46
- - ext/bsd/bsd.c
62
+ - lib/unix/sys/cpu.rb
47
63
  files:
48
64
  - CHANGES
49
65
  - doc/bsd.txt
@@ -56,13 +72,9 @@ files:
56
72
  - examples/example_sys_cpu_linux.rb
57
73
  - examples/example_sys_cpu_sunos.rb
58
74
  - examples/example_sys_cpu_windows.rb
59
- - ext/bsd/bsd.c
60
- - ext/extconf.rb
61
- - ext/hpux/hpux.c
62
- - ext/sunos/sunos.c
63
- - ext/version.h
64
75
  - install.rb
65
76
  - lib/linux/sys/cpu.rb
77
+ - lib/unix/sys/cpu.rb
66
78
  - lib/windows/sys/cpu.rb
67
79
  - MANIFEST
68
80
  - Rakefile
@@ -83,6 +95,7 @@ rdoc_options: []
83
95
 
84
96
  require_paths:
85
97
  - lib
98
+ - lib/unix
86
99
  required_ruby_version: !ruby/object:Gem::Requirement
87
100
  none: false
88
101
  requirements:
@@ -1,331 +0,0 @@
1
- /*****************************************************************************
2
- * bsd.c (cpu.c) - sys-cpu extension for the various BSD flavors and OS X.
3
- *
4
- * Author: Daniel J. Berger
5
- *
6
- * Interface to provide various types of cpu information.
7
- * Based on the Unix::Processors Perl module (Wilson Snyder) with ideas from
8
- * Sys::CPU (Matt Sanford) and Solaris::Kstat (Alan Burlison) as well.
9
- * OS X 10.5+ patch for uptime by Victor Costan.
10
- *
11
- * Portions of this code lifted from the MPlayer source (cpuinfo.c).
12
- *****************************************************************************/
13
- #include <ruby.h>
14
- #include "version.h"
15
-
16
- #ifdef HAVE_KVM_H
17
- #include <kvm.h>
18
- #endif
19
-
20
- #if defined (__OpenBSD__)
21
- #include <sys/param.h>
22
- #endif
23
-
24
- #include <sys/sysctl.h>
25
- #include <sys/types.h>
26
- #include <string.h>
27
- #include <errno.h>
28
-
29
- #ifndef MISSING_USLEEP
30
- #include <unistd.h>
31
- #endif
32
-
33
- #if defined(__MACH__) && defined(__APPLE__)
34
- #include <mach/machine.h>
35
- #endif
36
-
37
- VALUE cCPUError;
38
-
39
- /****************************************************************************
40
- * Used for FreeBSD 4.x to determine CPU clock speed. Borrowed from cpuinfo.c
41
- * in the MPlayer source code.
42
- ****************************************************************************/
43
- #if defined (__FreeBSD__) && (__FreeBSD__ < 5 )
44
- static int64_t rdtsc(void){
45
- unsigned int i, j;
46
- #define RDTSC ".byte 0x0f, 0x31; "
47
- asm(RDTSC : "=a"(i), "=d"(j) : );
48
- return ((int64_t)j<<32) + (int64_t)i;
49
- }
50
- #endif
51
-
52
- /*
53
- * call-seq:
54
- * CPU.load_average
55
- *
56
- * Returns an array of three floats indicating the 1, 5 and 15 minute load
57
- * average.
58
- */
59
- static VALUE cpu_load_avg(VALUE klass){
60
- int n;
61
- VALUE v_num_array = rb_ary_new();
62
-
63
- #ifdef HAVE_KVM_H
64
- int max = 3;
65
- kvm_t* k;
66
- double avgs[3];
67
-
68
- k = malloc(sizeof(kvm_t*));
69
-
70
- if(!kvm_getloadavg(k, avgs, max)){
71
- free(k);
72
- rb_raise(cCPUError, "error calling kvm_getloadavg(): %s", strerror(errno));
73
- }
74
-
75
- for(n = 0; n < 3; n++)
76
- rb_ary_push(v_num_array, rb_float_new(avgs[n]));
77
-
78
- free(k);
79
- #else
80
- struct loadavg k;
81
- size_t len = sizeof(k);
82
-
83
- #ifdef HAVE_SYSCTLBYNAME
84
- if(sysctlbyname("vm.loadavg", &k, &len, NULL, 0))
85
- rb_raise(cCPUError, "error calling sysctlbyname(): %s", strerror(errno));
86
- #else
87
- int mib[2];
88
- mib[0] = CTL_HW;
89
- mib[1] = VM_LOADAVG;
90
-
91
- if(sysctl(mib, 2, &k, &len, NULL, 0))
92
- rb_raise(cCPUError, "error calling sysctl(): %s", strerror(errno));
93
- #endif
94
- for(n = 0; n < 3; n++)
95
- rb_ary_push(v_num_array, rb_float_new(k.ldavg[n] / (float)k.fscale));
96
- #endif
97
-
98
- return v_num_array;
99
- }
100
-
101
- /*
102
- * call-seq:
103
- * CPU.num_cpu
104
- *
105
- * Returns the number of cpu's on your system. Note that each core on
106
- * multi-core systems are counted as a cpu, e.g. one dual core cpu would
107
- * return 2, not 1.
108
- */
109
- static VALUE cpu_num(VALUE klass){
110
- int num_cpu;
111
- size_t len = sizeof(num_cpu);
112
-
113
- #ifdef HAVE_SYSCTLBYNAME
114
- if(sysctlbyname("hw.ncpu", &num_cpu, &len, NULL, 0))
115
- rb_raise(cCPUError, "error calling sysctlbyname(): %s", strerror(errno));
116
- #else
117
- int mib[2];
118
- mib[0] = CTL_HW;
119
- mib[1] = HW_NCPU;
120
-
121
- if(sysctl(mib, 2, &num_cpu, &len, NULL, 0))
122
- rb_raise(cCPUError, "error calling sysctl(): %s", strerror(errno));
123
- #endif
124
-
125
- return INT2NUM(num_cpu);
126
- }
127
-
128
- /*
129
- * call-seq:
130
- * CPU.model
131
- *
132
- * Returns a string indicating the cpu model.
133
- *--
134
- * On OSX I use the hw.cputype instead of hw.model because OSX returns the
135
- * machine model instead of the cpu model for some reason. Initial attempts
136
- * to add the cpusubtype as well were unreliable.
137
- */
138
- static VALUE cpu_model(VALUE klass){
139
- char model[64];
140
- size_t len = sizeof(model);
141
-
142
- #ifdef HAVE_SYSCTLBYNAME
143
- #if defined(__MACH__) && defined(__APPLE__)
144
- int cpu_type;
145
- len = sizeof(cpu_type);
146
-
147
- if(sysctlbyname("hw.cputype", &cpu_type, &len, NULL, 0))
148
- #else
149
- if(sysctlbyname("hw.model", &model, &len, NULL, 0))
150
- #endif
151
- rb_raise(cCPUError, "error calling sysctlbyname(): %s", strerror(errno));
152
- #else
153
- int mib[2];
154
-
155
- mib[0] = CTL_HW;
156
- mib[1] = HW_MODEL;
157
-
158
- if(sysctl(mib, 2, &model, &len, NULL, 0))
159
- rb_raise(cCPUError, "error calling sysctl(): %s", strerror(errno));
160
- #endif
161
-
162
- #if defined(__MACH__) && defined(__APPLE__)
163
- // Intel and PowerPC only.
164
- switch(cpu_type){
165
- case CPU_TYPE_X86:
166
- case CPU_TYPE_X86_64:
167
- strcpy(model, "Intel");
168
- break;
169
- case CPU_TYPE_POWERPC:
170
- case CPU_TYPE_POWERPC64:
171
- strcpy(model, "PowerPC");
172
- break;
173
- default:
174
- strcpy(model, "Unknown");
175
- }
176
-
177
- // TODO: Add the subtype.
178
- #endif
179
-
180
- return rb_str_new2(model);
181
- }
182
-
183
- /*
184
- * call-seq:
185
- * CPU.architecture
186
- *
187
- * Returns the cpu's architecture. On most systems this will be identical
188
- * to the CPU.machine method. On OpenBSD it will be identical to the CPU.model
189
- * method.
190
- */
191
- static VALUE cpu_architecture(VALUE klass){
192
- char arch[32];
193
- size_t len = sizeof(arch);
194
-
195
- #ifdef HAVE_SYSCTLBYNAME
196
- #if defined(__MACH__) && defined(__APPLE__)
197
- if(sysctlbyname("hw.machine", &arch, &len, NULL, 0))
198
- rb_raise(cCPUError, "error calling sysctlbyname(): %s", strerror(errno));
199
- #else
200
- if(sysctlbyname("hw.machine_arch", &arch, &len, NULL, 0))
201
- rb_raise(cCPUError, "error calling sysctlbyname(): %s", strerror(errno));
202
- #endif
203
- #else
204
- int mib[2];
205
- mib[0] = CTL_VM;
206
- #ifdef HW_MACHINE_ARCH
207
- mib[1] = HW_MACHINE_ARCH;
208
- #else
209
- mib[1] = HW_MODEL;
210
- #endif
211
-
212
- if(sysctl(mib, 2, &arch, &len, NULL, 0))
213
- rb_raise(cCPUError, "error calling sysctl(): %s", strerror(errno));
214
- #endif
215
-
216
- return rb_str_new2(arch);
217
- }
218
-
219
- /*
220
- * call-seq:
221
- * CPU.machine
222
- *
223
- * Returns the cpu's class type. On most systems this will be identical
224
- * to the CPU.architecture method. On OpenBSD it will be identical to the
225
- * CPU.model method.
226
- */
227
- static VALUE cpu_machine(VALUE klass){
228
- char machine[32];
229
- size_t len = sizeof(machine);
230
-
231
- #ifdef HAVE_SYSCTLBYNAME
232
- if(sysctlbyname("hw.machine", &machine, &len, NULL, 0))
233
- rb_raise(cCPUError, "error calling sysctlbyname(): %s", strerror(errno));
234
- #else
235
- int mib[2];
236
- mib[0] = CTL_HW;
237
- #ifdef HW_MACHINE_ARCH
238
- mib[1] = HW_MACHINE;
239
- #else
240
- mib[1] = HW_MODEL;
241
- #endif
242
-
243
- if(sysctl(mib, 2, &machine, &len, NULL, 0))
244
- rb_raise(cCPUError, "error calling sysctl(): %s", strerror(errno));
245
- #endif
246
-
247
- return rb_str_new2(machine);
248
- }
249
-
250
- /*
251
- * call-seq:
252
- * CPU.freq
253
- *
254
- * Returns an integer indicating the speed (i.e. frequency in Mhz) of the cpu.
255
- */
256
- static VALUE cpu_freq(VALUE klass){
257
- int mhz;
258
- #if defined (__FreeBSD__) && (__FreeBSD__ < 5)
259
- int64_t tsc_start, tsc_end;
260
- struct timeval tv_start, tv_end;
261
- int usec_delay;
262
-
263
- tsc_start = rdtsc();
264
- gettimeofday(&tv_start,NULL);
265
- #ifdef MISSING_USLEEP
266
- sleep(1);
267
- #else
268
- usleep(100000);
269
- #endif
270
- tsc_end = rdtsc();
271
- gettimeofday(&tv_end,NULL);
272
-
273
- usec_delay = 1000000 * (tv_end.tv_sec - tv_start.tv_sec)
274
- + (tv_end.tv_usec - tv_start.tv_usec);
275
-
276
- mhz = ((tsc_end - tsc_start) / usec_delay);
277
- #else
278
- size_t len = sizeof(mhz);
279
- #ifdef HAVE_SYSCTLBYNAME
280
- #if defined(__MACH__) && defined(__APPLE__)
281
- size_t freq;
282
- len = sizeof(freq);
283
-
284
- if(sysctlbyname("hw.cpufrequency", &freq, &len, 0, 0))
285
- rb_raise(cCPUError, "error calling sysctlbyname(): %s", strerror(errno));
286
-
287
- mhz = freq / 1000000;
288
- #else
289
- if(sysctlbyname("hw.clockrate", &mhz, &len, 0, 0))
290
- rb_raise(cCPUError, "error calling sysctlbyname(): %s", strerror(errno));
291
- #endif
292
- #else
293
- int mib[2];
294
-
295
- mib[0] = CTL_KERN;
296
- mib[1] = KERN_CLOCKRATE;
297
-
298
- if(sysctl(mib, 2, &mhz, &len, NULL, 0))
299
- rb_raise(cCPUError,"error calling sysctlbyname(): %s", strerror(errno));
300
- #endif
301
- #endif
302
-
303
- return INT2NUM(mhz);
304
- }
305
-
306
- void Init_cpu()
307
- {
308
- VALUE mSys, cCPU;
309
-
310
- /* The Sys module serves as a toplevel namespace only */
311
- mSys = rb_define_module("Sys");
312
-
313
- /* The CPU class provides class methods for obtaining CPU information */
314
- cCPU = rb_define_class_under(mSys, "CPU", rb_cObject);
315
-
316
- /* The CPU::Error Exception class is raised whenever any of the CPU class
317
- * methods fail.
318
- */
319
- cCPUError = rb_define_class_under(cCPU, "Error", rb_eStandardError);
320
-
321
- /* 0.6.4: The version of the sys-cpu library */
322
- rb_define_const(cCPU, "VERSION", rb_str_new2(SYS_CPU_VERSION));
323
-
324
- /* Class Methods */
325
- rb_define_singleton_method(cCPU, "architecture", cpu_architecture, 0);
326
- rb_define_singleton_method(cCPU, "freq", cpu_freq, 0);
327
- rb_define_singleton_method(cCPU, "load_avg", cpu_load_avg, 0);
328
- rb_define_singleton_method(cCPU, "machine", cpu_machine, 0);
329
- rb_define_singleton_method(cCPU, "model", cpu_model, 0);
330
- rb_define_singleton_method(cCPU, "num_cpu", cpu_num, 0);
331
- }
@@ -1,26 +0,0 @@
1
- require 'mkmf'
2
- require 'fileutils'
3
- require 'rbconfig'
4
-
5
- File.delete('cpu.c') if File.exists?('cpu.c')
6
-
7
- case Config::CONFIG['host_os']
8
- when /hpux/i
9
- FileUtils.cp("hpux/hpux.c", "cpu.c")
10
- when /sunos|solaris/i
11
- FileUtils.cp("sunos/sunos.c", "cpu.c")
12
- unless have_func("getloadavg")
13
- have_library("kstat")
14
- end
15
- when /bsd|darwin/i
16
- FileUtils.cp("bsd/bsd.c", "cpu.c")
17
- have_func("sysctlbyname")
18
- have_library("kvm")
19
- have_header("kvm.h")
20
- when /linux|dos|windows|win32|mingw|cygwin/i
21
- STDERR.puts "Run 'rake gem:install' instead for this platform"
22
- else
23
- STDERR.puts "This platform is not currently supported. Exiting..."
24
- end
25
-
26
- create_makefile("sys/cpu")