pypy / pypy / rpython / memory / gc / test / test_stmgc.py

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
from pypy.rpython.lltypesystem import lltype, llmemory, llarena, rffi
from pypy.rpython.memory.gc.stmgc import StmGC, PRIMITIVE_SIZES, WORD
from pypy.rpython.memory.gc.stmgc import GCFLAG_GLOBAL, GCFLAG_WAS_COPIED


S = lltype.GcStruct('S', ('a', lltype.Signed), ('b', lltype.Signed),
                         ('c', lltype.Signed))
ofs_a = llmemory.offsetof(S, 'a')

SR = lltype.GcForwardReference()
SR.become(lltype.GcStruct('SR', ('s1', lltype.Ptr(S)),
                                ('sr2', lltype.Ptr(SR)),
                                ('sr3', lltype.Ptr(SR))))


class FakeStmOperations:
    # The point of this class is to make sure about the distinction between
    # RPython code in the GC versus C code in translator/stm/src_stm.  This
    # class contains a fake implementation of what should be in C.  So almost
    # any use of 'self._gc' is wrong here: it's stmgc.py that should call
    # et.c, and not the other way around.

    threadnum = 0          # 0 = main thread; 1,2,3... = transactional threads

    def setup_size_getter(self, getsize_fn):
        self._getsize_fn = getsize_fn

    def set_tls(self, tls, in_main_thread):
        assert lltype.typeOf(tls) == llmemory.Address
        assert tls
        if self.threadnum == 0:
            assert in_main_thread == 1
            assert not hasattr(self, '_tls_dict')
            self._tls_dict = {0: tls}
            self._tldicts = {0: {}}
            self._tldicts_iterators = {}
            self._transactional_copies = []
        else:
            assert in_main_thread == 0
            self._tls_dict[self.threadnum] = tls
            self._tldicts[self.threadnum] = {}

    def get_tls(self):
        return self._tls_dict[self.threadnum]

    def del_tls(self):
        del self._tls_dict[self.threadnum]
        del self._tldicts[self.threadnum]

    def tldict_lookup(self, obj):
        assert lltype.typeOf(obj) == llmemory.Address
        assert obj
        tldict = self._tldicts[self.threadnum]
        return tldict.get(obj, llmemory.NULL)

    def tldict_add(self, obj, localobj):
        assert lltype.typeOf(obj) == llmemory.Address
        assert lltype.typeOf(localobj) == llmemory.Address
        tldict = self._tldicts[self.threadnum]
        assert obj not in tldict
        tldict[obj] = localobj

    def enum_tldict_start(self):
        it = self._tldicts[self.threadnum].iteritems()
        self._tldicts_iterators[self.threadnum] = [it, None, None]

    def enum_tldict_find_next(self):
        state = self._tldicts_iterators[self.threadnum]
        try:
            next_key, next_value = state[0].next()
        except StopIteration:
            state[1] = None
            state[2] = None
            del self._tldicts_iterators[self.threadnum]
            return False
        state[1] = next_key
        state[2] = next_value
        return True

    def enum_tldict_globalobj(self):
        state = self._tldicts_iterators[self.threadnum]
        assert state[1] is not None
        return state[1]

    def enum_tldict_localobj(self):
        state = self._tldicts_iterators[self.threadnum]
        assert state[2] is not None
        return state[2]

    def _get_stm_reader(size, TYPE):
        assert rffi.sizeof(TYPE) == size
        PTYPE = rffi.CArrayPtr(TYPE)
        def stm_reader(self, obj, offset):
            hdr = self._gc.header(obj)
            if hdr.tid & GCFLAG_WAS_COPIED != 0:
                localobj = self.tldict_lookup(obj)
                if localobj:
                    assert self._gc.header(localobj).tid & GCFLAG_GLOBAL == 0
                    adr = rffi.cast(PTYPE, localobj + offset)
                    return adr[0]
            return 'stm_ll_read_int%d(%r, %r)' % (size, obj, offset)
        return stm_reader

    for _size, _TYPE in PRIMITIVE_SIZES.items():
        _func = _get_stm_reader(_size, _TYPE)
        locals()['stm_read_int%d' % _size] = _func

    def stm_copy_transactional_to_raw(self, srcobj, dstobj, size):
        llmemory.raw_memcopy(srcobj, dstobj, size)
        self._transactional_copies.append((srcobj, dstobj))


def fake_get_size(obj):
    TYPE = obj.ptr._TYPE.TO
    if isinstance(TYPE, lltype.GcStruct):
        return llmemory.sizeof(TYPE)
    else:
        assert 0

def fake_trace(obj, callback, arg):
    TYPE = obj.ptr._TYPE.TO
    if TYPE == S:
        ofslist = []     # no pointers in S
    elif TYPE == SR:
        ofslist = [llmemory.offsetof(SR, 's1'),
                   llmemory.offsetof(SR, 'sr2'),
                   llmemory.offsetof(SR, 'sr3')]
    else:
        assert 0
    for ofs in ofslist:
        addr = obj + ofs
        if addr.address[0]:
            callback(addr, arg)


class TestBasic:
    GCClass = StmGC

    def setup_method(self, meth):
        from pypy.config.pypyoption import get_pypy_config
        config = get_pypy_config(translating=True).translation
        self.gc = self.GCClass(config, FakeStmOperations(),
                               translated_to_c=False)
        self.gc.stm_operations._gc = self.gc
        self.gc.DEBUG = True
        self.gc.get_size = fake_get_size
        self.gc.trace = fake_trace
        self.gc.setup()

    def teardown_method(self, meth):
        if not hasattr(self, 'gc'):
            return
        for key in self.gc.stm_operations._tls_dict.keys():
            if key != 0:
                self.gc.stm_operations.threadnum = key
                self.gc.teardown_thread()

    # ----------
    # test helpers
    def malloc(self, STRUCT):
        size = llarena.round_up_for_allocation(llmemory.sizeof(STRUCT))
        gcref = self.gc.malloc_fixedsize_clear(123, size)
        realobj = lltype.cast_opaque_ptr(lltype.Ptr(STRUCT), gcref)
        addr = llmemory.cast_ptr_to_adr(realobj)
        return realobj, addr
    def select_thread(self, threadnum):
        self.gc.stm_operations.threadnum = threadnum
        if threadnum not in self.gc.stm_operations._tls_dict:
            self.gc.setup_thread(False)
    def gcsize(self, S):
        return (llmemory.raw_malloc_usage(llmemory.sizeof(self.gc.HDR)) +
                llmemory.raw_malloc_usage(llmemory.sizeof(S)))
    def checkflags(self, obj, must_have_global, must_have_was_copied,
                              must_have_version='?'):
        if lltype.typeOf(obj) != llmemory.Address:
            obj = llmemory.cast_ptr_to_adr(obj)
        hdr = self.gc.header(obj)
        assert (hdr.tid & GCFLAG_GLOBAL != 0) == must_have_global
        assert (hdr.tid & GCFLAG_WAS_COPIED != 0) == must_have_was_copied
        if must_have_version != '?':
            assert hdr.version == must_have_version
    def read_signed(self, obj, offset):
        meth = getattr(self.gc, 'read_int%d' % WORD)
        return meth(obj, offset)

    def test_gc_creation_works(self):
        pass

    def test_allocate_bump_pointer(self):
        a3 = self.gc.allocate_bump_pointer(3)
        a4 = self.gc.allocate_bump_pointer(4)
        a5 = self.gc.allocate_bump_pointer(5)
        a6 = self.gc.allocate_bump_pointer(6)
        assert a4 - a3 == 3
        assert a5 - a4 == 4
        assert a6 - a5 == 5

    def test_malloc_fixedsize_clear(self):
        gcref = self.gc.malloc_fixedsize_clear(123, llmemory.sizeof(S))
        s = lltype.cast_opaque_ptr(lltype.Ptr(S), gcref)
        assert s.a == 0
        assert s.b == 0
        gcref2 = self.gc.malloc_fixedsize_clear(123, llmemory.sizeof(S))
        assert gcref2 != gcref

    def test_malloc_main_vs_thread(self):
        gcref = self.gc.malloc_fixedsize_clear(123, llmemory.sizeof(S))
        obj = llmemory.cast_ptr_to_adr(gcref)
        assert self.gc.header(obj).tid & GCFLAG_GLOBAL != 0
        #
        self.select_thread(1)
        gcref = self.gc.malloc_fixedsize_clear(123, llmemory.sizeof(S))
        obj = llmemory.cast_ptr_to_adr(gcref)
        assert self.gc.header(obj).tid & GCFLAG_GLOBAL == 0

    def test_reader_direct(self):
        s, s_adr = self.malloc(S)
        assert self.gc.header(s_adr).tid & GCFLAG_GLOBAL != 0
        s.a = 42
        value = self.read_signed(s_adr, ofs_a)
        assert value == 'stm_ll_read_int%d(%r, %r)' % (WORD, s_adr, ofs_a)
        #
        self.select_thread(1)
        s, s_adr = self.malloc(S)
        assert self.gc.header(s_adr).tid & GCFLAG_GLOBAL == 0
        self.gc.header(s_adr).tid |= GCFLAG_WAS_COPIED   # should be ignored
        s.a = 42
        value = self.read_signed(s_adr, ofs_a)
        assert value == 42

    def test_reader_through_dict(self):
        s, s_adr = self.malloc(S)
        s.a = 42
        #
        self.select_thread(1)
        t, t_adr = self.malloc(S)
        t.a = 84
        #
        self.gc.header(s_adr).tid |= GCFLAG_WAS_COPIED
        self.gc.stm_operations._tldicts[1][s_adr] = t_adr
        #
        value = self.read_signed(s_adr, ofs_a)
        assert value == 84

    def test_reader_sizes(self):
        for size, TYPE in PRIMITIVE_SIZES.items():
            T = lltype.GcStruct('T', ('a', TYPE))
            ofs_a = llmemory.offsetof(T, 'a')
            #
            self.select_thread(0)
            t, t_adr = self.malloc(T)
            assert self.gc.header(t_adr).tid & GCFLAG_GLOBAL != 0
            t.a = lltype.cast_primitive(TYPE, 42)
            #
            value = getattr(self.gc, 'read_int%d' % size)(t_adr, ofs_a)
            assert value == 'stm_ll_read_int%d(%r, %r)' % (size, t_adr, ofs_a)
            #
            self.select_thread(1)
            t, t_adr = self.malloc(T)
            assert self.gc.header(t_adr).tid & GCFLAG_GLOBAL == 0
            t.a = lltype.cast_primitive(TYPE, 42)
            value = getattr(self.gc, 'read_int%d' % size)(t_adr, ofs_a)
            assert lltype.typeOf(value) == TYPE
            assert lltype.cast_primitive(lltype.Signed, value) == 42

    def test_write_barrier_exists(self):
        self.select_thread(1)
        t, t_adr = self.malloc(S)
        obj = self.gc.write_barrier(t_adr)     # local object
        assert obj == t_adr
        #
        self.select_thread(0)
        s, s_adr = self.malloc(S)
        #
        self.select_thread(1)
        self.gc.header(s_adr).tid |= GCFLAG_WAS_COPIED
        self.gc.header(t_adr).tid |= GCFLAG_WAS_COPIED
        self.gc.stm_operations._tldicts[1][s_adr] = t_adr
        obj = self.gc.write_barrier(s_adr)     # global copied object
        assert obj == t_adr
        assert self.gc.stm_operations._transactional_copies == []

    def test_write_barrier_new(self):
        self.select_thread(0)
        s, s_adr = self.malloc(S)
        s.a = 12
        s.b = 34
        #
        self.select_thread(1)
        t_adr = self.gc.write_barrier(s_adr) # global object, not copied so far
        assert t_adr != s_adr
        t = t_adr.ptr
        assert t.a == 12
        assert t.b == 34
        assert self.gc.stm_operations._transactional_copies == [(s_adr, t_adr)]
        #
        u_adr = self.gc.write_barrier(s_adr)  # again
        assert u_adr == t_adr
        #
        u_adr = self.gc.write_barrier(u_adr)  # local object
        assert u_adr == t_adr

    def test_commit_transaction_empty(self):
        self.select_thread(1)
        s, s_adr = self.malloc(S)
        t, t_adr = self.malloc(S)
        self.gc.collector.commit_transaction()    # no roots
        main_tls = self.gc.main_thread_tls
        assert main_tls.nursery_free == main_tls.nursery_start   # empty

    def test_commit_transaction_no_references(self):
        s, s_adr = self.malloc(S)
        s.b = 12345
        self.select_thread(1)
        t_adr = self.gc.write_barrier(s_adr)   # make a local copy
        t = llmemory.cast_adr_to_ptr(t_adr, lltype.Ptr(S))
        assert s != t
        assert self.gc.header(t_adr).version == s_adr
        t.b = 67890
        #
        main_tls = self.gc.main_thread_tls
        assert main_tls.nursery_free != main_tls.nursery_start  # contains s
        old_value = main_tls.nursery_free
        #
        self.gc.collector.commit_transaction()
        #
        assert main_tls.nursery_free == old_value    # no new object
        assert s.b == 12345     # not updated by the GC code
        assert t.b == 67890     # still valid

    def test_commit_transaction_with_one_reference(self):
        sr, sr_adr = self.malloc(SR)
        assert sr.s1 == lltype.nullptr(S)
        assert sr.sr2 == lltype.nullptr(SR)
        self.select_thread(1)
        tr_adr = self.gc.write_barrier(sr_adr)   # make a local copy
        tr = llmemory.cast_adr_to_ptr(tr_adr, lltype.Ptr(SR))
        assert sr != tr
        t, t_adr = self.malloc(S)
        t.b = 67890
        assert tr.s1 == lltype.nullptr(S)
        assert tr.sr2 == lltype.nullptr(SR)
        tr.s1 = t
        #
        main_tls = self.gc.main_thread_tls
        old_value = main_tls.nursery_free
        #
        self.gc.collector.commit_transaction()
        #
        assert main_tls.nursery_free - old_value == self.gcsize(S)

    def test_commit_transaction_with_graph(self):
        sr1, sr1_adr = self.malloc(SR)
        sr2, sr2_adr = self.malloc(SR)
        self.select_thread(1)
        tr1_adr = self.gc.write_barrier(sr1_adr)   # make a local copy
        tr2_adr = self.gc.write_barrier(sr2_adr)   # make a local copy
        tr1 = llmemory.cast_adr_to_ptr(tr1_adr, lltype.Ptr(SR))
        tr2 = llmemory.cast_adr_to_ptr(tr2_adr, lltype.Ptr(SR))
        tr3, tr3_adr = self.malloc(SR)
        tr4, tr4_adr = self.malloc(SR)
        t, t_adr = self.malloc(S)
        #
        tr1.sr2 = tr3; tr1.sr3 = tr1
        tr2.sr2 = tr3; tr2.sr3 = tr3
        tr3.sr2 = tr4; tr3.sr3 = tr2
        tr4.sr2 = tr3; tr4.sr3 = tr3; tr4.s1 = t
        #
        for i in range(4):
            self.malloc(S)     # forgotten
        #
        main_tls = self.gc.main_thread_tls
        old_value = main_tls.nursery_free
        #
        self.gc.collector.commit_transaction()
        #
        assert main_tls.nursery_free - old_value == (
            self.gcsize(SR) + self.gcsize(SR) + self.gcsize(S))
        #
        sr3_adr = self.gc.header(tr3_adr).version
        sr4_adr = self.gc.header(tr4_adr).version
        s_adr   = self.gc.header(t_adr  ).version
        assert len(set([sr3_adr, sr4_adr, s_adr])) == 3
        #
        sr3 = llmemory.cast_adr_to_ptr(sr3_adr, lltype.Ptr(SR))
        sr4 = llmemory.cast_adr_to_ptr(sr4_adr, lltype.Ptr(SR))
        s   = llmemory.cast_adr_to_ptr(s_adr,   lltype.Ptr(S))
        assert tr1.sr2 == sr3; assert tr1.sr3 == sr1     # roots: local obj
        assert tr2.sr2 == sr3; assert tr2.sr3 == sr3     #        is modified
        assert sr3.sr2 == sr4; assert sr3.sr3 == sr2     # non-roots: global
        assert sr4.sr2 == sr3; assert sr4.sr3 == sr3     #      obj is modified
        assert sr4.s1 == s
        #
        self.checkflags(sr1, 1, 1)
        self.checkflags(sr2, 1, 1)
        self.checkflags(sr3, 1, 0, llmemory.NULL)
        self.checkflags(sr4, 1, 0, llmemory.NULL)
        self.checkflags(s  , 1, 0, llmemory.NULL)

    def test_do_get_size(self):
        s1, s1_adr = self.malloc(S)
        assert (repr(self.gc.stm_operations._getsize_fn(s1_adr)) ==
                repr(fake_get_size(s1_adr)))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.