ruby-prof-danielhoey 0.8.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (72) hide show
  1. data/CHANGES +221 -0
  2. data/LICENSE +23 -0
  3. data/README +432 -0
  4. data/Rakefile +158 -0
  5. data/bin/ruby-prof +224 -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/ruby_prof/call_tree.c +392 -0
  10. data/ext/ruby_prof/call_tree.h +32 -0
  11. data/ext/ruby_prof/extconf.rb +40 -0
  12. data/ext/ruby_prof/list.c +66 -0
  13. data/ext/ruby_prof/list.h +10 -0
  14. data/ext/ruby_prof/measure_allocations.h +58 -0
  15. data/ext/ruby_prof/measure_cpu_time.h +152 -0
  16. data/ext/ruby_prof/measure_gc_runs.h +76 -0
  17. data/ext/ruby_prof/measure_gc_time.h +57 -0
  18. data/ext/ruby_prof/measure_memory.h +101 -0
  19. data/ext/ruby_prof/measure_process_time.h +52 -0
  20. data/ext/ruby_prof/measure_wall_time.h +53 -0
  21. data/ext/ruby_prof/measurement.h +13 -0
  22. data/ext/ruby_prof/mingw/Rakefile +23 -0
  23. data/ext/ruby_prof/mingw/build.rake +38 -0
  24. data/ext/ruby_prof/ruby_prof.c +1943 -0
  25. data/ext/ruby_prof/ruby_prof.h +183 -0
  26. data/ext/ruby_prof/version.h +4 -0
  27. data/lib/ruby-prof.rb +59 -0
  28. data/lib/ruby-prof/abstract_printer.rb +41 -0
  29. data/lib/ruby-prof/aggregate_call_info.rb +62 -0
  30. data/lib/ruby-prof/call_info.rb +47 -0
  31. data/lib/ruby-prof/call_tree/abstract_printer.rb +24 -0
  32. data/lib/ruby-prof/call_tree/html_printer.rb +89 -0
  33. data/lib/ruby-prof/call_tree/html_printer_output.html.erb +99 -0
  34. data/lib/ruby-prof/call_tree/text_printer.rb +28 -0
  35. data/lib/ruby-prof/call_tree_printer.rb +84 -0
  36. data/lib/ruby-prof/flat_printer.rb +78 -0
  37. data/lib/ruby-prof/flat_printer_with_line_numbers.rb +72 -0
  38. data/lib/ruby-prof/graph_html_printer.rb +256 -0
  39. data/lib/ruby-prof/graph_printer.rb +157 -0
  40. data/lib/ruby-prof/method_info.rb +111 -0
  41. data/lib/ruby-prof/symbol_to_proc.rb +8 -0
  42. data/lib/ruby-prof/task.rb +146 -0
  43. data/lib/ruby-prof/test.rb +148 -0
  44. data/lib/unprof.rb +8 -0
  45. data/rails/environment/profile.rb +24 -0
  46. data/rails/example/example_test.rb +9 -0
  47. data/rails/profile_test_helper.rb +21 -0
  48. data/test/aggregate_test.rb +121 -0
  49. data/test/basic_test.rb +290 -0
  50. data/test/current_failures_windows +8 -0
  51. data/test/do_nothing.rb +0 -0
  52. data/test/duplicate_names_test.rb +32 -0
  53. data/test/enumerable_test.rb +16 -0
  54. data/test/exceptions_test.rb +15 -0
  55. data/test/exclude_threads_test.rb +54 -0
  56. data/test/exec_test.rb +14 -0
  57. data/test/line_number_test.rb +73 -0
  58. data/test/measurement_test.rb +121 -0
  59. data/test/module_test.rb +54 -0
  60. data/test/no_method_class_test.rb +14 -0
  61. data/test/prime.rb +58 -0
  62. data/test/prime_test.rb +13 -0
  63. data/test/printers_test.rb +130 -0
  64. data/test/recursive_test.rb +275 -0
  65. data/test/ruby-prof-bin +20 -0
  66. data/test/singleton_test.rb +37 -0
  67. data/test/stack_test.rb +138 -0
  68. data/test/start_stop_test.rb +95 -0
  69. data/test/test_suite.rb +23 -0
  70. data/test/thread_test.rb +173 -0
  71. data/test/unique_call_path_test.rb +225 -0
  72. metadata +163 -0
@@ -0,0 +1,170 @@
1
+ = Graph Profiles
2
+
3
+ Graph profiles show how long each method runs, which methods call it
4
+ and which methods it calls.
5
+
6
+ As an example, here is the output from running printers_test.rb:
7
+
8
+
9
+ Thread ID: 21277412
10
+ %total %self total self children calls Name
11
+ --------------------------------------------------------------------------------
12
+ 100.00% 0.00% 8.77 0.00 8.77 1 #toplevel
13
+ 8.77 0.00 8.77 1/1 Object#run_primes
14
+ --------------------------------------------------------------------------------
15
+ 8.77 0.00 8.77 1/1 #toplevel
16
+ 100.00% 0.00% 8.77 0.00 8.77 1 Object#run_primes
17
+ 0.02 0.00 0.02 1/1 Object#make_random_array
18
+ 2.09 0.00 2.09 1/1 Object#find_largest
19
+ 6.66 0.00 6.66 1/1 Object#find_primes
20
+ --------------------------------------------------------------------------------
21
+ 6.63 4.06 2.56 500/501 Object#is_prime
22
+ 2.09 0.00 2.09 1/501 Object#find_largest
23
+ 99.48% 46.34% 8.72 4.06 4.66 501 Integer#upto
24
+ 0.00 0.00 0.00 61/61 Array#[]
25
+ 0.00 0.00 0.00 61/61 Fixnum#>
26
+ 2.09 2.09 0.00 61/61 Kernel.sleep
27
+ 1.24 1.24 0.00 250862/250862 Fixnum#==
28
+ 1.33 1.33 0.00 250862/250862 Fixnum#%
29
+ --------------------------------------------------------------------------------
30
+ 6.66 0.01 6.64 1/1 Object#find_primes
31
+ 75.93% 0.17% 6.66 0.01 6.64 1 Array#select
32
+ 6.64 0.01 6.63 500/500 Object#is_prime
33
+ --------------------------------------------------------------------------------
34
+ 6.66 0.00 6.66 1/1 Object#run_primes
35
+ 75.93% 0.00% 6.66 0.00 6.66 1 Object#find_primes
36
+ 6.66 0.01 6.64 1/1 Array#select
37
+ --------------------------------------------------------------------------------
38
+ 6.64 0.01 6.63 500/500 Array#select
39
+ 75.76% 0.17% 6.64 0.01 6.63 500 Object#is_prime
40
+ 0.00 0.00 0.00 500/501 Fixnum#-
41
+ 6.63 4.06 2.56 500/501 Integer#upto
42
+ --------------------------------------------------------------------------------
43
+ 2.09 0.00 2.09 1/1 Object#run_primes
44
+ 23.89% 0.00% 2.09 0.00 2.09 1 Object#find_largest
45
+ 0.00 0.00 0.00 1/501 Fixnum#-
46
+ 2.09 0.00 2.09 1/501 Integer#upto
47
+ 0.00 0.00 0.00 1/1 Array#first
48
+ 0.00 0.00 0.00 1/1 Array#length
49
+ --------------------------------------------------------------------------------
50
+ 2.09 2.09 0.00 61/61 Integer#upto
51
+ 23.89% 23.89% 2.09 2.09 0.00 61 Kernel.sleep
52
+ --------------------------------------------------------------------------------
53
+ 1.33 1.33 0.00 250862/250862 Integer#upto
54
+ 15.12% 15.12% 1.33 1.33 0.00 250862 Fixnum#%
55
+ --------------------------------------------------------------------------------
56
+ 1.24 1.24 0.00 250862/250862 Integer#upto
57
+ 14.13% 14.13% 1.24 1.24 0.00 250862 Fixnum#==
58
+ --------------------------------------------------------------------------------
59
+ 0.02 0.00 0.02 1/1 Object#run_primes
60
+ 0.18% 0.00% 0.02 0.00 0.02 1 Object#make_random_array
61
+ 0.02 0.02 0.00 1/1 Array#each_index
62
+ 0.00 0.00 0.00 1/1 Class#new
63
+ --------------------------------------------------------------------------------
64
+ 0.02 0.02 0.00 1/1 Object#make_random_array
65
+ 0.18% 0.18% 0.02 0.02 0.00 1 Array#each_index
66
+ 0.00 0.00 0.00 500/500 Kernel.rand
67
+ 0.00 0.00 0.00 500/500 Array#[]=
68
+ --------------------------------------------------------------------------------
69
+ 0.00 0.00 0.00 500/501 Object#is_prime
70
+ 0.00 0.00 0.00 1/501 Object#find_largest
71
+ 0.00% 0.00% 0.00 0.00 0.00 501 Fixnum#-
72
+ --------------------------------------------------------------------------------
73
+ 0.00 0.00 0.00 1/1 Kernel.rand
74
+ 0.00% 0.00% 0.00 0.00 0.00 1 Integer#to_int
75
+ --------------------------------------------------------------------------------
76
+ 0.00 0.00 0.00 1/1 Object#find_largest
77
+ 0.00% 0.00% 0.00 0.00 0.00 1 Array#first
78
+ --------------------------------------------------------------------------------
79
+ 0.00 0.00 0.00 1/1 Class#new
80
+ 0.00% 0.00% 0.00 0.00 0.00 1 Array#initialize
81
+ --------------------------------------------------------------------------------
82
+ 0.00 0.00 0.00 1/1 Object#find_largest
83
+ 0.00% 0.00% 0.00 0.00 0.00 1 Array#length
84
+ --------------------------------------------------------------------------------
85
+ 0.00 0.00 0.00 1/1 Object#make_random_array
86
+ 0.00% 0.00% 0.00 0.00 0.00 1 Class#new
87
+ 0.00 0.00 0.00 1/1 Array#initialize
88
+ --------------------------------------------------------------------------------
89
+ 0.00 0.00 0.00 61/61 Integer#upto
90
+ 0.00% 0.00% 0.00 0.00 0.00 61 Fixnum#>
91
+ --------------------------------------------------------------------------------
92
+ 0.00 0.00 0.00 61/61 Integer#upto
93
+ 0.00% 0.00% 0.00 0.00 0.00 61 Array#[]
94
+ --------------------------------------------------------------------------------
95
+ 0.00 0.00 0.00 500/500 Array#each_index
96
+ 0.00% 0.00% 0.00 0.00 0.00 500 Array#[]=
97
+ --------------------------------------------------------------------------------
98
+ 0.00 0.00 0.00 500/500 Array#each_index
99
+ 0.00% 0.00% 0.00 0.00 0.00 500 Kernel.rand
100
+ 0.00 0.00 0.00 1/1 Integer#to_int
101
+
102
+
103
+
104
+ == Overview
105
+ Dashed lines divide the report into entries, with one entry
106
+ per method. Entries are sorted by total time which is the
107
+ time spent in the method plus its children.
108
+
109
+ Each entry has a primary line demarked by values in the
110
+ %total and %self columns. The primary line represents
111
+ the method being profiled. Lines above it are the methods
112
+ that called this method (parents) while the lines below it
113
+ are the methods it called (children).
114
+
115
+ All values are in seconds. For the primary line, the columns represent:
116
+
117
+ %total - The percentage of time spent in this method and its children
118
+ %self - The percentage of time spent in this method
119
+ total - The time spent in this method and its children.
120
+ self - The time spent in this method.
121
+ children - The time spent in this method's children.
122
+ calls - The number of times this method was called.
123
+ name - The name of the method.
124
+
125
+ The interpretation of method names is:
126
+ * #toplevel - The root method that calls all other methods
127
+ * MyObject#test - An instance method "test" of the class "MyObject"
128
+ * <Object:MyObject>#test - The <> characters indicate a singleton method on a singleton class.
129
+
130
+ For example, we see that 99.48% of the time was spent in Integer#upto and its children.
131
+ Of that time, 4.06 seconds was spent in Integer#upto itself and 4.66 in its children.
132
+ Overall, Integer#upto was called 501 times.
133
+
134
+ == Parents
135
+ In each entry, the lines above the primary line are the methods that
136
+ called the current method. If the current method is a root method then
137
+ no parents are shown.
138
+
139
+
140
+ For parent lines, the columns represent:
141
+
142
+ total - The time spent in the current method and it children on behalf of the parent method.
143
+ self - The time spent in this method on behalf of the parent method.
144
+ children - The time spent in this method's children on behalf of the parent.
145
+ calls - The number of times the parent method called this child
146
+
147
+ Looking at Integer#upto again, we see that it was called 500 times from Object#is_prime
148
+ and 1 time from find_largest. Of the 8.72 total seconds spent in Integer#upto, 6.63
149
+ were done for Object#is_prime and 2.09 for Object#find_largest.
150
+
151
+
152
+ == Children
153
+ In each entry, the lines below the primary line are the methods that
154
+ the current method called. If the current method is a leaf method then
155
+ no children are shown.
156
+
157
+ For children lines, the columns represent:
158
+
159
+ total - The time spent in the child, and its children, on behalf of the current method
160
+ self - The time spent in the child on behalf of the current method.
161
+ children - The time spent in the child's children (ie, granchildren) in behalf of the current method
162
+ calls - The number of times the child method was called by the current method.
163
+
164
+ Taking our example of Integer#upto, we see that it called five other methods - Array#[],
165
+ Fixnum#>, Kernel.sleep, Fixnum#= and Fixnum#%. Looking at Kernel.sleep, we see that
166
+ its spent 2.09 seconds working for Integer#upto and its children spent 0 time working for
167
+ Integer#upto. To see the overall time Kernel.sleep took we would have to look up its entry
168
+ in the graph table.
169
+
170
+
@@ -0,0 +1,392 @@
1
+ #include "call_tree.h"
2
+ #include "list.h"
3
+ #include <assert.h>
4
+
5
+ VALUE cCallTree;
6
+ static ID call_tree_class_id;
7
+ int NULL_TIME = -1;
8
+ VALUE root;
9
+
10
+ #ifdef DEBUG
11
+ #define LOG_EVENT(event) \
12
+ printf("%s::%s %s at %u\n", rb_id2name(ct->klass), rb_id2name(ct->mid), event, (unsigned int) time);
13
+
14
+ #define LOG(printf_string, ...) \
15
+ printf(printf_string, ##__VA_ARGS__);
16
+ #else
17
+ #define LOG_EVENT(event);
18
+ #define LOG(printf_string, ...);
19
+ #endif //DEBUG
20
+
21
+ static VALUE method_name_text(ID mid)
22
+ {
23
+ char* name = rb_id2name(mid);
24
+ if (name == NULL) return rb_str_new2("[No method]");
25
+ else return rb_str_new2(name);
26
+ }
27
+
28
+ static VALUE class_name_text(ID mid)
29
+ {
30
+ char* name = rb_id2name(mid);
31
+ if (name == NULL) return rb_str_new2("[No class]");
32
+ else return rb_str_new2(name);
33
+ }
34
+
35
+
36
+ typedef struct call_tree_t {
37
+ ID mid;
38
+ ID klass;
39
+ VALUE parent;
40
+ char* file;
41
+ list children;
42
+ int call_count;
43
+ prof_measure_t start_time;
44
+ prof_measure_t time;
45
+ } call_tree_t;
46
+
47
+
48
+ static int match(call_tree_t* ct, ID klass, ID mid, char* file)
49
+ {
50
+ int match_class_and_mid = (klass == ct->klass) && (mid == ct->mid);
51
+ if (file) return match_class_and_mid && (strcmp(file, ct->file) == 0);
52
+ else return match_class_and_mid;
53
+ }
54
+
55
+
56
+ static VALUE call_tree_create(VALUE self, ID klass, ID mid, char* file)
57
+ {
58
+ VALUE args[4] = {self, klass, mid, file};
59
+ return rb_class_new_instance(4, &args[0], cCallTree);
60
+ }
61
+
62
+ /* CallTreeMethod */
63
+ /* :nodoc: */
64
+ VALUE call_tree_create_root()
65
+ {
66
+ root = call_tree_create(Qnil, rb_str_new2(""), rb_str_new2(""), "");
67
+ return root;
68
+ }
69
+
70
+
71
+ static int mark(st_data_t key, st_data_t value, st_data_t data)
72
+ {
73
+ rb_gc_mark(value);
74
+ return ST_CONTINUE;
75
+ }
76
+
77
+ static void call_tree_mark(call_tree_t* ct)
78
+ {
79
+ rb_gc_mark(ct->klass);
80
+ rb_gc_mark(ct->parent);
81
+
82
+ //mark each of the children
83
+ int i;
84
+ for(i=0; i<list_size(ct->children); i++)
85
+ {
86
+ rb_gc_mark(list_get(ct->children, i));
87
+ }
88
+ }
89
+
90
+ static void call_tree_free(call_tree_t* ct)
91
+ {
92
+ delete_list(ct->children);
93
+ xfree(ct);
94
+ }
95
+
96
+ static VALUE call_tree_alloc(ID klass)
97
+ {
98
+ call_tree_t* call_tree_stuct;
99
+ call_tree_stuct = ALLOC(call_tree_t);
100
+ call_tree_stuct->children = new_list(10);
101
+ return Data_Wrap_Struct(klass, call_tree_mark, call_tree_free, call_tree_stuct);
102
+ }
103
+
104
+ static call_tree_t* get_call_tree(VALUE c_call_tree)
105
+ {
106
+ call_tree_t* ct;
107
+ Data_Get_Struct(c_call_tree, call_tree_t, ct);
108
+ return ct;
109
+ }
110
+
111
+
112
+
113
+ void init_call_tree()
114
+ {
115
+ cCallTree = rb_define_class("CallTree", rb_cObject);
116
+ call_tree_class_id = rb_intern("CallTree");
117
+ rb_define_alloc_func(cCallTree, call_tree_alloc);
118
+ rb_define_method(cCallTree, "initialize", call_tree_initialize, 4);
119
+ rb_define_method(cCallTree, "initialize_copy", call_tree_initialize_copy, 3);
120
+ rb_define_method(cCallTree, "[]", call_tree_fetch, 1);
121
+ rb_define_method(cCallTree, "size", call_tree_size, 0);
122
+ rb_define_method(cCallTree, "parent", call_tree_parent, 0);
123
+ rb_define_method(cCallTree, "children", call_tree_children, 0);
124
+ rb_define_method(cCallTree, "method", call_tree_method, 0);
125
+ rb_define_method(cCallTree, "klass", call_tree_klass, 0);
126
+ rb_define_method(cCallTree, "time", call_tree_time, 0);
127
+ rb_define_method(cCallTree, "file", call_tree_file, 0);
128
+ rb_define_method(cCallTree, "call_count", call_tree_call_count, 0);
129
+ rb_define_method(cCallTree, "method_start", call_tree_method_start, 4);
130
+ rb_define_method(cCallTree, "method_stop", call_tree_method_stop, 1);
131
+ }
132
+
133
+ VALUE call_tree_initialize(VALUE self, VALUE parent, ID klass, ID mid, char* file)
134
+ {
135
+ call_tree_t* ct = get_call_tree(self);
136
+ ct->mid = mid;
137
+ ct->klass = klass;
138
+ ct->parent = parent;
139
+ ct->file = file;
140
+ ct->time = 0;
141
+ ct->call_count = 0;
142
+ return self;
143
+ }
144
+
145
+ VALUE call_tree_initialize_copy(VALUE copy, VALUE original)
146
+ {
147
+ return copy;
148
+ }
149
+
150
+ VALUE call_tree_children(VALUE self)
151
+ {
152
+ list children = get_call_tree(self)->children;
153
+ return rb_ary_new4(list_size(children), list_data(children));
154
+ }
155
+
156
+ VALUE call_tree_fetch(VALUE self, VALUE index)
157
+ {
158
+ list children = get_call_tree(self)->children;
159
+ int i = NUM2INT(index);
160
+
161
+ if (i >= list_size(children)) { return Qnil; }
162
+
163
+ return list_get(children, i);
164
+ }
165
+
166
+ VALUE call_tree_size(VALUE self)
167
+ {
168
+ return INT2NUM(call_tree_size2(self));
169
+ }
170
+
171
+ int call_tree_size2(VALUE self)
172
+ {
173
+ list children = get_call_tree(self)->children;
174
+ int i;
175
+ int size = list_size(children);
176
+ int total_size = size;
177
+ for (i=0; i < size; i++)
178
+ {
179
+ total_size += call_tree_size2(list_get(children, i));
180
+ }
181
+ return total_size;
182
+ }
183
+
184
+ VALUE call_tree_add(VALUE self, ID klass, ID mid, char* file)
185
+ {
186
+ VALUE child = call_tree_create(self, klass, mid, file);
187
+ list_add(get_call_tree(self)->children, child);
188
+ return child;
189
+ }
190
+
191
+ VALUE call_tree_find_parent(VALUE self, ID klass, ID mid, char* file)
192
+ {
193
+ if (NIL_P(self)) { return Qnil; }
194
+ call_tree_t* ct = get_call_tree(self);
195
+
196
+ if (match(ct, klass, mid, file))
197
+ {
198
+ return self;
199
+ }
200
+
201
+ return call_tree_find_parent(ct->parent, klass, mid, file);
202
+ }
203
+
204
+ VALUE find_thread_root(VALUE self)
205
+ {
206
+ VALUE thread_new = call_tree_find_parent(self, rb_intern("[thread]"), Qnil, NULL);
207
+
208
+ if (!NIL_P(thread_new)) return thread_new;
209
+ else return root;
210
+
211
+ }
212
+
213
+ VALUE call_tree_create_thread(VALUE current, char* thread_id, char* file, prof_measure_t time)
214
+ {
215
+ VALUE thread_new = call_tree_find_parent(current, rb_intern("<Class::Thread>"), rb_intern("new"), NULL);
216
+ return call_tree_method_start(call_tree_parent(thread_new), rb_str_new2("[thread]"), rb_intern(thread_id), file, time);
217
+ }
218
+
219
+ void update_time(call_tree_t* ct, prof_measure_t time)
220
+ {
221
+ if (ct->start_time == NULL_TIME) return;
222
+
223
+ prof_measure_t ct_time = ct->time;
224
+ prof_measure_t start_time = ct->start_time;
225
+ prof_measure_t diff = time - start_time;
226
+ ct_time += diff;
227
+ ct->time = ct_time;
228
+ ct->start_time = NULL_TIME;
229
+ LOG("%s::%s time: %u\n", rb_id2name(ct->klass), rb_id2name(ct->mid), (unsigned int) ct->time);
230
+ }
231
+
232
+ int thread_root(call_tree_t* ct)
233
+ {
234
+ return ct->klass == rb_intern("[thread]");
235
+ }
236
+
237
+ int sleep_method(call_tree_t* ct)
238
+ {
239
+ return match(ct, rb_intern("Kernel"), rb_intern("sleep"), NULL);
240
+ }
241
+
242
+ void call_tree_method_pause(VALUE self, prof_measure_t time)
243
+ {
244
+ VALUE method = self;
245
+
246
+ while(1)
247
+ {
248
+ call_tree_t* ct = get_call_tree(method);
249
+ if (sleep_method(ct)) break;
250
+ LOG_EVENT("pause");
251
+ if (thread_root(ct) && ct->start_time == NULL_TIME) break;
252
+ assert(ct->start_time != NULL_TIME);
253
+ update_time(ct, time);
254
+ if (method == root || thread_root(ct)) break;
255
+ method = ct->parent;
256
+ }
257
+ LOG("pause complete\n");
258
+ }
259
+
260
+ void call_tree_method_resume(VALUE self, prof_measure_t time)
261
+ {
262
+ VALUE method = self;
263
+
264
+ while(1)
265
+ {
266
+ call_tree_t* ct = get_call_tree(method);
267
+ if (sleep_method(ct)) break;
268
+ LOG_EVENT("resume");
269
+ assert(ct->start_time == NULL_TIME);
270
+ ct->start_time = time;
271
+ if (method == root || thread_root(ct)) break;
272
+ method = ct->parent;
273
+ }
274
+ LOG("resume complete\n");
275
+ }
276
+
277
+ VALUE call_tree_method_start(VALUE self, VALUE klass_string, ID mid, char* file, prof_measure_t time)
278
+ {
279
+ if (NIL_P(self)) { return self; }
280
+ ID klass = rb_intern(StringValuePtr(klass_string));
281
+ if (klass == call_tree_class_id) { return self; }
282
+
283
+ call_tree_t* parent = get_call_tree(self);
284
+ if (parent->start_time == NULL_TIME && thread_root(parent))
285
+ {
286
+ call_tree_method_resume(self, time);
287
+ }
288
+
289
+ int is_recursive = 0;
290
+ VALUE method_call = call_tree_find_child(self, klass, mid, file);
291
+ if (NIL_P(method_call))
292
+ {
293
+ method_call = call_tree_find_parent(self, klass, mid, file);
294
+ if (NIL_P(method_call))
295
+ {
296
+ method_call = call_tree_add(self, klass, mid, file);
297
+ }
298
+ else
299
+ {
300
+ is_recursive = 1;
301
+ }
302
+ }
303
+ else if (NIL_P(call_tree_find_parent(self, klass, mid, file)))
304
+ {
305
+ is_recursive = 1;
306
+ }
307
+
308
+
309
+ call_tree_t* ct = get_call_tree(method_call);
310
+ if (!is_recursive || ct->start_time == NULL_TIME)
311
+ {
312
+ LOG_EVENT("start");
313
+ ct->start_time = time;
314
+ }
315
+ ct->call_count++;
316
+ return method_call;
317
+ }
318
+
319
+ int recursive_call_child(call_tree_t* ct)
320
+ {
321
+ return (NIL_P(call_tree_find_parent(ct->parent, ct->klass, ct->mid, ct->file)));
322
+ }
323
+
324
+ VALUE call_tree_method_stop(VALUE self, prof_measure_t time)
325
+ {
326
+ if (NIL_P(self)) { return self; }
327
+
328
+ call_tree_t* ct = get_call_tree(self);
329
+
330
+ LOG_EVENT("stop");
331
+ if (recursive_call_child(ct))
332
+ {
333
+ update_time(ct, time);
334
+ }
335
+
336
+ call_tree_t* parent = get_call_tree(ct->parent);
337
+ if (thread_root(parent))
338
+ {
339
+ call_tree_method_pause(ct->parent, time);
340
+ }
341
+ return ct->parent;
342
+ }
343
+
344
+ VALUE call_tree_find_child(VALUE self, ID klass, ID mid, char* file)
345
+ {
346
+ list children = get_call_tree(self)->children;
347
+ int size = list_size(children);
348
+
349
+ int i;
350
+ for (i=0; i < size; i++)
351
+ {
352
+ VALUE child = list_get(children, i);
353
+ call_tree_t* ct = get_call_tree(child);
354
+ if (match(ct, klass, mid, file))
355
+ {
356
+ return child;
357
+ }
358
+ }
359
+ return Qnil;
360
+ }
361
+
362
+
363
+
364
+ VALUE call_tree_method(VALUE self)
365
+ {
366
+ return method_name_text(get_call_tree(self)->mid);
367
+ }
368
+
369
+ VALUE call_tree_klass(VALUE self)
370
+ {
371
+ return class_name_text(get_call_tree(self)->klass);
372
+ }
373
+
374
+ VALUE call_tree_time(VALUE self)
375
+ {
376
+ return rb_float_new(convert_measurement(get_call_tree(self)->time));
377
+ }
378
+
379
+ VALUE call_tree_call_count(VALUE self)
380
+ {
381
+ return rb_int_new(get_call_tree(self)->call_count);
382
+ }
383
+
384
+ VALUE call_tree_parent(VALUE self)
385
+ {
386
+ return get_call_tree(self)->parent;
387
+ }
388
+
389
+ VALUE call_tree_file(VALUE self)
390
+ {
391
+ return rb_str_new2(get_call_tree(self)->file);
392
+ }