sys-cpu 0.6.1 → 0.6.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -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 CHANGED
@@ -7,14 +7,20 @@
7
7
  * Based on the Unix::Processors Perl module (Wilson Snyder) with ideas from
8
8
  * Sys::CPU (Matt Sanford) and Solaris::Kstat (Alan Burlison) as well.
9
9
  * OS X 10.5+ patch for uptime by Victor Costan.
10
- *
10
+ *
11
11
  * Portions of this code lifted from the MPlayer source (cpuinfo.c).
12
12
  *****************************************************************************/
13
13
  #include <ruby.h>
14
14
  #include "version.h"
15
+
15
16
  #ifdef HAVE_KVM_H
16
17
  #include <kvm.h>
17
18
  #endif
19
+
20
+ #if defined (__OpenBSD__)
21
+ #include <sys/param.h>
22
+ #endif
23
+
18
24
  #include <sys/sysctl.h>
19
25
  #include <sys/types.h>
20
26
  #include <string.h>
@@ -27,7 +33,7 @@
27
33
  VALUE cCPUError;
28
34
 
29
35
  /****************************************************************************
30
- * Used for FreeBSD 4.x to determine CPU clock speed. Stolen from cpuinfo.c
36
+ * Used for FreeBSD 4.x to determine CPU clock speed. Borrowed from cpuinfo.c
31
37
  * in the MPlayer source code.
32
38
  ****************************************************************************/
33
39
  #if defined (__FreeBSD__) && (__FreeBSD__ < 5 )
@@ -41,8 +47,8 @@ static int64_t rdtsc(void){
41
47
 
42
48
  /*
43
49
  * call-seq:
44
- * CPU.load_average
45
- *
50
+ * CPU.load_average
51
+ *
46
52
  * Returns an array of three floats indicating the 1, 5 and 15 minute load
47
53
  * average.
48
54
  */
@@ -53,14 +59,14 @@ static VALUE cpu_load_avg(VALUE klass){
53
59
 
54
60
  #ifdef HAVE_KVM_H
55
61
  kvm_t* k;
56
-
62
+
57
63
  k = malloc(sizeof(kvm_t*));
58
-
64
+
59
65
  if(!kvm_getloadavg(k, avgs, max)){
60
66
  free(k);
61
67
  rb_raise(cCPUError, "error calling kvm_getloadavg(): %s", strerror(errno));
62
68
  }
63
-
69
+
64
70
  for(n = 0; n < 3; n++)
65
71
  rb_ary_push(v_num_array, rb_float_new(avgs[n]));
66
72
 
@@ -68,7 +74,7 @@ static VALUE cpu_load_avg(VALUE klass){
68
74
  #else
69
75
  struct loadavg k;
70
76
  size_t len = sizeof(k);
71
-
77
+
72
78
  #ifdef HAVE_SYSCTLBYNAME
73
79
  if(sysctlbyname("vm.loadavg", &k, &len, NULL, 0))
74
80
  rb_raise(cCPUError, "error calling sysctlbyname(): %s", strerror(errno));
@@ -76,13 +82,13 @@ static VALUE cpu_load_avg(VALUE klass){
76
82
  int mib[2];
77
83
  mib[0] = CTL_HW;
78
84
  mib[1] = VM_LOADAVG;
79
-
85
+
80
86
  if(sysctl(mib, 2, &k, &len, NULL, 0))
81
87
  rb_raise(cCPUError, "error calling sysctl(): %s", strerror(errno));
82
88
  #endif
83
-
89
+
84
90
  for(n = 0; n < 3; n++)
85
- rb_ary_push(v_num_array, rb_float_new(k.ldavg[n] / (float)k.fscale));
91
+ rb_ary_push(v_num_array, rb_float_new(k.ldavg[n] / (float)k.fscale));
86
92
  #endif
87
93
 
88
94
  return v_num_array;
@@ -107,12 +113,12 @@ static VALUE cpu_num(VALUE klass){
107
113
  int mib[2];
108
114
  mib[0] = CTL_HW;
109
115
  mib[1] = HW_NCPU;
110
-
116
+
111
117
  if(sysctl(mib, 2, &num_cpu, &len, NULL, 0))
112
118
  rb_raise(cCPUError, "error calling sysctl(): %s", strerror(errno));
113
119
  #endif
114
120
 
115
- return INT2NUM(num_cpu);
121
+ return INT2NUM(num_cpu);
116
122
  }
117
123
 
118
124
  /*
@@ -132,7 +138,7 @@ static VALUE cpu_model(VALUE klass){
132
138
  int mib[2];
133
139
  mib[0] = CTL_HW;
134
140
  mib[1] = HW_MODEL;
135
-
141
+
136
142
  if(sysctl(mib, 2, &model, &len, NULL, 0))
137
143
  rb_raise(cCPUError, "error calling sysctl(): %s", strerror(errno));
138
144
  #endif
@@ -145,7 +151,8 @@ static VALUE cpu_model(VALUE klass){
145
151
  * CPU.architecture
146
152
  *
147
153
  * Returns the cpu's architecture. On most systems this will be identical
148
- * to the CPU.machine method.
154
+ * to the CPU.machine method. On OpenBSD it will be identical to the CPU.model
155
+ * method.
149
156
  */
150
157
  static VALUE cpu_architecture(VALUE klass){
151
158
  char arch[32];
@@ -162,7 +169,11 @@ static VALUE cpu_architecture(VALUE klass){
162
169
  #else
163
170
  int mib[2];
164
171
  mib[0] = CTL_VM;
172
+ #ifdef HW_MACHINE_ARCH
165
173
  mib[1] = HW_MACHINE_ARCH;
174
+ #else
175
+ mib[1] = HW_MODEL;
176
+ #endif
166
177
 
167
178
  if(sysctl(mib, 2, &arch, &len, NULL, 0))
168
179
  rb_raise(cCPUError, "error calling sysctl(): %s", strerror(errno));
@@ -176,7 +187,8 @@ static VALUE cpu_architecture(VALUE klass){
176
187
  * CPU.machine
177
188
  *
178
189
  * Returns the cpu's class type. On most systems this will be identical
179
- * to the CPU.architecture method.
190
+ * to the CPU.architecture method. On OpenBSD it will be identical to the
191
+ * CPU.model method.
180
192
  */
181
193
  static VALUE cpu_machine(VALUE klass){
182
194
  char machine[32];
@@ -188,7 +200,11 @@ static VALUE cpu_machine(VALUE klass){
188
200
  #else
189
201
  int mib[2];
190
202
  mib[0] = CTL_HW;
203
+ #ifdef HW_MACHINE_ARCH
191
204
  mib[1] = HW_MACHINE;
205
+ #else
206
+ mib[1] = HW_MODEL;
207
+ #endif
192
208
 
193
209
  if(sysctl(mib, 2, &machine, &len, NULL, 0))
194
210
  rb_raise(cCPUError, "error calling sysctl(): %s", strerror(errno));
@@ -202,12 +218,12 @@ static VALUE cpu_machine(VALUE klass){
202
218
  * CPU.freq
203
219
  *
204
220
  * Returns an integer indicating the speed (i.e. frequency in Mhz) of the cpu.
205
- *
221
+ *
206
222
  * Not supported on OS X.
207
223
  *--
208
224
  * Not supported on OS X currently. The sysctl() approach returns a bogus
209
225
  * hard-coded value.
210
- *
226
+ *
211
227
  * TODO: Fix for OS X.
212
228
  */
213
229
  static VALUE cpu_freq(VALUE klass){
@@ -216,7 +232,7 @@ static VALUE cpu_freq(VALUE klass){
216
232
  int64_t tsc_start, tsc_end;
217
233
  struct timeval tv_start, tv_end;
218
234
  int usec_delay;
219
-
235
+
220
236
  tsc_start = rdtsc();
221
237
  gettimeofday(&tv_start,NULL);
222
238
  #ifdef MISSING_USLEEP
@@ -226,10 +242,10 @@ static VALUE cpu_freq(VALUE klass){
226
242
  #endif
227
243
  tsc_end = rdtsc();
228
244
  gettimeofday(&tv_end,NULL);
229
-
245
+
230
246
  usec_delay = 1000000 * (tv_end.tv_sec - tv_start.tv_sec)
231
247
  + (tv_end.tv_usec - tv_start.tv_usec);
232
-
248
+
233
249
  mhz = ((tsc_end - tsc_start) / usec_delay);
234
250
  #else
235
251
  size_t len = sizeof(mhz);
@@ -242,7 +258,7 @@ static VALUE cpu_freq(VALUE klass){
242
258
  mib[0] = CTL_KERN;
243
259
  mib[1] = KERN_CLOCKRATE;
244
260
 
245
- if(sysctl(mib, 2, &mhz, &len, NULL, 0)
261
+ if(sysctl(mib, 2, &mhz, &len, NULL, 0))
246
262
  rb_raise(cCPUError,"error calling sysctlbyname(): %s", strerror(errno));
247
263
  #endif
248
264
  #endif
@@ -265,7 +281,7 @@ void Init_cpu()
265
281
  */
266
282
  cCPUError = rb_define_class_under(cCPU, "Error", rb_eStandardError);
267
283
 
268
- /* 0.6.1: The version of this package, returned as a String */
284
+ /* 0.6.2: The version of the sys-cpu library */
269
285
  rb_define_const(cCPU, "VERSION", rb_str_new2(SYS_CPU_VERSION));
270
286
 
271
287
  /* Class Methods */
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