ruby-prof 1.6.3 → 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 (48) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGES +19 -0
  3. data/ext/ruby_prof/extconf.rb +23 -22
  4. data/ext/ruby_prof/rp_allocation.c +342 -342
  5. data/ext/ruby_prof/rp_call_tree.c +1 -1
  6. data/ext/ruby_prof/rp_call_tree.h +1 -1
  7. data/ext/ruby_prof/rp_call_trees.c +296 -296
  8. data/ext/ruby_prof/rp_call_trees.h +28 -28
  9. data/ext/ruby_prof/rp_measure_allocations.c +47 -47
  10. data/ext/ruby_prof/rp_measure_memory.c +46 -46
  11. data/ext/ruby_prof/rp_measure_process_time.c +64 -66
  12. data/ext/ruby_prof/rp_measure_wall_time.c +52 -64
  13. data/ext/ruby_prof/rp_measurement.c +364 -364
  14. data/ext/ruby_prof/rp_method.c +551 -550
  15. data/ext/ruby_prof/rp_method.h +5 -2
  16. data/ext/ruby_prof/rp_profile.c +2 -2
  17. data/ext/ruby_prof/rp_profile.h +36 -36
  18. data/ext/ruby_prof/rp_stack.c +212 -212
  19. data/ext/ruby_prof/rp_thread.c +1 -1
  20. data/ext/ruby_prof/ruby_prof.c +50 -50
  21. data/ext/ruby_prof/ruby_prof.h +35 -34
  22. data/ext/ruby_prof/vc/ruby_prof.vcxproj +5 -7
  23. data/lib/ruby-prof/compatibility.rb +113 -113
  24. data/lib/ruby-prof/exclude_common_methods.rb +204 -198
  25. data/lib/ruby-prof/method_info.rb +87 -85
  26. data/lib/ruby-prof/printers/abstract_printer.rb +156 -138
  27. data/lib/ruby-prof/version.rb +3 -3
  28. data/ruby-prof.gemspec +66 -64
  29. data/test/dynamic_method_test.rb +9 -21
  30. data/test/enumerable_test.rb +23 -21
  31. data/test/exclude_methods_test.rb +363 -146
  32. data/test/fiber_test.rb +195 -195
  33. data/test/gc_test.rb +104 -102
  34. data/test/line_number_test.rb +426 -134
  35. data/test/measure_allocations_test.rb +1172 -660
  36. data/test/measure_memory_test.rb +1193 -1024
  37. data/test/measure_process_time_test.rb +3330 -1610
  38. data/test/measure_wall_time_test.rb +634 -420
  39. data/test/merge_test.rb +146 -146
  40. data/test/method_info_test.rb +100 -95
  41. data/test/printers_test.rb +178 -135
  42. data/test/recursive_test.rb +796 -425
  43. data/test/start_stop_test.rb +4 -4
  44. data/test/test_helper.rb +20 -20
  45. data/test/thread_test.rb +229 -235
  46. data/test/unique_call_path_test.rb +9 -22
  47. data/test/yarv_test.rb +1 -5
  48. metadata +33 -8
@@ -1,550 +1,551 @@
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_allocation.h"
5
- #include "rp_call_trees.h"
6
- #include "rp_method.h"
7
-
8
- VALUE cRpMethodInfo;
9
-
10
- /* ================ Helper Functions =================*/
11
- VALUE resolve_klass(VALUE klass, unsigned int* klass_flags)
12
- {
13
- VALUE result = klass;
14
-
15
- if (klass == 0 || klass == Qnil)
16
- {
17
- result = Qnil;
18
- }
19
- else if (BUILTIN_TYPE(klass) == T_CLASS && FL_TEST(klass, FL_SINGLETON))
20
- {
21
- /* We have come across a singleton object. First
22
- figure out what it is attached to.*/
23
- VALUE attached = rb_iv_get(klass, "__attached__");
24
-
25
- switch (BUILTIN_TYPE(attached))
26
- {
27
- /* Is this a singleton class acting as a metaclass? */
28
- case T_CLASS:
29
- {
30
- *klass_flags |= kClassSingleton;
31
- result = attached;
32
- break;
33
- }
34
- /* Is this for singleton methods on a module? */
35
- case T_MODULE:
36
- {
37
- *klass_flags |= kModuleSingleton;
38
- result = attached;
39
- break;
40
- }
41
- /* Is this for singleton methods on an object? */
42
- case T_OBJECT:
43
- {
44
- *klass_flags |= kObjectSingleton;
45
- result = rb_class_superclass(klass);
46
- break;
47
- }
48
- /* Ok, this could be other things like an array put onto
49
- a singleton object (yeah, it happens, see the singleton
50
- objects test case). */
51
- default:
52
- {
53
- *klass_flags |= kOtherSingleton;
54
- result = klass;
55
- break;
56
- }
57
- }
58
- }
59
- /* Is this an include for a module? If so get the actual
60
- module class since we want to combine all profiling
61
- results for that module. */
62
- else if (BUILTIN_TYPE(klass) == T_ICLASS)
63
- {
64
- unsigned int dummy;
65
- *klass_flags |= kModuleIncludee;
66
- result = resolve_klass(RBASIC_CLASS(klass), &dummy);
67
- }
68
- return result;
69
- }
70
-
71
- VALUE resolve_klass_name(VALUE klass, unsigned int* klass_flags)
72
- {
73
- VALUE result = Qnil;
74
-
75
- if (klass == Qnil)
76
- {
77
- result = rb_str_new2("[global]");
78
- }
79
- else if (*klass_flags & kOtherSingleton)
80
- {
81
- result = rb_any_to_s(klass);
82
- }
83
- else
84
- {
85
- result = rb_class_name(klass);
86
- }
87
-
88
- return result;
89
- }
90
-
91
- st_data_t method_key(VALUE klass, VALUE msym)
92
- {
93
- VALUE resolved_klass = klass;
94
-
95
- /* Is this an include for a module? If so get the actual
96
- module class since we want to combine all profiling
97
- results for that module. */
98
- if (klass == 0 || klass == Qnil)
99
- {
100
- resolved_klass = Qnil;
101
- }
102
- else if (BUILTIN_TYPE(klass) == T_ICLASS)
103
- {
104
- resolved_klass = RBASIC_CLASS(klass);
105
- }
106
-
107
- st_data_t hash = rb_hash_start(0);
108
- hash = rb_hash_uint(hash, resolved_klass);
109
- hash = rb_hash_uint(hash, msym);
110
- hash = rb_hash_end(hash);
111
-
112
- return hash;
113
- }
114
-
115
- /* ================ prof_method_t =================*/
116
- prof_method_t* prof_get_method(VALUE self)
117
- {
118
- /* Can't use Data_Get_Struct because that triggers the event hook
119
- ending up in endless recursion. */
120
- prof_method_t* result = RTYPEDDATA_DATA(self);
121
-
122
- if (!result)
123
- rb_raise(rb_eRuntimeError, "This RubyProf::MethodInfo instance has already been freed, likely because its profile has been freed.");
124
-
125
- return result;
126
- }
127
-
128
- prof_method_t* prof_method_create(VALUE profile, VALUE klass, VALUE msym, VALUE source_file, int source_line)
129
- {
130
- prof_method_t* result = ALLOC(prof_method_t);
131
- result->profile = profile;
132
-
133
- result->key = method_key(klass, msym);
134
- result->klass_flags = 0;
135
-
136
- /* Note we do not call resolve_klass_name now because that causes an object allocation that shows up
137
- in the allocation results so we want to avoid it until after the profile run is complete. */
138
- result->klass = resolve_klass(klass, &result->klass_flags);
139
- result->klass_name = Qnil;
140
- result->method_name = msym;
141
- result->measurement = prof_measurement_create();
142
-
143
- result->call_trees = prof_call_trees_create();
144
- result->allocations_table = prof_allocations_create();
145
-
146
- result->visits = 0;
147
- result->recursive = false;
148
-
149
- result->object = Qnil;
150
-
151
- result->source_file = source_file;
152
- result->source_line = source_line;
153
-
154
- return result;
155
- }
156
-
157
- prof_method_t* prof_method_copy(prof_method_t* other)
158
- {
159
- prof_method_t* result = prof_method_create(other->profile, other->klass, other->method_name, other->source_file, other->source_line);
160
- result->measurement = prof_measurement_copy(other->measurement);
161
-
162
- return result;
163
- }
164
-
165
- /* The underlying c structures are freed when the parent profile is freed.
166
- However, on shutdown the Ruby GC frees objects in any will-nilly order.
167
- That means the ruby thread object wrapping the c thread struct may
168
- be freed before the parent profile. Thus we add in a free function
169
- for the garbage collector so that if it does get called will nil
170
- out our Ruby object reference.*/
171
- static void prof_method_ruby_gc_free(void* data)
172
- {
173
- if (data)
174
- {
175
- prof_method_t* method = (prof_method_t*)data;
176
- method->object = Qnil;
177
- }
178
- }
179
-
180
- static void prof_method_free(prof_method_t* method)
181
- {
182
- /* Has this method object been accessed by Ruby? If
183
- yes clean it up so to avoid a segmentation fault. */
184
- if (method->object != Qnil)
185
- {
186
- RTYPEDDATA(method->object)->data = NULL;
187
- method->object = Qnil;
188
- }
189
-
190
- prof_allocations_free(method->allocations_table);
191
- prof_call_trees_free(method->call_trees);
192
- prof_measurement_free(method->measurement);
193
- xfree(method);
194
- }
195
-
196
- size_t prof_method_size(const void* data)
197
- {
198
- return sizeof(prof_method_t);
199
- }
200
-
201
- void prof_method_mark(void* data)
202
- {
203
- if (!data) return;
204
-
205
- prof_method_t* method = (prof_method_t*)data;
206
-
207
- if (method->profile != Qnil)
208
- rb_gc_mark_movable(method->profile);
209
-
210
- if (method->object != Qnil)
211
- rb_gc_mark_movable(method->object);
212
-
213
- rb_gc_mark_movable(method->klass_name);
214
- rb_gc_mark_movable(method->method_name);
215
- rb_gc_mark(method->source_file);
216
-
217
- if (method->klass != Qnil)
218
- rb_gc_mark(method->klass);
219
-
220
- prof_measurement_mark(method->measurement);
221
- prof_allocations_mark(method->allocations_table);
222
- }
223
-
224
- void prof_method_compact(void* data)
225
- {
226
- prof_method_t* method = (prof_method_t*)data;
227
- method->object = rb_gc_location(method->object);
228
- method->profile = rb_gc_location(method->profile);
229
- method->klass_name = rb_gc_location(method->klass_name);
230
- method->method_name = rb_gc_location(method->method_name);
231
- }
232
-
233
- static VALUE prof_method_allocate(VALUE klass)
234
- {
235
- prof_method_t* method_data = prof_method_create(Qnil, Qnil, Qnil, Qnil, 0);
236
- method_data->object = prof_method_wrap(method_data);
237
- return method_data->object;
238
- }
239
-
240
- static const rb_data_type_t method_info_type =
241
- {
242
- .wrap_struct_name = "MethodInfo",
243
- .function =
244
- {
245
- .dmark = prof_method_mark,
246
- .dfree = prof_method_ruby_gc_free,
247
- .dsize = prof_method_size,
248
- .dcompact = prof_method_compact
249
- },
250
- .data = NULL,
251
- .flags = RUBY_TYPED_FREE_IMMEDIATELY
252
- };
253
-
254
- VALUE prof_method_wrap(prof_method_t* method)
255
- {
256
- if (method->object == Qnil)
257
- {
258
- method->object = TypedData_Wrap_Struct(cRpMethodInfo, &method_info_type, method);
259
- }
260
- return method->object;
261
- }
262
-
263
- st_table* method_table_create()
264
- {
265
- return rb_st_init_numtable();
266
- }
267
-
268
- static int method_table_free_iterator(st_data_t key, st_data_t value, st_data_t dummy)
269
- {
270
- prof_method_free((prof_method_t*)value);
271
- return ST_CONTINUE;
272
- }
273
-
274
- void method_table_free(st_table* table)
275
- {
276
- rb_st_foreach(table, method_table_free_iterator, 0);
277
- rb_st_free_table(table);
278
- }
279
-
280
- size_t method_table_insert(st_table* table, st_data_t key, prof_method_t* val)
281
- {
282
- return rb_st_insert(table, (st_data_t)key, (st_data_t)val);
283
- }
284
-
285
- static int prof_method_table_merge_internal(st_data_t key, st_data_t value, st_data_t data)
286
- {
287
- st_table* self_table = (st_table*)data;
288
- prof_method_t* other_child = (prof_method_t*)value;
289
-
290
- prof_method_t* self_child = method_table_lookup(self_table, other_child->key);
291
- if (self_child)
292
- {
293
- prof_measurement_merge_internal(self_child->measurement, other_child->measurement);
294
- }
295
- else
296
- {
297
- prof_method_t* copy = prof_method_copy(other_child);
298
- method_table_insert(self_table, copy->key, copy);
299
- }
300
-
301
- return ST_CONTINUE;
302
- }
303
-
304
- void prof_method_table_merge(st_table* self, st_table* other)
305
- {
306
- rb_st_foreach(other, prof_method_table_merge_internal, (st_data_t)self);
307
- }
308
-
309
- prof_method_t* method_table_lookup(st_table* table, st_data_t key)
310
- {
311
- st_data_t val;
312
- if (rb_st_lookup(table, (st_data_t)key, &val))
313
- {
314
- return (prof_method_t*)val;
315
- }
316
- else
317
- {
318
- return NULL;
319
- }
320
- }
321
-
322
- /* ================ Method Info =================*/
323
- /* Document-class: RubyProf::MethodInfo
324
- The RubyProf::MethodInfo class stores profiling data for a method.
325
- One instance of the RubyProf::MethodInfo class is created per method
326
- called per thread. Thus, if a method is called in two different
327
- thread then there will be two RubyProf::MethodInfo objects
328
- created. RubyProf::MethodInfo objects can be accessed via
329
- the RubyProf::Profile object.
330
- */
331
-
332
- /* call-seq:
333
- new(klass, method_name) -> method_info
334
-
335
- Creates a new MethodInfo instance. +Klass+ should be a reference to
336
- a Ruby class and +method_name+ a symbol identifying one of its instance methods.*/
337
- static VALUE prof_method_initialize(VALUE self, VALUE klass, VALUE method_name)
338
- {
339
- prof_method_t* method_ptr = prof_get_method(self);
340
- method_ptr->klass = klass;
341
- method_ptr->method_name = method_name;
342
-
343
- // Setup method key
344
- method_ptr->key = method_key(klass, method_name);
345
-
346
- // Get method object
347
- VALUE ruby_method = rb_funcall(klass, rb_intern("instance_method"), 1, method_name);
348
-
349
- // Get source file and line number
350
- VALUE location_array = rb_funcall(ruby_method, rb_intern("source_location"), 0);
351
- if (location_array != Qnil && RARRAY_LEN(location_array) == 2)
352
- {
353
- method_ptr->source_file = rb_ary_entry(location_array, 0);
354
- method_ptr->source_line = NUM2INT(rb_ary_entry(location_array, 1));
355
- }
356
-
357
- return self;
358
- }
359
-
360
- /* call-seq:
361
- hash -> hash
362
-
363
- Returns the hash key for this method info. The hash key is calculated based on the
364
- klass name and method name */
365
- static VALUE prof_method_hash(VALUE self)
366
- {
367
- prof_method_t* method_ptr = prof_get_method(self);
368
- return ULL2NUM(method_ptr->key);
369
- }
370
-
371
- /* call-seq:
372
- allocations -> array
373
-
374
- Returns an array of allocation information.*/
375
- static VALUE prof_method_allocations(VALUE self)
376
- {
377
- prof_method_t* method = prof_get_method(self);
378
- return prof_allocations_wrap(method->allocations_table);
379
- }
380
-
381
- /* call-seq:
382
- called -> Measurement
383
-
384
- Returns the measurement associated with this method. */
385
- static VALUE prof_method_measurement(VALUE self)
386
- {
387
- prof_method_t* method = prof_get_method(self);
388
- return prof_measurement_wrap(method->measurement);
389
- }
390
-
391
- /* call-seq:
392
- source_file => string
393
-
394
- Returns the source file of the method
395
- */
396
- static VALUE prof_method_source_file(VALUE self)
397
- {
398
- prof_method_t* method = prof_get_method(self);
399
- return method->source_file;
400
- }
401
-
402
- /* call-seq:
403
- line_no -> int
404
-
405
- returns the line number of the method */
406
- static VALUE prof_method_line(VALUE self)
407
- {
408
- prof_method_t* method = prof_get_method(self);
409
- return INT2FIX(method->source_line);
410
- }
411
-
412
- /* call-seq:
413
- klass_name -> String
414
-
415
- Returns the name of this method's class. Singleton classes
416
- will have the form <Object::Object>. */
417
-
418
- static VALUE prof_method_klass_name(VALUE self)
419
- {
420
- prof_method_t* method = prof_get_method(self);
421
- if (method->klass_name == Qnil)
422
- method->klass_name = resolve_klass_name(method->klass, &method->klass_flags);
423
-
424
- return method->klass_name;
425
- }
426
-
427
- /* call-seq:
428
- klass_flags -> integer
429
-
430
- Returns the klass flags */
431
-
432
- static VALUE prof_method_klass_flags(VALUE self)
433
- {
434
- prof_method_t* method = prof_get_method(self);
435
- return INT2FIX(method->klass_flags);
436
- }
437
-
438
- /* call-seq:
439
- method_name -> Symbol
440
-
441
- Returns the name of this method in the format Object#method. Singletons
442
- methods will be returned in the format <Object::Object>#method.*/
443
-
444
- static VALUE prof_method_name(VALUE self)
445
- {
446
- prof_method_t* method = prof_get_method(self);
447
- return method->method_name;
448
- }
449
-
450
- /* call-seq:
451
- recursive? -> boolean
452
-
453
- Returns the true if this method is recursively invoked */
454
- static VALUE prof_method_recursive(VALUE self)
455
- {
456
- prof_method_t* method = prof_get_method(self);
457
- return method->recursive ? Qtrue : Qfalse;
458
- }
459
-
460
- /* call-seq:
461
- call_trees -> CallTrees
462
-
463
- Returns the CallTrees associated with this method. */
464
- static VALUE prof_method_call_trees(VALUE self)
465
- {
466
- prof_method_t* method = prof_get_method(self);
467
- return prof_call_trees_wrap(method->call_trees);
468
- }
469
-
470
- /* :nodoc: */
471
- static VALUE prof_method_dump(VALUE self)
472
- {
473
- prof_method_t* method_data = prof_get_method(self);
474
- VALUE result = rb_hash_new();
475
-
476
- rb_hash_aset(result, ID2SYM(rb_intern("klass_name")), prof_method_klass_name(self));
477
- rb_hash_aset(result, ID2SYM(rb_intern("klass_flags")), INT2FIX(method_data->klass_flags));
478
- rb_hash_aset(result, ID2SYM(rb_intern("method_name")), method_data->method_name);
479
-
480
- rb_hash_aset(result, ID2SYM(rb_intern("key")), ULL2NUM(method_data->key));
481
- rb_hash_aset(result, ID2SYM(rb_intern("recursive")), prof_method_recursive(self));
482
- rb_hash_aset(result, ID2SYM(rb_intern("source_file")), method_data->source_file);
483
- rb_hash_aset(result, ID2SYM(rb_intern("source_line")), INT2FIX(method_data->source_line));
484
-
485
- rb_hash_aset(result, ID2SYM(rb_intern("call_trees")), prof_call_trees_wrap(method_data->call_trees));
486
- rb_hash_aset(result, ID2SYM(rb_intern("measurement")), prof_measurement_wrap(method_data->measurement));
487
- rb_hash_aset(result, ID2SYM(rb_intern("allocations")), prof_method_allocations(self));
488
-
489
- return result;
490
- }
491
-
492
- /* :nodoc: */
493
- static VALUE prof_method_load(VALUE self, VALUE data)
494
- {
495
- prof_method_t* method_data = prof_get_method(self);
496
- method_data->object = self;
497
-
498
- method_data->klass_name = rb_hash_aref(data, ID2SYM(rb_intern("klass_name")));
499
- method_data->klass_flags = FIX2INT(rb_hash_aref(data, ID2SYM(rb_intern("klass_flags"))));
500
- method_data->method_name = rb_hash_aref(data, ID2SYM(rb_intern("method_name")));
501
- method_data->key = RB_NUM2ULL(rb_hash_aref(data, ID2SYM(rb_intern("key"))));
502
-
503
- method_data->recursive = rb_hash_aref(data, ID2SYM(rb_intern("recursive"))) == Qtrue ? true : false;
504
-
505
- method_data->source_file = rb_hash_aref(data, ID2SYM(rb_intern("source_file")));
506
- method_data->source_line = FIX2INT(rb_hash_aref(data, ID2SYM(rb_intern("source_line"))));
507
-
508
- VALUE call_trees = rb_hash_aref(data, ID2SYM(rb_intern("call_trees")));
509
- method_data->call_trees = prof_get_call_trees(call_trees);
510
-
511
- VALUE measurement = rb_hash_aref(data, ID2SYM(rb_intern("measurement")));
512
- method_data->measurement = prof_get_measurement(measurement);
513
-
514
- VALUE allocations = rb_hash_aref(data, ID2SYM(rb_intern("allocations")));
515
- prof_allocations_unwrap(method_data->allocations_table, allocations);
516
- return data;
517
- }
518
-
519
- void rp_init_method_info()
520
- {
521
- /* MethodInfo */
522
- cRpMethodInfo = rb_define_class_under(mProf, "MethodInfo", rb_cObject);
523
-
524
- rb_define_const(cRpMethodInfo, "MODULE_INCLUDEE", INT2NUM(kModuleIncludee));
525
- rb_define_const(cRpMethodInfo, "CLASS_SINGLETON", INT2NUM(kClassSingleton));
526
- rb_define_const(cRpMethodInfo, "MODULE_SINGLETON", INT2NUM(kModuleSingleton));
527
- rb_define_const(cRpMethodInfo, "OBJECT_SINGLETON", INT2NUM(kObjectSingleton));
528
- rb_define_const(cRpMethodInfo, "OTHER_SINGLETON", INT2NUM(kOtherSingleton));
529
-
530
- rb_define_alloc_func(cRpMethodInfo, prof_method_allocate);
531
- rb_define_method(cRpMethodInfo, "initialize", prof_method_initialize, 2);
532
- rb_define_method(cRpMethodInfo, "hash", prof_method_hash, 0);
533
-
534
- rb_define_method(cRpMethodInfo, "klass_name", prof_method_klass_name, 0);
535
- rb_define_method(cRpMethodInfo, "klass_flags", prof_method_klass_flags, 0);
536
- rb_define_method(cRpMethodInfo, "method_name", prof_method_name, 0);
537
-
538
- rb_define_method(cRpMethodInfo, "call_trees", prof_method_call_trees, 0);
539
-
540
- rb_define_method(cRpMethodInfo, "allocations", prof_method_allocations, 0);
541
- rb_define_method(cRpMethodInfo, "measurement", prof_method_measurement, 0);
542
-
543
- rb_define_method(cRpMethodInfo, "source_file", prof_method_source_file, 0);
544
- rb_define_method(cRpMethodInfo, "line", prof_method_line, 0);
545
-
546
- rb_define_method(cRpMethodInfo, "recursive?", prof_method_recursive, 0);
547
-
548
- rb_define_method(cRpMethodInfo, "_dump_data", prof_method_dump, 0);
549
- rb_define_method(cRpMethodInfo, "_load_data", prof_method_load, 1);
550
- }
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_allocation.h"
5
+ #include "rp_call_trees.h"
6
+ #include "rp_method.h"
7
+ #include "rp_profile.h"
8
+
9
+ #include <ruby/version.h>
10
+
11
+ // Needed for Ruby 3.0.* and 3.1.*
12
+ #if RUBY_API_VERSION_MAJOR == 3 && RUBY_API_VERSION_MINOR < 2
13
+ VALUE rb_class_attached_object(VALUE klass)
14
+ {
15
+ return rb_iv_get(klass, "__attached__");
16
+ }
17
+ #endif
18
+
19
+ VALUE cRpMethodInfo;
20
+
21
+ /* ================ Helper Functions =================*/
22
+ VALUE resolve_klass(VALUE klass, unsigned int* klass_flags)
23
+ {
24
+ VALUE result = klass;
25
+
26
+ if (klass == 0 || klass == Qnil)
27
+ {
28
+ result = Qnil;
29
+ }
30
+ else if (BUILTIN_TYPE(klass) == T_CLASS && FL_TEST(klass, FL_SINGLETON))
31
+ {
32
+ /* We have come across a singleton object. First
33
+ figure out what it is attached to.*/
34
+ VALUE attached = rb_class_attached_object(klass);
35
+
36
+ switch (BUILTIN_TYPE(attached))
37
+ {
38
+ /* Is this a singleton class acting as a metaclass? */
39
+ case T_CLASS:
40
+ {
41
+ *klass_flags |= kClassSingleton;
42
+ result = attached;
43
+ break;
44
+ }
45
+ /* Is this for singleton methods on a module? */
46
+ case T_MODULE:
47
+ {
48
+ *klass_flags |= kModuleSingleton;
49
+ result = attached;
50
+ break;
51
+ }
52
+ /* Is this for singleton methods on an object? */
53
+ case T_OBJECT:
54
+ {
55
+ *klass_flags |= kObjectSingleton;
56
+ result = rb_class_superclass(klass);
57
+ break;
58
+ }
59
+ /* Ok, this could be other things like an array put onto
60
+ a singleton object (yeah, it happens, see the singleton
61
+ objects test case). */
62
+ default:
63
+ {
64
+ *klass_flags |= kOtherSingleton;
65
+ result = klass;
66
+ break;
67
+ }
68
+ }
69
+ }
70
+ /* Is this an include for a module? If so get the actual
71
+ module class since we want to combine all profiling
72
+ results for that module. */
73
+ else if (BUILTIN_TYPE(klass) == T_ICLASS)
74
+ {
75
+ unsigned int dummy;
76
+ *klass_flags |= kModuleIncludee;
77
+ result = resolve_klass(RBASIC_CLASS(klass), &dummy);
78
+ }
79
+ return result;
80
+ }
81
+
82
+ VALUE resolve_klass_name(VALUE klass, unsigned int* klass_flags)
83
+ {
84
+ VALUE result = Qnil;
85
+
86
+ if (klass == Qnil)
87
+ {
88
+ result = rb_str_new2("[global]");
89
+ }
90
+ else if (*klass_flags & kOtherSingleton)
91
+ {
92
+ result = rb_any_to_s(klass);
93
+ }
94
+ else
95
+ {
96
+ result = rb_class_name(klass);
97
+ }
98
+
99
+ return result;
100
+ }
101
+
102
+ st_data_t method_key(VALUE klass, VALUE msym)
103
+ {
104
+ unsigned int klass_flags = 0;
105
+ VALUE resolved_klass = resolve_klass(klass, &klass_flags);
106
+
107
+ st_data_t hash = rb_hash_start(0);
108
+ hash = rb_hash_uint(hash, resolved_klass);
109
+ hash = rb_hash_uint(hash, msym);
110
+ hash = rb_hash_end(hash);
111
+
112
+ return hash;
113
+ }
114
+
115
+ /* ================ prof_method_t =================*/
116
+ prof_method_t* prof_get_method(VALUE self)
117
+ {
118
+ /* Can't use Data_Get_Struct because that triggers the event hook
119
+ ending up in endless recursion. */
120
+ prof_method_t* result = RTYPEDDATA_DATA(self);
121
+
122
+ if (!result)
123
+ rb_raise(rb_eRuntimeError, "This RubyProf::MethodInfo instance has already been freed, likely because its profile has been freed.");
124
+
125
+ return result;
126
+ }
127
+
128
+ prof_method_t* prof_method_create(struct prof_profile_t* profile, VALUE klass, VALUE msym, VALUE source_file, int source_line)
129
+ {
130
+ prof_method_t* result = ALLOC(prof_method_t);
131
+ result->profile = profile;
132
+
133
+ result->key = method_key(klass, msym);
134
+ result->klass_flags = 0;
135
+
136
+ /* Note we do not call resolve_klass_name now because that causes an object allocation that shows up
137
+ in the allocation results so we want to avoid it until after the profile run is complete. */
138
+ result->klass = resolve_klass(klass, &result->klass_flags);
139
+ result->klass_name = Qnil;
140
+ result->method_name = msym;
141
+ result->measurement = prof_measurement_create();
142
+
143
+ result->call_trees = prof_call_trees_create();
144
+ result->allocations_table = prof_allocations_create();
145
+
146
+ result->visits = 0;
147
+ result->recursive = false;
148
+
149
+ result->object = Qnil;
150
+
151
+ result->source_file = source_file;
152
+ result->source_line = source_line;
153
+
154
+ return result;
155
+ }
156
+
157
+ prof_method_t* prof_method_copy(prof_method_t* other)
158
+ {
159
+ prof_method_t* result = prof_method_create(other->profile, other->klass, other->method_name, other->source_file, other->source_line);
160
+ result->measurement = prof_measurement_copy(other->measurement);
161
+
162
+ return result;
163
+ }
164
+
165
+ /* The underlying c structures are freed when the parent profile is freed.
166
+ However, on shutdown the Ruby GC frees objects in any will-nilly order.
167
+ That means the ruby thread object wrapping the c thread struct may
168
+ be freed before the parent profile. Thus we add in a free function
169
+ for the garbage collector so that if it does get called will nil
170
+ out our Ruby object reference.*/
171
+ static void prof_method_ruby_gc_free(void* data)
172
+ {
173
+ if (data)
174
+ {
175
+ prof_method_t* method = (prof_method_t*)data;
176
+ method->object = Qnil;
177
+ }
178
+ }
179
+
180
+ static void prof_method_free(prof_method_t* method)
181
+ {
182
+ /* Has this method object been accessed by Ruby? If
183
+ yes clean it up so to avoid a segmentation fault. */
184
+ if (method->object != Qnil)
185
+ {
186
+ RTYPEDDATA(method->object)->data = NULL;
187
+ method->object = Qnil;
188
+ }
189
+
190
+ prof_allocations_free(method->allocations_table);
191
+ prof_call_trees_free(method->call_trees);
192
+ prof_measurement_free(method->measurement);
193
+ xfree(method);
194
+ }
195
+
196
+ size_t prof_method_size(const void* data)
197
+ {
198
+ return sizeof(prof_method_t);
199
+ }
200
+
201
+ void prof_method_mark(void* data)
202
+ {
203
+ if (!data) return;
204
+
205
+ prof_method_t* method = (prof_method_t*)data;
206
+
207
+ if (method->object != Qnil)
208
+ rb_gc_mark_movable(method->object);
209
+
210
+ // Mark the profile to keep it alive. Can't call prof_profile_mark because that would
211
+ // cause recursion
212
+ if (method->profile && method->profile->object != Qnil)
213
+ rb_gc_mark(method->profile->object);
214
+
215
+ rb_gc_mark(method->klass_name);
216
+ rb_gc_mark(method->method_name);
217
+ rb_gc_mark(method->source_file);
218
+
219
+ if (method->klass != Qnil)
220
+ rb_gc_mark(method->klass);
221
+
222
+ prof_measurement_mark(method->measurement);
223
+ prof_allocations_mark(method->allocations_table);
224
+ }
225
+
226
+ void prof_method_compact(void* data)
227
+ {
228
+ prof_method_t* method = (prof_method_t*)data;
229
+ method->object = rb_gc_location(method->object);
230
+ method->klass_name = rb_gc_location(method->klass_name);
231
+ method->method_name = rb_gc_location(method->method_name);
232
+ }
233
+
234
+ static VALUE prof_method_allocate(VALUE klass)
235
+ {
236
+ prof_method_t* method_data = prof_method_create(NULL, Qnil, Qnil, Qnil, 0);
237
+ method_data->object = prof_method_wrap(method_data);
238
+ return method_data->object;
239
+ }
240
+
241
+ static const rb_data_type_t method_info_type =
242
+ {
243
+ .wrap_struct_name = "MethodInfo",
244
+ .function =
245
+ {
246
+ .dmark = prof_method_mark,
247
+ .dfree = prof_method_ruby_gc_free,
248
+ .dsize = prof_method_size,
249
+ .dcompact = prof_method_compact
250
+ },
251
+ .data = NULL,
252
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY
253
+ };
254
+
255
+ VALUE prof_method_wrap(prof_method_t* method)
256
+ {
257
+ if (method->object == Qnil)
258
+ {
259
+ method->object = TypedData_Wrap_Struct(cRpMethodInfo, &method_info_type, method);
260
+ }
261
+ return method->object;
262
+ }
263
+
264
+ st_table* method_table_create(void)
265
+ {
266
+ return rb_st_init_numtable();
267
+ }
268
+
269
+ static int method_table_free_iterator(st_data_t key, st_data_t value, st_data_t dummy)
270
+ {
271
+ prof_method_free((prof_method_t*)value);
272
+ return ST_CONTINUE;
273
+ }
274
+
275
+ void method_table_free(st_table* table)
276
+ {
277
+ rb_st_foreach(table, method_table_free_iterator, 0);
278
+ rb_st_free_table(table);
279
+ }
280
+
281
+ size_t method_table_insert(st_table* table, st_data_t key, prof_method_t* val)
282
+ {
283
+ return rb_st_insert(table, (st_data_t)key, (st_data_t)val);
284
+ }
285
+
286
+ static int prof_method_table_merge_internal(st_data_t key, st_data_t value, st_data_t data)
287
+ {
288
+ st_table* self_table = (st_table*)data;
289
+ prof_method_t* other_child = (prof_method_t*)value;
290
+
291
+ prof_method_t* self_child = method_table_lookup(self_table, other_child->key);
292
+ if (self_child)
293
+ {
294
+ prof_measurement_merge_internal(self_child->measurement, other_child->measurement);
295
+ }
296
+ else
297
+ {
298
+ prof_method_t* copy = prof_method_copy(other_child);
299
+ method_table_insert(self_table, copy->key, copy);
300
+ }
301
+
302
+ return ST_CONTINUE;
303
+ }
304
+
305
+ void prof_method_table_merge(st_table* self, st_table* other)
306
+ {
307
+ rb_st_foreach(other, prof_method_table_merge_internal, (st_data_t)self);
308
+ }
309
+
310
+ prof_method_t* method_table_lookup(st_table* table, st_data_t key)
311
+ {
312
+ st_data_t val;
313
+ if (rb_st_lookup(table, (st_data_t)key, &val))
314
+ {
315
+ return (prof_method_t*)val;
316
+ }
317
+ else
318
+ {
319
+ return NULL;
320
+ }
321
+ }
322
+
323
+ /* ================ Method Info =================*/
324
+ /* Document-class: RubyProf::MethodInfo
325
+ The RubyProf::MethodInfo class stores profiling data for a method.
326
+ One instance of the RubyProf::MethodInfo class is created per method
327
+ called per thread. Thus, if a method is called in two different
328
+ thread then there will be two RubyProf::MethodInfo objects
329
+ created. RubyProf::MethodInfo objects can be accessed via
330
+ the RubyProf::Profile object.
331
+ */
332
+
333
+ /* call-seq:
334
+ new(klass, method_name) -> method_info
335
+
336
+ Creates a new MethodInfo instance. +Klass+ should be a reference to
337
+ a Ruby class and +method_name+ a symbol identifying one of its instance methods.*/
338
+ static VALUE prof_method_initialize(VALUE self, VALUE klass, VALUE method_name)
339
+ {
340
+ prof_method_t* method_ptr = prof_get_method(self);
341
+ method_ptr->klass = klass;
342
+ method_ptr->method_name = method_name;
343
+
344
+ // Setup method key
345
+ method_ptr->key = method_key(klass, method_name);
346
+
347
+ // Get method object
348
+ VALUE ruby_method = rb_funcall(klass, rb_intern("instance_method"), 1, method_name);
349
+
350
+ // Get source file and line number
351
+ VALUE location_array = rb_funcall(ruby_method, rb_intern("source_location"), 0);
352
+ if (location_array != Qnil && RARRAY_LEN(location_array) == 2)
353
+ {
354
+ method_ptr->source_file = rb_ary_entry(location_array, 0);
355
+ method_ptr->source_line = NUM2INT(rb_ary_entry(location_array, 1));
356
+ }
357
+
358
+ return self;
359
+ }
360
+
361
+ /* call-seq:
362
+ hash -> hash
363
+
364
+ Returns the hash key for this method info. The hash key is calculated based on the
365
+ klass name and method name */
366
+ static VALUE prof_method_hash(VALUE self)
367
+ {
368
+ prof_method_t* method_ptr = prof_get_method(self);
369
+ return ULL2NUM(method_ptr->key);
370
+ }
371
+
372
+ /* call-seq:
373
+ allocations -> array
374
+
375
+ Returns an array of allocation information.*/
376
+ static VALUE prof_method_allocations(VALUE self)
377
+ {
378
+ prof_method_t* method = prof_get_method(self);
379
+ return prof_allocations_wrap(method->allocations_table);
380
+ }
381
+
382
+ /* call-seq:
383
+ called -> Measurement
384
+
385
+ Returns the measurement associated with this method. */
386
+ static VALUE prof_method_measurement(VALUE self)
387
+ {
388
+ prof_method_t* method = prof_get_method(self);
389
+ return prof_measurement_wrap(method->measurement);
390
+ }
391
+
392
+ /* call-seq:
393
+ source_file => string
394
+
395
+ Returns the source file of the method
396
+ */
397
+ static VALUE prof_method_source_file(VALUE self)
398
+ {
399
+ prof_method_t* method = prof_get_method(self);
400
+ return method->source_file;
401
+ }
402
+
403
+ /* call-seq:
404
+ line_no -> int
405
+
406
+ returns the line number of the method */
407
+ static VALUE prof_method_line(VALUE self)
408
+ {
409
+ prof_method_t* method = prof_get_method(self);
410
+ return INT2FIX(method->source_line);
411
+ }
412
+
413
+ /* call-seq:
414
+ klass_name -> String
415
+
416
+ Returns the name of this method's class. Singleton classes
417
+ will have the form <Object::Object>. */
418
+
419
+ static VALUE prof_method_klass_name(VALUE self)
420
+ {
421
+ prof_method_t* method = prof_get_method(self);
422
+ if (method->klass_name == Qnil)
423
+ method->klass_name = resolve_klass_name(method->klass, &method->klass_flags);
424
+
425
+ return method->klass_name;
426
+ }
427
+
428
+ /* call-seq:
429
+ klass_flags -> integer
430
+
431
+ Returns the klass flags */
432
+
433
+ static VALUE prof_method_klass_flags(VALUE self)
434
+ {
435
+ prof_method_t* method = prof_get_method(self);
436
+ return INT2FIX(method->klass_flags);
437
+ }
438
+
439
+ /* call-seq:
440
+ method_name -> Symbol
441
+
442
+ Returns the name of this method in the format Object#method. Singletons
443
+ methods will be returned in the format <Object::Object>#method.*/
444
+
445
+ static VALUE prof_method_name(VALUE self)
446
+ {
447
+ prof_method_t* method = prof_get_method(self);
448
+ return method->method_name;
449
+ }
450
+
451
+ /* call-seq:
452
+ recursive? -> boolean
453
+
454
+ Returns the true if this method is recursively invoked */
455
+ static VALUE prof_method_recursive(VALUE self)
456
+ {
457
+ prof_method_t* method = prof_get_method(self);
458
+ return method->recursive ? Qtrue : Qfalse;
459
+ }
460
+
461
+ /* call-seq:
462
+ call_trees -> CallTrees
463
+
464
+ Returns the CallTrees associated with this method. */
465
+ static VALUE prof_method_call_trees(VALUE self)
466
+ {
467
+ prof_method_t* method = prof_get_method(self);
468
+ return prof_call_trees_wrap(method->call_trees);
469
+ }
470
+
471
+ /* :nodoc: */
472
+ static VALUE prof_method_dump(VALUE self)
473
+ {
474
+ prof_method_t* method_data = prof_get_method(self);
475
+ VALUE result = rb_hash_new();
476
+
477
+ rb_hash_aset(result, ID2SYM(rb_intern("klass_name")), prof_method_klass_name(self));
478
+ rb_hash_aset(result, ID2SYM(rb_intern("klass_flags")), INT2FIX(method_data->klass_flags));
479
+ rb_hash_aset(result, ID2SYM(rb_intern("method_name")), method_data->method_name);
480
+
481
+ rb_hash_aset(result, ID2SYM(rb_intern("key")), ULL2NUM(method_data->key));
482
+ rb_hash_aset(result, ID2SYM(rb_intern("recursive")), prof_method_recursive(self));
483
+ rb_hash_aset(result, ID2SYM(rb_intern("source_file")), method_data->source_file);
484
+ rb_hash_aset(result, ID2SYM(rb_intern("source_line")), INT2FIX(method_data->source_line));
485
+
486
+ rb_hash_aset(result, ID2SYM(rb_intern("call_trees")), prof_call_trees_wrap(method_data->call_trees));
487
+ rb_hash_aset(result, ID2SYM(rb_intern("measurement")), prof_measurement_wrap(method_data->measurement));
488
+ rb_hash_aset(result, ID2SYM(rb_intern("allocations")), prof_method_allocations(self));
489
+
490
+ return result;
491
+ }
492
+
493
+ /* :nodoc: */
494
+ static VALUE prof_method_load(VALUE self, VALUE data)
495
+ {
496
+ prof_method_t* method_data = prof_get_method(self);
497
+ method_data->object = self;
498
+
499
+ method_data->klass_name = rb_hash_aref(data, ID2SYM(rb_intern("klass_name")));
500
+ method_data->klass_flags = FIX2INT(rb_hash_aref(data, ID2SYM(rb_intern("klass_flags"))));
501
+ method_data->method_name = rb_hash_aref(data, ID2SYM(rb_intern("method_name")));
502
+ method_data->key = RB_NUM2ULL(rb_hash_aref(data, ID2SYM(rb_intern("key"))));
503
+
504
+ method_data->recursive = rb_hash_aref(data, ID2SYM(rb_intern("recursive"))) == Qtrue ? true : false;
505
+
506
+ method_data->source_file = rb_hash_aref(data, ID2SYM(rb_intern("source_file")));
507
+ method_data->source_line = FIX2INT(rb_hash_aref(data, ID2SYM(rb_intern("source_line"))));
508
+
509
+ VALUE call_trees = rb_hash_aref(data, ID2SYM(rb_intern("call_trees")));
510
+ method_data->call_trees = prof_get_call_trees(call_trees);
511
+
512
+ VALUE measurement = rb_hash_aref(data, ID2SYM(rb_intern("measurement")));
513
+ method_data->measurement = prof_get_measurement(measurement);
514
+
515
+ VALUE allocations = rb_hash_aref(data, ID2SYM(rb_intern("allocations")));
516
+ prof_allocations_unwrap(method_data->allocations_table, allocations);
517
+ return data;
518
+ }
519
+
520
+ void rp_init_method_info(void)
521
+ {
522
+ /* MethodInfo */
523
+ cRpMethodInfo = rb_define_class_under(mProf, "MethodInfo", rb_cObject);
524
+
525
+ rb_define_const(cRpMethodInfo, "MODULE_INCLUDEE", INT2NUM(kModuleIncludee));
526
+ rb_define_const(cRpMethodInfo, "CLASS_SINGLETON", INT2NUM(kClassSingleton));
527
+ rb_define_const(cRpMethodInfo, "MODULE_SINGLETON", INT2NUM(kModuleSingleton));
528
+ rb_define_const(cRpMethodInfo, "OBJECT_SINGLETON", INT2NUM(kObjectSingleton));
529
+ rb_define_const(cRpMethodInfo, "OTHER_SINGLETON", INT2NUM(kOtherSingleton));
530
+
531
+ rb_define_alloc_func(cRpMethodInfo, prof_method_allocate);
532
+ rb_define_method(cRpMethodInfo, "initialize", prof_method_initialize, 2);
533
+ rb_define_method(cRpMethodInfo, "hash", prof_method_hash, 0);
534
+
535
+ rb_define_method(cRpMethodInfo, "klass_name", prof_method_klass_name, 0);
536
+ rb_define_method(cRpMethodInfo, "klass_flags", prof_method_klass_flags, 0);
537
+ rb_define_method(cRpMethodInfo, "method_name", prof_method_name, 0);
538
+
539
+ rb_define_method(cRpMethodInfo, "call_trees", prof_method_call_trees, 0);
540
+
541
+ rb_define_method(cRpMethodInfo, "allocations", prof_method_allocations, 0);
542
+ rb_define_method(cRpMethodInfo, "measurement", prof_method_measurement, 0);
543
+
544
+ rb_define_method(cRpMethodInfo, "source_file", prof_method_source_file, 0);
545
+ rb_define_method(cRpMethodInfo, "line", prof_method_line, 0);
546
+
547
+ rb_define_method(cRpMethodInfo, "recursive?", prof_method_recursive, 0);
548
+
549
+ rb_define_method(cRpMethodInfo, "_dump_data", prof_method_dump, 0);
550
+ rb_define_method(cRpMethodInfo, "_load_data", prof_method_load, 1);
551
+ }