Class: Tnw::Metrics::Parsers::ProcStat

Inherits:
Object
  • Object
show all
Defined in:
lib/tnw/metrics/parsers/proc_stat.rb

Constant Summary collapse

CPU_COUNTER_COUNT =
8

Class Method Summary collapse

Class Method Details

.parse(previous:, current:) ⇒ Object

Raises:



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/tnw/metrics/parsers/proc_stat.rb', line 7

def self.parse(previous:, current:)
  previous_counters = counters(previous)
  current_counters = counters(current)
  deltas = current_counters.zip(previous_counters).map { |after, before| after - before }

  raise ParseError, "/proc/stat counters moved backwards" if deltas.any?(&:negative?)

  total_delta = deltas.sum
  raise ParseError, "/proc/stat contains no elapsed CPU time" unless total_delta.positive?

  idle_delta = deltas.fetch(3) + deltas.fetch(4)
  { percent: ((total_delta - idle_delta) * 100.0 / total_delta).round(2) }
end