Skip to content

Commit

Permalink
debug deadlock
Browse files Browse the repository at this point in the history
Signed-off-by: Neil Shen <[email protected]>
  • Loading branch information
overvenus committed Jun 7, 2024
1 parent 45509f0 commit c23c775
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
2 changes: 2 additions & 0 deletions db/db_impl/db_impl_open.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1605,6 +1605,8 @@ Status DBImpl::Open(const DBOptions& db_options, const std::string& dbname,
const std::vector<ColumnFamilyDescriptor>& column_families,
std::vector<ColumnFamilyHandle*>* 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;
Expand Down
43 changes: 43 additions & 0 deletions port/port_posix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,39 @@

#include "util/string_util.h"

#include <execinfo.h>

#include "logging/logging.h"

namespace ROCKSDB_NAMESPACE {

static std::shared_ptr<Logger> global_info_log;
static std::atomic<unsigned int> 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.
Expand Down Expand Up @@ -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;
Expand All @@ -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() {
Expand Down
1 change: 1 addition & 0 deletions port/port_posix.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ class Mutex {
#ifndef NDEBUG
bool locked_ = false;
#endif
unsigned int lock_id_;
};

class RWMutex {
Expand Down

0 comments on commit c23c775

Please sign in to comment.