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