SwatDB
Loading...
Searching...
No Matches
bufmgr.h
Go to the documentation of this file.
1/*
2 * SwatDB
3 *
4 * @authors: See Contributors.doc for code contributors
5 *
6 * Copyright (c) 2020,2025 Swarthmore College Computer Science Department
7 * Swarthmore PA, Professors Tia Newhall, Ameet Soni
8 */
9
10// @@@@ SWATDB_ASSIGNMENT: ADDTODODATE
11
12#ifndef _SWATDB_BUFMGER_H_
13#define _SWATDB_BUFMGER_H_
14
20#include <utility>
21#include <unordered_map>
22#include <mutex>
23#include <list>
24#include <queue>
25
26#include "swatdb_types.h"
27#include "page.h" // need for alignment of Page object
28#include "bm_buffermap.h" // BufferMap class
29#include "bm_frame.h" // Frame class
30
31
32// @@@@ SWATDB_ASSIGNMENT: CUT_START
33// if this is defined: disables some policies for lab assignmnet solution
34// (right now if defined it includes Random and Clock only)
35// Either uncomment this or -DBUFMGR_LAB_SOL_OMIT=1 in Makefile to define this
36//#define BUFMGR_LAB_SOL_OMIT (1)
37// @@@@ SWATDB_ASSIGNMENT: CUT_END
38
39// can't just include bm_replacement.h or circular dependenices with .h file
40// (one shortcomming of #pragma once)
42class DiskManager;
43
44
45
46// @@@@ SWATDB_ASSIGNMENT: NOTEMSG Do not modify this struct
52
56 std::uint32_t total;
57
61 std::uint32_t valid;
62
66 std::uint32_t pinned;
67
71 std::uint32_t unpinned;
72
76 std::uint32_t dirty;
77
79
87 std::uint64_t rep_calls;
88
92 std::uint64_t new_page_calls;
93
99
103 std::uint32_t ref_bit;
104
108 std::uint32_t clock_hand;
109 } replace_stats;
110
111};
112
113
114// @@@@ SWATDB_ASSIGNMENT: NOTEMSG Do not modify this definition, you may add private helper methods only
122
123 public:
124
140
148
171 std::pair<Page*,PageId> allocatePage(FileId file_id);
172
173// @@@@ SWATDB_ASSIGNMENT: CUT_START
191 std::pair<Page*, PageId> allocateAnonymousPage();
192// @@@@ SWATDB_ASSIGNMENT: CUT_END
193
211 void deallocatePage(PageId page_id);
212
213// @@@@ SWATDB_ASSIGNMENT: CUT_START
228// @@@@ SWATDB_ASSIGNMENT: CUT_END
229
253 Page* getPage(PageId page_id);
254
268 void releasePage(PageId page_id, bool dirty);
269
280 void setDirty(PageId page_id);
281
295 void flushPage(PageId page_id);
296
308 void createFile(FileId file_id);
309
327 void removeFile(FileId file_id);
328
329// @@@@ SWATDB_ASSIGNMENT: CUT_START
337// @@@@ SWATDB_ASSIGNMENT: CUT_END
338
345
349 std::uint32_t getNumUnpinned();
350
358
365
371 void printFrame(FrameId frame_id);
372
379 void printPage(PageId page_id);
380
389
390
397
398 private:
399
407
413
418
424
430
431 // @@@@ SWATDB_ASSIGNMENT: NOTEMSG you may add private helper methods here
432
433 // @@@@ SWATDB_ASSIGNMENT: CUT_START
437 std::mutex buf_map_mtx;
438 // @@@@ SWATDB_ASSIGNMENT: CUT_END
439
440
443 // @@@@ SWATDB_ASSIGNMENT: CUT_START
459 FrameId _bufferReplace();
460 // @@@@ SWATDB_ASSIGNMENT: CUT_END
461
471 void _printFrameHelper(FrameId frame_id);
472
475};
476
477#endif
Definition bufmgr.h:121
void setDirty(PageId page_id)
Set the Page of the given PageId dirty.
void deallocateAnonymousPage(PageId page_id)
Removes the Page of the given anonymous PageId from the buffer pool, and deallocates the Page from th...
void flushPage(PageId page_id)
Flushes the Page of the given PageId to disk.
void printValidFrames()
THIS METHOD IS FOR DEBUGGING ONLY. Prints Frame state of every valid Frame in the buffer pool,...
~BufferManager()
BufferManager destructor.
std::pair< Page *, PageId > allocateAnonymousPage()
Allocates an anonymous Page for the file of given FileId. The Page is allocated both in the buffer po...
void printPage(PageId page_id)
THIS METHOD IS FOR DEBUGGING ONLY. Prints Frame state of given PageId, including FrameId,...
void createFile(FileId file_id)
Calls createFile() method on the DiskManager to create new Unix file that corresponds to the given Fi...
ReplacementPolicy * replacement_pol
Definition bufmgr.h:429
void clearBuffer()
THIS METHOD IS FOR DEBUGGING ONLY. Clears the entire buffer pool, resetting all frames and removing p...
void printBufferState()
THIS METHOD IS FOR DEBUGGING ONLY. Prints current buffer state, including total number of pages,...
void printReplacementStats()
This method is for performance tests. Prints number of calls to replacment policy,...
BufferManager(DiskManager *disk_mgr, RepType rep_type)
BufferManager constructor. Initializes the buf_pool and frame_table, and stores a pointer to SwatDB's...
void printAllFrames()
THIS METHOD IS FOR DEBUGGING ONLY. Prints Frame state of every Frame in the buffer pool,...
Frame frame_table[BUF_SIZE]
Definition bufmgr.h:412
void removeFile(FileId file_id)
Calls removeFile() method on the DiskManager. Checks that none of the file's pages are pinned in the ...
BufferMap buf_map
Definition bufmgr.h:406
std::uint32_t getNumUnpinned()
Return the amount of unpinned pages in the buffer pool.
void printFrame(FrameId frame_id)
THIS METHOD IS FOR DEBUGGING ONLY. Prints Frame state of given FrameId, including pin count,...
Page * getPage(PageId page_id)
Gets Page by page_id, pins the Page, and returns a pointer to the Page object.
std::mutex buf_map_mtx
Definition bufmgr.h:437
BufferState getBufferState()
THIS METHOD IS FOR DEBUGGING ONLY. Returns the current state of the buffer pool.
void deallocatePage(PageId page_id)
Removes the Page of the given PageId from the buffer pool, and deallocates the Page from the appopria...
std::pair< Page *, PageId > allocatePage(FileId file_id)
Allocates a Page for the file of given FileId. The Page is allocated both in the buffer pool,...
Page buf_pool[BUF_SIZE]
Definition bufmgr.h:417
DiskManager * disk_mgr
Definition bufmgr.h:423
void releasePage(PageId page_id, bool dirty)
Unpins a Page in the buffer pool.
Definition bm_buffermap.h:38
Definition diskmgr.h:38
Definition bm_frame.h:28
Definition page.h:27
Definition bm_replacement.h:29
Definition bufmgr.h:78
std::uint32_t ref_bit
Definition bufmgr.h:103
double avg_frames_checked
Definition bufmgr.h:98
std::uint64_t new_page_calls
Definition bufmgr.h:92
std::uint64_t rep_calls
Definition bufmgr.h:87
RepType rep_type
Definition bufmgr.h:83
std::uint32_t clock_hand
Definition bufmgr.h:108
Definition bufmgr.h:51
std::uint32_t pinned
Definition bufmgr.h:66
std::uint32_t total
Definition bufmgr.h:56
std::uint32_t valid
Definition bufmgr.h:61
std::uint32_t dirty
Definition bufmgr.h:76
std::uint32_t unpinned
Definition bufmgr.h:71
Definition swatdb_types.h:54
std::uint32_t FrameId
Index of each frame in the bufferpool of BufferManager.
Definition swatdb_types.h:44
const std::uint32_t BUF_SIZE
Definition swatdb_types.h:292
std::uint32_t FileId
Definition swatdb_types.h:33
RepType
Definition swatdb_types.h:167