]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
concurrency: make C++11 style debugging mutices
authorAdam C. Emerson <aemerson@redhat.com>
Mon, 14 Sep 2015 17:30:38 +0000 (13:30 -0400)
committerAdam C. Emerson <aemerson@redhat.com>
Fri, 5 Feb 2016 17:15:19 +0000 (12:15 -0500)
Signed-off-by: Adam C. Emerson <aemerson@redhat.com>
src/CMakeLists.txt
src/common/Makefile.am
src/common/mutex_debug.cc [new file with mode: 0644]
src/common/mutex_debug.h [new file with mode: 0644]
src/include/uuid.h
src/test/CMakeLists.txt
src/test/Makefile.am
src/test/common/test_mutex_debug.cc [new file with mode: 0644]

index 619519c2c599a3450bca828eba4b04f59fecfc59..42714b458c0d05ecdaf6de6c1bff591b9e49f324 100644 (file)
@@ -215,6 +215,7 @@ set(libcommon_files
   common/PrebufferedStreambuf.cc
   common/BackTrace.cc
   common/perf_counters.cc
+  common/mutex_debug.cc
   common/Mutex.cc
   common/OutputDataSocket.cc
   common/admin_socket.cc
index 964a3395867b639778c715216909b308b579bf2e..f86d52327fc87011220030d3ba6d9725957ca1f1 100644 (file)
@@ -8,6 +8,7 @@ libcommon_internal_la_SOURCES = \
        common/SloppyCRCMap.cc \
        common/BackTrace.cc \
        common/perf_counters.cc \
+       common/mutex_debug.cc \
        common/Mutex.cc \
        common/OutputDataSocket.cc \
        common/admin_socket.cc \
@@ -275,7 +276,8 @@ noinst_HEADERS += \
        common/PluginRegistry.h \
        common/ceph_time.h \
        common/ceph_timer.h \
-       common/align.h
+       common/align.h \
+       common/mutex_debug.h
 
 if ENABLE_XIO
 noinst_HEADERS += \
diff --git a/src/common/mutex_debug.cc b/src/common/mutex_debug.cc
new file mode 100644 (file)
index 0000000..a854358
--- /dev/null
@@ -0,0 +1,97 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
+// vim: ts=8 sw=2 smarttab
+/*
+ * Ceph - scalable distributed file system
+ *
+ * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
+ *
+ * This is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License version 2.1, as published by the Free Software
+ * Foundation.  See file COPYING.
+ *
+ */
+#include <string>
+
+#include <boost/uuid/uuid.hpp>
+#include <boost/uuid/uuid_generators.hpp>
+#include <boost/uuid/uuid_io.hpp>
+
+#include "common/mutex_debug.h"
+#include "common/perf_counters.h"
+#include "common/ceph_context.h"
+#include "common/config.h"
+#include "include/stringify.h"
+
+namespace ceph {
+namespace mutex_debug_detail {
+enum {
+  l_mutex_first = 999082,
+  l_mutex_wait,
+  l_mutex_last
+};
+
+mutex_debugging_base::mutex_debugging_base(const std::string &n, bool bt,
+                                          CephContext *cct) :
+  id(-1), backtrace(bt), nlock(0), locked_by(thread::id()),
+  cct(cct), logger(0) {
+  if (n.empty()) {
+    uuid_d uu;
+    uu.generate_random();
+    name = string("Unnamed-Mutex-") + uu.to_string();
+  } else {
+    name = n;
+  }
+  if (cct) {
+    PerfCountersBuilder b(cct, string("mutex-") + name,
+                         l_mutex_first, l_mutex_last);
+    b.add_time_avg(l_mutex_wait, "wait",
+                  "Average time of mutex in locked state");
+    logger = b.create_perf_counters();
+    cct->get_perfcounters_collection()->add(logger);
+    logger->set(l_mutex_wait, 0);
+  }
+  if (g_lockdep)
+    _register();
+}
+
+mutex_debugging_base::~mutex_debugging_base() {
+  assert(nlock == 0);
+  if (cct && logger) {
+    cct->get_perfcounters_collection()->remove(logger);
+    delete logger;
+  }
+  if (g_lockdep) {
+    lockdep_unregister(id);
+  }
+}
+
+void mutex_debugging_base::_register() {
+  id = lockdep_register(name.c_str());
+}
+void mutex_debugging_base::_will_lock() { // about to lock
+  id = lockdep_will_lock(name.c_str(), id, backtrace);
+}
+void mutex_debugging_base::_locked() {    // just locked
+  id = lockdep_locked(name.c_str(), id, backtrace);
+}
+void mutex_debugging_base::_will_unlock() {  // about to unlock
+  id = lockdep_will_unlock(name.c_str(), id);
+}
+
+ceph::mono_time mutex_debugging_base::before_lock_blocks() {
+  if (logger && cct && cct->_conf->mutex_perf_counter)
+    return ceph::mono_clock::now();
+  return ceph::mono_time::min();
+}
+
+void mutex_debugging_base::after_lock_blocks(ceph::mono_time start,
+                                            bool no_lockdep) {
+  if (logger && cct && cct->_conf->mutex_perf_counter)
+    logger->tinc(l_mutex_wait,
+                ceph::mono_clock::now() - start);
+  if (!no_lockdep && g_lockdep)
+    _locked();
+}
+} // namespace mutex_debug_detail
+} // namespace ceph
diff --git a/src/common/mutex_debug.h b/src/common/mutex_debug.h
new file mode 100644 (file)
index 0000000..c92a88f
--- /dev/null
@@ -0,0 +1,191 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
+// vim: ts=8 sw=2 smarttab
+/*
+ * Ceph - scalable distributed file system
+ *
+ * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
+ *
+ * This is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License version 2.1, as published by the Free Software
+ * Foundation.  See file COPYING.
+ *
+ */
+
+#ifndef CEPH_COMMON_MUTEX_DEBUG_H
+#define CEPH_COMMON_MUTEX_DEBUG_H
+
+#include <system_error>
+#include <thread>
+
+#include <pthread.h>
+
+#include "include/assert.h"
+
+#include "ceph_time.h"
+#include "likely.h"
+#include "lockdep.h"
+
+class CephContext;
+class PerfCounters;
+
+namespace ceph {
+namespace mutex_debug_detail {
+class mutex_debugging_base {
+protected:
+  std::string name;
+  int id;
+  bool backtrace; // gather backtrace on lock acquisition
+
+  int nlock;
+  std::thread::id locked_by;
+  CephContext *cct;
+  PerfCounters *logger;
+
+  void _register();
+  void _will_lock(); // about to lock
+  void _locked(); // just locked
+    void _will_unlock(); // about to unlock
+
+  mutex_debugging_base(const std::string &n = std::string(), bool bt = false,
+                      CephContext *cct = nullptr);
+  ~mutex_debugging_base();
+
+  ceph::mono_time before_lock_blocks();
+  void after_lock_blocks(ceph::mono_time start,
+                        bool no_lockdep);
+
+public:
+  bool is_locked() const {
+    return (nlock > 0);
+  }
+  bool is_locked_by_me() const {
+    return nlock > 0 && locked_by == std::this_thread::get_id();
+  }
+  operator bool() const {
+    return nlock > 0 && locked_by == std::this_thread::get_id();
+  }
+};
+
+template<typename Mutex>
+class mutex_debugging : public mutex_debugging_base {
+  Mutex* impl;
+
+public:
+  mutex_debugging(const std::string &n = std::string(), bool bt = false,
+                 CephContext *cct = nullptr) :
+    mutex_debugging_base(n, bt, cct), impl(static_cast<Mutex*>(this)) {}
+
+  ~mutex_debugging() = default;
+
+  void _post_lock() {
+    if (!impl->recursive)
+      assert(nlock == 0);
+    locked_by = std::this_thread::get_id();
+    nlock++;
+  }
+
+  void _pre_unlock() {
+    assert(nlock > 0);
+    --nlock;
+    assert(locked_by == std::this_thread::get_id());
+    if (!impl->recursive)
+      assert(nlock == 0);
+    if (nlock == 0)
+      locked_by = std::thread::id();
+  }
+
+  bool try_lock(bool no_lockdep = false) {
+    bool locked = impl->try_lock_impl();
+    if (locked) {
+      if (g_lockdep && !no_lockdep)
+       _locked();
+      _post_lock();
+    }
+    return locked;
+  }
+
+  void lock(bool no_lockdep = false) {
+    if (g_lockdep && !no_lockdep)
+      _will_lock();
+
+    if (try_lock())
+      return;
+
+    auto t = before_lock_blocks();
+    impl->lock_impl();
+    after_lock_blocks(t, no_lockdep);
+    _post_lock();
+  }
+
+  void unlock(bool no_lockdep = false) {
+    _pre_unlock();
+    if (!no_lockdep && g_lockdep)
+      _will_unlock();
+    impl->unlock_impl();
+  }
+};
+
+// Since this is a /debugging/ mutex just define it in terms of the
+// pthread error check mutex.
+template<bool Recursive>
+class mutex_debug_impl : public mutex_debugging<mutex_debug_impl<Recursive> > {
+private:
+  pthread_mutex_t m;
+public:
+  static constexpr bool recursive = Recursive;
+
+  // Mutex concept is DefaultConstructible
+  mutex_debug_impl(const std::string &n = std::string(), bool bt = false,
+                  CephContext *cct = nullptr) :
+    mutex_debugging<mutex_debug_impl<Recursive> >(n, bt, cct) {
+    if (recursive)
+      m = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
+    else
+      m = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;
+  }
+  // Mutex is Destructible
+  ~mutex_debug_impl() = default;
+
+  // Mutex concept is non-Copyable
+  mutex_debug_impl(const mutex_debug_impl&) = delete;
+  mutex_debug_impl& operator =(const mutex_debug_impl&) = delete;
+
+  // Mutex concept is non-Movable
+  mutex_debug_impl(mutex_debug_impl&&) = delete;
+  mutex_debug_impl& operator =(mutex_debug_impl&&) = delete;
+
+  void lock_impl() {
+    int r = pthread_mutex_lock(&m);
+    // Allowed error codes for Mutex concept
+    if (unlikely(r == EPERM ||
+                r == EDEADLK ||
+                r == EBUSY)) {
+      throw std::system_error(r, std::generic_category());
+    }
+    assert(r == 0);
+  }
+
+  void unlock_impl() noexcept {
+    int r = pthread_mutex_unlock(&m);
+    assert(r == 0);
+  }
+
+  bool try_lock_impl() {
+    int r = pthread_mutex_trylock(&m);
+    switch (r) {
+    case 0:
+      return true;
+    case EBUSY:
+      return false;
+    default:
+      throw std::system_error(r, std::generic_category());
+    }
+  }
+};
+} // namespace mutex_debug_detail
+typedef mutex_debug_detail::mutex_debug_impl<false> mutex_debug;
+typedef mutex_debug_detail::mutex_debug_impl<true> mutex_recursive_debug;
+} // namespace ceph
+
+#endif
index 03e6b5ad32c798402a4375d584ebb41890f629fd..bd888f863da10318329058cfde41adf96d378130 100644 (file)
@@ -44,10 +44,14 @@ struct uuid_d {
     memcpy(s, boost::uuids::to_string(uuid).c_str(), 37);
   }
 
+ std::string to_string() const {
+    return boost::uuids::to_string(uuid);
+  }
+
   char *bytes() const {
     return (char*)uuid.data;
   }
-  
+
   void encode(bufferlist& bl) const {
     ::encode_raw(uuid, bl);
   }
index 114e97764c1512bb94504866a2f155bb40ad9fcc..4f1fec6e2ae3666e4d7106441371a9ca4c8ae9a3 100644 (file)
@@ -544,6 +544,19 @@ target_link_libraries(unittest_str_map common global
 set_target_properties(unittest_str_map
   PROPERTIES COMPILE_FLAGS ${UNITTEST_CXX_FLAGS})
 
+# unittest_shared_mutex
+set(unittest_mutex_debug_srcs
+  common/test_mutex_debug.cc
+  )
+add_executable(unittest_mutex_debug
+  ${unittest_mutex_debug_srcs}
+  $<TARGET_OBJECTS:heap_profiler_objs>
+  )
+target_link_libraries(unittest_mutex_debug global
+  ${BLKID_LIBRARIES} ${CMAKE_DL_LIBS} ${TCMALLOC_LIBS} ${UNITTEST_LIBS} ${EXTRALIBS})
+set_target_properties(unittest_mutex_debug
+  PROPERTIES COMPILE_FLAGS ${UNITTEST_CXX_FLAGS})
+
 # unittest_sharedptr_registry
 add_executable(unittest_sharedptr_registry EXCLUDE_FROM_ALL
   common/test_sharedptr_registry.cc
index 2bcb8def33236435377a82e73691ca6c715b3e2a..873c69ff7839137047f9dff3aae7cf30e4b1c96e 100644 (file)
@@ -178,6 +178,11 @@ unittest_str_map_CXXFLAGS = $(UNITTEST_CXXFLAGS)
 unittest_str_map_LDADD = $(UNITTEST_LDADD) $(CEPH_GLOBAL)
 check_TESTPROGRAMS += unittest_str_map
 
+unittest_mutex_debug_SOURCES = test/common/test_mutex_debug.cc
+unittest_mutex_debug_CXXFLAGS = $(UNITTEST_CXXFLAGS)
+unittest_mutex_debug_LDADD = $(UNITTEST_LDADD) $(CEPH_GLOBAL) ${EXTRALIBS}
+check_TESTPROGRAMS += unittest_mutex_debug
+
 unittest_sharedptr_registry_SOURCES = test/common/test_sharedptr_registry.cc
 unittest_sharedptr_registry_CXXFLAGS = $(UNITTEST_CXXFLAGS)
 unittest_sharedptr_registry_LDADD = $(UNITTEST_LDADD) $(CEPH_GLOBAL)
diff --git a/src/test/common/test_mutex_debug.cc b/src/test/common/test_mutex_debug.cc
new file mode 100644 (file)
index 0000000..49cd499
--- /dev/null
@@ -0,0 +1,101 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
+// vim: ts=8 sw=2 &smarttab
+/*
+ * Ceph - scalable distributed file system
+ *
+ * Copyright (C) 2011 New Dream Network
+ *
+ * This is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License version 2, as published by the Free Software
+ * Foundation.  See file COPYING.
+ *
+ */
+
+#include <future>
+#include <mutex>
+#include <thread>
+
+#include "common/mutex_debug.h"
+
+#include "gtest/gtest.h"
+
+
+template<typename Mutex>
+static bool test_try_lock(Mutex* m) {
+  if (!m->try_lock())
+    return false;
+  m->unlock();
+  return true;
+}
+
+template<typename Mutex>
+static void test_lock() {
+  Mutex m;
+  auto ttl = &test_try_lock<Mutex>;
+
+  m.lock();
+  ASSERT_TRUE(m.is_locked());
+  auto f1 = std::async(std::launch::async, ttl, &m);
+  ASSERT_FALSE(f1.get());
+
+  ASSERT_TRUE(m.is_locked());
+  ASSERT_TRUE(!!m);
+
+  m.unlock();
+  ASSERT_FALSE(m.is_locked());
+  ASSERT_FALSE(!!m);
+
+  auto f3 = std::async(std::launch::async, ttl, &m);
+  ASSERT_TRUE(f3.get());
+
+  ASSERT_FALSE(m.is_locked());
+  ASSERT_FALSE(!!m);
+}
+
+TEST(MutexDebug, Lock) {
+  test_lock<ceph::mutex_debug>();
+}
+
+TEST(MutexDebug, NotRecursive) {
+  ceph::mutex_debug m;
+  auto ttl = &test_try_lock<mutex_debug>;
+
+  ASSERT_NO_THROW(m.lock());
+  ASSERT_TRUE(m.is_locked());
+  ASSERT_FALSE(std::async(std::launch::async, ttl, &m).get());
+
+  ASSERT_THROW(m.lock(), std::system_error);
+  ASSERT_TRUE(m.is_locked());
+  ASSERT_FALSE(std::async(std::launch::async, ttl, &m).get());
+
+  ASSERT_NO_THROW(m.unlock());
+  ASSERT_FALSE(m.is_locked());
+  ASSERT_TRUE(std::async(std::launch::async, ttl, &m).get());
+}
+
+TEST(MutexRecursiveDebug, Lock) {
+  test_lock<ceph::mutex_recursive_debug>();
+}
+
+
+TEST(MutexRecursiveDebug, Recursive) {
+  ceph::mutex_recursive_debug m;
+  auto ttl = &test_try_lock<mutex_recursive_debug>;
+
+  ASSERT_NO_THROW(m.lock());
+  ASSERT_TRUE(m.is_locked());
+  ASSERT_FALSE(std::async(std::launch::async, ttl, &m).get());
+
+  ASSERT_NO_THROW(m.lock());
+  ASSERT_TRUE(m.is_locked());
+  ASSERT_FALSE(std::async(std::launch::async, ttl, &m).get());
+
+  ASSERT_NO_THROW(m.unlock());
+  ASSERT_TRUE(m.is_locked());
+  ASSERT_FALSE(std::async(std::launch::async, ttl, &m).get());
+
+  ASSERT_NO_THROW(m.unlock());
+  ASSERT_FALSE(m.is_locked());
+  ASSERT_TRUE(std::async(std::launch::async, ttl, &m).get());
+}