ruby-prof 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 (99) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGES +532 -0
  3. data/LICENSE +25 -0
  4. data/README.rdoc +5 -0
  5. data/Rakefile +110 -0
  6. data/bin/ruby-prof +380 -0
  7. data/bin/ruby-prof-check-trace +45 -0
  8. data/ext/ruby_prof/extconf.rb +36 -0
  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 -0
  12. data/ext/ruby_prof/rp_call_info.h +35 -0
  13. data/ext/ruby_prof/rp_measure_allocations.c +52 -0
  14. data/ext/ruby_prof/rp_measure_memory.c +42 -0
  15. data/ext/ruby_prof/rp_measure_process_time.c +67 -0
  16. data/ext/ruby_prof/rp_measure_wall_time.c +62 -0
  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 -0
  20. data/ext/ruby_prof/rp_method.h +70 -0
  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 -0
  24. data/ext/ruby_prof/rp_stack.h +56 -0
  25. data/ext/ruby_prof/rp_thread.c +337 -0
  26. data/ext/ruby_prof/rp_thread.h +36 -0
  27. data/ext/ruby_prof/ruby_prof.c +48 -0
  28. data/ext/ruby_prof/ruby_prof.h +17 -0
  29. data/ext/ruby_prof/vc/ruby_prof.sln +31 -0
  30. data/ext/ruby_prof/vc/ruby_prof.vcxproj +143 -0
  31. data/lib/ruby-prof.rb +52 -0
  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 -0
  36. data/lib/ruby-prof/call_info_visitor.rb +38 -0
  37. data/lib/ruby-prof/compatibility.rb +109 -0
  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 -0
  41. data/lib/ruby-prof/printers/abstract_printer.rb +127 -0
  42. data/lib/ruby-prof/printers/call_info_printer.rb +51 -0
  43. data/lib/ruby-prof/printers/call_stack_printer.rb +182 -0
  44. data/lib/ruby-prof/printers/call_tree_printer.rb +151 -0
  45. data/lib/ruby-prof/printers/dot_printer.rb +132 -0
  46. data/lib/ruby-prof/printers/flat_printer.rb +52 -0
  47. data/lib/ruby-prof/printers/graph_html_printer.rb +63 -0
  48. data/lib/ruby-prof/printers/graph_printer.rb +114 -0
  49. data/lib/ruby-prof/printers/multi_printer.rb +127 -0
  50. data/lib/ruby-prof/profile.rb +33 -0
  51. data/lib/ruby-prof/rack.rb +171 -0
  52. data/lib/ruby-prof/task.rb +147 -0
  53. data/lib/ruby-prof/thread.rb +35 -0
  54. data/lib/ruby-prof/version.rb +3 -0
  55. data/lib/unprof.rb +10 -0
  56. data/ruby-prof.gemspec +58 -0
  57. data/test/abstract_printer_test.rb +26 -0
  58. data/test/alias_test.rb +129 -0
  59. data/test/basic_test.rb +129 -0
  60. data/test/call_info_visitor_test.rb +31 -0
  61. data/test/duplicate_names_test.rb +32 -0
  62. data/test/dynamic_method_test.rb +53 -0
  63. data/test/enumerable_test.rb +21 -0
  64. data/test/exceptions_test.rb +24 -0
  65. data/test/exclude_methods_test.rb +146 -0
  66. data/test/exclude_threads_test.rb +53 -0
  67. data/test/fiber_test.rb +73 -0
  68. data/test/gc_test.rb +96 -0
  69. data/test/line_number_test.rb +161 -0
  70. data/test/marshal_test.rb +119 -0
  71. data/test/measure_allocations.rb +30 -0
  72. data/test/measure_allocations_test.rb +385 -0
  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 -0
  76. data/test/measure_times.rb +54 -0
  77. data/test/measure_wall_time_test.rb +459 -0
  78. data/test/multi_printer_test.rb +71 -0
  79. data/test/no_method_class_test.rb +15 -0
  80. data/test/parser_timings.rb +24 -0
  81. data/test/pause_resume_test.rb +166 -0
  82. data/test/prime.rb +56 -0
  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 -0
  89. data/test/printing_recursive_graph_test.rb +81 -0
  90. data/test/rack_test.rb +157 -0
  91. data/test/recursive_test.rb +210 -0
  92. data/test/singleton_test.rb +38 -0
  93. data/test/stack_printer_test.rb +64 -0
  94. data/test/start_stop_test.rb +109 -0
  95. data/test/test_helper.rb +24 -0
  96. data/test/thread_test.rb +144 -0
  97. data/test/unique_call_path_test.rb +190 -0
  98. data/test/yarv_test.rb +56 -0
  99. metadata +191 -0
@@ -0,0 +1,37 @@
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_PROFILE_H__
5
+ #define __RP_PROFILE_H__
6
+
7
+ #include "ruby_prof.h"
8
+ #include "rp_measurement.h"
9
+ #include "rp_thread.h"
10
+
11
+ extern VALUE cProfile;
12
+
13
+ typedef struct
14
+ {
15
+ VALUE running;
16
+ VALUE paused;
17
+
18
+ prof_measurer_t* measurer;
19
+ VALUE threads;
20
+
21
+ VALUE tracepoints;
22
+
23
+ st_table* threads_tbl;
24
+ st_table* exclude_threads_tbl;
25
+ st_table* include_threads_tbl;
26
+ st_table* exclude_methods_tbl;
27
+ thread_data_t* last_thread_data;
28
+ double measurement_at_pause_resume;
29
+ bool allow_exceptions;
30
+ bool merge_fibers;
31
+ } prof_profile_t;
32
+
33
+ void rp_init_profile(void);
34
+ prof_profile_t* prof_get_profile(VALUE self);
35
+
36
+
37
+ #endif //__RP_PROFILE_H__
@@ -0,0 +1,196 @@
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_stack.h"
5
+
6
+ #define INITIAL_STACK_SIZE 32
7
+
8
+ void
9
+ prof_frame_pause(prof_frame_t *frame, double current_measurement)
10
+ {
11
+ if (frame && prof_frame_is_unpaused(frame))
12
+ frame->pause_time = current_measurement;
13
+ }
14
+
15
+ void
16
+ prof_frame_unpause(prof_frame_t *frame, double current_measurement)
17
+ {
18
+ if (frame && prof_frame_is_paused(frame))
19
+ {
20
+ frame->dead_time += (current_measurement - frame->pause_time);
21
+ frame->pause_time = -1;
22
+ }
23
+ }
24
+
25
+ /* Creates a stack of prof_frame_t to keep track
26
+ of timings for active methods. */
27
+ prof_stack_t *
28
+ prof_stack_create()
29
+ {
30
+ prof_stack_t *stack = ALLOC(prof_stack_t);
31
+ stack->start = ZALLOC_N(prof_frame_t, INITIAL_STACK_SIZE);
32
+ stack->ptr = stack->start;
33
+ stack->end = stack->start + INITIAL_STACK_SIZE;
34
+
35
+ return stack;
36
+ }
37
+
38
+ void
39
+ prof_stack_free(prof_stack_t *stack)
40
+ {
41
+ xfree(stack->start);
42
+ xfree(stack);
43
+ }
44
+
45
+ prof_frame_t *
46
+ prof_stack_push(prof_stack_t *stack, prof_call_info_t *call_info, double measurement, int paused)
47
+ {
48
+ prof_frame_t *result;
49
+ prof_frame_t* parent_frame;
50
+
51
+ /* Is there space on the stack? If not, double
52
+ its size. */
53
+ if (stack->ptr == stack->end - 1)
54
+ {
55
+ size_t len = stack->ptr - stack->start;
56
+ size_t new_capacity = (stack->end - stack->start) * 2;
57
+ REALLOC_N(stack->start, prof_frame_t, new_capacity);
58
+
59
+ /* Memory just got moved, reset pointers */
60
+ stack->ptr = stack->start + len;
61
+ stack->end = stack->start + new_capacity;
62
+ }
63
+
64
+ parent_frame = stack->ptr;
65
+ stack->ptr++;
66
+
67
+ result = stack->ptr;
68
+ result->call_info = call_info;
69
+ result->call_info->depth = (int)(stack->ptr - stack->start); // shortening of 64 bit into 32;
70
+ result->passes = 0;
71
+
72
+ result->start_time = measurement;
73
+ result->pause_time = -1; // init as not paused.
74
+ result->switch_time = 0;
75
+ result->wait_time = 0;
76
+ result->child_time = 0;
77
+ result->dead_time = 0;
78
+ result->source_file = Qnil;
79
+ result->source_line = 0;
80
+
81
+ call_info->measurement->called++;
82
+ call_info->visits++;
83
+
84
+ if (call_info->method->visits > 0)
85
+ {
86
+ call_info->method->recursive = true;
87
+ }
88
+ call_info->method->measurement->called++;
89
+ call_info->method->visits++;
90
+
91
+ // Unpause the parent frame, if it exists.
92
+ // If currently paused then:
93
+ // 1) The child frame will begin paused.
94
+ // 2) The parent will inherit the child's dead time.
95
+ prof_frame_unpause(parent_frame, measurement);
96
+
97
+ if (paused)
98
+ {
99
+ prof_frame_pause(result, measurement);
100
+ }
101
+
102
+ // Return the result
103
+ return result;
104
+ }
105
+
106
+ prof_frame_t *
107
+ prof_stack_pop(prof_stack_t *stack, double measurement)
108
+ {
109
+ prof_frame_t *frame;
110
+ prof_frame_t *parent_frame;
111
+ prof_call_info_t *call_info;
112
+
113
+ double total_time;
114
+ double self_time;
115
+
116
+ if (stack->ptr == stack->start)
117
+ return NULL;
118
+
119
+ frame = stack->ptr;
120
+
121
+ /* Match passes until we reach the frame itself. */
122
+ if (prof_frame_is_pass(frame))
123
+ {
124
+ frame->passes--;
125
+ /* Additional frames can be consumed. See pop_frames(). */
126
+ return frame;
127
+ }
128
+
129
+ /* Consume this frame. */
130
+ stack->ptr--;
131
+
132
+ parent_frame = stack->ptr;
133
+
134
+ /* Calculate the total time this method took */
135
+ prof_frame_unpause(frame, measurement);
136
+
137
+ total_time = measurement - frame->start_time - frame->dead_time;
138
+ self_time = total_time - frame->child_time - frame->wait_time;
139
+
140
+ /* Update information about the current method */
141
+ call_info = frame->call_info;
142
+
143
+ // Update method measurement
144
+ call_info->method->measurement->self_time += self_time;
145
+ call_info->method->measurement->wait_time += frame->wait_time;
146
+ if (call_info->method->visits == 1)
147
+ call_info->method->measurement->total_time += total_time;
148
+
149
+ call_info->method->visits--;
150
+
151
+ // Update method measurement
152
+ call_info->measurement->self_time += self_time;
153
+ call_info->measurement->wait_time += frame->wait_time;
154
+ if (call_info->visits == 1)
155
+ call_info->measurement->total_time += total_time;
156
+
157
+ call_info->visits--;
158
+
159
+ if (parent_frame)
160
+ {
161
+ parent_frame->child_time += total_time;
162
+ parent_frame->dead_time += frame->dead_time;
163
+ }
164
+
165
+ return frame;
166
+ }
167
+
168
+ prof_frame_t *
169
+ prof_stack_pass(prof_stack_t *stack)
170
+ {
171
+ prof_frame_t* frame = stack->ptr;
172
+ if (frame)
173
+ {
174
+ frame->passes++;
175
+ }
176
+ return frame;
177
+ }
178
+
179
+ prof_method_t*
180
+ prof_find_method(prof_stack_t* stack, VALUE source_file, int source_line)
181
+ {
182
+ prof_frame_t* frame = stack->ptr;
183
+ while (frame >= stack->start)
184
+ {
185
+ if (!frame->call_info)
186
+ return NULL;
187
+
188
+ if (rb_str_equal(source_file, frame->call_info->method->source_file) &&
189
+ source_line >= frame->call_info->method->source_line)
190
+ {
191
+ return frame->call_info->method;
192
+ }
193
+ frame--;
194
+ }
195
+ return NULL;
196
+ }
@@ -0,0 +1,56 @@
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_STACK__
5
+ #define __RP_STACK__
6
+
7
+ #include "ruby_prof.h"
8
+ #include "rp_call_info.h"
9
+
10
+ /* Temporary object that maintains profiling information
11
+ for active methods. They are created and destroyed
12
+ as the program moves up and down its stack. */
13
+ typedef struct
14
+ {
15
+ /* Caching prof_method_t values significantly
16
+ increases performance. */
17
+ prof_call_info_t *call_info;
18
+
19
+ VALUE source_file;
20
+ unsigned int source_line;
21
+ unsigned int passes; /* Count of "pass" frames, _after_ this one. */
22
+
23
+ double start_time;
24
+ double switch_time; /* Time at switch to different thread */
25
+ double wait_time;
26
+ double child_time;
27
+ double pause_time; // Time pause() was initiated
28
+ double dead_time; // Time to ignore (i.e. total amount of time between pause/resume blocks)
29
+ } prof_frame_t;
30
+
31
+ #define prof_frame_is_real(f) ((f)->passes == 0)
32
+ #define prof_frame_is_pass(f) ((f)->passes > 0)
33
+
34
+ #define prof_frame_is_paused(f) (f->pause_time >= 0)
35
+ #define prof_frame_is_unpaused(f) (f->pause_time < 0)
36
+
37
+ void prof_frame_pause(prof_frame_t*, double current_measurement);
38
+ void prof_frame_unpause(prof_frame_t*, double current_measurement);
39
+
40
+ /* Current stack of active methods.*/
41
+ typedef struct
42
+ {
43
+ prof_frame_t *start;
44
+ prof_frame_t *end;
45
+ prof_frame_t *ptr;
46
+ } prof_stack_t;
47
+
48
+ prof_stack_t *prof_stack_create(void);
49
+ void prof_stack_free(prof_stack_t *stack);
50
+
51
+ prof_frame_t *prof_stack_push(prof_stack_t *stack, prof_call_info_t *call_info, double measurement, int paused);
52
+ prof_frame_t *prof_stack_pop(prof_stack_t *stack, double measurement);
53
+ prof_frame_t *prof_stack_pass(prof_stack_t *stack);
54
+ prof_method_t *prof_find_method(prof_stack_t* stack, VALUE source_file, int source_line);
55
+
56
+ #endif //__RP_STACK__
@@ -0,0 +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
+ /* 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
+ }