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