Class: NewRelic::Agent::VM::MonotonicGCProfiler

Inherits:
Object
  • Object
show all
Defined in:
lib/new_relic/agent/vm/monotonic_gc_profiler.rb

Instance Method Summary collapse

Constructor Details

#initializeMonotonicGCProfiler

Returns a new instance of MonotonicGCProfiler.



17
18
19
20
# File 'lib/new_relic/agent/vm/monotonic_gc_profiler.rb', line 17

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

Instance Method Details

#total_time_sObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/new_relic/agent/vm/monotonic_gc_profiler.rb', line 22

def total_time_s
  if NewRelic::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
    NewRelic::Agent.logger.log_once(:warn, :gc_profiler_disabled,
      'Tried to measure GC time, but GC::Profiler was not enabled.')
  end

  @total_time_s
end