class RubyProf::CallTreePrinter
Generates profiling information in callgrind format for use by kcachegrind and similar tools.
Public Class Methods
needs_dir?()
click to toggle source
# File lib/ruby-prof/printers/call_tree_printer.rb 97 def self.needs_dir? 98 true 99 end
Public Instance Methods
calltree_name(method_info)
click to toggle source
# File lib/ruby-prof/printers/call_tree_printer.rb 12 def calltree_name(method_info) 13 klass_path = method_info.klass_name.gsub("::", '/') 14 result = "#{klass_path}::#{method_info.method_name}" 15 16 case method_info.klass_flags 17 when 0x2 18 "#{result}^" 19 when 0x4 20 "#{result}^" 21 when 0x8 22 "#{result}*" 23 else 24 result 25 end 26 end
convert(value)
click to toggle source
# File lib/ruby-prof/printers/call_tree_printer.rb 76 def convert(value) 77 (value * @value_scale).round 78 end
determine_event_specification_and_value_scale()
click to toggle source
# File lib/ruby-prof/printers/call_tree_printer.rb 28 def determine_event_specification_and_value_scale 29 @event_specification = "events: " 30 case RubyProf.measure_mode 31 when RubyProf::PROCESS_TIME 32 @value_scale = RubyProf::CLOCKS_PER_SEC 33 @event_specification << 'process_time' 34 when RubyProf::WALL_TIME 35 @value_scale = 1_000_000 36 @event_specification << 'wall_time' 37 when RubyProf.const_defined?(:ALLOCATIONS) && RubyProf::ALLOCATIONS 38 @value_scale = 1 39 @event_specification << 'allocations' 40 when RubyProf.const_defined?(:MEMORY) && RubyProf::MEMORY 41 @value_scale = 1 42 @event_specification << 'memory' 43 when RubyProf.const_defined?(:GC_RUNS) && RubyProf::GC_RUNS 44 @value_scale = 1 45 @event_specification << 'gc_runs' 46 when RubyProf.const_defined?(:GC_TIME) && RubyProf::GC_TIME 47 @value_scale = 1000000 48 @event_specification << 'gc_time' 49 else 50 raise "Unknown measure mode: #{RubyProf.measure_mode}" 51 end 52 end
file(method)
click to toggle source
# File lib/ruby-prof/printers/call_tree_printer.rb 80 def file(method) 81 method.source_file ? File.expand_path(method.source_file) : '' 82 end
file_name_for_thread(thread)
click to toggle source
# File lib/ruby-prof/printers/call_tree_printer.rb 107 def file_name_for_thread(thread) 108 if thread.fiber_id == Fiber.current.object_id 109 ["callgrind.out", $$].join(".") 110 else 111 ["callgrind.out", $$, thread.fiber_id].join(".") 112 end 113 end
file_path_for_thread(thread)
click to toggle source
# File lib/ruby-prof/printers/call_tree_printer.rb 115 def file_path_for_thread(thread) 116 File.join(path, file_name_for_thread(thread)) 117 end
path()
click to toggle source
# File lib/ruby-prof/printers/call_tree_printer.rb 93 def path 94 @options[:path] || "." 95 end
print(options = {})
click to toggle source
# File lib/ruby-prof/printers/call_tree_printer.rb 54 def print(options = {}) 55 validate_print_params(options) 56 setup_options(options) 57 determine_event_specification_and_value_scale 58 print_threads 59 end
print_headers(output, thread)
click to toggle source
# File lib/ruby-prof/printers/call_tree_printer.rb 119 def print_headers(output, thread) 120 output << "#{@event_specification}\n\n" 121 # this doesn't work. kcachegrind does not fully support the spec. 122 # output << "thread: #{thread.id}\n\n" 123 end
print_method(output, method)
click to toggle source
# File lib/ruby-prof/printers/call_tree_printer.rb 125 def print_method(output, method) 126 # Print out the file and method name 127 output << "fl=#{file(method)}\n" 128 output << "fn=#{self.calltree_name(method)}\n" 129 130 # Now print out the function line number and its self time 131 output << "#{method.line} #{convert(method.self_time)}\n" 132 133 # Now print out all the children methods 134 method.call_trees.callees.each do |callee| 135 output << "cfl=#{file(callee.target)}\n" 136 output << "cfn=#{self.calltree_name(callee.target)}\n" 137 output << "calls=#{callee.called} #{callee.line}\n" 138 139 # Print out total times here! 140 output << "#{callee.line} #{convert(callee.total_time)}\n" 141 end 142 output << "\n" 143 end
print_thread(thread)
click to toggle source
# File lib/ruby-prof/printers/call_tree_printer.rb 84 def print_thread(thread) 85 File.open(file_path_for_thread(thread), "w") do |f| 86 print_headers(f, thread) 87 thread.methods.reverse_each do |method| 88 print_method(f, method) 89 end 90 end 91 end
print_threads()
click to toggle source
# File lib/ruby-prof/printers/call_tree_printer.rb 69 def print_threads 70 remove_subsidiary_files_from_previous_profile_runs 71 @result.threads.each do |thread| 72 print_thread(thread) 73 end 74 end
remove_subsidiary_files_from_previous_profile_runs()
click to toggle source
# File lib/ruby-prof/printers/call_tree_printer.rb 101 def remove_subsidiary_files_from_previous_profile_runs 102 pattern = ["callgrind.out", $$, "*"].join(".") 103 files = Dir.glob(File.join(path, pattern)) 104 FileUtils.rm_f(files) 105 end
validate_print_params(options)
click to toggle source
# File lib/ruby-prof/printers/call_tree_printer.rb 61 def validate_print_params(options) 62 if options.is_a?(IO) 63 raise ArgumentError, "#{self.class.name}#print cannot print to IO objects" 64 elsif !options.is_a?(Hash) 65 raise ArgumentError, "#{self.class.name}#print requires an options hash" 66 end 67 end