[project @ 2004-02-12 02:04:59 by mthomas]
[ghc-hetmet.git] / ghc / rts / LdvProfile.c
1 /* -----------------------------------------------------------------------------
2  * $Id: LdvProfile.c,v 1.6 2003/11/12 17:49:08 sof Exp $
3  *
4  * (c) The GHC Team, 2001
5  * Author: Sungwoo Park
6  *
7  * Lag/Drag/Void profiling.
8  *
9  * ---------------------------------------------------------------------------*/
10
11 #ifdef PROFILING
12
13 #include "Stg.h"
14 #include "Rts.h"
15 #include "LdvProfile.h"
16 #include "RtsFlags.h"
17 #include "Profiling.h"
18 #include "Stats.h"
19 #include "Storage.h"
20 #include "RtsUtils.h"
21 #include "Schedule.h"
22
23 /* --------------------------------------------------------------------------
24  * Fills in the slop when a *dynamic* closure changes its type.
25  * First calls LDV_recordDead() to declare the closure is dead, and then
26  * fills in the slop.
27  * 
28  *  Invoked when:
29  *    1) blackholing, UPD_BH_UPDATABLE() and UPD_BH_SINGLE_ENTRY (in
30  *       includes/StgMacros.h), threadLazyBlackHole() and 
31  *       threadSqueezeStack() (in GC.c).
32  *    2) updating with indirection closures, updateWithIndirection() 
33  *       and updateWithPermIndirection() (in Storage.h).
34  * 
35  *  LDV_recordDead_FILL_SLOP_DYNAMIC() is not called on 'inherently used' 
36  *  closures such as TSO. It is not called on PAP because PAP is not updatable.
37  *  ----------------------------------------------------------------------- */
38 void 
39 LDV_recordDead_FILL_SLOP_DYNAMIC( StgClosure *p )
40 {
41     StgInfoTable *info;
42     nat nw, i;
43
44 #if defined(__GNUC__) && __GNUC__ < 3 && defined(DEBUG)
45 #error Please use gcc 3.0+ to compile this file with DEBUG; gcc < 3.0 miscompiles it
46 #endif
47
48     if (era > 0) {
49         info = get_itbl((p));
50         switch (info->type) {
51         case THUNK_1_0:
52         case THUNK_0_1:
53         case THUNK_2_0:
54         case THUNK_1_1:
55         case THUNK_0_2:
56         case THUNK_SELECTOR:
57             nw = MIN_UPD_SIZE;
58             break;
59         case THUNK:
60             nw = info->layout.payload.ptrs + info->layout.payload.nptrs;
61             if (nw < MIN_UPD_SIZE)
62                 nw = MIN_UPD_SIZE;
63             break;
64         case AP:
65             nw = sizeofW(StgPAP) - sizeofW(StgHeader) + ((StgPAP *)p)->n_args;
66             break;
67         case AP_STACK:
68             nw = sizeofW(StgAP_STACK) - sizeofW(StgHeader)
69                 + ((StgAP_STACK *)p)->size;
70             break;
71         case CAF_BLACKHOLE:
72         case BLACKHOLE:
73         case SE_BLACKHOLE:
74         case SE_CAF_BLACKHOLE:
75             nw = info->layout.payload.ptrs + info->layout.payload.nptrs;
76             break;
77         default:
78             barf("Unexpected closure type %u in LDV_recordDead_FILL_SLOP_DYNAMIC()", info->type);
79             break;
80         }
81         LDV_recordDead((StgClosure *)(p), nw + sizeofW(StgHeader));
82         for (i = 0; i < nw; i++) {
83             ((StgClosure *)(p))->payload[i] = 0;
84         }
85     }
86 }
87
88 /* --------------------------------------------------------------------------
89  * This function is called eventually on every object destroyed during
90  * a garbage collection, whether it is a major garbage collection or
91  * not.  If c is an 'inherently used' closure, nothing happens.  If c
92  * is an ordinary closure, LDV_recordDead() is called on c with its
93  * proper size which excludes the profiling header portion in the
94  * closure.  Returns the size of the closure, including the profiling
95  * header portion, so that the caller can find the next closure.
96  * ----------------------------------------------------------------------- */
97 STATIC_INLINE nat
98 processHeapClosureForDead( StgClosure *c )
99 {
100     nat size;
101     StgInfoTable *info;
102
103     info = get_itbl(c);
104
105     if (info->type != EVACUATED) {
106         ASSERT(((LDVW(c) & LDV_CREATE_MASK) >> LDV_SHIFT) <= era &&
107                ((LDVW(c) & LDV_CREATE_MASK) >> LDV_SHIFT) > 0);
108         ASSERT(((LDVW(c) & LDV_STATE_MASK) == LDV_STATE_CREATE) ||
109                (
110                    (LDVW(c) & LDV_LAST_MASK) <= era &&
111                    (LDVW(c) & LDV_LAST_MASK) > 0
112                    ));
113     }
114
115     switch (info->type) {
116         /*
117           'inherently used' cases: do nothing.
118         */
119
120     case TSO:
121         size = tso_sizeW((StgTSO *)c);
122         return size;
123
124     case MVAR:
125         size = sizeofW(StgMVar);
126         return size;
127
128     case MUT_ARR_PTRS:
129     case MUT_ARR_PTRS_FROZEN:
130         size = mut_arr_ptrs_sizeW((StgMutArrPtrs *)c);
131         return size;
132
133     case ARR_WORDS:
134         size = arr_words_sizeW((StgArrWords *)c);
135         return size;
136
137     case WEAK:
138     case MUT_VAR:
139     case MUT_CONS:
140     case FOREIGN:
141     case BCO:
142     case STABLE_NAME:
143         size = sizeW_fromITBL(info);
144         return size;
145
146         /*
147           ordinary cases: call LDV_recordDead().
148         */
149
150     case THUNK:
151         size = stg_max(sizeW_fromITBL(info), sizeofW(StgHeader) + MIN_UPD_SIZE);
152         break;
153
154     case THUNK_1_0:
155     case THUNK_0_1:
156     case THUNK_2_0:
157     case THUNK_1_1:
158     case THUNK_0_2:
159     case THUNK_SELECTOR:
160         size = sizeofW(StgHeader) + MIN_UPD_SIZE;
161         break;
162
163     case AP:
164     case PAP:
165         size = pap_sizeW((StgPAP *)c);
166         break;
167
168     case AP_STACK:
169         size = ap_stack_sizeW((StgAP_STACK *)c);
170         break;
171
172     case CONSTR:
173     case CONSTR_1_0:
174     case CONSTR_0_1:
175     case CONSTR_2_0:
176     case CONSTR_1_1:
177     case CONSTR_0_2:
178
179     case FUN:
180     case FUN_1_0:
181     case FUN_0_1:
182     case FUN_2_0:
183     case FUN_1_1:
184     case FUN_0_2:
185
186     case BLACKHOLE_BQ:
187     case BLACKHOLE:
188     case SE_BLACKHOLE:
189     case CAF_BLACKHOLE:
190     case SE_CAF_BLACKHOLE:
191         size = sizeW_fromITBL(info);
192         break;
193
194     case IND_PERM:
195         size = sizeofW(StgInd);
196         break;
197
198     case IND_OLDGEN_PERM:
199         size = sizeofW(StgIndOldGen);
200         break;
201
202         /*
203           'Ingore' cases
204         */
205         // Why can we ignore IND/IND_OLDGEN closures? We assume that
206         // any census is preceded by a major garbage collection, which
207         // IND/IND_OLDGEN closures cannot survive. Therefore, it is no
208         // use considering IND/IND_OLDGEN closures in the meanwhile
209         // because they will perish before the next census at any
210         // rate.
211     case IND:
212         size = sizeofW(StgInd);
213         return size;
214
215     case IND_OLDGEN:
216         size = sizeofW(StgIndOldGen);
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;
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 */