1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team 1998-2003
5 * Generational garbage collector
7 * ---------------------------------------------------------------------------*/
9 #include "PosixSource.h"
14 #include "OSThreads.h"
16 #include "LdvProfile.h"
21 #include "BlockAlloc.h"
27 #include "ParTicky.h" // ToDo: move into Rts.h
28 #include "GCCompact.h"
31 #if defined(GRAN) || defined(PAR)
32 # include "GranSimRts.h"
33 # include "ParallelRts.h"
37 # include "ParallelDebug.h"
42 #if defined(RTS_GTK_FRONTPANEL)
43 #include "FrontPanel.h"
46 #include "RetainerProfile.h"
50 /* STATIC OBJECT LIST.
53 * We maintain a linked list of static objects that are still live.
54 * The requirements for this list are:
56 * - we need to scan the list while adding to it, in order to
57 * scavenge all the static objects (in the same way that
58 * breadth-first scavenging works for dynamic objects).
60 * - we need to be able to tell whether an object is already on
61 * the list, to break loops.
63 * Each static object has a "static link field", which we use for
64 * linking objects on to the list. We use a stack-type list, consing
65 * objects on the front as they are added (this means that the
66 * scavenge phase is depth-first, not breadth-first, but that
69 * A separate list is kept for objects that have been scavenged
70 * already - this is so that we can zero all the marks afterwards.
72 * An object is on the list if its static link field is non-zero; this
73 * means that we have to mark the end of the list with '1', not NULL.
75 * Extra notes for generational GC:
77 * Each generation has a static object list associated with it. When
78 * collecting generations up to N, we treat the static object lists
79 * from generations > N as roots.
81 * We build up a static object list while collecting generations 0..N,
82 * which is then appended to the static object list of generation N+1.
84 static StgClosure* static_objects; // live static objects
85 StgClosure* scavenged_static_objects; // static objects scavenged so far
87 /* N is the oldest generation being collected, where the generations
88 * are numbered starting at 0. A major GC (indicated by the major_gc
89 * flag) is when we're collecting all generations. We only attempt to
90 * deal with static objects and GC CAFs when doing a major GC.
93 static rtsBool major_gc;
95 /* Youngest generation that objects should be evacuated to in
96 * evacuate(). (Logically an argument to evacuate, but it's static
97 * a lot of the time so we optimise it into a global variable).
103 StgWeak *old_weak_ptr_list; // also pending finaliser list
105 /* Which stage of processing various kinds of weak pointer are we at?
106 * (see traverse_weak_ptr_list() below for discussion).
108 typedef enum { WeakPtrs, WeakThreads, WeakDone } WeakStage;
109 static WeakStage weak_stage;
111 /* List of all threads during GC
113 static StgTSO *old_all_threads;
114 StgTSO *resurrected_threads;
116 /* Flag indicating failure to evacuate an object to the desired
119 static rtsBool failed_to_evac;
121 /* Old to-space (used for two-space collector only)
123 static bdescr *old_to_blocks;
125 /* Data used for allocation area sizing.
127 static lnat new_blocks; // blocks allocated during this GC
128 static lnat g0s0_pcnt_kept = 30; // percentage of g0s0 live at last minor GC
130 /* Used to avoid long recursion due to selector thunks
132 static lnat thunk_selector_depth = 0;
133 #define MAX_THUNK_SELECTOR_DEPTH 8
135 /* -----------------------------------------------------------------------------
136 Static function declarations
137 -------------------------------------------------------------------------- */
139 static bdescr * gc_alloc_block ( step *stp );
140 static void mark_root ( StgClosure **root );
142 // Use a register argument for evacuate, if available.
144 #define REGPARM1 __attribute__((regparm(1)))
149 REGPARM1 static StgClosure * evacuate (StgClosure *q);
151 static void zero_static_object_list ( StgClosure* first_static );
153 static rtsBool traverse_weak_ptr_list ( void );
154 static void mark_weak_ptr_list ( StgWeak **list );
156 static StgClosure * eval_thunk_selector ( nat field, StgSelector * p );
159 static void scavenge ( step * );
160 static void scavenge_mark_stack ( void );
161 static void scavenge_stack ( StgPtr p, StgPtr stack_end );
162 static rtsBool scavenge_one ( StgPtr p );
163 static void scavenge_large ( step * );
164 static void scavenge_static ( void );
165 static void scavenge_mutable_list ( generation *g );
167 static void scavenge_large_bitmap ( StgPtr p,
168 StgLargeBitmap *large_bitmap,
171 #if 0 && defined(DEBUG)
172 static void gcCAFs ( void );
175 /* -----------------------------------------------------------------------------
176 inline functions etc. for dealing with the mark bitmap & stack.
177 -------------------------------------------------------------------------- */
179 #define MARK_STACK_BLOCKS 4
181 static bdescr *mark_stack_bdescr;
182 static StgPtr *mark_stack;
183 static StgPtr *mark_sp;
184 static StgPtr *mark_splim;
186 // Flag and pointers used for falling back to a linear scan when the
187 // mark stack overflows.
188 static rtsBool mark_stack_overflowed;
189 static bdescr *oldgen_scan_bd;
190 static StgPtr oldgen_scan;
192 STATIC_INLINE rtsBool
193 mark_stack_empty(void)
195 return mark_sp == mark_stack;
198 STATIC_INLINE rtsBool
199 mark_stack_full(void)
201 return mark_sp >= mark_splim;
205 reset_mark_stack(void)
207 mark_sp = mark_stack;
211 push_mark_stack(StgPtr p)
222 /* -----------------------------------------------------------------------------
223 Allocate a new to-space block in the given step.
224 -------------------------------------------------------------------------- */
227 gc_alloc_block(step *stp)
229 bdescr *bd = allocBlock();
230 bd->gen_no = stp->gen_no;
234 // blocks in to-space in generations up to and including N
235 // get the BF_EVACUATED flag.
236 if (stp->gen_no <= N) {
237 bd->flags = BF_EVACUATED;
242 // Start a new to-space block, chain it on after the previous one.
243 if (stp->hp_bd == NULL) {
246 stp->hp_bd->free = stp->hp;
247 stp->hp_bd->link = bd;
252 stp->hpLim = stp->hp + BLOCK_SIZE_W;
260 /* -----------------------------------------------------------------------------
263 Rough outline of the algorithm: for garbage collecting generation N
264 (and all younger generations):
266 - follow all pointers in the root set. the root set includes all
267 mutable objects in all generations (mutable_list).
269 - for each pointer, evacuate the object it points to into either
271 + to-space of the step given by step->to, which is the next
272 highest step in this generation or the first step in the next
273 generation if this is the last step.
275 + to-space of generations[evac_gen]->steps[0], if evac_gen != 0.
276 When we evacuate an object we attempt to evacuate
277 everything it points to into the same generation - this is
278 achieved by setting evac_gen to the desired generation. If
279 we can't do this, then an entry in the mut list has to
280 be made for the cross-generation pointer.
282 + if the object is already in a generation > N, then leave
285 - repeatedly scavenge to-space from each step in each generation
286 being collected until no more objects can be evacuated.
288 - free from-space in each step, and set from-space = to-space.
290 Locks held: sched_mutex
292 -------------------------------------------------------------------------- */
295 GarbageCollect ( void (*get_roots)(evac_fn), rtsBool force_major_gc )
299 lnat live, allocated, collected = 0, copied = 0;
300 lnat oldgen_saved_blocks = 0;
304 CostCentreStack *prev_CCS;
307 #if defined(DEBUG) && defined(GRAN)
308 IF_DEBUG(gc, debugBelch("@@ Starting garbage collection at %ld (%lx)\n",
312 #if defined(RTS_USER_SIGNALS)
317 // tell the STM to discard any cached closures its hoping to re-use
320 // tell the stats department that we've started a GC
323 // Init stats and print par specific (timing) info
324 PAR_TICKY_PAR_START();
326 // attribute any costs to CCS_GC
332 /* Approximate how much we allocated.
333 * Todo: only when generating stats?
335 allocated = calcAllocated();
337 /* Figure out which generation to collect
339 if (force_major_gc) {
340 N = RtsFlags.GcFlags.generations - 1;
344 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
345 if (generations[g].steps[0].n_blocks +
346 generations[g].steps[0].n_large_blocks
347 >= generations[g].max_blocks) {
351 major_gc = (N == RtsFlags.GcFlags.generations-1);
354 #ifdef RTS_GTK_FRONTPANEL
355 if (RtsFlags.GcFlags.frontpanel) {
356 updateFrontPanelBeforeGC(N);
360 // check stack sanity *before* GC (ToDo: check all threads)
362 // ToDo!: check sanity IF_DEBUG(sanity, checkTSOsSanity());
364 IF_DEBUG(sanity, checkFreeListSanity());
366 /* Initialise the static object lists
368 static_objects = END_OF_STATIC_LIST;
369 scavenged_static_objects = END_OF_STATIC_LIST;
371 /* Save the old to-space if we're doing a two-space collection
373 if (RtsFlags.GcFlags.generations == 1) {
374 old_to_blocks = g0s0->to_blocks;
375 g0s0->to_blocks = NULL;
376 g0s0->n_to_blocks = 0;
379 /* Keep a count of how many new blocks we allocated during this GC
380 * (used for resizing the allocation area, later).
384 // Initialise to-space in all the generations/steps that we're
387 for (g = 0; g <= N; g++) {
389 // throw away the mutable list. Invariant: the mutable list
390 // always has at least one block; this means we can avoid a check for
391 // NULL in recordMutable().
393 freeChain(generations[g].mut_list);
394 generations[g].mut_list = allocBlock();
397 for (s = 0; s < generations[g].n_steps; s++) {
399 // generation 0, step 0 doesn't need to-space
400 if (g == 0 && s == 0 && RtsFlags.GcFlags.generations > 1) {
404 stp = &generations[g].steps[s];
405 ASSERT(stp->gen_no == g);
407 // start a new to-space for this step.
410 stp->to_blocks = NULL;
412 // allocate the first to-space block; extra blocks will be
413 // chained on as necessary.
414 bd = gc_alloc_block(stp);
416 stp->scan = bd->start;
419 // initialise the large object queues.
420 stp->new_large_objects = NULL;
421 stp->scavenged_large_objects = NULL;
422 stp->n_scavenged_large_blocks = 0;
424 // mark the large objects as not evacuated yet
425 for (bd = stp->large_objects; bd; bd = bd->link) {
426 bd->flags &= ~BF_EVACUATED;
429 // for a compacted step, we need to allocate the bitmap
430 if (stp->is_compacted) {
431 nat bitmap_size; // in bytes
432 bdescr *bitmap_bdescr;
435 bitmap_size = stp->n_blocks * BLOCK_SIZE / (sizeof(W_)*BITS_PER_BYTE);
437 if (bitmap_size > 0) {
438 bitmap_bdescr = allocGroup((lnat)BLOCK_ROUND_UP(bitmap_size)
440 stp->bitmap = bitmap_bdescr;
441 bitmap = bitmap_bdescr->start;
443 IF_DEBUG(gc, debugBelch("bitmap_size: %d, bitmap: %p",
444 bitmap_size, bitmap););
446 // don't forget to fill it with zeros!
447 memset(bitmap, 0, bitmap_size);
449 // For each block in this step, point to its bitmap from the
451 for (bd=stp->blocks; bd != NULL; bd = bd->link) {
452 bd->u.bitmap = bitmap;
453 bitmap += BLOCK_SIZE_W / (sizeof(W_)*BITS_PER_BYTE);
455 // Also at this point we set the BF_COMPACTED flag
456 // for this block. The invariant is that
457 // BF_COMPACTED is always unset, except during GC
458 // when it is set on those blocks which will be
460 bd->flags |= BF_COMPACTED;
467 /* make sure the older generations have at least one block to
468 * allocate into (this makes things easier for copy(), see below).
470 for (g = N+1; g < RtsFlags.GcFlags.generations; g++) {
471 for (s = 0; s < generations[g].n_steps; s++) {
472 stp = &generations[g].steps[s];
473 if (stp->hp_bd == NULL) {
474 ASSERT(stp->blocks == NULL);
475 bd = gc_alloc_block(stp);
479 /* Set the scan pointer for older generations: remember we
480 * still have to scavenge objects that have been promoted. */
482 stp->scan_bd = stp->hp_bd;
483 stp->to_blocks = NULL;
484 stp->n_to_blocks = 0;
485 stp->new_large_objects = NULL;
486 stp->scavenged_large_objects = NULL;
487 stp->n_scavenged_large_blocks = 0;
491 /* Allocate a mark stack if we're doing a major collection.
494 mark_stack_bdescr = allocGroup(MARK_STACK_BLOCKS);
495 mark_stack = (StgPtr *)mark_stack_bdescr->start;
496 mark_sp = mark_stack;
497 mark_splim = mark_stack + (MARK_STACK_BLOCKS * BLOCK_SIZE_W);
499 mark_stack_bdescr = NULL;
502 /* -----------------------------------------------------------------------
503 * follow all the roots that we know about:
504 * - mutable lists from each generation > N
505 * we want to *scavenge* these roots, not evacuate them: they're not
506 * going to move in this GC.
507 * Also: do them in reverse generation order. This is because we
508 * often want to promote objects that are pointed to by older
509 * generations early, so we don't have to repeatedly copy them.
510 * Doing the generations in reverse order ensures that we don't end
511 * up in the situation where we want to evac an object to gen 3 and
512 * it has already been evaced to gen 2.
516 for (g = RtsFlags.GcFlags.generations-1; g > N; g--) {
517 generations[g].saved_mut_list = generations[g].mut_list;
518 generations[g].mut_list = allocBlock();
519 // mut_list always has at least one block.
522 for (g = RtsFlags.GcFlags.generations-1; g > N; g--) {
523 IF_PAR_DEBUG(verbose, printMutableList(&generations[g]));
524 scavenge_mutable_list(&generations[g]);
526 for (st = generations[g].n_steps-1; st >= 0; st--) {
527 scavenge(&generations[g].steps[st]);
532 /* follow roots from the CAF list (used by GHCi)
537 /* follow all the roots that the application knows about.
540 get_roots(mark_root);
543 /* And don't forget to mark the TSO if we got here direct from
545 /* Not needed in a seq version?
547 CurrentTSO = (StgTSO *)MarkRoot((StgClosure *)CurrentTSO);
551 // Mark the entries in the GALA table of the parallel system
552 markLocalGAs(major_gc);
553 // Mark all entries on the list of pending fetches
554 markPendingFetches(major_gc);
557 /* Mark the weak pointer list, and prepare to detect dead weak
560 mark_weak_ptr_list(&weak_ptr_list);
561 old_weak_ptr_list = weak_ptr_list;
562 weak_ptr_list = NULL;
563 weak_stage = WeakPtrs;
565 /* The all_threads list is like the weak_ptr_list.
566 * See traverse_weak_ptr_list() for the details.
568 old_all_threads = all_threads;
569 all_threads = END_TSO_QUEUE;
570 resurrected_threads = END_TSO_QUEUE;
572 /* Mark the stable pointer table.
574 markStablePtrTable(mark_root);
576 /* -------------------------------------------------------------------------
577 * Repeatedly scavenge all the areas we know about until there's no
578 * more scavenging to be done.
585 // scavenge static objects
586 if (major_gc && static_objects != END_OF_STATIC_LIST) {
587 IF_DEBUG(sanity, checkStaticObjects(static_objects));
591 /* When scavenging the older generations: Objects may have been
592 * evacuated from generations <= N into older generations, and we
593 * need to scavenge these objects. We're going to try to ensure that
594 * any evacuations that occur move the objects into at least the
595 * same generation as the object being scavenged, otherwise we
596 * have to create new entries on the mutable list for the older
600 // scavenge each step in generations 0..maxgen
606 // scavenge objects in compacted generation
607 if (mark_stack_overflowed || oldgen_scan_bd != NULL ||
608 (mark_stack_bdescr != NULL && !mark_stack_empty())) {
609 scavenge_mark_stack();
613 for (gen = RtsFlags.GcFlags.generations; --gen >= 0; ) {
614 for (st = generations[gen].n_steps; --st >= 0; ) {
615 if (gen == 0 && st == 0 && RtsFlags.GcFlags.generations > 1) {
618 stp = &generations[gen].steps[st];
620 if (stp->hp_bd != stp->scan_bd || stp->scan < stp->hp) {
625 if (stp->new_large_objects != NULL) {
634 if (flag) { goto loop; }
636 // must be last... invariant is that everything is fully
637 // scavenged at this point.
638 if (traverse_weak_ptr_list()) { // returns rtsTrue if evaced something
643 /* Update the pointers from the "main thread" list - these are
644 * treated as weak pointers because we want to allow a main thread
645 * to get a BlockedOnDeadMVar exception in the same way as any other
646 * thread. Note that the threads should all have been retained by
647 * GC by virtue of being on the all_threads list, we're just
648 * updating pointers here.
653 for (m = main_threads; m != NULL; m = m->link) {
654 tso = (StgTSO *) isAlive((StgClosure *)m->tso);
656 barf("main thread has been GC'd");
663 // Reconstruct the Global Address tables used in GUM
664 rebuildGAtables(major_gc);
665 IF_DEBUG(sanity, checkLAGAtable(rtsTrue/*check closures, too*/));
668 // Now see which stable names are still alive.
671 // Tidy the end of the to-space chains
672 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
673 for (s = 0; s < generations[g].n_steps; s++) {
674 stp = &generations[g].steps[s];
675 if (!(g == 0 && s == 0 && RtsFlags.GcFlags.generations > 1)) {
676 ASSERT(Bdescr(stp->hp) == stp->hp_bd);
677 stp->hp_bd->free = stp->hp;
683 // We call processHeapClosureForDead() on every closure destroyed during
684 // the current garbage collection, so we invoke LdvCensusForDead().
685 if (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_LDV
686 || RtsFlags.ProfFlags.bioSelector != NULL)
690 // NO MORE EVACUATION AFTER THIS POINT!
691 // Finally: compaction of the oldest generation.
692 if (major_gc && oldest_gen->steps[0].is_compacted) {
693 // save number of blocks for stats
694 oldgen_saved_blocks = oldest_gen->steps[0].n_blocks;
698 IF_DEBUG(sanity, checkGlobalTSOList(rtsFalse));
700 /* run through all the generations/steps and tidy up
702 copied = new_blocks * BLOCK_SIZE_W;
703 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
706 generations[g].collections++; // for stats
709 // Count the mutable list as bytes "copied" for the purposes of
710 // stats. Every mutable list is copied during every GC.
712 for (bd = generations[g].mut_list; bd != NULL; bd = bd->link) {
713 copied += (bd->free - bd->start) * sizeof(StgWord);
717 for (s = 0; s < generations[g].n_steps; s++) {
719 stp = &generations[g].steps[s];
721 if (!(g == 0 && s == 0 && RtsFlags.GcFlags.generations > 1)) {
722 // stats information: how much we copied
724 copied -= stp->hp_bd->start + BLOCK_SIZE_W -
729 // for generations we collected...
732 // rough calculation of garbage collected, for stats output
733 if (stp->is_compacted) {
734 collected += (oldgen_saved_blocks - stp->n_blocks) * BLOCK_SIZE_W;
736 collected += stp->n_blocks * BLOCK_SIZE_W;
739 /* free old memory and shift to-space into from-space for all
740 * the collected steps (except the allocation area). These
741 * freed blocks will probaby be quickly recycled.
743 if (!(g == 0 && s == 0)) {
744 if (stp->is_compacted) {
745 // for a compacted step, just shift the new to-space
746 // onto the front of the now-compacted existing blocks.
747 for (bd = stp->to_blocks; bd != NULL; bd = bd->link) {
748 bd->flags &= ~BF_EVACUATED; // now from-space
750 // tack the new blocks on the end of the existing blocks
751 if (stp->blocks == NULL) {
752 stp->blocks = stp->to_blocks;
754 for (bd = stp->blocks; bd != NULL; bd = next) {
757 bd->link = stp->to_blocks;
759 // NB. this step might not be compacted next
760 // time, so reset the BF_COMPACTED flags.
761 // They are set before GC if we're going to
762 // compact. (search for BF_COMPACTED above).
763 bd->flags &= ~BF_COMPACTED;
766 // add the new blocks to the block tally
767 stp->n_blocks += stp->n_to_blocks;
769 freeChain(stp->blocks);
770 stp->blocks = stp->to_blocks;
771 stp->n_blocks = stp->n_to_blocks;
772 for (bd = stp->blocks; bd != NULL; bd = bd->link) {
773 bd->flags &= ~BF_EVACUATED; // now from-space
776 stp->to_blocks = NULL;
777 stp->n_to_blocks = 0;
780 /* LARGE OBJECTS. The current live large objects are chained on
781 * scavenged_large, having been moved during garbage
782 * collection from large_objects. Any objects left on
783 * large_objects list are therefore dead, so we free them here.
785 for (bd = stp->large_objects; bd != NULL; bd = next) {
791 // update the count of blocks used by large objects
792 for (bd = stp->scavenged_large_objects; bd != NULL; bd = bd->link) {
793 bd->flags &= ~BF_EVACUATED;
795 stp->large_objects = stp->scavenged_large_objects;
796 stp->n_large_blocks = stp->n_scavenged_large_blocks;
799 // for older generations...
801 /* For older generations, we need to append the
802 * scavenged_large_object list (i.e. large objects that have been
803 * promoted during this GC) to the large_object list for that step.
805 for (bd = stp->scavenged_large_objects; bd; bd = next) {
807 bd->flags &= ~BF_EVACUATED;
808 dbl_link_onto(bd, &stp->large_objects);
811 // add the new blocks we promoted during this GC
812 stp->n_blocks += stp->n_to_blocks;
813 stp->n_to_blocks = 0;
814 stp->n_large_blocks += stp->n_scavenged_large_blocks;
819 /* Reset the sizes of the older generations when we do a major
822 * CURRENT STRATEGY: make all generations except zero the same size.
823 * We have to stay within the maximum heap size, and leave a certain
824 * percentage of the maximum heap size available to allocate into.
826 if (major_gc && RtsFlags.GcFlags.generations > 1) {
827 nat live, size, min_alloc;
828 nat max = RtsFlags.GcFlags.maxHeapSize;
829 nat gens = RtsFlags.GcFlags.generations;
831 // live in the oldest generations
832 live = oldest_gen->steps[0].n_blocks +
833 oldest_gen->steps[0].n_large_blocks;
835 // default max size for all generations except zero
836 size = stg_max(live * RtsFlags.GcFlags.oldGenFactor,
837 RtsFlags.GcFlags.minOldGenSize);
839 // minimum size for generation zero
840 min_alloc = stg_max((RtsFlags.GcFlags.pcFreeHeap * max) / 200,
841 RtsFlags.GcFlags.minAllocAreaSize);
843 // Auto-enable compaction when the residency reaches a
844 // certain percentage of the maximum heap size (default: 30%).
845 if (RtsFlags.GcFlags.generations > 1 &&
846 (RtsFlags.GcFlags.compact ||
848 oldest_gen->steps[0].n_blocks >
849 (RtsFlags.GcFlags.compactThreshold * max) / 100))) {
850 oldest_gen->steps[0].is_compacted = 1;
851 // debugBelch("compaction: on\n", live);
853 oldest_gen->steps[0].is_compacted = 0;
854 // debugBelch("compaction: off\n", live);
857 // if we're going to go over the maximum heap size, reduce the
858 // size of the generations accordingly. The calculation is
859 // different if compaction is turned on, because we don't need
860 // to double the space required to collect the old generation.
863 // this test is necessary to ensure that the calculations
864 // below don't have any negative results - we're working
865 // with unsigned values here.
866 if (max < min_alloc) {
870 if (oldest_gen->steps[0].is_compacted) {
871 if ( (size + (size - 1) * (gens - 2) * 2) + min_alloc > max ) {
872 size = (max - min_alloc) / ((gens - 1) * 2 - 1);
875 if ( (size * (gens - 1) * 2) + min_alloc > max ) {
876 size = (max - min_alloc) / ((gens - 1) * 2);
886 debugBelch("live: %d, min_alloc: %d, size : %d, max = %d\n", live,
887 min_alloc, size, max);
890 for (g = 0; g < gens; g++) {
891 generations[g].max_blocks = size;
895 // Guess the amount of live data for stats.
898 /* Free the small objects allocated via allocate(), since this will
899 * all have been copied into G0S1 now.
901 if (small_alloc_list != NULL) {
902 freeChain(small_alloc_list);
904 small_alloc_list = NULL;
908 alloc_blocks_lim = RtsFlags.GcFlags.minAllocAreaSize;
910 // Start a new pinned_object_block
911 pinned_object_block = NULL;
913 /* Free the mark stack.
915 if (mark_stack_bdescr != NULL) {
916 freeGroup(mark_stack_bdescr);
921 for (g = 0; g <= N; g++) {
922 for (s = 0; s < generations[g].n_steps; s++) {
923 stp = &generations[g].steps[s];
924 if (stp->is_compacted && stp->bitmap != NULL) {
925 freeGroup(stp->bitmap);
930 /* Two-space collector:
931 * Free the old to-space, and estimate the amount of live data.
933 if (RtsFlags.GcFlags.generations == 1) {
936 if (old_to_blocks != NULL) {
937 freeChain(old_to_blocks);
939 for (bd = g0s0->to_blocks; bd != NULL; bd = bd->link) {
940 bd->flags = 0; // now from-space
943 /* For a two-space collector, we need to resize the nursery. */
945 /* set up a new nursery. Allocate a nursery size based on a
946 * function of the amount of live data (by default a factor of 2)
947 * Use the blocks from the old nursery if possible, freeing up any
950 * If we get near the maximum heap size, then adjust our nursery
951 * size accordingly. If the nursery is the same size as the live
952 * data (L), then we need 3L bytes. We can reduce the size of the
953 * nursery to bring the required memory down near 2L bytes.
955 * A normal 2-space collector would need 4L bytes to give the same
956 * performance we get from 3L bytes, reducing to the same
957 * performance at 2L bytes.
959 blocks = g0s0->n_to_blocks;
961 if ( RtsFlags.GcFlags.maxHeapSize != 0 &&
962 blocks * RtsFlags.GcFlags.oldGenFactor * 2 >
963 RtsFlags.GcFlags.maxHeapSize ) {
964 long adjusted_blocks; // signed on purpose
967 adjusted_blocks = (RtsFlags.GcFlags.maxHeapSize - 2 * blocks);
968 IF_DEBUG(gc, debugBelch("@@ Near maximum heap size of 0x%x blocks, blocks = %d, adjusted to %ld", RtsFlags.GcFlags.maxHeapSize, blocks, adjusted_blocks));
969 pc_free = adjusted_blocks * 100 / RtsFlags.GcFlags.maxHeapSize;
970 if (pc_free < RtsFlags.GcFlags.pcFreeHeap) /* might even be < 0 */ {
973 blocks = adjusted_blocks;
976 blocks *= RtsFlags.GcFlags.oldGenFactor;
977 if (blocks < RtsFlags.GcFlags.minAllocAreaSize) {
978 blocks = RtsFlags.GcFlags.minAllocAreaSize;
981 resizeNursery(blocks);
984 /* Generational collector:
985 * If the user has given us a suggested heap size, adjust our
986 * allocation area to make best use of the memory available.
989 if (RtsFlags.GcFlags.heapSizeSuggestion) {
991 nat needed = calcNeeded(); // approx blocks needed at next GC
993 /* Guess how much will be live in generation 0 step 0 next time.
994 * A good approximation is obtained by finding the
995 * percentage of g0s0 that was live at the last minor GC.
998 g0s0_pcnt_kept = (new_blocks * 100) / g0s0->n_blocks;
1001 /* Estimate a size for the allocation area based on the
1002 * information available. We might end up going slightly under
1003 * or over the suggested heap size, but we should be pretty
1006 * Formula: suggested - needed
1007 * ----------------------------
1008 * 1 + g0s0_pcnt_kept/100
1010 * where 'needed' is the amount of memory needed at the next
1011 * collection for collecting all steps except g0s0.
1014 (((long)RtsFlags.GcFlags.heapSizeSuggestion - (long)needed) * 100) /
1015 (100 + (long)g0s0_pcnt_kept);
1017 if (blocks < (long)RtsFlags.GcFlags.minAllocAreaSize) {
1018 blocks = RtsFlags.GcFlags.minAllocAreaSize;
1021 resizeNursery((nat)blocks);
1024 // we might have added extra large blocks to the nursery, so
1025 // resize back to minAllocAreaSize again.
1026 resizeNursery(RtsFlags.GcFlags.minAllocAreaSize);
1030 // mark the garbage collected CAFs as dead
1031 #if 0 && defined(DEBUG) // doesn't work at the moment
1032 if (major_gc) { gcCAFs(); }
1036 // resetStaticObjectForRetainerProfiling() must be called before
1038 resetStaticObjectForRetainerProfiling();
1041 // zero the scavenged static object list
1043 zero_static_object_list(scavenged_static_objects);
1046 // Reset the nursery
1049 RELEASE_LOCK(&sched_mutex);
1051 // start any pending finalizers
1052 scheduleFinalizers(old_weak_ptr_list);
1054 // send exceptions to any threads which were about to die
1055 resurrectThreads(resurrected_threads);
1057 ACQUIRE_LOCK(&sched_mutex);
1059 // Update the stable pointer hash table.
1060 updateStablePtrTable(major_gc);
1062 // check sanity after GC
1063 IF_DEBUG(sanity, checkSanity());
1065 // extra GC trace info
1066 IF_DEBUG(gc, statDescribeGens());
1069 // symbol-table based profiling
1070 /* heapCensus(to_blocks); */ /* ToDo */
1073 // restore enclosing cost centre
1078 // check for memory leaks if sanity checking is on
1079 IF_DEBUG(sanity, memInventory());
1081 #ifdef RTS_GTK_FRONTPANEL
1082 if (RtsFlags.GcFlags.frontpanel) {
1083 updateFrontPanelAfterGC( N, live );
1087 // ok, GC over: tell the stats department what happened.
1088 stat_endGC(allocated, collected, live, copied, N);
1090 #if defined(RTS_USER_SIGNALS)
1091 // unblock signals again
1092 unblockUserSignals();
1099 /* -----------------------------------------------------------------------------
1102 traverse_weak_ptr_list is called possibly many times during garbage
1103 collection. It returns a flag indicating whether it did any work
1104 (i.e. called evacuate on any live pointers).
1106 Invariant: traverse_weak_ptr_list is called when the heap is in an
1107 idempotent state. That means that there are no pending
1108 evacuate/scavenge operations. This invariant helps the weak
1109 pointer code decide which weak pointers are dead - if there are no
1110 new live weak pointers, then all the currently unreachable ones are
1113 For generational GC: we just don't try to finalize weak pointers in
1114 older generations than the one we're collecting. This could
1115 probably be optimised by keeping per-generation lists of weak
1116 pointers, but for a few weak pointers this scheme will work.
1118 There are three distinct stages to processing weak pointers:
1120 - weak_stage == WeakPtrs
1122 We process all the weak pointers whos keys are alive (evacuate
1123 their values and finalizers), and repeat until we can find no new
1124 live keys. If no live keys are found in this pass, then we
1125 evacuate the finalizers of all the dead weak pointers in order to
1128 - weak_stage == WeakThreads
1130 Now, we discover which *threads* are still alive. Pointers to
1131 threads from the all_threads and main thread lists are the
1132 weakest of all: a pointers from the finalizer of a dead weak
1133 pointer can keep a thread alive. Any threads found to be unreachable
1134 are evacuated and placed on the resurrected_threads list so we
1135 can send them a signal later.
1137 - weak_stage == WeakDone
1139 No more evacuation is done.
1141 -------------------------------------------------------------------------- */
1144 traverse_weak_ptr_list(void)
1146 StgWeak *w, **last_w, *next_w;
1148 rtsBool flag = rtsFalse;
1150 switch (weak_stage) {
1156 /* doesn't matter where we evacuate values/finalizers to, since
1157 * these pointers are treated as roots (iff the keys are alive).
1161 last_w = &old_weak_ptr_list;
1162 for (w = old_weak_ptr_list; w != NULL; w = next_w) {
1164 /* There might be a DEAD_WEAK on the list if finalizeWeak# was
1165 * called on a live weak pointer object. Just remove it.
1167 if (w->header.info == &stg_DEAD_WEAK_info) {
1168 next_w = ((StgDeadWeak *)w)->link;
1173 switch (get_itbl(w)->type) {
1176 next_w = (StgWeak *)((StgEvacuated *)w)->evacuee;
1181 /* Now, check whether the key is reachable.
1183 new = isAlive(w->key);
1186 // evacuate the value and finalizer
1187 w->value = evacuate(w->value);
1188 w->finalizer = evacuate(w->finalizer);
1189 // remove this weak ptr from the old_weak_ptr list
1191 // and put it on the new weak ptr list
1193 w->link = weak_ptr_list;
1196 IF_DEBUG(weak, debugBelch("Weak pointer still alive at %p -> %p",
1201 last_w = &(w->link);
1207 barf("traverse_weak_ptr_list: not WEAK");
1211 /* If we didn't make any changes, then we can go round and kill all
1212 * the dead weak pointers. The old_weak_ptr list is used as a list
1213 * of pending finalizers later on.
1215 if (flag == rtsFalse) {
1216 for (w = old_weak_ptr_list; w; w = w->link) {
1217 w->finalizer = evacuate(w->finalizer);
1220 // Next, move to the WeakThreads stage after fully
1221 // scavenging the finalizers we've just evacuated.
1222 weak_stage = WeakThreads;
1228 /* Now deal with the all_threads list, which behaves somewhat like
1229 * the weak ptr list. If we discover any threads that are about to
1230 * become garbage, we wake them up and administer an exception.
1233 StgTSO *t, *tmp, *next, **prev;
1235 prev = &old_all_threads;
1236 for (t = old_all_threads; t != END_TSO_QUEUE; t = next) {
1238 tmp = (StgTSO *)isAlive((StgClosure *)t);
1244 ASSERT(get_itbl(t)->type == TSO);
1245 switch (t->what_next) {
1246 case ThreadRelocated:
1251 case ThreadComplete:
1252 // finshed or died. The thread might still be alive, but we
1253 // don't keep it on the all_threads list. Don't forget to
1254 // stub out its global_link field.
1255 next = t->global_link;
1256 t->global_link = END_TSO_QUEUE;
1264 // not alive (yet): leave this thread on the
1265 // old_all_threads list.
1266 prev = &(t->global_link);
1267 next = t->global_link;
1270 // alive: move this thread onto the all_threads list.
1271 next = t->global_link;
1272 t->global_link = all_threads;
1279 /* And resurrect any threads which were about to become garbage.
1282 StgTSO *t, *tmp, *next;
1283 for (t = old_all_threads; t != END_TSO_QUEUE; t = next) {
1284 next = t->global_link;
1285 tmp = (StgTSO *)evacuate((StgClosure *)t);
1286 tmp->global_link = resurrected_threads;
1287 resurrected_threads = tmp;
1291 weak_stage = WeakDone; // *now* we're done,
1292 return rtsTrue; // but one more round of scavenging, please
1295 barf("traverse_weak_ptr_list");
1301 /* -----------------------------------------------------------------------------
1302 After GC, the live weak pointer list may have forwarding pointers
1303 on it, because a weak pointer object was evacuated after being
1304 moved to the live weak pointer list. We remove those forwarding
1307 Also, we don't consider weak pointer objects to be reachable, but
1308 we must nevertheless consider them to be "live" and retain them.
1309 Therefore any weak pointer objects which haven't as yet been
1310 evacuated need to be evacuated now.
1311 -------------------------------------------------------------------------- */
1315 mark_weak_ptr_list ( StgWeak **list )
1317 StgWeak *w, **last_w;
1320 for (w = *list; w; w = w->link) {
1321 // w might be WEAK, EVACUATED, or DEAD_WEAK (actually CON_STATIC) here
1322 ASSERT(w->header.info == &stg_DEAD_WEAK_info
1323 || get_itbl(w)->type == WEAK || get_itbl(w)->type == EVACUATED);
1324 w = (StgWeak *)evacuate((StgClosure *)w);
1326 last_w = &(w->link);
1330 /* -----------------------------------------------------------------------------
1331 isAlive determines whether the given closure is still alive (after
1332 a garbage collection) or not. It returns the new address of the
1333 closure if it is alive, or NULL otherwise.
1335 NOTE: Use it before compaction only!
1336 -------------------------------------------------------------------------- */
1340 isAlive(StgClosure *p)
1342 const StgInfoTable *info;
1347 ASSERT(LOOKS_LIKE_CLOSURE_PTR(p));
1350 // ignore static closures
1352 // ToDo: for static closures, check the static link field.
1353 // Problem here is that we sometimes don't set the link field, eg.
1354 // for static closures with an empty SRT or CONSTR_STATIC_NOCAFs.
1356 if (!HEAP_ALLOCED(p)) {
1360 // ignore closures in generations that we're not collecting.
1362 if (bd->gen_no > N) {
1366 // if it's a pointer into to-space, then we're done
1367 if (bd->flags & BF_EVACUATED) {
1371 // large objects use the evacuated flag
1372 if (bd->flags & BF_LARGE) {
1376 // check the mark bit for compacted steps
1377 if ((bd->flags & BF_COMPACTED) && is_marked((P_)p,bd)) {
1381 switch (info->type) {
1386 case IND_OLDGEN: // rely on compatible layout with StgInd
1387 case IND_OLDGEN_PERM:
1388 // follow indirections
1389 p = ((StgInd *)p)->indirectee;
1394 return ((StgEvacuated *)p)->evacuee;
1397 if (((StgTSO *)p)->what_next == ThreadRelocated) {
1398 p = (StgClosure *)((StgTSO *)p)->link;
1411 mark_root(StgClosure **root)
1413 *root = evacuate(*root);
1417 upd_evacuee(StgClosure *p, StgClosure *dest)
1419 // not true: (ToDo: perhaps it should be)
1420 // ASSERT(Bdescr((P_)dest)->flags & BF_EVACUATED);
1421 SET_INFO(p, &stg_EVACUATED_info);
1422 ((StgEvacuated *)p)->evacuee = dest;
1426 STATIC_INLINE StgClosure *
1427 copy(StgClosure *src, nat size, step *stp)
1432 nat size_org = size;
1435 TICK_GC_WORDS_COPIED(size);
1436 /* Find out where we're going, using the handy "to" pointer in
1437 * the step of the source object. If it turns out we need to
1438 * evacuate to an older generation, adjust it here (see comment
1441 if (stp->gen_no < evac_gen) {
1442 #ifdef NO_EAGER_PROMOTION
1443 failed_to_evac = rtsTrue;
1445 stp = &generations[evac_gen].steps[0];
1449 /* chain a new block onto the to-space for the destination step if
1452 if (stp->hp + size >= stp->hpLim) {
1453 gc_alloc_block(stp);
1456 for(to = stp->hp, from = (P_)src; size>0; --size) {
1462 upd_evacuee(src,(StgClosure *)dest);
1464 // We store the size of the just evacuated object in the LDV word so that
1465 // the profiler can guess the position of the next object later.
1466 SET_EVACUAEE_FOR_LDV(src, size_org);
1468 return (StgClosure *)dest;
1471 /* Special version of copy() for when we only want to copy the info
1472 * pointer of an object, but reserve some padding after it. This is
1473 * used to optimise evacuation of BLACKHOLEs.
1478 copyPart(StgClosure *src, nat size_to_reserve, nat size_to_copy, step *stp)
1483 nat size_to_copy_org = size_to_copy;
1486 TICK_GC_WORDS_COPIED(size_to_copy);
1487 if (stp->gen_no < evac_gen) {
1488 #ifdef NO_EAGER_PROMOTION
1489 failed_to_evac = rtsTrue;
1491 stp = &generations[evac_gen].steps[0];
1495 if (stp->hp + size_to_reserve >= stp->hpLim) {
1496 gc_alloc_block(stp);
1499 for(to = stp->hp, from = (P_)src; size_to_copy>0; --size_to_copy) {
1504 stp->hp += size_to_reserve;
1505 upd_evacuee(src,(StgClosure *)dest);
1507 // We store the size of the just evacuated object in the LDV word so that
1508 // the profiler can guess the position of the next object later.
1509 // size_to_copy_org is wrong because the closure already occupies size_to_reserve
1511 SET_EVACUAEE_FOR_LDV(src, size_to_reserve);
1513 if (size_to_reserve - size_to_copy_org > 0)
1514 FILL_SLOP(stp->hp - 1, (int)(size_to_reserve - size_to_copy_org));
1516 return (StgClosure *)dest;
1520 /* -----------------------------------------------------------------------------
1521 Evacuate a large object
1523 This just consists of removing the object from the (doubly-linked)
1524 step->large_objects list, and linking it on to the (singly-linked)
1525 step->new_large_objects list, from where it will be scavenged later.
1527 Convention: bd->flags has BF_EVACUATED set for a large object
1528 that has been evacuated, or unset otherwise.
1529 -------------------------------------------------------------------------- */
1533 evacuate_large(StgPtr p)
1535 bdescr *bd = Bdescr(p);
1538 // object must be at the beginning of the block (or be a ByteArray)
1539 ASSERT(get_itbl((StgClosure *)p)->type == ARR_WORDS ||
1540 (((W_)p & BLOCK_MASK) == 0));
1542 // already evacuated?
1543 if (bd->flags & BF_EVACUATED) {
1544 /* Don't forget to set the failed_to_evac flag if we didn't get
1545 * the desired destination (see comments in evacuate()).
1547 if (bd->gen_no < evac_gen) {
1548 failed_to_evac = rtsTrue;
1549 TICK_GC_FAILED_PROMOTION();
1555 // remove from large_object list
1557 bd->u.back->link = bd->link;
1558 } else { // first object in the list
1559 stp->large_objects = bd->link;
1562 bd->link->u.back = bd->u.back;
1565 /* link it on to the evacuated large object list of the destination step
1568 if (stp->gen_no < evac_gen) {
1569 #ifdef NO_EAGER_PROMOTION
1570 failed_to_evac = rtsTrue;
1572 stp = &generations[evac_gen].steps[0];
1577 bd->gen_no = stp->gen_no;
1578 bd->link = stp->new_large_objects;
1579 stp->new_large_objects = bd;
1580 bd->flags |= BF_EVACUATED;
1583 /* -----------------------------------------------------------------------------
1586 This is called (eventually) for every live object in the system.
1588 The caller to evacuate specifies a desired generation in the
1589 evac_gen global variable. The following conditions apply to
1590 evacuating an object which resides in generation M when we're
1591 collecting up to generation N
1595 else evac to step->to
1597 if M < evac_gen evac to evac_gen, step 0
1599 if the object is already evacuated, then we check which generation
1602 if M >= evac_gen do nothing
1603 if M < evac_gen set failed_to_evac flag to indicate that we
1604 didn't manage to evacuate this object into evac_gen.
1609 evacuate() is the single most important function performance-wise
1610 in the GC. Various things have been tried to speed it up, but as
1611 far as I can tell the code generated by gcc 3.2 with -O2 is about
1612 as good as it's going to get. We pass the argument to evacuate()
1613 in a register using the 'regparm' attribute (see the prototype for
1614 evacuate() near the top of this file).
1616 Changing evacuate() to take an (StgClosure **) rather than
1617 returning the new pointer seems attractive, because we can avoid
1618 writing back the pointer when it hasn't changed (eg. for a static
1619 object, or an object in a generation > N). However, I tried it and
1620 it doesn't help. One reason is that the (StgClosure **) pointer
1621 gets spilled to the stack inside evacuate(), resulting in far more
1622 extra reads/writes than we save.
1623 -------------------------------------------------------------------------- */
1625 REGPARM1 static StgClosure *
1626 evacuate(StgClosure *q)
1633 const StgInfoTable *info;
1636 if (HEAP_ALLOCED(q)) {
1639 if (bd->gen_no > N) {
1640 /* Can't evacuate this object, because it's in a generation
1641 * older than the ones we're collecting. Let's hope that it's
1642 * in evac_gen or older, or we will have to arrange to track
1643 * this pointer using the mutable list.
1645 if (bd->gen_no < evac_gen) {
1647 failed_to_evac = rtsTrue;
1648 TICK_GC_FAILED_PROMOTION();
1653 /* evacuate large objects by re-linking them onto a different list.
1655 if (bd->flags & BF_LARGE) {
1657 if (info->type == TSO &&
1658 ((StgTSO *)q)->what_next == ThreadRelocated) {
1659 q = (StgClosure *)((StgTSO *)q)->link;
1662 evacuate_large((P_)q);
1666 /* If the object is in a step that we're compacting, then we
1667 * need to use an alternative evacuate procedure.
1669 if (bd->flags & BF_COMPACTED) {
1670 if (!is_marked((P_)q,bd)) {
1672 if (mark_stack_full()) {
1673 mark_stack_overflowed = rtsTrue;
1676 push_mark_stack((P_)q);
1681 /* Object is not already evacuated. */
1682 ASSERT((bd->flags & BF_EVACUATED) == 0);
1687 else stp = NULL; // make sure copy() will crash if HEAP_ALLOCED is wrong
1690 // make sure the info pointer is into text space
1691 ASSERT(LOOKS_LIKE_CLOSURE_PTR(q));
1694 switch (info -> type) {
1698 return copy(q,sizeW_fromITBL(info),stp);
1702 StgWord w = (StgWord)q->payload[0];
1703 if (q->header.info == Czh_con_info &&
1704 // unsigned, so always true: (StgChar)w >= MIN_CHARLIKE &&
1705 (StgChar)w <= MAX_CHARLIKE) {
1706 return (StgClosure *)CHARLIKE_CLOSURE((StgChar)w);
1708 if (q->header.info == Izh_con_info &&
1709 (StgInt)w >= MIN_INTLIKE && (StgInt)w <= MAX_INTLIKE) {
1710 return (StgClosure *)INTLIKE_CLOSURE((StgInt)w);
1712 // else, fall through ...
1720 return copy(q,sizeofW(StgHeader)+1,stp);
1725 #ifdef NO_PROMOTE_THUNKS
1726 if (bd->gen_no == 0 &&
1727 bd->step->no != 0 &&
1728 bd->step->no == generations[bd->gen_no].n_steps-1) {
1732 return copy(q,sizeofW(StgHeader)+2,stp);
1740 return copy(q,sizeofW(StgHeader)+2,stp);
1746 case IND_OLDGEN_PERM:
1750 return copy(q,sizeW_fromITBL(info),stp);
1753 return copy(q,bco_sizeW((StgBCO *)q),stp);
1756 case SE_CAF_BLACKHOLE:
1759 return copyPart(q,BLACKHOLE_sizeW(),sizeofW(StgHeader),stp);
1761 case THUNK_SELECTOR:
1765 if (thunk_selector_depth > MAX_THUNK_SELECTOR_DEPTH) {
1766 return copy(q,THUNK_SELECTOR_sizeW(),stp);
1769 p = eval_thunk_selector(info->layout.selector_offset,
1773 return copy(q,THUNK_SELECTOR_sizeW(),stp);
1775 // q is still BLACKHOLE'd.
1776 thunk_selector_depth++;
1778 thunk_selector_depth--;
1781 // We store the size of the just evacuated object in the
1782 // LDV word so that the profiler can guess the position of
1783 // the next object later.
1784 SET_EVACUAEE_FOR_LDV(q, THUNK_SELECTOR_sizeW());
1792 // follow chains of indirections, don't evacuate them
1793 q = ((StgInd*)q)->indirectee;
1797 if (info->srt_bitmap != 0 && major_gc &&
1798 THUNK_STATIC_LINK((StgClosure *)q) == NULL) {
1799 THUNK_STATIC_LINK((StgClosure *)q) = static_objects;
1800 static_objects = (StgClosure *)q;
1805 if (info->srt_bitmap != 0 && major_gc &&
1806 FUN_STATIC_LINK((StgClosure *)q) == NULL) {
1807 FUN_STATIC_LINK((StgClosure *)q) = static_objects;
1808 static_objects = (StgClosure *)q;
1813 /* If q->saved_info != NULL, then it's a revertible CAF - it'll be
1814 * on the CAF list, so don't do anything with it here (we'll
1815 * scavenge it later).
1818 && ((StgIndStatic *)q)->saved_info == NULL
1819 && IND_STATIC_LINK((StgClosure *)q) == NULL) {
1820 IND_STATIC_LINK((StgClosure *)q) = static_objects;
1821 static_objects = (StgClosure *)q;
1826 if (major_gc && STATIC_LINK(info,(StgClosure *)q) == NULL) {
1827 STATIC_LINK(info,(StgClosure *)q) = static_objects;
1828 static_objects = (StgClosure *)q;
1832 case CONSTR_INTLIKE:
1833 case CONSTR_CHARLIKE:
1834 case CONSTR_NOCAF_STATIC:
1835 /* no need to put these on the static linked list, they don't need
1849 case CATCH_STM_FRAME:
1850 case CATCH_RETRY_FRAME:
1851 case ATOMICALLY_FRAME:
1852 // shouldn't see these
1853 barf("evacuate: stack frame at %p\n", q);
1857 return copy(q,pap_sizeW((StgPAP*)q),stp);
1860 return copy(q,ap_stack_sizeW((StgAP_STACK*)q),stp);
1863 /* Already evacuated, just return the forwarding address.
1864 * HOWEVER: if the requested destination generation (evac_gen) is
1865 * older than the actual generation (because the object was
1866 * already evacuated to a younger generation) then we have to
1867 * set the failed_to_evac flag to indicate that we couldn't
1868 * manage to promote the object to the desired generation.
1870 if (evac_gen > 0) { // optimisation
1871 StgClosure *p = ((StgEvacuated*)q)->evacuee;
1872 if (HEAP_ALLOCED(p) && Bdescr((P_)p)->gen_no < evac_gen) {
1873 failed_to_evac = rtsTrue;
1874 TICK_GC_FAILED_PROMOTION();
1877 return ((StgEvacuated*)q)->evacuee;
1880 // just copy the block
1881 return copy(q,arr_words_sizeW((StgArrWords *)q),stp);
1884 case MUT_ARR_PTRS_FROZEN:
1885 case MUT_ARR_PTRS_FROZEN0:
1886 // just copy the block
1887 return copy(q,mut_arr_ptrs_sizeW((StgMutArrPtrs *)q),stp);
1891 StgTSO *tso = (StgTSO *)q;
1893 /* Deal with redirected TSOs (a TSO that's had its stack enlarged).
1895 if (tso->what_next == ThreadRelocated) {
1896 q = (StgClosure *)tso->link;
1900 /* To evacuate a small TSO, we need to relocate the update frame
1907 new_tso = (StgTSO *)copyPart((StgClosure *)tso,
1909 sizeofW(StgTSO), stp);
1910 move_TSO(tso, new_tso);
1911 for (p = tso->sp, q = new_tso->sp;
1912 p < tso->stack+tso->stack_size;) {
1916 return (StgClosure *)new_tso;
1923 //StgInfoTable *rip = get_closure_info(q, &size, &ptrs, &nonptrs, &vhs, str);
1924 to = copy(q,BLACKHOLE_sizeW(),stp);
1925 //ToDo: derive size etc from reverted IP
1926 //to = copy(q,size,stp);
1928 debugBelch("@@ evacuate: RBH %p (%s) to %p (%s)",
1929 q, info_type(q), to, info_type(to)));
1934 ASSERT(sizeofW(StgBlockedFetch) >= MIN_NONUPD_SIZE);
1935 to = copy(q,sizeofW(StgBlockedFetch),stp);
1937 debugBelch("@@ evacuate: %p (%s) to %p (%s)",
1938 q, info_type(q), to, info_type(to)));
1945 ASSERT(sizeofW(StgBlockedFetch) >= MIN_UPD_SIZE);
1946 to = copy(q,sizeofW(StgFetchMe),stp);
1948 debugBelch("@@ evacuate: %p (%s) to %p (%s)",
1949 q, info_type(q), to, info_type(to)));
1953 ASSERT(sizeofW(StgBlockedFetch) >= MIN_UPD_SIZE);
1954 to = copy(q,sizeofW(StgFetchMeBlockingQueue),stp);
1956 debugBelch("@@ evacuate: %p (%s) to %p (%s)",
1957 q, info_type(q), to, info_type(to)));
1962 return copy(q,sizeofW(StgTRecHeader),stp);
1964 case TVAR_WAIT_QUEUE:
1965 return copy(q,sizeofW(StgTVarWaitQueue),stp);
1968 return copy(q,sizeofW(StgTVar),stp);
1971 return copy(q,sizeofW(StgTRecChunk),stp);
1974 barf("evacuate: strange closure type %d", (int)(info->type));
1980 /* -----------------------------------------------------------------------------
1981 Evaluate a THUNK_SELECTOR if possible.
1983 returns: NULL if we couldn't evaluate this THUNK_SELECTOR, or
1984 a closure pointer if we evaluated it and this is the result. Note
1985 that "evaluating" the THUNK_SELECTOR doesn't necessarily mean
1986 reducing it to HNF, just that we have eliminated the selection.
1987 The result might be another thunk, or even another THUNK_SELECTOR.
1989 If the return value is non-NULL, the original selector thunk has
1990 been BLACKHOLE'd, and should be updated with an indirection or a
1991 forwarding pointer. If the return value is NULL, then the selector
1993 -------------------------------------------------------------------------- */
1995 static inline rtsBool
1996 is_to_space ( StgClosure *p )
2000 bd = Bdescr((StgPtr)p);
2001 if (HEAP_ALLOCED(p) &&
2002 ((bd->flags & BF_EVACUATED)
2003 || ((bd->flags & BF_COMPACTED) &&
2004 is_marked((P_)p,bd)))) {
2012 eval_thunk_selector( nat field, StgSelector * p )
2015 const StgInfoTable *info_ptr;
2016 StgClosure *selectee;
2018 selectee = p->selectee;
2020 // Save the real info pointer (NOTE: not the same as get_itbl()).
2021 info_ptr = p->header.info;
2023 // If the THUNK_SELECTOR is in a generation that we are not
2024 // collecting, then bail out early. We won't be able to save any
2025 // space in any case, and updating with an indirection is trickier
2027 if (Bdescr((StgPtr)p)->gen_no > N) {
2031 // BLACKHOLE the selector thunk, since it is now under evaluation.
2032 // This is important to stop us going into an infinite loop if
2033 // this selector thunk eventually refers to itself.
2034 SET_INFO(p,&stg_BLACKHOLE_info);
2038 // We don't want to end up in to-space, because this causes
2039 // problems when the GC later tries to evacuate the result of
2040 // eval_thunk_selector(). There are various ways this could
2043 // 1. following an IND_STATIC
2045 // 2. when the old generation is compacted, the mark phase updates
2046 // from-space pointers to be to-space pointers, and we can't
2047 // reliably tell which we're following (eg. from an IND_STATIC).
2049 // 3. compacting GC again: if we're looking at a constructor in
2050 // the compacted generation, it might point directly to objects
2051 // in to-space. We must bale out here, otherwise doing the selection
2052 // will result in a to-space pointer being returned.
2054 // (1) is dealt with using a BF_EVACUATED test on the
2055 // selectee. (2) and (3): we can tell if we're looking at an
2056 // object in the compacted generation that might point to
2057 // to-space objects by testing that (a) it is BF_COMPACTED, (b)
2058 // the compacted generation is being collected, and (c) the
2059 // object is marked. Only a marked object may have pointers that
2060 // point to to-space objects, because that happens when
2063 // The to-space test is now embodied in the in_to_space() inline
2064 // function, as it is re-used below.
2066 if (is_to_space(selectee)) {
2070 info = get_itbl(selectee);
2071 switch (info->type) {
2079 case CONSTR_NOCAF_STATIC:
2080 // check that the size is in range
2081 ASSERT(field < (StgWord32)(info->layout.payload.ptrs +
2082 info->layout.payload.nptrs));
2084 // Select the right field from the constructor, and check
2085 // that the result isn't in to-space. It might be in
2086 // to-space if, for example, this constructor contains
2087 // pointers to younger-gen objects (and is on the mut-once
2092 q = selectee->payload[field];
2093 if (is_to_space(q)) {
2103 case IND_OLDGEN_PERM:
2105 selectee = ((StgInd *)selectee)->indirectee;
2109 // We don't follow pointers into to-space; the constructor
2110 // has already been evacuated, so we won't save any space
2111 // leaks by evaluating this selector thunk anyhow.
2114 case THUNK_SELECTOR:
2118 // check that we don't recurse too much, re-using the
2119 // depth bound also used in evacuate().
2120 if (thunk_selector_depth >= MAX_THUNK_SELECTOR_DEPTH) {
2123 thunk_selector_depth++;
2125 val = eval_thunk_selector(info->layout.selector_offset,
2126 (StgSelector *)selectee);
2128 thunk_selector_depth--;
2133 // We evaluated this selector thunk, so update it with
2134 // an indirection. NOTE: we don't use UPD_IND here,
2135 // because we are guaranteed that p is in a generation
2136 // that we are collecting, and we never want to put the
2137 // indirection on a mutable list.
2139 // For the purposes of LDV profiling, we have destroyed
2140 // the original selector thunk.
2141 SET_INFO(p, info_ptr);
2142 LDV_RECORD_DEAD_FILL_SLOP_DYNAMIC(selectee);
2144 ((StgInd *)selectee)->indirectee = val;
2145 SET_INFO(selectee,&stg_IND_info);
2147 // For the purposes of LDV profiling, we have created an
2149 LDV_RECORD_CREATE(selectee);
2166 case SE_CAF_BLACKHOLE:
2178 // not evaluated yet
2182 barf("eval_thunk_selector: strange selectee %d",
2187 // We didn't manage to evaluate this thunk; restore the old info pointer
2188 SET_INFO(p, info_ptr);
2192 /* -----------------------------------------------------------------------------
2193 move_TSO is called to update the TSO structure after it has been
2194 moved from one place to another.
2195 -------------------------------------------------------------------------- */
2198 move_TSO (StgTSO *src, StgTSO *dest)
2202 // relocate the stack pointer...
2203 diff = (StgPtr)dest - (StgPtr)src; // In *words*
2204 dest->sp = (StgPtr)dest->sp + diff;
2207 /* Similar to scavenge_large_bitmap(), but we don't write back the
2208 * pointers we get back from evacuate().
2211 scavenge_large_srt_bitmap( StgLargeSRT *large_srt )
2218 bitmap = large_srt->l.bitmap[b];
2219 size = (nat)large_srt->l.size;
2220 p = (StgClosure **)large_srt->srt;
2221 for (i = 0; i < size; ) {
2222 if ((bitmap & 1) != 0) {
2227 if (i % BITS_IN(W_) == 0) {
2229 bitmap = large_srt->l.bitmap[b];
2231 bitmap = bitmap >> 1;
2236 /* evacuate the SRT. If srt_bitmap is zero, then there isn't an
2237 * srt field in the info table. That's ok, because we'll
2238 * never dereference it.
2241 scavenge_srt (StgClosure **srt, nat srt_bitmap)
2246 bitmap = srt_bitmap;
2249 if (bitmap == (StgHalfWord)(-1)) {
2250 scavenge_large_srt_bitmap( (StgLargeSRT *)srt );
2254 while (bitmap != 0) {
2255 if ((bitmap & 1) != 0) {
2256 #ifdef ENABLE_WIN32_DLL_SUPPORT
2257 // Special-case to handle references to closures hiding out in DLLs, since
2258 // double indirections required to get at those. The code generator knows
2259 // which is which when generating the SRT, so it stores the (indirect)
2260 // reference to the DLL closure in the table by first adding one to it.
2261 // We check for this here, and undo the addition before evacuating it.
2263 // If the SRT entry hasn't got bit 0 set, the SRT entry points to a
2264 // closure that's fixed at link-time, and no extra magic is required.
2265 if ( (unsigned long)(*srt) & 0x1 ) {
2266 evacuate(*stgCast(StgClosure**,(stgCast(unsigned long, *srt) & ~0x1)));
2275 bitmap = bitmap >> 1;
2281 scavenge_thunk_srt(const StgInfoTable *info)
2283 StgThunkInfoTable *thunk_info;
2285 thunk_info = itbl_to_thunk_itbl(info);
2286 scavenge_srt((StgClosure **)GET_SRT(thunk_info), thunk_info->i.srt_bitmap);
2290 scavenge_fun_srt(const StgInfoTable *info)
2292 StgFunInfoTable *fun_info;
2294 fun_info = itbl_to_fun_itbl(info);
2295 scavenge_srt((StgClosure **)GET_FUN_SRT(fun_info), fun_info->i.srt_bitmap);
2299 scavenge_ret_srt(const StgInfoTable *info)
2301 StgRetInfoTable *ret_info;
2303 ret_info = itbl_to_ret_itbl(info);
2304 scavenge_srt((StgClosure **)GET_SRT(ret_info), ret_info->i.srt_bitmap);
2307 /* -----------------------------------------------------------------------------
2309 -------------------------------------------------------------------------- */
2312 scavengeTSO (StgTSO *tso)
2314 // chase the link field for any TSOs on the same queue
2315 tso->link = (StgTSO *)evacuate((StgClosure *)tso->link);
2316 if ( tso->why_blocked == BlockedOnMVar
2317 || tso->why_blocked == BlockedOnBlackHole
2318 || tso->why_blocked == BlockedOnException
2320 || tso->why_blocked == BlockedOnGA
2321 || tso->why_blocked == BlockedOnGA_NoSend
2324 tso->block_info.closure = evacuate(tso->block_info.closure);
2326 if ( tso->blocked_exceptions != NULL ) {
2327 tso->blocked_exceptions =
2328 (StgTSO *)evacuate((StgClosure *)tso->blocked_exceptions);
2331 // scavange current transaction record
2332 tso->trec = (StgTRecHeader *)evacuate((StgClosure *)tso->trec);
2334 // scavenge this thread's stack
2335 scavenge_stack(tso->sp, &(tso->stack[tso->stack_size]));
2338 /* -----------------------------------------------------------------------------
2339 Blocks of function args occur on the stack (at the top) and
2341 -------------------------------------------------------------------------- */
2343 STATIC_INLINE StgPtr
2344 scavenge_arg_block (StgFunInfoTable *fun_info, StgClosure **args)
2351 switch (fun_info->f.fun_type) {
2353 bitmap = BITMAP_BITS(fun_info->f.b.bitmap);
2354 size = BITMAP_SIZE(fun_info->f.b.bitmap);
2357 size = GET_FUN_LARGE_BITMAP(fun_info)->size;
2358 scavenge_large_bitmap(p, GET_FUN_LARGE_BITMAP(fun_info), size);
2362 bitmap = BITMAP_BITS(stg_arg_bitmaps[fun_info->f.fun_type]);
2363 size = BITMAP_SIZE(stg_arg_bitmaps[fun_info->f.fun_type]);
2366 if ((bitmap & 1) == 0) {
2367 *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
2370 bitmap = bitmap >> 1;
2378 STATIC_INLINE StgPtr
2379 scavenge_PAP (StgPAP *pap)
2382 StgWord bitmap, size;
2383 StgFunInfoTable *fun_info;
2385 pap->fun = evacuate(pap->fun);
2386 fun_info = get_fun_itbl(pap->fun);
2387 ASSERT(fun_info->i.type != PAP);
2389 p = (StgPtr)pap->payload;
2392 switch (fun_info->f.fun_type) {
2394 bitmap = BITMAP_BITS(fun_info->f.b.bitmap);
2397 scavenge_large_bitmap(p, GET_FUN_LARGE_BITMAP(fun_info), size);
2401 scavenge_large_bitmap((StgPtr)pap->payload, BCO_BITMAP(pap->fun), size);
2405 bitmap = BITMAP_BITS(stg_arg_bitmaps[fun_info->f.fun_type]);
2409 if ((bitmap & 1) == 0) {
2410 *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
2413 bitmap = bitmap >> 1;
2421 /* -----------------------------------------------------------------------------
2422 Scavenge a given step until there are no more objects in this step
2425 evac_gen is set by the caller to be either zero (for a step in a
2426 generation < N) or G where G is the generation of the step being
2429 We sometimes temporarily change evac_gen back to zero if we're
2430 scavenging a mutable object where early promotion isn't such a good
2432 -------------------------------------------------------------------------- */
2440 nat saved_evac_gen = evac_gen;
2445 failed_to_evac = rtsFalse;
2447 /* scavenge phase - standard breadth-first scavenging of the
2451 while (bd != stp->hp_bd || p < stp->hp) {
2453 // If we're at the end of this block, move on to the next block
2454 if (bd != stp->hp_bd && p == bd->free) {
2460 ASSERT(LOOKS_LIKE_CLOSURE_PTR(p));
2461 info = get_itbl((StgClosure *)p);
2463 ASSERT(thunk_selector_depth == 0);
2466 switch (info->type) {
2470 StgMVar *mvar = ((StgMVar *)p);
2472 mvar->head = (StgTSO *)evacuate((StgClosure *)mvar->head);
2473 mvar->tail = (StgTSO *)evacuate((StgClosure *)mvar->tail);
2474 mvar->value = evacuate((StgClosure *)mvar->value);
2475 evac_gen = saved_evac_gen;
2476 failed_to_evac = rtsTrue; // mutable.
2477 p += sizeofW(StgMVar);
2482 scavenge_fun_srt(info);
2483 ((StgClosure *)p)->payload[1] = evacuate(((StgClosure *)p)->payload[1]);
2484 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
2485 p += sizeofW(StgHeader) + 2;
2489 scavenge_thunk_srt(info);
2491 ((StgClosure *)p)->payload[1] = evacuate(((StgClosure *)p)->payload[1]);
2492 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
2493 p += sizeofW(StgHeader) + 2;
2497 scavenge_thunk_srt(info);
2498 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
2499 p += sizeofW(StgHeader) + 1;
2503 scavenge_fun_srt(info);
2505 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
2506 p += sizeofW(StgHeader) + 1;
2510 scavenge_thunk_srt(info);
2511 p += sizeofW(StgHeader) + 1;
2515 scavenge_fun_srt(info);
2517 p += sizeofW(StgHeader) + 1;
2521 scavenge_thunk_srt(info);
2522 p += sizeofW(StgHeader) + 2;
2526 scavenge_fun_srt(info);
2528 p += sizeofW(StgHeader) + 2;
2532 scavenge_thunk_srt(info);
2533 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
2534 p += sizeofW(StgHeader) + 2;
2538 scavenge_fun_srt(info);
2540 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
2541 p += sizeofW(StgHeader) + 2;
2545 scavenge_fun_srt(info);
2549 scavenge_thunk_srt(info);
2560 end = (P_)((StgClosure *)p)->payload + info->layout.payload.ptrs;
2561 for (p = (P_)((StgClosure *)p)->payload; p < end; p++) {
2562 *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
2564 p += info->layout.payload.nptrs;
2569 StgBCO *bco = (StgBCO *)p;
2570 bco->instrs = (StgArrWords *)evacuate((StgClosure *)bco->instrs);
2571 bco->literals = (StgArrWords *)evacuate((StgClosure *)bco->literals);
2572 bco->ptrs = (StgMutArrPtrs *)evacuate((StgClosure *)bco->ptrs);
2573 bco->itbls = (StgArrWords *)evacuate((StgClosure *)bco->itbls);
2574 p += bco_sizeW(bco);
2579 if (stp->gen->no != 0) {
2582 // No need to call LDV_recordDead_FILL_SLOP_DYNAMIC() because an
2583 // IND_OLDGEN_PERM closure is larger than an IND_PERM closure.
2584 LDV_recordDead((StgClosure *)p, sizeofW(StgInd));
2587 // Todo: maybe use SET_HDR() and remove LDV_RECORD_CREATE()?
2589 SET_INFO(((StgClosure *)p), &stg_IND_OLDGEN_PERM_info);
2591 // We pretend that p has just been created.
2592 LDV_RECORD_CREATE((StgClosure *)p);
2595 case IND_OLDGEN_PERM:
2596 ((StgInd *)p)->indirectee = evacuate(((StgInd *)p)->indirectee);
2597 p += sizeofW(StgInd);
2602 ((StgMutVar *)p)->var = evacuate(((StgMutVar *)p)->var);
2603 evac_gen = saved_evac_gen;
2604 failed_to_evac = rtsTrue; // mutable anyhow
2605 p += sizeofW(StgMutVar);
2609 case SE_CAF_BLACKHOLE:
2612 p += BLACKHOLE_sizeW();
2615 case THUNK_SELECTOR:
2617 StgSelector *s = (StgSelector *)p;
2618 s->selectee = evacuate(s->selectee);
2619 p += THUNK_SELECTOR_sizeW();
2623 // A chunk of stack saved in a heap object
2626 StgAP_STACK *ap = (StgAP_STACK *)p;
2628 ap->fun = evacuate(ap->fun);
2629 scavenge_stack((StgPtr)ap->payload, (StgPtr)ap->payload + ap->size);
2630 p = (StgPtr)ap->payload + ap->size;
2636 p = scavenge_PAP((StgPAP *)p);
2640 // nothing to follow
2641 p += arr_words_sizeW((StgArrWords *)p);
2645 // follow everything
2649 evac_gen = 0; // repeatedly mutable
2650 next = p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
2651 for (p = (P_)((StgMutArrPtrs *)p)->payload; p < next; p++) {
2652 *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
2654 evac_gen = saved_evac_gen;
2655 failed_to_evac = rtsTrue; // mutable anyhow.
2659 case MUT_ARR_PTRS_FROZEN:
2660 case MUT_ARR_PTRS_FROZEN0:
2661 // follow everything
2665 next = p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
2666 for (p = (P_)((StgMutArrPtrs *)p)->payload; p < next; p++) {
2667 *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
2669 // it's tempting to recordMutable() if failed_to_evac is
2670 // false, but that breaks some assumptions (eg. every
2671 // closure on the mutable list is supposed to have the MUT
2672 // flag set, and MUT_ARR_PTRS_FROZEN doesn't).
2678 StgTSO *tso = (StgTSO *)p;
2681 evac_gen = saved_evac_gen;
2682 failed_to_evac = rtsTrue; // mutable anyhow.
2683 p += tso_sizeW(tso);
2691 nat size, ptrs, nonptrs, vhs;
2693 StgInfoTable *rip = get_closure_info(p, &size, &ptrs, &nonptrs, &vhs, str);
2695 StgRBH *rbh = (StgRBH *)p;
2696 (StgClosure *)rbh->blocking_queue =
2697 evacuate((StgClosure *)rbh->blocking_queue);
2698 failed_to_evac = rtsTrue; // mutable anyhow.
2700 debugBelch("@@ scavenge: RBH %p (%s) (new blocking_queue link=%p)",
2701 p, info_type(p), (StgClosure *)rbh->blocking_queue));
2702 // ToDo: use size of reverted closure here!
2703 p += BLACKHOLE_sizeW();
2709 StgBlockedFetch *bf = (StgBlockedFetch *)p;
2710 // follow the pointer to the node which is being demanded
2711 (StgClosure *)bf->node =
2712 evacuate((StgClosure *)bf->node);
2713 // follow the link to the rest of the blocking queue
2714 (StgClosure *)bf->link =
2715 evacuate((StgClosure *)bf->link);
2717 debugBelch("@@ scavenge: %p (%s); node is now %p; exciting, isn't it",
2718 bf, info_type((StgClosure *)bf),
2719 bf->node, info_type(bf->node)));
2720 p += sizeofW(StgBlockedFetch);
2728 p += sizeofW(StgFetchMe);
2729 break; // nothing to do in this case
2733 StgFetchMeBlockingQueue *fmbq = (StgFetchMeBlockingQueue *)p;
2734 (StgClosure *)fmbq->blocking_queue =
2735 evacuate((StgClosure *)fmbq->blocking_queue);
2737 debugBelch("@@ scavenge: %p (%s) exciting, isn't it",
2738 p, info_type((StgClosure *)p)));
2739 p += sizeofW(StgFetchMeBlockingQueue);
2744 case TVAR_WAIT_QUEUE:
2746 StgTVarWaitQueue *wq = ((StgTVarWaitQueue *) p);
2748 wq->waiting_tso = (StgTSO *)evacuate((StgClosure*)wq->waiting_tso);
2749 wq->next_queue_entry = (StgTVarWaitQueue *)evacuate((StgClosure*)wq->next_queue_entry);
2750 wq->prev_queue_entry = (StgTVarWaitQueue *)evacuate((StgClosure*)wq->prev_queue_entry);
2751 evac_gen = saved_evac_gen;
2752 failed_to_evac = rtsTrue; // mutable
2753 p += sizeofW(StgTVarWaitQueue);
2759 StgTVar *tvar = ((StgTVar *) p);
2761 tvar->current_value = evacuate((StgClosure*)tvar->current_value);
2762 tvar->first_wait_queue_entry = (StgTVarWaitQueue *)evacuate((StgClosure*)tvar->first_wait_queue_entry);
2763 evac_gen = saved_evac_gen;
2764 failed_to_evac = rtsTrue; // mutable
2765 p += sizeofW(StgTVar);
2771 StgTRecHeader *trec = ((StgTRecHeader *) p);
2773 trec->enclosing_trec = (StgTRecHeader *)evacuate((StgClosure*)trec->enclosing_trec);
2774 trec->current_chunk = (StgTRecChunk *)evacuate((StgClosure*)trec->current_chunk);
2775 evac_gen = saved_evac_gen;
2776 failed_to_evac = rtsTrue; // mutable
2777 p += sizeofW(StgTRecHeader);
2784 StgTRecChunk *tc = ((StgTRecChunk *) p);
2785 TRecEntry *e = &(tc -> entries[0]);
2787 tc->prev_chunk = (StgTRecChunk *)evacuate((StgClosure*)tc->prev_chunk);
2788 for (i = 0; i < tc -> next_entry_idx; i ++, e++ ) {
2789 e->tvar = (StgTVar *)evacuate((StgClosure*)e->tvar);
2790 e->expected_value = evacuate((StgClosure*)e->expected_value);
2791 e->new_value = evacuate((StgClosure*)e->new_value);
2793 evac_gen = saved_evac_gen;
2794 failed_to_evac = rtsTrue; // mutable
2795 p += sizeofW(StgTRecChunk);
2800 barf("scavenge: unimplemented/strange closure type %d @ %p",
2805 * We need to record the current object on the mutable list if
2806 * (a) It is actually mutable, or
2807 * (b) It contains pointers to a younger generation.
2808 * Case (b) arises if we didn't manage to promote everything that
2809 * the current object points to into the current generation.
2811 if (failed_to_evac) {
2812 failed_to_evac = rtsFalse;
2813 recordMutableGen((StgClosure *)q, stp->gen);
2821 /* -----------------------------------------------------------------------------
2822 Scavenge everything on the mark stack.
2824 This is slightly different from scavenge():
2825 - we don't walk linearly through the objects, so the scavenger
2826 doesn't need to advance the pointer on to the next object.
2827 -------------------------------------------------------------------------- */
2830 scavenge_mark_stack(void)
2836 evac_gen = oldest_gen->no;
2837 saved_evac_gen = evac_gen;
2840 while (!mark_stack_empty()) {
2841 p = pop_mark_stack();
2843 ASSERT(LOOKS_LIKE_CLOSURE_PTR(p));
2844 info = get_itbl((StgClosure *)p);
2847 switch (info->type) {
2851 StgMVar *mvar = ((StgMVar *)p);
2853 mvar->head = (StgTSO *)evacuate((StgClosure *)mvar->head);
2854 mvar->tail = (StgTSO *)evacuate((StgClosure *)mvar->tail);
2855 mvar->value = evacuate((StgClosure *)mvar->value);
2856 evac_gen = saved_evac_gen;
2857 failed_to_evac = rtsTrue; // mutable.
2862 scavenge_fun_srt(info);
2863 ((StgClosure *)p)->payload[1] = evacuate(((StgClosure *)p)->payload[1]);
2864 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
2868 scavenge_thunk_srt(info);
2870 ((StgClosure *)p)->payload[1] = evacuate(((StgClosure *)p)->payload[1]);
2871 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
2876 scavenge_fun_srt(info);
2877 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
2882 scavenge_thunk_srt(info);
2885 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
2890 scavenge_fun_srt(info);
2895 scavenge_thunk_srt(info);
2903 scavenge_fun_srt(info);
2907 scavenge_thunk_srt(info);
2918 end = (P_)((StgClosure *)p)->payload + info->layout.payload.ptrs;
2919 for (p = (P_)((StgClosure *)p)->payload; p < end; p++) {
2920 *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
2926 StgBCO *bco = (StgBCO *)p;
2927 bco->instrs = (StgArrWords *)evacuate((StgClosure *)bco->instrs);
2928 bco->literals = (StgArrWords *)evacuate((StgClosure *)bco->literals);
2929 bco->ptrs = (StgMutArrPtrs *)evacuate((StgClosure *)bco->ptrs);
2930 bco->itbls = (StgArrWords *)evacuate((StgClosure *)bco->itbls);
2935 // don't need to do anything here: the only possible case
2936 // is that we're in a 1-space compacting collector, with
2937 // no "old" generation.
2941 case IND_OLDGEN_PERM:
2942 ((StgInd *)p)->indirectee =
2943 evacuate(((StgInd *)p)->indirectee);
2948 ((StgMutVar *)p)->var = evacuate(((StgMutVar *)p)->var);
2949 evac_gen = saved_evac_gen;
2950 failed_to_evac = rtsTrue;
2954 case SE_CAF_BLACKHOLE:
2960 case THUNK_SELECTOR:
2962 StgSelector *s = (StgSelector *)p;
2963 s->selectee = evacuate(s->selectee);
2967 // A chunk of stack saved in a heap object
2970 StgAP_STACK *ap = (StgAP_STACK *)p;
2972 ap->fun = evacuate(ap->fun);
2973 scavenge_stack((StgPtr)ap->payload, (StgPtr)ap->payload + ap->size);
2979 scavenge_PAP((StgPAP *)p);
2983 // follow everything
2987 evac_gen = 0; // repeatedly mutable
2988 next = p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
2989 for (p = (P_)((StgMutArrPtrs *)p)->payload; p < next; p++) {
2990 *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
2992 evac_gen = saved_evac_gen;
2993 failed_to_evac = rtsTrue; // mutable anyhow.
2997 case MUT_ARR_PTRS_FROZEN:
2998 case MUT_ARR_PTRS_FROZEN0:
2999 // follow everything
3003 next = p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
3004 for (p = (P_)((StgMutArrPtrs *)p)->payload; p < next; p++) {
3005 *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
3012 StgTSO *tso = (StgTSO *)p;
3015 evac_gen = saved_evac_gen;
3016 failed_to_evac = rtsTrue;
3024 nat size, ptrs, nonptrs, vhs;
3026 StgInfoTable *rip = get_closure_info(p, &size, &ptrs, &nonptrs, &vhs, str);
3028 StgRBH *rbh = (StgRBH *)p;
3029 bh->blocking_queue =
3030 (StgTSO *)evacuate((StgClosure *)bh->blocking_queue);
3031 failed_to_evac = rtsTrue; // mutable anyhow.
3033 debugBelch("@@ scavenge: RBH %p (%s) (new blocking_queue link=%p)",
3034 p, info_type(p), (StgClosure *)rbh->blocking_queue));
3040 StgBlockedFetch *bf = (StgBlockedFetch *)p;
3041 // follow the pointer to the node which is being demanded
3042 (StgClosure *)bf->node =
3043 evacuate((StgClosure *)bf->node);
3044 // follow the link to the rest of the blocking queue
3045 (StgClosure *)bf->link =
3046 evacuate((StgClosure *)bf->link);
3048 debugBelch("@@ scavenge: %p (%s); node is now %p; exciting, isn't it",
3049 bf, info_type((StgClosure *)bf),
3050 bf->node, info_type(bf->node)));
3058 break; // nothing to do in this case
3062 StgFetchMeBlockingQueue *fmbq = (StgFetchMeBlockingQueue *)p;
3063 (StgClosure *)fmbq->blocking_queue =
3064 evacuate((StgClosure *)fmbq->blocking_queue);
3066 debugBelch("@@ scavenge: %p (%s) exciting, isn't it",
3067 p, info_type((StgClosure *)p)));
3072 case TVAR_WAIT_QUEUE:
3074 StgTVarWaitQueue *wq = ((StgTVarWaitQueue *) p);
3076 wq->waiting_tso = (StgTSO *)evacuate((StgClosure*)wq->waiting_tso);
3077 wq->next_queue_entry = (StgTVarWaitQueue *)evacuate((StgClosure*)wq->next_queue_entry);
3078 wq->prev_queue_entry = (StgTVarWaitQueue *)evacuate((StgClosure*)wq->prev_queue_entry);
3079 evac_gen = saved_evac_gen;
3080 failed_to_evac = rtsTrue; // mutable
3086 StgTVar *tvar = ((StgTVar *) p);
3088 tvar->current_value = evacuate((StgClosure*)tvar->current_value);
3089 tvar->first_wait_queue_entry = (StgTVarWaitQueue *)evacuate((StgClosure*)tvar->first_wait_queue_entry);
3090 evac_gen = saved_evac_gen;
3091 failed_to_evac = rtsTrue; // mutable
3098 StgTRecChunk *tc = ((StgTRecChunk *) p);
3099 TRecEntry *e = &(tc -> entries[0]);
3101 tc->prev_chunk = (StgTRecChunk *)evacuate((StgClosure*)tc->prev_chunk);
3102 for (i = 0; i < tc -> next_entry_idx; i ++, e++ ) {
3103 e->tvar = (StgTVar *)evacuate((StgClosure*)e->tvar);
3104 e->expected_value = evacuate((StgClosure*)e->expected_value);
3105 e->new_value = evacuate((StgClosure*)e->new_value);
3107 evac_gen = saved_evac_gen;
3108 failed_to_evac = rtsTrue; // mutable
3114 StgTRecHeader *trec = ((StgTRecHeader *) p);
3116 trec->enclosing_trec = (StgTRecHeader *)evacuate((StgClosure*)trec->enclosing_trec);
3117 trec->current_chunk = (StgTRecChunk *)evacuate((StgClosure*)trec->current_chunk);
3118 evac_gen = saved_evac_gen;
3119 failed_to_evac = rtsTrue; // mutable
3124 barf("scavenge_mark_stack: unimplemented/strange closure type %d @ %p",
3128 if (failed_to_evac) {
3129 failed_to_evac = rtsFalse;
3130 recordMutableGen((StgClosure *)q, &generations[evac_gen]);
3133 // mark the next bit to indicate "scavenged"
3134 mark(q+1, Bdescr(q));
3136 } // while (!mark_stack_empty())
3138 // start a new linear scan if the mark stack overflowed at some point
3139 if (mark_stack_overflowed && oldgen_scan_bd == NULL) {
3140 IF_DEBUG(gc, debugBelch("scavenge_mark_stack: starting linear scan"));
3141 mark_stack_overflowed = rtsFalse;
3142 oldgen_scan_bd = oldest_gen->steps[0].blocks;
3143 oldgen_scan = oldgen_scan_bd->start;
3146 if (oldgen_scan_bd) {
3147 // push a new thing on the mark stack
3149 // find a closure that is marked but not scavenged, and start
3151 while (oldgen_scan < oldgen_scan_bd->free
3152 && !is_marked(oldgen_scan,oldgen_scan_bd)) {
3156 if (oldgen_scan < oldgen_scan_bd->free) {
3158 // already scavenged?
3159 if (is_marked(oldgen_scan+1,oldgen_scan_bd)) {
3160 oldgen_scan += sizeofW(StgHeader) + MIN_NONUPD_SIZE;
3163 push_mark_stack(oldgen_scan);
3164 // ToDo: bump the linear scan by the actual size of the object
3165 oldgen_scan += sizeofW(StgHeader) + MIN_NONUPD_SIZE;
3169 oldgen_scan_bd = oldgen_scan_bd->link;
3170 if (oldgen_scan_bd != NULL) {
3171 oldgen_scan = oldgen_scan_bd->start;
3177 /* -----------------------------------------------------------------------------
3178 Scavenge one object.
3180 This is used for objects that are temporarily marked as mutable
3181 because they contain old-to-new generation pointers. Only certain
3182 objects can have this property.
3183 -------------------------------------------------------------------------- */
3186 scavenge_one(StgPtr p)
3188 const StgInfoTable *info;
3189 nat saved_evac_gen = evac_gen;
3192 ASSERT(LOOKS_LIKE_CLOSURE_PTR(p));
3193 info = get_itbl((StgClosure *)p);
3195 switch (info->type) {
3199 StgMVar *mvar = ((StgMVar *)p);
3201 mvar->head = (StgTSO *)evacuate((StgClosure *)mvar->head);
3202 mvar->tail = (StgTSO *)evacuate((StgClosure *)mvar->tail);
3203 mvar->value = evacuate((StgClosure *)mvar->value);
3204 evac_gen = saved_evac_gen;
3205 failed_to_evac = rtsTrue; // mutable.
3210 case FUN_1_0: // hardly worth specialising these guys
3233 end = (StgPtr)((StgClosure *)p)->payload + info->layout.payload.ptrs;
3234 for (q = (StgPtr)((StgClosure *)p)->payload; q < end; q++) {
3235 *q = (StgWord)(StgPtr)evacuate((StgClosure *)*q);
3242 ((StgMutVar *)p)->var = evacuate(((StgMutVar *)p)->var);
3243 evac_gen = saved_evac_gen;
3244 failed_to_evac = rtsTrue; // mutable anyhow
3248 case SE_CAF_BLACKHOLE:
3253 case THUNK_SELECTOR:
3255 StgSelector *s = (StgSelector *)p;
3256 s->selectee = evacuate(s->selectee);
3262 StgAP_STACK *ap = (StgAP_STACK *)p;
3264 ap->fun = evacuate(ap->fun);
3265 scavenge_stack((StgPtr)ap->payload, (StgPtr)ap->payload + ap->size);
3266 p = (StgPtr)ap->payload + ap->size;
3272 p = scavenge_PAP((StgPAP *)p);
3276 // nothing to follow
3281 // follow everything
3284 evac_gen = 0; // repeatedly mutable
3285 next = p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
3286 for (p = (P_)((StgMutArrPtrs *)p)->payload; p < next; p++) {
3287 *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
3289 evac_gen = saved_evac_gen;
3290 failed_to_evac = rtsTrue;
3294 case MUT_ARR_PTRS_FROZEN:
3295 case MUT_ARR_PTRS_FROZEN0:
3297 // follow everything
3300 next = p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
3301 for (p = (P_)((StgMutArrPtrs *)p)->payload; p < next; p++) {
3302 *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
3309 StgTSO *tso = (StgTSO *)p;
3311 evac_gen = 0; // repeatedly mutable
3313 evac_gen = saved_evac_gen;
3314 failed_to_evac = rtsTrue;
3322 nat size, ptrs, nonptrs, vhs;
3324 StgInfoTable *rip = get_closure_info(p, &size, &ptrs, &nonptrs, &vhs, str);
3326 StgRBH *rbh = (StgRBH *)p;
3327 (StgClosure *)rbh->blocking_queue =
3328 evacuate((StgClosure *)rbh->blocking_queue);
3329 failed_to_evac = rtsTrue; // mutable anyhow.
3331 debugBelch("@@ scavenge: RBH %p (%s) (new blocking_queue link=%p)",
3332 p, info_type(p), (StgClosure *)rbh->blocking_queue));
3333 // ToDo: use size of reverted closure here!
3339 StgBlockedFetch *bf = (StgBlockedFetch *)p;
3340 // follow the pointer to the node which is being demanded
3341 (StgClosure *)bf->node =
3342 evacuate((StgClosure *)bf->node);
3343 // follow the link to the rest of the blocking queue
3344 (StgClosure *)bf->link =
3345 evacuate((StgClosure *)bf->link);
3347 debugBelch("@@ scavenge: %p (%s); node is now %p; exciting, isn't it",
3348 bf, info_type((StgClosure *)bf),
3349 bf->node, info_type(bf->node)));
3357 break; // nothing to do in this case
3361 StgFetchMeBlockingQueue *fmbq = (StgFetchMeBlockingQueue *)p;
3362 (StgClosure *)fmbq->blocking_queue =
3363 evacuate((StgClosure *)fmbq->blocking_queue);
3365 debugBelch("@@ scavenge: %p (%s) exciting, isn't it",
3366 p, info_type((StgClosure *)p)));
3371 case TVAR_WAIT_QUEUE:
3373 StgTVarWaitQueue *wq = ((StgTVarWaitQueue *) p);
3375 wq->waiting_tso = (StgTSO *)evacuate((StgClosure*)wq->waiting_tso);
3376 wq->next_queue_entry = (StgTVarWaitQueue *)evacuate((StgClosure*)wq->next_queue_entry);
3377 wq->prev_queue_entry = (StgTVarWaitQueue *)evacuate((StgClosure*)wq->prev_queue_entry);
3378 evac_gen = saved_evac_gen;
3379 failed_to_evac = rtsTrue; // mutable
3385 StgTVar *tvar = ((StgTVar *) p);
3387 tvar->current_value = evacuate((StgClosure*)tvar->current_value);
3388 tvar->first_wait_queue_entry = (StgTVarWaitQueue *)evacuate((StgClosure*)tvar->first_wait_queue_entry);
3389 evac_gen = saved_evac_gen;
3390 failed_to_evac = rtsTrue; // mutable
3396 StgTRecHeader *trec = ((StgTRecHeader *) p);
3398 trec->enclosing_trec = (StgTRecHeader *)evacuate((StgClosure*)trec->enclosing_trec);
3399 trec->current_chunk = (StgTRecChunk *)evacuate((StgClosure*)trec->current_chunk);
3400 evac_gen = saved_evac_gen;
3401 failed_to_evac = rtsTrue; // mutable
3408 StgTRecChunk *tc = ((StgTRecChunk *) p);
3409 TRecEntry *e = &(tc -> entries[0]);
3411 tc->prev_chunk = (StgTRecChunk *)evacuate((StgClosure*)tc->prev_chunk);
3412 for (i = 0; i < tc -> next_entry_idx; i ++, e++ ) {
3413 e->tvar = (StgTVar *)evacuate((StgClosure*)e->tvar);
3414 e->expected_value = evacuate((StgClosure*)e->expected_value);
3415 e->new_value = evacuate((StgClosure*)e->new_value);
3417 evac_gen = saved_evac_gen;
3418 failed_to_evac = rtsTrue; // mutable
3423 case IND_OLDGEN_PERM:
3426 /* Careful here: a THUNK can be on the mutable list because
3427 * it contains pointers to young gen objects. If such a thunk
3428 * is updated, the IND_OLDGEN will be added to the mutable
3429 * list again, and we'll scavenge it twice. evacuate()
3430 * doesn't check whether the object has already been
3431 * evacuated, so we perform that check here.
3433 StgClosure *q = ((StgInd *)p)->indirectee;
3434 if (HEAP_ALLOCED(q) && Bdescr((StgPtr)q)->flags & BF_EVACUATED) {
3437 ((StgInd *)p)->indirectee = evacuate(q);
3440 #if 0 && defined(DEBUG)
3441 if (RtsFlags.DebugFlags.gc)
3442 /* Debugging code to print out the size of the thing we just
3446 StgPtr start = gen->steps[0].scan;
3447 bdescr *start_bd = gen->steps[0].scan_bd;
3449 scavenge(&gen->steps[0]);
3450 if (start_bd != gen->steps[0].scan_bd) {
3451 size += (P_)BLOCK_ROUND_UP(start) - start;
3452 start_bd = start_bd->link;
3453 while (start_bd != gen->steps[0].scan_bd) {
3454 size += BLOCK_SIZE_W;
3455 start_bd = start_bd->link;
3457 size += gen->steps[0].scan -
3458 (P_)BLOCK_ROUND_DOWN(gen->steps[0].scan);
3460 size = gen->steps[0].scan - start;
3462 debugBelch("evac IND_OLDGEN: %ld bytes", size * sizeof(W_));
3468 barf("scavenge_one: strange object %d", (int)(info->type));
3471 no_luck = failed_to_evac;
3472 failed_to_evac = rtsFalse;
3476 /* -----------------------------------------------------------------------------
3477 Scavenging mutable lists.
3479 We treat the mutable list of each generation > N (i.e. all the
3480 generations older than the one being collected) as roots. We also
3481 remove non-mutable objects from the mutable list at this point.
3482 -------------------------------------------------------------------------- */
3485 scavenge_mutable_list(generation *gen)
3490 bd = gen->saved_mut_list;
3493 for (; bd != NULL; bd = bd->link) {
3494 for (q = bd->start; q < bd->free; q++) {
3496 ASSERT(LOOKS_LIKE_CLOSURE_PTR(p));
3497 if (scavenge_one(p)) {
3498 /* didn't manage to promote everything, so put the
3499 * object back on the list.
3501 recordMutableGen((StgClosure *)p,gen);
3506 // free the old mut_list
3507 freeChain(gen->saved_mut_list);
3508 gen->saved_mut_list = NULL;
3513 scavenge_static(void)
3515 StgClosure* p = static_objects;
3516 const StgInfoTable *info;
3518 /* Always evacuate straight to the oldest generation for static
3520 evac_gen = oldest_gen->no;
3522 /* keep going until we've scavenged all the objects on the linked
3524 while (p != END_OF_STATIC_LIST) {
3526 ASSERT(LOOKS_LIKE_CLOSURE_PTR(p));
3529 if (info->type==RBH)
3530 info = REVERT_INFOPTR(info); // if it's an RBH, look at the orig closure
3532 // make sure the info pointer is into text space
3534 /* Take this object *off* the static_objects list,
3535 * and put it on the scavenged_static_objects list.
3537 static_objects = STATIC_LINK(info,p);
3538 STATIC_LINK(info,p) = scavenged_static_objects;
3539 scavenged_static_objects = p;
3541 switch (info -> type) {
3545 StgInd *ind = (StgInd *)p;
3546 ind->indirectee = evacuate(ind->indirectee);
3548 /* might fail to evacuate it, in which case we have to pop it
3549 * back on the mutable list of the oldest generation. We
3550 * leave it *on* the scavenged_static_objects list, though,
3551 * in case we visit this object again.
3553 if (failed_to_evac) {
3554 failed_to_evac = rtsFalse;
3555 recordMutableGen((StgClosure *)p,oldest_gen);
3561 scavenge_thunk_srt(info);
3565 scavenge_fun_srt(info);
3572 next = (P_)p->payload + info->layout.payload.ptrs;
3573 // evacuate the pointers
3574 for (q = (P_)p->payload; q < next; q++) {
3575 *q = (StgWord)(StgPtr)evacuate((StgClosure *)*q);
3581 barf("scavenge_static: strange closure %d", (int)(info->type));
3584 ASSERT(failed_to_evac == rtsFalse);
3586 /* get the next static object from the list. Remember, there might
3587 * be more stuff on this list now that we've done some evacuating!
3588 * (static_objects is a global)
3594 /* -----------------------------------------------------------------------------
3595 scavenge a chunk of memory described by a bitmap
3596 -------------------------------------------------------------------------- */
3599 scavenge_large_bitmap( StgPtr p, StgLargeBitmap *large_bitmap, nat size )
3605 bitmap = large_bitmap->bitmap[b];
3606 for (i = 0; i < size; ) {
3607 if ((bitmap & 1) == 0) {
3608 *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
3612 if (i % BITS_IN(W_) == 0) {
3614 bitmap = large_bitmap->bitmap[b];
3616 bitmap = bitmap >> 1;
3621 STATIC_INLINE StgPtr
3622 scavenge_small_bitmap (StgPtr p, nat size, StgWord bitmap)
3625 if ((bitmap & 1) == 0) {
3626 *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
3629 bitmap = bitmap >> 1;
3635 /* -----------------------------------------------------------------------------
3636 scavenge_stack walks over a section of stack and evacuates all the
3637 objects pointed to by it. We can use the same code for walking
3638 AP_STACK_UPDs, since these are just sections of copied stack.
3639 -------------------------------------------------------------------------- */
3643 scavenge_stack(StgPtr p, StgPtr stack_end)
3645 const StgRetInfoTable* info;
3649 //IF_DEBUG(sanity, debugBelch(" scavenging stack between %p and %p", p, stack_end));
3652 * Each time around this loop, we are looking at a chunk of stack
3653 * that starts with an activation record.
3656 while (p < stack_end) {
3657 info = get_ret_itbl((StgClosure *)p);
3659 switch (info->i.type) {
3662 ((StgUpdateFrame *)p)->updatee
3663 = evacuate(((StgUpdateFrame *)p)->updatee);
3664 p += sizeofW(StgUpdateFrame);
3667 // small bitmap (< 32 entries, or 64 on a 64-bit machine)
3668 case CATCH_STM_FRAME:
3669 case CATCH_RETRY_FRAME:
3670 case ATOMICALLY_FRAME:
3675 bitmap = BITMAP_BITS(info->i.layout.bitmap);
3676 size = BITMAP_SIZE(info->i.layout.bitmap);
3677 // NOTE: the payload starts immediately after the info-ptr, we
3678 // don't have an StgHeader in the same sense as a heap closure.
3680 p = scavenge_small_bitmap(p, size, bitmap);
3683 scavenge_srt((StgClosure **)GET_SRT(info), info->i.srt_bitmap);
3691 *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
3694 size = BCO_BITMAP_SIZE(bco);
3695 scavenge_large_bitmap(p, BCO_BITMAP(bco), size);
3700 // large bitmap (> 32 entries, or > 64 on a 64-bit machine)
3706 size = GET_LARGE_BITMAP(&info->i)->size;
3708 scavenge_large_bitmap(p, GET_LARGE_BITMAP(&info->i), size);
3710 // and don't forget to follow the SRT
3714 // Dynamic bitmap: the mask is stored on the stack, and
3715 // there are a number of non-pointers followed by a number
3716 // of pointers above the bitmapped area. (see StgMacros.h,
3721 dyn = ((StgRetDyn *)p)->liveness;
3723 // traverse the bitmap first
3724 bitmap = RET_DYN_LIVENESS(dyn);
3725 p = (P_)&((StgRetDyn *)p)->payload[0];
3726 size = RET_DYN_BITMAP_SIZE;
3727 p = scavenge_small_bitmap(p, size, bitmap);
3729 // skip over the non-ptr words
3730 p += RET_DYN_NONPTRS(dyn) + RET_DYN_NONPTR_REGS_SIZE;
3732 // follow the ptr words
3733 for (size = RET_DYN_PTRS(dyn); size > 0; size--) {
3734 *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
3742 StgRetFun *ret_fun = (StgRetFun *)p;
3743 StgFunInfoTable *fun_info;
3745 ret_fun->fun = evacuate(ret_fun->fun);
3746 fun_info = get_fun_itbl(ret_fun->fun);
3747 p = scavenge_arg_block(fun_info, ret_fun->payload);
3752 barf("scavenge_stack: weird activation record found on stack: %d", (int)(info->i.type));
3757 /*-----------------------------------------------------------------------------
3758 scavenge the large object list.
3760 evac_gen set by caller; similar games played with evac_gen as with
3761 scavenge() - see comment at the top of scavenge(). Most large
3762 objects are (repeatedly) mutable, so most of the time evac_gen will
3764 --------------------------------------------------------------------------- */
3767 scavenge_large(step *stp)
3772 bd = stp->new_large_objects;
3774 for (; bd != NULL; bd = stp->new_large_objects) {
3776 /* take this object *off* the large objects list and put it on
3777 * the scavenged large objects list. This is so that we can
3778 * treat new_large_objects as a stack and push new objects on
3779 * the front when evacuating.
3781 stp->new_large_objects = bd->link;
3782 dbl_link_onto(bd, &stp->scavenged_large_objects);
3784 // update the block count in this step.
3785 stp->n_scavenged_large_blocks += bd->blocks;
3788 if (scavenge_one(p)) {
3789 recordMutableGen((StgClosure *)p, stp->gen);
3794 /* -----------------------------------------------------------------------------
3795 Initialising the static object & mutable lists
3796 -------------------------------------------------------------------------- */
3799 zero_static_object_list(StgClosure* first_static)
3803 const StgInfoTable *info;
3805 for (p = first_static; p != END_OF_STATIC_LIST; p = link) {
3807 link = STATIC_LINK(info, p);
3808 STATIC_LINK(info,p) = NULL;
3812 /* -----------------------------------------------------------------------------
3814 -------------------------------------------------------------------------- */
3821 for (c = (StgIndStatic *)revertible_caf_list; c != NULL;
3822 c = (StgIndStatic *)c->static_link)
3824 SET_INFO(c, c->saved_info);
3825 c->saved_info = NULL;
3826 // could, but not necessary: c->static_link = NULL;
3828 revertible_caf_list = NULL;
3832 markCAFs( evac_fn evac )
3836 for (c = (StgIndStatic *)caf_list; c != NULL;
3837 c = (StgIndStatic *)c->static_link)
3839 evac(&c->indirectee);
3841 for (c = (StgIndStatic *)revertible_caf_list; c != NULL;
3842 c = (StgIndStatic *)c->static_link)
3844 evac(&c->indirectee);
3848 /* -----------------------------------------------------------------------------
3849 Sanity code for CAF garbage collection.
3851 With DEBUG turned on, we manage a CAF list in addition to the SRT
3852 mechanism. After GC, we run down the CAF list and blackhole any
3853 CAFs which have been garbage collected. This means we get an error
3854 whenever the program tries to enter a garbage collected CAF.
3856 Any garbage collected CAFs are taken off the CAF list at the same
3858 -------------------------------------------------------------------------- */
3860 #if 0 && defined(DEBUG)
3867 const StgInfoTable *info;
3878 ASSERT(info->type == IND_STATIC);
3880 if (STATIC_LINK(info,p) == NULL) {
3881 IF_DEBUG(gccafs, debugBelch("CAF gc'd at 0x%04lx", (long)p));
3883 SET_INFO(p,&stg_BLACKHOLE_info);
3884 p = STATIC_LINK2(info,p);
3888 pp = &STATIC_LINK2(info,p);
3895 // debugBelch("%d CAFs live", i);
3900 /* -----------------------------------------------------------------------------
3903 Whenever a thread returns to the scheduler after possibly doing
3904 some work, we have to run down the stack and black-hole all the
3905 closures referred to by update frames.
3906 -------------------------------------------------------------------------- */
3909 threadLazyBlackHole(StgTSO *tso)
3912 StgRetInfoTable *info;
3916 stack_end = &tso->stack[tso->stack_size];
3918 frame = (StgClosure *)tso->sp;
3921 info = get_ret_itbl(frame);
3923 switch (info->i.type) {
3926 bh = ((StgUpdateFrame *)frame)->updatee;
3928 /* if the thunk is already blackholed, it means we've also
3929 * already blackholed the rest of the thunks on this stack,
3930 * so we can stop early.
3932 * The blackhole made for a CAF is a CAF_BLACKHOLE, so they
3933 * don't interfere with this optimisation.
3935 if (bh->header.info == &stg_BLACKHOLE_info) {
3939 if (bh->header.info != &stg_CAF_BLACKHOLE_info) {
3940 #if (!defined(LAZY_BLACKHOLING)) && defined(DEBUG)
3941 debugBelch("Unexpected lazy BHing required at 0x%04x",(int)bh);
3945 // We pretend that bh is now dead.
3946 LDV_recordDead_FILL_SLOP_DYNAMIC((StgClosure *)bh);
3948 SET_INFO(bh,&stg_BLACKHOLE_info);
3950 // We pretend that bh has just been created.
3951 LDV_RECORD_CREATE(bh);
3954 frame = (StgClosure *) ((StgUpdateFrame *)frame + 1);
3960 // normal stack frames; do nothing except advance the pointer
3962 frame = (StgClosure *)((StgPtr)frame + stack_frame_sizeW(frame));
3968 /* -----------------------------------------------------------------------------
3971 * Code largely pinched from old RTS, then hacked to bits. We also do
3972 * lazy black holing here.
3974 * -------------------------------------------------------------------------- */
3976 struct stack_gap { StgWord gap_size; struct stack_gap *next_gap; };
3979 threadSqueezeStack(StgTSO *tso)
3982 rtsBool prev_was_update_frame;
3983 StgClosure *updatee = NULL;
3985 StgRetInfoTable *info;
3986 StgWord current_gap_size;
3987 struct stack_gap *gap;
3990 // Traverse the stack upwards, replacing adjacent update frames
3991 // with a single update frame and a "stack gap". A stack gap
3992 // contains two values: the size of the gap, and the distance
3993 // to the next gap (or the stack top).
3995 bottom = &(tso->stack[tso->stack_size]);
3999 ASSERT(frame < bottom);
4001 prev_was_update_frame = rtsFalse;
4002 current_gap_size = 0;
4003 gap = (struct stack_gap *) (tso->sp - sizeofW(StgUpdateFrame));
4005 while (frame < bottom) {
4007 info = get_ret_itbl((StgClosure *)frame);
4008 switch (info->i.type) {
4012 StgUpdateFrame *upd = (StgUpdateFrame *)frame;
4014 if (upd->updatee->header.info == &stg_BLACKHOLE_info) {
4016 // found a BLACKHOLE'd update frame; we've been here
4017 // before, in a previous GC, so just break out.
4019 // Mark the end of the gap, if we're in one.
4020 if (current_gap_size != 0) {
4021 gap = (struct stack_gap *)(frame-sizeofW(StgUpdateFrame));
4024 frame += sizeofW(StgUpdateFrame);
4025 goto done_traversing;
4028 if (prev_was_update_frame) {
4030 TICK_UPD_SQUEEZED();
4031 /* wasn't there something about update squeezing and ticky to be
4032 * sorted out? oh yes: we aren't counting each enter properly
4033 * in this case. See the log somewhere. KSW 1999-04-21
4035 * Check two things: that the two update frames don't point to
4036 * the same object, and that the updatee_bypass isn't already an
4037 * indirection. Both of these cases only happen when we're in a
4038 * block hole-style loop (and there are multiple update frames
4039 * on the stack pointing to the same closure), but they can both
4040 * screw us up if we don't check.
4042 if (upd->updatee != updatee && !closure_IND(upd->updatee)) {
4043 UPD_IND_NOLOCK(upd->updatee, updatee);
4046 // now mark this update frame as a stack gap. The gap
4047 // marker resides in the bottom-most update frame of
4048 // the series of adjacent frames, and covers all the
4049 // frames in this series.
4050 current_gap_size += sizeofW(StgUpdateFrame);
4051 ((struct stack_gap *)frame)->gap_size = current_gap_size;
4052 ((struct stack_gap *)frame)->next_gap = gap;
4054 frame += sizeofW(StgUpdateFrame);
4058 // single update frame, or the topmost update frame in a series
4060 StgClosure *bh = upd->updatee;
4062 // Do lazy black-holing
4063 if (bh->header.info != &stg_BLACKHOLE_info &&
4064 bh->header.info != &stg_CAF_BLACKHOLE_info) {
4065 #if (!defined(LAZY_BLACKHOLING)) && defined(DEBUG)
4066 debugBelch("Unexpected lazy BHing required at 0x%04x",(int)bh);
4069 /* zero out the slop so that the sanity checker can tell
4070 * where the next closure is.
4073 StgInfoTable *bh_info = get_itbl(bh);
4074 nat np = bh_info->layout.payload.ptrs,
4075 nw = bh_info->layout.payload.nptrs, i;
4076 /* don't zero out slop for a THUNK_SELECTOR,
4077 * because its layout info is used for a
4078 * different purpose, and it's exactly the
4079 * same size as a BLACKHOLE in any case.
4081 if (bh_info->type != THUNK_SELECTOR) {
4082 for (i = 0; i < np + nw; i++) {
4083 ((StgClosure *)bh)->payload[i] = INVALID_OBJECT;
4089 // We pretend that bh is now dead.
4090 LDV_recordDead_FILL_SLOP_DYNAMIC((StgClosure *)bh);
4092 // Todo: maybe use SET_HDR() and remove LDV_RECORD_CREATE()?
4093 SET_INFO(bh,&stg_BLACKHOLE_info);
4095 // We pretend that bh has just been created.
4096 LDV_RECORD_CREATE(bh);
4099 prev_was_update_frame = rtsTrue;
4100 updatee = upd->updatee;
4101 frame += sizeofW(StgUpdateFrame);
4107 prev_was_update_frame = rtsFalse;
4109 // we're not in a gap... check whether this is the end of a gap
4110 // (an update frame can't be the end of a gap).
4111 if (current_gap_size != 0) {
4112 gap = (struct stack_gap *) (frame - sizeofW(StgUpdateFrame));
4114 current_gap_size = 0;
4116 frame += stack_frame_sizeW((StgClosure *)frame);
4123 // Now we have a stack with gaps in it, and we have to walk down
4124 // shoving the stack up to fill in the gaps. A diagram might
4128 // | ********* | <- sp
4132 // | stack_gap | <- gap | chunk_size
4134 // | ......... | <- gap_end v
4140 // 'sp' points the the current top-of-stack
4141 // 'gap' points to the stack_gap structure inside the gap
4142 // ***** indicates real stack data
4143 // ..... indicates gap
4144 // <empty> indicates unused
4148 void *gap_start, *next_gap_start, *gap_end;
4151 next_gap_start = (void *)((unsigned char*)gap + sizeof(StgUpdateFrame));
4152 sp = next_gap_start;
4154 while ((StgPtr)gap > tso->sp) {
4156 // we're working in *bytes* now...
4157 gap_start = next_gap_start;
4158 gap_end = (void*) ((unsigned char*)gap_start - gap->gap_size * sizeof(W_));
4160 gap = gap->next_gap;
4161 next_gap_start = (void *)((unsigned char*)gap + sizeof(StgUpdateFrame));
4163 chunk_size = (unsigned char*)gap_end - (unsigned char*)next_gap_start;
4165 memmove(sp, next_gap_start, chunk_size);
4168 tso->sp = (StgPtr)sp;
4172 /* -----------------------------------------------------------------------------
4175 * We have to prepare for GC - this means doing lazy black holing
4176 * here. We also take the opportunity to do stack squeezing if it's
4178 * -------------------------------------------------------------------------- */
4180 threadPaused(StgTSO *tso)
4182 if ( RtsFlags.GcFlags.squeezeUpdFrames == rtsTrue )
4183 threadSqueezeStack(tso); // does black holing too
4185 threadLazyBlackHole(tso);
4188 /* -----------------------------------------------------------------------------
4190 * -------------------------------------------------------------------------- */
4194 printMutableList(generation *gen)
4199 debugBelch("@@ Mutable list %p: ", gen->mut_list);
4201 for (bd = gen->mut_list; bd != NULL; bd = bd->link) {
4202 for (p = bd->start; p < bd->free; p++) {
4203 debugBelch("%p (%s), ", (void *)*p, info_type((StgClosure *)*p));
4209 STATIC_INLINE rtsBool
4210 maybeLarge(StgClosure *closure)
4212 StgInfoTable *info = get_itbl(closure);
4214 /* closure types that may be found on the new_large_objects list;
4215 see scavenge_large */
4216 return (info->type == MUT_ARR_PTRS ||
4217 info->type == MUT_ARR_PTRS_FROZEN ||
4218 info->type == MUT_ARR_PTRS_FROZEN0 ||
4219 info->type == TSO ||
4220 info->type == ARR_WORDS);