-
Notifications
You must be signed in to change notification settings - Fork 0
ASP25SCM06V/assign2_buffer_manager_G18
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
# Buffer Manager ## Prerequisites ``` sh $ apt install gcc $ apt install valgrind ``` CONTRIBUTIONS TABLE =================== Group No.: 18 Individual Contribution - Buffer Manager CWID | Name | Contribution | Percentage ------------|--------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------|------------- A20596106 | Anish Viswanathan Vuthukotai Ravi Chandran | Buffer Pool Initialization & Shutdown: Implemented initBufferPool, shutdownBufferPool, managed memory allocation/deallocation. | 25% A20562455 | Mohith Panchatcharam | Page Replacement Strategies: Developed logic for LRU, FIFO, optimized page swaps. | 25% A20581400 | Koushik Anand | Buffer Pool Operations: Implemented pinPage, unpinPage, forcePage, maintained buffer consistency. | 25% A20584131 | Mir Mustafa Ali | Testing & Debugging: Designed test cases, fixed bugs, created Makefile. | 25% RUNNING THE SCRIPT ==================== Open the Terminal and navigate to the Project root directory (Assignment2_BufferManager). Type make clean to delete any old compiled .o files. Type make to compile all the project files. Type make run_test1 to execute the test_assign2_1.c file. SOLUTION DESCRIPTION ====================== We have built a Buffer Management system, implementing various page replacement strategies while ensuring efficient memory management. HELPER FUNCTIONS ==================== writeBlockToDisk(...) This function writes a page from the buffer pool to disk. setNewPageToPageFrame(...) This function sets a new page into a specific page frame. PAGE REPLACEMENT ALGORITHM FUNCTIONS ========================================= These functions handle different page replacement strategies (FIFO, LRU, LFU, CLOCK) when pages are pinned. When the buffer pool is full and a new page needs to be pinned, one of the pages in the buffer pool needs to be replaced. The strategy determines which page will be replaced. FIFO(...) FIFO (First In, First Out) works similarly to a queue. The first page that was added to the buffer pool is the first one to be replaced. LFU(...) LFU (Least Frequently Used) removes the page that has been used the least. A reference counter, refNum, keeps track of how often each page is accessed. The page with the lowest reference count gets replaced. LRU(...) LRU (Least Recently Used) replaces the page that hasn’t been used in the longest time. The hitNum counter tracks how often a page is pinned, and the page with the lowest hitNum gets replaced. CLOCK(...) The CLOCK algorithm works by replacing the last added page in the buffer pool. It uses a hitNum counter and a pointer to determine which page to replace. BUFFER POOL FUNCTIONS =========================== These functions are responsible for managing the buffer pool, which stores pages in memory. The buffer pool is created for a page file that is stored on disk, using the Storage Manager (from Assignment 1) to perform disk operations. initBufferPool(...) This function initializes a new buffer pool in memory. The size of the buffer pool is defined by numPages, while pageFileName stores the name of the page file being cached. The page replacement strategy is specified by the strategy parameter, and stratData is used to pass any additional parameters for the strategy. shutdownBufferPool(...) This function shuts down the buffer pool. It frees all memory resources used by the buffer pool and, before destroying the pool, calls forceFlushPool(...) to write all dirty pages (modified pages) to the disk. If any page is still being used by a client, it throws an error (RC_PINNED_PAGES_IN_BUFFER). forceFlushPool(...) This function writes all dirty pages to disk. A page is considered dirty if its isDirtyPage flag is set to 1. The function checks all pages in the buffer pool to find dirty pages that are not being used by any client (fixCount == 0), and writes them to disk. PAGE MANAGEMENT FUNCTIONS ========================== These functions are responsible for loading pages from disk into the buffer pool (pinning pages), removing pages from the buffer pool (unpinning), marking pages as dirty, and forcing pages to be written to disk. pinPage(...) This function pins a page by reading it from the disk and storing it in the buffer pool. If there is space available in the buffer pool, the page is simply stored. If the pool is full, one of the pages is replaced using the chosen page replacement strategy. The algorithm determines which page to replace, and if the page being replaced is dirty (i.e., dirtyBit == 1), its content is written to the disk before the new page is pinned. unpinPage(...) This function unpins a page, meaning the page is no longer in use. The page to unpin is identified by its pageNum, and its fixCount is decreased by 1, indicating that a client is no longer using the page. makeDirty(...) This function marks the specified page as dirty, which means its content has been modified. It searches for the page in the buffer pool by its pageNum and sets the dirtyBit to 1. forcePage(...) This function forces a specified page to be written to disk. It searches for the page in the buffer pool and uses the Storage Manager functions to write the content to disk. After the content is written, it sets the dirtyBit to 0, indicating that the page is no longer dirty. STATISTICS FUNCTIONS ======================= The statistics related functions are used to gather some information about the buffer pool. So it provides various statistical information about the buffer pool. getFrameContents(...) --> This function returns an array of PageNumbers. The array size = buffer size (numPages). --> We iterate over all the page frames in the buffer pool to get the pageNum value of the page frames present in the buffer pool. --> The "n"th element is the page number of the page stored in the "n"th page frame. getDirtyFlags(...) --> This function returns an array of bools. The array size = buffer size (numPages). --> We iterate over all the page frames in the buffer pool to get the dirtyBit value of the page frames present in the buffer pool. --> The "n"th element is the TRUE if the page stored in the "n"th page frame is dirty. getFixCounts(...) --> This function returns an array of ints. The array size = buffer size (numPages). --> We iterate over all the page frames in the buffer pool to get the fixCount value of the page frames present in the buffer pool. --> The "n"th element is the fixCount of the page stored in the "n"th page frame. getNumReadIO(...) --> This function returns the count of total number of IO reads performed by the buffer pool i.e. number of pages read from the disk. --> We maintain this data using the rearIndex variable. getNumWriteIO(...) --> This function returns the count of total number of IO writes performed by the buffer pool i.e. number of pages written to the disk. --> We maintain this data using the writeCount variable. We initialize writeCount to 0 when buffer pool is initialized and increment it whenever a page frame is written to disk. Code Structure ================== --> `Frame` represents a page entry in buffer pool and `BufferPoolMgm_Info` represents the buffer pool metadata which is used for implementing the buffer manager. ``` c // Page Frame: each array entry in buffer pool typedef struct Frame { PageNumber pageNum; // which page is currently stored in the frame int pinCount; // how many processes are using this page int dirtyBit; // whether the page has been modified char* data; // points to the area in memory storing the content of the page } Frame; // The used frame information typedef struct BufferPoolMgm_Info { int front; // the first page in the buffer int rear; // the last page in the buffer int frameCnt; // the number of used frames in this buffer pool int capacity; // the total nuumber of frame the page cache can store int numRead; //stores number of pages that have been read int numWrite; //stores number of pages that been written int* arr; }BufferPoolMgm_Info; ``` --> Additional functions were implemented for managing the FIFO and LRU replacement strategy in buffer pool. For further internal details, refer `buffer_mgr.c`. ``` c // checks whether the buffer pool is full int isFull(); // checks whether the buffer pool is empty int isEmpty(); // pin the page with the given page number using FIFO strategy RC pinPageWithFIFO(BM_BufferPool *const bm, BM_PageHandle *const page, int pageNum); // remove the page using FIFO strategy RC removePageWithFIFO(BM_BufferPool *const bm, BM_PageHandle *const page); // pin the page with the given page number using LRU strategy RC pinPageWithLRU(BM_BufferPool *const bm, BM_PageHandle *const page, const PageNumber pageNum); // remove the page using LRU strategy Frame* removePageWithLRU(BM_BufferPool *const bm, BM_PageHandle *const page, int leastUsedPage); // updates the LRUs of the pages in the buffer pool RC updateLRUOrder(int pageNum); // search a page from the buffer pool using the page number Frame* searchPageFromBuffer(Frame** frames, int pageNum); ``` --> Memory leak erros in `buffer_mgr_stat.c` file are fixed by freeing the allocated memory in the functions `printPoolContent` and `sprintPoolContent`. ``` c void printPoolContent (BM_BufferPool *const bm) { // ... free(frameContent); free(dirty); free(fixCount); } char * sprintPoolContent (BM_BufferPool *const bm) { // ... free(frameContent); free(dirty); free(fixCount); return message; } ``` --> The test cases are implemented in `test_assign2_1.c` files. The test cases are implemented for testing the buffer manager with FIFO and LRU replacement strategy.
About
No description, website, or topics provided.
Resources
Stars
Watchers
Forks
Releases
No releases published
Packages 0
No packages published