1 /* -----------------------------------------------------------------------------
2 * $Id: GC.c,v 1.147 2003/02/12 11:59:49 simonmar 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"
27 #include "StablePriv.h"
29 #include "ParTicky.h" // ToDo: move into Rts.h
30 #include "GCCompact.h"
32 #if defined(GRAN) || defined(PAR)
33 # include "GranSimRts.h"
34 # include "ParallelRts.h"
38 # include "ParallelDebug.h"
43 #if defined(RTS_GTK_FRONTPANEL)
44 #include "FrontPanel.h"
47 #include "RetainerProfile.h"
48 #include "LdvProfile.h"
52 /* STATIC OBJECT LIST.
55 * We maintain a linked list of static objects that are still live.
56 * The requirements for this list are:
58 * - we need to scan the list while adding to it, in order to
59 * scavenge all the static objects (in the same way that
60 * breadth-first scavenging works for dynamic objects).
62 * - we need to be able to tell whether an object is already on
63 * the list, to break loops.
65 * Each static object has a "static link field", which we use for
66 * linking objects on to the list. We use a stack-type list, consing
67 * objects on the front as they are added (this means that the
68 * scavenge phase is depth-first, not breadth-first, but that
71 * A separate list is kept for objects that have been scavenged
72 * already - this is so that we can zero all the marks afterwards.
74 * An object is on the list if its static link field is non-zero; this
75 * means that we have to mark the end of the list with '1', not NULL.
77 * Extra notes for generational GC:
79 * Each generation has a static object list associated with it. When
80 * collecting generations up to N, we treat the static object lists
81 * from generations > N as roots.
83 * We build up a static object list while collecting generations 0..N,
84 * which is then appended to the static object list of generation N+1.
86 static StgClosure* static_objects; // live static objects
87 StgClosure* scavenged_static_objects; // static objects scavenged so far
89 /* N is the oldest generation being collected, where the generations
90 * are numbered starting at 0. A major GC (indicated by the major_gc
91 * flag) is when we're collecting all generations. We only attempt to
92 * deal with static objects and GC CAFs when doing a major GC.
95 static rtsBool major_gc;
97 /* Youngest generation that objects should be evacuated to in
98 * evacuate(). (Logically an argument to evacuate, but it's static
99 * a lot of the time so we optimise it into a global variable).
105 StgWeak *old_weak_ptr_list; // also pending finaliser list
107 /* Which stage of processing various kinds of weak pointer are we at?
108 * (see traverse_weak_ptr_list() below for discussion).
110 typedef enum { WeakPtrs, WeakThreads, WeakDone } WeakStage;
111 static WeakStage weak_stage;
113 /* List of all threads during GC
115 static StgTSO *old_all_threads;
116 StgTSO *resurrected_threads;
118 /* Flag indicating failure to evacuate an object to the desired
121 static rtsBool failed_to_evac;
123 /* Old to-space (used for two-space collector only)
125 static bdescr *old_to_blocks;
127 /* Data used for allocation area sizing.
129 static lnat new_blocks; // blocks allocated during this GC
130 static lnat g0s0_pcnt_kept = 30; // percentage of g0s0 live at last minor GC
132 /* Used to avoid long recursion due to selector thunks
134 static lnat thunk_selector_depth = 0;
135 #define MAX_THUNK_SELECTOR_DEPTH 8
137 /* -----------------------------------------------------------------------------
138 Static function declarations
139 -------------------------------------------------------------------------- */
141 static bdescr * gc_alloc_block ( step *stp );
142 static void mark_root ( StgClosure **root );
143 static StgClosure * evacuate ( StgClosure *q );
144 static void zero_static_object_list ( StgClosure* first_static );
145 static void zero_mutable_list ( StgMutClosure *first );
147 static rtsBool traverse_weak_ptr_list ( void );
148 static void mark_weak_ptr_list ( StgWeak **list );
150 static StgClosure * eval_thunk_selector ( nat field, StgSelector * p );
153 static void scavenge ( step * );
154 static void scavenge_mark_stack ( void );
155 static void scavenge_stack ( StgPtr p, StgPtr stack_end );
156 static rtsBool scavenge_one ( StgPtr p );
157 static void scavenge_large ( step * );
158 static void scavenge_static ( void );
159 static void scavenge_mutable_list ( generation *g );
160 static void scavenge_mut_once_list ( generation *g );
162 static void scavenge_large_bitmap ( StgPtr p,
163 StgLargeBitmap *large_bitmap,
166 #if 0 && defined(DEBUG)
167 static void gcCAFs ( void );
170 /* -----------------------------------------------------------------------------
171 inline functions etc. for dealing with the mark bitmap & stack.
172 -------------------------------------------------------------------------- */
174 #define MARK_STACK_BLOCKS 4
176 static bdescr *mark_stack_bdescr;
177 static StgPtr *mark_stack;
178 static StgPtr *mark_sp;
179 static StgPtr *mark_splim;
181 // Flag and pointers used for falling back to a linear scan when the
182 // mark stack overflows.
183 static rtsBool mark_stack_overflowed;
184 static bdescr *oldgen_scan_bd;
185 static StgPtr oldgen_scan;
187 static inline rtsBool
188 mark_stack_empty(void)
190 return mark_sp == mark_stack;
193 static inline rtsBool
194 mark_stack_full(void)
196 return mark_sp >= mark_splim;
200 reset_mark_stack(void)
202 mark_sp = mark_stack;
206 push_mark_stack(StgPtr p)
217 /* -----------------------------------------------------------------------------
218 Allocate a new to-space block in the given step.
219 -------------------------------------------------------------------------- */
222 gc_alloc_block(step *stp)
224 bdescr *bd = allocBlock();
225 bd->gen_no = stp->gen_no;
229 // blocks in to-space in generations up to and including N
230 // get the BF_EVACUATED flag.
231 if (stp->gen_no <= N) {
232 bd->flags = BF_EVACUATED;
237 // Start a new to-space block, chain it on after the previous one.
238 if (stp->hp_bd == NULL) {
241 stp->hp_bd->free = stp->hp;
242 stp->hp_bd->link = bd;
247 stp->hpLim = stp->hp + BLOCK_SIZE_W;
255 /* -----------------------------------------------------------------------------
258 Rough outline of the algorithm: for garbage collecting generation N
259 (and all younger generations):
261 - follow all pointers in the root set. the root set includes all
262 mutable objects in all generations (mutable_list and mut_once_list).
264 - for each pointer, evacuate the object it points to into either
266 + to-space of the step given by step->to, which is the next
267 highest step in this generation or the first step in the next
268 generation if this is the last step.
270 + to-space of generations[evac_gen]->steps[0], if evac_gen != 0.
271 When we evacuate an object we attempt to evacuate
272 everything it points to into the same generation - this is
273 achieved by setting evac_gen to the desired generation. If
274 we can't do this, then an entry in the mut_once list has to
275 be made for the cross-generation pointer.
277 + if the object is already in a generation > N, then leave
280 - repeatedly scavenge to-space from each step in each generation
281 being collected until no more objects can be evacuated.
283 - free from-space in each step, and set from-space = to-space.
285 Locks held: sched_mutex
287 -------------------------------------------------------------------------- */
290 GarbageCollect ( void (*get_roots)(evac_fn), rtsBool force_major_gc )
294 lnat live, allocated, collected = 0, copied = 0;
295 lnat oldgen_saved_blocks = 0;
299 CostCentreStack *prev_CCS;
302 #if defined(DEBUG) && defined(GRAN)
303 IF_DEBUG(gc, belch("@@ Starting garbage collection at %ld (%lx)\n",
307 #ifndef mingw32_TARGET_OS
312 // tell the stats department that we've started a GC
315 // Init stats and print par specific (timing) info
316 PAR_TICKY_PAR_START();
318 // attribute any costs to CCS_GC
324 /* Approximate how much we allocated.
325 * Todo: only when generating stats?
327 allocated = calcAllocated();
329 /* Figure out which generation to collect
331 if (force_major_gc) {
332 N = RtsFlags.GcFlags.generations - 1;
336 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
337 if (generations[g].steps[0].n_blocks +
338 generations[g].steps[0].n_large_blocks
339 >= generations[g].max_blocks) {
343 major_gc = (N == RtsFlags.GcFlags.generations-1);
346 #ifdef RTS_GTK_FRONTPANEL
347 if (RtsFlags.GcFlags.frontpanel) {
348 updateFrontPanelBeforeGC(N);
352 // check stack sanity *before* GC (ToDo: check all threads)
354 // ToDo!: check sanity IF_DEBUG(sanity, checkTSOsSanity());
356 IF_DEBUG(sanity, checkFreeListSanity());
358 /* Initialise the static object lists
360 static_objects = END_OF_STATIC_LIST;
361 scavenged_static_objects = END_OF_STATIC_LIST;
363 /* zero the mutable list for the oldest generation (see comment by
364 * zero_mutable_list below).
367 zero_mutable_list(generations[RtsFlags.GcFlags.generations-1].mut_once_list);
370 /* Save the old to-space if we're doing a two-space collection
372 if (RtsFlags.GcFlags.generations == 1) {
373 old_to_blocks = g0s0->to_blocks;
374 g0s0->to_blocks = NULL;
377 /* Keep a count of how many new blocks we allocated during this GC
378 * (used for resizing the allocation area, later).
382 // Initialise to-space in all the generations/steps that we're
385 for (g = 0; g <= N; g++) {
386 generations[g].mut_once_list = END_MUT_LIST;
387 generations[g].mut_list = END_MUT_LIST;
389 for (s = 0; s < generations[g].n_steps; s++) {
391 // generation 0, step 0 doesn't need to-space
392 if (g == 0 && s == 0 && RtsFlags.GcFlags.generations > 1) {
396 stp = &generations[g].steps[s];
397 ASSERT(stp->gen_no == g);
399 // start a new to-space for this step.
402 stp->to_blocks = NULL;
404 // allocate the first to-space block; extra blocks will be
405 // chained on as necessary.
406 bd = gc_alloc_block(stp);
408 stp->scan = bd->start;
411 // initialise the large object queues.
412 stp->new_large_objects = NULL;
413 stp->scavenged_large_objects = NULL;
414 stp->n_scavenged_large_blocks = 0;
416 // mark the large objects as not evacuated yet
417 for (bd = stp->large_objects; bd; bd = bd->link) {
418 bd->flags = BF_LARGE;
421 // for a compacted step, we need to allocate the bitmap
422 if (stp->is_compacted) {
423 nat bitmap_size; // in bytes
424 bdescr *bitmap_bdescr;
427 bitmap_size = stp->n_blocks * BLOCK_SIZE / (sizeof(W_)*BITS_PER_BYTE);
429 if (bitmap_size > 0) {
430 bitmap_bdescr = allocGroup((nat)BLOCK_ROUND_UP(bitmap_size)
432 stp->bitmap = bitmap_bdescr;
433 bitmap = bitmap_bdescr->start;
435 IF_DEBUG(gc, belch("bitmap_size: %d, bitmap: %p",
436 bitmap_size, bitmap););
438 // don't forget to fill it with zeros!
439 memset(bitmap, 0, bitmap_size);
441 // for each block in this step, point to its bitmap from the
443 for (bd=stp->blocks; bd != NULL; bd = bd->link) {
444 bd->u.bitmap = bitmap;
445 bitmap += BLOCK_SIZE_W / (sizeof(W_)*BITS_PER_BYTE);
452 /* make sure the older generations have at least one block to
453 * allocate into (this makes things easier for copy(), see below).
455 for (g = N+1; g < RtsFlags.GcFlags.generations; g++) {
456 for (s = 0; s < generations[g].n_steps; s++) {
457 stp = &generations[g].steps[s];
458 if (stp->hp_bd == NULL) {
459 ASSERT(stp->blocks == NULL);
460 bd = gc_alloc_block(stp);
464 /* Set the scan pointer for older generations: remember we
465 * still have to scavenge objects that have been promoted. */
467 stp->scan_bd = stp->hp_bd;
468 stp->to_blocks = NULL;
469 stp->n_to_blocks = 0;
470 stp->new_large_objects = NULL;
471 stp->scavenged_large_objects = NULL;
472 stp->n_scavenged_large_blocks = 0;
476 /* Allocate a mark stack if we're doing a major collection.
479 mark_stack_bdescr = allocGroup(MARK_STACK_BLOCKS);
480 mark_stack = (StgPtr *)mark_stack_bdescr->start;
481 mark_sp = mark_stack;
482 mark_splim = mark_stack + (MARK_STACK_BLOCKS * BLOCK_SIZE_W);
484 mark_stack_bdescr = NULL;
487 /* -----------------------------------------------------------------------
488 * follow all the roots that we know about:
489 * - mutable lists from each generation > N
490 * we want to *scavenge* these roots, not evacuate them: they're not
491 * going to move in this GC.
492 * Also: do them in reverse generation order. This is because we
493 * often want to promote objects that are pointed to by older
494 * generations early, so we don't have to repeatedly copy them.
495 * Doing the generations in reverse order ensures that we don't end
496 * up in the situation where we want to evac an object to gen 3 and
497 * it has already been evaced to gen 2.
501 for (g = RtsFlags.GcFlags.generations-1; g > N; g--) {
502 generations[g].saved_mut_list = generations[g].mut_list;
503 generations[g].mut_list = END_MUT_LIST;
506 // Do the mut-once lists first
507 for (g = RtsFlags.GcFlags.generations-1; g > N; g--) {
508 IF_PAR_DEBUG(verbose,
509 printMutOnceList(&generations[g]));
510 scavenge_mut_once_list(&generations[g]);
512 for (st = generations[g].n_steps-1; st >= 0; st--) {
513 scavenge(&generations[g].steps[st]);
517 for (g = RtsFlags.GcFlags.generations-1; g > N; g--) {
518 IF_PAR_DEBUG(verbose,
519 printMutableList(&generations[g]));
520 scavenge_mutable_list(&generations[g]);
522 for (st = generations[g].n_steps-1; st >= 0; st--) {
523 scavenge(&generations[g].steps[st]);
528 /* follow roots from the CAF list (used by GHCi)
533 /* follow all the roots that the application knows about.
536 get_roots(mark_root);
539 /* And don't forget to mark the TSO if we got here direct from
541 /* Not needed in a seq version?
543 CurrentTSO = (StgTSO *)MarkRoot((StgClosure *)CurrentTSO);
547 // Mark the entries in the GALA table of the parallel system
548 markLocalGAs(major_gc);
549 // Mark all entries on the list of pending fetches
550 markPendingFetches(major_gc);
553 /* Mark the weak pointer list, and prepare to detect dead weak
556 mark_weak_ptr_list(&weak_ptr_list);
557 old_weak_ptr_list = weak_ptr_list;
558 weak_ptr_list = NULL;
559 weak_stage = WeakPtrs;
561 /* The all_threads list is like the weak_ptr_list.
562 * See traverse_weak_ptr_list() for the details.
564 old_all_threads = all_threads;
565 all_threads = END_TSO_QUEUE;
566 resurrected_threads = END_TSO_QUEUE;
568 /* Mark the stable pointer table.
570 markStablePtrTable(mark_root);
574 /* ToDo: To fix the caf leak, we need to make the commented out
575 * parts of this code do something sensible - as described in
578 extern void markHugsObjects(void);
583 /* -------------------------------------------------------------------------
584 * Repeatedly scavenge all the areas we know about until there's no
585 * more scavenging to be done.
592 // scavenge static objects
593 if (major_gc && static_objects != END_OF_STATIC_LIST) {
594 IF_DEBUG(sanity, checkStaticObjects(static_objects));
598 /* When scavenging the older generations: Objects may have been
599 * evacuated from generations <= N into older generations, and we
600 * need to scavenge these objects. We're going to try to ensure that
601 * any evacuations that occur move the objects into at least the
602 * same generation as the object being scavenged, otherwise we
603 * have to create new entries on the mutable list for the older
607 // scavenge each step in generations 0..maxgen
613 // scavenge objects in compacted generation
614 if (mark_stack_overflowed || oldgen_scan_bd != NULL ||
615 (mark_stack_bdescr != NULL && !mark_stack_empty())) {
616 scavenge_mark_stack();
620 for (gen = RtsFlags.GcFlags.generations; --gen >= 0; ) {
621 for (st = generations[gen].n_steps; --st >= 0; ) {
622 if (gen == 0 && st == 0 && RtsFlags.GcFlags.generations > 1) {
625 stp = &generations[gen].steps[st];
627 if (stp->hp_bd != stp->scan_bd || stp->scan < stp->hp) {
632 if (stp->new_large_objects != NULL) {
641 if (flag) { goto loop; }
643 // must be last... invariant is that everything is fully
644 // scavenged at this point.
645 if (traverse_weak_ptr_list()) { // returns rtsTrue if evaced something
650 /* Update the pointers from the "main thread" list - these are
651 * treated as weak pointers because we want to allow a main thread
652 * to get a BlockedOnDeadMVar exception in the same way as any other
653 * thread. Note that the threads should all have been retained by
654 * GC by virtue of being on the all_threads list, we're just
655 * updating pointers here.
660 for (m = main_threads; m != NULL; m = m->link) {
661 tso = (StgTSO *) isAlive((StgClosure *)m->tso);
663 barf("main thread has been GC'd");
670 // Reconstruct the Global Address tables used in GUM
671 rebuildGAtables(major_gc);
672 IF_DEBUG(sanity, checkLAGAtable(rtsTrue/*check closures, too*/));
675 // Now see which stable names are still alive.
678 // Tidy the end of the to-space chains
679 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
680 for (s = 0; s < generations[g].n_steps; s++) {
681 stp = &generations[g].steps[s];
682 if (!(g == 0 && s == 0 && RtsFlags.GcFlags.generations > 1)) {
683 ASSERT(Bdescr(stp->hp) == stp->hp_bd);
684 stp->hp_bd->free = stp->hp;
690 // We call processHeapClosureForDead() on every closure destroyed during
691 // the current garbage collection, so we invoke LdvCensusForDead().
692 if (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_LDV
693 || RtsFlags.ProfFlags.bioSelector != NULL)
697 // NO MORE EVACUATION AFTER THIS POINT!
698 // Finally: compaction of the oldest generation.
699 if (major_gc && oldest_gen->steps[0].is_compacted) {
700 // save number of blocks for stats
701 oldgen_saved_blocks = oldest_gen->steps[0].n_blocks;
705 IF_DEBUG(sanity, checkGlobalTSOList(rtsFalse));
707 /* run through all the generations/steps and tidy up
709 copied = new_blocks * BLOCK_SIZE_W;
710 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
713 generations[g].collections++; // for stats
716 for (s = 0; s < generations[g].n_steps; s++) {
718 stp = &generations[g].steps[s];
720 if (!(g == 0 && s == 0 && RtsFlags.GcFlags.generations > 1)) {
721 // stats information: how much we copied
723 copied -= stp->hp_bd->start + BLOCK_SIZE_W -
728 // for generations we collected...
731 // rough calculation of garbage collected, for stats output
732 if (stp->is_compacted) {
733 collected += (oldgen_saved_blocks - stp->n_blocks) * BLOCK_SIZE_W;
735 collected += stp->n_blocks * BLOCK_SIZE_W;
738 /* free old memory and shift to-space into from-space for all
739 * the collected steps (except the allocation area). These
740 * freed blocks will probaby be quickly recycled.
742 if (!(g == 0 && s == 0)) {
743 if (stp->is_compacted) {
744 // for a compacted step, just shift the new to-space
745 // onto the front of the now-compacted existing blocks.
746 for (bd = stp->to_blocks; bd != NULL; bd = bd->link) {
747 bd->flags &= ~BF_EVACUATED; // now from-space
749 // tack the new blocks on the end of the existing blocks
750 if (stp->blocks == NULL) {
751 stp->blocks = stp->to_blocks;
753 for (bd = stp->blocks; bd != NULL; bd = next) {
756 bd->link = stp->to_blocks;
760 // add the new blocks to the block tally
761 stp->n_blocks += stp->n_to_blocks;
763 freeChain(stp->blocks);
764 stp->blocks = stp->to_blocks;
765 stp->n_blocks = stp->n_to_blocks;
766 for (bd = stp->blocks; bd != NULL; bd = bd->link) {
767 bd->flags &= ~BF_EVACUATED; // now from-space
770 stp->to_blocks = NULL;
771 stp->n_to_blocks = 0;
774 /* LARGE OBJECTS. The current live large objects are chained on
775 * scavenged_large, having been moved during garbage
776 * collection from large_objects. Any objects left on
777 * large_objects list are therefore dead, so we free them here.
779 for (bd = stp->large_objects; bd != NULL; bd = next) {
785 // update the count of blocks used by large objects
786 for (bd = stp->scavenged_large_objects; bd != NULL; bd = bd->link) {
787 bd->flags &= ~BF_EVACUATED;
789 stp->large_objects = stp->scavenged_large_objects;
790 stp->n_large_blocks = stp->n_scavenged_large_blocks;
793 // for older generations...
795 /* For older generations, we need to append the
796 * scavenged_large_object list (i.e. large objects that have been
797 * promoted during this GC) to the large_object list for that step.
799 for (bd = stp->scavenged_large_objects; bd; bd = next) {
801 bd->flags &= ~BF_EVACUATED;
802 dbl_link_onto(bd, &stp->large_objects);
805 // add the new blocks we promoted during this GC
806 stp->n_blocks += stp->n_to_blocks;
807 stp->n_to_blocks = 0;
808 stp->n_large_blocks += stp->n_scavenged_large_blocks;
813 /* Reset the sizes of the older generations when we do a major
816 * CURRENT STRATEGY: make all generations except zero the same size.
817 * We have to stay within the maximum heap size, and leave a certain
818 * percentage of the maximum heap size available to allocate into.
820 if (major_gc && RtsFlags.GcFlags.generations > 1) {
821 nat live, size, min_alloc;
822 nat max = RtsFlags.GcFlags.maxHeapSize;
823 nat gens = RtsFlags.GcFlags.generations;
825 // live in the oldest generations
826 live = oldest_gen->steps[0].n_blocks +
827 oldest_gen->steps[0].n_large_blocks;
829 // default max size for all generations except zero
830 size = stg_max(live * RtsFlags.GcFlags.oldGenFactor,
831 RtsFlags.GcFlags.minOldGenSize);
833 // minimum size for generation zero
834 min_alloc = stg_max((RtsFlags.GcFlags.pcFreeHeap * max) / 200,
835 RtsFlags.GcFlags.minAllocAreaSize);
837 // Auto-enable compaction when the residency reaches a
838 // certain percentage of the maximum heap size (default: 30%).
839 if (RtsFlags.GcFlags.generations > 1 &&
840 (RtsFlags.GcFlags.compact ||
842 oldest_gen->steps[0].n_blocks >
843 (RtsFlags.GcFlags.compactThreshold * max) / 100))) {
844 oldest_gen->steps[0].is_compacted = 1;
845 // fprintf(stderr,"compaction: on\n", live);
847 oldest_gen->steps[0].is_compacted = 0;
848 // fprintf(stderr,"compaction: off\n", live);
851 // if we're going to go over the maximum heap size, reduce the
852 // size of the generations accordingly. The calculation is
853 // different if compaction is turned on, because we don't need
854 // to double the space required to collect the old generation.
857 // this test is necessary to ensure that the calculations
858 // below don't have any negative results - we're working
859 // with unsigned values here.
860 if (max < min_alloc) {
864 if (oldest_gen->steps[0].is_compacted) {
865 if ( (size + (size - 1) * (gens - 2) * 2) + min_alloc > max ) {
866 size = (max - min_alloc) / ((gens - 1) * 2 - 1);
869 if ( (size * (gens - 1) * 2) + min_alloc > max ) {
870 size = (max - min_alloc) / ((gens - 1) * 2);
880 fprintf(stderr,"live: %d, min_alloc: %d, size : %d, max = %d\n", live,
881 min_alloc, size, max);
884 for (g = 0; g < gens; g++) {
885 generations[g].max_blocks = size;
889 // Guess the amount of live data for stats.
892 /* Free the small objects allocated via allocate(), since this will
893 * all have been copied into G0S1 now.
895 if (small_alloc_list != NULL) {
896 freeChain(small_alloc_list);
898 small_alloc_list = NULL;
902 alloc_blocks_lim = RtsFlags.GcFlags.minAllocAreaSize;
904 // Start a new pinned_object_block
905 pinned_object_block = NULL;
907 /* Free the mark stack.
909 if (mark_stack_bdescr != NULL) {
910 freeGroup(mark_stack_bdescr);
915 for (g = 0; g <= N; g++) {
916 for (s = 0; s < generations[g].n_steps; s++) {
917 stp = &generations[g].steps[s];
918 if (stp->is_compacted && stp->bitmap != NULL) {
919 freeGroup(stp->bitmap);
924 /* Two-space collector:
925 * Free the old to-space, and estimate the amount of live data.
927 if (RtsFlags.GcFlags.generations == 1) {
930 if (old_to_blocks != NULL) {
931 freeChain(old_to_blocks);
933 for (bd = g0s0->to_blocks; bd != NULL; bd = bd->link) {
934 bd->flags = 0; // now from-space
937 /* For a two-space collector, we need to resize the nursery. */
939 /* set up a new nursery. Allocate a nursery size based on a
940 * function of the amount of live data (by default a factor of 2)
941 * Use the blocks from the old nursery if possible, freeing up any
944 * If we get near the maximum heap size, then adjust our nursery
945 * size accordingly. If the nursery is the same size as the live
946 * data (L), then we need 3L bytes. We can reduce the size of the
947 * nursery to bring the required memory down near 2L bytes.
949 * A normal 2-space collector would need 4L bytes to give the same
950 * performance we get from 3L bytes, reducing to the same
951 * performance at 2L bytes.
953 blocks = g0s0->n_to_blocks;
955 if ( RtsFlags.GcFlags.maxHeapSize != 0 &&
956 blocks * RtsFlags.GcFlags.oldGenFactor * 2 >
957 RtsFlags.GcFlags.maxHeapSize ) {
958 long adjusted_blocks; // signed on purpose
961 adjusted_blocks = (RtsFlags.GcFlags.maxHeapSize - 2 * blocks);
962 IF_DEBUG(gc, belch("@@ Near maximum heap size of 0x%x blocks, blocks = %d, adjusted to %ld", RtsFlags.GcFlags.maxHeapSize, blocks, adjusted_blocks));
963 pc_free = adjusted_blocks * 100 / RtsFlags.GcFlags.maxHeapSize;
964 if (pc_free < RtsFlags.GcFlags.pcFreeHeap) /* might even be < 0 */ {
967 blocks = adjusted_blocks;
970 blocks *= RtsFlags.GcFlags.oldGenFactor;
971 if (blocks < RtsFlags.GcFlags.minAllocAreaSize) {
972 blocks = RtsFlags.GcFlags.minAllocAreaSize;
975 resizeNursery(blocks);
978 /* Generational collector:
979 * If the user has given us a suggested heap size, adjust our
980 * allocation area to make best use of the memory available.
983 if (RtsFlags.GcFlags.heapSizeSuggestion) {
985 nat needed = calcNeeded(); // approx blocks needed at next GC
987 /* Guess how much will be live in generation 0 step 0 next time.
988 * A good approximation is obtained by finding the
989 * percentage of g0s0 that was live at the last minor GC.
992 g0s0_pcnt_kept = (new_blocks * 100) / g0s0->n_blocks;
995 /* Estimate a size for the allocation area based on the
996 * information available. We might end up going slightly under
997 * or over the suggested heap size, but we should be pretty
1000 * Formula: suggested - needed
1001 * ----------------------------
1002 * 1 + g0s0_pcnt_kept/100
1004 * where 'needed' is the amount of memory needed at the next
1005 * collection for collecting all steps except g0s0.
1008 (((long)RtsFlags.GcFlags.heapSizeSuggestion - (long)needed) * 100) /
1009 (100 + (long)g0s0_pcnt_kept);
1011 if (blocks < (long)RtsFlags.GcFlags.minAllocAreaSize) {
1012 blocks = RtsFlags.GcFlags.minAllocAreaSize;
1015 resizeNursery((nat)blocks);
1018 // we might have added extra large blocks to the nursery, so
1019 // resize back to minAllocAreaSize again.
1020 resizeNursery(RtsFlags.GcFlags.minAllocAreaSize);
1024 // mark the garbage collected CAFs as dead
1025 #if 0 && defined(DEBUG) // doesn't work at the moment
1026 if (major_gc) { gcCAFs(); }
1030 // resetStaticObjectForRetainerProfiling() must be called before
1032 resetStaticObjectForRetainerProfiling();
1035 // zero the scavenged static object list
1037 zero_static_object_list(scavenged_static_objects);
1040 // Reset the nursery
1043 RELEASE_LOCK(&sched_mutex);
1045 // start any pending finalizers
1046 scheduleFinalizers(old_weak_ptr_list);
1048 // send exceptions to any threads which were about to die
1049 resurrectThreads(resurrected_threads);
1051 ACQUIRE_LOCK(&sched_mutex);
1053 // Update the stable pointer hash table.
1054 updateStablePtrTable(major_gc);
1056 // check sanity after GC
1057 IF_DEBUG(sanity, checkSanity());
1059 // extra GC trace info
1060 IF_DEBUG(gc, statDescribeGens());
1063 // symbol-table based profiling
1064 /* heapCensus(to_blocks); */ /* ToDo */
1067 // restore enclosing cost centre
1072 // check for memory leaks if sanity checking is on
1073 IF_DEBUG(sanity, memInventory());
1075 #ifdef RTS_GTK_FRONTPANEL
1076 if (RtsFlags.GcFlags.frontpanel) {
1077 updateFrontPanelAfterGC( N, live );
1081 // ok, GC over: tell the stats department what happened.
1082 stat_endGC(allocated, collected, live, copied, N);
1084 #ifndef mingw32_TARGET_OS
1085 // unblock signals again
1086 unblockUserSignals();
1093 /* -----------------------------------------------------------------------------
1096 traverse_weak_ptr_list is called possibly many times during garbage
1097 collection. It returns a flag indicating whether it did any work
1098 (i.e. called evacuate on any live pointers).
1100 Invariant: traverse_weak_ptr_list is called when the heap is in an
1101 idempotent state. That means that there are no pending
1102 evacuate/scavenge operations. This invariant helps the weak
1103 pointer code decide which weak pointers are dead - if there are no
1104 new live weak pointers, then all the currently unreachable ones are
1107 For generational GC: we just don't try to finalize weak pointers in
1108 older generations than the one we're collecting. This could
1109 probably be optimised by keeping per-generation lists of weak
1110 pointers, but for a few weak pointers this scheme will work.
1112 There are three distinct stages to processing weak pointers:
1114 - weak_stage == WeakPtrs
1116 We process all the weak pointers whos keys are alive (evacuate
1117 their values and finalizers), and repeat until we can find no new
1118 live keys. If no live keys are found in this pass, then we
1119 evacuate the finalizers of all the dead weak pointers in order to
1122 - weak_stage == WeakThreads
1124 Now, we discover which *threads* are still alive. Pointers to
1125 threads from the all_threads and main thread lists are the
1126 weakest of all: a pointers from the finalizer of a dead weak
1127 pointer can keep a thread alive. Any threads found to be unreachable
1128 are evacuated and placed on the resurrected_threads list so we
1129 can send them a signal later.
1131 - weak_stage == WeakDone
1133 No more evacuation is done.
1135 -------------------------------------------------------------------------- */
1138 traverse_weak_ptr_list(void)
1140 StgWeak *w, **last_w, *next_w;
1142 rtsBool flag = rtsFalse;
1144 switch (weak_stage) {
1150 /* doesn't matter where we evacuate values/finalizers to, since
1151 * these pointers are treated as roots (iff the keys are alive).
1155 last_w = &old_weak_ptr_list;
1156 for (w = old_weak_ptr_list; w != NULL; w = next_w) {
1158 /* There might be a DEAD_WEAK on the list if finalizeWeak# was
1159 * called on a live weak pointer object. Just remove it.
1161 if (w->header.info == &stg_DEAD_WEAK_info) {
1162 next_w = ((StgDeadWeak *)w)->link;
1167 switch (get_itbl(w)->type) {
1170 next_w = (StgWeak *)((StgEvacuated *)w)->evacuee;
1175 /* Now, check whether the key is reachable.
1177 new = isAlive(w->key);
1180 // evacuate the value and finalizer
1181 w->value = evacuate(w->value);
1182 w->finalizer = evacuate(w->finalizer);
1183 // remove this weak ptr from the old_weak_ptr list
1185 // and put it on the new weak ptr list
1187 w->link = weak_ptr_list;
1190 IF_DEBUG(weak, belch("Weak pointer still alive at %p -> %p",
1195 last_w = &(w->link);
1201 barf("traverse_weak_ptr_list: not WEAK");
1205 /* If we didn't make any changes, then we can go round and kill all
1206 * the dead weak pointers. The old_weak_ptr list is used as a list
1207 * of pending finalizers later on.
1209 if (flag == rtsFalse) {
1210 for (w = old_weak_ptr_list; w; w = w->link) {
1211 w->finalizer = evacuate(w->finalizer);
1214 // Next, move to the WeakThreads stage after fully
1215 // scavenging the finalizers we've just evacuated.
1216 weak_stage = WeakThreads;
1222 /* Now deal with the all_threads list, which behaves somewhat like
1223 * the weak ptr list. If we discover any threads that are about to
1224 * become garbage, we wake them up and administer an exception.
1227 StgTSO *t, *tmp, *next, **prev;
1229 prev = &old_all_threads;
1230 for (t = old_all_threads; t != END_TSO_QUEUE; t = next) {
1232 (StgClosure *)tmp = isAlive((StgClosure *)t);
1238 ASSERT(get_itbl(t)->type == TSO);
1239 switch (t->what_next) {
1240 case ThreadRelocated:
1245 case ThreadComplete:
1246 // finshed or died. The thread might still be alive, but we
1247 // don't keep it on the all_threads list. Don't forget to
1248 // stub out its global_link field.
1249 next = t->global_link;
1250 t->global_link = END_TSO_QUEUE;
1258 // not alive (yet): leave this thread on the
1259 // old_all_threads list.
1260 prev = &(t->global_link);
1261 next = t->global_link;
1264 // alive: move this thread onto the all_threads list.
1265 next = t->global_link;
1266 t->global_link = all_threads;
1273 /* And resurrect any threads which were about to become garbage.
1276 StgTSO *t, *tmp, *next;
1277 for (t = old_all_threads; t != END_TSO_QUEUE; t = next) {
1278 next = t->global_link;
1279 (StgClosure *)tmp = evacuate((StgClosure *)t);
1280 tmp->global_link = resurrected_threads;
1281 resurrected_threads = tmp;
1285 weak_stage = WeakDone; // *now* we're done,
1286 return rtsTrue; // but one more round of scavenging, please
1289 barf("traverse_weak_ptr_list");
1294 /* -----------------------------------------------------------------------------
1295 After GC, the live weak pointer list may have forwarding pointers
1296 on it, because a weak pointer object was evacuated after being
1297 moved to the live weak pointer list. We remove those forwarding
1300 Also, we don't consider weak pointer objects to be reachable, but
1301 we must nevertheless consider them to be "live" and retain them.
1302 Therefore any weak pointer objects which haven't as yet been
1303 evacuated need to be evacuated now.
1304 -------------------------------------------------------------------------- */
1308 mark_weak_ptr_list ( StgWeak **list )
1310 StgWeak *w, **last_w;
1313 for (w = *list; w; w = w->link) {
1314 // w might be WEAK, EVACUATED, or DEAD_WEAK (actually CON_STATIC) here
1315 ASSERT(w->header.info == &stg_DEAD_WEAK_info
1316 || get_itbl(w)->type == WEAK || get_itbl(w)->type == EVACUATED);
1317 (StgClosure *)w = evacuate((StgClosure *)w);
1319 last_w = &(w->link);
1323 /* -----------------------------------------------------------------------------
1324 isAlive determines whether the given closure is still alive (after
1325 a garbage collection) or not. It returns the new address of the
1326 closure if it is alive, or NULL otherwise.
1328 NOTE: Use it before compaction only!
1329 -------------------------------------------------------------------------- */
1333 isAlive(StgClosure *p)
1335 const StgInfoTable *info;
1340 ASSERT(LOOKS_LIKE_CLOSURE_PTR(p));
1343 // ignore static closures
1345 // ToDo: for static closures, check the static link field.
1346 // Problem here is that we sometimes don't set the link field, eg.
1347 // for static closures with an empty SRT or CONSTR_STATIC_NOCAFs.
1349 if (!HEAP_ALLOCED(p)) {
1353 // ignore closures in generations that we're not collecting.
1355 if (bd->gen_no > N) {
1359 // if it's a pointer into to-space, then we're done
1360 if (bd->flags & BF_EVACUATED) {
1364 // large objects use the evacuated flag
1365 if (bd->flags & BF_LARGE) {
1369 // check the mark bit for compacted steps
1370 if (bd->step->is_compacted && is_marked((P_)p,bd)) {
1374 switch (info->type) {
1379 case IND_OLDGEN: // rely on compatible layout with StgInd
1380 case IND_OLDGEN_PERM:
1381 // follow indirections
1382 p = ((StgInd *)p)->indirectee;
1387 return ((StgEvacuated *)p)->evacuee;
1390 if (((StgTSO *)p)->what_next == ThreadRelocated) {
1391 p = (StgClosure *)((StgTSO *)p)->link;
1404 mark_root(StgClosure **root)
1406 *root = evacuate(*root);
1409 static __inline__ void
1410 upd_evacuee(StgClosure *p, StgClosure *dest)
1412 // Source object must be in from-space:
1413 ASSERT((Bdescr((P_)p)->flags & BF_EVACUATED) == 0);
1414 // not true: (ToDo: perhaps it should be)
1415 // ASSERT(Bdescr((P_)dest)->flags & BF_EVACUATED);
1416 p->header.info = &stg_EVACUATED_info;
1417 ((StgEvacuated *)p)->evacuee = dest;
1421 static __inline__ StgClosure *
1422 copy(StgClosure *src, nat size, step *stp)
1427 nat size_org = size;
1430 TICK_GC_WORDS_COPIED(size);
1431 /* Find out where we're going, using the handy "to" pointer in
1432 * the step of the source object. If it turns out we need to
1433 * evacuate to an older generation, adjust it here (see comment
1436 if (stp->gen_no < evac_gen) {
1437 #ifdef NO_EAGER_PROMOTION
1438 failed_to_evac = rtsTrue;
1440 stp = &generations[evac_gen].steps[0];
1444 /* chain a new block onto the to-space for the destination step if
1447 if (stp->hp + size >= stp->hpLim) {
1448 gc_alloc_block(stp);
1451 for(to = stp->hp, from = (P_)src; size>0; --size) {
1457 upd_evacuee(src,(StgClosure *)dest);
1459 // We store the size of the just evacuated object in the LDV word so that
1460 // the profiler can guess the position of the next object later.
1461 SET_EVACUAEE_FOR_LDV(src, size_org);
1463 return (StgClosure *)dest;
1466 /* Special version of copy() for when we only want to copy the info
1467 * pointer of an object, but reserve some padding after it. This is
1468 * used to optimise evacuation of BLACKHOLEs.
1473 copyPart(StgClosure *src, nat size_to_reserve, nat size_to_copy, step *stp)
1478 nat size_to_copy_org = size_to_copy;
1481 TICK_GC_WORDS_COPIED(size_to_copy);
1482 if (stp->gen_no < evac_gen) {
1483 #ifdef NO_EAGER_PROMOTION
1484 failed_to_evac = rtsTrue;
1486 stp = &generations[evac_gen].steps[0];
1490 if (stp->hp + size_to_reserve >= stp->hpLim) {
1491 gc_alloc_block(stp);
1494 for(to = stp->hp, from = (P_)src; size_to_copy>0; --size_to_copy) {
1499 stp->hp += size_to_reserve;
1500 upd_evacuee(src,(StgClosure *)dest);
1502 // We store the size of the just evacuated object in the LDV word so that
1503 // the profiler can guess the position of the next object later.
1504 // size_to_copy_org is wrong because the closure already occupies size_to_reserve
1506 SET_EVACUAEE_FOR_LDV(src, size_to_reserve);
1508 if (size_to_reserve - size_to_copy_org > 0)
1509 FILL_SLOP(stp->hp - 1, (int)(size_to_reserve - size_to_copy_org));
1511 return (StgClosure *)dest;
1515 /* -----------------------------------------------------------------------------
1516 Evacuate a large object
1518 This just consists of removing the object from the (doubly-linked)
1519 step->large_objects list, and linking it on to the (singly-linked)
1520 step->new_large_objects list, from where it will be scavenged later.
1522 Convention: bd->flags has BF_EVACUATED set for a large object
1523 that has been evacuated, or unset otherwise.
1524 -------------------------------------------------------------------------- */
1528 evacuate_large(StgPtr p)
1530 bdescr *bd = Bdescr(p);
1533 // object must be at the beginning of the block (or be a ByteArray)
1534 ASSERT(get_itbl((StgClosure *)p)->type == ARR_WORDS ||
1535 (((W_)p & BLOCK_MASK) == 0));
1537 // already evacuated?
1538 if (bd->flags & BF_EVACUATED) {
1539 /* Don't forget to set the failed_to_evac flag if we didn't get
1540 * the desired destination (see comments in evacuate()).
1542 if (bd->gen_no < evac_gen) {
1543 failed_to_evac = rtsTrue;
1544 TICK_GC_FAILED_PROMOTION();
1550 // remove from large_object list
1552 bd->u.back->link = bd->link;
1553 } else { // first object in the list
1554 stp->large_objects = bd->link;
1557 bd->link->u.back = bd->u.back;
1560 /* link it on to the evacuated large object list of the destination step
1563 if (stp->gen_no < evac_gen) {
1564 #ifdef NO_EAGER_PROMOTION
1565 failed_to_evac = rtsTrue;
1567 stp = &generations[evac_gen].steps[0];
1572 bd->gen_no = stp->gen_no;
1573 bd->link = stp->new_large_objects;
1574 stp->new_large_objects = bd;
1575 bd->flags |= BF_EVACUATED;
1578 /* -----------------------------------------------------------------------------
1579 Adding a MUT_CONS to an older generation.
1581 This is necessary from time to time when we end up with an
1582 old-to-new generation pointer in a non-mutable object. We defer
1583 the promotion until the next GC.
1584 -------------------------------------------------------------------------- */
1587 mkMutCons(StgClosure *ptr, generation *gen)
1592 stp = &gen->steps[0];
1594 /* chain a new block onto the to-space for the destination step if
1597 if (stp->hp + sizeofW(StgIndOldGen) >= stp->hpLim) {
1598 gc_alloc_block(stp);
1601 q = (StgMutVar *)stp->hp;
1602 stp->hp += sizeofW(StgMutVar);
1604 SET_HDR(q,&stg_MUT_CONS_info,CCS_GC);
1606 recordOldToNewPtrs((StgMutClosure *)q);
1608 return (StgClosure *)q;
1611 /* -----------------------------------------------------------------------------
1614 This is called (eventually) for every live object in the system.
1616 The caller to evacuate specifies a desired generation in the
1617 evac_gen global variable. The following conditions apply to
1618 evacuating an object which resides in generation M when we're
1619 collecting up to generation N
1623 else evac to step->to
1625 if M < evac_gen evac to evac_gen, step 0
1627 if the object is already evacuated, then we check which generation
1630 if M >= evac_gen do nothing
1631 if M < evac_gen set failed_to_evac flag to indicate that we
1632 didn't manage to evacuate this object into evac_gen.
1634 -------------------------------------------------------------------------- */
1637 evacuate(StgClosure *q)
1642 const StgInfoTable *info;
1645 if (HEAP_ALLOCED(q)) {
1648 if (bd->gen_no > N) {
1649 /* Can't evacuate this object, because it's in a generation
1650 * older than the ones we're collecting. Let's hope that it's
1651 * in evac_gen or older, or we will have to arrange to track
1652 * this pointer using the mutable list.
1654 if (bd->gen_no < evac_gen) {
1656 failed_to_evac = rtsTrue;
1657 TICK_GC_FAILED_PROMOTION();
1662 /* evacuate large objects by re-linking them onto a different list.
1664 if (bd->flags & BF_LARGE) {
1666 if (info->type == TSO &&
1667 ((StgTSO *)q)->what_next == ThreadRelocated) {
1668 q = (StgClosure *)((StgTSO *)q)->link;
1671 evacuate_large((P_)q);
1675 /* If the object is in a step that we're compacting, then we
1676 * need to use an alternative evacuate procedure.
1678 if (bd->step->is_compacted) {
1679 if (!is_marked((P_)q,bd)) {
1681 if (mark_stack_full()) {
1682 mark_stack_overflowed = rtsTrue;
1685 push_mark_stack((P_)q);
1693 else stp = NULL; // make sure copy() will crash if HEAP_ALLOCED is wrong
1696 // make sure the info pointer is into text space
1697 ASSERT(LOOKS_LIKE_CLOSURE_PTR(q));
1700 switch (info -> type) {
1704 return copy(q,sizeW_fromITBL(info),stp);
1708 StgWord w = (StgWord)q->payload[0];
1709 if (q->header.info == Czh_con_info &&
1710 // unsigned, so always true: (StgChar)w >= MIN_CHARLIKE &&
1711 (StgChar)w <= MAX_CHARLIKE) {
1712 return (StgClosure *)CHARLIKE_CLOSURE((StgChar)w);
1714 if (q->header.info == Izh_con_info &&
1715 (StgInt)w >= MIN_INTLIKE && (StgInt)w <= MAX_INTLIKE) {
1716 return (StgClosure *)INTLIKE_CLOSURE((StgInt)w);
1718 // else, fall through ...
1724 return copy(q,sizeofW(StgHeader)+1,stp);
1726 case THUNK_1_0: // here because of MIN_UPD_SIZE
1731 #ifdef NO_PROMOTE_THUNKS
1732 if (bd->gen_no == 0 &&
1733 bd->step->no != 0 &&
1734 bd->step->no == generations[bd->gen_no].n_steps-1) {
1738 return copy(q,sizeofW(StgHeader)+2,stp);
1746 return copy(q,sizeofW(StgHeader)+2,stp);
1752 case IND_OLDGEN_PERM:
1757 return copy(q,sizeW_fromITBL(info),stp);
1760 case SE_CAF_BLACKHOLE:
1763 return copyPart(q,BLACKHOLE_sizeW(),sizeofW(StgHeader),stp);
1766 to = copy(q,BLACKHOLE_sizeW(),stp);
1769 case THUNK_SELECTOR:
1773 if (thunk_selector_depth > MAX_THUNK_SELECTOR_DEPTH) {
1774 return copy(q,THUNK_SELECTOR_sizeW(),stp);
1777 p = eval_thunk_selector(info->layout.selector_offset,
1781 return copy(q,THUNK_SELECTOR_sizeW(),stp);
1783 // q is still BLACKHOLE'd.
1784 thunk_selector_depth++;
1786 thunk_selector_depth--;
1789 // We store the size of the just evacuated object in the
1790 // LDV word so that the profiler can guess the position of
1791 // the next object later.
1792 SET_EVACUAEE_FOR_LDV(q, THUNK_SELECTOR_sizeW());
1800 // follow chains of indirections, don't evacuate them
1801 q = ((StgInd*)q)->indirectee;
1805 if (info->srt_len > 0 && major_gc &&
1806 THUNK_STATIC_LINK((StgClosure *)q) == NULL) {
1807 THUNK_STATIC_LINK((StgClosure *)q) = static_objects;
1808 static_objects = (StgClosure *)q;
1813 if (info->srt_len > 0 && major_gc &&
1814 FUN_STATIC_LINK((StgClosure *)q) == NULL) {
1815 FUN_STATIC_LINK((StgClosure *)q) = static_objects;
1816 static_objects = (StgClosure *)q;
1821 /* If q->saved_info != NULL, then it's a revertible CAF - it'll be
1822 * on the CAF list, so don't do anything with it here (we'll
1823 * scavenge it later).
1826 && ((StgIndStatic *)q)->saved_info == NULL
1827 && IND_STATIC_LINK((StgClosure *)q) == NULL) {
1828 IND_STATIC_LINK((StgClosure *)q) = static_objects;
1829 static_objects = (StgClosure *)q;
1834 if (major_gc && STATIC_LINK(info,(StgClosure *)q) == NULL) {
1835 STATIC_LINK(info,(StgClosure *)q) = static_objects;
1836 static_objects = (StgClosure *)q;
1840 case CONSTR_INTLIKE:
1841 case CONSTR_CHARLIKE:
1842 case CONSTR_NOCAF_STATIC:
1843 /* no need to put these on the static linked list, they don't need
1857 // shouldn't see these
1858 barf("evacuate: stack frame at %p\n", q);
1862 return copy(q,pap_sizeW((StgPAP*)q),stp);
1865 return copy(q,ap_stack_sizeW((StgAP_STACK*)q),stp);
1868 /* Already evacuated, just return the forwarding address.
1869 * HOWEVER: if the requested destination generation (evac_gen) is
1870 * older than the actual generation (because the object was
1871 * already evacuated to a younger generation) then we have to
1872 * set the failed_to_evac flag to indicate that we couldn't
1873 * manage to promote the object to the desired generation.
1875 if (evac_gen > 0) { // optimisation
1876 StgClosure *p = ((StgEvacuated*)q)->evacuee;
1877 if (HEAP_ALLOCED(p) && Bdescr((P_)p)->gen_no < evac_gen) {
1878 failed_to_evac = rtsTrue;
1879 TICK_GC_FAILED_PROMOTION();
1882 return ((StgEvacuated*)q)->evacuee;
1885 // just copy the block
1886 return copy(q,arr_words_sizeW((StgArrWords *)q),stp);
1889 case MUT_ARR_PTRS_FROZEN:
1890 // just copy the block
1891 return copy(q,mut_arr_ptrs_sizeW((StgMutArrPtrs *)q),stp);
1895 StgTSO *tso = (StgTSO *)q;
1897 /* Deal with redirected TSOs (a TSO that's had its stack enlarged).
1899 if (tso->what_next == ThreadRelocated) {
1900 q = (StgClosure *)tso->link;
1904 /* To evacuate a small TSO, we need to relocate the update frame
1908 StgTSO *new_tso = (StgTSO *)copy((StgClosure *)tso,tso_sizeW(tso),stp);
1909 move_TSO(tso, new_tso);
1910 return (StgClosure *)new_tso;
1915 case RBH: // cf. BLACKHOLE_BQ
1917 //StgInfoTable *rip = get_closure_info(q, &size, &ptrs, &nonptrs, &vhs, str);
1918 to = copy(q,BLACKHOLE_sizeW(),stp);
1919 //ToDo: derive size etc from reverted IP
1920 //to = copy(q,size,stp);
1922 belch("@@ evacuate: RBH %p (%s) to %p (%s)",
1923 q, info_type(q), to, info_type(to)));
1928 ASSERT(sizeofW(StgBlockedFetch) >= MIN_NONUPD_SIZE);
1929 to = copy(q,sizeofW(StgBlockedFetch),stp);
1931 belch("@@ evacuate: %p (%s) to %p (%s)",
1932 q, info_type(q), to, info_type(to)));
1939 ASSERT(sizeofW(StgBlockedFetch) >= MIN_UPD_SIZE);
1940 to = copy(q,sizeofW(StgFetchMe),stp);
1942 belch("@@ evacuate: %p (%s) to %p (%s)",
1943 q, info_type(q), to, info_type(to)));
1947 ASSERT(sizeofW(StgBlockedFetch) >= MIN_UPD_SIZE);
1948 to = copy(q,sizeofW(StgFetchMeBlockingQueue),stp);
1950 belch("@@ evacuate: %p (%s) to %p (%s)",
1951 q, info_type(q), to, info_type(to)));
1956 barf("evacuate: strange closure type %d", (int)(info->type));
1962 /* -----------------------------------------------------------------------------
1963 Evaluate a THUNK_SELECTOR if possible.
1965 returns: NULL if we couldn't evaluate this THUNK_SELECTOR, or
1966 a closure pointer if we evaluated it and this is the result. Note
1967 that "evaluating" the THUNK_SELECTOR doesn't necessarily mean
1968 reducing it to HNF, just that we have eliminated the selection.
1969 The result might be another thunk, or even another THUNK_SELECTOR.
1971 If the return value is non-NULL, the original selector thunk has
1972 been BLACKHOLE'd, and should be updated with an indirection or a
1973 forwarding pointer. If the return value is NULL, then the selector
1975 -------------------------------------------------------------------------- */
1978 eval_thunk_selector( nat field, StgSelector * p )
1981 const StgInfoTable *info_ptr;
1982 StgClosure *selectee;
1984 selectee = p->selectee;
1986 // Save the real info pointer (NOTE: not the same as get_itbl()).
1987 info_ptr = p->header.info;
1989 // If the THUNK_SELECTOR is in a generation that we are not
1990 // collecting, then bail out early. We won't be able to save any
1991 // space in any case, and updating with an indirection is trickier
1993 if (Bdescr((StgPtr)p)->gen_no > N) {
1997 // BLACKHOLE the selector thunk, since it is now under evaluation.
1998 // This is important to stop us going into an infinite loop if
1999 // this selector thunk eventually refers to itself.
2000 SET_INFO(p,&stg_BLACKHOLE_info);
2004 info = get_itbl(selectee);
2005 switch (info->type) {
2013 case CONSTR_NOCAF_STATIC:
2014 // check that the size is in range
2015 ASSERT(field < (StgWord32)(info->layout.payload.ptrs +
2016 info->layout.payload.nptrs));
2018 return selectee->payload[field];
2023 case IND_OLDGEN_PERM:
2024 selectee = ((StgInd *)selectee)->indirectee;
2028 // We don't follow pointers into to-space; the constructor
2029 // has already been evacuated, so we won't save any space
2030 // leaks by evaluating this selector thunk anyhow.
2034 // We can't easily tell whether the indirectee is into
2035 // from or to-space, so just bail out here.
2038 case THUNK_SELECTOR:
2042 // check that we don't recurse too much, re-using the
2043 // depth bound also used in evacuate().
2044 thunk_selector_depth++;
2045 if (thunk_selector_depth > MAX_THUNK_SELECTOR_DEPTH) {
2049 val = eval_thunk_selector(info->layout.selector_offset,
2050 (StgSelector *)selectee);
2052 thunk_selector_depth--;
2057 // We evaluated this selector thunk, so update it with
2058 // an indirection. NOTE: we don't use UPD_IND here,
2059 // because we are guaranteed that p is in a generation
2060 // that we are collecting, and we never want to put the
2061 // indirection on a mutable list.
2063 // For the purposes of LDV profiling, we have destroyed
2064 // the original selector thunk.
2065 SET_INFO(p, info_ptr);
2066 LDV_recordDead_FILL_SLOP_DYNAMIC(selectee);
2068 ((StgInd *)selectee)->indirectee = val;
2069 SET_INFO(selectee,&stg_IND_info);
2071 // For the purposes of LDV profiling, we have created an
2073 LDV_recordCreate(selectee);
2089 case SE_CAF_BLACKHOLE:
2102 // not evaluated yet
2106 barf("eval_thunk_selector: strange selectee %d",
2110 // We didn't manage to evaluate this thunk; restore the old info pointer
2111 SET_INFO(p, info_ptr);
2115 /* -----------------------------------------------------------------------------
2116 move_TSO is called to update the TSO structure after it has been
2117 moved from one place to another.
2118 -------------------------------------------------------------------------- */
2121 move_TSO (StgTSO *src, StgTSO *dest)
2125 // relocate the stack pointers...
2126 diff = (StgPtr)dest - (StgPtr)src; // In *words*
2127 dest->sp = (StgPtr)dest->sp + diff;
2130 /* evacuate the SRT. If srt_len is zero, then there isn't an
2131 * srt field in the info table. That's ok, because we'll
2132 * never dereference it.
2135 scavenge_srt (StgClosure **srt, nat srt_len)
2137 StgClosure **srt_end;
2139 srt_end = srt + srt_len;
2141 for (; srt < srt_end; srt++) {
2142 /* Special-case to handle references to closures hiding out in DLLs, since
2143 double indirections required to get at those. The code generator knows
2144 which is which when generating the SRT, so it stores the (indirect)
2145 reference to the DLL closure in the table by first adding one to it.
2146 We check for this here, and undo the addition before evacuating it.
2148 If the SRT entry hasn't got bit 0 set, the SRT entry points to a
2149 closure that's fixed at link-time, and no extra magic is required.
2151 #ifdef ENABLE_WIN32_DLL_SUPPORT
2152 if ( (unsigned long)(*srt) & 0x1 ) {
2153 evacuate(*stgCast(StgClosure**,(stgCast(unsigned long, *srt) & ~0x1)));
2165 scavenge_thunk_srt(const StgInfoTable *info)
2167 StgThunkInfoTable *thunk_info;
2169 thunk_info = itbl_to_thunk_itbl(info);
2170 scavenge_srt((StgClosure **)thunk_info->srt, thunk_info->i.srt_len);
2174 scavenge_fun_srt(const StgInfoTable *info)
2176 StgFunInfoTable *fun_info;
2178 fun_info = itbl_to_fun_itbl(info);
2179 scavenge_srt((StgClosure **)fun_info->srt, fun_info->i.srt_len);
2183 scavenge_ret_srt(const StgInfoTable *info)
2185 StgRetInfoTable *ret_info;
2187 ret_info = itbl_to_ret_itbl(info);
2188 scavenge_srt((StgClosure **)ret_info->srt, ret_info->i.srt_len);
2191 /* -----------------------------------------------------------------------------
2193 -------------------------------------------------------------------------- */
2196 scavengeTSO (StgTSO *tso)
2198 // chase the link field for any TSOs on the same queue
2199 (StgClosure *)tso->link = evacuate((StgClosure *)tso->link);
2200 if ( tso->why_blocked == BlockedOnMVar
2201 || tso->why_blocked == BlockedOnBlackHole
2202 || tso->why_blocked == BlockedOnException
2204 || tso->why_blocked == BlockedOnGA
2205 || tso->why_blocked == BlockedOnGA_NoSend
2208 tso->block_info.closure = evacuate(tso->block_info.closure);
2210 if ( tso->blocked_exceptions != NULL ) {
2211 tso->blocked_exceptions =
2212 (StgTSO *)evacuate((StgClosure *)tso->blocked_exceptions);
2215 // scavenge this thread's stack
2216 scavenge_stack(tso->sp, &(tso->stack[tso->stack_size]));
2219 /* -----------------------------------------------------------------------------
2220 Blocks of function args occur on the stack (at the top) and
2222 -------------------------------------------------------------------------- */
2224 static inline StgPtr
2225 scavenge_arg_block (StgFunInfoTable *fun_info, StgClosure **args)
2232 switch (fun_info->fun_type) {
2234 bitmap = BITMAP_BITS(fun_info->bitmap);
2235 size = BITMAP_SIZE(fun_info->bitmap);
2238 size = ((StgLargeBitmap *)fun_info->bitmap)->size;
2239 scavenge_large_bitmap(p, (StgLargeBitmap *)fun_info->bitmap, size);
2243 bitmap = BITMAP_BITS(stg_arg_bitmaps[fun_info->fun_type]);
2244 size = BITMAP_SIZE(stg_arg_bitmaps[fun_info->fun_type]);
2247 if ((bitmap & 1) == 0) {
2248 (StgClosure *)*p = evacuate((StgClosure *)*p);
2251 bitmap = bitmap >> 1;
2259 static inline StgPtr
2260 scavenge_PAP (StgPAP *pap)
2263 StgWord bitmap, size;
2264 StgFunInfoTable *fun_info;
2266 pap->fun = evacuate(pap->fun);
2267 fun_info = get_fun_itbl(pap->fun);
2268 ASSERT(fun_info->i.type != PAP);
2270 p = (StgPtr)pap->payload;
2273 switch (fun_info->fun_type) {
2275 bitmap = BITMAP_BITS(fun_info->bitmap);
2278 scavenge_large_bitmap(p, (StgLargeBitmap *)fun_info->bitmap, size);
2282 scavenge_large_bitmap((StgPtr)pap->payload, BCO_BITMAP(pap->fun), size);
2286 bitmap = BITMAP_BITS(stg_arg_bitmaps[fun_info->fun_type]);
2290 if ((bitmap & 1) == 0) {
2291 (StgClosure *)*p = evacuate((StgClosure *)*p);
2294 bitmap = bitmap >> 1;
2302 /* -----------------------------------------------------------------------------
2303 Scavenge a given step until there are no more objects in this step
2306 evac_gen is set by the caller to be either zero (for a step in a
2307 generation < N) or G where G is the generation of the step being
2310 We sometimes temporarily change evac_gen back to zero if we're
2311 scavenging a mutable object where early promotion isn't such a good
2313 -------------------------------------------------------------------------- */
2321 nat saved_evac_gen = evac_gen;
2326 failed_to_evac = rtsFalse;
2328 /* scavenge phase - standard breadth-first scavenging of the
2332 while (bd != stp->hp_bd || p < stp->hp) {
2334 // If we're at the end of this block, move on to the next block
2335 if (bd != stp->hp_bd && p == bd->free) {
2341 ASSERT(LOOKS_LIKE_CLOSURE_PTR(p));
2342 info = get_itbl((StgClosure *)p);
2344 ASSERT(thunk_selector_depth == 0);
2347 switch (info->type) {
2350 /* treat MVars specially, because we don't want to evacuate the
2351 * mut_link field in the middle of the closure.
2354 StgMVar *mvar = ((StgMVar *)p);
2356 (StgClosure *)mvar->head = evacuate((StgClosure *)mvar->head);
2357 (StgClosure *)mvar->tail = evacuate((StgClosure *)mvar->tail);
2358 (StgClosure *)mvar->value = evacuate((StgClosure *)mvar->value);
2359 evac_gen = saved_evac_gen;
2360 recordMutable((StgMutClosure *)mvar);
2361 failed_to_evac = rtsFalse; // mutable.
2362 p += sizeofW(StgMVar);
2367 scavenge_fun_srt(info);
2368 ((StgClosure *)p)->payload[1] = evacuate(((StgClosure *)p)->payload[1]);
2369 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
2370 p += sizeofW(StgHeader) + 2;
2374 scavenge_thunk_srt(info);
2376 ((StgClosure *)p)->payload[1] = evacuate(((StgClosure *)p)->payload[1]);
2377 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
2378 p += sizeofW(StgHeader) + 2;
2382 scavenge_thunk_srt(info);
2383 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
2384 p += sizeofW(StgHeader) + 2; // MIN_UPD_SIZE
2388 scavenge_fun_srt(info);
2390 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
2391 p += sizeofW(StgHeader) + 1;
2395 scavenge_thunk_srt(info);
2396 p += sizeofW(StgHeader) + 2; // MIN_UPD_SIZE
2400 scavenge_fun_srt(info);
2402 p += sizeofW(StgHeader) + 1;
2406 scavenge_thunk_srt(info);
2407 p += sizeofW(StgHeader) + 2;
2411 scavenge_fun_srt(info);
2413 p += sizeofW(StgHeader) + 2;
2417 scavenge_thunk_srt(info);
2418 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
2419 p += sizeofW(StgHeader) + 2;
2423 scavenge_fun_srt(info);
2425 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
2426 p += sizeofW(StgHeader) + 2;
2430 scavenge_fun_srt(info);
2434 scavenge_thunk_srt(info);
2446 end = (P_)((StgClosure *)p)->payload + info->layout.payload.ptrs;
2447 for (p = (P_)((StgClosure *)p)->payload; p < end; p++) {
2448 (StgClosure *)*p = evacuate((StgClosure *)*p);
2450 p += info->layout.payload.nptrs;
2455 if (stp->gen->no != 0) {
2458 // No need to call LDV_recordDead_FILL_SLOP_DYNAMIC() because an
2459 // IND_OLDGEN_PERM closure is larger than an IND_PERM closure.
2460 LDV_recordDead((StgClosure *)p, sizeofW(StgInd));
2463 // Todo: maybe use SET_HDR() and remove LDV_recordCreate()?
2465 SET_INFO(((StgClosure *)p), &stg_IND_OLDGEN_PERM_info);
2468 // We pretend that p has just been created.
2469 LDV_recordCreate((StgClosure *)p);
2473 case IND_OLDGEN_PERM:
2474 ((StgIndOldGen *)p)->indirectee =
2475 evacuate(((StgIndOldGen *)p)->indirectee);
2476 if (failed_to_evac) {
2477 failed_to_evac = rtsFalse;
2478 recordOldToNewPtrs((StgMutClosure *)p);
2480 p += sizeofW(StgIndOldGen);
2485 ((StgMutVar *)p)->var = evacuate(((StgMutVar *)p)->var);
2486 evac_gen = saved_evac_gen;
2487 recordMutable((StgMutClosure *)p);
2488 failed_to_evac = rtsFalse; // mutable anyhow
2489 p += sizeofW(StgMutVar);
2494 failed_to_evac = rtsFalse; // mutable anyhow
2495 p += sizeofW(StgMutVar);
2499 case SE_CAF_BLACKHOLE:
2502 p += BLACKHOLE_sizeW();
2507 StgBlockingQueue *bh = (StgBlockingQueue *)p;
2508 (StgClosure *)bh->blocking_queue =
2509 evacuate((StgClosure *)bh->blocking_queue);
2510 recordMutable((StgMutClosure *)bh);
2511 failed_to_evac = rtsFalse;
2512 p += BLACKHOLE_sizeW();
2516 case THUNK_SELECTOR:
2518 StgSelector *s = (StgSelector *)p;
2519 s->selectee = evacuate(s->selectee);
2520 p += THUNK_SELECTOR_sizeW();
2524 // A chunk of stack saved in a heap object
2527 StgAP_STACK *ap = (StgAP_STACK *)p;
2529 ap->fun = evacuate(ap->fun);
2530 scavenge_stack((StgPtr)ap->payload, (StgPtr)ap->payload + ap->size);
2531 p = (StgPtr)ap->payload + ap->size;
2537 p = scavenge_PAP((StgPAP *)p);
2541 // nothing to follow
2542 p += arr_words_sizeW((StgArrWords *)p);
2546 // follow everything
2550 evac_gen = 0; // repeatedly mutable
2551 next = p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
2552 for (p = (P_)((StgMutArrPtrs *)p)->payload; p < next; p++) {
2553 (StgClosure *)*p = evacuate((StgClosure *)*p);
2555 evac_gen = saved_evac_gen;
2556 recordMutable((StgMutClosure *)q);
2557 failed_to_evac = rtsFalse; // mutable anyhow.
2561 case MUT_ARR_PTRS_FROZEN:
2562 // follow everything
2566 next = p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
2567 for (p = (P_)((StgMutArrPtrs *)p)->payload; p < next; p++) {
2568 (StgClosure *)*p = evacuate((StgClosure *)*p);
2570 // it's tempting to recordMutable() if failed_to_evac is
2571 // false, but that breaks some assumptions (eg. every
2572 // closure on the mutable list is supposed to have the MUT
2573 // flag set, and MUT_ARR_PTRS_FROZEN doesn't).
2579 StgTSO *tso = (StgTSO *)p;
2582 evac_gen = saved_evac_gen;
2583 recordMutable((StgMutClosure *)tso);
2584 failed_to_evac = rtsFalse; // mutable anyhow.
2585 p += tso_sizeW(tso);
2590 case RBH: // cf. BLACKHOLE_BQ
2593 nat size, ptrs, nonptrs, vhs;
2595 StgInfoTable *rip = get_closure_info(p, &size, &ptrs, &nonptrs, &vhs, str);
2597 StgRBH *rbh = (StgRBH *)p;
2598 (StgClosure *)rbh->blocking_queue =
2599 evacuate((StgClosure *)rbh->blocking_queue);
2600 recordMutable((StgMutClosure *)to);
2601 failed_to_evac = rtsFalse; // mutable anyhow.
2603 belch("@@ scavenge: RBH %p (%s) (new blocking_queue link=%p)",
2604 p, info_type(p), (StgClosure *)rbh->blocking_queue));
2605 // ToDo: use size of reverted closure here!
2606 p += BLACKHOLE_sizeW();
2612 StgBlockedFetch *bf = (StgBlockedFetch *)p;
2613 // follow the pointer to the node which is being demanded
2614 (StgClosure *)bf->node =
2615 evacuate((StgClosure *)bf->node);
2616 // follow the link to the rest of the blocking queue
2617 (StgClosure *)bf->link =
2618 evacuate((StgClosure *)bf->link);
2619 if (failed_to_evac) {
2620 failed_to_evac = rtsFalse;
2621 recordMutable((StgMutClosure *)bf);
2624 belch("@@ scavenge: %p (%s); node is now %p; exciting, isn't it",
2625 bf, info_type((StgClosure *)bf),
2626 bf->node, info_type(bf->node)));
2627 p += sizeofW(StgBlockedFetch);
2635 p += sizeofW(StgFetchMe);
2636 break; // nothing to do in this case
2638 case FETCH_ME_BQ: // cf. BLACKHOLE_BQ
2640 StgFetchMeBlockingQueue *fmbq = (StgFetchMeBlockingQueue *)p;
2641 (StgClosure *)fmbq->blocking_queue =
2642 evacuate((StgClosure *)fmbq->blocking_queue);
2643 if (failed_to_evac) {
2644 failed_to_evac = rtsFalse;
2645 recordMutable((StgMutClosure *)fmbq);
2648 belch("@@ scavenge: %p (%s) exciting, isn't it",
2649 p, info_type((StgClosure *)p)));
2650 p += sizeofW(StgFetchMeBlockingQueue);
2656 barf("scavenge: unimplemented/strange closure type %d @ %p",
2660 /* If we didn't manage to promote all the objects pointed to by
2661 * the current object, then we have to designate this object as
2662 * mutable (because it contains old-to-new generation pointers).
2664 if (failed_to_evac) {
2665 failed_to_evac = rtsFalse;
2666 mkMutCons((StgClosure *)q, &generations[evac_gen]);
2674 /* -----------------------------------------------------------------------------
2675 Scavenge everything on the mark stack.
2677 This is slightly different from scavenge():
2678 - we don't walk linearly through the objects, so the scavenger
2679 doesn't need to advance the pointer on to the next object.
2680 -------------------------------------------------------------------------- */
2683 scavenge_mark_stack(void)
2689 evac_gen = oldest_gen->no;
2690 saved_evac_gen = evac_gen;
2693 while (!mark_stack_empty()) {
2694 p = pop_mark_stack();
2696 ASSERT(LOOKS_LIKE_CLOSURE_PTR(p));
2697 info = get_itbl((StgClosure *)p);
2700 switch (info->type) {
2703 /* treat MVars specially, because we don't want to evacuate the
2704 * mut_link field in the middle of the closure.
2707 StgMVar *mvar = ((StgMVar *)p);
2709 (StgClosure *)mvar->head = evacuate((StgClosure *)mvar->head);
2710 (StgClosure *)mvar->tail = evacuate((StgClosure *)mvar->tail);
2711 (StgClosure *)mvar->value = evacuate((StgClosure *)mvar->value);
2712 evac_gen = saved_evac_gen;
2713 failed_to_evac = rtsFalse; // mutable.
2718 scavenge_fun_srt(info);
2719 ((StgClosure *)p)->payload[1] = evacuate(((StgClosure *)p)->payload[1]);
2720 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
2724 scavenge_thunk_srt(info);
2726 ((StgClosure *)p)->payload[1] = evacuate(((StgClosure *)p)->payload[1]);
2727 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
2732 scavenge_fun_srt(info);
2733 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
2738 scavenge_thunk_srt(info);
2741 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
2746 scavenge_fun_srt(info);
2751 scavenge_thunk_srt(info);
2759 scavenge_fun_srt(info);
2763 scavenge_thunk_srt(info);
2775 end = (P_)((StgClosure *)p)->payload + info->layout.payload.ptrs;
2776 for (p = (P_)((StgClosure *)p)->payload; p < end; p++) {
2777 (StgClosure *)*p = evacuate((StgClosure *)*p);
2783 // don't need to do anything here: the only possible case
2784 // is that we're in a 1-space compacting collector, with
2785 // no "old" generation.
2789 case IND_OLDGEN_PERM:
2790 ((StgIndOldGen *)p)->indirectee =
2791 evacuate(((StgIndOldGen *)p)->indirectee);
2792 if (failed_to_evac) {
2793 recordOldToNewPtrs((StgMutClosure *)p);
2795 failed_to_evac = rtsFalse;
2800 ((StgMutVar *)p)->var = evacuate(((StgMutVar *)p)->var);
2801 evac_gen = saved_evac_gen;
2802 failed_to_evac = rtsFalse;
2807 failed_to_evac = rtsFalse;
2811 case SE_CAF_BLACKHOLE:
2819 StgBlockingQueue *bh = (StgBlockingQueue *)p;
2820 (StgClosure *)bh->blocking_queue =
2821 evacuate((StgClosure *)bh->blocking_queue);
2822 failed_to_evac = rtsFalse;
2826 case THUNK_SELECTOR:
2828 StgSelector *s = (StgSelector *)p;
2829 s->selectee = evacuate(s->selectee);
2833 // A chunk of stack saved in a heap object
2836 StgAP_STACK *ap = (StgAP_STACK *)p;
2838 ap->fun = evacuate(ap->fun);
2839 scavenge_stack((StgPtr)ap->payload, (StgPtr)ap->payload + ap->size);
2845 scavenge_PAP((StgPAP *)p);
2849 // follow everything
2853 evac_gen = 0; // repeatedly mutable
2854 next = p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
2855 for (p = (P_)((StgMutArrPtrs *)p)->payload; p < next; p++) {
2856 (StgClosure *)*p = evacuate((StgClosure *)*p);
2858 evac_gen = saved_evac_gen;
2859 failed_to_evac = rtsFalse; // mutable anyhow.
2863 case MUT_ARR_PTRS_FROZEN:
2864 // follow everything
2868 next = p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
2869 for (p = (P_)((StgMutArrPtrs *)p)->payload; p < next; p++) {
2870 (StgClosure *)*p = evacuate((StgClosure *)*p);
2877 StgTSO *tso = (StgTSO *)p;
2880 evac_gen = saved_evac_gen;
2881 failed_to_evac = rtsFalse;
2886 case RBH: // cf. BLACKHOLE_BQ
2889 nat size, ptrs, nonptrs, vhs;
2891 StgInfoTable *rip = get_closure_info(p, &size, &ptrs, &nonptrs, &vhs, str);
2893 StgRBH *rbh = (StgRBH *)p;
2894 (StgClosure *)rbh->blocking_queue =
2895 evacuate((StgClosure *)rbh->blocking_queue);
2896 recordMutable((StgMutClosure *)rbh);
2897 failed_to_evac = rtsFalse; // mutable anyhow.
2899 belch("@@ scavenge: RBH %p (%s) (new blocking_queue link=%p)",
2900 p, info_type(p), (StgClosure *)rbh->blocking_queue));
2906 StgBlockedFetch *bf = (StgBlockedFetch *)p;
2907 // follow the pointer to the node which is being demanded
2908 (StgClosure *)bf->node =
2909 evacuate((StgClosure *)bf->node);
2910 // follow the link to the rest of the blocking queue
2911 (StgClosure *)bf->link =
2912 evacuate((StgClosure *)bf->link);
2913 if (failed_to_evac) {
2914 failed_to_evac = rtsFalse;
2915 recordMutable((StgMutClosure *)bf);
2918 belch("@@ scavenge: %p (%s); node is now %p; exciting, isn't it",
2919 bf, info_type((StgClosure *)bf),
2920 bf->node, info_type(bf->node)));
2928 break; // nothing to do in this case
2930 case FETCH_ME_BQ: // cf. BLACKHOLE_BQ
2932 StgFetchMeBlockingQueue *fmbq = (StgFetchMeBlockingQueue *)p;
2933 (StgClosure *)fmbq->blocking_queue =
2934 evacuate((StgClosure *)fmbq->blocking_queue);
2935 if (failed_to_evac) {
2936 failed_to_evac = rtsFalse;
2937 recordMutable((StgMutClosure *)fmbq);
2940 belch("@@ scavenge: %p (%s) exciting, isn't it",
2941 p, info_type((StgClosure *)p)));
2947 barf("scavenge_mark_stack: unimplemented/strange closure type %d @ %p",
2951 if (failed_to_evac) {
2952 failed_to_evac = rtsFalse;
2953 mkMutCons((StgClosure *)q, &generations[evac_gen]);
2956 // mark the next bit to indicate "scavenged"
2957 mark(q+1, Bdescr(q));
2959 } // while (!mark_stack_empty())
2961 // start a new linear scan if the mark stack overflowed at some point
2962 if (mark_stack_overflowed && oldgen_scan_bd == NULL) {
2963 IF_DEBUG(gc, belch("scavenge_mark_stack: starting linear scan"));
2964 mark_stack_overflowed = rtsFalse;
2965 oldgen_scan_bd = oldest_gen->steps[0].blocks;
2966 oldgen_scan = oldgen_scan_bd->start;
2969 if (oldgen_scan_bd) {
2970 // push a new thing on the mark stack
2972 // find a closure that is marked but not scavenged, and start
2974 while (oldgen_scan < oldgen_scan_bd->free
2975 && !is_marked(oldgen_scan,oldgen_scan_bd)) {
2979 if (oldgen_scan < oldgen_scan_bd->free) {
2981 // already scavenged?
2982 if (is_marked(oldgen_scan+1,oldgen_scan_bd)) {
2983 oldgen_scan += sizeofW(StgHeader) + MIN_NONUPD_SIZE;
2986 push_mark_stack(oldgen_scan);
2987 // ToDo: bump the linear scan by the actual size of the object
2988 oldgen_scan += sizeofW(StgHeader) + MIN_NONUPD_SIZE;
2992 oldgen_scan_bd = oldgen_scan_bd->link;
2993 if (oldgen_scan_bd != NULL) {
2994 oldgen_scan = oldgen_scan_bd->start;
3000 /* -----------------------------------------------------------------------------
3001 Scavenge one object.
3003 This is used for objects that are temporarily marked as mutable
3004 because they contain old-to-new generation pointers. Only certain
3005 objects can have this property.
3006 -------------------------------------------------------------------------- */
3009 scavenge_one(StgPtr p)
3011 const StgInfoTable *info;
3012 nat saved_evac_gen = evac_gen;
3015 ASSERT(LOOKS_LIKE_CLOSURE_PTR(p));
3016 info = get_itbl((StgClosure *)p);
3018 switch (info->type) {
3021 case FUN_1_0: // hardly worth specialising these guys
3041 case IND_OLDGEN_PERM:
3045 end = (StgPtr)((StgClosure *)p)->payload + info->layout.payload.ptrs;
3046 for (q = (StgPtr)((StgClosure *)p)->payload; q < end; q++) {
3047 (StgClosure *)*q = evacuate((StgClosure *)*q);
3053 case SE_CAF_BLACKHOLE:
3058 case THUNK_SELECTOR:
3060 StgSelector *s = (StgSelector *)p;
3061 s->selectee = evacuate(s->selectee);
3066 // nothing to follow
3071 // follow everything
3074 evac_gen = 0; // repeatedly mutable
3075 recordMutable((StgMutClosure *)p);
3076 next = p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
3077 for (p = (P_)((StgMutArrPtrs *)p)->payload; p < next; p++) {
3078 (StgClosure *)*p = evacuate((StgClosure *)*p);
3080 evac_gen = saved_evac_gen;
3081 failed_to_evac = rtsFalse;
3085 case MUT_ARR_PTRS_FROZEN:
3087 // follow everything
3090 next = p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
3091 for (p = (P_)((StgMutArrPtrs *)p)->payload; p < next; p++) {
3092 (StgClosure *)*p = evacuate((StgClosure *)*p);
3099 StgTSO *tso = (StgTSO *)p;
3101 evac_gen = 0; // repeatedly mutable
3103 recordMutable((StgMutClosure *)tso);
3104 evac_gen = saved_evac_gen;
3105 failed_to_evac = rtsFalse;
3111 StgAP_STACK *ap = (StgAP_STACK *)p;
3113 ap->fun = evacuate(ap->fun);
3114 scavenge_stack((StgPtr)ap->payload, (StgPtr)ap->payload + ap->size);
3115 p = (StgPtr)ap->payload + ap->size;
3121 p = scavenge_PAP((StgPAP *)p);
3125 // This might happen if for instance a MUT_CONS was pointing to a
3126 // THUNK which has since been updated. The IND_OLDGEN will
3127 // be on the mutable list anyway, so we don't need to do anything
3132 barf("scavenge_one: strange object %d", (int)(info->type));
3135 no_luck = failed_to_evac;
3136 failed_to_evac = rtsFalse;
3140 /* -----------------------------------------------------------------------------
3141 Scavenging mutable lists.
3143 We treat the mutable list of each generation > N (i.e. all the
3144 generations older than the one being collected) as roots. We also
3145 remove non-mutable objects from the mutable list at this point.
3146 -------------------------------------------------------------------------- */
3149 scavenge_mut_once_list(generation *gen)
3151 const StgInfoTable *info;
3152 StgMutClosure *p, *next, *new_list;
3154 p = gen->mut_once_list;
3155 new_list = END_MUT_LIST;
3159 failed_to_evac = rtsFalse;
3161 for (; p != END_MUT_LIST; p = next, next = p->mut_link) {
3163 ASSERT(LOOKS_LIKE_CLOSURE_PTR(p));
3166 if (info->type==RBH)
3167 info = REVERT_INFOPTR(info); // if it's an RBH, look at the orig closure
3169 switch(info->type) {
3172 case IND_OLDGEN_PERM:
3174 /* Try to pull the indirectee into this generation, so we can
3175 * remove the indirection from the mutable list.
3177 ((StgIndOldGen *)p)->indirectee =
3178 evacuate(((StgIndOldGen *)p)->indirectee);
3180 #if 0 && defined(DEBUG)
3181 if (RtsFlags.DebugFlags.gc)
3182 /* Debugging code to print out the size of the thing we just
3186 StgPtr start = gen->steps[0].scan;
3187 bdescr *start_bd = gen->steps[0].scan_bd;
3189 scavenge(&gen->steps[0]);
3190 if (start_bd != gen->steps[0].scan_bd) {
3191 size += (P_)BLOCK_ROUND_UP(start) - start;
3192 start_bd = start_bd->link;
3193 while (start_bd != gen->steps[0].scan_bd) {
3194 size += BLOCK_SIZE_W;
3195 start_bd = start_bd->link;
3197 size += gen->steps[0].scan -
3198 (P_)BLOCK_ROUND_DOWN(gen->steps[0].scan);
3200 size = gen->steps[0].scan - start;
3202 belch("evac IND_OLDGEN: %ld bytes", size * sizeof(W_));
3206 /* failed_to_evac might happen if we've got more than two
3207 * generations, we're collecting only generation 0, the
3208 * indirection resides in generation 2 and the indirectee is
3211 if (failed_to_evac) {
3212 failed_to_evac = rtsFalse;
3213 p->mut_link = new_list;
3216 /* the mut_link field of an IND_STATIC is overloaded as the
3217 * static link field too (it just so happens that we don't need
3218 * both at the same time), so we need to NULL it out when
3219 * removing this object from the mutable list because the static
3220 * link fields are all assumed to be NULL before doing a major
3228 /* MUT_CONS is a kind of MUT_VAR, except it that we try to remove
3229 * it from the mutable list if possible by promoting whatever it
3232 if (scavenge_one((StgPtr)((StgMutVar *)p)->var)) {
3233 /* didn't manage to promote everything, so put the
3234 * MUT_CONS back on the list.
3236 p->mut_link = new_list;
3242 // shouldn't have anything else on the mutables list
3243 barf("scavenge_mut_once_list: strange object? %d", (int)(info->type));
3247 gen->mut_once_list = new_list;
3252 scavenge_mutable_list(generation *gen)
3254 const StgInfoTable *info;
3255 StgMutClosure *p, *next;
3257 p = gen->saved_mut_list;
3261 failed_to_evac = rtsFalse;
3263 for (; p != END_MUT_LIST; p = next, next = p->mut_link) {
3265 ASSERT(LOOKS_LIKE_CLOSURE_PTR(p));
3268 if (info->type==RBH)
3269 info = REVERT_INFOPTR(info); // if it's an RBH, look at the orig closure
3271 switch(info->type) {
3274 // follow everything
3275 p->mut_link = gen->mut_list;
3280 end = (P_)p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
3281 for (q = (P_)((StgMutArrPtrs *)p)->payload; q < end; q++) {
3282 (StgClosure *)*q = evacuate((StgClosure *)*q);
3287 // Happens if a MUT_ARR_PTRS in the old generation is frozen
3288 case MUT_ARR_PTRS_FROZEN:
3293 end = (P_)p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
3294 for (q = (P_)((StgMutArrPtrs *)p)->payload; q < end; q++) {
3295 (StgClosure *)*q = evacuate((StgClosure *)*q);
3299 if (failed_to_evac) {
3300 failed_to_evac = rtsFalse;
3301 mkMutCons((StgClosure *)p, gen);
3307 ((StgMutVar *)p)->var = evacuate(((StgMutVar *)p)->var);
3308 p->mut_link = gen->mut_list;
3314 StgMVar *mvar = (StgMVar *)p;
3315 (StgClosure *)mvar->head = evacuate((StgClosure *)mvar->head);
3316 (StgClosure *)mvar->tail = evacuate((StgClosure *)mvar->tail);
3317 (StgClosure *)mvar->value = evacuate((StgClosure *)mvar->value);
3318 p->mut_link = gen->mut_list;
3325 StgTSO *tso = (StgTSO *)p;
3329 /* Don't take this TSO off the mutable list - it might still
3330 * point to some younger objects (because we set evac_gen to 0
3333 tso->mut_link = gen->mut_list;
3334 gen->mut_list = (StgMutClosure *)tso;
3340 StgBlockingQueue *bh = (StgBlockingQueue *)p;
3341 (StgClosure *)bh->blocking_queue =
3342 evacuate((StgClosure *)bh->blocking_queue);
3343 p->mut_link = gen->mut_list;
3348 /* Happens if a BLACKHOLE_BQ in the old generation is updated:
3351 case IND_OLDGEN_PERM:
3352 /* Try to pull the indirectee into this generation, so we can
3353 * remove the indirection from the mutable list.
3356 ((StgIndOldGen *)p)->indirectee =
3357 evacuate(((StgIndOldGen *)p)->indirectee);
3360 if (failed_to_evac) {
3361 failed_to_evac = rtsFalse;
3362 p->mut_link = gen->mut_once_list;
3363 gen->mut_once_list = p;
3370 // HWL: check whether all of these are necessary
3372 case RBH: // cf. BLACKHOLE_BQ
3374 // nat size, ptrs, nonptrs, vhs;
3376 // StgInfoTable *rip = get_closure_info(p, &size, &ptrs, &nonptrs, &vhs, str);
3377 StgRBH *rbh = (StgRBH *)p;
3378 (StgClosure *)rbh->blocking_queue =
3379 evacuate((StgClosure *)rbh->blocking_queue);
3380 if (failed_to_evac) {
3381 failed_to_evac = rtsFalse;
3382 recordMutable((StgMutClosure *)rbh);
3384 // ToDo: use size of reverted closure here!
3385 p += BLACKHOLE_sizeW();
3391 StgBlockedFetch *bf = (StgBlockedFetch *)p;
3392 // follow the pointer to the node which is being demanded
3393 (StgClosure *)bf->node =
3394 evacuate((StgClosure *)bf->node);
3395 // follow the link to the rest of the blocking queue
3396 (StgClosure *)bf->link =
3397 evacuate((StgClosure *)bf->link);
3398 if (failed_to_evac) {
3399 failed_to_evac = rtsFalse;
3400 recordMutable((StgMutClosure *)bf);
3402 p += sizeofW(StgBlockedFetch);
3408 barf("scavenge_mutable_list: REMOTE_REF %d", (int)(info->type));
3411 p += sizeofW(StgFetchMe);
3412 break; // nothing to do in this case
3414 case FETCH_ME_BQ: // cf. BLACKHOLE_BQ
3416 StgFetchMeBlockingQueue *fmbq = (StgFetchMeBlockingQueue *)p;
3417 (StgClosure *)fmbq->blocking_queue =
3418 evacuate((StgClosure *)fmbq->blocking_queue);
3419 if (failed_to_evac) {
3420 failed_to_evac = rtsFalse;
3421 recordMutable((StgMutClosure *)fmbq);
3423 p += sizeofW(StgFetchMeBlockingQueue);
3429 // shouldn't have anything else on the mutables list
3430 barf("scavenge_mutable_list: strange object? %d", (int)(info->type));
3437 scavenge_static(void)
3439 StgClosure* p = static_objects;
3440 const StgInfoTable *info;
3442 /* Always evacuate straight to the oldest generation for static
3444 evac_gen = oldest_gen->no;
3446 /* keep going until we've scavenged all the objects on the linked
3448 while (p != END_OF_STATIC_LIST) {
3450 ASSERT(LOOKS_LIKE_CLOSURE_PTR(p));
3453 if (info->type==RBH)
3454 info = REVERT_INFOPTR(info); // if it's an RBH, look at the orig closure
3456 // make sure the info pointer is into text space
3458 /* Take this object *off* the static_objects list,
3459 * and put it on the scavenged_static_objects list.
3461 static_objects = STATIC_LINK(info,p);
3462 STATIC_LINK(info,p) = scavenged_static_objects;
3463 scavenged_static_objects = p;
3465 switch (info -> type) {
3469 StgInd *ind = (StgInd *)p;
3470 ind->indirectee = evacuate(ind->indirectee);
3472 /* might fail to evacuate it, in which case we have to pop it
3473 * back on the mutable list (and take it off the
3474 * scavenged_static list because the static link and mut link
3475 * pointers are one and the same).
3477 if (failed_to_evac) {
3478 failed_to_evac = rtsFalse;
3479 scavenged_static_objects = IND_STATIC_LINK(p);
3480 ((StgMutClosure *)ind)->mut_link = oldest_gen->mut_once_list;
3481 oldest_gen->mut_once_list = (StgMutClosure *)ind;
3487 scavenge_thunk_srt(info);
3491 scavenge_fun_srt(info);
3498 next = (P_)p->payload + info->layout.payload.ptrs;
3499 // evacuate the pointers
3500 for (q = (P_)p->payload; q < next; q++) {
3501 (StgClosure *)*q = evacuate((StgClosure *)*q);
3507 barf("scavenge_static: strange closure %d", (int)(info->type));
3510 ASSERT(failed_to_evac == rtsFalse);
3512 /* get the next static object from the list. Remember, there might
3513 * be more stuff on this list now that we've done some evacuating!
3514 * (static_objects is a global)
3520 /* -----------------------------------------------------------------------------
3521 scavenge a chunk of memory described by a bitmap
3522 -------------------------------------------------------------------------- */
3525 scavenge_large_bitmap( StgPtr p, StgLargeBitmap *large_bitmap, nat size )
3531 bitmap = large_bitmap->bitmap[b];
3532 for (i = 0; i < size; ) {
3533 if ((bitmap & 1) == 0) {
3534 (StgClosure *)*p = evacuate((StgClosure *)*p);
3538 if (i % BITS_IN(W_) == 0) {
3540 bitmap = large_bitmap->bitmap[b];
3542 bitmap = bitmap >> 1;
3547 static inline StgPtr
3548 scavenge_small_bitmap (StgPtr p, nat size, StgWord bitmap)
3551 if ((bitmap & 1) == 0) {
3552 (StgClosure *)*p = evacuate((StgClosure *)*p);
3555 bitmap = bitmap >> 1;
3561 /* -----------------------------------------------------------------------------
3562 scavenge_stack walks over a section of stack and evacuates all the
3563 objects pointed to by it. We can use the same code for walking
3564 AP_STACK_UPDs, since these are just sections of copied stack.
3565 -------------------------------------------------------------------------- */
3569 scavenge_stack(StgPtr p, StgPtr stack_end)
3571 const StgRetInfoTable* info;
3575 //IF_DEBUG(sanity, belch(" scavenging stack between %p and %p", p, stack_end));
3578 * Each time around this loop, we are looking at a chunk of stack
3579 * that starts with an activation record.
3582 while (p < stack_end) {
3583 info = get_ret_itbl((StgClosure *)p);
3585 switch (info->i.type) {
3588 ((StgUpdateFrame *)p)->updatee
3589 = evacuate(((StgUpdateFrame *)p)->updatee);
3590 p += sizeofW(StgUpdateFrame);
3593 // small bitmap (< 32 entries, or 64 on a 64-bit machine)
3598 bitmap = BITMAP_BITS(info->i.layout.bitmap);
3599 size = BITMAP_SIZE(info->i.layout.bitmap);
3600 // NOTE: the payload starts immediately after the info-ptr, we
3601 // don't have an StgHeader in the same sense as a heap closure.
3603 p = scavenge_small_bitmap(p, size, bitmap);
3606 scavenge_srt((StgClosure **)info->srt, info->i.srt_len);
3614 (StgClosure *)*p = evacuate((StgClosure *)*p);
3617 size = BCO_BITMAP_SIZE(bco);
3618 scavenge_large_bitmap(p, BCO_BITMAP(bco), size);
3623 // large bitmap (> 32 entries, or > 64 on a 64-bit machine)
3629 size = info->i.layout.large_bitmap->size;
3631 scavenge_large_bitmap(p, info->i.layout.large_bitmap, size);
3633 // and don't forget to follow the SRT
3637 // Dynamic bitmap: the mask is stored on the stack, and
3638 // there are a number of non-pointers followed by a number
3639 // of pointers above the bitmapped area. (see StgMacros.h,
3644 dyn = ((StgRetDyn *)p)->liveness;
3646 // traverse the bitmap first
3647 bitmap = GET_LIVENESS(dyn);
3648 p = (P_)&((StgRetDyn *)p)->payload[0];
3649 size = RET_DYN_SIZE;
3650 p = scavenge_small_bitmap(p, size, bitmap);
3652 // skip over the non-ptr words
3653 p += GET_NONPTRS(dyn);
3655 // follow the ptr words
3656 for (size = GET_PTRS(dyn); size > 0; size--) {
3657 (StgClosure *)*p = evacuate((StgClosure *)*p);
3665 StgRetFun *ret_fun = (StgRetFun *)p;
3666 StgFunInfoTable *fun_info;
3668 ret_fun->fun = evacuate(ret_fun->fun);
3669 fun_info = get_fun_itbl(ret_fun->fun);
3670 p = scavenge_arg_block(fun_info, ret_fun->payload);
3675 barf("scavenge_stack: weird activation record found on stack: %d", (int)(info->i.type));
3680 /*-----------------------------------------------------------------------------
3681 scavenge the large object list.
3683 evac_gen set by caller; similar games played with evac_gen as with
3684 scavenge() - see comment at the top of scavenge(). Most large
3685 objects are (repeatedly) mutable, so most of the time evac_gen will
3687 --------------------------------------------------------------------------- */
3690 scavenge_large(step *stp)
3695 bd = stp->new_large_objects;
3697 for (; bd != NULL; bd = stp->new_large_objects) {
3699 /* take this object *off* the large objects list and put it on
3700 * the scavenged large objects list. This is so that we can
3701 * treat new_large_objects as a stack and push new objects on
3702 * the front when evacuating.
3704 stp->new_large_objects = bd->link;
3705 dbl_link_onto(bd, &stp->scavenged_large_objects);
3707 // update the block count in this step.
3708 stp->n_scavenged_large_blocks += bd->blocks;
3711 if (scavenge_one(p)) {
3712 mkMutCons((StgClosure *)p, stp->gen);
3717 /* -----------------------------------------------------------------------------
3718 Initialising the static object & mutable lists
3719 -------------------------------------------------------------------------- */
3722 zero_static_object_list(StgClosure* first_static)
3726 const StgInfoTable *info;
3728 for (p = first_static; p != END_OF_STATIC_LIST; p = link) {
3730 link = STATIC_LINK(info, p);
3731 STATIC_LINK(info,p) = NULL;
3735 /* This function is only needed because we share the mutable link
3736 * field with the static link field in an IND_STATIC, so we have to
3737 * zero the mut_link field before doing a major GC, which needs the
3738 * static link field.
3740 * It doesn't do any harm to zero all the mutable link fields on the
3745 zero_mutable_list( StgMutClosure *first )
3747 StgMutClosure *next, *c;
3749 for (c = first; c != END_MUT_LIST; c = next) {
3755 /* -----------------------------------------------------------------------------
3757 -------------------------------------------------------------------------- */
3764 for (c = (StgIndStatic *)caf_list; c != NULL;
3765 c = (StgIndStatic *)c->static_link)
3767 c->header.info = c->saved_info;
3768 c->saved_info = NULL;
3769 // could, but not necessary: c->static_link = NULL;
3775 markCAFs( evac_fn evac )
3779 for (c = (StgIndStatic *)caf_list; c != NULL;
3780 c = (StgIndStatic *)c->static_link)
3782 evac(&c->indirectee);
3786 /* -----------------------------------------------------------------------------
3787 Sanity code for CAF garbage collection.
3789 With DEBUG turned on, we manage a CAF list in addition to the SRT
3790 mechanism. After GC, we run down the CAF list and blackhole any
3791 CAFs which have been garbage collected. This means we get an error
3792 whenever the program tries to enter a garbage collected CAF.
3794 Any garbage collected CAFs are taken off the CAF list at the same
3796 -------------------------------------------------------------------------- */
3798 #if 0 && defined(DEBUG)
3805 const StgInfoTable *info;
3816 ASSERT(info->type == IND_STATIC);
3818 if (STATIC_LINK(info,p) == NULL) {
3819 IF_DEBUG(gccafs, belch("CAF gc'd at 0x%04lx", (long)p));
3821 SET_INFO(p,&stg_BLACKHOLE_info);
3822 p = STATIC_LINK2(info,p);
3826 pp = &STATIC_LINK2(info,p);
3833 // belch("%d CAFs live", i);
3838 /* -----------------------------------------------------------------------------
3841 Whenever a thread returns to the scheduler after possibly doing
3842 some work, we have to run down the stack and black-hole all the
3843 closures referred to by update frames.
3844 -------------------------------------------------------------------------- */
3847 threadLazyBlackHole(StgTSO *tso)
3850 StgRetInfoTable *info;
3851 StgBlockingQueue *bh;
3854 stack_end = &tso->stack[tso->stack_size];
3856 frame = (StgClosure *)tso->sp;
3859 info = get_ret_itbl(frame);
3861 switch (info->i.type) {
3864 bh = (StgBlockingQueue *)((StgUpdateFrame *)frame)->updatee;
3866 /* if the thunk is already blackholed, it means we've also
3867 * already blackholed the rest of the thunks on this stack,
3868 * so we can stop early.
3870 * The blackhole made for a CAF is a CAF_BLACKHOLE, so they
3871 * don't interfere with this optimisation.
3873 if (bh->header.info == &stg_BLACKHOLE_info) {
3877 if (bh->header.info != &stg_BLACKHOLE_BQ_info &&
3878 bh->header.info != &stg_CAF_BLACKHOLE_info) {
3879 #if (!defined(LAZY_BLACKHOLING)) && defined(DEBUG)
3880 belch("Unexpected lazy BHing required at 0x%04x",(int)bh);
3884 // We pretend that bh is now dead.
3885 LDV_recordDead_FILL_SLOP_DYNAMIC((StgClosure *)bh);
3887 SET_INFO(bh,&stg_BLACKHOLE_info);
3890 // We pretend that bh has just been created.
3891 LDV_recordCreate(bh);
3895 frame = (StgClosure *) ((StgUpdateFrame *)frame + 1);
3901 // normal stack frames; do nothing except advance the pointer
3903 (StgPtr)frame += stack_frame_sizeW(frame);
3909 /* -----------------------------------------------------------------------------
3912 * Code largely pinched from old RTS, then hacked to bits. We also do
3913 * lazy black holing here.
3915 * -------------------------------------------------------------------------- */
3917 struct stack_gap { StgWord gap_size; struct stack_gap *next_gap; };
3920 threadSqueezeStack(StgTSO *tso)
3923 rtsBool prev_was_update_frame;
3924 StgClosure *updatee = NULL;
3926 StgRetInfoTable *info;
3927 StgWord current_gap_size;
3928 struct stack_gap *gap;
3931 // Traverse the stack upwards, replacing adjacent update frames
3932 // with a single update frame and a "stack gap". A stack gap
3933 // contains two values: the size of the gap, and the distance
3934 // to the next gap (or the stack top).
3936 bottom = &(tso->stack[tso->stack_size]);
3940 ASSERT(frame < bottom);
3942 prev_was_update_frame = rtsFalse;
3943 current_gap_size = 0;
3944 gap = (struct stack_gap *) (tso->sp - sizeofW(StgUpdateFrame));
3946 while (frame < bottom) {
3948 info = get_ret_itbl((StgClosure *)frame);
3949 switch (info->i.type) {
3953 StgUpdateFrame *upd = (StgUpdateFrame *)frame;
3955 if (upd->updatee->header.info == &stg_BLACKHOLE_info) {
3957 // found a BLACKHOLE'd update frame; we've been here
3958 // before, in a previous GC, so just break out.
3960 // Mark the end of the gap, if we're in one.
3961 if (current_gap_size != 0) {
3962 gap = (struct stack_gap *)(frame-sizeofW(StgUpdateFrame));
3965 frame += sizeofW(StgUpdateFrame);
3966 goto done_traversing;
3969 if (prev_was_update_frame) {
3971 TICK_UPD_SQUEEZED();
3972 /* wasn't there something about update squeezing and ticky to be
3973 * sorted out? oh yes: we aren't counting each enter properly
3974 * in this case. See the log somewhere. KSW 1999-04-21
3976 * Check two things: that the two update frames don't point to
3977 * the same object, and that the updatee_bypass isn't already an
3978 * indirection. Both of these cases only happen when we're in a
3979 * block hole-style loop (and there are multiple update frames
3980 * on the stack pointing to the same closure), but they can both
3981 * screw us up if we don't check.
3983 if (upd->updatee != updatee && !closure_IND(upd->updatee)) {
3984 // this wakes the threads up
3985 UPD_IND_NOLOCK(upd->updatee, updatee);
3988 // now mark this update frame as a stack gap. The gap
3989 // marker resides in the bottom-most update frame of
3990 // the series of adjacent frames, and covers all the
3991 // frames in this series.
3992 current_gap_size += sizeofW(StgUpdateFrame);
3993 ((struct stack_gap *)frame)->gap_size = current_gap_size;
3994 ((struct stack_gap *)frame)->next_gap = gap;
3996 frame += sizeofW(StgUpdateFrame);
4000 // single update frame, or the topmost update frame in a series
4002 StgBlockingQueue *bh = (StgBlockingQueue *)upd->updatee;
4004 // Do lazy black-holing
4005 if (bh->header.info != &stg_BLACKHOLE_info &&
4006 bh->header.info != &stg_BLACKHOLE_BQ_info &&
4007 bh->header.info != &stg_CAF_BLACKHOLE_info) {
4008 #if (!defined(LAZY_BLACKHOLING)) && defined(DEBUG)
4009 belch("Unexpected lazy BHing required at 0x%04x",(int)bh);
4012 /* zero out the slop so that the sanity checker can tell
4013 * where the next closure is.
4016 StgInfoTable *bh_info = get_itbl(bh);
4017 nat np = bh_info->layout.payload.ptrs,
4018 nw = bh_info->layout.payload.nptrs, i;
4019 /* don't zero out slop for a THUNK_SELECTOR,
4020 * because its layout info is used for a
4021 * different purpose, and it's exactly the
4022 * same size as a BLACKHOLE in any case.
4024 if (bh_info->type != THUNK_SELECTOR) {
4025 for (i = np; i < np + nw; i++) {
4026 ((StgClosure *)bh)->payload[i] = 0;
4032 // We pretend that bh is now dead.
4033 LDV_recordDead_FILL_SLOP_DYNAMIC((StgClosure *)bh);
4035 // Todo: maybe use SET_HDR() and remove LDV_recordCreate()?
4036 SET_INFO(bh,&stg_BLACKHOLE_info);
4038 // We pretend that bh has just been created.
4039 LDV_recordCreate(bh);
4043 prev_was_update_frame = rtsTrue;
4044 updatee = upd->updatee;
4045 frame += sizeofW(StgUpdateFrame);
4051 prev_was_update_frame = rtsFalse;
4053 // we're not in a gap... check whether this is the end of a gap
4054 // (an update frame can't be the end of a gap).
4055 if (current_gap_size != 0) {
4056 gap = (struct stack_gap *) (frame - sizeofW(StgUpdateFrame));
4058 current_gap_size = 0;
4060 frame += stack_frame_sizeW((StgClosure *)frame);
4067 // Now we have a stack with gaps in it, and we have to walk down
4068 // shoving the stack up to fill in the gaps. A diagram might
4072 // | ********* | <- sp
4076 // | stack_gap | <- gap | chunk_size
4078 // | ......... | <- gap_end v
4084 // 'sp' points the the current top-of-stack
4085 // 'gap' points to the stack_gap structure inside the gap
4086 // ***** indicates real stack data
4087 // ..... indicates gap
4088 // <empty> indicates unused
4092 void *gap_start, *next_gap_start, *gap_end;
4095 next_gap_start = (void *)gap + sizeof(StgUpdateFrame);
4096 sp = next_gap_start;
4098 while ((StgPtr)gap > tso->sp) {
4100 // we're working in *bytes* now...
4101 gap_start = next_gap_start;
4102 gap_end = gap_start - gap->gap_size * sizeof(W_);
4104 gap = gap->next_gap;
4105 next_gap_start = (void *)gap + sizeof(StgUpdateFrame);
4107 chunk_size = gap_end - next_gap_start;
4109 memmove(sp, next_gap_start, chunk_size);
4112 tso->sp = (StgPtr)sp;
4116 /* -----------------------------------------------------------------------------
4119 * We have to prepare for GC - this means doing lazy black holing
4120 * here. We also take the opportunity to do stack squeezing if it's
4122 * -------------------------------------------------------------------------- */
4124 threadPaused(StgTSO *tso)
4126 if ( RtsFlags.GcFlags.squeezeUpdFrames == rtsTrue )
4127 threadSqueezeStack(tso); // does black holing too
4129 threadLazyBlackHole(tso);
4132 /* -----------------------------------------------------------------------------
4134 * -------------------------------------------------------------------------- */
4138 printMutOnceList(generation *gen)
4140 StgMutClosure *p, *next;
4142 p = gen->mut_once_list;
4145 fprintf(stderr, "@@ Mut once list %p: ", gen->mut_once_list);
4146 for (; p != END_MUT_LIST; p = next, next = p->mut_link) {
4147 fprintf(stderr, "%p (%s), ",
4148 p, info_type((StgClosure *)p));
4150 fputc('\n', stderr);
4154 printMutableList(generation *gen)
4156 StgMutClosure *p, *next;
4161 fprintf(stderr, "@@ Mutable list %p: ", gen->mut_list);
4162 for (; p != END_MUT_LIST; p = next, next = p->mut_link) {
4163 fprintf(stderr, "%p (%s), ",
4164 p, info_type((StgClosure *)p));
4166 fputc('\n', stderr);
4169 static inline rtsBool
4170 maybeLarge(StgClosure *closure)
4172 StgInfoTable *info = get_itbl(closure);
4174 /* closure types that may be found on the new_large_objects list;
4175 see scavenge_large */
4176 return (info->type == MUT_ARR_PTRS ||
4177 info->type == MUT_ARR_PTRS_FROZEN ||
4178 info->type == TSO ||
4179 info->type == ARR_WORDS);