[commit: ghc] ghc-8.0: schedulePushWork: avoid unnecessary wakeups (ce60147)

git at git.haskell.org git at git.haskell.org
Thu Aug 25 16:37:07 UTC 2016


Repository : ssh://git@git.haskell.org/ghc

On branch  : ghc-8.0
Link       : http://ghc.haskell.org/trac/ghc/changeset/ce60147b77e585d8c1b2ae1d783bb1caeedbb198/ghc

>---------------------------------------------------------------

commit ce60147b77e585d8c1b2ae1d783bb1caeedbb198
Author: Simon Marlow <marlowsd at gmail.com>
Date:   Sun Apr 24 21:31:55 2016 +0100

    schedulePushWork: avoid unnecessary wakeups
    
    This function had some pathalogically bad behaviour: if we had 2 threads
    on the current capability and 23 other idle capabilities, we would
    
    * grab all 23 capabilities
    * migrate one Haskell thread to one of them
    * wake up a worker on *all* 23 other capabilities.
    
    This lead to a lot of unnecessary wakeups when using large -N values.
    
    Now, we
    
    * Count how many capabilities we need to wake up
    * Start from cap->no+1, so that we don't overload low-numbered capabilities
    * Only wake up capabilities that we migrated a thread to (unless we have
      sparks to steal)
    
    This results in a pretty dramatic improvement in our production system.
    
    (cherry picked from commit 1fa92ca9b1ed4cf44e2745830c9e9ccc2bee12d5)


>---------------------------------------------------------------

ce60147b77e585d8c1b2ae1d783bb1caeedbb198
 rts/Schedule.c | 32 +++++++++++++++++++++++++-------
 1 file changed, 25 insertions(+), 7 deletions(-)

diff --git a/rts/Schedule.c b/rts/Schedule.c
index 6c90e52..772748f 100644
--- a/rts/Schedule.c
+++ b/rts/Schedule.c
@@ -695,7 +695,8 @@ schedulePushWork(Capability *cap USED_IF_THREADS,
 #if defined(THREADED_RTS)
 
     Capability *free_caps[n_capabilities], *cap0;
-    nat i, n_free_caps;
+    nat i, n_wanted_caps, n_free_caps;
+    StgTSO *t;
 
     // migration can be turned off with +RTS -qm
     if (!RtsFlags.ParFlags.migrate) return;
@@ -709,8 +710,22 @@ schedulePushWork(Capability *cap USED_IF_THREADS,
             sparkPoolSizeCap(cap) < 1) return;
     }
 
-    // First grab as many free Capabilities as we can.
-    for (i=0, n_free_caps=0; i < n_capabilities; i++) {
+    // Figure out how many capabilities we want to wake up.  We need at least
+    // sparkPoolSize(cap) plus the number of spare threads we have.
+    t = cap->run_queue_hd;
+    n_wanted_caps = sparkPoolSizeCap(cap);
+    if (t != END_TSO_QUEUE) {
+        do {
+            t = t->_link;
+            if (t == END_TSO_QUEUE) break;
+            n_wanted_caps++;
+        } while (n_wanted_caps < n_capabilities-1);
+    }
+
+    // Grab free capabilities, starting from cap->no+1.
+    for (i = (cap->no + 1) % n_capabilities, n_free_caps=0;
+         n_free_caps < n_wanted_caps && i != cap->no;
+         i = (i + 1) % n_capabilities) {
         cap0 = capabilities[i];
         if (cap != cap0 && !cap0->disabled && tryGrabCapability(cap0,task)) {
             if (!emptyRunQueue(cap0)
@@ -820,10 +835,13 @@ schedulePushWork(Capability *cap USED_IF_THREADS,
         // release the capabilities
         for (i = 0; i < n_free_caps; i++) {
             task->cap = free_caps[i];
-            // The idea behind waking up the capability unconditionally is that
-            // it might be able to steal sparks.  Perhaps we should only do this
-            // if there were sparks to steal?
-            releaseAndWakeupCapability(free_caps[i]);
+            if (sparkPoolSizeCap(cap) > 0) {
+                // If we have sparks to steal, wake up a worker on the
+                // capability, even if it has no threads to run.
+                releaseAndWakeupCapability(free_caps[i]);
+            } else {
+                releaseCapability(free_caps[i]);
+            }
         }
     }
     task->cap = cap; // reset to point to our Capability.



More information about the ghc-commits mailing list