1 /* ---------------------------------------------------------------------------
3 * (c) The GHC Team, 2000-2008
5 * Sparking support for PARALLEL_HASKELL and THREADED_RTS versions of the RTS.
7 -------------------------------------------------------------------------*/
9 #include "PosixSource.h"
20 #include "SMP.h" // for cas
24 #if defined(THREADED_RTS) || defined(PARALLEL_HASKELL)
27 initSparkPools( void )
30 /* walk over the capabilities, allocating a spark pool for each one */
32 for (i = 0; i < n_capabilities; i++) {
33 capabilities[i].sparks = newWSDeque(RtsFlags.ParFlags.maxLocalSparks);
36 /* allocate a single spark pool */
37 MainCapability->sparks = newWSDeque(RtsFlags.ParFlags.maxLocalSparks);
42 freeSparkPool (SparkPool *pool)
47 /* -----------------------------------------------------------------------------
49 * Turn a spark into a real thread
51 * -------------------------------------------------------------------------- */
54 createSparkThread (Capability *cap)
58 tso = createIOThread (cap, RtsFlags.GcFlags.initialStkSize,
59 &base_GHCziConc_runSparks_closure);
61 if (cap->r.rCurrentTSO != NULL)
62 // Capability in a bound thread?
63 postEvent(cap, EVENT_SPARK_TO_THREAD, cap->r.rCurrentTSO->id, tso->id);
65 // Capability in a worker thread?
66 postEvent(cap, EVENT_SPARK_TO_THREAD, 0, tso->id);
68 appendToRunQueue(cap,tso);
71 /* --------------------------------------------------------------------------
72 * newSpark: create a new spark, as a result of calling "par"
73 * Called directly from STG.
74 * -------------------------------------------------------------------------- */
77 newSpark (StgRegTable *reg, StgClosure *p)
79 Capability *cap = regTableToCapability(reg);
80 SparkPool *pool = cap->sparks;
82 /* I am not sure whether this is the right thing to do.
83 * Maybe it is better to exploit the tag information
84 * instead of throwing it away?
88 if (closure_SHOULD_SPARK(p)) {
92 cap->sparks_created++;
94 postEvent(cap, EVENT_CREATE_SPARK, reg->rCurrentTSO->id, 0);
99 /* -----------------------------------------------------------------------------
101 * tryStealSpark: try to steal a spark from a Capability.
103 * Returns a valid spark, or NULL if the pool was empty, and can
104 * occasionally return NULL if there was a race with another thread
105 * stealing from the same pool. In this case, try again later.
107 -------------------------------------------------------------------------- */
110 tryStealSpark (Capability *cap)
112 SparkPool *pool = cap->sparks;
116 stolen = stealWSDeque_(pool);
117 // use the no-loopy version, stealWSDeque_(), since if we get a
118 // spurious NULL here the caller may want to try stealing from
119 // other pools before trying again.
120 } while (stolen != NULL && !closure_SHOULD_SPARK(stolen));
125 /* --------------------------------------------------------------------------
126 * Remove all sparks from the spark queues which should not spark any
127 * more. Called after GC. We assume exclusive access to the structure
128 * and replace all sparks in the queue, see explanation below. At exit,
129 * the spark pool only contains sparkable closures.
130 * -------------------------------------------------------------------------- */
133 pruneSparkQueue (evac_fn evac, void *user, Capability *cap)
136 StgClosurePtr spark, tmp, *elements;
137 nat n, pruned_sparks; // stats only
138 StgWord botInd,oldBotInd,currInd; // indices in array (always < size)
139 const StgInfoTable *info;
141 PAR_TICKY_MARK_SPARK_QUEUE_START();
148 // it is possible that top > bottom, indicating an empty pool. We
149 // fix that here; this is only necessary because the loop below
151 if (pool->top > pool->bottom)
152 pool->top = pool->bottom;
154 // Take this opportunity to reset top/bottom modulo the size of
155 // the array, to avoid overflow. This is only possible because no
156 // stealing is happening during GC.
157 pool->bottom -= pool->top & ~pool->moduloSize;
158 pool->top &= pool->moduloSize;
159 pool->topBound = pool->top;
161 debugTrace(DEBUG_sched,
162 "markSparkQueue: current spark queue len=%ld; (hd=%ld; tl=%ld)",
163 sparkPoolSize(pool), pool->bottom, pool->top);
165 ASSERT_WSDEQUE_INVARIANTS(pool);
167 elements = (StgClosurePtr *)pool->elements;
169 /* We have exclusive access to the structure here, so we can reset
170 bottom and top counters, and prune invalid sparks. Contents are
171 copied in-place if they are valuable, otherwise discarded. The
172 routine uses "real" indices t and b, starts by computing them
173 as the modulus size of top and bottom,
177 At the beginning, the pool structure can look like this:
178 ( bottom % size >= top % size , no wrap-around)
180 ___________***********_________________
182 or like this ( bottom % size < top % size, wrap-around )
184 ***********__________******************
185 As we need to remove useless sparks anyway, we make one pass
186 between t and b, moving valuable content to b and subsequent
187 cells (wrapping around when the size is reached).
190 ***********OOO_______XX_X__X?**********
193 After this movement, botInd becomes the new bottom, and old
194 bottom becomes the new top index, both as indices in the array
198 currInd = (pool->top) & (pool->moduloSize); // mod
200 // copies of evacuated closures go to space from botInd on
201 // we keep oldBotInd to know when to stop
202 oldBotInd = botInd = (pool->bottom) & (pool->moduloSize); // mod
204 // on entry to loop, we are within the bounds
205 ASSERT( currInd < pool->size && botInd < pool->size );
207 while (currInd != oldBotInd ) {
208 /* must use != here, wrap-around at size
209 subtle: loop not entered if queue empty
212 /* check element at currInd. if valuable, evacuate and move to
213 botInd, otherwise move on */
214 spark = elements[currInd];
216 // We have to be careful here: in the parallel GC, another
217 // thread might evacuate this closure while we're looking at it,
218 // so grab the info pointer just once.
219 info = spark->header.info;
220 if (IS_FORWARDING_PTR(info)) {
221 tmp = (StgClosure*)UN_FORWARDING_PTR(info);
222 /* if valuable work: shift inside the pool */
223 if (closure_SHOULD_SPARK(tmp)) {
224 elements[botInd] = tmp; // keep entry (new address)
228 pruned_sparks++; // discard spark
229 cap->sparks_pruned++;
232 if (!(closure_flags[INFO_PTR_TO_STRUCT(info)->type] & _NS)) {
233 elements[botInd] = spark; // keep entry (new address)
234 evac (user, &elements[botInd]);
238 pruned_sparks++; // discard spark
239 cap->sparks_pruned++;
244 // in the loop, we may reach the bounds, and instantly wrap around
245 ASSERT( currInd <= pool->size && botInd <= pool->size );
246 if ( currInd == pool->size ) { currInd = 0; }
247 if ( botInd == pool->size ) { botInd = 0; }
249 } // while-loop over spark pool elements
251 ASSERT(currInd == oldBotInd);
253 pool->top = oldBotInd; // where we started writing
254 pool->topBound = pool->top;
256 pool->bottom = (oldBotInd <= botInd) ? botInd : (botInd + pool->size);
257 // first free place we did not use (corrected by wraparound)
259 PAR_TICKY_MARK_SPARK_QUEUE_END(n);
261 debugTrace(DEBUG_sched, "pruned %d sparks", pruned_sparks);
263 debugTrace(DEBUG_sched,
264 "new spark queue len=%ld; (hd=%ld; tl=%ld)",
265 sparkPoolSize(pool), pool->bottom, pool->top);
267 ASSERT_WSDEQUE_INVARIANTS(pool);
270 /* GC for the spark pool, called inside Capability.c for all
271 capabilities in turn. Blindly "evac"s complete spark pool. */
273 traverseSparkQueue (evac_fn evac, void *user, Capability *cap)
277 StgWord top,bottom, modMask;
281 ASSERT_WSDEQUE_INVARIANTS(pool);
284 bottom = pool->bottom;
285 sparkp = (StgClosurePtr*)pool->elements;
286 modMask = pool->moduloSize;
288 while (top < bottom) {
289 /* call evac for all closures in range (wrap-around via modulo)
290 * In GHC-6.10, evac takes an additional 1st argument to hold a
291 * GC-specific register, see rts/sm/GC.c::mark_root()
293 evac( user , sparkp + (top & modMask) );
297 debugTrace(DEBUG_sched,
298 "traversed spark queue, len=%ld; (hd=%ld; tl=%ld)",
299 sparkPoolSize(pool), pool->bottom, pool->top);
302 /* ----------------------------------------------------------------------------
303 * balanceSparkPoolsCaps: takes an array of capabilities (usually: all
304 * capabilities) and its size. Accesses all spark pools and equally
305 * distributes the sparks among them.
307 * Could be called after GC, before Cap. release, from scheduler.
308 * -------------------------------------------------------------------------- */
309 void balanceSparkPoolsCaps(nat n_caps, Capability caps[]);
311 void balanceSparkPoolsCaps(nat n_caps STG_UNUSED,
312 Capability caps[] STG_UNUSED) {
313 barf("not implemented");
319 newSpark (StgRegTable *reg STG_UNUSED, StgClosure *p STG_UNUSED)
326 #endif /* PARALLEL_HASKELL || THREADED_RTS */
329 /* -----------------------------------------------------------------------------
331 * GRAN & PARALLEL_HASKELL stuff beyond here.
335 * -------------------------------------------------------------------------- */
337 #if defined(PARALLEL_HASKELL) || defined(GRAN)
339 static void slide_spark_pool( StgSparkPool *pool );
342 add_to_spark_queue( StgClosure *closure, StgSparkPool *pool )
344 if (pool->tl == pool->lim)
345 slide_spark_pool(pool);
347 if (closure_SHOULD_SPARK(closure) &&
348 pool->tl < pool->lim) {
349 *(pool->tl++) = closure;
351 #if defined(PARALLEL_HASKELL)
352 // collect parallel global statistics (currently done together with GC stats)
353 if (RtsFlags.ParFlags.ParStats.Global &&
354 RtsFlags.GcFlags.giveStats > NO_GC_STATS) {
355 // debugBelch("Creating spark for %x @ %11.2f\n", closure, usertime());
356 globalParStats.tot_sparks_created++;
361 #if defined(PARALLEL_HASKELL)
362 // collect parallel global statistics (currently done together with GC stats)
363 if (RtsFlags.ParFlags.ParStats.Global &&
364 RtsFlags.GcFlags.giveStats > NO_GC_STATS) {
365 //debugBelch("Ignoring spark for %x @ %11.2f\n", closure, usertime());
366 globalParStats.tot_sparks_ignored++;
374 slide_spark_pool( StgSparkPool *pool )
376 StgClosure **sparkp, **to_sparkp;
379 to_sparkp = pool->base;
380 while (sparkp < pool->tl) {
381 ASSERT(to_sparkp<=sparkp);
382 ASSERT(*sparkp!=NULL);
383 ASSERT(LOOKS_LIKE_GHC_INFO((*sparkp)->header.info));
385 if (closure_SHOULD_SPARK(*sparkp)) {
386 *to_sparkp++ = *sparkp++;
391 pool->hd = pool->base;
392 pool->tl = to_sparkp;
399 #if !defined(THREADED_RTS)
404 pool = &(cap->rSparks);
405 ASSERT(pool->hd <= pool->tl && pool->tl <= pool->lim);
407 ASSERT(spark != (StgClosure *)NULL);
415 Search the spark queue of the proc in event for a spark that's worth
416 turning into a thread
417 (was gimme_spark in the old RTS)
420 findLocalSpark (rtsEvent *event, rtsBool *found_res, rtsSparkQ *spark_res)
422 PEs proc = event->proc, /* proc to search for work */
423 creator = event->creator; /* proc that requested work */
426 rtsSparkQ spark_of_non_local_node = NULL,
427 spark_of_non_local_node_prev = NULL,
428 low_priority_spark = NULL,
429 low_priority_spark_prev = NULL,
430 spark = NULL, prev = NULL;
432 /* Choose a spark from the local spark queue */
433 prev = (rtsSpark*)NULL;
434 spark = pending_sparks_hds[proc];
437 // ToDo: check this code & implement local sparking !! -- HWL
438 while (!found && spark != (rtsSpark*)NULL)
440 ASSERT((prev!=(rtsSpark*)NULL || spark==pending_sparks_hds[proc]) &&
441 (prev==(rtsSpark*)NULL || prev->next==spark) &&
442 (spark->prev==prev));
444 if (!closure_SHOULD_SPARK(node))
446 IF_GRAN_DEBUG(checkSparkQ,
447 debugBelch("^^ pruning spark %p (node %p) in gimme_spark",
450 if (RtsFlags.GranFlags.GranSimStats.Sparks)
451 DumpRawGranEvent(proc, (PEs)0, SP_PRUNED,(StgTSO*)NULL,
452 spark->node, spark->name, spark_queue_len(proc));
454 ASSERT(spark != (rtsSpark*)NULL);
455 ASSERT(SparksAvail>0);
458 ASSERT(prev==(rtsSpark*)NULL || prev->next==spark);
459 spark = delete_from_sparkq (spark, proc, rtsTrue);
460 if (spark != (rtsSpark*)NULL)
464 /* -- node should eventually be sparked */
465 else if (RtsFlags.GranFlags.PreferSparksOfLocalNodes &&
466 !IS_LOCAL_TO(PROCS(node),CurrentProc))
468 barf("Local sparking not yet implemented");
470 /* Remember first low priority spark */
471 if (spark_of_non_local_node==(rtsSpark*)NULL) {
472 spark_of_non_local_node_prev = prev;
473 spark_of_non_local_node = spark;
476 if (spark->next == (rtsSpark*)NULL) {
477 /* ASSERT(spark==SparkQueueTl); just for testing */
478 prev = spark_of_non_local_node_prev;
479 spark = spark_of_non_local_node;
484 # if defined(GRAN) && defined(GRAN_CHECK)
485 /* Should never happen; just for testing
486 if (spark==pending_sparks_tl) {
487 debugBelch("ReSchedule: Last spark != SparkQueueTl\n");
488 stg_exit(EXIT_FAILURE);
493 ASSERT(SparksAvail>0);
497 else if ( RtsFlags.GranFlags.DoPrioritySparking ||
498 (spark->gran_info >= RtsFlags.GranFlags.SparkPriority2) )
500 if (RtsFlags.GranFlags.DoPrioritySparking)
501 barf("Priority sparking not yet implemented");
506 else /* only used if SparkPriority2 is defined */
508 /* ToDo: fix the code below and re-integrate it */
509 /* Remember first low priority spark */
510 if (low_priority_spark==(rtsSpark*)NULL) {
511 low_priority_spark_prev = prev;
512 low_priority_spark = spark;
515 if (spark->next == (rtsSpark*)NULL) {
516 /* ASSERT(spark==spark_queue_tl); just for testing */
517 prev = low_priority_spark_prev;
518 spark = low_priority_spark;
519 found = rtsTrue; /* take low pri spark => rc is 2 */
523 /* Should never happen; just for testing
524 if (spark==pending_sparks_tl) {
525 debugBelch("ReSchedule: Last spark != SparkQueueTl\n");
526 stg_exit(EXIT_FAILURE);
533 debugBelch("++ Ignoring spark of priority %u (SparkPriority=%u); node=%p; name=%u\n",
534 spark->gran_info, RtsFlags.GranFlags.SparkPriority,
535 spark->node, spark->name);)
538 } /* while (spark!=NULL && !found) */
545 Turn the spark into a thread.
546 In GranSim this basically means scheduling a StartThread event for the
547 node pointed to by the spark at some point in the future.
548 (was munch_spark in the old RTS)
551 activateSpark (rtsEvent *event, rtsSparkQ spark)
553 PEs proc = event->proc, /* proc to search for work */
554 creator = event->creator; /* proc that requested work */
557 rtsTime spark_arrival_time;
560 We've found a node on PE proc requested by PE creator.
561 If proc==creator we can turn the spark into a thread immediately;
562 otherwise we schedule a MoveSpark event on the requesting PE
568 /* only possible if we simulate GUM style fishing */
569 ASSERT(RtsFlags.GranFlags.Fishing);
571 /* Message packing costs for sending a Fish; qeq jabbI'ID */
572 CurrentTime[proc] += RtsFlags.GranFlags.Costs.mpacktime;
574 if (RtsFlags.GranFlags.GranSimStats.Sparks)
575 DumpRawGranEvent(proc, (PEs)0, SP_EXPORTED,
576 (StgTSO*)NULL, spark->node,
577 spark->name, spark_queue_len(proc));
579 /* time of the spark arrival on the remote PE */
580 spark_arrival_time = CurrentTime[proc] + RtsFlags.GranFlags.Costs.latency;
582 new_event(creator, proc, spark_arrival_time,
584 (StgTSO*)NULL, spark->node, spark);
586 CurrentTime[proc] += RtsFlags.GranFlags.Costs.mtidytime;
588 } else { /* proc==creator i.e. turn the spark into a thread */
590 if ( RtsFlags.GranFlags.GranSimStats.Global &&
591 spark->gran_info < RtsFlags.GranFlags.SparkPriority2 ) {
593 globalGranStats.tot_low_pri_sparks++;
595 debugBelch("++ No high priority spark available; low priority (%u) spark chosen: node=%p; name=%u\n",
597 spark->node, spark->name));
600 CurrentTime[proc] += RtsFlags.GranFlags.Costs.threadcreatetime;
605 /* ToDo: fix the GC interface and move to StartThread handling-- HWL */
606 if (GARBAGE COLLECTION IS NECESSARY) {
607 /* Some kind of backoff needed here in case there's too little heap */
608 # if defined(GRAN_CHECK) && defined(GRAN)
609 if (RtsFlags.GcFlags.giveStats)
610 fprintf(RtsFlags.GcFlags.statsFile,"***** vIS Qu' chen veQ boSwI'; spark=%p, node=%p; name=%u\n",
611 /* (found==2 ? "no hi pri spark" : "hi pri spark"), */
612 spark, node, spark->name);
614 new_event(CurrentProc, CurrentProc, CurrentTime[CurrentProc]+1,
616 (StgTSO*)NULL, (StgClosure*)NULL, (rtsSpark*)NULL);
617 barf("//// activateSpark: out of heap ; ToDo: call GarbageCollect()");
618 GarbageCollect(GetRoots, rtsFalse);
619 // HWL old: ReallyPerformThreadGC(TSO_HS+TSO_CTS_SIZE,rtsFalse);
620 // HWL old: SAVE_Hp -= TSO_HS+TSO_CTS_SIZE;
622 return; /* was: continue; */ /* to the next event, eventually */
626 if (RtsFlags.GranFlags.GranSimStats.Sparks)
627 DumpRawGranEvent(CurrentProc,(PEs)0,SP_USED,(StgTSO*)NULL,
628 spark->node, spark->name,
629 spark_queue_len(CurrentProc));
631 new_event(proc, proc, CurrentTime[proc],
633 END_TSO_QUEUE, node, spark); // (rtsSpark*)NULL);
635 procStatus[proc] = Starting;
639 /* -------------------------------------------------------------------------
640 This is the main point where handling granularity information comes into
642 ------------------------------------------------------------------------- */
644 #define MAX_RAND_PRI 100
647 Granularity info transformers.
648 Applied to the GRAN_INFO field of a spark.
650 STATIC_INLINE nat ID(nat x) { return(x); };
651 STATIC_INLINE nat INV(nat x) { return(-x); };
652 STATIC_INLINE nat IGNORE(nat x) { return (0); };
653 STATIC_INLINE nat RAND(nat x) { return ((random() % MAX_RAND_PRI) + 1); }
655 /* NB: size_info and par_info are currently unused (what a shame!) -- HWL */
657 newSpark(node,name,gran_info,size_info,par_info,local)
659 nat name, gran_info, size_info, par_info, local;
664 pri = RtsFlags.GranFlags.RandomPriorities ? RAND(gran_info) :
665 RtsFlags.GranFlags.InversePriorities ? INV(gran_info) :
666 RtsFlags.GranFlags.IgnorePriorities ? IGNORE(gran_info) :
669 if ( RtsFlags.GranFlags.SparkPriority!=0 &&
670 pri<RtsFlags.GranFlags.SparkPriority ) {
672 debugBelch(",, NewSpark: Ignoring spark of priority %u (SparkPriority=%u); node=%#x; name=%u\n",
673 pri, RtsFlags.GranFlags.SparkPriority, node, name));
674 return ((rtsSpark*)NULL);
677 newspark = (rtsSpark*) stgMallocBytes(sizeof(rtsSpark), "NewSpark");
678 newspark->prev = newspark->next = (rtsSpark*)NULL;
679 newspark->node = node;
680 newspark->name = (name==1) ? CurrentTSO->gran.sparkname : name;
681 newspark->gran_info = pri;
682 newspark->global = !local; /* Check that with parAt, parAtAbs !!*/
684 if (RtsFlags.GranFlags.GranSimStats.Global) {
685 globalGranStats.tot_sparks_created++;
686 globalGranStats.sparks_created_on_PE[CurrentProc]++;
707 disposeSparkQ(spark->next);
710 if (SparksAvail < 0) {
711 debugBelch("disposeSparkQ: SparksAvail<0 after disposing sparkq @ %p\n", &spark);
720 With PrioritySparking add_to_spark_queue performs an insert sort to keep
721 the spark queue sorted. Otherwise the spark is just added to the end of
726 add_to_spark_queue(spark)
729 rtsSpark *prev = NULL, *next = NULL;
731 rtsBool found = rtsFalse;
733 if ( spark == (rtsSpark *)NULL ) {
737 if (RtsFlags.GranFlags.DoPrioritySparking && (spark->gran_info != 0) ) {
738 /* Priority sparking is enabled i.e. spark queues must be sorted */
740 for (prev = NULL, next = pending_sparks_hd, count=0;
742 !(found = (spark->gran_info >= next->gran_info));
743 prev = next, next = next->next, count++)
746 } else { /* 'utQo' */
747 /* Priority sparking is disabled */
749 found = rtsFalse; /* to add it at the end */
754 /* next points to the first spark with a gran_info smaller than that
755 of spark; therefore, add spark before next into the spark queue */
757 if ( next == NULL ) {
758 pending_sparks_tl = spark;
763 if ( prev == NULL ) {
764 pending_sparks_hd = spark;
768 } else { /* (RtsFlags.GranFlags.DoPrioritySparking && !found) || !DoPrioritySparking */
769 /* add the spark at the end of the spark queue */
771 spark->prev = pending_sparks_tl;
772 if (pending_sparks_hd == NULL)
773 pending_sparks_hd = spark;
775 pending_sparks_tl->next = spark;
776 pending_sparks_tl = spark;
780 /* add costs for search in priority sparking */
781 if (RtsFlags.GranFlags.DoPrioritySparking) {
782 CurrentTime[CurrentProc] += count * RtsFlags.GranFlags.Costs.pri_spark_overhead;
785 IF_GRAN_DEBUG(checkSparkQ,
786 debugBelch("++ Spark stats after adding spark %p (node %p) to queue on PE %d",
787 spark, spark->node, CurrentProc);
788 print_sparkq_stats());
790 # if defined(GRAN_CHECK)
791 if (RtsFlags.GranFlags.Debug.checkSparkQ) {
792 for (prev = NULL, next = pending_sparks_hd;
794 prev = next, next = next->next)
796 if ( (prev!=NULL) && (prev!=pending_sparks_tl) )
797 debugBelch("SparkQ inconsistency after adding spark %p: (PE %u) pending_sparks_tl (%p) not end of queue (%p)\n",
799 pending_sparks_tl, prev);
803 # if defined(GRAN_CHECK)
804 /* Check if the sparkq is still sorted. Just for testing, really! */
805 if ( RtsFlags.GranFlags.Debug.checkSparkQ &&
806 RtsFlags.GranFlags.Debug.pri ) {
807 rtsBool sorted = rtsTrue;
808 rtsSpark *prev, *next;
810 if (pending_sparks_hd == NULL ||
811 pending_sparks_hd->next == NULL ) {
812 /* just 1 elem => ok */
814 for (prev = pending_sparks_hd,
815 next = pending_sparks_hd->next;
817 prev = next, next = next->next) {
819 (prev->gran_info >= next->gran_info);
823 debugBelch("ghuH: SPARKQ on PE %d is not sorted:\n",
825 print_sparkq(CurrentProc);
832 spark_queue_len(proc)
835 rtsSpark *prev, *spark; /* prev only for testing !! */
838 for (len = 0, prev = NULL, spark = pending_sparks_hds[proc];
840 len++, prev = spark, spark = spark->next)
843 # if defined(GRAN_CHECK)
844 if ( RtsFlags.GranFlags.Debug.checkSparkQ )
845 if ( (prev!=NULL) && (prev!=pending_sparks_tls[proc]) )
846 debugBelch("ERROR in spark_queue_len: (PE %u) pending_sparks_tl (%p) not end of queue (%p)\n",
847 proc, pending_sparks_tls[proc], prev);
854 Take spark out of the spark queue on PE p and nuke the spark. Adjusts
855 hd and tl pointers of the spark queue. Returns a pointer to the next
859 delete_from_sparkq (spark, p, dispose_too) /* unlink and dispose spark */
867 barf("delete_from_sparkq: trying to delete NULL spark\n");
869 # if defined(GRAN_CHECK)
870 if ( RtsFlags.GranFlags.Debug.checkSparkQ ) {
871 debugBelch("## |%p:%p| (%p)<-spark=%p->(%p) <-(%p)\n",
872 pending_sparks_hd, pending_sparks_tl,
873 spark->prev, spark, spark->next,
874 (spark->next==NULL ? 0 : spark->next->prev));
878 if (spark->prev==NULL) {
879 /* spark is first spark of queue => adjust hd pointer */
880 ASSERT(pending_sparks_hds[p]==spark);
881 pending_sparks_hds[p] = spark->next;
883 spark->prev->next = spark->next;
885 if (spark->next==NULL) {
886 ASSERT(pending_sparks_tls[p]==spark);
887 /* spark is first spark of queue => adjust tl pointer */
888 pending_sparks_tls[p] = spark->prev;
890 spark->next->prev = spark->prev;
892 new_spark = spark->next;
894 # if defined(GRAN_CHECK)
895 if ( RtsFlags.GranFlags.Debug.checkSparkQ ) {
896 debugBelch("## |%p:%p| (%p)<-spark=%p->(%p) <-(%p); spark=%p will be deleted NOW \n",
897 pending_sparks_hd, pending_sparks_tl,
898 spark->prev, spark, spark->next,
899 (spark->next==NULL ? 0 : spark->next->prev), spark);
909 /* Mark all nodes pointed to by sparks in the spark queues (for GC) */
913 StgClosure *MarkRoot(StgClosure *root); // prototype
917 for (p=0; p<RtsFlags.GranFlags.proc; p++)
918 for (sp=pending_sparks_hds[p]; sp!=NULL; sp=sp->next) {
919 ASSERT(sp->node!=NULL);
920 ASSERT(LOOKS_LIKE_GHC_INFO(sp->node->header.info));
921 // ToDo?: statistics gathering here (also for GUM!)
922 sp->node = (StgClosure *)MarkRoot(sp->node);
926 debugBelch("markSparkQueue: spark statistics at start of GC:");
927 print_sparkq_stats());
937 debugBelch("Spark: NIL\n");
941 ((spark->node==NULL) ? "______" : "%#6lx"),
942 stgCast(StgPtr,spark->node));
944 debugBelch("Spark: Node %8s, Name %#6x, Global %5s, Creator %5x, Prev %6p, Next %6p\n",
946 ((spark->global)==rtsTrue?"True":"False"), spark->creator,
947 spark->prev, spark->next);
956 rtsSpark *x = pending_sparks_hds[proc];
958 debugBelch("Spark Queue of PE %d with root at %p:\n", proc, x);
959 for (; x!=(rtsSpark*)NULL; x=x->next) {
965 Print a statistics of all spark queues.
968 print_sparkq_stats(void)
972 debugBelch("SparkQs: [");
973 for (p=0; p<RtsFlags.GranFlags.proc; p++)
974 debugBelch(", PE %d: %d", p, spark_queue_len(p));