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