class ProcParser::CPUStat

Attributes

guest[RW]

This class read CPU usage information from the /proc/stat file. The behavior for Kernel version prior to 2.5.41 is undefined.

guest_nice[RW]

This class read CPU usage information from the /proc/stat file. The behavior for Kernel version prior to 2.5.41 is undefined.

idle[RW]

This class read CPU usage information from the /proc/stat file. The behavior for Kernel version prior to 2.5.41 is undefined.

iowait[RW]

This class read CPU usage information from the /proc/stat file. The behavior for Kernel version prior to 2.5.41 is undefined.

irq[RW]

This class read CPU usage information from the /proc/stat file. The behavior for Kernel version prior to 2.5.41 is undefined.

nb_cpu[RW]
nice[RW]

This class read CPU usage information from the /proc/stat file. The behavior for Kernel version prior to 2.5.41 is undefined.

softirq[RW]

This class read CPU usage information from the /proc/stat file. The behavior for Kernel version prior to 2.5.41 is undefined.

steal[RW]

This class read CPU usage information from the /proc/stat file. The behavior for Kernel version prior to 2.5.41 is undefined.

system[RW]

This class read CPU usage information from the /proc/stat file. The behavior for Kernel version prior to 2.5.41 is undefined.

user[RW]

This class read CPU usage information from the /proc/stat file. The behavior for Kernel version prior to 2.5.41 is undefined.

Public Class Methods

new(stat_file = '/proc/stat') click to toggle source
# File lib/proc_parser/cpu_stat.rb, line 24
def initialize(stat_file = '/proc/stat')
  raise NoProcData, "This system doesn't have /proc/stat data." if !File.exist?(stat_file)

  File.open(stat_file, 'r') do |file|
    firstline = file.readline.strip.squeeze(' ').split(' ')
    raise NoProcData, 'Unsupported format of /proc/stat' if firstline[0] != 'cpu'

    @@attributes.each do |attribute, index|
      instance_variable_set("@#{attribute}", firstline[index].to_i)
    end

    @nb_cpu = 0
    file.each do |line|
      splitted_line = line.strip.squeeze(' ').split(' ')
      break if !splitted_line[0].start_with? 'cpu'
      @nb_cpu += 1
    end
  end
end

Public Instance Methods

idletime() click to toggle source

The next values (idletime, non_idletime and totaltime) are computed based on the following information:

Computing the percentage usage is done with the following algorithm: totald = Total - PrevTotal idled = Idle - PrevIdle CPU_Percentage = (totald - idled)/totald

With the Prev* variables fetched earlier than the current values.

# File lib/proc_parser/cpu_stat.rb, line 58
def idletime
  return @idle + @iowait
end
non_idletime() click to toggle source
# File lib/proc_parser/cpu_stat.rb, line 62
def non_idletime
  # Guest time is already accounted in user time
  user = @user - @guest
  nice = @nice - @guest_nice

  systemalltime = @system + @irq + @softirq
  virtalltime = @guest + @guest_nice
  return user + nice + systemalltime + @steal + virtalltime
end
totaltime() click to toggle source
# File lib/proc_parser/cpu_stat.rb, line 72
def totaltime
  return idletime + non_idletime
end