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"
25 #include "OSThreads.h"
26 #include "Capability.h"
29 #include "RetainerProfile.h" // for counting memory blocks (memInventory)
39 * All these globals require sm_mutex to access in THREADED_RTS mode.
41 StgClosure *caf_list = NULL;
42 StgClosure *revertible_caf_list = NULL;
45 bdescr *pinned_object_block; /* allocate pinned objects into this block */
46 nat alloc_blocks; /* number of allocate()d blocks since GC */
47 nat alloc_blocks_lim; /* approximate limit on alloc_blocks */
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.
69 * This mutex is used by atomicModifyMutVar# only
71 Mutex atomic_modify_mutvar_mutex;
78 static void *stgAllocForGMP (size_t size_in_bytes);
79 static void *stgReallocForGMP (void *ptr, size_t old_size, size_t new_size);
80 static void stgDeallocForGMP (void *ptr, size_t size);
83 initStep (step *stp, int g, int s)
86 stp->abs_no = RtsFlags.GcFlags.steps * g + s;
90 stp->live_estimate = 0;
91 stp->old_blocks = NULL;
92 stp->n_old_blocks = 0;
93 stp->gen = &generations[g];
95 stp->large_objects = NULL;
96 stp->n_large_blocks = 0;
97 stp->scavenged_large_objects = NULL;
98 stp->n_scavenged_large_blocks = 0;
103 initSpinLock(&stp->sync_todo);
104 initSpinLock(&stp->sync_large_objects);
106 stp->threads = END_TSO_QUEUE;
107 stp->old_threads = END_TSO_QUEUE;
116 if (generations != NULL) {
117 // multi-init protection
123 /* Sanity check to make sure the LOOKS_LIKE_ macros appear to be
124 * doing something reasonable.
126 /* We use the NOT_NULL variant or gcc warns that the test is always true */
127 ASSERT(LOOKS_LIKE_INFO_PTR_NOT_NULL((StgWord)&stg_BLACKHOLE_info));
128 ASSERT(LOOKS_LIKE_CLOSURE_PTR(&stg_dummy_ret_closure));
129 ASSERT(!HEAP_ALLOCED(&stg_dummy_ret_closure));
131 if (RtsFlags.GcFlags.maxHeapSize != 0 &&
132 RtsFlags.GcFlags.heapSizeSuggestion >
133 RtsFlags.GcFlags.maxHeapSize) {
134 RtsFlags.GcFlags.maxHeapSize = RtsFlags.GcFlags.heapSizeSuggestion;
137 if (RtsFlags.GcFlags.maxHeapSize != 0 &&
138 RtsFlags.GcFlags.minAllocAreaSize >
139 RtsFlags.GcFlags.maxHeapSize) {
140 errorBelch("maximum heap size (-M) is smaller than minimum alloc area size (-A)");
141 RtsFlags.GcFlags.minAllocAreaSize = RtsFlags.GcFlags.maxHeapSize;
144 initBlockAllocator();
146 #if defined(THREADED_RTS)
147 initMutex(&sm_mutex);
148 initMutex(&atomic_modify_mutvar_mutex);
153 /* allocate generation info array */
154 generations = (generation *)stgMallocBytes(RtsFlags.GcFlags.generations
155 * sizeof(struct generation_),
156 "initStorage: gens");
158 /* allocate all the steps into an array. It is important that we do
159 it this way, because we need the invariant that two step pointers
160 can be directly compared to see which is the oldest.
161 Remember that the last generation has only one step. */
162 total_steps = 1 + (RtsFlags.GcFlags.generations - 1) * RtsFlags.GcFlags.steps;
163 all_steps = stgMallocBytes(total_steps * sizeof(struct step_),
164 "initStorage: steps");
166 /* Initialise all generations */
167 for(g = 0; g < RtsFlags.GcFlags.generations; g++) {
168 gen = &generations[g];
170 gen->mut_list = allocBlock();
171 gen->collections = 0;
172 gen->par_collections = 0;
173 gen->failed_promotions = 0;
177 /* A couple of convenience pointers */
178 g0 = &generations[0];
179 oldest_gen = &generations[RtsFlags.GcFlags.generations-1];
181 /* Allocate step structures in each generation */
182 if (RtsFlags.GcFlags.generations > 1) {
183 /* Only for multiple-generations */
185 /* Oldest generation: one step */
186 oldest_gen->n_steps = 1;
187 oldest_gen->steps = all_steps + (RtsFlags.GcFlags.generations - 1)
188 * RtsFlags.GcFlags.steps;
190 /* set up all except the oldest generation with 2 steps */
191 for(g = 0; g < RtsFlags.GcFlags.generations-1; g++) {
192 generations[g].n_steps = RtsFlags.GcFlags.steps;
193 generations[g].steps = all_steps + g * RtsFlags.GcFlags.steps;
197 /* single generation, i.e. a two-space collector */
199 g0->steps = all_steps;
203 n_nurseries = n_capabilities;
207 nurseries = stgMallocBytes (n_nurseries * sizeof(struct step_),
208 "initStorage: nurseries");
210 /* Initialise all steps */
211 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
212 for (s = 0; s < generations[g].n_steps; s++) {
213 initStep(&generations[g].steps[s], g, s);
217 for (s = 0; s < n_nurseries; s++) {
218 initStep(&nurseries[s], 0, s);
221 /* Set up the destination pointers in each younger gen. step */
222 for (g = 0; g < RtsFlags.GcFlags.generations-1; g++) {
223 for (s = 0; s < generations[g].n_steps-1; s++) {
224 generations[g].steps[s].to = &generations[g].steps[s+1];
226 generations[g].steps[s].to = &generations[g+1].steps[0];
228 oldest_gen->steps[0].to = &oldest_gen->steps[0];
230 for (s = 0; s < n_nurseries; s++) {
231 nurseries[s].to = generations[0].steps[0].to;
234 /* The oldest generation has one step. */
235 if (RtsFlags.GcFlags.compact || RtsFlags.GcFlags.sweep) {
236 if (RtsFlags.GcFlags.generations == 1) {
237 errorBelch("WARNING: compact/sweep is incompatible with -G1; disabled");
239 oldest_gen->steps[0].mark = 1;
240 if (RtsFlags.GcFlags.compact)
241 oldest_gen->steps[0].compact = 1;
245 generations[0].max_blocks = 0;
246 g0s0 = &generations[0].steps[0];
248 /* The allocation area. Policy: keep the allocation area
249 * small to begin with, even if we have a large suggested heap
250 * size. Reason: we're going to do a major collection first, and we
251 * don't want it to be a big one. This vague idea is borne out by
252 * rigorous experimental evidence.
256 weak_ptr_list = NULL;
258 revertible_caf_list = NULL;
260 /* initialise the allocate() interface */
262 alloc_blocks_lim = RtsFlags.GcFlags.minAllocAreaSize;
264 /* Tell GNU multi-precision pkg about our custom alloc functions */
265 mp_set_memory_functions(stgAllocForGMP, stgReallocForGMP, stgDeallocForGMP);
268 initSpinLock(&gc_alloc_block_sync);
269 initSpinLock(&recordMutableGen_sync);
273 IF_DEBUG(gc, statDescribeGens());
281 stat_exit(calcAllocated());
287 stgFree(g0s0); // frees all the steps
288 stgFree(generations);
290 #if defined(THREADED_RTS)
291 closeMutex(&sm_mutex);
292 closeMutex(&atomic_modify_mutvar_mutex);
297 /* -----------------------------------------------------------------------------
300 The entry code for every CAF does the following:
302 - builds a CAF_BLACKHOLE in the heap
303 - pushes an update frame pointing to the CAF_BLACKHOLE
304 - invokes UPD_CAF(), which:
305 - calls newCaf, below
306 - updates the CAF with a static indirection to the CAF_BLACKHOLE
308 Why do we build a BLACKHOLE in the heap rather than just updating
309 the thunk directly? It's so that we only need one kind of update
310 frame - otherwise we'd need a static version of the update frame too.
312 newCaf() does the following:
314 - it puts the CAF on the oldest generation's mut-once list.
315 This is so that we can treat the CAF as a root when collecting
318 For GHCI, we have additional requirements when dealing with CAFs:
320 - we must *retain* all dynamically-loaded CAFs ever entered,
321 just in case we need them again.
322 - we must be able to *revert* CAFs that have been evaluated, to
323 their pre-evaluated form.
325 To do this, we use an additional CAF list. When newCaf() is
326 called on a dynamically-loaded CAF, we add it to the CAF list
327 instead of the old-generation mutable list, and save away its
328 old info pointer (in caf->saved_info) for later reversion.
330 To revert all the CAFs, we traverse the CAF list and reset the
331 info pointer to caf->saved_info, then throw away the CAF list.
332 (see GC.c:revertCAFs()).
336 -------------------------------------------------------------------------- */
339 newCAF(StgClosure* caf)
346 // If we are in GHCi _and_ we are using dynamic libraries,
347 // then we can't redirect newCAF calls to newDynCAF (see below),
348 // so we make newCAF behave almost like newDynCAF.
349 // The dynamic libraries might be used by both the interpreted
350 // program and GHCi itself, so they must not be reverted.
351 // This also means that in GHCi with dynamic libraries, CAFs are not
352 // garbage collected. If this turns out to be a problem, we could
353 // do another hack here and do an address range test on caf to figure
354 // out whether it is from a dynamic library.
355 ((StgIndStatic *)caf)->saved_info = (StgInfoTable *)caf->header.info;
356 ((StgIndStatic *)caf)->static_link = caf_list;
361 /* Put this CAF on the mutable list for the old generation.
362 * This is a HACK - the IND_STATIC closure doesn't really have
363 * a mut_link field, but we pretend it has - in fact we re-use
364 * the STATIC_LINK field for the time being, because when we
365 * come to do a major GC we won't need the mut_link field
366 * any more and can use it as a STATIC_LINK.
368 ((StgIndStatic *)caf)->saved_info = NULL;
369 recordMutableGen(caf, oldest_gen);
375 // An alternate version of newCaf which is used for dynamically loaded
376 // object code in GHCi. In this case we want to retain *all* CAFs in
377 // the object code, because they might be demanded at any time from an
378 // expression evaluated on the command line.
379 // Also, GHCi might want to revert CAFs, so we add these to the
380 // revertible_caf_list.
382 // The linker hackily arranges that references to newCaf from dynamic
383 // code end up pointing to newDynCAF.
385 newDynCAF(StgClosure *caf)
389 ((StgIndStatic *)caf)->saved_info = (StgInfoTable *)caf->header.info;
390 ((StgIndStatic *)caf)->static_link = revertible_caf_list;
391 revertible_caf_list = caf;
396 /* -----------------------------------------------------------------------------
398 -------------------------------------------------------------------------- */
401 allocNursery (step *stp, bdescr *tail, nat blocks)
406 // Allocate a nursery: we allocate fresh blocks one at a time and
407 // cons them on to the front of the list, not forgetting to update
408 // the back pointer on the tail of the list to point to the new block.
409 for (i=0; i < blocks; i++) {
412 processNursery() in LdvProfile.c assumes that every block group in
413 the nursery contains only a single block. So, if a block group is
414 given multiple blocks, change processNursery() accordingly.
418 // double-link the nursery: we might need to insert blocks
425 bd->free = bd->start;
433 assignNurseriesToCapabilities (void)
438 for (i = 0; i < n_nurseries; i++) {
439 capabilities[i].r.rNursery = &nurseries[i];
440 capabilities[i].r.rCurrentNursery = nurseries[i].blocks;
441 capabilities[i].r.rCurrentAlloc = NULL;
443 #else /* THREADED_RTS */
444 MainCapability.r.rNursery = &nurseries[0];
445 MainCapability.r.rCurrentNursery = nurseries[0].blocks;
446 MainCapability.r.rCurrentAlloc = NULL;
451 allocNurseries( void )
455 for (i = 0; i < n_nurseries; i++) {
456 nurseries[i].blocks =
457 allocNursery(&nurseries[i], NULL,
458 RtsFlags.GcFlags.minAllocAreaSize);
459 nurseries[i].n_blocks = RtsFlags.GcFlags.minAllocAreaSize;
460 nurseries[i].old_blocks = NULL;
461 nurseries[i].n_old_blocks = 0;
463 assignNurseriesToCapabilities();
467 resetNurseries( void )
473 for (i = 0; i < n_nurseries; i++) {
475 for (bd = stp->blocks; bd; bd = bd->link) {
476 bd->free = bd->start;
477 ASSERT(bd->gen_no == 0);
478 ASSERT(bd->step == stp);
479 IF_DEBUG(sanity,memset(bd->start, 0xaa, BLOCK_SIZE));
482 assignNurseriesToCapabilities();
486 countNurseryBlocks (void)
491 for (i = 0; i < n_nurseries; i++) {
492 blocks += nurseries[i].n_blocks;
498 resizeNursery ( step *stp, nat blocks )
503 nursery_blocks = stp->n_blocks;
504 if (nursery_blocks == blocks) return;
506 if (nursery_blocks < blocks) {
507 debugTrace(DEBUG_gc, "increasing size of nursery to %d blocks",
509 stp->blocks = allocNursery(stp, stp->blocks, blocks-nursery_blocks);
514 debugTrace(DEBUG_gc, "decreasing size of nursery to %d blocks",
518 while (nursery_blocks > blocks) {
520 next_bd->u.back = NULL;
521 nursery_blocks -= bd->blocks; // might be a large block
526 // might have gone just under, by freeing a large block, so make
527 // up the difference.
528 if (nursery_blocks < blocks) {
529 stp->blocks = allocNursery(stp, stp->blocks, blocks-nursery_blocks);
533 stp->n_blocks = blocks;
534 ASSERT(countBlocks(stp->blocks) == stp->n_blocks);
538 // Resize each of the nurseries to the specified size.
541 resizeNurseriesFixed (nat blocks)
544 for (i = 0; i < n_nurseries; i++) {
545 resizeNursery(&nurseries[i], blocks);
550 // Resize the nurseries to the total specified size.
553 resizeNurseries (nat blocks)
555 // If there are multiple nurseries, then we just divide the number
556 // of available blocks between them.
557 resizeNurseriesFixed(blocks / n_nurseries);
561 /* -----------------------------------------------------------------------------
562 move_TSO is called to update the TSO structure after it has been
563 moved from one place to another.
564 -------------------------------------------------------------------------- */
567 move_TSO (StgTSO *src, StgTSO *dest)
571 // relocate the stack pointer...
572 diff = (StgPtr)dest - (StgPtr)src; // In *words*
573 dest->sp = (StgPtr)dest->sp + diff;
576 /* -----------------------------------------------------------------------------
577 The allocate() interface
579 allocateInGen() function allocates memory directly into a specific
580 generation. It always succeeds, and returns a chunk of memory n
581 words long. n can be larger than the size of a block if necessary,
582 in which case a contiguous block group will be allocated.
584 allocate(n) is equivalent to allocateInGen(g0).
585 -------------------------------------------------------------------------- */
588 allocateInGen (generation *g, lnat n)
596 TICK_ALLOC_HEAP_NOCTR(n);
601 if (n >= LARGE_OBJECT_THRESHOLD/sizeof(W_))
603 lnat req_blocks = (lnat)BLOCK_ROUND_UP(n*sizeof(W_)) / BLOCK_SIZE;
605 // Attempting to allocate an object larger than maxHeapSize
606 // should definitely be disallowed. (bug #1791)
607 if (RtsFlags.GcFlags.maxHeapSize > 0 &&
608 req_blocks >= RtsFlags.GcFlags.maxHeapSize) {
612 bd = allocGroup(req_blocks);
613 dbl_link_onto(bd, &stp->large_objects);
614 stp->n_large_blocks += bd->blocks; // might be larger than req_blocks
617 bd->flags = BF_LARGE;
618 bd->free = bd->start + n;
623 // small allocation (<LARGE_OBJECT_THRESHOLD) */
625 if (bd == NULL || bd->free + n > bd->start + BLOCK_SIZE_W) {
630 bd->link = stp->blocks;
647 return allocateInGen(g0,n);
651 allocatedBytes( void )
655 allocated = alloc_blocks * BLOCK_SIZE_W;
656 if (pinned_object_block != NULL) {
657 allocated -= (pinned_object_block->start + BLOCK_SIZE_W) -
658 pinned_object_block->free;
664 // split N blocks off the front of the given bdescr, returning the
665 // new block group. We treat the remainder as if it
666 // had been freshly allocated in generation 0.
668 splitLargeBlock (bdescr *bd, nat blocks)
672 // subtract the original number of blocks from the counter first
673 bd->step->n_large_blocks -= bd->blocks;
675 new_bd = splitBlockGroup (bd, blocks);
677 dbl_link_onto(new_bd, &g0s0->large_objects);
678 g0s0->n_large_blocks += new_bd->blocks;
679 new_bd->gen_no = g0s0->no;
681 new_bd->flags = BF_LARGE;
682 new_bd->free = bd->free;
683 ASSERT(new_bd->free <= new_bd->start + new_bd->blocks * BLOCK_SIZE_W);
685 // add the new number of blocks to the counter. Due to the gaps
686 // for block descriptor, new_bd->blocks + bd->blocks might not be
687 // equal to the original bd->blocks, which is why we do it this way.
688 bd->step->n_large_blocks += bd->blocks;
693 /* -----------------------------------------------------------------------------
696 This allocates memory in the current thread - it is intended for
697 use primarily from STG-land where we have a Capability. It is
698 better than allocate() because it doesn't require taking the
699 sm_mutex lock in the common case.
701 Memory is allocated directly from the nursery if possible (but not
702 from the current nursery block, so as not to interfere with
704 -------------------------------------------------------------------------- */
707 allocateLocal (Capability *cap, lnat n)
712 if (n >= LARGE_OBJECT_THRESHOLD/sizeof(W_)) {
713 return allocateInGen(g0,n);
716 /* small allocation (<LARGE_OBJECT_THRESHOLD) */
718 TICK_ALLOC_HEAP_NOCTR(n);
721 bd = cap->r.rCurrentAlloc;
722 if (bd == NULL || bd->free + n > bd->start + BLOCK_SIZE_W) {
724 // The CurrentAlloc block is full, we need to find another
725 // one. First, we try taking the next block from the
727 bd = cap->r.rCurrentNursery->link;
729 if (bd == NULL || bd->free + n > bd->start + BLOCK_SIZE_W) {
730 // The nursery is empty, or the next block is already
731 // full: allocate a fresh block (we can't fail here).
734 cap->r.rNursery->n_blocks++;
737 bd->step = cap->r.rNursery;
739 // NO: alloc_blocks++;
740 // calcAllocated() uses the size of the nursery, and we've
741 // already bumpted nursery->n_blocks above.
743 // we have a block in the nursery: take it and put
744 // it at the *front* of the nursery list, and use it
745 // to allocate() from.
746 cap->r.rCurrentNursery->link = bd->link;
747 if (bd->link != NULL) {
748 bd->link->u.back = cap->r.rCurrentNursery;
751 dbl_link_onto(bd, &cap->r.rNursery->blocks);
752 cap->r.rCurrentAlloc = bd;
753 IF_DEBUG(sanity, checkNurserySanity(cap->r.rNursery));
760 /* ---------------------------------------------------------------------------
761 Allocate a fixed/pinned object.
763 We allocate small pinned objects into a single block, allocating a
764 new block when the current one overflows. The block is chained
765 onto the large_object_list of generation 0 step 0.
767 NOTE: The GC can't in general handle pinned objects. This
768 interface is only safe to use for ByteArrays, which have no
769 pointers and don't require scavenging. It works because the
770 block's descriptor has the BF_LARGE flag set, so the block is
771 treated as a large object and chained onto various lists, rather
772 than the individual objects being copied. However, when it comes
773 to scavenge the block, the GC will only scavenge the first object.
774 The reason is that the GC can't linearly scan a block of pinned
775 objects at the moment (doing so would require using the
776 mostly-copying techniques). But since we're restricting ourselves
777 to pinned ByteArrays, not scavenging is ok.
779 This function is called by newPinnedByteArray# which immediately
780 fills the allocated memory with a MutableByteArray#.
781 ------------------------------------------------------------------------- */
784 allocatePinned( lnat n )
787 bdescr *bd = pinned_object_block;
789 // If the request is for a large object, then allocate()
790 // will give us a pinned object anyway.
791 if (n >= LARGE_OBJECT_THRESHOLD/sizeof(W_)) {
797 TICK_ALLOC_HEAP_NOCTR(n);
800 // we always return 8-byte aligned memory. bd->free must be
801 // 8-byte aligned to begin with, so we just round up n to
802 // the nearest multiple of 8 bytes.
803 if (sizeof(StgWord) == 4) {
807 // If we don't have a block of pinned objects yet, or the current
808 // one isn't large enough to hold the new object, allocate a new one.
809 if (bd == NULL || (bd->free + n) > (bd->start + BLOCK_SIZE_W)) {
810 pinned_object_block = bd = allocBlock();
811 dbl_link_onto(bd, &g0s0->large_objects);
812 g0s0->n_large_blocks++;
815 bd->flags = BF_PINNED | BF_LARGE;
816 bd->free = bd->start;
826 /* -----------------------------------------------------------------------------
828 -------------------------------------------------------------------------- */
831 This is the write barrier for MUT_VARs, a.k.a. IORefs. A
832 MUT_VAR_CLEAN object is not on the mutable list; a MUT_VAR_DIRTY
833 is. When written to, a MUT_VAR_CLEAN turns into a MUT_VAR_DIRTY
834 and is put on the mutable list.
837 dirty_MUT_VAR(StgRegTable *reg, StgClosure *p)
839 Capability *cap = regTableToCapability(reg);
841 if (p->header.info == &stg_MUT_VAR_CLEAN_info) {
842 p->header.info = &stg_MUT_VAR_DIRTY_info;
843 bd = Bdescr((StgPtr)p);
844 if (bd->gen_no > 0) recordMutableCap(p,cap,bd->gen_no);
848 // Setting a TSO's link field with a write barrier.
849 // It is *not* necessary to call this function when
850 // * setting the link field to END_TSO_QUEUE
851 // * putting a TSO on the blackhole_queue
852 // * setting the link field of the currently running TSO, as it
853 // will already be dirty.
855 setTSOLink (Capability *cap, StgTSO *tso, StgTSO *target)
858 if ((tso->flags & (TSO_DIRTY|TSO_LINK_DIRTY)) == 0) {
859 tso->flags |= TSO_LINK_DIRTY;
860 bd = Bdescr((StgPtr)tso);
861 if (bd->gen_no > 0) recordMutableCap((StgClosure*)tso,cap,bd->gen_no);
867 dirty_TSO (Capability *cap, StgTSO *tso)
870 if ((tso->flags & (TSO_DIRTY|TSO_LINK_DIRTY)) == 0) {
871 bd = Bdescr((StgPtr)tso);
872 if (bd->gen_no > 0) recordMutableCap((StgClosure*)tso,cap,bd->gen_no);
874 tso->flags |= TSO_DIRTY;
878 This is the write barrier for MVARs. An MVAR_CLEAN objects is not
879 on the mutable list; a MVAR_DIRTY is. When written to, a
880 MVAR_CLEAN turns into a MVAR_DIRTY and is put on the mutable list.
881 The check for MVAR_CLEAN is inlined at the call site for speed,
882 this really does make a difference on concurrency-heavy benchmarks
883 such as Chaneneos and cheap-concurrency.
886 dirty_MVAR(StgRegTable *reg, StgClosure *p)
888 Capability *cap = regTableToCapability(reg);
890 bd = Bdescr((StgPtr)p);
891 if (bd->gen_no > 0) recordMutableCap(p,cap,bd->gen_no);
894 /* -----------------------------------------------------------------------------
895 Allocation functions for GMP.
897 These all use the allocate() interface - we can't have any garbage
898 collection going on during a gmp operation, so we use allocate()
899 which always succeeds. The gmp operations which might need to
900 allocate will ask the storage manager (via doYouWantToGC()) whether
901 a garbage collection is required, in case we get into a loop doing
902 only allocate() style allocation.
903 -------------------------------------------------------------------------- */
906 stgAllocForGMP (size_t size_in_bytes)
909 nat data_size_in_words, total_size_in_words;
911 /* round up to a whole number of words */
912 data_size_in_words = (size_in_bytes + sizeof(W_) + 1) / sizeof(W_);
913 total_size_in_words = sizeofW(StgArrWords) + data_size_in_words;
915 /* allocate and fill it in. */
916 #if defined(THREADED_RTS)
917 arr = (StgArrWords *)allocateLocal(myTask()->cap, total_size_in_words);
919 arr = (StgArrWords *)allocateLocal(&MainCapability, total_size_in_words);
921 SET_ARR_HDR(arr, &stg_ARR_WORDS_info, CCCS, data_size_in_words);
923 /* and return a ptr to the goods inside the array */
928 stgReallocForGMP (void *ptr, size_t old_size, size_t new_size)
931 void *new_stuff_ptr = stgAllocForGMP(new_size);
933 char *p = (char *) ptr;
934 char *q = (char *) new_stuff_ptr;
936 min_size = old_size < new_size ? old_size : new_size;
937 for (; i < min_size; i++, p++, q++) {
941 return(new_stuff_ptr);
945 stgDeallocForGMP (void *ptr STG_UNUSED,
946 size_t size STG_UNUSED)
948 /* easy for us: the garbage collector does the dealloc'n */
951 /* -----------------------------------------------------------------------------
953 * -------------------------------------------------------------------------- */
955 /* -----------------------------------------------------------------------------
958 * Approximate how much we've allocated: number of blocks in the
959 * nursery + blocks allocated via allocate() - unused nusery blocks.
960 * This leaves a little slop at the end of each block, and doesn't
961 * take into account large objects (ToDo).
962 * -------------------------------------------------------------------------- */
965 calcAllocated( void )
970 allocated = allocatedBytes();
971 allocated += countNurseryBlocks() * BLOCK_SIZE_W;
976 for (i = 0; i < n_nurseries; i++) {
978 for ( bd = capabilities[i].r.rCurrentNursery->link;
979 bd != NULL; bd = bd->link ) {
980 allocated -= BLOCK_SIZE_W;
982 cap = &capabilities[i];
983 if (cap->r.rCurrentNursery->free <
984 cap->r.rCurrentNursery->start + BLOCK_SIZE_W) {
985 allocated -= (cap->r.rCurrentNursery->start + BLOCK_SIZE_W)
986 - cap->r.rCurrentNursery->free;
990 bdescr *current_nursery = MainCapability.r.rCurrentNursery;
992 for ( bd = current_nursery->link; bd != NULL; bd = bd->link ) {
993 allocated -= BLOCK_SIZE_W;
995 if (current_nursery->free < current_nursery->start + BLOCK_SIZE_W) {
996 allocated -= (current_nursery->start + BLOCK_SIZE_W)
997 - current_nursery->free;
1002 total_allocated += allocated;
1006 /* Approximate the amount of live data in the heap. To be called just
1007 * after garbage collection (see GarbageCollect()).
1010 calcLiveBlocks(void)
1016 if (RtsFlags.GcFlags.generations == 1) {
1017 return g0s0->n_large_blocks + g0s0->n_blocks;
1020 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
1021 for (s = 0; s < generations[g].n_steps; s++) {
1022 /* approximate amount of live data (doesn't take into account slop
1023 * at end of each block).
1025 if (g == 0 && s == 0) {
1028 stp = &generations[g].steps[s];
1029 live += stp->n_large_blocks + stp->n_blocks;
1036 countOccupied(bdescr *bd)
1041 for (; bd != NULL; bd = bd->link) {
1042 ASSERT(bd->free <= bd->start + bd->blocks * BLOCK_SIZE_W);
1043 words += bd->free - bd->start;
1048 // Return an accurate count of the live data in the heap, excluding
1057 if (RtsFlags.GcFlags.generations == 1) {
1058 return g0s0->n_words + countOccupied(g0s0->large_objects);
1062 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
1063 for (s = 0; s < generations[g].n_steps; s++) {
1064 if (g == 0 && s == 0) continue;
1065 stp = &generations[g].steps[s];
1066 live += stp->n_words + countOccupied(stp->large_objects);
1072 /* Approximate the number of blocks that will be needed at the next
1073 * garbage collection.
1075 * Assume: all data currently live will remain live. Steps that will
1076 * be collected next time will therefore need twice as many blocks
1077 * since all the data will be copied.
1086 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
1087 for (s = 0; s < generations[g].n_steps; s++) {
1088 if (g == 0 && s == 0) { continue; }
1089 stp = &generations[g].steps[s];
1091 // we need at least this much space
1092 needed += stp->n_blocks + stp->n_large_blocks;
1094 // any additional space needed to collect this gen next time?
1095 if (g == 0 || // always collect gen 0
1096 (generations[g].steps[0].n_blocks +
1097 generations[g].steps[0].n_large_blocks
1098 > generations[g].max_blocks)) {
1099 // we will collect this gen next time
1102 needed += stp->n_blocks / BITS_IN(W_);
1104 needed += stp->n_blocks / 100;
1107 continue; // no additional space needed for compaction
1109 needed += stp->n_blocks;
1117 /* ----------------------------------------------------------------------------
1120 Executable memory must be managed separately from non-executable
1121 memory. Most OSs these days require you to jump through hoops to
1122 dynamically allocate executable memory, due to various security
1125 Here we provide a small memory allocator for executable memory.
1126 Memory is managed with a page granularity; we allocate linearly
1127 in the page, and when the page is emptied (all objects on the page
1128 are free) we free the page again, not forgetting to make it
1131 TODO: The inability to handle objects bigger than BLOCK_SIZE_W means that
1132 the linker cannot use allocateExec for loading object code files
1133 on Windows. Once allocateExec can handle larger objects, the linker
1134 should be modified to use allocateExec instead of VirtualAlloc.
1135 ------------------------------------------------------------------------- */
1137 static bdescr *exec_block;
1139 void *allocateExec (nat bytes)
1146 // round up to words.
1147 n = (bytes + sizeof(W_) + 1) / sizeof(W_);
1149 if (n+1 > BLOCK_SIZE_W) {
1150 barf("allocateExec: can't handle large objects");
1153 if (exec_block == NULL ||
1154 exec_block->free + n + 1 > exec_block->start + BLOCK_SIZE_W) {
1156 lnat pagesize = getPageSize();
1157 bd = allocGroup(stg_max(1, pagesize / BLOCK_SIZE));
1158 debugTrace(DEBUG_gc, "allocate exec block %p", bd->start);
1160 bd->flags = BF_EXEC;
1161 bd->link = exec_block;
1162 if (exec_block != NULL) {
1163 exec_block->u.back = bd;
1166 setExecutable(bd->start, bd->blocks * BLOCK_SIZE, rtsTrue);
1169 *(exec_block->free) = n; // store the size of this chunk
1170 exec_block->gen_no += n; // gen_no stores the number of words allocated
1171 ret = exec_block->free + 1;
1172 exec_block->free += n + 1;
1178 void freeExec (void *addr)
1180 StgPtr p = (StgPtr)addr - 1;
1181 bdescr *bd = Bdescr((StgPtr)p);
1183 if ((bd->flags & BF_EXEC) == 0) {
1184 barf("freeExec: not executable");
1187 if (*(StgPtr)p == 0) {
1188 barf("freeExec: already free?");
1193 bd->gen_no -= *(StgPtr)p;
1196 if (bd->gen_no == 0) {
1197 // Free the block if it is empty, but not if it is the block at
1198 // the head of the queue.
1199 if (bd != exec_block) {
1200 debugTrace(DEBUG_gc, "free exec block %p", bd->start);
1201 dbl_link_remove(bd, &exec_block);
1202 setExecutable(bd->start, bd->blocks * BLOCK_SIZE, rtsFalse);
1205 bd->free = bd->start;
1212 /* -----------------------------------------------------------------------------
1215 memInventory() checks for memory leaks by counting up all the
1216 blocks we know about and comparing that to the number of blocks
1217 allegedly floating around in the system.
1218 -------------------------------------------------------------------------- */
1222 // Useful for finding partially full blocks in gdb
1223 void findSlop(bdescr *bd);
1224 void findSlop(bdescr *bd)
1228 for (; bd != NULL; bd = bd->link) {
1229 slop = (bd->blocks * BLOCK_SIZE_W) - (bd->free - bd->start);
1230 if (slop > (1024/sizeof(W_))) {
1231 debugBelch("block at %p (bdescr %p) has %ldKB slop\n",
1232 bd->start, bd, slop / (1024/sizeof(W_)));
1238 countBlocks(bdescr *bd)
1241 for (n=0; bd != NULL; bd=bd->link) {
1247 // (*1) Just like countBlocks, except that we adjust the count for a
1248 // megablock group so that it doesn't include the extra few blocks
1249 // that would be taken up by block descriptors in the second and
1250 // subsequent megablock. This is so we can tally the count with the
1251 // number of blocks allocated in the system, for memInventory().
1253 countAllocdBlocks(bdescr *bd)
1256 for (n=0; bd != NULL; bd=bd->link) {
1258 // hack for megablock groups: see (*1) above
1259 if (bd->blocks > BLOCKS_PER_MBLOCK) {
1260 n -= (MBLOCK_SIZE / BLOCK_SIZE - BLOCKS_PER_MBLOCK)
1261 * (bd->blocks/(MBLOCK_SIZE/BLOCK_SIZE));
1268 stepBlocks (step *stp)
1270 ASSERT(countBlocks(stp->blocks) == stp->n_blocks);
1271 ASSERT(countBlocks(stp->large_objects) == stp->n_large_blocks);
1272 return stp->n_blocks + stp->n_old_blocks +
1273 countAllocdBlocks(stp->large_objects);
1276 // If memInventory() calculates that we have a memory leak, this
1277 // function will try to find the block(s) that are leaking by marking
1278 // all the ones that we know about, and search through memory to find
1279 // blocks that are not marked. In the debugger this can help to give
1280 // us a clue about what kind of block leaked. In the future we might
1281 // annotate blocks with their allocation site to give more helpful
1284 findMemoryLeak (void)
1287 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
1288 for (i = 0; i < n_capabilities; i++) {
1289 markBlocks(capabilities[i].mut_lists[g]);
1291 markBlocks(generations[g].mut_list);
1292 for (s = 0; s < generations[g].n_steps; s++) {
1293 markBlocks(generations[g].steps[s].blocks);
1294 markBlocks(generations[g].steps[s].large_objects);
1298 for (i = 0; i < n_nurseries; i++) {
1299 markBlocks(nurseries[i].blocks);
1300 markBlocks(nurseries[i].large_objects);
1305 // if (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_RETAINER) {
1306 // markRetainerBlocks();
1310 // count the blocks allocated by the arena allocator
1312 // markArenaBlocks();
1314 // count the blocks containing executable memory
1315 markBlocks(exec_block);
1317 reportUnmarkedBlocks();
1322 memInventory (rtsBool show)
1326 lnat gen_blocks[RtsFlags.GcFlags.generations];
1327 lnat nursery_blocks, retainer_blocks,
1328 arena_blocks, exec_blocks;
1329 lnat live_blocks = 0, free_blocks = 0;
1332 // count the blocks we current have
1334 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
1336 for (i = 0; i < n_capabilities; i++) {
1337 gen_blocks[g] += countBlocks(capabilities[i].mut_lists[g]);
1339 gen_blocks[g] += countAllocdBlocks(generations[g].mut_list);
1340 for (s = 0; s < generations[g].n_steps; s++) {
1341 stp = &generations[g].steps[s];
1342 gen_blocks[g] += stepBlocks(stp);
1347 for (i = 0; i < n_nurseries; i++) {
1348 nursery_blocks += stepBlocks(&nurseries[i]);
1351 retainer_blocks = 0;
1353 if (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_RETAINER) {
1354 retainer_blocks = retainerStackBlocks();
1358 // count the blocks allocated by the arena allocator
1359 arena_blocks = arenaBlocks();
1361 // count the blocks containing executable memory
1362 exec_blocks = countAllocdBlocks(exec_block);
1364 /* count the blocks on the free list */
1365 free_blocks = countFreeList();
1368 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
1369 live_blocks += gen_blocks[g];
1371 live_blocks += nursery_blocks +
1372 + retainer_blocks + arena_blocks + exec_blocks;
1374 #define MB(n) (((n) * BLOCK_SIZE_W) / ((1024*1024)/sizeof(W_)))
1376 leak = live_blocks + free_blocks != mblocks_allocated * BLOCKS_PER_MBLOCK;
1381 debugBelch("Memory leak detected:\n");
1383 debugBelch("Memory inventory:\n");
1385 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
1386 debugBelch(" gen %d blocks : %5lu blocks (%lu MB)\n", g,
1387 gen_blocks[g], MB(gen_blocks[g]));
1389 debugBelch(" nursery : %5lu blocks (%lu MB)\n",
1390 nursery_blocks, MB(nursery_blocks));
1391 debugBelch(" retainer : %5lu blocks (%lu MB)\n",
1392 retainer_blocks, MB(retainer_blocks));
1393 debugBelch(" arena blocks : %5lu blocks (%lu MB)\n",
1394 arena_blocks, MB(arena_blocks));
1395 debugBelch(" exec : %5lu blocks (%lu MB)\n",
1396 exec_blocks, MB(exec_blocks));
1397 debugBelch(" free : %5lu blocks (%lu MB)\n",
1398 free_blocks, MB(free_blocks));
1399 debugBelch(" total : %5lu blocks (%lu MB)\n",
1400 live_blocks + free_blocks, MB(live_blocks+free_blocks));
1402 debugBelch("\n in system : %5lu blocks (%lu MB)\n",
1403 mblocks_allocated * BLOCKS_PER_MBLOCK, mblocks_allocated);
1411 ASSERT(n_alloc_blocks == live_blocks);
1416 /* Full heap sanity check. */
1422 if (RtsFlags.GcFlags.generations == 1) {
1423 checkHeap(g0s0->blocks);
1424 checkChain(g0s0->large_objects);
1427 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
1428 for (s = 0; s < generations[g].n_steps; s++) {
1429 if (g == 0 && s == 0) { continue; }
1430 ASSERT(countBlocks(generations[g].steps[s].blocks)
1431 == generations[g].steps[s].n_blocks);
1432 ASSERT(countBlocks(generations[g].steps[s].large_objects)
1433 == generations[g].steps[s].n_large_blocks);
1434 checkHeap(generations[g].steps[s].blocks);
1435 checkChain(generations[g].steps[s].large_objects);
1437 checkMutableList(generations[g].mut_list, g);
1442 for (s = 0; s < n_nurseries; s++) {
1443 ASSERT(countBlocks(nurseries[s].blocks)
1444 == nurseries[s].n_blocks);
1445 ASSERT(countBlocks(nurseries[s].large_objects)
1446 == nurseries[s].n_large_blocks);
1449 checkFreeListSanity();
1452 #if defined(THREADED_RTS)
1453 // check the stacks too in threaded mode, because we don't do a
1454 // full heap sanity check in this case (see checkHeap())
1455 checkGlobalTSOList(rtsTrue);
1457 checkGlobalTSOList(rtsFalse);
1461 /* Nursery sanity check */
1463 checkNurserySanity( step *stp )
1469 for (bd = stp->blocks; bd != NULL; bd = bd->link) {
1470 ASSERT(bd->u.back == prev);
1472 blocks += bd->blocks;
1474 ASSERT(blocks == stp->n_blocks);
1477 // handy function for use in gdb, because Bdescr() is inlined.
1478 extern bdescr *_bdescr( StgPtr p );