Class: Scrap::Middleware
- Inherits:
-
Object
- Object
- Scrap::Middleware
- Defined in:
- lib/scrap.rb
Constant Summary collapse
- COMMIFY_REGEX =
/(\d)(?=(\d\d\d)+(?!\d))/
- CRLF =
"\r\n"
- @@gc_stats =
{}
- @@last_gc_run =
nil
- @@last_gc_mem =
nil
- @@requests_processed =
0
- @@request_list =
[]
- @@alive_at =
nil
- @@gc_stats_enabled =
nil
- @@config =
nil
Class Method Summary collapse
- .add_os(c, s, options = {}) ⇒ Object
- .call(env) ⇒ Object
- .commify(i) ⇒ Object
- .config ⇒ Object
- .gc_stats ⇒ Object
- .get_usage ⇒ Object
- .memcheck(top, klass = Object, mode = :normal) ⇒ Object
- .readable_gc_stat ⇒ Object
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app) ⇒ Middleware
constructor
A new instance of Middleware.
Constructor Details
#initialize(app) ⇒ Middleware
Returns a new instance of Middleware.
8 9 10 |
# File 'lib/scrap.rb', line 8 def initialize(app) @@app = app end |
Class Method Details
.add_os(c, s, options = {}) ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/scrap.rb', line 146 def self.add_os(c, s, = {}) print_objects = ["print_objects"] small = ["small"] min = ["min"] show_fields = ["show_fields"] ct = ObjectSpace.each_object(c) {} return if min and ct < min if small s << "#{c} (#{ct})<br />" else s << "<h3>#{c} (#{ct})</h3>" end return if !print_objects or ct == 0 s << CRLF s << '<table>' val = ObjectSpace.each_object(c) do |m| s << '<tr><td class="t">' << "<#{m.class.to_s}:#{sprintf("0x%.8x", m.object_id)}></td>" if show_fields then show_fields.each do |field| v = m.attributes[field.to_s] if v.blank? s << '<td> </td>' else s << "<td>#{field}: #{v}</td>" end end end s << '</tr>' end s << '</table>' << CRLF end |
.call(env) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/scrap.rb', line 34 def self.call(env) if !@@gc_stats_enabled GC.enable_stats if GC.respond_to? :enable_stats # for REE GC::Profiler.enable if defined? GC::Profiler # for 1.9.3+ @@gc_stats_enabled = true end @@requests_processed += 1 @@last_gc_run ||= @@alive_at ||= Time.now.to_f @@last_gc_mem ||= get_usage req = sprintf("<p>vsize:[%-10.2fMB] rss:[%-10.2fMB] %s %s</p>", get_usage[:virtual], get_usage[:real], env["REQUEST_METHOD"], env["PATH_INFO"]) req << "<pre>#{ObjectSpace.statistics}</pre>" if ObjectSpace.respond_to? :statistics req << "<pre>#{readable_gc_stat}</pre>" if GC.respond_to? :stat @@request_list.unshift req @@request_list.pop if @@request_list.length > (config["max_requests"] || 150) if env["PATH_INFO"] == "/stats/scrap" gc_stats else @@app.call(env) end end |
.commify(i) ⇒ Object
210 211 212 |
# File 'lib/scrap.rb', line 210 def self.commify(i) i.to_s.gsub(COMMIFY_REGEX, "\\1,") end |
.config ⇒ Object
25 26 27 28 29 30 31 32 |
# File 'lib/scrap.rb', line 25 def self.config @@config ||= YAML::load open(File.join(Rails.root, "config", "scrap.yml")).read rescue Errno::ENOENT @@config = {} rescue puts "[scrap] scrap.yml: #{$!.}" @@config = {} end |
.gc_stats ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/scrap.rb', line 57 def self.gc_stats collected = nil puts "Respond to? #{ObjectSpace.respond_to? :live_objects}" if ObjectSpace.respond_to? :live_objects live = ObjectSpace.live_objects GC.start collected = live - ObjectSpace.live_objects else GC.start end usage = get_usage virtual_mem_delta = usage[:virtual] - @@last_gc_mem[:virtual] real_mem_delta = usage[:real] - @@last_gc_mem[:real] time_delta = Time.now.to_f - @@last_gc_run s = '' s << '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"' << CRLF s << ' "http://www.w3.org/TR/html4/strict.dtd">' << CRLF s << '<html><head>' << CRLF s << "<title>[#{$$}] Garbage Report</title>" << CRLF s << '<style type="text/css"> body { font-family: monospace; color: #222; } td { border-bottom: 1px solid #eee; padding: 1px 9px; } td.t { background: #fafafa; } tr:hover td { background: #fafaf0; border-color: #e0e0dd; } h1,h2,h3 { border-bottom: 1px solid #ddd; font-family: sans-serif; } </style>' << CRLF s << '<body>' << CRLF s << "<h1>Scrap - PID #{$$}</h1>" << CRLF s << '<table>' << CRLF s << sprintf('<tr><td class="t">Virtual Memory usage:</td><td>%2.2fMB</td></tr>', usage[:virtual]) s << sprintf('<tr><td class="t">Real Memory usage:</td><td>%2.2fMB</td></tr>', usage[:real]) s << sprintf('<tr><td class="t">Vsize Delta:</td><td>%2.2fMB</td></tr>', virtual_mem_delta) s << sprintf('<tr><td class="t">RSS Delta:</td><td>%2.2fMB</td></tr>', real_mem_delta) s << sprintf('<tr><td class="t">Last Scrap req:</td><td>%2.2f seconds ago</td></tr>', time_delta) s << sprintf('<tr><td class="t">Requests processed:</td><td>%s</td></tr>', @@requests_processed) s << sprintf('<tr><td class="t">Alive for:</td><td>%2.2f seconds</td></tr>', Time.now.to_f - @@alive_at) if GC.respond_to? :time s << sprintf('<tr><td class="t">Total time spent in GC:</td><td>%2.2f seconds</td></tr>', GC.time / 1000000.0) elsif defined? GC::Profiler s << sprintf('<tr><td class="t">Total time spent in GC:</td><td>%2.2f seconds</td></tr>', GC::Profiler.total_time / 1000.0) end if collected s << sprintf('<tr><td class="t">Collected objects:</td><td>%2d</td></tr>', collected) s << sprintf('<tr><td class="t">Live objects:</td><td>%2d</td></tr>', ObjectSpace.live_objects) end s << '</table>' << CRLF s << "<h3>Top #{config["max_objects"]} deltas since last request</h3>" s << '<table border="0">' memcheck(config["max_objects"], Object, :deltas).each do |v| next if v.last == 0 s << "<tr><td class='t'>#{v.first}</td><td>#{sprintf("%s%s", v.last >= 0 ? "+" : "-", commify(v.last))}</td></tr>" end s << '</table>' s << "<h3>Top #{config["max_objects"]} objects</h3>" s << '<table border="0">' memcheck(config["max_objects"]).each do |v| s << "<tr><td class='t'>#{v.first}</td><td>#{commify v.last}</td></tr>" end s << '</table>' (config["classes"] || {}).each do |klass, val| puts val.inspect opts = val === true ? {"print_objects" => true} : val add_os(klass.constantize, s, opts) end s << '<h3>Request history</h3>' @@request_list.each do |req| s << req end s << '</body></html>' @@last_gc_run = Time.now.to_f @@last_gc_mem = usage @@requests_processed = 0 [200, {"Content-Type" => "text/html"}, [s]] end |
.get_usage ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/scrap.rb', line 134 def self.get_usage usage = Hash.new("N/A") begin stat = `cat /proc/#{$$}/stat`.split(" ") usage[:virtual] = stat[22].to_i / (1024 * 1024).to_f usage[:real] = stat[23].to_i * (`getconf PAGESIZE`.to_f) / (1024 * 1024).to_f rescue # pass end return usage end |
.memcheck(top, klass = Object, mode = :normal) ⇒ Object
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/scrap.rb', line 181 def self.memcheck(top, klass = Object, mode = :normal) top ||= 50 os = Hash.new(0) ObjectSpace.each_object(klass) do |o| begin; # If this is true, it's an association proxy, and we don't want to invoke method_missing on it, # as it will result in a load of the association proxy from the DB, doing extra DB work and # potentially creating a lot of AR objects. Hackalicious. next if o.respond_to? :proxy_respond_to? os[o.class.to_s] += 1 if o.respond_to? :class rescue; end end if mode == :deltas then os2 = Hash.new(0) os.each do |k, v| os2[k] = v - (@@gc_stats[k] || 0) @@gc_stats[k] = v end sorted = os2.sort_by{|k,v| -v }.first(top) else sorted = os.sort_by{|k,v| -v }.first(top) end end |
.readable_gc_stat ⇒ Object
205 206 207 208 |
# File 'lib/scrap.rb', line 205 def self.readable_gc_stat stat = GC.stat "GC cycles so far: #{stat[:count]}\nNumber of heaps : #{stat[:heap_used]}\nNumber of objects : #{ObjectSpace.count_objects[:TOTAL]}\nHeap length : #{stat[:heap_length]}\nHeap increment : #{stat[:heap_increment]}\nHeap live num : #{stat[:heap_live_num]}\nHeap free num : #{stat[:heap_free_num]}\nHeap final num : #{stat[:heap_final_num]}\n" end |
Instance Method Details
#call(env) ⇒ Object
12 13 14 |
# File 'lib/scrap.rb', line 12 def call(env) self.class.call(env) end |