Debian Codegraph @

rpython/memory/test/snippet.py

raw symbols SCIP
1 from __future__ import print_function
2
3 import os, py
4 from rpython.tool.udir import udir
5 from rpython.rlib import rgc
6 from rpython.rtyper.lltypesystem import lltype
7 from rpython.rtyper.lltypesystem.lloperation import llop
8
9
10 class SemiSpaceGCTestDefines:
11 large_tests_ok = False
12
13 def definestr_finalizer_order(cls):
14 import random
15 from rpython.tool.algo import graphlib
16
17 cls.finalizer_order_examples = examples = []
18 if cls.large_tests_ok:
19 letters = 'abcdefghijklmnopqrstuvwxyz'
20 COUNT = 20
21 else:
22 letters = 'abcdefghijklm'
23 COUNT = 2
24 for i in range(COUNT):
25 input = []
26 edges = {}
27 for c in letters:
28 edges[c] = []
29 # make up a random graph
30 for c in letters:
31 for j in range(random.randrange(0, 4)):
32 d = random.choice(letters)
33 edges[c].append(graphlib.Edge(c, d))
34 input.append((c, d))
35 # find the expected order in which destructors should be called
36 components = list(graphlib.strong_components(edges, edges))
37 head = {}
38 for component in components:
39 c = component.keys()[0]
40 for d in component:
41 assert d not in head
42 head[d] = c
43 assert len(head) == len(letters)
44 strict = []
45 for c, d in input:
46 if head[c] != head[d]:
47 strict.append((c, d))
48 examples.append((input, components, strict))
49
50 class State:
51 pass
52 state = State()
53 def age_of(c):
54 return state.age[ord(c) - ord('a')]
55 def set_age_of(c, newvalue):
56 # NB. this used to be a dictionary, but setting into a dict
57 # consumes memory. This has the effect that this test's
58 # finalizer_trigger method can consume more memory and potentially
59 # cause another collection. This would result in objects
60 # being unexpectedly destroyed at the same 'state.time'.
61 state.age[ord(c) - ord('a')] = newvalue
62
63 class A:
64 def __init__(self, key):
65 self.key = key
66 self.refs = []
67 fq.register_finalizer(self)
68
69 class FQ(rgc.FinalizerQueue):
70 Class = A
71 def finalizer_trigger(self):
72 from rpython.rlib.debug import debug_print
73 while True:
74 a = self.next_dead()
75 if a is None:
76 break
77 debug_print("DEL:", a.key)
78 assert age_of(a.key) == -1
79 set_age_of(a.key, state.time)
80 state.progress = True
81 fq = FQ()
82
83 def build_example(input):
84 state.time = 0
85 state.age = [-1] * len(letters)
86 vertices = {}
87 for c in letters:
88 vertices[c] = A(c)
89 for c, d in input:
90 vertices[c].refs.append(vertices[d])
91
92 def f(_):
93 i = 0
94 while i < len(examples):
95 input, components, strict = examples[i]
96 build_example(input)
97 while state.time < len(letters):
98 from rpython.rlib.debug import debug_print
99 debug_print("STATE.TIME:", state.time)
100 state.progress = False
101 llop.gc__collect(lltype.Void)
102 if not state.progress:
103 break
104 state.time += 1
105 # summarize the finalization order
106 lst = []
107 for c in letters:
108 lst.append('%s:%d' % (c, age_of(c)))
109 summary = ', '.join(lst)
110
111 # check that all instances have been finalized
112 if -1 in state.age:
113 return error(i, summary, "not all instances finalized")
114 # check that if a -> b and a and b are not in the same
115 # strong component, then a is finalized strictly before b
116 for c, d in strict:
117 if age_of(c) >= age_of(d):
118 return error(i, summary,
119 "%s should be finalized before %s"
120 % (c, d))
121 # check that two instances in the same strong component
122 # are never finalized during the same collection
123 for component in components:
124 seen = {}
125 for c in component:
126 age = age_of(c)
127 if age in seen:
128 d = seen[age]
129 return error(i, summary,
130 "%s and %s should not be finalized"
131 " at the same time" % (c, d))
132 seen[age] = c
133 i += 1
134 return "ok"
135
136 def error(i, summary, msg):
137 return '%d\n%s\n%s' % (i, summary, msg)
138
139 return f
140
141 def test_finalizer_order(self):
142 res = self.run('finalizer_order')
143 if res != "ok":
144 i, summary, msg = res.split('\n')
145 i = int(i)
146 import pprint
147 print('Example:')
148 pprint.pprint(self.finalizer_order_examples[i])
149 print('Finalization ages:')
150 print(summary)
151 print(msg)
152 py.test.fail(msg)
153
154
155 def define_from_objwithfinalizer_to_youngobj(cls):
156 import gc
157 if cls.large_tests_ok:
158 MAX = 500000
159 else:
160 MAX = 150
161
162 class B:
163 count = 0
164 class A:
165 pass
166
167 class FQ(rgc.FinalizerQueue):
168 Class = A
169 def finalizer_trigger(self):
170 while True:
171 a = self.next_dead()
172 if a is None:
173 break
174 a.b.count += 1
175 fq = FQ()
176
177 def g():
178 b = B()
179 a = A()
180 fq.register_finalizer(a)
181 a.b = b
182 i = 0
183 lst = [None]
184 while i < MAX:
185 lst[0] = str(i)
186 i += 1
187 return a.b, lst
188 g._dont_inline_ = True
189
190 def f():
191 b, lst = g()
192 gc.collect()
193 return b.count
194 return f
195
196 def test_from_objwithfinalizer_to_youngobj(self):
197 res = self.run('from_objwithfinalizer_to_youngobj')
198 assert res == 1
199
200 class SemiSpaceGCTests(SemiSpaceGCTestDefines):
201 # xxx messy
202
203 def run(self, name): # for test_gc.py
204 if name == 'finalizer_order':
205 func = self.definestr_finalizer_order()
206 res = self.interpret(func, [-1])
207 return ''.join(res.chars)
208 elif name == 'from_objwithfinalizer_to_youngobj':
209 func = self.define_from_objwithfinalizer_to_youngobj()
210 return self.interpret(func, [])
211 else:
212 assert 0, "don't know what to do with that"