class SysLite::ProcTable

The ProcTable class encapsulates process table information.

Constants

ProcTableStruct
VERSION

The version of the sys-proctable library

Public Class Methods

fields() click to toggle source

Returns an array of fields that each ProcTableStruct will contain. This may be useful if you want to know in advance what fields are available without having to perform at least one read of the /proc table.

Example:

Sys::ProcTable.fields.each{ |field|
   puts "Field: #{field}"
}
# File lib/server_metrics/lib/proctable_lite.rb, line 149
def self.fields
  @fields
end
ps(pid=nil) { |struct| ... } click to toggle source

In block form, yields a ProcTableStruct for each process entry that you have rights to. This method returns an array of ProcTableStruct's in non-block form.

If a pid is provided, then only a single ProcTableStruct is yielded or returned, or nil if no process information is found for that pid.

Example:

# Iterate over all processes
ProcTable.ps do |proc_info|
   p proc_info
end

# Print process table information for only pid 1001
p ProcTable.ps(1001)
# File lib/server_metrics/lib/proctable_lite.rb, line 79
def self.ps(pid=nil)
  array  = block_given? ? nil : []
  struct = nil
  raise TypeError unless pid.is_a?(Fixnum) if pid

  proc_dir = ServerMetrics::SystemInfo.proc_dir
  
  Dir.chdir(proc_dir)
  Dir.glob("[0-9]*").each do |file|
    next unless file.to_i == pid if pid

    struct = ProcTableStruct.new

    # Get /proc/<pid>/stat information
    stat = IO.read("#{proc_dir}/#{file}/stat") rescue next

    # Deal with spaces in comm name. Courtesy of Ara Howard.
    re = %r/\(.*\)/
    comm = stat[re]
    comm.tr!(' ', '-')
    stat[re] = comm

    stat = stat.split

    struct.pid         = stat[0].to_i
    # Remove parens. Note this could be overwritten in #get_comm_group_name.
    struct.comm        = stat[1].tr('()','')
    struct.ppid        = stat[3].to_i
    struct.utime       = stat[13].to_i
    struct.stime       = stat[14].to_i
    struct.rss         = stat[23].to_i
    
    # don't report kthreadd chidren individually - aggregate into the parent.
    if kthreadd_child?(struct.ppid)
      @kthreadd.utime += struct.utime
      @kthreadd.stime += struct.stime
      @kthreadd.rss += struct.rss
      next
    elsif !@kthreadd and %w(kthread kthreadd).include?(struct.comm)
      @kthreadd = struct
      next
    end
    
    struct.freeze # This is read-only data

    if block_given?
      yield struct
    else
      array << struct
    end
  end # Dir.glob

  if pid
    struct
  else 
    array << @kthreadd if @kthreadd # not added when iterating.
    array
  end
end

Private Class Methods

get_pctcpu(utime, start_time) click to toggle source

Calculate the percentage of CPU usage for the given process.

# File lib/server_metrics/lib/proctable_lite.rb, line 171
def self.get_pctcpu(utime, start_time)
  return nil unless @boot_time
  hertz = 100.0
  utime = (utime * 10000).to_f
  stime = (start_time.to_f / hertz) + @boot_time
  sprintf("%3.2f", (utime / 10000.0) / (Time.now.to_i - stime)).to_f
end
get_pctmem(rss) click to toggle source

Calculate the percentage of memory usage for the given process.

# File lib/server_metrics/lib/proctable_lite.rb, line 162
def self.get_pctmem(rss)
  return nil unless @mem_total
  page_size = 4096
  rss_total = rss * page_size
  sprintf("%3.2f", (rss_total.to_f / @mem_total) * 100).to_f
end
kthreadd_child?(ppid) click to toggle source

True if the process's parent process id is kthreadd.

# File lib/server_metrics/lib/proctable_lite.rb, line 156
def self.kthreadd_child?(ppid)
  @kthreadd and @kthreadd.pid == ppid
end