1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team 1998-2006
5 * Generational garbage collector: scavenging 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 * ---------------------------------------------------------------------------*/
25 #include "LdvProfile.h"
28 static void scavenge_stack (StgPtr p, StgPtr stack_end);
30 static void scavenge_large_bitmap (StgPtr p,
31 StgLargeBitmap *large_bitmap,
35 /* Similar to scavenge_large_bitmap(), but we don't write back the
36 * pointers we get back from evacuate().
39 scavenge_large_srt_bitmap( StgLargeSRT *large_srt )
46 bitmap = large_srt->l.bitmap[b];
47 size = (nat)large_srt->l.size;
48 p = (StgClosure **)large_srt->srt;
49 for (i = 0; i < size; ) {
50 if ((bitmap & 1) != 0) {
55 if (i % BITS_IN(W_) == 0) {
57 bitmap = large_srt->l.bitmap[b];
64 /* evacuate the SRT. If srt_bitmap is zero, then there isn't an
65 * srt field in the info table. That's ok, because we'll
66 * never dereference it.
69 scavenge_srt (StgClosure **srt, nat srt_bitmap)
77 if (bitmap == (StgHalfWord)(-1)) {
78 scavenge_large_srt_bitmap( (StgLargeSRT *)srt );
83 if ((bitmap & 1) != 0) {
84 #if defined(__PIC__) && defined(mingw32_TARGET_OS)
85 // Special-case to handle references to closures hiding out in DLLs, since
86 // double indirections required to get at those. The code generator knows
87 // which is which when generating the SRT, so it stores the (indirect)
88 // reference to the DLL closure in the table by first adding one to it.
89 // We check for this here, and undo the addition before evacuating it.
91 // If the SRT entry hasn't got bit 0 set, the SRT entry points to a
92 // closure that's fixed at link-time, and no extra magic is required.
93 if ( (unsigned long)(*srt) & 0x1 ) {
94 evacuate(stgCast(StgClosure**,(stgCast(unsigned long, *srt) & ~0x1)));
103 bitmap = bitmap >> 1;
109 scavenge_thunk_srt(const StgInfoTable *info)
111 StgThunkInfoTable *thunk_info;
113 if (!major_gc) return;
115 thunk_info = itbl_to_thunk_itbl(info);
116 scavenge_srt((StgClosure **)GET_SRT(thunk_info), thunk_info->i.srt_bitmap);
120 scavenge_fun_srt(const StgInfoTable *info)
122 StgFunInfoTable *fun_info;
124 if (!major_gc) return;
126 fun_info = itbl_to_fun_itbl(info);
127 scavenge_srt((StgClosure **)GET_FUN_SRT(fun_info), fun_info->i.srt_bitmap);
130 /* -----------------------------------------------------------------------------
132 -------------------------------------------------------------------------- */
135 scavengeTSO (StgTSO *tso)
137 if ( tso->why_blocked == BlockedOnMVar
138 || tso->why_blocked == BlockedOnBlackHole
139 || tso->why_blocked == BlockedOnException
141 evacuate(&tso->block_info.closure);
143 evacuate((StgClosure **)&tso->blocked_exceptions);
145 // We don't always chase the link field: TSOs on the blackhole
146 // queue are not automatically alive, so the link field is a
147 // "weak" pointer in that case.
148 if (tso->why_blocked != BlockedOnBlackHole) {
149 evacuate((StgClosure **)&tso->link);
152 // scavange current transaction record
153 evacuate((StgClosure **)&tso->trec);
155 // scavenge this thread's stack
156 scavenge_stack(tso->sp, &(tso->stack[tso->stack_size]));
159 /* -----------------------------------------------------------------------------
160 Blocks of function args occur on the stack (at the top) and
162 -------------------------------------------------------------------------- */
165 scavenge_arg_block (StgFunInfoTable *fun_info, StgClosure **args)
172 switch (fun_info->f.fun_type) {
174 bitmap = BITMAP_BITS(fun_info->f.b.bitmap);
175 size = BITMAP_SIZE(fun_info->f.b.bitmap);
178 size = GET_FUN_LARGE_BITMAP(fun_info)->size;
179 scavenge_large_bitmap(p, GET_FUN_LARGE_BITMAP(fun_info), size);
183 bitmap = BITMAP_BITS(stg_arg_bitmaps[fun_info->f.fun_type]);
184 size = BITMAP_SIZE(stg_arg_bitmaps[fun_info->f.fun_type]);
187 if ((bitmap & 1) == 0) {
188 evacuate((StgClosure **)p);
191 bitmap = bitmap >> 1;
200 scavenge_PAP_payload (StgClosure *fun, StgClosure **payload, StgWord size)
204 StgFunInfoTable *fun_info;
206 fun_info = get_fun_itbl(UNTAG_CLOSURE(fun));
207 ASSERT(fun_info->i.type != PAP);
210 switch (fun_info->f.fun_type) {
212 bitmap = BITMAP_BITS(fun_info->f.b.bitmap);
215 scavenge_large_bitmap(p, GET_FUN_LARGE_BITMAP(fun_info), size);
219 scavenge_large_bitmap((StgPtr)payload, BCO_BITMAP(fun), size);
223 bitmap = BITMAP_BITS(stg_arg_bitmaps[fun_info->f.fun_type]);
226 if ((bitmap & 1) == 0) {
227 evacuate((StgClosure **)p);
230 bitmap = bitmap >> 1;
239 scavenge_PAP (StgPAP *pap)
242 return scavenge_PAP_payload (pap->fun, pap->payload, pap->n_args);
246 scavenge_AP (StgAP *ap)
249 return scavenge_PAP_payload (ap->fun, ap->payload, ap->n_args);
252 /* -----------------------------------------------------------------------------
253 Scavenge everything on the mark stack.
255 This is slightly different from scavenge():
256 - we don't walk linearly through the objects, so the scavenger
257 doesn't need to advance the pointer on to the next object.
258 -------------------------------------------------------------------------- */
261 scavenge_mark_stack(void)
265 step *saved_evac_step;
267 gct->evac_step = &oldest_gen->steps[0];
268 saved_evac_step = gct->evac_step;
271 while (!mark_stack_empty()) {
272 p = pop_mark_stack();
274 ASSERT(LOOKS_LIKE_CLOSURE_PTR(p));
275 info = get_itbl((StgClosure *)p);
278 switch (((volatile StgWord *)info)[1] & 0xffff) {
283 rtsBool saved_eager_promotion = gct->eager_promotion;
285 StgMVar *mvar = ((StgMVar *)p);
286 gct->eager_promotion = rtsFalse;
287 evacuate((StgClosure **)&mvar->head);
288 evacuate((StgClosure **)&mvar->tail);
289 evacuate((StgClosure **)&mvar->value);
290 gct->eager_promotion = saved_eager_promotion;
292 if (gct->failed_to_evac) {
293 mvar->header.info = &stg_MVAR_DIRTY_info;
295 mvar->header.info = &stg_MVAR_CLEAN_info;
301 scavenge_fun_srt(info);
302 evacuate(&((StgClosure *)p)->payload[1]);
303 evacuate(&((StgClosure *)p)->payload[0]);
307 scavenge_thunk_srt(info);
308 evacuate(&((StgThunk *)p)->payload[1]);
309 evacuate(&((StgThunk *)p)->payload[0]);
313 evacuate(&((StgClosure *)p)->payload[1]);
314 evacuate(&((StgClosure *)p)->payload[0]);
319 scavenge_fun_srt(info);
320 evacuate(&((StgClosure *)p)->payload[0]);
325 scavenge_thunk_srt(info);
326 evacuate(&((StgThunk *)p)->payload[0]);
331 evacuate(&((StgClosure *)p)->payload[0]);
336 scavenge_fun_srt(info);
341 scavenge_thunk_srt(info);
349 scavenge_fun_srt(info);
356 scavenge_thunk_srt(info);
357 end = (P_)((StgThunk *)p)->payload + info->layout.payload.ptrs;
358 for (p = (P_)((StgThunk *)p)->payload; p < end; p++) {
359 evacuate((StgClosure **)p);
371 end = (P_)((StgClosure *)p)->payload + info->layout.payload.ptrs;
372 for (p = (P_)((StgClosure *)p)->payload; p < end; p++) {
373 evacuate((StgClosure **)p);
379 StgBCO *bco = (StgBCO *)p;
380 evacuate((StgClosure **)&bco->instrs);
381 evacuate((StgClosure **)&bco->literals);
382 evacuate((StgClosure **)&bco->ptrs);
387 // don't need to do anything here: the only possible case
388 // is that we're in a 1-space compacting collector, with
389 // no "old" generation.
393 case IND_OLDGEN_PERM:
394 evacuate(&((StgInd *)p)->indirectee);
398 case MUT_VAR_DIRTY: {
399 rtsBool saved_eager_promotion = gct->eager_promotion;
401 gct->eager_promotion = rtsFalse;
402 evacuate(&((StgMutVar *)p)->var);
403 gct->eager_promotion = saved_eager_promotion;
405 if (gct->failed_to_evac) {
406 ((StgClosure *)q)->header.info = &stg_MUT_VAR_DIRTY_info;
408 ((StgClosure *)q)->header.info = &stg_MUT_VAR_CLEAN_info;
414 case SE_CAF_BLACKHOLE:
422 StgSelector *s = (StgSelector *)p;
423 evacuate(&s->selectee);
427 // A chunk of stack saved in a heap object
430 StgAP_STACK *ap = (StgAP_STACK *)p;
433 scavenge_stack((StgPtr)ap->payload, (StgPtr)ap->payload + ap->size);
438 scavenge_PAP((StgPAP *)p);
442 scavenge_AP((StgAP *)p);
445 case MUT_ARR_PTRS_CLEAN:
446 case MUT_ARR_PTRS_DIRTY:
452 // We don't eagerly promote objects pointed to by a mutable
453 // array, but if we find the array only points to objects in
454 // the same or an older generation, we mark it "clean" and
455 // avoid traversing it during minor GCs.
456 saved_eager = gct->eager_promotion;
457 gct->eager_promotion = rtsFalse;
458 next = p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
459 for (p = (P_)((StgMutArrPtrs *)p)->payload; p < next; p++) {
460 evacuate((StgClosure **)p);
462 gct->eager_promotion = saved_eager;
464 if (gct->failed_to_evac) {
465 ((StgClosure *)q)->header.info = &stg_MUT_ARR_PTRS_DIRTY_info;
467 ((StgClosure *)q)->header.info = &stg_MUT_ARR_PTRS_CLEAN_info;
470 gct->failed_to_evac = rtsTrue; // mutable anyhow.
474 case MUT_ARR_PTRS_FROZEN:
475 case MUT_ARR_PTRS_FROZEN0:
480 next = p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
481 for (p = (P_)((StgMutArrPtrs *)p)->payload; p < next; p++) {
482 evacuate((StgClosure **)p);
485 // If we're going to put this object on the mutable list, then
486 // set its info ptr to MUT_ARR_PTRS_FROZEN0 to indicate that.
487 if (gct->failed_to_evac) {
488 ((StgClosure *)q)->header.info = &stg_MUT_ARR_PTRS_FROZEN0_info;
490 ((StgClosure *)q)->header.info = &stg_MUT_ARR_PTRS_FROZEN_info;
497 StgTSO *tso = (StgTSO *)p;
498 rtsBool saved_eager = gct->eager_promotion;
500 gct->eager_promotion = rtsFalse;
502 gct->eager_promotion = saved_eager;
504 if (gct->failed_to_evac) {
505 tso->flags |= TSO_DIRTY;
507 tso->flags &= ~TSO_DIRTY;
510 gct->failed_to_evac = rtsTrue; // always on the mutable list
514 case TVAR_WATCH_QUEUE:
516 StgTVarWatchQueue *wq = ((StgTVarWatchQueue *) p);
518 evacuate((StgClosure **)&wq->closure);
519 evacuate((StgClosure **)&wq->next_queue_entry);
520 evacuate((StgClosure **)&wq->prev_queue_entry);
521 gct->evac_step = saved_evac_step;
522 gct->failed_to_evac = rtsTrue; // mutable
528 StgTVar *tvar = ((StgTVar *) p);
530 evacuate((StgClosure **)&tvar->current_value);
531 evacuate((StgClosure **)&tvar->first_watch_queue_entry);
532 gct->evac_step = saved_evac_step;
533 gct->failed_to_evac = rtsTrue; // mutable
540 StgTRecChunk *tc = ((StgTRecChunk *) p);
541 TRecEntry *e = &(tc -> entries[0]);
543 evacuate((StgClosure **)&tc->prev_chunk);
544 for (i = 0; i < tc -> next_entry_idx; i ++, e++ ) {
545 evacuate((StgClosure **)&e->tvar);
546 evacuate((StgClosure **)&e->expected_value);
547 evacuate((StgClosure **)&e->new_value);
549 gct->evac_step = saved_evac_step;
550 gct->failed_to_evac = rtsTrue; // mutable
556 StgTRecHeader *trec = ((StgTRecHeader *) p);
558 evacuate((StgClosure **)&trec->enclosing_trec);
559 evacuate((StgClosure **)&trec->current_chunk);
560 evacuate((StgClosure **)&trec->invariants_to_check);
561 gct->evac_step = saved_evac_step;
562 gct->failed_to_evac = rtsTrue; // mutable
566 case ATOMIC_INVARIANT:
568 StgAtomicInvariant *invariant = ((StgAtomicInvariant *) p);
570 evacuate(&invariant->code);
571 evacuate((StgClosure **)&invariant->last_execution);
572 gct->evac_step = saved_evac_step;
573 gct->failed_to_evac = rtsTrue; // mutable
577 case INVARIANT_CHECK_QUEUE:
579 StgInvariantCheckQueue *queue = ((StgInvariantCheckQueue *) p);
581 evacuate((StgClosure **)&queue->invariant);
582 evacuate((StgClosure **)&queue->my_execution);
583 evacuate((StgClosure **)&queue->next_queue_entry);
584 gct->evac_step = saved_evac_step;
585 gct->failed_to_evac = rtsTrue; // mutable
590 barf("scavenge_mark_stack: unimplemented/strange closure type %d @ %p",
594 if (gct->failed_to_evac) {
595 gct->failed_to_evac = rtsFalse;
596 if (gct->evac_step) {
597 recordMutableGen_GC((StgClosure *)q, gct->evac_step->gen);
601 // mark the next bit to indicate "scavenged"
602 mark(q+1, Bdescr(q));
604 } // while (!mark_stack_empty())
606 // start a new linear scan if the mark stack overflowed at some point
607 if (mark_stack_overflowed && oldgen_scan_bd == NULL) {
608 debugTrace(DEBUG_gc, "scavenge_mark_stack: starting linear scan");
609 mark_stack_overflowed = rtsFalse;
610 oldgen_scan_bd = oldest_gen->steps[0].old_blocks;
611 oldgen_scan = oldgen_scan_bd->start;
614 if (oldgen_scan_bd) {
615 // push a new thing on the mark stack
617 // find a closure that is marked but not scavenged, and start
619 while (oldgen_scan < oldgen_scan_bd->free
620 && !is_marked(oldgen_scan,oldgen_scan_bd)) {
624 if (oldgen_scan < oldgen_scan_bd->free) {
626 // already scavenged?
627 if (is_marked(oldgen_scan+1,oldgen_scan_bd)) {
628 oldgen_scan += sizeofW(StgHeader) + MIN_PAYLOAD_SIZE;
631 push_mark_stack(oldgen_scan);
632 // ToDo: bump the linear scan by the actual size of the object
633 oldgen_scan += sizeofW(StgHeader) + MIN_PAYLOAD_SIZE;
637 oldgen_scan_bd = oldgen_scan_bd->link;
638 if (oldgen_scan_bd != NULL) {
639 oldgen_scan = oldgen_scan_bd->start;
645 /* -----------------------------------------------------------------------------
648 This is used for objects that are temporarily marked as mutable
649 because they contain old-to-new generation pointers. Only certain
650 objects can have this property.
651 -------------------------------------------------------------------------- */
654 scavenge_one(StgPtr p)
656 const StgInfoTable *info;
657 step *saved_evac_step = gct->evac_step;
660 ASSERT(LOOKS_LIKE_CLOSURE_PTR(p));
661 info = get_itbl((StgClosure *)p);
663 switch (info->type) {
668 rtsBool saved_eager_promotion = gct->eager_promotion;
670 StgMVar *mvar = ((StgMVar *)p);
671 gct->eager_promotion = rtsFalse;
672 evacuate((StgClosure **)&mvar->head);
673 evacuate((StgClosure **)&mvar->tail);
674 evacuate((StgClosure **)&mvar->value);
675 gct->eager_promotion = saved_eager_promotion;
677 if (gct->failed_to_evac) {
678 mvar->header.info = &stg_MVAR_DIRTY_info;
680 mvar->header.info = &stg_MVAR_CLEAN_info;
694 end = (StgPtr)((StgThunk *)p)->payload + info->layout.payload.ptrs;
695 for (q = (StgPtr)((StgThunk *)p)->payload; q < end; q++) {
696 evacuate((StgClosure **)q);
702 case FUN_1_0: // hardly worth specialising these guys
718 end = (StgPtr)((StgClosure *)p)->payload + info->layout.payload.ptrs;
719 for (q = (StgPtr)((StgClosure *)p)->payload; q < end; q++) {
720 evacuate((StgClosure **)q);
726 case MUT_VAR_DIRTY: {
728 rtsBool saved_eager_promotion = gct->eager_promotion;
730 gct->eager_promotion = rtsFalse;
731 evacuate(&((StgMutVar *)p)->var);
732 gct->eager_promotion = saved_eager_promotion;
734 if (gct->failed_to_evac) {
735 ((StgClosure *)q)->header.info = &stg_MUT_VAR_DIRTY_info;
737 ((StgClosure *)q)->header.info = &stg_MUT_VAR_CLEAN_info;
743 case SE_CAF_BLACKHOLE:
750 StgSelector *s = (StgSelector *)p;
751 evacuate(&s->selectee);
757 StgAP_STACK *ap = (StgAP_STACK *)p;
760 scavenge_stack((StgPtr)ap->payload, (StgPtr)ap->payload + ap->size);
761 p = (StgPtr)ap->payload + ap->size;
766 p = scavenge_PAP((StgPAP *)p);
770 p = scavenge_AP((StgAP *)p);
777 case MUT_ARR_PTRS_CLEAN:
778 case MUT_ARR_PTRS_DIRTY:
783 // We don't eagerly promote objects pointed to by a mutable
784 // array, but if we find the array only points to objects in
785 // the same or an older generation, we mark it "clean" and
786 // avoid traversing it during minor GCs.
787 saved_eager = gct->eager_promotion;
788 gct->eager_promotion = rtsFalse;
790 next = p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
791 for (p = (P_)((StgMutArrPtrs *)p)->payload; p < next; p++) {
792 evacuate((StgClosure **)p);
794 gct->eager_promotion = saved_eager;
796 if (gct->failed_to_evac) {
797 ((StgClosure *)q)->header.info = &stg_MUT_ARR_PTRS_DIRTY_info;
799 ((StgClosure *)q)->header.info = &stg_MUT_ARR_PTRS_CLEAN_info;
802 gct->failed_to_evac = rtsTrue;
806 case MUT_ARR_PTRS_FROZEN:
807 case MUT_ARR_PTRS_FROZEN0:
812 next = p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
813 for (p = (P_)((StgMutArrPtrs *)p)->payload; p < next; p++) {
814 evacuate((StgClosure **)p);
817 // If we're going to put this object on the mutable list, then
818 // set its info ptr to MUT_ARR_PTRS_FROZEN0 to indicate that.
819 if (gct->failed_to_evac) {
820 ((StgClosure *)q)->header.info = &stg_MUT_ARR_PTRS_FROZEN0_info;
822 ((StgClosure *)q)->header.info = &stg_MUT_ARR_PTRS_FROZEN_info;
829 StgTSO *tso = (StgTSO *)p;
830 rtsBool saved_eager = gct->eager_promotion;
832 gct->eager_promotion = rtsFalse;
834 gct->eager_promotion = saved_eager;
836 if (gct->failed_to_evac) {
837 tso->flags |= TSO_DIRTY;
839 tso->flags &= ~TSO_DIRTY;
842 gct->failed_to_evac = rtsTrue; // always on the mutable list
846 case TVAR_WATCH_QUEUE:
848 StgTVarWatchQueue *wq = ((StgTVarWatchQueue *) p);
850 evacuate((StgClosure **)&wq->closure);
851 evacuate((StgClosure **)&wq->next_queue_entry);
852 evacuate((StgClosure **)&wq->prev_queue_entry);
853 gct->evac_step = saved_evac_step;
854 gct->failed_to_evac = rtsTrue; // mutable
860 StgTVar *tvar = ((StgTVar *) p);
862 evacuate((StgClosure **)&tvar->current_value);
863 evacuate((StgClosure **)&tvar->first_watch_queue_entry);
864 gct->evac_step = saved_evac_step;
865 gct->failed_to_evac = rtsTrue; // mutable
871 StgTRecHeader *trec = ((StgTRecHeader *) p);
873 evacuate((StgClosure **)&trec->enclosing_trec);
874 evacuate((StgClosure **)&trec->current_chunk);
875 evacuate((StgClosure **)&trec->invariants_to_check);
876 gct->evac_step = saved_evac_step;
877 gct->failed_to_evac = rtsTrue; // mutable
884 StgTRecChunk *tc = ((StgTRecChunk *) p);
885 TRecEntry *e = &(tc -> entries[0]);
887 evacuate((StgClosure **)&tc->prev_chunk);
888 for (i = 0; i < tc -> next_entry_idx; i ++, e++ ) {
889 evacuate((StgClosure **)&e->tvar);
890 evacuate((StgClosure **)&e->expected_value);
891 evacuate((StgClosure **)&e->new_value);
893 gct->evac_step = saved_evac_step;
894 gct->failed_to_evac = rtsTrue; // mutable
898 case ATOMIC_INVARIANT:
900 StgAtomicInvariant *invariant = ((StgAtomicInvariant *) p);
902 evacuate(&invariant->code);
903 evacuate((StgClosure **)&invariant->last_execution);
904 gct->evac_step = saved_evac_step;
905 gct->failed_to_evac = rtsTrue; // mutable
909 case INVARIANT_CHECK_QUEUE:
911 StgInvariantCheckQueue *queue = ((StgInvariantCheckQueue *) p);
913 evacuate((StgClosure **)&queue->invariant);
914 evacuate((StgClosure **)&queue->my_execution);
915 evacuate((StgClosure **)&queue->next_queue_entry);
916 gct->evac_step = saved_evac_step;
917 gct->failed_to_evac = rtsTrue; // mutable
922 case IND_OLDGEN_PERM:
925 /* Careful here: a THUNK can be on the mutable list because
926 * it contains pointers to young gen objects. If such a thunk
927 * is updated, the IND_OLDGEN will be added to the mutable
928 * list again, and we'll scavenge it twice. evacuate()
929 * doesn't check whether the object has already been
930 * evacuated, so we perform that check here.
932 StgClosure *q = ((StgInd *)p)->indirectee;
933 if (HEAP_ALLOCED(q) && Bdescr((StgPtr)q)->flags & BF_EVACUATED) {
936 evacuate(&((StgInd *)p)->indirectee);
939 #if 0 && defined(DEBUG)
940 if (RtsFlags.DebugFlags.gc)
941 /* Debugging code to print out the size of the thing we just
945 StgPtr start = gen->steps[0].scan;
946 bdescr *start_bd = gen->steps[0].scan_bd;
948 scavenge(&gen->steps[0]);
949 if (start_bd != gen->steps[0].scan_bd) {
950 size += (P_)BLOCK_ROUND_UP(start) - start;
951 start_bd = start_bd->link;
952 while (start_bd != gen->steps[0].scan_bd) {
953 size += BLOCK_SIZE_W;
954 start_bd = start_bd->link;
956 size += gen->steps[0].scan -
957 (P_)BLOCK_ROUND_DOWN(gen->steps[0].scan);
959 size = gen->steps[0].scan - start;
961 debugBelch("evac IND_OLDGEN: %ld bytes", size * sizeof(W_));
967 barf("scavenge_one: strange object %d", (int)(info->type));
970 no_luck = gct->failed_to_evac;
971 gct->failed_to_evac = rtsFalse;
975 /* -----------------------------------------------------------------------------
976 Scavenging mutable lists.
978 We treat the mutable list of each generation > N (i.e. all the
979 generations older than the one being collected) as roots. We also
980 remove non-mutable objects from the mutable list at this point.
981 -------------------------------------------------------------------------- */
984 scavenge_mutable_list(generation *gen)
989 bd = gen->saved_mut_list;
991 gct->evac_step = &gen->steps[0];
992 for (; bd != NULL; bd = bd->link) {
993 for (q = bd->start; q < bd->free; q++) {
995 ASSERT(LOOKS_LIKE_CLOSURE_PTR(p));
998 switch (get_itbl((StgClosure *)p)->type) {
1000 barf("MUT_VAR_CLEAN on mutable list");
1002 mutlist_MUTVARS++; break;
1003 case MUT_ARR_PTRS_CLEAN:
1004 case MUT_ARR_PTRS_DIRTY:
1005 case MUT_ARR_PTRS_FROZEN:
1006 case MUT_ARR_PTRS_FROZEN0:
1007 mutlist_MUTARRS++; break;
1009 barf("MVAR_CLEAN on mutable list");
1011 mutlist_MVARS++; break;
1013 mutlist_OTHERS++; break;
1017 // Check whether this object is "clean", that is it
1018 // definitely doesn't point into a young generation.
1019 // Clean objects don't need to be scavenged. Some clean
1020 // objects (MUT_VAR_CLEAN) are not kept on the mutable
1021 // list at all; others, such as MUT_ARR_PTRS_CLEAN and
1022 // TSO, are always on the mutable list.
1024 switch (get_itbl((StgClosure *)p)->type) {
1025 case MUT_ARR_PTRS_CLEAN:
1026 recordMutableGen_GC((StgClosure *)p,gen);
1029 StgTSO *tso = (StgTSO *)p;
1030 if ((tso->flags & TSO_DIRTY) == 0) {
1031 // A clean TSO: we don't have to traverse its
1032 // stack. However, we *do* follow the link field:
1033 // we don't want to have to mark a TSO dirty just
1034 // because we put it on a different queue.
1035 if (tso->why_blocked != BlockedOnBlackHole) {
1036 evacuate((StgClosure **)&tso->link);
1038 recordMutableGen_GC((StgClosure *)p,gen);
1046 if (scavenge_one(p)) {
1047 // didn't manage to promote everything, so put the
1048 // object back on the list.
1049 recordMutableGen_GC((StgClosure *)p,gen);
1054 // free the old mut_list
1055 freeChain_sync(gen->saved_mut_list);
1056 gen->saved_mut_list = NULL;
1059 /* -----------------------------------------------------------------------------
1060 Scavenging the static objects.
1062 We treat the mutable list of each generation > N (i.e. all the
1063 generations older than the one being collected) as roots. We also
1064 remove non-mutable objects from the mutable list at this point.
1065 -------------------------------------------------------------------------- */
1068 scavenge_static(void)
1071 const StgInfoTable *info;
1073 /* Always evacuate straight to the oldest generation for static
1075 gct->evac_step = &oldest_gen->steps[0];
1077 /* keep going until we've scavenged all the objects on the linked
1082 ACQUIRE_SPIN_LOCK(&static_objects_sync);
1084 /* get the next static object from the list. Remember, there might
1085 * be more stuff on this list after each evacuation...
1086 * (static_objects is a global)
1089 if (p == END_OF_STATIC_LIST) {
1090 RELEASE_SPIN_LOCK(&static_objects_sync);
1094 ASSERT(LOOKS_LIKE_CLOSURE_PTR(p));
1097 if (info->type==RBH)
1098 info = REVERT_INFOPTR(info); // if it's an RBH, look at the orig closure
1100 // make sure the info pointer is into text space
1102 /* Take this object *off* the static_objects list,
1103 * and put it on the scavenged_static_objects list.
1105 static_objects = *STATIC_LINK(info,p);
1106 *STATIC_LINK(info,p) = scavenged_static_objects;
1107 scavenged_static_objects = p;
1109 RELEASE_SPIN_LOCK(&static_objects_sync);
1111 switch (info -> type) {
1115 StgInd *ind = (StgInd *)p;
1116 evacuate(&ind->indirectee);
1118 /* might fail to evacuate it, in which case we have to pop it
1119 * back on the mutable list of the oldest generation. We
1120 * leave it *on* the scavenged_static_objects list, though,
1121 * in case we visit this object again.
1123 if (gct->failed_to_evac) {
1124 gct->failed_to_evac = rtsFalse;
1125 recordMutableGen_GC((StgClosure *)p,oldest_gen);
1131 scavenge_thunk_srt(info);
1135 scavenge_fun_srt(info);
1142 next = (P_)p->payload + info->layout.payload.ptrs;
1143 // evacuate the pointers
1144 for (q = (P_)p->payload; q < next; q++) {
1145 evacuate((StgClosure **)q);
1151 barf("scavenge_static: strange closure %d", (int)(info->type));
1154 ASSERT(gct->failed_to_evac == rtsFalse);
1158 /* -----------------------------------------------------------------------------
1159 scavenge a chunk of memory described by a bitmap
1160 -------------------------------------------------------------------------- */
1163 scavenge_large_bitmap( StgPtr p, StgLargeBitmap *large_bitmap, nat size )
1169 bitmap = large_bitmap->bitmap[b];
1170 for (i = 0; i < size; ) {
1171 if ((bitmap & 1) == 0) {
1172 evacuate((StgClosure **)p);
1176 if (i % BITS_IN(W_) == 0) {
1178 bitmap = large_bitmap->bitmap[b];
1180 bitmap = bitmap >> 1;
1185 STATIC_INLINE StgPtr
1186 scavenge_small_bitmap (StgPtr p, nat size, StgWord bitmap)
1189 if ((bitmap & 1) == 0) {
1190 evacuate((StgClosure **)p);
1193 bitmap = bitmap >> 1;
1199 /* -----------------------------------------------------------------------------
1200 scavenge_stack walks over a section of stack and evacuates all the
1201 objects pointed to by it. We can use the same code for walking
1202 AP_STACK_UPDs, since these are just sections of copied stack.
1203 -------------------------------------------------------------------------- */
1206 scavenge_stack(StgPtr p, StgPtr stack_end)
1208 const StgRetInfoTable* info;
1213 * Each time around this loop, we are looking at a chunk of stack
1214 * that starts with an activation record.
1217 while (p < stack_end) {
1218 info = get_ret_itbl((StgClosure *)p);
1220 switch (info->i.type) {
1223 // In SMP, we can get update frames that point to indirections
1224 // when two threads evaluate the same thunk. We do attempt to
1225 // discover this situation in threadPaused(), but it's
1226 // possible that the following sequence occurs:
1235 // Now T is an indirection, and the update frame is already
1236 // marked on A's stack, so we won't traverse it again in
1237 // threadPaused(). We could traverse the whole stack again
1238 // before GC, but that seems like overkill.
1240 // Scavenging this update frame as normal would be disastrous;
1241 // the updatee would end up pointing to the value. So we turn
1242 // the indirection into an IND_PERM, so that evacuate will
1243 // copy the indirection into the old generation instead of
1247 type = get_itbl(((StgUpdateFrame *)p)->updatee)->type;
1249 ((StgUpdateFrame *)p)->updatee->header.info =
1250 (StgInfoTable *)&stg_IND_PERM_info;
1251 } else if (type == IND_OLDGEN) {
1252 ((StgUpdateFrame *)p)->updatee->header.info =
1253 (StgInfoTable *)&stg_IND_OLDGEN_PERM_info;
1255 evacuate(&((StgUpdateFrame *)p)->updatee);
1256 p += sizeofW(StgUpdateFrame);
1260 // small bitmap (< 32 entries, or 64 on a 64-bit machine)
1261 case CATCH_STM_FRAME:
1262 case CATCH_RETRY_FRAME:
1263 case ATOMICALLY_FRAME:
1267 bitmap = BITMAP_BITS(info->i.layout.bitmap);
1268 size = BITMAP_SIZE(info->i.layout.bitmap);
1269 // NOTE: the payload starts immediately after the info-ptr, we
1270 // don't have an StgHeader in the same sense as a heap closure.
1272 p = scavenge_small_bitmap(p, size, bitmap);
1276 scavenge_srt((StgClosure **)GET_SRT(info), info->i.srt_bitmap);
1284 evacuate((StgClosure **)p);
1287 size = BCO_BITMAP_SIZE(bco);
1288 scavenge_large_bitmap(p, BCO_BITMAP(bco), size);
1293 // large bitmap (> 32 entries, or > 64 on a 64-bit machine)
1298 size = GET_LARGE_BITMAP(&info->i)->size;
1300 scavenge_large_bitmap(p, GET_LARGE_BITMAP(&info->i), size);
1302 // and don't forget to follow the SRT
1306 // Dynamic bitmap: the mask is stored on the stack, and
1307 // there are a number of non-pointers followed by a number
1308 // of pointers above the bitmapped area. (see StgMacros.h,
1313 dyn = ((StgRetDyn *)p)->liveness;
1315 // traverse the bitmap first
1316 bitmap = RET_DYN_LIVENESS(dyn);
1317 p = (P_)&((StgRetDyn *)p)->payload[0];
1318 size = RET_DYN_BITMAP_SIZE;
1319 p = scavenge_small_bitmap(p, size, bitmap);
1321 // skip over the non-ptr words
1322 p += RET_DYN_NONPTRS(dyn) + RET_DYN_NONPTR_REGS_SIZE;
1324 // follow the ptr words
1325 for (size = RET_DYN_PTRS(dyn); size > 0; size--) {
1326 evacuate((StgClosure **)p);
1334 StgRetFun *ret_fun = (StgRetFun *)p;
1335 StgFunInfoTable *fun_info;
1337 evacuate(&ret_fun->fun);
1338 fun_info = get_fun_itbl(UNTAG_CLOSURE(ret_fun->fun));
1339 p = scavenge_arg_block(fun_info, ret_fun->payload);
1344 barf("scavenge_stack: weird activation record found on stack: %d", (int)(info->i.type));
1349 /*-----------------------------------------------------------------------------
1350 scavenge the large object list.
1352 evac_step set by caller; similar games played with evac_step as with
1353 scavenge() - see comment at the top of scavenge(). Most large
1354 objects are (repeatedly) mutable, so most of the time evac_step will
1356 --------------------------------------------------------------------------- */
1359 scavenge_large (step_workspace *ws)
1364 gct->evac_step = ws->stp;
1366 bd = ws->todo_large_objects;
1368 for (; bd != NULL; bd = ws->todo_large_objects) {
1370 // take this object *off* the large objects list and put it on
1371 // the scavenged large objects list. This is so that we can
1372 // treat new_large_objects as a stack and push new objects on
1373 // the front when evacuating.
1374 ws->todo_large_objects = bd->link;
1376 ACQUIRE_SPIN_LOCK(&ws->stp->sync_large_objects);
1377 dbl_link_onto(bd, &ws->stp->scavenged_large_objects);
1378 ws->stp->n_scavenged_large_blocks += bd->blocks;
1379 RELEASE_SPIN_LOCK(&ws->stp->sync_large_objects);
1382 if (scavenge_one(p)) {
1383 if (ws->stp->gen_no > 0) {
1384 recordMutableGen_GC((StgClosure *)p, ws->stp->gen);
1390 /* ----------------------------------------------------------------------------
1392 ------------------------------------------------------------------------- */
1395 #include "Scav.c-inc"
1397 #include "Scav.c-inc"
1399 /* ----------------------------------------------------------------------------
1400 Find the oldest full block to scavenge, and scavenge it.
1401 ------------------------------------------------------------------------- */
1404 scavenge_find_global_work (void)
1412 for (g = RtsFlags.GcFlags.generations-1; g >= 0; g--) {
1413 for (s = generations[g].n_steps-1; s >= 0; s--) {
1414 if (g == 0 && s == 0 && RtsFlags.GcFlags.generations > 1) {
1417 ws = &gct->steps[g][s];
1419 // If we have any large objects to scavenge, do them now.
1420 if (ws->todo_large_objects) {
1425 if ((bd = grab_todo_block(ws)) != NULL) {
1426 // no need to assign this to ws->scan_bd, we're going
1427 // to scavenge the whole thing and then push it on
1428 // our scavd list. This saves pushing out the
1429 // scan_bd block, which might be partial.
1431 scavenge_block0(bd, bd->start);
1433 scavenge_block(bd, bd->start);
1435 push_scan_block(bd, ws);
1439 if (flag) return rtsTrue;
1445 /* ----------------------------------------------------------------------------
1446 Look for local work to do.
1448 We can have outstanding scavenging to do if, for any of the workspaces,
1450 - the scan block is the same as the todo block, and new objects
1451 have been evacuated to the todo block.
1453 - the scan block *was* the same as the todo block, but the todo
1454 block filled up and a new one has been allocated.
1455 ------------------------------------------------------------------------- */
1458 scavenge_find_local_work (void)
1465 for (g = RtsFlags.GcFlags.generations; --g >= 0; ) {
1466 for (s = generations[g].n_steps; --s >= 0; ) {
1467 if (g == 0 && s == 0 && RtsFlags.GcFlags.generations > 1) {
1470 ws = &gct->steps[g][s];
1472 if (ws->todo_bd != NULL)
1474 ws->todo_bd->free = ws->todo_free;
1477 // If we have a todo block and no scan block, start
1478 // scanning the todo block.
1479 if (ws->scan_bd == NULL && ws->todo_bd != NULL)
1481 ws->scan_bd = ws->todo_bd;
1482 ws->scan = ws->scan_bd->start;
1485 // If we have a scan block with some work to do,
1486 // scavenge everything up to the free pointer.
1487 if (ws->scan != NULL && ws->scan < ws->scan_bd->free)
1490 scavenge_block0(ws->scan_bd, ws->scan);
1492 scavenge_block(ws->scan_bd, ws->scan);
1494 ws->scan = ws->scan_bd->free;
1498 if (ws->scan_bd != NULL && ws->scan == ws->scan_bd->free
1499 && ws->scan_bd != ws->todo_bd)
1501 // we're not going to evac any more objects into
1502 // this block, so push it now.
1503 push_scan_block(ws->scan_bd, ws);
1506 // we might be able to scan the todo block now. But
1507 // don't do it right away: there might be full blocks
1508 // waiting to be scanned as a result of scavenge_block above.
1512 if (flag) return rtsTrue;
1518 /* ----------------------------------------------------------------------------
1519 Scavenge until we can't find anything more to scavenge.
1520 ------------------------------------------------------------------------- */
1528 work_to_do = rtsFalse;
1530 // scavenge static objects
1531 if (major_gc && static_objects != END_OF_STATIC_LIST) {
1532 IF_DEBUG(sanity, checkStaticObjects(static_objects));
1536 // scavenge objects in compacted generation
1537 if (mark_stack_overflowed || oldgen_scan_bd != NULL ||
1538 (mark_stack_bdescr != NULL && !mark_stack_empty())) {
1539 scavenge_mark_stack();
1540 work_to_do = rtsTrue;
1543 // Order is important here: we want to deal in full blocks as
1544 // much as possible, so go for global work in preference to
1545 // local work. Only if all the global work has been exhausted
1546 // do we start scavenging the fragments of blocks in the local
1548 if (scavenge_find_global_work()) goto loop;
1549 if (scavenge_find_local_work()) goto loop;
1551 if (work_to_do) goto loop;
1562 // scavenge static objects
1563 if (major_gc && static_objects != END_OF_STATIC_LIST) {
1567 // scavenge objects in compacted generation
1568 if (mark_stack_overflowed || oldgen_scan_bd != NULL ||
1569 (mark_stack_bdescr != NULL && !mark_stack_empty())) {
1573 // Check for global work in any step. We don't need to check for
1574 // local work, because we have already exited scavenge_loop(),
1575 // which means there is no local work for this thread.
1576 for (g = RtsFlags.GcFlags.generations-1; g >= 0; g--) {
1577 for (s = generations[g].n_steps-1; s >= 0; s--) {
1578 if (g == 0 && s == 0 && RtsFlags.GcFlags.generations > 1) {
1581 ws = &gct->steps[g][s];
1582 if (ws->todo_large_objects) return rtsTrue;
1583 if (ws->stp->todos) return rtsTrue;