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



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



61
62
63
# File 'lib/rack/allocation_tracer.rb', line 61

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
42
43
44
45
46
47
48
49
50
51
52
53
54
# 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}",
      Rack::Utils.escape_html(klass ? klass.name : '<internal>'),
      count, oldcount, total_age / Float(count), min_age, max_age, memsize]
  }

  begin
    table = table.sort_by{|vs|
      ary = @sort_order.map{|i| Numeric === vs[i] ? -vs[i] : vs[i]}
    }
  rescue
    ts = []
    table.each{|*cols|
      cols.each.with_index{|c, i|
        h = (ts[i] ||= Hash.new(0))
        h[c.class] += 1
      }
    }
    return "<pre>Sorting error\n" + Rack::Utils.escape_html(h.inspect) + "</pre>"
  end

  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



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
# File 'lib/rack/allocation_tracer.rb', line 83

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



56
57
58
59
# File 'lib/rack/allocation_tracer.rb', line 56

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



65
66
67
# File 'lib/rack/allocation_tracer.rb', line 65

def freed_count_table_page
  count_table_page ObjectSpace::AllocationTracer.freed_count_table
end

#lifetime_table_pageObject



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/rack/allocation_tracer.rb', line 69

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