| 1 |
from rpython.rtyper.lltypesystem import lltype, llmemory |
| 2 |
from rpython.memory.gc.hook import GcHooks |
| 3 |
from rpython.memory.gc.test.test_direct import BaseDirectGCTest, S |
| 4 |
|
| 5 |
|
| 6 |
class MyGcHooks(GcHooks): |
| 7 |
|
| 8 |
def __init__(self): |
| 9 |
GcHooks.__init__(self) |
| 10 |
self._gc_minor_enabled = False |
| 11 |
self._gc_collect_step_enabled = False |
| 12 |
self._gc_collect_enabled = False |
| 13 |
self.reset() |
| 14 |
|
| 15 |
def is_gc_minor_enabled(self): |
| 16 |
return self._gc_minor_enabled |
| 17 |
|
| 18 |
def is_gc_collect_step_enabled(self): |
| 19 |
return self._gc_collect_step_enabled |
| 20 |
|
| 21 |
def is_gc_collect_enabled(self): |
| 22 |
return self._gc_collect_enabled |
| 23 |
|
| 24 |
def reset(self): |
| 25 |
self.minors = [] |
| 26 |
self.steps = [] |
| 27 |
self.collects = [] |
| 28 |
self.durations = [] |
| 29 |
|
| 30 |
def on_gc_minor(self, duration, total_memory_used, pinned_objects): |
| 31 |
self.durations.append(duration) |
| 32 |
self.minors.append({ |
| 33 |
'total_memory_used': total_memory_used, |
| 34 |
'pinned_objects': pinned_objects}) |
| 35 |
|
| 36 |
def on_gc_collect_step(self, duration, oldstate, newstate): |
| 37 |
self.durations.append(duration) |
| 38 |
self.steps.append({ |
| 39 |
'oldstate': oldstate, |
| 40 |
'newstate': newstate}) |
| 41 |
|
| 42 |
def on_gc_collect(self, num_major_collects, |
| 43 |
arenas_count_before, arenas_count_after, |
| 44 |
arenas_bytes, rawmalloc_bytes_before, |
| 45 |
rawmalloc_bytes_after, pinned_objects): |
| 46 |
self.collects.append({ |
| 47 |
'num_major_collects': num_major_collects, |
| 48 |
'arenas_count_before': arenas_count_before, |
| 49 |
'arenas_count_after': arenas_count_after, |
| 50 |
'arenas_bytes': arenas_bytes, |
| 51 |
'rawmalloc_bytes_before': rawmalloc_bytes_before, |
| 52 |
'rawmalloc_bytes_after': rawmalloc_bytes_after, |
| 53 |
'pinned_objects': pinned_objects, |
| 54 |
}) |
| 55 |
|
| 56 |
|
| 57 |
class TestIncMiniMarkHooks(BaseDirectGCTest): |
| 58 |
from rpython.memory.gc.incminimark import IncrementalMiniMarkGC as GCClass |
| 59 |
|
| 60 |
def get_extra_gc_params(self): |
| 61 |
return {'hooks': MyGcHooks()} |
| 62 |
|
| 63 |
def setup_method(self, m): |
| 64 |
BaseDirectGCTest.setup_method(self, m) |
| 65 |
size = llmemory.sizeof(S) + self.gc.gcheaderbuilder.size_gc_header |
| 66 |
self.size_of_S = llmemory.raw_malloc_usage(size) |
| 67 |
|
| 68 |
def test_on_gc_minor(self): |
| 69 |
self.gc.hooks._gc_minor_enabled = True |
| 70 |
self.malloc(S) |
| 71 |
self.gc._minor_collection() |
| 72 |
assert self.gc.hooks.minors == [ |
| 73 |
{'total_memory_used': 0, 'pinned_objects': 0} |
| 74 |
] |
| 75 |
assert self.gc.hooks.durations[0] > 0. |
| 76 |
self.gc.hooks.reset() |
| 77 |
|
| 78 |
|
| 79 |
self.stackroots.append(self.malloc(S)) |
| 80 |
self.stackroots.append(self.malloc(S)) |
| 81 |
self.gc._minor_collection() |
| 82 |
assert self.gc.hooks.minors == [ |
| 83 |
{'total_memory_used': self.size_of_S*2, 'pinned_objects': 0} |
| 84 |
] |
| 85 |
|
| 86 |
def test_on_gc_collect(self): |
| 87 |
from rpython.memory.gc import incminimark as m |
| 88 |
self.gc.hooks._gc_collect_step_enabled = True |
| 89 |
self.gc.hooks._gc_collect_enabled = True |
| 90 |
self.malloc(S) |
| 91 |
self.gc.collect() |
| 92 |
assert self.gc.hooks.steps == [ |
| 93 |
{'oldstate': m.STATE_SCANNING, 'newstate': m.STATE_MARKING}, |
| 94 |
{'oldstate': m.STATE_MARKING, 'newstate': m.STATE_SWEEPING}, |
| 95 |
{'oldstate': m.STATE_SWEEPING, 'newstate': m.STATE_FINALIZING}, |
| 96 |
{'oldstate': m.STATE_FINALIZING, 'newstate': m.STATE_SCANNING} |
| 97 |
] |
| 98 |
assert self.gc.hooks.collects == [ |
| 99 |
{'num_major_collects': 1, |
| 100 |
'arenas_count_before': 0, |
| 101 |
'arenas_count_after': 0, |
| 102 |
'arenas_bytes': 0, |
| 103 |
'rawmalloc_bytes_after': 0, |
| 104 |
'rawmalloc_bytes_before': 0, |
| 105 |
'pinned_objects': 0, |
| 106 |
} |
| 107 |
] |
| 108 |
assert len(self.gc.hooks.durations) == 4 |
| 109 |
for d in self.gc.hooks.durations: |
| 110 |
assert d > 0.0 |
| 111 |
self.gc.hooks.reset() |
| 112 |
|
| 113 |
self.stackroots.append(self.malloc(S)) |
| 114 |
self.gc.collect() |
| 115 |
assert self.gc.hooks.collects == [ |
| 116 |
{'num_major_collects': 2, |
| 117 |
'arenas_count_before': 1, |
| 118 |
'arenas_count_after': 1, |
| 119 |
'arenas_bytes': self.size_of_S, |
| 120 |
'rawmalloc_bytes_after': 0, |
| 121 |
'rawmalloc_bytes_before': 0, |
| 122 |
'pinned_objects': 0, |
| 123 |
} |
| 124 |
] |
| 125 |
|
| 126 |
def test_hook_disabled(self): |
| 127 |
self.gc._minor_collection() |
| 128 |
self.gc.collect() |
| 129 |
assert self.gc.hooks.minors == [] |
| 130 |
assert self.gc.hooks.steps == [] |
| 131 |
assert self.gc.hooks.collects == [] |