Project

General

Profile

Bug #1152 » 1152-2009-03-09.patch

latest patch - jabley (James Abley), 03/16/2009 09:37 AM

View differences:

lib/profiler.rb (working copy)
module Profiler__
# internal values
@@start = @@stack = @@map = nil
@@start = nil # the start time that profiling began
@@stacks = nil # the map of stack arrays keyed by thread
@@maps = nil # the map of map of call data keyed by thread, then by class and id. Call data arrays contains the call count, total time,
PROFILE_PROC = proc{|event, file, line, id, binding, klass|
case event
when "call", "c-call"
now = Process.times[0]
@@stack.push [now, 0.0]
stack = (@@stacks[Thread.current] ||= [])
stack.push [now, 0.0]
when "return", "c-return"
now = Process.times[0]
key = [klass, id]
if tick = @@stack.pop
data = (@@map[key] ||= [0, 0.0, 0.0, key])
stack = @@stacks[Thread.current]
if tick = stack.pop
threadMap = (@@maps[Thread.current] ||= {})
data = (threadMap[key] ||= [0, 0.0, 0.0, key])
data[0] += 1
cost = now - tick[0]
data[1] += cost
data[2] += cost - tick[1]
@@stack[-1][1] += cost if @@stack[-1]
stack[-1][1] += cost if stack[-1]
end
end
}
module_function
def start_profile
@@start = Process.times[0]
@@stack = []
@@map = {}
@@stacks = {}
@@maps = {}
set_trace_func PROFILE_PROC
end
def stop_profile
......
stop_profile
total = Process.times[0] - @@start
if total == 0 then total = 0.01 end
data = @@map.values
totals = {}
@@maps.each { |thread, threadMap|
threadMap.each {|key, data|
total_data = (totals[key] ||= [0, 0.0, 0.0, key])
total_data[0] += data[0]
total_data[1] += data[1] #TODO summing this field doesn't make much sense? Needs discussion on ruby-core
total_data[2] += data[2]
}
# Print the thread output
print_thread(f, thread, threadMap.values, total) if @@maps.length > 1
}
data = totals.values
data = data.sort_by{|x| -x[2]}
sum = 0
f.printf "Summary\n" if @@maps.length > 1
print_header(f)
print_call_sites(f, data, total)
print_footer(f, total)
end
def get_name(klass, id)
name = klass.to_s || ""
if klass.kind_of? Class
name += "#"
else
name += "."
end
name + id.id2name
end
def print_thread(f, thread, data, total)
data = data.sort_by{|x| -x[2]}
f.printf " %s\n", thread
print_header(f)
print_call_sites(f, data, total)
print_footer(f, total)
f.printf "\n"
end
def print_header(f)
f.printf " %% cumulative self self total\n"
f.printf " time seconds seconds calls ms/call ms/call name\n"
end
def print_call_sites(f, data, total)
sum = 0
for d in data
sum += d[2]
f.printf "%6.2f %8.2f %8.2f %8d ", d[2]/total*100, sum, d[2], d[0]
f.printf "%8.2f %8.2f %s\n", d[2]*1000/d[0], d[1]*1000/d[0], get_name(*d[3])
end
end
def print_footer(f, total)
f.printf "%6.2f %8.2f %8.2f %8d ", 0.0, total, 0.0, 1 # ???
f.printf "%8.2f %8.2f %s\n", 0.0, total*1000, "#toplevel" # ???
end
def get_name(klass, id)
name = klass.to_s || ""
if klass.kind_of? Class
name += "#"
else
name += "."
end
name + id.id2name
end
private :get_name
private :print_thread
private :print_header
private :print_call_sites
private :print_footer
end
(2-2/2)