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.
- checksums.yaml +5 -5
- data/CHANGES +579 -371
- data/LICENSE +24 -23
- data/README.rdoc +5 -433
- data/Rakefile +98 -110
- data/bin/ruby-prof +328 -329
- data/bin/ruby-prof-check-trace +45 -0
- data/ext/ruby_prof/extconf.rb +16 -59
- data/ext/ruby_prof/rp_aggregate_call_tree.c +59 -0
- data/ext/ruby_prof/rp_aggregate_call_tree.h +13 -0
- data/ext/ruby_prof/rp_allocation.c +287 -0
- data/ext/ruby_prof/rp_allocation.h +31 -0
- data/ext/ruby_prof/rp_call_tree.c +369 -0
- data/ext/ruby_prof/rp_call_tree.h +43 -0
- data/ext/ruby_prof/rp_call_trees.c +288 -0
- data/ext/ruby_prof/rp_call_trees.h +28 -0
- data/ext/ruby_prof/rp_measure_allocations.c +50 -65
- data/ext/ruby_prof/rp_measure_memory.c +42 -73
- data/ext/ruby_prof/rp_measure_process_time.c +65 -71
- data/ext/ruby_prof/rp_measure_wall_time.c +64 -42
- data/ext/ruby_prof/rp_measurement.c +237 -0
- data/ext/ruby_prof/rp_measurement.h +50 -0
- data/ext/ruby_prof/rp_method.c +491 -420
- data/ext/ruby_prof/rp_method.h +62 -57
- data/ext/ruby_prof/rp_profile.c +908 -0
- data/ext/ruby_prof/rp_profile.h +35 -0
- data/ext/ruby_prof/rp_stack.c +212 -128
- data/ext/ruby_prof/rp_stack.h +53 -51
- data/ext/ruby_prof/rp_thread.c +362 -268
- data/ext/ruby_prof/rp_thread.h +39 -27
- data/ext/ruby_prof/ruby_prof.c +52 -695
- data/ext/ruby_prof/ruby_prof.h +26 -55
- data/ext/ruby_prof/vc/ruby_prof.sln +28 -21
- data/ext/ruby_prof/vc/{ruby_prof_20.vcxproj → ruby_prof.vcxproj} +56 -8
- data/lib/ruby-prof.rb +52 -67
- data/lib/ruby-prof/assets/call_stack_printer.html.erb +710 -0
- data/lib/ruby-prof/assets/call_stack_printer.png +0 -0
- data/lib/ruby-prof/assets/graph_printer.html.erb +355 -0
- data/lib/ruby-prof/call_tree.rb +57 -0
- data/lib/ruby-prof/call_tree_visitor.rb +36 -0
- data/lib/ruby-prof/compatibility.rb +99 -169
- data/lib/ruby-prof/exclude_common_methods.rb +198 -0
- data/lib/ruby-prof/measurement.rb +17 -0
- data/lib/ruby-prof/method_info.rb +78 -131
- data/lib/ruby-prof/printers/abstract_printer.rb +137 -85
- data/lib/ruby-prof/printers/call_info_printer.rb +53 -41
- data/lib/ruby-prof/printers/call_stack_printer.rb +180 -773
- data/lib/ruby-prof/printers/call_tree_printer.rb +151 -92
- data/lib/ruby-prof/printers/dot_printer.rb +132 -132
- data/lib/ruby-prof/printers/flat_printer.rb +53 -69
- data/lib/ruby-prof/printers/graph_html_printer.rb +63 -255
- data/lib/ruby-prof/printers/graph_printer.rb +113 -116
- data/lib/ruby-prof/printers/multi_printer.rb +127 -56
- data/lib/ruby-prof/profile.rb +37 -77
- data/lib/ruby-prof/rack.rb +62 -15
- data/lib/ruby-prof/task.rb +147 -147
- data/lib/ruby-prof/thread.rb +10 -12
- data/lib/ruby-prof/version.rb +3 -0
- data/lib/unprof.rb +10 -10
- data/ruby-prof.gemspec +65 -61
- data/test/abstract_printer_test.rb +26 -0
- data/test/alias_test.rb +126 -0
- data/test/basic_test.rb +43 -128
- data/test/call_tree_visitor_test.rb +32 -0
- data/test/call_trees_test.rb +66 -0
- data/test/duplicate_names_test.rb +32 -32
- data/test/dynamic_method_test.rb +53 -74
- data/test/enumerable_test.rb +21 -16
- data/test/exceptions_test.rb +24 -16
- data/test/exclude_methods_test.rb +151 -0
- data/test/exclude_threads_test.rb +53 -54
- data/test/fiber_test.rb +129 -65
- data/test/gc_test.rb +90 -0
- data/test/inverse_call_tree_test.rb +175 -0
- data/test/line_number_test.rb +158 -71
- data/test/marshal_test.rb +113 -0
- data/test/measure_allocations.rb +30 -0
- data/test/measure_allocations_test.rb +375 -25
- data/test/measure_allocations_trace_test.rb +375 -0
- data/test/measure_memory_trace_test.rb +1101 -0
- data/test/measure_process_time_test.rb +785 -62
- data/test/measure_times.rb +56 -0
- data/test/measure_wall_time_test.rb +434 -254
- data/test/multi_printer_test.rb +71 -82
- data/test/no_method_class_test.rb +15 -15
- data/test/pause_resume_test.rb +175 -166
- data/test/prime.rb +54 -54
- data/test/prime_script.rb +6 -0
- data/test/printer_call_stack_test.rb +27 -0
- data/test/printer_call_tree_test.rb +30 -0
- data/test/printer_flat_test.rb +99 -0
- data/test/printer_graph_html_test.rb +59 -0
- data/test/printer_graph_test.rb +40 -0
- data/test/printers_test.rb +141 -257
- data/test/printing_recursive_graph_test.rb +81 -0
- data/test/profile_test.rb +16 -0
- data/test/rack_test.rb +93 -0
- data/test/recursive_test.rb +206 -215
- data/test/singleton_test.rb +38 -38
- data/test/stack_printer_test.rb +64 -78
- data/test/start_stop_test.rb +109 -112
- data/test/test_helper.rb +13 -115
- data/test/thread_test.rb +144 -178
- data/test/unique_call_path_test.rb +120 -224
- data/test/yarv_test.rb +56 -0
- metadata +77 -133
- data/doc/LICENSE.html +0 -155
- data/doc/README_rdoc.html +0 -648
- data/doc/Rack.html +0 -167
- data/doc/Rack/RubyProf.html +0 -319
- data/doc/RubyProf.html +0 -1000
- data/doc/RubyProf/AbstractPrinter.html +0 -580
- data/doc/RubyProf/AggregateCallInfo.html +0 -570
- data/doc/RubyProf/CallInfo.html +0 -512
- data/doc/RubyProf/CallInfoPrinter.html +0 -190
- data/doc/RubyProf/CallInfoVisitor.html +0 -332
- data/doc/RubyProf/CallStackPrinter.html +0 -1600
- data/doc/RubyProf/CallTreePrinter.html +0 -413
- data/doc/RubyProf/Cmd.html +0 -669
- data/doc/RubyProf/DotPrinter.html +0 -312
- data/doc/RubyProf/FlatPrinter.html +0 -229
- data/doc/RubyProf/FlatPrinterWithLineNumbers.html +0 -267
- data/doc/RubyProf/GraphHtmlPrinter.html +0 -630
- data/doc/RubyProf/GraphPrinter.html +0 -209
- data/doc/RubyProf/MethodInfo.html +0 -713
- data/doc/RubyProf/MultiPrinter.html +0 -407
- data/doc/RubyProf/Profile.html +0 -821
- data/doc/RubyProf/ProfileTask.html +0 -532
- data/doc/RubyProf/Test.html +0 -578
- data/doc/RubyProf/Thread.html +0 -262
- data/doc/created.rid +0 -32
- data/doc/examples/flat_txt.html +0 -191
- data/doc/examples/graph_txt.html +0 -305
- data/doc/images/add.png +0 -0
- data/doc/images/brick.png +0 -0
- data/doc/images/brick_link.png +0 -0
- data/doc/images/bug.png +0 -0
- data/doc/images/bullet_black.png +0 -0
- data/doc/images/bullet_toggle_minus.png +0 -0
- data/doc/images/bullet_toggle_plus.png +0 -0
- data/doc/images/date.png +0 -0
- data/doc/images/delete.png +0 -0
- data/doc/images/find.png +0 -0
- data/doc/images/loadingAnimation.gif +0 -0
- data/doc/images/macFFBgHack.png +0 -0
- data/doc/images/package.png +0 -0
- data/doc/images/page_green.png +0 -0
- data/doc/images/page_white_text.png +0 -0
- data/doc/images/page_white_width.png +0 -0
- data/doc/images/plugin.png +0 -0
- data/doc/images/ruby.png +0 -0
- data/doc/images/tag_blue.png +0 -0
- data/doc/images/tag_green.png +0 -0
- data/doc/images/transparent.png +0 -0
- data/doc/images/wrench.png +0 -0
- data/doc/images/wrench_orange.png +0 -0
- data/doc/images/zoom.png +0 -0
- data/doc/index.html +0 -647
- data/doc/js/darkfish.js +0 -155
- data/doc/js/jquery.js +0 -18
- data/doc/js/navigation.js +0 -142
- data/doc/js/search.js +0 -94
- data/doc/js/search_index.js +0 -1
- data/doc/js/searcher.js +0 -228
- data/doc/rdoc.css +0 -543
- data/doc/table_of_contents.html +0 -462
- data/examples/empty.png +0 -0
- data/examples/flat.txt +0 -55
- data/examples/graph.dot +0 -106
- data/examples/graph.html +0 -823
- data/examples/graph.png +0 -0
- data/examples/graph.txt +0 -170
- data/examples/minus.png +0 -0
- data/examples/multi.flat.txt +0 -23
- data/examples/multi.graph.html +0 -906
- data/examples/multi.grind.dat +0 -194
- data/examples/multi.stack.html +0 -573
- data/examples/plus.png +0 -0
- data/examples/stack.html +0 -573
- data/ext/ruby_prof/rp_call_info.c +0 -407
- data/ext/ruby_prof/rp_call_info.h +0 -48
- data/ext/ruby_prof/rp_measure.c +0 -48
- data/ext/ruby_prof/rp_measure.h +0 -45
- data/ext/ruby_prof/rp_measure_cpu_time.c +0 -112
- data/ext/ruby_prof/rp_measure_gc_runs.c +0 -65
- data/ext/ruby_prof/rp_measure_gc_time.c +0 -57
- data/ext/ruby_prof/vc/ruby_prof_18.vcxproj +0 -108
- data/ext/ruby_prof/vc/ruby_prof_19.vcxproj +0 -110
- data/ext/ruby_prof/version.h +0 -7
- data/lib/ruby-prof/aggregate_call_info.rb +0 -72
- data/lib/ruby-prof/call_info.rb +0 -89
- data/lib/ruby-prof/call_info_visitor.rb +0 -44
- data/lib/ruby-prof/images/empty.png +0 -0
- data/lib/ruby-prof/images/minus.png +0 -0
- data/lib/ruby-prof/images/plus.png +0 -0
- data/lib/ruby-prof/printers/flat_printer_with_line_numbers.rb +0 -57
- data/lib/ruby-prof/test.rb +0 -150
- data/test/aggregate_test.rb +0 -136
- data/test/call_info_test.rb +0 -78
- data/test/call_info_visitor_test.rb +0 -31
- data/test/exec_test.rb +0 -14
- data/test/measure_cpu_time_test.rb +0 -220
- data/test/measure_gc_runs_test.rb +0 -32
- data/test/measure_gc_time_test.rb +0 -36
- data/test/measure_memory_test.rb +0 -31
- data/test/method_elimination_test.rb +0 -84
- data/test/module_test.rb +0 -45
- data/test/stack_test.rb +0 -138
- data/test/test_suite.rb +0 -37
data/ext/ruby_prof/rp_thread.c
CHANGED
@@ -1,268 +1,362 @@
|
|
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
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
}
|
39
|
-
|
40
|
-
static
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
}
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
{
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
}
|
87
|
-
|
88
|
-
static thread_data_t*
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
{
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
}
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
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
|
+
#include "rp_thread.h"
|
20
|
+
#include "rp_profile.h"
|
21
|
+
|
22
|
+
VALUE cRpThread;
|
23
|
+
|
24
|
+
// ====== thread_data_t ======
|
25
|
+
thread_data_t* thread_data_create(void)
|
26
|
+
{
|
27
|
+
thread_data_t* result = ALLOC(thread_data_t);
|
28
|
+
result->stack = prof_stack_create();
|
29
|
+
result->method_table = method_table_create();
|
30
|
+
result->call_tree = NULL;
|
31
|
+
result->object = Qnil;
|
32
|
+
result->methods = Qnil;
|
33
|
+
result->fiber_id = Qnil;
|
34
|
+
result->thread_id = Qnil;
|
35
|
+
result->trace = true;
|
36
|
+
result->fiber = Qnil;
|
37
|
+
return result;
|
38
|
+
}
|
39
|
+
|
40
|
+
static int mark_methods(st_data_t key, st_data_t value, st_data_t result)
|
41
|
+
{
|
42
|
+
prof_method_t* method = (prof_method_t*)value;
|
43
|
+
prof_method_mark(method);
|
44
|
+
return ST_CONTINUE;
|
45
|
+
}
|
46
|
+
|
47
|
+
size_t prof_thread_size(const void* data)
|
48
|
+
{
|
49
|
+
return sizeof(thread_data_t);
|
50
|
+
}
|
51
|
+
|
52
|
+
void prof_thread_mark(void* data)
|
53
|
+
{
|
54
|
+
if (!data)
|
55
|
+
return;
|
56
|
+
|
57
|
+
thread_data_t* thread = (thread_data_t*)data;
|
58
|
+
|
59
|
+
if (thread->object != Qnil)
|
60
|
+
rb_gc_mark(thread->object);
|
61
|
+
|
62
|
+
rb_gc_mark(thread->fiber);
|
63
|
+
|
64
|
+
if (thread->methods != Qnil)
|
65
|
+
rb_gc_mark(thread->methods);
|
66
|
+
|
67
|
+
if (thread->fiber_id != Qnil)
|
68
|
+
rb_gc_mark(thread->fiber_id);
|
69
|
+
|
70
|
+
if (thread->thread_id != Qnil)
|
71
|
+
rb_gc_mark(thread->thread_id);
|
72
|
+
|
73
|
+
if (thread->call_tree)
|
74
|
+
prof_call_tree_mark(thread->call_tree);
|
75
|
+
|
76
|
+
rb_st_foreach(thread->method_table, mark_methods, 0);
|
77
|
+
}
|
78
|
+
|
79
|
+
void prof_thread_ruby_gc_free(void* data)
|
80
|
+
{
|
81
|
+
if (data)
|
82
|
+
{
|
83
|
+
thread_data_t* thread_data = (thread_data_t*)data;
|
84
|
+
thread_data->object = Qnil;
|
85
|
+
}
|
86
|
+
}
|
87
|
+
|
88
|
+
static void prof_thread_free(thread_data_t* thread_data)
|
89
|
+
{
|
90
|
+
/* Has this method object been accessed by Ruby? If
|
91
|
+
yes then set its data to nil to avoid a segmentation fault on the next mark and sweep. */
|
92
|
+
if (thread_data->object != Qnil)
|
93
|
+
{
|
94
|
+
RTYPEDDATA(thread_data->object)->data = NULL;
|
95
|
+
thread_data->object = Qnil;
|
96
|
+
}
|
97
|
+
|
98
|
+
method_table_free(thread_data->method_table);
|
99
|
+
|
100
|
+
if (thread_data->call_tree)
|
101
|
+
prof_call_tree_free(thread_data->call_tree);
|
102
|
+
|
103
|
+
prof_stack_free(thread_data->stack);
|
104
|
+
|
105
|
+
xfree(thread_data);
|
106
|
+
}
|
107
|
+
|
108
|
+
static const rb_data_type_t thread_type =
|
109
|
+
{
|
110
|
+
.wrap_struct_name = "ThreadInfo",
|
111
|
+
.function =
|
112
|
+
{
|
113
|
+
.dmark = prof_thread_mark,
|
114
|
+
.dfree = prof_thread_ruby_gc_free,
|
115
|
+
.dsize = prof_thread_size,
|
116
|
+
},
|
117
|
+
.data = NULL,
|
118
|
+
.flags = RUBY_TYPED_FREE_IMMEDIATELY
|
119
|
+
};
|
120
|
+
|
121
|
+
VALUE prof_thread_wrap(thread_data_t* thread)
|
122
|
+
{
|
123
|
+
if (thread->object == Qnil)
|
124
|
+
{
|
125
|
+
thread->object = TypedData_Wrap_Struct(cRpThread, &thread_type, thread);
|
126
|
+
}
|
127
|
+
return thread->object;
|
128
|
+
}
|
129
|
+
|
130
|
+
static VALUE prof_thread_allocate(VALUE klass)
|
131
|
+
{
|
132
|
+
thread_data_t* thread_data = thread_data_create();
|
133
|
+
thread_data->object = prof_thread_wrap(thread_data);
|
134
|
+
return thread_data->object;
|
135
|
+
}
|
136
|
+
|
137
|
+
thread_data_t* prof_get_thread(VALUE self)
|
138
|
+
{
|
139
|
+
/* Can't use Data_Get_Struct because that triggers the event hook
|
140
|
+
ending up in endless recursion. */
|
141
|
+
thread_data_t* result = RTYPEDDATA_DATA(self);
|
142
|
+
if (!result)
|
143
|
+
rb_raise(rb_eRuntimeError, "This RubyProf::Thread instance has already been freed, likely because its profile has been freed.");
|
144
|
+
|
145
|
+
return result;
|
146
|
+
}
|
147
|
+
|
148
|
+
// ====== Thread Table ======
|
149
|
+
// The thread table is hash keyed on ruby fiber_id that stores instances of thread_data_t.
|
150
|
+
|
151
|
+
st_table* threads_table_create()
|
152
|
+
{
|
153
|
+
return rb_st_init_numtable();
|
154
|
+
}
|
155
|
+
|
156
|
+
static int thread_table_free_iterator(st_data_t key, st_data_t value, st_data_t dummy)
|
157
|
+
{
|
158
|
+
prof_thread_free((thread_data_t*)value);
|
159
|
+
return ST_CONTINUE;
|
160
|
+
}
|
161
|
+
|
162
|
+
void threads_table_free(st_table* table)
|
163
|
+
{
|
164
|
+
rb_st_foreach(table, thread_table_free_iterator, 0);
|
165
|
+
rb_st_free_table(table);
|
166
|
+
}
|
167
|
+
|
168
|
+
thread_data_t* threads_table_lookup(void* prof, VALUE fiber)
|
169
|
+
{
|
170
|
+
prof_profile_t* profile = prof;
|
171
|
+
thread_data_t* result = NULL;
|
172
|
+
st_data_t val;
|
173
|
+
|
174
|
+
if (rb_st_lookup(profile->threads_tbl, fiber, &val))
|
175
|
+
{
|
176
|
+
result = (thread_data_t*)val;
|
177
|
+
}
|
178
|
+
|
179
|
+
return result;
|
180
|
+
}
|
181
|
+
|
182
|
+
thread_data_t* threads_table_insert(void* prof, VALUE fiber)
|
183
|
+
{
|
184
|
+
prof_profile_t* profile = prof;
|
185
|
+
thread_data_t* result = thread_data_create();
|
186
|
+
VALUE thread = rb_thread_current();
|
187
|
+
|
188
|
+
result->fiber = fiber;
|
189
|
+
result->fiber_id = rb_obj_id(fiber);
|
190
|
+
result->thread_id = rb_obj_id(thread);
|
191
|
+
rb_st_insert(profile->threads_tbl, (st_data_t)fiber, (st_data_t)result);
|
192
|
+
|
193
|
+
// Are we tracing this thread?
|
194
|
+
if (profile->include_threads_tbl && !rb_st_lookup(profile->include_threads_tbl, thread, 0))
|
195
|
+
{
|
196
|
+
result->trace = false;
|
197
|
+
}
|
198
|
+
else if (profile->exclude_threads_tbl && rb_st_lookup(profile->exclude_threads_tbl, thread, 0))
|
199
|
+
{
|
200
|
+
result->trace = false;
|
201
|
+
}
|
202
|
+
else
|
203
|
+
{
|
204
|
+
result->trace = true;
|
205
|
+
}
|
206
|
+
|
207
|
+
return result;
|
208
|
+
}
|
209
|
+
|
210
|
+
// ====== Profiling Methods ======
|
211
|
+
void switch_thread(void* prof, thread_data_t* thread_data, double measurement)
|
212
|
+
{
|
213
|
+
prof_profile_t* profile = prof;
|
214
|
+
|
215
|
+
/* Get current frame for this thread */
|
216
|
+
prof_frame_t* frame = prof_frame_current(thread_data->stack);
|
217
|
+
if (frame)
|
218
|
+
{
|
219
|
+
frame->wait_time += measurement - frame->switch_time;
|
220
|
+
frame->switch_time = 0;
|
221
|
+
}
|
222
|
+
|
223
|
+
/* Save on the last thread the time of the context switch
|
224
|
+
and reset this thread's last context switch to 0.*/
|
225
|
+
if (profile->last_thread_data)
|
226
|
+
{
|
227
|
+
prof_frame_t* last_frame = prof_frame_current(profile->last_thread_data->stack);
|
228
|
+
if (last_frame)
|
229
|
+
last_frame->switch_time = measurement;
|
230
|
+
}
|
231
|
+
|
232
|
+
profile->last_thread_data = thread_data;
|
233
|
+
}
|
234
|
+
|
235
|
+
int pause_thread(st_data_t key, st_data_t value, st_data_t data)
|
236
|
+
{
|
237
|
+
thread_data_t* thread_data = (thread_data_t*)value;
|
238
|
+
prof_profile_t* profile = (prof_profile_t*)data;
|
239
|
+
|
240
|
+
prof_frame_t* frame = prof_frame_current(thread_data->stack);
|
241
|
+
prof_frame_pause(frame, profile->measurement_at_pause_resume);
|
242
|
+
|
243
|
+
return ST_CONTINUE;
|
244
|
+
}
|
245
|
+
|
246
|
+
int unpause_thread(st_data_t key, st_data_t value, st_data_t data)
|
247
|
+
{
|
248
|
+
thread_data_t* thread_data = (thread_data_t*)value;
|
249
|
+
prof_profile_t* profile = (prof_profile_t*)data;
|
250
|
+
|
251
|
+
prof_frame_t* frame = prof_frame_current(thread_data->stack);
|
252
|
+
prof_frame_unpause(frame, profile->measurement_at_pause_resume);
|
253
|
+
|
254
|
+
return ST_CONTINUE;
|
255
|
+
}
|
256
|
+
|
257
|
+
// ====== Helper Methods ======
|
258
|
+
static int collect_methods(st_data_t key, st_data_t value, st_data_t result)
|
259
|
+
{
|
260
|
+
/* Called for each method stored in a thread's method table.
|
261
|
+
We want to store the method info information into an array.*/
|
262
|
+
VALUE methods = (VALUE)result;
|
263
|
+
prof_method_t* method = (prof_method_t*)value;
|
264
|
+
rb_ary_push(methods, prof_method_wrap(method));
|
265
|
+
|
266
|
+
return ST_CONTINUE;
|
267
|
+
}
|
268
|
+
|
269
|
+
// ====== RubyProf::Thread ======
|
270
|
+
/* call-seq:
|
271
|
+
id -> number
|
272
|
+
|
273
|
+
Returns the thread id of this thread. */
|
274
|
+
static VALUE prof_thread_id(VALUE self)
|
275
|
+
{
|
276
|
+
thread_data_t* thread = prof_get_thread(self);
|
277
|
+
return thread->thread_id;
|
278
|
+
}
|
279
|
+
|
280
|
+
/* call-seq:
|
281
|
+
fiber_id -> number
|
282
|
+
|
283
|
+
Returns the fiber id of this thread. */
|
284
|
+
static VALUE prof_fiber_id(VALUE self)
|
285
|
+
{
|
286
|
+
thread_data_t* thread = prof_get_thread(self);
|
287
|
+
return thread->fiber_id;
|
288
|
+
}
|
289
|
+
|
290
|
+
/* call-seq:
|
291
|
+
call_tree -> CallTree
|
292
|
+
|
293
|
+
Returns the root of the call tree. */
|
294
|
+
static VALUE prof_call_tree(VALUE self)
|
295
|
+
{
|
296
|
+
thread_data_t* thread = prof_get_thread(self);
|
297
|
+
return prof_call_tree_wrap(thread->call_tree);
|
298
|
+
}
|
299
|
+
|
300
|
+
/* call-seq:
|
301
|
+
methods -> [RubyProf::MethodInfo]
|
302
|
+
|
303
|
+
Returns an array of methods that were called from this
|
304
|
+
thread during program execution. */
|
305
|
+
static VALUE prof_thread_methods(VALUE self)
|
306
|
+
{
|
307
|
+
thread_data_t* thread = prof_get_thread(self);
|
308
|
+
if (thread->methods == Qnil)
|
309
|
+
{
|
310
|
+
thread->methods = rb_ary_new();
|
311
|
+
rb_st_foreach(thread->method_table, collect_methods, thread->methods);
|
312
|
+
}
|
313
|
+
return thread->methods;
|
314
|
+
}
|
315
|
+
|
316
|
+
/* :nodoc: */
|
317
|
+
static VALUE prof_thread_dump(VALUE self)
|
318
|
+
{
|
319
|
+
thread_data_t* thread_data = RTYPEDDATA_DATA(self);
|
320
|
+
|
321
|
+
VALUE result = rb_hash_new();
|
322
|
+
rb_hash_aset(result, ID2SYM(rb_intern("fiber_id")), thread_data->fiber_id);
|
323
|
+
rb_hash_aset(result, ID2SYM(rb_intern("methods")), prof_thread_methods(self));
|
324
|
+
rb_hash_aset(result, ID2SYM(rb_intern("call_tree")), prof_call_tree(self));
|
325
|
+
|
326
|
+
return result;
|
327
|
+
}
|
328
|
+
|
329
|
+
/* :nodoc: */
|
330
|
+
static VALUE prof_thread_load(VALUE self, VALUE data)
|
331
|
+
{
|
332
|
+
thread_data_t* thread_data = RTYPEDDATA_DATA(self);
|
333
|
+
|
334
|
+
VALUE call_tree = rb_hash_aref(data, ID2SYM(rb_intern("call_tree")));
|
335
|
+
thread_data->call_tree = prof_get_call_tree(call_tree);
|
336
|
+
|
337
|
+
thread_data->fiber_id = rb_hash_aref(data, ID2SYM(rb_intern("fiber_id")));
|
338
|
+
|
339
|
+
VALUE methods = rb_hash_aref(data, ID2SYM(rb_intern("methods")));
|
340
|
+
for (int i = 0; i < rb_array_len(methods); i++)
|
341
|
+
{
|
342
|
+
VALUE method = rb_ary_entry(methods, i);
|
343
|
+
prof_method_t* method_data = RTYPEDDATA_DATA(method);
|
344
|
+
method_table_insert(thread_data->method_table, method_data->key, method_data);
|
345
|
+
}
|
346
|
+
|
347
|
+
return data;
|
348
|
+
}
|
349
|
+
|
350
|
+
void rp_init_thread(void)
|
351
|
+
{
|
352
|
+
cRpThread = rb_define_class_under(mProf, "Thread", rb_cObject);
|
353
|
+
rb_undef_method(CLASS_OF(cRpThread), "new");
|
354
|
+
rb_define_alloc_func(cRpThread, prof_thread_allocate);
|
355
|
+
|
356
|
+
rb_define_method(cRpThread, "id", prof_thread_id, 0);
|
357
|
+
rb_define_method(cRpThread, "call_tree", prof_call_tree, 0);
|
358
|
+
rb_define_method(cRpThread, "fiber_id", prof_fiber_id, 0);
|
359
|
+
rb_define_method(cRpThread, "methods", prof_thread_methods, 0);
|
360
|
+
rb_define_method(cRpThread, "_dump_data", prof_thread_dump, 0);
|
361
|
+
rb_define_method(cRpThread, "_load_data", prof_thread_load, 1);
|
362
|
+
}
|