Class: Process::Metrics::General

Inherits:
Struct
  • Object
show all
Defined in:
lib/process/metrics/general.rb

Overview

General process information.

Defined Under Namespace

Modules: Linux, ProcessStatus

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#commandObject

Returns the value of attribute command

Returns:

  • (Object)

    the current value of command



39
40
41
# File 'lib/process/metrics/general.rb', line 39

def command
  @command
end

#elapsed_timeObject

Returns the value of attribute elapsed_time

Returns:

  • (Object)

    the current value of elapsed_time



39
40
41
# File 'lib/process/metrics/general.rb', line 39

def elapsed_time
  @elapsed_time
end

#memoryObject

Returns the value of attribute memory

Returns:

  • (Object)

    the current value of memory



39
40
41
# File 'lib/process/metrics/general.rb', line 39

def memory
  @memory
end

#parent_process_idObject

Returns the value of attribute parent_process_id

Returns:

  • (Object)

    the current value of parent_process_id



39
40
41
# File 'lib/process/metrics/general.rb', line 39

def parent_process_id
  @parent_process_id
end

#process_group_idObject

Returns the value of attribute process_group_id

Returns:

  • (Object)

    the current value of process_group_id



39
40
41
# File 'lib/process/metrics/general.rb', line 39

def process_group_id
  @process_group_id
end

#process_idObject

Returns the value of attribute process_id

Returns:

  • (Object)

    the current value of process_id



39
40
41
# File 'lib/process/metrics/general.rb', line 39

def process_id
  @process_id
end

#processor_timeObject

Returns the value of attribute processor_time

Returns:

  • (Object)

    the current value of processor_time



39
40
41
# File 'lib/process/metrics/general.rb', line 39

def processor_time
  @processor_time
end

#processor_utilizationObject

Returns the value of attribute processor_utilization

Returns:

  • (Object)

    the current value of processor_utilization



39
40
41
# File 'lib/process/metrics/general.rb', line 39

def processor_utilization
  @processor_utilization
end

#resident_sizeObject

Returns the value of attribute resident_size

Returns:

  • (Object)

    the current value of resident_size



39
40
41
# File 'lib/process/metrics/general.rb', line 39

def resident_size
  @resident_size
end

#start_timeObject

Returns the value of attribute start_time

Returns:

  • (Object)

    the current value of start_time



39
40
41
# File 'lib/process/metrics/general.rb', line 39

def start_time
  @start_time
end

#virtual_sizeObject

Returns the value of attribute virtual_size

Returns:

  • (Object)

    the current value of virtual_size



39
40
41
# File 'lib/process/metrics/general.rb', line 39

def virtual_size
  @virtual_size
end

Class Method Details

.build_tree(processes) ⇒ Object

Build a parent-to-children process hierarchy from a set of processes.



101
102
103
104
105
106
107
108
109
110
111
# 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 parent_process_id = process.parent_process_id
			hierarchy[parent_process_id] << process.process_id
		end
	end
	
	return hierarchy
end

.captureObject



136
137
138
# File 'lib/process/metrics/general/linux.rb', line 136

def capture(...)
	Process::Metrics::General::Linux.capture(...)
end

.capture_memory(processes) ⇒ Object

Capture detailed memory metrics for each process in the given collection.



115
116
117
118
119
120
121
# File 'lib/process/metrics/general.rb', line 115

def self.capture_memory(processes)
	count = processes.size
	
	processes.each do |pid, process|
		process.memory = Memory.capture(pid, count: count)
	end
end

.expand(pid, hierarchy, pids) ⇒ Object

Recursively expand a process and its descendants into a collection.



88
89
90
91
92
93
94
95
96
# File 'lib/process/metrics/general.rb', line 88

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) ⇒ Object

Recursively expand a set of child PIDs into a collection.



78
79
80
81
82
# File 'lib/process/metrics/general.rb', line 78

def self.expand_children(children, hierarchy, pids)
	children.each do |pid|
		self.expand(pid, hierarchy, pids)
	end
end

Instance Method Details

#as_jsonObject

Convert the object to a JSON serializable hash.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/process/metrics/general.rb', line 41

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,
		start_time: self.start_time,
		command: self.command,
		memory: self.memory&.as_json,
	}
end

#The processor utilization in core units, where `1.0` represents one fully occupied CPU core. Multi-threaded processes may report values greater than `1.0`.=(processorutilization) ⇒ Object

General process information.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/process/metrics/general.rb', line 39

class General < Struct.new(:process_id, :parent_process_id, :process_group_id, :processor_utilization, :virtual_size, :resident_size, :processor_time, :elapsed_time, :start_time, :command, :memory)
	# Convert the object to a JSON serializable hash.
	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,
			start_time: self.start_time,
			command: self.command,
			memory: self.memory&.as_json,
		}
	end
	
	# Convert the object to a JSON string.
	def to_json(*arguments)
		as_json.to_json(*arguments)
	end
	
	# The total size of the process in memory, in bytes.
	def total_size
		if memory = self.memory
			memory.proportional_size
		else
			self.resident_size
		end
	end
	
	alias memory_usage total_size
	
	# Recursively expand a set of child PIDs into a collection.
	# @parameter children [Array<Integer>] The list of child process IDs to expand.
	# @parameter hierarchy [Hash<Integer, Array<Integer>>] The parent-to-children process hierarchy.
	# @parameter pids [Set<Integer>] The set to populate with process IDs.
	def self.expand_children(children, hierarchy, pids)
		children.each do |pid|
			self.expand(pid, hierarchy, pids)
		end
	end
	
	# Recursively expand a process and its descendants into a collection.
	# @parameter pid [Integer] The process ID to expand.
	# @parameter hierarchy [Hash<Integer, Array<Integer>>] The parent-to-children process hierarchy.
	# @parameter pids [Set<Integer>] The set to populate with process IDs.
	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
	
	# Build a parent-to-children process hierarchy from a set of processes.
	# @parameter processes [Hash<Integer, General>] A hash mapping PIDs to General instances.
	# @returns [Hash<Integer, Array<Integer>>] A hash mapping each parent PID to an array of child PIDs.
	def self.build_tree(processes)
		hierarchy = Hash.new{|h,k| h[k] = []}
		
		processes.each_value do |process|
			if parent_process_id = process.parent_process_id
				hierarchy[parent_process_id] << process.process_id
			end
		end
		
		return hierarchy
	end
	
	# Capture detailed memory metrics for each process in the given collection.
	# @parameter processes [Hash<Integer, General>] A hash mapping PIDs to General instances.
	def self.capture_memory(processes)
		count = processes.size
		
		processes.each do |pid, process|
			process.memory = Memory.capture(pid, count: count)
		end
	end
end

#to_json(*arguments) ⇒ Object

Convert the object to a JSON string.



59
60
61
# File 'lib/process/metrics/general.rb', line 59

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

#total_sizeObject Also known as: memory_usage

The total size of the process in memory, in bytes.



64
65
66
67
68
69
70
# File 'lib/process/metrics/general.rb', line 64

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