Class: StackProf::Report
- Inherits:
-
Object
- Object
- StackProf::Report
- Defined in:
- lib/stackprof/report.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
Instance Method Summary collapse
- #add_lines(a, b) ⇒ Object
- #files ⇒ Object
- #frames(sort_by_total = false) ⇒ Object
-
#initialize(data) ⇒ Report
constructor
A new instance of Report.
- #max_samples ⇒ Object
- #modeline ⇒ Object
- #normalized_frames ⇒ Object
- #overall_samples ⇒ Object
- #print_callgrind(f = STDOUT) ⇒ Object
- #print_debug ⇒ Object
- #print_file(filter, f = STDOUT) ⇒ Object
- #print_files(sort_by_total = false, limit = nil, f = STDOUT) ⇒ Object
- #print_graphviz(filter = nil, f = STDOUT) ⇒ Object
- #print_method(name, f = STDOUT) ⇒ Object
- #print_text(sort_by_total = false, limit = nil, f = STDOUT) ⇒ Object
- #version ⇒ Object
Constructor Details
#initialize(data) ⇒ Report
6 7 8 9 10 11 12 |
# File 'lib/stackprof/report.rb', line 6 def initialize(data) @data = data frames = {} @data[:frames].each{ |k,v| frames[k.to_s] = v } @data[:frames] = frames end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
13 14 15 |
# File 'lib/stackprof/report.rb', line 13 def data @data end |
Instance Method Details
#add_lines(a, b) ⇒ Object
59 60 61 62 63 64 |
# File 'lib/stackprof/report.rb', line 59 def add_lines(a, b) return b if a.nil? return a+b if a.is_a? Fixnum return [ a[0], a[1]+b ] if b.is_a? Fixnum [ a[0]+b[0], a[1]+b[1] ] end |
#files ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/stackprof/report.rb', line 47 def files @data[:files] ||= @data[:frames].inject(Hash.new) do |hash, (addr, frame)| if file = frame[:file] and lines = frame[:lines] hash[file] ||= Hash.new lines.each do |line, weight| hash[file][line] = add_lines(hash[file][line], weight) end end hash end end |
#frames(sort_by_total = false) ⇒ Object
15 16 17 |
# File 'lib/stackprof/report.rb', line 15 def frames(sort_by_total=false) Hash[ *@data[:frames].sort_by{ |iseq, stats| -stats[sort_by_total ? :total_samples : :samples] }.flatten(1) ] end |
#max_samples ⇒ Object
43 44 45 |
# File 'lib/stackprof/report.rb', line 43 def max_samples @data[:max_samples] ||= frames.max_by{ |addr, frame| frame[:samples] }.last[:samples] end |
#modeline ⇒ Object
35 36 37 |
# File 'lib/stackprof/report.rb', line 35 def modeline "#{@data[:mode]}(#{@data[:interval]})" end |
#normalized_frames ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/stackprof/report.rb', line 19 def normalized_frames id2hash = {} @data[:frames].each do |frame, info| id2hash[frame.to_s] = info[:hash] = Digest::MD5.hexdigest("#{info[:name]}#{info[:file]}#{info[:line]}") end @data[:frames].inject(Hash.new) do |hash, (frame, info)| info = hash[id2hash[frame.to_s]] = info.dup info[:edges] = info[:edges].inject(Hash.new){ |edges, (edge, weight)| edges[id2hash[edge.to_s]] = weight; edges } if info[:edges] hash end end |
#overall_samples ⇒ Object
39 40 41 |
# File 'lib/stackprof/report.rb', line 39 def overall_samples @data[:samples] end |
#print_callgrind(f = STDOUT) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/stackprof/report.rb', line 125 def print_callgrind(f = STDOUT) f.puts "version: 1" f.puts "creator: stackprof" f.puts "pid: 0" f.puts "cmd: ruby" f.puts "part: 1" f.puts "desc: mode: #{modeline}" f.puts "desc: missed: #{@data[:missed_samples]})" f.puts "positions: line" f.puts "events: Instructions" f.puts "summary: #{@data[:samples]}" list = frames list.each do |addr, frame| f.puts "fl=#{frame[:file]}" f.puts "fn=#{frame[:name]}" frame[:lines].each do |line, weight| f.puts "#{line} #{weight.is_a?(Array) ? weight[1] : weight}" end if frame[:lines] frame[:edges].each do |edge, weight| oframe = list[edge.to_s] f.puts "cfl=#{oframe[:file]}" unless oframe[:file] == frame[:file] f.puts "cfn=#{oframe[:name]}" f.puts "calls=#{weight} #{frame[:line] || 0}\n#{oframe[:line] || 0} #{weight}" end if frame[:edges] f.puts end f.puts "totals: #{@data[:samples]}" end |
#print_debug ⇒ Object
66 67 68 |
# File 'lib/stackprof/report.rb', line 66 def print_debug pp @data end |
#print_file(filter, f = STDOUT) ⇒ Object
199 200 201 202 203 204 205 206 |
# File 'lib/stackprof/report.rb', line 199 def print_file(filter, f = STDOUT) filter = /#{Regexp.escape filter}/ unless Regexp === filter list = files list.select!{ |name, lines| name =~ filter } list.sort_by{ |file, vals| -vals.values.inject(0){ |sum, n| sum + (n.is_a?(Array) ? n[1] : n) } }.each do |file, lines| source_display(f, file, lines) end end |
#print_files(sort_by_total = false, limit = nil, f = STDOUT) ⇒ Object
189 190 191 192 193 194 195 196 197 |
# File 'lib/stackprof/report.rb', line 189 def print_files(sort_by_total=false, limit=nil, f = STDOUT) list = files.map{ |file, vals| [file, vals.values.inject([0,0]){ |sum, n| add_lines(sum, n) }] } list = list.sort_by{ |file, samples| -samples[1] } list = list.first(limit) if limit list.each do |file, vals| total_samples, samples = *vals f.printf "% 5d (%2.1f%%) / % 5d (%2.1f%%) %s\n", total_samples, (100.0*total_samples/overall_samples), samples, (100.0*samples/overall_samples), file end end |
#print_graphviz(filter = nil, f = STDOUT) ⇒ Object
70 71 72 73 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 |
# File 'lib/stackprof/report.rb', line 70 def print_graphviz(filter = nil, f = STDOUT) if filter mark_stack = [] list = frames list.each{ |addr, frame| mark_stack << addr if frame[:name] =~ filter } while addr = mark_stack.pop frame = list[addr] unless frame[:marked] $stderr.puts frame[:edges].inspect mark_stack += frame[:edges].map{ |addr, weight| addr.to_s if list[addr.to_s][:total_samples] <= weight*1.2 }.compact if frame[:edges] frame[:marked] = true end end list = list.select{ |addr, frame| frame[:marked] } list.each{ |addr, frame| frame[:edges] && frame[:edges].delete_if{ |k,v| list[k.to_s].nil? } } list else list = frames end f.puts "digraph profile {" list.each do |frame, info| call, total = info.values_at(:samples, :total_samples) sample = '' sample << "#{call} (%2.1f%%)\\rof " % (call*100.0/overall_samples) if call < total sample << "#{total} (%2.1f%%)\\r" % (total*100.0/overall_samples) fontsize = (1.0 * call / max_samples) * 28 + 10 size = (1.0 * total / overall_samples) * 2.0 + 0.5 f.puts " #{frame} [size=#{size}] [fontsize=#{fontsize}] [penwidth=\"#{size}\"] [shape=box] [label=\"#{info[:name]}\\n#{sample}\"];" if edges = info[:edges] edges.each do |edge, weight| size = (1.0 * weight / overall_samples) * 2.0 + 0.5 f.puts " #{frame} -> #{edge} [label=\"#{weight}\"] [weight=\"#{weight}\"] [penwidth=\"#{size}\"];" end end end f.puts "}" end |
#print_method(name, f = STDOUT) ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/stackprof/report.rb', line 156 def print_method(name, f = STDOUT) name = /#{Regexp.escape name}/ unless Regexp === name frames.each do |frame, info| next unless info[:name] =~ name file, line = info.values_at(:file, :line) line ||= 1 lines = info[:lines] maxline = lines ? lines.keys.max : line + 5 f.printf "%s (%s:%d)\n", info[:name], file, line f.printf " samples: % 5d self (%2.1f%%) / % 5d total (%2.1f%%)\n", info[:samples], 100.0*info[:samples]/overall_samples, info[:total_samples], 100.0*info[:total_samples]/overall_samples if (callers = data[:frames].map{ |id, other| [other[:name], other[:edges][frame.to_i]] if other[:edges] && other[:edges].include?(frame.to_i) }.compact).any? f.puts " callers:" callers = callers.sort_by(&:last).reverse callers.each do |name, weight| f.printf " % 5d (% 8s) %s\n", weight, "%3.1f%%" % (100.0*weight/info[:total_samples]), name end end if callees = info[:edges] f.printf " callees (%d total):\n", info[:total_samples]-info[:samples] callees = callees.map{ |k, weight| [data[:frames][k.to_s][:name], weight] } callees.each do |name, weight| f.printf " % 5d (% 8s) %s\n", weight, "%3.1f%%" % (100.0*weight/(info[:total_samples]-info[:samples])), name end end f.puts " code:" source_display(f, file, lines, line-1..maxline) end end |
#print_text(sort_by_total = false, limit = nil, f = STDOUT) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/stackprof/report.rb', line 110 def print_text(sort_by_total=false, limit=nil, f = STDOUT) f.puts "==================================" f.printf " Mode: #{modeline}\n" f.printf " Samples: #{@data[:samples]} (%.2f%% miss rate)\n", 100.0*@data[:missed_samples]/(@data[:missed_samples]+@data[:samples]) f.printf " GC: #{@data[:gc_samples]} (%.2f%%)\n", 100.0*@data[:gc_samples]/@data[:samples] f.puts "==================================" f.printf "% 10s (pct) % 10s (pct) FRAME\n" % ["TOTAL", "SAMPLES"] list = frames(sort_by_total) list = list.first(limit) if limit list.each do |frame, info| call, total = info.values_at(:samples, :total_samples) f.printf "% 10d % 8s % 10d % 8s %s\n", total, "(%2.1f%%)" % (total*100.0/overall_samples), call, "(%2.1f%%)" % (call*100.0/overall_samples), info[:name] end end |
#version ⇒ Object
31 32 33 |
# File 'lib/stackprof/report.rb', line 31 def version @data[:version] end |