1 /* -----------------------------------------------------------------------------
2 * $Id: GC.c,v 1.51 1999/03/15 16:30:27 simonm Exp $
4 * (c) The GHC Team 1998-1999
6 * Generational garbage collector
8 * ---------------------------------------------------------------------------*/
14 #include "StoragePriv.h"
17 #include "SchedAPI.h" /* for ReverCAFs prototype */
20 #include "BlockAlloc.h"
22 #include "DebugProf.h"
25 #include "StablePriv.h"
29 /* STATIC OBJECT LIST.
32 * We maintain a linked list of static objects that are still live.
33 * The requirements for this list are:
35 * - we need to scan the list while adding to it, in order to
36 * scavenge all the static objects (in the same way that
37 * breadth-first scavenging works for dynamic objects).
39 * - we need to be able to tell whether an object is already on
40 * the list, to break loops.
42 * Each static object has a "static link field", which we use for
43 * linking objects on to the list. We use a stack-type list, consing
44 * objects on the front as they are added (this means that the
45 * scavenge phase is depth-first, not breadth-first, but that
48 * A separate list is kept for objects that have been scavenged
49 * already - this is so that we can zero all the marks afterwards.
51 * An object is on the list if its static link field is non-zero; this
52 * means that we have to mark the end of the list with '1', not NULL.
54 * Extra notes for generational GC:
56 * Each generation has a static object list associated with it. When
57 * collecting generations up to N, we treat the static object lists
58 * from generations > N as roots.
60 * We build up a static object list while collecting generations 0..N,
61 * which is then appended to the static object list of generation N+1.
63 StgClosure* static_objects; /* live static objects */
64 StgClosure* scavenged_static_objects; /* static objects scavenged so far */
66 /* N is the oldest generation being collected, where the generations
67 * are numbered starting at 0. A major GC (indicated by the major_gc
68 * flag) is when we're collecting all generations. We only attempt to
69 * deal with static objects and GC CAFs when doing a major GC.
72 static rtsBool major_gc;
74 /* Youngest generation that objects should be evacuated to in
75 * evacuate(). (Logically an argument to evacuate, but it's static
76 * a lot of the time so we optimise it into a global variable).
82 static StgWeak *old_weak_ptr_list; /* also pending finaliser list */
83 static rtsBool weak_done; /* all done for this pass */
85 /* Flag indicating failure to evacuate an object to the desired
88 static rtsBool failed_to_evac;
90 /* Old to-space (used for two-space collector only)
94 /* Data used for allocation area sizing.
96 lnat new_blocks; /* blocks allocated during this GC */
97 lnat g0s0_pcnt_kept = 30; /* percentage of g0s0 live at last minor GC */
99 /* -----------------------------------------------------------------------------
100 Static function declarations
101 -------------------------------------------------------------------------- */
103 static StgClosure * evacuate ( StgClosure *q );
104 static void zero_static_object_list ( StgClosure* first_static );
105 static void zero_mutable_list ( StgMutClosure *first );
106 static void revert_dead_CAFs ( void );
108 static rtsBool traverse_weak_ptr_list ( void );
109 static void cleanup_weak_ptr_list ( StgWeak **list );
111 static void scavenge_stack ( StgPtr p, StgPtr stack_end );
112 static void scavenge_large ( step *step );
113 static void scavenge ( step *step );
114 static void scavenge_static ( void );
115 static void scavenge_mutable_list ( generation *g );
116 static void scavenge_mut_once_list ( generation *g );
119 static void gcCAFs ( void );
122 /* -----------------------------------------------------------------------------
125 For garbage collecting generation N (and all younger generations):
127 - follow all pointers in the root set. the root set includes all
128 mutable objects in all steps in all generations.
130 - for each pointer, evacuate the object it points to into either
131 + to-space in the next higher step in that generation, if one exists,
132 + if the object's generation == N, then evacuate it to the next
133 generation if one exists, or else to-space in the current
135 + if the object's generation < N, then evacuate it to to-space
136 in the next generation.
138 - repeatedly scavenge to-space from each step in each generation
139 being collected until no more objects can be evacuated.
141 - free from-space in each step, and set from-space = to-space.
143 -------------------------------------------------------------------------- */
145 void GarbageCollect(void (*get_roots)(void))
149 lnat live, allocated, collected = 0, copied = 0;
153 CostCentreStack *prev_CCS;
156 /* tell the stats department that we've started a GC */
159 /* attribute any costs to CCS_GC */
165 /* We might have been called from Haskell land by _ccall_GC, in
166 * which case we need to call threadPaused() because the scheduler
167 * won't have done it.
169 if (CurrentTSO) { threadPaused(CurrentTSO); }
171 /* Approximate how much we allocated: number of blocks in the
172 * nursery + blocks allocated via allocate() - unused nusery blocks.
173 * This leaves a little slop at the end of each block, and doesn't
174 * take into account large objects (ToDo).
176 allocated = (nursery_blocks * BLOCK_SIZE_W) + allocated_bytes();
177 for ( bd = current_nursery->link; bd != NULL; bd = bd->link ) {
178 allocated -= BLOCK_SIZE_W;
180 if (current_nursery->free < current_nursery->start + BLOCK_SIZE_W) {
181 allocated -= (current_nursery->start + BLOCK_SIZE_W)
182 - current_nursery->free;
185 /* Figure out which generation to collect
188 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
189 if (generations[g].steps[0].n_blocks >= generations[g].max_blocks) {
193 major_gc = (N == RtsFlags.GcFlags.generations-1);
195 /* check stack sanity *before* GC (ToDo: check all threads) */
196 /*IF_DEBUG(sanity, checkTSO(MainTSO,0)); */
197 IF_DEBUG(sanity, checkFreeListSanity());
199 /* Initialise the static object lists
201 static_objects = END_OF_STATIC_LIST;
202 scavenged_static_objects = END_OF_STATIC_LIST;
204 /* zero the mutable list for the oldest generation (see comment by
205 * zero_mutable_list below).
208 zero_mutable_list(generations[RtsFlags.GcFlags.generations-1].mut_once_list);
211 /* Save the old to-space if we're doing a two-space collection
213 if (RtsFlags.GcFlags.generations == 1) {
214 old_to_space = g0s0->to_space;
215 g0s0->to_space = NULL;
218 /* Keep a count of how many new blocks we allocated during this GC
219 * (used for resizing the allocation area, later).
223 /* Initialise to-space in all the generations/steps that we're
226 for (g = 0; g <= N; g++) {
227 generations[g].mut_once_list = END_MUT_LIST;
228 generations[g].mut_list = END_MUT_LIST;
230 for (s = 0; s < generations[g].n_steps; s++) {
232 /* generation 0, step 0 doesn't need to-space */
233 if (g == 0 && s == 0 && RtsFlags.GcFlags.generations > 1) {
237 /* Get a free block for to-space. Extra blocks will be chained on
241 step = &generations[g].steps[s];
242 ASSERT(step->gen->no == g);
243 ASSERT(step->hp ? Bdescr(step->hp)->step == step : rtsTrue);
244 bd->gen = &generations[g];
247 bd->evacuated = 1; /* it's a to-space block */
248 step->hp = bd->start;
249 step->hpLim = step->hp + BLOCK_SIZE_W;
253 step->scan = bd->start;
255 step->new_large_objects = NULL;
256 step->scavenged_large_objects = NULL;
258 /* mark the large objects as not evacuated yet */
259 for (bd = step->large_objects; bd; bd = bd->link) {
265 /* make sure the older generations have at least one block to
266 * allocate into (this makes things easier for copy(), see below.
268 for (g = N+1; g < RtsFlags.GcFlags.generations; g++) {
269 for (s = 0; s < generations[g].n_steps; s++) {
270 step = &generations[g].steps[s];
271 if (step->hp_bd == NULL) {
273 bd->gen = &generations[g];
276 bd->evacuated = 0; /* *not* a to-space block */
277 step->hp = bd->start;
278 step->hpLim = step->hp + BLOCK_SIZE_W;
284 /* Set the scan pointer for older generations: remember we
285 * still have to scavenge objects that have been promoted. */
286 step->scan = step->hp;
287 step->scan_bd = step->hp_bd;
288 step->to_space = NULL;
290 step->new_large_objects = NULL;
291 step->scavenged_large_objects = NULL;
295 /* -----------------------------------------------------------------------
296 * follow all the roots that we know about:
297 * - mutable lists from each generation > N
298 * we want to *scavenge* these roots, not evacuate them: they're not
299 * going to move in this GC.
300 * Also: do them in reverse generation order. This is because we
301 * often want to promote objects that are pointed to by older
302 * generations early, so we don't have to repeatedly copy them.
303 * Doing the generations in reverse order ensures that we don't end
304 * up in the situation where we want to evac an object to gen 3 and
305 * it has already been evaced to gen 2.
309 for (g = RtsFlags.GcFlags.generations-1; g > N; g--) {
310 generations[g].saved_mut_list = generations[g].mut_list;
311 generations[g].mut_list = END_MUT_LIST;
314 /* Do the mut-once lists first */
315 for (g = RtsFlags.GcFlags.generations-1; g > N; g--) {
316 scavenge_mut_once_list(&generations[g]);
318 for (st = generations[g].n_steps-1; st >= 0; st--) {
319 scavenge(&generations[g].steps[st]);
323 for (g = RtsFlags.GcFlags.generations-1; g > N; g--) {
324 scavenge_mutable_list(&generations[g]);
326 for (st = generations[g].n_steps-1; st >= 0; st--) {
327 scavenge(&generations[g].steps[st]);
332 /* follow all the roots that the application knows about.
337 /* And don't forget to mark the TSO if we got here direct from
340 CurrentTSO = (StgTSO *)MarkRoot((StgClosure *)CurrentTSO);
343 /* Mark the weak pointer list, and prepare to detect dead weak
346 old_weak_ptr_list = weak_ptr_list;
347 weak_ptr_list = NULL;
348 weak_done = rtsFalse;
350 /* Mark the stable pointer table.
352 markStablePtrTable(major_gc);
356 /* ToDo: To fix the caf leak, we need to make the commented out
357 * parts of this code do something sensible - as described in
360 extern void markHugsObjects(void);
365 /* -------------------------------------------------------------------------
366 * Repeatedly scavenge all the areas we know about until there's no
367 * more scavenging to be done.
374 /* scavenge static objects */
375 if (major_gc && static_objects != END_OF_STATIC_LIST) {
379 /* When scavenging the older generations: Objects may have been
380 * evacuated from generations <= N into older generations, and we
381 * need to scavenge these objects. We're going to try to ensure that
382 * any evacuations that occur move the objects into at least the
383 * same generation as the object being scavenged, otherwise we
384 * have to create new entries on the mutable list for the older
388 /* scavenge each step in generations 0..maxgen */
392 for (gen = RtsFlags.GcFlags.generations-1; gen >= 0; gen--) {
393 for (st = generations[gen].n_steps-1; st >= 0 ; st--) {
394 if (gen == 0 && st == 0 && RtsFlags.GcFlags.generations > 1) {
397 step = &generations[gen].steps[st];
399 if (step->hp_bd != step->scan_bd || step->scan < step->hp) {
404 if (step->new_large_objects != NULL) {
405 scavenge_large(step);
412 if (flag) { goto loop; }
414 /* must be last... */
415 if (traverse_weak_ptr_list()) { /* returns rtsTrue if evaced something */
420 /* Final traversal of the weak pointer list (see comment by
421 * cleanUpWeakPtrList below).
423 cleanup_weak_ptr_list(&weak_ptr_list);
425 /* Now see which stable names are still alive.
427 gcStablePtrTable(major_gc);
429 /* revert dead CAFs and update enteredCAFs list */
432 /* Set the maximum blocks for the oldest generation, based on twice
433 * the amount of live data now, adjusted to fit the maximum heap
436 * This is an approximation, since in the worst case we'll need
437 * twice the amount of live data plus whatever space the other
440 if (RtsFlags.GcFlags.generations > 1) {
442 oldest_gen->max_blocks =
443 stg_max(oldest_gen->steps[0].to_blocks * RtsFlags.GcFlags.oldGenFactor,
444 RtsFlags.GcFlags.minOldGenSize);
445 if (oldest_gen->max_blocks > RtsFlags.GcFlags.maxHeapSize / 2) {
446 oldest_gen->max_blocks = RtsFlags.GcFlags.maxHeapSize / 2;
447 if (((int)oldest_gen->max_blocks -
448 (int)oldest_gen->steps[0].to_blocks) <
449 (RtsFlags.GcFlags.pcFreeHeap *
450 RtsFlags.GcFlags.maxHeapSize / 200)) {
457 /* run through all the generations/steps and tidy up
459 copied = new_blocks * BLOCK_SIZE_W;
460 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
463 generations[g].collections++; /* for stats */
466 for (s = 0; s < generations[g].n_steps; s++) {
468 step = &generations[g].steps[s];
470 if (!(g == 0 && s == 0 && RtsFlags.GcFlags.generations > 1)) {
471 /* Tidy the end of the to-space chains */
472 step->hp_bd->free = step->hp;
473 step->hp_bd->link = NULL;
474 /* stats information: how much we copied */
476 copied -= step->hp_bd->start + BLOCK_SIZE_W -
481 /* for generations we collected... */
484 collected += step->n_blocks * BLOCK_SIZE_W; /* for stats */
486 /* free old memory and shift to-space into from-space for all
487 * the collected steps (except the allocation area). These
488 * freed blocks will probaby be quickly recycled.
490 if (!(g == 0 && s == 0)) {
491 freeChain(step->blocks);
492 step->blocks = step->to_space;
493 step->n_blocks = step->to_blocks;
494 step->to_space = NULL;
496 for (bd = step->blocks; bd != NULL; bd = bd->link) {
497 bd->evacuated = 0; /* now from-space */
501 /* LARGE OBJECTS. The current live large objects are chained on
502 * scavenged_large, having been moved during garbage
503 * collection from large_objects. Any objects left on
504 * large_objects list are therefore dead, so we free them here.
506 for (bd = step->large_objects; bd != NULL; bd = next) {
511 for (bd = step->scavenged_large_objects; bd != NULL; bd = bd->link) {
514 step->large_objects = step->scavenged_large_objects;
516 /* Set the maximum blocks for this generation, interpolating
517 * between the maximum size of the oldest and youngest
520 * max_blocks = oldgen_max_blocks * G
521 * ----------------------
526 generations[g].max_blocks = (oldest_gen->max_blocks * g)
527 / (RtsFlags.GcFlags.generations-1);
529 generations[g].max_blocks = oldest_gen->max_blocks;
532 /* for older generations... */
535 /* For older generations, we need to append the
536 * scavenged_large_object list (i.e. large objects that have been
537 * promoted during this GC) to the large_object list for that step.
539 for (bd = step->scavenged_large_objects; bd; bd = next) {
542 dbl_link_onto(bd, &step->large_objects);
545 /* add the new blocks we promoted during this GC */
546 step->n_blocks += step->to_blocks;
551 /* Guess the amount of live data for stats. */
554 /* Free the small objects allocated via allocate(), since this will
555 * all have been copied into G0S1 now.
557 if (small_alloc_list != NULL) {
558 freeChain(small_alloc_list);
560 small_alloc_list = NULL;
564 alloc_blocks_lim = RtsFlags.GcFlags.minAllocAreaSize;
566 /* Two-space collector:
567 * Free the old to-space, and estimate the amount of live data.
569 if (RtsFlags.GcFlags.generations == 1) {
572 if (old_to_space != NULL) {
573 freeChain(old_to_space);
575 for (bd = g0s0->to_space; bd != NULL; bd = bd->link) {
576 bd->evacuated = 0; /* now from-space */
579 /* For a two-space collector, we need to resize the nursery. */
581 /* set up a new nursery. Allocate a nursery size based on a
582 * function of the amount of live data (currently a factor of 2,
583 * should be configurable (ToDo)). Use the blocks from the old
584 * nursery if possible, freeing up any left over blocks.
586 * If we get near the maximum heap size, then adjust our nursery
587 * size accordingly. If the nursery is the same size as the live
588 * data (L), then we need 3L bytes. We can reduce the size of the
589 * nursery to bring the required memory down near 2L bytes.
591 * A normal 2-space collector would need 4L bytes to give the same
592 * performance we get from 3L bytes, reducing to the same
593 * performance at 2L bytes.
595 blocks = g0s0->to_blocks;
597 if ( blocks * RtsFlags.GcFlags.oldGenFactor * 2 >
598 RtsFlags.GcFlags.maxHeapSize ) {
599 int adjusted_blocks; /* signed on purpose */
602 adjusted_blocks = (RtsFlags.GcFlags.maxHeapSize - 2 * blocks);
603 IF_DEBUG(gc, fprintf(stderr, "Near maximum heap size of 0x%x blocks, blocks = %d, adjusted to %d\n", RtsFlags.GcFlags.maxHeapSize, blocks, adjusted_blocks));
604 pc_free = adjusted_blocks * 100 / RtsFlags.GcFlags.maxHeapSize;
605 if (pc_free < RtsFlags.GcFlags.pcFreeHeap) /* might even be < 0 */ {
608 blocks = adjusted_blocks;
611 blocks *= RtsFlags.GcFlags.oldGenFactor;
612 if (blocks < RtsFlags.GcFlags.minAllocAreaSize) {
613 blocks = RtsFlags.GcFlags.minAllocAreaSize;
616 resizeNursery(blocks);
619 /* Generational collector:
620 * If the user has given us a suggested heap size, adjust our
621 * allocation area to make best use of the memory available.
624 if (RtsFlags.GcFlags.heapSizeSuggestion) {
626 nat needed = calcNeeded(); /* approx blocks needed at next GC */
628 /* Guess how much will be live in generation 0 step 0 next time.
629 * A good approximation is the obtained by finding the
630 * percentage of g0s0 that was live at the last minor GC.
633 g0s0_pcnt_kept = (new_blocks * 100) / g0s0->n_blocks;
636 /* Estimate a size for the allocation area based on the
637 * information available. We might end up going slightly under
638 * or over the suggested heap size, but we should be pretty
641 * Formula: suggested - needed
642 * ----------------------------
643 * 1 + g0s0_pcnt_kept/100
645 * where 'needed' is the amount of memory needed at the next
646 * collection for collecting all steps except g0s0.
649 (((int)RtsFlags.GcFlags.heapSizeSuggestion - (int)needed) * 100) /
650 (100 + (int)g0s0_pcnt_kept);
652 if (blocks < (int)RtsFlags.GcFlags.minAllocAreaSize) {
653 blocks = RtsFlags.GcFlags.minAllocAreaSize;
656 resizeNursery((nat)blocks);
660 /* mark the garbage collected CAFs as dead */
662 if (major_gc) { gcCAFs(); }
665 /* zero the scavenged static object list */
667 zero_static_object_list(scavenged_static_objects);
672 for (bd = g0s0->blocks; bd; bd = bd->link) {
673 bd->free = bd->start;
674 ASSERT(bd->gen == g0);
675 ASSERT(bd->step == g0s0);
676 IF_DEBUG(sanity,memset(bd->start, 0xaa, BLOCK_SIZE));
678 current_nursery = g0s0->blocks;
680 /* start any pending finalizers */
681 scheduleFinalizers(old_weak_ptr_list);
683 /* check sanity after GC */
684 IF_DEBUG(sanity, checkSanity(N));
686 /* extra GC trace info */
687 IF_DEBUG(gc, stat_describe_gens());
690 /* symbol-table based profiling */
691 /* heapCensus(to_space); */ /* ToDo */
694 /* restore enclosing cost centre */
699 /* check for memory leaks if sanity checking is on */
700 IF_DEBUG(sanity, memInventory());
702 /* ok, GC over: tell the stats department what happened. */
703 stat_endGC(allocated, collected, live, copied, N);
706 /* -----------------------------------------------------------------------------
709 traverse_weak_ptr_list is called possibly many times during garbage
710 collection. It returns a flag indicating whether it did any work
711 (i.e. called evacuate on any live pointers).
713 Invariant: traverse_weak_ptr_list is called when the heap is in an
714 idempotent state. That means that there are no pending
715 evacuate/scavenge operations. This invariant helps the weak
716 pointer code decide which weak pointers are dead - if there are no
717 new live weak pointers, then all the currently unreachable ones are
720 For generational GC: we just don't try to finalize weak pointers in
721 older generations than the one we're collecting. This could
722 probably be optimised by keeping per-generation lists of weak
723 pointers, but for a few weak pointers this scheme will work.
724 -------------------------------------------------------------------------- */
727 traverse_weak_ptr_list(void)
729 StgWeak *w, **last_w, *next_w;
731 rtsBool flag = rtsFalse;
733 if (weak_done) { return rtsFalse; }
735 /* doesn't matter where we evacuate values/finalizers to, since
736 * these pointers are treated as roots (iff the keys are alive).
740 last_w = &old_weak_ptr_list;
741 for (w = old_weak_ptr_list; w; w = next_w) {
743 /* First, this weak pointer might have been evacuated. If so,
744 * remove the forwarding pointer from the weak_ptr_list.
746 if (get_itbl(w)->type == EVACUATED) {
747 w = (StgWeak *)((StgEvacuated *)w)->evacuee;
751 /* There might be a DEAD_WEAK on the list if finalizeWeak# was
752 * called on a live weak pointer object. Just remove it.
754 if (w->header.info == &DEAD_WEAK_info) {
755 next_w = ((StgDeadWeak *)w)->link;
760 ASSERT(get_itbl(w)->type == WEAK);
762 /* Now, check whether the key is reachable.
764 if ((new = isAlive(w->key))) {
766 /* evacuate the value and finalizer */
767 w->value = evacuate(w->value);
768 w->finalizer = evacuate(w->finalizer);
769 /* remove this weak ptr from the old_weak_ptr list */
771 /* and put it on the new weak ptr list */
773 w->link = weak_ptr_list;
776 IF_DEBUG(weak, fprintf(stderr,"Weak pointer still alive at %p -> %p\n", w, w->key));
786 /* If we didn't make any changes, then we can go round and kill all
787 * the dead weak pointers. The old_weak_ptr list is used as a list
788 * of pending finalizers later on.
790 if (flag == rtsFalse) {
791 cleanup_weak_ptr_list(&old_weak_ptr_list);
792 for (w = old_weak_ptr_list; w; w = w->link) {
793 w->finalizer = evacuate(w->finalizer);
801 /* -----------------------------------------------------------------------------
802 After GC, the live weak pointer list may have forwarding pointers
803 on it, because a weak pointer object was evacuated after being
804 moved to the live weak pointer list. We remove those forwarding
807 Also, we don't consider weak pointer objects to be reachable, but
808 we must nevertheless consider them to be "live" and retain them.
809 Therefore any weak pointer objects which haven't as yet been
810 evacuated need to be evacuated now.
811 -------------------------------------------------------------------------- */
814 cleanup_weak_ptr_list ( StgWeak **list )
816 StgWeak *w, **last_w;
819 for (w = *list; w; w = w->link) {
821 if (get_itbl(w)->type == EVACUATED) {
822 w = (StgWeak *)((StgEvacuated *)w)->evacuee;
826 if (Bdescr((P_)w)->evacuated == 0) {
827 (StgClosure *)w = evacuate((StgClosure *)w);
834 /* -----------------------------------------------------------------------------
835 isAlive determines whether the given closure is still alive (after
836 a garbage collection) or not. It returns the new address of the
837 closure if it is alive, or NULL otherwise.
838 -------------------------------------------------------------------------- */
841 isAlive(StgClosure *p)
849 /* ToDo: for static closures, check the static link field.
850 * Problem here is that we sometimes don't set the link field, eg.
851 * for static closures with an empty SRT or CONSTR_STATIC_NOCAFs.
854 /* ignore closures in generations that we're not collecting. */
855 if (LOOKS_LIKE_STATIC(p) || Bdescr((P_)p)->gen->no > N) {
859 switch (info->type) {
864 case IND_OLDGEN: /* rely on compatible layout with StgInd */
865 case IND_OLDGEN_PERM:
866 /* follow indirections */
867 p = ((StgInd *)p)->indirectee;
872 return ((StgEvacuated *)p)->evacuee;
882 MarkRoot(StgClosure *root)
884 return evacuate(root);
887 static void addBlock(step *step)
889 bdescr *bd = allocBlock();
893 if (step->gen->no <= N) {
899 step->hp_bd->free = step->hp;
900 step->hp_bd->link = bd;
901 step->hp = bd->start;
902 step->hpLim = step->hp + BLOCK_SIZE_W;
908 static __inline__ void
909 upd_evacuee(StgClosure *p, StgClosure *dest)
911 p->header.info = &EVACUATED_info;
912 ((StgEvacuated *)p)->evacuee = dest;
915 static __inline__ StgClosure *
916 copy(StgClosure *src, nat size, step *step)
920 TICK_GC_WORDS_COPIED(size);
921 /* Find out where we're going, using the handy "to" pointer in
922 * the step of the source object. If it turns out we need to
923 * evacuate to an older generation, adjust it here (see comment
926 if (step->gen->no < evac_gen) {
927 #ifdef NO_EAGER_PROMOTION
928 failed_to_evac = rtsTrue;
930 step = &generations[evac_gen].steps[0];
934 /* chain a new block onto the to-space for the destination step if
937 if (step->hp + size >= step->hpLim) {
941 for(to = step->hp, from = (P_)src; size>0; --size) {
947 upd_evacuee(src,(StgClosure *)dest);
948 return (StgClosure *)dest;
951 /* Special version of copy() for when we only want to copy the info
952 * pointer of an object, but reserve some padding after it. This is
953 * used to optimise evacuation of BLACKHOLEs.
956 static __inline__ StgClosure *
957 copyPart(StgClosure *src, nat size_to_reserve, nat size_to_copy, step *step)
961 TICK_GC_WORDS_COPIED(size_to_copy);
962 if (step->gen->no < evac_gen) {
963 #ifdef NO_EAGER_PROMOTION
964 failed_to_evac = rtsTrue;
966 step = &generations[evac_gen].steps[0];
970 if (step->hp + size_to_reserve >= step->hpLim) {
974 for(to = step->hp, from = (P_)src; size_to_copy>0; --size_to_copy) {
979 step->hp += size_to_reserve;
980 upd_evacuee(src,(StgClosure *)dest);
981 return (StgClosure *)dest;
984 /* -----------------------------------------------------------------------------
985 Evacuate a large object
987 This just consists of removing the object from the (doubly-linked)
988 large_alloc_list, and linking it on to the (singly-linked)
989 new_large_objects list, from where it will be scavenged later.
991 Convention: bd->evacuated is /= 0 for a large object that has been
992 evacuated, or 0 otherwise.
993 -------------------------------------------------------------------------- */
996 evacuate_large(StgPtr p, rtsBool mutable)
998 bdescr *bd = Bdescr(p);
1001 /* should point to the beginning of the block */
1002 ASSERT(((W_)p & BLOCK_MASK) == 0);
1004 /* already evacuated? */
1005 if (bd->evacuated) {
1006 /* Don't forget to set the failed_to_evac flag if we didn't get
1007 * the desired destination (see comments in evacuate()).
1009 if (bd->gen->no < evac_gen) {
1010 failed_to_evac = rtsTrue;
1011 TICK_GC_FAILED_PROMOTION();
1017 /* remove from large_object list */
1019 bd->back->link = bd->link;
1020 } else { /* first object in the list */
1021 step->large_objects = bd->link;
1024 bd->link->back = bd->back;
1027 /* link it on to the evacuated large object list of the destination step
1029 step = bd->step->to;
1030 if (step->gen->no < evac_gen) {
1031 #ifdef NO_EAGER_PROMOTION
1032 failed_to_evac = rtsTrue;
1034 step = &generations[evac_gen].steps[0];
1039 bd->gen = step->gen;
1040 bd->link = step->new_large_objects;
1041 step->new_large_objects = bd;
1045 recordMutable((StgMutClosure *)p);
1049 /* -----------------------------------------------------------------------------
1050 Adding a MUT_CONS to an older generation.
1052 This is necessary from time to time when we end up with an
1053 old-to-new generation pointer in a non-mutable object. We defer
1054 the promotion until the next GC.
1055 -------------------------------------------------------------------------- */
1058 mkMutCons(StgClosure *ptr, generation *gen)
1063 step = &gen->steps[0];
1065 /* chain a new block onto the to-space for the destination step if
1068 if (step->hp + sizeofW(StgIndOldGen) >= step->hpLim) {
1072 q = (StgMutVar *)step->hp;
1073 step->hp += sizeofW(StgMutVar);
1075 SET_HDR(q,&MUT_CONS_info,CCS_GC);
1077 recordOldToNewPtrs((StgMutClosure *)q);
1079 return (StgClosure *)q;
1082 /* -----------------------------------------------------------------------------
1085 This is called (eventually) for every live object in the system.
1087 The caller to evacuate specifies a desired generation in the
1088 evac_gen global variable. The following conditions apply to
1089 evacuating an object which resides in generation M when we're
1090 collecting up to generation N
1094 else evac to step->to
1096 if M < evac_gen evac to evac_gen, step 0
1098 if the object is already evacuated, then we check which generation
1101 if M >= evac_gen do nothing
1102 if M < evac_gen set failed_to_evac flag to indicate that we
1103 didn't manage to evacuate this object into evac_gen.
1105 -------------------------------------------------------------------------- */
1109 evacuate(StgClosure *q)
1114 const StgInfoTable *info;
1117 if (HEAP_ALLOCED(q)) {
1119 if (bd->gen->no > N) {
1120 /* Can't evacuate this object, because it's in a generation
1121 * older than the ones we're collecting. Let's hope that it's
1122 * in evac_gen or older, or we will have to make an IND_OLDGEN object.
1124 if (bd->gen->no < evac_gen) {
1126 failed_to_evac = rtsTrue;
1127 TICK_GC_FAILED_PROMOTION();
1131 step = bd->step->to;
1134 /* make sure the info pointer is into text space */
1135 ASSERT(q && (LOOKS_LIKE_GHC_INFO(GET_INFO(q))
1136 || IS_HUGS_CONSTR_INFO(GET_INFO(q))));
1139 switch (info -> type) {
1142 return copy(q,bco_sizeW(stgCast(StgBCO*,q)),step);
1145 ASSERT(q->header.info != &MUT_CONS_info);
1147 to = copy(q,sizeW_fromITBL(info),step);
1148 recordMutable((StgMutClosure *)to);
1155 return copy(q,sizeofW(StgHeader)+1,step);
1157 case THUNK_1_0: /* here because of MIN_UPD_SIZE */
1162 #ifdef NO_PROMOTE_THUNKS
1163 if (bd->gen->no == 0 &&
1164 bd->step->no != 0 &&
1165 bd->step->no == bd->gen->n_steps-1) {
1169 return copy(q,sizeofW(StgHeader)+2,step);
1177 return copy(q,sizeofW(StgHeader)+2,step);
1183 case IND_OLDGEN_PERM:
1189 return copy(q,sizeW_fromITBL(info),step);
1193 return copyPart(q,BLACKHOLE_sizeW(),sizeofW(StgHeader),step);
1196 to = copy(q,BLACKHOLE_sizeW(),step);
1197 recordMutable((StgMutClosure *)to);
1200 case THUNK_SELECTOR:
1202 const StgInfoTable* selectee_info;
1203 StgClosure* selectee = ((StgSelector*)q)->selectee;
1206 selectee_info = get_itbl(selectee);
1207 switch (selectee_info->type) {
1216 StgWord32 offset = info->layout.selector_offset;
1218 /* check that the size is in range */
1220 (StgWord32)(selectee_info->layout.payload.ptrs +
1221 selectee_info->layout.payload.nptrs));
1223 /* perform the selection! */
1224 q = selectee->payload[offset];
1226 /* if we're already in to-space, there's no need to continue
1227 * with the evacuation, just update the source address with
1228 * a pointer to the (evacuated) constructor field.
1230 if (HEAP_ALLOCED(q)) {
1231 bdescr *bd = Bdescr((P_)q);
1232 if (bd->evacuated) {
1233 if (bd->gen->no < evac_gen) {
1234 failed_to_evac = rtsTrue;
1235 TICK_GC_FAILED_PROMOTION();
1241 /* otherwise, carry on and evacuate this constructor field,
1242 * (but not the constructor itself)
1251 case IND_OLDGEN_PERM:
1252 selectee = stgCast(StgInd *,selectee)->indirectee;
1256 selectee = stgCast(StgCAF *,selectee)->value;
1260 selectee = stgCast(StgEvacuated*,selectee)->evacuee;
1270 case THUNK_SELECTOR:
1271 /* aargh - do recursively???? */
1276 /* not evaluated yet */
1280 barf("evacuate: THUNK_SELECTOR: strange selectee");
1283 return copy(q,THUNK_SELECTOR_sizeW(),step);
1287 /* follow chains of indirections, don't evacuate them */
1288 q = ((StgInd*)q)->indirectee;
1291 /* ToDo: optimise STATIC_LINK for known cases.
1292 - FUN_STATIC : payload[0]
1293 - THUNK_STATIC : payload[1]
1294 - IND_STATIC : payload[1]
1298 if (info->srt_len == 0) { /* small optimisation */
1304 /* don't want to evacuate these, but we do want to follow pointers
1305 * from SRTs - see scavenge_static.
1308 /* put the object on the static list, if necessary.
1310 if (major_gc && STATIC_LINK(info,(StgClosure *)q) == NULL) {
1311 STATIC_LINK(info,(StgClosure *)q) = static_objects;
1312 static_objects = (StgClosure *)q;
1316 case CONSTR_INTLIKE:
1317 case CONSTR_CHARLIKE:
1318 case CONSTR_NOCAF_STATIC:
1319 /* no need to put these on the static linked list, they don't need
1334 /* shouldn't see these */
1335 barf("evacuate: stack frame\n");
1339 /* these are special - the payload is a copy of a chunk of stack,
1341 return copy(q,pap_sizeW(stgCast(StgPAP*,q)),step);
1344 /* Already evacuated, just return the forwarding address.
1345 * HOWEVER: if the requested destination generation (evac_gen) is
1346 * older than the actual generation (because the object was
1347 * already evacuated to a younger generation) then we have to
1348 * set the failed_to_evac flag to indicate that we couldn't
1349 * manage to promote the object to the desired generation.
1351 if (evac_gen > 0) { /* optimisation */
1352 StgClosure *p = ((StgEvacuated*)q)->evacuee;
1353 if (Bdescr((P_)p)->gen->no < evac_gen) {
1354 /* fprintf(stderr,"evac failed!\n");*/
1355 failed_to_evac = rtsTrue;
1356 TICK_GC_FAILED_PROMOTION();
1359 return ((StgEvacuated*)q)->evacuee;
1363 nat size = arr_words_sizeW(stgCast(StgArrWords*,q));
1365 if (size >= LARGE_OBJECT_THRESHOLD/sizeof(W_)) {
1366 evacuate_large((P_)q, rtsFalse);
1369 /* just copy the block */
1370 return copy(q,size,step);
1375 case MUT_ARR_PTRS_FROZEN:
1377 nat size = mut_arr_ptrs_sizeW(stgCast(StgMutArrPtrs*,q));
1379 if (size >= LARGE_OBJECT_THRESHOLD/sizeof(W_)) {
1380 evacuate_large((P_)q, info->type == MUT_ARR_PTRS);
1383 /* just copy the block */
1384 to = copy(q,size,step);
1385 if (info->type == MUT_ARR_PTRS) {
1386 recordMutable((StgMutClosure *)to);
1394 StgTSO *tso = stgCast(StgTSO *,q);
1395 nat size = tso_sizeW(tso);
1398 /* Large TSOs don't get moved, so no relocation is required.
1400 if (size >= LARGE_OBJECT_THRESHOLD/sizeof(W_)) {
1401 evacuate_large((P_)q, rtsTrue);
1404 /* To evacuate a small TSO, we need to relocate the update frame
1408 StgTSO *new_tso = (StgTSO *)copy((StgClosure *)tso,tso_sizeW(tso),step);
1410 diff = (StgPtr)new_tso - (StgPtr)tso; /* In *words* */
1412 /* relocate the stack pointers... */
1413 new_tso->su = (StgUpdateFrame *) ((StgPtr)new_tso->su + diff);
1414 new_tso->sp = (StgPtr)new_tso->sp + diff;
1415 new_tso->splim = (StgPtr)new_tso->splim + diff;
1417 relocate_TSO(tso, new_tso);
1419 recordMutable((StgMutClosure *)new_tso);
1420 return (StgClosure *)new_tso;
1426 fprintf(stderr,"evacuate: unimplemented/strange closure type\n");
1430 barf("evacuate: strange closure type");
1436 /* -----------------------------------------------------------------------------
1437 relocate_TSO is called just after a TSO has been copied from src to
1438 dest. It adjusts the update frame list for the new location.
1439 -------------------------------------------------------------------------- */
1442 relocate_TSO(StgTSO *src, StgTSO *dest)
1449 diff = (StgPtr)dest->sp - (StgPtr)src->sp; /* In *words* */
1453 while ((P_)su < dest->stack + dest->stack_size) {
1454 switch (get_itbl(su)->type) {
1456 /* GCC actually manages to common up these three cases! */
1459 su->link = (StgUpdateFrame *) ((StgPtr)su->link + diff);
1464 cf = (StgCatchFrame *)su;
1465 cf->link = (StgUpdateFrame *) ((StgPtr)cf->link + diff);
1470 sf = (StgSeqFrame *)su;
1471 sf->link = (StgUpdateFrame *) ((StgPtr)sf->link + diff);
1480 barf("relocate_TSO");
1489 scavenge_srt(const StgInfoTable *info)
1491 StgClosure **srt, **srt_end;
1493 /* evacuate the SRT. If srt_len is zero, then there isn't an
1494 * srt field in the info table. That's ok, because we'll
1495 * never dereference it.
1497 srt = stgCast(StgClosure **,info->srt);
1498 srt_end = srt + info->srt_len;
1499 for (; srt < srt_end; srt++) {
1500 /* Special-case to handle references to closures hiding out in DLLs, since
1501 double indirections required to get at those. The code generator knows
1502 which is which when generating the SRT, so it stores the (indirect)
1503 reference to the DLL closure in the table by first adding one to it.
1504 We check for this here, and undo the addition before evacuating it.
1506 If the SRT entry hasn't got bit 0 set, the SRT entry points to a
1507 closure that's fixed at link-time, and no extra magic is required.
1509 #ifdef HAVE_WIN32_DLL_SUPPORT
1510 if ( stgCast(unsigned long,*srt) & 0x1 ) {
1511 evacuate(*stgCast(StgClosure**,(stgCast(unsigned long, *srt) & ~0x1)));
1521 /* -----------------------------------------------------------------------------
1522 Scavenge a given step until there are no more objects in this step
1525 evac_gen is set by the caller to be either zero (for a step in a
1526 generation < N) or G where G is the generation of the step being
1529 We sometimes temporarily change evac_gen back to zero if we're
1530 scavenging a mutable object where early promotion isn't such a good
1532 -------------------------------------------------------------------------- */
1536 scavenge(step *step)
1539 const StgInfoTable *info;
1541 nat saved_evac_gen = evac_gen; /* used for temporarily changing evac_gen */
1546 failed_to_evac = rtsFalse;
1548 /* scavenge phase - standard breadth-first scavenging of the
1552 while (bd != step->hp_bd || p < step->hp) {
1554 /* If we're at the end of this block, move on to the next block */
1555 if (bd != step->hp_bd && p == bd->free) {
1561 q = p; /* save ptr to object */
1563 ASSERT(p && (LOOKS_LIKE_GHC_INFO(GET_INFO((StgClosure *)p))
1564 || IS_HUGS_CONSTR_INFO(GET_INFO((StgClosure *)p))));
1566 info = get_itbl((StgClosure *)p);
1567 switch (info -> type) {
1571 StgBCO* bco = stgCast(StgBCO*,p);
1573 for (i = 0; i < bco->n_ptrs; i++) {
1574 bcoConstCPtr(bco,i) = evacuate(bcoConstCPtr(bco,i));
1576 p += bco_sizeW(bco);
1581 /* treat MVars specially, because we don't want to evacuate the
1582 * mut_link field in the middle of the closure.
1585 StgMVar *mvar = ((StgMVar *)p);
1587 (StgClosure *)mvar->head = evacuate((StgClosure *)mvar->head);
1588 (StgClosure *)mvar->tail = evacuate((StgClosure *)mvar->tail);
1589 (StgClosure *)mvar->value = evacuate((StgClosure *)mvar->value);
1590 p += sizeofW(StgMVar);
1591 evac_gen = saved_evac_gen;
1599 ((StgClosure *)p)->payload[1] = evacuate(((StgClosure *)p)->payload[1]);
1600 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
1601 p += sizeofW(StgHeader) + 2;
1606 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
1607 p += sizeofW(StgHeader) + 2; /* MIN_UPD_SIZE */
1613 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
1614 p += sizeofW(StgHeader) + 1;
1619 p += sizeofW(StgHeader) + 2; /* MIN_UPD_SIZE */
1625 p += sizeofW(StgHeader) + 1;
1632 p += sizeofW(StgHeader) + 2;
1639 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
1640 p += sizeofW(StgHeader) + 2;
1653 case IND_OLDGEN_PERM:
1658 end = (P_)((StgClosure *)p)->payload + info->layout.payload.ptrs;
1659 for (p = (P_)((StgClosure *)p)->payload; p < end; p++) {
1660 (StgClosure *)*p = evacuate((StgClosure *)*p);
1662 p += info->layout.payload.nptrs;
1668 StgCAF *caf = (StgCAF *)p;
1670 caf->body = evacuate(caf->body);
1671 caf->value = evacuate(caf->value);
1672 if (failed_to_evac) {
1673 failed_to_evac = rtsFalse;
1674 recordOldToNewPtrs((StgMutClosure *)p);
1676 p += sizeofW(StgCAF);
1681 /* ignore MUT_CONSs */
1682 if (((StgMutVar *)p)->header.info != &MUT_CONS_info) {
1684 ((StgMutVar *)p)->var = evacuate(((StgMutVar *)p)->var);
1685 evac_gen = saved_evac_gen;
1687 p += sizeofW(StgMutVar);
1692 p += BLACKHOLE_sizeW();
1697 StgBlockingQueue *bh = (StgBlockingQueue *)p;
1698 (StgClosure *)bh->blocking_queue =
1699 evacuate((StgClosure *)bh->blocking_queue);
1700 if (failed_to_evac) {
1701 failed_to_evac = rtsFalse;
1702 recordMutable((StgMutClosure *)bh);
1704 p += BLACKHOLE_sizeW();
1708 case THUNK_SELECTOR:
1710 StgSelector *s = (StgSelector *)p;
1711 s->selectee = evacuate(s->selectee);
1712 p += THUNK_SELECTOR_sizeW();
1718 barf("scavenge:IND???\n");
1720 case CONSTR_INTLIKE:
1721 case CONSTR_CHARLIKE:
1723 case CONSTR_NOCAF_STATIC:
1727 /* Shouldn't see a static object here. */
1728 barf("scavenge: STATIC object\n");
1740 /* Shouldn't see stack frames here. */
1741 barf("scavenge: stack frame\n");
1743 case AP_UPD: /* same as PAPs */
1745 /* Treat a PAP just like a section of stack, not forgetting to
1746 * evacuate the function pointer too...
1749 StgPAP* pap = stgCast(StgPAP*,p);
1751 pap->fun = evacuate(pap->fun);
1752 scavenge_stack((P_)pap->payload, (P_)pap->payload + pap->n_args);
1753 p += pap_sizeW(pap);
1758 /* nothing to follow */
1759 p += arr_words_sizeW(stgCast(StgArrWords*,p));
1763 /* follow everything */
1767 evac_gen = 0; /* repeatedly mutable */
1768 next = p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
1769 for (p = (P_)((StgMutArrPtrs *)p)->payload; p < next; p++) {
1770 (StgClosure *)*p = evacuate((StgClosure *)*p);
1772 evac_gen = saved_evac_gen;
1776 case MUT_ARR_PTRS_FROZEN:
1777 /* follow everything */
1779 StgPtr start = p, next;
1781 next = p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
1782 for (p = (P_)((StgMutArrPtrs *)p)->payload; p < next; p++) {
1783 (StgClosure *)*p = evacuate((StgClosure *)*p);
1785 if (failed_to_evac) {
1786 /* we can do this easier... */
1787 recordMutable((StgMutClosure *)start);
1788 failed_to_evac = rtsFalse;
1799 /* chase the link field for any TSOs on the same queue */
1800 (StgClosure *)tso->link = evacuate((StgClosure *)tso->link);
1801 /* scavenge this thread's stack */
1802 scavenge_stack(tso->sp, &(tso->stack[tso->stack_size]));
1803 evac_gen = saved_evac_gen;
1804 p += tso_sizeW(tso);
1811 barf("scavenge: unimplemented/strange closure type\n");
1817 /* If we didn't manage to promote all the objects pointed to by
1818 * the current object, then we have to designate this object as
1819 * mutable (because it contains old-to-new generation pointers).
1821 if (failed_to_evac) {
1822 mkMutCons((StgClosure *)q, &generations[evac_gen]);
1823 failed_to_evac = rtsFalse;
1831 /* -----------------------------------------------------------------------------
1832 Scavenge one object.
1834 This is used for objects that are temporarily marked as mutable
1835 because they contain old-to-new generation pointers. Only certain
1836 objects can have this property.
1837 -------------------------------------------------------------------------- */
1839 scavenge_one(StgClosure *p)
1844 ASSERT(p && (LOOKS_LIKE_GHC_INFO(GET_INFO(p))
1845 || IS_HUGS_CONSTR_INFO(GET_INFO(p))));
1849 switch (info -> type) {
1852 case FUN_1_0: /* hardly worth specialising these guys */
1872 case IND_OLDGEN_PERM:
1877 end = (P_)p->payload + info->layout.payload.ptrs;
1878 for (q = (P_)p->payload; q < end; q++) {
1879 (StgClosure *)*q = evacuate((StgClosure *)*q);
1888 case THUNK_SELECTOR:
1890 StgSelector *s = (StgSelector *)p;
1891 s->selectee = evacuate(s->selectee);
1895 case AP_UPD: /* same as PAPs */
1897 /* Treat a PAP just like a section of stack, not forgetting to
1898 * evacuate the function pointer too...
1901 StgPAP* pap = (StgPAP *)p;
1903 pap->fun = evacuate(pap->fun);
1904 scavenge_stack((P_)pap->payload, (P_)pap->payload + pap->n_args);
1909 /* This might happen if for instance a MUT_CONS was pointing to a
1910 * THUNK which has since been updated. The IND_OLDGEN will
1911 * be on the mutable list anyway, so we don't need to do anything
1917 barf("scavenge_one: strange object");
1920 no_luck = failed_to_evac;
1921 failed_to_evac = rtsFalse;
1926 /* -----------------------------------------------------------------------------
1927 Scavenging mutable lists.
1929 We treat the mutable list of each generation > N (i.e. all the
1930 generations older than the one being collected) as roots. We also
1931 remove non-mutable objects from the mutable list at this point.
1932 -------------------------------------------------------------------------- */
1935 scavenge_mut_once_list(generation *gen)
1938 StgMutClosure *p, *next, *new_list;
1940 p = gen->mut_once_list;
1941 new_list = END_MUT_LIST;
1945 failed_to_evac = rtsFalse;
1947 for (; p != END_MUT_LIST; p = next, next = p->mut_link) {
1949 /* make sure the info pointer is into text space */
1950 ASSERT(p && (LOOKS_LIKE_GHC_INFO(GET_INFO(p))
1951 || IS_HUGS_CONSTR_INFO(GET_INFO(p))));
1954 switch(info->type) {
1957 case IND_OLDGEN_PERM:
1959 /* Try to pull the indirectee into this generation, so we can
1960 * remove the indirection from the mutable list.
1962 ((StgIndOldGen *)p)->indirectee =
1963 evacuate(((StgIndOldGen *)p)->indirectee);
1966 /* Debugging code to print out the size of the thing we just
1970 StgPtr start = gen->steps[0].scan;
1971 bdescr *start_bd = gen->steps[0].scan_bd;
1973 scavenge(&gen->steps[0]);
1974 if (start_bd != gen->steps[0].scan_bd) {
1975 size += (P_)BLOCK_ROUND_UP(start) - start;
1976 start_bd = start_bd->link;
1977 while (start_bd != gen->steps[0].scan_bd) {
1978 size += BLOCK_SIZE_W;
1979 start_bd = start_bd->link;
1981 size += gen->steps[0].scan -
1982 (P_)BLOCK_ROUND_DOWN(gen->steps[0].scan);
1984 size = gen->steps[0].scan - start;
1986 fprintf(stderr,"evac IND_OLDGEN: %d bytes\n", size * sizeof(W_));
1990 /* failed_to_evac might happen if we've got more than two
1991 * generations, we're collecting only generation 0, the
1992 * indirection resides in generation 2 and the indirectee is
1995 if (failed_to_evac) {
1996 failed_to_evac = rtsFalse;
1997 p->mut_link = new_list;
2000 /* the mut_link field of an IND_STATIC is overloaded as the
2001 * static link field too (it just so happens that we don't need
2002 * both at the same time), so we need to NULL it out when
2003 * removing this object from the mutable list because the static
2004 * link fields are all assumed to be NULL before doing a major
2012 /* MUT_CONS is a kind of MUT_VAR, except it that we try to remove
2013 * it from the mutable list if possible by promoting whatever it
2016 ASSERT(p->header.info == &MUT_CONS_info);
2017 if (scavenge_one(((StgMutVar *)p)->var) == rtsTrue) {
2018 /* didn't manage to promote everything, so put the
2019 * MUT_CONS back on the list.
2021 p->mut_link = new_list;
2028 StgCAF *caf = (StgCAF *)p;
2029 caf->body = evacuate(caf->body);
2030 caf->value = evacuate(caf->value);
2031 if (failed_to_evac) {
2032 failed_to_evac = rtsFalse;
2033 p->mut_link = new_list;
2041 StgCAF *caf = (StgCAF *)p;
2042 caf->body = evacuate(caf->body);
2043 if (failed_to_evac) {
2044 failed_to_evac = rtsFalse;
2045 p->mut_link = new_list;
2052 /* shouldn't have anything else on the mutables list */
2053 barf("scavenge_mut_once_list: strange object?");
2057 gen->mut_once_list = new_list;
2062 scavenge_mutable_list(generation *gen)
2065 StgMutClosure *p, *next;
2067 p = gen->saved_mut_list;
2071 failed_to_evac = rtsFalse;
2073 for (; p != END_MUT_LIST; p = next, next = p->mut_link) {
2075 /* make sure the info pointer is into text space */
2076 ASSERT(p && (LOOKS_LIKE_GHC_INFO(GET_INFO(p))
2077 || IS_HUGS_CONSTR_INFO(GET_INFO(p))));
2080 switch(info->type) {
2082 case MUT_ARR_PTRS_FROZEN:
2083 /* remove this guy from the mutable list, but follow the ptrs
2084 * anyway (and make sure they get promoted to this gen).
2089 end = (P_)p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
2091 for (q = (P_)((StgMutArrPtrs *)p)->payload; q < end; q++) {
2092 (StgClosure *)*q = evacuate((StgClosure *)*q);
2096 if (failed_to_evac) {
2097 failed_to_evac = rtsFalse;
2098 p->mut_link = gen->mut_list;
2105 /* follow everything */
2106 p->mut_link = gen->mut_list;
2111 end = (P_)p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
2112 for (q = (P_)((StgMutArrPtrs *)p)->payload; q < end; q++) {
2113 (StgClosure *)*q = evacuate((StgClosure *)*q);
2119 /* MUT_CONS is a kind of MUT_VAR, except that we try to remove
2120 * it from the mutable list if possible by promoting whatever it
2123 ASSERT(p->header.info != &MUT_CONS_info);
2124 ((StgMutVar *)p)->var = evacuate(((StgMutVar *)p)->var);
2125 p->mut_link = gen->mut_list;
2131 StgMVar *mvar = (StgMVar *)p;
2132 (StgClosure *)mvar->head = evacuate((StgClosure *)mvar->head);
2133 (StgClosure *)mvar->tail = evacuate((StgClosure *)mvar->tail);
2134 (StgClosure *)mvar->value = evacuate((StgClosure *)mvar->value);
2135 p->mut_link = gen->mut_list;
2141 /* follow ptrs and remove this from the mutable list */
2143 StgTSO *tso = (StgTSO *)p;
2145 /* Don't bother scavenging if this thread is dead
2147 if (!(tso->whatNext == ThreadComplete ||
2148 tso->whatNext == ThreadKilled)) {
2149 /* Don't need to chase the link field for any TSOs on the
2150 * same queue. Just scavenge this thread's stack
2152 scavenge_stack(tso->sp, &(tso->stack[tso->stack_size]));
2155 /* Don't take this TSO off the mutable list - it might still
2156 * point to some younger objects (because we set evac_gen to 0
2159 tso->mut_link = gen->mut_list;
2160 gen->mut_list = (StgMutClosure *)tso;
2166 StgBlockingQueue *bh = (StgBlockingQueue *)p;
2167 (StgClosure *)bh->blocking_queue =
2168 evacuate((StgClosure *)bh->blocking_queue);
2169 p->mut_link = gen->mut_list;
2175 /* shouldn't have anything else on the mutables list */
2176 barf("scavenge_mut_list: strange object?");
2182 scavenge_static(void)
2184 StgClosure* p = static_objects;
2185 const StgInfoTable *info;
2187 /* Always evacuate straight to the oldest generation for static
2189 evac_gen = oldest_gen->no;
2191 /* keep going until we've scavenged all the objects on the linked
2193 while (p != END_OF_STATIC_LIST) {
2197 /* make sure the info pointer is into text space */
2198 ASSERT(p && (LOOKS_LIKE_GHC_INFO(GET_INFO(p))
2199 || IS_HUGS_CONSTR_INFO(GET_INFO(p))));
2201 /* Take this object *off* the static_objects list,
2202 * and put it on the scavenged_static_objects list.
2204 static_objects = STATIC_LINK(info,p);
2205 STATIC_LINK(info,p) = scavenged_static_objects;
2206 scavenged_static_objects = p;
2208 switch (info -> type) {
2212 StgInd *ind = (StgInd *)p;
2213 ind->indirectee = evacuate(ind->indirectee);
2215 /* might fail to evacuate it, in which case we have to pop it
2216 * back on the mutable list (and take it off the
2217 * scavenged_static list because the static link and mut link
2218 * pointers are one and the same).
2220 if (failed_to_evac) {
2221 failed_to_evac = rtsFalse;
2222 scavenged_static_objects = STATIC_LINK(info,p);
2223 ((StgMutClosure *)ind)->mut_link = oldest_gen->mut_once_list;
2224 oldest_gen->mut_once_list = (StgMutClosure *)ind;
2238 next = (P_)p->payload + info->layout.payload.ptrs;
2239 /* evacuate the pointers */
2240 for (q = (P_)p->payload; q < next; q++) {
2241 (StgClosure *)*q = evacuate((StgClosure *)*q);
2247 barf("scavenge_static");
2250 ASSERT(failed_to_evac == rtsFalse);
2252 /* get the next static object from the list. Remeber, there might
2253 * be more stuff on this list now that we've done some evacuating!
2254 * (static_objects is a global)
2260 /* -----------------------------------------------------------------------------
2261 scavenge_stack walks over a section of stack and evacuates all the
2262 objects pointed to by it. We can use the same code for walking
2263 PAPs, since these are just sections of copied stack.
2264 -------------------------------------------------------------------------- */
2267 scavenge_stack(StgPtr p, StgPtr stack_end)
2270 const StgInfoTable* info;
2274 * Each time around this loop, we are looking at a chunk of stack
2275 * that starts with either a pending argument section or an
2276 * activation record.
2279 while (p < stack_end) {
2280 q = *stgCast(StgPtr*,p);
2282 /* If we've got a tag, skip over that many words on the stack */
2283 if (IS_ARG_TAG(stgCast(StgWord,q))) {
2288 /* Is q a pointer to a closure?
2291 if (! LOOKS_LIKE_GHC_INFO(q)) {
2293 if ( 0 && LOOKS_LIKE_STATIC_CLOSURE(q) ) { /* Is it a static closure? */
2294 ASSERT(closure_STATIC(stgCast(StgClosure*,q)));
2296 /* otherwise, must be a pointer into the allocation space. */
2299 (StgClosure *)*p = evacuate((StgClosure *)q);
2305 * Otherwise, q must be the info pointer of an activation
2306 * record. All activation records have 'bitmap' style layout
2309 info = get_itbl(stgCast(StgClosure*,p));
2311 switch (info->type) {
2313 /* Dynamic bitmap: the mask is stored on the stack */
2315 bitmap = stgCast(StgRetDyn*,p)->liveness;
2316 p = &payloadWord(stgCast(StgRetDyn*,p),0);
2319 /* probably a slow-entry point return address: */
2325 /* Specialised code for update frames, since they're so common.
2326 * We *know* the updatee points to a BLACKHOLE, CAF_BLACKHOLE,
2327 * or BLACKHOLE_BQ, so just inline the code to evacuate it here.
2331 StgUpdateFrame *frame = (StgUpdateFrame *)p;
2333 nat type = get_itbl(frame->updatee)->type;
2335 p += sizeofW(StgUpdateFrame);
2336 if (type == EVACUATED) {
2337 frame->updatee = evacuate(frame->updatee);
2340 bdescr *bd = Bdescr((P_)frame->updatee);
2342 if (bd->gen->no > N) {
2343 if (bd->gen->no < evac_gen) {
2344 failed_to_evac = rtsTrue;
2349 /* Don't promote blackholes */
2351 if (!(step->gen->no == 0 &&
2353 step->no == step->gen->n_steps-1)) {
2360 to = copyPart(frame->updatee, BLACKHOLE_sizeW(),
2361 sizeofW(StgHeader), step);
2362 frame->updatee = to;
2365 to = copy(frame->updatee, BLACKHOLE_sizeW(), step);
2366 frame->updatee = to;
2367 recordMutable((StgMutClosure *)to);
2370 barf("scavenge_stack: UPDATE_FRAME updatee");
2375 /* small bitmap (< 32 entries, or 64 on a 64-bit machine) */
2382 bitmap = info->layout.bitmap;
2385 while (bitmap != 0) {
2386 if ((bitmap & 1) == 0) {
2387 (StgClosure *)*p = evacuate((StgClosure *)*p);
2390 bitmap = bitmap >> 1;
2397 /* large bitmap (> 32 entries) */
2402 StgLargeBitmap *large_bitmap;
2405 large_bitmap = info->layout.large_bitmap;
2408 for (i=0; i<large_bitmap->size; i++) {
2409 bitmap = large_bitmap->bitmap[i];
2410 q = p + sizeof(W_) * 8;
2411 while (bitmap != 0) {
2412 if ((bitmap & 1) == 0) {
2413 (StgClosure *)*p = evacuate((StgClosure *)*p);
2416 bitmap = bitmap >> 1;
2418 if (i+1 < large_bitmap->size) {
2420 (StgClosure *)*p = evacuate((StgClosure *)*p);
2426 /* and don't forget to follow the SRT */
2431 barf("scavenge_stack: weird activation record found on stack.\n");
2436 /*-----------------------------------------------------------------------------
2437 scavenge the large object list.
2439 evac_gen set by caller; similar games played with evac_gen as with
2440 scavenge() - see comment at the top of scavenge(). Most large
2441 objects are (repeatedly) mutable, so most of the time evac_gen will
2443 --------------------------------------------------------------------------- */
2446 scavenge_large(step *step)
2450 const StgInfoTable* info;
2451 nat saved_evac_gen = evac_gen; /* used for temporarily changing evac_gen */
2453 evac_gen = 0; /* most objects are mutable */
2454 bd = step->new_large_objects;
2456 for (; bd != NULL; bd = step->new_large_objects) {
2458 /* take this object *off* the large objects list and put it on
2459 * the scavenged large objects list. This is so that we can
2460 * treat new_large_objects as a stack and push new objects on
2461 * the front when evacuating.
2463 step->new_large_objects = bd->link;
2464 dbl_link_onto(bd, &step->scavenged_large_objects);
2467 info = get_itbl(stgCast(StgClosure*,p));
2469 switch (info->type) {
2471 /* only certain objects can be "large"... */
2474 /* nothing to follow */
2478 /* follow everything */
2482 next = p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
2483 for (p = (P_)((StgMutArrPtrs *)p)->payload; p < next; p++) {
2484 (StgClosure *)*p = evacuate((StgClosure *)*p);
2489 case MUT_ARR_PTRS_FROZEN:
2490 /* follow everything */
2492 StgPtr start = p, next;
2494 evac_gen = saved_evac_gen; /* not really mutable */
2495 next = p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
2496 for (p = (P_)((StgMutArrPtrs *)p)->payload; p < next; p++) {
2497 (StgClosure *)*p = evacuate((StgClosure *)*p);
2500 if (failed_to_evac) {
2501 recordMutable((StgMutClosure *)start);
2508 StgBCO* bco = stgCast(StgBCO*,p);
2510 evac_gen = saved_evac_gen;
2511 for (i = 0; i < bco->n_ptrs; i++) {
2512 bcoConstCPtr(bco,i) = evacuate(bcoConstCPtr(bco,i));
2523 /* chase the link field for any TSOs on the same queue */
2524 (StgClosure *)tso->link = evacuate((StgClosure *)tso->link);
2525 /* scavenge this thread's stack */
2526 scavenge_stack(tso->sp, &(tso->stack[tso->stack_size]));
2531 barf("scavenge_large: unknown/strange object");
2537 zero_static_object_list(StgClosure* first_static)
2541 const StgInfoTable *info;
2543 for (p = first_static; p != END_OF_STATIC_LIST; p = link) {
2545 link = STATIC_LINK(info, p);
2546 STATIC_LINK(info,p) = NULL;
2550 /* This function is only needed because we share the mutable link
2551 * field with the static link field in an IND_STATIC, so we have to
2552 * zero the mut_link field before doing a major GC, which needs the
2553 * static link field.
2555 * It doesn't do any harm to zero all the mutable link fields on the
2559 zero_mutable_list( StgMutClosure *first )
2561 StgMutClosure *next, *c;
2563 for (c = first; c != END_MUT_LIST; c = next) {
2569 /* -----------------------------------------------------------------------------
2571 -------------------------------------------------------------------------- */
2573 void RevertCAFs(void)
2575 while (enteredCAFs != END_CAF_LIST) {
2576 StgCAF* caf = enteredCAFs;
2578 enteredCAFs = caf->link;
2579 ASSERT(get_itbl(caf)->type == CAF_ENTERED);
2580 SET_INFO(caf,&CAF_UNENTERED_info);
2581 caf->value = stgCast(StgClosure*,0xdeadbeef);
2582 caf->link = stgCast(StgCAF*,0xdeadbeef);
2584 enteredCAFs = END_CAF_LIST;
2587 void revert_dead_CAFs(void)
2589 StgCAF* caf = enteredCAFs;
2590 enteredCAFs = END_CAF_LIST;
2591 while (caf != END_CAF_LIST) {
2594 new = (StgCAF*)isAlive((StgClosure*)caf);
2596 new->link = enteredCAFs;
2600 SET_INFO(caf,&CAF_UNENTERED_info);
2601 caf->value = (StgClosure*)0xdeadbeef;
2602 caf->link = (StgCAF*)0xdeadbeef;
2608 /* -----------------------------------------------------------------------------
2609 Sanity code for CAF garbage collection.
2611 With DEBUG turned on, we manage a CAF list in addition to the SRT
2612 mechanism. After GC, we run down the CAF list and blackhole any
2613 CAFs which have been garbage collected. This means we get an error
2614 whenever the program tries to enter a garbage collected CAF.
2616 Any garbage collected CAFs are taken off the CAF list at the same
2618 -------------------------------------------------------------------------- */
2626 const StgInfoTable *info;
2637 ASSERT(info->type == IND_STATIC);
2639 if (STATIC_LINK(info,p) == NULL) {
2640 IF_DEBUG(gccafs, fprintf(stderr, "CAF gc'd at 0x%04x\n", (int)p));
2642 SET_INFO(p,&BLACKHOLE_info);
2643 p = STATIC_LINK2(info,p);
2647 pp = &STATIC_LINK2(info,p);
2654 /* fprintf(stderr, "%d CAFs live\n", i); */
2658 /* -----------------------------------------------------------------------------
2661 Whenever a thread returns to the scheduler after possibly doing
2662 some work, we have to run down the stack and black-hole all the
2663 closures referred to by update frames.
2664 -------------------------------------------------------------------------- */
2667 threadLazyBlackHole(StgTSO *tso)
2669 StgUpdateFrame *update_frame;
2670 StgBlockingQueue *bh;
2673 stack_end = &tso->stack[tso->stack_size];
2674 update_frame = tso->su;
2677 switch (get_itbl(update_frame)->type) {
2680 update_frame = stgCast(StgCatchFrame*,update_frame)->link;
2684 bh = (StgBlockingQueue *)update_frame->updatee;
2686 /* if the thunk is already blackholed, it means we've also
2687 * already blackholed the rest of the thunks on this stack,
2688 * so we can stop early.
2690 * The blackhole made for a CAF is a CAF_BLACKHOLE, so they
2691 * don't interfere with this optimisation.
2693 if (bh->header.info == &BLACKHOLE_info) {
2697 if (bh->header.info != &BLACKHOLE_BQ_info &&
2698 bh->header.info != &CAF_BLACKHOLE_info) {
2699 SET_INFO(bh,&BLACKHOLE_info);
2702 update_frame = update_frame->link;
2706 update_frame = stgCast(StgSeqFrame*,update_frame)->link;
2712 barf("threadPaused");
2717 /* -----------------------------------------------------------------------------
2720 * Code largely pinched from old RTS, then hacked to bits. We also do
2721 * lazy black holing here.
2723 * -------------------------------------------------------------------------- */
2726 threadSqueezeStack(StgTSO *tso)
2728 lnat displacement = 0;
2729 StgUpdateFrame *frame;
2730 StgUpdateFrame *next_frame; /* Temporally next */
2731 StgUpdateFrame *prev_frame; /* Temporally previous */
2733 rtsBool prev_was_update_frame;
2735 bottom = &(tso->stack[tso->stack_size]);
2738 /* There must be at least one frame, namely the STOP_FRAME.
2740 ASSERT((P_)frame < bottom);
2742 /* Walk down the stack, reversing the links between frames so that
2743 * we can walk back up as we squeeze from the bottom. Note that
2744 * next_frame and prev_frame refer to next and previous as they were
2745 * added to the stack, rather than the way we see them in this
2746 * walk. (It makes the next loop less confusing.)
2748 * Stop if we find an update frame pointing to a black hole
2749 * (see comment in threadLazyBlackHole()).
2753 while ((P_)frame < bottom - 1) { /* bottom - 1 is the STOP_FRAME */
2754 prev_frame = frame->link;
2755 frame->link = next_frame;
2758 if (get_itbl(frame)->type == UPDATE_FRAME
2759 && frame->updatee->header.info == &BLACKHOLE_info) {
2764 /* Now, we're at the bottom. Frame points to the lowest update
2765 * frame on the stack, and its link actually points to the frame
2766 * above. We have to walk back up the stack, squeezing out empty
2767 * update frames and turning the pointers back around on the way
2770 * The bottom-most frame (the STOP_FRAME) has not been altered, and
2771 * we never want to eliminate it anyway. Just walk one step up
2772 * before starting to squeeze. When you get to the topmost frame,
2773 * remember that there are still some words above it that might have
2780 prev_was_update_frame = (get_itbl(prev_frame)->type == UPDATE_FRAME);
2783 * Loop through all of the frames (everything except the very
2784 * bottom). Things are complicated by the fact that we have
2785 * CATCH_FRAMEs and SEQ_FRAMEs interspersed with the update frames.
2786 * We can only squeeze when there are two consecutive UPDATE_FRAMEs.
2788 while (frame != NULL) {
2790 StgPtr frame_bottom = (P_)frame + sizeofW(StgUpdateFrame);
2791 rtsBool is_update_frame;
2793 next_frame = frame->link;
2794 is_update_frame = (get_itbl(frame)->type == UPDATE_FRAME);
2797 * 1. both the previous and current frame are update frames
2798 * 2. the current frame is empty
2800 if (prev_was_update_frame && is_update_frame &&
2801 (P_)prev_frame == frame_bottom + displacement) {
2803 /* Now squeeze out the current frame */
2804 StgClosure *updatee_keep = prev_frame->updatee;
2805 StgClosure *updatee_bypass = frame->updatee;
2808 fprintf(stderr, "squeezing frame at %p\n", frame);
2811 /* Deal with blocking queues. If both updatees have blocked
2812 * threads, then we should merge the queues into the update
2813 * frame that we're keeping.
2815 * Alternatively, we could just wake them up: they'll just go
2816 * straight to sleep on the proper blackhole! This is less code
2817 * and probably less bug prone, although it's probably much
2820 #if 0 /* do it properly... */
2821 if (GET_INFO(updatee_bypass) == BLACKHOLE_BQ_info) {
2822 /* Sigh. It has one. Don't lose those threads! */
2823 if (GET_INFO(updatee_keep) == BLACKHOLE_BQ_info) {
2824 /* Urgh. Two queues. Merge them. */
2825 P_ keep_tso = ((StgBlockingQueue *)updatee_keep)->blocking_queue;
2827 while (keep_tso->link != END_TSO_QUEUE) {
2828 keep_tso = keep_tso->link;
2830 keep_tso->link = ((StgBlockingQueue *)updatee_bypass)->blocking_queue;
2833 /* For simplicity, just swap the BQ for the BH */
2834 P_ temp = updatee_keep;
2836 updatee_keep = updatee_bypass;
2837 updatee_bypass = temp;
2839 /* Record the swap in the kept frame (below) */
2840 prev_frame->updatee = updatee_keep;
2845 TICK_UPD_SQUEEZED();
2846 UPD_IND(updatee_bypass, updatee_keep); /* this wakes the threads up */
2848 sp = (P_)frame - 1; /* sp = stuff to slide */
2849 displacement += sizeofW(StgUpdateFrame);
2852 /* No squeeze for this frame */
2853 sp = frame_bottom - 1; /* Keep the current frame */
2855 /* Do lazy black-holing.
2857 if (is_update_frame) {
2858 StgBlockingQueue *bh = (StgBlockingQueue *)frame->updatee;
2859 if (bh->header.info != &BLACKHOLE_BQ_info &&
2860 bh->header.info != &CAF_BLACKHOLE_info) {
2861 SET_INFO(bh,&BLACKHOLE_info);
2865 /* Fix the link in the current frame (should point to the frame below) */
2866 frame->link = prev_frame;
2867 prev_was_update_frame = is_update_frame;
2870 /* Now slide all words from sp up to the next frame */
2872 if (displacement > 0) {
2873 P_ next_frame_bottom;
2875 if (next_frame != NULL)
2876 next_frame_bottom = (P_)next_frame + sizeofW(StgUpdateFrame);
2878 next_frame_bottom = tso->sp - 1;
2881 fprintf(stderr, "sliding [%p, %p] by %ld\n", sp, next_frame_bottom,
2885 while (sp >= next_frame_bottom) {
2886 sp[displacement] = *sp;
2890 (P_)prev_frame = (P_)frame + displacement;
2894 tso->sp += displacement;
2895 tso->su = prev_frame;
2898 /* -----------------------------------------------------------------------------
2901 * We have to prepare for GC - this means doing lazy black holing
2902 * here. We also take the opportunity to do stack squeezing if it's
2904 * -------------------------------------------------------------------------- */
2907 threadPaused(StgTSO *tso)
2909 if ( RtsFlags.GcFlags.squeezeUpdFrames == rtsTrue )
2910 threadSqueezeStack(tso); /* does black holing too */
2912 threadLazyBlackHole(tso);