Debian Codegraph @

rpython/translator/c/test/test_boehm.py

raw symbols SCIP
1 import weakref
2
3 import py
4
5 from rpython.rlib import rgc, debug
6 from rpython.rlib.objectmodel import (keepalive_until_here, compute_unique_id,
7 compute_hash, current_object_addr_as_int)
8 from rpython.rtyper.lltypesystem import lltype, llmemory
9 from rpython.rtyper.lltypesystem.lloperation import llop
10 from rpython.rtyper.lltypesystem.rstr import STR
11 from rpython.translator.c.test.test_genc import compile
12
13
14 def setup_module(mod):
15 from rpython.rtyper.tool.rffi_platform import configure_boehm
16 from rpython.translator.platform import CompilationError
17 try:
18 configure_boehm()
19 except CompilationError:
20 py.test.skip("Boehm GC not present")
21
22
23 class AbstractGCTestClass(object):
24 gcpolicy = "boehm"
25 use_threads = False
26 extra_options = {}
27
28 # deal with cleanups
29 def setup_method(self, meth):
30 self._cleanups = []
31
32 def teardown_method(self, meth):
33 while self._cleanups:
34 #print "CLEANUP"
35 self._cleanups.pop()()
36
37 def getcompiled(self, func, argstypelist=[], annotatorpolicy=None,
38 extra_options={}):
39 return compile(func, argstypelist, gcpolicy=self.gcpolicy,
40 thread=self.use_threads, **extra_options)
41
42
43 class TestUsingBoehm(AbstractGCTestClass):
44 gcpolicy = "boehm"
45
46 def test_malloc_a_lot(self):
47 def malloc_a_lot():
48 i = 0
49 while i < 10:
50 i += 1
51 a = [1] * 10
52 j = 0
53 while j < 20:
54 j += 1
55 a.append(j)
56 fn = self.getcompiled(malloc_a_lot)
57 fn()
58
59 def test__del__(self):
60 class State:
61 pass
62 s = State()
63 class A(object):
64 def __del__(self):
65 s.a_dels += 1
66 class B(A):
67 def __del__(self):
68 s.b_dels += 1
69 class C(A):
70 pass
71 def f():
72 s.a_dels = 0
73 s.b_dels = 0
74 A()
75 B()
76 C()
77 A()
78 B()
79 C()
80 llop.gc__collect(lltype.Void)
81 return s.a_dels * 10 + s.b_dels
82 fn = self.getcompiled(f)
83 # we can't demand that boehm has collected all of the objects,
84 # even with the gc__collect call. calling the compiled
85 # function twice seems to help, though.
86 res = 0
87 res += fn()
88 res += fn()
89 # if res is still 0, then we haven't tested anything so fail.
90 # it might be the test's fault though.
91 assert 0 < res <= 84
92
93 def test_id_is_weak(self):
94 # test that compute_unique_id(obj) does not keep obj alive
95 class State:
96 pass
97 s = State()
98 class A(object):
99 def __del__(self):
100 s.a_dels += 1
101 class B(A):
102 def __del__(self):
103 s.b_dels += 1
104 class C(A):
105 pass
106 def run_once():
107 a = A()
108 ida = compute_unique_id(a)
109 b = B()
110 idb = compute_unique_id(b)
111 c = C()
112 idc = compute_unique_id(c)
113 llop.gc__collect(lltype.Void)
114 llop.gc__collect(lltype.Void)
115 llop.gc__collect(lltype.Void)
116 return ida, idb, idc
117 def f(n):
118 s.a_dels = 0
119 s.b_dels = 0
120 a1, b1, c1 = run_once()
121 a2, b2, c2 = run_once()
122 a3, b3, c3 = run_once()
123 a4, b4, c4 = run_once()
124 a5, b5, c5 = run_once()
125 return str((s.a_dels, s.b_dels,
126 a1, b1, c1,
127 a2, b2, c2,
128 a3, b3, c3,
129 a4, b4, c4,
130 a5, b5, c5))
131 fn = self.getcompiled(f, [int])
132 # we can't demand that boehm has collected all of the objects,
133 # even with the gc__collect call.
134 res = fn(50)
135 res1, res2 = eval(res)[:2]
136 # if res1 or res2 is still 0, then we haven't tested anything so fail.
137 # it might be the test's fault though.
138 print res1, res2
139 assert 0 < res1 <= 10
140 assert 0 < res2 <= 5
141
142 def test_del_raises(self):
143 class A(object):
144 def __del__(self):
145 s.dels += 1
146 raise Exception
147 class State:
148 pass
149 s = State()
150 s.dels = 0
151 def g():
152 a = A()
153 def f():
154 s.dels = 0
155 for i in range(10):
156 g()
157 llop.gc__collect(lltype.Void)
158 return s.dels
159 fn = self.getcompiled(f)
160 # we can't demand that boehm has collected all of the objects,
161 # even with the gc__collect call. calling the compiled
162 # function twice seems to help, though.
163 res = 0
164 res += fn()
165 res += fn()
166 # if res is still 0, then we haven't tested anything so fail.
167 # it might be the test's fault though.
168 assert res > 0
169
170 def test_memory_error_varsize(self):
171 N = int(2**31-1)
172 A = lltype.GcArray(lltype.Char)
173 def alloc(n):
174 return lltype.malloc(A, n)
175 def f():
176 try:
177 x = alloc(N)
178 except MemoryError:
179 y = alloc(10)
180 return len(y)
181 y = alloc(10)
182 return len(y) # allocation may work on 64 bits machines
183 fn = self.getcompiled(f)
184 res = fn()
185 assert res == 10
186
187 def test_zero_malloc(self):
188 T = lltype.GcStruct("C", ('x', lltype.Signed))
189 def fixed_size():
190 t = lltype.malloc(T, zero=True)
191 return t.x
192 c_fixed_size = self.getcompiled(fixed_size, [])
193 res = c_fixed_size()
194 assert res == 0
195 A = lltype.GcArray(lltype.Signed)
196 def var_size():
197 a = lltype.malloc(A, 1, zero=True)
198 return a[0]
199 c_var_size = self.getcompiled(var_size, [])
200 res = c_var_size()
201 assert res == 0
202
203 def test_gc_set_max_heap_size(self):
204 def g(n):
205 return 'x' * n
206 def fn():
207 from rpython.rlib import rgc
208 rgc.set_max_heap_size(500000)
209 s1 = s2 = s3 = None
210 try:
211 s1 = g(10000)
212 s2 = g(100000)
213 s3 = g(1000000)
214 except MemoryError:
215 pass
216 return (s1 is not None) + (s2 is not None) + (s3 is not None)
217 c_fn = self.getcompiled(fn, [])
218 res = c_fn()
219 assert res == 2
220
221 def test_weakref(self):
222 class A:
223 pass
224
225 def fn(n):
226 keepalive = []
227 weakrefs = []
228 a = None
229 for i in range(n):
230 if i & 1 == 0:
231 a = A()
232 a.index = i
233 assert a is not None
234 weakrefs.append(weakref.ref(a))
235 if i % 7 == 6:
236 keepalive.append(a)
237 rgc.collect()
238 count_free = 0
239 for i in range(n):
240 a = weakrefs[i]()
241 if i % 7 == 6:
242 assert a is not None
243 if a is not None:
244 assert a.index == i & ~1
245 else:
246 count_free += 1
247 keepalive_until_here(keepalive)
248 return count_free
249 c_fn = self.getcompiled(fn, [int])
250 res = c_fn(7000)
251 # more than half of them should have been freed, ideally up to 6000
252 assert 3500 <= res <= 6000
253
254 def test_prebuilt_weakref(self):
255 class A:
256 pass
257 a = A()
258 a.hello = 42
259 r1 = weakref.ref(a)
260 r2 = weakref.ref(A())
261 rgc.collect()
262 assert r2() is None
263 def fn(n):
264 if n:
265 r = r1
266 else:
267 r = r2
268 a = r()
269 rgc.collect()
270 if a is None:
271 return -5
272 else:
273 return a.hello
274 c_fn = self.getcompiled(fn, [int])
275 res = c_fn(1)
276 assert res == 42
277 res = c_fn(0)
278 assert res == -5
279
280 def test_weakref_to_prebuilt(self):
281 class A:
282 pass
283 a = A()
284 a.hello = 42
285 def fn(n):
286 lst = [weakref.ref(a) for i in range(n)]
287 rgc.collect()
288 for r in lst:
289 assert r() is a
290 c_fn = self.getcompiled(fn, [int])
291 c_fn(100)
292
293 def test_nested_finalizers(self):
294 class State:
295 pass
296 state = State()
297 def g():
298 n = state.counter
299 if n > 0:
300 for i in range(5):
301 state.a = A(n)
302 state.a = None
303 rgc.collect()
304 return n
305
306 fun = g
307 for i in range(200):
308 def fun(next=fun):
309 return next() + 1 # prevents tail-call optimization
310
311 class A:
312 def __init__(self, level):
313 self.level = level
314 def __del__(self):
315 if state.counter == self.level:
316 state.counter -= 1
317 fun()
318 def fn(n):
319 state.counter = n
320 fun()
321 return state.counter
322 c_fn = self.getcompiled(fn, [int])
323 res = c_fn(10000)
324 assert res == 0
325
326 def test_can_move(self):
327 class A:
328 pass
329 def fn():
330 return rgc.can_move(A())
331
332 c_fn = self.getcompiled(fn, [])
333 assert c_fn() == False
334
335 def test_heap_stats(self):
336 def fn():
337 return bool(rgc._heap_stats())
338
339 c_fn = self.getcompiled(fn, [])
340 assert not c_fn()
341
342 def test_shrink_array(self):
343 def f():
344 ptr = lltype.malloc(STR, 3)
345 ptr.hash = 0x62
346 ptr.chars[0] = '0'
347 ptr.chars[1] = 'B'
348 ptr.chars[2] = 'C'
349 ptr2 = rgc.ll_shrink_array(ptr, 2)
350 return ((ptr == ptr2) +
351 ord(ptr2.chars[0]) +
352 (ord(ptr2.chars[1]) << 8) +
353 (len(ptr2.chars) << 16) +
354 (ptr2.hash << 24))
355
356 run = self.getcompiled(f)
357 assert run() == 0x62024230
358
359 def test_write_barrier_nop(self):
360 S = lltype.GcStruct('S', ('x', lltype.Signed))
361 s = lltype.malloc(S)
362 s.x = 0
363 def f():
364 llop.gc_writebarrier(lltype.Void, llmemory.cast_ptr_to_adr(s))
365 return True
366 run = self.getcompiled(f)
367 assert run() == True
368
369 def test_hash_preservation(self):
370 class C:
371 pass
372 class D(C):
373 pass
374 c = C()
375 d = D()
376 compute_hash(d) # force to be cached on 'd', but not on 'c'
377 #
378 def fn():
379 d2 = D()
380 return str((compute_hash(d2),
381 current_object_addr_as_int(d2),
382 compute_hash(c),
383 compute_hash(d),
384 compute_hash(("Hi", None, (7.5, 2)))))
385
386 f = self.getcompiled(fn)
387 res = f()
388 res = eval(res)
389
390 # xxx the next line is too precise, checking the exact implementation
391 assert res[0] == ~res[1]
392 assert res[2] != compute_hash(c) # likely
393 assert res[3] != compute_hash(d) # likely *not* preserved
394 assert res[4] == compute_hash(("Hi", None, (7.5, 2)))
395 # ^^ true as long as we're using the default 'fnv' hash for strings
396 # and not e.g. siphash24
397
398 def test_finalizer_queue(self):
399 class A(object):
400 def __init__(self, i):
401 self.i = i
402 class Glob:
403 triggered = 0
404 glob = Glob()
405 class FQ(rgc.FinalizerQueue):
406 Class = A
407 triggered = 0
408 def finalizer_trigger(self):
409 glob.triggered += 1
410 fq = FQ()
411 #
412 def fn():
413 for i in range(1000):
414 x = A(i)
415 fq.register_finalizer(x)
416 rgc.may_ignore_finalizer(x) # this is ignored with Boehm
417 rgc.collect()
418 rgc.collect()
419 if glob.triggered == 0:
420 print "not triggered!"
421 return 50
422 seen = {}
423 while True:
424 a = fq.next_dead()
425 if a is None:
426 break
427 assert a.i not in seen
428 seen[a.i] = True
429 if len(seen) < 500:
430 print "seen only %d!" % len(seen)
431 return 51
432 return 42
433
434 f = self.getcompiled(fn)
435 res = f()
436 assert res == 42