|
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_noc.h" |
|
17 #include "cachemgr.h" |
|
18 #include <bsul/bsul.h> |
|
19 |
|
20 #define UNUSED_VAR(a) a = a |
|
21 |
|
22 _LIT(KCacheLit, "CoarseGrainedCache"); |
|
23 _LIT(KDefaultCacheSizeLit, "size"); |
|
24 _LIT(KDefaultEvictionTimeoutLit, "timeout"); |
|
25 |
|
26 CRepositoryCacheManager* CRepositoryCacheManager::NewLC(RFs& aFs) |
|
27 { |
|
28 CRepositoryCacheManager* self = new(ELeave) CRepositoryCacheManager; |
|
29 CleanupStack::PushL(self); |
|
30 self->ConstructL(aFs); |
|
31 return self; |
|
32 } |
|
33 |
|
34 CRepositoryCacheManager::~CRepositoryCacheManager() |
|
35 { |
|
36 DisableCache(ETrue); |
|
37 } |
|
38 |
|
39 void CRepositoryCacheManager::ConstructL(RFs& aFs) |
|
40 { |
|
41 CTimer::ConstructL(); |
|
42 |
|
43 BSUL::CIniFile16* iniFile = NULL; |
|
44 TInt res = KErrNone; |
|
45 TBuf<KMaxFileName> iniFileName; |
|
46 |
|
47 iniFileName.Copy( *TServerResources::iInstallDirectory ); |
|
48 iniFileName.Append( KCacheMgrIniFile ); |
|
49 TRAP(res, iniFile = BSUL::CIniFile16::NewL(aFs, iniFileName, ETrue)); |
|
50 if(res==KErrNotFound) |
|
51 { |
|
52 // if RomDirectory exists |
|
53 if (TServerResources::iRomDirectory) |
|
54 { |
|
55 iniFileName.Copy( *TServerResources::iRomDirectory ); |
|
56 iniFileName.Append( KCacheMgrIniFile ); |
|
57 TRAP(res, iniFile = BSUL::CIniFile16::NewL(aFs, iniFileName, ETrue)); |
|
58 } |
|
59 if(res==KErrNotFound) |
|
60 { |
|
61 __CENTREP_TRACE1("CENTREP: Central Repository Cache Parameters ini file %S not found. Default values will be used.", &KCacheMgrIniFile); |
|
62 return; |
|
63 } |
|
64 } |
|
65 if (res != KErrNone) |
|
66 { |
|
67 User::Leave(res); |
|
68 } |
|
69 |
|
70 CleanupStack::PushL(iniFile); |
|
71 |
|
72 TBuf<20> buffer; |
|
73 TPtrC ptr(buffer); |
|
74 |
|
75 // find the value |
|
76 res = iniFile->FindVar(KCacheLit(), KDefaultCacheSizeLit(), ptr); |
|
77 TLex lex(ptr); |
|
78 |
|
79 TBool valueFound = EFalse; |
|
80 |
|
81 // if the value can't be found or can't be converted into a positive integer, use the default |
|
82 if (res != KErrNone || lex.Val(iCacheSize) != KErrNone || iCacheSize < 0) |
|
83 { |
|
84 iCacheSize = KDefaultCacheSize; |
|
85 } |
|
86 else |
|
87 { |
|
88 valueFound = ETrue; |
|
89 } |
|
90 |
|
91 // if the value can be found, convert it |
|
92 if (iniFile->FindVar(KCacheLit(), KDefaultEvictionTimeoutLit(), ptr) == KErrNone) |
|
93 { |
|
94 TInt tempTimeout; |
|
95 lex.Assign(ptr); |
|
96 // if the value can be converted into a positive integer, assign it to iDefaultTimeout. |
|
97 if (lex.Val(tempTimeout) == KErrNone && tempTimeout >= 0) |
|
98 { |
|
99 iDefaultTimeout = tempTimeout; |
|
100 valueFound = ETrue; |
|
101 } |
|
102 } |
|
103 |
|
104 #ifdef _DEBUG |
|
105 // in Debug mode, if the Cache ini file exists either in install directory or |
|
106 // rom directory but does not contains the correct section "CoarseGrainedCache" |
|
107 // nor any key of "size" and "timeout", the server panics. |
|
108 if(! valueFound) |
|
109 { |
|
110 Panic(ECacheIniFileCorrupted); |
|
111 } |
|
112 #else |
|
113 UNUSED_VAR(valueFound); |
|
114 #endif |
|
115 |
|
116 CleanupStack::PopAndDestroy(iniFile); |
|
117 } |
|
118 |
|
119 void CRepositoryCacheManager::EnableCache(TInt aDefaultTimeout, TInt aCacheSize) |
|
120 { |
|
121 if (aDefaultTimeout>0) |
|
122 { |
|
123 iDefaultTimeout = aDefaultTimeout; |
|
124 } |
|
125 if (aCacheSize>0) |
|
126 { |
|
127 iCacheSize = aCacheSize; |
|
128 } |
|
129 |
|
130 EnableCache(); |
|
131 } |
|
132 |
|
133 void CRepositoryCacheManager::EnableCache() |
|
134 { |
|
135 // If disabled, enable |
|
136 if (!iEnabled) |
|
137 { |
|
138 iEnabled = ETrue; |
|
139 __CENTREP_TRACE2("CENTREP: Cache Enabled. Size:%d Default Timeout:%d", iCacheSize, iDefaultTimeout.Int()); |
|
140 } |
|
141 } |
|
142 |
|
143 void CRepositoryCacheManager::DisableCache(TBool aFullFlush) |
|
144 { |
|
145 // If enabled, disable |
|
146 if (iEnabled) |
|
147 { |
|
148 // cancel any outstanding timer |
|
149 Cancel(); |
|
150 |
|
151 FlushCache(aFullFlush); |
|
152 |
|
153 iEnabled = EFalse; |
|
154 __CENTREP_TRACE("CENTREP: Cache Disabled"); |
|
155 } |
|
156 } |
|
157 |
|
158 void CRepositoryCacheManager::RescheduleTimer(const TTime& aTimeInUTC) |
|
159 { |
|
160 |
|
161 TTime now; |
|
162 now.UniversalTime(); |
|
163 |
|
164 //Get the 64bit time interval between now and the cache timeout |
|
165 TTimeIntervalMicroSeconds interval64 = aTimeInUTC.MicroSecondsFrom(now); |
|
166 TTimeIntervalMicroSeconds32 interval32(iDefaultTimeout); |
|
167 |
|
168 //If the interval is positive, i.e. the timeout is in the future, convert |
|
169 //this interval to a 32 bit value, otherwise use the default timeout |
|
170 if(interval64 > 0) |
|
171 { |
|
172 //If the interval value is less than the maximum 32 bit value cast |
|
173 //interval to 32 bit value, otherwise the interval is too large for |
|
174 //a 32 bit value so just set the interval to the max 32 bit value |
|
175 const TInt64 KMax32BitValue(KMaxTInt32); |
|
176 interval32 = (interval64 <= KMax32BitValue) ? |
|
177 static_cast<TTimeIntervalMicroSeconds32>(interval64.Int64()): KMaxTInt32; |
|
178 } |
|
179 |
|
180 //Reschedule the timer |
|
181 After(interval32); |
|
182 |
|
183 } |
|
184 |
|
185 void CRepositoryCacheManager::RemoveIdleRepository(CSharedRepository* aRepository) |
|
186 { |
|
187 if (iEnabled) |
|
188 { |
|
189 TInt i; |
|
190 TInt count=iIdleRepositories.Count(); |
|
191 for(i=count-1; i>=0; i--) |
|
192 { |
|
193 if(iIdleRepositories[i].iSharedRepository==aRepository) |
|
194 { |
|
195 break; |
|
196 } |
|
197 } |
|
198 |
|
199 // Idle repository might not be found in the list if multiple clients try to open the same |
|
200 // repository at the same time. First client will remove it and second one will not find it |
|
201 if(i>=0) |
|
202 { |
|
203 __CENTREP_TRACE1("CENTREP: Cache Hit when opening repository %x", aRepository->Uid().iUid); |
|
204 |
|
205 iTotalCacheUsage -= iIdleRepositories[i].iSharedRepository->Size(); |
|
206 iIdleRepositories.Remove(i); |
|
207 |
|
208 // If this was the first repository on the list, it means its timer is still ticking. |
|
209 // We have to stop it and ... |
|
210 if (i==0) |
|
211 { |
|
212 Cancel(); |
|
213 // if there's still other repositories in the list, reschedule the timer with the |
|
214 // new top-of-the-list |
|
215 if (iIdleRepositories.Count()) |
|
216 { |
|
217 RescheduleTimer(iIdleRepositories[0].iCacheTime); |
|
218 } |
|
219 } |
|
220 } |
|
221 else |
|
222 { |
|
223 __CENTREP_TRACE1("CENTREP: Cache Miss when opening repository %x", aRepository->Uid().iUid); |
|
224 } |
|
225 } |
|
226 } |
|
227 |
|
228 #ifdef CACHE_OOM_TESTABILITY |
|
229 // This code is only for tesing and doesn't go into MCL |
|
230 // Hence hide the leave in a macro instead of making StartEvictionL |
|
231 #define TEST_CODE_LEAVE(x) User::Leave(x) |
|
232 #endif |
|
233 |
|
234 TBool CRepositoryCacheManager::StartEviction(CSharedRepository*& aRepository) |
|
235 { |
|
236 // find the item in the cache and remove it if it exists to reset the timer |
|
237 RemoveIdleRepository(aRepository); |
|
238 |
|
239 TInt64 lastTop = 0; |
|
240 |
|
241 if (iIdleRepositories.Count()) |
|
242 { |
|
243 lastTop = iIdleRepositories[0].iCacheTime.Int64(); |
|
244 } |
|
245 |
|
246 // Execute the forced eviction algorithm only if it will make sense |
|
247 // The eviction makes sense if: |
|
248 // - there's anything in the cache to evict |
|
249 // - the repository we're trying to cache can fit in the cache after evictions |
|
250 if (iIdleRepositories.Count() && (aRepository->Size() < iCacheSize)) |
|
251 { |
|
252 // Check to see if current cache size + the current repository size is overshooting the limit |
|
253 if (iTotalCacheUsage + aRepository->Size() > iCacheSize) |
|
254 { |
|
255 // Forced eviction |
|
256 __CENTREP_TRACE3("CENTREP: Cache Size Exceeded: Current(%d)+Size(%d)>Cache(%d)", iTotalCacheUsage, aRepository->Size(), iCacheSize); |
|
257 |
|
258 // Sort in the forced eviction order |
|
259 TLinearOrder<TRepositoryCacheInfo> forcedSortOrder(CRepositoryCacheManager::ForcedEvictionSortOrder); |
|
260 iIdleRepositories.Sort(forcedSortOrder); |
|
261 |
|
262 // Evict one by one until there's enough cache space or we run out of idle reps |
|
263 do |
|
264 { |
|
265 __CENTREP_TRACE1("CENTREP: Forced Eviction of repository %x", iIdleRepositories[0].iSharedRepository->Uid().iUid); |
|
266 iTotalCacheUsage -= iIdleRepositories[0].iSharedRepository->Size(); |
|
267 Evict(0); |
|
268 iIdleRepositories.Remove(0); |
|
269 } while (iIdleRepositories.Count() && (iTotalCacheUsage + aRepository->Size() > iCacheSize)); |
|
270 |
|
271 #ifdef CENTREP_TRACE |
|
272 if (!iIdleRepositories.Count()) |
|
273 { |
|
274 __CENTREP_TRACE("CENREP: Cache Emptied by Forced Eviction"); |
|
275 } |
|
276 #endif |
|
277 // Re-sort to timer order for normal operation |
|
278 TLinearOrder<TRepositoryCacheInfo> timerSortOrder(CRepositoryCacheManager::TimerEvictionSortOrder); |
|
279 iIdleRepositories.Sort(timerSortOrder); |
|
280 }; |
|
281 } |
|
282 |
|
283 // See if there's enough space now |
|
284 if (iTotalCacheUsage + aRepository->Size() > iCacheSize) |
|
285 { |
|
286 return EFalse; |
|
287 } |
|
288 |
|
289 // Create new item for the cache and insert it in the list |
|
290 TRepositoryCacheInfo repInfo; |
|
291 |
|
292 repInfo.iCacheTime.UniversalTime(); |
|
293 repInfo.iCacheTime += TTimeIntervalMicroSeconds32(iDefaultTimeout); |
|
294 repInfo.iSharedRepository = aRepository; |
|
295 |
|
296 TLinearOrder<TRepositoryCacheInfo> timerSortOrder(CRepositoryCacheManager::TimerEvictionSortOrder); |
|
297 // With the same timeout value assigned to all repositories, no two repositories can have the same |
|
298 // timeout theoretically, so InsertInOrder is sufficient. But in practice, because of the poor |
|
299 // resolution of the NTickCount() function used by TTime::UniversalTime(), InsertInOrderAllowRepeats |
|
300 // should be used instead of InsertInOrder to allow for duplicate timer values caused by two |
|
301 // repositories cached in quick succession (<1ms) |
|
302 TInt err = iIdleRepositories.InsertInOrderAllowRepeats(repInfo, timerSortOrder); |
|
303 #ifdef CACHE_OOM_TESTABILITY |
|
304 // This code is only for tesing and doesn't go into MCL |
|
305 if (err == KErrNoMemory) |
|
306 { |
|
307 TServerResources::iObserver->RemoveOpenRepository(aRepository); |
|
308 aRepository = NULL; |
|
309 // Should Leave here for the OOM tests to successfully complete. |
|
310 TEST_CODE_LEAVE(err); |
|
311 } |
|
312 #endif |
|
313 if (err!=KErrNone) |
|
314 { |
|
315 return EFalse; |
|
316 } |
|
317 |
|
318 iTotalCacheUsage += repInfo.iSharedRepository->Size(); |
|
319 |
|
320 // Only reset timer if necessary. This operation takes time and doing it every time reduces performance considerably |
|
321 if (lastTop != iIdleRepositories[0].iCacheTime.Int64()) |
|
322 { |
|
323 // reset timer to the new top-of-the-list |
|
324 Cancel(); |
|
325 RescheduleTimer(iIdleRepositories[0].iCacheTime); |
|
326 } |
|
327 |
|
328 return ETrue; |
|
329 } |
|
330 |
|
331 void CRepositoryCacheManager::FlushCache(TBool aFullFlush) |
|
332 { |
|
333 // iterate through idle repositories (loaded in memory, scheduled to be evicted) |
|
334 TInt idleRepCount = iIdleRepositories.Count(); |
|
335 for(TInt repCount = idleRepCount - 1; repCount >= 0 ; repCount--) |
|
336 { |
|
337 // check if there are any observers listening (to see if any client is connected to this repository) |
|
338 if (aFullFlush || (TServerResources::iObserver->FindConnectedRepository(iIdleRepositories[repCount].iSharedRepository->Uid())==KErrNotFound)) |
|
339 { |
|
340 // if the client has already been disconnected, unload from memory |
|
341 Evict(repCount); |
|
342 } |
|
343 } |
|
344 // this whole iteration and search above can be replaced by a simple reference counter variable check, |
|
345 // if the server is redesigned using a resource manager type pattern with CSharedRepository object as a resource |
|
346 |
|
347 // empty the list |
|
348 iIdleRepositories.Reset(); |
|
349 |
|
350 iTotalCacheUsage = 0; |
|
351 __CENTREP_TRACE1("CENTREP: Cache Flush: %d repositories flushed", idleRepCount); |
|
352 } |
|
353 |
|
354 // Evict removes items from iOpenRepositories list, not from iIdleRepositories list |
|
355 void CRepositoryCacheManager::Evict(TInt aIdleRepIndex) |
|
356 { |
|
357 // find,remove and delete the idle repositories' pointers in the open repositories list |
|
358 TServerResources::iObserver->RemoveOpenRepository(iIdleRepositories[aIdleRepIndex].iSharedRepository); |
|
359 } |
|
360 |
|
361 void CRepositoryCacheManager::RunL() |
|
362 { |
|
363 TTime now; |
|
364 |
|
365 now.UniversalTime(); |
|
366 |
|
367 TInt count = iIdleRepositories.Count(); |
|
368 |
|
369 // repositories that are involved in active transactions are not idle. |
|
370 // this checks to make sure that we're not trying to reclaim memory that |
|
371 // is actually still currently in use. |
|
372 |
|
373 for (TInt i = 0;i < count;i++) |
|
374 { |
|
375 if (iIdleRepositories[i].iCacheTime > now) |
|
376 { |
|
377 break; |
|
378 } |
|
379 |
|
380 if (iIdleRepositories[i].iSharedRepository->IsTransactionActive()) |
|
381 { |
|
382 __CENTREP_TRACE1("CRepositoryCacheManager::RunL - rescheduling UID 0x%x, in active transaction", |
|
383 iIdleRepositories[i].iSharedRepository->Uid().iUid); |
|
384 StartEviction(iIdleRepositories[i].iSharedRepository); |
|
385 return; |
|
386 } |
|
387 } |
|
388 |
|
389 |
|
390 // Try to evict all the repositories which have expired. There might be more than one repository |
|
391 // destined to expire at the same time, or there are more than one repository with expiry times |
|
392 // between the scheduled expiry time and now (which theoretically should have been the same, but maybe |
|
393 // because of other procesor activity, the timer Active Object just got late a bit) |
|
394 while((iIdleRepositories.Count()) && (iIdleRepositories[0].iCacheTime<=now)) |
|
395 { |
|
396 __CENTREP_TRACE1("CENTREP: Normal Eviction of repository %x", iIdleRepositories[0].iSharedRepository->Uid().iUid); |
|
397 // Always remove from the top of the sorted list |
|
398 iTotalCacheUsage -= iIdleRepositories[0].iSharedRepository->Size(); |
|
399 Evict(0); |
|
400 iIdleRepositories.Remove(0); |
|
401 }; |
|
402 |
|
403 // reschedule to run again at the expiry date of next repository on the list, if any |
|
404 if (iIdleRepositories.Count()) |
|
405 { |
|
406 RescheduleTimer(iIdleRepositories[0].iCacheTime); |
|
407 } |
|
408 else |
|
409 { |
|
410 __CENTREP_TRACE("CENTREP: Cache Empty/Timer Disabled"); |
|
411 } |
|
412 } |
|
413 |
|
414 TInt CRepositoryCacheManager::ForcedEvictionSortOrder(const TRepositoryCacheInfo &aRepository1, const TRepositoryCacheInfo &aRepository2) |
|
415 { |
|
416 /* |
|
417 The code in the comments below is the original simple-to-read version of the algebraically |
|
418 simplified production code. |
|
419 |
|
420 TTime now; |
|
421 |
|
422 now.UniversalTime(); |
|
423 |
|
424 // we calculate the ages of the repositories by taking the difference between now and when |
|
425 // they were last became idle. When refactoring, the calculation of the absolute ages will be |
|
426 // eleminated and the age difference between the repositories will be used in the formula instead |
|
427 |
|
428 TTimeIntervalMicroSeconds age1 = now.MicroSecondsFrom(aRepository1.iCacheTime); |
|
429 TTimeIntervalMicroSeconds age2 = now.MicroSecondsFrom(aRepository2.iCacheTime); |
|
430 |
|
431 // then divide the resulting numbers by conversion constant to get a number in a compatible unit |
|
432 // to the size. This operation reduces the microsecond-based values to having an approx. max |
|
433 // of 100K units |
|
434 |
|
435 TInt t1 = age1.Int64()/KTimeoutToSizeConversion; |
|
436 TInt t2 = age2.Int64()/KTimeoutToSizeConversion; |
|
437 |
|
438 // the resulting normalized time difference values are added to the size of the repository |
|
439 // resulting in an implicit %50 weight in the overall importance value |
|
440 // An approx. maximum size of a repository is assumed to be around 100K |
|
441 |
|
442 TInt importance1 = t1+aRepository1.iSharedRepository->Size(); |
|
443 TInt importance2 = t2+aRepository2.iSharedRepository->Size(); |
|
444 |
|
445 // the difference between the importances of the repositories determine the sorting order |
|
446 |
|
447 return static_cast<TInt>(importance1-importance2); |
|
448 */ |
|
449 // after refactoring, the resulting formula is this one |
|
450 return static_cast<TInt>(((aRepository1.iCacheTime.Int64()-aRepository2.iCacheTime.Int64())/KTimeoutToSizeConversion)+(aRepository1.iSharedRepository->Size()-aRepository2.iSharedRepository->Size())); |
|
451 } |
|
452 |
|
453 TInt CRepositoryCacheManager::TimerEvictionSortOrder(const TRepositoryCacheInfo &aRepository1, const TRepositoryCacheInfo &aRepository2) |
|
454 { |
|
455 return static_cast<TInt>(aRepository1.iCacheTime.Int64()-aRepository2.iCacheTime.Int64()); |
|
456 } |