1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team, 1998-2008
5 * Storage manager front end
7 * Documentation on the architecture of the Storage Manager can be
8 * found in the online commentary:
10 * http://hackage.haskell.org/trac/ghc/wiki/Commentary/Rts/Storage
12 * ---------------------------------------------------------------------------*/
14 #include "PosixSource.h"
20 #include "BlockAlloc.h"
24 #include "Capability.h"
26 #include "RetainerProfile.h" // for counting memory blocks (memInventory)
37 * All these globals require sm_mutex to access in THREADED_RTS mode.
39 StgClosure *caf_list = NULL;
40 StgClosure *revertible_caf_list = NULL;
43 bdescr *pinned_object_block; /* allocate pinned objects into this block */
44 nat alloc_blocks; /* number of allocate()d blocks since GC */
45 nat alloc_blocks_lim; /* approximate limit on alloc_blocks */
47 static bdescr *exec_block;
49 generation *generations = NULL; /* all the generations */
50 generation *g0 = NULL; /* generation 0, for convenience */
51 generation *oldest_gen = NULL; /* oldest generation, for convenience */
52 step *g0s0 = NULL; /* generation 0, step 0, for convenience */
55 step *all_steps = NULL; /* single array of steps */
57 ullong total_allocated = 0; /* total memory allocated during run */
59 nat n_nurseries = 0; /* == RtsFlags.ParFlags.nNodes, convenience */
60 step *nurseries = NULL; /* array of nurseries, >1 only if THREADED_RTS */
64 * Storage manager mutex: protects all the above state from
65 * simultaneous access by two STG threads.
70 static void allocNurseries ( void );
73 initStep (step *stp, int g, int s)
76 stp->abs_no = RtsFlags.GcFlags.steps * g + s;
80 stp->live_estimate = 0;
81 stp->old_blocks = NULL;
82 stp->n_old_blocks = 0;
83 stp->gen = &generations[g];
85 stp->large_objects = NULL;
86 stp->n_large_blocks = 0;
87 stp->scavenged_large_objects = NULL;
88 stp->n_scavenged_large_blocks = 0;
93 initSpinLock(&stp->sync_large_objects);
95 stp->threads = END_TSO_QUEUE;
96 stp->old_threads = END_TSO_QUEUE;
105 if (generations != NULL) {
106 // multi-init protection
112 /* Sanity check to make sure the LOOKS_LIKE_ macros appear to be
113 * doing something reasonable.
115 /* We use the NOT_NULL variant or gcc warns that the test is always true */
116 ASSERT(LOOKS_LIKE_INFO_PTR_NOT_NULL((StgWord)&stg_BLACKHOLE_info));
117 ASSERT(LOOKS_LIKE_CLOSURE_PTR(&stg_dummy_ret_closure));
118 ASSERT(!HEAP_ALLOCED(&stg_dummy_ret_closure));
120 if (RtsFlags.GcFlags.maxHeapSize != 0 &&
121 RtsFlags.GcFlags.heapSizeSuggestion >
122 RtsFlags.GcFlags.maxHeapSize) {
123 RtsFlags.GcFlags.maxHeapSize = RtsFlags.GcFlags.heapSizeSuggestion;
126 if (RtsFlags.GcFlags.maxHeapSize != 0 &&
127 RtsFlags.GcFlags.minAllocAreaSize >
128 RtsFlags.GcFlags.maxHeapSize) {
129 errorBelch("maximum heap size (-M) is smaller than minimum alloc area size (-A)");
130 RtsFlags.GcFlags.minAllocAreaSize = RtsFlags.GcFlags.maxHeapSize;
133 initBlockAllocator();
135 #if defined(THREADED_RTS)
136 initMutex(&sm_mutex);
141 /* allocate generation info array */
142 generations = (generation *)stgMallocBytes(RtsFlags.GcFlags.generations
143 * sizeof(struct generation_),
144 "initStorage: gens");
146 /* allocate all the steps into an array. It is important that we do
147 it this way, because we need the invariant that two step pointers
148 can be directly compared to see which is the oldest.
149 Remember that the last generation has only one step. */
150 total_steps = 1 + (RtsFlags.GcFlags.generations - 1) * RtsFlags.GcFlags.steps;
151 all_steps = stgMallocBytes(total_steps * sizeof(struct step_),
152 "initStorage: steps");
154 /* Initialise all generations */
155 for(g = 0; g < RtsFlags.GcFlags.generations; g++) {
156 gen = &generations[g];
158 gen->mut_list = allocBlock();
159 gen->collections = 0;
160 gen->par_collections = 0;
161 gen->failed_promotions = 0;
165 /* A couple of convenience pointers */
166 g0 = &generations[0];
167 oldest_gen = &generations[RtsFlags.GcFlags.generations-1];
169 /* Allocate step structures in each generation */
170 if (RtsFlags.GcFlags.generations > 1) {
171 /* Only for multiple-generations */
173 /* Oldest generation: one step */
174 oldest_gen->n_steps = 1;
175 oldest_gen->steps = all_steps + (RtsFlags.GcFlags.generations - 1)
176 * RtsFlags.GcFlags.steps;
178 /* set up all except the oldest generation with 2 steps */
179 for(g = 0; g < RtsFlags.GcFlags.generations-1; g++) {
180 generations[g].n_steps = RtsFlags.GcFlags.steps;
181 generations[g].steps = all_steps + g * RtsFlags.GcFlags.steps;
185 /* single generation, i.e. a two-space collector */
187 g0->steps = all_steps;
191 n_nurseries = n_capabilities;
195 nurseries = stgMallocBytes (n_nurseries * sizeof(struct step_),
196 "initStorage: nurseries");
198 /* Initialise all steps */
199 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
200 for (s = 0; s < generations[g].n_steps; s++) {
201 initStep(&generations[g].steps[s], g, s);
205 for (s = 0; s < n_nurseries; s++) {
206 initStep(&nurseries[s], 0, s);
209 /* Set up the destination pointers in each younger gen. step */
210 for (g = 0; g < RtsFlags.GcFlags.generations-1; g++) {
211 for (s = 0; s < generations[g].n_steps-1; s++) {
212 generations[g].steps[s].to = &generations[g].steps[s+1];
214 generations[g].steps[s].to = &generations[g+1].steps[0];
216 oldest_gen->steps[0].to = &oldest_gen->steps[0];
218 for (s = 0; s < n_nurseries; s++) {
219 nurseries[s].to = generations[0].steps[0].to;
222 /* The oldest generation has one step. */
223 if (RtsFlags.GcFlags.compact || RtsFlags.GcFlags.sweep) {
224 if (RtsFlags.GcFlags.generations == 1) {
225 errorBelch("WARNING: compact/sweep is incompatible with -G1; disabled");
227 oldest_gen->steps[0].mark = 1;
228 if (RtsFlags.GcFlags.compact)
229 oldest_gen->steps[0].compact = 1;
233 generations[0].max_blocks = 0;
234 g0s0 = &generations[0].steps[0];
236 /* The allocation area. Policy: keep the allocation area
237 * small to begin with, even if we have a large suggested heap
238 * size. Reason: we're going to do a major collection first, and we
239 * don't want it to be a big one. This vague idea is borne out by
240 * rigorous experimental evidence.
244 weak_ptr_list = NULL;
246 revertible_caf_list = NULL;
248 /* initialise the allocate() interface */
250 alloc_blocks_lim = RtsFlags.GcFlags.minAllocAreaSize;
255 initSpinLock(&gc_alloc_block_sync);
263 IF_DEBUG(gc, statDescribeGens());
271 stat_exit(calcAllocated());
277 stgFree(g0s0); // frees all the steps
278 stgFree(generations);
280 #if defined(THREADED_RTS)
281 closeMutex(&sm_mutex);
287 /* -----------------------------------------------------------------------------
290 The entry code for every CAF does the following:
292 - builds a CAF_BLACKHOLE in the heap
293 - pushes an update frame pointing to the CAF_BLACKHOLE
294 - invokes UPD_CAF(), which:
295 - calls newCaf, below
296 - updates the CAF with a static indirection to the CAF_BLACKHOLE
298 Why do we build a BLACKHOLE in the heap rather than just updating
299 the thunk directly? It's so that we only need one kind of update
300 frame - otherwise we'd need a static version of the update frame too.
302 newCaf() does the following:
304 - it puts the CAF on the oldest generation's mut-once list.
305 This is so that we can treat the CAF as a root when collecting
308 For GHCI, we have additional requirements when dealing with CAFs:
310 - we must *retain* all dynamically-loaded CAFs ever entered,
311 just in case we need them again.
312 - we must be able to *revert* CAFs that have been evaluated, to
313 their pre-evaluated form.
315 To do this, we use an additional CAF list. When newCaf() is
316 called on a dynamically-loaded CAF, we add it to the CAF list
317 instead of the old-generation mutable list, and save away its
318 old info pointer (in caf->saved_info) for later reversion.
320 To revert all the CAFs, we traverse the CAF list and reset the
321 info pointer to caf->saved_info, then throw away the CAF list.
322 (see GC.c:revertCAFs()).
326 -------------------------------------------------------------------------- */
329 newCAF(StgClosure* caf)
337 // If we are in GHCi _and_ we are using dynamic libraries,
338 // then we can't redirect newCAF calls to newDynCAF (see below),
339 // so we make newCAF behave almost like newDynCAF.
340 // The dynamic libraries might be used by both the interpreted
341 // program and GHCi itself, so they must not be reverted.
342 // This also means that in GHCi with dynamic libraries, CAFs are not
343 // garbage collected. If this turns out to be a problem, we could
344 // do another hack here and do an address range test on caf to figure
345 // out whether it is from a dynamic library.
346 ((StgIndStatic *)caf)->saved_info = (StgInfoTable *)caf->header.info;
347 ((StgIndStatic *)caf)->static_link = caf_list;
353 /* Put this CAF on the mutable list for the old generation.
354 * This is a HACK - the IND_STATIC closure doesn't really have
355 * a mut_link field, but we pretend it has - in fact we re-use
356 * the STATIC_LINK field for the time being, because when we
357 * come to do a major GC we won't need the mut_link field
358 * any more and can use it as a STATIC_LINK.
360 ((StgIndStatic *)caf)->saved_info = NULL;
361 recordMutableGen(caf, oldest_gen->no);
367 // An alternate version of newCaf which is used for dynamically loaded
368 // object code in GHCi. In this case we want to retain *all* CAFs in
369 // the object code, because they might be demanded at any time from an
370 // expression evaluated on the command line.
371 // Also, GHCi might want to revert CAFs, so we add these to the
372 // revertible_caf_list.
374 // The linker hackily arranges that references to newCaf from dynamic
375 // code end up pointing to newDynCAF.
377 newDynCAF(StgClosure *caf)
381 ((StgIndStatic *)caf)->saved_info = (StgInfoTable *)caf->header.info;
382 ((StgIndStatic *)caf)->static_link = revertible_caf_list;
383 revertible_caf_list = caf;
388 /* -----------------------------------------------------------------------------
390 -------------------------------------------------------------------------- */
393 allocNursery (step *stp, bdescr *tail, nat blocks)
398 // Allocate a nursery: we allocate fresh blocks one at a time and
399 // cons them on to the front of the list, not forgetting to update
400 // the back pointer on the tail of the list to point to the new block.
401 for (i=0; i < blocks; i++) {
404 processNursery() in LdvProfile.c assumes that every block group in
405 the nursery contains only a single block. So, if a block group is
406 given multiple blocks, change processNursery() accordingly.
410 // double-link the nursery: we might need to insert blocks
416 bd->free = bd->start;
424 assignNurseriesToCapabilities (void)
429 for (i = 0; i < n_nurseries; i++) {
430 capabilities[i].r.rNursery = &nurseries[i];
431 capabilities[i].r.rCurrentNursery = nurseries[i].blocks;
432 capabilities[i].r.rCurrentAlloc = NULL;
434 #else /* THREADED_RTS */
435 MainCapability.r.rNursery = &nurseries[0];
436 MainCapability.r.rCurrentNursery = nurseries[0].blocks;
437 MainCapability.r.rCurrentAlloc = NULL;
442 allocNurseries( void )
446 for (i = 0; i < n_nurseries; i++) {
447 nurseries[i].blocks =
448 allocNursery(&nurseries[i], NULL,
449 RtsFlags.GcFlags.minAllocAreaSize);
450 nurseries[i].n_blocks = RtsFlags.GcFlags.minAllocAreaSize;
451 nurseries[i].old_blocks = NULL;
452 nurseries[i].n_old_blocks = 0;
454 assignNurseriesToCapabilities();
458 resetNurseries( void )
464 for (i = 0; i < n_nurseries; i++) {
466 for (bd = stp->blocks; bd; bd = bd->link) {
467 bd->free = bd->start;
468 ASSERT(bd->gen_no == 0);
469 ASSERT(bd->step == stp);
470 IF_DEBUG(sanity,memset(bd->start, 0xaa, BLOCK_SIZE));
473 assignNurseriesToCapabilities();
477 countNurseryBlocks (void)
482 for (i = 0; i < n_nurseries; i++) {
483 blocks += nurseries[i].n_blocks;
489 resizeNursery ( step *stp, nat blocks )
494 nursery_blocks = stp->n_blocks;
495 if (nursery_blocks == blocks) return;
497 if (nursery_blocks < blocks) {
498 debugTrace(DEBUG_gc, "increasing size of nursery to %d blocks",
500 stp->blocks = allocNursery(stp, stp->blocks, blocks-nursery_blocks);
505 debugTrace(DEBUG_gc, "decreasing size of nursery to %d blocks",
509 while (nursery_blocks > blocks) {
511 next_bd->u.back = NULL;
512 nursery_blocks -= bd->blocks; // might be a large block
517 // might have gone just under, by freeing a large block, so make
518 // up the difference.
519 if (nursery_blocks < blocks) {
520 stp->blocks = allocNursery(stp, stp->blocks, blocks-nursery_blocks);
524 stp->n_blocks = blocks;
525 ASSERT(countBlocks(stp->blocks) == stp->n_blocks);
529 // Resize each of the nurseries to the specified size.
532 resizeNurseriesFixed (nat blocks)
535 for (i = 0; i < n_nurseries; i++) {
536 resizeNursery(&nurseries[i], blocks);
541 // Resize the nurseries to the total specified size.
544 resizeNurseries (nat blocks)
546 // If there are multiple nurseries, then we just divide the number
547 // of available blocks between them.
548 resizeNurseriesFixed(blocks / n_nurseries);
552 /* -----------------------------------------------------------------------------
553 move_TSO is called to update the TSO structure after it has been
554 moved from one place to another.
555 -------------------------------------------------------------------------- */
558 move_TSO (StgTSO *src, StgTSO *dest)
562 // relocate the stack pointer...
563 diff = (StgPtr)dest - (StgPtr)src; // In *words*
564 dest->sp = (StgPtr)dest->sp + diff;
567 /* -----------------------------------------------------------------------------
568 The allocate() interface
570 allocateInGen() function allocates memory directly into a specific
571 generation. It always succeeds, and returns a chunk of memory n
572 words long. n can be larger than the size of a block if necessary,
573 in which case a contiguous block group will be allocated.
575 allocate(n) is equivalent to allocateInGen(g0).
576 -------------------------------------------------------------------------- */
579 allocateInGen (generation *g, lnat n)
587 TICK_ALLOC_HEAP_NOCTR(n);
592 if (n >= LARGE_OBJECT_THRESHOLD/sizeof(W_))
594 lnat req_blocks = (lnat)BLOCK_ROUND_UP(n*sizeof(W_)) / BLOCK_SIZE;
596 // Attempting to allocate an object larger than maxHeapSize
597 // should definitely be disallowed. (bug #1791)
598 if (RtsFlags.GcFlags.maxHeapSize > 0 &&
599 req_blocks >= RtsFlags.GcFlags.maxHeapSize) {
601 // heapOverflow() doesn't exit (see #2592), but we aren't
602 // in a position to do a clean shutdown here: we
603 // either have to allocate the memory or exit now.
604 // Allocating the memory would be bad, because the user
605 // has requested that we not exceed maxHeapSize, so we
607 stg_exit(EXIT_HEAPOVERFLOW);
610 bd = allocGroup(req_blocks);
611 dbl_link_onto(bd, &stp->large_objects);
612 stp->n_large_blocks += bd->blocks; // might be larger than req_blocks
613 alloc_blocks += bd->blocks;
615 bd->flags = BF_LARGE;
616 bd->free = bd->start + n;
621 // small allocation (<LARGE_OBJECT_THRESHOLD) */
623 if (bd == NULL || bd->free + n > bd->start + BLOCK_SIZE_W) {
627 bd->link = stp->blocks;
644 return allocateInGen(g0,n);
648 allocatedBytes( void )
652 allocated = alloc_blocks * BLOCK_SIZE_W;
653 if (pinned_object_block != NULL) {
654 allocated -= (pinned_object_block->start + BLOCK_SIZE_W) -
655 pinned_object_block->free;
661 // split N blocks off the front of the given bdescr, returning the
662 // new block group. We treat the remainder as if it
663 // had been freshly allocated in generation 0.
665 splitLargeBlock (bdescr *bd, nat blocks)
669 // subtract the original number of blocks from the counter first
670 bd->step->n_large_blocks -= bd->blocks;
672 new_bd = splitBlockGroup (bd, blocks);
674 dbl_link_onto(new_bd, &g0s0->large_objects);
675 g0s0->n_large_blocks += new_bd->blocks;
676 initBdescr(new_bd, g0s0);
677 new_bd->flags = BF_LARGE;
678 new_bd->free = bd->free;
679 ASSERT(new_bd->free <= new_bd->start + new_bd->blocks * BLOCK_SIZE_W);
681 // add the new number of blocks to the counter. Due to the gaps
682 // for block descriptor, new_bd->blocks + bd->blocks might not be
683 // equal to the original bd->blocks, which is why we do it this way.
684 bd->step->n_large_blocks += bd->blocks;
689 /* -----------------------------------------------------------------------------
692 This allocates memory in the current thread - it is intended for
693 use primarily from STG-land where we have a Capability. It is
694 better than allocate() because it doesn't require taking the
695 sm_mutex lock in the common case.
697 Memory is allocated directly from the nursery if possible (but not
698 from the current nursery block, so as not to interfere with
700 -------------------------------------------------------------------------- */
703 allocateLocal (Capability *cap, lnat n)
708 if (n >= LARGE_OBJECT_THRESHOLD/sizeof(W_)) {
709 return allocateInGen(g0,n);
712 /* small allocation (<LARGE_OBJECT_THRESHOLD) */
714 TICK_ALLOC_HEAP_NOCTR(n);
717 bd = cap->r.rCurrentAlloc;
718 if (bd == NULL || bd->free + n > bd->start + BLOCK_SIZE_W) {
720 // The CurrentAlloc block is full, we need to find another
721 // one. First, we try taking the next block from the
723 bd = cap->r.rCurrentNursery->link;
725 if (bd == NULL || bd->free + n > bd->start + BLOCK_SIZE_W) {
726 // The nursery is empty, or the next block is already
727 // full: allocate a fresh block (we can't fail here).
730 cap->r.rNursery->n_blocks++;
732 initBdescr(bd, cap->r.rNursery);
734 // NO: alloc_blocks++;
735 // calcAllocated() uses the size of the nursery, and we've
736 // already bumpted nursery->n_blocks above. We'll GC
737 // pretty quickly now anyway, because MAYBE_GC() will
738 // notice that CurrentNursery->link is NULL.
740 // we have a block in the nursery: take it and put
741 // it at the *front* of the nursery list, and use it
742 // to allocate() from.
743 cap->r.rCurrentNursery->link = bd->link;
744 if (bd->link != NULL) {
745 bd->link->u.back = cap->r.rCurrentNursery;
748 dbl_link_onto(bd, &cap->r.rNursery->blocks);
749 cap->r.rCurrentAlloc = bd;
750 IF_DEBUG(sanity, checkNurserySanity(cap->r.rNursery));
757 /* ---------------------------------------------------------------------------
758 Allocate a fixed/pinned object.
760 We allocate small pinned objects into a single block, allocating a
761 new block when the current one overflows. The block is chained
762 onto the large_object_list of generation 0 step 0.
764 NOTE: The GC can't in general handle pinned objects. This
765 interface is only safe to use for ByteArrays, which have no
766 pointers and don't require scavenging. It works because the
767 block's descriptor has the BF_LARGE flag set, so the block is
768 treated as a large object and chained onto various lists, rather
769 than the individual objects being copied. However, when it comes
770 to scavenge the block, the GC will only scavenge the first object.
771 The reason is that the GC can't linearly scan a block of pinned
772 objects at the moment (doing so would require using the
773 mostly-copying techniques). But since we're restricting ourselves
774 to pinned ByteArrays, not scavenging is ok.
776 This function is called by newPinnedByteArray# which immediately
777 fills the allocated memory with a MutableByteArray#.
778 ------------------------------------------------------------------------- */
781 allocatePinned( lnat n )
784 bdescr *bd = pinned_object_block;
786 // If the request is for a large object, then allocate()
787 // will give us a pinned object anyway.
788 if (n >= LARGE_OBJECT_THRESHOLD/sizeof(W_)) {
790 Bdescr(p)->flags |= BF_PINNED;
796 TICK_ALLOC_HEAP_NOCTR(n);
799 // If we don't have a block of pinned objects yet, or the current
800 // one isn't large enough to hold the new object, allocate a new one.
801 if (bd == NULL || (bd->free + n) > (bd->start + BLOCK_SIZE_W)) {
802 pinned_object_block = bd = allocBlock();
803 dbl_link_onto(bd, &g0s0->large_objects);
804 g0s0->n_large_blocks++;
805 initBdescr(bd, g0s0);
806 bd->flags = BF_PINNED | BF_LARGE;
807 bd->free = bd->start;
817 /* -----------------------------------------------------------------------------
819 -------------------------------------------------------------------------- */
822 This is the write barrier for MUT_VARs, a.k.a. IORefs. A
823 MUT_VAR_CLEAN object is not on the mutable list; a MUT_VAR_DIRTY
824 is. When written to, a MUT_VAR_CLEAN turns into a MUT_VAR_DIRTY
825 and is put on the mutable list.
828 dirty_MUT_VAR(StgRegTable *reg, StgClosure *p)
830 Capability *cap = regTableToCapability(reg);
832 if (p->header.info == &stg_MUT_VAR_CLEAN_info) {
833 p->header.info = &stg_MUT_VAR_DIRTY_info;
834 bd = Bdescr((StgPtr)p);
835 if (bd->gen_no > 0) recordMutableCap(p,cap,bd->gen_no);
839 // Setting a TSO's link field with a write barrier.
840 // It is *not* necessary to call this function when
841 // * setting the link field to END_TSO_QUEUE
842 // * putting a TSO on the blackhole_queue
843 // * setting the link field of the currently running TSO, as it
844 // will already be dirty.
846 setTSOLink (Capability *cap, StgTSO *tso, StgTSO *target)
849 if (tso->dirty == 0 && (tso->flags & TSO_LINK_DIRTY) == 0) {
850 tso->flags |= TSO_LINK_DIRTY;
851 bd = Bdescr((StgPtr)tso);
852 if (bd->gen_no > 0) recordMutableCap((StgClosure*)tso,cap,bd->gen_no);
858 dirty_TSO (Capability *cap, StgTSO *tso)
861 if (tso->dirty == 0 && (tso->flags & TSO_LINK_DIRTY) == 0) {
862 bd = Bdescr((StgPtr)tso);
863 if (bd->gen_no > 0) recordMutableCap((StgClosure*)tso,cap,bd->gen_no);
869 This is the write barrier for MVARs. An MVAR_CLEAN objects is not
870 on the mutable list; a MVAR_DIRTY is. When written to, a
871 MVAR_CLEAN turns into a MVAR_DIRTY and is put on the mutable list.
872 The check for MVAR_CLEAN is inlined at the call site for speed,
873 this really does make a difference on concurrency-heavy benchmarks
874 such as Chaneneos and cheap-concurrency.
877 dirty_MVAR(StgRegTable *reg, StgClosure *p)
879 Capability *cap = regTableToCapability(reg);
881 bd = Bdescr((StgPtr)p);
882 if (bd->gen_no > 0) recordMutableCap(p,cap,bd->gen_no);
885 /* -----------------------------------------------------------------------------
887 * -------------------------------------------------------------------------- */
889 /* -----------------------------------------------------------------------------
892 * Approximate how much we've allocated: number of blocks in the
893 * nursery + blocks allocated via allocate() - unused nusery blocks.
894 * This leaves a little slop at the end of each block, and doesn't
895 * take into account large objects (ToDo).
896 * -------------------------------------------------------------------------- */
899 calcAllocated( void )
904 allocated = allocatedBytes();
905 allocated += countNurseryBlocks() * BLOCK_SIZE_W;
910 for (i = 0; i < n_nurseries; i++) {
912 for ( bd = capabilities[i].r.rCurrentNursery->link;
913 bd != NULL; bd = bd->link ) {
914 allocated -= BLOCK_SIZE_W;
916 cap = &capabilities[i];
917 if (cap->r.rCurrentNursery->free <
918 cap->r.rCurrentNursery->start + BLOCK_SIZE_W) {
919 allocated -= (cap->r.rCurrentNursery->start + BLOCK_SIZE_W)
920 - cap->r.rCurrentNursery->free;
924 bdescr *current_nursery = MainCapability.r.rCurrentNursery;
926 for ( bd = current_nursery->link; bd != NULL; bd = bd->link ) {
927 allocated -= BLOCK_SIZE_W;
929 if (current_nursery->free < current_nursery->start + BLOCK_SIZE_W) {
930 allocated -= (current_nursery->start + BLOCK_SIZE_W)
931 - current_nursery->free;
936 total_allocated += allocated;
940 /* Approximate the amount of live data in the heap. To be called just
941 * after garbage collection (see GarbageCollect()).
950 if (RtsFlags.GcFlags.generations == 1) {
951 return g0s0->n_large_blocks + g0s0->n_blocks;
954 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
955 for (s = 0; s < generations[g].n_steps; s++) {
956 /* approximate amount of live data (doesn't take into account slop
957 * at end of each block).
959 if (g == 0 && s == 0) {
962 stp = &generations[g].steps[s];
963 live += stp->n_large_blocks + stp->n_blocks;
970 countOccupied(bdescr *bd)
975 for (; bd != NULL; bd = bd->link) {
976 ASSERT(bd->free <= bd->start + bd->blocks * BLOCK_SIZE_W);
977 words += bd->free - bd->start;
982 // Return an accurate count of the live data in the heap, excluding
991 if (RtsFlags.GcFlags.generations == 1) {
992 return g0s0->n_words + countOccupied(g0s0->large_objects);
996 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
997 for (s = 0; s < generations[g].n_steps; s++) {
998 if (g == 0 && s == 0) continue;
999 stp = &generations[g].steps[s];
1000 live += stp->n_words + countOccupied(stp->large_objects);
1006 /* Approximate the number of blocks that will be needed at the next
1007 * garbage collection.
1009 * Assume: all data currently live will remain live. Steps that will
1010 * be collected next time will therefore need twice as many blocks
1011 * since all the data will be copied.
1020 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
1021 for (s = 0; s < generations[g].n_steps; s++) {
1022 if (g == 0 && s == 0) { continue; }
1023 stp = &generations[g].steps[s];
1025 // we need at least this much space
1026 needed += stp->n_blocks + stp->n_large_blocks;
1028 // any additional space needed to collect this gen next time?
1029 if (g == 0 || // always collect gen 0
1030 (generations[g].steps[0].n_blocks +
1031 generations[g].steps[0].n_large_blocks
1032 > generations[g].max_blocks)) {
1033 // we will collect this gen next time
1036 needed += stp->n_blocks / BITS_IN(W_);
1038 needed += stp->n_blocks / 100;
1041 continue; // no additional space needed for compaction
1043 needed += stp->n_blocks;
1051 /* ----------------------------------------------------------------------------
1054 Executable memory must be managed separately from non-executable
1055 memory. Most OSs these days require you to jump through hoops to
1056 dynamically allocate executable memory, due to various security
1059 Here we provide a small memory allocator for executable memory.
1060 Memory is managed with a page granularity; we allocate linearly
1061 in the page, and when the page is emptied (all objects on the page
1062 are free) we free the page again, not forgetting to make it
1065 TODO: The inability to handle objects bigger than BLOCK_SIZE_W means that
1066 the linker cannot use allocateExec for loading object code files
1067 on Windows. Once allocateExec can handle larger objects, the linker
1068 should be modified to use allocateExec instead of VirtualAlloc.
1069 ------------------------------------------------------------------------- */
1071 #if defined(linux_HOST_OS)
1073 // On Linux we need to use libffi for allocating executable memory,
1074 // because it knows how to work around the restrictions put in place
1077 void *allocateExec (nat bytes, void **exec_ret)
1081 ret = ffi_closure_alloc (sizeof(void *) + (size_t)bytes, (void**)&exec);
1083 if (ret == NULL) return ret;
1084 *ret = ret; // save the address of the writable mapping, for freeExec().
1085 *exec_ret = exec + 1;
1089 // freeExec gets passed the executable address, not the writable address.
1090 void freeExec (void *addr)
1093 writable = *((void**)addr - 1);
1095 ffi_closure_free (writable);
1101 void *allocateExec (nat bytes, void **exec_ret)
1108 // round up to words.
1109 n = (bytes + sizeof(W_) + 1) / sizeof(W_);
1111 if (n+1 > BLOCK_SIZE_W) {
1112 barf("allocateExec: can't handle large objects");
1115 if (exec_block == NULL ||
1116 exec_block->free + n + 1 > exec_block->start + BLOCK_SIZE_W) {
1118 lnat pagesize = getPageSize();
1119 bd = allocGroup(stg_max(1, pagesize / BLOCK_SIZE));
1120 debugTrace(DEBUG_gc, "allocate exec block %p", bd->start);
1122 bd->flags = BF_EXEC;
1123 bd->link = exec_block;
1124 if (exec_block != NULL) {
1125 exec_block->u.back = bd;
1128 setExecutable(bd->start, bd->blocks * BLOCK_SIZE, rtsTrue);
1131 *(exec_block->free) = n; // store the size of this chunk
1132 exec_block->gen_no += n; // gen_no stores the number of words allocated
1133 ret = exec_block->free + 1;
1134 exec_block->free += n + 1;
1141 void freeExec (void *addr)
1143 StgPtr p = (StgPtr)addr - 1;
1144 bdescr *bd = Bdescr((StgPtr)p);
1146 if ((bd->flags & BF_EXEC) == 0) {
1147 barf("freeExec: not executable");
1150 if (*(StgPtr)p == 0) {
1151 barf("freeExec: already free?");
1156 bd->gen_no -= *(StgPtr)p;
1159 if (bd->gen_no == 0) {
1160 // Free the block if it is empty, but not if it is the block at
1161 // the head of the queue.
1162 if (bd != exec_block) {
1163 debugTrace(DEBUG_gc, "free exec block %p", bd->start);
1164 dbl_link_remove(bd, &exec_block);
1165 setExecutable(bd->start, bd->blocks * BLOCK_SIZE, rtsFalse);
1168 bd->free = bd->start;
1175 #endif /* mingw32_HOST_OS */
1177 /* -----------------------------------------------------------------------------
1180 memInventory() checks for memory leaks by counting up all the
1181 blocks we know about and comparing that to the number of blocks
1182 allegedly floating around in the system.
1183 -------------------------------------------------------------------------- */
1187 // Useful for finding partially full blocks in gdb
1188 void findSlop(bdescr *bd);
1189 void findSlop(bdescr *bd)
1193 for (; bd != NULL; bd = bd->link) {
1194 slop = (bd->blocks * BLOCK_SIZE_W) - (bd->free - bd->start);
1195 if (slop > (1024/sizeof(W_))) {
1196 debugBelch("block at %p (bdescr %p) has %ldKB slop\n",
1197 bd->start, bd, slop / (1024/sizeof(W_)));
1203 countBlocks(bdescr *bd)
1206 for (n=0; bd != NULL; bd=bd->link) {
1212 // (*1) Just like countBlocks, except that we adjust the count for a
1213 // megablock group so that it doesn't include the extra few blocks
1214 // that would be taken up by block descriptors in the second and
1215 // subsequent megablock. This is so we can tally the count with the
1216 // number of blocks allocated in the system, for memInventory().
1218 countAllocdBlocks(bdescr *bd)
1221 for (n=0; bd != NULL; bd=bd->link) {
1223 // hack for megablock groups: see (*1) above
1224 if (bd->blocks > BLOCKS_PER_MBLOCK) {
1225 n -= (MBLOCK_SIZE / BLOCK_SIZE - BLOCKS_PER_MBLOCK)
1226 * (bd->blocks/(MBLOCK_SIZE/BLOCK_SIZE));
1233 stepBlocks (step *stp)
1235 ASSERT(countBlocks(stp->blocks) == stp->n_blocks);
1236 ASSERT(countBlocks(stp->large_objects) == stp->n_large_blocks);
1237 return stp->n_blocks + stp->n_old_blocks +
1238 countAllocdBlocks(stp->large_objects);
1241 // If memInventory() calculates that we have a memory leak, this
1242 // function will try to find the block(s) that are leaking by marking
1243 // all the ones that we know about, and search through memory to find
1244 // blocks that are not marked. In the debugger this can help to give
1245 // us a clue about what kind of block leaked. In the future we might
1246 // annotate blocks with their allocation site to give more helpful
1249 findMemoryLeak (void)
1252 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
1253 for (i = 0; i < n_capabilities; i++) {
1254 markBlocks(capabilities[i].mut_lists[g]);
1256 markBlocks(generations[g].mut_list);
1257 for (s = 0; s < generations[g].n_steps; s++) {
1258 markBlocks(generations[g].steps[s].blocks);
1259 markBlocks(generations[g].steps[s].large_objects);
1263 for (i = 0; i < n_nurseries; i++) {
1264 markBlocks(nurseries[i].blocks);
1265 markBlocks(nurseries[i].large_objects);
1270 // if (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_RETAINER) {
1271 // markRetainerBlocks();
1275 // count the blocks allocated by the arena allocator
1277 // markArenaBlocks();
1279 // count the blocks containing executable memory
1280 markBlocks(exec_block);
1282 reportUnmarkedBlocks();
1287 memInventory (rtsBool show)
1291 lnat gen_blocks[RtsFlags.GcFlags.generations];
1292 lnat nursery_blocks, retainer_blocks,
1293 arena_blocks, exec_blocks;
1294 lnat live_blocks = 0, free_blocks = 0;
1297 // count the blocks we current have
1299 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
1301 for (i = 0; i < n_capabilities; i++) {
1302 gen_blocks[g] += countBlocks(capabilities[i].mut_lists[g]);
1304 gen_blocks[g] += countAllocdBlocks(generations[g].mut_list);
1305 for (s = 0; s < generations[g].n_steps; s++) {
1306 stp = &generations[g].steps[s];
1307 gen_blocks[g] += stepBlocks(stp);
1312 for (i = 0; i < n_nurseries; i++) {
1313 nursery_blocks += stepBlocks(&nurseries[i]);
1316 retainer_blocks = 0;
1318 if (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_RETAINER) {
1319 retainer_blocks = retainerStackBlocks();
1323 // count the blocks allocated by the arena allocator
1324 arena_blocks = arenaBlocks();
1326 // count the blocks containing executable memory
1327 exec_blocks = countAllocdBlocks(exec_block);
1329 /* count the blocks on the free list */
1330 free_blocks = countFreeList();
1333 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
1334 live_blocks += gen_blocks[g];
1336 live_blocks += nursery_blocks +
1337 + retainer_blocks + arena_blocks + exec_blocks;
1339 #define MB(n) (((n) * BLOCK_SIZE_W) / ((1024*1024)/sizeof(W_)))
1341 leak = live_blocks + free_blocks != mblocks_allocated * BLOCKS_PER_MBLOCK;
1346 debugBelch("Memory leak detected:\n");
1348 debugBelch("Memory inventory:\n");
1350 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
1351 debugBelch(" gen %d blocks : %5lu blocks (%lu MB)\n", g,
1352 gen_blocks[g], MB(gen_blocks[g]));
1354 debugBelch(" nursery : %5lu blocks (%lu MB)\n",
1355 nursery_blocks, MB(nursery_blocks));
1356 debugBelch(" retainer : %5lu blocks (%lu MB)\n",
1357 retainer_blocks, MB(retainer_blocks));
1358 debugBelch(" arena blocks : %5lu blocks (%lu MB)\n",
1359 arena_blocks, MB(arena_blocks));
1360 debugBelch(" exec : %5lu blocks (%lu MB)\n",
1361 exec_blocks, MB(exec_blocks));
1362 debugBelch(" free : %5lu blocks (%lu MB)\n",
1363 free_blocks, MB(free_blocks));
1364 debugBelch(" total : %5lu blocks (%lu MB)\n",
1365 live_blocks + free_blocks, MB(live_blocks+free_blocks));
1367 debugBelch("\n in system : %5lu blocks (%lu MB)\n",
1368 mblocks_allocated * BLOCKS_PER_MBLOCK, mblocks_allocated);
1376 ASSERT(n_alloc_blocks == live_blocks);
1381 /* Full heap sanity check. */
1387 if (RtsFlags.GcFlags.generations == 1) {
1388 checkHeap(g0s0->blocks);
1389 checkLargeObjects(g0s0->large_objects);
1392 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
1393 for (s = 0; s < generations[g].n_steps; s++) {
1394 if (g == 0 && s == 0) { continue; }
1395 ASSERT(countBlocks(generations[g].steps[s].blocks)
1396 == generations[g].steps[s].n_blocks);
1397 ASSERT(countBlocks(generations[g].steps[s].large_objects)
1398 == generations[g].steps[s].n_large_blocks);
1399 checkHeap(generations[g].steps[s].blocks);
1400 checkLargeObjects(generations[g].steps[s].large_objects);
1404 for (s = 0; s < n_nurseries; s++) {
1405 ASSERT(countBlocks(nurseries[s].blocks)
1406 == nurseries[s].n_blocks);
1407 ASSERT(countBlocks(nurseries[s].large_objects)
1408 == nurseries[s].n_large_blocks);
1411 checkFreeListSanity();
1414 #if defined(THREADED_RTS)
1415 // check the stacks too in threaded mode, because we don't do a
1416 // full heap sanity check in this case (see checkHeap())
1417 checkMutableLists(rtsTrue);
1419 checkMutableLists(rtsFalse);
1423 /* Nursery sanity check */
1425 checkNurserySanity( step *stp )
1431 for (bd = stp->blocks; bd != NULL; bd = bd->link) {
1432 ASSERT(bd->u.back == prev);
1434 blocks += bd->blocks;
1436 ASSERT(blocks == stp->n_blocks);
1439 // handy function for use in gdb, because Bdescr() is inlined.
1440 extern bdescr *_bdescr( StgPtr p );