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.



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/new_relic/agent/thread_profiler.rb', line 112

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.



106
107
108
# File 'lib/new_relic/agent/thread_profiler.rb', line 106

def interval
  @interval
end

#poll_countObject (readonly)

Returns the value of attribute poll_count.



106
107
108
# File 'lib/new_relic/agent/thread_profiler.rb', line 106

def poll_count
  @poll_count
end

#profile_agent_codeObject (readonly)

Returns the value of attribute profile_agent_code.



106
107
108
# File 'lib/new_relic/agent/thread_profiler.rb', line 106

def profile_agent_code
  @profile_agent_code
end

#profile_idObject (readonly)

Returns the value of attribute profile_id.



106
107
108
# File 'lib/new_relic/agent/thread_profiler.rb', line 106

def profile_id
  @profile_id
end

#sample_countObject (readonly)

Returns the value of attribute sample_count.



106
107
108
# File 'lib/new_relic/agent/thread_profiler.rb', line 106

def sample_count
  @sample_count
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



106
107
108
# File 'lib/new_relic/agent/thread_profiler.rb', line 106

def start_time
  @start_time
end

#stop_timeObject (readonly)

Returns the value of attribute stop_time.



106
107
108
# File 'lib/new_relic/agent/thread_profiler.rb', line 106

def stop_time
  @stop_time
end

#tracesObject (readonly)

Returns the value of attribute traces.



106
107
108
# File 'lib/new_relic/agent/thread_profiler.rb', line 106

def traces
  @traces
end

Class Method Details

.flattened_nodes(nodes) ⇒ Object



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

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

.parse_backtrace(trace) ⇒ Object



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

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



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/new_relic/agent/thread_profiler.rb', line 170

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)


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

def finished?
  @finished
end

#mark_doneObject



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

def mark_done
  @finished = true
  @stop_time = now_in_millis
end

#mark_for_pruning(nodes, count_to_keep) ⇒ Object



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

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



228
229
230
# File 'lib/new_relic/agent/thread_profiler.rb', line 228

def now_in_millis
  Time.now.to_f * 1_000
end

#prune!(count_to_keep) ⇒ Object



192
193
194
195
196
197
198
199
200
201
# File 'lib/new_relic/agent/thread_profiler.rb', line 192

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

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

  mark_for_pruning(@flattened_nodes, count_to_keep)

  traces.each { |_, nodes| Node.prune!(nodes) }
end

#runObject



133
134
135
136
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
# File 'lib/new_relic/agent/thread_profiler.rb', line 133

def run
  AgentThread.new('Thread Profiler') do
    @start_time = now_in_millis

    @worker_loop.run(@interval) do
      NewRelic::Agent.instance.stats_engine.
        record_supportability_metrics_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_metrics_count(@failure_count, "ThreadProfiler/BacktraceFailures")
  end
end

#stopObject



164
165
166
167
168
# File 'lib/new_relic/agent/thread_profiler.rb', line 164

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

#to_collector_array(encoder) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/new_relic/agent/thread_profiler.rb', line 207

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