1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team 1998-2006
5 * Generational garbage collector: evacuation functions
7 * Documentation on the architecture of the Garbage Collector can be
8 * found in the online commentary:
10 * http://hackage.haskell.org/trac/ghc/wiki/Commentary/Rts/Storage/GC
12 * ---------------------------------------------------------------------------*/
22 #include "LdvProfile.h"
24 /* Used to avoid long recursion due to selector thunks
26 #define MAX_THUNK_SELECTOR_DEPTH 16
28 static void eval_thunk_selector (StgClosure **q, StgSelector * p, rtsBool);
29 STATIC_INLINE void evacuate_large(StgPtr p);
31 /* -----------------------------------------------------------------------------
32 Allocate some space in which to copy an object.
33 -------------------------------------------------------------------------- */
36 alloc_for_copy (nat size, step *stp)
41 /* Find out where we're going, using the handy "to" pointer in
42 * the step of the source object. If it turns out we need to
43 * evacuate to an older generation, adjust it here (see comment
46 if (stp < gct->evac_step) {
47 if (gct->eager_promotion) {
50 gct->failed_to_evac = rtsTrue;
54 ws = &gct->steps[stp->gen_no][stp->no];
56 /* chain a new block onto the to-space for the destination step if
60 ASSERT(ws->todo_free >= ws->todo_bd->free && ws->todo_free <= ws->todo_lim);
62 if (to + size > ws->todo_lim) {
63 to = gc_alloc_todo_block(ws);
65 ws->todo_free = to + size;
66 ASSERT(ws->todo_free >= ws->todo_bd->free && ws->todo_free <= ws->todo_lim);
71 /* -----------------------------------------------------------------------------
73 -------------------------------------------------------------------------- */
81 /* -----------------------------------------------------------------------------
82 Evacuate a large object
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.
88 Convention: bd->flags has BF_EVACUATED set for a large object
89 that has been evacuated, or unset otherwise.
90 -------------------------------------------------------------------------- */
93 evacuate_large(StgPtr p)
95 bdescr *bd = Bdescr(p);
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));
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()).
108 if (bd->step < gct->evac_step) {
109 gct->failed_to_evac = rtsTrue;
110 TICK_GC_FAILED_PROMOTION();
117 ACQUIRE_SPIN_LOCK(&stp->sync_large_objects);
118 // remove from large_object list
120 bd->u.back->link = bd->link;
121 } else { // first object in the list
122 stp->large_objects = bd->link;
125 bd->link->u.back = bd->u.back;
127 RELEASE_SPIN_LOCK(&stp->sync_large_objects);
129 /* link it on to the evacuated large object list of the destination step
132 if (stp < gct->evac_step) {
133 if (gct->eager_promotion) {
134 stp = gct->evac_step;
136 gct->failed_to_evac = rtsTrue;
140 ws = &gct->steps[stp->gen_no][stp->no];
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;
148 /* -----------------------------------------------------------------------------
149 Evaluate a THUNK_SELECTOR if possible.
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.
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
158 -------------------------------------------------------------------------- */
160 unchain_thunk_selectors(StgSelector *p, StgClosure *val)
168 ASSERT(p->header.info == &stg_WHITEHOLE_info);
170 ASSERT(p->header.info == &stg_BLACKHOLE_info);
172 // val must be in to-space. Not always: when we recursively
173 // invoke eval_thunk_selector(), the recursive calls will not
174 // evacuate the value (because we want to select on the value,
175 // not evacuate it), so in this case val is in from-space.
176 // ASSERT(!HEAP_ALLOCED(val) || Bdescr((P_)val)->gen_no > N || (Bdescr((P_)val)->flags & BF_EVACUATED));
178 prev = (StgSelector*)((StgClosure *)p)->payload[0];
180 // Update the THUNK_SELECTOR with an indirection to the
181 // EVACUATED closure now at p. Why do this rather than
182 // upd_evacuee(q,p)? Because we have an invariant that an
183 // EVACUATED closure always points to an object in the
184 // same or an older generation (required by the short-cut
185 // test in the EVACUATED case, below).
186 ((StgInd *)p)->indirectee = val;
188 SET_INFO(p, &stg_IND_info);
190 // For the purposes of LDV profiling, we have created an
192 LDV_RECORD_CREATE(p);
199 eval_thunk_selector (StgClosure **q, StgSelector * p, rtsBool evac)
200 // NB. for legacy reasons, p & q are swapped around :(
205 StgClosure *selectee;
206 StgSelector *prev_thunk_selector;
210 prev_thunk_selector = NULL;
211 // this is a chain of THUNK_SELECTORs that we are going to update
212 // to point to the value of the current THUNK_SELECTOR. Each
213 // closure on the chain is a BLACKHOLE, and points to the next in the
214 // chain with payload[0].
218 bd = Bdescr((StgPtr)p);
219 if (HEAP_ALLOCED(p)) {
220 // If the THUNK_SELECTOR is in to-space or in a generation that we
221 // are not collecting, then bale out early. We won't be able to
222 // save any space in any case, and updating with an indirection is
223 // trickier in a non-collected gen: we would have to update the
225 if ((bd->gen_no > N) || (bd->flags & BF_EVACUATED)) {
226 unchain_thunk_selectors(prev_thunk_selector, (StgClosure *)p);
227 *q = (StgClosure *)p;
230 // we don't update THUNK_SELECTORS in the compacted
231 // generation, because compaction does not remove the INDs
232 // that result, this causes confusion later
233 // (scavenge_mark_stack doesn't deal with IND). BEWARE! This
234 // bit is very tricky to get right. If you make changes
235 // around here, test by compiling stage 3 with +RTS -c -RTS.
236 if (bd->flags & BF_COMPACTED) {
237 // must call evacuate() to mark this closure if evac==rtsTrue
238 *q = (StgClosure *)p;
239 if (evac) evacuate(q);
240 unchain_thunk_selectors(prev_thunk_selector, (StgClosure *)p);
246 // BLACKHOLE the selector thunk, since it is now under evaluation.
247 // This is important to stop us going into an infinite loop if
248 // this selector thunk eventually refers to itself.
249 #if defined(THREADED_RTS)
250 // In threaded mode, we'll use WHITEHOLE to lock the selector
251 // thunk while we evaluate it.
254 info_ptr = xchg((StgPtr)&p->header.info, (W_)&stg_WHITEHOLE_info);
255 } while (info_ptr == (W_)&stg_WHITEHOLE_info);
257 // make sure someone else didn't get here first...
258 if (INFO_PTR_TO_STRUCT(info_ptr)->type != THUNK_SELECTOR) {
259 // v. tricky now. The THUNK_SELECTOR has been evacuated
260 // by another thread, and is now either EVACUATED or IND.
261 // We need to extract ourselves from the current situation
262 // as cleanly as possible.
263 // - unlock the closure
264 // - update *q, we may have done *some* evaluation
265 // - if evac, we need to call evacuate(), because we
266 // need the write-barrier stuff.
267 // - undo the chain we've built to point to p.
268 SET_INFO(p, (const StgInfoTable *)info_ptr);
269 *q = (StgClosure *)p;
270 if (evac) evacuate(q);
271 unchain_thunk_selectors(prev_thunk_selector, (StgClosure *)p);
276 // Save the real info pointer (NOTE: not the same as get_itbl()).
277 info_ptr = (StgWord)p->header.info;
278 SET_INFO(p,&stg_BLACKHOLE_info);
281 field = INFO_PTR_TO_STRUCT(info_ptr)->layout.selector_offset;
283 // The selectee might be a constructor closure,
284 // so we untag the pointer.
285 selectee = UNTAG_CLOSURE(p->selectee);
288 // selectee now points to the closure that we're trying to select
289 // a field from. It may or may not be in to-space: we try not to
290 // end up in to-space, but it's impractical to avoid it in
291 // general. The compacting GC scatters to-space pointers in
292 // from-space during marking, for example. We rely on the property
293 // that evacuate() doesn't mind if it gets passed a to-space pointer.
295 info = get_itbl(selectee);
296 switch (info->type) {
298 goto bale_out; // about to be evacuated by another thread (or a loop).
307 case CONSTR_NOCAF_STATIC:
309 // check that the size is in range
310 ASSERT(field < (StgWord32)(info->layout.payload.ptrs +
311 info->layout.payload.nptrs));
313 // Select the right field from the constructor
314 val = selectee->payload[field];
317 // For the purposes of LDV profiling, we have destroyed
318 // the original selector thunk, p.
319 SET_INFO(p, (StgInfoTable *)info_ptr);
320 LDV_RECORD_DEAD_FILL_SLOP_DYNAMIC((StgClosure *)p);
321 SET_INFO(p, &stg_BLACKHOLE_info);
324 // the closure in val is now the "value" of the
325 // THUNK_SELECTOR in p. However, val may itself be a
326 // THUNK_SELECTOR, in which case we want to continue
327 // evaluating until we find the real value, and then
328 // update the whole chain to point to the value.
330 info = get_itbl(UNTAG_CLOSURE(val));
331 switch (info->type) {
335 case IND_OLDGEN_PERM:
337 val = ((StgInd *)val)->indirectee;
340 ((StgClosure*)p)->payload[0] = (StgClosure *)prev_thunk_selector;
341 prev_thunk_selector = p;
342 p = (StgSelector*)val;
345 ((StgClosure*)p)->payload[0] = (StgClosure *)prev_thunk_selector;
346 prev_thunk_selector = p;
349 if (evac) evacuate(q);
351 // evacuate() cannot recurse through
352 // eval_thunk_selector(), because we know val is not
354 unchain_thunk_selectors(prev_thunk_selector, val);
362 case IND_OLDGEN_PERM:
364 // Again, we might need to untag a constructor.
365 selectee = UNTAG_CLOSURE( ((StgInd *)selectee)->indirectee );
369 // We don't follow pointers into to-space; the constructor
370 // has already been evacuated, so we won't save any space
371 // leaks by evaluating this selector thunk anyhow.
378 // recursively evaluate this selector. We don't want to
379 // recurse indefinitely, so we impose a depth bound.
380 if (gct->thunk_selector_depth >= MAX_THUNK_SELECTOR_DEPTH) {
384 gct->thunk_selector_depth++;
385 // rtsFalse says "don't evacuate the result". It will,
386 // however, update any THUNK_SELECTORs that are evaluated
388 eval_thunk_selector(&val, (StgSelector*)selectee, rtsFalse);
389 gct->thunk_selector_depth--;
391 // did we actually manage to evaluate it?
392 if (val == selectee) goto bale_out;
394 // Of course this pointer might be tagged...
395 selectee = UNTAG_CLOSURE(val);
409 case SE_CAF_BLACKHOLE:
416 barf("eval_thunk_selector: strange selectee %d",
421 // We didn't manage to evaluate this thunk; restore the old info
422 // pointer. But don't forget: we still need to evacuate the thunk itself.
423 SET_INFO(p, (const StgInfoTable *)info_ptr);
424 // THREADED_RTS: we just unlocked the thunk, so another thread
425 // might get in and update it. copy() will lock it again and
426 // check whether it was updated in the meantime.
427 *q = (StgClosure *)p;
429 copy(q,(StgClosure *)p,THUNK_SELECTOR_sizeW(),bd->step->to);
431 unchain_thunk_selectors(prev_thunk_selector, *q);
435 /* -----------------------------------------------------------------------------
436 move_TSO is called to update the TSO structure after it has been
437 moved from one place to another.
438 -------------------------------------------------------------------------- */
441 move_TSO (StgTSO *src, StgTSO *dest)
445 // relocate the stack pointer...
446 diff = (StgPtr)dest - (StgPtr)src; // In *words*
447 dest->sp = (StgPtr)dest->sp + diff;