class NewRelic::Agent::Threading::ThreadProfile

Constants

THREAD_PROFILER_NODES

Attributes

backtrace_count[R]
command_arguments[R]
created_at[R]
duration[R]
failure_count[R]
finished_at[RW]
poll_count[R]
profile_agent_code[R]
profile_id[R]
sample_period[R]
traces[R]

Public Class Methods

new(command_arguments = {}) click to toggle source
# 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

Public Instance Methods

aggregate(backtrace, bucket, thread) click to toggle source
# 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) click to toggle source
# 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?() click to toggle source
# File lib/new_relic/agent/threading/thread_profile.rb, line 51
def empty?
  @backtrace_count == 0
end
generate_traces() click to toggle source
# 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_count() click to toggle source
# File lib/new_relic/agent/threading/thread_profile.rb, line 47
def increment_poll_count
  @poll_count += 1
end
requested_period() click to toggle source
# File lib/new_relic/agent/threading/thread_profile.rb, line 43
def requested_period
  @sample_period
end
to_collector_array(encoder) click to toggle source
# 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_description() click to toggle source
# 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_count() click to toggle source
# 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