Advertisement
Guest User

Untitled

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