ruby-prof 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (74) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGES +12 -1
  3. data/bin/ruby-prof +100 -152
  4. data/ext/ruby_prof/rp_aggregate_call_tree.c +41 -0
  5. data/ext/ruby_prof/rp_aggregate_call_tree.h +13 -0
  6. data/ext/ruby_prof/rp_allocation.c +31 -51
  7. data/ext/ruby_prof/rp_allocation.h +2 -2
  8. data/ext/ruby_prof/rp_call_tree.c +353 -0
  9. data/ext/ruby_prof/rp_call_tree.h +43 -0
  10. data/ext/ruby_prof/rp_call_trees.c +266 -0
  11. data/ext/ruby_prof/rp_call_trees.h +29 -0
  12. data/ext/ruby_prof/rp_measure_allocations.c +11 -13
  13. data/ext/ruby_prof/rp_measure_process_time.c +11 -13
  14. data/ext/ruby_prof/rp_measure_wall_time.c +17 -15
  15. data/ext/ruby_prof/rp_measurement.c +27 -36
  16. data/ext/ruby_prof/rp_measurement.h +6 -6
  17. data/ext/ruby_prof/rp_method.c +88 -248
  18. data/ext/ruby_prof/rp_method.h +12 -19
  19. data/ext/ruby_prof/rp_profile.c +277 -270
  20. data/ext/ruby_prof/rp_profile.h +0 -1
  21. data/ext/ruby_prof/rp_stack.c +113 -105
  22. data/ext/ruby_prof/rp_stack.h +15 -18
  23. data/ext/ruby_prof/rp_thread.c +115 -107
  24. data/ext/ruby_prof/rp_thread.h +9 -8
  25. data/ext/ruby_prof/ruby_prof.c +27 -23
  26. data/ext/ruby_prof/ruby_prof.h +9 -0
  27. data/ext/ruby_prof/vc/ruby_prof.vcxproj +11 -7
  28. data/lib/ruby-prof.rb +2 -3
  29. data/lib/ruby-prof/assets/call_stack_printer.html.erb +4 -7
  30. data/lib/ruby-prof/assets/graph_printer.html.erb +5 -6
  31. data/lib/ruby-prof/{call_info.rb → call_tree.rb} +6 -6
  32. data/lib/ruby-prof/call_tree_visitor.rb +36 -0
  33. data/lib/ruby-prof/measurement.rb +5 -2
  34. data/lib/ruby-prof/method_info.rb +3 -15
  35. data/lib/ruby-prof/printers/call_info_printer.rb +12 -10
  36. data/lib/ruby-prof/printers/call_stack_printer.rb +19 -22
  37. data/lib/ruby-prof/printers/call_tree_printer.rb +1 -1
  38. data/lib/ruby-prof/printers/dot_printer.rb +3 -3
  39. data/lib/ruby-prof/printers/graph_printer.rb +3 -4
  40. data/lib/ruby-prof/printers/multi_printer.rb +2 -2
  41. data/lib/ruby-prof/rack.rb +3 -0
  42. data/lib/ruby-prof/thread.rb +3 -18
  43. data/lib/ruby-prof/version.rb +1 -1
  44. data/ruby-prof.gemspec +7 -0
  45. data/test/alias_test.rb +42 -45
  46. data/test/basic_test.rb +0 -86
  47. data/test/{call_info_visitor_test.rb → call_tree_visitor_test.rb} +6 -5
  48. data/test/call_trees_test.rb +66 -0
  49. data/test/exclude_methods_test.rb +17 -12
  50. data/test/fiber_test.rb +203 -6
  51. data/test/gc_test.rb +32 -23
  52. data/test/inverse_call_tree_test.rb +175 -0
  53. data/test/line_number_test.rb +64 -67
  54. data/test/marshal_test.rb +7 -11
  55. data/test/measure_allocations_test.rb +224 -234
  56. data/test/measure_allocations_trace_test.rb +224 -234
  57. data/test/measure_memory_trace_test.rb +814 -469
  58. data/test/measure_process_time_test.rb +0 -64
  59. data/test/measure_times.rb +2 -0
  60. data/test/measure_wall_time_test.rb +34 -58
  61. data/test/pause_resume_test.rb +19 -10
  62. data/test/prime.rb +1 -3
  63. data/test/prime_script.rb +6 -0
  64. data/test/printers_test.rb +1 -1
  65. data/test/recursive_test.rb +50 -54
  66. data/test/start_stop_test.rb +19 -19
  67. data/test/test_helper.rb +3 -15
  68. data/test/thread_test.rb +11 -11
  69. data/test/unique_call_path_test.rb +25 -95
  70. metadata +19 -9
  71. data/ext/ruby_prof/rp_call_info.c +0 -271
  72. data/ext/ruby_prof/rp_call_info.h +0 -35
  73. data/lib/ruby-prof/call_info_visitor.rb +0 -38
  74. data/test/parser_timings.rb +0 -24
@@ -22,10 +22,10 @@ typedef struct
22
22
 
23
23
  void rp_init_allocation(void);
24
24
  void prof_allocation_free(prof_allocation_t* allocation);
25
- void prof_allocation_mark(void *data);
25
+ void prof_allocation_mark(void* data);
26
26
  VALUE prof_allocation_wrap(prof_allocation_t* allocation);
27
27
  prof_allocation_t* prof_allocation_get(VALUE self);
28
- prof_allocation_t* prof_allocate_increment(prof_method_t *method, rb_trace_arg_t *trace_arg);
28
+ prof_allocation_t* prof_allocate_increment(prof_method_t* method, rb_trace_arg_t* trace_arg);
29
29
 
30
30
 
31
31
  #endif //_RP_ALLOCATION_
@@ -0,0 +1,353 @@
1
+ /* Copyright (C) 2005-2019 Shugo Maeda <shugo@ruby-lang.org> and Charlie Savage <cfis@savagexi.com>
2
+ Please see the LICENSE file for copyright and distribution information */
3
+
4
+ #include "rp_call_tree.h"
5
+
6
+ #define INITIAL_CALL_TREES_SIZE 2
7
+
8
+ VALUE cRpCallTree;
9
+
10
+ /* ======= prof_call_tree_t ========*/
11
+ prof_call_tree_t* prof_call_tree_create(prof_method_t* method, prof_call_tree_t* parent, VALUE source_file, int source_line)
12
+ {
13
+ prof_call_tree_t* result = ALLOC(prof_call_tree_t);
14
+ result->method = method;
15
+ result->parent = parent;
16
+ result->children = rb_st_init_numtable();
17
+ result->object = Qnil;
18
+ result->measurement = prof_measurement_create();
19
+
20
+ result->visits = 0;
21
+
22
+ result->source_line = source_line;
23
+ result->source_file = source_file;
24
+
25
+ return result;
26
+ }
27
+
28
+ prof_call_tree_t* prof_call_tree_copy(prof_call_tree_t* other)
29
+ {
30
+ prof_call_tree_t* result = ALLOC(prof_call_tree_t);
31
+ result->children = rb_st_init_numtable();
32
+ result->object = Qnil;
33
+ result->visits = 0;
34
+
35
+ result->method = other->method;
36
+ result->parent = other->parent;
37
+ result->source_line = other->source_line;
38
+ result->source_file = other->source_file;
39
+
40
+ result->measurement = prof_measurement_create();
41
+ result->measurement->called = other->measurement->called;
42
+ result->measurement->total_time = other->measurement->total_time;
43
+ result->measurement->self_time = other->measurement->self_time;
44
+ result->measurement->wait_time = other->measurement->wait_time;
45
+ result->measurement->object = Qnil;
46
+
47
+ return result;
48
+ }
49
+
50
+ void prof_call_tree_merge(prof_call_tree_t* result, prof_call_tree_t* other)
51
+ {
52
+ result->measurement->called += other->measurement->called;
53
+ result->measurement->total_time += other->measurement->total_time;
54
+ result->measurement->self_time += other->measurement->self_time;
55
+ result->measurement->wait_time += other->measurement->wait_time;
56
+ }
57
+
58
+ static int prof_call_tree_collect_children(st_data_t key, st_data_t value, st_data_t result)
59
+ {
60
+ prof_call_tree_t* call_tree = (prof_call_tree_t*)value;
61
+ VALUE arr = (VALUE)result;
62
+ rb_ary_push(arr, prof_call_tree_wrap(call_tree));
63
+ return ST_CONTINUE;
64
+ }
65
+
66
+ static int prof_call_tree_mark_children(st_data_t key, st_data_t value, st_data_t data)
67
+ {
68
+ prof_call_tree_t* call_tree = (prof_call_tree_t*)value;
69
+ rb_st_foreach(call_tree->children, prof_call_tree_mark_children, data);
70
+ prof_call_tree_mark(call_tree);
71
+ return ST_CONTINUE;
72
+ }
73
+
74
+ void prof_call_tree_mark(void* data)
75
+ {
76
+ prof_call_tree_t* call_tree = (prof_call_tree_t*)data;
77
+
78
+ if (call_tree->object != Qnil)
79
+ rb_gc_mark(call_tree->object);
80
+
81
+ if (call_tree->source_file != Qnil)
82
+ rb_gc_mark(call_tree->source_file);
83
+
84
+ prof_measurement_mark(call_tree->measurement);
85
+
86
+ // Recurse down through the whole call tree but only from the top node
87
+ // to avoid calling mark over and over and over.
88
+ if (!call_tree->parent)
89
+ rb_st_foreach(call_tree->children, prof_call_tree_mark_children, 0);
90
+ }
91
+
92
+ static void prof_call_tree_ruby_gc_free(void* data)
93
+ {
94
+ prof_call_tree_t* call_tree = (prof_call_tree_t*)data;
95
+ call_tree->object = Qnil;
96
+ }
97
+
98
+ static int prof_call_tree_free_children(st_data_t key, st_data_t value, st_data_t data)
99
+ {
100
+ prof_call_tree_t* call_tree = (prof_call_tree_t*)value;
101
+ prof_call_tree_free(call_tree);
102
+ return ST_CONTINUE;
103
+ }
104
+
105
+ void prof_call_tree_free(prof_call_tree_t* call_tree_data)
106
+ {
107
+ /* Has this call info object been accessed by Ruby? If
108
+ yes clean it up so to avoid a segmentation fault. */
109
+ if (call_tree_data->object != Qnil)
110
+ {
111
+ RDATA(call_tree_data->object)->dmark = NULL;
112
+ RDATA(call_tree_data->object)->dfree = NULL;
113
+ RDATA(call_tree_data->object)->data = NULL;
114
+ call_tree_data->object = Qnil;
115
+ }
116
+
117
+ // Free children
118
+ rb_st_foreach(call_tree_data->children, prof_call_tree_free_children, 0);
119
+ rb_st_free_table(call_tree_data->children);
120
+
121
+ // Free measurement
122
+ prof_measurement_free(call_tree_data->measurement);
123
+
124
+ // Finally free self
125
+ xfree(call_tree_data);
126
+ }
127
+
128
+ size_t prof_call_tree_size(const void* data)
129
+ {
130
+ return sizeof(prof_call_tree_t);
131
+ }
132
+
133
+ VALUE prof_call_tree_wrap(prof_call_tree_t* call_tree)
134
+ {
135
+ if (call_tree->object == Qnil)
136
+ {
137
+ call_tree->object = Data_Wrap_Struct(cRpCallTree, prof_call_tree_mark, prof_call_tree_ruby_gc_free, call_tree);
138
+ }
139
+ return call_tree->object;
140
+ }
141
+
142
+ static VALUE prof_call_tree_allocate(VALUE klass)
143
+ {
144
+ prof_call_tree_t* call_tree = prof_call_tree_create(NULL, NULL, Qnil, 0);
145
+ call_tree->object = prof_call_tree_wrap(call_tree);
146
+ return call_tree->object;
147
+ }
148
+
149
+ prof_call_tree_t* prof_get_call_tree(VALUE self)
150
+ {
151
+ /* Can't use Data_Get_Struct because that triggers the event hook
152
+ ending up in endless recursion. */
153
+ prof_call_tree_t* result = DATA_PTR(self);
154
+
155
+ if (!result)
156
+ rb_raise(rb_eRuntimeError, "This RubyProf::CallTree instance has already been freed, likely because its profile has been freed.");
157
+
158
+ return result;
159
+ }
160
+
161
+ /* ======= Call Tree Table ========*/
162
+ static size_t call_tree_table_insert(st_table* table, st_data_t key, prof_call_tree_t* val)
163
+ {
164
+ return rb_st_insert(table, (st_data_t)key, (st_data_t)val);
165
+ }
166
+
167
+ prof_call_tree_t* call_tree_table_lookup(st_table* table, st_data_t key)
168
+ {
169
+ st_data_t val;
170
+ if (rb_st_lookup(table, (st_data_t)key, &val))
171
+ {
172
+ return (prof_call_tree_t*)val;
173
+ }
174
+ else
175
+ {
176
+ return NULL;
177
+ }
178
+ }
179
+
180
+ uint32_t prof_call_figure_depth(prof_call_tree_t* call_tree_data)
181
+ {
182
+ uint32_t result = 0;
183
+
184
+ while (call_tree_data->parent)
185
+ {
186
+ result++;
187
+ call_tree_data = call_tree_data->parent;
188
+ }
189
+
190
+ return result;
191
+ }
192
+
193
+ void prof_call_tree_add_parent(prof_call_tree_t* self, prof_call_tree_t* parent)
194
+ {
195
+ prof_call_tree_add_child(parent, self);
196
+ self->parent = parent;
197
+ }
198
+
199
+ void prof_call_tree_add_child(prof_call_tree_t* self, prof_call_tree_t* child)
200
+ {
201
+ call_tree_table_insert(self->children, child->method->key, child);
202
+ }
203
+
204
+ /* ======= RubyProf::CallTree ========*/
205
+
206
+ /* call-seq:
207
+ parent -> call_tree
208
+
209
+ Returns the CallTree parent call_tree object (the method that called this method).*/
210
+ static VALUE prof_call_tree_parent(VALUE self)
211
+ {
212
+ prof_call_tree_t* call_tree = prof_get_call_tree(self);
213
+ if (call_tree->parent)
214
+ return prof_call_tree_wrap(call_tree->parent);
215
+ else
216
+ return Qnil;
217
+ }
218
+
219
+ /* call-seq:
220
+ callees -> array
221
+
222
+ Returns an array of call info objects that this method called (ie, children).*/
223
+ static VALUE prof_call_tree_children(VALUE self)
224
+ {
225
+ prof_call_tree_t* call_tree = prof_get_call_tree(self);
226
+ VALUE result = rb_ary_new();
227
+ rb_st_foreach(call_tree->children, prof_call_tree_collect_children, result);
228
+ return result;
229
+ }
230
+
231
+ /* call-seq:
232
+ called -> MethodInfo
233
+
234
+ Returns the target method. */
235
+ static VALUE prof_call_tree_target(VALUE self)
236
+ {
237
+ prof_call_tree_t* call_tree = prof_get_call_tree(self);
238
+ return prof_method_wrap(call_tree->method);
239
+ }
240
+
241
+ /* call-seq:
242
+ called -> Measurement
243
+
244
+ Returns the measurement associated with this call_tree. */
245
+ static VALUE prof_call_tree_measurement(VALUE self)
246
+ {
247
+ prof_call_tree_t* call_tree = prof_get_call_tree(self);
248
+ return prof_measurement_wrap(call_tree->measurement);
249
+ }
250
+
251
+ /* call-seq:
252
+ depth -> int
253
+
254
+ returns the depth of this call info in the call graph */
255
+ static VALUE prof_call_tree_depth(VALUE self)
256
+ {
257
+ prof_call_tree_t* call_tree_data = prof_get_call_tree(self);
258
+ uint32_t depth = prof_call_figure_depth(call_tree_data);
259
+ return rb_int_new(depth);
260
+ }
261
+
262
+ /* call-seq:
263
+ source_file => string
264
+
265
+ return the source file of the method
266
+ */
267
+ static VALUE prof_call_tree_source_file(VALUE self)
268
+ {
269
+ prof_call_tree_t* result = prof_get_call_tree(self);
270
+ return result->source_file;
271
+ }
272
+
273
+ /* call-seq:
274
+ line_no -> int
275
+
276
+ returns the line number of the method */
277
+ static VALUE prof_call_tree_line(VALUE self)
278
+ {
279
+ prof_call_tree_t* result = prof_get_call_tree(self);
280
+ return INT2FIX(result->source_line);
281
+ }
282
+
283
+ /* :nodoc: */
284
+ static VALUE prof_call_tree_dump(VALUE self)
285
+ {
286
+ prof_call_tree_t* call_tree_data = prof_get_call_tree(self);
287
+ VALUE result = rb_hash_new();
288
+
289
+ rb_hash_aset(result, ID2SYM(rb_intern("measurement")), prof_measurement_wrap(call_tree_data->measurement));
290
+
291
+ rb_hash_aset(result, ID2SYM(rb_intern("source_file")), call_tree_data->source_file);
292
+ rb_hash_aset(result, ID2SYM(rb_intern("source_line")), INT2FIX(call_tree_data->source_line));
293
+
294
+ rb_hash_aset(result, ID2SYM(rb_intern("parent")), prof_call_tree_parent(self));
295
+ rb_hash_aset(result, ID2SYM(rb_intern("children")), prof_call_tree_children(self));
296
+ rb_hash_aset(result, ID2SYM(rb_intern("target")), prof_call_tree_target(self));
297
+
298
+ return result;
299
+ }
300
+
301
+ /* :nodoc: */
302
+ static VALUE prof_call_tree_load(VALUE self, VALUE data)
303
+ {
304
+ VALUE target = Qnil;
305
+ VALUE parent = Qnil;
306
+ prof_call_tree_t* call_tree = prof_get_call_tree(self);
307
+ call_tree->object = self;
308
+
309
+ VALUE measurement = rb_hash_aref(data, ID2SYM(rb_intern("measurement")));
310
+ call_tree->measurement = prof_get_measurement(measurement);
311
+
312
+ call_tree->source_file = rb_hash_aref(data, ID2SYM(rb_intern("source_file")));
313
+ call_tree->source_line = FIX2INT(rb_hash_aref(data, ID2SYM(rb_intern("source_line"))));
314
+
315
+ parent = rb_hash_aref(data, ID2SYM(rb_intern("parent")));
316
+ if (parent != Qnil)
317
+ call_tree->parent = prof_get_call_tree(parent);
318
+
319
+ VALUE callees = rb_hash_aref(data, ID2SYM(rb_intern("children")));
320
+ for (int i = 0; i < rb_array_len(callees); i++)
321
+ {
322
+ VALUE call_tree_object = rb_ary_entry(callees, i);
323
+ prof_call_tree_t* call_tree_data = prof_get_call_tree(call_tree_object);
324
+
325
+ st_data_t key = call_tree_data->method ? call_tree_data->method->key : method_key(Qnil, 0);
326
+ call_tree_table_insert(call_tree->children, key, call_tree_data);
327
+ }
328
+
329
+ target = rb_hash_aref(data, ID2SYM(rb_intern("target")));
330
+ call_tree->method = prof_get_method(target);
331
+
332
+ return data;
333
+ }
334
+
335
+ void rp_init_call_tree()
336
+ {
337
+ /* CallTree */
338
+ cRpCallTree = rb_define_class_under(mProf, "CallTree", rb_cData);
339
+ rb_undef_method(CLASS_OF(cRpCallTree), "new");
340
+ rb_define_alloc_func(cRpCallTree, prof_call_tree_allocate);
341
+
342
+ rb_define_method(cRpCallTree, "parent", prof_call_tree_parent, 0);
343
+ rb_define_method(cRpCallTree, "children", prof_call_tree_children, 0);
344
+ rb_define_method(cRpCallTree, "target", prof_call_tree_target, 0);
345
+ rb_define_method(cRpCallTree, "measurement", prof_call_tree_measurement, 0);
346
+
347
+ rb_define_method(cRpCallTree, "depth", prof_call_tree_depth, 0);
348
+ rb_define_method(cRpCallTree, "source_file", prof_call_tree_source_file, 0);
349
+ rb_define_method(cRpCallTree, "line", prof_call_tree_line, 0);
350
+
351
+ rb_define_method(cRpCallTree, "_dump_data", prof_call_tree_dump, 0);
352
+ rb_define_method(cRpCallTree, "_load_data", prof_call_tree_load, 1);
353
+ }
@@ -0,0 +1,43 @@
1
+ /* Copyright (C) 2005-2019 Shugo Maeda <shugo@ruby-lang.org> and Charlie Savage <cfis@savagexi.com>
2
+ Please see the LICENSE file for copyright and distribution information */
3
+
4
+ #ifndef __RP_CALL_TREE_H__
5
+ #define __RP_CALL_TREE_H__
6
+
7
+ #include "ruby_prof.h"
8
+ #include "rp_measurement.h"
9
+ #include "rp_method.h"
10
+
11
+ extern VALUE cRpCallTree;
12
+
13
+ /* Callers and callee information for a method. */
14
+ typedef struct prof_call_tree_t
15
+ {
16
+ prof_method_t* method;
17
+ struct prof_call_tree_t* parent;
18
+ st_table* children; /* Call infos that this call info calls */
19
+ prof_measurement_t* measurement;
20
+ VALUE object;
21
+
22
+ int visits; /* Current visits on the stack */
23
+
24
+ unsigned int source_line;
25
+ VALUE source_file;
26
+ } prof_call_tree_t;
27
+
28
+ prof_call_tree_t* prof_call_tree_create(prof_method_t* method, prof_call_tree_t* parent, VALUE source_file, int source_line);
29
+ prof_call_tree_t* prof_call_tree_copy(prof_call_tree_t* other);
30
+ void prof_call_tree_merge(prof_call_tree_t* result, prof_call_tree_t* other);
31
+ void prof_call_tree_mark(void* data);
32
+ prof_call_tree_t* call_tree_table_lookup(st_table* table, st_data_t key);
33
+
34
+ void prof_call_tree_add_parent(prof_call_tree_t* self, prof_call_tree_t* parent);
35
+ void prof_call_tree_add_child(prof_call_tree_t* self, prof_call_tree_t* child);
36
+
37
+ uint32_t prof_call_figure_depth(prof_call_tree_t* call_tree_data);
38
+ prof_call_tree_t* prof_get_call_tree(VALUE self);
39
+ VALUE prof_call_tree_wrap(prof_call_tree_t* call_tree);
40
+ void prof_call_tree_free(prof_call_tree_t* call_tree);
41
+ void rp_init_call_tree(void);
42
+
43
+ #endif //__RP_CALL_TREE_H__
@@ -0,0 +1,266 @@
1
+ /* Copyright (C) 2005-2013 Shugo Maeda <shugo@ruby-lang.org> and Charlie Savage <cfis@savagexi.com>
2
+ Please see the LICENSE file for copyright and distribution information */
3
+
4
+ #include "rp_aggregate_call_tree.h"
5
+ #include "rp_call_trees.h"
6
+ #include "rp_measurement.h"
7
+
8
+ #define INITIAL_CALL_TREES_SIZE 2
9
+
10
+ VALUE cRpCallTrees;
11
+
12
+ /* ======= Call Infos ========*/
13
+ prof_call_trees_t* prof_get_call_trees(VALUE self)
14
+ {
15
+ /* Can't use Data_Get_Struct because that triggers the event hook
16
+ ending up in endless recursion. */
17
+ prof_call_trees_t* result = DATA_PTR(self);
18
+
19
+ if (!result)
20
+ rb_raise(rb_eRuntimeError, "This RubyProf::CallTrees instance has already been freed, likely because its profile has been freed.");
21
+
22
+ return result;
23
+ }
24
+
25
+ prof_call_trees_t* prof_call_trees_create()
26
+ {
27
+ prof_call_trees_t* result = ALLOC(prof_call_trees_t);
28
+ result->start = ALLOC_N(prof_call_tree_t*, INITIAL_CALL_TREES_SIZE);
29
+ result->end = result->start + INITIAL_CALL_TREES_SIZE;
30
+ result->ptr = result->start;
31
+ result->object = Qnil;
32
+ return result;
33
+ }
34
+
35
+ void prof_call_trees_mark(prof_call_trees_t* call_trees)
36
+ {
37
+ prof_call_tree_t** call_tree;
38
+ for (call_tree = call_trees->start; call_tree < call_trees->ptr; call_tree++)
39
+ {
40
+ prof_call_tree_mark(*call_tree);
41
+ }
42
+ }
43
+
44
+ void prof_call_trees_free(prof_call_trees_t* call_trees)
45
+ {
46
+ /* Has this method object been accessed by Ruby? If
47
+ yes clean it up so to avoid a segmentation fault. */
48
+ if (call_trees->object != Qnil)
49
+ {
50
+ RDATA(call_trees->object)->dmark = NULL;
51
+ RDATA(call_trees->object)->dfree = NULL;
52
+ RDATA(call_trees->object)->data = NULL;
53
+ call_trees->object = Qnil;
54
+ }
55
+
56
+ // Note we do not free our call_tree structures - since they have no parents they will free themselves
57
+ xfree(call_trees);
58
+ }
59
+
60
+ void prof_call_trees_ruby_gc_free(void* data)
61
+ {
62
+ // This object gets freed by its owning method
63
+ prof_call_trees_t* call_trees = (prof_call_trees_t*)data;
64
+ call_trees->object = Qnil;
65
+ }
66
+
67
+ static int prof_call_trees_collect_aggregates(st_data_t key, st_data_t value, st_data_t data)
68
+ {
69
+ VALUE result = (VALUE)data;
70
+ prof_call_tree_t* call_tree_data = (prof_call_tree_t*)value;
71
+ VALUE aggregate_call_tree = prof_aggregate_call_tree_wrap(call_tree_data);
72
+ rb_ary_push(result, aggregate_call_tree);
73
+
74
+ return ST_CONTINUE;
75
+ }
76
+
77
+ static int prof_call_trees_collect_callees(st_data_t key, st_data_t value, st_data_t hash)
78
+ {
79
+ st_table* callers = (st_table*)hash;
80
+ prof_call_tree_t* call_tree_data = (prof_call_tree_t*)value;
81
+
82
+ prof_call_tree_t* aggregate_call_tree_data = NULL;
83
+
84
+ if (rb_st_lookup(callers, call_tree_data->method->key, (st_data_t*)&aggregate_call_tree_data))
85
+ {
86
+ prof_call_tree_merge(aggregate_call_tree_data, call_tree_data);
87
+ }
88
+ else
89
+ {
90
+ aggregate_call_tree_data = prof_call_tree_copy(call_tree_data);
91
+ rb_st_insert(callers, call_tree_data->method->key, (st_data_t)aggregate_call_tree_data);
92
+ }
93
+
94
+ return ST_CONTINUE;
95
+ }
96
+
97
+ VALUE prof_call_trees_wrap(prof_call_trees_t* call_trees)
98
+ {
99
+ if (call_trees->object == Qnil)
100
+ {
101
+ call_trees->object = Data_Wrap_Struct(cRpCallTrees, prof_call_trees_mark, prof_call_trees_ruby_gc_free, call_trees);
102
+ }
103
+ return call_trees->object;
104
+ }
105
+
106
+ void prof_add_call_tree(prof_call_trees_t* call_trees, prof_call_tree_t* call_tree)
107
+ {
108
+ if (call_trees->ptr == call_trees->end)
109
+ {
110
+ size_t len = call_trees->ptr - call_trees->start;
111
+ size_t new_capacity = (call_trees->end - call_trees->start) * 2;
112
+ REALLOC_N(call_trees->start, prof_call_tree_t*, new_capacity);
113
+ call_trees->ptr = call_trees->start + len;
114
+ call_trees->end = call_trees->start + new_capacity;
115
+ }
116
+ *call_trees->ptr = call_tree;
117
+ call_trees->ptr++;
118
+ }
119
+
120
+ /* ================ Call Infos =================*/
121
+ /* Document-class: RubyProf::CallTrees
122
+ The RubyProf::MethodInfo class stores profiling data for a method.
123
+ One instance of the RubyProf::MethodInfo class is created per method
124
+ called per thread. Thus, if a method is called in two different
125
+ thread then there will be two RubyProf::MethodInfo objects
126
+ created. RubyProf::MethodInfo objects can be accessed via
127
+ the RubyProf::Profile object. */
128
+ VALUE prof_call_trees_allocate(VALUE klass)
129
+ {
130
+ prof_call_trees_t* call_trees_data = prof_call_trees_create();
131
+ call_trees_data->object = prof_call_trees_wrap(call_trees_data);
132
+ return call_trees_data->object;
133
+ }
134
+
135
+
136
+ /* call-seq:
137
+ min_depth -> Integer
138
+
139
+ Returns the minimum depth of this method in any call tree */
140
+ VALUE prof_call_trees_min_depth(VALUE self)
141
+ {
142
+ unsigned int depth = INT_MAX;
143
+
144
+ prof_call_trees_t* call_trees = prof_get_call_trees(self);
145
+ for (prof_call_tree_t** p_call_tree = call_trees->start; p_call_tree < call_trees->ptr; p_call_tree++)
146
+ {
147
+ unsigned int call_tree_depth = prof_call_figure_depth(*p_call_tree);
148
+ if (call_tree_depth < depth)
149
+ depth = call_tree_depth;
150
+ }
151
+
152
+ return UINT2NUM(depth);
153
+ }
154
+
155
+ /* call-seq:
156
+ callers -> array
157
+
158
+ Returns an array of all CallTree objects that called this method. */
159
+ VALUE prof_call_trees_call_trees(VALUE self)
160
+ {
161
+ VALUE result = rb_ary_new();
162
+
163
+ prof_call_trees_t* call_trees = prof_get_call_trees(self);
164
+ for (prof_call_tree_t** p_call_tree = call_trees->start; p_call_tree < call_trees->ptr; p_call_tree++)
165
+ {
166
+ VALUE call_tree = prof_call_tree_wrap(*p_call_tree);
167
+ rb_ary_push(result, call_tree);
168
+ }
169
+ return result;
170
+ }
171
+
172
+ /* call-seq:
173
+ callers -> array
174
+
175
+ Returns an array of aggregated CallTree objects that called this method (ie, parents).*/
176
+ VALUE prof_call_trees_callers(VALUE self)
177
+ {
178
+ st_table* callers = rb_st_init_numtable();
179
+
180
+ prof_call_trees_t* call_trees = prof_get_call_trees(self);
181
+ for (prof_call_tree_t** p_call_tree = call_trees->start; p_call_tree < call_trees->ptr; p_call_tree++)
182
+ {
183
+ prof_call_tree_t* parent = (*p_call_tree)->parent;
184
+ if (parent == NULL)
185
+ continue;
186
+
187
+ prof_call_tree_t* aggregate_call_tree_data = NULL;
188
+
189
+ if (rb_st_lookup(callers, parent->method->key, (st_data_t*)&aggregate_call_tree_data))
190
+ {
191
+ prof_call_tree_merge(aggregate_call_tree_data, *p_call_tree);
192
+ }
193
+ else
194
+ {
195
+ aggregate_call_tree_data = prof_call_tree_copy(*p_call_tree);
196
+ rb_st_insert(callers, parent->method->key, (st_data_t)aggregate_call_tree_data);
197
+ }
198
+ }
199
+
200
+ VALUE result = rb_ary_new_capa(callers->num_entries);
201
+ rb_st_foreach(callers, prof_call_trees_collect_aggregates, result);
202
+ rb_st_free_table(callers);
203
+ return result;
204
+ }
205
+
206
+ /* call-seq:
207
+ callees -> array
208
+
209
+ Returns an array of aggregated CallTree objects that this method called (ie, children).*/
210
+ VALUE prof_call_trees_callees(VALUE self)
211
+ {
212
+ st_table* callees = rb_st_init_numtable();
213
+
214
+ prof_call_trees_t* call_trees = prof_get_call_trees(self);
215
+ for (prof_call_tree_t** call_tree = call_trees->start; call_tree < call_trees->ptr; call_tree++)
216
+ {
217
+ rb_st_foreach((*call_tree)->children, prof_call_trees_collect_callees, (st_data_t)callees);
218
+ }
219
+
220
+ VALUE result = rb_ary_new_capa(callees->num_entries);
221
+ rb_st_foreach(callees, prof_call_trees_collect_aggregates, result);
222
+ rb_st_free_table(callees);
223
+ return result;
224
+ }
225
+
226
+ /* :nodoc: */
227
+ VALUE prof_call_trees_dump(VALUE self)
228
+ {
229
+ VALUE result = rb_hash_new();
230
+ rb_hash_aset(result, ID2SYM(rb_intern("call_trees")), prof_call_trees_call_trees(self));
231
+
232
+ return result;
233
+ }
234
+
235
+ /* :nodoc: */
236
+ VALUE prof_call_trees_load(VALUE self, VALUE data)
237
+ {
238
+ prof_call_trees_t* call_trees_data = DATA_PTR(self);
239
+ call_trees_data->object = self;
240
+
241
+ VALUE call_trees = rb_hash_aref(data, ID2SYM(rb_intern("call_trees")));
242
+ for (int i = 0; i < rb_array_len(call_trees); i++)
243
+ {
244
+ VALUE call_tree = rb_ary_entry(call_trees, i);
245
+ prof_call_tree_t* call_tree_data = prof_get_call_tree(call_tree);
246
+ prof_add_call_tree(call_trees_data, call_tree_data);
247
+ }
248
+
249
+ return data;
250
+ }
251
+
252
+ void rp_init_call_trees()
253
+ {
254
+ cRpCallTrees = rb_define_class_under(mProf, "CallTrees", rb_cData);
255
+ rb_undef_method(CLASS_OF(cRpCallTrees), "new");
256
+ rb_define_alloc_func(cRpCallTrees, prof_call_trees_allocate);
257
+
258
+ rb_define_method(cRpCallTrees, "min_depth", prof_call_trees_min_depth, 0);
259
+
260
+ rb_define_method(cRpCallTrees, "call_trees", prof_call_trees_call_trees, 0);
261
+ rb_define_method(cRpCallTrees, "callers", prof_call_trees_callers, 0);
262
+ rb_define_method(cRpCallTrees, "callees", prof_call_trees_callees, 0);
263
+
264
+ rb_define_method(cRpCallTrees, "_dump_data", prof_call_trees_dump, 0);
265
+ rb_define_method(cRpCallTrees, "_load_data", prof_call_trees_load, 1);
266
+ }