acunote-ruby-prof 0.9.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (84) hide show
  1. data/CHANGES +240 -0
  2. data/LICENSE +23 -0
  3. data/README.rdoc +439 -0
  4. data/Rakefile +148 -0
  5. data/bin/ruby-prof +236 -0
  6. data/examples/empty.png +0 -0
  7. data/examples/flat.txt +55 -0
  8. data/examples/graph.dot +106 -0
  9. data/examples/graph.html +823 -0
  10. data/examples/graph.png +0 -0
  11. data/examples/graph.txt +170 -0
  12. data/examples/minus.png +0 -0
  13. data/examples/multi.flat.txt +23 -0
  14. data/examples/multi.graph.html +906 -0
  15. data/examples/multi.grind.dat +194 -0
  16. data/examples/multi.stack.html +573 -0
  17. data/examples/plus.png +0 -0
  18. data/examples/stack.html +573 -0
  19. data/ext/ruby_prof/extconf.rb +43 -0
  20. data/ext/ruby_prof/measure_allocations.h +58 -0
  21. data/ext/ruby_prof/measure_cpu_time.h +152 -0
  22. data/ext/ruby_prof/measure_gc_runs.h +76 -0
  23. data/ext/ruby_prof/measure_gc_time.h +57 -0
  24. data/ext/ruby_prof/measure_memory.h +101 -0
  25. data/ext/ruby_prof/measure_process_time.h +52 -0
  26. data/ext/ruby_prof/measure_wall_time.h +53 -0
  27. data/ext/ruby_prof/mingw/Rakefile +23 -0
  28. data/ext/ruby_prof/mingw/build.rake +38 -0
  29. data/ext/ruby_prof/ruby_prof.c +1834 -0
  30. data/ext/ruby_prof/ruby_prof.h +190 -0
  31. data/ext/ruby_prof/version.h +4 -0
  32. data/lib/ruby-prof.rb +62 -0
  33. data/lib/ruby-prof/abstract_printer.rb +41 -0
  34. data/lib/ruby-prof/aggregate_call_info.rb +68 -0
  35. data/lib/ruby-prof/call_info.rb +112 -0
  36. data/lib/ruby-prof/call_stack_printer.rb +751 -0
  37. data/lib/ruby-prof/call_tree_printer.rb +133 -0
  38. data/lib/ruby-prof/dot_printer.rb +153 -0
  39. data/lib/ruby-prof/empty.png +0 -0
  40. data/lib/ruby-prof/flat_printer.rb +78 -0
  41. data/lib/ruby-prof/flat_printer_with_line_numbers.rb +72 -0
  42. data/lib/ruby-prof/graph_html_printer.rb +278 -0
  43. data/lib/ruby-prof/graph_printer.rb +245 -0
  44. data/lib/ruby-prof/method_info.rb +131 -0
  45. data/lib/ruby-prof/minus.png +0 -0
  46. data/lib/ruby-prof/multi_printer.rb +54 -0
  47. data/lib/ruby-prof/plus.png +0 -0
  48. data/lib/ruby-prof/rack.rb +30 -0
  49. data/lib/ruby-prof/result.rb +70 -0
  50. data/lib/ruby-prof/symbol_to_proc.rb +8 -0
  51. data/lib/ruby-prof/task.rb +146 -0
  52. data/lib/ruby-prof/test.rb +148 -0
  53. data/lib/unprof.rb +8 -0
  54. data/rails/environment/profile.rb +24 -0
  55. data/rails/example/example_test.rb +9 -0
  56. data/rails/profile_test_helper.rb +21 -0
  57. data/test/aggregate_test.rb +136 -0
  58. data/test/basic_test.rb +290 -0
  59. data/test/current_failures_windows +8 -0
  60. data/test/do_nothing.rb +0 -0
  61. data/test/duplicate_names_test.rb +32 -0
  62. data/test/enumerable_test.rb +16 -0
  63. data/test/exceptions_test.rb +15 -0
  64. data/test/exclude_threads_test.rb +54 -0
  65. data/test/exec_test.rb +14 -0
  66. data/test/line_number_test.rb +73 -0
  67. data/test/measurement_test.rb +122 -0
  68. data/test/method_elimination_test.rb +74 -0
  69. data/test/module_test.rb +44 -0
  70. data/test/multi_printer_test.rb +81 -0
  71. data/test/no_method_class_test.rb +13 -0
  72. data/test/prime.rb +55 -0
  73. data/test/prime_test.rb +13 -0
  74. data/test/printers_test.rb +164 -0
  75. data/test/recursive_test.rb +236 -0
  76. data/test/ruby-prof-bin +20 -0
  77. data/test/singleton_test.rb +38 -0
  78. data/test/stack_printer_test.rb +74 -0
  79. data/test/stack_test.rb +138 -0
  80. data/test/start_stop_test.rb +112 -0
  81. data/test/test_suite.rb +32 -0
  82. data/test/thread_test.rb +173 -0
  83. data/test/unique_call_path_test.rb +225 -0
  84. metadata +185 -0
@@ -0,0 +1,190 @@
1
+ /*
2
+ * Copyright (C) 2008 Shugo Maeda <shugo@ruby-lang.org>
3
+ * Charlie Savage <cfis@savagexi.com>
4
+ * All rights reserved.
5
+ *
6
+ * Redistribution and use in source and binary forms, with or without
7
+ * modification, are permitted provided that the following conditions
8
+ * are met:
9
+ * 1. Redistributions of source code must retain the above copyright
10
+ * notice, this list of conditions and the following disclaimer.
11
+ * 2. Redistributions in binary form must reproduce the above copyright
12
+ * notice, this list of conditions and the following disclaimer in the
13
+ * documentation and/or other materials provided with the distribution.
14
+ *
15
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
16
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
19
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25
+ * SUCH DAMAGE.
26
+ */
27
+
28
+ /* ruby-prof tracks the time spent executing every method in ruby programming.
29
+ The main players are:
30
+
31
+ prof_result_t - Its one field, values, contains the overall results
32
+ thread_data_t - Stores data about a single thread.
33
+ prof_stack_t - The method call stack in a particular thread
34
+ prof_method_t - Profiling information for each method
35
+ prof_call_info_t - Keeps track a method's callers and callees.
36
+
37
+ The final resulut is a hash table of thread_data_t, keyed on the thread
38
+ id. Each thread has an hash a table of prof_method_t, keyed on the
39
+ method id. A hash table is used for quick look up when doing a profile.
40
+ However, it is exposed to Ruby as an array.
41
+
42
+ Each prof_method_t has two hash tables, parent and children, of prof_call_info_t.
43
+ These objects keep track of a method's callers (who called the method) and its
44
+ callees (who the method called). These are keyed the method id, but once again,
45
+ are exposed to Ruby as arrays. Each prof_call_into_t maintains a pointer to the
46
+ caller or callee method, thereby making it easy to navigate through the call
47
+ hierarchy in ruby - which is very helpful for creating call graphs.
48
+ */
49
+
50
+ #ifndef RUBY_PROF_H
51
+ #define RUBY_PROF_H
52
+
53
+ #include <stdio.h>
54
+
55
+ #include <ruby.h>
56
+
57
+ #ifndef RUBY_VM
58
+ #include <node.h>
59
+ #include <st.h>
60
+ typedef rb_event_t rb_event_flag_t;
61
+ #define rb_sourcefile() (node ? node->nd_file : 0)
62
+ #define rb_sourceline() (node ? nd_line(node) : 0)
63
+ #endif
64
+
65
+ #include "version.h"
66
+
67
+ /* ================ Constants =================*/
68
+ #define INITIAL_STACK_SIZE 8
69
+ #define INITIAL_CALL_INFOS_SIZE 2
70
+
71
+
72
+ /* ================ Measurement =================*/
73
+ #ifdef HAVE_LONG_LONG
74
+ typedef unsigned LONG_LONG prof_measure_t; // long long is 8 bytes on 32-bit
75
+ #else
76
+ typedef unsigned long prof_measure_t;
77
+ #endif
78
+
79
+ #include "measure_process_time.h"
80
+ #include "measure_wall_time.h"
81
+ #include "measure_cpu_time.h"
82
+ #include "measure_allocations.h"
83
+ #include "measure_memory.h"
84
+ #include "measure_gc_runs.h"
85
+ #include "measure_gc_time.h"
86
+
87
+ static prof_measure_t (*get_measurement)() = measure_process_time;
88
+ static double (*convert_measurement)(prof_measure_t) = convert_process_time;
89
+
90
+ /* ================ DataTypes =================*/
91
+ static VALUE mProf;
92
+ static VALUE cResult;
93
+ static VALUE cMethodInfo;
94
+ static VALUE cCallInfo;
95
+
96
+ /* nasty hack to avoid compilation warnings related to 64/32 bit conversions */
97
+ #ifndef SIZEOF_ST_INDEX_T
98
+ #define st_index_t int
99
+ #endif
100
+
101
+ /* Profiling information for each method. */
102
+ typedef struct {
103
+ VALUE klass; /* The method's class. */
104
+ ID mid; /* The method id. */
105
+ int depth; /* The recursion depth. */
106
+ st_index_t key; /* Cache calculated key */
107
+ } prof_method_key_t;
108
+
109
+ struct prof_call_infos_t;
110
+
111
+ /* Profiling information for each method. */
112
+ typedef struct {
113
+ prof_method_key_t *key; /* Method key */
114
+ const char *source_file; /* The method's source file */
115
+ int line; /* The method's line number. */
116
+ struct prof_call_infos_t *call_infos; /* Call info objects for this method */
117
+ VALUE object; /* Cahced ruby object */
118
+ } prof_method_t;
119
+
120
+ /* Callers and callee information for a method. */
121
+ typedef struct prof_call_info_t {
122
+ prof_method_t *target; /* Use target instead of method to avoid conflict with Ruby method */
123
+ struct prof_call_info_t *parent;
124
+ st_table *call_infos;
125
+ int called;
126
+ prof_measure_t total_time;
127
+ prof_measure_t self_time;
128
+ prof_measure_t wait_time;
129
+ int line;
130
+ VALUE object;
131
+ VALUE children;
132
+ } prof_call_info_t;
133
+
134
+ /* Array of call_info objects */
135
+ typedef struct prof_call_infos_t {
136
+ prof_call_info_t **start;
137
+ prof_call_info_t **end;
138
+ prof_call_info_t **ptr;
139
+ VALUE object;
140
+ } prof_call_infos_t;
141
+
142
+
143
+ /* Temporary object that maintains profiling information
144
+ for active methods - there is one per method.*/
145
+ typedef struct {
146
+ /* Caching prof_method_t values significantly
147
+ increases performance. */
148
+ prof_call_info_t *call_info;
149
+ prof_measure_t start_time;
150
+ prof_measure_t wait_time;
151
+ prof_measure_t child_time;
152
+ unsigned int line;
153
+ } prof_frame_t;
154
+
155
+ /* Current stack of active methods.*/
156
+ typedef struct {
157
+ prof_frame_t *start;
158
+ prof_frame_t *end;
159
+ prof_frame_t *ptr;
160
+ } prof_stack_t;
161
+
162
+ /* Profiling information for a thread. */
163
+ typedef struct {
164
+ VALUE thread_id; /* Thread id */
165
+ st_table* method_table; /* Methods called in the thread */
166
+ prof_stack_t* stack; /* Active methods */
167
+ prof_measure_t last_switch; /* Point of last context switch */
168
+ } thread_data_t;
169
+
170
+ typedef struct {
171
+ VALUE threads;
172
+ } prof_result_t;
173
+
174
+
175
+ /* ================ Variables =================*/
176
+ static int measure_mode;
177
+ static st_table *threads_tbl = NULL;
178
+ static st_table *exclude_threads_tbl = NULL;
179
+
180
+ /* TODO - If Ruby become multi-threaded this has to turn into
181
+ a separate stack since this isn't thread safe! */
182
+ static thread_data_t* last_thread_data = NULL;
183
+
184
+
185
+ /* Forward declarations */
186
+ static VALUE prof_call_infos_wrap(prof_call_infos_t *call_infos);
187
+ static VALUE prof_call_info_wrap(prof_call_info_t *call_info);
188
+ static VALUE prof_method_wrap(prof_method_t *result);
189
+
190
+ #endif
@@ -0,0 +1,4 @@
1
+ #define RUBY_PROF_VERSION "0.9.2" // for easy parsing from rake files
2
+ #define RUBY_PROF_VERSION_MAJ 0
3
+ #define RUBY_PROF_VERSION_MIN 9
4
+ #define RUBY_PROF_VERSION_MIC 2
@@ -0,0 +1,62 @@
1
+ # require the .so file...
2
+
3
+ me = File.dirname(__FILE__) + '/'
4
+ begin
5
+ # fat binaries
6
+ require "#{me}/#{RUBY_VERSION[0..2]}/ruby_prof"
7
+ rescue Exception
8
+ require "#{me}/../ext/ruby_prof/ruby_prof"
9
+ end
10
+
11
+ require "ruby-prof/result"
12
+ require "ruby-prof/method_info"
13
+ require "ruby-prof/call_info"
14
+ require "ruby-prof/aggregate_call_info"
15
+ require "ruby-prof/flat_printer"
16
+ require "ruby-prof/flat_printer_with_line_numbers"
17
+ require "ruby-prof/graph_printer"
18
+ require "ruby-prof/graph_html_printer"
19
+ require "ruby-prof/call_tree_printer"
20
+ require "ruby-prof/call_stack_printer"
21
+ require "ruby-prof/multi_printer"
22
+ require "ruby-prof/dot_printer"
23
+ require "ruby-prof/symbol_to_proc" # for 1.8's backward compatibility benefit
24
+ require "ruby-prof/rack"
25
+
26
+ module RubyProf
27
+ # See if the user specified the clock mode via
28
+ # the RUBY_PROF_MEASURE_MODE environment variable
29
+ def self.figure_measure_mode
30
+ case ENV["RUBY_PROF_MEASURE_MODE"]
31
+ when "wall" || "wall_time"
32
+ RubyProf.measure_mode = RubyProf::WALL_TIME
33
+ when "cpu" || "cpu_time"
34
+ if ENV.key?("RUBY_PROF_CPU_FREQUENCY")
35
+ RubyProf.cpu_frequency = ENV["RUBY_PROF_CPU_FREQUENCY"].to_f
36
+ else
37
+ begin
38
+ open("/proc/cpuinfo") do |f|
39
+ f.each_line do |line|
40
+ s = line.slice(/cpu MHz\s*:\s*(.*)/, 1)
41
+ if s
42
+ RubyProf.cpu_frequency = s.to_f * 1000000
43
+ break
44
+ end
45
+ end
46
+ end
47
+ rescue Errno::ENOENT
48
+ end
49
+ end
50
+ RubyProf.measure_mode = RubyProf::CPU_TIME
51
+ when "allocations"
52
+ RubyProf.measure_mode = RubyProf::ALLOCATIONS
53
+ when "memory"
54
+ RubyProf.measure_mode = RubyProf::MEMORY
55
+ else
56
+ # the default...
57
+ RubyProf.measure_mode = RubyProf::PROCESS_TIME
58
+ end
59
+ end
60
+ end
61
+
62
+ 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