Class: RubyProf::CallTreePrinter

Inherits:
AbstractPrinter show all
Defined in:
lib/ruby-prof/printers/call_tree_printer.rb

Overview

Generate profiling information in callgrind format for use by kcachegrind and similar tools.

Note: when profiling for a callgrind printer, one should use the merge_fibers: true option when creating the profile. Otherwise each fiber would appear as a separate profile.

Instance Method Summary collapse

Methods inherited from AbstractPrinter

#initialize, #method_name, #min_percent, #print_file, #print_footer, #print_header, #setup_options, #sort_method

Constructor Details

This class inherits a constructor from RubyProf::AbstractPrinter

Instance Method Details

#base_nameObject



82
83
84
# File 'lib/ruby-prof/printers/call_tree_printer.rb', line 82

def base_name
  @options[:profile] || "profile"
end

#convert(value) ⇒ Object



61
62
63
# File 'lib/ruby-prof/printers/call_tree_printer.rb', line 61

def convert(value)
  (value * @value_scale).round
end

#determine_event_specification_and_value_scaleObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ruby-prof/printers/call_tree_printer.rb', line 17

def determine_event_specification_and_value_scale
  @event_specification = "events: "
  case @result.measure_modes.first
    when RubyProf::PROCESS_TIME
      @value_scale = RubyProf::CLOCKS_PER_SEC
      @event_specification << 'process_time'
    when RubyProf::WALL_TIME
      @value_scale = 1_000_000
      @event_specification << 'wall_time'
    when RubyProf.const_defined?(:CPU_TIME) && RubyProf::CPU_TIME
      @value_scale = RubyProf.cpu_frequency
      @event_specification << 'cpu_time'
    when RubyProf.const_defined?(:ALLOCATIONS) && RubyProf::ALLOCATIONS
      @value_scale = 1
      @event_specification << 'allocations'
    when RubyProf.const_defined?(:MEMORY) && RubyProf::MEMORY
      @value_scale = 1
      @event_specification << 'memory'
    when RubyProf.const_defined?(:GC_RUNS) && RubyProf::GC_RUNS
      @value_scale = 1
      @event_specification << 'gc_runs'
    when RubyProf.const_defined?(:GC_TIME) && RubyProf::GC_TIME
      @value_scale = 1000000
      @event_specification << 'gc_time'
    else
      raise "Unknown measure mode: #{RubyProf.measure_mode}"
  end
end

#file(method) ⇒ Object



65
66
67
# File 'lib/ruby-prof/printers/call_tree_printer.rb', line 65

def file(method)
  File.expand_path(method.source_file)
end

#file_name_for_thread(thread) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/ruby-prof/printers/call_tree_printer.rb', line 92

def file_name_for_thread(thread)
  if thread.fiber_id == Fiber.current.object_id
    [base_name, "callgrind.out", $$].join(".")
  else
    [base_name, "callgrind.out", $$, thread.fiber_id].join(".")
  end
end

#file_path_for_thread(thread) ⇒ Object



100
101
102
# File 'lib/ruby-prof/printers/call_tree_printer.rb', line 100

def file_path_for_thread(thread)
  File.join(path, file_name_for_thread(thread))
end

#pathObject



78
79
80
# File 'lib/ruby-prof/printers/call_tree_printer.rb', line 78

def path
  @options[:path] || "."
end


46
47
48
49
50
# File 'lib/ruby-prof/printers/call_tree_printer.rb', line 46

def print(options = {})
  setup_options(options)
  determine_event_specification_and_value_scale
  print_threads
end


104
105
106
107
108
# File 'lib/ruby-prof/printers/call_tree_printer.rb', line 104

def print_headers(output, thread)
  output << "#{@event_specification}\n\n"
  # this doesn't work. kcachegrind does not fully support the spec.
  # output << "thread: #{thread.id}\n\n"
end


110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/ruby-prof/printers/call_tree_printer.rb', line 110

def print_method(output, method)
  # Print out the file and method name
  output << "fl=#{file(method)}\n"
  output << "fn=#{method.calltree_name}\n"

  # Now print out the function line number and its self time
  output << "#{method.line} #{convert(method.self_time)}\n"

  # Now print out all the children methods
  method.children.each do |callee|
    output << "cfl=#{file(callee.target)}\n"
    output << "cfn=#{callee.target.calltree_name}\n"
    output << "calls=#{callee.called} #{callee.line}\n"

    # Print out total times here!
    output << "#{callee.line} #{convert(callee.total_time)}\n"
  end
  output << "\n"
end


69
70
71
72
73
74
75
76
# File 'lib/ruby-prof/printers/call_tree_printer.rb', line 69

def print_thread(thread)
  File.open(file_path_for_thread(thread), "w") do |f|
    print_headers(f, thread)
    thread.methods.reverse_each do |method|
      print_method(f, method)
    end
  end
end


52
53
54
55
56
57
58
59
# File 'lib/ruby-prof/printers/call_tree_printer.rb', line 52

def print_threads
  remove_subsidiary_files_from_previous_profile_runs
  # TODO: merge fibers of a given thread here, instead of relying
  # on the profiler to merge fibers.
  @result.threads.each do |thread|
    print_thread(thread)
  end
end

#remove_subsidiary_files_from_previous_profile_runsObject



86
87
88
89
90
# File 'lib/ruby-prof/printers/call_tree_printer.rb', line 86

def remove_subsidiary_files_from_previous_profile_runs
  pattern = [base_name, "callgrind.out", $$, "*"].join(".")
  files = Dir.glob(File.join(path, pattern))
  FileUtils.rm_f(files)
end