| 1 |
from pypy.interpreter.gateway import unwrap_spec |
| 2 |
from pypy.interpreter.error import oefmt |
| 3 |
from rpython.rlib import rgc |
| 4 |
from pypy.module.gc.hook import W_GcCollectStepStats |
| 5 |
|
| 6 |
|
| 7 |
@unwrap_spec(generation=int) |
| 8 |
def collect(space, generation=0): |
| 9 |
"Run a full collection. The optional argument is ignored." |
| 10 |
|
| 11 |
|
| 12 |
from pypy.objspace.std.typeobject import MethodCache |
| 13 |
from pypy.objspace.std.mapdict import MapAttrCache |
| 14 |
cache = space.fromcache(MethodCache) |
| 15 |
cache.clear() |
| 16 |
cache = space.fromcache(MapAttrCache) |
| 17 |
cache.clear() |
| 18 |
|
| 19 |
rgc.collect() |
| 20 |
_run_finalizers(space) |
| 21 |
if space.config.objspace.usemodules.cpyext: |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
from pypy.module.cpyext.state import _rawrefcount_perform |
| 26 |
_rawrefcount_perform(space) |
| 27 |
|
| 28 |
def _run_finalizers(space): |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
temp_reenable = not space.user_del_action.enabled_at_app_level |
| 36 |
if temp_reenable: |
| 37 |
enable_finalizers(space) |
| 38 |
try: |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
space.user_del_action._run_finalizers() |
| 44 |
finally: |
| 45 |
if temp_reenable: |
| 46 |
disable_finalizers(space) |
| 47 |
|
| 48 |
return space.newint(0) |
| 49 |
|
| 50 |
def enable(space): |
| 51 |
"""Non-recursive version. Enable major collections and finalizers. |
| 52 |
If they were already enabled, no-op. |
| 53 |
If they were disabled even several times, enable them anyway. |
| 54 |
""" |
| 55 |
rgc.enable() |
| 56 |
if not space.user_del_action.enabled_at_app_level: |
| 57 |
space.user_del_action.enabled_at_app_level = True |
| 58 |
enable_finalizers(space) |
| 59 |
|
| 60 |
def disable(space): |
| 61 |
"""Non-recursive version. Disable major collections and finalizers. |
| 62 |
Multiple calls to this function are ignored. |
| 63 |
""" |
| 64 |
rgc.disable() |
| 65 |
if space.user_del_action.enabled_at_app_level: |
| 66 |
space.user_del_action.enabled_at_app_level = False |
| 67 |
disable_finalizers(space) |
| 68 |
|
| 69 |
def isenabled(space): |
| 70 |
return space.newbool(space.user_del_action.enabled_at_app_level) |
| 71 |
|
| 72 |
def enable_finalizers(space): |
| 73 |
uda = space.user_del_action |
| 74 |
if uda.finalizers_lock_count == 0: |
| 75 |
raise oefmt(space.w_ValueError, "finalizers are already enabled") |
| 76 |
uda.finalizers_lock_count -= 1 |
| 77 |
if uda.finalizers_lock_count == 0: |
| 78 |
pending = uda.pending_with_disabled_del |
| 79 |
uda.pending_with_disabled_del = None |
| 80 |
if pending is not None: |
| 81 |
for i in range(len(pending)): |
| 82 |
uda._call_finalizer(pending[i]) |
| 83 |
pending[i] = None |
| 84 |
|
| 85 |
def disable_finalizers(space): |
| 86 |
uda = space.user_del_action |
| 87 |
uda.finalizers_lock_count += 1 |
| 88 |
if uda.pending_with_disabled_del is None: |
| 89 |
uda.pending_with_disabled_del = [] |
| 90 |
|
| 91 |
|
| 92 |
class StepCollector(object): |
| 93 |
""" |
| 94 |
Invoke rgc.collect_step() until we are done, then run the app-level |
| 95 |
finalizers as a separate step |
| 96 |
""" |
| 97 |
|
| 98 |
def __init__(self, space): |
| 99 |
self.space = space |
| 100 |
self.finalizing = False |
| 101 |
|
| 102 |
def do(self): |
| 103 |
if self.finalizing: |
| 104 |
self._run_finalizers() |
| 105 |
self.finalizing = False |
| 106 |
oldstate = W_GcCollectStepStats.STATE_USERDEL |
| 107 |
newstate = W_GcCollectStepStats.STATE_SCANNING |
| 108 |
major_is_done = True |
| 109 |
else: |
| 110 |
states = self._collect_step() |
| 111 |
oldstate = rgc.old_state(states) |
| 112 |
newstate = rgc.new_state(states) |
| 113 |
major_is_done = False |
| 114 |
if rgc.is_done(states): |
| 115 |
newstate = W_GcCollectStepStats.STATE_USERDEL |
| 116 |
self.finalizing = True |
| 117 |
|
| 118 |
duration = -1 |
| 119 |
return W_GcCollectStepStats( |
| 120 |
count = 1, |
| 121 |
duration = duration, |
| 122 |
duration_min = duration, |
| 123 |
duration_max = duration, |
| 124 |
oldstate = oldstate, |
| 125 |
newstate = newstate, |
| 126 |
major_is_done = major_is_done) |
| 127 |
|
| 128 |
def _collect_step(self): |
| 129 |
return rgc.collect_step() |
| 130 |
|
| 131 |
def _run_finalizers(self): |
| 132 |
_run_finalizers(self.space) |
| 133 |
|
| 134 |
def collect_step(space): |
| 135 |
""" |
| 136 |
If the GC is incremental, run a single gc-collect-step. Return True when |
| 137 |
the major collection is completed. |
| 138 |
If the GC is not incremental, do a full collection and return True. |
| 139 |
""" |
| 140 |
sc = space.fromcache(StepCollector) |
| 141 |
w_stats = sc.do() |
| 142 |
return w_stats |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
@unwrap_spec(filename='fsencode') |
| 147 |
def dump_heap_stats(space, filename): |
| 148 |
tb = rgc._heap_stats() |
| 149 |
if not tb: |
| 150 |
raise oefmt(space.w_RuntimeError, "Wrong GC") |
| 151 |
f = open(filename, mode="w") |
| 152 |
for i in range(len(tb)): |
| 153 |
f.write("%d %d " % (tb[i].count, tb[i].size)) |
| 154 |
f.write(",".join([str(tb[i].links[j]) for j in range(len(tb))]) + "\n") |
| 155 |
f.close() |