1 /* -----------------------------------------------------------------------------
2 * $Id: GC.c,v 1.19 1999/01/26 11:12:43 simonm Exp $
4 * Two-space garbage collector
6 * ---------------------------------------------------------------------------*/
12 #include "StoragePriv.h"
15 #include "SchedAPI.h" /* for ReverCAFs prototype */
18 #include "BlockAlloc.h"
20 #include "DebugProf.h"
23 #include "StablePriv.h"
27 /* STATIC OBJECT LIST.
30 * We maintain a linked list of static objects that are still live.
31 * The requirements for this list are:
33 * - we need to scan the list while adding to it, in order to
34 * scavenge all the static objects (in the same way that
35 * breadth-first scavenging works for dynamic objects).
37 * - we need to be able to tell whether an object is already on
38 * the list, to break loops.
40 * Each static object has a "static link field", which we use for
41 * linking objects on to the list. We use a stack-type list, consing
42 * objects on the front as they are added (this means that the
43 * scavenge phase is depth-first, not breadth-first, but that
46 * A separate list is kept for objects that have been scavenged
47 * already - this is so that we can zero all the marks afterwards.
49 * An object is on the list if its static link field is non-zero; this
50 * means that we have to mark the end of the list with '1', not NULL.
52 * Extra notes for generational GC:
54 * Each generation has a static object list associated with it. When
55 * collecting generations up to N, we treat the static object lists
56 * from generations > N as roots.
58 * We build up a static object list while collecting generations 0..N,
59 * which is then appended to the static object list of generation N+1.
61 StgClosure* static_objects; /* live static objects */
62 StgClosure* scavenged_static_objects; /* static objects scavenged so far */
64 /* N is the oldest generation being collected, where the generations
65 * are numbered starting at 0. A major GC (indicated by the major_gc
66 * flag) is when we're collecting all generations. We only attempt to
67 * deal with static objects and GC CAFs when doing a major GC.
70 static rtsBool major_gc;
72 /* Youngest generation that objects should be evacuated to in
73 * evacuate(). (Logically an argument to evacuate, but it's static
74 * a lot of the time so we optimise it into a global variable).
80 static StgWeak *old_weak_ptr_list; /* also pending finaliser list */
81 static rtsBool weak_done; /* all done for this pass */
83 /* Flag indicating failure to evacuate an object to the desired
86 static rtsBool failed_to_evac;
88 /* Old to-space (used for two-space collector only)
92 /* -----------------------------------------------------------------------------
93 Static function declarations
94 -------------------------------------------------------------------------- */
96 static StgClosure *evacuate(StgClosure *q);
97 static void zeroStaticObjectList(StgClosure* first_static);
98 static rtsBool traverse_weak_ptr_list(void);
99 static void zeroMutableList(StgMutClosure *first);
100 static void revertDeadCAFs(void);
102 static void scavenge_stack(StgPtr p, StgPtr stack_end);
103 static void scavenge_large(step *step);
104 static void scavenge(step *step);
105 static void scavenge_static(void);
106 static StgMutClosure *scavenge_mutable_list(StgMutClosure *p, nat gen);
109 static void gcCAFs(void);
112 /* -----------------------------------------------------------------------------
115 For garbage collecting generation N (and all younger generations):
117 - follow all pointers in the root set. the root set includes all
118 mutable objects in all steps in all generations.
120 - for each pointer, evacuate the object it points to into either
121 + to-space in the next higher step in that generation, if one exists,
122 + if the object's generation == N, then evacuate it to the next
123 generation if one exists, or else to-space in the current
125 + if the object's generation < N, then evacuate it to to-space
126 in the next generation.
128 - repeatedly scavenge to-space from each step in each generation
129 being collected until no more objects can be evacuated.
131 - free from-space in each step, and set from-space = to-space.
133 -------------------------------------------------------------------------- */
135 void GarbageCollect(void (*get_roots)(void))
139 lnat live, allocated, collected = 0;
143 CostCentreStack *prev_CCS;
146 /* tell the stats department that we've started a GC */
149 /* attribute any costs to CCS_GC */
155 /* We might have been called from Haskell land by _ccall_GC, in
156 * which case we need to call threadPaused() because the scheduler
157 * won't have done it.
159 if (CurrentTSO) { threadPaused(CurrentTSO); }
161 /* Approximate how much we allocated: number of blocks in the
162 * nursery + blocks allocated via allocate() - unused nusery blocks.
163 * This leaves a little slop at the end of each block, and doesn't
164 * take into account large objects (ToDo).
166 allocated = (nursery_blocks * BLOCK_SIZE_W) + allocated_bytes();
167 for ( bd = current_nursery->link; bd != NULL; bd = bd->link ) {
168 allocated -= BLOCK_SIZE_W;
171 /* Figure out which generation to collect
174 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
175 if (generations[g].steps[0].n_blocks >= generations[g].max_blocks) {
179 major_gc = (N == RtsFlags.GcFlags.generations-1);
181 /* check stack sanity *before* GC (ToDo: check all threads) */
182 /*IF_DEBUG(sanity, checkTSO(MainTSO,0)); */
183 IF_DEBUG(sanity, checkFreeListSanity());
185 /* Initialise the static object lists
187 static_objects = END_OF_STATIC_LIST;
188 scavenged_static_objects = END_OF_STATIC_LIST;
190 /* zero the mutable list for the oldest generation (see comment by
191 * zeroMutableList below).
194 zeroMutableList(generations[RtsFlags.GcFlags.generations-1].mut_list);
197 /* Save the old to-space if we're doing a two-space collection
199 if (RtsFlags.GcFlags.generations == 1) {
200 old_to_space = g0s0->to_space;
201 g0s0->to_space = NULL;
204 /* Initialise to-space in all the generations/steps that we're
207 for (g = 0; g <= N; g++) {
208 generations[g].mut_list = END_MUT_LIST;
210 for (s = 0; s < generations[g].n_steps; s++) {
212 /* generation 0, step 0 doesn't need to-space */
213 if (g == 0 && s == 0 && RtsFlags.GcFlags.generations > 1) {
217 /* Get a free block for to-space. Extra blocks will be chained on
221 step = &generations[g].steps[s];
222 ASSERT(step->gen->no == g);
223 ASSERT(step->hp ? Bdescr(step->hp)->step == step : rtsTrue);
224 bd->gen = &generations[g];
227 bd->evacuated = 1; /* it's a to-space block */
228 step->hp = bd->start;
229 step->hpLim = step->hp + BLOCK_SIZE_W;
232 step->to_blocks = 1; /* ???? */
233 step->scan = bd->start;
235 step->new_large_objects = NULL;
236 step->scavenged_large_objects = NULL;
237 /* mark the large objects as not evacuated yet */
238 for (bd = step->large_objects; bd; bd = bd->link) {
244 /* make sure the older generations have at least one block to
245 * allocate into (this makes things easier for copy(), see below.
247 for (g = N+1; g < RtsFlags.GcFlags.generations; g++) {
248 for (s = 0; s < generations[g].n_steps; s++) {
249 step = &generations[g].steps[s];
250 if (step->hp_bd == NULL) {
252 bd->gen = &generations[g];
255 bd->evacuated = 0; /* *not* a to-space block */
256 step->hp = bd->start;
257 step->hpLim = step->hp + BLOCK_SIZE_W;
262 /* Set the scan pointer for older generations: remember we
263 * still have to scavenge objects that have been promoted. */
264 step->scan = step->hp;
265 step->scan_bd = step->hp_bd;
266 step->to_space = NULL;
268 step->new_large_objects = NULL;
269 step->scavenged_large_objects = NULL;
273 /* -----------------------------------------------------------------------
274 * follow all the roots that we know about:
275 * - mutable lists from each generation > N
276 * we want to *scavenge* these roots, not evacuate them: they're not
277 * going to move in this GC.
278 * Also: do them in reverse generation order. This is because we
279 * often want to promote objects that are pointed to by older
280 * generations early, so we don't have to repeatedly copy them.
281 * Doing the generations in reverse order ensures that we don't end
282 * up in the situation where we want to evac an object to gen 3 and
283 * it has already been evaced to gen 2.
286 StgMutClosure *tmp, **pp;
287 for (g = N+1; g < RtsFlags.GcFlags.generations; g++) {
288 generations[g].saved_mut_list = generations[g].mut_list;
289 generations[g].mut_list = END_MUT_LIST;
292 for (g = RtsFlags.GcFlags.generations-1; g > N; g--) {
293 tmp = scavenge_mutable_list(generations[g].saved_mut_list, g);
294 pp = &generations[g].mut_list;
295 while (*pp != END_MUT_LIST) {
296 pp = &(*pp)->mut_link;
302 /* follow all the roots that the application knows about.
307 /* And don't forget to mark the TSO if we got here direct from
310 CurrentTSO = (StgTSO *)MarkRoot((StgClosure *)CurrentTSO);
313 /* Mark the weak pointer list, and prepare to detect dead weak
317 old_weak_ptr_list = weak_ptr_list;
318 weak_ptr_list = NULL;
319 weak_done = rtsFalse;
321 /* Mark the stable pointer table.
323 markStablePtrTable(major_gc);
327 /* ToDo: To fix the caf leak, we need to make the commented out
328 * parts of this code do something sensible - as described in
331 extern void markHugsObjects(void);
333 /* ToDo: This (undefined) function should contain the scavenge
334 * loop immediately below this block of code - but I'm not sure
335 * enough of the details to do this myself.
337 scavengeEverything();
338 /* revert dead CAFs and update enteredCAFs list */
343 /* This will keep the CAFs and the attached BCOs alive
344 * but the values will have been reverted
346 scavengeEverything();
351 /* -------------------------------------------------------------------------
352 * Repeatedly scavenge all the areas we know about until there's no
353 * more scavenging to be done.
360 /* scavenge static objects */
361 if (major_gc && static_objects != END_OF_STATIC_LIST) {
365 /* When scavenging the older generations: Objects may have been
366 * evacuated from generations <= N into older generations, and we
367 * need to scavenge these objects. We're going to try to ensure that
368 * any evacuations that occur move the objects into at least the
369 * same generation as the object being scavenged, otherwise we
370 * have to create new entries on the mutable list for the older
374 /* scavenge each step in generations 0..maxgen */
377 for (gen = RtsFlags.GcFlags.generations-1; gen >= 0; gen--) {
378 for (s = 0; s < generations[gen].n_steps; s++) {
379 step = &generations[gen].steps[s];
381 if (step->hp_bd != step->scan_bd || step->scan < step->hp) {
385 if (step->new_large_objects != NULL) {
386 scavenge_large(step);
392 if (flag) { goto loop; }
394 /* must be last... */
395 if (traverse_weak_ptr_list()) { /* returns rtsTrue if evaced something */
400 /* Now see which stable names are still alive
402 gcStablePtrTable(major_gc);
404 /* Set the maximum blocks for the oldest generation, based on twice
405 * the amount of live data now, adjusted to fit the maximum heap
408 * This is an approximation, since in the worst case we'll need
409 * twice the amount of live data plus whatever space the other
412 if (RtsFlags.GcFlags.generations > 1) {
414 oldest_gen->max_blocks =
415 stg_max(oldest_gen->steps[0].to_blocks * RtsFlags.GcFlags.oldGenFactor,
416 RtsFlags.GcFlags.minOldGenSize);
417 if (oldest_gen->max_blocks > RtsFlags.GcFlags.maxHeapSize / 2) {
418 oldest_gen->max_blocks = RtsFlags.GcFlags.maxHeapSize / 2;
419 if (((int)oldest_gen->max_blocks -
420 (int)oldest_gen->steps[0].to_blocks) <
421 (RtsFlags.GcFlags.pcFreeHeap *
422 RtsFlags.GcFlags.maxHeapSize / 200)) {
428 /* For a two-space collector, we need to resize the nursery. */
430 /* set up a new nursery. Allocate a nursery size based on a
431 * function of the amount of live data (currently a factor of 2,
432 * should be configurable (ToDo)). Use the blocks from the old
433 * nursery if possible, freeing up any left over blocks.
435 * If we get near the maximum heap size, then adjust our nursery
436 * size accordingly. If the nursery is the same size as the live
437 * data (L), then we need 3L bytes. We can reduce the size of the
438 * nursery to bring the required memory down near 2L bytes.
440 * A normal 2-space collector would need 4L bytes to give the same
441 * performance we get from 3L bytes, reducing to the same
442 * performance at 2L bytes.
444 nat blocks = g0s0->to_blocks;
446 if ( blocks * RtsFlags.GcFlags.oldGenFactor * 2 >
447 RtsFlags.GcFlags.maxHeapSize ) {
448 int adjusted_blocks; /* signed on purpose */
451 adjusted_blocks = (RtsFlags.GcFlags.maxHeapSize - 2 * blocks);
452 IF_DEBUG(gc, fprintf(stderr, "Near maximum heap size of 0x%x blocks, blocks = %d, adjusted to %d\n", RtsFlags.GcFlags.maxHeapSize, blocks, adjusted_blocks));
453 pc_free = adjusted_blocks * 100 / RtsFlags.GcFlags.maxHeapSize;
454 if (pc_free < RtsFlags.GcFlags.pcFreeHeap) /* might even be < 0 */ {
457 blocks = adjusted_blocks;
460 blocks *= RtsFlags.GcFlags.oldGenFactor;
461 if (blocks < RtsFlags.GcFlags.minAllocAreaSize) {
462 blocks = RtsFlags.GcFlags.minAllocAreaSize;
466 if (nursery_blocks < blocks) {
467 IF_DEBUG(gc, fprintf(stderr, "Increasing size of nursery to %d blocks\n",
469 g0s0->blocks = allocNursery(g0s0->blocks, blocks-nursery_blocks);
473 IF_DEBUG(gc, fprintf(stderr, "Decreasing size of nursery to %d blocks\n",
475 for (bd = g0s0->blocks; nursery_blocks > blocks; nursery_blocks--) {
483 g0s0->n_blocks = nursery_blocks = blocks;
486 /* run through all the generations/steps and tidy up
488 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
491 generations[g].collections++; /* for stats */
494 for (s = 0; s < generations[g].n_steps; s++) {
496 step = &generations[g].steps[s];
498 if (!(g == 0 && s == 0 && RtsFlags.GcFlags.generations > 1)) {
499 /* Tidy the end of the to-space chains */
500 step->hp_bd->free = step->hp;
501 step->hp_bd->link = NULL;
504 /* for generations we collected... */
507 collected += step->n_blocks * BLOCK_SIZE_W; /* for stats */
509 /* free old memory and shift to-space into from-space for all
510 * the collected steps (except the allocation area). These
511 * freed blocks will probaby be quickly recycled.
513 if (!(g == 0 && s == 0)) {
514 freeChain(step->blocks);
515 step->blocks = step->to_space;
516 step->n_blocks = step->to_blocks;
517 step->to_space = NULL;
519 for (bd = step->blocks; bd != NULL; bd = bd->link) {
520 bd->evacuated = 0; /* now from-space */
524 /* LARGE OBJECTS. The current live large objects are chained on
525 * scavenged_large, having been moved during garbage
526 * collection from large_objects. Any objects left on
527 * large_objects list are therefore dead, so we free them here.
529 for (bd = step->large_objects; bd != NULL; bd = next) {
534 for (bd = step->scavenged_large_objects; bd != NULL; bd = bd->link) {
537 step->large_objects = step->scavenged_large_objects;
539 /* Set the maximum blocks for this generation, interpolating
540 * between the maximum size of the oldest and youngest
543 * max_blocks = alloc_area_size +
544 * (oldgen_max_blocks - alloc_area_size) * G
545 * -----------------------------------------
549 generations[g].max_blocks =
550 RtsFlags.GcFlags.minAllocAreaSize +
551 (((oldest_gen->max_blocks - RtsFlags.GcFlags.minAllocAreaSize) * g)
552 / (RtsFlags.GcFlags.generations-1));
555 /* for older generations... */
558 /* For older generations, we need to append the
559 * scavenged_large_object list (i.e. large objects that have been
560 * promoted during this GC) to the large_object list for that step.
562 for (bd = step->scavenged_large_objects; bd; bd = next) {
565 dbl_link_onto(bd, &step->large_objects);
568 /* add the new blocks we promoted during this GC */
569 step->n_blocks += step->to_blocks;
574 /* Two-space collector:
575 * Free the old to-space, and estimate the amount of live data.
577 if (RtsFlags.GcFlags.generations == 1) {
578 if (old_to_space != NULL) {
579 freeChain(old_to_space);
581 for (bd = g0s0->to_space; bd != NULL; bd = bd->link) {
582 bd->evacuated = 0; /* now from-space */
584 live = g0s0->to_blocks * BLOCK_SIZE_W +
585 ((lnat)g0s0->hp_bd->free - (lnat)g0s0->hp_bd->start) / sizeof(W_);
587 /* Generational collector:
588 * estimate the amount of live data.
592 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
593 for (s = 0; s < generations[g].n_steps; s++) {
594 /* approximate amount of live data (doesn't take into account slop
595 * at end of each block). ToDo: this more accurately.
597 if (g == 0 && s == 0) { continue; }
598 step = &generations[g].steps[s];
599 live += step->n_blocks * BLOCK_SIZE_W +
600 ((lnat)step->hp_bd->free -(lnat)step->hp_bd->start) / sizeof(W_);
605 /* revert dead CAFs and update enteredCAFs list */
608 /* mark the garbage collected CAFs as dead */
610 if (major_gc) { gcCAFs(); }
613 /* zero the scavenged static object list */
615 zeroStaticObjectList(scavenged_static_objects);
620 for (bd = g0s0->blocks; bd; bd = bd->link) {
621 bd->free = bd->start;
622 ASSERT(bd->gen == g0);
623 ASSERT(bd->step == g0s0);
625 current_nursery = g0s0->blocks;
627 /* Free the small objects allocated via allocate(), since this will
628 * all have been copied into G0S1 now.
630 if (small_alloc_list != NULL) {
631 freeChain(small_alloc_list);
633 small_alloc_list = NULL;
635 alloc_blocks_lim = RtsFlags.GcFlags.minAllocAreaSize;
637 /* start any pending finalisers */
638 scheduleFinalisers(old_weak_ptr_list);
640 /* check sanity after GC */
642 if (RtsFlags.GcFlags.generations == 1) {
643 IF_DEBUG(sanity, checkHeap(g0s0->to_space, NULL));
644 IF_DEBUG(sanity, checkChain(g0s0->large_objects));
647 for (g = 0; g <= N; g++) {
648 for (s = 0; s < generations[g].n_steps; s++) {
649 if (g == 0 && s == 0) { continue; }
650 IF_DEBUG(sanity, checkHeap(generations[g].steps[s].blocks, NULL));
653 for (g = N+1; g < RtsFlags.GcFlags.generations; g++) {
654 for (s = 0; s < generations[g].n_steps; s++) {
655 IF_DEBUG(sanity, checkHeap(generations[g].steps[s].blocks,
656 generations[g].steps[s].blocks->start));
657 IF_DEBUG(sanity, checkChain(generations[g].steps[s].large_objects));
660 IF_DEBUG(sanity, checkFreeListSanity());
664 IF_DEBUG(gc, stat_describe_gens());
667 /* symbol-table based profiling */
668 /* heapCensus(to_space); */ /* ToDo */
671 /* restore enclosing cost centre */
676 /* check for memory leaks if sanity checking is on */
677 IF_DEBUG(sanity, memInventory());
679 /* ok, GC over: tell the stats department what happened. */
680 stat_endGC(allocated, collected, live, N);
683 /* -----------------------------------------------------------------------------
686 traverse_weak_ptr_list is called possibly many times during garbage
687 collection. It returns a flag indicating whether it did any work
688 (i.e. called evacuate on any live pointers).
690 Invariant: traverse_weak_ptr_list is called when the heap is in an
691 idempotent state. That means that there are no pending
692 evacuate/scavenge operations. This invariant helps the weak
693 pointer code decide which weak pointers are dead - if there are no
694 new live weak pointers, then all the currently unreachable ones are
697 For generational GC: we just don't try to finalise weak pointers in
698 older generations than the one we're collecting. This could
699 probably be optimised by keeping per-generation lists of weak
700 pointers, but for a few weak pointers this scheme will work.
701 -------------------------------------------------------------------------- */
704 traverse_weak_ptr_list(void)
706 StgWeak *w, **last_w, *next_w;
708 rtsBool flag = rtsFalse;
710 if (weak_done) { return rtsFalse; }
712 /* doesn't matter where we evacuate values/finalisers to, since
713 * these pointers are treated as roots (iff the keys are alive).
717 last_w = &old_weak_ptr_list;
718 for (w = old_weak_ptr_list; w; w = next_w) {
720 if ((new = isAlive(w->key))) {
722 /* evacuate the value and finaliser */
723 w->value = evacuate(w->value);
724 w->finaliser = evacuate(w->finaliser);
725 /* remove this weak ptr from the old_weak_ptr list */
727 /* and put it on the new weak ptr list */
729 w->link = weak_ptr_list;
732 IF_DEBUG(weak, fprintf(stderr,"Weak pointer still alive at %p -> %p\n", w, w->key));
742 /* If we didn't make any changes, then we can go round and kill all
743 * the dead weak pointers. The old_weak_ptr list is used as a list
744 * of pending finalisers later on.
746 if (flag == rtsFalse) {
747 for (w = old_weak_ptr_list; w; w = w->link) {
748 w->value = evacuate(w->value);
749 w->finaliser = evacuate(w->finaliser);
757 /* -----------------------------------------------------------------------------
758 isAlive determines whether the given closure is still alive (after
759 a garbage collection) or not. It returns the new address of the
760 closure if it is alive, or NULL otherwise.
761 -------------------------------------------------------------------------- */
764 isAlive(StgClosure *p)
772 /* ToDo: for static closures, check the static link field.
773 * Problem here is that we sometimes don't set the link field, eg.
774 * for static closures with an empty SRT or CONSTR_STATIC_NOCAFs.
777 /* ignore closures in generations that we're not collecting. */
778 if (LOOKS_LIKE_STATIC(p) || Bdescr((P_)p)->gen->no > N) {
782 switch (info->type) {
787 case IND_OLDGEN: /* rely on compatible layout with StgInd */
788 case IND_OLDGEN_PERM:
789 /* follow indirections */
790 p = ((StgInd *)p)->indirectee;
795 return ((StgEvacuated *)p)->evacuee;
805 MarkRoot(StgClosure *root)
807 return evacuate(root);
810 static inline void addBlock(step *step)
812 bdescr *bd = allocBlock();
816 if (step->gen->no <= N) {
822 step->hp_bd->free = step->hp;
823 step->hp_bd->link = bd;
824 step->hp = bd->start;
825 step->hpLim = step->hp + BLOCK_SIZE_W;
830 static __inline__ StgClosure *
831 copy(StgClosure *src, nat size, bdescr *bd)
836 /* Find out where we're going, using the handy "to" pointer in
837 * the step of the source object. If it turns out we need to
838 * evacuate to an older generation, adjust it here (see comment
842 if (step->gen->no < evac_gen) {
843 step = &generations[evac_gen].steps[0];
846 /* chain a new block onto the to-space for the destination step if
849 if (step->hp + size >= step->hpLim) {
855 for(to = dest, from = (P_)src; size>0; --size) {
858 return (StgClosure *)dest;
861 /* Special version of copy() for when we only want to copy the info
862 * pointer of an object, but reserve some padding after it. This is
863 * used to optimise evacuation of BLACKHOLEs.
866 static __inline__ StgClosure *
867 copyPart(StgClosure *src, nat size_to_reserve, nat size_to_copy, bdescr *bd)
873 if (step->gen->no < evac_gen) {
874 step = &generations[evac_gen].steps[0];
877 if (step->hp + size_to_reserve >= step->hpLim) {
882 step->hp += size_to_reserve;
883 for(to = dest, from = (P_)src; size_to_copy>0; --size_to_copy) {
887 return (StgClosure *)dest;
890 static __inline__ void
891 upd_evacuee(StgClosure *p, StgClosure *dest)
893 StgEvacuated *q = (StgEvacuated *)p;
895 SET_INFO(q,&EVACUATED_info);
899 /* -----------------------------------------------------------------------------
900 Evacuate a mutable object
902 If we evacuate a mutable object to an old generation, cons the
903 object onto the older generation's mutable list.
904 -------------------------------------------------------------------------- */
907 evacuate_mutable(StgMutClosure *c)
912 if (bd->gen->no > 0) {
913 c->mut_link = bd->gen->mut_list;
914 bd->gen->mut_list = c;
918 /* -----------------------------------------------------------------------------
919 Evacuate a large object
921 This just consists of removing the object from the (doubly-linked)
922 large_alloc_list, and linking it on to the (singly-linked)
923 new_large_objects list, from where it will be scavenged later.
925 Convention: bd->evacuated is /= 0 for a large object that has been
926 evacuated, or 0 otherwise.
927 -------------------------------------------------------------------------- */
930 evacuate_large(StgPtr p, rtsBool mutable)
932 bdescr *bd = Bdescr(p);
935 /* should point to the beginning of the block */
936 ASSERT(((W_)p & BLOCK_MASK) == 0);
938 /* already evacuated? */
940 /* Don't forget to set the failed_to_evac flag if we didn't get
941 * the desired destination (see comments in evacuate()).
943 if (bd->gen->no < evac_gen) {
944 failed_to_evac = rtsTrue;
950 /* remove from large_object list */
952 bd->back->link = bd->link;
953 } else { /* first object in the list */
954 step->large_objects = bd->link;
957 bd->link->back = bd->back;
960 /* link it on to the evacuated large object list of the destination step
963 if (step->gen->no < evac_gen) {
964 step = &generations[evac_gen].steps[0];
969 bd->link = step->new_large_objects;
970 step->new_large_objects = bd;
974 evacuate_mutable((StgMutClosure *)p);
978 /* -----------------------------------------------------------------------------
979 Adding a MUT_CONS to an older generation.
981 This is necessary from time to time when we end up with an
982 old-to-new generation pointer in a non-mutable object. We defer
983 the promotion until the next GC.
984 -------------------------------------------------------------------------- */
987 mkMutCons(StgClosure *ptr, generation *gen)
992 step = &gen->steps[0];
994 /* chain a new block onto the to-space for the destination step if
997 if (step->hp + sizeofW(StgIndOldGen) >= step->hpLim) {
1001 q = (StgMutVar *)step->hp;
1002 step->hp += sizeofW(StgMutVar);
1004 SET_HDR(q,&MUT_CONS_info,CCS_GC);
1006 evacuate_mutable((StgMutClosure *)q);
1008 return (StgClosure *)q;
1011 /* -----------------------------------------------------------------------------
1014 This is called (eventually) for every live object in the system.
1016 The caller to evacuate specifies a desired generation in the
1017 evac_gen global variable. The following conditions apply to
1018 evacuating an object which resides in generation M when we're
1019 collecting up to generation N
1023 else evac to step->to
1025 if M < evac_gen evac to evac_gen, step 0
1027 if the object is already evacuated, then we check which generation
1030 if M >= evac_gen do nothing
1031 if M < evac_gen set failed_to_evac flag to indicate that we
1032 didn't manage to evacuate this object into evac_gen.
1034 -------------------------------------------------------------------------- */
1038 evacuate(StgClosure *q)
1042 const StgInfoTable *info;
1045 if (!LOOKS_LIKE_STATIC(q)) {
1047 if (bd->gen->no > N) {
1048 /* Can't evacuate this object, because it's in a generation
1049 * older than the ones we're collecting. Let's hope that it's
1050 * in evac_gen or older, or we will have to make an IND_OLDGEN object.
1052 if (bd->gen->no < evac_gen) {
1054 failed_to_evac = rtsTrue;
1060 /* make sure the info pointer is into text space */
1061 ASSERT(q && (LOOKS_LIKE_GHC_INFO(GET_INFO(q))
1062 || IS_HUGS_CONSTR_INFO(GET_INFO(q))));
1065 switch (info -> type) {
1068 to = copy(q,bco_sizeW(stgCast(StgBCO*,q)),bd);
1074 to = copy(q,sizeW_fromITBL(info),bd);
1076 evacuate_mutable((StgMutClosure *)to);
1080 stable_ptr_table[((StgStableName *)q)->sn].keep = rtsTrue;
1081 to = copy(q,sizeofW(StgStableName),bd);
1089 case IND_OLDGEN_PERM:
1094 to = copy(q,sizeW_fromITBL(info),bd);
1100 to = copyPart(q,BLACKHOLE_sizeW(),sizeofW(StgHeader),bd);
1105 to = copy(q,BLACKHOLE_sizeW(),bd);
1107 evacuate_mutable((StgMutClosure *)to);
1110 case THUNK_SELECTOR:
1112 const StgInfoTable* selectee_info;
1113 StgClosure* selectee = ((StgSelector*)q)->selectee;
1116 selectee_info = get_itbl(selectee);
1117 switch (selectee_info->type) {
1121 StgNat32 offset = info->layout.selector_offset;
1123 /* check that the size is in range */
1125 (StgNat32)(selectee_info->layout.payload.ptrs +
1126 selectee_info->layout.payload.nptrs));
1128 /* perform the selection! */
1129 q = selectee->payload[offset];
1131 /* if we're already in to-space, there's no need to continue
1132 * with the evacuation, just update the source address with
1133 * a pointer to the (evacuated) constructor field.
1135 if (IS_USER_PTR(q)) {
1136 bdescr *bd = Bdescr((P_)q);
1137 if (bd->evacuated) {
1138 if (bd->gen->no < evac_gen) {
1139 failed_to_evac = rtsTrue;
1145 /* otherwise, carry on and evacuate this constructor field,
1146 * (but not the constructor itself)
1155 case IND_OLDGEN_PERM:
1156 selectee = stgCast(StgInd *,selectee)->indirectee;
1160 selectee = stgCast(StgCAF *,selectee)->value;
1164 selectee = stgCast(StgEvacuated*,selectee)->evacuee;
1169 case THUNK_SELECTOR:
1170 /* aargh - do recursively???? */
1175 /* not evaluated yet */
1179 barf("evacuate: THUNK_SELECTOR: strange selectee");
1182 to = copy(q,THUNK_SELECTOR_sizeW(),bd);
1188 /* follow chains of indirections, don't evacuate them */
1189 q = ((StgInd*)q)->indirectee;
1192 /* ToDo: optimise STATIC_LINK for known cases.
1193 - FUN_STATIC : payload[0]
1194 - THUNK_STATIC : payload[1]
1195 - IND_STATIC : payload[1]
1199 if (info->srt_len == 0) { /* small optimisation */
1205 /* don't want to evacuate these, but we do want to follow pointers
1206 * from SRTs - see scavenge_static.
1209 /* put the object on the static list, if necessary.
1211 if (major_gc && STATIC_LINK(info,(StgClosure *)q) == NULL) {
1212 STATIC_LINK(info,(StgClosure *)q) = static_objects;
1213 static_objects = (StgClosure *)q;
1217 case CONSTR_INTLIKE:
1218 case CONSTR_CHARLIKE:
1219 case CONSTR_NOCAF_STATIC:
1220 /* no need to put these on the static linked list, they don't need
1235 /* shouldn't see these */
1236 barf("evacuate: stack frame\n");
1240 /* these are special - the payload is a copy of a chunk of stack,
1242 to = copy(q,pap_sizeW(stgCast(StgPAP*,q)),bd);
1247 /* Already evacuated, just return the forwarding address.
1248 * HOWEVER: if the requested destination generation (evac_gen) is
1249 * older than the actual generation (because the object was
1250 * already evacuated to a younger generation) then we have to
1251 * set the failed_to_evac flag to indicate that we couldn't
1252 * manage to promote the object to the desired generation.
1254 if (evac_gen > 0) { /* optimisation */
1255 StgClosure *p = ((StgEvacuated*)q)->evacuee;
1256 if (Bdescr((P_)p)->gen->no < evac_gen) {
1257 /* fprintf(stderr,"evac failed!\n");*/
1258 failed_to_evac = rtsTrue;
1261 return ((StgEvacuated*)q)->evacuee;
1266 nat size = arr_words_sizeW(stgCast(StgArrWords*,q));
1268 if (size >= LARGE_OBJECT_THRESHOLD/sizeof(W_)) {
1269 evacuate_large((P_)q, rtsFalse);
1272 /* just copy the block */
1273 to = copy(q,size,bd);
1280 case MUT_ARR_PTRS_FROZEN:
1282 nat size = mut_arr_ptrs_sizeW(stgCast(StgMutArrPtrs*,q));
1284 if (size >= LARGE_OBJECT_THRESHOLD/sizeof(W_)) {
1285 evacuate_large((P_)q, info->type == MUT_ARR_PTRS);
1288 /* just copy the block */
1289 to = copy(q,size,bd);
1291 if (info->type == MUT_ARR_PTRS) {
1292 evacuate_mutable((StgMutClosure *)to);
1300 StgTSO *tso = stgCast(StgTSO *,q);
1301 nat size = tso_sizeW(tso);
1304 /* Large TSOs don't get moved, so no relocation is required.
1306 if (size >= LARGE_OBJECT_THRESHOLD/sizeof(W_)) {
1307 evacuate_large((P_)q, rtsTrue);
1310 /* To evacuate a small TSO, we need to relocate the update frame
1314 StgTSO *new_tso = (StgTSO *)copy((StgClosure *)tso,tso_sizeW(tso),bd);
1316 diff = (StgPtr)new_tso - (StgPtr)tso; /* In *words* */
1318 /* relocate the stack pointers... */
1319 new_tso->su = (StgUpdateFrame *) ((StgPtr)new_tso->su + diff);
1320 new_tso->sp = (StgPtr)new_tso->sp + diff;
1321 new_tso->splim = (StgPtr)new_tso->splim + diff;
1323 relocate_TSO(tso, new_tso);
1324 upd_evacuee(q,(StgClosure *)new_tso);
1326 evacuate_mutable((StgMutClosure *)new_tso);
1327 return (StgClosure *)new_tso;
1333 fprintf(stderr,"evacuate: unimplemented/strange closure type\n");
1337 barf("evacuate: strange closure type");
1343 /* -----------------------------------------------------------------------------
1344 relocate_TSO is called just after a TSO has been copied from src to
1345 dest. It adjusts the update frame list for the new location.
1346 -------------------------------------------------------------------------- */
1349 relocate_TSO(StgTSO *src, StgTSO *dest)
1356 diff = (StgPtr)dest->sp - (StgPtr)src->sp; /* In *words* */
1360 while ((P_)su < dest->stack + dest->stack_size) {
1361 switch (get_itbl(su)->type) {
1363 /* GCC actually manages to common up these three cases! */
1366 su->link = (StgUpdateFrame *) ((StgPtr)su->link + diff);
1371 cf = (StgCatchFrame *)su;
1372 cf->link = (StgUpdateFrame *) ((StgPtr)cf->link + diff);
1377 sf = (StgSeqFrame *)su;
1378 sf->link = (StgUpdateFrame *) ((StgPtr)sf->link + diff);
1387 barf("relocate_TSO");
1396 scavenge_srt(const StgInfoTable *info)
1398 StgClosure **srt, **srt_end;
1400 /* evacuate the SRT. If srt_len is zero, then there isn't an
1401 * srt field in the info table. That's ok, because we'll
1402 * never dereference it.
1404 srt = stgCast(StgClosure **,info->srt);
1405 srt_end = srt + info->srt_len;
1406 for (; srt < srt_end; srt++) {
1411 /* -----------------------------------------------------------------------------
1412 Scavenge a given step until there are no more objects in this step
1415 evac_gen is set by the caller to be either zero (for a step in a
1416 generation < N) or G where G is the generation of the step being
1419 We sometimes temporarily change evac_gen back to zero if we're
1420 scavenging a mutable object where early promotion isn't such a good
1422 -------------------------------------------------------------------------- */
1426 scavenge(step *step)
1429 const StgInfoTable *info;
1431 nat saved_evac_gen = evac_gen; /* used for temporarily changing evac_gen */
1436 failed_to_evac = rtsFalse;
1438 /* scavenge phase - standard breadth-first scavenging of the
1442 while (bd != step->hp_bd || p < step->hp) {
1444 /* If we're at the end of this block, move on to the next block */
1445 if (bd != step->hp_bd && p == bd->free) {
1451 q = p; /* save ptr to object */
1453 ASSERT(p && (LOOKS_LIKE_GHC_INFO(GET_INFO((StgClosure *)p))
1454 || IS_HUGS_CONSTR_INFO(GET_INFO((StgClosure *)p))));
1456 info = get_itbl((StgClosure *)p);
1457 switch (info -> type) {
1461 StgBCO* bco = stgCast(StgBCO*,p);
1463 for (i = 0; i < bco->n_ptrs; i++) {
1464 bcoConstCPtr(bco,i) = evacuate(bcoConstCPtr(bco,i));
1466 p += bco_sizeW(bco);
1471 /* treat MVars specially, because we don't want to evacuate the
1472 * mut_link field in the middle of the closure.
1475 StgMVar *mvar = ((StgMVar *)p);
1477 (StgClosure *)mvar->head = evacuate((StgClosure *)mvar->head);
1478 (StgClosure *)mvar->tail = evacuate((StgClosure *)mvar->tail);
1479 (StgClosure *)mvar->value = evacuate((StgClosure *)mvar->value);
1480 p += sizeofW(StgMVar);
1481 evac_gen = saved_evac_gen;
1495 case IND_OLDGEN_PERM:
1501 end = (P_)((StgClosure *)p)->payload + info->layout.payload.ptrs;
1502 for (p = (P_)((StgClosure *)p)->payload; p < end; p++) {
1503 (StgClosure *)*p = evacuate((StgClosure *)*p);
1505 p += info->layout.payload.nptrs;
1510 /* ignore MUT_CONSs */
1511 if (((StgMutVar *)p)->header.info != &MUT_CONS_info) {
1513 ((StgMutVar *)p)->var = evacuate(((StgMutVar *)p)->var);
1514 evac_gen = saved_evac_gen;
1516 p += sizeofW(StgMutVar);
1521 p += BLACKHOLE_sizeW();
1526 StgBlockingQueue *bh = (StgBlockingQueue *)p;
1527 (StgClosure *)bh->blocking_queue =
1528 evacuate((StgClosure *)bh->blocking_queue);
1529 if (failed_to_evac) {
1530 failed_to_evac = rtsFalse;
1531 evacuate_mutable((StgMutClosure *)bh);
1533 p += BLACKHOLE_sizeW();
1537 case THUNK_SELECTOR:
1539 StgSelector *s = (StgSelector *)p;
1540 s->selectee = evacuate(s->selectee);
1541 p += THUNK_SELECTOR_sizeW();
1547 barf("scavenge:IND???\n");
1549 case CONSTR_INTLIKE:
1550 case CONSTR_CHARLIKE:
1552 case CONSTR_NOCAF_STATIC:
1556 /* Shouldn't see a static object here. */
1557 barf("scavenge: STATIC object\n");
1569 /* Shouldn't see stack frames here. */
1570 barf("scavenge: stack frame\n");
1572 case AP_UPD: /* same as PAPs */
1574 /* Treat a PAP just like a section of stack, not forgetting to
1575 * evacuate the function pointer too...
1578 StgPAP* pap = stgCast(StgPAP*,p);
1580 pap->fun = evacuate(pap->fun);
1581 scavenge_stack((P_)pap->payload, (P_)pap->payload + pap->n_args);
1582 p += pap_sizeW(pap);
1588 /* nothing to follow */
1589 p += arr_words_sizeW(stgCast(StgArrWords*,p));
1593 /* follow everything */
1597 evac_gen = 0; /* repeatedly mutable */
1598 next = p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
1599 for (p = (P_)((StgMutArrPtrs *)p)->payload; p < next; p++) {
1600 (StgClosure *)*p = evacuate((StgClosure *)*p);
1602 evac_gen = saved_evac_gen;
1606 case MUT_ARR_PTRS_FROZEN:
1607 /* follow everything */
1609 StgPtr start = p, next;
1611 next = p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
1612 for (p = (P_)((StgMutArrPtrs *)p)->payload; p < next; p++) {
1613 (StgClosure *)*p = evacuate((StgClosure *)*p);
1615 if (failed_to_evac) {
1616 /* we can do this easier... */
1617 evacuate_mutable((StgMutClosure *)start);
1618 failed_to_evac = rtsFalse;
1629 /* chase the link field for any TSOs on the same queue */
1630 (StgClosure *)tso->link = evacuate((StgClosure *)tso->link);
1631 /* scavenge this thread's stack */
1632 scavenge_stack(tso->sp, &(tso->stack[tso->stack_size]));
1633 evac_gen = saved_evac_gen;
1634 p += tso_sizeW(tso);
1641 barf("scavenge: unimplemented/strange closure type\n");
1647 /* If we didn't manage to promote all the objects pointed to by
1648 * the current object, then we have to designate this object as
1649 * mutable (because it contains old-to-new generation pointers).
1651 if (failed_to_evac) {
1652 mkMutCons((StgClosure *)q, &generations[evac_gen]);
1653 failed_to_evac = rtsFalse;
1661 /* -----------------------------------------------------------------------------
1662 Scavenge one object.
1664 This is used for objects that are temporarily marked as mutable
1665 because they contain old-to-new generation pointers. Only certain
1666 objects can have this property.
1667 -------------------------------------------------------------------------- */
1669 scavenge_one(StgPtr p)
1674 ASSERT(p && (LOOKS_LIKE_GHC_INFO(GET_INFO((StgClosure *)p))
1675 || IS_HUGS_CONSTR_INFO(GET_INFO((StgClosure *)p))));
1677 info = get_itbl((StgClosure *)p);
1679 switch (info -> type) {
1687 case IND_OLDGEN_PERM:
1693 end = (P_)((StgClosure *)p)->payload + info->layout.payload.ptrs;
1694 for (p = (P_)((StgClosure *)p)->payload; p < end; p++) {
1695 (StgClosure *)*p = evacuate((StgClosure *)*p);
1704 case THUNK_SELECTOR:
1706 StgSelector *s = (StgSelector *)p;
1707 s->selectee = evacuate(s->selectee);
1711 case AP_UPD: /* same as PAPs */
1713 /* Treat a PAP just like a section of stack, not forgetting to
1714 * evacuate the function pointer too...
1717 StgPAP* pap = stgCast(StgPAP*,p);
1719 pap->fun = evacuate(pap->fun);
1720 scavenge_stack((P_)pap->payload, (P_)pap->payload + pap->n_args);
1725 /* This might happen if for instance a MUT_CONS was pointing to a
1726 * THUNK which has since been updated. The IND_OLDGEN will
1727 * be on the mutable list anyway, so we don't need to do anything
1733 barf("scavenge_one: strange object");
1736 no_luck = failed_to_evac;
1737 failed_to_evac = rtsFalse;
1742 /* -----------------------------------------------------------------------------
1743 Scavenging mutable lists.
1745 We treat the mutable list of each generation > N (i.e. all the
1746 generations older than the one being collected) as roots. We also
1747 remove non-mutable objects from the mutable list at this point.
1748 -------------------------------------------------------------------------- */
1750 static StgMutClosure *
1751 scavenge_mutable_list(StgMutClosure *p, nat gen)
1754 StgMutClosure *start;
1755 StgMutClosure **prev;
1762 failed_to_evac = rtsFalse;
1764 for (; p != END_MUT_LIST; p = *prev) {
1766 /* make sure the info pointer is into text space */
1767 ASSERT(p && (LOOKS_LIKE_GHC_INFO(GET_INFO(p))
1768 || IS_HUGS_CONSTR_INFO(GET_INFO(p))));
1771 switch(info->type) {
1773 case MUT_ARR_PTRS_FROZEN:
1774 /* remove this guy from the mutable list, but follow the ptrs
1775 * anyway (and make sure they get promoted to this gen).
1780 end = (P_)p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
1782 for (q = (P_)((StgMutArrPtrs *)p)->payload; q < end; q++) {
1783 (StgClosure *)*q = evacuate((StgClosure *)*q);
1787 if (failed_to_evac) {
1788 failed_to_evac = rtsFalse;
1789 prev = &p->mut_link;
1791 *prev = p->mut_link;
1797 /* follow everything */
1798 prev = &p->mut_link;
1802 end = (P_)p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
1803 for (q = (P_)((StgMutArrPtrs *)p)->payload; q < end; q++) {
1804 (StgClosure *)*q = evacuate((StgClosure *)*q);
1810 /* MUT_CONS is a kind of MUT_VAR, except that we try to remove
1811 * it from the mutable list if possible by promoting whatever it
1814 if (p->header.info == &MUT_CONS_info) {
1816 if (scavenge_one((P_)((StgMutVar *)p)->var) == rtsTrue) {
1817 /* didn't manage to promote everything, so leave the
1818 * MUT_CONS on the list.
1820 prev = &p->mut_link;
1822 *prev = p->mut_link;
1826 ((StgMutVar *)p)->var = evacuate(((StgMutVar *)p)->var);
1827 prev = &p->mut_link;
1833 StgMVar *mvar = (StgMVar *)p;
1834 (StgClosure *)mvar->head = evacuate((StgClosure *)mvar->head);
1835 (StgClosure *)mvar->tail = evacuate((StgClosure *)mvar->tail);
1836 (StgClosure *)mvar->value = evacuate((StgClosure *)mvar->value);
1837 prev = &p->mut_link;
1842 /* follow ptrs and remove this from the mutable list */
1844 StgTSO *tso = (StgTSO *)p;
1846 /* Don't bother scavenging if this thread is dead
1848 if (!(tso->whatNext == ThreadComplete ||
1849 tso->whatNext == ThreadKilled)) {
1850 /* Don't need to chase the link field for any TSOs on the
1851 * same queue. Just scavenge this thread's stack
1853 scavenge_stack(tso->sp, &(tso->stack[tso->stack_size]));
1856 /* Don't take this TSO off the mutable list - it might still
1857 * point to some younger objects (because we set evac_gen to 0
1860 prev = &tso->mut_link;
1865 case IND_OLDGEN_PERM:
1867 /* Try to pull the indirectee into this generation, so we can
1868 * remove the indirection from the mutable list.
1871 ((StgIndOldGen *)p)->indirectee =
1872 evacuate(((StgIndOldGen *)p)->indirectee);
1875 if (failed_to_evac) {
1876 failed_to_evac = rtsFalse;
1877 prev = &p->mut_link;
1879 *prev = p->mut_link;
1880 /* the mut_link field of an IND_STATIC is overloaded as the
1881 * static link field too (it just so happens that we don't need
1882 * both at the same time), so we need to NULL it out when
1883 * removing this object from the mutable list because the static
1884 * link fields are all assumed to be NULL before doing a major
1893 StgBlockingQueue *bh = (StgBlockingQueue *)p;
1894 (StgClosure *)bh->blocking_queue =
1895 evacuate((StgClosure *)bh->blocking_queue);
1896 prev = &p->mut_link;
1901 /* shouldn't have anything else on the mutables list */
1902 barf("scavenge_mutable_object: non-mutable object?");
1909 scavenge_static(void)
1911 StgClosure* p = static_objects;
1912 const StgInfoTable *info;
1914 /* Always evacuate straight to the oldest generation for static
1916 evac_gen = oldest_gen->no;
1918 /* keep going until we've scavenged all the objects on the linked
1920 while (p != END_OF_STATIC_LIST) {
1924 /* make sure the info pointer is into text space */
1925 ASSERT(p && (LOOKS_LIKE_GHC_INFO(GET_INFO(p))
1926 || IS_HUGS_CONSTR_INFO(GET_INFO(p))));
1928 /* Take this object *off* the static_objects list,
1929 * and put it on the scavenged_static_objects list.
1931 static_objects = STATIC_LINK(info,p);
1932 STATIC_LINK(info,p) = scavenged_static_objects;
1933 scavenged_static_objects = p;
1935 switch (info -> type) {
1939 StgInd *ind = (StgInd *)p;
1940 ind->indirectee = evacuate(ind->indirectee);
1942 /* might fail to evacuate it, in which case we have to pop it
1943 * back on the mutable list (and take it off the
1944 * scavenged_static list because the static link and mut link
1945 * pointers are one and the same).
1947 if (failed_to_evac) {
1948 failed_to_evac = rtsFalse;
1949 scavenged_static_objects = STATIC_LINK(info,p);
1950 ((StgMutClosure *)ind)->mut_link = oldest_gen->mut_list;
1951 oldest_gen->mut_list = (StgMutClosure *)ind;
1965 next = (P_)p->payload + info->layout.payload.ptrs;
1966 /* evacuate the pointers */
1967 for (q = (P_)p->payload; q < next; q++) {
1968 (StgClosure *)*q = evacuate((StgClosure *)*q);
1974 barf("scavenge_static");
1977 ASSERT(failed_to_evac == rtsFalse);
1979 /* get the next static object from the list. Remeber, there might
1980 * be more stuff on this list now that we've done some evacuating!
1981 * (static_objects is a global)
1987 /* -----------------------------------------------------------------------------
1988 scavenge_stack walks over a section of stack and evacuates all the
1989 objects pointed to by it. We can use the same code for walking
1990 PAPs, since these are just sections of copied stack.
1991 -------------------------------------------------------------------------- */
1994 scavenge_stack(StgPtr p, StgPtr stack_end)
1997 const StgInfoTable* info;
2001 * Each time around this loop, we are looking at a chunk of stack
2002 * that starts with either a pending argument section or an
2003 * activation record.
2006 while (p < stack_end) {
2007 q = *stgCast(StgPtr*,p);
2009 /* If we've got a tag, skip over that many words on the stack */
2010 if (IS_ARG_TAG(stgCast(StgWord,q))) {
2015 /* Is q a pointer to a closure?
2017 if (! LOOKS_LIKE_GHC_INFO(q)) {
2020 if (LOOKS_LIKE_STATIC(q)) { /* Is it a static closure? */
2021 ASSERT(closure_STATIC(stgCast(StgClosure*,q)));
2023 /* otherwise, must be a pointer into the allocation space.
2027 (StgClosure *)*p = evacuate((StgClosure *)q);
2033 * Otherwise, q must be the info pointer of an activation
2034 * record. All activation records have 'bitmap' style layout
2037 info = get_itbl(stgCast(StgClosure*,p));
2039 switch (info->type) {
2041 /* Dynamic bitmap: the mask is stored on the stack */
2043 bitmap = stgCast(StgRetDyn*,p)->liveness;
2044 p = &payloadWord(stgCast(StgRetDyn*,p),0);
2047 /* probably a slow-entry point return address: */
2053 /* Specialised code for update frames, since they're so common.
2054 * We *know* the updatee points to a BLACKHOLE, CAF_BLACKHOLE,
2055 * or BLACKHOLE_BQ, so just inline the code to evacuate it here.
2059 StgUpdateFrame *frame = (StgUpdateFrame *)p;
2061 StgClosureType type = get_itbl(frame->updatee)->type;
2063 p += sizeofW(StgUpdateFrame);
2064 if (type == EVACUATED) {
2065 frame->updatee = evacuate(frame->updatee);
2068 bdescr *bd = Bdescr((P_)frame->updatee);
2069 if (bd->gen->no > N) {
2070 if (bd->gen->no < evac_gen) {
2071 failed_to_evac = rtsTrue;
2078 to = copyPart(frame->updatee, BLACKHOLE_sizeW(),
2079 sizeofW(StgHeader), bd);
2080 upd_evacuee(frame->updatee,to);
2081 frame->updatee = to;
2084 to = copy(frame->updatee, BLACKHOLE_sizeW(), bd);
2085 upd_evacuee(frame->updatee,to);
2086 frame->updatee = to;
2087 evacuate_mutable((StgMutClosure *)to);
2090 barf("scavenge_stack: UPDATE_FRAME updatee");
2095 /* small bitmap (< 32 entries, or 64 on a 64-bit machine) */
2102 bitmap = info->layout.bitmap;
2105 while (bitmap != 0) {
2106 if ((bitmap & 1) == 0) {
2107 (StgClosure *)*p = evacuate((StgClosure *)*p);
2110 bitmap = bitmap >> 1;
2117 /* large bitmap (> 32 entries) */
2122 StgLargeBitmap *large_bitmap;
2125 large_bitmap = info->layout.large_bitmap;
2128 for (i=0; i<large_bitmap->size; i++) {
2129 bitmap = large_bitmap->bitmap[i];
2130 q = p + sizeof(W_) * 8;
2131 while (bitmap != 0) {
2132 if ((bitmap & 1) == 0) {
2133 (StgClosure *)*p = evacuate((StgClosure *)*p);
2136 bitmap = bitmap >> 1;
2138 if (i+1 < large_bitmap->size) {
2140 (StgClosure *)*p = evacuate((StgClosure *)*p);
2146 /* and don't forget to follow the SRT */
2151 barf("scavenge_stack: weird activation record found on stack.\n");
2156 /*-----------------------------------------------------------------------------
2157 scavenge the large object list.
2159 evac_gen set by caller; similar games played with evac_gen as with
2160 scavenge() - see comment at the top of scavenge(). Most large
2161 objects are (repeatedly) mutable, so most of the time evac_gen will
2163 --------------------------------------------------------------------------- */
2166 scavenge_large(step *step)
2170 const StgInfoTable* info;
2171 nat saved_evac_gen = evac_gen; /* used for temporarily changing evac_gen */
2173 evac_gen = 0; /* most objects are mutable */
2174 bd = step->new_large_objects;
2176 for (; bd != NULL; bd = step->new_large_objects) {
2178 /* take this object *off* the large objects list and put it on
2179 * the scavenged large objects list. This is so that we can
2180 * treat new_large_objects as a stack and push new objects on
2181 * the front when evacuating.
2183 step->new_large_objects = bd->link;
2184 dbl_link_onto(bd, &step->scavenged_large_objects);
2187 info = get_itbl(stgCast(StgClosure*,p));
2189 switch (info->type) {
2191 /* only certain objects can be "large"... */
2195 /* nothing to follow */
2199 /* follow everything */
2203 next = p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
2204 for (p = (P_)((StgMutArrPtrs *)p)->payload; p < next; p++) {
2205 (StgClosure *)*p = evacuate((StgClosure *)*p);
2210 case MUT_ARR_PTRS_FROZEN:
2211 /* follow everything */
2213 StgPtr start = p, next;
2215 evac_gen = saved_evac_gen; /* not really mutable */
2216 next = p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
2217 for (p = (P_)((StgMutArrPtrs *)p)->payload; p < next; p++) {
2218 (StgClosure *)*p = evacuate((StgClosure *)*p);
2221 if (failed_to_evac) {
2222 evacuate_mutable((StgMutClosure *)start);
2229 StgBCO* bco = stgCast(StgBCO*,p);
2231 evac_gen = saved_evac_gen;
2232 for (i = 0; i < bco->n_ptrs; i++) {
2233 bcoConstCPtr(bco,i) = evacuate(bcoConstCPtr(bco,i));
2244 /* chase the link field for any TSOs on the same queue */
2245 (StgClosure *)tso->link = evacuate((StgClosure *)tso->link);
2246 /* scavenge this thread's stack */
2247 scavenge_stack(tso->sp, &(tso->stack[tso->stack_size]));
2252 barf("scavenge_large: unknown/strange object");
2258 zeroStaticObjectList(StgClosure* first_static)
2262 const StgInfoTable *info;
2264 for (p = first_static; p != END_OF_STATIC_LIST; p = link) {
2266 link = STATIC_LINK(info, p);
2267 STATIC_LINK(info,p) = NULL;
2271 /* This function is only needed because we share the mutable link
2272 * field with the static link field in an IND_STATIC, so we have to
2273 * zero the mut_link field before doing a major GC, which needs the
2274 * static link field.
2276 * It doesn't do any harm to zero all the mutable link fields on the
2280 zeroMutableList(StgMutClosure *first)
2282 StgMutClosure *next, *c;
2284 for (c = first; c != END_MUT_LIST; c = next) {
2290 /* -----------------------------------------------------------------------------
2292 -------------------------------------------------------------------------- */
2294 void RevertCAFs(void)
2296 while (enteredCAFs != END_CAF_LIST) {
2297 StgCAF* caf = enteredCAFs;
2299 enteredCAFs = caf->link;
2300 ASSERT(get_itbl(caf)->type == CAF_ENTERED);
2301 SET_INFO(caf,&CAF_UNENTERED_info);
2302 caf->value = stgCast(StgClosure*,0xdeadbeef);
2303 caf->link = stgCast(StgCAF*,0xdeadbeef);
2307 void revertDeadCAFs(void)
2309 StgCAF* caf = enteredCAFs;
2310 enteredCAFs = END_CAF_LIST;
2311 while (caf != END_CAF_LIST) {
2312 StgCAF* next = caf->link;
2314 switch(GET_INFO(caf)->type) {
2317 /* This object has been evacuated, it must be live. */
2318 StgCAF* new = stgCast(StgCAF*,stgCast(StgEvacuated*,caf)->evacuee);
2319 new->link = enteredCAFs;
2325 SET_INFO(caf,&CAF_UNENTERED_info);
2326 caf->value = stgCast(StgClosure*,0xdeadbeef);
2327 caf->link = stgCast(StgCAF*,0xdeadbeef);
2331 barf("revertDeadCAFs: enteredCAFs list corrupted");
2337 /* -----------------------------------------------------------------------------
2338 Sanity code for CAF garbage collection.
2340 With DEBUG turned on, we manage a CAF list in addition to the SRT
2341 mechanism. After GC, we run down the CAF list and blackhole any
2342 CAFs which have been garbage collected. This means we get an error
2343 whenever the program tries to enter a garbage collected CAF.
2345 Any garbage collected CAFs are taken off the CAF list at the same
2347 -------------------------------------------------------------------------- */
2355 const StgInfoTable *info;
2366 ASSERT(info->type == IND_STATIC);
2368 if (STATIC_LINK(info,p) == NULL) {
2369 IF_DEBUG(gccafs, fprintf(stderr, "CAF gc'd at 0x%04x\n", (int)p));
2371 SET_INFO(p,&BLACKHOLE_info);
2372 p = STATIC_LINK2(info,p);
2376 pp = &STATIC_LINK2(info,p);
2383 /* fprintf(stderr, "%d CAFs live\n", i); */
2387 /* -----------------------------------------------------------------------------
2390 Whenever a thread returns to the scheduler after possibly doing
2391 some work, we have to run down the stack and black-hole all the
2392 closures referred to by update frames.
2393 -------------------------------------------------------------------------- */
2396 threadLazyBlackHole(StgTSO *tso)
2398 StgUpdateFrame *update_frame;
2399 StgBlockingQueue *bh;
2402 stack_end = &tso->stack[tso->stack_size];
2403 update_frame = tso->su;
2406 switch (get_itbl(update_frame)->type) {
2409 update_frame = stgCast(StgCatchFrame*,update_frame)->link;
2413 bh = (StgBlockingQueue *)update_frame->updatee;
2415 /* if the thunk is already blackholed, it means we've also
2416 * already blackholed the rest of the thunks on this stack,
2417 * so we can stop early.
2419 * The blackhole made for a CAF is a CAF_BLACKHOLE, so they
2420 * don't interfere with this optimisation.
2422 if (bh->header.info == &BLACKHOLE_info) {
2426 if (bh->header.info != &BLACKHOLE_BQ_info &&
2427 bh->header.info != &CAF_BLACKHOLE_info) {
2428 SET_INFO(bh,&BLACKHOLE_info);
2431 update_frame = update_frame->link;
2435 update_frame = stgCast(StgSeqFrame*,update_frame)->link;
2441 barf("threadPaused");
2446 /* -----------------------------------------------------------------------------
2449 * Code largely pinched from old RTS, then hacked to bits. We also do
2450 * lazy black holing here.
2452 * -------------------------------------------------------------------------- */
2455 threadSqueezeStack(StgTSO *tso)
2457 lnat displacement = 0;
2458 StgUpdateFrame *frame;
2459 StgUpdateFrame *next_frame; /* Temporally next */
2460 StgUpdateFrame *prev_frame; /* Temporally previous */
2462 rtsBool prev_was_update_frame;
2464 bottom = &(tso->stack[tso->stack_size]);
2467 /* There must be at least one frame, namely the STOP_FRAME.
2469 ASSERT((P_)frame < bottom);
2471 /* Walk down the stack, reversing the links between frames so that
2472 * we can walk back up as we squeeze from the bottom. Note that
2473 * next_frame and prev_frame refer to next and previous as they were
2474 * added to the stack, rather than the way we see them in this
2475 * walk. (It makes the next loop less confusing.)
2477 * Stop if we find an update frame pointing to a black hole
2478 * (see comment in threadLazyBlackHole()).
2482 while ((P_)frame < bottom - 1) { /* bottom - 1 is the STOP_FRAME */
2483 prev_frame = frame->link;
2484 frame->link = next_frame;
2487 if (get_itbl(frame)->type == UPDATE_FRAME
2488 && frame->updatee->header.info == &BLACKHOLE_info) {
2493 /* Now, we're at the bottom. Frame points to the lowest update
2494 * frame on the stack, and its link actually points to the frame
2495 * above. We have to walk back up the stack, squeezing out empty
2496 * update frames and turning the pointers back around on the way
2499 * The bottom-most frame (the STOP_FRAME) has not been altered, and
2500 * we never want to eliminate it anyway. Just walk one step up
2501 * before starting to squeeze. When you get to the topmost frame,
2502 * remember that there are still some words above it that might have
2509 prev_was_update_frame = (get_itbl(prev_frame)->type == UPDATE_FRAME);
2512 * Loop through all of the frames (everything except the very
2513 * bottom). Things are complicated by the fact that we have
2514 * CATCH_FRAMEs and SEQ_FRAMEs interspersed with the update frames.
2515 * We can only squeeze when there are two consecutive UPDATE_FRAMEs.
2517 while (frame != NULL) {
2519 StgPtr frame_bottom = (P_)frame + sizeofW(StgUpdateFrame);
2520 rtsBool is_update_frame;
2522 next_frame = frame->link;
2523 is_update_frame = (get_itbl(frame)->type == UPDATE_FRAME);
2526 * 1. both the previous and current frame are update frames
2527 * 2. the current frame is empty
2529 if (prev_was_update_frame && is_update_frame &&
2530 (P_)prev_frame == frame_bottom + displacement) {
2532 /* Now squeeze out the current frame */
2533 StgClosure *updatee_keep = prev_frame->updatee;
2534 StgClosure *updatee_bypass = frame->updatee;
2537 fprintf(stderr, "squeezing frame at %p\n", frame);
2540 /* Deal with blocking queues. If both updatees have blocked
2541 * threads, then we should merge the queues into the update
2542 * frame that we're keeping.
2544 * Alternatively, we could just wake them up: they'll just go
2545 * straight to sleep on the proper blackhole! This is less code
2546 * and probably less bug prone, although it's probably much
2549 #if 0 /* do it properly... */
2550 if (GET_INFO(updatee_bypass) == BLACKHOLE_BQ_info) {
2551 /* Sigh. It has one. Don't lose those threads! */
2552 if (GET_INFO(updatee_keep) == BLACKHOLE_BQ_info) {
2553 /* Urgh. Two queues. Merge them. */
2554 P_ keep_tso = ((StgBlockingQueue *)updatee_keep)->blocking_queue;
2556 while (keep_tso->link != END_TSO_QUEUE) {
2557 keep_tso = keep_tso->link;
2559 keep_tso->link = ((StgBlockingQueue *)updatee_bypass)->blocking_queue;
2562 /* For simplicity, just swap the BQ for the BH */
2563 P_ temp = updatee_keep;
2565 updatee_keep = updatee_bypass;
2566 updatee_bypass = temp;
2568 /* Record the swap in the kept frame (below) */
2569 prev_frame->updatee = updatee_keep;
2574 TICK_UPD_SQUEEZED();
2575 UPD_IND(updatee_bypass, updatee_keep); /* this wakes the threads up */
2577 sp = (P_)frame - 1; /* sp = stuff to slide */
2578 displacement += sizeofW(StgUpdateFrame);
2581 /* No squeeze for this frame */
2582 sp = frame_bottom - 1; /* Keep the current frame */
2584 /* Do lazy black-holing.
2586 if (is_update_frame) {
2587 StgBlockingQueue *bh = (StgBlockingQueue *)frame->updatee;
2588 if (bh->header.info != &BLACKHOLE_BQ_info &&
2589 bh->header.info != &CAF_BLACKHOLE_info) {
2590 SET_INFO(bh,&BLACKHOLE_info);
2594 /* Fix the link in the current frame (should point to the frame below) */
2595 frame->link = prev_frame;
2596 prev_was_update_frame = is_update_frame;
2599 /* Now slide all words from sp up to the next frame */
2601 if (displacement > 0) {
2602 P_ next_frame_bottom;
2604 if (next_frame != NULL)
2605 next_frame_bottom = (P_)next_frame + sizeofW(StgUpdateFrame);
2607 next_frame_bottom = tso->sp - 1;
2610 fprintf(stderr, "sliding [%p, %p] by %ld\n", sp, next_frame_bottom,
2614 while (sp >= next_frame_bottom) {
2615 sp[displacement] = *sp;
2619 (P_)prev_frame = (P_)frame + displacement;
2623 tso->sp += displacement;
2624 tso->su = prev_frame;
2627 /* -----------------------------------------------------------------------------
2630 * We have to prepare for GC - this means doing lazy black holing
2631 * here. We also take the opportunity to do stack squeezing if it's
2633 * -------------------------------------------------------------------------- */
2636 threadPaused(StgTSO *tso)
2638 if ( RtsFlags.GcFlags.squeezeUpdFrames == rtsTrue )
2639 threadSqueezeStack(tso); /* does black holing too */
2641 threadLazyBlackHole(tso);