Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add support to handle grandchild processes

  • Loading branch information...
commit f998e267f0a37dec4167a7f5e989363d2a69369c 1 parent d2c196b
@smly smly authored
Showing with 20 additions and 11 deletions.
  1. +20 −11 lib/god/system/slash_proc_extended_poller.rb
View
31 lib/god/system/slash_proc_extended_poller.rb
@@ -7,20 +7,20 @@ class SlashProcExtendedPoller < PortablePoller
@@processes = nil
@@processes_fetch = 0
-
+
# FreeBSD has /proc by default, but nothing mounted there!
# So we should check for the actual required paths!
# Returns true if +RequiredPaths+ are readable.
def self.usable?
God::System.usable?
end
-
+
def memory
stat[:rss].to_i * @@kb_per_page
rescue # This shouldn't fail is there's an error (or proc doesn't exist)
0
end
-
+
def percent_memory
(memory / God::System.total_mem.to_f) * 100
rescue # This shouldn't fail is there's an error (or proc doesn't exist)
@@ -61,7 +61,7 @@ def group_memory
mem += pg[0][:smaps][:Shared_Clean] || 0
mem
end
-
+
def group_cpu
cpu = 0
processgroup.each do |process|
@@ -79,9 +79,9 @@ def group_cpu
return cpu
end
end
-
+
private
-
+
# Some systems (CentOS?) have a /proc, but they can hang when trying to
# read from them. Try to use this sparingly as it is expensive.
def self.readable?(path)
@@ -96,7 +96,7 @@ def self.readable?(path)
def uptime
File.read(UptimePath).split[0].to_f
end
-
+
def stat(pid = @pid)
stats = {}
if File.exist?("/proc/#{pid}/") && File.exist?("/proc/#{pid}/stat")
@@ -125,13 +125,22 @@ def processgroup(check_smaps = true)
stats = stat
stats[:smaps] = smaps(@pid) if check_smaps
processes = [stats]
+
+ parentdict = Hash.new(nil)
Dir.glob('/proc/[1-9]*') do |process|
pid = process.match(/(\d+)$/).to_a[1]
stats = stat(pid)
- if @pid == stats[:ppid].to_i
- stats[:smaps] = smaps(pid) if check_smaps
- processes << stats
- end
+ ppid = stats[:ppid].to_i
+ parentdict[ppid] = Array.new if parentdict[ppid].nil?
+ parentdict[ppid] << pid.to_i
+ end
+ queue = parentdict[@pid].nil? ? [] : Array.new(parentdict[@pid])
+ while not queue.empty?
+ pid = queue.pop
+ queue.concat parentdict[pid] if not parentdict[pid].nil?
+ stats = stat(pid)
+ stats[:smaps] = smaps(pid) if check_smaps
+ processes << stats
end
if check_smaps
@@processes_fetch = Time.now
Please sign in to comment.
Something went wrong with that request. Please try again.