CMU 15-445 Bustub Project 1 - Buffer Pool Manager - Note

Last updated on September 28, 2026 am

This article is currently an experimental machine translation and may contain errors. If anything is unclear, please refer to original Chinese version. I am continuously working to improve the translation.

Preface

Since there might be a chance to work on cloud database-related things, I picked up this course that I had always wanted to take but never did.

The difficulty feels genuinely over the top.

I basically completed it with AI "nudging me along" — writing it purely by hand was a bit beyond my difficulty threshold... (same as xv6). Mourning my hand-writing ability.

Setting Up the Environment

My dev machine is Arch Linux, with system toolchain clang 22 and libstdc++ 16, while Fall 2025's BusTub uses clang 15, but AUR no longer provides it either — unless I compile one myself, but that would take too long.

In the end, I used an Ubuntu 22.04 Docker image to unify the build environment. The image installs clang, clang-format, clang-tidy 15, CMake, and BusTub's build dependencies; the repository is mounted into the container, and build artifacts go into a separate build/ubuntu directory.

The build_support/run_in_ubuntu.sh script in the repository automatically builds the image on first run. First configure the project:

./build_support/run_in_ubuntu.sh cmake -S . -B build/ubuntu \
  -DCMAKE_BUILD_TYPE=Debug \
  -DCMAKE_C_COMPILER=/usr/bin/clang-15 \
  -DCMAKE_CXX_COMPILER=/usr/bin/clang++-15 \
  -DCLANG_FORMAT_BIN=/usr/bin/clang-format-15 \
  -DCLANG_TIDY_BIN=/usr/bin/clang-tidy-15

Debug builds enable AddressSanitizer by default. P1's public tests can be compiled and run directly inside the container:

for test in arc_replacer_test disk_scheduler_test page_guard_test buffer_pool_manager_test; do
  ./build_support/run_in_ubuntu.sh cmake --build build/ubuntu --target "$test" -j 4
  ./build_support/run_in_ubuntu.sh "./build/ubuntu/test/$test"
done

Before submitting, check formatting and clang-tidy:

./build_support/run_in_ubuntu.sh cmake --build build/ubuntu --target check-format
./build_support/run_in_ubuntu.sh cmake --build build/ubuntu --target check-clang-tidy-p1

Prerequisite Knowledge for the Project

Actually not much — you just need to be clear about the relationship between "page" and "frame":

  • A page is a page that actually exists on disk, although in an OS it's called a Block
  • A frame is the in-memory cache of a disk page, although in an OS this is called a page

Task 1

Task 1 is mainly about getting to the root of the eviction algorithm. To clarify the essence of ARC — the transitions among MRU, MFU, and the two Ghost lists — here's a state machine diagram:

stateDiagram-v2
    state "未记录" as None
    state "MRU" as MRU
    state "MFU" as MFU
    state "MRU Ghost" as MRUG
    state "MFU Ghost" as MFUG

    [*] --> None
    None --> MRU: 首次访问
    MRU --> MFU: 再次访问
    MFU --> MFU: 再次访问
    MRU --> MRUG: Evict
    MFU --> MFUG: Evict
    MRUG --> MFU: 再次访问,增大 MRU 目标
    MFUG --> MFU: 再次访问,减小 MRU 目标
    MRUG --> None: 历史记录被裁剪
    MFUG --> None: 历史记录被裁剪
    MRU --> None: Remove
    MFU --> None: Remove

The diagram shows which list a page is currently in. MRU and MFU record pages still in memory, while Ghost only keeps the evicted page numbers, unrelated to frames. Whether a page can be evicted at any given moment is controlled by evictable_.

Once that's clear, it's really just translating code.

However, understanding why it's done this way is more beneficial than simply translating natural language into code. If you truly understand ARC, the transitions among MRU, MFU, and Ghost all come naturally:

  • Hitting an MRU Ghost means a just-evicted page was accessed again, so the MRU target size is adjusted upward;
  • Hitting an MFU Ghost adjusts it downward.
  • On eviction: if |mru_| >= mru_target_size_, evict from mru_ first; otherwise, evict from mfu_ first. There are two special points in the BusTub comments:
    1. When mru_.size() == mru_target_size_, still evict from mru_.
    2. If the preferred side is all pinned / non-evictable, try the other side. In summary, simply put: decide the preferred eviction side based on mru_.size() and mru_targetsize; if nothing is found there, check the other side; if neither has one, return.

My initial implementation used std::find() to search the linked list, which had poor time complexity. Later I added corresponding iterators in FrameStatus:

  • When a node is placed into a list, store its iterator in that list, so it can be found directly via the map afterwards.
  • Map lookup is $O(1)$ on average, and the original iterator remains valid after moves.

However, this only eliminates the scan for finding the node; Evict() may still traverse the list when looking for an evictable node. There's room for further optimization, but I didn't do it.

Points I missed, summarized from my Q&A records with the LLM

  • alive_map_ looks up records currently in memory by frame_id, while ghost_map_ looks up historical records by page_id, so after a Ghost hit, you can't directly move the node storing page_id into the MRU/MFU that stores frame_id.
  • The target position of splice() must also be an iterator into the target list.
  • The preferred list having no evictable nodes doesn't mean the other list doesn't either.

Task 2

As for Task 2, it's actually not hard once you understand the APIs. In other words, I think the difficulty in understanding mainly lies in unfamiliarity with the framework code and C++.

The Disk Scheduler has a worker thread responsible for processing queued requests. Schedule() puts a DiskRequest into a Channel, StartWorkerThread() picks up requests in the background, and then calls the DiskManager's ReadPage() / WritePage(). For a Go programmer, Channel is actually quite familiar. Here, the worker thread exits by putting a std::nullopt into the queue; the destructor calls join(), and upon reading the std::nullopt, the thread ends.

Also worth noting are std::promise<bool> and std::future<bool>. They are used to pass a one-time processing result between threads:

  • The thread that initiated the request calls future.get(), which blocks if the result isn't ready yet;
  • After the worker thread finishes the I/O, it notifies the requester via promise.set_value(true) in the request, a bit like waiting for a result using a Go Channel.
  • Though I think it has nothing to do with JavaScript's Promise, despite the same name.

Task 3

Task 3 gets complicated. The main issue is that the dependencies between code are too complex — you can't untangle it just by reading, and even having AI draw out the call chains doesn't help you remember them. It's better to just start writing, and then it becomes natural.

The core is PageGuard and the BufferPoolManager. PageGuard is the only legitimate way to access a page, encapsulating operations on the page. It was originally split into Read and Write, but I restructured it into an inheritance pattern, moving the common members and functions into an abstract base class PageGuard.

Another key point is that member variables need to be added, because the comments mention that a PageGuard must hold the lock at all times:

However, the existence of any ReadPageGuard on a page implies that no thread can be mutating the page's data.

So, a read Guard holds a shared lock on the page, and a write Guard holds an exclusive lock:

// ReadPageGuard
std::shared_lock<std::shared_mutex> read_lock_{frame_->rwlatch_};
// WritePageGuard
std::unique_lock<std::shared_mutex> read_lock_{frame_->rwlatch_};

Note that a PageGuard holds the page lock for the entire duration from construction to Drop(), so no temporary locking is needed at all.

As for the BufferPoolManager, the most complex parts are still CheckedReadPage() and CheckedWritePage(). The comments mention three cases to consider:

  1. The page is already in page_table_: find the corresponding frame, no disk read needed.
  2. The page is not in memory, but free_frames_ has a free frame: use it to hold this page, which requires reading from disk.
  3. No free frame: let ARC find an evictable frame; if the old page is dirty, write it back to disk first, then read in the page to be accessed; if nothing can be evicted, return std::nullopt.

The concurrency lock control inside is also troublesome. Most notably, the increment and decrement of pin counts must be coordinated with the pool lock: when a pin count goes from 0 to 1, or from 1 to 0, the ARC's evictable state for that frame must also be updated accordingly.

Still, I think the Flush family of functions is the most complex, mainly because concurrent code is just too hard to write. When to lock, when not to, the relationship between the pool lock and frame locks — it's genuinely complicated.

The most notable point here is: PageGuard::Flush() can rely on the frame lock the Guard already holds; BPM's FlushPage() has no Guard, so it must first find the frame under the pool lock, temporarily pin++ and set it non-evictable, release the pool lock, then acquire the frame lock to write to disk, and finally pin-- and set evictable as needed.

FlushPageUnsafe() is not allowed to hold the frame lock, but is allowed to use the pool lock, to maintain thread safety of page_table_ access.


CMU 15-445 Bustub Project 1 - Buffer Pool Manager - Note
https://blog.wendain.ing/en/posts/cmu-15-445-bustub-proj1/
Author
wendaining
Posted on
September 28, 2026
Updated on
September 28, 2026
Licensed under