sys-cpu 0.6.4-universal-linux → 0.7.0-universal-linux
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES +11 -1
- data/README +6 -2
- data/Rakefile +19 -50
- data/doc/sunos.txt +2 -2
- data/doc/windows.txt +2 -2
- data/examples/example_sys_cpu_windows.rb +5 -4
- data/lib/linux/sys/cpu.rb +1 -1
- data/lib/unix/sys/cpu.rb +347 -0
- data/lib/windows/sys/cpu.rb +4 -4
- data/sys-cpu.gemspec +4 -11
- data/test/test_sys_cpu.rb +12 -12
- data/test/test_sys_cpu_bsd.rb +84 -60
- data/test/test_sys_cpu_sunos.rb +62 -46
- data/test/test_sys_cpu_version.rb +2 -2
- data/test/test_sys_cpu_windows.rb +13 -13
- metadata +25 -13
- data/ext/bsd/bsd.c +0 -331
- data/ext/extconf.rb +0 -26
- data/ext/hpux/hpux.c +0 -219
- data/ext/sunos/sunos.c +0 -281
- data/ext/version.h +0 -2
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:
|
4
|
+
hash: 3
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 7
|
9
|
+
- 0
|
10
|
+
version: 0.7.0
|
11
11
|
platform: universal-linux
|
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-
|
18
|
+
date: 2011-12-14 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
|
-
name:
|
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,7 +48,7 @@ dependencies:
|
|
32
48
|
- 2
|
33
49
|
version: 2.1.2
|
34
50
|
type: :development
|
35
|
-
version_requirements: *
|
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: []
|
@@ -45,11 +61,6 @@ extra_rdoc_files:
|
|
45
61
|
- MANIFEST
|
46
62
|
- lib/linux/sys/cpu.rb
|
47
63
|
files:
|
48
|
-
- ext/version.h
|
49
|
-
- ext/hpux/hpux.c
|
50
|
-
- ext/sunos/sunos.c
|
51
|
-
- ext/bsd/bsd.c
|
52
|
-
- ext/extconf.rb
|
53
64
|
- doc/windows.txt
|
54
65
|
- doc/sunos.txt
|
55
66
|
- doc/bsd.txt
|
@@ -66,6 +77,7 @@ files:
|
|
66
77
|
- MANIFEST
|
67
78
|
- CHANGES
|
68
79
|
- sys-cpu.gemspec
|
80
|
+
- lib/unix/sys/cpu.rb
|
69
81
|
- lib/linux/sys/cpu.rb
|
70
82
|
- lib/windows/sys/cpu.rb
|
71
83
|
- install.rb
|
@@ -105,7 +117,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
105
117
|
requirements: []
|
106
118
|
|
107
119
|
rubyforge_project: sysutils
|
108
|
-
rubygems_version: 1.8.
|
120
|
+
rubygems_version: 1.8.11
|
109
121
|
signing_key:
|
110
122
|
specification_version: 3
|
111
123
|
summary: A Ruby interface for providing CPU information
|
data/ext/bsd/bsd.c
DELETED
@@ -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
|
-
}
|
data/ext/extconf.rb
DELETED
@@ -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")
|
data/ext/hpux/hpux.c
DELETED
@@ -1,219 +0,0 @@
|
|
1
|
-
/*****************************************************************************
|
2
|
-
* hpux.c (cpu.c) - sys-cpu extension for HP-UX
|
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
|
-
*****************************************************************************/
|
10
|
-
#include <ruby.h>
|
11
|
-
#include "version.h"
|
12
|
-
#include <unistd.h>
|
13
|
-
#include <sys/pstat.h>
|
14
|
-
|
15
|
-
#ifndef PST_MAX_PROCS
|
16
|
-
#define PST_MAX_PROCS 32
|
17
|
-
#endif
|
18
|
-
|
19
|
-
#ifdef __cplusplus
|
20
|
-
extern "C"
|
21
|
-
{
|
22
|
-
#endif
|
23
|
-
|
24
|
-
VALUE cCPUError;
|
25
|
-
|
26
|
-
/*
|
27
|
-
* call-seq:
|
28
|
-
* CPU.load_avg
|
29
|
-
* CPU.load_avg(cpu_num)
|
30
|
-
* CPU.load_avg{ block }
|
31
|
-
*
|
32
|
-
* In non-block form returns an array of three floats indicating the 1, 5
|
33
|
-
* and 15 minute overall load average.
|
34
|
-
*
|
35
|
-
* In block form, it returns an array of three floats indicating the 1, 5
|
36
|
-
* and 15 minute load average for each cpu. Only useful on multi-cpu
|
37
|
-
* systems.
|
38
|
-
*
|
39
|
-
* If 'cpu_num' is provided, returns the load average (as a 3-element
|
40
|
-
* array) for that cpu only.
|
41
|
-
*
|
42
|
-
* You cannot provide +cpu_num+ and use block form at the same time.
|
43
|
-
*/
|
44
|
-
static VALUE cpu_load_avg(int argc, VALUE *argv)
|
45
|
-
{
|
46
|
-
struct pst_processor psp[PST_MAX_PROCS];
|
47
|
-
struct pst_dynamic psd;
|
48
|
-
int i, num_cpu;
|
49
|
-
VALUE ncpu = Qnil;
|
50
|
-
VALUE la_ary = rb_ary_new();
|
51
|
-
|
52
|
-
rb_scan_args(argc,argv,"01",&ncpu);
|
53
|
-
|
54
|
-
if( (ncpu != Qnil) && (rb_block_given_p()) )
|
55
|
-
{
|
56
|
-
rb_raise(rb_eArgError,"Can't use block form when CPU number is provided");
|
57
|
-
}
|
58
|
-
|
59
|
-
if( (ncpu != Qnil) || ((ncpu == Qnil) && (rb_block_given_p())) )
|
60
|
-
{
|
61
|
-
num_cpu = pstat_getprocessor(psp,sizeof(struct pst_processor),PST_MAX_PROCS,0);
|
62
|
-
if(num_cpu < 0)
|
63
|
-
{
|
64
|
-
rb_raise(cCPUError,"Call to pstat_getprocessor() failed.");
|
65
|
-
}
|
66
|
-
else
|
67
|
-
{
|
68
|
-
for(i = 0; i < num_cpu; i++)
|
69
|
-
{
|
70
|
-
if( (ncpu != Qnil) && (NUM2INT(ncpu)) != i){ continue; }
|
71
|
-
|
72
|
-
rb_ary_push(la_ary,rb_float_new(psp[i].psp_avg_1_min));
|
73
|
-
rb_ary_push(la_ary,rb_float_new(psp[i].psp_avg_5_min));
|
74
|
-
rb_ary_push(la_ary,rb_float_new(psp[i].psp_avg_15_min));
|
75
|
-
if(rb_block_given_p())
|
76
|
-
{
|
77
|
-
rb_yield(la_ary);
|
78
|
-
}
|
79
|
-
else
|
80
|
-
{
|
81
|
-
return la_ary;
|
82
|
-
}
|
83
|
-
rb_ary_clear(la_ary);
|
84
|
-
}
|
85
|
-
}
|
86
|
-
}
|
87
|
-
else
|
88
|
-
{
|
89
|
-
if(pstat_getdynamic(&psd,sizeof(psd), (size_t)1, 0) == -1)
|
90
|
-
{
|
91
|
-
rb_raise(cCPUError,"Call to pstat_getdynamic() failed.");
|
92
|
-
}
|
93
|
-
else
|
94
|
-
{
|
95
|
-
rb_ary_push(la_ary,rb_float_new(psd.psd_avg_1_min));
|
96
|
-
rb_ary_push(la_ary,rb_float_new(psd.psd_avg_5_min));
|
97
|
-
rb_ary_push(la_ary,rb_float_new(psd.psd_avg_15_min));
|
98
|
-
return la_ary;
|
99
|
-
}
|
100
|
-
}
|
101
|
-
|
102
|
-
return Qnil;
|
103
|
-
}
|
104
|
-
|
105
|
-
/*
|
106
|
-
* call-seq:
|
107
|
-
* CPU.num_cpu
|
108
|
-
*
|
109
|
-
* Returns the number of CPU's on the system.
|
110
|
-
*/
|
111
|
-
static VALUE cpu_num()
|
112
|
-
{
|
113
|
-
struct pst_dynamic dyn;
|
114
|
-
pstat_getdynamic(&dyn, sizeof(dyn), 0, 0);
|
115
|
-
return INT2NUM(dyn.psd_max_proc_cnt);
|
116
|
-
}
|
117
|
-
|
118
|
-
/* call-seq:
|
119
|
-
* CPU.num_active_cpu
|
120
|
-
*
|
121
|
-
* Returns the number of active CPU's on the system.
|
122
|
-
*/
|
123
|
-
static VALUE cpu_num_active()
|
124
|
-
{
|
125
|
-
struct pst_dynamic dyn;
|
126
|
-
pstat_getdynamic(&dyn, sizeof(dyn), 0, 0);
|
127
|
-
return INT2NUM(dyn.psd_proc_cnt);
|
128
|
-
}
|
129
|
-
|
130
|
-
/*
|
131
|
-
* call-seq:
|
132
|
-
* CPU.architecture
|
133
|
-
*
|
134
|
-
* Returns the cpu architecture, e.g. PA RISC 1.2, etc, or nil if it
|
135
|
-
* cannot be determined.
|
136
|
-
*/
|
137
|
-
static VALUE cpu_architecture()
|
138
|
-
{
|
139
|
-
long cpu_ver = sysconf(_SC_CPU_VERSION);
|
140
|
-
|
141
|
-
if(cpu_ver == CPU_HP_MC68020){ return rb_str_new2("Motorola MC68020"); }
|
142
|
-
if(cpu_ver == CPU_HP_MC68030){ return rb_str_new2("Motorola MC68030"); }
|
143
|
-
if(cpu_ver == CPU_HP_MC68040){ return rb_str_new2("Motorola MC68040"); }
|
144
|
-
if(cpu_ver == CPU_PA_RISC1_0){ return rb_str_new2("HP PA-RISC 1.0"); }
|
145
|
-
if(cpu_ver == CPU_PA_RISC1_1){ return rb_str_new2("HP PA-RISC 1.1"); }
|
146
|
-
if(cpu_ver == CPU_PA_RISC1_2){ return rb_str_new2("HP PA-RISC 1.2"); }
|
147
|
-
if(cpu_ver == CPU_PA_RISC2_0){ return rb_str_new2("HP PA-RISC 2.0"); }
|
148
|
-
|
149
|
-
return Qnil;
|
150
|
-
}
|
151
|
-
|
152
|
-
/*
|
153
|
-
* call-seq:
|
154
|
-
* CPU.freq(cpu_num=0)
|
155
|
-
*
|
156
|
-
* Returns an integer indicating the speed (i.e. frequency in Mhz) of
|
157
|
-
* +cpu_num+, or CPU 0 if no +cpu_num+ is specified.
|
158
|
-
*/
|
159
|
-
static VALUE cpu_freq(int argc, VALUE *argv)
|
160
|
-
{
|
161
|
-
int cpu_num = 0; /* default value */
|
162
|
-
struct pst_processor psp;
|
163
|
-
unsigned long int clock_speed, scclktick;
|
164
|
-
VALUE ncpu = Qnil;
|
165
|
-
|
166
|
-
rb_scan_args(argc, argv, "01", &ncpu);
|
167
|
-
|
168
|
-
if(ncpu != Qnil)
|
169
|
-
{
|
170
|
-
Check_Type(ncpu,T_FIXNUM);
|
171
|
-
cpu_num = NUM2INT(ncpu);
|
172
|
-
}
|
173
|
-
|
174
|
-
if((pstat_getprocessor(&psp,sizeof(psp),(size_t)1,cpu_num)) == -1)
|
175
|
-
rb_raise(cCPUError, "Invalid CPU number?");
|
176
|
-
|
177
|
-
scclktick=sysconf(_SC_CLK_TCK);
|
178
|
-
clock_speed = (psp.psp_iticksperclktick * scclktick) / 1000000;
|
179
|
-
|
180
|
-
/************************************************************************/
|
181
|
-
/* It appears that pstat_getprocessor does not return a failure code */
|
182
|
-
/* for an invalid processor number. So, we'll assume that if the clock */
|
183
|
-
/* speed is 0 that an invalid number was provided. */
|
184
|
-
/************************************************************************/
|
185
|
-
if(clock_speed <= 0)
|
186
|
-
rb_raise(cCPUError, "Invalid CPU number?");
|
187
|
-
|
188
|
-
return UINT2NUM(clock_speed);
|
189
|
-
}
|
190
|
-
|
191
|
-
void Init_cpu()
|
192
|
-
{
|
193
|
-
VALUE mSys, cCPU;
|
194
|
-
|
195
|
-
/* The Sys module serves as a toplevel namespace only */
|
196
|
-
mSys = rb_define_module("Sys");
|
197
|
-
|
198
|
-
/* The CPU class provides class methods for obtaining CPU information */
|
199
|
-
cCPU = rb_define_class_under(mSys, "CPU", rb_cObject);
|
200
|
-
|
201
|
-
/* The CPU::Error Exception class is raised whenever any of the CPU class
|
202
|
-
* methods fail.
|
203
|
-
*/
|
204
|
-
cCPUError = rb_define_class_under(cCPU, "Error", rb_eStandardError);
|
205
|
-
|
206
|
-
/* 0.6.4: The version of the sys-cpu library */
|
207
|
-
rb_define_const(cCPU, "VERSION", rb_str_new2(SYS_CPU_VERSION));
|
208
|
-
|
209
|
-
/* Class Methods */
|
210
|
-
rb_define_singleton_method(cCPU, "freq", cpu_freq, -1);
|
211
|
-
rb_define_singleton_method(cCPU, "num_cpu", cpu_num, 0);
|
212
|
-
rb_define_singleton_method(cCPU, "num_active_cpu", cpu_num_active, 0);
|
213
|
-
rb_define_singleton_method(cCPU, "load_avg", cpu_load_avg, -1);
|
214
|
-
rb_define_singleton_method(cCPU, "architecture", cpu_architecture, 0);
|
215
|
-
}
|
216
|
-
|
217
|
-
#ifdef __cplusplus
|
218
|
-
}
|
219
|
-
#endif
|