From 5020e611bbffd51bb8cebd3790f8537b7eb4c03c Mon Sep 17 00:00:00 2001 From: Rahul Sundararaman Date: Tue, 23 Jul 2019 13:57:25 -0400 Subject: SERVER-41362 Attach diagnostic captures to OperationContexts --- src/mongo/platform/SConscript | 3 --- src/mongo/platform/mutex.cpp | 21 ++++++++++++++++++++- src/mongo/platform/mutex.h | 10 ++++++++++ src/mongo/platform/mutex_test.cpp | 5 ----- 4 files changed, 30 insertions(+), 9 deletions(-) (limited to 'src/mongo/platform') diff --git a/src/mongo/platform/SConscript b/src/mongo/platform/SConscript index 171a183fa45..6ccbf7f77ce 100644 --- a/src/mongo/platform/SConscript +++ b/src/mongo/platform/SConscript @@ -17,7 +17,4 @@ env.CppUnitTest( 'decimal128_bson_test.cpp', 'overflow_arithmetic_test.cpp' ], - LIBDEPS=[ - '$BUILD_DIR/mongo/db/service_context' - ] ) \ No newline at end of file diff --git a/src/mongo/platform/mutex.cpp b/src/mongo/platform/mutex.cpp index c81783f4bd7..b5661cf6f53 100644 --- a/src/mongo/platform/mutex.cpp +++ b/src/mongo/platform/mutex.cpp @@ -31,16 +31,35 @@ namespace mongo { +namespace { +std::unique_ptr gLockActions; +} + void Mutex::lock() { - auto hasLock = _mutex.try_lock_for(_lockTimeout.toSystemDuration()); + auto hasLock = _mutex.try_lock_for(kContendedLockTimeout.toSystemDuration()); + if (hasLock) { + return; + } + if (gLockActions) { + gLockActions->onContendedLock(_name); + } + hasLock = _mutex.try_lock_for(_lockTimeout.toSystemDuration() - + kContendedLockTimeout.toSystemDuration()); uassert( ErrorCodes::InternalError, "Unable to take latch, wait time exceeds set timeout", hasLock); } void Mutex::unlock() { + if (gLockActions) { + gLockActions->onUnlock(); + } _mutex.unlock(); } bool Mutex::try_lock() { return _mutex.try_lock(); } +void Mutex::setLockActions(std::unique_ptr actions) { + gLockActions = std::move(actions); +} + } // namespace mongo diff --git a/src/mongo/platform/mutex.h b/src/mongo/platform/mutex.h index a0be7ae971d..bb885d0cbe1 100644 --- a/src/mongo/platform/mutex.h +++ b/src/mongo/platform/mutex.h @@ -36,6 +36,13 @@ namespace mongo { +class LockActions { +public: + virtual ~LockActions() = default; + virtual void onContendedLock(const StringData& name) = 0; + virtual void onUnlock() = 0; +}; + class Mutex { public: Mutex() : Mutex("AnonymousMutex"_sd) {} @@ -48,9 +55,12 @@ public: return _name; } + static void setLockActions(std::unique_ptr actions); + private: const StringData _name; const Seconds _lockTimeout = Seconds(60); + static constexpr Milliseconds kContendedLockTimeout = Milliseconds(100); stdx::timed_mutex _mutex; }; diff --git a/src/mongo/platform/mutex_test.cpp b/src/mongo/platform/mutex_test.cpp index 895914c529b..6c6674aa197 100644 --- a/src/mongo/platform/mutex_test.cpp +++ b/src/mongo/platform/mutex_test.cpp @@ -29,15 +29,10 @@ #include "mongo/unittest/unittest.h" -#include "mongo/db/service_context.h" #include "mongo/platform/mutex.h" -#include "mongo/stdx/thread.h" namespace mongo { TEST(MongoMutexTest, BasicSingleThread) { - auto serviceContext = ServiceContext::make(); - setGlobalServiceContext(std::move(serviceContext)); - Mutex m; m.lock(); ASSERT(!m.try_lock()); -- cgit v1.2.1