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 lnat thunk_selector_depth = 0;
27 #define MAX_THUNK_SELECTOR_DEPTH 16
29 static StgClosure * eval_thunk_selector (StgSelector * p, rtsBool);
32 upd_evacuee(StgClosure *p, StgClosure *dest)
34 // not true: (ToDo: perhaps it should be)
35 // ASSERT(Bdescr((P_)dest)->flags & BF_EVACUATED);
36 SET_INFO(p, &stg_EVACUATED_info);
37 ((StgEvacuated *)p)->evacuee = dest;
41 STATIC_INLINE StgClosure *
42 copy_tag(StgClosure *src, nat size, step *stp,StgWord tag)
51 TICK_GC_WORDS_COPIED(size);
52 /* Find out where we're going, using the handy "to" pointer in
53 * the step of the source object. If it turns out we need to
54 * evacuate to an older generation, adjust it here (see comment
57 if (stp->gen_no < evac_gen) {
58 if (eager_promotion) {
59 stp = &generations[evac_gen].steps[0];
61 failed_to_evac = rtsTrue;
65 /* chain a new block onto the to-space for the destination step if
68 if (stp->hp + size >= stp->hpLim) {
75 for (i = 0; i < size; i++) { // unroll for small i
79 /* retag pointer before updating EVACUATE closure and returning */
80 to = (StgPtr)TAG_CLOSURE(tag,(StgClosure*)to);
82 upd_evacuee((StgClosure *)from,(StgClosure *)to);
85 // We store the size of the just evacuated object in the LDV word so that
86 // the profiler can guess the position of the next object later.
87 SET_EVACUAEE_FOR_LDV(from, size_org);
89 return (StgClosure *)to;
92 // Same as copy() above, except the object will be allocated in memory
93 // that will not be scavenged. Used for object that have no pointer
95 STATIC_INLINE StgClosure *
96 copy_noscav_tag(StgClosure *src, nat size, step *stp, StgWord tag)
105 TICK_GC_WORDS_COPIED(size);
106 /* Find out where we're going, using the handy "to" pointer in
107 * the step of the source object. If it turns out we need to
108 * evacuate to an older generation, adjust it here (see comment
111 if (stp->gen_no < evac_gen) {
112 if (eager_promotion) {
113 stp = &generations[evac_gen].steps[0];
115 failed_to_evac = rtsTrue;
119 /* chain a new block onto the to-space for the destination step if
122 if (stp->scavd_hp + size >= stp->scavd_hpLim) {
123 gc_alloc_scavd_block(stp);
128 stp->scavd_hp = to + size;
129 for (i = 0; i < size; i++) { // unroll for small i
133 /* retag pointer before updating EVACUATE closure and returning */
134 to = (StgPtr)TAG_CLOSURE(tag,(StgClosure*)to);
136 upd_evacuee((StgClosure *)from,(StgClosure *)to);
139 // We store the size of the just evacuated object in the LDV word so that
140 // the profiler can guess the position of the next object later.
141 SET_EVACUAEE_FOR_LDV(from, size_org);
143 return (StgClosure *)to;
146 /* Special version of copy() for when we only want to copy the info
147 * pointer of an object, but reserve some padding after it. This is
148 * used to optimise evacuation of BLACKHOLEs.
153 copyPart(StgClosure *src, nat size_to_reserve, nat size_to_copy, step *stp)
158 nat size_to_copy_org = size_to_copy;
161 TICK_GC_WORDS_COPIED(size_to_copy);
162 if (stp->gen_no < evac_gen) {
163 if (eager_promotion) {
164 stp = &generations[evac_gen].steps[0];
166 failed_to_evac = rtsTrue;
170 if (stp->hp + size_to_reserve >= stp->hpLim) {
174 for(to = stp->hp, from = (P_)src; size_to_copy>0; --size_to_copy) {
179 stp->hp += size_to_reserve;
180 upd_evacuee(src,(StgClosure *)dest);
182 // We store the size of the just evacuated object in the LDV word so that
183 // the profiler can guess the position of the next object later.
184 // size_to_copy_org is wrong because the closure already occupies size_to_reserve
186 SET_EVACUAEE_FOR_LDV(src, size_to_reserve);
188 if (size_to_reserve - size_to_copy_org > 0)
189 LDV_FILL_SLOP(stp->hp - 1, (int)(size_to_reserve - size_to_copy_org));
191 return (StgClosure *)dest;
195 /* Copy wrappers that don't tag the closure after copying */
196 STATIC_INLINE StgClosure *
197 copy(StgClosure *src, nat size, step *stp)
199 return copy_tag(src,size,stp,0);
202 STATIC_INLINE StgClosure *
203 copy_noscav(StgClosure *src, nat size, step *stp)
205 return copy_noscav_tag(src,size,stp,0);
208 /* -----------------------------------------------------------------------------
209 Evacuate a large object
211 This just consists of removing the object from the (doubly-linked)
212 step->large_objects list, and linking it on to the (singly-linked)
213 step->new_large_objects list, from where it will be scavenged later.
215 Convention: bd->flags has BF_EVACUATED set for a large object
216 that has been evacuated, or unset otherwise.
217 -------------------------------------------------------------------------- */
221 evacuate_large(StgPtr p)
223 bdescr *bd = Bdescr(p);
226 // object must be at the beginning of the block (or be a ByteArray)
227 ASSERT(get_itbl((StgClosure *)p)->type == ARR_WORDS ||
228 (((W_)p & BLOCK_MASK) == 0));
230 // already evacuated?
231 if (bd->flags & BF_EVACUATED) {
232 /* Don't forget to set the failed_to_evac flag if we didn't get
233 * the desired destination (see comments in evacuate()).
235 if (bd->gen_no < evac_gen) {
236 failed_to_evac = rtsTrue;
237 TICK_GC_FAILED_PROMOTION();
243 // remove from large_object list
245 bd->u.back->link = bd->link;
246 } else { // first object in the list
247 stp->large_objects = bd->link;
250 bd->link->u.back = bd->u.back;
253 /* link it on to the evacuated large object list of the destination step
256 if (stp->gen_no < evac_gen) {
257 if (eager_promotion) {
258 stp = &generations[evac_gen].steps[0];
260 failed_to_evac = rtsTrue;
265 bd->gen_no = stp->gen_no;
266 bd->link = stp->new_large_objects;
267 stp->new_large_objects = bd;
268 bd->flags |= BF_EVACUATED;
271 /* -----------------------------------------------------------------------------
274 This is called (eventually) for every live object in the system.
276 The caller to evacuate specifies a desired generation in the
277 evac_gen global variable. The following conditions apply to
278 evacuating an object which resides in generation M when we're
279 collecting up to generation N
283 else evac to step->to
285 if M < evac_gen evac to evac_gen, step 0
287 if the object is already evacuated, then we check which generation
290 if M >= evac_gen do nothing
291 if M < evac_gen set failed_to_evac flag to indicate that we
292 didn't manage to evacuate this object into evac_gen.
297 evacuate() is the single most important function performance-wise
298 in the GC. Various things have been tried to speed it up, but as
299 far as I can tell the code generated by gcc 3.2 with -O2 is about
300 as good as it's going to get. We pass the argument to evacuate()
301 in a register using the 'regparm' attribute (see the prototype for
302 evacuate() near the top of this file).
304 Changing evacuate() to take an (StgClosure **) rather than
305 returning the new pointer seems attractive, because we can avoid
306 writing back the pointer when it hasn't changed (eg. for a static
307 object, or an object in a generation > N). However, I tried it and
308 it doesn't help. One reason is that the (StgClosure **) pointer
309 gets spilled to the stack inside evacuate(), resulting in far more
310 extra reads/writes than we save.
311 -------------------------------------------------------------------------- */
313 REGPARM1 StgClosure *
314 evacuate(StgClosure *q)
318 const StgInfoTable *info;
322 /* The tag and the pointer are split, to be merged after evacing */
323 tag = GET_CLOSURE_TAG(q);
324 q = UNTAG_CLOSURE(q);
326 ASSERT(LOOKS_LIKE_CLOSURE_PTR(q));
328 if (!HEAP_ALLOCED(q)) {
330 if (!major_gc) return TAG_CLOSURE(tag,q);
333 switch (info->type) {
336 if (info->srt_bitmap != 0 &&
337 *THUNK_STATIC_LINK((StgClosure *)q) == NULL) {
338 *THUNK_STATIC_LINK((StgClosure *)q) = static_objects;
339 static_objects = (StgClosure *)q;
344 if (info->srt_bitmap != 0 &&
345 *FUN_STATIC_LINK((StgClosure *)q) == NULL) {
346 *FUN_STATIC_LINK((StgClosure *)q) = static_objects;
347 static_objects = (StgClosure *)q;
352 /* If q->saved_info != NULL, then it's a revertible CAF - it'll be
353 * on the CAF list, so don't do anything with it here (we'll
354 * scavenge it later).
356 if (((StgIndStatic *)q)->saved_info == NULL
357 && *IND_STATIC_LINK((StgClosure *)q) == NULL) {
358 *IND_STATIC_LINK((StgClosure *)q) = static_objects;
359 static_objects = (StgClosure *)q;
364 if (*STATIC_LINK(info,(StgClosure *)q) == NULL) {
365 *STATIC_LINK(info,(StgClosure *)q) = static_objects;
366 static_objects = (StgClosure *)q;
367 /* I am assuming that static_objects pointers are not
368 * written to other objects, and thus, no need to retag. */
370 return TAG_CLOSURE(tag,q);
372 case CONSTR_NOCAF_STATIC:
373 /* no need to put these on the static linked list, they don't need
376 return TAG_CLOSURE(tag,q);
379 barf("evacuate(static): strange closure type %d", (int)(info->type));
385 if (bd->gen_no > N) {
386 /* Can't evacuate this object, because it's in a generation
387 * older than the ones we're collecting. Let's hope that it's
388 * in evac_gen or older, or we will have to arrange to track
389 * this pointer using the mutable list.
391 if (bd->gen_no < evac_gen) {
393 failed_to_evac = rtsTrue;
394 TICK_GC_FAILED_PROMOTION();
396 return TAG_CLOSURE(tag,q);
399 if ((bd->flags & (BF_LARGE | BF_COMPACTED | BF_EVACUATED)) != 0) {
401 /* pointer into to-space: just return it. This normally
402 * shouldn't happen, but alllowing it makes certain things
403 * slightly easier (eg. the mutable list can contain the same
404 * object twice, for example).
406 if (bd->flags & BF_EVACUATED) {
407 if (bd->gen_no < evac_gen) {
408 failed_to_evac = rtsTrue;
409 TICK_GC_FAILED_PROMOTION();
411 return TAG_CLOSURE(tag,q);
414 /* evacuate large objects by re-linking them onto a different list.
416 if (bd->flags & BF_LARGE) {
418 if (info->type == TSO &&
419 ((StgTSO *)q)->what_next == ThreadRelocated) {
420 q = (StgClosure *)((StgTSO *)q)->link;
423 evacuate_large((P_)q);
424 return TAG_CLOSURE(tag,q);
427 /* If the object is in a step that we're compacting, then we
428 * need to use an alternative evacuate procedure.
430 if (bd->flags & BF_COMPACTED) {
431 if (!is_marked((P_)q,bd)) {
433 if (mark_stack_full()) {
434 mark_stack_overflowed = rtsTrue;
437 push_mark_stack((P_)q);
439 return TAG_CLOSURE(tag,q);
447 switch (info->type) {
453 return copy(q,sizeW_fromITBL(info),stp);
457 StgWord w = (StgWord)q->payload[0];
458 if (q->header.info == Czh_con_info &&
459 // unsigned, so always true: (StgChar)w >= MIN_CHARLIKE &&
460 (StgChar)w <= MAX_CHARLIKE) {
461 return TAG_CLOSURE(tag,
462 (StgClosure *)CHARLIKE_CLOSURE((StgChar)w)
465 if (q->header.info == Izh_con_info &&
466 (StgInt)w >= MIN_INTLIKE && (StgInt)w <= MAX_INTLIKE) {
467 return TAG_CLOSURE(tag,
468 (StgClosure *)INTLIKE_CLOSURE((StgInt)w)
472 return copy_noscav_tag(q,sizeofW(StgHeader)+1,stp,tag);
478 return copy_tag(q,sizeofW(StgHeader)+1,stp,tag);
482 return copy(q,sizeofW(StgThunk)+1,stp);
487 #ifdef NO_PROMOTE_THUNKS
488 if (bd->gen_no == 0 &&
490 bd->step->no == generations[bd->gen_no].n_steps-1) {
494 return copy(q,sizeofW(StgThunk)+2,stp);
501 return copy_tag(q,sizeofW(StgHeader)+2,stp,tag);
504 return copy_noscav_tag(q,sizeofW(StgHeader)+2,stp,tag);
507 return copy(q,thunk_sizeW_fromITBL(info),stp);
511 case IND_OLDGEN_PERM:
515 return copy_tag(q,sizeW_fromITBL(info),stp,tag);
518 return copy(q,bco_sizeW((StgBCO *)q),stp);
521 case SE_CAF_BLACKHOLE:
524 return copyPart(q,BLACKHOLE_sizeW(),sizeofW(StgHeader),stp);
527 return eval_thunk_selector((StgSelector *)q, rtsTrue);
531 // follow chains of indirections, don't evacuate them
532 q = ((StgInd*)q)->indirectee;
542 case CATCH_STM_FRAME:
543 case CATCH_RETRY_FRAME:
544 case ATOMICALLY_FRAME:
545 // shouldn't see these
546 barf("evacuate: stack frame at %p\n", q);
549 return copy(q,pap_sizeW((StgPAP*)q),stp);
552 return copy(q,ap_sizeW((StgAP*)q),stp);
555 return copy(q,ap_stack_sizeW((StgAP_STACK*)q),stp);
558 /* Already evacuated, just return the forwarding address.
559 * HOWEVER: if the requested destination generation (evac_gen) is
560 * older than the actual generation (because the object was
561 * already evacuated to a younger generation) then we have to
562 * set the failed_to_evac flag to indicate that we couldn't
563 * manage to promote the object to the desired generation.
566 * Optimisation: the check is fairly expensive, but we can often
567 * shortcut it if either the required generation is 0, or the
568 * current object (the EVACUATED) is in a high enough generation.
569 * We know that an EVACUATED always points to an object in the
570 * same or an older generation. stp is the lowest step that the
571 * current object would be evacuated to, so we only do the full
572 * check if stp is too low.
574 if (evac_gen > 0 && stp->gen_no < evac_gen) { // optimisation
575 StgClosure *p = ((StgEvacuated*)q)->evacuee;
576 if (HEAP_ALLOCED(p) && Bdescr((P_)p)->gen_no < evac_gen) {
577 failed_to_evac = rtsTrue;
578 TICK_GC_FAILED_PROMOTION();
581 return ((StgEvacuated*)q)->evacuee;
584 // just copy the block
585 return copy_noscav(q,arr_words_sizeW((StgArrWords *)q),stp);
587 case MUT_ARR_PTRS_CLEAN:
588 case MUT_ARR_PTRS_DIRTY:
589 case MUT_ARR_PTRS_FROZEN:
590 case MUT_ARR_PTRS_FROZEN0:
591 // just copy the block
592 return copy(q,mut_arr_ptrs_sizeW((StgMutArrPtrs *)q),stp);
596 StgTSO *tso = (StgTSO *)q;
598 /* Deal with redirected TSOs (a TSO that's had its stack enlarged).
600 if (tso->what_next == ThreadRelocated) {
601 q = (StgClosure *)tso->link;
605 /* To evacuate a small TSO, we need to relocate the update frame
612 new_tso = (StgTSO *)copyPart((StgClosure *)tso,
614 sizeofW(StgTSO), stp);
615 move_TSO(tso, new_tso);
616 for (p = tso->sp, q = new_tso->sp;
617 p < tso->stack+tso->stack_size;) {
621 return (StgClosure *)new_tso;
626 return copy(q,sizeofW(StgTRecHeader),stp);
628 case TVAR_WATCH_QUEUE:
629 return copy(q,sizeofW(StgTVarWatchQueue),stp);
632 return copy(q,sizeofW(StgTVar),stp);
635 return copy(q,sizeofW(StgTRecChunk),stp);
637 case ATOMIC_INVARIANT:
638 return copy(q,sizeofW(StgAtomicInvariant),stp);
640 case INVARIANT_CHECK_QUEUE:
641 return copy(q,sizeofW(StgInvariantCheckQueue),stp);
644 barf("evacuate: strange closure type %d", (int)(info->type));
651 unchain_thunk_selectors(StgSelector *p, StgClosure *val)
658 ASSERT(p->header.info == &stg_BLACKHOLE_info);
659 prev = (StgSelector*)((StgClosure *)p)->payload[0];
661 // Update the THUNK_SELECTOR with an indirection to the
662 // EVACUATED closure now at p. Why do this rather than
663 // upd_evacuee(q,p)? Because we have an invariant that an
664 // EVACUATED closure always points to an object in the
665 // same or an older generation (required by the short-cut
666 // test in the EVACUATED case, below).
667 SET_INFO(p, &stg_IND_info);
668 ((StgInd *)p)->indirectee = val;
670 // For the purposes of LDV profiling, we have created an
672 LDV_RECORD_CREATE(p);
678 /* -----------------------------------------------------------------------------
679 Evaluate a THUNK_SELECTOR if possible.
681 p points to a THUNK_SELECTOR that we want to evaluate. The
682 result of "evaluating" it will be evacuated and a pointer to the
683 to-space closure will be returned.
685 If the THUNK_SELECTOR could not be evaluated (its selectee is still
686 a THUNK, for example), then the THUNK_SELECTOR itself will be
688 -------------------------------------------------------------------------- */
691 eval_thunk_selector (StgSelector * p, rtsBool evac)
695 const StgInfoTable *info_ptr;
696 StgClosure *selectee;
697 StgSelector *prev_thunk_selector;
701 prev_thunk_selector = NULL;
702 // this is a chain of THUNK_SELECTORs that we are going to update
703 // to point to the value of the current THUNK_SELECTOR. Each
704 // closure on the chain is a BLACKHOLE, and points to the next in the
705 // chain with payload[0].
709 // The selectee might be a constructor closure,
710 // so we untag the pointer.
711 selectee = UNTAG_CLOSURE(p->selectee);
713 // Save the real info pointer (NOTE: not the same as get_itbl()).
714 info_ptr = p->header.info;
715 field = get_itbl(p)->layout.selector_offset;
717 bd = Bdescr((StgPtr)p);
718 if (HEAP_ALLOCED(p)) {
719 // If the THUNK_SELECTOR is in to-space or in a generation that we
720 // are not collecting, then bale out early. We won't be able to
721 // save any space in any case, and updating with an indirection is
722 // trickier in a non-collected gen: we would have to update the
724 if ((bd->gen_no > N) || (bd->flags & BF_EVACUATED)) {
725 unchain_thunk_selectors(prev_thunk_selector, (StgClosure *)p);
726 return (StgClosure *)p;
728 // we don't update THUNK_SELECTORS in the compacted
729 // generation, because compaction does not remove the INDs
730 // that result, this causes confusion later
731 // (scavenge_mark_stack doesn't deal with IND). BEWARE! This
732 // bit is very tricky to get right. If you make changes
733 // around here, test by compiling stage 3 with +RTS -c -RTS.
734 if (bd->flags & BF_COMPACTED) {
735 // must call evacuate() to mark this closure if evac==rtsTrue
736 if (evac) p = (StgSelector *)evacuate((StgClosure *)p);
737 unchain_thunk_selectors(prev_thunk_selector, (StgClosure *)p);
738 return (StgClosure *)p;
742 // BLACKHOLE the selector thunk, since it is now under evaluation.
743 // This is important to stop us going into an infinite loop if
744 // this selector thunk eventually refers to itself.
745 SET_INFO(p,&stg_BLACKHOLE_info);
748 // selectee now points to the closure that we're trying to select
749 // a field from. It may or may not be in to-space: we try not to
750 // end up in to-space, but it's impractical to avoid it in
751 // general. The compacting GC scatters to-space pointers in
752 // from-space during marking, for example. We rely on the property
753 // that evacuate() doesn't mind if it gets passed a to-space pointer.
755 info = get_itbl(selectee);
756 switch (info->type) {
764 case CONSTR_NOCAF_STATIC:
766 // check that the size is in range
767 ASSERT(field < (StgWord32)(info->layout.payload.ptrs +
768 info->layout.payload.nptrs));
770 // Select the right field from the constructor
771 val = selectee->payload[field];
774 // For the purposes of LDV profiling, we have destroyed
775 // the original selector thunk, p.
776 SET_INFO(p, info_ptr);
777 LDV_RECORD_DEAD_FILL_SLOP_DYNAMIC((StgClosure *)p);
778 SET_INFO(p, &stg_BLACKHOLE_info);
781 // the closure in val is now the "value" of the
782 // THUNK_SELECTOR in p. However, val may itself be a
783 // THUNK_SELECTOR, in which case we want to continue
784 // evaluating until we find the real value, and then
785 // update the whole chain to point to the value.
787 info = get_itbl(UNTAG_CLOSURE(val));
788 switch (info->type) {
792 case IND_OLDGEN_PERM:
794 val = ((StgInd *)val)->indirectee;
797 ((StgClosure*)p)->payload[0] = (StgClosure *)prev_thunk_selector;
798 prev_thunk_selector = p;
799 p = (StgSelector*)val;
802 ((StgClosure*)p)->payload[0] = (StgClosure *)prev_thunk_selector;
803 prev_thunk_selector = p;
805 if (evac) val = evacuate(val);
806 // evacuate() cannot recurse through
807 // eval_thunk_selector(), because we know val is not
809 unchain_thunk_selectors(prev_thunk_selector, val);
817 case IND_OLDGEN_PERM:
819 // Again, we might need to untag a constructor.
820 selectee = UNTAG_CLOSURE( ((StgInd *)selectee)->indirectee );
824 // We don't follow pointers into to-space; the constructor
825 // has already been evacuated, so we won't save any space
826 // leaks by evaluating this selector thunk anyhow.
833 // recursively evaluate this selector. We don't want to
834 // recurse indefinitely, so we impose a depth bound.
835 if (thunk_selector_depth >= MAX_THUNK_SELECTOR_DEPTH) {
839 thunk_selector_depth++;
840 // rtsFalse says "don't evacuate the result". It will,
841 // however, update any THUNK_SELECTORs that are evaluated
843 val = eval_thunk_selector((StgSelector *)selectee, rtsFalse);
844 thunk_selector_depth--;
846 // did we actually manage to evaluate it?
847 if (val == selectee) goto bale_out;
849 // Of course this pointer might be tagged...
850 selectee = UNTAG_CLOSURE(val);
864 case SE_CAF_BLACKHOLE:
871 barf("eval_thunk_selector: strange selectee %d",
876 // We didn't manage to evaluate this thunk; restore the old info
877 // pointer. But don't forget: we still need to evacuate the thunk itself.
878 SET_INFO(p, info_ptr);
880 val = copy((StgClosure *)p,THUNK_SELECTOR_sizeW(),bd->step->to);
882 val = (StgClosure *)p;
884 unchain_thunk_selectors(prev_thunk_selector, val);
888 /* -----------------------------------------------------------------------------
889 move_TSO is called to update the TSO structure after it has been
890 moved from one place to another.
891 -------------------------------------------------------------------------- */
894 move_TSO (StgTSO *src, StgTSO *dest)
898 // relocate the stack pointer...
899 diff = (StgPtr)dest - (StgPtr)src; // In *words*
900 dest->sp = (StgPtr)dest->sp + diff;