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.



151
152
153
154
# File 'lib/server_metrics/collectors/processes.rb', line 151

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



166
167
168
# File 'lib/server_metrics/collectors/processes.rb', line 166

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



150
151
152
# File 'lib/server_metrics/collectors/processes.rb', line 150

def recent_cpu
  @recent_cpu
end

#recent_cpu_percentageObject

used to store the calculation of CPU since last sample



150
151
152
# File 'lib/server_metrics/collectors/processes.rb', line 150

def recent_cpu_percentage
  @recent_cpu_percentage
end

Instance Method Details

#combined_cpuObject



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

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)


156
157
158
# File 'lib/server_metrics/collectors/processes.rb', line 156

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