[PATCH] D55815: DO NOT SUBMIT. [clangd] A helper Mutex class that reports contention stats

Ilya Biryukov via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Dec 18 02:30:08 PST 2018


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


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D55815.178628.patch
Type: text/x-patch
Size: 1616 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20181218/353c6307/attachment.bin>


More information about the cfe-commits mailing list