ruby-prof 0.13.1 → 1.4.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 (209) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGES +579 -371
  3. data/LICENSE +24 -23
  4. data/README.rdoc +5 -433
  5. data/Rakefile +98 -110
  6. data/bin/ruby-prof +328 -329
  7. data/bin/ruby-prof-check-trace +45 -0
  8. data/ext/ruby_prof/extconf.rb +16 -59
  9. data/ext/ruby_prof/rp_aggregate_call_tree.c +59 -0
  10. data/ext/ruby_prof/rp_aggregate_call_tree.h +13 -0
  11. data/ext/ruby_prof/rp_allocation.c +287 -0
  12. data/ext/ruby_prof/rp_allocation.h +31 -0
  13. data/ext/ruby_prof/rp_call_tree.c +369 -0
  14. data/ext/ruby_prof/rp_call_tree.h +43 -0
  15. data/ext/ruby_prof/rp_call_trees.c +288 -0
  16. data/ext/ruby_prof/rp_call_trees.h +28 -0
  17. data/ext/ruby_prof/rp_measure_allocations.c +50 -65
  18. data/ext/ruby_prof/rp_measure_memory.c +42 -73
  19. data/ext/ruby_prof/rp_measure_process_time.c +65 -71
  20. data/ext/ruby_prof/rp_measure_wall_time.c +64 -42
  21. data/ext/ruby_prof/rp_measurement.c +237 -0
  22. data/ext/ruby_prof/rp_measurement.h +50 -0
  23. data/ext/ruby_prof/rp_method.c +491 -420
  24. data/ext/ruby_prof/rp_method.h +62 -57
  25. data/ext/ruby_prof/rp_profile.c +908 -0
  26. data/ext/ruby_prof/rp_profile.h +35 -0
  27. data/ext/ruby_prof/rp_stack.c +212 -128
  28. data/ext/ruby_prof/rp_stack.h +53 -51
  29. data/ext/ruby_prof/rp_thread.c +362 -268
  30. data/ext/ruby_prof/rp_thread.h +39 -27
  31. data/ext/ruby_prof/ruby_prof.c +52 -695
  32. data/ext/ruby_prof/ruby_prof.h +26 -55
  33. data/ext/ruby_prof/vc/ruby_prof.sln +28 -21
  34. data/ext/ruby_prof/vc/{ruby_prof_20.vcxproj → ruby_prof.vcxproj} +56 -8
  35. data/lib/ruby-prof.rb +52 -67
  36. data/lib/ruby-prof/assets/call_stack_printer.html.erb +710 -0
  37. data/lib/ruby-prof/assets/call_stack_printer.png +0 -0
  38. data/lib/ruby-prof/assets/graph_printer.html.erb +355 -0
  39. data/lib/ruby-prof/call_tree.rb +57 -0
  40. data/lib/ruby-prof/call_tree_visitor.rb +36 -0
  41. data/lib/ruby-prof/compatibility.rb +99 -169
  42. data/lib/ruby-prof/exclude_common_methods.rb +198 -0
  43. data/lib/ruby-prof/measurement.rb +17 -0
  44. data/lib/ruby-prof/method_info.rb +78 -131
  45. data/lib/ruby-prof/printers/abstract_printer.rb +137 -85
  46. data/lib/ruby-prof/printers/call_info_printer.rb +53 -41
  47. data/lib/ruby-prof/printers/call_stack_printer.rb +180 -773
  48. data/lib/ruby-prof/printers/call_tree_printer.rb +151 -92
  49. data/lib/ruby-prof/printers/dot_printer.rb +132 -132
  50. data/lib/ruby-prof/printers/flat_printer.rb +53 -69
  51. data/lib/ruby-prof/printers/graph_html_printer.rb +63 -255
  52. data/lib/ruby-prof/printers/graph_printer.rb +113 -116
  53. data/lib/ruby-prof/printers/multi_printer.rb +127 -56
  54. data/lib/ruby-prof/profile.rb +37 -77
  55. data/lib/ruby-prof/rack.rb +62 -15
  56. data/lib/ruby-prof/task.rb +147 -147
  57. data/lib/ruby-prof/thread.rb +10 -12
  58. data/lib/ruby-prof/version.rb +3 -0
  59. data/lib/unprof.rb +10 -10
  60. data/ruby-prof.gemspec +65 -61
  61. data/test/abstract_printer_test.rb +26 -0
  62. data/test/alias_test.rb +126 -0
  63. data/test/basic_test.rb +43 -128
  64. data/test/call_tree_visitor_test.rb +32 -0
  65. data/test/call_trees_test.rb +66 -0
  66. data/test/duplicate_names_test.rb +32 -32
  67. data/test/dynamic_method_test.rb +53 -74
  68. data/test/enumerable_test.rb +21 -16
  69. data/test/exceptions_test.rb +24 -16
  70. data/test/exclude_methods_test.rb +151 -0
  71. data/test/exclude_threads_test.rb +53 -54
  72. data/test/fiber_test.rb +129 -65
  73. data/test/gc_test.rb +90 -0
  74. data/test/inverse_call_tree_test.rb +175 -0
  75. data/test/line_number_test.rb +158 -71
  76. data/test/marshal_test.rb +113 -0
  77. data/test/measure_allocations.rb +30 -0
  78. data/test/measure_allocations_test.rb +375 -25
  79. data/test/measure_allocations_trace_test.rb +375 -0
  80. data/test/measure_memory_trace_test.rb +1101 -0
  81. data/test/measure_process_time_test.rb +785 -62
  82. data/test/measure_times.rb +56 -0
  83. data/test/measure_wall_time_test.rb +434 -254
  84. data/test/multi_printer_test.rb +71 -82
  85. data/test/no_method_class_test.rb +15 -15
  86. data/test/pause_resume_test.rb +175 -166
  87. data/test/prime.rb +54 -54
  88. data/test/prime_script.rb +6 -0
  89. data/test/printer_call_stack_test.rb +27 -0
  90. data/test/printer_call_tree_test.rb +30 -0
  91. data/test/printer_flat_test.rb +99 -0
  92. data/test/printer_graph_html_test.rb +59 -0
  93. data/test/printer_graph_test.rb +40 -0
  94. data/test/printers_test.rb +141 -257
  95. data/test/printing_recursive_graph_test.rb +81 -0
  96. data/test/profile_test.rb +16 -0
  97. data/test/rack_test.rb +93 -0
  98. data/test/recursive_test.rb +206 -215
  99. data/test/singleton_test.rb +38 -38
  100. data/test/stack_printer_test.rb +64 -78
  101. data/test/start_stop_test.rb +109 -112
  102. data/test/test_helper.rb +13 -115
  103. data/test/thread_test.rb +144 -178
  104. data/test/unique_call_path_test.rb +120 -224
  105. data/test/yarv_test.rb +56 -0
  106. metadata +77 -133
  107. data/doc/LICENSE.html +0 -155
  108. data/doc/README_rdoc.html +0 -648
  109. data/doc/Rack.html +0 -167
  110. data/doc/Rack/RubyProf.html +0 -319
  111. data/doc/RubyProf.html +0 -1000
  112. data/doc/RubyProf/AbstractPrinter.html +0 -580
  113. data/doc/RubyProf/AggregateCallInfo.html +0 -570
  114. data/doc/RubyProf/CallInfo.html +0 -512
  115. data/doc/RubyProf/CallInfoPrinter.html +0 -190
  116. data/doc/RubyProf/CallInfoVisitor.html +0 -332
  117. data/doc/RubyProf/CallStackPrinter.html +0 -1600
  118. data/doc/RubyProf/CallTreePrinter.html +0 -413
  119. data/doc/RubyProf/Cmd.html +0 -669
  120. data/doc/RubyProf/DotPrinter.html +0 -312
  121. data/doc/RubyProf/FlatPrinter.html +0 -229
  122. data/doc/RubyProf/FlatPrinterWithLineNumbers.html +0 -267
  123. data/doc/RubyProf/GraphHtmlPrinter.html +0 -630
  124. data/doc/RubyProf/GraphPrinter.html +0 -209
  125. data/doc/RubyProf/MethodInfo.html +0 -713
  126. data/doc/RubyProf/MultiPrinter.html +0 -407
  127. data/doc/RubyProf/Profile.html +0 -821
  128. data/doc/RubyProf/ProfileTask.html +0 -532
  129. data/doc/RubyProf/Test.html +0 -578
  130. data/doc/RubyProf/Thread.html +0 -262
  131. data/doc/created.rid +0 -32
  132. data/doc/examples/flat_txt.html +0 -191
  133. data/doc/examples/graph_txt.html +0 -305
  134. data/doc/images/add.png +0 -0
  135. data/doc/images/brick.png +0 -0
  136. data/doc/images/brick_link.png +0 -0
  137. data/doc/images/bug.png +0 -0
  138. data/doc/images/bullet_black.png +0 -0
  139. data/doc/images/bullet_toggle_minus.png +0 -0
  140. data/doc/images/bullet_toggle_plus.png +0 -0
  141. data/doc/images/date.png +0 -0
  142. data/doc/images/delete.png +0 -0
  143. data/doc/images/find.png +0 -0
  144. data/doc/images/loadingAnimation.gif +0 -0
  145. data/doc/images/macFFBgHack.png +0 -0
  146. data/doc/images/package.png +0 -0
  147. data/doc/images/page_green.png +0 -0
  148. data/doc/images/page_white_text.png +0 -0
  149. data/doc/images/page_white_width.png +0 -0
  150. data/doc/images/plugin.png +0 -0
  151. data/doc/images/ruby.png +0 -0
  152. data/doc/images/tag_blue.png +0 -0
  153. data/doc/images/tag_green.png +0 -0
  154. data/doc/images/transparent.png +0 -0
  155. data/doc/images/wrench.png +0 -0
  156. data/doc/images/wrench_orange.png +0 -0
  157. data/doc/images/zoom.png +0 -0
  158. data/doc/index.html +0 -647
  159. data/doc/js/darkfish.js +0 -155
  160. data/doc/js/jquery.js +0 -18
  161. data/doc/js/navigation.js +0 -142
  162. data/doc/js/search.js +0 -94
  163. data/doc/js/search_index.js +0 -1
  164. data/doc/js/searcher.js +0 -228
  165. data/doc/rdoc.css +0 -543
  166. data/doc/table_of_contents.html +0 -462
  167. data/examples/empty.png +0 -0
  168. data/examples/flat.txt +0 -55
  169. data/examples/graph.dot +0 -106
  170. data/examples/graph.html +0 -823
  171. data/examples/graph.png +0 -0
  172. data/examples/graph.txt +0 -170
  173. data/examples/minus.png +0 -0
  174. data/examples/multi.flat.txt +0 -23
  175. data/examples/multi.graph.html +0 -906
  176. data/examples/multi.grind.dat +0 -194
  177. data/examples/multi.stack.html +0 -573
  178. data/examples/plus.png +0 -0
  179. data/examples/stack.html +0 -573
  180. data/ext/ruby_prof/rp_call_info.c +0 -407
  181. data/ext/ruby_prof/rp_call_info.h +0 -48
  182. data/ext/ruby_prof/rp_measure.c +0 -48
  183. data/ext/ruby_prof/rp_measure.h +0 -45
  184. data/ext/ruby_prof/rp_measure_cpu_time.c +0 -112
  185. data/ext/ruby_prof/rp_measure_gc_runs.c +0 -65
  186. data/ext/ruby_prof/rp_measure_gc_time.c +0 -57
  187. data/ext/ruby_prof/vc/ruby_prof_18.vcxproj +0 -108
  188. data/ext/ruby_prof/vc/ruby_prof_19.vcxproj +0 -110
  189. data/ext/ruby_prof/version.h +0 -7
  190. data/lib/ruby-prof/aggregate_call_info.rb +0 -72
  191. data/lib/ruby-prof/call_info.rb +0 -89
  192. data/lib/ruby-prof/call_info_visitor.rb +0 -44
  193. data/lib/ruby-prof/images/empty.png +0 -0
  194. data/lib/ruby-prof/images/minus.png +0 -0
  195. data/lib/ruby-prof/images/plus.png +0 -0
  196. data/lib/ruby-prof/printers/flat_printer_with_line_numbers.rb +0 -57
  197. data/lib/ruby-prof/test.rb +0 -150
  198. data/test/aggregate_test.rb +0 -136
  199. data/test/call_info_test.rb +0 -78
  200. data/test/call_info_visitor_test.rb +0 -31
  201. data/test/exec_test.rb +0 -14
  202. data/test/measure_cpu_time_test.rb +0 -220
  203. data/test/measure_gc_runs_test.rb +0 -32
  204. data/test/measure_gc_time_test.rb +0 -36
  205. data/test/measure_memory_test.rb +0 -31
  206. data/test/method_elimination_test.rb +0 -84
  207. data/test/module_test.rb +0 -45
  208. data/test/stack_test.rb +0 -138
  209. data/test/test_suite.rb +0 -37
@@ -0,0 +1,45 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ stacks = Hash.new{|h,k| h[k] = Hash.new{|h,k| h[k] = []}}
4
+ i = 0
5
+ File.open(ARGV[0]).each_line do |l|
6
+ i += 1
7
+ unless l =~ /^(\d+):(\d+): *\d+ms *([^ ]+) *(.*): *(\d+) *(.+)$/
8
+ next if l =~/^ *$/
9
+ puts "line doesn't match: #{l}"
10
+ next
11
+ end
12
+ details = $1.to_i, $2.to_i, $3, $4, $5.to_i, $6
13
+ thread, fiber, event, file, line, method = *details
14
+ # puts method
15
+ stack = stacks[thread][fiber]
16
+ case event
17
+ when 'call', 'c-call'
18
+ stack << method
19
+ when 'return', 'c-return'
20
+ last_method = stack.pop
21
+ if last_method != method
22
+ puts "LINE #{i}: return event without call: #{method}"
23
+ puts "STACK: #{stack.inspect}"
24
+ if stack.find(method)
25
+ puts "fixing stack"
26
+ while (popped = stack.pop) && (popped != method)
27
+ puts "popped #{popped}"
28
+ end
29
+ else
30
+ raise "stack unfixable"
31
+ end
32
+ # stack << last_method
33
+ end
34
+ when 'line'
35
+ last_method = stack[-1]
36
+ if last_method != method
37
+ unless stack.find(method)
38
+ raise "LINE #{i}: line event without call: #{method}"
39
+ end
40
+ end
41
+ else
42
+ puts "unkown event"
43
+ end
44
+ end
45
+ puts stacks.inspect
@@ -1,59 +1,16 @@
1
- require "mkmf"
2
-
3
- if RUBY_VERSION == "1.8.6"
4
- STDERR.print("Ruby #{RUBY_VERSION} is no longer supported. Please upgrade to 1.8.7 or 1.9.2 or higher\n")
5
- exit(1)
6
- end
7
-
8
- if RUBY_VERSION == "1.9.0" or RUBY_VERSION == "1.9.1"
9
- STDERR.print("Ruby #{RUBY_VERSION} is no longer supported. Please upgrade to 1.9.2 or higher\n")
10
- exit(1)
11
- end
12
-
13
- have_header("sys/times.h")
14
-
15
- # Stefan Kaes / Alexander Dymo GC patch
16
- have_func("rb_os_allocated_objects")
17
- have_func("rb_gc_allocated_size")
18
- have_func("rb_gc_collections")
19
- have_func("rb_gc_time")
20
-
21
- # 1.9.3 superclass
22
- have_func("rb_class_superclass")
23
-
24
- # Lloyd Hilaiel's heap info patch
25
- have_func("rb_heap_total_mem")
26
- have_func("rb_gc_heap_info")
27
-
28
- # whether our ruby has fibers
29
- have_func("rb_fiber_current")
30
-
31
- def add_define(name, value = nil)
32
- if value
33
- $defs.push("-D#{name}=#{value}")
34
- else
35
- $defs.push("-D#{name}")
36
- end
37
- end
38
-
39
- # if have_func("rb_gc_enable_stats")
40
- # add_define("TOGGLE_GC_STATS", 1)
41
- # end
42
-
43
- require 'rubygems'
44
- unless Gem.win_platform? || RUBY_PLATFORM =~ /(darwin|openbsd)/
45
- $LDFLAGS += " -lrt" # for clock_gettime
46
- end
47
- add_define("RUBY_VERSION", RUBY_VERSION.gsub('.', ''))
48
-
49
- # for ruby 1.9, determine whether threads inherit trace flags (latest 1.9.2 works correctly)
50
- if RUBY_VERSION > "1.9"
51
- require 'set'
52
- threads = Set.new
53
- set_trace_func lambda { |*args| threads << Thread.current.object_id }
54
- Thread.new{1}.join
55
- set_trace_func nil
56
- add_define("THREADS_INHERIT_EVENT_FLAGS", (threads.size == 2) ? "1" : "0")
57
- end
58
-
59
- create_makefile("ruby_prof")
1
+ require "mkmf"
2
+
3
+ # This function was added in Ruby 2.5, so once Ruby 2.4 is no longer supported this can be removed
4
+ have_func('rb_tracearg_callee_id', ["ruby.h"])
5
+
6
+ # We want to intermix declarations and code (ie, don't define all variables at the top of the method)
7
+ unless RUBY_PLATFORM =~ /mswin/
8
+ $CFLAGS += ' -std=c99'
9
+ end
10
+
11
+ # And since we are using C99 we want to disable Ruby sending these warnings to gcc
12
+ if CONFIG['warnflags']
13
+ CONFIG['warnflags'].gsub!('-Wdeclaration-after-statement', '')
14
+ end
15
+
16
+ create_makefile("ruby_prof")
@@ -0,0 +1,59 @@
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_aggregate_call_tree.h"
5
+
6
+ VALUE cRpAggregateCallTree;
7
+
8
+ void prof_aggregate_call_tree_mark(void* data)
9
+ {
10
+ prof_call_tree_t* call_tree = (prof_call_tree_t*)data;
11
+
12
+ if (call_tree->object != Qnil)
13
+ rb_gc_mark(call_tree->object);
14
+
15
+ if (call_tree->source_file != Qnil)
16
+ rb_gc_mark(call_tree->source_file);
17
+
18
+ prof_measurement_mark(call_tree->measurement);
19
+ }
20
+
21
+ static void prof_aggregate_call_tree_ruby_gc_free(void* data)
22
+ {
23
+ prof_call_tree_t* call_tree = (prof_call_tree_t*)data;
24
+ prof_call_tree_free(call_tree);
25
+ }
26
+
27
+ size_t prof_aggregate_call_tree_size(const void* data)
28
+ {
29
+ return sizeof(prof_call_tree_t);
30
+ }
31
+
32
+ static const rb_data_type_t aggregate_call_tree_type =
33
+ {
34
+ .wrap_struct_name = "Aggregate_CallTree",
35
+ .function =
36
+ {
37
+ .dmark = prof_aggregate_call_tree_mark,
38
+ .dfree = prof_aggregate_call_tree_ruby_gc_free,
39
+ .dsize = prof_aggregate_call_tree_size,
40
+ },
41
+ .data = NULL,
42
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY
43
+ };
44
+
45
+ VALUE prof_aggregate_call_tree_wrap(prof_call_tree_t* call_tree)
46
+ {
47
+ if (call_tree->object == Qnil)
48
+ {
49
+ call_tree->object = TypedData_Wrap_Struct(cRpAggregateCallTree, &aggregate_call_tree_type, call_tree);
50
+ }
51
+ return call_tree->object;
52
+ }
53
+
54
+ void rp_init_aggregate_call_tree()
55
+ {
56
+ // AggregateCallTree
57
+ cRpAggregateCallTree = rb_define_class_under(mProf, "AggregateCallTree", cRpCallTree);
58
+ rb_undef_method(CLASS_OF(cRpAggregateCallTree), "new");
59
+ }
@@ -0,0 +1,13 @@
1
+ /* Copyright (C) 2005-2019 Shugo Maeda <shugo@ruby-lang.org> and Charlie Savage <cfis@savagexi.com>
2
+ Please see the LICENSE file for copyright and distribution information */
3
+
4
+ #ifndef __RP_AGGREGATE_CALL_TREE_H__
5
+ #define __RP_AGGREGATE_CALL_TREE_H__
6
+
7
+ #include "ruby_prof.h"
8
+ #include "rp_call_tree.h"
9
+
10
+ void rp_init_aggregate_call_tree(void);
11
+ VALUE prof_aggregate_call_tree_wrap(prof_call_tree_t* call_tree);
12
+
13
+ #endif //__RP_AGGREGATE_CALL_TREE_H__
@@ -0,0 +1,287 @@
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_allocation.h"
5
+
6
+ VALUE cRpAllocation;
7
+
8
+ prof_allocation_t* allocations_table_lookup(st_table* table, st_data_t key)
9
+ {
10
+ prof_allocation_t* result = NULL;
11
+ st_data_t value;
12
+ if (rb_st_lookup(table, key, &value))
13
+ {
14
+ result = (prof_allocation_t*)value;
15
+ }
16
+
17
+ return result;
18
+ }
19
+
20
+ void allocations_table_insert(st_table* table, st_data_t key, prof_allocation_t* allocation)
21
+ {
22
+ rb_st_insert(table, (st_data_t)key, (st_data_t)allocation);
23
+ }
24
+
25
+ st_data_t allocations_key(VALUE klass, int source_line)
26
+ {
27
+ return (klass << 4) + source_line;
28
+ }
29
+
30
+ /* ====== prof_allocation_t ====== */
31
+ prof_allocation_t* prof_allocation_create(void)
32
+ {
33
+ prof_allocation_t* result = ALLOC(prof_allocation_t);
34
+ result->count = 0;
35
+ result->klass = Qnil;
36
+ result->klass_name = Qnil;
37
+ result->object = Qnil;
38
+ result->memory = 0;
39
+ result->source_line = 0;
40
+ result->source_file = Qnil;
41
+ result->key = 0;
42
+
43
+ return result;
44
+ }
45
+
46
+ prof_allocation_t* prof_get_allocation(VALUE self)
47
+ {
48
+ /* Can't use Data_Get_Struct because that triggers the event hook
49
+ ending up in endless recursion. */
50
+ prof_allocation_t* result = RTYPEDDATA_DATA(self);
51
+
52
+ if (!result)
53
+ rb_raise(rb_eRuntimeError, "This RubyProf::Allocation instance has already been freed, likely because its profile has been freed.");
54
+
55
+ return result;
56
+ }
57
+
58
+ prof_allocation_t* prof_allocate_increment(prof_method_t* method, rb_trace_arg_t* trace_arg)
59
+ {
60
+ VALUE object = rb_tracearg_object(trace_arg);
61
+ if (BUILTIN_TYPE(object) == T_IMEMO)
62
+ return NULL;
63
+
64
+ VALUE klass = rb_obj_class(object);
65
+
66
+ int source_line = FIX2INT(rb_tracearg_lineno(trace_arg));
67
+ st_data_t key = allocations_key(klass, source_line);
68
+
69
+ prof_allocation_t* allocation = allocations_table_lookup(method->allocations_table, key);
70
+ if (!allocation)
71
+ {
72
+ allocation = prof_allocation_create();
73
+ allocation->source_line = source_line;
74
+ allocation->source_file = rb_tracearg_path(trace_arg);
75
+ allocation->klass_flags = 0;
76
+ allocation->klass = resolve_klass(klass, &allocation->klass_flags);
77
+
78
+ allocation->key = key;
79
+ allocations_table_insert(method->allocations_table, key, allocation);
80
+ }
81
+
82
+ allocation->count++;
83
+ allocation->memory += rb_obj_memsize_of(object);
84
+
85
+ return allocation;
86
+ }
87
+
88
+ static void prof_allocation_ruby_gc_free(void* data)
89
+ {
90
+ if (data)
91
+ {
92
+ prof_allocation_t* allocation = (prof_allocation_t*)data;
93
+ allocation->object = Qnil;
94
+ }
95
+ }
96
+
97
+ void prof_allocation_free(prof_allocation_t* allocation)
98
+ {
99
+ /* Has this allocation object been accessed by Ruby? If
100
+ yes clean it up so to avoid a segmentation fault. */
101
+ if (allocation->object != Qnil)
102
+ {
103
+ RTYPEDDATA(allocation->object)->data = NULL;
104
+ allocation->object = Qnil;
105
+ }
106
+
107
+ xfree(allocation);
108
+ }
109
+
110
+ size_t prof_allocation_size(const void* data)
111
+ {
112
+ return sizeof(prof_allocation_t);
113
+ }
114
+
115
+ void prof_allocation_mark(void* data)
116
+ {
117
+ if (!data) return;
118
+
119
+ prof_allocation_t* allocation = (prof_allocation_t*)data;
120
+ if (allocation->object != Qnil)
121
+ rb_gc_mark(allocation->object);
122
+
123
+ if (allocation->klass != Qnil)
124
+ rb_gc_mark(allocation->klass);
125
+
126
+ if (allocation->klass_name != Qnil)
127
+ rb_gc_mark(allocation->klass_name);
128
+
129
+ if (allocation->source_file != Qnil)
130
+ rb_gc_mark(allocation->source_file);
131
+ }
132
+
133
+ static const rb_data_type_t allocation_type =
134
+ {
135
+ .wrap_struct_name = "Allocation",
136
+ .function =
137
+ {
138
+ .dmark = prof_allocation_mark,
139
+ .dfree = prof_allocation_ruby_gc_free,
140
+ .dsize = prof_allocation_size,
141
+ },
142
+ .data = NULL,
143
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY
144
+ };
145
+
146
+ VALUE prof_allocation_wrap(prof_allocation_t* allocation)
147
+ {
148
+ if (allocation->object == Qnil)
149
+ {
150
+ allocation->object = TypedData_Wrap_Struct(cRpAllocation, &allocation_type, allocation);
151
+ }
152
+ return allocation->object;
153
+ }
154
+
155
+ static VALUE prof_allocation_allocate(VALUE klass)
156
+ {
157
+ prof_allocation_t* allocation = prof_allocation_create();
158
+ allocation->object = prof_allocation_wrap(allocation);
159
+ return allocation->object;
160
+ }
161
+
162
+ prof_allocation_t* prof_allocation_get(VALUE self)
163
+ {
164
+ /* Can't use Data_Get_Struct because that triggers the event hook
165
+ ending up in endless recursion. */
166
+ prof_allocation_t* result = RTYPEDDATA_DATA(self);
167
+ if (!result)
168
+ rb_raise(rb_eRuntimeError, "This RubyProf::Allocation instance has already been freed, likely because its profile has been freed.");
169
+
170
+ return result;
171
+ }
172
+
173
+ /* call-seq:
174
+ klass -> Class
175
+
176
+ Returns the type of Class being allocated. */
177
+ static VALUE prof_allocation_klass_name(VALUE self)
178
+ {
179
+ prof_allocation_t* allocation = prof_allocation_get(self);
180
+
181
+ if (allocation->klass_name == Qnil)
182
+ allocation->klass_name = resolve_klass_name(allocation->klass, &allocation->klass_flags);
183
+
184
+ return allocation->klass_name;
185
+ }
186
+
187
+ /* call-seq:
188
+ klass_flags -> integer
189
+
190
+ Returns the klass flags */
191
+
192
+ static VALUE prof_allocation_klass_flags(VALUE self)
193
+ {
194
+ prof_allocation_t* allocation = prof_allocation_get(self);
195
+ return INT2FIX(allocation->klass_flags);
196
+ }
197
+
198
+ /* call-seq:
199
+ source_file -> string
200
+
201
+ Returns the the line number where objects were allocated. */
202
+ static VALUE prof_allocation_source_file(VALUE self)
203
+ {
204
+ prof_allocation_t* allocation = prof_allocation_get(self);
205
+ return allocation->source_file;
206
+ }
207
+
208
+ /* call-seq:
209
+ line -> number
210
+
211
+ Returns the the line number where objects were allocated. */
212
+ static VALUE prof_allocation_source_line(VALUE self)
213
+ {
214
+ prof_allocation_t* allocation = prof_allocation_get(self);
215
+ return INT2FIX(allocation->source_line);
216
+ }
217
+
218
+ /* call-seq:
219
+ count -> number
220
+
221
+ Returns the number of times this class has been allocated. */
222
+ static VALUE prof_allocation_count(VALUE self)
223
+ {
224
+ prof_allocation_t* allocation = prof_allocation_get(self);
225
+ return INT2FIX(allocation->count);
226
+ }
227
+
228
+ /* call-seq:
229
+ memory -> number
230
+
231
+ Returns the amount of memory allocated. */
232
+ static VALUE prof_allocation_memory(VALUE self)
233
+ {
234
+ prof_allocation_t* allocation = prof_allocation_get(self);
235
+ return ULL2NUM(allocation->memory);
236
+ }
237
+
238
+ /* :nodoc: */
239
+ static VALUE prof_allocation_dump(VALUE self)
240
+ {
241
+ prof_allocation_t* allocation = prof_get_allocation(self);
242
+
243
+ VALUE result = rb_hash_new();
244
+
245
+ rb_hash_aset(result, ID2SYM(rb_intern("key")), INT2FIX(allocation->key));
246
+ rb_hash_aset(result, ID2SYM(rb_intern("klass_name")), prof_allocation_klass_name(self));
247
+ rb_hash_aset(result, ID2SYM(rb_intern("klass_flags")), INT2FIX(allocation->klass_flags));
248
+ rb_hash_aset(result, ID2SYM(rb_intern("source_file")), allocation->source_file);
249
+ rb_hash_aset(result, ID2SYM(rb_intern("source_line")), INT2FIX(allocation->source_line));
250
+ rb_hash_aset(result, ID2SYM(rb_intern("count")), INT2FIX(allocation->count));
251
+ rb_hash_aset(result, ID2SYM(rb_intern("memory")), LONG2FIX(allocation->memory));
252
+
253
+ return result;
254
+ }
255
+
256
+ /* :nodoc: */
257
+ static VALUE prof_allocation_load(VALUE self, VALUE data)
258
+ {
259
+ prof_allocation_t* allocation = prof_get_allocation(self);
260
+ allocation->object = self;
261
+
262
+ allocation->key = FIX2LONG(rb_hash_aref(data, ID2SYM(rb_intern("key"))));
263
+ allocation->klass_name = rb_hash_aref(data, ID2SYM(rb_intern("klass_name")));
264
+ allocation->klass_flags = FIX2INT(rb_hash_aref(data, ID2SYM(rb_intern("klass_flags"))));
265
+ allocation->source_file = rb_hash_aref(data, ID2SYM(rb_intern("source_file")));
266
+ allocation->source_line = FIX2INT(rb_hash_aref(data, ID2SYM(rb_intern("source_line"))));
267
+ allocation->count = FIX2INT(rb_hash_aref(data, ID2SYM(rb_intern("count"))));
268
+ allocation->memory = FIX2LONG(rb_hash_aref(data, ID2SYM(rb_intern("memory"))));
269
+
270
+ return data;
271
+ }
272
+
273
+ void rp_init_allocation(void)
274
+ {
275
+ cRpAllocation = rb_define_class_under(mProf, "Allocation", rb_cObject);
276
+ rb_undef_method(CLASS_OF(cRpAllocation), "new");
277
+ rb_define_alloc_func(cRpAllocation, prof_allocation_allocate);
278
+
279
+ rb_define_method(cRpAllocation, "klass_name", prof_allocation_klass_name, 0);
280
+ rb_define_method(cRpAllocation, "klass_flags", prof_allocation_klass_flags, 0);
281
+ rb_define_method(cRpAllocation, "source_file", prof_allocation_source_file, 0);
282
+ rb_define_method(cRpAllocation, "line", prof_allocation_source_line, 0);
283
+ rb_define_method(cRpAllocation, "count", prof_allocation_count, 0);
284
+ rb_define_method(cRpAllocation, "memory", prof_allocation_memory, 0);
285
+ rb_define_method(cRpAllocation, "_dump_data", prof_allocation_dump, 0);
286
+ rb_define_method(cRpAllocation, "_load_data", prof_allocation_load, 1);
287
+ }