Module: Process

Defined in:
lib/webget_ruby_ramp/process.rb

Overview

Process extensions to help debug Ruby programs.

Examples

p = Process.ps
puts p
=> the results of the 'ps' command for the current process id

p = Process.ps(1234)
puts p
=> the results of the 'ps' command for process id 1234

p = Process.pss
p['%cpu'] => percentage of cpu use, as a float
p['%mem'] => percentage of memory use, as a float

Constant Summary collapse

PS_ALIASES_DEFAULT =

Get the list of process alias keywords as typically defined by the shell.

For example, a shell may consider “%cpu” and “pcpu” to be identical.

{
  '%cpu'=>'pcpu',
  '%mem'=>'pmem',
  'sig_block'=>'blocked',
  'sigmask'=>'blocked',
  'sig_catch'=>'caught',
  'sigcatch'=>'caught',
  'cls'=>'class',
  'cls'=>'policy',
  'cputime'=>'time',
  'gid'=>'egid',
  'group'=>'egroup',
  'uid'=>'euid',
  'uname'=>'euser',
  'user'=>'euser',
  'flag'=>'f',
  'flags'=>'f',
  'fsuid'=>'fuid',
  'sig_ignore'=>'ignored',
  'sigignore'=>'ignored',
  'spid'=>'lwp',
  'tid'=>'lwp',
  'nice'=>'ni',
  'thcount'=>'nlwp',
  'sig'=>'pending',
  'sig_pend'=>'pending',
  'pgrp'=>'pgid',
  'rssize'=>'rss',
  'rsz'=>'rss',
  'state'=>'s',
  'sess'=>'sid',
  'session'=>'sid',
  'svgid'=>'sgid',
  'tt'=>'tname',
  'tty'=>'tname', 
  'vsz'=>'vsize'
}
PS_KEYS_DEFAULT =

The list of process keywords.

%w'blocked bsdtime c caught class cp egid egroup eip esp etime euid euser f fgid fgroup fuid fuser group ignored label lwp ni nlwp nwchan pending pcpu pgid pid pmem ppid pri psr rgid rgroup rss rtprio ruid ruser s sched sgi_p sgid sgroup sid sig size stackp start_time stat suid suser sz time tname tpgid vsize wchan'
PS_COMMAND_DEFAULT =

The process command, i.e. what the sytem will call for the “ps” command.

'ps h ww -o "'+self.ps_keys.join(',')+'"'

Class Method Summary collapse

Class Method Details

.ps(pid = Process.pid) ⇒ Object

Get the ‘ps’ command as one long text string.

This is typically useful for logging to a text file.



27
28
29
# File 'lib/webget_ruby_ramp/process.rb', line 27

def self.ps(pid=Process.pid)
  `#{self.ps_command} #{pid.to_i}`
end

.ps_aliasesObject

Get the list of process alias keywords as typically defined by the shell.

For example, a shell may consider “%cpu” and “pcpu” to be identical.



110
111
112
# File 'lib/webget_ruby_ramp/process.rb', line 110

def self.ps_aliases
  @@ps_aliases||=PS_ALIASES_DEFAULT
end

.ps_aliases=(aliases) ⇒ Object

Set the list of process alias keywords.

For example, a shell may consider “%cpu” and “pcpu” to be identical.



119
120
121
# File 'lib/webget_ruby_ramp/process.rb', line 119

def self.ps_aliases=(aliases)
  @@ps_aliases=aliases
end

.ps_commandObject

Get the process command, i.e. what the sytem will call for the “ps” command.

Example

Process.ps_command => "ps h ww -o blocked,group,pending,size"


159
160
161
# File 'lib/webget_ruby_ramp/process.rb', line 159

def self.ps_command
  @@ps_command||=PS_COMMAND_DEFAULT
end

.ps_command=(command) ⇒ Object

Set the process command, i.e. what the sytem will call for the “ps” command.

Example

Process.ps_command = "ps h ww -o blocked,group,pending,size"


169
170
171
# File 'lib/webget_ruby_ramp/process.rb', line 169

def self.ps_command=(command)
  @@ps_command=command
end

.ps_keysObject

Get the list of process keywords.

Example

Process.ps_keys => ["blocked","group","pending","size"]


134
135
136
# File 'lib/webget_ruby_ramp/process.rb', line 134

def self.ps_keys
  @@ps_keys||=PS_KEYS_DEFAULT
end

.ps_keys=(keys) ⇒ Object

Set the list of process keywords.

Example

Process.ps_keys = ["blocked","group","pending","size"]


144
145
146
# File 'lib/webget_ruby_ramp/process.rb', line 144

def self.ps_keys=(keys)
  @@ps_keys=keys
end

.pss(pid = Process.pid) ⇒ Object

Get the ‘ps’ command as a hash of keys and values. - OPTIMIZE: add dates, times



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/webget_ruby_ramp/process.rb', line 36

def self.pss(pid=Process.pid)
  ps=self.ps(pid)
  h=Hash[*self.ps_keys.zip(ps.split).flatten]
  h['c']      =h['c'].to_i
  h['cp']     =h['cp'].to_f
  h['egid']   =h['egid'].to_i
  h['egroup'] =h['egroup'].to_i
  h['uid']    =h['uid'].to_i
  h['fgid']   =h['fgid'].to_i
  h['lwp']    =h['lwp'].to_i
  h['ni']     =h['ni'].to_i
  h['nlwp']   =h['nlwp'].to_i
  h['pcpu']   =h['pcpu'].to_f
  h['pgid']   =h['pgid'].to_i
  h['pid']    =h['pid'].to_i
  h['pmem']   =h['pmem'].to_f
  h['ppid']   =h['ppid'].to_i
  h['rgid']   =h['rgid'].to_i
  h['rss']    =h['rss'].to_i
  h['ruid']   =h['ruid'].to_i  
  h['sid']    =h['sid'].to_i  
  h['sgid']   =h['sgid'].to_i  
  h['suid']   =h['suid'].to_i  
  self.ps_aliases.each_pair{|key,val| h[key]=h[val]}
  return h
end