1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team 1998-2003
5 * Generational garbage collector
7 * ---------------------------------------------------------------------------*/
9 #include "PosixSource.h"
14 #include "OSThreads.h"
16 #include "LdvProfile.h"
21 #include "BlockAlloc.h"
27 #include "ParTicky.h" // ToDo: move into Rts.h
28 #include "GCCompact.h"
29 #include "RtsSignals.h"
31 #if defined(GRAN) || defined(PAR)
32 # include "GranSimRts.h"
33 # include "ParallelRts.h"
37 # include "ParallelDebug.h"
42 #if defined(RTS_GTK_FRONTPANEL)
43 #include "FrontPanel.h"
46 #include "RetainerProfile.h"
50 // Turn off inlining when debugging - it obfuscates things
53 # define STATIC_INLINE static
56 /* STATIC OBJECT LIST.
59 * We maintain a linked list of static objects that are still live.
60 * The requirements for this list are:
62 * - we need to scan the list while adding to it, in order to
63 * scavenge all the static objects (in the same way that
64 * breadth-first scavenging works for dynamic objects).
66 * - we need to be able to tell whether an object is already on
67 * the list, to break loops.
69 * Each static object has a "static link field", which we use for
70 * linking objects on to the list. We use a stack-type list, consing
71 * objects on the front as they are added (this means that the
72 * scavenge phase is depth-first, not breadth-first, but that
75 * A separate list is kept for objects that have been scavenged
76 * already - this is so that we can zero all the marks afterwards.
78 * An object is on the list if its static link field is non-zero; this
79 * means that we have to mark the end of the list with '1', not NULL.
81 * Extra notes for generational GC:
83 * Each generation has a static object list associated with it. When
84 * collecting generations up to N, we treat the static object lists
85 * from generations > N as roots.
87 * We build up a static object list while collecting generations 0..N,
88 * which is then appended to the static object list of generation N+1.
90 static StgClosure* static_objects; // live static objects
91 StgClosure* scavenged_static_objects; // static objects scavenged so far
93 /* N is the oldest generation being collected, where the generations
94 * are numbered starting at 0. A major GC (indicated by the major_gc
95 * flag) is when we're collecting all generations. We only attempt to
96 * deal with static objects and GC CAFs when doing a major GC.
99 static rtsBool major_gc;
101 /* Youngest generation that objects should be evacuated to in
102 * evacuate(). (Logically an argument to evacuate, but it's static
103 * a lot of the time so we optimise it into a global variable).
107 /* Whether to do eager promotion or not.
109 static rtsBool eager_promotion;
113 StgWeak *old_weak_ptr_list; // also pending finaliser list
115 /* Which stage of processing various kinds of weak pointer are we at?
116 * (see traverse_weak_ptr_list() below for discussion).
118 typedef enum { WeakPtrs, WeakThreads, WeakDone } WeakStage;
119 static WeakStage weak_stage;
121 /* List of all threads during GC
123 static StgTSO *old_all_threads;
124 StgTSO *resurrected_threads;
126 /* Flag indicating failure to evacuate an object to the desired
129 static rtsBool failed_to_evac;
131 /* Saved nursery (used for 2-space collector only)
133 static bdescr *saved_nursery;
134 static nat saved_n_blocks;
136 /* Data used for allocation area sizing.
138 static lnat new_blocks; // blocks allocated during this GC
139 static lnat new_scavd_blocks; // ditto, but depth-first blocks
140 static lnat g0s0_pcnt_kept = 30; // percentage of g0s0 live at last minor GC
142 /* Used to avoid long recursion due to selector thunks
144 static lnat thunk_selector_depth = 0;
145 #define MAX_THUNK_SELECTOR_DEPTH 8
155 /* -----------------------------------------------------------------------------
156 Static function declarations
157 -------------------------------------------------------------------------- */
159 static bdescr * gc_alloc_block ( step *stp );
160 static void mark_root ( StgClosure **root );
162 // Use a register argument for evacuate, if available.
164 #define REGPARM1 __attribute__((regparm(1)))
169 REGPARM1 static StgClosure * evacuate (StgClosure *q);
171 static void zero_static_object_list ( StgClosure* first_static );
173 static rtsBool traverse_weak_ptr_list ( void );
174 static void mark_weak_ptr_list ( StgWeak **list );
175 static rtsBool traverse_blackhole_queue ( void );
177 static StgClosure * eval_thunk_selector ( nat field, StgSelector * p );
180 static void scavenge ( step * );
181 static void scavenge_mark_stack ( void );
182 static void scavenge_stack ( StgPtr p, StgPtr stack_end );
183 static rtsBool scavenge_one ( StgPtr p );
184 static void scavenge_large ( step * );
185 static void scavenge_static ( void );
186 static void scavenge_mutable_list ( generation *g );
188 static void scavenge_large_bitmap ( StgPtr p,
189 StgLargeBitmap *large_bitmap,
192 #if 0 && defined(DEBUG)
193 static void gcCAFs ( void );
196 /* -----------------------------------------------------------------------------
197 inline functions etc. for dealing with the mark bitmap & stack.
198 -------------------------------------------------------------------------- */
200 #define MARK_STACK_BLOCKS 4
202 static bdescr *mark_stack_bdescr;
203 static StgPtr *mark_stack;
204 static StgPtr *mark_sp;
205 static StgPtr *mark_splim;
207 // Flag and pointers used for falling back to a linear scan when the
208 // mark stack overflows.
209 static rtsBool mark_stack_overflowed;
210 static bdescr *oldgen_scan_bd;
211 static StgPtr oldgen_scan;
213 STATIC_INLINE rtsBool
214 mark_stack_empty(void)
216 return mark_sp == mark_stack;
219 STATIC_INLINE rtsBool
220 mark_stack_full(void)
222 return mark_sp >= mark_splim;
226 reset_mark_stack(void)
228 mark_sp = mark_stack;
232 push_mark_stack(StgPtr p)
243 /* -----------------------------------------------------------------------------
244 Allocate a new to-space block in the given step.
245 -------------------------------------------------------------------------- */
248 gc_alloc_block(step *stp)
250 bdescr *bd = allocBlock();
251 bd->gen_no = stp->gen_no;
255 // blocks in to-space in generations up to and including N
256 // get the BF_EVACUATED flag.
257 if (stp->gen_no <= N) {
258 bd->flags = BF_EVACUATED;
263 // Start a new to-space block, chain it on after the previous one.
264 if (stp->hp_bd != NULL) {
265 stp->hp_bd->free = stp->hp;
266 stp->hp_bd->link = bd;
271 stp->hpLim = stp->hp + BLOCK_SIZE_W;
280 gc_alloc_scavd_block(step *stp)
282 bdescr *bd = allocBlock();
283 bd->gen_no = stp->gen_no;
286 // blocks in to-space in generations up to and including N
287 // get the BF_EVACUATED flag.
288 if (stp->gen_no <= N) {
289 bd->flags = BF_EVACUATED;
294 bd->link = stp->blocks;
297 if (stp->scavd_hp != NULL) {
298 Bdescr(stp->scavd_hp)->free = stp->scavd_hp;
300 stp->scavd_hp = bd->start;
301 stp->scavd_hpLim = stp->scavd_hp + BLOCK_SIZE_W;
309 /* -----------------------------------------------------------------------------
312 Rough outline of the algorithm: for garbage collecting generation N
313 (and all younger generations):
315 - follow all pointers in the root set. the root set includes all
316 mutable objects in all generations (mutable_list).
318 - for each pointer, evacuate the object it points to into either
320 + to-space of the step given by step->to, which is the next
321 highest step in this generation or the first step in the next
322 generation if this is the last step.
324 + to-space of generations[evac_gen]->steps[0], if evac_gen != 0.
325 When we evacuate an object we attempt to evacuate
326 everything it points to into the same generation - this is
327 achieved by setting evac_gen to the desired generation. If
328 we can't do this, then an entry in the mut list has to
329 be made for the cross-generation pointer.
331 + if the object is already in a generation > N, then leave
334 - repeatedly scavenge to-space from each step in each generation
335 being collected until no more objects can be evacuated.
337 - free from-space in each step, and set from-space = to-space.
339 Locks held: all capabilities are held throughout GarbageCollect().
341 -------------------------------------------------------------------------- */
344 GarbageCollect ( void (*get_roots)(evac_fn), rtsBool force_major_gc )
348 lnat live, allocated, copied = 0, scavd_copied = 0;
349 lnat oldgen_saved_blocks = 0;
355 CostCentreStack *prev_CCS;
358 debugTrace(DEBUG_gc, "starting GC");
360 #if defined(RTS_USER_SIGNALS)
365 // tell the STM to discard any cached closures its hoping to re-use
368 // tell the stats department that we've started a GC
372 // check for memory leaks if DEBUG is on
382 // Init stats and print par specific (timing) info
383 PAR_TICKY_PAR_START();
385 // attribute any costs to CCS_GC
391 /* Approximate how much we allocated.
392 * Todo: only when generating stats?
394 allocated = calcAllocated();
396 /* Figure out which generation to collect
398 if (force_major_gc) {
399 N = RtsFlags.GcFlags.generations - 1;
403 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
404 if (generations[g].steps[0].n_blocks +
405 generations[g].steps[0].n_large_blocks
406 >= generations[g].max_blocks) {
410 major_gc = (N == RtsFlags.GcFlags.generations-1);
413 #ifdef RTS_GTK_FRONTPANEL
414 if (RtsFlags.GcFlags.frontpanel) {
415 updateFrontPanelBeforeGC(N);
419 // check stack sanity *before* GC (ToDo: check all threads)
421 // ToDo!: check sanity IF_DEBUG(sanity, checkTSOsSanity());
423 IF_DEBUG(sanity, checkFreeListSanity());
425 /* Initialise the static object lists
427 static_objects = END_OF_STATIC_LIST;
428 scavenged_static_objects = END_OF_STATIC_LIST;
430 /* Save the nursery if we're doing a two-space collection.
431 * g0s0->blocks will be used for to-space, so we need to get the
432 * nursery out of the way.
434 if (RtsFlags.GcFlags.generations == 1) {
435 saved_nursery = g0s0->blocks;
436 saved_n_blocks = g0s0->n_blocks;
441 /* Keep a count of how many new blocks we allocated during this GC
442 * (used for resizing the allocation area, later).
445 new_scavd_blocks = 0;
447 // Initialise to-space in all the generations/steps that we're
450 for (g = 0; g <= N; g++) {
452 // throw away the mutable list. Invariant: the mutable list
453 // always has at least one block; this means we can avoid a check for
454 // NULL in recordMutable().
456 freeChain(generations[g].mut_list);
457 generations[g].mut_list = allocBlock();
458 for (i = 0; i < n_capabilities; i++) {
459 freeChain(capabilities[i].mut_lists[g]);
460 capabilities[i].mut_lists[g] = allocBlock();
464 for (s = 0; s < generations[g].n_steps; s++) {
466 // generation 0, step 0 doesn't need to-space
467 if (g == 0 && s == 0 && RtsFlags.GcFlags.generations > 1) {
471 stp = &generations[g].steps[s];
472 ASSERT(stp->gen_no == g);
474 // start a new to-space for this step.
475 stp->old_blocks = stp->blocks;
476 stp->n_old_blocks = stp->n_blocks;
478 // allocate the first to-space block; extra blocks will be
479 // chained on as necessary.
481 bd = gc_alloc_block(stp);
484 stp->scan = bd->start;
487 // allocate a block for "already scavenged" objects. This goes
488 // on the front of the stp->blocks list, so it won't be
489 // traversed by the scavenging sweep.
490 gc_alloc_scavd_block(stp);
492 // initialise the large object queues.
493 stp->new_large_objects = NULL;
494 stp->scavenged_large_objects = NULL;
495 stp->n_scavenged_large_blocks = 0;
497 // mark the large objects as not evacuated yet
498 for (bd = stp->large_objects; bd; bd = bd->link) {
499 bd->flags &= ~BF_EVACUATED;
502 // for a compacted step, we need to allocate the bitmap
503 if (stp->is_compacted) {
504 nat bitmap_size; // in bytes
505 bdescr *bitmap_bdescr;
508 bitmap_size = stp->n_old_blocks * BLOCK_SIZE / (sizeof(W_)*BITS_PER_BYTE);
510 if (bitmap_size > 0) {
511 bitmap_bdescr = allocGroup((lnat)BLOCK_ROUND_UP(bitmap_size)
513 stp->bitmap = bitmap_bdescr;
514 bitmap = bitmap_bdescr->start;
516 debugTrace(DEBUG_gc, "bitmap_size: %d, bitmap: %p",
517 bitmap_size, bitmap);
519 // don't forget to fill it with zeros!
520 memset(bitmap, 0, bitmap_size);
522 // For each block in this step, point to its bitmap from the
524 for (bd=stp->old_blocks; bd != NULL; bd = bd->link) {
525 bd->u.bitmap = bitmap;
526 bitmap += BLOCK_SIZE_W / (sizeof(W_)*BITS_PER_BYTE);
528 // Also at this point we set the BF_COMPACTED flag
529 // for this block. The invariant is that
530 // BF_COMPACTED is always unset, except during GC
531 // when it is set on those blocks which will be
533 bd->flags |= BF_COMPACTED;
540 /* make sure the older generations have at least one block to
541 * allocate into (this makes things easier for copy(), see below).
543 for (g = N+1; g < RtsFlags.GcFlags.generations; g++) {
544 for (s = 0; s < generations[g].n_steps; s++) {
545 stp = &generations[g].steps[s];
546 if (stp->hp_bd == NULL) {
547 ASSERT(stp->blocks == NULL);
548 bd = gc_alloc_block(stp);
552 if (stp->scavd_hp == NULL) {
553 gc_alloc_scavd_block(stp);
556 /* Set the scan pointer for older generations: remember we
557 * still have to scavenge objects that have been promoted. */
559 stp->scan_bd = stp->hp_bd;
560 stp->new_large_objects = NULL;
561 stp->scavenged_large_objects = NULL;
562 stp->n_scavenged_large_blocks = 0;
565 /* Move the private mutable lists from each capability onto the
566 * main mutable list for the generation.
568 for (i = 0; i < n_capabilities; i++) {
569 for (bd = capabilities[i].mut_lists[g];
570 bd->link != NULL; bd = bd->link) {
573 bd->link = generations[g].mut_list;
574 generations[g].mut_list = capabilities[i].mut_lists[g];
575 capabilities[i].mut_lists[g] = allocBlock();
579 /* Allocate a mark stack if we're doing a major collection.
582 mark_stack_bdescr = allocGroup(MARK_STACK_BLOCKS);
583 mark_stack = (StgPtr *)mark_stack_bdescr->start;
584 mark_sp = mark_stack;
585 mark_splim = mark_stack + (MARK_STACK_BLOCKS * BLOCK_SIZE_W);
587 mark_stack_bdescr = NULL;
590 eager_promotion = rtsTrue; // for now
592 /* -----------------------------------------------------------------------
593 * follow all the roots that we know about:
594 * - mutable lists from each generation > N
595 * we want to *scavenge* these roots, not evacuate them: they're not
596 * going to move in this GC.
597 * Also: do them in reverse generation order. This is because we
598 * often want to promote objects that are pointed to by older
599 * generations early, so we don't have to repeatedly copy them.
600 * Doing the generations in reverse order ensures that we don't end
601 * up in the situation where we want to evac an object to gen 3 and
602 * it has already been evaced to gen 2.
606 for (g = RtsFlags.GcFlags.generations-1; g > N; g--) {
607 generations[g].saved_mut_list = generations[g].mut_list;
608 generations[g].mut_list = allocBlock();
609 // mut_list always has at least one block.
612 for (g = RtsFlags.GcFlags.generations-1; g > N; g--) {
613 IF_PAR_DEBUG(verbose, printMutableList(&generations[g]));
614 scavenge_mutable_list(&generations[g]);
616 for (st = generations[g].n_steps-1; st >= 0; st--) {
617 scavenge(&generations[g].steps[st]);
622 /* follow roots from the CAF list (used by GHCi)
627 /* follow all the roots that the application knows about.
630 get_roots(mark_root);
633 /* And don't forget to mark the TSO if we got here direct from
635 /* Not needed in a seq version?
637 CurrentTSO = (StgTSO *)MarkRoot((StgClosure *)CurrentTSO);
641 // Mark the entries in the GALA table of the parallel system
642 markLocalGAs(major_gc);
643 // Mark all entries on the list of pending fetches
644 markPendingFetches(major_gc);
647 /* Mark the weak pointer list, and prepare to detect dead weak
650 mark_weak_ptr_list(&weak_ptr_list);
651 old_weak_ptr_list = weak_ptr_list;
652 weak_ptr_list = NULL;
653 weak_stage = WeakPtrs;
655 /* The all_threads list is like the weak_ptr_list.
656 * See traverse_weak_ptr_list() for the details.
658 old_all_threads = all_threads;
659 all_threads = END_TSO_QUEUE;
660 resurrected_threads = END_TSO_QUEUE;
662 /* Mark the stable pointer table.
664 markStablePtrTable(mark_root);
666 /* Mark the root pointer table.
668 markRootPtrTable(mark_root);
670 /* -------------------------------------------------------------------------
671 * Repeatedly scavenge all the areas we know about until there's no
672 * more scavenging to be done.
679 // scavenge static objects
680 if (major_gc && static_objects != END_OF_STATIC_LIST) {
681 IF_DEBUG(sanity, checkStaticObjects(static_objects));
685 /* When scavenging the older generations: Objects may have been
686 * evacuated from generations <= N into older generations, and we
687 * need to scavenge these objects. We're going to try to ensure that
688 * any evacuations that occur move the objects into at least the
689 * same generation as the object being scavenged, otherwise we
690 * have to create new entries on the mutable list for the older
694 // scavenge each step in generations 0..maxgen
700 // scavenge objects in compacted generation
701 if (mark_stack_overflowed || oldgen_scan_bd != NULL ||
702 (mark_stack_bdescr != NULL && !mark_stack_empty())) {
703 scavenge_mark_stack();
707 for (gen = RtsFlags.GcFlags.generations; --gen >= 0; ) {
708 for (st = generations[gen].n_steps; --st >= 0; ) {
709 if (gen == 0 && st == 0 && RtsFlags.GcFlags.generations > 1) {
712 stp = &generations[gen].steps[st];
714 if (stp->hp_bd != stp->scan_bd || stp->scan < stp->hp) {
719 if (stp->new_large_objects != NULL) {
728 // if any blackholes are alive, make the threads that wait on
730 if (traverse_blackhole_queue())
733 if (flag) { goto loop; }
735 // must be last... invariant is that everything is fully
736 // scavenged at this point.
737 if (traverse_weak_ptr_list()) { // returns rtsTrue if evaced something
742 /* Update the pointers from the task list - these are
743 * treated as weak pointers because we want to allow a main thread
744 * to get a BlockedOnDeadMVar exception in the same way as any other
745 * thread. Note that the threads should all have been retained by
746 * GC by virtue of being on the all_threads list, we're just
747 * updating pointers here.
752 for (task = all_tasks; task != NULL; task = task->all_link) {
753 if (!task->stopped && task->tso) {
754 ASSERT(task->tso->bound == task);
755 tso = (StgTSO *) isAlive((StgClosure *)task->tso);
757 barf("task %p: main thread %d has been GC'd",
771 // Reconstruct the Global Address tables used in GUM
772 rebuildGAtables(major_gc);
773 IF_DEBUG(sanity, checkLAGAtable(rtsTrue/*check closures, too*/));
776 // Now see which stable names are still alive.
779 // Tidy the end of the to-space chains
780 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
781 for (s = 0; s < generations[g].n_steps; s++) {
782 stp = &generations[g].steps[s];
783 if (!(g == 0 && s == 0 && RtsFlags.GcFlags.generations > 1)) {
784 ASSERT(Bdescr(stp->hp) == stp->hp_bd);
785 stp->hp_bd->free = stp->hp;
786 Bdescr(stp->scavd_hp)->free = stp->scavd_hp;
792 // We call processHeapClosureForDead() on every closure destroyed during
793 // the current garbage collection, so we invoke LdvCensusForDead().
794 if (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_LDV
795 || RtsFlags.ProfFlags.bioSelector != NULL)
799 // NO MORE EVACUATION AFTER THIS POINT!
800 // Finally: compaction of the oldest generation.
801 if (major_gc && oldest_gen->steps[0].is_compacted) {
802 // save number of blocks for stats
803 oldgen_saved_blocks = oldest_gen->steps[0].n_old_blocks;
807 IF_DEBUG(sanity, checkGlobalTSOList(rtsFalse));
809 /* run through all the generations/steps and tidy up
811 copied = new_blocks * BLOCK_SIZE_W;
812 scavd_copied = new_scavd_blocks * BLOCK_SIZE_W;
813 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
816 generations[g].collections++; // for stats
819 // Count the mutable list as bytes "copied" for the purposes of
820 // stats. Every mutable list is copied during every GC.
822 nat mut_list_size = 0;
823 for (bd = generations[g].mut_list; bd != NULL; bd = bd->link) {
824 mut_list_size += bd->free - bd->start;
826 copied += mut_list_size;
829 "mut_list_size: %ld (%d vars, %d arrays, %d others)",
830 mut_list_size * sizeof(W_),
831 mutlist_MUTVARS, mutlist_MUTARRS, mutlist_OTHERS);
834 for (s = 0; s < generations[g].n_steps; s++) {
836 stp = &generations[g].steps[s];
838 if (!(g == 0 && s == 0 && RtsFlags.GcFlags.generations > 1)) {
839 // stats information: how much we copied
841 copied -= stp->hp_bd->start + BLOCK_SIZE_W -
843 scavd_copied -= (P_)(BLOCK_ROUND_UP(stp->scavd_hp)) - stp->scavd_hp;
847 // for generations we collected...
850 /* free old memory and shift to-space into from-space for all
851 * the collected steps (except the allocation area). These
852 * freed blocks will probaby be quickly recycled.
854 if (!(g == 0 && s == 0)) {
855 if (stp->is_compacted) {
856 // for a compacted step, just shift the new to-space
857 // onto the front of the now-compacted existing blocks.
858 for (bd = stp->blocks; bd != NULL; bd = bd->link) {
859 bd->flags &= ~BF_EVACUATED; // now from-space
861 // tack the new blocks on the end of the existing blocks
862 if (stp->old_blocks != NULL) {
863 for (bd = stp->old_blocks; bd != NULL; bd = next) {
864 // NB. this step might not be compacted next
865 // time, so reset the BF_COMPACTED flags.
866 // They are set before GC if we're going to
867 // compact. (search for BF_COMPACTED above).
868 bd->flags &= ~BF_COMPACTED;
871 bd->link = stp->blocks;
874 stp->blocks = stp->old_blocks;
876 // add the new blocks to the block tally
877 stp->n_blocks += stp->n_old_blocks;
878 ASSERT(countBlocks(stp->blocks) == stp->n_blocks);
880 freeChain(stp->old_blocks);
881 for (bd = stp->blocks; bd != NULL; bd = bd->link) {
882 bd->flags &= ~BF_EVACUATED; // now from-space
885 stp->old_blocks = NULL;
886 stp->n_old_blocks = 0;
889 /* LARGE OBJECTS. The current live large objects are chained on
890 * scavenged_large, having been moved during garbage
891 * collection from large_objects. Any objects left on
892 * large_objects list are therefore dead, so we free them here.
894 for (bd = stp->large_objects; bd != NULL; bd = next) {
900 // update the count of blocks used by large objects
901 for (bd = stp->scavenged_large_objects; bd != NULL; bd = bd->link) {
902 bd->flags &= ~BF_EVACUATED;
904 stp->large_objects = stp->scavenged_large_objects;
905 stp->n_large_blocks = stp->n_scavenged_large_blocks;
908 // for older generations...
910 /* For older generations, we need to append the
911 * scavenged_large_object list (i.e. large objects that have been
912 * promoted during this GC) to the large_object list for that step.
914 for (bd = stp->scavenged_large_objects; bd; bd = next) {
916 bd->flags &= ~BF_EVACUATED;
917 dbl_link_onto(bd, &stp->large_objects);
920 // add the new blocks we promoted during this GC
921 stp->n_large_blocks += stp->n_scavenged_large_blocks;
926 /* Reset the sizes of the older generations when we do a major
929 * CURRENT STRATEGY: make all generations except zero the same size.
930 * We have to stay within the maximum heap size, and leave a certain
931 * percentage of the maximum heap size available to allocate into.
933 if (major_gc && RtsFlags.GcFlags.generations > 1) {
934 nat live, size, min_alloc;
935 nat max = RtsFlags.GcFlags.maxHeapSize;
936 nat gens = RtsFlags.GcFlags.generations;
938 // live in the oldest generations
939 live = oldest_gen->steps[0].n_blocks +
940 oldest_gen->steps[0].n_large_blocks;
942 // default max size for all generations except zero
943 size = stg_max(live * RtsFlags.GcFlags.oldGenFactor,
944 RtsFlags.GcFlags.minOldGenSize);
946 // minimum size for generation zero
947 min_alloc = stg_max((RtsFlags.GcFlags.pcFreeHeap * max) / 200,
948 RtsFlags.GcFlags.minAllocAreaSize);
950 // Auto-enable compaction when the residency reaches a
951 // certain percentage of the maximum heap size (default: 30%).
952 if (RtsFlags.GcFlags.generations > 1 &&
953 (RtsFlags.GcFlags.compact ||
955 oldest_gen->steps[0].n_blocks >
956 (RtsFlags.GcFlags.compactThreshold * max) / 100))) {
957 oldest_gen->steps[0].is_compacted = 1;
958 // debugBelch("compaction: on\n", live);
960 oldest_gen->steps[0].is_compacted = 0;
961 // debugBelch("compaction: off\n", live);
964 // if we're going to go over the maximum heap size, reduce the
965 // size of the generations accordingly. The calculation is
966 // different if compaction is turned on, because we don't need
967 // to double the space required to collect the old generation.
970 // this test is necessary to ensure that the calculations
971 // below don't have any negative results - we're working
972 // with unsigned values here.
973 if (max < min_alloc) {
977 if (oldest_gen->steps[0].is_compacted) {
978 if ( (size + (size - 1) * (gens - 2) * 2) + min_alloc > max ) {
979 size = (max - min_alloc) / ((gens - 1) * 2 - 1);
982 if ( (size * (gens - 1) * 2) + min_alloc > max ) {
983 size = (max - min_alloc) / ((gens - 1) * 2);
993 debugBelch("live: %d, min_alloc: %d, size : %d, max = %d\n", live,
994 min_alloc, size, max);
997 for (g = 0; g < gens; g++) {
998 generations[g].max_blocks = size;
1002 // Guess the amount of live data for stats.
1005 /* Free the small objects allocated via allocate(), since this will
1006 * all have been copied into G0S1 now.
1008 if (small_alloc_list != NULL) {
1009 freeChain(small_alloc_list);
1011 small_alloc_list = NULL;
1015 alloc_blocks_lim = RtsFlags.GcFlags.minAllocAreaSize;
1017 // Start a new pinned_object_block
1018 pinned_object_block = NULL;
1020 /* Free the mark stack.
1022 if (mark_stack_bdescr != NULL) {
1023 freeGroup(mark_stack_bdescr);
1026 /* Free any bitmaps.
1028 for (g = 0; g <= N; g++) {
1029 for (s = 0; s < generations[g].n_steps; s++) {
1030 stp = &generations[g].steps[s];
1031 if (stp->bitmap != NULL) {
1032 freeGroup(stp->bitmap);
1038 /* Two-space collector:
1039 * Free the old to-space, and estimate the amount of live data.
1041 if (RtsFlags.GcFlags.generations == 1) {
1044 if (g0s0->old_blocks != NULL) {
1045 freeChain(g0s0->old_blocks);
1047 for (bd = g0s0->blocks; bd != NULL; bd = bd->link) {
1048 bd->flags = 0; // now from-space
1050 g0s0->old_blocks = g0s0->blocks;
1051 g0s0->n_old_blocks = g0s0->n_blocks;
1052 g0s0->blocks = saved_nursery;
1053 g0s0->n_blocks = saved_n_blocks;
1055 /* For a two-space collector, we need to resize the nursery. */
1057 /* set up a new nursery. Allocate a nursery size based on a
1058 * function of the amount of live data (by default a factor of 2)
1059 * Use the blocks from the old nursery if possible, freeing up any
1062 * If we get near the maximum heap size, then adjust our nursery
1063 * size accordingly. If the nursery is the same size as the live
1064 * data (L), then we need 3L bytes. We can reduce the size of the
1065 * nursery to bring the required memory down near 2L bytes.
1067 * A normal 2-space collector would need 4L bytes to give the same
1068 * performance we get from 3L bytes, reducing to the same
1069 * performance at 2L bytes.
1071 blocks = g0s0->n_old_blocks;
1073 if ( RtsFlags.GcFlags.maxHeapSize != 0 &&
1074 blocks * RtsFlags.GcFlags.oldGenFactor * 2 >
1075 RtsFlags.GcFlags.maxHeapSize ) {
1076 long adjusted_blocks; // signed on purpose
1079 adjusted_blocks = (RtsFlags.GcFlags.maxHeapSize - 2 * blocks);
1081 debugTrace(DEBUG_gc, "near maximum heap size of 0x%x blocks, blocks = %d, adjusted to %ld",
1082 RtsFlags.GcFlags.maxHeapSize, blocks, adjusted_blocks);
1084 pc_free = adjusted_blocks * 100 / RtsFlags.GcFlags.maxHeapSize;
1085 if (pc_free < RtsFlags.GcFlags.pcFreeHeap) /* might even be < 0 */ {
1088 blocks = adjusted_blocks;
1091 blocks *= RtsFlags.GcFlags.oldGenFactor;
1092 if (blocks < RtsFlags.GcFlags.minAllocAreaSize) {
1093 blocks = RtsFlags.GcFlags.minAllocAreaSize;
1096 resizeNurseries(blocks);
1099 /* Generational collector:
1100 * If the user has given us a suggested heap size, adjust our
1101 * allocation area to make best use of the memory available.
1104 if (RtsFlags.GcFlags.heapSizeSuggestion) {
1106 nat needed = calcNeeded(); // approx blocks needed at next GC
1108 /* Guess how much will be live in generation 0 step 0 next time.
1109 * A good approximation is obtained by finding the
1110 * percentage of g0s0 that was live at the last minor GC.
1113 g0s0_pcnt_kept = (new_blocks * 100) / countNurseryBlocks();
1116 /* Estimate a size for the allocation area based on the
1117 * information available. We might end up going slightly under
1118 * or over the suggested heap size, but we should be pretty
1121 * Formula: suggested - needed
1122 * ----------------------------
1123 * 1 + g0s0_pcnt_kept/100
1125 * where 'needed' is the amount of memory needed at the next
1126 * collection for collecting all steps except g0s0.
1129 (((long)RtsFlags.GcFlags.heapSizeSuggestion - (long)needed) * 100) /
1130 (100 + (long)g0s0_pcnt_kept);
1132 if (blocks < (long)RtsFlags.GcFlags.minAllocAreaSize) {
1133 blocks = RtsFlags.GcFlags.minAllocAreaSize;
1136 resizeNurseries((nat)blocks);
1139 // we might have added extra large blocks to the nursery, so
1140 // resize back to minAllocAreaSize again.
1141 resizeNurseriesFixed(RtsFlags.GcFlags.minAllocAreaSize);
1145 // mark the garbage collected CAFs as dead
1146 #if 0 && defined(DEBUG) // doesn't work at the moment
1147 if (major_gc) { gcCAFs(); }
1151 // resetStaticObjectForRetainerProfiling() must be called before
1153 resetStaticObjectForRetainerProfiling();
1156 // zero the scavenged static object list
1158 zero_static_object_list(scavenged_static_objects);
1161 // Reset the nursery
1164 // start any pending finalizers
1166 scheduleFinalizers(last_free_capability, old_weak_ptr_list);
1169 // send exceptions to any threads which were about to die
1171 resurrectThreads(resurrected_threads);
1174 // Update the stable pointer hash table.
1175 updateStablePtrTable(major_gc);
1177 // check sanity after GC
1178 IF_DEBUG(sanity, checkSanity());
1180 // extra GC trace info
1181 IF_DEBUG(gc, statDescribeGens());
1184 // symbol-table based profiling
1185 /* heapCensus(to_blocks); */ /* ToDo */
1188 // restore enclosing cost centre
1194 // check for memory leaks if DEBUG is on
1198 #ifdef RTS_GTK_FRONTPANEL
1199 if (RtsFlags.GcFlags.frontpanel) {
1200 updateFrontPanelAfterGC( N, live );
1204 // ok, GC over: tell the stats department what happened.
1205 stat_endGC(allocated, live, copied, scavd_copied, N);
1207 #if defined(RTS_USER_SIGNALS)
1208 // unblock signals again
1209 unblockUserSignals();
1218 /* -----------------------------------------------------------------------------
1221 traverse_weak_ptr_list is called possibly many times during garbage
1222 collection. It returns a flag indicating whether it did any work
1223 (i.e. called evacuate on any live pointers).
1225 Invariant: traverse_weak_ptr_list is called when the heap is in an
1226 idempotent state. That means that there are no pending
1227 evacuate/scavenge operations. This invariant helps the weak
1228 pointer code decide which weak pointers are dead - if there are no
1229 new live weak pointers, then all the currently unreachable ones are
1232 For generational GC: we just don't try to finalize weak pointers in
1233 older generations than the one we're collecting. This could
1234 probably be optimised by keeping per-generation lists of weak
1235 pointers, but for a few weak pointers this scheme will work.
1237 There are three distinct stages to processing weak pointers:
1239 - weak_stage == WeakPtrs
1241 We process all the weak pointers whos keys are alive (evacuate
1242 their values and finalizers), and repeat until we can find no new
1243 live keys. If no live keys are found in this pass, then we
1244 evacuate the finalizers of all the dead weak pointers in order to
1247 - weak_stage == WeakThreads
1249 Now, we discover which *threads* are still alive. Pointers to
1250 threads from the all_threads and main thread lists are the
1251 weakest of all: a pointers from the finalizer of a dead weak
1252 pointer can keep a thread alive. Any threads found to be unreachable
1253 are evacuated and placed on the resurrected_threads list so we
1254 can send them a signal later.
1256 - weak_stage == WeakDone
1258 No more evacuation is done.
1260 -------------------------------------------------------------------------- */
1263 traverse_weak_ptr_list(void)
1265 StgWeak *w, **last_w, *next_w;
1267 rtsBool flag = rtsFalse;
1269 switch (weak_stage) {
1275 /* doesn't matter where we evacuate values/finalizers to, since
1276 * these pointers are treated as roots (iff the keys are alive).
1280 last_w = &old_weak_ptr_list;
1281 for (w = old_weak_ptr_list; w != NULL; w = next_w) {
1283 /* There might be a DEAD_WEAK on the list if finalizeWeak# was
1284 * called on a live weak pointer object. Just remove it.
1286 if (w->header.info == &stg_DEAD_WEAK_info) {
1287 next_w = ((StgDeadWeak *)w)->link;
1292 switch (get_itbl(w)->type) {
1295 next_w = (StgWeak *)((StgEvacuated *)w)->evacuee;
1300 /* Now, check whether the key is reachable.
1302 new = isAlive(w->key);
1305 // evacuate the value and finalizer
1306 w->value = evacuate(w->value);
1307 w->finalizer = evacuate(w->finalizer);
1308 // remove this weak ptr from the old_weak_ptr list
1310 // and put it on the new weak ptr list
1312 w->link = weak_ptr_list;
1316 debugTrace(DEBUG_weak,
1317 "weak pointer still alive at %p -> %p",
1322 last_w = &(w->link);
1328 barf("traverse_weak_ptr_list: not WEAK");
1332 /* If we didn't make any changes, then we can go round and kill all
1333 * the dead weak pointers. The old_weak_ptr list is used as a list
1334 * of pending finalizers later on.
1336 if (flag == rtsFalse) {
1337 for (w = old_weak_ptr_list; w; w = w->link) {
1338 w->finalizer = evacuate(w->finalizer);
1341 // Next, move to the WeakThreads stage after fully
1342 // scavenging the finalizers we've just evacuated.
1343 weak_stage = WeakThreads;
1349 /* Now deal with the all_threads list, which behaves somewhat like
1350 * the weak ptr list. If we discover any threads that are about to
1351 * become garbage, we wake them up and administer an exception.
1354 StgTSO *t, *tmp, *next, **prev;
1356 prev = &old_all_threads;
1357 for (t = old_all_threads; t != END_TSO_QUEUE; t = next) {
1359 tmp = (StgTSO *)isAlive((StgClosure *)t);
1365 ASSERT(get_itbl(t)->type == TSO);
1366 switch (t->what_next) {
1367 case ThreadRelocated:
1372 case ThreadComplete:
1373 // finshed or died. The thread might still be alive, but we
1374 // don't keep it on the all_threads list. Don't forget to
1375 // stub out its global_link field.
1376 next = t->global_link;
1377 t->global_link = END_TSO_QUEUE;
1385 // not alive (yet): leave this thread on the
1386 // old_all_threads list.
1387 prev = &(t->global_link);
1388 next = t->global_link;
1391 // alive: move this thread onto the all_threads list.
1392 next = t->global_link;
1393 t->global_link = all_threads;
1400 /* If we evacuated any threads, we need to go back to the scavenger.
1402 if (flag) return rtsTrue;
1404 /* And resurrect any threads which were about to become garbage.
1407 StgTSO *t, *tmp, *next;
1408 for (t = old_all_threads; t != END_TSO_QUEUE; t = next) {
1409 next = t->global_link;
1410 tmp = (StgTSO *)evacuate((StgClosure *)t);
1411 tmp->global_link = resurrected_threads;
1412 resurrected_threads = tmp;
1416 /* Finally, we can update the blackhole_queue. This queue
1417 * simply strings together TSOs blocked on black holes, it is
1418 * not intended to keep anything alive. Hence, we do not follow
1419 * pointers on the blackhole_queue until now, when we have
1420 * determined which TSOs are otherwise reachable. We know at
1421 * this point that all TSOs have been evacuated, however.
1425 for (pt = &blackhole_queue; *pt != END_TSO_QUEUE; pt = &((*pt)->link)) {
1426 *pt = (StgTSO *)isAlive((StgClosure *)*pt);
1427 ASSERT(*pt != NULL);
1431 weak_stage = WeakDone; // *now* we're done,
1432 return rtsTrue; // but one more round of scavenging, please
1435 barf("traverse_weak_ptr_list");
1441 /* -----------------------------------------------------------------------------
1444 Threads on this list behave like weak pointers during the normal
1445 phase of garbage collection: if the blackhole is reachable, then
1446 the thread is reachable too.
1447 -------------------------------------------------------------------------- */
1449 traverse_blackhole_queue (void)
1451 StgTSO *prev, *t, *tmp;
1457 for (t = blackhole_queue; t != END_TSO_QUEUE; prev=t, t = t->link) {
1458 if (! (tmp = (StgTSO *)isAlive((StgClosure*)t))) {
1459 if (isAlive(t->block_info.closure)) {
1460 t = (StgTSO *)evacuate((StgClosure *)t);
1461 if (prev) prev->link = t;
1469 /* -----------------------------------------------------------------------------
1470 After GC, the live weak pointer list may have forwarding pointers
1471 on it, because a weak pointer object was evacuated after being
1472 moved to the live weak pointer list. We remove those forwarding
1475 Also, we don't consider weak pointer objects to be reachable, but
1476 we must nevertheless consider them to be "live" and retain them.
1477 Therefore any weak pointer objects which haven't as yet been
1478 evacuated need to be evacuated now.
1479 -------------------------------------------------------------------------- */
1483 mark_weak_ptr_list ( StgWeak **list )
1485 StgWeak *w, **last_w;
1488 for (w = *list; w; w = w->link) {
1489 // w might be WEAK, EVACUATED, or DEAD_WEAK (actually CON_STATIC) here
1490 ASSERT(w->header.info == &stg_DEAD_WEAK_info
1491 || get_itbl(w)->type == WEAK || get_itbl(w)->type == EVACUATED);
1492 w = (StgWeak *)evacuate((StgClosure *)w);
1494 last_w = &(w->link);
1498 /* -----------------------------------------------------------------------------
1499 isAlive determines whether the given closure is still alive (after
1500 a garbage collection) or not. It returns the new address of the
1501 closure if it is alive, or NULL otherwise.
1503 NOTE: Use it before compaction only!
1504 -------------------------------------------------------------------------- */
1508 isAlive(StgClosure *p)
1510 const StgInfoTable *info;
1515 ASSERT(LOOKS_LIKE_CLOSURE_PTR(p));
1518 // ignore static closures
1520 // ToDo: for static closures, check the static link field.
1521 // Problem here is that we sometimes don't set the link field, eg.
1522 // for static closures with an empty SRT or CONSTR_STATIC_NOCAFs.
1524 if (!HEAP_ALLOCED(p)) {
1528 // ignore closures in generations that we're not collecting.
1530 if (bd->gen_no > N) {
1534 // if it's a pointer into to-space, then we're done
1535 if (bd->flags & BF_EVACUATED) {
1539 // large objects use the evacuated flag
1540 if (bd->flags & BF_LARGE) {
1544 // check the mark bit for compacted steps
1545 if ((bd->flags & BF_COMPACTED) && is_marked((P_)p,bd)) {
1549 switch (info->type) {
1554 case IND_OLDGEN: // rely on compatible layout with StgInd
1555 case IND_OLDGEN_PERM:
1556 // follow indirections
1557 p = ((StgInd *)p)->indirectee;
1562 return ((StgEvacuated *)p)->evacuee;
1565 if (((StgTSO *)p)->what_next == ThreadRelocated) {
1566 p = (StgClosure *)((StgTSO *)p)->link;
1579 mark_root(StgClosure **root)
1581 *root = evacuate(*root);
1585 upd_evacuee(StgClosure *p, StgClosure *dest)
1587 // not true: (ToDo: perhaps it should be)
1588 // ASSERT(Bdescr((P_)dest)->flags & BF_EVACUATED);
1589 SET_INFO(p, &stg_EVACUATED_info);
1590 ((StgEvacuated *)p)->evacuee = dest;
1594 STATIC_INLINE StgClosure *
1595 copy(StgClosure *src, nat size, step *stp)
1601 nat size_org = size;
1604 TICK_GC_WORDS_COPIED(size);
1605 /* Find out where we're going, using the handy "to" pointer in
1606 * the step of the source object. If it turns out we need to
1607 * evacuate to an older generation, adjust it here (see comment
1610 if (stp->gen_no < evac_gen) {
1611 if (eager_promotion) {
1612 stp = &generations[evac_gen].steps[0];
1614 failed_to_evac = rtsTrue;
1618 /* chain a new block onto the to-space for the destination step if
1621 if (stp->hp + size >= stp->hpLim) {
1622 gc_alloc_block(stp);
1627 stp->hp = to + size;
1628 for (i = 0; i < size; i++) { // unroll for small i
1631 upd_evacuee((StgClosure *)from,(StgClosure *)to);
1634 // We store the size of the just evacuated object in the LDV word so that
1635 // the profiler can guess the position of the next object later.
1636 SET_EVACUAEE_FOR_LDV(from, size_org);
1638 return (StgClosure *)to;
1641 // Same as copy() above, except the object will be allocated in memory
1642 // that will not be scavenged. Used for object that have no pointer
1644 STATIC_INLINE StgClosure *
1645 copy_noscav(StgClosure *src, nat size, step *stp)
1651 nat size_org = size;
1654 TICK_GC_WORDS_COPIED(size);
1655 /* Find out where we're going, using the handy "to" pointer in
1656 * the step of the source object. If it turns out we need to
1657 * evacuate to an older generation, adjust it here (see comment
1660 if (stp->gen_no < evac_gen) {
1661 if (eager_promotion) {
1662 stp = &generations[evac_gen].steps[0];
1664 failed_to_evac = rtsTrue;
1668 /* chain a new block onto the to-space for the destination step if
1671 if (stp->scavd_hp + size >= stp->scavd_hpLim) {
1672 gc_alloc_scavd_block(stp);
1677 stp->scavd_hp = to + size;
1678 for (i = 0; i < size; i++) { // unroll for small i
1681 upd_evacuee((StgClosure *)from,(StgClosure *)to);
1684 // We store the size of the just evacuated object in the LDV word so that
1685 // the profiler can guess the position of the next object later.
1686 SET_EVACUAEE_FOR_LDV(from, size_org);
1688 return (StgClosure *)to;
1691 /* Special version of copy() for when we only want to copy the info
1692 * pointer of an object, but reserve some padding after it. This is
1693 * used to optimise evacuation of BLACKHOLEs.
1698 copyPart(StgClosure *src, nat size_to_reserve, nat size_to_copy, step *stp)
1703 nat size_to_copy_org = size_to_copy;
1706 TICK_GC_WORDS_COPIED(size_to_copy);
1707 if (stp->gen_no < evac_gen) {
1708 if (eager_promotion) {
1709 stp = &generations[evac_gen].steps[0];
1711 failed_to_evac = rtsTrue;
1715 if (stp->hp + size_to_reserve >= stp->hpLim) {
1716 gc_alloc_block(stp);
1719 for(to = stp->hp, from = (P_)src; size_to_copy>0; --size_to_copy) {
1724 stp->hp += size_to_reserve;
1725 upd_evacuee(src,(StgClosure *)dest);
1727 // We store the size of the just evacuated object in the LDV word so that
1728 // the profiler can guess the position of the next object later.
1729 // size_to_copy_org is wrong because the closure already occupies size_to_reserve
1731 SET_EVACUAEE_FOR_LDV(src, size_to_reserve);
1733 if (size_to_reserve - size_to_copy_org > 0)
1734 LDV_FILL_SLOP(stp->hp - 1, (int)(size_to_reserve - size_to_copy_org));
1736 return (StgClosure *)dest;
1740 /* -----------------------------------------------------------------------------
1741 Evacuate a large object
1743 This just consists of removing the object from the (doubly-linked)
1744 step->large_objects list, and linking it on to the (singly-linked)
1745 step->new_large_objects list, from where it will be scavenged later.
1747 Convention: bd->flags has BF_EVACUATED set for a large object
1748 that has been evacuated, or unset otherwise.
1749 -------------------------------------------------------------------------- */
1753 evacuate_large(StgPtr p)
1755 bdescr *bd = Bdescr(p);
1758 // object must be at the beginning of the block (or be a ByteArray)
1759 ASSERT(get_itbl((StgClosure *)p)->type == ARR_WORDS ||
1760 (((W_)p & BLOCK_MASK) == 0));
1762 // already evacuated?
1763 if (bd->flags & BF_EVACUATED) {
1764 /* Don't forget to set the failed_to_evac flag if we didn't get
1765 * the desired destination (see comments in evacuate()).
1767 if (bd->gen_no < evac_gen) {
1768 failed_to_evac = rtsTrue;
1769 TICK_GC_FAILED_PROMOTION();
1775 // remove from large_object list
1777 bd->u.back->link = bd->link;
1778 } else { // first object in the list
1779 stp->large_objects = bd->link;
1782 bd->link->u.back = bd->u.back;
1785 /* link it on to the evacuated large object list of the destination step
1788 if (stp->gen_no < evac_gen) {
1789 if (eager_promotion) {
1790 stp = &generations[evac_gen].steps[0];
1792 failed_to_evac = rtsTrue;
1797 bd->gen_no = stp->gen_no;
1798 bd->link = stp->new_large_objects;
1799 stp->new_large_objects = bd;
1800 bd->flags |= BF_EVACUATED;
1803 /* -----------------------------------------------------------------------------
1806 This is called (eventually) for every live object in the system.
1808 The caller to evacuate specifies a desired generation in the
1809 evac_gen global variable. The following conditions apply to
1810 evacuating an object which resides in generation M when we're
1811 collecting up to generation N
1815 else evac to step->to
1817 if M < evac_gen evac to evac_gen, step 0
1819 if the object is already evacuated, then we check which generation
1822 if M >= evac_gen do nothing
1823 if M < evac_gen set failed_to_evac flag to indicate that we
1824 didn't manage to evacuate this object into evac_gen.
1829 evacuate() is the single most important function performance-wise
1830 in the GC. Various things have been tried to speed it up, but as
1831 far as I can tell the code generated by gcc 3.2 with -O2 is about
1832 as good as it's going to get. We pass the argument to evacuate()
1833 in a register using the 'regparm' attribute (see the prototype for
1834 evacuate() near the top of this file).
1836 Changing evacuate() to take an (StgClosure **) rather than
1837 returning the new pointer seems attractive, because we can avoid
1838 writing back the pointer when it hasn't changed (eg. for a static
1839 object, or an object in a generation > N). However, I tried it and
1840 it doesn't help. One reason is that the (StgClosure **) pointer
1841 gets spilled to the stack inside evacuate(), resulting in far more
1842 extra reads/writes than we save.
1843 -------------------------------------------------------------------------- */
1845 REGPARM1 static StgClosure *
1846 evacuate(StgClosure *q)
1853 const StgInfoTable *info;
1856 ASSERT(LOOKS_LIKE_CLOSURE_PTR(q));
1858 if (!HEAP_ALLOCED(q)) {
1860 if (!major_gc) return q;
1863 switch (info->type) {
1866 if (info->srt_bitmap != 0 &&
1867 *THUNK_STATIC_LINK((StgClosure *)q) == NULL) {
1868 *THUNK_STATIC_LINK((StgClosure *)q) = static_objects;
1869 static_objects = (StgClosure *)q;
1874 if (info->srt_bitmap != 0 &&
1875 *FUN_STATIC_LINK((StgClosure *)q) == NULL) {
1876 *FUN_STATIC_LINK((StgClosure *)q) = static_objects;
1877 static_objects = (StgClosure *)q;
1882 /* If q->saved_info != NULL, then it's a revertible CAF - it'll be
1883 * on the CAF list, so don't do anything with it here (we'll
1884 * scavenge it later).
1886 if (((StgIndStatic *)q)->saved_info == NULL
1887 && *IND_STATIC_LINK((StgClosure *)q) == NULL) {
1888 *IND_STATIC_LINK((StgClosure *)q) = static_objects;
1889 static_objects = (StgClosure *)q;
1894 if (*STATIC_LINK(info,(StgClosure *)q) == NULL) {
1895 *STATIC_LINK(info,(StgClosure *)q) = static_objects;
1896 static_objects = (StgClosure *)q;
1900 case CONSTR_INTLIKE:
1901 case CONSTR_CHARLIKE:
1902 case CONSTR_NOCAF_STATIC:
1903 /* no need to put these on the static linked list, they don't need
1909 barf("evacuate(static): strange closure type %d", (int)(info->type));
1915 if (bd->gen_no > N) {
1916 /* Can't evacuate this object, because it's in a generation
1917 * older than the ones we're collecting. Let's hope that it's
1918 * in evac_gen or older, or we will have to arrange to track
1919 * this pointer using the mutable list.
1921 if (bd->gen_no < evac_gen) {
1923 failed_to_evac = rtsTrue;
1924 TICK_GC_FAILED_PROMOTION();
1929 if ((bd->flags & (BF_LARGE | BF_COMPACTED | BF_EVACUATED)) != 0) {
1931 /* pointer into to-space: just return it. This normally
1932 * shouldn't happen, but alllowing it makes certain things
1933 * slightly easier (eg. the mutable list can contain the same
1934 * object twice, for example).
1936 if (bd->flags & BF_EVACUATED) {
1937 if (bd->gen_no < evac_gen) {
1938 failed_to_evac = rtsTrue;
1939 TICK_GC_FAILED_PROMOTION();
1944 /* evacuate large objects by re-linking them onto a different list.
1946 if (bd->flags & BF_LARGE) {
1948 if (info->type == TSO &&
1949 ((StgTSO *)q)->what_next == ThreadRelocated) {
1950 q = (StgClosure *)((StgTSO *)q)->link;
1953 evacuate_large((P_)q);
1957 /* If the object is in a step that we're compacting, then we
1958 * need to use an alternative evacuate procedure.
1960 if (bd->flags & BF_COMPACTED) {
1961 if (!is_marked((P_)q,bd)) {
1963 if (mark_stack_full()) {
1964 mark_stack_overflowed = rtsTrue;
1967 push_mark_stack((P_)q);
1977 switch (info->type) {
1982 return copy(q,sizeW_fromITBL(info),stp);
1986 StgWord w = (StgWord)q->payload[0];
1987 if (q->header.info == Czh_con_info &&
1988 // unsigned, so always true: (StgChar)w >= MIN_CHARLIKE &&
1989 (StgChar)w <= MAX_CHARLIKE) {
1990 return (StgClosure *)CHARLIKE_CLOSURE((StgChar)w);
1992 if (q->header.info == Izh_con_info &&
1993 (StgInt)w >= MIN_INTLIKE && (StgInt)w <= MAX_INTLIKE) {
1994 return (StgClosure *)INTLIKE_CLOSURE((StgInt)w);
1997 return copy_noscav(q,sizeofW(StgHeader)+1,stp);
2003 return copy(q,sizeofW(StgHeader)+1,stp);
2007 return copy(q,sizeofW(StgThunk)+1,stp);
2012 #ifdef NO_PROMOTE_THUNKS
2013 if (bd->gen_no == 0 &&
2014 bd->step->no != 0 &&
2015 bd->step->no == generations[bd->gen_no].n_steps-1) {
2019 return copy(q,sizeofW(StgThunk)+2,stp);
2026 return copy(q,sizeofW(StgHeader)+2,stp);
2029 return copy_noscav(q,sizeofW(StgHeader)+2,stp);
2032 return copy(q,thunk_sizeW_fromITBL(info),stp);
2037 case IND_OLDGEN_PERM:
2040 return copy(q,sizeW_fromITBL(info),stp);
2043 return copy(q,bco_sizeW((StgBCO *)q),stp);
2046 case SE_CAF_BLACKHOLE:
2049 return copyPart(q,BLACKHOLE_sizeW(),sizeofW(StgHeader),stp);
2051 case THUNK_SELECTOR:
2054 const StgInfoTable *info_ptr;
2056 if (thunk_selector_depth > MAX_THUNK_SELECTOR_DEPTH) {
2057 return copy(q,THUNK_SELECTOR_sizeW(),stp);
2060 // stashed away for LDV profiling, see below
2061 info_ptr = q->header.info;
2063 p = eval_thunk_selector(info->layout.selector_offset,
2067 return copy(q,THUNK_SELECTOR_sizeW(),stp);
2070 // q is still BLACKHOLE'd.
2071 thunk_selector_depth++;
2073 thunk_selector_depth--;
2076 // For the purposes of LDV profiling, we have destroyed
2077 // the original selector thunk.
2078 SET_INFO(q, info_ptr);
2079 LDV_RECORD_DEAD_FILL_SLOP_DYNAMIC(q);
2082 // Update the THUNK_SELECTOR with an indirection to the
2083 // EVACUATED closure now at p. Why do this rather than
2084 // upd_evacuee(q,p)? Because we have an invariant that an
2085 // EVACUATED closure always points to an object in the
2086 // same or an older generation (required by the short-cut
2087 // test in the EVACUATED case, below).
2088 SET_INFO(q, &stg_IND_info);
2089 ((StgInd *)q)->indirectee = p;
2091 // For the purposes of LDV profiling, we have created an
2093 LDV_RECORD_CREATE(q);
2101 // follow chains of indirections, don't evacuate them
2102 q = ((StgInd*)q)->indirectee;
2114 case CATCH_STM_FRAME:
2115 case CATCH_RETRY_FRAME:
2116 case ATOMICALLY_FRAME:
2117 // shouldn't see these
2118 barf("evacuate: stack frame at %p\n", q);
2121 return copy(q,pap_sizeW((StgPAP*)q),stp);
2124 return copy(q,ap_sizeW((StgAP*)q),stp);
2127 return copy(q,ap_stack_sizeW((StgAP_STACK*)q),stp);
2130 /* Already evacuated, just return the forwarding address.
2131 * HOWEVER: if the requested destination generation (evac_gen) is
2132 * older than the actual generation (because the object was
2133 * already evacuated to a younger generation) then we have to
2134 * set the failed_to_evac flag to indicate that we couldn't
2135 * manage to promote the object to the desired generation.
2138 * Optimisation: the check is fairly expensive, but we can often
2139 * shortcut it if either the required generation is 0, or the
2140 * current object (the EVACUATED) is in a high enough generation.
2141 * We know that an EVACUATED always points to an object in the
2142 * same or an older generation. stp is the lowest step that the
2143 * current object would be evacuated to, so we only do the full
2144 * check if stp is too low.
2146 if (evac_gen > 0 && stp->gen_no < evac_gen) { // optimisation
2147 StgClosure *p = ((StgEvacuated*)q)->evacuee;
2148 if (HEAP_ALLOCED(p) && Bdescr((P_)p)->gen_no < evac_gen) {
2149 failed_to_evac = rtsTrue;
2150 TICK_GC_FAILED_PROMOTION();
2153 return ((StgEvacuated*)q)->evacuee;
2156 // just copy the block
2157 return copy_noscav(q,arr_words_sizeW((StgArrWords *)q),stp);
2159 case MUT_ARR_PTRS_CLEAN:
2160 case MUT_ARR_PTRS_DIRTY:
2161 case MUT_ARR_PTRS_FROZEN:
2162 case MUT_ARR_PTRS_FROZEN0:
2163 // just copy the block
2164 return copy(q,mut_arr_ptrs_sizeW((StgMutArrPtrs *)q),stp);
2168 StgTSO *tso = (StgTSO *)q;
2170 /* Deal with redirected TSOs (a TSO that's had its stack enlarged).
2172 if (tso->what_next == ThreadRelocated) {
2173 q = (StgClosure *)tso->link;
2177 /* To evacuate a small TSO, we need to relocate the update frame
2184 new_tso = (StgTSO *)copyPart((StgClosure *)tso,
2186 sizeofW(StgTSO), stp);
2187 move_TSO(tso, new_tso);
2188 for (p = tso->sp, q = new_tso->sp;
2189 p < tso->stack+tso->stack_size;) {
2193 return (StgClosure *)new_tso;
2200 //StgInfoTable *rip = get_closure_info(q, &size, &ptrs, &nonptrs, &vhs, str);
2201 to = copy(q,BLACKHOLE_sizeW(),stp);
2202 //ToDo: derive size etc from reverted IP
2203 //to = copy(q,size,stp);
2204 debugTrace(DEBUG_gc, "evacuate: RBH %p (%s) to %p (%s)",
2205 q, info_type(q), to, info_type(to));
2210 ASSERT(sizeofW(StgBlockedFetch) >= MIN_PAYLOD_SIZE);
2211 to = copy(q,sizeofW(StgBlockedFetch),stp);
2212 debugTrace(DEBUG_gc, "evacuate: %p (%s) to %p (%s)",
2213 q, info_type(q), to, info_type(to));
2220 ASSERT(sizeofW(StgBlockedFetch) >= MIN_PAYLOAD_SIZE);
2221 to = copy(q,sizeofW(StgFetchMe),stp);
2222 debugTrace(DEBUG_gc, "evacuate: %p (%s) to %p (%s)",
2223 q, info_type(q), to, info_type(to)));
2227 ASSERT(sizeofW(StgBlockedFetch) >= MIN_PAYLOAD_SIZE);
2228 to = copy(q,sizeofW(StgFetchMeBlockingQueue),stp);
2229 debugTrace(DEBUG_gc, "evacuate: %p (%s) to %p (%s)",
2230 q, info_type(q), to, info_type(to)));
2235 return copy(q,sizeofW(StgTRecHeader),stp);
2237 case TVAR_WAIT_QUEUE:
2238 return copy(q,sizeofW(StgTVarWaitQueue),stp);
2241 return copy(q,sizeofW(StgTVar),stp);
2244 return copy(q,sizeofW(StgTRecChunk),stp);
2247 barf("evacuate: strange closure type %d", (int)(info->type));
2253 /* -----------------------------------------------------------------------------
2254 Evaluate a THUNK_SELECTOR if possible.
2256 returns: NULL if we couldn't evaluate this THUNK_SELECTOR, or
2257 a closure pointer if we evaluated it and this is the result. Note
2258 that "evaluating" the THUNK_SELECTOR doesn't necessarily mean
2259 reducing it to HNF, just that we have eliminated the selection.
2260 The result might be another thunk, or even another THUNK_SELECTOR.
2262 If the return value is non-NULL, the original selector thunk has
2263 been BLACKHOLE'd, and should be updated with an indirection or a
2264 forwarding pointer. If the return value is NULL, then the selector
2268 ToDo: the treatment of THUNK_SELECTORS could be improved in the
2269 following way (from a suggestion by Ian Lynagh):
2271 We can have a chain like this:
2275 |-----> sel_0 --> (a,b)
2277 |-----> sel_0 --> ...
2279 and the depth limit means we don't go all the way to the end of the
2280 chain, which results in a space leak. This affects the recursive
2281 call to evacuate() in the THUNK_SELECTOR case in evacuate(): *not*
2282 the recursive call to eval_thunk_selector() in
2283 eval_thunk_selector().
2285 We could eliminate the depth bound in this case, in the following
2288 - traverse the chain once to discover the *value* of the
2289 THUNK_SELECTOR. Mark all THUNK_SELECTORS that we
2290 visit on the way as having been visited already (somehow).
2292 - in a second pass, traverse the chain again updating all
2293 THUNK_SEELCTORS that we find on the way with indirections to
2296 - if we encounter a "marked" THUNK_SELECTOR in a normal
2297 evacuate(), we konw it can't be updated so just evac it.
2299 Program that illustrates the problem:
2302 foo (x:xs) = let (ys, zs) = foo xs
2303 in if x >= 0 then (x:ys, zs) else (ys, x:zs)
2305 main = bar [1..(100000000::Int)]
2306 bar xs = (\(ys, zs) -> print ys >> print zs) (foo xs)
2308 -------------------------------------------------------------------------- */
2310 static inline rtsBool
2311 is_to_space ( StgClosure *p )
2315 bd = Bdescr((StgPtr)p);
2316 if (HEAP_ALLOCED(p) &&
2317 ((bd->flags & BF_EVACUATED)
2318 || ((bd->flags & BF_COMPACTED) &&
2319 is_marked((P_)p,bd)))) {
2327 eval_thunk_selector( nat field, StgSelector * p )
2330 const StgInfoTable *info_ptr;
2331 StgClosure *selectee;
2333 selectee = p->selectee;
2335 // Save the real info pointer (NOTE: not the same as get_itbl()).
2336 info_ptr = p->header.info;
2338 // If the THUNK_SELECTOR is in a generation that we are not
2339 // collecting, then bail out early. We won't be able to save any
2340 // space in any case, and updating with an indirection is trickier
2342 if (Bdescr((StgPtr)p)->gen_no > N) {
2346 // BLACKHOLE the selector thunk, since it is now under evaluation.
2347 // This is important to stop us going into an infinite loop if
2348 // this selector thunk eventually refers to itself.
2349 SET_INFO(p,&stg_BLACKHOLE_info);
2353 // We don't want to end up in to-space, because this causes
2354 // problems when the GC later tries to evacuate the result of
2355 // eval_thunk_selector(). There are various ways this could
2358 // 1. following an IND_STATIC
2360 // 2. when the old generation is compacted, the mark phase updates
2361 // from-space pointers to be to-space pointers, and we can't
2362 // reliably tell which we're following (eg. from an IND_STATIC).
2364 // 3. compacting GC again: if we're looking at a constructor in
2365 // the compacted generation, it might point directly to objects
2366 // in to-space. We must bale out here, otherwise doing the selection
2367 // will result in a to-space pointer being returned.
2369 // (1) is dealt with using a BF_EVACUATED test on the
2370 // selectee. (2) and (3): we can tell if we're looking at an
2371 // object in the compacted generation that might point to
2372 // to-space objects by testing that (a) it is BF_COMPACTED, (b)
2373 // the compacted generation is being collected, and (c) the
2374 // object is marked. Only a marked object may have pointers that
2375 // point to to-space objects, because that happens when
2378 // The to-space test is now embodied in the in_to_space() inline
2379 // function, as it is re-used below.
2381 if (is_to_space(selectee)) {
2385 info = get_itbl(selectee);
2386 switch (info->type) {
2394 case CONSTR_NOCAF_STATIC:
2395 // check that the size is in range
2396 ASSERT(field < (StgWord32)(info->layout.payload.ptrs +
2397 info->layout.payload.nptrs));
2399 // Select the right field from the constructor, and check
2400 // that the result isn't in to-space. It might be in
2401 // to-space if, for example, this constructor contains
2402 // pointers to younger-gen objects (and is on the mut-once
2407 q = selectee->payload[field];
2408 if (is_to_space(q)) {
2418 case IND_OLDGEN_PERM:
2420 selectee = ((StgInd *)selectee)->indirectee;
2424 // We don't follow pointers into to-space; the constructor
2425 // has already been evacuated, so we won't save any space
2426 // leaks by evaluating this selector thunk anyhow.
2429 case THUNK_SELECTOR:
2433 // check that we don't recurse too much, re-using the
2434 // depth bound also used in evacuate().
2435 if (thunk_selector_depth >= MAX_THUNK_SELECTOR_DEPTH) {
2438 thunk_selector_depth++;
2440 val = eval_thunk_selector(info->layout.selector_offset,
2441 (StgSelector *)selectee);
2443 thunk_selector_depth--;
2448 // We evaluated this selector thunk, so update it with
2449 // an indirection. NOTE: we don't use UPD_IND here,
2450 // because we are guaranteed that p is in a generation
2451 // that we are collecting, and we never want to put the
2452 // indirection on a mutable list.
2454 // For the purposes of LDV profiling, we have destroyed
2455 // the original selector thunk.
2456 SET_INFO(p, info_ptr);
2457 LDV_RECORD_DEAD_FILL_SLOP_DYNAMIC(selectee);
2459 ((StgInd *)selectee)->indirectee = val;
2460 SET_INFO(selectee,&stg_IND_info);
2462 // For the purposes of LDV profiling, we have created an
2464 LDV_RECORD_CREATE(selectee);
2481 case SE_CAF_BLACKHOLE:
2493 // not evaluated yet
2497 barf("eval_thunk_selector: strange selectee %d",
2502 // We didn't manage to evaluate this thunk; restore the old info pointer
2503 SET_INFO(p, info_ptr);
2507 /* -----------------------------------------------------------------------------
2508 move_TSO is called to update the TSO structure after it has been
2509 moved from one place to another.
2510 -------------------------------------------------------------------------- */
2513 move_TSO (StgTSO *src, StgTSO *dest)
2517 // relocate the stack pointer...
2518 diff = (StgPtr)dest - (StgPtr)src; // In *words*
2519 dest->sp = (StgPtr)dest->sp + diff;
2522 /* Similar to scavenge_large_bitmap(), but we don't write back the
2523 * pointers we get back from evacuate().
2526 scavenge_large_srt_bitmap( StgLargeSRT *large_srt )
2533 bitmap = large_srt->l.bitmap[b];
2534 size = (nat)large_srt->l.size;
2535 p = (StgClosure **)large_srt->srt;
2536 for (i = 0; i < size; ) {
2537 if ((bitmap & 1) != 0) {
2542 if (i % BITS_IN(W_) == 0) {
2544 bitmap = large_srt->l.bitmap[b];
2546 bitmap = bitmap >> 1;
2551 /* evacuate the SRT. If srt_bitmap is zero, then there isn't an
2552 * srt field in the info table. That's ok, because we'll
2553 * never dereference it.
2556 scavenge_srt (StgClosure **srt, nat srt_bitmap)
2561 bitmap = srt_bitmap;
2564 if (bitmap == (StgHalfWord)(-1)) {
2565 scavenge_large_srt_bitmap( (StgLargeSRT *)srt );
2569 while (bitmap != 0) {
2570 if ((bitmap & 1) != 0) {
2571 #ifdef ENABLE_WIN32_DLL_SUPPORT
2572 // Special-case to handle references to closures hiding out in DLLs, since
2573 // double indirections required to get at those. The code generator knows
2574 // which is which when generating the SRT, so it stores the (indirect)
2575 // reference to the DLL closure in the table by first adding one to it.
2576 // We check for this here, and undo the addition before evacuating it.
2578 // If the SRT entry hasn't got bit 0 set, the SRT entry points to a
2579 // closure that's fixed at link-time, and no extra magic is required.
2580 if ( (unsigned long)(*srt) & 0x1 ) {
2581 evacuate(*stgCast(StgClosure**,(stgCast(unsigned long, *srt) & ~0x1)));
2590 bitmap = bitmap >> 1;
2596 scavenge_thunk_srt(const StgInfoTable *info)
2598 StgThunkInfoTable *thunk_info;
2600 if (!major_gc) return;
2602 thunk_info = itbl_to_thunk_itbl(info);
2603 scavenge_srt((StgClosure **)GET_SRT(thunk_info), thunk_info->i.srt_bitmap);
2607 scavenge_fun_srt(const StgInfoTable *info)
2609 StgFunInfoTable *fun_info;
2611 if (!major_gc) return;
2613 fun_info = itbl_to_fun_itbl(info);
2614 scavenge_srt((StgClosure **)GET_FUN_SRT(fun_info), fun_info->i.srt_bitmap);
2617 /* -----------------------------------------------------------------------------
2619 -------------------------------------------------------------------------- */
2622 scavengeTSO (StgTSO *tso)
2624 if ( tso->why_blocked == BlockedOnMVar
2625 || tso->why_blocked == BlockedOnBlackHole
2626 || tso->why_blocked == BlockedOnException
2628 || tso->why_blocked == BlockedOnGA
2629 || tso->why_blocked == BlockedOnGA_NoSend
2632 tso->block_info.closure = evacuate(tso->block_info.closure);
2634 if ( tso->blocked_exceptions != NULL ) {
2635 tso->blocked_exceptions =
2636 (StgTSO *)evacuate((StgClosure *)tso->blocked_exceptions);
2639 // We don't always chase the link field: TSOs on the blackhole
2640 // queue are not automatically alive, so the link field is a
2641 // "weak" pointer in that case.
2642 if (tso->why_blocked != BlockedOnBlackHole) {
2643 tso->link = (StgTSO *)evacuate((StgClosure *)tso->link);
2646 // scavange current transaction record
2647 tso->trec = (StgTRecHeader *)evacuate((StgClosure *)tso->trec);
2649 // scavenge this thread's stack
2650 scavenge_stack(tso->sp, &(tso->stack[tso->stack_size]));
2653 /* -----------------------------------------------------------------------------
2654 Blocks of function args occur on the stack (at the top) and
2656 -------------------------------------------------------------------------- */
2658 STATIC_INLINE StgPtr
2659 scavenge_arg_block (StgFunInfoTable *fun_info, StgClosure **args)
2666 switch (fun_info->f.fun_type) {
2668 bitmap = BITMAP_BITS(fun_info->f.b.bitmap);
2669 size = BITMAP_SIZE(fun_info->f.b.bitmap);
2672 size = GET_FUN_LARGE_BITMAP(fun_info)->size;
2673 scavenge_large_bitmap(p, GET_FUN_LARGE_BITMAP(fun_info), size);
2677 bitmap = BITMAP_BITS(stg_arg_bitmaps[fun_info->f.fun_type]);
2678 size = BITMAP_SIZE(stg_arg_bitmaps[fun_info->f.fun_type]);
2681 if ((bitmap & 1) == 0) {
2682 *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
2685 bitmap = bitmap >> 1;
2693 STATIC_INLINE StgPtr
2694 scavenge_PAP_payload (StgClosure *fun, StgClosure **payload, StgWord size)
2698 StgFunInfoTable *fun_info;
2700 fun_info = get_fun_itbl(fun);
2701 ASSERT(fun_info->i.type != PAP);
2702 p = (StgPtr)payload;
2704 switch (fun_info->f.fun_type) {
2706 bitmap = BITMAP_BITS(fun_info->f.b.bitmap);
2709 scavenge_large_bitmap(p, GET_FUN_LARGE_BITMAP(fun_info), size);
2713 scavenge_large_bitmap((StgPtr)payload, BCO_BITMAP(fun), size);
2717 bitmap = BITMAP_BITS(stg_arg_bitmaps[fun_info->f.fun_type]);
2720 if ((bitmap & 1) == 0) {
2721 *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
2724 bitmap = bitmap >> 1;
2732 STATIC_INLINE StgPtr
2733 scavenge_PAP (StgPAP *pap)
2735 pap->fun = evacuate(pap->fun);
2736 return scavenge_PAP_payload (pap->fun, pap->payload, pap->n_args);
2739 STATIC_INLINE StgPtr
2740 scavenge_AP (StgAP *ap)
2742 ap->fun = evacuate(ap->fun);
2743 return scavenge_PAP_payload (ap->fun, ap->payload, ap->n_args);
2746 /* -----------------------------------------------------------------------------
2747 Scavenge a given step until there are no more objects in this step
2750 evac_gen is set by the caller to be either zero (for a step in a
2751 generation < N) or G where G is the generation of the step being
2754 We sometimes temporarily change evac_gen back to zero if we're
2755 scavenging a mutable object where early promotion isn't such a good
2757 -------------------------------------------------------------------------- */
2765 nat saved_evac_gen = evac_gen;
2770 failed_to_evac = rtsFalse;
2772 /* scavenge phase - standard breadth-first scavenging of the
2776 while (bd != stp->hp_bd || p < stp->hp) {
2778 // If we're at the end of this block, move on to the next block
2779 if (bd != stp->hp_bd && p == bd->free) {
2785 ASSERT(LOOKS_LIKE_CLOSURE_PTR(p));
2786 info = get_itbl((StgClosure *)p);
2788 ASSERT(thunk_selector_depth == 0);
2791 switch (info->type) {
2795 StgMVar *mvar = ((StgMVar *)p);
2797 mvar->head = (StgTSO *)evacuate((StgClosure *)mvar->head);
2798 mvar->tail = (StgTSO *)evacuate((StgClosure *)mvar->tail);
2799 mvar->value = evacuate((StgClosure *)mvar->value);
2800 evac_gen = saved_evac_gen;
2801 failed_to_evac = rtsTrue; // mutable.
2802 p += sizeofW(StgMVar);
2807 scavenge_fun_srt(info);
2808 ((StgClosure *)p)->payload[1] = evacuate(((StgClosure *)p)->payload[1]);
2809 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
2810 p += sizeofW(StgHeader) + 2;
2814 scavenge_thunk_srt(info);
2815 ((StgThunk *)p)->payload[1] = evacuate(((StgThunk *)p)->payload[1]);
2816 ((StgThunk *)p)->payload[0] = evacuate(((StgThunk *)p)->payload[0]);
2817 p += sizeofW(StgThunk) + 2;
2821 ((StgClosure *)p)->payload[1] = evacuate(((StgClosure *)p)->payload[1]);
2822 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
2823 p += sizeofW(StgHeader) + 2;
2827 scavenge_thunk_srt(info);
2828 ((StgThunk *)p)->payload[0] = evacuate(((StgThunk *)p)->payload[0]);
2829 p += sizeofW(StgThunk) + 1;
2833 scavenge_fun_srt(info);
2835 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
2836 p += sizeofW(StgHeader) + 1;
2840 scavenge_thunk_srt(info);
2841 p += sizeofW(StgThunk) + 1;
2845 scavenge_fun_srt(info);
2847 p += sizeofW(StgHeader) + 1;
2851 scavenge_thunk_srt(info);
2852 p += sizeofW(StgThunk) + 2;
2856 scavenge_fun_srt(info);
2858 p += sizeofW(StgHeader) + 2;
2862 scavenge_thunk_srt(info);
2863 ((StgThunk *)p)->payload[0] = evacuate(((StgThunk *)p)->payload[0]);
2864 p += sizeofW(StgThunk) + 2;
2868 scavenge_fun_srt(info);
2870 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
2871 p += sizeofW(StgHeader) + 2;
2875 scavenge_fun_srt(info);
2882 scavenge_thunk_srt(info);
2883 end = (P_)((StgThunk *)p)->payload + info->layout.payload.ptrs;
2884 for (p = (P_)((StgThunk *)p)->payload; p < end; p++) {
2885 *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
2887 p += info->layout.payload.nptrs;
2898 end = (P_)((StgClosure *)p)->payload + info->layout.payload.ptrs;
2899 for (p = (P_)((StgClosure *)p)->payload; p < end; p++) {
2900 *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
2902 p += info->layout.payload.nptrs;
2907 StgBCO *bco = (StgBCO *)p;
2908 bco->instrs = (StgArrWords *)evacuate((StgClosure *)bco->instrs);
2909 bco->literals = (StgArrWords *)evacuate((StgClosure *)bco->literals);
2910 bco->ptrs = (StgMutArrPtrs *)evacuate((StgClosure *)bco->ptrs);
2911 bco->itbls = (StgArrWords *)evacuate((StgClosure *)bco->itbls);
2912 p += bco_sizeW(bco);
2917 if (stp->gen->no != 0) {
2920 // No need to call LDV_recordDead_FILL_SLOP_DYNAMIC() because an
2921 // IND_OLDGEN_PERM closure is larger than an IND_PERM closure.
2922 LDV_recordDead((StgClosure *)p, sizeofW(StgInd));
2925 // Todo: maybe use SET_HDR() and remove LDV_RECORD_CREATE()?
2927 SET_INFO(((StgClosure *)p), &stg_IND_OLDGEN_PERM_info);
2929 // We pretend that p has just been created.
2930 LDV_RECORD_CREATE((StgClosure *)p);
2933 case IND_OLDGEN_PERM:
2934 ((StgInd *)p)->indirectee = evacuate(((StgInd *)p)->indirectee);
2935 p += sizeofW(StgInd);
2939 case MUT_VAR_DIRTY: {
2940 rtsBool saved_eager_promotion = eager_promotion;
2942 eager_promotion = rtsFalse;
2943 ((StgMutVar *)p)->var = evacuate(((StgMutVar *)p)->var);
2944 eager_promotion = saved_eager_promotion;
2946 if (failed_to_evac) {
2947 ((StgClosure *)q)->header.info = &stg_MUT_VAR_DIRTY_info;
2949 ((StgClosure *)q)->header.info = &stg_MUT_VAR_CLEAN_info;
2951 p += sizeofW(StgMutVar);
2956 case SE_CAF_BLACKHOLE:
2959 p += BLACKHOLE_sizeW();
2962 case THUNK_SELECTOR:
2964 StgSelector *s = (StgSelector *)p;
2965 s->selectee = evacuate(s->selectee);
2966 p += THUNK_SELECTOR_sizeW();
2970 // A chunk of stack saved in a heap object
2973 StgAP_STACK *ap = (StgAP_STACK *)p;
2975 ap->fun = evacuate(ap->fun);
2976 scavenge_stack((StgPtr)ap->payload, (StgPtr)ap->payload + ap->size);
2977 p = (StgPtr)ap->payload + ap->size;
2982 p = scavenge_PAP((StgPAP *)p);
2986 p = scavenge_AP((StgAP *)p);
2990 // nothing to follow
2991 p += arr_words_sizeW((StgArrWords *)p);
2994 case MUT_ARR_PTRS_CLEAN:
2995 case MUT_ARR_PTRS_DIRTY:
2996 // follow everything
2999 rtsBool saved_eager;
3001 // We don't eagerly promote objects pointed to by a mutable
3002 // array, but if we find the array only points to objects in
3003 // the same or an older generation, we mark it "clean" and
3004 // avoid traversing it during minor GCs.
3005 saved_eager = eager_promotion;
3006 eager_promotion = rtsFalse;
3007 next = p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
3008 for (p = (P_)((StgMutArrPtrs *)p)->payload; p < next; p++) {
3009 *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
3011 eager_promotion = saved_eager;
3013 if (failed_to_evac) {
3014 ((StgClosure *)q)->header.info = &stg_MUT_ARR_PTRS_DIRTY_info;
3016 ((StgClosure *)q)->header.info = &stg_MUT_ARR_PTRS_CLEAN_info;
3019 failed_to_evac = rtsTrue; // always put it on the mutable list.
3023 case MUT_ARR_PTRS_FROZEN:
3024 case MUT_ARR_PTRS_FROZEN0:
3025 // follow everything
3029 next = p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
3030 for (p = (P_)((StgMutArrPtrs *)p)->payload; p < next; p++) {
3031 *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
3034 // If we're going to put this object on the mutable list, then
3035 // set its info ptr to MUT_ARR_PTRS_FROZEN0 to indicate that.
3036 if (failed_to_evac) {
3037 ((StgClosure *)q)->header.info = &stg_MUT_ARR_PTRS_FROZEN0_info;
3039 ((StgClosure *)q)->header.info = &stg_MUT_ARR_PTRS_FROZEN_info;
3046 StgTSO *tso = (StgTSO *)p;
3047 rtsBool saved_eager = eager_promotion;
3049 eager_promotion = rtsFalse;
3051 eager_promotion = saved_eager;
3053 if (failed_to_evac) {
3054 tso->flags |= TSO_DIRTY;
3056 tso->flags &= ~TSO_DIRTY;
3059 failed_to_evac = rtsTrue; // always on the mutable list
3060 p += tso_sizeW(tso);
3068 nat size, ptrs, nonptrs, vhs;
3070 StgInfoTable *rip = get_closure_info(p, &size, &ptrs, &nonptrs, &vhs, str);
3072 StgRBH *rbh = (StgRBH *)p;
3073 (StgClosure *)rbh->blocking_queue =
3074 evacuate((StgClosure *)rbh->blocking_queue);
3075 failed_to_evac = rtsTrue; // mutable anyhow.
3076 debugTrace(DEBUG_gc, "scavenge: RBH %p (%s) (new blocking_queue link=%p)",
3077 p, info_type(p), (StgClosure *)rbh->blocking_queue);
3078 // ToDo: use size of reverted closure here!
3079 p += BLACKHOLE_sizeW();
3085 StgBlockedFetch *bf = (StgBlockedFetch *)p;
3086 // follow the pointer to the node which is being demanded
3087 (StgClosure *)bf->node =
3088 evacuate((StgClosure *)bf->node);
3089 // follow the link to the rest of the blocking queue
3090 (StgClosure *)bf->link =
3091 evacuate((StgClosure *)bf->link);
3092 debugTrace(DEBUG_gc, "scavenge: %p (%s); node is now %p; exciting, isn't it",
3093 bf, info_type((StgClosure *)bf),
3094 bf->node, info_type(bf->node)));
3095 p += sizeofW(StgBlockedFetch);
3103 p += sizeofW(StgFetchMe);
3104 break; // nothing to do in this case
3108 StgFetchMeBlockingQueue *fmbq = (StgFetchMeBlockingQueue *)p;
3109 (StgClosure *)fmbq->blocking_queue =
3110 evacuate((StgClosure *)fmbq->blocking_queue);
3111 debugTrace(DEBUG_gc, "scavenge: %p (%s) exciting, isn't it",
3112 p, info_type((StgClosure *)p)));
3113 p += sizeofW(StgFetchMeBlockingQueue);
3118 case TVAR_WAIT_QUEUE:
3120 StgTVarWaitQueue *wq = ((StgTVarWaitQueue *) p);
3122 wq->waiting_tso = (StgTSO *)evacuate((StgClosure*)wq->waiting_tso);
3123 wq->next_queue_entry = (StgTVarWaitQueue *)evacuate((StgClosure*)wq->next_queue_entry);
3124 wq->prev_queue_entry = (StgTVarWaitQueue *)evacuate((StgClosure*)wq->prev_queue_entry);
3125 evac_gen = saved_evac_gen;
3126 failed_to_evac = rtsTrue; // mutable
3127 p += sizeofW(StgTVarWaitQueue);
3133 StgTVar *tvar = ((StgTVar *) p);
3135 tvar->current_value = evacuate((StgClosure*)tvar->current_value);
3136 tvar->first_wait_queue_entry = (StgTVarWaitQueue *)evacuate((StgClosure*)tvar->first_wait_queue_entry);
3137 evac_gen = saved_evac_gen;
3138 failed_to_evac = rtsTrue; // mutable
3139 p += sizeofW(StgTVar);
3145 StgTRecHeader *trec = ((StgTRecHeader *) p);
3147 trec->enclosing_trec = (StgTRecHeader *)evacuate((StgClosure*)trec->enclosing_trec);
3148 trec->current_chunk = (StgTRecChunk *)evacuate((StgClosure*)trec->current_chunk);
3149 evac_gen = saved_evac_gen;
3150 failed_to_evac = rtsTrue; // mutable
3151 p += sizeofW(StgTRecHeader);
3158 StgTRecChunk *tc = ((StgTRecChunk *) p);
3159 TRecEntry *e = &(tc -> entries[0]);
3161 tc->prev_chunk = (StgTRecChunk *)evacuate((StgClosure*)tc->prev_chunk);
3162 for (i = 0; i < tc -> next_entry_idx; i ++, e++ ) {
3163 e->tvar = (StgTVar *)evacuate((StgClosure*)e->tvar);
3164 e->expected_value = evacuate((StgClosure*)e->expected_value);
3165 e->new_value = evacuate((StgClosure*)e->new_value);
3167 evac_gen = saved_evac_gen;
3168 failed_to_evac = rtsTrue; // mutable
3169 p += sizeofW(StgTRecChunk);
3174 barf("scavenge: unimplemented/strange closure type %d @ %p",
3179 * We need to record the current object on the mutable list if
3180 * (a) It is actually mutable, or
3181 * (b) It contains pointers to a younger generation.
3182 * Case (b) arises if we didn't manage to promote everything that
3183 * the current object points to into the current generation.
3185 if (failed_to_evac) {
3186 failed_to_evac = rtsFalse;
3187 if (stp->gen_no > 0) {
3188 recordMutableGen((StgClosure *)q, stp->gen);
3197 /* -----------------------------------------------------------------------------
3198 Scavenge everything on the mark stack.
3200 This is slightly different from scavenge():
3201 - we don't walk linearly through the objects, so the scavenger
3202 doesn't need to advance the pointer on to the next object.
3203 -------------------------------------------------------------------------- */
3206 scavenge_mark_stack(void)
3212 evac_gen = oldest_gen->no;
3213 saved_evac_gen = evac_gen;
3216 while (!mark_stack_empty()) {
3217 p = pop_mark_stack();
3219 ASSERT(LOOKS_LIKE_CLOSURE_PTR(p));
3220 info = get_itbl((StgClosure *)p);
3223 switch (info->type) {
3227 StgMVar *mvar = ((StgMVar *)p);
3229 mvar->head = (StgTSO *)evacuate((StgClosure *)mvar->head);
3230 mvar->tail = (StgTSO *)evacuate((StgClosure *)mvar->tail);
3231 mvar->value = evacuate((StgClosure *)mvar->value);
3232 evac_gen = saved_evac_gen;
3233 failed_to_evac = rtsTrue; // mutable.
3238 scavenge_fun_srt(info);
3239 ((StgClosure *)p)->payload[1] = evacuate(((StgClosure *)p)->payload[1]);
3240 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
3244 scavenge_thunk_srt(info);
3245 ((StgThunk *)p)->payload[1] = evacuate(((StgThunk *)p)->payload[1]);
3246 ((StgThunk *)p)->payload[0] = evacuate(((StgThunk *)p)->payload[0]);
3250 ((StgClosure *)p)->payload[1] = evacuate(((StgClosure *)p)->payload[1]);
3251 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
3256 scavenge_fun_srt(info);
3257 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
3262 scavenge_thunk_srt(info);
3263 ((StgThunk *)p)->payload[0] = evacuate(((StgThunk *)p)->payload[0]);
3268 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
3273 scavenge_fun_srt(info);
3278 scavenge_thunk_srt(info);
3286 scavenge_fun_srt(info);
3293 scavenge_thunk_srt(info);
3294 end = (P_)((StgThunk *)p)->payload + info->layout.payload.ptrs;
3295 for (p = (P_)((StgThunk *)p)->payload; p < end; p++) {
3296 *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
3308 end = (P_)((StgClosure *)p)->payload + info->layout.payload.ptrs;
3309 for (p = (P_)((StgClosure *)p)->payload; p < end; p++) {
3310 *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
3316 StgBCO *bco = (StgBCO *)p;
3317 bco->instrs = (StgArrWords *)evacuate((StgClosure *)bco->instrs);
3318 bco->literals = (StgArrWords *)evacuate((StgClosure *)bco->literals);
3319 bco->ptrs = (StgMutArrPtrs *)evacuate((StgClosure *)bco->ptrs);
3320 bco->itbls = (StgArrWords *)evacuate((StgClosure *)bco->itbls);
3325 // don't need to do anything here: the only possible case
3326 // is that we're in a 1-space compacting collector, with
3327 // no "old" generation.
3331 case IND_OLDGEN_PERM:
3332 ((StgInd *)p)->indirectee =
3333 evacuate(((StgInd *)p)->indirectee);
3337 case MUT_VAR_DIRTY: {
3338 rtsBool saved_eager_promotion = eager_promotion;
3340 eager_promotion = rtsFalse;
3341 ((StgMutVar *)p)->var = evacuate(((StgMutVar *)p)->var);
3342 eager_promotion = saved_eager_promotion;
3344 if (failed_to_evac) {
3345 ((StgClosure *)q)->header.info = &stg_MUT_VAR_DIRTY_info;
3347 ((StgClosure *)q)->header.info = &stg_MUT_VAR_CLEAN_info;
3353 case SE_CAF_BLACKHOLE:
3359 case THUNK_SELECTOR:
3361 StgSelector *s = (StgSelector *)p;
3362 s->selectee = evacuate(s->selectee);
3366 // A chunk of stack saved in a heap object
3369 StgAP_STACK *ap = (StgAP_STACK *)p;
3371 ap->fun = evacuate(ap->fun);
3372 scavenge_stack((StgPtr)ap->payload, (StgPtr)ap->payload + ap->size);
3377 scavenge_PAP((StgPAP *)p);
3381 scavenge_AP((StgAP *)p);
3384 case MUT_ARR_PTRS_CLEAN:
3385 case MUT_ARR_PTRS_DIRTY:
3386 // follow everything
3389 rtsBool saved_eager;
3391 // We don't eagerly promote objects pointed to by a mutable
3392 // array, but if we find the array only points to objects in
3393 // the same or an older generation, we mark it "clean" and
3394 // avoid traversing it during minor GCs.
3395 saved_eager = eager_promotion;
3396 eager_promotion = rtsFalse;
3397 next = p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
3398 for (p = (P_)((StgMutArrPtrs *)p)->payload; p < next; p++) {
3399 *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
3401 eager_promotion = saved_eager;
3403 if (failed_to_evac) {
3404 ((StgClosure *)q)->header.info = &stg_MUT_ARR_PTRS_DIRTY_info;
3406 ((StgClosure *)q)->header.info = &stg_MUT_ARR_PTRS_CLEAN_info;
3409 failed_to_evac = rtsTrue; // mutable anyhow.
3413 case MUT_ARR_PTRS_FROZEN:
3414 case MUT_ARR_PTRS_FROZEN0:
3415 // follow everything
3419 next = p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
3420 for (p = (P_)((StgMutArrPtrs *)p)->payload; p < next; p++) {
3421 *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
3424 // If we're going to put this object on the mutable list, then
3425 // set its info ptr to MUT_ARR_PTRS_FROZEN0 to indicate that.
3426 if (failed_to_evac) {
3427 ((StgClosure *)q)->header.info = &stg_MUT_ARR_PTRS_FROZEN0_info;
3429 ((StgClosure *)q)->header.info = &stg_MUT_ARR_PTRS_FROZEN_info;
3436 StgTSO *tso = (StgTSO *)p;
3437 rtsBool saved_eager = eager_promotion;
3439 eager_promotion = rtsFalse;
3441 eager_promotion = saved_eager;
3443 if (failed_to_evac) {
3444 tso->flags |= TSO_DIRTY;
3446 tso->flags &= ~TSO_DIRTY;
3449 failed_to_evac = rtsTrue; // always on the mutable list
3457 nat size, ptrs, nonptrs, vhs;
3459 StgInfoTable *rip = get_closure_info(p, &size, &ptrs, &nonptrs, &vhs, str);
3461 StgRBH *rbh = (StgRBH *)p;
3462 bh->blocking_queue =
3463 (StgTSO *)evacuate((StgClosure *)bh->blocking_queue);
3464 failed_to_evac = rtsTrue; // mutable anyhow.
3465 debugTrace(DEBUG_gc, "scavenge: RBH %p (%s) (new blocking_queue link=%p)",
3466 p, info_type(p), (StgClosure *)rbh->blocking_queue));
3472 StgBlockedFetch *bf = (StgBlockedFetch *)p;
3473 // follow the pointer to the node which is being demanded
3474 (StgClosure *)bf->node =
3475 evacuate((StgClosure *)bf->node);
3476 // follow the link to the rest of the blocking queue
3477 (StgClosure *)bf->link =
3478 evacuate((StgClosure *)bf->link);
3479 debugTrace(DEBUG_gc, "scavenge: %p (%s); node is now %p; exciting, isn't it",
3480 bf, info_type((StgClosure *)bf),
3481 bf->node, info_type(bf->node)));
3489 break; // nothing to do in this case
3493 StgFetchMeBlockingQueue *fmbq = (StgFetchMeBlockingQueue *)p;
3494 (StgClosure *)fmbq->blocking_queue =
3495 evacuate((StgClosure *)fmbq->blocking_queue);
3496 debugTrace(DEBUG_gc, "scavenge: %p (%s) exciting, isn't it",
3497 p, info_type((StgClosure *)p)));
3502 case TVAR_WAIT_QUEUE:
3504 StgTVarWaitQueue *wq = ((StgTVarWaitQueue *) p);
3506 wq->waiting_tso = (StgTSO *)evacuate((StgClosure*)wq->waiting_tso);
3507 wq->next_queue_entry = (StgTVarWaitQueue *)evacuate((StgClosure*)wq->next_queue_entry);
3508 wq->prev_queue_entry = (StgTVarWaitQueue *)evacuate((StgClosure*)wq->prev_queue_entry);
3509 evac_gen = saved_evac_gen;
3510 failed_to_evac = rtsTrue; // mutable
3516 StgTVar *tvar = ((StgTVar *) p);
3518 tvar->current_value = evacuate((StgClosure*)tvar->current_value);
3519 tvar->first_wait_queue_entry = (StgTVarWaitQueue *)evacuate((StgClosure*)tvar->first_wait_queue_entry);
3520 evac_gen = saved_evac_gen;
3521 failed_to_evac = rtsTrue; // mutable
3528 StgTRecChunk *tc = ((StgTRecChunk *) p);
3529 TRecEntry *e = &(tc -> entries[0]);
3531 tc->prev_chunk = (StgTRecChunk *)evacuate((StgClosure*)tc->prev_chunk);
3532 for (i = 0; i < tc -> next_entry_idx; i ++, e++ ) {
3533 e->tvar = (StgTVar *)evacuate((StgClosure*)e->tvar);
3534 e->expected_value = evacuate((StgClosure*)e->expected_value);
3535 e->new_value = evacuate((StgClosure*)e->new_value);
3537 evac_gen = saved_evac_gen;
3538 failed_to_evac = rtsTrue; // mutable
3544 StgTRecHeader *trec = ((StgTRecHeader *) p);
3546 trec->enclosing_trec = (StgTRecHeader *)evacuate((StgClosure*)trec->enclosing_trec);
3547 trec->current_chunk = (StgTRecChunk *)evacuate((StgClosure*)trec->current_chunk);
3548 evac_gen = saved_evac_gen;
3549 failed_to_evac = rtsTrue; // mutable
3554 barf("scavenge_mark_stack: unimplemented/strange closure type %d @ %p",
3558 if (failed_to_evac) {
3559 failed_to_evac = rtsFalse;
3561 recordMutableGen((StgClosure *)q, &generations[evac_gen]);
3565 // mark the next bit to indicate "scavenged"
3566 mark(q+1, Bdescr(q));
3568 } // while (!mark_stack_empty())
3570 // start a new linear scan if the mark stack overflowed at some point
3571 if (mark_stack_overflowed && oldgen_scan_bd == NULL) {
3572 debugTrace(DEBUG_gc, "scavenge_mark_stack: starting linear scan");
3573 mark_stack_overflowed = rtsFalse;
3574 oldgen_scan_bd = oldest_gen->steps[0].old_blocks;
3575 oldgen_scan = oldgen_scan_bd->start;
3578 if (oldgen_scan_bd) {
3579 // push a new thing on the mark stack
3581 // find a closure that is marked but not scavenged, and start
3583 while (oldgen_scan < oldgen_scan_bd->free
3584 && !is_marked(oldgen_scan,oldgen_scan_bd)) {
3588 if (oldgen_scan < oldgen_scan_bd->free) {
3590 // already scavenged?
3591 if (is_marked(oldgen_scan+1,oldgen_scan_bd)) {
3592 oldgen_scan += sizeofW(StgHeader) + MIN_PAYLOAD_SIZE;
3595 push_mark_stack(oldgen_scan);
3596 // ToDo: bump the linear scan by the actual size of the object
3597 oldgen_scan += sizeofW(StgHeader) + MIN_PAYLOAD_SIZE;
3601 oldgen_scan_bd = oldgen_scan_bd->link;
3602 if (oldgen_scan_bd != NULL) {
3603 oldgen_scan = oldgen_scan_bd->start;
3609 /* -----------------------------------------------------------------------------
3610 Scavenge one object.
3612 This is used for objects that are temporarily marked as mutable
3613 because they contain old-to-new generation pointers. Only certain
3614 objects can have this property.
3615 -------------------------------------------------------------------------- */
3618 scavenge_one(StgPtr p)
3620 const StgInfoTable *info;
3621 nat saved_evac_gen = evac_gen;
3624 ASSERT(LOOKS_LIKE_CLOSURE_PTR(p));
3625 info = get_itbl((StgClosure *)p);
3627 switch (info->type) {
3631 StgMVar *mvar = ((StgMVar *)p);
3633 mvar->head = (StgTSO *)evacuate((StgClosure *)mvar->head);
3634 mvar->tail = (StgTSO *)evacuate((StgClosure *)mvar->tail);
3635 mvar->value = evacuate((StgClosure *)mvar->value);
3636 evac_gen = saved_evac_gen;
3637 failed_to_evac = rtsTrue; // mutable.
3650 end = (StgPtr)((StgThunk *)p)->payload + info->layout.payload.ptrs;
3651 for (q = (StgPtr)((StgThunk *)p)->payload; q < end; q++) {
3652 *q = (StgWord)(StgPtr)evacuate((StgClosure *)*q);
3658 case FUN_1_0: // hardly worth specialising these guys
3674 end = (StgPtr)((StgClosure *)p)->payload + info->layout.payload.ptrs;
3675 for (q = (StgPtr)((StgClosure *)p)->payload; q < end; q++) {
3676 *q = (StgWord)(StgPtr)evacuate((StgClosure *)*q);
3682 case MUT_VAR_DIRTY: {
3684 rtsBool saved_eager_promotion = eager_promotion;
3686 eager_promotion = rtsFalse;
3687 ((StgMutVar *)p)->var = evacuate(((StgMutVar *)p)->var);
3688 eager_promotion = saved_eager_promotion;
3690 if (failed_to_evac) {
3691 ((StgClosure *)q)->header.info = &stg_MUT_VAR_DIRTY_info;
3693 ((StgClosure *)q)->header.info = &stg_MUT_VAR_CLEAN_info;
3699 case SE_CAF_BLACKHOLE:
3704 case THUNK_SELECTOR:
3706 StgSelector *s = (StgSelector *)p;
3707 s->selectee = evacuate(s->selectee);
3713 StgAP_STACK *ap = (StgAP_STACK *)p;
3715 ap->fun = evacuate(ap->fun);
3716 scavenge_stack((StgPtr)ap->payload, (StgPtr)ap->payload + ap->size);
3717 p = (StgPtr)ap->payload + ap->size;
3722 p = scavenge_PAP((StgPAP *)p);
3726 p = scavenge_AP((StgAP *)p);
3730 // nothing to follow
3733 case MUT_ARR_PTRS_CLEAN:
3734 case MUT_ARR_PTRS_DIRTY:
3737 rtsBool saved_eager;
3739 // We don't eagerly promote objects pointed to by a mutable
3740 // array, but if we find the array only points to objects in
3741 // the same or an older generation, we mark it "clean" and
3742 // avoid traversing it during minor GCs.
3743 saved_eager = eager_promotion;
3744 eager_promotion = rtsFalse;
3746 next = p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
3747 for (p = (P_)((StgMutArrPtrs *)p)->payload; p < next; p++) {
3748 *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
3750 eager_promotion = saved_eager;
3752 if (failed_to_evac) {
3753 ((StgClosure *)q)->header.info = &stg_MUT_ARR_PTRS_DIRTY_info;
3755 ((StgClosure *)q)->header.info = &stg_MUT_ARR_PTRS_CLEAN_info;
3758 failed_to_evac = rtsTrue;
3762 case MUT_ARR_PTRS_FROZEN:
3763 case MUT_ARR_PTRS_FROZEN0:
3765 // follow everything
3768 next = p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
3769 for (p = (P_)((StgMutArrPtrs *)p)->payload; p < next; p++) {
3770 *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
3773 // If we're going to put this object on the mutable list, then
3774 // set its info ptr to MUT_ARR_PTRS_FROZEN0 to indicate that.
3775 if (failed_to_evac) {
3776 ((StgClosure *)q)->header.info = &stg_MUT_ARR_PTRS_FROZEN0_info;
3778 ((StgClosure *)q)->header.info = &stg_MUT_ARR_PTRS_FROZEN_info;
3785 StgTSO *tso = (StgTSO *)p;
3786 rtsBool saved_eager = eager_promotion;
3788 eager_promotion = rtsFalse;
3790 eager_promotion = saved_eager;
3792 if (failed_to_evac) {
3793 tso->flags |= TSO_DIRTY;
3795 tso->flags &= ~TSO_DIRTY;
3798 failed_to_evac = rtsTrue; // always on the mutable list
3806 nat size, ptrs, nonptrs, vhs;
3808 StgInfoTable *rip = get_closure_info(p, &size, &ptrs, &nonptrs, &vhs, str);
3810 StgRBH *rbh = (StgRBH *)p;
3811 (StgClosure *)rbh->blocking_queue =
3812 evacuate((StgClosure *)rbh->blocking_queue);
3813 failed_to_evac = rtsTrue; // mutable anyhow.
3814 debugTrace(DEBUG_gc, "scavenge: RBH %p (%s) (new blocking_queue link=%p)",
3815 p, info_type(p), (StgClosure *)rbh->blocking_queue));
3816 // ToDo: use size of reverted closure here!
3822 StgBlockedFetch *bf = (StgBlockedFetch *)p;
3823 // follow the pointer to the node which is being demanded
3824 (StgClosure *)bf->node =
3825 evacuate((StgClosure *)bf->node);
3826 // follow the link to the rest of the blocking queue
3827 (StgClosure *)bf->link =
3828 evacuate((StgClosure *)bf->link);
3829 debugTrace(DEBUG_gc,
3830 "scavenge: %p (%s); node is now %p; exciting, isn't it",
3831 bf, info_type((StgClosure *)bf),
3832 bf->node, info_type(bf->node)));
3840 break; // nothing to do in this case
3844 StgFetchMeBlockingQueue *fmbq = (StgFetchMeBlockingQueue *)p;
3845 (StgClosure *)fmbq->blocking_queue =
3846 evacuate((StgClosure *)fmbq->blocking_queue);
3847 debugTrace(DEBUG_gc, "scavenge: %p (%s) exciting, isn't it",
3848 p, info_type((StgClosure *)p)));
3853 case TVAR_WAIT_QUEUE:
3855 StgTVarWaitQueue *wq = ((StgTVarWaitQueue *) p);
3857 wq->waiting_tso = (StgTSO *)evacuate((StgClosure*)wq->waiting_tso);
3858 wq->next_queue_entry = (StgTVarWaitQueue *)evacuate((StgClosure*)wq->next_queue_entry);
3859 wq->prev_queue_entry = (StgTVarWaitQueue *)evacuate((StgClosure*)wq->prev_queue_entry);
3860 evac_gen = saved_evac_gen;
3861 failed_to_evac = rtsTrue; // mutable
3867 StgTVar *tvar = ((StgTVar *) p);
3869 tvar->current_value = evacuate((StgClosure*)tvar->current_value);
3870 tvar->first_wait_queue_entry = (StgTVarWaitQueue *)evacuate((StgClosure*)tvar->first_wait_queue_entry);
3871 evac_gen = saved_evac_gen;
3872 failed_to_evac = rtsTrue; // mutable
3878 StgTRecHeader *trec = ((StgTRecHeader *) p);
3880 trec->enclosing_trec = (StgTRecHeader *)evacuate((StgClosure*)trec->enclosing_trec);
3881 trec->current_chunk = (StgTRecChunk *)evacuate((StgClosure*)trec->current_chunk);
3882 evac_gen = saved_evac_gen;
3883 failed_to_evac = rtsTrue; // mutable
3890 StgTRecChunk *tc = ((StgTRecChunk *) p);
3891 TRecEntry *e = &(tc -> entries[0]);
3893 tc->prev_chunk = (StgTRecChunk *)evacuate((StgClosure*)tc->prev_chunk);
3894 for (i = 0; i < tc -> next_entry_idx; i ++, e++ ) {
3895 e->tvar = (StgTVar *)evacuate((StgClosure*)e->tvar);
3896 e->expected_value = evacuate((StgClosure*)e->expected_value);
3897 e->new_value = evacuate((StgClosure*)e->new_value);
3899 evac_gen = saved_evac_gen;
3900 failed_to_evac = rtsTrue; // mutable
3905 case IND_OLDGEN_PERM:
3908 /* Careful here: a THUNK can be on the mutable list because
3909 * it contains pointers to young gen objects. If such a thunk
3910 * is updated, the IND_OLDGEN will be added to the mutable
3911 * list again, and we'll scavenge it twice. evacuate()
3912 * doesn't check whether the object has already been
3913 * evacuated, so we perform that check here.
3915 StgClosure *q = ((StgInd *)p)->indirectee;
3916 if (HEAP_ALLOCED(q) && Bdescr((StgPtr)q)->flags & BF_EVACUATED) {
3919 ((StgInd *)p)->indirectee = evacuate(q);
3922 #if 0 && defined(DEBUG)
3923 if (RtsFlags.DebugFlags.gc)
3924 /* Debugging code to print out the size of the thing we just
3928 StgPtr start = gen->steps[0].scan;
3929 bdescr *start_bd = gen->steps[0].scan_bd;
3931 scavenge(&gen->steps[0]);
3932 if (start_bd != gen->steps[0].scan_bd) {
3933 size += (P_)BLOCK_ROUND_UP(start) - start;
3934 start_bd = start_bd->link;
3935 while (start_bd != gen->steps[0].scan_bd) {
3936 size += BLOCK_SIZE_W;
3937 start_bd = start_bd->link;
3939 size += gen->steps[0].scan -
3940 (P_)BLOCK_ROUND_DOWN(gen->steps[0].scan);
3942 size = gen->steps[0].scan - start;
3944 debugBelch("evac IND_OLDGEN: %ld bytes", size * sizeof(W_));
3950 barf("scavenge_one: strange object %d", (int)(info->type));
3953 no_luck = failed_to_evac;
3954 failed_to_evac = rtsFalse;
3958 /* -----------------------------------------------------------------------------
3959 Scavenging mutable lists.
3961 We treat the mutable list of each generation > N (i.e. all the
3962 generations older than the one being collected) as roots. We also
3963 remove non-mutable objects from the mutable list at this point.
3964 -------------------------------------------------------------------------- */
3967 scavenge_mutable_list(generation *gen)
3972 bd = gen->saved_mut_list;
3975 for (; bd != NULL; bd = bd->link) {
3976 for (q = bd->start; q < bd->free; q++) {
3978 ASSERT(LOOKS_LIKE_CLOSURE_PTR(p));
3981 switch (get_itbl((StgClosure *)p)->type) {
3983 barf("MUT_VAR_CLEAN on mutable list");
3985 mutlist_MUTVARS++; break;
3986 case MUT_ARR_PTRS_CLEAN:
3987 case MUT_ARR_PTRS_DIRTY:
3988 case MUT_ARR_PTRS_FROZEN:
3989 case MUT_ARR_PTRS_FROZEN0:
3990 mutlist_MUTARRS++; break;
3992 mutlist_OTHERS++; break;
3996 // Check whether this object is "clean", that is it
3997 // definitely doesn't point into a young generation.
3998 // Clean objects don't need to be scavenged. Some clean
3999 // objects (MUT_VAR_CLEAN) are not kept on the mutable
4000 // list at all; others, such as MUT_ARR_PTRS_CLEAN and
4001 // TSO, are always on the mutable list.
4003 switch (get_itbl((StgClosure *)p)->type) {
4004 case MUT_ARR_PTRS_CLEAN:
4005 recordMutableGen((StgClosure *)p,gen);
4008 StgTSO *tso = (StgTSO *)p;
4009 if ((tso->flags & TSO_DIRTY) == 0) {
4010 // A clean TSO: we don't have to traverse its
4011 // stack. However, we *do* follow the link field:
4012 // we don't want to have to mark a TSO dirty just
4013 // because we put it on a different queue.
4014 if (tso->why_blocked != BlockedOnBlackHole) {
4015 tso->link = (StgTSO *)evacuate((StgClosure *)tso->link);
4017 recordMutableGen((StgClosure *)p,gen);
4025 if (scavenge_one(p)) {
4026 // didn't manage to promote everything, so put the
4027 // object back on the list.
4028 recordMutableGen((StgClosure *)p,gen);
4033 // free the old mut_list
4034 freeChain(gen->saved_mut_list);
4035 gen->saved_mut_list = NULL;
4040 scavenge_static(void)
4042 StgClosure* p = static_objects;
4043 const StgInfoTable *info;
4045 /* Always evacuate straight to the oldest generation for static
4047 evac_gen = oldest_gen->no;
4049 /* keep going until we've scavenged all the objects on the linked
4051 while (p != END_OF_STATIC_LIST) {
4053 ASSERT(LOOKS_LIKE_CLOSURE_PTR(p));
4056 if (info->type==RBH)
4057 info = REVERT_INFOPTR(info); // if it's an RBH, look at the orig closure
4059 // make sure the info pointer is into text space
4061 /* Take this object *off* the static_objects list,
4062 * and put it on the scavenged_static_objects list.
4064 static_objects = *STATIC_LINK(info,p);
4065 *STATIC_LINK(info,p) = scavenged_static_objects;
4066 scavenged_static_objects = p;
4068 switch (info -> type) {
4072 StgInd *ind = (StgInd *)p;
4073 ind->indirectee = evacuate(ind->indirectee);
4075 /* might fail to evacuate it, in which case we have to pop it
4076 * back on the mutable list of the oldest generation. We
4077 * leave it *on* the scavenged_static_objects list, though,
4078 * in case we visit this object again.
4080 if (failed_to_evac) {
4081 failed_to_evac = rtsFalse;
4082 recordMutableGen((StgClosure *)p,oldest_gen);
4088 scavenge_thunk_srt(info);
4092 scavenge_fun_srt(info);
4099 next = (P_)p->payload + info->layout.payload.ptrs;
4100 // evacuate the pointers
4101 for (q = (P_)p->payload; q < next; q++) {
4102 *q = (StgWord)(StgPtr)evacuate((StgClosure *)*q);
4108 barf("scavenge_static: strange closure %d", (int)(info->type));
4111 ASSERT(failed_to_evac == rtsFalse);
4113 /* get the next static object from the list. Remember, there might
4114 * be more stuff on this list now that we've done some evacuating!
4115 * (static_objects is a global)
4121 /* -----------------------------------------------------------------------------
4122 scavenge a chunk of memory described by a bitmap
4123 -------------------------------------------------------------------------- */
4126 scavenge_large_bitmap( StgPtr p, StgLargeBitmap *large_bitmap, nat size )
4132 bitmap = large_bitmap->bitmap[b];
4133 for (i = 0; i < size; ) {
4134 if ((bitmap & 1) == 0) {
4135 *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
4139 if (i % BITS_IN(W_) == 0) {
4141 bitmap = large_bitmap->bitmap[b];
4143 bitmap = bitmap >> 1;
4148 STATIC_INLINE StgPtr
4149 scavenge_small_bitmap (StgPtr p, nat size, StgWord bitmap)
4152 if ((bitmap & 1) == 0) {
4153 *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
4156 bitmap = bitmap >> 1;
4162 /* -----------------------------------------------------------------------------
4163 scavenge_stack walks over a section of stack and evacuates all the
4164 objects pointed to by it. We can use the same code for walking
4165 AP_STACK_UPDs, since these are just sections of copied stack.
4166 -------------------------------------------------------------------------- */
4170 scavenge_stack(StgPtr p, StgPtr stack_end)
4172 const StgRetInfoTable* info;
4177 * Each time around this loop, we are looking at a chunk of stack
4178 * that starts with an activation record.
4181 while (p < stack_end) {
4182 info = get_ret_itbl((StgClosure *)p);
4184 switch (info->i.type) {
4187 // In SMP, we can get update frames that point to indirections
4188 // when two threads evaluate the same thunk. We do attempt to
4189 // discover this situation in threadPaused(), but it's
4190 // possible that the following sequence occurs:
4199 // Now T is an indirection, and the update frame is already
4200 // marked on A's stack, so we won't traverse it again in
4201 // threadPaused(). We could traverse the whole stack again
4202 // before GC, but that seems like overkill.
4204 // Scavenging this update frame as normal would be disastrous;
4205 // the updatee would end up pointing to the value. So we turn
4206 // the indirection into an IND_PERM, so that evacuate will
4207 // copy the indirection into the old generation instead of
4209 if (get_itbl(((StgUpdateFrame *)p)->updatee)->type == IND) {
4210 ((StgUpdateFrame *)p)->updatee->header.info =
4211 (StgInfoTable *)&stg_IND_PERM_info;
4213 ((StgUpdateFrame *)p)->updatee
4214 = evacuate(((StgUpdateFrame *)p)->updatee);
4215 p += sizeofW(StgUpdateFrame);
4218 // small bitmap (< 32 entries, or 64 on a 64-bit machine)
4219 case CATCH_STM_FRAME:
4220 case CATCH_RETRY_FRAME:
4221 case ATOMICALLY_FRAME:
4226 bitmap = BITMAP_BITS(info->i.layout.bitmap);
4227 size = BITMAP_SIZE(info->i.layout.bitmap);
4228 // NOTE: the payload starts immediately after the info-ptr, we
4229 // don't have an StgHeader in the same sense as a heap closure.
4231 p = scavenge_small_bitmap(p, size, bitmap);
4235 scavenge_srt((StgClosure **)GET_SRT(info), info->i.srt_bitmap);
4243 *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
4246 size = BCO_BITMAP_SIZE(bco);
4247 scavenge_large_bitmap(p, BCO_BITMAP(bco), size);
4252 // large bitmap (> 32 entries, or > 64 on a 64-bit machine)
4258 size = GET_LARGE_BITMAP(&info->i)->size;
4260 scavenge_large_bitmap(p, GET_LARGE_BITMAP(&info->i), size);
4262 // and don't forget to follow the SRT
4266 // Dynamic bitmap: the mask is stored on the stack, and
4267 // there are a number of non-pointers followed by a number
4268 // of pointers above the bitmapped area. (see StgMacros.h,
4273 dyn = ((StgRetDyn *)p)->liveness;
4275 // traverse the bitmap first
4276 bitmap = RET_DYN_LIVENESS(dyn);
4277 p = (P_)&((StgRetDyn *)p)->payload[0];
4278 size = RET_DYN_BITMAP_SIZE;
4279 p = scavenge_small_bitmap(p, size, bitmap);
4281 // skip over the non-ptr words
4282 p += RET_DYN_NONPTRS(dyn) + RET_DYN_NONPTR_REGS_SIZE;
4284 // follow the ptr words
4285 for (size = RET_DYN_PTRS(dyn); size > 0; size--) {
4286 *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
4294 StgRetFun *ret_fun = (StgRetFun *)p;
4295 StgFunInfoTable *fun_info;
4297 ret_fun->fun = evacuate(ret_fun->fun);
4298 fun_info = get_fun_itbl(ret_fun->fun);
4299 p = scavenge_arg_block(fun_info, ret_fun->payload);
4304 barf("scavenge_stack: weird activation record found on stack: %d", (int)(info->i.type));
4309 /*-----------------------------------------------------------------------------
4310 scavenge the large object list.
4312 evac_gen set by caller; similar games played with evac_gen as with
4313 scavenge() - see comment at the top of scavenge(). Most large
4314 objects are (repeatedly) mutable, so most of the time evac_gen will
4316 --------------------------------------------------------------------------- */
4319 scavenge_large(step *stp)
4324 bd = stp->new_large_objects;
4326 for (; bd != NULL; bd = stp->new_large_objects) {
4328 /* take this object *off* the large objects list and put it on
4329 * the scavenged large objects list. This is so that we can
4330 * treat new_large_objects as a stack and push new objects on
4331 * the front when evacuating.
4333 stp->new_large_objects = bd->link;
4334 dbl_link_onto(bd, &stp->scavenged_large_objects);
4336 // update the block count in this step.
4337 stp->n_scavenged_large_blocks += bd->blocks;
4340 if (scavenge_one(p)) {
4341 if (stp->gen_no > 0) {
4342 recordMutableGen((StgClosure *)p, stp->gen);
4348 /* -----------------------------------------------------------------------------
4349 Initialising the static object & mutable lists
4350 -------------------------------------------------------------------------- */
4353 zero_static_object_list(StgClosure* first_static)
4357 const StgInfoTable *info;
4359 for (p = first_static; p != END_OF_STATIC_LIST; p = link) {
4361 link = *STATIC_LINK(info, p);
4362 *STATIC_LINK(info,p) = NULL;
4366 /* -----------------------------------------------------------------------------
4368 -------------------------------------------------------------------------- */
4375 for (c = (StgIndStatic *)revertible_caf_list; c != NULL;
4376 c = (StgIndStatic *)c->static_link)
4378 SET_INFO(c, c->saved_info);
4379 c->saved_info = NULL;
4380 // could, but not necessary: c->static_link = NULL;
4382 revertible_caf_list = NULL;
4386 markCAFs( evac_fn evac )
4390 for (c = (StgIndStatic *)caf_list; c != NULL;
4391 c = (StgIndStatic *)c->static_link)
4393 evac(&c->indirectee);
4395 for (c = (StgIndStatic *)revertible_caf_list; c != NULL;
4396 c = (StgIndStatic *)c->static_link)
4398 evac(&c->indirectee);
4402 /* -----------------------------------------------------------------------------
4403 Sanity code for CAF garbage collection.
4405 With DEBUG turned on, we manage a CAF list in addition to the SRT
4406 mechanism. After GC, we run down the CAF list and blackhole any
4407 CAFs which have been garbage collected. This means we get an error
4408 whenever the program tries to enter a garbage collected CAF.
4410 Any garbage collected CAFs are taken off the CAF list at the same
4412 -------------------------------------------------------------------------- */
4414 #if 0 && defined(DEBUG)
4421 const StgInfoTable *info;
4432 ASSERT(info->type == IND_STATIC);
4434 if (STATIC_LINK(info,p) == NULL) {
4435 debugTrace(DEBUG_gccafs, "CAF gc'd at 0x%04lx", (long)p);
4437 SET_INFO(p,&stg_BLACKHOLE_info);
4438 p = STATIC_LINK2(info,p);
4442 pp = &STATIC_LINK2(info,p);
4449 debugTrace(DEBUG_gccafs, "%d CAFs live", i);
4454 /* -----------------------------------------------------------------------------
4457 * Code largely pinched from old RTS, then hacked to bits. We also do
4458 * lazy black holing here.
4460 * -------------------------------------------------------------------------- */
4462 struct stack_gap { StgWord gap_size; struct stack_gap *next_gap; };
4465 stackSqueeze(StgTSO *tso, StgPtr bottom)
4468 rtsBool prev_was_update_frame;
4469 StgClosure *updatee = NULL;
4470 StgRetInfoTable *info;
4471 StgWord current_gap_size;
4472 struct stack_gap *gap;
4475 // Traverse the stack upwards, replacing adjacent update frames
4476 // with a single update frame and a "stack gap". A stack gap
4477 // contains two values: the size of the gap, and the distance
4478 // to the next gap (or the stack top).
4482 ASSERT(frame < bottom);
4484 prev_was_update_frame = rtsFalse;
4485 current_gap_size = 0;
4486 gap = (struct stack_gap *) (tso->sp - sizeofW(StgUpdateFrame));
4488 while (frame < bottom) {
4490 info = get_ret_itbl((StgClosure *)frame);
4491 switch (info->i.type) {
4495 StgUpdateFrame *upd = (StgUpdateFrame *)frame;
4497 if (prev_was_update_frame) {
4499 TICK_UPD_SQUEEZED();
4500 /* wasn't there something about update squeezing and ticky to be
4501 * sorted out? oh yes: we aren't counting each enter properly
4502 * in this case. See the log somewhere. KSW 1999-04-21
4504 * Check two things: that the two update frames don't point to
4505 * the same object, and that the updatee_bypass isn't already an
4506 * indirection. Both of these cases only happen when we're in a
4507 * block hole-style loop (and there are multiple update frames
4508 * on the stack pointing to the same closure), but they can both
4509 * screw us up if we don't check.
4511 if (upd->updatee != updatee && !closure_IND(upd->updatee)) {
4512 UPD_IND_NOLOCK(upd->updatee, updatee);
4515 // now mark this update frame as a stack gap. The gap
4516 // marker resides in the bottom-most update frame of
4517 // the series of adjacent frames, and covers all the
4518 // frames in this series.
4519 current_gap_size += sizeofW(StgUpdateFrame);
4520 ((struct stack_gap *)frame)->gap_size = current_gap_size;
4521 ((struct stack_gap *)frame)->next_gap = gap;
4523 frame += sizeofW(StgUpdateFrame);
4527 // single update frame, or the topmost update frame in a series
4529 prev_was_update_frame = rtsTrue;
4530 updatee = upd->updatee;
4531 frame += sizeofW(StgUpdateFrame);
4537 prev_was_update_frame = rtsFalse;
4539 // we're not in a gap... check whether this is the end of a gap
4540 // (an update frame can't be the end of a gap).
4541 if (current_gap_size != 0) {
4542 gap = (struct stack_gap *) (frame - sizeofW(StgUpdateFrame));
4544 current_gap_size = 0;
4546 frame += stack_frame_sizeW((StgClosure *)frame);
4551 if (current_gap_size != 0) {
4552 gap = (struct stack_gap *) (frame - sizeofW(StgUpdateFrame));
4555 // Now we have a stack with gaps in it, and we have to walk down
4556 // shoving the stack up to fill in the gaps. A diagram might
4560 // | ********* | <- sp
4564 // | stack_gap | <- gap | chunk_size
4566 // | ......... | <- gap_end v
4572 // 'sp' points the the current top-of-stack
4573 // 'gap' points to the stack_gap structure inside the gap
4574 // ***** indicates real stack data
4575 // ..... indicates gap
4576 // <empty> indicates unused
4580 void *gap_start, *next_gap_start, *gap_end;
4583 next_gap_start = (void *)((unsigned char*)gap + sizeof(StgUpdateFrame));
4584 sp = next_gap_start;
4586 while ((StgPtr)gap > tso->sp) {
4588 // we're working in *bytes* now...
4589 gap_start = next_gap_start;
4590 gap_end = (void*) ((unsigned char*)gap_start - gap->gap_size * sizeof(W_));
4592 gap = gap->next_gap;
4593 next_gap_start = (void *)((unsigned char*)gap + sizeof(StgUpdateFrame));
4595 chunk_size = (unsigned char*)gap_end - (unsigned char*)next_gap_start;
4597 memmove(sp, next_gap_start, chunk_size);
4600 tso->sp = (StgPtr)sp;
4604 /* -----------------------------------------------------------------------------
4607 * We have to prepare for GC - this means doing lazy black holing
4608 * here. We also take the opportunity to do stack squeezing if it's
4610 * -------------------------------------------------------------------------- */
4612 threadPaused(Capability *cap, StgTSO *tso)
4615 StgRetInfoTable *info;
4618 nat words_to_squeeze = 0;
4620 nat weight_pending = 0;
4621 rtsBool prev_was_update_frame;
4623 stack_end = &tso->stack[tso->stack_size];
4625 frame = (StgClosure *)tso->sp;
4628 // If we've already marked this frame, then stop here.
4629 if (frame->header.info == (StgInfoTable *)&stg_marked_upd_frame_info) {
4633 info = get_ret_itbl(frame);
4635 switch (info->i.type) {
4639 SET_INFO(frame, (StgInfoTable *)&stg_marked_upd_frame_info);
4641 bh = ((StgUpdateFrame *)frame)->updatee;
4643 if (closure_IND(bh) || bh->header.info == &stg_BLACKHOLE_info) {
4644 debugTrace(DEBUG_squeeze,
4645 "suspending duplicate work: %ld words of stack",
4646 (StgPtr)frame - tso->sp);
4648 // If this closure is already an indirection, then
4649 // suspend the computation up to this point:
4650 suspendComputation(cap,tso,(StgPtr)frame);
4652 // Now drop the update frame, and arrange to return
4653 // the value to the frame underneath:
4654 tso->sp = (StgPtr)frame + sizeofW(StgUpdateFrame) - 2;
4655 tso->sp[1] = (StgWord)bh;
4656 tso->sp[0] = (W_)&stg_enter_info;
4658 // And continue with threadPaused; there might be
4659 // yet more computation to suspend.
4660 threadPaused(cap,tso);
4664 if (bh->header.info != &stg_CAF_BLACKHOLE_info) {
4665 #if (!defined(LAZY_BLACKHOLING)) && defined(DEBUG)
4666 debugBelch("Unexpected lazy BHing required at 0x%04lx\n",(long)bh);
4668 // zero out the slop so that the sanity checker can tell
4669 // where the next closure is.
4670 DEBUG_FILL_SLOP(bh);
4673 // We pretend that bh is now dead.
4674 LDV_recordDead_FILL_SLOP_DYNAMIC((StgClosure *)bh);
4676 SET_INFO(bh,&stg_BLACKHOLE_info);
4678 // We pretend that bh has just been created.
4679 LDV_RECORD_CREATE(bh);
4682 frame = (StgClosure *) ((StgUpdateFrame *)frame + 1);
4683 if (prev_was_update_frame) {
4684 words_to_squeeze += sizeofW(StgUpdateFrame);
4685 weight += weight_pending;
4688 prev_was_update_frame = rtsTrue;
4694 // normal stack frames; do nothing except advance the pointer
4697 nat frame_size = stack_frame_sizeW(frame);
4698 weight_pending += frame_size;
4699 frame = (StgClosure *)((StgPtr)frame + frame_size);
4700 prev_was_update_frame = rtsFalse;
4706 debugTrace(DEBUG_squeeze,
4707 "words_to_squeeze: %d, weight: %d, squeeze: %s",
4708 words_to_squeeze, weight,
4709 weight < words_to_squeeze ? "YES" : "NO");
4711 // Should we squeeze or not? Arbitrary heuristic: we squeeze if
4712 // the number of words we have to shift down is less than the
4713 // number of stack words we squeeze away by doing so.
4714 if (RtsFlags.GcFlags.squeezeUpdFrames == rtsTrue &&
4715 weight < words_to_squeeze) {
4716 stackSqueeze(tso, (StgPtr)frame);
4720 /* -----------------------------------------------------------------------------
4722 * -------------------------------------------------------------------------- */
4726 printMutableList(generation *gen)
4731 debugBelch("mutable list %p: ", gen->mut_list);
4733 for (bd = gen->mut_list; bd != NULL; bd = bd->link) {
4734 for (p = bd->start; p < bd->free; p++) {
4735 debugBelch("%p (%s), ", (void *)*p, info_type((StgClosure *)*p));