add Outputable instance for OccIfaceEq
[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_CLEAN:
100     case MVAR_DIRTY:
101     case MUT_ARR_PTRS_CLEAN:
102     case MUT_ARR_PTRS_DIRTY:
103     case MUT_ARR_PTRS_FROZEN:
104     case MUT_ARR_PTRS_FROZEN0:
105     case ARR_WORDS:
106     case WEAK:
107     case MUT_VAR_CLEAN:
108     case MUT_VAR_DIRTY:
109     case BCO:
110     case STABLE_NAME:
111     case TVAR_WATCH_QUEUE:
112     case TVAR:
113     case TREC_HEADER:
114     case TREC_CHUNK:
115     case INVARIANT_CHECK_QUEUE:
116     case ATOMIC_INVARIANT:
117         return size;
118
119         /*
120           ordinary cases: call LDV_recordDead().
121         */
122     case THUNK:
123     case THUNK_1_0:
124     case THUNK_0_1:
125     case THUNK_SELECTOR:
126     case THUNK_2_0:
127     case THUNK_1_1:
128     case THUNK_0_2:
129     case AP:
130     case PAP:
131     case AP_STACK:
132     case CONSTR:
133     case CONSTR_1_0:
134     case CONSTR_0_1:
135     case CONSTR_2_0:
136     case CONSTR_1_1:
137     case CONSTR_0_2:
138     case FUN:
139     case FUN_1_0:
140     case FUN_0_1:
141     case FUN_2_0:
142     case FUN_1_1:
143     case FUN_0_2:
144     case BLACKHOLE:
145     case SE_BLACKHOLE:
146     case CAF_BLACKHOLE:
147     case SE_CAF_BLACKHOLE:
148     case IND_PERM:
149     case IND_OLDGEN_PERM:
150         /*
151           'Ingore' cases
152         */
153         // Why can we ignore IND/IND_OLDGEN closures? We assume that
154         // any census is preceded by a major garbage collection, which
155         // IND/IND_OLDGEN closures cannot survive. Therefore, it is no
156         // use considering IND/IND_OLDGEN closures in the meanwhile
157         // because they will perish before the next census at any
158         // rate.
159     case IND:
160     case IND_OLDGEN:
161         // Found a dead closure: record its size
162         LDV_recordDead(c, size);
163         return size;
164
165         /*
166           Error case
167         */
168         // static objects
169     case IND_STATIC:
170     case CONSTR_STATIC:
171     case FUN_STATIC:
172     case THUNK_STATIC:
173     case CONSTR_NOCAF_STATIC:
174         // stack objects
175     case UPDATE_FRAME:
176     case CATCH_FRAME:
177     case STOP_FRAME:
178     case RET_DYN:
179     case RET_BCO:
180     case RET_SMALL:
181     case RET_BIG:
182         // others
183     case BLOCKED_FETCH:
184     case FETCH_ME:
185     case FETCH_ME_BQ:
186     case RBH:
187     case REMOTE_REF:
188     case INVALID_OBJECT:
189     default:
190         barf("Invalid object in processHeapClosureForDead(): %d", info->type);
191         return 0;
192     }
193 }
194
195 /* --------------------------------------------------------------------------
196  * Calls processHeapClosureForDead() on every *dead* closures in the
197  * heap blocks starting at bd.
198  * ----------------------------------------------------------------------- */
199 static void
200 processHeapForDead( bdescr *bd )
201 {
202     StgPtr p;
203
204     while (bd != NULL) {
205         p = bd->start;
206         while (p < bd->free) {
207             p += processHeapClosureForDead((StgClosure *)p);
208             while (p < bd->free && !*p)   // skip slop
209                 p++;
210         }
211         ASSERT(p == bd->free);
212         bd = bd->link;
213     }
214 }
215
216 /* --------------------------------------------------------------------------
217  * Calls processHeapClosureForDead() on every *dead* closures in the nursery.
218  * ----------------------------------------------------------------------- */
219 static void
220 processNurseryForDead( void )
221 {
222     StgPtr p, bdLimit;
223     bdescr *bd;
224
225     bd = MainCapability.r.rNursery->blocks;
226     while (bd->start < bd->free) {
227         p = bd->start;
228         bdLimit = bd->start + BLOCK_SIZE_W;
229         while (p < bd->free && p < bdLimit) {
230             p += processHeapClosureForDead((StgClosure *)p);
231             while (p < bd->free && p < bdLimit && !*p)  // skip slop
232                 p++;
233         }
234         bd = bd->link;
235         if (bd == NULL)
236             break;
237     }
238 }
239
240 /* --------------------------------------------------------------------------
241  * Calls processHeapClosureForDead() on every *dead* closures in the
242  * small object pool.
243  * ----------------------------------------------------------------------- */
244 static void
245 processSmallObjectPoolForDead( void )
246 {
247     bdescr *bd;
248     StgPtr p;
249
250     for (bd = g0s0->blocks; bd != NULL; bd = bd->link) {
251         p = bd->start;
252         while (p < bd->free) {
253             p += processHeapClosureForDead((StgClosure *)p);
254             while (p < bd->free && !*p)    // skip slop
255                 p++;
256         }
257         ASSERT(p == bd->free);
258     }
259 }
260
261 /* --------------------------------------------------------------------------
262  * Calls processHeapClosureForDead() on every *dead* closures in the closure
263  * chain.
264  * ----------------------------------------------------------------------- */
265 static void
266 processChainForDead( bdescr *bd )
267 {
268     // Any object still in the chain is dead!
269     while (bd != NULL) {
270         processHeapClosureForDead((StgClosure *)bd->start);
271         bd = bd->link;
272     }
273 }
274
275 /* --------------------------------------------------------------------------
276  * Start a census for *dead* closures, and calls
277  * processHeapClosureForDead() on every closure which died in the
278  * current garbage collection.  This function is called from a garbage
279  * collector right before tidying up, when all dead closures are still
280  * stored in the heap and easy to identify.  Generations 0 through N
281  * have just beed garbage collected.
282  * ----------------------------------------------------------------------- */
283 void
284 LdvCensusForDead( nat N )
285 {
286     nat g, s;
287
288     // ldvTime == 0 means that LDV profiling is currently turned off.
289     if (era == 0)
290         return;
291
292     if (RtsFlags.GcFlags.generations == 1) {
293         //
294         // Todo: support LDV for two-space garbage collection.
295         //
296         barf("Lag/Drag/Void profiling not supported with -G1");
297     } else {
298         for (g = 0; g <= N; g++)
299             for (s = 0; s < generations[g].n_steps; s++) {
300                 if (g == 0 && s == 0) {
301                     processSmallObjectPoolForDead();
302                     processNurseryForDead();
303                     processChainForDead(generations[g].steps[s].large_objects);
304                 } else{
305                     processHeapForDead(generations[g].steps[s].old_blocks);
306                     processChainForDead(generations[g].steps[s].large_objects);
307                 }
308             }
309     }
310 }
311
312 /* --------------------------------------------------------------------------
313  * Regard any closure in the current heap as dead or moribund and update
314  * LDV statistics accordingly.
315  * Called from shutdownHaskell() in RtsStartup.c.
316  * Also, stops LDV profiling by resetting ldvTime to 0.
317  * ----------------------------------------------------------------------- */
318 void
319 LdvCensusKillAll( void )
320 {
321     LdvCensusForDead(RtsFlags.GcFlags.generations - 1);
322 }
323
324 #endif /* PROFILING */