1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team 1998-2006
5 * Generational garbage collector: evacuation functions
7 * ---------------------------------------------------------------------------*/
17 #include "LdvProfile.h"
19 /* Used to avoid long recursion due to selector thunks
21 lnat thunk_selector_depth = 0;
22 #define MAX_THUNK_SELECTOR_DEPTH 8
24 static StgClosure * eval_thunk_selector ( nat field, StgSelector * p );
27 upd_evacuee(StgClosure *p, StgClosure *dest)
29 // not true: (ToDo: perhaps it should be)
30 // ASSERT(Bdescr((P_)dest)->flags & BF_EVACUATED);
31 SET_INFO(p, &stg_EVACUATED_info);
32 ((StgEvacuated *)p)->evacuee = dest;
36 STATIC_INLINE StgClosure *
37 copy(StgClosure *src, nat size, step *stp)
46 TICK_GC_WORDS_COPIED(size);
47 /* Find out where we're going, using the handy "to" pointer in
48 * the step of the source object. If it turns out we need to
49 * evacuate to an older generation, adjust it here (see comment
52 if (stp->gen_no < evac_gen) {
53 if (eager_promotion) {
54 stp = &generations[evac_gen].steps[0];
56 failed_to_evac = rtsTrue;
60 /* chain a new block onto the to-space for the destination step if
63 if (stp->hp + size >= stp->hpLim) {
70 for (i = 0; i < size; i++) { // unroll for small i
73 upd_evacuee((StgClosure *)from,(StgClosure *)to);
76 // We store the size of the just evacuated object in the LDV word so that
77 // the profiler can guess the position of the next object later.
78 SET_EVACUAEE_FOR_LDV(from, size_org);
80 return (StgClosure *)to;
83 // Same as copy() above, except the object will be allocated in memory
84 // that will not be scavenged. Used for object that have no pointer
86 STATIC_INLINE StgClosure *
87 copy_noscav(StgClosure *src, nat size, step *stp)
96 TICK_GC_WORDS_COPIED(size);
97 /* Find out where we're going, using the handy "to" pointer in
98 * the step of the source object. If it turns out we need to
99 * evacuate to an older generation, adjust it here (see comment
102 if (stp->gen_no < evac_gen) {
103 if (eager_promotion) {
104 stp = &generations[evac_gen].steps[0];
106 failed_to_evac = rtsTrue;
110 /* chain a new block onto the to-space for the destination step if
113 if (stp->scavd_hp + size >= stp->scavd_hpLim) {
114 gc_alloc_scavd_block(stp);
119 stp->scavd_hp = to + size;
120 for (i = 0; i < size; i++) { // unroll for small i
123 upd_evacuee((StgClosure *)from,(StgClosure *)to);
126 // We store the size of the just evacuated object in the LDV word so that
127 // the profiler can guess the position of the next object later.
128 SET_EVACUAEE_FOR_LDV(from, size_org);
130 return (StgClosure *)to;
133 /* Special version of copy() for when we only want to copy the info
134 * pointer of an object, but reserve some padding after it. This is
135 * used to optimise evacuation of BLACKHOLEs.
140 copyPart(StgClosure *src, nat size_to_reserve, nat size_to_copy, step *stp)
145 nat size_to_copy_org = size_to_copy;
148 TICK_GC_WORDS_COPIED(size_to_copy);
149 if (stp->gen_no < evac_gen) {
150 if (eager_promotion) {
151 stp = &generations[evac_gen].steps[0];
153 failed_to_evac = rtsTrue;
157 if (stp->hp + size_to_reserve >= stp->hpLim) {
161 for(to = stp->hp, from = (P_)src; size_to_copy>0; --size_to_copy) {
166 stp->hp += size_to_reserve;
167 upd_evacuee(src,(StgClosure *)dest);
169 // We store the size of the just evacuated object in the LDV word so that
170 // the profiler can guess the position of the next object later.
171 // size_to_copy_org is wrong because the closure already occupies size_to_reserve
173 SET_EVACUAEE_FOR_LDV(src, size_to_reserve);
175 if (size_to_reserve - size_to_copy_org > 0)
176 LDV_FILL_SLOP(stp->hp - 1, (int)(size_to_reserve - size_to_copy_org));
178 return (StgClosure *)dest;
182 /* -----------------------------------------------------------------------------
183 Evacuate a large object
185 This just consists of removing the object from the (doubly-linked)
186 step->large_objects list, and linking it on to the (singly-linked)
187 step->new_large_objects list, from where it will be scavenged later.
189 Convention: bd->flags has BF_EVACUATED set for a large object
190 that has been evacuated, or unset otherwise.
191 -------------------------------------------------------------------------- */
195 evacuate_large(StgPtr p)
197 bdescr *bd = Bdescr(p);
200 // object must be at the beginning of the block (or be a ByteArray)
201 ASSERT(get_itbl((StgClosure *)p)->type == ARR_WORDS ||
202 (((W_)p & BLOCK_MASK) == 0));
204 // already evacuated?
205 if (bd->flags & BF_EVACUATED) {
206 /* Don't forget to set the failed_to_evac flag if we didn't get
207 * the desired destination (see comments in evacuate()).
209 if (bd->gen_no < evac_gen) {
210 failed_to_evac = rtsTrue;
211 TICK_GC_FAILED_PROMOTION();
217 // remove from large_object list
219 bd->u.back->link = bd->link;
220 } else { // first object in the list
221 stp->large_objects = bd->link;
224 bd->link->u.back = bd->u.back;
227 /* link it on to the evacuated large object list of the destination step
230 if (stp->gen_no < evac_gen) {
231 if (eager_promotion) {
232 stp = &generations[evac_gen].steps[0];
234 failed_to_evac = rtsTrue;
239 bd->gen_no = stp->gen_no;
240 bd->link = stp->new_large_objects;
241 stp->new_large_objects = bd;
242 bd->flags |= BF_EVACUATED;
245 /* -----------------------------------------------------------------------------
248 This is called (eventually) for every live object in the system.
250 The caller to evacuate specifies a desired generation in the
251 evac_gen global variable. The following conditions apply to
252 evacuating an object which resides in generation M when we're
253 collecting up to generation N
257 else evac to step->to
259 if M < evac_gen evac to evac_gen, step 0
261 if the object is already evacuated, then we check which generation
264 if M >= evac_gen do nothing
265 if M < evac_gen set failed_to_evac flag to indicate that we
266 didn't manage to evacuate this object into evac_gen.
271 evacuate() is the single most important function performance-wise
272 in the GC. Various things have been tried to speed it up, but as
273 far as I can tell the code generated by gcc 3.2 with -O2 is about
274 as good as it's going to get. We pass the argument to evacuate()
275 in a register using the 'regparm' attribute (see the prototype for
276 evacuate() near the top of this file).
278 Changing evacuate() to take an (StgClosure **) rather than
279 returning the new pointer seems attractive, because we can avoid
280 writing back the pointer when it hasn't changed (eg. for a static
281 object, or an object in a generation > N). However, I tried it and
282 it doesn't help. One reason is that the (StgClosure **) pointer
283 gets spilled to the stack inside evacuate(), resulting in far more
284 extra reads/writes than we save.
285 -------------------------------------------------------------------------- */
287 REGPARM1 StgClosure *
288 evacuate(StgClosure *q)
292 const StgInfoTable *info;
295 ASSERT(LOOKS_LIKE_CLOSURE_PTR(q));
297 if (!HEAP_ALLOCED(q)) {
299 if (!major_gc) return q;
302 switch (info->type) {
305 if (info->srt_bitmap != 0 &&
306 *THUNK_STATIC_LINK((StgClosure *)q) == NULL) {
307 *THUNK_STATIC_LINK((StgClosure *)q) = static_objects;
308 static_objects = (StgClosure *)q;
313 if (info->srt_bitmap != 0 &&
314 *FUN_STATIC_LINK((StgClosure *)q) == NULL) {
315 *FUN_STATIC_LINK((StgClosure *)q) = static_objects;
316 static_objects = (StgClosure *)q;
321 /* If q->saved_info != NULL, then it's a revertible CAF - it'll be
322 * on the CAF list, so don't do anything with it here (we'll
323 * scavenge it later).
325 if (((StgIndStatic *)q)->saved_info == NULL
326 && *IND_STATIC_LINK((StgClosure *)q) == NULL) {
327 *IND_STATIC_LINK((StgClosure *)q) = static_objects;
328 static_objects = (StgClosure *)q;
333 if (*STATIC_LINK(info,(StgClosure *)q) == NULL) {
334 *STATIC_LINK(info,(StgClosure *)q) = static_objects;
335 static_objects = (StgClosure *)q;
339 case CONSTR_NOCAF_STATIC:
340 /* no need to put these on the static linked list, they don't need
346 barf("evacuate(static): strange closure type %d", (int)(info->type));
352 if (bd->gen_no > N) {
353 /* Can't evacuate this object, because it's in a generation
354 * older than the ones we're collecting. Let's hope that it's
355 * in evac_gen or older, or we will have to arrange to track
356 * this pointer using the mutable list.
358 if (bd->gen_no < evac_gen) {
360 failed_to_evac = rtsTrue;
361 TICK_GC_FAILED_PROMOTION();
366 if ((bd->flags & (BF_LARGE | BF_COMPACTED | BF_EVACUATED)) != 0) {
368 /* pointer into to-space: just return it. This normally
369 * shouldn't happen, but alllowing it makes certain things
370 * slightly easier (eg. the mutable list can contain the same
371 * object twice, for example).
373 if (bd->flags & BF_EVACUATED) {
374 if (bd->gen_no < evac_gen) {
375 failed_to_evac = rtsTrue;
376 TICK_GC_FAILED_PROMOTION();
381 /* evacuate large objects by re-linking them onto a different list.
383 if (bd->flags & BF_LARGE) {
385 if (info->type == TSO &&
386 ((StgTSO *)q)->what_next == ThreadRelocated) {
387 q = (StgClosure *)((StgTSO *)q)->link;
390 evacuate_large((P_)q);
394 /* If the object is in a step that we're compacting, then we
395 * need to use an alternative evacuate procedure.
397 if (bd->flags & BF_COMPACTED) {
398 if (!is_marked((P_)q,bd)) {
400 if (mark_stack_full()) {
401 mark_stack_overflowed = rtsTrue;
404 push_mark_stack((P_)q);
414 switch (info->type) {
419 return copy(q,sizeW_fromITBL(info),stp);
423 StgWord w = (StgWord)q->payload[0];
424 if (q->header.info == Czh_con_info &&
425 // unsigned, so always true: (StgChar)w >= MIN_CHARLIKE &&
426 (StgChar)w <= MAX_CHARLIKE) {
427 return (StgClosure *)CHARLIKE_CLOSURE((StgChar)w);
429 if (q->header.info == Izh_con_info &&
430 (StgInt)w >= MIN_INTLIKE && (StgInt)w <= MAX_INTLIKE) {
431 return (StgClosure *)INTLIKE_CLOSURE((StgInt)w);
434 return copy_noscav(q,sizeofW(StgHeader)+1,stp);
440 return copy(q,sizeofW(StgHeader)+1,stp);
444 return copy(q,sizeofW(StgThunk)+1,stp);
449 #ifdef NO_PROMOTE_THUNKS
450 if (bd->gen_no == 0 &&
452 bd->step->no == generations[bd->gen_no].n_steps-1) {
456 return copy(q,sizeofW(StgThunk)+2,stp);
463 return copy(q,sizeofW(StgHeader)+2,stp);
466 return copy_noscav(q,sizeofW(StgHeader)+2,stp);
469 return copy(q,thunk_sizeW_fromITBL(info),stp);
474 case IND_OLDGEN_PERM:
477 return copy(q,sizeW_fromITBL(info),stp);
480 return copy(q,bco_sizeW((StgBCO *)q),stp);
483 case SE_CAF_BLACKHOLE:
486 return copyPart(q,BLACKHOLE_sizeW(),sizeofW(StgHeader),stp);
491 const StgInfoTable *info_ptr;
493 if (thunk_selector_depth > MAX_THUNK_SELECTOR_DEPTH) {
494 return copy(q,THUNK_SELECTOR_sizeW(),stp);
497 // stashed away for LDV profiling, see below
498 info_ptr = q->header.info;
500 p = eval_thunk_selector(info->layout.selector_offset,
504 return copy(q,THUNK_SELECTOR_sizeW(),stp);
507 // q is still BLACKHOLE'd.
508 thunk_selector_depth++;
510 thunk_selector_depth--;
513 // For the purposes of LDV profiling, we have destroyed
514 // the original selector thunk.
515 SET_INFO(q, info_ptr);
516 LDV_RECORD_DEAD_FILL_SLOP_DYNAMIC(q);
519 // Update the THUNK_SELECTOR with an indirection to the
520 // EVACUATED closure now at p. Why do this rather than
521 // upd_evacuee(q,p)? Because we have an invariant that an
522 // EVACUATED closure always points to an object in the
523 // same or an older generation (required by the short-cut
524 // test in the EVACUATED case, below).
525 SET_INFO(q, &stg_IND_info);
526 ((StgInd *)q)->indirectee = p;
528 // For the purposes of LDV profiling, we have created an
530 LDV_RECORD_CREATE(q);
538 // follow chains of indirections, don't evacuate them
539 q = ((StgInd*)q)->indirectee;
551 case CATCH_STM_FRAME:
552 case CATCH_RETRY_FRAME:
553 case ATOMICALLY_FRAME:
554 // shouldn't see these
555 barf("evacuate: stack frame at %p\n", q);
558 return copy(q,pap_sizeW((StgPAP*)q),stp);
561 return copy(q,ap_sizeW((StgAP*)q),stp);
564 return copy(q,ap_stack_sizeW((StgAP_STACK*)q),stp);
567 /* Already evacuated, just return the forwarding address.
568 * HOWEVER: if the requested destination generation (evac_gen) is
569 * older than the actual generation (because the object was
570 * already evacuated to a younger generation) then we have to
571 * set the failed_to_evac flag to indicate that we couldn't
572 * manage to promote the object to the desired generation.
575 * Optimisation: the check is fairly expensive, but we can often
576 * shortcut it if either the required generation is 0, or the
577 * current object (the EVACUATED) is in a high enough generation.
578 * We know that an EVACUATED always points to an object in the
579 * same or an older generation. stp is the lowest step that the
580 * current object would be evacuated to, so we only do the full
581 * check if stp is too low.
583 if (evac_gen > 0 && stp->gen_no < evac_gen) { // optimisation
584 StgClosure *p = ((StgEvacuated*)q)->evacuee;
585 if (HEAP_ALLOCED(p) && Bdescr((P_)p)->gen_no < evac_gen) {
586 failed_to_evac = rtsTrue;
587 TICK_GC_FAILED_PROMOTION();
590 return ((StgEvacuated*)q)->evacuee;
593 // just copy the block
594 return copy_noscav(q,arr_words_sizeW((StgArrWords *)q),stp);
596 case MUT_ARR_PTRS_CLEAN:
597 case MUT_ARR_PTRS_DIRTY:
598 case MUT_ARR_PTRS_FROZEN:
599 case MUT_ARR_PTRS_FROZEN0:
600 // just copy the block
601 return copy(q,mut_arr_ptrs_sizeW((StgMutArrPtrs *)q),stp);
605 StgTSO *tso = (StgTSO *)q;
607 /* Deal with redirected TSOs (a TSO that's had its stack enlarged).
609 if (tso->what_next == ThreadRelocated) {
610 q = (StgClosure *)tso->link;
614 /* To evacuate a small TSO, we need to relocate the update frame
621 new_tso = (StgTSO *)copyPart((StgClosure *)tso,
623 sizeofW(StgTSO), stp);
624 move_TSO(tso, new_tso);
625 for (p = tso->sp, q = new_tso->sp;
626 p < tso->stack+tso->stack_size;) {
630 return (StgClosure *)new_tso;
635 return copy(q,sizeofW(StgTRecHeader),stp);
637 case TVAR_WATCH_QUEUE:
638 return copy(q,sizeofW(StgTVarWatchQueue),stp);
641 return copy(q,sizeofW(StgTVar),stp);
644 return copy(q,sizeofW(StgTRecChunk),stp);
646 case ATOMIC_INVARIANT:
647 return copy(q,sizeofW(StgAtomicInvariant),stp);
649 case INVARIANT_CHECK_QUEUE:
650 return copy(q,sizeofW(StgInvariantCheckQueue),stp);
653 barf("evacuate: strange closure type %d", (int)(info->type));
659 /* -----------------------------------------------------------------------------
660 Evaluate a THUNK_SELECTOR if possible.
662 returns: NULL if we couldn't evaluate this THUNK_SELECTOR, or
663 a closure pointer if we evaluated it and this is the result. Note
664 that "evaluating" the THUNK_SELECTOR doesn't necessarily mean
665 reducing it to HNF, just that we have eliminated the selection.
666 The result might be another thunk, or even another THUNK_SELECTOR.
668 If the return value is non-NULL, the original selector thunk has
669 been BLACKHOLE'd, and should be updated with an indirection or a
670 forwarding pointer. If the return value is NULL, then the selector
674 ToDo: the treatment of THUNK_SELECTORS could be improved in the
675 following way (from a suggestion by Ian Lynagh):
677 We can have a chain like this:
681 |-----> sel_0 --> (a,b)
683 |-----> sel_0 --> ...
685 and the depth limit means we don't go all the way to the end of the
686 chain, which results in a space leak. This affects the recursive
687 call to evacuate() in the THUNK_SELECTOR case in evacuate(): *not*
688 the recursive call to eval_thunk_selector() in
689 eval_thunk_selector().
691 We could eliminate the depth bound in this case, in the following
694 - traverse the chain once to discover the *value* of the
695 THUNK_SELECTOR. Mark all THUNK_SELECTORS that we
696 visit on the way as having been visited already (somehow).
698 - in a second pass, traverse the chain again updating all
699 THUNK_SEELCTORS that we find on the way with indirections to
702 - if we encounter a "marked" THUNK_SELECTOR in a normal
703 evacuate(), we konw it can't be updated so just evac it.
705 Program that illustrates the problem:
708 foo (x:xs) = let (ys, zs) = foo xs
709 in if x >= 0 then (x:ys, zs) else (ys, x:zs)
711 main = bar [1..(100000000::Int)]
712 bar xs = (\(ys, zs) -> print ys >> print zs) (foo xs)
714 -------------------------------------------------------------------------- */
716 static inline rtsBool
717 is_to_space ( StgClosure *p )
721 bd = Bdescr((StgPtr)p);
722 if (HEAP_ALLOCED(p) &&
723 ((bd->flags & BF_EVACUATED)
724 || ((bd->flags & BF_COMPACTED) &&
725 is_marked((P_)p,bd)))) {
733 eval_thunk_selector( nat field, StgSelector * p )
736 const StgInfoTable *info_ptr;
737 StgClosure *selectee;
739 selectee = p->selectee;
741 // Save the real info pointer (NOTE: not the same as get_itbl()).
742 info_ptr = p->header.info;
744 // If the THUNK_SELECTOR is in a generation that we are not
745 // collecting, then bail out early. We won't be able to save any
746 // space in any case, and updating with an indirection is trickier
748 if (Bdescr((StgPtr)p)->gen_no > N) {
752 // BLACKHOLE the selector thunk, since it is now under evaluation.
753 // This is important to stop us going into an infinite loop if
754 // this selector thunk eventually refers to itself.
755 SET_INFO(p,&stg_BLACKHOLE_info);
759 // We don't want to end up in to-space, because this causes
760 // problems when the GC later tries to evacuate the result of
761 // eval_thunk_selector(). There are various ways this could
764 // 1. following an IND_STATIC
766 // 2. when the old generation is compacted, the mark phase updates
767 // from-space pointers to be to-space pointers, and we can't
768 // reliably tell which we're following (eg. from an IND_STATIC).
770 // 3. compacting GC again: if we're looking at a constructor in
771 // the compacted generation, it might point directly to objects
772 // in to-space. We must bale out here, otherwise doing the selection
773 // will result in a to-space pointer being returned.
775 // (1) is dealt with using a BF_EVACUATED test on the
776 // selectee. (2) and (3): we can tell if we're looking at an
777 // object in the compacted generation that might point to
778 // to-space objects by testing that (a) it is BF_COMPACTED, (b)
779 // the compacted generation is being collected, and (c) the
780 // object is marked. Only a marked object may have pointers that
781 // point to to-space objects, because that happens when
784 // The to-space test is now embodied in the in_to_space() inline
785 // function, as it is re-used below.
787 if (is_to_space(selectee)) {
791 info = get_itbl(selectee);
792 switch (info->type) {
800 case CONSTR_NOCAF_STATIC:
801 // check that the size is in range
802 ASSERT(field < (StgWord32)(info->layout.payload.ptrs +
803 info->layout.payload.nptrs));
805 // Select the right field from the constructor, and check
806 // that the result isn't in to-space. It might be in
807 // to-space if, for example, this constructor contains
808 // pointers to younger-gen objects (and is on the mut-once
813 q = selectee->payload[field];
814 if (is_to_space(q)) {
824 case IND_OLDGEN_PERM:
826 selectee = ((StgInd *)selectee)->indirectee;
830 // We don't follow pointers into to-space; the constructor
831 // has already been evacuated, so we won't save any space
832 // leaks by evaluating this selector thunk anyhow.
839 // check that we don't recurse too much, re-using the
840 // depth bound also used in evacuate().
841 if (thunk_selector_depth >= MAX_THUNK_SELECTOR_DEPTH) {
844 thunk_selector_depth++;
846 val = eval_thunk_selector(info->layout.selector_offset,
847 (StgSelector *)selectee);
849 thunk_selector_depth--;
854 // We evaluated this selector thunk, so update it with
855 // an indirection. NOTE: we don't use UPD_IND here,
856 // because we are guaranteed that p is in a generation
857 // that we are collecting, and we never want to put the
858 // indirection on a mutable list.
860 // For the purposes of LDV profiling, we have destroyed
861 // the original selector thunk.
862 SET_INFO(p, info_ptr);
863 LDV_RECORD_DEAD_FILL_SLOP_DYNAMIC(selectee);
865 ((StgInd *)selectee)->indirectee = val;
866 SET_INFO(selectee,&stg_IND_info);
868 // For the purposes of LDV profiling, we have created an
870 LDV_RECORD_CREATE(selectee);
887 case SE_CAF_BLACKHOLE:
894 barf("eval_thunk_selector: strange selectee %d",
899 // We didn't manage to evaluate this thunk; restore the old info pointer
900 SET_INFO(p, info_ptr);
904 /* -----------------------------------------------------------------------------
905 move_TSO is called to update the TSO structure after it has been
906 moved from one place to another.
907 -------------------------------------------------------------------------- */
910 move_TSO (StgTSO *src, StgTSO *dest)
914 // relocate the stack pointer...
915 diff = (StgPtr)dest - (StgPtr)src; // In *words*
916 dest->sp = (StgPtr)dest->sp + diff;