Class: Rack::AllocationTracerMiddleware::Tracer

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/allocation_tracer.rb

Direct Known Subclasses

TotalTracer

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Tracer

Returns a new instance of Tracer.



14
15
16
17
# File 'lib/rack/allocation_tracer.rb', line 14

def initialize app
  @app = app
  @sort_order = (0..7).to_a
end

Instance Method Details

#allocated_count_table_pageObject



48
49
50
# File 'lib/rack/allocation_tracer.rb', line 48

def allocated_count_table_page
  count_table_page ObjectSpace::AllocationTracer.allocated_count_table
end

#allocation_trace_page(result, env) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rack/allocation_tracer.rb', line 19

def allocation_trace_page result, env
  if /\As=(\d+)/ =~ env["QUERY_STRING"]
    top = $1.to_i
    @sort_order.unshift top if @sort_order.delete top
  end

  table = result.map{|(file, line, klass), (count, oldcount, total_age, min_age, max_age, memsize)|
    ["#{Rack::Utils.escape_html(file)}:#{'%04d' % line}",
     klass ? klass.name : '<internal>',
     count, oldcount, total_age / Float(count), min_age, max_age, memsize]
  }.sort_by{|vs|
    ary = @sort_order.map{|i| Numeric === vs[i] ? -vs[i] : vs[i]}
  }

  headers = %w(path class count old_count average_age min_age max_age memsize).map.with_index{|e, i|
    "<th><a href='./?s=#{i}'>#{e}</a></th>"
  }.join("\n")
  header = "<tr>#{headers}</tr>"
  body = table.map{|cols|
    "<tr>" + cols.map{|c| "<td>#{c}</td>"}.join("\n") + "</tr>"
  }.join("\n")
  "<table>#{header}#{body}</table>"
end

#call(env) ⇒ 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
# File 'lib/rack/allocation_tracer.rb', line 70

def call env
  if /\A\/allocation_tracer\// =~ env["PATH_INFO"]
    result = ObjectSpace::AllocationTracer.result
    ObjectSpace::AllocationTracer.pause

    begin
      html = case env["PATH_INFO"]
             when /lifetime_table/
               lifetime_table_page
             when /allocated_count_table/
               allocated_count_table_page
             when /freed_count_table/
               freed_count_table_page
             else
               allocation_trace_page result, env
             end
      #
      [200, {"Content-Type" => "text/html"}, [html]]
    ensure
      ObjectSpace::AllocationTracer.resume
    end
  else
    @app.call env
  end
end

#count_table_page(count_table) ⇒ Object



43
44
45
46
# File 'lib/rack/allocation_tracer.rb', line 43

def count_table_page count_table
  text = count_table.map{|k, v| "%-10s\t%8d" % [k, v]}.join("\n")
  "<pre>#{text}</pre>"
end

#freed_count_table_pageObject



52
53
54
# File 'lib/rack/allocation_tracer.rb', line 52

def freed_count_table_page
  count_table_page ObjectSpace::AllocationTracer.freed_count_table
end

#lifetime_table_pageObject



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rack/allocation_tracer.rb', line 56

def lifetime_table_page
  table = []
  max_age = 0
  ObjectSpace::AllocationTracer.lifetime_table.each{|type, ages|
    max_age = [max_age, ages.size - 1].max
    table << [type, *ages]
  }
  headers = ['type', *(0..max_age)].map{|e| "<th>#{e}</th>"}.join("\n")
  body =  table.map{|cols|
    "<tr>" + cols.map{|c| "<td>#{c}</td>"}.join("\n") + "</tr>"
  }.join("\n")
  "<table border='1'><tr>#{headers}</tr>\n#{body}</table>"
end