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 // Turn off inlining when debugging - it obfuscates things
53 # define STATIC_INLINE static
56 /* STATIC OBJECT LIST.
59 * We maintain a linked list of static objects that are still live.
60 * The requirements for this list are:
62 * - we need to scan the list while adding to it, in order to
63 * scavenge all the static objects (in the same way that
64 * breadth-first scavenging works for dynamic objects).
66 * - we need to be able to tell whether an object is already on
67 * the list, to break loops.
69 * Each static object has a "static link field", which we use for
70 * linking objects on to the list. We use a stack-type list, consing
71 * objects on the front as they are added (this means that the
72 * scavenge phase is depth-first, not breadth-first, but that
75 * A separate list is kept for objects that have been scavenged
76 * already - this is so that we can zero all the marks afterwards.
78 * An object is on the list if its static link field is non-zero; this
79 * means that we have to mark the end of the list with '1', not NULL.
81 * Extra notes for generational GC:
83 * Each generation has a static object list associated with it. When
84 * collecting generations up to N, we treat the static object lists
85 * from generations > N as roots.
87 * We build up a static object list while collecting generations 0..N,
88 * which is then appended to the static object list of generation N+1.
90 static StgClosure* static_objects; // live static objects
91 StgClosure* scavenged_static_objects; // static objects scavenged so far
93 /* N is the oldest generation being collected, where the generations
94 * are numbered starting at 0. A major GC (indicated by the major_gc
95 * flag) is when we're collecting all generations. We only attempt to
96 * deal with static objects and GC CAFs when doing a major GC.
99 static rtsBool major_gc;
101 /* Youngest generation that objects should be evacuated to in
102 * evacuate(). (Logically an argument to evacuate, but it's static
103 * a lot of the time so we optimise it into a global variable).
109 StgWeak *old_weak_ptr_list; // also pending finaliser list
111 /* Which stage of processing various kinds of weak pointer are we at?
112 * (see traverse_weak_ptr_list() below for discussion).
114 typedef enum { WeakPtrs, WeakThreads, WeakDone } WeakStage;
115 static WeakStage weak_stage;
117 /* List of all threads during GC
119 static StgTSO *old_all_threads;
120 StgTSO *resurrected_threads;
122 /* Flag indicating failure to evacuate an object to the desired
125 static rtsBool failed_to_evac;
127 /* Saved nursery (used for 2-space collector only)
129 static bdescr *saved_nursery;
130 static nat saved_n_blocks;
132 /* Data used for allocation area sizing.
134 static lnat new_blocks; // blocks allocated during this GC
135 static lnat new_scavd_blocks; // ditto, but depth-first blocks
136 static lnat g0s0_pcnt_kept = 30; // percentage of g0s0 live at last minor GC
138 /* Used to avoid long recursion due to selector thunks
140 static lnat thunk_selector_depth = 0;
141 #define MAX_THUNK_SELECTOR_DEPTH 8
143 /* -----------------------------------------------------------------------------
144 Static function declarations
145 -------------------------------------------------------------------------- */
147 static bdescr * gc_alloc_block ( step *stp );
148 static void mark_root ( StgClosure **root );
150 // Use a register argument for evacuate, if available.
152 #define REGPARM1 __attribute__((regparm(1)))
157 REGPARM1 static StgClosure * evacuate (StgClosure *q);
159 static void zero_static_object_list ( StgClosure* first_static );
161 static rtsBool traverse_weak_ptr_list ( void );
162 static void mark_weak_ptr_list ( StgWeak **list );
164 static StgClosure * eval_thunk_selector ( nat field, StgSelector * p );
167 static void scavenge ( step * );
168 static void scavenge_mark_stack ( void );
169 static void scavenge_stack ( StgPtr p, StgPtr stack_end );
170 static rtsBool scavenge_one ( StgPtr p );
171 static void scavenge_large ( step * );
172 static void scavenge_static ( void );
173 static void scavenge_mutable_list ( generation *g );
175 static void scavenge_large_bitmap ( StgPtr p,
176 StgLargeBitmap *large_bitmap,
179 #if 0 && defined(DEBUG)
180 static void gcCAFs ( void );
183 /* -----------------------------------------------------------------------------
184 inline functions etc. for dealing with the mark bitmap & stack.
185 -------------------------------------------------------------------------- */
187 #define MARK_STACK_BLOCKS 4
189 static bdescr *mark_stack_bdescr;
190 static StgPtr *mark_stack;
191 static StgPtr *mark_sp;
192 static StgPtr *mark_splim;
194 // Flag and pointers used for falling back to a linear scan when the
195 // mark stack overflows.
196 static rtsBool mark_stack_overflowed;
197 static bdescr *oldgen_scan_bd;
198 static StgPtr oldgen_scan;
200 STATIC_INLINE rtsBool
201 mark_stack_empty(void)
203 return mark_sp == mark_stack;
206 STATIC_INLINE rtsBool
207 mark_stack_full(void)
209 return mark_sp >= mark_splim;
213 reset_mark_stack(void)
215 mark_sp = mark_stack;
219 push_mark_stack(StgPtr p)
230 /* -----------------------------------------------------------------------------
231 Allocate a new to-space block in the given step.
232 -------------------------------------------------------------------------- */
235 gc_alloc_block(step *stp)
237 bdescr *bd = allocBlock();
238 bd->gen_no = stp->gen_no;
242 // blocks in to-space in generations up to and including N
243 // get the BF_EVACUATED flag.
244 if (stp->gen_no <= N) {
245 bd->flags = BF_EVACUATED;
250 // Start a new to-space block, chain it on after the previous one.
251 if (stp->hp_bd != NULL) {
252 stp->hp_bd->free = stp->hp;
253 stp->hp_bd->link = bd;
258 stp->hpLim = stp->hp + BLOCK_SIZE_W;
267 gc_alloc_scavd_block(step *stp)
269 bdescr *bd = allocBlock();
270 bd->gen_no = stp->gen_no;
273 // blocks in to-space in generations up to and including N
274 // get the BF_EVACUATED flag.
275 if (stp->gen_no <= N) {
276 bd->flags = BF_EVACUATED;
281 bd->link = stp->blocks;
284 if (stp->scavd_hp != NULL) {
285 Bdescr(stp->scavd_hp)->free = stp->scavd_hp;
287 stp->scavd_hp = bd->start;
288 stp->scavd_hpLim = stp->scavd_hp + BLOCK_SIZE_W;
296 /* -----------------------------------------------------------------------------
299 Rough outline of the algorithm: for garbage collecting generation N
300 (and all younger generations):
302 - follow all pointers in the root set. the root set includes all
303 mutable objects in all generations (mutable_list).
305 - for each pointer, evacuate the object it points to into either
307 + to-space of the step given by step->to, which is the next
308 highest step in this generation or the first step in the next
309 generation if this is the last step.
311 + to-space of generations[evac_gen]->steps[0], if evac_gen != 0.
312 When we evacuate an object we attempt to evacuate
313 everything it points to into the same generation - this is
314 achieved by setting evac_gen to the desired generation. If
315 we can't do this, then an entry in the mut list has to
316 be made for the cross-generation pointer.
318 + if the object is already in a generation > N, then leave
321 - repeatedly scavenge to-space from each step in each generation
322 being collected until no more objects can be evacuated.
324 - free from-space in each step, and set from-space = to-space.
326 Locks held: sched_mutex
328 -------------------------------------------------------------------------- */
331 GarbageCollect ( void (*get_roots)(evac_fn), rtsBool force_major_gc )
335 lnat live, allocated, collected = 0, copied = 0, scavd_copied = 0;
336 lnat oldgen_saved_blocks = 0;
340 CostCentreStack *prev_CCS;
343 #if defined(DEBUG) && defined(GRAN)
344 IF_DEBUG(gc, debugBelch("@@ Starting garbage collection at %ld (%lx)\n",
348 #if defined(RTS_USER_SIGNALS)
353 // tell the STM to discard any cached closures its hoping to re-use
356 // tell the stats department that we've started a GC
360 // check for memory leaks if DEBUG is on
364 // Init stats and print par specific (timing) info
365 PAR_TICKY_PAR_START();
367 // attribute any costs to CCS_GC
373 /* Approximate how much we allocated.
374 * Todo: only when generating stats?
376 allocated = calcAllocated();
378 /* Figure out which generation to collect
380 if (force_major_gc) {
381 N = RtsFlags.GcFlags.generations - 1;
385 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
386 if (generations[g].steps[0].n_blocks +
387 generations[g].steps[0].n_large_blocks
388 >= generations[g].max_blocks) {
392 major_gc = (N == RtsFlags.GcFlags.generations-1);
395 #ifdef RTS_GTK_FRONTPANEL
396 if (RtsFlags.GcFlags.frontpanel) {
397 updateFrontPanelBeforeGC(N);
401 // check stack sanity *before* GC (ToDo: check all threads)
403 // ToDo!: check sanity IF_DEBUG(sanity, checkTSOsSanity());
405 IF_DEBUG(sanity, checkFreeListSanity());
407 /* Initialise the static object lists
409 static_objects = END_OF_STATIC_LIST;
410 scavenged_static_objects = END_OF_STATIC_LIST;
412 /* Save the nursery if we're doing a two-space collection.
413 * g0s0->blocks will be used for to-space, so we need to get the
414 * nursery out of the way.
416 if (RtsFlags.GcFlags.generations == 1) {
417 saved_nursery = g0s0->blocks;
418 saved_n_blocks = g0s0->n_blocks;
423 /* Keep a count of how many new blocks we allocated during this GC
424 * (used for resizing the allocation area, later).
427 new_scavd_blocks = 0;
429 // Initialise to-space in all the generations/steps that we're
432 for (g = 0; g <= N; g++) {
434 // throw away the mutable list. Invariant: the mutable list
435 // always has at least one block; this means we can avoid a check for
436 // NULL in recordMutable().
438 freeChain(generations[g].mut_list);
439 generations[g].mut_list = allocBlock();
442 for (s = 0; s < generations[g].n_steps; s++) {
444 // generation 0, step 0 doesn't need to-space
445 if (g == 0 && s == 0 && RtsFlags.GcFlags.generations > 1) {
449 stp = &generations[g].steps[s];
450 ASSERT(stp->gen_no == g);
452 // start a new to-space for this step.
453 stp->old_blocks = stp->blocks;
454 stp->n_old_blocks = stp->n_blocks;
456 // allocate the first to-space block; extra blocks will be
457 // chained on as necessary.
459 bd = gc_alloc_block(stp);
462 stp->scan = bd->start;
465 // allocate a block for "already scavenged" objects. This goes
466 // on the front of the stp->blocks list, so it won't be
467 // traversed by the scavenging sweep.
468 gc_alloc_scavd_block(stp);
470 // initialise the large object queues.
471 stp->new_large_objects = NULL;
472 stp->scavenged_large_objects = NULL;
473 stp->n_scavenged_large_blocks = 0;
475 // mark the large objects as not evacuated yet
476 for (bd = stp->large_objects; bd; bd = bd->link) {
477 bd->flags &= ~BF_EVACUATED;
480 // for a compacted step, we need to allocate the bitmap
481 if (stp->is_compacted) {
482 nat bitmap_size; // in bytes
483 bdescr *bitmap_bdescr;
486 bitmap_size = stp->n_old_blocks * BLOCK_SIZE / (sizeof(W_)*BITS_PER_BYTE);
488 if (bitmap_size > 0) {
489 bitmap_bdescr = allocGroup((lnat)BLOCK_ROUND_UP(bitmap_size)
491 stp->bitmap = bitmap_bdescr;
492 bitmap = bitmap_bdescr->start;
494 IF_DEBUG(gc, debugBelch("bitmap_size: %d, bitmap: %p",
495 bitmap_size, bitmap););
497 // don't forget to fill it with zeros!
498 memset(bitmap, 0, bitmap_size);
500 // For each block in this step, point to its bitmap from the
502 for (bd=stp->old_blocks; bd != NULL; bd = bd->link) {
503 bd->u.bitmap = bitmap;
504 bitmap += BLOCK_SIZE_W / (sizeof(W_)*BITS_PER_BYTE);
506 // Also at this point we set the BF_COMPACTED flag
507 // for this block. The invariant is that
508 // BF_COMPACTED is always unset, except during GC
509 // when it is set on those blocks which will be
511 bd->flags |= BF_COMPACTED;
518 /* make sure the older generations have at least one block to
519 * allocate into (this makes things easier for copy(), see below).
521 for (g = N+1; g < RtsFlags.GcFlags.generations; g++) {
522 for (s = 0; s < generations[g].n_steps; s++) {
523 stp = &generations[g].steps[s];
524 if (stp->hp_bd == NULL) {
525 ASSERT(stp->blocks == NULL);
526 bd = gc_alloc_block(stp);
530 if (stp->scavd_hp == NULL) {
531 gc_alloc_scavd_block(stp);
534 /* Set the scan pointer for older generations: remember we
535 * still have to scavenge objects that have been promoted. */
537 stp->scan_bd = stp->hp_bd;
538 stp->new_large_objects = NULL;
539 stp->scavenged_large_objects = NULL;
540 stp->n_scavenged_large_blocks = 0;
544 /* Allocate a mark stack if we're doing a major collection.
547 mark_stack_bdescr = allocGroup(MARK_STACK_BLOCKS);
548 mark_stack = (StgPtr *)mark_stack_bdescr->start;
549 mark_sp = mark_stack;
550 mark_splim = mark_stack + (MARK_STACK_BLOCKS * BLOCK_SIZE_W);
552 mark_stack_bdescr = NULL;
555 /* -----------------------------------------------------------------------
556 * follow all the roots that we know about:
557 * - mutable lists from each generation > N
558 * we want to *scavenge* these roots, not evacuate them: they're not
559 * going to move in this GC.
560 * Also: do them in reverse generation order. This is because we
561 * often want to promote objects that are pointed to by older
562 * generations early, so we don't have to repeatedly copy them.
563 * Doing the generations in reverse order ensures that we don't end
564 * up in the situation where we want to evac an object to gen 3 and
565 * it has already been evaced to gen 2.
569 for (g = RtsFlags.GcFlags.generations-1; g > N; g--) {
570 generations[g].saved_mut_list = generations[g].mut_list;
571 generations[g].mut_list = allocBlock();
572 // mut_list always has at least one block.
575 for (g = RtsFlags.GcFlags.generations-1; g > N; g--) {
576 IF_PAR_DEBUG(verbose, printMutableList(&generations[g]));
577 scavenge_mutable_list(&generations[g]);
579 for (st = generations[g].n_steps-1; st >= 0; st--) {
580 scavenge(&generations[g].steps[st]);
585 /* follow roots from the CAF list (used by GHCi)
590 /* follow all the roots that the application knows about.
593 get_roots(mark_root);
596 /* And don't forget to mark the TSO if we got here direct from
598 /* Not needed in a seq version?
600 CurrentTSO = (StgTSO *)MarkRoot((StgClosure *)CurrentTSO);
604 // Mark the entries in the GALA table of the parallel system
605 markLocalGAs(major_gc);
606 // Mark all entries on the list of pending fetches
607 markPendingFetches(major_gc);
610 /* Mark the weak pointer list, and prepare to detect dead weak
613 mark_weak_ptr_list(&weak_ptr_list);
614 old_weak_ptr_list = weak_ptr_list;
615 weak_ptr_list = NULL;
616 weak_stage = WeakPtrs;
618 /* The all_threads list is like the weak_ptr_list.
619 * See traverse_weak_ptr_list() for the details.
621 old_all_threads = all_threads;
622 all_threads = END_TSO_QUEUE;
623 resurrected_threads = END_TSO_QUEUE;
625 /* Mark the stable pointer table.
627 markStablePtrTable(mark_root);
629 /* -------------------------------------------------------------------------
630 * Repeatedly scavenge all the areas we know about until there's no
631 * more scavenging to be done.
638 // scavenge static objects
639 if (major_gc && static_objects != END_OF_STATIC_LIST) {
640 IF_DEBUG(sanity, checkStaticObjects(static_objects));
644 /* When scavenging the older generations: Objects may have been
645 * evacuated from generations <= N into older generations, and we
646 * need to scavenge these objects. We're going to try to ensure that
647 * any evacuations that occur move the objects into at least the
648 * same generation as the object being scavenged, otherwise we
649 * have to create new entries on the mutable list for the older
653 // scavenge each step in generations 0..maxgen
659 // scavenge objects in compacted generation
660 if (mark_stack_overflowed || oldgen_scan_bd != NULL ||
661 (mark_stack_bdescr != NULL && !mark_stack_empty())) {
662 scavenge_mark_stack();
666 for (gen = RtsFlags.GcFlags.generations; --gen >= 0; ) {
667 for (st = generations[gen].n_steps; --st >= 0; ) {
668 if (gen == 0 && st == 0 && RtsFlags.GcFlags.generations > 1) {
671 stp = &generations[gen].steps[st];
673 if (stp->hp_bd != stp->scan_bd || stp->scan < stp->hp) {
678 if (stp->new_large_objects != NULL) {
687 if (flag) { goto loop; }
689 // must be last... invariant is that everything is fully
690 // scavenged at this point.
691 if (traverse_weak_ptr_list()) { // returns rtsTrue if evaced something
696 /* Update the pointers from the "main thread" list - these are
697 * treated as weak pointers because we want to allow a main thread
698 * to get a BlockedOnDeadMVar exception in the same way as any other
699 * thread. Note that the threads should all have been retained by
700 * GC by virtue of being on the all_threads list, we're just
701 * updating pointers here.
706 for (m = main_threads; m != NULL; m = m->link) {
707 tso = (StgTSO *) isAlive((StgClosure *)m->tso);
709 barf("main thread has been GC'd");
716 // Reconstruct the Global Address tables used in GUM
717 rebuildGAtables(major_gc);
718 IF_DEBUG(sanity, checkLAGAtable(rtsTrue/*check closures, too*/));
721 // Now see which stable names are still alive.
724 // Tidy the end of the to-space chains
725 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
726 for (s = 0; s < generations[g].n_steps; s++) {
727 stp = &generations[g].steps[s];
728 if (!(g == 0 && s == 0 && RtsFlags.GcFlags.generations > 1)) {
729 ASSERT(Bdescr(stp->hp) == stp->hp_bd);
730 stp->hp_bd->free = stp->hp;
731 Bdescr(stp->scavd_hp)->free = stp->scavd_hp;
737 // We call processHeapClosureForDead() on every closure destroyed during
738 // the current garbage collection, so we invoke LdvCensusForDead().
739 if (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_LDV
740 || RtsFlags.ProfFlags.bioSelector != NULL)
744 // NO MORE EVACUATION AFTER THIS POINT!
745 // Finally: compaction of the oldest generation.
746 if (major_gc && oldest_gen->steps[0].is_compacted) {
747 // save number of blocks for stats
748 oldgen_saved_blocks = oldest_gen->steps[0].n_old_blocks;
752 IF_DEBUG(sanity, checkGlobalTSOList(rtsFalse));
754 /* run through all the generations/steps and tidy up
756 copied = new_blocks * BLOCK_SIZE_W;
757 scavd_copied = new_scavd_blocks * BLOCK_SIZE_W;
758 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
761 generations[g].collections++; // for stats
764 // Count the mutable list as bytes "copied" for the purposes of
765 // stats. Every mutable list is copied during every GC.
767 for (bd = generations[g].mut_list; bd != NULL; bd = bd->link) {
768 copied += (bd->free - bd->start) * sizeof(StgWord);
772 for (s = 0; s < generations[g].n_steps; s++) {
774 stp = &generations[g].steps[s];
776 if (!(g == 0 && s == 0 && RtsFlags.GcFlags.generations > 1)) {
777 // stats information: how much we copied
779 copied -= stp->hp_bd->start + BLOCK_SIZE_W -
781 scavd_copied -= (P_)(BLOCK_ROUND_UP(stp->scavd_hp)) - stp->scavd_hp;
785 // for generations we collected...
788 // rough calculation of garbage collected, for stats output
789 if (stp->is_compacted) {
790 collected += (oldgen_saved_blocks - stp->n_old_blocks) * BLOCK_SIZE_W;
792 if (g == 0 && s == 0) {
793 collected += countNurseryBlocks() * BLOCK_SIZE_W;
794 collected += alloc_blocks;
796 collected += stp->n_old_blocks * BLOCK_SIZE_W;
800 /* free old memory and shift to-space into from-space for all
801 * the collected steps (except the allocation area). These
802 * freed blocks will probaby be quickly recycled.
804 if (!(g == 0 && s == 0)) {
805 if (stp->is_compacted) {
806 // for a compacted step, just shift the new to-space
807 // onto the front of the now-compacted existing blocks.
808 for (bd = stp->blocks; bd != NULL; bd = bd->link) {
809 bd->flags &= ~BF_EVACUATED; // now from-space
811 // tack the new blocks on the end of the existing blocks
812 if (stp->old_blocks != NULL) {
813 for (bd = stp->old_blocks; bd != NULL; bd = next) {
814 // NB. this step might not be compacted next
815 // time, so reset the BF_COMPACTED flags.
816 // They are set before GC if we're going to
817 // compact. (search for BF_COMPACTED above).
818 bd->flags &= ~BF_COMPACTED;
821 bd->link = stp->blocks;
824 stp->blocks = stp->old_blocks;
826 // add the new blocks to the block tally
827 stp->n_blocks += stp->n_old_blocks;
828 ASSERT(countBlocks(stp->blocks) == stp->n_blocks);
830 freeChain(stp->old_blocks);
831 for (bd = stp->blocks; bd != NULL; bd = bd->link) {
832 bd->flags &= ~BF_EVACUATED; // now from-space
835 stp->old_blocks = NULL;
836 stp->n_old_blocks = 0;
839 /* LARGE OBJECTS. The current live large objects are chained on
840 * scavenged_large, having been moved during garbage
841 * collection from large_objects. Any objects left on
842 * large_objects list are therefore dead, so we free them here.
844 for (bd = stp->large_objects; bd != NULL; bd = next) {
850 // update the count of blocks used by large objects
851 for (bd = stp->scavenged_large_objects; bd != NULL; bd = bd->link) {
852 bd->flags &= ~BF_EVACUATED;
854 stp->large_objects = stp->scavenged_large_objects;
855 stp->n_large_blocks = stp->n_scavenged_large_blocks;
858 // for older generations...
860 /* For older generations, we need to append the
861 * scavenged_large_object list (i.e. large objects that have been
862 * promoted during this GC) to the large_object list for that step.
864 for (bd = stp->scavenged_large_objects; bd; bd = next) {
866 bd->flags &= ~BF_EVACUATED;
867 dbl_link_onto(bd, &stp->large_objects);
870 // add the new blocks we promoted during this GC
871 stp->n_large_blocks += stp->n_scavenged_large_blocks;
876 /* Reset the sizes of the older generations when we do a major
879 * CURRENT STRATEGY: make all generations except zero the same size.
880 * We have to stay within the maximum heap size, and leave a certain
881 * percentage of the maximum heap size available to allocate into.
883 if (major_gc && RtsFlags.GcFlags.generations > 1) {
884 nat live, size, min_alloc;
885 nat max = RtsFlags.GcFlags.maxHeapSize;
886 nat gens = RtsFlags.GcFlags.generations;
888 // live in the oldest generations
889 live = oldest_gen->steps[0].n_blocks +
890 oldest_gen->steps[0].n_large_blocks;
892 // default max size for all generations except zero
893 size = stg_max(live * RtsFlags.GcFlags.oldGenFactor,
894 RtsFlags.GcFlags.minOldGenSize);
896 // minimum size for generation zero
897 min_alloc = stg_max((RtsFlags.GcFlags.pcFreeHeap * max) / 200,
898 RtsFlags.GcFlags.minAllocAreaSize);
900 // Auto-enable compaction when the residency reaches a
901 // certain percentage of the maximum heap size (default: 30%).
902 if (RtsFlags.GcFlags.generations > 1 &&
903 (RtsFlags.GcFlags.compact ||
905 oldest_gen->steps[0].n_blocks >
906 (RtsFlags.GcFlags.compactThreshold * max) / 100))) {
907 oldest_gen->steps[0].is_compacted = 1;
908 // debugBelch("compaction: on\n", live);
910 oldest_gen->steps[0].is_compacted = 0;
911 // debugBelch("compaction: off\n", live);
914 // if we're going to go over the maximum heap size, reduce the
915 // size of the generations accordingly. The calculation is
916 // different if compaction is turned on, because we don't need
917 // to double the space required to collect the old generation.
920 // this test is necessary to ensure that the calculations
921 // below don't have any negative results - we're working
922 // with unsigned values here.
923 if (max < min_alloc) {
927 if (oldest_gen->steps[0].is_compacted) {
928 if ( (size + (size - 1) * (gens - 2) * 2) + min_alloc > max ) {
929 size = (max - min_alloc) / ((gens - 1) * 2 - 1);
932 if ( (size * (gens - 1) * 2) + min_alloc > max ) {
933 size = (max - min_alloc) / ((gens - 1) * 2);
943 debugBelch("live: %d, min_alloc: %d, size : %d, max = %d\n", live,
944 min_alloc, size, max);
947 for (g = 0; g < gens; g++) {
948 generations[g].max_blocks = size;
952 // Guess the amount of live data for stats.
955 /* Free the small objects allocated via allocate(), since this will
956 * all have been copied into G0S1 now.
958 if (small_alloc_list != NULL) {
959 freeChain(small_alloc_list);
961 small_alloc_list = NULL;
965 alloc_blocks_lim = RtsFlags.GcFlags.minAllocAreaSize;
967 // Start a new pinned_object_block
968 pinned_object_block = NULL;
970 /* Free the mark stack.
972 if (mark_stack_bdescr != NULL) {
973 freeGroup(mark_stack_bdescr);
978 for (g = 0; g <= N; g++) {
979 for (s = 0; s < generations[g].n_steps; s++) {
980 stp = &generations[g].steps[s];
981 if (stp->bitmap != NULL) {
982 freeGroup(stp->bitmap);
988 /* Two-space collector:
989 * Free the old to-space, and estimate the amount of live data.
991 if (RtsFlags.GcFlags.generations == 1) {
994 if (g0s0->old_blocks != NULL) {
995 freeChain(g0s0->old_blocks);
997 for (bd = g0s0->blocks; bd != NULL; bd = bd->link) {
998 bd->flags = 0; // now from-space
1000 g0s0->old_blocks = g0s0->blocks;
1001 g0s0->n_old_blocks = g0s0->n_blocks;
1002 g0s0->blocks = saved_nursery;
1003 g0s0->n_blocks = saved_n_blocks;
1005 /* For a two-space collector, we need to resize the nursery. */
1007 /* set up a new nursery. Allocate a nursery size based on a
1008 * function of the amount of live data (by default a factor of 2)
1009 * Use the blocks from the old nursery if possible, freeing up any
1012 * If we get near the maximum heap size, then adjust our nursery
1013 * size accordingly. If the nursery is the same size as the live
1014 * data (L), then we need 3L bytes. We can reduce the size of the
1015 * nursery to bring the required memory down near 2L bytes.
1017 * A normal 2-space collector would need 4L bytes to give the same
1018 * performance we get from 3L bytes, reducing to the same
1019 * performance at 2L bytes.
1021 blocks = g0s0->n_old_blocks;
1023 if ( RtsFlags.GcFlags.maxHeapSize != 0 &&
1024 blocks * RtsFlags.GcFlags.oldGenFactor * 2 >
1025 RtsFlags.GcFlags.maxHeapSize ) {
1026 long adjusted_blocks; // signed on purpose
1029 adjusted_blocks = (RtsFlags.GcFlags.maxHeapSize - 2 * blocks);
1030 IF_DEBUG(gc, debugBelch("@@ Near maximum heap size of 0x%x blocks, blocks = %d, adjusted to %ld", RtsFlags.GcFlags.maxHeapSize, blocks, adjusted_blocks));
1031 pc_free = adjusted_blocks * 100 / RtsFlags.GcFlags.maxHeapSize;
1032 if (pc_free < RtsFlags.GcFlags.pcFreeHeap) /* might even be < 0 */ {
1035 blocks = adjusted_blocks;
1038 blocks *= RtsFlags.GcFlags.oldGenFactor;
1039 if (blocks < RtsFlags.GcFlags.minAllocAreaSize) {
1040 blocks = RtsFlags.GcFlags.minAllocAreaSize;
1043 resizeNurseries(blocks);
1046 /* Generational collector:
1047 * If the user has given us a suggested heap size, adjust our
1048 * allocation area to make best use of the memory available.
1051 if (RtsFlags.GcFlags.heapSizeSuggestion) {
1053 nat needed = calcNeeded(); // approx blocks needed at next GC
1055 /* Guess how much will be live in generation 0 step 0 next time.
1056 * A good approximation is obtained by finding the
1057 * percentage of g0s0 that was live at the last minor GC.
1060 g0s0_pcnt_kept = (new_blocks * 100) / countNurseryBlocks();
1063 /* Estimate a size for the allocation area based on the
1064 * information available. We might end up going slightly under
1065 * or over the suggested heap size, but we should be pretty
1068 * Formula: suggested - needed
1069 * ----------------------------
1070 * 1 + g0s0_pcnt_kept/100
1072 * where 'needed' is the amount of memory needed at the next
1073 * collection for collecting all steps except g0s0.
1076 (((long)RtsFlags.GcFlags.heapSizeSuggestion - (long)needed) * 100) /
1077 (100 + (long)g0s0_pcnt_kept);
1079 if (blocks < (long)RtsFlags.GcFlags.minAllocAreaSize) {
1080 blocks = RtsFlags.GcFlags.minAllocAreaSize;
1083 resizeNurseries((nat)blocks);
1086 // we might have added extra large blocks to the nursery, so
1087 // resize back to minAllocAreaSize again.
1088 resizeNurseriesFixed(RtsFlags.GcFlags.minAllocAreaSize);
1092 // mark the garbage collected CAFs as dead
1093 #if 0 && defined(DEBUG) // doesn't work at the moment
1094 if (major_gc) { gcCAFs(); }
1098 // resetStaticObjectForRetainerProfiling() must be called before
1100 resetStaticObjectForRetainerProfiling();
1103 // zero the scavenged static object list
1105 zero_static_object_list(scavenged_static_objects);
1108 // Reset the nursery
1111 RELEASE_LOCK(&sched_mutex);
1113 // start any pending finalizers
1114 scheduleFinalizers(old_weak_ptr_list);
1116 // send exceptions to any threads which were about to die
1117 resurrectThreads(resurrected_threads);
1119 ACQUIRE_LOCK(&sched_mutex);
1121 // Update the stable pointer hash table.
1122 updateStablePtrTable(major_gc);
1124 // check sanity after GC
1125 IF_DEBUG(sanity, checkSanity());
1127 // extra GC trace info
1128 IF_DEBUG(gc, statDescribeGens());
1131 // symbol-table based profiling
1132 /* heapCensus(to_blocks); */ /* ToDo */
1135 // restore enclosing cost centre
1141 // check for memory leaks if DEBUG is on
1145 #ifdef RTS_GTK_FRONTPANEL
1146 if (RtsFlags.GcFlags.frontpanel) {
1147 updateFrontPanelAfterGC( N, live );
1151 // ok, GC over: tell the stats department what happened.
1152 stat_endGC(allocated, collected, live, copied, scavd_copied, N);
1154 #if defined(RTS_USER_SIGNALS)
1155 // unblock signals again
1156 unblockUserSignals();
1163 /* -----------------------------------------------------------------------------
1166 traverse_weak_ptr_list is called possibly many times during garbage
1167 collection. It returns a flag indicating whether it did any work
1168 (i.e. called evacuate on any live pointers).
1170 Invariant: traverse_weak_ptr_list is called when the heap is in an
1171 idempotent state. That means that there are no pending
1172 evacuate/scavenge operations. This invariant helps the weak
1173 pointer code decide which weak pointers are dead - if there are no
1174 new live weak pointers, then all the currently unreachable ones are
1177 For generational GC: we just don't try to finalize weak pointers in
1178 older generations than the one we're collecting. This could
1179 probably be optimised by keeping per-generation lists of weak
1180 pointers, but for a few weak pointers this scheme will work.
1182 There are three distinct stages to processing weak pointers:
1184 - weak_stage == WeakPtrs
1186 We process all the weak pointers whos keys are alive (evacuate
1187 their values and finalizers), and repeat until we can find no new
1188 live keys. If no live keys are found in this pass, then we
1189 evacuate the finalizers of all the dead weak pointers in order to
1192 - weak_stage == WeakThreads
1194 Now, we discover which *threads* are still alive. Pointers to
1195 threads from the all_threads and main thread lists are the
1196 weakest of all: a pointers from the finalizer of a dead weak
1197 pointer can keep a thread alive. Any threads found to be unreachable
1198 are evacuated and placed on the resurrected_threads list so we
1199 can send them a signal later.
1201 - weak_stage == WeakDone
1203 No more evacuation is done.
1205 -------------------------------------------------------------------------- */
1208 traverse_weak_ptr_list(void)
1210 StgWeak *w, **last_w, *next_w;
1212 rtsBool flag = rtsFalse;
1214 switch (weak_stage) {
1220 /* doesn't matter where we evacuate values/finalizers to, since
1221 * these pointers are treated as roots (iff the keys are alive).
1225 last_w = &old_weak_ptr_list;
1226 for (w = old_weak_ptr_list; w != NULL; w = next_w) {
1228 /* There might be a DEAD_WEAK on the list if finalizeWeak# was
1229 * called on a live weak pointer object. Just remove it.
1231 if (w->header.info == &stg_DEAD_WEAK_info) {
1232 next_w = ((StgDeadWeak *)w)->link;
1237 switch (get_itbl(w)->type) {
1240 next_w = (StgWeak *)((StgEvacuated *)w)->evacuee;
1245 /* Now, check whether the key is reachable.
1247 new = isAlive(w->key);
1250 // evacuate the value and finalizer
1251 w->value = evacuate(w->value);
1252 w->finalizer = evacuate(w->finalizer);
1253 // remove this weak ptr from the old_weak_ptr list
1255 // and put it on the new weak ptr list
1257 w->link = weak_ptr_list;
1260 IF_DEBUG(weak, debugBelch("Weak pointer still alive at %p -> %p",
1265 last_w = &(w->link);
1271 barf("traverse_weak_ptr_list: not WEAK");
1275 /* If we didn't make any changes, then we can go round and kill all
1276 * the dead weak pointers. The old_weak_ptr list is used as a list
1277 * of pending finalizers later on.
1279 if (flag == rtsFalse) {
1280 for (w = old_weak_ptr_list; w; w = w->link) {
1281 w->finalizer = evacuate(w->finalizer);
1284 // Next, move to the WeakThreads stage after fully
1285 // scavenging the finalizers we've just evacuated.
1286 weak_stage = WeakThreads;
1292 /* Now deal with the all_threads list, which behaves somewhat like
1293 * the weak ptr list. If we discover any threads that are about to
1294 * become garbage, we wake them up and administer an exception.
1297 StgTSO *t, *tmp, *next, **prev;
1299 prev = &old_all_threads;
1300 for (t = old_all_threads; t != END_TSO_QUEUE; t = next) {
1302 tmp = (StgTSO *)isAlive((StgClosure *)t);
1308 ASSERT(get_itbl(t)->type == TSO);
1309 switch (t->what_next) {
1310 case ThreadRelocated:
1315 case ThreadComplete:
1316 // finshed or died. The thread might still be alive, but we
1317 // don't keep it on the all_threads list. Don't forget to
1318 // stub out its global_link field.
1319 next = t->global_link;
1320 t->global_link = END_TSO_QUEUE;
1327 // Threads blocked on black holes: if the black hole
1328 // is alive, then the thread is alive too.
1329 if (tmp == NULL && t->why_blocked == BlockedOnBlackHole) {
1330 if (isAlive(t->block_info.closure)) {
1331 t = (StgTSO *)evacuate((StgClosure *)t);
1338 // not alive (yet): leave this thread on the
1339 // old_all_threads list.
1340 prev = &(t->global_link);
1341 next = t->global_link;
1344 // alive: move this thread onto the all_threads list.
1345 next = t->global_link;
1346 t->global_link = all_threads;
1353 /* If we evacuated any threads, we need to go back to the scavenger.
1355 if (flag) return rtsTrue;
1357 /* And resurrect any threads which were about to become garbage.
1360 StgTSO *t, *tmp, *next;
1361 for (t = old_all_threads; t != END_TSO_QUEUE; t = next) {
1362 next = t->global_link;
1363 tmp = (StgTSO *)evacuate((StgClosure *)t);
1364 tmp->global_link = resurrected_threads;
1365 resurrected_threads = tmp;
1369 /* Finally, we can update the blackhole_queue. This queue
1370 * simply strings together TSOs blocked on black holes, it is
1371 * not intended to keep anything alive. Hence, we do not follow
1372 * pointers on the blackhole_queue until now, when we have
1373 * determined which TSOs are otherwise reachable. We know at
1374 * this point that all TSOs have been evacuated, however.
1378 for (pt = &blackhole_queue; *pt != END_TSO_QUEUE; pt = &((*pt)->link)) {
1379 *pt = (StgTSO *)isAlive((StgClosure *)*pt);
1380 ASSERT(*pt != NULL);
1384 weak_stage = WeakDone; // *now* we're done,
1385 return rtsTrue; // but one more round of scavenging, please
1388 barf("traverse_weak_ptr_list");
1394 /* -----------------------------------------------------------------------------
1395 After GC, the live weak pointer list may have forwarding pointers
1396 on it, because a weak pointer object was evacuated after being
1397 moved to the live weak pointer list. We remove those forwarding
1400 Also, we don't consider weak pointer objects to be reachable, but
1401 we must nevertheless consider them to be "live" and retain them.
1402 Therefore any weak pointer objects which haven't as yet been
1403 evacuated need to be evacuated now.
1404 -------------------------------------------------------------------------- */
1408 mark_weak_ptr_list ( StgWeak **list )
1410 StgWeak *w, **last_w;
1413 for (w = *list; w; w = w->link) {
1414 // w might be WEAK, EVACUATED, or DEAD_WEAK (actually CON_STATIC) here
1415 ASSERT(w->header.info == &stg_DEAD_WEAK_info
1416 || get_itbl(w)->type == WEAK || get_itbl(w)->type == EVACUATED);
1417 w = (StgWeak *)evacuate((StgClosure *)w);
1419 last_w = &(w->link);
1423 /* -----------------------------------------------------------------------------
1424 isAlive determines whether the given closure is still alive (after
1425 a garbage collection) or not. It returns the new address of the
1426 closure if it is alive, or NULL otherwise.
1428 NOTE: Use it before compaction only!
1429 -------------------------------------------------------------------------- */
1433 isAlive(StgClosure *p)
1435 const StgInfoTable *info;
1440 ASSERT(LOOKS_LIKE_CLOSURE_PTR(p));
1443 // ignore static closures
1445 // ToDo: for static closures, check the static link field.
1446 // Problem here is that we sometimes don't set the link field, eg.
1447 // for static closures with an empty SRT or CONSTR_STATIC_NOCAFs.
1449 if (!HEAP_ALLOCED(p)) {
1453 // ignore closures in generations that we're not collecting.
1455 if (bd->gen_no > N) {
1459 // if it's a pointer into to-space, then we're done
1460 if (bd->flags & BF_EVACUATED) {
1464 // large objects use the evacuated flag
1465 if (bd->flags & BF_LARGE) {
1469 // check the mark bit for compacted steps
1470 if ((bd->flags & BF_COMPACTED) && is_marked((P_)p,bd)) {
1474 switch (info->type) {
1479 case IND_OLDGEN: // rely on compatible layout with StgInd
1480 case IND_OLDGEN_PERM:
1481 // follow indirections
1482 p = ((StgInd *)p)->indirectee;
1487 return ((StgEvacuated *)p)->evacuee;
1490 if (((StgTSO *)p)->what_next == ThreadRelocated) {
1491 p = (StgClosure *)((StgTSO *)p)->link;
1504 mark_root(StgClosure **root)
1506 *root = evacuate(*root);
1510 upd_evacuee(StgClosure *p, StgClosure *dest)
1512 // not true: (ToDo: perhaps it should be)
1513 // ASSERT(Bdescr((P_)dest)->flags & BF_EVACUATED);
1514 SET_INFO(p, &stg_EVACUATED_info);
1515 ((StgEvacuated *)p)->evacuee = dest;
1519 STATIC_INLINE StgClosure *
1520 copy(StgClosure *src, nat size, step *stp)
1526 nat size_org = size;
1529 TICK_GC_WORDS_COPIED(size);
1530 /* Find out where we're going, using the handy "to" pointer in
1531 * the step of the source object. If it turns out we need to
1532 * evacuate to an older generation, adjust it here (see comment
1535 if (stp->gen_no < evac_gen) {
1536 #ifdef NO_EAGER_PROMOTION
1537 failed_to_evac = rtsTrue;
1539 stp = &generations[evac_gen].steps[0];
1543 /* chain a new block onto the to-space for the destination step if
1546 if (stp->hp + size >= stp->hpLim) {
1547 gc_alloc_block(stp);
1552 stp->hp = to + size;
1553 for (i = 0; i < size; i++) { // unroll for small i
1556 upd_evacuee((StgClosure *)from,(StgClosure *)to);
1559 // We store the size of the just evacuated object in the LDV word so that
1560 // the profiler can guess the position of the next object later.
1561 SET_EVACUAEE_FOR_LDV(from, size_org);
1563 return (StgClosure *)to;
1566 // Same as copy() above, except the object will be allocated in memory
1567 // that will not be scavenged. Used for object that have no pointer
1569 STATIC_INLINE StgClosure *
1570 copy_noscav(StgClosure *src, nat size, step *stp)
1576 nat size_org = size;
1579 TICK_GC_WORDS_COPIED(size);
1580 /* Find out where we're going, using the handy "to" pointer in
1581 * the step of the source object. If it turns out we need to
1582 * evacuate to an older generation, adjust it here (see comment
1585 if (stp->gen_no < evac_gen) {
1586 #ifdef NO_EAGER_PROMOTION
1587 failed_to_evac = rtsTrue;
1589 stp = &generations[evac_gen].steps[0];
1593 /* chain a new block onto the to-space for the destination step if
1596 if (stp->scavd_hp + size >= stp->scavd_hpLim) {
1597 gc_alloc_scavd_block(stp);
1602 stp->scavd_hp = to + size;
1603 for (i = 0; i < size; i++) { // unroll for small i
1606 upd_evacuee((StgClosure *)from,(StgClosure *)to);
1609 // We store the size of the just evacuated object in the LDV word so that
1610 // the profiler can guess the position of the next object later.
1611 SET_EVACUAEE_FOR_LDV(from, size_org);
1613 return (StgClosure *)to;
1616 /* Special version of copy() for when we only want to copy the info
1617 * pointer of an object, but reserve some padding after it. This is
1618 * used to optimise evacuation of BLACKHOLEs.
1623 copyPart(StgClosure *src, nat size_to_reserve, nat size_to_copy, step *stp)
1628 nat size_to_copy_org = size_to_copy;
1631 TICK_GC_WORDS_COPIED(size_to_copy);
1632 if (stp->gen_no < evac_gen) {
1633 #ifdef NO_EAGER_PROMOTION
1634 failed_to_evac = rtsTrue;
1636 stp = &generations[evac_gen].steps[0];
1640 if (stp->hp + size_to_reserve >= stp->hpLim) {
1641 gc_alloc_block(stp);
1644 for(to = stp->hp, from = (P_)src; size_to_copy>0; --size_to_copy) {
1649 stp->hp += size_to_reserve;
1650 upd_evacuee(src,(StgClosure *)dest);
1652 // We store the size of the just evacuated object in the LDV word so that
1653 // the profiler can guess the position of the next object later.
1654 // size_to_copy_org is wrong because the closure already occupies size_to_reserve
1656 SET_EVACUAEE_FOR_LDV(src, size_to_reserve);
1658 if (size_to_reserve - size_to_copy_org > 0)
1659 FILL_SLOP(stp->hp - 1, (int)(size_to_reserve - size_to_copy_org));
1661 return (StgClosure *)dest;
1665 /* -----------------------------------------------------------------------------
1666 Evacuate a large object
1668 This just consists of removing the object from the (doubly-linked)
1669 step->large_objects list, and linking it on to the (singly-linked)
1670 step->new_large_objects list, from where it will be scavenged later.
1672 Convention: bd->flags has BF_EVACUATED set for a large object
1673 that has been evacuated, or unset otherwise.
1674 -------------------------------------------------------------------------- */
1678 evacuate_large(StgPtr p)
1680 bdescr *bd = Bdescr(p);
1683 // object must be at the beginning of the block (or be a ByteArray)
1684 ASSERT(get_itbl((StgClosure *)p)->type == ARR_WORDS ||
1685 (((W_)p & BLOCK_MASK) == 0));
1687 // already evacuated?
1688 if (bd->flags & BF_EVACUATED) {
1689 /* Don't forget to set the failed_to_evac flag if we didn't get
1690 * the desired destination (see comments in evacuate()).
1692 if (bd->gen_no < evac_gen) {
1693 failed_to_evac = rtsTrue;
1694 TICK_GC_FAILED_PROMOTION();
1700 // remove from large_object list
1702 bd->u.back->link = bd->link;
1703 } else { // first object in the list
1704 stp->large_objects = bd->link;
1707 bd->link->u.back = bd->u.back;
1710 /* link it on to the evacuated large object list of the destination step
1713 if (stp->gen_no < evac_gen) {
1714 #ifdef NO_EAGER_PROMOTION
1715 failed_to_evac = rtsTrue;
1717 stp = &generations[evac_gen].steps[0];
1722 bd->gen_no = stp->gen_no;
1723 bd->link = stp->new_large_objects;
1724 stp->new_large_objects = bd;
1725 bd->flags |= BF_EVACUATED;
1728 /* -----------------------------------------------------------------------------
1731 This is called (eventually) for every live object in the system.
1733 The caller to evacuate specifies a desired generation in the
1734 evac_gen global variable. The following conditions apply to
1735 evacuating an object which resides in generation M when we're
1736 collecting up to generation N
1740 else evac to step->to
1742 if M < evac_gen evac to evac_gen, step 0
1744 if the object is already evacuated, then we check which generation
1747 if M >= evac_gen do nothing
1748 if M < evac_gen set failed_to_evac flag to indicate that we
1749 didn't manage to evacuate this object into evac_gen.
1754 evacuate() is the single most important function performance-wise
1755 in the GC. Various things have been tried to speed it up, but as
1756 far as I can tell the code generated by gcc 3.2 with -O2 is about
1757 as good as it's going to get. We pass the argument to evacuate()
1758 in a register using the 'regparm' attribute (see the prototype for
1759 evacuate() near the top of this file).
1761 Changing evacuate() to take an (StgClosure **) rather than
1762 returning the new pointer seems attractive, because we can avoid
1763 writing back the pointer when it hasn't changed (eg. for a static
1764 object, or an object in a generation > N). However, I tried it and
1765 it doesn't help. One reason is that the (StgClosure **) pointer
1766 gets spilled to the stack inside evacuate(), resulting in far more
1767 extra reads/writes than we save.
1768 -------------------------------------------------------------------------- */
1770 REGPARM1 static StgClosure *
1771 evacuate(StgClosure *q)
1778 const StgInfoTable *info;
1781 ASSERT(LOOKS_LIKE_CLOSURE_PTR(q));
1783 if (!HEAP_ALLOCED(q)) {
1785 if (!major_gc) return q;
1788 switch (info->type) {
1791 if (info->srt_bitmap != 0 &&
1792 *THUNK_STATIC_LINK((StgClosure *)q) == NULL) {
1793 *THUNK_STATIC_LINK((StgClosure *)q) = static_objects;
1794 static_objects = (StgClosure *)q;
1799 if (info->srt_bitmap != 0 &&
1800 *FUN_STATIC_LINK((StgClosure *)q) == NULL) {
1801 *FUN_STATIC_LINK((StgClosure *)q) = static_objects;
1802 static_objects = (StgClosure *)q;
1807 /* If q->saved_info != NULL, then it's a revertible CAF - it'll be
1808 * on the CAF list, so don't do anything with it here (we'll
1809 * scavenge it later).
1811 if (((StgIndStatic *)q)->saved_info == NULL
1812 && *IND_STATIC_LINK((StgClosure *)q) == NULL) {
1813 *IND_STATIC_LINK((StgClosure *)q) = static_objects;
1814 static_objects = (StgClosure *)q;
1819 if (*STATIC_LINK(info,(StgClosure *)q) == NULL) {
1820 *STATIC_LINK(info,(StgClosure *)q) = static_objects;
1821 static_objects = (StgClosure *)q;
1825 case CONSTR_INTLIKE:
1826 case CONSTR_CHARLIKE:
1827 case CONSTR_NOCAF_STATIC:
1828 /* no need to put these on the static linked list, they don't need
1834 barf("evacuate(static): strange closure type %d", (int)(info->type));
1840 if (bd->gen_no > N) {
1841 /* Can't evacuate this object, because it's in a generation
1842 * older than the ones we're collecting. Let's hope that it's
1843 * in evac_gen or older, or we will have to arrange to track
1844 * this pointer using the mutable list.
1846 if (bd->gen_no < evac_gen) {
1848 failed_to_evac = rtsTrue;
1849 TICK_GC_FAILED_PROMOTION();
1854 if ((bd->flags & (BF_LARGE | BF_COMPACTED | BF_EVACUATED)) != 0) {
1856 /* pointer into to-space: just return it. This normally
1857 * shouldn't happen, but alllowing it makes certain things
1858 * slightly easier (eg. the mutable list can contain the same
1859 * object twice, for example).
1861 if (bd->flags & BF_EVACUATED) {
1862 if (bd->gen_no < evac_gen) {
1863 failed_to_evac = rtsTrue;
1864 TICK_GC_FAILED_PROMOTION();
1869 /* evacuate large objects by re-linking them onto a different list.
1871 if (bd->flags & BF_LARGE) {
1873 if (info->type == TSO &&
1874 ((StgTSO *)q)->what_next == ThreadRelocated) {
1875 q = (StgClosure *)((StgTSO *)q)->link;
1878 evacuate_large((P_)q);
1882 /* If the object is in a step that we're compacting, then we
1883 * need to use an alternative evacuate procedure.
1885 if (bd->flags & BF_COMPACTED) {
1886 if (!is_marked((P_)q,bd)) {
1888 if (mark_stack_full()) {
1889 mark_stack_overflowed = rtsTrue;
1892 push_mark_stack((P_)q);
1902 switch (info->type) {
1906 return copy(q,sizeW_fromITBL(info),stp);
1910 StgWord w = (StgWord)q->payload[0];
1911 if (q->header.info == Czh_con_info &&
1912 // unsigned, so always true: (StgChar)w >= MIN_CHARLIKE &&
1913 (StgChar)w <= MAX_CHARLIKE) {
1914 return (StgClosure *)CHARLIKE_CLOSURE((StgChar)w);
1916 if (q->header.info == Izh_con_info &&
1917 (StgInt)w >= MIN_INTLIKE && (StgInt)w <= MAX_INTLIKE) {
1918 return (StgClosure *)INTLIKE_CLOSURE((StgInt)w);
1921 return copy_noscav(q,sizeofW(StgHeader)+1,stp);
1927 return copy(q,sizeofW(StgHeader)+1,stp);
1931 return copy(q,sizeofW(StgThunk)+1,stp);
1936 #ifdef NO_PROMOTE_THUNKS
1937 if (bd->gen_no == 0 &&
1938 bd->step->no != 0 &&
1939 bd->step->no == generations[bd->gen_no].n_steps-1) {
1943 return copy(q,sizeofW(StgThunk)+2,stp);
1950 return copy(q,sizeofW(StgHeader)+2,stp);
1953 return copy_noscav(q,sizeofW(StgHeader)+2,stp);
1956 return copy(q,thunk_sizeW_fromITBL(info),stp);
1961 case IND_OLDGEN_PERM:
1964 return copy(q,sizeW_fromITBL(info),stp);
1967 return copy(q,bco_sizeW((StgBCO *)q),stp);
1970 case SE_CAF_BLACKHOLE:
1973 return copyPart(q,BLACKHOLE_sizeW(),sizeofW(StgHeader),stp);
1975 case THUNK_SELECTOR:
1979 if (thunk_selector_depth > MAX_THUNK_SELECTOR_DEPTH) {
1980 return copy(q,THUNK_SELECTOR_sizeW(),stp);
1983 p = eval_thunk_selector(info->layout.selector_offset,
1987 return copy(q,THUNK_SELECTOR_sizeW(),stp);
1990 // q is still BLACKHOLE'd.
1991 thunk_selector_depth++;
1993 thunk_selector_depth--;
1995 // Update the THUNK_SELECTOR with an indirection to the
1996 // EVACUATED closure now at p. Why do this rather than
1997 // upd_evacuee(q,p)? Because we have an invariant that an
1998 // EVACUATED closure always points to an object in the
1999 // same or an older generation (required by the short-cut
2000 // test in the EVACUATED case, below).
2001 SET_INFO(q, &stg_IND_info);
2002 ((StgInd *)q)->indirectee = p;
2005 // We store the size of the just evacuated object in the
2006 // LDV word so that the profiler can guess the position of
2007 // the next object later.
2008 SET_EVACUAEE_FOR_LDV(q, THUNK_SELECTOR_sizeW());
2016 // follow chains of indirections, don't evacuate them
2017 q = ((StgInd*)q)->indirectee;
2029 case CATCH_STM_FRAME:
2030 case CATCH_RETRY_FRAME:
2031 case ATOMICALLY_FRAME:
2032 // shouldn't see these
2033 barf("evacuate: stack frame at %p\n", q);
2036 return copy(q,pap_sizeW((StgPAP*)q),stp);
2039 return copy(q,ap_sizeW((StgAP*)q),stp);
2042 return copy(q,ap_stack_sizeW((StgAP_STACK*)q),stp);
2045 /* Already evacuated, just return the forwarding address.
2046 * HOWEVER: if the requested destination generation (evac_gen) is
2047 * older than the actual generation (because the object was
2048 * already evacuated to a younger generation) then we have to
2049 * set the failed_to_evac flag to indicate that we couldn't
2050 * manage to promote the object to the desired generation.
2053 * Optimisation: the check is fairly expensive, but we can often
2054 * shortcut it if either the required generation is 0, or the
2055 * current object (the EVACUATED) is in a high enough generation.
2056 * We know that an EVACUATED always points to an object in the
2057 * same or an older generation. stp is the lowest step that the
2058 * current object would be evacuated to, so we only do the full
2059 * check if stp is too low.
2061 if (evac_gen > 0 && stp->gen_no < evac_gen) { // optimisation
2062 StgClosure *p = ((StgEvacuated*)q)->evacuee;
2063 if (HEAP_ALLOCED(p) && Bdescr((P_)p)->gen_no < evac_gen) {
2064 failed_to_evac = rtsTrue;
2065 TICK_GC_FAILED_PROMOTION();
2068 return ((StgEvacuated*)q)->evacuee;
2071 // just copy the block
2072 return copy_noscav(q,arr_words_sizeW((StgArrWords *)q),stp);
2075 case MUT_ARR_PTRS_FROZEN:
2076 case MUT_ARR_PTRS_FROZEN0:
2077 // just copy the block
2078 return copy(q,mut_arr_ptrs_sizeW((StgMutArrPtrs *)q),stp);
2082 StgTSO *tso = (StgTSO *)q;
2084 /* Deal with redirected TSOs (a TSO that's had its stack enlarged).
2086 if (tso->what_next == ThreadRelocated) {
2087 q = (StgClosure *)tso->link;
2091 /* To evacuate a small TSO, we need to relocate the update frame
2098 new_tso = (StgTSO *)copyPart((StgClosure *)tso,
2100 sizeofW(StgTSO), stp);
2101 move_TSO(tso, new_tso);
2102 for (p = tso->sp, q = new_tso->sp;
2103 p < tso->stack+tso->stack_size;) {
2107 return (StgClosure *)new_tso;
2114 //StgInfoTable *rip = get_closure_info(q, &size, &ptrs, &nonptrs, &vhs, str);
2115 to = copy(q,BLACKHOLE_sizeW(),stp);
2116 //ToDo: derive size etc from reverted IP
2117 //to = copy(q,size,stp);
2119 debugBelch("@@ evacuate: RBH %p (%s) to %p (%s)",
2120 q, info_type(q), to, info_type(to)));
2125 ASSERT(sizeofW(StgBlockedFetch) >= MIN_NONUPD_SIZE);
2126 to = copy(q,sizeofW(StgBlockedFetch),stp);
2128 debugBelch("@@ evacuate: %p (%s) to %p (%s)",
2129 q, info_type(q), to, info_type(to)));
2136 ASSERT(sizeofW(StgBlockedFetch) >= MIN_UPD_SIZE);
2137 to = copy(q,sizeofW(StgFetchMe),stp);
2139 debugBelch("@@ evacuate: %p (%s) to %p (%s)",
2140 q, info_type(q), to, info_type(to)));
2144 ASSERT(sizeofW(StgBlockedFetch) >= MIN_UPD_SIZE);
2145 to = copy(q,sizeofW(StgFetchMeBlockingQueue),stp);
2147 debugBelch("@@ evacuate: %p (%s) to %p (%s)",
2148 q, info_type(q), to, info_type(to)));
2153 return copy(q,sizeofW(StgTRecHeader),stp);
2155 case TVAR_WAIT_QUEUE:
2156 return copy(q,sizeofW(StgTVarWaitQueue),stp);
2159 return copy(q,sizeofW(StgTVar),stp);
2162 return copy(q,sizeofW(StgTRecChunk),stp);
2165 barf("evacuate: strange closure type %d", (int)(info->type));
2171 /* -----------------------------------------------------------------------------
2172 Evaluate a THUNK_SELECTOR if possible.
2174 returns: NULL if we couldn't evaluate this THUNK_SELECTOR, or
2175 a closure pointer if we evaluated it and this is the result. Note
2176 that "evaluating" the THUNK_SELECTOR doesn't necessarily mean
2177 reducing it to HNF, just that we have eliminated the selection.
2178 The result might be another thunk, or even another THUNK_SELECTOR.
2180 If the return value is non-NULL, the original selector thunk has
2181 been BLACKHOLE'd, and should be updated with an indirection or a
2182 forwarding pointer. If the return value is NULL, then the selector
2186 ToDo: the treatment of THUNK_SELECTORS could be improved in the
2187 following way (from a suggestion by Ian Lynagh):
2189 We can have a chain like this:
2193 |-----> sel_0 --> (a,b)
2195 |-----> sel_0 --> ...
2197 and the depth limit means we don't go all the way to the end of the
2198 chain, which results in a space leak. This affects the recursive
2199 call to evacuate() in the THUNK_SELECTOR case in evacuate(): *not*
2200 the recursive call to eval_thunk_selector() in
2201 eval_thunk_selector().
2203 We could eliminate the depth bound in this case, in the following
2206 - traverse the chain once to discover the *value* of the
2207 THUNK_SELECTOR. Mark all THUNK_SELECTORS that we
2208 visit on the way as having been visited already (somehow).
2210 - in a second pass, traverse the chain again updating all
2211 THUNK_SEELCTORS that we find on the way with indirections to
2214 - if we encounter a "marked" THUNK_SELECTOR in a normal
2215 evacuate(), we konw it can't be updated so just evac it.
2217 Program that illustrates the problem:
2220 foo (x:xs) = let (ys, zs) = foo xs
2221 in if x >= 0 then (x:ys, zs) else (ys, x:zs)
2223 main = bar [1..(100000000::Int)]
2224 bar xs = (\(ys, zs) -> print ys >> print zs) (foo xs)
2226 -------------------------------------------------------------------------- */
2228 static inline rtsBool
2229 is_to_space ( StgClosure *p )
2233 bd = Bdescr((StgPtr)p);
2234 if (HEAP_ALLOCED(p) &&
2235 ((bd->flags & BF_EVACUATED)
2236 || ((bd->flags & BF_COMPACTED) &&
2237 is_marked((P_)p,bd)))) {
2245 eval_thunk_selector( nat field, StgSelector * p )
2248 const StgInfoTable *info_ptr;
2249 StgClosure *selectee;
2251 selectee = p->selectee;
2253 // Save the real info pointer (NOTE: not the same as get_itbl()).
2254 info_ptr = p->header.info;
2256 // If the THUNK_SELECTOR is in a generation that we are not
2257 // collecting, then bail out early. We won't be able to save any
2258 // space in any case, and updating with an indirection is trickier
2260 if (Bdescr((StgPtr)p)->gen_no > N) {
2264 // BLACKHOLE the selector thunk, since it is now under evaluation.
2265 // This is important to stop us going into an infinite loop if
2266 // this selector thunk eventually refers to itself.
2267 SET_INFO(p,&stg_BLACKHOLE_info);
2271 // We don't want to end up in to-space, because this causes
2272 // problems when the GC later tries to evacuate the result of
2273 // eval_thunk_selector(). There are various ways this could
2276 // 1. following an IND_STATIC
2278 // 2. when the old generation is compacted, the mark phase updates
2279 // from-space pointers to be to-space pointers, and we can't
2280 // reliably tell which we're following (eg. from an IND_STATIC).
2282 // 3. compacting GC again: if we're looking at a constructor in
2283 // the compacted generation, it might point directly to objects
2284 // in to-space. We must bale out here, otherwise doing the selection
2285 // will result in a to-space pointer being returned.
2287 // (1) is dealt with using a BF_EVACUATED test on the
2288 // selectee. (2) and (3): we can tell if we're looking at an
2289 // object in the compacted generation that might point to
2290 // to-space objects by testing that (a) it is BF_COMPACTED, (b)
2291 // the compacted generation is being collected, and (c) the
2292 // object is marked. Only a marked object may have pointers that
2293 // point to to-space objects, because that happens when
2296 // The to-space test is now embodied in the in_to_space() inline
2297 // function, as it is re-used below.
2299 if (is_to_space(selectee)) {
2303 info = get_itbl(selectee);
2304 switch (info->type) {
2312 case CONSTR_NOCAF_STATIC:
2313 // check that the size is in range
2314 ASSERT(field < (StgWord32)(info->layout.payload.ptrs +
2315 info->layout.payload.nptrs));
2317 // Select the right field from the constructor, and check
2318 // that the result isn't in to-space. It might be in
2319 // to-space if, for example, this constructor contains
2320 // pointers to younger-gen objects (and is on the mut-once
2325 q = selectee->payload[field];
2326 if (is_to_space(q)) {
2336 case IND_OLDGEN_PERM:
2338 selectee = ((StgInd *)selectee)->indirectee;
2342 // We don't follow pointers into to-space; the constructor
2343 // has already been evacuated, so we won't save any space
2344 // leaks by evaluating this selector thunk anyhow.
2347 case THUNK_SELECTOR:
2351 // check that we don't recurse too much, re-using the
2352 // depth bound also used in evacuate().
2353 if (thunk_selector_depth >= MAX_THUNK_SELECTOR_DEPTH) {
2356 thunk_selector_depth++;
2358 val = eval_thunk_selector(info->layout.selector_offset,
2359 (StgSelector *)selectee);
2361 thunk_selector_depth--;
2366 // We evaluated this selector thunk, so update it with
2367 // an indirection. NOTE: we don't use UPD_IND here,
2368 // because we are guaranteed that p is in a generation
2369 // that we are collecting, and we never want to put the
2370 // indirection on a mutable list.
2372 // For the purposes of LDV profiling, we have destroyed
2373 // the original selector thunk.
2374 SET_INFO(p, info_ptr);
2375 LDV_RECORD_DEAD_FILL_SLOP_DYNAMIC(selectee);
2377 ((StgInd *)selectee)->indirectee = val;
2378 SET_INFO(selectee,&stg_IND_info);
2380 // For the purposes of LDV profiling, we have created an
2382 LDV_RECORD_CREATE(selectee);
2399 case SE_CAF_BLACKHOLE:
2411 // not evaluated yet
2415 barf("eval_thunk_selector: strange selectee %d",
2420 // We didn't manage to evaluate this thunk; restore the old info pointer
2421 SET_INFO(p, info_ptr);
2425 /* -----------------------------------------------------------------------------
2426 move_TSO is called to update the TSO structure after it has been
2427 moved from one place to another.
2428 -------------------------------------------------------------------------- */
2431 move_TSO (StgTSO *src, StgTSO *dest)
2435 // relocate the stack pointer...
2436 diff = (StgPtr)dest - (StgPtr)src; // In *words*
2437 dest->sp = (StgPtr)dest->sp + diff;
2440 /* Similar to scavenge_large_bitmap(), but we don't write back the
2441 * pointers we get back from evacuate().
2444 scavenge_large_srt_bitmap( StgLargeSRT *large_srt )
2451 bitmap = large_srt->l.bitmap[b];
2452 size = (nat)large_srt->l.size;
2453 p = (StgClosure **)large_srt->srt;
2454 for (i = 0; i < size; ) {
2455 if ((bitmap & 1) != 0) {
2460 if (i % BITS_IN(W_) == 0) {
2462 bitmap = large_srt->l.bitmap[b];
2464 bitmap = bitmap >> 1;
2469 /* evacuate the SRT. If srt_bitmap is zero, then there isn't an
2470 * srt field in the info table. That's ok, because we'll
2471 * never dereference it.
2474 scavenge_srt (StgClosure **srt, nat srt_bitmap)
2479 bitmap = srt_bitmap;
2482 if (bitmap == (StgHalfWord)(-1)) {
2483 scavenge_large_srt_bitmap( (StgLargeSRT *)srt );
2487 while (bitmap != 0) {
2488 if ((bitmap & 1) != 0) {
2489 #ifdef ENABLE_WIN32_DLL_SUPPORT
2490 // Special-case to handle references to closures hiding out in DLLs, since
2491 // double indirections required to get at those. The code generator knows
2492 // which is which when generating the SRT, so it stores the (indirect)
2493 // reference to the DLL closure in the table by first adding one to it.
2494 // We check for this here, and undo the addition before evacuating it.
2496 // If the SRT entry hasn't got bit 0 set, the SRT entry points to a
2497 // closure that's fixed at link-time, and no extra magic is required.
2498 if ( (unsigned long)(*srt) & 0x1 ) {
2499 evacuate(*stgCast(StgClosure**,(stgCast(unsigned long, *srt) & ~0x1)));
2508 bitmap = bitmap >> 1;
2514 scavenge_thunk_srt(const StgInfoTable *info)
2516 StgThunkInfoTable *thunk_info;
2518 if (!major_gc) return;
2520 thunk_info = itbl_to_thunk_itbl(info);
2521 scavenge_srt((StgClosure **)GET_SRT(thunk_info), thunk_info->i.srt_bitmap);
2525 scavenge_fun_srt(const StgInfoTable *info)
2527 StgFunInfoTable *fun_info;
2529 if (!major_gc) return;
2531 fun_info = itbl_to_fun_itbl(info);
2532 scavenge_srt((StgClosure **)GET_FUN_SRT(fun_info), fun_info->i.srt_bitmap);
2535 /* -----------------------------------------------------------------------------
2537 -------------------------------------------------------------------------- */
2540 scavengeTSO (StgTSO *tso)
2542 if ( tso->why_blocked == BlockedOnMVar
2543 || tso->why_blocked == BlockedOnBlackHole
2544 || tso->why_blocked == BlockedOnException
2546 || tso->why_blocked == BlockedOnGA
2547 || tso->why_blocked == BlockedOnGA_NoSend
2550 tso->block_info.closure = evacuate(tso->block_info.closure);
2552 if ( tso->blocked_exceptions != NULL ) {
2553 tso->blocked_exceptions =
2554 (StgTSO *)evacuate((StgClosure *)tso->blocked_exceptions);
2557 // We don't always chase the link field: TSOs on the blackhole
2558 // queue are not automatically alive, so the link field is a
2559 // "weak" pointer in that case.
2560 if (tso->why_blocked != BlockedOnBlackHole) {
2561 tso->link = (StgTSO *)evacuate((StgClosure *)tso->link);
2564 // scavange current transaction record
2565 tso->trec = (StgTRecHeader *)evacuate((StgClosure *)tso->trec);
2567 // scavenge this thread's stack
2568 scavenge_stack(tso->sp, &(tso->stack[tso->stack_size]));
2571 /* -----------------------------------------------------------------------------
2572 Blocks of function args occur on the stack (at the top) and
2574 -------------------------------------------------------------------------- */
2576 STATIC_INLINE StgPtr
2577 scavenge_arg_block (StgFunInfoTable *fun_info, StgClosure **args)
2584 switch (fun_info->f.fun_type) {
2586 bitmap = BITMAP_BITS(fun_info->f.b.bitmap);
2587 size = BITMAP_SIZE(fun_info->f.b.bitmap);
2590 size = GET_FUN_LARGE_BITMAP(fun_info)->size;
2591 scavenge_large_bitmap(p, GET_FUN_LARGE_BITMAP(fun_info), size);
2595 bitmap = BITMAP_BITS(stg_arg_bitmaps[fun_info->f.fun_type]);
2596 size = BITMAP_SIZE(stg_arg_bitmaps[fun_info->f.fun_type]);
2599 if ((bitmap & 1) == 0) {
2600 *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
2603 bitmap = bitmap >> 1;
2611 STATIC_INLINE StgPtr
2612 scavenge_PAP_payload (StgClosure *fun, StgClosure **payload, StgWord size)
2616 StgFunInfoTable *fun_info;
2618 fun_info = get_fun_itbl(fun);
2619 ASSERT(fun_info->i.type != PAP);
2620 p = (StgPtr)payload;
2622 switch (fun_info->f.fun_type) {
2624 bitmap = BITMAP_BITS(fun_info->f.b.bitmap);
2627 scavenge_large_bitmap(p, GET_FUN_LARGE_BITMAP(fun_info), size);
2631 scavenge_large_bitmap((StgPtr)payload, BCO_BITMAP(fun), size);
2635 bitmap = BITMAP_BITS(stg_arg_bitmaps[fun_info->f.fun_type]);
2638 if ((bitmap & 1) == 0) {
2639 *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
2642 bitmap = bitmap >> 1;
2650 STATIC_INLINE StgPtr
2651 scavenge_PAP (StgPAP *pap)
2653 pap->fun = evacuate(pap->fun);
2654 return scavenge_PAP_payload (pap->fun, pap->payload, pap->n_args);
2657 STATIC_INLINE StgPtr
2658 scavenge_AP (StgAP *ap)
2660 ap->fun = evacuate(ap->fun);
2661 return scavenge_PAP_payload (ap->fun, ap->payload, ap->n_args);
2664 /* -----------------------------------------------------------------------------
2665 Scavenge a given step until there are no more objects in this step
2668 evac_gen is set by the caller to be either zero (for a step in a
2669 generation < N) or G where G is the generation of the step being
2672 We sometimes temporarily change evac_gen back to zero if we're
2673 scavenging a mutable object where early promotion isn't such a good
2675 -------------------------------------------------------------------------- */
2683 nat saved_evac_gen = evac_gen;
2688 failed_to_evac = rtsFalse;
2690 /* scavenge phase - standard breadth-first scavenging of the
2694 while (bd != stp->hp_bd || p < stp->hp) {
2696 // If we're at the end of this block, move on to the next block
2697 if (bd != stp->hp_bd && p == bd->free) {
2703 ASSERT(LOOKS_LIKE_CLOSURE_PTR(p));
2704 info = get_itbl((StgClosure *)p);
2706 ASSERT(thunk_selector_depth == 0);
2709 switch (info->type) {
2713 StgMVar *mvar = ((StgMVar *)p);
2715 mvar->head = (StgTSO *)evacuate((StgClosure *)mvar->head);
2716 mvar->tail = (StgTSO *)evacuate((StgClosure *)mvar->tail);
2717 mvar->value = evacuate((StgClosure *)mvar->value);
2718 evac_gen = saved_evac_gen;
2719 failed_to_evac = rtsTrue; // mutable.
2720 p += sizeofW(StgMVar);
2725 scavenge_fun_srt(info);
2726 ((StgClosure *)p)->payload[1] = evacuate(((StgClosure *)p)->payload[1]);
2727 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
2728 p += sizeofW(StgHeader) + 2;
2732 scavenge_thunk_srt(info);
2733 ((StgThunk *)p)->payload[1] = evacuate(((StgThunk *)p)->payload[1]);
2734 ((StgThunk *)p)->payload[0] = evacuate(((StgThunk *)p)->payload[0]);
2735 p += sizeofW(StgThunk) + 2;
2739 ((StgClosure *)p)->payload[1] = evacuate(((StgClosure *)p)->payload[1]);
2740 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
2741 p += sizeofW(StgHeader) + 2;
2745 scavenge_thunk_srt(info);
2746 ((StgThunk *)p)->payload[0] = evacuate(((StgThunk *)p)->payload[0]);
2747 p += sizeofW(StgThunk) + 1;
2751 scavenge_fun_srt(info);
2753 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
2754 p += sizeofW(StgHeader) + 1;
2758 scavenge_thunk_srt(info);
2759 p += sizeofW(StgThunk) + 1;
2763 scavenge_fun_srt(info);
2765 p += sizeofW(StgHeader) + 1;
2769 scavenge_thunk_srt(info);
2770 p += sizeofW(StgThunk) + 2;
2774 scavenge_fun_srt(info);
2776 p += sizeofW(StgHeader) + 2;
2780 scavenge_thunk_srt(info);
2781 ((StgThunk *)p)->payload[0] = evacuate(((StgThunk *)p)->payload[0]);
2782 p += sizeofW(StgThunk) + 2;
2786 scavenge_fun_srt(info);
2788 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
2789 p += sizeofW(StgHeader) + 2;
2793 scavenge_fun_srt(info);
2800 scavenge_thunk_srt(info);
2801 end = (P_)((StgThunk *)p)->payload + info->layout.payload.ptrs;
2802 for (p = (P_)((StgThunk *)p)->payload; p < end; p++) {
2803 *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
2805 p += info->layout.payload.nptrs;
2816 end = (P_)((StgClosure *)p)->payload + info->layout.payload.ptrs;
2817 for (p = (P_)((StgClosure *)p)->payload; p < end; p++) {
2818 *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
2820 p += info->layout.payload.nptrs;
2825 StgBCO *bco = (StgBCO *)p;
2826 bco->instrs = (StgArrWords *)evacuate((StgClosure *)bco->instrs);
2827 bco->literals = (StgArrWords *)evacuate((StgClosure *)bco->literals);
2828 bco->ptrs = (StgMutArrPtrs *)evacuate((StgClosure *)bco->ptrs);
2829 bco->itbls = (StgArrWords *)evacuate((StgClosure *)bco->itbls);
2830 p += bco_sizeW(bco);
2835 if (stp->gen->no != 0) {
2838 // No need to call LDV_recordDead_FILL_SLOP_DYNAMIC() because an
2839 // IND_OLDGEN_PERM closure is larger than an IND_PERM closure.
2840 LDV_recordDead((StgClosure *)p, sizeofW(StgInd));
2843 // Todo: maybe use SET_HDR() and remove LDV_RECORD_CREATE()?
2845 SET_INFO(((StgClosure *)p), &stg_IND_OLDGEN_PERM_info);
2847 // We pretend that p has just been created.
2848 LDV_RECORD_CREATE((StgClosure *)p);
2851 case IND_OLDGEN_PERM:
2852 ((StgInd *)p)->indirectee = evacuate(((StgInd *)p)->indirectee);
2853 p += sizeofW(StgInd);
2858 ((StgMutVar *)p)->var = evacuate(((StgMutVar *)p)->var);
2859 evac_gen = saved_evac_gen;
2860 failed_to_evac = rtsTrue; // mutable anyhow
2861 p += sizeofW(StgMutVar);
2865 case SE_CAF_BLACKHOLE:
2868 p += BLACKHOLE_sizeW();
2871 case THUNK_SELECTOR:
2873 StgSelector *s = (StgSelector *)p;
2874 s->selectee = evacuate(s->selectee);
2875 p += THUNK_SELECTOR_sizeW();
2879 // A chunk of stack saved in a heap object
2882 StgAP_STACK *ap = (StgAP_STACK *)p;
2884 ap->fun = evacuate(ap->fun);
2885 scavenge_stack((StgPtr)ap->payload, (StgPtr)ap->payload + ap->size);
2886 p = (StgPtr)ap->payload + ap->size;
2891 p = scavenge_PAP((StgPAP *)p);
2895 p = scavenge_AP((StgAP *)p);
2899 // nothing to follow
2900 p += arr_words_sizeW((StgArrWords *)p);
2904 // follow everything
2908 evac_gen = 0; // repeatedly mutable
2909 next = p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
2910 for (p = (P_)((StgMutArrPtrs *)p)->payload; p < next; p++) {
2911 *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
2913 evac_gen = saved_evac_gen;
2914 failed_to_evac = rtsTrue; // mutable anyhow.
2918 case MUT_ARR_PTRS_FROZEN:
2919 case MUT_ARR_PTRS_FROZEN0:
2920 // follow everything
2924 next = p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
2925 for (p = (P_)((StgMutArrPtrs *)p)->payload; p < next; p++) {
2926 *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
2928 // it's tempting to recordMutable() if failed_to_evac is
2929 // false, but that breaks some assumptions (eg. every
2930 // closure on the mutable list is supposed to have the MUT
2931 // flag set, and MUT_ARR_PTRS_FROZEN doesn't).
2937 StgTSO *tso = (StgTSO *)p;
2940 evac_gen = saved_evac_gen;
2941 failed_to_evac = rtsTrue; // mutable anyhow.
2942 p += tso_sizeW(tso);
2950 nat size, ptrs, nonptrs, vhs;
2952 StgInfoTable *rip = get_closure_info(p, &size, &ptrs, &nonptrs, &vhs, str);
2954 StgRBH *rbh = (StgRBH *)p;
2955 (StgClosure *)rbh->blocking_queue =
2956 evacuate((StgClosure *)rbh->blocking_queue);
2957 failed_to_evac = rtsTrue; // mutable anyhow.
2959 debugBelch("@@ scavenge: RBH %p (%s) (new blocking_queue link=%p)",
2960 p, info_type(p), (StgClosure *)rbh->blocking_queue));
2961 // ToDo: use size of reverted closure here!
2962 p += BLACKHOLE_sizeW();
2968 StgBlockedFetch *bf = (StgBlockedFetch *)p;
2969 // follow the pointer to the node which is being demanded
2970 (StgClosure *)bf->node =
2971 evacuate((StgClosure *)bf->node);
2972 // follow the link to the rest of the blocking queue
2973 (StgClosure *)bf->link =
2974 evacuate((StgClosure *)bf->link);
2976 debugBelch("@@ scavenge: %p (%s); node is now %p; exciting, isn't it",
2977 bf, info_type((StgClosure *)bf),
2978 bf->node, info_type(bf->node)));
2979 p += sizeofW(StgBlockedFetch);
2987 p += sizeofW(StgFetchMe);
2988 break; // nothing to do in this case
2992 StgFetchMeBlockingQueue *fmbq = (StgFetchMeBlockingQueue *)p;
2993 (StgClosure *)fmbq->blocking_queue =
2994 evacuate((StgClosure *)fmbq->blocking_queue);
2996 debugBelch("@@ scavenge: %p (%s) exciting, isn't it",
2997 p, info_type((StgClosure *)p)));
2998 p += sizeofW(StgFetchMeBlockingQueue);
3003 case TVAR_WAIT_QUEUE:
3005 StgTVarWaitQueue *wq = ((StgTVarWaitQueue *) p);
3007 wq->waiting_tso = (StgTSO *)evacuate((StgClosure*)wq->waiting_tso);
3008 wq->next_queue_entry = (StgTVarWaitQueue *)evacuate((StgClosure*)wq->next_queue_entry);
3009 wq->prev_queue_entry = (StgTVarWaitQueue *)evacuate((StgClosure*)wq->prev_queue_entry);
3010 evac_gen = saved_evac_gen;
3011 failed_to_evac = rtsTrue; // mutable
3012 p += sizeofW(StgTVarWaitQueue);
3018 StgTVar *tvar = ((StgTVar *) p);
3020 tvar->current_value = evacuate((StgClosure*)tvar->current_value);
3021 tvar->first_wait_queue_entry = (StgTVarWaitQueue *)evacuate((StgClosure*)tvar->first_wait_queue_entry);
3023 tvar->last_update_by = (StgTRecHeader *)evacuate((StgClosure*)tvar->last_update_by);
3025 evac_gen = saved_evac_gen;
3026 failed_to_evac = rtsTrue; // mutable
3027 p += sizeofW(StgTVar);
3033 StgTRecHeader *trec = ((StgTRecHeader *) p);
3035 trec->enclosing_trec = (StgTRecHeader *)evacuate((StgClosure*)trec->enclosing_trec);
3036 trec->current_chunk = (StgTRecChunk *)evacuate((StgClosure*)trec->current_chunk);
3037 evac_gen = saved_evac_gen;
3038 failed_to_evac = rtsTrue; // mutable
3039 p += sizeofW(StgTRecHeader);
3046 StgTRecChunk *tc = ((StgTRecChunk *) p);
3047 TRecEntry *e = &(tc -> entries[0]);
3049 tc->prev_chunk = (StgTRecChunk *)evacuate((StgClosure*)tc->prev_chunk);
3050 for (i = 0; i < tc -> next_entry_idx; i ++, e++ ) {
3051 e->tvar = (StgTVar *)evacuate((StgClosure*)e->tvar);
3052 e->expected_value = evacuate((StgClosure*)e->expected_value);
3053 e->new_value = evacuate((StgClosure*)e->new_value);
3055 evac_gen = saved_evac_gen;
3056 failed_to_evac = rtsTrue; // mutable
3057 p += sizeofW(StgTRecChunk);
3062 barf("scavenge: unimplemented/strange closure type %d @ %p",
3067 * We need to record the current object on the mutable list if
3068 * (a) It is actually mutable, or
3069 * (b) It contains pointers to a younger generation.
3070 * Case (b) arises if we didn't manage to promote everything that
3071 * the current object points to into the current generation.
3073 if (failed_to_evac) {
3074 failed_to_evac = rtsFalse;
3075 if (stp->gen_no > 0) {
3076 recordMutableGen((StgClosure *)q, stp->gen);
3085 /* -----------------------------------------------------------------------------
3086 Scavenge everything on the mark stack.
3088 This is slightly different from scavenge():
3089 - we don't walk linearly through the objects, so the scavenger
3090 doesn't need to advance the pointer on to the next object.
3091 -------------------------------------------------------------------------- */
3094 scavenge_mark_stack(void)
3100 evac_gen = oldest_gen->no;
3101 saved_evac_gen = evac_gen;
3104 while (!mark_stack_empty()) {
3105 p = pop_mark_stack();
3107 ASSERT(LOOKS_LIKE_CLOSURE_PTR(p));
3108 info = get_itbl((StgClosure *)p);
3111 switch (info->type) {
3115 StgMVar *mvar = ((StgMVar *)p);
3117 mvar->head = (StgTSO *)evacuate((StgClosure *)mvar->head);
3118 mvar->tail = (StgTSO *)evacuate((StgClosure *)mvar->tail);
3119 mvar->value = evacuate((StgClosure *)mvar->value);
3120 evac_gen = saved_evac_gen;
3121 failed_to_evac = rtsTrue; // mutable.
3126 scavenge_fun_srt(info);
3127 ((StgClosure *)p)->payload[1] = evacuate(((StgClosure *)p)->payload[1]);
3128 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
3132 scavenge_thunk_srt(info);
3133 ((StgThunk *)p)->payload[1] = evacuate(((StgThunk *)p)->payload[1]);
3134 ((StgThunk *)p)->payload[0] = evacuate(((StgThunk *)p)->payload[0]);
3138 ((StgClosure *)p)->payload[1] = evacuate(((StgClosure *)p)->payload[1]);
3139 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
3144 scavenge_fun_srt(info);
3145 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
3150 scavenge_thunk_srt(info);
3151 ((StgThunk *)p)->payload[0] = evacuate(((StgThunk *)p)->payload[0]);
3156 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
3161 scavenge_fun_srt(info);
3166 scavenge_thunk_srt(info);
3174 scavenge_fun_srt(info);
3181 scavenge_thunk_srt(info);
3182 end = (P_)((StgThunk *)p)->payload + info->layout.payload.ptrs;
3183 for (p = (P_)((StgThunk *)p)->payload; p < end; p++) {
3184 *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
3196 end = (P_)((StgClosure *)p)->payload + info->layout.payload.ptrs;
3197 for (p = (P_)((StgClosure *)p)->payload; p < end; p++) {
3198 *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
3204 StgBCO *bco = (StgBCO *)p;
3205 bco->instrs = (StgArrWords *)evacuate((StgClosure *)bco->instrs);
3206 bco->literals = (StgArrWords *)evacuate((StgClosure *)bco->literals);
3207 bco->ptrs = (StgMutArrPtrs *)evacuate((StgClosure *)bco->ptrs);
3208 bco->itbls = (StgArrWords *)evacuate((StgClosure *)bco->itbls);
3213 // don't need to do anything here: the only possible case
3214 // is that we're in a 1-space compacting collector, with
3215 // no "old" generation.
3219 case IND_OLDGEN_PERM:
3220 ((StgInd *)p)->indirectee =
3221 evacuate(((StgInd *)p)->indirectee);
3226 ((StgMutVar *)p)->var = evacuate(((StgMutVar *)p)->var);
3227 evac_gen = saved_evac_gen;
3228 failed_to_evac = rtsTrue;
3232 case SE_CAF_BLACKHOLE:
3238 case THUNK_SELECTOR:
3240 StgSelector *s = (StgSelector *)p;
3241 s->selectee = evacuate(s->selectee);
3245 // A chunk of stack saved in a heap object
3248 StgAP_STACK *ap = (StgAP_STACK *)p;
3250 ap->fun = evacuate(ap->fun);
3251 scavenge_stack((StgPtr)ap->payload, (StgPtr)ap->payload + ap->size);
3256 scavenge_PAP((StgPAP *)p);
3260 scavenge_AP((StgAP *)p);
3264 // follow everything
3268 evac_gen = 0; // repeatedly mutable
3269 next = p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
3270 for (p = (P_)((StgMutArrPtrs *)p)->payload; p < next; p++) {
3271 *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
3273 evac_gen = saved_evac_gen;
3274 failed_to_evac = rtsTrue; // mutable anyhow.
3278 case MUT_ARR_PTRS_FROZEN:
3279 case MUT_ARR_PTRS_FROZEN0:
3280 // follow everything
3284 next = p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
3285 for (p = (P_)((StgMutArrPtrs *)p)->payload; p < next; p++) {
3286 *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
3293 StgTSO *tso = (StgTSO *)p;
3296 evac_gen = saved_evac_gen;
3297 failed_to_evac = rtsTrue;
3305 nat size, ptrs, nonptrs, vhs;
3307 StgInfoTable *rip = get_closure_info(p, &size, &ptrs, &nonptrs, &vhs, str);
3309 StgRBH *rbh = (StgRBH *)p;
3310 bh->blocking_queue =
3311 (StgTSO *)evacuate((StgClosure *)bh->blocking_queue);
3312 failed_to_evac = rtsTrue; // mutable anyhow.
3314 debugBelch("@@ scavenge: RBH %p (%s) (new blocking_queue link=%p)",
3315 p, info_type(p), (StgClosure *)rbh->blocking_queue));
3321 StgBlockedFetch *bf = (StgBlockedFetch *)p;
3322 // follow the pointer to the node which is being demanded
3323 (StgClosure *)bf->node =
3324 evacuate((StgClosure *)bf->node);
3325 // follow the link to the rest of the blocking queue
3326 (StgClosure *)bf->link =
3327 evacuate((StgClosure *)bf->link);
3329 debugBelch("@@ scavenge: %p (%s); node is now %p; exciting, isn't it",
3330 bf, info_type((StgClosure *)bf),
3331 bf->node, info_type(bf->node)));
3339 break; // nothing to do in this case
3343 StgFetchMeBlockingQueue *fmbq = (StgFetchMeBlockingQueue *)p;
3344 (StgClosure *)fmbq->blocking_queue =
3345 evacuate((StgClosure *)fmbq->blocking_queue);
3347 debugBelch("@@ scavenge: %p (%s) exciting, isn't it",
3348 p, info_type((StgClosure *)p)));
3353 case TVAR_WAIT_QUEUE:
3355 StgTVarWaitQueue *wq = ((StgTVarWaitQueue *) p);
3357 wq->waiting_tso = (StgTSO *)evacuate((StgClosure*)wq->waiting_tso);
3358 wq->next_queue_entry = (StgTVarWaitQueue *)evacuate((StgClosure*)wq->next_queue_entry);
3359 wq->prev_queue_entry = (StgTVarWaitQueue *)evacuate((StgClosure*)wq->prev_queue_entry);
3360 evac_gen = saved_evac_gen;
3361 failed_to_evac = rtsTrue; // mutable
3367 StgTVar *tvar = ((StgTVar *) p);
3369 tvar->current_value = evacuate((StgClosure*)tvar->current_value);
3370 tvar->first_wait_queue_entry = (StgTVarWaitQueue *)evacuate((StgClosure*)tvar->first_wait_queue_entry);
3372 tvar->last_update_by = (StgTRecHeader *)evacuate((StgClosure*)tvar->last_update_by);
3374 evac_gen = saved_evac_gen;
3375 failed_to_evac = rtsTrue; // mutable
3382 StgTRecChunk *tc = ((StgTRecChunk *) p);
3383 TRecEntry *e = &(tc -> entries[0]);
3385 tc->prev_chunk = (StgTRecChunk *)evacuate((StgClosure*)tc->prev_chunk);
3386 for (i = 0; i < tc -> next_entry_idx; i ++, e++ ) {
3387 e->tvar = (StgTVar *)evacuate((StgClosure*)e->tvar);
3388 e->expected_value = evacuate((StgClosure*)e->expected_value);
3389 e->new_value = evacuate((StgClosure*)e->new_value);
3391 evac_gen = saved_evac_gen;
3392 failed_to_evac = rtsTrue; // mutable
3398 StgTRecHeader *trec = ((StgTRecHeader *) p);
3400 trec->enclosing_trec = (StgTRecHeader *)evacuate((StgClosure*)trec->enclosing_trec);
3401 trec->current_chunk = (StgTRecChunk *)evacuate((StgClosure*)trec->current_chunk);
3402 evac_gen = saved_evac_gen;
3403 failed_to_evac = rtsTrue; // mutable
3408 barf("scavenge_mark_stack: unimplemented/strange closure type %d @ %p",
3412 if (failed_to_evac) {
3413 failed_to_evac = rtsFalse;
3415 recordMutableGen((StgClosure *)q, &generations[evac_gen]);
3419 // mark the next bit to indicate "scavenged"
3420 mark(q+1, Bdescr(q));
3422 } // while (!mark_stack_empty())
3424 // start a new linear scan if the mark stack overflowed at some point
3425 if (mark_stack_overflowed && oldgen_scan_bd == NULL) {
3426 IF_DEBUG(gc, debugBelch("scavenge_mark_stack: starting linear scan"));
3427 mark_stack_overflowed = rtsFalse;
3428 oldgen_scan_bd = oldest_gen->steps[0].old_blocks;
3429 oldgen_scan = oldgen_scan_bd->start;
3432 if (oldgen_scan_bd) {
3433 // push a new thing on the mark stack
3435 // find a closure that is marked but not scavenged, and start
3437 while (oldgen_scan < oldgen_scan_bd->free
3438 && !is_marked(oldgen_scan,oldgen_scan_bd)) {
3442 if (oldgen_scan < oldgen_scan_bd->free) {
3444 // already scavenged?
3445 if (is_marked(oldgen_scan+1,oldgen_scan_bd)) {
3446 oldgen_scan += sizeofW(StgHeader) + MIN_NONUPD_SIZE;
3449 push_mark_stack(oldgen_scan);
3450 // ToDo: bump the linear scan by the actual size of the object
3451 oldgen_scan += sizeofW(StgHeader) + MIN_NONUPD_SIZE;
3455 oldgen_scan_bd = oldgen_scan_bd->link;
3456 if (oldgen_scan_bd != NULL) {
3457 oldgen_scan = oldgen_scan_bd->start;
3463 /* -----------------------------------------------------------------------------
3464 Scavenge one object.
3466 This is used for objects that are temporarily marked as mutable
3467 because they contain old-to-new generation pointers. Only certain
3468 objects can have this property.
3469 -------------------------------------------------------------------------- */
3472 scavenge_one(StgPtr p)
3474 const StgInfoTable *info;
3475 nat saved_evac_gen = evac_gen;
3478 ASSERT(LOOKS_LIKE_CLOSURE_PTR(p));
3479 info = get_itbl((StgClosure *)p);
3481 switch (info->type) {
3485 StgMVar *mvar = ((StgMVar *)p);
3487 mvar->head = (StgTSO *)evacuate((StgClosure *)mvar->head);
3488 mvar->tail = (StgTSO *)evacuate((StgClosure *)mvar->tail);
3489 mvar->value = evacuate((StgClosure *)mvar->value);
3490 evac_gen = saved_evac_gen;
3491 failed_to_evac = rtsTrue; // mutable.
3504 end = (StgPtr)((StgThunk *)p)->payload + info->layout.payload.ptrs;
3505 for (q = (StgPtr)((StgThunk *)p)->payload; q < end; q++) {
3506 *q = (StgWord)(StgPtr)evacuate((StgClosure *)*q);
3512 case FUN_1_0: // hardly worth specialising these guys
3528 end = (StgPtr)((StgClosure *)p)->payload + info->layout.payload.ptrs;
3529 for (q = (StgPtr)((StgClosure *)p)->payload; q < end; q++) {
3530 *q = (StgWord)(StgPtr)evacuate((StgClosure *)*q);
3537 ((StgMutVar *)p)->var = evacuate(((StgMutVar *)p)->var);
3538 evac_gen = saved_evac_gen;
3539 failed_to_evac = rtsTrue; // mutable anyhow
3543 case SE_CAF_BLACKHOLE:
3548 case THUNK_SELECTOR:
3550 StgSelector *s = (StgSelector *)p;
3551 s->selectee = evacuate(s->selectee);
3557 StgAP_STACK *ap = (StgAP_STACK *)p;
3559 ap->fun = evacuate(ap->fun);
3560 scavenge_stack((StgPtr)ap->payload, (StgPtr)ap->payload + ap->size);
3561 p = (StgPtr)ap->payload + ap->size;
3566 p = scavenge_PAP((StgPAP *)p);
3570 p = scavenge_AP((StgAP *)p);
3574 // nothing to follow
3579 // follow everything
3582 evac_gen = 0; // repeatedly mutable
3583 next = p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
3584 for (p = (P_)((StgMutArrPtrs *)p)->payload; p < next; p++) {
3585 *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
3587 evac_gen = saved_evac_gen;
3588 failed_to_evac = rtsTrue;
3592 case MUT_ARR_PTRS_FROZEN:
3593 case MUT_ARR_PTRS_FROZEN0:
3595 // follow everything
3598 next = p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
3599 for (p = (P_)((StgMutArrPtrs *)p)->payload; p < next; p++) {
3600 *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
3607 StgTSO *tso = (StgTSO *)p;
3609 evac_gen = 0; // repeatedly mutable
3611 evac_gen = saved_evac_gen;
3612 failed_to_evac = rtsTrue;
3620 nat size, ptrs, nonptrs, vhs;
3622 StgInfoTable *rip = get_closure_info(p, &size, &ptrs, &nonptrs, &vhs, str);
3624 StgRBH *rbh = (StgRBH *)p;
3625 (StgClosure *)rbh->blocking_queue =
3626 evacuate((StgClosure *)rbh->blocking_queue);
3627 failed_to_evac = rtsTrue; // mutable anyhow.
3629 debugBelch("@@ scavenge: RBH %p (%s) (new blocking_queue link=%p)",
3630 p, info_type(p), (StgClosure *)rbh->blocking_queue));
3631 // ToDo: use size of reverted closure here!
3637 StgBlockedFetch *bf = (StgBlockedFetch *)p;
3638 // follow the pointer to the node which is being demanded
3639 (StgClosure *)bf->node =
3640 evacuate((StgClosure *)bf->node);
3641 // follow the link to the rest of the blocking queue
3642 (StgClosure *)bf->link =
3643 evacuate((StgClosure *)bf->link);
3645 debugBelch("@@ scavenge: %p (%s); node is now %p; exciting, isn't it",
3646 bf, info_type((StgClosure *)bf),
3647 bf->node, info_type(bf->node)));
3655 break; // nothing to do in this case
3659 StgFetchMeBlockingQueue *fmbq = (StgFetchMeBlockingQueue *)p;
3660 (StgClosure *)fmbq->blocking_queue =
3661 evacuate((StgClosure *)fmbq->blocking_queue);
3663 debugBelch("@@ scavenge: %p (%s) exciting, isn't it",
3664 p, info_type((StgClosure *)p)));
3669 case TVAR_WAIT_QUEUE:
3671 StgTVarWaitQueue *wq = ((StgTVarWaitQueue *) p);
3673 wq->waiting_tso = (StgTSO *)evacuate((StgClosure*)wq->waiting_tso);
3674 wq->next_queue_entry = (StgTVarWaitQueue *)evacuate((StgClosure*)wq->next_queue_entry);
3675 wq->prev_queue_entry = (StgTVarWaitQueue *)evacuate((StgClosure*)wq->prev_queue_entry);
3676 evac_gen = saved_evac_gen;
3677 failed_to_evac = rtsTrue; // mutable
3683 StgTVar *tvar = ((StgTVar *) p);
3685 tvar->current_value = evacuate((StgClosure*)tvar->current_value);
3686 tvar->first_wait_queue_entry = (StgTVarWaitQueue *)evacuate((StgClosure*)tvar->first_wait_queue_entry);
3688 tvar->last_update_by = (StgTRecHeader *)evacuate((StgClosure*)tvar->last_update_by);
3690 evac_gen = saved_evac_gen;
3691 failed_to_evac = rtsTrue; // mutable
3697 StgTRecHeader *trec = ((StgTRecHeader *) p);
3699 trec->enclosing_trec = (StgTRecHeader *)evacuate((StgClosure*)trec->enclosing_trec);
3700 trec->current_chunk = (StgTRecChunk *)evacuate((StgClosure*)trec->current_chunk);
3701 evac_gen = saved_evac_gen;
3702 failed_to_evac = rtsTrue; // mutable
3709 StgTRecChunk *tc = ((StgTRecChunk *) p);
3710 TRecEntry *e = &(tc -> entries[0]);
3712 tc->prev_chunk = (StgTRecChunk *)evacuate((StgClosure*)tc->prev_chunk);
3713 for (i = 0; i < tc -> next_entry_idx; i ++, e++ ) {
3714 e->tvar = (StgTVar *)evacuate((StgClosure*)e->tvar);
3715 e->expected_value = evacuate((StgClosure*)e->expected_value);
3716 e->new_value = evacuate((StgClosure*)e->new_value);
3718 evac_gen = saved_evac_gen;
3719 failed_to_evac = rtsTrue; // mutable
3724 case IND_OLDGEN_PERM:
3727 /* Careful here: a THUNK can be on the mutable list because
3728 * it contains pointers to young gen objects. If such a thunk
3729 * is updated, the IND_OLDGEN will be added to the mutable
3730 * list again, and we'll scavenge it twice. evacuate()
3731 * doesn't check whether the object has already been
3732 * evacuated, so we perform that check here.
3734 StgClosure *q = ((StgInd *)p)->indirectee;
3735 if (HEAP_ALLOCED(q) && Bdescr((StgPtr)q)->flags & BF_EVACUATED) {
3738 ((StgInd *)p)->indirectee = evacuate(q);
3741 #if 0 && defined(DEBUG)
3742 if (RtsFlags.DebugFlags.gc)
3743 /* Debugging code to print out the size of the thing we just
3747 StgPtr start = gen->steps[0].scan;
3748 bdescr *start_bd = gen->steps[0].scan_bd;
3750 scavenge(&gen->steps[0]);
3751 if (start_bd != gen->steps[0].scan_bd) {
3752 size += (P_)BLOCK_ROUND_UP(start) - start;
3753 start_bd = start_bd->link;
3754 while (start_bd != gen->steps[0].scan_bd) {
3755 size += BLOCK_SIZE_W;
3756 start_bd = start_bd->link;
3758 size += gen->steps[0].scan -
3759 (P_)BLOCK_ROUND_DOWN(gen->steps[0].scan);
3761 size = gen->steps[0].scan - start;
3763 debugBelch("evac IND_OLDGEN: %ld bytes", size * sizeof(W_));
3769 barf("scavenge_one: strange object %d", (int)(info->type));
3772 no_luck = failed_to_evac;
3773 failed_to_evac = rtsFalse;
3777 /* -----------------------------------------------------------------------------
3778 Scavenging mutable lists.
3780 We treat the mutable list of each generation > N (i.e. all the
3781 generations older than the one being collected) as roots. We also
3782 remove non-mutable objects from the mutable list at this point.
3783 -------------------------------------------------------------------------- */
3786 scavenge_mutable_list(generation *gen)
3791 bd = gen->saved_mut_list;
3794 for (; bd != NULL; bd = bd->link) {
3795 for (q = bd->start; q < bd->free; q++) {
3797 ASSERT(LOOKS_LIKE_CLOSURE_PTR(p));
3798 if (scavenge_one(p)) {
3799 /* didn't manage to promote everything, so put the
3800 * object back on the list.
3802 recordMutableGen((StgClosure *)p,gen);
3807 // free the old mut_list
3808 freeChain(gen->saved_mut_list);
3809 gen->saved_mut_list = NULL;
3814 scavenge_static(void)
3816 StgClosure* p = static_objects;
3817 const StgInfoTable *info;
3819 /* Always evacuate straight to the oldest generation for static
3821 evac_gen = oldest_gen->no;
3823 /* keep going until we've scavenged all the objects on the linked
3825 while (p != END_OF_STATIC_LIST) {
3827 ASSERT(LOOKS_LIKE_CLOSURE_PTR(p));
3830 if (info->type==RBH)
3831 info = REVERT_INFOPTR(info); // if it's an RBH, look at the orig closure
3833 // make sure the info pointer is into text space
3835 /* Take this object *off* the static_objects list,
3836 * and put it on the scavenged_static_objects list.
3838 static_objects = *STATIC_LINK(info,p);
3839 *STATIC_LINK(info,p) = scavenged_static_objects;
3840 scavenged_static_objects = p;
3842 switch (info -> type) {
3846 StgInd *ind = (StgInd *)p;
3847 ind->indirectee = evacuate(ind->indirectee);
3849 /* might fail to evacuate it, in which case we have to pop it
3850 * back on the mutable list of the oldest generation. We
3851 * leave it *on* the scavenged_static_objects list, though,
3852 * in case we visit this object again.
3854 if (failed_to_evac) {
3855 failed_to_evac = rtsFalse;
3856 recordMutableGen((StgClosure *)p,oldest_gen);
3862 scavenge_thunk_srt(info);
3866 scavenge_fun_srt(info);
3873 next = (P_)p->payload + info->layout.payload.ptrs;
3874 // evacuate the pointers
3875 for (q = (P_)p->payload; q < next; q++) {
3876 *q = (StgWord)(StgPtr)evacuate((StgClosure *)*q);
3882 barf("scavenge_static: strange closure %d", (int)(info->type));
3885 ASSERT(failed_to_evac == rtsFalse);
3887 /* get the next static object from the list. Remember, there might
3888 * be more stuff on this list now that we've done some evacuating!
3889 * (static_objects is a global)
3895 /* -----------------------------------------------------------------------------
3896 scavenge a chunk of memory described by a bitmap
3897 -------------------------------------------------------------------------- */
3900 scavenge_large_bitmap( StgPtr p, StgLargeBitmap *large_bitmap, nat size )
3906 bitmap = large_bitmap->bitmap[b];
3907 for (i = 0; i < size; ) {
3908 if ((bitmap & 1) == 0) {
3909 *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
3913 if (i % BITS_IN(W_) == 0) {
3915 bitmap = large_bitmap->bitmap[b];
3917 bitmap = bitmap >> 1;
3922 STATIC_INLINE StgPtr
3923 scavenge_small_bitmap (StgPtr p, nat size, StgWord bitmap)
3926 if ((bitmap & 1) == 0) {
3927 *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
3930 bitmap = bitmap >> 1;
3936 /* -----------------------------------------------------------------------------
3937 scavenge_stack walks over a section of stack and evacuates all the
3938 objects pointed to by it. We can use the same code for walking
3939 AP_STACK_UPDs, since these are just sections of copied stack.
3940 -------------------------------------------------------------------------- */
3944 scavenge_stack(StgPtr p, StgPtr stack_end)
3946 const StgRetInfoTable* info;
3950 //IF_DEBUG(sanity, debugBelch(" scavenging stack between %p and %p", p, stack_end));
3953 * Each time around this loop, we are looking at a chunk of stack
3954 * that starts with an activation record.
3957 while (p < stack_end) {
3958 info = get_ret_itbl((StgClosure *)p);
3960 switch (info->i.type) {
3963 ((StgUpdateFrame *)p)->updatee
3964 = evacuate(((StgUpdateFrame *)p)->updatee);
3965 p += sizeofW(StgUpdateFrame);
3968 // small bitmap (< 32 entries, or 64 on a 64-bit machine)
3969 case CATCH_STM_FRAME:
3970 case CATCH_RETRY_FRAME:
3971 case ATOMICALLY_FRAME:
3976 bitmap = BITMAP_BITS(info->i.layout.bitmap);
3977 size = BITMAP_SIZE(info->i.layout.bitmap);
3978 // NOTE: the payload starts immediately after the info-ptr, we
3979 // don't have an StgHeader in the same sense as a heap closure.
3981 p = scavenge_small_bitmap(p, size, bitmap);
3985 scavenge_srt((StgClosure **)GET_SRT(info), info->i.srt_bitmap);
3993 *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
3996 size = BCO_BITMAP_SIZE(bco);
3997 scavenge_large_bitmap(p, BCO_BITMAP(bco), size);
4002 // large bitmap (> 32 entries, or > 64 on a 64-bit machine)
4008 size = GET_LARGE_BITMAP(&info->i)->size;
4010 scavenge_large_bitmap(p, GET_LARGE_BITMAP(&info->i), size);
4012 // and don't forget to follow the SRT
4016 // Dynamic bitmap: the mask is stored on the stack, and
4017 // there are a number of non-pointers followed by a number
4018 // of pointers above the bitmapped area. (see StgMacros.h,
4023 dyn = ((StgRetDyn *)p)->liveness;
4025 // traverse the bitmap first
4026 bitmap = RET_DYN_LIVENESS(dyn);
4027 p = (P_)&((StgRetDyn *)p)->payload[0];
4028 size = RET_DYN_BITMAP_SIZE;
4029 p = scavenge_small_bitmap(p, size, bitmap);
4031 // skip over the non-ptr words
4032 p += RET_DYN_NONPTRS(dyn) + RET_DYN_NONPTR_REGS_SIZE;
4034 // follow the ptr words
4035 for (size = RET_DYN_PTRS(dyn); size > 0; size--) {
4036 *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
4044 StgRetFun *ret_fun = (StgRetFun *)p;
4045 StgFunInfoTable *fun_info;
4047 ret_fun->fun = evacuate(ret_fun->fun);
4048 fun_info = get_fun_itbl(ret_fun->fun);
4049 p = scavenge_arg_block(fun_info, ret_fun->payload);
4054 barf("scavenge_stack: weird activation record found on stack: %d", (int)(info->i.type));
4059 /*-----------------------------------------------------------------------------
4060 scavenge the large object list.
4062 evac_gen set by caller; similar games played with evac_gen as with
4063 scavenge() - see comment at the top of scavenge(). Most large
4064 objects are (repeatedly) mutable, so most of the time evac_gen will
4066 --------------------------------------------------------------------------- */
4069 scavenge_large(step *stp)
4074 bd = stp->new_large_objects;
4076 for (; bd != NULL; bd = stp->new_large_objects) {
4078 /* take this object *off* the large objects list and put it on
4079 * the scavenged large objects list. This is so that we can
4080 * treat new_large_objects as a stack and push new objects on
4081 * the front when evacuating.
4083 stp->new_large_objects = bd->link;
4084 dbl_link_onto(bd, &stp->scavenged_large_objects);
4086 // update the block count in this step.
4087 stp->n_scavenged_large_blocks += bd->blocks;
4090 if (scavenge_one(p)) {
4091 if (stp->gen_no > 0) {
4092 recordMutableGen((StgClosure *)p, stp->gen);
4098 /* -----------------------------------------------------------------------------
4099 Initialising the static object & mutable lists
4100 -------------------------------------------------------------------------- */
4103 zero_static_object_list(StgClosure* first_static)
4107 const StgInfoTable *info;
4109 for (p = first_static; p != END_OF_STATIC_LIST; p = link) {
4111 link = *STATIC_LINK(info, p);
4112 *STATIC_LINK(info,p) = NULL;
4116 /* -----------------------------------------------------------------------------
4118 -------------------------------------------------------------------------- */
4125 for (c = (StgIndStatic *)revertible_caf_list; c != NULL;
4126 c = (StgIndStatic *)c->static_link)
4128 SET_INFO(c, c->saved_info);
4129 c->saved_info = NULL;
4130 // could, but not necessary: c->static_link = NULL;
4132 revertible_caf_list = NULL;
4136 markCAFs( evac_fn evac )
4140 for (c = (StgIndStatic *)caf_list; c != NULL;
4141 c = (StgIndStatic *)c->static_link)
4143 evac(&c->indirectee);
4145 for (c = (StgIndStatic *)revertible_caf_list; c != NULL;
4146 c = (StgIndStatic *)c->static_link)
4148 evac(&c->indirectee);
4152 /* -----------------------------------------------------------------------------
4153 Sanity code for CAF garbage collection.
4155 With DEBUG turned on, we manage a CAF list in addition to the SRT
4156 mechanism. After GC, we run down the CAF list and blackhole any
4157 CAFs which have been garbage collected. This means we get an error
4158 whenever the program tries to enter a garbage collected CAF.
4160 Any garbage collected CAFs are taken off the CAF list at the same
4162 -------------------------------------------------------------------------- */
4164 #if 0 && defined(DEBUG)
4171 const StgInfoTable *info;
4182 ASSERT(info->type == IND_STATIC);
4184 if (STATIC_LINK(info,p) == NULL) {
4185 IF_DEBUG(gccafs, debugBelch("CAF gc'd at 0x%04lx", (long)p));
4187 SET_INFO(p,&stg_BLACKHOLE_info);
4188 p = STATIC_LINK2(info,p);
4192 pp = &STATIC_LINK2(info,p);
4199 // debugBelch("%d CAFs live", i);
4204 /* -----------------------------------------------------------------------------
4207 Whenever a thread returns to the scheduler after possibly doing
4208 some work, we have to run down the stack and black-hole all the
4209 closures referred to by update frames.
4210 -------------------------------------------------------------------------- */
4213 threadLazyBlackHole(StgTSO *tso)
4216 StgRetInfoTable *info;
4220 stack_end = &tso->stack[tso->stack_size];
4222 frame = (StgClosure *)tso->sp;
4225 info = get_ret_itbl(frame);
4227 switch (info->i.type) {
4230 bh = ((StgUpdateFrame *)frame)->updatee;
4232 /* if the thunk is already blackholed, it means we've also
4233 * already blackholed the rest of the thunks on this stack,
4234 * so we can stop early.
4236 * The blackhole made for a CAF is a CAF_BLACKHOLE, so they
4237 * don't interfere with this optimisation.
4239 if (bh->header.info == &stg_BLACKHOLE_info) {
4243 if (bh->header.info != &stg_CAF_BLACKHOLE_info) {
4244 #if (!defined(LAZY_BLACKHOLING)) && defined(DEBUG)
4245 debugBelch("Unexpected lazy BHing required at 0x%04x\n",(int)bh);
4249 // We pretend that bh is now dead.
4250 LDV_recordDead_FILL_SLOP_DYNAMIC((StgClosure *)bh);
4252 SET_INFO(bh,&stg_BLACKHOLE_info);
4254 // We pretend that bh has just been created.
4255 LDV_RECORD_CREATE(bh);
4258 frame = (StgClosure *) ((StgUpdateFrame *)frame + 1);
4264 // normal stack frames; do nothing except advance the pointer
4266 frame = (StgClosure *)((StgPtr)frame + stack_frame_sizeW(frame));
4272 /* -----------------------------------------------------------------------------
4275 * Code largely pinched from old RTS, then hacked to bits. We also do
4276 * lazy black holing here.
4278 * -------------------------------------------------------------------------- */
4280 struct stack_gap { StgWord gap_size; struct stack_gap *next_gap; };
4283 threadSqueezeStack(StgTSO *tso)
4286 rtsBool prev_was_update_frame;
4287 StgClosure *updatee = NULL;
4289 StgRetInfoTable *info;
4290 StgWord current_gap_size;
4291 struct stack_gap *gap;
4294 // Traverse the stack upwards, replacing adjacent update frames
4295 // with a single update frame and a "stack gap". A stack gap
4296 // contains two values: the size of the gap, and the distance
4297 // to the next gap (or the stack top).
4299 bottom = &(tso->stack[tso->stack_size]);
4303 ASSERT(frame < bottom);
4305 prev_was_update_frame = rtsFalse;
4306 current_gap_size = 0;
4307 gap = (struct stack_gap *) (tso->sp - sizeofW(StgUpdateFrame));
4309 while (frame < bottom) {
4311 info = get_ret_itbl((StgClosure *)frame);
4312 switch (info->i.type) {
4316 StgUpdateFrame *upd = (StgUpdateFrame *)frame;
4318 if (upd->updatee->header.info == &stg_BLACKHOLE_info) {
4320 // found a BLACKHOLE'd update frame; we've been here
4321 // before, in a previous GC, so just break out.
4323 // Mark the end of the gap, if we're in one.
4324 if (current_gap_size != 0) {
4325 gap = (struct stack_gap *)(frame-sizeofW(StgUpdateFrame));
4328 frame += sizeofW(StgUpdateFrame);
4329 goto done_traversing;
4332 if (prev_was_update_frame) {
4334 TICK_UPD_SQUEEZED();
4335 /* wasn't there something about update squeezing and ticky to be
4336 * sorted out? oh yes: we aren't counting each enter properly
4337 * in this case. See the log somewhere. KSW 1999-04-21
4339 * Check two things: that the two update frames don't point to
4340 * the same object, and that the updatee_bypass isn't already an
4341 * indirection. Both of these cases only happen when we're in a
4342 * block hole-style loop (and there are multiple update frames
4343 * on the stack pointing to the same closure), but they can both
4344 * screw us up if we don't check.
4346 if (upd->updatee != updatee && !closure_IND(upd->updatee)) {
4347 UPD_IND_NOLOCK(upd->updatee, updatee);
4350 // now mark this update frame as a stack gap. The gap
4351 // marker resides in the bottom-most update frame of
4352 // the series of adjacent frames, and covers all the
4353 // frames in this series.
4354 current_gap_size += sizeofW(StgUpdateFrame);
4355 ((struct stack_gap *)frame)->gap_size = current_gap_size;
4356 ((struct stack_gap *)frame)->next_gap = gap;
4358 frame += sizeofW(StgUpdateFrame);
4362 // single update frame, or the topmost update frame in a series
4364 StgClosure *bh = upd->updatee;
4366 // Do lazy black-holing
4367 if (bh->header.info != &stg_BLACKHOLE_info &&
4368 bh->header.info != &stg_CAF_BLACKHOLE_info) {
4369 #if (!defined(LAZY_BLACKHOLING)) && defined(DEBUG)
4370 debugBelch("Unexpected lazy BHing required at 0x%04x",(int)bh);
4373 // zero out the slop so that the sanity checker can tell
4374 // where the next closure is.
4375 DEBUG_FILL_SLOP(bh);
4378 // We pretend that bh is now dead.
4379 // ToDo: is the slop filling the same as DEBUG_FILL_SLOP?
4380 LDV_recordDead_FILL_SLOP_DYNAMIC((StgClosure *)bh);
4382 // Todo: maybe use SET_HDR() and remove LDV_RECORD_CREATE()?
4383 SET_INFO(bh,&stg_BLACKHOLE_info);
4385 // We pretend that bh has just been created.
4386 LDV_RECORD_CREATE(bh);
4389 prev_was_update_frame = rtsTrue;
4390 updatee = upd->updatee;
4391 frame += sizeofW(StgUpdateFrame);
4397 prev_was_update_frame = rtsFalse;
4399 // we're not in a gap... check whether this is the end of a gap
4400 // (an update frame can't be the end of a gap).
4401 if (current_gap_size != 0) {
4402 gap = (struct stack_gap *) (frame - sizeofW(StgUpdateFrame));
4404 current_gap_size = 0;
4406 frame += stack_frame_sizeW((StgClosure *)frame);
4413 // Now we have a stack with gaps in it, and we have to walk down
4414 // shoving the stack up to fill in the gaps. A diagram might
4418 // | ********* | <- sp
4422 // | stack_gap | <- gap | chunk_size
4424 // | ......... | <- gap_end v
4430 // 'sp' points the the current top-of-stack
4431 // 'gap' points to the stack_gap structure inside the gap
4432 // ***** indicates real stack data
4433 // ..... indicates gap
4434 // <empty> indicates unused
4438 void *gap_start, *next_gap_start, *gap_end;
4441 next_gap_start = (void *)((unsigned char*)gap + sizeof(StgUpdateFrame));
4442 sp = next_gap_start;
4444 while ((StgPtr)gap > tso->sp) {
4446 // we're working in *bytes* now...
4447 gap_start = next_gap_start;
4448 gap_end = (void*) ((unsigned char*)gap_start - gap->gap_size * sizeof(W_));
4450 gap = gap->next_gap;
4451 next_gap_start = (void *)((unsigned char*)gap + sizeof(StgUpdateFrame));
4453 chunk_size = (unsigned char*)gap_end - (unsigned char*)next_gap_start;
4455 memmove(sp, next_gap_start, chunk_size);
4458 tso->sp = (StgPtr)sp;
4462 /* -----------------------------------------------------------------------------
4465 * We have to prepare for GC - this means doing lazy black holing
4466 * here. We also take the opportunity to do stack squeezing if it's
4468 * -------------------------------------------------------------------------- */
4470 threadPaused(StgTSO *tso)
4472 if ( RtsFlags.GcFlags.squeezeUpdFrames == rtsTrue )
4473 threadSqueezeStack(tso); // does black holing too
4475 threadLazyBlackHole(tso);
4478 /* -----------------------------------------------------------------------------
4480 * -------------------------------------------------------------------------- */
4484 printMutableList(generation *gen)
4489 debugBelch("@@ Mutable list %p: ", gen->mut_list);
4491 for (bd = gen->mut_list; bd != NULL; bd = bd->link) {
4492 for (p = bd->start; p < bd->free; p++) {
4493 debugBelch("%p (%s), ", (void *)*p, info_type((StgClosure *)*p));