Debian Codegraph @

rpython/jit/metainterp/test/test_memmgr.py

raw symbols SCIP
1 import sys
2 if len(sys.argv) >= 4 and sys.argv[1] == '--sub':
3 sys.path[:] = eval(sys.argv[2]) # hacks for test_integration
4 # XXX we don't invokve py.test machinery but try to make sure
5 # pypy support code sees the test options from the invoking
6 # process
7 import rpython.conftest
8 class opt:
9 pass
10 rpython.conftest.option = opt()
11 rpython.conftest.option.__dict__.update(eval(sys.argv[3]))
12
13 import py
14 from rpython.jit.metainterp.memmgr import MemoryManager
15 from rpython.jit.metainterp.test.support import LLJitMixin
16 from rpython.rlib.jit import JitDriver, dont_look_inside
17 from rpython.jit.metainterp.warmspot import get_stats
18 from rpython.jit.metainterp.warmstate import BaseJitCell
19 from rpython.rlib import rgc
20
21 class FakeLoopToken:
22 generation = 0
23 invalidated = False
24
25
26 class _TestMemoryManager:
27 # We spawn a fresh process below to lower the time it takes to do
28 # all these gc.collect() in memmgr.py. This issue is particularly
29 # important when running all the tests, because test_[a-l]*.py have
30 # left tons of stuff in memory. To get temporarily the normal
31 # behavior just rename this class to TestMemoryManager.
32
33 def test_disabled(self):
34 memmgr = MemoryManager()
35 memmgr.set_max_age(0)
36 tokens = [FakeLoopToken() for i in range(10)]
37 for token in tokens:
38 memmgr.keep_loop_alive(token)
39 memmgr.next_generation()
40 assert memmgr.alive_loops == dict.fromkeys(tokens)
41
42 def test_basic(self):
43 memmgr = MemoryManager()
44 memmgr.set_max_age(4, 1)
45 tokens = [FakeLoopToken() for i in range(10)]
46 for token in tokens:
47 memmgr.keep_loop_alive(token)
48 memmgr.next_generation()
49 assert memmgr.alive_loops == dict.fromkeys(tokens[7:])
50
51 def test_basic_2(self):
52 memmgr = MemoryManager()
53 memmgr.set_max_age(4, 1)
54 token = FakeLoopToken()
55 memmgr.keep_loop_alive(token)
56 for i in range(10):
57 memmgr.next_generation()
58 if i < 3:
59 assert memmgr.alive_loops == {token: None}
60 else:
61 assert memmgr.alive_loops == {}
62
63 def test_basic_3(self):
64 memmgr = MemoryManager()
65 memmgr.set_max_age(4, 1)
66 tokens = [FakeLoopToken() for i in range(10)]
67 for i in range(len(tokens)):
68 print 'record tokens[%d]' % i
69 memmgr.keep_loop_alive(tokens[i])
70 memmgr.next_generation()
71 for j in range(0, i, 2):
72 assert tokens[j] in memmgr.alive_loops
73 print 'also keep alive tokens[%d]' % j
74 memmgr.keep_loop_alive(tokens[j])
75 for i in range(len(tokens)):
76 if i < 7 and (i%2) != 0:
77 assert tokens[i] not in memmgr.alive_loops
78 else:
79 assert tokens[i] in memmgr.alive_loops
80
81
82 class _TestIntegration(LLJitMixin):
83 # See comments in TestMemoryManager. To get temporarily the normal
84 # behavior just rename this class to TestIntegration.
85
86 # We need an extra rgc.collect in get_procedure_token() for some of
87 # these tests to pass. But we dont want it there always since that will
88 # make all other tests take forever.
89 def setup_class(cls):
90 original_get_procedure_token = BaseJitCell.get_procedure_token
91 def get_procedure_token(self):
92 rgc.collect();
93 return original_get_procedure_token(self)
94 BaseJitCell.get_procedure_token = get_procedure_token
95 cls.original_get_procedure_token = original_get_procedure_token
96
97 def teardown_class(cls):
98 BaseJitCell.get_procedure_token = cls.original_get_procedure_token
99
100 def test_loop_kept_alive(self):
101 myjitdriver = JitDriver(greens=[], reds=['n'])
102 def g():
103 n = 10
104 while n > 0:
105 myjitdriver.can_enter_jit(n=n)
106 myjitdriver.jit_merge_point(n=n)
107 n = n - 1
108 return 21
109 def f():
110 for i in range(15):
111 g()
112 return 42
113
114 res = self.meta_interp(f, [], loop_longevity=2)
115 assert res == 42
116
117 # we should see only the loop and the entry bridge
118 self.check_target_token_count(2)
119
120 def test_target_loop_kept_alive_or_not(self):
121 myjitdriver = JitDriver(greens=['m'], reds=['n'])
122 def g(m):
123 n = 10
124 while n > 0:
125 myjitdriver.can_enter_jit(n=n, m=m)
126 myjitdriver.jit_merge_point(n=n, m=m)
127 n = n - 1
128 return 21
129 def f():
130 # If loop_longevity is large enough, this creates a loop
131 # and an entry bridge for 'g(7)', and another for 'g(5)':
132 # total 4. If loop_longevity is set to 1 instead, whenever
133 # we create a loop for 'g(7)' we forget the loop created
134 # for 'g(5)' and vice-versa. We end up creating loops
135 # over and over again, for a total of 40 of them.
136 for i in range(20):
137 g(7)
138 g(5)
139 return 42
140
141 # case A
142 res = self.meta_interp(f, [], loop_longevity=3)
143 assert res == 42
144 # we should see (1) the loop-with-preamble, (2) the exit bridge
145 # for g(7), and another time the same for g(5).
146 self.check_enter_count(4)
147
148 # case B, with a lower longevity
149 res = self.meta_interp(f, [], loop_longevity=1)
150 assert res == 42
151 # we should see a loop for each call to g()
152 self.check_enter_count(40)
153
154 def test_target_loop_kept_alive_or_not_2(self):
155 myjitdriver = JitDriver(greens=['m'], reds=['n'])
156 def g(m):
157 n = 10
158 while n > 0:
159 myjitdriver.can_enter_jit(n=n, m=m)
160 myjitdriver.jit_merge_point(n=n, m=m)
161 n = n - 1
162 return 21
163 def f():
164 # If loop_longevity is large enough, this creates a loop
165 # and an entry bridge for 'g(7)', and another for 'g(5)':
166 # total 4. If loop_longevity is set to 1 instead, whenever
167 # we create a loop for 'g(7)', we create the entry bridge
168 # on the next 'g(7)', but we forget them both when we move
169 # on to 'g(5)', and vice-versa. We end up creating loops
170 # and entry bridges over and over again, for a total of 32
171 # of them.
172 for i in range(8):
173 g(7); g(7)
174 g(5); g(5)
175 return 42
176
177 # case A
178 res = self.meta_interp(f, [], loop_longevity=5)
179 assert res == 42
180 # we should see (1) the loop-with-preamble, (2) the exit bridge
181 # for g(7), and another time the same for g(5).
182 self.check_enter_count(4)
183
184 # case B, with a lower longevity
185 res = self.meta_interp(f, [], loop_longevity=1)
186 assert res == 42
187 # we should see a loop for each call to g()
188 self.check_enter_count(32)
189
190 def test_throw_away_old_loops(self):
191 myjitdriver = JitDriver(greens=['m'], reds=['n'])
192 def g(m):
193 n = 10
194 while n > 0:
195 myjitdriver.can_enter_jit(n=n, m=m)
196 myjitdriver.jit_merge_point(n=n, m=m)
197 n = n - 1
198 return 21
199 def f():
200 for i in range(10):
201 g(1) # g(1) gets a loop with an entry bridge
202 g(2) # and an exit bridge, stays alive
203 g(1)
204 g(3)
205 g(1)
206 g(4) # g(2), g(3), g(4), g(5) are thrown away every iteration
207 g(1) # (no entry bridge for them)
208 g(5)
209 return 42
210
211 res = self.meta_interp(f, [], loop_longevity=3)
212 assert res == 42
213 self.check_enter_count(2 + 10*4)
214
215 def test_call_assembler_keep_alive(self):
216 myjitdriver1 = JitDriver(greens=['m'], reds=['n'])
217 myjitdriver2 = JitDriver(greens=['m'], reds=['n', 'rec'])
218 def h(m, n):
219 while True:
220 myjitdriver1.can_enter_jit(n=n, m=m)
221 myjitdriver1.jit_merge_point(n=n, m=m)
222 n = n >> 1
223 if n == 0:
224 return 21
225 def g(m, rec):
226 n = 5
227 while n > 0:
228 myjitdriver2.can_enter_jit(n=n, m=m, rec=rec)
229 myjitdriver2.jit_merge_point(n=n, m=m, rec=rec)
230 if rec:
231 h(m, rec)
232 n = n - 1
233 return 21
234 def f(u):
235 for i in range(8):
236 h(u, 32) # make a loop and an exit bridge for h(u)
237 g(u, 8) # make a loop for g(u) with a call_assembler
238 g(u, 0); g(u+1, 0) # \
239 g(u, 0); g(u+2, 0) # \ make more loops for g(u+1) to g(u+4),
240 g(u, 0); g(u+3, 0) # / but keeps g(u) alive
241 g(u, 0); g(u+4, 0) # /
242 g(u, 8) # call g(u) again, with its call_assembler to h(u)
243 return 42
244
245 res = self.meta_interp(f, [1], loop_longevity=4, inline=True)
246 assert res == 42
247 self.check_jitcell_token_count(6)
248 tokens = [t() for t in get_stats().jitcell_token_wrefs]
249 # Some loops have been freed
250 assert None in tokens
251 # Loop with number 1, h(), has not been freed
252 assert 1 in [t.number for t in tokens if t]
253
254 # ____________________________________________________________
255
256 def test_all():
257 if sys.platform == 'win32':
258 py.test.skip(
259 "passing repr() to subprocess.Popen probably doesn't work")
260 import os, subprocess
261 from rpython.conftest import option
262 thisfile = os.path.abspath(__file__)
263 p = subprocess.Popen([sys.executable, thisfile,
264 '--sub', repr(sys.path), repr(option.__dict__)])
265 result = p.wait()
266 assert result == 0
267
268 if __name__ == '__main__':
269 # occurs in the subprocess
270 for test in [_TestMemoryManager(), _TestIntegration()]:
271 if hasattr(test, 'setup_class'):
272 test.setup_class()
273 try:
274 for name in dir(test):
275 if name.startswith('test_'):
276 print
277 print '-'*79
278 print '----- Now running test', name, '-----'
279 print
280 getattr(test, name)()
281 finally:
282 if hasattr(test, 'teardown_class'):
283 test.teardown_class()
284