Earl Ou has uploaded this change for review. ( https://gem5-review.googlesource.com/c/public/gem5/+/34915 )

Change subject: base,sim: implement a faster mutex for single thread case
......................................................................

base,sim: implement a faster mutex for single thread case

This change applies an atomic variable to check if we really need to
obtain a mutex, and uses a condition variable to notify.

See about 5% improvement in the simulation speed.

Change-Id: I7e165987dcb587b27fae90978b9b3fde6f5563ef
---
A src/base/fast_mutex.hh
M src/sim/eventq.cc
M src/sim/eventq.hh
3 files changed, 86 insertions(+), 3 deletions(-)



diff --git a/src/base/fast_mutex.hh b/src/base/fast_mutex.hh
new file mode 100644
index 0000000..b003072
--- /dev/null
+++ b/src/base/fast_mutex.hh
@@ -0,0 +1,82 @@
+/*
+ * Copyright 2020 Google, Inc.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met: redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer;
+ * redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution;
+ * neither the name of the copyright holders nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef __BASE_FAST_MUTEXT_HH__
+#define __BASE_FAST_MUTEXT_HH__
+
+#include <atomic>
+#include <condition_variable>
+#include <mutex>
+
+/*
+ * The FastMutex uses an atomic flag to check if we really need to obtain a
+ * mutex lock. For most cases without multi-threads event queues, e.g. non-KVM
+ * simulation, this avoid the system call and speed up the simulation.
+ */
+class FastMutex {
+  private:
+    // A flag to record the current status:
+    // 0: no one has the lock
+    // 1: exactly one thread has the lock
+    // >1: more than one threads are waiting for the lock.
+    std::atomic<int> flag;
+    std::mutex m;
+    std::condition_variable cv;
+
+    bool test_and_set(int expected, int desired) {
+        return flag.compare_exchange_strong(expected, desired);
+    }
+
+  public:
+    FastMutex() : flag(0) {}
+
+    void
+    lock()
+    {
+        while (!test_and_set(0, 1)) {
+            std::unique_lock<std::mutex> ul(m);
+            if (flag++ == 0)  // in case flag is set to 0 before we get m.
+                break;
+            cv.wait(ul);
+        }
+    }
+
+    void
+    unlock()
+    {
+        if (test_and_set(1, 0))
+            return;
+
+        {
+            std::lock_guard<std::mutex> g(m);
+            flag = 0;
+        }
+        cv.notify_all();
+    }
+};
+
+#endif // __BASE_FAST_MUTEXT_HH__
diff --git a/src/sim/eventq.cc b/src/sim/eventq.cc
index bc4864c..adce51e 100644
--- a/src/sim/eventq.cc
+++ b/src/sim/eventq.cc
@@ -32,6 +32,7 @@

 #include <cassert>
 #include <iostream>
+#include <mutex>
 #include <string>
 #include <unordered_map>
 #include <vector>
diff --git a/src/sim/eventq.hh b/src/sim/eventq.hh
index aa54722..ecd9b78 100644
--- a/src/sim/eventq.hh
+++ b/src/sim/eventq.hh
@@ -41,10 +41,10 @@
 #include <functional>
 #include <iosfwd>
 #include <memory>
-#include <mutex>
 #include <string>

 #include "base/debug.hh"
+#include "base/fast_mutex.hh"
 #include "base/flags.hh"
 #include "base/types.hh"
 #include "debug/Event.hh"
@@ -622,7 +622,7 @@
     Tick _curTick;

     //! Mutex to protect async queue.
-    std::mutex async_queue_mutex;
+    FastMutex async_queue_mutex;

     //! List of events added by other threads to this event queue.
     std::list<Event*> async_queue;
@@ -647,7 +647,7 @@
      * @see EventQueue::lock()
      * @see EventQueue::unlock()
      */
-    std::mutex service_mutex;
+    FastMutex service_mutex;

     //! Insert / remove event from the queue. Should only be called
     //! by thread operating this queue.

--
To view, visit https://gem5-review.googlesource.com/c/public/gem5/+/34915
To unsubscribe, or for help writing mail filters, visit https://gem5-review.googlesource.com/settings

Gerrit-Project: public/gem5
Gerrit-Branch: develop
Gerrit-Change-Id: I7e165987dcb587b27fae90978b9b3fde6f5563ef
Gerrit-Change-Number: 34915
Gerrit-PatchSet: 1
Gerrit-Owner: Earl Ou <[email protected]>
Gerrit-MessageType: newchange
_______________________________________________
gem5-dev mailing list -- [email protected]
To unsubscribe send an email to [email protected]
%(web_page_url)slistinfo%(cgiext)s/%(_internal_name)s

Reply via email to