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

architecture() click to toggle source

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.

# File lib/sys/darwin/sys/cpu.rb, line 66
def self.architecture
  optr = FFI::MemoryPointer.new(:char, 256)
  size = FFI::MemoryPointer.new(:size_t)

  size.write_int(optr.size)

  if sysctlbyname('hw.machine', optr, size, nil, 0) < 0
    raise Error, 'sysctlbyname function failed'
  end

  optr.read_string
end
cpu_stats() click to toggle source

Returns a hash of arrays that contains an array of the following information (as of 2.6.33), respectively:

  • user: time spent in user mode.

  • nice: time spent in user mode with low priority.

  • system: time spent in system mode.

  • idle: time spent in the idle task.

  • iowait: time waiting for IO to complete.

  • irq: time servicing interrupts.

  • softirq: time servicing softirqs.

  • steal: time spent in other operating systems when running in a virtualized environment.

  • guest: time spent running a virtual CPU for guest operating systems.

  • guest_nice: time spent running a niced guest, i.e a virtual CPU for guest operating systems.

Note that older kernels may not necessarily include some of these fields.

# File lib/sys/linux/sys/cpu.rb, line 142
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.to_i / 100 } # 100 jiffies/sec.
    hash[array[0]] = vals
  }

  hash
end
cpu_type(host = Socket.gethostname) click to toggle source

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
    wmi.Manufacturer
  end
end
fpu_type() click to toggle source

Returns the floating point processor type.

Not supported on all platforms.

# File lib/sys/unix/sys/cpu.rb, line 294
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
freq() click to toggle source

Returns an integer indicating the speed of the CPU.

# File lib/sys/darwin/sys/cpu.rb, line 141
def self.freq
  optr = FFI::MemoryPointer.new(:long)
  size = FFI::MemoryPointer.new(:size_t)

  size.write_long(optr.size)

  if sysctlbyname('hw.cpufrequency', optr, size, nil, 0) < 0
    raise Error, 'sysctlbyname failed'
  end

  optr.read_long / 1000000
end
load_avg() click to toggle source

Returns an array of three floats indicating the 1, 5 and 15 minute load average.

# File lib/sys/darwin/sys/cpu.rb, line 157
def self.load_avg
  loadavg = FFI::MemoryPointer.new(:double, 3)

  if getloadavg(loadavg, loadavg.size) < 0
    raise Error, 'getloadavg function failed'
  end

  loadavg.get_array_of_double(0, 3)
end
machine() click to toggle source

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.

# File lib/sys/darwin/sys/cpu.rb, line 100
def self.machine
  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
end
model() click to toggle source

Returns a string indicating the cpu model.

# File lib/sys/darwin/sys/cpu.rb, line 117
def self.model
  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
end
num_cpu() click to toggle source

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/darwin/sys/cpu.rb, line 83
def self.num_cpu
  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
end
processors() { |struct| ... } click to toggle source

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 59
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
state(num = 0) click to toggle source

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 313
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

get_availability(num) click to toggle source

convert an Availability number into a string

# File lib/sys/windows/sys/cpu.rb, line 426
def self.get_availability(num)
  case num
    when 1
      'Other'
    when 2
      'Unknown'
    when 3
      'Running'
    when 4
      'Warning'
    when 5
      'In Test'
    when 6
      'Not Applicable'
    when 7
      'Power Off'
    when 8
      'Off Line'
    when 9
      'Off Duty'
    when 10
      'Degraded'
    when 11
      'Not Installed'
    when 12
      'Install Error'
    when 13
      'Power Save - Unknown'
    when 14
      'Power Save - Low Power Mode'
    when 15
      'Power Save - Standby'
    when 16
      'Power Cycle'
    when 17
      'Power Save - Warning'
    when 18
      'Paused'
    when 19
      'Not Ready'
    when 20
      'Not Configured'
    when 21
      'Quiesced'
    else
      nil
  end
end
get_cmec(num) click to toggle source

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 num
    when 0
      str = 'The device is working properly.'
      str
    when 1
      str = 'The device is not configured correctly.'
      str
    when 2
      str = 'Windows cannot load the driver for the device.'
      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.'
      str
    when 4
      str = 'The device is not working properly. One of the drivers'
      str << ' or the registry might be corrupted.'
      str
    when 5
      str = 'The driver for this device needs a resource that'
      str << ' Windows cannot manage.'
      str
    when 6
      str = 'The boot configuration for this device conflicts with'
      str << ' other devices.'
      str
    when 7
      str = 'Cannot filter.'
      str
    when 8
      str = 'The driver loader for the device is missing.'
      str
    when 9
      str = 'This device is not working properly because the'
      str << ' controlling firmware is reporting the resources'
      str << ' for the device incorrectly.'
      str
    when 10
      str = 'This device cannot start.'
      str
    when 11
      str = 'This device failed.'
      str
    when 12
      str = 'This device cannot find enough free resources that'
      str << ' it can use.'
      str
    when 13
      str = "Windows cannot verify this device's resources."
      str
    when 14
      str = 'This device cannot work properly until you restart'
      str << ' your computer.'
      str
    when 15
      str = 'This device is not working properly because there is'
      str << ' probably a re-enumeration problem.'
      str
    when 16
       str = 'Windows cannot identify all the resources this device '
       str << ' uses.'
       str
    when 17
      str = 'This device is asking for an unknown resource type.'
      str
    when 18
      str = 'Reinstall the drivers for this device.'
      str
    when 19
      str = 'Failure using the VXD loader.'
      str
    when 20
      str = 'Your registry might be corrupted.'
      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.'
      str
    when 22
      str = 'This device is disabled.'
      str
    when 23
      str = 'System failure: try changing the driver for this device.'
      str << "If that doesn't work, see your hardware documentation."
      str
    when 24
      str = 'This device is not present, not working properly, or'
      str << ' does not have all its drivers installed.'
      str
    when 25
      str = 'Windows is still setting up this device.'
      str
    when 26
      str = 'Windows is still setting up this device.'
      str
    when 27
      str = 'This device does not have valid log configuration.'
      str
    when 28
      str = 'The drivers for this device are not installed.'
      str
    when 29
      str = 'This device is disabled because the firmware of the'
      str << ' device did not give it the required resources.'
      str
    when 30
      str = 'This device is using an Interrupt Request (IRQ)'
      str << ' resource that another device is using.'
      str
    when 31
      str = 'This device is not working properly because Windows'
      str << ' cannot load the drivers required for this device'
      str
    else
      nil
  end
end
get_cpu_arch(num) click to toggle source

Convert an cpu architecture number to a string

# File lib/sys/windows/sys/cpu.rb, line 404
def self.get_cpu_arch(num)
  case num
    when 0
      'x86'
    when 1
      'MIPS'
    when 2
      'Alpha'
    when 3
      'PowerPC'
    when 6
      'IA64'
    when 9
      'x64'
    else
      nil
  end
end
get_family(num) click to toggle source

Convert a family number into the equivalent string

# File lib/sys/windows/sys/cpu.rb, line 501
def self.get_family(num)
  case num
    when 1
      'Other'
    when 2
      'Unknown'
    when 3
      '8086'
    when 4
      '80286'
    when 5
      '80386'
    when 6
      '80486'
    when 7
      '8087'
    when 8
      '80287'
    when 9
      '80387'
    when 10
      '80487'
    when 11
      'Pentium?'
    when 12
      'Pentium?'
    when 13
      'Pentium?'
    when 14
      'Pentium?'
    when 15
      'Celeron?'
    when 16
      'Pentium?'
    when 17
      'Pentium?'
    when 18
      'M1'
    when 19
      'M2'
    when 24
      'K5'
    when 25
      'K6'
    when 26
      'K6-2'
    when 27
      'K6-3'
    when 28
      'AMD'
    when 29
      'AMD?'
    when 30
      'AMD2900'
    when 31
      'K6-2+'
    when 32
      'Power'
    when 33
      'Power'
    when 34
      'Power'
    when 35
      'Power'
    when 36
      'Power'
    when 37
      'Power'
    when 38
      'Power'
    when 39
      'Power'
    when 48
      'Alpha'
    when 49
      'Alpha'
    when 50
      'Alpha'
    when 51
      'Alpha'
    when 52
      'Alpha'
    when 53
      'Alpha'
    when 54
      'Alpha'
    when 55
      'Alpha'
    when 64
      'MIPS'
    when 65
      'MIPS'
    when 66
      'MIPS'
    when 67
      'MIPS'
    when 68
      'MIPS'
    when 69
      'MIPS'
    when 80
      'SPARC'
    when 81
      'SuperSPARC'
    when 82
      'microSPARC'
    when 83
      'microSPARC'
    when 84
      'UltraSPARC'
    when 85
      'UltraSPARC'
    when 86
      'UltraSPARC'
    when 87
      'UltraSPARC'
    when 88
      'UltraSPARC'
    when 96
      '68040'
    when 97
      '68xxx'
    when 98
      '68000'
    when 99
      '68010'
    when 100
      '68020'
    when 101
      '68030'
    when 112
      'Hobbit'
    when 120
      'Crusoe?'
    when 121
      'Crusoe?'
    when 128
      'Weitek'
    when 130
      'Itanium?'
    when 144
      'PA-RISC'
    when 145
      'PA-RISC'
    when 146
      'PA-RISC'
    when 147
      'PA-RISC'
    when 148
      'PA-RISC'
    when 149
      'PA-RISC'
    when 150
      'PA-RISC'
    when 160
      'V30'
    when 176
      'Pentium?'
    when 177
      'Pentium?'
    when 178
      'Pentium?'
    when 179
      'Intel?'
    when 180
      'AS400'
    when 181
      'Intel?'
    when 182
      'AMD'
    when 183
      'AMD'
    when 184
      'Intel?'
    when 185
      'AMD'
    when 190
      'K7'
    when 200
      'IBM390'
    when 201
      'G4'
    when 202
      'G5'
    when 250
      'i860'
    when 251
      'i960'
    when 260
      'SH-3'
    when 261
      'SH-4'
    when 280
      'ARM'
    when 281
      'StrongARM'
    when 300
      '6x86'
    when 301
      'MediaGX'
    when 302
      'MII'
    when 320
      'WinChip'
    when 350
      'DSP'
    when 500
      'Video'
    else
      nil
  end
end
get_pmc(num) click to toggle source

Convert power management capabilities number to its equivalent string

# File lib/sys/windows/sys/cpu.rb, line 717
def self.get_pmc(num)
  case num
    when 0
      'Unknown'
    when 1
      'Not Supported'
    when 2
      'Disabled'
    when 3
      'Enabled'
    when 4
      'Power Saving Modes Entered Automatically'
    when 5
      'Power State Settable'
    when 6
      'Power Cycling Supported'
    when 7
      'Timed Power On Supported'
    else
      nil
  end
end
get_processor_type(num) click to toggle source

Convert a processor type into its equivalent string

# File lib/sys/windows/sys/cpu.rb, line 743
def self.get_processor_type(num)
  case num
    when 1
      'Other'
    when 2
      'Unknown'
    when 3
      'Central Processor'
    when 4
      'Math Processor'
    when 5
      'DSP Processor'
    when 6
      'Video Processor'
    else
      nil
  end
end
get_status(num) click to toggle source

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 479
def self.get_status(num)
  case num
    when 0
      'Unknown'
    when 1
      'Enabled'
    when 2
      'Disabled by User via BIOS Setup'
    when 3
      'Disabled By BIOS (POST Error)'
    when 4
      'Idle'
    when 7
      'Other'
    else
      nil
  end
end
get_upgrade_method(num) click to toggle source

Convert an upgrade method into its equivalent string

# File lib/sys/windows/sys/cpu.rb, line 765
def self.get_upgrade_method(num)
  case num
    when 1
      'Other'
    when 2
      'Unknown'
    when 3
      'Daughter Board'
    when 4
      'ZIF Socket'
    when 5
      'Replacement/Piggy Back'
    when 6
      'None'
    when 7
      'LIF Socket'
    when 8
      'Slot 1'
    when 9
      'Slot 2'
    when 10
      '370 Pin Socket'
    when 11
      'Slot A'
    when 12
      'Slot M'
    else
      nil
  end
end
get_voltage_caps(num) click to toggle source

Convert return values to voltage cap values (floats)

# File lib/sys/windows/sys/cpu.rb, line 799
def self.get_voltage_caps(num)
  case num
    when 1
      5.0
    when 2
      3.3
    when 4
      2.9
    else
      nil
  end
end
method_missing(id, arg=0) click to toggle source

Create singleton methods for each of the attributes.

# File lib/sys/linux/sys/cpu.rb, line 106
def self.method_missing(id, arg=0)
  raise NoMethodError, "'#{id}'" unless CPU_ARRAY[arg].has_key?(id.to_s)
  rv = CPU_ARRAY[arg][id.to_s]
  if rv.nil?
    id = id.to_s + '?'
    rv = CPU_ARRAY[arg][id]
  end
  rv
end