1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team 1998-2003
5 * Generational garbage collector
7 * ---------------------------------------------------------------------------*/
9 #include "PosixSource.h"
15 #include "LdvProfile.h"
20 #include "BlockAlloc.h"
26 #include "ParTicky.h" // ToDo: move into Rts.h
27 #include "GCCompact.h"
30 #if defined(GRAN) || defined(PAR)
31 # include "GranSimRts.h"
32 # include "ParallelRts.h"
36 # include "ParallelDebug.h"
41 #if defined(RTS_GTK_FRONTPANEL)
42 #include "FrontPanel.h"
45 #include "RetainerProfile.h"
49 /* STATIC OBJECT LIST.
52 * We maintain a linked list of static objects that are still live.
53 * The requirements for this list are:
55 * - we need to scan the list while adding to it, in order to
56 * scavenge all the static objects (in the same way that
57 * breadth-first scavenging works for dynamic objects).
59 * - we need to be able to tell whether an object is already on
60 * the list, to break loops.
62 * Each static object has a "static link field", which we use for
63 * linking objects on to the list. We use a stack-type list, consing
64 * objects on the front as they are added (this means that the
65 * scavenge phase is depth-first, not breadth-first, but that
68 * A separate list is kept for objects that have been scavenged
69 * already - this is so that we can zero all the marks afterwards.
71 * An object is on the list if its static link field is non-zero; this
72 * means that we have to mark the end of the list with '1', not NULL.
74 * Extra notes for generational GC:
76 * Each generation has a static object list associated with it. When
77 * collecting generations up to N, we treat the static object lists
78 * from generations > N as roots.
80 * We build up a static object list while collecting generations 0..N,
81 * which is then appended to the static object list of generation N+1.
83 static StgClosure* static_objects; // live static objects
84 StgClosure* scavenged_static_objects; // static objects scavenged so far
86 /* N is the oldest generation being collected, where the generations
87 * are numbered starting at 0. A major GC (indicated by the major_gc
88 * flag) is when we're collecting all generations. We only attempt to
89 * deal with static objects and GC CAFs when doing a major GC.
92 static rtsBool major_gc;
94 /* Youngest generation that objects should be evacuated to in
95 * evacuate(). (Logically an argument to evacuate, but it's static
96 * a lot of the time so we optimise it into a global variable).
102 StgWeak *old_weak_ptr_list; // also pending finaliser list
104 /* Which stage of processing various kinds of weak pointer are we at?
105 * (see traverse_weak_ptr_list() below for discussion).
107 typedef enum { WeakPtrs, WeakThreads, WeakDone } WeakStage;
108 static WeakStage weak_stage;
110 /* List of all threads during GC
112 static StgTSO *old_all_threads;
113 StgTSO *resurrected_threads;
115 /* Flag indicating failure to evacuate an object to the desired
118 static rtsBool failed_to_evac;
120 /* Old to-space (used for two-space collector only)
122 static bdescr *old_to_blocks;
124 /* Data used for allocation area sizing.
126 static lnat new_blocks; // blocks allocated during this GC
127 static lnat g0s0_pcnt_kept = 30; // percentage of g0s0 live at last minor GC
129 /* Used to avoid long recursion due to selector thunks
131 static lnat thunk_selector_depth = 0;
132 #define MAX_THUNK_SELECTOR_DEPTH 8
134 /* -----------------------------------------------------------------------------
135 Static function declarations
136 -------------------------------------------------------------------------- */
138 static bdescr * gc_alloc_block ( step *stp );
139 static void mark_root ( StgClosure **root );
141 // Use a register argument for evacuate, if available.
143 #define REGPARM1 __attribute__((regparm(1)))
148 REGPARM1 static StgClosure * evacuate (StgClosure *q);
150 static void zero_static_object_list ( StgClosure* first_static );
152 static rtsBool traverse_weak_ptr_list ( void );
153 static void mark_weak_ptr_list ( StgWeak **list );
155 static StgClosure * eval_thunk_selector ( nat field, StgSelector * p );
158 static void scavenge ( step * );
159 static void scavenge_mark_stack ( void );
160 static void scavenge_stack ( StgPtr p, StgPtr stack_end );
161 static rtsBool scavenge_one ( StgPtr p );
162 static void scavenge_large ( step * );
163 static void scavenge_static ( void );
164 static void scavenge_mutable_list ( generation *g );
166 static void scavenge_large_bitmap ( StgPtr p,
167 StgLargeBitmap *large_bitmap,
170 #if 0 && defined(DEBUG)
171 static void gcCAFs ( void );
174 /* -----------------------------------------------------------------------------
175 inline functions etc. for dealing with the mark bitmap & stack.
176 -------------------------------------------------------------------------- */
178 #define MARK_STACK_BLOCKS 4
180 static bdescr *mark_stack_bdescr;
181 static StgPtr *mark_stack;
182 static StgPtr *mark_sp;
183 static StgPtr *mark_splim;
185 // Flag and pointers used for falling back to a linear scan when the
186 // mark stack overflows.
187 static rtsBool mark_stack_overflowed;
188 static bdescr *oldgen_scan_bd;
189 static StgPtr oldgen_scan;
191 STATIC_INLINE rtsBool
192 mark_stack_empty(void)
194 return mark_sp == mark_stack;
197 STATIC_INLINE rtsBool
198 mark_stack_full(void)
200 return mark_sp >= mark_splim;
204 reset_mark_stack(void)
206 mark_sp = mark_stack;
210 push_mark_stack(StgPtr p)
221 /* -----------------------------------------------------------------------------
222 Allocate a new to-space block in the given step.
223 -------------------------------------------------------------------------- */
226 gc_alloc_block(step *stp)
228 bdescr *bd = allocBlock();
229 bd->gen_no = stp->gen_no;
233 // blocks in to-space in generations up to and including N
234 // get the BF_EVACUATED flag.
235 if (stp->gen_no <= N) {
236 bd->flags = BF_EVACUATED;
241 // Start a new to-space block, chain it on after the previous one.
242 if (stp->hp_bd == NULL) {
245 stp->hp_bd->free = stp->hp;
246 stp->hp_bd->link = bd;
251 stp->hpLim = stp->hp + BLOCK_SIZE_W;
259 /* -----------------------------------------------------------------------------
262 Rough outline of the algorithm: for garbage collecting generation N
263 (and all younger generations):
265 - follow all pointers in the root set. the root set includes all
266 mutable objects in all generations (mutable_list).
268 - for each pointer, evacuate the object it points to into either
270 + to-space of the step given by step->to, which is the next
271 highest step in this generation or the first step in the next
272 generation if this is the last step.
274 + to-space of generations[evac_gen]->steps[0], if evac_gen != 0.
275 When we evacuate an object we attempt to evacuate
276 everything it points to into the same generation - this is
277 achieved by setting evac_gen to the desired generation. If
278 we can't do this, then an entry in the mut list has to
279 be made for the cross-generation pointer.
281 + if the object is already in a generation > N, then leave
284 - repeatedly scavenge to-space from each step in each generation
285 being collected until no more objects can be evacuated.
287 - free from-space in each step, and set from-space = to-space.
289 Locks held: sched_mutex
291 -------------------------------------------------------------------------- */
294 GarbageCollect ( void (*get_roots)(evac_fn), rtsBool force_major_gc )
298 lnat live, allocated, collected = 0, copied = 0;
299 lnat oldgen_saved_blocks = 0;
303 CostCentreStack *prev_CCS;
306 #if defined(DEBUG) && defined(GRAN)
307 IF_DEBUG(gc, debugBelch("@@ Starting garbage collection at %ld (%lx)\n",
311 #if defined(RTS_USER_SIGNALS)
316 // tell the STM to discard any cached closures its hoping to re-use
319 // tell the stats department that we've started a GC
322 // Init stats and print par specific (timing) info
323 PAR_TICKY_PAR_START();
325 // attribute any costs to CCS_GC
331 /* Approximate how much we allocated.
332 * Todo: only when generating stats?
334 allocated = calcAllocated();
336 /* Figure out which generation to collect
338 if (force_major_gc) {
339 N = RtsFlags.GcFlags.generations - 1;
343 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
344 if (generations[g].steps[0].n_blocks +
345 generations[g].steps[0].n_large_blocks
346 >= generations[g].max_blocks) {
350 major_gc = (N == RtsFlags.GcFlags.generations-1);
353 #ifdef RTS_GTK_FRONTPANEL
354 if (RtsFlags.GcFlags.frontpanel) {
355 updateFrontPanelBeforeGC(N);
359 // check stack sanity *before* GC (ToDo: check all threads)
361 // ToDo!: check sanity IF_DEBUG(sanity, checkTSOsSanity());
363 IF_DEBUG(sanity, checkFreeListSanity());
365 /* Initialise the static object lists
367 static_objects = END_OF_STATIC_LIST;
368 scavenged_static_objects = END_OF_STATIC_LIST;
370 /* Save the old to-space if we're doing a two-space collection
372 if (RtsFlags.GcFlags.generations == 1) {
373 old_to_blocks = g0s0->to_blocks;
374 g0s0->to_blocks = NULL;
375 g0s0->n_to_blocks = 0;
378 /* Keep a count of how many new blocks we allocated during this GC
379 * (used for resizing the allocation area, later).
383 // Initialise to-space in all the generations/steps that we're
386 for (g = 0; g <= N; g++) {
388 // throw away the mutable list. Invariant: the mutable list
389 // always has at least one block; this means we can avoid a check for
390 // NULL in recordMutable().
392 freeChain(generations[g].mut_list);
393 generations[g].mut_list = allocBlock();
396 for (s = 0; s < generations[g].n_steps; s++) {
398 // generation 0, step 0 doesn't need to-space
399 if (g == 0 && s == 0 && RtsFlags.GcFlags.generations > 1) {
403 stp = &generations[g].steps[s];
404 ASSERT(stp->gen_no == g);
406 // start a new to-space for this step.
409 stp->to_blocks = NULL;
411 // allocate the first to-space block; extra blocks will be
412 // chained on as necessary.
413 bd = gc_alloc_block(stp);
415 stp->scan = bd->start;
418 // initialise the large object queues.
419 stp->new_large_objects = NULL;
420 stp->scavenged_large_objects = NULL;
421 stp->n_scavenged_large_blocks = 0;
423 // mark the large objects as not evacuated yet
424 for (bd = stp->large_objects; bd; bd = bd->link) {
425 bd->flags &= ~BF_EVACUATED;
428 // for a compacted step, we need to allocate the bitmap
429 if (stp->is_compacted) {
430 nat bitmap_size; // in bytes
431 bdescr *bitmap_bdescr;
434 bitmap_size = stp->n_blocks * BLOCK_SIZE / (sizeof(W_)*BITS_PER_BYTE);
436 if (bitmap_size > 0) {
437 bitmap_bdescr = allocGroup((nat)BLOCK_ROUND_UP(bitmap_size)
439 stp->bitmap = bitmap_bdescr;
440 bitmap = bitmap_bdescr->start;
442 IF_DEBUG(gc, debugBelch("bitmap_size: %d, bitmap: %p",
443 bitmap_size, bitmap););
445 // don't forget to fill it with zeros!
446 memset(bitmap, 0, bitmap_size);
448 // For each block in this step, point to its bitmap from the
450 for (bd=stp->blocks; bd != NULL; bd = bd->link) {
451 bd->u.bitmap = bitmap;
452 bitmap += BLOCK_SIZE_W / (sizeof(W_)*BITS_PER_BYTE);
454 // Also at this point we set the BF_COMPACTED flag
455 // for this block. The invariant is that
456 // BF_COMPACTED is always unset, except during GC
457 // when it is set on those blocks which will be
459 bd->flags |= BF_COMPACTED;
466 /* make sure the older generations have at least one block to
467 * allocate into (this makes things easier for copy(), see below).
469 for (g = N+1; g < RtsFlags.GcFlags.generations; g++) {
470 for (s = 0; s < generations[g].n_steps; s++) {
471 stp = &generations[g].steps[s];
472 if (stp->hp_bd == NULL) {
473 ASSERT(stp->blocks == NULL);
474 bd = gc_alloc_block(stp);
478 /* Set the scan pointer for older generations: remember we
479 * still have to scavenge objects that have been promoted. */
481 stp->scan_bd = stp->hp_bd;
482 stp->to_blocks = NULL;
483 stp->n_to_blocks = 0;
484 stp->new_large_objects = NULL;
485 stp->scavenged_large_objects = NULL;
486 stp->n_scavenged_large_blocks = 0;
490 /* Allocate a mark stack if we're doing a major collection.
493 mark_stack_bdescr = allocGroup(MARK_STACK_BLOCKS);
494 mark_stack = (StgPtr *)mark_stack_bdescr->start;
495 mark_sp = mark_stack;
496 mark_splim = mark_stack + (MARK_STACK_BLOCKS * BLOCK_SIZE_W);
498 mark_stack_bdescr = NULL;
501 /* -----------------------------------------------------------------------
502 * follow all the roots that we know about:
503 * - mutable lists from each generation > N
504 * we want to *scavenge* these roots, not evacuate them: they're not
505 * going to move in this GC.
506 * Also: do them in reverse generation order. This is because we
507 * often want to promote objects that are pointed to by older
508 * generations early, so we don't have to repeatedly copy them.
509 * Doing the generations in reverse order ensures that we don't end
510 * up in the situation where we want to evac an object to gen 3 and
511 * it has already been evaced to gen 2.
515 for (g = RtsFlags.GcFlags.generations-1; g > N; g--) {
516 generations[g].saved_mut_list = generations[g].mut_list;
517 generations[g].mut_list = allocBlock();
518 // mut_list always has at least one block.
521 for (g = RtsFlags.GcFlags.generations-1; g > N; g--) {
522 IF_PAR_DEBUG(verbose, printMutableList(&generations[g]));
523 scavenge_mutable_list(&generations[g]);
525 for (st = generations[g].n_steps-1; st >= 0; st--) {
526 scavenge(&generations[g].steps[st]);
531 /* follow roots from the CAF list (used by GHCi)
536 /* follow all the roots that the application knows about.
539 get_roots(mark_root);
542 /* And don't forget to mark the TSO if we got here direct from
544 /* Not needed in a seq version?
546 CurrentTSO = (StgTSO *)MarkRoot((StgClosure *)CurrentTSO);
550 // Mark the entries in the GALA table of the parallel system
551 markLocalGAs(major_gc);
552 // Mark all entries on the list of pending fetches
553 markPendingFetches(major_gc);
556 /* Mark the weak pointer list, and prepare to detect dead weak
559 mark_weak_ptr_list(&weak_ptr_list);
560 old_weak_ptr_list = weak_ptr_list;
561 weak_ptr_list = NULL;
562 weak_stage = WeakPtrs;
564 /* The all_threads list is like the weak_ptr_list.
565 * See traverse_weak_ptr_list() for the details.
567 old_all_threads = all_threads;
568 all_threads = END_TSO_QUEUE;
569 resurrected_threads = END_TSO_QUEUE;
571 /* Mark the stable pointer table.
573 markStablePtrTable(mark_root);
575 /* -------------------------------------------------------------------------
576 * Repeatedly scavenge all the areas we know about until there's no
577 * more scavenging to be done.
584 // scavenge static objects
585 if (major_gc && static_objects != END_OF_STATIC_LIST) {
586 IF_DEBUG(sanity, checkStaticObjects(static_objects));
590 /* When scavenging the older generations: Objects may have been
591 * evacuated from generations <= N into older generations, and we
592 * need to scavenge these objects. We're going to try to ensure that
593 * any evacuations that occur move the objects into at least the
594 * same generation as the object being scavenged, otherwise we
595 * have to create new entries on the mutable list for the older
599 // scavenge each step in generations 0..maxgen
605 // scavenge objects in compacted generation
606 if (mark_stack_overflowed || oldgen_scan_bd != NULL ||
607 (mark_stack_bdescr != NULL && !mark_stack_empty())) {
608 scavenge_mark_stack();
612 for (gen = RtsFlags.GcFlags.generations; --gen >= 0; ) {
613 for (st = generations[gen].n_steps; --st >= 0; ) {
614 if (gen == 0 && st == 0 && RtsFlags.GcFlags.generations > 1) {
617 stp = &generations[gen].steps[st];
619 if (stp->hp_bd != stp->scan_bd || stp->scan < stp->hp) {
624 if (stp->new_large_objects != NULL) {
633 if (flag) { goto loop; }
635 // must be last... invariant is that everything is fully
636 // scavenged at this point.
637 if (traverse_weak_ptr_list()) { // returns rtsTrue if evaced something
642 /* Update the pointers from the "main thread" list - these are
643 * treated as weak pointers because we want to allow a main thread
644 * to get a BlockedOnDeadMVar exception in the same way as any other
645 * thread. Note that the threads should all have been retained by
646 * GC by virtue of being on the all_threads list, we're just
647 * updating pointers here.
652 for (m = main_threads; m != NULL; m = m->link) {
653 tso = (StgTSO *) isAlive((StgClosure *)m->tso);
655 barf("main thread has been GC'd");
662 // Reconstruct the Global Address tables used in GUM
663 rebuildGAtables(major_gc);
664 IF_DEBUG(sanity, checkLAGAtable(rtsTrue/*check closures, too*/));
667 // Now see which stable names are still alive.
670 // Tidy the end of the to-space chains
671 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
672 for (s = 0; s < generations[g].n_steps; s++) {
673 stp = &generations[g].steps[s];
674 if (!(g == 0 && s == 0 && RtsFlags.GcFlags.generations > 1)) {
675 ASSERT(Bdescr(stp->hp) == stp->hp_bd);
676 stp->hp_bd->free = stp->hp;
682 // We call processHeapClosureForDead() on every closure destroyed during
683 // the current garbage collection, so we invoke LdvCensusForDead().
684 if (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_LDV
685 || RtsFlags.ProfFlags.bioSelector != NULL)
689 // NO MORE EVACUATION AFTER THIS POINT!
690 // Finally: compaction of the oldest generation.
691 if (major_gc && oldest_gen->steps[0].is_compacted) {
692 // save number of blocks for stats
693 oldgen_saved_blocks = oldest_gen->steps[0].n_blocks;
697 IF_DEBUG(sanity, checkGlobalTSOList(rtsFalse));
699 /* run through all the generations/steps and tidy up
701 copied = new_blocks * BLOCK_SIZE_W;
702 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
705 generations[g].collections++; // for stats
708 // Count the mutable list as bytes "copied" for the purposes of
709 // stats. Every mutable list is copied during every GC.
711 for (bd = generations[g].mut_list; bd != NULL; bd = bd->link) {
712 copied += (bd->free - bd->start) * sizeof(StgWord);
716 for (s = 0; s < generations[g].n_steps; s++) {
718 stp = &generations[g].steps[s];
720 if (!(g == 0 && s == 0 && RtsFlags.GcFlags.generations > 1)) {
721 // stats information: how much we copied
723 copied -= stp->hp_bd->start + BLOCK_SIZE_W -
728 // for generations we collected...
731 // rough calculation of garbage collected, for stats output
732 if (stp->is_compacted) {
733 collected += (oldgen_saved_blocks - stp->n_blocks) * BLOCK_SIZE_W;
735 collected += stp->n_blocks * BLOCK_SIZE_W;
738 /* free old memory and shift to-space into from-space for all
739 * the collected steps (except the allocation area). These
740 * freed blocks will probaby be quickly recycled.
742 if (!(g == 0 && s == 0)) {
743 if (stp->is_compacted) {
744 // for a compacted step, just shift the new to-space
745 // onto the front of the now-compacted existing blocks.
746 for (bd = stp->to_blocks; bd != NULL; bd = bd->link) {
747 bd->flags &= ~BF_EVACUATED; // now from-space
749 // tack the new blocks on the end of the existing blocks
750 if (stp->blocks == NULL) {
751 stp->blocks = stp->to_blocks;
753 for (bd = stp->blocks; bd != NULL; bd = next) {
756 bd->link = stp->to_blocks;
758 // NB. this step might not be compacted next
759 // time, so reset the BF_COMPACTED flags.
760 // They are set before GC if we're going to
761 // compact. (search for BF_COMPACTED above).
762 bd->flags &= ~BF_COMPACTED;
765 // add the new blocks to the block tally
766 stp->n_blocks += stp->n_to_blocks;
768 freeChain(stp->blocks);
769 stp->blocks = stp->to_blocks;
770 stp->n_blocks = stp->n_to_blocks;
771 for (bd = stp->blocks; bd != NULL; bd = bd->link) {
772 bd->flags &= ~BF_EVACUATED; // now from-space
775 stp->to_blocks = NULL;
776 stp->n_to_blocks = 0;
779 /* LARGE OBJECTS. The current live large objects are chained on
780 * scavenged_large, having been moved during garbage
781 * collection from large_objects. Any objects left on
782 * large_objects list are therefore dead, so we free them here.
784 for (bd = stp->large_objects; bd != NULL; bd = next) {
790 // update the count of blocks used by large objects
791 for (bd = stp->scavenged_large_objects; bd != NULL; bd = bd->link) {
792 bd->flags &= ~BF_EVACUATED;
794 stp->large_objects = stp->scavenged_large_objects;
795 stp->n_large_blocks = stp->n_scavenged_large_blocks;
798 // for older generations...
800 /* For older generations, we need to append the
801 * scavenged_large_object list (i.e. large objects that have been
802 * promoted during this GC) to the large_object list for that step.
804 for (bd = stp->scavenged_large_objects; bd; bd = next) {
806 bd->flags &= ~BF_EVACUATED;
807 dbl_link_onto(bd, &stp->large_objects);
810 // add the new blocks we promoted during this GC
811 stp->n_blocks += stp->n_to_blocks;
812 stp->n_to_blocks = 0;
813 stp->n_large_blocks += stp->n_scavenged_large_blocks;
818 /* Reset the sizes of the older generations when we do a major
821 * CURRENT STRATEGY: make all generations except zero the same size.
822 * We have to stay within the maximum heap size, and leave a certain
823 * percentage of the maximum heap size available to allocate into.
825 if (major_gc && RtsFlags.GcFlags.generations > 1) {
826 nat live, size, min_alloc;
827 nat max = RtsFlags.GcFlags.maxHeapSize;
828 nat gens = RtsFlags.GcFlags.generations;
830 // live in the oldest generations
831 live = oldest_gen->steps[0].n_blocks +
832 oldest_gen->steps[0].n_large_blocks;
834 // default max size for all generations except zero
835 size = stg_max(live * RtsFlags.GcFlags.oldGenFactor,
836 RtsFlags.GcFlags.minOldGenSize);
838 // minimum size for generation zero
839 min_alloc = stg_max((RtsFlags.GcFlags.pcFreeHeap * max) / 200,
840 RtsFlags.GcFlags.minAllocAreaSize);
842 // Auto-enable compaction when the residency reaches a
843 // certain percentage of the maximum heap size (default: 30%).
844 if (RtsFlags.GcFlags.generations > 1 &&
845 (RtsFlags.GcFlags.compact ||
847 oldest_gen->steps[0].n_blocks >
848 (RtsFlags.GcFlags.compactThreshold * max) / 100))) {
849 oldest_gen->steps[0].is_compacted = 1;
850 // debugBelch("compaction: on\n", live);
852 oldest_gen->steps[0].is_compacted = 0;
853 // debugBelch("compaction: off\n", live);
856 // if we're going to go over the maximum heap size, reduce the
857 // size of the generations accordingly. The calculation is
858 // different if compaction is turned on, because we don't need
859 // to double the space required to collect the old generation.
862 // this test is necessary to ensure that the calculations
863 // below don't have any negative results - we're working
864 // with unsigned values here.
865 if (max < min_alloc) {
869 if (oldest_gen->steps[0].is_compacted) {
870 if ( (size + (size - 1) * (gens - 2) * 2) + min_alloc > max ) {
871 size = (max - min_alloc) / ((gens - 1) * 2 - 1);
874 if ( (size * (gens - 1) * 2) + min_alloc > max ) {
875 size = (max - min_alloc) / ((gens - 1) * 2);
885 debugBelch("live: %d, min_alloc: %d, size : %d, max = %d\n", live,
886 min_alloc, size, max);
889 for (g = 0; g < gens; g++) {
890 generations[g].max_blocks = size;
894 // Guess the amount of live data for stats.
897 /* Free the small objects allocated via allocate(), since this will
898 * all have been copied into G0S1 now.
900 if (small_alloc_list != NULL) {
901 freeChain(small_alloc_list);
903 small_alloc_list = NULL;
907 alloc_blocks_lim = RtsFlags.GcFlags.minAllocAreaSize;
909 // Start a new pinned_object_block
910 pinned_object_block = NULL;
912 /* Free the mark stack.
914 if (mark_stack_bdescr != NULL) {
915 freeGroup(mark_stack_bdescr);
920 for (g = 0; g <= N; g++) {
921 for (s = 0; s < generations[g].n_steps; s++) {
922 stp = &generations[g].steps[s];
923 if (stp->is_compacted && stp->bitmap != NULL) {
924 freeGroup(stp->bitmap);
929 /* Two-space collector:
930 * Free the old to-space, and estimate the amount of live data.
932 if (RtsFlags.GcFlags.generations == 1) {
935 if (old_to_blocks != NULL) {
936 freeChain(old_to_blocks);
938 for (bd = g0s0->to_blocks; bd != NULL; bd = bd->link) {
939 bd->flags = 0; // now from-space
942 /* For a two-space collector, we need to resize the nursery. */
944 /* set up a new nursery. Allocate a nursery size based on a
945 * function of the amount of live data (by default a factor of 2)
946 * Use the blocks from the old nursery if possible, freeing up any
949 * If we get near the maximum heap size, then adjust our nursery
950 * size accordingly. If the nursery is the same size as the live
951 * data (L), then we need 3L bytes. We can reduce the size of the
952 * nursery to bring the required memory down near 2L bytes.
954 * A normal 2-space collector would need 4L bytes to give the same
955 * performance we get from 3L bytes, reducing to the same
956 * performance at 2L bytes.
958 blocks = g0s0->n_to_blocks;
960 if ( RtsFlags.GcFlags.maxHeapSize != 0 &&
961 blocks * RtsFlags.GcFlags.oldGenFactor * 2 >
962 RtsFlags.GcFlags.maxHeapSize ) {
963 long adjusted_blocks; // signed on purpose
966 adjusted_blocks = (RtsFlags.GcFlags.maxHeapSize - 2 * blocks);
967 IF_DEBUG(gc, debugBelch("@@ Near maximum heap size of 0x%x blocks, blocks = %d, adjusted to %ld", RtsFlags.GcFlags.maxHeapSize, blocks, adjusted_blocks));
968 pc_free = adjusted_blocks * 100 / RtsFlags.GcFlags.maxHeapSize;
969 if (pc_free < RtsFlags.GcFlags.pcFreeHeap) /* might even be < 0 */ {
972 blocks = adjusted_blocks;
975 blocks *= RtsFlags.GcFlags.oldGenFactor;
976 if (blocks < RtsFlags.GcFlags.minAllocAreaSize) {
977 blocks = RtsFlags.GcFlags.minAllocAreaSize;
980 resizeNursery(blocks);
983 /* Generational collector:
984 * If the user has given us a suggested heap size, adjust our
985 * allocation area to make best use of the memory available.
988 if (RtsFlags.GcFlags.heapSizeSuggestion) {
990 nat needed = calcNeeded(); // approx blocks needed at next GC
992 /* Guess how much will be live in generation 0 step 0 next time.
993 * A good approximation is obtained by finding the
994 * percentage of g0s0 that was live at the last minor GC.
997 g0s0_pcnt_kept = (new_blocks * 100) / g0s0->n_blocks;
1000 /* Estimate a size for the allocation area based on the
1001 * information available. We might end up going slightly under
1002 * or over the suggested heap size, but we should be pretty
1005 * Formula: suggested - needed
1006 * ----------------------------
1007 * 1 + g0s0_pcnt_kept/100
1009 * where 'needed' is the amount of memory needed at the next
1010 * collection for collecting all steps except g0s0.
1013 (((long)RtsFlags.GcFlags.heapSizeSuggestion - (long)needed) * 100) /
1014 (100 + (long)g0s0_pcnt_kept);
1016 if (blocks < (long)RtsFlags.GcFlags.minAllocAreaSize) {
1017 blocks = RtsFlags.GcFlags.minAllocAreaSize;
1020 resizeNursery((nat)blocks);
1023 // we might have added extra large blocks to the nursery, so
1024 // resize back to minAllocAreaSize again.
1025 resizeNursery(RtsFlags.GcFlags.minAllocAreaSize);
1029 // mark the garbage collected CAFs as dead
1030 #if 0 && defined(DEBUG) // doesn't work at the moment
1031 if (major_gc) { gcCAFs(); }
1035 // resetStaticObjectForRetainerProfiling() must be called before
1037 resetStaticObjectForRetainerProfiling();
1040 // zero the scavenged static object list
1042 zero_static_object_list(scavenged_static_objects);
1045 // Reset the nursery
1048 RELEASE_LOCK(&sched_mutex);
1050 // start any pending finalizers
1051 scheduleFinalizers(old_weak_ptr_list);
1053 // send exceptions to any threads which were about to die
1054 resurrectThreads(resurrected_threads);
1056 ACQUIRE_LOCK(&sched_mutex);
1058 // Update the stable pointer hash table.
1059 updateStablePtrTable(major_gc);
1061 // check sanity after GC
1062 IF_DEBUG(sanity, checkSanity());
1064 // extra GC trace info
1065 IF_DEBUG(gc, statDescribeGens());
1068 // symbol-table based profiling
1069 /* heapCensus(to_blocks); */ /* ToDo */
1072 // restore enclosing cost centre
1077 // check for memory leaks if sanity checking is on
1078 IF_DEBUG(sanity, memInventory());
1080 #ifdef RTS_GTK_FRONTPANEL
1081 if (RtsFlags.GcFlags.frontpanel) {
1082 updateFrontPanelAfterGC( N, live );
1086 // ok, GC over: tell the stats department what happened.
1087 stat_endGC(allocated, collected, live, copied, N);
1089 #if defined(RTS_USER_SIGNALS)
1090 // unblock signals again
1091 unblockUserSignals();
1098 /* -----------------------------------------------------------------------------
1101 traverse_weak_ptr_list is called possibly many times during garbage
1102 collection. It returns a flag indicating whether it did any work
1103 (i.e. called evacuate on any live pointers).
1105 Invariant: traverse_weak_ptr_list is called when the heap is in an
1106 idempotent state. That means that there are no pending
1107 evacuate/scavenge operations. This invariant helps the weak
1108 pointer code decide which weak pointers are dead - if there are no
1109 new live weak pointers, then all the currently unreachable ones are
1112 For generational GC: we just don't try to finalize weak pointers in
1113 older generations than the one we're collecting. This could
1114 probably be optimised by keeping per-generation lists of weak
1115 pointers, but for a few weak pointers this scheme will work.
1117 There are three distinct stages to processing weak pointers:
1119 - weak_stage == WeakPtrs
1121 We process all the weak pointers whos keys are alive (evacuate
1122 their values and finalizers), and repeat until we can find no new
1123 live keys. If no live keys are found in this pass, then we
1124 evacuate the finalizers of all the dead weak pointers in order to
1127 - weak_stage == WeakThreads
1129 Now, we discover which *threads* are still alive. Pointers to
1130 threads from the all_threads and main thread lists are the
1131 weakest of all: a pointers from the finalizer of a dead weak
1132 pointer can keep a thread alive. Any threads found to be unreachable
1133 are evacuated and placed on the resurrected_threads list so we
1134 can send them a signal later.
1136 - weak_stage == WeakDone
1138 No more evacuation is done.
1140 -------------------------------------------------------------------------- */
1143 traverse_weak_ptr_list(void)
1145 StgWeak *w, **last_w, *next_w;
1147 rtsBool flag = rtsFalse;
1149 switch (weak_stage) {
1155 /* doesn't matter where we evacuate values/finalizers to, since
1156 * these pointers are treated as roots (iff the keys are alive).
1160 last_w = &old_weak_ptr_list;
1161 for (w = old_weak_ptr_list; w != NULL; w = next_w) {
1163 /* There might be a DEAD_WEAK on the list if finalizeWeak# was
1164 * called on a live weak pointer object. Just remove it.
1166 if (w->header.info == &stg_DEAD_WEAK_info) {
1167 next_w = ((StgDeadWeak *)w)->link;
1172 switch (get_itbl(w)->type) {
1175 next_w = (StgWeak *)((StgEvacuated *)w)->evacuee;
1180 /* Now, check whether the key is reachable.
1182 new = isAlive(w->key);
1185 // evacuate the value and finalizer
1186 w->value = evacuate(w->value);
1187 w->finalizer = evacuate(w->finalizer);
1188 // remove this weak ptr from the old_weak_ptr list
1190 // and put it on the new weak ptr list
1192 w->link = weak_ptr_list;
1195 IF_DEBUG(weak, debugBelch("Weak pointer still alive at %p -> %p",
1200 last_w = &(w->link);
1206 barf("traverse_weak_ptr_list: not WEAK");
1210 /* If we didn't make any changes, then we can go round and kill all
1211 * the dead weak pointers. The old_weak_ptr list is used as a list
1212 * of pending finalizers later on.
1214 if (flag == rtsFalse) {
1215 for (w = old_weak_ptr_list; w; w = w->link) {
1216 w->finalizer = evacuate(w->finalizer);
1219 // Next, move to the WeakThreads stage after fully
1220 // scavenging the finalizers we've just evacuated.
1221 weak_stage = WeakThreads;
1227 /* Now deal with the all_threads list, which behaves somewhat like
1228 * the weak ptr list. If we discover any threads that are about to
1229 * become garbage, we wake them up and administer an exception.
1232 StgTSO *t, *tmp, *next, **prev;
1234 prev = &old_all_threads;
1235 for (t = old_all_threads; t != END_TSO_QUEUE; t = next) {
1237 tmp = (StgTSO *)isAlive((StgClosure *)t);
1243 ASSERT(get_itbl(t)->type == TSO);
1244 switch (t->what_next) {
1245 case ThreadRelocated:
1250 case ThreadComplete:
1251 // finshed or died. The thread might still be alive, but we
1252 // don't keep it on the all_threads list. Don't forget to
1253 // stub out its global_link field.
1254 next = t->global_link;
1255 t->global_link = END_TSO_QUEUE;
1263 // not alive (yet): leave this thread on the
1264 // old_all_threads list.
1265 prev = &(t->global_link);
1266 next = t->global_link;
1269 // alive: move this thread onto the all_threads list.
1270 next = t->global_link;
1271 t->global_link = all_threads;
1278 /* And resurrect any threads which were about to become garbage.
1281 StgTSO *t, *tmp, *next;
1282 for (t = old_all_threads; t != END_TSO_QUEUE; t = next) {
1283 next = t->global_link;
1284 tmp = (StgTSO *)evacuate((StgClosure *)t);
1285 tmp->global_link = resurrected_threads;
1286 resurrected_threads = tmp;
1290 weak_stage = WeakDone; // *now* we're done,
1291 return rtsTrue; // but one more round of scavenging, please
1294 barf("traverse_weak_ptr_list");
1300 /* -----------------------------------------------------------------------------
1301 After GC, the live weak pointer list may have forwarding pointers
1302 on it, because a weak pointer object was evacuated after being
1303 moved to the live weak pointer list. We remove those forwarding
1306 Also, we don't consider weak pointer objects to be reachable, but
1307 we must nevertheless consider them to be "live" and retain them.
1308 Therefore any weak pointer objects which haven't as yet been
1309 evacuated need to be evacuated now.
1310 -------------------------------------------------------------------------- */
1314 mark_weak_ptr_list ( StgWeak **list )
1316 StgWeak *w, **last_w;
1319 for (w = *list; w; w = w->link) {
1320 // w might be WEAK, EVACUATED, or DEAD_WEAK (actually CON_STATIC) here
1321 ASSERT(w->header.info == &stg_DEAD_WEAK_info
1322 || get_itbl(w)->type == WEAK || get_itbl(w)->type == EVACUATED);
1323 w = (StgWeak *)evacuate((StgClosure *)w);
1325 last_w = &(w->link);
1329 /* -----------------------------------------------------------------------------
1330 isAlive determines whether the given closure is still alive (after
1331 a garbage collection) or not. It returns the new address of the
1332 closure if it is alive, or NULL otherwise.
1334 NOTE: Use it before compaction only!
1335 -------------------------------------------------------------------------- */
1339 isAlive(StgClosure *p)
1341 const StgInfoTable *info;
1346 ASSERT(LOOKS_LIKE_CLOSURE_PTR(p));
1349 // ignore static closures
1351 // ToDo: for static closures, check the static link field.
1352 // Problem here is that we sometimes don't set the link field, eg.
1353 // for static closures with an empty SRT or CONSTR_STATIC_NOCAFs.
1355 if (!HEAP_ALLOCED(p)) {
1359 // ignore closures in generations that we're not collecting.
1361 if (bd->gen_no > N) {
1365 // if it's a pointer into to-space, then we're done
1366 if (bd->flags & BF_EVACUATED) {
1370 // large objects use the evacuated flag
1371 if (bd->flags & BF_LARGE) {
1375 // check the mark bit for compacted steps
1376 if ((bd->flags & BF_COMPACTED) && is_marked((P_)p,bd)) {
1380 switch (info->type) {
1385 case IND_OLDGEN: // rely on compatible layout with StgInd
1386 case IND_OLDGEN_PERM:
1387 // follow indirections
1388 p = ((StgInd *)p)->indirectee;
1393 return ((StgEvacuated *)p)->evacuee;
1396 if (((StgTSO *)p)->what_next == ThreadRelocated) {
1397 p = (StgClosure *)((StgTSO *)p)->link;
1410 mark_root(StgClosure **root)
1412 *root = evacuate(*root);
1416 upd_evacuee(StgClosure *p, StgClosure *dest)
1418 // not true: (ToDo: perhaps it should be)
1419 // ASSERT(Bdescr((P_)dest)->flags & BF_EVACUATED);
1420 SET_INFO(p, &stg_EVACUATED_info);
1421 ((StgEvacuated *)p)->evacuee = dest;
1425 STATIC_INLINE StgClosure *
1426 copy(StgClosure *src, nat size, step *stp)
1431 nat size_org = size;
1434 TICK_GC_WORDS_COPIED(size);
1435 /* Find out where we're going, using the handy "to" pointer in
1436 * the step of the source object. If it turns out we need to
1437 * evacuate to an older generation, adjust it here (see comment
1440 if (stp->gen_no < evac_gen) {
1441 #ifdef NO_EAGER_PROMOTION
1442 failed_to_evac = rtsTrue;
1444 stp = &generations[evac_gen].steps[0];
1448 /* chain a new block onto the to-space for the destination step if
1451 if (stp->hp + size >= stp->hpLim) {
1452 gc_alloc_block(stp);
1455 for(to = stp->hp, from = (P_)src; size>0; --size) {
1461 upd_evacuee(src,(StgClosure *)dest);
1463 // We store the size of the just evacuated object in the LDV word so that
1464 // the profiler can guess the position of the next object later.
1465 SET_EVACUAEE_FOR_LDV(src, size_org);
1467 return (StgClosure *)dest;
1470 /* Special version of copy() for when we only want to copy the info
1471 * pointer of an object, but reserve some padding after it. This is
1472 * used to optimise evacuation of BLACKHOLEs.
1477 copyPart(StgClosure *src, nat size_to_reserve, nat size_to_copy, step *stp)
1482 nat size_to_copy_org = size_to_copy;
1485 TICK_GC_WORDS_COPIED(size_to_copy);
1486 if (stp->gen_no < evac_gen) {
1487 #ifdef NO_EAGER_PROMOTION
1488 failed_to_evac = rtsTrue;
1490 stp = &generations[evac_gen].steps[0];
1494 if (stp->hp + size_to_reserve >= stp->hpLim) {
1495 gc_alloc_block(stp);
1498 for(to = stp->hp, from = (P_)src; size_to_copy>0; --size_to_copy) {
1503 stp->hp += size_to_reserve;
1504 upd_evacuee(src,(StgClosure *)dest);
1506 // We store the size of the just evacuated object in the LDV word so that
1507 // the profiler can guess the position of the next object later.
1508 // size_to_copy_org is wrong because the closure already occupies size_to_reserve
1510 SET_EVACUAEE_FOR_LDV(src, size_to_reserve);
1512 if (size_to_reserve - size_to_copy_org > 0)
1513 FILL_SLOP(stp->hp - 1, (int)(size_to_reserve - size_to_copy_org));
1515 return (StgClosure *)dest;
1519 /* -----------------------------------------------------------------------------
1520 Evacuate a large object
1522 This just consists of removing the object from the (doubly-linked)
1523 step->large_objects list, and linking it on to the (singly-linked)
1524 step->new_large_objects list, from where it will be scavenged later.
1526 Convention: bd->flags has BF_EVACUATED set for a large object
1527 that has been evacuated, or unset otherwise.
1528 -------------------------------------------------------------------------- */
1532 evacuate_large(StgPtr p)
1534 bdescr *bd = Bdescr(p);
1537 // object must be at the beginning of the block (or be a ByteArray)
1538 ASSERT(get_itbl((StgClosure *)p)->type == ARR_WORDS ||
1539 (((W_)p & BLOCK_MASK) == 0));
1541 // already evacuated?
1542 if (bd->flags & BF_EVACUATED) {
1543 /* Don't forget to set the failed_to_evac flag if we didn't get
1544 * the desired destination (see comments in evacuate()).
1546 if (bd->gen_no < evac_gen) {
1547 failed_to_evac = rtsTrue;
1548 TICK_GC_FAILED_PROMOTION();
1554 // remove from large_object list
1556 bd->u.back->link = bd->link;
1557 } else { // first object in the list
1558 stp->large_objects = bd->link;
1561 bd->link->u.back = bd->u.back;
1564 /* link it on to the evacuated large object list of the destination step
1567 if (stp->gen_no < evac_gen) {
1568 #ifdef NO_EAGER_PROMOTION
1569 failed_to_evac = rtsTrue;
1571 stp = &generations[evac_gen].steps[0];
1576 bd->gen_no = stp->gen_no;
1577 bd->link = stp->new_large_objects;
1578 stp->new_large_objects = bd;
1579 bd->flags |= BF_EVACUATED;
1582 /* -----------------------------------------------------------------------------
1585 This is called (eventually) for every live object in the system.
1587 The caller to evacuate specifies a desired generation in the
1588 evac_gen global variable. The following conditions apply to
1589 evacuating an object which resides in generation M when we're
1590 collecting up to generation N
1594 else evac to step->to
1596 if M < evac_gen evac to evac_gen, step 0
1598 if the object is already evacuated, then we check which generation
1601 if M >= evac_gen do nothing
1602 if M < evac_gen set failed_to_evac flag to indicate that we
1603 didn't manage to evacuate this object into evac_gen.
1608 evacuate() is the single most important function performance-wise
1609 in the GC. Various things have been tried to speed it up, but as
1610 far as I can tell the code generated by gcc 3.2 with -O2 is about
1611 as good as it's going to get. We pass the argument to evacuate()
1612 in a register using the 'regparm' attribute (see the prototype for
1613 evacuate() near the top of this file).
1615 Changing evacuate() to take an (StgClosure **) rather than
1616 returning the new pointer seems attractive, because we can avoid
1617 writing back the pointer when it hasn't changed (eg. for a static
1618 object, or an object in a generation > N). However, I tried it and
1619 it doesn't help. One reason is that the (StgClosure **) pointer
1620 gets spilled to the stack inside evacuate(), resulting in far more
1621 extra reads/writes than we save.
1622 -------------------------------------------------------------------------- */
1624 REGPARM1 static StgClosure *
1625 evacuate(StgClosure *q)
1630 const StgInfoTable *info;
1633 if (HEAP_ALLOCED(q)) {
1636 if (bd->gen_no > N) {
1637 /* Can't evacuate this object, because it's in a generation
1638 * older than the ones we're collecting. Let's hope that it's
1639 * in evac_gen or older, or we will have to arrange to track
1640 * this pointer using the mutable list.
1642 if (bd->gen_no < evac_gen) {
1644 failed_to_evac = rtsTrue;
1645 TICK_GC_FAILED_PROMOTION();
1650 /* evacuate large objects by re-linking them onto a different list.
1652 if (bd->flags & BF_LARGE) {
1654 if (info->type == TSO &&
1655 ((StgTSO *)q)->what_next == ThreadRelocated) {
1656 q = (StgClosure *)((StgTSO *)q)->link;
1659 evacuate_large((P_)q);
1663 /* If the object is in a step that we're compacting, then we
1664 * need to use an alternative evacuate procedure.
1666 if (bd->flags & BF_COMPACTED) {
1667 if (!is_marked((P_)q,bd)) {
1669 if (mark_stack_full()) {
1670 mark_stack_overflowed = rtsTrue;
1673 push_mark_stack((P_)q);
1678 /* Object is not already evacuated. */
1679 ASSERT((bd->flags & BF_EVACUATED) == 0);
1684 else stp = NULL; // make sure copy() will crash if HEAP_ALLOCED is wrong
1687 // make sure the info pointer is into text space
1688 ASSERT(LOOKS_LIKE_CLOSURE_PTR(q));
1691 switch (info -> type) {
1695 return copy(q,sizeW_fromITBL(info),stp);
1699 StgWord w = (StgWord)q->payload[0];
1700 if (q->header.info == Czh_con_info &&
1701 // unsigned, so always true: (StgChar)w >= MIN_CHARLIKE &&
1702 (StgChar)w <= MAX_CHARLIKE) {
1703 return (StgClosure *)CHARLIKE_CLOSURE((StgChar)w);
1705 if (q->header.info == Izh_con_info &&
1706 (StgInt)w >= MIN_INTLIKE && (StgInt)w <= MAX_INTLIKE) {
1707 return (StgClosure *)INTLIKE_CLOSURE((StgInt)w);
1709 // else, fall through ...
1717 return copy(q,sizeofW(StgHeader)+1,stp);
1722 #ifdef NO_PROMOTE_THUNKS
1723 if (bd->gen_no == 0 &&
1724 bd->step->no != 0 &&
1725 bd->step->no == generations[bd->gen_no].n_steps-1) {
1729 return copy(q,sizeofW(StgHeader)+2,stp);
1737 return copy(q,sizeofW(StgHeader)+2,stp);
1743 case IND_OLDGEN_PERM:
1747 return copy(q,sizeW_fromITBL(info),stp);
1750 return copy(q,bco_sizeW((StgBCO *)q),stp);
1753 case SE_CAF_BLACKHOLE:
1756 return copyPart(q,BLACKHOLE_sizeW(),sizeofW(StgHeader),stp);
1759 to = copy(q,BLACKHOLE_sizeW(),stp);
1762 case THUNK_SELECTOR:
1766 if (thunk_selector_depth > MAX_THUNK_SELECTOR_DEPTH) {
1767 return copy(q,THUNK_SELECTOR_sizeW(),stp);
1770 p = eval_thunk_selector(info->layout.selector_offset,
1774 return copy(q,THUNK_SELECTOR_sizeW(),stp);
1776 // q is still BLACKHOLE'd.
1777 thunk_selector_depth++;
1779 thunk_selector_depth--;
1782 // We store the size of the just evacuated object in the
1783 // LDV word so that the profiler can guess the position of
1784 // the next object later.
1785 SET_EVACUAEE_FOR_LDV(q, THUNK_SELECTOR_sizeW());
1793 // follow chains of indirections, don't evacuate them
1794 q = ((StgInd*)q)->indirectee;
1798 if (info->srt_bitmap != 0 && major_gc &&
1799 THUNK_STATIC_LINK((StgClosure *)q) == NULL) {
1800 THUNK_STATIC_LINK((StgClosure *)q) = static_objects;
1801 static_objects = (StgClosure *)q;
1806 if (info->srt_bitmap != 0 && major_gc &&
1807 FUN_STATIC_LINK((StgClosure *)q) == NULL) {
1808 FUN_STATIC_LINK((StgClosure *)q) = static_objects;
1809 static_objects = (StgClosure *)q;
1814 /* If q->saved_info != NULL, then it's a revertible CAF - it'll be
1815 * on the CAF list, so don't do anything with it here (we'll
1816 * scavenge it later).
1819 && ((StgIndStatic *)q)->saved_info == NULL
1820 && IND_STATIC_LINK((StgClosure *)q) == NULL) {
1821 IND_STATIC_LINK((StgClosure *)q) = static_objects;
1822 static_objects = (StgClosure *)q;
1827 if (major_gc && STATIC_LINK(info,(StgClosure *)q) == NULL) {
1828 STATIC_LINK(info,(StgClosure *)q) = static_objects;
1829 static_objects = (StgClosure *)q;
1833 case CONSTR_INTLIKE:
1834 case CONSTR_CHARLIKE:
1835 case CONSTR_NOCAF_STATIC:
1836 /* no need to put these on the static linked list, they don't need
1850 case CATCH_STM_FRAME:
1851 case CATCH_RETRY_FRAME:
1852 case ATOMICALLY_FRAME:
1853 // shouldn't see these
1854 barf("evacuate: stack frame at %p\n", q);
1858 return copy(q,pap_sizeW((StgPAP*)q),stp);
1861 return copy(q,ap_stack_sizeW((StgAP_STACK*)q),stp);
1864 /* Already evacuated, just return the forwarding address.
1865 * HOWEVER: if the requested destination generation (evac_gen) is
1866 * older than the actual generation (because the object was
1867 * already evacuated to a younger generation) then we have to
1868 * set the failed_to_evac flag to indicate that we couldn't
1869 * manage to promote the object to the desired generation.
1871 if (evac_gen > 0) { // optimisation
1872 StgClosure *p = ((StgEvacuated*)q)->evacuee;
1873 if (HEAP_ALLOCED(p) && Bdescr((P_)p)->gen_no < evac_gen) {
1874 failed_to_evac = rtsTrue;
1875 TICK_GC_FAILED_PROMOTION();
1878 return ((StgEvacuated*)q)->evacuee;
1881 // just copy the block
1882 return copy(q,arr_words_sizeW((StgArrWords *)q),stp);
1885 case MUT_ARR_PTRS_FROZEN:
1886 case MUT_ARR_PTRS_FROZEN0:
1887 // just copy the block
1888 return copy(q,mut_arr_ptrs_sizeW((StgMutArrPtrs *)q),stp);
1892 StgTSO *tso = (StgTSO *)q;
1894 /* Deal with redirected TSOs (a TSO that's had its stack enlarged).
1896 if (tso->what_next == ThreadRelocated) {
1897 q = (StgClosure *)tso->link;
1901 /* To evacuate a small TSO, we need to relocate the update frame
1908 new_tso = (StgTSO *)copyPart((StgClosure *)tso,
1910 sizeofW(StgTSO), stp);
1911 move_TSO(tso, new_tso);
1912 for (p = tso->sp, q = new_tso->sp;
1913 p < tso->stack+tso->stack_size;) {
1917 return (StgClosure *)new_tso;
1922 case RBH: // cf. BLACKHOLE_BQ
1924 //StgInfoTable *rip = get_closure_info(q, &size, &ptrs, &nonptrs, &vhs, str);
1925 to = copy(q,BLACKHOLE_sizeW(),stp);
1926 //ToDo: derive size etc from reverted IP
1927 //to = copy(q,size,stp);
1929 debugBelch("@@ evacuate: RBH %p (%s) to %p (%s)",
1930 q, info_type(q), to, info_type(to)));
1935 ASSERT(sizeofW(StgBlockedFetch) >= MIN_NONUPD_SIZE);
1936 to = copy(q,sizeofW(StgBlockedFetch),stp);
1938 debugBelch("@@ evacuate: %p (%s) to %p (%s)",
1939 q, info_type(q), to, info_type(to)));
1946 ASSERT(sizeofW(StgBlockedFetch) >= MIN_UPD_SIZE);
1947 to = copy(q,sizeofW(StgFetchMe),stp);
1949 debugBelch("@@ evacuate: %p (%s) to %p (%s)",
1950 q, info_type(q), to, info_type(to)));
1954 ASSERT(sizeofW(StgBlockedFetch) >= MIN_UPD_SIZE);
1955 to = copy(q,sizeofW(StgFetchMeBlockingQueue),stp);
1957 debugBelch("@@ evacuate: %p (%s) to %p (%s)",
1958 q, info_type(q), to, info_type(to)));
1963 return copy(q,sizeofW(StgTRecHeader),stp);
1965 case TVAR_WAIT_QUEUE:
1966 return copy(q,sizeofW(StgTVarWaitQueue),stp);
1969 return copy(q,sizeofW(StgTVar),stp);
1972 return copy(q,sizeofW(StgTRecChunk),stp);
1975 barf("evacuate: strange closure type %d", (int)(info->type));
1981 /* -----------------------------------------------------------------------------
1982 Evaluate a THUNK_SELECTOR if possible.
1984 returns: NULL if we couldn't evaluate this THUNK_SELECTOR, or
1985 a closure pointer if we evaluated it and this is the result. Note
1986 that "evaluating" the THUNK_SELECTOR doesn't necessarily mean
1987 reducing it to HNF, just that we have eliminated the selection.
1988 The result might be another thunk, or even another THUNK_SELECTOR.
1990 If the return value is non-NULL, the original selector thunk has
1991 been BLACKHOLE'd, and should be updated with an indirection or a
1992 forwarding pointer. If the return value is NULL, then the selector
1994 -------------------------------------------------------------------------- */
1996 static inline rtsBool
1997 is_to_space ( StgClosure *p )
2001 bd = Bdescr((StgPtr)p);
2002 if (HEAP_ALLOCED(p) &&
2003 ((bd->flags & BF_EVACUATED)
2004 || ((bd->flags & BF_COMPACTED) &&
2005 is_marked((P_)p,bd)))) {
2013 eval_thunk_selector( nat field, StgSelector * p )
2016 const StgInfoTable *info_ptr;
2017 StgClosure *selectee;
2019 selectee = p->selectee;
2021 // Save the real info pointer (NOTE: not the same as get_itbl()).
2022 info_ptr = p->header.info;
2024 // If the THUNK_SELECTOR is in a generation that we are not
2025 // collecting, then bail out early. We won't be able to save any
2026 // space in any case, and updating with an indirection is trickier
2028 if (Bdescr((StgPtr)p)->gen_no > N) {
2032 // BLACKHOLE the selector thunk, since it is now under evaluation.
2033 // This is important to stop us going into an infinite loop if
2034 // this selector thunk eventually refers to itself.
2035 SET_INFO(p,&stg_BLACKHOLE_info);
2039 // We don't want to end up in to-space, because this causes
2040 // problems when the GC later tries to evacuate the result of
2041 // eval_thunk_selector(). There are various ways this could
2044 // 1. following an IND_STATIC
2046 // 2. when the old generation is compacted, the mark phase updates
2047 // from-space pointers to be to-space pointers, and we can't
2048 // reliably tell which we're following (eg. from an IND_STATIC).
2050 // 3. compacting GC again: if we're looking at a constructor in
2051 // the compacted generation, it might point directly to objects
2052 // in to-space. We must bale out here, otherwise doing the selection
2053 // will result in a to-space pointer being returned.
2055 // (1) is dealt with using a BF_EVACUATED test on the
2056 // selectee. (2) and (3): we can tell if we're looking at an
2057 // object in the compacted generation that might point to
2058 // to-space objects by testing that (a) it is BF_COMPACTED, (b)
2059 // the compacted generation is being collected, and (c) the
2060 // object is marked. Only a marked object may have pointers that
2061 // point to to-space objects, because that happens when
2064 // The to-space test is now embodied in the in_to_space() inline
2065 // function, as it is re-used below.
2067 if (is_to_space(selectee)) {
2071 info = get_itbl(selectee);
2072 switch (info->type) {
2080 case CONSTR_NOCAF_STATIC:
2081 // check that the size is in range
2082 ASSERT(field < (StgWord32)(info->layout.payload.ptrs +
2083 info->layout.payload.nptrs));
2085 // Select the right field from the constructor, and check
2086 // that the result isn't in to-space. It might be in
2087 // to-space if, for example, this constructor contains
2088 // pointers to younger-gen objects (and is on the mut-once
2093 q = selectee->payload[field];
2094 if (is_to_space(q)) {
2104 case IND_OLDGEN_PERM:
2106 selectee = ((StgInd *)selectee)->indirectee;
2110 // We don't follow pointers into to-space; the constructor
2111 // has already been evacuated, so we won't save any space
2112 // leaks by evaluating this selector thunk anyhow.
2115 case THUNK_SELECTOR:
2119 // check that we don't recurse too much, re-using the
2120 // depth bound also used in evacuate().
2121 if (thunk_selector_depth >= MAX_THUNK_SELECTOR_DEPTH) {
2124 thunk_selector_depth++;
2126 val = eval_thunk_selector(info->layout.selector_offset,
2127 (StgSelector *)selectee);
2129 thunk_selector_depth--;
2134 // We evaluated this selector thunk, so update it with
2135 // an indirection. NOTE: we don't use UPD_IND here,
2136 // because we are guaranteed that p is in a generation
2137 // that we are collecting, and we never want to put the
2138 // indirection on a mutable list.
2140 // For the purposes of LDV profiling, we have destroyed
2141 // the original selector thunk.
2142 SET_INFO(p, info_ptr);
2143 LDV_RECORD_DEAD_FILL_SLOP_DYNAMIC(selectee);
2145 ((StgInd *)selectee)->indirectee = val;
2146 SET_INFO(selectee,&stg_IND_info);
2148 // For the purposes of LDV profiling, we have created an
2150 LDV_RECORD_CREATE(selectee);
2167 case SE_CAF_BLACKHOLE:
2180 // not evaluated yet
2184 barf("eval_thunk_selector: strange selectee %d",
2189 // We didn't manage to evaluate this thunk; restore the old info pointer
2190 SET_INFO(p, info_ptr);
2194 /* -----------------------------------------------------------------------------
2195 move_TSO is called to update the TSO structure after it has been
2196 moved from one place to another.
2197 -------------------------------------------------------------------------- */
2200 move_TSO (StgTSO *src, StgTSO *dest)
2204 // relocate the stack pointer...
2205 diff = (StgPtr)dest - (StgPtr)src; // In *words*
2206 dest->sp = (StgPtr)dest->sp + diff;
2209 /* Similar to scavenge_large_bitmap(), but we don't write back the
2210 * pointers we get back from evacuate().
2213 scavenge_large_srt_bitmap( StgLargeSRT *large_srt )
2220 bitmap = large_srt->l.bitmap[b];
2221 size = (nat)large_srt->l.size;
2222 p = (StgClosure **)large_srt->srt;
2223 for (i = 0; i < size; ) {
2224 if ((bitmap & 1) != 0) {
2229 if (i % BITS_IN(W_) == 0) {
2231 bitmap = large_srt->l.bitmap[b];
2233 bitmap = bitmap >> 1;
2238 /* evacuate the SRT. If srt_bitmap is zero, then there isn't an
2239 * srt field in the info table. That's ok, because we'll
2240 * never dereference it.
2243 scavenge_srt (StgClosure **srt, nat srt_bitmap)
2248 bitmap = srt_bitmap;
2251 if (bitmap == (StgHalfWord)(-1)) {
2252 scavenge_large_srt_bitmap( (StgLargeSRT *)srt );
2256 while (bitmap != 0) {
2257 if ((bitmap & 1) != 0) {
2258 #ifdef ENABLE_WIN32_DLL_SUPPORT
2259 // Special-case to handle references to closures hiding out in DLLs, since
2260 // double indirections required to get at those. The code generator knows
2261 // which is which when generating the SRT, so it stores the (indirect)
2262 // reference to the DLL closure in the table by first adding one to it.
2263 // We check for this here, and undo the addition before evacuating it.
2265 // If the SRT entry hasn't got bit 0 set, the SRT entry points to a
2266 // closure that's fixed at link-time, and no extra magic is required.
2267 if ( (unsigned long)(*srt) & 0x1 ) {
2268 evacuate(*stgCast(StgClosure**,(stgCast(unsigned long, *srt) & ~0x1)));
2277 bitmap = bitmap >> 1;
2283 scavenge_thunk_srt(const StgInfoTable *info)
2285 StgThunkInfoTable *thunk_info;
2287 thunk_info = itbl_to_thunk_itbl(info);
2288 scavenge_srt((StgClosure **)GET_SRT(thunk_info), thunk_info->i.srt_bitmap);
2292 scavenge_fun_srt(const StgInfoTable *info)
2294 StgFunInfoTable *fun_info;
2296 fun_info = itbl_to_fun_itbl(info);
2297 scavenge_srt((StgClosure **)GET_FUN_SRT(fun_info), fun_info->i.srt_bitmap);
2301 scavenge_ret_srt(const StgInfoTable *info)
2303 StgRetInfoTable *ret_info;
2305 ret_info = itbl_to_ret_itbl(info);
2306 scavenge_srt((StgClosure **)GET_SRT(ret_info), ret_info->i.srt_bitmap);
2309 /* -----------------------------------------------------------------------------
2311 -------------------------------------------------------------------------- */
2314 scavengeTSO (StgTSO *tso)
2316 // chase the link field for any TSOs on the same queue
2317 tso->link = (StgTSO *)evacuate((StgClosure *)tso->link);
2318 if ( tso->why_blocked == BlockedOnMVar
2319 || tso->why_blocked == BlockedOnBlackHole
2320 || tso->why_blocked == BlockedOnException
2322 || tso->why_blocked == BlockedOnGA
2323 || tso->why_blocked == BlockedOnGA_NoSend
2326 tso->block_info.closure = evacuate(tso->block_info.closure);
2328 if ( tso->blocked_exceptions != NULL ) {
2329 tso->blocked_exceptions =
2330 (StgTSO *)evacuate((StgClosure *)tso->blocked_exceptions);
2333 // scavange current transaction record
2334 tso->trec = (StgTRecHeader *)evacuate((StgClosure *)tso->trec);
2336 // scavenge this thread's stack
2337 scavenge_stack(tso->sp, &(tso->stack[tso->stack_size]));
2340 /* -----------------------------------------------------------------------------
2341 Blocks of function args occur on the stack (at the top) and
2343 -------------------------------------------------------------------------- */
2345 STATIC_INLINE StgPtr
2346 scavenge_arg_block (StgFunInfoTable *fun_info, StgClosure **args)
2353 switch (fun_info->f.fun_type) {
2355 bitmap = BITMAP_BITS(fun_info->f.bitmap);
2356 size = BITMAP_SIZE(fun_info->f.bitmap);
2359 size = GET_FUN_LARGE_BITMAP(fun_info)->size;
2360 scavenge_large_bitmap(p, GET_FUN_LARGE_BITMAP(fun_info), size);
2364 bitmap = BITMAP_BITS(stg_arg_bitmaps[fun_info->f.fun_type]);
2365 size = BITMAP_SIZE(stg_arg_bitmaps[fun_info->f.fun_type]);
2368 if ((bitmap & 1) == 0) {
2369 *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
2372 bitmap = bitmap >> 1;
2380 STATIC_INLINE StgPtr
2381 scavenge_PAP (StgPAP *pap)
2384 StgWord bitmap, size;
2385 StgFunInfoTable *fun_info;
2387 pap->fun = evacuate(pap->fun);
2388 fun_info = get_fun_itbl(pap->fun);
2389 ASSERT(fun_info->i.type != PAP);
2391 p = (StgPtr)pap->payload;
2394 switch (fun_info->f.fun_type) {
2396 bitmap = BITMAP_BITS(fun_info->f.bitmap);
2399 scavenge_large_bitmap(p, GET_FUN_LARGE_BITMAP(fun_info), size);
2403 scavenge_large_bitmap((StgPtr)pap->payload, BCO_BITMAP(pap->fun), size);
2407 bitmap = BITMAP_BITS(stg_arg_bitmaps[fun_info->f.fun_type]);
2411 if ((bitmap & 1) == 0) {
2412 *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
2415 bitmap = bitmap >> 1;
2423 /* -----------------------------------------------------------------------------
2424 Scavenge a given step until there are no more objects in this step
2427 evac_gen is set by the caller to be either zero (for a step in a
2428 generation < N) or G where G is the generation of the step being
2431 We sometimes temporarily change evac_gen back to zero if we're
2432 scavenging a mutable object where early promotion isn't such a good
2434 -------------------------------------------------------------------------- */
2442 nat saved_evac_gen = evac_gen;
2447 failed_to_evac = rtsFalse;
2449 /* scavenge phase - standard breadth-first scavenging of the
2453 while (bd != stp->hp_bd || p < stp->hp) {
2455 // If we're at the end of this block, move on to the next block
2456 if (bd != stp->hp_bd && p == bd->free) {
2462 ASSERT(LOOKS_LIKE_CLOSURE_PTR(p));
2463 info = get_itbl((StgClosure *)p);
2465 ASSERT(thunk_selector_depth == 0);
2468 switch (info->type) {
2472 StgMVar *mvar = ((StgMVar *)p);
2474 mvar->head = (StgTSO *)evacuate((StgClosure *)mvar->head);
2475 mvar->tail = (StgTSO *)evacuate((StgClosure *)mvar->tail);
2476 mvar->value = evacuate((StgClosure *)mvar->value);
2477 evac_gen = saved_evac_gen;
2478 failed_to_evac = rtsTrue; // mutable.
2479 p += sizeofW(StgMVar);
2484 scavenge_fun_srt(info);
2485 ((StgClosure *)p)->payload[1] = evacuate(((StgClosure *)p)->payload[1]);
2486 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
2487 p += sizeofW(StgHeader) + 2;
2491 scavenge_thunk_srt(info);
2493 ((StgClosure *)p)->payload[1] = evacuate(((StgClosure *)p)->payload[1]);
2494 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
2495 p += sizeofW(StgHeader) + 2;
2499 scavenge_thunk_srt(info);
2500 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
2501 p += sizeofW(StgHeader) + 1;
2505 scavenge_fun_srt(info);
2507 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
2508 p += sizeofW(StgHeader) + 1;
2512 scavenge_thunk_srt(info);
2513 p += sizeofW(StgHeader) + 1;
2517 scavenge_fun_srt(info);
2519 p += sizeofW(StgHeader) + 1;
2523 scavenge_thunk_srt(info);
2524 p += sizeofW(StgHeader) + 2;
2528 scavenge_fun_srt(info);
2530 p += sizeofW(StgHeader) + 2;
2534 scavenge_thunk_srt(info);
2535 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
2536 p += sizeofW(StgHeader) + 2;
2540 scavenge_fun_srt(info);
2542 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
2543 p += sizeofW(StgHeader) + 2;
2547 scavenge_fun_srt(info);
2551 scavenge_thunk_srt(info);
2562 end = (P_)((StgClosure *)p)->payload + info->layout.payload.ptrs;
2563 for (p = (P_)((StgClosure *)p)->payload; p < end; p++) {
2564 *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
2566 p += info->layout.payload.nptrs;
2571 StgBCO *bco = (StgBCO *)p;
2572 bco->instrs = (StgArrWords *)evacuate((StgClosure *)bco->instrs);
2573 bco->literals = (StgArrWords *)evacuate((StgClosure *)bco->literals);
2574 bco->ptrs = (StgMutArrPtrs *)evacuate((StgClosure *)bco->ptrs);
2575 bco->itbls = (StgArrWords *)evacuate((StgClosure *)bco->itbls);
2576 p += bco_sizeW(bco);
2581 if (stp->gen->no != 0) {
2584 // No need to call LDV_recordDead_FILL_SLOP_DYNAMIC() because an
2585 // IND_OLDGEN_PERM closure is larger than an IND_PERM closure.
2586 LDV_recordDead((StgClosure *)p, sizeofW(StgInd));
2589 // Todo: maybe use SET_HDR() and remove LDV_RECORD_CREATE()?
2591 SET_INFO(((StgClosure *)p), &stg_IND_OLDGEN_PERM_info);
2593 // We pretend that p has just been created.
2594 LDV_RECORD_CREATE((StgClosure *)p);
2597 case IND_OLDGEN_PERM:
2598 ((StgInd *)p)->indirectee = evacuate(((StgInd *)p)->indirectee);
2599 p += sizeofW(StgInd);
2604 ((StgMutVar *)p)->var = evacuate(((StgMutVar *)p)->var);
2605 evac_gen = saved_evac_gen;
2606 failed_to_evac = rtsTrue; // mutable anyhow
2607 p += sizeofW(StgMutVar);
2611 case SE_CAF_BLACKHOLE:
2614 p += BLACKHOLE_sizeW();
2619 StgBlockingQueue *bh = (StgBlockingQueue *)p;
2620 bh->blocking_queue =
2621 (StgTSO *)evacuate((StgClosure *)bh->blocking_queue);
2622 failed_to_evac = rtsTrue;
2623 p += BLACKHOLE_sizeW();
2627 case THUNK_SELECTOR:
2629 StgSelector *s = (StgSelector *)p;
2630 s->selectee = evacuate(s->selectee);
2631 p += THUNK_SELECTOR_sizeW();
2635 // A chunk of stack saved in a heap object
2638 StgAP_STACK *ap = (StgAP_STACK *)p;
2640 ap->fun = evacuate(ap->fun);
2641 scavenge_stack((StgPtr)ap->payload, (StgPtr)ap->payload + ap->size);
2642 p = (StgPtr)ap->payload + ap->size;
2648 p = scavenge_PAP((StgPAP *)p);
2652 // nothing to follow
2653 p += arr_words_sizeW((StgArrWords *)p);
2657 // follow everything
2661 evac_gen = 0; // repeatedly mutable
2662 next = p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
2663 for (p = (P_)((StgMutArrPtrs *)p)->payload; p < next; p++) {
2664 *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
2666 evac_gen = saved_evac_gen;
2667 failed_to_evac = rtsTrue; // mutable anyhow.
2671 case MUT_ARR_PTRS_FROZEN:
2672 case MUT_ARR_PTRS_FROZEN0:
2673 // follow everything
2677 next = p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
2678 for (p = (P_)((StgMutArrPtrs *)p)->payload; p < next; p++) {
2679 *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
2681 // it's tempting to recordMutable() if failed_to_evac is
2682 // false, but that breaks some assumptions (eg. every
2683 // closure on the mutable list is supposed to have the MUT
2684 // flag set, and MUT_ARR_PTRS_FROZEN doesn't).
2690 StgTSO *tso = (StgTSO *)p;
2693 evac_gen = saved_evac_gen;
2694 failed_to_evac = rtsTrue; // mutable anyhow.
2695 p += tso_sizeW(tso);
2700 case RBH: // cf. BLACKHOLE_BQ
2703 nat size, ptrs, nonptrs, vhs;
2705 StgInfoTable *rip = get_closure_info(p, &size, &ptrs, &nonptrs, &vhs, str);
2707 StgRBH *rbh = (StgRBH *)p;
2708 (StgClosure *)rbh->blocking_queue =
2709 evacuate((StgClosure *)rbh->blocking_queue);
2710 failed_to_evac = rtsTrue; // mutable anyhow.
2712 debugBelch("@@ scavenge: RBH %p (%s) (new blocking_queue link=%p)",
2713 p, info_type(p), (StgClosure *)rbh->blocking_queue));
2714 // ToDo: use size of reverted closure here!
2715 p += BLACKHOLE_sizeW();
2721 StgBlockedFetch *bf = (StgBlockedFetch *)p;
2722 // follow the pointer to the node which is being demanded
2723 (StgClosure *)bf->node =
2724 evacuate((StgClosure *)bf->node);
2725 // follow the link to the rest of the blocking queue
2726 (StgClosure *)bf->link =
2727 evacuate((StgClosure *)bf->link);
2729 debugBelch("@@ scavenge: %p (%s); node is now %p; exciting, isn't it",
2730 bf, info_type((StgClosure *)bf),
2731 bf->node, info_type(bf->node)));
2732 p += sizeofW(StgBlockedFetch);
2740 p += sizeofW(StgFetchMe);
2741 break; // nothing to do in this case
2743 case FETCH_ME_BQ: // cf. BLACKHOLE_BQ
2745 StgFetchMeBlockingQueue *fmbq = (StgFetchMeBlockingQueue *)p;
2746 (StgClosure *)fmbq->blocking_queue =
2747 evacuate((StgClosure *)fmbq->blocking_queue);
2749 debugBelch("@@ scavenge: %p (%s) exciting, isn't it",
2750 p, info_type((StgClosure *)p)));
2751 p += sizeofW(StgFetchMeBlockingQueue);
2756 case TVAR_WAIT_QUEUE:
2758 StgTVarWaitQueue *wq = ((StgTVarWaitQueue *) p);
2760 wq->waiting_tso = (StgTSO *)evacuate((StgClosure*)wq->waiting_tso);
2761 wq->next_queue_entry = (StgTVarWaitQueue *)evacuate((StgClosure*)wq->next_queue_entry);
2762 wq->prev_queue_entry = (StgTVarWaitQueue *)evacuate((StgClosure*)wq->prev_queue_entry);
2763 evac_gen = saved_evac_gen;
2764 failed_to_evac = rtsTrue; // mutable
2765 p += sizeofW(StgTVarWaitQueue);
2771 StgTVar *tvar = ((StgTVar *) p);
2773 tvar->current_value = evacuate((StgClosure*)tvar->current_value);
2774 tvar->first_wait_queue_entry = (StgTVarWaitQueue *)evacuate((StgClosure*)tvar->first_wait_queue_entry);
2775 evac_gen = saved_evac_gen;
2776 failed_to_evac = rtsTrue; // mutable
2777 p += sizeofW(StgTVar);
2783 StgTRecHeader *trec = ((StgTRecHeader *) p);
2785 trec->enclosing_trec = (StgTRecHeader *)evacuate((StgClosure*)trec->enclosing_trec);
2786 trec->current_chunk = (StgTRecChunk *)evacuate((StgClosure*)trec->current_chunk);
2787 evac_gen = saved_evac_gen;
2788 failed_to_evac = rtsTrue; // mutable
2789 p += sizeofW(StgTRecHeader);
2796 StgTRecChunk *tc = ((StgTRecChunk *) p);
2797 TRecEntry *e = &(tc -> entries[0]);
2799 tc->prev_chunk = (StgTRecChunk *)evacuate((StgClosure*)tc->prev_chunk);
2800 for (i = 0; i < tc -> next_entry_idx; i ++, e++ ) {
2801 e->tvar = (StgTVar *)evacuate((StgClosure*)e->tvar);
2802 e->expected_value = evacuate((StgClosure*)e->expected_value);
2803 e->new_value = evacuate((StgClosure*)e->new_value);
2805 evac_gen = saved_evac_gen;
2806 failed_to_evac = rtsTrue; // mutable
2807 p += sizeofW(StgTRecChunk);
2812 barf("scavenge: unimplemented/strange closure type %d @ %p",
2817 * We need to record the current object on the mutable list if
2818 * (a) It is actually mutable, or
2819 * (b) It contains pointers to a younger generation.
2820 * Case (b) arises if we didn't manage to promote everything that
2821 * the current object points to into the current generation.
2823 if (failed_to_evac) {
2824 failed_to_evac = rtsFalse;
2825 recordMutableGen((StgClosure *)q, stp->gen);
2833 /* -----------------------------------------------------------------------------
2834 Scavenge everything on the mark stack.
2836 This is slightly different from scavenge():
2837 - we don't walk linearly through the objects, so the scavenger
2838 doesn't need to advance the pointer on to the next object.
2839 -------------------------------------------------------------------------- */
2842 scavenge_mark_stack(void)
2848 evac_gen = oldest_gen->no;
2849 saved_evac_gen = evac_gen;
2852 while (!mark_stack_empty()) {
2853 p = pop_mark_stack();
2855 ASSERT(LOOKS_LIKE_CLOSURE_PTR(p));
2856 info = get_itbl((StgClosure *)p);
2859 switch (info->type) {
2863 StgMVar *mvar = ((StgMVar *)p);
2865 mvar->head = (StgTSO *)evacuate((StgClosure *)mvar->head);
2866 mvar->tail = (StgTSO *)evacuate((StgClosure *)mvar->tail);
2867 mvar->value = evacuate((StgClosure *)mvar->value);
2868 evac_gen = saved_evac_gen;
2869 failed_to_evac = rtsTrue; // mutable.
2874 scavenge_fun_srt(info);
2875 ((StgClosure *)p)->payload[1] = evacuate(((StgClosure *)p)->payload[1]);
2876 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
2880 scavenge_thunk_srt(info);
2882 ((StgClosure *)p)->payload[1] = evacuate(((StgClosure *)p)->payload[1]);
2883 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
2888 scavenge_fun_srt(info);
2889 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
2894 scavenge_thunk_srt(info);
2897 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
2902 scavenge_fun_srt(info);
2907 scavenge_thunk_srt(info);
2915 scavenge_fun_srt(info);
2919 scavenge_thunk_srt(info);
2930 end = (P_)((StgClosure *)p)->payload + info->layout.payload.ptrs;
2931 for (p = (P_)((StgClosure *)p)->payload; p < end; p++) {
2932 *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
2938 StgBCO *bco = (StgBCO *)p;
2939 bco->instrs = (StgArrWords *)evacuate((StgClosure *)bco->instrs);
2940 bco->literals = (StgArrWords *)evacuate((StgClosure *)bco->literals);
2941 bco->ptrs = (StgMutArrPtrs *)evacuate((StgClosure *)bco->ptrs);
2942 bco->itbls = (StgArrWords *)evacuate((StgClosure *)bco->itbls);
2947 // don't need to do anything here: the only possible case
2948 // is that we're in a 1-space compacting collector, with
2949 // no "old" generation.
2953 case IND_OLDGEN_PERM:
2954 ((StgInd *)p)->indirectee =
2955 evacuate(((StgInd *)p)->indirectee);
2960 ((StgMutVar *)p)->var = evacuate(((StgMutVar *)p)->var);
2961 evac_gen = saved_evac_gen;
2962 failed_to_evac = rtsTrue;
2966 case SE_CAF_BLACKHOLE:
2974 StgBlockingQueue *bh = (StgBlockingQueue *)p;
2975 bh->blocking_queue =
2976 (StgTSO *)evacuate((StgClosure *)bh->blocking_queue);
2977 failed_to_evac = rtsTrue;
2981 case THUNK_SELECTOR:
2983 StgSelector *s = (StgSelector *)p;
2984 s->selectee = evacuate(s->selectee);
2988 // A chunk of stack saved in a heap object
2991 StgAP_STACK *ap = (StgAP_STACK *)p;
2993 ap->fun = evacuate(ap->fun);
2994 scavenge_stack((StgPtr)ap->payload, (StgPtr)ap->payload + ap->size);
3000 scavenge_PAP((StgPAP *)p);
3004 // follow everything
3008 evac_gen = 0; // repeatedly mutable
3009 next = p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
3010 for (p = (P_)((StgMutArrPtrs *)p)->payload; p < next; p++) {
3011 *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
3013 evac_gen = saved_evac_gen;
3014 failed_to_evac = rtsTrue; // mutable anyhow.
3018 case MUT_ARR_PTRS_FROZEN:
3019 case MUT_ARR_PTRS_FROZEN0:
3020 // follow everything
3024 next = p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
3025 for (p = (P_)((StgMutArrPtrs *)p)->payload; p < next; p++) {
3026 *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
3033 StgTSO *tso = (StgTSO *)p;
3036 evac_gen = saved_evac_gen;
3037 failed_to_evac = rtsTrue;
3042 case RBH: // cf. BLACKHOLE_BQ
3045 nat size, ptrs, nonptrs, vhs;
3047 StgInfoTable *rip = get_closure_info(p, &size, &ptrs, &nonptrs, &vhs, str);
3049 StgRBH *rbh = (StgRBH *)p;
3050 bh->blocking_queue =
3051 (StgTSO *)evacuate((StgClosure *)bh->blocking_queue);
3052 failed_to_evac = rtsTrue; // mutable anyhow.
3054 debugBelch("@@ scavenge: RBH %p (%s) (new blocking_queue link=%p)",
3055 p, info_type(p), (StgClosure *)rbh->blocking_queue));
3061 StgBlockedFetch *bf = (StgBlockedFetch *)p;
3062 // follow the pointer to the node which is being demanded
3063 (StgClosure *)bf->node =
3064 evacuate((StgClosure *)bf->node);
3065 // follow the link to the rest of the blocking queue
3066 (StgClosure *)bf->link =
3067 evacuate((StgClosure *)bf->link);
3069 debugBelch("@@ scavenge: %p (%s); node is now %p; exciting, isn't it",
3070 bf, info_type((StgClosure *)bf),
3071 bf->node, info_type(bf->node)));
3079 break; // nothing to do in this case
3081 case FETCH_ME_BQ: // cf. BLACKHOLE_BQ
3083 StgFetchMeBlockingQueue *fmbq = (StgFetchMeBlockingQueue *)p;
3084 (StgClosure *)fmbq->blocking_queue =
3085 evacuate((StgClosure *)fmbq->blocking_queue);
3087 debugBelch("@@ scavenge: %p (%s) exciting, isn't it",
3088 p, info_type((StgClosure *)p)));
3093 case TVAR_WAIT_QUEUE:
3095 StgTVarWaitQueue *wq = ((StgTVarWaitQueue *) p);
3097 wq->waiting_tso = (StgTSO *)evacuate((StgClosure*)wq->waiting_tso);
3098 wq->next_queue_entry = (StgTVarWaitQueue *)evacuate((StgClosure*)wq->next_queue_entry);
3099 wq->prev_queue_entry = (StgTVarWaitQueue *)evacuate((StgClosure*)wq->prev_queue_entry);
3100 evac_gen = saved_evac_gen;
3101 failed_to_evac = rtsTrue; // mutable
3107 StgTVar *tvar = ((StgTVar *) p);
3109 tvar->current_value = evacuate((StgClosure*)tvar->current_value);
3110 tvar->first_wait_queue_entry = (StgTVarWaitQueue *)evacuate((StgClosure*)tvar->first_wait_queue_entry);
3111 evac_gen = saved_evac_gen;
3112 failed_to_evac = rtsTrue; // mutable
3119 StgTRecChunk *tc = ((StgTRecChunk *) p);
3120 TRecEntry *e = &(tc -> entries[0]);
3122 tc->prev_chunk = (StgTRecChunk *)evacuate((StgClosure*)tc->prev_chunk);
3123 for (i = 0; i < tc -> next_entry_idx; i ++, e++ ) {
3124 e->tvar = (StgTVar *)evacuate((StgClosure*)e->tvar);
3125 e->expected_value = evacuate((StgClosure*)e->expected_value);
3126 e->new_value = evacuate((StgClosure*)e->new_value);
3128 evac_gen = saved_evac_gen;
3129 failed_to_evac = rtsTrue; // mutable
3135 StgTRecHeader *trec = ((StgTRecHeader *) p);
3137 trec->enclosing_trec = (StgTRecHeader *)evacuate((StgClosure*)trec->enclosing_trec);
3138 trec->current_chunk = (StgTRecChunk *)evacuate((StgClosure*)trec->current_chunk);
3139 evac_gen = saved_evac_gen;
3140 failed_to_evac = rtsTrue; // mutable
3145 barf("scavenge_mark_stack: unimplemented/strange closure type %d @ %p",
3149 if (failed_to_evac) {
3150 failed_to_evac = rtsFalse;
3151 recordMutableGen((StgClosure *)q, &generations[evac_gen]);
3154 // mark the next bit to indicate "scavenged"
3155 mark(q+1, Bdescr(q));
3157 } // while (!mark_stack_empty())
3159 // start a new linear scan if the mark stack overflowed at some point
3160 if (mark_stack_overflowed && oldgen_scan_bd == NULL) {
3161 IF_DEBUG(gc, debugBelch("scavenge_mark_stack: starting linear scan"));
3162 mark_stack_overflowed = rtsFalse;
3163 oldgen_scan_bd = oldest_gen->steps[0].blocks;
3164 oldgen_scan = oldgen_scan_bd->start;
3167 if (oldgen_scan_bd) {
3168 // push a new thing on the mark stack
3170 // find a closure that is marked but not scavenged, and start
3172 while (oldgen_scan < oldgen_scan_bd->free
3173 && !is_marked(oldgen_scan,oldgen_scan_bd)) {
3177 if (oldgen_scan < oldgen_scan_bd->free) {
3179 // already scavenged?
3180 if (is_marked(oldgen_scan+1,oldgen_scan_bd)) {
3181 oldgen_scan += sizeofW(StgHeader) + MIN_NONUPD_SIZE;
3184 push_mark_stack(oldgen_scan);
3185 // ToDo: bump the linear scan by the actual size of the object
3186 oldgen_scan += sizeofW(StgHeader) + MIN_NONUPD_SIZE;
3190 oldgen_scan_bd = oldgen_scan_bd->link;
3191 if (oldgen_scan_bd != NULL) {
3192 oldgen_scan = oldgen_scan_bd->start;
3198 /* -----------------------------------------------------------------------------
3199 Scavenge one object.
3201 This is used for objects that are temporarily marked as mutable
3202 because they contain old-to-new generation pointers. Only certain
3203 objects can have this property.
3204 -------------------------------------------------------------------------- */
3207 scavenge_one(StgPtr p)
3209 const StgInfoTable *info;
3210 nat saved_evac_gen = evac_gen;
3213 ASSERT(LOOKS_LIKE_CLOSURE_PTR(p));
3214 info = get_itbl((StgClosure *)p);
3216 switch (info->type) {
3220 StgMVar *mvar = ((StgMVar *)p);
3222 mvar->head = (StgTSO *)evacuate((StgClosure *)mvar->head);
3223 mvar->tail = (StgTSO *)evacuate((StgClosure *)mvar->tail);
3224 mvar->value = evacuate((StgClosure *)mvar->value);
3225 evac_gen = saved_evac_gen;
3226 failed_to_evac = rtsTrue; // mutable.
3231 case FUN_1_0: // hardly worth specialising these guys
3254 end = (StgPtr)((StgClosure *)p)->payload + info->layout.payload.ptrs;
3255 for (q = (StgPtr)((StgClosure *)p)->payload; q < end; q++) {
3256 *q = (StgWord)(StgPtr)evacuate((StgClosure *)*q);
3263 ((StgMutVar *)p)->var = evacuate(((StgMutVar *)p)->var);
3264 evac_gen = saved_evac_gen;
3265 failed_to_evac = rtsTrue; // mutable anyhow
3269 case SE_CAF_BLACKHOLE:
3276 StgBlockingQueue *bh = (StgBlockingQueue *)p;
3277 evac_gen = 0; // repeatedly mutable
3278 bh->blocking_queue =
3279 (StgTSO *)evacuate((StgClosure *)bh->blocking_queue);
3280 failed_to_evac = rtsTrue;
3284 case THUNK_SELECTOR:
3286 StgSelector *s = (StgSelector *)p;
3287 s->selectee = evacuate(s->selectee);
3293 StgAP_STACK *ap = (StgAP_STACK *)p;
3295 ap->fun = evacuate(ap->fun);
3296 scavenge_stack((StgPtr)ap->payload, (StgPtr)ap->payload + ap->size);
3297 p = (StgPtr)ap->payload + ap->size;
3303 p = scavenge_PAP((StgPAP *)p);
3307 // nothing to follow
3312 // follow everything
3315 evac_gen = 0; // repeatedly mutable
3316 next = p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
3317 for (p = (P_)((StgMutArrPtrs *)p)->payload; p < next; p++) {
3318 *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
3320 evac_gen = saved_evac_gen;
3321 failed_to_evac = rtsTrue;
3325 case MUT_ARR_PTRS_FROZEN:
3326 case MUT_ARR_PTRS_FROZEN0:
3328 // follow everything
3331 next = p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
3332 for (p = (P_)((StgMutArrPtrs *)p)->payload; p < next; p++) {
3333 *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
3340 StgTSO *tso = (StgTSO *)p;
3342 evac_gen = 0; // repeatedly mutable
3344 evac_gen = saved_evac_gen;
3345 failed_to_evac = rtsTrue;
3350 case RBH: // cf. BLACKHOLE_BQ
3353 nat size, ptrs, nonptrs, vhs;
3355 StgInfoTable *rip = get_closure_info(p, &size, &ptrs, &nonptrs, &vhs, str);
3357 StgRBH *rbh = (StgRBH *)p;
3358 (StgClosure *)rbh->blocking_queue =
3359 evacuate((StgClosure *)rbh->blocking_queue);
3360 failed_to_evac = rtsTrue; // mutable anyhow.
3362 debugBelch("@@ scavenge: RBH %p (%s) (new blocking_queue link=%p)",
3363 p, info_type(p), (StgClosure *)rbh->blocking_queue));
3364 // ToDo: use size of reverted closure here!
3370 StgBlockedFetch *bf = (StgBlockedFetch *)p;
3371 // follow the pointer to the node which is being demanded
3372 (StgClosure *)bf->node =
3373 evacuate((StgClosure *)bf->node);
3374 // follow the link to the rest of the blocking queue
3375 (StgClosure *)bf->link =
3376 evacuate((StgClosure *)bf->link);
3378 debugBelch("@@ scavenge: %p (%s); node is now %p; exciting, isn't it",
3379 bf, info_type((StgClosure *)bf),
3380 bf->node, info_type(bf->node)));
3388 break; // nothing to do in this case
3390 case FETCH_ME_BQ: // cf. BLACKHOLE_BQ
3392 StgFetchMeBlockingQueue *fmbq = (StgFetchMeBlockingQueue *)p;
3393 (StgClosure *)fmbq->blocking_queue =
3394 evacuate((StgClosure *)fmbq->blocking_queue);
3396 debugBelch("@@ scavenge: %p (%s) exciting, isn't it",
3397 p, info_type((StgClosure *)p)));
3402 case TVAR_WAIT_QUEUE:
3404 StgTVarWaitQueue *wq = ((StgTVarWaitQueue *) p);
3406 wq->waiting_tso = (StgTSO *)evacuate((StgClosure*)wq->waiting_tso);
3407 wq->next_queue_entry = (StgTVarWaitQueue *)evacuate((StgClosure*)wq->next_queue_entry);
3408 wq->prev_queue_entry = (StgTVarWaitQueue *)evacuate((StgClosure*)wq->prev_queue_entry);
3409 evac_gen = saved_evac_gen;
3410 failed_to_evac = rtsTrue; // mutable
3416 StgTVar *tvar = ((StgTVar *) p);
3418 tvar->current_value = evacuate((StgClosure*)tvar->current_value);
3419 tvar->first_wait_queue_entry = (StgTVarWaitQueue *)evacuate((StgClosure*)tvar->first_wait_queue_entry);
3420 evac_gen = saved_evac_gen;
3421 failed_to_evac = rtsTrue; // mutable
3427 StgTRecHeader *trec = ((StgTRecHeader *) p);
3429 trec->enclosing_trec = (StgTRecHeader *)evacuate((StgClosure*)trec->enclosing_trec);
3430 trec->current_chunk = (StgTRecChunk *)evacuate((StgClosure*)trec->current_chunk);
3431 evac_gen = saved_evac_gen;
3432 failed_to_evac = rtsTrue; // mutable
3439 StgTRecChunk *tc = ((StgTRecChunk *) p);
3440 TRecEntry *e = &(tc -> entries[0]);
3442 tc->prev_chunk = (StgTRecChunk *)evacuate((StgClosure*)tc->prev_chunk);
3443 for (i = 0; i < tc -> next_entry_idx; i ++, e++ ) {
3444 e->tvar = (StgTVar *)evacuate((StgClosure*)e->tvar);
3445 e->expected_value = evacuate((StgClosure*)e->expected_value);
3446 e->new_value = evacuate((StgClosure*)e->new_value);
3448 evac_gen = saved_evac_gen;
3449 failed_to_evac = rtsTrue; // mutable
3454 case IND_OLDGEN_PERM:
3457 /* Careful here: a THUNK can be on the mutable list because
3458 * it contains pointers to young gen objects. If such a thunk
3459 * is updated, the IND_OLDGEN will be added to the mutable
3460 * list again, and we'll scavenge it twice. evacuate()
3461 * doesn't check whether the object has already been
3462 * evacuated, so we perform that check here.
3464 StgClosure *q = ((StgInd *)p)->indirectee;
3465 if (HEAP_ALLOCED(q) && Bdescr((StgPtr)q)->flags & BF_EVACUATED) {
3468 ((StgInd *)p)->indirectee = evacuate(q);
3471 #if 0 && defined(DEBUG)
3472 if (RtsFlags.DebugFlags.gc)
3473 /* Debugging code to print out the size of the thing we just
3477 StgPtr start = gen->steps[0].scan;
3478 bdescr *start_bd = gen->steps[0].scan_bd;
3480 scavenge(&gen->steps[0]);
3481 if (start_bd != gen->steps[0].scan_bd) {
3482 size += (P_)BLOCK_ROUND_UP(start) - start;
3483 start_bd = start_bd->link;
3484 while (start_bd != gen->steps[0].scan_bd) {
3485 size += BLOCK_SIZE_W;
3486 start_bd = start_bd->link;
3488 size += gen->steps[0].scan -
3489 (P_)BLOCK_ROUND_DOWN(gen->steps[0].scan);
3491 size = gen->steps[0].scan - start;
3493 debugBelch("evac IND_OLDGEN: %ld bytes", size * sizeof(W_));
3499 barf("scavenge_one: strange object %d", (int)(info->type));
3502 no_luck = failed_to_evac;
3503 failed_to_evac = rtsFalse;
3507 /* -----------------------------------------------------------------------------
3508 Scavenging mutable lists.
3510 We treat the mutable list of each generation > N (i.e. all the
3511 generations older than the one being collected) as roots. We also
3512 remove non-mutable objects from the mutable list at this point.
3513 -------------------------------------------------------------------------- */
3516 scavenge_mutable_list(generation *gen)
3521 bd = gen->saved_mut_list;
3524 for (; bd != NULL; bd = bd->link) {
3525 for (q = bd->start; q < bd->free; q++) {
3527 ASSERT(LOOKS_LIKE_CLOSURE_PTR(p));
3528 if (scavenge_one(p)) {
3529 /* didn't manage to promote everything, so put the
3530 * object back on the list.
3532 recordMutableGen((StgClosure *)p,gen);
3537 // free the old mut_list
3538 freeChain(gen->saved_mut_list);
3539 gen->saved_mut_list = NULL;
3544 scavenge_static(void)
3546 StgClosure* p = static_objects;
3547 const StgInfoTable *info;
3549 /* Always evacuate straight to the oldest generation for static
3551 evac_gen = oldest_gen->no;
3553 /* keep going until we've scavenged all the objects on the linked
3555 while (p != END_OF_STATIC_LIST) {
3557 ASSERT(LOOKS_LIKE_CLOSURE_PTR(p));
3560 if (info->type==RBH)
3561 info = REVERT_INFOPTR(info); // if it's an RBH, look at the orig closure
3563 // make sure the info pointer is into text space
3565 /* Take this object *off* the static_objects list,
3566 * and put it on the scavenged_static_objects list.
3568 static_objects = STATIC_LINK(info,p);
3569 STATIC_LINK(info,p) = scavenged_static_objects;
3570 scavenged_static_objects = p;
3572 switch (info -> type) {
3576 StgInd *ind = (StgInd *)p;
3577 ind->indirectee = evacuate(ind->indirectee);
3579 /* might fail to evacuate it, in which case we have to pop it
3580 * back on the mutable list of the oldest generation. We
3581 * leave it *on* the scavenged_static_objects list, though,
3582 * in case we visit this object again.
3584 if (failed_to_evac) {
3585 failed_to_evac = rtsFalse;
3586 recordMutableGen((StgClosure *)p,oldest_gen);
3592 scavenge_thunk_srt(info);
3596 scavenge_fun_srt(info);
3603 next = (P_)p->payload + info->layout.payload.ptrs;
3604 // evacuate the pointers
3605 for (q = (P_)p->payload; q < next; q++) {
3606 *q = (StgWord)(StgPtr)evacuate((StgClosure *)*q);
3612 barf("scavenge_static: strange closure %d", (int)(info->type));
3615 ASSERT(failed_to_evac == rtsFalse);
3617 /* get the next static object from the list. Remember, there might
3618 * be more stuff on this list now that we've done some evacuating!
3619 * (static_objects is a global)
3625 /* -----------------------------------------------------------------------------
3626 scavenge a chunk of memory described by a bitmap
3627 -------------------------------------------------------------------------- */
3630 scavenge_large_bitmap( StgPtr p, StgLargeBitmap *large_bitmap, nat size )
3636 bitmap = large_bitmap->bitmap[b];
3637 for (i = 0; i < size; ) {
3638 if ((bitmap & 1) == 0) {
3639 *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
3643 if (i % BITS_IN(W_) == 0) {
3645 bitmap = large_bitmap->bitmap[b];
3647 bitmap = bitmap >> 1;
3652 STATIC_INLINE StgPtr
3653 scavenge_small_bitmap (StgPtr p, nat size, StgWord bitmap)
3656 if ((bitmap & 1) == 0) {
3657 *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
3660 bitmap = bitmap >> 1;
3666 /* -----------------------------------------------------------------------------
3667 scavenge_stack walks over a section of stack and evacuates all the
3668 objects pointed to by it. We can use the same code for walking
3669 AP_STACK_UPDs, since these are just sections of copied stack.
3670 -------------------------------------------------------------------------- */
3674 scavenge_stack(StgPtr p, StgPtr stack_end)
3676 const StgRetInfoTable* info;
3680 //IF_DEBUG(sanity, debugBelch(" scavenging stack between %p and %p", p, stack_end));
3683 * Each time around this loop, we are looking at a chunk of stack
3684 * that starts with an activation record.
3687 while (p < stack_end) {
3688 info = get_ret_itbl((StgClosure *)p);
3690 switch (info->i.type) {
3693 ((StgUpdateFrame *)p)->updatee
3694 = evacuate(((StgUpdateFrame *)p)->updatee);
3695 p += sizeofW(StgUpdateFrame);
3698 // small bitmap (< 32 entries, or 64 on a 64-bit machine)
3699 case CATCH_STM_FRAME:
3700 case CATCH_RETRY_FRAME:
3701 case ATOMICALLY_FRAME:
3706 bitmap = BITMAP_BITS(info->i.layout.bitmap);
3707 size = BITMAP_SIZE(info->i.layout.bitmap);
3708 // NOTE: the payload starts immediately after the info-ptr, we
3709 // don't have an StgHeader in the same sense as a heap closure.
3711 p = scavenge_small_bitmap(p, size, bitmap);
3714 scavenge_srt((StgClosure **)GET_SRT(info), info->i.srt_bitmap);
3722 *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
3725 size = BCO_BITMAP_SIZE(bco);
3726 scavenge_large_bitmap(p, BCO_BITMAP(bco), size);
3731 // large bitmap (> 32 entries, or > 64 on a 64-bit machine)
3737 size = GET_LARGE_BITMAP(&info->i)->size;
3739 scavenge_large_bitmap(p, GET_LARGE_BITMAP(&info->i), size);
3741 // and don't forget to follow the SRT
3745 // Dynamic bitmap: the mask is stored on the stack, and
3746 // there are a number of non-pointers followed by a number
3747 // of pointers above the bitmapped area. (see StgMacros.h,
3752 dyn = ((StgRetDyn *)p)->liveness;
3754 // traverse the bitmap first
3755 bitmap = RET_DYN_LIVENESS(dyn);
3756 p = (P_)&((StgRetDyn *)p)->payload[0];
3757 size = RET_DYN_BITMAP_SIZE;
3758 p = scavenge_small_bitmap(p, size, bitmap);
3760 // skip over the non-ptr words
3761 p += RET_DYN_NONPTRS(dyn) + RET_DYN_NONPTR_REGS_SIZE;
3763 // follow the ptr words
3764 for (size = RET_DYN_PTRS(dyn); size > 0; size--) {
3765 *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
3773 StgRetFun *ret_fun = (StgRetFun *)p;
3774 StgFunInfoTable *fun_info;
3776 ret_fun->fun = evacuate(ret_fun->fun);
3777 fun_info = get_fun_itbl(ret_fun->fun);
3778 p = scavenge_arg_block(fun_info, ret_fun->payload);
3783 barf("scavenge_stack: weird activation record found on stack: %d", (int)(info->i.type));
3788 /*-----------------------------------------------------------------------------
3789 scavenge the large object list.
3791 evac_gen set by caller; similar games played with evac_gen as with
3792 scavenge() - see comment at the top of scavenge(). Most large
3793 objects are (repeatedly) mutable, so most of the time evac_gen will
3795 --------------------------------------------------------------------------- */
3798 scavenge_large(step *stp)
3803 bd = stp->new_large_objects;
3805 for (; bd != NULL; bd = stp->new_large_objects) {
3807 /* take this object *off* the large objects list and put it on
3808 * the scavenged large objects list. This is so that we can
3809 * treat new_large_objects as a stack and push new objects on
3810 * the front when evacuating.
3812 stp->new_large_objects = bd->link;
3813 dbl_link_onto(bd, &stp->scavenged_large_objects);
3815 // update the block count in this step.
3816 stp->n_scavenged_large_blocks += bd->blocks;
3819 if (scavenge_one(p)) {
3820 recordMutableGen((StgClosure *)p, stp->gen);
3825 /* -----------------------------------------------------------------------------
3826 Initialising the static object & mutable lists
3827 -------------------------------------------------------------------------- */
3830 zero_static_object_list(StgClosure* first_static)
3834 const StgInfoTable *info;
3836 for (p = first_static; p != END_OF_STATIC_LIST; p = link) {
3838 link = STATIC_LINK(info, p);
3839 STATIC_LINK(info,p) = NULL;
3843 /* -----------------------------------------------------------------------------
3845 -------------------------------------------------------------------------- */
3852 for (c = (StgIndStatic *)caf_list; c != NULL;
3853 c = (StgIndStatic *)c->static_link)
3855 SET_INFO(c, c->saved_info);
3856 c->saved_info = NULL;
3857 // could, but not necessary: c->static_link = NULL;
3863 markCAFs( evac_fn evac )
3867 for (c = (StgIndStatic *)caf_list; c != NULL;
3868 c = (StgIndStatic *)c->static_link)
3870 evac(&c->indirectee);
3874 /* -----------------------------------------------------------------------------
3875 Sanity code for CAF garbage collection.
3877 With DEBUG turned on, we manage a CAF list in addition to the SRT
3878 mechanism. After GC, we run down the CAF list and blackhole any
3879 CAFs which have been garbage collected. This means we get an error
3880 whenever the program tries to enter a garbage collected CAF.
3882 Any garbage collected CAFs are taken off the CAF list at the same
3884 -------------------------------------------------------------------------- */
3886 #if 0 && defined(DEBUG)
3893 const StgInfoTable *info;
3904 ASSERT(info->type == IND_STATIC);
3906 if (STATIC_LINK(info,p) == NULL) {
3907 IF_DEBUG(gccafs, debugBelch("CAF gc'd at 0x%04lx", (long)p));
3909 SET_INFO(p,&stg_BLACKHOLE_info);
3910 p = STATIC_LINK2(info,p);
3914 pp = &STATIC_LINK2(info,p);
3921 // debugBelch("%d CAFs live", i);
3926 /* -----------------------------------------------------------------------------
3929 Whenever a thread returns to the scheduler after possibly doing
3930 some work, we have to run down the stack and black-hole all the
3931 closures referred to by update frames.
3932 -------------------------------------------------------------------------- */
3935 threadLazyBlackHole(StgTSO *tso)
3938 StgRetInfoTable *info;
3939 StgBlockingQueue *bh;
3942 stack_end = &tso->stack[tso->stack_size];
3944 frame = (StgClosure *)tso->sp;
3947 info = get_ret_itbl(frame);
3949 switch (info->i.type) {
3952 bh = (StgBlockingQueue *)((StgUpdateFrame *)frame)->updatee;
3954 /* if the thunk is already blackholed, it means we've also
3955 * already blackholed the rest of the thunks on this stack,
3956 * so we can stop early.
3958 * The blackhole made for a CAF is a CAF_BLACKHOLE, so they
3959 * don't interfere with this optimisation.
3961 if (bh->header.info == &stg_BLACKHOLE_info) {
3965 if (bh->header.info != &stg_BLACKHOLE_BQ_info &&
3966 bh->header.info != &stg_CAF_BLACKHOLE_info) {
3967 #if (!defined(LAZY_BLACKHOLING)) && defined(DEBUG)
3968 debugBelch("Unexpected lazy BHing required at 0x%04x",(int)bh);
3972 // We pretend that bh is now dead.
3973 LDV_recordDead_FILL_SLOP_DYNAMIC((StgClosure *)bh);
3975 SET_INFO(bh,&stg_BLACKHOLE_info);
3977 // We pretend that bh has just been created.
3978 LDV_RECORD_CREATE(bh);
3981 frame = (StgClosure *) ((StgUpdateFrame *)frame + 1);
3987 // normal stack frames; do nothing except advance the pointer
3989 frame = (StgClosure *)((StgPtr)frame + stack_frame_sizeW(frame));
3995 /* -----------------------------------------------------------------------------
3998 * Code largely pinched from old RTS, then hacked to bits. We also do
3999 * lazy black holing here.
4001 * -------------------------------------------------------------------------- */
4003 struct stack_gap { StgWord gap_size; struct stack_gap *next_gap; };
4006 threadSqueezeStack(StgTSO *tso)
4009 rtsBool prev_was_update_frame;
4010 StgClosure *updatee = NULL;
4012 StgRetInfoTable *info;
4013 StgWord current_gap_size;
4014 struct stack_gap *gap;
4017 // Traverse the stack upwards, replacing adjacent update frames
4018 // with a single update frame and a "stack gap". A stack gap
4019 // contains two values: the size of the gap, and the distance
4020 // to the next gap (or the stack top).
4022 bottom = &(tso->stack[tso->stack_size]);
4026 ASSERT(frame < bottom);
4028 prev_was_update_frame = rtsFalse;
4029 current_gap_size = 0;
4030 gap = (struct stack_gap *) (tso->sp - sizeofW(StgUpdateFrame));
4032 while (frame < bottom) {
4034 info = get_ret_itbl((StgClosure *)frame);
4035 switch (info->i.type) {
4039 StgUpdateFrame *upd = (StgUpdateFrame *)frame;
4041 if (upd->updatee->header.info == &stg_BLACKHOLE_info) {
4043 // found a BLACKHOLE'd update frame; we've been here
4044 // before, in a previous GC, so just break out.
4046 // Mark the end of the gap, if we're in one.
4047 if (current_gap_size != 0) {
4048 gap = (struct stack_gap *)(frame-sizeofW(StgUpdateFrame));
4051 frame += sizeofW(StgUpdateFrame);
4052 goto done_traversing;
4055 if (prev_was_update_frame) {
4057 TICK_UPD_SQUEEZED();
4058 /* wasn't there something about update squeezing and ticky to be
4059 * sorted out? oh yes: we aren't counting each enter properly
4060 * in this case. See the log somewhere. KSW 1999-04-21
4062 * Check two things: that the two update frames don't point to
4063 * the same object, and that the updatee_bypass isn't already an
4064 * indirection. Both of these cases only happen when we're in a
4065 * block hole-style loop (and there are multiple update frames
4066 * on the stack pointing to the same closure), but they can both
4067 * screw us up if we don't check.
4069 if (upd->updatee != updatee && !closure_IND(upd->updatee)) {
4070 // this wakes the threads up
4071 UPD_IND_NOLOCK(upd->updatee, updatee);
4074 // now mark this update frame as a stack gap. The gap
4075 // marker resides in the bottom-most update frame of
4076 // the series of adjacent frames, and covers all the
4077 // frames in this series.
4078 current_gap_size += sizeofW(StgUpdateFrame);
4079 ((struct stack_gap *)frame)->gap_size = current_gap_size;
4080 ((struct stack_gap *)frame)->next_gap = gap;
4082 frame += sizeofW(StgUpdateFrame);
4086 // single update frame, or the topmost update frame in a series
4088 StgBlockingQueue *bh = (StgBlockingQueue *)upd->updatee;
4090 // Do lazy black-holing
4091 if (bh->header.info != &stg_BLACKHOLE_info &&
4092 bh->header.info != &stg_BLACKHOLE_BQ_info &&
4093 bh->header.info != &stg_CAF_BLACKHOLE_info) {
4094 #if (!defined(LAZY_BLACKHOLING)) && defined(DEBUG)
4095 debugBelch("Unexpected lazy BHing required at 0x%04x",(int)bh);
4098 /* zero out the slop so that the sanity checker can tell
4099 * where the next closure is.
4102 StgInfoTable *bh_info = get_itbl(bh);
4103 nat np = bh_info->layout.payload.ptrs,
4104 nw = bh_info->layout.payload.nptrs, i;
4105 /* don't zero out slop for a THUNK_SELECTOR,
4106 * because its layout info is used for a
4107 * different purpose, and it's exactly the
4108 * same size as a BLACKHOLE in any case.
4110 if (bh_info->type != THUNK_SELECTOR) {
4111 for (i = 0; i < np + nw; i++) {
4112 ((StgClosure *)bh)->payload[i] = INVALID_OBJECT;
4118 // We pretend that bh is now dead.
4119 LDV_recordDead_FILL_SLOP_DYNAMIC((StgClosure *)bh);
4121 // Todo: maybe use SET_HDR() and remove LDV_RECORD_CREATE()?
4122 SET_INFO(bh,&stg_BLACKHOLE_info);
4124 // We pretend that bh has just been created.
4125 LDV_RECORD_CREATE(bh);
4128 prev_was_update_frame = rtsTrue;
4129 updatee = upd->updatee;
4130 frame += sizeofW(StgUpdateFrame);
4136 prev_was_update_frame = rtsFalse;
4138 // we're not in a gap... check whether this is the end of a gap
4139 // (an update frame can't be the end of a gap).
4140 if (current_gap_size != 0) {
4141 gap = (struct stack_gap *) (frame - sizeofW(StgUpdateFrame));
4143 current_gap_size = 0;
4145 frame += stack_frame_sizeW((StgClosure *)frame);
4152 // Now we have a stack with gaps in it, and we have to walk down
4153 // shoving the stack up to fill in the gaps. A diagram might
4157 // | ********* | <- sp
4161 // | stack_gap | <- gap | chunk_size
4163 // | ......... | <- gap_end v
4169 // 'sp' points the the current top-of-stack
4170 // 'gap' points to the stack_gap structure inside the gap
4171 // ***** indicates real stack data
4172 // ..... indicates gap
4173 // <empty> indicates unused
4177 void *gap_start, *next_gap_start, *gap_end;
4180 next_gap_start = (void *)((unsigned char*)gap + sizeof(StgUpdateFrame));
4181 sp = next_gap_start;
4183 while ((StgPtr)gap > tso->sp) {
4185 // we're working in *bytes* now...
4186 gap_start = next_gap_start;
4187 gap_end = (void*) ((unsigned char*)gap_start - gap->gap_size * sizeof(W_));
4189 gap = gap->next_gap;
4190 next_gap_start = (void *)((unsigned char*)gap + sizeof(StgUpdateFrame));
4192 chunk_size = (unsigned char*)gap_end - (unsigned char*)next_gap_start;
4194 memmove(sp, next_gap_start, chunk_size);
4197 tso->sp = (StgPtr)sp;
4201 /* -----------------------------------------------------------------------------
4204 * We have to prepare for GC - this means doing lazy black holing
4205 * here. We also take the opportunity to do stack squeezing if it's
4207 * -------------------------------------------------------------------------- */
4209 threadPaused(StgTSO *tso)
4211 if ( RtsFlags.GcFlags.squeezeUpdFrames == rtsTrue )
4212 threadSqueezeStack(tso); // does black holing too
4214 threadLazyBlackHole(tso);
4217 /* -----------------------------------------------------------------------------
4219 * -------------------------------------------------------------------------- */
4223 printMutableList(generation *gen)
4228 debugBelch("@@ Mutable list %p: ", gen->mut_list);
4230 for (bd = gen->mut_list; bd != NULL; bd = bd->link) {
4231 for (p = bd->start; p < bd->free; p++) {
4232 debugBelch("%p (%s), ", (void *)*p, info_type((StgClosure *)*p));
4238 STATIC_INLINE rtsBool
4239 maybeLarge(StgClosure *closure)
4241 StgInfoTable *info = get_itbl(closure);
4243 /* closure types that may be found on the new_large_objects list;
4244 see scavenge_large */
4245 return (info->type == MUT_ARR_PTRS ||
4246 info->type == MUT_ARR_PTRS_FROZEN ||
4247 info->type == MUT_ARR_PTRS_FROZEN0 ||
4248 info->type == TSO ||
4249 info->type == ARR_WORDS);