CacheTestBase.cpp

Go to the documentation of this file.
00001 /*
00002 // $Id: //open/dev/fennel/test/CacheTestBase.cpp#13 $
00003 // Fennel is a library of data storage and processing components.
00004 // Copyright (C) 2005-2009 The Eigenbase Project
00005 // Copyright (C) 2005-2009 SQLstream, Inc.
00006 // Copyright (C) 2005-2009 LucidEra, Inc.
00007 // Portions Copyright (C) 1999-2009 John V. Sichi
00008 //
00009 // This program is free software; you can redistribute it and/or modify it
00010 // under the terms of the GNU General Public License as published by the Free
00011 // Software Foundation; either version 2 of the License, or (at your option)
00012 // any later version approved by The Eigenbase Project.
00013 //
00014 // This program is distributed in the hope that it will be useful,
00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 // GNU General Public License for more details.
00018 //
00019 // You should have received a copy of the GNU General Public License
00020 // along with this program; if not, write to the Free Software
00021 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 */
00023 
00024 #include "fennel/common/CommonPreamble.h"
00025 #include "fennel/test/CacheTestBase.h"
00026 #include "fennel/common/FileSystem.h"
00027 #include "fennel/device/RandomAccessFileDevice.h"
00028 #include "fennel/synch/Thread.h"
00029 #include "fennel/cache/CachePage.h"
00030 #include "fennel/cache/CacheImpl.h"
00031 #include "fennel/cache/RandomVictimPolicy.h"
00032 #include "fennel/cache/LRUVictimPolicy.h"
00033 #include <boost/test/test_tools.hpp>
00034 #include <strstream>
00035 
00036 #ifdef __MSVC__
00037 #include <process.h>
00038 #endif
00039 
00040 using namespace fennel;
00041 
00042 Cache &CacheTestBase::getCache()
00043 {
00044     return *pCache;
00045 }
00046 
00047 typedef CacheImpl<
00048     CachePage,
00049     RandomVictimPolicy<CachePage>
00050 > RandomCache;
00051 
00052 class LRUPage : public CachePage, public LRUVictim
00053 {
00054 public:
00055     LRUPage(Cache &cache,PBuffer buffer)
00056         : CachePage(cache,buffer)
00057     {
00058     }
00059 };
00060 
00061 typedef CacheImpl<
00062     LRUPage,
00063     LRUVictimPolicy<LRUPage>
00064 > LRUCache;
00065 
00066 SharedCache CacheTestBase::newCache()
00067 {
00068     switch (victimPolicy) {
00069     case victimRandom:
00070         return SharedCache(
00071             new RandomCache(cacheParams),
00072             ClosableObjectDestructor());
00073     case victimLRU:
00074         return SharedCache(
00075             new LRUCache(cacheParams),
00076             ClosableObjectDestructor());
00077     case victimTwoQ:
00078         return Cache::newCache(cacheParams);
00079     default:
00080         permAssert(false);
00081     }
00082 }
00083 
00084 SharedRandomAccessDevice CacheTestBase::openDevice(
00085     std::string devName,DeviceMode openMode,uint nDevicePages,
00086     DeviceId deviceId)
00087 {
00088     if (openMode.create) {
00089         FileSystem::remove(devName.c_str());
00090     }
00091     SharedRandomAccessDevice pDevice(
00092         new RandomAccessFileDevice(devName,openMode));
00093     if (openMode.create) {
00094         pDevice->setSizeInBytes(nDevicePages*cbPageFull);
00095     }
00096     pCache->registerDevice(deviceId,pDevice);
00097     return pDevice;
00098 }
00099 
00100 void CacheTestBase::openStorage(DeviceMode openMode)
00101 {
00102     // make a test.dat filename unique to each process
00103     std::ostrstream testDataFile;
00104     testDataFile << "test-" << getpid() << ".dat" << ends;
00105 
00106     pCache = newCache();
00107 
00108     statsTimer.addSource(pCache);
00109     statsTimer.start();
00110 
00111     pRandomAccessDevice = openDevice(
00112         testDataFile.str(),openMode,nDiskPages,dataDeviceId);
00113 }
00114 
00115 void CacheTestBase::closeStorage()
00116 {
00117     closeDevice(dataDeviceId,pRandomAccessDevice);
00118     statsTimer.stop();
00119     if (pCache) {
00120         assert(pCache.unique());
00121         pCache.reset();
00122     }
00123 }
00124 
00125 void CacheTestBase::testCaseTearDown()
00126 {
00127     closeStorage();
00128 }
00129 
00130 void CacheTestBase::closeDevice(
00131     DeviceId deviceId,SharedRandomAccessDevice &pDevice)
00132 {
00133     if (!pDevice) {
00134         return;
00135     }
00136     DeviceIdPagePredicate pagePredicate(deviceId);
00137     pCache->checkpointPages(pagePredicate,CHECKPOINT_FLUSH_AND_UNMAP);
00138     pCache->unregisterDevice(deviceId);
00139     assert(pDevice.unique());
00140     pDevice.reset();
00141 }
00142 
00143 CacheTestBase::CacheTestBase()
00144 {
00145     cacheParams.readConfig(configMap);
00146 
00147     nDiskPages = configMap.getIntParam("diskPages",1000);
00148     dataDeviceId = DeviceId(23);
00149     nMemPages = cacheParams.nMemPagesMax;
00150     cbPageFull = cacheParams.cbPage;
00151     std::string victimPolicyString = configMap.getStringParam(
00152         "victimPolicy","twoq");
00153     if (victimPolicyString == "random") {
00154         victimPolicy = victimRandom;
00155     } else if (victimPolicyString == "lru") {
00156         victimPolicy = victimLRU;
00157     } else if (victimPolicyString == "twoq") {
00158         victimPolicy = victimTwoQ;
00159     } else {
00160         std::cerr << "Unknown victim policy " << victimPolicyString
00161                   << std::endl;
00162         exit(-1);
00163     }
00164 
00165 }
00166 
00167 // End CacheTestBase.cpp

Generated on Mon Jun 22 04:00:20 2009 for Fennel by  doxygen 1.5.1