[project @ 2005-02-10 13:01:52 by simonmar]
[ghc-hetmet.git] / ghc / rts / LdvProfile.c
1 /* -----------------------------------------------------------------------------
2  *
3  * (c) The GHC Team, 2001
4  * Author: Sungwoo Park
5  *
6  * Lag/Drag/Void profiling.
7  *
8  * ---------------------------------------------------------------------------*/
9
10 #ifdef PROFILING
11
12 #include "Rts.h"
13 #include "LdvProfile.h"
14 #include "RtsFlags.h"
15 #include "Profiling.h"
16 #include "Stats.h"
17 #include "Storage.h"
18 #include "RtsUtils.h"
19 #include "Schedule.h"
20
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
24  * fills in the slop.
25  * 
26  *  Invoked when:
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).
32  * 
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  *  ----------------------------------------------------------------------- */
36 void 
37 LDV_recordDead_FILL_SLOP_DYNAMIC( StgClosure *p )
38 {
39     StgInfoTable *info;
40     nat nw, i;
41
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
44 #endif
45
46     if (era > 0) {
47         info = get_itbl((p));
48         switch (info->type) {
49         case THUNK_1_0:
50         case THUNK_0_1:
51         case THUNK_2_0:
52         case THUNK_1_1:
53         case THUNK_0_2:
54         case THUNK_SELECTOR:
55             nw = MIN_UPD_SIZE;
56             break;
57         case THUNK:
58             nw = info->layout.payload.ptrs + info->layout.payload.nptrs;
59             if (nw < MIN_UPD_SIZE)
60                 nw = MIN_UPD_SIZE;
61             break;
62         case AP:
63             nw = sizeofW(StgPAP) - sizeofW(StgHeader) + ((StgPAP *)p)->n_args;
64             break;
65         case AP_STACK:
66             nw = sizeofW(StgAP_STACK) - sizeofW(StgHeader)
67                 + ((StgAP_STACK *)p)->size;
68             break;
69         case CAF_BLACKHOLE:
70         case BLACKHOLE:
71         case SE_BLACKHOLE:
72         case SE_CAF_BLACKHOLE:
73             nw = info->layout.payload.ptrs + info->layout.payload.nptrs;
74             break;
75         default:
76             barf("Unexpected closure type %u in LDV_recordDead_FILL_SLOP_DYNAMIC()", info->type);
77             break;
78         }
79         LDV_recordDead((StgClosure *)(p), nw + sizeofW(StgHeader));
80         for (i = 0; i < nw; i++) {
81             ((StgClosure *)(p))->payload[i] = 0;
82         }
83     }
84 }
85
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  * ----------------------------------------------------------------------- */
95 STATIC_INLINE nat
96 processHeapClosureForDead( StgClosure *c )
97 {
98     nat size;
99     StgInfoTable *info;
100
101     info = get_itbl(c);
102
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) ||
107                (
108                    (LDVW(c) & LDV_LAST_MASK) <= era &&
109                    (LDVW(c) & LDV_LAST_MASK) > 0
110                    ));
111     }
112
113     switch (info->type) {
114         /*
115           'inherently used' cases: do nothing.
116         */
117
118     case TSO:
119         size = tso_sizeW((StgTSO *)c);
120         return size;
121
122     case MVAR:
123         size = sizeofW(StgMVar);
124         return size;
125
126     case MUT_ARR_PTRS:
127     case MUT_ARR_PTRS_FROZEN:
128         size = mut_arr_ptrs_sizeW((StgMutArrPtrs *)c);
129         return size;
130
131     case ARR_WORDS:
132         size = arr_words_sizeW((StgArrWords *)c);
133         return size;
134
135     case WEAK:
136     case MUT_VAR:
137     case FOREIGN:
138     case BCO:
139     case STABLE_NAME:
140         size = sizeW_fromITBL(info);
141         return size;
142
143         /*
144           ordinary cases: call LDV_recordDead().
145         */
146
147     case THUNK:
148         size = stg_max(sizeW_fromITBL(info), sizeofW(StgHeader) + MIN_UPD_SIZE);
149         break;
150
151     case THUNK_1_0:
152     case THUNK_0_1:
153     case THUNK_2_0:
154     case THUNK_1_1:
155     case THUNK_0_2:
156     case THUNK_SELECTOR:
157         size = sizeofW(StgHeader) + MIN_UPD_SIZE;
158         break;
159
160     case AP:
161     case PAP:
162         size = pap_sizeW((StgPAP *)c);
163         break;
164
165     case AP_STACK:
166         size = ap_stack_sizeW((StgAP_STACK *)c);
167         break;
168
169     case CONSTR:
170     case CONSTR_1_0:
171     case CONSTR_0_1:
172     case CONSTR_2_0:
173     case CONSTR_1_1:
174     case CONSTR_0_2:
175
176     case FUN:
177     case FUN_1_0:
178     case FUN_0_1:
179     case FUN_2_0:
180     case FUN_1_1:
181     case FUN_0_2:
182
183     case BLACKHOLE_BQ:
184     case BLACKHOLE:
185     case SE_BLACKHOLE:
186     case CAF_BLACKHOLE:
187     case SE_CAF_BLACKHOLE:
188         size = sizeW_fromITBL(info);
189         break;
190
191     case IND_PERM:
192     case IND_OLDGEN_PERM:
193         size = sizeofW(StgInd);
194         break;
195
196         /*
197           'Ingore' cases
198         */
199         // Why can we ignore IND/IND_OLDGEN closures? We assume that
200         // any census is preceded by a major garbage collection, which
201         // IND/IND_OLDGEN closures cannot survive. Therefore, it is no
202         // use considering IND/IND_OLDGEN closures in the meanwhile
203         // because they will perish before the next census at any
204         // rate.
205     case IND:
206     case IND_OLDGEN:
207         size = sizeofW(StgInd);
208         return size;
209
210     case EVACUATED:
211         // The size of the evacuated closure is currently stored in
212         // the LDV field.  See SET_EVACUAEE_FOR_LDV() in
213         // includes/StgLdvProf.h.
214         return LDVW(c);
215
216         /*
217           Error case
218         */
219         // static objects
220     case IND_STATIC:
221     case CONSTR_STATIC:
222     case FUN_STATIC:
223     case THUNK_STATIC:
224     case CONSTR_INTLIKE:
225     case CONSTR_CHARLIKE:
226     case CONSTR_NOCAF_STATIC:
227         // stack objects
228     case UPDATE_FRAME:
229     case CATCH_FRAME:
230     case STOP_FRAME:
231     case RET_DYN:
232     case RET_BCO:
233     case RET_SMALL:
234     case RET_VEC_SMALL:
235     case RET_BIG:
236     case RET_VEC_BIG:
237         // others
238     case BLOCKED_FETCH:
239     case FETCH_ME:
240     case FETCH_ME_BQ:
241     case RBH:
242     case REMOTE_REF:
243     case INVALID_OBJECT:
244     default:
245         barf("Invalid object in processHeapClosureForDead(): %d", info->type);
246         return 0;
247     }
248
249     // Found a dead closure: record its size
250     LDV_recordDead(c, size);
251     return size;
252 }
253
254 /* --------------------------------------------------------------------------
255  * Calls processHeapClosureForDead() on every *dead* closures in the
256  * heap blocks starting at bd.
257  * ----------------------------------------------------------------------- */
258 static void
259 processHeapForDead( bdescr *bd )
260 {
261     StgPtr p;
262
263     while (bd != NULL) {
264         p = bd->start;
265         while (p < bd->free) {
266             p += processHeapClosureForDead((StgClosure *)p);
267             while (p < bd->free && !*p)   // skip slop
268                 p++;
269         }
270         ASSERT(p == bd->free);
271         bd = bd->link;
272     }
273 }
274
275 /* --------------------------------------------------------------------------
276  * Calls processHeapClosureForDead() on every *dead* closures in the nursery.
277  * ----------------------------------------------------------------------- */
278 static void
279 processNurseryForDead( void )
280 {
281     StgPtr p, bdLimit;
282     bdescr *bd;
283
284     bd = MainCapability.r.rNursery;
285     while (bd->start < bd->free) {
286         p = bd->start;
287         bdLimit = bd->start + BLOCK_SIZE_W;
288         while (p < bd->free && p < bdLimit) {
289             p += processHeapClosureForDead((StgClosure *)p);
290             while (p < bd->free && p < bdLimit && !*p)  // skip slop
291                 p++;
292         }
293         bd = bd->link;
294         if (bd == NULL)
295             break;
296     }
297 }
298
299 /* --------------------------------------------------------------------------
300  * Calls processHeapClosureForDead() on every *dead* closures in the
301  * small object pool.
302  * ----------------------------------------------------------------------- */
303 static void
304 processSmallObjectPoolForDead( void )
305 {
306     bdescr *bd;
307     StgPtr p;
308
309     bd = small_alloc_list;
310
311     // first block
312     if (bd == NULL)
313         return;
314
315     p = bd->start;
316     while (p < alloc_Hp) {
317         p += processHeapClosureForDead((StgClosure *)p);
318         while (p < alloc_Hp && !*p)     // skip slop
319             p++;
320     }
321     ASSERT(p == alloc_Hp);
322
323     bd = bd->link;
324     while (bd != NULL) {
325         p = bd->start;
326         while (p < bd->free) {
327             p += processHeapClosureForDead((StgClosure *)p);
328             while (p < bd->free && !*p)    // skip slop
329                 p++;
330         }
331         ASSERT(p == bd->free);
332         bd = bd->link;
333     }
334 }
335
336 /* --------------------------------------------------------------------------
337  * Calls processHeapClosureForDead() on every *dead* closures in the closure
338  * chain.
339  * ----------------------------------------------------------------------- */
340 static void
341 processChainForDead( bdescr *bd )
342 {
343     // Any object still in the chain is dead!
344     while (bd != NULL) {
345         processHeapClosureForDead((StgClosure *)bd->start);
346         bd = bd->link;
347     }
348 }
349
350 /* --------------------------------------------------------------------------
351  * Start a census for *dead* closures, and calls
352  * processHeapClosureForDead() on every closure which died in the
353  * current garbage collection.  This function is called from a garbage
354  * collector right before tidying up, when all dead closures are still
355  * stored in the heap and easy to identify.  Generations 0 through N
356  * have just beed garbage collected.
357  * ----------------------------------------------------------------------- */
358 void
359 LdvCensusForDead( nat N )
360 {
361     nat g, s;
362
363     // ldvTime == 0 means that LDV profiling is currently turned off.
364     if (era == 0)
365         return;
366
367     if (RtsFlags.GcFlags.generations == 1) {
368         //
369         // Todo: support LDV for two-space garbage collection.
370         //
371         barf("Lag/Drag/Void profiling not supported with -G1");
372     } else {
373         for (g = 0; g <= N; g++)
374             for (s = 0; s < generations[g].n_steps; s++) {
375                 if (g == 0 && s == 0) {
376                     processSmallObjectPoolForDead();
377                     processNurseryForDead();
378                     processChainForDead(generations[g].steps[s].large_objects);
379                 } else{
380                     processHeapForDead(generations[g].steps[s].blocks);
381                     processChainForDead(generations[g].steps[s].large_objects);
382                 }
383             }
384     }
385 }
386
387 /* --------------------------------------------------------------------------
388  * Regard any closure in the current heap as dead or moribund and update
389  * LDV statistics accordingly.
390  * Called from shutdownHaskell() in RtsStartup.c.
391  * Also, stops LDV profiling by resetting ldvTime to 0.
392  * ----------------------------------------------------------------------- */
393 void
394 LdvCensusKillAll( void )
395 {
396     LdvCensusForDead(RtsFlags.GcFlags.generations - 1);
397 }
398
399 #endif /* PROFILING */