From 151c0511050c196003d0737858e0bf3ea9b665a4 Mon Sep 17 00:00:00 2001 From: Sage Weil Date: Sun, 23 Aug 2015 14:02:59 -0400 Subject: [PATCH] common/Mutex: avoid trylock on lock if instrumentation is not enabled Benchmarks have shown that the trylock in the lock path has a high latency cost. Only pay the penalty if instrumentation is actually enabled. While we are at it, avoid the duplicate conditional check so that the fast path is faster. Signed-off-by: Sage Weil --- src/common/Mutex.cc | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/common/Mutex.cc b/src/common/Mutex.cc index cedba098128c..5e9b590a5395 100644 --- a/src/common/Mutex.cc +++ b/src/common/Mutex.cc @@ -82,21 +82,26 @@ Mutex::~Mutex() { } void Mutex::Lock(bool no_lockdep) { - utime_t start; int r; if (lockdep && g_lockdep && !no_lockdep) _will_lock(); - if (TryLock()) { - goto out; - } - - if (logger && cct && cct->_conf->mutex_perf_counter) + if (logger && cct && cct->_conf->mutex_perf_counter) { + utime_t start; + // instrumented mutex enabled start = ceph_clock_now(cct); - r = pthread_mutex_lock(&_m); - if (logger && cct && cct->_conf->mutex_perf_counter) + if (TryLock()) { + goto out; + } + + r = pthread_mutex_lock(&_m); + logger->tinc(l_mutex_wait, ceph_clock_now(cct) - start); + } else { + r = pthread_mutex_lock(&_m); + } + assert(r == 0); if (lockdep && g_lockdep) _locked(); _post_lock(); -- 2.47.3