Title: [207566] trunk/Source
Revision
207566
Author
fpi...@apple.com
Date
2016-10-19 13:37:20 -0700 (Wed, 19 Oct 2016)

Log Message

Baseline JIT should use AutomaticThread
https://bugs.webkit.org/show_bug.cgi?id=163686

Reviewed by Geoffrey Garen.
        
Source/_javascript_Core:

Change the JITWorklist to use AutomaticThread, so that the Baseline JIT's concurrent
compiler thread shuts down automatically after inactivity.
        
With this change, all of JSC's threads shut down automatically. If you run splay for a few
seconds (which fires up all threads - compiler and GC) and then go to sleep for a second,
you'll see that the only threads left are the main thread and the bmalloc thread.

* jit/JITWorklist.cpp:
(JSC::JITWorklist::Thread::Thread):
(JSC::JITWorklist::JITWorklist):
(JSC::JITWorklist::completeAllForVM):
(JSC::JITWorklist::poll):
(JSC::JITWorklist::compileLater):
(JSC::JITWorklist::compileNow):
(JSC::JITWorklist::finalizePlans):
(JSC::JITWorklist::runThread): Deleted.
* jit/JITWorklist.h:

Source/WTF:

Added a AutomaticThreadCondition::wait() method, so that if you really want to use one
common condition for your thread and something else, you can do it. This trivially works
if you only use notifyAll(), and behaves as you'd expect for notifyOne() (i.e. it's
dangerous, since you don't know who will wake up).
        
The Baseline JIT used the one-true-Condition idiom because it used notifyAll() in an
optimal way: there are just two threads talking to each other, so it wakes up at most one
thread and that thread is exactly the one you want woken up. Adding wait() means that I did
not have to change that code.

* wtf/AutomaticThread.cpp:
(WTF::AutomaticThreadCondition::wait):
* wtf/AutomaticThread.h:

Modified Paths

Diff

Modified: trunk/Source/_javascript_Core/ChangeLog (207565 => 207566)


--- trunk/Source/_javascript_Core/ChangeLog	2016-10-19 20:34:14 UTC (rev 207565)
+++ trunk/Source/_javascript_Core/ChangeLog	2016-10-19 20:37:20 UTC (rev 207566)
@@ -1,3 +1,28 @@
+2016-10-19  Filip Pizlo  <fpi...@apple.com>
+
+        Baseline JIT should use AutomaticThread
+        https://bugs.webkit.org/show_bug.cgi?id=163686
+
+        Reviewed by Geoffrey Garen.
+        
+        Change the JITWorklist to use AutomaticThread, so that the Baseline JIT's concurrent
+        compiler thread shuts down automatically after inactivity.
+        
+        With this change, all of JSC's threads shut down automatically. If you run splay for a few
+        seconds (which fires up all threads - compiler and GC) and then go to sleep for a second,
+        you'll see that the only threads left are the main thread and the bmalloc thread.
+
+        * jit/JITWorklist.cpp:
+        (JSC::JITWorklist::Thread::Thread):
+        (JSC::JITWorklist::JITWorklist):
+        (JSC::JITWorklist::completeAllForVM):
+        (JSC::JITWorklist::poll):
+        (JSC::JITWorklist::compileLater):
+        (JSC::JITWorklist::compileNow):
+        (JSC::JITWorklist::finalizePlans):
+        (JSC::JITWorklist::runThread): Deleted.
+        * jit/JITWorklist.h:
+
 2016-10-19  Myles C. Maxfield  <mmaxfi...@apple.com>
 
         [macOS] [iOS] Disable variation fonts on macOS El Capitan and iOS 9

Modified: trunk/Source/_javascript_Core/jit/JITWorklist.cpp (207565 => 207566)


--- trunk/Source/_javascript_Core/jit/JITWorklist.cpp	2016-10-19 20:34:14 UTC (rev 207565)
+++ trunk/Source/_javascript_Core/jit/JITWorklist.cpp	2016-10-19 20:37:20 UTC (rev 207566)
@@ -97,9 +97,60 @@
     bool m_isFinishedCompiling { false };
 };
 
+class JITWorklist::Thread : public AutomaticThread {
+public:
+    Thread(const LockHolder& locker, JITWorklist& worklist)
+        : AutomaticThread(locker, worklist.m_lock, worklist.m_condition)
+        , m_worklist(worklist)
+    {
+        m_worklist.m_numAvailableThreads++;
+    }
+    
+protected:
+    PollResult poll(const LockHolder&) override
+    {
+        RELEASE_ASSERT(m_worklist.m_numAvailableThreads);
+        
+        if (m_worklist.m_queue.isEmpty())
+            return PollResult::Wait;
+        
+        m_myPlans = WTFMove(m_worklist.m_queue);
+        m_worklist.m_numAvailableThreads--;
+        return PollResult::Work;
+    }
+    
+    WorkResult work() override
+    {
+        RELEASE_ASSERT(!m_myPlans.isEmpty());
+        
+        for (RefPtr<Plan>& plan : m_myPlans) {
+            plan->compileInThread();
+            plan = nullptr;
+            
+            // Make sure that the main thread realizes that we just compiled something. Notifying
+            // a condition is basically free if nobody is waiting.
+            LockHolder locker(*m_worklist.m_lock);
+            m_worklist.m_condition->notifyAll(locker);
+        }
+        
+        m_myPlans.clear();
+        
+        LockHolder locker(*m_worklist.m_lock);
+        m_worklist.m_numAvailableThreads++;
+        return WorkResult::Continue;
+    }
+    
+private:
+    JITWorklist& m_worklist;
+    Plans m_myPlans;
+};
+
 JITWorklist::JITWorklist()
+    : m_lock(Box<Lock>::create())
+    , m_condition(AutomaticThreadCondition::create())
 {
-    createThread("JIT Worklist Worker Thread", [this] () { runThread(); });
+    LockHolder locker(*m_lock);
+    m_thread = new Thread(locker, *this);
 }
 
 JITWorklist::~JITWorklist()
@@ -113,7 +164,7 @@
     for (;;) {
         Vector<RefPtr<Plan>, 32> myPlans;
         {
-            LockHolder locker(m_lock);
+            LockHolder locker(*m_lock);
             for (;;) {
                 bool didFindUnfinishedPlan = false;
                 m_plans.removeAllMatching(
@@ -137,7 +188,7 @@
                 if (!didFindUnfinishedPlan)
                     return;
                 
-                m_condition.wait(m_lock);
+                m_condition->wait(*m_lock);
             }
         }
         
@@ -150,7 +201,7 @@
     DeferGC deferGC(vm.heap);
     Plans myPlans;
     {
-        LockHolder locker(m_lock);
+        LockHolder locker(*m_lock);
         m_plans.removeAllMatching(
             [&] (RefPtr<Plan>& plan) {
                 if (plan->vm() != &vm)
@@ -183,7 +234,7 @@
     codeBlock->jitSoon();
 
     {
-        LockHolder locker(m_lock);
+        LockHolder locker(*m_lock);
         
         if (m_planned.contains(codeBlock))
             return;
@@ -193,7 +244,7 @@
             RefPtr<Plan> plan = adoptRef(new Plan(codeBlock));
             m_plans.append(plan);
             m_queue.append(plan);
-            m_condition.notifyAll();
+            m_condition->notifyAll(locker);
             return;
         }
     }
@@ -225,7 +276,7 @@
     
     bool isPlanned;
     {
-        LockHolder locker(m_lock);
+        LockHolder locker(*m_lock);
         isPlanned = m_planned.contains(codeBlock);
     }
     
@@ -248,42 +299,12 @@
     codeBlock->ownerScriptExecutable()->installCode(codeBlock);
 }
 
-void JITWorklist::runThread()
-{
-    for (;;) {
-        Plans myPlans;
-        {
-            LockHolder locker(m_lock);
-            m_numAvailableThreads++;
-            while (m_queue.isEmpty())
-                m_condition.wait(m_lock);
-            m_numAvailableThreads--;
-            
-            // This is a fun way to dequeue. I don't know if it's any better or worse than dequeuing
-            // one thing at a time.
-            myPlans = WTFMove(m_queue);
-        }
-        
-        RELEASE_ASSERT(!myPlans.isEmpty());
-        
-        for (RefPtr<Plan>& plan : myPlans) {
-            plan->compileInThread();
-            plan = nullptr;
-            
-            // Make sure that the main thread realizes that we just compiled something. Notifying
-            // a WTF condition is basically free if nobody is waiting.
-            LockHolder locker(m_lock);
-            m_condition.notifyAll();
-        }
-    }
-}
-
 void JITWorklist::finalizePlans(Plans& myPlans)
 {
     for (RefPtr<Plan>& plan : myPlans) {
         plan->finalize();
         
-        LockHolder locker(m_lock);
+        LockHolder locker(*m_lock);
         m_planned.remove(plan->codeBlock());
     }
 }

Modified: trunk/Source/_javascript_Core/jit/JITWorklist.h (207565 => 207566)


--- trunk/Source/_javascript_Core/jit/JITWorklist.h	2016-10-19 20:34:14 UTC (rev 207565)
+++ trunk/Source/_javascript_Core/jit/JITWorklist.h	2016-10-19 20:37:20 UTC (rev 207566)
@@ -27,7 +27,7 @@
 
 #if ENABLE(JIT)
 
-#include <wtf/Condition.h>
+#include <wtf/AutomaticThread.h>
 #include <wtf/FastMalloc.h>
 #include <wtf/HashSet.h>
 #include <wtf/Lock.h>
@@ -62,7 +62,8 @@
 private:
     JITWorklist();
     
-    NO_RETURN void runThread();
+    class Thread;
+    friend class Thread;
     
     void finalizePlans(Plans&);
     
@@ -70,8 +71,9 @@
     Plans m_plans;
     HashSet<CodeBlock*> m_planned;
     
-    Lock m_lock;
-    Condition m_condition; // We use One True Condition for everything because that's easier.
+    Box<Lock> m_lock;
+    RefPtr<AutomaticThreadCondition> m_condition; // We use One True Condition for everything because that's easier.
+    RefPtr<AutomaticThread> m_thread;
     
     unsigned m_numAvailableThreads { 0 };
 };

Modified: trunk/Source/WTF/ChangeLog (207565 => 207566)


--- trunk/Source/WTF/ChangeLog	2016-10-19 20:34:14 UTC (rev 207565)
+++ trunk/Source/WTF/ChangeLog	2016-10-19 20:37:20 UTC (rev 207566)
@@ -1,3 +1,24 @@
+2016-10-19  Filip Pizlo  <fpi...@apple.com>
+
+        Baseline JIT should use AutomaticThread
+        https://bugs.webkit.org/show_bug.cgi?id=163686
+
+        Reviewed by Geoffrey Garen.
+        
+        Added a AutomaticThreadCondition::wait() method, so that if you really want to use one
+        common condition for your thread and something else, you can do it. This trivially works
+        if you only use notifyAll(), and behaves as you'd expect for notifyOne() (i.e. it's
+        dangerous, since you don't know who will wake up).
+        
+        The Baseline JIT used the one-true-Condition idiom because it used notifyAll() in an
+        optimal way: there are just two threads talking to each other, so it wakes up at most one
+        thread and that thread is exactly the one you want woken up. Adding wait() means that I did
+        not have to change that code.
+
+        * wtf/AutomaticThread.cpp:
+        (WTF::AutomaticThreadCondition::wait):
+        * wtf/AutomaticThread.h:
+
 2016-10-18  Filip Pizlo  <fpi...@apple.com>
 
         DFG worklist should use AutomaticThread

Modified: trunk/Source/WTF/wtf/AutomaticThread.cpp (207565 => 207566)


--- trunk/Source/WTF/wtf/AutomaticThread.cpp	2016-10-19 20:34:14 UTC (rev 207565)
+++ trunk/Source/WTF/wtf/AutomaticThread.cpp	2016-10-19 20:37:20 UTC (rev 207566)
@@ -65,6 +65,11 @@
     m_threads.clear();
 }
 
+void AutomaticThreadCondition::wait(Lock& lock)
+{
+    m_condition.wait(lock);
+}
+
 void AutomaticThreadCondition::add(const LockHolder&, AutomaticThread* thread)
 {
     ASSERT(!m_threads.contains(thread));

Modified: trunk/Source/WTF/wtf/AutomaticThread.h (207565 => 207566)


--- trunk/Source/WTF/wtf/AutomaticThread.h	2016-10-19 20:34:14 UTC (rev 207565)
+++ trunk/Source/WTF/wtf/AutomaticThread.h	2016-10-19 20:37:20 UTC (rev 207566)
@@ -78,6 +78,13 @@
     WTF_EXPORT_PRIVATE void notifyOne(const LockHolder&);
     WTF_EXPORT_PRIVATE void notifyAll(const LockHolder&);
     
+    // You can reuse this condition for other things, just as you would any other condition.
+    // However, since conflating conditions could lead to thundering herd, it's best to avoid it.
+    // One known-good case for one-true-condition is when the communication involves just two
+    // threads. In such cases, the thread doing the notifyAll() can wake up at most one thread -
+    // its partner.
+    WTF_EXPORT_PRIVATE void wait(Lock&);
+    
 private:
     friend class AutomaticThread;
     
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to