Class: Sys::ProcessInfo

Inherits:
Struct show all
Defined in:
lib/epitools/sys.rb

Overview

Contains all the information that PS can report about a process for the current platform.

The following attribute accessor methods are available:

pid     (integer)
command (string -- the 'ps' name)
name    (alias for 'command')
pcpu    (float)
pmem    (float)
stat    (string)
rss     (integer)
vsz     (integer)
user    (string)
majflt  (integer)
minflt  (integer)
state   (array of symbols; see DARWIN_STATES or LINUX_STATES)

Only on linux:

exename (string -- path to the binary)
fds     (array -- list of open file descriptors)

Constant Summary

DARWIN_STATES =
{
  "R"=>:running,
  "S"=>:sleeping,
  "I"=>:idle,
  "T"=>:stopped,
  "U"=>:wait,
  "Z"=>:zombie,
  "W"=>:swapped,
  
  "s"=>:session_leader,
  "X"=>:debugging,
  "E"=>:exiting,
  "<"=>:high_priority,
  "N"=>:low_priority,
  "+"=>:foreground,
  "L"=>:locked_pages,
}
LINUX_STATES =
{
  "R"=>:running,
  "S"=>:sleeping,
  "T"=>:stopped,
  "D"=>:wait,
  "Z"=>:zombie,
  "W"=>:swapped,
  "X"=>:dead,

  "s"=>:session_leader,
  "<"=>:high_priority,
  "N"=>:low_priority,
  "+"=>:foreground,
  "L"=>:locked_pages,
  "l"=>:multithreaded,
}

Instance Method Summary (collapse)

Methods inherited from Struct

#to_json

Constructor Details

- (ProcessInfo) initialize(*args)

Returns a new instance of ProcessInfo



154
155
156
157
158
# File 'lib/epitools/sys.rb', line 154

def initialize(*args)
  @dead = false
  args << stat_to_state(args[PS_FIELDS.index(:stat)])
  super(*args)
end

Instance Method Details

- (Object) children



164
165
166
167
# File 'lib/epitools/sys.rb', line 164

def children
  @@parents ||= Sys.ps.group_by(&:ppid)
  @@parents[pid]
end

- (Boolean) dead?

Has this process been killed?

Returns:

  • (Boolean)


188
189
190
# File 'lib/epitools/sys.rb', line 188

def dead?
  @dead ||= Sys.pid(pid).empty?
end

- (Object) exename



213
214
215
216
# File 'lib/epitools/sys.rb', line 213

def exename
  @exename ||= File.readlink("/proc/#{pid}/exe") rescue :unknown
  @exename == :unknown ? nil : @exename
end

- (Object) fds



218
219
220
# File 'lib/epitools/sys.rb', line 218

def fds
  Dir["/proc/#{pid}/fd/*"].map { |fd| File.readlink(fd) rescue nil }
end

- (Object) kill!(signal = "TERM")

Send the TERM signal to this process.



179
180
181
182
183
# File 'lib/epitools/sys.rb', line 179

def kill!(signal="TERM")
  puts "Killing #{pid} (#{signal})"
  Process.kill(signal, pid)
  # TODO: handle exception Errno::ESRCH (no such process)
end

- (Object) parent



160
161
162
# File 'lib/epitools/sys.rb', line 160

def parent
  Sys.ps(ppid).first unless ppid < 1
end

- (Object) refresh

Refresh this process' statistics.



195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/epitools/sys.rb', line 195

def refresh
  processes = Sys.ps(pid)
  
  if processes.empty?
    @dead = true
    raise ProcessNotFound
  end
  
  updated_process = processes.first
  members.each { |member| self[member] = updated_process[member] }
  self
end

- (Object) to_hash

Convert all the process information to a hash.



172
173
174
# File 'lib/epitools/sys.rb', line 172

def to_hash
  Hash[ *members.zip(values).flatten(1) ]
end