ilya-biryukov created this revision.
Herald added subscribers: cfe-commits, kadircet, jfb, arphaman, jkorous, 
MaskRay, ioeric.

Useful in getting some stats of a contention on a particular mutex.
Currently it only reports the sum of wall time taken by all threads
waiting on a mutex.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D55815

Files:
  clangd/Threading.cpp
  clangd/Threading.h


Index: clangd/Threading.h
===================================================================
--- clangd/Threading.h
+++ clangd/Threading.h
@@ -13,7 +13,9 @@
 #include "Context.h"
 #include "Function.h"
 #include "llvm/ADT/Twine.h"
+#include <atomic>
 #include <cassert>
+#include <chrono>
 #include <condition_variable>
 #include <memory>
 #include <mutex>
@@ -118,6 +120,18 @@
   std::size_t InFlightTasks = 0;
 };
 
+class Mutex {
+public:
+  ~Mutex();
+
+  void lock();
+  void unlock();
+
+private:
+  std::mutex Mut;
+  std::chrono::steady_clock::duration WaitAllCPUs;
+};
+
 enum class ThreadPriority {
   Low = 0,
   Normal = 1,
Index: clangd/Threading.cpp
===================================================================
--- clangd/Threading.cpp
+++ clangd/Threading.cpp
@@ -1,9 +1,11 @@
 #include "Threading.h"
+#include "Logger.h"
 #include "Trace.h"
 #include "llvm/ADT/ScopeExit.h"
 #include "llvm/Support/FormatVariadic.h"
 #include "llvm/Support/Threading.h"
 #include <atomic>
+#include <chrono>
 #include <thread>
 #ifdef __USE_POSIX
 #include <pthread.h>
@@ -127,5 +129,23 @@
 
 void preventThreadStarvationInTests() { AvoidThreadStarvation = true; }
 
+Mutex::~Mutex() {
+  log("Lock '{0}' was contended for {1} ms.", this,
+      
std::chrono::duration_cast<std::chrono::milliseconds>(WaitAllCPUs).count());
+}
+
+void Mutex::lock() {
+  if (Mut.try_lock())
+    return;
+
+  auto Start = std::chrono::steady_clock::now();
+  Mut.lock();
+  WaitAllCPUs += std::chrono::steady_clock::now() - Start;
+}
+
+void Mutex::unlock() {
+  Mut.unlock();
+}
+
 } // namespace clangd
 } // namespace clang


Index: clangd/Threading.h
===================================================================
--- clangd/Threading.h
+++ clangd/Threading.h
@@ -13,7 +13,9 @@
 #include "Context.h"
 #include "Function.h"
 #include "llvm/ADT/Twine.h"
+#include <atomic>
 #include <cassert>
+#include <chrono>
 #include <condition_variable>
 #include <memory>
 #include <mutex>
@@ -118,6 +120,18 @@
   std::size_t InFlightTasks = 0;
 };
 
+class Mutex {
+public:
+  ~Mutex();
+
+  void lock();
+  void unlock();
+
+private:
+  std::mutex Mut;
+  std::chrono::steady_clock::duration WaitAllCPUs;
+};
+
 enum class ThreadPriority {
   Low = 0,
   Normal = 1,
Index: clangd/Threading.cpp
===================================================================
--- clangd/Threading.cpp
+++ clangd/Threading.cpp
@@ -1,9 +1,11 @@
 #include "Threading.h"
+#include "Logger.h"
 #include "Trace.h"
 #include "llvm/ADT/ScopeExit.h"
 #include "llvm/Support/FormatVariadic.h"
 #include "llvm/Support/Threading.h"
 #include <atomic>
+#include <chrono>
 #include <thread>
 #ifdef __USE_POSIX
 #include <pthread.h>
@@ -127,5 +129,23 @@
 
 void preventThreadStarvationInTests() { AvoidThreadStarvation = true; }
 
+Mutex::~Mutex() {
+  log("Lock '{0}' was contended for {1} ms.", this,
+      std::chrono::duration_cast<std::chrono::milliseconds>(WaitAllCPUs).count());
+}
+
+void Mutex::lock() {
+  if (Mut.try_lock())
+    return;
+
+  auto Start = std::chrono::steady_clock::now();
+  Mut.lock();
+  WaitAllCPUs += std::chrono::steady_clock::now() - Start;
+}
+
+void Mutex::unlock() {
+  Mut.unlock();
+}
+
 } // namespace clangd
 } // namespace clang
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to