1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team, 1998-2004
5 * External Storage Manger Interface
7 * ---------------------------------------------------------------------------*/
13 #include "OSThreads.h"
15 /* -----------------------------------------------------------------------------
18 * We support an arbitrary number of generations, with an arbitrary number
19 * of steps per generation. Notes (in no particular order):
21 * - all generations except the oldest should have two steps. This gives
22 * objects a decent chance to age before being promoted, and in
23 * particular will ensure that we don't end up with too many
24 * thunks being updated in older generations.
26 * - the oldest generation has one step. There's no point in aging
27 * objects in the oldest generation.
29 * - generation 0, step 0 (G0S0) is the allocation area. It is given
30 * a fixed set of blocks during initialisation, and these blocks
33 * - during garbage collection, each step which is an evacuation
34 * destination (i.e. all steps except G0S0) is allocated a to-space.
35 * evacuated objects are allocated into the step's to-space until
36 * GC is finished, when the original step's contents may be freed
37 * and replaced by the to-space.
39 * - the mutable-list is per-generation (not per-step). G0 doesn't
40 * have one (since every garbage collection collects at least G0).
42 * - block descriptors contain pointers to both the step and the
43 * generation that the block belongs to, for convenience.
45 * - static objects are stored in per-generation lists. See GC.c for
46 * details of how we collect CAFs in the generational scheme.
48 * - large objects are per-step, and are promoted in the same way
49 * as small objects, except that we may allocate large objects into
50 * generation 1 initially.
52 * ------------------------------------------------------------------------- */
54 typedef struct step_ {
55 unsigned int no; /* step number */
56 bdescr * blocks; /* blocks in this step */
57 unsigned int n_blocks; /* number of blocks */
58 struct step_ * to; /* destination step for live objects */
59 struct generation_ * gen; /* generation this step belongs to */
60 unsigned int gen_no; /* generation number (cached) */
61 bdescr * large_objects; /* large objects (doubly linked) */
62 unsigned int n_large_blocks; /* no. of blocks used by large objs */
63 int is_compacted; /* compact this step? (old gen only) */
65 /* During GC, if we are collecting this step, blocks and n_blocks
66 * are copied into the following two fields. After GC, these blocks
68 bdescr * old_blocks; /* bdescr of first from-space block */
69 unsigned int n_old_blocks; /* number of blocks in from-space */
71 /* temporary use during GC: */
72 StgPtr hp; /* next free locn in to-space */
73 StgPtr hpLim; /* end of current to-space block */
74 bdescr * hp_bd; /* bdescr of current to-space block */
75 StgPtr scavd_hp; /* ... same as above, but already */
76 StgPtr scavd_hpLim; /* scavenged. */
77 bdescr * scan_bd; /* block currently being scanned */
78 StgPtr scan; /* scan pointer in current block */
79 bdescr * new_large_objects; /* large objects collected so far */
80 bdescr * scavenged_large_objects; /* live large objs after GC (d-link) */
81 unsigned int n_scavenged_large_blocks;/* size of above */
82 bdescr * bitmap; /* bitmap for compacting collection */
85 typedef struct generation_ {
86 unsigned int no; /* generation number */
87 step * steps; /* steps */
88 unsigned int n_steps; /* number of steps */
89 unsigned int max_blocks; /* max blocks in step 0 */
90 bdescr *mut_list; /* mut objects in this gen (not G0)*/
92 /* temporary use during GC: */
93 bdescr *saved_mut_list;
95 /* stats information */
96 unsigned int collections;
97 unsigned int failed_promotions;
100 extern generation * RTS_VAR(generations);
102 extern generation * RTS_VAR(g0);
103 extern step * RTS_VAR(g0s0);
104 extern generation * RTS_VAR(oldest_gen);
106 /* -----------------------------------------------------------------------------
107 Initialisation / De-initialisation
108 -------------------------------------------------------------------------- */
110 extern void initStorage(void);
111 extern void exitStorage(void);
113 /* -----------------------------------------------------------------------------
116 StgPtr allocate(nat n) Allocates a chunk of contiguous store
117 n words long, returning a pointer to
118 the first word. Always succeeds.
120 StgPtr allocatePinned(nat n) Allocates a chunk of contiguous store
121 n words long, which is at a fixed
122 address (won't be moved by GC).
123 Returns a pointer to the first word.
126 NOTE: the GC can't in general handle
127 pinned objects, so allocatePinned()
128 can only be used for ByteArrays at the
131 Don't forget to TICK_ALLOC_XXX(...)
132 after calling allocate or
133 allocatePinned, for the
134 benefit of the ticky-ticky profiler.
136 rtsBool doYouWantToGC(void) Returns True if the storage manager is
137 ready to perform a GC, False otherwise.
139 lnat allocated_bytes(void) Returns the number of bytes allocated
140 via allocate() since the last GC.
141 Used in the reporting of statistics.
143 SMP: allocate and doYouWantToGC can be used from STG code, they are
144 surrounded by a mutex.
145 -------------------------------------------------------------------------- */
147 extern StgPtr allocate ( nat n );
148 extern StgPtr allocateLocal ( StgRegTable *reg, nat n );
149 extern StgPtr allocatePinned ( nat n );
150 extern lnat allocated_bytes ( void );
152 extern bdescr * RTS_VAR(small_alloc_list);
153 extern bdescr * RTS_VAR(large_alloc_list);
154 extern bdescr * RTS_VAR(pinned_object_block);
156 extern StgPtr RTS_VAR(alloc_Hp);
157 extern StgPtr RTS_VAR(alloc_HpLim);
159 extern nat RTS_VAR(alloc_blocks);
160 extern nat RTS_VAR(alloc_blocks_lim);
162 INLINE_HEADER rtsBool
163 doYouWantToGC( void )
165 return (alloc_blocks >= alloc_blocks_lim);
168 /* -----------------------------------------------------------------------------
169 Performing Garbage Collection
171 GarbageCollect(get_roots) Performs a garbage collection.
172 'get_roots' is called to find all the
173 roots that the system knows about.
175 StgClosure Called by get_roots on each root.
176 MarkRoot(StgClosure *p) Returns the new location of the root.
177 -------------------------------------------------------------------------- */
179 extern void GarbageCollect(void (*get_roots)(evac_fn),rtsBool force_major_gc);
181 /* -----------------------------------------------------------------------------
182 Generational garbage collection support
184 recordMutable(StgPtr p) Informs the garbage collector that a
185 previously immutable object has
186 become (permanently) mutable. Used
187 by thawArray and similar.
189 updateWithIndirection(p1,p2) Updates the object at p1 with an
190 indirection pointing to p2. This is
191 normally called for objects in an old
192 generation (>0) when they are updated.
194 updateWithPermIndirection(p1,p2) As above but uses a permanent indir.
196 -------------------------------------------------------------------------- */
199 * Storage manager mutex
202 extern Mutex sm_mutex;
206 #define ACQUIRE_SM_LOCK ACQUIRE_LOCK(&sm_mutex);
207 #define RELEASE_SM_LOCK RELEASE_LOCK(&sm_mutex);
209 #define ACQUIRE_SM_LOCK
210 #define RELEASE_SM_LOCK
214 recordMutableGen(StgClosure *p, generation *gen)
219 if (bd->free >= bd->start + BLOCK_SIZE_W) {
221 new_bd = allocBlock();
226 *bd->free++ = (StgWord)p;
231 recordMutableGenLock(StgClosure *p, generation *gen)
234 recordMutableGen(p,gen);
239 recordMutable(StgClosure *p)
242 ASSERT(closure_MUTABLE(p));
244 if (bd->gen_no > 0) recordMutableGen(p, &RTS_DEREF(generations)[bd->gen_no]);
248 recordMutableLock(StgClosure *p)
255 /* -----------------------------------------------------------------------------
256 The CAF table - used to let us revert CAFs in GHCi
257 -------------------------------------------------------------------------- */
259 /* set to disable CAF garbage collection in GHCi. */
260 /* (needed when dynamic libraries are used). */
261 extern rtsBool keepCAFs;
263 /* -----------------------------------------------------------------------------
264 DEBUGGING predicates for pointers
266 LOOKS_LIKE_INFO_PTR(p) returns False if p is definitely not an info ptr
267 LOOKS_LIKE_CLOSURE_PTR(p) returns False if p is definitely not a closure ptr
269 These macros are complete but not sound. That is, they might
270 return false positives. Do not rely on them to distinguish info
271 pointers from closure pointers, for example.
273 We don't use address-space predicates these days, for portability
274 reasons, and the fact that code/data can be scattered about the
275 address space in a dynamically-linked environment. Our best option
276 is to look at the alleged info table and see whether it seems to
278 -------------------------------------------------------------------------- */
280 #define LOOKS_LIKE_INFO_PTR(p) \
281 (p && ((StgInfoTable *)(INFO_PTR_TO_STRUCT(p)))->type != INVALID_OBJECT && \
282 ((StgInfoTable *)(INFO_PTR_TO_STRUCT(p)))->type < N_CLOSURE_TYPES)
284 #define LOOKS_LIKE_CLOSURE_PTR(p) \
285 (LOOKS_LIKE_INFO_PTR(((StgClosure *)(p))->header.info))
287 /* -----------------------------------------------------------------------------
288 Macros for calculating how big a closure will be (used during allocation)
289 -------------------------------------------------------------------------- */
291 INLINE_HEADER StgOffset PAP_sizeW ( nat n_args )
292 { return sizeofW(StgPAP) + n_args; }
294 INLINE_HEADER StgOffset AP_sizeW ( nat n_args )
295 { return sizeofW(StgAP) + n_args; }
297 INLINE_HEADER StgOffset AP_STACK_sizeW ( nat size )
298 { return sizeofW(StgAP_STACK) + size; }
300 INLINE_HEADER StgOffset CONSTR_sizeW( nat p, nat np )
301 { return sizeofW(StgHeader) + p + np; }
303 INLINE_HEADER StgOffset THUNK_SELECTOR_sizeW ( void )
304 { return stg_max(sizeofW(StgHeader)+MIN_UPD_SIZE, sizeofW(StgSelector)); }
306 INLINE_HEADER StgOffset BLACKHOLE_sizeW ( void )
307 { return sizeofW(StgHeader)+MIN_UPD_SIZE; }
309 /* --------------------------------------------------------------------------
311 ------------------------------------------------------------------------*/
313 INLINE_HEADER StgOffset sizeW_fromITBL( const StgInfoTable* itbl )
314 { return sizeofW(StgClosure)
315 + sizeofW(StgPtr) * itbl->layout.payload.ptrs
316 + sizeofW(StgWord) * itbl->layout.payload.nptrs; }
318 INLINE_HEADER StgOffset thunk_sizeW_fromITBL( const StgInfoTable* itbl )
319 { return sizeofW(StgThunk)
320 + sizeofW(StgPtr) * itbl->layout.payload.ptrs
321 + sizeofW(StgWord) * itbl->layout.payload.nptrs; }
323 INLINE_HEADER StgOffset ap_stack_sizeW( StgAP_STACK* x )
324 { return AP_STACK_sizeW(x->size); }
326 INLINE_HEADER StgOffset ap_sizeW( StgAP* x )
327 { return AP_sizeW(x->n_args); }
329 INLINE_HEADER StgOffset pap_sizeW( StgPAP* x )
330 { return PAP_sizeW(x->n_args); }
332 INLINE_HEADER StgOffset arr_words_sizeW( StgArrWords* x )
333 { return sizeofW(StgArrWords) + x->words; }
335 INLINE_HEADER StgOffset mut_arr_ptrs_sizeW( StgMutArrPtrs* x )
336 { return sizeofW(StgMutArrPtrs) + x->ptrs; }
338 INLINE_HEADER StgWord tso_sizeW ( StgTSO *tso )
339 { return TSO_STRUCT_SIZEW + tso->stack_size; }
341 INLINE_HEADER StgWord bco_sizeW ( StgBCO *bco )
342 { return bco->size; }
344 /* -----------------------------------------------------------------------------
345 Sizes of stack frames
346 -------------------------------------------------------------------------- */
348 INLINE_HEADER StgWord stack_frame_sizeW( StgClosure *frame )
350 StgRetInfoTable *info;
352 info = get_ret_itbl(frame);
353 switch (info->i.type) {
357 StgRetDyn *dyn = (StgRetDyn *)frame;
358 return sizeofW(StgRetDyn) + RET_DYN_BITMAP_SIZE +
359 RET_DYN_NONPTR_REGS_SIZE +
360 RET_DYN_PTRS(dyn->liveness) + RET_DYN_NONPTRS(dyn->liveness);
364 return sizeofW(StgRetFun) + ((StgRetFun *)frame)->size;
368 return 1 + GET_LARGE_BITMAP(&info->i)->size;
371 return 2 + BCO_BITMAP_SIZE((StgBCO *)((P_)frame)[1]);
374 return 1 + BITMAP_SIZE(info->i.layout.bitmap);
378 /* -----------------------------------------------------------------------------
380 -------------------------------------------------------------------------- */
382 extern void allocNurseries ( void );
383 extern void resetNurseries ( void );
384 extern void resizeNurseries ( nat blocks );
385 extern void resizeNurseriesFixed ( nat blocks );
386 extern void tidyAllocateLists ( void );
387 extern lnat countNurseryBlocks ( void );
389 /* -----------------------------------------------------------------------------
391 -------------------------------------------------------------------------- */
393 extern void threadPaused ( StgTSO * );
394 extern StgClosure * isAlive ( StgClosure *p );
395 extern void markCAFs ( evac_fn evac );
397 /* -----------------------------------------------------------------------------
398 Stats 'n' DEBUG stuff
399 -------------------------------------------------------------------------- */
401 extern ullong RTS_VAR(total_allocated);
403 extern lnat calcAllocated ( void );
404 extern lnat calcLive ( void );
405 extern lnat calcNeeded ( void );
408 extern void memInventory(void);
409 extern void checkSanity(void);
410 extern nat countBlocks(bdescr *);
411 extern void checkNurserySanity( step *stp );
415 void printMutOnceList(generation *gen);
416 void printMutableList(generation *gen);
419 /* ----------------------------------------------------------------------------
420 Storage manager internal APIs and globals
421 ------------------------------------------------------------------------- */
423 #define END_OF_STATIC_LIST stgCast(StgClosure*,1)
425 extern void newDynCAF(StgClosure *);
427 extern void move_TSO(StgTSO *src, StgTSO *dest);
428 extern StgTSO *relocate_stack(StgTSO *dest, ptrdiff_t diff);
430 extern StgClosure * RTS_VAR(scavenged_static_objects);
431 extern StgWeak * RTS_VAR(old_weak_ptr_list);
432 extern StgWeak * RTS_VAR(weak_ptr_list);
433 extern StgClosure * RTS_VAR(caf_list);
434 extern StgClosure * RTS_VAR(revertible_caf_list);
435 extern StgTSO * RTS_VAR(resurrected_threads);
437 #endif /* STORAGE_H */