Class: Datadog::Profiling::Collectors::ThreadContext

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/profiling/collectors/thread_context.rb,
ext/datadog_profiling_native_extension/collectors_thread_context.c

Overview

Used to trigger sampling of threads, based on external "events", such as:

  • periodic timer for cpu-time and wall-time
  • VM garbage collection events
  • VM object allocation events Triggering of this component (e.g. watching for the above "events") is implemented by Collectors::CpuAndWallTimeWorker. The stack collection itself is handled using the Datadog::Profiling::Collectors::Stack. Almost all of this class is implemented as native code.

Methods prefixed with native are implemented in collectors_thread_context.c

Defined Under Namespace

Modules: Testing

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(recorder:, max_frames:, tracer:, endpoint_collection_enabled:, otel_context_enabled:, native_filenames_enabled:, show_classes:) ⇒ ThreadContext

Returns a new instance of ThreadContext.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/datadog/profiling/collectors/thread_context.rb', line 17

def initialize(
  recorder:,
  max_frames:,
  tracer:,
  endpoint_collection_enabled:,
  otel_context_enabled:,
  native_filenames_enabled:,
  show_classes:
)
  tracer_context_key = safely_extract_context_key_from(tracer)
  self.class._native_initialize(
    self_instance: self,
    recorder: recorder,
    max_frames: max_frames,
    tracer_context_key: tracer_context_key,
    endpoint_collection_enabled: endpoint_collection_enabled,
    otel_context_enabled: otel_context_enabled,
    native_filenames_enabled: native_filenames_enabled,
    show_classes: show_classes,
    overhead_filename: __FILE__,
  )
end

Class Method Details

._native_initialize ⇒ Object



305
# File 'ext/datadog_profiling_native_extension/collectors_thread_context.c', line 305

static VALUE _native_initialize(int argc, VALUE *argv, DDTRACE_UNUSED VALUE _self);

._native_inspect ⇒ Object



332
# File 'ext/datadog_profiling_native_extension/collectors_thread_context.c', line 332

static VALUE _native_inspect(VALUE self, VALUE collector_instance);

._native_reset_after_fork ⇒ Object



349
# File 'ext/datadog_profiling_native_extension/collectors_thread_context.c', line 349

static VALUE _native_reset_after_fork(DDTRACE_UNUSED VALUE self, VALUE collector_instance);

.for_testing(recorder:, max_frames: 400, tracer: nil, endpoint_collection_enabled: false, otel_context_enabled: false, native_filenames_enabled: true, show_classes: false, trigger_global_reset: true, **options) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/datadog/profiling/collectors/thread_context.rb', line 40

def self.for_testing(
  recorder:,
  max_frames: 400,
  tracer: nil,
  endpoint_collection_enabled: false,
  otel_context_enabled: false,
  native_filenames_enabled: true,
  show_classes: false,
  trigger_global_reset: true,
  **options
)
  collector = new(
    recorder: recorder,
    max_frames: max_frames,
    tracer: tracer,
    endpoint_collection_enabled: endpoint_collection_enabled,
    otel_context_enabled: otel_context_enabled,
    native_filenames_enabled: native_filenames_enabled,
    show_classes: show_classes,
    **options,
  )

  # By default, mirror what the CpuAndWallTimeWorker does when profiling starts: reset the global per-thread
  # context state, which (among other things) resizes the per-thread sampling buffers to this collector's
  # max_frames. Tests that need to control this explicitly can pass `trigger_global_reset: false`.
  Testing._native_global_reset_per_thread_context(collector) if trigger_global_reset

  collector
end

Instance Method Details

#inspect ⇒ Object



70
71
72
73
74
75
# File 'lib/datadog/profiling/collectors/thread_context.rb', line 70

def inspect
  # Compose Ruby's default inspect with our custom inspect for the native parts
  result = super
  result[-1] = "#{self.class._native_inspect(self)}>"
  result
end

#reset_after_fork ⇒ Object



77
78
79
# File 'lib/datadog/profiling/collectors/thread_context.rb', line 77

def reset_after_fork
  self.class._native_reset_after_fork(self)
end