Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: THEend8_
CS 564 Programming Project 2
Buffer Manager
INTRODUCTION
The goal of the BadgerDB projects is to allow students in CS 564 to learn about the inter-
nals of a data processing engine. In this assignment, you will build a buffer manager, on
top of an I/O layer that we provide.
Logistics. BadgerDB is coded in C++ and runs on the CS REL machines. Here are a few
logistical points:
• Platform: The stages will be compiled and tested on the CS department’s 64-bit
Ubuntu Linux machines called rockhopper. We will use the latest g++ compiler
on those machines. You are free to develop on other platforms, but you must make
sure that your project works with the official configuration. You can log on to the
rockhopper pool remotely using the machines rockhopper-01.cs.wisc.edu
through rockhopper-09.cs.wisc.edu. You can get more information about
• Warnings: One of the strengths of C++ is that it does compile time code checking
(consequently reducing run-time errors). Try to take advantage of this by turning
on as many compiler warnings as possible. The Makefile that we will supply will
have -Wall on as default.
• Tools: Always be on the lookout for tools that might simplify your job. Exam-
ple: make for compiling and building your project, makedepend for automatically
generating dependencies, perl for writing test scripts, valgrind for tracking down
memory errors, gdb for debugging, and git/svn for version control. While we will
not explicitly educate you about each of these, feel free to seek the TAs’ advice.
• Software Engineering: A large project such as this requires significant design effort.
Spend some time thinking before you start writing code.
Evaluation. We will run a bunch of our own (private) tests to check your code. So please
develop tests beyond the ones that we give you to stress test your solution. We will also
browse your code to review your coding style and read your Doxygen-generated files.
80% of each project grade is allocated to the correctness test, and 20% for your coding
style and clarity of documenting your code.
1
Academic Integrity. You are not allowed to share any code with other students in the
class. Nor will you attempt to use any code from previous offerings of this course. Devi-
ations from this will be punished to the fullest extent possible. We will use a code diffing
program to find cheaters.
THE BADGERDB I/O LAYER
The lowest layer of the BadgerDB database systems is the I/O layer. This layer allows
the upper level of the system to create/destroy files, allocate/deallocate pages within a
file and to read and write pages of a file. This layer consists of two classes: a file (class File)
and a page (class Page) class. These classes use C++ exceptions to handle the occurrence
of any unexpected event.
Implementation of the File class, the Page class, and the exception classes are provided
to you. To start, you can copy BufMgr.tar.gz to your private workspace and expand
this tarball using the following command:
/bin/zcat bufmgr.tar.gz | /bin/tar -xvf -
The code has been adequately commented to help you with understanding how it
does what it does. Please use Doxygen to generate documentation files by running the
command make doc inside the bufmgr directory. The doc files will be generated in the
docs directory. You can now open the docs/index.html file inside a browser and go
through descriptions of classes and their methods to better understand their implemen-
tation.
THE BADGERDB BUFFER MANAGER
A database buffer pool is an array of fixed-sized memory buffers called frames that
are used to hold database pages (also called disk blocks) that have been read from disk
into memory. A page is the unit of transfer between the disk and the buffer pool residing
in main memory. Most modern database systems use a page size of at least 8,192 bytes.
Another important thing to note is that a database page in memory is an exact copy of
the corresponding page on disk when it is first read in. Once a page has been read from
disk to the buffer pool, the DBMS software can update information stored on the page,
causing the copy in the buffer pool to be different from the copy on disk. Such pages are
termed dirty.
Since the database on disk itself is often larger than the amount of main memory that
is available for the buffer pool, only a subset of the database pages fit in memory at any
given time. The buffer manager is used to control which pages to keep in memory. When-
ever the buffer manager receives a request for a data page, it checks to see if the requested
page is already in the one of the frames that constitute the buffer pool. If so, the buffer
manager simply returns a pointer to the page. If not, the buffer manager frees a frame
(possibly writing the page it contains to disk if the page is dirty) and then reads in the
requested page from disk into the frame that has been freed.
2
Before reading further, you should first read the documentation that describes the
I/O layer of BadgerDB so that you understand its capabilities (described on the previous
page). In a nutshell, the I/O layer provides an object-oriented interface to the Unix file
with methods to open and close files and to read/write pages of a file. For now, the key
thing you need to know is that opening a file (by passing in a character string name) re-
turns an object of type File. This class has methods to read and write pages of the File.
You will use these methods to move pages between the disk and the buffer pool.
Buffer Replacement Policies and the Clock Algorithm.
There are many ways of deciding which page to replace when a free frame is needed.
Commonly used policies in operating systems are FIFO, MRU, and LRU. Even though
LRU is one of the most commonly used policies, it has high overhead and is not the best
strategy to use in a number of common cases that occur in database systems. Instead,
many systems use the clock algorithm that approximates LRU behavior and is much faster.
Figure 1 shows the conceptual layout of a buffer pool. Figure 2 illustrates the execution
of the clock algorithm.
Figure 1: Structure of the Buffer Manager
In Figure 1, each square box corresponds to a frame in the buffer pool. Assume that
the buffer pool contains numBufs frames, numbered 0 to numBufs− 1. Conceptually, all
the frames in the buffer pool are arranged in a circular list.
Associated with each frame is a bit termed the refbit. Each time a page in the buffer
pool is accessed (via a readPage() call to the buffer manager), the refbit of the correspond-
ing frame is set to true. When the buffer manager needs to replace a page, it starts advanc-
ing the clock hand (an integer whose value is between 0 and numBufs− 1) in a clockwise
direction, using modular arithmetic so that it does not go past numBufs − 1. For each
frame that the clock hand goes past, the refbit is examined and then cleared. If the bit is