Class: Sys::CPU
- Inherits:
-
Object
- Object
- Sys::CPU
- Defined in:
- lib/linux/sys/cpu.rb,
lib/windows/sys/cpu.rb,
ext/bsd/bsd.c
Overview
Encapsulates system CPU information
Defined Under Namespace
Constant Summary collapse
- VERSION =
The version of the sys-cpu library
0.6.4
Class Method Summary collapse
-
.architecture ⇒ Object
Returns the cpu’s architecture.
-
.cpu_stats ⇒ Object
Returns a hash of arrays that contain the number of seconds that the system spent in user mode, user mode with low priority (nice), system mode, and the idle task, respectively.
-
.freq ⇒ Object
Returns an integer indicating the speed (i.e. frequency in Mhz) of the cpu.
-
.load_average ⇒ Object
Returns an array of three floats indicating the 1, 5 and 15 minute load average.
-
.machine ⇒ Object
Returns the cpu’s class type.
-
.model ⇒ Object
Returns a string indicating the cpu model.
-
.num_cpu ⇒ Object
Returns the number of cpu’s on your system.
-
.processors(host = Socket.gethostname) ⇒ Object
Returns a CPUStruct for each CPU on
host
, or the localhost if nohost
is specified. -
.type(host = Socket.gethostname) ⇒ Object
Returns a string indicating the type of processor, e.g.
Class Method Details
.architecture ⇒ Object
Returns the cpu’s architecture. On most systems this will be identical to the CPU.machine method. On OpenBSD it will be identical to the CPU.model method.
75 76 77 78 79 80 81 82 83 84 |
# File 'lib/windows/sys/cpu.rb', line 75 def self.architecture(host=Socket.gethostname) cs = BASE_CS + "//#{host}/root/cimv2:Win32_Processor='cpu0'" begin wmi = WIN32OLE.connect(cs) rescue WIN32OLERuntimeError => e raise Error, e else self.get_cpu_arch(wmi.Architecture) end end |
.cpu_stats ⇒ Object
Returns a hash of arrays that contain the number of seconds that the system spent in user mode, user mode with low priority (nice), system mode, and the idle task, respectively.
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/linux/sys/cpu.rb', line 99 def self.cpu_stats cpu_stat_file = "/proc/stat" hash = {} # Hash needed for multi-cpu systems lines = IO.readlines(cpu_stat_file) lines.each_with_index{ |line, i| array = line.split break unless array[0] =~ /cpu/ # 'cpu' entries always on top # Some machines list a 'cpu' and a 'cpu0'. In this case only # return values for the numbered cpu entry. if lines[i].split[0] == "cpu" && lines[i+1].split[0] =~ /cpu\d/ next end vals = array[1..-1].map{ |e| e = e.to_i / 100 } # 100 jiffies/sec. hash[array[0]] = vals } hash end |
.freq ⇒ Object
Returns an integer indicating the speed (i.e. frequency in Mhz) of the cpu.
91 92 93 94 95 96 97 98 99 100 |
# File 'lib/windows/sys/cpu.rb', line 91 def self.freq(cpu_num = 0, host = Socket.gethostname) cs = BASE_CS + "//#{host}/root/cimv2:Win32_Processor='cpu#{cpu_num}'" begin wmi = WIN32OLE.connect(cs) rescue WIN32OLERuntimeError => e raise Error, e else return wmi.CurrentClockSpeed end end |
.load_average ⇒ Object
Returns an array of three floats indicating the 1, 5 and 15 minute load average.
90 91 92 93 |
# File 'lib/linux/sys/cpu.rb', line 90 def self.load_avg load_avg_file = "/proc/loadavg" IO.readlines(load_avg_file).first.split[0..2].map{ |e| e.to_f } end |
.machine ⇒ Object
Returns the cpu’s class type. On most systems this will be identical to the CPU.architecture method. On OpenBSD it will be identical to the CPU.model method.
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'ext/bsd/bsd.c', line 227
static VALUE cpu_machine(VALUE klass){
char machine[32];
size_t len = sizeof(machine);
#ifdef HAVE_SYSCTLBYNAME
if(sysctlbyname("hw.machine", &machine, &len, NULL, 0))
rb_raise(cCPUError, "error calling sysctlbyname(): %s", strerror(errno));
#else
int mib[2];
mib[0] = CTL_HW;
#ifdef HW_MACHINE_ARCH
mib[1] = HW_MACHINE;
#else
mib[1] = HW_MODEL;
#endif
if(sysctl(mib, 2, &machine, &len, NULL, 0))
rb_raise(cCPUError, "error calling sysctl(): %s", strerror(errno));
#endif
return rb_str_new2(machine);
}
|
.model ⇒ Object
Returns a string indicating the cpu model.
123 124 125 126 127 128 129 130 131 132 |
# File 'lib/windows/sys/cpu.rb', line 123 def self.model(host = Socket.gethostname) cs = BASE_CS + "//#{host}/root/cimv2:Win32_Processor='cpu0'" begin wmi = WIN32OLE.connect(cs) rescue WIN32OLERuntimeError => e raise Error, e else return wmi.Name end end |
.num_cpu ⇒ Object
Returns the number of cpu’s on your system. Note that each core on multi-core systems are counted as a cpu, e.g. one dual core cpu would return 2, not 1.
138 139 140 141 142 143 144 145 146 147 |
# File 'lib/windows/sys/cpu.rb', line 138 def self.num_cpu(host = Socket.gethostname) cs = BASE_CS + "//#{host}/root/cimv2:Win32_ComputerSystem='#{host}'" begin wmi = WIN32OLE.connect(cs) rescue WIN32OLERuntimeError => e raise Error, e else return wmi.NumberOfProcessors end end |
.processors(host = Socket.gethostname) ⇒ Object
Returns a CPUStruct for each CPU on host
, or the localhost if no host
is specified. A CPUStruct contains the following members:
-
address_width
-
architecture
-
availability
-
caption
-
config_manager_error_code
-
config_manager_user_config
-
cpu_status
-
creation_class_name
-
freq
-
voltage
-
data_width
-
description
-
device_id
-
error_cleared?
-
error_description
-
ext_clock
-
family
-
install_date
-
l2_cache_size
-
l2_cache_speed
-
last_error_code
-
level
-
load_avg
-
manufacturer
-
max_clock_speed
-
name
-
other_family_description
-
pnp_device_id
-
power_management_supported?
-
power_management_capabilities
-
processor_id
-
processor_type
-
revision
-
role
-
socket_designation
-
status
-
status_info
-
stepping
-
system_creation_class_name
-
system_name
-
unique_id
-
upgrade_method
-
version
-
voltage_caps
Note that not all of these members will necessarily be defined.
58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/linux/sys/cpu.rb', line 58 def self.processors array = [] $cpu_array.each{ |hash| struct = CPUStruct.new struct.members.each{ |m| struct.send("#{m}=", hash[m]) } if block_given? yield struct else array << struct end } array unless block_given? end |
.type(host = Socket.gethostname) ⇒ Object
Returns a string indicating the type of processor, e.g. GenuineIntel.
258 259 260 261 262 263 264 265 266 267 |
# File 'lib/windows/sys/cpu.rb', line 258 def self.type(host = Socket.gethostname) cs = BASE_CS + "//#{host}/root/cimv2:Win32_Processor='cpu0'" begin wmi = WIN32OLE.connect(cs) rescue WIN32OLERuntimeError => e raise Error, e else return wmi.Manufacturer end end |