Process::Metrics SourceProcessMetricsGeneral

class General

General process information.

Definitions

def as_json

Convert the object to a JSON serializable hash.

Implementation

def as_json
	{
		process_id: self.process_id,
		parent_process_id: self.parent_process_id,
		process_group_id: self.process_group_id,
		processor_utilization: self.processor_utilization,
		total_size: self.total_size,
		virtual_size: self.virtual_size,
		resident_size: self.resident_size,
		processor_time: self.processor_time,
		elapsed_time: self.elapsed_time,
		command: self.command,
		memory: self.memory&.as_json,
	}
end

def to_json(*arguments)

Convert the object to a JSON string.

Implementation

def to_json(*arguments)
	as_json.to_json(*arguments)
end

def total_size

The general memory usage of the process using the best available information.

Implementation

def total_size
	if memory = self.memory
		memory.proportional_size
	else
		self.resident_size
	end
end

def self.capture(pid: nil, ppid: nil, ps: PS)

Capture process information. If given a pid, it will capture the details of that process. If given a ppid, it will capture the details of all child processes. Specify both pid and ppid if you want to capture a process and all its children.

Signature

parameter pid Integer

The process ID to capture.

parameter ppid Integer

The parent process ID to capture.

Implementation

def self.capture(pid: nil, ppid: nil, ps: PS)
	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.process_id] = 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