From 0844a209aa009c15f4e6dd1c5d2c58c979789e07 Mon Sep 17 00:00:00 2001 From: Radoslaw Zarzynski Date: Fri, 12 Jul 2019 20:00:27 -0400 Subject: [PATCH] common: refactor handling of lockdep's group name in debug locks. Signed-off-by: Radoslaw Zarzynski --- src/common/lockdep.h | 3 +++ src/common/mutex_debug.cc | 22 ++++++++++------------ src/common/mutex_debug.h | 14 ++++---------- src/common/shared_mutex_debug.cc | 4 ++-- src/common/shared_mutex_debug.h | 2 +- 5 files changed, 20 insertions(+), 25 deletions(-) diff --git a/src/common/lockdep.h b/src/common/lockdep.h index 1c854a9eaec..11f21f56045 100644 --- a/src/common/lockdep.h +++ b/src/common/lockdep.h @@ -21,6 +21,9 @@ extern bool g_lockdep; extern void lockdep_register_ceph_context(CephContext *cct); extern void lockdep_unregister_ceph_context(CephContext *cct); +// lockdep tracks dependencies between multiple and different instances +// of locks within a class denoted by `n`. +// Caller is obliged to guarantee name uniqueness. extern int lockdep_register(const char *n); extern void lockdep_unregister(int id); extern int lockdep_will_lock(const char *n, int id, bool force_backtrace=false, diff --git a/src/common/mutex_debug.cc b/src/common/mutex_debug.cc index 5660826ef2c..e6b77c1424d 100644 --- a/src/common/mutex_debug.cc +++ b/src/common/mutex_debug.cc @@ -25,14 +25,12 @@ enum { l_mutex_last }; -mutex_debugging_base::mutex_debugging_base(const std::string &n, bool bt) - : name(n), id(-1), backtrace(bt), nlock(0), locked_by(thread::id()) -{ - if (g_lockdep) - _register(); -} -mutex_debugging_base::mutex_debugging_base(const char *n, bool bt) - : name(n), id(-1), backtrace(bt), nlock(0), locked_by(thread::id()) +mutex_debugging_base::mutex_debugging_base(std::string group, bool bt) + : group(std::move(group)), + id(-1), + backtrace(bt), + nlock(0), + locked_by(thread::id()) { if (g_lockdep) _register(); @@ -46,16 +44,16 @@ mutex_debugging_base::~mutex_debugging_base() { } void mutex_debugging_base::_register() { - id = lockdep_register(name.c_str()); + id = lockdep_register(group.c_str()); } void mutex_debugging_base::_will_lock(bool recursive) { // about to lock - id = lockdep_will_lock(name.c_str(), id, backtrace, recursive); + id = lockdep_will_lock(group.c_str(), id, backtrace, recursive); } void mutex_debugging_base::_locked() { // just locked - id = lockdep_locked(name.c_str(), id, backtrace); + id = lockdep_locked(group.c_str(), id, backtrace); } void mutex_debugging_base::_will_unlock() { // about to unlock - id = lockdep_will_unlock(name.c_str(), id); + id = lockdep_will_unlock(group.c_str(), id); } } // namespace mutex_debug_detail diff --git a/src/common/mutex_debug.h b/src/common/mutex_debug.h index 247233fd999..57e1a69bd20 100644 --- a/src/common/mutex_debug.h +++ b/src/common/mutex_debug.h @@ -35,7 +35,7 @@ namespace mutex_debug_detail { class mutex_debugging_base { protected: - std::string name; + std::string group; int id; bool backtrace; // gather backtrace on lock acquisition @@ -48,8 +48,7 @@ protected: void _locked(); // just locked void _will_unlock(); // about to unlock - mutex_debugging_base(const std::string &n, bool bt = false); - mutex_debugging_base(const char *n, bool bt = false); + mutex_debugging_base(std::string group, bool bt = false); ~mutex_debugging_base(); public: @@ -98,13 +97,8 @@ private: public: static constexpr bool recursive = Recursive; - // Mutex concept is DefaultConstructible - mutex_debug_impl(const std::string &n, bool bt = false) - : mutex_debugging_base(n, bt) { - _init(); - } - mutex_debug_impl(const char *n, bool bt = false) - : mutex_debugging_base(n, bt) { + mutex_debug_impl(std::string group, bool bt = false) + : mutex_debugging_base(group, bt) { _init(); } diff --git a/src/common/shared_mutex_debug.cc b/src/common/shared_mutex_debug.cc index c0a031a42d5..3dd3814edb6 100644 --- a/src/common/shared_mutex_debug.cc +++ b/src/common/shared_mutex_debug.cc @@ -7,11 +7,11 @@ namespace ceph { -shared_mutex_debug::shared_mutex_debug(const std::string& n, +shared_mutex_debug::shared_mutex_debug(std::string group, bool track_lock, bool enable_lock_dep, bool prioritize_write) - : mutex_debugging_base{n, false /* backtrace */}, + : mutex_debugging_base{std::move(group), false /* backtrace */}, track(track_lock), lockdep(enable_lock_dep) { diff --git a/src/common/shared_mutex_debug.h b/src/common/shared_mutex_debug.h index c0abf54b515..2144974372a 100644 --- a/src/common/shared_mutex_debug.h +++ b/src/common/shared_mutex_debug.h @@ -19,7 +19,7 @@ class shared_mutex_debug : std::atomic nrlock{0}; public: - shared_mutex_debug(const std::string& n, + shared_mutex_debug(std::string group, bool track_lock=true, bool enable_lock_dep=true, bool prioritize_write=false); -- 2.39.5