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 8
29 static StgClosure * eval_thunk_selector ( nat field, StgSelector * p );
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(StgClosure *src, nat size, step *stp)
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
78 upd_evacuee((StgClosure *)from,(StgClosure *)to);
81 // We store the size of the just evacuated object in the LDV word so that
82 // the profiler can guess the position of the next object later.
83 SET_EVACUAEE_FOR_LDV(from, size_org);
85 return (StgClosure *)to;
88 // Same as copy() above, except the object will be allocated in memory
89 // that will not be scavenged. Used for object that have no pointer
91 STATIC_INLINE StgClosure *
92 copy_noscav(StgClosure *src, nat size, step *stp)
101 TICK_GC_WORDS_COPIED(size);
102 /* Find out where we're going, using the handy "to" pointer in
103 * the step of the source object. If it turns out we need to
104 * evacuate to an older generation, adjust it here (see comment
107 if (stp->gen_no < evac_gen) {
108 if (eager_promotion) {
109 stp = &generations[evac_gen].steps[0];
111 failed_to_evac = rtsTrue;
115 /* chain a new block onto the to-space for the destination step if
118 if (stp->scavd_hp + size >= stp->scavd_hpLim) {
119 gc_alloc_scavd_block(stp);
124 stp->scavd_hp = to + size;
125 for (i = 0; i < size; i++) { // unroll for small i
128 upd_evacuee((StgClosure *)from,(StgClosure *)to);
131 // We store the size of the just evacuated object in the LDV word so that
132 // the profiler can guess the position of the next object later.
133 SET_EVACUAEE_FOR_LDV(from, size_org);
135 return (StgClosure *)to;
138 /* Special version of copy() for when we only want to copy the info
139 * pointer of an object, but reserve some padding after it. This is
140 * used to optimise evacuation of BLACKHOLEs.
145 copyPart(StgClosure *src, nat size_to_reserve, nat size_to_copy, step *stp)
150 nat size_to_copy_org = size_to_copy;
153 TICK_GC_WORDS_COPIED(size_to_copy);
154 if (stp->gen_no < evac_gen) {
155 if (eager_promotion) {
156 stp = &generations[evac_gen].steps[0];
158 failed_to_evac = rtsTrue;
162 if (stp->hp + size_to_reserve >= stp->hpLim) {
166 for(to = stp->hp, from = (P_)src; size_to_copy>0; --size_to_copy) {
171 stp->hp += size_to_reserve;
172 upd_evacuee(src,(StgClosure *)dest);
174 // We store the size of the just evacuated object in the LDV word so that
175 // the profiler can guess the position of the next object later.
176 // size_to_copy_org is wrong because the closure already occupies size_to_reserve
178 SET_EVACUAEE_FOR_LDV(src, size_to_reserve);
180 if (size_to_reserve - size_to_copy_org > 0)
181 LDV_FILL_SLOP(stp->hp - 1, (int)(size_to_reserve - size_to_copy_org));
183 return (StgClosure *)dest;
187 /* -----------------------------------------------------------------------------
188 Evacuate a large object
190 This just consists of removing the object from the (doubly-linked)
191 step->large_objects list, and linking it on to the (singly-linked)
192 step->new_large_objects list, from where it will be scavenged later.
194 Convention: bd->flags has BF_EVACUATED set for a large object
195 that has been evacuated, or unset otherwise.
196 -------------------------------------------------------------------------- */
200 evacuate_large(StgPtr p)
202 bdescr *bd = Bdescr(p);
205 // object must be at the beginning of the block (or be a ByteArray)
206 ASSERT(get_itbl((StgClosure *)p)->type == ARR_WORDS ||
207 (((W_)p & BLOCK_MASK) == 0));
209 // already evacuated?
210 if (bd->flags & BF_EVACUATED) {
211 /* Don't forget to set the failed_to_evac flag if we didn't get
212 * the desired destination (see comments in evacuate()).
214 if (bd->gen_no < evac_gen) {
215 failed_to_evac = rtsTrue;
216 TICK_GC_FAILED_PROMOTION();
222 // remove from large_object list
224 bd->u.back->link = bd->link;
225 } else { // first object in the list
226 stp->large_objects = bd->link;
229 bd->link->u.back = bd->u.back;
232 /* link it on to the evacuated large object list of the destination step
235 if (stp->gen_no < evac_gen) {
236 if (eager_promotion) {
237 stp = &generations[evac_gen].steps[0];
239 failed_to_evac = rtsTrue;
244 bd->gen_no = stp->gen_no;
245 bd->link = stp->new_large_objects;
246 stp->new_large_objects = bd;
247 bd->flags |= BF_EVACUATED;
250 /* -----------------------------------------------------------------------------
253 This is called (eventually) for every live object in the system.
255 The caller to evacuate specifies a desired generation in the
256 evac_gen global variable. The following conditions apply to
257 evacuating an object which resides in generation M when we're
258 collecting up to generation N
262 else evac to step->to
264 if M < evac_gen evac to evac_gen, step 0
266 if the object is already evacuated, then we check which generation
269 if M >= evac_gen do nothing
270 if M < evac_gen set failed_to_evac flag to indicate that we
271 didn't manage to evacuate this object into evac_gen.
276 evacuate() is the single most important function performance-wise
277 in the GC. Various things have been tried to speed it up, but as
278 far as I can tell the code generated by gcc 3.2 with -O2 is about
279 as good as it's going to get. We pass the argument to evacuate()
280 in a register using the 'regparm' attribute (see the prototype for
281 evacuate() near the top of this file).
283 Changing evacuate() to take an (StgClosure **) rather than
284 returning the new pointer seems attractive, because we can avoid
285 writing back the pointer when it hasn't changed (eg. for a static
286 object, or an object in a generation > N). However, I tried it and
287 it doesn't help. One reason is that the (StgClosure **) pointer
288 gets spilled to the stack inside evacuate(), resulting in far more
289 extra reads/writes than we save.
290 -------------------------------------------------------------------------- */
292 REGPARM1 StgClosure *
293 evacuate(StgClosure *q)
297 const StgInfoTable *info;
300 ASSERT(LOOKS_LIKE_CLOSURE_PTR(q));
302 if (!HEAP_ALLOCED(q)) {
304 if (!major_gc) return q;
307 switch (info->type) {
310 if (info->srt_bitmap != 0 &&
311 *THUNK_STATIC_LINK((StgClosure *)q) == NULL) {
312 *THUNK_STATIC_LINK((StgClosure *)q) = static_objects;
313 static_objects = (StgClosure *)q;
318 if (info->srt_bitmap != 0 &&
319 *FUN_STATIC_LINK((StgClosure *)q) == NULL) {
320 *FUN_STATIC_LINK((StgClosure *)q) = static_objects;
321 static_objects = (StgClosure *)q;
326 /* If q->saved_info != NULL, then it's a revertible CAF - it'll be
327 * on the CAF list, so don't do anything with it here (we'll
328 * scavenge it later).
330 if (((StgIndStatic *)q)->saved_info == NULL
331 && *IND_STATIC_LINK((StgClosure *)q) == NULL) {
332 *IND_STATIC_LINK((StgClosure *)q) = static_objects;
333 static_objects = (StgClosure *)q;
338 if (*STATIC_LINK(info,(StgClosure *)q) == NULL) {
339 *STATIC_LINK(info,(StgClosure *)q) = static_objects;
340 static_objects = (StgClosure *)q;
344 case CONSTR_NOCAF_STATIC:
345 /* no need to put these on the static linked list, they don't need
351 barf("evacuate(static): strange closure type %d", (int)(info->type));
357 if (bd->gen_no > N) {
358 /* Can't evacuate this object, because it's in a generation
359 * older than the ones we're collecting. Let's hope that it's
360 * in evac_gen or older, or we will have to arrange to track
361 * this pointer using the mutable list.
363 if (bd->gen_no < evac_gen) {
365 failed_to_evac = rtsTrue;
366 TICK_GC_FAILED_PROMOTION();
371 if ((bd->flags & (BF_LARGE | BF_COMPACTED | BF_EVACUATED)) != 0) {
373 /* pointer into to-space: just return it. This normally
374 * shouldn't happen, but alllowing it makes certain things
375 * slightly easier (eg. the mutable list can contain the same
376 * object twice, for example).
378 if (bd->flags & BF_EVACUATED) {
379 if (bd->gen_no < evac_gen) {
380 failed_to_evac = rtsTrue;
381 TICK_GC_FAILED_PROMOTION();
386 /* evacuate large objects by re-linking them onto a different list.
388 if (bd->flags & BF_LARGE) {
390 if (info->type == TSO &&
391 ((StgTSO *)q)->what_next == ThreadRelocated) {
392 q = (StgClosure *)((StgTSO *)q)->link;
395 evacuate_large((P_)q);
399 /* If the object is in a step that we're compacting, then we
400 * need to use an alternative evacuate procedure.
402 if (bd->flags & BF_COMPACTED) {
403 if (!is_marked((P_)q,bd)) {
405 if (mark_stack_full()) {
406 mark_stack_overflowed = rtsTrue;
409 push_mark_stack((P_)q);
419 switch (info->type) {
424 return copy(q,sizeW_fromITBL(info),stp);
428 StgWord w = (StgWord)q->payload[0];
429 if (q->header.info == Czh_con_info &&
430 // unsigned, so always true: (StgChar)w >= MIN_CHARLIKE &&
431 (StgChar)w <= MAX_CHARLIKE) {
432 return (StgClosure *)CHARLIKE_CLOSURE((StgChar)w);
434 if (q->header.info == Izh_con_info &&
435 (StgInt)w >= MIN_INTLIKE && (StgInt)w <= MAX_INTLIKE) {
436 return (StgClosure *)INTLIKE_CLOSURE((StgInt)w);
439 return copy_noscav(q,sizeofW(StgHeader)+1,stp);
445 return copy(q,sizeofW(StgHeader)+1,stp);
449 return copy(q,sizeofW(StgThunk)+1,stp);
454 #ifdef NO_PROMOTE_THUNKS
455 if (bd->gen_no == 0 &&
457 bd->step->no == generations[bd->gen_no].n_steps-1) {
461 return copy(q,sizeofW(StgThunk)+2,stp);
468 return copy(q,sizeofW(StgHeader)+2,stp);
471 return copy_noscav(q,sizeofW(StgHeader)+2,stp);
474 return copy(q,thunk_sizeW_fromITBL(info),stp);
479 case IND_OLDGEN_PERM:
482 return copy(q,sizeW_fromITBL(info),stp);
485 return copy(q,bco_sizeW((StgBCO *)q),stp);
488 case SE_CAF_BLACKHOLE:
491 return copyPart(q,BLACKHOLE_sizeW(),sizeofW(StgHeader),stp);
496 const StgInfoTable *info_ptr;
498 if (thunk_selector_depth > MAX_THUNK_SELECTOR_DEPTH) {
499 return copy(q,THUNK_SELECTOR_sizeW(),stp);
502 // stashed away for LDV profiling, see below
503 info_ptr = q->header.info;
505 p = eval_thunk_selector(info->layout.selector_offset,
509 return copy(q,THUNK_SELECTOR_sizeW(),stp);
512 // q is still BLACKHOLE'd.
513 thunk_selector_depth++;
515 thunk_selector_depth--;
518 // For the purposes of LDV profiling, we have destroyed
519 // the original selector thunk.
520 SET_INFO(q, info_ptr);
521 LDV_RECORD_DEAD_FILL_SLOP_DYNAMIC(q);
524 // Update the THUNK_SELECTOR with an indirection to the
525 // EVACUATED closure now at p. Why do this rather than
526 // upd_evacuee(q,p)? Because we have an invariant that an
527 // EVACUATED closure always points to an object in the
528 // same or an older generation (required by the short-cut
529 // test in the EVACUATED case, below).
530 SET_INFO(q, &stg_IND_info);
531 ((StgInd *)q)->indirectee = p;
533 // For the purposes of LDV profiling, we have created an
535 LDV_RECORD_CREATE(q);
543 // follow chains of indirections, don't evacuate them
544 q = ((StgInd*)q)->indirectee;
554 case CATCH_STM_FRAME:
555 case CATCH_RETRY_FRAME:
556 case ATOMICALLY_FRAME:
557 // shouldn't see these
558 barf("evacuate: stack frame at %p\n", q);
561 return copy(q,pap_sizeW((StgPAP*)q),stp);
564 return copy(q,ap_sizeW((StgAP*)q),stp);
567 return copy(q,ap_stack_sizeW((StgAP_STACK*)q),stp);
570 /* Already evacuated, just return the forwarding address.
571 * HOWEVER: if the requested destination generation (evac_gen) is
572 * older than the actual generation (because the object was
573 * already evacuated to a younger generation) then we have to
574 * set the failed_to_evac flag to indicate that we couldn't
575 * manage to promote the object to the desired generation.
578 * Optimisation: the check is fairly expensive, but we can often
579 * shortcut it if either the required generation is 0, or the
580 * current object (the EVACUATED) is in a high enough generation.
581 * We know that an EVACUATED always points to an object in the
582 * same or an older generation. stp is the lowest step that the
583 * current object would be evacuated to, so we only do the full
584 * check if stp is too low.
586 if (evac_gen > 0 && stp->gen_no < evac_gen) { // optimisation
587 StgClosure *p = ((StgEvacuated*)q)->evacuee;
588 if (HEAP_ALLOCED(p) && Bdescr((P_)p)->gen_no < evac_gen) {
589 failed_to_evac = rtsTrue;
590 TICK_GC_FAILED_PROMOTION();
593 return ((StgEvacuated*)q)->evacuee;
596 // just copy the block
597 return copy_noscav(q,arr_words_sizeW((StgArrWords *)q),stp);
599 case MUT_ARR_PTRS_CLEAN:
600 case MUT_ARR_PTRS_DIRTY:
601 case MUT_ARR_PTRS_FROZEN:
602 case MUT_ARR_PTRS_FROZEN0:
603 // just copy the block
604 return copy(q,mut_arr_ptrs_sizeW((StgMutArrPtrs *)q),stp);
608 StgTSO *tso = (StgTSO *)q;
610 /* Deal with redirected TSOs (a TSO that's had its stack enlarged).
612 if (tso->what_next == ThreadRelocated) {
613 q = (StgClosure *)tso->link;
617 /* To evacuate a small TSO, we need to relocate the update frame
624 new_tso = (StgTSO *)copyPart((StgClosure *)tso,
626 sizeofW(StgTSO), stp);
627 move_TSO(tso, new_tso);
628 for (p = tso->sp, q = new_tso->sp;
629 p < tso->stack+tso->stack_size;) {
633 return (StgClosure *)new_tso;
638 return copy(q,sizeofW(StgTRecHeader),stp);
640 case TVAR_WATCH_QUEUE:
641 return copy(q,sizeofW(StgTVarWatchQueue),stp);
644 return copy(q,sizeofW(StgTVar),stp);
647 return copy(q,sizeofW(StgTRecChunk),stp);
649 case ATOMIC_INVARIANT:
650 return copy(q,sizeofW(StgAtomicInvariant),stp);
652 case INVARIANT_CHECK_QUEUE:
653 return copy(q,sizeofW(StgInvariantCheckQueue),stp);
656 barf("evacuate: strange closure type %d", (int)(info->type));
662 /* -----------------------------------------------------------------------------
663 Evaluate a THUNK_SELECTOR if possible.
665 returns: NULL if we couldn't evaluate this THUNK_SELECTOR, or
666 a closure pointer if we evaluated it and this is the result. Note
667 that "evaluating" the THUNK_SELECTOR doesn't necessarily mean
668 reducing it to HNF, just that we have eliminated the selection.
669 The result might be another thunk, or even another THUNK_SELECTOR.
671 If the return value is non-NULL, the original selector thunk has
672 been BLACKHOLE'd, and should be updated with an indirection or a
673 forwarding pointer. If the return value is NULL, then the selector
677 ToDo: the treatment of THUNK_SELECTORS could be improved in the
678 following way (from a suggestion by Ian Lynagh):
680 We can have a chain like this:
684 |-----> sel_0 --> (a,b)
686 |-----> sel_0 --> ...
688 and the depth limit means we don't go all the way to the end of the
689 chain, which results in a space leak. This affects the recursive
690 call to evacuate() in the THUNK_SELECTOR case in evacuate(): *not*
691 the recursive call to eval_thunk_selector() in
692 eval_thunk_selector().
694 We could eliminate the depth bound in this case, in the following
697 - traverse the chain once to discover the *value* of the
698 THUNK_SELECTOR. Mark all THUNK_SELECTORS that we
699 visit on the way as having been visited already (somehow).
701 - in a second pass, traverse the chain again updating all
702 THUNK_SEELCTORS that we find on the way with indirections to
705 - if we encounter a "marked" THUNK_SELECTOR in a normal
706 evacuate(), we konw it can't be updated so just evac it.
708 Program that illustrates the problem:
711 foo (x:xs) = let (ys, zs) = foo xs
712 in if x >= 0 then (x:ys, zs) else (ys, x:zs)
714 main = bar [1..(100000000::Int)]
715 bar xs = (\(ys, zs) -> print ys >> print zs) (foo xs)
717 -------------------------------------------------------------------------- */
719 static inline rtsBool
720 is_to_space ( StgClosure *p )
724 bd = Bdescr((StgPtr)p);
725 if (HEAP_ALLOCED(p) &&
726 ((bd->flags & BF_EVACUATED)
727 || ((bd->flags & BF_COMPACTED) &&
728 is_marked((P_)p,bd)))) {
736 eval_thunk_selector( nat field, StgSelector * p )
739 const StgInfoTable *info_ptr;
740 StgClosure *selectee;
742 selectee = p->selectee;
744 // Save the real info pointer (NOTE: not the same as get_itbl()).
745 info_ptr = p->header.info;
747 // If the THUNK_SELECTOR is in a generation that we are not
748 // collecting, then bail out early. We won't be able to save any
749 // space in any case, and updating with an indirection is trickier
751 if (Bdescr((StgPtr)p)->gen_no > N) {
755 // BLACKHOLE the selector thunk, since it is now under evaluation.
756 // This is important to stop us going into an infinite loop if
757 // this selector thunk eventually refers to itself.
758 SET_INFO(p,&stg_BLACKHOLE_info);
762 // We don't want to end up in to-space, because this causes
763 // problems when the GC later tries to evacuate the result of
764 // eval_thunk_selector(). There are various ways this could
767 // 1. following an IND_STATIC
769 // 2. when the old generation is compacted, the mark phase updates
770 // from-space pointers to be to-space pointers, and we can't
771 // reliably tell which we're following (eg. from an IND_STATIC).
773 // 3. compacting GC again: if we're looking at a constructor in
774 // the compacted generation, it might point directly to objects
775 // in to-space. We must bale out here, otherwise doing the selection
776 // will result in a to-space pointer being returned.
778 // (1) is dealt with using a BF_EVACUATED test on the
779 // selectee. (2) and (3): we can tell if we're looking at an
780 // object in the compacted generation that might point to
781 // to-space objects by testing that (a) it is BF_COMPACTED, (b)
782 // the compacted generation is being collected, and (c) the
783 // object is marked. Only a marked object may have pointers that
784 // point to to-space objects, because that happens when
787 // The to-space test is now embodied in the in_to_space() inline
788 // function, as it is re-used below.
790 if (is_to_space(selectee)) {
794 info = get_itbl(selectee);
795 switch (info->type) {
803 case CONSTR_NOCAF_STATIC:
804 // check that the size is in range
805 ASSERT(field < (StgWord32)(info->layout.payload.ptrs +
806 info->layout.payload.nptrs));
808 // Select the right field from the constructor, and check
809 // that the result isn't in to-space. It might be in
810 // to-space if, for example, this constructor contains
811 // pointers to younger-gen objects (and is on the mut-once
816 q = selectee->payload[field];
817 if (is_to_space(q)) {
827 case IND_OLDGEN_PERM:
829 selectee = ((StgInd *)selectee)->indirectee;
833 // We don't follow pointers into to-space; the constructor
834 // has already been evacuated, so we won't save any space
835 // leaks by evaluating this selector thunk anyhow.
842 // check that we don't recurse too much, re-using the
843 // depth bound also used in evacuate().
844 if (thunk_selector_depth >= MAX_THUNK_SELECTOR_DEPTH) {
848 // we don't update THUNK_SELECTORS in the compacted
849 // generation, because compaction does not remove the INDs
850 // that result, this causes confusion later.
851 if (Bdescr((P_)selectee)->flags && BF_COMPACTED) {
855 thunk_selector_depth++;
857 val = eval_thunk_selector(info->layout.selector_offset,
858 (StgSelector *)selectee);
860 thunk_selector_depth--;
865 // We evaluated this selector thunk, so update it with
866 // an indirection. NOTE: we don't use UPD_IND here,
867 // because we are guaranteed that p is in a generation
868 // that we are collecting, and we never want to put the
869 // indirection on a mutable list.
871 // For the purposes of LDV profiling, we have destroyed
872 // the original selector thunk.
873 SET_INFO(p, info_ptr);
874 LDV_RECORD_DEAD_FILL_SLOP_DYNAMIC(selectee);
876 ((StgInd *)selectee)->indirectee = val;
877 SET_INFO(selectee,&stg_IND_info);
879 // For the purposes of LDV profiling, we have created an
881 LDV_RECORD_CREATE(selectee);
898 case SE_CAF_BLACKHOLE:
905 barf("eval_thunk_selector: strange selectee %d",
910 // We didn't manage to evaluate this thunk; restore the old info pointer
911 SET_INFO(p, info_ptr);
915 /* -----------------------------------------------------------------------------
916 move_TSO is called to update the TSO structure after it has been
917 moved from one place to another.
918 -------------------------------------------------------------------------- */
921 move_TSO (StgTSO *src, StgTSO *dest)
925 // relocate the stack pointer...
926 diff = (StgPtr)dest - (StgPtr)src; // In *words*
927 dest->sp = (StgPtr)dest->sp + diff;