Project

General

Profile

Bug #1152 » 1152.patch

Sample patch for discussion - jabley (James Abley), 03/16/2009 09:29 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 stacks keyed by thread
@@maps = nil # the map of call data keyed by thread, class and id. Call data 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.values.each { |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?
total_data[2] += data[2]
}
}
# Maybe we should show a per thread output and a totals view?
data = totals.values
data = data.sort_by{|x| -x[2]}
sum = 0
f.printf " %% cumulative self self total\n"
(1-1/2)