File: gc_update_phase.cpp

package info (click to toggle)
polyml 5.7.1-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 40,616 kB
  • sloc: cpp: 44,142; ansic: 26,963; sh: 22,002; asm: 13,486; makefile: 602; exp: 525; python: 253; awk: 91
file content (286 lines) | stat: -rw-r--r-- 9,930 bytes parent folder | download | duplicates (3)
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
/*
    Title:      Multi-Threaded Garbage Collector - Update phase

    Copyright (c) 2010-12 David C. J. Matthews

    Based on the original garbage collector code
        Copyright 2000-2008
        Cambridge University Technical Services Limited

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.
    
    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.
    
    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

*/
/*
This is the third, update, phase of the garbage collector.  The previous, copy,
phase will have moved cells in memory.  The update phase goes through all cells
that could contain an address of a cell that has been moved and looks for a
tomb-stone that contains its new location. 
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#elif defined(_WIN32)
#include "winconfig.h"
#else
#error "No configuration file"
#endif

#ifdef HAVE_ASSERT_H
#include <assert.h>
#define ASSERT(x)   assert(x)
#else
#define ASSERT(x)
#endif

#include "globals.h"
#include "run_time.h"
#include "processes.h"
#include "gc.h"
#include "scanaddrs.h"
#include "check_objects.h"
#include "bitmap.h"
#include "memmgr.h"
#include "gctaskfarm.h"
#include "diagnostics.h"

class MTGCProcessUpdate: public ScanAddress
{
public:
    virtual POLYUNSIGNED ScanAddressAt(PolyWord *pt);
    virtual void ScanRuntimeAddress(PolyObject **pt, RtsStrength weak);
    virtual PolyObject *ScanObjectAddress(PolyObject *base);

    void UpdateObjectsInArea(LocalMemSpace *area);

private:
    static void UpdateAddress(PolyObject *&obj)
    {
        while (obj->ContainsForwardingPtr())
            obj = obj->GetForwardingPtr();
    }
};

/*********************************************************************/
/* This function is called in the update phase to update pointers to */
/* objects in the gc area that are in old mutable segments.          */
/*********************************************************************/
PolyObject *MTGCProcessUpdate::ScanObjectAddress(PolyObject *obj)
{
    PolyWord val = obj;

    LocalMemSpace *space = gMem.LocalSpaceForAddress(val.AsStackAddr()-1);
    if (space != 0)
    {
        UpdateAddress(obj);
        ASSERT(obj->ContainsNormalLengthWord());
    }
    return obj;
}

void MTGCProcessUpdate::ScanRuntimeAddress(PolyObject **pt, RtsStrength/* weak*/)
/* weak is not used, but needed so type of the function is correct */
{
    PolyObject *obj = *pt;
    if (obj->ContainsForwardingPtr())
    {
        UpdateAddress(obj);
        *pt = obj;
    }
}  

// Update the addresses in a group of words.
POLYUNSIGNED MTGCProcessUpdate::ScanAddressAt(PolyWord *pt)
{
    PolyWord val = *pt;

    if (val.IsTagged())
        return 0;

    // It looked like it would be possible to simplify this code and
    // just call ContainsForwardingPtr on any address.
    // Profiling shows that it's quite important to avoid calling
    // ContainsForwardingPtr unnecessarily. I guess the reason is that
    // it actually accesses the memory referenced by the address and it
    // is unlikely to be in the cache.

    PolyObject *obj = val.AsObjPtr();
    if (obj->ContainsForwardingPtr())
    {
        UpdateAddress(obj);
        *pt = obj;
    }
    return 0;
}

// Updates the addresses for objects in the area with the "allocated" bit set.
// It processes the area between area->pointer and the bit corresponding to area->highest.
// area->highest corresponds to gen_top i.e. we don't process older generations.
void MTGCProcessUpdate::UpdateObjectsInArea(LocalMemSpace *area)
{
    PolyWord *pt      = area->upperAllocPtr;
    POLYUNSIGNED   bitno   = area->wordNo(pt);
    POLYUNSIGNED   highest = area->wordNo(area->top);

    for (;;)
    {
        ASSERT(bitno <= highest);
        /* Zero unused words.  This is necessary so that
           ScanAddressesInRegion can work.  It requires the allocated
           area of memory to contain either objects with a valid length
           word or forwarding pointer or zeros.  We should only be
           zeroing words that we couldn't fill with real data so it
           shouldn't be too much.  Profiling showed that using dummy
           byte objects here didn't make a measurable difference,
        */
        while (bitno < highest && !area->bitmap.TestBit(bitno))
        {
            *pt++ = PolyWord::FromUnsigned(0);
            bitno++;
        }
        
        if (bitno == highest) {
            // Have reached the top of the area
            ASSERT(pt == area->top);
            break;
        }
        
        /* first set bit corresponds to the length word */
        pt++;
        PolyObject *obj = (PolyObject*)pt;
        POLYUNSIGNED L = obj->LengthWord();
        bitno++;
        
        if (obj->ContainsForwardingPtr())
        {
            // Skip over moved objects.  We have to find the new location to find
            // its length.
            UpdateAddress(obj);            
            POLYUNSIGNED length = obj->Length();
            pt    += length;
            bitno += length;
        }
        else // Contains real object
        {
            
            if (OBJ_IS_WORD_OBJECT(L))
            {
                POLYUNSIGNED length = OBJ_OBJECT_LENGTH(L);
                
                area->updated += length+1;
                
                while (length--)
                {
                    PolyWord val = *pt;

                    if (! val.IsTagged() && val != PolyWord::FromUnsigned(0))
                    {
                        PolyObject *obj = val.AsObjPtr();
                    
                        if (obj->ContainsForwardingPtr())
                        {
                            UpdateAddress(obj);
                            *pt = obj;
                        }
                    }
                    
                    pt++;
                    bitno++;
                }
            }
            
            else /* !OBJ_IS_WORD_OBJECT(L) */
            {
                POLYUNSIGNED length = OBJ_OBJECT_LENGTH(L);
                area->updated += length+1;
                ScanAddressesInObject(obj, L);
                pt    += length;
                bitno += length;
            } /* !OBJ_IS_WORD_OBJECT(L) */

            CheckObject(obj); // Can check it after it's been updated
        }  /* !OBJ_IS_POINTER(L) */
    } /* for loop */
}

// Task to update addresses in a local area.
static void updateLocalArea(GCTaskId*, void *arg1, void *arg2)
{
    MTGCProcessUpdate *processUpdate = (MTGCProcessUpdate *)arg1;
    LocalMemSpace *space = (LocalMemSpace *)arg2;
    if (debugOptions & DEBUG_GC_ENHANCED)
        Log("GC: Update local area %p\n", space);
    // Process the current generation for mutable or immutable areas.
    processUpdate->UpdateObjectsInArea(space);
    if (debugOptions & DEBUG_GC_ENHANCED)
        Log("GC: Completed local update for %p. %lu words updated\n", space, space->updated);
}

// Task to update addresses in a non-local area.
static void updateNonLocalMutableArea(GCTaskId*, void *arg1, void *arg2)
{
    MTGCProcessUpdate *processUpdate = (MTGCProcessUpdate *)arg1;
    MemSpace *space = (MemSpace *)arg2;
    if (debugOptions & DEBUG_GC_ENHANCED)
        Log("GC: Update non-local mutable area %p\n", space);
    processUpdate->ScanAddressesInRegion(space->bottom, space->top);
    if (debugOptions & DEBUG_GC_ENHANCED)
        Log("GC: Completed non-local mutable update for %p\n", space);
}

// Task to update addresses maintained by the RTS itself.
static void updateGCProcAddresses(GCTaskId*, void *arg1, void *)
{
    MTGCProcessUpdate *processUpdate = (MTGCProcessUpdate *)arg1;
    GCModules(processUpdate);
}

void GCUpdatePhase()
{
    /* Update phase */
    mainThreadPhase = MTP_GCPHASEUPDATE;
    
    /* Invariant: at most the first (gen_top - bottom) bits of each bitmap can be dirty here. */
    for(std::vector<LocalMemSpace*>::iterator i = gMem.lSpaces.begin(); i < gMem.lSpaces.end(); i++)
        (*i)->updated = 0;

    // We can do the updates in parallel since they don't interfere at all.
    MTGCProcessUpdate processUpdate;

    // Process local areas.
    for (std::vector<LocalMemSpace*>::iterator i = gMem.lSpaces.begin(); i < gMem.lSpaces.end(); i++)
    {
        LocalMemSpace *space = *i;
        // As well as updating the addresses this also clears the bitmaps.
        gpTaskFarm->AddWorkOrRunNow(&updateLocalArea, &processUpdate, space);
    }
    // Scan the permanent mutable areas and the code areas.
    for (std::vector<PermanentMemSpace*>::iterator i = gMem.pSpaces.begin(); i < gMem.pSpaces.end(); i++)
    {
        PermanentMemSpace *space = *i;
        if (space->isMutable && ! space->byteOnly)
            gpTaskFarm->AddWorkOrRunNow(&updateNonLocalMutableArea, &processUpdate, space);
    }
    for (std::vector<CodeSpace *>::iterator i = gMem.cSpaces.begin(); i < gMem.cSpaces.end(); i++)
    {
        CodeSpace *space = *i;
        gpTaskFarm->AddWorkOrRunNow(&updateNonLocalMutableArea, &processUpdate, space);
        // We could remove the mutable bit if there are no longer any mutable code objects
        // but it's easier to leave that to the minor GC.
    }

    // Update addresses in RTS modules.
    gpTaskFarm->AddWorkOrRunNow(&updateGCProcAddresses, &processUpdate, 0);
    // Wait for these to complete before proceeding.
    gpTaskFarm->WaitForCompletion();
}