|
1 // Copyright (c) 2006-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 the License "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 // f32\sfile\sf_cache_man.cpp |
|
15 // |
|
16 // |
|
17 |
|
18 /** |
|
19 @file |
|
20 @internalTechnology |
|
21 */ |
|
22 |
|
23 |
|
24 #include <e32std.h> |
|
25 #include <e32std_private.h> |
|
26 #include "sf_std.h" |
|
27 #include <e32uid.h> |
|
28 #include <e32wins.h> |
|
29 #include <f32file.h> |
|
30 #include <hal.h> |
|
31 #include "sf_cache_man.h" |
|
32 #include "sf_cache_client.h" |
|
33 #include "sf_file_cache.h" |
|
34 #include "sf_file_cache_defs.h" |
|
35 |
|
36 //#undef __SIMULATE_LOCK_FAILURES__ |
|
37 //#define __RAMDOM_LOCK_FAILURES__ |
|
38 //#define __PSEUDO_RANDOM_FAILURES__ |
|
39 //#define __LOCK_ENTIRE_CACHELINE__ |
|
40 |
|
41 #ifdef __RAMDOM_LOCK_FAILURES__ |
|
42 #include <e32math.h> |
|
43 #endif |
|
44 |
|
45 #define CACHE_NUM_TO_ADDR(n) (iBase + (n << iCacheLineSizeLog2)) |
|
46 #define ADDR_TO_CACHELINE_ADDR(a) ((((a - iBase) >> iCacheLineSizeLog2) << iCacheLineSizeLog2) + iBase) |
|
47 #define ADDR_TO_INDEX(a) ((a - iBase) >> iCacheLineSizeLog2) |
|
48 |
|
49 #if defined(_DEBUG) |
|
50 #define __CACHELINE_INVARIANT(aCacheLine) \ |
|
51 __ASSERT_DEBUG( \ |
|
52 aCacheLine.iDirtySegments <= aCacheLine.iFilledSegments && \ |
|
53 aCacheLine.iFilledSegments <= aCacheLine.iAllocatedSegments, \ |
|
54 Fault(EInvalidCacheLine)); |
|
55 |
|
56 #else |
|
57 #define __CACHELINE_INVARIANT(aCacheLine) |
|
58 #endif |
|
59 |
|
60 const TInt KSegmentSize = 4096; |
|
61 const TInt KSegmentSizeLog2 = 12; |
|
62 const TInt64 KSegmentSizeMask = MAKE_TINT64(0x7FFFFFFF,0xFFFFF000); |
|
63 |
|
64 const TInt KCacheLineSizeLog2 = 17; // 128K |
|
65 const TInt KCacheLineSize = (1 << KCacheLineSizeLog2 ); |
|
66 const TInt KCacheLineSizeInSegments = (KCacheLineSize >> KSegmentSizeLog2); |
|
67 |
|
68 CCacheManager* CCacheManagerFactory::iCacheManager = NULL; |
|
69 |
|
70 |
|
71 LOCAL_C void Fault(TCacheManagerFault aFault) |
|
72 // |
|
73 // Report a fault in the cache manager |
|
74 // |
|
75 { |
|
76 User::Panic(_L("FSCACHEMAN"), aFault); |
|
77 } |
|
78 |
|
79 |
|
80 /* |
|
81 Indicates if a number passed in is a power of two |
|
82 |
|
83 @param aNum number to be tested |
|
84 @return Flag to indicate the result of the test |
|
85 */ |
|
86 LOCAL_C TBool IsPowerOfTwo(TInt aNum) |
|
87 { |
|
88 return (aNum != 0 && (aNum & -aNum) == aNum); |
|
89 } |
|
90 |
|
91 |
|
92 //******************** |
|
93 // CCacheManagerFactory |
|
94 //******************** |
|
95 void CCacheManagerFactory::CreateL() |
|
96 { |
|
97 __ASSERT_ALWAYS(iCacheManager == NULL, Fault(ECacheAlreadyCreated)); |
|
98 |
|
99 if (TGlobalFileCacheSettings::Enabled()) |
|
100 { |
|
101 iCacheManager = CCacheManager::NewCacheL(TGlobalFileCacheSettings::CacheSize()); |
|
102 } |
|
103 else |
|
104 { |
|
105 Destroy(); |
|
106 } |
|
107 } |
|
108 |
|
109 CCacheManager* CCacheManagerFactory::CacheManager() |
|
110 { |
|
111 return iCacheManager; |
|
112 } |
|
113 |
|
114 TInt CCacheManagerFactory::Destroy() |
|
115 { |
|
116 delete iCacheManager; |
|
117 iCacheManager = NULL; |
|
118 return KErrNone; |
|
119 } |
|
120 |
|
121 //******************** |
|
122 // CCacheManager |
|
123 //******************** |
|
124 CCacheManager* CCacheManager::NewCacheL(TInt aCacheSize) |
|
125 { |
|
126 CCacheManager* cacheManager = new (ELeave) CCacheManager(aCacheSize); |
|
127 |
|
128 CleanupStack::PushL(cacheManager); |
|
129 cacheManager->ConstructL(); |
|
130 CleanupStack::Pop(1, cacheManager); |
|
131 |
|
132 return cacheManager; |
|
133 } |
|
134 |
|
135 CCacheManager::~CCacheManager() |
|
136 { |
|
137 CacheLock(); |
|
138 |
|
139 iFreeQueue.Close(); |
|
140 iUsedQueue.Close(); |
|
141 |
|
142 delete [] iCacheLines; |
|
143 |
|
144 CacheUnlock(); |
|
145 iLock.Close(); |
|
146 |
|
147 iChunk.Close(); |
|
148 } |
|
149 |
|
150 |
|
151 CCacheManager::CCacheManager(TUint aCacheSize) |
|
152 { |
|
153 // Get the page size |
|
154 TInt pageSize; |
|
155 TInt r = HAL::Get(HAL::EMemoryPageSize, pageSize); |
|
156 if (r != KErrNone) |
|
157 pageSize = KSegmentSize; |
|
158 __ASSERT_ALWAYS(IsPowerOfTwo(pageSize), Fault(EIllegalPageSize)); |
|
159 |
|
160 // For the time being we only a support page size of 4K and we assume |
|
161 // that page size = segment size, since the extra overhead of supporting |
|
162 // variable page sizes now is non-trivial. |
|
163 // |
|
164 // If a processor with a different page size needs to be supported |
|
165 // in the future, then we need to either rework this code to be able to |
|
166 // devide up pages into smaller segments or analyze the impact of having |
|
167 // a different page size on performance. |
|
168 __ASSERT_ALWAYS(pageSize == KSegmentSize, Fault(EIllegalPageSize)); |
|
169 |
|
170 iCacheLineSize = KCacheLineSize; |
|
171 iCacheLineSizeLog2 = KCacheLineSizeLog2; |
|
172 |
|
173 iCacheSize = aCacheSize; |
|
174 |
|
175 iSegmentsPerCacheLine = 1 << (iCacheLineSizeLog2 - KSegmentSizeLog2); |
|
176 |
|
177 iMaxLockedSegments = TGlobalFileCacheSettings::MaxLockedSize() >> KSegmentSizeLog2; |
|
178 |
|
179 #ifdef __SIMULATE_LOCK_FAILURES__ |
|
180 iSimulateLockFailureMode = ETrue; |
|
181 #endif |
|
182 } |
|
183 |
|
184 void CCacheManager::ConstructL() |
|
185 { |
|
186 |
|
187 // calculate the low-memory threshold below which we fail any attempt to allocate memory |
|
188 TMemoryInfoV1Buf meminfo; |
|
189 TInt r = UserHal::MemoryInfo(meminfo); |
|
190 __ASSERT_ALWAYS(r==KErrNone,Fault(EMemoryInfoFailed)); |
|
191 |
|
192 iLowMemoryThreshold = (meminfo().iTotalRamInBytes * TGlobalFileCacheSettings::LowMemoryThreshold()) / 100; |
|
193 __CACHE_PRINT4(_L("CACHEMAN: totalRAM %d freeRAM %d KDefaultLowMemoryThresholdPercent %d iLowMemoryThreshold %d"), |
|
194 meminfo().iTotalRamInBytes, meminfo().iFreeRamInBytes, KDefaultLowMemoryThreshold, iLowMemoryThreshold); |
|
195 __CACHE_PRINT1(_L("CACHEMAN: iCacheSize %d"), iCacheSize); |
|
196 TChunkCreateInfo createInfo; |
|
197 createInfo.SetCache(iCacheSize); |
|
198 createInfo.SetOwner(EOwnerProcess); |
|
199 r = iChunk.Create(createInfo); |
|
200 User::LeaveIfError(r); |
|
201 |
|
202 |
|
203 TInt mm = UserSvr::HalFunction(EHalGroupKernel, EKernelHalMemModelInfo, 0, 0) & EMemModelTypeMask; |
|
204 if (mm < EMemModelTypeFlexible) |
|
205 { |
|
206 // On memory models before flexible, File System has to register chunks that will be DMA-ed. |
|
207 // On flexible memory model, local media is using new physical pinning Kernel interface that |
|
208 // doesn't require registration of the chunk. |
|
209 UserSvr::RegisterTrustedChunk(iChunk.Handle()); |
|
210 } |
|
211 |
|
212 |
|
213 __ASSERT_ALWAYS(iCacheSize > KSegmentSize, Fault(EIllegalCacheSize)); |
|
214 |
|
215 r = iLock.CreateLocal(); |
|
216 User::LeaveIfError(r); |
|
217 |
|
218 iNumOfCacheLines = iCacheSize >> iCacheLineSizeLog2; |
|
219 iBase = Base(); |
|
220 |
|
221 iCacheLines = new (ELeave) TCacheLine[iNumOfCacheLines]; |
|
222 |
|
223 for (TInt n=0; n<iNumOfCacheLines; n++) |
|
224 { |
|
225 // coverity[var_decl] |
|
226 TCacheLine cacheLine; |
|
227 cacheLine.iAddr = CACHE_NUM_TO_ADDR(n); |
|
228 cacheLine.iAllocatedSegments = 0; |
|
229 cacheLine.iFilledSegments = 0; |
|
230 cacheLine.iDirtySegments = 0; |
|
231 cacheLine.iLockCount = 0; |
|
232 cacheLine.iLockedSegmentStart = 0; |
|
233 cacheLine.iLockedSegmentCount = 0; |
|
234 cacheLine.iClient = NULL; |
|
235 // coverity[uninit_use] |
|
236 iCacheLines[n] = cacheLine; |
|
237 |
|
238 r = iFreeQueue.Append(&iCacheLines[n]); |
|
239 User::LeaveIfError(r); |
|
240 } |
|
241 |
|
242 } |
|
243 |
|
244 CCacheClient* CCacheManager::CreateClientL() |
|
245 { |
|
246 CCacheClient* client = CCacheClient::NewL(*this); |
|
247 return client; |
|
248 } |
|
249 |
|
250 void CCacheManager::RegisterClient(CCacheClient& /*aClient*/) |
|
251 { |
|
252 #if defined(_DEBUG) || defined(_DEBUG_RELEASE) |
|
253 iStats.iFileCount++; |
|
254 #endif |
|
255 } |
|
256 |
|
257 void CCacheManager::DeregisterClient(CCacheClient& /*aClient*/) |
|
258 { |
|
259 #if defined(_DEBUG) || defined(_DEBUG_RELEASE) |
|
260 iStats.iFileCount--; |
|
261 #endif |
|
262 } |
|
263 |
|
264 |
|
265 void CCacheManager::FreeMemoryChanged(TBool aMemoryLow) |
|
266 { |
|
267 iMemoryLow = aMemoryLow; |
|
268 } |
|
269 |
|
270 TBool CCacheManager::TooManyLockedSegments() |
|
271 { |
|
272 return (iLockedSegmentCount >= iMaxLockedSegments)?(TBool)ETrue:(TBool)EFalse; |
|
273 } |
|
274 |
|
275 TInt CCacheManager::SegmentSize() |
|
276 { |
|
277 return KSegmentSize; |
|
278 } |
|
279 |
|
280 TInt CCacheManager::SegmentSizeLog2() |
|
281 { |
|
282 return KSegmentSizeLog2; |
|
283 } |
|
284 |
|
285 TInt64 CCacheManager::SegmentSizeMask() |
|
286 { |
|
287 return KSegmentSizeMask; |
|
288 } |
|
289 |
|
290 TInt CCacheManager::CacheLineSize() |
|
291 { |
|
292 return KCacheLineSize; |
|
293 } |
|
294 |
|
295 TInt CCacheManager::CacheLineSizeInSegments() |
|
296 { |
|
297 return KCacheLineSizeInSegments; |
|
298 } |
|
299 |
|
300 TInt CCacheManager::CacheLineSizeLog2() |
|
301 { |
|
302 return KCacheLineSizeLog2; |
|
303 } |
|
304 |
|
305 |
|
306 #if defined(_DEBUG) || defined(_DEBUG_RELEASE) |
|
307 TFileCacheStats& CCacheManager::Stats() |
|
308 { |
|
309 iStats.iFreeCount = iFreeQueue.Count(); |
|
310 iStats.iUsedCount = iUsedQueue.Count(); |
|
311 iStats.iLockedSegmentCount = iLockedSegmentCount; |
|
312 iStats.iFilesOnClosedQueue = TClosedFileUtils::Count(); |
|
313 __ASSERT_DEBUG(iStats.iFreeCount >= 0, Fault(EInvalidStats)); |
|
314 __ASSERT_DEBUG(iStats.iUsedCount >= 0, Fault(EInvalidStats)); |
|
315 __ASSERT_DEBUG(iStats.iLockedSegmentCount >= 0, Fault(EInvalidStats)); |
|
316 __ASSERT_DEBUG(iStats.iFilesOnClosedQueue >= 0, Fault(EInvalidStats)); |
|
317 return iStats; |
|
318 } |
|
319 #endif // #if defined(_DEBUG) || defined(_DEBUG_RELEASE) |
|
320 |
|
321 |
|
322 |
|
323 void CCacheManager::CacheLock() |
|
324 { |
|
325 iLock.Wait(); |
|
326 |
|
327 #if defined(_DEBUG) || defined(_DEBUG_RELEASE) |
|
328 iManagerLocked = ETrue; |
|
329 #endif |
|
330 } |
|
331 |
|
332 void CCacheManager::CacheUnlock() |
|
333 { |
|
334 #if defined(_DEBUG) || defined(_DEBUG_RELEASE) |
|
335 iManagerLocked = EFalse; |
|
336 #endif |
|
337 iLock.Signal(); |
|
338 } |
|
339 |
|
340 |
|
341 TInt CCacheManager::AllocateAndLockCacheLine(CCacheClient* aClient, TInt64 aPos, const TCacheLine*& aCacheLine, TInt aSegmentCount) |
|
342 { |
|
343 __ASSERT_DEBUG(aSegmentCount <= iSegmentsPerCacheLine, Fault(EInvalidAllocCount)); |
|
344 |
|
345 // check for low system memory |
|
346 TMemoryInfoV1Buf meminfo; |
|
347 TInt r = UserHal::MemoryInfo(meminfo); |
|
348 __ASSERT_ALWAYS(r==KErrNone,Fault(EMemoryInfoFailed)); |
|
349 if (iMemoryLow || (meminfo().iFreeRamInBytes < iLowMemoryThreshold)) |
|
350 { |
|
351 __CACHE_PRINT(_L("CACHEMAN: free RAM below threshold !!!")); |
|
352 return KErrNoMemory; |
|
353 } |
|
354 |
|
355 CacheLock(); |
|
356 |
|
357 __CACHE_PRINT1(_L("CACHEMAN: Allocate %d segments, Lock %d"), aSegmentCount); |
|
358 __CACHE_PRINT2(_L("CACHEMAN: iFreeQueue %d iUsedQueue %d"), iFreeQueue.Count(), iUsedQueue.Count()); |
|
359 |
|
360 #ifdef __SIMULATE_LOCK_FAILURES__ |
|
361 if (SimulatedFailure(iAllocFailureCount)) |
|
362 { |
|
363 __CACHE_PRINT(_L("CACHEMAN: simulating allocation failure")); |
|
364 CacheUnlock(); |
|
365 return KErrNoMemory; |
|
366 } |
|
367 #endif |
|
368 |
|
369 // any cachelines free ? |
|
370 TInt freeCacheLines = iFreeQueue.Count(); |
|
371 TCacheLine* cacheLine = NULL; |
|
372 if (freeCacheLines == 0 && !StealCacheLine(aClient)) |
|
373 { |
|
374 CacheUnlock(); |
|
375 return KErrNotFound; |
|
376 } |
|
377 |
|
378 cacheLine = iFreeQueue[0]; |
|
379 |
|
380 __CACHELINE_INVARIANT((*cacheLine)); |
|
381 __ASSERT_DEBUG( cacheLine->iAllocatedSegments == 0, Fault(EInvalidAllocCount)); |
|
382 __ASSERT_DEBUG( cacheLine->iFilledSegments == 0, Fault(EInvalidFillCount)); |
|
383 __ASSERT_DEBUG( cacheLine->iDirtySegments == 0, Fault(EInvalidFillCount)); |
|
384 __ASSERT_DEBUG( cacheLine->iLockCount == 0, Fault(EInvalidLockCount)); |
|
385 __ASSERT_DEBUG( cacheLine->iLockedSegmentStart == 0, Fault(EInvalidLockedPageStart)); |
|
386 __ASSERT_DEBUG( cacheLine->iLockedSegmentCount == 0, Fault(EInvalidLockedPageCount)); |
|
387 __ASSERT_DEBUG( cacheLine->iClient == NULL, Fault(EInvalidClient)); |
|
388 |
|
389 TUint8* addr = cacheLine->iAddr; |
|
390 |
|
391 // Commit it |
|
392 r = Commit(addr, aSegmentCount); |
|
393 if (r != KErrNone) |
|
394 { |
|
395 CacheUnlock(); |
|
396 return r; |
|
397 } |
|
398 cacheLine->iAllocatedSegments = (TUint8) (aSegmentCount); |
|
399 cacheLine->iLockedSegmentStart = 0; |
|
400 cacheLine->iLockedSegmentCount = (TUint8) aSegmentCount; |
|
401 |
|
402 // Add to used queue |
|
403 r = iUsedQueue.InsertInAddressOrder(cacheLine); |
|
404 if (r != KErrNone) |
|
405 { |
|
406 Decommit(addr, aSegmentCount); |
|
407 CacheUnlock(); |
|
408 return r; |
|
409 } |
|
410 |
|
411 cacheLine->iClient = aClient; |
|
412 cacheLine->iPos = aPos; |
|
413 |
|
414 // Remove from free queue |
|
415 iFreeQueue.Remove(0); |
|
416 |
|
417 // RChunk will lock segments initially unless explicitly unlocked |
|
418 |
|
419 cacheLine->iLockCount++; |
|
420 |
|
421 __CACHE_PRINT2(_L("CACHEMAN: LockCount for %08X is %d"), cacheLine->iAddr, cacheLine->iLockCount); |
|
422 |
|
423 CacheUnlock(); |
|
424 |
|
425 aCacheLine = cacheLine; |
|
426 return r; |
|
427 |
|
428 } |
|
429 |
|
430 /** |
|
431 CCacheManager::ExtendCacheLine() |
|
432 |
|
433 Attempts to extend the length of an existing cacheline by committing extra segments |
|
434 The cacheline must be owned and locked already. |
|
435 |
|
436 @param aCacheLine A reference to a locked, owned cacheline |
|
437 @param aSegmentCount The new length of the cacheline (including the existing segments) |
|
438 This must be greater then the existing length. |
|
439 |
|
440 @return KErrNone if successful |
|
441 or other system wide error code. |
|
442 */ |
|
443 TInt CCacheManager::ExtendCacheLine(CCacheClient* aClient, const TCacheLine& aCacheLine, TInt aSegmentCount) |
|
444 { |
|
445 CacheLock(); |
|
446 |
|
447 __ASSERT_DEBUG(aSegmentCount > 0, Fault(EInvalidSegmentCount)); |
|
448 |
|
449 TCacheLine& cacheLine = const_cast<TCacheLine&>(aCacheLine); |
|
450 __CACHELINE_INVARIANT((cacheLine)); |
|
451 __ASSERT_DEBUG(aSegmentCount <= iSegmentsPerCacheLine, Fault(EInvalidSegmentCount)); |
|
452 __ASSERT_DEBUG(aSegmentCount > cacheLine.iAllocatedSegments, Fault(EInvalidSegmentCount)); |
|
453 __ASSERT_DEBUG(cacheLine.iLockCount > 0, Fault(EInvalidLockCount)); // must be locked already |
|
454 __ASSERT_ALWAYS(cacheLine.iClient == aClient, Fault(EExtendingUnownedCacheline)); |
|
455 |
|
456 // ensure all pages in cachline are locked |
|
457 __ASSERT_DEBUG(cacheLine.iLockedSegmentStart == 0, Fault(EInvalidLockRange)); |
|
458 __ASSERT_DEBUG(cacheLine.iLockedSegmentCount == cacheLine.iAllocatedSegments, Fault(EInvalidLockRange)); |
|
459 |
|
460 __CACHE_PRINT2(_L("CACHEMAN: ExtendCacheLine(%08X, %d)"), cacheLine.iAddr, aSegmentCount); |
|
461 |
|
462 // Commit the new segments |
|
463 TUint8* addrNewSegment = cacheLine.iAddr + (cacheLine.iAllocatedSegments << KSegmentSizeLog2); |
|
464 TInt segmentCountNew = aSegmentCount - cacheLine.iAllocatedSegments; |
|
465 |
|
466 TInt r = Commit(addrNewSegment, segmentCountNew); |
|
467 if (r == KErrNone) |
|
468 { |
|
469 cacheLine.iAllocatedSegments = cacheLine.iLockedSegmentCount = (TUint8) aSegmentCount; |
|
470 } |
|
471 |
|
472 |
|
473 CacheUnlock(); |
|
474 return r; |
|
475 } |
|
476 |
|
477 /** |
|
478 CCacheManager::ReUseCacheLine() |
|
479 |
|
480 Attempts to lock and then extend or shrink an already owned cacheline, ready for re-use. |
|
481 If successful the cacheline is returned locked and all segments are marked as empty. |
|
482 The cacheline must initially be unlocked. |
|
483 |
|
484 @param aCacheLine A reference to an unlocked cacheline |
|
485 @param aSegmentCount The new length of the cacheline; |
|
486 this may be greater or less than the existing length. |
|
487 |
|
488 @return KErrNone if successful |
|
489 or other system wide error code. |
|
490 */ |
|
491 TInt CCacheManager::ReAllocateAndLockCacheLine(CCacheClient* aClient, TInt64 aPos, const TCacheLine& aCacheLine, TInt aSegmentCount) |
|
492 { |
|
493 TCacheLine& cacheLine = const_cast<TCacheLine&>(aCacheLine); |
|
494 __CACHELINE_INVARIANT((cacheLine)); |
|
495 |
|
496 __ASSERT_DEBUG(aSegmentCount > 0, Fault(EInvalidSegmentCount)); |
|
497 __ASSERT_DEBUG(aSegmentCount <= iSegmentsPerCacheLine, Fault(EInvalidSegmentCount)); |
|
498 |
|
499 __CACHE_PRINT2(_L("CACHEMAN: ReUseCacheLine(%08X, %d)"), cacheLine.iAddr, aSegmentCount); |
|
500 |
|
501 TInt r; |
|
502 // old cacheline same size or bigger ? |
|
503 if (cacheLine.iAllocatedSegments >= aSegmentCount) |
|
504 { |
|
505 r = LockCacheLine(aClient, aCacheLine, 0, aSegmentCount); |
|
506 if (r!= KErrNone) |
|
507 return r; |
|
508 if (cacheLine.iAllocatedSegments > aSegmentCount) |
|
509 { |
|
510 cacheLine.iFilledSegments = (TUint8) aSegmentCount; |
|
511 RemoveEmptySegments(aClient, aCacheLine); |
|
512 } |
|
513 } |
|
514 // old cacheline smaller |
|
515 else |
|
516 { |
|
517 r = LockCacheLine(aClient, aCacheLine, 0, cacheLine.iAllocatedSegments); |
|
518 if (r != KErrNone) |
|
519 return r; |
|
520 r = ExtendCacheLine(aClient, aCacheLine, aSegmentCount); |
|
521 if (r != KErrNone) |
|
522 { |
|
523 UnlockCacheLine(aClient, aCacheLine); |
|
524 return r; |
|
525 } |
|
526 } |
|
527 cacheLine.iFilledSegments = 0; |
|
528 cacheLine.iPos = aPos; |
|
529 |
|
530 return KErrNone; |
|
531 } |
|
532 |
|
533 |
|
534 /** |
|
535 CCacheManager::LockCacheLine() |
|
536 |
|
537 @return KErrNone on success |
|
538 */ |
|
539 TInt CCacheManager::LockCacheLine( |
|
540 CCacheClient* aClient, |
|
541 const TCacheLine& aCacheLine, |
|
542 TInt aLockedPageStart, |
|
543 TInt aLockedPageCount) |
|
544 { |
|
545 CacheLock(); |
|
546 |
|
547 |
|
548 TCacheLine& cacheLine = const_cast<TCacheLine&>(aCacheLine); |
|
549 __CACHELINE_INVARIANT((cacheLine)); |
|
550 |
|
551 // has the cacheline been stolen ? |
|
552 if (cacheLine.iClient != aClient) |
|
553 { |
|
554 __CACHE_PRINT(_L("CCacheManager::LockCacheLine(), Cacheline Stolen !\n")); |
|
555 CacheUnlock(); |
|
556 return KErrNotFound; |
|
557 } |
|
558 |
|
559 // validate lock range |
|
560 __ASSERT_DEBUG(aLockedPageStart >= 0, Fault(EInvalidLockRange)); |
|
561 __ASSERT_DEBUG(aLockedPageStart + aLockedPageCount <= cacheLine.iAllocatedSegments, Fault(EInvalidLockRange)); |
|
562 __ASSERT_DEBUG(aLockedPageCount <= iSegmentsPerCacheLine, Fault(EInvalidLockRange)); |
|
563 // if already locked, don't allow lock range to grow down or up (to simplify code) |
|
564 __ASSERT_DEBUG(cacheLine.iLockCount == 0 || |
|
565 aLockedPageStart >= cacheLine.iLockedSegmentStart, |
|
566 Fault(EInvalidLockRange)); |
|
567 __ASSERT_DEBUG(cacheLine.iLockCount == 0 || |
|
568 aLockedPageStart + aLockedPageCount <= cacheLine.iLockedSegmentStart + cacheLine.iLockedSegmentCount, |
|
569 Fault(EInvalidLockRange)); |
|
570 |
|
571 __CACHE_PRINT1(_L("CACHEMAN: LockCacheLine(%08X, %d)"), cacheLine.iAddr); |
|
572 |
|
573 if (InUse(aCacheLine)) |
|
574 { |
|
575 __CACHE_PRINT(_L("CCacheManager::LockCacheLine(), Cacheline in use !\n")); |
|
576 CacheUnlock(); |
|
577 return KErrInUse; |
|
578 } |
|
579 |
|
580 TInt lockErr = KErrNone; |
|
581 |
|
582 // increment the lock count |
|
583 // if not already locked, lock requested segments |
|
584 if (cacheLine.iLockCount++ == 0) |
|
585 { |
|
586 #ifdef __LOCK_ENTIRE_CACHELINE__ |
|
587 lockErr = Lock(cacheLine.iAddr, cacheLine.iAllocatedSegments); |
|
588 #else |
|
589 __ASSERT_DEBUG(cacheLine.iDirtySegments == 0, Fault(ELockingAndAlreadyDirty)); |
|
590 lockErr = Lock( cacheLine.iAddr + (aLockedPageStart<<KSegmentSizeLog2), aLockedPageCount); |
|
591 #endif |
|
592 |
|
593 if (lockErr == KErrNone) |
|
594 { |
|
595 cacheLine.iLockedSegmentStart = (TUint8) aLockedPageStart; |
|
596 cacheLine.iLockedSegmentCount = (TUint8) aLockedPageCount; |
|
597 } |
|
598 else // if (lockErr != KErrNone) |
|
599 { |
|
600 __CACHE_PRINT2(_L("CACHEMAN: LockCacheLine(%08X) failure %d"), cacheLine.iAddr, lockErr); |
|
601 cacheLine.iLockCount--; |
|
602 // NB a lock failure implies segment is decomitted |
|
603 FreeCacheLine(cacheLine); |
|
604 } |
|
605 } |
|
606 // if already locked (because cacheline is dirty) ensure lock range |
|
607 // isn't growing (this isn't allowed to keep the code simpler) |
|
608 else |
|
609 { |
|
610 __ASSERT_DEBUG(cacheLine.iLockedSegmentStart == 0, Fault(EInvalidLockRange)); |
|
611 __ASSERT_DEBUG(cacheLine.iLockedSegmentCount >= aLockedPageStart + aLockedPageCount, Fault(EInvalidLockRange)); |
|
612 } |
|
613 |
|
614 CacheUnlock(); |
|
615 return lockErr; |
|
616 } |
|
617 |
|
618 /** |
|
619 UnlockCacheLine() |
|
620 */ |
|
621 TBool CCacheManager::UnlockCacheLine(CCacheClient* aClient, const TCacheLine& aCacheLine) |
|
622 { |
|
623 CacheLock(); |
|
624 |
|
625 TCacheLine& cacheLine = const_cast<TCacheLine&>(aCacheLine); |
|
626 __CACHELINE_INVARIANT((cacheLine)); |
|
627 |
|
628 __ASSERT_DEBUG(cacheLine.iLockCount > 0, Fault(EInvalidLockCount)); |
|
629 __ASSERT_ALWAYS(cacheLine.iClient == aClient, Fault(EUnlockingUnownedCacheline)); |
|
630 |
|
631 |
|
632 __CACHE_PRINT2(_L("CACHEMAN: UnlockCacheLine(%08X, %d)"), cacheLine.iAddr, cacheLine.iAllocatedSegments); |
|
633 |
|
634 |
|
635 // decrement the lock count |
|
636 TBool success = ETrue; |
|
637 |
|
638 if (cacheLine.iLockCount > 1) |
|
639 { |
|
640 cacheLine.iLockCount--; |
|
641 } |
|
642 else |
|
643 { |
|
644 if (cacheLine.iDirtySegments == 0) |
|
645 { |
|
646 cacheLine.iLockCount--; |
|
647 #ifdef __LOCK_ENTIRE_CACHELINE__ |
|
648 Unlock(cacheLine.iAddr, cacheLine.iAllocatedSegments); |
|
649 #else |
|
650 Unlock( |
|
651 cacheLine.iAddr + (cacheLine.iLockedSegmentStart<<KSegmentSizeLog2), |
|
652 cacheLine.iLockedSegmentCount); |
|
653 #endif |
|
654 |
|
655 cacheLine.iLockedSegmentStart = cacheLine.iLockedSegmentCount = 0; |
|
656 } |
|
657 else |
|
658 { |
|
659 __CACHE_PRINT(_L("CACHEMAN: UnlockCacheLine - not unlocking segment dirty")); |
|
660 success = EFalse; |
|
661 } |
|
662 } |
|
663 |
|
664 CacheUnlock(); |
|
665 |
|
666 return success; |
|
667 } |
|
668 |
|
669 |
|
670 |
|
671 TBool CCacheManager::StealCacheLine(CCacheClient* aClient) |
|
672 { |
|
673 __ASSERT_DEBUG(iManagerLocked, Fault(EManagerNotLocked)); |
|
674 |
|
675 TInt usedQueueSize = iUsedQueue.Count(); |
|
676 |
|
677 #define INC_INDEX(x) (x++, x = (x >= usedQueueSize ? 0 : x)) |
|
678 |
|
679 __CACHE_PRINT2(_L("CACHEMAN: StealCacheLine, iNextCacheLineToSteal %d used count %d"), iNextCacheLineToSteal, iUsedQueue.Count()); |
|
680 |
|
681 TInt iInitIndex = iNextCacheLineToSteal; |
|
682 |
|
683 // Loop through all used cachelines, starting at the last stolen |
|
684 // cacheline index + 1 and ending when we find a suitable cacheline |
|
685 // to steal or we have looped back to the start |
|
686 for (INC_INDEX(iNextCacheLineToSteal); |
|
687 iNextCacheLineToSteal != iInitIndex; |
|
688 INC_INDEX(iNextCacheLineToSteal)) |
|
689 { |
|
690 TCacheLine& cacheLine = *iUsedQueue[iNextCacheLineToSteal]; |
|
691 if (cacheLine.iLockCount == 0 && cacheLine.iClient != aClient) |
|
692 { |
|
693 __CACHE_PRINT3(_L("CACHEMAN: StealCacheLine, stealing %d from %08X to %08X"), |
|
694 iNextCacheLineToSteal, cacheLine.iClient, aClient); |
|
695 FreeCacheLine(cacheLine); |
|
696 return ETrue; |
|
697 } |
|
698 } |
|
699 return EFalse; |
|
700 } |
|
701 |
|
702 |
|
703 TBool CCacheManager::FreeCacheLine(CCacheClient* aClient, const TCacheLine& aCacheLine) |
|
704 { |
|
705 CacheLock(); |
|
706 |
|
707 TCacheLine& cacheLine = const_cast<TCacheLine&>(aCacheLine); |
|
708 __CACHELINE_INVARIANT((cacheLine)); |
|
709 |
|
710 // Has the cacheline been stolen ? (Assume success if it has) |
|
711 if (cacheLine.iClient != aClient) |
|
712 { |
|
713 __CACHE_PRINT(_L("CCacheManager::FreeCacheLine(), Cacheline Stolen !!!!\n")); |
|
714 CacheUnlock(); |
|
715 return ETrue; |
|
716 } |
|
717 |
|
718 // Is the cacheline locked ? |
|
719 if (cacheLine.iLockCount > 0) |
|
720 { |
|
721 __CACHE_PRINT(_L("CCacheManager::FreeCacheLine(), attempt to free locked cacheline\n")); |
|
722 CacheUnlock(); |
|
723 return EFalse; |
|
724 } |
|
725 |
|
726 FreeCacheLine(cacheLine); |
|
727 |
|
728 CacheUnlock(); |
|
729 return ETrue; |
|
730 } |
|
731 |
|
732 TInt CCacheManager::LockCount(CCacheClient* aClient, const TCacheLine& aCacheLine) |
|
733 { |
|
734 TCacheLine& cacheLine = const_cast<TCacheLine&>(aCacheLine); |
|
735 __CACHELINE_INVARIANT((cacheLine)); |
|
736 |
|
737 // cacheline stolen ? |
|
738 if (cacheLine.iClient != aClient) |
|
739 return 0; |
|
740 |
|
741 return cacheLine.iLockCount; |
|
742 } |
|
743 |
|
744 TInt CCacheManager::FillCount(CCacheClient* aClient, const TCacheLine& aCacheLine) |
|
745 { |
|
746 TCacheLine& cacheLine = const_cast<TCacheLine&>(aCacheLine); |
|
747 __CACHELINE_INVARIANT((cacheLine)); |
|
748 |
|
749 // cacheline stolen ? |
|
750 if (cacheLine.iClient != aClient) |
|
751 return 0; |
|
752 |
|
753 return cacheLine.iFilledSegments; |
|
754 } |
|
755 |
|
756 TInt CCacheManager::DirtyCount(CCacheClient* aClient, const TCacheLine& aCacheLine) |
|
757 { |
|
758 TCacheLine& cacheLine = const_cast<TCacheLine&>(aCacheLine); |
|
759 __CACHELINE_INVARIANT((cacheLine)); |
|
760 |
|
761 // cacheline stolen ? |
|
762 if (cacheLine.iClient != aClient) |
|
763 return 0; |
|
764 |
|
765 return cacheLine.iDirtySegments; |
|
766 } |
|
767 |
|
768 TBool CCacheManager::InUse(const TCacheLine& aCacheLine) |
|
769 { |
|
770 TCacheLine& cacheLine = const_cast<TCacheLine&>(aCacheLine); |
|
771 __CACHELINE_INVARIANT((cacheLine)); |
|
772 |
|
773 // busy if lock count >= 1 and there are no dirty segments |
|
774 // or if lock count >= 2 and there are dirty segments |
|
775 return (cacheLine.iLockCount - |
|
776 (cacheLine.iDirtySegments?1:0)) > 0 ? (TBool)ETrue : (TBool)EFalse; |
|
777 } |
|
778 |
|
779 void CCacheManager::SetFilled(CCacheClient* aClient, const TCacheLine& aCacheLine, TInt aSegmentCount) |
|
780 { |
|
781 TCacheLine& cacheLine = const_cast<TCacheLine&>(aCacheLine); |
|
782 __CACHELINE_INVARIANT((cacheLine)); |
|
783 __ASSERT_DEBUG(aSegmentCount <= iSegmentsPerCacheLine, Fault(EInvalidSegmentCount)); |
|
784 __ASSERT_DEBUG(aSegmentCount <= cacheLine.iAllocatedSegments , Fault(EInvalidDirtyCount)); |
|
785 __ASSERT_DEBUG(cacheLine.iLockCount > 0, Fault(ESetFilledNotLocked)); |
|
786 __ASSERT_ALWAYS(cacheLine.iClient == aClient, Fault(EExtendingUnownedCacheline)); |
|
787 |
|
788 |
|
789 cacheLine.iFilledSegments = Max(cacheLine.iFilledSegments, (TUint8) aSegmentCount); |
|
790 |
|
791 __ASSERT_DEBUG(cacheLine.iFilledSegments >= cacheLine.iDirtySegments , Fault(EInvalidDirtyCount)); |
|
792 } |
|
793 |
|
794 void CCacheManager::SetDirty(CCacheClient* aClient, const TCacheLine& aCacheLine, TInt aSegmentCount) |
|
795 { |
|
796 TCacheLine& cacheLine = const_cast<TCacheLine&>(aCacheLine); |
|
797 __CACHELINE_INVARIANT((cacheLine)); |
|
798 __ASSERT_DEBUG(aSegmentCount <= iSegmentsPerCacheLine, Fault(EInvalidSegmentCount)); |
|
799 __ASSERT_DEBUG(aSegmentCount <= cacheLine.iAllocatedSegments , Fault(EInvalidDirtyCount)); |
|
800 __ASSERT_DEBUG(cacheLine.iLockCount > 0, Fault(ESetDirtyNotLocked)); |
|
801 __ASSERT_ALWAYS(cacheLine.iClient == aClient, Fault(EExtendingUnownedCacheline)); |
|
802 |
|
803 // ensure that lock range is valid - we insist on dirty |
|
804 // cachelines having all dirty pages locked up to the the last dirty page |
|
805 __ASSERT_DEBUG(cacheLine.iLockedSegmentStart == 0, Fault(ESetDirtyInvalidLockRange)); |
|
806 __ASSERT_DEBUG(cacheLine.iLockedSegmentCount >= aSegmentCount, Fault(ESetDirtyInvalidLockRange)); |
|
807 |
|
808 cacheLine.iFilledSegments = Max(cacheLine.iFilledSegments, (TUint8) aSegmentCount); |
|
809 cacheLine.iDirtySegments = Max(cacheLine.iDirtySegments, (TUint8) aSegmentCount); |
|
810 |
|
811 __ASSERT_DEBUG(cacheLine.iFilledSegments >= cacheLine.iDirtySegments , Fault(EInvalidDirtyCount)); |
|
812 } |
|
813 |
|
814 void CCacheManager::ClearDirty(CCacheClient* aClient, const TCacheLine& aCacheLine) |
|
815 { |
|
816 TCacheLine& cacheLine = const_cast<TCacheLine&>(aCacheLine); |
|
817 __CACHELINE_INVARIANT((cacheLine)); |
|
818 __ASSERT_DEBUG(cacheLine.iLockCount > 0, Fault(EClearDirtyNotLocked)); |
|
819 __ASSERT_ALWAYS(cacheLine.iClient == aClient, Fault(EExtendingUnownedCacheline)); |
|
820 |
|
821 TInt dirtySegments = cacheLine.iDirtySegments; |
|
822 cacheLine.iDirtySegments = 0; |
|
823 SetFilled(aClient, cacheLine, dirtySegments); |
|
824 UnlockCacheLine(aClient, cacheLine); |
|
825 } |
|
826 |
|
827 |
|
828 void CCacheManager::RemoveEmptySegments(CCacheClient* aClient, const TCacheLine& aCacheLine) |
|
829 { |
|
830 CacheLock(); |
|
831 |
|
832 TCacheLine& cacheLine = const_cast<TCacheLine&>(aCacheLine); |
|
833 __CACHELINE_INVARIANT((cacheLine)); |
|
834 |
|
835 // has the cacheline been stolen ? |
|
836 if (cacheLine.iClient != aClient) |
|
837 { |
|
838 __CACHE_PRINT(_L("CCacheManager::RemoveEmptySegments((), Cacheline Stolen ! !!!\n")); |
|
839 CacheUnlock(); |
|
840 return; |
|
841 } |
|
842 |
|
843 __ASSERT_DEBUG(cacheLine.iLockCount > 0, Fault(ERemovingEmptyUnlocked)); |
|
844 |
|
845 // Unlock any locked segments past the last filled segment |
|
846 TInt filledSegmentCount = cacheLine.iFilledSegments; |
|
847 TInt firstSegmentToUnlock; |
|
848 TInt segmentsToUnlock; |
|
849 #ifdef __LOCK_ENTIRE_CACHELINE__ |
|
850 firstSegmentToUnlock = filledSegmentCount; |
|
851 segmentsToUnlock = cacheLine.iAllocatedSegments - filledSegmentCount; |
|
852 #else |
|
853 TInt firstLockedSegment = cacheLine.iLockedSegmentStart; |
|
854 if (firstLockedSegment <= filledSegmentCount) |
|
855 { |
|
856 firstSegmentToUnlock = filledSegmentCount; |
|
857 segmentsToUnlock = firstLockedSegment + cacheLine.iLockedSegmentCount - filledSegmentCount; |
|
858 } |
|
859 else |
|
860 { |
|
861 firstSegmentToUnlock = firstLockedSegment; |
|
862 segmentsToUnlock = cacheLine.iLockedSegmentCount; |
|
863 } |
|
864 #endif |
|
865 TUint8* addrFirstSegmentToUnlock = |
|
866 cacheLine.iAddr + (firstSegmentToUnlock << KSegmentSizeLog2); |
|
867 if (segmentsToUnlock > 0) |
|
868 { |
|
869 Unlock(addrFirstSegmentToUnlock, segmentsToUnlock); |
|
870 cacheLine.iLockedSegmentCount = |
|
871 (TUint8) (cacheLine.iLockedSegmentCount - segmentsToUnlock); |
|
872 } |
|
873 |
|
874 // Decommit any segments past the last filled segment |
|
875 Decommit( |
|
876 cacheLine.iAddr + (filledSegmentCount << KSegmentSizeLog2), |
|
877 cacheLine.iAllocatedSegments - filledSegmentCount); |
|
878 cacheLine.iAllocatedSegments = (TUint8) filledSegmentCount; |
|
879 |
|
880 CacheUnlock(); |
|
881 } |
|
882 |
|
883 |
|
884 void CCacheManager::FreeCacheLine(TCacheLine& aCacheLine) |
|
885 { |
|
886 __ASSERT_DEBUG(iManagerLocked, Fault(EManagerNotLocked)); |
|
887 aCacheLine.iClient = NULL; |
|
888 __ASSERT_ALWAYS( (aCacheLine.iLockCount == 0), Fault(EFreeingLockedCacheLine)); |
|
889 __ASSERT_ALWAYS( (aCacheLine.iDirtySegments == 0), Fault(EFreeingDirtyCacheLine)); |
|
890 |
|
891 Decommit(aCacheLine.iAddr, aCacheLine.iAllocatedSegments); |
|
892 aCacheLine.iAllocatedSegments = 0; |
|
893 aCacheLine.iFilledSegments = 0; |
|
894 aCacheLine.iLockedSegmentStart = 0; |
|
895 aCacheLine.iLockedSegmentCount = 0; |
|
896 |
|
897 // Remove from used queue |
|
898 TInt index = iUsedQueue.FindInAddressOrder(&aCacheLine); |
|
899 __ASSERT_ALWAYS(index != KErrNotFound, Fault(ESegmentNotFound)); |
|
900 iUsedQueue.Remove(index); |
|
901 |
|
902 // put back on free queue |
|
903 // TInt r = iFreeQueue.Append(&aCacheLine); |
|
904 TInt r = iFreeQueue.InsertInAddressOrder(&aCacheLine); |
|
905 __ASSERT_ALWAYS(r == KErrNone, Fault(EAppendToFreeQueueFailed)); |
|
906 |
|
907 __CACHE_PRINT2(_L("CACHEMAN: FreeCacheLine, iFreeQueue %d iUsedQueue %d"), iFreeQueue.Count(), iUsedQueue.Count()); |
|
908 } |
|
909 |
|
910 |
|
911 #if defined(_DEBUG) || defined(_DEBUG_RELEASE) |
|
912 void CCacheManager::DumpCacheLine(TCacheLine& aCacheLine) |
|
913 { |
|
914 RDebug::Print(_L("CACHEMAN: Cacheline client %08X addr %08X pos %d alloc %d filled %d dirty %d lock %d \tData: %02X %02X %02X %02X %02X %02X %02X %02X "), |
|
915 aCacheLine.iClient, |
|
916 aCacheLine.iAddr, |
|
917 I64LOW(aCacheLine.iPos), |
|
918 |
|
919 aCacheLine.iAllocatedSegments, |
|
920 aCacheLine.iFilledSegments, |
|
921 aCacheLine.iDirtySegments, |
|
922 aCacheLine.iLockCount, |
|
923 |
|
924 aCacheLine.iAddr[0], |
|
925 aCacheLine.iAddr[1], |
|
926 aCacheLine.iAddr[2], |
|
927 aCacheLine.iAddr[3], |
|
928 aCacheLine.iAddr[4], |
|
929 aCacheLine.iAddr[5], |
|
930 aCacheLine.iAddr[6], |
|
931 aCacheLine.iAddr[7] |
|
932 ); |
|
933 } |
|
934 |
|
935 void CCacheManager::DumpCache() |
|
936 { |
|
937 CacheLock(); |
|
938 |
|
939 RPointerArray<CCacheClient> clients; |
|
940 |
|
941 RDebug::Print(_L("**** CACHEMAN: CacheLines ****\n")); |
|
942 TInt usedQueueSize = iUsedQueue.Count(); |
|
943 TInt n; |
|
944 for (n=0; n<usedQueueSize; n++) |
|
945 { |
|
946 TCacheLine& cacheLine = *iUsedQueue[n]; |
|
947 DumpCacheLine(cacheLine); |
|
948 |
|
949 clients.InsertInAddressOrder(cacheLine.iClient); |
|
950 } |
|
951 |
|
952 TInt clientCount = clients.Count(); |
|
953 |
|
954 for (n=0; n<clientCount; n++) |
|
955 { |
|
956 RDebug::Print(_L("**** CACHEMAN: CacheClient #%d ****\n"), n); |
|
957 clients[n]->DumpCache(); |
|
958 } |
|
959 |
|
960 clients.Close(); |
|
961 |
|
962 CacheUnlock(); |
|
963 } |
|
964 #endif |
|
965 |
|
966 |
|
967 |
|
968 |
|
969 TUint8* CCacheManager::Base() |
|
970 { |
|
971 return iChunk.Base(); |
|
972 } |
|
973 |
|
974 |
|
975 TInt CCacheManager::Lock(TUint8* const aAddr, TInt aSegmentCount) |
|
976 { |
|
977 TInt r = iChunk.Lock(aAddr-iBase, aSegmentCount << KSegmentSizeLog2); |
|
978 //RDebug::Print(_L("RChunk::Lock(%08X, %d) %d"), aAddr-iBase, aSegmentCount << KSegmentSizeLog2, r); |
|
979 __ASSERT_DEBUG(r == KErrNone || r == KErrNoMemory || r == KErrNotFound, Fault(EUnexpectedLockFailure)); |
|
980 |
|
981 __CACHE_PRINT3(_L("CACHEMAN: LOCK %08X %d %d"), aAddr, aSegmentCount, r); |
|
982 |
|
983 #ifdef __SIMULATE_LOCK_FAILURES__ |
|
984 if (SimulatedFailure(iLockFailureCount)) |
|
985 { |
|
986 __CACHE_PRINT(_L("CACHEMAN: simulating lock failure")); |
|
987 r = KErrNotFound; |
|
988 } |
|
989 #endif |
|
990 |
|
991 if (r == KErrNone) |
|
992 { |
|
993 iLockedSegmentCount+= aSegmentCount; |
|
994 } |
|
995 else |
|
996 { |
|
997 __CACHE_PRINT(_L("CACHEMAN: LOCK FAILED")); |
|
998 #if defined(_DEBUG) || defined(_DEBUG_RELEASE) |
|
999 iStats.iLockFailureCount++; |
|
1000 #endif |
|
1001 } |
|
1002 |
|
1003 return r; |
|
1004 } |
|
1005 |
|
1006 TInt CCacheManager::Unlock(TUint8* const aAddr, TInt aSegmentCount) |
|
1007 { |
|
1008 TInt r = iChunk.Unlock(aAddr-iBase, aSegmentCount << KSegmentSizeLog2); |
|
1009 //RDebug::Print(_L("RChunk::Unlock(%08X, %d) %d"), aAddr-iBase, aSegmentCount << KSegmentSizeLog2, r); |
|
1010 |
|
1011 __CACHE_PRINT3(_L("CACHEMAN: UNLOCK %08X %d %d"), aAddr, aSegmentCount, r); |
|
1012 if (r == KErrNone) |
|
1013 { |
|
1014 iLockedSegmentCount-= aSegmentCount; |
|
1015 } |
|
1016 else |
|
1017 { |
|
1018 __CACHE_PRINT(_L("CACHEMAN: UNLOCK FAILED")); |
|
1019 } |
|
1020 |
|
1021 return r; |
|
1022 } |
|
1023 |
|
1024 TInt CCacheManager::Commit(TUint8* const aAddr, TInt aSegmentCount) |
|
1025 { |
|
1026 #ifdef __SIMULATE_LOCK_FAILURES__ |
|
1027 if (SimulatedFailure(iCommitFailureCount)) |
|
1028 { |
|
1029 __CACHE_PRINT(_L("CACHEMAN: simulating commit failure ")); |
|
1030 return KErrNoMemory; |
|
1031 } |
|
1032 #endif |
|
1033 |
|
1034 TInt r = iChunk.Commit(aAddr-iBase, aSegmentCount << KSegmentSizeLog2); |
|
1035 //RDebug::Print(_L("RChunk::Commit(%08X, %d) %d, "), aAddr-iBase, aSegmentCount << KSegmentSizeLog2, r); |
|
1036 |
|
1037 __CACHE_PRINT3(_L("CACHEMAN: COMMIT: %08X %d %d, "), aAddr, aSegmentCount, r); |
|
1038 if (r == KErrNone) |
|
1039 { |
|
1040 iLockedSegmentCount+= aSegmentCount; |
|
1041 #if defined(_DEBUG) || defined(_DEBUG_RELEASE) |
|
1042 iStats.iAllocatedSegmentCount+= aSegmentCount; |
|
1043 #endif |
|
1044 } |
|
1045 else |
|
1046 { |
|
1047 __CACHE_PRINT(_L("CACHEMAN: COMMIT FAILED")); |
|
1048 #if defined(_DEBUG) || defined(_DEBUG_RELEASE) |
|
1049 iStats.iCommitFailureCount++; |
|
1050 #endif |
|
1051 } |
|
1052 |
|
1053 |
|
1054 __ASSERT_DEBUG(r == KErrNone || r == KErrNoMemory, Fault(EUnexpectedCommitFailure)); |
|
1055 |
|
1056 return r; |
|
1057 } |
|
1058 |
|
1059 TInt CCacheManager::Decommit(TUint8* const aAddr, TInt aSegmentCount) |
|
1060 { |
|
1061 TInt r = iChunk.Decommit(aAddr-iBase, aSegmentCount << KSegmentSizeLog2); |
|
1062 //RDebug::Print(_L("RChunk::Decommit(%08X, %d), %d"), aAddr-iBase, aSegmentCount << KSegmentSizeLog2, r); |
|
1063 |
|
1064 __CACHE_PRINT3(_L("CACHEMAN: DECOMMIT: %08X %d %d, "), aAddr, aSegmentCount, r); |
|
1065 if (r == KErrNone) |
|
1066 { |
|
1067 #if defined(_DEBUG) || defined(_DEBUG_RELEASE) |
|
1068 iStats.iAllocatedSegmentCount-= aSegmentCount; |
|
1069 #endif |
|
1070 } |
|
1071 else |
|
1072 { |
|
1073 __CACHE_PRINT(_L("CACHEMAN: DECOMMIT FAILED")); |
|
1074 } |
|
1075 |
|
1076 return r; |
|
1077 } |
|
1078 |
|
1079 #if defined(_DEBUG) || defined(_DEBUG_RELEASE) |
|
1080 |
|
1081 void CCacheManager::SimulateLockFailureMode(TBool aEnable) |
|
1082 { |
|
1083 iSimulateLockFailureMode = aEnable; |
|
1084 __CACHE_PRINT1(_L("CACHEMAN: SimulateLockFailureMode: %d, "), iSimulateLockFailureMode); |
|
1085 } |
|
1086 |
|
1087 void CCacheManager::AllocateMaxSegments(TBool aEnable) |
|
1088 { |
|
1089 iAllocateMaxSegments = aEnable; |
|
1090 __CACHE_PRINT1(_L("CACHEMAN: iAllocateMaxSegments: %d, "), iAllocateMaxSegments); |
|
1091 } |
|
1092 |
|
1093 TBool CCacheManager::AllocateMaxSegments() |
|
1094 { |
|
1095 return iAllocateMaxSegments; |
|
1096 } |
|
1097 |
|
1098 void CCacheManager::SimulateWriteFailure() |
|
1099 { |
|
1100 iSimulateWriteFailure = ETrue; |
|
1101 } |
|
1102 |
|
1103 TBool CCacheManager::SimulateWriteFailureEnabled() |
|
1104 { |
|
1105 TBool b = iSimulateWriteFailure; |
|
1106 iSimulateWriteFailure = EFalse; |
|
1107 return b; |
|
1108 } |
|
1109 |
|
1110 TBool CCacheManager::SimulatedFailure(TInt& aFailureCount) |
|
1111 { |
|
1112 #ifdef __SIMULATE_LOCK_FAILURES__ |
|
1113 if (iSimulateLockFailureMode) |
|
1114 { |
|
1115 #if defined (__RAMDOM_LOCK_FAILURES__) |
|
1116 static TInt FailCount = 15; |
|
1117 if (++aFailureCount >= FailCount) |
|
1118 #elif defined (__PSEUDO_RANDOM_FAILURES__) |
|
1119 const TInt FailCounts[] = {15,2,0,21,1,12,24}; |
|
1120 const TInt FailCountSize = sizeof(FailCounts) / sizeof(TInt); |
|
1121 static TInt FailCountIndex = 0; |
|
1122 if (++aFailureCount >= FailCounts[FailCountIndex]) |
|
1123 #else |
|
1124 const TInt KFailCount = 15; |
|
1125 if (++aFailureCount >= KFailCount) |
|
1126 #endif |
|
1127 { |
|
1128 aFailureCount = 0; |
|
1129 #if defined (__RAMDOM_LOCK_FAILURES__) |
|
1130 FailCount = Math::Random() & 0x1F; |
|
1131 __CACHE_PRINT1(_L("FailCount %d"), FailCount); |
|
1132 #endif |
|
1133 |
|
1134 #if defined (__PSEUDO_RANDOM_FAILURES__) |
|
1135 FailCountIndex = (FailCountIndex +1) % FailCountSize ; |
|
1136 __CACHE_PRINT1(_L("FailCount %d"), FailCounts[FailCountIndex]); |
|
1137 #endif |
|
1138 |
|
1139 return ETrue; |
|
1140 } |
|
1141 } |
|
1142 #endif |
|
1143 return EFalse; |
|
1144 } |
|
1145 #endif // defined(_DEBUG) || defined(_DEBUG_RELEASE) |
|
1146 |
|
1147 //************************************ |
|
1148 // TGlobalFileCacheSettings |
|
1149 //************************************ |
|
1150 |
|
1151 //TGlobalCacheFlags TGlobalFileCacheSettings::iFlags = TGlobalCacheFlags(ECacheEnabled); |
|
1152 TInt32 TGlobalFileCacheSettings::iCacheSize = KDefaultGlobalCacheSize << KByteToByteShift; |
|
1153 TInt32 TGlobalFileCacheSettings::iMaxLockedSize = KDefaultGlobalCacheMaxLockedSize << KByteToByteShift; |
|
1154 TInt32 TGlobalFileCacheSettings::iLowMemoryThreshold = KDefaultLowMemoryThreshold; |
|
1155 TBool TGlobalFileCacheSettings::iEnabled = KDefaultGlobalCacheEnabled; |
|
1156 |
|
1157 _LIT8(KLitSectionNameFileCache,"FileCache"); |
|
1158 |
|
1159 void TGlobalFileCacheSettings::ReadPropertiesFile() |
|
1160 { |
|
1161 F32Properties::GetBool(KLitSectionNameFileCache, _L8("GlobalCacheEnabled"), iEnabled); |
|
1162 |
|
1163 // Get size of cache in kilobytes |
|
1164 TInt32 cacheSizeInKBytes; |
|
1165 if (F32Properties::GetInt(KLitSectionNameFileCache, _L8("GlobalCacheSize"), cacheSizeInKBytes)) |
|
1166 iCacheSize = cacheSizeInKBytes << KByteToByteShift; |
|
1167 |
|
1168 // Get maximum amount of locked data i.e data unavailable for paging |
|
1169 TInt32 maxLockedSize; |
|
1170 if (F32Properties::GetInt(KLitSectionNameFileCache, _L8("GlobalCacheMaxLockedSize"), maxLockedSize)) |
|
1171 iMaxLockedSize = maxLockedSize << KByteToByteShift; |
|
1172 |
|
1173 // Get low memory threshold |
|
1174 TInt32 lowMemoryThreshold; |
|
1175 if (F32Properties::GetInt(KLitSectionNameFileCache, _L8("LowMemoryThreshold"), lowMemoryThreshold)) |
|
1176 iLowMemoryThreshold = lowMemoryThreshold; |
|
1177 |
|
1178 } |
|
1179 |
|
1180 |
|
1181 TBool TGlobalFileCacheSettings::Enabled() |
|
1182 { |
|
1183 return iEnabled; |
|
1184 } |
|
1185 |
|
1186 TInt TGlobalFileCacheSettings::CacheSize() |
|
1187 { |
|
1188 return iCacheSize; |
|
1189 } |
|
1190 TInt TGlobalFileCacheSettings::MaxLockedSize() |
|
1191 { |
|
1192 return iMaxLockedSize; |
|
1193 } |
|
1194 |
|
1195 TInt TGlobalFileCacheSettings::LowMemoryThreshold() |
|
1196 { |
|
1197 return iLowMemoryThreshold; |
|
1198 } |