Class: OneApm::Support::VM::MonotonicGCProfiler

Inherits:
Object
  • Object
show all
Defined in:
lib/one_apm/support/vm/monotonic_gc_profiler.rb

Instance Method Summary collapse

Constructor Details

#initializeMonotonicGCProfiler

Returns a new instance of MonotonicGCProfiler.



15
16
17
18
# File 'lib/one_apm/support/vm/monotonic_gc_profiler.rb', line 15

def initialize
  @total_time_s = 0
  @lock         = Mutex.new
end

Instance Method Details

#total_time_sObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/one_apm/support/vm/monotonic_gc_profiler.rb', line 20

def total_time_s
  if OneApm::LanguageSupport.gc_profiler_enabled?
    # There's a race here if the next two lines don't execute as an atomic
    # unit - we may end up double-counting some GC time in that scenario.
    # Locking around them guarantees atomicity of the read/increment/reset
    # sequence.
    @lock.synchronize do
      # The Ruby 1.9.x docs claim that GC::Profiler.total_time returns
      # a value in milliseconds. They are incorrect - both 1.9.x and 2.x
      # return values in seconds.
      @total_time_s += ::GC::Profiler.total_time
      ::GC::Profiler.clear
    end
  else
    OneApm::Manager.logger.log_once(:warn, :gc_profiler_disabled,
      "Tried to measure GC time, but GC::Profiler was not enabled.")
  end

  @total_time_s
end