Guest User

Untitled

a guest
Mar 3rd, 2016
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 30.73 KB | None | 0 0
  1. #include "buffer_mgr.h"
  2. #include "storage_mgr.h"
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6.  
  7. /*
  8. Struct that contains the essential information of all the frames in the buffer pool.
  9. The buffer pool itself is an array of elements of this struct.
  10. */
  11. typedef struct FrameInfo
  12. {
  13. // Pointer to the data of the frame
  14. char * frameData;
  15. // Frame Number in the Buffer Pool (from 0 to numPages-1)
  16. int frameNumber;
  17. // Number of the page in the page file
  18. PageNumber pageNumber;
  19. // Counts how many users are reading the frame
  20. int fixCount;
  21. // Boolean that gives information about changes in the file
  22. bool isDirty;
  23. // Previous Frame in the Buffer Pool
  24. struct FrameInfo *previousFrame;
  25. // Next Frame in the Buffer Pool
  26. struct FrameInfo *nextFrame;
  27. } FrameInfo;
  28.  
  29. /*
  30. Struct that contains the buffer pool itself, as well as essential information needed for
  31. management purposes.
  32. */
  33. typedef struct BufferPoolInfo
  34. {
  35. // Pointer to the Buffer Pool
  36. FrameInfo *bufferPool;
  37. // First frame in the Buffer Pool
  38. FrameInfo *firstFrame;
  39. // Last frame in the Buffer Pool
  40. FrameInfo *lastFrame;
  41. // Number of pages read
  42. int readNumber;
  43. // Number of pages written
  44. int writeNumber;
  45. //number of filled frames
  46. int count;
  47. } BufferPoolInfo;
  48.  
  49. /************************************************************
  50. * Replacement Strategies *
  51. ************************************************************/
  52.  
  53. // FIFO
  54. /*******************************************************************
  55. * NAME : RC doFifo(BM_BufferPool *const bm, BM_PageHandle *const page,PageNumber pageNum)
  56. *
  57. * DESCRIPTION : Store block in to memory using FIFO strategy
  58. *
  59. * PARAMETERS:
  60. * BM_BufferPool *const bm Stores specific information about a buffer pool
  61. * BM_PageHandle *const page Page information
  62. * PageNumber pageNum) Page wanted to be store
  63. *
  64. * RETURN :
  65. * Type: RC Returned code:
  66. * Values: RC_OK block stored successfully
  67. * RC_READ_NON_EXISTING_PAGE Cannot find page
  68. *
  69. * AUTHOR :
  70. * Patipat Duangchalomnin <pduangchalomnin@hawk.iit.edu>
  71. *
  72. * HISTORY :
  73. * DATE WHO DETAIL
  74. * ----------- --------------------------------------- ---------------------------------
  75. * 2016-02-27 Patipat Duangchalomnin <pduangchalomnin@hawk.iit.edu> Initialization
  76. * 2016-02-29 Patipat Duangchalomnin <pduangchalomnin@hawk.iit.edu> Correction
  77. * 2016-02-29 Patipat Duangchalomnin <pduangchalomnin@hawk.iit.edu> Added header comment and correction
  78. *
  79. *******************************************************************/
  80. RC doFifo(BM_BufferPool *const bm, BM_PageHandle *const page, PageNumber pageNum)
  81. {
  82. BufferPoolInfo *buffPoolInfo = bm->mgmtData;
  83. FrameInfo *bufferPool = buffPoolInfo->bufferPool;
  84. int frameCounter = 0;
  85.  
  86. SM_FileHandle fileHandle;
  87. char *fileName = bm->pageFile;
  88.  
  89. int numPages = bm->numPages;
  90. bool *dirtyFlags = getDirtyFlags(bm);
  91.  
  92. FrameInfo *frame = buffPoolInfo->firstFrame;
  93. while(frame->fixCount != 0 )
  94. {
  95. printf("ENTER\n");
  96. frameCounter++;
  97. if(frame->nextFrame==NULL)
  98. {
  99. frameCounter = 0;
  100. break;
  101. }
  102. frame = frame->nextFrame;
  103. }
  104.  
  105. //If dirty write it down to disk
  106. for (int i = 0; i < numPages; i++) {
  107. if (*(dirtyFlags + i) == true) {
  108. SM_FileHandle fileHandle;
  109. char *fileName = bm->pageFile;
  110. //forceFlushPool(bm);
  111. // Open file
  112. openPageFile(fileName, &fileHandle);
  113. // ensureCapacity(pageNum + 1, &fileHandle);
  114. // Write page to disk
  115. printf("%d : %s pageNum%d\n",i,bufferPool[i].frameData,bufferPool[i].pageNumber);
  116. RC test = writeBlock(bufferPool[i].pageNumber, &fileHandle, bufferPool[i].frameData);
  117. if(test !=RC_OK)
  118. {
  119. return test;
  120. }
  121. bufferPool[i].isDirty = false;
  122. buffPoolInfo->writeNumber++;
  123. // Close page file
  124. closePageFile(&fileHandle);
  125. // Not need to keep checking
  126. int s;
  127. // scanf("%d",&s);
  128. }
  129. }
  130.  
  131. // //if(frame->isDirty == true)
  132. // //{
  133. // printf("sss %d %d\n" ,frame->pageNumber,frame->isDirty);
  134. // // Open file
  135. // openPageFile(fileName, &fileHandle);
  136. // // Write page to disk
  137. // RC write = writeBlock(frame->pageNumber, &fileHandle, frame->frameData);
  138. // if(write!=RC_OK)
  139. // {
  140. // return RC_FILE_NOT_FOUND;
  141. // }
  142. // frame->isDirty = false;
  143. // // Close page file
  144. // closePageFile(&fileHandle);
  145. // buffPoolInfo->writeNumber++;
  146. //
  147. // //}
  148. //Pop first frame by shrift every frame by one
  149. for (int i = frameCounter; i < bm->numPages - 1 ; i++)
  150. {
  151. strcpy(bufferPool[i].frameData,bufferPool[i + 1].frameData);
  152. //bufferPool[i].frameData = bufferPool[i + 1].frameData;
  153. bufferPool[i].pageNumber = bufferPool[i + 1].pageNumber;
  154. }
  155. frame = &bufferPool[bm->numPages-1];
  156. //Set last frame
  157. // Open file
  158. openPageFile(fileName, &fileHandle);
  159.  
  160. ensureCapacity(pageNum + 1, &fileHandle);
  161. //Read page to memory
  162. RC readToMem = readBlock(pageNum, &fileHandle, frame->frameData);
  163.  
  164. // Close page file
  165. closePageFile(&fileHandle);
  166. if (readToMem != RC_OK)
  167. {
  168. printf("FLAG!!\n");
  169. return readToMem;
  170. }
  171. frame->fixCount +=1;
  172. //buffPoolInfo->lastFrame->fixCount++;
  173. buffPoolInfo->readNumber++;
  174. frame->pageNumber = pageNum;
  175. //buffPoolInfo->lastFrame->pageNumber = pageNum;
  176. strcpy(page->data,frame->frameData);
  177. page->pageNum = pageNum;
  178.  
  179. return RC_OK;
  180.  
  181. }
  182. // LRU
  183. RC LRU(BM_BufferPool *const bm, BM_PageHandle *const page, PageNumber pageNum)
  184. {
  185. return RC_OK;
  186. }
  187. // CLOCK (Extra)
  188.  
  189. // LFU (Extra)
  190.  
  191. // LFU_K (Extra)
  192.  
  193. /************************************************************
  194. * Buffer Manager Interface Pool Handling *
  195. ************************************************************/
  196.  
  197. /*******************************************************************
  198. * NAME : RC initBufferPool(BM_BufferPool *const bm, const char *const pageFileName,
  199. * const int numPages, ReplacementStrategy strategy,
  200. * void *stratData)
  201. *
  202. * DESCRIPTION : Creates a new buffer pool with numPages page frames using the page replacement
  203. * given in the parameters
  204. *
  205. * PARAMETERS:
  206. * BM_BufferPool *const bm Stores specific information about a buffer pool
  207. * const char *const pageFileName Name of the file to read pages from
  208. * const int numPages Size of the buffer pool
  209. * ReplacementStrategy strategy Replacement strategy followed
  210. * void *stratData Extra parameters
  211. *
  212. * RETURN :
  213. * Type: RC Returned code:
  214. * Values: RC_OK buffer pool created successfully
  215. *
  216. * AUTHOR :
  217. * Adrian Tirados <atirados@hawk.iit.edu>
  218. *
  219. * HISTORY :
  220. * DATE WHO DETAIL
  221. * ----------- --------------------------------------- ---------------------------------
  222. * 2016-02-18 Adrian Tirados <atirados@hawk.iit.edu> Initialization
  223. * 2016-02-29 Adrian Tirados <atirados@hawk.iit.edu> Added comments and header comment
  224. *
  225. *******************************************************************/
  226. RC initBufferPool(BM_BufferPool *const bm, const char *const pageFileName,
  227. const int numPages, ReplacementStrategy strategy,
  228. void *stratData) {
  229. // Create the Buffer Pool, which is an array of #numPages FrameInfo structs
  230. FrameInfo *bufferPool = (FrameInfo *) malloc(numPages * sizeof(FrameInfo));
  231.  
  232. // Initialize the Frames inside the Buffer Pool
  233. int i = 0;
  234. while (i < numPages) {
  235. // Allocate the memory for the Frame (same as a page)
  236. bufferPool[i].frameData = (char *) malloc(PAGE_SIZE * sizeof(char));
  237. // Set the frame number
  238. bufferPool[i].frameNumber = i;
  239. // Set the page number to -1, since we are initializing the buffer
  240. bufferPool[i].pageNumber = NO_PAGE;
  241. // Set fixCount to 0 and isDirty to false, since we are initializing the buffer
  242. bufferPool[i].fixCount = 0;
  243. bufferPool[i].isDirty = false;
  244.  
  245. // Next, fill the information for the previous and next frames for each frame
  246. if (i == 0) {
  247. // First frame has no previous frame
  248. bufferPool[i].previousFrame = NULL;
  249. } else {
  250. bufferPool[i].previousFrame = &bufferPool[i - 1];
  251. }
  252. if (i == numPages - 1) {
  253. // Last frame has no next frame
  254. bufferPool[i].nextFrame = NULL;
  255. } else {
  256. bufferPool[i].nextFrame = &bufferPool[i + 1];
  257. }
  258.  
  259. i++;
  260. }
  261.  
  262. // Create the BufferPoolInfo variable
  263. BufferPoolInfo *buffPoolInfo = (BufferPoolInfo *) malloc(sizeof(BufferPoolInfo));
  264.  
  265. // Fill the BufferPoolInfo:
  266.  
  267. // Assign the pointer to the Buffer Pool array
  268. buffPoolInfo->bufferPool = bufferPool;
  269.  
  270. // Assign the pointers to the first and last frames
  271. buffPoolInfo->firstFrame = &bufferPool[0];
  272. buffPoolInfo->lastFrame = &bufferPool[numPages - 1];
  273.  
  274. // Set number of reads and writes to 0
  275. buffPoolInfo->readNumber = 0;
  276. buffPoolInfo->writeNumber = 0;
  277.  
  278. // Fill the BM_BufferPool struct
  279. bm->pageFile = (char *) pageFileName;
  280. bm->numPages = numPages;
  281. bm->strategy = strategy;
  282. bm->mgmtData = buffPoolInfo;
  283.  
  284. return RC_OK;
  285. }
  286.  
  287. /*******************************************************************
  288. * NAME : RC shutdownBufferPool(BM_BufferPool *const bm)
  289. *
  290. * DESCRIPTION : Destroys a buffer pool. This method also frees up all resources
  291. * associated with the buffer pool.
  292. *
  293. * PARAMETERS:
  294. * BM_BufferPool *const bm Stores specific information about a buffer pool
  295. *
  296. * RETURN :
  297. * Type: RC Returned code:
  298. * Values: RC_OK buffer pool destroyed successfully
  299. * RC_RC_PINNED_PAGES error while trying to shutdown, caused by pinned pages
  300. *
  301. * AUTHOR :
  302. * Adrian Tirados <atirados@hawk.iit.edu>
  303. *
  304. * HISTORY :
  305. * DATE WHO DETAIL
  306. * ----------- --------------------------------------- ---------------------------------
  307. * 2016-02-18 Adrian Tirados <atirados@hawk.iit.edu> Initialization
  308. * 2016-02-29 Adrian Tirados <atirados@hawk.iit.edu> Added comments and header comment
  309. *
  310. *******************************************************************/
  311. RC shutdownBufferPool(BM_BufferPool *const bm) {
  312. // Get number of pages to iterate
  313. int numPages = bm->numPages;
  314. printf("here");
  315. // Create the BufferPoolInfo pointer and point it to the mgmt information
  316. BufferPoolInfo *buffPoolInfo = bm->mgmtData;
  317. FrameInfo *bufferPool = buffPoolInfo->bufferPool;
  318.  
  319. // Get the information about pinned pages and dirty pages
  320. int *fixCounts = getFixCounts(bm);
  321. bool *dirtyFlags = getDirtyFlags(bm);
  322.  
  323. // Check if there are any frames accessed by any client(s)
  324. for (int i = 0; i < numPages; i++) {
  325. // If there is any, abort shutdown
  326. if (*(fixCounts + i) != 0) {
  327. // Return custom error because there are pinned files
  328. free(fixCounts);
  329. free(dirtyFlags);
  330. printf("This page is pinned: %d\n", i);
  331. return RC_PINNED_PAGES;
  332. }
  333. }
  334.  
  335. // Check if there are any frames with unsaved changes
  336. for (int i = 0; i < numPages; i++) {
  337. printf("pageinShutDown: %d\n",bufferPool[i].pageNumber);
  338. // If there are dirty frames, save changes by calling forceFlushPool
  339. if (*(dirtyFlags + i) == true) {
  340. SM_FileHandle fileHandle;
  341. char *fileName = bm->pageFile;
  342.  
  343. //forceFlushPool(bm);
  344. // Open file
  345. openPageFile(fileName, &fileHandle);
  346. // Write page to disk
  347. printf("%d : %s\n",i,bufferPool[i].frameData);
  348. writeBlock(bufferPool[i].pageNumber, &fileHandle, bufferPool[i].frameData);
  349. //frame->isDirty = false;
  350. buffPoolInfo->writeNumber++;
  351. // Close page file
  352. closePageFile(&fileHandle);
  353. }
  354. }
  355.  
  356. // Free up resources and return RC code
  357. free(buffPoolInfo);
  358.  
  359. free(fixCounts);
  360. free(dirtyFlags);
  361. return RC_OK;
  362. }
  363.  
  364. /*******************************************************************
  365. * NAME : RC forceFlushPool(BM_BufferPool *const bm)
  366. *
  367. * DESCRIPTION : Writes all dirty pages (with fix count 0) from the buffer pool to disk.
  368. *
  369. * PARAMETERS:
  370. * BM_BufferPool *const bm Stores specific information about a buffer pool
  371. *
  372. * RETURN :
  373. * Type: RC Returned code:
  374. * Values: RC_OK dirty pages written successfully
  375. *
  376. * AUTHOR :
  377. * Adrian Tirados <atirados@hawk.iit.edu>
  378. *
  379. * HISTORY :
  380. * DATE WHO DETAIL
  381. * ----------- --------------------------------------- ---------------------------------
  382. * 2016-02-18 Adrian Tirados <atirados@hawk.iit.edu> Initialization
  383. * 2016-02-19 Adrian Tirados <atirados@hawk.iit.edu> Edited
  384. * 2016-02-29 Adrian Tirados <atirados@hawk.iit.edu> Added comments and header comment
  385. *
  386. *******************************************************************/
  387. RC forceFlushPool(BM_BufferPool *const bm) {
  388. // Get number of pages to iterate
  389. int numPages = bm->numPages;
  390.  
  391. // Get the information about the page from the buffer pool and start a fileHandle
  392. char *fileName = bm->pageFile;
  393. SM_FileHandle fileHandle;
  394. SM_PageHandle pageHandle = (SM_PageHandle) malloc(sizeof(SM_PageHandle));
  395.  
  396. // Create the BufferPoolInfo pointer and point it to the mgmt information
  397. BufferPoolInfo *buffPoolInfo = bm->mgmtData;
  398.  
  399. // Check for dirty flags
  400. bool *dirtyFlags = getDirtyFlags(bm);
  401.  
  402. int *fixCounts = getFixCounts(bm);
  403.  
  404. for (int i = 0; i < numPages; i++) {
  405. // If dirty flag and not pinned
  406. if ((*(dirtyFlags + i) == true) && (*(fixCounts + i) == 0)) {
  407. // Load frame and page number
  408. FrameInfo *frame = &(buffPoolInfo->bufferPool[i]);
  409. PageNumber pageNum = frame->pageNumber;
  410.  
  411. // Copy frame to pageHandle
  412. strcpy(pageHandle, frame->frameData);
  413. // Open file
  414. openPageFile(fileName, &fileHandle);
  415. ensureCapacity(frame->pageNumber+1,&fileHandle);
  416. // Write page to disk
  417. writeBlock(pageNum, &fileHandle, pageHandle);
  418. // Close page file
  419. closePageFile(&fileHandle);
  420. // Set dirty flag as false
  421. frame->isDirty = false;
  422. // Increase number of pages written
  423. buffPoolInfo->writeNumber++;
  424. }
  425. }
  426.  
  427. free(fixCounts);
  428. free(dirtyFlags);
  429. return RC_OK;
  430. }
  431.  
  432. /************************************************************
  433. * Buffer Manager Interface Access Pages *
  434. ************************************************************/
  435.  
  436. /*******************************************************************
  437. * NAME : RC markDirty (BM_BufferPool *const bm, BM_PageHandle *const page)
  438. *
  439. * DESCRIPTION : Mark page as dirty
  440. *
  441. * PARAMETERS:
  442. * BM_BufferPool *const bm Stores specific information about a buffer pool
  443. * BM_PageHandle *const page Page information
  444. *
  445. * RETURN :
  446. * Type: RC Returned code:
  447. * Values: RC_OK block stored successfully
  448. * RC_WRITE_FAILED Write dirty failed
  449. *
  450. * AUTHOR :
  451. * Patipat Duangchalomnin <pduangchalomnin@hawk.iit.edu>
  452. *
  453. * HISTORY :
  454. * DATE WHO DETAIL
  455. * ----------- --------------------------------------- ---------------------------------
  456. * 2016-02-26 Patipat Duangchalomnin <pduangchalomnin@hawk.iit.edu> Initialization
  457. * 2016-02-28 Patipat Duangchalomnin <pduangchalomnin@hawk.iit.edu> Correction
  458. * 2016-02-29 Patipat Duangchalomnin <pduangchalomnin@hawk.iit.edu> Added header comment
  459. *
  460. *******************************************************************/
  461. RC markDirty (BM_BufferPool *const bm, BM_PageHandle *const page) {
  462.  
  463. BufferPoolInfo *buffPoolInfo = bm->mgmtData;
  464. FrameInfo *bufferPool = buffPoolInfo->bufferPool;
  465.  
  466. // Get number of pages on buffer pool
  467. int numPages = bm->numPages;
  468. bool *dirtyFlags = getDirtyFlags(bm);
  469.  
  470. printf("numPages: %d\n", numPages);
  471. printf("pageNum: %d\n", page->pageNum);
  472.  
  473. for (int i = 0; i < numPages; i++) {
  474. printf("i = %d\n",i);
  475. FrameInfo *frame = &bufferPool[i];
  476. printf("framePageNumber = %d || pageNum = %d\n",frame->pageNumber,page->pageNum);
  477. if (frame->pageNumber == page->pageNum) {
  478. printf("data: %s\n",page->data);
  479. //strcpy("%s\n",frame->frameData);
  480. strcpy(frame->frameData,page->data);
  481. //frame->frameData = page->data;
  482. // for(int j=0; j<PAGE_SIZE;j++)
  483. // {
  484. // *(frame->frameData + PAGE_SIZE+j) = *(page->data +j);
  485. // }
  486. frame->isDirty = true;
  487. printf("frame data:%s framepage:%d\n",frame->frameData,frame->pageNumber);
  488.  
  489. free(dirtyFlags);
  490. return RC_OK;
  491. }
  492.  
  493. }
  494.  
  495. return RC_WRITE_FAILED;
  496. }
  497.  
  498. /*******************************************************************
  499. * NAME : RC unpinPage (BM_BufferPool *const bm, BM_PageHandle *const page)
  500. *
  501. * DESCRIPTION : Unpin page in memory
  502. *
  503. * PARAMETERS:
  504. * BM_BufferPool *const bm Stores specific information about a buffer pool
  505. * BM_PageHandle *const page Page information
  506. *
  507. * RETURN :
  508. * Type: RC Returned code:
  509. * Values: RC_OK block stored successfully
  510. * RC_WRITE_FAILED Write dirty failed
  511. *
  512. * AUTHOR :
  513. * Patipat Duangchalomnin <pduangchalomnin@hawk.iit.edu>
  514. *
  515. * HISTORY :
  516. * DATE WHO DETAIL
  517. * ----------- --------------------------------------- ---------------------------------
  518. * 2016-02-26 Patipat Duangchalomnin <pduangchalomnin@hawk.iit.edu> Initialization
  519. * 2016-03-01 Patipat Duangchalomnin <pduangchalomnin@hawk.iit.edu> Added header comment and correction
  520. *
  521. *******************************************************************/
  522. RC unpinPage (BM_BufferPool *const bm, BM_PageHandle *const page) {
  523.  
  524. BufferPoolInfo *buffPoolInfo = bm->mgmtData;
  525. FrameInfo *bufferPool = buffPoolInfo->bufferPool;
  526.  
  527. // Get number of pages on buffer pool
  528. int numPages = bm->numPages;
  529. for (int i = 0; i < numPages; i++)
  530. {
  531. FrameInfo *frame = &bufferPool[i];
  532. if (frame->pageNumber == page->pageNum)
  533. {
  534. // if (frame->isDirty)
  535. // {
  536. // printf("Page %d is dirty: %d\n", i, frame->isDirty);
  537. // SM_FileHandle fileHandle;
  538. // char *fileName = bm->pageFile;
  539. // openPageFile(fileName, &fileHandle);
  540. // // Write page to disk
  541. // writeBlock(frame->pageNumber, &fileHandle, page->data);
  542. // frame->isDirty = false;
  543. // // Close page file
  544. // closePageFile(&fileHandle);
  545. // //strcpy(bufferPool[i].frameData,page->data);
  546. // //forcePage(bm, page);
  547. // printf("Page %d is dirty: %d\n", i, frame->isDirty);
  548. //
  549. // }
  550. frame->fixCount--;
  551. printf("fixCount %d\n",frame->fixCount);
  552. return RC_OK;
  553. }
  554. }
  555. // Page not found
  556. return RC_WRITE_FAILED;
  557. }
  558.  
  559. /*******************************************************************
  560. * NAME : forcePage (BM_BufferPool *const bm, BM_PageHandle *const page)
  561. *
  562. * DESCRIPTION : force page from memory
  563. *
  564. * PARAMETERS:
  565. * BM_BufferPool *const bm Stores specific information about a buffer pool
  566. * BM_PageHandle *const page Page information
  567. *
  568. * RETURN :
  569. * Type: RC Returned code:
  570. * Values: RC_OK block stored successfully
  571. * RC_FILE_NOT_FOUND Cannot find file
  572. *
  573. * AUTHOR :
  574. * Patipat Duangchalomnin <pduangchalomnin@hawk.iit.edu>
  575. *
  576. * HISTORY :
  577. * DATE WHO DETAIL
  578. * ----------- --------------------------------------- ---------------------------------
  579. * 2016-02-26 Patipat Duangchalomnin <pduangchalomnin@hawk.iit.edu> Initialization
  580. * 2016-02-29 Patipat Duangchalomnin <pduangchalomnin@hawk.iit.edu> Correction
  581. * 2016-03-01 Patipat Duangchalomnin <pduangchalomnin@hawk.iit.edu> Added header comment and correction
  582. *
  583. *******************************************************************/
  584. RC forcePage (BM_BufferPool *const bm, BM_PageHandle *const page) {
  585. BufferPoolInfo *buffPoolInfo = bm->mgmtData;
  586. FrameInfo *bufferPool = buffPoolInfo->bufferPool;
  587. SM_FileHandle fileHandle;
  588. char *fileName = bm->pageFile;
  589. for(int i = 0; i<bm->numPages;i++)
  590. {
  591. if(bufferPool[i].pageNumber==page->pageNum)
  592. {
  593. // Open file
  594. openPageFile(fileName, &fileHandle);
  595. // Write page to disk
  596. printf(">> %s %d\n",page->data,page->pageNum);
  597. //strcpy(pageHandle,page->data);
  598. writeBlock(page->pageNum, &fileHandle, bufferPool[i].frameData);
  599. // Close page file
  600. closePageFile(&fileHandle);
  601. bufferPool[i].isDirty = false;
  602. buffPoolInfo->writeNumber++;
  603. return RC_OK;
  604. }
  605. }
  606. //If buffer pool exists
  607. // if (buffPoolInfo != NULL)
  608. // {
  609. // SM_FileHandle fileHandle;
  610. // char *fileName = bm->pageFile;
  611. // SM_PageHandle pageHandle = (SM_PageHandle)malloc(sizeof(SM_PageHandle));
  612. //
  613. //
  614. //
  615. //
  616. // FrameInfo *bufferPool = buffPoolInfo->bufferPool;
  617. // for(int i=0; i<bm->numPages;i++)
  618. // {
  619. // if(bufferPool[i].pageNumber == page->pageNum)
  620. // {
  621. //
  622. // break;
  623. // }
  624. // }
  625. // //Increase write time
  626. //
  627. // return RC_OK;
  628. // }
  629. // else
  630. // {
  631. //
  632. // return RC_FILE_NOT_FOUND;
  633. // }
  634. }
  635.  
  636. //Written 2016/02/27
  637. //Edited 2016/02/29
  638. // Fixed 2016/03/01
  639. RC pinPage (BM_BufferPool *const bm, BM_PageHandle *const page,
  640. const PageNumber pageNum) {
  641.  
  642. if (pageNum < 0) {
  643. return RC_READ_NON_EXISTING_PAGE;
  644. }
  645. SM_FileHandle fileHandle;
  646. char *fileName = bm->pageFile;
  647.  
  648.  
  649. // Allocate memory for the data pointer in the PageHandle
  650. page->data = (char* ) malloc(PAGE_SIZE * sizeof(char));
  651.  
  652. // Retrieve the information about the buffer pool
  653. BufferPoolInfo *buffPoolInfo = bm->mgmtData;
  654. FrameInfo *bufferPool = buffPoolInfo->bufferPool;
  655.  
  656. // Get number of pages to iterate
  657. int numPages = bm->numPages;
  658.  
  659. // Get the content of each frame in the buffer pool
  660. PageNumber *frameContents = getFrameContents(bm);
  661.  
  662. for (int i = 0; i < numPages; i++) {
  663. page->pageNum = pageNum;
  664. // Check if page is already in the buffer
  665. if (*(frameContents + i) == pageNum) {
  666. // If it's in the buffer, copy the info to page from the frame
  667. FrameInfo *frame = &bufferPool[i];
  668. strcpy(page->data, frame->frameData);
  669.  
  670. // Increase fixCount and close
  671. frame->fixCount++;
  672.  
  673. free(frameContents);
  674. return RC_OK;
  675.  
  676. }
  677. }
  678. for (int i = 0; i < numPages; i++) {
  679. page->pageNum = pageNum;
  680. // Check if there is a free frame that we can use
  681. if (*(frameContents + i) == NO_PAGE) {
  682. // Set pageNumber of frame to the page we are pinning
  683. FrameInfo *frame = &bufferPool[i];
  684. frame->pageNumber = pageNum;
  685.  
  686. // Increase fixCount
  687. frame->fixCount++;
  688. // If there's a free frame, copy page to it
  689. openPageFile(fileName, &fileHandle);
  690. ensureCapacity(pageNum + 1, &fileHandle);
  691. readBlock(pageNum, &fileHandle, frame->frameData);
  692. buffPoolInfo->readNumber++;
  693. strcpy(page->data, frame->frameData);
  694. closePageFile(&fileHandle);
  695. free(frameContents);
  696. return RC_OK;
  697.  
  698. }
  699. }
  700. free(frameContents);
  701. ReplacementStrategy strategy = bm->strategy;
  702. // Check for FIFO
  703. if (strategy == 0) {
  704. return doFifo(bm, page, pageNum);
  705. }
  706. // Check for LRU
  707. if (strategy == 1) {
  708. return RC_OK;
  709. //return LRU(bm, page, pageNum);
  710. }
  711.  
  712. }
  713.  
  714. /************************************************************
  715. * Statistics Interface *
  716. ************************************************************/
  717.  
  718. /*******************************************************************
  719. * NAME : PageNumber *getFrameContents (BM_BufferPool *const bm)
  720. *
  721. * DESCRIPTION : Returns an array of PageNumbers (of size numPages) where the ith element is
  722. * the number of the page stored in the ith page frame.
  723. * An empty page frame is represented using the constant NO_PAGE.
  724. *
  725. * PARAMETERS:
  726. * BM_BufferPool *const bm Stores specific information about a buffer pool
  727. *
  728. * RETURN :
  729. * Type: PageNumber *
  730. * Values: integer representing the number of the page stored in the ith frame, NO_PAGE if empty.
  731. *
  732. * AUTHOR :
  733. * Adrian Tirados <atirados@hawk.iit.edu>
  734. *
  735. * HISTORY :
  736. * DATE WHO DETAIL
  737. * ----------- --------------------------------------- ---------------------------------
  738. * 2016-02-19 Adrian Tirados <atirados@hawk.iit.edu> Initialization
  739. * 2016-02-29 Adrian Tirados <atirados@hawk.iit.edu> Added comments and header comment
  740. *
  741. *******************************************************************/
  742. PageNumber *getFrameContents (BM_BufferPool *const bm) {
  743. // Retrieve the information about the buffer pool
  744. BufferPoolInfo *buffPoolInfo = bm->mgmtData;
  745. FrameInfo *bufferPool = buffPoolInfo->bufferPool;
  746.  
  747. // Get number of pages to iterate
  748. int numPages = bm->numPages;
  749.  
  750. // Allocate a buffer to write the page numbers with numPages size
  751. PageNumber *frameContents = (PageNumber *) malloc(numPages * sizeof(PageNumber));
  752.  
  753. for (int i = 0; i < numPages; i++) {
  754. // Obtain the information directly from the buffer pool struct
  755. frameContents[i] = bufferPool[i].pageNumber;
  756. }
  757.  
  758. return frameContents;
  759. }
  760.  
  761. /*******************************************************************
  762. * NAME : bool *getDirtyFlags (BM_BufferPool *const bm)
  763. *
  764. * DESCRIPTION : Returns an array of bools (of size numPages) where the ith element
  765. * is TRUE if the page stored in the ith page frame is dirty.
  766. * Empty page frames are considered as clean.
  767. *
  768. * PARAMETERS:
  769. * BM_BufferPool *const bm Stores specific information about a buffer pool
  770. *
  771. * RETURN :
  772. * Type: bool *
  773. * Values: boolean representing if page stored in the ith frame is dirty (true) or not (false)
  774. *
  775. * AUTHOR :
  776. * Adrian Tirados <atirados@hawk.iit.edu>
  777. *
  778. * HISTORY :
  779. * DATE WHO DETAIL
  780. * ----------- --------------------------------------- ---------------------------------
  781. * 2016-02-18 Adrian Tirados <atirados@hawk.iit.edu> Initialization
  782. * 2016-02-29 Adrian Tirados <atirados@hawk.iit.edu> Added comments and header comment
  783. *
  784. *******************************************************************/
  785. bool *getDirtyFlags (BM_BufferPool *const bm) {
  786. // Retrieve the information about the buffer pool
  787. BufferPoolInfo *buffPoolInfo = bm->mgmtData;
  788. FrameInfo *bufferPool = buffPoolInfo->bufferPool;
  789.  
  790. // Get number of pages to iterate
  791. int numPages = bm->numPages;
  792.  
  793. // Create array of bools with numPages size
  794. bool *dirtyFlags = (bool *) malloc(numPages * sizeof(bool));
  795.  
  796. // Iterate through the frames and fill this array
  797. for (int i = 0; i < numPages; i++) {
  798. // Obtain the information directly from the buffer pool struct
  799. dirtyFlags[i] = bufferPool[i].isDirty;
  800. }
  801.  
  802. return dirtyFlags;
  803. }
  804.  
  805. /*******************************************************************
  806. * NAME : int *getFixCounts (BM_BufferPool *const bm)
  807. *
  808. * DESCRIPTION : Returns an array of ints (of size numPages) where the ith element is
  809. * the fix count of the page stored in the ith page frame.
  810. * Return 0 for empty page frames.
  811. *
  812. * PARAMETERS:
  813. * BM_BufferPool *const bm Stores specific information about a buffer pool
  814. *
  815. * RETURN :
  816. * Type: int *
  817. * Values: integer representing the number of clients that pinned the file
  818. *
  819. * AUTHOR :
  820. * Adrian Tirados <atirados@hawk.iit.edu>
  821. *
  822. * HISTORY :
  823. * DATE WHO DETAIL
  824. * ----------- --------------------------------------- ---------------------------------
  825. * 2016-02-18 Adrian Tirados <atirados@hawk.iit.edu> Initialization
  826. * 2016-02-29 Adrian Tirados <atirados@hawk.iit.edu> Added comments and header comment
  827. *
  828. *******************************************************************/
  829. int *getFixCounts (BM_BufferPool *const bm) {
  830. // Retrieve the information about the buffer pool
  831. BufferPoolInfo *buffPoolInfo = bm->mgmtData;
  832. FrameInfo *bufferPool = buffPoolInfo->bufferPool;
  833.  
  834. // Get number of pages to iterate
  835. int numPages = bm->numPages;
  836.  
  837. // Allocate a buffer of ints of numPages size
  838. int *fixCounts = (int *) malloc(numPages * sizeof(int));
  839.  
  840. for (int i = 0; i < numPages; i++) {
  841. // Obtain the information directly from the buffer pool struct
  842. fixCounts[i] = bufferPool[i].fixCount;
  843. }
  844.  
  845. return fixCounts;
  846. }
  847.  
  848. /*******************************************************************
  849. * NAME : int getNumReadIO (BM_BufferPool *const bm)
  850. *
  851. * DESCRIPTION : Returns the number of pages that have been read from disk since
  852. * a buffer pool has been initialized.
  853. *
  854. * PARAMETERS:
  855. * BM_BufferPool *const bm Stores specific information about a buffer pool
  856. *
  857. * RETURN :
  858. * Type: int *
  859. * Values: integer representing the number of pages read from disk
  860. *
  861. * AUTHOR :
  862. * Adrian Tirados <atirados@hawk.iit.edu>
  863. *
  864. * HISTORY :
  865. * DATE WHO DETAIL
  866. * ----------- --------------------------------------- ---------------------------------
  867. * 2016-02-19 Adrian Tirados <atirados@hawk.iit.edu> Initialization
  868. * 2016-02-29 Adrian Tirados <atirados@hawk.iit.edu> Added comments and header comment
  869. *
  870. *******************************************************************/
  871. int getNumReadIO (BM_BufferPool *const bm) {
  872. // Obtain the information directly from the buffer pool struct
  873. BufferPoolInfo *buffPoolInfo = bm->mgmtData;
  874. return buffPoolInfo->readNumber;
  875. }
  876.  
  877. /*******************************************************************
  878. * NAME : int getNumWriteIO (BM_BufferPool *const bm)
  879. *
  880. * DESCRIPTION : Returns the number of pages written to the page file since
  881. * the buffer pool has been initialized.
  882. *
  883. * PARAMETERS:
  884. * BM_BufferPool *const bm Stores specific information about a buffer pool
  885. *
  886. * RETURN :
  887. * Type: int *
  888. * Values: integer representing the number of pages written to the page file
  889. *
  890. * AUTHOR :
  891. * Adrian Tirados <atirados@hawk.iit.edu>
  892. *
  893. * HISTORY :
  894. * DATE WHO DETAIL
  895. * ----------- --------------------------------------- ---------------------------------
  896. * 2016-02-19 Adrian Tirados <atirados@hawk.iit.edu> Initialization
  897. * 2016-02-29 Adrian Tirados <atirados@hawk.iit.edu> Added comments and header comment
  898. *
  899. *******************************************************************/
  900. int getNumWriteIO (BM_BufferPool *const bm) {
  901. // Obtain the information directly from the buffer pool struct
  902. BufferPoolInfo *buffPoolInfo = bm->mgmtData;
  903. return buffPoolInfo->writeNumber;
  904. }
Add Comment
Please, Sign In to add comment