class Process::Metrics::General

Public Class Methods

build_tree(processes) click to toggle source
# File lib/process/metrics/general.rb, line 101
def self.build_tree(processes)
        hierarchy = Hash.new{|h,k| h[k] = []}
        
        processes.each_value do |process|
                if ppid = process.ppid
                        hierarchy[ppid] << process.pid
                end
        end
        
        return hierarchy
end
capture(pid: nil, ppid: nil, ps: PS, fields: FIELDS) click to toggle source
# File lib/process/metrics/general.rb, line 119
def self.capture(pid: nil, ppid: nil, ps: PS, fields: FIELDS)
        input, output = IO.pipe
        
        arguments = [ps]
        
        if pid && ppid.nil?
                arguments.push("-p", Array(pid).join(','))
        else
                arguments.push("ax")
        end
        
        arguments.push("-o", fields.keys.join(','))
        
        ps_pid = Process.spawn(*arguments, out: output, pgroup: true)
        
        output.close
        
        header, *lines = input.readlines.map(&:strip)
        
        processes = {}
        
        lines.map do |line|
                record = fields.
                        zip(line.split(/\s+/, fields.size)).
                        map{|(key, type), value| type.call(value)}
                
                instance = self.new(*record)
                
                processes[instance.pid] = instance
        end
        
        if ppid
                pids = Set.new
                
                hierarchy = self.build_tree(processes)
                
                self.expand_children(Array(pid), hierarchy, pids)
                self.expand_children(Array(ppid), hierarchy, pids)
                
                processes.select! do |pid, process|
                        if pid != ps_pid
                                pids.include?(pid)
                        end
                end
        end
        
        if Memory.supported?
                self.capture_memory(processes)
                
                # if pid
                #  self.compute_summary(pid, processes)
                # end
        end
        
        return processes
ensure
        Process.wait(ps_pid) if ps_pid
end
capture_memory(processes) click to toggle source
# File lib/process/metrics/general.rb, line 113
def self.capture_memory(processes)
        processes.each do |pid, process|
                process.memory = Memory.capture(Array(pid))
        end
end
expand(pid, hierarchy, pids) click to toggle source
# File lib/process/metrics/general.rb, line 91
def self.expand(pid, hierarchy, pids)
        unless pids.include?(pid)
                pids << pid
                
                if children = hierarchy.fetch(pid, nil)
                        self.expand_children(children, hierarchy, pids)
                end
        end
end
expand_children(children, hierarchy, pids) click to toggle source
# File lib/process/metrics/general.rb, line 85
def self.expand_children(children, hierarchy, pids)
        children.each do |pid|
                self.expand(pid, hierarchy, pids)
        end
end

Public Instance Methods

as_json() click to toggle source
# File lib/process/metrics/general.rb, line 58
def as_json
        {
                pid: self.pid,
                ppid: self.ppid,
                pgid: self.pgid,
                pcpu: self.pcpu,
                vsz: self.vsz,
                rss: self.rss,
                time: self.time,
                etime: self.etime,
                command: self.command,
                memory: self.memory&.as_json,
        }
end
memory_usage() click to toggle source
# File lib/process/metrics/general.rb, line 77
def memory_usage
        if self.memory
                self.memory.proportional_size
        else
                self.rss
        end
end
to_json(*arguments) click to toggle source
# File lib/process/metrics/general.rb, line 73
def to_json(*arguments)
        as_json.to_json(*arguments)
end