1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team, 2001
6 * Lag/Drag/Void profiling.
8 * ---------------------------------------------------------------------------*/
13 #include "LdvProfile.h"
15 #include "Profiling.h"
21 /* --------------------------------------------------------------------------
22 * Fills in the slop when a *dynamic* closure changes its type.
23 * First calls LDV_recordDead() to declare the closure is dead, and then
27 * 1) blackholing, UPD_BH_UPDATABLE() and UPD_BH_SINGLE_ENTRY (in
28 * includes/StgMacros.h), threadLazyBlackHole() and
29 * threadSqueezeStack() (in GC.c).
30 * 2) updating with indirection closures, updateWithIndirection()
31 * and updateWithPermIndirection() (in Storage.h).
33 * LDV_recordDead_FILL_SLOP_DYNAMIC() is not called on 'inherently used'
34 * closures such as TSO. It is not called on PAP because PAP is not updatable.
35 * ----------------------------------------------------------------------- */
37 LDV_recordDead_FILL_SLOP_DYNAMIC( StgClosure *p )
42 #if defined(__GNUC__) && __GNUC__ < 3 && defined(DEBUG)
43 #error Please use gcc 3.0+ to compile this file with DEBUG; gcc < 3.0 miscompiles it
58 nw = info->layout.payload.ptrs + info->layout.payload.nptrs;
59 if (nw < MIN_UPD_SIZE)
63 nw = sizeofW(StgPAP) - sizeofW(StgHeader) + ((StgPAP *)p)->n_args;
66 nw = sizeofW(StgAP_STACK) - sizeofW(StgHeader)
67 + ((StgAP_STACK *)p)->size;
72 case SE_CAF_BLACKHOLE:
73 nw = info->layout.payload.ptrs + info->layout.payload.nptrs;
76 barf("Unexpected closure type %u in LDV_recordDead_FILL_SLOP_DYNAMIC()", info->type);
79 LDV_recordDead((StgClosure *)(p), nw + sizeofW(StgHeader));
80 for (i = 0; i < nw; i++) {
81 ((StgClosure *)(p))->payload[i] = 0;
86 /* --------------------------------------------------------------------------
87 * This function is called eventually on every object destroyed during
88 * a garbage collection, whether it is a major garbage collection or
89 * not. If c is an 'inherently used' closure, nothing happens. If c
90 * is an ordinary closure, LDV_recordDead() is called on c with its
91 * proper size which excludes the profiling header portion in the
92 * closure. Returns the size of the closure, including the profiling
93 * header portion, so that the caller can find the next closure.
94 * ----------------------------------------------------------------------- */
96 processHeapClosureForDead( StgClosure *c )
103 if (info->type != EVACUATED) {
104 ASSERT(((LDVW(c) & LDV_CREATE_MASK) >> LDV_SHIFT) <= era &&
105 ((LDVW(c) & LDV_CREATE_MASK) >> LDV_SHIFT) > 0);
106 ASSERT(((LDVW(c) & LDV_STATE_MASK) == LDV_STATE_CREATE) ||
108 (LDVW(c) & LDV_LAST_MASK) <= era &&
109 (LDVW(c) & LDV_LAST_MASK) > 0
113 switch (info->type) {
115 'inherently used' cases: do nothing.
119 size = tso_sizeW((StgTSO *)c);
123 size = sizeofW(StgMVar);
127 case MUT_ARR_PTRS_FROZEN:
128 size = mut_arr_ptrs_sizeW((StgMutArrPtrs *)c);
132 size = arr_words_sizeW((StgArrWords *)c);
141 size = sizeW_fromITBL(info);
145 ordinary cases: call LDV_recordDead().
149 size = stg_max(sizeW_fromITBL(info), sizeofW(StgHeader) + MIN_UPD_SIZE);
158 size = sizeofW(StgHeader) + MIN_UPD_SIZE;
163 size = pap_sizeW((StgPAP *)c);
167 size = ap_stack_sizeW((StgAP_STACK *)c);
188 case SE_CAF_BLACKHOLE:
189 size = sizeW_fromITBL(info);
193 size = sizeofW(StgInd);
196 case IND_OLDGEN_PERM:
197 size = sizeofW(StgIndOldGen);
203 // Why can we ignore IND/IND_OLDGEN closures? We assume that
204 // any census is preceded by a major garbage collection, which
205 // IND/IND_OLDGEN closures cannot survive. Therefore, it is no
206 // use considering IND/IND_OLDGEN closures in the meanwhile
207 // because they will perish before the next census at any
210 size = sizeofW(StgInd);
214 size = sizeofW(StgIndOldGen);
218 // The size of the evacuated closure is currently stored in
219 // the LDV field. See SET_EVACUAEE_FOR_LDV() in
220 // includes/StgLdvProf.h.
232 case CONSTR_CHARLIKE:
233 case CONSTR_NOCAF_STATIC:
252 barf("Invalid object in processHeapClosureForDead(): %d", info->type);
256 // Found a dead closure: record its size
257 LDV_recordDead(c, size);
261 /* --------------------------------------------------------------------------
262 * Calls processHeapClosureForDead() on every *dead* closures in the
263 * heap blocks starting at bd.
264 * ----------------------------------------------------------------------- */
266 processHeapForDead( bdescr *bd )
272 while (p < bd->free) {
273 p += processHeapClosureForDead((StgClosure *)p);
274 while (p < bd->free && !*p) // skip slop
277 ASSERT(p == bd->free);
282 /* --------------------------------------------------------------------------
283 * Calls processHeapClosureForDead() on every *dead* closures in the nursery.
284 * ----------------------------------------------------------------------- */
286 processNurseryForDead( void )
291 bd = MainCapability.r.rNursery;
292 while (bd->start < bd->free) {
294 bdLimit = bd->start + BLOCK_SIZE_W;
295 while (p < bd->free && p < bdLimit) {
296 p += processHeapClosureForDead((StgClosure *)p);
297 while (p < bd->free && p < bdLimit && !*p) // skip slop
306 /* --------------------------------------------------------------------------
307 * Calls processHeapClosureForDead() on every *dead* closures in the
309 * ----------------------------------------------------------------------- */
311 processSmallObjectPoolForDead( void )
316 bd = small_alloc_list;
323 while (p < alloc_Hp) {
324 p += processHeapClosureForDead((StgClosure *)p);
325 while (p < alloc_Hp && !*p) // skip slop
328 ASSERT(p == alloc_Hp);
333 while (p < bd->free) {
334 p += processHeapClosureForDead((StgClosure *)p);
335 while (p < bd->free && !*p) // skip slop
338 ASSERT(p == bd->free);
343 /* --------------------------------------------------------------------------
344 * Calls processHeapClosureForDead() on every *dead* closures in the closure
346 * ----------------------------------------------------------------------- */
348 processChainForDead( bdescr *bd )
350 // Any object still in the chain is dead!
352 processHeapClosureForDead((StgClosure *)bd->start);
357 /* --------------------------------------------------------------------------
358 * Start a census for *dead* closures, and calls
359 * processHeapClosureForDead() on every closure which died in the
360 * current garbage collection. This function is called from a garbage
361 * collector right before tidying up, when all dead closures are still
362 * stored in the heap and easy to identify. Generations 0 through N
363 * have just beed garbage collected.
364 * ----------------------------------------------------------------------- */
366 LdvCensusForDead( nat N )
370 // ldvTime == 0 means that LDV profiling is currently turned off.
374 if (RtsFlags.GcFlags.generations == 1) {
376 // Todo: support LDV for two-space garbage collection.
378 barf("Lag/Drag/Void profiling not supported with -G1");
380 for (g = 0; g <= N; g++)
381 for (s = 0; s < generations[g].n_steps; s++) {
382 if (g == 0 && s == 0) {
383 processSmallObjectPoolForDead();
384 processNurseryForDead();
385 processChainForDead(generations[g].steps[s].large_objects);
387 processHeapForDead(generations[g].steps[s].blocks);
388 processChainForDead(generations[g].steps[s].large_objects);
394 /* --------------------------------------------------------------------------
395 * Regard any closure in the current heap as dead or moribund and update
396 * LDV statistics accordingly.
397 * Called from shutdownHaskell() in RtsStartup.c.
398 * Also, stops LDV profiling by resetting ldvTime to 0.
399 * ----------------------------------------------------------------------- */
401 LdvCensusKillAll( void )
403 LdvCensusForDead(RtsFlags.GcFlags.generations - 1);
406 #endif /* PROFILING */