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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Coerce

#float, #int, #log_failure, #string

Constructor Details

#initialize(agent_command) ⇒ ThreadProfile

Returns a new instance of ThreadProfile.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/new_relic/agent/threading/thread_profile.rb', line 24

def initialize(agent_command)
  arguments = agent_command.arguments
  @profile_id = arguments.fetch('profile_id', -1)
  @profile_agent_code = arguments.fetch('profile_agent_code', true)

  @duration = arguments.fetch('duration', 120)
  @worker_loop = NewRelic::Agent::WorkerLoop.new(:duration => duration)
  @interval = arguments.fetch('sample_period', 0.1)
  @finished = false

  @traces = {
    :agent => [],
    :background => [],
    :other => [],
    :request => []
  }
  @flattened_nodes = []

  @poll_count = 0
  @sample_count = 0
  @failure_count = 0
end

Instance Attribute Details

#durationObject (readonly)

Returns the value of attribute duration.



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

def duration
  @duration
end

#intervalObject (readonly)

Returns the value of attribute interval.



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

def interval
  @interval
end

#poll_countObject (readonly)

Returns the value of attribute poll_count.



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

def poll_count
  @poll_count
end

#profile_agent_codeObject (readonly)

Returns the value of attribute profile_agent_code.



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

def profile_agent_code
  @profile_agent_code
end

#profile_idObject (readonly)

Returns the value of attribute profile_id.



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

def profile_id
  @profile_id
end

#sample_countObject (readonly)

Returns the value of attribute sample_count.



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

def sample_count
  @sample_count
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



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

def start_time
  @start_time
end

#stop_timeObject (readonly)

Returns the value of attribute stop_time.



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

def stop_time
  @stop_time
end

#tracesObject (readonly)

Returns the value of attribute traces.



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

def traces
  @traces
end

Class Method Details

.parse_backtrace(trace) ⇒ Object



162
163
164
165
166
167
# File 'lib/new_relic/agent/threading/thread_profile.rb', line 162

def self.parse_backtrace(trace)
  trace.map do |line|
    line =~ /(.*)\:(\d+)\:in `(.*)'/
      { :method => $3, :line_no => $2.to_i, :file => $1 }
  end
end

Instance Method Details

#aggregate(trace, trees = , parent = nil) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/new_relic/agent/threading/thread_profile.rb', line 86

def aggregate(trace, trees=@traces[:request], parent=nil)
  return nil if trace.nil? || trace.empty?
  node = Threading::BacktraceNode.new(trace.last)
  existing = trees.find {|n| n == node}

  if existing.nil?
    existing = node
    @flattened_nodes << node
  end

  if parent
    parent.add_child(node)
  else
    trees << node unless trees.include? node
  end

  existing.runnable_count += 1
  aggregate(trace[0..-2], existing.children, existing)

  existing
end

#finished?Boolean

Returns:

  • (Boolean)


148
149
150
# File 'lib/new_relic/agent/threading/thread_profile.rb', line 148

def finished?
  @finished
end

#mark_doneObject



152
153
154
155
# File 'lib/new_relic/agent/threading/thread_profile.rb', line 152

def mark_done
  @finished = true
  @stop_time = now_in_millis
end

#mark_for_pruning(nodes, count_to_keep) ⇒ Object



157
158
159
160
# File 'lib/new_relic/agent/threading/thread_profile.rb', line 157

def mark_for_pruning(nodes, count_to_keep)
  to_prune = nodes[count_to_keep..-1] || []
  to_prune.each { |n| n.to_prune = true }
end

#now_in_millisObject



144
145
146
# File 'lib/new_relic/agent/threading/thread_profile.rb', line 144

def now_in_millis
  Time.now.to_f * 1_000
end

#prune!(count_to_keep) ⇒ Object



108
109
110
111
112
113
114
115
116
117
# File 'lib/new_relic/agent/threading/thread_profile.rb', line 108

def prune!(count_to_keep)
  @flattened_nodes.sort!(&:order_for_pruning)

  NewRelic::Agent.instance.stats_engine.
    record_supportability_metric_count("ThreadProfiler/NodeCount", @flattened_nodes.size)

  mark_for_pruning(@flattened_nodes, count_to_keep)

  traces.each { |_, nodes| Threading::BacktraceNode.prune!(nodes) }
end

#runObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/new_relic/agent/threading/thread_profile.rb', line 47

def run
  NewRelic::Agent.logger.debug("Starting thread profile. profile_id=#{profile_id}, duration=#{duration}")

  Threading::AgentThread.new('Thread Profiler') do
    @start_time = now_in_millis

    @worker_loop.run(@interval) do
      NewRelic::Agent.instance.stats_engine.
        record_supportability_metric_timed("ThreadProfiler/PollingTime") do

        @poll_count += 1
        Threading::AgentThread.list.each do |t|
          bucket = Threading::AgentThread.bucket_thread(t, @profile_agent_code)
          if bucket != :ignore
            backtrace = Threading::AgentThread.scrub_backtrace(t, @profile_agent_code)
            if backtrace.nil?
              @failure_count += 1
            else
              @sample_count += 1
              aggregate(backtrace, @traces[bucket])
            end
          end
        end
        end
    end

    mark_done
    NewRelic::Agent.logger.debug("Finished thread profile. #{@sample_count} backtraces, #{@failure_count} failures. Will send with next harvest.")
    NewRelic::Agent.instance.stats_engine.
      record_supportability_metric_count("ThreadProfiler/BacktraceFailures", @failure_count)
  end
end

#stopObject



80
81
82
83
84
# File 'lib/new_relic/agent/threading/thread_profile.rb', line 80

def stop
  @worker_loop.stop
  mark_done
  NewRelic::Agent.logger.debug("Stopping thread profile.")
end

#to_collector_array(encoder) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/new_relic/agent/threading/thread_profile.rb', line 123

def to_collector_array(encoder)
  prune!(THREAD_PROFILER_NODES)

  traces = {
    "OTHER" => @traces[:other].map{|t| t.to_array },
    "REQUEST" => @traces[:request].map{|t| t.to_array },
    "AGENT" => @traces[:agent].map{|t| t.to_array },
    "BACKGROUND" => @traces[:background].map{|t| t.to_array }
  }

  [[
    int(@profile_id),
    float(@start_time),
    float(@stop_time),
    int(@poll_count),
    string(encoder.encode(traces)),
    int(@sample_count),
    0
  ]]
end