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