ruby-prof 1.7.2 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/{CHANGES → CHANGELOG.md} +112 -178
- data/README.md +5 -5
- data/bin/ruby-prof +1 -4
- data/docs/advanced-usage.md +132 -0
- data/docs/alternatives.md +98 -0
- data/docs/architecture.md +122 -0
- data/docs/best-practices.md +27 -0
- data/docs/getting-started.md +130 -0
- data/docs/history.md +11 -0
- data/docs/index.md +45 -0
- data/docs/profiling-rails.md +64 -0
- data/docs/public/examples/example.rb +33 -0
- data/docs/public/examples/generate_reports.rb +92 -0
- data/docs/public/examples/reports/call_info.txt +27 -0
- data/docs/public/examples/reports/call_stack.html +835 -0
- data/docs/public/examples/reports/callgrind.out +150 -0
- data/docs/public/examples/reports/flame_graph.html +408 -0
- data/docs/public/examples/reports/flat.txt +45 -0
- data/docs/public/examples/reports/graph.dot +129 -0
- data/docs/public/examples/reports/graph.html +1319 -0
- data/docs/public/examples/reports/graph.txt +100 -0
- data/docs/public/examples/reports/graphviz_viewer.html +1 -0
- data/docs/public/images/call_stack.png +0 -0
- data/docs/public/images/class_diagram.png +0 -0
- data/docs/public/images/dot_printer.png +0 -0
- data/docs/public/images/flame_graph.png +0 -0
- data/docs/public/images/flat.png +0 -0
- data/docs/public/images/graph.png +0 -0
- data/docs/public/images/graph_html.png +0 -0
- data/docs/public/images/ruby-prof-logo.svg +1 -0
- data/docs/reports.md +150 -0
- data/docs/stylesheets/extra.css +80 -0
- data/ext/ruby_prof/rp_allocation.c +0 -15
- data/ext/ruby_prof/rp_allocation.h +29 -33
- data/ext/ruby_prof/rp_call_tree.c +3 -0
- data/ext/ruby_prof/rp_call_tree.h +1 -4
- data/ext/ruby_prof/rp_call_trees.h +1 -4
- data/ext/ruby_prof/rp_measurement.c +0 -5
- data/ext/ruby_prof/rp_measurement.h +49 -53
- data/ext/ruby_prof/rp_method.c +3 -0
- data/ext/ruby_prof/rp_method.h +1 -4
- data/ext/ruby_prof/rp_profile.c +1 -1
- data/ext/ruby_prof/rp_profile.h +1 -5
- data/ext/ruby_prof/rp_stack.h +50 -53
- data/ext/ruby_prof/rp_thread.h +1 -4
- data/ext/ruby_prof/ruby_prof.h +1 -4
- data/ext/ruby_prof/vc/ruby_prof.vcxproj +7 -8
- data/lib/ruby-prof/assets/call_stack_printer.html.erb +746 -711
- data/lib/ruby-prof/assets/flame_graph_printer.html.erb +412 -0
- data/lib/ruby-prof/assets/graph_printer.html.erb +355 -355
- data/lib/ruby-prof/call_tree.rb +57 -57
- data/lib/ruby-prof/call_tree_visitor.rb +36 -36
- data/lib/ruby-prof/measurement.rb +17 -17
- data/lib/ruby-prof/printers/abstract_printer.rb +19 -33
- data/lib/ruby-prof/printers/call_info_printer.rb +53 -53
- data/lib/ruby-prof/printers/call_stack_printer.rb +168 -180
- data/lib/ruby-prof/printers/call_tree_printer.rb +132 -145
- data/lib/ruby-prof/printers/dot_printer.rb +177 -132
- data/lib/ruby-prof/printers/flame_graph_printer.rb +79 -0
- data/lib/ruby-prof/printers/flat_printer.rb +52 -52
- data/lib/ruby-prof/printers/graph_html_printer.rb +62 -63
- data/lib/ruby-prof/printers/graph_printer.rb +112 -113
- data/lib/ruby-prof/printers/multi_printer.rb +134 -127
- data/lib/ruby-prof/profile.rb +13 -0
- data/lib/ruby-prof/rack.rb +114 -105
- data/lib/ruby-prof/task.rb +147 -147
- data/lib/ruby-prof/thread.rb +20 -20
- data/lib/ruby-prof/version.rb +1 -1
- data/lib/ruby-prof.rb +50 -52
- data/lib/unprof.rb +10 -10
- data/ruby-prof.gemspec +5 -5
- data/test/abstract_printer_test.rb +25 -27
- data/test/alias_test.rb +203 -117
- data/test/call_tree_builder.rb +126 -126
- data/test/call_tree_visitor_test.rb +27 -27
- data/test/call_trees_test.rb +66 -66
- data/test/duplicate_names_test.rb +32 -32
- data/test/dynamic_method_test.rb +50 -50
- data/test/exceptions_test.rb +24 -24
- data/test/exclude_threads_test.rb +48 -48
- data/test/fiber_test.rb +72 -72
- data/test/inverse_call_tree_test.rb +174 -174
- data/test/line_number_test.rb +138 -1
- data/test/marshal_test.rb +144 -145
- data/test/measure_allocations.rb +26 -26
- data/test/measure_allocations_test.rb +340 -1
- data/test/measure_process_time_test.rb +3098 -3142
- data/test/measure_times.rb +56 -56
- data/test/measure_wall_time_test.rb +511 -372
- data/test/measurement_test.rb +82 -82
- data/test/merge_test.rb +48 -48
- data/test/multi_printer_test.rb +52 -66
- data/test/no_method_class_test.rb +15 -15
- data/test/pause_resume_test.rb +171 -171
- data/test/prime.rb +54 -54
- data/test/prime_script.rb +5 -5
- data/test/printer_call_stack_test.rb +28 -27
- data/test/printer_call_tree_test.rb +30 -30
- data/test/printer_flame_graph_test.rb +82 -0
- data/test/printer_flat_test.rb +99 -99
- data/test/printer_graph_html_test.rb +62 -59
- data/test/printer_graph_test.rb +42 -40
- data/test/printers_test.rb +28 -44
- data/test/printing_recursive_graph_test.rb +81 -81
- data/test/profile_test.rb +101 -101
- data/test/rack_test.rb +103 -93
- data/test/recursive_test.rb +139 -139
- data/test/scheduler.rb +4 -0
- data/test/singleton_test.rb +39 -38
- data/test/stack_printer_test.rb +61 -61
- data/test/start_stop_test.rb +106 -106
- data/test/test_helper.rb +4 -0
- data/test/thread_test.rb +29 -29
- data/test/unique_call_path_test.rb +123 -123
- data/test/yarv_test.rb +56 -56
- metadata +53 -11
- data/ext/ruby_prof/rp_measure_memory.c +0 -46
- data/lib/ruby-prof/compatibility.rb +0 -113
- data/test/compatibility_test.rb +0 -49
- data/test/measure_memory_test.rb +0 -1193
|
@@ -1,180 +1,168 @@
|
|
|
1
|
-
# encoding: utf-8
|
|
2
|
-
|
|
3
|
-
require 'erb'
|
|
4
|
-
require 'fileutils'
|
|
5
|
-
require 'base64'
|
|
6
|
-
require 'set'
|
|
7
|
-
require 'stringio'
|
|
8
|
-
|
|
9
|
-
module RubyProf
|
|
10
|
-
# Prints a HTML visualization of the call tree.
|
|
11
|
-
#
|
|
12
|
-
# To use the printer:
|
|
13
|
-
#
|
|
14
|
-
# result = RubyProf.profile do
|
|
15
|
-
# [code to profile]
|
|
16
|
-
# end
|
|
17
|
-
#
|
|
18
|
-
# printer = RubyProf::CallStackPrinter.new(result)
|
|
19
|
-
# printer.print(STDOUT)
|
|
20
|
-
|
|
21
|
-
class CallStackPrinter < AbstractPrinter
|
|
22
|
-
include ERB::Util
|
|
23
|
-
|
|
24
|
-
# Specify print options.
|
|
25
|
-
#
|
|
26
|
-
#
|
|
27
|
-
#
|
|
28
|
-
#
|
|
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
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
when 5
|
|
141
|
-
"
|
|
142
|
-
when
|
|
143
|
-
"
|
|
144
|
-
|
|
145
|
-
"
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
def arguments
|
|
154
|
-
ARGV.join(' ')
|
|
155
|
-
end
|
|
156
|
-
|
|
157
|
-
def
|
|
158
|
-
@
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
def base64_image
|
|
170
|
-
@data ||= begin
|
|
171
|
-
file = open_asset('call_stack_printer.png')
|
|
172
|
-
Base64.encode64(file).gsub(/\n/, '')
|
|
173
|
-
end
|
|
174
|
-
end
|
|
175
|
-
|
|
176
|
-
def template
|
|
177
|
-
open_asset('call_stack_printer.html.erb')
|
|
178
|
-
end
|
|
179
|
-
end
|
|
180
|
-
end
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
require 'erb'
|
|
4
|
+
require 'fileutils'
|
|
5
|
+
require 'base64'
|
|
6
|
+
require 'set'
|
|
7
|
+
require 'stringio'
|
|
8
|
+
|
|
9
|
+
module RubyProf
|
|
10
|
+
# Prints a HTML visualization of the call tree.
|
|
11
|
+
#
|
|
12
|
+
# To use the printer:
|
|
13
|
+
#
|
|
14
|
+
# result = RubyProf.profile do
|
|
15
|
+
# [code to profile]
|
|
16
|
+
# end
|
|
17
|
+
#
|
|
18
|
+
# printer = RubyProf::CallStackPrinter.new(result)
|
|
19
|
+
# printer.print(STDOUT)
|
|
20
|
+
|
|
21
|
+
class CallStackPrinter < AbstractPrinter
|
|
22
|
+
include ERB::Util
|
|
23
|
+
|
|
24
|
+
# Specify print options.
|
|
25
|
+
#
|
|
26
|
+
# output - Any IO object, including STDOUT or a file.
|
|
27
|
+
#
|
|
28
|
+
# Keyword arguments:
|
|
29
|
+
# title: - a String to override the default "ruby-prof call stack"
|
|
30
|
+
# title of the report.
|
|
31
|
+
#
|
|
32
|
+
# threshold: - a float from 0 to 100 that sets the threshold of
|
|
33
|
+
# results displayed.
|
|
34
|
+
# Default value is 1.0
|
|
35
|
+
#
|
|
36
|
+
# expansion: - a float from 0 to 100 that sets the threshold of
|
|
37
|
+
# results that are expanded, if the percent_total
|
|
38
|
+
# exceeds it.
|
|
39
|
+
# Default value is 10.0
|
|
40
|
+
#
|
|
41
|
+
# application: - a String to override the name of the application,
|
|
42
|
+
# as it appears on the report.
|
|
43
|
+
#
|
|
44
|
+
# Also accepts min_percent:, max_percent:, filter_by:, and sort_method:
|
|
45
|
+
# from AbstractPrinter.
|
|
46
|
+
def print(output = STDOUT, title: "ruby-prof call stack", threshold: 1.0,
|
|
47
|
+
expansion: 10.0, application: $PROGRAM_NAME,
|
|
48
|
+
min_percent: 0, max_percent: 100, filter_by: :self_time, sort_method: nil, **)
|
|
49
|
+
@min_percent = min_percent
|
|
50
|
+
@max_percent = max_percent
|
|
51
|
+
@filter_by = filter_by
|
|
52
|
+
@sort_method = sort_method
|
|
53
|
+
@title = title
|
|
54
|
+
@threshold = threshold
|
|
55
|
+
@expansion = expansion
|
|
56
|
+
@application = application
|
|
57
|
+
output << ERB.new(self.template).result(binding)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def print_stack(output, visited, call_tree, parent_time)
|
|
61
|
+
total_time = call_tree.total_time
|
|
62
|
+
percent_parent = (total_time/parent_time)*100
|
|
63
|
+
percent_total = (total_time/@overall_time)*100
|
|
64
|
+
return unless percent_total > min_percent
|
|
65
|
+
color = self.color(percent_total)
|
|
66
|
+
visible = percent_total >= threshold
|
|
67
|
+
expanded = percent_total >= expansion
|
|
68
|
+
display = visible ? "block" : "none"
|
|
69
|
+
|
|
70
|
+
output << "<li class=\"color#{color}\" style=\"display:#{display}\">" << "\n"
|
|
71
|
+
|
|
72
|
+
if visited.include?(call_tree)
|
|
73
|
+
output << "<a href=\"#\" class=\"toggle empty\" ></a>" << "\n"
|
|
74
|
+
output << "<span>%s %s</span>" % [link(call_tree.target, true), graph_link(call_tree)] << "\n"
|
|
75
|
+
else
|
|
76
|
+
visited << call_tree
|
|
77
|
+
|
|
78
|
+
if call_tree.children.empty?
|
|
79
|
+
output << "<a href=\"#\" class=\"toggle empty\" ></a>" << "\n"
|
|
80
|
+
else
|
|
81
|
+
visible_children = call_tree.children.any?{|ci| (ci.total_time/@overall_time)*100 >= threshold}
|
|
82
|
+
image = visible_children ? (expanded ? "minus" : "plus") : "empty"
|
|
83
|
+
output << "<a href=\"#\" class=\"toggle #{image}\" ></a>" << "\n"
|
|
84
|
+
end
|
|
85
|
+
output << "<span>%4.2f%% (%4.2f%%) %s %s</span>" % [percent_total, percent_parent,
|
|
86
|
+
link(call_tree.target, false), graph_link(call_tree)] << "\n"
|
|
87
|
+
|
|
88
|
+
unless call_tree.children.empty?
|
|
89
|
+
output << (expanded ? '<ul>' : '<ul style="display:none">') << "\n"
|
|
90
|
+
call_tree.children.sort_by{|c| -c.total_time}.each do |child_call_tree|
|
|
91
|
+
print_stack(output, visited, child_call_tree, total_time)
|
|
92
|
+
end
|
|
93
|
+
output << '</ul>' << "\n"
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
visited.delete(call_tree)
|
|
97
|
+
end
|
|
98
|
+
output << '</li>' << "\n"
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def name(call_tree)
|
|
102
|
+
method = call_tree.target
|
|
103
|
+
method.full_name
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def link(method, recursive)
|
|
107
|
+
method_name = "#{recursive ? '*' : ''}#{method.full_name}"
|
|
108
|
+
if method.source_file.nil?
|
|
109
|
+
h method_name
|
|
110
|
+
else
|
|
111
|
+
file = File.expand_path(method.source_file)
|
|
112
|
+
"<a href=\"file://#{file}##{method.line}\">#{h method_name}</a>"
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
def graph_link(call_tree)
|
|
117
|
+
total_calls = call_tree.target.called
|
|
118
|
+
totals = total_calls.to_s
|
|
119
|
+
"[#{call_tree.called} calls, #{totals} total]"
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def method_href(method)
|
|
123
|
+
h(method.full_name.gsub(/[><#\.\?=:]/,"_"))
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
def total_time(call_trees)
|
|
127
|
+
sum(call_trees.map{|ci| ci.total_time})
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
def sum(a)
|
|
131
|
+
a.inject(0.0){|s,t| s+=t}
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
def dump(ci)
|
|
135
|
+
$stderr.printf "%s/%d t:%f s:%f w:%f \n", ci, ci.object_id, ci.total_time, ci.self_time, ci.wait_time
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
def color(p)
|
|
139
|
+
case i = p.to_i
|
|
140
|
+
when 0..5
|
|
141
|
+
"01"
|
|
142
|
+
when 5..10
|
|
143
|
+
"05"
|
|
144
|
+
when 100
|
|
145
|
+
"9"
|
|
146
|
+
else
|
|
147
|
+
"#{i/10}"
|
|
148
|
+
end
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
attr_reader :application, :title, :threshold, :expansion
|
|
152
|
+
|
|
153
|
+
def arguments
|
|
154
|
+
ARGV.join(' ')
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
def base64_image
|
|
158
|
+
@data ||= begin
|
|
159
|
+
file = open_asset('call_stack_printer.png')
|
|
160
|
+
Base64.encode64(file).gsub(/\n/, '')
|
|
161
|
+
end
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
def template
|
|
165
|
+
open_asset('call_stack_printer.html.erb')
|
|
166
|
+
end
|
|
167
|
+
end
|
|
168
|
+
end
|
|
@@ -1,145 +1,132 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
require '
|
|
5
|
-
require '
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
#
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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
|
-
|
|
45
|
-
@
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
end
|
|
68
|
-
|
|
69
|
-
def
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
def
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
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
|
|
144
|
-
end
|
|
145
|
-
end
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
# encoding: utf-8
|
|
3
|
+
|
|
4
|
+
require 'fiber'
|
|
5
|
+
require 'thread'
|
|
6
|
+
require 'fileutils'
|
|
7
|
+
|
|
8
|
+
module RubyProf
|
|
9
|
+
# Generates profiling information in callgrind format for use by
|
|
10
|
+
# kcachegrind and similar tools.
|
|
11
|
+
|
|
12
|
+
class CallTreePrinter < AbstractPrinter
|
|
13
|
+
def calltree_name(method_info)
|
|
14
|
+
klass_path = method_info.klass_name.gsub("::", '/')
|
|
15
|
+
result = "#{klass_path}::#{method_info.method_name}"
|
|
16
|
+
|
|
17
|
+
case method_info.klass_flags
|
|
18
|
+
when 0x2
|
|
19
|
+
"#{result}^"
|
|
20
|
+
when 0x4
|
|
21
|
+
"#{result}^"
|
|
22
|
+
when 0x8
|
|
23
|
+
"#{result}*"
|
|
24
|
+
else
|
|
25
|
+
result
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def determine_event_specification_and_value_scale
|
|
30
|
+
@event_specification = String.new("events: ")
|
|
31
|
+
case @result.measure_mode
|
|
32
|
+
when RubyProf::PROCESS_TIME
|
|
33
|
+
@value_scale = RubyProf::CLOCKS_PER_SEC
|
|
34
|
+
@event_specification << 'process_time'
|
|
35
|
+
when RubyProf::WALL_TIME
|
|
36
|
+
@value_scale = 1_000_000
|
|
37
|
+
@event_specification << 'wall_time'
|
|
38
|
+
when RubyProf.const_defined?(:ALLOCATIONS) && RubyProf::ALLOCATIONS
|
|
39
|
+
@value_scale = 1
|
|
40
|
+
@event_specification << 'allocations'
|
|
41
|
+
when RubyProf.const_defined?(:GC_RUNS) && RubyProf::GC_RUNS
|
|
42
|
+
@value_scale = 1
|
|
43
|
+
@event_specification << 'gc_runs'
|
|
44
|
+
when RubyProf.const_defined?(:GC_TIME) && RubyProf::GC_TIME
|
|
45
|
+
@value_scale = 1000000
|
|
46
|
+
@event_specification << 'gc_time'
|
|
47
|
+
else
|
|
48
|
+
raise "Unknown measure mode: #{RubyProf.measure_mode}"
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def print(path: ".", **)
|
|
53
|
+
@path = path
|
|
54
|
+
determine_event_specification_and_value_scale
|
|
55
|
+
print_threads
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def print_threads
|
|
59
|
+
remove_subsidiary_files_from_previous_profile_runs
|
|
60
|
+
@result.threads.each do |thread|
|
|
61
|
+
print_thread(thread)
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def convert(value)
|
|
66
|
+
(value * @value_scale).round
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def file(method)
|
|
70
|
+
method.source_file ? File.expand_path(method.source_file) : ''
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def print_thread(thread)
|
|
74
|
+
File.open(file_path_for_thread(thread), "w") do |f|
|
|
75
|
+
print_headers(f, thread)
|
|
76
|
+
thread.methods.reverse_each do |method|
|
|
77
|
+
print_method(f, method)
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
attr_reader :path
|
|
83
|
+
|
|
84
|
+
def self.needs_dir?
|
|
85
|
+
true
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def remove_subsidiary_files_from_previous_profile_runs
|
|
89
|
+
pattern = ["callgrind.out", $$, "*"].join(".")
|
|
90
|
+
files = Dir.glob(File.join(path, pattern))
|
|
91
|
+
FileUtils.rm_f(files)
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def file_name_for_thread(thread)
|
|
95
|
+
if thread.fiber_id == Fiber.current.object_id
|
|
96
|
+
["callgrind.out", $$].join(".")
|
|
97
|
+
else
|
|
98
|
+
["callgrind.out", $$, thread.fiber_id].join(".")
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def file_path_for_thread(thread)
|
|
103
|
+
File.join(path, file_name_for_thread(thread))
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def print_headers(output, thread)
|
|
107
|
+
output << "#{@event_specification}\n\n"
|
|
108
|
+
# this doesn't work. kcachegrind does not fully support the spec.
|
|
109
|
+
# output << "thread: #{thread.id}\n\n"
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
def print_method(output, method)
|
|
113
|
+
# Print out the file and method name
|
|
114
|
+
output << "fl=#{file(method)}\n"
|
|
115
|
+
output << "fn=#{self.calltree_name(method)}\n"
|
|
116
|
+
|
|
117
|
+
# Now print out the function line number and its self time
|
|
118
|
+
output << "#{method.line} #{convert(method.self_time)}\n"
|
|
119
|
+
|
|
120
|
+
# Now print out all the children methods
|
|
121
|
+
method.call_trees.callees.each do |callee|
|
|
122
|
+
output << "cfl=#{file(callee.target)}\n"
|
|
123
|
+
output << "cfn=#{self.calltree_name(callee.target)}\n"
|
|
124
|
+
output << "calls=#{callee.called} #{callee.line}\n"
|
|
125
|
+
|
|
126
|
+
# Print out total times here!
|
|
127
|
+
output << "#{callee.line} #{convert(callee.total_time)}\n"
|
|
128
|
+
end
|
|
129
|
+
output << "\n"
|
|
130
|
+
end
|
|
131
|
+
end
|
|
132
|
+
end
|