Class: Rfc::CpuTimeAverager

Inherits:
Object
  • Object
show all
Defined in:
lib/rfc/cpu_time_averager.rb

Defined Under Namespace

Classes: Sample

Instance Method Summary collapse

Constructor Details

#initializeCpuTimeAverager

Returns a new instance of CpuTimeAverager.



8
9
10
# File 'lib/rfc/cpu_time_averager.rb', line 8

def initialize
  @samples = []
end

Instance Method Details

#enough?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/rfc/cpu_time_averager.rb', line 32

def enough?
  @samples.last.first - @samples.first.first >= 2
end

#firstObject



36
37
38
# File 'lib/rfc/cpu_time_averager.rb', line 36

def first
  @samples.first
end

#lastObject



40
41
42
# File 'lib/rfc/cpu_time_averager.rb', line 40

def last
  @samples.last
end

#sampleObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 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