Fold testsuite.git into ghc.git (re #8545)
[ghc.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 "PosixSource.h"
13 #include "Rts.h"
14
15 #include "Profiling.h"
16 #include "LdvProfile.h"
17 #include "Stats.h"
18 #include "RtsUtils.h"
19 #include "Schedule.h"
20
21 /* --------------------------------------------------------------------------
22 * This function is called eventually on every object destroyed during
23 * a garbage collection, whether it is a major garbage collection or
24 * not. If c is an 'inherently used' closure, nothing happens. If c
25 * is an ordinary closure, LDV_recordDead() is called on c with its
26 * proper size which excludes the profiling header portion in the
27 * closure. Returns the size of the closure, including the profiling
28 * header portion, so that the caller can find the next closure.
29 * ----------------------------------------------------------------------- */
30 STATIC_INLINE nat
31 processHeapClosureForDead( StgClosure *c )
32 {
33 nat size;
34 const StgInfoTable *info;
35
36 info = get_itbl(c);
37
38 info = c->header.info;
39 if (IS_FORWARDING_PTR(info)) {
40 // The size of the evacuated closure is currently stored in
41 // the LDV field. See SET_EVACUAEE_FOR_LDV() in
42 // includes/StgLdvProf.h.
43 return LDVW(c);
44 }
45 info = INFO_PTR_TO_STRUCT(info);
46
47 ASSERT(((LDVW(c) & LDV_CREATE_MASK) >> LDV_SHIFT) <= era &&
48 ((LDVW(c) & LDV_CREATE_MASK) >> LDV_SHIFT) > 0);
49 ASSERT(((LDVW(c) & LDV_STATE_MASK) == LDV_STATE_CREATE) ||
50 (
51 (LDVW(c) & LDV_LAST_MASK) <= era &&
52 (LDVW(c) & LDV_LAST_MASK) > 0
53 ));
54
55
56 size = closure_sizeW(c);
57
58 switch (info->type) {
59 /*
60 'inherently used' cases: do nothing.
61 */
62 case TSO:
63 case STACK:
64 case MVAR_CLEAN:
65 case MVAR_DIRTY:
66 case TVAR:
67 case MUT_ARR_PTRS_CLEAN:
68 case MUT_ARR_PTRS_DIRTY:
69 case MUT_ARR_PTRS_FROZEN:
70 case MUT_ARR_PTRS_FROZEN0:
71 case ARR_WORDS:
72 case WEAK:
73 case MUT_VAR_CLEAN:
74 case MUT_VAR_DIRTY:
75 case BCO:
76 case PRIM:
77 case MUT_PRIM:
78 case TREC_CHUNK:
79 return size;
80
81 /*
82 ordinary cases: call LDV_recordDead().
83 */
84 case THUNK:
85 case THUNK_1_0:
86 case THUNK_0_1:
87 case THUNK_SELECTOR:
88 case THUNK_2_0:
89 case THUNK_1_1:
90 case THUNK_0_2:
91 case AP:
92 case PAP:
93 case AP_STACK:
94 case CONSTR:
95 case CONSTR_1_0:
96 case CONSTR_0_1:
97 case CONSTR_2_0:
98 case CONSTR_1_1:
99 case CONSTR_0_2:
100 case FUN:
101 case FUN_1_0:
102 case FUN_0_1:
103 case FUN_2_0:
104 case FUN_1_1:
105 case FUN_0_2:
106 case BLACKHOLE:
107 case BLOCKING_QUEUE:
108 case IND_PERM:
109 /*
110 'Ingore' cases
111 */
112 // Why can we ignore IND closures? We assume that
113 // any census is preceded by a major garbage collection, which
114 // IND closures cannot survive. Therefore, it is no
115 // use considering IND closures in the meanwhile
116 // because they will perish before the next census at any
117 // rate.
118 case IND:
119 // Found a dead closure: record its size
120 LDV_recordDead(c, size);
121 return size;
122
123 /*
124 Error case
125 */
126 // static objects
127 case IND_STATIC:
128 case CONSTR_STATIC:
129 case FUN_STATIC:
130 case THUNK_STATIC:
131 case CONSTR_NOCAF_STATIC:
132 // stack objects
133 case UPDATE_FRAME:
134 case CATCH_FRAME:
135 case UNDERFLOW_FRAME:
136 case STOP_FRAME:
137 case RET_BCO:
138 case RET_SMALL:
139 case RET_BIG:
140 // others
141 case INVALID_OBJECT:
142 default:
143 barf("Invalid object in processHeapClosureForDead(): %d", info->type);
144 return 0;
145 }
146 }
147
148 /* --------------------------------------------------------------------------
149 * Calls processHeapClosureForDead() on every *dead* closures in the
150 * heap blocks starting at bd.
151 * ----------------------------------------------------------------------- */
152 static void
153 processHeapForDead( bdescr *bd )
154 {
155 StgPtr p;
156
157 while (bd != NULL) {
158 p = bd->start;
159 while (p < bd->free) {
160 p += processHeapClosureForDead((StgClosure *)p);
161 while (p < bd->free && !*p) // skip slop
162 p++;
163 }
164 ASSERT(p == bd->free);
165 bd = bd->link;
166 }
167 }
168
169 /* --------------------------------------------------------------------------
170 * Calls processHeapClosureForDead() on every *dead* closures in the nursery.
171 * ----------------------------------------------------------------------- */
172 static void
173 processNurseryForDead( void )
174 {
175 StgPtr p;
176 bdescr *bd;
177
178 for (bd = MainCapability.r.rNursery->blocks; bd != NULL; bd = bd->link) {
179 p = bd->start;
180 while (p < bd->free) {
181 while (p < bd->free && !*p) p++; // skip slop
182 if (p >= bd->free) break;
183 p += processHeapClosureForDead((StgClosure *)p);
184 }
185 }
186 }
187
188 /* --------------------------------------------------------------------------
189 * Calls processHeapClosureForDead() on every *dead* closures in the closure
190 * chain.
191 * ----------------------------------------------------------------------- */
192 static void
193 processChainForDead( bdescr *bd )
194 {
195 // Any object still in the chain is dead!
196 while (bd != NULL) {
197 if (!(bd->flags & BF_PINNED)) {
198 processHeapClosureForDead((StgClosure *)bd->start);
199 }
200 bd = bd->link;
201 }
202 }
203
204 /* --------------------------------------------------------------------------
205 * Start a census for *dead* closures, and calls
206 * processHeapClosureForDead() on every closure which died in the
207 * current garbage collection. This function is called from a garbage
208 * collector right before tidying up, when all dead closures are still
209 * stored in the heap and easy to identify. Generations 0 through N
210 * have just been garbage collected.
211 * ----------------------------------------------------------------------- */
212 void
213 LdvCensusForDead( nat N )
214 {
215 nat g;
216
217 // ldvTime == 0 means that LDV profiling is currently turned off.
218 if (era == 0)
219 return;
220
221 if (RtsFlags.GcFlags.generations == 1) {
222 //
223 // Todo: support LDV for two-space garbage collection.
224 //
225 barf("Lag/Drag/Void profiling not supported with -G1");
226 } else {
227 processNurseryForDead();
228 for (g = 0; g <= N; g++) {
229 processHeapForDead(generations[g].old_blocks);
230 processChainForDead(generations[g].large_objects);
231 }
232 }
233 }
234
235 /* --------------------------------------------------------------------------
236 * Regard any closure in the current heap as dead or moribund and update
237 * LDV statistics accordingly.
238 * Called from shutdownHaskell() in RtsStartup.c.
239 * Also, stops LDV profiling by resetting ldvTime to 0.
240 * ----------------------------------------------------------------------- */
241 void
242 LdvCensusKillAll( void )
243 {
244 LdvCensusForDead(RtsFlags.GcFlags.generations - 1);
245 }
246
247 #endif /* PROFILING */