sys-cpu 0.6.1-x86-linux → 0.6.2-x86-linux

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.
@@ -0,0 +1,19 @@
1
+ #######################################################################
2
+ # example_sys_cpu_bsd.rb
3
+ #
4
+ # Sample cript for general futzing. You can run this code via the
5
+ # 'rake example' task.
6
+ #
7
+ # Modify as you see fit.
8
+ #######################################################################
9
+ require "sys/cpu"
10
+ include Sys
11
+
12
+ puts "VERSION: " + CPU::VERSION
13
+
14
+ puts "Load Average: " + CPU.load_avg.join(", ")
15
+ puts "CPU Freq (speed): " + CPU.freq.to_s unless RUBY_PLATFORM.match('darwin')
16
+ puts "Num CPU: " + CPU.num_cpu.to_s
17
+ puts "Architecture: " + CPU.architecture
18
+ puts "Machine: " + CPU.machine
19
+ puts "Model: " + CPU.model
@@ -0,0 +1,27 @@
1
+ #######################################################################
2
+ # example_sys_cpu_hpux.rb
3
+ #
4
+ # Sample cript for general futzing. You can run this code via the
5
+ # 'rake example' task.
6
+ #
7
+ # Modify as you see fit.
8
+ #######################################################################
9
+ require "sys/cpu"
10
+ include Sys
11
+
12
+ puts "VERSION: " + CPU::VERSION
13
+ puts "========"
14
+
15
+ puts "Num CPU: " + CPU.num_cpu.to_s
16
+ puts "Active CPU: " + CPU.num_active_cpu.to_s
17
+ puts "Architecture: " + CPU.architecture
18
+ puts "Speed/Freq: " + CPU.freq.to_s
19
+
20
+ puts "Load average for CPU 0: " + CPU.load_avg(0).join(", ")
21
+ puts "Overall Load Average: " + CPU.load_avg.join(", ")
22
+
23
+ puts "Individual Loads Averages:"
24
+ puts "=========================="
25
+ CPU.load_avg{ |e|
26
+ p e
27
+ }
@@ -0,0 +1,25 @@
1
+ #######################################################################
2
+ # example_sys_cpu_linux.rb
3
+ #
4
+ # Sample cript for general futzing. You can run this code via the
5
+ # 'rake example' task.
6
+ #
7
+ # Modify as you see fit.
8
+ #######################################################################
9
+ require "sys/cpu"
10
+ require "pp"
11
+ include Sys
12
+
13
+ puts "VERSION: " + CPU::VERSION
14
+ puts "========"
15
+
16
+ puts "Load Average: " + CPU.load_avg.join(", ")
17
+
18
+ puts "Processor Info:"
19
+ puts "==============="
20
+ pp CPU.processors
21
+
22
+ puts "CPU STATS:"
23
+ puts "=========:"
24
+
25
+ pp CPU.cpu_stats
@@ -0,0 +1,21 @@
1
+ #######################################################################
2
+ # example_sys_cpu_sunos.rb
3
+ #
4
+ # Sample cript for general futzing. You can run this code via the
5
+ # 'rake example' task.
6
+ #
7
+ # Modify as you see fit.
8
+ #######################################################################
9
+ require "sys/cpu"
10
+ include Sys
11
+
12
+ puts "VERSION: " + CPU::VERSION
13
+ puts "========"
14
+
15
+ puts "Load Average: " + CPU.load_avg.join(", ")
16
+ puts "CPU Freq (speed): " + CPU.freq.to_s
17
+ puts "CPU State: " + CPU.state(0)
18
+ puts "Num CPU: " + CPU.num_cpu.to_s
19
+ puts "Type: " + CPU.cpu_type
20
+ puts "FPU Type: " + CPU.fpu_type
21
+ puts "Model: " + CPU.model
@@ -0,0 +1,24 @@
1
+ #######################################################################
2
+ # example_sys_cpu_windows.rb
3
+ #
4
+ # Sample cript for general futzing. You can run this code via the
5
+ # 'rake example' task.
6
+ #
7
+ # Modify as you see fit.
8
+ #######################################################################
9
+ require "sys/cpu"
10
+ include Sys
11
+
12
+ puts "VERSION: " + CPU::VERSION
13
+ puts "========"
14
+
15
+ puts "Architecture: " + CPU.architecture.to_s
16
+ puts "CPU Speed (Frequency): " + CPU.freq.to_s
17
+ puts "Load Average: " + CPU.load_average.to_s
18
+ puts "Model: " + CPU.model.to_s
19
+ puts "Type: " + CPU.type.to_s
20
+ puts "Num CPU: " + CPU.num_cpu.to_s
21
+
22
+ CPU.processors{ |cpu|
23
+ p cpu
24
+ }
data/ext/bsd/bsd.c ADDED
@@ -0,0 +1,294 @@
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
+ VALUE cCPUError;
34
+
35
+ /****************************************************************************
36
+ * Used for FreeBSD 4.x to determine CPU clock speed. Borrowed from cpuinfo.c
37
+ * in the MPlayer source code.
38
+ ****************************************************************************/
39
+ #if defined (__FreeBSD__) && (__FreeBSD__ < 5 )
40
+ static int64_t rdtsc(void){
41
+ unsigned int i, j;
42
+ #define RDTSC ".byte 0x0f, 0x31; "
43
+ asm(RDTSC : "=a"(i), "=d"(j) : );
44
+ return ((int64_t)j<<32) + (int64_t)i;
45
+ }
46
+ #endif
47
+
48
+ /*
49
+ * call-seq:
50
+ * CPU.load_average
51
+ *
52
+ * Returns an array of three floats indicating the 1, 5 and 15 minute load
53
+ * average.
54
+ */
55
+ static VALUE cpu_load_avg(VALUE klass){
56
+ double avgs[3];
57
+ int n, max = 3;
58
+ VALUE v_num_array = rb_ary_new();
59
+
60
+ #ifdef HAVE_KVM_H
61
+ kvm_t* k;
62
+
63
+ k = malloc(sizeof(kvm_t*));
64
+
65
+ if(!kvm_getloadavg(k, avgs, max)){
66
+ free(k);
67
+ rb_raise(cCPUError, "error calling kvm_getloadavg(): %s", strerror(errno));
68
+ }
69
+
70
+ for(n = 0; n < 3; n++)
71
+ rb_ary_push(v_num_array, rb_float_new(avgs[n]));
72
+
73
+ free(k);
74
+ #else
75
+ struct loadavg k;
76
+ size_t len = sizeof(k);
77
+
78
+ #ifdef HAVE_SYSCTLBYNAME
79
+ if(sysctlbyname("vm.loadavg", &k, &len, NULL, 0))
80
+ rb_raise(cCPUError, "error calling sysctlbyname(): %s", strerror(errno));
81
+ #else
82
+ int mib[2];
83
+ mib[0] = CTL_HW;
84
+ mib[1] = VM_LOADAVG;
85
+
86
+ if(sysctl(mib, 2, &k, &len, NULL, 0))
87
+ rb_raise(cCPUError, "error calling sysctl(): %s", strerror(errno));
88
+ #endif
89
+
90
+ for(n = 0; n < 3; n++)
91
+ rb_ary_push(v_num_array, rb_float_new(k.ldavg[n] / (float)k.fscale));
92
+ #endif
93
+
94
+ return v_num_array;
95
+ }
96
+
97
+ /*
98
+ * call-seq:
99
+ * CPU.num_cpu
100
+ *
101
+ * Returns the number of cpu's on your system. Note that each core on
102
+ * multi-core systems are counted as a cpu, e.g. one dual core cpu would
103
+ * return 2, not 1.
104
+ */
105
+ static VALUE cpu_num(VALUE klass){
106
+ int num_cpu;
107
+ size_t len = sizeof(num_cpu);
108
+
109
+ #ifdef HAVE_SYSCTLBYNAME
110
+ if(sysctlbyname("hw.ncpu", &num_cpu, &len, NULL, 0))
111
+ rb_raise(cCPUError, "error calling sysctlbyname(): %s", strerror(errno));
112
+ #else
113
+ int mib[2];
114
+ mib[0] = CTL_HW;
115
+ mib[1] = HW_NCPU;
116
+
117
+ if(sysctl(mib, 2, &num_cpu, &len, NULL, 0))
118
+ rb_raise(cCPUError, "error calling sysctl(): %s", strerror(errno));
119
+ #endif
120
+
121
+ return INT2NUM(num_cpu);
122
+ }
123
+
124
+ /*
125
+ * call-seq:
126
+ * CPU.model
127
+ *
128
+ * Returns a string indicating the cpu model.
129
+ */
130
+ static VALUE cpu_model(VALUE klass){
131
+ char model[64];
132
+ size_t len = sizeof(model);
133
+
134
+ #ifdef HAVE_SYSCTLBYNAME
135
+ if(sysctlbyname("hw.model", &model, &len, NULL, 0))
136
+ rb_raise(cCPUError, "error calling sysctlbyname(): %s", strerror(errno));
137
+ #else
138
+ int mib[2];
139
+ mib[0] = CTL_HW;
140
+ mib[1] = HW_MODEL;
141
+
142
+ if(sysctl(mib, 2, &model, &len, NULL, 0))
143
+ rb_raise(cCPUError, "error calling sysctl(): %s", strerror(errno));
144
+ #endif
145
+
146
+ return rb_str_new2(model);
147
+ }
148
+
149
+ /*
150
+ * call-seq:
151
+ * CPU.architecture
152
+ *
153
+ * Returns the cpu's architecture. On most systems this will be identical
154
+ * to the CPU.machine method. On OpenBSD it will be identical to the CPU.model
155
+ * method.
156
+ */
157
+ static VALUE cpu_architecture(VALUE klass){
158
+ char arch[32];
159
+ size_t len = sizeof(arch);
160
+
161
+ #ifdef HAVE_SYSCTLBYNAME
162
+ #if defined(__MACH__) && defined(__APPLE__)
163
+ if(sysctlbyname("hw.machine", &arch, &len, NULL, 0))
164
+ rb_raise(cCPUError, "error calling sysctlbyname(): %s", strerror(errno));
165
+ #else
166
+ if(sysctlbyname("hw.machine_arch", &arch, &len, NULL, 0))
167
+ rb_raise(cCPUError, "error calling sysctlbyname(): %s", strerror(errno));
168
+ #endif
169
+ #else
170
+ int mib[2];
171
+ mib[0] = CTL_VM;
172
+ #ifdef HW_MACHINE_ARCH
173
+ mib[1] = HW_MACHINE_ARCH;
174
+ #else
175
+ mib[1] = HW_MODEL;
176
+ #endif
177
+
178
+ if(sysctl(mib, 2, &arch, &len, NULL, 0))
179
+ rb_raise(cCPUError, "error calling sysctl(): %s", strerror(errno));
180
+ #endif
181
+
182
+ return rb_str_new2(arch);
183
+ }
184
+
185
+ /*
186
+ * call-seq:
187
+ * CPU.machine
188
+ *
189
+ * Returns the cpu's class type. On most systems this will be identical
190
+ * to the CPU.architecture method. On OpenBSD it will be identical to the
191
+ * CPU.model method.
192
+ */
193
+ static VALUE cpu_machine(VALUE klass){
194
+ char machine[32];
195
+ size_t len = sizeof(machine);
196
+
197
+ #ifdef HAVE_SYSCTLBYNAME
198
+ if(sysctlbyname("hw.machine", &machine, &len, NULL, 0))
199
+ rb_raise(cCPUError, "error calling sysctlbyname(): %s", strerror(errno));
200
+ #else
201
+ int mib[2];
202
+ mib[0] = CTL_HW;
203
+ #ifdef HW_MACHINE_ARCH
204
+ mib[1] = HW_MACHINE;
205
+ #else
206
+ mib[1] = HW_MODEL;
207
+ #endif
208
+
209
+ if(sysctl(mib, 2, &machine, &len, NULL, 0))
210
+ rb_raise(cCPUError, "error calling sysctl(): %s", strerror(errno));
211
+ #endif
212
+
213
+ return rb_str_new2(machine);
214
+ }
215
+
216
+ /*
217
+ * call-seq:
218
+ * CPU.freq
219
+ *
220
+ * Returns an integer indicating the speed (i.e. frequency in Mhz) of the cpu.
221
+ *
222
+ * Not supported on OS X.
223
+ *--
224
+ * Not supported on OS X currently. The sysctl() approach returns a bogus
225
+ * hard-coded value.
226
+ *
227
+ * TODO: Fix for OS X.
228
+ */
229
+ static VALUE cpu_freq(VALUE klass){
230
+ int mhz;
231
+ #if defined (__FreeBSD__) && (__FreeBSD__ < 5)
232
+ int64_t tsc_start, tsc_end;
233
+ struct timeval tv_start, tv_end;
234
+ int usec_delay;
235
+
236
+ tsc_start = rdtsc();
237
+ gettimeofday(&tv_start,NULL);
238
+ #ifdef MISSING_USLEEP
239
+ sleep(1);
240
+ #else
241
+ usleep(100000);
242
+ #endif
243
+ tsc_end = rdtsc();
244
+ gettimeofday(&tv_end,NULL);
245
+
246
+ usec_delay = 1000000 * (tv_end.tv_sec - tv_start.tv_sec)
247
+ + (tv_end.tv_usec - tv_start.tv_usec);
248
+
249
+ mhz = ((tsc_end - tsc_start) / usec_delay);
250
+ #else
251
+ size_t len = sizeof(mhz);
252
+ #ifdef HAVE_SYSCTLBYNAME
253
+ if(sysctlbyname("hw.clockrate", &mhz, &len, 0, 0))
254
+ rb_raise(cCPUError, "error calling sysctlbyname(): %s", strerror(errno));
255
+ #else
256
+ int mib[2];
257
+
258
+ mib[0] = CTL_KERN;
259
+ mib[1] = KERN_CLOCKRATE;
260
+
261
+ if(sysctl(mib, 2, &mhz, &len, NULL, 0))
262
+ rb_raise(cCPUError,"error calling sysctlbyname(): %s", strerror(errno));
263
+ #endif
264
+ #endif
265
+
266
+ return INT2NUM(mhz);
267
+ }
268
+
269
+ void Init_cpu()
270
+ {
271
+ VALUE mSys, cCPU;
272
+
273
+ /* The Sys module serves as a toplevel namespace only */
274
+ mSys = rb_define_module("Sys");
275
+
276
+ /* The CPU class provides class methods for obtaining CPU information */
277
+ cCPU = rb_define_class_under(mSys, "CPU", rb_cObject);
278
+
279
+ /* The CPU::Error Exception class is raised whenever any of the CPU class
280
+ * methods fail.
281
+ */
282
+ cCPUError = rb_define_class_under(cCPU, "Error", rb_eStandardError);
283
+
284
+ /* 0.6.2: The version of the sys-cpu library */
285
+ rb_define_const(cCPU, "VERSION", rb_str_new2(SYS_CPU_VERSION));
286
+
287
+ /* Class Methods */
288
+ rb_define_singleton_method(cCPU, "architecture", cpu_architecture, 0);
289
+ rb_define_singleton_method(cCPU, "freq", cpu_freq, 0);
290
+ rb_define_singleton_method(cCPU, "load_avg", cpu_load_avg, 0);
291
+ rb_define_singleton_method(cCPU, "machine", cpu_machine, 0);
292
+ rb_define_singleton_method(cCPU, "model", cpu_model, 0);
293
+ rb_define_singleton_method(cCPU, "num_cpu", cpu_num, 0);
294
+ }
data/ext/extconf.rb ADDED
@@ -0,0 +1,25 @@
1
+ require "mkmf"
2
+ require "fileutils"
3
+
4
+ File.delete('cpu.c') if File.exists?('cpu.c')
5
+
6
+ case RUBY_PLATFORM
7
+ when /hpux/i
8
+ FileUtils.cp("hpux/hpux.c", "cpu.c")
9
+ when /sunos|solaris/i
10
+ FileUtils.cp("sunos/sunos.c", "cpu.c")
11
+ unless have_func("getloadavg")
12
+ have_library("kstat")
13
+ end
14
+ when /bsd|darwin/i
15
+ FileUtils.cp("bsd/bsd.c", "cpu.c")
16
+ have_func("sysctlbyname")
17
+ have_library("kvm")
18
+ have_header("kvm.h")
19
+ when /linux|dos|windows|win32|mingw|cygwin/i
20
+ STDERR.puts "Run 'ruby install.rb' instead for this platform"
21
+ else
22
+ STDERR.puts "This platform is not currently supported. Exiting..."
23
+ end
24
+
25
+ create_makefile("sys/cpu")
data/ext/hpux/hpux.c ADDED
@@ -0,0 +1,219 @@
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.2: 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