-
Notifications
You must be signed in to change notification settings - Fork 0
/
buffer_mgr.c
421 lines (367 loc) · 12.1 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
//including required header files
#include <stdio.h>
#include <stdlib.h>
#include "buffer_mgr.h"
#include "storage_mgr.h"
typedef struct Structure_Details
{
char *data;
PageNumber pagenum;
int dirtyPage;
int clientcount;
int freq_used;
int nr;
}Structure_Details;
//declaring and initializing values, pointers
//int buffpg_size = 0;
//int next = 0;
//int writeIO = 0;
//int strike = 0;
SM_FileHandle fh;
//Structure_Details *sd;
//Structure_Details *pin;
//initializing bufferpool - creating a buffer pool for an existing page file
RC initBufferPool(BM_BufferPool *const bm, const char *const pageFileName,const int numPages, ReplacementStrategy strategy,void *stratData)
{
Structure_Details *sd = calloc(numPages,sizeof(Structure_Details)); //allocating memory
bm -> bufferpage=numPages;
int i=0;
int np=-1;
int initialize=0;
bm->pageFile = (char *)pageFileName;
bm->numPages = numPages;
bm->strategy = strategy;
bm->mgmtData = sd;
do{
sd[i].data = NULL;
sd[i].pagenum = np;
sd[i].dirtyPage = initialize;
sd[i].clientcount = initialize;
sd[i].freq_used = initialize;
sd[i].nr = initialize;
i=i+1;
}while(i<bm -> bufferpage);
bm->writeio = 0;
return RC_OK;
}
RC shutdownBufferPool(BM_BufferPool *const bm)
{
//sd = (Structure_Details *)bm->mgmtData;
forceFlushPool(bm);
int i;
for(i=0;i< bm->bufferpage;i++)
{
if(((Structure_Details *)bm->mgmtData)[i].clientcount != 0)
return RC_PINNED_PAGES_STILL_IN_BUFFER;
}
//free(sd); // destorys the buffer pool
bm->mgmtData = NULL;
return RC_OK;
}
RC writeDirtyPage(BM_BufferPool *const bm, BM_PageHandle *const page)
{
//BM_BufferPool *bm;
//BM_PageHandle *page;
int i;
for(i=0;i<bm -> bufferpage;i++) // checks each page and writes the current content of the page back to the page file on disk.
{
if(((Structure_Details *)bm->mgmtData)[i].pagenum == page->pageNum)
{
//openPageFile (bm->pageFile, &fh);
writeBlock (((Structure_Details *)bm->mgmtData)[i].pagenum, &fh, ((Structure_Details *)bm->mgmtData)[i].data);
//once written dirtypage is cleared
bm->writeio++;
((Structure_Details *)bm->mgmtData)[i].dirtyPage = 0;
}
}
}
//writing data and flushing pool
RC forceFlushPool(BM_BufferPool *const bm)
{
//sd = (Structure_Details *)bm->mgmtData;
int i;
for(i=0;i<bm -> bufferpage;i++)
{
if((((Structure_Details *)bm->mgmtData)[i].dirtyPage == 1) && (((Structure_Details *)bm->mgmtData)[i].clientcount == 0)) // //checks all dirty pages (with fix count 0)
{
//openPageFile (bm->pageFile, &fh);
writeBlock (((Structure_Details *)bm->mgmtData)[i].pagenum, &fh, ((Structure_Details *)bm->mgmtData)[i].data); // writing to disk from the buffer pool.
bm->writeio++;
((Structure_Details *)bm->mgmtData)[i].dirtyPage = 0;
}
}
return RC_OK;
}
//make page Dirty
RC markDirty (BM_BufferPool *const bm, BM_PageHandle *const page)
{
//sd = (Structure_Details *)bm->mgmtData;
int i;
for(i=0;i<bm -> bufferpage;i++)
{
if(((Structure_Details *)bm->mgmtData)[i].pagenum == page->pageNum)
{
((Structure_Details *)bm->mgmtData)[i].dirtyPage = 1;
return RC_OK; //if success it returns OK
}
}
return RC_ERROR;
}
//unPinPage
RC unpinPage (BM_BufferPool *const bm, BM_PageHandle *const page)
{
//sd = (Structure_Details *)bm->mgmtData;
int i;
for(i=0;i<bm -> bufferpage;i++) // checks each page and removes the clientcount
{
if(((Structure_Details *)bm->mgmtData)[i].pagenum == page->pageNum)
{
((Structure_Details *)bm->mgmtData)[i].clientcount--;
break;
}
}
return RC_OK;
}
//forePage
RC forcePage (BM_BufferPool *const bm, BM_PageHandle *const page)
{
writeDirtyPage(bm,page);
return RC_OK;
}
//FIFO
RC FIFO(BM_BufferPool *bm, Structure_Details *node)
{
int i;
int prev = bm->next%bm -> bufferpage;
//sd = (Structure_Details *) bm->mgmtData;
for(i=0;i<bm -> bufferpage;i++) // checks if the page size is greater than 0
{
if(((Structure_Details *)bm->mgmtData)[prev].clientcount == 0)
{
if(((Structure_Details *)bm->mgmtData)[prev].dirtyPage == 1) // checks if dirtypage is present and writes it to the disk
{
writeBlock (((Structure_Details *)bm->mgmtData)[prev].pagenum, &fh, ((Structure_Details *)bm->mgmtData)[prev].data);
bm->writeio++;
}
((Structure_Details *)bm->mgmtData)[prev].data = node->data;
((Structure_Details *)bm->mgmtData)[prev].pagenum = node->pagenum;
((Structure_Details *)bm->mgmtData)[prev].dirtyPage = node->dirtyPage;
((Structure_Details *)bm->mgmtData)[prev].clientcount = node->clientcount;
break;
}
else{
prev++;
if(prev%bm -> bufferpage == 0)
prev=0;
}
}
}
RC LRU(BM_BufferPool *const bm, Structure_Details *node)
{
//sd=(Structure_Details *) bm->mgmtData;
int i;
int prev;
int minimum;
for(i=0;i<bm -> bufferpage;i++)
{
if(((Structure_Details *)bm->mgmtData)[i].clientcount == 0)
{
prev= i;
minimum = ((Structure_Details *)bm->mgmtData)[i].freq_used;
break;
}
}
//i=prev+1;
for(i=prev + 1;i<bm -> bufferpage;i++)
{
if(((Structure_Details *)bm->mgmtData)[i].freq_used < minimum)
{
prev = i;
minimum = ((Structure_Details *)bm->mgmtData)[i].freq_used;
}
}
if(((Structure_Details *)bm->mgmtData)[prev].dirtyPage == 1) // checks if dirty page present and writes it to disk
{
writeBlock (((Structure_Details *)bm->mgmtData)[prev].pagenum, &fh, ((Structure_Details *)bm->mgmtData)[prev].data);
bm->writeio++;
}
((Structure_Details *)bm->mgmtData)[prev].data = node->data;
((Structure_Details *)bm->mgmtData)[prev].pagenum = node->pagenum;
((Structure_Details *)bm->mgmtData)[prev].dirtyPage = node->dirtyPage;
((Structure_Details *)bm->mgmtData)[prev].clientcount = node->clientcount;
((Structure_Details *)bm->mgmtData)[prev].freq_used = node->freq_used;
}
//pinPage
Structure_Details * bufferCheck(BM_BufferPool *const bm, BM_PageHandle *const page, const PageNumber pageNum){
Structure_Details *pin = (Structure_Details *)malloc(sizeof(Structure_Details));
openPageFile (bm->pageFile, &fh);
pin->data = (SM_PageHandle) malloc(PAGE_SIZE);
readBlock(pageNum, &fh, pin->data);
pin->pagenum = pageNum;
pin->dirtyPage = 0;
pin->clientcount = 1;
//pin->nr = 0;
bm->next++;
bm->strike++;
pin->freq_used = bm->strike;
page->pageNum = pageNum;
page->data = pin->data;
return pin;
}
RC pinPage (BM_BufferPool *const bm, BM_PageHandle *const page, const PageNumber pageNum)
{
if(bm->strategy==RS_FIFO){
//sd = (Structure_Details *)bm->mgmtData;
if(((Structure_Details *)bm->mgmtData)[0].pagenum == -1) //if there is no page in memory
{
//openPageFile (bm->pageFile, &fh);
((Structure_Details *)bm->mgmtData)[0].data = (SM_PageHandle) malloc(PAGE_SIZE);
//ensureCapacity(pageNum,&fh);
readBlock(pageNum, &fh, ((Structure_Details *)bm->mgmtData)[0].data);
((Structure_Details *)bm->mgmtData)[0].pagenum = pageNum;
((Structure_Details *)bm->mgmtData)[0].clientcount++;
bm->next = 0;
//sd->strike = 0;
//sd[0].freq_used = sd->strike;
//sd[0].nr = 0;
page->pageNum = pageNum;
page->data = ((Structure_Details *)bm->mgmtData)[0].data;
return RC_OK;
}
int i;
int buffer_check = 0;
for(i=0;i<bm -> bufferpage;i++)
{
if(((Structure_Details *)bm->mgmtData)[i].pagenum != -1)
{
// printf("LOOP 1 \n");
if(((Structure_Details *)bm->mgmtData)[i].pagenum == pageNum)
{
((Structure_Details *)bm->mgmtData)[i].clientcount++;
buffer_check = 1;
bm->strike++;
((Structure_Details *)bm->mgmtData)[i].freq_used = bm->strike;
page->pageNum = pageNum;
page->data = ((Structure_Details *)bm->mgmtData)[i].data;
break;
}
}
}
if(buffer_check == 0)
{
Structure_Details *a =bufferCheck(bm,page,pageNum);
FIFO(bm,a);
}
return RC_OK;
}
else if(bm->strategy==RS_LRU){
//sd = (Structure_Details *)bm->mgmtData;
if(((Structure_Details *)bm->mgmtData)[0].pagenum == -1)
{
//openPageFile (bm->pageFile, &fh);
((Structure_Details *)bm->mgmtData)[0].data = (SM_PageHandle) malloc(PAGE_SIZE);
//ensureCapacity(pageNum,&fh);
readBlock(pageNum, &fh, ((Structure_Details *)bm->mgmtData)[0].data);
((Structure_Details *)bm->mgmtData)[0].pagenum = pageNum;
((Structure_Details *)bm->mgmtData)[0].clientcount++;
bm->next = 0;
//sd->strike = 0;
//sd[0].freq_used = sd->strike;
//sd[0].nr = 0;
page->pageNum = pageNum;
page->data = ((Structure_Details *)bm->mgmtData)[0].data;
return RC_OK;
}
int i;
int buffer_check = 0;
for(i=0;i<bm -> bufferpage;i++)
{
if(((Structure_Details *)bm->mgmtData)[i].pagenum != -1)
{
if(((Structure_Details *)bm->mgmtData)[i].pagenum == pageNum)
{
((Structure_Details *)bm->mgmtData)[i].clientcount++;
buffer_check = 1;
bm->strike++;
((Structure_Details *)bm->mgmtData)[i].freq_used = bm->strike;
page->pageNum = pageNum;
page->data = ((Structure_Details *)bm->mgmtData)[i].data;
break;
}
}
else
{
openPageFile (bm->pageFile, &fh);
((Structure_Details *)bm->mgmtData)[i].data = (SM_PageHandle) malloc(PAGE_SIZE);
readBlock(pageNum, &fh, ((Structure_Details *)bm->mgmtData)[i].data);
((Structure_Details *)bm->mgmtData)[i].pagenum = pageNum;
((Structure_Details *)bm->mgmtData)[i].clientcount = 1;
((Structure_Details *)bm->mgmtData)[i].nr = 0;
bm->next++;
bm->strike++;
((Structure_Details *)bm->mgmtData)[i].freq_used = bm->strike;
page->pageNum = pageNum;
page->data = ((Structure_Details *)bm->mgmtData)[i].data;
buffer_check = 1;
break;
}
}
if(buffer_check == 0)
{
Structure_Details *a =bufferCheck(bm,page,pageNum);
LRU(bm,a);
}
return RC_OK;
}
}
// get frame contents
PageNumber *getFrameContents (BM_BufferPool *const bm)
{
PageNumber *val = calloc(bm -> bufferpage,sizeof(PageNumber));
//sd= (Structure_Details *)bm->mgmtData;
int i;
for(i=0;i<bm -> bufferpage;i++) //function returns an array of PageNumbers (of size numPages)
{
val[i] = ((Structure_Details *)bm->mgmtData)[i].pagenum;
}
return val;
}
//get dirty flags
bool *getDirtyFlags (BM_BufferPool *const bm)
{
bool *val = calloc(bm -> bufferpage,sizeof(bool));
//sd= (Structure_Details *)bm->mgmtData;
int i;
for(i=0;i<bm -> bufferpage;i++) // returns an array of bools (of size numPages)
{
if(((Structure_Details *)bm->mgmtData)[i].dirtyPage == 1)
{
val[i]= 1;
}
else
{
val[i]=0;
}
}
return val;
}
//get fix counts
int *getFixCounts (BM_BufferPool *const bm)
{
int *val = calloc(bm -> bufferpage,sizeof(int));
//sd= (Structure_Details *)bm->mgmtData;
int i;
for(i=0;i<bm -> bufferpage;i++) //returns an array of ints (of size numPages)
{
val[i] = ((Structure_Details *)bm->mgmtData)[i].clientcount;
}
return val;
}
int getNumReadIO (BM_BufferPool *const bm)
{
return (bm->next+1);
}
int getNumWriteIO (BM_BufferPool *const bm)
{
return bm->writeio;
}