Class: RubyProf::FastCallTreePrinter

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-prof/printers/fast_call_tree_printer.rb,
ext/ruby_prof/rp_fast_call_tree_printer.c

Overview

Optimized implementation of CallTreePrinter

Instance Method Summary collapse

Constructor Details

#initialize(result) ⇒ FastCallTreePrinter

Returns a new instance of FastCallTreePrinter.



10
11
12
13
# File 'lib/ruby-prof/printers/fast_call_tree_printer.rb', line 10

def initialize(result)
  @result = result
  @output = nil
end

Instance Method Details

#determine_event_specification_and_value_scaleObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ruby-prof/printers/fast_call_tree_printer.rb', line 29

def determine_event_specification_and_value_scale
  @value_scales = []
  @event_specifications = ['events:']

  @result.measure_modes.each do |measure_mode|
    case measure_mode
      when RubyProf::PROCESS_TIME
        @value_scales << RubyProf::CLOCKS_PER_SEC
        @event_specifications << 'process_time'
      when RubyProf::WALL_TIME
        @value_scales << 1_000_000
        @event_specifications << 'wall_time'
      when RubyProf.const_defined?(:CPU_TIME) && RubyProf::CPU_TIME
        @value_scales << RubyProf.cpu_frequency
        @event_specifications << 'cpu_time'
      when RubyProf.const_defined?(:ALLOCATIONS) && RubyProf::ALLOCATIONS
        @value_scales << 1
        @event_specifications << 'allocations'
      when RubyProf.const_defined?(:MEMORY) && RubyProf::MEMORY
        @value_scales << 1
        @event_specifications << 'memory'
      when RubyProf.const_defined?(:GC_RUNS) && RubyProf::GC_RUNS
        @value_scales << 1
        @event_specifications << 'gc_runs'
      when RubyProf.const_defined?(:GC_TIME) && RubyProf::GC_TIME
        @value_scales << 1000000
        @event_specifications << 'gc_time'
      else
        raise "Unknown measure mode: #{measure_mode}"
    end
  end
end

Specify print options.

options - Hash table

:only_threads - list of threads to print


20
21
22
23
24
25
26
27
# File 'lib/ruby-prof/printers/fast_call_tree_printer.rb', line 20

def print(output = STDOUT, options = {})
  @output = output
  @options = options

  determine_event_specification_and_value_scale
  print_headers
  print_threads
end


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

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

Prints to @output the call tree of a thread

Returns:

  • (nil)


216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'ext/ruby_prof/rp_fast_call_tree_printer.c', line 216

VALUE
prof_fast_call_tree_printer_print_thread(VALUE self, VALUE thread) {
    thread_data_t *thread_data = prof_get_thread(thread);

    VALUE output = rb_iv_get(self, "@output");
    VALUE value_scales_val = rb_iv_get(self, "@value_scales");

    Check_Type(value_scales_val, T_ARRAY);
    size_t value_scales_len = RARRAY_LEN(value_scales_val);

    call_tree_printer_vars *vars = call_tree_printer_vars_allocate(value_scales_len);
    vars->output = output;
    for(size_t i = 0; i < value_scales_len; i++) {
        vars->value_scales[i] = NUM2DBL(rb_ary_entry(value_scales_val, i));
    }

    print_methods_in_reverse(vars, thread_data->method_table);

    xfree(vars);

    return Qnil;
}


62
63
64
65
66
67
68
# File 'lib/ruby-prof/printers/fast_call_tree_printer.rb', line 62

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

#printable_threadsObject



70
71
72
73
74
75
76
77
78
79
# File 'lib/ruby-prof/printers/fast_call_tree_printer.rb', line 70

def printable_threads
  if @options[:only_threads]
    only_thread_ids = @options[:only_threads].map(&:object_id)
    @result.threads.select do |t|
      only_thread_ids.include?(t.id)
    end
  else
    @result.threads
  end
end