mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
profiler.rb: Wrapper for BasicObject
* lib/profiler.rb (Profiler__::Wrapper): support calling singleton methods of an instance of BasicObject. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@38575 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
ef6abd10c7
commit
c81a5012f1
2 changed files with 23 additions and 24 deletions
|
@ -1,4 +1,7 @@
|
||||||
Sun Dec 23 17:57:19 2012 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
Sun Dec 23 17:57:25 2012 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
|
* lib/profiler.rb (Profiler__::Wrapper): support calling singleton
|
||||||
|
methods of an instance of BasicObject.
|
||||||
|
|
||||||
* lib/profiler.rb (Profiler__::PROFILE_PROC): use TracePoint.
|
* lib/profiler.rb (Profiler__::PROFILE_PROC): use TracePoint.
|
||||||
|
|
||||||
|
|
|
@ -58,8 +58,22 @@
|
||||||
# 0.00 0.19 0.00 1 0.00 190.00 #toplevel
|
# 0.00 0.19 0.00 1 0.00 190.00 #toplevel
|
||||||
|
|
||||||
module Profiler__
|
module Profiler__
|
||||||
|
class Wrapper < Struct.new(:defined_class, :method_id, :hash) # :nodoc:
|
||||||
|
private :defined_class=, :method_id=, :hash=
|
||||||
|
|
||||||
|
def initialize(klass, mid)
|
||||||
|
super(klass, mid, nil)
|
||||||
|
self.hash = Struct.instance_method(:hash).bind(self).call
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_s
|
||||||
|
"#{defined_class.inspect}#".sub(/\A\#<Class:(.*)>#\z/, '\1.') << method_id.to_s
|
||||||
|
end
|
||||||
|
alias inspect to_s
|
||||||
|
end
|
||||||
|
|
||||||
# internal values
|
# internal values
|
||||||
@@start = @@stack = @@map = @@array = nil
|
@@start = @@stack = @@map = nil
|
||||||
PROFILE_PROC = TracePoint.new(:call, :c_call, :return, :c_return) {|tp|
|
PROFILE_PROC = TracePoint.new(:call, :c_call, :return, :c_return) {|tp|
|
||||||
case tp.event
|
case tp.event
|
||||||
when :call, :c_call
|
when :call, :c_call
|
||||||
|
@ -67,13 +81,9 @@ module Profiler__
|
||||||
@@stack.push [now, 0.0]
|
@@stack.push [now, 0.0]
|
||||||
when :return, :c_return
|
when :return, :c_return
|
||||||
now = Process.times[0]
|
now = Process.times[0]
|
||||||
key = [tp.defined_class, tp.method_id]
|
key = Wrapper.new(tp.defined_class, tp.method_id)
|
||||||
if tick = @@stack.pop
|
if tick = @@stack.pop
|
||||||
data = begin
|
data = (@@map[key] ||= [0, 0.0, 0.0, key])
|
||||||
@@map[key] ||= [0, 0.0, 0.0, key]
|
|
||||||
rescue NoMethodError
|
|
||||||
@@array.find{|i| i[3] == key} || (@@array << [0, 0.0, 0.0, key])[-1]
|
|
||||||
end
|
|
||||||
data[0] += 1
|
data[0] += 1
|
||||||
cost = now - tick[0]
|
cost = now - tick[0]
|
||||||
data[1] += cost
|
data[1] += cost
|
||||||
|
@ -87,7 +97,6 @@ module_function
|
||||||
@@start = Process.times[0]
|
@@start = Process.times[0]
|
||||||
@@stack = []
|
@@stack = []
|
||||||
@@map = {}
|
@@map = {}
|
||||||
@@array = []
|
|
||||||
PROFILE_PROC.enable
|
PROFILE_PROC.enable
|
||||||
end
|
end
|
||||||
def stop_profile
|
def stop_profile
|
||||||
|
@ -97,7 +106,7 @@ module_function
|
||||||
stop_profile
|
stop_profile
|
||||||
total = Process.times[0] - @@start
|
total = Process.times[0] - @@start
|
||||||
if total == 0 then total = 0.01 end
|
if total == 0 then total = 0.01 end
|
||||||
data = @@map.values + @@array
|
data = @@map.values
|
||||||
data = data.sort_by{|x| -x[2]}
|
data = data.sort_by{|x| -x[2]}
|
||||||
sum = 0
|
sum = 0
|
||||||
f.printf " %% cumulative self self total\n"
|
f.printf " %% cumulative self self total\n"
|
||||||
|
@ -105,22 +114,9 @@ module_function
|
||||||
for d in data
|
for d in data
|
||||||
sum += d[2]
|
sum += d[2]
|
||||||
f.printf "%6.2f %8.2f %8.2f %8d ", d[2]/total*100, sum, d[2], d[0]
|
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])
|
f.printf "%8.2f %8.2f %s\n", d[2]*1000/d[0], d[1]*1000/d[0], d[3]
|
||||||
end
|
end
|
||||||
f.printf "%6.2f %8.2f %8.2f %8d ", 0.0, total, 0.0, 1 # ???
|
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" # ???
|
f.printf "%8.2f %8.2f %s\n", 0.0, total*1000, "#toplevel" # ???
|
||||||
end
|
end
|
||||||
def get_name(klass, id)
|
|
||||||
name = klass.to_s || ""
|
|
||||||
if klass.kind_of? Class
|
|
||||||
name += "#"
|
|
||||||
else
|
|
||||||
name += "."
|
|
||||||
end
|
|
||||||
name + id.id2name
|
|
||||||
rescue NoMethodError => e
|
|
||||||
name = e.message.slice(/#<.*?:0x[0-9a-f]+>/) || ""
|
|
||||||
name + "." + id.id2name
|
|
||||||
end
|
|
||||||
private :get_name
|
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue