Class: Process::Metrics::Processor
- Inherits:
-
Object
- Object
- Process::Metrics::Processor
- Defined in:
- lib/process/metrics/processor.rb
Overview
Computes interval CPU utilization from cumulative process metrics.
Defined Under Namespace
Instance Method Summary collapse
-
#initialize(capture: General.method(:capture)) ⇒ Processor
constructor
Initialize a process metrics sampler.
-
#sample(process_ids) ⇒ Object
Sample CPU utilization for the given processes.
Constructor Details
Instance Method Details
#sample(process_ids) ⇒ Object
Sample CPU utilization for the given processes. The first observation of each process establishes a baseline and does not produce a sample.
32 33 34 35 36 37 38 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 |
# File 'lib/process/metrics/processor.rb', line 32 def sample(process_ids) processes = @capture.call(pid: process_ids, memory: false) = now return {} unless finite?() samples = {} snapshots = {} processes.each do |process_id, process| start_time = process.start_time processor_time = process.processor_time next unless finite?(start_time) && finite?(processor_time) current = Snapshot.new(start_time, processor_time, ) snapshots[process_id] = current if previous = @snapshots[process_id] next unless previous.start_time == current.start_time duration = current. - previous. processor_time = current.processor_time - previous.processor_time next unless finite?(duration) && duration > 0.0 next unless finite?(processor_time) && processor_time >= 0.0 utilization = processor_time / duration next unless finite?(utilization) samples[process_id] = Sample.new(process_id, duration, processor_time, utilization).freeze end end @snapshots = snapshots return samples end |