SwatDB
Loading...
Searching...
No Matches
Public Member Functions | Private Member Functions | Private Attributes | List of all members
Clock Class Reference

#include <bm_policies.h>

Inheritance diagram for Clock:
Inheritance graph
[legend]
Collaboration diagram for Clock:
Collaboration graph
[legend]

Public Member Functions

 Clock (Frame *frame_table)
 Clock constructor. Initializes pointer to buffer manager's frame_table, creates the free list, sets clock_hand, rep_calls, and avg_frames_checked to 0, and sets all ref_bits to false.
 
 ~Clock ()
 
FrameId replace ()
 Method that implements core replacement policy functionality of the clock replacement policy.
 
void pin (FrameId frame_id)
 Frame is being pinned with pin count going from 0 to 1. Clock policy has nothing to do here, but an empty method must exist for BufferManager to call.
 
void unpin (FrameId frame_id)
 Frame is being unpinned with pin count going from 1 to 0. Set the ref_bit associated with this frame_id to true.
 
void getRepStats (struct BufferState::ReplacementStats *rep_stats)
 Updates the struct of replacement policy statistics within the buffer state struct. Adds the replacement type, number of calls to the replacement policy, average frames checked by each call to policy, the ref_bit count and clock_hand, and new page calls.
 
void freeFrame (FrameId frame_id)
 The frame is invalidated in the buffer manager, and is added to the free list of frames in the replacement policy. Ref_bit associated with this frame is set to false.
 
void printFrame (FrameId frame_id)
 THIS METHOD IS FOR DEBUGGING ONLY. Prints Frame state, including pin count and ref_bit.
 
void printStats ()
 Prints clock_hand, average frames checked, replacement calls, how many frames have ref_bit set.
 
- Public Member Functions inherited from ReplacementPolicy
virtual ~ReplacementPolicy ()
 Empty Destructor.
 
void printFrame (FrameId frame_id)
 THIS METHOD IS FOR DEBUGGING ONLY. Prints Frame state relevant to the replacement policy of a given FrameId, including pin count and ref_bit for Clock Replacement.
 
void advanceCalls ()
 Increment new_page_calls by 1. Called in both allocatePage and getPage in BufferManager.

 

Private Member Functions

void _advanceClock ()
 Increments the clock hand according to the clock replacement policy.
 
RepType _getType ()
 Derived class from pure virtual function that returns replacement policy type specific to this derived class.
 

Private Attributes

std::uint32_t clock_hand
 
bool ref_table [BUF_SIZE]
 

Additional Inherited Members

- Protected Member Functions inherited from ReplacementPolicy
void _createFree ()
 Creates a free list for the buffer pool by checking each frame in the buffer pool and checking if it is valid. Used in constructor of each replacement policy.
 
- Protected Attributes inherited from ReplacementPolicy
Frameframe_table
 
std::queue< FrameIdfree
 
std::uint64_t rep_calls
 
double avg_frames_checked
 
std::uint64_t new_page_calls
 

Detailed Description

SwatDB Clock Class. Clock is a derived class of ReplacementPolicy that manages the buffer replacement policy of the DBMS. The Clock algorithm is an approximation of least recently used with less overhead. Chooses a page for replacement using a clock hand, which loops the buffer frames in a circular order. Each frame has an associated reference bit, which is turned on when the pin count goes to zero.

Constructor & Destructor Documentation

◆ Clock()

Clock::Clock ( Frame frame_table)

Clock constructor. Initializes pointer to buffer manager's frame_table, creates the free list, sets clock_hand, rep_calls, and avg_frames_checked to 0, and sets all ref_bits to false.

Precondition
BufferManager constructor is being called, frame_table parameter points to an initialized frame_table object.
Postcondition
A Clock object will be initialized with member variables initialized.
Parameters
Frame*frame_table, pointer to frame_table array of BufferManager.

◆ ~Clock()

Clock::~Clock ( )

Empty Destructor.

Member Function Documentation

◆ _advanceClock()

void Clock::_advanceClock ( )
private

Increments the clock hand according to the clock replacement policy.

Precondition
None.
Postcondition
clock_hand is advanced.

◆ _getType()

RepType Clock::_getType ( )
privatevirtual

Derived class from pure virtual function that returns replacement policy type specific to this derived class.

Returns
RepType ClockT.

Implements ReplacementPolicy.

◆ freeFrame()

void Clock::freeFrame ( FrameId  frame_id)
virtual

The frame is invalidated in the buffer manager, and is added to the free list of frames in the replacement policy. Ref_bit associated with this frame is set to false.

Precondition
A page in the buffer pool has been deallocated.
Postcondition
The replacement policy state is updated to reflect the freeing of a specific frame.
Parameters
FrameIdframe_id of the frame being set invalid.

Reimplemented from ReplacementPolicy.

◆ getRepStats()

void Clock::getRepStats ( struct BufferState::ReplacementStats rep_stats)
virtual

Updates the struct of replacement policy statistics within the buffer state struct. Adds the replacement type, number of calls to the replacement policy, average frames checked by each call to policy, the ref_bit count and clock_hand, and new page calls.

Parameters
Pointerto the ReplacementStats struct member of BufferState struct.

Reimplemented from ReplacementPolicy.

◆ pin()

void Clock::pin ( FrameId  frame_id)
virtual

Frame is being pinned with pin count going from 0 to 1. Clock policy has nothing to do here, but an empty method must exist for BufferManager to call.

Precondition
A page in the buffer pool has been pinned, and the pin count of that frame has increased from 0 to 1.
Postcondition
Nothing.
Parameters
FrameIdframe_id of the frame being unpinned.

Implements ReplacementPolicy.

◆ printStats()

void Clock::printStats ( )
virtual

Prints clock_hand, average frames checked, replacement calls, how many frames have ref_bit set.

Implements ReplacementPolicy.

◆ replace()

FrameId Clock::replace ( )
virtual

Method that implements core replacement policy functionality of the clock replacement policy.

Precondition
The replacement policy has been invoked. The buffer manager is attempting to add a frame to the buffer pool. The buffer map is locked.
Postcondition
If there is a free (invalid) frame, that FrameID is chosen for replacement and is returned. If there is a Page that is valid, and is not referenced, it is selected for eviction and FrameId is returned. In the process of performing the replacement algorithm, ref_bit of some valid pages that are not pinned may be set to false.
Returns
FrameID of the frame that is eligible for replacement in the buffer pool.
Exceptions
InsufficientSpaceBufMgrif all pages in the buffer pool are pinned.

Implements ReplacementPolicy.

◆ unpin()

void Clock::unpin ( FrameId  frame_id)
virtual

Frame is being unpinned with pin count going from 1 to 0. Set the ref_bit associated with this frame_id to true.

Precondition
A page in the buffer pool has been unpinned from pin count 1 to 0.
Postcondition
Ref_bit of frame_id is set to true.
Parameters
FrameIdframe_id of the frame being unpinned.

Implements ReplacementPolicy.

Member Data Documentation

◆ clock_hand

std::uint32_t Clock::clock_hand
private

Clock hand which is a std::uint32_t, corresponding to the index of a Frame in frame_table.

◆ ref_table

bool Clock::ref_table[BUF_SIZE]
private

Parallel array to the frame_table which stores the ref_bits of each frame.


The documentation for this class was generated from the following file: