355d09d0287ee7e2f9c7e6fb97bdcc41c0bb1d98
[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 size, 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         // very like FILL_SLOP(), except that we call LDV_recordDead().
48         size = closure_sizeW(p);
49
50         LDV_recordDead((StgClosure *)(p), size);
51
52         if (size > sizeofW(StgThunkHeader)) {
53             for (i = 0; i < size - sizeofW(StgThunkHeader); i++) {
54                 ((StgThunk *)(p))->payload[i] = 0;
55             }
56         }
57     }
58 }
59
60 /* --------------------------------------------------------------------------
61  * This function is called eventually on every object destroyed during
62  * a garbage collection, whether it is a major garbage collection or
63  * not.  If c is an 'inherently used' closure, nothing happens.  If c
64  * is an ordinary closure, LDV_recordDead() is called on c with its
65  * proper size which excludes the profiling header portion in the
66  * closure.  Returns the size of the closure, including the profiling
67  * header portion, so that the caller can find the next closure.
68  * ----------------------------------------------------------------------- */
69 STATIC_INLINE nat
70 processHeapClosureForDead( StgClosure *c )
71 {
72     nat size;
73     StgInfoTable *info;
74
75     info = get_itbl(c);
76
77     if (info->type != EVACUATED) {
78         ASSERT(((LDVW(c) & LDV_CREATE_MASK) >> LDV_SHIFT) <= era &&
79                ((LDVW(c) & LDV_CREATE_MASK) >> LDV_SHIFT) > 0);
80         ASSERT(((LDVW(c) & LDV_STATE_MASK) == LDV_STATE_CREATE) ||
81                (
82                    (LDVW(c) & LDV_LAST_MASK) <= era &&
83                    (LDVW(c) & LDV_LAST_MASK) > 0
84                    ));
85     }
86
87     if (info->type == EVACUATED) {
88         // The size of the evacuated closure is currently stored in
89         // the LDV field.  See SET_EVACUAEE_FOR_LDV() in
90         // includes/StgLdvProf.h.
91         return LDVW(c);
92     }
93
94     size = closure_sizeW(c);
95
96     switch (info->type) {
97         /*
98           'inherently used' cases: do nothing.
99         */
100     case TSO:
101     case MVAR:
102     case MUT_ARR_PTRS_CLEAN:
103     case MUT_ARR_PTRS_DIRTY:
104     case MUT_ARR_PTRS_FROZEN:
105     case MUT_ARR_PTRS_FROZEN0:
106     case ARR_WORDS:
107     case WEAK:
108     case MUT_VAR_CLEAN:
109     case MUT_VAR_DIRTY:
110     case BCO:
111     case STABLE_NAME:
112         return size;
113
114         /*
115           ordinary cases: call LDV_recordDead().
116         */
117     case THUNK:
118     case THUNK_1_0:
119     case THUNK_0_1:
120     case THUNK_SELECTOR:
121     case THUNK_2_0:
122     case THUNK_1_1:
123     case THUNK_0_2:
124     case AP:
125     case PAP:
126     case AP_STACK:
127     case CONSTR:
128     case CONSTR_1_0:
129     case CONSTR_0_1:
130     case CONSTR_2_0:
131     case CONSTR_1_1:
132     case CONSTR_0_2:
133     case FUN:
134     case FUN_1_0:
135     case FUN_0_1:
136     case FUN_2_0:
137     case FUN_1_1:
138     case FUN_0_2:
139     case BLACKHOLE:
140     case SE_BLACKHOLE:
141     case CAF_BLACKHOLE:
142     case SE_CAF_BLACKHOLE:
143     case IND_PERM:
144     case IND_OLDGEN_PERM:
145         /*
146           'Ingore' cases
147         */
148         // Why can we ignore IND/IND_OLDGEN closures? We assume that
149         // any census is preceded by a major garbage collection, which
150         // IND/IND_OLDGEN closures cannot survive. Therefore, it is no
151         // use considering IND/IND_OLDGEN closures in the meanwhile
152         // because they will perish before the next census at any
153         // rate.
154     case IND:
155     case IND_OLDGEN:
156         // Found a dead closure: record its size
157         LDV_recordDead(c, size);
158         return size;
159
160         /*
161           Error case
162         */
163         // static objects
164     case IND_STATIC:
165     case CONSTR_STATIC:
166     case FUN_STATIC:
167     case THUNK_STATIC:
168     case CONSTR_INTLIKE:
169     case CONSTR_CHARLIKE:
170     case CONSTR_NOCAF_STATIC:
171         // stack objects
172     case UPDATE_FRAME:
173     case CATCH_FRAME:
174     case STOP_FRAME:
175     case RET_DYN:
176     case RET_BCO:
177     case RET_SMALL:
178     case RET_VEC_SMALL:
179     case RET_BIG:
180     case RET_VEC_BIG:
181         // others
182     case BLOCKED_FETCH:
183     case FETCH_ME:
184     case FETCH_ME_BQ:
185     case RBH:
186     case REMOTE_REF:
187     case INVALID_OBJECT:
188     default:
189         barf("Invalid object in processHeapClosureForDead(): %d", info->type);
190         return 0;
191     }
192 }
193
194 /* --------------------------------------------------------------------------
195  * Calls processHeapClosureForDead() on every *dead* closures in the
196  * heap blocks starting at bd.
197  * ----------------------------------------------------------------------- */
198 static void
199 processHeapForDead( bdescr *bd )
200 {
201     StgPtr p;
202
203     while (bd != NULL) {
204         p = bd->start;
205         while (p < bd->free) {
206             p += processHeapClosureForDead((StgClosure *)p);
207             while (p < bd->free && !*p)   // skip slop
208                 p++;
209         }
210         ASSERT(p == bd->free);
211         bd = bd->link;
212     }
213 }
214
215 /* --------------------------------------------------------------------------
216  * Calls processHeapClosureForDead() on every *dead* closures in the nursery.
217  * ----------------------------------------------------------------------- */
218 static void
219 processNurseryForDead( void )
220 {
221     StgPtr p, bdLimit;
222     bdescr *bd;
223
224     bd = MainCapability.r.rNursery->blocks;
225     while (bd->start < bd->free) {
226         p = bd->start;
227         bdLimit = bd->start + BLOCK_SIZE_W;
228         while (p < bd->free && p < bdLimit) {
229             p += processHeapClosureForDead((StgClosure *)p);
230             while (p < bd->free && p < bdLimit && !*p)  // skip slop
231                 p++;
232         }
233         bd = bd->link;
234         if (bd == NULL)
235             break;
236     }
237 }
238
239 /* --------------------------------------------------------------------------
240  * Calls processHeapClosureForDead() on every *dead* closures in the
241  * small object pool.
242  * ----------------------------------------------------------------------- */
243 static void
244 processSmallObjectPoolForDead( void )
245 {
246     bdescr *bd;
247     StgPtr p;
248
249     bd = small_alloc_list;
250
251     // first block
252     if (bd == NULL)
253         return;
254
255     p = bd->start;
256     while (p < alloc_Hp) {
257         p += processHeapClosureForDead((StgClosure *)p);
258         while (p < alloc_Hp && !*p)     // skip slop
259             p++;
260     }
261     ASSERT(p == alloc_Hp);
262
263     bd = bd->link;
264     while (bd != NULL) {
265         p = bd->start;
266         while (p < bd->free) {
267             p += processHeapClosureForDead((StgClosure *)p);
268             while (p < bd->free && !*p)    // skip slop
269                 p++;
270         }
271         ASSERT(p == bd->free);
272         bd = bd->link;
273     }
274 }
275
276 /* --------------------------------------------------------------------------
277  * Calls processHeapClosureForDead() on every *dead* closures in the closure
278  * chain.
279  * ----------------------------------------------------------------------- */
280 static void
281 processChainForDead( bdescr *bd )
282 {
283     // Any object still in the chain is dead!
284     while (bd != NULL) {
285         processHeapClosureForDead((StgClosure *)bd->start);
286         bd = bd->link;
287     }
288 }
289
290 /* --------------------------------------------------------------------------
291  * Start a census for *dead* closures, and calls
292  * processHeapClosureForDead() on every closure which died in the
293  * current garbage collection.  This function is called from a garbage
294  * collector right before tidying up, when all dead closures are still
295  * stored in the heap and easy to identify.  Generations 0 through N
296  * have just beed garbage collected.
297  * ----------------------------------------------------------------------- */
298 void
299 LdvCensusForDead( nat N )
300 {
301     nat g, s;
302
303     // ldvTime == 0 means that LDV profiling is currently turned off.
304     if (era == 0)
305         return;
306
307     if (RtsFlags.GcFlags.generations == 1) {
308         //
309         // Todo: support LDV for two-space garbage collection.
310         //
311         barf("Lag/Drag/Void profiling not supported with -G1");
312     } else {
313         for (g = 0; g <= N; g++)
314             for (s = 0; s < generations[g].n_steps; s++) {
315                 if (g == 0 && s == 0) {
316                     processSmallObjectPoolForDead();
317                     processNurseryForDead();
318                     processChainForDead(generations[g].steps[s].large_objects);
319                 } else{
320                     processHeapForDead(generations[g].steps[s].blocks);
321                     processChainForDead(generations[g].steps[s].large_objects);
322                 }
323             }
324     }
325 }
326
327 /* --------------------------------------------------------------------------
328  * Regard any closure in the current heap as dead or moribund and update
329  * LDV statistics accordingly.
330  * Called from shutdownHaskell() in RtsStartup.c.
331  * Also, stops LDV profiling by resetting ldvTime to 0.
332  * ----------------------------------------------------------------------- */
333 void
334 LdvCensusKillAll( void )
335 {
336     LdvCensusForDead(RtsFlags.GcFlags.generations - 1);
337 }
338
339 #endif /* PROFILING */