mutex.cpp 7.8 KB
Newer Older
羽飞's avatar
羽飞 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved.
miniob is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
         http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details. */

//
// Created by Longda on 2010
//

#include "common/lang/mutex.h"
#include "common/log/log.h"
namespace common {

std::map<pthread_mutex_t *, LockTrace::LockID> LockTrace::mLocks;
std::map<pthread_mutex_t *, int> LockTrace::mWaitTimes;
std::map<long long, pthread_mutex_t *> LockTrace::mWaitLocks;
std::map<long long, std::set<pthread_mutex_t *>> LockTrace::mOwnLocks;
std::set<pthread_mutex_t *> LockTrace::mEnableRecurisives;

pthread_rwlock_t LockTrace::mMapMutex = PTHREAD_RWLOCK_INITIALIZER;
int LockTrace::mMaxBlockTids = 8;

#define CHECK_UNLOCK 0

30 31 32
void LockTrace::foundDeadLock(LockID &current, LockTrace::LockID &other, pthread_mutex_t *otherWaitMutex)
{
  std::map<pthread_mutex_t *, LockTrace::LockID>::iterator itLocks = mLocks.find(otherWaitMutex);
羽飞's avatar
羽飞 已提交
33 34 35
  if (itLocks == mLocks.end()) {
    LOG_ERROR("Thread %ld own mutex %p and try to get mutex %s:%d, "
              "other thread %ld own mutex %s:%d and try to get %p",
36 37 38 39 40 41 42 43
        current.mThreadId,
        otherWaitMutex,
        current.mFile.c_str(),
        current.mLine,
        other.mThreadId,
        current.mFile.c_str(),
        current.mLine,
        otherWaitMutex);
羽飞's avatar
羽飞 已提交
44 45 46 47 48
  } else {
    LockTrace::LockID &otherRecusive = itLocks->second;

    LOG_ERROR("Thread %ld own mutex %p:%s:%d and try to get mutex %s:%d, "
              "other thread %ld own mutex %s:%d and try to get %p:%s:%d",
49 50 51 52 53 54 55 56 57 58 59 60
        current.mThreadId,
        otherWaitMutex,
        otherRecusive.mFile.c_str(),
        otherRecusive.mLine,
        current.mFile.c_str(),
        current.mLine,
        other.mThreadId,
        current.mFile.c_str(),
        current.mLine,
        otherWaitMutex,
        otherRecusive.mFile.c_str(),
        otherRecusive.mLine);
羽飞's avatar
羽飞 已提交
61 62 63
  }
}

64 65 66
bool LockTrace::deadlockCheck(
    LockID &current, std::set<pthread_mutex_t *> &ownMutexs, LockTrace::LockID &other, int recusiveNum)
{
羽飞's avatar
羽飞 已提交
67 68 69 70
  if (recusiveNum >= mMaxBlockTids) {
    return false;
  }

71
  std::map<long long, pthread_mutex_t *>::iterator otherIt = mWaitLocks.find(other.mThreadId);
羽飞's avatar
羽飞 已提交
72 73 74 75 76 77 78 79 80 81 82
  if (otherIt == mWaitLocks.end()) {
    return false;
  }
  pthread_mutex_t *otherWaitMutex = otherIt->second;

  if (ownMutexs.find(otherWaitMutex) != ownMutexs.end()) {
    // dead lock
    foundDeadLock(current, other, otherWaitMutex);
    return true;
  }

83
  std::map<pthread_mutex_t *, LockTrace::LockID>::iterator itLocks = mLocks.find(otherWaitMutex);
羽飞's avatar
羽飞 已提交
84 85 86 87 88 89 90 91
  if (itLocks == mLocks.end()) {
    return false;
  }
  LockTrace::LockID &otherRecusive = itLocks->second;

  return deadlockCheck(current, ownMutexs, otherRecusive, recusiveNum + 1);
}

92 93
bool LockTrace::deadlockCheck(pthread_mutex_t *mutex, const long long threadId, const char *file, const int line)
{
羽飞's avatar
羽飞 已提交
94 95
  mWaitLocks[threadId] = mutex;

96
  std::map<pthread_mutex_t *, LockTrace::LockID>::iterator itLocks = mLocks.find(mutex);
羽飞's avatar
羽飞 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
  if (itLocks == mLocks.end()) {
    return false;
  }
  LockTrace::LockID &other = itLocks->second;
  if (threadId == other.mThreadId) {
    // lock by himself
    if (mEnableRecurisives.find(mutex) != mEnableRecurisives.end()) {
      // the mutex's attributes has been set recurisves
      return false;
    } else {
      LockID current(threadId, file, line);
      foundDeadLock(current, other, mutex);
      return true;
    }
  }

113
  std::map<long long, std::set<pthread_mutex_t *>>::iterator it = mOwnLocks.find(threadId);
羽飞's avatar
羽飞 已提交
114 115 116 117 118 119 120 121 122 123 124 125
  if (it == mOwnLocks.end()) {
    return false;
  }
  std::set<pthread_mutex_t *> &ownMutexs = it->second;
  if (ownMutexs.empty() == true) {
    return false;
  }

  LockID current(threadId, file, line);
  return deadlockCheck(current, ownMutexs, other, 1);
}

126 127
bool LockTrace::checkLockTimes(pthread_mutex_t *mutex, const char *file, const int line)
{
羽飞's avatar
羽飞 已提交
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
  std::map<pthread_mutex_t *, int>::iterator it = mWaitTimes.find(mutex);
  if (it == mWaitTimes.end()) {
    mWaitTimes.insert(std::pair<pthread_mutex_t *, int>(mutex, 1));

    return false;
  }

  int lockTimes = it->second;
  mWaitTimes[mutex] = lockTimes + 1;
  if (lockTimes >= mMaxBlockTids) {

    // std::string          lastLockId = lockId.toString();
    LockTrace::LockID &lockId = mLocks[mutex];
    LOG_WARN("mutex %p has been already lock %d times, this time %s:%d, first "
             "time:%ld:%s:%d",
143 144 145 146 147 148 149
        mutex,
        lockTimes,
        file,
        line,
        lockId.mThreadId,
        lockId.mFile.c_str(),
        lockId.mLine);
羽飞's avatar
羽飞 已提交
150 151 152 153 154 155 156

    return true;
  } else {
    return false;
  }
}

157 158
void LockTrace::check(pthread_mutex_t *mutex, const long long threadId, const char *file, const int line)
{
羽飞's avatar
羽飞 已提交
159 160 161 162 163 164 165 166 167 168
  MUTEX_LOG("Lock mutex %p, %s:%d", mutex, file, line);
  pthread_rwlock_rdlock(&mMapMutex);

  deadlockCheck(mutex, threadId, file, line);

  checkLockTimes(mutex, file, line);

  pthread_rwlock_unlock(&mMapMutex);
}

169 170
void LockTrace::insertLock(pthread_mutex_t *mutex, const long long threadId, const char *file, const int line)
{
羽飞's avatar
羽飞 已提交
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
  LockID lockID(threadId, file, line);

  mLocks.insert(std::pair<pthread_mutex_t *, LockID>(mutex, lockID));

  mWaitLocks.erase(threadId);

  // add entry to mOwnLocks
  std::set<pthread_mutex_t *> &ownLockSet = mOwnLocks[threadId];
  ownLockSet.insert(mutex);

  std::map<pthread_mutex_t *, int>::iterator itTimes = mWaitTimes.find(mutex);
  if (itTimes == mWaitTimes.end()) {
    LOG_ERROR("No entry of %p:%s:%d in mWaitTimes", mutex, file, line);

  } else {
    mWaitTimes[mutex] = itTimes->second - 1;
  }
}

190 191
void LockTrace::lock(pthread_mutex_t *mutex, const long long threadId, const char *file, const int line)
{
羽飞's avatar
羽飞 已提交
192 193 194 195 196 197
  pthread_rwlock_wrlock(&mMapMutex);

  insertLock(mutex, threadId, file, line);
  pthread_rwlock_unlock(&mMapMutex);
}

198 199
void LockTrace::tryLock(pthread_mutex_t *mutex, const long long threadId, const char *file, const int line)
{
羽飞's avatar
羽飞 已提交
200 201 202 203 204 205 206 207 208 209
  pthread_rwlock_wrlock(&mMapMutex);
  if (mLocks.find(mutex) != mLocks.end()) {
    pthread_rwlock_unlock(&mMapMutex);
    return;
  }

  insertLock(mutex, threadId, file, line);
  pthread_rwlock_unlock(&mMapMutex);
}

210 211
void LockTrace::unlock(pthread_mutex_t *mutex, long long threadId, const char *file, int line)
{
羽飞's avatar
羽飞 已提交
212 213 214 215 216 217 218 219 220 221
  pthread_rwlock_wrlock(&mMapMutex);

  mLocks.erase(mutex);

  std::set<pthread_mutex_t *> &ownLockSet = mOwnLocks[threadId];
  ownLockSet.erase(mutex);

  pthread_rwlock_unlock(&mMapMutex);
}

222 223
void LockTrace::toString(std::string &result)
{
羽飞's avatar
羽飞 已提交
224 225 226 227

  const int TEMP_PAIR_LEN = 24;
  // pthread_mutex_lock(&mMapMutex);
  result = " mLocks:\n";
228
  for (std::map<pthread_mutex_t *, LockID>::iterator it = mLocks.begin(); it != mLocks.end(); it++) {
羽飞's avatar
羽飞 已提交
229 230 231 232 233 234 235 236 237
    result += it->second.toString();

    char pointerBuf[TEMP_PAIR_LEN] = {0};
    snprintf(pointerBuf, TEMP_PAIR_LEN, ",mutex:%p\n", it->first);

    result += pointerBuf;
  }

  result += "mWaitTimes:\n";
238
  for (std::map<pthread_mutex_t *, int>::iterator it = mWaitTimes.begin(); it != mWaitTimes.end(); it++) {
羽飞's avatar
羽飞 已提交
239
    char pointerBuf[TEMP_PAIR_LEN] = {0};
240
    snprintf(pointerBuf, TEMP_PAIR_LEN, ",mutex:%p, times:%d\n", it->first, it->second);
羽飞's avatar
羽飞 已提交
241 242 243 244
    result += pointerBuf;
  }

  result += "mWaitLocks:\n";
245
  for (std::map<long long, pthread_mutex_t *>::iterator it = mWaitLocks.begin(); it != mWaitLocks.end(); it++) {
羽飞's avatar
羽飞 已提交
246
    char pointerBuf[TEMP_PAIR_LEN] = {0};
247 248 249 250 251 252
    snprintf(pointerBuf,
        TEMP_PAIR_LEN,
        "threadID: %llx"
        ", mutex:%p\n",
        it->first,
        it->second);
羽飞's avatar
羽飞 已提交
253 254 255 256 257 258 259 260
    result += pointerBuf;
  }
  // pthread_mutex_unlock(&mMapMutex);
  // skip mOwnLocks output

  return;
}

261
}  // namespace common