1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team 1998-2008
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 * ---------------------------------------------------------------------------*/
23 #include "LdvProfile.h"
25 #if defined(PROF_SPIN) && defined(THREADED_RTS)
26 StgWord64 whitehole_spin = 0;
29 /* Used to avoid long recursion due to selector thunks
31 #define MAX_THUNK_SELECTOR_DEPTH 16
33 static void eval_thunk_selector (StgClosure **q, StgSelector * p, rtsBool);
34 STATIC_INLINE void evacuate_large(StgPtr p);
36 /* -----------------------------------------------------------------------------
37 Allocate some space in which to copy an object.
38 -------------------------------------------------------------------------- */
41 alloc_for_copy (nat size, step *stp)
46 /* Find out where we're going, using the handy "to" pointer in
47 * the step of the source object. If it turns out we need to
48 * evacuate to an older generation, adjust it here (see comment
51 if (stp < gct->evac_step) {
52 if (gct->eager_promotion) {
55 gct->failed_to_evac = rtsTrue;
59 ws = &gct->steps[stp->abs_no];
60 // this compiles to a single mem access to stp->abs_no only
62 /* chain a new block onto the to-space for the destination step if
66 if (to + size > ws->todo_lim) {
67 to = todo_block_full(size, ws);
69 ws->todo_free = to + size;
70 ASSERT(ws->todo_free >= ws->todo_bd->free && ws->todo_free <= ws->todo_lim);
75 /* -----------------------------------------------------------------------------
77 -------------------------------------------------------------------------- */
87 /* -----------------------------------------------------------------------------
88 Evacuate a large object
90 This just consists of removing the object from the (doubly-linked)
91 step->large_objects list, and linking it on to the (singly-linked)
92 step->new_large_objects list, from where it will be scavenged later.
94 Convention: bd->flags has BF_EVACUATED set for a large object
95 that has been evacuated, or unset otherwise.
96 -------------------------------------------------------------------------- */
99 evacuate_large(StgPtr p)
101 bdescr *bd = Bdescr(p);
106 ACQUIRE_SPIN_LOCK(&stp->sync_large_objects);
108 // object must be at the beginning of the block (or be a ByteArray)
109 ASSERT(get_itbl((StgClosure *)p)->type == ARR_WORDS ||
110 (((W_)p & BLOCK_MASK) == 0));
112 // already evacuated?
113 if (bd->flags & BF_EVACUATED) {
114 /* Don't forget to set the gct->failed_to_evac flag if we didn't get
115 * the desired destination (see comments in evacuate()).
117 if (stp < gct->evac_step) {
118 gct->failed_to_evac = rtsTrue;
119 TICK_GC_FAILED_PROMOTION();
121 RELEASE_SPIN_LOCK(&stp->sync_large_objects);
125 // remove from large_object list
127 bd->u.back->link = bd->link;
128 } else { // first object in the list
129 stp->large_objects = bd->link;
132 bd->link->u.back = bd->u.back;
135 /* link it on to the evacuated large object list of the destination step
138 if (new_stp < gct->evac_step) {
139 if (gct->eager_promotion) {
140 new_stp = gct->evac_step;
142 gct->failed_to_evac = rtsTrue;
146 ws = &gct->steps[new_stp->abs_no];
147 bd->flags |= BF_EVACUATED;
149 bd->gen_no = new_stp->gen_no;
150 bd->link = ws->todo_large_objects;
151 ws->todo_large_objects = bd;
153 RELEASE_SPIN_LOCK(&stp->sync_large_objects);
156 /* -----------------------------------------------------------------------------
157 Evaluate a THUNK_SELECTOR if possible.
159 p points to a THUNK_SELECTOR that we want to evaluate. The
160 result of "evaluating" it will be evacuated and a pointer to the
161 to-space closure will be returned.
163 If the THUNK_SELECTOR could not be evaluated (its selectee is still
164 a THUNK, for example), then the THUNK_SELECTOR itself will be
166 -------------------------------------------------------------------------- */
168 unchain_thunk_selectors(StgSelector *p, StgClosure *val)
176 ASSERT(p->header.info == &stg_WHITEHOLE_info);
178 ASSERT(p->header.info == &stg_BLACKHOLE_info);
180 // val must be in to-space. Not always: when we recursively
181 // invoke eval_thunk_selector(), the recursive calls will not
182 // evacuate the value (because we want to select on the value,
183 // not evacuate it), so in this case val is in from-space.
184 // ASSERT(!HEAP_ALLOCED(val) || Bdescr((P_)val)->gen_no > N || (Bdescr((P_)val)->flags & BF_EVACUATED));
186 prev = (StgSelector*)((StgClosure *)p)->payload[0];
188 // Update the THUNK_SELECTOR with an indirection to the
189 // EVACUATED closure now at p. Why do this rather than
190 // upd_evacuee(q,p)? Because we have an invariant that an
191 // EVACUATED closure always points to an object in the
192 // same or an older generation (required by the short-cut
193 // test in the EVACUATED case, below).
194 ((StgInd *)p)->indirectee = val;
196 SET_INFO(p, &stg_IND_info);
198 // For the purposes of LDV profiling, we have created an
200 LDV_RECORD_CREATE(p);
207 eval_thunk_selector (StgClosure **q, StgSelector * p, rtsBool evac)
208 // NB. for legacy reasons, p & q are swapped around :(
213 StgClosure *selectee;
214 StgSelector *prev_thunk_selector;
218 prev_thunk_selector = NULL;
219 // this is a chain of THUNK_SELECTORs that we are going to update
220 // to point to the value of the current THUNK_SELECTOR. Each
221 // closure on the chain is a BLACKHOLE, and points to the next in the
222 // chain with payload[0].
226 bd = Bdescr((StgPtr)p);
227 if (HEAP_ALLOCED(p)) {
228 // If the THUNK_SELECTOR is in to-space or in a generation that we
229 // are not collecting, then bale out early. We won't be able to
230 // save any space in any case, and updating with an indirection is
231 // trickier in a non-collected gen: we would have to update the
233 if (bd->flags & BF_EVACUATED) {
234 unchain_thunk_selectors(prev_thunk_selector, (StgClosure *)p);
235 *q = (StgClosure *)p;
238 // we don't update THUNK_SELECTORS in the compacted
239 // generation, because compaction does not remove the INDs
240 // that result, this causes confusion later
241 // (scavenge_mark_stack doesn't deal with IND). BEWARE! This
242 // bit is very tricky to get right. If you make changes
243 // around here, test by compiling stage 3 with +RTS -c -RTS.
244 if (bd->flags & BF_COMPACTED) {
245 // must call evacuate() to mark this closure if evac==rtsTrue
246 *q = (StgClosure *)p;
247 if (evac) evacuate(q);
248 unchain_thunk_selectors(prev_thunk_selector, (StgClosure *)p);
254 // BLACKHOLE the selector thunk, since it is now under evaluation.
255 // This is important to stop us going into an infinite loop if
256 // this selector thunk eventually refers to itself.
257 #if defined(THREADED_RTS)
258 // In threaded mode, we'll use WHITEHOLE to lock the selector
259 // thunk while we evaluate it.
262 info_ptr = xchg((StgPtr)&p->header.info, (W_)&stg_WHITEHOLE_info);
263 } while (info_ptr == (W_)&stg_WHITEHOLE_info);
265 // make sure someone else didn't get here first...
266 if (IS_FORWARDING_PTR(p) ||
267 INFO_PTR_TO_STRUCT(info_ptr)->type != THUNK_SELECTOR) {
268 // v. tricky now. The THUNK_SELECTOR has been evacuated
269 // by another thread, and is now either a forwarding ptr or IND.
270 // We need to extract ourselves from the current situation
271 // as cleanly as possible.
272 // - unlock the closure
273 // - update *q, we may have done *some* evaluation
274 // - if evac, we need to call evacuate(), because we
275 // need the write-barrier stuff.
276 // - undo the chain we've built to point to p.
277 SET_INFO(p, (const StgInfoTable *)info_ptr);
278 *q = (StgClosure *)p;
279 if (evac) evacuate(q);
280 unchain_thunk_selectors(prev_thunk_selector, (StgClosure *)p);
285 // Save the real info pointer (NOTE: not the same as get_itbl()).
286 info_ptr = (StgWord)p->header.info;
287 SET_INFO(p,&stg_BLACKHOLE_info);
290 field = INFO_PTR_TO_STRUCT(info_ptr)->layout.selector_offset;
292 // The selectee might be a constructor closure,
293 // so we untag the pointer.
294 selectee = UNTAG_CLOSURE(p->selectee);
297 // selectee now points to the closure that we're trying to select
298 // a field from. It may or may not be in to-space: we try not to
299 // end up in to-space, but it's impractical to avoid it in
300 // general. The compacting GC scatters to-space pointers in
301 // from-space during marking, for example. We rely on the property
302 // that evacuate() doesn't mind if it gets passed a to-space pointer.
304 info = (StgInfoTable*)selectee->header.info;
306 if (IS_FORWARDING_PTR(info)) {
307 // We don't follow pointers into to-space; the constructor
308 // has already been evacuated, so we won't save any space
309 // leaks by evaluating this selector thunk anyhow.
313 info = INFO_PTR_TO_STRUCT(info);
314 switch (info->type) {
316 goto bale_out; // about to be evacuated by another thread (or a loop).
325 case CONSTR_NOCAF_STATIC:
327 // check that the size is in range
328 ASSERT(field < (StgWord32)(info->layout.payload.ptrs +
329 info->layout.payload.nptrs));
331 // Select the right field from the constructor
332 val = selectee->payload[field];
335 // For the purposes of LDV profiling, we have destroyed
336 // the original selector thunk, p.
337 SET_INFO(p, (StgInfoTable *)info_ptr);
338 LDV_RECORD_DEAD_FILL_SLOP_DYNAMIC((StgClosure *)p);
339 SET_INFO(p, &stg_BLACKHOLE_info);
342 // the closure in val is now the "value" of the
343 // THUNK_SELECTOR in p. However, val may itself be a
344 // THUNK_SELECTOR, in which case we want to continue
345 // evaluating until we find the real value, and then
346 // update the whole chain to point to the value.
348 info_ptr = (StgWord)UNTAG_CLOSURE(val)->header.info;
349 if (!IS_FORWARDING_PTR(info_ptr))
351 info = INFO_PTR_TO_STRUCT(info_ptr);
352 switch (info->type) {
356 case IND_OLDGEN_PERM:
358 val = ((StgInd *)val)->indirectee;
361 ((StgClosure*)p)->payload[0] = (StgClosure *)prev_thunk_selector;
362 prev_thunk_selector = p;
363 p = (StgSelector*)val;
369 ((StgClosure*)p)->payload[0] = (StgClosure *)prev_thunk_selector;
370 prev_thunk_selector = p;
373 if (evac) evacuate(q);
375 // evacuate() cannot recurse through
376 // eval_thunk_selector(), because we know val is not
378 unchain_thunk_selectors(prev_thunk_selector, val);
385 case IND_OLDGEN_PERM:
387 // Again, we might need to untag a constructor.
388 selectee = UNTAG_CLOSURE( ((StgInd *)selectee)->indirectee );
395 // recursively evaluate this selector. We don't want to
396 // recurse indefinitely, so we impose a depth bound.
397 if (gct->thunk_selector_depth >= MAX_THUNK_SELECTOR_DEPTH) {
401 gct->thunk_selector_depth++;
402 // rtsFalse says "don't evacuate the result". It will,
403 // however, update any THUNK_SELECTORs that are evaluated
405 eval_thunk_selector(&val, (StgSelector*)selectee, rtsFalse);
406 gct->thunk_selector_depth--;
408 // did we actually manage to evaluate it?
409 if (val == selectee) goto bale_out;
411 // Of course this pointer might be tagged...
412 selectee = UNTAG_CLOSURE(val);
426 case SE_CAF_BLACKHOLE:
433 barf("eval_thunk_selector: strange selectee %d",
438 // We didn't manage to evaluate this thunk; restore the old info
439 // pointer. But don't forget: we still need to evacuate the thunk itself.
440 SET_INFO(p, (const StgInfoTable *)info_ptr);
441 // THREADED_RTS: we just unlocked the thunk, so another thread
442 // might get in and update it. copy() will lock it again and
443 // check whether it was updated in the meantime.
444 *q = (StgClosure *)p;
446 copy(q,(const StgInfoTable *)info_ptr,(StgClosure *)p,THUNK_SELECTOR_sizeW(),bd->step->to);
448 unchain_thunk_selectors(prev_thunk_selector, *q);
452 /* -----------------------------------------------------------------------------
453 move_TSO is called to update the TSO structure after it has been
454 moved from one place to another.
455 -------------------------------------------------------------------------- */
458 move_TSO (StgTSO *src, StgTSO *dest)
462 // relocate the stack pointer...
463 diff = (StgPtr)dest - (StgPtr)src; // In *words*
464 dest->sp = (StgPtr)dest->sp + diff;