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)
295 const StgInfoTable *info;
298 ASSERT(LOOKS_LIKE_CLOSURE_PTR(q));
300 if (!HEAP_ALLOCED(q)) {
302 if (!major_gc) return q;
305 switch (info->type) {
308 if (info->srt_bitmap != 0 &&
309 *THUNK_STATIC_LINK((StgClosure *)q) == NULL) {
310 *THUNK_STATIC_LINK((StgClosure *)q) = static_objects;
311 static_objects = (StgClosure *)q;
316 if (info->srt_bitmap != 0 &&
317 *FUN_STATIC_LINK((StgClosure *)q) == NULL) {
318 *FUN_STATIC_LINK((StgClosure *)q) = static_objects;
319 static_objects = (StgClosure *)q;
324 /* If q->saved_info != NULL, then it's a revertible CAF - it'll be
325 * on the CAF list, so don't do anything with it here (we'll
326 * scavenge it later).
328 if (((StgIndStatic *)q)->saved_info == NULL
329 && *IND_STATIC_LINK((StgClosure *)q) == NULL) {
330 *IND_STATIC_LINK((StgClosure *)q) = static_objects;
331 static_objects = (StgClosure *)q;
336 if (*STATIC_LINK(info,(StgClosure *)q) == NULL) {
337 *STATIC_LINK(info,(StgClosure *)q) = static_objects;
338 static_objects = (StgClosure *)q;
342 case CONSTR_NOCAF_STATIC:
343 /* no need to put these on the static linked list, they don't need
349 barf("evacuate(static): strange closure type %d", (int)(info->type));
355 if (bd->gen_no > N) {
356 /* Can't evacuate this object, because it's in a generation
357 * older than the ones we're collecting. Let's hope that it's
358 * in evac_gen or older, or we will have to arrange to track
359 * this pointer using the mutable list.
361 if (bd->gen_no < evac_gen) {
363 failed_to_evac = rtsTrue;
364 TICK_GC_FAILED_PROMOTION();
369 if ((bd->flags & (BF_LARGE | BF_COMPACTED | BF_EVACUATED)) != 0) {
371 /* pointer into to-space: just return it. This normally
372 * shouldn't happen, but alllowing it makes certain things
373 * slightly easier (eg. the mutable list can contain the same
374 * object twice, for example).
376 if (bd->flags & BF_EVACUATED) {
377 if (bd->gen_no < evac_gen) {
378 failed_to_evac = rtsTrue;
379 TICK_GC_FAILED_PROMOTION();
384 /* evacuate large objects by re-linking them onto a different list.
386 if (bd->flags & BF_LARGE) {
388 if (info->type == TSO &&
389 ((StgTSO *)q)->what_next == ThreadRelocated) {
390 q = (StgClosure *)((StgTSO *)q)->link;
393 evacuate_large((P_)q);
397 /* If the object is in a step that we're compacting, then we
398 * need to use an alternative evacuate procedure.
400 if (bd->flags & BF_COMPACTED) {
401 if (!is_marked((P_)q,bd)) {
403 if (mark_stack_full()) {
404 mark_stack_overflowed = rtsTrue;
407 push_mark_stack((P_)q);
417 switch (info->type) {
422 return copy(q,sizeW_fromITBL(info),stp);
426 StgWord w = (StgWord)q->payload[0];
427 if (q->header.info == Czh_con_info &&
428 // unsigned, so always true: (StgChar)w >= MIN_CHARLIKE &&
429 (StgChar)w <= MAX_CHARLIKE) {
430 return (StgClosure *)CHARLIKE_CLOSURE((StgChar)w);
432 if (q->header.info == Izh_con_info &&
433 (StgInt)w >= MIN_INTLIKE && (StgInt)w <= MAX_INTLIKE) {
434 return (StgClosure *)INTLIKE_CLOSURE((StgInt)w);
437 return copy_noscav(q,sizeofW(StgHeader)+1,stp);
443 return copy(q,sizeofW(StgHeader)+1,stp);
447 return copy(q,sizeofW(StgThunk)+1,stp);
452 #ifdef NO_PROMOTE_THUNKS
453 if (bd->gen_no == 0 &&
455 bd->step->no == generations[bd->gen_no].n_steps-1) {
459 return copy(q,sizeofW(StgThunk)+2,stp);
466 return copy(q,sizeofW(StgHeader)+2,stp);
469 return copy_noscav(q,sizeofW(StgHeader)+2,stp);
472 return copy(q,thunk_sizeW_fromITBL(info),stp);
477 case IND_OLDGEN_PERM:
480 return copy(q,sizeW_fromITBL(info),stp);
483 return copy(q,bco_sizeW((StgBCO *)q),stp);
486 case SE_CAF_BLACKHOLE:
489 return copyPart(q,BLACKHOLE_sizeW(),sizeofW(StgHeader),stp);
494 const StgInfoTable *info_ptr;
496 if (thunk_selector_depth > MAX_THUNK_SELECTOR_DEPTH) {
497 return copy(q,THUNK_SELECTOR_sizeW(),stp);
500 // stashed away for LDV profiling, see below
501 info_ptr = q->header.info;
503 p = eval_thunk_selector(info->layout.selector_offset,
507 return copy(q,THUNK_SELECTOR_sizeW(),stp);
510 // q is still BLACKHOLE'd.
511 thunk_selector_depth++;
513 thunk_selector_depth--;
516 // For the purposes of LDV profiling, we have destroyed
517 // the original selector thunk.
518 SET_INFO(q, info_ptr);
519 LDV_RECORD_DEAD_FILL_SLOP_DYNAMIC(q);
522 // Update the THUNK_SELECTOR with an indirection to the
523 // EVACUATED closure now at p. Why do this rather than
524 // upd_evacuee(q,p)? Because we have an invariant that an
525 // EVACUATED closure always points to an object in the
526 // same or an older generation (required by the short-cut
527 // test in the EVACUATED case, below).
528 SET_INFO(q, &stg_IND_info);
529 ((StgInd *)q)->indirectee = p;
531 // For the purposes of LDV profiling, we have created an
533 LDV_RECORD_CREATE(q);
541 // follow chains of indirections, don't evacuate them
542 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;
640 //StgInfoTable *rip = get_closure_info(q, &size, &ptrs, &nonptrs, &vhs, str);
641 to = copy(q,BLACKHOLE_sizeW(),stp);
642 //ToDo: derive size etc from reverted IP
643 //to = copy(q,size,stp);
644 debugTrace(DEBUG_gc, "evacuate: RBH %p (%s) to %p (%s)",
645 q, info_type(q), to, info_type(to));
650 ASSERT(sizeofW(StgBlockedFetch) >= MIN_PAYLOD_SIZE);
651 to = copy(q,sizeofW(StgBlockedFetch),stp);
652 debugTrace(DEBUG_gc, "evacuate: %p (%s) to %p (%s)",
653 q, info_type(q), to, info_type(to));
660 ASSERT(sizeofW(StgBlockedFetch) >= MIN_PAYLOAD_SIZE);
661 to = copy(q,sizeofW(StgFetchMe),stp);
662 debugTrace(DEBUG_gc, "evacuate: %p (%s) to %p (%s)",
663 q, info_type(q), to, info_type(to)));
667 ASSERT(sizeofW(StgBlockedFetch) >= MIN_PAYLOAD_SIZE);
668 to = copy(q,sizeofW(StgFetchMeBlockingQueue),stp);
669 debugTrace(DEBUG_gc, "evacuate: %p (%s) to %p (%s)",
670 q, info_type(q), to, info_type(to)));
675 return copy(q,sizeofW(StgTRecHeader),stp);
677 case TVAR_WATCH_QUEUE:
678 return copy(q,sizeofW(StgTVarWatchQueue),stp);
681 return copy(q,sizeofW(StgTVar),stp);
684 return copy(q,sizeofW(StgTRecChunk),stp);
686 case ATOMIC_INVARIANT:
687 return copy(q,sizeofW(StgAtomicInvariant),stp);
689 case INVARIANT_CHECK_QUEUE:
690 return copy(q,sizeofW(StgInvariantCheckQueue),stp);
693 barf("evacuate: strange closure type %d", (int)(info->type));
699 /* -----------------------------------------------------------------------------
700 Evaluate a THUNK_SELECTOR if possible.
702 returns: NULL if we couldn't evaluate this THUNK_SELECTOR, or
703 a closure pointer if we evaluated it and this is the result. Note
704 that "evaluating" the THUNK_SELECTOR doesn't necessarily mean
705 reducing it to HNF, just that we have eliminated the selection.
706 The result might be another thunk, or even another THUNK_SELECTOR.
708 If the return value is non-NULL, the original selector thunk has
709 been BLACKHOLE'd, and should be updated with an indirection or a
710 forwarding pointer. If the return value is NULL, then the selector
714 ToDo: the treatment of THUNK_SELECTORS could be improved in the
715 following way (from a suggestion by Ian Lynagh):
717 We can have a chain like this:
721 |-----> sel_0 --> (a,b)
723 |-----> sel_0 --> ...
725 and the depth limit means we don't go all the way to the end of the
726 chain, which results in a space leak. This affects the recursive
727 call to evacuate() in the THUNK_SELECTOR case in evacuate(): *not*
728 the recursive call to eval_thunk_selector() in
729 eval_thunk_selector().
731 We could eliminate the depth bound in this case, in the following
734 - traverse the chain once to discover the *value* of the
735 THUNK_SELECTOR. Mark all THUNK_SELECTORS that we
736 visit on the way as having been visited already (somehow).
738 - in a second pass, traverse the chain again updating all
739 THUNK_SEELCTORS that we find on the way with indirections to
742 - if we encounter a "marked" THUNK_SELECTOR in a normal
743 evacuate(), we konw it can't be updated so just evac it.
745 Program that illustrates the problem:
748 foo (x:xs) = let (ys, zs) = foo xs
749 in if x >= 0 then (x:ys, zs) else (ys, x:zs)
751 main = bar [1..(100000000::Int)]
752 bar xs = (\(ys, zs) -> print ys >> print zs) (foo xs)
754 -------------------------------------------------------------------------- */
756 static inline rtsBool
757 is_to_space ( StgClosure *p )
761 bd = Bdescr((StgPtr)p);
762 if (HEAP_ALLOCED(p) &&
763 ((bd->flags & BF_EVACUATED)
764 || ((bd->flags & BF_COMPACTED) &&
765 is_marked((P_)p,bd)))) {
773 eval_thunk_selector( nat field, StgSelector * p )
776 const StgInfoTable *info_ptr;
777 StgClosure *selectee;
779 selectee = p->selectee;
781 // Save the real info pointer (NOTE: not the same as get_itbl()).
782 info_ptr = p->header.info;
784 // If the THUNK_SELECTOR is in a generation that we are not
785 // collecting, then bail out early. We won't be able to save any
786 // space in any case, and updating with an indirection is trickier
788 if (Bdescr((StgPtr)p)->gen_no > N) {
792 // BLACKHOLE the selector thunk, since it is now under evaluation.
793 // This is important to stop us going into an infinite loop if
794 // this selector thunk eventually refers to itself.
795 SET_INFO(p,&stg_BLACKHOLE_info);
799 // We don't want to end up in to-space, because this causes
800 // problems when the GC later tries to evacuate the result of
801 // eval_thunk_selector(). There are various ways this could
804 // 1. following an IND_STATIC
806 // 2. when the old generation is compacted, the mark phase updates
807 // from-space pointers to be to-space pointers, and we can't
808 // reliably tell which we're following (eg. from an IND_STATIC).
810 // 3. compacting GC again: if we're looking at a constructor in
811 // the compacted generation, it might point directly to objects
812 // in to-space. We must bale out here, otherwise doing the selection
813 // will result in a to-space pointer being returned.
815 // (1) is dealt with using a BF_EVACUATED test on the
816 // selectee. (2) and (3): we can tell if we're looking at an
817 // object in the compacted generation that might point to
818 // to-space objects by testing that (a) it is BF_COMPACTED, (b)
819 // the compacted generation is being collected, and (c) the
820 // object is marked. Only a marked object may have pointers that
821 // point to to-space objects, because that happens when
824 // The to-space test is now embodied in the in_to_space() inline
825 // function, as it is re-used below.
827 if (is_to_space(selectee)) {
831 info = get_itbl(selectee);
832 switch (info->type) {
840 case CONSTR_NOCAF_STATIC:
841 // check that the size is in range
842 ASSERT(field < (StgWord32)(info->layout.payload.ptrs +
843 info->layout.payload.nptrs));
845 // Select the right field from the constructor, and check
846 // that the result isn't in to-space. It might be in
847 // to-space if, for example, this constructor contains
848 // pointers to younger-gen objects (and is on the mut-once
853 q = selectee->payload[field];
854 if (is_to_space(q)) {
864 case IND_OLDGEN_PERM:
866 selectee = ((StgInd *)selectee)->indirectee;
870 // We don't follow pointers into to-space; the constructor
871 // has already been evacuated, so we won't save any space
872 // leaks by evaluating this selector thunk anyhow.
879 // check that we don't recurse too much, re-using the
880 // depth bound also used in evacuate().
881 if (thunk_selector_depth >= MAX_THUNK_SELECTOR_DEPTH) {
884 thunk_selector_depth++;
886 val = eval_thunk_selector(info->layout.selector_offset,
887 (StgSelector *)selectee);
889 thunk_selector_depth--;
894 // We evaluated this selector thunk, so update it with
895 // an indirection. NOTE: we don't use UPD_IND here,
896 // because we are guaranteed that p is in a generation
897 // that we are collecting, and we never want to put the
898 // indirection on a mutable list.
900 // For the purposes of LDV profiling, we have destroyed
901 // the original selector thunk.
902 SET_INFO(p, info_ptr);
903 LDV_RECORD_DEAD_FILL_SLOP_DYNAMIC(selectee);
905 ((StgInd *)selectee)->indirectee = val;
906 SET_INFO(selectee,&stg_IND_info);
908 // For the purposes of LDV profiling, we have created an
910 LDV_RECORD_CREATE(selectee);
927 case SE_CAF_BLACKHOLE:
943 barf("eval_thunk_selector: strange selectee %d",
948 // We didn't manage to evaluate this thunk; restore the old info pointer
949 SET_INFO(p, info_ptr);
953 /* -----------------------------------------------------------------------------
954 move_TSO is called to update the TSO structure after it has been
955 moved from one place to another.
956 -------------------------------------------------------------------------- */
959 move_TSO (StgTSO *src, StgTSO *dest)
963 // relocate the stack pointer...
964 diff = (StgPtr)dest - (StgPtr)src; // In *words*
965 dest->sp = (StgPtr)dest->sp + diff;