16bd297afcafa97acc797222e52eba4d68064171
[ghc-hetmet.git] / rts / sm / Evac.c-inc
1 /* -----------------------------------------------------------------------*-c-*-
2  *
3  * (c) The GHC Team 1998-2008
4  *
5  * Generational garbage collector: evacuation functions
6  *
7  * ---------------------------------------------------------------------------*/
8
9 // We have two versions of evacuate(): one for minor GC, and one for
10 // non-minor, parallel, GC.  This file contains the code for both,
11 // controllled by the CPP symbol MINOR_GC.
12
13 #ifndef PARALLEL_GC
14 #define copy(a,b,c,d) copy1(a,b,c,d)
15 #define copy_tag(a,b,c,d,e) copy_tag1(a,b,c,d,e)
16 #define copyPart(a,b,c,d,e) copyPart1(a,b,c,d,e)
17 #define evacuate(a) evacuate1(a)
18 #else
19 #undef copy
20 #undef copy_tag
21 #undef copyPart
22 #undef evacuate
23 #endif
24
25 STATIC_INLINE void
26 copy_tag(StgClosure **p, StgClosure *src, nat size, step *stp, StgWord tag)
27 {
28     StgPtr to, tagged_to, from;
29     nat i;
30     StgWord info;
31
32 #if defined(PARALLEL_GC) && defined(THREADED_RTS)
33 spin:
34         info = xchg((StgPtr)&src->header.info, (W_)&stg_WHITEHOLE_info);
35         // so..  what is it?
36     if (info == (W_)&stg_WHITEHOLE_info) {
37 #ifdef PROF_SPIN
38             whitehole_spin++;
39 #endif
40             goto spin;
41     }
42     if (info == (W_)&stg_EVACUATED_info || info == (W_)&stg_IND_info) {
43         // NB. a closure might be updated with an IND by
44         // unchain_selector_thunks(), hence the test above.
45         src->header.info = (const StgInfoTable *)info;
46         return evacuate(p); // does the failed_to_evac stuff
47     }
48 #else
49     ASSERT(n_gc_threads == 1);
50     info = (W_)src->header.info;
51     src->header.info = &stg_EVACUATED_info;
52 #endif
53
54     to = alloc_for_copy(size,stp);
55     tagged_to = (StgPtr)TAG_CLOSURE(tag,(StgClosure*)to);
56     *p = (StgClosure *)tagged_to;
57     
58     TICK_GC_WORDS_COPIED(size);
59
60     from = (StgPtr)src;
61     to[0] = info;
62     for (i = 1; i < size; i++) { // unroll for small i
63         to[i] = from[i];
64     }
65
66 //  if (to+size+2 < bd->start + BLOCK_SIZE_W) {
67 //      __builtin_prefetch(to + size + 2, 1);
68 //  }
69
70     ((StgEvacuated*)from)->evacuee = (StgClosure *)tagged_to;
71 #if defined(PARALLEL_GC) && defined(THREADED_RTS)
72     write_barrier();
73     ((StgEvacuated*)from)->header.info = &stg_EVACUATED_info;
74 #endif
75
76 #ifdef PROFILING
77     // We store the size of the just evacuated object in the LDV word so that
78     // the profiler can guess the position of the next object later.
79     SET_EVACUAEE_FOR_LDV(from, size);
80 #endif
81 }
82
83
84 /* Special version of copy() for when we only want to copy the info
85  * pointer of an object, but reserve some padding after it.  This is
86  * used to optimise evacuation of BLACKHOLEs.
87  */
88 static void
89 copyPart(StgClosure **p, StgClosure *src, nat size_to_reserve, nat size_to_copy, step *stp)
90 {
91     StgPtr to, from;
92     nat i;
93     StgWord info;
94     
95 #if defined(PARALLEL_GC) && defined(THREADED_RTS)
96 spin:
97         info = xchg((StgPtr)&src->header.info, (W_)&stg_WHITEHOLE_info);
98         if (info == (W_)&stg_WHITEHOLE_info) {
99 #ifdef PROF_SPIN
100             whitehole_spin++;
101 #endif
102             goto spin;
103         }
104     if (info == (W_)&stg_EVACUATED_info) {
105         src->header.info = (const StgInfoTable *)info;
106         evacuate(p); // does the failed_to_evac stuff
107         return ;
108     }
109 #else
110     info = (W_)src->header.info;
111     src->header.info = &stg_EVACUATED_info;
112 #endif
113
114     to = alloc_for_copy(size_to_reserve, stp);
115     *p = (StgClosure *)to;
116
117     TICK_GC_WORDS_COPIED(size_to_copy);
118
119     from = (StgPtr)src;
120     to[0] = info;
121     for (i = 1; i < size_to_copy; i++) { // unroll for small i
122         to[i] = from[i];
123     }
124     
125     ((StgEvacuated*)from)->evacuee = (StgClosure *)to;
126 #if defined(PARALLEL_GC) && defined(THREADED_RTS)
127     write_barrier();
128     ((StgEvacuated*)from)->header.info = &stg_EVACUATED_info;
129 #endif
130     
131 #ifdef PROFILING
132     // We store the size of the just evacuated object in the LDV word so that
133     // the profiler can guess the position of the next object later.
134     SET_EVACUAEE_FOR_LDV(from, size_to_reserve);
135     // fill the slop
136     if (size_to_reserve - size_to_copy > 0)
137         LDV_FILL_SLOP(to + size_to_copy - 1, (int)(size_to_reserve - size_to_copy)); 
138 #endif
139 }
140
141
142 /* Copy wrappers that don't tag the closure after copying */
143 STATIC_INLINE void
144 copy(StgClosure **p, StgClosure *src, nat size, step *stp)
145 {
146     copy_tag(p,src,size,stp,0);
147 }
148
149 /* ----------------------------------------------------------------------------
150    Evacuate
151
152    This is called (eventually) for every live object in the system.
153
154    The caller to evacuate specifies a desired generation in the
155    gct->evac_step thread-local variable.  The following conditions apply to
156    evacuating an object which resides in generation M when we're
157    collecting up to generation N
158
159    if  M >= gct->evac_step 
160            if  M > N     do nothing
161            else          evac to step->to
162
163    if  M < gct->evac_step      evac to gct->evac_step, step 0
164
165    if the object is already evacuated, then we check which generation
166    it now resides in.
167
168    if  M >= gct->evac_step     do nothing
169    if  M <  gct->evac_step     set gct->failed_to_evac flag to indicate that we
170                          didn't manage to evacuate this object into gct->evac_step.
171
172
173    OPTIMISATION NOTES:
174
175    evacuate() is the single most important function performance-wise
176    in the GC.  Various things have been tried to speed it up, but as
177    far as I can tell the code generated by gcc 3.2 with -O2 is about
178    as good as it's going to get.  We pass the argument to evacuate()
179    in a register using the 'regparm' attribute (see the prototype for
180    evacuate() near the top of this file).
181
182    Changing evacuate() to take an (StgClosure **) rather than
183    returning the new pointer seems attractive, because we can avoid
184    writing back the pointer when it hasn't changed (eg. for a static
185    object, or an object in a generation > N).  However, I tried it and
186    it doesn't help.  One reason is that the (StgClosure **) pointer
187    gets spilled to the stack inside evacuate(), resulting in far more
188    extra reads/writes than we save.
189    ------------------------------------------------------------------------- */
190
191 REGPARM1 void
192 evacuate(StgClosure **p)
193 {
194   bdescr *bd = NULL;
195   step *stp;
196   StgClosure *q;
197   const StgInfoTable *info;
198   StgWord tag;
199
200   q = *p;
201
202 loop:
203   /* The tag and the pointer are split, to be merged after evacing */
204   tag = GET_CLOSURE_TAG(q);
205   q = UNTAG_CLOSURE(q);
206
207   ASSERT(LOOKS_LIKE_CLOSURE_PTR(q));
208
209   if (!HEAP_ALLOCED(q)) {
210
211       if (!major_gc) return;
212
213       info = get_itbl(q);
214       switch (info->type) {
215
216       case THUNK_STATIC:
217           if (info->srt_bitmap != 0) {
218               if (*THUNK_STATIC_LINK((StgClosure *)q) == NULL) {
219 #ifndef THREADED_RTS
220                   *THUNK_STATIC_LINK((StgClosure *)q) = gct->static_objects;
221                   gct->static_objects = (StgClosure *)q;
222 #else
223                   StgPtr link;
224                   link = (StgPtr)cas((StgPtr)THUNK_STATIC_LINK((StgClosure *)q),
225                                      (StgWord)NULL,
226                                      (StgWord)gct->static_objects);
227                   if (link == NULL) {
228                       gct->static_objects = (StgClosure *)q;
229                   }
230 #endif
231               }
232           }
233           return;
234
235       case FUN_STATIC:
236           if (info->srt_bitmap != 0 &&
237               *FUN_STATIC_LINK((StgClosure *)q) == NULL) {
238 #ifndef THREADED_RTS
239               *FUN_STATIC_LINK((StgClosure *)q) = gct->static_objects;
240               gct->static_objects = (StgClosure *)q;
241 #else
242               StgPtr link;
243               link = (StgPtr)cas((StgPtr)FUN_STATIC_LINK((StgClosure *)q),
244                                  (StgWord)NULL,
245                                  (StgWord)gct->static_objects);
246               if (link == NULL) {
247                   gct->static_objects = (StgClosure *)q;
248               }
249 #endif
250           }
251           return;
252           
253       case IND_STATIC:
254           /* If q->saved_info != NULL, then it's a revertible CAF - it'll be
255            * on the CAF list, so don't do anything with it here (we'll
256            * scavenge it later).
257            */
258           if (((StgIndStatic *)q)->saved_info == NULL) {
259               if (*IND_STATIC_LINK((StgClosure *)q) == NULL) {
260 #ifndef THREADED_RTS
261                   *IND_STATIC_LINK((StgClosure *)q) = gct->static_objects;
262                   gct->static_objects = (StgClosure *)q;
263 #else
264                   StgPtr link;
265                   link = (StgPtr)cas((StgPtr)IND_STATIC_LINK((StgClosure *)q),
266                                      (StgWord)NULL,
267                                      (StgWord)gct->static_objects);
268                   if (link == NULL) {
269                       gct->static_objects = (StgClosure *)q;
270                   }
271 #endif
272               }
273           }
274           return;
275           
276       case CONSTR_STATIC:
277           if (*STATIC_LINK(info,(StgClosure *)q) == NULL) {
278 #ifndef THREADED_RTS
279               *STATIC_LINK(info,(StgClosure *)q) = gct->static_objects;
280               gct->static_objects = (StgClosure *)q;
281 #else
282               StgPtr link;
283               link = (StgPtr)cas((StgPtr)STATIC_LINK(info,(StgClosure *)q),
284                                  (StgWord)NULL,
285                                  (StgWord)gct->static_objects);
286               if (link == NULL) {
287                   gct->static_objects = (StgClosure *)q;
288               }
289 #endif
290           }
291           /* I am assuming that static_objects pointers are not
292            * written to other objects, and thus, no need to retag. */
293           return;
294           
295       case CONSTR_NOCAF_STATIC:
296           /* no need to put these on the static linked list, they don't need
297            * to be scavenged.
298            */
299           return;
300           
301       default:
302           barf("evacuate(static): strange closure type %d", (int)(info->type));
303       }
304   }
305
306   bd = Bdescr((P_)q);
307
308   if ((bd->flags & (BF_LARGE | BF_COMPACTED | BF_EVACUATED)) != 0) {
309
310       // pointer into to-space: just return it.  It might be a pointer
311       // into a generation that we aren't collecting (> N), or it
312       // might just be a pointer into to-space.  The latter doesn't
313       // happen often, but allowing it makes certain things a bit
314       // easier; e.g. scavenging an object is idempotent, so it's OK to
315       // have an object on the mutable list multiple times.
316       if (bd->flags & BF_EVACUATED) {
317           // We aren't copying this object, so we have to check
318           // whether it is already in the target generation.  (this is
319           // the write barrier).
320           if (bd->step < gct->evac_step) {
321               gct->failed_to_evac = rtsTrue;
322               TICK_GC_FAILED_PROMOTION();
323           }
324           return;
325       }
326
327       /* evacuate large objects by re-linking them onto a different list.
328        */
329       if (bd->flags & BF_LARGE) {
330           info = get_itbl(q);
331           if (info->type == TSO && 
332               ((StgTSO *)q)->what_next == ThreadRelocated) {
333               q = (StgClosure *)((StgTSO *)q)->link;
334               *p = q;
335               goto loop;
336           }
337           evacuate_large((P_)q);
338           return;
339       }
340       
341       /* If the object is in a step that we're compacting, then we
342        * need to use an alternative evacuate procedure.
343        */
344       if (bd->flags & BF_COMPACTED) {
345           if (!is_marked((P_)q,bd)) {
346               mark((P_)q,bd);
347               if (mark_stack_full()) {
348                   mark_stack_overflowed = rtsTrue;
349                   reset_mark_stack();
350               }
351               push_mark_stack((P_)q);
352           }
353           return;
354       }
355   }
356       
357   stp = bd->step->to;
358
359   info = get_itbl(q);
360   
361   switch (info->type) {
362
363   case WHITEHOLE:
364       goto loop;
365
366   case MUT_VAR_CLEAN:
367   case MUT_VAR_DIRTY:
368   case MVAR_CLEAN:
369   case MVAR_DIRTY:
370       copy(p,q,sizeW_fromITBL(info),stp);
371       return;
372
373   case CONSTR_0_1:
374   { 
375       StgWord w = (StgWord)q->payload[0];
376       if (q->header.info == Czh_con_info &&
377           // unsigned, so always true:  (StgChar)w >= MIN_CHARLIKE &&  
378           (StgChar)w <= MAX_CHARLIKE) {
379           *p =  TAG_CLOSURE(tag,
380                             (StgClosure *)CHARLIKE_CLOSURE((StgChar)w)
381                            );
382       }
383       else if (q->header.info == Izh_con_info &&
384           (StgInt)w >= MIN_INTLIKE && (StgInt)w <= MAX_INTLIKE) {
385           *p = TAG_CLOSURE(tag,
386                              (StgClosure *)INTLIKE_CLOSURE((StgInt)w)
387                              );
388       }
389       else {
390           copy_tag(p,q,sizeofW(StgHeader)+1,stp,tag);
391       }
392       return;
393   }
394
395   case FUN_0_1:
396   case FUN_1_0:
397   case CONSTR_1_0:
398       copy_tag(p,q,sizeofW(StgHeader)+1,stp,tag);
399       return;
400
401   case THUNK_1_0:
402   case THUNK_0_1:
403       copy(p,q,sizeofW(StgThunk)+1,stp);
404       return;
405
406   case THUNK_1_1:
407   case THUNK_2_0:
408   case THUNK_0_2:
409 #ifdef NO_PROMOTE_THUNKS
410     if (bd->gen_no == 0 && 
411         bd->step->no != 0 &&
412         bd->step->no == generations[bd->gen_no].n_steps-1) {
413       stp = bd->step;
414     }
415 #endif
416     copy(p,q,sizeofW(StgThunk)+2,stp);
417     return;
418
419   case FUN_1_1:
420   case FUN_2_0:
421   case FUN_0_2:
422   case CONSTR_1_1:
423   case CONSTR_2_0:
424       copy_tag(p,q,sizeofW(StgHeader)+2,stp,tag);
425       return;
426
427   case CONSTR_0_2:
428       copy_tag(p,q,sizeofW(StgHeader)+2,stp,tag);
429       return;
430
431   case THUNK:
432       copy(p,q,thunk_sizeW_fromITBL(info),stp);
433       return;
434
435   case FUN:
436   case IND_PERM:
437   case IND_OLDGEN_PERM:
438   case WEAK:
439   case STABLE_NAME:
440   case CONSTR:
441       copy_tag(p,q,sizeW_fromITBL(info),stp,tag);
442       return;
443
444   case BCO:
445       copy(p,q,bco_sizeW((StgBCO *)q),stp);
446       return;
447
448   case CAF_BLACKHOLE:
449   case SE_CAF_BLACKHOLE:
450   case SE_BLACKHOLE:
451   case BLACKHOLE:
452       copyPart(p,q,BLACKHOLE_sizeW(),sizeofW(StgHeader),stp);
453       return;
454
455   case THUNK_SELECTOR:
456       eval_thunk_selector(p, (StgSelector *)q, rtsTrue);
457       return;
458
459   case IND:
460   case IND_OLDGEN:
461     // follow chains of indirections, don't evacuate them 
462     q = ((StgInd*)q)->indirectee;
463     *p = q;
464     goto loop;
465
466   case RET_BCO:
467   case RET_SMALL:
468   case RET_BIG:
469   case RET_DYN:
470   case UPDATE_FRAME:
471   case STOP_FRAME:
472   case CATCH_FRAME:
473   case CATCH_STM_FRAME:
474   case CATCH_RETRY_FRAME:
475   case ATOMICALLY_FRAME:
476     // shouldn't see these 
477     barf("evacuate: stack frame at %p\n", q);
478
479   case PAP:
480       copy(p,q,pap_sizeW((StgPAP*)q),stp);
481       return;
482
483   case AP:
484       copy(p,q,ap_sizeW((StgAP*)q),stp);
485       return;
486
487   case AP_STACK:
488       copy(p,q,ap_stack_sizeW((StgAP_STACK*)q),stp);
489       return;
490
491   case EVACUATED:
492     /* Already evacuated, just return the forwarding address.
493      * HOWEVER: if the requested destination generation (gct->evac_step) is
494      * older than the actual generation (because the object was
495      * already evacuated to a younger generation) then we have to
496      * set the gct->failed_to_evac flag to indicate that we couldn't 
497      * manage to promote the object to the desired generation.
498      */
499     /* 
500      * Optimisation: the check is fairly expensive, but we can often
501      * shortcut it if either the required generation is 0, or the
502      * current object (the EVACUATED) is in a high enough generation.
503      * We know that an EVACUATED always points to an object in the
504      * same or an older generation.  stp is the lowest step that the
505      * current object would be evacuated to, so we only do the full
506      * check if stp is too low.
507      */
508   {
509       StgClosure *e = ((StgEvacuated*)q)->evacuee;
510       *p = e;
511       if (stp < gct->evac_step) {  // optimisation 
512           if (Bdescr((P_)e)->step < gct->evac_step) {
513               gct->failed_to_evac = rtsTrue;
514               TICK_GC_FAILED_PROMOTION();
515           }
516       }
517       return;
518   }
519
520   case ARR_WORDS:
521       // just copy the block 
522       copy(p,q,arr_words_sizeW((StgArrWords *)q),stp);
523       return;
524
525   case MUT_ARR_PTRS_CLEAN:
526   case MUT_ARR_PTRS_DIRTY:
527   case MUT_ARR_PTRS_FROZEN:
528   case MUT_ARR_PTRS_FROZEN0:
529       // just copy the block 
530       copy(p,q,mut_arr_ptrs_sizeW((StgMutArrPtrs *)q),stp);
531       return;
532
533   case TSO:
534     {
535       StgTSO *tso = (StgTSO *)q;
536
537       /* Deal with redirected TSOs (a TSO that's had its stack enlarged).
538        */
539       if (tso->what_next == ThreadRelocated) {
540         q = (StgClosure *)tso->link;
541         *p = q;
542         goto loop;
543       }
544
545       /* To evacuate a small TSO, we need to relocate the update frame
546        * list it contains.  
547        */
548       {
549           StgTSO *new_tso;
550           StgPtr r, s;
551
552           copyPart(p,(StgClosure *)tso, tso_sizeW(tso), sizeofW(StgTSO), stp);
553           new_tso = (StgTSO *)*p;
554           move_TSO(tso, new_tso);
555           for (r = tso->sp, s = new_tso->sp;
556                r < tso->stack+tso->stack_size;) {
557               *s++ = *r++;
558           }
559           return;
560       }
561     }
562
563   case TREC_HEADER: 
564       copy(p,q,sizeofW(StgTRecHeader),stp);
565       return;
566
567   case TVAR_WATCH_QUEUE:
568       copy(p,q,sizeofW(StgTVarWatchQueue),stp);
569       return;
570
571   case TVAR:
572       copy(p,q,sizeofW(StgTVar),stp);
573       return;
574     
575   case TREC_CHUNK:
576       copy(p,q,sizeofW(StgTRecChunk),stp);
577       return;
578
579   case ATOMIC_INVARIANT:
580       copy(p,q,sizeofW(StgAtomicInvariant),stp);
581       return;
582
583   case INVARIANT_CHECK_QUEUE:
584       copy(p,q,sizeofW(StgInvariantCheckQueue),stp);
585       return;
586
587   default:
588     barf("evacuate: strange closure type %d", (int)(info->type));
589   }
590
591   barf("evacuate");
592 }
593
594 #undef copy
595 #undef copy_tag
596 #undef copyPart
597 #undef evacuate