c98a47e010b562bd5ad42015090b7b7e44b9b4a3
[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:
130     case MUT_ARR_PTRS_FROZEN:
131     case MUT_ARR_PTRS_FROZEN0:
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 BCO:
142     case STABLE_NAME:
143         size = sizeW_fromITBL(info);
144         return size;
145
146         /*
147           ordinary cases: call LDV_recordDead().
148         */
149
150     case THUNK:
151         size = stg_max(sizeW_fromITBL(info), sizeofW(StgHeader) + MIN_UPD_SIZE);
152         break;
153
154     case THUNK_1_0:
155     case THUNK_0_1:
156     case THUNK_SELECTOR:
157         size = sizeofW(StgHeader) + stg_max(MIN_UPD_SIZE, 1);
158         break;
159
160     case THUNK_2_0:
161     case THUNK_1_1:
162     case THUNK_0_2:
163         size = sizeofW(StgHeader) + stg_max(MIN_UPD_SIZE, 2);
164         break;
165
166     case AP:
167         size = ap_sizeW((StgAP *)c);
168         break;
169
170     case PAP:
171         size = pap_sizeW((StgPAP *)c);
172         break;
173
174     case AP_STACK:
175         size = ap_stack_sizeW((StgAP_STACK *)c);
176         break;
177
178     case CONSTR:
179     case CONSTR_1_0:
180     case CONSTR_0_1:
181     case CONSTR_2_0:
182     case CONSTR_1_1:
183     case CONSTR_0_2:
184
185     case FUN:
186     case FUN_1_0:
187     case FUN_0_1:
188     case FUN_2_0:
189     case FUN_1_1:
190     case FUN_0_2:
191
192     case BLACKHOLE:
193     case SE_BLACKHOLE:
194     case CAF_BLACKHOLE:
195     case SE_CAF_BLACKHOLE:
196         size = sizeW_fromITBL(info);
197         break;
198
199     case IND_PERM:
200     case IND_OLDGEN_PERM:
201         size = sizeofW(StgInd);
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     case IND_OLDGEN:
215         size = sizeofW(StgInd);
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->blocks;
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 */