[project @ 2001-11-26 16:54:21 by simonmar]
[ghc-hetmet.git] / ghc / rts / LdvProfile.c
1 /* -----------------------------------------------------------------------------
2  * $Id: LdvProfile.c,v 1.2 2001/11/26 16:54:21 simonmar Exp $
3  *
4  * (c) The GHC Team, 2001
5  * Author: Sungwoo Park
6  *
7  * Lag/Drag/Void profiling.
8  *
9  * ---------------------------------------------------------------------------*/
10
11 #ifdef PROFILING
12
13 #include "Stg.h"
14 #include "Rts.h"
15 #include "LdvProfile.h"
16 #include "RtsFlags.h"
17 #include "Itimer.h"
18 #include "Proftimer.h"
19 #include "Profiling.h"
20 #include "Stats.h"
21 #include "Storage.h"
22 #include "RtsUtils.h"
23 #include "Schedule.h"
24
25 // ldvTimeSave is set in LdvCensusKillAll(), and stores the final number of
26 // times that LDV profiling was proformed.
27 static nat ldvTimeSave;
28
29 /* --------------------------------------------------------------------------
30  * Fills in the slop when a *dynamic* closure changes its type.
31  * First calls LDV_recordDead() to declare the closure is dead, and then
32  * fills in the slop.
33  * 
34  *  Invoked when:
35  *    1) blackholing, UPD_BH_UPDATABLE() and UPD_BH_SINGLE_ENTRY (in
36  *       includes/StgMacros.h), threadLazyBlackHole() and 
37  *       threadSqueezeStack() (in GC.c).
38  *    2) updating with indirection closures, updateWithIndirection() 
39  *       and updateWithPermIndirection() (in Storage.h).
40  * 
41  *  LDV_recordDead_FILL_SLOP_DYNAMIC() is not called on 'inherently used' 
42  *  closures such as TSO. It is not called on PAP because PAP is not updatable.
43  *  ----------------------------------------------------------------------- */
44 void 
45 LDV_recordDead_FILL_SLOP_DYNAMIC( StgClosure *p )
46 {
47     if (era > 0) {
48         StgInfoTable *inf = get_itbl((p));
49         nat nw, i;
50         switch (inf->type) {
51         case THUNK_1_0:
52         case THUNK_0_1:
53         case THUNK_2_0:
54         case THUNK_1_1:
55         case THUNK_0_2:
56         case THUNK_SELECTOR:
57             nw = MIN_UPD_SIZE;
58             break;
59         case THUNK:
60             nw = inf->layout.payload.ptrs + inf->layout.payload.nptrs;
61             if (nw < MIN_UPD_SIZE)
62                 nw = MIN_UPD_SIZE;
63             break;
64         case AP_UPD:
65             nw = sizeofW(StgPAP) - sizeofW(StgHeader) + ((StgPAP *)p)->n_args;
66             break;
67         case CAF_BLACKHOLE:
68         case BLACKHOLE:
69         case SE_BLACKHOLE:
70         case SE_CAF_BLACKHOLE:
71             nw = inf->layout.payload.ptrs + inf->layout.payload.nptrs;
72             break;
73         default:
74             barf("Unexpected closure type %u in LDV_recordDead_FILL_SLOP_DYNAMIC()", inf->type);
75             break;
76         }
77         LDV_recordDead((StgClosure *)(p), nw + sizeofW(StgHeader));
78         for (i = 0; i < nw; i++) {
79             ((StgClosure *)(p))->payload[i] = 0;
80         }
81     }
82 }
83
84 /* --------------------------------------------------------------------------
85  * This function is called eventually on every object destroyed during
86  * a garbage collection, whether it is a major garbage collection or
87  * not.  If c is an 'inherently used' closure, nothing happens.  If c
88  * is an ordinary closure, LDV_recordDead() is called on c with its
89  * proper size which excludes the profiling header portion in the
90  * closure.  Returns the size of the closure, including the profiling
91  * header portion, so that the caller can find the next closure.
92  * ----------------------------------------------------------------------- */
93 static inline nat
94 processHeapClosureForDead( StgClosure *c )
95 {
96     nat size;
97     StgInfoTable *info;
98
99     info = get_itbl(c);
100
101     if (info->type != EVACUATED) {
102         ASSERT(((LDVW(c) & LDV_CREATE_MASK) >> LDV_SHIFT) <= era &&
103                ((LDVW(c) & LDV_CREATE_MASK) >> LDV_SHIFT) > 0);
104         ASSERT(((LDVW(c) & LDV_STATE_MASK) == LDV_STATE_CREATE) ||
105                (
106                    (LDVW(c) & LDV_LAST_MASK) <= era &&
107                    (LDVW(c) & LDV_LAST_MASK) > 0
108                    ));
109     }
110
111     switch (info->type) {
112         /*
113           'inherently used' cases: do nothing.
114         */
115
116     case TSO:
117         size = tso_sizeW((StgTSO *)c);
118         return size;
119
120     case MVAR:
121         size = sizeofW(StgMVar);
122         return size;
123
124     case MUT_ARR_PTRS:
125     case MUT_ARR_PTRS_FROZEN:
126         size = mut_arr_ptrs_sizeW((StgMutArrPtrs *)c);
127         return size;
128
129     case ARR_WORDS:
130         size = arr_words_sizeW((StgArrWords *)c);
131         return size;
132
133     case WEAK:
134     case MUT_VAR:
135     case MUT_CONS:
136     case FOREIGN:
137     case BCO:
138     case STABLE_NAME:
139         size = sizeW_fromITBL(info);
140         return size;
141
142         /*
143           ordinary cases: call LDV_recordDead().
144         */
145
146     case THUNK:
147         size = stg_max(sizeW_fromITBL(info), sizeofW(StgHeader) + MIN_UPD_SIZE);
148         break;
149
150     case THUNK_1_0:
151     case THUNK_0_1:
152     case THUNK_2_0:
153     case THUNK_1_1:
154     case THUNK_0_2:
155     case THUNK_SELECTOR:
156         size = sizeofW(StgHeader) + MIN_UPD_SIZE;
157         break;
158
159     case AP_UPD:
160     case PAP:
161         size = pap_sizeW((StgPAP *)c);
162         break;
163
164     case CONSTR:
165     case CONSTR_1_0:
166     case CONSTR_0_1:
167     case CONSTR_2_0:
168     case CONSTR_1_1:
169     case CONSTR_0_2:
170
171     case FUN:
172     case FUN_1_0:
173     case FUN_0_1:
174     case FUN_2_0:
175     case FUN_1_1:
176     case FUN_0_2:
177
178     case BLACKHOLE_BQ:
179     case BLACKHOLE:
180     case SE_BLACKHOLE:
181     case CAF_BLACKHOLE:
182     case SE_CAF_BLACKHOLE:
183         size = sizeW_fromITBL(info);
184         break;
185
186     case IND_PERM:
187         size = sizeofW(StgInd);
188         break;
189
190     case IND_OLDGEN_PERM:
191         size = sizeofW(StgIndOldGen);
192         break;
193
194         /*
195           'Ingore' cases
196         */
197         // Why can we ignore IND/IND_OLDGEN closures? We assume that
198         // any census is preceded by a major garbage collection, which
199         // IND/IND_OLDGEN closures cannot survive. Therefore, it is no
200         // use considering IND/IND_OLDGEN closures in the meanwhile
201         // because they will perish before the next census at any
202         // rate.
203     case IND:
204         size = sizeofW(StgInd);
205         return size;
206
207     case IND_OLDGEN:
208         size = sizeofW(StgIndOldGen);
209         return size;
210
211     case EVACUATED:
212         // The size of the evacuated closure is currently stored in
213         // the LDV field.  See SET_EVACUAEE_FOR_LDV() in
214         // includes/StgLdvProf.h.
215         return LDVW(c);
216
217         /*
218           Error case
219         */
220         // static objects
221     case IND_STATIC:
222     case CONSTR_STATIC:
223     case FUN_STATIC:
224     case THUNK_STATIC:
225     case CONSTR_INTLIKE:
226     case CONSTR_CHARLIKE:
227     case CONSTR_NOCAF_STATIC:
228         // stack objects
229     case UPDATE_FRAME:
230     case CATCH_FRAME:
231     case STOP_FRAME:
232     case SEQ_FRAME:
233     case RET_DYN:
234     case RET_BCO:
235     case RET_SMALL:
236     case RET_VEC_SMALL:
237     case RET_BIG:
238     case RET_VEC_BIG:
239         // others
240     case BLOCKED_FETCH:
241     case FETCH_ME:
242     case FETCH_ME_BQ:
243     case RBH:
244     case REMOTE_REF:
245     case INVALID_OBJECT:
246     default:
247         barf("Invalid object in processHeapClosureForDead(): %d", info->type);
248         return 0;
249     }
250
251     // Found a dead closure: record its size
252     LDV_recordDead(c, size);
253     return size;
254 }
255
256 /* --------------------------------------------------------------------------
257  * Calls processHeapClosureForDead() on every *dead* closures in the
258  * heap blocks starting at bd.
259  * ----------------------------------------------------------------------- */
260 static void
261 processHeapForDead( bdescr *bd )
262 {
263     StgPtr p;
264
265     while (bd != NULL) {
266         p = bd->start;
267         while (p < bd->free) {
268             p += processHeapClosureForDead((StgClosure *)p);
269             while (p < bd->free && !*p)   // skip slop
270                 p++;
271         }
272         ASSERT(p == bd->free);
273         bd = bd->link;
274     }
275 }
276
277 /* --------------------------------------------------------------------------
278  * Calls processHeapClosureForDead() on every *dead* closures in the nursery.
279  * ----------------------------------------------------------------------- */
280 static void
281 processNurseryForDead( void )
282 {
283     StgPtr p, bdLimit;
284     bdescr *bd;
285
286     bd = MainCapability.r.rNursery;
287     while (bd->start < bd->free) {
288         p = bd->start;
289         bdLimit = bd->start + BLOCK_SIZE_W;
290         while (p < bd->free && p < bdLimit) {
291             p += processHeapClosureForDead((StgClosure *)p);
292             while (p < bd->free && p < bdLimit && !*p)  // skip slop
293                 p++;
294         }
295         bd = bd->link;
296         if (bd == NULL)
297             break;
298     }
299 }
300
301 /* --------------------------------------------------------------------------
302  * Calls processHeapClosureForDead() on every *dead* closures in the
303  * small object pool.
304  * ----------------------------------------------------------------------- */
305 static void
306 processSmallObjectPoolForDead( void )
307 {
308     bdescr *bd;
309     StgPtr p;
310
311     bd = small_alloc_list;
312
313     // first block
314     if (bd == NULL)
315         return;
316
317     p = bd->start;
318     while (p < alloc_Hp) {
319         p += processHeapClosureForDead((StgClosure *)p);
320         while (p < alloc_Hp && !*p)     // skip slop
321             p++;
322     }
323     ASSERT(p == alloc_Hp);
324
325     bd = bd->link;
326     while (bd != NULL) {
327         p = bd->start;
328         while (p < bd->free) {
329             p += processHeapClosureForDead((StgClosure *)p);
330             while (p < bd->free && !*p)    // skip slop
331                 p++;
332         }
333         ASSERT(p == bd->free);
334         bd = bd->link;
335     }
336 }
337
338 /* --------------------------------------------------------------------------
339  * Calls processHeapClosureForDead() on every *dead* closures in the closure
340  * chain.
341  * ----------------------------------------------------------------------- */
342 static void
343 processChainForDead( bdescr *bd )
344 {
345     // Any object still in the chain is dead!
346     while (bd != NULL) {
347         processHeapClosureForDead((StgClosure *)bd->start);
348         bd = bd->link;
349     }
350 }
351
352 /* --------------------------------------------------------------------------
353  * Start a census for *dead* closures, and calls
354  * processHeapClosureForDead() on every closure which died in the
355  * current garbage collection.  This function is called from a garbage
356  * collector right before tidying up, when all dead closures are still
357  * stored in the heap and easy to identify.  Generations 0 through N
358  * have just beed garbage collected.
359  * ----------------------------------------------------------------------- */
360 void
361 LdvCensusForDead( nat N )
362 {
363     nat g, s;
364
365     // ldvTime == 0 means that LDV profiling is currently turned off.
366     if (era == 0)
367         return;
368
369     if (RtsFlags.GcFlags.generations == 1) {
370         //
371         // Todo: support LDV for two-space garbage collection.
372         //
373         barf("Lag/Drag/Void profiling not supported with -G1");
374     } else {
375         for (g = 0; g <= N; g++)
376             for (s = 0; s < generations[g].n_steps; s++) {
377                 if (g == 0 && s == 0) {
378                     processSmallObjectPoolForDead();
379                     processNurseryForDead();
380                     processChainForDead(generations[g].steps[s].large_objects);
381                 } else{
382                     processHeapForDead(generations[g].steps[s].blocks);
383                     processChainForDead(generations[g].steps[s].large_objects);
384                 }
385             }
386     }
387 }
388
389 /* --------------------------------------------------------------------------
390  * Regard any closure in the current heap as dead or moribund and update
391  * LDV statistics accordingly.
392  * Called from shutdownHaskell() in RtsStartup.c.
393  * Also, stops LDV profiling by resetting ldvTime to 0.
394  * ----------------------------------------------------------------------- */
395 void
396 LdvCensusKillAll( void )
397 {
398     LdvCensusForDead(RtsFlags.GcFlags.generations - 1);
399 }
400
401 #endif /* PROFILING */