Parent

Namespace

Class/Module Index [+]

Quicksearch

Sys::CPU

Encapsulates system CPU information

Public Class Methods

architecture(host=Socket.gethostname) click to toggle source

Returns the host CPU's architecture, or nil if it cannot be determined.

# 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() click to toggle source

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/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
cpu_type(host = Socket.gethostname) click to toggle source

Returns a string indicating the type of processor, e.g. GenuineIntel.

# File lib/windows/sys/cpu.rb, line 258
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
fpu_type() click to toggle source
# File lib/unix/sys/cpu.rb, line 306
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(cpu_num = 0, host = Socket.gethostname) click to toggle source

Returns an integer indicating the speed (i.e. frequency in Mhz) of cpu_num on host, or the localhost if no host is specified. If cpu_num +1 is greater than the number of cpu's on your system or this call fails for any other reason, a Error is raised.

# 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_avg() click to toggle source

Returns a 3 element Array corresponding to the 1, 5 and 15 minute load average for the system.

# 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() click to toggle source
# File lib/unix/sys/cpu.rb, line 170
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
model(host = Socket.gethostname) click to toggle source

Returns a string indicating the cpu model, e.g. Intel Pentium 4.

# 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(host = Socket.gethostname) click to toggle source

Returns an integer indicating the number of cpu's on the system.

# 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() 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/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
state(num = 0) click to toggle source
# File lib/unix/sys/cpu.rb, line 320
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

[Validate]

Generated with the Darkfish Rdoc Generator 2.