Module: VCAP::ProcessUtils

Defined in:
lib/vcap/process_utils.rb

Constant Summary collapse

STAT_FIELDS =
[
  {:name => :rss,   :parse_method => :to_i},
  {:name => :vsize, :parse_method => :to_i},
  {:name => :pcpu,  :parse_method => :to_f},
]

Class Method Summary collapse

Class Method Details

.get_stats(pid = nil) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/vcap/process_utils.rb', line 15

def get_stats(pid=nil)
  pid ||= Process.pid

  flags = STAT_FIELDS.map {|f| "-o #{f[:name]}=" }.join(' ')
  begin
    stdout, stderr, status = VCAP::Subprocess.run("ps #{flags} -p #{pid}")
  rescue VCAP::SubprocessStatusError => se
    # Process not running
    if se.status.exitstatus == 1
      return nil
    else
      raise se
    end
  end

  ret = {}
  stdout.split.each_with_index do |val, ii|
    field = STAT_FIELDS[ii]
    ret[field[:name]] = val.send(field[:parse_method])
  end

  ret
end