frame.cpp 9.0 KB
Newer Older
羽飞's avatar
羽飞 已提交
1
/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
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 lianyu on 2022/10/29.
//

#include "storage/buffer/frame.h"
#include "session/thread_data.h"

羽飞's avatar
羽飞 已提交
18 19
using namespace std;

20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
FrameId::FrameId(int file_desc, PageNum page_num) : file_desc_(file_desc), page_num_(page_num)
{}

bool FrameId::equal_to(const FrameId &other) const
{
  return file_desc_ == other.file_desc_ && page_num_ == other.page_num_;
}

bool FrameId::operator==(const FrameId &other) const
{
  return this->equal_to(other);
}

size_t FrameId::hash() const
{
  return (static_cast<size_t>(file_desc_) << 32L) | page_num_;
}

int FrameId::file_desc() const
{
  return file_desc_;
}
PageNum FrameId::page_num() const
{
  return page_num_;
}

羽飞's avatar
羽飞 已提交
47
string to_string(const FrameId &frame_id)
48
{
羽飞's avatar
羽飞 已提交
49
  stringstream ss;
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
  ss << "fd:" << frame_id.file_desc() << ",page_num:" << frame_id.page_num();
  return ss.str();
}

////////////////////////////////////////////////////////////////////////////////
intptr_t get_default_debug_xid()
{
  ThreadData *thd = ThreadData::current();
  intptr_t xid = (thd == nullptr) ? 
                 // pthread_self的返回值类型是pthread_t,pthread_t在linux和mac上不同
                 // 在Linux上是一个整数类型,而在mac上是一个指针。为了能在两个平台上都编译通过,
                 // 就将pthread_self返回值转换两次
                 reinterpret_cast<intptr_t>(reinterpret_cast<void*>(pthread_self())) : 
                 reinterpret_cast<intptr_t>(thd);
  return xid;
}

void Frame::write_latch()
{
  write_latch(get_default_debug_xid());
}

void Frame::write_latch(intptr_t xid)
{
  {
羽飞's avatar
羽飞 已提交
75
    scoped_lock debug_lock(debug_lock_);
76 77 78 79 80 81 82 83 84 85 86 87 88
    ASSERT(pin_count_.load() > 0,
           "frame lock. write lock failed while pin count is invalid. "
           "this=%p, pin=%d, pageNum=%d, fd=%d, xid=%lx, lbt=%s",
           this, pin_count_.load(), page_.page_num, file_desc_, xid, lbt());

    ASSERT(read_lockers_.find(xid) == read_lockers_.end(),
           "frame lock write while holding the read lock."
           "this=%p, pin=%d, pageNum=%d, fd=%d, xid=%lx, lbt=%s",
           this, pin_count_.load(), page_.page_num, file_desc_, xid, lbt());
  }

  lock_.lock();
  write_locker_ = xid;
羽飞's avatar
羽飞 已提交
89
  write_recursive_count_++;
90 91

  LOG_DEBUG("frame write lock success."
羽飞's avatar
羽飞 已提交
92 93
            "this=%p, pin=%d, pageNum=%d, write locker=%lx(recursive=%d), fd=%d, xid=%lx, lbt=%s",
            this, pin_count_.load(), page_.page_num, write_locker_, write_recursive_count_, file_desc_, xid, lbt());
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
}

void Frame::write_unlatch()
{
  write_unlatch(get_default_debug_xid());
}

void Frame::write_unlatch(intptr_t xid)
{
  // 因为当前已经加着写锁,而且写锁只有一个,所以不再加debug_lock来做校验

  ASSERT(pin_count_.load() > 0, 
        "frame lock. write unlock failed while pin count is invalid."
        "this=%p, pin=%d, pageNum=%d, fd=%d, xid=%lx, lbt=%s",
         this, pin_count_.load(), page_.page_num, file_desc_, xid, lbt());

  ASSERT(write_locker_ == xid,
         "frame unlock write while not the owner."
         "write_locker=%lx, this=%p, pin=%d, pageNum=%d, fd=%d, xid=%lx, lbt=%s",
         write_locker_, this, pin_count_.load(), page_.page_num, file_desc_, xid, lbt());

  LOG_DEBUG("frame write unlock success. this=%p, pin=%d, pageNum=%d, fd=%d, xid=%lx, lbt=%s",
            this, pin_count_.load(), page_.page_num, file_desc_, xid, lbt());

羽飞's avatar
羽飞 已提交
118 119 120
  if (--write_recursive_count_ == 0) {
    write_locker_ = 0;
  }
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
  lock_.unlock();
}

void Frame::read_latch()
{
  read_latch(get_default_debug_xid());
}

void Frame::read_latch(intptr_t xid) 
{
  {
    std::scoped_lock debug_lock(debug_lock_);
    ASSERT(pin_count_ > 0, "frame lock. read lock failed while pin count is invalid."
           "this=%p, pin=%d, pageNum=%d, fd=%d, xid=%lx, lbt=%s",
           this, pin_count_.load(), page_.page_num, file_desc_, xid, lbt());

    ASSERT(xid != write_locker_,
           "frame lock read while holding the write lock."
           "this=%p, pin=%d, pageNum=%d, fd=%d, xid=%lx, lbt=%s",
           this, pin_count_.load(), page_.page_num, file_desc_, xid, lbt());
  }

羽飞's avatar
羽飞 已提交
143 144 145 146 147 148 149 150 151
  lock_.lock_shared();

  {
    scoped_lock debug_lock(debug_lock_);
    int recursive_count = ++read_lockers_[xid];
    LOG_DEBUG("frame read lock success."
              "this=%p, pin=%d, pageNum=%d, fd=%d, xid=%lx, recursive=%d, lbt=%s",
              this, pin_count_.load(), page_.page_num, file_desc_, xid, recursive_count, lbt());
  }
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
}

bool Frame::try_read_latch()
{
  intptr_t xid = get_default_debug_xid();
  {
    std::scoped_lock debug_lock(debug_lock_);
    ASSERT(pin_count_ > 0, "frame try lock. read lock failed while pin count is invalid."
           "this=%p, pin=%d, pageNum=%d, fd=%d, xid=%lx, lbt=%s",
           this, pin_count_.load(), page_.page_num, file_desc_, xid, lbt());

    ASSERT(xid != write_locker_,
           "frame try to lock read while holding the write lock."
           "this=%p, pin=%d, pageNum=%d, fd=%d, xid=%lx, lbt=%s",
           this, pin_count_.load(), page_.page_num, file_desc_, xid, lbt());
  }

羽飞's avatar
羽飞 已提交
169
  bool ret = lock_.try_lock_shared();
170 171
  if (ret) {
    debug_lock_.lock();
羽飞's avatar
羽飞 已提交
172
    int recursive_count = ++read_lockers_[xid];
173
    LOG_DEBUG("frame read lock success."
羽飞's avatar
羽飞 已提交
174 175
              "this=%p, pin=%d, pageNum=%d, fd=%d, xid=%lx, recursive=%d, lbt=%s",
              this, pin_count_.load(), page_.page_num, file_desc_, xid, recursive_count, lbt());
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
    debug_lock_.unlock();
  }

  return ret;
}

void Frame::read_unlatch()
{
  read_unlatch(get_default_debug_xid());
}

void Frame::read_unlatch(intptr_t xid)
{
  {
    std::scoped_lock debug_lock(debug_lock_);
    ASSERT(pin_count_.load() > 0,
            "frame lock. read unlock failed while pin count is invalid."
            "this=%p, pin=%d, pageNum=%d, fd=%d, xid=%lx, lbt=%s",
           this, pin_count_.load(), page_.page_num, file_desc_, xid, lbt());

羽飞's avatar
羽飞 已提交
196 197 198 199
#if DEBUG
    auto read_lock_iter = read_lockers_.find(xid);
    int recursive_count = read_lock_iter != read_lockers_.end() ? read_lock_iter->second : 0;
    ASSERT(recursive_count > 0,
200
           "frame unlock while not holding read lock."
羽飞's avatar
羽飞 已提交
201 202 203 204 205 206 207 208
           "this=%p, pin=%d, pageNum=%d, fd=%d, xid=%lx, recursive=%d, lbt=%s",
           this, pin_count_.load(), page_.page_num, file_desc_, xid, recursive_count, lbt());

    if (1 == recursive_count) {
      read_lockers_.erase(xid);
    }

#endif // DEBUG
209 210 211 212 213 214 215

  }

  LOG_DEBUG("frame read unlock success."
            "this=%p, pin=%d, pageNum=%d, fd=%d, xid=%lx, lbt=%s",
            this, pin_count_.load(), page_.page_num, file_desc_, xid, lbt());

羽飞's avatar
羽飞 已提交
216
  lock_.unlock_shared();
217 218 219 220 221 222 223 224 225
}

void Frame::pin()
{
  std::scoped_lock debug_lock(debug_lock_);

  intptr_t xid = get_default_debug_xid();
  int pin_count = ++pin_count_;

羽飞's avatar
羽飞 已提交
226 227
  LOG_DEBUG("after frame pin. "
            "this=%p, write locker=%lx, read locker has xid %d? pin=%d, fd=%d, pageNum=%d, xid=%lx, lbt=%s",
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
            this, write_locker_, read_lockers_.find(xid) != read_lockers_.end(), 
            pin_count, file_desc_, page_.page_num, xid, lbt());
}

int Frame::unpin()
{
  intptr_t xid = get_default_debug_xid();

  ASSERT(pin_count_.load() > 0,
         "try to unpin a frame that pin count <= 0."
         "this=%p, pin=%d, pageNum=%d, fd=%d, xid=%lx, lbt=%s",
         this, pin_count_.load(), page_.page_num, file_desc_, xid, lbt());
  
  std::scoped_lock debug_lock(debug_lock_);

  int pin_count = --pin_count_;

  LOG_DEBUG("after frame unpin. "
            "this=%p, write locker=%lx, read locker has xid? %d, pin=%d, fd=%d, pageNum=%d, xid=%lx, lbt=%s",
            this, write_locker_, read_lockers_.find(xid) != read_lockers_.end(), 
            pin_count, file_desc_, page_.page_num, xid, lbt());
  
  if (0 == pin_count) {
    ASSERT(write_locker_ == 0,
           "frame unpin to 0 failed while someone hold the write lock. write locker=%lx, pageNum=%d, fd=%d, xid=%lx",
           write_locker_, page_.page_num, file_desc_, xid);
    ASSERT(read_lockers_.empty(),
           "frame unpin to 0 failed while someone hold the read locks. reader num=%d, pageNum=%d, fd=%d, xid=%lx",
           read_lockers_.size(), page_.page_num, file_desc_, xid);
  }
  return pin_count;
}


unsigned long current_time()
{
  struct timespec tp;
  clock_gettime(CLOCK_MONOTONIC, &tp);
  return tp.tv_sec * 1000 * 1000 * 1000UL + tp.tv_nsec;
}

void Frame::access()
{
  acc_time_ = current_time();
}

羽飞's avatar
羽飞 已提交
274
string to_string(const Frame &frame)
275
{
羽飞's avatar
羽飞 已提交
276
  stringstream ss;
277 278 279 280 281 282 283 284
  ss << "frame id:" << to_string(frame.frame_id()) 
     << ", dirty=" << frame.dirty()
     << ", pin=" << frame.pin_count()
     << ", fd=" << frame.file_desc()
     << ", page num=" << frame.page_num()
     << ", lsn=" << frame.lsn();
  return ss.str();
}