Class: HeapScope::Flamegraph
- Inherits:
-
Object
- Object
- HeapScope::Flamegraph
- Defined in:
- lib/heapscope/flamegraph.rb
Overview
Turns snapshot allocation sites into folded stacks or Speedscope JSON.
Folded output is compatible with inferno / flamegraph.pl. Speedscope JSON can be dropped onto https://www.speedscope.app (local file, no upload required by HeapScope itself).
Defined Under Namespace
Classes: Frame
Constant Summary collapse
- FORMATS =
%i[folded speedscope].freeze
Instance Attribute Summary collapse
-
#frames ⇒ Object
readonly
Returns the value of attribute frames.
-
#snapshot_id ⇒ Object
readonly
Returns the value of attribute snapshot_id.
-
#unit ⇒ Object
readonly
Returns the value of attribute unit.
Class Method Summary collapse
Instance Method Summary collapse
- #empty? ⇒ Boolean
-
#initialize(frames:, snapshot_id: nil, unit: :count) ⇒ Flamegraph
constructor
A new instance of Flamegraph.
- #render(format: :folded) ⇒ Object
- #to_folded ⇒ Object
- #to_h ⇒ Object
- #to_speedscope ⇒ Object
- #total ⇒ Object
Constructor Details
#initialize(frames:, snapshot_id: nil, unit: :count) ⇒ Flamegraph
Returns a new instance of Flamegraph.
16 17 18 19 20 |
# File 'lib/heapscope/flamegraph.rb', line 16 def initialize(frames:, snapshot_id: nil, unit: :count) @frames = frames @snapshot_id = snapshot_id @unit = unit end |
Instance Attribute Details
#frames ⇒ Object (readonly)
Returns the value of attribute frames.
14 15 16 |
# File 'lib/heapscope/flamegraph.rb', line 14 def frames @frames end |
#snapshot_id ⇒ Object (readonly)
Returns the value of attribute snapshot_id.
14 15 16 |
# File 'lib/heapscope/flamegraph.rb', line 14 def snapshot_id @snapshot_id end |
#unit ⇒ Object (readonly)
Returns the value of attribute unit.
14 15 16 |
# File 'lib/heapscope/flamegraph.rb', line 14 def unit @unit end |
Class Method Details
.from_snapshot(snapshot, unit: :count) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/heapscope/flamegraph.rb', line 22 def self.from_snapshot(snapshot, unit: :count) raise ArgumentError, "unit must be :count or :bytes" unless %i[count bytes].include?(unit.to_sym) snapshot = Snapshot.load(snapshot) if snapshot.is_a?(String) frames = [] snapshot.allocation_sites.each do |site_key, stats| stats = symbolize(stats) site = stats[:site] || site_key.to_s method_id = stats[:method_id] classes = stats[:classes] || {} if classes.empty? frames << Frame.new( site: site, method_id: method_id, klass: nil, allocations: stats[:count].to_i, bytes: stats[:shallow_bytes].to_i ) else classes.each do |klass, class_count| share = stats[:count].to_i.positive? ? (class_count.to_f / stats[:count]) : 0.0 frames << Frame.new( site: site, method_id: method_id, klass: klass.to_s, allocations: class_count.to_i, bytes: (stats[:shallow_bytes].to_i * share).round ) end end end new(frames: frames.sort_by { |frame| [-value_for(frame, unit), frame.site.to_s] }, snapshot_id: snapshot.id, unit: unit.to_sym) end |
Instance Method Details
#empty? ⇒ Boolean
57 58 59 |
# File 'lib/heapscope/flamegraph.rb', line 57 def empty? frames.empty? || total.zero? end |
#render(format: :folded) ⇒ Object
115 116 117 118 119 120 |
# File 'lib/heapscope/flamegraph.rb', line 115 def render(format: :folded) format = format.to_sym raise ArgumentError, "unknown flamegraph format #{format}" unless FORMATS.include?(format) format == :speedscope ? JSON.pretty_generate(to_speedscope) : to_folded end |
#to_folded ⇒ Object
65 66 67 68 69 70 71 72 |
# File 'lib/heapscope/flamegraph.rb', line 65 def to_folded frames.filter_map do |frame| weight = value_for(frame, unit) next if weight <= 0 "#{stack_for(frame)} #{weight}" end.join("\n") end |
#to_h ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/heapscope/flamegraph.rb', line 122 def to_h { snapshot_id: snapshot_id, unit: unit, total: total, frames: frames.map do |frame| { site: frame.site, method_id: frame.method_id, class: frame.klass, count: frame.allocations, bytes: frame.bytes, stack: stack_for(frame) } end } end |
#to_speedscope ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/heapscope/flamegraph.rb', line 74 def to_speedscope names = [] name_index = {} samples = [] weights = [] frames.each do |frame| weight = value_for(frame, unit) next if weight <= 0 stack = [] stack_for(frame).split(";").each do |name| unless name_index.key?(name) name_index[name] = names.length names << name end stack << name_index[name] end samples << stack weights << weight end { "$schema" => "https://www.speedscope.app/file-format-schema.json", shared: { frames: names.map { |name| { name: name } } }, profiles: [ { type: "sampled", name: "HeapScope allocations (#{unit})", unit: unit == :bytes ? "bytes" : "none", startValue: 0, endValue: weights.sum, samples: samples, weights: weights } ], exporter: "heapscope #{VERSION}", name: snapshot_id && "heapscope-#{snapshot_id}" }.compact end |
#total ⇒ Object
61 62 63 |
# File 'lib/heapscope/flamegraph.rb', line 61 def total frames.sum { |frame| value_for(frame, unit) } end |