class Sys::CPU
Encapsulates system CPU information
Constants
- CPU_ARCH_ABI64
- CPU_TYPE_POWERPC
- CPU_TYPE_POWERPC64
- CPU_TYPE_SPARC
- CPU_TYPE_X86
- CPU_TYPE_X86_64
- CTL_HW
- HW_CPU_FREQ
- HW_MACHINE
- HW_MACHINE_ARCH
- HW_MODEL
- HW_NCPU
- P_FAULTED
- P_NOINTR
- P_OFFLINE
- P_ONLINE
- P_POWEROFF
- P_SPARE
- SC_NPROCESSORS_ONLN
- SI_ARCHITECTURE
- SI_MACHINE
- VERSION
The version of the sys-cpu gem.
Public Class Methods
Returns the cpu's architecture. On most systems this will be identical to the ::machine method. On OpenBSD it will be identical to the ::model method.
# File lib/sys/unix/sys/cpu.rb, line 93 def self.architecture if respond_to?(:sysinfo, true) buf = 0.chr * 257 if sysinfo(SI_ARCHITECTURE, buf, buf.size) < 0 raise Error, "sysinfo function failed" end buf.strip elsif respond_to?(:sysctlbyname, true) optr = FFI::MemoryPointer.new(:char, 256) size = FFI::MemoryPointer.new(:size_t) size.write_int(optr.size) if RbConfig::CONFIG['host_os'] =~ /darwin/i name = 'hw.machine' else name = 'hw.machine_arch' end if sysctlbyname(name, optr, size, nil, 0) < 0 raise Error, "sysctlbyname function failed" end optr.read_string else buf = 0.chr * 64 mib = FFI::MemoryPointer.new(:int, 2) size = FFI::MemoryPointer.new(:long, 1) mib.write_array_of_int([CTL_HW, HW_MACHINE_ARCH]) size.write_int(buf.size) if sysctl(mib, 2, buf, size, nil, 0) < 0 raise Error, "sysctl function failed" end buf.strip end end
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.
# File lib/sys/linux/sys/cpu.rb, line 96 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
Returns a string indicating the type of processor, e.g. GenuineIntel.
# File lib/sys/windows/sys/cpu.rb, line 264 def self.cpu_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
Returns the floating point processor type.
Not supported on all platforms.
# File lib/sys/unix/sys/cpu.rb, line 326 def self.fpu_type raise NoMethodError unless respond_to?(:processor_info, true) pinfo = ProcInfo.new if processor_info(0, pinfo) < 0 if processor_info(1, pinfo) < 0 raise Error, "process_info function failed" end end pinfo[:pi_fputypes].to_s end
Returns an integer indicating the speed of the CPU.
# File lib/sys/unix/sys/cpu.rb, line 258 def self.freq if respond_to?(:sysctlbyname, true) optr = FFI::MemoryPointer.new(:long) size = FFI::MemoryPointer.new(:size_t) size.write_long(optr.size) if RbConfig::CONFIG['host_os'] =~ /bsd/i name = 'hw.clockrate' else name = 'hw.cpufrequency' end if sysctlbyname(name, optr, size, nil, 0) < 0 raise Error, "sysctlbyname failed" end if RbConfig::CONFIG['host_os'] =~ /darwin/i optr.read_long / 1000000 else optr.read_long end elsif respond_to?(:sysctl, true) buf = 0.chr * 16 mib = FFI::MemoryPointer.new(:int, 2) size = FFI::MemoryPointer.new(:long, 1) mib.write_array_of_int([CTL_HW, HW_CPU_FREQ]) size.write_int(buf.size) if sysctl(mib, 2, buf, size, nil, 0) < 0 raise Error, "sysctl function failed" end buf.unpack("I*").first / 1000000 else pinfo = ProcInfo.new # Some systems start at 0, some at 1 if processor_info(0, pinfo) < 0 if processor_info(1, pinfo) < 0 raise Error, "process_info function failed" end end pinfo[:pi_clock].to_i end end
Returns a 3 element Array corresponding to the 1, 5 and 15 minute load average for the system.
# File lib/sys/linux/sys/cpu.rb, line 87 def self.load_avg load_avg_file = "/proc/loadavg" IO.readlines(load_avg_file).first.split[0..2].map{ |e| e.to_f } end
Returns the cpu's class type. On most systems this will be identical to the ::architecture method. On OpenBSD it will be identical to the ::model method.
# File lib/sys/unix/sys/cpu.rb, line 179 def self.machine if respond_to?(:sysctl, true) buf = 0.chr * 32 mib = FFI::MemoryPointer.new(:int, 2) size = FFI::MemoryPointer.new(:long, 1) mib.write_array_of_int([CTL_HW, HW_MACHINE]) size.write_int(buf.size) if sysctl(mib, 2, buf, size, nil, 0) < 0 raise Error, "sysctl function failed" end buf.strip else buf = 0.chr * 257 if sysinfo(SI_MACHINE, buf, buf.size) < 0 raise Error, "sysinfo function failed" end buf.strip end end
Returns a string indicating the cpu model.
# File lib/sys/unix/sys/cpu.rb, line 206 def self.model if RbConfig::CONFIG['host_os'] =~ /darwin/i ptr = FFI::MemoryPointer.new(:long) size = FFI::MemoryPointer.new(:size_t) size.write_long(ptr.size) if sysctlbyname("hw.cputype", ptr, size, nil, 0) < 0 raise "sysctlbyname function failed" end case ptr.read_long when CPU_TYPE_X86, CPU_TYPE_X86_64 "Intel" when CPU_TYPE_SPARC "Sparc" when CPU_TYPE_POWERPC, CPU_TYPE_POWERPC64 "PowerPC" else "Unknown" end else if respond_to?(:sysctl, true) buf = 0.chr * 64 mib = FFI::MemoryPointer.new(:int, 2) size = FFI::MemoryPointer.new(:long, 1) mib.write_array_of_int([CTL_HW, HW_MODEL]) size.write_int(buf.size) if sysctl(mib, 2, buf, size, nil, 0) < 0 raise Error, "sysctl function failed" end buf.strip else pinfo = ProcInfo.new # Some systems start at 0, some at 1 if processor_info(0, pinfo) < 0 if processor_info(1, pinfo) < 0 raise Error, "process_info function failed" end end pinfo[:pi_processor_type].to_s end end end
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.
# File lib/sys/unix/sys/cpu.rb, line 139 def self.num_cpu if respond_to?(:sysctlbyname, true) optr = FFI::MemoryPointer.new(:long) size = FFI::MemoryPointer.new(:size_t) size.write_long(optr.size) if sysctlbyname('hw.ncpu', optr, size, nil, 0) < 0 raise Error, "sysctlbyname failed" end optr.read_long elsif respond_to?(:sysconf, true) num = sysconf(SC_NPROCESSORS_ONLN) if num < 0 raise Error, "sysconf function failed" end num else buf = 0.chr * 4 mib = FFI::MemoryPointer.new(:int, 2) size = FFI::MemoryPointer.new(:long, 1) mib.write_array_of_int([CTL_HW, HW_NCPU]) size.write_int(buf.size) if sysctl(mib, 2, buf, size, nil, 0) < 0 raise Error, "sysctl function failed" end buf.strip.unpack("C").first end end
In block form, yields a CPUStruct for each CPU on the system. In non-block form, returns an Array of CPUStruct's.
The exact members of the struct vary on Linux systems.
# File lib/sys/linux/sys/cpu.rb, line 55 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
Returns the current state of processor num
, or 0 if no number
is specified.
Not supported on all platforms.
# File lib/sys/unix/sys/cpu.rb, line 345 def self.state(num = 0) raise NoMethodError unless respond_to?(:processor_info, true) pinfo = ProcInfo.new if processor_info(num, pinfo) < 0 raise Error, "process_info function failed" end case pinfo[:pi_state].to_i when P_ONLINE "online" when P_OFFLINE "offline" when P_POWEROFF "poweroff" when P_FAULTED "faulted" when P_NOINTR "nointr" when P_SPARE "spare" else "unknown" end end
Private Class Methods
convert an Availability number into a string
# File lib/sys/windows/sys/cpu.rb, line 422 def self.get_availability(num) case num when 1 return "Other" when 2 return "Unknown" when 3 return "Running" when 4 return "Warning" when 5 return "In Test" when 6 return "Not Applicable" when 7 return "Power Off" when 8 return "Off Line" when 9 return "Off Duty" when 10 return "Degraded" when 11 return "Not Installed" when 12 return "Install Error" when 13 return "Power Save - Unknown" when 14 return "Power Save - Low Power Mode" when 15 return "Power Save - Standby" when 16 return "Power Cycle" when 17 return "Power Save - Warning" when 18 return "Paused" when 19 return "Not Ready" when 20 return "Not Configured" when 21 return "Quiesced" else return nil end end
Convert the ConfigManagerErrorCode number to its corresponding string Note that this value returns nil on my system.
# File lib/sys/windows/sys/cpu.rb, line 280 def self.get_cmec(num) case when 0 str = "The device is working properly." return str when 1 str = "The device is not configured correctly." return str when 2 str = "Windows cannot load the driver for the device." return str when 3 str = "The driver for the device might be corrupted, or the" str << " system may be running low on memory or other" str << " resources." return str when 4 str = "The device is not working properly. One of the drivers" str << " or the registry might be corrupted." return str when 5 str = "The driver for this device needs a resource that" str << " Windows cannot manage." return str when 6 str = "The boot configuration for this device conflicts with" str << " other devices." return str when 7 str = "Cannot filter." return str when 8 str = "The driver loader for the device is missing." return str when 9 str = "This device is not working properly because the" str << " controlling firmware is reporting the resources" str << " for the device incorrectly." return str when 10 str = "This device cannot start." return str when 11 str = "This device failed." return str when 12 str = "This device cannot find enough free resources that" str << " it can use." return str when 13 str = "Windows cannot verify this device's resources." return str when 14 str = "This device cannot work properly until you restart" str << " your computer." return str when 15 str = "This device is not working properly because there is" str << " probably a re-enumeration problem." return str when 16 str = "Windows cannot identify all the resources this device " str << " uses." return str when 17 str = "This device is asking for an unknown resource type." return str when 18 str = "Reinstall the drivers for this device." return str when 19 str = "Failure using the VXD loader." return str when 20 str = "Your registry might be corrupted." return str when 21 str = "System failure: try changing the driver for this device." str << " If that does not work, see your hardware documentation." str << " Windows is removing this device." return str when 22 str = "This device is disabled." return str when 23 str = "System failure: try changing the driver for this device." str << "If that doesn't work, see your hardware documentation." return str when 24 str = "This device is not present, not working properly, or" str << " does not have all its drivers installed." return str when 25 str = "Windows is still setting up this device." return str when 26 str = "Windows is still setting up this device." return str when 27 str = "This device does not have valid log configuration." return str when 28 str = "The drivers for this device are not installed." return str when 29 str = "This device is disabled because the firmware of the" str << " device did not give it the required resources." return str when 30 str = "This device is using an Interrupt Request (IRQ)" str << " resource that another device is using." return str when 31 str = "This device is not working properly because Windows" str << " cannot load the drivers required for this device" return str else return nil end end
Convert an cpu architecture number to a string
# File lib/sys/windows/sys/cpu.rb, line 402 def self.get_cpu_arch(num) case num when 0 return "x86" when 1 return "MIPS" when 2 return "Alpha" when 3 return "PowerPC" when 6 return "IA64" when 9 return "x64" else return nil end end
Convert a family number into the equivalent string
# File lib/sys/windows/sys/cpu.rb, line 493 def self.get_family(num) case num when 1 return "Other" when 2 return "Unknown" when 3 return "8086" when 4 return "80286" when 5 return "80386" when 6 return "80486" when 7 return "8087" when 8 return "80287" when 9 return "80387" when 10 return "80487" when 11 return "Pentium?" when 12 return "Pentium?" when 13 return "Pentium?" when 14 return "Pentium?" when 15 return "Celeron?" when 16 return "Pentium?" when 17 return "Pentium?" when 18 return "M1" when 19 return "M2" when 24 return "K5" when 25 return "K6" when 26 return "K6-2" when 27 return "K6-3" when 28 return "AMD" when 29 return "AMD?" when 30 return "AMD2900" when 31 return "K6-2+" when 32 return "Power" when 33 return "Power" when 34 return "Power" when 35 return "Power" when 36 return "Power" when 37 return "Power" when 38 return "Power" when 39 return "Power" when 48 return "Alpha" when 49 return "Alpha" when 50 return "Alpha" when 51 return "Alpha" when 52 return "Alpha" when 53 return "Alpha" when 54 return "Alpha" when 55 return "Alpha" when 64 return "MIPS" when 65 return "MIPS" when 66 return "MIPS" when 67 return "MIPS" when 68 return "MIPS" when 69 return "MIPS" when 80 return "SPARC" when 81 return "SuperSPARC" when 82 return "microSPARC" when 83 return "microSPARC" when 84 return "UltraSPARC" when 85 return "UltraSPARC" when 86 return "UltraSPARC" when 87 return "UltraSPARC" when 88 return "UltraSPARC" when 96 return "68040" when 97 return "68xxx" when 98 return "68000" when 99 return "68010" when 100 return "68020" when 101 return "68030" when 112 return "Hobbit" when 120 return "Crusoe?" when 121 return "Crusoe?" when 128 return "Weitek" when 130 return "Itanium?" when 144 return "PA-RISC" when 145 return "PA-RISC" when 146 return "PA-RISC" when 147 return "PA-RISC" when 148 return "PA-RISC" when 149 return "PA-RISC" when 150 return "PA-RISC" when 160 return "V30" when 176 return "Pentium?" when 177 return "Pentium?" when 178 return "Pentium?" when 179 return "Intel?" when 180 return "AS400" when 181 return "Intel?" when 182 return "AMD" when 183 return "AMD" when 184 return "Intel?" when 185 return "AMD" when 190 return "K7" when 200 return "IBM390" when 201 return "G4" when 202 return "G5" when 250 return "i860" when 251 return "i960" when 260 return "SH-3" when 261 return "SH-4" when 280 return "ARM" when 281 return "StrongARM" when 300 return "6x86" when 301 return "MediaGX" when 302 return "MII" when 320 return "WinChip" when 350 return "DSP" when 500 return "Video" else return nil end end
Convert power management capabilities number to its equivalent string
# File lib/sys/windows/sys/cpu.rb, line 707 def self.get_pmc(num) case num when 0 return "Unknown" when 1 return "Not Supported" when 2 return "Disabled" when 3 return "Enabled" when 4 return "Power Saving Modes Entered Automatically" when 5 return "Power State Settable" when 6 return "Power Cycling Supported" when 7 return "Timed Power On Supported" else return nil end end
Convert a processor type into its equivalent string
# File lib/sys/windows/sys/cpu.rb, line 731 def self.get_processor_type(num) case num when 1 return "Other" when 2 return "Unknown" when 3 return "Central Processor" when 4 return "Math Processor" when 5 return "DSP Processor" when 6 return "Video Processor" else return nil end end
convert CpuStatus to a string form. Note that values 5 and 6 are skipped because they're reserved.
# File lib/sys/windows/sys/cpu.rb, line 473 def self.get_status(num) case num when 0 return "Unknown" when 1 return "Enabled" when 2 return "Disabled by User via BIOS Setup" when 3 return "Disabled By BIOS (POST Error)" when 4 return "Idle" when 7 return "Other" else return nil end end
Convert an upgrade method into its equivalent string
# File lib/sys/windows/sys/cpu.rb, line 751 def self.get_upgrade_method(num) case num when 1 return "Other" when 2 return "Unknown" when 3 return "Daughter Board" when 4 return "ZIF Socket" when 5 return "Replacement/Piggy Back" when 6 return "None" when 7 return "LIF Socket" when 8 return "Slot 1" when 9 return "Slot 2" when 10 return "370 Pin Socket" when 11 return "Slot A" when 12 return "Slot M" else return nil end end
Convert return values to voltage cap values (floats)
# File lib/sys/windows/sys/cpu.rb, line 783 def self.get_voltage_caps(num) case num when 1 return 5.0 when 2 return 3.3 when 4 return 2.9 else return nil end end
Create singleton methods for each of the attributes.
# File lib/sys/linux/sys/cpu.rb, line 73 def self.method_missing(id, arg=0) rv = CPU_ARRAY[arg][id.to_s] if rv.nil? id = id.to_s + "?" rv = CPU_ARRAY[arg][id] end rv end