Debian Codegraph @

pypy/module/gc/test/test_hook.py

raw symbols SCIP
1 import pytest
2 from rpython.rlib.rarithmetic import r_uint
3 from pypy.module.gc.hook import LowLevelGcHooks
4 from pypy.interpreter.baseobjspace import ObjSpace
5 from pypy.interpreter.gateway import interp2app, unwrap_spec
6
7 class AppTestGcHooks(object):
8
9 def setup_class(cls):
10 if cls.runappdirect:
11 pytest.skip("these tests cannot work with -A")
12 space = cls.space
13 gchooks = space.fromcache(LowLevelGcHooks)
14
15 @unwrap_spec(ObjSpace, int, r_uint, int)
16 def fire_gc_minor(space, duration, total_memory_used, pinned_objects):
17 gchooks.fire_gc_minor(duration, total_memory_used, pinned_objects)
18
19 @unwrap_spec(ObjSpace, int, int, int)
20 def fire_gc_collect_step(space, duration, oldstate, newstate):
21 gchooks.fire_gc_collect_step(duration, oldstate, newstate)
22
23 @unwrap_spec(ObjSpace, int, int, int, r_uint, r_uint, r_uint, r_uint)
24 def fire_gc_collect(space, a, b, c, d, e, f, g):
25 gchooks.fire_gc_collect(a, b, c, d, e, f, g)
26
27 @unwrap_spec(ObjSpace)
28 def fire_many(space):
29 gchooks.fire_gc_minor(5.0, 0, 0)
30 gchooks.fire_gc_minor(7.0, 0, 0)
31 gchooks.fire_gc_collect_step(5.0, 0, 0)
32 gchooks.fire_gc_collect_step(15.0, 0, 0)
33 gchooks.fire_gc_collect_step(22.0, 0, 0)
34 gchooks.fire_gc_collect(1, 2, 3, 4, 5, 6, 7)
35
36 cls.w_fire_gc_minor = space.wrap(interp2app(fire_gc_minor))
37 cls.w_fire_gc_collect_step = space.wrap(interp2app(fire_gc_collect_step))
38 cls.w_fire_gc_collect = space.wrap(interp2app(fire_gc_collect))
39 cls.w_fire_many = space.wrap(interp2app(fire_many))
40
41 def test_default(self):
42 import gc
43 assert gc.hooks.on_gc_minor is None
44 assert gc.hooks.on_gc_collect_step is None
45 assert gc.hooks.on_gc_collect is None
46
47 def test_on_gc_minor(self):
48 import gc
49 lst = []
50 def on_gc_minor(stats):
51 lst.append((stats.count,
52 stats.duration,
53 stats.total_memory_used,
54 stats.pinned_objects))
55 gc.hooks.on_gc_minor = on_gc_minor
56 self.fire_gc_minor(10, 20, 30)
57 self.fire_gc_minor(40, 50, 60)
58 assert lst == [
59 (1, 10, 20, 30),
60 (1, 40, 50, 60),
61 ]
62 #
63 gc.hooks.on_gc_minor = None
64 self.fire_gc_minor(70, 80, 90) # won't fire because the hooks is disabled
65 assert lst == [
66 (1, 10, 20, 30),
67 (1, 40, 50, 60),
68 ]
69
70 def test_on_gc_collect_step(self):
71 import gc
72 SCANNING = 0
73 MARKING = 1
74 SWEEPING = 2
75 FINALIZING = 3
76 lst = []
77 def on_gc_collect_step(stats):
78 lst.append((stats.count,
79 stats.duration,
80 stats.oldstate,
81 stats.newstate,
82 stats.major_is_done))
83 gc.hooks.on_gc_collect_step = on_gc_collect_step
84 self.fire_gc_collect_step(10, SCANNING, MARKING)
85 self.fire_gc_collect_step(40, FINALIZING, SCANNING)
86 assert lst == [
87 (1, 10, SCANNING, MARKING, False),
88 (1, 40, FINALIZING, SCANNING, True),
89 ]
90 #
91 gc.hooks.on_gc_collect_step = None
92 oldlst = lst[:]
93 self.fire_gc_collect_step(70, SCANNING, MARKING) # won't fire
94 assert lst == oldlst
95
96 def test_on_gc_collect(self):
97 import gc
98 lst = []
99 def on_gc_collect(stats):
100 lst.append((stats.count,
101 stats.num_major_collects,
102 stats.arenas_count_before,
103 stats.arenas_count_after,
104 stats.arenas_bytes,
105 stats.rawmalloc_bytes_before,
106 stats.rawmalloc_bytes_after,
107 stats.pinned_objects))
108 gc.hooks.on_gc_collect = on_gc_collect
109 self.fire_gc_collect(1, 2, 3, 4, 5, 6, 20)
110 self.fire_gc_collect(7, 8, 9, 10, 11, 12, 21)
111 assert lst == [
112 (1, 1, 2, 3, 4, 5, 6, 20),
113 (1, 7, 8, 9, 10, 11, 12, 21),
114 ]
115 #
116 gc.hooks.on_gc_collect = None
117 self.fire_gc_collect(42, 42, 42, 42, 42, 42, 43) # won't fire
118 assert lst == [
119 (1, 1, 2, 3, 4, 5, 6, 20),
120 (1, 7, 8, 9, 10, 11, 12, 21),
121 ]
122
123 def test_consts(self):
124 import gc
125 S = gc.GcCollectStepStats
126 assert S.STATE_SCANNING == 0
127 assert S.STATE_MARKING == 1
128 assert S.STATE_SWEEPING == 2
129 assert S.STATE_FINALIZING == 3
130 assert S.GC_STATES == ('SCANNING', 'MARKING', 'SWEEPING',
131 'FINALIZING', 'USERDEL')
132
133 def test_cumulative(self):
134 import gc
135 class MyHooks(object):
136
137 def __init__(self):
138 self.minors = []
139 self.steps = []
140
141 def on_gc_minor(self, stats):
142 self.minors.append((stats.count, stats.duration,
143 stats.duration_min, stats.duration_max))
144
145 def on_gc_collect_step(self, stats):
146 self.steps.append((stats.count, stats.duration,
147 stats.duration_min, stats.duration_max))
148
149 on_gc_collect = None
150
151 myhooks = MyHooks()
152 gc.hooks.set(myhooks)
153 self.fire_many()
154 assert myhooks.minors == [(2, 12, 5, 7)]
155 assert myhooks.steps == [(3, 42, 5, 22)]
156
157 def test_clear_queue(self):
158 import gc
159 class MyHooks(object):
160
161 def __init__(self):
162 self.lst = []
163
164 def on_gc_minor(self, stats):
165 self.lst.append('minor')
166
167 def on_gc_collect_step(self, stats):
168 self.lst.append('step')
169
170 def on_gc_collect(self, stats):
171 self.lst.append('collect')
172
173 myhooks = MyHooks()
174 gc.hooks.set(myhooks)
175 self.fire_many()
176 assert myhooks.lst == ['minor', 'step', 'collect']
177 myhooks.lst[:] = []
178 self.fire_gc_minor(0, 0, 0)
179 assert myhooks.lst == ['minor']
180 gc.hooks.reset()
181 assert gc.hooks.on_gc_minor is None
182 assert gc.hooks.on_gc_collect_step is None
183 assert gc.hooks.on_gc_collect is None
184
185 def test_no_recursive(self):
186 import gc
187 lst = []
188 def on_gc_minor(stats):
189 lst.append((stats.count,
190 stats.duration,
191 stats.total_memory_used,
192 stats.pinned_objects))
193 self.fire_gc_minor(1, 2, 3) # won't fire NOW
194 gc.hooks.on_gc_minor = on_gc_minor
195 self.fire_gc_minor(10, 20, 30)
196 self.fire_gc_minor(40, 50, 60)
197 # the duration for the 2nd call is 41, because it also counts the 1
198 # which was fired recursively
199 assert lst == [
200 (1, 10, 20, 30),
201 (2, 41, 50, 60),
202 ]