mirror of
https://github.com/puma/puma.git
synced 2022-11-09 13:48:40 -05:00
79be4b98e5
git-svn-id: svn+ssh://rubyforge.org/var/svn/mongrel/trunk@141 19e92222-5c0b-0410-8929-a290d50e31e9
173 lines
3.9 KiB
Ruby
173 lines
3.9 KiB
Ruby
require 'logger'
|
|
require 'set'
|
|
require 'socket'
|
|
|
|
$mongrel_debugging=true
|
|
|
|
module MongrelDbg
|
|
SETTINGS = { :tracing => {}}
|
|
LOGGING = { }
|
|
|
|
def MongrelDbg::configure(log_dir = "log/mongrel_debug")
|
|
Dir.mkdir(log_dir) if not File.exist?(log_dir)
|
|
@log_dir = log_dir
|
|
end
|
|
|
|
|
|
def MongrelDbg::trace(target, message)
|
|
if SETTINGS[:tracing][target] and LOGGING[target]
|
|
LOGGING[target].log(Logger::DEBUG, message)
|
|
end
|
|
end
|
|
|
|
def MongrelDbg::begin_trace(target)
|
|
SETTINGS[:tracing][target] = true
|
|
if not LOGGING[target]
|
|
LOGGING[target] = Logger.new(File.join(@log_dir, "#{target.to_s}.log"))
|
|
end
|
|
MongrelDbg::trace(target, "TRACING ON #{Time.now}")
|
|
end
|
|
|
|
def MongrelDbg::end_trace(target)
|
|
SETTINGS[:tracing][target] = false
|
|
MongrelDbg::trace(target, "TRACING OFF #{Time.now}")
|
|
LOGGING[target].close
|
|
LOGGING[target] = nil
|
|
end
|
|
|
|
def MongrelDbg::tracing?(target)
|
|
SETTINGS[:tracing][target]
|
|
end
|
|
end
|
|
|
|
|
|
module ObjectTracker
|
|
@active_objects = nil
|
|
|
|
def ObjectTracker.configure
|
|
@active_objects = Set.new
|
|
|
|
ObjectSpace.each_object do |obj|
|
|
@active_objects << obj.object_id
|
|
end
|
|
end
|
|
|
|
def ObjectTracker.sample
|
|
ospace = Set.new
|
|
counts = {}
|
|
|
|
# Strings can't be tracked easily and are so numerous that they drown out all else
|
|
# so we just ignore them in the counts.
|
|
ObjectSpace.each_object do |obj|
|
|
ospace << obj.object_id
|
|
counts[obj.class] ||= 0
|
|
counts[obj.class] += 1
|
|
end
|
|
|
|
dead_objects = @active_objects - ospace
|
|
new_objects = ospace - @active_objects
|
|
live_objects = ospace & @active_objects
|
|
|
|
MongrelDbg::trace(:objects, "COUNTS: #{dead_objects.length},#{new_objects.length},#{live_objects.length}")
|
|
|
|
if MongrelDbg::tracing? :objects
|
|
top_20 = counts.sort{|a,b| b[1] <=> a[1]}[0..20]
|
|
MongrelDbg::trace(:objects,"TOP 20: #{top_20.inspect}")
|
|
end
|
|
|
|
@active_objects = live_objects + new_objects
|
|
|
|
[@active_objects, top_20]
|
|
end
|
|
|
|
end
|
|
|
|
$open_files = {}
|
|
|
|
class IO
|
|
alias_method :orig_open, :open
|
|
alias_method :orig_close, :close
|
|
|
|
def open(*arg, &blk)
|
|
$open_files[self] = args.inspect
|
|
orig_open(*arg,&blk)
|
|
end
|
|
|
|
def close(*arg,&blk)
|
|
$open_files.delete self
|
|
orig_close(*arg,&blk)
|
|
end
|
|
end
|
|
|
|
|
|
module Kernel
|
|
alias_method :orig_open, :open
|
|
|
|
def open(*arg, &blk)
|
|
$open_files[self] = arg[0]
|
|
orig_open(*arg,&blk)
|
|
end
|
|
|
|
def log_open_files
|
|
open_counts = {}
|
|
$open_files.each do |f,args|
|
|
open_counts[args] ||= 0
|
|
open_counts[args] += 1
|
|
end
|
|
MongrelDbg::trace(:files, open_counts.to_yaml)
|
|
end
|
|
end
|
|
|
|
|
|
|
|
module RequestLog
|
|
|
|
# Just logs whatever requests it gets to STDERR (which ends up in the mongrel
|
|
# log when daemonized).
|
|
class Access < GemPlugin::Plugin "/handlers"
|
|
include Mongrel::HttpHandlerPlugin
|
|
|
|
def process(request,response)
|
|
p = request.params
|
|
STDERR.puts "#{p['REMOTE_ADDR']} - [#{Mongrel::HttpServer.httpdate(Time.now)}] \"#{p['REQUEST_METHOD']} #{p["REQUEST_URI"]} HTTP/1.1\""
|
|
end
|
|
end
|
|
|
|
|
|
class Files < GemPlugin::Plugin "/handlers"
|
|
include Mongrel::HttpHandlerPlugin
|
|
|
|
def process(request, response)
|
|
MongrelDbg::trace(:files, "#{Time.now} FILES OPEN BEFORE REQUEST #{request.params['PATH_INFO']}")
|
|
log_open_files
|
|
end
|
|
|
|
end
|
|
|
|
class Objects < GemPlugin::Plugin "/handlers"
|
|
include Mongrel::HttpHandlerPlugin
|
|
|
|
def process(request, response)
|
|
MongrelDbg::trace(:objects, "#{Time.now} OBJECT STATS BEFORE REQUEST #{request.params['PATH_INFO']}")
|
|
ObjectTracker.sample
|
|
end
|
|
|
|
end
|
|
|
|
|
|
class Params < GemPlugin::Plugin "/handlers"
|
|
include Mongrel::HttpHandlerPlugin
|
|
|
|
def process(request, response)
|
|
MongrelDbg::trace(:rails, "#{Time.now} REQUEST #{request.params['PATH_INFO']}")
|
|
MongrelDbg::trace(:rails, request.params.to_yaml)
|
|
end
|
|
|
|
end
|
|
end
|
|
|
|
|
|
END {
|
|
MongrelDbg::trace(:files, "FILES OPEN AT EXIT")
|
|
log_open_files
|
|
}
|