compile special minor GC versions of evacuate() and scavenge_block()
[ghc-hetmet.git] / rts / sm / Evac.c
1 /* -----------------------------------------------------------------------------
2  *
3  * (c) The GHC Team 1998-2006
4  *
5  * Generational garbage collector: evacuation functions
6  *
7  * Documentation on the architecture of the Garbage Collector can be
8  * found in the online commentary:
9  * 
10  *   http://hackage.haskell.org/trac/ghc/wiki/Commentary/Rts/Storage/GC
11  *
12  * ---------------------------------------------------------------------------*/
13
14 #include "Rts.h"
15 #include "Storage.h"
16 #include "MBlock.h"
17 #include "Evac.h"
18 #include "GC.h"
19 #include "GCUtils.h"
20 #include "Compact.h"
21 #include "Prelude.h"
22 #include "LdvProfile.h"
23
24 /* Used to avoid long recursion due to selector thunks
25  */
26 #define MAX_THUNK_SELECTOR_DEPTH 16
27
28 static void eval_thunk_selector (StgClosure **q, StgSelector * p, rtsBool);
29 STATIC_INLINE void evacuate_large(StgPtr p);
30
31 /* -----------------------------------------------------------------------------
32    Allocate some space in which to copy an object.
33    -------------------------------------------------------------------------- */
34
35 STATIC_INLINE StgPtr
36 alloc_for_copy (nat size, step *stp)
37 {
38     StgPtr to;
39     step_workspace *ws;
40     bdescr *bd;
41
42     /* Find out where we're going, using the handy "to" pointer in 
43      * the step of the source object.  If it turns out we need to
44      * evacuate to an older generation, adjust it here (see comment
45      * by evacuate()).
46      */
47     if (stp < gct->evac_step) {
48         if (gct->eager_promotion) {
49             stp = gct->evac_step;
50         } else {
51             gct->failed_to_evac = rtsTrue;
52         }
53     }
54     
55     ws = &gct->steps[stp->gen_no][stp->no];
56     
57     /* chain a new block onto the to-space for the destination step if
58      * necessary.
59      */
60     bd = ws->todo_bd;
61     to = bd->free;
62     if (to + size >= bd->start + BLOCK_SIZE_W) {
63         bd = gc_alloc_todo_block(ws);
64         to = bd->free;
65     }
66     bd->free = to + size;
67
68     return to;
69 }
70   
71 STATIC_INLINE void
72 copy_tag(StgClosure **p, StgClosure *src, nat size, step *stp,StgWord tag)
73 {
74     StgPtr to, tagged_to, from;
75     nat i;
76     StgWord info;
77
78 #ifdef THREADED_RTS
79     do {
80         info = xchg((StgPtr)&src->header.info, (W_)&stg_WHITEHOLE_info);
81         // so..  what is it?
82     } while (info == (W_)&stg_WHITEHOLE_info);
83     if (info == (W_)&stg_EVACUATED_info) {
84         src->header.info = (const StgInfoTable *)info;
85         return evacuate(p); // does the failed_to_evac stuff
86     }
87 #else
88     info = (W_)src->header.info;
89     src->header.info = &stg_EVACUATED_info;
90 #endif
91
92     to = alloc_for_copy(size,stp);
93     tagged_to = (StgPtr)TAG_CLOSURE(tag,(StgClosure*)to);
94     *p = (StgClosure *)tagged_to;
95     
96     TICK_GC_WORDS_COPIED(size);
97
98     from = (StgPtr)src;
99     to[0] = info;
100     for (i = 1; i < size; i++) { // unroll for small i
101         to[i] = from[i];
102     }
103     
104     ((StgEvacuated*)from)->evacuee = (StgClosure *)tagged_to;
105
106     // retag pointer before updating EVACUATE closure and returning
107
108 //  if (to+size+2 < bd->start + BLOCK_SIZE_W) {
109 //      __builtin_prefetch(to + size + 2, 1);
110 //  }
111
112 #ifdef THREADED_RTS
113     write_barrier();
114     ((StgEvacuated*)from)->header.info = &stg_EVACUATED_info;
115 #endif
116
117 #ifdef PROFILING
118     // We store the size of the just evacuated object in the LDV word so that
119     // the profiler can guess the position of the next object later.
120     SET_EVACUAEE_FOR_LDV(from, size);
121 #endif
122 }
123   
124 /* Special version of copy() for when we only want to copy the info
125  * pointer of an object, but reserve some padding after it.  This is
126  * used to optimise evacuation of BLACKHOLEs.
127  */
128 static void
129 copyPart(StgClosure **p, StgClosure *src, nat size_to_reserve, nat size_to_copy, step *stp)
130 {
131     StgPtr to, from;
132     nat i;
133     StgWord info;
134     
135 #ifdef THREADED_RTS
136     do {
137         info = xchg((StgPtr)&src->header.info, (W_)&stg_WHITEHOLE_info);
138     } while (info == (W_)&stg_WHITEHOLE_info);
139     if (info == (W_)&stg_EVACUATED_info) {
140         src->header.info = (const StgInfoTable *)info;
141         return evacuate(p); // does the failed_to_evac stuff
142     }
143 #else
144     info = (W_)src->header.info;
145     src->header.info = &stg_EVACUATED_info;
146 #endif
147
148     to = alloc_for_copy(size_to_reserve, stp);
149     *p = (StgClosure *)to;
150
151     TICK_GC_WORDS_COPIED(size_to_copy);
152
153     from = (StgPtr)src;
154     to[0] = info;
155     for (i = 1; i < size_to_copy; i++) { // unroll for small i
156         to[i] = from[i];
157     }
158     
159     ((StgEvacuated*)from)->evacuee = (StgClosure *)to;
160 #ifdef THREADED_RTS
161     write_barrier();
162     ((StgEvacuated*)from)->header.info = &stg_EVACUATED_info;
163 #endif
164     
165 #ifdef PROFILING
166     // We store the size of the just evacuated object in the LDV word so that
167     // the profiler can guess the position of the next object later.
168     SET_EVACUAEE_FOR_LDV(from, size_to_reserve);
169     // fill the slop
170     if (size_to_reserve - size_to_copy > 0)
171         LDV_FILL_SLOP(to + size_to_copy - 1, (int)(size_to_reserve - size_to_copy)); 
172 #endif
173 }
174
175
176 /* Copy wrappers that don't tag the closure after copying */
177 STATIC_INLINE void
178 copy(StgClosure **p, StgClosure *src, nat size, step *stp)
179 {
180     copy_tag(p,src,size,stp,0);
181 }
182
183 /* -----------------------------------------------------------------------------
184    The evacuate() code
185    -------------------------------------------------------------------------- */
186
187 #define MINOR_GC
188 #include "Evac.c-inc"
189
190 #undef MINOR_GC
191 #include "Evac.c-inc"
192
193 /* -----------------------------------------------------------------------------
194    Evacuate a large object
195
196    This just consists of removing the object from the (doubly-linked)
197    step->large_objects list, and linking it on to the (singly-linked)
198    step->new_large_objects list, from where it will be scavenged later.
199
200    Convention: bd->flags has BF_EVACUATED set for a large object
201    that has been evacuated, or unset otherwise.
202    -------------------------------------------------------------------------- */
203
204 STATIC_INLINE void
205 evacuate_large(StgPtr p)
206 {
207   bdescr *bd = Bdescr(p);
208   step *stp;
209   step_workspace *ws;
210
211   // object must be at the beginning of the block (or be a ByteArray)
212   ASSERT(get_itbl((StgClosure *)p)->type == ARR_WORDS ||
213          (((W_)p & BLOCK_MASK) == 0));
214
215   // already evacuated? 
216   if (bd->flags & BF_EVACUATED) { 
217     /* Don't forget to set the gct->failed_to_evac flag if we didn't get
218      * the desired destination (see comments in evacuate()).
219      */
220     if (bd->step < gct->evac_step) {
221       gct->failed_to_evac = rtsTrue;
222       TICK_GC_FAILED_PROMOTION();
223     }
224     return;
225   }
226
227   stp = bd->step;
228
229   ACQUIRE_SPIN_LOCK(&stp->sync_large_objects);
230   // remove from large_object list 
231   if (bd->u.back) {
232     bd->u.back->link = bd->link;
233   } else { // first object in the list 
234     stp->large_objects = bd->link;
235   }
236   if (bd->link) {
237     bd->link->u.back = bd->u.back;
238   }
239   RELEASE_SPIN_LOCK(&stp->sync_large_objects);
240   
241   /* link it on to the evacuated large object list of the destination step
242    */
243   stp = bd->step->to;
244   if (stp < gct->evac_step) {
245       if (gct->eager_promotion) {
246           stp = gct->evac_step;
247       } else {
248           gct->failed_to_evac = rtsTrue;
249       }
250   }
251
252   ws = &gct->steps[stp->gen_no][stp->no];
253   bd->step = stp;
254   bd->gen_no = stp->gen_no;
255   bd->link = ws->todo_large_objects;
256   ws->todo_large_objects = bd;
257   bd->flags |= BF_EVACUATED;
258 }
259
260 /* -----------------------------------------------------------------------------
261    Evaluate a THUNK_SELECTOR if possible.
262
263    p points to a THUNK_SELECTOR that we want to evaluate.  The
264    result of "evaluating" it will be evacuated and a pointer to the
265    to-space closure will be returned.
266
267    If the THUNK_SELECTOR could not be evaluated (its selectee is still
268    a THUNK, for example), then the THUNK_SELECTOR itself will be
269    evacuated.
270    -------------------------------------------------------------------------- */
271 static void
272 unchain_thunk_selectors(StgSelector *p, StgClosure *val)
273 {
274     StgSelector *prev;
275
276     prev = NULL;
277     while (p)
278     {
279         ASSERT(p->header.info == &stg_BLACKHOLE_info);
280         prev = (StgSelector*)((StgClosure *)p)->payload[0];
281
282         // Update the THUNK_SELECTOR with an indirection to the
283         // EVACUATED closure now at p.  Why do this rather than
284         // upd_evacuee(q,p)?  Because we have an invariant that an
285         // EVACUATED closure always points to an object in the
286         // same or an older generation (required by the short-cut
287         // test in the EVACUATED case, below).
288         SET_INFO(p, &stg_IND_info);
289         ((StgInd *)p)->indirectee = val;
290
291         // For the purposes of LDV profiling, we have created an
292         // indirection.
293         LDV_RECORD_CREATE(p);
294
295         p = prev;
296     }
297 }
298
299 static void
300 eval_thunk_selector (StgClosure **q, StgSelector * p, rtsBool evac)
301                  // NB. for legacy reasons, p & q are swapped around :(
302 {
303     nat field;
304     StgInfoTable *info;
305     StgWord info_ptr;
306     StgClosure *selectee;
307     StgSelector *prev_thunk_selector;
308     bdescr *bd;
309     StgClosure *val;
310     
311     prev_thunk_selector = NULL;
312     // this is a chain of THUNK_SELECTORs that we are going to update
313     // to point to the value of the current THUNK_SELECTOR.  Each
314     // closure on the chain is a BLACKHOLE, and points to the next in the
315     // chain with payload[0].
316
317 selector_chain:
318
319     bd = Bdescr((StgPtr)p);
320     if (HEAP_ALLOCED(p)) {
321         // If the THUNK_SELECTOR is in to-space or in a generation that we
322         // are not collecting, then bale out early.  We won't be able to
323         // save any space in any case, and updating with an indirection is
324         // trickier in a non-collected gen: we would have to update the
325         // mutable list.
326         if ((bd->gen_no > N) || (bd->flags & BF_EVACUATED)) {
327             unchain_thunk_selectors(prev_thunk_selector, (StgClosure *)p);
328             *q = (StgClosure *)p;
329             return;
330         }
331         // we don't update THUNK_SELECTORS in the compacted
332         // generation, because compaction does not remove the INDs
333         // that result, this causes confusion later
334         // (scavenge_mark_stack doesn't deal with IND).  BEWARE!  This
335         // bit is very tricky to get right.  If you make changes
336         // around here, test by compiling stage 3 with +RTS -c -RTS.
337         if (bd->flags & BF_COMPACTED) {
338             // must call evacuate() to mark this closure if evac==rtsTrue
339             *q = (StgClosure *)p;
340             if (evac) evacuate(q);
341             unchain_thunk_selectors(prev_thunk_selector, (StgClosure *)p);
342             return;
343         }
344     }
345
346
347     // BLACKHOLE the selector thunk, since it is now under evaluation.
348     // This is important to stop us going into an infinite loop if
349     // this selector thunk eventually refers to itself.
350 #if defined(THREADED_RTS)
351     // In threaded mode, we'll use WHITEHOLE to lock the selector
352     // thunk while we evaluate it.
353     {
354         info_ptr = (StgInfoTable *)xchg((StgPtr)&p->header.info, (W_)&stg_WHITEHOLE_info);
355         if (info_ptr == (W_)&stg_WHITEHOLE_info) {
356             do {
357                 info_ptr = xchg((StgPtr)&p->header.info, (W_)&stg_WHITEHOLE_info);
358             } while (info_ptr == (W_)&stg_WHITEHOLE_info);
359             goto bale_out;
360         }
361         // make sure someone else didn't get here first
362         if (INFO_PTR_TO_STRUCT(info_ptr)->type != THUNK_SELECTOR) {
363             goto bale_out;
364         }
365     }
366 #else
367     // Save the real info pointer (NOTE: not the same as get_itbl()).
368     info_ptr = (StgWord)p->header.info;
369     SET_INFO(p,&stg_BLACKHOLE_info);
370 #endif
371
372     field = INFO_PTR_TO_STRUCT(info_ptr)->layout.selector_offset;
373
374     // The selectee might be a constructor closure,
375     // so we untag the pointer.
376     selectee = UNTAG_CLOSURE(p->selectee);
377
378 selector_loop:
379     // selectee now points to the closure that we're trying to select
380     // a field from.  It may or may not be in to-space: we try not to
381     // end up in to-space, but it's impractical to avoid it in
382     // general.  The compacting GC scatters to-space pointers in
383     // from-space during marking, for example.  We rely on the property
384     // that evacuate() doesn't mind if it gets passed a to-space pointer.
385
386     info = get_itbl(selectee);
387     switch (info->type) {
388       case WHITEHOLE:
389           goto bale_out; // about to be evacuated by another thread (or a loop).
390         
391       case CONSTR:
392       case CONSTR_1_0:
393       case CONSTR_0_1:
394       case CONSTR_2_0:
395       case CONSTR_1_1:
396       case CONSTR_0_2:
397       case CONSTR_STATIC:
398       case CONSTR_NOCAF_STATIC:
399           {
400               // check that the size is in range 
401               ASSERT(field <  (StgWord32)(info->layout.payload.ptrs + 
402                                           info->layout.payload.nptrs));
403           
404               // Select the right field from the constructor
405               val = selectee->payload[field];
406               
407 #ifdef PROFILING
408               // For the purposes of LDV profiling, we have destroyed
409               // the original selector thunk, p.
410               SET_INFO(p, info_ptr);
411               LDV_RECORD_DEAD_FILL_SLOP_DYNAMIC((StgClosure *)p);
412               SET_INFO(p, &stg_BLACKHOLE_info);
413 #endif
414
415               // the closure in val is now the "value" of the
416               // THUNK_SELECTOR in p.  However, val may itself be a
417               // THUNK_SELECTOR, in which case we want to continue
418               // evaluating until we find the real value, and then
419               // update the whole chain to point to the value.
420           val_loop:
421               info = get_itbl(UNTAG_CLOSURE(val));
422               switch (info->type) {
423               case IND:
424               case IND_PERM:
425               case IND_OLDGEN:
426               case IND_OLDGEN_PERM:
427               case IND_STATIC:
428                   val = ((StgInd *)val)->indirectee;
429                   goto val_loop;
430               case THUNK_SELECTOR:
431                   ((StgClosure*)p)->payload[0] = (StgClosure *)prev_thunk_selector;
432                   prev_thunk_selector = p;
433                   p = (StgSelector*)val;
434                   goto selector_chain;
435               default:
436                   ((StgClosure*)p)->payload[0] = (StgClosure *)prev_thunk_selector;
437                   prev_thunk_selector = p;
438
439                   *q = val;
440                   if (evac) evacuate(q);
441                   val = *q;
442                   // evacuate() cannot recurse through
443                   // eval_thunk_selector(), because we know val is not
444                   // a THUNK_SELECTOR.
445                   unchain_thunk_selectors(prev_thunk_selector, val);
446                   return;
447               }
448           }
449
450       case IND:
451       case IND_PERM:
452       case IND_OLDGEN:
453       case IND_OLDGEN_PERM:
454       case IND_STATIC:
455           // Again, we might need to untag a constructor.
456           selectee = UNTAG_CLOSURE( ((StgInd *)selectee)->indirectee );
457           goto selector_loop;
458
459       case EVACUATED:
460           // We don't follow pointers into to-space; the constructor
461           // has already been evacuated, so we won't save any space
462           // leaks by evaluating this selector thunk anyhow.
463           goto bale_out;
464
465       case THUNK_SELECTOR:
466       {
467           StgClosure *val;
468
469           // recursively evaluate this selector.  We don't want to
470           // recurse indefinitely, so we impose a depth bound.
471           if (gct->thunk_selector_depth >= MAX_THUNK_SELECTOR_DEPTH) {
472               goto bale_out;
473           }
474
475           gct->thunk_selector_depth++;
476           // rtsFalse says "don't evacuate the result".  It will,
477           // however, update any THUNK_SELECTORs that are evaluated
478           // along the way.
479           eval_thunk_selector(&val, (StgSelector*)selectee, rtsFalse);
480           gct->thunk_selector_depth--;
481
482           // did we actually manage to evaluate it?
483           if (val == selectee) goto bale_out;
484
485           // Of course this pointer might be tagged...
486           selectee = UNTAG_CLOSURE(val);
487           goto selector_loop;
488       }
489
490       case AP:
491       case AP_STACK:
492       case THUNK:
493       case THUNK_1_0:
494       case THUNK_0_1:
495       case THUNK_2_0:
496       case THUNK_1_1:
497       case THUNK_0_2:
498       case THUNK_STATIC:
499       case CAF_BLACKHOLE:
500       case SE_CAF_BLACKHOLE:
501       case SE_BLACKHOLE:
502       case BLACKHOLE:
503           // not evaluated yet 
504           goto bale_out;
505     
506       default:
507         barf("eval_thunk_selector: strange selectee %d",
508              (int)(info->type));
509     }
510
511 bale_out:
512     // We didn't manage to evaluate this thunk; restore the old info
513     // pointer.  But don't forget: we still need to evacuate the thunk itself.
514     SET_INFO(p, (const StgInfoTable *)info_ptr);
515     if (evac) {
516         copy(&val,(StgClosure *)p,THUNK_SELECTOR_sizeW(),bd->step->to);
517     } else {
518         val = (StgClosure *)p;
519     }
520     *q = val;
521     unchain_thunk_selectors(prev_thunk_selector, val);
522     return;
523 }
524
525 /* -----------------------------------------------------------------------------
526    move_TSO is called to update the TSO structure after it has been
527    moved from one place to another.
528    -------------------------------------------------------------------------- */
529
530 void
531 move_TSO (StgTSO *src, StgTSO *dest)
532 {
533     ptrdiff_t diff;
534
535     // relocate the stack pointer... 
536     diff = (StgPtr)dest - (StgPtr)src; // In *words* 
537     dest->sp = (StgPtr)dest->sp + diff;
538 }
539