sys-cpu 0.6.4-universal-mingw32
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES +160 -0
- data/MANIFEST +29 -0
- data/README +73 -0
- data/Rakefile +89 -0
- data/doc/bsd.txt +49 -0
- data/doc/hpux.txt +54 -0
- data/doc/linux.txt +41 -0
- data/doc/sunos.txt +56 -0
- data/doc/windows.txt +130 -0
- data/examples/example_sys_cpu_bsd.rb +19 -0
- data/examples/example_sys_cpu_hpux.rb +27 -0
- data/examples/example_sys_cpu_linux.rb +24 -0
- data/examples/example_sys_cpu_sunos.rb +21 -0
- data/examples/example_sys_cpu_windows.rb +24 -0
- data/ext/bsd/bsd.c +331 -0
- data/ext/extconf.rb +26 -0
- data/ext/hpux/hpux.c +219 -0
- data/ext/sunos/sunos.c +281 -0
- data/ext/version.h +2 -0
- data/install.rb +85 -0
- data/lib/linux/sys/cpu.rb +122 -0
- data/lib/windows/sys/cpu.rb +790 -0
- data/sys-cpu.gemspec +50 -0
- data/test/test_sys_cpu.rb +23 -0
- data/test/test_sys_cpu_bsd.rb +76 -0
- data/test/test_sys_cpu_hpux.rb +52 -0
- data/test/test_sys_cpu_linux.rb +34 -0
- data/test/test_sys_cpu_sunos.rb +67 -0
- data/test/test_sys_cpu_version.rb +18 -0
- data/test/test_sys_cpu_windows.rb +72 -0
- metadata +113 -0
data/doc/linux.txt
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
== Description
|
2
|
+
Sys::CPU - An interface for various cpu statistics
|
3
|
+
|
4
|
+
== Synopsis
|
5
|
+
require "sys/cpu"
|
6
|
+
include Sys
|
7
|
+
|
8
|
+
CPU.processors{ |cs|
|
9
|
+
cs.members.each{ |m|
|
10
|
+
puts "#{m}: " + cs[m].to_s
|
11
|
+
}
|
12
|
+
}
|
13
|
+
|
14
|
+
CPU.bogomips(1) # -> returns bogomips for cpu #2
|
15
|
+
|
16
|
+
== Notes
|
17
|
+
Portions of this documentation built dynamically
|
18
|
+
|
19
|
+
== Constants
|
20
|
+
VERSION
|
21
|
+
Returns the current version number for this package as a String.
|
22
|
+
|
23
|
+
== Class Methods
|
24
|
+
CPU.load_avg
|
25
|
+
Returns an array of three floats indicating the 1, 5 and 15 minute load
|
26
|
+
average.
|
27
|
+
|
28
|
+
CPU.cpu_stats
|
29
|
+
Returns a hash, with the cpu number as the key and an array as the value.
|
30
|
+
The array contains the number of seconds that the system spent in
|
31
|
+
user mode, user mode with low priority (nice), system mode, and the
|
32
|
+
idle task, respectively, for that cpu.
|
33
|
+
|
34
|
+
CPU.processors{ |cpu struct| block }
|
35
|
+
Calls the block for each processor on your system, passing a CPUStruct as the
|
36
|
+
parameter.
|
37
|
+
|
38
|
+
The exact members of the CPUStruct are the same as the class method names, except
|
39
|
+
for CPU.processors (although you may optionally omit the "?" when referring to a
|
40
|
+
struct member). These were determined when you installed this package because they
|
41
|
+
vary from one chip architecture to another.
|
data/doc/sunos.txt
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
== Description
|
2
|
+
Sys::CPU - An interface for various cpu statistics
|
3
|
+
|
4
|
+
== Synopsis
|
5
|
+
require "sys/cpu"
|
6
|
+
include Sys
|
7
|
+
|
8
|
+
# Solaris
|
9
|
+
puts "Mhz: " + CPU.cpu_freq(0).to_s
|
10
|
+
puts "State: " + CPU.state(0)
|
11
|
+
puts "Number of cpu's on this system: " + CPU.num_cpu.to_s
|
12
|
+
puts "CPU type: " + CPU.type
|
13
|
+
puts "FPU type: " + CPU.fpu_type
|
14
|
+
puts "CPU model: " + CPU.model
|
15
|
+
puts "Load averages: " + CPU.load_avg.join(", ")
|
16
|
+
|
17
|
+
== Constants
|
18
|
+
VERSION
|
19
|
+
Returns the current version number for this package.
|
20
|
+
|
21
|
+
== Class Methods
|
22
|
+
CPU.freq(cpu_num=0)
|
23
|
+
Returns an integer indicating the speed (i.e. frequency in Mhz) of
|
24
|
+
'cpu_num', or CPU 0 if no number is provided.
|
25
|
+
|
26
|
+
CPU.type
|
27
|
+
Returns a string indicating the type of processor. This is the
|
28
|
+
architecture (e.g. sparcv9), not the exact model (e.g. Ultra-IIe).
|
29
|
+
Returns nil if not found.
|
30
|
+
|
31
|
+
CPU.fpu_type
|
32
|
+
Returns a string indicating the type of floating point unit, or nil if
|
33
|
+
not found.
|
34
|
+
|
35
|
+
CPU.load_avg
|
36
|
+
Returns an array of three floats indicating the 1, 5 and 15 minute load
|
37
|
+
average.
|
38
|
+
|
39
|
+
CPU.model
|
40
|
+
Returns a string indicating the cpu model. For now, this is the
|
41
|
+
architecture type, rather than the exact model. However, see the
|
42
|
+
'Future Plans' section below.
|
43
|
+
|
44
|
+
CPU.num_cpu
|
45
|
+
Returns an integer indicating the number of cpu's on the system.
|
46
|
+
|
47
|
+
CPU.state(cpu_num)
|
48
|
+
Returns a string indicating the cpu state of 'cpu_num'.
|
49
|
+
|
50
|
+
== Error Classes
|
51
|
+
CPU::Error < StandardError
|
52
|
+
Raised is response to internal function errors, usually relating to an
|
53
|
+
invalid cpu number.
|
54
|
+
|
55
|
+
== More Information
|
56
|
+
See the README file for more information.
|
data/doc/windows.txt
ADDED
@@ -0,0 +1,130 @@
|
|
1
|
+
== Description
|
2
|
+
Sys::CPU - An interface for various cpu statistics
|
3
|
+
|
4
|
+
== Synopsis
|
5
|
+
require "sys/cpu"
|
6
|
+
include Sys
|
7
|
+
|
8
|
+
CPU.processors{ |cs|
|
9
|
+
cs.members.each{ |m|
|
10
|
+
puts "#{m}: " + cs[m].to_s
|
11
|
+
}
|
12
|
+
}
|
13
|
+
|
14
|
+
puts "CPU Speed (Frequency): " + CPU.freq(0).to_s
|
15
|
+
puts "Load avg: " + CPU.load_avg.to_s
|
16
|
+
puts "Num CPU: " + CPU.num_cpu.to_s
|
17
|
+
puts "Type: " + CPU.type
|
18
|
+
puts "Model: " + CPU.model
|
19
|
+
puts "Architecture: " + CPU.architecture
|
20
|
+
|
21
|
+
== Constants
|
22
|
+
VERSION
|
23
|
+
Returns the current version number for this package.
|
24
|
+
|
25
|
+
== Class Methods
|
26
|
+
CPU.architecture(host=localhost)
|
27
|
+
Returns the architecture of the cpu, e.g. x86 Family 15 Model 2
|
28
|
+
|
29
|
+
CPU.freq(cpu_num=0, host=localhost)
|
30
|
+
Returns an integer indicating the speed (i.e. frequency in Mhz) of
|
31
|
+
'cpu_num'. If cpu_num+1 is greater than the number of cpu's on
|
32
|
+
your system, or this call fails for any other reason, a CPUError
|
33
|
+
is raised.
|
34
|
+
|
35
|
+
CPU.load_avg(host=localhost)
|
36
|
+
Returns the load capacity of each processor, averaged to the last second.
|
37
|
+
Processor loading refers to the total computing burden for each
|
38
|
+
processor at one time.
|
39
|
+
|
40
|
+
Note that this attribute is actually the LoadPercentage. I may use
|
41
|
+
one of the Win32_Perf* classes in the future.
|
42
|
+
|
43
|
+
CPU.model(host=localhost)
|
44
|
+
Returns a string indicating the cpu model, e.g. Intel Pentium 4
|
45
|
+
|
46
|
+
CPU.num_cpu(host=localhost)
|
47
|
+
Returns an integer indicating the number of cpu's on the system.
|
48
|
+
|
49
|
+
CPU.processors(host=localhost){ |cpu| ... }
|
50
|
+
Returns a CPUStruct for each CPU on the system containing the following
|
51
|
+
members:
|
52
|
+
|
53
|
+
* address_width
|
54
|
+
* architecture
|
55
|
+
* availability
|
56
|
+
* caption
|
57
|
+
* config_manager_error_code
|
58
|
+
* config_manager_user_config
|
59
|
+
* cpu_status
|
60
|
+
* creation_class_name
|
61
|
+
* freq
|
62
|
+
* voltage
|
63
|
+
* data_width
|
64
|
+
* description
|
65
|
+
* device_id
|
66
|
+
* error_cleared?
|
67
|
+
* error_description
|
68
|
+
* ext_clock
|
69
|
+
* family
|
70
|
+
* install_date
|
71
|
+
* l2_cache_size
|
72
|
+
* l2_cache_speed
|
73
|
+
* last_error_code
|
74
|
+
* level
|
75
|
+
* load_avg
|
76
|
+
* manufacturer
|
77
|
+
* max_clock_speed
|
78
|
+
* name
|
79
|
+
* other_family_description
|
80
|
+
* pnp_device_id
|
81
|
+
* power_management_supported?
|
82
|
+
* power_management_capabilities
|
83
|
+
* processor_id
|
84
|
+
* processor_type
|
85
|
+
* revision
|
86
|
+
* role
|
87
|
+
* socket_designation
|
88
|
+
* status
|
89
|
+
* status_info
|
90
|
+
* stepping
|
91
|
+
* system_creation_class_name
|
92
|
+
* system_name
|
93
|
+
* unique_id
|
94
|
+
* upgrade_method
|
95
|
+
* version
|
96
|
+
* voltage_caps
|
97
|
+
|
98
|
+
Note that not all of these members will necessarily be defined. See the
|
99
|
+
NOTES section below.
|
100
|
+
|
101
|
+
CPU.type(host=localhost)
|
102
|
+
Returns a string indicating the type of processor, e.g. GenuineIntel.
|
103
|
+
|
104
|
+
== Exception Classes
|
105
|
+
CPU::Error < StandardError
|
106
|
+
Raised is response to internal function errors, most likely to be raised
|
107
|
+
in the event that in invalid cpu number is provided for the 'freq'
|
108
|
+
method.
|
109
|
+
|
110
|
+
== Notes
|
111
|
+
Some of the CPUStruct members may be nil. As far as I can tell, this
|
112
|
+
means that Windows is unable to determine the value for the attribute due
|
113
|
+
to your BIOS and/or BIOS settings. There is nothing clear in the
|
114
|
+
documentation I could find for this, however.
|
115
|
+
|
116
|
+
The 'family' attribute may contain the trademark symbol.
|
117
|
+
|
118
|
+
The 'status_info' attribute was not converted to a string because the
|
119
|
+
'status' attribute already is a string. I'm not even sure why MS has both
|
120
|
+
versions.
|
121
|
+
|
122
|
+
The 'system_name' attribute appears to be the hostname. I have no idea
|
123
|
+
why this is included as part of the CPU information, but there you go.
|
124
|
+
|
125
|
+
See http://tinyurl.com/2mros (and click on 'Win32_Processor' in the left
|
126
|
+
frame) for a list of attributes and their meaning. Link courtesy of
|
127
|
+
tinyurl.com.
|
128
|
+
|
129
|
+
== More Information
|
130
|
+
See the README file for more information.
|
@@ -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,24 @@
|
|
1
|
+
#######################################################################
|
2
|
+
# example_sys_cpu_linux.rb
|
3
|
+
#
|
4
|
+
# Sample cript for general futzing. You should 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
|
+
|
12
|
+
puts "VERSION: " + Sys::CPU::VERSION
|
13
|
+
puts "========"
|
14
|
+
|
15
|
+
puts "Load Average: " + Sys::CPU.load_avg.join(", ")
|
16
|
+
|
17
|
+
puts "Processor Info:"
|
18
|
+
puts "==============="
|
19
|
+
pp Sys::CPU.processors
|
20
|
+
|
21
|
+
puts "CPU STATS:"
|
22
|
+
puts "=========:"
|
23
|
+
|
24
|
+
pp Sys::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,331 @@
|
|
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
|
+
}
|