Class: Sys::ProcessInfo

Inherits:
Struct show all
Defined in:
lib/epitools/sys/ps.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 collapse

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

#initialize(*args) ⇒ ProcessInfo

Returns a new instance of ProcessInfo.



127
128
129
130
131
# File 'lib/epitools/sys/ps.rb', line 127

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

Instance Method Details

#childrenObject



137
138
139
140
# File 'lib/epitools/sys/ps.rb', line 137

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

#dead?Boolean

Has this process been killed?

Returns:

  • (Boolean)


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

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

#exenameObject



186
187
188
189
# File 'lib/epitools/sys/ps.rb', line 186

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

#fdsObject



191
192
193
# File 'lib/epitools/sys/ps.rb', line 191

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

#kill!(signal = "TERM") ⇒ Object

Send the TERM signal to this process.



152
153
154
155
156
# File 'lib/epitools/sys/ps.rb', line 152

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

#parentObject



133
134
135
# File 'lib/epitools/sys/ps.rb', line 133

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

#refreshObject

Refresh this process’ statistics.



168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/epitools/sys/ps.rb', line 168

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

#to_hashObject

Convert all the process information to a hash.



145
146
147
# File 'lib/epitools/sys/ps.rb', line 145

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