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

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/profiling/collectors/idle_sampling_helper.rb,
ext/datadog_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/datadog_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/datadog_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/datadog_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
49
50
# 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
    @worker_thread.name = self.class.name # Repeated from above to make sure thread gets named asap
    @worker_thread.thread_variable_set(:fork_safe, true)
  end

  true
end

#stopObject



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

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