-
Notifications
You must be signed in to change notification settings - Fork 0
/
buffer_mgr.c
302 lines (246 loc) · 8.89 KB
/
buffer_mgr.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "buffer_mgr.h"
#include "storage_mgr.h"
#include "dt.h"
// we need to declare a struct
struct Bag {
char *memoryFrame;
int pageOrder;
bool isDirty;
int fixCount;
int lru_index;
};
// we need to define two counter, readIO and writeIO
int readIO = 0;
int writeIO = 0;
// we need to define some helper variable
int fifo_remove_index = 0;
int pin_num = 0;
int lru_slot[5];
// initialize the buffer pool
RC initBufferPool(BM_BufferPool *const bm, const char *const pageFileName,
const int numPages, ReplacementStrategy strategy,
void *stratData) {
bm->pageFile = (char *)pageFileName; // char pointer
bm->numPages = numPages; // int
bm->strategy = strategy; // ReplacementStrategy, which is enum
struct Bag *arr_bag = malloc (sizeof(struct Bag) * numPages);
int i;
for (i=0; i<numPages; i++) {
arr_bag[i].memoryFrame = malloc (PAGE_SIZE);
arr_bag[i].pageOrder = NO_PAGE;
arr_bag[i].isDirty = FALSE;
arr_bag[i].fixCount = 0;
arr_bag[i].lru_index = 0;
}
bm->mgmtData = (struct Bag *) arr_bag;
return 0;
}
// statistic interface
PageNumber *getFrameContents (BM_BufferPool *const bm) {
struct Bag *tmpArray = (struct Bag *) bm->mgmtData;
int *cacheArray = malloc (sizeof(int) * bm->numPages);
int i;
for (i=0; i<bm->numPages; i++) {
cacheArray[i] = tmpArray[i].pageOrder;
}
return cacheArray;
}
bool *getDirtyFlags (BM_BufferPool *const bm) {
struct Bag *tmpArray = (struct Bag *) bm->mgmtData;
bool *dirtyArray = malloc (sizeof(bool) * bm->numPages);
int i;
for (i=0; i<bm->numPages; i++) {
dirtyArray[i] = tmpArray[i].isDirty;
}
return dirtyArray;
}
int *getFixCounts (BM_BufferPool *const bm) {
struct Bag *tmpArray = (struct Bag *) bm->mgmtData;
int *fixCountsArray = malloc (sizeof(int) * bm->numPages);
int i;
for (i=0; i<bm->numPages; i++) {
fixCountsArray[i] = tmpArray[i].fixCount;
}
return fixCountsArray;
}
int getNumReadIO (BM_BufferPool *const bm) {
return readIO;
}
int getNumWriteIO (BM_BufferPool *const bm) {
return writeIO;
}
// key function
int inCache(BM_BufferPool *const bm, const PageNumber pageNum) {
int i;
int *tmp = getFrameContents(bm);
for (i=0; i<bm->numPages; i++) {
if (tmp[i]==pageNum) {
return i;
}
}
free(tmp);
return -1;
}
int isFull(BM_BufferPool *const bm) {
int i;
int *tmp = getFrameContents(bm);
for (i=0; i<bm->numPages; i++) {
if (tmp[i]==NO_PAGE) {
return i;
}
}
free(tmp);
return -1;
}
void FIFO_strategy(BM_BufferPool *const bm, BM_PageHandle *const page, char *const tmp_reading) {
struct Bag *tmpArray = (struct Bag *)bm->mgmtData;
if (tmpArray[fifo_remove_index].isDirty == TRUE) {
// write to disk
SM_FileHandle fh;
openPageFile(bm->pageFile,&fh);
//printf("write to:%d\n",tmpArray[fifo_remove_index].pageOrder);
//printf("write what:%s\n",tmpArray[fifo_remove_index].memoryFrame);
writeBlock(tmpArray[fifo_remove_index].pageOrder, &fh, tmpArray[fifo_remove_index].memoryFrame);
writeIO++;
}
// accomodate new element
tmpArray[fifo_remove_index].memoryFrame = tmp_reading;
page->data = tmpArray[fifo_remove_index].memoryFrame;
tmpArray[fifo_remove_index].pageOrder= page->pageNum;
tmpArray[fifo_remove_index].isDirty = FALSE;
tmpArray[fifo_remove_index].fixCount++;
// update fifo_remove_index
fifo_remove_index = (fifo_remove_index + 1)%bm->numPages;
}
void LRU_strategy(BM_BufferPool *const bm, BM_PageHandle *const page, char *const tmp_reading) {
// determine the least recent use page frame
int lru_remove_index = lru_slot[pin_num%5];
// actual part
struct Bag *tmpArray = (struct Bag *) bm->mgmtData;
if (tmpArray[lru_remove_index].isDirty == TRUE) {
// write to disk
SM_FileHandle fh;
openPageFile(bm->pageFile,&fh);
//printf("write to:%d\n",tmpArray[lru_remove_index].pageOrder);
//printf("write what:%s\n",tmpArray[lru_remove_index].memoryFrame);
writeBlock(tmpArray[lru_remove_index].pageOrder, &fh, tmpArray[lru_remove_index].memoryFrame);
writeIO++;
}
// accomodate new element
tmpArray[lru_remove_index].memoryFrame = tmp_reading;
page->data = tmpArray[lru_remove_index].memoryFrame;
tmpArray[lru_remove_index].pageOrder= page->pageNum;
tmpArray[lru_remove_index].isDirty = FALSE;
tmpArray[lru_remove_index].fixCount++;
}
RC pinPage (BM_BufferPool *const bm, BM_PageHandle *const page,
const PageNumber pageNum) {
page->pageNum = pageNum; // int
// check if requested page is in cache
struct Bag *tmpArray = (struct Bag *) bm->mgmtData;
int result = inCache(bm,pageNum);
if (result==-1) { // requested page is not in cache
SM_FileHandle fh;
openPageFile(bm->pageFile, &fh);
char *tmp_reading = malloc (PAGE_SIZE);
//printf("total page: %d",fh.totalNumPages);
ensureCapacity(pageNum, &fh);
//printf("total page: %d\n",fh.totalNumPages);
//printf("which page to read: %d\n",pageNum);
readBlock(pageNum,&fh,tmp_reading);
readIO++;
//printf("read what:%s\n",tmp_reading);
int full = isFull(bm);
if (full!=-1) { // pool is not full
tmpArray[full].memoryFrame = tmp_reading;
page->data = tmpArray[full].memoryFrame;
tmpArray[full].pageOrder = pageNum;
tmpArray[full].fixCount++;
lru_slot[pin_num%5] = full;
}
else { // pool is full, need employ replacement strategy
if (bm->strategy == RS_FIFO) {
if (tmpArray[fifo_remove_index].fixCount==0) {
FIFO_strategy(bm,page,tmp_reading);
}
else {
fifo_remove_index = (fifo_remove_index + 1)%bm->numPages;
FIFO_strategy(bm,page,tmp_reading);
}
}
else if (bm->strategy == RS_LRU) {LRU_strategy(bm,page,tmp_reading);}
}
}
else { // requested page is in cache
page->data = tmpArray[result].memoryFrame;
tmpArray[result].fixCount++;
tmpArray[result].pageOrder = pageNum;
lru_slot[pin_num%5] = result;
}
pin_num++; // global pin_num++
return RC_OK;
}
RC markDirty (BM_BufferPool *const bm, BM_PageHandle *const page) {
struct Bag *tmpArray = (struct Bag *)bm->mgmtData;
int i;
for (i=0; i<bm->numPages; i++) {
if (tmpArray[i].pageOrder == page->pageNum) {
tmpArray[i].isDirty = TRUE;
return RC_OK;
}
}
return RC_OK;
}
RC unpinPage (BM_BufferPool *const bm, BM_PageHandle *const page) {
struct Bag *tmpArray = (struct Bag *)bm->mgmtData;
int i;
for (i=0; i<bm->numPages; i++) {
if (tmpArray[i].pageOrder == page->pageNum) {
tmpArray[i].fixCount--;
return RC_OK;
}
}
return RC_OK;
}
RC forcePage (BM_BufferPool *const bm, BM_PageHandle *const page) { // write the current content of page back to the page file on disk
struct Bag *tmpArray = (struct Bag *)bm->mgmtData;
int i;
for (i=0; i<bm->numPages; i++) {
if (tmpArray[i].pageOrder == page->pageNum) {
SM_FileHandle fh;
openPageFile(bm->pageFile, &fh);
writeBlock(tmpArray[i].pageOrder,&fh,tmpArray[i].memoryFrame);
tmpArray[i].isDirty = FALSE;
writeIO++;
}
}
return RC_OK;
}
RC shutdownBufferPool(BM_BufferPool *const bm) {
forceFlushPool(bm);
fifo_remove_index = 0;
readIO = 0;
writeIO = 0;
free(bm->mgmtData);
return RC_OK;
}
RC forceFlushPool(BM_BufferPool *const bm) {
struct Bag *tmpArray = (struct Bag *)bm->mgmtData;
int i;
for (i=0; i<bm->numPages; i++) {
if (tmpArray[i].fixCount==0 && tmpArray[i].isDirty == TRUE) {
SM_FileHandle fh;
openPageFile(bm->pageFile,&fh);
//printf("write to:%d\n",tmpArray[i].pageOrder);
//printf("write what: %s\n",tmpArray[i].memoryFrame);
writeBlock(tmpArray[i].pageOrder, &fh, tmpArray[i].memoryFrame);
tmpArray[i].isDirty = 0;
writeIO++;
}
}
return RC_OK;
}