X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=rts%2Fsm%2FEvac.c;h=fb9f4c49b2decb7a3e1833d1d12ae357a4ce2857;hb=698364afaf2f346227910c0cf8d4f1929cdc56ef;hp=dda565967516acd91086bcdbdfae6399758ca241;hpb=9ff76535edb25ab7434284adddb5c64708ecb547;p=ghc-hetmet.git diff --git a/rts/sm/Evac.c b/rts/sm/Evac.c index dda5659..fb9f4c4 100644 --- a/rts/sm/Evac.c +++ b/rts/sm/Evac.c @@ -23,167 +23,251 @@ /* Used to avoid long recursion due to selector thunks */ -lnat thunk_selector_depth = 0; -#define MAX_THUNK_SELECTOR_DEPTH 8 +#define MAX_THUNK_SELECTOR_DEPTH 16 -static StgClosure * eval_thunk_selector ( nat field, StgSelector * p ); +static StgClosure * eval_thunk_selector (StgSelector * p, rtsBool); -STATIC_INLINE void -upd_evacuee(StgClosure *p, StgClosure *dest) +STATIC_INLINE StgPtr +alloc_for_copy (nat size, step *stp) { - // not true: (ToDo: perhaps it should be) - // ASSERT(Bdescr((P_)dest)->flags & BF_EVACUATED); - SET_INFO(p, &stg_EVACUATED_info); - ((StgEvacuated *)p)->evacuee = dest; + StgPtr to; + step_workspace *ws; + bdescr *bd; + + /* Find out where we're going, using the handy "to" pointer in + * the step of the source object. If it turns out we need to + * evacuate to an older generation, adjust it here (see comment + * by evacuate()). + */ + if (stp->gen_no < gct->evac_gen) { + if (gct->eager_promotion) { + stp = &generations[gct->evac_gen].steps[0]; + } else { + gct->failed_to_evac = rtsTrue; + } + } + + ws = &gct->steps[stp->gen_no][stp->no]; + + /* chain a new block onto the to-space for the destination step if + * necessary. + */ + bd = ws->todo_bd; + to = bd->free; + if (to + size >= bd->start + BLOCK_SIZE_W) { + bd = gc_alloc_todo_block(ws); + to = bd->free; + } + bd->free = to + size; + + return to; } + +STATIC_INLINE StgPtr +alloc_for_copy_noscav (nat size, step *stp) +{ + StgPtr to; + step_workspace *ws; + bdescr *bd; + /* Find out where we're going, using the handy "to" pointer in + * the step of the source object. If it turns out we need to + * evacuate to an older generation, adjust it here (see comment + * by evacuate()). + */ + if (stp->gen_no < gct->evac_gen) { + if (gct->eager_promotion) { + stp = &generations[gct->evac_gen].steps[0]; + } else { + gct->failed_to_evac = rtsTrue; + } + } + + ws = &gct->steps[stp->gen_no][stp->no]; + + /* chain a new block onto the to-space for the destination step if + * necessary. + */ + bd = ws->scavd_list; + to = bd->free; + if (to + size >= bd->start + BLOCK_SIZE_W) { + bd = gc_alloc_scavd_block(ws); + to = bd->free; + } + bd->free = to + size; + return to; +} + STATIC_INLINE StgClosure * -copy(StgClosure *src, nat size, step *stp) +copy_tag(StgClosure *src, nat size, step *stp,StgWord tag) { StgPtr to, from; nat i; -#ifdef PROFILING - // @LDV profiling - nat size_org = size; + StgWord info; + +#ifdef THREADED_RTS + do { + info = xchg((StgPtr)&src->header.info, (W_)&stg_WHITEHOLE_info); + // so.. what is it? + } while (info == (W_)&stg_WHITEHOLE_info); + if (info == (W_)&stg_EVACUATED_info) { + src->header.info = (const StgInfoTable *)info; + return evacuate(src); // does the failed_to_evac stuff + } +#else + info = (W_)src->header.info; + src->header.info = &stg_EVACUATED_info; #endif - TICK_GC_WORDS_COPIED(size); - /* Find out where we're going, using the handy "to" pointer in - * the step of the source object. If it turns out we need to - * evacuate to an older generation, adjust it here (see comment - * by evacuate()). - */ - if (stp->gen_no < evac_gen) { - if (eager_promotion) { - stp = &generations[evac_gen].steps[0]; - } else { - failed_to_evac = rtsTrue; - } - } + to = alloc_for_copy(size,stp); + + TICK_GC_WORDS_COPIED(size); - /* chain a new block onto the to-space for the destination step if - * necessary. - */ - if (stp->hp + size >= stp->hpLim) { - gc_alloc_block(stp); - } + from = (StgPtr)src; + to[0] = info; + for (i = 1; i < size; i++) { // unroll for small i + to[i] = from[i]; + } + + // retag pointer before updating EVACUATE closure and returning + to = (StgPtr)TAG_CLOSURE(tag,(StgClosure*)to); - to = stp->hp; - from = (StgPtr)src; - stp->hp = to + size; - for (i = 0; i < size; i++) { // unroll for small i - to[i] = from[i]; - } - upd_evacuee((StgClosure *)from,(StgClosure *)to); +// if (to+size+2 < bd->start + BLOCK_SIZE_W) { +// __builtin_prefetch(to + size + 2, 1); +// } + + ((StgEvacuated*)from)->evacuee = (StgClosure *)to; +#ifdef THREADED_RTS + write_barrier(); + ((StgEvacuated*)from)->header.info = &stg_EVACUATED_info; +#endif #ifdef PROFILING - // We store the size of the just evacuated object in the LDV word so that - // the profiler can guess the position of the next object later. - SET_EVACUAEE_FOR_LDV(from, size_org); + // We store the size of the just evacuated object in the LDV word so that + // the profiler can guess the position of the next object later. + SET_EVACUAEE_FOR_LDV(from, size); #endif - return (StgClosure *)to; + return (StgClosure *)to; } + // Same as copy() above, except the object will be allocated in memory // that will not be scavenged. Used for object that have no pointer // fields. STATIC_INLINE StgClosure * -copy_noscav(StgClosure *src, nat size, step *stp) +copy_noscav_tag(StgClosure *src, nat size, step *stp, StgWord tag) { - StgPtr to, from; - nat i; -#ifdef PROFILING - // @LDV profiling - nat size_org = size; + StgPtr to, from; + nat i; + StgWord info; + +#ifdef THREADED_RTS + do { + info = xchg((StgPtr)&src->header.info, (W_)&stg_WHITEHOLE_info); + } while (info == (W_)&stg_WHITEHOLE_info); + if (info == (W_)&stg_EVACUATED_info) { + src->header.info = (const StgInfoTable *)info; + return evacuate(src); // does the failed_to_evac stuff + } +#else + info = (W_)src->header.info; + src->header.info = &stg_EVACUATED_info; #endif + + to = alloc_for_copy_noscav(size,stp); - TICK_GC_WORDS_COPIED(size); - /* Find out where we're going, using the handy "to" pointer in - * the step of the source object. If it turns out we need to - * evacuate to an older generation, adjust it here (see comment - * by evacuate()). - */ - if (stp->gen_no < evac_gen) { - if (eager_promotion) { - stp = &generations[evac_gen].steps[0]; - } else { - failed_to_evac = rtsTrue; - } - } - - /* chain a new block onto the to-space for the destination step if - * necessary. - */ - if (stp->scavd_hp + size >= stp->scavd_hpLim) { - gc_alloc_scavd_block(stp); - } + TICK_GC_WORDS_COPIED(size); + + from = (StgPtr)src; + to[0] = info; + for (i = 1; i < size; i++) { // unroll for small i + to[i] = from[i]; + } - to = stp->scavd_hp; - from = (StgPtr)src; - stp->scavd_hp = to + size; - for (i = 0; i < size; i++) { // unroll for small i - to[i] = from[i]; - } - upd_evacuee((StgClosure *)from,(StgClosure *)to); + // retag pointer before updating EVACUATE closure and returning + to = (StgPtr)TAG_CLOSURE(tag,(StgClosure*)to); + ((StgEvacuated*)from)->evacuee = (StgClosure *)to; +#ifdef THREADED_RTS + write_barrier(); + ((StgEvacuated*)from)->header.info = &stg_EVACUATED_info; +#endif + #ifdef PROFILING - // We store the size of the just evacuated object in the LDV word so that - // the profiler can guess the position of the next object later. - SET_EVACUAEE_FOR_LDV(from, size_org); + // We store the size of the just evacuated object in the LDV word so that + // the profiler can guess the position of the next object later. + SET_EVACUAEE_FOR_LDV(from, size); #endif - return (StgClosure *)to; + return (StgClosure *)to; } + /* Special version of copy() for when we only want to copy the info * pointer of an object, but reserve some padding after it. This is * used to optimise evacuation of BLACKHOLEs. */ - - static StgClosure * copyPart(StgClosure *src, nat size_to_reserve, nat size_to_copy, step *stp) { - P_ dest, to, from; -#ifdef PROFILING - // @LDV profiling - nat size_to_copy_org = size_to_copy; + StgPtr to, from; + nat i; + StgWord info; + +#ifdef THREADED_RTS + do { + info = xchg((StgPtr)&src->header.info, (W_)&stg_WHITEHOLE_info); + } while (info == (W_)&stg_WHITEHOLE_info); + if (info == (W_)&stg_EVACUATED_info) { + src->header.info = (const StgInfoTable *)info; + return evacuate(src); // does the failed_to_evac stuff + } +#else + info = (W_)src->header.info; + src->header.info = &stg_EVACUATED_info; #endif + + to = alloc_for_copy(size_to_reserve, stp); - TICK_GC_WORDS_COPIED(size_to_copy); - if (stp->gen_no < evac_gen) { - if (eager_promotion) { - stp = &generations[evac_gen].steps[0]; - } else { - failed_to_evac = rtsTrue; - } - } - - if (stp->hp + size_to_reserve >= stp->hpLim) { - gc_alloc_block(stp); - } + TICK_GC_WORDS_COPIED(size_to_copy); - for(to = stp->hp, from = (P_)src; size_to_copy>0; --size_to_copy) { - *to++ = *from++; - } - - dest = stp->hp; - stp->hp += size_to_reserve; - upd_evacuee(src,(StgClosure *)dest); + from = (StgPtr)src; + to[0] = info; + for (i = 1; i < size_to_copy; i++) { // unroll for small i + to[i] = from[i]; + } + + ((StgEvacuated*)from)->evacuee = (StgClosure *)to; +#ifdef THREADED_RTS + write_barrier(); + ((StgEvacuated*)from)->header.info = &stg_EVACUATED_info; +#endif + #ifdef PROFILING - // We store the size of the just evacuated object in the LDV word so that - // the profiler can guess the position of the next object later. - // size_to_copy_org is wrong because the closure already occupies size_to_reserve - // words. - SET_EVACUAEE_FOR_LDV(src, size_to_reserve); - // fill the slop - if (size_to_reserve - size_to_copy_org > 0) - LDV_FILL_SLOP(stp->hp - 1, (int)(size_to_reserve - size_to_copy_org)); + // We store the size of the just evacuated object in the LDV word so that + // the profiler can guess the position of the next object later. + SET_EVACUAEE_FOR_LDV(from, size_to_reserve); + // fill the slop + if (size_to_reserve - size_to_copy > 0) + LDV_FILL_SLOP(to + size_to_copy - 1, (int)(size_to_reserve - size_to_copy)); #endif - return (StgClosure *)dest; + return (StgClosure *)to; } +/* Copy wrappers that don't tag the closure after copying */ +STATIC_INLINE StgClosure * +copy(StgClosure *src, nat size, step *stp) +{ + return copy_tag(src,size,stp,0); +} + +STATIC_INLINE StgClosure * +copy_noscav(StgClosure *src, nat size, step *stp) +{ + return copy_noscav_tag(src,size,stp,0); +} + /* ----------------------------------------------------------------------------- Evacuate a large object @@ -201,6 +285,7 @@ evacuate_large(StgPtr p) { bdescr *bd = Bdescr(p); step *stp; + step_workspace *ws; // object must be at the beginning of the block (or be a ByteArray) ASSERT(get_itbl((StgClosure *)p)->type == ARR_WORDS || @@ -208,17 +293,19 @@ evacuate_large(StgPtr p) // already evacuated? if (bd->flags & BF_EVACUATED) { - /* Don't forget to set the failed_to_evac flag if we didn't get + /* Don't forget to set the gct->failed_to_evac flag if we didn't get * the desired destination (see comments in evacuate()). */ - if (bd->gen_no < evac_gen) { - failed_to_evac = rtsTrue; + if (bd->gen_no < gct->evac_gen) { + gct->failed_to_evac = rtsTrue; TICK_GC_FAILED_PROMOTION(); } return; } stp = bd->step; + + ACQUIRE_SPIN_LOCK(&stp->sync_large_objects); // remove from large_object list if (bd->u.back) { bd->u.back->link = bd->link; @@ -228,22 +315,24 @@ evacuate_large(StgPtr p) if (bd->link) { bd->link->u.back = bd->u.back; } + RELEASE_SPIN_LOCK(&stp->sync_large_objects); /* link it on to the evacuated large object list of the destination step */ stp = bd->step->to; - if (stp->gen_no < evac_gen) { - if (eager_promotion) { - stp = &generations[evac_gen].steps[0]; + if (stp->gen_no < gct->evac_gen) { + if (gct->eager_promotion) { + stp = &generations[gct->evac_gen].steps[0]; } else { - failed_to_evac = rtsTrue; + gct->failed_to_evac = rtsTrue; } } + ws = &gct->steps[stp->gen_no][stp->no]; bd->step = stp; bd->gen_no = stp->gen_no; - bd->link = stp->new_large_objects; - stp->new_large_objects = bd; + bd->link = ws->todo_large_objects; + ws->todo_large_objects = bd; bd->flags |= BF_EVACUATED; } @@ -253,22 +342,22 @@ evacuate_large(StgPtr p) This is called (eventually) for every live object in the system. The caller to evacuate specifies a desired generation in the - evac_gen global variable. The following conditions apply to + gct->evac_gen thread-lock variable. The following conditions apply to evacuating an object which resides in generation M when we're collecting up to generation N - if M >= evac_gen + if M >= gct->evac_gen if M > N do nothing else evac to step->to - if M < evac_gen evac to evac_gen, step 0 + if M < gct->evac_gen evac to gct->evac_gen, step 0 if the object is already evacuated, then we check which generation it now resides in. - if M >= evac_gen do nothing - if M < evac_gen set failed_to_evac flag to indicate that we - didn't manage to evacuate this object into evac_gen. + if M >= gct->evac_gen do nothing + if M < gct->evac_gen set gct->failed_to_evac flag to indicate that we + didn't manage to evacuate this object into gct->evac_gen. OPTIMISATION NOTES: @@ -295,30 +384,43 @@ evacuate(StgClosure *q) bdescr *bd = NULL; step *stp; const StgInfoTable *info; + StgWord tag; loop: + /* The tag and the pointer are split, to be merged after evacing */ + tag = GET_CLOSURE_TAG(q); + q = UNTAG_CLOSURE(q); + ASSERT(LOOKS_LIKE_CLOSURE_PTR(q)); if (!HEAP_ALLOCED(q)) { - if (!major_gc) return q; + if (!major_gc) return TAG_CLOSURE(tag,q); info = get_itbl(q); switch (info->type) { case THUNK_STATIC: - if (info->srt_bitmap != 0 && + if (info->srt_bitmap != 0 && *THUNK_STATIC_LINK((StgClosure *)q) == NULL) { - *THUNK_STATIC_LINK((StgClosure *)q) = static_objects; - static_objects = (StgClosure *)q; + ACQUIRE_SPIN_LOCK(&static_objects_sync); + if (*THUNK_STATIC_LINK((StgClosure *)q) == NULL) { + *THUNK_STATIC_LINK((StgClosure *)q) = static_objects; + static_objects = (StgClosure *)q; + } + RELEASE_SPIN_LOCK(&static_objects_sync); } return q; case FUN_STATIC: - if (info->srt_bitmap != 0 && + if (info->srt_bitmap != 0 && *FUN_STATIC_LINK((StgClosure *)q) == NULL) { - *FUN_STATIC_LINK((StgClosure *)q) = static_objects; - static_objects = (StgClosure *)q; + ACQUIRE_SPIN_LOCK(&static_objects_sync); + if (*FUN_STATIC_LINK((StgClosure *)q) == NULL) { + *FUN_STATIC_LINK((StgClosure *)q) = static_objects; + static_objects = (StgClosure *)q; + } + RELEASE_SPIN_LOCK(&static_objects_sync); } return q; @@ -327,25 +429,35 @@ loop: * on the CAF list, so don't do anything with it here (we'll * scavenge it later). */ - if (((StgIndStatic *)q)->saved_info == NULL - && *IND_STATIC_LINK((StgClosure *)q) == NULL) { - *IND_STATIC_LINK((StgClosure *)q) = static_objects; - static_objects = (StgClosure *)q; + if (((StgIndStatic *)q)->saved_info == NULL) { + ACQUIRE_SPIN_LOCK(&static_objects_sync); + if (*IND_STATIC_LINK((StgClosure *)q) == NULL) { + *IND_STATIC_LINK((StgClosure *)q) = static_objects; + static_objects = (StgClosure *)q; + } + RELEASE_SPIN_LOCK(&static_objects_sync); } return q; case CONSTR_STATIC: if (*STATIC_LINK(info,(StgClosure *)q) == NULL) { - *STATIC_LINK(info,(StgClosure *)q) = static_objects; - static_objects = (StgClosure *)q; + ACQUIRE_SPIN_LOCK(&static_objects_sync); + // re-test, after acquiring lock + if (*STATIC_LINK(info,(StgClosure *)q) == NULL) { + *STATIC_LINK(info,(StgClosure *)q) = static_objects; + static_objects = (StgClosure *)q; + } + RELEASE_SPIN_LOCK(&static_objects_sync); + /* I am assuming that static_objects pointers are not + * written to other objects, and thus, no need to retag. */ } - return q; + return TAG_CLOSURE(tag,q); case CONSTR_NOCAF_STATIC: /* no need to put these on the static linked list, they don't need * to be scavenged. */ - return q; + return TAG_CLOSURE(tag,q); default: barf("evacuate(static): strange closure type %d", (int)(info->type)); @@ -357,15 +469,15 @@ loop: if (bd->gen_no > N) { /* Can't evacuate this object, because it's in a generation * older than the ones we're collecting. Let's hope that it's - * in evac_gen or older, or we will have to arrange to track + * in gct->evac_gen or older, or we will have to arrange to track * this pointer using the mutable list. */ - if (bd->gen_no < evac_gen) { + if (bd->gen_no < gct->evac_gen) { // nope - failed_to_evac = rtsTrue; + gct->failed_to_evac = rtsTrue; TICK_GC_FAILED_PROMOTION(); } - return q; + return TAG_CLOSURE(tag,q); } if ((bd->flags & (BF_LARGE | BF_COMPACTED | BF_EVACUATED)) != 0) { @@ -376,11 +488,11 @@ loop: * object twice, for example). */ if (bd->flags & BF_EVACUATED) { - if (bd->gen_no < evac_gen) { - failed_to_evac = rtsTrue; + if (bd->gen_no < gct->evac_gen) { + gct->failed_to_evac = rtsTrue; TICK_GC_FAILED_PROMOTION(); } - return q; + return TAG_CLOSURE(tag,q); } /* evacuate large objects by re-linking them onto a different list. @@ -393,7 +505,7 @@ loop: goto loop; } evacuate_large((P_)q); - return q; + return TAG_CLOSURE(tag,q); } /* If the object is in a step that we're compacting, then we @@ -408,7 +520,7 @@ loop: } push_mark_stack((P_)q); } - return q; + return TAG_CLOSURE(tag,q); } } @@ -418,9 +530,13 @@ loop: switch (info->type) { + case WHITEHOLE: + goto loop; + case MUT_VAR_CLEAN: case MUT_VAR_DIRTY: - case MVAR: + case MVAR_CLEAN: + case MVAR_DIRTY: return copy(q,sizeW_fromITBL(info),stp); case CONSTR_0_1: @@ -429,20 +545,24 @@ loop: if (q->header.info == Czh_con_info && // unsigned, so always true: (StgChar)w >= MIN_CHARLIKE && (StgChar)w <= MAX_CHARLIKE) { - return (StgClosure *)CHARLIKE_CLOSURE((StgChar)w); + return TAG_CLOSURE(tag, + (StgClosure *)CHARLIKE_CLOSURE((StgChar)w) + ); } if (q->header.info == Izh_con_info && (StgInt)w >= MIN_INTLIKE && (StgInt)w <= MAX_INTLIKE) { - return (StgClosure *)INTLIKE_CLOSURE((StgInt)w); + return TAG_CLOSURE(tag, + (StgClosure *)INTLIKE_CLOSURE((StgInt)w) + ); } // else - return copy_noscav(q,sizeofW(StgHeader)+1,stp); + return copy_noscav_tag(q,sizeofW(StgHeader)+1,stp,tag); } case FUN_0_1: case FUN_1_0: case CONSTR_1_0: - return copy(q,sizeofW(StgHeader)+1,stp); + return copy_tag(q,sizeofW(StgHeader)+1,stp,tag); case THUNK_1_0: case THUNK_0_1: @@ -462,27 +582,27 @@ loop: case FUN_1_1: case FUN_2_0: + case FUN_0_2: case CONSTR_1_1: case CONSTR_2_0: - case FUN_0_2: - return copy(q,sizeofW(StgHeader)+2,stp); + return copy_tag(q,sizeofW(StgHeader)+2,stp,tag); case CONSTR_0_2: - return copy_noscav(q,sizeofW(StgHeader)+2,stp); + return copy_noscav_tag(q,sizeofW(StgHeader)+2,stp,tag); case THUNK: return copy(q,thunk_sizeW_fromITBL(info),stp); case FUN: - case CONSTR: case IND_PERM: case IND_OLDGEN_PERM: case WEAK: case STABLE_NAME: - return copy(q,sizeW_fromITBL(info),stp); + case CONSTR: + return copy_tag(q,sizeW_fromITBL(info),stp,tag); case BCO: - return copy(q,bco_sizeW((StgBCO *)q),stp); + return copy(q,bco_sizeW((StgBCO *)q),stp); case CAF_BLACKHOLE: case SE_CAF_BLACKHOLE: @@ -491,52 +611,7 @@ loop: return copyPart(q,BLACKHOLE_sizeW(),sizeofW(StgHeader),stp); case THUNK_SELECTOR: - { - StgClosure *p; - const StgInfoTable *info_ptr; - - if (thunk_selector_depth > MAX_THUNK_SELECTOR_DEPTH) { - return copy(q,THUNK_SELECTOR_sizeW(),stp); - } - - // stashed away for LDV profiling, see below - info_ptr = q->header.info; - - p = eval_thunk_selector(info->layout.selector_offset, - (StgSelector *)q); - - if (p == NULL) { - return copy(q,THUNK_SELECTOR_sizeW(),stp); - } else { - StgClosure *val; - // q is still BLACKHOLE'd. - thunk_selector_depth++; - val = evacuate(p); - thunk_selector_depth--; - -#ifdef PROFILING - // For the purposes of LDV profiling, we have destroyed - // the original selector thunk. - SET_INFO(q, info_ptr); - LDV_RECORD_DEAD_FILL_SLOP_DYNAMIC(q); -#endif - - // Update the THUNK_SELECTOR with an indirection to the - // EVACUATED closure now at p. Why do this rather than - // upd_evacuee(q,p)? Because we have an invariant that an - // EVACUATED closure always points to an object in the - // same or an older generation (required by the short-cut - // test in the EVACUATED case, below). - SET_INFO(q, &stg_IND_info); - ((StgInd *)q)->indirectee = p; - - // For the purposes of LDV profiling, we have created an - // indirection. - LDV_RECORD_CREATE(q); - - return val; - } - } + return eval_thunk_selector((StgSelector *)q, rtsTrue); case IND: case IND_OLDGEN: @@ -568,10 +643,10 @@ loop: case EVACUATED: /* Already evacuated, just return the forwarding address. - * HOWEVER: if the requested destination generation (evac_gen) is + * HOWEVER: if the requested destination generation (gct->evac_gen) is * older than the actual generation (because the object was * already evacuated to a younger generation) then we have to - * set the failed_to_evac flag to indicate that we couldn't + * set the gct->failed_to_evac flag to indicate that we couldn't * manage to promote the object to the desired generation. */ /* @@ -583,10 +658,10 @@ loop: * current object would be evacuated to, so we only do the full * check if stp is too low. */ - if (evac_gen > 0 && stp->gen_no < evac_gen) { // optimisation + if (gct->evac_gen > 0 && stp->gen_no < gct->evac_gen) { // optimisation StgClosure *p = ((StgEvacuated*)q)->evacuee; - if (HEAP_ALLOCED(p) && Bdescr((P_)p)->gen_no < evac_gen) { - failed_to_evac = rtsTrue; + if (HEAP_ALLOCED(p) && Bdescr((P_)p)->gen_no < gct->evac_gen) { + gct->failed_to_evac = rtsTrue; TICK_GC_FAILED_PROMOTION(); } } @@ -659,97 +734,96 @@ loop: barf("evacuate"); } -/* ----------------------------------------------------------------------------- - Evaluate a THUNK_SELECTOR if possible. - - returns: NULL if we couldn't evaluate this THUNK_SELECTOR, or - a closure pointer if we evaluated it and this is the result. Note - that "evaluating" the THUNK_SELECTOR doesn't necessarily mean - reducing it to HNF, just that we have eliminated the selection. - The result might be another thunk, or even another THUNK_SELECTOR. - - If the return value is non-NULL, the original selector thunk has - been BLACKHOLE'd, and should be updated with an indirection or a - forwarding pointer. If the return value is NULL, then the selector - thunk is unchanged. - - *** - ToDo: the treatment of THUNK_SELECTORS could be improved in the - following way (from a suggestion by Ian Lynagh): - - We can have a chain like this: - - sel_0 --> (a,b) - | - |-----> sel_0 --> (a,b) - | - |-----> sel_0 --> ... - - and the depth limit means we don't go all the way to the end of the - chain, which results in a space leak. This affects the recursive - call to evacuate() in the THUNK_SELECTOR case in evacuate(): *not* - the recursive call to eval_thunk_selector() in - eval_thunk_selector(). - - We could eliminate the depth bound in this case, in the following - way: - - - traverse the chain once to discover the *value* of the - THUNK_SELECTOR. Mark all THUNK_SELECTORS that we - visit on the way as having been visited already (somehow). - - - in a second pass, traverse the chain again updating all - THUNK_SEELCTORS that we find on the way with indirections to - the value. - - - if we encounter a "marked" THUNK_SELECTOR in a normal - evacuate(), we konw it can't be updated so just evac it. +static void +unchain_thunk_selectors(StgSelector *p, StgClosure *val) +{ + StgSelector *prev; - Program that illustrates the problem: + prev = NULL; + while (p) + { + ASSERT(p->header.info == &stg_BLACKHOLE_info); + prev = (StgSelector*)((StgClosure *)p)->payload[0]; + + // Update the THUNK_SELECTOR with an indirection to the + // EVACUATED closure now at p. Why do this rather than + // upd_evacuee(q,p)? Because we have an invariant that an + // EVACUATED closure always points to an object in the + // same or an older generation (required by the short-cut + // test in the EVACUATED case, below). + SET_INFO(p, &stg_IND_info); + ((StgInd *)p)->indirectee = val; + + // For the purposes of LDV profiling, we have created an + // indirection. + LDV_RECORD_CREATE(p); + + p = prev; + } +} - foo [] = ([], []) - foo (x:xs) = let (ys, zs) = foo xs - in if x >= 0 then (x:ys, zs) else (ys, x:zs) +/* ----------------------------------------------------------------------------- + Evaluate a THUNK_SELECTOR if possible. - main = bar [1..(100000000::Int)] - bar xs = (\(ys, zs) -> print ys >> print zs) (foo xs) + p points to a THUNK_SELECTOR that we want to evaluate. The + result of "evaluating" it will be evacuated and a pointer to the + to-space closure will be returned. + If the THUNK_SELECTOR could not be evaluated (its selectee is still + a THUNK, for example), then the THUNK_SELECTOR itself will be + evacuated. -------------------------------------------------------------------------- */ -static inline rtsBool -is_to_space ( StgClosure *p ) -{ - bdescr *bd; - - bd = Bdescr((StgPtr)p); - if (HEAP_ALLOCED(p) && - ((bd->flags & BF_EVACUATED) - || ((bd->flags & BF_COMPACTED) && - is_marked((P_)p,bd)))) { - return rtsTrue; - } else { - return rtsFalse; - } -} - static StgClosure * -eval_thunk_selector( nat field, StgSelector * p ) +eval_thunk_selector (StgSelector * p, rtsBool evac) { + nat field; StgInfoTable *info; const StgInfoTable *info_ptr; StgClosure *selectee; + StgSelector *prev_thunk_selector; + bdescr *bd; + StgClosure *val; - selectee = p->selectee; + prev_thunk_selector = NULL; + // this is a chain of THUNK_SELECTORs that we are going to update + // to point to the value of the current THUNK_SELECTOR. Each + // closure on the chain is a BLACKHOLE, and points to the next in the + // chain with payload[0]. + +selector_chain: + + // The selectee might be a constructor closure, + // so we untag the pointer. + selectee = UNTAG_CLOSURE(p->selectee); // Save the real info pointer (NOTE: not the same as get_itbl()). info_ptr = p->header.info; + field = get_itbl(p)->layout.selector_offset; - // If the THUNK_SELECTOR is in a generation that we are not - // collecting, then bail out early. We won't be able to save any - // space in any case, and updating with an indirection is trickier - // in an old gen. - if (Bdescr((StgPtr)p)->gen_no > N) { - return NULL; + bd = Bdescr((StgPtr)p); + if (HEAP_ALLOCED(p)) { + // If the THUNK_SELECTOR is in to-space or in a generation that we + // are not collecting, then bale out early. We won't be able to + // save any space in any case, and updating with an indirection is + // trickier in a non-collected gen: we would have to update the + // mutable list. + if ((bd->gen_no > N) || (bd->flags & BF_EVACUATED)) { + unchain_thunk_selectors(prev_thunk_selector, (StgClosure *)p); + return (StgClosure *)p; + } + // we don't update THUNK_SELECTORS in the compacted + // generation, because compaction does not remove the INDs + // that result, this causes confusion later + // (scavenge_mark_stack doesn't deal with IND). BEWARE! This + // bit is very tricky to get right. If you make changes + // around here, test by compiling stage 3 with +RTS -c -RTS. + if (bd->flags & BF_COMPACTED) { + // must call evacuate() to mark this closure if evac==rtsTrue + if (evac) p = (StgSelector *)evacuate((StgClosure *)p); + unchain_thunk_selectors(prev_thunk_selector, (StgClosure *)p); + return (StgClosure *)p; + } } // BLACKHOLE the selector thunk, since it is now under evaluation. @@ -758,38 +832,12 @@ eval_thunk_selector( nat field, StgSelector * p ) SET_INFO(p,&stg_BLACKHOLE_info); selector_loop: - - // We don't want to end up in to-space, because this causes - // problems when the GC later tries to evacuate the result of - // eval_thunk_selector(). There are various ways this could - // happen: - // - // 1. following an IND_STATIC - // - // 2. when the old generation is compacted, the mark phase updates - // from-space pointers to be to-space pointers, and we can't - // reliably tell which we're following (eg. from an IND_STATIC). - // - // 3. compacting GC again: if we're looking at a constructor in - // the compacted generation, it might point directly to objects - // in to-space. We must bale out here, otherwise doing the selection - // will result in a to-space pointer being returned. - // - // (1) is dealt with using a BF_EVACUATED test on the - // selectee. (2) and (3): we can tell if we're looking at an - // object in the compacted generation that might point to - // to-space objects by testing that (a) it is BF_COMPACTED, (b) - // the compacted generation is being collected, and (c) the - // object is marked. Only a marked object may have pointers that - // point to to-space objects, because that happens when - // scavenging. - // - // The to-space test is now embodied in the in_to_space() inline - // function, as it is re-used below. - // - if (is_to_space(selectee)) { - goto bale_out; - } + // selectee now points to the closure that we're trying to select + // a field from. It may or may not be in to-space: we try not to + // end up in to-space, but it's impractical to avoid it in + // general. The compacting GC scatters to-space pointers in + // from-space during marking, for example. We rely on the property + // that evacuate() doesn't mind if it gets passed a to-space pointer. info = get_itbl(selectee); switch (info->type) { @@ -801,88 +849,93 @@ selector_loop: case CONSTR_0_2: case CONSTR_STATIC: case CONSTR_NOCAF_STATIC: - // check that the size is in range - ASSERT(field < (StgWord32)(info->layout.payload.ptrs + - info->layout.payload.nptrs)); + { + // check that the size is in range + ASSERT(field < (StgWord32)(info->layout.payload.ptrs + + info->layout.payload.nptrs)); - // Select the right field from the constructor, and check - // that the result isn't in to-space. It might be in - // to-space if, for example, this constructor contains - // pointers to younger-gen objects (and is on the mut-once - // list). - // - { - StgClosure *q; - q = selectee->payload[field]; - if (is_to_space(q)) { - goto bale_out; - } else { - return q; - } - } + // Select the right field from the constructor + val = selectee->payload[field]; + +#ifdef PROFILING + // For the purposes of LDV profiling, we have destroyed + // the original selector thunk, p. + SET_INFO(p, info_ptr); + LDV_RECORD_DEAD_FILL_SLOP_DYNAMIC((StgClosure *)p); + SET_INFO(p, &stg_BLACKHOLE_info); +#endif + + // the closure in val is now the "value" of the + // THUNK_SELECTOR in p. However, val may itself be a + // THUNK_SELECTOR, in which case we want to continue + // evaluating until we find the real value, and then + // update the whole chain to point to the value. + val_loop: + info = get_itbl(UNTAG_CLOSURE(val)); + switch (info->type) { + case IND: + case IND_PERM: + case IND_OLDGEN: + case IND_OLDGEN_PERM: + case IND_STATIC: + val = ((StgInd *)val)->indirectee; + goto val_loop; + case THUNK_SELECTOR: + ((StgClosure*)p)->payload[0] = (StgClosure *)prev_thunk_selector; + prev_thunk_selector = p; + p = (StgSelector*)val; + goto selector_chain; + default: + ((StgClosure*)p)->payload[0] = (StgClosure *)prev_thunk_selector; + prev_thunk_selector = p; + + if (evac) val = evacuate(val); + // evacuate() cannot recurse through + // eval_thunk_selector(), because we know val is not + // a THUNK_SELECTOR. + unchain_thunk_selectors(prev_thunk_selector, val); + return val; + } + } case IND: case IND_PERM: case IND_OLDGEN: case IND_OLDGEN_PERM: case IND_STATIC: - selectee = ((StgInd *)selectee)->indirectee; + // Again, we might need to untag a constructor. + selectee = UNTAG_CLOSURE( ((StgInd *)selectee)->indirectee ); goto selector_loop; case EVACUATED: // We don't follow pointers into to-space; the constructor // has already been evacuated, so we won't save any space // leaks by evaluating this selector thunk anyhow. - break; + goto bale_out; case THUNK_SELECTOR: { StgClosure *val; - // check that we don't recurse too much, re-using the - // depth bound also used in evacuate(). - if (thunk_selector_depth >= MAX_THUNK_SELECTOR_DEPTH) { - break; + // recursively evaluate this selector. We don't want to + // recurse indefinitely, so we impose a depth bound. + if (gct->thunk_selector_depth >= MAX_THUNK_SELECTOR_DEPTH) { + goto bale_out; } - // we don't update THUNK_SELECTORS in the compacted - // generation, because compaction does not remove the INDs - // that result, this causes confusion later. - if (Bdescr((P_)selectee)->flags && BF_COMPACTED) { - break; - } - - thunk_selector_depth++; - - val = eval_thunk_selector(info->layout.selector_offset, - (StgSelector *)selectee); - - thunk_selector_depth--; - - if (val == NULL) { - break; - } else { - // We evaluated this selector thunk, so update it with - // an indirection. NOTE: we don't use UPD_IND here, - // because we are guaranteed that p is in a generation - // that we are collecting, and we never want to put the - // indirection on a mutable list. -#ifdef PROFILING - // For the purposes of LDV profiling, we have destroyed - // the original selector thunk. - SET_INFO(p, info_ptr); - LDV_RECORD_DEAD_FILL_SLOP_DYNAMIC(selectee); -#endif - ((StgInd *)selectee)->indirectee = val; - SET_INFO(selectee,&stg_IND_info); + gct->thunk_selector_depth++; + // rtsFalse says "don't evacuate the result". It will, + // however, update any THUNK_SELECTORs that are evaluated + // along the way. + val = eval_thunk_selector((StgSelector *)selectee, rtsFalse); + gct->thunk_selector_depth--; - // For the purposes of LDV profiling, we have created an - // indirection. - LDV_RECORD_CREATE(selectee); + // did we actually manage to evaluate it? + if (val == selectee) goto bale_out; - selectee = val; - goto selector_loop; - } + // Of course this pointer might be tagged... + selectee = UNTAG_CLOSURE(val); + goto selector_loop; } case AP: @@ -899,7 +952,7 @@ selector_loop: case SE_BLACKHOLE: case BLACKHOLE: // not evaluated yet - break; + goto bale_out; default: barf("eval_thunk_selector: strange selectee %d", @@ -907,9 +960,16 @@ selector_loop: } bale_out: - // We didn't manage to evaluate this thunk; restore the old info pointer + // We didn't manage to evaluate this thunk; restore the old info + // pointer. But don't forget: we still need to evacuate the thunk itself. SET_INFO(p, info_ptr); - return NULL; + if (evac) { + val = copy((StgClosure *)p,THUNK_SELECTOR_sizeW(),bd->step->to); + } else { + val = (StgClosure *)p; + } + unchain_thunk_selectors(prev_thunk_selector, val); + return val; } /* -----------------------------------------------------------------------------