[project @ 2006-01-17 16:13:18 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_CLEAN:
142     case MUT_VAR_DIRTY:
143     case BCO:
144     case STABLE_NAME:
145         size = sizeW_fromITBL(info);
146         return size;
147
148         /*
149           ordinary cases: call LDV_recordDead().
150         */
151
152     case THUNK:
153         size = stg_max(sizeW_fromITBL(info), sizeofW(StgHeader) + MIN_UPD_SIZE);
154         break;
155
156     case THUNK_1_0:
157     case THUNK_0_1:
158     case THUNK_SELECTOR:
159         size = sizeofW(StgHeader) + stg_max(MIN_UPD_SIZE, 1);
160         break;
161
162     case THUNK_2_0:
163     case THUNK_1_1:
164     case THUNK_0_2:
165         size = sizeofW(StgHeader) + stg_max(MIN_UPD_SIZE, 2);
166         break;
167
168     case AP:
169         size = ap_sizeW((StgAP *)c);
170         break;
171
172     case PAP:
173         size = pap_sizeW((StgPAP *)c);
174         break;
175
176     case AP_STACK:
177         size = ap_stack_sizeW((StgAP_STACK *)c);
178         break;
179
180     case CONSTR:
181     case CONSTR_1_0:
182     case CONSTR_0_1:
183     case CONSTR_2_0:
184     case CONSTR_1_1:
185     case CONSTR_0_2:
186
187     case FUN:
188     case FUN_1_0:
189     case FUN_0_1:
190     case FUN_2_0:
191     case FUN_1_1:
192     case FUN_0_2:
193
194     case BLACKHOLE:
195     case SE_BLACKHOLE:
196     case CAF_BLACKHOLE:
197     case SE_CAF_BLACKHOLE:
198         size = sizeW_fromITBL(info);
199         break;
200
201     case IND_PERM:
202     case IND_OLDGEN_PERM:
203         size = sizeofW(StgInd);
204         break;
205
206         /*
207           'Ingore' cases
208         */
209         // Why can we ignore IND/IND_OLDGEN closures? We assume that
210         // any census is preceded by a major garbage collection, which
211         // IND/IND_OLDGEN closures cannot survive. Therefore, it is no
212         // use considering IND/IND_OLDGEN closures in the meanwhile
213         // because they will perish before the next census at any
214         // rate.
215     case IND:
216     case IND_OLDGEN:
217         size = sizeofW(StgInd);
218         return size;
219
220     case EVACUATED:
221         // The size of the evacuated closure is currently stored in
222         // the LDV field.  See SET_EVACUAEE_FOR_LDV() in
223         // includes/StgLdvProf.h.
224         return LDVW(c);
225
226         /*
227           Error case
228         */
229         // static objects
230     case IND_STATIC:
231     case CONSTR_STATIC:
232     case FUN_STATIC:
233     case THUNK_STATIC:
234     case CONSTR_INTLIKE:
235     case CONSTR_CHARLIKE:
236     case CONSTR_NOCAF_STATIC:
237         // stack objects
238     case UPDATE_FRAME:
239     case CATCH_FRAME:
240     case STOP_FRAME:
241     case RET_DYN:
242     case RET_BCO:
243     case RET_SMALL:
244     case RET_VEC_SMALL:
245     case RET_BIG:
246     case RET_VEC_BIG:
247         // others
248     case BLOCKED_FETCH:
249     case FETCH_ME:
250     case FETCH_ME_BQ:
251     case RBH:
252     case REMOTE_REF:
253     case INVALID_OBJECT:
254     default:
255         barf("Invalid object in processHeapClosureForDead(): %d", info->type);
256         return 0;
257     }
258
259     // Found a dead closure: record its size
260     LDV_recordDead(c, size);
261     return size;
262 }
263
264 /* --------------------------------------------------------------------------
265  * Calls processHeapClosureForDead() on every *dead* closures in the
266  * heap blocks starting at bd.
267  * ----------------------------------------------------------------------- */
268 static void
269 processHeapForDead( bdescr *bd )
270 {
271     StgPtr p;
272
273     while (bd != NULL) {
274         p = bd->start;
275         while (p < bd->free) {
276             p += processHeapClosureForDead((StgClosure *)p);
277             while (p < bd->free && !*p)   // skip slop
278                 p++;
279         }
280         ASSERT(p == bd->free);
281         bd = bd->link;
282     }
283 }
284
285 /* --------------------------------------------------------------------------
286  * Calls processHeapClosureForDead() on every *dead* closures in the nursery.
287  * ----------------------------------------------------------------------- */
288 static void
289 processNurseryForDead( void )
290 {
291     StgPtr p, bdLimit;
292     bdescr *bd;
293
294     bd = MainCapability.r.rNursery->blocks;
295     while (bd->start < bd->free) {
296         p = bd->start;
297         bdLimit = bd->start + BLOCK_SIZE_W;
298         while (p < bd->free && p < bdLimit) {
299             p += processHeapClosureForDead((StgClosure *)p);
300             while (p < bd->free && p < bdLimit && !*p)  // skip slop
301                 p++;
302         }
303         bd = bd->link;
304         if (bd == NULL)
305             break;
306     }
307 }
308
309 /* --------------------------------------------------------------------------
310  * Calls processHeapClosureForDead() on every *dead* closures in the
311  * small object pool.
312  * ----------------------------------------------------------------------- */
313 static void
314 processSmallObjectPoolForDead( void )
315 {
316     bdescr *bd;
317     StgPtr p;
318
319     bd = small_alloc_list;
320
321     // first block
322     if (bd == NULL)
323         return;
324
325     p = bd->start;
326     while (p < alloc_Hp) {
327         p += processHeapClosureForDead((StgClosure *)p);
328         while (p < alloc_Hp && !*p)     // skip slop
329             p++;
330     }
331     ASSERT(p == alloc_Hp);
332
333     bd = bd->link;
334     while (bd != NULL) {
335         p = bd->start;
336         while (p < bd->free) {
337             p += processHeapClosureForDead((StgClosure *)p);
338             while (p < bd->free && !*p)    // skip slop
339                 p++;
340         }
341         ASSERT(p == bd->free);
342         bd = bd->link;
343     }
344 }
345
346 /* --------------------------------------------------------------------------
347  * Calls processHeapClosureForDead() on every *dead* closures in the closure
348  * chain.
349  * ----------------------------------------------------------------------- */
350 static void
351 processChainForDead( bdescr *bd )
352 {
353     // Any object still in the chain is dead!
354     while (bd != NULL) {
355         processHeapClosureForDead((StgClosure *)bd->start);
356         bd = bd->link;
357     }
358 }
359
360 /* --------------------------------------------------------------------------
361  * Start a census for *dead* closures, and calls
362  * processHeapClosureForDead() on every closure which died in the
363  * current garbage collection.  This function is called from a garbage
364  * collector right before tidying up, when all dead closures are still
365  * stored in the heap and easy to identify.  Generations 0 through N
366  * have just beed garbage collected.
367  * ----------------------------------------------------------------------- */
368 void
369 LdvCensusForDead( nat N )
370 {
371     nat g, s;
372
373     // ldvTime == 0 means that LDV profiling is currently turned off.
374     if (era == 0)
375         return;
376
377     if (RtsFlags.GcFlags.generations == 1) {
378         //
379         // Todo: support LDV for two-space garbage collection.
380         //
381         barf("Lag/Drag/Void profiling not supported with -G1");
382     } else {
383         for (g = 0; g <= N; g++)
384             for (s = 0; s < generations[g].n_steps; s++) {
385                 if (g == 0 && s == 0) {
386                     processSmallObjectPoolForDead();
387                     processNurseryForDead();
388                     processChainForDead(generations[g].steps[s].large_objects);
389                 } else{
390                     processHeapForDead(generations[g].steps[s].blocks);
391                     processChainForDead(generations[g].steps[s].large_objects);
392                 }
393             }
394     }
395 }
396
397 /* --------------------------------------------------------------------------
398  * Regard any closure in the current heap as dead or moribund and update
399  * LDV statistics accordingly.
400  * Called from shutdownHaskell() in RtsStartup.c.
401  * Also, stops LDV profiling by resetting ldvTime to 0.
402  * ----------------------------------------------------------------------- */
403 void
404 LdvCensusKillAll( void )
405 {
406     LdvCensusForDead(RtsFlags.GcFlags.generations - 1);
407 }
408
409 #endif /* PROFILING */