diff --git a/db/db_impl/db_impl_open.cc b/db/db_impl/db_impl_open.cc index a75a62f5b69..a9b6063a1b4 100644 --- a/db/db_impl/db_impl_open.cc +++ b/db/db_impl/db_impl_open.cc @@ -1605,6 +1605,8 @@ Status DBImpl::Open(const DBOptions& db_options, const std::string& dbname, const std::vector& column_families, std::vector* handles, DB** dbptr, const bool seq_per_batch, const bool batch_per_txn) { + global_info_log = db_options.info_log; + Status s = ValidateOptionsByTable(db_options, column_families); if (!s.ok()) { return s; diff --git a/port/port_posix.cc b/port/port_posix.cc index 935c8a97837..c03525473bf 100644 --- a/port/port_posix.cc +++ b/port/port_posix.cc @@ -30,8 +30,39 @@ #include "util/string_util.h" +#include + +#include "logging/logging.h" + namespace ROCKSDB_NAMESPACE { +static std::shared_ptr global_info_log; +static std::atomic global_lock_id(0); + +std::string print_backtrace() { + // Buffer to store backtrace addresses + void* buffer[100]; + + // Capture the backtrace + int nptrs = backtrace(buffer, 100); + + // Convert addresses to strings + char** strings = backtrace_symbols(buffer, nptrs); + if (strings == nullptr) { + perror("backtrace_symbols"); + exit(EXIT_FAILURE); + } + + std::ostringstream oss; + for (int i = 0; i < nptrs; i++) { + oss << strings[i] << "\n"; + } + // Free the memory allocated for the strings + free(strings); + + return oss.str(); +} + // We want to give users opportunity to default all the mutexes to adaptive if // not specified otherwise. This enables a quick way to conduct various // performance related experiements. @@ -79,6 +110,13 @@ Mutex::Mutex(bool adaptive) { Mutex::~Mutex() { PthreadCall("destroy mutex", pthread_mutex_destroy(&mu_)); } void Mutex::Lock() { + std::string b = print_backtrace(); + if (global_info_log.get() != nullptr) { + ROCKS_LOG_INFO(global_info_log, + "dbg Mutex::Lock %u@%p backtrace %s", + lock_id_, this, b); + } + PthreadCall("lock", pthread_mutex_lock(&mu_)); #ifndef NDEBUG locked_ = true; @@ -90,6 +128,11 @@ void Mutex::Unlock() { locked_ = false; #endif PthreadCall("unlock", pthread_mutex_unlock(&mu_)); + if (global_info_log.get() != nullptr) { + ROCKS_LOG_INFO(global_info_log, + "dbg Mutex::Unlock %u@%p", + lock_id_, this); + } } bool Mutex::TryLock() { diff --git a/port/port_posix.h b/port/port_posix.h index fdf90c236cc..cd2a0c766d1 100644 --- a/port/port_posix.h +++ b/port/port_posix.h @@ -129,6 +129,7 @@ class Mutex { #ifndef NDEBUG bool locked_ = false; #endif + unsigned int lock_id_; }; class RWMutex {