Class: NewRelic::Agent::Threading::ThreadProfile

Inherits:
Object
  • Object
show all
Includes:
Coerce
Defined in:
lib/new_relic/agent/threading/thread_profile.rb

Constant Summary collapse

THREAD_PROFILER_NODES =
20_000

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Coerce

boolean_int!, float, float!, int, int!, int_or_nil, log_failure, scalar, string, value_or_nil

Constructor Details

#initialize(command_arguments = {}) ⇒ ThreadProfile

Returns a new instance of ThreadProfile.



20
21
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/threading/thread_profile.rb', line 20

def initialize(command_arguments = {})
  @command_arguments = command_arguments
  @profile_id = command_arguments.fetch('profile_id', -1)
  @duration = command_arguments.fetch('duration', 120)
  @sample_period = command_arguments.fetch('sample_period', 0.1)
  @profile_agent_code = command_arguments.fetch('profile_agent_code', false)
  @finished = false

  @traces = {
    :agent => BacktraceRoot.new,
    :background => BacktraceRoot.new,
    :other => BacktraceRoot.new,
    :request => BacktraceRoot.new
  }

  @poll_count = 0
  @backtrace_count = 0
  @failure_count = 0
  @unique_threads = []

  @created_at = Process.clock_gettime(Process::CLOCK_REALTIME)
end

Instance Attribute Details

#backtrace_countObject (readonly)

Returns the value of attribute backtrace_count.



15
16
17
# File 'lib/new_relic/agent/threading/thread_profile.rb', line 15

def backtrace_count
  @backtrace_count
end

#command_argumentsObject (readonly)

Returns the value of attribute command_arguments.



15
16
17
# File 'lib/new_relic/agent/threading/thread_profile.rb', line 15

def command_arguments
  @command_arguments
end

#created_atObject (readonly)

Returns the value of attribute created_at.



15
16
17
# File 'lib/new_relic/agent/threading/thread_profile.rb', line 15

def created_at
  @created_at
end

#durationObject (readonly)

Returns the value of attribute duration.



15
16
17
# File 'lib/new_relic/agent/threading/thread_profile.rb', line 15

def duration
  @duration
end

#failure_countObject (readonly)

Returns the value of attribute failure_count.



15
16
17
# File 'lib/new_relic/agent/threading/thread_profile.rb', line 15

def failure_count
  @failure_count
end

#finished_atObject

Returns the value of attribute finished_at.



18
19
20
# File 'lib/new_relic/agent/threading/thread_profile.rb', line 18

def finished_at
  @finished_at
end

#poll_countObject (readonly)

Returns the value of attribute poll_count.



15
16
17
# File 'lib/new_relic/agent/threading/thread_profile.rb', line 15

def poll_count
  @poll_count
end

#profile_agent_codeObject (readonly)

Returns the value of attribute profile_agent_code.



15
16
17
# File 'lib/new_relic/agent/threading/thread_profile.rb', line 15

def profile_agent_code
  @profile_agent_code
end

#profile_idObject (readonly)

Returns the value of attribute profile_id.



15
16
17
# File 'lib/new_relic/agent/threading/thread_profile.rb', line 15

def profile_id
  @profile_id
end

#sample_periodObject (readonly)

Returns the value of attribute sample_period.



15
16
17
# File 'lib/new_relic/agent/threading/thread_profile.rb', line 15

def sample_period
  @sample_period
end

#tracesObject (readonly)

Returns the value of attribute traces.



15
16
17
# File 'lib/new_relic/agent/threading/thread_profile.rb', line 15

def traces
  @traces
end

Instance Method Details

#aggregate(backtrace, bucket, thread) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/new_relic/agent/threading/thread_profile.rb', line 61

def aggregate(backtrace, bucket, thread)
  if backtrace.nil?
    @failure_count += 1
  else
    @backtrace_count += 1
    @traces[bucket].aggregate(backtrace)
    @unique_threads << thread unless @unique_threads.include?(thread)
  end
end

#convert_N_trace_nodes_to_arrays(count_to_keep) ⇒ Object

THREAD_LOCAL_ACCESS



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/new_relic/agent/threading/thread_profile.rb', line 71

def convert_N_trace_nodes_to_arrays(count_to_keep) # THREAD_LOCAL_ACCESS
  all_nodes = @traces.values.map { |n| n.flattened }.flatten

  NewRelic::Agent.instance.stats_engine
    .tl_record_supportability_metric_count('ThreadProfiler/NodeCount', all_nodes.size)

  all_nodes.sort! do |a, b|
    # we primarily prefer higher runnable_count
    comparison = b.runnable_count <=> a.runnable_count
    # we secondarily prefer lower depth
    comparison = a.depth <=> b.depth if comparison == 0
    # it is thus impossible for any child to precede their parent
    comparison
  end

  all_nodes.each_with_index do |n, i|
    break if i >= count_to_keep

    n.mark_for_array_conversion
  end
  all_nodes.each_with_index do |n, i|
    break if i >= count_to_keep

    n.complete_array_conversion
  end
end

#empty?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/new_relic/agent/threading/thread_profile.rb', line 51

def empty?
  @backtrace_count == 0
end

#generate_tracesObject



102
103
104
105
106
107
108
109
110
111
# File 'lib/new_relic/agent/threading/thread_profile.rb', line 102

def generate_traces
  convert_N_trace_nodes_to_arrays(THREAD_PROFILER_NODES)

  {
    'OTHER' => @traces[:other].as_array,
    'REQUEST' => @traces[:request].as_array,
    'AGENT' => @traces[:agent].as_array,
    'BACKGROUND' => @traces[:background].as_array
  }
end

#increment_poll_countObject



47
48
49
# File 'lib/new_relic/agent/threading/thread_profile.rb', line 47

def increment_poll_count
  @poll_count += 1
end

#requested_periodObject



43
44
45
# File 'lib/new_relic/agent/threading/thread_profile.rb', line 43

def requested_period
  @sample_period
end

#to_collector_array(encoder) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/new_relic/agent/threading/thread_profile.rb', line 113

def to_collector_array(encoder)
  encoded_trace_tree = encoder.encode(generate_traces, :skip_normalization => true)
  result = [
    int(profile_id),
    float(created_at),
    float(finished_at),
    int(poll_count),
    encoded_trace_tree,
    int(unique_thread_count),
    0 # runnable thread count, which we don't track
  ]
  result
end

#to_log_descriptionObject



127
128
129
130
131
# File 'lib/new_relic/agent/threading/thread_profile.rb', line 127

def to_log_description
  "#<ThreadProfile:#{object_id} " \
    "@profile_id: #{profile_id} " \
    "@command_arguments=#{@command_arguments.inspect}>"
end

#unique_thread_countObject



55
56
57
58
59
# File 'lib/new_relic/agent/threading/thread_profile.rb', line 55

def unique_thread_count
  return 0 if @unique_threads.nil?

  @unique_threads.length
end