ruby-prof 0.16.2 → 1.1.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 (203) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGES +532 -467
  3. data/LICENSE +24 -24
  4. data/README.rdoc +5 -454
  5. data/Rakefile +110 -113
  6. data/bin/ruby-prof +380 -340
  7. data/bin/ruby-prof-check-trace +45 -45
  8. data/ext/ruby_prof/extconf.rb +36 -64
  9. data/ext/ruby_prof/rp_allocation.c +279 -0
  10. data/ext/ruby_prof/rp_allocation.h +31 -0
  11. data/ext/ruby_prof/rp_call_info.c +271 -407
  12. data/ext/ruby_prof/rp_call_info.h +35 -48
  13. data/ext/ruby_prof/rp_measure_allocations.c +52 -76
  14. data/ext/ruby_prof/rp_measure_memory.c +42 -77
  15. data/ext/ruby_prof/rp_measure_process_time.c +67 -71
  16. data/ext/ruby_prof/rp_measure_wall_time.c +62 -45
  17. data/ext/ruby_prof/rp_measurement.c +230 -0
  18. data/ext/ruby_prof/rp_measurement.h +50 -0
  19. data/ext/ruby_prof/rp_method.c +630 -411
  20. data/ext/ruby_prof/rp_method.h +70 -52
  21. data/ext/ruby_prof/rp_profile.c +895 -0
  22. data/ext/ruby_prof/rp_profile.h +37 -0
  23. data/ext/ruby_prof/rp_stack.c +196 -128
  24. data/ext/ruby_prof/rp_stack.h +56 -51
  25. data/ext/ruby_prof/rp_thread.c +337 -273
  26. data/ext/ruby_prof/rp_thread.h +36 -27
  27. data/ext/ruby_prof/ruby_prof.c +48 -671
  28. data/ext/ruby_prof/ruby_prof.h +17 -56
  29. data/ext/ruby_prof/vc/ruby_prof.sln +20 -21
  30. data/ext/ruby_prof/vc/{ruby_prof_20.vcxproj → ruby_prof.vcxproj} +38 -5
  31. data/lib/ruby-prof.rb +52 -58
  32. data/lib/ruby-prof/assets/call_stack_printer.html.erb +713 -0
  33. data/lib/ruby-prof/assets/call_stack_printer.png +0 -0
  34. data/lib/ruby-prof/assets/graph_printer.html.erb +356 -0
  35. data/lib/ruby-prof/call_info.rb +57 -126
  36. data/lib/ruby-prof/call_info_visitor.rb +38 -40
  37. data/lib/ruby-prof/compatibility.rb +109 -178
  38. data/lib/ruby-prof/exclude_common_methods.rb +198 -0
  39. data/lib/ruby-prof/measurement.rb +14 -0
  40. data/lib/ruby-prof/method_info.rb +90 -129
  41. data/lib/ruby-prof/printers/abstract_printer.rb +127 -85
  42. data/lib/ruby-prof/printers/call_info_printer.rb +51 -41
  43. data/lib/ruby-prof/printers/call_stack_printer.rb +182 -260
  44. data/lib/ruby-prof/printers/call_tree_printer.rb +151 -130
  45. data/lib/ruby-prof/printers/dot_printer.rb +132 -132
  46. data/lib/ruby-prof/printers/flat_printer.rb +52 -70
  47. data/lib/ruby-prof/printers/graph_html_printer.rb +63 -244
  48. data/lib/ruby-prof/printers/graph_printer.rb +114 -116
  49. data/lib/ruby-prof/printers/multi_printer.rb +127 -58
  50. data/lib/ruby-prof/profile.rb +33 -55
  51. data/lib/ruby-prof/rack.rb +171 -95
  52. data/lib/ruby-prof/task.rb +147 -147
  53. data/lib/ruby-prof/thread.rb +35 -41
  54. data/lib/ruby-prof/version.rb +3 -3
  55. data/lib/unprof.rb +10 -10
  56. data/ruby-prof.gemspec +58 -57
  57. data/test/abstract_printer_test.rb +26 -0
  58. data/test/alias_test.rb +129 -0
  59. data/test/basic_test.rb +129 -128
  60. data/test/call_info_visitor_test.rb +31 -31
  61. data/test/duplicate_names_test.rb +32 -32
  62. data/test/dynamic_method_test.rb +53 -55
  63. data/test/enumerable_test.rb +21 -21
  64. data/test/exceptions_test.rb +24 -16
  65. data/test/exclude_methods_test.rb +146 -0
  66. data/test/exclude_threads_test.rb +53 -53
  67. data/test/fiber_test.rb +73 -79
  68. data/test/gc_test.rb +96 -0
  69. data/test/line_number_test.rb +161 -71
  70. data/test/marshal_test.rb +119 -0
  71. data/test/measure_allocations.rb +30 -0
  72. data/test/measure_allocations_test.rb +385 -26
  73. data/test/measure_allocations_trace_test.rb +385 -0
  74. data/test/measure_memory_trace_test.rb +756 -0
  75. data/test/measure_process_time_test.rb +849 -63
  76. data/test/measure_times.rb +54 -0
  77. data/test/measure_wall_time_test.rb +459 -255
  78. data/test/multi_printer_test.rb +71 -83
  79. data/test/no_method_class_test.rb +15 -15
  80. data/test/parser_timings.rb +24 -0
  81. data/test/pause_resume_test.rb +166 -166
  82. data/test/prime.rb +56 -54
  83. data/test/printer_call_stack_test.rb +28 -0
  84. data/test/printer_call_tree_test.rb +31 -0
  85. data/test/printer_flat_test.rb +68 -0
  86. data/test/printer_graph_html_test.rb +60 -0
  87. data/test/printer_graph_test.rb +41 -0
  88. data/test/printers_test.rb +141 -255
  89. data/test/printing_recursive_graph_test.rb +81 -127
  90. data/test/rack_test.rb +157 -93
  91. data/test/recursive_test.rb +210 -215
  92. data/test/singleton_test.rb +38 -38
  93. data/test/stack_printer_test.rb +64 -78
  94. data/test/start_stop_test.rb +109 -112
  95. data/test/test_helper.rb +24 -264
  96. data/test/thread_test.rb +144 -187
  97. data/test/unique_call_path_test.rb +190 -202
  98. data/test/yarv_test.rb +56 -55
  99. metadata +34 -114
  100. data/doc/LICENSE.html +0 -114
  101. data/doc/README_rdoc.html +0 -603
  102. data/doc/Rack.html +0 -95
  103. data/doc/Rack/RubyProf.html +0 -226
  104. data/doc/RubyProf.html +0 -962
  105. data/doc/RubyProf/AbstractPrinter.html +0 -546
  106. data/doc/RubyProf/AggregateCallInfo.html +0 -551
  107. data/doc/RubyProf/CallInfo.html +0 -639
  108. data/doc/RubyProf/CallInfoPrinter.html +0 -120
  109. data/doc/RubyProf/CallInfoVisitor.html +0 -198
  110. data/doc/RubyProf/CallStackPrinter.html +0 -1121
  111. data/doc/RubyProf/CallTreePrinter.html +0 -641
  112. data/doc/RubyProf/Cmd.html +0 -631
  113. data/doc/RubyProf/DotPrinter.html +0 -257
  114. data/doc/RubyProf/FlatPrinter.html +0 -163
  115. data/doc/RubyProf/FlatPrinterWithLineNumbers.html +0 -208
  116. data/doc/RubyProf/GraphHtmlPrinter.html +0 -552
  117. data/doc/RubyProf/GraphPrinter.html +0 -139
  118. data/doc/RubyProf/MethodInfo.html +0 -745
  119. data/doc/RubyProf/MultiPrinter.html +0 -360
  120. data/doc/RubyProf/Profile.html +0 -763
  121. data/doc/RubyProf/ProfileTask.html +0 -490
  122. data/doc/RubyProf/Thread.html +0 -310
  123. data/doc/created.rid +0 -31
  124. data/doc/css/fonts.css +0 -167
  125. data/doc/css/rdoc.css +0 -590
  126. data/doc/examples/flat_txt.html +0 -138
  127. data/doc/examples/graph_html.html +0 -909
  128. data/doc/examples/graph_txt.html +0 -247
  129. data/doc/fonts/Lato-Light.ttf +0 -0
  130. data/doc/fonts/Lato-LightItalic.ttf +0 -0
  131. data/doc/fonts/Lato-Regular.ttf +0 -0
  132. data/doc/fonts/Lato-RegularItalic.ttf +0 -0
  133. data/doc/fonts/SourceCodePro-Bold.ttf +0 -0
  134. data/doc/fonts/SourceCodePro-Regular.ttf +0 -0
  135. data/doc/images/add.png +0 -0
  136. data/doc/images/arrow_up.png +0 -0
  137. data/doc/images/brick.png +0 -0
  138. data/doc/images/brick_link.png +0 -0
  139. data/doc/images/bug.png +0 -0
  140. data/doc/images/bullet_black.png +0 -0
  141. data/doc/images/bullet_toggle_minus.png +0 -0
  142. data/doc/images/bullet_toggle_plus.png +0 -0
  143. data/doc/images/date.png +0 -0
  144. data/doc/images/delete.png +0 -0
  145. data/doc/images/find.png +0 -0
  146. data/doc/images/loadingAnimation.gif +0 -0
  147. data/doc/images/macFFBgHack.png +0 -0
  148. data/doc/images/package.png +0 -0
  149. data/doc/images/page_green.png +0 -0
  150. data/doc/images/page_white_text.png +0 -0
  151. data/doc/images/page_white_width.png +0 -0
  152. data/doc/images/plugin.png +0 -0
  153. data/doc/images/ruby.png +0 -0
  154. data/doc/images/tag_blue.png +0 -0
  155. data/doc/images/tag_green.png +0 -0
  156. data/doc/images/transparent.png +0 -0
  157. data/doc/images/wrench.png +0 -0
  158. data/doc/images/wrench_orange.png +0 -0
  159. data/doc/images/zoom.png +0 -0
  160. data/doc/index.html +0 -626
  161. data/doc/js/darkfish.js +0 -161
  162. data/doc/js/jquery.js +0 -4
  163. data/doc/js/navigation.js +0 -142
  164. data/doc/js/navigation.js.gz +0 -0
  165. data/doc/js/search.js +0 -109
  166. data/doc/js/search_index.js +0 -1
  167. data/doc/js/search_index.js.gz +0 -0
  168. data/doc/js/searcher.js +0 -228
  169. data/doc/js/searcher.js.gz +0 -0
  170. data/doc/table_of_contents.html +0 -942
  171. data/examples/cachegrind.out.1 +0 -114
  172. data/examples/cachegrind.out.1.32313213 +0 -114
  173. data/examples/flat.txt +0 -50
  174. data/examples/graph.dot +0 -84
  175. data/examples/graph.html +0 -823
  176. data/examples/graph.txt +0 -139
  177. data/examples/multi.flat.txt +0 -23
  178. data/examples/multi.graph.html +0 -760
  179. data/examples/multi.grind.dat +0 -114
  180. data/examples/multi.stack.html +0 -547
  181. data/examples/stack.html +0 -547
  182. data/ext/ruby_prof/rp_measure.c +0 -40
  183. data/ext/ruby_prof/rp_measure.h +0 -45
  184. data/ext/ruby_prof/rp_measure_cpu_time.c +0 -136
  185. data/ext/ruby_prof/rp_measure_gc_runs.c +0 -73
  186. data/ext/ruby_prof/rp_measure_gc_time.c +0 -60
  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/lib/ruby-prof/aggregate_call_info.rb +0 -76
  190. data/lib/ruby-prof/assets/call_stack_printer.css.html +0 -117
  191. data/lib/ruby-prof/assets/call_stack_printer.js.html +0 -385
  192. data/lib/ruby-prof/printers/flat_printer_with_line_numbers.rb +0 -64
  193. data/test/aggregate_test.rb +0 -136
  194. data/test/block_test.rb +0 -74
  195. data/test/call_info_test.rb +0 -78
  196. data/test/issue137_test.rb +0 -63
  197. data/test/measure_cpu_time_test.rb +0 -213
  198. data/test/measure_gc_runs_test.rb +0 -32
  199. data/test/measure_gc_time_test.rb +0 -36
  200. data/test/measure_memory_test.rb +0 -33
  201. data/test/method_elimination_test.rb +0 -84
  202. data/test/module_test.rb +0 -45
  203. data/test/stack_test.rb +0 -138
@@ -1,27 +1,36 @@
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_THREAD__
5
- #define __RP_THREAD__
6
-
7
- /* Profiling information for a thread. */
8
- typedef struct
9
- {
10
- VALUE object; /* Cache to wrapped object */
11
- VALUE methods; /* Array of RubyProf::MethodInfo */
12
- VALUE thread_id; /* Thread id */
13
- VALUE fiber_id; /* Fiber id */
14
- st_table* method_table; /* Methods called in the thread */
15
- prof_stack_t* stack; /* Stack of frames */
16
- } thread_data_t;
17
-
18
- void rp_init_thread();
19
- st_table * threads_table_create();
20
- thread_data_t* switch_thread(void* prof, VALUE thread_id, VALUE fiber_id);
21
- void threads_table_free(st_table *table);
22
- VALUE prof_thread_wrap(thread_data_t *thread);
23
- void prof_thread_mark(thread_data_t *thread);
24
- int pause_thread(st_data_t key, st_data_t value, st_data_t data);
25
- int unpause_thread(st_data_t key, st_data_t value, st_data_t data);
26
-
27
- #endif //__RP_THREAD__
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_THREAD__
5
+ #define __RP_THREAD__
6
+
7
+ #include "ruby_prof.h"
8
+ #include "rp_stack.h"
9
+
10
+ /* Profiling information for a thread. */
11
+ typedef struct
12
+ {
13
+ // Runtime
14
+ VALUE object; /* Cache to wrapped object */
15
+ VALUE fiber; /* Fiber */
16
+ prof_stack_t* stack; /* Stack of frames */
17
+ bool trace; /* Are we tracking this thread */
18
+
19
+ VALUE thread_id; /* Thread id */
20
+ VALUE fiber_id; /* Fiber id */
21
+ VALUE methods; /* Array of RubyProf::MethodInfo */
22
+ st_table* method_table; /* Methods called in the thread */
23
+ } thread_data_t;
24
+
25
+ void rp_init_thread(void);
26
+ st_table * threads_table_create(void);
27
+ thread_data_t *threads_table_lookup(void *profile, VALUE fiber);
28
+ thread_data_t* threads_table_insert(void *profile, VALUE fiber);
29
+ void switch_thread(void *profile, thread_data_t *thread_data, double measurement);
30
+ void threads_table_free(st_table *table);
31
+ VALUE prof_thread_wrap(thread_data_t *thread);
32
+ void prof_thread_mark(void *data);
33
+ int pause_thread(st_data_t key, st_data_t value, st_data_t data);
34
+ int unpause_thread(st_data_t key, st_data_t value, st_data_t data);
35
+
36
+ #endif //__RP_THREAD__
@@ -1,671 +1,48 @@
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
- /* ruby-prof tracks the time spent executing every method in ruby programming.
5
- The main players are:
6
-
7
- profile_t - This represents 1 profile.
8
- thread_data_t - Stores data about a single thread.
9
- prof_stack_t - The method call stack in a particular thread
10
- prof_method_t - Profiling information about each method
11
- prof_call_info_t - Keeps track a method's callers and callees.
12
-
13
- The final result is an instance of a profile object which has a hash table of
14
- thread_data_t, keyed on the thread id. Each thread in turn has a hash table
15
- of prof_method_t, keyed on the method id. A hash table is used for quick
16
- look up when doing a profile. However, it is exposed to Ruby as an array.
17
-
18
- Each prof_method_t has two hash tables, parent and children, of prof_call_info_t.
19
- These objects keep track of a method's callers (who called the method) and its
20
- callees (who the method called). These are keyed the method id, but once again,
21
- are exposed to Ruby as arrays. Each prof_call_into_t maintains a pointer to the
22
- caller or callee method, thereby making it easy to navigate through the call
23
- hierarchy in ruby - which is very helpful for creating call graphs.
24
- */
25
-
26
- #include "ruby_prof.h"
27
- #include <assert.h>
28
-
29
- VALUE mProf;
30
- VALUE cProfile;
31
-
32
- static prof_profile_t*
33
- prof_get_profile(VALUE self)
34
- {
35
- /* Can't use Data_Get_Struct because that triggers the event hook
36
- ending up in endless recursion. */
37
- return (prof_profile_t*)RDATA(self)->data;
38
- }
39
-
40
- /* support tracing ruby events from ruby-prof. useful for getting at
41
- what actually happens inside the ruby interpreter (and ruby-prof).
42
- set environment variable RUBY_PROF_TRACE to filename you want to
43
- find the trace in.
44
- */
45
- static FILE* trace_file = NULL;
46
-
47
- /* Copied from thread.c (1.9.3) */
48
- static const char *
49
- get_event_name(rb_event_flag_t event)
50
- {
51
- switch (event) {
52
- case RUBY_EVENT_LINE:
53
- return "line";
54
- case RUBY_EVENT_CLASS:
55
- return "class";
56
- case RUBY_EVENT_END:
57
- return "end";
58
- case RUBY_EVENT_CALL:
59
- return "call";
60
- case RUBY_EVENT_RETURN:
61
- return "return";
62
- case RUBY_EVENT_C_CALL:
63
- return "c-call";
64
- case RUBY_EVENT_C_RETURN:
65
- return "c-return";
66
- case RUBY_EVENT_RAISE:
67
- return "raise";
68
- default:
69
- return "unknown";
70
- }
71
- }
72
-
73
- static prof_method_t*
74
- create_method(rb_event_flag_t event, VALUE klass, ID mid, const char* source_file, int line)
75
- {
76
- /* Line numbers are not accurate for c method calls */
77
- if (event == RUBY_EVENT_C_CALL)
78
- {
79
- line = 0;
80
- source_file = NULL;
81
- }
82
-
83
- return prof_method_create(klass, mid, source_file, line);
84
- }
85
-
86
-
87
- static prof_method_t*
88
- get_method(rb_event_flag_t event, VALUE klass, ID mid, thread_data_t* thread_data)
89
- {
90
- prof_method_key_t key;
91
- prof_method_t *method = NULL;
92
-
93
- method_key(&key, klass, mid);
94
- method = method_table_lookup(thread_data->method_table, &key);
95
-
96
- if (!method)
97
- {
98
- const char* source_file = rb_sourcefile();
99
- int line = rb_sourceline();
100
-
101
- method = create_method(event, klass, mid, source_file, line);
102
- method_table_insert(thread_data->method_table, method->key, method);
103
- }
104
- return method;
105
- }
106
-
107
- static int
108
- pop_frames(st_data_t key, st_data_t value, st_data_t data)
109
- {
110
- thread_data_t* thread_data = (thread_data_t *) value;
111
- prof_profile_t* profile = (prof_profile_t*) data;
112
- VALUE thread_id = thread_data->thread_id;
113
- VALUE fiber_id = thread_data->fiber_id;
114
- double measurement = profile->measurer->measure();
115
-
116
- if (!profile->last_thread_data
117
- || (!profile->merge_fibers && profile->last_thread_data->fiber_id != fiber_id)
118
- || profile->last_thread_data->thread_id != thread_id
119
- )
120
- thread_data = switch_thread(profile, thread_id, fiber_id);
121
- else
122
- thread_data = profile->last_thread_data;
123
-
124
- while (prof_stack_pop(thread_data->stack, measurement));
125
-
126
- return ST_CONTINUE;
127
- }
128
-
129
- static void
130
- prof_pop_threads(prof_profile_t* profile)
131
- {
132
- st_foreach(profile->threads_tbl, pop_frames, (st_data_t) profile);
133
- }
134
-
135
- /* =========== Profiling ================= */
136
- static void
137
- prof_trace(prof_profile_t* profile, rb_event_flag_t event, ID mid, VALUE klass, double measurement)
138
- {
139
- static VALUE last_fiber_id = Qnil;
140
-
141
- VALUE thread = rb_thread_current();
142
- VALUE thread_id = rb_obj_id(thread);
143
- VALUE fiber = rb_fiber_current();
144
- VALUE fiber_id = rb_obj_id(fiber);
145
- const char* class_name = NULL;
146
- const char* method_name = rb_id2name(mid);
147
- const char* source_file = rb_sourcefile();
148
- unsigned int source_line = rb_sourceline();
149
-
150
- const char* event_name = get_event_name(event);
151
-
152
- if (klass != 0)
153
- klass = (BUILTIN_TYPE(klass) == T_ICLASS ? RBASIC(klass)->klass : klass);
154
-
155
- class_name = rb_class2name(klass);
156
-
157
- if (last_fiber_id != fiber_id)
158
- {
159
- fprintf(trace_file, "\n");
160
- }
161
-
162
- fprintf(trace_file, "%2lu:%2lu:%2ums %-8s %s:%2d %s#%s\n",
163
- (unsigned long) thread_id, (unsigned long) fiber_id, (unsigned int) measurement*1000,
164
- event_name, source_file, source_line, class_name, method_name);
165
- fflush(trace_file);
166
- last_fiber_id = fiber_id;
167
- }
168
-
169
- static void
170
- prof_event_hook(rb_event_flag_t event, VALUE data, VALUE self, ID mid, VALUE klass)
171
- {
172
- prof_profile_t* profile = prof_get_profile(data);
173
- VALUE thread = Qnil;
174
- VALUE thread_id = Qnil;
175
- VALUE fiber = Qnil;
176
- VALUE fiber_id = Qnil;
177
- thread_data_t* thread_data = NULL;
178
- prof_frame_t *frame = NULL;
179
- double measurement;
180
-
181
- /* if we don't have a valid method id, try to retrieve one */
182
- if (mid == 0) {
183
- rb_frame_method_id_and_class(&mid, &klass);
184
- }
185
-
186
- /* Get current measurement */
187
- measurement = profile->measurer->measure();
188
-
189
- /* Special case - skip any methods from the mProf
190
- module or cProfile class since they clutter
191
- the results but aren't important to them results. */
192
- if (self == mProf || klass == cProfile)
193
- return;
194
-
195
- if (trace_file != NULL)
196
- {
197
- prof_trace(profile, event, mid, klass, measurement);
198
- }
199
-
200
- /* Get the current thread and fiber information. */
201
- thread = rb_thread_current();
202
- thread_id = rb_obj_id(thread);
203
- fiber = rb_fiber_current();
204
- fiber_id = rb_obj_id(fiber);
205
-
206
- /* Don't measure anything if the include_threads option has been specified
207
- and the current thread is not in the list
208
- */
209
- if (profile->include_threads_tbl && !st_lookup(profile->include_threads_tbl, (st_data_t) thread_id, 0))
210
- {
211
- return;
212
- }
213
-
214
- /* Don't measure anything if the current thread is in the excluded thread table
215
- */
216
- if (profile->exclude_threads_tbl && st_lookup(profile->exclude_threads_tbl, (st_data_t) thread_id, 0))
217
- {
218
- return;
219
- }
220
-
221
- /* We need to switch the profiling context if we either had none before,
222
- we don't merge fibers and the fiber ids differ, or the thread ids differ.
223
- */
224
- if (!profile->last_thread_data
225
- || (!profile->merge_fibers && profile->last_thread_data->fiber_id != fiber_id)
226
- || profile->last_thread_data->thread_id != thread_id
227
- )
228
- thread_data = switch_thread(profile, thread_id, fiber_id);
229
- else
230
- thread_data = profile->last_thread_data;
231
-
232
- /* Get the current frame for the current thread. */
233
- frame = prof_stack_peek(thread_data->stack);
234
-
235
- switch (event) {
236
- case RUBY_EVENT_LINE:
237
- {
238
- /* Keep track of the current line number in this method. When
239
- a new method is called, we know what line number it was
240
- called from. */
241
-
242
- if (frame)
243
- {
244
- frame->line = rb_sourceline();
245
- break;
246
- }
247
-
248
- /* If we get here there was no frame, which means this is
249
- the first method seen for this thread, so fall through
250
- to below to create it. */
251
- }
252
- case RUBY_EVENT_CALL:
253
- case RUBY_EVENT_C_CALL:
254
- {
255
- prof_call_info_t *call_info = NULL;
256
- prof_method_t *method = NULL;
257
-
258
- method = get_method(event, klass, mid, thread_data);
259
-
260
- if (!frame)
261
- {
262
- call_info = prof_call_info_create(method, NULL);
263
- prof_add_call_info(method->call_infos, call_info);
264
- }
265
- else
266
- {
267
- call_info = call_info_table_lookup(frame->call_info->call_infos, method->key);
268
-
269
- if (!call_info)
270
- {
271
- /* This call info does not yet exist. So create it, then add
272
- it to previous callinfo's children and to the current method .*/
273
- call_info = prof_call_info_create(method, frame->call_info);
274
- call_info_table_insert(frame->call_info->call_infos, method->key, call_info);
275
- prof_add_call_info(method->call_infos, call_info);
276
- }
277
-
278
- // Unpause the parent frame. If currently paused then:
279
- // 1) The child frame will begin paused.
280
- // 2) The parent will inherit the child's dead time.
281
- prof_frame_unpause(frame, measurement);
282
- }
283
-
284
- /* Push a new frame onto the stack for a new c-call or ruby call (into a method) */
285
- frame = prof_stack_push(thread_data->stack, measurement);
286
- frame->call_info = call_info;
287
- frame->call_info->depth = frame->depth;
288
- frame->pause_time = profile->paused == Qtrue ? measurement : -1;
289
- frame->line = rb_sourceline();
290
- break;
291
- }
292
- case RUBY_EVENT_RETURN:
293
- case RUBY_EVENT_C_RETURN:
294
- {
295
- prof_stack_pop(thread_data->stack, measurement);
296
- break;
297
- }
298
- }
299
- }
300
-
301
- void
302
- prof_install_hook(VALUE self)
303
- {
304
- rb_add_event_hook(prof_event_hook,
305
- RUBY_EVENT_CALL | RUBY_EVENT_RETURN |
306
- RUBY_EVENT_C_CALL | RUBY_EVENT_C_RETURN |
307
- RUBY_EVENT_LINE, self);
308
- }
309
-
310
- void
311
- prof_remove_hook()
312
- {
313
- rb_remove_event_hook(prof_event_hook);
314
- }
315
-
316
- static int
317
- collect_threads(st_data_t key, st_data_t value, st_data_t result)
318
- {
319
- thread_data_t* thread_data = (thread_data_t*) value;
320
- VALUE threads_array = (VALUE) result;
321
- rb_ary_push(threads_array, prof_thread_wrap(thread_data));
322
- return ST_CONTINUE;
323
- }
324
-
325
- /* ======== Profile Class ====== */
326
- static int
327
- mark_threads(st_data_t key, st_data_t value, st_data_t result)
328
- {
329
- thread_data_t *thread = (thread_data_t *) value;
330
- prof_thread_mark(thread);
331
- return ST_CONTINUE;
332
- }
333
-
334
- static void
335
- prof_mark(prof_profile_t *profile)
336
- {
337
- st_foreach(profile->threads_tbl, mark_threads, 0);
338
- }
339
-
340
- /* Freeing the profile creates a cascade of freeing.
341
- It fress the thread table, which frees its methods,
342
- which frees its call infos. */
343
- static void
344
- prof_free(prof_profile_t *profile)
345
- {
346
- profile->last_thread_data = NULL;
347
-
348
- threads_table_free(profile->threads_tbl);
349
- profile->threads_tbl = NULL;
350
-
351
- if (profile->exclude_threads_tbl) {
352
- st_free_table(profile->exclude_threads_tbl);
353
- profile->exclude_threads_tbl = NULL;
354
- }
355
-
356
- if (profile->include_threads_tbl) {
357
- st_free_table(profile->include_threads_tbl);
358
- profile->include_threads_tbl = NULL;
359
- }
360
-
361
- xfree(profile->measurer);
362
- profile->measurer = NULL;
363
-
364
- xfree(profile);
365
- }
366
-
367
- static VALUE
368
- prof_allocate(VALUE klass)
369
- {
370
- VALUE result;
371
- prof_profile_t* profile;
372
- result = Data_Make_Struct(klass, prof_profile_t, prof_mark, prof_free, profile);
373
- profile->threads_tbl = threads_table_create();
374
- profile->exclude_threads_tbl = NULL;
375
- profile->include_threads_tbl = NULL;
376
- profile->running = Qfalse;
377
- profile->merge_fibers = 0;
378
- return result;
379
- }
380
-
381
- /* call-seq:
382
- new()
383
- new(options)
384
-
385
- Returns a new profiler. Possible options for the options hash are:
386
-
387
- measure_mode:: Measure mode. Specifies the profile measure mode.
388
- If not specified, defaults to RubyProf::WALL_TIME.
389
- exclude_threads:: Threads to exclude from the profiling results.
390
- include_threads:: Focus profiling on only the given threads. This will ignore
391
- all other threads.
392
- merge_fibers:: Whether to merge all fibers under a given thread. This should be
393
- used when profiling for a callgrind printer.
394
- */
395
- static VALUE
396
- prof_initialize(int argc, VALUE *argv, VALUE self)
397
- {
398
- prof_profile_t* profile = prof_get_profile(self);
399
- VALUE mode_or_options;
400
- VALUE mode = Qnil;
401
- VALUE exclude_threads = Qnil;
402
- VALUE include_threads = Qnil;
403
- VALUE merge_fibers = Qnil;
404
- int i;
405
-
406
- switch (rb_scan_args(argc, argv, "02", &mode_or_options, &exclude_threads)) {
407
- case 0:
408
- break;
409
- case 1:
410
- if (FIXNUM_P(mode_or_options)) {
411
- mode = mode_or_options;
412
- }
413
- else {
414
- Check_Type(mode_or_options, T_HASH);
415
- mode = rb_hash_aref(mode_or_options, ID2SYM(rb_intern("measure_mode")));
416
- merge_fibers = rb_hash_aref(mode_or_options, ID2SYM(rb_intern("merge_fibers")));
417
- exclude_threads = rb_hash_aref(mode_or_options, ID2SYM(rb_intern("exclude_threads")));
418
- include_threads = rb_hash_aref(mode_or_options, ID2SYM(rb_intern("include_threads")));
419
- }
420
- break;
421
- case 2:
422
- Check_Type(exclude_threads, T_ARRAY);
423
- break;
424
- }
425
-
426
- if (mode == Qnil) {
427
- mode = INT2NUM(MEASURE_WALL_TIME);
428
- } else {
429
- Check_Type(mode, T_FIXNUM);
430
- }
431
- profile->measurer = prof_get_measurer(NUM2INT(mode));
432
- profile->merge_fibers = merge_fibers != Qnil && merge_fibers != Qfalse;
433
-
434
- if (exclude_threads != Qnil) {
435
- Check_Type(exclude_threads, T_ARRAY);
436
- assert(profile->exclude_threads_tbl == NULL);
437
- profile->exclude_threads_tbl = threads_table_create();
438
- for (i = 0; i < RARRAY_LEN(exclude_threads); i++) {
439
- VALUE thread = rb_ary_entry(exclude_threads, i);
440
- VALUE thread_id = rb_obj_id(thread);
441
- st_insert(profile->exclude_threads_tbl, thread_id, Qtrue);
442
- }
443
- }
444
-
445
- if (include_threads != Qnil) {
446
- Check_Type(include_threads, T_ARRAY);
447
- assert(profile->include_threads_tbl == NULL);
448
- profile->include_threads_tbl = threads_table_create();
449
- for (i = 0; i < RARRAY_LEN(include_threads); i++) {
450
- VALUE thread = rb_ary_entry(include_threads, i);
451
- VALUE thread_id = rb_obj_id(thread);
452
- st_insert(profile->include_threads_tbl, thread_id, Qtrue);
453
- }
454
- }
455
-
456
- return self;
457
- }
458
-
459
- /* call-seq:
460
- paused? -> boolean
461
-
462
- Returns whether a profile is currently paused.*/
463
- static VALUE
464
- prof_paused(VALUE self)
465
- {
466
- prof_profile_t* profile = prof_get_profile(self);
467
- return profile->paused;
468
- }
469
-
470
- /* call-seq:
471
- running? -> boolean
472
-
473
- Returns whether a profile is currently running.*/
474
- static VALUE
475
- prof_running(VALUE self)
476
- {
477
- prof_profile_t* profile = prof_get_profile(self);
478
- return profile->running;
479
- }
480
-
481
- /* call-seq:
482
- start -> self
483
-
484
- Starts recording profile data.*/
485
- static VALUE
486
- prof_start(VALUE self)
487
- {
488
- char* trace_file_name;
489
-
490
- prof_profile_t* profile = prof_get_profile(self);
491
-
492
- if (profile->running == Qtrue)
493
- {
494
- rb_raise(rb_eRuntimeError, "RubyProf.start was already called");
495
- }
496
-
497
- profile->running = Qtrue;
498
- profile->paused = Qfalse;
499
- profile->last_thread_data = NULL;
500
-
501
-
502
- /* open trace file if environment wants it */
503
- trace_file_name = getenv("RUBY_PROF_TRACE");
504
- if (trace_file_name != NULL)
505
- {
506
- if (strcmp(trace_file_name, "stdout") == 0)
507
- {
508
- trace_file = stdout;
509
- }
510
- else if (strcmp(trace_file_name, "stderr") == 0)
511
- {
512
- trace_file = stderr;
513
- }
514
- else
515
- {
516
- trace_file = fopen(trace_file_name, "w");
517
- }
518
- }
519
-
520
- prof_install_hook(self);
521
- return self;
522
- }
523
-
524
- /* call-seq:
525
- pause -> self
526
-
527
- Pauses collecting profile data. */
528
- static VALUE
529
- prof_pause(VALUE self)
530
- {
531
- prof_profile_t* profile = prof_get_profile(self);
532
- if (profile->running == Qfalse)
533
- {
534
- rb_raise(rb_eRuntimeError, "RubyProf is not running.");
535
- }
536
-
537
- if (profile->paused == Qfalse)
538
- {
539
- profile->paused = Qtrue;
540
- profile->measurement_at_pause_resume = profile->measurer->measure();
541
- st_foreach(profile->threads_tbl, pause_thread, (st_data_t) profile);
542
- }
543
-
544
- return self;
545
- }
546
-
547
- /* call-seq:
548
- resume -> self
549
- resume(&block) -> self
550
-
551
- Resumes recording profile data.*/
552
- static VALUE
553
- prof_resume(VALUE self)
554
- {
555
- prof_profile_t* profile = prof_get_profile(self);
556
- if (profile->running == Qfalse)
557
- {
558
- rb_raise(rb_eRuntimeError, "RubyProf is not running.");
559
- }
560
-
561
- if (profile->paused == Qtrue)
562
- {
563
- profile->paused = Qfalse;
564
- profile->measurement_at_pause_resume = profile->measurer->measure();
565
- st_foreach(profile->threads_tbl, unpause_thread, (st_data_t) profile);
566
- }
567
-
568
- return rb_block_given_p() ? rb_ensure(rb_yield, self, prof_pause, self) : self;
569
- }
570
-
571
- /* call-seq:
572
- stop -> self
573
-
574
- Stops collecting profile data.*/
575
- static VALUE
576
- prof_stop(VALUE self)
577
- {
578
- prof_profile_t* profile = prof_get_profile(self);
579
-
580
- if (profile->running == Qfalse)
581
- {
582
- rb_raise(rb_eRuntimeError, "RubyProf.start was not yet called");
583
- }
584
-
585
- prof_remove_hook();
586
-
587
- /* close trace file if open */
588
- if (trace_file != NULL)
589
- {
590
- if (trace_file !=stderr && trace_file != stdout)
591
- {
592
- #ifdef _MSC_VER
593
- _fcloseall();
594
- #else
595
- fclose(trace_file);
596
- #endif
597
- }
598
- trace_file = NULL;
599
- }
600
-
601
- prof_pop_threads(profile);
602
-
603
- /* Unset the last_thread_data (very important!)
604
- and the threads table */
605
- profile->running = profile->paused = Qfalse;
606
- profile->last_thread_data = NULL;
607
-
608
- /* Post process result */
609
- rb_funcall(self, rb_intern("post_process") , 0);
610
-
611
- return self;
612
- }
613
-
614
- /* call-seq:
615
- profile(&block) -> self
616
- profile(options, &block) -> self
617
-
618
- Profiles the specified block and returns a RubyProf::Profile
619
- object. Arguments are passed to Profile initialize method.
620
- */
621
- static VALUE
622
- prof_profile(int argc, VALUE *argv, VALUE klass)
623
- {
624
- int result;
625
- VALUE profile = rb_class_new_instance(argc, argv, cProfile);
626
-
627
- if (!rb_block_given_p())
628
- {
629
- rb_raise(rb_eArgError, "A block must be provided to the profile method.");
630
- }
631
-
632
- prof_start(profile);
633
- rb_protect(rb_yield, profile, &result);
634
- return prof_stop(profile);
635
- }
636
-
637
- /* call-seq:
638
- threads -> array of RubyProf::Thread
639
-
640
- Returns an array of RubyProf::Thread instances that were executed
641
- while the the program was being run. */
642
- static VALUE
643
- prof_threads(VALUE self)
644
- {
645
- VALUE result = rb_ary_new();
646
- prof_profile_t* profile = prof_get_profile(self);
647
- st_foreach(profile->threads_tbl, collect_threads, result);
648
- return result;
649
- }
650
-
651
- void Init_ruby_prof()
652
- {
653
- mProf = rb_define_module("RubyProf");
654
-
655
- rp_init_measure();
656
- rp_init_method_info();
657
- rp_init_call_info();
658
- rp_init_thread();
659
-
660
- cProfile = rb_define_class_under(mProf, "Profile", rb_cObject);
661
- rb_define_singleton_method(cProfile, "profile", prof_profile, -1);
662
- rb_define_alloc_func (cProfile, prof_allocate);
663
- rb_define_method(cProfile, "initialize", prof_initialize, -1);
664
- rb_define_method(cProfile, "start", prof_start, 0);
665
- rb_define_method(cProfile, "stop", prof_stop, 0);
666
- rb_define_method(cProfile, "resume", prof_resume, 0);
667
- rb_define_method(cProfile, "pause", prof_pause, 0);
668
- rb_define_method(cProfile, "running?", prof_running, 0);
669
- rb_define_method(cProfile, "paused?", prof_paused, 0);
670
- rb_define_method(cProfile, "threads", prof_threads, 0);
671
- }
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
+ /* ruby-prof tracks the time spent executing every method in ruby programming.
5
+ The main players are:
6
+
7
+ profile_t - This represents 1 profile.
8
+ thread_data_t - Stores data about a single thread.
9
+ prof_stack_t - The method call stack in a particular thread
10
+ prof_method_t - Profiling information about each method
11
+ prof_call_info_t - Keeps track a method's callers and callees.
12
+
13
+ The final result is an instance of a profile object which has a hash table of
14
+ thread_data_t, keyed on the thread id. Each thread in turn has a hash table
15
+ of prof_method_t, keyed on the method id. A hash table is used for quick
16
+ look up when doing a profile. However, it is exposed to Ruby as an array.
17
+
18
+ Each prof_method_t has two hash tables, parent and children, of prof_call_info_t.
19
+ These objects keep track of a method's callers (who called the method) and its
20
+ callees (who the method called). These are keyed the method id, but once again,
21
+ are exposed to Ruby as arrays. Each prof_call_into_t maintains a pointer to the
22
+ caller or callee method, thereby making it easy to navigate through the call
23
+ hierarchy in ruby - which is very helpful for creating call graphs.
24
+ */
25
+
26
+ #include "ruby_prof.h"
27
+
28
+ #include "rp_allocation.h"
29
+ #include "rp_measurement.h"
30
+ #include "rp_method.h"
31
+ #include "rp_call_info.h"
32
+ #include "rp_profile.h"
33
+ #include "rp_stack.h"
34
+ #include "rp_thread.h"
35
+
36
+ VALUE mProf;
37
+
38
+ void Init_ruby_prof()
39
+ {
40
+ mProf = rb_define_module("RubyProf");
41
+
42
+ rp_init_allocation();
43
+ rp_init_call_info();
44
+ rp_init_measure();
45
+ rp_init_method_info();
46
+ rp_init_profile();
47
+ rp_init_thread();
48
+ }