| 1 |
import py |
| 2 |
import inspect |
| 3 |
|
| 4 |
from rpython.rlib.objectmodel import compute_hash, compute_identity_hash |
| 5 |
from rpython.translator.c import gc |
| 6 |
from rpython.annotator import model as annmodel |
| 7 |
from rpython.rtyper.llannotation import SomePtr |
| 8 |
from rpython.rtyper.lltypesystem import lltype, llmemory, rffi, llgroup |
| 9 |
from rpython.memory.gctransform import framework, shadowstack |
| 10 |
from rpython.rtyper.lltypesystem.lloperation import llop, void |
| 11 |
from rpython.rlib.objectmodel import compute_unique_id, we_are_translated |
| 12 |
from rpython.rlib.debug import ll_assert |
| 13 |
from rpython.rlib import rgc |
| 14 |
from rpython.conftest import option |
| 15 |
from rpython.rlib.rstring import StringBuilder |
| 16 |
from rpython.rlib.rarithmetic import LONG_BIT |
| 17 |
from rpython.rlib.nonconst import NonConstant |
| 18 |
from rpython.rtyper.rtyper import llinterp_backend |
| 19 |
from rpython.memory.gc.hook import GcHooks |
| 20 |
|
| 21 |
|
| 22 |
WORD = LONG_BIT // 8 |
| 23 |
|
| 24 |
|
| 25 |
def rtype(func, inputtypes, specialize=True, gcname='ref', |
| 26 |
backendopt=False, **extraconfigopts): |
| 27 |
from rpython.translator.translator import TranslationContext |
| 28 |
t = TranslationContext() |
| 29 |
|
| 30 |
t.config.translation.gc = gcname |
| 31 |
t.config.translation.gcremovetypeptr = True |
| 32 |
t.config.set(**extraconfigopts) |
| 33 |
ann = t.buildannotator() |
| 34 |
ann.build_types(func, inputtypes) |
| 35 |
rtyper = t.buildrtyper() |
| 36 |
rtyper.backend = llinterp_backend |
| 37 |
|
| 38 |
if specialize: |
| 39 |
rtyper.specialize() |
| 40 |
if backendopt: |
| 41 |
from rpython.translator.backendopt.all import backend_optimizations |
| 42 |
backend_optimizations(t) |
| 43 |
if option.view: |
| 44 |
t.viewcg() |
| 45 |
return t |
| 46 |
|
| 47 |
ARGS = lltype.FixedSizeArray(lltype.Signed, 3) |
| 48 |
|
| 49 |
class GCTest(object): |
| 50 |
gcpolicy = None |
| 51 |
GC_CAN_MOVE = False |
| 52 |
taggedpointers = False |
| 53 |
gchooks = None |
| 54 |
|
| 55 |
def setup(self): |
| 56 |
|
| 57 |
|
| 58 |
import gc |
| 59 |
gc.collect() |
| 60 |
|
| 61 |
def setup_class(cls): |
| 62 |
cls.marker = lltype.malloc(rffi.CArray(lltype.Signed), 1, |
| 63 |
flavor='raw', zero=True) |
| 64 |
funcs0 = [] |
| 65 |
funcs2 = [] |
| 66 |
cleanups = [] |
| 67 |
name_to_func = {} |
| 68 |
mixlevelstuff = [] |
| 69 |
for fullname in dir(cls): |
| 70 |
if not fullname.startswith('define'): |
| 71 |
continue |
| 72 |
definefunc = getattr(cls, fullname) |
| 73 |
_, name = fullname.split('_', 1) |
| 74 |
func_fixup = definefunc.im_func(cls) |
| 75 |
cleanup = None |
| 76 |
if isinstance(func_fixup, tuple): |
| 77 |
func, cleanup, fixup = func_fixup |
| 78 |
mixlevelstuff.append(fixup) |
| 79 |
else: |
| 80 |
func = func_fixup |
| 81 |
func.__name__ = "f_%s" % name |
| 82 |
if cleanup: |
| 83 |
cleanup.__name__ = "clean_%s" % name |
| 84 |
|
| 85 |
nargs = len(inspect.getargspec(func)[0]) |
| 86 |
name_to_func[name] = len(funcs0) |
| 87 |
if nargs == 2: |
| 88 |
funcs2.append(func) |
| 89 |
funcs0.append(None) |
| 90 |
elif nargs == 0: |
| 91 |
funcs0.append(func) |
| 92 |
funcs2.append(None) |
| 93 |
else: |
| 94 |
raise NotImplementedError( |
| 95 |
"defined test functions should have 0/2 arguments") |
| 96 |
|
| 97 |
|
| 98 |
cleanups.append(cleanup) |
| 99 |
|
| 100 |
def entrypoint(args): |
| 101 |
num = args[0] |
| 102 |
func = funcs0[num] |
| 103 |
if func: |
| 104 |
res = func() |
| 105 |
else: |
| 106 |
func = funcs2[num] |
| 107 |
res = func(args[1], args[2]) |
| 108 |
cleanup = cleanups[num] |
| 109 |
if cleanup: |
| 110 |
cleanup() |
| 111 |
return res |
| 112 |
|
| 113 |
from rpython.translator.c.genc import CStandaloneBuilder |
| 114 |
|
| 115 |
s_args = SomePtr(lltype.Ptr(ARGS)) |
| 116 |
t = rtype(entrypoint, [s_args], gcname=cls.gcname, |
| 117 |
taggedpointers=cls.taggedpointers) |
| 118 |
|
| 119 |
for fixup in mixlevelstuff: |
| 120 |
if fixup: |
| 121 |
fixup(t) |
| 122 |
|
| 123 |
cbuild = CStandaloneBuilder(t, entrypoint, config=t.config, |
| 124 |
gcpolicy=cls.gcpolicy, |
| 125 |
gchooks=cls.gchooks) |
| 126 |
cbuild.make_entrypoint_wrapper = False |
| 127 |
db = cbuild.build_database() |
| 128 |
entrypointptr = cbuild.getentrypointptr() |
| 129 |
entrygraph = entrypointptr._obj.graph |
| 130 |
if option.view: |
| 131 |
t.viewcg() |
| 132 |
|
| 133 |
cls.name_to_func = name_to_func |
| 134 |
cls.entrygraph = entrygraph |
| 135 |
cls.rtyper = t.rtyper |
| 136 |
cls.db = db |
| 137 |
|
| 138 |
def runner(self, name, transformer=False): |
| 139 |
db = self.db |
| 140 |
name_to_func = self.name_to_func |
| 141 |
entrygraph = self.entrygraph |
| 142 |
from rpython.rtyper.llinterp import LLInterpreter |
| 143 |
|
| 144 |
llinterp = LLInterpreter(self.rtyper) |
| 145 |
|
| 146 |
gct = db.gctransformer |
| 147 |
|
| 148 |
if self.__class__.__dict__.get('_used', False): |
| 149 |
teardowngraph = gct.frameworkgc__teardown_ptr.value._obj.graph |
| 150 |
llinterp.eval_graph(teardowngraph, []) |
| 151 |
self.__class__._used = True |
| 152 |
|
| 153 |
|
| 154 |
setupgraph = gct.frameworkgc_setup_ptr.value._obj.graph |
| 155 |
|
| 156 |
llinterp.eval_graph(setupgraph, []) |
| 157 |
def run(args): |
| 158 |
ll_args = lltype.malloc(ARGS, immortal=True) |
| 159 |
ll_args[0] = name_to_func[name] |
| 160 |
for i in range(len(args)): |
| 161 |
ll_args[1+i] = args[i] |
| 162 |
res = llinterp.eval_graph(entrygraph, [ll_args]) |
| 163 |
return res |
| 164 |
|
| 165 |
if transformer: |
| 166 |
return run, gct |
| 167 |
else: |
| 168 |
return run |
| 169 |
|
| 170 |
class GenericGCTests(GCTest): |
| 171 |
GC_CAN_SHRINK_ARRAY = False |
| 172 |
def define_instances(cls): |
| 173 |
class A(object): |
| 174 |
pass |
| 175 |
class B(A): |
| 176 |
def __init__(self, something): |
| 177 |
self.something = something |
| 178 |
def malloc_a_lot(): |
| 179 |
i = 0 |
| 180 |
first = None |
| 181 |
while i < 10: |
| 182 |
i += 1 |
| 183 |
a = somea = A() |
| 184 |
a.last = first |
| 185 |
first = a |
| 186 |
j = 0 |
| 187 |
while j < 30: |
| 188 |
b = B(somea) |
| 189 |
b.last = first |
| 190 |
j += 1 |
| 191 |
return 0 |
| 192 |
return malloc_a_lot |
| 193 |
|
| 194 |
def test_instances(self): |
| 195 |
run = self.runner("instances") |
| 196 |
run([]) |
| 197 |
|
| 198 |
|
| 199 |
def define_llinterp_lists(cls): |
| 200 |
def malloc_a_lot(): |
| 201 |
i = 0 |
| 202 |
while i < 10: |
| 203 |
i += 1 |
| 204 |
a = [1] * 10 |
| 205 |
j = 0 |
| 206 |
while j < 30: |
| 207 |
j += 1 |
| 208 |
a.append(j) |
| 209 |
return 0 |
| 210 |
return malloc_a_lot |
| 211 |
|
| 212 |
def test_llinterp_lists(self): |
| 213 |
run = self.runner("llinterp_lists") |
| 214 |
run([]) |
| 215 |
|
| 216 |
def define_llinterp_tuples(cls): |
| 217 |
def malloc_a_lot(): |
| 218 |
i = 0 |
| 219 |
while i < 10: |
| 220 |
i += 1 |
| 221 |
a = (1, 2, i) |
| 222 |
b = [a] * 10 |
| 223 |
j = 0 |
| 224 |
while j < 20: |
| 225 |
j += 1 |
| 226 |
b.append((1, j, i)) |
| 227 |
return 0 |
| 228 |
return malloc_a_lot |
| 229 |
|
| 230 |
def test_llinterp_tuples(self): |
| 231 |
run = self.runner("llinterp_tuples") |
| 232 |
run([]) |
| 233 |
|
| 234 |
def define_llinterp_dict(self): |
| 235 |
class A(object): |
| 236 |
pass |
| 237 |
def malloc_a_lot(): |
| 238 |
i = 0 |
| 239 |
while i < 10: |
| 240 |
i += 1 |
| 241 |
a = (1, 2, i) |
| 242 |
b = {a: A()} |
| 243 |
j = 0 |
| 244 |
while j < 20: |
| 245 |
j += 1 |
| 246 |
b[1, j, i] = A() |
| 247 |
return 0 |
| 248 |
return malloc_a_lot |
| 249 |
|
| 250 |
def test_llinterp_dict(self): |
| 251 |
run = self.runner("llinterp_dict") |
| 252 |
run([]) |
| 253 |
|
| 254 |
def skipdefine_global_list(cls): |
| 255 |
gl = [] |
| 256 |
class Box: |
| 257 |
def __init__(self): |
| 258 |
self.lst = gl |
| 259 |
box = Box() |
| 260 |
def append_to_list(i, j): |
| 261 |
box.lst.append([i] * 50) |
| 262 |
llop.gc__collect(lltype.Void) |
| 263 |
return box.lst[j][0] |
| 264 |
return append_to_list, None, None |
| 265 |
|
| 266 |
def test_global_list(self): |
| 267 |
py.test.skip("doesn't fit in the model, tested elsewhere too") |
| 268 |
run = self.runner("global_list") |
| 269 |
res = run([0, 0]) |
| 270 |
assert res == 0 |
| 271 |
for i in range(1, 5): |
| 272 |
res = run([i, i - 1]) |
| 273 |
assert res == i - 1 |
| 274 |
|
| 275 |
def define_string_concatenation(cls): |
| 276 |
def concat(j, dummy): |
| 277 |
lst = [] |
| 278 |
for i in range(j): |
| 279 |
lst.append(str(i)) |
| 280 |
return len("".join(lst)) |
| 281 |
return concat |
| 282 |
|
| 283 |
def test_string_concatenation(self): |
| 284 |
run = self.runner("string_concatenation") |
| 285 |
res = run([100, 0]) |
| 286 |
assert res == len(''.join([str(x) for x in range(100)])) |
| 287 |
|
| 288 |
def define_nongc_static_root(cls): |
| 289 |
T1 = lltype.GcStruct("C", ('x', lltype.Signed)) |
| 290 |
T2 = lltype.Struct("C", ('p', lltype.Ptr(T1))) |
| 291 |
static = lltype.malloc(T2, immortal=True) |
| 292 |
def f(): |
| 293 |
t1 = lltype.malloc(T1) |
| 294 |
t1.x = 42 |
| 295 |
static.p = t1 |
| 296 |
llop.gc__collect(lltype.Void) |
| 297 |
return static.p.x |
| 298 |
def cleanup(): |
| 299 |
static.p = lltype.nullptr(T1) |
| 300 |
return f, cleanup, None |
| 301 |
|
| 302 |
def test_nongc_static_root(self): |
| 303 |
run = self.runner("nongc_static_root") |
| 304 |
res = run([]) |
| 305 |
assert res == 42 |
| 306 |
|
| 307 |
def define_destructor(cls): |
| 308 |
class B(object): |
| 309 |
pass |
| 310 |
b = B() |
| 311 |
b.nextid = 0 |
| 312 |
b.num_deleted = 0 |
| 313 |
class A(object): |
| 314 |
def __init__(self): |
| 315 |
self.id = b.nextid |
| 316 |
b.nextid += 1 |
| 317 |
def __del__(self): |
| 318 |
b.num_deleted += 1 |
| 319 |
def f(x, y): |
| 320 |
a = A() |
| 321 |
i = 0 |
| 322 |
while i < x: |
| 323 |
i += 1 |
| 324 |
a = A() |
| 325 |
llop.gc__collect(lltype.Void) |
| 326 |
llop.gc__collect(lltype.Void) |
| 327 |
return b.num_deleted |
| 328 |
return f |
| 329 |
|
| 330 |
def test_destructor(self): |
| 331 |
run = self.runner("destructor") |
| 332 |
res = run([5, 42]) |
| 333 |
assert res == 6 |
| 334 |
|
| 335 |
def define_old_style_finalizer(cls): |
| 336 |
class B(object): |
| 337 |
pass |
| 338 |
b = B() |
| 339 |
b.nextid = 0 |
| 340 |
b.num_deleted = 0 |
| 341 |
class A(object): |
| 342 |
def __init__(self): |
| 343 |
self.id = b.nextid |
| 344 |
b.nextid += 1 |
| 345 |
def __del__(self): |
| 346 |
llop.gc__collect(lltype.Void) |
| 347 |
b.num_deleted += 1 |
| 348 |
def f(x, y): |
| 349 |
a = A() |
| 350 |
i = 0 |
| 351 |
while i < x: |
| 352 |
i += 1 |
| 353 |
a = A() |
| 354 |
llop.gc__collect(lltype.Void) |
| 355 |
llop.gc__collect(lltype.Void) |
| 356 |
return b.num_deleted |
| 357 |
return f |
| 358 |
|
| 359 |
def test_old_style_finalizer(self): |
| 360 |
run = self.runner("old_style_finalizer") |
| 361 |
res = run([5, 42]) |
| 362 |
assert res == 6 |
| 363 |
|
| 364 |
def define_finalizer(cls): |
| 365 |
class B(object): |
| 366 |
pass |
| 367 |
b = B() |
| 368 |
b.nextid = 0 |
| 369 |
b.num_deleted = 0 |
| 370 |
class A(object): |
| 371 |
def __init__(self): |
| 372 |
self.id = b.nextid |
| 373 |
b.nextid += 1 |
| 374 |
fq.register_finalizer(self) |
| 375 |
class FQ(rgc.FinalizerQueue): |
| 376 |
Class = A |
| 377 |
def finalizer_trigger(self): |
| 378 |
while self.next_dead() is not None: |
| 379 |
b.num_deleted += 1 |
| 380 |
fq = FQ() |
| 381 |
def f(x, y): |
| 382 |
a = A() |
| 383 |
i = 0 |
| 384 |
while i < x: |
| 385 |
i += 1 |
| 386 |
a = A() |
| 387 |
llop.gc__collect(lltype.Void) |
| 388 |
llop.gc__collect(lltype.Void) |
| 389 |
return b.num_deleted |
| 390 |
return f |
| 391 |
|
| 392 |
def test_finalizer(self): |
| 393 |
run = self.runner("finalizer") |
| 394 |
res = run([5, 42]) |
| 395 |
assert res == 6 |
| 396 |
|
| 397 |
def define_finalizer_calls_malloc(cls): |
| 398 |
class B(object): |
| 399 |
pass |
| 400 |
b = B() |
| 401 |
b.nextid = 0 |
| 402 |
b.num_deleted = 0 |
| 403 |
class AAA(object): |
| 404 |
def __init__(self): |
| 405 |
self.id = b.nextid |
| 406 |
b.nextid += 1 |
| 407 |
fq.register_finalizer(self) |
| 408 |
class C(AAA): |
| 409 |
pass |
| 410 |
class FQ(rgc.FinalizerQueue): |
| 411 |
Class = AAA |
| 412 |
def finalizer_trigger(self): |
| 413 |
while True: |
| 414 |
a = self.next_dead() |
| 415 |
if a is None: |
| 416 |
break |
| 417 |
b.num_deleted += 1 |
| 418 |
if not isinstance(a, C): |
| 419 |
C() |
| 420 |
fq = FQ() |
| 421 |
def f(x, y): |
| 422 |
a = AAA() |
| 423 |
i = 0 |
| 424 |
while i < x: |
| 425 |
i += 1 |
| 426 |
a = AAA() |
| 427 |
llop.gc__collect(lltype.Void) |
| 428 |
llop.gc__collect(lltype.Void) |
| 429 |
return b.num_deleted |
| 430 |
return f |
| 431 |
|
| 432 |
def test_finalizer_calls_malloc(self): |
| 433 |
run = self.runner("finalizer_calls_malloc") |
| 434 |
res = run([5, 42]) |
| 435 |
assert res == 12 |
| 436 |
|
| 437 |
def define_finalizer_resurrects(cls): |
| 438 |
class B(object): |
| 439 |
pass |
| 440 |
b = B() |
| 441 |
b.nextid = 0 |
| 442 |
b.num_deleted = 0 |
| 443 |
class A(object): |
| 444 |
def __init__(self): |
| 445 |
self.id = b.nextid |
| 446 |
b.nextid += 1 |
| 447 |
fq.register_finalizer(self) |
| 448 |
class FQ(rgc.FinalizerQueue): |
| 449 |
Class = A |
| 450 |
def finalizer_trigger(self): |
| 451 |
while True: |
| 452 |
a = self.next_dead() |
| 453 |
if a is None: |
| 454 |
break |
| 455 |
b.num_deleted += 1 |
| 456 |
b.a = a |
| 457 |
fq = FQ() |
| 458 |
def f(x, y): |
| 459 |
a = A() |
| 460 |
i = 0 |
| 461 |
while i < x: |
| 462 |
i += 1 |
| 463 |
a = A() |
| 464 |
llop.gc__collect(lltype.Void) |
| 465 |
llop.gc__collect(lltype.Void) |
| 466 |
aid = b.a.id |
| 467 |
b.a = None |
| 468 |
|
| 469 |
llop.gc__collect(lltype.Void) |
| 470 |
llop.gc__collect(lltype.Void) |
| 471 |
return b.num_deleted * 10 + aid + 100 * (b.a is None) |
| 472 |
return f |
| 473 |
|
| 474 |
def test_finalizer_resurrects(self): |
| 475 |
run = self.runner("finalizer_resurrects") |
| 476 |
res = run([5, 42]) |
| 477 |
assert 160 <= res <= 165 |
| 478 |
|
| 479 |
def define_custom_trace(cls): |
| 480 |
|
| 481 |
S = lltype.GcStruct('S', ('x', llmemory.Address)) |
| 482 |
T = lltype.GcStruct('T', ('z', lltype.Signed)) |
| 483 |
offset_of_x = llmemory.offsetof(S, 'x') |
| 484 |
def customtrace(gc, obj, callback, arg1, arg2): |
| 485 |
gc._trace_callback(callback, arg1, arg2, obj + offset_of_x) |
| 486 |
lambda_customtrace = lambda: customtrace |
| 487 |
|
| 488 |
|
| 489 |
def setup(): |
| 490 |
rgc.register_custom_trace_hook(S, lambda_customtrace) |
| 491 |
tx = lltype.malloc(T) |
| 492 |
tx.z = 4243 |
| 493 |
s1 = lltype.malloc(S) |
| 494 |
s1.x = llmemory.cast_ptr_to_adr(tx) |
| 495 |
return s1 |
| 496 |
def f(): |
| 497 |
s1 = setup() |
| 498 |
llop.gc__collect(lltype.Void) |
| 499 |
return llmemory.cast_adr_to_ptr(s1.x, lltype.Ptr(T)).z |
| 500 |
return f |
| 501 |
|
| 502 |
def test_custom_trace(self): |
| 503 |
run = self.runner("custom_trace") |
| 504 |
res = run([]) |
| 505 |
assert res == 4243 |
| 506 |
|
| 507 |
def define_weakref(cls): |
| 508 |
import weakref, gc |
| 509 |
class A(object): |
| 510 |
pass |
| 511 |
def g(): |
| 512 |
a = A() |
| 513 |
return weakref.ref(a) |
| 514 |
def f(): |
| 515 |
a = A() |
| 516 |
ref = weakref.ref(a) |
| 517 |
result = ref() is a |
| 518 |
ref = g() |
| 519 |
llop.gc__collect(lltype.Void) |
| 520 |
result = result and (ref() is None) |
| 521 |
|
| 522 |
llop.gc__collect(lltype.Void) |
| 523 |
result = result and (ref() is None) |
| 524 |
return result |
| 525 |
return f |
| 526 |
|
| 527 |
def test_weakref(self): |
| 528 |
run = self.runner("weakref") |
| 529 |
res = run([]) |
| 530 |
assert res |
| 531 |
|
| 532 |
def define_weakref_to_object_with_destructor(cls): |
| 533 |
import weakref, gc |
| 534 |
class A(object): |
| 535 |
count = 0 |
| 536 |
a = A() |
| 537 |
class B(object): |
| 538 |
def __del__(self): |
| 539 |
a.count += 1 |
| 540 |
def g(): |
| 541 |
b = B() |
| 542 |
return weakref.ref(b) |
| 543 |
def f(): |
| 544 |
ref = g() |
| 545 |
llop.gc__collect(lltype.Void) |
| 546 |
llop.gc__collect(lltype.Void) |
| 547 |
result = a.count == 1 and (ref() is None) |
| 548 |
return result |
| 549 |
return f |
| 550 |
|
| 551 |
def test_weakref_to_object_with_destructor(self): |
| 552 |
run = self.runner("weakref_to_object_with_destructor") |
| 553 |
res = run([]) |
| 554 |
assert res |
| 555 |
|
| 556 |
def define_weakref_to_object_with_finalizer(cls): |
| 557 |
import weakref, gc |
| 558 |
class A(object): |
| 559 |
count = 0 |
| 560 |
a = A() |
| 561 |
class B(object): |
| 562 |
pass |
| 563 |
class FQ(rgc.FinalizerQueue): |
| 564 |
Class = B |
| 565 |
def finalizer_trigger(self): |
| 566 |
while self.next_dead() is not None: |
| 567 |
a.count += 1 |
| 568 |
fq = FQ() |
| 569 |
def g(): |
| 570 |
b = B() |
| 571 |
fq.register_finalizer(b) |
| 572 |
return weakref.ref(b) |
| 573 |
def f(): |
| 574 |
ref = g() |
| 575 |
llop.gc__collect(lltype.Void) |
| 576 |
llop.gc__collect(lltype.Void) |
| 577 |
result = a.count == 1 and (ref() is None) |
| 578 |
return result |
| 579 |
return f |
| 580 |
|
| 581 |
def test_weakref_to_object_with_finalizer(self): |
| 582 |
run = self.runner("weakref_to_object_with_finalizer") |
| 583 |
res = run([]) |
| 584 |
assert res |
| 585 |
|
| 586 |
def define_collect_during_collect(cls): |
| 587 |
class B(object): |
| 588 |
pass |
| 589 |
b = B() |
| 590 |
b.nextid = 1 |
| 591 |
b.num_deleted = 0 |
| 592 |
b.num_deleted_c = 0 |
| 593 |
class A(object): |
| 594 |
def __init__(self): |
| 595 |
self.id = b.nextid |
| 596 |
b.nextid += 1 |
| 597 |
fq.register_finalizer(self) |
| 598 |
class C(A): |
| 599 |
pass |
| 600 |
class FQ(rgc.FinalizerQueue): |
| 601 |
Class = A |
| 602 |
def finalizer_trigger(self): |
| 603 |
while True: |
| 604 |
a = self.next_dead() |
| 605 |
if a is None: |
| 606 |
break |
| 607 |
llop.gc__collect(lltype.Void) |
| 608 |
b.num_deleted += 1 |
| 609 |
if isinstance(a, C): |
| 610 |
b.num_deleted_c += 1 |
| 611 |
else: |
| 612 |
C() |
| 613 |
C() |
| 614 |
fq = FQ() |
| 615 |
def f(x, y): |
| 616 |
persistent_a1 = A() |
| 617 |
persistent_a2 = A() |
| 618 |
i = 0 |
| 619 |
while i < x: |
| 620 |
i += 1 |
| 621 |
a = A() |
| 622 |
persistent_a3 = A() |
| 623 |
persistent_a4 = A() |
| 624 |
llop.gc__collect(lltype.Void) |
| 625 |
llop.gc__collect(lltype.Void) |
| 626 |
b.bla = persistent_a1.id + persistent_a2.id + persistent_a3.id + persistent_a4.id |
| 627 |
|
| 628 |
llop.debug_print(lltype.Void, b.num_deleted_c) |
| 629 |
return b.num_deleted |
| 630 |
return f |
| 631 |
|
| 632 |
def test_collect_during_collect(self): |
| 633 |
run = self.runner("collect_during_collect") |
| 634 |
|
| 635 |
res = run([4, 42]) |
| 636 |
assert res == 12 |
| 637 |
|
| 638 |
def define_collect_0(cls): |
| 639 |
def concat(j, dummy): |
| 640 |
lst = [] |
| 641 |
for i in range(j): |
| 642 |
lst.append(str(i)) |
| 643 |
result = len("".join(lst)) |
| 644 |
if we_are_translated(): |
| 645 |
llop.gc__collect(lltype.Void, 0) |
| 646 |
return result |
| 647 |
return concat |
| 648 |
|
| 649 |
def test_collect_0(self): |
| 650 |
run = self.runner("collect_0") |
| 651 |
res = run([100, 0]) |
| 652 |
assert res == len(''.join([str(x) for x in range(100)])) |
| 653 |
|
| 654 |
def define_interior_ptrs(cls): |
| 655 |
from rpython.rtyper.lltypesystem.lltype import Struct, GcStruct, GcArray |
| 656 |
from rpython.rtyper.lltypesystem.lltype import Array, Signed, malloc |
| 657 |
|
| 658 |
S1 = Struct("S1", ('x', Signed)) |
| 659 |
T1 = GcStruct("T1", ('s', S1)) |
| 660 |
def f1(): |
| 661 |
t = malloc(T1) |
| 662 |
t.s.x = 1 |
| 663 |
return t.s.x |
| 664 |
|
| 665 |
S2 = Struct("S2", ('x', Signed)) |
| 666 |
T2 = GcArray(S2) |
| 667 |
def f2(): |
| 668 |
t = malloc(T2, 1) |
| 669 |
t[0].x = 1 |
| 670 |
return t[0].x |
| 671 |
|
| 672 |
S3 = Struct("S3", ('x', Signed)) |
| 673 |
T3 = GcStruct("T3", ('items', Array(S3))) |
| 674 |
def f3(): |
| 675 |
t = malloc(T3, 1) |
| 676 |
t.items[0].x = 1 |
| 677 |
return t.items[0].x |
| 678 |
|
| 679 |
S4 = Struct("S4", ('x', Signed)) |
| 680 |
T4 = Struct("T4", ('s', S4)) |
| 681 |
U4 = GcArray(T4) |
| 682 |
def f4(): |
| 683 |
u = malloc(U4, 1) |
| 684 |
u[0].s.x = 1 |
| 685 |
return u[0].s.x |
| 686 |
|
| 687 |
S5 = Struct("S5", ('x', Signed)) |
| 688 |
T5 = GcStruct("T5", ('items', Array(S5))) |
| 689 |
def f5(): |
| 690 |
t = malloc(T5, 1) |
| 691 |
return len(t.items) |
| 692 |
|
| 693 |
T6 = GcStruct("T6", ('s', Array(Signed))) |
| 694 |
def f6(): |
| 695 |
t = malloc(T6, 1) |
| 696 |
t.s[0] = 1 |
| 697 |
return t.s[0] |
| 698 |
|
| 699 |
def func(): |
| 700 |
return (f1() * 100000 + |
| 701 |
f2() * 10000 + |
| 702 |
f3() * 1000 + |
| 703 |
f4() * 100 + |
| 704 |
f5() * 10 + |
| 705 |
f6()) |
| 706 |
|
| 707 |
assert func() == 111111 |
| 708 |
return func |
| 709 |
|
| 710 |
def test_interior_ptrs(self): |
| 711 |
run = self.runner("interior_ptrs") |
| 712 |
res = run([]) |
| 713 |
assert res == 111111 |
| 714 |
|
| 715 |
def define_id(cls): |
| 716 |
class A(object): |
| 717 |
pass |
| 718 |
a1 = A() |
| 719 |
def func(): |
| 720 |
a2 = A() |
| 721 |
a3 = A() |
| 722 |
id1 = compute_unique_id(a1) |
| 723 |
id2 = compute_unique_id(a2) |
| 724 |
id3 = compute_unique_id(a3) |
| 725 |
llop.gc__collect(lltype.Void) |
| 726 |
error = 0 |
| 727 |
if id1 != compute_unique_id(a1): error += 1 |
| 728 |
if id2 != compute_unique_id(a2): error += 2 |
| 729 |
if id3 != compute_unique_id(a3): error += 4 |
| 730 |
return error |
| 731 |
return func |
| 732 |
|
| 733 |
def test_id(self): |
| 734 |
run = self.runner("id") |
| 735 |
res = run([]) |
| 736 |
assert res == 0 |
| 737 |
|
| 738 |
def define_can_move(cls): |
| 739 |
TP = lltype.GcArray(lltype.Float) |
| 740 |
def func(): |
| 741 |
return rgc.can_move(lltype.malloc(TP, 1)) |
| 742 |
return func |
| 743 |
|
| 744 |
def test_can_move(self): |
| 745 |
run = self.runner("can_move") |
| 746 |
res = run([]) |
| 747 |
assert res == self.GC_CAN_MOVE |
| 748 |
|
| 749 |
def define_shrink_array(cls): |
| 750 |
from rpython.rtyper.lltypesystem.rstr import STR |
| 751 |
|
| 752 |
def f(): |
| 753 |
ptr = lltype.malloc(STR, 3) |
| 754 |
ptr.hash = 0x62 |
| 755 |
ptr.chars[0] = '0' |
| 756 |
ptr.chars[1] = 'B' |
| 757 |
ptr.chars[2] = 'C' |
| 758 |
ptr2 = rgc.ll_shrink_array(ptr, 2) |
| 759 |
return ((ptr == ptr2) + |
| 760 |
ord(ptr2.chars[0]) + |
| 761 |
(ord(ptr2.chars[1]) << 8) + |
| 762 |
(len(ptr2.chars) << 16) + |
| 763 |
(ptr2.hash << 24)) |
| 764 |
return f |
| 765 |
|
| 766 |
def test_shrink_array(self): |
| 767 |
run = self.runner("shrink_array") |
| 768 |
if self.GC_CAN_SHRINK_ARRAY: |
| 769 |
expected = 0x62024231 |
| 770 |
else: |
| 771 |
expected = 0x62024230 |
| 772 |
assert run([]) == expected |
| 773 |
|
| 774 |
def define_string_builder_over_allocation(cls): |
| 775 |
import gc |
| 776 |
def fn(): |
| 777 |
s = StringBuilder(4) |
| 778 |
s.append("abcd") |
| 779 |
s.append("defg") |
| 780 |
s.append("rty") |
| 781 |
s.append_multiple_char('y', 1000) |
| 782 |
gc.collect() |
| 783 |
s.append_multiple_char('y', 1000) |
| 784 |
res = s.build()[1000] |
| 785 |
gc.collect() |
| 786 |
return ord(res) |
| 787 |
return fn |
| 788 |
|
| 789 |
def test_string_builder_over_allocation(self): |
| 790 |
fn = self.runner("string_builder_over_allocation") |
| 791 |
res = fn([]) |
| 792 |
assert res == ord('y') |
| 793 |
|
| 794 |
class GenericMovingGCTests(GenericGCTests): |
| 795 |
GC_CAN_MOVE = True |
| 796 |
GC_CAN_TEST_ID = False |
| 797 |
def define_many_ids(cls): |
| 798 |
class A(object): |
| 799 |
pass |
| 800 |
def f(): |
| 801 |
from rpython.rtyper.lltypesystem import rffi |
| 802 |
alist = [A() for i in range(50)] |
| 803 |
idarray = lltype.malloc(rffi.SIGNEDP.TO, len(alist), flavor='raw') |
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
i = 0 |
| 808 |
while i < len(alist): |
| 809 |
idarray[i] = compute_unique_id(alist[i]) |
| 810 |
i += 1 |
| 811 |
j = 0 |
| 812 |
while j < 2: |
| 813 |
if j == 1: |
| 814 |
[A() for i in range(20)] |
| 815 |
i = 0 |
| 816 |
while i < len(alist): |
| 817 |
assert idarray[i] == compute_unique_id(alist[i]) |
| 818 |
i += 1 |
| 819 |
j += 1 |
| 820 |
lltype.free(idarray, flavor='raw') |
| 821 |
return 0 |
| 822 |
return f |
| 823 |
|
| 824 |
def test_many_ids(self): |
| 825 |
if not self.GC_CAN_TEST_ID: |
| 826 |
py.test.skip("fails for bad reasons in lltype.py :-(") |
| 827 |
run = self.runner("many_ids") |
| 828 |
run([]) |
| 829 |
|
| 830 |
@classmethod |
| 831 |
def ensure_layoutbuilder(cls, translator): |
| 832 |
jit2gc = getattr(translator, '_jit2gc', None) |
| 833 |
if jit2gc: |
| 834 |
assert 'invoke_after_minor_collection' in jit2gc |
| 835 |
return jit2gc['layoutbuilder'] |
| 836 |
marker = cls.marker |
| 837 |
GCClass = cls.gcpolicy.transformerclass.GCClass |
| 838 |
layoutbuilder = framework.TransformerLayoutBuilder(translator, GCClass) |
| 839 |
layoutbuilder.delay_encoding() |
| 840 |
|
| 841 |
def seeme(): |
| 842 |
marker[0] += 1 |
| 843 |
translator._jit2gc = { |
| 844 |
'layoutbuilder': layoutbuilder, |
| 845 |
'invoke_after_minor_collection': seeme, |
| 846 |
} |
| 847 |
return layoutbuilder |
| 848 |
|
| 849 |
def define_do_malloc_operations(cls): |
| 850 |
P = lltype.GcStruct('P', ('x', lltype.Signed)) |
| 851 |
def g(): |
| 852 |
r = lltype.malloc(P) |
| 853 |
r.x = 1 |
| 854 |
p = llop.do_malloc_fixedsize(llmemory.GCREF) |
| 855 |
p = lltype.cast_opaque_ptr(lltype.Ptr(P), p) |
| 856 |
p.x = r.x |
| 857 |
return p.x |
| 858 |
def f(): |
| 859 |
i = 0 |
| 860 |
while i < 40: |
| 861 |
g() |
| 862 |
i += 1 |
| 863 |
return 0 |
| 864 |
|
| 865 |
if cls.gcname == 'incminimark': |
| 866 |
marker = cls.marker |
| 867 |
def cleanup(): |
| 868 |
assert marker[0] > 0 |
| 869 |
marker[0] = 0 |
| 870 |
else: |
| 871 |
cleanup = None |
| 872 |
|
| 873 |
def fix_graph_of_g(translator): |
| 874 |
from rpython.translator.translator import graphof |
| 875 |
from rpython.flowspace.model import Constant |
| 876 |
from rpython.rtyper.lltypesystem import rffi |
| 877 |
layoutbuilder = cls.ensure_layoutbuilder(translator) |
| 878 |
|
| 879 |
type_id = layoutbuilder.get_type_id(P) |
| 880 |
|
| 881 |
|
| 882 |
graph = graphof(translator, g) |
| 883 |
for op in graph.startblock.operations: |
| 884 |
if op.opname == 'do_malloc_fixedsize': |
| 885 |
op.args = [Constant(type_id, llgroup.HALFWORD), |
| 886 |
Constant(llmemory.sizeof(P), lltype.Signed), |
| 887 |
Constant(False, lltype.Bool), |
| 888 |
Constant(False, lltype.Bool), |
| 889 |
Constant(False, lltype.Bool)] |
| 890 |
break |
| 891 |
else: |
| 892 |
assert 0, "oups, not found" |
| 893 |
return f, cleanup, fix_graph_of_g |
| 894 |
|
| 895 |
def test_do_malloc_operations(self): |
| 896 |
run = self.runner("do_malloc_operations") |
| 897 |
run([]) |
| 898 |
|
| 899 |
def define_do_malloc_operations_in_call(cls): |
| 900 |
P = lltype.GcStruct('P', ('x', lltype.Signed)) |
| 901 |
def g(): |
| 902 |
llop.do_malloc_fixedsize(llmemory.GCREF) |
| 903 |
def f(): |
| 904 |
q = lltype.malloc(P) |
| 905 |
q.x = 1 |
| 906 |
i = 0 |
| 907 |
while i < 40: |
| 908 |
g() |
| 909 |
i += q.x |
| 910 |
return 0 |
| 911 |
def fix_graph_of_g(translator): |
| 912 |
from rpython.translator.translator import graphof |
| 913 |
from rpython.flowspace.model import Constant |
| 914 |
from rpython.rtyper.lltypesystem import rffi |
| 915 |
layoutbuilder = cls.ensure_layoutbuilder(translator) |
| 916 |
type_id = layoutbuilder.get_type_id(P) |
| 917 |
|
| 918 |
|
| 919 |
graph = graphof(translator, g) |
| 920 |
for op in graph.startblock.operations: |
| 921 |
if op.opname == 'do_malloc_fixedsize': |
| 922 |
op.args = [Constant(type_id, llgroup.HALFWORD), |
| 923 |
Constant(llmemory.sizeof(P), lltype.Signed), |
| 924 |
Constant(False, lltype.Bool), |
| 925 |
Constant(False, lltype.Bool), |
| 926 |
Constant(False, lltype.Bool)] |
| 927 |
break |
| 928 |
else: |
| 929 |
assert 0, "oups, not found" |
| 930 |
return f, None, fix_graph_of_g |
| 931 |
|
| 932 |
def test_do_malloc_operations_in_call(self): |
| 933 |
run = self.runner("do_malloc_operations_in_call") |
| 934 |
run([]) |
| 935 |
|
| 936 |
def define_gc_heap_stats(cls): |
| 937 |
S = lltype.GcStruct('S', ('x', lltype.Signed)) |
| 938 |
l1 = [] |
| 939 |
l2 = [] |
| 940 |
l3 = [] |
| 941 |
l4 = [] |
| 942 |
|
| 943 |
def f(): |
| 944 |
for i in range(10): |
| 945 |
s = lltype.malloc(S) |
| 946 |
l1.append(s) |
| 947 |
l2.append(s) |
| 948 |
if i < 3: |
| 949 |
l3.append(s) |
| 950 |
l4.append(s) |
| 951 |
|
| 952 |
|
| 953 |
llop.gc__collect(lltype.Void) |
| 954 |
tb = rgc._heap_stats() |
| 955 |
a = 0 |
| 956 |
nr = 0 |
| 957 |
b = 0 |
| 958 |
c = 0 |
| 959 |
d = 0 |
| 960 |
e = 0 |
| 961 |
for i in range(len(tb)): |
| 962 |
if tb[i].count == 10: |
| 963 |
a += 1 |
| 964 |
nr = i |
| 965 |
if tb[i].count > 50: |
| 966 |
d += 1 |
| 967 |
for i in range(len(tb)): |
| 968 |
if tb[i].count == 4: |
| 969 |
b += 1 |
| 970 |
c += tb[i].links[nr] |
| 971 |
e += tb[i].size |
| 972 |
return d * 1000 + c * 100 + b * 10 + a |
| 973 |
return f |
| 974 |
|
| 975 |
def test_gc_heap_stats(self): |
| 976 |
py.test.skip("this test makes the following test crash. Investigate.") |
| 977 |
run = self.runner("gc_heap_stats") |
| 978 |
res = run([]) |
| 979 |
assert res % 10000 == 2611 |
| 980 |
totsize = (res / 10000) |
| 981 |
size_of_int = rffi.sizeof(lltype.Signed) |
| 982 |
assert (totsize - 26 * size_of_int) % 4 == 0 |
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
def define_writebarrier_before_copy(cls): |
| 987 |
S = lltype.GcStruct('S', ('x', lltype.Char)) |
| 988 |
TP = lltype.GcArray(lltype.Ptr(S)) |
| 989 |
def fn(): |
| 990 |
l = lltype.malloc(TP, 100) |
| 991 |
l2 = lltype.malloc(TP, 100) |
| 992 |
for i in range(100): |
| 993 |
l[i] = lltype.malloc(S) |
| 994 |
rgc.ll_arraycopy(l, l2, 50, 0, 50) |
| 995 |
|
| 996 |
x = [] |
| 997 |
for i in range(20): |
| 998 |
x.append((1, lltype.malloc(S))) |
| 999 |
for i in range(50): |
| 1000 |
assert l2[i] == l[50 + i] |
| 1001 |
return 0 |
| 1002 |
|
| 1003 |
return fn |
| 1004 |
|
| 1005 |
def test_writebarrier_before_copy(self): |
| 1006 |
run = self.runner("writebarrier_before_copy") |
| 1007 |
run([]) |
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
class TestSemiSpaceGC(GenericMovingGCTests): |
| 1012 |
gcname = "semispace" |
| 1013 |
GC_CAN_SHRINK_ARRAY = True |
| 1014 |
|
| 1015 |
class gcpolicy(gc.BasicFrameworkGcPolicy): |
| 1016 |
class transformerclass(shadowstack.ShadowStackFrameworkGCTransformer): |
| 1017 |
from rpython.memory.gc.semispace import SemiSpaceGC as GCClass |
| 1018 |
GC_PARAMS = {'space_size': 512*WORD, |
| 1019 |
'translated_to_c': False} |
| 1020 |
root_stack_depth = 200 |
| 1021 |
|
| 1022 |
class TestGenerationGC(GenericMovingGCTests): |
| 1023 |
gcname = "generation" |
| 1024 |
GC_CAN_SHRINK_ARRAY = True |
| 1025 |
|
| 1026 |
class gcpolicy(gc.BasicFrameworkGcPolicy): |
| 1027 |
class transformerclass(shadowstack.ShadowStackFrameworkGCTransformer): |
| 1028 |
from rpython.memory.gc.generation import GenerationGC as \ |
| 1029 |
GCClass |
| 1030 |
GC_PARAMS = {'space_size': 512*WORD, |
| 1031 |
'nursery_size': 32*WORD, |
| 1032 |
'translated_to_c': False} |
| 1033 |
root_stack_depth = 200 |
| 1034 |
|
| 1035 |
def define_weakref_across_minor_collection(cls): |
| 1036 |
import weakref |
| 1037 |
class A: |
| 1038 |
pass |
| 1039 |
def f(): |
| 1040 |
x = 20 |
| 1041 |
a = A() |
| 1042 |
a.foo = x |
| 1043 |
ref = weakref.ref(a) |
| 1044 |
all = [None] * x |
| 1045 |
i = 0 |
| 1046 |
while i < x: |
| 1047 |
all[i] = [i] * i |
| 1048 |
i += 1 |
| 1049 |
assert ref() is a |
| 1050 |
llop.gc__collect(lltype.Void) |
| 1051 |
assert ref() is a |
| 1052 |
return a.foo + len(all) |
| 1053 |
return f |
| 1054 |
|
| 1055 |
def test_weakref_across_minor_collection(self): |
| 1056 |
run = self.runner("weakref_across_minor_collection") |
| 1057 |
res = run([]) |
| 1058 |
assert res == 20 + 20 |
| 1059 |
|
| 1060 |
def define_nongc_static_root_minor_collect(cls): |
| 1061 |
T1 = lltype.GcStruct("C", ('x', lltype.Signed)) |
| 1062 |
T2 = lltype.Struct("C", ('p', lltype.Ptr(T1))) |
| 1063 |
static = lltype.malloc(T2, immortal=True) |
| 1064 |
def f(): |
| 1065 |
t1 = lltype.malloc(T1) |
| 1066 |
t1.x = 42 |
| 1067 |
static.p = t1 |
| 1068 |
x = 20 |
| 1069 |
all = [None] * x |
| 1070 |
i = 0 |
| 1071 |
while i < x: |
| 1072 |
all[i] = [i] * i |
| 1073 |
i += 1 |
| 1074 |
i = static.p.x |
| 1075 |
llop.gc__collect(lltype.Void) |
| 1076 |
return static.p.x + i |
| 1077 |
def cleanup(): |
| 1078 |
static.p = lltype.nullptr(T1) |
| 1079 |
return f, cleanup, None |
| 1080 |
|
| 1081 |
def test_nongc_static_root_minor_collect(self): |
| 1082 |
run = self.runner("nongc_static_root_minor_collect") |
| 1083 |
res = run([]) |
| 1084 |
assert res == 84 |
| 1085 |
|
| 1086 |
|
| 1087 |
def define_static_root_minor_collect(cls): |
| 1088 |
class A: |
| 1089 |
pass |
| 1090 |
class B: |
| 1091 |
pass |
| 1092 |
static = A() |
| 1093 |
static.p = None |
| 1094 |
def f(): |
| 1095 |
t1 = B() |
| 1096 |
t1.x = 42 |
| 1097 |
static.p = t1 |
| 1098 |
x = 20 |
| 1099 |
all = [None] * x |
| 1100 |
i = 0 |
| 1101 |
while i < x: |
| 1102 |
all[i] = [i] * i |
| 1103 |
i += 1 |
| 1104 |
i = static.p.x |
| 1105 |
llop.gc__collect(lltype.Void) |
| 1106 |
return static.p.x + i |
| 1107 |
def cleanup(): |
| 1108 |
static.p = None |
| 1109 |
return f, cleanup, None |
| 1110 |
|
| 1111 |
def test_static_root_minor_collect(self): |
| 1112 |
run = self.runner("static_root_minor_collect") |
| 1113 |
res = run([]) |
| 1114 |
assert res == 84 |
| 1115 |
|
| 1116 |
|
| 1117 |
def define_many_weakrefs(cls): |
| 1118 |
|
| 1119 |
|
| 1120 |
import weakref |
| 1121 |
class A: |
| 1122 |
pass |
| 1123 |
def f(): |
| 1124 |
a = A() |
| 1125 |
i = 0 |
| 1126 |
while i < 17: |
| 1127 |
ref = weakref.ref(a) |
| 1128 |
assert ref() is a |
| 1129 |
i += 1 |
| 1130 |
return 0 |
| 1131 |
|
| 1132 |
return f |
| 1133 |
|
| 1134 |
def test_many_weakrefs(self): |
| 1135 |
run = self.runner("many_weakrefs") |
| 1136 |
run([]) |
| 1137 |
|
| 1138 |
def define_immutable_to_old_promotion(cls): |
| 1139 |
T_CHILD = lltype.Ptr(lltype.GcStruct('Child', ('field', lltype.Signed))) |
| 1140 |
T_PARENT = lltype.Ptr(lltype.GcStruct('Parent', ('sub', T_CHILD))) |
| 1141 |
child = lltype.malloc(T_CHILD.TO) |
| 1142 |
child2 = lltype.malloc(T_CHILD.TO) |
| 1143 |
parent = lltype.malloc(T_PARENT.TO) |
| 1144 |
parent2 = lltype.malloc(T_PARENT.TO) |
| 1145 |
parent.sub = child |
| 1146 |
child.field = 3 |
| 1147 |
parent2.sub = child2 |
| 1148 |
child2.field = 8 |
| 1149 |
|
| 1150 |
T_ALL = lltype.Ptr(lltype.GcArray(T_PARENT)) |
| 1151 |
all = lltype.malloc(T_ALL.TO, 2) |
| 1152 |
all[0] = parent |
| 1153 |
all[1] = parent2 |
| 1154 |
|
| 1155 |
def f(x, y): |
| 1156 |
res = all[x] |
| 1157 |
|
| 1158 |
return res.sub.field |
| 1159 |
|
| 1160 |
return f |
| 1161 |
|
| 1162 |
def test_immutable_to_old_promotion(self): |
| 1163 |
run, transformer = self.runner("immutable_to_old_promotion", transformer=True) |
| 1164 |
run([1, 4]) |
| 1165 |
if not transformer.GCClass.prebuilt_gc_objects_are_static_roots: |
| 1166 |
assert len(transformer.layoutbuilder.addresses_of_static_ptrs) == 0 |
| 1167 |
else: |
| 1168 |
assert len(transformer.layoutbuilder.addresses_of_static_ptrs) >= 4 |
| 1169 |
|
| 1170 |
|
| 1171 |
|
| 1172 |
|
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 1176 |
|
| 1177 |
|
| 1178 |
|
| 1179 |
|
| 1180 |
def define_adr_of_nursery(cls): |
| 1181 |
class A(object): |
| 1182 |
pass |
| 1183 |
|
| 1184 |
def f(): |
| 1185 |
|
| 1186 |
a = A() |
| 1187 |
nf_a = llop.gc_adr_of_nursery_free(llmemory.Address) |
| 1188 |
nt_a = llop.gc_adr_of_nursery_top(llmemory.Address) |
| 1189 |
nf0 = nf_a.address[0] |
| 1190 |
nt0 = nt_a.address[0] |
| 1191 |
a0 = A() |
| 1192 |
a1 = A() |
| 1193 |
nf1 = nf_a.address[0] |
| 1194 |
nt1 = nt_a.address[0] |
| 1195 |
assert nf1 > nf0 |
| 1196 |
assert nt1 > nf1 |
| 1197 |
assert nt1 == nt0 |
| 1198 |
return 0 |
| 1199 |
|
| 1200 |
return f |
| 1201 |
|
| 1202 |
def test_adr_of_nursery(self): |
| 1203 |
run = self.runner("adr_of_nursery") |
| 1204 |
res = run([]) |
| 1205 |
|
| 1206 |
|
| 1207 |
class TestGenerationalNoFullCollectGC(GCTest): |
| 1208 |
|
| 1209 |
|
| 1210 |
|
| 1211 |
gcname = "generation" |
| 1212 |
|
| 1213 |
class gcpolicy(gc.BasicFrameworkGcPolicy): |
| 1214 |
class transformerclass(shadowstack.ShadowStackFrameworkGCTransformer): |
| 1215 |
from rpython.memory.gc.generation import GenerationGC |
| 1216 |
class GCClass(GenerationGC): |
| 1217 |
__ready = False |
| 1218 |
def setup(self): |
| 1219 |
from rpython.memory.gc.generation import GenerationGC |
| 1220 |
GenerationGC.setup(self) |
| 1221 |
self.__ready = True |
| 1222 |
def semispace_collect(self, size_changing=False): |
| 1223 |
ll_assert(not self.__ready, |
| 1224 |
"no full collect should occur in this test") |
| 1225 |
def _teardown(self): |
| 1226 |
self.__ready = False |
| 1227 |
GenerationGC._teardown(self) |
| 1228 |
|
| 1229 |
GC_PARAMS = {'space_size': 512*WORD, |
| 1230 |
'nursery_size': 128*WORD, |
| 1231 |
'translated_to_c': False} |
| 1232 |
root_stack_depth = 200 |
| 1233 |
|
| 1234 |
def define_working_nursery(cls): |
| 1235 |
def f(): |
| 1236 |
total = 0 |
| 1237 |
i = 0 |
| 1238 |
while i < 40: |
| 1239 |
lst = [] |
| 1240 |
j = 0 |
| 1241 |
while j < 5: |
| 1242 |
lst.append(i*j) |
| 1243 |
j += 1 |
| 1244 |
total += len(lst) |
| 1245 |
i += 1 |
| 1246 |
return total |
| 1247 |
return f |
| 1248 |
|
| 1249 |
def test_working_nursery(self): |
| 1250 |
run = self.runner("working_nursery") |
| 1251 |
res = run([]) |
| 1252 |
assert res == 40 * 5 |
| 1253 |
|
| 1254 |
class TestHybridGC(TestGenerationGC): |
| 1255 |
gcname = "hybrid" |
| 1256 |
|
| 1257 |
class gcpolicy(gc.BasicFrameworkGcPolicy): |
| 1258 |
class transformerclass(shadowstack.ShadowStackFrameworkGCTransformer): |
| 1259 |
from rpython.memory.gc.hybrid import HybridGC as GCClass |
| 1260 |
GC_PARAMS = {'space_size': 512*WORD, |
| 1261 |
'nursery_size': 32*WORD, |
| 1262 |
'large_object': 8*WORD, |
| 1263 |
'translated_to_c': False} |
| 1264 |
root_stack_depth = 200 |
| 1265 |
|
| 1266 |
def define_ref_from_rawmalloced_to_regular(cls): |
| 1267 |
import gc |
| 1268 |
S = lltype.GcStruct('S', ('x', lltype.Signed)) |
| 1269 |
A = lltype.GcStruct('A', ('p', lltype.Ptr(S)), |
| 1270 |
('a', lltype.Array(lltype.Char))) |
| 1271 |
def setup(j): |
| 1272 |
p = lltype.malloc(S) |
| 1273 |
p.x = j*2 |
| 1274 |
lst = lltype.malloc(A, j) |
| 1275 |
|
| 1276 |
|
| 1277 |
|
| 1278 |
lst.p = p |
| 1279 |
return lst |
| 1280 |
def f(i, j): |
| 1281 |
lst = setup(j) |
| 1282 |
gc.collect() |
| 1283 |
return lst.p.x |
| 1284 |
return f |
| 1285 |
|
| 1286 |
def test_ref_from_rawmalloced_to_regular(self): |
| 1287 |
run = self.runner("ref_from_rawmalloced_to_regular") |
| 1288 |
res = run([100, 100]) |
| 1289 |
assert res == 200 |
| 1290 |
|
| 1291 |
def define_write_barrier_direct(cls): |
| 1292 |
from rpython.rlib import rgc |
| 1293 |
S = lltype.GcForwardReference() |
| 1294 |
S.become(lltype.GcStruct('S', |
| 1295 |
('x', lltype.Signed), |
| 1296 |
('prev', lltype.Ptr(S)), |
| 1297 |
('next', lltype.Ptr(S)))) |
| 1298 |
s0 = lltype.malloc(S, immortal=True) |
| 1299 |
def f(): |
| 1300 |
s = lltype.malloc(S) |
| 1301 |
s.x = 42 |
| 1302 |
llop.bare_setfield(lltype.Void, s0, void('next'), s) |
| 1303 |
llop.gc_writebarrier(lltype.Void, llmemory.cast_ptr_to_adr(s0)) |
| 1304 |
rgc.collect(0) |
| 1305 |
return s0.next.x |
| 1306 |
|
| 1307 |
def cleanup(): |
| 1308 |
s0.next = lltype.nullptr(S) |
| 1309 |
|
| 1310 |
return f, cleanup, None |
| 1311 |
|
| 1312 |
def test_write_barrier_direct(self): |
| 1313 |
run = self.runner("write_barrier_direct") |
| 1314 |
res = run([]) |
| 1315 |
assert res == 42 |
| 1316 |
|
| 1317 |
class TestMiniMarkGC(TestHybridGC): |
| 1318 |
gcname = "minimark" |
| 1319 |
GC_CAN_TEST_ID = True |
| 1320 |
|
| 1321 |
class gcpolicy(gc.BasicFrameworkGcPolicy): |
| 1322 |
class transformerclass(shadowstack.ShadowStackFrameworkGCTransformer): |
| 1323 |
from rpython.memory.gc.minimark import MiniMarkGC as GCClass |
| 1324 |
GC_PARAMS = {'nursery_size': 32*WORD, |
| 1325 |
'page_size': 16*WORD, |
| 1326 |
'arena_size': 64*WORD, |
| 1327 |
'small_request_threshold': 5*WORD, |
| 1328 |
'large_object': 8*WORD, |
| 1329 |
'card_page_indices': 4, |
| 1330 |
'translated_to_c': False, |
| 1331 |
} |
| 1332 |
root_stack_depth = 200 |
| 1333 |
|
| 1334 |
def define_no_clean_setarrayitems(cls): |
| 1335 |
|
| 1336 |
|
| 1337 |
|
| 1338 |
S = lltype.GcStruct('S', ('x', lltype.Signed)) |
| 1339 |
A = lltype.GcArray(lltype.Ptr(S)) |
| 1340 |
def sub(lst): |
| 1341 |
lst[15] = lltype.malloc(S) |
| 1342 |
lst[15].x = 123 |
| 1343 |
lst[0] = lst[15] |
| 1344 |
def f(): |
| 1345 |
lst = lltype.malloc(A, 16) |
| 1346 |
rgc.collect() |
| 1347 |
sub(lst) |
| 1348 |
null = lltype.nullptr(S) |
| 1349 |
lst[15] = null |
| 1350 |
rgc.collect() |
| 1351 |
return lst[0].x |
| 1352 |
return f |
| 1353 |
|
| 1354 |
def test_no_clean_setarrayitems(self): |
| 1355 |
run = self.runner("no_clean_setarrayitems") |
| 1356 |
res = run([]) |
| 1357 |
assert res == 123 |
| 1358 |
|
| 1359 |
def define_nursery_hash_base(cls): |
| 1360 |
class A: |
| 1361 |
pass |
| 1362 |
def fn(): |
| 1363 |
objects = [] |
| 1364 |
hashes = [] |
| 1365 |
for i in range(200): |
| 1366 |
rgc.collect(0) |
| 1367 |
obj = A() |
| 1368 |
objects.append(obj) |
| 1369 |
hashes.append(compute_identity_hash(obj)) |
| 1370 |
unique = {} |
| 1371 |
for i in range(len(objects)): |
| 1372 |
assert compute_identity_hash(objects[i]) == hashes[i] |
| 1373 |
unique[hashes[i]] = None |
| 1374 |
return len(unique) |
| 1375 |
return fn |
| 1376 |
|
| 1377 |
def test_nursery_hash_base(self): |
| 1378 |
res = self.runner('nursery_hash_base') |
| 1379 |
assert res([]) >= 195 |
| 1380 |
|
| 1381 |
def define_instantiate_nonmovable(cls): |
| 1382 |
from rpython.rlib import objectmodel |
| 1383 |
from rpython.rtyper import annlowlevel |
| 1384 |
class A: |
| 1385 |
pass |
| 1386 |
def fn(): |
| 1387 |
a1 = A() |
| 1388 |
a = objectmodel.instantiate(A, nonmovable=True) |
| 1389 |
a.next = a1 |
| 1390 |
res = rgc.can_move(annlowlevel.cast_instance_to_base_ptr(a)) |
| 1391 |
rgc.collect() |
| 1392 |
objectmodel.keepalive_until_here(a) |
| 1393 |
return res |
| 1394 |
return fn |
| 1395 |
|
| 1396 |
def test_instantiate_nonmovable(self): |
| 1397 |
res = self.runner('instantiate_nonmovable') |
| 1398 |
assert res([]) == 0 |
| 1399 |
|
| 1400 |
|
| 1401 |
class GcHooksStats(object): |
| 1402 |
minors = 0 |
| 1403 |
steps = 0 |
| 1404 |
collects = 0 |
| 1405 |
|
| 1406 |
def reset(self): |
| 1407 |
|
| 1408 |
|
| 1409 |
|
| 1410 |
|
| 1411 |
self.minors = NonConstant(0) |
| 1412 |
self.steps = NonConstant(0) |
| 1413 |
self.collects = NonConstant(0) |
| 1414 |
|
| 1415 |
|
| 1416 |
class MyGcHooks(GcHooks): |
| 1417 |
|
| 1418 |
def __init__(self, stats=None): |
| 1419 |
self.stats = stats or GcHooksStats() |
| 1420 |
|
| 1421 |
def is_gc_minor_enabled(self): |
| 1422 |
return True |
| 1423 |
|
| 1424 |
def is_gc_collect_step_enabled(self): |
| 1425 |
return True |
| 1426 |
|
| 1427 |
def is_gc_collect_enabled(self): |
| 1428 |
return True |
| 1429 |
|
| 1430 |
def on_gc_minor(self, duration, total_memory_used, pinned_objects): |
| 1431 |
self.stats.minors += 1 |
| 1432 |
|
| 1433 |
def on_gc_collect_step(self, duration, oldstate, newstate): |
| 1434 |
self.stats.steps += 1 |
| 1435 |
|
| 1436 |
def on_gc_collect(self, num_major_collects, |
| 1437 |
arenas_count_before, arenas_count_after, |
| 1438 |
arenas_bytes, rawmalloc_bytes_before, |
| 1439 |
rawmalloc_bytes_after, pinned_objects): |
| 1440 |
self.stats.collects += 1 |
| 1441 |
|
| 1442 |
|
| 1443 |
class TestIncrementalMiniMarkGC(TestMiniMarkGC): |
| 1444 |
gcname = "incminimark" |
| 1445 |
|
| 1446 |
class gcpolicy(gc.BasicFrameworkGcPolicy): |
| 1447 |
class transformerclass(shadowstack.ShadowStackFrameworkGCTransformer): |
| 1448 |
from rpython.memory.gc.incminimark import IncrementalMiniMarkGC \ |
| 1449 |
as GCClass |
| 1450 |
GC_PARAMS = {'nursery_size': 32*WORD, |
| 1451 |
'page_size': 16*WORD, |
| 1452 |
'arena_size': 64*WORD, |
| 1453 |
'small_request_threshold': 5*WORD, |
| 1454 |
'large_object': 8*WORD, |
| 1455 |
'card_page_indices': 4, |
| 1456 |
'translated_to_c': False, |
| 1457 |
} |
| 1458 |
root_stack_depth = 200 |
| 1459 |
|
| 1460 |
gchooks = MyGcHooks() |
| 1461 |
|
| 1462 |
def define_malloc_array_of_gcptr(self): |
| 1463 |
S = lltype.GcStruct('S', ('x', lltype.Signed)) |
| 1464 |
A = lltype.GcArray(lltype.Ptr(S)) |
| 1465 |
def f(): |
| 1466 |
lst = lltype.malloc(A, 5) |
| 1467 |
return (lst[0] == lltype.nullptr(S) |
| 1468 |
and lst[1] == lltype.nullptr(S) |
| 1469 |
and lst[2] == lltype.nullptr(S) |
| 1470 |
and lst[3] == lltype.nullptr(S) |
| 1471 |
and lst[4] == lltype.nullptr(S)) |
| 1472 |
return f |
| 1473 |
|
| 1474 |
def test_malloc_array_of_gcptr(self): |
| 1475 |
run = self.runner('malloc_array_of_gcptr') |
| 1476 |
res = run([]) |
| 1477 |
assert res |
| 1478 |
|
| 1479 |
def define_malloc_struct_of_gcptr(cls): |
| 1480 |
S1 = lltype.GcStruct('S', ('x', lltype.Signed)) |
| 1481 |
S = lltype.GcStruct('S', |
| 1482 |
('x', lltype.Signed), |
| 1483 |
('filed1', lltype.Ptr(S1)), |
| 1484 |
('filed2', lltype.Ptr(S1))) |
| 1485 |
s0 = lltype.malloc(S) |
| 1486 |
def f(): |
| 1487 |
return (s0.filed1 == lltype.nullptr(S1) and s0.filed2 == lltype.nullptr(S1)) |
| 1488 |
return f |
| 1489 |
|
| 1490 |
def test_malloc_struct_of_gcptr(self): |
| 1491 |
run = self.runner("malloc_struct_of_gcptr") |
| 1492 |
res = run([]) |
| 1493 |
assert res |
| 1494 |
|
| 1495 |
def define_gc_hooks(cls): |
| 1496 |
gchooks = cls.gchooks |
| 1497 |
|
| 1498 |
|
| 1499 |
stats = gchooks.stats |
| 1500 |
def f(): |
| 1501 |
stats.reset() |
| 1502 |
|
| 1503 |
llop.gc__collect(lltype.Void) |
| 1504 |
llop.gc__collect(lltype.Void) |
| 1505 |
return (10000 * stats.collects + |
| 1506 |
100 * stats.steps + |
| 1507 |
1 * stats.minors) |
| 1508 |
return f |
| 1509 |
|
| 1510 |
def test_gc_hooks(self): |
| 1511 |
run = self.runner("gc_hooks") |
| 1512 |
count = run([]) |
| 1513 |
collects, count = divmod(count, 10000) |
| 1514 |
steps, minors = divmod(count, 100) |
| 1515 |
|
| 1516 |
|
| 1517 |
|
| 1518 |
|
| 1519 |
assert collects == 2 |
| 1520 |
assert steps == 4 * collects |
| 1521 |
assert minors == steps |
| 1522 |
|
| 1523 |
|
| 1524 |
|
| 1525 |
|
| 1526 |
class TaggedPointerGCTests(GCTest): |
| 1527 |
taggedpointers = True |
| 1528 |
|
| 1529 |
def define_tagged_simple(cls): |
| 1530 |
class Unrelated(object): |
| 1531 |
pass |
| 1532 |
|
| 1533 |
u = Unrelated() |
| 1534 |
u.x = UnboxedObject(47) |
| 1535 |
def fn(n): |
| 1536 |
rgc.collect() |
| 1537 |
if n > 0: |
| 1538 |
x = BoxedObject(n) |
| 1539 |
else: |
| 1540 |
x = UnboxedObject(n) |
| 1541 |
u.x = x |
| 1542 |
rgc.collect() |
| 1543 |
return x.meth(100) |
| 1544 |
def func(): |
| 1545 |
return fn(1000) + fn(-1000) |
| 1546 |
assert func() == 205 |
| 1547 |
return func |
| 1548 |
|
| 1549 |
def test_tagged_simple(self): |
| 1550 |
func = self.runner("tagged_simple") |
| 1551 |
res = func([]) |
| 1552 |
assert res == 205 |
| 1553 |
|
| 1554 |
def define_tagged_prebuilt(cls): |
| 1555 |
|
| 1556 |
class F: |
| 1557 |
pass |
| 1558 |
|
| 1559 |
f = F() |
| 1560 |
f.l = [UnboxedObject(10)] |
| 1561 |
def fn(n): |
| 1562 |
if n > 0: |
| 1563 |
x = BoxedObject(n) |
| 1564 |
else: |
| 1565 |
x = UnboxedObject(n) |
| 1566 |
f.l.append(x) |
| 1567 |
rgc.collect() |
| 1568 |
return f.l[-1].meth(100) |
| 1569 |
def func(): |
| 1570 |
return fn(1000) ^ fn(-1000) |
| 1571 |
assert func() == -1999 |
| 1572 |
return func |
| 1573 |
|
| 1574 |
def test_tagged_prebuilt(self): |
| 1575 |
func = self.runner("tagged_prebuilt") |
| 1576 |
res = func([]) |
| 1577 |
assert res == -1999 |
| 1578 |
|
| 1579 |
def define_gettypeid(cls): |
| 1580 |
class A(object): |
| 1581 |
pass |
| 1582 |
|
| 1583 |
def fn(): |
| 1584 |
a = A() |
| 1585 |
return rgc.get_typeid(a) |
| 1586 |
|
| 1587 |
return fn |
| 1588 |
|
| 1589 |
def test_gettypeid(self): |
| 1590 |
func = self.runner("gettypeid") |
| 1591 |
res = func([]) |
| 1592 |
print res |
| 1593 |
|
| 1594 |
|
| 1595 |
from rpython.rlib.objectmodel import UnboxedValue |
| 1596 |
|
| 1597 |
class TaggedBase(object): |
| 1598 |
__slots__ = () |
| 1599 |
def meth(self, x): |
| 1600 |
raise NotImplementedError |
| 1601 |
|
| 1602 |
class BoxedObject(TaggedBase): |
| 1603 |
attrvalue = 66 |
| 1604 |
def __init__(self, normalint): |
| 1605 |
self.normalint = normalint |
| 1606 |
def meth(self, x): |
| 1607 |
return self.normalint + x + 2 |
| 1608 |
|
| 1609 |
class UnboxedObject(TaggedBase, UnboxedValue): |
| 1610 |
__slots__ = 'smallint' |
| 1611 |
def meth(self, x): |
| 1612 |
return self.smallint + x + 3 |
| 1613 |
|
| 1614 |
|
| 1615 |
class TestHybridTaggedPointerGC(TaggedPointerGCTests): |
| 1616 |
gcname = "hybrid" |
| 1617 |
|
| 1618 |
class gcpolicy(gc.BasicFrameworkGcPolicy): |
| 1619 |
class transformerclass(shadowstack.ShadowStackFrameworkGCTransformer): |
| 1620 |
from rpython.memory.gc.generation import GenerationGC as \ |
| 1621 |
GCClass |
| 1622 |
GC_PARAMS = {'space_size': 512*WORD, |
| 1623 |
'nursery_size': 32*WORD, |
| 1624 |
'translated_to_c': False} |
| 1625 |
root_stack_depth = 200 |
| 1626 |
|
| 1627 |
def test_gettypeid(self): |
| 1628 |
py.test.skip("fails for obscure reasons") |