Class: Datadog::Profiling::Collectors::IdleSamplingHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/profiling/collectors/idle_sampling_helper.rb,
ext/ddtrace_profiling_native_extension/collectors_idle_sampling_helper.c

Overview

Used by the Collectors::CpuAndWallTimeWorker to gather samples when the Ruby process is idle. Almost all of this class is implemented as native code.

Methods prefixed with native are implemented in ‘collectors_idle_sampling_helper.c`

Defined Under Namespace

Modules: Testing

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeIdleSamplingHelper

Returns a new instance of IdleSamplingHelper.



15
16
17
18
# File 'lib/datadog/profiling/collectors/idle_sampling_helper.rb', line 15

def initialize
  @worker_thread = nil
  @start_stop_mutex = Mutex.new
end

Class Method Details

._native_idle_sampling_loopObject



33
# File 'ext/ddtrace_profiling_native_extension/collectors_idle_sampling_helper.c', line 33

static VALUE _native_idle_sampling_loop(DDTRACE_UNUSED VALUE self, VALUE self_instance);

._native_resetObject



37
# File 'ext/ddtrace_profiling_native_extension/collectors_idle_sampling_helper.c', line 37

static VALUE _native_reset(DDTRACE_UNUSED VALUE self, VALUE self_instance);

._native_stopObject



34
# File 'ext/ddtrace_profiling_native_extension/collectors_idle_sampling_helper.c', line 34

static VALUE _native_stop(DDTRACE_UNUSED VALUE self, VALUE self_instance);

Instance Method Details

#startObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/datadog/profiling/collectors/idle_sampling_helper.rb', line 20

def start
  @start_stop_mutex.synchronize do
    return if @worker_thread && @worker_thread.alive?

    Datadog.logger.debug { "Starting thread for: #{self}" }

    # The same instance of the IdleSamplingHelper can be reused multiple times, and this resets it back to
    # a pristine state before recreating the worker thread
    self.class._native_reset(self)

    @worker_thread = Thread.new do
      begin
        Thread.current.name = self.class.name

        self.class._native_idle_sampling_loop(self)

        Datadog.logger.debug('IdleSamplingHelper thread stopping cleanly')
      rescue Exception => e # rubocop:disable Lint/RescueException
        @failure_exception = e
        Datadog.logger.warn(
          'IdleSamplingHelper thread error. ' \
          "Cause: #{e.class.name} #{e.message} Location: #{Array(e.backtrace).first}"
        )
      end
    end
  end

  true
end

#stop(*_) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/datadog/profiling/collectors/idle_sampling_helper.rb', line 50

def stop(*_)
  @start_stop_mutex.synchronize do
    Datadog.logger.debug('Requesting IdleSamplingHelper thread shut down')

    return unless @worker_thread

    self.class._native_stop(self)

    @worker_thread.join
    @worker_thread = nil
    @failure_exception = nil
  end
end