Class: ServerMetrics::Processes::Process

Inherits:
Object
  • Object
show all
Defined in:
lib/server_metrics/collectors/processes.rb

Overview

a thin wrapper around Sys:ProcTable’s ProcTableStruct. We’re using it to add some fields and behavior. Beyond what we’re adding, it just passes through to its instance of ProcTableStruct

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(proctable_struct) ⇒ Process

Returns a new instance of Process.



159
160
161
162
# File 'lib/server_metrics/collectors/processes.rb', line 159

def initialize(proctable_struct)
  @pts=proctable_struct
  @recent_cpu = 0
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object

delegate everything else to ProcTable::Struct



174
175
176
# File 'lib/server_metrics/collectors/processes.rb', line 174

def method_missing(sym, *args, &block)
  @pts.send sym, *args, &block
end

Instance Attribute Details

#recent_cpuObject

used to store the calculation of CPU since last sample



158
159
160
# File 'lib/server_metrics/collectors/processes.rb', line 158

def recent_cpu
  @recent_cpu
end

#recent_cpu_percentageObject

used to store the calculation of CPU since last sample



158
159
160
# File 'lib/server_metrics/collectors/processes.rb', line 158

def recent_cpu_percentage
  @recent_cpu_percentage
end

Instance Method Details

#combined_cpuObject



167
168
169
170
171
172
# File 'lib/server_metrics/collectors/processes.rb', line 167

def combined_cpu
  # best thread I've seen on cutime vs utime & cstime vs stime: https://www.ruby-forum.com/topic/93176
  # * cutime and cstime include CPUusage of child processes
  # * utime and stime don't include CPU usage of child processes
  (utime || 0) + (stime || 0)  # utime and stime aren't available on mac. Result is %cpu is 0 on mac.
end

#has?(method_name) ⇒ Boolean

because apparently respond_to doesn’t work through method_missing

Returns:

  • (Boolean)


164
165
166
# File 'lib/server_metrics/collectors/processes.rb', line 164

def has?(method_name)
  @pts.respond_to?(method_name)
end