void
testClock(void)
{
    // expected results
    const char *poolContents[]= {
        
    "[3x0],[-1 0],[-1 0],[-1 0]",
    "[3x0],[2 0],[-1 0],[-1 0]",
    "[3x0],[2 0],[0 0],[-1 0]",
    "[3x0],[2 0],[0 0],[8 0]",
    "[4 0],[2 0],[0 0],[8 0]",
    "[4 0],[2 0],[0 0],[8 0]",
    "[4 0],[2 0],[5 0],[8 0]",
    "[4 0],[2 0],[5 0],[0 0]",
    "[9 0],[2 0],[5 0],[0 0]",
    "[9 0],[8 0],[5 0],[0 0]",
    "[9 0],[8 0],[3x0],[0 0]",
    "[9 0],[8 0],[3x0],[2 0]"
    };
    const int orderRequests[]= {3,2,0,8,4,2,5,0,9,8,3,2};
        
    int i;
    int snapshot = 0;
    BM_BufferPool *bm = MAKE_POOL();
    BM_PageHandle *h = MAKE_PAGE_HANDLE();
    testName = "Testing CLOCK page replacement";

    CHECK(createPageFile("testbuffer.bin"));
    createDummyPages(bm, 100);
    CHECK(initBufferPool(bm, "testbuffer.bin", 4, RS_CLOCK, NULL));
    
    for (i=0;i<11;i++)
    {
        pinPage(bm,h,orderRequests[i]);
	if(orderRequests[i] == 3)
		markDirty(bm,h);
        unpinPage(bm,h);
        ASSERT_EQUALS_POOL(poolContents[snapshot++], bm, "check pool content using pages");
    }
    
    forceFlushPool(bm);
    // check number of write IOs
    ASSERT_EQUALS_INT(2, getNumWriteIO(bm), "check number of write I/Os");
    ASSERT_EQUALS_INT(10, getNumReadIO(bm), "check number of read I/Os");
    
    CHECK(shutdownBufferPool(bm));
    CHECK(destroyPageFile("testbuffer.bin"));
    
    free(bm);
    free(h);
    TEST_DONE();
}
Exemplo n.º 2
0
/* Purpose: returns if the write has failed or it is fine */
RC forcePage (BM_BufferPool *const bm, BM_PageHandle *const page)
{
BufferManager *mgmtDataInfo=(BufferManager *)bm->mgmtData;
PageFrames *findpage=mgmtDataInfo->HeadNode;
SM_FileHandle fh;
mgmtDataInfo->Total_WriteIO=getNumWriteIO(bm);
if((findpage = Find_Page_Exsist(findpage,page->pageNum,false)) != NULL)
{
		if(writeBlock(findpage->PageNumberValue,&fh,findpage->Data_Value)==RC_OK)
		{
            mgmtDataInfo->Total_WriteIO+=1;
            findpage->DirtyCount=0;
            mgmtDataInfo->DirtyValue[(findpage->PageNumberValue)-1]=false;
		}
}
else
{
	RC_message = "forceFlushPool";
	RC_WRITE_FAILED;
}
return RC_OK;
}
Exemplo n.º 3
0
/*Purpose: when buffer pool is to be shut down, this function will write the pages which are dirty to the memory */
RC forceFlushPool(BM_BufferPool *const bm)
{

BufferManager *mgmtDataInfo=(BufferManager *)bm->mgmtData;
PageFrames *TempTraverseNode=mgmtDataInfo->HeadNode;
RC msg;
if(TempTraverseNode==NULL){return RC_FRAME_NOT_INIT;} 
mgmtDataInfo->Total_WriteIO=getNumWriteIO(bm);

while(TempTraverseNode!=NULL)
{
	if(TempTraverseNode->Pining==0){
	if(TempTraverseNode->DirtyCount==1)
	{
		if((msg = ensureCapacity(TempTraverseNode->PageNumberValue, &mgmtDataInfo->fileHandler_StorageMgr)) != RC_OK){
            return msg;
        }
		if((msg=writeBlock(TempTraverseNode->PageNumberValue,&mgmtDataInfo->fileHandler_StorageMgr,TempTraverseNode->Data_Value))==RC_OK)
		{
            TempTraverseNode->DirtyCount=0;
            mgmtDataInfo->DirtyValue[(TempTraverseNode->PageNumberValue)-1]=false;
            mgmtDataInfo->Total_WriteIO+=1;
		}
        else
        {
            RC_message = "forceFlushPool";
            RC_WRITE_FAILED;
        }
    }
}
else return RC_PINED_PAGE_FOUND;

TempTraverseNode=TempTraverseNode->next;
}

closePageFile(&mgmtDataInfo->fileHandler_StorageMgr);
free(TempTraverseNode);
return RC_OK;
}
// test the LRU page replacement strategy
void
testLRU (void)
{
  // expected results
  const char *poolContents[] = { 
    // read first five pages and directly unpin them
    "[0 0],[-1 0],[-1 0],[-1 0],[-1 0]" , 
    "[0 0],[1 0],[-1 0],[-1 0],[-1 0]", 
    "[0 0],[1 0],[2 0],[-1 0],[-1 0]",
    "[0 0],[1 0],[2 0],[3 0],[-1 0]",
    "[0 0],[1 0],[2 0],[3 0],[4 0]",
    // use some of the page to create a fixed LRU order without changing pool content
    "[0 0],[1 0],[2 0],[3 0],[4 0]",
    "[0 0],[1 0],[2 0],[3 0],[4 0]",
    "[0 0],[1 0],[2 0],[3 0],[4 0]",
    "[0 0],[1 0],[2 0],[3 0],[4 0]",
    "[0 0],[1 0],[2 0],[3 0],[4 0]",
    // check that pages get evicted in LRU order
    "[0 0],[1 0],[2 0],[5 0],[4 0]",
    "[0 0],[1 0],[2 0],[5 0],[6 0]",
    "[7 0],[1 0],[2 0],[5 0],[6 0]",
    "[7 0],[1 0],[8 0],[5 0],[6 0]",
    "[7 0],[9 0],[8 0],[5 0],[6 0]"
  };
  const int orderRequests[] = {3,4,0,2,1};
  const int numLRUOrderChange = 5;

  int i;
  int snapshot = 0;
  BM_BufferPool *bm = MAKE_POOL();
  BM_PageHandle *h = MAKE_PAGE_HANDLE();
  testName = "Testing LRU page replacement";

  CHECK(createPageFile("testbuffer.bin"));
  createDummyPages(bm, 100);
  CHECK(initBufferPool(bm, "testbuffer.bin", 5, RS_LRU, NULL));

  // reading first five pages linearly with direct unpin and no modifications
  for(i = 0; i < 5; i++)
  {
      pinPage(bm, h, i);
      unpinPage(bm, h);
      ASSERT_EQUALS_POOL(poolContents[snapshot++], bm, "check pool content reading in pages");
  }

  // read pages to change LRU order
  for(i = 0; i < numLRUOrderChange; i++)
  {
      pinPage(bm, h, orderRequests[i]);
      unpinPage(bm, h);
      ASSERT_EQUALS_POOL(poolContents[snapshot++], bm, "check pool content using pages");
  }

  // replace pages and check that it happens in LRU order
  for(i = 0; i < 5; i++)
  {
      pinPage(bm, h, 5 + i);
      unpinPage(bm, h);
      ASSERT_EQUALS_POOL(poolContents[snapshot++], bm, "check pool content using pages");
  }

  // check number of write IOs
  ASSERT_EQUALS_INT(0, getNumWriteIO(bm), "check number of write I/Os");
  ASSERT_EQUALS_INT(10, getNumReadIO(bm), "check number of read I/Os");

  CHECK(shutdownBufferPool(bm));
  CHECK(destroyPageFile("testbuffer.bin"));

  free(bm);
  free(h);
  TEST_DONE();
}
void
testFIFO ()
{
  // expected results
  const char *poolContents[] = { 
    "[0 0],[-1 0],[-1 0]" , 
    "[0 0],[1 0],[-1 0]", 
    "[0 0],[1 0],[2 0]", 
    "[3 0],[1 0],[2 0]", 
    "[3 0],[4 0],[2 0]",
    "[3 0],[4 1],[2 0]",
    "[3 0],[4 1],[5x0]",
    "[6x0],[4 1],[5x0]",
    "[6x0],[4 1],[0x0]",
    "[6x0],[4 0],[0x0]",
    "[6 0],[4 0],[0 0]"
  };
  const int requests[] = {0,1,2,3,4,4,5,6,0};
  const int numLinRequests = 5;
  const int numChangeRequests = 3;

  int i;
  BM_BufferPool *bm = MAKE_POOL();
  BM_PageHandle *h = MAKE_PAGE_HANDLE();
  testName = "Testing FIFO page replacement";

  CHECK(createPageFile("testbuffer.bin"));

  createDummyPages(bm, 100);

  CHECK(initBufferPool(bm, "testbuffer.bin", 3, RS_FIFO, NULL));

  // reading some pages linearly with direct unpin and no modifications
  for(i = 0; i < numLinRequests; i++)
    {
      pinPage(bm, h, requests[i]);
      unpinPage(bm, h);
      ASSERT_EQUALS_POOL(poolContents[i], bm, "check pool content");
    }

  // pin one page and test remainder
  i = numLinRequests;
  pinPage(bm, h, requests[i]);
  ASSERT_EQUALS_POOL(poolContents[i],bm,"pool content after pin page");

  // read pages and mark them as dirty
  for(i = numLinRequests + 1; i < numLinRequests + numChangeRequests + 1; i++)
    {
      pinPage(bm, h, requests[i]);
      markDirty(bm, h);
      unpinPage(bm, h);
      ASSERT_EQUALS_POOL(poolContents[i], bm, "check pool content");
    }

  // flush buffer pool to disk
  i = numLinRequests + numChangeRequests + 1;
  h->pageNum = 4;
  unpinPage(bm, h);
  ASSERT_EQUALS_POOL(poolContents[i],bm,"unpin last page");
  
  i++;
  forceFlushPool(bm);
  ASSERT_EQUALS_POOL(poolContents[i],bm,"pool content after flush");

  // check number of write IOs
  ASSERT_EQUALS_INT(3, getNumWriteIO(bm), "check number of write I/Os");
  ASSERT_EQUALS_INT(8, getNumReadIO(bm), "check number of read I/Os");

  CHECK(shutdownBufferPool(bm));
  CHECK(destroyPageFile("testbuffer.bin"));

  free(bm);
  free(h);
  TEST_DONE();
}
Exemplo n.º 6
0
// test the CLOCK page replacement strategy
void
testClock (void)
{
    // expected results
    const char *poolContents[] = {
        // read first five pages and directly unpin them
        "[0 0],[-1 0],[-1 0]" ,
        "[0 0],[1 0],[-1 0]" ,
        "[2 0],[1 0],[-1 0]" ,
        "[2 0],[1 0],[3 0]" ,
        
        // pin the page 2 again
        "[2 0],[1 0],[3 0]" ,
        
        // read other pages use pin_CLOCK
        "[2 0],[4 0],[3 0]" ,
        "[5 0],[4 0],[3 0]" ,
        "[6 0],[4 0],[3 0]",
        "[6 0],[7 0],[3 0]"
    };
    const int orderRequests[] = {2};
    const int numrfChange = 1;
    
    int i;
    int snapshot = 0;
    BM_BufferPool *bm = MAKE_POOL();
    BM_PageHandle *h = MAKE_PAGE_HANDLE();
    testName = "Testing CLOCK page replacement";
    
    CHECK(createPageFile("testbuffer.bin"));
    createDummyPages(bm, 100);
    CHECK(initBufferPool(bm, "testbuffer.bin", 3, RS_CLOCK, NULL));
    
    // reading first five pages linearly with direct unpin and no modifications
    for(i = 0; i <4; i++)
    {
        pinPage(bm, h, i);
        unpinPage(bm, h);
        ASSERT_EQUALS_POOL(poolContents[snapshot++], bm, "check pool content reading in pages");
    }
    // read pages to rf of the frames
    for(i = 0; i < numrfChange; i++)
    {
        pinPage(bm, h, orderRequests[i]);
        unpinPage(bm, h);
        ASSERT_EQUALS_POOL(poolContents[snapshot++], bm, "check pool content using pages");
    }
    
    // replace pages and check that it happens in LRU_K order
    for(i = 0; i < 4; i++)
    {
        pinPage(bm, h, 4 + i);
        unpinPage(bm, h);
        ASSERT_EQUALS_POOL(poolContents[snapshot++], bm, "check pool content using pages");
        
    }
    
    // check number of write IOs
    ASSERT_EQUALS_INT(0, getNumWriteIO(bm), "check number of write I/Os");
    ASSERT_EQUALS_INT(8, getNumReadIO(bm), "check number of read I/Os");
    
    CHECK(shutdownBufferPool(bm));
    CHECK(destroyPageFile("testbuffer.bin"));
    
    free(bm);
    free(h);
    TEST_DONE();
}
//Clock[START]
void testClock()
{
	// expected results
	  const char *poolContents[] = {
			  "[0 0],[-1 0],[-1 0]",
			  	  "[0 0],[4 0],[-1 0]",
			  	  "[0 0],[4 0],[1 0]",
			  	  "[0 0],[4 0],[1 0]",
			  	  "[2 0],[4 0],[1 0]",
			  	  "[2 0],[4 0],[1 0]",
			  	  "[2 0],[4 0],[3 0]",
			  	  "[2 0],[4 0],[3 0]",
			  	  "[2 0],[4 0],[3 0]",
			  	  "[2 0],[4 0],[3 0]",
			  	  "[2 0],[4 0],[0 0]",
			  	  "[2 0],[4 0],[0 0]",
			  	  "[1 0],[4 0],[0 0]",
			  	  "[1 0],[4 0],[0 0]",
			  	  "[1 0],[4 0],[2 0]",
			  	  "[1 0],[4 0],[2 0]",
			  	  "[3 0],[4 0],[2 0]",
			  	  "[3 0],[4 0],[2 0]",
				  "[3 0],[4 0],[2 0]",
	  };
	  const int requests[] = {0,4,1,4,2,4,3,4,2,4,0,4,1,4,2,4,3,4};


	  int i;
	  BM_BufferPool *bm = MAKE_POOL();
	  BM_PageHandle *h = MAKE_PAGE_HANDLE();
	  testName = "Testing CLOCK page replacement";

	  CHECK(createPageFile("testbuffer.bin"));

	  createDummyPages(bm, 100);

	  CHECK(initBufferPool(bm, "testbuffer.bin", 3, RS_CLOCK, NULL));

	  // reading some pages linearly with direct unpin and no modifications
	  for(i = 0; i < 18; i++)
	    {
	      pinPage(bm, h, requests[i]);
	      unpinPage(bm, h);
	      ASSERT_EQUALS_POOL(poolContents[i], bm, "check pool content");
	    }


 	  forceFlushPool(bm);
	  ASSERT_EQUALS_POOL(poolContents[i],bm,"pool content after flush");

	  // check number of write IOs
	  //0 writes because no dirty pages were present
	  ASSERT_EQUALS_INT(0, getNumWriteIO(bm), "check number of write I/Os");


	  ASSERT_EQUALS_INT(9, getNumReadIO(bm), "check number of read I/Os");

	  CHECK(shutdownBufferPool(bm));
	  CHECK(destroyPageFile("testbuffer.bin"));

	  free(bm);
	  free(h);
	  TEST_DONE();

}
void testLFU()
{
	// expected results
	  const char *poolContents[] = {
		//Pin and Unpin 3 pages
	    "[0 0],[-1 0],[-1 0]" ,
	    "[0 0],[1 0],[-1 0]",
	    "[0 0],[1 0],[2 0]",

		//Pin the 0th page thrice,1st page twice
		"[0 3],[1 2],[2 0]",

		//Now pin a new page say 4. 4 should replace page 2 as page 2 has been Least Freq Used.
		"[0 3],[1 2],[4 1]",

		//Pin the 4th page thrice
		"[0 3],[1 2],[4 4]",

		//Now pin a new page 5. 5 should replace page 1 as page 1 has been Least Freq Used.
		"[0 3],[5 1],[4 4]",

		//Unpin page 0 thrice, page 5 once and page 4 four times.
		"[0 0],[5 0],[4 0]",
	  };
	  const int requests[] = {0,1,2,3,4,5,6};
	  const int numLinRequests = 3;

	  int i;
	  BM_BufferPool *bm = MAKE_POOL();
	  BM_PageHandle *h = MAKE_PAGE_HANDLE();
	  testName = "Testing LFU page replacement";

	  CHECK(createPageFile("testbuffer.bin"));

	  createDummyPages(bm, 100);

	  CHECK(initBufferPool(bm, "testbuffer.bin", 3, RS_LFU, NULL));

	  // reading some pages linearly with direct unpin and no modifications
	  for(i = 0; i < numLinRequests; i++)
	    {
	      pinPage(bm, h, requests[i]);
	      unpinPage(bm, h);
	      ASSERT_EQUALS_POOL(poolContents[i], bm, "check pool content");
	    }


	  pinPage(bm, h, 0);
	  pinPage(bm, h, 0);
	  pinPage(bm, h, 0);
	  pinPage(bm, h, 1);
	  pinPage(bm, h, 1);
	  ASSERT_EQUALS_POOL(poolContents[i++],bm,"pool content after pin page");

	  pinPage(bm, h, 4);
	  ASSERT_EQUALS_POOL(poolContents[i++],bm,"pool content after pin page");

	  pinPage(bm,h,4);
	  pinPage(bm,h,4);
	  pinPage(bm,h,4);
	  ASSERT_EQUALS_POOL(poolContents[i++],bm,"pool content after pin page");

	  pinPage(bm,h,5);
	  ASSERT_EQUALS_POOL(poolContents[i++],bm,"pool content after pin page");


	  h->data = "Page-0";
	  h->pageNum = 0;
	  unpinPage(bm,h);
	  unpinPage(bm,h);
	  unpinPage(bm,h);

	  h->data = "Page-5";
	  h->pageNum = 5;
	  unpinPage(bm,h);

	  h->data = "Page-4";
	  h->pageNum = 4;
	  unpinPage(bm,h);
	  unpinPage(bm,h);
	  unpinPage(bm,h);
	  unpinPage(bm,h);
 	  forceFlushPool(bm);
	  ASSERT_EQUALS_POOL(poolContents[i],bm,"pool content after flush");

	  // check number of write IOs
	  //0 writes because no dirty pages were present
	  ASSERT_EQUALS_INT(0, getNumWriteIO(bm), "check number of write I/Os");


	  ASSERT_EQUALS_INT(5, getNumReadIO(bm), "check number of read I/Os");

	  CHECK(shutdownBufferPool(bm));
	  CHECK(destroyPageFile("testbuffer.bin"));

	  free(bm);
	  free(h);
	  TEST_DONE();

}