1 /* -----------------------------------------------------------------------------
2 * $Id: GC.c,v 1.141 2002/09/10 10:43:52 simonmar Exp $
4 * (c) The GHC Team 1998-1999
6 * Generational garbage collector
8 * ---------------------------------------------------------------------------*/
10 #include "PosixSource.h"
15 #include "StoragePriv.h"
18 #include "SchedAPI.h" // for ReverCAFs prototype
20 #include "BlockAlloc.h"
26 #include "StablePriv.h"
28 #include "ParTicky.h" // ToDo: move into Rts.h
29 #include "GCCompact.h"
30 #if defined(GRAN) || defined(PAR)
31 # include "GranSimRts.h"
32 # include "ParallelRts.h"
36 # include "ParallelDebug.h"
41 #if defined(RTS_GTK_FRONTPANEL)
42 #include "FrontPanel.h"
45 #include "RetainerProfile.h"
46 #include "LdvProfile.h"
50 /* STATIC OBJECT LIST.
53 * We maintain a linked list of static objects that are still live.
54 * The requirements for this list are:
56 * - we need to scan the list while adding to it, in order to
57 * scavenge all the static objects (in the same way that
58 * breadth-first scavenging works for dynamic objects).
60 * - we need to be able to tell whether an object is already on
61 * the list, to break loops.
63 * Each static object has a "static link field", which we use for
64 * linking objects on to the list. We use a stack-type list, consing
65 * objects on the front as they are added (this means that the
66 * scavenge phase is depth-first, not breadth-first, but that
69 * A separate list is kept for objects that have been scavenged
70 * already - this is so that we can zero all the marks afterwards.
72 * An object is on the list if its static link field is non-zero; this
73 * means that we have to mark the end of the list with '1', not NULL.
75 * Extra notes for generational GC:
77 * Each generation has a static object list associated with it. When
78 * collecting generations up to N, we treat the static object lists
79 * from generations > N as roots.
81 * We build up a static object list while collecting generations 0..N,
82 * which is then appended to the static object list of generation N+1.
84 static StgClosure* static_objects; // live static objects
85 StgClosure* scavenged_static_objects; // static objects scavenged so far
87 /* N is the oldest generation being collected, where the generations
88 * are numbered starting at 0. A major GC (indicated by the major_gc
89 * flag) is when we're collecting all generations. We only attempt to
90 * deal with static objects and GC CAFs when doing a major GC.
93 static rtsBool major_gc;
95 /* Youngest generation that objects should be evacuated to in
96 * evacuate(). (Logically an argument to evacuate, but it's static
97 * a lot of the time so we optimise it into a global variable).
103 StgWeak *old_weak_ptr_list; // also pending finaliser list
105 /* Which stage of processing various kinds of weak pointer are we at?
106 * (see traverse_weak_ptr_list() below for discussion).
108 typedef enum { WeakPtrs, WeakThreads, WeakDone } WeakStage;
109 static WeakStage weak_stage;
111 /* List of all threads during GC
113 static StgTSO *old_all_threads;
114 StgTSO *resurrected_threads;
116 /* Flag indicating failure to evacuate an object to the desired
119 static rtsBool failed_to_evac;
121 /* Old to-space (used for two-space collector only)
123 static bdescr *old_to_blocks;
125 /* Data used for allocation area sizing.
127 static lnat new_blocks; // blocks allocated during this GC
128 static lnat g0s0_pcnt_kept = 30; // percentage of g0s0 live at last minor GC
130 /* Used to avoid long recursion due to selector thunks
132 static lnat thunk_selector_depth = 0;
133 #define MAX_THUNK_SELECTOR_DEPTH 8
135 /* -----------------------------------------------------------------------------
136 Static function declarations
137 -------------------------------------------------------------------------- */
139 static void mark_root ( StgClosure **root );
140 static StgClosure * evacuate ( StgClosure *q );
141 static void zero_static_object_list ( StgClosure* first_static );
142 static void zero_mutable_list ( StgMutClosure *first );
144 static rtsBool traverse_weak_ptr_list ( void );
145 static void mark_weak_ptr_list ( StgWeak **list );
147 static StgClosure * eval_thunk_selector ( nat field, StgSelector * p );
149 static void scavenge ( step * );
150 static void scavenge_mark_stack ( void );
151 static void scavenge_stack ( StgPtr p, StgPtr stack_end );
152 static rtsBool scavenge_one ( StgPtr p );
153 static void scavenge_large ( step * );
154 static void scavenge_static ( void );
155 static void scavenge_mutable_list ( generation *g );
156 static void scavenge_mut_once_list ( generation *g );
158 #if 0 && defined(DEBUG)
159 static void gcCAFs ( void );
162 /* -----------------------------------------------------------------------------
163 inline functions etc. for dealing with the mark bitmap & stack.
164 -------------------------------------------------------------------------- */
166 #define MARK_STACK_BLOCKS 4
168 static bdescr *mark_stack_bdescr;
169 static StgPtr *mark_stack;
170 static StgPtr *mark_sp;
171 static StgPtr *mark_splim;
173 // Flag and pointers used for falling back to a linear scan when the
174 // mark stack overflows.
175 static rtsBool mark_stack_overflowed;
176 static bdescr *oldgen_scan_bd;
177 static StgPtr oldgen_scan;
179 static inline rtsBool
180 mark_stack_empty(void)
182 return mark_sp == mark_stack;
185 static inline rtsBool
186 mark_stack_full(void)
188 return mark_sp >= mark_splim;
192 reset_mark_stack(void)
194 mark_sp = mark_stack;
198 push_mark_stack(StgPtr p)
209 /* -----------------------------------------------------------------------------
212 For garbage collecting generation N (and all younger generations):
214 - follow all pointers in the root set. the root set includes all
215 mutable objects in all steps in all generations.
217 - for each pointer, evacuate the object it points to into either
218 + to-space in the next higher step in that generation, if one exists,
219 + if the object's generation == N, then evacuate it to the next
220 generation if one exists, or else to-space in the current
222 + if the object's generation < N, then evacuate it to to-space
223 in the next generation.
225 - repeatedly scavenge to-space from each step in each generation
226 being collected until no more objects can be evacuated.
228 - free from-space in each step, and set from-space = to-space.
230 Locks held: sched_mutex
232 -------------------------------------------------------------------------- */
235 GarbageCollect ( void (*get_roots)(evac_fn), rtsBool force_major_gc )
239 lnat live, allocated, collected = 0, copied = 0;
240 lnat oldgen_saved_blocks = 0;
244 CostCentreStack *prev_CCS;
247 #if defined(DEBUG) && defined(GRAN)
248 IF_DEBUG(gc, belch("@@ Starting garbage collection at %ld (%lx)\n",
252 // tell the stats department that we've started a GC
255 // Init stats and print par specific (timing) info
256 PAR_TICKY_PAR_START();
258 // attribute any costs to CCS_GC
264 /* Approximate how much we allocated.
265 * Todo: only when generating stats?
267 allocated = calcAllocated();
269 /* Figure out which generation to collect
271 if (force_major_gc) {
272 N = RtsFlags.GcFlags.generations - 1;
276 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
277 if (generations[g].steps[0].n_blocks +
278 generations[g].steps[0].n_large_blocks
279 >= generations[g].max_blocks) {
283 major_gc = (N == RtsFlags.GcFlags.generations-1);
286 #ifdef RTS_GTK_FRONTPANEL
287 if (RtsFlags.GcFlags.frontpanel) {
288 updateFrontPanelBeforeGC(N);
292 // check stack sanity *before* GC (ToDo: check all threads)
294 // ToDo!: check sanity IF_DEBUG(sanity, checkTSOsSanity());
296 IF_DEBUG(sanity, checkFreeListSanity());
298 /* Initialise the static object lists
300 static_objects = END_OF_STATIC_LIST;
301 scavenged_static_objects = END_OF_STATIC_LIST;
303 /* zero the mutable list for the oldest generation (see comment by
304 * zero_mutable_list below).
307 zero_mutable_list(generations[RtsFlags.GcFlags.generations-1].mut_once_list);
310 /* Save the old to-space if we're doing a two-space collection
312 if (RtsFlags.GcFlags.generations == 1) {
313 old_to_blocks = g0s0->to_blocks;
314 g0s0->to_blocks = NULL;
317 /* Keep a count of how many new blocks we allocated during this GC
318 * (used for resizing the allocation area, later).
322 /* Initialise to-space in all the generations/steps that we're
325 for (g = 0; g <= N; g++) {
326 generations[g].mut_once_list = END_MUT_LIST;
327 generations[g].mut_list = END_MUT_LIST;
329 for (s = 0; s < generations[g].n_steps; s++) {
331 // generation 0, step 0 doesn't need to-space
332 if (g == 0 && s == 0 && RtsFlags.GcFlags.generations > 1) {
336 /* Get a free block for to-space. Extra blocks will be chained on
340 stp = &generations[g].steps[s];
341 ASSERT(stp->gen_no == g);
342 ASSERT(stp->hp ? Bdescr(stp->hp)->step == stp : rtsTrue);
346 bd->flags = BF_EVACUATED; // it's a to-space block
348 stp->hpLim = stp->hp + BLOCK_SIZE_W;
351 stp->n_to_blocks = 1;
352 stp->scan = bd->start;
354 stp->new_large_objects = NULL;
355 stp->scavenged_large_objects = NULL;
356 stp->n_scavenged_large_blocks = 0;
358 // mark the large objects as not evacuated yet
359 for (bd = stp->large_objects; bd; bd = bd->link) {
360 bd->flags = BF_LARGE;
363 // for a compacted step, we need to allocate the bitmap
364 if (stp->is_compacted) {
365 nat bitmap_size; // in bytes
366 bdescr *bitmap_bdescr;
369 bitmap_size = stp->n_blocks * BLOCK_SIZE / (sizeof(W_)*BITS_PER_BYTE);
371 if (bitmap_size > 0) {
372 bitmap_bdescr = allocGroup((nat)BLOCK_ROUND_UP(bitmap_size)
374 stp->bitmap = bitmap_bdescr;
375 bitmap = bitmap_bdescr->start;
377 IF_DEBUG(gc, belch("bitmap_size: %d, bitmap: %p",
378 bitmap_size, bitmap););
380 // don't forget to fill it with zeros!
381 memset(bitmap, 0, bitmap_size);
383 // for each block in this step, point to its bitmap from the
385 for (bd=stp->blocks; bd != NULL; bd = bd->link) {
386 bd->u.bitmap = bitmap;
387 bitmap += BLOCK_SIZE_W / (sizeof(W_)*BITS_PER_BYTE);
394 /* make sure the older generations have at least one block to
395 * allocate into (this makes things easier for copy(), see below.
397 for (g = N+1; g < RtsFlags.GcFlags.generations; g++) {
398 for (s = 0; s < generations[g].n_steps; s++) {
399 stp = &generations[g].steps[s];
400 if (stp->hp_bd == NULL) {
401 ASSERT(stp->blocks == NULL);
406 bd->flags = 0; // *not* a to-space block or a large object
408 stp->hpLim = stp->hp + BLOCK_SIZE_W;
414 /* Set the scan pointer for older generations: remember we
415 * still have to scavenge objects that have been promoted. */
417 stp->scan_bd = stp->hp_bd;
418 stp->to_blocks = NULL;
419 stp->n_to_blocks = 0;
420 stp->new_large_objects = NULL;
421 stp->scavenged_large_objects = NULL;
422 stp->n_scavenged_large_blocks = 0;
426 /* Allocate a mark stack if we're doing a major collection.
429 mark_stack_bdescr = allocGroup(MARK_STACK_BLOCKS);
430 mark_stack = (StgPtr *)mark_stack_bdescr->start;
431 mark_sp = mark_stack;
432 mark_splim = mark_stack + (MARK_STACK_BLOCKS * BLOCK_SIZE_W);
434 mark_stack_bdescr = NULL;
437 /* -----------------------------------------------------------------------
438 * follow all the roots that we know about:
439 * - mutable lists from each generation > N
440 * we want to *scavenge* these roots, not evacuate them: they're not
441 * going to move in this GC.
442 * Also: do them in reverse generation order. This is because we
443 * often want to promote objects that are pointed to by older
444 * generations early, so we don't have to repeatedly copy them.
445 * Doing the generations in reverse order ensures that we don't end
446 * up in the situation where we want to evac an object to gen 3 and
447 * it has already been evaced to gen 2.
451 for (g = RtsFlags.GcFlags.generations-1; g > N; g--) {
452 generations[g].saved_mut_list = generations[g].mut_list;
453 generations[g].mut_list = END_MUT_LIST;
456 // Do the mut-once lists first
457 for (g = RtsFlags.GcFlags.generations-1; g > N; g--) {
458 IF_PAR_DEBUG(verbose,
459 printMutOnceList(&generations[g]));
460 scavenge_mut_once_list(&generations[g]);
462 for (st = generations[g].n_steps-1; st >= 0; st--) {
463 scavenge(&generations[g].steps[st]);
467 for (g = RtsFlags.GcFlags.generations-1; g > N; g--) {
468 IF_PAR_DEBUG(verbose,
469 printMutableList(&generations[g]));
470 scavenge_mutable_list(&generations[g]);
472 for (st = generations[g].n_steps-1; st >= 0; st--) {
473 scavenge(&generations[g].steps[st]);
478 /* follow roots from the CAF list (used by GHCi)
483 /* follow all the roots that the application knows about.
486 get_roots(mark_root);
489 /* And don't forget to mark the TSO if we got here direct from
491 /* Not needed in a seq version?
493 CurrentTSO = (StgTSO *)MarkRoot((StgClosure *)CurrentTSO);
497 // Mark the entries in the GALA table of the parallel system
498 markLocalGAs(major_gc);
499 // Mark all entries on the list of pending fetches
500 markPendingFetches(major_gc);
503 /* Mark the weak pointer list, and prepare to detect dead weak
506 mark_weak_ptr_list(&weak_ptr_list);
507 old_weak_ptr_list = weak_ptr_list;
508 weak_ptr_list = NULL;
509 weak_stage = WeakPtrs;
511 /* The all_threads list is like the weak_ptr_list.
512 * See traverse_weak_ptr_list() for the details.
514 old_all_threads = all_threads;
515 all_threads = END_TSO_QUEUE;
516 resurrected_threads = END_TSO_QUEUE;
518 /* Mark the stable pointer table.
520 markStablePtrTable(mark_root);
524 /* ToDo: To fix the caf leak, we need to make the commented out
525 * parts of this code do something sensible - as described in
528 extern void markHugsObjects(void);
533 /* -------------------------------------------------------------------------
534 * Repeatedly scavenge all the areas we know about until there's no
535 * more scavenging to be done.
542 // scavenge static objects
543 if (major_gc && static_objects != END_OF_STATIC_LIST) {
544 IF_DEBUG(sanity, checkStaticObjects(static_objects));
548 /* When scavenging the older generations: Objects may have been
549 * evacuated from generations <= N into older generations, and we
550 * need to scavenge these objects. We're going to try to ensure that
551 * any evacuations that occur move the objects into at least the
552 * same generation as the object being scavenged, otherwise we
553 * have to create new entries on the mutable list for the older
557 // scavenge each step in generations 0..maxgen
563 // scavenge objects in compacted generation
564 if (mark_stack_overflowed || oldgen_scan_bd != NULL ||
565 (mark_stack_bdescr != NULL && !mark_stack_empty())) {
566 scavenge_mark_stack();
570 for (gen = RtsFlags.GcFlags.generations; --gen >= 0; ) {
571 for (st = generations[gen].n_steps; --st >= 0; ) {
572 if (gen == 0 && st == 0 && RtsFlags.GcFlags.generations > 1) {
575 stp = &generations[gen].steps[st];
577 if (stp->hp_bd != stp->scan_bd || stp->scan < stp->hp) {
582 if (stp->new_large_objects != NULL) {
591 if (flag) { goto loop; }
593 // must be last... invariant is that everything is fully
594 // scavenged at this point.
595 if (traverse_weak_ptr_list()) { // returns rtsTrue if evaced something
600 /* Update the pointers from the "main thread" list - these are
601 * treated as weak pointers because we want to allow a main thread
602 * to get a BlockedOnDeadMVar exception in the same way as any other
603 * thread. Note that the threads should all have been retained by
604 * GC by virtue of being on the all_threads list, we're just
605 * updating pointers here.
610 for (m = main_threads; m != NULL; m = m->link) {
611 tso = (StgTSO *) isAlive((StgClosure *)m->tso);
613 barf("main thread has been GC'd");
620 // Reconstruct the Global Address tables used in GUM
621 rebuildGAtables(major_gc);
622 IF_DEBUG(sanity, checkLAGAtable(rtsTrue/*check closures, too*/));
625 // Now see which stable names are still alive.
628 // Tidy the end of the to-space chains
629 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
630 for (s = 0; s < generations[g].n_steps; s++) {
631 stp = &generations[g].steps[s];
632 if (!(g == 0 && s == 0 && RtsFlags.GcFlags.generations > 1)) {
633 stp->hp_bd->free = stp->hp;
634 stp->hp_bd->link = NULL;
640 // We call processHeapClosureForDead() on every closure destroyed during
641 // the current garbage collection, so we invoke LdvCensusForDead().
642 if (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_LDV
643 || RtsFlags.ProfFlags.bioSelector != NULL)
647 // NO MORE EVACUATION AFTER THIS POINT!
648 // Finally: compaction of the oldest generation.
649 if (major_gc && oldest_gen->steps[0].is_compacted) {
650 // save number of blocks for stats
651 oldgen_saved_blocks = oldest_gen->steps[0].n_blocks;
655 IF_DEBUG(sanity, checkGlobalTSOList(rtsFalse));
657 /* run through all the generations/steps and tidy up
659 copied = new_blocks * BLOCK_SIZE_W;
660 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
663 generations[g].collections++; // for stats
666 for (s = 0; s < generations[g].n_steps; s++) {
668 stp = &generations[g].steps[s];
670 if (!(g == 0 && s == 0 && RtsFlags.GcFlags.generations > 1)) {
671 // stats information: how much we copied
673 copied -= stp->hp_bd->start + BLOCK_SIZE_W -
678 // for generations we collected...
681 // rough calculation of garbage collected, for stats output
682 if (stp->is_compacted) {
683 collected += (oldgen_saved_blocks - stp->n_blocks) * BLOCK_SIZE_W;
685 collected += stp->n_blocks * BLOCK_SIZE_W;
688 /* free old memory and shift to-space into from-space for all
689 * the collected steps (except the allocation area). These
690 * freed blocks will probaby be quickly recycled.
692 if (!(g == 0 && s == 0)) {
693 if (stp->is_compacted) {
694 // for a compacted step, just shift the new to-space
695 // onto the front of the now-compacted existing blocks.
696 for (bd = stp->to_blocks; bd != NULL; bd = bd->link) {
697 bd->flags &= ~BF_EVACUATED; // now from-space
699 // tack the new blocks on the end of the existing blocks
700 if (stp->blocks == NULL) {
701 stp->blocks = stp->to_blocks;
703 for (bd = stp->blocks; bd != NULL; bd = next) {
706 bd->link = stp->to_blocks;
710 // add the new blocks to the block tally
711 stp->n_blocks += stp->n_to_blocks;
713 freeChain(stp->blocks);
714 stp->blocks = stp->to_blocks;
715 stp->n_blocks = stp->n_to_blocks;
716 for (bd = stp->blocks; bd != NULL; bd = bd->link) {
717 bd->flags &= ~BF_EVACUATED; // now from-space
720 stp->to_blocks = NULL;
721 stp->n_to_blocks = 0;
724 /* LARGE OBJECTS. The current live large objects are chained on
725 * scavenged_large, having been moved during garbage
726 * collection from large_objects. Any objects left on
727 * large_objects list are therefore dead, so we free them here.
729 for (bd = stp->large_objects; bd != NULL; bd = next) {
735 // update the count of blocks used by large objects
736 for (bd = stp->scavenged_large_objects; bd != NULL; bd = bd->link) {
737 bd->flags &= ~BF_EVACUATED;
739 stp->large_objects = stp->scavenged_large_objects;
740 stp->n_large_blocks = stp->n_scavenged_large_blocks;
743 // for older generations...
745 /* For older generations, we need to append the
746 * scavenged_large_object list (i.e. large objects that have been
747 * promoted during this GC) to the large_object list for that step.
749 for (bd = stp->scavenged_large_objects; bd; bd = next) {
751 bd->flags &= ~BF_EVACUATED;
752 dbl_link_onto(bd, &stp->large_objects);
755 // add the new blocks we promoted during this GC
756 stp->n_blocks += stp->n_to_blocks;
757 stp->n_large_blocks += stp->n_scavenged_large_blocks;
762 /* Reset the sizes of the older generations when we do a major
765 * CURRENT STRATEGY: make all generations except zero the same size.
766 * We have to stay within the maximum heap size, and leave a certain
767 * percentage of the maximum heap size available to allocate into.
769 if (major_gc && RtsFlags.GcFlags.generations > 1) {
770 nat live, size, min_alloc;
771 nat max = RtsFlags.GcFlags.maxHeapSize;
772 nat gens = RtsFlags.GcFlags.generations;
774 // live in the oldest generations
775 live = oldest_gen->steps[0].n_blocks +
776 oldest_gen->steps[0].n_large_blocks;
778 // default max size for all generations except zero
779 size = stg_max(live * RtsFlags.GcFlags.oldGenFactor,
780 RtsFlags.GcFlags.minOldGenSize);
782 // minimum size for generation zero
783 min_alloc = stg_max((RtsFlags.GcFlags.pcFreeHeap * max) / 200,
784 RtsFlags.GcFlags.minAllocAreaSize);
786 // Auto-enable compaction when the residency reaches a
787 // certain percentage of the maximum heap size (default: 30%).
788 if (RtsFlags.GcFlags.generations > 1 &&
789 (RtsFlags.GcFlags.compact ||
791 oldest_gen->steps[0].n_blocks >
792 (RtsFlags.GcFlags.compactThreshold * max) / 100))) {
793 oldest_gen->steps[0].is_compacted = 1;
794 // fprintf(stderr,"compaction: on\n", live);
796 oldest_gen->steps[0].is_compacted = 0;
797 // fprintf(stderr,"compaction: off\n", live);
800 // if we're going to go over the maximum heap size, reduce the
801 // size of the generations accordingly. The calculation is
802 // different if compaction is turned on, because we don't need
803 // to double the space required to collect the old generation.
806 // this test is necessary to ensure that the calculations
807 // below don't have any negative results - we're working
808 // with unsigned values here.
809 if (max < min_alloc) {
813 if (oldest_gen->steps[0].is_compacted) {
814 if ( (size + (size - 1) * (gens - 2) * 2) + min_alloc > max ) {
815 size = (max - min_alloc) / ((gens - 1) * 2 - 1);
818 if ( (size * (gens - 1) * 2) + min_alloc > max ) {
819 size = (max - min_alloc) / ((gens - 1) * 2);
829 fprintf(stderr,"live: %d, min_alloc: %d, size : %d, max = %d\n", live,
830 min_alloc, size, max);
833 for (g = 0; g < gens; g++) {
834 generations[g].max_blocks = size;
838 // Guess the amount of live data for stats.
841 /* Free the small objects allocated via allocate(), since this will
842 * all have been copied into G0S1 now.
844 if (small_alloc_list != NULL) {
845 freeChain(small_alloc_list);
847 small_alloc_list = NULL;
851 alloc_blocks_lim = RtsFlags.GcFlags.minAllocAreaSize;
853 // Start a new pinned_object_block
854 pinned_object_block = NULL;
856 /* Free the mark stack.
858 if (mark_stack_bdescr != NULL) {
859 freeGroup(mark_stack_bdescr);
864 for (g = 0; g <= N; g++) {
865 for (s = 0; s < generations[g].n_steps; s++) {
866 stp = &generations[g].steps[s];
867 if (stp->is_compacted && stp->bitmap != NULL) {
868 freeGroup(stp->bitmap);
873 /* Two-space collector:
874 * Free the old to-space, and estimate the amount of live data.
876 if (RtsFlags.GcFlags.generations == 1) {
879 if (old_to_blocks != NULL) {
880 freeChain(old_to_blocks);
882 for (bd = g0s0->to_blocks; bd != NULL; bd = bd->link) {
883 bd->flags = 0; // now from-space
886 /* For a two-space collector, we need to resize the nursery. */
888 /* set up a new nursery. Allocate a nursery size based on a
889 * function of the amount of live data (by default a factor of 2)
890 * Use the blocks from the old nursery if possible, freeing up any
893 * If we get near the maximum heap size, then adjust our nursery
894 * size accordingly. If the nursery is the same size as the live
895 * data (L), then we need 3L bytes. We can reduce the size of the
896 * nursery to bring the required memory down near 2L bytes.
898 * A normal 2-space collector would need 4L bytes to give the same
899 * performance we get from 3L bytes, reducing to the same
900 * performance at 2L bytes.
902 blocks = g0s0->n_to_blocks;
904 if ( RtsFlags.GcFlags.maxHeapSize != 0 &&
905 blocks * RtsFlags.GcFlags.oldGenFactor * 2 >
906 RtsFlags.GcFlags.maxHeapSize ) {
907 long adjusted_blocks; // signed on purpose
910 adjusted_blocks = (RtsFlags.GcFlags.maxHeapSize - 2 * blocks);
911 IF_DEBUG(gc, belch("@@ Near maximum heap size of 0x%x blocks, blocks = %d, adjusted to %ld", RtsFlags.GcFlags.maxHeapSize, blocks, adjusted_blocks));
912 pc_free = adjusted_blocks * 100 / RtsFlags.GcFlags.maxHeapSize;
913 if (pc_free < RtsFlags.GcFlags.pcFreeHeap) /* might even be < 0 */ {
916 blocks = adjusted_blocks;
919 blocks *= RtsFlags.GcFlags.oldGenFactor;
920 if (blocks < RtsFlags.GcFlags.minAllocAreaSize) {
921 blocks = RtsFlags.GcFlags.minAllocAreaSize;
924 resizeNursery(blocks);
927 /* Generational collector:
928 * If the user has given us a suggested heap size, adjust our
929 * allocation area to make best use of the memory available.
932 if (RtsFlags.GcFlags.heapSizeSuggestion) {
934 nat needed = calcNeeded(); // approx blocks needed at next GC
936 /* Guess how much will be live in generation 0 step 0 next time.
937 * A good approximation is obtained by finding the
938 * percentage of g0s0 that was live at the last minor GC.
941 g0s0_pcnt_kept = (new_blocks * 100) / g0s0->n_blocks;
944 /* Estimate a size for the allocation area based on the
945 * information available. We might end up going slightly under
946 * or over the suggested heap size, but we should be pretty
949 * Formula: suggested - needed
950 * ----------------------------
951 * 1 + g0s0_pcnt_kept/100
953 * where 'needed' is the amount of memory needed at the next
954 * collection for collecting all steps except g0s0.
957 (((long)RtsFlags.GcFlags.heapSizeSuggestion - (long)needed) * 100) /
958 (100 + (long)g0s0_pcnt_kept);
960 if (blocks < (long)RtsFlags.GcFlags.minAllocAreaSize) {
961 blocks = RtsFlags.GcFlags.minAllocAreaSize;
964 resizeNursery((nat)blocks);
967 // we might have added extra large blocks to the nursery, so
968 // resize back to minAllocAreaSize again.
969 resizeNursery(RtsFlags.GcFlags.minAllocAreaSize);
973 // mark the garbage collected CAFs as dead
974 #if 0 && defined(DEBUG) // doesn't work at the moment
975 if (major_gc) { gcCAFs(); }
979 // resetStaticObjectForRetainerProfiling() must be called before
981 resetStaticObjectForRetainerProfiling();
984 // zero the scavenged static object list
986 zero_static_object_list(scavenged_static_objects);
992 RELEASE_LOCK(&sched_mutex);
994 // start any pending finalizers
995 scheduleFinalizers(old_weak_ptr_list);
997 // send exceptions to any threads which were about to die
998 resurrectThreads(resurrected_threads);
1000 ACQUIRE_LOCK(&sched_mutex);
1002 // Update the stable pointer hash table.
1003 updateStablePtrTable(major_gc);
1005 // check sanity after GC
1006 IF_DEBUG(sanity, checkSanity());
1008 // extra GC trace info
1009 IF_DEBUG(gc, statDescribeGens());
1012 // symbol-table based profiling
1013 /* heapCensus(to_blocks); */ /* ToDo */
1016 // restore enclosing cost centre
1021 // check for memory leaks if sanity checking is on
1022 IF_DEBUG(sanity, memInventory());
1024 #ifdef RTS_GTK_FRONTPANEL
1025 if (RtsFlags.GcFlags.frontpanel) {
1026 updateFrontPanelAfterGC( N, live );
1030 // ok, GC over: tell the stats department what happened.
1031 stat_endGC(allocated, collected, live, copied, N);
1037 /* -----------------------------------------------------------------------------
1040 traverse_weak_ptr_list is called possibly many times during garbage
1041 collection. It returns a flag indicating whether it did any work
1042 (i.e. called evacuate on any live pointers).
1044 Invariant: traverse_weak_ptr_list is called when the heap is in an
1045 idempotent state. That means that there are no pending
1046 evacuate/scavenge operations. This invariant helps the weak
1047 pointer code decide which weak pointers are dead - if there are no
1048 new live weak pointers, then all the currently unreachable ones are
1051 For generational GC: we just don't try to finalize weak pointers in
1052 older generations than the one we're collecting. This could
1053 probably be optimised by keeping per-generation lists of weak
1054 pointers, but for a few weak pointers this scheme will work.
1056 There are three distinct stages to processing weak pointers:
1058 - weak_stage == WeakPtrs
1060 We process all the weak pointers whos keys are alive (evacuate
1061 their values and finalizers), and repeat until we can find no new
1062 live keys. If no live keys are found in this pass, then we
1063 evacuate the finalizers of all the dead weak pointers in order to
1066 - weak_stage == WeakThreads
1068 Now, we discover which *threads* are still alive. Pointers to
1069 threads from the all_threads and main thread lists are the
1070 weakest of all: a pointers from the finalizer of a dead weak
1071 pointer can keep a thread alive. Any threads found to be unreachable
1072 are evacuated and placed on the resurrected_threads list so we
1073 can send them a signal later.
1075 - weak_stage == WeakDone
1077 No more evacuation is done.
1079 -------------------------------------------------------------------------- */
1082 traverse_weak_ptr_list(void)
1084 StgWeak *w, **last_w, *next_w;
1086 rtsBool flag = rtsFalse;
1088 switch (weak_stage) {
1094 /* doesn't matter where we evacuate values/finalizers to, since
1095 * these pointers are treated as roots (iff the keys are alive).
1099 last_w = &old_weak_ptr_list;
1100 for (w = old_weak_ptr_list; w != NULL; w = next_w) {
1102 /* There might be a DEAD_WEAK on the list if finalizeWeak# was
1103 * called on a live weak pointer object. Just remove it.
1105 if (w->header.info == &stg_DEAD_WEAK_info) {
1106 next_w = ((StgDeadWeak *)w)->link;
1111 switch (get_itbl(w)->type) {
1114 next_w = (StgWeak *)((StgEvacuated *)w)->evacuee;
1119 /* Now, check whether the key is reachable.
1121 new = isAlive(w->key);
1124 // evacuate the value and finalizer
1125 w->value = evacuate(w->value);
1126 w->finalizer = evacuate(w->finalizer);
1127 // remove this weak ptr from the old_weak_ptr list
1129 // and put it on the new weak ptr list
1131 w->link = weak_ptr_list;
1134 IF_DEBUG(weak, belch("Weak pointer still alive at %p -> %p",
1139 last_w = &(w->link);
1145 barf("traverse_weak_ptr_list: not WEAK");
1149 /* If we didn't make any changes, then we can go round and kill all
1150 * the dead weak pointers. The old_weak_ptr list is used as a list
1151 * of pending finalizers later on.
1153 if (flag == rtsFalse) {
1154 for (w = old_weak_ptr_list; w; w = w->link) {
1155 w->finalizer = evacuate(w->finalizer);
1158 // Next, move to the WeakThreads stage after fully
1159 // scavenging the finalizers we've just evacuated.
1160 weak_stage = WeakThreads;
1166 /* Now deal with the all_threads list, which behaves somewhat like
1167 * the weak ptr list. If we discover any threads that are about to
1168 * become garbage, we wake them up and administer an exception.
1171 StgTSO *t, *tmp, *next, **prev;
1173 prev = &old_all_threads;
1174 for (t = old_all_threads; t != END_TSO_QUEUE; t = next) {
1176 (StgClosure *)tmp = isAlive((StgClosure *)t);
1182 ASSERT(get_itbl(t)->type == TSO);
1183 switch (t->what_next) {
1184 case ThreadRelocated:
1189 case ThreadComplete:
1190 // finshed or died. The thread might still be alive, but we
1191 // don't keep it on the all_threads list. Don't forget to
1192 // stub out its global_link field.
1193 next = t->global_link;
1194 t->global_link = END_TSO_QUEUE;
1202 // not alive (yet): leave this thread on the
1203 // old_all_threads list.
1204 prev = &(t->global_link);
1205 next = t->global_link;
1208 // alive: move this thread onto the all_threads list.
1209 next = t->global_link;
1210 t->global_link = all_threads;
1217 /* And resurrect any threads which were about to become garbage.
1220 StgTSO *t, *tmp, *next;
1221 for (t = old_all_threads; t != END_TSO_QUEUE; t = next) {
1222 next = t->global_link;
1223 (StgClosure *)tmp = evacuate((StgClosure *)t);
1224 tmp->global_link = resurrected_threads;
1225 resurrected_threads = tmp;
1229 weak_stage = WeakDone; // *now* we're done,
1230 return rtsTrue; // but one more round of scavenging, please
1233 barf("traverse_weak_ptr_list");
1238 /* -----------------------------------------------------------------------------
1239 After GC, the live weak pointer list may have forwarding pointers
1240 on it, because a weak pointer object was evacuated after being
1241 moved to the live weak pointer list. We remove those forwarding
1244 Also, we don't consider weak pointer objects to be reachable, but
1245 we must nevertheless consider them to be "live" and retain them.
1246 Therefore any weak pointer objects which haven't as yet been
1247 evacuated need to be evacuated now.
1248 -------------------------------------------------------------------------- */
1252 mark_weak_ptr_list ( StgWeak **list )
1254 StgWeak *w, **last_w;
1257 for (w = *list; w; w = w->link) {
1258 // w might be WEAK, EVACUATED, or DEAD_WEAK (actually CON_STATIC) here
1259 ASSERT(w->header.info == &stg_DEAD_WEAK_info
1260 || get_itbl(w)->type == WEAK || get_itbl(w)->type == EVACUATED);
1261 (StgClosure *)w = evacuate((StgClosure *)w);
1263 last_w = &(w->link);
1267 /* -----------------------------------------------------------------------------
1268 isAlive determines whether the given closure is still alive (after
1269 a garbage collection) or not. It returns the new address of the
1270 closure if it is alive, or NULL otherwise.
1272 NOTE: Use it before compaction only!
1273 -------------------------------------------------------------------------- */
1277 isAlive(StgClosure *p)
1279 const StgInfoTable *info;
1286 /* ToDo: for static closures, check the static link field.
1287 * Problem here is that we sometimes don't set the link field, eg.
1288 * for static closures with an empty SRT or CONSTR_STATIC_NOCAFs.
1294 // ignore closures in generations that we're not collecting.
1295 if (LOOKS_LIKE_STATIC(p) || bd->gen_no > N) {
1298 // large objects have an evacuated flag
1299 if (bd->flags & BF_LARGE) {
1300 if (bd->flags & BF_EVACUATED) {
1306 // check the mark bit for compacted steps
1307 if (bd->step->is_compacted && is_marked((P_)p,bd)) {
1311 switch (info->type) {
1316 case IND_OLDGEN: // rely on compatible layout with StgInd
1317 case IND_OLDGEN_PERM:
1318 // follow indirections
1319 p = ((StgInd *)p)->indirectee;
1324 return ((StgEvacuated *)p)->evacuee;
1327 if (((StgTSO *)p)->what_next == ThreadRelocated) {
1328 p = (StgClosure *)((StgTSO *)p)->link;
1340 mark_root(StgClosure **root)
1342 *root = evacuate(*root);
1348 bdescr *bd = allocBlock();
1349 bd->gen_no = stp->gen_no;
1352 if (stp->gen_no <= N) {
1353 bd->flags = BF_EVACUATED;
1358 stp->hp_bd->free = stp->hp;
1359 stp->hp_bd->link = bd;
1360 stp->hp = bd->start;
1361 stp->hpLim = stp->hp + BLOCK_SIZE_W;
1368 static __inline__ void
1369 upd_evacuee(StgClosure *p, StgClosure *dest)
1371 p->header.info = &stg_EVACUATED_info;
1372 ((StgEvacuated *)p)->evacuee = dest;
1376 static __inline__ StgClosure *
1377 copy(StgClosure *src, nat size, step *stp)
1382 nat size_org = size;
1385 TICK_GC_WORDS_COPIED(size);
1386 /* Find out where we're going, using the handy "to" pointer in
1387 * the step of the source object. If it turns out we need to
1388 * evacuate to an older generation, adjust it here (see comment
1391 if (stp->gen_no < evac_gen) {
1392 #ifdef NO_EAGER_PROMOTION
1393 failed_to_evac = rtsTrue;
1395 stp = &generations[evac_gen].steps[0];
1399 /* chain a new block onto the to-space for the destination step if
1402 if (stp->hp + size >= stp->hpLim) {
1406 for(to = stp->hp, from = (P_)src; size>0; --size) {
1412 upd_evacuee(src,(StgClosure *)dest);
1414 // We store the size of the just evacuated object in the LDV word so that
1415 // the profiler can guess the position of the next object later.
1416 SET_EVACUAEE_FOR_LDV(src, size_org);
1418 return (StgClosure *)dest;
1421 /* Special version of copy() for when we only want to copy the info
1422 * pointer of an object, but reserve some padding after it. This is
1423 * used to optimise evacuation of BLACKHOLEs.
1428 copyPart(StgClosure *src, nat size_to_reserve, nat size_to_copy, step *stp)
1433 nat size_to_copy_org = size_to_copy;
1436 TICK_GC_WORDS_COPIED(size_to_copy);
1437 if (stp->gen_no < evac_gen) {
1438 #ifdef NO_EAGER_PROMOTION
1439 failed_to_evac = rtsTrue;
1441 stp = &generations[evac_gen].steps[0];
1445 if (stp->hp + size_to_reserve >= stp->hpLim) {
1449 for(to = stp->hp, from = (P_)src; size_to_copy>0; --size_to_copy) {
1454 stp->hp += size_to_reserve;
1455 upd_evacuee(src,(StgClosure *)dest);
1457 // We store the size of the just evacuated object in the LDV word so that
1458 // the profiler can guess the position of the next object later.
1459 // size_to_copy_org is wrong because the closure already occupies size_to_reserve
1461 SET_EVACUAEE_FOR_LDV(src, size_to_reserve);
1463 if (size_to_reserve - size_to_copy_org > 0)
1464 FILL_SLOP(stp->hp - 1, (int)(size_to_reserve - size_to_copy_org));
1466 return (StgClosure *)dest;
1470 /* -----------------------------------------------------------------------------
1471 Evacuate a large object
1473 This just consists of removing the object from the (doubly-linked)
1474 step->large_objects list, and linking it on to the (singly-linked)
1475 step->new_large_objects list, from where it will be scavenged later.
1477 Convention: bd->flags has BF_EVACUATED set for a large object
1478 that has been evacuated, or unset otherwise.
1479 -------------------------------------------------------------------------- */
1483 evacuate_large(StgPtr p)
1485 bdescr *bd = Bdescr(p);
1488 // object must be at the beginning of the block (or be a ByteArray)
1489 ASSERT(get_itbl((StgClosure *)p)->type == ARR_WORDS ||
1490 (((W_)p & BLOCK_MASK) == 0));
1492 // already evacuated?
1493 if (bd->flags & BF_EVACUATED) {
1494 /* Don't forget to set the failed_to_evac flag if we didn't get
1495 * the desired destination (see comments in evacuate()).
1497 if (bd->gen_no < evac_gen) {
1498 failed_to_evac = rtsTrue;
1499 TICK_GC_FAILED_PROMOTION();
1505 // remove from large_object list
1507 bd->u.back->link = bd->link;
1508 } else { // first object in the list
1509 stp->large_objects = bd->link;
1512 bd->link->u.back = bd->u.back;
1515 /* link it on to the evacuated large object list of the destination step
1518 if (stp->gen_no < evac_gen) {
1519 #ifdef NO_EAGER_PROMOTION
1520 failed_to_evac = rtsTrue;
1522 stp = &generations[evac_gen].steps[0];
1527 bd->gen_no = stp->gen_no;
1528 bd->link = stp->new_large_objects;
1529 stp->new_large_objects = bd;
1530 bd->flags |= BF_EVACUATED;
1533 /* -----------------------------------------------------------------------------
1534 Adding a MUT_CONS to an older generation.
1536 This is necessary from time to time when we end up with an
1537 old-to-new generation pointer in a non-mutable object. We defer
1538 the promotion until the next GC.
1539 -------------------------------------------------------------------------- */
1543 mkMutCons(StgClosure *ptr, generation *gen)
1548 stp = &gen->steps[0];
1550 /* chain a new block onto the to-space for the destination step if
1553 if (stp->hp + sizeofW(StgIndOldGen) >= stp->hpLim) {
1557 q = (StgMutVar *)stp->hp;
1558 stp->hp += sizeofW(StgMutVar);
1560 SET_HDR(q,&stg_MUT_CONS_info,CCS_GC);
1562 recordOldToNewPtrs((StgMutClosure *)q);
1564 return (StgClosure *)q;
1567 /* -----------------------------------------------------------------------------
1570 This is called (eventually) for every live object in the system.
1572 The caller to evacuate specifies a desired generation in the
1573 evac_gen global variable. The following conditions apply to
1574 evacuating an object which resides in generation M when we're
1575 collecting up to generation N
1579 else evac to step->to
1581 if M < evac_gen evac to evac_gen, step 0
1583 if the object is already evacuated, then we check which generation
1586 if M >= evac_gen do nothing
1587 if M < evac_gen set failed_to_evac flag to indicate that we
1588 didn't manage to evacuate this object into evac_gen.
1590 -------------------------------------------------------------------------- */
1593 evacuate(StgClosure *q)
1598 const StgInfoTable *info;
1601 if (HEAP_ALLOCED(q)) {
1604 if (bd->gen_no > N) {
1605 /* Can't evacuate this object, because it's in a generation
1606 * older than the ones we're collecting. Let's hope that it's
1607 * in evac_gen or older, or we will have to arrange to track
1608 * this pointer using the mutable list.
1610 if (bd->gen_no < evac_gen) {
1612 failed_to_evac = rtsTrue;
1613 TICK_GC_FAILED_PROMOTION();
1618 /* evacuate large objects by re-linking them onto a different list.
1620 if (bd->flags & BF_LARGE) {
1622 if (info->type == TSO &&
1623 ((StgTSO *)q)->what_next == ThreadRelocated) {
1624 q = (StgClosure *)((StgTSO *)q)->link;
1627 evacuate_large((P_)q);
1631 /* If the object is in a step that we're compacting, then we
1632 * need to use an alternative evacuate procedure.
1634 if (bd->step->is_compacted) {
1635 if (!is_marked((P_)q,bd)) {
1637 if (mark_stack_full()) {
1638 mark_stack_overflowed = rtsTrue;
1641 push_mark_stack((P_)q);
1649 else stp = NULL; // make sure copy() will crash if HEAP_ALLOCED is wrong
1652 // make sure the info pointer is into text space
1653 ASSERT(q && (LOOKS_LIKE_GHC_INFO(GET_INFO(q))
1654 || IS_HUGS_CONSTR_INFO(GET_INFO(q))));
1657 switch (info -> type) {
1661 to = copy(q,sizeW_fromITBL(info),stp);
1666 StgWord w = (StgWord)q->payload[0];
1667 if (q->header.info == Czh_con_info &&
1668 // unsigned, so always true: (StgChar)w >= MIN_CHARLIKE &&
1669 (StgChar)w <= MAX_CHARLIKE) {
1670 return (StgClosure *)CHARLIKE_CLOSURE((StgChar)w);
1672 if (q->header.info == Izh_con_info &&
1673 (StgInt)w >= MIN_INTLIKE && (StgInt)w <= MAX_INTLIKE) {
1674 return (StgClosure *)INTLIKE_CLOSURE((StgInt)w);
1676 // else, fall through ...
1682 return copy(q,sizeofW(StgHeader)+1,stp);
1684 case THUNK_1_0: // here because of MIN_UPD_SIZE
1689 #ifdef NO_PROMOTE_THUNKS
1690 if (bd->gen_no == 0 &&
1691 bd->step->no != 0 &&
1692 bd->step->no == generations[bd->gen_no].n_steps-1) {
1696 return copy(q,sizeofW(StgHeader)+2,stp);
1704 return copy(q,sizeofW(StgHeader)+2,stp);
1710 case IND_OLDGEN_PERM:
1715 return copy(q,sizeW_fromITBL(info),stp);
1718 case SE_CAF_BLACKHOLE:
1721 return copyPart(q,BLACKHOLE_sizeW(),sizeofW(StgHeader),stp);
1724 to = copy(q,BLACKHOLE_sizeW(),stp);
1727 case THUNK_SELECTOR:
1731 if (thunk_selector_depth > MAX_THUNK_SELECTOR_DEPTH) {
1732 return copy(q,THUNK_SELECTOR_sizeW(),stp);
1735 p = eval_thunk_selector(info->layout.selector_offset,
1739 return copy(q,THUNK_SELECTOR_sizeW(),stp);
1741 // q is still BLACKHOLE'd.
1742 thunk_selector_depth++;
1744 thunk_selector_depth--;
1752 // follow chains of indirections, don't evacuate them
1753 q = ((StgInd*)q)->indirectee;
1757 if (info->srt_len > 0 && major_gc &&
1758 THUNK_STATIC_LINK((StgClosure *)q) == NULL) {
1759 THUNK_STATIC_LINK((StgClosure *)q) = static_objects;
1760 static_objects = (StgClosure *)q;
1765 if (info->srt_len > 0 && major_gc &&
1766 FUN_STATIC_LINK((StgClosure *)q) == NULL) {
1767 FUN_STATIC_LINK((StgClosure *)q) = static_objects;
1768 static_objects = (StgClosure *)q;
1773 /* If q->saved_info != NULL, then it's a revertible CAF - it'll be
1774 * on the CAF list, so don't do anything with it here (we'll
1775 * scavenge it later).
1778 && ((StgIndStatic *)q)->saved_info == NULL
1779 && IND_STATIC_LINK((StgClosure *)q) == NULL) {
1780 IND_STATIC_LINK((StgClosure *)q) = static_objects;
1781 static_objects = (StgClosure *)q;
1786 if (major_gc && STATIC_LINK(info,(StgClosure *)q) == NULL) {
1787 STATIC_LINK(info,(StgClosure *)q) = static_objects;
1788 static_objects = (StgClosure *)q;
1792 case CONSTR_INTLIKE:
1793 case CONSTR_CHARLIKE:
1794 case CONSTR_NOCAF_STATIC:
1795 /* no need to put these on the static linked list, they don't need
1810 // shouldn't see these
1811 barf("evacuate: stack frame at %p\n", q);
1815 /* PAPs and AP_UPDs are special - the payload is a copy of a chunk
1816 * of stack, tagging and all.
1818 return copy(q,pap_sizeW((StgPAP*)q),stp);
1821 /* Already evacuated, just return the forwarding address.
1822 * HOWEVER: if the requested destination generation (evac_gen) is
1823 * older than the actual generation (because the object was
1824 * already evacuated to a younger generation) then we have to
1825 * set the failed_to_evac flag to indicate that we couldn't
1826 * manage to promote the object to the desired generation.
1828 if (evac_gen > 0) { // optimisation
1829 StgClosure *p = ((StgEvacuated*)q)->evacuee;
1830 if (HEAP_ALLOCED(p) && Bdescr((P_)p)->gen_no < evac_gen) {
1831 failed_to_evac = rtsTrue;
1832 TICK_GC_FAILED_PROMOTION();
1835 return ((StgEvacuated*)q)->evacuee;
1838 // just copy the block
1839 return copy(q,arr_words_sizeW((StgArrWords *)q),stp);
1842 case MUT_ARR_PTRS_FROZEN:
1843 // just copy the block
1844 return copy(q,mut_arr_ptrs_sizeW((StgMutArrPtrs *)q),stp);
1848 StgTSO *tso = (StgTSO *)q;
1850 /* Deal with redirected TSOs (a TSO that's had its stack enlarged).
1852 if (tso->what_next == ThreadRelocated) {
1853 q = (StgClosure *)tso->link;
1857 /* To evacuate a small TSO, we need to relocate the update frame
1861 StgTSO *new_tso = (StgTSO *)copy((StgClosure *)tso,tso_sizeW(tso),stp);
1862 move_TSO(tso, new_tso);
1863 return (StgClosure *)new_tso;
1868 case RBH: // cf. BLACKHOLE_BQ
1870 //StgInfoTable *rip = get_closure_info(q, &size, &ptrs, &nonptrs, &vhs, str);
1871 to = copy(q,BLACKHOLE_sizeW(),stp);
1872 //ToDo: derive size etc from reverted IP
1873 //to = copy(q,size,stp);
1875 belch("@@ evacuate: RBH %p (%s) to %p (%s)",
1876 q, info_type(q), to, info_type(to)));
1881 ASSERT(sizeofW(StgBlockedFetch) >= MIN_NONUPD_SIZE);
1882 to = copy(q,sizeofW(StgBlockedFetch),stp);
1884 belch("@@ evacuate: %p (%s) to %p (%s)",
1885 q, info_type(q), to, info_type(to)));
1892 ASSERT(sizeofW(StgBlockedFetch) >= MIN_UPD_SIZE);
1893 to = copy(q,sizeofW(StgFetchMe),stp);
1895 belch("@@ evacuate: %p (%s) to %p (%s)",
1896 q, info_type(q), to, info_type(to)));
1900 ASSERT(sizeofW(StgBlockedFetch) >= MIN_UPD_SIZE);
1901 to = copy(q,sizeofW(StgFetchMeBlockingQueue),stp);
1903 belch("@@ evacuate: %p (%s) to %p (%s)",
1904 q, info_type(q), to, info_type(to)));
1909 barf("evacuate: strange closure type %d", (int)(info->type));
1915 /* -----------------------------------------------------------------------------
1916 Evaluate a THUNK_SELECTOR if possible.
1918 returns: NULL if we couldn't evaluate this THUNK_SELECTOR, or
1919 a closure pointer if we evaluated it and this is the result. Note
1920 that "evaluating" the THUNK_SELECTOR doesn't necessarily mean
1921 reducing it to HNF, just that we have eliminated the selection.
1922 The result might be another thunk, or even another THUNK_SELECTOR.
1924 If the return value is non-NULL, the original selector thunk has
1925 been BLACKHOLE'd, and should be updated with an indirection or a
1926 forwarding pointer. If the return value is NULL, then the selector
1928 -------------------------------------------------------------------------- */
1931 eval_thunk_selector( nat field, StgSelector * p )
1934 const StgInfoTable *info_ptr;
1935 StgClosure *selectee;
1937 selectee = p->selectee;
1939 // Save the real info pointer (NOTE: not the same as get_itbl()).
1940 info_ptr = p->header.info;
1942 // If the THUNK_SELECTOR is in a generation that we are not
1943 // collecting, then bail out early. We won't be able to save any
1944 // space in any case, and updating with an indirection is trickier
1946 if (Bdescr((StgPtr)p)->gen_no > N) {
1950 // BLACKHOLE the selector thunk, since it is now under evaluation.
1951 // This is important to stop us going into an infinite loop if
1952 // this selector thunk eventually refers to itself.
1953 SET_INFO(p,&stg_BLACKHOLE_info);
1957 info = get_itbl(selectee);
1958 switch (info->type) {
1966 case CONSTR_NOCAF_STATIC:
1967 // check that the size is in range
1968 ASSERT(field < (StgWord32)(info->layout.payload.ptrs +
1969 info->layout.payload.nptrs));
1971 return selectee->payload[field];
1977 case IND_OLDGEN_PERM:
1978 selectee = ((StgInd *)selectee)->indirectee;
1982 // We don't follow pointers into to-space; the constructor
1983 // has already been evacuated, so we won't save any space
1984 // leaks by evaluating this selector thunk anyhow.
1987 case THUNK_SELECTOR:
1991 // check that we don't recurse too much, re-using the
1992 // depth bound also used in evacuate().
1993 thunk_selector_depth++;
1994 if (thunk_selector_depth > MAX_THUNK_SELECTOR_DEPTH) {
1998 val = eval_thunk_selector(info->layout.selector_offset,
1999 (StgSelector *)selectee);
2001 thunk_selector_depth--;
2006 // We evaluated this selector thunk, so update it with
2007 // an indirection. NOTE: we don't use UPD_IND here,
2008 // because we are guaranteed that p is in a generation
2009 // that we are collecting, and we never want to put the
2010 // indirection on a mutable list.
2011 ((StgInd *)selectee)->indirectee = val;
2012 SET_INFO(selectee,&stg_IND_info);
2027 case SE_CAF_BLACKHOLE:
2040 // not evaluated yet
2044 barf("eval_thunk_selector: strange selectee %d",
2048 // We didn't manage to evaluate this thunk; restore the old info pointer
2049 SET_INFO(p, info_ptr);
2053 /* -----------------------------------------------------------------------------
2054 move_TSO is called to update the TSO structure after it has been
2055 moved from one place to another.
2056 -------------------------------------------------------------------------- */
2059 move_TSO(StgTSO *src, StgTSO *dest)
2063 // relocate the stack pointers...
2064 diff = (StgPtr)dest - (StgPtr)src; // In *words*
2065 dest->sp = (StgPtr)dest->sp + diff;
2066 dest->su = (StgUpdateFrame *) ((P_)dest->su + diff);
2068 relocate_stack(dest, diff);
2071 /* -----------------------------------------------------------------------------
2072 relocate_stack is called to update the linkage between
2073 UPDATE_FRAMEs (and SEQ_FRAMEs etc.) when a stack is moved from one
2075 -------------------------------------------------------------------------- */
2078 relocate_stack(StgTSO *dest, ptrdiff_t diff)
2086 while ((P_)su < dest->stack + dest->stack_size) {
2087 switch (get_itbl(su)->type) {
2089 // GCC actually manages to common up these three cases!
2092 su->link = (StgUpdateFrame *) ((StgPtr)su->link + diff);
2097 cf = (StgCatchFrame *)su;
2098 cf->link = (StgUpdateFrame *) ((StgPtr)cf->link + diff);
2103 sf = (StgSeqFrame *)su;
2104 sf->link = (StgUpdateFrame *) ((StgPtr)sf->link + diff);
2113 barf("relocate_stack %d", (int)(get_itbl(su)->type));
2124 scavenge_srt(const StgInfoTable *info)
2126 StgClosure **srt, **srt_end;
2128 /* evacuate the SRT. If srt_len is zero, then there isn't an
2129 * srt field in the info table. That's ok, because we'll
2130 * never dereference it.
2132 srt = (StgClosure **)(info->srt);
2133 srt_end = srt + info->srt_len;
2134 for (; srt < srt_end; srt++) {
2135 /* Special-case to handle references to closures hiding out in DLLs, since
2136 double indirections required to get at those. The code generator knows
2137 which is which when generating the SRT, so it stores the (indirect)
2138 reference to the DLL closure in the table by first adding one to it.
2139 We check for this here, and undo the addition before evacuating it.
2141 If the SRT entry hasn't got bit 0 set, the SRT entry points to a
2142 closure that's fixed at link-time, and no extra magic is required.
2144 #ifdef ENABLE_WIN32_DLL_SUPPORT
2145 if ( (unsigned long)(*srt) & 0x1 ) {
2146 evacuate(*stgCast(StgClosure**,(stgCast(unsigned long, *srt) & ~0x1)));
2156 /* -----------------------------------------------------------------------------
2158 -------------------------------------------------------------------------- */
2161 scavengeTSO (StgTSO *tso)
2163 // chase the link field for any TSOs on the same queue
2164 (StgClosure *)tso->link = evacuate((StgClosure *)tso->link);
2165 if ( tso->why_blocked == BlockedOnMVar
2166 || tso->why_blocked == BlockedOnBlackHole
2167 || tso->why_blocked == BlockedOnException
2169 || tso->why_blocked == BlockedOnGA
2170 || tso->why_blocked == BlockedOnGA_NoSend
2173 tso->block_info.closure = evacuate(tso->block_info.closure);
2175 if ( tso->blocked_exceptions != NULL ) {
2176 tso->blocked_exceptions =
2177 (StgTSO *)evacuate((StgClosure *)tso->blocked_exceptions);
2179 // scavenge this thread's stack
2180 scavenge_stack(tso->sp, &(tso->stack[tso->stack_size]));
2183 /* -----------------------------------------------------------------------------
2184 Scavenge a given step until there are no more objects in this step
2187 evac_gen is set by the caller to be either zero (for a step in a
2188 generation < N) or G where G is the generation of the step being
2191 We sometimes temporarily change evac_gen back to zero if we're
2192 scavenging a mutable object where early promotion isn't such a good
2194 -------------------------------------------------------------------------- */
2202 nat saved_evac_gen = evac_gen;
2207 failed_to_evac = rtsFalse;
2209 /* scavenge phase - standard breadth-first scavenging of the
2213 while (bd != stp->hp_bd || p < stp->hp) {
2215 // If we're at the end of this block, move on to the next block
2216 if (bd != stp->hp_bd && p == bd->free) {
2222 info = get_itbl((StgClosure *)p);
2223 ASSERT(p && (LOOKS_LIKE_GHC_INFO(info) || IS_HUGS_CONSTR_INFO(info)));
2225 ASSERT(thunk_selector_depth == 0);
2228 switch (info->type) {
2231 /* treat MVars specially, because we don't want to evacuate the
2232 * mut_link field in the middle of the closure.
2235 StgMVar *mvar = ((StgMVar *)p);
2237 (StgClosure *)mvar->head = evacuate((StgClosure *)mvar->head);
2238 (StgClosure *)mvar->tail = evacuate((StgClosure *)mvar->tail);
2239 (StgClosure *)mvar->value = evacuate((StgClosure *)mvar->value);
2240 evac_gen = saved_evac_gen;
2241 recordMutable((StgMutClosure *)mvar);
2242 failed_to_evac = rtsFalse; // mutable.
2243 p += sizeofW(StgMVar);
2251 ((StgClosure *)p)->payload[1] = evacuate(((StgClosure *)p)->payload[1]);
2252 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
2253 p += sizeofW(StgHeader) + 2;
2258 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
2259 p += sizeofW(StgHeader) + 2; // MIN_UPD_SIZE
2265 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
2266 p += sizeofW(StgHeader) + 1;
2271 p += sizeofW(StgHeader) + 2; // MIN_UPD_SIZE
2277 p += sizeofW(StgHeader) + 1;
2284 p += sizeofW(StgHeader) + 2;
2291 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
2292 p += sizeofW(StgHeader) + 2;
2308 end = (P_)((StgClosure *)p)->payload + info->layout.payload.ptrs;
2309 for (p = (P_)((StgClosure *)p)->payload; p < end; p++) {
2310 (StgClosure *)*p = evacuate((StgClosure *)*p);
2312 p += info->layout.payload.nptrs;
2317 if (stp->gen->no != 0) {
2320 // No need to call LDV_recordDead_FILL_SLOP_DYNAMIC() because an
2321 // IND_OLDGEN_PERM closure is larger than an IND_PERM closure.
2322 LDV_recordDead((StgClosure *)p, sizeofW(StgInd));
2325 // Todo: maybe use SET_HDR() and remove LDV_recordCreate()?
2327 SET_INFO(((StgClosure *)p), &stg_IND_OLDGEN_PERM_info);
2330 // We pretend that p has just been created.
2331 LDV_recordCreate((StgClosure *)p);
2335 case IND_OLDGEN_PERM:
2336 ((StgIndOldGen *)p)->indirectee =
2337 evacuate(((StgIndOldGen *)p)->indirectee);
2338 if (failed_to_evac) {
2339 failed_to_evac = rtsFalse;
2340 recordOldToNewPtrs((StgMutClosure *)p);
2342 p += sizeofW(StgIndOldGen);
2347 ((StgMutVar *)p)->var = evacuate(((StgMutVar *)p)->var);
2348 evac_gen = saved_evac_gen;
2349 recordMutable((StgMutClosure *)p);
2350 failed_to_evac = rtsFalse; // mutable anyhow
2351 p += sizeofW(StgMutVar);
2356 failed_to_evac = rtsFalse; // mutable anyhow
2357 p += sizeofW(StgMutVar);
2361 case SE_CAF_BLACKHOLE:
2364 p += BLACKHOLE_sizeW();
2369 StgBlockingQueue *bh = (StgBlockingQueue *)p;
2370 (StgClosure *)bh->blocking_queue =
2371 evacuate((StgClosure *)bh->blocking_queue);
2372 recordMutable((StgMutClosure *)bh);
2373 failed_to_evac = rtsFalse;
2374 p += BLACKHOLE_sizeW();
2378 case THUNK_SELECTOR:
2380 StgSelector *s = (StgSelector *)p;
2381 s->selectee = evacuate(s->selectee);
2382 p += THUNK_SELECTOR_sizeW();
2386 case AP_UPD: // same as PAPs
2388 /* Treat a PAP just like a section of stack, not forgetting to
2389 * evacuate the function pointer too...
2392 StgPAP* pap = (StgPAP *)p;
2394 pap->fun = evacuate(pap->fun);
2395 scavenge_stack((P_)pap->payload, (P_)pap->payload + pap->n_args);
2396 p += pap_sizeW(pap);
2401 // nothing to follow
2402 p += arr_words_sizeW((StgArrWords *)p);
2406 // follow everything
2410 evac_gen = 0; // repeatedly mutable
2411 next = p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
2412 for (p = (P_)((StgMutArrPtrs *)p)->payload; p < next; p++) {
2413 (StgClosure *)*p = evacuate((StgClosure *)*p);
2415 evac_gen = saved_evac_gen;
2416 recordMutable((StgMutClosure *)q);
2417 failed_to_evac = rtsFalse; // mutable anyhow.
2421 case MUT_ARR_PTRS_FROZEN:
2422 // follow everything
2426 next = p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
2427 for (p = (P_)((StgMutArrPtrs *)p)->payload; p < next; p++) {
2428 (StgClosure *)*p = evacuate((StgClosure *)*p);
2430 // it's tempting to recordMutable() if failed_to_evac is
2431 // false, but that breaks some assumptions (eg. every
2432 // closure on the mutable list is supposed to have the MUT
2433 // flag set, and MUT_ARR_PTRS_FROZEN doesn't).
2439 StgTSO *tso = (StgTSO *)p;
2442 evac_gen = saved_evac_gen;
2443 recordMutable((StgMutClosure *)tso);
2444 failed_to_evac = rtsFalse; // mutable anyhow.
2445 p += tso_sizeW(tso);
2450 case RBH: // cf. BLACKHOLE_BQ
2453 nat size, ptrs, nonptrs, vhs;
2455 StgInfoTable *rip = get_closure_info(p, &size, &ptrs, &nonptrs, &vhs, str);
2457 StgRBH *rbh = (StgRBH *)p;
2458 (StgClosure *)rbh->blocking_queue =
2459 evacuate((StgClosure *)rbh->blocking_queue);
2460 recordMutable((StgMutClosure *)to);
2461 failed_to_evac = rtsFalse; // mutable anyhow.
2463 belch("@@ scavenge: RBH %p (%s) (new blocking_queue link=%p)",
2464 p, info_type(p), (StgClosure *)rbh->blocking_queue));
2465 // ToDo: use size of reverted closure here!
2466 p += BLACKHOLE_sizeW();
2472 StgBlockedFetch *bf = (StgBlockedFetch *)p;
2473 // follow the pointer to the node which is being demanded
2474 (StgClosure *)bf->node =
2475 evacuate((StgClosure *)bf->node);
2476 // follow the link to the rest of the blocking queue
2477 (StgClosure *)bf->link =
2478 evacuate((StgClosure *)bf->link);
2479 if (failed_to_evac) {
2480 failed_to_evac = rtsFalse;
2481 recordMutable((StgMutClosure *)bf);
2484 belch("@@ scavenge: %p (%s); node is now %p; exciting, isn't it",
2485 bf, info_type((StgClosure *)bf),
2486 bf->node, info_type(bf->node)));
2487 p += sizeofW(StgBlockedFetch);
2495 p += sizeofW(StgFetchMe);
2496 break; // nothing to do in this case
2498 case FETCH_ME_BQ: // cf. BLACKHOLE_BQ
2500 StgFetchMeBlockingQueue *fmbq = (StgFetchMeBlockingQueue *)p;
2501 (StgClosure *)fmbq->blocking_queue =
2502 evacuate((StgClosure *)fmbq->blocking_queue);
2503 if (failed_to_evac) {
2504 failed_to_evac = rtsFalse;
2505 recordMutable((StgMutClosure *)fmbq);
2508 belch("@@ scavenge: %p (%s) exciting, isn't it",
2509 p, info_type((StgClosure *)p)));
2510 p += sizeofW(StgFetchMeBlockingQueue);
2516 barf("scavenge: unimplemented/strange closure type %d @ %p",
2520 /* If we didn't manage to promote all the objects pointed to by
2521 * the current object, then we have to designate this object as
2522 * mutable (because it contains old-to-new generation pointers).
2524 if (failed_to_evac) {
2525 failed_to_evac = rtsFalse;
2526 mkMutCons((StgClosure *)q, &generations[evac_gen]);
2534 /* -----------------------------------------------------------------------------
2535 Scavenge everything on the mark stack.
2537 This is slightly different from scavenge():
2538 - we don't walk linearly through the objects, so the scavenger
2539 doesn't need to advance the pointer on to the next object.
2540 -------------------------------------------------------------------------- */
2543 scavenge_mark_stack(void)
2549 evac_gen = oldest_gen->no;
2550 saved_evac_gen = evac_gen;
2553 while (!mark_stack_empty()) {
2554 p = pop_mark_stack();
2556 info = get_itbl((StgClosure *)p);
2557 ASSERT(p && (LOOKS_LIKE_GHC_INFO(info) || IS_HUGS_CONSTR_INFO(info)));
2560 switch (info->type) {
2563 /* treat MVars specially, because we don't want to evacuate the
2564 * mut_link field in the middle of the closure.
2567 StgMVar *mvar = ((StgMVar *)p);
2569 (StgClosure *)mvar->head = evacuate((StgClosure *)mvar->head);
2570 (StgClosure *)mvar->tail = evacuate((StgClosure *)mvar->tail);
2571 (StgClosure *)mvar->value = evacuate((StgClosure *)mvar->value);
2572 evac_gen = saved_evac_gen;
2573 failed_to_evac = rtsFalse; // mutable.
2581 ((StgClosure *)p)->payload[1] = evacuate(((StgClosure *)p)->payload[1]);
2582 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
2592 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
2617 end = (P_)((StgClosure *)p)->payload + info->layout.payload.ptrs;
2618 for (p = (P_)((StgClosure *)p)->payload; p < end; p++) {
2619 (StgClosure *)*p = evacuate((StgClosure *)*p);
2625 // don't need to do anything here: the only possible case
2626 // is that we're in a 1-space compacting collector, with
2627 // no "old" generation.
2631 case IND_OLDGEN_PERM:
2632 ((StgIndOldGen *)p)->indirectee =
2633 evacuate(((StgIndOldGen *)p)->indirectee);
2634 if (failed_to_evac) {
2635 recordOldToNewPtrs((StgMutClosure *)p);
2637 failed_to_evac = rtsFalse;
2642 ((StgMutVar *)p)->var = evacuate(((StgMutVar *)p)->var);
2643 evac_gen = saved_evac_gen;
2644 failed_to_evac = rtsFalse;
2649 failed_to_evac = rtsFalse;
2653 case SE_CAF_BLACKHOLE:
2661 StgBlockingQueue *bh = (StgBlockingQueue *)p;
2662 (StgClosure *)bh->blocking_queue =
2663 evacuate((StgClosure *)bh->blocking_queue);
2664 failed_to_evac = rtsFalse;
2668 case THUNK_SELECTOR:
2670 StgSelector *s = (StgSelector *)p;
2671 s->selectee = evacuate(s->selectee);
2675 case AP_UPD: // same as PAPs
2677 /* Treat a PAP just like a section of stack, not forgetting to
2678 * evacuate the function pointer too...
2681 StgPAP* pap = (StgPAP *)p;
2683 pap->fun = evacuate(pap->fun);
2684 scavenge_stack((P_)pap->payload, (P_)pap->payload + pap->n_args);
2689 // follow everything
2693 evac_gen = 0; // repeatedly mutable
2694 next = p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
2695 for (p = (P_)((StgMutArrPtrs *)p)->payload; p < next; p++) {
2696 (StgClosure *)*p = evacuate((StgClosure *)*p);
2698 evac_gen = saved_evac_gen;
2699 failed_to_evac = rtsFalse; // mutable anyhow.
2703 case MUT_ARR_PTRS_FROZEN:
2704 // follow everything
2708 next = p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
2709 for (p = (P_)((StgMutArrPtrs *)p)->payload; p < next; p++) {
2710 (StgClosure *)*p = evacuate((StgClosure *)*p);
2717 StgTSO *tso = (StgTSO *)p;
2720 evac_gen = saved_evac_gen;
2721 failed_to_evac = rtsFalse;
2726 case RBH: // cf. BLACKHOLE_BQ
2729 nat size, ptrs, nonptrs, vhs;
2731 StgInfoTable *rip = get_closure_info(p, &size, &ptrs, &nonptrs, &vhs, str);
2733 StgRBH *rbh = (StgRBH *)p;
2734 (StgClosure *)rbh->blocking_queue =
2735 evacuate((StgClosure *)rbh->blocking_queue);
2736 recordMutable((StgMutClosure *)rbh);
2737 failed_to_evac = rtsFalse; // mutable anyhow.
2739 belch("@@ scavenge: RBH %p (%s) (new blocking_queue link=%p)",
2740 p, info_type(p), (StgClosure *)rbh->blocking_queue));
2746 StgBlockedFetch *bf = (StgBlockedFetch *)p;
2747 // follow the pointer to the node which is being demanded
2748 (StgClosure *)bf->node =
2749 evacuate((StgClosure *)bf->node);
2750 // follow the link to the rest of the blocking queue
2751 (StgClosure *)bf->link =
2752 evacuate((StgClosure *)bf->link);
2753 if (failed_to_evac) {
2754 failed_to_evac = rtsFalse;
2755 recordMutable((StgMutClosure *)bf);
2758 belch("@@ scavenge: %p (%s); node is now %p; exciting, isn't it",
2759 bf, info_type((StgClosure *)bf),
2760 bf->node, info_type(bf->node)));
2768 break; // nothing to do in this case
2770 case FETCH_ME_BQ: // cf. BLACKHOLE_BQ
2772 StgFetchMeBlockingQueue *fmbq = (StgFetchMeBlockingQueue *)p;
2773 (StgClosure *)fmbq->blocking_queue =
2774 evacuate((StgClosure *)fmbq->blocking_queue);
2775 if (failed_to_evac) {
2776 failed_to_evac = rtsFalse;
2777 recordMutable((StgMutClosure *)fmbq);
2780 belch("@@ scavenge: %p (%s) exciting, isn't it",
2781 p, info_type((StgClosure *)p)));
2787 barf("scavenge_mark_stack: unimplemented/strange closure type %d @ %p",
2791 if (failed_to_evac) {
2792 failed_to_evac = rtsFalse;
2793 mkMutCons((StgClosure *)q, &generations[evac_gen]);
2796 // mark the next bit to indicate "scavenged"
2797 mark(q+1, Bdescr(q));
2799 } // while (!mark_stack_empty())
2801 // start a new linear scan if the mark stack overflowed at some point
2802 if (mark_stack_overflowed && oldgen_scan_bd == NULL) {
2803 IF_DEBUG(gc, belch("scavenge_mark_stack: starting linear scan"));
2804 mark_stack_overflowed = rtsFalse;
2805 oldgen_scan_bd = oldest_gen->steps[0].blocks;
2806 oldgen_scan = oldgen_scan_bd->start;
2809 if (oldgen_scan_bd) {
2810 // push a new thing on the mark stack
2812 // find a closure that is marked but not scavenged, and start
2814 while (oldgen_scan < oldgen_scan_bd->free
2815 && !is_marked(oldgen_scan,oldgen_scan_bd)) {
2819 if (oldgen_scan < oldgen_scan_bd->free) {
2821 // already scavenged?
2822 if (is_marked(oldgen_scan+1,oldgen_scan_bd)) {
2823 oldgen_scan += sizeofW(StgHeader) + MIN_NONUPD_SIZE;
2826 push_mark_stack(oldgen_scan);
2827 // ToDo: bump the linear scan by the actual size of the object
2828 oldgen_scan += sizeofW(StgHeader) + MIN_NONUPD_SIZE;
2832 oldgen_scan_bd = oldgen_scan_bd->link;
2833 if (oldgen_scan_bd != NULL) {
2834 oldgen_scan = oldgen_scan_bd->start;
2840 /* -----------------------------------------------------------------------------
2841 Scavenge one object.
2843 This is used for objects that are temporarily marked as mutable
2844 because they contain old-to-new generation pointers. Only certain
2845 objects can have this property.
2846 -------------------------------------------------------------------------- */
2849 scavenge_one(StgPtr p)
2851 const StgInfoTable *info;
2852 nat saved_evac_gen = evac_gen;
2855 ASSERT(p && (LOOKS_LIKE_GHC_INFO(GET_INFO((StgClosure *)p))
2856 || IS_HUGS_CONSTR_INFO(GET_INFO((StgClosure *)p))));
2858 info = get_itbl((StgClosure *)p);
2860 switch (info->type) {
2863 case FUN_1_0: // hardly worth specialising these guys
2883 case IND_OLDGEN_PERM:
2887 end = (StgPtr)((StgClosure *)p)->payload + info->layout.payload.ptrs;
2888 for (q = (StgPtr)((StgClosure *)p)->payload; q < end; q++) {
2889 (StgClosure *)*q = evacuate((StgClosure *)*q);
2895 case SE_CAF_BLACKHOLE:
2900 case THUNK_SELECTOR:
2902 StgSelector *s = (StgSelector *)p;
2903 s->selectee = evacuate(s->selectee);
2908 // nothing to follow
2913 // follow everything
2916 evac_gen = 0; // repeatedly mutable
2917 recordMutable((StgMutClosure *)p);
2918 next = p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
2919 for (p = (P_)((StgMutArrPtrs *)p)->payload; p < next; p++) {
2920 (StgClosure *)*p = evacuate((StgClosure *)*p);
2922 evac_gen = saved_evac_gen;
2923 failed_to_evac = rtsFalse;
2927 case MUT_ARR_PTRS_FROZEN:
2929 // follow everything
2932 next = p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
2933 for (p = (P_)((StgMutArrPtrs *)p)->payload; p < next; p++) {
2934 (StgClosure *)*p = evacuate((StgClosure *)*p);
2941 StgTSO *tso = (StgTSO *)p;
2943 evac_gen = 0; // repeatedly mutable
2945 recordMutable((StgMutClosure *)tso);
2946 evac_gen = saved_evac_gen;
2947 failed_to_evac = rtsFalse;
2954 StgPAP* pap = (StgPAP *)p;
2955 pap->fun = evacuate(pap->fun);
2956 scavenge_stack((P_)pap->payload, (P_)pap->payload + pap->n_args);
2961 // This might happen if for instance a MUT_CONS was pointing to a
2962 // THUNK which has since been updated. The IND_OLDGEN will
2963 // be on the mutable list anyway, so we don't need to do anything
2968 barf("scavenge_one: strange object %d", (int)(info->type));
2971 no_luck = failed_to_evac;
2972 failed_to_evac = rtsFalse;
2976 /* -----------------------------------------------------------------------------
2977 Scavenging mutable lists.
2979 We treat the mutable list of each generation > N (i.e. all the
2980 generations older than the one being collected) as roots. We also
2981 remove non-mutable objects from the mutable list at this point.
2982 -------------------------------------------------------------------------- */
2985 scavenge_mut_once_list(generation *gen)
2987 const StgInfoTable *info;
2988 StgMutClosure *p, *next, *new_list;
2990 p = gen->mut_once_list;
2991 new_list = END_MUT_LIST;
2995 failed_to_evac = rtsFalse;
2997 for (; p != END_MUT_LIST; p = next, next = p->mut_link) {
2999 // make sure the info pointer is into text space
3000 ASSERT(p && (LOOKS_LIKE_GHC_INFO(GET_INFO(p))
3001 || IS_HUGS_CONSTR_INFO(GET_INFO(p))));
3005 if (info->type==RBH)
3006 info = REVERT_INFOPTR(info); // if it's an RBH, look at the orig closure
3008 switch(info->type) {
3011 case IND_OLDGEN_PERM:
3013 /* Try to pull the indirectee into this generation, so we can
3014 * remove the indirection from the mutable list.
3016 ((StgIndOldGen *)p)->indirectee =
3017 evacuate(((StgIndOldGen *)p)->indirectee);
3019 #if 0 && defined(DEBUG)
3020 if (RtsFlags.DebugFlags.gc)
3021 /* Debugging code to print out the size of the thing we just
3025 StgPtr start = gen->steps[0].scan;
3026 bdescr *start_bd = gen->steps[0].scan_bd;
3028 scavenge(&gen->steps[0]);
3029 if (start_bd != gen->steps[0].scan_bd) {
3030 size += (P_)BLOCK_ROUND_UP(start) - start;
3031 start_bd = start_bd->link;
3032 while (start_bd != gen->steps[0].scan_bd) {
3033 size += BLOCK_SIZE_W;
3034 start_bd = start_bd->link;
3036 size += gen->steps[0].scan -
3037 (P_)BLOCK_ROUND_DOWN(gen->steps[0].scan);
3039 size = gen->steps[0].scan - start;
3041 belch("evac IND_OLDGEN: %ld bytes", size * sizeof(W_));
3045 /* failed_to_evac might happen if we've got more than two
3046 * generations, we're collecting only generation 0, the
3047 * indirection resides in generation 2 and the indirectee is
3050 if (failed_to_evac) {
3051 failed_to_evac = rtsFalse;
3052 p->mut_link = new_list;
3055 /* the mut_link field of an IND_STATIC is overloaded as the
3056 * static link field too (it just so happens that we don't need
3057 * both at the same time), so we need to NULL it out when
3058 * removing this object from the mutable list because the static
3059 * link fields are all assumed to be NULL before doing a major
3067 /* MUT_CONS is a kind of MUT_VAR, except it that we try to remove
3068 * it from the mutable list if possible by promoting whatever it
3071 if (scavenge_one((StgPtr)((StgMutVar *)p)->var)) {
3072 /* didn't manage to promote everything, so put the
3073 * MUT_CONS back on the list.
3075 p->mut_link = new_list;
3081 // shouldn't have anything else on the mutables list
3082 barf("scavenge_mut_once_list: strange object? %d", (int)(info->type));
3086 gen->mut_once_list = new_list;
3091 scavenge_mutable_list(generation *gen)
3093 const StgInfoTable *info;
3094 StgMutClosure *p, *next;
3096 p = gen->saved_mut_list;
3100 failed_to_evac = rtsFalse;
3102 for (; p != END_MUT_LIST; p = next, next = p->mut_link) {
3104 // make sure the info pointer is into text space
3105 ASSERT(p && (LOOKS_LIKE_GHC_INFO(GET_INFO(p))
3106 || IS_HUGS_CONSTR_INFO(GET_INFO(p))));
3110 if (info->type==RBH)
3111 info = REVERT_INFOPTR(info); // if it's an RBH, look at the orig closure
3113 switch(info->type) {
3116 // follow everything
3117 p->mut_link = gen->mut_list;
3122 end = (P_)p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
3123 for (q = (P_)((StgMutArrPtrs *)p)->payload; q < end; q++) {
3124 (StgClosure *)*q = evacuate((StgClosure *)*q);
3129 // Happens if a MUT_ARR_PTRS in the old generation is frozen
3130 case MUT_ARR_PTRS_FROZEN:
3135 end = (P_)p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
3136 for (q = (P_)((StgMutArrPtrs *)p)->payload; q < end; q++) {
3137 (StgClosure *)*q = evacuate((StgClosure *)*q);
3141 if (failed_to_evac) {
3142 failed_to_evac = rtsFalse;
3143 mkMutCons((StgClosure *)p, gen);
3149 ((StgMutVar *)p)->var = evacuate(((StgMutVar *)p)->var);
3150 p->mut_link = gen->mut_list;
3156 StgMVar *mvar = (StgMVar *)p;
3157 (StgClosure *)mvar->head = evacuate((StgClosure *)mvar->head);
3158 (StgClosure *)mvar->tail = evacuate((StgClosure *)mvar->tail);
3159 (StgClosure *)mvar->value = evacuate((StgClosure *)mvar->value);
3160 p->mut_link = gen->mut_list;
3167 StgTSO *tso = (StgTSO *)p;
3171 /* Don't take this TSO off the mutable list - it might still
3172 * point to some younger objects (because we set evac_gen to 0
3175 tso->mut_link = gen->mut_list;
3176 gen->mut_list = (StgMutClosure *)tso;
3182 StgBlockingQueue *bh = (StgBlockingQueue *)p;
3183 (StgClosure *)bh->blocking_queue =
3184 evacuate((StgClosure *)bh->blocking_queue);
3185 p->mut_link = gen->mut_list;
3190 /* Happens if a BLACKHOLE_BQ in the old generation is updated:
3193 case IND_OLDGEN_PERM:
3194 /* Try to pull the indirectee into this generation, so we can
3195 * remove the indirection from the mutable list.
3198 ((StgIndOldGen *)p)->indirectee =
3199 evacuate(((StgIndOldGen *)p)->indirectee);
3202 if (failed_to_evac) {
3203 failed_to_evac = rtsFalse;
3204 p->mut_link = gen->mut_once_list;
3205 gen->mut_once_list = p;
3212 // HWL: check whether all of these are necessary
3214 case RBH: // cf. BLACKHOLE_BQ
3216 // nat size, ptrs, nonptrs, vhs;
3218 // StgInfoTable *rip = get_closure_info(p, &size, &ptrs, &nonptrs, &vhs, str);
3219 StgRBH *rbh = (StgRBH *)p;
3220 (StgClosure *)rbh->blocking_queue =
3221 evacuate((StgClosure *)rbh->blocking_queue);
3222 if (failed_to_evac) {
3223 failed_to_evac = rtsFalse;
3224 recordMutable((StgMutClosure *)rbh);
3226 // ToDo: use size of reverted closure here!
3227 p += BLACKHOLE_sizeW();
3233 StgBlockedFetch *bf = (StgBlockedFetch *)p;
3234 // follow the pointer to the node which is being demanded
3235 (StgClosure *)bf->node =
3236 evacuate((StgClosure *)bf->node);
3237 // follow the link to the rest of the blocking queue
3238 (StgClosure *)bf->link =
3239 evacuate((StgClosure *)bf->link);
3240 if (failed_to_evac) {
3241 failed_to_evac = rtsFalse;
3242 recordMutable((StgMutClosure *)bf);
3244 p += sizeofW(StgBlockedFetch);
3250 barf("scavenge_mutable_list: REMOTE_REF %d", (int)(info->type));
3253 p += sizeofW(StgFetchMe);
3254 break; // nothing to do in this case
3256 case FETCH_ME_BQ: // cf. BLACKHOLE_BQ
3258 StgFetchMeBlockingQueue *fmbq = (StgFetchMeBlockingQueue *)p;
3259 (StgClosure *)fmbq->blocking_queue =
3260 evacuate((StgClosure *)fmbq->blocking_queue);
3261 if (failed_to_evac) {
3262 failed_to_evac = rtsFalse;
3263 recordMutable((StgMutClosure *)fmbq);
3265 p += sizeofW(StgFetchMeBlockingQueue);
3271 // shouldn't have anything else on the mutables list
3272 barf("scavenge_mutable_list: strange object? %d", (int)(info->type));
3279 scavenge_static(void)
3281 StgClosure* p = static_objects;
3282 const StgInfoTable *info;
3284 /* Always evacuate straight to the oldest generation for static
3286 evac_gen = oldest_gen->no;
3288 /* keep going until we've scavenged all the objects on the linked
3290 while (p != END_OF_STATIC_LIST) {
3294 if (info->type==RBH)
3295 info = REVERT_INFOPTR(info); // if it's an RBH, look at the orig closure
3297 // make sure the info pointer is into text space
3298 ASSERT(p && (LOOKS_LIKE_GHC_INFO(GET_INFO(p))
3299 || IS_HUGS_CONSTR_INFO(GET_INFO(p))));
3301 /* Take this object *off* the static_objects list,
3302 * and put it on the scavenged_static_objects list.
3304 static_objects = STATIC_LINK(info,p);
3305 STATIC_LINK(info,p) = scavenged_static_objects;
3306 scavenged_static_objects = p;
3308 switch (info -> type) {
3312 StgInd *ind = (StgInd *)p;
3313 ind->indirectee = evacuate(ind->indirectee);
3315 /* might fail to evacuate it, in which case we have to pop it
3316 * back on the mutable list (and take it off the
3317 * scavenged_static list because the static link and mut link
3318 * pointers are one and the same).
3320 if (failed_to_evac) {
3321 failed_to_evac = rtsFalse;
3322 scavenged_static_objects = IND_STATIC_LINK(p);
3323 ((StgMutClosure *)ind)->mut_link = oldest_gen->mut_once_list;
3324 oldest_gen->mut_once_list = (StgMutClosure *)ind;
3338 next = (P_)p->payload + info->layout.payload.ptrs;
3339 // evacuate the pointers
3340 for (q = (P_)p->payload; q < next; q++) {
3341 (StgClosure *)*q = evacuate((StgClosure *)*q);
3347 barf("scavenge_static: strange closure %d", (int)(info->type));
3350 ASSERT(failed_to_evac == rtsFalse);
3352 /* get the next static object from the list. Remember, there might
3353 * be more stuff on this list now that we've done some evacuating!
3354 * (static_objects is a global)
3360 /* -----------------------------------------------------------------------------
3361 scavenge_stack walks over a section of stack and evacuates all the
3362 objects pointed to by it. We can use the same code for walking
3363 PAPs, since these are just sections of copied stack.
3364 -------------------------------------------------------------------------- */
3367 scavenge_stack(StgPtr p, StgPtr stack_end)
3370 const StgInfoTable* info;
3373 //IF_DEBUG(sanity, belch(" scavenging stack between %p and %p", p, stack_end));
3376 * Each time around this loop, we are looking at a chunk of stack
3377 * that starts with either a pending argument section or an
3378 * activation record.
3381 while (p < stack_end) {
3384 // If we've got a tag, skip over that many words on the stack
3385 if (IS_ARG_TAG((W_)q)) {
3390 /* Is q a pointer to a closure?
3392 if (! LOOKS_LIKE_GHC_INFO(q) ) {
3394 if ( 0 && LOOKS_LIKE_STATIC_CLOSURE(q) ) { // Is it a static closure?
3395 ASSERT(closure_STATIC((StgClosure *)q));
3397 // otherwise, must be a pointer into the allocation space.
3400 (StgClosure *)*p = evacuate((StgClosure *)q);
3406 * Otherwise, q must be the info pointer of an activation
3407 * record. All activation records have 'bitmap' style layout
3410 info = get_itbl((StgClosure *)p);
3412 switch (info->type) {
3414 // Dynamic bitmap: the mask is stored on the stack
3416 bitmap = ((StgRetDyn *)p)->liveness;
3417 p = (P_)&((StgRetDyn *)p)->payload[0];
3420 // probably a slow-entry point return address:
3428 belch("HWL: scavenge_stack: FUN(_STATIC) adjusting p from %p to %p (instead of %p)",
3429 old_p, p, old_p+1));
3431 p++; // what if FHS!=1 !? -- HWL
3436 /* Specialised code for update frames, since they're so common.
3437 * We *know* the updatee points to a BLACKHOLE, CAF_BLACKHOLE,
3438 * or BLACKHOLE_BQ, so just inline the code to evacuate it here.
3442 StgUpdateFrame *frame = (StgUpdateFrame *)p;
3444 p += sizeofW(StgUpdateFrame);
3447 frame->updatee = evacuate(frame->updatee);
3449 #else // specialised code for update frames, not sure if it's worth it.
3451 nat type = get_itbl(frame->updatee)->type;
3453 if (type == EVACUATED) {
3454 frame->updatee = evacuate(frame->updatee);
3457 bdescr *bd = Bdescr((P_)frame->updatee);
3459 if (bd->gen_no > N) {
3460 if (bd->gen_no < evac_gen) {
3461 failed_to_evac = rtsTrue;
3466 // Don't promote blackholes
3468 if (!(stp->gen_no == 0 &&
3470 stp->no == stp->gen->n_steps-1)) {
3477 to = copyPart(frame->updatee, BLACKHOLE_sizeW(),
3478 sizeofW(StgHeader), stp);
3479 frame->updatee = to;
3482 to = copy(frame->updatee, BLACKHOLE_sizeW(), stp);
3483 frame->updatee = to;
3484 recordMutable((StgMutClosure *)to);
3487 /* will never be SE_{,CAF_}BLACKHOLE, since we
3488 don't push an update frame for single-entry thunks. KSW 1999-01. */
3489 barf("scavenge_stack: UPDATE_FRAME updatee");
3495 // small bitmap (< 32 entries, or 64 on a 64-bit machine)
3502 bitmap = info->layout.bitmap;
3504 // this assumes that the payload starts immediately after the info-ptr
3506 while (bitmap != 0) {
3507 if ((bitmap & 1) == 0) {
3508 (StgClosure *)*p = evacuate((StgClosure *)*p);
3511 bitmap = bitmap >> 1;
3518 // large bitmap (> 32 entries, or > 64 on a 64-bit machine)
3523 StgLargeBitmap *large_bitmap;
3526 large_bitmap = info->layout.large_bitmap;
3529 for (i=0; i<large_bitmap->size; i++) {
3530 bitmap = large_bitmap->bitmap[i];
3531 q = p + BITS_IN(W_);
3532 while (bitmap != 0) {
3533 if ((bitmap & 1) == 0) {
3534 (StgClosure *)*p = evacuate((StgClosure *)*p);
3537 bitmap = bitmap >> 1;
3539 if (i+1 < large_bitmap->size) {
3541 (StgClosure *)*p = evacuate((StgClosure *)*p);
3547 // and don't forget to follow the SRT
3552 barf("scavenge_stack: weird activation record found on stack: %d", (int)(info->type));
3557 /*-----------------------------------------------------------------------------
3558 scavenge the large object list.
3560 evac_gen set by caller; similar games played with evac_gen as with
3561 scavenge() - see comment at the top of scavenge(). Most large
3562 objects are (repeatedly) mutable, so most of the time evac_gen will
3564 --------------------------------------------------------------------------- */
3567 scavenge_large(step *stp)
3572 bd = stp->new_large_objects;
3574 for (; bd != NULL; bd = stp->new_large_objects) {
3576 /* take this object *off* the large objects list and put it on
3577 * the scavenged large objects list. This is so that we can
3578 * treat new_large_objects as a stack and push new objects on
3579 * the front when evacuating.
3581 stp->new_large_objects = bd->link;
3582 dbl_link_onto(bd, &stp->scavenged_large_objects);
3584 // update the block count in this step.
3585 stp->n_scavenged_large_blocks += bd->blocks;
3588 if (scavenge_one(p)) {
3589 mkMutCons((StgClosure *)p, stp->gen);
3594 /* -----------------------------------------------------------------------------
3595 Initialising the static object & mutable lists
3596 -------------------------------------------------------------------------- */
3599 zero_static_object_list(StgClosure* first_static)
3603 const StgInfoTable *info;
3605 for (p = first_static; p != END_OF_STATIC_LIST; p = link) {
3607 link = STATIC_LINK(info, p);
3608 STATIC_LINK(info,p) = NULL;
3612 /* This function is only needed because we share the mutable link
3613 * field with the static link field in an IND_STATIC, so we have to
3614 * zero the mut_link field before doing a major GC, which needs the
3615 * static link field.
3617 * It doesn't do any harm to zero all the mutable link fields on the
3622 zero_mutable_list( StgMutClosure *first )
3624 StgMutClosure *next, *c;
3626 for (c = first; c != END_MUT_LIST; c = next) {
3632 /* -----------------------------------------------------------------------------
3634 -------------------------------------------------------------------------- */
3641 for (c = (StgIndStatic *)caf_list; c != NULL;
3642 c = (StgIndStatic *)c->static_link)
3644 c->header.info = c->saved_info;
3645 c->saved_info = NULL;
3646 // could, but not necessary: c->static_link = NULL;
3652 markCAFs( evac_fn evac )
3656 for (c = (StgIndStatic *)caf_list; c != NULL;
3657 c = (StgIndStatic *)c->static_link)
3659 evac(&c->indirectee);
3663 /* -----------------------------------------------------------------------------
3664 Sanity code for CAF garbage collection.
3666 With DEBUG turned on, we manage a CAF list in addition to the SRT
3667 mechanism. After GC, we run down the CAF list and blackhole any
3668 CAFs which have been garbage collected. This means we get an error
3669 whenever the program tries to enter a garbage collected CAF.
3671 Any garbage collected CAFs are taken off the CAF list at the same
3673 -------------------------------------------------------------------------- */
3675 #if 0 && defined(DEBUG)
3682 const StgInfoTable *info;
3693 ASSERT(info->type == IND_STATIC);
3695 if (STATIC_LINK(info,p) == NULL) {
3696 IF_DEBUG(gccafs, belch("CAF gc'd at 0x%04lx", (long)p));
3698 SET_INFO(p,&stg_BLACKHOLE_info);
3699 p = STATIC_LINK2(info,p);
3703 pp = &STATIC_LINK2(info,p);
3710 // belch("%d CAFs live", i);
3715 /* -----------------------------------------------------------------------------
3718 Whenever a thread returns to the scheduler after possibly doing
3719 some work, we have to run down the stack and black-hole all the
3720 closures referred to by update frames.
3721 -------------------------------------------------------------------------- */
3724 threadLazyBlackHole(StgTSO *tso)
3726 StgUpdateFrame *update_frame;
3727 StgBlockingQueue *bh;
3730 stack_end = &tso->stack[tso->stack_size];
3731 update_frame = tso->su;
3734 switch (get_itbl(update_frame)->type) {
3737 update_frame = ((StgCatchFrame *)update_frame)->link;
3741 bh = (StgBlockingQueue *)update_frame->updatee;
3743 /* if the thunk is already blackholed, it means we've also
3744 * already blackholed the rest of the thunks on this stack,
3745 * so we can stop early.
3747 * The blackhole made for a CAF is a CAF_BLACKHOLE, so they
3748 * don't interfere with this optimisation.
3750 if (bh->header.info == &stg_BLACKHOLE_info) {
3754 if (bh->header.info != &stg_BLACKHOLE_BQ_info &&
3755 bh->header.info != &stg_CAF_BLACKHOLE_info) {
3756 #if (!defined(LAZY_BLACKHOLING)) && defined(DEBUG)
3757 belch("Unexpected lazy BHing required at 0x%04x",(int)bh);
3761 // We pretend that bh is now dead.
3762 LDV_recordDead_FILL_SLOP_DYNAMIC((StgClosure *)bh);
3764 SET_INFO(bh,&stg_BLACKHOLE_info);
3767 // We pretend that bh has just been created.
3768 LDV_recordCreate(bh);
3772 update_frame = update_frame->link;
3776 update_frame = ((StgSeqFrame *)update_frame)->link;
3782 barf("threadPaused");
3788 /* -----------------------------------------------------------------------------
3791 * Code largely pinched from old RTS, then hacked to bits. We also do
3792 * lazy black holing here.
3794 * -------------------------------------------------------------------------- */
3797 threadSqueezeStack(StgTSO *tso)
3799 lnat displacement = 0;
3800 StgUpdateFrame *frame;
3801 StgUpdateFrame *next_frame; // Temporally next
3802 StgUpdateFrame *prev_frame; // Temporally previous
3804 rtsBool prev_was_update_frame;
3806 StgUpdateFrame *top_frame;
3807 nat upd_frames=0, stop_frames=0, catch_frames=0, seq_frames=0,
3809 void printObj( StgClosure *obj ); // from Printer.c
3811 top_frame = tso->su;
3814 bottom = &(tso->stack[tso->stack_size]);
3817 /* There must be at least one frame, namely the STOP_FRAME.
3819 ASSERT((P_)frame < bottom);
3821 /* Walk down the stack, reversing the links between frames so that
3822 * we can walk back up as we squeeze from the bottom. Note that
3823 * next_frame and prev_frame refer to next and previous as they were
3824 * added to the stack, rather than the way we see them in this
3825 * walk. (It makes the next loop less confusing.)
3827 * Stop if we find an update frame pointing to a black hole
3828 * (see comment in threadLazyBlackHole()).
3832 // bottom - sizeof(StgStopFrame) is the STOP_FRAME
3833 while ((P_)frame < bottom - sizeofW(StgStopFrame)) {
3834 prev_frame = frame->link;
3835 frame->link = next_frame;
3840 if (!(frame>=top_frame && frame<=(StgUpdateFrame *)bottom)) {
3841 printObj((StgClosure *)prev_frame);
3842 barf("threadSqueezeStack: current frame is rubbish %p; previous was %p\n",
3845 switch (get_itbl(frame)->type) {
3848 if (frame->updatee->header.info == &stg_BLACKHOLE_info)
3861 barf("Found non-frame during stack squeezing at %p (prev frame was %p)\n",
3863 printObj((StgClosure *)prev_frame);
3866 if (get_itbl(frame)->type == UPDATE_FRAME
3867 && frame->updatee->header.info == &stg_BLACKHOLE_info) {
3872 /* Now, we're at the bottom. Frame points to the lowest update
3873 * frame on the stack, and its link actually points to the frame
3874 * above. We have to walk back up the stack, squeezing out empty
3875 * update frames and turning the pointers back around on the way
3878 * The bottom-most frame (the STOP_FRAME) has not been altered, and
3879 * we never want to eliminate it anyway. Just walk one step up
3880 * before starting to squeeze. When you get to the topmost frame,
3881 * remember that there are still some words above it that might have
3888 prev_was_update_frame = (get_itbl(prev_frame)->type == UPDATE_FRAME);
3891 * Loop through all of the frames (everything except the very
3892 * bottom). Things are complicated by the fact that we have
3893 * CATCH_FRAMEs and SEQ_FRAMEs interspersed with the update frames.
3894 * We can only squeeze when there are two consecutive UPDATE_FRAMEs.
3896 while (frame != NULL) {
3898 StgPtr frame_bottom = (P_)frame + sizeofW(StgUpdateFrame);
3899 rtsBool is_update_frame;
3901 next_frame = frame->link;
3902 is_update_frame = (get_itbl(frame)->type == UPDATE_FRAME);
3905 * 1. both the previous and current frame are update frames
3906 * 2. the current frame is empty
3908 if (prev_was_update_frame && is_update_frame &&
3909 (P_)prev_frame == frame_bottom + displacement) {
3911 // Now squeeze out the current frame
3912 StgClosure *updatee_keep = prev_frame->updatee;
3913 StgClosure *updatee_bypass = frame->updatee;
3916 IF_DEBUG(gc, belch("@@ squeezing frame at %p", frame));
3920 /* Deal with blocking queues. If both updatees have blocked
3921 * threads, then we should merge the queues into the update
3922 * frame that we're keeping.
3924 * Alternatively, we could just wake them up: they'll just go
3925 * straight to sleep on the proper blackhole! This is less code
3926 * and probably less bug prone, although it's probably much
3929 #if 0 // do it properly...
3930 # if (!defined(LAZY_BLACKHOLING)) && defined(DEBUG)
3931 # error Unimplemented lazy BH warning. (KSW 1999-01)
3933 if (GET_INFO(updatee_bypass) == stg_BLACKHOLE_BQ_info
3934 || GET_INFO(updatee_bypass) == stg_CAF_BLACKHOLE_info
3936 // Sigh. It has one. Don't lose those threads!
3937 if (GET_INFO(updatee_keep) == stg_BLACKHOLE_BQ_info) {
3938 // Urgh. Two queues. Merge them.
3939 P_ keep_tso = ((StgBlockingQueue *)updatee_keep)->blocking_queue;
3941 while (keep_tso->link != END_TSO_QUEUE) {
3942 keep_tso = keep_tso->link;
3944 keep_tso->link = ((StgBlockingQueue *)updatee_bypass)->blocking_queue;
3947 // For simplicity, just swap the BQ for the BH
3948 P_ temp = updatee_keep;
3950 updatee_keep = updatee_bypass;
3951 updatee_bypass = temp;
3953 // Record the swap in the kept frame (below)
3954 prev_frame->updatee = updatee_keep;
3959 TICK_UPD_SQUEEZED();
3960 /* wasn't there something about update squeezing and ticky to be
3961 * sorted out? oh yes: we aren't counting each enter properly
3962 * in this case. See the log somewhere. KSW 1999-04-21
3964 * Check two things: that the two update frames don't point to
3965 * the same object, and that the updatee_bypass isn't already an
3966 * indirection. Both of these cases only happen when we're in a
3967 * block hole-style loop (and there are multiple update frames
3968 * on the stack pointing to the same closure), but they can both
3969 * screw us up if we don't check.
3971 if (updatee_bypass != updatee_keep && !closure_IND(updatee_bypass)) {
3972 // this wakes the threads up
3973 UPD_IND_NOLOCK(updatee_bypass, updatee_keep);
3976 sp = (P_)frame - 1; // sp = stuff to slide
3977 displacement += sizeofW(StgUpdateFrame);
3980 // No squeeze for this frame
3981 sp = frame_bottom - 1; // Keep the current frame
3983 /* Do lazy black-holing.
3985 if (is_update_frame) {
3986 StgBlockingQueue *bh = (StgBlockingQueue *)frame->updatee;
3987 if (bh->header.info != &stg_BLACKHOLE_info &&
3988 bh->header.info != &stg_BLACKHOLE_BQ_info &&
3989 bh->header.info != &stg_CAF_BLACKHOLE_info) {
3990 #if (!defined(LAZY_BLACKHOLING)) && defined(DEBUG)
3991 belch("Unexpected lazy BHing required at 0x%04x",(int)bh);
3994 /* zero out the slop so that the sanity checker can tell
3995 * where the next closure is.
3998 StgInfoTable *info = get_itbl(bh);
3999 nat np = info->layout.payload.ptrs, nw = info->layout.payload.nptrs, i;
4000 /* don't zero out slop for a THUNK_SELECTOR, because its layout
4001 * info is used for a different purpose, and it's exactly the
4002 * same size as a BLACKHOLE in any case.
4004 if (info->type != THUNK_SELECTOR) {
4005 for (i = np; i < np + nw; i++) {
4006 ((StgClosure *)bh)->payload[i] = 0;
4013 // We pretend that bh is now dead.
4014 LDV_recordDead_FILL_SLOP_DYNAMIC((StgClosure *)bh);
4017 // Todo: maybe use SET_HDR() and remove LDV_recordCreate()?
4019 SET_INFO(bh,&stg_BLACKHOLE_info);
4022 // We pretend that bh has just been created.
4023 LDV_recordCreate(bh);
4028 // Fix the link in the current frame (should point to the frame below)
4029 frame->link = prev_frame;
4030 prev_was_update_frame = is_update_frame;
4033 // Now slide all words from sp up to the next frame
4035 if (displacement > 0) {
4036 P_ next_frame_bottom;
4038 if (next_frame != NULL)
4039 next_frame_bottom = (P_)next_frame + sizeofW(StgUpdateFrame);
4041 next_frame_bottom = tso->sp - 1;
4045 belch("sliding [%p, %p] by %ld", sp, next_frame_bottom,
4049 while (sp >= next_frame_bottom) {
4050 sp[displacement] = *sp;
4054 (P_)prev_frame = (P_)frame + displacement;
4058 tso->sp += displacement;
4059 tso->su = prev_frame;
4062 belch("@@ threadSqueezeStack: squeezed %d update-frames; found %d BHs; found %d update-, %d stop-, %d catch, %d seq-frames",
4063 squeezes, bhs, upd_frames, stop_frames, catch_frames, seq_frames))
4068 /* -----------------------------------------------------------------------------
4071 * We have to prepare for GC - this means doing lazy black holing
4072 * here. We also take the opportunity to do stack squeezing if it's
4074 * -------------------------------------------------------------------------- */
4076 threadPaused(StgTSO *tso)
4078 if ( RtsFlags.GcFlags.squeezeUpdFrames == rtsTrue )
4079 threadSqueezeStack(tso); // does black holing too
4081 threadLazyBlackHole(tso);
4084 /* -----------------------------------------------------------------------------
4086 * -------------------------------------------------------------------------- */
4090 printMutOnceList(generation *gen)
4092 StgMutClosure *p, *next;
4094 p = gen->mut_once_list;
4097 fprintf(stderr, "@@ Mut once list %p: ", gen->mut_once_list);
4098 for (; p != END_MUT_LIST; p = next, next = p->mut_link) {
4099 fprintf(stderr, "%p (%s), ",
4100 p, info_type((StgClosure *)p));
4102 fputc('\n', stderr);
4106 printMutableList(generation *gen)
4108 StgMutClosure *p, *next;
4113 fprintf(stderr, "@@ Mutable list %p: ", gen->mut_list);
4114 for (; p != END_MUT_LIST; p = next, next = p->mut_link) {
4115 fprintf(stderr, "%p (%s), ",
4116 p, info_type((StgClosure *)p));
4118 fputc('\n', stderr);
4121 static inline rtsBool
4122 maybeLarge(StgClosure *closure)
4124 StgInfoTable *info = get_itbl(closure);
4126 /* closure types that may be found on the new_large_objects list;
4127 see scavenge_large */
4128 return (info->type == MUT_ARR_PTRS ||
4129 info->type == MUT_ARR_PTRS_FROZEN ||
4130 info->type == TSO ||
4131 info->type == ARR_WORDS);