class Rfc::CpuTimeAverager

Constants

Sample

Public Class Methods

new() click to toggle source
# File lib/rfc/cpu_time_averager.rb, line 8
def initialize
  @samples = []
end

Public Instance Methods

enough?() click to toggle source
# File lib/rfc/cpu_time_averager.rb, line 32
def enough?
  @samples.last.first - @samples.first.first >= 2
end
first() click to toggle source
# File lib/rfc/cpu_time_averager.rb, line 36
def first
  @samples.first
end
last() click to toggle source
# File lib/rfc/cpu_time_averager.rb, line 40
def last
  @samples.last
end
sample() click to toggle source
# File lib/rfc/cpu_time_averager.rb, line 12
def sample
  # https://www.linuxhowtos.org/manpages/5/proc.htm
  IO.readlines('/proc/stat').each do |line|
    if line =~ /^cpu/
      _, user, nice, system, idle, iowait, irq, softirq, steal,
        guest, guest_nice = line.split.map(&:to_i)
      @samples << [Time.now, Sample.new(
        user, nice, system, idle, iowait, irq, softirq, steal,
        guest, guest_nice,
      )]
      break
    end
  end

  threshold = Time.now - 10
  while @samples.length > 2 && @samples.first.first < threshold
    @samples.shift
  end
end