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
@@ -1,551 +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
- #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
- }
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
+ }