ruby-prof 0.13.1 → 1.4.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (209) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGES +579 -371
  3. data/LICENSE +24 -23
  4. data/README.rdoc +5 -433
  5. data/Rakefile +98 -110
  6. data/bin/ruby-prof +328 -329
  7. data/bin/ruby-prof-check-trace +45 -0
  8. data/ext/ruby_prof/extconf.rb +16 -59
  9. data/ext/ruby_prof/rp_aggregate_call_tree.c +59 -0
  10. data/ext/ruby_prof/rp_aggregate_call_tree.h +13 -0
  11. data/ext/ruby_prof/rp_allocation.c +287 -0
  12. data/ext/ruby_prof/rp_allocation.h +31 -0
  13. data/ext/ruby_prof/rp_call_tree.c +369 -0
  14. data/ext/ruby_prof/rp_call_tree.h +43 -0
  15. data/ext/ruby_prof/rp_call_trees.c +288 -0
  16. data/ext/ruby_prof/rp_call_trees.h +28 -0
  17. data/ext/ruby_prof/rp_measure_allocations.c +50 -65
  18. data/ext/ruby_prof/rp_measure_memory.c +42 -73
  19. data/ext/ruby_prof/rp_measure_process_time.c +65 -71
  20. data/ext/ruby_prof/rp_measure_wall_time.c +64 -42
  21. data/ext/ruby_prof/rp_measurement.c +237 -0
  22. data/ext/ruby_prof/rp_measurement.h +50 -0
  23. data/ext/ruby_prof/rp_method.c +491 -420
  24. data/ext/ruby_prof/rp_method.h +62 -57
  25. data/ext/ruby_prof/rp_profile.c +908 -0
  26. data/ext/ruby_prof/rp_profile.h +35 -0
  27. data/ext/ruby_prof/rp_stack.c +212 -128
  28. data/ext/ruby_prof/rp_stack.h +53 -51
  29. data/ext/ruby_prof/rp_thread.c +362 -268
  30. data/ext/ruby_prof/rp_thread.h +39 -27
  31. data/ext/ruby_prof/ruby_prof.c +52 -695
  32. data/ext/ruby_prof/ruby_prof.h +26 -55
  33. data/ext/ruby_prof/vc/ruby_prof.sln +28 -21
  34. data/ext/ruby_prof/vc/{ruby_prof_20.vcxproj → ruby_prof.vcxproj} +56 -8
  35. data/lib/ruby-prof.rb +52 -67
  36. data/lib/ruby-prof/assets/call_stack_printer.html.erb +710 -0
  37. data/lib/ruby-prof/assets/call_stack_printer.png +0 -0
  38. data/lib/ruby-prof/assets/graph_printer.html.erb +355 -0
  39. data/lib/ruby-prof/call_tree.rb +57 -0
  40. data/lib/ruby-prof/call_tree_visitor.rb +36 -0
  41. data/lib/ruby-prof/compatibility.rb +99 -169
  42. data/lib/ruby-prof/exclude_common_methods.rb +198 -0
  43. data/lib/ruby-prof/measurement.rb +17 -0
  44. data/lib/ruby-prof/method_info.rb +78 -131
  45. data/lib/ruby-prof/printers/abstract_printer.rb +137 -85
  46. data/lib/ruby-prof/printers/call_info_printer.rb +53 -41
  47. data/lib/ruby-prof/printers/call_stack_printer.rb +180 -773
  48. data/lib/ruby-prof/printers/call_tree_printer.rb +151 -92
  49. data/lib/ruby-prof/printers/dot_printer.rb +132 -132
  50. data/lib/ruby-prof/printers/flat_printer.rb +53 -69
  51. data/lib/ruby-prof/printers/graph_html_printer.rb +63 -255
  52. data/lib/ruby-prof/printers/graph_printer.rb +113 -116
  53. data/lib/ruby-prof/printers/multi_printer.rb +127 -56
  54. data/lib/ruby-prof/profile.rb +37 -77
  55. data/lib/ruby-prof/rack.rb +62 -15
  56. data/lib/ruby-prof/task.rb +147 -147
  57. data/lib/ruby-prof/thread.rb +10 -12
  58. data/lib/ruby-prof/version.rb +3 -0
  59. data/lib/unprof.rb +10 -10
  60. data/ruby-prof.gemspec +65 -61
  61. data/test/abstract_printer_test.rb +26 -0
  62. data/test/alias_test.rb +126 -0
  63. data/test/basic_test.rb +43 -128
  64. data/test/call_tree_visitor_test.rb +32 -0
  65. data/test/call_trees_test.rb +66 -0
  66. data/test/duplicate_names_test.rb +32 -32
  67. data/test/dynamic_method_test.rb +53 -74
  68. data/test/enumerable_test.rb +21 -16
  69. data/test/exceptions_test.rb +24 -16
  70. data/test/exclude_methods_test.rb +151 -0
  71. data/test/exclude_threads_test.rb +53 -54
  72. data/test/fiber_test.rb +129 -65
  73. data/test/gc_test.rb +90 -0
  74. data/test/inverse_call_tree_test.rb +175 -0
  75. data/test/line_number_test.rb +158 -71
  76. data/test/marshal_test.rb +113 -0
  77. data/test/measure_allocations.rb +30 -0
  78. data/test/measure_allocations_test.rb +375 -25
  79. data/test/measure_allocations_trace_test.rb +375 -0
  80. data/test/measure_memory_trace_test.rb +1101 -0
  81. data/test/measure_process_time_test.rb +785 -62
  82. data/test/measure_times.rb +56 -0
  83. data/test/measure_wall_time_test.rb +434 -254
  84. data/test/multi_printer_test.rb +71 -82
  85. data/test/no_method_class_test.rb +15 -15
  86. data/test/pause_resume_test.rb +175 -166
  87. data/test/prime.rb +54 -54
  88. data/test/prime_script.rb +6 -0
  89. data/test/printer_call_stack_test.rb +27 -0
  90. data/test/printer_call_tree_test.rb +30 -0
  91. data/test/printer_flat_test.rb +99 -0
  92. data/test/printer_graph_html_test.rb +59 -0
  93. data/test/printer_graph_test.rb +40 -0
  94. data/test/printers_test.rb +141 -257
  95. data/test/printing_recursive_graph_test.rb +81 -0
  96. data/test/profile_test.rb +16 -0
  97. data/test/rack_test.rb +93 -0
  98. data/test/recursive_test.rb +206 -215
  99. data/test/singleton_test.rb +38 -38
  100. data/test/stack_printer_test.rb +64 -78
  101. data/test/start_stop_test.rb +109 -112
  102. data/test/test_helper.rb +13 -115
  103. data/test/thread_test.rb +144 -178
  104. data/test/unique_call_path_test.rb +120 -224
  105. data/test/yarv_test.rb +56 -0
  106. metadata +77 -133
  107. data/doc/LICENSE.html +0 -155
  108. data/doc/README_rdoc.html +0 -648
  109. data/doc/Rack.html +0 -167
  110. data/doc/Rack/RubyProf.html +0 -319
  111. data/doc/RubyProf.html +0 -1000
  112. data/doc/RubyProf/AbstractPrinter.html +0 -580
  113. data/doc/RubyProf/AggregateCallInfo.html +0 -570
  114. data/doc/RubyProf/CallInfo.html +0 -512
  115. data/doc/RubyProf/CallInfoPrinter.html +0 -190
  116. data/doc/RubyProf/CallInfoVisitor.html +0 -332
  117. data/doc/RubyProf/CallStackPrinter.html +0 -1600
  118. data/doc/RubyProf/CallTreePrinter.html +0 -413
  119. data/doc/RubyProf/Cmd.html +0 -669
  120. data/doc/RubyProf/DotPrinter.html +0 -312
  121. data/doc/RubyProf/FlatPrinter.html +0 -229
  122. data/doc/RubyProf/FlatPrinterWithLineNumbers.html +0 -267
  123. data/doc/RubyProf/GraphHtmlPrinter.html +0 -630
  124. data/doc/RubyProf/GraphPrinter.html +0 -209
  125. data/doc/RubyProf/MethodInfo.html +0 -713
  126. data/doc/RubyProf/MultiPrinter.html +0 -407
  127. data/doc/RubyProf/Profile.html +0 -821
  128. data/doc/RubyProf/ProfileTask.html +0 -532
  129. data/doc/RubyProf/Test.html +0 -578
  130. data/doc/RubyProf/Thread.html +0 -262
  131. data/doc/created.rid +0 -32
  132. data/doc/examples/flat_txt.html +0 -191
  133. data/doc/examples/graph_txt.html +0 -305
  134. data/doc/images/add.png +0 -0
  135. data/doc/images/brick.png +0 -0
  136. data/doc/images/brick_link.png +0 -0
  137. data/doc/images/bug.png +0 -0
  138. data/doc/images/bullet_black.png +0 -0
  139. data/doc/images/bullet_toggle_minus.png +0 -0
  140. data/doc/images/bullet_toggle_plus.png +0 -0
  141. data/doc/images/date.png +0 -0
  142. data/doc/images/delete.png +0 -0
  143. data/doc/images/find.png +0 -0
  144. data/doc/images/loadingAnimation.gif +0 -0
  145. data/doc/images/macFFBgHack.png +0 -0
  146. data/doc/images/package.png +0 -0
  147. data/doc/images/page_green.png +0 -0
  148. data/doc/images/page_white_text.png +0 -0
  149. data/doc/images/page_white_width.png +0 -0
  150. data/doc/images/plugin.png +0 -0
  151. data/doc/images/ruby.png +0 -0
  152. data/doc/images/tag_blue.png +0 -0
  153. data/doc/images/tag_green.png +0 -0
  154. data/doc/images/transparent.png +0 -0
  155. data/doc/images/wrench.png +0 -0
  156. data/doc/images/wrench_orange.png +0 -0
  157. data/doc/images/zoom.png +0 -0
  158. data/doc/index.html +0 -647
  159. data/doc/js/darkfish.js +0 -155
  160. data/doc/js/jquery.js +0 -18
  161. data/doc/js/navigation.js +0 -142
  162. data/doc/js/search.js +0 -94
  163. data/doc/js/search_index.js +0 -1
  164. data/doc/js/searcher.js +0 -228
  165. data/doc/rdoc.css +0 -543
  166. data/doc/table_of_contents.html +0 -462
  167. data/examples/empty.png +0 -0
  168. data/examples/flat.txt +0 -55
  169. data/examples/graph.dot +0 -106
  170. data/examples/graph.html +0 -823
  171. data/examples/graph.png +0 -0
  172. data/examples/graph.txt +0 -170
  173. data/examples/minus.png +0 -0
  174. data/examples/multi.flat.txt +0 -23
  175. data/examples/multi.graph.html +0 -906
  176. data/examples/multi.grind.dat +0 -194
  177. data/examples/multi.stack.html +0 -573
  178. data/examples/plus.png +0 -0
  179. data/examples/stack.html +0 -573
  180. data/ext/ruby_prof/rp_call_info.c +0 -407
  181. data/ext/ruby_prof/rp_call_info.h +0 -48
  182. data/ext/ruby_prof/rp_measure.c +0 -48
  183. data/ext/ruby_prof/rp_measure.h +0 -45
  184. data/ext/ruby_prof/rp_measure_cpu_time.c +0 -112
  185. data/ext/ruby_prof/rp_measure_gc_runs.c +0 -65
  186. data/ext/ruby_prof/rp_measure_gc_time.c +0 -57
  187. data/ext/ruby_prof/vc/ruby_prof_18.vcxproj +0 -108
  188. data/ext/ruby_prof/vc/ruby_prof_19.vcxproj +0 -110
  189. data/ext/ruby_prof/version.h +0 -7
  190. data/lib/ruby-prof/aggregate_call_info.rb +0 -72
  191. data/lib/ruby-prof/call_info.rb +0 -89
  192. data/lib/ruby-prof/call_info_visitor.rb +0 -44
  193. data/lib/ruby-prof/images/empty.png +0 -0
  194. data/lib/ruby-prof/images/minus.png +0 -0
  195. data/lib/ruby-prof/images/plus.png +0 -0
  196. data/lib/ruby-prof/printers/flat_printer_with_line_numbers.rb +0 -57
  197. data/lib/ruby-prof/test.rb +0 -150
  198. data/test/aggregate_test.rb +0 -136
  199. data/test/call_info_test.rb +0 -78
  200. data/test/call_info_visitor_test.rb +0 -31
  201. data/test/exec_test.rb +0 -14
  202. data/test/measure_cpu_time_test.rb +0 -220
  203. data/test/measure_gc_runs_test.rb +0 -32
  204. data/test/measure_gc_time_test.rb +0 -36
  205. data/test/measure_memory_test.rb +0 -31
  206. data/test/method_elimination_test.rb +0 -84
  207. data/test/module_test.rb +0 -45
  208. data/test/stack_test.rb +0 -138
  209. data/test/test_suite.rb +0 -37
@@ -0,0 +1,31 @@
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_ALLOCATION_
5
+ #define _RP_ALLOCATION_
6
+
7
+ #include "ruby_prof.h"
8
+ #include "rp_method.h"
9
+
10
+ typedef struct prof_allocation_t
11
+ {
12
+ st_data_t key; /* Key in hash table */
13
+ unsigned int klass_flags; /* Information about the type of class */
14
+ VALUE klass; /* Klass that was created */
15
+ VALUE klass_name; /* Name of the class that was created */
16
+ VALUE source_file; /* Line number where allocation happens */
17
+ int source_line; /* Line number where allocation happens */
18
+ int count; /* Number of allocations */
19
+ size_t memory; /* Amount of allocated memory */
20
+ VALUE object; /* Cache to wrapped object */
21
+ } prof_allocation_t;
22
+
23
+ void rp_init_allocation(void);
24
+ void prof_allocation_free(prof_allocation_t* allocation);
25
+ void prof_allocation_mark(void* data);
26
+ VALUE prof_allocation_wrap(prof_allocation_t* allocation);
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);
29
+
30
+
31
+ #endif //_RP_ALLOCATION_
@@ -0,0 +1,369 @@
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->object = Qnil;
17
+ result->visits = 0;
18
+ result->source_line = source_line;
19
+ result->source_file = source_file;
20
+ result->children = rb_st_init_numtable();
21
+ result->measurement = prof_measurement_create();
22
+
23
+ return result;
24
+ }
25
+
26
+ prof_call_tree_t* prof_call_tree_copy(prof_call_tree_t* other)
27
+ {
28
+ prof_call_tree_t* result = ALLOC(prof_call_tree_t);
29
+ result->children = rb_st_init_numtable();
30
+ result->object = Qnil;
31
+ result->visits = 0;
32
+
33
+ result->method = other->method;
34
+ result->parent = other->parent;
35
+ result->source_line = other->source_line;
36
+ result->source_file = other->source_file;
37
+
38
+ result->measurement = prof_measurement_create();
39
+ result->measurement->called = other->measurement->called;
40
+ result->measurement->total_time = other->measurement->total_time;
41
+ result->measurement->self_time = other->measurement->self_time;
42
+ result->measurement->wait_time = other->measurement->wait_time;
43
+ result->measurement->object = Qnil;
44
+
45
+ return result;
46
+ }
47
+
48
+ void prof_call_tree_merge(prof_call_tree_t* result, prof_call_tree_t* other)
49
+ {
50
+ result->measurement->called += other->measurement->called;
51
+ result->measurement->total_time += other->measurement->total_time;
52
+ result->measurement->self_time += other->measurement->self_time;
53
+ result->measurement->wait_time += other->measurement->wait_time;
54
+ }
55
+
56
+ static int prof_call_tree_collect_children(st_data_t key, st_data_t value, st_data_t result)
57
+ {
58
+ prof_call_tree_t* call_tree = (prof_call_tree_t*)value;
59
+ VALUE arr = (VALUE)result;
60
+ rb_ary_push(arr, prof_call_tree_wrap(call_tree));
61
+ return ST_CONTINUE;
62
+ }
63
+
64
+ static int prof_call_tree_mark_children(st_data_t key, st_data_t value, st_data_t data)
65
+ {
66
+ prof_call_tree_t* call_tree = (prof_call_tree_t*)value;
67
+ rb_st_foreach(call_tree->children, prof_call_tree_mark_children, data);
68
+ prof_call_tree_mark(call_tree);
69
+ return ST_CONTINUE;
70
+ }
71
+
72
+ void prof_call_tree_mark(void* data)
73
+ {
74
+ if (!data)
75
+ return;
76
+
77
+ prof_call_tree_t* call_tree = (prof_call_tree_t*)data;
78
+
79
+ if (call_tree->object != Qnil)
80
+ rb_gc_mark(call_tree->object);
81
+
82
+ if (call_tree->source_file != Qnil)
83
+ rb_gc_mark(call_tree->source_file);
84
+
85
+ prof_method_mark(call_tree->method);
86
+ prof_measurement_mark(call_tree->measurement);
87
+
88
+ // Recurse down through the whole call tree but only from the top node
89
+ // to avoid calling mark over and over and over.
90
+ if (!call_tree->parent)
91
+ rb_st_foreach(call_tree->children, prof_call_tree_mark_children, 0);
92
+ }
93
+
94
+ static void prof_call_tree_ruby_gc_free(void* data)
95
+ {
96
+ if (data)
97
+ {
98
+ prof_call_tree_t* call_tree = (prof_call_tree_t*)data;
99
+ call_tree->object = Qnil;
100
+ }
101
+ }
102
+
103
+ static int prof_call_tree_free_children(st_data_t key, st_data_t value, st_data_t data)
104
+ {
105
+ prof_call_tree_t* call_tree = (prof_call_tree_t*)value;
106
+ prof_call_tree_free(call_tree);
107
+ return ST_CONTINUE;
108
+ }
109
+
110
+ void prof_call_tree_free(prof_call_tree_t* call_tree_data)
111
+ {
112
+ /* Has this call info object been accessed by Ruby? If
113
+ yes clean it up so to avoid a segmentation fault. */
114
+ if (call_tree_data->object != Qnil)
115
+ {
116
+ RTYPEDDATA(call_tree_data->object)->data = NULL;
117
+ call_tree_data->object = Qnil;
118
+ }
119
+
120
+ // Free children
121
+ rb_st_foreach(call_tree_data->children, prof_call_tree_free_children, 0);
122
+ rb_st_free_table(call_tree_data->children);
123
+
124
+ // Free measurement
125
+ prof_measurement_free(call_tree_data->measurement);
126
+
127
+ // Finally free self
128
+ xfree(call_tree_data);
129
+ }
130
+
131
+ size_t prof_call_tree_size(const void* data)
132
+ {
133
+ return sizeof(prof_call_tree_t);
134
+ }
135
+
136
+ static const rb_data_type_t call_tree_type =
137
+ {
138
+ .wrap_struct_name = "CallTree",
139
+ .function =
140
+ {
141
+ .dmark = prof_call_tree_mark,
142
+ .dfree = prof_call_tree_ruby_gc_free,
143
+ .dsize = prof_call_tree_size,
144
+ },
145
+ .data = NULL,
146
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY
147
+ };
148
+
149
+ VALUE prof_call_tree_wrap(prof_call_tree_t* call_tree)
150
+ {
151
+ if (call_tree->object == Qnil)
152
+ {
153
+ call_tree->object = TypedData_Wrap_Struct(cRpCallTree, &call_tree_type, call_tree);
154
+ }
155
+ return call_tree->object;
156
+ }
157
+
158
+ static VALUE prof_call_tree_allocate(VALUE klass)
159
+ {
160
+ prof_call_tree_t* call_tree = prof_call_tree_create(NULL, NULL, Qnil, 0);
161
+ call_tree->object = prof_call_tree_wrap(call_tree);
162
+ return call_tree->object;
163
+ }
164
+
165
+ prof_call_tree_t* prof_get_call_tree(VALUE self)
166
+ {
167
+ /* Can't use Data_Get_Struct because that triggers the event hook
168
+ ending up in endless recursion. */
169
+ prof_call_tree_t* result = RTYPEDDATA_DATA(self);
170
+
171
+ if (!result)
172
+ rb_raise(rb_eRuntimeError, "This RubyProf::CallTree instance has already been freed, likely because its profile has been freed.");
173
+
174
+ return result;
175
+ }
176
+
177
+ /* ======= Call Tree Table ========*/
178
+ static size_t call_tree_table_insert(st_table* table, st_data_t key, prof_call_tree_t* val)
179
+ {
180
+ return rb_st_insert(table, (st_data_t)key, (st_data_t)val);
181
+ }
182
+
183
+ prof_call_tree_t* call_tree_table_lookup(st_table* table, st_data_t key)
184
+ {
185
+ st_data_t val;
186
+ if (rb_st_lookup(table, (st_data_t)key, &val))
187
+ {
188
+ return (prof_call_tree_t*)val;
189
+ }
190
+ else
191
+ {
192
+ return NULL;
193
+ }
194
+ }
195
+
196
+ uint32_t prof_call_figure_depth(prof_call_tree_t* call_tree_data)
197
+ {
198
+ uint32_t result = 0;
199
+
200
+ while (call_tree_data->parent)
201
+ {
202
+ result++;
203
+ call_tree_data = call_tree_data->parent;
204
+ }
205
+
206
+ return result;
207
+ }
208
+
209
+ void prof_call_tree_add_parent(prof_call_tree_t* self, prof_call_tree_t* parent)
210
+ {
211
+ prof_call_tree_add_child(parent, self);
212
+ self->parent = parent;
213
+ }
214
+
215
+ void prof_call_tree_add_child(prof_call_tree_t* self, prof_call_tree_t* child)
216
+ {
217
+ call_tree_table_insert(self->children, child->method->key, child);
218
+ }
219
+
220
+ /* ======= RubyProf::CallTree ========*/
221
+
222
+ /* call-seq:
223
+ parent -> call_tree
224
+
225
+ Returns the CallTree parent call_tree object (the method that called this method).*/
226
+ static VALUE prof_call_tree_parent(VALUE self)
227
+ {
228
+ prof_call_tree_t* call_tree = prof_get_call_tree(self);
229
+ if (call_tree->parent)
230
+ return prof_call_tree_wrap(call_tree->parent);
231
+ else
232
+ return Qnil;
233
+ }
234
+
235
+ /* call-seq:
236
+ callees -> array
237
+
238
+ Returns an array of call info objects that this method called (ie, children).*/
239
+ static VALUE prof_call_tree_children(VALUE self)
240
+ {
241
+ prof_call_tree_t* call_tree = prof_get_call_tree(self);
242
+ VALUE result = rb_ary_new();
243
+ rb_st_foreach(call_tree->children, prof_call_tree_collect_children, result);
244
+ return result;
245
+ }
246
+
247
+ /* call-seq:
248
+ called -> MethodInfo
249
+
250
+ Returns the target method. */
251
+ static VALUE prof_call_tree_target(VALUE self)
252
+ {
253
+ prof_call_tree_t* call_tree = prof_get_call_tree(self);
254
+ return prof_method_wrap(call_tree->method);
255
+ }
256
+
257
+ /* call-seq:
258
+ called -> Measurement
259
+
260
+ Returns the measurement associated with this call_tree. */
261
+ static VALUE prof_call_tree_measurement(VALUE self)
262
+ {
263
+ prof_call_tree_t* call_tree = prof_get_call_tree(self);
264
+ return prof_measurement_wrap(call_tree->measurement);
265
+ }
266
+
267
+ /* call-seq:
268
+ depth -> int
269
+
270
+ returns the depth of this call info in the call graph */
271
+ static VALUE prof_call_tree_depth(VALUE self)
272
+ {
273
+ prof_call_tree_t* call_tree_data = prof_get_call_tree(self);
274
+ uint32_t depth = prof_call_figure_depth(call_tree_data);
275
+ return rb_int_new(depth);
276
+ }
277
+
278
+ /* call-seq:
279
+ source_file => string
280
+
281
+ return the source file of the method
282
+ */
283
+ static VALUE prof_call_tree_source_file(VALUE self)
284
+ {
285
+ prof_call_tree_t* result = prof_get_call_tree(self);
286
+ return result->source_file;
287
+ }
288
+
289
+ /* call-seq:
290
+ line_no -> int
291
+
292
+ returns the line number of the method */
293
+ static VALUE prof_call_tree_line(VALUE self)
294
+ {
295
+ prof_call_tree_t* result = prof_get_call_tree(self);
296
+ return INT2FIX(result->source_line);
297
+ }
298
+
299
+ /* :nodoc: */
300
+ static VALUE prof_call_tree_dump(VALUE self)
301
+ {
302
+ prof_call_tree_t* call_tree_data = prof_get_call_tree(self);
303
+ VALUE result = rb_hash_new();
304
+
305
+ rb_hash_aset(result, ID2SYM(rb_intern("measurement")), prof_measurement_wrap(call_tree_data->measurement));
306
+
307
+ rb_hash_aset(result, ID2SYM(rb_intern("source_file")), call_tree_data->source_file);
308
+ rb_hash_aset(result, ID2SYM(rb_intern("source_line")), INT2FIX(call_tree_data->source_line));
309
+
310
+ rb_hash_aset(result, ID2SYM(rb_intern("parent")), prof_call_tree_parent(self));
311
+ rb_hash_aset(result, ID2SYM(rb_intern("children")), prof_call_tree_children(self));
312
+ rb_hash_aset(result, ID2SYM(rb_intern("target")), prof_call_tree_target(self));
313
+
314
+ return result;
315
+ }
316
+
317
+ /* :nodoc: */
318
+ static VALUE prof_call_tree_load(VALUE self, VALUE data)
319
+ {
320
+ VALUE target = Qnil;
321
+ VALUE parent = Qnil;
322
+ prof_call_tree_t* call_tree = prof_get_call_tree(self);
323
+ call_tree->object = self;
324
+
325
+ VALUE measurement = rb_hash_aref(data, ID2SYM(rb_intern("measurement")));
326
+ call_tree->measurement = prof_get_measurement(measurement);
327
+
328
+ call_tree->source_file = rb_hash_aref(data, ID2SYM(rb_intern("source_file")));
329
+ call_tree->source_line = FIX2INT(rb_hash_aref(data, ID2SYM(rb_intern("source_line"))));
330
+
331
+ parent = rb_hash_aref(data, ID2SYM(rb_intern("parent")));
332
+ if (parent != Qnil)
333
+ call_tree->parent = prof_get_call_tree(parent);
334
+
335
+ VALUE callees = rb_hash_aref(data, ID2SYM(rb_intern("children")));
336
+ for (int i = 0; i < rb_array_len(callees); i++)
337
+ {
338
+ VALUE call_tree_object = rb_ary_entry(callees, i);
339
+ prof_call_tree_t* call_tree_data = prof_get_call_tree(call_tree_object);
340
+
341
+ st_data_t key = call_tree_data->method ? call_tree_data->method->key : method_key(Qnil, 0);
342
+ call_tree_table_insert(call_tree->children, key, call_tree_data);
343
+ }
344
+
345
+ target = rb_hash_aref(data, ID2SYM(rb_intern("target")));
346
+ call_tree->method = prof_get_method(target);
347
+
348
+ return data;
349
+ }
350
+
351
+ void rp_init_call_tree()
352
+ {
353
+ /* CallTree */
354
+ cRpCallTree = rb_define_class_under(mProf, "CallTree", rb_cObject);
355
+ rb_undef_method(CLASS_OF(cRpCallTree), "new");
356
+ rb_define_alloc_func(cRpCallTree, prof_call_tree_allocate);
357
+
358
+ rb_define_method(cRpCallTree, "parent", prof_call_tree_parent, 0);
359
+ rb_define_method(cRpCallTree, "children", prof_call_tree_children, 0);
360
+ rb_define_method(cRpCallTree, "target", prof_call_tree_target, 0);
361
+ rb_define_method(cRpCallTree, "measurement", prof_call_tree_measurement, 0);
362
+
363
+ rb_define_method(cRpCallTree, "depth", prof_call_tree_depth, 0);
364
+ rb_define_method(cRpCallTree, "source_file", prof_call_tree_source_file, 0);
365
+ rb_define_method(cRpCallTree, "line", prof_call_tree_line, 0);
366
+
367
+ rb_define_method(cRpCallTree, "_dump_data", prof_call_tree_dump, 0);
368
+ rb_define_method(cRpCallTree, "_load_data", prof_call_tree_load, 1);
369
+ }
@@ -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,288 @@
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 = RTYPEDDATA_DATA(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(void* data)
36
+ {
37
+ if (!data) return;
38
+
39
+ prof_call_trees_t* call_trees = (prof_call_trees_t*)data;
40
+ prof_call_tree_t** call_tree;
41
+ for (call_tree = call_trees->start; call_tree < call_trees->ptr; call_tree++)
42
+ {
43
+ prof_call_tree_mark(*call_tree);
44
+ }
45
+ }
46
+
47
+ void prof_call_trees_free(prof_call_trees_t* call_trees)
48
+ {
49
+ /* Has this method object been accessed by Ruby? If
50
+ yes clean it up so to avoid a segmentation fault. */
51
+ if (call_trees->object != Qnil)
52
+ {
53
+ RTYPEDDATA(call_trees->object)->data = NULL;
54
+ call_trees->object = Qnil;
55
+ }
56
+
57
+ // Note we do not free our call_tree structures - since they have no parents they will free themselves
58
+ xfree(call_trees);
59
+ }
60
+
61
+ void prof_call_trees_ruby_gc_free(void* data)
62
+ {
63
+ if (data)
64
+ {
65
+ // This object gets freed by its owning method
66
+ prof_call_trees_t* call_trees = (prof_call_trees_t*)data;
67
+ call_trees->object = Qnil;
68
+ }
69
+ }
70
+
71
+ static int prof_call_trees_collect_aggregates(st_data_t key, st_data_t value, st_data_t data)
72
+ {
73
+ VALUE result = (VALUE)data;
74
+ prof_call_tree_t* call_tree_data = (prof_call_tree_t*)value;
75
+ VALUE aggregate_call_tree = prof_aggregate_call_tree_wrap(call_tree_data);
76
+ rb_ary_push(result, aggregate_call_tree);
77
+
78
+ return ST_CONTINUE;
79
+ }
80
+
81
+ static int prof_call_trees_collect_callees(st_data_t key, st_data_t value, st_data_t hash)
82
+ {
83
+ st_table* callers = (st_table*)hash;
84
+ prof_call_tree_t* call_tree_data = (prof_call_tree_t*)value;
85
+
86
+ prof_call_tree_t* aggregate_call_tree_data = NULL;
87
+
88
+ if (rb_st_lookup(callers, call_tree_data->method->key, (st_data_t*)&aggregate_call_tree_data))
89
+ {
90
+ prof_call_tree_merge(aggregate_call_tree_data, call_tree_data);
91
+ }
92
+ else
93
+ {
94
+ aggregate_call_tree_data = prof_call_tree_copy(call_tree_data);
95
+ rb_st_insert(callers, call_tree_data->method->key, (st_data_t)aggregate_call_tree_data);
96
+ }
97
+
98
+ return ST_CONTINUE;
99
+ }
100
+
101
+ size_t prof_call_trees_size(const void* data)
102
+ {
103
+ return sizeof(prof_call_trees_t);
104
+ }
105
+
106
+ static const rb_data_type_t call_trees_type =
107
+ {
108
+ .wrap_struct_name = "CallTrees",
109
+ .function =
110
+ {
111
+ .dmark = prof_call_trees_mark,
112
+ .dfree = prof_call_trees_ruby_gc_free,
113
+ .dsize = prof_call_trees_size,
114
+ },
115
+ .data = NULL,
116
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY
117
+ };
118
+
119
+ VALUE prof_call_trees_wrap(prof_call_trees_t* call_trees)
120
+ {
121
+ if (call_trees->object == Qnil)
122
+ {
123
+ call_trees->object = TypedData_Wrap_Struct(cRpCallTrees, &call_trees_type, call_trees);
124
+ }
125
+ return call_trees->object;
126
+ }
127
+
128
+ void prof_add_call_tree(prof_call_trees_t* call_trees, prof_call_tree_t* call_tree)
129
+ {
130
+ if (call_trees->ptr == call_trees->end)
131
+ {
132
+ size_t len = call_trees->ptr - call_trees->start;
133
+ size_t new_capacity = (call_trees->end - call_trees->start) * 2;
134
+ REALLOC_N(call_trees->start, prof_call_tree_t*, new_capacity);
135
+ call_trees->ptr = call_trees->start + len;
136
+ call_trees->end = call_trees->start + new_capacity;
137
+ }
138
+ *call_trees->ptr = call_tree;
139
+ call_trees->ptr++;
140
+ }
141
+
142
+ /* ================ Call Infos =================*/
143
+ /* Document-class: RubyProf::CallTrees
144
+ The RubyProf::MethodInfo class stores profiling data for a method.
145
+ One instance of the RubyProf::MethodInfo class is created per method
146
+ called per thread. Thus, if a method is called in two different
147
+ thread then there will be two RubyProf::MethodInfo objects
148
+ created. RubyProf::MethodInfo objects can be accessed via
149
+ the RubyProf::Profile object. */
150
+ VALUE prof_call_trees_allocate(VALUE klass)
151
+ {
152
+ prof_call_trees_t* call_trees_data = prof_call_trees_create();
153
+ call_trees_data->object = prof_call_trees_wrap(call_trees_data);
154
+ return call_trees_data->object;
155
+ }
156
+
157
+
158
+ /* call-seq:
159
+ min_depth -> Integer
160
+
161
+ Returns the minimum depth of this method in any call tree */
162
+ VALUE prof_call_trees_min_depth(VALUE self)
163
+ {
164
+ unsigned int depth = INT_MAX;
165
+
166
+ prof_call_trees_t* call_trees = prof_get_call_trees(self);
167
+ for (prof_call_tree_t** p_call_tree = call_trees->start; p_call_tree < call_trees->ptr; p_call_tree++)
168
+ {
169
+ unsigned int call_tree_depth = prof_call_figure_depth(*p_call_tree);
170
+ if (call_tree_depth < depth)
171
+ depth = call_tree_depth;
172
+ }
173
+
174
+ return UINT2NUM(depth);
175
+ }
176
+
177
+ /* call-seq:
178
+ callers -> array
179
+
180
+ Returns an array of all CallTree objects that called this method. */
181
+ VALUE prof_call_trees_call_trees(VALUE self)
182
+ {
183
+ VALUE result = rb_ary_new();
184
+
185
+ prof_call_trees_t* call_trees = prof_get_call_trees(self);
186
+ for (prof_call_tree_t** p_call_tree = call_trees->start; p_call_tree < call_trees->ptr; p_call_tree++)
187
+ {
188
+ VALUE call_tree = prof_call_tree_wrap(*p_call_tree);
189
+ rb_ary_push(result, call_tree);
190
+ }
191
+ return result;
192
+ }
193
+
194
+ /* call-seq:
195
+ callers -> array
196
+
197
+ Returns an array of aggregated CallTree objects that called this method (ie, parents).*/
198
+ VALUE prof_call_trees_callers(VALUE self)
199
+ {
200
+ st_table* callers = rb_st_init_numtable();
201
+
202
+ prof_call_trees_t* call_trees = prof_get_call_trees(self);
203
+ for (prof_call_tree_t** p_call_tree = call_trees->start; p_call_tree < call_trees->ptr; p_call_tree++)
204
+ {
205
+ prof_call_tree_t* parent = (*p_call_tree)->parent;
206
+ if (parent == NULL)
207
+ continue;
208
+
209
+ prof_call_tree_t* aggregate_call_tree_data = NULL;
210
+
211
+ if (rb_st_lookup(callers, parent->method->key, (st_data_t*)&aggregate_call_tree_data))
212
+ {
213
+ prof_call_tree_merge(aggregate_call_tree_data, *p_call_tree);
214
+ }
215
+ else
216
+ {
217
+ aggregate_call_tree_data = prof_call_tree_copy(*p_call_tree);
218
+ rb_st_insert(callers, parent->method->key, (st_data_t)aggregate_call_tree_data);
219
+ }
220
+ }
221
+
222
+ VALUE result = rb_ary_new_capa((long)callers->num_entries);
223
+ rb_st_foreach(callers, prof_call_trees_collect_aggregates, result);
224
+ rb_st_free_table(callers);
225
+ return result;
226
+ }
227
+
228
+ /* call-seq:
229
+ callees -> array
230
+
231
+ Returns an array of aggregated CallTree objects that this method called (ie, children).*/
232
+ VALUE prof_call_trees_callees(VALUE self)
233
+ {
234
+ st_table* callees = rb_st_init_numtable();
235
+
236
+ prof_call_trees_t* call_trees = prof_get_call_trees(self);
237
+ for (prof_call_tree_t** call_tree = call_trees->start; call_tree < call_trees->ptr; call_tree++)
238
+ {
239
+ rb_st_foreach((*call_tree)->children, prof_call_trees_collect_callees, (st_data_t)callees);
240
+ }
241
+
242
+ VALUE result = rb_ary_new_capa((long)callees->num_entries);
243
+ rb_st_foreach(callees, prof_call_trees_collect_aggregates, result);
244
+ rb_st_free_table(callees);
245
+ return result;
246
+ }
247
+
248
+ /* :nodoc: */
249
+ VALUE prof_call_trees_dump(VALUE self)
250
+ {
251
+ VALUE result = rb_hash_new();
252
+ rb_hash_aset(result, ID2SYM(rb_intern("call_trees")), prof_call_trees_call_trees(self));
253
+
254
+ return result;
255
+ }
256
+
257
+ /* :nodoc: */
258
+ VALUE prof_call_trees_load(VALUE self, VALUE data)
259
+ {
260
+ prof_call_trees_t* call_trees_data = prof_get_call_trees(self);
261
+ call_trees_data->object = self;
262
+
263
+ VALUE call_trees = rb_hash_aref(data, ID2SYM(rb_intern("call_trees")));
264
+ for (int i = 0; i < rb_array_len(call_trees); i++)
265
+ {
266
+ VALUE call_tree = rb_ary_entry(call_trees, i);
267
+ prof_call_tree_t* call_tree_data = prof_get_call_tree(call_tree);
268
+ prof_add_call_tree(call_trees_data, call_tree_data);
269
+ }
270
+
271
+ return data;
272
+ }
273
+
274
+ void rp_init_call_trees()
275
+ {
276
+ cRpCallTrees = rb_define_class_under(mProf, "CallTrees", rb_cObject);
277
+ rb_undef_method(CLASS_OF(cRpCallTrees), "new");
278
+ rb_define_alloc_func(cRpCallTrees, prof_call_trees_allocate);
279
+
280
+ rb_define_method(cRpCallTrees, "min_depth", prof_call_trees_min_depth, 0);
281
+
282
+ rb_define_method(cRpCallTrees, "call_trees", prof_call_trees_call_trees, 0);
283
+ rb_define_method(cRpCallTrees, "callers", prof_call_trees_callers, 0);
284
+ rb_define_method(cRpCallTrees, "callees", prof_call_trees_callees, 0);
285
+
286
+ rb_define_method(cRpCallTrees, "_dump_data", prof_call_trees_dump, 0);
287
+ rb_define_method(cRpCallTrees, "_load_data", prof_call_trees_load, 1);
288
+ }