-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuffer.h
313 lines (234 loc) · 7.79 KB
/
buffer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <list>
#include <unordered_map>
#include <stack>
#include <assert.h>
#include "const.h"
using namespace std;
struct bFrame
{
char field[FRAMESIZE];
};
struct BCB
{
BCB() {};
int page_id;
int frame_id;
int count = 0;
bool referenced = true;
int dirty = 0;
};
class DSMgr {
public:
DSMgr() {};
// OpenFile function is called anytime a file needs to be opened for reading or writing.
// The prototype for this function is OpenFile(String filename) and returns an error code.
// The function opens the file specified by the filename.
void openFile(string Filename);
// ReadPage function is called by the FixPage function in the buffer manager. This
// prototype is ReadPage(page_id, bytes) and returns what it has read in. This function calls
// fseek() and fread() to gain data from a file.
void readPage(int page_id, bFrame& frame);
// WritePage function is called whenever a page is taken out of the buffer. The
// prototype is WritePage(frame_id, frm) and returns how many bytes were written. This
// function calls fseek() and fwrite() to save data into a file.
int writePage(int page_id, bFrame& frm);
int getIONum() {
return io_num;
}
private:
FILE* currFile = NULL;
int io_num = 0;
};
class Bgr{
public:
Bgr() {
for (int i = 0; i < BUFFERSIZE; ++i) {
buf.push_back(bFrame());
}
};
void open(string fname) {
dsmgr.openFile(fname);
}
int fixPage(int page_id);
bFrame* readPage(int page_id);
void writePage(int page_id, char* data);
int UnfixPage(int page_id);
// returns the number of buffer pages that are free and able to be used.
// returns an integer from 0 to BUFFERSIZE-1(1023).
const int numFreeFrames() {return BUFFERSIZE - frame_num;};
void incNumFrames() {frame_num += 1;};
// SelectVictim function selects a frame to replace.
virtual int selectVictim() {return 0;};
void removeBCB(BCB* ptr);
BCB* addBCB(int page_id, int frame_id);
void SetDirty(int frame_id);
virtual void updateHitted(BCB* bcb) {};
virtual void updateUnhitted(BCB* bcb) {};
virtual void printTitle() {
cout << "##############################" << endl;
cout << "####### Buffer Manager #######" << endl;
cout << "##############################" << endl;
}
void printStat() {
printTitle();
cout << "Total Request : " << access_num<< endl;
cout << "Total IO : " << dsmgr.getIONum() << endl;
cout << "Total hit count : " << hit_count << endl;
cout << "Buffer hit rate : " << (float)(hit_count) / (float) access_num << endl;
}
private:
DSMgr dsmgr;
int frame_num = 0;
vector<bFrame> buf;
// For query BCB via frame id, frame to bcb
BCB* ftob[BUFFERSIZE];
unordered_map<int, int> ftop;
unordered_map<int, BCB*> ptof;
int hit_count = 0;
int access_num = 0;
};
class LRUBgr: public Bgr {
public:
virtual void updateHitted(BCB* bcb) {
auto iter = bcb2iter[bcb];
lru_list.erase(iter);
lru_list.push_back(bcb);
bcb2iter[bcb] = --lru_list.end();
}
virtual void updateUnhitted(BCB* bcb) {
lru_list.push_back(bcb);
bcb2iter[bcb] = --lru_list.end();
}
virtual int selectVictim() {
BCB* bcb = lru_list.front();
lru_list.pop_front();
return bcb->frame_id;
}
virtual void printTitle() {
cout << "##############################" << endl;
cout << "##### LRU Buffer Manager #####" << endl;
cout << "##############################" << endl;
}
private:
// Used for LRU
std::list<BCB*> lru_list;
// Hash map to list iterator for erase
std::unordered_map<BCB*, list<BCB*>::iterator> bcb2iter;
};
class LRU2Bgr: public Bgr {
public:
virtual void updateHitted(BCB* bcb) {
auto iter = bcb2iter[bcb];
if (bcb->count < 2) {
s_lru_list.erase(iter);
} else {
l_lru_list.erase(iter);
}
bcb->count += 1;
l_lru_list.push_back(bcb);
bcb2iter[bcb] = --l_lru_list.end();
}
virtual void updateUnhitted(BCB* bcb) {
bcb->count += 1;
s_lru_list.push_back(bcb);
bcb2iter[bcb] = --s_lru_list.end();
}
virtual int selectVictim() {
if (s_lru_list.size() > 0) {
BCB* bcb = s_lru_list.front();
s_lru_list.pop_front();
return bcb->frame_id;
} else {
BCB* bcb = l_lru_list.front();
l_lru_list.pop_front();
return bcb->frame_id;
}
}
virtual void printTitle() {
cout << "##############################" << endl;
cout << "#### LRU2 Buffer Manager #####" << endl;
cout << "##############################" << endl;
}
private:
// access time smaller than k.
list<BCB*> s_lru_list;
// access time larger than k.
list<BCB*> l_lru_list;
// Hash map to list iterator for erase
std::unordered_map<BCB*, list<BCB*>::iterator> bcb2iter;
};
class CLOCKBgr: public Bgr {
public:
virtual void updateHitted(BCB* bcb) {
bcb->referenced = true;
assert(bcb->frame_id < BUFFERSIZE);
}
virtual void updateUnhitted(BCB* bcb) {
bcb_list.insert(current, bcb);
assert(bcb->frame_id < BUFFERSIZE);
// Update current
if (numFreeFrames() > 0) {
current =bcb_list.begin();
} else {
// replaced
current = bcb_list.erase(current);
if (current == bcb_list.end()) {
current = bcb_list.begin();
}
}
}
virtual int selectVictim() {
BCB* bcb = *current;
assert(bcb->frame_id < BUFFERSIZE);
while (bcb->referenced) {
bcb->referenced = false;
current ++;
if (current == bcb_list.end()) {
current = bcb_list.begin();
}
bcb = *current;
}
return bcb->frame_id;
}
virtual void printTitle() {
cout << "##############################" << endl;
cout << "#### CLOCK Buffer Manager ####" << endl;
cout << "##############################" << endl;
}
private:
list<BCB*> bcb_list;
list<BCB*>::iterator current;
};
class DBSAccesser {
private:
Bgr* buffer_mgr = NULL;
public:
DBSAccesser(string filename, string mode="LRU") {
if (mode == "LRU") {
buffer_mgr = new LRUBgr();
} else if (mode == "LRU2") {
buffer_mgr = new LRU2Bgr();
} else if (mode == "CLOCK") {
buffer_mgr = new CLOCKBgr();
}
buffer_mgr->open(filename);
};
virtual ~DBSAccesser() {
delete buffer_mgr;
}
bFrame* read(int page_id) {
return buffer_mgr->readPage(page_id);
}
int write(int page_id, char* data) {
buffer_mgr->writePage(page_id, data);
return 1;
}
void printStat() {
buffer_mgr->printStat();
}
};