Module: Cognizant::System::PS::ClassMethods

Defined in:
lib/cognizant/system/ps.rb

Constant Summary collapse

IDX_MAP =

Fields to fetch from ps.

{
  :pid  => 0,
  :ppid => 1,
  :pcpu => 2,
  :rss  => 3
}

Instance Method Summary collapse

Instance Method Details

#cpu_usage(pid) ⇒ Object


17
18
19
# File 'lib/cognizant/system/ps.rb', line 17

def cpu_usage(pid)
  ps_axu[pid] && ps_axu[pid][IDX_MAP[:pcpu]].to_f
end

#get_children(parent_pid) ⇒ Object


25
26
27
28
29
30
31
# File 'lib/cognizant/system/ps.rb', line 25

def get_children(parent_pid)
  child_pids = Array.new
  ps_axu.each_pair do |pid, chunks|
    child_pids << chunks[IDX_MAP[:pid]].to_i if chunks[IDX_MAP[:ppid]].to_i == parent_pid.to_i
  end
  child_pids
end

#memory_usage(pid) ⇒ Object


21
22
23
# File 'lib/cognizant/system/ps.rb', line 21

def memory_usage(pid)
  ps_axu[pid] && ps_axu[pid][IDX_MAP[:rss]].to_f
end

#ps_axuObject


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/cognizant/system/ps.rb', line 37

def ps_axu
  # TODO: Need a mutex here?
  store[:ps_axu] ||= begin
    # BSD style ps invocation.
    lines = `ps axo #{IDX_MAP.keys.join(",")}`.split("\n")

    lines.inject(Hash.new) do |mem, line|
      chunks = line.split(/\s+/)
      chunks.delete_if {|c| c.strip.empty? }
      pid = chunks[IDX_MAP[:pid]].strip.to_i
      mem[pid] = chunks
      mem
    end
  end
end

#reset_data!Object


33
34
35
# File 'lib/cognizant/system/ps.rb', line 33

def reset_data!
  store.clear unless store.empty?
end