1 /* -----------------------------------------------------------------------------
2 * $Id: GC.c,v 1.165 2004/05/07 21:19:21 panne Exp $
4 * (c) The GHC Team 1998-2003
6 * Generational garbage collector
8 * ---------------------------------------------------------------------------*/
10 #include "PosixSource.h"
16 #include "StoragePriv.h"
19 #include "SchedAPI.h" // for ReverCAFs prototype
21 #include "BlockAlloc.h"
26 #include "StablePriv.h"
28 #include "ParTicky.h" // ToDo: move into Rts.h
29 #include "GCCompact.h"
31 #if defined(GRAN) || defined(PAR)
32 # include "GranSimRts.h"
33 # include "ParallelRts.h"
37 # include "ParallelDebug.h"
42 #if defined(RTS_GTK_FRONTPANEL)
43 #include "FrontPanel.h"
46 #include "RetainerProfile.h"
47 #include "LdvProfile.h"
51 /* STATIC OBJECT LIST.
54 * We maintain a linked list of static objects that are still live.
55 * The requirements for this list are:
57 * - we need to scan the list while adding to it, in order to
58 * scavenge all the static objects (in the same way that
59 * breadth-first scavenging works for dynamic objects).
61 * - we need to be able to tell whether an object is already on
62 * the list, to break loops.
64 * Each static object has a "static link field", which we use for
65 * linking objects on to the list. We use a stack-type list, consing
66 * objects on the front as they are added (this means that the
67 * scavenge phase is depth-first, not breadth-first, but that
70 * A separate list is kept for objects that have been scavenged
71 * already - this is so that we can zero all the marks afterwards.
73 * An object is on the list if its static link field is non-zero; this
74 * means that we have to mark the end of the list with '1', not NULL.
76 * Extra notes for generational GC:
78 * Each generation has a static object list associated with it. When
79 * collecting generations up to N, we treat the static object lists
80 * from generations > N as roots.
82 * We build up a static object list while collecting generations 0..N,
83 * which is then appended to the static object list of generation N+1.
85 static StgClosure* static_objects; // live static objects
86 StgClosure* scavenged_static_objects; // static objects scavenged so far
88 /* N is the oldest generation being collected, where the generations
89 * are numbered starting at 0. A major GC (indicated by the major_gc
90 * flag) is when we're collecting all generations. We only attempt to
91 * deal with static objects and GC CAFs when doing a major GC.
94 static rtsBool major_gc;
96 /* Youngest generation that objects should be evacuated to in
97 * evacuate(). (Logically an argument to evacuate, but it's static
98 * a lot of the time so we optimise it into a global variable).
104 StgWeak *old_weak_ptr_list; // also pending finaliser list
106 /* Which stage of processing various kinds of weak pointer are we at?
107 * (see traverse_weak_ptr_list() below for discussion).
109 typedef enum { WeakPtrs, WeakThreads, WeakDone } WeakStage;
110 static WeakStage weak_stage;
112 /* List of all threads during GC
114 static StgTSO *old_all_threads;
115 StgTSO *resurrected_threads;
117 /* Flag indicating failure to evacuate an object to the desired
120 static rtsBool failed_to_evac;
122 /* Old to-space (used for two-space collector only)
124 static bdescr *old_to_blocks;
126 /* Data used for allocation area sizing.
128 static lnat new_blocks; // blocks allocated during this GC
129 static lnat g0s0_pcnt_kept = 30; // percentage of g0s0 live at last minor GC
131 /* Used to avoid long recursion due to selector thunks
133 static lnat thunk_selector_depth = 0;
134 #define MAX_THUNK_SELECTOR_DEPTH 8
136 /* -----------------------------------------------------------------------------
137 Static function declarations
138 -------------------------------------------------------------------------- */
140 static bdescr * gc_alloc_block ( step *stp );
141 static void mark_root ( StgClosure **root );
143 // Use a register argument for evacuate, if available.
145 #define REGPARM1 __attribute__((regparm(1)))
150 REGPARM1 static StgClosure * evacuate (StgClosure *q);
152 static void zero_static_object_list ( StgClosure* first_static );
153 static void zero_mutable_list ( StgMutClosure *first );
155 static rtsBool traverse_weak_ptr_list ( void );
156 static void mark_weak_ptr_list ( StgWeak **list );
158 static StgClosure * eval_thunk_selector ( nat field, StgSelector * p );
161 static void scavenge ( step * );
162 static void scavenge_mark_stack ( void );
163 static void scavenge_stack ( StgPtr p, StgPtr stack_end );
164 static rtsBool scavenge_one ( StgPtr p );
165 static void scavenge_large ( step * );
166 static void scavenge_static ( void );
167 static void scavenge_mutable_list ( generation *g );
168 static void scavenge_mut_once_list ( generation *g );
170 static void scavenge_large_bitmap ( StgPtr p,
171 StgLargeBitmap *large_bitmap,
174 #if 0 && defined(DEBUG)
175 static void gcCAFs ( void );
178 /* -----------------------------------------------------------------------------
179 inline functions etc. for dealing with the mark bitmap & stack.
180 -------------------------------------------------------------------------- */
182 #define MARK_STACK_BLOCKS 4
184 static bdescr *mark_stack_bdescr;
185 static StgPtr *mark_stack;
186 static StgPtr *mark_sp;
187 static StgPtr *mark_splim;
189 // Flag and pointers used for falling back to a linear scan when the
190 // mark stack overflows.
191 static rtsBool mark_stack_overflowed;
192 static bdescr *oldgen_scan_bd;
193 static StgPtr oldgen_scan;
195 STATIC_INLINE rtsBool
196 mark_stack_empty(void)
198 return mark_sp == mark_stack;
201 STATIC_INLINE rtsBool
202 mark_stack_full(void)
204 return mark_sp >= mark_splim;
208 reset_mark_stack(void)
210 mark_sp = mark_stack;
214 push_mark_stack(StgPtr p)
225 /* -----------------------------------------------------------------------------
226 Allocate a new to-space block in the given step.
227 -------------------------------------------------------------------------- */
230 gc_alloc_block(step *stp)
232 bdescr *bd = allocBlock();
233 bd->gen_no = stp->gen_no;
237 // blocks in to-space in generations up to and including N
238 // get the BF_EVACUATED flag.
239 if (stp->gen_no <= N) {
240 bd->flags = BF_EVACUATED;
245 // Start a new to-space block, chain it on after the previous one.
246 if (stp->hp_bd == NULL) {
249 stp->hp_bd->free = stp->hp;
250 stp->hp_bd->link = bd;
255 stp->hpLim = stp->hp + BLOCK_SIZE_W;
263 /* -----------------------------------------------------------------------------
266 Rough outline of the algorithm: for garbage collecting generation N
267 (and all younger generations):
269 - follow all pointers in the root set. the root set includes all
270 mutable objects in all generations (mutable_list and mut_once_list).
272 - for each pointer, evacuate the object it points to into either
274 + to-space of the step given by step->to, which is the next
275 highest step in this generation or the first step in the next
276 generation if this is the last step.
278 + to-space of generations[evac_gen]->steps[0], if evac_gen != 0.
279 When we evacuate an object we attempt to evacuate
280 everything it points to into the same generation - this is
281 achieved by setting evac_gen to the desired generation. If
282 we can't do this, then an entry in the mut_once list has to
283 be made for the cross-generation pointer.
285 + if the object is already in a generation > N, then leave
288 - repeatedly scavenge to-space from each step in each generation
289 being collected until no more objects can be evacuated.
291 - free from-space in each step, and set from-space = to-space.
293 Locks held: sched_mutex
295 -------------------------------------------------------------------------- */
298 GarbageCollect ( void (*get_roots)(evac_fn), rtsBool force_major_gc )
302 lnat live, allocated, collected = 0, copied = 0;
303 lnat oldgen_saved_blocks = 0;
307 CostCentreStack *prev_CCS;
310 #if defined(DEBUG) && defined(GRAN)
311 IF_DEBUG(gc, belch("@@ Starting garbage collection at %ld (%lx)\n",
315 #if defined(RTS_USER_SIGNALS)
320 // tell the stats department that we've started a GC
323 // Init stats and print par specific (timing) info
324 PAR_TICKY_PAR_START();
326 // attribute any costs to CCS_GC
332 /* Approximate how much we allocated.
333 * Todo: only when generating stats?
335 allocated = calcAllocated();
337 /* Figure out which generation to collect
339 if (force_major_gc) {
340 N = RtsFlags.GcFlags.generations - 1;
344 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
345 if (generations[g].steps[0].n_blocks +
346 generations[g].steps[0].n_large_blocks
347 >= generations[g].max_blocks) {
351 major_gc = (N == RtsFlags.GcFlags.generations-1);
354 #ifdef RTS_GTK_FRONTPANEL
355 if (RtsFlags.GcFlags.frontpanel) {
356 updateFrontPanelBeforeGC(N);
360 // check stack sanity *before* GC (ToDo: check all threads)
362 // ToDo!: check sanity IF_DEBUG(sanity, checkTSOsSanity());
364 IF_DEBUG(sanity, checkFreeListSanity());
366 /* Initialise the static object lists
368 static_objects = END_OF_STATIC_LIST;
369 scavenged_static_objects = END_OF_STATIC_LIST;
371 /* zero the mutable list for the oldest generation (see comment by
372 * zero_mutable_list below).
375 zero_mutable_list(generations[RtsFlags.GcFlags.generations-1].mut_once_list);
378 /* Save the old to-space if we're doing a two-space collection
380 if (RtsFlags.GcFlags.generations == 1) {
381 old_to_blocks = g0s0->to_blocks;
382 g0s0->to_blocks = NULL;
383 g0s0->n_to_blocks = 0;
386 /* Keep a count of how many new blocks we allocated during this GC
387 * (used for resizing the allocation area, later).
391 // Initialise to-space in all the generations/steps that we're
394 for (g = 0; g <= N; g++) {
395 generations[g].mut_once_list = END_MUT_LIST;
396 generations[g].mut_list = END_MUT_LIST;
398 for (s = 0; s < generations[g].n_steps; s++) {
400 // generation 0, step 0 doesn't need to-space
401 if (g == 0 && s == 0 && RtsFlags.GcFlags.generations > 1) {
405 stp = &generations[g].steps[s];
406 ASSERT(stp->gen_no == g);
408 // start a new to-space for this step.
411 stp->to_blocks = NULL;
413 // allocate the first to-space block; extra blocks will be
414 // chained on as necessary.
415 bd = gc_alloc_block(stp);
417 stp->scan = bd->start;
420 // initialise the large object queues.
421 stp->new_large_objects = NULL;
422 stp->scavenged_large_objects = NULL;
423 stp->n_scavenged_large_blocks = 0;
425 // mark the large objects as not evacuated yet
426 for (bd = stp->large_objects; bd; bd = bd->link) {
427 bd->flags &= ~BF_EVACUATED;
430 // for a compacted step, we need to allocate the bitmap
431 if (stp->is_compacted) {
432 nat bitmap_size; // in bytes
433 bdescr *bitmap_bdescr;
436 bitmap_size = stp->n_blocks * BLOCK_SIZE / (sizeof(W_)*BITS_PER_BYTE);
438 if (bitmap_size > 0) {
439 bitmap_bdescr = allocGroup((nat)BLOCK_ROUND_UP(bitmap_size)
441 stp->bitmap = bitmap_bdescr;
442 bitmap = bitmap_bdescr->start;
444 IF_DEBUG(gc, belch("bitmap_size: %d, bitmap: %p",
445 bitmap_size, bitmap););
447 // don't forget to fill it with zeros!
448 memset(bitmap, 0, bitmap_size);
450 // for each block in this step, point to its bitmap from the
452 for (bd=stp->blocks; bd != NULL; bd = bd->link) {
453 bd->u.bitmap = bitmap;
454 bitmap += BLOCK_SIZE_W / (sizeof(W_)*BITS_PER_BYTE);
461 /* make sure the older generations have at least one block to
462 * allocate into (this makes things easier for copy(), see below).
464 for (g = N+1; g < RtsFlags.GcFlags.generations; g++) {
465 for (s = 0; s < generations[g].n_steps; s++) {
466 stp = &generations[g].steps[s];
467 if (stp->hp_bd == NULL) {
468 ASSERT(stp->blocks == NULL);
469 bd = gc_alloc_block(stp);
473 /* Set the scan pointer for older generations: remember we
474 * still have to scavenge objects that have been promoted. */
476 stp->scan_bd = stp->hp_bd;
477 stp->to_blocks = NULL;
478 stp->n_to_blocks = 0;
479 stp->new_large_objects = NULL;
480 stp->scavenged_large_objects = NULL;
481 stp->n_scavenged_large_blocks = 0;
485 /* Allocate a mark stack if we're doing a major collection.
488 mark_stack_bdescr = allocGroup(MARK_STACK_BLOCKS);
489 mark_stack = (StgPtr *)mark_stack_bdescr->start;
490 mark_sp = mark_stack;
491 mark_splim = mark_stack + (MARK_STACK_BLOCKS * BLOCK_SIZE_W);
493 mark_stack_bdescr = NULL;
496 /* -----------------------------------------------------------------------
497 * follow all the roots that we know about:
498 * - mutable lists from each generation > N
499 * we want to *scavenge* these roots, not evacuate them: they're not
500 * going to move in this GC.
501 * Also: do them in reverse generation order. This is because we
502 * often want to promote objects that are pointed to by older
503 * generations early, so we don't have to repeatedly copy them.
504 * Doing the generations in reverse order ensures that we don't end
505 * up in the situation where we want to evac an object to gen 3 and
506 * it has already been evaced to gen 2.
510 for (g = RtsFlags.GcFlags.generations-1; g > N; g--) {
511 generations[g].saved_mut_list = generations[g].mut_list;
512 generations[g].mut_list = END_MUT_LIST;
515 // Do the mut-once lists first
516 for (g = RtsFlags.GcFlags.generations-1; g > N; g--) {
517 IF_PAR_DEBUG(verbose,
518 printMutOnceList(&generations[g]));
519 scavenge_mut_once_list(&generations[g]);
521 for (st = generations[g].n_steps-1; st >= 0; st--) {
522 scavenge(&generations[g].steps[st]);
526 for (g = RtsFlags.GcFlags.generations-1; g > N; g--) {
527 IF_PAR_DEBUG(verbose,
528 printMutableList(&generations[g]));
529 scavenge_mutable_list(&generations[g]);
531 for (st = generations[g].n_steps-1; st >= 0; st--) {
532 scavenge(&generations[g].steps[st]);
537 /* follow roots from the CAF list (used by GHCi)
542 /* follow all the roots that the application knows about.
545 get_roots(mark_root);
548 /* And don't forget to mark the TSO if we got here direct from
550 /* Not needed in a seq version?
552 CurrentTSO = (StgTSO *)MarkRoot((StgClosure *)CurrentTSO);
556 // Mark the entries in the GALA table of the parallel system
557 markLocalGAs(major_gc);
558 // Mark all entries on the list of pending fetches
559 markPendingFetches(major_gc);
562 /* Mark the weak pointer list, and prepare to detect dead weak
565 mark_weak_ptr_list(&weak_ptr_list);
566 old_weak_ptr_list = weak_ptr_list;
567 weak_ptr_list = NULL;
568 weak_stage = WeakPtrs;
570 /* The all_threads list is like the weak_ptr_list.
571 * See traverse_weak_ptr_list() for the details.
573 old_all_threads = all_threads;
574 all_threads = END_TSO_QUEUE;
575 resurrected_threads = END_TSO_QUEUE;
577 /* Mark the stable pointer table.
579 markStablePtrTable(mark_root);
583 /* ToDo: To fix the caf leak, we need to make the commented out
584 * parts of this code do something sensible - as described in
587 extern void markHugsObjects(void);
592 /* -------------------------------------------------------------------------
593 * Repeatedly scavenge all the areas we know about until there's no
594 * more scavenging to be done.
601 // scavenge static objects
602 if (major_gc && static_objects != END_OF_STATIC_LIST) {
603 IF_DEBUG(sanity, checkStaticObjects(static_objects));
607 /* When scavenging the older generations: Objects may have been
608 * evacuated from generations <= N into older generations, and we
609 * need to scavenge these objects. We're going to try to ensure that
610 * any evacuations that occur move the objects into at least the
611 * same generation as the object being scavenged, otherwise we
612 * have to create new entries on the mutable list for the older
616 // scavenge each step in generations 0..maxgen
622 // scavenge objects in compacted generation
623 if (mark_stack_overflowed || oldgen_scan_bd != NULL ||
624 (mark_stack_bdescr != NULL && !mark_stack_empty())) {
625 scavenge_mark_stack();
629 for (gen = RtsFlags.GcFlags.generations; --gen >= 0; ) {
630 for (st = generations[gen].n_steps; --st >= 0; ) {
631 if (gen == 0 && st == 0 && RtsFlags.GcFlags.generations > 1) {
634 stp = &generations[gen].steps[st];
636 if (stp->hp_bd != stp->scan_bd || stp->scan < stp->hp) {
641 if (stp->new_large_objects != NULL) {
650 if (flag) { goto loop; }
652 // must be last... invariant is that everything is fully
653 // scavenged at this point.
654 if (traverse_weak_ptr_list()) { // returns rtsTrue if evaced something
659 /* Update the pointers from the "main thread" list - these are
660 * treated as weak pointers because we want to allow a main thread
661 * to get a BlockedOnDeadMVar exception in the same way as any other
662 * thread. Note that the threads should all have been retained by
663 * GC by virtue of being on the all_threads list, we're just
664 * updating pointers here.
669 for (m = main_threads; m != NULL; m = m->link) {
670 tso = (StgTSO *) isAlive((StgClosure *)m->tso);
672 barf("main thread has been GC'd");
679 // Reconstruct the Global Address tables used in GUM
680 rebuildGAtables(major_gc);
681 IF_DEBUG(sanity, checkLAGAtable(rtsTrue/*check closures, too*/));
684 // Now see which stable names are still alive.
687 // Tidy the end of the to-space chains
688 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
689 for (s = 0; s < generations[g].n_steps; s++) {
690 stp = &generations[g].steps[s];
691 if (!(g == 0 && s == 0 && RtsFlags.GcFlags.generations > 1)) {
692 ASSERT(Bdescr(stp->hp) == stp->hp_bd);
693 stp->hp_bd->free = stp->hp;
699 // We call processHeapClosureForDead() on every closure destroyed during
700 // the current garbage collection, so we invoke LdvCensusForDead().
701 if (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_LDV
702 || RtsFlags.ProfFlags.bioSelector != NULL)
706 // NO MORE EVACUATION AFTER THIS POINT!
707 // Finally: compaction of the oldest generation.
708 if (major_gc && oldest_gen->steps[0].is_compacted) {
709 // save number of blocks for stats
710 oldgen_saved_blocks = oldest_gen->steps[0].n_blocks;
714 IF_DEBUG(sanity, checkGlobalTSOList(rtsFalse));
716 /* run through all the generations/steps and tidy up
718 copied = new_blocks * BLOCK_SIZE_W;
719 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
722 generations[g].collections++; // for stats
725 for (s = 0; s < generations[g].n_steps; s++) {
727 stp = &generations[g].steps[s];
729 if (!(g == 0 && s == 0 && RtsFlags.GcFlags.generations > 1)) {
730 // stats information: how much we copied
732 copied -= stp->hp_bd->start + BLOCK_SIZE_W -
737 // for generations we collected...
740 // rough calculation of garbage collected, for stats output
741 if (stp->is_compacted) {
742 collected += (oldgen_saved_blocks - stp->n_blocks) * BLOCK_SIZE_W;
744 collected += stp->n_blocks * BLOCK_SIZE_W;
747 /* free old memory and shift to-space into from-space for all
748 * the collected steps (except the allocation area). These
749 * freed blocks will probaby be quickly recycled.
751 if (!(g == 0 && s == 0)) {
752 if (stp->is_compacted) {
753 // for a compacted step, just shift the new to-space
754 // onto the front of the now-compacted existing blocks.
755 for (bd = stp->to_blocks; bd != NULL; bd = bd->link) {
756 bd->flags &= ~BF_EVACUATED; // now from-space
757 bd->flags |= BF_COMPACTED; // compacted next time
759 // tack the new blocks on the end of the existing blocks
760 if (stp->blocks == NULL) {
761 stp->blocks = stp->to_blocks;
763 for (bd = stp->blocks; bd != NULL; bd = next) {
766 bd->link = stp->to_blocks;
770 // add the new blocks to the block tally
771 stp->n_blocks += stp->n_to_blocks;
773 freeChain(stp->blocks);
774 stp->blocks = stp->to_blocks;
775 stp->n_blocks = stp->n_to_blocks;
776 for (bd = stp->blocks; bd != NULL; bd = bd->link) {
777 bd->flags &= ~BF_EVACUATED; // now from-space
780 stp->to_blocks = NULL;
781 stp->n_to_blocks = 0;
784 /* LARGE OBJECTS. The current live large objects are chained on
785 * scavenged_large, having been moved during garbage
786 * collection from large_objects. Any objects left on
787 * large_objects list are therefore dead, so we free them here.
789 for (bd = stp->large_objects; bd != NULL; bd = next) {
795 // update the count of blocks used by large objects
796 for (bd = stp->scavenged_large_objects; bd != NULL; bd = bd->link) {
797 bd->flags &= ~BF_EVACUATED;
799 stp->large_objects = stp->scavenged_large_objects;
800 stp->n_large_blocks = stp->n_scavenged_large_blocks;
803 // for older generations...
805 /* For older generations, we need to append the
806 * scavenged_large_object list (i.e. large objects that have been
807 * promoted during this GC) to the large_object list for that step.
809 for (bd = stp->scavenged_large_objects; bd; bd = next) {
811 bd->flags &= ~BF_EVACUATED;
812 dbl_link_onto(bd, &stp->large_objects);
815 // add the new blocks we promoted during this GC
816 stp->n_blocks += stp->n_to_blocks;
817 stp->n_to_blocks = 0;
818 stp->n_large_blocks += stp->n_scavenged_large_blocks;
823 /* Reset the sizes of the older generations when we do a major
826 * CURRENT STRATEGY: make all generations except zero the same size.
827 * We have to stay within the maximum heap size, and leave a certain
828 * percentage of the maximum heap size available to allocate into.
830 if (major_gc && RtsFlags.GcFlags.generations > 1) {
831 nat live, size, min_alloc;
832 nat max = RtsFlags.GcFlags.maxHeapSize;
833 nat gens = RtsFlags.GcFlags.generations;
835 // live in the oldest generations
836 live = oldest_gen->steps[0].n_blocks +
837 oldest_gen->steps[0].n_large_blocks;
839 // default max size for all generations except zero
840 size = stg_max(live * RtsFlags.GcFlags.oldGenFactor,
841 RtsFlags.GcFlags.minOldGenSize);
843 // minimum size for generation zero
844 min_alloc = stg_max((RtsFlags.GcFlags.pcFreeHeap * max) / 200,
845 RtsFlags.GcFlags.minAllocAreaSize);
847 // Auto-enable compaction when the residency reaches a
848 // certain percentage of the maximum heap size (default: 30%).
849 if (RtsFlags.GcFlags.generations > 1 &&
850 (RtsFlags.GcFlags.compact ||
852 oldest_gen->steps[0].n_blocks >
853 (RtsFlags.GcFlags.compactThreshold * max) / 100))) {
854 oldest_gen->steps[0].is_compacted = 1;
855 // fprintf(stderr,"compaction: on\n", live);
857 oldest_gen->steps[0].is_compacted = 0;
858 // fprintf(stderr,"compaction: off\n", live);
861 // if we're going to go over the maximum heap size, reduce the
862 // size of the generations accordingly. The calculation is
863 // different if compaction is turned on, because we don't need
864 // to double the space required to collect the old generation.
867 // this test is necessary to ensure that the calculations
868 // below don't have any negative results - we're working
869 // with unsigned values here.
870 if (max < min_alloc) {
874 if (oldest_gen->steps[0].is_compacted) {
875 if ( (size + (size - 1) * (gens - 2) * 2) + min_alloc > max ) {
876 size = (max - min_alloc) / ((gens - 1) * 2 - 1);
879 if ( (size * (gens - 1) * 2) + min_alloc > max ) {
880 size = (max - min_alloc) / ((gens - 1) * 2);
890 fprintf(stderr,"live: %d, min_alloc: %d, size : %d, max = %d\n", live,
891 min_alloc, size, max);
894 for (g = 0; g < gens; g++) {
895 generations[g].max_blocks = size;
899 // Guess the amount of live data for stats.
902 /* Free the small objects allocated via allocate(), since this will
903 * all have been copied into G0S1 now.
905 if (small_alloc_list != NULL) {
906 freeChain(small_alloc_list);
908 small_alloc_list = NULL;
912 alloc_blocks_lim = RtsFlags.GcFlags.minAllocAreaSize;
914 // Start a new pinned_object_block
915 pinned_object_block = NULL;
917 /* Free the mark stack.
919 if (mark_stack_bdescr != NULL) {
920 freeGroup(mark_stack_bdescr);
925 for (g = 0; g <= N; g++) {
926 for (s = 0; s < generations[g].n_steps; s++) {
927 stp = &generations[g].steps[s];
928 if (stp->is_compacted && stp->bitmap != NULL) {
929 freeGroup(stp->bitmap);
934 /* Two-space collector:
935 * Free the old to-space, and estimate the amount of live data.
937 if (RtsFlags.GcFlags.generations == 1) {
940 if (old_to_blocks != NULL) {
941 freeChain(old_to_blocks);
943 for (bd = g0s0->to_blocks; bd != NULL; bd = bd->link) {
944 bd->flags = 0; // now from-space
947 /* For a two-space collector, we need to resize the nursery. */
949 /* set up a new nursery. Allocate a nursery size based on a
950 * function of the amount of live data (by default a factor of 2)
951 * Use the blocks from the old nursery if possible, freeing up any
954 * If we get near the maximum heap size, then adjust our nursery
955 * size accordingly. If the nursery is the same size as the live
956 * data (L), then we need 3L bytes. We can reduce the size of the
957 * nursery to bring the required memory down near 2L bytes.
959 * A normal 2-space collector would need 4L bytes to give the same
960 * performance we get from 3L bytes, reducing to the same
961 * performance at 2L bytes.
963 blocks = g0s0->n_to_blocks;
965 if ( RtsFlags.GcFlags.maxHeapSize != 0 &&
966 blocks * RtsFlags.GcFlags.oldGenFactor * 2 >
967 RtsFlags.GcFlags.maxHeapSize ) {
968 long adjusted_blocks; // signed on purpose
971 adjusted_blocks = (RtsFlags.GcFlags.maxHeapSize - 2 * blocks);
972 IF_DEBUG(gc, belch("@@ Near maximum heap size of 0x%x blocks, blocks = %d, adjusted to %ld", RtsFlags.GcFlags.maxHeapSize, blocks, adjusted_blocks));
973 pc_free = adjusted_blocks * 100 / RtsFlags.GcFlags.maxHeapSize;
974 if (pc_free < RtsFlags.GcFlags.pcFreeHeap) /* might even be < 0 */ {
977 blocks = adjusted_blocks;
980 blocks *= RtsFlags.GcFlags.oldGenFactor;
981 if (blocks < RtsFlags.GcFlags.minAllocAreaSize) {
982 blocks = RtsFlags.GcFlags.minAllocAreaSize;
985 resizeNursery(blocks);
988 /* Generational collector:
989 * If the user has given us a suggested heap size, adjust our
990 * allocation area to make best use of the memory available.
993 if (RtsFlags.GcFlags.heapSizeSuggestion) {
995 nat needed = calcNeeded(); // approx blocks needed at next GC
997 /* Guess how much will be live in generation 0 step 0 next time.
998 * A good approximation is obtained by finding the
999 * percentage of g0s0 that was live at the last minor GC.
1002 g0s0_pcnt_kept = (new_blocks * 100) / g0s0->n_blocks;
1005 /* Estimate a size for the allocation area based on the
1006 * information available. We might end up going slightly under
1007 * or over the suggested heap size, but we should be pretty
1010 * Formula: suggested - needed
1011 * ----------------------------
1012 * 1 + g0s0_pcnt_kept/100
1014 * where 'needed' is the amount of memory needed at the next
1015 * collection for collecting all steps except g0s0.
1018 (((long)RtsFlags.GcFlags.heapSizeSuggestion - (long)needed) * 100) /
1019 (100 + (long)g0s0_pcnt_kept);
1021 if (blocks < (long)RtsFlags.GcFlags.minAllocAreaSize) {
1022 blocks = RtsFlags.GcFlags.minAllocAreaSize;
1025 resizeNursery((nat)blocks);
1028 // we might have added extra large blocks to the nursery, so
1029 // resize back to minAllocAreaSize again.
1030 resizeNursery(RtsFlags.GcFlags.minAllocAreaSize);
1034 // mark the garbage collected CAFs as dead
1035 #if 0 && defined(DEBUG) // doesn't work at the moment
1036 if (major_gc) { gcCAFs(); }
1040 // resetStaticObjectForRetainerProfiling() must be called before
1042 resetStaticObjectForRetainerProfiling();
1045 // zero the scavenged static object list
1047 zero_static_object_list(scavenged_static_objects);
1050 // Reset the nursery
1053 RELEASE_LOCK(&sched_mutex);
1055 // start any pending finalizers
1056 scheduleFinalizers(old_weak_ptr_list);
1058 // send exceptions to any threads which were about to die
1059 resurrectThreads(resurrected_threads);
1061 ACQUIRE_LOCK(&sched_mutex);
1063 // Update the stable pointer hash table.
1064 updateStablePtrTable(major_gc);
1066 // check sanity after GC
1067 IF_DEBUG(sanity, checkSanity());
1069 // extra GC trace info
1070 IF_DEBUG(gc, statDescribeGens());
1073 // symbol-table based profiling
1074 /* heapCensus(to_blocks); */ /* ToDo */
1077 // restore enclosing cost centre
1082 // check for memory leaks if sanity checking is on
1083 IF_DEBUG(sanity, memInventory());
1085 #ifdef RTS_GTK_FRONTPANEL
1086 if (RtsFlags.GcFlags.frontpanel) {
1087 updateFrontPanelAfterGC( N, live );
1091 // ok, GC over: tell the stats department what happened.
1092 stat_endGC(allocated, collected, live, copied, N);
1094 #if defined(RTS_USER_SIGNALS)
1095 // unblock signals again
1096 unblockUserSignals();
1103 /* -----------------------------------------------------------------------------
1106 traverse_weak_ptr_list is called possibly many times during garbage
1107 collection. It returns a flag indicating whether it did any work
1108 (i.e. called evacuate on any live pointers).
1110 Invariant: traverse_weak_ptr_list is called when the heap is in an
1111 idempotent state. That means that there are no pending
1112 evacuate/scavenge operations. This invariant helps the weak
1113 pointer code decide which weak pointers are dead - if there are no
1114 new live weak pointers, then all the currently unreachable ones are
1117 For generational GC: we just don't try to finalize weak pointers in
1118 older generations than the one we're collecting. This could
1119 probably be optimised by keeping per-generation lists of weak
1120 pointers, but for a few weak pointers this scheme will work.
1122 There are three distinct stages to processing weak pointers:
1124 - weak_stage == WeakPtrs
1126 We process all the weak pointers whos keys are alive (evacuate
1127 their values and finalizers), and repeat until we can find no new
1128 live keys. If no live keys are found in this pass, then we
1129 evacuate the finalizers of all the dead weak pointers in order to
1132 - weak_stage == WeakThreads
1134 Now, we discover which *threads* are still alive. Pointers to
1135 threads from the all_threads and main thread lists are the
1136 weakest of all: a pointers from the finalizer of a dead weak
1137 pointer can keep a thread alive. Any threads found to be unreachable
1138 are evacuated and placed on the resurrected_threads list so we
1139 can send them a signal later.
1141 - weak_stage == WeakDone
1143 No more evacuation is done.
1145 -------------------------------------------------------------------------- */
1148 traverse_weak_ptr_list(void)
1150 StgWeak *w, **last_w, *next_w;
1152 rtsBool flag = rtsFalse;
1154 switch (weak_stage) {
1160 /* doesn't matter where we evacuate values/finalizers to, since
1161 * these pointers are treated as roots (iff the keys are alive).
1165 last_w = &old_weak_ptr_list;
1166 for (w = old_weak_ptr_list; w != NULL; w = next_w) {
1168 /* There might be a DEAD_WEAK on the list if finalizeWeak# was
1169 * called on a live weak pointer object. Just remove it.
1171 if (w->header.info == &stg_DEAD_WEAK_info) {
1172 next_w = ((StgDeadWeak *)w)->link;
1177 switch (get_itbl(w)->type) {
1180 next_w = (StgWeak *)((StgEvacuated *)w)->evacuee;
1185 /* Now, check whether the key is reachable.
1187 new = isAlive(w->key);
1190 // evacuate the value and finalizer
1191 w->value = evacuate(w->value);
1192 w->finalizer = evacuate(w->finalizer);
1193 // remove this weak ptr from the old_weak_ptr list
1195 // and put it on the new weak ptr list
1197 w->link = weak_ptr_list;
1200 IF_DEBUG(weak, belch("Weak pointer still alive at %p -> %p",
1205 last_w = &(w->link);
1211 barf("traverse_weak_ptr_list: not WEAK");
1215 /* If we didn't make any changes, then we can go round and kill all
1216 * the dead weak pointers. The old_weak_ptr list is used as a list
1217 * of pending finalizers later on.
1219 if (flag == rtsFalse) {
1220 for (w = old_weak_ptr_list; w; w = w->link) {
1221 w->finalizer = evacuate(w->finalizer);
1224 // Next, move to the WeakThreads stage after fully
1225 // scavenging the finalizers we've just evacuated.
1226 weak_stage = WeakThreads;
1232 /* Now deal with the all_threads list, which behaves somewhat like
1233 * the weak ptr list. If we discover any threads that are about to
1234 * become garbage, we wake them up and administer an exception.
1237 StgTSO *t, *tmp, *next, **prev;
1239 prev = &old_all_threads;
1240 for (t = old_all_threads; t != END_TSO_QUEUE; t = next) {
1242 (StgClosure *)tmp = isAlive((StgClosure *)t);
1248 ASSERT(get_itbl(t)->type == TSO);
1249 switch (t->what_next) {
1250 case ThreadRelocated:
1255 case ThreadComplete:
1256 // finshed or died. The thread might still be alive, but we
1257 // don't keep it on the all_threads list. Don't forget to
1258 // stub out its global_link field.
1259 next = t->global_link;
1260 t->global_link = END_TSO_QUEUE;
1268 // not alive (yet): leave this thread on the
1269 // old_all_threads list.
1270 prev = &(t->global_link);
1271 next = t->global_link;
1274 // alive: move this thread onto the all_threads list.
1275 next = t->global_link;
1276 t->global_link = all_threads;
1283 /* And resurrect any threads which were about to become garbage.
1286 StgTSO *t, *tmp, *next;
1287 for (t = old_all_threads; t != END_TSO_QUEUE; t = next) {
1288 next = t->global_link;
1289 (StgClosure *)tmp = evacuate((StgClosure *)t);
1290 tmp->global_link = resurrected_threads;
1291 resurrected_threads = tmp;
1295 weak_stage = WeakDone; // *now* we're done,
1296 return rtsTrue; // but one more round of scavenging, please
1299 barf("traverse_weak_ptr_list");
1305 /* -----------------------------------------------------------------------------
1306 After GC, the live weak pointer list may have forwarding pointers
1307 on it, because a weak pointer object was evacuated after being
1308 moved to the live weak pointer list. We remove those forwarding
1311 Also, we don't consider weak pointer objects to be reachable, but
1312 we must nevertheless consider them to be "live" and retain them.
1313 Therefore any weak pointer objects which haven't as yet been
1314 evacuated need to be evacuated now.
1315 -------------------------------------------------------------------------- */
1319 mark_weak_ptr_list ( StgWeak **list )
1321 StgWeak *w, **last_w;
1324 for (w = *list; w; w = w->link) {
1325 // w might be WEAK, EVACUATED, or DEAD_WEAK (actually CON_STATIC) here
1326 ASSERT(w->header.info == &stg_DEAD_WEAK_info
1327 || get_itbl(w)->type == WEAK || get_itbl(w)->type == EVACUATED);
1328 (StgClosure *)w = evacuate((StgClosure *)w);
1330 last_w = &(w->link);
1334 /* -----------------------------------------------------------------------------
1335 isAlive determines whether the given closure is still alive (after
1336 a garbage collection) or not. It returns the new address of the
1337 closure if it is alive, or NULL otherwise.
1339 NOTE: Use it before compaction only!
1340 -------------------------------------------------------------------------- */
1344 isAlive(StgClosure *p)
1346 const StgInfoTable *info;
1351 ASSERT(LOOKS_LIKE_CLOSURE_PTR(p));
1354 // ignore static closures
1356 // ToDo: for static closures, check the static link field.
1357 // Problem here is that we sometimes don't set the link field, eg.
1358 // for static closures with an empty SRT or CONSTR_STATIC_NOCAFs.
1360 if (!HEAP_ALLOCED(p)) {
1364 // ignore closures in generations that we're not collecting.
1366 if (bd->gen_no > N) {
1370 // if it's a pointer into to-space, then we're done
1371 if (bd->flags & BF_EVACUATED) {
1375 // large objects use the evacuated flag
1376 if (bd->flags & BF_LARGE) {
1380 // check the mark bit for compacted steps
1381 if ((bd->flags & BF_COMPACTED) && is_marked((P_)p,bd)) {
1385 switch (info->type) {
1390 case IND_OLDGEN: // rely on compatible layout with StgInd
1391 case IND_OLDGEN_PERM:
1392 // follow indirections
1393 p = ((StgInd *)p)->indirectee;
1398 return ((StgEvacuated *)p)->evacuee;
1401 if (((StgTSO *)p)->what_next == ThreadRelocated) {
1402 p = (StgClosure *)((StgTSO *)p)->link;
1415 mark_root(StgClosure **root)
1417 *root = evacuate(*root);
1421 upd_evacuee(StgClosure *p, StgClosure *dest)
1423 // Source object must be in from-space:
1424 ASSERT((Bdescr((P_)p)->flags & BF_EVACUATED) == 0);
1425 // not true: (ToDo: perhaps it should be)
1426 // ASSERT(Bdescr((P_)dest)->flags & BF_EVACUATED);
1427 p->header.info = &stg_EVACUATED_info;
1428 ((StgEvacuated *)p)->evacuee = dest;
1432 STATIC_INLINE StgClosure *
1433 copy(StgClosure *src, nat size, step *stp)
1438 nat size_org = size;
1441 TICK_GC_WORDS_COPIED(size);
1442 /* Find out where we're going, using the handy "to" pointer in
1443 * the step of the source object. If it turns out we need to
1444 * evacuate to an older generation, adjust it here (see comment
1447 if (stp->gen_no < evac_gen) {
1448 #ifdef NO_EAGER_PROMOTION
1449 failed_to_evac = rtsTrue;
1451 stp = &generations[evac_gen].steps[0];
1455 /* chain a new block onto the to-space for the destination step if
1458 if (stp->hp + size >= stp->hpLim) {
1459 gc_alloc_block(stp);
1462 for(to = stp->hp, from = (P_)src; size>0; --size) {
1468 upd_evacuee(src,(StgClosure *)dest);
1470 // We store the size of the just evacuated object in the LDV word so that
1471 // the profiler can guess the position of the next object later.
1472 SET_EVACUAEE_FOR_LDV(src, size_org);
1474 return (StgClosure *)dest;
1477 /* Special version of copy() for when we only want to copy the info
1478 * pointer of an object, but reserve some padding after it. This is
1479 * used to optimise evacuation of BLACKHOLEs.
1484 copyPart(StgClosure *src, nat size_to_reserve, nat size_to_copy, step *stp)
1489 nat size_to_copy_org = size_to_copy;
1492 TICK_GC_WORDS_COPIED(size_to_copy);
1493 if (stp->gen_no < evac_gen) {
1494 #ifdef NO_EAGER_PROMOTION
1495 failed_to_evac = rtsTrue;
1497 stp = &generations[evac_gen].steps[0];
1501 if (stp->hp + size_to_reserve >= stp->hpLim) {
1502 gc_alloc_block(stp);
1505 for(to = stp->hp, from = (P_)src; size_to_copy>0; --size_to_copy) {
1510 stp->hp += size_to_reserve;
1511 upd_evacuee(src,(StgClosure *)dest);
1513 // We store the size of the just evacuated object in the LDV word so that
1514 // the profiler can guess the position of the next object later.
1515 // size_to_copy_org is wrong because the closure already occupies size_to_reserve
1517 SET_EVACUAEE_FOR_LDV(src, size_to_reserve);
1519 if (size_to_reserve - size_to_copy_org > 0)
1520 FILL_SLOP(stp->hp - 1, (int)(size_to_reserve - size_to_copy_org));
1522 return (StgClosure *)dest;
1526 /* -----------------------------------------------------------------------------
1527 Evacuate a large object
1529 This just consists of removing the object from the (doubly-linked)
1530 step->large_objects list, and linking it on to the (singly-linked)
1531 step->new_large_objects list, from where it will be scavenged later.
1533 Convention: bd->flags has BF_EVACUATED set for a large object
1534 that has been evacuated, or unset otherwise.
1535 -------------------------------------------------------------------------- */
1539 evacuate_large(StgPtr p)
1541 bdescr *bd = Bdescr(p);
1544 // object must be at the beginning of the block (or be a ByteArray)
1545 ASSERT(get_itbl((StgClosure *)p)->type == ARR_WORDS ||
1546 (((W_)p & BLOCK_MASK) == 0));
1548 // already evacuated?
1549 if (bd->flags & BF_EVACUATED) {
1550 /* Don't forget to set the failed_to_evac flag if we didn't get
1551 * the desired destination (see comments in evacuate()).
1553 if (bd->gen_no < evac_gen) {
1554 failed_to_evac = rtsTrue;
1555 TICK_GC_FAILED_PROMOTION();
1561 // remove from large_object list
1563 bd->u.back->link = bd->link;
1564 } else { // first object in the list
1565 stp->large_objects = bd->link;
1568 bd->link->u.back = bd->u.back;
1571 /* link it on to the evacuated large object list of the destination step
1574 if (stp->gen_no < evac_gen) {
1575 #ifdef NO_EAGER_PROMOTION
1576 failed_to_evac = rtsTrue;
1578 stp = &generations[evac_gen].steps[0];
1583 bd->gen_no = stp->gen_no;
1584 bd->link = stp->new_large_objects;
1585 stp->new_large_objects = bd;
1586 bd->flags |= BF_EVACUATED;
1589 /* -----------------------------------------------------------------------------
1590 Adding a MUT_CONS to an older generation.
1592 This is necessary from time to time when we end up with an
1593 old-to-new generation pointer in a non-mutable object. We defer
1594 the promotion until the next GC.
1595 -------------------------------------------------------------------------- */
1598 mkMutCons(StgClosure *ptr, generation *gen)
1603 stp = &gen->steps[0];
1605 /* chain a new block onto the to-space for the destination step if
1608 if (stp->hp + sizeofW(StgIndOldGen) >= stp->hpLim) {
1609 gc_alloc_block(stp);
1612 q = (StgMutVar *)stp->hp;
1613 stp->hp += sizeofW(StgMutVar);
1615 SET_HDR(q,&stg_MUT_CONS_info,CCS_GC);
1617 recordOldToNewPtrs((StgMutClosure *)q);
1619 return (StgClosure *)q;
1622 /* -----------------------------------------------------------------------------
1625 This is called (eventually) for every live object in the system.
1627 The caller to evacuate specifies a desired generation in the
1628 evac_gen global variable. The following conditions apply to
1629 evacuating an object which resides in generation M when we're
1630 collecting up to generation N
1634 else evac to step->to
1636 if M < evac_gen evac to evac_gen, step 0
1638 if the object is already evacuated, then we check which generation
1641 if M >= evac_gen do nothing
1642 if M < evac_gen set failed_to_evac flag to indicate that we
1643 didn't manage to evacuate this object into evac_gen.
1648 evacuate() is the single most important function performance-wise
1649 in the GC. Various things have been tried to speed it up, but as
1650 far as I can tell the code generated by gcc 3.2 with -O2 is about
1651 as good as it's going to get. We pass the argument to evacuate()
1652 in a register using the 'regparm' attribute (see the prototype for
1653 evacuate() near the top of this file).
1655 Changing evacuate() to take an (StgClosure **) rather than
1656 returning the new pointer seems attractive, because we can avoid
1657 writing back the pointer when it hasn't changed (eg. for a static
1658 object, or an object in a generation > N). However, I tried it and
1659 it doesn't help. One reason is that the (StgClosure **) pointer
1660 gets spilled to the stack inside evacuate(), resulting in far more
1661 extra reads/writes than we save.
1662 -------------------------------------------------------------------------- */
1664 REGPARM1 static StgClosure *
1665 evacuate(StgClosure *q)
1670 const StgInfoTable *info;
1673 if (HEAP_ALLOCED(q)) {
1676 if (bd->gen_no > N) {
1677 /* Can't evacuate this object, because it's in a generation
1678 * older than the ones we're collecting. Let's hope that it's
1679 * in evac_gen or older, or we will have to arrange to track
1680 * this pointer using the mutable list.
1682 if (bd->gen_no < evac_gen) {
1684 failed_to_evac = rtsTrue;
1685 TICK_GC_FAILED_PROMOTION();
1690 /* evacuate large objects by re-linking them onto a different list.
1692 if (bd->flags & BF_LARGE) {
1694 if (info->type == TSO &&
1695 ((StgTSO *)q)->what_next == ThreadRelocated) {
1696 q = (StgClosure *)((StgTSO *)q)->link;
1699 evacuate_large((P_)q);
1703 /* If the object is in a step that we're compacting, then we
1704 * need to use an alternative evacuate procedure.
1706 if (bd->flags & BF_COMPACTED) {
1707 if (!is_marked((P_)q,bd)) {
1709 if (mark_stack_full()) {
1710 mark_stack_overflowed = rtsTrue;
1713 push_mark_stack((P_)q);
1721 else stp = NULL; // make sure copy() will crash if HEAP_ALLOCED is wrong
1724 // make sure the info pointer is into text space
1725 ASSERT(LOOKS_LIKE_CLOSURE_PTR(q));
1728 switch (info -> type) {
1732 return copy(q,sizeW_fromITBL(info),stp);
1736 StgWord w = (StgWord)q->payload[0];
1737 if (q->header.info == Czh_con_info &&
1738 // unsigned, so always true: (StgChar)w >= MIN_CHARLIKE &&
1739 (StgChar)w <= MAX_CHARLIKE) {
1740 return (StgClosure *)CHARLIKE_CLOSURE((StgChar)w);
1742 if (q->header.info == Izh_con_info &&
1743 (StgInt)w >= MIN_INTLIKE && (StgInt)w <= MAX_INTLIKE) {
1744 return (StgClosure *)INTLIKE_CLOSURE((StgInt)w);
1746 // else, fall through ...
1752 return copy(q,sizeofW(StgHeader)+1,stp);
1754 case THUNK_1_0: // here because of MIN_UPD_SIZE
1759 #ifdef NO_PROMOTE_THUNKS
1760 if (bd->gen_no == 0 &&
1761 bd->step->no != 0 &&
1762 bd->step->no == generations[bd->gen_no].n_steps-1) {
1766 return copy(q,sizeofW(StgHeader)+2,stp);
1774 return copy(q,sizeofW(StgHeader)+2,stp);
1780 case IND_OLDGEN_PERM:
1784 return copy(q,sizeW_fromITBL(info),stp);
1787 return copy(q,bco_sizeW((StgBCO *)q),stp);
1790 case SE_CAF_BLACKHOLE:
1793 return copyPart(q,BLACKHOLE_sizeW(),sizeofW(StgHeader),stp);
1796 to = copy(q,BLACKHOLE_sizeW(),stp);
1799 case THUNK_SELECTOR:
1803 if (thunk_selector_depth > MAX_THUNK_SELECTOR_DEPTH) {
1804 return copy(q,THUNK_SELECTOR_sizeW(),stp);
1807 p = eval_thunk_selector(info->layout.selector_offset,
1811 return copy(q,THUNK_SELECTOR_sizeW(),stp);
1813 // q is still BLACKHOLE'd.
1814 thunk_selector_depth++;
1816 thunk_selector_depth--;
1819 // We store the size of the just evacuated object in the
1820 // LDV word so that the profiler can guess the position of
1821 // the next object later.
1822 SET_EVACUAEE_FOR_LDV(q, THUNK_SELECTOR_sizeW());
1830 // follow chains of indirections, don't evacuate them
1831 q = ((StgInd*)q)->indirectee;
1835 if (info->srt_bitmap != 0 && major_gc &&
1836 THUNK_STATIC_LINK((StgClosure *)q) == NULL) {
1837 THUNK_STATIC_LINK((StgClosure *)q) = static_objects;
1838 static_objects = (StgClosure *)q;
1843 if (info->srt_bitmap != 0 && major_gc &&
1844 FUN_STATIC_LINK((StgClosure *)q) == NULL) {
1845 FUN_STATIC_LINK((StgClosure *)q) = static_objects;
1846 static_objects = (StgClosure *)q;
1851 /* If q->saved_info != NULL, then it's a revertible CAF - it'll be
1852 * on the CAF list, so don't do anything with it here (we'll
1853 * scavenge it later).
1856 && ((StgIndStatic *)q)->saved_info == NULL
1857 && IND_STATIC_LINK((StgClosure *)q) == NULL) {
1858 IND_STATIC_LINK((StgClosure *)q) = static_objects;
1859 static_objects = (StgClosure *)q;
1864 if (major_gc && STATIC_LINK(info,(StgClosure *)q) == NULL) {
1865 STATIC_LINK(info,(StgClosure *)q) = static_objects;
1866 static_objects = (StgClosure *)q;
1870 case CONSTR_INTLIKE:
1871 case CONSTR_CHARLIKE:
1872 case CONSTR_NOCAF_STATIC:
1873 /* no need to put these on the static linked list, they don't need
1887 // shouldn't see these
1888 barf("evacuate: stack frame at %p\n", q);
1892 return copy(q,pap_sizeW((StgPAP*)q),stp);
1895 return copy(q,ap_stack_sizeW((StgAP_STACK*)q),stp);
1898 /* Already evacuated, just return the forwarding address.
1899 * HOWEVER: if the requested destination generation (evac_gen) is
1900 * older than the actual generation (because the object was
1901 * already evacuated to a younger generation) then we have to
1902 * set the failed_to_evac flag to indicate that we couldn't
1903 * manage to promote the object to the desired generation.
1905 if (evac_gen > 0) { // optimisation
1906 StgClosure *p = ((StgEvacuated*)q)->evacuee;
1907 if (HEAP_ALLOCED(p) && Bdescr((P_)p)->gen_no < evac_gen) {
1908 failed_to_evac = rtsTrue;
1909 TICK_GC_FAILED_PROMOTION();
1912 return ((StgEvacuated*)q)->evacuee;
1915 // just copy the block
1916 return copy(q,arr_words_sizeW((StgArrWords *)q),stp);
1919 case MUT_ARR_PTRS_FROZEN:
1920 // just copy the block
1921 return copy(q,mut_arr_ptrs_sizeW((StgMutArrPtrs *)q),stp);
1925 StgTSO *tso = (StgTSO *)q;
1927 /* Deal with redirected TSOs (a TSO that's had its stack enlarged).
1929 if (tso->what_next == ThreadRelocated) {
1930 q = (StgClosure *)tso->link;
1934 /* To evacuate a small TSO, we need to relocate the update frame
1941 new_tso = (StgTSO *)copyPart((StgClosure *)tso,
1943 sizeofW(StgTSO), stp);
1944 move_TSO(tso, new_tso);
1945 for (p = tso->sp, q = new_tso->sp;
1946 p < tso->stack+tso->stack_size;) {
1950 return (StgClosure *)new_tso;
1955 case RBH: // cf. BLACKHOLE_BQ
1957 //StgInfoTable *rip = get_closure_info(q, &size, &ptrs, &nonptrs, &vhs, str);
1958 to = copy(q,BLACKHOLE_sizeW(),stp);
1959 //ToDo: derive size etc from reverted IP
1960 //to = copy(q,size,stp);
1962 belch("@@ evacuate: RBH %p (%s) to %p (%s)",
1963 q, info_type(q), to, info_type(to)));
1968 ASSERT(sizeofW(StgBlockedFetch) >= MIN_NONUPD_SIZE);
1969 to = copy(q,sizeofW(StgBlockedFetch),stp);
1971 belch("@@ evacuate: %p (%s) to %p (%s)",
1972 q, info_type(q), to, info_type(to)));
1979 ASSERT(sizeofW(StgBlockedFetch) >= MIN_UPD_SIZE);
1980 to = copy(q,sizeofW(StgFetchMe),stp);
1982 belch("@@ evacuate: %p (%s) to %p (%s)",
1983 q, info_type(q), to, info_type(to)));
1987 ASSERT(sizeofW(StgBlockedFetch) >= MIN_UPD_SIZE);
1988 to = copy(q,sizeofW(StgFetchMeBlockingQueue),stp);
1990 belch("@@ evacuate: %p (%s) to %p (%s)",
1991 q, info_type(q), to, info_type(to)));
1996 barf("evacuate: strange closure type %d", (int)(info->type));
2002 /* -----------------------------------------------------------------------------
2003 Evaluate a THUNK_SELECTOR if possible.
2005 returns: NULL if we couldn't evaluate this THUNK_SELECTOR, or
2006 a closure pointer if we evaluated it and this is the result. Note
2007 that "evaluating" the THUNK_SELECTOR doesn't necessarily mean
2008 reducing it to HNF, just that we have eliminated the selection.
2009 The result might be another thunk, or even another THUNK_SELECTOR.
2011 If the return value is non-NULL, the original selector thunk has
2012 been BLACKHOLE'd, and should be updated with an indirection or a
2013 forwarding pointer. If the return value is NULL, then the selector
2015 -------------------------------------------------------------------------- */
2018 eval_thunk_selector( nat field, StgSelector * p )
2021 const StgInfoTable *info_ptr;
2022 StgClosure *selectee;
2025 selectee = p->selectee;
2027 // Save the real info pointer (NOTE: not the same as get_itbl()).
2028 info_ptr = p->header.info;
2030 // If the THUNK_SELECTOR is in a generation that we are not
2031 // collecting, then bail out early. We won't be able to save any
2032 // space in any case, and updating with an indirection is trickier
2034 if (Bdescr((StgPtr)p)->gen_no > N) {
2038 // BLACKHOLE the selector thunk, since it is now under evaluation.
2039 // This is important to stop us going into an infinite loop if
2040 // this selector thunk eventually refers to itself.
2041 SET_INFO(p,&stg_BLACKHOLE_info);
2045 // We don't want to end up in to-space, because this causes
2046 // problems when the GC later tries to evacuate the result of
2047 // eval_thunk_selector(). There are various ways this could
2050 // 1. following an IND_STATIC
2052 // 2. when the old generation is compacted, the mark phase updates
2053 // from-space pointers to be to-space pointers, and we can't
2054 // reliably tell which we're following (eg. from an IND_STATIC).
2056 // 3. compacting GC again: if we're looking at a constructor in
2057 // the compacted generation, it might point directly to objects
2058 // in to-space. We must bale out here, otherwise doing the selection
2059 // will result in a to-space pointer being returned.
2061 // (1) is dealt with using a BF_EVACUATED test on the
2062 // selectee. (2) and (3): we can tell if we're looking at an
2063 // object in the compacted generation that might point to
2064 // to-space objects by testing that (a) it is BF_COMPACTED, (b)
2065 // the compacted generation is being collected, and (c) the
2066 // object is marked. Only a marked object may have pointers that
2067 // point to to-space objects, because that happens when
2070 bd = Bdescr((StgPtr)selectee);
2071 if (HEAP_ALLOCED(selectee) &&
2072 ((bd->flags & BF_EVACUATED)
2073 || ((bd->flags & BF_COMPACTED) &&
2075 is_marked((P_)selectee,bd)))) {
2079 info = get_itbl(selectee);
2080 switch (info->type) {
2088 case CONSTR_NOCAF_STATIC:
2089 // check that the size is in range
2090 ASSERT(field < (StgWord32)(info->layout.payload.ptrs +
2091 info->layout.payload.nptrs));
2093 // ToDo: shouldn't we test whether this pointer is in
2095 return selectee->payload[field];
2100 case IND_OLDGEN_PERM:
2102 selectee = ((StgInd *)selectee)->indirectee;
2106 // We don't follow pointers into to-space; the constructor
2107 // has already been evacuated, so we won't save any space
2108 // leaks by evaluating this selector thunk anyhow.
2111 case THUNK_SELECTOR:
2115 // check that we don't recurse too much, re-using the
2116 // depth bound also used in evacuate().
2117 thunk_selector_depth++;
2118 if (thunk_selector_depth > MAX_THUNK_SELECTOR_DEPTH) {
2122 val = eval_thunk_selector(info->layout.selector_offset,
2123 (StgSelector *)selectee);
2125 thunk_selector_depth--;
2130 // We evaluated this selector thunk, so update it with
2131 // an indirection. NOTE: we don't use UPD_IND here,
2132 // because we are guaranteed that p is in a generation
2133 // that we are collecting, and we never want to put the
2134 // indirection on a mutable list.
2136 // For the purposes of LDV profiling, we have destroyed
2137 // the original selector thunk.
2138 SET_INFO(p, info_ptr);
2139 LDV_recordDead_FILL_SLOP_DYNAMIC(selectee);
2141 ((StgInd *)selectee)->indirectee = val;
2142 SET_INFO(selectee,&stg_IND_info);
2144 // For the purposes of LDV profiling, we have created an
2146 LDV_recordCreate(selectee);
2163 case SE_CAF_BLACKHOLE:
2176 // not evaluated yet
2180 barf("eval_thunk_selector: strange selectee %d",
2185 // We didn't manage to evaluate this thunk; restore the old info pointer
2186 SET_INFO(p, info_ptr);
2190 /* -----------------------------------------------------------------------------
2191 move_TSO is called to update the TSO structure after it has been
2192 moved from one place to another.
2193 -------------------------------------------------------------------------- */
2196 move_TSO (StgTSO *src, StgTSO *dest)
2200 // relocate the stack pointer...
2201 diff = (StgPtr)dest - (StgPtr)src; // In *words*
2202 dest->sp = (StgPtr)dest->sp + diff;
2205 /* Similar to scavenge_large_bitmap(), but we don't write back the
2206 * pointers we get back from evacuate().
2209 scavenge_large_srt_bitmap( StgLargeSRT *large_srt )
2216 bitmap = large_srt->l.bitmap[b];
2217 size = (nat)large_srt->l.size;
2218 p = (StgClosure **)large_srt->srt;
2219 for (i = 0; i < size; ) {
2220 if ((bitmap & 1) != 0) {
2225 if (i % BITS_IN(W_) == 0) {
2227 bitmap = large_srt->l.bitmap[b];
2229 bitmap = bitmap >> 1;
2234 /* evacuate the SRT. If srt_bitmap is zero, then there isn't an
2235 * srt field in the info table. That's ok, because we'll
2236 * never dereference it.
2239 scavenge_srt (StgClosure **srt, nat srt_bitmap)
2244 bitmap = srt_bitmap;
2247 if (bitmap == (StgHalfWord)(-1)) {
2248 scavenge_large_srt_bitmap( (StgLargeSRT *)srt );
2252 while (bitmap != 0) {
2253 if ((bitmap & 1) != 0) {
2254 #ifdef ENABLE_WIN32_DLL_SUPPORT
2255 // Special-case to handle references to closures hiding out in DLLs, since
2256 // double indirections required to get at those. The code generator knows
2257 // which is which when generating the SRT, so it stores the (indirect)
2258 // reference to the DLL closure in the table by first adding one to it.
2259 // We check for this here, and undo the addition before evacuating it.
2261 // If the SRT entry hasn't got bit 0 set, the SRT entry points to a
2262 // closure that's fixed at link-time, and no extra magic is required.
2263 if ( (unsigned long)(*srt) & 0x1 ) {
2264 evacuate(*stgCast(StgClosure**,(stgCast(unsigned long, *srt) & ~0x1)));
2273 bitmap = bitmap >> 1;
2279 scavenge_thunk_srt(const StgInfoTable *info)
2281 StgThunkInfoTable *thunk_info;
2283 thunk_info = itbl_to_thunk_itbl(info);
2284 scavenge_srt((StgClosure **)thunk_info->srt, thunk_info->i.srt_bitmap);
2288 scavenge_fun_srt(const StgInfoTable *info)
2290 StgFunInfoTable *fun_info;
2292 fun_info = itbl_to_fun_itbl(info);
2293 scavenge_srt((StgClosure **)fun_info->srt, fun_info->i.srt_bitmap);
2297 scavenge_ret_srt(const StgInfoTable *info)
2299 StgRetInfoTable *ret_info;
2301 ret_info = itbl_to_ret_itbl(info);
2302 scavenge_srt((StgClosure **)ret_info->srt, ret_info->i.srt_bitmap);
2305 /* -----------------------------------------------------------------------------
2307 -------------------------------------------------------------------------- */
2310 scavengeTSO (StgTSO *tso)
2312 // chase the link field for any TSOs on the same queue
2313 (StgClosure *)tso->link = evacuate((StgClosure *)tso->link);
2314 if ( tso->why_blocked == BlockedOnMVar
2315 || tso->why_blocked == BlockedOnBlackHole
2316 || tso->why_blocked == BlockedOnException
2318 || tso->why_blocked == BlockedOnGA
2319 || tso->why_blocked == BlockedOnGA_NoSend
2322 tso->block_info.closure = evacuate(tso->block_info.closure);
2324 if ( tso->blocked_exceptions != NULL ) {
2325 tso->blocked_exceptions =
2326 (StgTSO *)evacuate((StgClosure *)tso->blocked_exceptions);
2329 // scavenge this thread's stack
2330 scavenge_stack(tso->sp, &(tso->stack[tso->stack_size]));
2333 /* -----------------------------------------------------------------------------
2334 Blocks of function args occur on the stack (at the top) and
2336 -------------------------------------------------------------------------- */
2338 STATIC_INLINE StgPtr
2339 scavenge_arg_block (StgFunInfoTable *fun_info, StgClosure **args)
2346 switch (fun_info->fun_type) {
2348 bitmap = BITMAP_BITS(fun_info->bitmap);
2349 size = BITMAP_SIZE(fun_info->bitmap);
2352 size = ((StgLargeBitmap *)fun_info->bitmap)->size;
2353 scavenge_large_bitmap(p, (StgLargeBitmap *)fun_info->bitmap, size);
2357 bitmap = BITMAP_BITS(stg_arg_bitmaps[fun_info->fun_type]);
2358 size = BITMAP_SIZE(stg_arg_bitmaps[fun_info->fun_type]);
2361 if ((bitmap & 1) == 0) {
2362 (StgClosure *)*p = evacuate((StgClosure *)*p);
2365 bitmap = bitmap >> 1;
2373 STATIC_INLINE StgPtr
2374 scavenge_PAP (StgPAP *pap)
2377 StgWord bitmap, size;
2378 StgFunInfoTable *fun_info;
2380 pap->fun = evacuate(pap->fun);
2381 fun_info = get_fun_itbl(pap->fun);
2382 ASSERT(fun_info->i.type != PAP);
2384 p = (StgPtr)pap->payload;
2387 switch (fun_info->fun_type) {
2389 bitmap = BITMAP_BITS(fun_info->bitmap);
2392 scavenge_large_bitmap(p, (StgLargeBitmap *)fun_info->bitmap, size);
2396 scavenge_large_bitmap((StgPtr)pap->payload, BCO_BITMAP(pap->fun), size);
2400 bitmap = BITMAP_BITS(stg_arg_bitmaps[fun_info->fun_type]);
2404 if ((bitmap & 1) == 0) {
2405 (StgClosure *)*p = evacuate((StgClosure *)*p);
2408 bitmap = bitmap >> 1;
2416 /* -----------------------------------------------------------------------------
2417 Scavenge a given step until there are no more objects in this step
2420 evac_gen is set by the caller to be either zero (for a step in a
2421 generation < N) or G where G is the generation of the step being
2424 We sometimes temporarily change evac_gen back to zero if we're
2425 scavenging a mutable object where early promotion isn't such a good
2427 -------------------------------------------------------------------------- */
2435 nat saved_evac_gen = evac_gen;
2440 failed_to_evac = rtsFalse;
2442 /* scavenge phase - standard breadth-first scavenging of the
2446 while (bd != stp->hp_bd || p < stp->hp) {
2448 // If we're at the end of this block, move on to the next block
2449 if (bd != stp->hp_bd && p == bd->free) {
2455 ASSERT(LOOKS_LIKE_CLOSURE_PTR(p));
2456 info = get_itbl((StgClosure *)p);
2458 ASSERT(thunk_selector_depth == 0);
2461 switch (info->type) {
2464 /* treat MVars specially, because we don't want to evacuate the
2465 * mut_link field in the middle of the closure.
2468 StgMVar *mvar = ((StgMVar *)p);
2470 (StgClosure *)mvar->head = evacuate((StgClosure *)mvar->head);
2471 (StgClosure *)mvar->tail = evacuate((StgClosure *)mvar->tail);
2472 (StgClosure *)mvar->value = evacuate((StgClosure *)mvar->value);
2473 evac_gen = saved_evac_gen;
2474 recordMutable((StgMutClosure *)mvar);
2475 failed_to_evac = rtsFalse; // mutable.
2476 p += sizeofW(StgMVar);
2481 scavenge_fun_srt(info);
2482 ((StgClosure *)p)->payload[1] = evacuate(((StgClosure *)p)->payload[1]);
2483 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
2484 p += sizeofW(StgHeader) + 2;
2488 scavenge_thunk_srt(info);
2490 ((StgClosure *)p)->payload[1] = evacuate(((StgClosure *)p)->payload[1]);
2491 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
2492 p += sizeofW(StgHeader) + 2;
2496 scavenge_thunk_srt(info);
2497 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
2498 p += sizeofW(StgHeader) + 2; // MIN_UPD_SIZE
2502 scavenge_fun_srt(info);
2504 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
2505 p += sizeofW(StgHeader) + 1;
2509 scavenge_thunk_srt(info);
2510 p += sizeofW(StgHeader) + 2; // MIN_UPD_SIZE
2514 scavenge_fun_srt(info);
2516 p += sizeofW(StgHeader) + 1;
2520 scavenge_thunk_srt(info);
2521 p += sizeofW(StgHeader) + 2;
2525 scavenge_fun_srt(info);
2527 p += sizeofW(StgHeader) + 2;
2531 scavenge_thunk_srt(info);
2532 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
2533 p += sizeofW(StgHeader) + 2;
2537 scavenge_fun_srt(info);
2539 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
2540 p += sizeofW(StgHeader) + 2;
2544 scavenge_fun_srt(info);
2548 scavenge_thunk_srt(info);
2559 end = (P_)((StgClosure *)p)->payload + info->layout.payload.ptrs;
2560 for (p = (P_)((StgClosure *)p)->payload; p < end; p++) {
2561 (StgClosure *)*p = evacuate((StgClosure *)*p);
2563 p += info->layout.payload.nptrs;
2568 StgBCO *bco = (StgBCO *)p;
2569 (StgClosure *)bco->instrs = evacuate((StgClosure *)bco->instrs);
2570 (StgClosure *)bco->literals = evacuate((StgClosure *)bco->literals);
2571 (StgClosure *)bco->ptrs = evacuate((StgClosure *)bco->ptrs);
2572 (StgClosure *)bco->itbls = evacuate((StgClosure *)bco->itbls);
2573 p += bco_sizeW(bco);
2578 if (stp->gen->no != 0) {
2581 // No need to call LDV_recordDead_FILL_SLOP_DYNAMIC() because an
2582 // IND_OLDGEN_PERM closure is larger than an IND_PERM closure.
2583 LDV_recordDead((StgClosure *)p, sizeofW(StgInd));
2586 // Todo: maybe use SET_HDR() and remove LDV_recordCreate()?
2588 SET_INFO(((StgClosure *)p), &stg_IND_OLDGEN_PERM_info);
2591 // We pretend that p has just been created.
2592 LDV_recordCreate((StgClosure *)p);
2596 case IND_OLDGEN_PERM:
2597 ((StgIndOldGen *)p)->indirectee =
2598 evacuate(((StgIndOldGen *)p)->indirectee);
2599 if (failed_to_evac) {
2600 failed_to_evac = rtsFalse;
2601 recordOldToNewPtrs((StgMutClosure *)p);
2603 p += sizeofW(StgIndOldGen);
2608 ((StgMutVar *)p)->var = evacuate(((StgMutVar *)p)->var);
2609 evac_gen = saved_evac_gen;
2610 recordMutable((StgMutClosure *)p);
2611 failed_to_evac = rtsFalse; // mutable anyhow
2612 p += sizeofW(StgMutVar);
2617 failed_to_evac = rtsFalse; // mutable anyhow
2618 p += sizeofW(StgMutVar);
2622 case SE_CAF_BLACKHOLE:
2625 p += BLACKHOLE_sizeW();
2630 StgBlockingQueue *bh = (StgBlockingQueue *)p;
2631 (StgClosure *)bh->blocking_queue =
2632 evacuate((StgClosure *)bh->blocking_queue);
2633 recordMutable((StgMutClosure *)bh);
2634 failed_to_evac = rtsFalse;
2635 p += BLACKHOLE_sizeW();
2639 case THUNK_SELECTOR:
2641 StgSelector *s = (StgSelector *)p;
2642 s->selectee = evacuate(s->selectee);
2643 p += THUNK_SELECTOR_sizeW();
2647 // A chunk of stack saved in a heap object
2650 StgAP_STACK *ap = (StgAP_STACK *)p;
2652 ap->fun = evacuate(ap->fun);
2653 scavenge_stack((StgPtr)ap->payload, (StgPtr)ap->payload + ap->size);
2654 p = (StgPtr)ap->payload + ap->size;
2660 p = scavenge_PAP((StgPAP *)p);
2664 // nothing to follow
2665 p += arr_words_sizeW((StgArrWords *)p);
2669 // follow everything
2673 evac_gen = 0; // repeatedly mutable
2674 next = p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
2675 for (p = (P_)((StgMutArrPtrs *)p)->payload; p < next; p++) {
2676 (StgClosure *)*p = evacuate((StgClosure *)*p);
2678 evac_gen = saved_evac_gen;
2679 recordMutable((StgMutClosure *)q);
2680 failed_to_evac = rtsFalse; // mutable anyhow.
2684 case MUT_ARR_PTRS_FROZEN:
2685 // follow everything
2689 // Set the mut_link field to NULL, so that we will put this
2690 // array back on the mutable list if it is subsequently thawed
2692 ((StgMutArrPtrs*)p)->mut_link = NULL;
2694 next = p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
2695 for (p = (P_)((StgMutArrPtrs *)p)->payload; p < next; p++) {
2696 (StgClosure *)*p = evacuate((StgClosure *)*p);
2698 // it's tempting to recordMutable() if failed_to_evac is
2699 // false, but that breaks some assumptions (eg. every
2700 // closure on the mutable list is supposed to have the MUT
2701 // flag set, and MUT_ARR_PTRS_FROZEN doesn't).
2707 StgTSO *tso = (StgTSO *)p;
2710 evac_gen = saved_evac_gen;
2711 recordMutable((StgMutClosure *)tso);
2712 failed_to_evac = rtsFalse; // mutable anyhow.
2713 p += tso_sizeW(tso);
2718 case RBH: // cf. BLACKHOLE_BQ
2721 nat size, ptrs, nonptrs, vhs;
2723 StgInfoTable *rip = get_closure_info(p, &size, &ptrs, &nonptrs, &vhs, str);
2725 StgRBH *rbh = (StgRBH *)p;
2726 (StgClosure *)rbh->blocking_queue =
2727 evacuate((StgClosure *)rbh->blocking_queue);
2728 recordMutable((StgMutClosure *)to);
2729 failed_to_evac = rtsFalse; // mutable anyhow.
2731 belch("@@ scavenge: RBH %p (%s) (new blocking_queue link=%p)",
2732 p, info_type(p), (StgClosure *)rbh->blocking_queue));
2733 // ToDo: use size of reverted closure here!
2734 p += BLACKHOLE_sizeW();
2740 StgBlockedFetch *bf = (StgBlockedFetch *)p;
2741 // follow the pointer to the node which is being demanded
2742 (StgClosure *)bf->node =
2743 evacuate((StgClosure *)bf->node);
2744 // follow the link to the rest of the blocking queue
2745 (StgClosure *)bf->link =
2746 evacuate((StgClosure *)bf->link);
2747 if (failed_to_evac) {
2748 failed_to_evac = rtsFalse;
2749 recordMutable((StgMutClosure *)bf);
2752 belch("@@ scavenge: %p (%s); node is now %p; exciting, isn't it",
2753 bf, info_type((StgClosure *)bf),
2754 bf->node, info_type(bf->node)));
2755 p += sizeofW(StgBlockedFetch);
2763 p += sizeofW(StgFetchMe);
2764 break; // nothing to do in this case
2766 case FETCH_ME_BQ: // cf. BLACKHOLE_BQ
2768 StgFetchMeBlockingQueue *fmbq = (StgFetchMeBlockingQueue *)p;
2769 (StgClosure *)fmbq->blocking_queue =
2770 evacuate((StgClosure *)fmbq->blocking_queue);
2771 if (failed_to_evac) {
2772 failed_to_evac = rtsFalse;
2773 recordMutable((StgMutClosure *)fmbq);
2776 belch("@@ scavenge: %p (%s) exciting, isn't it",
2777 p, info_type((StgClosure *)p)));
2778 p += sizeofW(StgFetchMeBlockingQueue);
2784 barf("scavenge: unimplemented/strange closure type %d @ %p",
2788 /* If we didn't manage to promote all the objects pointed to by
2789 * the current object, then we have to designate this object as
2790 * mutable (because it contains old-to-new generation pointers).
2792 if (failed_to_evac) {
2793 failed_to_evac = rtsFalse;
2794 mkMutCons((StgClosure *)q, &generations[evac_gen]);
2802 /* -----------------------------------------------------------------------------
2803 Scavenge everything on the mark stack.
2805 This is slightly different from scavenge():
2806 - we don't walk linearly through the objects, so the scavenger
2807 doesn't need to advance the pointer on to the next object.
2808 -------------------------------------------------------------------------- */
2811 scavenge_mark_stack(void)
2817 evac_gen = oldest_gen->no;
2818 saved_evac_gen = evac_gen;
2821 while (!mark_stack_empty()) {
2822 p = pop_mark_stack();
2824 ASSERT(LOOKS_LIKE_CLOSURE_PTR(p));
2825 info = get_itbl((StgClosure *)p);
2828 switch (info->type) {
2831 /* treat MVars specially, because we don't want to evacuate the
2832 * mut_link field in the middle of the closure.
2835 StgMVar *mvar = ((StgMVar *)p);
2837 (StgClosure *)mvar->head = evacuate((StgClosure *)mvar->head);
2838 (StgClosure *)mvar->tail = evacuate((StgClosure *)mvar->tail);
2839 (StgClosure *)mvar->value = evacuate((StgClosure *)mvar->value);
2840 evac_gen = saved_evac_gen;
2841 failed_to_evac = rtsFalse; // mutable.
2846 scavenge_fun_srt(info);
2847 ((StgClosure *)p)->payload[1] = evacuate(((StgClosure *)p)->payload[1]);
2848 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
2852 scavenge_thunk_srt(info);
2854 ((StgClosure *)p)->payload[1] = evacuate(((StgClosure *)p)->payload[1]);
2855 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
2860 scavenge_fun_srt(info);
2861 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
2866 scavenge_thunk_srt(info);
2869 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
2874 scavenge_fun_srt(info);
2879 scavenge_thunk_srt(info);
2887 scavenge_fun_srt(info);
2891 scavenge_thunk_srt(info);
2902 end = (P_)((StgClosure *)p)->payload + info->layout.payload.ptrs;
2903 for (p = (P_)((StgClosure *)p)->payload; p < end; p++) {
2904 (StgClosure *)*p = evacuate((StgClosure *)*p);
2910 StgBCO *bco = (StgBCO *)p;
2911 (StgClosure *)bco->instrs = evacuate((StgClosure *)bco->instrs);
2912 (StgClosure *)bco->literals = evacuate((StgClosure *)bco->literals);
2913 (StgClosure *)bco->ptrs = evacuate((StgClosure *)bco->ptrs);
2914 (StgClosure *)bco->itbls = evacuate((StgClosure *)bco->itbls);
2919 // don't need to do anything here: the only possible case
2920 // is that we're in a 1-space compacting collector, with
2921 // no "old" generation.
2925 case IND_OLDGEN_PERM:
2926 ((StgIndOldGen *)p)->indirectee =
2927 evacuate(((StgIndOldGen *)p)->indirectee);
2928 if (failed_to_evac) {
2929 recordOldToNewPtrs((StgMutClosure *)p);
2931 failed_to_evac = rtsFalse;
2936 ((StgMutVar *)p)->var = evacuate(((StgMutVar *)p)->var);
2937 evac_gen = saved_evac_gen;
2938 failed_to_evac = rtsFalse;
2943 failed_to_evac = rtsFalse;
2947 case SE_CAF_BLACKHOLE:
2955 StgBlockingQueue *bh = (StgBlockingQueue *)p;
2956 (StgClosure *)bh->blocking_queue =
2957 evacuate((StgClosure *)bh->blocking_queue);
2958 failed_to_evac = rtsFalse;
2962 case THUNK_SELECTOR:
2964 StgSelector *s = (StgSelector *)p;
2965 s->selectee = evacuate(s->selectee);
2969 // A chunk of stack saved in a heap object
2972 StgAP_STACK *ap = (StgAP_STACK *)p;
2974 ap->fun = evacuate(ap->fun);
2975 scavenge_stack((StgPtr)ap->payload, (StgPtr)ap->payload + ap->size);
2981 scavenge_PAP((StgPAP *)p);
2985 // follow everything
2989 evac_gen = 0; // repeatedly mutable
2990 next = p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
2991 for (p = (P_)((StgMutArrPtrs *)p)->payload; p < next; p++) {
2992 (StgClosure *)*p = evacuate((StgClosure *)*p);
2994 evac_gen = saved_evac_gen;
2995 failed_to_evac = rtsFalse; // mutable anyhow.
2999 case MUT_ARR_PTRS_FROZEN:
3000 // follow everything
3004 // Set the mut_link field to NULL, so that we will put this
3005 // array on the mutable list if it is subsequently thawed
3007 ((StgMutArrPtrs*)p)->mut_link = NULL;
3009 next = p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
3010 for (p = (P_)((StgMutArrPtrs *)p)->payload; p < next; p++) {
3011 (StgClosure *)*p = evacuate((StgClosure *)*p);
3018 StgTSO *tso = (StgTSO *)p;
3021 evac_gen = saved_evac_gen;
3022 failed_to_evac = rtsFalse;
3027 case RBH: // cf. BLACKHOLE_BQ
3030 nat size, ptrs, nonptrs, vhs;
3032 StgInfoTable *rip = get_closure_info(p, &size, &ptrs, &nonptrs, &vhs, str);
3034 StgRBH *rbh = (StgRBH *)p;
3035 (StgClosure *)rbh->blocking_queue =
3036 evacuate((StgClosure *)rbh->blocking_queue);
3037 recordMutable((StgMutClosure *)rbh);
3038 failed_to_evac = rtsFalse; // mutable anyhow.
3040 belch("@@ scavenge: RBH %p (%s) (new blocking_queue link=%p)",
3041 p, info_type(p), (StgClosure *)rbh->blocking_queue));
3047 StgBlockedFetch *bf = (StgBlockedFetch *)p;
3048 // follow the pointer to the node which is being demanded
3049 (StgClosure *)bf->node =
3050 evacuate((StgClosure *)bf->node);
3051 // follow the link to the rest of the blocking queue
3052 (StgClosure *)bf->link =
3053 evacuate((StgClosure *)bf->link);
3054 if (failed_to_evac) {
3055 failed_to_evac = rtsFalse;
3056 recordMutable((StgMutClosure *)bf);
3059 belch("@@ scavenge: %p (%s); node is now %p; exciting, isn't it",
3060 bf, info_type((StgClosure *)bf),
3061 bf->node, info_type(bf->node)));
3069 break; // nothing to do in this case
3071 case FETCH_ME_BQ: // cf. BLACKHOLE_BQ
3073 StgFetchMeBlockingQueue *fmbq = (StgFetchMeBlockingQueue *)p;
3074 (StgClosure *)fmbq->blocking_queue =
3075 evacuate((StgClosure *)fmbq->blocking_queue);
3076 if (failed_to_evac) {
3077 failed_to_evac = rtsFalse;
3078 recordMutable((StgMutClosure *)fmbq);
3081 belch("@@ scavenge: %p (%s) exciting, isn't it",
3082 p, info_type((StgClosure *)p)));
3088 barf("scavenge_mark_stack: unimplemented/strange closure type %d @ %p",
3092 if (failed_to_evac) {
3093 failed_to_evac = rtsFalse;
3094 mkMutCons((StgClosure *)q, &generations[evac_gen]);
3097 // mark the next bit to indicate "scavenged"
3098 mark(q+1, Bdescr(q));
3100 } // while (!mark_stack_empty())
3102 // start a new linear scan if the mark stack overflowed at some point
3103 if (mark_stack_overflowed && oldgen_scan_bd == NULL) {
3104 IF_DEBUG(gc, belch("scavenge_mark_stack: starting linear scan"));
3105 mark_stack_overflowed = rtsFalse;
3106 oldgen_scan_bd = oldest_gen->steps[0].blocks;
3107 oldgen_scan = oldgen_scan_bd->start;
3110 if (oldgen_scan_bd) {
3111 // push a new thing on the mark stack
3113 // find a closure that is marked but not scavenged, and start
3115 while (oldgen_scan < oldgen_scan_bd->free
3116 && !is_marked(oldgen_scan,oldgen_scan_bd)) {
3120 if (oldgen_scan < oldgen_scan_bd->free) {
3122 // already scavenged?
3123 if (is_marked(oldgen_scan+1,oldgen_scan_bd)) {
3124 oldgen_scan += sizeofW(StgHeader) + MIN_NONUPD_SIZE;
3127 push_mark_stack(oldgen_scan);
3128 // ToDo: bump the linear scan by the actual size of the object
3129 oldgen_scan += sizeofW(StgHeader) + MIN_NONUPD_SIZE;
3133 oldgen_scan_bd = oldgen_scan_bd->link;
3134 if (oldgen_scan_bd != NULL) {
3135 oldgen_scan = oldgen_scan_bd->start;
3141 /* -----------------------------------------------------------------------------
3142 Scavenge one object.
3144 This is used for objects that are temporarily marked as mutable
3145 because they contain old-to-new generation pointers. Only certain
3146 objects can have this property.
3147 -------------------------------------------------------------------------- */
3150 scavenge_one(StgPtr p)
3152 const StgInfoTable *info;
3153 nat saved_evac_gen = evac_gen;
3156 ASSERT(LOOKS_LIKE_CLOSURE_PTR(p));
3157 info = get_itbl((StgClosure *)p);
3159 switch (info->type) {
3162 case FUN_1_0: // hardly worth specialising these guys
3182 case IND_OLDGEN_PERM:
3186 end = (StgPtr)((StgClosure *)p)->payload + info->layout.payload.ptrs;
3187 for (q = (StgPtr)((StgClosure *)p)->payload; q < end; q++) {
3188 (StgClosure *)*q = evacuate((StgClosure *)*q);
3194 case SE_CAF_BLACKHOLE:
3199 case THUNK_SELECTOR:
3201 StgSelector *s = (StgSelector *)p;
3202 s->selectee = evacuate(s->selectee);
3207 // nothing to follow
3212 // follow everything
3215 evac_gen = 0; // repeatedly mutable
3216 recordMutable((StgMutClosure *)p);
3217 next = p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
3218 for (p = (P_)((StgMutArrPtrs *)p)->payload; p < next; p++) {
3219 (StgClosure *)*p = evacuate((StgClosure *)*p);
3221 evac_gen = saved_evac_gen;
3222 failed_to_evac = rtsFalse;
3226 case MUT_ARR_PTRS_FROZEN:
3228 // follow everything
3231 // Set the mut_link field to NULL, so that we will put this
3232 // array on the mutable list if it is subsequently thawed
3234 ((StgMutArrPtrs*)p)->mut_link = NULL;
3236 next = p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
3237 for (p = (P_)((StgMutArrPtrs *)p)->payload; p < next; p++) {
3238 (StgClosure *)*p = evacuate((StgClosure *)*p);
3245 StgTSO *tso = (StgTSO *)p;
3247 evac_gen = 0; // repeatedly mutable
3249 recordMutable((StgMutClosure *)tso);
3250 evac_gen = saved_evac_gen;
3251 failed_to_evac = rtsFalse;
3257 StgAP_STACK *ap = (StgAP_STACK *)p;
3259 ap->fun = evacuate(ap->fun);
3260 scavenge_stack((StgPtr)ap->payload, (StgPtr)ap->payload + ap->size);
3261 p = (StgPtr)ap->payload + ap->size;
3267 p = scavenge_PAP((StgPAP *)p);
3271 // This might happen if for instance a MUT_CONS was pointing to a
3272 // THUNK which has since been updated. The IND_OLDGEN will
3273 // be on the mutable list anyway, so we don't need to do anything
3278 barf("scavenge_one: strange object %d", (int)(info->type));
3281 no_luck = failed_to_evac;
3282 failed_to_evac = rtsFalse;
3286 /* -----------------------------------------------------------------------------
3287 Scavenging mutable lists.
3289 We treat the mutable list of each generation > N (i.e. all the
3290 generations older than the one being collected) as roots. We also
3291 remove non-mutable objects from the mutable list at this point.
3292 -------------------------------------------------------------------------- */
3295 scavenge_mut_once_list(generation *gen)
3297 const StgInfoTable *info;
3298 StgMutClosure *p, *next, *new_list;
3300 p = gen->mut_once_list;
3301 new_list = END_MUT_LIST;
3305 failed_to_evac = rtsFalse;
3307 for (; p != END_MUT_LIST; p = next, next = p->mut_link) {
3309 ASSERT(LOOKS_LIKE_CLOSURE_PTR(p));
3312 if (info->type==RBH)
3313 info = REVERT_INFOPTR(info); // if it's an RBH, look at the orig closure
3315 switch(info->type) {
3318 case IND_OLDGEN_PERM:
3320 /* Try to pull the indirectee into this generation, so we can
3321 * remove the indirection from the mutable list.
3323 ((StgIndOldGen *)p)->indirectee =
3324 evacuate(((StgIndOldGen *)p)->indirectee);
3326 #if 0 && defined(DEBUG)
3327 if (RtsFlags.DebugFlags.gc)
3328 /* Debugging code to print out the size of the thing we just
3332 StgPtr start = gen->steps[0].scan;
3333 bdescr *start_bd = gen->steps[0].scan_bd;
3335 scavenge(&gen->steps[0]);
3336 if (start_bd != gen->steps[0].scan_bd) {
3337 size += (P_)BLOCK_ROUND_UP(start) - start;
3338 start_bd = start_bd->link;
3339 while (start_bd != gen->steps[0].scan_bd) {
3340 size += BLOCK_SIZE_W;
3341 start_bd = start_bd->link;
3343 size += gen->steps[0].scan -
3344 (P_)BLOCK_ROUND_DOWN(gen->steps[0].scan);
3346 size = gen->steps[0].scan - start;
3348 belch("evac IND_OLDGEN: %ld bytes", size * sizeof(W_));
3352 /* failed_to_evac might happen if we've got more than two
3353 * generations, we're collecting only generation 0, the
3354 * indirection resides in generation 2 and the indirectee is
3357 if (failed_to_evac) {
3358 failed_to_evac = rtsFalse;
3359 p->mut_link = new_list;
3362 /* the mut_link field of an IND_STATIC is overloaded as the
3363 * static link field too (it just so happens that we don't need
3364 * both at the same time), so we need to NULL it out when
3365 * removing this object from the mutable list because the static
3366 * link fields are all assumed to be NULL before doing a major
3374 /* MUT_CONS is a kind of MUT_VAR, except it that we try to remove
3375 * it from the mutable list if possible by promoting whatever it
3378 if (scavenge_one((StgPtr)((StgMutVar *)p)->var)) {
3379 /* didn't manage to promote everything, so put the
3380 * MUT_CONS back on the list.
3382 p->mut_link = new_list;
3388 // shouldn't have anything else on the mutables list
3389 barf("scavenge_mut_once_list: strange object? %d", (int)(info->type));
3393 gen->mut_once_list = new_list;
3398 scavenge_mutable_list(generation *gen)
3400 const StgInfoTable *info;
3401 StgMutClosure *p, *next;
3403 p = gen->saved_mut_list;
3407 failed_to_evac = rtsFalse;
3409 for (; p != END_MUT_LIST; p = next, next = p->mut_link) {
3411 ASSERT(LOOKS_LIKE_CLOSURE_PTR(p));
3414 if (info->type==RBH)
3415 info = REVERT_INFOPTR(info); // if it's an RBH, look at the orig closure
3417 switch(info->type) {
3420 // follow everything
3421 p->mut_link = gen->mut_list;
3426 end = (P_)p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
3427 for (q = (P_)((StgMutArrPtrs *)p)->payload; q < end; q++) {
3428 (StgClosure *)*q = evacuate((StgClosure *)*q);
3433 // Happens if a MUT_ARR_PTRS in the old generation is frozen
3434 case MUT_ARR_PTRS_FROZEN:
3439 end = (P_)p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
3440 for (q = (P_)((StgMutArrPtrs *)p)->payload; q < end; q++) {
3441 (StgClosure *)*q = evacuate((StgClosure *)*q);
3444 // Set the mut_link field to NULL, so that we will put this
3445 // array back on the mutable list if it is subsequently thawed
3448 if (failed_to_evac) {
3449 failed_to_evac = rtsFalse;
3450 mkMutCons((StgClosure *)p, gen);
3456 ((StgMutVar *)p)->var = evacuate(((StgMutVar *)p)->var);
3457 p->mut_link = gen->mut_list;
3463 StgMVar *mvar = (StgMVar *)p;
3464 (StgClosure *)mvar->head = evacuate((StgClosure *)mvar->head);
3465 (StgClosure *)mvar->tail = evacuate((StgClosure *)mvar->tail);
3466 (StgClosure *)mvar->value = evacuate((StgClosure *)mvar->value);
3467 p->mut_link = gen->mut_list;
3474 StgTSO *tso = (StgTSO *)p;
3478 /* Don't take this TSO off the mutable list - it might still
3479 * point to some younger objects (because we set evac_gen to 0
3482 tso->mut_link = gen->mut_list;
3483 gen->mut_list = (StgMutClosure *)tso;
3489 StgBlockingQueue *bh = (StgBlockingQueue *)p;
3490 (StgClosure *)bh->blocking_queue =
3491 evacuate((StgClosure *)bh->blocking_queue);
3492 p->mut_link = gen->mut_list;
3497 /* Happens if a BLACKHOLE_BQ in the old generation is updated:
3500 case IND_OLDGEN_PERM:
3501 /* Try to pull the indirectee into this generation, so we can
3502 * remove the indirection from the mutable list.
3505 ((StgIndOldGen *)p)->indirectee =
3506 evacuate(((StgIndOldGen *)p)->indirectee);
3509 if (failed_to_evac) {
3510 failed_to_evac = rtsFalse;
3511 p->mut_link = gen->mut_once_list;
3512 gen->mut_once_list = p;
3519 // HWL: check whether all of these are necessary
3521 case RBH: // cf. BLACKHOLE_BQ
3523 // nat size, ptrs, nonptrs, vhs;
3525 // StgInfoTable *rip = get_closure_info(p, &size, &ptrs, &nonptrs, &vhs, str);
3526 StgRBH *rbh = (StgRBH *)p;
3527 (StgClosure *)rbh->blocking_queue =
3528 evacuate((StgClosure *)rbh->blocking_queue);
3529 if (failed_to_evac) {
3530 failed_to_evac = rtsFalse;
3531 recordMutable((StgMutClosure *)rbh);
3533 // ToDo: use size of reverted closure here!
3534 p += BLACKHOLE_sizeW();
3540 StgBlockedFetch *bf = (StgBlockedFetch *)p;
3541 // follow the pointer to the node which is being demanded
3542 (StgClosure *)bf->node =
3543 evacuate((StgClosure *)bf->node);
3544 // follow the link to the rest of the blocking queue
3545 (StgClosure *)bf->link =
3546 evacuate((StgClosure *)bf->link);
3547 if (failed_to_evac) {
3548 failed_to_evac = rtsFalse;
3549 recordMutable((StgMutClosure *)bf);
3551 p += sizeofW(StgBlockedFetch);
3557 barf("scavenge_mutable_list: REMOTE_REF %d", (int)(info->type));
3560 p += sizeofW(StgFetchMe);
3561 break; // nothing to do in this case
3563 case FETCH_ME_BQ: // cf. BLACKHOLE_BQ
3565 StgFetchMeBlockingQueue *fmbq = (StgFetchMeBlockingQueue *)p;
3566 (StgClosure *)fmbq->blocking_queue =
3567 evacuate((StgClosure *)fmbq->blocking_queue);
3568 if (failed_to_evac) {
3569 failed_to_evac = rtsFalse;
3570 recordMutable((StgMutClosure *)fmbq);
3572 p += sizeofW(StgFetchMeBlockingQueue);
3578 // shouldn't have anything else on the mutables list
3579 barf("scavenge_mutable_list: strange object? %d", (int)(info->type));
3586 scavenge_static(void)
3588 StgClosure* p = static_objects;
3589 const StgInfoTable *info;
3591 /* Always evacuate straight to the oldest generation for static
3593 evac_gen = oldest_gen->no;
3595 /* keep going until we've scavenged all the objects on the linked
3597 while (p != END_OF_STATIC_LIST) {
3599 ASSERT(LOOKS_LIKE_CLOSURE_PTR(p));
3602 if (info->type==RBH)
3603 info = REVERT_INFOPTR(info); // if it's an RBH, look at the orig closure
3605 // make sure the info pointer is into text space
3607 /* Take this object *off* the static_objects list,
3608 * and put it on the scavenged_static_objects list.
3610 static_objects = STATIC_LINK(info,p);
3611 STATIC_LINK(info,p) = scavenged_static_objects;
3612 scavenged_static_objects = p;
3614 switch (info -> type) {
3618 StgInd *ind = (StgInd *)p;
3619 ind->indirectee = evacuate(ind->indirectee);
3621 /* might fail to evacuate it, in which case we have to pop it
3622 * back on the mutable list (and take it off the
3623 * scavenged_static list because the static link and mut link
3624 * pointers are one and the same).
3626 if (failed_to_evac) {
3627 failed_to_evac = rtsFalse;
3628 scavenged_static_objects = IND_STATIC_LINK(p);
3629 ((StgMutClosure *)ind)->mut_link = oldest_gen->mut_once_list;
3630 oldest_gen->mut_once_list = (StgMutClosure *)ind;
3636 scavenge_thunk_srt(info);
3640 scavenge_fun_srt(info);
3647 next = (P_)p->payload + info->layout.payload.ptrs;
3648 // evacuate the pointers
3649 for (q = (P_)p->payload; q < next; q++) {
3650 (StgClosure *)*q = evacuate((StgClosure *)*q);
3656 barf("scavenge_static: strange closure %d", (int)(info->type));
3659 ASSERT(failed_to_evac == rtsFalse);
3661 /* get the next static object from the list. Remember, there might
3662 * be more stuff on this list now that we've done some evacuating!
3663 * (static_objects is a global)
3669 /* -----------------------------------------------------------------------------
3670 scavenge a chunk of memory described by a bitmap
3671 -------------------------------------------------------------------------- */
3674 scavenge_large_bitmap( StgPtr p, StgLargeBitmap *large_bitmap, nat size )
3680 bitmap = large_bitmap->bitmap[b];
3681 for (i = 0; i < size; ) {
3682 if ((bitmap & 1) == 0) {
3683 (StgClosure *)*p = evacuate((StgClosure *)*p);
3687 if (i % BITS_IN(W_) == 0) {
3689 bitmap = large_bitmap->bitmap[b];
3691 bitmap = bitmap >> 1;
3696 STATIC_INLINE StgPtr
3697 scavenge_small_bitmap (StgPtr p, nat size, StgWord bitmap)
3700 if ((bitmap & 1) == 0) {
3701 (StgClosure *)*p = evacuate((StgClosure *)*p);
3704 bitmap = bitmap >> 1;
3710 /* -----------------------------------------------------------------------------
3711 scavenge_stack walks over a section of stack and evacuates all the
3712 objects pointed to by it. We can use the same code for walking
3713 AP_STACK_UPDs, since these are just sections of copied stack.
3714 -------------------------------------------------------------------------- */
3718 scavenge_stack(StgPtr p, StgPtr stack_end)
3720 const StgRetInfoTable* info;
3724 //IF_DEBUG(sanity, belch(" scavenging stack between %p and %p", p, stack_end));
3727 * Each time around this loop, we are looking at a chunk of stack
3728 * that starts with an activation record.
3731 while (p < stack_end) {
3732 info = get_ret_itbl((StgClosure *)p);
3734 switch (info->i.type) {
3737 ((StgUpdateFrame *)p)->updatee
3738 = evacuate(((StgUpdateFrame *)p)->updatee);
3739 p += sizeofW(StgUpdateFrame);
3742 // small bitmap (< 32 entries, or 64 on a 64-bit machine)
3747 bitmap = BITMAP_BITS(info->i.layout.bitmap);
3748 size = BITMAP_SIZE(info->i.layout.bitmap);
3749 // NOTE: the payload starts immediately after the info-ptr, we
3750 // don't have an StgHeader in the same sense as a heap closure.
3752 p = scavenge_small_bitmap(p, size, bitmap);
3755 scavenge_srt((StgClosure **)info->srt, info->i.srt_bitmap);
3763 (StgClosure *)*p = evacuate((StgClosure *)*p);
3766 size = BCO_BITMAP_SIZE(bco);
3767 scavenge_large_bitmap(p, BCO_BITMAP(bco), size);
3772 // large bitmap (> 32 entries, or > 64 on a 64-bit machine)
3778 size = info->i.layout.large_bitmap->size;
3780 scavenge_large_bitmap(p, info->i.layout.large_bitmap, size);
3782 // and don't forget to follow the SRT
3786 // Dynamic bitmap: the mask is stored on the stack, and
3787 // there are a number of non-pointers followed by a number
3788 // of pointers above the bitmapped area. (see StgMacros.h,
3793 dyn = ((StgRetDyn *)p)->liveness;
3795 // traverse the bitmap first
3796 bitmap = GET_LIVENESS(dyn);
3797 p = (P_)&((StgRetDyn *)p)->payload[0];
3798 size = RET_DYN_BITMAP_SIZE;
3799 p = scavenge_small_bitmap(p, size, bitmap);
3801 // skip over the non-ptr words
3802 p += GET_NONPTRS(dyn) + RET_DYN_NONPTR_REGS_SIZE;
3804 // follow the ptr words
3805 for (size = GET_PTRS(dyn); size > 0; size--) {
3806 (StgClosure *)*p = evacuate((StgClosure *)*p);
3814 StgRetFun *ret_fun = (StgRetFun *)p;
3815 StgFunInfoTable *fun_info;
3817 ret_fun->fun = evacuate(ret_fun->fun);
3818 fun_info = get_fun_itbl(ret_fun->fun);
3819 p = scavenge_arg_block(fun_info, ret_fun->payload);
3824 barf("scavenge_stack: weird activation record found on stack: %d", (int)(info->i.type));
3829 /*-----------------------------------------------------------------------------
3830 scavenge the large object list.
3832 evac_gen set by caller; similar games played with evac_gen as with
3833 scavenge() - see comment at the top of scavenge(). Most large
3834 objects are (repeatedly) mutable, so most of the time evac_gen will
3836 --------------------------------------------------------------------------- */
3839 scavenge_large(step *stp)
3844 bd = stp->new_large_objects;
3846 for (; bd != NULL; bd = stp->new_large_objects) {
3848 /* take this object *off* the large objects list and put it on
3849 * the scavenged large objects list. This is so that we can
3850 * treat new_large_objects as a stack and push new objects on
3851 * the front when evacuating.
3853 stp->new_large_objects = bd->link;
3854 dbl_link_onto(bd, &stp->scavenged_large_objects);
3856 // update the block count in this step.
3857 stp->n_scavenged_large_blocks += bd->blocks;
3860 if (scavenge_one(p)) {
3861 mkMutCons((StgClosure *)p, stp->gen);
3866 /* -----------------------------------------------------------------------------
3867 Initialising the static object & mutable lists
3868 -------------------------------------------------------------------------- */
3871 zero_static_object_list(StgClosure* first_static)
3875 const StgInfoTable *info;
3877 for (p = first_static; p != END_OF_STATIC_LIST; p = link) {
3879 link = STATIC_LINK(info, p);
3880 STATIC_LINK(info,p) = NULL;
3884 /* This function is only needed because we share the mutable link
3885 * field with the static link field in an IND_STATIC, so we have to
3886 * zero the mut_link field before doing a major GC, which needs the
3887 * static link field.
3889 * It doesn't do any harm to zero all the mutable link fields on the
3894 zero_mutable_list( StgMutClosure *first )
3896 StgMutClosure *next, *c;
3898 for (c = first; c != END_MUT_LIST; c = next) {
3904 /* -----------------------------------------------------------------------------
3906 -------------------------------------------------------------------------- */
3913 for (c = (StgIndStatic *)caf_list; c != NULL;
3914 c = (StgIndStatic *)c->static_link)
3916 c->header.info = c->saved_info;
3917 c->saved_info = NULL;
3918 // could, but not necessary: c->static_link = NULL;
3924 markCAFs( evac_fn evac )
3928 for (c = (StgIndStatic *)caf_list; c != NULL;
3929 c = (StgIndStatic *)c->static_link)
3931 evac(&c->indirectee);
3935 /* -----------------------------------------------------------------------------
3936 Sanity code for CAF garbage collection.
3938 With DEBUG turned on, we manage a CAF list in addition to the SRT
3939 mechanism. After GC, we run down the CAF list and blackhole any
3940 CAFs which have been garbage collected. This means we get an error
3941 whenever the program tries to enter a garbage collected CAF.
3943 Any garbage collected CAFs are taken off the CAF list at the same
3945 -------------------------------------------------------------------------- */
3947 #if 0 && defined(DEBUG)
3954 const StgInfoTable *info;
3965 ASSERT(info->type == IND_STATIC);
3967 if (STATIC_LINK(info,p) == NULL) {
3968 IF_DEBUG(gccafs, belch("CAF gc'd at 0x%04lx", (long)p));
3970 SET_INFO(p,&stg_BLACKHOLE_info);
3971 p = STATIC_LINK2(info,p);
3975 pp = &STATIC_LINK2(info,p);
3982 // belch("%d CAFs live", i);
3987 /* -----------------------------------------------------------------------------
3990 Whenever a thread returns to the scheduler after possibly doing
3991 some work, we have to run down the stack and black-hole all the
3992 closures referred to by update frames.
3993 -------------------------------------------------------------------------- */
3996 threadLazyBlackHole(StgTSO *tso)
3999 StgRetInfoTable *info;
4000 StgBlockingQueue *bh;
4003 stack_end = &tso->stack[tso->stack_size];
4005 frame = (StgClosure *)tso->sp;
4008 info = get_ret_itbl(frame);
4010 switch (info->i.type) {
4013 bh = (StgBlockingQueue *)((StgUpdateFrame *)frame)->updatee;
4015 /* if the thunk is already blackholed, it means we've also
4016 * already blackholed the rest of the thunks on this stack,
4017 * so we can stop early.
4019 * The blackhole made for a CAF is a CAF_BLACKHOLE, so they
4020 * don't interfere with this optimisation.
4022 if (bh->header.info == &stg_BLACKHOLE_info) {
4026 if (bh->header.info != &stg_BLACKHOLE_BQ_info &&
4027 bh->header.info != &stg_CAF_BLACKHOLE_info) {
4028 #if (!defined(LAZY_BLACKHOLING)) && defined(DEBUG)
4029 belch("Unexpected lazy BHing required at 0x%04x",(int)bh);
4033 // We pretend that bh is now dead.
4034 LDV_recordDead_FILL_SLOP_DYNAMIC((StgClosure *)bh);
4036 SET_INFO(bh,&stg_BLACKHOLE_info);
4039 // We pretend that bh has just been created.
4040 LDV_recordCreate(bh);
4044 frame = (StgClosure *) ((StgUpdateFrame *)frame + 1);
4050 // normal stack frames; do nothing except advance the pointer
4052 (StgPtr)frame += stack_frame_sizeW(frame);
4058 /* -----------------------------------------------------------------------------
4061 * Code largely pinched from old RTS, then hacked to bits. We also do
4062 * lazy black holing here.
4064 * -------------------------------------------------------------------------- */
4066 struct stack_gap { StgWord gap_size; struct stack_gap *next_gap; };
4069 threadSqueezeStack(StgTSO *tso)
4072 rtsBool prev_was_update_frame;
4073 StgClosure *updatee = NULL;
4075 StgRetInfoTable *info;
4076 StgWord current_gap_size;
4077 struct stack_gap *gap;
4080 // Traverse the stack upwards, replacing adjacent update frames
4081 // with a single update frame and a "stack gap". A stack gap
4082 // contains two values: the size of the gap, and the distance
4083 // to the next gap (or the stack top).
4085 bottom = &(tso->stack[tso->stack_size]);
4089 ASSERT(frame < bottom);
4091 prev_was_update_frame = rtsFalse;
4092 current_gap_size = 0;
4093 gap = (struct stack_gap *) (tso->sp - sizeofW(StgUpdateFrame));
4095 while (frame < bottom) {
4097 info = get_ret_itbl((StgClosure *)frame);
4098 switch (info->i.type) {
4102 StgUpdateFrame *upd = (StgUpdateFrame *)frame;
4104 if (upd->updatee->header.info == &stg_BLACKHOLE_info) {
4106 // found a BLACKHOLE'd update frame; we've been here
4107 // before, in a previous GC, so just break out.
4109 // Mark the end of the gap, if we're in one.
4110 if (current_gap_size != 0) {
4111 gap = (struct stack_gap *)(frame-sizeofW(StgUpdateFrame));
4114 frame += sizeofW(StgUpdateFrame);
4115 goto done_traversing;
4118 if (prev_was_update_frame) {
4120 TICK_UPD_SQUEEZED();
4121 /* wasn't there something about update squeezing and ticky to be
4122 * sorted out? oh yes: we aren't counting each enter properly
4123 * in this case. See the log somewhere. KSW 1999-04-21
4125 * Check two things: that the two update frames don't point to
4126 * the same object, and that the updatee_bypass isn't already an
4127 * indirection. Both of these cases only happen when we're in a
4128 * block hole-style loop (and there are multiple update frames
4129 * on the stack pointing to the same closure), but they can both
4130 * screw us up if we don't check.
4132 if (upd->updatee != updatee && !closure_IND(upd->updatee)) {
4133 // this wakes the threads up
4134 UPD_IND_NOLOCK(upd->updatee, updatee);
4137 // now mark this update frame as a stack gap. The gap
4138 // marker resides in the bottom-most update frame of
4139 // the series of adjacent frames, and covers all the
4140 // frames in this series.
4141 current_gap_size += sizeofW(StgUpdateFrame);
4142 ((struct stack_gap *)frame)->gap_size = current_gap_size;
4143 ((struct stack_gap *)frame)->next_gap = gap;
4145 frame += sizeofW(StgUpdateFrame);
4149 // single update frame, or the topmost update frame in a series
4151 StgBlockingQueue *bh = (StgBlockingQueue *)upd->updatee;
4153 // Do lazy black-holing
4154 if (bh->header.info != &stg_BLACKHOLE_info &&
4155 bh->header.info != &stg_BLACKHOLE_BQ_info &&
4156 bh->header.info != &stg_CAF_BLACKHOLE_info) {
4157 #if (!defined(LAZY_BLACKHOLING)) && defined(DEBUG)
4158 belch("Unexpected lazy BHing required at 0x%04x",(int)bh);
4161 /* zero out the slop so that the sanity checker can tell
4162 * where the next closure is.
4165 StgInfoTable *bh_info = get_itbl(bh);
4166 nat np = bh_info->layout.payload.ptrs,
4167 nw = bh_info->layout.payload.nptrs, i;
4168 /* don't zero out slop for a THUNK_SELECTOR,
4169 * because its layout info is used for a
4170 * different purpose, and it's exactly the
4171 * same size as a BLACKHOLE in any case.
4173 if (bh_info->type != THUNK_SELECTOR) {
4174 for (i = np; i < np + nw; i++) {
4175 ((StgClosure *)bh)->payload[i] = 0;
4181 // We pretend that bh is now dead.
4182 LDV_recordDead_FILL_SLOP_DYNAMIC((StgClosure *)bh);
4184 // Todo: maybe use SET_HDR() and remove LDV_recordCreate()?
4185 SET_INFO(bh,&stg_BLACKHOLE_info);
4187 // We pretend that bh has just been created.
4188 LDV_recordCreate(bh);
4192 prev_was_update_frame = rtsTrue;
4193 updatee = upd->updatee;
4194 frame += sizeofW(StgUpdateFrame);
4200 prev_was_update_frame = rtsFalse;
4202 // we're not in a gap... check whether this is the end of a gap
4203 // (an update frame can't be the end of a gap).
4204 if (current_gap_size != 0) {
4205 gap = (struct stack_gap *) (frame - sizeofW(StgUpdateFrame));
4207 current_gap_size = 0;
4209 frame += stack_frame_sizeW((StgClosure *)frame);
4216 // Now we have a stack with gaps in it, and we have to walk down
4217 // shoving the stack up to fill in the gaps. A diagram might
4221 // | ********* | <- sp
4225 // | stack_gap | <- gap | chunk_size
4227 // | ......... | <- gap_end v
4233 // 'sp' points the the current top-of-stack
4234 // 'gap' points to the stack_gap structure inside the gap
4235 // ***** indicates real stack data
4236 // ..... indicates gap
4237 // <empty> indicates unused
4241 void *gap_start, *next_gap_start, *gap_end;
4244 next_gap_start = (void *)((unsigned char*)gap + sizeof(StgUpdateFrame));
4245 sp = next_gap_start;
4247 while ((StgPtr)gap > tso->sp) {
4249 // we're working in *bytes* now...
4250 gap_start = next_gap_start;
4251 gap_end = (void*) ((unsigned char*)gap_start - gap->gap_size * sizeof(W_));
4253 gap = gap->next_gap;
4254 next_gap_start = (void *)((unsigned char*)gap + sizeof(StgUpdateFrame));
4256 chunk_size = (unsigned char*)gap_end - (unsigned char*)next_gap_start;
4257 (unsigned char*)sp -= chunk_size;
4258 memmove(sp, next_gap_start, chunk_size);
4261 tso->sp = (StgPtr)sp;
4265 /* -----------------------------------------------------------------------------
4268 * We have to prepare for GC - this means doing lazy black holing
4269 * here. We also take the opportunity to do stack squeezing if it's
4271 * -------------------------------------------------------------------------- */
4273 threadPaused(StgTSO *tso)
4275 if ( RtsFlags.GcFlags.squeezeUpdFrames == rtsTrue )
4276 threadSqueezeStack(tso); // does black holing too
4278 threadLazyBlackHole(tso);
4281 /* -----------------------------------------------------------------------------
4283 * -------------------------------------------------------------------------- */
4287 printMutOnceList(generation *gen)
4289 StgMutClosure *p, *next;
4291 p = gen->mut_once_list;
4294 fprintf(stderr, "@@ Mut once list %p: ", gen->mut_once_list);
4295 for (; p != END_MUT_LIST; p = next, next = p->mut_link) {
4296 fprintf(stderr, "%p (%s), ",
4297 p, info_type((StgClosure *)p));
4299 fputc('\n', stderr);
4303 printMutableList(generation *gen)
4305 StgMutClosure *p, *next;
4310 fprintf(stderr, "@@ Mutable list %p: ", gen->mut_list);
4311 for (; p != END_MUT_LIST; p = next, next = p->mut_link) {
4312 fprintf(stderr, "%p (%s), ",
4313 p, info_type((StgClosure *)p));
4315 fputc('\n', stderr);
4318 STATIC_INLINE rtsBool
4319 maybeLarge(StgClosure *closure)
4321 StgInfoTable *info = get_itbl(closure);
4323 /* closure types that may be found on the new_large_objects list;
4324 see scavenge_large */
4325 return (info->type == MUT_ARR_PTRS ||
4326 info->type == MUT_ARR_PTRS_FROZEN ||
4327 info->type == TSO ||
4328 info->type == ARR_WORDS);