Class: ScoutRails::Instruments::Process::ProcessCpu

Inherits:
Object
  • Object
show all
Defined in:
lib/scout_rails/instruments/process/process_cpu.rb

Instance Method Summary collapse

Constructor Details

#initialize(num_processors) ⇒ ProcessCpu

Returns a new instance of ProcessCpu.



4
5
6
# File 'lib/scout_rails/instruments/process/process_cpu.rb', line 4

def initialize(num_processors)
  @num_processors = num_processors || 1
end

Instance Method Details

#runObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/scout_rails/instruments/process/process_cpu.rb', line 8

def run
  res=nil
  now = Time.now
  t = ::Process.times
  if @last_run
    elapsed_time = now - @last_run
    if elapsed_time >= 1
      user_time_since_last_sample = t.utime - @last_utime
      system_time_since_last_sample = t.stime - @last_stime
      res = ((user_time_since_last_sample + system_time_since_last_sample)/(elapsed_time * @num_processors))*100
    end
  end
  @last_utime = t.utime
  @last_stime = t.stime
  @last_run = now
  return res
end