ruby-prof 1.7.1 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (134) hide show
  1. checksums.yaml +4 -4
  2. data/{CHANGES → CHANGELOG.md} +118 -176
  3. data/README.md +5 -5
  4. data/bin/ruby-prof +1 -4
  5. data/docs/advanced-usage.md +132 -0
  6. data/docs/alternatives.md +98 -0
  7. data/docs/architecture.md +122 -0
  8. data/docs/best-practices.md +27 -0
  9. data/docs/getting-started.md +130 -0
  10. data/docs/history.md +11 -0
  11. data/docs/index.md +45 -0
  12. data/docs/profiling-rails.md +64 -0
  13. data/docs/public/examples/example.rb +33 -0
  14. data/docs/public/examples/generate_reports.rb +92 -0
  15. data/docs/public/examples/reports/call_info.txt +27 -0
  16. data/docs/public/examples/reports/call_stack.html +835 -0
  17. data/docs/public/examples/reports/callgrind.out +150 -0
  18. data/docs/public/examples/reports/flame_graph.html +408 -0
  19. data/docs/public/examples/reports/flat.txt +45 -0
  20. data/docs/public/examples/reports/graph.dot +129 -0
  21. data/docs/public/examples/reports/graph.html +1319 -0
  22. data/docs/public/examples/reports/graph.txt +100 -0
  23. data/docs/public/examples/reports/graphviz_viewer.html +1 -0
  24. data/docs/public/images/call_stack.png +0 -0
  25. data/docs/public/images/class_diagram.png +0 -0
  26. data/docs/public/images/dot_printer.png +0 -0
  27. data/docs/public/images/flame_graph.png +0 -0
  28. data/docs/public/images/flat.png +0 -0
  29. data/docs/public/images/graph.png +0 -0
  30. data/docs/public/images/graph_html.png +0 -0
  31. data/docs/public/images/ruby-prof-logo.svg +1 -0
  32. data/docs/reports.md +150 -0
  33. data/docs/stylesheets/extra.css +80 -0
  34. data/ext/ruby_prof/extconf.rb +23 -22
  35. data/ext/ruby_prof/rp_allocation.c +0 -15
  36. data/ext/ruby_prof/rp_allocation.h +29 -33
  37. data/ext/ruby_prof/rp_call_tree.c +3 -0
  38. data/ext/ruby_prof/rp_call_tree.h +1 -4
  39. data/ext/ruby_prof/rp_call_trees.c +296 -296
  40. data/ext/ruby_prof/rp_call_trees.h +25 -28
  41. data/ext/ruby_prof/rp_measure_allocations.c +47 -47
  42. data/ext/ruby_prof/rp_measure_process_time.c +64 -66
  43. data/ext/ruby_prof/rp_measure_wall_time.c +52 -64
  44. data/ext/ruby_prof/rp_measurement.c +0 -5
  45. data/ext/ruby_prof/rp_measurement.h +49 -53
  46. data/ext/ruby_prof/rp_method.c +554 -551
  47. data/ext/ruby_prof/rp_method.h +1 -4
  48. data/ext/ruby_prof/rp_profile.c +1 -1
  49. data/ext/ruby_prof/rp_profile.h +1 -5
  50. data/ext/ruby_prof/rp_stack.c +212 -212
  51. data/ext/ruby_prof/rp_stack.h +50 -53
  52. data/ext/ruby_prof/rp_thread.h +1 -4
  53. data/ext/ruby_prof/ruby_prof.c +50 -50
  54. data/ext/ruby_prof/ruby_prof.h +4 -6
  55. data/ext/ruby_prof/vc/ruby_prof.vcxproj +7 -8
  56. data/lib/ruby-prof/assets/call_stack_printer.html.erb +746 -711
  57. data/lib/ruby-prof/assets/flame_graph_printer.html.erb +412 -0
  58. data/lib/ruby-prof/assets/graph_printer.html.erb +355 -355
  59. data/lib/ruby-prof/call_tree.rb +57 -57
  60. data/lib/ruby-prof/call_tree_visitor.rb +36 -36
  61. data/lib/ruby-prof/exclude_common_methods.rb +204 -204
  62. data/lib/ruby-prof/measurement.rb +17 -17
  63. data/lib/ruby-prof/printers/abstract_printer.rb +142 -138
  64. data/lib/ruby-prof/printers/call_info_printer.rb +53 -53
  65. data/lib/ruby-prof/printers/call_stack_printer.rb +168 -180
  66. data/lib/ruby-prof/printers/call_tree_printer.rb +132 -145
  67. data/lib/ruby-prof/printers/dot_printer.rb +177 -132
  68. data/lib/ruby-prof/printers/flame_graph_printer.rb +79 -0
  69. data/lib/ruby-prof/printers/flat_printer.rb +52 -52
  70. data/lib/ruby-prof/printers/graph_html_printer.rb +62 -63
  71. data/lib/ruby-prof/printers/graph_printer.rb +112 -113
  72. data/lib/ruby-prof/printers/multi_printer.rb +134 -127
  73. data/lib/ruby-prof/profile.rb +13 -0
  74. data/lib/ruby-prof/rack.rb +114 -105
  75. data/lib/ruby-prof/task.rb +147 -147
  76. data/lib/ruby-prof/thread.rb +20 -20
  77. data/lib/ruby-prof/version.rb +3 -3
  78. data/lib/ruby-prof.rb +50 -52
  79. data/lib/unprof.rb +10 -10
  80. data/ruby-prof.gemspec +66 -65
  81. data/test/abstract_printer_test.rb +25 -27
  82. data/test/alias_test.rb +203 -117
  83. data/test/call_tree_builder.rb +126 -126
  84. data/test/call_tree_visitor_test.rb +27 -27
  85. data/test/call_trees_test.rb +66 -66
  86. data/test/duplicate_names_test.rb +32 -32
  87. data/test/dynamic_method_test.rb +50 -62
  88. data/test/enumerable_test.rb +23 -21
  89. data/test/exceptions_test.rb +24 -24
  90. data/test/exclude_methods_test.rb +363 -257
  91. data/test/exclude_threads_test.rb +48 -48
  92. data/test/fiber_test.rb +195 -195
  93. data/test/gc_test.rb +104 -102
  94. data/test/inverse_call_tree_test.rb +174 -174
  95. data/test/line_number_test.rb +563 -289
  96. data/test/marshal_test.rb +144 -145
  97. data/test/measure_allocations.rb +26 -26
  98. data/test/measure_allocations_test.rb +1511 -1081
  99. data/test/measure_process_time_test.rb +3286 -2477
  100. data/test/measure_times.rb +56 -56
  101. data/test/measure_wall_time_test.rb +773 -568
  102. data/test/measurement_test.rb +82 -82
  103. data/test/merge_test.rb +146 -146
  104. data/test/method_info_test.rb +100 -95
  105. data/test/multi_printer_test.rb +52 -66
  106. data/test/no_method_class_test.rb +15 -15
  107. data/test/pause_resume_test.rb +171 -171
  108. data/test/prime.rb +54 -54
  109. data/test/prime_script.rb +5 -5
  110. data/test/printer_call_stack_test.rb +28 -27
  111. data/test/printer_call_tree_test.rb +30 -30
  112. data/test/printer_flame_graph_test.rb +82 -0
  113. data/test/printer_flat_test.rb +99 -99
  114. data/test/printer_graph_html_test.rb +62 -59
  115. data/test/printer_graph_test.rb +42 -40
  116. data/test/printers_test.rb +162 -135
  117. data/test/printing_recursive_graph_test.rb +81 -81
  118. data/test/profile_test.rb +101 -101
  119. data/test/rack_test.rb +103 -93
  120. data/test/recursive_test.rb +796 -622
  121. data/test/scheduler.rb +4 -0
  122. data/test/singleton_test.rb +39 -38
  123. data/test/stack_printer_test.rb +61 -61
  124. data/test/start_stop_test.rb +106 -106
  125. data/test/test_helper.rb +24 -20
  126. data/test/thread_test.rb +229 -231
  127. data/test/unique_call_path_test.rb +123 -136
  128. data/test/yarv_test.rb +56 -60
  129. metadata +68 -16
  130. data/ext/ruby_prof/rp_measure_memory.c +0 -46
  131. data/lib/ruby-prof/compatibility.rb +0 -113
  132. data/test/compatibility_test.rb +0 -49
  133. data/test/crash2.rb +0 -144
  134. data/test/measure_memory_test.rb +0 -1456
@@ -1,296 +1,296 @@
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_call_trees.h"
5
- #include "rp_measurement.h"
6
-
7
- #define INITIAL_CALL_TREES_SIZE 2
8
-
9
- VALUE cRpCallTrees;
10
-
11
- /* ======= Call Infos ========*/
12
- prof_call_trees_t* prof_get_call_trees(VALUE self)
13
- {
14
- /* Can't use Data_Get_Struct because that triggers the event hook
15
- ending up in endless recursion. */
16
- prof_call_trees_t* result = RTYPEDDATA_DATA(self);
17
-
18
- if (!result)
19
- rb_raise(rb_eRuntimeError, "This RubyProf::CallTrees instance has already been freed, likely because its profile has been freed.");
20
-
21
- return result;
22
- }
23
-
24
- prof_call_trees_t* prof_call_trees_create(void)
25
- {
26
- prof_call_trees_t* result = ALLOC(prof_call_trees_t);
27
- result->start = ALLOC_N(prof_call_tree_t*, INITIAL_CALL_TREES_SIZE);
28
- result->end = result->start + INITIAL_CALL_TREES_SIZE;
29
- result->ptr = result->start;
30
- result->object = Qnil;
31
- return result;
32
- }
33
-
34
- void prof_call_trees_mark(void* data)
35
- {
36
- if (!data) return;
37
-
38
- prof_call_trees_t* call_trees = (prof_call_trees_t*)data;
39
- prof_call_tree_t** call_tree;
40
- for (call_tree = call_trees->start; call_tree < call_trees->ptr; call_tree++)
41
- {
42
- prof_call_tree_mark(*call_tree);
43
- }
44
- }
45
-
46
- void prof_call_trees_free(prof_call_trees_t* call_trees)
47
- {
48
- /* Has this method object been accessed by Ruby? If
49
- yes clean it up so to avoid a segmentation fault. */
50
- if (call_trees->object != Qnil)
51
- {
52
- RTYPEDDATA(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
- if (data)
63
- {
64
- // This object gets freed by its owning method
65
- prof_call_trees_t* call_trees = (prof_call_trees_t*)data;
66
- call_trees->object = Qnil;
67
- }
68
- }
69
-
70
- static int prof_call_trees_collect(st_data_t key, st_data_t value, st_data_t data)
71
- {
72
- VALUE result = (VALUE)data;
73
- prof_call_tree_t* call_tree_data = (prof_call_tree_t*)value;
74
- VALUE aggregate_call_tree = prof_call_tree_wrap(call_tree_data);
75
- rb_ary_push(result, aggregate_call_tree);
76
-
77
- return ST_CONTINUE;
78
- }
79
-
80
- static int prof_call_trees_collect_callees(st_data_t key, st_data_t value, st_data_t hash)
81
- {
82
- st_table* callers = (st_table*)hash;
83
- prof_call_tree_t* call_tree_data = (prof_call_tree_t*)value;
84
-
85
- prof_call_tree_t* aggregate_call_tree_data = NULL;
86
-
87
- if (rb_st_lookup(callers, call_tree_data->method->key, (st_data_t*)&aggregate_call_tree_data))
88
- {
89
- prof_measurement_merge_internal(aggregate_call_tree_data->measurement, call_tree_data->measurement);
90
- }
91
- else
92
- {
93
- // Copy the call tree so we don't touch the original and give Ruby ownerhip
94
- // of it so that it is freed on GC
95
- aggregate_call_tree_data = prof_call_tree_copy(call_tree_data);
96
- aggregate_call_tree_data->owner = OWNER_RUBY;
97
-
98
-
99
- rb_st_insert(callers, call_tree_data->method->key, (st_data_t)aggregate_call_tree_data);
100
- }
101
-
102
- return ST_CONTINUE;
103
- }
104
-
105
- size_t prof_call_trees_size(const void* data)
106
- {
107
- return sizeof(prof_call_trees_t);
108
- }
109
-
110
- static const rb_data_type_t call_trees_type =
111
- {
112
- .wrap_struct_name = "CallTrees",
113
- .function =
114
- {
115
- .dmark = prof_call_trees_mark,
116
- .dfree = prof_call_trees_ruby_gc_free,
117
- .dsize = prof_call_trees_size,
118
- },
119
- .data = NULL,
120
- .flags = RUBY_TYPED_FREE_IMMEDIATELY
121
- };
122
-
123
- VALUE prof_call_trees_wrap(prof_call_trees_t* call_trees)
124
- {
125
- if (call_trees->object == Qnil)
126
- {
127
- call_trees->object = TypedData_Wrap_Struct(cRpCallTrees, &call_trees_type, call_trees);
128
- }
129
- return call_trees->object;
130
- }
131
-
132
- void prof_add_call_tree(prof_call_trees_t* call_trees, prof_call_tree_t* call_tree)
133
- {
134
- if (call_trees->ptr == call_trees->end)
135
- {
136
- size_t len = call_trees->ptr - call_trees->start;
137
- size_t new_capacity = (call_trees->end - call_trees->start) * 2;
138
- REALLOC_N(call_trees->start, prof_call_tree_t*, new_capacity);
139
- call_trees->ptr = call_trees->start + len;
140
- call_trees->end = call_trees->start + new_capacity;
141
- }
142
- *call_trees->ptr = call_tree;
143
- call_trees->ptr++;
144
- }
145
-
146
- /* ================ Call Infos =================*/
147
- /* Document-class: RubyProf::CallTrees
148
- The RubyProf::MethodInfo class stores profiling data for a method.
149
- One instance of the RubyProf::MethodInfo class is created per method
150
- called per thread. Thus, if a method is called in two different
151
- thread then there will be two RubyProf::MethodInfo objects
152
- created. RubyProf::MethodInfo objects can be accessed via
153
- the RubyProf::Profile object. */
154
- VALUE prof_call_trees_allocate(VALUE klass)
155
- {
156
- prof_call_trees_t* call_trees_data = prof_call_trees_create();
157
- call_trees_data->object = prof_call_trees_wrap(call_trees_data);
158
- return call_trees_data->object;
159
- }
160
-
161
-
162
- /* call-seq:
163
- min_depth -> Integer
164
-
165
- Returns the minimum depth of this method in any call tree */
166
- VALUE prof_call_trees_min_depth(VALUE self)
167
- {
168
- unsigned int depth = INT_MAX;
169
-
170
- prof_call_trees_t* call_trees = prof_get_call_trees(self);
171
- for (prof_call_tree_t** p_call_tree = call_trees->start; p_call_tree < call_trees->ptr; p_call_tree++)
172
- {
173
- unsigned int call_tree_depth = prof_call_tree_figure_depth(*p_call_tree);
174
- if (call_tree_depth < depth)
175
- depth = call_tree_depth;
176
- }
177
-
178
- return UINT2NUM(depth);
179
- }
180
-
181
- /* call-seq:
182
- callers -> array
183
-
184
- Returns an array of all CallTree objects that called this method. */
185
- VALUE prof_call_trees_call_trees(VALUE self)
186
- {
187
- VALUE result = rb_ary_new();
188
-
189
- prof_call_trees_t* call_trees = prof_get_call_trees(self);
190
- for (prof_call_tree_t** p_call_tree = call_trees->start; p_call_tree < call_trees->ptr; p_call_tree++)
191
- {
192
- VALUE call_tree = prof_call_tree_wrap(*p_call_tree);
193
- rb_ary_push(result, call_tree);
194
- }
195
- return result;
196
- }
197
-
198
- /* call-seq:
199
- callers -> array
200
-
201
- Returns an array of aggregated CallTree objects that called this method (ie, parents).*/
202
- VALUE prof_call_trees_callers(VALUE self)
203
- {
204
- st_table* callers = rb_st_init_numtable();
205
-
206
- prof_call_trees_t* call_trees = prof_get_call_trees(self);
207
- for (prof_call_tree_t** p_call_tree = call_trees->start; p_call_tree < call_trees->ptr; p_call_tree++)
208
- {
209
- prof_call_tree_t* parent = (*p_call_tree)->parent;
210
- if (parent == NULL)
211
- continue;
212
-
213
- prof_call_tree_t* aggregate_call_tree_data = NULL;
214
-
215
- if (rb_st_lookup(callers, parent->method->key, (st_data_t*)&aggregate_call_tree_data))
216
- {
217
- prof_measurement_merge_internal(aggregate_call_tree_data->measurement, (*p_call_tree)->measurement);
218
- }
219
- else
220
- {
221
- // Copy the call tree so we don't touch the original and give Ruby ownerhip
222
- // of it so that it is freed on GC
223
- aggregate_call_tree_data = prof_call_tree_copy(*p_call_tree);
224
- aggregate_call_tree_data->owner = OWNER_RUBY;
225
-
226
- rb_st_insert(callers, parent->method->key, (st_data_t)aggregate_call_tree_data);
227
- }
228
- }
229
-
230
- VALUE result = rb_ary_new_capa((long)callers->num_entries);
231
- rb_st_foreach(callers, prof_call_trees_collect, result);
232
- rb_st_free_table(callers);
233
- return result;
234
- }
235
-
236
- /* call-seq:
237
- callees -> array
238
-
239
- Returns an array of aggregated CallTree objects that this method called (ie, children).*/
240
- VALUE prof_call_trees_callees(VALUE self)
241
- {
242
- st_table* callees = rb_st_init_numtable();
243
-
244
- prof_call_trees_t* call_trees = prof_get_call_trees(self);
245
- for (prof_call_tree_t** call_tree = call_trees->start; call_tree < call_trees->ptr; call_tree++)
246
- {
247
- rb_st_foreach((*call_tree)->children, prof_call_trees_collect_callees, (st_data_t)callees);
248
- }
249
-
250
- VALUE result = rb_ary_new_capa((long)callees->num_entries);
251
- rb_st_foreach(callees, prof_call_trees_collect, result);
252
- rb_st_free_table(callees);
253
- return result;
254
- }
255
-
256
- /* :nodoc: */
257
- VALUE prof_call_trees_dump(VALUE self)
258
- {
259
- VALUE result = rb_hash_new();
260
- rb_hash_aset(result, ID2SYM(rb_intern("call_trees")), prof_call_trees_call_trees(self));
261
-
262
- return result;
263
- }
264
-
265
- /* :nodoc: */
266
- VALUE prof_call_trees_load(VALUE self, VALUE data)
267
- {
268
- prof_call_trees_t* call_trees_data = prof_get_call_trees(self);
269
- call_trees_data->object = self;
270
-
271
- VALUE call_trees = rb_hash_aref(data, ID2SYM(rb_intern("call_trees")));
272
- for (int i = 0; i < rb_array_len(call_trees); i++)
273
- {
274
- VALUE call_tree = rb_ary_entry(call_trees, i);
275
- prof_call_tree_t* call_tree_data = prof_get_call_tree(call_tree);
276
- prof_add_call_tree(call_trees_data, call_tree_data);
277
- }
278
-
279
- return data;
280
- }
281
-
282
- void rp_init_call_trees(void)
283
- {
284
- cRpCallTrees = rb_define_class_under(mProf, "CallTrees", rb_cObject);
285
- rb_undef_method(CLASS_OF(cRpCallTrees), "new");
286
- rb_define_alloc_func(cRpCallTrees, prof_call_trees_allocate);
287
-
288
- rb_define_method(cRpCallTrees, "min_depth", prof_call_trees_min_depth, 0);
289
-
290
- rb_define_method(cRpCallTrees, "call_trees", prof_call_trees_call_trees, 0);
291
- rb_define_method(cRpCallTrees, "callers", prof_call_trees_callers, 0);
292
- rb_define_method(cRpCallTrees, "callees", prof_call_trees_callees, 0);
293
-
294
- rb_define_method(cRpCallTrees, "_dump_data", prof_call_trees_dump, 0);
295
- rb_define_method(cRpCallTrees, "_load_data", prof_call_trees_load, 1);
296
- }
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_call_trees.h"
5
+ #include "rp_measurement.h"
6
+
7
+ #define INITIAL_CALL_TREES_SIZE 2
8
+
9
+ VALUE cRpCallTrees;
10
+
11
+ /* ======= Call Infos ========*/
12
+ prof_call_trees_t* prof_get_call_trees(VALUE self)
13
+ {
14
+ /* Can't use Data_Get_Struct because that triggers the event hook
15
+ ending up in endless recursion. */
16
+ prof_call_trees_t* result = RTYPEDDATA_DATA(self);
17
+
18
+ if (!result)
19
+ rb_raise(rb_eRuntimeError, "This RubyProf::CallTrees instance has already been freed, likely because its profile has been freed.");
20
+
21
+ return result;
22
+ }
23
+
24
+ prof_call_trees_t* prof_call_trees_create(void)
25
+ {
26
+ prof_call_trees_t* result = ALLOC(prof_call_trees_t);
27
+ result->start = ALLOC_N(prof_call_tree_t*, INITIAL_CALL_TREES_SIZE);
28
+ result->end = result->start + INITIAL_CALL_TREES_SIZE;
29
+ result->ptr = result->start;
30
+ result->object = Qnil;
31
+ return result;
32
+ }
33
+
34
+ void prof_call_trees_mark(void* data)
35
+ {
36
+ if (!data) return;
37
+
38
+ prof_call_trees_t* call_trees = (prof_call_trees_t*)data;
39
+ prof_call_tree_t** call_tree;
40
+ for (call_tree = call_trees->start; call_tree < call_trees->ptr; call_tree++)
41
+ {
42
+ prof_call_tree_mark(*call_tree);
43
+ }
44
+ }
45
+
46
+ void prof_call_trees_free(prof_call_trees_t* call_trees)
47
+ {
48
+ /* Has this method object been accessed by Ruby? If
49
+ yes clean it up so to avoid a segmentation fault. */
50
+ if (call_trees->object != Qnil)
51
+ {
52
+ RTYPEDDATA(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
+ if (data)
63
+ {
64
+ // This object gets freed by its owning method
65
+ prof_call_trees_t* call_trees = (prof_call_trees_t*)data;
66
+ call_trees->object = Qnil;
67
+ }
68
+ }
69
+
70
+ static int prof_call_trees_collect(st_data_t key, st_data_t value, st_data_t data)
71
+ {
72
+ VALUE result = (VALUE)data;
73
+ prof_call_tree_t* call_tree_data = (prof_call_tree_t*)value;
74
+ VALUE aggregate_call_tree = prof_call_tree_wrap(call_tree_data);
75
+ rb_ary_push(result, aggregate_call_tree);
76
+
77
+ return ST_CONTINUE;
78
+ }
79
+
80
+ static int prof_call_trees_collect_callees(st_data_t key, st_data_t value, st_data_t hash)
81
+ {
82
+ st_table* callers = (st_table*)hash;
83
+ prof_call_tree_t* call_tree_data = (prof_call_tree_t*)value;
84
+
85
+ prof_call_tree_t* aggregate_call_tree_data = NULL;
86
+
87
+ if (rb_st_lookup(callers, call_tree_data->method->key, (st_data_t*)&aggregate_call_tree_data))
88
+ {
89
+ prof_measurement_merge_internal(aggregate_call_tree_data->measurement, call_tree_data->measurement);
90
+ }
91
+ else
92
+ {
93
+ // Copy the call tree so we don't touch the original and give Ruby ownerhip
94
+ // of it so that it is freed on GC
95
+ aggregate_call_tree_data = prof_call_tree_copy(call_tree_data);
96
+ aggregate_call_tree_data->owner = OWNER_RUBY;
97
+
98
+
99
+ rb_st_insert(callers, call_tree_data->method->key, (st_data_t)aggregate_call_tree_data);
100
+ }
101
+
102
+ return ST_CONTINUE;
103
+ }
104
+
105
+ size_t prof_call_trees_size(const void* data)
106
+ {
107
+ return sizeof(prof_call_trees_t);
108
+ }
109
+
110
+ static const rb_data_type_t call_trees_type =
111
+ {
112
+ .wrap_struct_name = "CallTrees",
113
+ .function =
114
+ {
115
+ .dmark = prof_call_trees_mark,
116
+ .dfree = prof_call_trees_ruby_gc_free,
117
+ .dsize = prof_call_trees_size,
118
+ },
119
+ .data = NULL,
120
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY
121
+ };
122
+
123
+ VALUE prof_call_trees_wrap(prof_call_trees_t* call_trees)
124
+ {
125
+ if (call_trees->object == Qnil)
126
+ {
127
+ call_trees->object = TypedData_Wrap_Struct(cRpCallTrees, &call_trees_type, call_trees);
128
+ }
129
+ return call_trees->object;
130
+ }
131
+
132
+ void prof_add_call_tree(prof_call_trees_t* call_trees, prof_call_tree_t* call_tree)
133
+ {
134
+ if (call_trees->ptr == call_trees->end)
135
+ {
136
+ size_t len = call_trees->ptr - call_trees->start;
137
+ size_t new_capacity = (call_trees->end - call_trees->start) * 2;
138
+ REALLOC_N(call_trees->start, prof_call_tree_t*, new_capacity);
139
+ call_trees->ptr = call_trees->start + len;
140
+ call_trees->end = call_trees->start + new_capacity;
141
+ }
142
+ *call_trees->ptr = call_tree;
143
+ call_trees->ptr++;
144
+ }
145
+
146
+ /* ================ Call Infos =================*/
147
+ /* Document-class: RubyProf::CallTrees
148
+ The RubyProf::MethodInfo class stores profiling data for a method.
149
+ One instance of the RubyProf::MethodInfo class is created per method
150
+ called per thread. Thus, if a method is called in two different
151
+ thread then there will be two RubyProf::MethodInfo objects
152
+ created. RubyProf::MethodInfo objects can be accessed via
153
+ the RubyProf::Profile object. */
154
+ VALUE prof_call_trees_allocate(VALUE klass)
155
+ {
156
+ prof_call_trees_t* call_trees_data = prof_call_trees_create();
157
+ call_trees_data->object = prof_call_trees_wrap(call_trees_data);
158
+ return call_trees_data->object;
159
+ }
160
+
161
+
162
+ /* call-seq:
163
+ min_depth -> Integer
164
+
165
+ Returns the minimum depth of this method in any call tree */
166
+ VALUE prof_call_trees_min_depth(VALUE self)
167
+ {
168
+ unsigned int depth = INT_MAX;
169
+
170
+ prof_call_trees_t* call_trees = prof_get_call_trees(self);
171
+ for (prof_call_tree_t** p_call_tree = call_trees->start; p_call_tree < call_trees->ptr; p_call_tree++)
172
+ {
173
+ unsigned int call_tree_depth = prof_call_tree_figure_depth(*p_call_tree);
174
+ if (call_tree_depth < depth)
175
+ depth = call_tree_depth;
176
+ }
177
+
178
+ return UINT2NUM(depth);
179
+ }
180
+
181
+ /* call-seq:
182
+ callers -> array
183
+
184
+ Returns an array of all CallTree objects that called this method. */
185
+ VALUE prof_call_trees_call_trees(VALUE self)
186
+ {
187
+ VALUE result = rb_ary_new();
188
+
189
+ prof_call_trees_t* call_trees = prof_get_call_trees(self);
190
+ for (prof_call_tree_t** p_call_tree = call_trees->start; p_call_tree < call_trees->ptr; p_call_tree++)
191
+ {
192
+ VALUE call_tree = prof_call_tree_wrap(*p_call_tree);
193
+ rb_ary_push(result, call_tree);
194
+ }
195
+ return result;
196
+ }
197
+
198
+ /* call-seq:
199
+ callers -> array
200
+
201
+ Returns an array of aggregated CallTree objects that called this method (ie, parents).*/
202
+ VALUE prof_call_trees_callers(VALUE self)
203
+ {
204
+ st_table* callers = rb_st_init_numtable();
205
+
206
+ prof_call_trees_t* call_trees = prof_get_call_trees(self);
207
+ for (prof_call_tree_t** p_call_tree = call_trees->start; p_call_tree < call_trees->ptr; p_call_tree++)
208
+ {
209
+ prof_call_tree_t* parent = (*p_call_tree)->parent;
210
+ if (parent == NULL)
211
+ continue;
212
+
213
+ prof_call_tree_t* aggregate_call_tree_data = NULL;
214
+
215
+ if (rb_st_lookup(callers, parent->method->key, (st_data_t*)&aggregate_call_tree_data))
216
+ {
217
+ prof_measurement_merge_internal(aggregate_call_tree_data->measurement, (*p_call_tree)->measurement);
218
+ }
219
+ else
220
+ {
221
+ // Copy the call tree so we don't touch the original and give Ruby ownerhip
222
+ // of it so that it is freed on GC
223
+ aggregate_call_tree_data = prof_call_tree_copy(*p_call_tree);
224
+ aggregate_call_tree_data->owner = OWNER_RUBY;
225
+
226
+ rb_st_insert(callers, parent->method->key, (st_data_t)aggregate_call_tree_data);
227
+ }
228
+ }
229
+
230
+ VALUE result = rb_ary_new_capa((long)callers->num_entries);
231
+ rb_st_foreach(callers, prof_call_trees_collect, result);
232
+ rb_st_free_table(callers);
233
+ return result;
234
+ }
235
+
236
+ /* call-seq:
237
+ callees -> array
238
+
239
+ Returns an array of aggregated CallTree objects that this method called (ie, children).*/
240
+ VALUE prof_call_trees_callees(VALUE self)
241
+ {
242
+ st_table* callees = rb_st_init_numtable();
243
+
244
+ prof_call_trees_t* call_trees = prof_get_call_trees(self);
245
+ for (prof_call_tree_t** call_tree = call_trees->start; call_tree < call_trees->ptr; call_tree++)
246
+ {
247
+ rb_st_foreach((*call_tree)->children, prof_call_trees_collect_callees, (st_data_t)callees);
248
+ }
249
+
250
+ VALUE result = rb_ary_new_capa((long)callees->num_entries);
251
+ rb_st_foreach(callees, prof_call_trees_collect, result);
252
+ rb_st_free_table(callees);
253
+ return result;
254
+ }
255
+
256
+ /* :nodoc: */
257
+ VALUE prof_call_trees_dump(VALUE self)
258
+ {
259
+ VALUE result = rb_hash_new();
260
+ rb_hash_aset(result, ID2SYM(rb_intern("call_trees")), prof_call_trees_call_trees(self));
261
+
262
+ return result;
263
+ }
264
+
265
+ /* :nodoc: */
266
+ VALUE prof_call_trees_load(VALUE self, VALUE data)
267
+ {
268
+ prof_call_trees_t* call_trees_data = prof_get_call_trees(self);
269
+ call_trees_data->object = self;
270
+
271
+ VALUE call_trees = rb_hash_aref(data, ID2SYM(rb_intern("call_trees")));
272
+ for (int i = 0; i < rb_array_len(call_trees); i++)
273
+ {
274
+ VALUE call_tree = rb_ary_entry(call_trees, i);
275
+ prof_call_tree_t* call_tree_data = prof_get_call_tree(call_tree);
276
+ prof_add_call_tree(call_trees_data, call_tree_data);
277
+ }
278
+
279
+ return data;
280
+ }
281
+
282
+ void rp_init_call_trees(void)
283
+ {
284
+ cRpCallTrees = rb_define_class_under(mProf, "CallTrees", rb_cObject);
285
+ rb_undef_method(CLASS_OF(cRpCallTrees), "new");
286
+ rb_define_alloc_func(cRpCallTrees, prof_call_trees_allocate);
287
+
288
+ rb_define_method(cRpCallTrees, "min_depth", prof_call_trees_min_depth, 0);
289
+
290
+ rb_define_method(cRpCallTrees, "call_trees", prof_call_trees_call_trees, 0);
291
+ rb_define_method(cRpCallTrees, "callers", prof_call_trees_callers, 0);
292
+ rb_define_method(cRpCallTrees, "callees", prof_call_trees_callees, 0);
293
+
294
+ rb_define_method(cRpCallTrees, "_dump_data", prof_call_trees_dump, 0);
295
+ rb_define_method(cRpCallTrees, "_load_data", prof_call_trees_load, 1);
296
+ }
@@ -1,28 +1,25 @@
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
- #ifndef __RP_CALL_TREES_H__
5
- #define __RP_CALL_TREES_H__
6
-
7
- #include "ruby_prof.h"
8
- #include "rp_call_tree.h"
9
-
10
- /* Array of call_tree objects */
11
- typedef struct prof_call_trees_t
12
- {
13
- prof_call_tree_t** start;
14
- prof_call_tree_t** end;
15
- prof_call_tree_t** ptr;
16
-
17
- VALUE object;
18
- } prof_call_trees_t;
19
-
20
-
21
- void rp_init_call_trees(void);
22
- prof_call_trees_t* prof_call_trees_create(void);
23
- void prof_call_trees_free(prof_call_trees_t* call_trees);
24
- prof_call_trees_t* prof_get_call_trees(VALUE self);
25
- void prof_add_call_tree(prof_call_trees_t* call_trees, prof_call_tree_t* call_tree);
26
- VALUE prof_call_trees_wrap(prof_call_trees_t* call_trees);
27
-
28
- #endif //__RP_CALL_TREES_H__
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
+ #pragma once
5
+
6
+ #include "ruby_prof.h"
7
+ #include "rp_call_tree.h"
8
+
9
+ /* Array of call_tree objects */
10
+ typedef struct prof_call_trees_t
11
+ {
12
+ prof_call_tree_t** start;
13
+ prof_call_tree_t** end;
14
+ prof_call_tree_t** ptr;
15
+
16
+ VALUE object;
17
+ } prof_call_trees_t;
18
+
19
+
20
+ void rp_init_call_trees(void);
21
+ prof_call_trees_t* prof_call_trees_create(void);
22
+ void prof_call_trees_free(prof_call_trees_t* call_trees);
23
+ prof_call_trees_t* prof_get_call_trees(VALUE self);
24
+ void prof_add_call_tree(prof_call_trees_t* call_trees, prof_call_tree_t* call_tree);
25
+ VALUE prof_call_trees_wrap(prof_call_trees_t* call_trees);