8
8
# GNU General Public License version 2, or
9
9
# GNU Lesser General Public License version 2.1.
10
10
11
- require 'json'
12
- require 'tempfile'
13
- require 'weakref'
11
+ module Truffle ::ObjSpace
12
+ def self . count_nodes_method ( method , nodes )
13
+ node_stack = [ Truffle . ast ( method ) ]
14
+
15
+ until node_stack . empty?
16
+ node = node_stack . pop
17
+ next if node . nil?
18
+
19
+ name = node . first
20
+ children = node . drop ( 1 )
21
+ nodes [ name ] ||= 0
22
+ nodes [ name ] += 1
23
+ node_stack . push ( *children )
24
+ end
25
+ end
26
+ end
14
27
15
28
module ObjectSpace
29
+ module_function
16
30
17
31
def count_nodes ( nodes = { } )
18
32
ObjectSpace . each_object ( Module ) do |mod |
19
33
mod . methods ( false ) . each do |name |
20
- count_nodes_method mod . method ( name ) , nodes
34
+ Truffle :: ObjSpace . count_nodes_method mod . method ( name ) , nodes
21
35
end
22
36
23
37
mod . private_methods ( false ) . each do |name |
24
- count_nodes_method mod . method ( name ) , nodes
38
+ Truffle :: ObjSpace . count_nodes_method mod . method ( name ) , nodes
25
39
end
26
40
end
27
41
28
42
ObjectSpace . each_object ( Proc ) do |proc |
29
- count_nodes_method proc , nodes
43
+ Truffle :: ObjSpace . count_nodes_method proc , nodes
30
44
end
31
45
32
46
ObjectSpace . each_object ( Method ) do |method |
33
- count_nodes_method method , nodes
47
+ Truffle :: ObjSpace . count_nodes_method method , nodes
34
48
end
35
49
36
50
ObjectSpace . each_object ( UnboundMethod ) do |umethod |
37
- count_nodes_method umethod , nodes
51
+ Truffle :: ObjSpace . count_nodes_method umethod , nodes
38
52
end
39
53
40
54
nodes
41
55
end
42
56
43
- module_function :count_nodes
44
-
45
- class << self
46
-
47
- def count_nodes_method ( method , nodes )
48
- node_stack = [ Truffle . ast ( method ) ]
49
-
50
- until node_stack . empty?
51
- node = node_stack . pop
52
- next if node . nil?
53
-
54
- name = node . first
55
- children = node . drop ( 1 )
56
- nodes [ name ] ||= 0
57
- nodes [ name ] += 1
58
- node_stack . push ( *children )
59
- end
60
- end
61
- private :count_nodes_method
62
-
63
- end
64
-
65
57
def count_objects_size ( hash = { } )
66
58
total = 0
67
59
ObjectSpace . each_object ( Class ) do |klass |
@@ -72,7 +64,6 @@ def count_objects_size(hash = {})
72
64
hash [ :TOTAL ] = total
73
65
hash
74
66
end
75
- module_function :count_objects_size
76
67
77
68
def count_tdata_objects ( hash = { } )
78
69
ObjectSpace . each_object do |object |
@@ -82,11 +73,11 @@ def count_tdata_objects(hash = {})
82
73
end
83
74
hash
84
75
end
85
- module_function :count_tdata_objects
86
76
87
77
def dump ( object , output : :string )
88
78
case output
89
79
when :string
80
+ require 'json'
90
81
json = {
91
82
address : '0x' + object . object_id . to_s ( 16 ) ,
92
83
class : '0x' + object . class . object_id . to_s ( 16 ) ,
@@ -119,6 +110,7 @@ def dump(object, output: :string)
119
110
end
120
111
JSON . generate ( json )
121
112
when :file
113
+ require 'tempfile'
122
114
f = Tempfile . new ( [ 'rubyobj' , '.json' ] )
123
115
f . write dump ( object , output : :string )
124
116
f . close
@@ -128,7 +120,6 @@ def dump(object, output: :string)
128
120
nil
129
121
end
130
122
end
131
- module_function :dump
132
123
133
124
def dump_all ( output : :file )
134
125
case output
@@ -139,6 +130,7 @@ def dump_all(output: :file)
139
130
end
140
131
objects . join ( "\n " )
141
132
when :file
133
+ require 'tempfile'
142
134
f = Tempfile . new ( [ 'ruby' , '.json' ] )
143
135
f . write dump_all ( output : :string )
144
136
f . close
@@ -151,7 +143,6 @@ def dump_all(output: :file)
151
143
nil
152
144
end
153
145
end
154
- module_function :dump_all
155
146
156
147
def memsize_of ( object )
157
148
size = Truffle ::ObjSpace . memsize_of ( object )
@@ -163,7 +154,6 @@ def memsize_of(object)
163
154
size
164
155
end
165
156
end
166
- module_function :memsize_of
167
157
168
158
def memsize_of_all ( klass = BasicObject )
169
159
total = 0
@@ -172,17 +162,14 @@ def memsize_of_all(klass = BasicObject)
172
162
end
173
163
total
174
164
end
175
- module_function :memsize_of_all
176
165
177
166
def reachable_objects_from ( object )
178
167
Truffle ::ObjSpace . adjacent_objects ( object )
179
168
end
180
- module_function :reachable_objects_from
181
169
182
170
def reachable_objects_from_root
183
- { 'roots' => Truffle ::ObjSpace . root_objects }
171
+ { 'roots' => Truffle ::ObjSpace . root_objects }
184
172
end
185
- module_function :reachable_objects_from_root
186
173
187
174
def trace_object_allocations
188
175
trace_object_allocations_start
@@ -192,89 +179,40 @@ def trace_object_allocations
192
179
trace_object_allocations_stop
193
180
end
194
181
end
195
- module_function :trace_object_allocations
196
-
197
- def trace_object_allocations_clear
198
- TruffleRuby . synchronized ( ALLOCATIONS ) do
199
- ALLOCATIONS . clear
200
- end
201
- end
202
- module_function :trace_object_allocations_clear
203
182
204
183
def trace_object_allocations_debug_start
205
184
trace_object_allocations_start
206
185
end
207
- module_function :trace_object_allocations_debug_start
208
186
209
187
def trace_object_allocations_start
210
188
Truffle ::ObjSpace . trace_allocations_start
211
189
end
212
- module_function :trace_object_allocations_start
213
190
214
191
def trace_object_allocations_stop
215
192
Truffle ::ObjSpace . trace_allocations_stop
216
193
end
217
- module_function :trace_object_allocations_stop
194
+
195
+ def trace_object_allocations_clear
196
+ Truffle ::ObjSpace . trace_allocations_clear
197
+ end
218
198
219
199
def allocation_class_path ( object )
220
- allocation = TruffleRuby . synchronized ( ALLOCATIONS ) do
221
- ALLOCATIONS [ object ]
222
- end
223
- return nil if allocation . nil?
224
- allocation . class_path
200
+ Primitive . allocation_class_path ( object )
225
201
end
226
- module_function :allocation_class_path
227
202
228
203
def allocation_generation ( object )
229
- allocation = TruffleRuby . synchronized ( ALLOCATIONS ) do
230
- ALLOCATIONS [ object ]
231
- end
232
- return nil if allocation . nil?
233
- allocation . generation
204
+ Primitive . allocation_generation ( object )
234
205
end
235
- module_function :allocation_generation
236
206
237
207
def allocation_method_id ( object )
238
- allocation = TruffleRuby . synchronized ( ALLOCATIONS ) do
239
- ALLOCATIONS [ object ]
240
- end
241
- return nil if allocation . nil?
242
-
243
- method_id = allocation . method_id
244
- # The allocator function is hidden in MRI
245
- method_id = :new if method_id == :__allocate__
246
- method_id
208
+ Primitive . allocation_method_id ( object )
247
209
end
248
- module_function :allocation_method_id
249
210
250
211
def allocation_sourcefile ( object )
251
- allocation = TruffleRuby . synchronized ( ALLOCATIONS ) do
252
- ALLOCATIONS [ object ]
253
- end
254
- return nil if allocation . nil?
255
- allocation . sourcefile
212
+ Primitive . allocation_sourcefile ( object )
256
213
end
257
- module_function :allocation_sourcefile
258
214
259
215
def allocation_sourceline ( object )
260
- allocation = TruffleRuby . synchronized ( ALLOCATIONS ) do
261
- ALLOCATIONS [ object ]
262
- end
263
- return nil if allocation . nil?
264
- allocation . sourceline
265
- end
266
- module_function :allocation_sourceline
267
-
268
- Allocation = Struct . new ( :class_path , :method_id , :sourcefile , :sourceline , :generation )
269
-
270
- ALLOCATIONS = { } . compare_by_identity
271
-
272
- def trace_allocation ( object , class_path , method_id , sourcefile , sourceline , generation )
273
- allocation = Allocation . new ( class_path , method_id , sourcefile , sourceline , generation )
274
- TruffleRuby . synchronized ( ALLOCATIONS ) do
275
- ALLOCATIONS [ object ] = allocation
276
- end
216
+ Primitive . allocation_sourceline ( object )
277
217
end
278
- module_function :trace_allocation
279
-
280
218
end
0 commit comments