Debian Codegraph @

pypy/module/gc/referents.py

raw symbols SCIP
1 from rpython.rlib import rgc, jit_hooks
2 from pypy.interpreter.baseobjspace import W_Root
3 from pypy.interpreter.typedef import TypeDef, interp_attrproperty
4 from pypy.interpreter.gateway import unwrap_spec, interp2app, WrappedDefault
5 from pypy.interpreter.error import oefmt, wrap_oserror
6 from rpython.rlib.objectmodel import we_are_translated
7 from pypy.module._hpy_universal.interp_field import is_hpy_object, hpy_get_referents
8
9
10 class W_GcRef(W_Root):
11 def __init__(self, gcref):
12 self.gcref = gcref
13
14 W_GcRef.typedef = TypeDef("GcRef")
15
16
17 def try_cast_gcref_to_w_root(gcref):
18 if rgc.get_gcflag_dummy(gcref):
19 return None
20 w_obj = rgc.try_cast_gcref_to_instance(W_Root, gcref)
21 # Ignore the instances of W_Root that are not really valid as Python
22 # objects. There is e.g. WeakrefLifeline in module/_weakref that
23 # inherits from W_Root for internal reasons. Such instances don't
24 # have a typedef at all (or have a null typedef after translation).
25 if not we_are_translated():
26 if getattr(w_obj, 'typedef', None) is None:
27 return None
28 else:
29 if w_obj is None or not w_obj.typedef:
30 return None
31 return w_obj
32
33 def wrap(space, gcref):
34 w_obj = try_cast_gcref_to_w_root(gcref)
35 if w_obj is None:
36 w_obj = W_GcRef(gcref)
37 return w_obj
38
39 def unwrap(space, w_obj):
40 if isinstance(w_obj, W_GcRef):
41 gcref = w_obj.gcref
42 else:
43 gcref = rgc.cast_instance_to_gcref(w_obj)
44 return gcref
45
46 def missing_operation(space):
47 return oefmt(space.w_NotImplementedError,
48 "operation not implemented by this GC")
49
50
51 # ____________________________________________________________
52
53 def _list_w_obj_referents(gcref, result_w):
54 # Get all W_Root reachable directly from gcref, and add them to
55 # the list 'result_w'.
56 pending = [] # = list of all objects whose gcflag was toggled
57 i = 0
58 gcrefparent = gcref
59 while True:
60 for gcref in rgc.get_rpy_referents(gcrefparent):
61 if rgc.get_gcflag_extra(gcref):
62 continue
63 rgc.toggle_gcflag_extra(gcref)
64 pending.append(gcref)
65
66 while i < len(pending):
67 gcrefparent = pending[i]
68 i += 1
69 w_obj = try_cast_gcref_to_w_root(gcrefparent)
70 if w_obj is not None:
71 result_w.append(w_obj)
72 else:
73 break # jump back to the start of the outermost loop
74 else:
75 break # done
76
77 for gcref in pending:
78 rgc.toggle_gcflag_extra(gcref) # reset the gcflag_extra's
79
80 # ____________________________________________________________
81
82 def get_rpy_roots(space):
83 lst = rgc.get_rpy_roots()
84 if lst is None:
85 raise missing_operation(space)
86 return space.newlist([wrap(space, gcref) for gcref in lst if gcref])
87
88 def get_rpy_referents(space, w_obj):
89 """Return a list of all the referents, as reported by the GC.
90 This is likely to contain a lot of GcRefs."""
91 gcref = unwrap(space, w_obj)
92 lst = rgc.get_rpy_referents(gcref)
93 if lst is None:
94 raise missing_operation(space)
95 return space.newlist([wrap(space, gcref) for gcref in lst])
96
97 def get_rpy_memory_usage(space, w_obj):
98 """Return the memory usage of just the given object or GcRef.
99 This does not include the internal structures of the object."""
100 gcref = unwrap(space, w_obj)
101 size = rgc.get_rpy_memory_usage(gcref)
102 if size < 0:
103 raise missing_operation(space)
104 return space.newint(size)
105
106 def get_rpy_type_index(space, w_obj):
107 """Return an integer identifying the RPython type of the given
108 object or GcRef. The number starts at 1; it is an index in the
109 file typeids.txt produced at translation."""
110 gcref = unwrap(space, w_obj)
111 index = rgc.get_rpy_type_index(gcref)
112 if index < 0:
113 raise missing_operation(space)
114 return space.newint(index)
115
116 @unwrap_spec(w_generation=WrappedDefault(None))
117 def get_objects(space, w_generation=None):
118 """Return a list of all app-level objects."""
119 space.audit('gc.get_objects', [space.newint(-1)])
120 if not space.is_w(w_generation, space.w_None):
121 raise oefmt(space.w_NotImplementedError,
122 "get_objects(generation=None) accepts only None on PyPy")
123 if not rgc.has_gcflag_extra():
124 raise missing_operation(space)
125 result_w = rgc.do_get_objects(try_cast_gcref_to_w_root)
126 return space.newlist(result_w)
127
128 def get_referents(space, args_w):
129 """Return a list of objects directly referred to by any of the arguments.
130 """
131 if not rgc.has_gcflag_extra():
132 raise missing_operation(space)
133 space.audit('gc.get_referents', args_w)
134 result_w = []
135 for w_obj in args_w:
136 if not we_are_translated() and is_hpy_object(w_obj):
137 # special case for hpy, only for tests. See
138 # e.g. test_fields.test_tp_traverse
139 result_w += hpy_get_referents(space, w_obj)
140 else:
141 # normal case
142 gcref = rgc.cast_instance_to_gcref(w_obj)
143 _list_w_obj_referents(gcref, result_w)
144 rgc.assert_no_more_gcflags()
145 return space.newlist(result_w)
146
147 def get_referrers(space, args_w):
148 """Return the list of objects that directly refer to any of objs."""
149 if not rgc.has_gcflag_extra():
150 raise missing_operation(space)
151 # xxx uses a lot of memory to make the list of all W_Root objects,
152 # but it's simpler this way and more correct than the previous
153 # version of this code (issue #2612). It is potentially very slow
154 # because each of the n calls to _list_w_obj_referents() could take
155 # O(n) time as well, in theory, but I hope in practice the whole
156 # thing takes much less than O(n^2). We could re-add an algorithm
157 # that visits most objects only once, if needed...
158 space.audit('gc.get_referrers', args_w)
159 all_objects_w = rgc.do_get_objects(try_cast_gcref_to_w_root)
160 result_w = []
161 for w_obj in all_objects_w:
162 refs_w = []
163 gcref = rgc.cast_instance_to_gcref(w_obj)
164 _list_w_obj_referents(gcref, refs_w)
165 for w_arg in args_w:
166 if w_arg in refs_w:
167 result_w.append(w_obj)
168 rgc.assert_no_more_gcflags()
169 return space.newlist(result_w)
170
171 @unwrap_spec(fd=int)
172 def _dump_rpy_heap(space, fd):
173 try:
174 ok = rgc.dump_rpy_heap(fd)
175 except OSError as e:
176 raise wrap_oserror(space, e)
177 if not ok:
178 raise missing_operation(space)
179
180 def get_typeids_z(space):
181 a = rgc.get_typeids_z()
182 s = ''.join([a[i] for i in range(len(a))])
183 return space.newbytes(s)
184
185 def get_typeids_list(space):
186 l = rgc.get_typeids_list()
187 list_w = [space.newint(l[i]) for i in range(len(l))]
188 return space.newlist(list_w)
189
190 class W_GcStats(W_Root):
191 def __init__(self, memory_pressure):
192 if memory_pressure:
193 self.total_memory_pressure = rgc.get_stats(rgc.TOTAL_MEMORY_PRESSURE)
194 else:
195 self.total_memory_pressure = -1
196 self.total_gc_memory = rgc.get_stats(rgc.TOTAL_MEMORY)
197 self.total_allocated_memory = rgc.get_stats(rgc.TOTAL_ALLOCATED_MEMORY)
198 self.peak_memory = rgc.get_stats(rgc.PEAK_MEMORY)
199 self.peak_allocated_memory = rgc.get_stats(rgc.PEAK_ALLOCATED_MEMORY)
200 self.jit_backend_allocated = jit_hooks.stats_asmmemmgr_allocated(None)
201 self.jit_backend_used = jit_hooks.stats_asmmemmgr_used(None)
202 self.total_arena_memory = rgc.get_stats(rgc.TOTAL_ARENA_MEMORY)
203 self.total_rawmalloced_memory = rgc.get_stats(
204 rgc.TOTAL_RAWMALLOCED_MEMORY)
205 self.peak_arena_memory = rgc.get_stats(rgc.PEAK_ARENA_MEMORY)
206 self.peak_rawmalloced_memory = rgc.get_stats(rgc.PEAK_RAWMALLOCED_MEMORY)
207 self.nursery_size = rgc.get_stats(rgc.NURSERY_SIZE)
208 self.total_gc_time = rgc.get_stats(rgc.TOTAL_GC_TIME)
209
210 W_GcStats.typedef = TypeDef("GcStats",
211 total_memory_pressure=interp_attrproperty("total_memory_pressure",
212 cls=W_GcStats, wrapfn="newint"),
213 total_gc_memory=interp_attrproperty("total_gc_memory",
214 cls=W_GcStats, wrapfn="newint"),
215 peak_allocated_memory=interp_attrproperty("peak_allocated_memory",
216 cls=W_GcStats, wrapfn="newint"),
217 peak_memory=interp_attrproperty("peak_memory",
218 cls=W_GcStats, wrapfn="newint"),
219 total_allocated_memory=interp_attrproperty("total_allocated_memory",
220 cls=W_GcStats, wrapfn="newint"),
221 jit_backend_allocated=interp_attrproperty("jit_backend_allocated",
222 cls=W_GcStats, wrapfn="newint"),
223 jit_backend_used=interp_attrproperty("jit_backend_used",
224 cls=W_GcStats, wrapfn="newint"),
225 total_arena_memory=interp_attrproperty("total_arena_memory",
226 cls=W_GcStats, wrapfn="newint"),
227 total_rawmalloced_memory=interp_attrproperty("total_rawmalloced_memory",
228 cls=W_GcStats, wrapfn="newint"),
229 peak_arena_memory=interp_attrproperty("peak_arena_memory",
230 cls=W_GcStats, wrapfn="newint"),
231 peak_rawmalloced_memory=interp_attrproperty("peak_rawmalloced_memory",
232 cls=W_GcStats, wrapfn="newint"),
233 nursery_size=interp_attrproperty("nursery_size",
234 cls=W_GcStats, wrapfn="newint"),
235 total_gc_time=interp_attrproperty("total_gc_time",
236 cls=W_GcStats, wrapfn="newint"),
237 )
238
239 @unwrap_spec(memory_pressure=bool)
240 def get_stats(space, memory_pressure=False):
241 return W_GcStats(memory_pressure)