applayerpluginsandutils/bookmarksupport/test/cenrepsrv/cachemgr.cpp
changeset 0 b16258d2340f
equal deleted inserted replaced
-1:000000000000 0:b16258d2340f
       
     1 // Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include "srvrepos.h"
       
    17 #include "cachemgr.h"
       
    18 
       
    19 _LIT(KCacheLit, "CoarseGrainedCache");
       
    20 _LIT(KDefaultCacheSizeLit, "size");
       
    21 _LIT(KDefaultEvictionTimeoutLit, "timeout");
       
    22     
       
    23 CRepositoryCacheManager* CRepositoryCacheManager::NewLC()
       
    24 	{
       
    25 	CRepositoryCacheManager* self = new(ELeave) CRepositoryCacheManager;
       
    26 	CleanupStack::PushL(self);
       
    27 	self->ConstructL();
       
    28 	return self;
       
    29 	}
       
    30 
       
    31 CRepositoryCacheManager::~CRepositoryCacheManager()
       
    32 	{
       
    33 	DisableCache();
       
    34 	}
       
    35 
       
    36 void CRepositoryCacheManager::ConstructL()
       
    37 	{
       
    38 	CTimer::ConstructL();
       
    39 		
       
    40 	CIniData* iniData = NULL;
       
    41 	TInt res = KErrNone;
       
    42 	TBuf<KMaxFileName> iniFile;
       
    43 	
       
    44 	iniFile.Copy( *TServerResources::iInstallDirectory );
       
    45 	iniFile.Append( KCacheMgrIniFile );	
       
    46 	TRAP(res, iniData = CIniData::NewL(iniFile));
       
    47 	if(res==KErrNotFound)
       
    48 		{
       
    49 		// if RomDirectory exists
       
    50 		if (TServerResources::iRomDirectory)
       
    51 			{
       
    52 			iniFile.Copy( *TServerResources::iRomDirectory );
       
    53 			iniFile.Append( KCacheMgrIniFile );	
       
    54 			TRAP(res, iniData = CIniData::NewL(iniFile));
       
    55 			}
       
    56 		if(res==KErrNotFound)
       
    57 			{
       
    58 			__CENTREP_TRACE1("CENTREP: Central Repository Cache Parameters ini file %S not found. Default values will be used.", &KCacheMgrIniFile);
       
    59 			return;
       
    60 			}
       
    61 		}
       
    62 	if (res != KErrNone)
       
    63 		{
       
    64 		User::Leave(res);
       
    65 		}
       
    66 		
       
    67 	CleanupStack::PushL(iniData);
       
    68 	
       
    69 	// process the file here
       
    70 	if(!iniData->FindVar(KCacheLit(),KDefaultCacheSizeLit(), iCacheSize))
       
    71 		{
       
    72 		iCacheSize = KDefaultCacheSize;
       
    73 		}
       
    74 
       
    75 	TInt tempTimeout;
       
    76 	if(iniData->FindVar(KCacheLit(),KDefaultEvictionTimeoutLit(), tempTimeout))
       
    77 		{
       
    78 		iDefaultTimeout = tempTimeout;
       
    79 		}
       
    80 	
       
    81 	CleanupStack::PopAndDestroy();		
       
    82 	};
       
    83 
       
    84 void CRepositoryCacheManager::EnableCache(TInt aDefaultTimeout, TInt aCacheSize)
       
    85 	{
       
    86 	if (aDefaultTimeout>0)
       
    87 		{
       
    88 		iDefaultTimeout = aDefaultTimeout;
       
    89 		}
       
    90 	if (aCacheSize>0)
       
    91 		{
       
    92 		iCacheSize = aCacheSize;
       
    93 		}
       
    94 	
       
    95 	EnableCache();
       
    96 	}
       
    97 
       
    98 void CRepositoryCacheManager::EnableCache()
       
    99 	{
       
   100 	// If disabled, enable
       
   101 	if (!iEnabled)
       
   102 		{
       
   103 		iEnabled = ETrue;
       
   104 		__CENTREP_TRACE2("CENTREP: Cache Enabled. Size:%d Default Timeout:%d", iCacheSize, iDefaultTimeout.Int());
       
   105 		}
       
   106 	}
       
   107 		
       
   108 void CRepositoryCacheManager::DisableCache()
       
   109 	{
       
   110 	// If enabled, disable
       
   111 	if (iEnabled)
       
   112 		{
       
   113 		FlushCache();
       
   114 		
       
   115 		iEnabled = EFalse;
       
   116 		__CENTREP_TRACE("CENTREP: Cache Disabled");
       
   117 		}
       
   118 	}
       
   119 
       
   120 void CRepositoryCacheManager::RemoveIdleRepository(CSharedRepository* aRepository)
       
   121 	{
       
   122 	if (iEnabled)
       
   123 		{
       
   124 		TInt i;
       
   125 		for(i=iIdleRepositories.Count()-1; i>=0; i--)
       
   126 			{
       
   127 			if(iIdleRepositories[i].iSharedRepository==aRepository)
       
   128 				{
       
   129 				break;
       
   130 				}
       
   131 			}
       
   132 		
       
   133 		// Idle repository might not be found in the list if multiple clients try to open the same 
       
   134 		// repository at the same time. First client will remove it and second one will not find it
       
   135 		if(i>=0)
       
   136 			{
       
   137 			__CENTREP_TRACE1("CENTREP: Cache Hit when opening repository %x", aRepository->Uid().iUid);
       
   138 
       
   139 			iTotalCacheUsage -= iIdleRepositories[i].iSharedRepository->Size();		
       
   140 			iIdleRepositories.Remove(i);
       
   141 			
       
   142 			// If this was the first repository on the list, it means its timer is still ticking. 
       
   143 			// We have to stop it and ...
       
   144 			if (i==0)
       
   145 				{
       
   146 				Cancel();
       
   147 				// if there's still other repositories in the list, reschedule the timer with the
       
   148 				// new top-of-the-list  
       
   149 				if (iIdleRepositories.Count())
       
   150 					{
       
   151 					AtUTC(iIdleRepositories[0].iCacheTime);
       
   152 					}
       
   153 				}
       
   154 			}
       
   155 		else
       
   156 			{
       
   157 			__CENTREP_TRACE1("CENTREP: Multiple Client Hit when opening repository %x", aRepository->Uid().iUid);			
       
   158 			}
       
   159 		}
       
   160 	}
       
   161 
       
   162 #ifdef CACHE_OOM_TESTABILITY
       
   163   	// This code is only for tesing and doesn't go into MCL
       
   164 	// Hence hide the leave in a macro instead of making StartEvictionL
       
   165 #define TEST_CODE_LEAVE(x) User::Leave(x)
       
   166 #endif	
       
   167 
       
   168 TBool CRepositoryCacheManager::StartEviction(CSharedRepository*& aRepository)
       
   169 	{
       
   170 	// Execute the forced eviction algorithm only if it will make sense
       
   171 	// The eviction makes sense if:
       
   172 	// - there's anything in the cache to evict
       
   173 	// - the repository we're trying to cache can fit in the cache after evictions
       
   174 	if (iIdleRepositories.Count() && (aRepository->Size() < iCacheSize))
       
   175 		{
       
   176 		// Check to see if current cache size + the current repository size is overshooting the limit
       
   177 		if (iTotalCacheUsage + aRepository->Size() > iCacheSize)
       
   178 			{
       
   179 			// Forced eviction
       
   180 			__CENTREP_TRACE3("CENTREP: Cache Size Exceeded: Current(%d)+Size(%d)>Cache(%d)", iTotalCacheUsage, aRepository->Size(), iCacheSize);
       
   181 			
       
   182 			// Sort in the forced eviction order
       
   183 			TLinearOrder<TRepositoryCacheInfo> forcedSortOrder(CRepositoryCacheManager::ForcedEvictionSortOrder);
       
   184 			iIdleRepositories.Sort(forcedSortOrder);
       
   185 			
       
   186 			// Evict one by one until there's enough cache space or we run out of idle reps
       
   187 			do
       
   188 				{
       
   189 				__CENTREP_TRACE1("CENTREP: Forced Eviction of repository %x", iIdleRepositories[0].iSharedRepository->Uid().iUid);			
       
   190 				iTotalCacheUsage -= iIdleRepositories[0].iSharedRepository->Size();
       
   191 				Evict(0);
       
   192 				iIdleRepositories.Remove(0);		
       
   193 				} while (iIdleRepositories.Count() && (iTotalCacheUsage + aRepository->Size() > iCacheSize));
       
   194 			
       
   195 #ifdef CENTREP_TRACE			
       
   196 			if (!iIdleRepositories.Count())
       
   197 				{
       
   198 				__CENTREP_TRACE("CENREP: Cache Emptied by Forced Eviction");
       
   199 				}
       
   200 #endif				
       
   201 			// Re-sort to timer order for normal operation
       
   202 			TLinearOrder<TRepositoryCacheInfo> timerSortOrder(CRepositoryCacheManager::TimerEvictionSortOrder);
       
   203 			iIdleRepositories.Sort(timerSortOrder);
       
   204 			};
       
   205 		}
       
   206 	
       
   207 	// See if there's enough space now
       
   208 	if (iTotalCacheUsage + aRepository->Size() > iCacheSize)
       
   209 		{
       
   210 		return EFalse;
       
   211 		}
       
   212 
       
   213 	// Create new item for the cache and insert it in the list
       
   214 	TRepositoryCacheInfo repInfo;
       
   215 	
       
   216 	repInfo.iCacheTime.UniversalTime();
       
   217 	repInfo.iCacheTime += TTimeIntervalMicroSeconds32(iDefaultTimeout);
       
   218 	repInfo.iSharedRepository = aRepository;
       
   219 	
       
   220 	TLinearOrder<TRepositoryCacheInfo> timerSortOrder(CRepositoryCacheManager::TimerEvictionSortOrder);
       
   221 	// With the same timeout value assigned to all repositories, no two repositories can have the same 
       
   222 	// timeout theoretically, so InsertInOrder is sufficient. But in practice, because of the poor 
       
   223 	// resolution of the NTickCount() function used by TTime::UniversalTime(), InsertInOrderAllowRepeats 
       
   224 	// should be used instead of InsertInOrder to allow for duplicate timer values caused by two 
       
   225 	// repositories cached in quick succession (<1ms)
       
   226 	TInt err = iIdleRepositories.InsertInOrderAllowRepeats(repInfo, timerSortOrder);
       
   227 #ifdef CACHE_OOM_TESTABILITY
       
   228   	// This code is only for tesing and doesn't go into MCL
       
   229   	if (err == KErrNoMemory)	
       
   230   		{
       
   231   		delete aRepository;
       
   232   		aRepository = NULL;
       
   233   		// Should Leave here for the OOM tests to successfully complete. 
       
   234 		TEST_CODE_LEAVE(err);
       
   235   		}
       
   236 #endif	
       
   237 	if (err!=KErrNone)
       
   238 		{
       
   239 		return EFalse;
       
   240 		}
       
   241 
       
   242 	iTotalCacheUsage += repInfo.iSharedRepository->Size();
       
   243 	
       
   244 	// reset timer to the new top-of-the-list
       
   245 	Cancel();
       
   246 	AtUTC(iIdleRepositories[0].iCacheTime);
       
   247 
       
   248 	return ETrue;
       
   249 	}
       
   250 
       
   251 void CRepositoryCacheManager::FlushCache()
       
   252 	{
       
   253 	// cancel any outstanding timer
       
   254 	Cancel();
       
   255 
       
   256 	TInt idleRepCount = iIdleRepositories.Count();
       
   257 	for(TInt repCount = idleRepCount - 1; repCount >= 0 ; repCount--)	
       
   258 		{
       
   259 		Evict(repCount);
       
   260 		}
       
   261 	
       
   262 	// empty the list
       
   263 	iIdleRepositories.Reset();
       
   264 	
       
   265 	iTotalCacheUsage = 0;
       
   266 	__CENTREP_TRACE1("CENTREP: Cache Flush: %d repositories flushed", idleRepCount);
       
   267 	}
       
   268 	
       
   269 // Evict only removes items from iOpenRepositories list, not from iIdleRepositories list
       
   270 void CRepositoryCacheManager::Evict(TInt aIdleRepIndex)
       
   271 	{
       
   272 	// find and remove the idle repositories' pointers in the open repositories list 
       
   273 	CServerRepository::RemoveRepository(iIdleRepositories[aIdleRepIndex].iSharedRepository);
       
   274 	
       
   275 	// delete the repository objects 
       
   276 	delete iIdleRepositories[aIdleRepIndex].iSharedRepository;
       
   277 	
       
   278 	}
       
   279 		
       
   280 void CRepositoryCacheManager::RunL()
       
   281 	{
       
   282 	TTime now;
       
   283 
       
   284 	now.UniversalTime();
       
   285 
       
   286 	// Try to evict all the repositories which have expired. There might be more than one repository
       
   287 	// destined to expire at the same time, or there are more than one repository with expiry times
       
   288 	// between the scheduled expiry time and now (which theoretically should have been the same, but maybe
       
   289 	// because of other procesor activity, the timer Active Object just got late a bit)
       
   290 	while((iIdleRepositories.Count()) && (iIdleRepositories[0].iCacheTime<=now))
       
   291 		{
       
   292 		__CENTREP_TRACE1("CENTREP: Normal Eviction of repository %x", iIdleRepositories[0].iSharedRepository->Uid().iUid);			
       
   293 		// Always remove from the top of the sorted list
       
   294 		iTotalCacheUsage -= iIdleRepositories[0].iSharedRepository->Size();		
       
   295 		Evict(0);
       
   296 		iIdleRepositories.Remove(0);		
       
   297 		};
       
   298 		
       
   299 	// reschedule to run again at the expiry date of next repository on the list, if any
       
   300 	if (iIdleRepositories.Count())
       
   301 		{
       
   302 		AtUTC(iIdleRepositories[0].iCacheTime);
       
   303 		}
       
   304 	else
       
   305 		{
       
   306 		__CENTREP_TRACE("CENTREP: Cache Empty/Timer Disabled");			
       
   307 		}
       
   308 	}
       
   309 
       
   310 TInt CRepositoryCacheManager::ForcedEvictionSortOrder(const TRepositoryCacheInfo &aRepository1, const TRepositoryCacheInfo &aRepository2)
       
   311 	{
       
   312 /*
       
   313    The code in the comments below is the original simple-to-read version of the algebraically
       
   314    simplified production code. 
       
   315 
       
   316 	TTime now;
       
   317 
       
   318 	now.UniversalTime();
       
   319 
       
   320 	// we calculate the ages of the repositories by taking the difference between now and when
       
   321 	// they were last became idle. When refactoring, the calculation of the absolute ages will be 
       
   322 	// eleminated and the age difference between the repositories will be used in the formula instead
       
   323 	
       
   324 	TTimeIntervalMicroSeconds age1 = now.MicroSecondsFrom(aRepository1.iCacheTime);
       
   325 	TTimeIntervalMicroSeconds age2 = now.MicroSecondsFrom(aRepository2.iCacheTime);
       
   326 	
       
   327 	// then divide the resulting numbers by conversion constant to get a number in a compatible unit
       
   328 	// to the size. This operation reduces the microsecond-based values to having an approx. max
       
   329 	// of 100K units
       
   330 
       
   331 	TInt t1 = age1.Int64()/KTimeoutToSizeConversion;
       
   332 	TInt t2 = age2.Int64()/KTimeoutToSizeConversion;
       
   333 	
       
   334 	// the resulting normalized time difference values are added to the size of the repository
       
   335 	// resulting in an implicit %50 weight in the overall importance value 
       
   336 	// An approx. maximum size of a repository is assumed to be around 100K
       
   337 	
       
   338 	TInt importance1 = t1+aRepository1.iSharedRepository->Size();
       
   339 	TInt importance2 = t2+aRepository2.iSharedRepository->Size();
       
   340 	
       
   341 	// the difference between the importances of the repositories determine the sorting order
       
   342 
       
   343 	return static_cast<TInt>(importance1-importance2);
       
   344 */	
       
   345 	//	after refactoring, the resulting formula is this one
       
   346 	return static_cast<TInt>(((aRepository1.iCacheTime.Int64()-aRepository2.iCacheTime.Int64())/KTimeoutToSizeConversion)+(aRepository1.iSharedRepository->Size()-aRepository2.iSharedRepository->Size()));	
       
   347 	}
       
   348 
       
   349 TInt CRepositoryCacheManager::TimerEvictionSortOrder(const TRepositoryCacheInfo &aRepository1, const TRepositoryCacheInfo &aRepository2)
       
   350 	{
       
   351 	return static_cast<TInt>(aRepository1.iCacheTime.Int64()-aRepository2.iCacheTime.Int64());
       
   352 	}