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