Class: SysLite::ProcTable

Inherits:
Object
  • Object
show all
Defined in:
lib/server_metrics/lib/proctable_lite.rb

Overview

The ProcTable class encapsulates process table information.

Defined Under Namespace

Classes: Error, ProcTableStruct

Constant Summary collapse

VERSION =

The version of the sys-proctable library

'0.9.3'

Class Method Summary collapse

Class Method Details

.fieldsObject

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}"
}


149
150
151
# File 'lib/server_metrics/lib/proctable_lite.rb', line 149

def self.fields
  @fields
end

.ps(pid = nil) ⇒ Object

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)

–

It's possible that a process could terminate while gathering
information for that process. When that happens, this library
will simply skip to the next record. In short, this library will
either return all information for a process, or none at all.

Raises:

  • (TypeError)


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# 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