Typo fix in scavenge_one comment [skip ci]
[ghc.git] / rts / Sparks.c
1 /* ---------------------------------------------------------------------------
2 *
3 * (c) The GHC Team, 2000-2008
4 *
5 * Sparking support for THREADED_RTS version of the RTS.
6 *
7 -------------------------------------------------------------------------*/
8
9 #include "PosixSource.h"
10 #include "Rts.h"
11
12 #include "Schedule.h"
13 #include "RtsUtils.h"
14 #include "Trace.h"
15 #include "Prelude.h"
16 #include "Sparks.h"
17 #include "ThreadLabels.h"
18 #include "sm/HeapAlloc.h"
19
20 #if defined(THREADED_RTS)
21
22 SparkPool *
23 allocSparkPool( void )
24 {
25 return newWSDeque(RtsFlags.ParFlags.maxLocalSparks);
26 }
27
28 void
29 freeSparkPool (SparkPool *pool)
30 {
31 freeWSDeque(pool);
32 }
33
34 /* -----------------------------------------------------------------------------
35 *
36 * Turn a spark into a real thread
37 *
38 * -------------------------------------------------------------------------- */
39
40 void
41 createSparkThread (Capability *cap)
42 {
43 StgTSO *tso;
44
45 tso = createIOThread (cap, RtsFlags.GcFlags.initialStkSize,
46 (StgClosure *)runSparks_closure);
47 labelThread(cap, tso, "spark evaluator");
48 traceEventCreateSparkThread(cap, tso->id);
49
50 appendToRunQueue(cap,tso);
51 }
52
53 /* --------------------------------------------------------------------------
54 * newSpark: create a new spark, as a result of calling "par"
55 * Called directly from STG.
56 * -------------------------------------------------------------------------- */
57
58 StgInt
59 newSpark (StgRegTable *reg, StgClosure *p)
60 {
61 Capability *cap = regTableToCapability(reg);
62 SparkPool *pool = cap->sparks;
63
64 if (!fizzledSpark(p)) {
65 if (pushWSDeque(pool,p)) {
66 cap->spark_stats.created++;
67 traceEventSparkCreate(cap);
68 } else {
69 /* overflowing the spark pool */
70 cap->spark_stats.overflowed++;
71 traceEventSparkOverflow(cap);
72 }
73 } else {
74 cap->spark_stats.dud++;
75 traceEventSparkDud(cap);
76 }
77
78 return 1;
79 }
80
81 /* --------------------------------------------------------------------------
82 * Remove all sparks from the spark queues which should not spark any
83 * more. Called after GC. We assume exclusive access to the structure
84 * and replace all sparks in the queue, see explanation below. At exit,
85 * the spark pool only contains sparkable closures.
86 * -------------------------------------------------------------------------- */
87
88 void
89 pruneSparkQueue (Capability *cap)
90 {
91 SparkPool *pool;
92 StgClosurePtr spark, tmp, *elements;
93 uint32_t n, pruned_sparks; // stats only
94 StgWord botInd,oldBotInd,currInd; // indices in array (always < size)
95 const StgInfoTable *info;
96
97 n = 0;
98 pruned_sparks = 0;
99
100 pool = cap->sparks;
101
102 // it is possible that top > bottom, indicating an empty pool. We
103 // fix that here; this is only necessary because the loop below
104 // assumes it.
105 if (pool->top > pool->bottom)
106 pool->top = pool->bottom;
107
108 // Take this opportunity to reset top/bottom modulo the size of
109 // the array, to avoid overflow. This is only possible because no
110 // stealing is happening during GC.
111 pool->bottom -= pool->top & ~pool->moduloSize;
112 pool->top &= pool->moduloSize;
113 pool->topBound = pool->top;
114
115 debugTrace(DEBUG_sparks,
116 "markSparkQueue: current spark queue len=%ld; (hd=%ld; tl=%ld)",
117 sparkPoolSize(pool), pool->bottom, pool->top);
118
119 ASSERT_WSDEQUE_INVARIANTS(pool);
120
121 elements = (StgClosurePtr *)pool->elements;
122
123 /* We have exclusive access to the structure here, so we can reset
124 bottom and top counters, and prune invalid sparks. Contents are
125 copied in-place if they are valuable, otherwise discarded. The
126 routine uses "real" indices t and b, starts by computing them
127 as the modulus size of top and bottom,
128
129 Copying:
130
131 At the beginning, the pool structure can look like this:
132 ( bottom % size >= top % size , no wrap-around)
133 t b
134 ___________***********_________________
135
136 or like this ( bottom % size < top % size, wrap-around )
137 b t
138 ***********__________******************
139 As we need to remove useless sparks anyway, we make one pass
140 between t and b, moving valuable content to b and subsequent
141 cells (wrapping around when the size is reached).
142
143 b t
144 ***********OOO_______XX_X__X?**********
145 ^____move?____/
146
147 After this movement, botInd becomes the new bottom, and old
148 bottom becomes the new top index, both as indices in the array
149 size range.
150 */
151 // starting here
152 currInd = (pool->top) & (pool->moduloSize); // mod
153
154 // copies of evacuated closures go to space from botInd on
155 // we keep oldBotInd to know when to stop
156 oldBotInd = botInd = (pool->bottom) & (pool->moduloSize); // mod
157
158 // on entry to loop, we are within the bounds
159 ASSERT( currInd < pool->size && botInd < pool->size );
160
161 while (currInd != oldBotInd ) {
162 /* must use != here, wrap-around at size
163 subtle: loop not entered if queue empty
164 */
165
166 /* check element at currInd. if valuable, evacuate and move to
167 botInd, otherwise move on */
168 spark = elements[currInd];
169
170 // We have to be careful here: in the parallel GC, another
171 // thread might evacuate this closure while we're looking at it,
172 // so grab the info pointer just once.
173 if (GET_CLOSURE_TAG(spark) != 0) {
174 // Tagged pointer is a value, so the spark has fizzled. It
175 // probably never happens that we get a tagged pointer in
176 // the spark pool, because we would have pruned the spark
177 // during the previous GC cycle if it turned out to be
178 // evaluated, but it doesn't hurt to have this check for
179 // robustness.
180 pruned_sparks++;
181 cap->spark_stats.fizzled++;
182 traceEventSparkFizzle(cap);
183 } else {
184 info = spark->header.info;
185 if (IS_FORWARDING_PTR(info)) {
186 tmp = (StgClosure*)UN_FORWARDING_PTR(info);
187 /* if valuable work: shift inside the pool */
188 if (closure_SHOULD_SPARK(tmp)) {
189 elements[botInd] = tmp; // keep entry (new address)
190 botInd++;
191 n++;
192 } else {
193 pruned_sparks++; // discard spark
194 cap->spark_stats.fizzled++;
195 traceEventSparkFizzle(cap);
196 }
197 } else if (HEAP_ALLOCED(spark)) {
198 if ((Bdescr((P_)spark)->flags & BF_EVACUATED)) {
199 if (closure_SHOULD_SPARK(spark)) {
200 elements[botInd] = spark; // keep entry (new address)
201 botInd++;
202 n++;
203 } else {
204 pruned_sparks++; // discard spark
205 cap->spark_stats.fizzled++;
206 traceEventSparkFizzle(cap);
207 }
208 } else {
209 pruned_sparks++; // discard spark
210 cap->spark_stats.gcd++;
211 traceEventSparkGC(cap);
212 }
213 } else {
214 if (INFO_PTR_TO_STRUCT(info)->type == THUNK_STATIC) {
215 // We can't tell whether a THUNK_STATIC is garbage or not.
216 // See also Note [STATIC_LINK fields]
217 // isAlive() also ignores static closures (see GCAux.c)
218 elements[botInd] = spark; // keep entry (new address)
219 botInd++;
220 n++;
221 } else {
222 pruned_sparks++; // discard spark
223 cap->spark_stats.fizzled++;
224 traceEventSparkFizzle(cap);
225 }
226 }
227 }
228
229 currInd++;
230
231 // in the loop, we may reach the bounds, and instantly wrap around
232 ASSERT( currInd <= pool->size && botInd <= pool->size );
233 if ( currInd == pool->size ) { currInd = 0; }
234 if ( botInd == pool->size ) { botInd = 0; }
235
236 } // while-loop over spark pool elements
237
238 ASSERT(currInd == oldBotInd);
239
240 pool->top = oldBotInd; // where we started writing
241 pool->topBound = pool->top;
242
243 pool->bottom = (oldBotInd <= botInd) ? botInd : (botInd + pool->size);
244 // first free place we did not use (corrected by wraparound)
245
246 debugTrace(DEBUG_sparks, "pruned %d sparks", pruned_sparks);
247
248 debugTrace(DEBUG_sparks,
249 "new spark queue len=%ld; (hd=%ld; tl=%ld)",
250 sparkPoolSize(pool), pool->bottom, pool->top);
251
252 ASSERT_WSDEQUE_INVARIANTS(pool);
253 }
254
255 /* GC for the spark pool, called inside Capability.c for all
256 capabilities in turn. Blindly "evac"s complete spark pool. */
257 void
258 traverseSparkQueue (evac_fn evac, void *user, Capability *cap)
259 {
260 StgClosure **sparkp;
261 SparkPool *pool;
262 StgWord top,bottom, modMask;
263
264 pool = cap->sparks;
265
266 ASSERT_WSDEQUE_INVARIANTS(pool);
267
268 top = pool->top;
269 bottom = pool->bottom;
270 sparkp = (StgClosurePtr*)pool->elements;
271 modMask = pool->moduloSize;
272
273 while (top < bottom) {
274 /* call evac for all closures in range (wrap-around via modulo)
275 * In GHC-6.10, evac takes an additional 1st argument to hold a
276 * GC-specific register, see rts/sm/GC.c::mark_root()
277 */
278 evac( user , sparkp + (top & modMask) );
279 top++;
280 }
281
282 debugTrace(DEBUG_sparks,
283 "traversed spark queue, len=%ld; (hd=%ld; tl=%ld)",
284 sparkPoolSize(pool), pool->bottom, pool->top);
285 }
286
287 #else
288
289 StgInt
290 newSpark (StgRegTable *reg STG_UNUSED, StgClosure *p STG_UNUSED)
291 {
292 /* nothing */
293 return 1;
294 }
295
296 #endif /* THREADED_RTS */