-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuffer_mgr.c
533 lines (450 loc) · 12.2 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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
//Including all the neccessary header files
#include <stdio.h>
#include <stdlib.h>
#include "buffer_mgr.h"
#include "storage_mgr.h"
//Struct for every frame page
typedef struct FrameDetail
{
SM_PageHandle data;
int pageNumber;
int isDirty;
int C_count;
int f_count;
int No_Replace;
}FrameDetail;
int count = 0;
SM_FileHandle fh;
FrameDetail *fPtr;
void getPageFromFile(BM_BufferPool *const bm, int i, BM_PageHandle *const page, const PageNumber pageNum);
void * allocateMemory(int size,int type);
/*
This function allocates memory based on the type parameter
*/
void * allocateMemory(size, type){
void *ptr=NULL;
switch(type){
case 1: ptr= (char *)malloc(sizeof(FrameDetail)*size);
break;
case 2 : ptr= (char *)malloc(sizeof(int)*size);
break;
case 3: ptr= (char *)malloc(sizeof(bool)*size);
break;
case 4: ptr= (int *)malloc(sizeof(int)*size);
break;
default:
break;
}
return ptr;
}
/*********************************************
this method initialize the bufferpool and framedetail struct
********************************************/
RC initBufferPool(BM_BufferPool *const bm, const char *const pageFileName,const int numPages, ReplacementStrategy strategy,void *stratData)
{
FrameDetail *frame = NULL;
if(bm == NULL || numPages <=0)
return RC_INVALID_BM;
if(pageFileName == NULL)
return RC_FILE_NOT_FOUND;
if(strategy < 0)
return RC_UNKNOWN_STRATEGY;
frame = allocateMemory(numPages, 1);
if(frame != NULL){
bm->pageFile = (char *)pageFileName;
bm->numPages = numPages;
bm->strategy = strategy;
bm->mgmtData = frame;
int i=0;
for(i=0; i<numPages; i++ ){
frame[i].data = NULL;
frame[i].pageNumber = -1;
frame[i].C_count = frame[i].f_count = frame[i].isDirty = 0;
}
bm->readcount = 0;
bm->writecount = 0;
return RC_OK;
}else{
return RC_MEMORY_ERROR;
}
}
/*********************************************
this method shutdown the buffer pool
that is passed as parameter
*********************************************/
RC shutdownBufferPool(BM_BufferPool *const bm)
{
int i=0;
FrameDetail *fd = bm->mgmtData;
if(bm == NULL)
return RC_NULL_ERROR;
if(forceFlushPool(bm) == RC_OK){
while(i< bm->numPages)
{
if(fd[i].C_count != 0)
return RC_PINNEN_PAGES_IN_BUFFER;
i++;
}
fd = NULL;
return RC_OK;
}else{
return RC_WRITE_ERROR;
}
}
/*****************************************
this method write all dirty page to the disk
*******************************************/
RC forceFlushPool(BM_BufferPool *const bm)
{
if(bm == NULL)
return RC_NULL_ERROR;
int i=0;
FrameDetail *fd = bm->mgmtData;
while(i<bm -> numPages)
{
if(fd[i].isDirty == 1 && fd[i].C_count == 0)
{
writeBlock (fd[i].pageNumber, &fh, fd[i].data);
bm->writecount++;
fd[i].isDirty = 0;
}
i++;
}
return RC_OK;
}
/************************************************************
this method mark the page dirty and return the pageHandle
*************************************************************/
RC markDirty (BM_BufferPool *const bm, BM_PageHandle *const page)
{
if(bm == NULL || page == NULL)
return RC_NULL_ERROR;
int i=0;
int numPages = bm->numPages;
FrameDetail *fd = bm->mgmtData;
while(i<bm -> numPages)
{
if(fd[i].pageNumber == page->pageNum)
{
fd[i].isDirty = 1;
return RC_OK;
}
i++;
}
return RC_OK;
}
/************************************************
this method unpin the page from bufferpool and decrease the C_count
************************************************/
RC unpinPage (BM_BufferPool *const bm, BM_PageHandle *const page)
{
if(bm == NULL || page == NULL)
return RC_NULL_ERROR;
int i=0;
int numPages = bm->numPages;
FrameDetail *fd = bm->mgmtData;
while(i<bm -> numPages)
{
if(fd[i].pageNumber == page->pageNum)
{
fd[i].C_count = fd[i].C_count-1;
break;
}
i++;
}
return RC_OK;
}
/***************************************
this method write page back to disk from bufferpool
****************************************/
RC forcePage (BM_BufferPool *const bm, BM_PageHandle *const page)
{
if(bm == NULL || page == NULL)
{
return RC_NULL_ERROR;
}
if(writeDirtyPage(bm,page)!=RC_OK)
{
return RC_WRITE_ERROR;
}
else{
return RC_OK;
}
}
/*********************************************************
this method write dirty page back to disk from bufferpool
**********************************************************/
RC writeDirtyPage(BM_BufferPool *const bm, BM_PageHandle *const page)
{
if(bm !=NULL && page != NULL){
int i;
for(i=0;i<bm -> numPages;i++)
{
if(((FrameDetail *)bm->mgmtData)[i].pageNumber == page->pageNum)
{
ensureCapacity(((FrameDetail *)bm->mgmtData)[i].pageNumber, &fh);
writeBlock (((FrameDetail *)bm->mgmtData)[i].pageNumber, &fh, ((FrameDetail *)bm->mgmtData)[i].data);
bm->writecount++;
((FrameDetail *)bm->mgmtData)[i].isDirty = 0;
return RC_OK;
}
}
}
else{
return RC_NULL_ERROR;
}
}
/***************************************
First In First out Stratergy
****************************************/
void FIFO(BM_BufferPool *const bm, FrameDetail *fd)
{
int i;
int pages = bm -> numPages;
int previous = bm->readcount%pages;
fPtr = (FrameDetail *) bm->mgmtData;
for(i=0; i<pages; i++){
if(fPtr[previous].C_count == 0)
{
if(fPtr[previous].isDirty == 1){
openPageFile (bm->pageFile, &fh);
writeBlock (fPtr[previous].pageNumber, &fh, fPtr[previous].data);
bm -> writecount++;
}
fPtr[previous].data = fd->data;
fPtr[previous].pageNumber = fd->pageNumber;
fPtr[previous].isDirty = fd->isDirty;
fPtr[previous].C_count = fd->C_count;
break;
}
else
{
previous++;
if(previous%pages == 0)
previous=0;
}
}
}
/***************************************
Least Recently Used Stratergy
****************************************/
void LRU(BM_BufferPool *const bm, FrameDetail *fd)
{
fPtr=(FrameDetail *) bm->mgmtData;
int i=0;
int pages = bm->numPages;
int previous;
int least;
for(i=0; i<pages; i++)
{
if(fPtr[i].C_count == 0)
{
previous= i;
least = fPtr[i].f_count;
break;
}
}
for(i=previous+1; i<pages; i++)
{
if(fPtr[i].f_count < least)
{
previous = i;
least = fPtr[i].f_count;
}
}
if(fPtr[previous].isDirty == 1){
openPageFile (bm->pageFile, &fh);
writeBlock (fPtr[previous].pageNumber, &fh, fPtr[previous].data);
bm -> writecount++;
}
fPtr[previous].data = fd->data;
fPtr[previous].pageNumber = fd->pageNumber;
fPtr[previous].isDirty = fd->isDirty;
fPtr[previous].C_count = fd->C_count;
fPtr[previous].f_count = fd->f_count;
}
/************************************************
this method pins the page with different strategy(FIFO/LRU)
************************************************/
RC pinPage (BM_BufferPool *const bm, BM_PageHandle *const page, const PageNumber pageNum)
{
if(bm == NULL || page == NULL)
return RC_PIN_PAGE_PARAMETER_ERROR;
if(((FrameDetail *)bm->mgmtData)[0].pageNumber == -1)
{
getPageFromFile(bm, 0, page, pageNum);
bm->readcount = 0;
count = 0;
((FrameDetail *)bm->mgmtData)[0].f_count = count;
return RC_OK;
}
else
{
int i=0;
int buffer_check = 0;
while(i<bm -> numPages)
{
if(((FrameDetail *)bm->mgmtData)[i].pageNumber != -1)
{
if(((FrameDetail *)bm->mgmtData)[i].pageNumber == pageNum)
{
((FrameDetail *)bm->mgmtData)[i].C_count++;
buffer_check = 1;
count++;
if(bm->strategy == RS_LRU)
((FrameDetail *)bm->mgmtData)[i].f_count = count;
page->pageNum = pageNum;
page->data = ((FrameDetail *)bm->mgmtData)[i].data;
break;
}
}
else
{
getPageFromFile(bm, i, page, pageNum);
bm->readcount++;
count++;
if(bm->strategy == RS_LRU)
((FrameDetail *)bm->mgmtData)[i].f_count = count;
buffer_check = 1;
break;
}
i++;
}
if(buffer_check == 0) callPageReplacement(bm, page, pageNum);
return RC_OK;
}
}
RC callPageReplacement(BM_BufferPool *const bm, BM_PageHandle *const page, const PageNumber pageNum){
FrameDetail *frame;
frame = (FrameDetail *)malloc(sizeof(FrameDetail));
frame->data = (SM_PageHandle) malloc(PAGE_SIZE);
openPageFile (bm->pageFile, &fh);
readBlock(pageNum, &fh, frame->data);
frame->pageNumber = pageNum;
frame->isDirty = 0;
frame->C_count = 1;
frame->No_Replace = 0;
page->pageNum = pageNum;
page->data = frame->data;
bm->readcount++;
count++;
int replacementAlgo = bm->strategy;
if(replacementAlgo == RS_FIFO){
FIFO(bm,frame);
}
else if (replacementAlgo == RS_LRU){
frame->f_count = count;
LRU(bm,frame);
}else{
return RC_NULL_ERROR;
}
}
void getPageFromFile(BM_BufferPool *const bm, int i, BM_PageHandle *const page, const PageNumber pageNum){
fPtr = (FrameDetail *)bm->mgmtData;
openPageFile (bm->pageFile, &fh);
fPtr[i].data = (SM_PageHandle) malloc(PAGE_SIZE);
readBlock(pageNum, &fh, fPtr[i].data);
fPtr[i].pageNumber = pageNum;
fPtr[i].C_count=1;
fPtr[i].No_Replace = 0;
page->pageNum = pageNum;
page->data = fPtr[i].data;
}
/************************************************
this method returns the frame contents
************************************************/
PageNumber *getFrameContents(BM_BufferPool *const bm)
{
int i;
int pages = bm->numPages;
if(bm == NULL){
return (int *)RC_INVALID_BM;
}
PageNumber *pageNo = allocateMemory(pages, 2);
for(i=0;i<pages;i++)
{
pageNo[i] = ((FrameDetail *)bm->mgmtData)[i].pageNumber;
}
return pageNo;
}
/************************************************
this method returns a boolean array of the dirty page flags
************************************************/
bool *getDirtyFlags (BM_BufferPool *const bm)
{
if(bm == NULL){
return (bool *)RC_INVALID_BM;
}
int pages = bm->numPages;
int i;
bool *dirtyPage;
dirtyPage = allocateMemory(pages, 3);
for(i=0;i<pages;i++)
{
if(((FrameDetail *)bm->mgmtData)[i].isDirty == 1)
{
dirtyPage[i]= 1;
}
else
{
dirtyPage[i]=0;
}
}
return dirtyPage;
}
/************************************************
this method returns fix count of all the page
************************************************/
int *getFixCounts (BM_BufferPool *const bm)
{
if(bm == NULL)
{
return (int *)RC_INVALID_BM;
}
int i=0;
int pages = bm->numPages;
int *fixCount;
fixCount = allocateMemory(pages, 4);
while(i<pages)
{
fixCount[i] = ((FrameDetail *)bm->mgmtData)[i].C_count;
i++;
}
return fixCount;
}
/************************************************
this method return number of time file read
************************************************/
int getNumReadIO (BM_BufferPool *const bm)
{
if(bm == NULL)
{
return RC_INVALID_BM;
}
int readCount = bm->readcount+1;
if(readCount < 0)
{
return RC_READ_COUNT_ERROR;
}
else{
return readCount;
}
}
/************************************************
this method return number of time file write
************************************************/
int getNumWriteIO (BM_BufferPool *const bm)
{
if(bm == NULL)
{
return RC_INVALID_BM;
}
int writeCount = bm->writecount;
if(writeCount < 0)
{
return RC_WRITE_COUNT_ERROR;
}
else{
return writeCount;
}
}