ruby-prof 0.16.2 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
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,273 +1,337 @@
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 "ruby_prof.h"
5
-
6
- VALUE cRpThread;
7
-
8
- /* ====== thread_data_t ====== */
9
- thread_data_t*
10
- thread_data_create()
11
- {
12
- thread_data_t* result = ALLOC(thread_data_t);
13
- result->stack = prof_stack_create();
14
- result->method_table = method_table_create();
15
- result->object = Qnil;
16
- result->methods = Qnil;
17
- return result;
18
- }
19
-
20
- /* The underlying c structures are freed when the parent profile is freed.
21
- However, on shutdown the Ruby GC frees objects in any will-nilly order.
22
- That means the ruby thread object wrapping the c thread struct may
23
- be freed before the parent profile. Thus we add in a free function
24
- for the garbage collector so that if it does get called will nil
25
- out our Ruby object reference.*/
26
- static void
27
- thread_data_ruby_gc_free(thread_data_t* thread_data)
28
- {
29
- /* Has this thread object been accessed by Ruby? If
30
- yes clean it up so to avoid a segmentation fault. */
31
- if (thread_data->object != Qnil)
32
- {
33
- RDATA(thread_data->object)->data = NULL;
34
- RDATA(thread_data->object)->dfree = NULL;
35
- RDATA(thread_data->object)->dmark = NULL;
36
- }
37
- thread_data->object = Qnil;
38
- }
39
-
40
- static void
41
- thread_data_free(thread_data_t* thread_data)
42
- {
43
- thread_data_ruby_gc_free(thread_data);
44
- method_table_free(thread_data->method_table);
45
- prof_stack_free(thread_data->stack);
46
-
47
- thread_data->thread_id = Qnil;
48
-
49
- xfree(thread_data);
50
- }
51
-
52
- static int
53
- mark_methods(st_data_t key, st_data_t value, st_data_t result)
54
- {
55
- prof_method_t *method = (prof_method_t *) value;
56
- prof_method_mark(method);
57
- return ST_CONTINUE;
58
- }
59
-
60
- void
61
- prof_thread_mark(thread_data_t *thread)
62
- {
63
- if (thread->object != Qnil)
64
- rb_gc_mark(thread->object);
65
-
66
- if (thread->methods != Qnil)
67
- rb_gc_mark(thread->methods);
68
-
69
- if (thread->thread_id != Qnil)
70
- rb_gc_mark(thread->thread_id);
71
-
72
- if (thread->fiber_id != Qnil)
73
- rb_gc_mark(thread->fiber_id);
74
-
75
- st_foreach(thread->method_table, mark_methods, 0);
76
- }
77
-
78
- VALUE
79
- prof_thread_wrap(thread_data_t *thread)
80
- {
81
- if (thread->object == Qnil) {
82
- thread->object = Data_Wrap_Struct(cRpThread, prof_thread_mark, thread_data_ruby_gc_free, thread);
83
- }
84
- return thread->object;
85
- }
86
-
87
- static thread_data_t*
88
- prof_get_thread(VALUE self)
89
- {
90
- /* Can't use Data_Get_Struct because that triggers the event hook
91
- ending up in endless recursion. */
92
- thread_data_t* result = DATA_PTR(self);
93
- if (!result)
94
- rb_raise(rb_eRuntimeError, "This RubyProf::Thread instance has already been freed, likely because its profile has been freed.");
95
-
96
- return result;
97
- }
98
-
99
- /* ====== Thread Table ====== */
100
- /* The thread table is hash keyed on ruby thread_id that stores instances
101
- of thread_data_t. */
102
-
103
- st_table *
104
- threads_table_create()
105
- {
106
- return st_init_numtable();
107
- }
108
-
109
- static int
110
- thread_table_free_iterator(st_data_t key, st_data_t value, st_data_t dummy)
111
- {
112
- thread_data_free((thread_data_t*)value);
113
- return ST_CONTINUE;
114
- }
115
-
116
- void
117
- threads_table_free(st_table *table)
118
- {
119
- st_foreach(table, thread_table_free_iterator, 0);
120
- st_free_table(table);
121
- }
122
-
123
- size_t
124
- threads_table_insert(prof_profile_t* profile, VALUE fiber, thread_data_t *thread_data)
125
- {
126
- /* Its too slow to key on the real thread id so just typecast thread instead. */
127
- return st_insert(profile->threads_tbl, (st_data_t) fiber, (st_data_t) thread_data);
128
- }
129
-
130
- thread_data_t *
131
- threads_table_lookup(prof_profile_t* profile, VALUE thread_id, VALUE fiber_id)
132
- {
133
- thread_data_t* result;
134
- st_data_t val;
135
-
136
- /* If we should merge fibers, we use the thread_id as key, otherwise the fiber id.
137
- None of this is perfect, as garbage collected fiber/thread might be reused again later.
138
- A real solution would require integration with the garbage collector.
139
- */
140
- VALUE key = profile->merge_fibers ? thread_id : fiber_id;
141
- if (st_lookup(profile->threads_tbl, (st_data_t) key, &val))
142
- {
143
- result = (thread_data_t *) val;
144
- }
145
- else
146
- {
147
- result = thread_data_create();
148
- result->thread_id = thread_id;
149
- /* We set fiber id to 0 in the merge fiber case. Real fibers never have id 0,
150
- so we can identify them later during printing.
151
- */
152
- result->fiber_id = profile->merge_fibers ? INT2FIX(0) : fiber_id;
153
- /* Insert the table */
154
- threads_table_insert(profile, key, result);
155
- }
156
- return result;
157
- }
158
-
159
- thread_data_t *
160
- switch_thread(void* prof, VALUE thread_id, VALUE fiber_id)
161
- {
162
- prof_profile_t* profile = (prof_profile_t*)prof;
163
- double measurement = profile->measurer->measure();
164
-
165
- /* Get new thread information. */
166
- thread_data_t *thread_data = threads_table_lookup(profile, thread_id, fiber_id);
167
-
168
- /* Get current frame for this thread */
169
- prof_frame_t *frame = prof_stack_peek(thread_data->stack);
170
-
171
- /* Update the time this thread waited for another thread */
172
- if (frame)
173
- {
174
- frame->wait_time += measurement - frame->switch_time;
175
- frame->switch_time = measurement;
176
- }
177
-
178
- /* Save on the last thread the time of the context switch
179
- and reset this thread's last context switch to 0.*/
180
- if (profile->last_thread_data)
181
- {
182
- prof_frame_t *last_frame = prof_stack_peek(profile->last_thread_data->stack);
183
- if (last_frame)
184
- last_frame->switch_time = measurement;
185
- }
186
-
187
- profile->last_thread_data = thread_data;
188
- return thread_data;
189
- }
190
-
191
- int pause_thread(st_data_t key, st_data_t value, st_data_t data)
192
- {
193
- thread_data_t* thread_data = (thread_data_t *) value;
194
- prof_profile_t* profile = (prof_profile_t*)data;
195
-
196
- prof_frame_t* frame = prof_stack_peek(thread_data->stack);
197
- prof_frame_pause(frame, profile->measurement_at_pause_resume);
198
-
199
- return ST_CONTINUE;
200
- }
201
-
202
- int unpause_thread(st_data_t key, st_data_t value, st_data_t data)
203
- {
204
- thread_data_t* thread_data = (thread_data_t *) value;
205
- prof_profile_t* profile = (prof_profile_t*)data;
206
-
207
- prof_frame_t* frame = prof_stack_peek(thread_data->stack);
208
- prof_frame_unpause(frame, profile->measurement_at_pause_resume);
209
-
210
- return ST_CONTINUE;
211
- }
212
-
213
- static int
214
- collect_methods(st_data_t key, st_data_t value, st_data_t result)
215
- {
216
- /* Called for each method stored in a thread's method table.
217
- We want to store the method info information into an array.*/
218
- VALUE methods = (VALUE) result;
219
- prof_method_t *method = (prof_method_t *) value;
220
- rb_ary_push(methods, prof_method_wrap(method));
221
-
222
- return ST_CONTINUE;
223
- }
224
-
225
-
226
- /* call-seq:
227
- id -> number
228
-
229
- Returns the id of this thread. */
230
- static VALUE
231
- prof_thread_id(VALUE self)
232
- {
233
- thread_data_t* thread = prof_get_thread(self);
234
- return thread->thread_id;
235
- }
236
-
237
- /* call-seq:
238
- fiber_id -> number
239
-
240
- Returns the fiber id of this thread. */
241
- static VALUE
242
- prof_fiber_id(VALUE self)
243
- {
244
- thread_data_t* thread = prof_get_thread(self);
245
- return thread->fiber_id;
246
- }
247
-
248
- /* call-seq:
249
- methods -> Array of MethodInfo
250
-
251
- Returns an array of methods that were called from this
252
- thread during program execution. */
253
- static VALUE
254
- prof_thread_methods(VALUE self)
255
- {
256
- thread_data_t* thread = prof_get_thread(self);
257
- if (thread->methods == Qnil)
258
- {
259
- thread->methods = rb_ary_new();
260
- st_foreach(thread->method_table, collect_methods, thread->methods);
261
- }
262
- return thread->methods;
263
- }
264
-
265
- void rp_init_thread()
266
- {
267
- cRpThread = rb_define_class_under(mProf, "Thread", rb_cObject);
268
- rb_undef_method(CLASS_OF(cRpThread), "new");
269
-
270
- rb_define_method(cRpThread, "id", prof_thread_id, 0);
271
- rb_define_method(cRpThread, "fiber_id", prof_fiber_id, 0);
272
- rb_define_method(cRpThread, "methods", prof_thread_methods, 0);
273
- }
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
+ /* Document-class: RubyProf::Thread
5
+
6
+ The Thread class contains profile results for a single fiber (note a Ruby thread can run multiple fibers).
7
+ You cannot create an instance of RubyProf::Thread, instead you access it from a RubyProf::Profile object.
8
+
9
+ profile = RubyProf::Profile.profile do
10
+ ...
11
+ end
12
+
13
+ profile.threads.each do |thread|
14
+ thread.root_methods.sort.each do |method|
15
+ puts method.total_time
16
+ end
17
+ end
18
+ */
19
+
20
+ #include "rp_thread.h"
21
+ #include "rp_profile.h"
22
+
23
+ VALUE cRpThread;
24
+
25
+ /* ====== thread_data_t ====== */
26
+ thread_data_t*
27
+ thread_data_create(void)
28
+ {
29
+ thread_data_t* result = ALLOC(thread_data_t);
30
+ result->stack = prof_stack_create();
31
+ result->method_table = method_table_create();
32
+ result->object = Qnil;
33
+ result->methods = Qnil;
34
+ result->fiber_id = Qnil;
35
+ result->thread_id = Qnil;
36
+ result->trace = true;
37
+ result->fiber = Qnil;
38
+ return result;
39
+ }
40
+
41
+ static void
42
+ prof_thread_free(thread_data_t* thread_data)
43
+ {
44
+ /* Has this method object been accessed by Ruby? If
45
+ yes then set its data to nil to avoid a segmentation fault on the next mark and sweep. */
46
+ if (thread_data->object != Qnil)
47
+ {
48
+ RDATA(thread_data->object)->dmark = NULL;
49
+ RDATA(thread_data->object)->dfree = NULL;
50
+ RDATA(thread_data->object)->data = NULL;
51
+ thread_data->object = Qnil;
52
+ }
53
+
54
+ method_table_free(thread_data->method_table);
55
+ prof_stack_free(thread_data->stack);
56
+
57
+
58
+ xfree(thread_data);
59
+ }
60
+
61
+ static int
62
+ mark_methods(st_data_t key, st_data_t value, st_data_t result)
63
+ {
64
+ prof_method_t *method = (prof_method_t *) value;
65
+ prof_method_mark(method);
66
+ return ST_CONTINUE;
67
+ }
68
+
69
+ size_t
70
+ prof_thread_size(const void *data)
71
+ {
72
+ return sizeof(prof_call_info_t);
73
+ }
74
+
75
+ void
76
+ prof_thread_mark(void *data)
77
+ {
78
+ thread_data_t *thread = (thread_data_t*)data;
79
+
80
+ if (thread->object != Qnil)
81
+ rb_gc_mark(thread->object);
82
+
83
+ if (thread->methods != Qnil)
84
+ rb_gc_mark(thread->methods);
85
+
86
+ if (thread->fiber_id != Qnil)
87
+ rb_gc_mark(thread->fiber_id);
88
+
89
+ if (thread->thread_id != Qnil)
90
+ rb_gc_mark(thread->thread_id);
91
+
92
+ st_foreach(thread->method_table, mark_methods, 0);
93
+ }
94
+
95
+
96
+ VALUE
97
+ prof_thread_wrap(thread_data_t *thread)
98
+ {
99
+ if (thread->object == Qnil)
100
+ {
101
+ thread->object = Data_Wrap_Struct(cRpThread, prof_thread_mark, NULL, thread);
102
+ }
103
+ return thread->object;
104
+ }
105
+
106
+ static VALUE
107
+ prof_thread_allocate(VALUE klass)
108
+ {
109
+ thread_data_t* thread_data = thread_data_create();
110
+ thread_data->object = prof_thread_wrap(thread_data);
111
+ return thread_data->object;
112
+ }
113
+
114
+ static thread_data_t*
115
+ prof_get_thread(VALUE self)
116
+ {
117
+ /* Can't use Data_Get_Struct because that triggers the event hook
118
+ ending up in endless recursion. */
119
+ thread_data_t* result = DATA_PTR(self);
120
+ if (!result)
121
+ rb_raise(rb_eRuntimeError, "This RubyProf::Thread instance has already been freed, likely because its profile has been freed.");
122
+
123
+ return result;
124
+ }
125
+
126
+ /* ====== Thread Table ====== */
127
+ /* The thread table is hash keyed on ruby thread_id that stores instances
128
+ of thread_data_t. */
129
+
130
+ st_table *
131
+ threads_table_create()
132
+ {
133
+ return st_init_numtable();
134
+ }
135
+
136
+ static int
137
+ thread_table_free_iterator(st_data_t key, st_data_t value, st_data_t dummy)
138
+ {
139
+ prof_thread_free((thread_data_t*)value);
140
+ return ST_CONTINUE;
141
+ }
142
+
143
+ void
144
+ threads_table_free(st_table *table)
145
+ {
146
+ st_foreach(table, thread_table_free_iterator, 0);
147
+ st_free_table(table);
148
+ }
149
+
150
+ thread_data_t *
151
+ threads_table_lookup(void *prof, VALUE fiber)
152
+ {
153
+ prof_profile_t *profile = prof;
154
+ thread_data_t* result = NULL;
155
+ st_data_t val;
156
+
157
+ if (st_lookup(profile->threads_tbl, fiber, &val))
158
+ {
159
+ result = (thread_data_t*)val;
160
+ }
161
+
162
+ return result;
163
+ }
164
+
165
+ thread_data_t*
166
+ threads_table_insert(void *prof, VALUE fiber)
167
+ {
168
+ prof_profile_t *profile = prof;
169
+ thread_data_t *result = thread_data_create();
170
+ VALUE thread = rb_thread_current();
171
+
172
+ result->fiber = fiber;
173
+ result->fiber_id = rb_obj_id(fiber);
174
+ result->thread_id = rb_obj_id(thread);
175
+ st_insert(profile->threads_tbl, (st_data_t)fiber, (st_data_t)result);
176
+
177
+ // Are we tracing this thread?
178
+ if (profile->include_threads_tbl && !st_lookup(profile->include_threads_tbl, thread, 0))
179
+ {
180
+ result->trace= false;
181
+ }
182
+ else if (profile->exclude_threads_tbl && st_lookup(profile->exclude_threads_tbl, thread, 0))
183
+ {
184
+ result->trace = false;
185
+ }
186
+ else
187
+ {
188
+ result->trace = true;
189
+ }
190
+
191
+ return result;
192
+ }
193
+
194
+ void
195
+ switch_thread(void *prof, thread_data_t *thread_data, double measurement)
196
+ {
197
+ prof_profile_t *profile = prof;
198
+
199
+ /* Get current frame for this thread */
200
+ prof_frame_t* frame = thread_data->stack->ptr;
201
+ frame->wait_time += measurement - frame->switch_time;
202
+ frame->switch_time = measurement;
203
+
204
+ /* Save on the last thread the time of the context switch
205
+ and reset this thread's last context switch to 0.*/
206
+ if (profile->last_thread_data)
207
+ {
208
+ prof_frame_t* last_frame = profile->last_thread_data->stack->ptr;
209
+ if (last_frame)
210
+ last_frame->switch_time = measurement;
211
+ }
212
+
213
+ profile->last_thread_data = thread_data;
214
+ }
215
+
216
+ int pause_thread(st_data_t key, st_data_t value, st_data_t data)
217
+ {
218
+ thread_data_t* thread_data = (thread_data_t *) value;
219
+ prof_profile_t* profile = (prof_profile_t*)data;
220
+
221
+ prof_frame_t* frame = thread_data->stack->ptr;
222
+ prof_frame_pause(frame, profile->measurement_at_pause_resume);
223
+
224
+ return ST_CONTINUE;
225
+ }
226
+
227
+ int unpause_thread(st_data_t key, st_data_t value, st_data_t data)
228
+ {
229
+ thread_data_t* thread_data = (thread_data_t *) value;
230
+ prof_profile_t* profile = (prof_profile_t*)data;
231
+
232
+ prof_frame_t* frame = thread_data->stack->ptr;
233
+ prof_frame_unpause(frame, profile->measurement_at_pause_resume);
234
+
235
+ return ST_CONTINUE;
236
+ }
237
+
238
+ static int
239
+ collect_methods(st_data_t key, st_data_t value, st_data_t result)
240
+ {
241
+ /* Called for each method stored in a thread's method table.
242
+ We want to store the method info information into an array.*/
243
+ VALUE methods = (VALUE) result;
244
+ prof_method_t *method = (prof_method_t *) value;
245
+
246
+ if (!method->excluded)
247
+ {
248
+ rb_ary_push(methods, prof_method_wrap(method));
249
+ }
250
+
251
+ return ST_CONTINUE;
252
+ }
253
+
254
+ /* call-seq:
255
+ id -> number
256
+
257
+ Returns the thread id of this thread. */
258
+ static VALUE
259
+ prof_thread_id(VALUE self)
260
+ {
261
+ thread_data_t* thread = prof_get_thread(self);
262
+ return thread->thread_id;
263
+ }
264
+
265
+ /* call-seq:
266
+ fiber_id -> number
267
+
268
+ Returns the fiber id of this thread. */
269
+ static VALUE
270
+ prof_fiber_id(VALUE self)
271
+ {
272
+ thread_data_t* thread = prof_get_thread(self);
273
+ return thread->fiber_id;
274
+ }
275
+
276
+ /* call-seq:
277
+ methods -> [RubyProf::MethodInfo]
278
+
279
+ Returns an array of methods that were called from this
280
+ thread during program execution. */
281
+ static VALUE
282
+ prof_thread_methods(VALUE self)
283
+ {
284
+ thread_data_t* thread = prof_get_thread(self);
285
+ if (thread->methods == Qnil)
286
+ {
287
+ thread->methods = rb_ary_new();
288
+ st_foreach(thread->method_table, collect_methods, thread->methods);
289
+ }
290
+ return thread->methods;
291
+ }
292
+
293
+ /* :nodoc: */
294
+ static VALUE
295
+ prof_thread_dump(VALUE self)
296
+ {
297
+ thread_data_t* thread_data = DATA_PTR(self);
298
+
299
+ VALUE result = rb_hash_new();
300
+ rb_hash_aset(result, ID2SYM(rb_intern("fiber_id")), thread_data->fiber_id);
301
+ rb_hash_aset(result, ID2SYM(rb_intern("methods")), prof_thread_methods(self));
302
+
303
+ return result;
304
+ }
305
+
306
+ /* :nodoc: */
307
+ static VALUE
308
+ prof_thread_load(VALUE self, VALUE data)
309
+ {
310
+ thread_data_t* thread_data = DATA_PTR(self);
311
+ thread_data->object = self;
312
+
313
+ thread_data->fiber_id = rb_hash_aref(data, ID2SYM(rb_intern("fiber_id")));
314
+ VALUE methods = rb_hash_aref(data, ID2SYM(rb_intern("methods")));
315
+
316
+ for (int i = 0; i < rb_array_len(methods); i++)
317
+ {
318
+ VALUE method = rb_ary_entry(methods, i);
319
+ prof_method_t *method_data = DATA_PTR(method);
320
+ method_table_insert(thread_data->method_table, method_data->key, method_data);
321
+ }
322
+
323
+ return data;
324
+ }
325
+
326
+ void rp_init_thread(void)
327
+ {
328
+ cRpThread = rb_define_class_under(mProf, "Thread", rb_cData);
329
+ rb_undef_method(CLASS_OF(cRpThread), "new");
330
+ rb_define_alloc_func(cRpThread, prof_thread_allocate);
331
+
332
+ rb_define_method(cRpThread, "id", prof_thread_id, 0);
333
+ rb_define_method(cRpThread, "fiber_id", prof_fiber_id, 0);
334
+ rb_define_method(cRpThread, "methods", prof_thread_methods, 0);
335
+ rb_define_method(cRpThread, "_dump_data", prof_thread_dump, 0);
336
+ rb_define_method(cRpThread, "_load_data", prof_thread_load, 1);
337
+ }