count fizzled and GC'd sparks separately
[ghc-hetmet.git] / rts / Sparks.c
1 /* ---------------------------------------------------------------------------
2  *
3  * (c) The GHC Team, 2000-2008
4  *
5  * Sparking support for PARALLEL_HASKELL and THREADED_RTS versions 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
18 #if defined(THREADED_RTS)
19
20 void
21 initSparkPools( void )
22 {
23     /* walk over the capabilities, allocating a spark pool for each one */
24     nat i;
25     for (i = 0; i < n_capabilities; i++) {
26       capabilities[i].sparks = newWSDeque(RtsFlags.ParFlags.maxLocalSparks);
27     }
28 }
29
30 void
31 freeSparkPool (SparkPool *pool)
32 {
33     freeWSDeque(pool);
34 }
35
36 /* -----------------------------------------------------------------------------
37  * 
38  * Turn a spark into a real thread
39  *
40  * -------------------------------------------------------------------------- */
41
42 void
43 createSparkThread (Capability *cap)
44 {
45     StgTSO *tso;
46
47     tso = createIOThread (cap, RtsFlags.GcFlags.initialStkSize, 
48                           (StgClosure *)runSparks_closure);
49
50     traceEventCreateSparkThread(cap, tso->id);
51
52     appendToRunQueue(cap,tso);
53 }
54
55 /* --------------------------------------------------------------------------
56  * newSpark: create a new spark, as a result of calling "par"
57  * Called directly from STG.
58  * -------------------------------------------------------------------------- */
59
60 StgInt
61 newSpark (StgRegTable *reg, StgClosure *p)
62 {
63     Capability *cap = regTableToCapability(reg);
64     SparkPool *pool = cap->sparks;
65
66     /* I am not sure whether this is the right thing to do.
67      * Maybe it is better to exploit the tag information
68      * instead of throwing it away?
69      */
70     p = UNTAG_CLOSURE(p);
71
72     if (closure_SHOULD_SPARK(p)) {
73         pushWSDeque(pool,p);
74         cap->sparks_created++;
75     } else {
76         cap->sparks_dud++;
77     }
78
79     return 1;
80 }
81
82 /* -----------------------------------------------------------------------------
83  * 
84  * tryStealSpark: try to steal a spark from a Capability.
85  *
86  * Returns a valid spark, or NULL if the pool was empty, and can
87  * occasionally return NULL if there was a race with another thread
88  * stealing from the same pool.  In this case, try again later.
89  *
90  -------------------------------------------------------------------------- */
91
92 StgClosure *
93 tryStealSpark (Capability *cap)
94 {
95   SparkPool *pool = cap->sparks;
96   StgClosure *stolen;
97
98   do { 
99       stolen = stealWSDeque_(pool); 
100       // use the no-loopy version, stealWSDeque_(), since if we get a
101       // spurious NULL here the caller may want to try stealing from
102       // other pools before trying again.
103   } while (stolen != NULL && !closure_SHOULD_SPARK(stolen));
104
105   return stolen;
106 }
107
108 /* --------------------------------------------------------------------------
109  * Remove all sparks from the spark queues which should not spark any
110  * more.  Called after GC. We assume exclusive access to the structure
111  * and replace  all sparks in the queue, see explanation below. At exit,
112  * the spark pool only contains sparkable closures.
113  * -------------------------------------------------------------------------- */
114
115 void
116 pruneSparkQueue (Capability *cap)
117
118     SparkPool *pool;
119     StgClosurePtr spark, tmp, *elements;
120     nat n, pruned_sparks; // stats only
121     StgWord botInd,oldBotInd,currInd; // indices in array (always < size)
122     const StgInfoTable *info;
123     
124     n = 0;
125     pruned_sparks = 0;
126     
127     pool = cap->sparks;
128     
129     // it is possible that top > bottom, indicating an empty pool.  We
130     // fix that here; this is only necessary because the loop below
131     // assumes it.
132     if (pool->top > pool->bottom)
133         pool->top = pool->bottom;
134
135     // Take this opportunity to reset top/bottom modulo the size of
136     // the array, to avoid overflow.  This is only possible because no
137     // stealing is happening during GC.
138     pool->bottom  -= pool->top & ~pool->moduloSize;
139     pool->top     &= pool->moduloSize;
140     pool->topBound = pool->top;
141
142     debugTrace(DEBUG_sparks,
143                "markSparkQueue: current spark queue len=%ld; (hd=%ld; tl=%ld)",
144                sparkPoolSize(pool), pool->bottom, pool->top);
145
146     ASSERT_WSDEQUE_INVARIANTS(pool);
147
148     elements = (StgClosurePtr *)pool->elements;
149
150     /* We have exclusive access to the structure here, so we can reset
151        bottom and top counters, and prune invalid sparks. Contents are
152        copied in-place if they are valuable, otherwise discarded. The
153        routine uses "real" indices t and b, starts by computing them
154        as the modulus size of top and bottom,
155
156        Copying:
157
158        At the beginning, the pool structure can look like this:
159        ( bottom % size >= top % size , no wrap-around)
160                   t          b
161        ___________***********_________________
162
163        or like this ( bottom % size < top % size, wrap-around )
164                   b         t
165        ***********__________******************
166        As we need to remove useless sparks anyway, we make one pass
167        between t and b, moving valuable content to b and subsequent
168        cells (wrapping around when the size is reached).
169
170                      b      t
171        ***********OOO_______XX_X__X?**********
172                      ^____move?____/
173
174        After this movement, botInd becomes the new bottom, and old
175        bottom becomes the new top index, both as indices in the array
176        size range.
177     */
178     // starting here
179     currInd = (pool->top) & (pool->moduloSize); // mod
180
181     // copies of evacuated closures go to space from botInd on
182     // we keep oldBotInd to know when to stop
183     oldBotInd = botInd = (pool->bottom) & (pool->moduloSize); // mod
184
185     // on entry to loop, we are within the bounds
186     ASSERT( currInd < pool->size && botInd  < pool->size );
187
188     while (currInd != oldBotInd ) {
189       /* must use != here, wrap-around at size
190          subtle: loop not entered if queue empty
191        */
192
193       /* check element at currInd. if valuable, evacuate and move to
194          botInd, otherwise move on */
195       spark = elements[currInd];
196
197       // We have to be careful here: in the parallel GC, another
198       // thread might evacuate this closure while we're looking at it,
199       // so grab the info pointer just once.
200       info = spark->header.info;
201       if (IS_FORWARDING_PTR(info)) {
202           tmp = (StgClosure*)UN_FORWARDING_PTR(info);
203           /* if valuable work: shift inside the pool */
204           if (closure_SHOULD_SPARK(tmp)) {
205               elements[botInd] = tmp; // keep entry (new address)
206               botInd++;
207               n++;
208           } else {
209               pruned_sparks++; // discard spark
210               cap->sparks_fizzled++;
211           }
212       } else if (HEAP_ALLOCED(spark) && 
213                  (Bdescr((P_)spark)->flags & BF_EVACUATED)) {
214           if (closure_SHOULD_SPARK(spark)) {
215               elements[botInd] = spark; // keep entry (new address)
216               botInd++;
217               n++;
218           } else {
219               pruned_sparks++; // discard spark
220               cap->sparks_fizzled++;
221           }
222       } else {
223           pruned_sparks++; // discard spark
224           cap->sparks_gcd++;
225       }
226
227       currInd++;
228
229       // in the loop, we may reach the bounds, and instantly wrap around
230       ASSERT( currInd <= pool->size && botInd <= pool->size );
231       if ( currInd == pool->size ) { currInd = 0; }
232       if ( botInd == pool->size )  { botInd = 0;  }
233
234     } // while-loop over spark pool elements
235
236     ASSERT(currInd == oldBotInd);
237
238     pool->top = oldBotInd; // where we started writing
239     pool->topBound = pool->top;
240
241     pool->bottom = (oldBotInd <= botInd) ? botInd : (botInd + pool->size); 
242     // first free place we did not use (corrected by wraparound)
243
244     debugTrace(DEBUG_sparks, "pruned %d sparks", pruned_sparks);
245     
246     debugTrace(DEBUG_sparks,
247                "new spark queue len=%ld; (hd=%ld; tl=%ld)",
248                sparkPoolSize(pool), pool->bottom, pool->top);
249
250     ASSERT_WSDEQUE_INVARIANTS(pool);
251 }
252
253 /* GC for the spark pool, called inside Capability.c for all
254    capabilities in turn. Blindly "evac"s complete spark pool. */
255 void
256 traverseSparkQueue (evac_fn evac, void *user, Capability *cap)
257 {
258     StgClosure **sparkp;
259     SparkPool *pool;
260     StgWord top,bottom, modMask;
261     
262     pool = cap->sparks;
263
264     ASSERT_WSDEQUE_INVARIANTS(pool);
265
266     top = pool->top;
267     bottom = pool->bottom;
268     sparkp = (StgClosurePtr*)pool->elements;
269     modMask = pool->moduloSize;
270
271     while (top < bottom) {
272     /* call evac for all closures in range (wrap-around via modulo)
273      * In GHC-6.10, evac takes an additional 1st argument to hold a
274      * GC-specific register, see rts/sm/GC.c::mark_root()
275      */
276       evac( user , sparkp + (top & modMask) ); 
277       top++;
278     }
279
280     debugTrace(DEBUG_sparks,
281                "traversed spark queue, len=%ld; (hd=%ld; tl=%ld)",
282                sparkPoolSize(pool), pool->bottom, pool->top);
283 }
284
285 /* ----------------------------------------------------------------------------
286  * balanceSparkPoolsCaps: takes an array of capabilities (usually: all
287  * capabilities) and its size. Accesses all spark pools and equally
288  * distributes the sparks among them.
289  *
290  * Could be called after GC, before Cap. release, from scheduler. 
291  * -------------------------------------------------------------------------- */
292 void balanceSparkPoolsCaps(nat n_caps, Capability caps[])
293    GNUC3_ATTRIBUTE(__noreturn__);
294
295 void balanceSparkPoolsCaps(nat n_caps STG_UNUSED, 
296                            Capability caps[] STG_UNUSED) {
297   barf("not implemented");
298 }
299
300 #else
301
302 StgInt
303 newSpark (StgRegTable *reg STG_UNUSED, StgClosure *p STG_UNUSED)
304 {
305     /* nothing */
306     return 1;
307 }
308
309 #endif /* THREADED_RTS */