class PosixPsutil::CPU

Public Class Methods

calculate_cpu_percent_field(start, last) click to toggle source
# File linux/system.rb, line 164
def self.calculate_cpu_percent_field(start, last)
  start_sum = 0
  start.marshal_dump.each_value {|value| start_sum += value}
  last_sum = 0
  last.marshal_dump.each_value {|value| last_sum += value}

  ret = OpenStruct.new
  [:user, :nice, :system, :idle, :iowait, :irq, 
            :softirq, :steal, :guest, :guest_nice].each do |field|
    start_field = start[field]
    last_field = last[field]
    # be aware of float precision issue
    last_field = start_field if last_field < start_field
    field_delta = last_field - start_field
    all_delta = last_sum - start_sum
    # if the interval is too small
    if all_delta == 0
      percent = 0
    else
      percent = field_delta * 100 / all_delta
    end
    ret[field] = percent.round(2)
  end

  ret
end
cpu_count(logical=true) click to toggle source

Return the number of physical/logical CPUs in the system.

# File linux/system.rb, line 82
def self.cpu_count(logical=true)
  count = 0
  if !logical
    unless @physical_cpu_count
      IO.readlines('/proc/cpuinfo').each do |line|
        count += 1 if line.start_with?('physical id')
      end
      @physical_cpu_count = count
    end
    return @physical_cpu_count
  end

  unless @logical_cpu_count
    Dir.entries('/sys/devices/system/cpu').each do |entry|
      count += 1 if entry.start_with?('cpu')
    end
    count -= 2 # except cpuidle and cpufreq
    @logical_cpu_count = count
  end
  @logical_cpu_count
end
cpu_percent(interval=0.0, percpu=false) click to toggle source

measure cpu usage percent during an interval WARNING: set a small interval will cause incorrect result

# File linux/system.rb, line 29
def self.cpu_percent(interval=0.0, percpu=false)
  if interval > 0.0
    total_start = self.cpu_times(percpu)
    sleep interval
  else
    if percpu
      total_start = @last_per_cpu_times
    else
      total_start = @last_cpu_times
    end
  end

  if percpu
    @last_per_cpu_times = self.cpu_times(true)
    ret = []
    total_start.each_index do |i|
      ret.push(calculate_cpu_percent(total_start[i], @last_per_cpu_times[i]))
    end
    ret
  else
    @last_cpu_times = self.cpu_times()
    calculate_cpu_percent(total_start, @last_cpu_times)
  end
end
cpu_times(precpu=false) click to toggle source

Return OpenStruct representing the CPU times for all CPU available in the system. If precpu is true, return an Array of that OpenStructs, one per CPU.

For the format of OpenStruct, see `get_cpu_fields`.

# File linux/system.rb, line 14
def self.cpu_times(precpu=false)
  proc_stat = File.new('/proc/stat')
  cpu = proc_stat.readline()
  return get_cpu_fields(cpu) unless precpu
  cpus = []
  loop do
    cpu = proc_stat.readline()
    break unless cpu.start_with?('cpu')
    cpus.push(get_cpu_fields(cpu))
  end
  cpus
end
cpu_times_percent(interval=0.0, percpu=false) click to toggle source
# File linux/system.rb, line 54
def self.cpu_times_percent(interval=0.0, percpu=false)
  if interval > 0.0
    total_start = self.cpu_times(percpu)
    sleep interval
  else
    if percpu
      total_start = @last_per_cpu_times_fields
    else
      total_start = @last_cpu_times_fields
    end
  end

  if percpu
    @last_per_cpu_times_fields = self.cpu_times(true)
    ret = []
    total_start.each_index do |i|
      ret.push(calculate_cpu_percent_field(total_start[i], @last_per_cpu_times_fields[i]))
    end
    ret
  else
    @last_cpu_times = self.cpu_times()
    calculate_cpu_percent_field(total_start, @last_cpu_times_fields)
  end
end