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