fix a warning
[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 /* -----------------------------------------------------------------------------
72    The evacuate() code
73    -------------------------------------------------------------------------- */
74
75 #define MINOR_GC
76 #include "Evac.c-inc"
77
78 #undef MINOR_GC
79 #include "Evac.c-inc"
80
81 /* -----------------------------------------------------------------------------
82    Evacuate a large object
83
84    This just consists of removing the object from the (doubly-linked)
85    step->large_objects list, and linking it on to the (singly-linked)
86    step->new_large_objects list, from where it will be scavenged later.
87
88    Convention: bd->flags has BF_EVACUATED set for a large object
89    that has been evacuated, or unset otherwise.
90    -------------------------------------------------------------------------- */
91
92 STATIC_INLINE void
93 evacuate_large(StgPtr p)
94 {
95   bdescr *bd = Bdescr(p);
96   step *stp;
97   step_workspace *ws;
98
99   // object must be at the beginning of the block (or be a ByteArray)
100   ASSERT(get_itbl((StgClosure *)p)->type == ARR_WORDS ||
101          (((W_)p & BLOCK_MASK) == 0));
102
103   // already evacuated? 
104   if (bd->flags & BF_EVACUATED) { 
105     /* Don't forget to set the gct->failed_to_evac flag if we didn't get
106      * the desired destination (see comments in evacuate()).
107      */
108     if (bd->step < gct->evac_step) {
109       gct->failed_to_evac = rtsTrue;
110       TICK_GC_FAILED_PROMOTION();
111     }
112     return;
113   }
114
115   stp = bd->step;
116
117   ACQUIRE_SPIN_LOCK(&stp->sync_large_objects);
118   // remove from large_object list 
119   if (bd->u.back) {
120     bd->u.back->link = bd->link;
121   } else { // first object in the list 
122     stp->large_objects = bd->link;
123   }
124   if (bd->link) {
125     bd->link->u.back = bd->u.back;
126   }
127   RELEASE_SPIN_LOCK(&stp->sync_large_objects);
128   
129   /* link it on to the evacuated large object list of the destination step
130    */
131   stp = bd->step->to;
132   if (stp < gct->evac_step) {
133       if (gct->eager_promotion) {
134           stp = gct->evac_step;
135       } else {
136           gct->failed_to_evac = rtsTrue;
137       }
138   }
139
140   ws = &gct->steps[stp->gen_no][stp->no];
141   bd->step = stp;
142   bd->gen_no = stp->gen_no;
143   bd->link = ws->todo_large_objects;
144   ws->todo_large_objects = bd;
145   bd->flags |= BF_EVACUATED;
146 }
147
148 /* -----------------------------------------------------------------------------
149    Evaluate a THUNK_SELECTOR if possible.
150
151    p points to a THUNK_SELECTOR that we want to evaluate.  The
152    result of "evaluating" it will be evacuated and a pointer to the
153    to-space closure will be returned.
154
155    If the THUNK_SELECTOR could not be evaluated (its selectee is still
156    a THUNK, for example), then the THUNK_SELECTOR itself will be
157    evacuated.
158    -------------------------------------------------------------------------- */
159 static void
160 unchain_thunk_selectors(StgSelector *p, StgClosure *val)
161 {
162     StgSelector *prev;
163
164     prev = NULL;
165     while (p)
166     {
167         ASSERT(p->header.info == &stg_BLACKHOLE_info);
168         prev = (StgSelector*)((StgClosure *)p)->payload[0];
169
170         // Update the THUNK_SELECTOR with an indirection to the
171         // EVACUATED closure now at p.  Why do this rather than
172         // upd_evacuee(q,p)?  Because we have an invariant that an
173         // EVACUATED closure always points to an object in the
174         // same or an older generation (required by the short-cut
175         // test in the EVACUATED case, below).
176         SET_INFO(p, &stg_IND_info);
177         ((StgInd *)p)->indirectee = val;
178
179         // For the purposes of LDV profiling, we have created an
180         // indirection.
181         LDV_RECORD_CREATE(p);
182
183         p = prev;
184     }
185 }
186
187 static void
188 eval_thunk_selector (StgClosure **q, StgSelector * p, rtsBool evac)
189                  // NB. for legacy reasons, p & q are swapped around :(
190 {
191     nat field;
192     StgInfoTable *info;
193     StgWord info_ptr;
194     StgClosure *selectee;
195     StgSelector *prev_thunk_selector;
196     bdescr *bd;
197     StgClosure *val;
198     
199     prev_thunk_selector = NULL;
200     // this is a chain of THUNK_SELECTORs that we are going to update
201     // to point to the value of the current THUNK_SELECTOR.  Each
202     // closure on the chain is a BLACKHOLE, and points to the next in the
203     // chain with payload[0].
204
205 selector_chain:
206
207     bd = Bdescr((StgPtr)p);
208     if (HEAP_ALLOCED(p)) {
209         // If the THUNK_SELECTOR is in to-space or in a generation that we
210         // are not collecting, then bale out early.  We won't be able to
211         // save any space in any case, and updating with an indirection is
212         // trickier in a non-collected gen: we would have to update the
213         // mutable list.
214         if ((bd->gen_no > N) || (bd->flags & BF_EVACUATED)) {
215             unchain_thunk_selectors(prev_thunk_selector, (StgClosure *)p);
216             *q = (StgClosure *)p;
217             return;
218         }
219         // we don't update THUNK_SELECTORS in the compacted
220         // generation, because compaction does not remove the INDs
221         // that result, this causes confusion later
222         // (scavenge_mark_stack doesn't deal with IND).  BEWARE!  This
223         // bit is very tricky to get right.  If you make changes
224         // around here, test by compiling stage 3 with +RTS -c -RTS.
225         if (bd->flags & BF_COMPACTED) {
226             // must call evacuate() to mark this closure if evac==rtsTrue
227             *q = (StgClosure *)p;
228             if (evac) evacuate(q);
229             unchain_thunk_selectors(prev_thunk_selector, (StgClosure *)p);
230             return;
231         }
232     }
233
234
235     // BLACKHOLE the selector thunk, since it is now under evaluation.
236     // This is important to stop us going into an infinite loop if
237     // this selector thunk eventually refers to itself.
238 #if defined(THREADED_RTS)
239     // In threaded mode, we'll use WHITEHOLE to lock the selector
240     // thunk while we evaluate it.
241     {
242         info_ptr = xchg((StgPtr)&p->header.info, (W_)&stg_WHITEHOLE_info);
243         if (info_ptr == (W_)&stg_WHITEHOLE_info) {
244             do {
245                 info_ptr = xchg((StgPtr)&p->header.info, (W_)&stg_WHITEHOLE_info);
246             } while (info_ptr == (W_)&stg_WHITEHOLE_info);
247             goto bale_out;
248         }
249         // make sure someone else didn't get here first
250         if (INFO_PTR_TO_STRUCT(info_ptr)->type != THUNK_SELECTOR) {
251             goto bale_out;
252         }
253     }
254 #else
255     // Save the real info pointer (NOTE: not the same as get_itbl()).
256     info_ptr = (StgWord)p->header.info;
257     SET_INFO(p,&stg_BLACKHOLE_info);
258 #endif
259
260     field = INFO_PTR_TO_STRUCT(info_ptr)->layout.selector_offset;
261
262     // The selectee might be a constructor closure,
263     // so we untag the pointer.
264     selectee = UNTAG_CLOSURE(p->selectee);
265
266 selector_loop:
267     // selectee now points to the closure that we're trying to select
268     // a field from.  It may or may not be in to-space: we try not to
269     // end up in to-space, but it's impractical to avoid it in
270     // general.  The compacting GC scatters to-space pointers in
271     // from-space during marking, for example.  We rely on the property
272     // that evacuate() doesn't mind if it gets passed a to-space pointer.
273
274     info = get_itbl(selectee);
275     switch (info->type) {
276       case WHITEHOLE:
277           goto bale_out; // about to be evacuated by another thread (or a loop).
278         
279       case CONSTR:
280       case CONSTR_1_0:
281       case CONSTR_0_1:
282       case CONSTR_2_0:
283       case CONSTR_1_1:
284       case CONSTR_0_2:
285       case CONSTR_STATIC:
286       case CONSTR_NOCAF_STATIC:
287           {
288               // check that the size is in range 
289               ASSERT(field <  (StgWord32)(info->layout.payload.ptrs + 
290                                           info->layout.payload.nptrs));
291           
292               // Select the right field from the constructor
293               val = selectee->payload[field];
294               
295 #ifdef PROFILING
296               // For the purposes of LDV profiling, we have destroyed
297               // the original selector thunk, p.
298               SET_INFO(p, info_ptr);
299               LDV_RECORD_DEAD_FILL_SLOP_DYNAMIC((StgClosure *)p);
300               SET_INFO(p, &stg_BLACKHOLE_info);
301 #endif
302
303               // the closure in val is now the "value" of the
304               // THUNK_SELECTOR in p.  However, val may itself be a
305               // THUNK_SELECTOR, in which case we want to continue
306               // evaluating until we find the real value, and then
307               // update the whole chain to point to the value.
308           val_loop:
309               info = get_itbl(UNTAG_CLOSURE(val));
310               switch (info->type) {
311               case IND:
312               case IND_PERM:
313               case IND_OLDGEN:
314               case IND_OLDGEN_PERM:
315               case IND_STATIC:
316                   val = ((StgInd *)val)->indirectee;
317                   goto val_loop;
318               case THUNK_SELECTOR:
319                   ((StgClosure*)p)->payload[0] = (StgClosure *)prev_thunk_selector;
320                   prev_thunk_selector = p;
321                   p = (StgSelector*)val;
322                   goto selector_chain;
323               default:
324                   ((StgClosure*)p)->payload[0] = (StgClosure *)prev_thunk_selector;
325                   prev_thunk_selector = p;
326
327                   *q = val;
328                   if (evac) evacuate(q);
329                   val = *q;
330                   // evacuate() cannot recurse through
331                   // eval_thunk_selector(), because we know val is not
332                   // a THUNK_SELECTOR.
333                   unchain_thunk_selectors(prev_thunk_selector, val);
334                   return;
335               }
336           }
337
338       case IND:
339       case IND_PERM:
340       case IND_OLDGEN:
341       case IND_OLDGEN_PERM:
342       case IND_STATIC:
343           // Again, we might need to untag a constructor.
344           selectee = UNTAG_CLOSURE( ((StgInd *)selectee)->indirectee );
345           goto selector_loop;
346
347       case EVACUATED:
348           // We don't follow pointers into to-space; the constructor
349           // has already been evacuated, so we won't save any space
350           // leaks by evaluating this selector thunk anyhow.
351           goto bale_out;
352
353       case THUNK_SELECTOR:
354       {
355           StgClosure *val;
356
357           // recursively evaluate this selector.  We don't want to
358           // recurse indefinitely, so we impose a depth bound.
359           if (gct->thunk_selector_depth >= MAX_THUNK_SELECTOR_DEPTH) {
360               goto bale_out;
361           }
362
363           gct->thunk_selector_depth++;
364           // rtsFalse says "don't evacuate the result".  It will,
365           // however, update any THUNK_SELECTORs that are evaluated
366           // along the way.
367           eval_thunk_selector(&val, (StgSelector*)selectee, rtsFalse);
368           gct->thunk_selector_depth--;
369
370           // did we actually manage to evaluate it?
371           if (val == selectee) goto bale_out;
372
373           // Of course this pointer might be tagged...
374           selectee = UNTAG_CLOSURE(val);
375           goto selector_loop;
376       }
377
378       case AP:
379       case AP_STACK:
380       case THUNK:
381       case THUNK_1_0:
382       case THUNK_0_1:
383       case THUNK_2_0:
384       case THUNK_1_1:
385       case THUNK_0_2:
386       case THUNK_STATIC:
387       case CAF_BLACKHOLE:
388       case SE_CAF_BLACKHOLE:
389       case SE_BLACKHOLE:
390       case BLACKHOLE:
391           // not evaluated yet 
392           goto bale_out;
393     
394       default:
395         barf("eval_thunk_selector: strange selectee %d",
396              (int)(info->type));
397     }
398
399 bale_out:
400     // We didn't manage to evaluate this thunk; restore the old info
401     // pointer.  But don't forget: we still need to evacuate the thunk itself.
402     SET_INFO(p, (const StgInfoTable *)info_ptr);
403     if (evac) {
404         copy(&val,(StgClosure *)p,THUNK_SELECTOR_sizeW(),bd->step->to);
405     } else {
406         val = (StgClosure *)p;
407     }
408     *q = val;
409     unchain_thunk_selectors(prev_thunk_selector, val);
410     return;
411 }
412
413 /* -----------------------------------------------------------------------------
414    move_TSO is called to update the TSO structure after it has been
415    moved from one place to another.
416    -------------------------------------------------------------------------- */
417
418 void
419 move_TSO (StgTSO *src, StgTSO *dest)
420 {
421     ptrdiff_t diff;
422
423     // relocate the stack pointer... 
424     diff = (StgPtr)dest - (StgPtr)src; // In *words* 
425     dest->sp = (StgPtr)dest->sp + diff;
426 }
427