ruby-prof 1.7.1 → 1.7.2

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 (39) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGES +8 -0
  3. data/ext/ruby_prof/extconf.rb +23 -22
  4. data/ext/ruby_prof/rp_call_trees.c +296 -296
  5. data/ext/ruby_prof/rp_call_trees.h +28 -28
  6. data/ext/ruby_prof/rp_measure_allocations.c +47 -47
  7. data/ext/ruby_prof/rp_measure_process_time.c +64 -66
  8. data/ext/ruby_prof/rp_measure_wall_time.c +52 -64
  9. data/ext/ruby_prof/rp_method.c +551 -551
  10. data/ext/ruby_prof/rp_stack.c +212 -212
  11. data/ext/ruby_prof/ruby_prof.c +50 -50
  12. data/ext/ruby_prof/ruby_prof.h +3 -2
  13. data/ext/ruby_prof/vc/ruby_prof.vcxproj +3 -3
  14. data/lib/ruby-prof/compatibility.rb +113 -113
  15. data/lib/ruby-prof/exclude_common_methods.rb +204 -204
  16. data/lib/ruby-prof/printers/abstract_printer.rb +156 -138
  17. data/lib/ruby-prof/version.rb +3 -3
  18. data/ruby-prof.gemspec +66 -65
  19. data/test/dynamic_method_test.rb +9 -21
  20. data/test/enumerable_test.rb +23 -21
  21. data/test/exclude_methods_test.rb +363 -257
  22. data/test/fiber_test.rb +195 -195
  23. data/test/gc_test.rb +104 -102
  24. data/test/line_number_test.rb +426 -289
  25. data/test/measure_allocations_test.rb +1172 -1081
  26. data/test/measure_memory_test.rb +1193 -1456
  27. data/test/measure_process_time_test.rb +3330 -2477
  28. data/test/measure_wall_time_test.rb +634 -568
  29. data/test/merge_test.rb +146 -146
  30. data/test/method_info_test.rb +100 -95
  31. data/test/printers_test.rb +178 -135
  32. data/test/recursive_test.rb +796 -622
  33. data/test/start_stop_test.rb +4 -4
  34. data/test/test_helper.rb +20 -20
  35. data/test/thread_test.rb +229 -231
  36. data/test/unique_call_path_test.rb +9 -22
  37. data/test/yarv_test.rb +1 -5
  38. metadata +19 -9
  39. data/test/crash2.rb +0 -144
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0fbaeec67f3e4771202c58f1902376076dc8e9185e0923e0bad648f6fdaae8a5
4
- data.tar.gz: be5b6c0740418aae93dc7cbe90d13d992b71ffe822cf7332075c1930f3cb6501
3
+ metadata.gz: 5755a16ded5472a0dd58f411dd22a1da4213136a8050eef0c9f392f558a86970
4
+ data.tar.gz: d8b67fcd1b208fe448864b24b7c76630bce578eda1f4588ef11e0644987d9632
5
5
  SHA512:
6
- metadata.gz: 40403f7f034a87120c6a7328057ff9ad2e922f0c1c8aa080720c6f70c8ae84a08bc71bfaecfe040f38f5ebdfb38497326c84136fe2e5ce6f7e4fd775f368739f
7
- data.tar.gz: e5e9def95a8b0240ce330f1d093f549ea9555cfb491d45ba06647c3112dd55229244fb8d42ec871b81cad0b72bb1c9a5e49df1db0ff8008060a197d381acbe8e
6
+ metadata.gz: d1d8167a024ac0ec233d0ad2e1638bde210c4279caf28c8d9c091c371e23eb5d4c6aa54bb9aecb60a3ae04611a03e4c3fb504140e4d0943e3b34467cebf75af4
7
+ data.tar.gz: 75e8cd9be4f9cb5bb7c82ef8460b2f0c7f807325d9e65df4389063fce416991908aaf09cf58d6295d8603f5231a1af4780ea099159cb486daa0e6edcae28c984
data/CHANGES CHANGED
@@ -1,3 +1,11 @@
1
+ 1.7.2 (2025-05-21)
2
+ =====================
3
+ * Fix compiling on Ubuntu and Arch Linux
4
+ * Update tests for Ruby 3.4
5
+ * Remove tests for Ruby 3.1
6
+ * Update MacOS to use clock_gettime (same as Linux) instead of proprietary mach_absolute_time API
7
+ * Add CMake support
8
+
1
9
  1.7.1 (2024-10-19)
2
10
  =====================
3
11
  * Fix crashes when calling merge due to inconsistent method keys (Chris Heald)
@@ -1,22 +1,23 @@
1
- require "mkmf"
2
-
3
- # Let's go with a modern version of C! want to intermix declarations and code (ie, don't define
4
- # all variables at the top of the method). If using Visual Studio, you'll need 2019 version
5
- # 16.8 or higher
6
- if RUBY_PLATFORM =~ /mswin/
7
- $CFLAGS += ' /std:c11'
8
- else
9
- $CFLAGS += ' -std=c11'
10
- end
11
-
12
- # For gcc add -s to strip symbols, reducing library size from 17MB to 78KB (at least on Windows with mingw64)
13
- if RUBY_PLATFORM !~ /mswin/
14
- $LDFLAGS += ' -s'
15
- end
16
-
17
- # And since we are using C99 we want to disable Ruby sending these warnings to gcc
18
- if CONFIG['warnflags']
19
- CONFIG['warnflags'].gsub!('-Wdeclaration-after-statement', '')
20
- end
21
-
22
- create_makefile("ruby_prof")
1
+ require "mkmf"
2
+
3
+ # Let's go with a modern version of C! want to intermix declarations and code (ie, don't define
4
+ # all variables at the top of the method). If using Visual Studio, you'll need 2019 version
5
+ # 16.8 or higher
6
+ if RUBY_PLATFORM =~ /mswin/
7
+ $CFLAGS += ' /std:c11'
8
+ else
9
+ # Need -D_POSIX_C_SOURCE=199309L for clock_gettime
10
+ $CFLAGS += ' -std=c11 -D_POSIX_C_SOURCE=199309L'
11
+ end
12
+
13
+ # For gcc add -s to strip symbols, reducing library size from 17MB to 78KB (at least on Windows with mingw64)
14
+ if RUBY_PLATFORM !~ /mswin/
15
+ $LDFLAGS += ' -s'
16
+ end
17
+
18
+ # And since we are using C99 we want to disable Ruby sending these warnings to gcc
19
+ if CONFIG['warnflags']
20
+ CONFIG['warnflags'].gsub!('-Wdeclaration-after-statement', '')
21
+ end
22
+
23
+ create_makefile("ruby_prof")
@@ -1,296 +1,296 @@
1
- /* Copyright (C) 2005-2013 Shugo Maeda <shugo@ruby-lang.org> and Charlie Savage <cfis@savagexi.com>
2
- Please see the LICENSE file for copyright and distribution information */
3
-
4
- #include "rp_call_trees.h"
5
- #include "rp_measurement.h"
6
-
7
- #define INITIAL_CALL_TREES_SIZE 2
8
-
9
- VALUE cRpCallTrees;
10
-
11
- /* ======= Call Infos ========*/
12
- prof_call_trees_t* prof_get_call_trees(VALUE self)
13
- {
14
- /* Can't use Data_Get_Struct because that triggers the event hook
15
- ending up in endless recursion. */
16
- prof_call_trees_t* result = RTYPEDDATA_DATA(self);
17
-
18
- if (!result)
19
- rb_raise(rb_eRuntimeError, "This RubyProf::CallTrees instance has already been freed, likely because its profile has been freed.");
20
-
21
- return result;
22
- }
23
-
24
- prof_call_trees_t* prof_call_trees_create(void)
25
- {
26
- prof_call_trees_t* result = ALLOC(prof_call_trees_t);
27
- result->start = ALLOC_N(prof_call_tree_t*, INITIAL_CALL_TREES_SIZE);
28
- result->end = result->start + INITIAL_CALL_TREES_SIZE;
29
- result->ptr = result->start;
30
- result->object = Qnil;
31
- return result;
32
- }
33
-
34
- void prof_call_trees_mark(void* data)
35
- {
36
- if (!data) return;
37
-
38
- prof_call_trees_t* call_trees = (prof_call_trees_t*)data;
39
- prof_call_tree_t** call_tree;
40
- for (call_tree = call_trees->start; call_tree < call_trees->ptr; call_tree++)
41
- {
42
- prof_call_tree_mark(*call_tree);
43
- }
44
- }
45
-
46
- void prof_call_trees_free(prof_call_trees_t* call_trees)
47
- {
48
- /* Has this method object been accessed by Ruby? If
49
- yes clean it up so to avoid a segmentation fault. */
50
- if (call_trees->object != Qnil)
51
- {
52
- RTYPEDDATA(call_trees->object)->data = NULL;
53
- call_trees->object = Qnil;
54
- }
55
-
56
- // Note we do not free our call_tree structures - since they have no parents they will free themselves
57
- xfree(call_trees);
58
- }
59
-
60
- void prof_call_trees_ruby_gc_free(void* data)
61
- {
62
- if (data)
63
- {
64
- // This object gets freed by its owning method
65
- prof_call_trees_t* call_trees = (prof_call_trees_t*)data;
66
- call_trees->object = Qnil;
67
- }
68
- }
69
-
70
- static int prof_call_trees_collect(st_data_t key, st_data_t value, st_data_t data)
71
- {
72
- VALUE result = (VALUE)data;
73
- prof_call_tree_t* call_tree_data = (prof_call_tree_t*)value;
74
- VALUE aggregate_call_tree = prof_call_tree_wrap(call_tree_data);
75
- rb_ary_push(result, aggregate_call_tree);
76
-
77
- return ST_CONTINUE;
78
- }
79
-
80
- static int prof_call_trees_collect_callees(st_data_t key, st_data_t value, st_data_t hash)
81
- {
82
- st_table* callers = (st_table*)hash;
83
- prof_call_tree_t* call_tree_data = (prof_call_tree_t*)value;
84
-
85
- prof_call_tree_t* aggregate_call_tree_data = NULL;
86
-
87
- if (rb_st_lookup(callers, call_tree_data->method->key, (st_data_t*)&aggregate_call_tree_data))
88
- {
89
- prof_measurement_merge_internal(aggregate_call_tree_data->measurement, call_tree_data->measurement);
90
- }
91
- else
92
- {
93
- // Copy the call tree so we don't touch the original and give Ruby ownerhip
94
- // of it so that it is freed on GC
95
- aggregate_call_tree_data = prof_call_tree_copy(call_tree_data);
96
- aggregate_call_tree_data->owner = OWNER_RUBY;
97
-
98
-
99
- rb_st_insert(callers, call_tree_data->method->key, (st_data_t)aggregate_call_tree_data);
100
- }
101
-
102
- return ST_CONTINUE;
103
- }
104
-
105
- size_t prof_call_trees_size(const void* data)
106
- {
107
- return sizeof(prof_call_trees_t);
108
- }
109
-
110
- static const rb_data_type_t call_trees_type =
111
- {
112
- .wrap_struct_name = "CallTrees",
113
- .function =
114
- {
115
- .dmark = prof_call_trees_mark,
116
- .dfree = prof_call_trees_ruby_gc_free,
117
- .dsize = prof_call_trees_size,
118
- },
119
- .data = NULL,
120
- .flags = RUBY_TYPED_FREE_IMMEDIATELY
121
- };
122
-
123
- VALUE prof_call_trees_wrap(prof_call_trees_t* call_trees)
124
- {
125
- if (call_trees->object == Qnil)
126
- {
127
- call_trees->object = TypedData_Wrap_Struct(cRpCallTrees, &call_trees_type, call_trees);
128
- }
129
- return call_trees->object;
130
- }
131
-
132
- void prof_add_call_tree(prof_call_trees_t* call_trees, prof_call_tree_t* call_tree)
133
- {
134
- if (call_trees->ptr == call_trees->end)
135
- {
136
- size_t len = call_trees->ptr - call_trees->start;
137
- size_t new_capacity = (call_trees->end - call_trees->start) * 2;
138
- REALLOC_N(call_trees->start, prof_call_tree_t*, new_capacity);
139
- call_trees->ptr = call_trees->start + len;
140
- call_trees->end = call_trees->start + new_capacity;
141
- }
142
- *call_trees->ptr = call_tree;
143
- call_trees->ptr++;
144
- }
145
-
146
- /* ================ Call Infos =================*/
147
- /* Document-class: RubyProf::CallTrees
148
- The RubyProf::MethodInfo class stores profiling data for a method.
149
- One instance of the RubyProf::MethodInfo class is created per method
150
- called per thread. Thus, if a method is called in two different
151
- thread then there will be two RubyProf::MethodInfo objects
152
- created. RubyProf::MethodInfo objects can be accessed via
153
- the RubyProf::Profile object. */
154
- VALUE prof_call_trees_allocate(VALUE klass)
155
- {
156
- prof_call_trees_t* call_trees_data = prof_call_trees_create();
157
- call_trees_data->object = prof_call_trees_wrap(call_trees_data);
158
- return call_trees_data->object;
159
- }
160
-
161
-
162
- /* call-seq:
163
- min_depth -> Integer
164
-
165
- Returns the minimum depth of this method in any call tree */
166
- VALUE prof_call_trees_min_depth(VALUE self)
167
- {
168
- unsigned int depth = INT_MAX;
169
-
170
- prof_call_trees_t* call_trees = prof_get_call_trees(self);
171
- for (prof_call_tree_t** p_call_tree = call_trees->start; p_call_tree < call_trees->ptr; p_call_tree++)
172
- {
173
- unsigned int call_tree_depth = prof_call_tree_figure_depth(*p_call_tree);
174
- if (call_tree_depth < depth)
175
- depth = call_tree_depth;
176
- }
177
-
178
- return UINT2NUM(depth);
179
- }
180
-
181
- /* call-seq:
182
- callers -> array
183
-
184
- Returns an array of all CallTree objects that called this method. */
185
- VALUE prof_call_trees_call_trees(VALUE self)
186
- {
187
- VALUE result = rb_ary_new();
188
-
189
- prof_call_trees_t* call_trees = prof_get_call_trees(self);
190
- for (prof_call_tree_t** p_call_tree = call_trees->start; p_call_tree < call_trees->ptr; p_call_tree++)
191
- {
192
- VALUE call_tree = prof_call_tree_wrap(*p_call_tree);
193
- rb_ary_push(result, call_tree);
194
- }
195
- return result;
196
- }
197
-
198
- /* call-seq:
199
- callers -> array
200
-
201
- Returns an array of aggregated CallTree objects that called this method (ie, parents).*/
202
- VALUE prof_call_trees_callers(VALUE self)
203
- {
204
- st_table* callers = rb_st_init_numtable();
205
-
206
- prof_call_trees_t* call_trees = prof_get_call_trees(self);
207
- for (prof_call_tree_t** p_call_tree = call_trees->start; p_call_tree < call_trees->ptr; p_call_tree++)
208
- {
209
- prof_call_tree_t* parent = (*p_call_tree)->parent;
210
- if (parent == NULL)
211
- continue;
212
-
213
- prof_call_tree_t* aggregate_call_tree_data = NULL;
214
-
215
- if (rb_st_lookup(callers, parent->method->key, (st_data_t*)&aggregate_call_tree_data))
216
- {
217
- prof_measurement_merge_internal(aggregate_call_tree_data->measurement, (*p_call_tree)->measurement);
218
- }
219
- else
220
- {
221
- // Copy the call tree so we don't touch the original and give Ruby ownerhip
222
- // of it so that it is freed on GC
223
- aggregate_call_tree_data = prof_call_tree_copy(*p_call_tree);
224
- aggregate_call_tree_data->owner = OWNER_RUBY;
225
-
226
- rb_st_insert(callers, parent->method->key, (st_data_t)aggregate_call_tree_data);
227
- }
228
- }
229
-
230
- VALUE result = rb_ary_new_capa((long)callers->num_entries);
231
- rb_st_foreach(callers, prof_call_trees_collect, result);
232
- rb_st_free_table(callers);
233
- return result;
234
- }
235
-
236
- /* call-seq:
237
- callees -> array
238
-
239
- Returns an array of aggregated CallTree objects that this method called (ie, children).*/
240
- VALUE prof_call_trees_callees(VALUE self)
241
- {
242
- st_table* callees = rb_st_init_numtable();
243
-
244
- prof_call_trees_t* call_trees = prof_get_call_trees(self);
245
- for (prof_call_tree_t** call_tree = call_trees->start; call_tree < call_trees->ptr; call_tree++)
246
- {
247
- rb_st_foreach((*call_tree)->children, prof_call_trees_collect_callees, (st_data_t)callees);
248
- }
249
-
250
- VALUE result = rb_ary_new_capa((long)callees->num_entries);
251
- rb_st_foreach(callees, prof_call_trees_collect, result);
252
- rb_st_free_table(callees);
253
- return result;
254
- }
255
-
256
- /* :nodoc: */
257
- VALUE prof_call_trees_dump(VALUE self)
258
- {
259
- VALUE result = rb_hash_new();
260
- rb_hash_aset(result, ID2SYM(rb_intern("call_trees")), prof_call_trees_call_trees(self));
261
-
262
- return result;
263
- }
264
-
265
- /* :nodoc: */
266
- VALUE prof_call_trees_load(VALUE self, VALUE data)
267
- {
268
- prof_call_trees_t* call_trees_data = prof_get_call_trees(self);
269
- call_trees_data->object = self;
270
-
271
- VALUE call_trees = rb_hash_aref(data, ID2SYM(rb_intern("call_trees")));
272
- for (int i = 0; i < rb_array_len(call_trees); i++)
273
- {
274
- VALUE call_tree = rb_ary_entry(call_trees, i);
275
- prof_call_tree_t* call_tree_data = prof_get_call_tree(call_tree);
276
- prof_add_call_tree(call_trees_data, call_tree_data);
277
- }
278
-
279
- return data;
280
- }
281
-
282
- void rp_init_call_trees(void)
283
- {
284
- cRpCallTrees = rb_define_class_under(mProf, "CallTrees", rb_cObject);
285
- rb_undef_method(CLASS_OF(cRpCallTrees), "new");
286
- rb_define_alloc_func(cRpCallTrees, prof_call_trees_allocate);
287
-
288
- rb_define_method(cRpCallTrees, "min_depth", prof_call_trees_min_depth, 0);
289
-
290
- rb_define_method(cRpCallTrees, "call_trees", prof_call_trees_call_trees, 0);
291
- rb_define_method(cRpCallTrees, "callers", prof_call_trees_callers, 0);
292
- rb_define_method(cRpCallTrees, "callees", prof_call_trees_callees, 0);
293
-
294
- rb_define_method(cRpCallTrees, "_dump_data", prof_call_trees_dump, 0);
295
- rb_define_method(cRpCallTrees, "_load_data", prof_call_trees_load, 1);
296
- }
1
+ /* Copyright (C) 2005-2013 Shugo Maeda <shugo@ruby-lang.org> and Charlie Savage <cfis@savagexi.com>
2
+ Please see the LICENSE file for copyright and distribution information */
3
+
4
+ #include "rp_call_trees.h"
5
+ #include "rp_measurement.h"
6
+
7
+ #define INITIAL_CALL_TREES_SIZE 2
8
+
9
+ VALUE cRpCallTrees;
10
+
11
+ /* ======= Call Infos ========*/
12
+ prof_call_trees_t* prof_get_call_trees(VALUE self)
13
+ {
14
+ /* Can't use Data_Get_Struct because that triggers the event hook
15
+ ending up in endless recursion. */
16
+ prof_call_trees_t* result = RTYPEDDATA_DATA(self);
17
+
18
+ if (!result)
19
+ rb_raise(rb_eRuntimeError, "This RubyProf::CallTrees instance has already been freed, likely because its profile has been freed.");
20
+
21
+ return result;
22
+ }
23
+
24
+ prof_call_trees_t* prof_call_trees_create(void)
25
+ {
26
+ prof_call_trees_t* result = ALLOC(prof_call_trees_t);
27
+ result->start = ALLOC_N(prof_call_tree_t*, INITIAL_CALL_TREES_SIZE);
28
+ result->end = result->start + INITIAL_CALL_TREES_SIZE;
29
+ result->ptr = result->start;
30
+ result->object = Qnil;
31
+ return result;
32
+ }
33
+
34
+ void prof_call_trees_mark(void* data)
35
+ {
36
+ if (!data) return;
37
+
38
+ prof_call_trees_t* call_trees = (prof_call_trees_t*)data;
39
+ prof_call_tree_t** call_tree;
40
+ for (call_tree = call_trees->start; call_tree < call_trees->ptr; call_tree++)
41
+ {
42
+ prof_call_tree_mark(*call_tree);
43
+ }
44
+ }
45
+
46
+ void prof_call_trees_free(prof_call_trees_t* call_trees)
47
+ {
48
+ /* Has this method object been accessed by Ruby? If
49
+ yes clean it up so to avoid a segmentation fault. */
50
+ if (call_trees->object != Qnil)
51
+ {
52
+ RTYPEDDATA(call_trees->object)->data = NULL;
53
+ call_trees->object = Qnil;
54
+ }
55
+
56
+ // Note we do not free our call_tree structures - since they have no parents they will free themselves
57
+ xfree(call_trees);
58
+ }
59
+
60
+ void prof_call_trees_ruby_gc_free(void* data)
61
+ {
62
+ if (data)
63
+ {
64
+ // This object gets freed by its owning method
65
+ prof_call_trees_t* call_trees = (prof_call_trees_t*)data;
66
+ call_trees->object = Qnil;
67
+ }
68
+ }
69
+
70
+ static int prof_call_trees_collect(st_data_t key, st_data_t value, st_data_t data)
71
+ {
72
+ VALUE result = (VALUE)data;
73
+ prof_call_tree_t* call_tree_data = (prof_call_tree_t*)value;
74
+ VALUE aggregate_call_tree = prof_call_tree_wrap(call_tree_data);
75
+ rb_ary_push(result, aggregate_call_tree);
76
+
77
+ return ST_CONTINUE;
78
+ }
79
+
80
+ static int prof_call_trees_collect_callees(st_data_t key, st_data_t value, st_data_t hash)
81
+ {
82
+ st_table* callers = (st_table*)hash;
83
+ prof_call_tree_t* call_tree_data = (prof_call_tree_t*)value;
84
+
85
+ prof_call_tree_t* aggregate_call_tree_data = NULL;
86
+
87
+ if (rb_st_lookup(callers, call_tree_data->method->key, (st_data_t*)&aggregate_call_tree_data))
88
+ {
89
+ prof_measurement_merge_internal(aggregate_call_tree_data->measurement, call_tree_data->measurement);
90
+ }
91
+ else
92
+ {
93
+ // Copy the call tree so we don't touch the original and give Ruby ownerhip
94
+ // of it so that it is freed on GC
95
+ aggregate_call_tree_data = prof_call_tree_copy(call_tree_data);
96
+ aggregate_call_tree_data->owner = OWNER_RUBY;
97
+
98
+
99
+ rb_st_insert(callers, call_tree_data->method->key, (st_data_t)aggregate_call_tree_data);
100
+ }
101
+
102
+ return ST_CONTINUE;
103
+ }
104
+
105
+ size_t prof_call_trees_size(const void* data)
106
+ {
107
+ return sizeof(prof_call_trees_t);
108
+ }
109
+
110
+ static const rb_data_type_t call_trees_type =
111
+ {
112
+ .wrap_struct_name = "CallTrees",
113
+ .function =
114
+ {
115
+ .dmark = prof_call_trees_mark,
116
+ .dfree = prof_call_trees_ruby_gc_free,
117
+ .dsize = prof_call_trees_size,
118
+ },
119
+ .data = NULL,
120
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY
121
+ };
122
+
123
+ VALUE prof_call_trees_wrap(prof_call_trees_t* call_trees)
124
+ {
125
+ if (call_trees->object == Qnil)
126
+ {
127
+ call_trees->object = TypedData_Wrap_Struct(cRpCallTrees, &call_trees_type, call_trees);
128
+ }
129
+ return call_trees->object;
130
+ }
131
+
132
+ void prof_add_call_tree(prof_call_trees_t* call_trees, prof_call_tree_t* call_tree)
133
+ {
134
+ if (call_trees->ptr == call_trees->end)
135
+ {
136
+ size_t len = call_trees->ptr - call_trees->start;
137
+ size_t new_capacity = (call_trees->end - call_trees->start) * 2;
138
+ REALLOC_N(call_trees->start, prof_call_tree_t*, new_capacity);
139
+ call_trees->ptr = call_trees->start + len;
140
+ call_trees->end = call_trees->start + new_capacity;
141
+ }
142
+ *call_trees->ptr = call_tree;
143
+ call_trees->ptr++;
144
+ }
145
+
146
+ /* ================ Call Infos =================*/
147
+ /* Document-class: RubyProf::CallTrees
148
+ The RubyProf::MethodInfo class stores profiling data for a method.
149
+ One instance of the RubyProf::MethodInfo class is created per method
150
+ called per thread. Thus, if a method is called in two different
151
+ thread then there will be two RubyProf::MethodInfo objects
152
+ created. RubyProf::MethodInfo objects can be accessed via
153
+ the RubyProf::Profile object. */
154
+ VALUE prof_call_trees_allocate(VALUE klass)
155
+ {
156
+ prof_call_trees_t* call_trees_data = prof_call_trees_create();
157
+ call_trees_data->object = prof_call_trees_wrap(call_trees_data);
158
+ return call_trees_data->object;
159
+ }
160
+
161
+
162
+ /* call-seq:
163
+ min_depth -> Integer
164
+
165
+ Returns the minimum depth of this method in any call tree */
166
+ VALUE prof_call_trees_min_depth(VALUE self)
167
+ {
168
+ unsigned int depth = INT_MAX;
169
+
170
+ prof_call_trees_t* call_trees = prof_get_call_trees(self);
171
+ for (prof_call_tree_t** p_call_tree = call_trees->start; p_call_tree < call_trees->ptr; p_call_tree++)
172
+ {
173
+ unsigned int call_tree_depth = prof_call_tree_figure_depth(*p_call_tree);
174
+ if (call_tree_depth < depth)
175
+ depth = call_tree_depth;
176
+ }
177
+
178
+ return UINT2NUM(depth);
179
+ }
180
+
181
+ /* call-seq:
182
+ callers -> array
183
+
184
+ Returns an array of all CallTree objects that called this method. */
185
+ VALUE prof_call_trees_call_trees(VALUE self)
186
+ {
187
+ VALUE result = rb_ary_new();
188
+
189
+ prof_call_trees_t* call_trees = prof_get_call_trees(self);
190
+ for (prof_call_tree_t** p_call_tree = call_trees->start; p_call_tree < call_trees->ptr; p_call_tree++)
191
+ {
192
+ VALUE call_tree = prof_call_tree_wrap(*p_call_tree);
193
+ rb_ary_push(result, call_tree);
194
+ }
195
+ return result;
196
+ }
197
+
198
+ /* call-seq:
199
+ callers -> array
200
+
201
+ Returns an array of aggregated CallTree objects that called this method (ie, parents).*/
202
+ VALUE prof_call_trees_callers(VALUE self)
203
+ {
204
+ st_table* callers = rb_st_init_numtable();
205
+
206
+ prof_call_trees_t* call_trees = prof_get_call_trees(self);
207
+ for (prof_call_tree_t** p_call_tree = call_trees->start; p_call_tree < call_trees->ptr; p_call_tree++)
208
+ {
209
+ prof_call_tree_t* parent = (*p_call_tree)->parent;
210
+ if (parent == NULL)
211
+ continue;
212
+
213
+ prof_call_tree_t* aggregate_call_tree_data = NULL;
214
+
215
+ if (rb_st_lookup(callers, parent->method->key, (st_data_t*)&aggregate_call_tree_data))
216
+ {
217
+ prof_measurement_merge_internal(aggregate_call_tree_data->measurement, (*p_call_tree)->measurement);
218
+ }
219
+ else
220
+ {
221
+ // Copy the call tree so we don't touch the original and give Ruby ownerhip
222
+ // of it so that it is freed on GC
223
+ aggregate_call_tree_data = prof_call_tree_copy(*p_call_tree);
224
+ aggregate_call_tree_data->owner = OWNER_RUBY;
225
+
226
+ rb_st_insert(callers, parent->method->key, (st_data_t)aggregate_call_tree_data);
227
+ }
228
+ }
229
+
230
+ VALUE result = rb_ary_new_capa((long)callers->num_entries);
231
+ rb_st_foreach(callers, prof_call_trees_collect, result);
232
+ rb_st_free_table(callers);
233
+ return result;
234
+ }
235
+
236
+ /* call-seq:
237
+ callees -> array
238
+
239
+ Returns an array of aggregated CallTree objects that this method called (ie, children).*/
240
+ VALUE prof_call_trees_callees(VALUE self)
241
+ {
242
+ st_table* callees = rb_st_init_numtable();
243
+
244
+ prof_call_trees_t* call_trees = prof_get_call_trees(self);
245
+ for (prof_call_tree_t** call_tree = call_trees->start; call_tree < call_trees->ptr; call_tree++)
246
+ {
247
+ rb_st_foreach((*call_tree)->children, prof_call_trees_collect_callees, (st_data_t)callees);
248
+ }
249
+
250
+ VALUE result = rb_ary_new_capa((long)callees->num_entries);
251
+ rb_st_foreach(callees, prof_call_trees_collect, result);
252
+ rb_st_free_table(callees);
253
+ return result;
254
+ }
255
+
256
+ /* :nodoc: */
257
+ VALUE prof_call_trees_dump(VALUE self)
258
+ {
259
+ VALUE result = rb_hash_new();
260
+ rb_hash_aset(result, ID2SYM(rb_intern("call_trees")), prof_call_trees_call_trees(self));
261
+
262
+ return result;
263
+ }
264
+
265
+ /* :nodoc: */
266
+ VALUE prof_call_trees_load(VALUE self, VALUE data)
267
+ {
268
+ prof_call_trees_t* call_trees_data = prof_get_call_trees(self);
269
+ call_trees_data->object = self;
270
+
271
+ VALUE call_trees = rb_hash_aref(data, ID2SYM(rb_intern("call_trees")));
272
+ for (int i = 0; i < rb_array_len(call_trees); i++)
273
+ {
274
+ VALUE call_tree = rb_ary_entry(call_trees, i);
275
+ prof_call_tree_t* call_tree_data = prof_get_call_tree(call_tree);
276
+ prof_add_call_tree(call_trees_data, call_tree_data);
277
+ }
278
+
279
+ return data;
280
+ }
281
+
282
+ void rp_init_call_trees(void)
283
+ {
284
+ cRpCallTrees = rb_define_class_under(mProf, "CallTrees", rb_cObject);
285
+ rb_undef_method(CLASS_OF(cRpCallTrees), "new");
286
+ rb_define_alloc_func(cRpCallTrees, prof_call_trees_allocate);
287
+
288
+ rb_define_method(cRpCallTrees, "min_depth", prof_call_trees_min_depth, 0);
289
+
290
+ rb_define_method(cRpCallTrees, "call_trees", prof_call_trees_call_trees, 0);
291
+ rb_define_method(cRpCallTrees, "callers", prof_call_trees_callers, 0);
292
+ rb_define_method(cRpCallTrees, "callees", prof_call_trees_callees, 0);
293
+
294
+ rb_define_method(cRpCallTrees, "_dump_data", prof_call_trees_dump, 0);
295
+ rb_define_method(cRpCallTrees, "_load_data", prof_call_trees_load, 1);
296
+ }