Fennel: /home/pub/open/dev/fennel/test/CacheTestBase.cpp Source File (original) (raw)

00001 00002 00003 00004 00005 00006 00007 00008 00009 00010 00011 00012 00013 00014 00015 00016 00017 00018 00019 00020 00021 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 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 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 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(nDevicePagescbPageFull); 00095 } 00096 pCache->registerDevice(deviceId,pDevice); 00097 return pDevice; 00098 } 00099 00100 void CacheTestBase::openStorage(DeviceMode openMode) 00101 { 00102
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