ruby-prof 0.18.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (129) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGES +44 -1
  3. data/LICENSE +2 -2
  4. data/README.rdoc +1 -483
  5. data/Rakefile +3 -6
  6. data/bin/ruby-prof +111 -128
  7. data/ext/ruby_prof/extconf.rb +6 -38
  8. data/ext/ruby_prof/rp_aggregate_call_tree.c +41 -0
  9. data/ext/ruby_prof/rp_aggregate_call_tree.h +13 -0
  10. data/ext/ruby_prof/rp_allocation.c +259 -0
  11. data/ext/ruby_prof/rp_allocation.h +31 -0
  12. data/ext/ruby_prof/rp_call_tree.c +353 -0
  13. data/ext/ruby_prof/rp_call_tree.h +43 -0
  14. data/ext/ruby_prof/rp_call_trees.c +266 -0
  15. data/ext/ruby_prof/rp_call_trees.h +29 -0
  16. data/ext/ruby_prof/rp_measure_allocations.c +25 -51
  17. data/ext/ruby_prof/rp_measure_memory.c +21 -56
  18. data/ext/ruby_prof/rp_measure_process_time.c +37 -43
  19. data/ext/ruby_prof/rp_measure_wall_time.c +40 -21
  20. data/ext/ruby_prof/rp_measurement.c +221 -0
  21. data/ext/ruby_prof/rp_measurement.h +50 -0
  22. data/ext/ruby_prof/rp_method.c +279 -439
  23. data/ext/ruby_prof/rp_method.h +33 -45
  24. data/ext/ruby_prof/rp_profile.c +902 -0
  25. data/ext/ruby_prof/rp_profile.h +36 -0
  26. data/ext/ruby_prof/rp_stack.c +163 -132
  27. data/ext/ruby_prof/rp_stack.h +18 -28
  28. data/ext/ruby_prof/rp_thread.c +192 -124
  29. data/ext/ruby_prof/rp_thread.h +18 -8
  30. data/ext/ruby_prof/ruby_prof.c +36 -778
  31. data/ext/ruby_prof/ruby_prof.h +11 -45
  32. data/ext/ruby_prof/vc/ruby_prof.vcxproj +18 -12
  33. data/lib/ruby-prof.rb +4 -21
  34. data/lib/ruby-prof/assets/call_stack_printer.html.erb +710 -0
  35. data/lib/ruby-prof/assets/call_stack_printer.png +0 -0
  36. data/lib/ruby-prof/assets/graph_printer.html.erb +355 -0
  37. data/lib/ruby-prof/call_tree.rb +57 -0
  38. data/lib/ruby-prof/call_tree_visitor.rb +36 -0
  39. data/lib/ruby-prof/compatibility.rb +37 -107
  40. data/lib/ruby-prof/exclude_common_methods.rb +198 -0
  41. data/lib/ruby-prof/measurement.rb +17 -0
  42. data/lib/ruby-prof/method_info.rb +47 -90
  43. data/lib/ruby-prof/printers/abstract_printer.rb +73 -50
  44. data/lib/ruby-prof/printers/call_info_printer.rb +24 -12
  45. data/lib/ruby-prof/printers/call_stack_printer.rb +66 -152
  46. data/lib/ruby-prof/printers/call_tree_printer.rb +20 -12
  47. data/lib/ruby-prof/printers/dot_printer.rb +5 -5
  48. data/lib/ruby-prof/printers/flat_printer.rb +6 -24
  49. data/lib/ruby-prof/printers/graph_html_printer.rb +6 -192
  50. data/lib/ruby-prof/printers/graph_printer.rb +11 -14
  51. data/lib/ruby-prof/printers/multi_printer.rb +66 -23
  52. data/lib/ruby-prof/profile.rb +10 -3
  53. data/lib/ruby-prof/thread.rb +5 -20
  54. data/lib/ruby-prof/version.rb +1 -1
  55. data/ruby-prof.gemspec +9 -2
  56. data/test/abstract_printer_test.rb +0 -27
  57. data/test/alias_test.rb +126 -0
  58. data/test/basic_test.rb +1 -86
  59. data/test/call_tree_visitor_test.rb +32 -0
  60. data/test/call_trees_test.rb +66 -0
  61. data/test/dynamic_method_test.rb +0 -2
  62. data/test/exclude_methods_test.rb +17 -12
  63. data/test/fiber_test.rb +214 -23
  64. data/test/gc_test.rb +105 -0
  65. data/test/inverse_call_tree_test.rb +175 -0
  66. data/test/line_number_test.rb +118 -40
  67. data/test/marshal_test.rb +115 -0
  68. data/test/measure_allocations.rb +30 -0
  69. data/test/measure_allocations_test.rb +361 -12
  70. data/test/measure_allocations_trace_test.rb +375 -0
  71. data/test/measure_memory_trace_test.rb +1101 -0
  72. data/test/measure_process_time_test.rb +757 -33
  73. data/test/measure_times.rb +56 -0
  74. data/test/measure_wall_time_test.rb +329 -149
  75. data/test/multi_printer_test.rb +1 -34
  76. data/test/pause_resume_test.rb +24 -15
  77. data/test/prime.rb +1 -1
  78. data/test/prime_script.rb +6 -0
  79. data/test/printer_call_stack_test.rb +28 -0
  80. data/test/printer_call_tree_test.rb +31 -0
  81. data/test/printer_flat_test.rb +68 -0
  82. data/test/printer_graph_html_test.rb +60 -0
  83. data/test/printer_graph_test.rb +41 -0
  84. data/test/printers_test.rb +32 -166
  85. data/test/printing_recursive_graph_test.rb +26 -72
  86. data/test/recursive_test.rb +68 -77
  87. data/test/stack_printer_test.rb +2 -15
  88. data/test/start_stop_test.rb +22 -25
  89. data/test/test_helper.rb +6 -261
  90. data/test/thread_test.rb +11 -54
  91. data/test/unique_call_path_test.rb +25 -107
  92. data/test/yarv_test.rb +1 -0
  93. metadata +43 -41
  94. data/examples/flat.txt +0 -50
  95. data/examples/graph.dot +0 -84
  96. data/examples/graph.html +0 -823
  97. data/examples/graph.txt +0 -139
  98. data/examples/multi.flat.txt +0 -23
  99. data/examples/multi.graph.html +0 -760
  100. data/examples/multi.grind.dat +0 -114
  101. data/examples/multi.stack.html +0 -547
  102. data/examples/stack.html +0 -547
  103. data/ext/ruby_prof/rp_call_info.c +0 -425
  104. data/ext/ruby_prof/rp_call_info.h +0 -53
  105. data/ext/ruby_prof/rp_measure.c +0 -40
  106. data/ext/ruby_prof/rp_measure.h +0 -45
  107. data/ext/ruby_prof/rp_measure_cpu_time.c +0 -136
  108. data/ext/ruby_prof/rp_measure_gc_runs.c +0 -73
  109. data/ext/ruby_prof/rp_measure_gc_time.c +0 -60
  110. data/lib/ruby-prof/aggregate_call_info.rb +0 -76
  111. data/lib/ruby-prof/assets/call_stack_printer.css.html +0 -117
  112. data/lib/ruby-prof/assets/call_stack_printer.js.html +0 -385
  113. data/lib/ruby-prof/call_info.rb +0 -115
  114. data/lib/ruby-prof/call_info_visitor.rb +0 -40
  115. data/lib/ruby-prof/printers/flat_printer_with_line_numbers.rb +0 -83
  116. data/lib/ruby-prof/profile/exclude_common_methods.rb +0 -207
  117. data/lib/ruby-prof/profile/legacy_method_elimination.rb +0 -50
  118. data/test/aggregate_test.rb +0 -136
  119. data/test/block_test.rb +0 -74
  120. data/test/call_info_test.rb +0 -78
  121. data/test/call_info_visitor_test.rb +0 -31
  122. data/test/issue137_test.rb +0 -63
  123. data/test/measure_cpu_time_test.rb +0 -212
  124. data/test/measure_gc_runs_test.rb +0 -32
  125. data/test/measure_gc_time_test.rb +0 -36
  126. data/test/measure_memory_test.rb +0 -33
  127. data/test/method_elimination_test.rb +0 -84
  128. data/test/module_test.rb +0 -45
  129. data/test/stack_test.rb +0 -138
@@ -0,0 +1,43 @@
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_CALL_TREE_H__
5
+ #define __RP_CALL_TREE_H__
6
+
7
+ #include "ruby_prof.h"
8
+ #include "rp_measurement.h"
9
+ #include "rp_method.h"
10
+
11
+ extern VALUE cRpCallTree;
12
+
13
+ /* Callers and callee information for a method. */
14
+ typedef struct prof_call_tree_t
15
+ {
16
+ prof_method_t* method;
17
+ struct prof_call_tree_t* parent;
18
+ st_table* children; /* Call infos that this call info calls */
19
+ prof_measurement_t* measurement;
20
+ VALUE object;
21
+
22
+ int visits; /* Current visits on the stack */
23
+
24
+ unsigned int source_line;
25
+ VALUE source_file;
26
+ } prof_call_tree_t;
27
+
28
+ prof_call_tree_t* prof_call_tree_create(prof_method_t* method, prof_call_tree_t* parent, VALUE source_file, int source_line);
29
+ prof_call_tree_t* prof_call_tree_copy(prof_call_tree_t* other);
30
+ void prof_call_tree_merge(prof_call_tree_t* result, prof_call_tree_t* other);
31
+ void prof_call_tree_mark(void* data);
32
+ prof_call_tree_t* call_tree_table_lookup(st_table* table, st_data_t key);
33
+
34
+ void prof_call_tree_add_parent(prof_call_tree_t* self, prof_call_tree_t* parent);
35
+ void prof_call_tree_add_child(prof_call_tree_t* self, prof_call_tree_t* child);
36
+
37
+ uint32_t prof_call_figure_depth(prof_call_tree_t* call_tree_data);
38
+ prof_call_tree_t* prof_get_call_tree(VALUE self);
39
+ VALUE prof_call_tree_wrap(prof_call_tree_t* call_tree);
40
+ void prof_call_tree_free(prof_call_tree_t* call_tree);
41
+ void rp_init_call_tree(void);
42
+
43
+ #endif //__RP_CALL_TREE_H__
@@ -0,0 +1,266 @@
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_aggregate_call_tree.h"
5
+ #include "rp_call_trees.h"
6
+ #include "rp_measurement.h"
7
+
8
+ #define INITIAL_CALL_TREES_SIZE 2
9
+
10
+ VALUE cRpCallTrees;
11
+
12
+ /* ======= Call Infos ========*/
13
+ prof_call_trees_t* prof_get_call_trees(VALUE self)
14
+ {
15
+ /* Can't use Data_Get_Struct because that triggers the event hook
16
+ ending up in endless recursion. */
17
+ prof_call_trees_t* result = DATA_PTR(self);
18
+
19
+ if (!result)
20
+ rb_raise(rb_eRuntimeError, "This RubyProf::CallTrees instance has already been freed, likely because its profile has been freed.");
21
+
22
+ return result;
23
+ }
24
+
25
+ prof_call_trees_t* prof_call_trees_create()
26
+ {
27
+ prof_call_trees_t* result = ALLOC(prof_call_trees_t);
28
+ result->start = ALLOC_N(prof_call_tree_t*, INITIAL_CALL_TREES_SIZE);
29
+ result->end = result->start + INITIAL_CALL_TREES_SIZE;
30
+ result->ptr = result->start;
31
+ result->object = Qnil;
32
+ return result;
33
+ }
34
+
35
+ void prof_call_trees_mark(prof_call_trees_t* call_trees)
36
+ {
37
+ prof_call_tree_t** call_tree;
38
+ for (call_tree = call_trees->start; call_tree < call_trees->ptr; call_tree++)
39
+ {
40
+ prof_call_tree_mark(*call_tree);
41
+ }
42
+ }
43
+
44
+ void prof_call_trees_free(prof_call_trees_t* call_trees)
45
+ {
46
+ /* Has this method object been accessed by Ruby? If
47
+ yes clean it up so to avoid a segmentation fault. */
48
+ if (call_trees->object != Qnil)
49
+ {
50
+ RDATA(call_trees->object)->dmark = NULL;
51
+ RDATA(call_trees->object)->dfree = NULL;
52
+ RDATA(call_trees->object)->data = NULL;
53
+ call_trees->object = Qnil;
54
+ }
55
+
56
+ // Note we do not free our call_tree structures - since they have no parents they will free themselves
57
+ xfree(call_trees);
58
+ }
59
+
60
+ void prof_call_trees_ruby_gc_free(void* data)
61
+ {
62
+ // This object gets freed by its owning method
63
+ prof_call_trees_t* call_trees = (prof_call_trees_t*)data;
64
+ call_trees->object = Qnil;
65
+ }
66
+
67
+ static int prof_call_trees_collect_aggregates(st_data_t key, st_data_t value, st_data_t data)
68
+ {
69
+ VALUE result = (VALUE)data;
70
+ prof_call_tree_t* call_tree_data = (prof_call_tree_t*)value;
71
+ VALUE aggregate_call_tree = prof_aggregate_call_tree_wrap(call_tree_data);
72
+ rb_ary_push(result, aggregate_call_tree);
73
+
74
+ return ST_CONTINUE;
75
+ }
76
+
77
+ static int prof_call_trees_collect_callees(st_data_t key, st_data_t value, st_data_t hash)
78
+ {
79
+ st_table* callers = (st_table*)hash;
80
+ prof_call_tree_t* call_tree_data = (prof_call_tree_t*)value;
81
+
82
+ prof_call_tree_t* aggregate_call_tree_data = NULL;
83
+
84
+ if (rb_st_lookup(callers, call_tree_data->method->key, (st_data_t*)&aggregate_call_tree_data))
85
+ {
86
+ prof_call_tree_merge(aggregate_call_tree_data, call_tree_data);
87
+ }
88
+ else
89
+ {
90
+ aggregate_call_tree_data = prof_call_tree_copy(call_tree_data);
91
+ rb_st_insert(callers, call_tree_data->method->key, (st_data_t)aggregate_call_tree_data);
92
+ }
93
+
94
+ return ST_CONTINUE;
95
+ }
96
+
97
+ VALUE prof_call_trees_wrap(prof_call_trees_t* call_trees)
98
+ {
99
+ if (call_trees->object == Qnil)
100
+ {
101
+ call_trees->object = Data_Wrap_Struct(cRpCallTrees, prof_call_trees_mark, prof_call_trees_ruby_gc_free, call_trees);
102
+ }
103
+ return call_trees->object;
104
+ }
105
+
106
+ void prof_add_call_tree(prof_call_trees_t* call_trees, prof_call_tree_t* call_tree)
107
+ {
108
+ if (call_trees->ptr == call_trees->end)
109
+ {
110
+ size_t len = call_trees->ptr - call_trees->start;
111
+ size_t new_capacity = (call_trees->end - call_trees->start) * 2;
112
+ REALLOC_N(call_trees->start, prof_call_tree_t*, new_capacity);
113
+ call_trees->ptr = call_trees->start + len;
114
+ call_trees->end = call_trees->start + new_capacity;
115
+ }
116
+ *call_trees->ptr = call_tree;
117
+ call_trees->ptr++;
118
+ }
119
+
120
+ /* ================ Call Infos =================*/
121
+ /* Document-class: RubyProf::CallTrees
122
+ The RubyProf::MethodInfo class stores profiling data for a method.
123
+ One instance of the RubyProf::MethodInfo class is created per method
124
+ called per thread. Thus, if a method is called in two different
125
+ thread then there will be two RubyProf::MethodInfo objects
126
+ created. RubyProf::MethodInfo objects can be accessed via
127
+ the RubyProf::Profile object. */
128
+ VALUE prof_call_trees_allocate(VALUE klass)
129
+ {
130
+ prof_call_trees_t* call_trees_data = prof_call_trees_create();
131
+ call_trees_data->object = prof_call_trees_wrap(call_trees_data);
132
+ return call_trees_data->object;
133
+ }
134
+
135
+
136
+ /* call-seq:
137
+ min_depth -> Integer
138
+
139
+ Returns the minimum depth of this method in any call tree */
140
+ VALUE prof_call_trees_min_depth(VALUE self)
141
+ {
142
+ unsigned int depth = INT_MAX;
143
+
144
+ prof_call_trees_t* call_trees = prof_get_call_trees(self);
145
+ for (prof_call_tree_t** p_call_tree = call_trees->start; p_call_tree < call_trees->ptr; p_call_tree++)
146
+ {
147
+ unsigned int call_tree_depth = prof_call_figure_depth(*p_call_tree);
148
+ if (call_tree_depth < depth)
149
+ depth = call_tree_depth;
150
+ }
151
+
152
+ return UINT2NUM(depth);
153
+ }
154
+
155
+ /* call-seq:
156
+ callers -> array
157
+
158
+ Returns an array of all CallTree objects that called this method. */
159
+ VALUE prof_call_trees_call_trees(VALUE self)
160
+ {
161
+ VALUE result = rb_ary_new();
162
+
163
+ prof_call_trees_t* call_trees = prof_get_call_trees(self);
164
+ for (prof_call_tree_t** p_call_tree = call_trees->start; p_call_tree < call_trees->ptr; p_call_tree++)
165
+ {
166
+ VALUE call_tree = prof_call_tree_wrap(*p_call_tree);
167
+ rb_ary_push(result, call_tree);
168
+ }
169
+ return result;
170
+ }
171
+
172
+ /* call-seq:
173
+ callers -> array
174
+
175
+ Returns an array of aggregated CallTree objects that called this method (ie, parents).*/
176
+ VALUE prof_call_trees_callers(VALUE self)
177
+ {
178
+ st_table* callers = rb_st_init_numtable();
179
+
180
+ prof_call_trees_t* call_trees = prof_get_call_trees(self);
181
+ for (prof_call_tree_t** p_call_tree = call_trees->start; p_call_tree < call_trees->ptr; p_call_tree++)
182
+ {
183
+ prof_call_tree_t* parent = (*p_call_tree)->parent;
184
+ if (parent == NULL)
185
+ continue;
186
+
187
+ prof_call_tree_t* aggregate_call_tree_data = NULL;
188
+
189
+ if (rb_st_lookup(callers, parent->method->key, (st_data_t*)&aggregate_call_tree_data))
190
+ {
191
+ prof_call_tree_merge(aggregate_call_tree_data, *p_call_tree);
192
+ }
193
+ else
194
+ {
195
+ aggregate_call_tree_data = prof_call_tree_copy(*p_call_tree);
196
+ rb_st_insert(callers, parent->method->key, (st_data_t)aggregate_call_tree_data);
197
+ }
198
+ }
199
+
200
+ VALUE result = rb_ary_new_capa(callers->num_entries);
201
+ rb_st_foreach(callers, prof_call_trees_collect_aggregates, result);
202
+ rb_st_free_table(callers);
203
+ return result;
204
+ }
205
+
206
+ /* call-seq:
207
+ callees -> array
208
+
209
+ Returns an array of aggregated CallTree objects that this method called (ie, children).*/
210
+ VALUE prof_call_trees_callees(VALUE self)
211
+ {
212
+ st_table* callees = rb_st_init_numtable();
213
+
214
+ prof_call_trees_t* call_trees = prof_get_call_trees(self);
215
+ for (prof_call_tree_t** call_tree = call_trees->start; call_tree < call_trees->ptr; call_tree++)
216
+ {
217
+ rb_st_foreach((*call_tree)->children, prof_call_trees_collect_callees, (st_data_t)callees);
218
+ }
219
+
220
+ VALUE result = rb_ary_new_capa(callees->num_entries);
221
+ rb_st_foreach(callees, prof_call_trees_collect_aggregates, result);
222
+ rb_st_free_table(callees);
223
+ return result;
224
+ }
225
+
226
+ /* :nodoc: */
227
+ VALUE prof_call_trees_dump(VALUE self)
228
+ {
229
+ VALUE result = rb_hash_new();
230
+ rb_hash_aset(result, ID2SYM(rb_intern("call_trees")), prof_call_trees_call_trees(self));
231
+
232
+ return result;
233
+ }
234
+
235
+ /* :nodoc: */
236
+ VALUE prof_call_trees_load(VALUE self, VALUE data)
237
+ {
238
+ prof_call_trees_t* call_trees_data = DATA_PTR(self);
239
+ call_trees_data->object = self;
240
+
241
+ VALUE call_trees = rb_hash_aref(data, ID2SYM(rb_intern("call_trees")));
242
+ for (int i = 0; i < rb_array_len(call_trees); i++)
243
+ {
244
+ VALUE call_tree = rb_ary_entry(call_trees, i);
245
+ prof_call_tree_t* call_tree_data = prof_get_call_tree(call_tree);
246
+ prof_add_call_tree(call_trees_data, call_tree_data);
247
+ }
248
+
249
+ return data;
250
+ }
251
+
252
+ void rp_init_call_trees()
253
+ {
254
+ cRpCallTrees = rb_define_class_under(mProf, "CallTrees", rb_cData);
255
+ rb_undef_method(CLASS_OF(cRpCallTrees), "new");
256
+ rb_define_alloc_func(cRpCallTrees, prof_call_trees_allocate);
257
+
258
+ rb_define_method(cRpCallTrees, "min_depth", prof_call_trees_min_depth, 0);
259
+
260
+ rb_define_method(cRpCallTrees, "call_trees", prof_call_trees_call_trees, 0);
261
+ rb_define_method(cRpCallTrees, "callers", prof_call_trees_callers, 0);
262
+ rb_define_method(cRpCallTrees, "callees", prof_call_trees_callees, 0);
263
+
264
+ rb_define_method(cRpCallTrees, "_dump_data", prof_call_trees_dump, 0);
265
+ rb_define_method(cRpCallTrees, "_load_data", prof_call_trees_load, 1);
266
+ }
@@ -0,0 +1,29 @@
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
+ #ifndef __RP_CALL_TREES_H__
5
+ #define __RP_CALL_TREES_H__
6
+
7
+ #include "ruby_prof.h"
8
+ #include "rp_call_tree.h"
9
+
10
+ /* Array of call_tree objects */
11
+ typedef struct prof_call_trees_t
12
+ {
13
+ prof_call_tree_t** start;
14
+ prof_call_tree_t** end;
15
+ prof_call_tree_t** ptr;
16
+
17
+ VALUE object;
18
+ } prof_call_trees_t;
19
+
20
+
21
+ void rp_init_call_trees();
22
+ prof_call_trees_t* prof_call_trees_create();
23
+ void prof_call_trees_mark(prof_call_trees_t* call_trees);
24
+ void prof_call_trees_free(prof_call_trees_t* call_trees);
25
+ prof_call_trees_t* prof_get_call_trees(VALUE self);
26
+ void prof_add_call_tree(prof_call_trees_t* call_trees, prof_call_tree_t* call_tree);
27
+ VALUE prof_call_trees_wrap(prof_call_trees_t* call_trees);
28
+
29
+ #endif //__RP_CALL_TREES_H__
@@ -3,74 +3,48 @@
3
3
 
4
4
  /* :nodoc: */
5
5
 
6
- #include "ruby_prof.h"
6
+ #include "rp_measurement.h"
7
7
 
8
8
  static VALUE cMeasureAllocations;
9
+ VALUE total_allocated_objects_key;
9
10
 
10
- #if defined(HAVE_RB_OS_ALLOCATED_OBJECTS)
11
- unsigned LONG_LONG rb_os_allocated_objects();
12
- #endif
13
-
14
- #if defined(HAVE_RB_GC_STAT)
15
- size_t rb_gc_stat(VALUE key);
16
-
17
- #if RUBY_PROF_RUBY_VERSION >= 20200
18
- #define TOTAL_ALLOCATED_OBJECTS_STRING "total_allocated_objects"
19
- #else
20
- #define TOTAL_ALLOCATED_OBJECTS_STRING "total_allocated_object"
21
- #endif
22
-
23
- #endif
11
+ static double measure_allocations_via_gc_stats(rb_trace_arg_t* trace_arg)
12
+ {
13
+ return rb_gc_stat(total_allocated_objects_key);
14
+ }
24
15
 
25
- static double
26
- measure_allocations()
16
+ static double measure_allocations_via_tracing(rb_trace_arg_t* trace_arg)
27
17
  {
28
- #if defined(HAVE_RB_OS_ALLOCATED_OBJECTS)
29
- #define MEASURE_ALLOCATIONS_ENABLED Qtrue
30
- return rb_os_allocated_objects();
18
+ static double result = 0;
31
19
 
32
- #elif defined(HAVE_RB_GC_STAT) && RUBY_PROF_RUBY_VERSION >= 20100
33
- #define MEASURE_ALLOCATIONS_ENABLED Qtrue
34
- static VALUE total_alloc_symbol = 0;
35
- if (!total_alloc_symbol) {
36
- total_alloc_symbol = ID2SYM(rb_intern_const(TOTAL_ALLOCATED_OBJECTS_STRING));
20
+ if (trace_arg)
21
+ {
22
+ rb_event_flag_t event = rb_tracearg_event_flag(trace_arg);
23
+ if (event == RUBY_INTERNAL_EVENT_NEWOBJ)
24
+ result++;
37
25
  }
38
- return rb_gc_stat(total_alloc_symbol);
39
-
40
- #else
41
- #define MEASURE_ALLOCATIONS_ENABLED Qfalse
42
- return 0;
43
- #endif
26
+ return result;
44
27
  }
45
28
 
46
-
47
- prof_measurer_t* prof_measurer_allocations()
29
+ prof_measurer_t* prof_measurer_allocations(bool track_allocations)
48
30
  {
49
- prof_measurer_t* measure = ALLOC(prof_measurer_t);
50
- measure->measure = measure_allocations;
51
- return measure;
52
- }
31
+ prof_measurer_t* measure = ALLOC(prof_measurer_t);
32
+ measure->mode = MEASURE_ALLOCATIONS;
33
+ measure->multiplier = 1;
34
+ measure->track_allocations = track_allocations;
53
35
 
54
- /* call-seq:
55
- measure -> int
36
+ if (track_allocations)
37
+ measure->measure = measure_allocations_via_tracing;
38
+ else
39
+ measure->measure = measure_allocations_via_gc_stats;
56
40
 
57
- Returns the number of Ruby object allocations. */
58
-
59
- static VALUE
60
- prof_measure_allocations(VALUE self)
61
- {
62
- #if defined(HAVE_LONG_LONG)
63
- return ULL2NUM(measure_allocations());
64
- #else
65
- return ULONG2NUM(measure_allocations());
66
- #endif
41
+ return measure;
67
42
  }
68
43
 
69
44
  void rp_init_measure_allocations()
70
45
  {
46
+ total_allocated_objects_key = ID2SYM(rb_intern("total_allocated_objects"));
71
47
  rb_define_const(mProf, "ALLOCATIONS", INT2NUM(MEASURE_ALLOCATIONS));
72
- rb_define_const(mProf, "ALLOCATIONS_ENABLED", MEASURE_ALLOCATIONS_ENABLED);
73
48
 
74
49
  cMeasureAllocations = rb_define_class_under(mMeasure, "Allocations", rb_cObject);
75
- rb_define_singleton_method(cMeasureAllocations, "measure", prof_measure_allocations, 0);
76
50
  }
@@ -1,77 +1,42 @@
1
- /* Copyright (C) 2005-2019 Shugo Maeda <shugo@ruby-lang.org> and Charlie Savage <cfis@savagexi.com>
1
+ /* Copyright (C) 2005-2013 Shugo Maeda <shugo@ruby-lang.org> and Charlie Savage <cfis@savagexi.com>
2
2
  Please see the LICENSE file for copyright and distribution information */
3
3
 
4
4
  /* :nodoc: */
5
5
 
6
- #include "ruby_prof.h"
6
+ #include "rp_measurement.h"
7
7
 
8
8
  static VALUE cMeasureMemory;
9
9
 
10
-
11
- #if defined(HAVE_RB_GC_ALLOCATED_SIZE)
12
- VALUE rb_gc_allocated_size();
13
- #endif
14
-
15
- #if defined(HAVE_RB_GC_MALLOC_ALLOCATED_SIZE)
16
- size_t rb_gc_malloc_allocated_size();
17
- #endif
18
-
19
- #if defined(HAVE_RB_HEAP_TOTAL_MEM)
20
- //FIXME: did not find the patch to check prototype, assuming it to return size_t
21
- size_t rb_heap_total_mem();
22
- #endif
23
-
24
10
  static double
25
- measure_memory()
11
+ measure_memory_via_tracing(rb_trace_arg_t* trace_arg)
26
12
  {
27
- #if defined(HAVE_RB_GC_ALLOCATED_SIZE)
28
- #define MEASURE_MEMORY_ENABLED Qtrue
29
- #if defined(HAVE_LONG_LONG)
30
- return NUM2LL(rb_gc_allocated_size()) / 1024.0;
31
- #else
32
- return NUM2ULONG(rb_gc_allocated_size()) / 1024.0;
33
- #endif
34
-
35
- #elif defined(HAVE_RB_GC_MALLOC_ALLOCATED_SIZE)
36
- #define MEASURE_MEMORY_ENABLED Qtrue
37
- return rb_gc_malloc_allocated_size() / 1024.0;
38
-
39
- #elif defined(HAVE_RB_GC_TOTAL_MALLOCED_BYTES)
40
- #define MEASURE_MEMORY_ENABLED Qtrue
41
- return rb_gc_total_malloced_bytes() / 1024.0;
42
-
43
- #elif defined(HAVE_RB_HEAP_TOTAL_MEM)
44
- #define MEASURE_MEMORY_ENABLED Qtrue
45
- return rb_heap_total_mem() / 1024.0;
46
-
47
- #else
48
- #define MEASURE_MEMORY_ENABLED Qfalse
49
- return 0;
50
- #endif
13
+ static double result = 0;
14
+
15
+ if (trace_arg)
16
+ {
17
+ rb_event_flag_t event = rb_tracearg_event_flag(trace_arg);
18
+ if (event == RUBY_INTERNAL_EVENT_NEWOBJ)
19
+ {
20
+ VALUE object = rb_tracearg_object(trace_arg);
21
+ result += rb_obj_memsize_of(object);
22
+ }
23
+ }
24
+ return result;
51
25
  }
52
26
 
53
- prof_measurer_t* prof_measurer_memory()
27
+ prof_measurer_t* prof_measurer_memory(bool track_allocations)
54
28
  {
55
29
  prof_measurer_t* measure = ALLOC(prof_measurer_t);
56
- measure->measure = measure_memory;
30
+ measure->mode = MEASURE_MEMORY;
31
+ measure->measure = measure_memory_via_tracing;
32
+ measure->multiplier = 1;
33
+ measure->track_allocations = true;
57
34
  return measure;
58
35
  }
59
36
 
60
- /* call-seq:
61
- measure_process_time -> float
62
-
63
- Returns the process time.*/
64
- static VALUE
65
- prof_measure_memory(VALUE self)
66
- {
67
- return rb_float_new(measure_memory());
68
- }
69
-
70
37
  void rp_init_measure_memory()
71
38
  {
72
39
  rb_define_const(mProf, "MEMORY", INT2NUM(MEASURE_MEMORY));
73
- rb_define_const(mProf, "MEMORY_ENABLED", MEASURE_MEMORY_ENABLED);
74
40
 
75
- cMeasureMemory = rb_define_class_under(mMeasure, "Memory", rb_cObject);
76
- rb_define_singleton_method(cMeasureMemory, "measure", prof_measure_memory, 0);
41
+ cMeasureMemory = rb_define_class_under(mMeasure, "Allocations", rb_cObject);
77
42
  }