Class: CpuInspectCore::Collectors::LinuxCollector

Inherits:
CpuInspectCore::Collector show all
Defined in:
lib/cpu_inspect_core/collectors/linux_collector.rb

Constant Summary collapse

PROC_STAT =
'/proc/stat'
IDLE_INDEX =

/proc/stat field layout (0-indexed, after the cpu name token):

0:user  1:nice  2:system  3:idle  4:iowait  5:irq  6:softirq  7:steal  8:guest  9:guest_nice

iowait (index 4) is intentionally counted as busy — a blocked process is not idle and should show up in load on Heroku dynos.

3

Instance Method Summary collapse

Constructor Details

#initializeLinuxCollector

Returns a new instance of LinuxCollector.



17
18
19
20
21
# File 'lib/cpu_inspect_core/collectors/linux_collector.rb', line 17

def initialize
  super
  @previous = {}
  @mutex    = Mutex.new
end

Instance Method Details

#available?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/cpu_inspect_core/collectors/linux_collector.rb', line 23

def available?
  File.exist?(PROC_STAT)
end

#sampleObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/cpu_inspect_core/collectors/linux_collector.rb', line 27

def sample
  current = read_proc_stat
  result  = {}

  @mutex.synchronize do
    current.each do |name, fields|
      prev = @previous[name]
      result[name] = compute_usage(prev, fields) if prev
    end
    @previous = current
  end

  result
end