skaes-ruby-prof 0.7.3

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.
Files changed (67) hide show
  1. data/CHANGES +202 -0
  2. data/LICENSE +23 -0
  3. data/README +436 -0
  4. data/Rakefile +129 -0
  5. data/bin/ruby-prof +207 -0
  6. data/examples/flat.txt +55 -0
  7. data/examples/graph.html +823 -0
  8. data/examples/graph.txt +170 -0
  9. data/ext/extconf.rb +34 -0
  10. data/ext/measure_allocations.h +58 -0
  11. data/ext/measure_cpu_time.h +152 -0
  12. data/ext/measure_gc_runs.h +76 -0
  13. data/ext/measure_gc_time.h +57 -0
  14. data/ext/measure_memory.h +101 -0
  15. data/ext/measure_process_time.h +52 -0
  16. data/ext/measure_wall_time.h +53 -0
  17. data/ext/mingw/Rakefile +23 -0
  18. data/ext/mingw/build.rake +38 -0
  19. data/ext/ruby_prof.c +1747 -0
  20. data/ext/ruby_prof.h +185 -0
  21. data/ext/vc/ruby_prof.sln +20 -0
  22. data/ext/vc/ruby_prof.vcproj +241 -0
  23. data/ext/version.h +4 -0
  24. data/lib/ruby-prof.rb +51 -0
  25. data/lib/ruby-prof/abstract_printer.rb +41 -0
  26. data/lib/ruby-prof/aggregate_call_info.rb +68 -0
  27. data/lib/ruby-prof/call_info.rb +112 -0
  28. data/lib/ruby-prof/call_stack_printer.rb +746 -0
  29. data/lib/ruby-prof/call_tree_printer.rb +84 -0
  30. data/lib/ruby-prof/empty.png +0 -0
  31. data/lib/ruby-prof/flat_printer.rb +79 -0
  32. data/lib/ruby-prof/graph_html_printer.rb +272 -0
  33. data/lib/ruby-prof/graph_printer.rb +164 -0
  34. data/lib/ruby-prof/method_info.rb +131 -0
  35. data/lib/ruby-prof/minus.png +0 -0
  36. data/lib/ruby-prof/multi_printer.rb +55 -0
  37. data/lib/ruby-prof/plus.png +0 -0
  38. data/lib/ruby-prof/result.rb +70 -0
  39. data/lib/ruby-prof/task.rb +146 -0
  40. data/lib/ruby-prof/test.rb +148 -0
  41. data/lib/unprof.rb +8 -0
  42. data/rails/environment/profile.rb +24 -0
  43. data/rails/example/example_test.rb +9 -0
  44. data/rails/profile_test_helper.rb +21 -0
  45. data/test/aggregate_test.rb +136 -0
  46. data/test/basic_test.rb +283 -0
  47. data/test/duplicate_names_test.rb +32 -0
  48. data/test/exceptions_test.rb +15 -0
  49. data/test/exclude_threads_test.rb +54 -0
  50. data/test/line_number_test.rb +73 -0
  51. data/test/measurement_test.rb +121 -0
  52. data/test/method_elimination_test.rb +74 -0
  53. data/test/module_test.rb +54 -0
  54. data/test/multi_printer_test.rb +81 -0
  55. data/test/no_method_class_test.rb +13 -0
  56. data/test/prime.rb +58 -0
  57. data/test/prime_test.rb +13 -0
  58. data/test/printers_test.rb +73 -0
  59. data/test/recursive_test.rb +215 -0
  60. data/test/singleton_test.rb +38 -0
  61. data/test/stack_printer_test.rb +74 -0
  62. data/test/stack_test.rb +138 -0
  63. data/test/start_stop_test.rb +95 -0
  64. data/test/test_suite.rb +26 -0
  65. data/test/thread_test.rb +159 -0
  66. data/test/unique_call_path_test.rb +206 -0
  67. metadata +128 -0
@@ -0,0 +1,51 @@
1
+ require "ruby_prof.so"
2
+
3
+ require "ruby-prof/result"
4
+ require "ruby-prof/method_info"
5
+ require "ruby-prof/call_info"
6
+ require "ruby-prof/aggregate_call_info"
7
+ require "ruby-prof/flat_printer"
8
+ require "ruby-prof/graph_printer"
9
+ require "ruby-prof/graph_html_printer"
10
+ require "ruby-prof/call_tree_printer"
11
+ require "ruby-prof/call_stack_printer"
12
+ require "ruby-prof/multi_printer"
13
+
14
+ require "ruby-prof/test"
15
+
16
+ module RubyProf
17
+ # See if the user specified the clock mode via
18
+ # the RUBY_PROF_MEASURE_MODE environment variable
19
+ def self.figure_measure_mode
20
+ case ENV["RUBY_PROF_MEASURE_MODE"]
21
+ when "wall" || "wall_time"
22
+ RubyProf.measure_mode = RubyProf::WALL_TIME
23
+ when "cpu" || "cpu_time"
24
+ if ENV.key?("RUBY_PROF_CPU_FREQUENCY")
25
+ RubyProf.cpu_frequency = ENV["RUBY_PROF_CPU_FREQUENCY"].to_f
26
+ else
27
+ begin
28
+ open("/proc/cpuinfo") do |f|
29
+ f.each_line do |line|
30
+ s = line.slice(/cpu MHz\s*:\s*(.*)/, 1)
31
+ if s
32
+ RubyProf.cpu_frequency = s.to_f * 1000000
33
+ break
34
+ end
35
+ end
36
+ end
37
+ rescue Errno::ENOENT
38
+ end
39
+ end
40
+ RubyProf.measure_mode = RubyProf::CPU_TIME
41
+ when "allocations"
42
+ RubyProf.measure_mode = RubyProf::ALLOCATIONS
43
+ when "memory"
44
+ RubyProf.measure_mode = RubyProf::MEMORY
45
+ else
46
+ RubyProf.measure_mode = RubyProf::PROCESS_TIME
47
+ end
48
+ end
49
+ end
50
+
51
+ RubyProf::figure_measure_mode
@@ -0,0 +1,41 @@
1
+ module RubyProf
2
+ class AbstractPrinter
3
+ def initialize(result)
4
+ @result = result
5
+ @output = nil
6
+ @options = {}
7
+ end
8
+
9
+ # Specify print options.
10
+ #
11
+ # options - Hash table
12
+ # :min_percent - Number 0 to 100 that specifes the minimum
13
+ # %self (the methods self time divided by the
14
+ # overall total time) that a method must take
15
+ # for it to be printed out in the report.
16
+ # Default value is 0.
17
+ #
18
+ # :print_file - True or false. Specifies if a method's source
19
+ # file should be printed. Default value if false.
20
+ #
21
+ def setup_options(options = {})
22
+ @options = options
23
+ end
24
+
25
+ def min_percent
26
+ @options[:min_percent] || 0
27
+ end
28
+
29
+ def print_file
30
+ @options[:print_file] || false
31
+ end
32
+
33
+ def method_name(method)
34
+ name = method.full_name
35
+ if print_file
36
+ name += " (#{method.source_file}:#{method.line}}"
37
+ end
38
+ name
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,68 @@
1
+ module RubyProf
2
+ class AggregateCallInfo
3
+ attr_reader :call_infos
4
+ def initialize(call_infos)
5
+ if call_infos.length == 0
6
+ raise(ArgumentError, "Must specify at least one call info.")
7
+ end
8
+ @call_infos = call_infos
9
+ end
10
+
11
+ def target
12
+ call_infos.first.target
13
+ end
14
+
15
+ def parent
16
+ call_infos.first.parent
17
+ end
18
+
19
+ def line
20
+ call_infos.first.line
21
+ end
22
+
23
+ def children
24
+ call_infos.inject(Array.new) do |result, call_info|
25
+ result.concat(call_info.children)
26
+ end
27
+ end
28
+
29
+ def total_time
30
+ aggregate_minimal(:total_time)
31
+ end
32
+
33
+ def self_time
34
+ aggregate(:self_time)
35
+ end
36
+
37
+ def wait_time
38
+ aggregate(:wait_time)
39
+ end
40
+
41
+ def children_time
42
+ aggregate_minimal(:children_time)
43
+ end
44
+
45
+ def called
46
+ aggregate(:called)
47
+ end
48
+
49
+ def to_s
50
+ "#{call_infos.first.full_name}"
51
+ end
52
+
53
+ private
54
+
55
+ def aggregate(method_name)
56
+ self.call_infos.inject(0) do |sum, call_info|
57
+ sum += call_info.send(method_name)
58
+ end
59
+ end
60
+
61
+ def aggregate_minimal(method_name)
62
+ self.call_infos.inject(0) do |sum, call_info|
63
+ sum += call_info.send(method_name) if call_info.minimal?
64
+ sum
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,112 @@
1
+ module RubyProf
2
+ class CallInfo
3
+ def depth
4
+ result = 0
5
+ call_info = self.parent
6
+
7
+ while call_info
8
+ result += 1
9
+ call_info = call_info.parent
10
+ end
11
+ result
12
+ end
13
+
14
+ def children_time
15
+ children.inject(0) do |sum, call_info|
16
+ sum += call_info.total_time
17
+ end
18
+ end
19
+
20
+ def stack
21
+ @stack ||= begin
22
+ methods = Array.new
23
+ call_info = self
24
+
25
+ while call_info
26
+ methods << call_info.target
27
+ call_info = call_info.parent
28
+ end
29
+ methods.reverse
30
+ end
31
+ end
32
+
33
+ def call_sequence
34
+ @call_sequence ||= begin
35
+ stack.map {|method| method.full_name}.join('->')
36
+ end
37
+ end
38
+
39
+ def root?
40
+ self.parent.nil?
41
+ end
42
+
43
+ def to_s
44
+ "#{call_sequence}"
45
+ end
46
+
47
+ def minimal?
48
+ @minimal
49
+ end
50
+
51
+ def compute_minimality(parent_methods)
52
+ if parent_methods.include?(target)
53
+ @minimal = false
54
+ else
55
+ @minimal = true
56
+ parent_methods << target unless children.empty?
57
+ end
58
+ children.each {|ci| ci.compute_minimality(parent_methods)}
59
+ parent_methods.delete(target) if @minimal && !children.empty?
60
+ end
61
+
62
+ # eliminate call info from the call tree.
63
+ # adds self and wait time to parent and attaches called methods to parent.
64
+ # merges call trees for methods called from both praent end self.
65
+ def eliminate!
66
+ # puts "eliminating #{self}"
67
+ return unless parent
68
+ parent.add_self_time(self)
69
+ parent.add_wait_time(self)
70
+ children.each do |kid|
71
+ if call = parent.find_call(kid)
72
+ call.merge_call_tree(kid)
73
+ else
74
+ parent.children << kid
75
+ # $stderr.puts "setting parent of #{kid}\nto #{parent}"
76
+ kid.parent = parent
77
+ end
78
+ end
79
+ parent.children.delete(self)
80
+ end
81
+
82
+ # find a sepcific call in list of children. returns nil if not found.
83
+ # note: there can't be more than one child with a given target method. in other words:
84
+ # x.children.grep{|y|y.target==m}.size <= 1 for all method infos m and call infos x
85
+ def find_call(other)
86
+ matching = children.select { |kid| kid.target == other.target }
87
+ raise "inconsistent call tree" unless matching.size <= 1
88
+ matching.first
89
+ end
90
+
91
+ # merge two call trees. adds self, wait, and total time of other to self and merges children of other into children of self.
92
+ def merge_call_tree(other)
93
+ # $stderr.puts "merging #{self}\nand #{other}"
94
+ self.called += other.called
95
+ add_self_time(other)
96
+ add_wait_time(other)
97
+ add_total_time(other)
98
+ other.children.each do |other_kid|
99
+ if kid = find_call(other_kid)
100
+ # $stderr.puts "merging kids"
101
+ kid.merge_call_tree(other_kid)
102
+ else
103
+ other_kid.parent = self
104
+ children << other_kid
105
+ end
106
+ end
107
+ other.children.clear
108
+ other.target.call_infos.delete(other)
109
+ end
110
+
111
+ end
112
+ end
@@ -0,0 +1,746 @@
1
+ require 'ruby-prof/abstract_printer'
2
+ require 'erb'
3
+
4
+ module RubyProf
5
+ class CallStackPrinter < AbstractPrinter
6
+ include ERB::Util
7
+
8
+ def initialize(result)
9
+ super(result)
10
+ end
11
+
12
+ def print(output = STDOUT, options = {})
13
+ @output = output
14
+ setup_options(options)
15
+ filename = options[:filename]
16
+ if @graph_html = options.delete(:graph)
17
+ @graph_html = "file://" + @graph_html if @graph_html[0]=="/"
18
+ end
19
+
20
+ @overall_threads_time = 0.0
21
+ @threads_totals = Hash.new
22
+ @result.threads.each do |thread_id, methods|
23
+ roots = methods.select{|m| m.root?}
24
+ thread_total_time = sum(roots.map{|r| self.total_time(r.call_infos)})
25
+ @overall_threads_time += thread_total_time
26
+ @threads_totals[thread_id] = thread_total_time
27
+ end
28
+
29
+ print_header
30
+
31
+ @result.threads.keys.sort.each do |thread_id|
32
+ @current_thread_id = thread_id
33
+ @overall_time = @threads_totals[thread_id]
34
+ @output.print "<div class=\"thread\">Thread: #{thread_id} (#{"%4.2f%%" % ((@overall_time/@overall_threads_time)*100)} ~ #{@overall_time} seconds)</div>"
35
+ @output.print "<ul name=\"thread\">"
36
+ @result.threads[thread_id].each do |m|
37
+ # $stderr.print m.dump
38
+ next unless m.root?
39
+ m.call_infos.each do |ci|
40
+ next unless ci.root?
41
+ print_stack ci, @threads_totals[thread_id]
42
+ end
43
+ end
44
+ @output.print "</ul>"
45
+ end
46
+
47
+ print_footer
48
+
49
+ copy_image_files
50
+ end
51
+
52
+ def print_stack(call_info, parent_time)
53
+ total_time = call_info.total_time
54
+ percent_parent = (total_time/parent_time)*100
55
+ percent_total = (total_time/@overall_time)*100
56
+ return unless percent_total > min_percent
57
+ color = self.color(percent_total)
58
+ kids = call_info.children
59
+ visible = percent_total >= threshold
60
+ expanded = percent_total >= expansion
61
+ display = visible ? "block" : "none"
62
+ @output.print "<li class=\"color#{color}\" style=\"display:#{display}\">"
63
+ if kids.empty?
64
+ @output.print "<img src=\"empty.png\">"
65
+ else
66
+ visible_children = kids.any?{|ci| (ci.total_time/@overall_time)*100 >= threshold}
67
+ image = visible_children ? (expanded ? "minus" : "plus") : "empty"
68
+ @output.print "<img class=\"toggle\" src=\"#{image}.png\">"
69
+ end
70
+ @output.printf " %4.2f%% (%4.2f%%) %s %s\n", percent_total, percent_parent, link(call_info), graph_link(call_info)
71
+ unless kids.empty?
72
+ if expanded
73
+ @output.print "<ul>"
74
+ else
75
+ @output.print '<ul style="display:none">'
76
+ end
77
+ kids.sort_by{|c| -c.total_time}.each do |call_info|
78
+ print_stack call_info, total_time
79
+ end
80
+ @output.print "</ul>"
81
+ end
82
+ @output.print "</li>"
83
+ end
84
+
85
+ def name(call_info)
86
+ method = call_info.target
87
+ method.full_name
88
+ end
89
+
90
+ def link(call_info)
91
+ method = call_info.target
92
+ file = File.expand_path(method.source_file)
93
+ if file =~ /\/ruby_runtime$/
94
+ h(name(call_info))
95
+ else
96
+ "<a href=\"txmt://open?url=file://#{file}&line=#{method.line}\">#{h(name(call_info))}</a>"
97
+ end
98
+ end
99
+
100
+ def graph_link(call_info)
101
+ total_calls = call_info.target.call_infos.inject(0){|t, ci| t += ci.called}
102
+ href = "#{@graph_html}##{method_href(call_info.target)}"
103
+ totals = @graph_html ? "<a href='#{href}'>#{total_calls}</a>" : total_calls.to_s
104
+ "[#{call_info.called} calls, #{totals} total]"
105
+ end
106
+
107
+ def method_href(method)
108
+ h(method.full_name.gsub(/[><#\.\?=:]/,"_") + "_" + @current_thread_id.to_s)
109
+ end
110
+
111
+ def total_time(call_infos)
112
+ sum(call_infos.map{|ci| ci.total_time})
113
+ end
114
+
115
+ def sum(a)
116
+ a.inject(0.0){|s,t| s+=t}
117
+ end
118
+
119
+ def dump(ci)
120
+ $stderr.printf "%s/%d t:%f s:%f w:%f \n", ci, ci.object_id, ci.total_time, ci.self_time, ci.wait_time
121
+ end
122
+
123
+ def color(p)
124
+ case i = p.to_i
125
+ when 0..5
126
+ "01"
127
+ when 5..10
128
+ "05"
129
+ when 100
130
+ "9"
131
+ else
132
+ "#{i/10}"
133
+ end
134
+ end
135
+
136
+ def application
137
+ @options[:application] || $PROGRAM_NAME
138
+ end
139
+
140
+ def arguments
141
+ ARGV.join(' ')
142
+ end
143
+
144
+ def title
145
+ @title ||= @options.delete(:title) || "ruby-prof call tree"
146
+ end
147
+
148
+ def threshold
149
+ @options[:threshold] || 1.0
150
+ end
151
+
152
+ def expansion
153
+ @options[:expansion] || 10.0
154
+ end
155
+
156
+ def copy_image_files
157
+ if @output.is_a?(File)
158
+ target_dir = File.dirname(@output.path)
159
+ image_dir = File.dirname(__FILE__)
160
+ %w(empty plus minus).each do |img|
161
+ source_file = "#{image_dir}/#{img}.png"
162
+ target_file = "#{target_dir}/#{img}.png"
163
+ FileUtils.cp(source_file, target_file) unless File.exist?(target_file)
164
+ end
165
+ end
166
+ end
167
+
168
+ def print_header
169
+ @output.puts "<html><head>"
170
+ @output.puts '<meta http-equiv="content-type" content="text/html; charset=utf-8">'
171
+ @output.puts "<title>#{h title}</title>"
172
+ print_css
173
+ print_java_script
174
+ @output.puts '</head><body>'
175
+ print_title_bar
176
+ print_commands
177
+ print_help
178
+ end
179
+
180
+ def print_footer
181
+ @output.puts '<div id="sentinel"></div></body></html>'
182
+ end
183
+
184
+ def print_css
185
+ @output.puts <<-'end_css'
186
+ <style type="text/css">
187
+ <!--
188
+ body {
189
+ font-size:70%;
190
+ padding:0px;
191
+ margin:5px;
192
+ margin-right:0px;
193
+ margin-left:0px;
194
+ background: #ffffff;
195
+ }
196
+ ul {
197
+ margin-left:0px;
198
+ margin-top:0px;
199
+ margin-bottom:0px;
200
+ padding-left:0px;
201
+ list-style-type:none;
202
+ }
203
+ li {
204
+ margin-left:11px;
205
+ padding:0px;
206
+ white-space:nowrap;
207
+ border-top:1px solid #cccccc;
208
+ border-left:1px solid #cccccc;
209
+ border-bottom:none;
210
+ }
211
+ .thread {
212
+ margin-left:11px;
213
+ background:#708090;
214
+ padding-top:3px;
215
+ padding-left:12px;
216
+ padding-bottom:2px;
217
+ border-left:1px solid #CCCCCC;
218
+ border-top:1px solid #CCCCCC;
219
+ font-weight:bold;
220
+ }
221
+ .hidden {
222
+ display:none;
223
+ width:0px;
224
+ height:0px;
225
+ margin:0px;
226
+ padding:0px;
227
+ border-style:none;
228
+ }
229
+ .color01 { background:#adbdeb }
230
+ .color05 { background:#9daddb }
231
+ .color0 { background:#8d9dcb }
232
+ .color1 { background:#89bccb }
233
+ .color2 { background:#56e3e7 }
234
+ .color3 { background:#32cd70 }
235
+ .color4 { background:#a3d53c }
236
+ .color5 { background:#c4cb34 }
237
+ .color6 { background:#dcb66d }
238
+ .color7 { background:#cda59e }
239
+ .color8 { background:#be9d9c }
240
+ .color9 { background:#cf947a }
241
+ #commands {
242
+ font-size:10pt;
243
+ padding:10px;
244
+ margin-left:11px;
245
+ margin-bottom:0px;
246
+ margin-top:0px;
247
+ background:#708090;
248
+ border-top:1px solid #cccccc;
249
+ border-left:1px solid #cccccc;
250
+ border-bottom:none;
251
+ }
252
+ #titlebar {
253
+ font-size:10pt;
254
+ padding:10px;
255
+ margin-left:11px;
256
+ margin-bottom:0px;
257
+ margin-top:10px;
258
+ background:#8090a0;
259
+ border-top:1px solid #cccccc;
260
+ border-left:1px solid #cccccc;
261
+ border-bottom:none;
262
+ }
263
+ #help {
264
+ font-size:10pt;
265
+ padding:10px;
266
+ margin-left:11px;
267
+ margin-bottom:0px;
268
+ margin-top:0px;
269
+ background:#8090a0;
270
+ display:none;
271
+ border-top:1px solid #cccccc;
272
+ border-left:1px solid #cccccc;
273
+ border-bottom:none;
274
+ }
275
+ #sentinel {
276
+ height: 400px;
277
+ margin-left:11px;
278
+ background:#8090a0;
279
+ border-top:1px solid #cccccc;
280
+ border-left:1px solid #cccccc;
281
+ border-bottom:none;
282
+ }
283
+ input { margin-left:10px; }
284
+ -->
285
+ </style>
286
+ end_css
287
+ end
288
+
289
+ def print_java_script
290
+ @output.puts <<-'end_java_script'
291
+ <script type="text/javascript">
292
+ /*
293
+ Copyright (C) 2005,2009 Stefan Kaes
294
+ skaes@railsexpress.de
295
+ */
296
+
297
+ function rootNode() {
298
+ return currentThread;
299
+ }
300
+
301
+ function hideUL(node) {
302
+ var lis = node.childNodes
303
+ var l = lis.length;
304
+ for (var i=0; i < l ; i++ ) {
305
+ hideLI(lis[i]);
306
+ }
307
+ }
308
+
309
+ function showUL(node) {
310
+ var lis = node.childNodes;
311
+ var l = lis.length;
312
+ for (var i=0; i < l ; i++ ) {
313
+ showLI(lis[i]);
314
+ }
315
+ }
316
+
317
+ function findUlChild(li){
318
+ var ul = li.childNodes[2];
319
+ while (ul && ul.nodeName != "UL") {
320
+ ul = ul.nextSibling;
321
+ }
322
+ return ul;
323
+ }
324
+
325
+ function isLeafNode(li) {
326
+ var img = li.firstChild;
327
+ return (img.src.indexOf('empty.png') > -1);
328
+ }
329
+
330
+ function hideLI(li) {
331
+ if (isLeafNode(li))
332
+ return;
333
+
334
+ var img = li.firstChild;
335
+ img.src = 'plus.png';
336
+
337
+ var ul = findUlChild(li);
338
+ if (ul) {
339
+ ul.style.display = 'none';
340
+ hideUL(ul);
341
+ }
342
+ }
343
+
344
+ function showLI(li) {
345
+ if (isLeafNode(li))
346
+ return;
347
+
348
+ var img = li.firstChild;
349
+ img.src = 'minus.png';
350
+
351
+ var ul = findUlChild(li);
352
+ if (ul) {
353
+ ul.style.display = 'block';
354
+ showUL(ul);
355
+ }
356
+ }
357
+
358
+ function toggleLI(li) {
359
+ var img = li.firstChild;
360
+ if (img.src.indexOf("minus.png")>-1)
361
+ hideLI(li);
362
+ else {
363
+ if (img.src.indexOf("plus.png")>-1)
364
+ showLI(li);
365
+ }
366
+ }
367
+
368
+ function aboveThreshold(text, threshold) {
369
+ var match = text.match(/\d+[.,]\d+/);
370
+ return (match && parseFloat(match[0].replace(/,/, '.'))>=threshold);
371
+ }
372
+
373
+ function setThresholdLI(li, threshold) {
374
+ var img = li.firstChild;
375
+ var text = img.nextSibling;
376
+ var ul = findUlChild(li);
377
+
378
+ var visible = aboveThreshold(text.nodeValue, threshold) ? 1 : 0;
379
+
380
+ var count = 0;
381
+ if (ul) {
382
+ count = setThresholdUL(ul, threshold);
383
+ }
384
+ if (count>0) {
385
+ img.src = 'minus.png';
386
+ }
387
+ else {
388
+ img.src = 'empty.png';
389
+ }
390
+ if (visible) {
391
+ li.style.display = 'block'
392
+ }
393
+ else {
394
+ li.style.display = 'none'
395
+ }
396
+ return visible;
397
+ }
398
+
399
+ function setThresholdUL(node, threshold) {
400
+ var lis = node.childNodes;
401
+ var l = lis.length;
402
+
403
+ var count = 0;
404
+ for ( var i = 0; i < l ; i++ ) {
405
+ count = count + setThresholdLI(lis[i], threshold);
406
+ }
407
+
408
+ var visible = (count > 0) ? 1 : 0;
409
+ if (visible) {
410
+ node.style.display = 'block';
411
+ }
412
+ else {
413
+ node.style.display = 'none';
414
+ }
415
+ return visible;
416
+ }
417
+
418
+ function toggleChildren(img, event) {
419
+ event.cancelBubble=true;
420
+
421
+ if (img.src.indexOf('empty.png') > -1)
422
+ return;
423
+
424
+ var minus = (img.src.indexOf('minus.png') > -1);
425
+
426
+ if (minus) {
427
+ img.src = 'plus.png';
428
+ }
429
+ else
430
+ img.src = 'minus.png';
431
+
432
+ var li = img.parentNode;
433
+ var ul = findUlChild(li);
434
+ if (ul) {
435
+ if (minus)
436
+ ul.style.display = 'none';
437
+ else
438
+ ul.style.display = 'block';
439
+ }
440
+ if (minus)
441
+ moveSelectionIfNecessary(li);
442
+ }
443
+
444
+ function showChildren(li) {
445
+ var img = li.firstChild;
446
+ if (img.src.indexOf('empty.png') > -1)
447
+ return;
448
+ img.src = 'minus.png';
449
+
450
+ var ul = findUlChild(li);
451
+ if (ul) {
452
+ ul.style.display = 'block';
453
+ }
454
+ }
455
+
456
+ function setThreshold() {
457
+ var tv = document.getElementById("threshold").value;
458
+ if (tv.match(/[0-9]+([.,][0-9]+)?/)) {
459
+ var f = parseFloat(tv.replace(/,/, '.'));
460
+ var threads = document.getElementsByName("thread");
461
+ var l = threads.length;
462
+ for ( var i = 0; i < l ; i++ ) {
463
+ setThresholdUL(threads[i], f);
464
+ }
465
+ var p = selectedNode;
466
+ while (p && p.style.display=='none')
467
+ p=p.parentNode.parentNode;
468
+ if (p && p.nodeName=="LI")
469
+ selectNode(p);
470
+ }
471
+ else {
472
+ alert("Please specify a decimal number as threshold value!");
473
+ }
474
+ }
475
+
476
+ function collapseAll(event) {
477
+ event.cancelBubble=true;
478
+ var threads = document.getElementsByName("thread");
479
+ var l = threads.length;
480
+ for ( var i = 0; i < l ; i++ ) {
481
+ hideUL(threads[i]);
482
+ }
483
+ selectNode(rootNode(), null);
484
+ }
485
+
486
+ function expandAll(event) {
487
+ event.cancelBubble=true;
488
+ var threads = document.getElementsByName("thread");
489
+ var l = threads.length;
490
+ for ( var i = 0; i < l ; i++ ) {
491
+ showUL(threads[i]);
492
+ }
493
+ }
494
+
495
+ function toggleHelp(node) {
496
+ var help = document.getElementById("help");
497
+ if (node.value == "Show Help") {
498
+ node.value = "Hide Help";
499
+ help.style.display = 'block';
500
+ }
501
+ else {
502
+ node.value = "Show Help";
503
+ help.style.display = 'none';
504
+ }
505
+ }
506
+
507
+ var selectedNode = null;
508
+ var selectedColor = null;
509
+ var selectedThread = null;
510
+
511
+ function descendentOf(a,b){
512
+ while (a!=b && b!=null)
513
+ b=b.parentNode;
514
+ return (a==b);
515
+ }
516
+
517
+ function moveSelectionIfNecessary(node){
518
+ if (descendentOf(node, selectedNode))
519
+ selectNode(node, null);
520
+ }
521
+
522
+ function selectNode(node, event) {
523
+ if (event) {
524
+ event.cancelBubble = true;
525
+ thread = findThread(node);
526
+ selectThread(thread);
527
+ }
528
+ if (selectedNode) {
529
+ selectedNode.style.background = selectedColor;
530
+ }
531
+ selectedNode = node;
532
+ selectedColor = node.style.background;
533
+ selectedNode.style.background = "red";
534
+ selectedNode.scrollIntoView();
535
+ window.scrollBy(0,-400);
536
+ }
537
+
538
+ function moveUp(){
539
+ var p = selectedNode.previousSibling;
540
+ while (p && p.style.display == 'none')
541
+ p = p.previousSibling;
542
+ if (p && p.nodeName == "LI") {
543
+ selectNode(p, null);
544
+ }
545
+ }
546
+
547
+ function moveDown(){
548
+ var p = selectedNode.nextSibling;
549
+ while (p && p.style.display == 'none')
550
+ p = p.nextSibling;
551
+ if (p && p.nodeName == "LI") {
552
+ selectNode(p, null);
553
+ }
554
+ }
555
+
556
+ function moveLeft(){
557
+ var p = selectedNode.parentNode.parentNode;
558
+ if (p && p.nodeName=="LI") {
559
+ selectNode(p, null);
560
+ }
561
+ }
562
+
563
+ function moveRight(){
564
+ if (!isLeafNode(selectedNode)) {
565
+ showChildren(selectedNode);
566
+ var ul = findUlChild(selectedNode);
567
+ if (ul) {
568
+ selectNode(ul.firstChild, null);
569
+ }
570
+ }
571
+ }
572
+
573
+ function moveForward(){
574
+ if (isLeafNode(selectedNode)) {
575
+ var p = selectedNode;
576
+ while ((p.nextSibling == null || p.nextSibling.style.display=='none') && p.nodeName=="LI") {
577
+ p = p.parentNode.parentNode;
578
+ }
579
+ if (p.nodeName=="LI")
580
+ selectNode(p.nextSibling, null);
581
+ }
582
+ else {
583
+ moveRight();
584
+ }
585
+ }
586
+
587
+ function isExpandedNode(li){
588
+ var img = li.firstChild;
589
+ return(img.src.indexOf('minus.png')>-1);
590
+ }
591
+
592
+ function moveBackward(){
593
+ var p = selectedNode;
594
+ var q = p.previousSibling;
595
+ while (q != null && q.style.display=='none')
596
+ q = q.previousSibling;
597
+ if (q == null) {
598
+ p = p.parentNode.parentNode;
599
+ } else {
600
+ while (!isLeafNode(q) && isExpandedNode(q)) {
601
+ q = findUlChild(q).lastChild;
602
+ while (q.style.display=='none')
603
+ q = q.previousSibling;
604
+ }
605
+ p = q;
606
+ }
607
+ if (p.nodeName=="LI")
608
+ selectNode(p, null);
609
+ }
610
+
611
+ function moveHome() {
612
+ selectNode(currentThread);
613
+ }
614
+
615
+ var currentThreadIndex = null;
616
+
617
+ function findThread(node){
618
+ while (node && node.parentNode.nodeName!="BODY") {
619
+ node = node.parentNode;
620
+ }
621
+ return node.firstChild;
622
+ }
623
+
624
+ function selectThread(node){
625
+ var threads = document.getElementsByName("thread");
626
+ currentThread = node;
627
+ for (var i=0; i<threads.length; i++) {
628
+ if (threads[i]==currentThread.parentNode)
629
+ currentThreadIndex = i;
630
+ }
631
+ }
632
+
633
+ function nextThread(){
634
+ var threads = document.getElementsByName("thread");
635
+ if (currentThreadIndex==threads.length-1)
636
+ currentThreadIndex = 0;
637
+ else
638
+ currentThreadIndex += 1
639
+ currentThread = threads[currentThreadIndex].firstChild;
640
+ selectNode(currentThread, null);
641
+ }
642
+
643
+ function previousThread(){
644
+ var threads = document.getElementsByName("thread");
645
+ if (currentThreadIndex==0)
646
+ currentThreadIndex = threads.length-1;
647
+ else
648
+ currentThreadIndex -= 1
649
+ currentThread = threads[currentThreadIndex].firstChild;
650
+ selectNode(currentThread, null);
651
+ }
652
+
653
+ function switchThread(node, event){
654
+ event.cancelBubble = true;
655
+ selectThread(node.nextSibling.firstChild);
656
+ selectNode(currentThread, null);
657
+ }
658
+
659
+ function handleKeyEvent(event){
660
+ var code = event.charCode ? event.charCode : event.keyCode;
661
+ var str = String.fromCharCode(code);
662
+ switch (str) {
663
+ case "a": moveLeft(); break;
664
+ case "s": moveDown(); break;
665
+ case "d": moveRight(); break;
666
+ case "w": moveUp(); break;
667
+ case "f": moveForward(); break;
668
+ case "b": moveBackward(); break;
669
+ case "x": toggleChildren(selectedNode.firstChild, event); break;
670
+ case "*": toggleLI(selectedNode); break;
671
+ case "n": nextThread(); break;
672
+ case "h": moveHome(); break;
673
+ case "p": previousThread(); break;
674
+ }
675
+ }
676
+ document.onkeypress=function(event){ handleKeyEvent(event) };
677
+
678
+ window.onload=function(){
679
+ var images = document.getElementsByTagName("img");
680
+ for (var i=0; i<images.length; i++) {
681
+ var img = images[i];
682
+ if (img.className == "toggle") {
683
+ img.onclick = function(event){ toggleChildren(this, event); };
684
+ }
685
+ }
686
+ var divs = document.getElementsByTagName("div");
687
+ for (i=0; i<divs.length; i++) {
688
+ var div = divs[i];
689
+ if (div.className == "thread")
690
+ div.onclick = function(event){ switchThread(this, event) };
691
+ }
692
+ var lis = document.getElementsByTagName("li");
693
+ for (var i=0; i<lis.length; i++) {
694
+ lis[i].onclick = function(event){ selectNode(this, event); };
695
+ }
696
+ var threads = document.getElementsByName("thread");
697
+ currentThreadIndex = 0;
698
+ currentThread = threads[0].firstChild;
699
+ selectNode(currentThread, null);
700
+ }
701
+ </script>
702
+ end_java_script
703
+ end
704
+
705
+ def print_title_bar
706
+ @output.puts <<-"end_title_bar"
707
+ <div id="titlebar">
708
+ Call tree for application <b>#{h application} #{h arguments}</b><br/>
709
+ Generated on #{Time.now} with options #{h @options.inspect}<br/>
710
+ </div>
711
+ end_title_bar
712
+ end
713
+
714
+ def print_commands
715
+ @output.puts <<-"end_commands"
716
+ <div id=\"commands\">
717
+ <span style=\"font-size: 11pt; font-weight: bold;\">Threshold:</span>
718
+ <input value=\"#{h threshold}\" size=\"3\" id=\"threshold\" type=\"text\">
719
+ <input value=\"Apply\" onclick=\"setThreshold();\" type=\"submit\">
720
+ <input value=\"Expand All\" onclick=\"expandAll(event);\" type=\"submit\">
721
+ <input value=\"Collapse All\" onclick=\"collapseAll(event);\" type=\"submit\">
722
+ <input value=\"Show Help\" onclick=\"toggleHelp(this);\" type=\"submit\">
723
+ </div>
724
+ end_commands
725
+ end
726
+
727
+ def print_help
728
+ @output.puts <<-'end_help'
729
+ <div style="display: none;" id="help">
730
+ <img src="empty.png"> Enter a decimal value <i>d</i> into the threshold field and click "Apply"
731
+ to hide all nodes marked with time values lower than <i>d</i>.<br>
732
+ <img src="empty.png"> Click on "Expand All" for full tree expansion.<br>
733
+ <img src="empty.png"> Click on "Collapse All" to show only top level nodes.<br>
734
+ <img src="empty.png"> Use a, s, d, w as in Quake or Urban Terror to navigate the tree.<br>
735
+ <img src="empty.png"> Use f and b to navigate the tree in preorder forward and backwards.<br>
736
+ <img src="empty.png"> Use x to toggle visibility of a subtree.<br>
737
+ <img src="empty.png"> Use * to expand/collapse a whole subtree.<br>
738
+ <img src="empty.png"> Use h to navigate to thread root.<br>
739
+ <img src="empty.png"> Use n and p to navigate between threads.<br>
740
+ <img src="empty.png"> Click on background to move focus to a subtree.<br>
741
+ </div>
742
+ end_help
743
+ end
744
+ end
745
+ end
746
+