update for changes in hetmet Makefile
[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       if (GET_CLOSURE_TAG(spark) != 0) {
201           // Tagged pointer is a value, so the spark has fizzled.  It
202           // probably never happens that we get a tagged pointer in
203           // the spark pool, because we would have pruned the spark
204           // during the previous GC cycle if it turned out to be
205           // evaluated, but it doesn't hurt to have this check for
206           // robustness.
207           pruned_sparks++;
208           cap->sparks_fizzled++;
209       } else {
210           info = spark->header.info;
211           if (IS_FORWARDING_PTR(info)) {
212               tmp = (StgClosure*)UN_FORWARDING_PTR(info);
213               /* if valuable work: shift inside the pool */
214               if (closure_SHOULD_SPARK(tmp)) {
215                   elements[botInd] = tmp; // keep entry (new address)
216                   botInd++;
217                   n++;
218               } else {
219                   pruned_sparks++; // discard spark
220                   cap->sparks_fizzled++;
221               }
222           } else if (HEAP_ALLOCED(spark) &&
223                      (Bdescr((P_)spark)->flags & BF_EVACUATED)) {
224               if (closure_SHOULD_SPARK(spark)) {
225                   elements[botInd] = spark; // keep entry (new address)
226                   botInd++;
227                   n++;
228               } else {
229                   pruned_sparks++; // discard spark
230                   cap->sparks_fizzled++;
231               }
232           } else {
233               pruned_sparks++; // discard spark
234               cap->sparks_gcd++;
235           }
236       }
237
238       currInd++;
239
240       // in the loop, we may reach the bounds, and instantly wrap around
241       ASSERT( currInd <= pool->size && botInd <= pool->size );
242       if ( currInd == pool->size ) { currInd = 0; }
243       if ( botInd == pool->size )  { botInd = 0;  }
244
245     } // while-loop over spark pool elements
246
247     ASSERT(currInd == oldBotInd);
248
249     pool->top = oldBotInd; // where we started writing
250     pool->topBound = pool->top;
251
252     pool->bottom = (oldBotInd <= botInd) ? botInd : (botInd + pool->size); 
253     // first free place we did not use (corrected by wraparound)
254
255     debugTrace(DEBUG_sparks, "pruned %d sparks", pruned_sparks);
256     
257     debugTrace(DEBUG_sparks,
258                "new spark queue len=%ld; (hd=%ld; tl=%ld)",
259                sparkPoolSize(pool), pool->bottom, pool->top);
260
261     ASSERT_WSDEQUE_INVARIANTS(pool);
262 }
263
264 /* GC for the spark pool, called inside Capability.c for all
265    capabilities in turn. Blindly "evac"s complete spark pool. */
266 void
267 traverseSparkQueue (evac_fn evac, void *user, Capability *cap)
268 {
269     StgClosure **sparkp;
270     SparkPool *pool;
271     StgWord top,bottom, modMask;
272     
273     pool = cap->sparks;
274
275     ASSERT_WSDEQUE_INVARIANTS(pool);
276
277     top = pool->top;
278     bottom = pool->bottom;
279     sparkp = (StgClosurePtr*)pool->elements;
280     modMask = pool->moduloSize;
281
282     while (top < bottom) {
283     /* call evac for all closures in range (wrap-around via modulo)
284      * In GHC-6.10, evac takes an additional 1st argument to hold a
285      * GC-specific register, see rts/sm/GC.c::mark_root()
286      */
287       evac( user , sparkp + (top & modMask) ); 
288       top++;
289     }
290
291     debugTrace(DEBUG_sparks,
292                "traversed spark queue, len=%ld; (hd=%ld; tl=%ld)",
293                sparkPoolSize(pool), pool->bottom, pool->top);
294 }
295
296 /* ----------------------------------------------------------------------------
297  * balanceSparkPoolsCaps: takes an array of capabilities (usually: all
298  * capabilities) and its size. Accesses all spark pools and equally
299  * distributes the sparks among them.
300  *
301  * Could be called after GC, before Cap. release, from scheduler. 
302  * -------------------------------------------------------------------------- */
303 void balanceSparkPoolsCaps(nat n_caps, Capability caps[])
304    GNUC3_ATTRIBUTE(__noreturn__);
305
306 void balanceSparkPoolsCaps(nat n_caps STG_UNUSED, 
307                            Capability caps[] STG_UNUSED) {
308   barf("not implemented");
309 }
310
311 #else
312
313 StgInt
314 newSpark (StgRegTable *reg STG_UNUSED, StgClosure *p STG_UNUSED)
315 {
316     /* nothing */
317     return 1;
318 }
319
320 #endif /* THREADED_RTS */