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 appendToRunQueue(cap,tso);
64 /* --------------------------------------------------------------------------
65 * newSpark: create a new spark, as a result of calling "par"
66 * Called directly from STG.
67 * -------------------------------------------------------------------------- */
70 newSpark (StgRegTable *reg, StgClosure *p)
72 Capability *cap = regTableToCapability(reg);
73 SparkPool *pool = cap->sparks;
75 /* I am not sure whether this is the right thing to do.
76 * Maybe it is better to exploit the tag information
77 * instead of throwing it away?
81 if (closure_SHOULD_SPARK(p)) {
85 cap->sparks_created++;
87 postEvent(cap, EVENT_CREATE_SPARK, reg->rCurrentTSO->id, 0);
92 /* -----------------------------------------------------------------------------
94 * tryStealSpark: try to steal a spark from a Capability.
96 * Returns a valid spark, or NULL if the pool was empty, and can
97 * occasionally return NULL if there was a race with another thread
98 * stealing from the same pool. In this case, try again later.
100 -------------------------------------------------------------------------- */
103 tryStealSpark (Capability *cap)
105 SparkPool *pool = cap->sparks;
109 stolen = stealWSDeque_(pool);
110 // use the no-loopy version, stealWSDeque_(), since if we get a
111 // spurious NULL here the caller may want to try stealing from
112 // other pools before trying again.
113 } while (stolen != NULL && !closure_SHOULD_SPARK(stolen));
118 /* --------------------------------------------------------------------------
119 * Remove all sparks from the spark queues which should not spark any
120 * more. Called after GC. We assume exclusive access to the structure
121 * and replace all sparks in the queue, see explanation below. At exit,
122 * the spark pool only contains sparkable closures.
123 * -------------------------------------------------------------------------- */
126 pruneSparkQueue (evac_fn evac, void *user, Capability *cap)
129 StgClosurePtr spark, tmp, *elements;
130 nat n, pruned_sparks; // stats only
131 StgWord botInd,oldBotInd,currInd; // indices in array (always < size)
132 const StgInfoTable *info;
134 PAR_TICKY_MARK_SPARK_QUEUE_START();
141 // it is possible that top > bottom, indicating an empty pool. We
142 // fix that here; this is only necessary because the loop below
144 if (pool->top > pool->bottom)
145 pool->top = pool->bottom;
147 // Take this opportunity to reset top/bottom modulo the size of
148 // the array, to avoid overflow. This is only possible because no
149 // stealing is happening during GC.
150 pool->bottom -= pool->top & ~pool->moduloSize;
151 pool->top &= pool->moduloSize;
152 pool->topBound = pool->top;
154 debugTrace(DEBUG_sched,
155 "markSparkQueue: current spark queue len=%ld; (hd=%ld; tl=%ld)",
156 sparkPoolSize(pool), pool->bottom, pool->top);
158 ASSERT_WSDEQUE_INVARIANTS(pool);
160 elements = (StgClosurePtr *)pool->elements;
162 /* We have exclusive access to the structure here, so we can reset
163 bottom and top counters, and prune invalid sparks. Contents are
164 copied in-place if they are valuable, otherwise discarded. The
165 routine uses "real" indices t and b, starts by computing them
166 as the modulus size of top and bottom,
170 At the beginning, the pool structure can look like this:
171 ( bottom % size >= top % size , no wrap-around)
173 ___________***********_________________
175 or like this ( bottom % size < top % size, wrap-around )
177 ***********__________******************
178 As we need to remove useless sparks anyway, we make one pass
179 between t and b, moving valuable content to b and subsequent
180 cells (wrapping around when the size is reached).
183 ***********OOO_______XX_X__X?**********
186 After this movement, botInd becomes the new bottom, and old
187 bottom becomes the new top index, both as indices in the array
191 currInd = (pool->top) & (pool->moduloSize); // mod
193 // copies of evacuated closures go to space from botInd on
194 // we keep oldBotInd to know when to stop
195 oldBotInd = botInd = (pool->bottom) & (pool->moduloSize); // mod
197 // on entry to loop, we are within the bounds
198 ASSERT( currInd < pool->size && botInd < pool->size );
200 while (currInd != oldBotInd ) {
201 /* must use != here, wrap-around at size
202 subtle: loop not entered if queue empty
205 /* check element at currInd. if valuable, evacuate and move to
206 botInd, otherwise move on */
207 spark = elements[currInd];
209 // We have to be careful here: in the parallel GC, another
210 // thread might evacuate this closure while we're looking at it,
211 // so grab the info pointer just once.
212 info = spark->header.info;
213 if (IS_FORWARDING_PTR(info)) {
214 tmp = (StgClosure*)UN_FORWARDING_PTR(info);
215 /* if valuable work: shift inside the pool */
216 if (closure_SHOULD_SPARK(tmp)) {
217 elements[botInd] = tmp; // keep entry (new address)
221 pruned_sparks++; // discard spark
222 cap->sparks_pruned++;
225 if (!(closure_flags[INFO_PTR_TO_STRUCT(info)->type] & _NS)) {
226 elements[botInd] = spark; // keep entry (new address)
227 evac (user, &elements[botInd]);
231 pruned_sparks++; // discard spark
232 cap->sparks_pruned++;
237 // in the loop, we may reach the bounds, and instantly wrap around
238 ASSERT( currInd <= pool->size && botInd <= pool->size );
239 if ( currInd == pool->size ) { currInd = 0; }
240 if ( botInd == pool->size ) { botInd = 0; }
242 } // while-loop over spark pool elements
244 ASSERT(currInd == oldBotInd);
246 pool->top = oldBotInd; // where we started writing
247 pool->topBound = pool->top;
249 pool->bottom = (oldBotInd <= botInd) ? botInd : (botInd + pool->size);
250 // first free place we did not use (corrected by wraparound)
252 PAR_TICKY_MARK_SPARK_QUEUE_END(n);
254 debugTrace(DEBUG_sched, "pruned %d sparks", pruned_sparks);
256 debugTrace(DEBUG_sched,
257 "new spark queue len=%ld; (hd=%ld; tl=%ld)",
258 sparkPoolSize(pool), pool->bottom, pool->top);
260 ASSERT_WSDEQUE_INVARIANTS(pool);
263 /* GC for the spark pool, called inside Capability.c for all
264 capabilities in turn. Blindly "evac"s complete spark pool. */
266 traverseSparkQueue (evac_fn evac, void *user, Capability *cap)
270 StgWord top,bottom, modMask;
274 ASSERT_WSDEQUE_INVARIANTS(pool);
277 bottom = pool->bottom;
278 sparkp = (StgClosurePtr*)pool->elements;
279 modMask = pool->moduloSize;
281 while (top < bottom) {
282 /* call evac for all closures in range (wrap-around via modulo)
283 * In GHC-6.10, evac takes an additional 1st argument to hold a
284 * GC-specific register, see rts/sm/GC.c::mark_root()
286 evac( user , sparkp + (top & modMask) );
290 debugTrace(DEBUG_sched,
291 "traversed spark queue, len=%ld; (hd=%ld; tl=%ld)",
292 sparkPoolSize(pool), pool->bottom, pool->top);
295 /* ----------------------------------------------------------------------------
296 * balanceSparkPoolsCaps: takes an array of capabilities (usually: all
297 * capabilities) and its size. Accesses all spark pools and equally
298 * distributes the sparks among them.
300 * Could be called after GC, before Cap. release, from scheduler.
301 * -------------------------------------------------------------------------- */
302 void balanceSparkPoolsCaps(nat n_caps, Capability caps[]);
304 void balanceSparkPoolsCaps(nat n_caps STG_UNUSED,
305 Capability caps[] STG_UNUSED) {
306 barf("not implemented");
312 newSpark (StgRegTable *reg STG_UNUSED, StgClosure *p STG_UNUSED)
319 #endif /* PARALLEL_HASKELL || THREADED_RTS */
322 /* -----------------------------------------------------------------------------
324 * GRAN & PARALLEL_HASKELL stuff beyond here.
328 * -------------------------------------------------------------------------- */
330 #if defined(PARALLEL_HASKELL) || defined(GRAN)
332 static void slide_spark_pool( StgSparkPool *pool );
335 add_to_spark_queue( StgClosure *closure, StgSparkPool *pool )
337 if (pool->tl == pool->lim)
338 slide_spark_pool(pool);
340 if (closure_SHOULD_SPARK(closure) &&
341 pool->tl < pool->lim) {
342 *(pool->tl++) = closure;
344 #if defined(PARALLEL_HASKELL)
345 // collect parallel global statistics (currently done together with GC stats)
346 if (RtsFlags.ParFlags.ParStats.Global &&
347 RtsFlags.GcFlags.giveStats > NO_GC_STATS) {
348 // debugBelch("Creating spark for %x @ %11.2f\n", closure, usertime());
349 globalParStats.tot_sparks_created++;
354 #if defined(PARALLEL_HASKELL)
355 // collect parallel global statistics (currently done together with GC stats)
356 if (RtsFlags.ParFlags.ParStats.Global &&
357 RtsFlags.GcFlags.giveStats > NO_GC_STATS) {
358 //debugBelch("Ignoring spark for %x @ %11.2f\n", closure, usertime());
359 globalParStats.tot_sparks_ignored++;
367 slide_spark_pool( StgSparkPool *pool )
369 StgClosure **sparkp, **to_sparkp;
372 to_sparkp = pool->base;
373 while (sparkp < pool->tl) {
374 ASSERT(to_sparkp<=sparkp);
375 ASSERT(*sparkp!=NULL);
376 ASSERT(LOOKS_LIKE_GHC_INFO((*sparkp)->header.info));
378 if (closure_SHOULD_SPARK(*sparkp)) {
379 *to_sparkp++ = *sparkp++;
384 pool->hd = pool->base;
385 pool->tl = to_sparkp;
392 #if !defined(THREADED_RTS)
397 pool = &(cap->rSparks);
398 ASSERT(pool->hd <= pool->tl && pool->tl <= pool->lim);
400 ASSERT(spark != (StgClosure *)NULL);
408 Search the spark queue of the proc in event for a spark that's worth
409 turning into a thread
410 (was gimme_spark in the old RTS)
413 findLocalSpark (rtsEvent *event, rtsBool *found_res, rtsSparkQ *spark_res)
415 PEs proc = event->proc, /* proc to search for work */
416 creator = event->creator; /* proc that requested work */
419 rtsSparkQ spark_of_non_local_node = NULL,
420 spark_of_non_local_node_prev = NULL,
421 low_priority_spark = NULL,
422 low_priority_spark_prev = NULL,
423 spark = NULL, prev = NULL;
425 /* Choose a spark from the local spark queue */
426 prev = (rtsSpark*)NULL;
427 spark = pending_sparks_hds[proc];
430 // ToDo: check this code & implement local sparking !! -- HWL
431 while (!found && spark != (rtsSpark*)NULL)
433 ASSERT((prev!=(rtsSpark*)NULL || spark==pending_sparks_hds[proc]) &&
434 (prev==(rtsSpark*)NULL || prev->next==spark) &&
435 (spark->prev==prev));
437 if (!closure_SHOULD_SPARK(node))
439 IF_GRAN_DEBUG(checkSparkQ,
440 debugBelch("^^ pruning spark %p (node %p) in gimme_spark",
443 if (RtsFlags.GranFlags.GranSimStats.Sparks)
444 DumpRawGranEvent(proc, (PEs)0, SP_PRUNED,(StgTSO*)NULL,
445 spark->node, spark->name, spark_queue_len(proc));
447 ASSERT(spark != (rtsSpark*)NULL);
448 ASSERT(SparksAvail>0);
451 ASSERT(prev==(rtsSpark*)NULL || prev->next==spark);
452 spark = delete_from_sparkq (spark, proc, rtsTrue);
453 if (spark != (rtsSpark*)NULL)
457 /* -- node should eventually be sparked */
458 else if (RtsFlags.GranFlags.PreferSparksOfLocalNodes &&
459 !IS_LOCAL_TO(PROCS(node),CurrentProc))
461 barf("Local sparking not yet implemented");
463 /* Remember first low priority spark */
464 if (spark_of_non_local_node==(rtsSpark*)NULL) {
465 spark_of_non_local_node_prev = prev;
466 spark_of_non_local_node = spark;
469 if (spark->next == (rtsSpark*)NULL) {
470 /* ASSERT(spark==SparkQueueTl); just for testing */
471 prev = spark_of_non_local_node_prev;
472 spark = spark_of_non_local_node;
477 # if defined(GRAN) && defined(GRAN_CHECK)
478 /* Should never happen; just for testing
479 if (spark==pending_sparks_tl) {
480 debugBelch("ReSchedule: Last spark != SparkQueueTl\n");
481 stg_exit(EXIT_FAILURE);
486 ASSERT(SparksAvail>0);
490 else if ( RtsFlags.GranFlags.DoPrioritySparking ||
491 (spark->gran_info >= RtsFlags.GranFlags.SparkPriority2) )
493 if (RtsFlags.GranFlags.DoPrioritySparking)
494 barf("Priority sparking not yet implemented");
499 else /* only used if SparkPriority2 is defined */
501 /* ToDo: fix the code below and re-integrate it */
502 /* Remember first low priority spark */
503 if (low_priority_spark==(rtsSpark*)NULL) {
504 low_priority_spark_prev = prev;
505 low_priority_spark = spark;
508 if (spark->next == (rtsSpark*)NULL) {
509 /* ASSERT(spark==spark_queue_tl); just for testing */
510 prev = low_priority_spark_prev;
511 spark = low_priority_spark;
512 found = rtsTrue; /* take low pri spark => rc is 2 */
516 /* Should never happen; just for testing
517 if (spark==pending_sparks_tl) {
518 debugBelch("ReSchedule: Last spark != SparkQueueTl\n");
519 stg_exit(EXIT_FAILURE);
526 debugBelch("++ Ignoring spark of priority %u (SparkPriority=%u); node=%p; name=%u\n",
527 spark->gran_info, RtsFlags.GranFlags.SparkPriority,
528 spark->node, spark->name);)
531 } /* while (spark!=NULL && !found) */
538 Turn the spark into a thread.
539 In GranSim this basically means scheduling a StartThread event for the
540 node pointed to by the spark at some point in the future.
541 (was munch_spark in the old RTS)
544 activateSpark (rtsEvent *event, rtsSparkQ spark)
546 PEs proc = event->proc, /* proc to search for work */
547 creator = event->creator; /* proc that requested work */
550 rtsTime spark_arrival_time;
553 We've found a node on PE proc requested by PE creator.
554 If proc==creator we can turn the spark into a thread immediately;
555 otherwise we schedule a MoveSpark event on the requesting PE
561 /* only possible if we simulate GUM style fishing */
562 ASSERT(RtsFlags.GranFlags.Fishing);
564 /* Message packing costs for sending a Fish; qeq jabbI'ID */
565 CurrentTime[proc] += RtsFlags.GranFlags.Costs.mpacktime;
567 if (RtsFlags.GranFlags.GranSimStats.Sparks)
568 DumpRawGranEvent(proc, (PEs)0, SP_EXPORTED,
569 (StgTSO*)NULL, spark->node,
570 spark->name, spark_queue_len(proc));
572 /* time of the spark arrival on the remote PE */
573 spark_arrival_time = CurrentTime[proc] + RtsFlags.GranFlags.Costs.latency;
575 new_event(creator, proc, spark_arrival_time,
577 (StgTSO*)NULL, spark->node, spark);
579 CurrentTime[proc] += RtsFlags.GranFlags.Costs.mtidytime;
581 } else { /* proc==creator i.e. turn the spark into a thread */
583 if ( RtsFlags.GranFlags.GranSimStats.Global &&
584 spark->gran_info < RtsFlags.GranFlags.SparkPriority2 ) {
586 globalGranStats.tot_low_pri_sparks++;
588 debugBelch("++ No high priority spark available; low priority (%u) spark chosen: node=%p; name=%u\n",
590 spark->node, spark->name));
593 CurrentTime[proc] += RtsFlags.GranFlags.Costs.threadcreatetime;
598 /* ToDo: fix the GC interface and move to StartThread handling-- HWL */
599 if (GARBAGE COLLECTION IS NECESSARY) {
600 /* Some kind of backoff needed here in case there's too little heap */
601 # if defined(GRAN_CHECK) && defined(GRAN)
602 if (RtsFlags.GcFlags.giveStats)
603 fprintf(RtsFlags.GcFlags.statsFile,"***** vIS Qu' chen veQ boSwI'; spark=%p, node=%p; name=%u\n",
604 /* (found==2 ? "no hi pri spark" : "hi pri spark"), */
605 spark, node, spark->name);
607 new_event(CurrentProc, CurrentProc, CurrentTime[CurrentProc]+1,
609 (StgTSO*)NULL, (StgClosure*)NULL, (rtsSpark*)NULL);
610 barf("//// activateSpark: out of heap ; ToDo: call GarbageCollect()");
611 GarbageCollect(GetRoots, rtsFalse);
612 // HWL old: ReallyPerformThreadGC(TSO_HS+TSO_CTS_SIZE,rtsFalse);
613 // HWL old: SAVE_Hp -= TSO_HS+TSO_CTS_SIZE;
615 return; /* was: continue; */ /* to the next event, eventually */
619 if (RtsFlags.GranFlags.GranSimStats.Sparks)
620 DumpRawGranEvent(CurrentProc,(PEs)0,SP_USED,(StgTSO*)NULL,
621 spark->node, spark->name,
622 spark_queue_len(CurrentProc));
624 new_event(proc, proc, CurrentTime[proc],
626 END_TSO_QUEUE, node, spark); // (rtsSpark*)NULL);
628 procStatus[proc] = Starting;
632 /* -------------------------------------------------------------------------
633 This is the main point where handling granularity information comes into
635 ------------------------------------------------------------------------- */
637 #define MAX_RAND_PRI 100
640 Granularity info transformers.
641 Applied to the GRAN_INFO field of a spark.
643 STATIC_INLINE nat ID(nat x) { return(x); };
644 STATIC_INLINE nat INV(nat x) { return(-x); };
645 STATIC_INLINE nat IGNORE(nat x) { return (0); };
646 STATIC_INLINE nat RAND(nat x) { return ((random() % MAX_RAND_PRI) + 1); }
648 /* NB: size_info and par_info are currently unused (what a shame!) -- HWL */
650 newSpark(node,name,gran_info,size_info,par_info,local)
652 nat name, gran_info, size_info, par_info, local;
657 pri = RtsFlags.GranFlags.RandomPriorities ? RAND(gran_info) :
658 RtsFlags.GranFlags.InversePriorities ? INV(gran_info) :
659 RtsFlags.GranFlags.IgnorePriorities ? IGNORE(gran_info) :
662 if ( RtsFlags.GranFlags.SparkPriority!=0 &&
663 pri<RtsFlags.GranFlags.SparkPriority ) {
665 debugBelch(",, NewSpark: Ignoring spark of priority %u (SparkPriority=%u); node=%#x; name=%u\n",
666 pri, RtsFlags.GranFlags.SparkPriority, node, name));
667 return ((rtsSpark*)NULL);
670 newspark = (rtsSpark*) stgMallocBytes(sizeof(rtsSpark), "NewSpark");
671 newspark->prev = newspark->next = (rtsSpark*)NULL;
672 newspark->node = node;
673 newspark->name = (name==1) ? CurrentTSO->gran.sparkname : name;
674 newspark->gran_info = pri;
675 newspark->global = !local; /* Check that with parAt, parAtAbs !!*/
677 if (RtsFlags.GranFlags.GranSimStats.Global) {
678 globalGranStats.tot_sparks_created++;
679 globalGranStats.sparks_created_on_PE[CurrentProc]++;
700 disposeSparkQ(spark->next);
703 if (SparksAvail < 0) {
704 debugBelch("disposeSparkQ: SparksAvail<0 after disposing sparkq @ %p\n", &spark);
713 With PrioritySparking add_to_spark_queue performs an insert sort to keep
714 the spark queue sorted. Otherwise the spark is just added to the end of
719 add_to_spark_queue(spark)
722 rtsSpark *prev = NULL, *next = NULL;
724 rtsBool found = rtsFalse;
726 if ( spark == (rtsSpark *)NULL ) {
730 if (RtsFlags.GranFlags.DoPrioritySparking && (spark->gran_info != 0) ) {
731 /* Priority sparking is enabled i.e. spark queues must be sorted */
733 for (prev = NULL, next = pending_sparks_hd, count=0;
735 !(found = (spark->gran_info >= next->gran_info));
736 prev = next, next = next->next, count++)
739 } else { /* 'utQo' */
740 /* Priority sparking is disabled */
742 found = rtsFalse; /* to add it at the end */
747 /* next points to the first spark with a gran_info smaller than that
748 of spark; therefore, add spark before next into the spark queue */
750 if ( next == NULL ) {
751 pending_sparks_tl = spark;
756 if ( prev == NULL ) {
757 pending_sparks_hd = spark;
761 } else { /* (RtsFlags.GranFlags.DoPrioritySparking && !found) || !DoPrioritySparking */
762 /* add the spark at the end of the spark queue */
764 spark->prev = pending_sparks_tl;
765 if (pending_sparks_hd == NULL)
766 pending_sparks_hd = spark;
768 pending_sparks_tl->next = spark;
769 pending_sparks_tl = spark;
773 /* add costs for search in priority sparking */
774 if (RtsFlags.GranFlags.DoPrioritySparking) {
775 CurrentTime[CurrentProc] += count * RtsFlags.GranFlags.Costs.pri_spark_overhead;
778 IF_GRAN_DEBUG(checkSparkQ,
779 debugBelch("++ Spark stats after adding spark %p (node %p) to queue on PE %d",
780 spark, spark->node, CurrentProc);
781 print_sparkq_stats());
783 # if defined(GRAN_CHECK)
784 if (RtsFlags.GranFlags.Debug.checkSparkQ) {
785 for (prev = NULL, next = pending_sparks_hd;
787 prev = next, next = next->next)
789 if ( (prev!=NULL) && (prev!=pending_sparks_tl) )
790 debugBelch("SparkQ inconsistency after adding spark %p: (PE %u) pending_sparks_tl (%p) not end of queue (%p)\n",
792 pending_sparks_tl, prev);
796 # if defined(GRAN_CHECK)
797 /* Check if the sparkq is still sorted. Just for testing, really! */
798 if ( RtsFlags.GranFlags.Debug.checkSparkQ &&
799 RtsFlags.GranFlags.Debug.pri ) {
800 rtsBool sorted = rtsTrue;
801 rtsSpark *prev, *next;
803 if (pending_sparks_hd == NULL ||
804 pending_sparks_hd->next == NULL ) {
805 /* just 1 elem => ok */
807 for (prev = pending_sparks_hd,
808 next = pending_sparks_hd->next;
810 prev = next, next = next->next) {
812 (prev->gran_info >= next->gran_info);
816 debugBelch("ghuH: SPARKQ on PE %d is not sorted:\n",
818 print_sparkq(CurrentProc);
825 spark_queue_len(proc)
828 rtsSpark *prev, *spark; /* prev only for testing !! */
831 for (len = 0, prev = NULL, spark = pending_sparks_hds[proc];
833 len++, prev = spark, spark = spark->next)
836 # if defined(GRAN_CHECK)
837 if ( RtsFlags.GranFlags.Debug.checkSparkQ )
838 if ( (prev!=NULL) && (prev!=pending_sparks_tls[proc]) )
839 debugBelch("ERROR in spark_queue_len: (PE %u) pending_sparks_tl (%p) not end of queue (%p)\n",
840 proc, pending_sparks_tls[proc], prev);
847 Take spark out of the spark queue on PE p and nuke the spark. Adjusts
848 hd and tl pointers of the spark queue. Returns a pointer to the next
852 delete_from_sparkq (spark, p, dispose_too) /* unlink and dispose spark */
860 barf("delete_from_sparkq: trying to delete NULL spark\n");
862 # if defined(GRAN_CHECK)
863 if ( RtsFlags.GranFlags.Debug.checkSparkQ ) {
864 debugBelch("## |%p:%p| (%p)<-spark=%p->(%p) <-(%p)\n",
865 pending_sparks_hd, pending_sparks_tl,
866 spark->prev, spark, spark->next,
867 (spark->next==NULL ? 0 : spark->next->prev));
871 if (spark->prev==NULL) {
872 /* spark is first spark of queue => adjust hd pointer */
873 ASSERT(pending_sparks_hds[p]==spark);
874 pending_sparks_hds[p] = spark->next;
876 spark->prev->next = spark->next;
878 if (spark->next==NULL) {
879 ASSERT(pending_sparks_tls[p]==spark);
880 /* spark is first spark of queue => adjust tl pointer */
881 pending_sparks_tls[p] = spark->prev;
883 spark->next->prev = spark->prev;
885 new_spark = spark->next;
887 # if defined(GRAN_CHECK)
888 if ( RtsFlags.GranFlags.Debug.checkSparkQ ) {
889 debugBelch("## |%p:%p| (%p)<-spark=%p->(%p) <-(%p); spark=%p will be deleted NOW \n",
890 pending_sparks_hd, pending_sparks_tl,
891 spark->prev, spark, spark->next,
892 (spark->next==NULL ? 0 : spark->next->prev), spark);
902 /* Mark all nodes pointed to by sparks in the spark queues (for GC) */
906 StgClosure *MarkRoot(StgClosure *root); // prototype
910 for (p=0; p<RtsFlags.GranFlags.proc; p++)
911 for (sp=pending_sparks_hds[p]; sp!=NULL; sp=sp->next) {
912 ASSERT(sp->node!=NULL);
913 ASSERT(LOOKS_LIKE_GHC_INFO(sp->node->header.info));
914 // ToDo?: statistics gathering here (also for GUM!)
915 sp->node = (StgClosure *)MarkRoot(sp->node);
919 debugBelch("markSparkQueue: spark statistics at start of GC:");
920 print_sparkq_stats());
930 debugBelch("Spark: NIL\n");
934 ((spark->node==NULL) ? "______" : "%#6lx"),
935 stgCast(StgPtr,spark->node));
937 debugBelch("Spark: Node %8s, Name %#6x, Global %5s, Creator %5x, Prev %6p, Next %6p\n",
939 ((spark->global)==rtsTrue?"True":"False"), spark->creator,
940 spark->prev, spark->next);
949 rtsSpark *x = pending_sparks_hds[proc];
951 debugBelch("Spark Queue of PE %d with root at %p:\n", proc, x);
952 for (; x!=(rtsSpark*)NULL; x=x->next) {
958 Print a statistics of all spark queues.
961 print_sparkq_stats(void)
965 debugBelch("SparkQs: [");
966 for (p=0; p<RtsFlags.GranFlags.proc; p++)
967 debugBelch(", PE %d: %d", p, spark_queue_len(p));