summaryrefslogtreecommitdiff
path: root/ghc/runtime/storage/SMcopying.lc
blob: 77c41243559aaa4d33a4d863edfbe8f97f4f67e7 (plain)
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
\section[SM-copying]{Copying Collector Subroutines}

This is a collection of C functions used in implementing the copying
collectors.

The motivation for making this a separate file/section is twofold:

1) It lets us focus on one thing.

2) If we don't do this, there will be a huge amount of repetition
   between the various GC schemes --- a maintenance nightmare.

The second is the major motivation.  


\begin{code} 
#if defined(GC2s) || defined(GCdu) || defined(GCap) || defined(GCgn)
    /* to the end */

#define SCAV_REG_MAP
#include "SMinternal.h"
REGDUMP(ScavRegDump);

#include "SMcopying.h"
\end{code}

Comment stolen from SMscav.lc: When doing a new generation copy
collection for Appel's collector only evacuate references that point
to the new generation.  OldGen must be set to point to the end of old
space.

\begin{code}
#ifdef GCap

#define MAYBE_EVACUATE_CLOSURE( closure )   \
do {                                        \
  P_ evac = (P_) (closure);                 \
  if (evac > OldGen) {                      \
    (closure) = EVACUATE_CLOSURE(evac);     \
  }                                         \
} while (0)

#else

#define MAYBE_EVACUATE_CLOSURE( closure )   \
do {                                        \
  P_ evac = (P_) (closure);                 \
  (closure) = EVACUATE_CLOSURE(evac);       \
} while (0)

#endif
\end{code}

\begin{code}
void
SetCAFInfoTables(P_ CAFlist)
{
  P_ CAFptr;

  /* Set CAF info tables for evacuation */
  DEBUG_STRING("Setting Evac & Upd CAFs:");
  for (CAFptr = CAFlist; 
       CAFptr != NULL;
       CAFptr = (P_) IND_CLOSURE_LINK(CAFptr) ) {
    INFO_PTR(CAFptr) = (W_) Caf_Evac_Upd_info;
  }
}
\end{code}

\begin{code}
void
EvacuateRoots(P_ roots[], I_ rootno)
{
  I_ root;

  DEBUG_STRING("Evacuate (Reg) Roots:");
  for (root = 0; root < rootno; root++) {
    MAYBE_EVACUATE_CLOSURE( roots[root] );
  }
}
\end{code}

Evacuating events is necessary in GRAN since some TSOs and closures are only
pointed at by events we have to schedule later on.

\begin{code}
#if defined(GRAN)
void
EvacuateEvents(STG_NO_ARGS)
{
  eventq event = EventHd;

# if defined(GRAN) && defined(GRAN_CHECK)
  if ( RTSflags.GcFlags.giveStats && (RTSflags.GranFlags.debug & 0x40) )
    fprintf(RTSflags.GcFlags.statsFile,"Evacuating Events ...\n");
#endif

  DEBUG_STRING("Evacuate Events:");
  while(event != NULL)
    {
      if(EVENT_TYPE(event) == RESUMETHREAD || 
         EVENT_TYPE(event) == MOVETHREAD || 
         EVENT_TYPE(event) == CONTINUETHREAD || 
         EVENT_TYPE(event) == STARTTHREAD )

       MAYBE_EVACUATE_CLOSURE( EVENT_TSO(event) );

      else if(EVENT_TYPE(event) == MOVESPARK)

        MAYBE_EVACUATE_CLOSURE( SPARK_NODE(EVENT_SPARK(event)) );

      else if (EVENT_TYPE(event) == FETCHNODE ||
               EVENT_TYPE(event) == FETCHREPLY )
        {

          MAYBE_EVACUATE_CLOSURE( EVENT_TSO(event) );

	  /* In the case of packet fetching, EVENT_NODE(event) points to */
	  /* the packet (currently, malloced). The packet is just a list of */
	  /* closure addresses, with the length of the list at index 1 (the */
	  /* structure of the packet is defined in Pack.lc). */
	  if ( RTSflags.GranFlags.DoGUMMFetching && 
	       (EVENT_TYPE(event)==FETCHREPLY)) {
	    P_ buffer = (P_) EVENT_NODE(event);
	    int size = (int) buffer[PACK_SIZE_LOCN], i;

	    for (i = PACK_HDR_SIZE; i <= size-1; i++) {
              MAYBE_EVACUATE_CLOSURE( (P_)buffer[i] );
	    }
	  } else 
            MAYBE_EVACUATE_CLOSURE( EVENT_NODE(event) );
        } 
      else if (EVENT_TYPE(event) == GLOBALBLOCK)
	{
          MAYBE_EVACUATE_CLOSURE( EVENT_TSO(event) );
          MAYBE_EVACUATE_CLOSURE( EVENT_NODE(event) );
	}
      else if (EVENT_TYPE(event) == UNBLOCKTHREAD) 
	{
          MAYBE_EVACUATE_CLOSURE( EVENT_TSO(event) );
	}
      event = EVENT_NEXT(event);
    }
}
#endif  /* GRAN */
\end{code}

\begin{code}
#if defined(CONCURRENT) 
# if defined(GRAN)
void
EvacuateSparks(STG_NO_ARGS)
{
  sparkq spark;
  PROC proc;
  I_ pool, total_sparks=0;

  /* Sparks have been pruned already at this point */

# if defined(GRAN) && defined(GRAN_CHECK)
  if ( RTSflags.GcFlags.giveStats && (RTSflags.GranFlags.debug & 0x40) )
    fprintf(RTSflags.GcFlags.statsFile,"Evacuating Sparks ...\n");
# endif

  DEBUG_STRING("Evacuate Sparks (GRAN):");
  for(proc = 0; proc < RTSflags.GranFlags.proc; ++proc) {
    for(pool = 0; pool < SPARK_POOLS; ++pool) {
      for(spark = PendingSparksHd[proc][pool]; 
	  spark != NULL; 
	  spark = SPARK_NEXT(spark))
        {
# if defined(GRAN) && defined(GRAN_CHECK)
          if ( RTSflags.GcFlags.giveStats && 
	       (RTSflags.GranFlags.debug & 0x40) &&
	       !SHOULD_SPARK(SPARK_NODE(spark)) )
             fprintf(RTSflags.GcFlags.statsFile,"Qagh {EvacuateSparks}Daq: spark @ 0x%x with node 0x%x shouldn't spark!\n",
                     spark,SPARK_NODE(spark));
# endif
          MAYBE_EVACUATE_CLOSURE(SPARK_NODE(spark));
        }  /* forall spark ... */
    }     /* forall pool ... */
  }      /* forall proc ... */
}

# else  /* !GRAN */

void
EvacuateSparks(STG_NO_ARGS)
{
    PP_ sparkptr;
    int pool;


    DEBUG_STRING("Evacuate Sparks:");
    for (pool = 0; pool < SPARK_POOLS; pool++) {
	for (sparkptr = PendingSparksHd[pool];
	  sparkptr < PendingSparksTl[pool]; sparkptr++) {
	    MAYBE_EVACUATE_CLOSURE(*((PP_) sparkptr));
	}
    }
}
# endif
#endif  /* CONCURRENT */
\end{code}

Note: no \tr{evacuate[AB]Stack} for ``parallel'' systems, because they
don't have a single main stack.

\begin{code}
#if !defined(PAR)
void
EvacuateAStack(PP_ stackA, PP_ botA /* botA points to bottom-most word */)
{
  PP_ stackptr;
  
  DEBUG_STRING("Evacuate A Stack:");
  for (stackptr = stackA;
       SUBTRACT_A_STK(stackptr, botA) >= 0;
       stackptr = stackptr + AREL(1)) {
    MAYBE_EVACUATE_CLOSURE( *((PP_) stackptr) );
  }
}
#endif /* not PAR */
\end{code}

ToDo: Optimisation which squeezes out update frames which point to
garbage closures.

Perform collection first

Then process B stack removing update frames (bot to top via pointer
reversal) that reference garbage closues (test infoptr !=
EVACUATED_INFOPTR)

Otherwise closure is live update reference to to-space address

\begin{code}
#if !defined(PAR)
void
EvacuateBStack( stackB, botB, roots )
  P_ stackB;
  P_ botB;  /* botB points to bottom-most word */
  I_ *roots;
{
  I_ bstk_roots;
  P_ updateFramePtr;
  P_ updatee;

  DEBUG_STRING("Evacuate B Stack:");
  bstk_roots = 0;
  for (updateFramePtr = stackB;  /* stackB points to topmost update frame */
       SUBTRACT_B_STK(updateFramePtr, botB) > 0;
       updateFramePtr = GRAB_SuB(updateFramePtr)) {
    
    /* Evacuate the thing to be updated */
    updatee = GRAB_UPDATEE(updateFramePtr);
    MAYBE_EVACUATE_CLOSURE(updatee);
    PUSH_UPDATEE(updateFramePtr, updatee);
    bstk_roots++;
  }
  *roots = bstk_roots;
}
#endif /* not PAR */
\end{code}

When we do a copying collection, we want to evacuate all of the local
entries in the GALA table for which there are outstanding remote
pointers (i.e. for which the weight is not MAX_GA_WEIGHT.)

\begin{code}
#ifdef PAR

void
EvacuateLocalGAs(rtsBool full)
{
    GALA *gala;
    GALA *next;
    GALA *prev = NULL;

    for (gala = liveIndirections; gala != NULL; gala = next) {
	next = gala->next;
	ASSERT(gala->ga.loc.gc.gtid == mytid);
        if (gala->ga.weight != MAX_GA_WEIGHT) {
	    /* Remote references exist, so we must evacuate the local closure */
	    P_ old = gala->la;
	    MAYBE_EVACUATE_CLOSURE(gala->la);
	    if (!full && gala->preferred && gala->la != old) {
		(void) removeHashTable(LAtoGALAtable, (W_) old, (void *) gala);
		insertHashTable(LAtoGALAtable, (W_) gala->la, (void *) gala);
	    }
	    gala->next = prev;
	    prev = gala;
	} else {
	    /* Since we have all of the weight, this GA is no longer needed */
	    W_ pga = PackGA(thisPE, gala->ga.loc.gc.slot);

#ifdef FREE_DEBUG
	    fprintf(stderr, "Freeing slot %d\n", gala->ga.loc.gc.slot);
#endif
	    gala->next = freeIndirections;
	    freeIndirections = gala;
	    (void) removeHashTable(pGAtoGALAtable, pga, (void *) gala);
	    if (!full && gala->preferred)
		(void) removeHashTable(LAtoGALAtable, (W_) gala->la, (void *) gala);
#ifdef DEBUG
	    gala->ga.weight = 0x0d0d0d0d;
	    gala->la = (P_) 0xbadbad;
#endif
	}
    }
    liveIndirections = prev;
}

\end{code}

\begin{code}

EXTDATA_RO(Forward_Ref_info);

void
RebuildGAtables(rtsBool full)
{
    GALA *gala;
    GALA *next;
    GALA *prev;
    P_ closure;

    prepareFreeMsgBuffers();

    for (gala = liveRemoteGAs, prev = NULL; gala != NULL; gala = next) {
	next = gala->next;
	ASSERT(gala->ga.loc.gc.gtid != mytid);

	closure = gala->la;

	/*
	 * If the old closure has not been forwarded, we let go.  Note that this
	 * approach also drops global aliases for PLCs.
	 */

#if defined(GCgn) || defined(GCap)
	if (closure > OldGen) {
#endif
	    if (!full && gala->preferred)
		(void) removeHashTable(LAtoGALAtable, (W_) gala->la, (void *) gala);

	    /* Follow indirection chains to the end, just in case */
	    while (IS_INDIRECTION(INFO_PTR(closure)))
		closure = (P_) IND_CLOSURE_PTR(closure);

	    /* Change later to incorporate a _FO bit in the INFO_TYPE for GCgn */
#ifdef GCgn
    fall over, until _FO bits are added
#endif
	    if (INFO_PTR(closure) != (W_) Forward_Ref_info) {
		int pe = taskIDtoPE(gala->ga.loc.gc.gtid);
		W_ pga = PackGA(pe, gala->ga.loc.gc.slot);

		(void) removeHashTable(pGAtoGALAtable, pga, (void *) gala);
		freeRemoteGA(pe, &(gala->ga));
		gala->next = freeGALAList;
		freeGALAList = gala;
	    } else {
		/* Find the new space object */
		closure = (P_) FORWARD_ADDRESS(closure);
		gala->la = closure;

		if (!full && gala->preferred)
		    insertHashTable(LAtoGALAtable, (W_) gala->la, (void *) gala);
		gala->next = prev;
		prev = gala;
	    }
#if defined(GCgn) || defined(GCap)
	} else {
	    /* Old generation, minor collection; just keep it */
	    gala->next = prev;
	    prev = gala;
	}
#endif
    }
    liveRemoteGAs = prev;

    /* If we have any remaining FREE messages to send off, do so now */
    sendFreeMessages();

    if (full)
	RebuildLAGAtable();
}

#endif

\end{code}

\begin{code}
void
Scavenge(void)
{
  DEBUG_SCAN("Scavenging Start", Scav, "ToHp", ToHp);
  while (Scav <= ToHp) (SCAV_CODE(INFO_PTR(Scav)))();
  DEBUG_SCAN("Scavenging End", Scav, "ToHp", ToHp);
}
\end{code}

\begin{code}
#ifdef GCdu

void
EvacuateCAFs( CAFlist )
  P_ CAFlist;
{
  P_ CAFptr;

  DEBUG_STRING("Evacuate CAFs:");
  for (CAFptr = CAFlist; 
       CAFptr != NULL;
       CAFptr = (P_) IND_CLOSURE_LINK(CAFptr)) {
    EVACUATE_CLOSURE(CAFptr); /* evac & upd OR return */
  }
}

/* ToDo: put GCap EvacuateCAFs code here */

#else /* not GCdu */

void
EvacAndScavengeCAFs( CAFlist, extra_words, roots )
  P_ CAFlist;
  I_ *extra_words;
  I_ *roots;
{
  I_ caf_roots = 0;
  P_ caf_start = ToHp;
  P_ CAFptr;

  DEBUG_STRING("Evacuate & Scavenge CAFs:");
  for (CAFptr = CAFlist; 
       CAFptr != NULL;
       CAFptr = (P_) IND_CLOSURE_LINK(CAFptr)) {

      EVACUATE_CLOSURE(CAFptr); /* evac & upd OR return */
      caf_roots++;

      DEBUG_SCAN("Scavenging CAF", Scav, "ToHp", ToHp);
      while (Scav <= ToHp) (SCAV_CODE(INFO_PTR(Scav)))();
      DEBUG_SCAN("Scavenging End", Scav, "ToHp", ToHp);
  }
  *extra_words = ToHp - caf_start;
  *roots = caf_roots;
}

#endif /* !GCdu */

#endif /* defined(_INFO_COPYING) */
\end{code}