1 /* -----------------------------------------------------------------------------
2 * $Id: LdvProfile.c,v 1.4 2003/01/30 10:06:35 simonmar Exp $
4 * (c) The GHC Team, 2001
7 * Lag/Drag/Void profiling.
9 * ---------------------------------------------------------------------------*/
15 #include "LdvProfile.h"
18 #include "Proftimer.h"
19 #include "Profiling.h"
25 /* --------------------------------------------------------------------------
26 * Fills in the slop when a *dynamic* closure changes its type.
27 * First calls LDV_recordDead() to declare the closure is dead, and then
31 * 1) blackholing, UPD_BH_UPDATABLE() and UPD_BH_SINGLE_ENTRY (in
32 * includes/StgMacros.h), threadLazyBlackHole() and
33 * threadSqueezeStack() (in GC.c).
34 * 2) updating with indirection closures, updateWithIndirection()
35 * and updateWithPermIndirection() (in Storage.h).
37 * LDV_recordDead_FILL_SLOP_DYNAMIC() is not called on 'inherently used'
38 * closures such as TSO. It is not called on PAP because PAP is not updatable.
39 * ----------------------------------------------------------------------- */
41 LDV_recordDead_FILL_SLOP_DYNAMIC( StgClosure *p )
46 #if defined(__GNUC__) && __GNUC__ < 3 && defined(DEBUG)
47 #error Please use gcc 3.0+ to compile this file with DEBUG; gcc < 3.0 miscompiles it
62 nw = info->layout.payload.ptrs + info->layout.payload.nptrs;
63 if (nw < MIN_UPD_SIZE)
67 nw = sizeofW(StgPAP) - sizeofW(StgHeader) + ((StgPAP *)p)->n_args;
70 nw = sizeofW(StgAP_STACK) - sizeofW(StgHeader)
71 + ((StgAP_STACK *)p)->size;
76 case SE_CAF_BLACKHOLE:
77 nw = info->layout.payload.ptrs + info->layout.payload.nptrs;
80 barf("Unexpected closure type %u in LDV_recordDead_FILL_SLOP_DYNAMIC()", info->type);
83 LDV_recordDead((StgClosure *)(p), nw + sizeofW(StgHeader));
84 for (i = 0; i < nw; i++) {
85 ((StgClosure *)(p))->payload[i] = 0;
90 /* --------------------------------------------------------------------------
91 * This function is called eventually on every object destroyed during
92 * a garbage collection, whether it is a major garbage collection or
93 * not. If c is an 'inherently used' closure, nothing happens. If c
94 * is an ordinary closure, LDV_recordDead() is called on c with its
95 * proper size which excludes the profiling header portion in the
96 * closure. Returns the size of the closure, including the profiling
97 * header portion, so that the caller can find the next closure.
98 * ----------------------------------------------------------------------- */
100 processHeapClosureForDead( StgClosure *c )
107 if (info->type != EVACUATED) {
108 ASSERT(((LDVW(c) & LDV_CREATE_MASK) >> LDV_SHIFT) <= era &&
109 ((LDVW(c) & LDV_CREATE_MASK) >> LDV_SHIFT) > 0);
110 ASSERT(((LDVW(c) & LDV_STATE_MASK) == LDV_STATE_CREATE) ||
112 (LDVW(c) & LDV_LAST_MASK) <= era &&
113 (LDVW(c) & LDV_LAST_MASK) > 0
117 switch (info->type) {
119 'inherently used' cases: do nothing.
123 size = tso_sizeW((StgTSO *)c);
127 size = sizeofW(StgMVar);
131 case MUT_ARR_PTRS_FROZEN:
132 size = mut_arr_ptrs_sizeW((StgMutArrPtrs *)c);
136 size = arr_words_sizeW((StgArrWords *)c);
145 size = sizeW_fromITBL(info);
149 ordinary cases: call LDV_recordDead().
153 size = stg_max(sizeW_fromITBL(info), sizeofW(StgHeader) + MIN_UPD_SIZE);
162 size = sizeofW(StgHeader) + MIN_UPD_SIZE;
167 size = pap_sizeW((StgPAP *)c);
171 size = ap_stack_sizeW((StgAP_STACK *)c);
192 case SE_CAF_BLACKHOLE:
193 size = sizeW_fromITBL(info);
197 size = sizeofW(StgInd);
200 case IND_OLDGEN_PERM:
201 size = sizeofW(StgIndOldGen);
207 // Why can we ignore IND/IND_OLDGEN closures? We assume that
208 // any census is preceded by a major garbage collection, which
209 // IND/IND_OLDGEN closures cannot survive. Therefore, it is no
210 // use considering IND/IND_OLDGEN closures in the meanwhile
211 // because they will perish before the next census at any
214 size = sizeofW(StgInd);
218 size = sizeofW(StgIndOldGen);
222 // The size of the evacuated closure is currently stored in
223 // the LDV field. See SET_EVACUAEE_FOR_LDV() in
224 // includes/StgLdvProf.h.
236 case CONSTR_CHARLIKE:
237 case CONSTR_NOCAF_STATIC:
256 barf("Invalid object in processHeapClosureForDead(): %d", info->type);
260 // Found a dead closure: record its size
261 LDV_recordDead(c, size);
265 /* --------------------------------------------------------------------------
266 * Calls processHeapClosureForDead() on every *dead* closures in the
267 * heap blocks starting at bd.
268 * ----------------------------------------------------------------------- */
270 processHeapForDead( bdescr *bd )
276 while (p < bd->free) {
277 p += processHeapClosureForDead((StgClosure *)p);
278 while (p < bd->free && !*p) // skip slop
281 ASSERT(p == bd->free);
286 /* --------------------------------------------------------------------------
287 * Calls processHeapClosureForDead() on every *dead* closures in the nursery.
288 * ----------------------------------------------------------------------- */
290 processNurseryForDead( void )
295 bd = MainCapability.r.rNursery;
296 while (bd->start < bd->free) {
298 bdLimit = bd->start + BLOCK_SIZE_W;
299 while (p < bd->free && p < bdLimit) {
300 p += processHeapClosureForDead((StgClosure *)p);
301 while (p < bd->free && p < bdLimit && !*p) // skip slop
310 /* --------------------------------------------------------------------------
311 * Calls processHeapClosureForDead() on every *dead* closures in the
313 * ----------------------------------------------------------------------- */
315 processSmallObjectPoolForDead( void )
320 bd = small_alloc_list;
327 while (p < alloc_Hp) {
328 p += processHeapClosureForDead((StgClosure *)p);
329 while (p < alloc_Hp && !*p) // skip slop
332 ASSERT(p == alloc_Hp);
337 while (p < bd->free) {
338 p += processHeapClosureForDead((StgClosure *)p);
339 while (p < bd->free && !*p) // skip slop
342 ASSERT(p == bd->free);
347 /* --------------------------------------------------------------------------
348 * Calls processHeapClosureForDead() on every *dead* closures in the closure
350 * ----------------------------------------------------------------------- */
352 processChainForDead( bdescr *bd )
354 // Any object still in the chain is dead!
356 processHeapClosureForDead((StgClosure *)bd->start);
361 /* --------------------------------------------------------------------------
362 * Start a census for *dead* closures, and calls
363 * processHeapClosureForDead() on every closure which died in the
364 * current garbage collection. This function is called from a garbage
365 * collector right before tidying up, when all dead closures are still
366 * stored in the heap and easy to identify. Generations 0 through N
367 * have just beed garbage collected.
368 * ----------------------------------------------------------------------- */
370 LdvCensusForDead( nat N )
374 // ldvTime == 0 means that LDV profiling is currently turned off.
378 if (RtsFlags.GcFlags.generations == 1) {
380 // Todo: support LDV for two-space garbage collection.
382 barf("Lag/Drag/Void profiling not supported with -G1");
384 for (g = 0; g <= N; g++)
385 for (s = 0; s < generations[g].n_steps; s++) {
386 if (g == 0 && s == 0) {
387 processSmallObjectPoolForDead();
388 processNurseryForDead();
389 processChainForDead(generations[g].steps[s].large_objects);
391 processHeapForDead(generations[g].steps[s].blocks);
392 processChainForDead(generations[g].steps[s].large_objects);
398 /* --------------------------------------------------------------------------
399 * Regard any closure in the current heap as dead or moribund and update
400 * LDV statistics accordingly.
401 * Called from shutdownHaskell() in RtsStartup.c.
402 * Also, stops LDV profiling by resetting ldvTime to 0.
403 * ----------------------------------------------------------------------- */
405 LdvCensusKillAll( void )
407 LdvCensusForDead(RtsFlags.GcFlags.generations - 1);
410 #endif /* PROFILING */