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

#initialize(thread_context_collector:) ⇒ IdleSamplingHelper

: (thread_context_collector: Datadog::Profiling::Collectors::ThreadContext) -> void



22
23
24
25
26
# File 'lib/datadog/profiling/collectors/idle_sampling_helper.rb', line 22

def initialize(thread_context_collector:)
  @worker_thread = nil
  @start_stop_mutex = Mutex.new
  @thread_context_collector = thread_context_collector
end

Class Method Details

._native_idle_sampling_loop ⇒ Object



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

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

._native_reset ⇒ Object



40
# File 'ext/datadog_profiling_native_extension/collectors_idle_sampling_helper.c', line 40

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

._native_stop ⇒ Object



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

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

Instance Method Details

#start ⇒ Object

: () -> (nil | true)



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/datadog/profiling/collectors/idle_sampling_helper.rb', line 29

def start
  @start_stop_mutex.synchronize do
    return if @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
      Thread.current.name = self.class.name

      self.class._native_idle_sampling_loop(self, @thread_context_collector)

      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}: #{e.message} Location: #{Array(e.backtrace).first}"
      )
      Datadog::Core::Telemetry::Logger.report(e, description: "IdleSamplingHelper thread error")
    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

#stop ⇒ Object

: () -> void



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/datadog/profiling/collectors/idle_sampling_helper.rb', line 61

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