Class: NewRelic::Agent::ThreadProfile

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

Defined Under Namespace

Classes: Node

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(profile_id, duration, interval, profile_agent_code) ⇒ ThreadProfile

Returns a new instance of ThreadProfile.



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/new_relic/agent/thread_profiler.rb', line 116

def initialize(profile_id, duration, interval, profile_agent_code)
  @profile_id = profile_id
  @profile_agent_code = profile_agent_code

  @worker_loop = NewRelic::Agent::WorkerLoop.new(:duration => duration)
  @interval = interval
  @finished = false

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

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

Instance Attribute Details

#intervalObject (readonly)

Returns the value of attribute interval.



110
111
112
# File 'lib/new_relic/agent/thread_profiler.rb', line 110

def interval
  @interval
end

#poll_countObject (readonly)

Returns the value of attribute poll_count.



110
111
112
# File 'lib/new_relic/agent/thread_profiler.rb', line 110

def poll_count
  @poll_count
end

#profile_agent_codeObject (readonly)

Returns the value of attribute profile_agent_code.



110
111
112
# File 'lib/new_relic/agent/thread_profiler.rb', line 110

def profile_agent_code
  @profile_agent_code
end

#profile_idObject (readonly)

Returns the value of attribute profile_id.



110
111
112
# File 'lib/new_relic/agent/thread_profiler.rb', line 110

def profile_id
  @profile_id
end

#sample_countObject (readonly)

Returns the value of attribute sample_count.



110
111
112
# File 'lib/new_relic/agent/thread_profiler.rb', line 110

def sample_count
  @sample_count
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



110
111
112
# File 'lib/new_relic/agent/thread_profiler.rb', line 110

def start_time
  @start_time
end

#stop_timeObject (readonly)

Returns the value of attribute stop_time.



110
111
112
# File 'lib/new_relic/agent/thread_profiler.rb', line 110

def stop_time
  @stop_time
end

#tracesObject (readonly)

Returns the value of attribute traces.



110
111
112
# File 'lib/new_relic/agent/thread_profiler.rb', line 110

def traces
  @traces
end

Class Method Details

.flattened_nodes(nodes) ⇒ Object



250
251
252
# File 'lib/new_relic/agent/thread_profiler.rb', line 250

def self.flattened_nodes(nodes)
  nodes.map { |n| [n, flattened_nodes(n.children)] }.flatten
end

.parse_backtrace(trace) ⇒ Object



254
255
256
257
258
259
# File 'lib/new_relic/agent/thread_profiler.rb', line 254

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



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/new_relic/agent/thread_profiler.rb', line 174

def aggregate(trace, trees=@traces[:request], parent=nil)
  return nil if trace.nil? || trace.empty?
  node = Node.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)


236
237
238
# File 'lib/new_relic/agent/thread_profiler.rb', line 236

def finished?
  @finished
end

#mark_doneObject



240
241
242
243
# File 'lib/new_relic/agent/thread_profiler.rb', line 240

def mark_done
  @finished = true
  @stop_time = now_in_millis
end

#mark_for_pruning(nodes, count_to_keep) ⇒ Object



245
246
247
248
# File 'lib/new_relic/agent/thread_profiler.rb', line 245

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



232
233
234
# File 'lib/new_relic/agent/thread_profiler.rb', line 232

def now_in_millis
  Time.now.to_f * 1_000
end

#prune!(count_to_keep) ⇒ Object



196
197
198
199
200
201
202
203
204
205
# File 'lib/new_relic/agent/thread_profiler.rb', line 196

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| Node.prune!(nodes) }
end

#runObject



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/new_relic/agent/thread_profiler.rb', line 137

def run
  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
        AgentThread.list.each do |t|
          bucket = AgentThread.bucket_thread(t, @profile_agent_code)
          if bucket != :ignore
            backtrace = 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



168
169
170
171
172
# File 'lib/new_relic/agent/thread_profiler.rb', line 168

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

#to_collector_array(encoder) ⇒ Object



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/new_relic/agent/thread_profiler.rb', line 211

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