1 /* -----------------------------------------------------------------------------
2 * $Id: GC.c,v 1.132 2002/03/12 11:50:02 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"
48 /* STATIC OBJECT LIST.
51 * We maintain a linked list of static objects that are still live.
52 * The requirements for this list are:
54 * - we need to scan the list while adding to it, in order to
55 * scavenge all the static objects (in the same way that
56 * breadth-first scavenging works for dynamic objects).
58 * - we need to be able to tell whether an object is already on
59 * the list, to break loops.
61 * Each static object has a "static link field", which we use for
62 * linking objects on to the list. We use a stack-type list, consing
63 * objects on the front as they are added (this means that the
64 * scavenge phase is depth-first, not breadth-first, but that
67 * A separate list is kept for objects that have been scavenged
68 * already - this is so that we can zero all the marks afterwards.
70 * An object is on the list if its static link field is non-zero; this
71 * means that we have to mark the end of the list with '1', not NULL.
73 * Extra notes for generational GC:
75 * Each generation has a static object list associated with it. When
76 * collecting generations up to N, we treat the static object lists
77 * from generations > N as roots.
79 * We build up a static object list while collecting generations 0..N,
80 * which is then appended to the static object list of generation N+1.
82 StgClosure* static_objects; // live static objects
83 StgClosure* scavenged_static_objects; // static objects scavenged so far
85 /* N is the oldest generation being collected, where the generations
86 * are numbered starting at 0. A major GC (indicated by the major_gc
87 * flag) is when we're collecting all generations. We only attempt to
88 * deal with static objects and GC CAFs when doing a major GC.
91 static rtsBool major_gc;
93 /* Youngest generation that objects should be evacuated to in
94 * evacuate(). (Logically an argument to evacuate, but it's static
95 * a lot of the time so we optimise it into a global variable).
101 StgWeak *old_weak_ptr_list; // also pending finaliser list
103 /* Which stage of processing various kinds of weak pointer are we at?
104 * (see traverse_weak_ptr_list() below for discussion).
106 typedef enum { WeakPtrs, WeakThreads, WeakDone } WeakStage;
107 static WeakStage weak_stage;
109 /* List of all threads during GC
111 static StgTSO *old_all_threads;
112 StgTSO *resurrected_threads;
114 /* Flag indicating failure to evacuate an object to the desired
117 static rtsBool failed_to_evac;
119 /* Old to-space (used for two-space collector only)
121 bdescr *old_to_blocks;
123 /* Data used for allocation area sizing.
125 lnat new_blocks; // blocks allocated during this GC
126 lnat g0s0_pcnt_kept = 30; // percentage of g0s0 live at last minor GC
128 /* Used to avoid long recursion due to selector thunks
130 lnat thunk_selector_depth = 0;
131 #define MAX_THUNK_SELECTOR_DEPTH 256
133 /* -----------------------------------------------------------------------------
134 Static function declarations
135 -------------------------------------------------------------------------- */
137 static void mark_root ( StgClosure **root );
138 static StgClosure * evacuate ( StgClosure *q );
139 static void zero_static_object_list ( StgClosure* first_static );
140 static void zero_mutable_list ( StgMutClosure *first );
142 static rtsBool traverse_weak_ptr_list ( void );
143 static void mark_weak_ptr_list ( StgWeak **list );
145 static void scavenge ( step * );
146 static void scavenge_mark_stack ( void );
147 static void scavenge_stack ( StgPtr p, StgPtr stack_end );
148 static rtsBool scavenge_one ( StgPtr p );
149 static void scavenge_large ( step * );
150 static void scavenge_static ( void );
151 static void scavenge_mutable_list ( generation *g );
152 static void scavenge_mut_once_list ( generation *g );
154 #if 0 && defined(DEBUG)
155 static void gcCAFs ( void );
158 /* -----------------------------------------------------------------------------
159 inline functions etc. for dealing with the mark bitmap & stack.
160 -------------------------------------------------------------------------- */
162 #define MARK_STACK_BLOCKS 4
164 static bdescr *mark_stack_bdescr;
165 static StgPtr *mark_stack;
166 static StgPtr *mark_sp;
167 static StgPtr *mark_splim;
169 // Flag and pointers used for falling back to a linear scan when the
170 // mark stack overflows.
171 static rtsBool mark_stack_overflowed;
172 static bdescr *oldgen_scan_bd;
173 static StgPtr oldgen_scan;
175 static inline rtsBool
176 mark_stack_empty(void)
178 return mark_sp == mark_stack;
181 static inline rtsBool
182 mark_stack_full(void)
184 return mark_sp >= mark_splim;
188 reset_mark_stack(void)
190 mark_sp = mark_stack;
194 push_mark_stack(StgPtr p)
205 /* -----------------------------------------------------------------------------
208 For garbage collecting generation N (and all younger generations):
210 - follow all pointers in the root set. the root set includes all
211 mutable objects in all steps in all generations.
213 - for each pointer, evacuate the object it points to into either
214 + to-space in the next higher step in that generation, if one exists,
215 + if the object's generation == N, then evacuate it to the next
216 generation if one exists, or else to-space in the current
218 + if the object's generation < N, then evacuate it to to-space
219 in the next generation.
221 - repeatedly scavenge to-space from each step in each generation
222 being collected until no more objects can be evacuated.
224 - free from-space in each step, and set from-space = to-space.
226 Locks held: sched_mutex
228 -------------------------------------------------------------------------- */
231 GarbageCollect ( void (*get_roots)(evac_fn), rtsBool force_major_gc )
235 lnat live, allocated, collected = 0, copied = 0;
236 lnat oldgen_saved_blocks = 0;
240 CostCentreStack *prev_CCS;
243 #if defined(DEBUG) && defined(GRAN)
244 IF_DEBUG(gc, belch("@@ Starting garbage collection at %ld (%lx)\n",
248 // tell the stats department that we've started a GC
251 // Init stats and print par specific (timing) info
252 PAR_TICKY_PAR_START();
254 // attribute any costs to CCS_GC
260 /* Approximate how much we allocated.
261 * Todo: only when generating stats?
263 allocated = calcAllocated();
265 /* Figure out which generation to collect
267 if (force_major_gc) {
268 N = RtsFlags.GcFlags.generations - 1;
272 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
273 if (generations[g].steps[0].n_blocks +
274 generations[g].steps[0].n_large_blocks
275 >= generations[g].max_blocks) {
279 major_gc = (N == RtsFlags.GcFlags.generations-1);
282 #ifdef RTS_GTK_FRONTPANEL
283 if (RtsFlags.GcFlags.frontpanel) {
284 updateFrontPanelBeforeGC(N);
288 // check stack sanity *before* GC (ToDo: check all threads)
290 // ToDo!: check sanity IF_DEBUG(sanity, checkTSOsSanity());
292 IF_DEBUG(sanity, checkFreeListSanity());
294 /* Initialise the static object lists
296 static_objects = END_OF_STATIC_LIST;
297 scavenged_static_objects = END_OF_STATIC_LIST;
299 /* zero the mutable list for the oldest generation (see comment by
300 * zero_mutable_list below).
303 zero_mutable_list(generations[RtsFlags.GcFlags.generations-1].mut_once_list);
306 /* Save the old to-space if we're doing a two-space collection
308 if (RtsFlags.GcFlags.generations == 1) {
309 old_to_blocks = g0s0->to_blocks;
310 g0s0->to_blocks = NULL;
313 /* Keep a count of how many new blocks we allocated during this GC
314 * (used for resizing the allocation area, later).
318 /* Initialise to-space in all the generations/steps that we're
321 for (g = 0; g <= N; g++) {
322 generations[g].mut_once_list = END_MUT_LIST;
323 generations[g].mut_list = END_MUT_LIST;
325 for (s = 0; s < generations[g].n_steps; s++) {
327 // generation 0, step 0 doesn't need to-space
328 if (g == 0 && s == 0 && RtsFlags.GcFlags.generations > 1) {
332 /* Get a free block for to-space. Extra blocks will be chained on
336 stp = &generations[g].steps[s];
337 ASSERT(stp->gen_no == g);
338 ASSERT(stp->hp ? Bdescr(stp->hp)->step == stp : rtsTrue);
342 bd->flags = BF_EVACUATED; // it's a to-space block
344 stp->hpLim = stp->hp + BLOCK_SIZE_W;
347 stp->n_to_blocks = 1;
348 stp->scan = bd->start;
350 stp->new_large_objects = NULL;
351 stp->scavenged_large_objects = NULL;
352 stp->n_scavenged_large_blocks = 0;
354 // mark the large objects as not evacuated yet
355 for (bd = stp->large_objects; bd; bd = bd->link) {
356 bd->flags = BF_LARGE;
359 // for a compacted step, we need to allocate the bitmap
360 if (stp->is_compacted) {
361 nat bitmap_size; // in bytes
362 bdescr *bitmap_bdescr;
365 bitmap_size = stp->n_blocks * BLOCK_SIZE / (sizeof(W_)*BITS_PER_BYTE);
367 if (bitmap_size > 0) {
368 bitmap_bdescr = allocGroup((nat)BLOCK_ROUND_UP(bitmap_size)
370 stp->bitmap = bitmap_bdescr;
371 bitmap = bitmap_bdescr->start;
373 IF_DEBUG(gc, belch("bitmap_size: %d, bitmap: %p",
374 bitmap_size, bitmap););
376 // don't forget to fill it with zeros!
377 memset(bitmap, 0, bitmap_size);
379 // for each block in this step, point to its bitmap from the
381 for (bd=stp->blocks; bd != NULL; bd = bd->link) {
382 bd->u.bitmap = bitmap;
383 bitmap += BLOCK_SIZE_W / (sizeof(W_)*BITS_PER_BYTE);
390 /* make sure the older generations have at least one block to
391 * allocate into (this makes things easier for copy(), see below.
393 for (g = N+1; g < RtsFlags.GcFlags.generations; g++) {
394 for (s = 0; s < generations[g].n_steps; s++) {
395 stp = &generations[g].steps[s];
396 if (stp->hp_bd == NULL) {
397 ASSERT(stp->blocks == NULL);
402 bd->flags = 0; // *not* a to-space block or a large object
404 stp->hpLim = stp->hp + BLOCK_SIZE_W;
410 /* Set the scan pointer for older generations: remember we
411 * still have to scavenge objects that have been promoted. */
413 stp->scan_bd = stp->hp_bd;
414 stp->to_blocks = NULL;
415 stp->n_to_blocks = 0;
416 stp->new_large_objects = NULL;
417 stp->scavenged_large_objects = NULL;
418 stp->n_scavenged_large_blocks = 0;
422 /* Allocate a mark stack if we're doing a major collection.
425 mark_stack_bdescr = allocGroup(MARK_STACK_BLOCKS);
426 mark_stack = (StgPtr *)mark_stack_bdescr->start;
427 mark_sp = mark_stack;
428 mark_splim = mark_stack + (MARK_STACK_BLOCKS * BLOCK_SIZE_W);
430 mark_stack_bdescr = NULL;
433 /* -----------------------------------------------------------------------
434 * follow all the roots that we know about:
435 * - mutable lists from each generation > N
436 * we want to *scavenge* these roots, not evacuate them: they're not
437 * going to move in this GC.
438 * Also: do them in reverse generation order. This is because we
439 * often want to promote objects that are pointed to by older
440 * generations early, so we don't have to repeatedly copy them.
441 * Doing the generations in reverse order ensures that we don't end
442 * up in the situation where we want to evac an object to gen 3 and
443 * it has already been evaced to gen 2.
447 for (g = RtsFlags.GcFlags.generations-1; g > N; g--) {
448 generations[g].saved_mut_list = generations[g].mut_list;
449 generations[g].mut_list = END_MUT_LIST;
452 // Do the mut-once lists first
453 for (g = RtsFlags.GcFlags.generations-1; g > N; g--) {
454 IF_PAR_DEBUG(verbose,
455 printMutOnceList(&generations[g]));
456 scavenge_mut_once_list(&generations[g]);
458 for (st = generations[g].n_steps-1; st >= 0; st--) {
459 scavenge(&generations[g].steps[st]);
463 for (g = RtsFlags.GcFlags.generations-1; g > N; g--) {
464 IF_PAR_DEBUG(verbose,
465 printMutableList(&generations[g]));
466 scavenge_mutable_list(&generations[g]);
468 for (st = generations[g].n_steps-1; st >= 0; st--) {
469 scavenge(&generations[g].steps[st]);
474 /* follow roots from the CAF list (used by GHCi)
479 /* follow all the roots that the application knows about.
482 get_roots(mark_root);
485 /* And don't forget to mark the TSO if we got here direct from
487 /* Not needed in a seq version?
489 CurrentTSO = (StgTSO *)MarkRoot((StgClosure *)CurrentTSO);
493 // Mark the entries in the GALA table of the parallel system
494 markLocalGAs(major_gc);
495 // Mark all entries on the list of pending fetches
496 markPendingFetches(major_gc);
499 /* Mark the weak pointer list, and prepare to detect dead weak
502 mark_weak_ptr_list(&weak_ptr_list);
503 old_weak_ptr_list = weak_ptr_list;
504 weak_ptr_list = NULL;
505 weak_stage = WeakPtrs;
507 /* The all_threads list is like the weak_ptr_list.
508 * See traverse_weak_ptr_list() for the details.
510 old_all_threads = all_threads;
511 all_threads = END_TSO_QUEUE;
512 resurrected_threads = END_TSO_QUEUE;
514 /* Mark the stable pointer table.
516 markStablePtrTable(mark_root);
520 /* ToDo: To fix the caf leak, we need to make the commented out
521 * parts of this code do something sensible - as described in
524 extern void markHugsObjects(void);
529 /* -------------------------------------------------------------------------
530 * Repeatedly scavenge all the areas we know about until there's no
531 * more scavenging to be done.
538 // scavenge static objects
539 if (major_gc && static_objects != END_OF_STATIC_LIST) {
540 IF_DEBUG(sanity, checkStaticObjects(static_objects));
544 /* When scavenging the older generations: Objects may have been
545 * evacuated from generations <= N into older generations, and we
546 * need to scavenge these objects. We're going to try to ensure that
547 * any evacuations that occur move the objects into at least the
548 * same generation as the object being scavenged, otherwise we
549 * have to create new entries on the mutable list for the older
553 // scavenge each step in generations 0..maxgen
559 // scavenge objects in compacted generation
560 if (mark_stack_overflowed || oldgen_scan_bd != NULL ||
561 (mark_stack_bdescr != NULL && !mark_stack_empty())) {
562 scavenge_mark_stack();
566 for (gen = RtsFlags.GcFlags.generations; --gen >= 0; ) {
567 for (st = generations[gen].n_steps; --st >= 0; ) {
568 if (gen == 0 && st == 0 && RtsFlags.GcFlags.generations > 1) {
571 stp = &generations[gen].steps[st];
573 if (stp->hp_bd != stp->scan_bd || stp->scan < stp->hp) {
578 if (stp->new_large_objects != NULL) {
587 if (flag) { goto loop; }
589 // must be last... invariant is that everything is fully
590 // scavenged at this point.
591 if (traverse_weak_ptr_list()) { // returns rtsTrue if evaced something
596 /* Update the pointers from the "main thread" list - these are
597 * treated as weak pointers because we want to allow a main thread
598 * to get a BlockedOnDeadMVar exception in the same way as any other
599 * thread. Note that the threads should all have been retained by
600 * GC by virtue of being on the all_threads list, we're just
601 * updating pointers here.
606 for (m = main_threads; m != NULL; m = m->link) {
607 tso = (StgTSO *) isAlive((StgClosure *)m->tso);
609 barf("main thread has been GC'd");
616 // Reconstruct the Global Address tables used in GUM
617 rebuildGAtables(major_gc);
618 IF_DEBUG(sanity, checkLAGAtable(rtsTrue/*check closures, too*/));
621 // Now see which stable names are still alive.
624 // Tidy the end of the to-space chains
625 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
626 for (s = 0; s < generations[g].n_steps; s++) {
627 stp = &generations[g].steps[s];
628 if (!(g == 0 && s == 0 && RtsFlags.GcFlags.generations > 1)) {
629 stp->hp_bd->free = stp->hp;
630 stp->hp_bd->link = NULL;
636 // We call processHeapClosureForDead() on every closure destroyed during
637 // the current garbage collection, so we invoke LdvCensusForDead().
638 if (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_LDV
639 || RtsFlags.ProfFlags.bioSelector != NULL)
643 // NO MORE EVACUATION AFTER THIS POINT!
644 // Finally: compaction of the oldest generation.
645 if (major_gc && oldest_gen->steps[0].is_compacted) {
646 // save number of blocks for stats
647 oldgen_saved_blocks = oldest_gen->steps[0].n_blocks;
651 IF_DEBUG(sanity, checkGlobalTSOList(rtsFalse));
653 /* run through all the generations/steps and tidy up
655 copied = new_blocks * BLOCK_SIZE_W;
656 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
659 generations[g].collections++; // for stats
662 for (s = 0; s < generations[g].n_steps; s++) {
664 stp = &generations[g].steps[s];
666 if (!(g == 0 && s == 0 && RtsFlags.GcFlags.generations > 1)) {
667 // stats information: how much we copied
669 copied -= stp->hp_bd->start + BLOCK_SIZE_W -
674 // for generations we collected...
677 // rough calculation of garbage collected, for stats output
678 if (stp->is_compacted) {
679 collected += (oldgen_saved_blocks - stp->n_blocks) * BLOCK_SIZE_W;
681 collected += stp->n_blocks * BLOCK_SIZE_W;
684 /* free old memory and shift to-space into from-space for all
685 * the collected steps (except the allocation area). These
686 * freed blocks will probaby be quickly recycled.
688 if (!(g == 0 && s == 0)) {
689 if (stp->is_compacted) {
690 // for a compacted step, just shift the new to-space
691 // onto the front of the now-compacted existing blocks.
692 for (bd = stp->to_blocks; bd != NULL; bd = bd->link) {
693 bd->flags &= ~BF_EVACUATED; // now from-space
695 // tack the new blocks on the end of the existing blocks
696 if (stp->blocks == NULL) {
697 stp->blocks = stp->to_blocks;
699 for (bd = stp->blocks; bd != NULL; bd = next) {
702 bd->link = stp->to_blocks;
706 // add the new blocks to the block tally
707 stp->n_blocks += stp->n_to_blocks;
709 freeChain(stp->blocks);
710 stp->blocks = stp->to_blocks;
711 stp->n_blocks = stp->n_to_blocks;
712 for (bd = stp->blocks; bd != NULL; bd = bd->link) {
713 bd->flags &= ~BF_EVACUATED; // now from-space
716 stp->to_blocks = NULL;
717 stp->n_to_blocks = 0;
720 /* LARGE OBJECTS. The current live large objects are chained on
721 * scavenged_large, having been moved during garbage
722 * collection from large_objects. Any objects left on
723 * large_objects list are therefore dead, so we free them here.
725 for (bd = stp->large_objects; bd != NULL; bd = next) {
731 // update the count of blocks used by large objects
732 for (bd = stp->scavenged_large_objects; bd != NULL; bd = bd->link) {
733 bd->flags &= ~BF_EVACUATED;
735 stp->large_objects = stp->scavenged_large_objects;
736 stp->n_large_blocks = stp->n_scavenged_large_blocks;
739 // for older generations...
741 /* For older generations, we need to append the
742 * scavenged_large_object list (i.e. large objects that have been
743 * promoted during this GC) to the large_object list for that step.
745 for (bd = stp->scavenged_large_objects; bd; bd = next) {
747 bd->flags &= ~BF_EVACUATED;
748 dbl_link_onto(bd, &stp->large_objects);
751 // add the new blocks we promoted during this GC
752 stp->n_blocks += stp->n_to_blocks;
753 stp->n_large_blocks += stp->n_scavenged_large_blocks;
758 /* Reset the sizes of the older generations when we do a major
761 * CURRENT STRATEGY: make all generations except zero the same size.
762 * We have to stay within the maximum heap size, and leave a certain
763 * percentage of the maximum heap size available to allocate into.
765 if (major_gc && RtsFlags.GcFlags.generations > 1) {
766 nat live, size, min_alloc;
767 nat max = RtsFlags.GcFlags.maxHeapSize;
768 nat gens = RtsFlags.GcFlags.generations;
770 // live in the oldest generations
771 live = oldest_gen->steps[0].n_blocks +
772 oldest_gen->steps[0].n_large_blocks;
774 // default max size for all generations except zero
775 size = stg_max(live * RtsFlags.GcFlags.oldGenFactor,
776 RtsFlags.GcFlags.minOldGenSize);
778 // minimum size for generation zero
779 min_alloc = stg_max((RtsFlags.GcFlags.pcFreeHeap * max) / 200,
780 RtsFlags.GcFlags.minAllocAreaSize);
782 // Auto-enable compaction when the residency reaches a
783 // certain percentage of the maximum heap size (default: 30%).
784 if (RtsFlags.GcFlags.generations > 1 &&
785 (RtsFlags.GcFlags.compact ||
787 oldest_gen->steps[0].n_blocks >
788 (RtsFlags.GcFlags.compactThreshold * max) / 100))) {
789 oldest_gen->steps[0].is_compacted = 1;
790 // fprintf(stderr,"compaction: on\n", live);
792 oldest_gen->steps[0].is_compacted = 0;
793 // fprintf(stderr,"compaction: off\n", live);
796 // if we're going to go over the maximum heap size, reduce the
797 // size of the generations accordingly. The calculation is
798 // different if compaction is turned on, because we don't need
799 // to double the space required to collect the old generation.
802 // this test is necessary to ensure that the calculations
803 // below don't have any negative results - we're working
804 // with unsigned values here.
805 if (max < min_alloc) {
809 if (oldest_gen->steps[0].is_compacted) {
810 if ( (size + (size - 1) * (gens - 2) * 2) + min_alloc > max ) {
811 size = (max - min_alloc) / ((gens - 1) * 2 - 1);
814 if ( (size * (gens - 1) * 2) + min_alloc > max ) {
815 size = (max - min_alloc) / ((gens - 1) * 2);
825 fprintf(stderr,"live: %d, min_alloc: %d, size : %d, max = %d\n", live,
826 min_alloc, size, max);
829 for (g = 0; g < gens; g++) {
830 generations[g].max_blocks = size;
834 // Guess the amount of live data for stats.
837 /* Free the small objects allocated via allocate(), since this will
838 * all have been copied into G0S1 now.
840 if (small_alloc_list != NULL) {
841 freeChain(small_alloc_list);
843 small_alloc_list = NULL;
847 alloc_blocks_lim = RtsFlags.GcFlags.minAllocAreaSize;
849 // Start a new pinned_object_block
850 pinned_object_block = NULL;
852 /* Free the mark stack.
854 if (mark_stack_bdescr != NULL) {
855 freeGroup(mark_stack_bdescr);
860 for (g = 0; g <= N; g++) {
861 for (s = 0; s < generations[g].n_steps; s++) {
862 stp = &generations[g].steps[s];
863 if (stp->is_compacted && stp->bitmap != NULL) {
864 freeGroup(stp->bitmap);
869 /* Two-space collector:
870 * Free the old to-space, and estimate the amount of live data.
872 if (RtsFlags.GcFlags.generations == 1) {
875 if (old_to_blocks != NULL) {
876 freeChain(old_to_blocks);
878 for (bd = g0s0->to_blocks; bd != NULL; bd = bd->link) {
879 bd->flags = 0; // now from-space
882 /* For a two-space collector, we need to resize the nursery. */
884 /* set up a new nursery. Allocate a nursery size based on a
885 * function of the amount of live data (by default a factor of 2)
886 * Use the blocks from the old nursery if possible, freeing up any
889 * If we get near the maximum heap size, then adjust our nursery
890 * size accordingly. If the nursery is the same size as the live
891 * data (L), then we need 3L bytes. We can reduce the size of the
892 * nursery to bring the required memory down near 2L bytes.
894 * A normal 2-space collector would need 4L bytes to give the same
895 * performance we get from 3L bytes, reducing to the same
896 * performance at 2L bytes.
898 blocks = g0s0->n_to_blocks;
900 if ( RtsFlags.GcFlags.maxHeapSize != 0 &&
901 blocks * RtsFlags.GcFlags.oldGenFactor * 2 >
902 RtsFlags.GcFlags.maxHeapSize ) {
903 long adjusted_blocks; // signed on purpose
906 adjusted_blocks = (RtsFlags.GcFlags.maxHeapSize - 2 * blocks);
907 IF_DEBUG(gc, belch("@@ Near maximum heap size of 0x%x blocks, blocks = %d, adjusted to %ld", RtsFlags.GcFlags.maxHeapSize, blocks, adjusted_blocks));
908 pc_free = adjusted_blocks * 100 / RtsFlags.GcFlags.maxHeapSize;
909 if (pc_free < RtsFlags.GcFlags.pcFreeHeap) /* might even be < 0 */ {
912 blocks = adjusted_blocks;
915 blocks *= RtsFlags.GcFlags.oldGenFactor;
916 if (blocks < RtsFlags.GcFlags.minAllocAreaSize) {
917 blocks = RtsFlags.GcFlags.minAllocAreaSize;
920 resizeNursery(blocks);
923 /* Generational collector:
924 * If the user has given us a suggested heap size, adjust our
925 * allocation area to make best use of the memory available.
928 if (RtsFlags.GcFlags.heapSizeSuggestion) {
930 nat needed = calcNeeded(); // approx blocks needed at next GC
932 /* Guess how much will be live in generation 0 step 0 next time.
933 * A good approximation is obtained by finding the
934 * percentage of g0s0 that was live at the last minor GC.
937 g0s0_pcnt_kept = (new_blocks * 100) / g0s0->n_blocks;
940 /* Estimate a size for the allocation area based on the
941 * information available. We might end up going slightly under
942 * or over the suggested heap size, but we should be pretty
945 * Formula: suggested - needed
946 * ----------------------------
947 * 1 + g0s0_pcnt_kept/100
949 * where 'needed' is the amount of memory needed at the next
950 * collection for collecting all steps except g0s0.
953 (((long)RtsFlags.GcFlags.heapSizeSuggestion - (long)needed) * 100) /
954 (100 + (long)g0s0_pcnt_kept);
956 if (blocks < (long)RtsFlags.GcFlags.minAllocAreaSize) {
957 blocks = RtsFlags.GcFlags.minAllocAreaSize;
960 resizeNursery((nat)blocks);
963 // we might have added extra large blocks to the nursery, so
964 // resize back to minAllocAreaSize again.
965 resizeNursery(RtsFlags.GcFlags.minAllocAreaSize);
969 // mark the garbage collected CAFs as dead
970 #if 0 && defined(DEBUG) // doesn't work at the moment
971 if (major_gc) { gcCAFs(); }
975 // resetStaticObjectForRetainerProfiling() must be called before
977 resetStaticObjectForRetainerProfiling();
980 // zero the scavenged static object list
982 zero_static_object_list(scavenged_static_objects);
988 // let go of lock (so that it can be re-grabbed below).
989 RELEASE_LOCK(&sched_mutex);
991 // start any pending finalizers
992 scheduleFinalizers(old_weak_ptr_list);
994 // send exceptions to any threads which were about to die
995 resurrectThreads(resurrected_threads);
997 ACQUIRE_LOCK(&sched_mutex);
999 // Update the stable pointer hash table.
1000 updateStablePtrTable(major_gc);
1002 // check sanity after GC
1003 IF_DEBUG(sanity, checkSanity());
1005 // extra GC trace info
1006 IF_DEBUG(gc, statDescribeGens());
1009 // symbol-table based profiling
1010 /* heapCensus(to_blocks); */ /* ToDo */
1013 // restore enclosing cost centre
1018 // check for memory leaks if sanity checking is on
1019 IF_DEBUG(sanity, memInventory());
1021 #ifdef RTS_GTK_FRONTPANEL
1022 if (RtsFlags.GcFlags.frontpanel) {
1023 updateFrontPanelAfterGC( N, live );
1027 // ok, GC over: tell the stats department what happened.
1028 stat_endGC(allocated, collected, live, copied, N);
1034 /* -----------------------------------------------------------------------------
1037 traverse_weak_ptr_list is called possibly many times during garbage
1038 collection. It returns a flag indicating whether it did any work
1039 (i.e. called evacuate on any live pointers).
1041 Invariant: traverse_weak_ptr_list is called when the heap is in an
1042 idempotent state. That means that there are no pending
1043 evacuate/scavenge operations. This invariant helps the weak
1044 pointer code decide which weak pointers are dead - if there are no
1045 new live weak pointers, then all the currently unreachable ones are
1048 For generational GC: we just don't try to finalize weak pointers in
1049 older generations than the one we're collecting. This could
1050 probably be optimised by keeping per-generation lists of weak
1051 pointers, but for a few weak pointers this scheme will work.
1053 There are three distinct stages to processing weak pointers:
1055 - weak_stage == WeakPtrs
1057 We process all the weak pointers whos keys are alive (evacuate
1058 their values and finalizers), and repeat until we can find no new
1059 live keys. If no live keys are found in this pass, then we
1060 evacuate the finalizers of all the dead weak pointers in order to
1063 - weak_stage == WeakThreads
1065 Now, we discover which *threads* are still alive. Pointers to
1066 threads from the all_threads and main thread lists are the
1067 weakest of all: a pointers from the finalizer of a dead weak
1068 pointer can keep a thread alive. Any threads found to be unreachable
1069 are evacuated and placed on the resurrected_threads list so we
1070 can send them a signal later.
1072 - weak_stage == WeakDone
1074 No more evacuation is done.
1076 -------------------------------------------------------------------------- */
1079 traverse_weak_ptr_list(void)
1081 StgWeak *w, **last_w, *next_w;
1083 rtsBool flag = rtsFalse;
1085 switch (weak_stage) {
1091 /* doesn't matter where we evacuate values/finalizers to, since
1092 * these pointers are treated as roots (iff the keys are alive).
1096 last_w = &old_weak_ptr_list;
1097 for (w = old_weak_ptr_list; w != NULL; w = next_w) {
1099 /* There might be a DEAD_WEAK on the list if finalizeWeak# was
1100 * called on a live weak pointer object. Just remove it.
1102 if (w->header.info == &stg_DEAD_WEAK_info) {
1103 next_w = ((StgDeadWeak *)w)->link;
1108 ASSERT(get_itbl(w)->type == WEAK);
1110 /* Now, check whether the key is reachable.
1112 new = isAlive(w->key);
1115 // evacuate the value and finalizer
1116 w->value = evacuate(w->value);
1117 w->finalizer = evacuate(w->finalizer);
1118 // remove this weak ptr from the old_weak_ptr list
1120 // and put it on the new weak ptr list
1122 w->link = weak_ptr_list;
1125 IF_DEBUG(weak, belch("Weak pointer still alive at %p -> %p",
1130 last_w = &(w->link);
1136 /* If we didn't make any changes, then we can go round and kill all
1137 * the dead weak pointers. The old_weak_ptr list is used as a list
1138 * of pending finalizers later on.
1140 if (flag == rtsFalse) {
1141 for (w = old_weak_ptr_list; w; w = w->link) {
1142 w->finalizer = evacuate(w->finalizer);
1145 // Next, move to the WeakThreads stage after fully
1146 // scavenging the finalizers we've just evacuated.
1147 weak_stage = WeakThreads;
1153 /* Now deal with the all_threads list, which behaves somewhat like
1154 * the weak ptr list. If we discover any threads that are about to
1155 * become garbage, we wake them up and administer an exception.
1158 StgTSO *t, *tmp, *next, **prev;
1160 prev = &old_all_threads;
1161 for (t = old_all_threads; t != END_TSO_QUEUE; t = next) {
1163 (StgClosure *)tmp = isAlive((StgClosure *)t);
1169 ASSERT(get_itbl(t)->type == TSO);
1170 switch (t->what_next) {
1171 case ThreadRelocated:
1176 case ThreadComplete:
1177 // finshed or died. The thread might still be alive, but we
1178 // don't keep it on the all_threads list. Don't forget to
1179 // stub out its global_link field.
1180 next = t->global_link;
1181 t->global_link = END_TSO_QUEUE;
1189 // not alive (yet): leave this thread on the
1190 // old_all_threads list.
1191 prev = &(t->global_link);
1192 next = t->global_link;
1195 // alive: move this thread onto the all_threads list.
1196 next = t->global_link;
1197 t->global_link = all_threads;
1204 /* And resurrect any threads which were about to become garbage.
1207 StgTSO *t, *tmp, *next;
1208 for (t = old_all_threads; t != END_TSO_QUEUE; t = next) {
1209 next = t->global_link;
1210 (StgClosure *)tmp = evacuate((StgClosure *)t);
1211 tmp->global_link = resurrected_threads;
1212 resurrected_threads = tmp;
1216 weak_stage = WeakDone; // *now* we're done,
1217 return rtsTrue; // but one more round of scavenging, please
1220 barf("traverse_weak_ptr_list");
1225 /* -----------------------------------------------------------------------------
1226 After GC, the live weak pointer list may have forwarding pointers
1227 on it, because a weak pointer object was evacuated after being
1228 moved to the live weak pointer list. We remove those forwarding
1231 Also, we don't consider weak pointer objects to be reachable, but
1232 we must nevertheless consider them to be "live" and retain them.
1233 Therefore any weak pointer objects which haven't as yet been
1234 evacuated need to be evacuated now.
1235 -------------------------------------------------------------------------- */
1239 mark_weak_ptr_list ( StgWeak **list )
1241 StgWeak *w, **last_w;
1244 for (w = *list; w; w = w->link) {
1245 (StgClosure *)w = evacuate((StgClosure *)w);
1247 last_w = &(w->link);
1251 /* -----------------------------------------------------------------------------
1252 isAlive determines whether the given closure is still alive (after
1253 a garbage collection) or not. It returns the new address of the
1254 closure if it is alive, or NULL otherwise.
1256 NOTE: Use it before compaction only!
1257 -------------------------------------------------------------------------- */
1261 isAlive(StgClosure *p)
1263 const StgInfoTable *info;
1270 /* ToDo: for static closures, check the static link field.
1271 * Problem here is that we sometimes don't set the link field, eg.
1272 * for static closures with an empty SRT or CONSTR_STATIC_NOCAFs.
1277 // ignore closures in generations that we're not collecting.
1278 if (LOOKS_LIKE_STATIC(p) || bd->gen_no > N) {
1281 // large objects have an evacuated flag
1282 if (bd->flags & BF_LARGE) {
1283 if (bd->flags & BF_EVACUATED) {
1289 // check the mark bit for compacted steps
1290 if (bd->step->is_compacted && is_marked((P_)p,bd)) {
1294 switch (info->type) {
1299 case IND_OLDGEN: // rely on compatible layout with StgInd
1300 case IND_OLDGEN_PERM:
1301 // follow indirections
1302 p = ((StgInd *)p)->indirectee;
1307 return ((StgEvacuated *)p)->evacuee;
1310 if (((StgTSO *)p)->what_next == ThreadRelocated) {
1311 p = (StgClosure *)((StgTSO *)p)->link;
1323 mark_root(StgClosure **root)
1325 *root = evacuate(*root);
1331 bdescr *bd = allocBlock();
1332 bd->gen_no = stp->gen_no;
1335 if (stp->gen_no <= N) {
1336 bd->flags = BF_EVACUATED;
1341 stp->hp_bd->free = stp->hp;
1342 stp->hp_bd->link = bd;
1343 stp->hp = bd->start;
1344 stp->hpLim = stp->hp + BLOCK_SIZE_W;
1351 static __inline__ void
1352 upd_evacuee(StgClosure *p, StgClosure *dest)
1354 p->header.info = &stg_EVACUATED_info;
1355 ((StgEvacuated *)p)->evacuee = dest;
1359 static __inline__ StgClosure *
1360 copy(StgClosure *src, nat size, step *stp)
1365 nat size_org = size;
1368 TICK_GC_WORDS_COPIED(size);
1369 /* Find out where we're going, using the handy "to" pointer in
1370 * the step of the source object. If it turns out we need to
1371 * evacuate to an older generation, adjust it here (see comment
1374 if (stp->gen_no < evac_gen) {
1375 #ifdef NO_EAGER_PROMOTION
1376 failed_to_evac = rtsTrue;
1378 stp = &generations[evac_gen].steps[0];
1382 /* chain a new block onto the to-space for the destination step if
1385 if (stp->hp + size >= stp->hpLim) {
1389 for(to = stp->hp, from = (P_)src; size>0; --size) {
1395 upd_evacuee(src,(StgClosure *)dest);
1397 // We store the size of the just evacuated object in the LDV word so that
1398 // the profiler can guess the position of the next object later.
1399 SET_EVACUAEE_FOR_LDV(src, size_org);
1401 return (StgClosure *)dest;
1404 /* Special version of copy() for when we only want to copy the info
1405 * pointer of an object, but reserve some padding after it. This is
1406 * used to optimise evacuation of BLACKHOLEs.
1411 copyPart(StgClosure *src, nat size_to_reserve, nat size_to_copy, step *stp)
1416 nat size_to_copy_org = size_to_copy;
1419 TICK_GC_WORDS_COPIED(size_to_copy);
1420 if (stp->gen_no < evac_gen) {
1421 #ifdef NO_EAGER_PROMOTION
1422 failed_to_evac = rtsTrue;
1424 stp = &generations[evac_gen].steps[0];
1428 if (stp->hp + size_to_reserve >= stp->hpLim) {
1432 for(to = stp->hp, from = (P_)src; size_to_copy>0; --size_to_copy) {
1437 stp->hp += size_to_reserve;
1438 upd_evacuee(src,(StgClosure *)dest);
1440 // We store the size of the just evacuated object in the LDV word so that
1441 // the profiler can guess the position of the next object later.
1442 // size_to_copy_org is wrong because the closure already occupies size_to_reserve
1444 SET_EVACUAEE_FOR_LDV(src, size_to_reserve);
1446 if (size_to_reserve - size_to_copy_org > 0)
1447 FILL_SLOP(stp->hp - 1, (int)(size_to_reserve - size_to_copy_org));
1449 return (StgClosure *)dest;
1453 /* -----------------------------------------------------------------------------
1454 Evacuate a large object
1456 This just consists of removing the object from the (doubly-linked)
1457 large_alloc_list, and linking it on to the (singly-linked)
1458 new_large_objects list, from where it will be scavenged later.
1460 Convention: bd->flags has BF_EVACUATED set for a large object
1461 that has been evacuated, or unset otherwise.
1462 -------------------------------------------------------------------------- */
1466 evacuate_large(StgPtr p)
1468 bdescr *bd = Bdescr(p);
1471 // object must be at the beginning of the block (or be a ByteArray)
1472 ASSERT(get_itbl((StgClosure *)p)->type == ARR_WORDS ||
1473 (((W_)p & BLOCK_MASK) == 0));
1475 // already evacuated?
1476 if (bd->flags & BF_EVACUATED) {
1477 /* Don't forget to set the failed_to_evac flag if we didn't get
1478 * the desired destination (see comments in evacuate()).
1480 if (bd->gen_no < evac_gen) {
1481 failed_to_evac = rtsTrue;
1482 TICK_GC_FAILED_PROMOTION();
1488 // remove from large_object list
1490 bd->u.back->link = bd->link;
1491 } else { // first object in the list
1492 stp->large_objects = bd->link;
1495 bd->link->u.back = bd->u.back;
1498 /* link it on to the evacuated large object list of the destination step
1501 if (stp->gen_no < evac_gen) {
1502 #ifdef NO_EAGER_PROMOTION
1503 failed_to_evac = rtsTrue;
1505 stp = &generations[evac_gen].steps[0];
1510 bd->gen_no = stp->gen_no;
1511 bd->link = stp->new_large_objects;
1512 stp->new_large_objects = bd;
1513 bd->flags |= BF_EVACUATED;
1516 /* -----------------------------------------------------------------------------
1517 Adding a MUT_CONS to an older generation.
1519 This is necessary from time to time when we end up with an
1520 old-to-new generation pointer in a non-mutable object. We defer
1521 the promotion until the next GC.
1522 -------------------------------------------------------------------------- */
1526 mkMutCons(StgClosure *ptr, generation *gen)
1531 stp = &gen->steps[0];
1533 /* chain a new block onto the to-space for the destination step if
1536 if (stp->hp + sizeofW(StgIndOldGen) >= stp->hpLim) {
1540 q = (StgMutVar *)stp->hp;
1541 stp->hp += sizeofW(StgMutVar);
1543 SET_HDR(q,&stg_MUT_CONS_info,CCS_GC);
1545 recordOldToNewPtrs((StgMutClosure *)q);
1547 return (StgClosure *)q;
1550 /* -----------------------------------------------------------------------------
1553 This is called (eventually) for every live object in the system.
1555 The caller to evacuate specifies a desired generation in the
1556 evac_gen global variable. The following conditions apply to
1557 evacuating an object which resides in generation M when we're
1558 collecting up to generation N
1562 else evac to step->to
1564 if M < evac_gen evac to evac_gen, step 0
1566 if the object is already evacuated, then we check which generation
1569 if M >= evac_gen do nothing
1570 if M < evac_gen set failed_to_evac flag to indicate that we
1571 didn't manage to evacuate this object into evac_gen.
1573 -------------------------------------------------------------------------- */
1576 evacuate(StgClosure *q)
1581 const StgInfoTable *info;
1584 if (HEAP_ALLOCED(q)) {
1587 // not a group head: find the group head
1588 if (bd->blocks == 0) { bd = bd->link; }
1590 if (bd->gen_no > N) {
1591 /* Can't evacuate this object, because it's in a generation
1592 * older than the ones we're collecting. Let's hope that it's
1593 * in evac_gen or older, or we will have to arrange to track
1594 * this pointer using the mutable list.
1596 if (bd->gen_no < evac_gen) {
1598 failed_to_evac = rtsTrue;
1599 TICK_GC_FAILED_PROMOTION();
1604 /* evacuate large objects by re-linking them onto a different list.
1606 if (bd->flags & BF_LARGE) {
1608 if (info->type == TSO &&
1609 ((StgTSO *)q)->what_next == ThreadRelocated) {
1610 q = (StgClosure *)((StgTSO *)q)->link;
1613 evacuate_large((P_)q);
1617 /* If the object is in a step that we're compacting, then we
1618 * need to use an alternative evacuate procedure.
1620 if (bd->step->is_compacted) {
1621 if (!is_marked((P_)q,bd)) {
1623 if (mark_stack_full()) {
1624 mark_stack_overflowed = rtsTrue;
1627 push_mark_stack((P_)q);
1635 else stp = NULL; // make sure copy() will crash if HEAP_ALLOCED is wrong
1638 // make sure the info pointer is into text space
1639 ASSERT(q && (LOOKS_LIKE_GHC_INFO(GET_INFO(q))
1640 || IS_HUGS_CONSTR_INFO(GET_INFO(q))));
1643 switch (info -> type) {
1647 to = copy(q,sizeW_fromITBL(info),stp);
1652 StgWord w = (StgWord)q->payload[0];
1653 if (q->header.info == Czh_con_info &&
1654 // unsigned, so always true: (StgChar)w >= MIN_CHARLIKE &&
1655 (StgChar)w <= MAX_CHARLIKE) {
1656 return (StgClosure *)CHARLIKE_CLOSURE((StgChar)w);
1658 if (q->header.info == Izh_con_info &&
1659 (StgInt)w >= MIN_INTLIKE && (StgInt)w <= MAX_INTLIKE) {
1660 return (StgClosure *)INTLIKE_CLOSURE((StgInt)w);
1662 // else, fall through ...
1668 return copy(q,sizeofW(StgHeader)+1,stp);
1670 case THUNK_1_0: // here because of MIN_UPD_SIZE
1675 #ifdef NO_PROMOTE_THUNKS
1676 if (bd->gen_no == 0 &&
1677 bd->step->no != 0 &&
1678 bd->step->no == generations[bd->gen_no].n_steps-1) {
1682 return copy(q,sizeofW(StgHeader)+2,stp);
1690 return copy(q,sizeofW(StgHeader)+2,stp);
1696 case IND_OLDGEN_PERM:
1701 return copy(q,sizeW_fromITBL(info),stp);
1704 case SE_CAF_BLACKHOLE:
1707 return copyPart(q,BLACKHOLE_sizeW(),sizeofW(StgHeader),stp);
1710 to = copy(q,BLACKHOLE_sizeW(),stp);
1713 case THUNK_SELECTOR:
1715 const StgInfoTable* selectee_info;
1716 StgClosure* selectee = ((StgSelector*)q)->selectee;
1719 selectee_info = get_itbl(selectee);
1720 switch (selectee_info->type) {
1728 case CONSTR_NOCAF_STATIC:
1730 StgWord offset = info->layout.selector_offset;
1732 // check that the size is in range
1734 (StgWord32)(selectee_info->layout.payload.ptrs +
1735 selectee_info->layout.payload.nptrs));
1737 // perform the selection!
1738 q = selectee->payload[offset];
1739 if (major_gc==rtsTrue) {TICK_GC_SEL_MAJOR();} else {TICK_GC_SEL_MINOR();}
1741 /* if we're already in to-space, there's no need to continue
1742 * with the evacuation, just update the source address with
1743 * a pointer to the (evacuated) constructor field.
1745 if (HEAP_ALLOCED(q)) {
1746 bdescr *bd = Bdescr((P_)q);
1747 if (bd->flags & BF_EVACUATED) {
1748 if (bd->gen_no < evac_gen) {
1749 failed_to_evac = rtsTrue;
1750 TICK_GC_FAILED_PROMOTION();
1756 /* otherwise, carry on and evacuate this constructor field,
1757 * (but not the constructor itself)
1766 case IND_OLDGEN_PERM:
1767 selectee = ((StgInd *)selectee)->indirectee;
1771 selectee = ((StgEvacuated *)selectee)->evacuee;
1774 case THUNK_SELECTOR:
1776 /* Disabled 03 April 2001 by JRS; it seems to cause the GC (or
1777 something) to go into an infinite loop when the nightly
1778 stage2 compiles PrelTup.lhs. */
1780 /* we can't recurse indefinitely in evacuate(), so set a
1781 * limit on the number of times we can go around this
1784 if (thunk_selector_depth < MAX_THUNK_SELECTOR_DEPTH) {
1786 bd = Bdescr((P_)selectee);
1787 if (!bd->flags & BF_EVACUATED) {
1788 thunk_selector_depth++;
1789 selectee = evacuate(selectee);
1790 thunk_selector_depth--;
1794 TICK_GC_SEL_ABANDONED();
1795 // and fall through...
1808 case SE_CAF_BLACKHOLE:
1812 // not evaluated yet
1816 // a copy of the top-level cases below
1817 case RBH: // cf. BLACKHOLE_BQ
1819 //StgInfoTable *rip = get_closure_info(q, &size, &ptrs, &nonptrs, &vhs, str);
1820 to = copy(q,BLACKHOLE_sizeW(),stp);
1821 //ToDo: derive size etc from reverted IP
1822 //to = copy(q,size,stp);
1823 // recordMutable((StgMutClosure *)to);
1828 ASSERT(sizeofW(StgBlockedFetch) >= MIN_NONUPD_SIZE);
1829 to = copy(q,sizeofW(StgBlockedFetch),stp);
1836 ASSERT(sizeofW(StgBlockedFetch) >= MIN_UPD_SIZE);
1837 to = copy(q,sizeofW(StgFetchMe),stp);
1841 ASSERT(sizeofW(StgBlockedFetch) >= MIN_UPD_SIZE);
1842 to = copy(q,sizeofW(StgFetchMeBlockingQueue),stp);
1847 barf("evacuate: THUNK_SELECTOR: strange selectee %d",
1848 (int)(selectee_info->type));
1851 return copy(q,THUNK_SELECTOR_sizeW(),stp);
1855 // follow chains of indirections, don't evacuate them
1856 q = ((StgInd*)q)->indirectee;
1860 if (info->srt_len > 0 && major_gc &&
1861 THUNK_STATIC_LINK((StgClosure *)q) == NULL) {
1862 THUNK_STATIC_LINK((StgClosure *)q) = static_objects;
1863 static_objects = (StgClosure *)q;
1868 if (info->srt_len > 0 && major_gc &&
1869 FUN_STATIC_LINK((StgClosure *)q) == NULL) {
1870 FUN_STATIC_LINK((StgClosure *)q) = static_objects;
1871 static_objects = (StgClosure *)q;
1876 /* If q->saved_info != NULL, then it's a revertible CAF - it'll be
1877 * on the CAF list, so don't do anything with it here (we'll
1878 * scavenge it later).
1881 && ((StgIndStatic *)q)->saved_info == NULL
1882 && IND_STATIC_LINK((StgClosure *)q) == NULL) {
1883 IND_STATIC_LINK((StgClosure *)q) = static_objects;
1884 static_objects = (StgClosure *)q;
1889 if (major_gc && STATIC_LINK(info,(StgClosure *)q) == NULL) {
1890 STATIC_LINK(info,(StgClosure *)q) = static_objects;
1891 static_objects = (StgClosure *)q;
1895 case CONSTR_INTLIKE:
1896 case CONSTR_CHARLIKE:
1897 case CONSTR_NOCAF_STATIC:
1898 /* no need to put these on the static linked list, they don't need
1913 // shouldn't see these
1914 barf("evacuate: stack frame at %p\n", q);
1918 /* PAPs and AP_UPDs are special - the payload is a copy of a chunk
1919 * of stack, tagging and all.
1921 return copy(q,pap_sizeW((StgPAP*)q),stp);
1924 /* Already evacuated, just return the forwarding address.
1925 * HOWEVER: if the requested destination generation (evac_gen) is
1926 * older than the actual generation (because the object was
1927 * already evacuated to a younger generation) then we have to
1928 * set the failed_to_evac flag to indicate that we couldn't
1929 * manage to promote the object to the desired generation.
1931 if (evac_gen > 0) { // optimisation
1932 StgClosure *p = ((StgEvacuated*)q)->evacuee;
1933 if (Bdescr((P_)p)->gen_no < evac_gen) {
1934 failed_to_evac = rtsTrue;
1935 TICK_GC_FAILED_PROMOTION();
1938 return ((StgEvacuated*)q)->evacuee;
1941 // just copy the block
1942 return copy(q,arr_words_sizeW((StgArrWords *)q),stp);
1945 case MUT_ARR_PTRS_FROZEN:
1946 // just copy the block
1947 return copy(q,mut_arr_ptrs_sizeW((StgMutArrPtrs *)q),stp);
1951 StgTSO *tso = (StgTSO *)q;
1953 /* Deal with redirected TSOs (a TSO that's had its stack enlarged).
1955 if (tso->what_next == ThreadRelocated) {
1956 q = (StgClosure *)tso->link;
1960 /* To evacuate a small TSO, we need to relocate the update frame
1964 StgTSO *new_tso = (StgTSO *)copy((StgClosure *)tso,tso_sizeW(tso),stp);
1965 move_TSO(tso, new_tso);
1966 return (StgClosure *)new_tso;
1971 case RBH: // cf. BLACKHOLE_BQ
1973 //StgInfoTable *rip = get_closure_info(q, &size, &ptrs, &nonptrs, &vhs, str);
1974 to = copy(q,BLACKHOLE_sizeW(),stp);
1975 //ToDo: derive size etc from reverted IP
1976 //to = copy(q,size,stp);
1978 belch("@@ evacuate: RBH %p (%s) to %p (%s)",
1979 q, info_type(q), to, info_type(to)));
1984 ASSERT(sizeofW(StgBlockedFetch) >= MIN_NONUPD_SIZE);
1985 to = copy(q,sizeofW(StgBlockedFetch),stp);
1987 belch("@@ evacuate: %p (%s) to %p (%s)",
1988 q, info_type(q), to, info_type(to)));
1995 ASSERT(sizeofW(StgBlockedFetch) >= MIN_UPD_SIZE);
1996 to = copy(q,sizeofW(StgFetchMe),stp);
1998 belch("@@ evacuate: %p (%s) to %p (%s)",
1999 q, info_type(q), to, info_type(to)));
2003 ASSERT(sizeofW(StgBlockedFetch) >= MIN_UPD_SIZE);
2004 to = copy(q,sizeofW(StgFetchMeBlockingQueue),stp);
2006 belch("@@ evacuate: %p (%s) to %p (%s)",
2007 q, info_type(q), to, info_type(to)));
2012 barf("evacuate: strange closure type %d", (int)(info->type));
2018 /* -----------------------------------------------------------------------------
2019 move_TSO is called to update the TSO structure after it has been
2020 moved from one place to another.
2021 -------------------------------------------------------------------------- */
2024 move_TSO(StgTSO *src, StgTSO *dest)
2028 // relocate the stack pointers...
2029 diff = (StgPtr)dest - (StgPtr)src; // In *words*
2030 dest->sp = (StgPtr)dest->sp + diff;
2031 dest->su = (StgUpdateFrame *) ((P_)dest->su + diff);
2033 relocate_stack(dest, diff);
2036 /* -----------------------------------------------------------------------------
2037 relocate_stack is called to update the linkage between
2038 UPDATE_FRAMEs (and SEQ_FRAMEs etc.) when a stack is moved from one
2040 -------------------------------------------------------------------------- */
2043 relocate_stack(StgTSO *dest, ptrdiff_t diff)
2051 while ((P_)su < dest->stack + dest->stack_size) {
2052 switch (get_itbl(su)->type) {
2054 // GCC actually manages to common up these three cases!
2057 su->link = (StgUpdateFrame *) ((StgPtr)su->link + diff);
2062 cf = (StgCatchFrame *)su;
2063 cf->link = (StgUpdateFrame *) ((StgPtr)cf->link + diff);
2068 sf = (StgSeqFrame *)su;
2069 sf->link = (StgUpdateFrame *) ((StgPtr)sf->link + diff);
2078 barf("relocate_stack %d", (int)(get_itbl(su)->type));
2089 scavenge_srt(const StgInfoTable *info)
2091 StgClosure **srt, **srt_end;
2093 /* evacuate the SRT. If srt_len is zero, then there isn't an
2094 * srt field in the info table. That's ok, because we'll
2095 * never dereference it.
2097 srt = (StgClosure **)(info->srt);
2098 srt_end = srt + info->srt_len;
2099 for (; srt < srt_end; srt++) {
2100 /* Special-case to handle references to closures hiding out in DLLs, since
2101 double indirections required to get at those. The code generator knows
2102 which is which when generating the SRT, so it stores the (indirect)
2103 reference to the DLL closure in the table by first adding one to it.
2104 We check for this here, and undo the addition before evacuating it.
2106 If the SRT entry hasn't got bit 0 set, the SRT entry points to a
2107 closure that's fixed at link-time, and no extra magic is required.
2109 #ifdef ENABLE_WIN32_DLL_SUPPORT
2110 if ( (unsigned long)(*srt) & 0x1 ) {
2111 evacuate(*stgCast(StgClosure**,(stgCast(unsigned long, *srt) & ~0x1)));
2121 /* -----------------------------------------------------------------------------
2123 -------------------------------------------------------------------------- */
2126 scavengeTSO (StgTSO *tso)
2128 // chase the link field for any TSOs on the same queue
2129 (StgClosure *)tso->link = evacuate((StgClosure *)tso->link);
2130 if ( tso->why_blocked == BlockedOnMVar
2131 || tso->why_blocked == BlockedOnBlackHole
2132 || tso->why_blocked == BlockedOnException
2134 || tso->why_blocked == BlockedOnGA
2135 || tso->why_blocked == BlockedOnGA_NoSend
2138 tso->block_info.closure = evacuate(tso->block_info.closure);
2140 if ( tso->blocked_exceptions != NULL ) {
2141 tso->blocked_exceptions =
2142 (StgTSO *)evacuate((StgClosure *)tso->blocked_exceptions);
2144 // scavenge this thread's stack
2145 scavenge_stack(tso->sp, &(tso->stack[tso->stack_size]));
2148 /* -----------------------------------------------------------------------------
2149 Scavenge a given step until there are no more objects in this step
2152 evac_gen is set by the caller to be either zero (for a step in a
2153 generation < N) or G where G is the generation of the step being
2156 We sometimes temporarily change evac_gen back to zero if we're
2157 scavenging a mutable object where early promotion isn't such a good
2159 -------------------------------------------------------------------------- */
2167 nat saved_evac_gen = evac_gen;
2172 failed_to_evac = rtsFalse;
2174 /* scavenge phase - standard breadth-first scavenging of the
2178 while (bd != stp->hp_bd || p < stp->hp) {
2180 // If we're at the end of this block, move on to the next block
2181 if (bd != stp->hp_bd && p == bd->free) {
2187 info = get_itbl((StgClosure *)p);
2188 ASSERT(p && (LOOKS_LIKE_GHC_INFO(info) || IS_HUGS_CONSTR_INFO(info)));
2191 switch (info->type) {
2194 /* treat MVars specially, because we don't want to evacuate the
2195 * mut_link field in the middle of the closure.
2198 StgMVar *mvar = ((StgMVar *)p);
2200 (StgClosure *)mvar->head = evacuate((StgClosure *)mvar->head);
2201 (StgClosure *)mvar->tail = evacuate((StgClosure *)mvar->tail);
2202 (StgClosure *)mvar->value = evacuate((StgClosure *)mvar->value);
2203 evac_gen = saved_evac_gen;
2204 recordMutable((StgMutClosure *)mvar);
2205 failed_to_evac = rtsFalse; // mutable.
2206 p += sizeofW(StgMVar);
2214 ((StgClosure *)p)->payload[1] = evacuate(((StgClosure *)p)->payload[1]);
2215 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
2216 p += sizeofW(StgHeader) + 2;
2221 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
2222 p += sizeofW(StgHeader) + 2; // MIN_UPD_SIZE
2228 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
2229 p += sizeofW(StgHeader) + 1;
2234 p += sizeofW(StgHeader) + 2; // MIN_UPD_SIZE
2240 p += sizeofW(StgHeader) + 1;
2247 p += sizeofW(StgHeader) + 2;
2254 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
2255 p += sizeofW(StgHeader) + 2;
2271 end = (P_)((StgClosure *)p)->payload + info->layout.payload.ptrs;
2272 for (p = (P_)((StgClosure *)p)->payload; p < end; p++) {
2273 (StgClosure *)*p = evacuate((StgClosure *)*p);
2275 p += info->layout.payload.nptrs;
2280 if (stp->gen->no != 0) {
2283 // No need to call LDV_recordDead_FILL_SLOP_DYNAMIC() because an
2284 // IND_OLDGEN_PERM closure is larger than an IND_PERM closure.
2285 LDV_recordDead((StgClosure *)p, sizeofW(StgInd));
2288 // Todo: maybe use SET_HDR() and remove LDV_recordCreate()?
2290 SET_INFO(((StgClosure *)p), &stg_IND_OLDGEN_PERM_info);
2293 // We pretend that p has just been created.
2294 LDV_recordCreate((StgClosure *)p);
2298 case IND_OLDGEN_PERM:
2299 ((StgIndOldGen *)p)->indirectee =
2300 evacuate(((StgIndOldGen *)p)->indirectee);
2301 if (failed_to_evac) {
2302 failed_to_evac = rtsFalse;
2303 recordOldToNewPtrs((StgMutClosure *)p);
2305 p += sizeofW(StgIndOldGen);
2310 ((StgMutVar *)p)->var = evacuate(((StgMutVar *)p)->var);
2311 evac_gen = saved_evac_gen;
2312 recordMutable((StgMutClosure *)p);
2313 failed_to_evac = rtsFalse; // mutable anyhow
2314 p += sizeofW(StgMutVar);
2319 failed_to_evac = rtsFalse; // mutable anyhow
2320 p += sizeofW(StgMutVar);
2324 case SE_CAF_BLACKHOLE:
2327 p += BLACKHOLE_sizeW();
2332 StgBlockingQueue *bh = (StgBlockingQueue *)p;
2333 (StgClosure *)bh->blocking_queue =
2334 evacuate((StgClosure *)bh->blocking_queue);
2335 recordMutable((StgMutClosure *)bh);
2336 failed_to_evac = rtsFalse;
2337 p += BLACKHOLE_sizeW();
2341 case THUNK_SELECTOR:
2343 StgSelector *s = (StgSelector *)p;
2344 s->selectee = evacuate(s->selectee);
2345 p += THUNK_SELECTOR_sizeW();
2349 case AP_UPD: // same as PAPs
2351 /* Treat a PAP just like a section of stack, not forgetting to
2352 * evacuate the function pointer too...
2355 StgPAP* pap = (StgPAP *)p;
2357 pap->fun = evacuate(pap->fun);
2358 scavenge_stack((P_)pap->payload, (P_)pap->payload + pap->n_args);
2359 p += pap_sizeW(pap);
2364 // nothing to follow
2365 p += arr_words_sizeW((StgArrWords *)p);
2369 // follow everything
2373 evac_gen = 0; // repeatedly mutable
2374 next = p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
2375 for (p = (P_)((StgMutArrPtrs *)p)->payload; p < next; p++) {
2376 (StgClosure *)*p = evacuate((StgClosure *)*p);
2378 evac_gen = saved_evac_gen;
2379 recordMutable((StgMutClosure *)q);
2380 failed_to_evac = rtsFalse; // mutable anyhow.
2384 case MUT_ARR_PTRS_FROZEN:
2385 // follow everything
2389 next = p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
2390 for (p = (P_)((StgMutArrPtrs *)p)->payload; p < next; p++) {
2391 (StgClosure *)*p = evacuate((StgClosure *)*p);
2393 // it's tempting to recordMutable() if failed_to_evac is
2394 // false, but that breaks some assumptions (eg. every
2395 // closure on the mutable list is supposed to have the MUT
2396 // flag set, and MUT_ARR_PTRS_FROZEN doesn't).
2402 StgTSO *tso = (StgTSO *)p;
2405 evac_gen = saved_evac_gen;
2406 recordMutable((StgMutClosure *)tso);
2407 failed_to_evac = rtsFalse; // mutable anyhow.
2408 p += tso_sizeW(tso);
2413 case RBH: // cf. BLACKHOLE_BQ
2416 nat size, ptrs, nonptrs, vhs;
2418 StgInfoTable *rip = get_closure_info(p, &size, &ptrs, &nonptrs, &vhs, str);
2420 StgRBH *rbh = (StgRBH *)p;
2421 (StgClosure *)rbh->blocking_queue =
2422 evacuate((StgClosure *)rbh->blocking_queue);
2423 recordMutable((StgMutClosure *)to);
2424 failed_to_evac = rtsFalse; // mutable anyhow.
2426 belch("@@ scavenge: RBH %p (%s) (new blocking_queue link=%p)",
2427 p, info_type(p), (StgClosure *)rbh->blocking_queue));
2428 // ToDo: use size of reverted closure here!
2429 p += BLACKHOLE_sizeW();
2435 StgBlockedFetch *bf = (StgBlockedFetch *)p;
2436 // follow the pointer to the node which is being demanded
2437 (StgClosure *)bf->node =
2438 evacuate((StgClosure *)bf->node);
2439 // follow the link to the rest of the blocking queue
2440 (StgClosure *)bf->link =
2441 evacuate((StgClosure *)bf->link);
2442 if (failed_to_evac) {
2443 failed_to_evac = rtsFalse;
2444 recordMutable((StgMutClosure *)bf);
2447 belch("@@ scavenge: %p (%s); node is now %p; exciting, isn't it",
2448 bf, info_type((StgClosure *)bf),
2449 bf->node, info_type(bf->node)));
2450 p += sizeofW(StgBlockedFetch);
2458 p += sizeofW(StgFetchMe);
2459 break; // nothing to do in this case
2461 case FETCH_ME_BQ: // cf. BLACKHOLE_BQ
2463 StgFetchMeBlockingQueue *fmbq = (StgFetchMeBlockingQueue *)p;
2464 (StgClosure *)fmbq->blocking_queue =
2465 evacuate((StgClosure *)fmbq->blocking_queue);
2466 if (failed_to_evac) {
2467 failed_to_evac = rtsFalse;
2468 recordMutable((StgMutClosure *)fmbq);
2471 belch("@@ scavenge: %p (%s) exciting, isn't it",
2472 p, info_type((StgClosure *)p)));
2473 p += sizeofW(StgFetchMeBlockingQueue);
2479 barf("scavenge: unimplemented/strange closure type %d @ %p",
2483 /* If we didn't manage to promote all the objects pointed to by
2484 * the current object, then we have to designate this object as
2485 * mutable (because it contains old-to-new generation pointers).
2487 if (failed_to_evac) {
2488 failed_to_evac = rtsFalse;
2489 mkMutCons((StgClosure *)q, &generations[evac_gen]);
2497 /* -----------------------------------------------------------------------------
2498 Scavenge everything on the mark stack.
2500 This is slightly different from scavenge():
2501 - we don't walk linearly through the objects, so the scavenger
2502 doesn't need to advance the pointer on to the next object.
2503 -------------------------------------------------------------------------- */
2506 scavenge_mark_stack(void)
2512 evac_gen = oldest_gen->no;
2513 saved_evac_gen = evac_gen;
2516 while (!mark_stack_empty()) {
2517 p = pop_mark_stack();
2519 info = get_itbl((StgClosure *)p);
2520 ASSERT(p && (LOOKS_LIKE_GHC_INFO(info) || IS_HUGS_CONSTR_INFO(info)));
2523 switch (info->type) {
2526 /* treat MVars specially, because we don't want to evacuate the
2527 * mut_link field in the middle of the closure.
2530 StgMVar *mvar = ((StgMVar *)p);
2532 (StgClosure *)mvar->head = evacuate((StgClosure *)mvar->head);
2533 (StgClosure *)mvar->tail = evacuate((StgClosure *)mvar->tail);
2534 (StgClosure *)mvar->value = evacuate((StgClosure *)mvar->value);
2535 evac_gen = saved_evac_gen;
2536 failed_to_evac = rtsFalse; // mutable.
2544 ((StgClosure *)p)->payload[1] = evacuate(((StgClosure *)p)->payload[1]);
2545 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
2555 ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
2580 end = (P_)((StgClosure *)p)->payload + info->layout.payload.ptrs;
2581 for (p = (P_)((StgClosure *)p)->payload; p < end; p++) {
2582 (StgClosure *)*p = evacuate((StgClosure *)*p);
2588 // don't need to do anything here: the only possible case
2589 // is that we're in a 1-space compacting collector, with
2590 // no "old" generation.
2594 case IND_OLDGEN_PERM:
2595 ((StgIndOldGen *)p)->indirectee =
2596 evacuate(((StgIndOldGen *)p)->indirectee);
2597 if (failed_to_evac) {
2598 recordOldToNewPtrs((StgMutClosure *)p);
2600 failed_to_evac = rtsFalse;
2605 ((StgMutVar *)p)->var = evacuate(((StgMutVar *)p)->var);
2606 evac_gen = saved_evac_gen;
2607 failed_to_evac = rtsFalse;
2612 failed_to_evac = rtsFalse;
2616 case SE_CAF_BLACKHOLE:
2624 StgBlockingQueue *bh = (StgBlockingQueue *)p;
2625 (StgClosure *)bh->blocking_queue =
2626 evacuate((StgClosure *)bh->blocking_queue);
2627 failed_to_evac = rtsFalse;
2631 case THUNK_SELECTOR:
2633 StgSelector *s = (StgSelector *)p;
2634 s->selectee = evacuate(s->selectee);
2638 case AP_UPD: // same as PAPs
2640 /* Treat a PAP just like a section of stack, not forgetting to
2641 * evacuate the function pointer too...
2644 StgPAP* pap = (StgPAP *)p;
2646 pap->fun = evacuate(pap->fun);
2647 scavenge_stack((P_)pap->payload, (P_)pap->payload + pap->n_args);
2652 // follow everything
2656 evac_gen = 0; // repeatedly mutable
2657 next = p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
2658 for (p = (P_)((StgMutArrPtrs *)p)->payload; p < next; p++) {
2659 (StgClosure *)*p = evacuate((StgClosure *)*p);
2661 evac_gen = saved_evac_gen;
2662 failed_to_evac = rtsFalse; // mutable anyhow.
2666 case MUT_ARR_PTRS_FROZEN:
2667 // follow everything
2671 next = p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
2672 for (p = (P_)((StgMutArrPtrs *)p)->payload; p < next; p++) {
2673 (StgClosure *)*p = evacuate((StgClosure *)*p);
2680 StgTSO *tso = (StgTSO *)p;
2683 evac_gen = saved_evac_gen;
2684 failed_to_evac = rtsFalse;
2689 case RBH: // cf. BLACKHOLE_BQ
2692 nat size, ptrs, nonptrs, vhs;
2694 StgInfoTable *rip = get_closure_info(p, &size, &ptrs, &nonptrs, &vhs, str);
2696 StgRBH *rbh = (StgRBH *)p;
2697 (StgClosure *)rbh->blocking_queue =
2698 evacuate((StgClosure *)rbh->blocking_queue);
2699 recordMutable((StgMutClosure *)rbh);
2700 failed_to_evac = rtsFalse; // mutable anyhow.
2702 belch("@@ scavenge: RBH %p (%s) (new blocking_queue link=%p)",
2703 p, info_type(p), (StgClosure *)rbh->blocking_queue));
2709 StgBlockedFetch *bf = (StgBlockedFetch *)p;
2710 // follow the pointer to the node which is being demanded
2711 (StgClosure *)bf->node =
2712 evacuate((StgClosure *)bf->node);
2713 // follow the link to the rest of the blocking queue
2714 (StgClosure *)bf->link =
2715 evacuate((StgClosure *)bf->link);
2716 if (failed_to_evac) {
2717 failed_to_evac = rtsFalse;
2718 recordMutable((StgMutClosure *)bf);
2721 belch("@@ scavenge: %p (%s); node is now %p; exciting, isn't it",
2722 bf, info_type((StgClosure *)bf),
2723 bf->node, info_type(bf->node)));
2731 break; // nothing to do in this case
2733 case FETCH_ME_BQ: // cf. BLACKHOLE_BQ
2735 StgFetchMeBlockingQueue *fmbq = (StgFetchMeBlockingQueue *)p;
2736 (StgClosure *)fmbq->blocking_queue =
2737 evacuate((StgClosure *)fmbq->blocking_queue);
2738 if (failed_to_evac) {
2739 failed_to_evac = rtsFalse;
2740 recordMutable((StgMutClosure *)fmbq);
2743 belch("@@ scavenge: %p (%s) exciting, isn't it",
2744 p, info_type((StgClosure *)p)));
2750 barf("scavenge_mark_stack: unimplemented/strange closure type %d @ %p",
2754 if (failed_to_evac) {
2755 failed_to_evac = rtsFalse;
2756 mkMutCons((StgClosure *)q, &generations[evac_gen]);
2759 // mark the next bit to indicate "scavenged"
2760 mark(q+1, Bdescr(q));
2762 } // while (!mark_stack_empty())
2764 // start a new linear scan if the mark stack overflowed at some point
2765 if (mark_stack_overflowed && oldgen_scan_bd == NULL) {
2766 IF_DEBUG(gc, belch("scavenge_mark_stack: starting linear scan"));
2767 mark_stack_overflowed = rtsFalse;
2768 oldgen_scan_bd = oldest_gen->steps[0].blocks;
2769 oldgen_scan = oldgen_scan_bd->start;
2772 if (oldgen_scan_bd) {
2773 // push a new thing on the mark stack
2775 // find a closure that is marked but not scavenged, and start
2777 while (oldgen_scan < oldgen_scan_bd->free
2778 && !is_marked(oldgen_scan,oldgen_scan_bd)) {
2782 if (oldgen_scan < oldgen_scan_bd->free) {
2784 // already scavenged?
2785 if (is_marked(oldgen_scan+1,oldgen_scan_bd)) {
2786 oldgen_scan += sizeofW(StgHeader) + MIN_NONUPD_SIZE;
2789 push_mark_stack(oldgen_scan);
2790 // ToDo: bump the linear scan by the actual size of the object
2791 oldgen_scan += sizeofW(StgHeader) + MIN_NONUPD_SIZE;
2795 oldgen_scan_bd = oldgen_scan_bd->link;
2796 if (oldgen_scan_bd != NULL) {
2797 oldgen_scan = oldgen_scan_bd->start;
2803 /* -----------------------------------------------------------------------------
2804 Scavenge one object.
2806 This is used for objects that are temporarily marked as mutable
2807 because they contain old-to-new generation pointers. Only certain
2808 objects can have this property.
2809 -------------------------------------------------------------------------- */
2812 scavenge_one(StgPtr p)
2814 const StgInfoTable *info;
2815 nat saved_evac_gen = evac_gen;
2818 ASSERT(p && (LOOKS_LIKE_GHC_INFO(GET_INFO((StgClosure *)p))
2819 || IS_HUGS_CONSTR_INFO(GET_INFO((StgClosure *)p))));
2821 info = get_itbl((StgClosure *)p);
2823 switch (info->type) {
2826 case FUN_1_0: // hardly worth specialising these guys
2846 case IND_OLDGEN_PERM:
2850 end = (StgPtr)((StgClosure *)p)->payload + info->layout.payload.ptrs;
2851 for (q = (StgPtr)((StgClosure *)p)->payload; q < end; q++) {
2852 (StgClosure *)*q = evacuate((StgClosure *)*q);
2858 case SE_CAF_BLACKHOLE:
2863 case THUNK_SELECTOR:
2865 StgSelector *s = (StgSelector *)p;
2866 s->selectee = evacuate(s->selectee);
2871 // nothing to follow
2876 // follow everything
2879 evac_gen = 0; // repeatedly mutable
2880 recordMutable((StgMutClosure *)p);
2881 next = p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
2882 for (p = (P_)((StgMutArrPtrs *)p)->payload; p < next; p++) {
2883 (StgClosure *)*p = evacuate((StgClosure *)*p);
2885 evac_gen = saved_evac_gen;
2886 failed_to_evac = rtsFalse;
2890 case MUT_ARR_PTRS_FROZEN:
2892 // follow everything
2895 next = p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
2896 for (p = (P_)((StgMutArrPtrs *)p)->payload; p < next; p++) {
2897 (StgClosure *)*p = evacuate((StgClosure *)*p);
2904 StgTSO *tso = (StgTSO *)p;
2906 evac_gen = 0; // repeatedly mutable
2908 recordMutable((StgMutClosure *)tso);
2909 evac_gen = saved_evac_gen;
2910 failed_to_evac = rtsFalse;
2917 StgPAP* pap = (StgPAP *)p;
2918 pap->fun = evacuate(pap->fun);
2919 scavenge_stack((P_)pap->payload, (P_)pap->payload + pap->n_args);
2924 // This might happen if for instance a MUT_CONS was pointing to a
2925 // THUNK which has since been updated. The IND_OLDGEN will
2926 // be on the mutable list anyway, so we don't need to do anything
2931 barf("scavenge_one: strange object %d", (int)(info->type));
2934 no_luck = failed_to_evac;
2935 failed_to_evac = rtsFalse;
2939 /* -----------------------------------------------------------------------------
2940 Scavenging mutable lists.
2942 We treat the mutable list of each generation > N (i.e. all the
2943 generations older than the one being collected) as roots. We also
2944 remove non-mutable objects from the mutable list at this point.
2945 -------------------------------------------------------------------------- */
2948 scavenge_mut_once_list(generation *gen)
2950 const StgInfoTable *info;
2951 StgMutClosure *p, *next, *new_list;
2953 p = gen->mut_once_list;
2954 new_list = END_MUT_LIST;
2958 failed_to_evac = rtsFalse;
2960 for (; p != END_MUT_LIST; p = next, next = p->mut_link) {
2962 // make sure the info pointer is into text space
2963 ASSERT(p && (LOOKS_LIKE_GHC_INFO(GET_INFO(p))
2964 || IS_HUGS_CONSTR_INFO(GET_INFO(p))));
2968 if (info->type==RBH)
2969 info = REVERT_INFOPTR(info); // if it's an RBH, look at the orig closure
2971 switch(info->type) {
2974 case IND_OLDGEN_PERM:
2976 /* Try to pull the indirectee into this generation, so we can
2977 * remove the indirection from the mutable list.
2979 ((StgIndOldGen *)p)->indirectee =
2980 evacuate(((StgIndOldGen *)p)->indirectee);
2982 #if 0 && defined(DEBUG)
2983 if (RtsFlags.DebugFlags.gc)
2984 /* Debugging code to print out the size of the thing we just
2988 StgPtr start = gen->steps[0].scan;
2989 bdescr *start_bd = gen->steps[0].scan_bd;
2991 scavenge(&gen->steps[0]);
2992 if (start_bd != gen->steps[0].scan_bd) {
2993 size += (P_)BLOCK_ROUND_UP(start) - start;
2994 start_bd = start_bd->link;
2995 while (start_bd != gen->steps[0].scan_bd) {
2996 size += BLOCK_SIZE_W;
2997 start_bd = start_bd->link;
2999 size += gen->steps[0].scan -
3000 (P_)BLOCK_ROUND_DOWN(gen->steps[0].scan);
3002 size = gen->steps[0].scan - start;
3004 belch("evac IND_OLDGEN: %ld bytes", size * sizeof(W_));
3008 /* failed_to_evac might happen if we've got more than two
3009 * generations, we're collecting only generation 0, the
3010 * indirection resides in generation 2 and the indirectee is
3013 if (failed_to_evac) {
3014 failed_to_evac = rtsFalse;
3015 p->mut_link = new_list;
3018 /* the mut_link field of an IND_STATIC is overloaded as the
3019 * static link field too (it just so happens that we don't need
3020 * both at the same time), so we need to NULL it out when
3021 * removing this object from the mutable list because the static
3022 * link fields are all assumed to be NULL before doing a major
3030 /* MUT_CONS is a kind of MUT_VAR, except it that we try to remove
3031 * it from the mutable list if possible by promoting whatever it
3034 if (scavenge_one((StgPtr)((StgMutVar *)p)->var)) {
3035 /* didn't manage to promote everything, so put the
3036 * MUT_CONS back on the list.
3038 p->mut_link = new_list;
3044 // shouldn't have anything else on the mutables list
3045 barf("scavenge_mut_once_list: strange object? %d", (int)(info->type));
3049 gen->mut_once_list = new_list;
3054 scavenge_mutable_list(generation *gen)
3056 const StgInfoTable *info;
3057 StgMutClosure *p, *next;
3059 p = gen->saved_mut_list;
3063 failed_to_evac = rtsFalse;
3065 for (; p != END_MUT_LIST; p = next, next = p->mut_link) {
3067 // make sure the info pointer is into text space
3068 ASSERT(p && (LOOKS_LIKE_GHC_INFO(GET_INFO(p))
3069 || IS_HUGS_CONSTR_INFO(GET_INFO(p))));
3073 if (info->type==RBH)
3074 info = REVERT_INFOPTR(info); // if it's an RBH, look at the orig closure
3076 switch(info->type) {
3079 // follow everything
3080 p->mut_link = gen->mut_list;
3085 end = (P_)p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
3086 for (q = (P_)((StgMutArrPtrs *)p)->payload; q < end; q++) {
3087 (StgClosure *)*q = evacuate((StgClosure *)*q);
3092 // Happens if a MUT_ARR_PTRS in the old generation is frozen
3093 case MUT_ARR_PTRS_FROZEN:
3098 end = (P_)p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
3099 for (q = (P_)((StgMutArrPtrs *)p)->payload; q < end; q++) {
3100 (StgClosure *)*q = evacuate((StgClosure *)*q);
3104 if (failed_to_evac) {
3105 failed_to_evac = rtsFalse;
3106 mkMutCons((StgClosure *)p, gen);
3112 ((StgMutVar *)p)->var = evacuate(((StgMutVar *)p)->var);
3113 p->mut_link = gen->mut_list;
3119 StgMVar *mvar = (StgMVar *)p;
3120 (StgClosure *)mvar->head = evacuate((StgClosure *)mvar->head);
3121 (StgClosure *)mvar->tail = evacuate((StgClosure *)mvar->tail);
3122 (StgClosure *)mvar->value = evacuate((StgClosure *)mvar->value);
3123 p->mut_link = gen->mut_list;
3130 StgTSO *tso = (StgTSO *)p;
3134 /* Don't take this TSO off the mutable list - it might still
3135 * point to some younger objects (because we set evac_gen to 0
3138 tso->mut_link = gen->mut_list;
3139 gen->mut_list = (StgMutClosure *)tso;
3145 StgBlockingQueue *bh = (StgBlockingQueue *)p;
3146 (StgClosure *)bh->blocking_queue =
3147 evacuate((StgClosure *)bh->blocking_queue);
3148 p->mut_link = gen->mut_list;
3153 /* Happens if a BLACKHOLE_BQ in the old generation is updated:
3156 case IND_OLDGEN_PERM:
3157 /* Try to pull the indirectee into this generation, so we can
3158 * remove the indirection from the mutable list.
3161 ((StgIndOldGen *)p)->indirectee =
3162 evacuate(((StgIndOldGen *)p)->indirectee);
3165 if (failed_to_evac) {
3166 failed_to_evac = rtsFalse;
3167 p->mut_link = gen->mut_once_list;
3168 gen->mut_once_list = p;
3175 // HWL: check whether all of these are necessary
3177 case RBH: // cf. BLACKHOLE_BQ
3179 // nat size, ptrs, nonptrs, vhs;
3181 // StgInfoTable *rip = get_closure_info(p, &size, &ptrs, &nonptrs, &vhs, str);
3182 StgRBH *rbh = (StgRBH *)p;
3183 (StgClosure *)rbh->blocking_queue =
3184 evacuate((StgClosure *)rbh->blocking_queue);
3185 if (failed_to_evac) {
3186 failed_to_evac = rtsFalse;
3187 recordMutable((StgMutClosure *)rbh);
3189 // ToDo: use size of reverted closure here!
3190 p += BLACKHOLE_sizeW();
3196 StgBlockedFetch *bf = (StgBlockedFetch *)p;
3197 // follow the pointer to the node which is being demanded
3198 (StgClosure *)bf->node =
3199 evacuate((StgClosure *)bf->node);
3200 // follow the link to the rest of the blocking queue
3201 (StgClosure *)bf->link =
3202 evacuate((StgClosure *)bf->link);
3203 if (failed_to_evac) {
3204 failed_to_evac = rtsFalse;
3205 recordMutable((StgMutClosure *)bf);
3207 p += sizeofW(StgBlockedFetch);
3213 barf("scavenge_mutable_list: REMOTE_REF %d", (int)(info->type));
3216 p += sizeofW(StgFetchMe);
3217 break; // nothing to do in this case
3219 case FETCH_ME_BQ: // cf. BLACKHOLE_BQ
3221 StgFetchMeBlockingQueue *fmbq = (StgFetchMeBlockingQueue *)p;
3222 (StgClosure *)fmbq->blocking_queue =
3223 evacuate((StgClosure *)fmbq->blocking_queue);
3224 if (failed_to_evac) {
3225 failed_to_evac = rtsFalse;
3226 recordMutable((StgMutClosure *)fmbq);
3228 p += sizeofW(StgFetchMeBlockingQueue);
3234 // shouldn't have anything else on the mutables list
3235 barf("scavenge_mutable_list: strange object? %d", (int)(info->type));
3242 scavenge_static(void)
3244 StgClosure* p = static_objects;
3245 const StgInfoTable *info;
3247 /* Always evacuate straight to the oldest generation for static
3249 evac_gen = oldest_gen->no;
3251 /* keep going until we've scavenged all the objects on the linked
3253 while (p != END_OF_STATIC_LIST) {
3257 if (info->type==RBH)
3258 info = REVERT_INFOPTR(info); // if it's an RBH, look at the orig closure
3260 // make sure the info pointer is into text space
3261 ASSERT(p && (LOOKS_LIKE_GHC_INFO(GET_INFO(p))
3262 || IS_HUGS_CONSTR_INFO(GET_INFO(p))));
3264 /* Take this object *off* the static_objects list,
3265 * and put it on the scavenged_static_objects list.
3267 static_objects = STATIC_LINK(info,p);
3268 STATIC_LINK(info,p) = scavenged_static_objects;
3269 scavenged_static_objects = p;
3271 switch (info -> type) {
3275 StgInd *ind = (StgInd *)p;
3276 ind->indirectee = evacuate(ind->indirectee);
3278 /* might fail to evacuate it, in which case we have to pop it
3279 * back on the mutable list (and take it off the
3280 * scavenged_static list because the static link and mut link
3281 * pointers are one and the same).
3283 if (failed_to_evac) {
3284 failed_to_evac = rtsFalse;
3285 scavenged_static_objects = IND_STATIC_LINK(p);
3286 ((StgMutClosure *)ind)->mut_link = oldest_gen->mut_once_list;
3287 oldest_gen->mut_once_list = (StgMutClosure *)ind;
3301 next = (P_)p->payload + info->layout.payload.ptrs;
3302 // evacuate the pointers
3303 for (q = (P_)p->payload; q < next; q++) {
3304 (StgClosure *)*q = evacuate((StgClosure *)*q);
3310 barf("scavenge_static: strange closure %d", (int)(info->type));
3313 ASSERT(failed_to_evac == rtsFalse);
3315 /* get the next static object from the list. Remember, there might
3316 * be more stuff on this list now that we've done some evacuating!
3317 * (static_objects is a global)
3323 /* -----------------------------------------------------------------------------
3324 scavenge_stack walks over a section of stack and evacuates all the
3325 objects pointed to by it. We can use the same code for walking
3326 PAPs, since these are just sections of copied stack.
3327 -------------------------------------------------------------------------- */
3330 scavenge_stack(StgPtr p, StgPtr stack_end)
3333 const StgInfoTable* info;
3336 //IF_DEBUG(sanity, belch(" scavenging stack between %p and %p", p, stack_end));
3339 * Each time around this loop, we are looking at a chunk of stack
3340 * that starts with either a pending argument section or an
3341 * activation record.
3344 while (p < stack_end) {
3347 // If we've got a tag, skip over that many words on the stack
3348 if (IS_ARG_TAG((W_)q)) {
3353 /* Is q a pointer to a closure?
3355 if (! LOOKS_LIKE_GHC_INFO(q) ) {
3357 if ( 0 && LOOKS_LIKE_STATIC_CLOSURE(q) ) { // Is it a static closure?
3358 ASSERT(closure_STATIC((StgClosure *)q));
3360 // otherwise, must be a pointer into the allocation space.
3363 (StgClosure *)*p = evacuate((StgClosure *)q);
3369 * Otherwise, q must be the info pointer of an activation
3370 * record. All activation records have 'bitmap' style layout
3373 info = get_itbl((StgClosure *)p);
3375 switch (info->type) {
3377 // Dynamic bitmap: the mask is stored on the stack
3379 bitmap = ((StgRetDyn *)p)->liveness;
3380 p = (P_)&((StgRetDyn *)p)->payload[0];
3383 // probably a slow-entry point return address:
3391 belch("HWL: scavenge_stack: FUN(_STATIC) adjusting p from %p to %p (instead of %p)",
3392 old_p, p, old_p+1));
3394 p++; // what if FHS!=1 !? -- HWL
3399 /* Specialised code for update frames, since they're so common.
3400 * We *know* the updatee points to a BLACKHOLE, CAF_BLACKHOLE,
3401 * or BLACKHOLE_BQ, so just inline the code to evacuate it here.
3405 StgUpdateFrame *frame = (StgUpdateFrame *)p;
3407 p += sizeofW(StgUpdateFrame);
3410 frame->updatee = evacuate(frame->updatee);
3412 #else // specialised code for update frames, not sure if it's worth it.
3414 nat type = get_itbl(frame->updatee)->type;
3416 if (type == EVACUATED) {
3417 frame->updatee = evacuate(frame->updatee);
3420 bdescr *bd = Bdescr((P_)frame->updatee);
3422 if (bd->gen_no > N) {
3423 if (bd->gen_no < evac_gen) {
3424 failed_to_evac = rtsTrue;
3429 // Don't promote blackholes
3431 if (!(stp->gen_no == 0 &&
3433 stp->no == stp->gen->n_steps-1)) {
3440 to = copyPart(frame->updatee, BLACKHOLE_sizeW(),
3441 sizeofW(StgHeader), stp);
3442 frame->updatee = to;
3445 to = copy(frame->updatee, BLACKHOLE_sizeW(), stp);
3446 frame->updatee = to;
3447 recordMutable((StgMutClosure *)to);
3450 /* will never be SE_{,CAF_}BLACKHOLE, since we
3451 don't push an update frame for single-entry thunks. KSW 1999-01. */
3452 barf("scavenge_stack: UPDATE_FRAME updatee");
3458 // small bitmap (< 32 entries, or 64 on a 64-bit machine)
3465 bitmap = info->layout.bitmap;
3467 // this assumes that the payload starts immediately after the info-ptr
3469 while (bitmap != 0) {
3470 if ((bitmap & 1) == 0) {
3471 (StgClosure *)*p = evacuate((StgClosure *)*p);
3474 bitmap = bitmap >> 1;
3481 // large bitmap (> 32 entries, or > 64 on a 64-bit machine)
3486 StgLargeBitmap *large_bitmap;
3489 large_bitmap = info->layout.large_bitmap;
3492 for (i=0; i<large_bitmap->size; i++) {
3493 bitmap = large_bitmap->bitmap[i];
3494 q = p + BITS_IN(W_);
3495 while (bitmap != 0) {
3496 if ((bitmap & 1) == 0) {
3497 (StgClosure *)*p = evacuate((StgClosure *)*p);
3500 bitmap = bitmap >> 1;
3502 if (i+1 < large_bitmap->size) {
3504 (StgClosure *)*p = evacuate((StgClosure *)*p);
3510 // and don't forget to follow the SRT
3515 barf("scavenge_stack: weird activation record found on stack: %d", (int)(info->type));
3520 /*-----------------------------------------------------------------------------
3521 scavenge the large object list.
3523 evac_gen set by caller; similar games played with evac_gen as with
3524 scavenge() - see comment at the top of scavenge(). Most large
3525 objects are (repeatedly) mutable, so most of the time evac_gen will
3527 --------------------------------------------------------------------------- */
3530 scavenge_large(step *stp)
3535 bd = stp->new_large_objects;
3537 for (; bd != NULL; bd = stp->new_large_objects) {
3539 /* take this object *off* the large objects list and put it on
3540 * the scavenged large objects list. This is so that we can
3541 * treat new_large_objects as a stack and push new objects on
3542 * the front when evacuating.
3544 stp->new_large_objects = bd->link;
3545 dbl_link_onto(bd, &stp->scavenged_large_objects);
3547 // update the block count in this step.
3548 stp->n_scavenged_large_blocks += bd->blocks;
3551 if (scavenge_one(p)) {
3552 mkMutCons((StgClosure *)p, stp->gen);
3557 /* -----------------------------------------------------------------------------
3558 Initialising the static object & mutable lists
3559 -------------------------------------------------------------------------- */
3562 zero_static_object_list(StgClosure* first_static)
3566 const StgInfoTable *info;
3568 for (p = first_static; p != END_OF_STATIC_LIST; p = link) {
3570 link = STATIC_LINK(info, p);
3571 STATIC_LINK(info,p) = NULL;
3575 /* This function is only needed because we share the mutable link
3576 * field with the static link field in an IND_STATIC, so we have to
3577 * zero the mut_link field before doing a major GC, which needs the
3578 * static link field.
3580 * It doesn't do any harm to zero all the mutable link fields on the
3585 zero_mutable_list( StgMutClosure *first )
3587 StgMutClosure *next, *c;
3589 for (c = first; c != END_MUT_LIST; c = next) {
3595 /* -----------------------------------------------------------------------------
3597 -------------------------------------------------------------------------- */
3604 for (c = (StgIndStatic *)caf_list; c != NULL;
3605 c = (StgIndStatic *)c->static_link)
3607 c->header.info = c->saved_info;
3608 c->saved_info = NULL;
3609 // could, but not necessary: c->static_link = NULL;
3615 markCAFs( evac_fn evac )
3619 for (c = (StgIndStatic *)caf_list; c != NULL;
3620 c = (StgIndStatic *)c->static_link)
3622 evac(&c->indirectee);
3626 /* -----------------------------------------------------------------------------
3627 Sanity code for CAF garbage collection.
3629 With DEBUG turned on, we manage a CAF list in addition to the SRT
3630 mechanism. After GC, we run down the CAF list and blackhole any
3631 CAFs which have been garbage collected. This means we get an error
3632 whenever the program tries to enter a garbage collected CAF.
3634 Any garbage collected CAFs are taken off the CAF list at the same
3636 -------------------------------------------------------------------------- */
3638 #if 0 && defined(DEBUG)
3645 const StgInfoTable *info;
3656 ASSERT(info->type == IND_STATIC);
3658 if (STATIC_LINK(info,p) == NULL) {
3659 IF_DEBUG(gccafs, belch("CAF gc'd at 0x%04lx", (long)p));
3661 SET_INFO(p,&stg_BLACKHOLE_info);
3662 p = STATIC_LINK2(info,p);
3666 pp = &STATIC_LINK2(info,p);
3673 // belch("%d CAFs live", i);
3678 /* -----------------------------------------------------------------------------
3681 Whenever a thread returns to the scheduler after possibly doing
3682 some work, we have to run down the stack and black-hole all the
3683 closures referred to by update frames.
3684 -------------------------------------------------------------------------- */
3687 threadLazyBlackHole(StgTSO *tso)
3689 StgUpdateFrame *update_frame;
3690 StgBlockingQueue *bh;
3693 stack_end = &tso->stack[tso->stack_size];
3694 update_frame = tso->su;
3697 switch (get_itbl(update_frame)->type) {
3700 update_frame = ((StgCatchFrame *)update_frame)->link;
3704 bh = (StgBlockingQueue *)update_frame->updatee;
3706 /* if the thunk is already blackholed, it means we've also
3707 * already blackholed the rest of the thunks on this stack,
3708 * so we can stop early.
3710 * The blackhole made for a CAF is a CAF_BLACKHOLE, so they
3711 * don't interfere with this optimisation.
3713 if (bh->header.info == &stg_BLACKHOLE_info) {
3717 if (bh->header.info != &stg_BLACKHOLE_BQ_info &&
3718 bh->header.info != &stg_CAF_BLACKHOLE_info) {
3719 #if (!defined(LAZY_BLACKHOLING)) && defined(DEBUG)
3720 belch("Unexpected lazy BHing required at 0x%04x",(int)bh);
3724 // We pretend that bh is now dead.
3725 LDV_recordDead_FILL_SLOP_DYNAMIC((StgClosure *)bh);
3727 SET_INFO(bh,&stg_BLACKHOLE_info);
3730 // We pretend that bh has just been created.
3731 LDV_recordCreate(bh);
3735 update_frame = update_frame->link;
3739 update_frame = ((StgSeqFrame *)update_frame)->link;
3745 barf("threadPaused");
3751 /* -----------------------------------------------------------------------------
3754 * Code largely pinched from old RTS, then hacked to bits. We also do
3755 * lazy black holing here.
3757 * -------------------------------------------------------------------------- */
3760 threadSqueezeStack(StgTSO *tso)
3762 lnat displacement = 0;
3763 StgUpdateFrame *frame;
3764 StgUpdateFrame *next_frame; // Temporally next
3765 StgUpdateFrame *prev_frame; // Temporally previous
3767 rtsBool prev_was_update_frame;
3769 StgUpdateFrame *top_frame;
3770 nat upd_frames=0, stop_frames=0, catch_frames=0, seq_frames=0,
3772 void printObj( StgClosure *obj ); // from Printer.c
3774 top_frame = tso->su;
3777 bottom = &(tso->stack[tso->stack_size]);
3780 /* There must be at least one frame, namely the STOP_FRAME.
3782 ASSERT((P_)frame < bottom);
3784 /* Walk down the stack, reversing the links between frames so that
3785 * we can walk back up as we squeeze from the bottom. Note that
3786 * next_frame and prev_frame refer to next and previous as they were
3787 * added to the stack, rather than the way we see them in this
3788 * walk. (It makes the next loop less confusing.)
3790 * Stop if we find an update frame pointing to a black hole
3791 * (see comment in threadLazyBlackHole()).
3795 // bottom - sizeof(StgStopFrame) is the STOP_FRAME
3796 while ((P_)frame < bottom - sizeofW(StgStopFrame)) {
3797 prev_frame = frame->link;
3798 frame->link = next_frame;
3803 if (!(frame>=top_frame && frame<=(StgUpdateFrame *)bottom)) {
3804 printObj((StgClosure *)prev_frame);
3805 barf("threadSqueezeStack: current frame is rubbish %p; previous was %p\n",
3808 switch (get_itbl(frame)->type) {
3811 if (frame->updatee->header.info == &stg_BLACKHOLE_info)
3824 barf("Found non-frame during stack squeezing at %p (prev frame was %p)\n",
3826 printObj((StgClosure *)prev_frame);
3829 if (get_itbl(frame)->type == UPDATE_FRAME
3830 && frame->updatee->header.info == &stg_BLACKHOLE_info) {
3835 /* Now, we're at the bottom. Frame points to the lowest update
3836 * frame on the stack, and its link actually points to the frame
3837 * above. We have to walk back up the stack, squeezing out empty
3838 * update frames and turning the pointers back around on the way
3841 * The bottom-most frame (the STOP_FRAME) has not been altered, and
3842 * we never want to eliminate it anyway. Just walk one step up
3843 * before starting to squeeze. When you get to the topmost frame,
3844 * remember that there are still some words above it that might have
3851 prev_was_update_frame = (get_itbl(prev_frame)->type == UPDATE_FRAME);
3854 * Loop through all of the frames (everything except the very
3855 * bottom). Things are complicated by the fact that we have
3856 * CATCH_FRAMEs and SEQ_FRAMEs interspersed with the update frames.
3857 * We can only squeeze when there are two consecutive UPDATE_FRAMEs.
3859 while (frame != NULL) {
3861 StgPtr frame_bottom = (P_)frame + sizeofW(StgUpdateFrame);
3862 rtsBool is_update_frame;
3864 next_frame = frame->link;
3865 is_update_frame = (get_itbl(frame)->type == UPDATE_FRAME);
3868 * 1. both the previous and current frame are update frames
3869 * 2. the current frame is empty
3871 if (prev_was_update_frame && is_update_frame &&
3872 (P_)prev_frame == frame_bottom + displacement) {
3874 // Now squeeze out the current frame
3875 StgClosure *updatee_keep = prev_frame->updatee;
3876 StgClosure *updatee_bypass = frame->updatee;
3879 IF_DEBUG(gc, belch("@@ squeezing frame at %p", frame));
3883 /* Deal with blocking queues. If both updatees have blocked
3884 * threads, then we should merge the queues into the update
3885 * frame that we're keeping.
3887 * Alternatively, we could just wake them up: they'll just go
3888 * straight to sleep on the proper blackhole! This is less code
3889 * and probably less bug prone, although it's probably much
3892 #if 0 // do it properly...
3893 # if (!defined(LAZY_BLACKHOLING)) && defined(DEBUG)
3894 # error Unimplemented lazy BH warning. (KSW 1999-01)
3896 if (GET_INFO(updatee_bypass) == stg_BLACKHOLE_BQ_info
3897 || GET_INFO(updatee_bypass) == stg_CAF_BLACKHOLE_info
3899 // Sigh. It has one. Don't lose those threads!
3900 if (GET_INFO(updatee_keep) == stg_BLACKHOLE_BQ_info) {
3901 // Urgh. Two queues. Merge them.
3902 P_ keep_tso = ((StgBlockingQueue *)updatee_keep)->blocking_queue;
3904 while (keep_tso->link != END_TSO_QUEUE) {
3905 keep_tso = keep_tso->link;
3907 keep_tso->link = ((StgBlockingQueue *)updatee_bypass)->blocking_queue;
3910 // For simplicity, just swap the BQ for the BH
3911 P_ temp = updatee_keep;
3913 updatee_keep = updatee_bypass;
3914 updatee_bypass = temp;
3916 // Record the swap in the kept frame (below)
3917 prev_frame->updatee = updatee_keep;
3922 TICK_UPD_SQUEEZED();
3923 /* wasn't there something about update squeezing and ticky to be
3924 * sorted out? oh yes: we aren't counting each enter properly
3925 * in this case. See the log somewhere. KSW 1999-04-21
3927 * Check two things: that the two update frames don't point to
3928 * the same object, and that the updatee_bypass isn't already an
3929 * indirection. Both of these cases only happen when we're in a
3930 * block hole-style loop (and there are multiple update frames
3931 * on the stack pointing to the same closure), but they can both
3932 * screw us up if we don't check.
3934 if (updatee_bypass != updatee_keep && !closure_IND(updatee_bypass)) {
3935 // this wakes the threads up
3936 UPD_IND_NOLOCK(updatee_bypass, updatee_keep);
3939 sp = (P_)frame - 1; // sp = stuff to slide
3940 displacement += sizeofW(StgUpdateFrame);
3943 // No squeeze for this frame
3944 sp = frame_bottom - 1; // Keep the current frame
3946 /* Do lazy black-holing.
3948 if (is_update_frame) {
3949 StgBlockingQueue *bh = (StgBlockingQueue *)frame->updatee;
3950 if (bh->header.info != &stg_BLACKHOLE_info &&
3951 bh->header.info != &stg_BLACKHOLE_BQ_info &&
3952 bh->header.info != &stg_CAF_BLACKHOLE_info) {
3953 #if (!defined(LAZY_BLACKHOLING)) && defined(DEBUG)
3954 belch("Unexpected lazy BHing required at 0x%04x",(int)bh);
3957 /* zero out the slop so that the sanity checker can tell
3958 * where the next closure is.
3961 StgInfoTable *info = get_itbl(bh);
3962 nat np = info->layout.payload.ptrs, nw = info->layout.payload.nptrs, i;
3963 /* don't zero out slop for a THUNK_SELECTOR, because its layout
3964 * info is used for a different purpose, and it's exactly the
3965 * same size as a BLACKHOLE in any case.
3967 if (info->type != THUNK_SELECTOR) {
3968 for (i = np; i < np + nw; i++) {
3969 ((StgClosure *)bh)->payload[i] = 0;
3976 // We pretend that bh is now dead.
3977 LDV_recordDead_FILL_SLOP_DYNAMIC((StgClosure *)bh);
3980 // Todo: maybe use SET_HDR() and remove LDV_recordCreate()?
3982 SET_INFO(bh,&stg_BLACKHOLE_info);
3985 // We pretend that bh has just been created.
3986 LDV_recordCreate(bh);
3991 // Fix the link in the current frame (should point to the frame below)
3992 frame->link = prev_frame;
3993 prev_was_update_frame = is_update_frame;
3996 // Now slide all words from sp up to the next frame
3998 if (displacement > 0) {
3999 P_ next_frame_bottom;
4001 if (next_frame != NULL)
4002 next_frame_bottom = (P_)next_frame + sizeofW(StgUpdateFrame);
4004 next_frame_bottom = tso->sp - 1;
4008 belch("sliding [%p, %p] by %ld", sp, next_frame_bottom,
4012 while (sp >= next_frame_bottom) {
4013 sp[displacement] = *sp;
4017 (P_)prev_frame = (P_)frame + displacement;
4021 tso->sp += displacement;
4022 tso->su = prev_frame;
4025 belch("@@ threadSqueezeStack: squeezed %d update-frames; found %d BHs; found %d update-, %d stop-, %d catch, %d seq-frames",
4026 squeezes, bhs, upd_frames, stop_frames, catch_frames, seq_frames))
4031 /* -----------------------------------------------------------------------------
4034 * We have to prepare for GC - this means doing lazy black holing
4035 * here. We also take the opportunity to do stack squeezing if it's
4037 * -------------------------------------------------------------------------- */
4039 threadPaused(StgTSO *tso)
4041 if ( RtsFlags.GcFlags.squeezeUpdFrames == rtsTrue )
4042 threadSqueezeStack(tso); // does black holing too
4044 threadLazyBlackHole(tso);
4047 /* -----------------------------------------------------------------------------
4049 * -------------------------------------------------------------------------- */
4053 printMutOnceList(generation *gen)
4055 StgMutClosure *p, *next;
4057 p = gen->mut_once_list;
4060 fprintf(stderr, "@@ Mut once list %p: ", gen->mut_once_list);
4061 for (; p != END_MUT_LIST; p = next, next = p->mut_link) {
4062 fprintf(stderr, "%p (%s), ",
4063 p, info_type((StgClosure *)p));
4065 fputc('\n', stderr);
4069 printMutableList(generation *gen)
4071 StgMutClosure *p, *next;
4076 fprintf(stderr, "@@ Mutable list %p: ", gen->mut_list);
4077 for (; p != END_MUT_LIST; p = next, next = p->mut_link) {
4078 fprintf(stderr, "%p (%s), ",
4079 p, info_type((StgClosure *)p));
4081 fputc('\n', stderr);
4084 static inline rtsBool
4085 maybeLarge(StgClosure *closure)
4087 StgInfoTable *info = get_itbl(closure);
4089 /* closure types that may be found on the new_large_objects list;
4090 see scavenge_large */
4091 return (info->type == MUT_ARR_PTRS ||
4092 info->type == MUT_ARR_PTRS_FROZEN ||
4093 info->type == TSO ||
4094 info->type == ARR_WORDS);