sys-cpu 0.6.2-x86-mingw32
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.
- data/CHANGES +144 -0
- data/MANIFEST +29 -0
- data/README +79 -0
- data/Rakefile +95 -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 +25 -0
- data/examples/example_sys_cpu_sunos.rb +21 -0
- data/examples/example_sys_cpu_windows.rb +24 -0
- data/ext/bsd/bsd.c +294 -0
- data/ext/extconf.rb +25 -0
- data/ext/hpux/hpux.c +219 -0
- data/ext/sunos/sunos.c +281 -0
- data/ext/version.h +2 -0
- data/install.rb +84 -0
- data/lib/linux/sys/cpu.rb +122 -0
- data/lib/windows/sys/cpu.rb +784 -0
- data/sys-cpu.gemspec +47 -0
- data/test/test_sys_cpu.rb +23 -0
- data/test/test_sys_cpu_bsd.rb +77 -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 +97 -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,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
|
+
}
|