userlibandfileserver/fileserver/sfile/sf_cache_man.h
changeset 0 a41df078684a
child 297 b2826f67641f
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     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.h
       
    15 // 
       
    16 //
       
    17 
       
    18 /**
       
    19  @file
       
    20  @internalTechnology
       
    21 */
       
    22 
       
    23 #if !defined(__SF_CACHE_MAN_H__)
       
    24 #define __SF_CACHE_MAN_H__
       
    25 
       
    26 #ifdef _DEBUG
       
    27 #define __SIMULATE_LOCK_FAILURES__
       
    28 #endif
       
    29 
       
    30 // forward ref
       
    31 class CCacheManager;
       
    32 class CCacheClient;
       
    33 
       
    34 const TInt KByteToByteShift = 10;
       
    35 
       
    36 enum TCacheManagerFault
       
    37 	{
       
    38 	ENoCacheManager				= 0,
       
    39 	ECacheAlreadyCreated		= 1,
       
    40 	EIllegalDriveNumber			= 2,
       
    41 	EIllegalCacheSize			= 3,			
       
    42 	EIllegalPageSize			= 4,
       
    43 	EInvalidAllocCount			= 5,
       
    44 	EInvalidLockCount			= 6,
       
    45 	EInvalidFillCount			= 7,
       
    46 	EAppendToFreeQueueFailed	= 8,
       
    47 	ESegmentNotFound			= 9,
       
    48 	EUnlockFailed				= 10,
       
    49 	EInvalidSegmentCount		= 11,
       
    50 	EInvalidAddress				= 12,
       
    51 	EInvalidDirtyCount			= 13,
       
    52 	EDecommitFailed				= 14,
       
    53 	EUnexpectedCommitFailure	= 15,
       
    54 	EUnexpectedLockFailure		= 16,
       
    55 	EInvalidCacheLine			= 17,
       
    56 	EInvalidClient				= 18,
       
    57 	ERemovingEmptyUnlocked		= 19,
       
    58 	EFreeingLockedCacheLine		= 20,
       
    59 	EFreeingDirtyCacheLine		= 21,
       
    60 	ESetDirtyNotLocked			= 22,
       
    61 	EClearDirtyNotLocked		= 23,
       
    62 	ESetFilledNotLocked			= 24,
       
    63 	EManagerNotLocked			= 25,
       
    64 	EExtendingUnownedCacheline	= 26,
       
    65 	EUnlockingUnownedCacheline	= 27,
       
    66 	EInvalidLockedPageStart		= 28,
       
    67 	EInvalidLockedPageCount		= 29,
       
    68 	EInvalidLockRange			= 30,
       
    69 	ESetDirtyInvalidLockRange	= 31,
       
    70 	ELockingAndAlreadyDirty		= 32,
       
    71 	EInvalidStats				= 33
       
    72 	};
       
    73 
       
    74 
       
    75 class CCacheManagerFactory
       
    76 	{
       
    77 public:
       
    78 	static void CreateL();
       
    79 	static TInt Destroy();
       
    80 	static CCacheManager* CacheManager();
       
    81 private:
       
    82 	static CCacheManager* iCacheManager;
       
    83 	};
       
    84 
       
    85 
       
    86 NONSHARABLE_CLASS(CCacheManager) : public CBase
       
    87 	{
       
    88 private:
       
    89 
       
    90 	class TCacheLine
       
    91 		{
       
    92 	public:
       
    93 		TUint8* iAddr;
       
    94 		TUint8 iAllocatedSegments;	// number of allocated pages 
       
    95 		TUint8 iFilledSegments;		// number of full pages (i.e. pages with data in)
       
    96 		TUint8 iDirtySegments;		// number of dirty pages
       
    97 		TUint8 iLockCount;			// number of times cacheline has been locked
       
    98 		TUint8 iLockedSegmentStart;	// zero based index of first locked paged
       
    99 		TUint8 iLockedSegmentCount;	// number of locked pages
       
   100 		TUint8 iSpare[2];			// padding
       
   101 		CCacheClient* iClient;		// owner of this cacheline
       
   102 		TInt64 iPos;				// arbitrary data owned by client
       
   103 		};
       
   104 
       
   105 public:
       
   106 	CCacheClient* CreateClientL();
       
   107 	void RegisterClient(CCacheClient& aClient);
       
   108 	void DeregisterClient(CCacheClient& aClient);
       
   109 
       
   110 
       
   111 	TInt SegmentSize();
       
   112 	TInt SegmentSizeLog2();
       
   113 	TInt64 SegmentSizeMask();
       
   114 	TInt CacheLineSize();
       
   115 	TInt CacheLineSizeLog2();
       
   116 	TInt CacheLineSizeInSegments();
       
   117 
       
   118 	// called from CCacheClient
       
   119 	TInt AllocateAndLockCacheLine(CCacheClient* aClient, TInt64 aPos, const TCacheLine*& aCacheLine, TInt aSegmentCount);
       
   120 	TInt ReAllocateAndLockCacheLine(CCacheClient* aClient, TInt64 aPos, const TCacheLine& aCacheLine, TInt aSegmentCount);
       
   121 
       
   122 	TInt ExtendCacheLine(CCacheClient* aClient, const TCacheLine& aCacheLine, TInt aSegmentCount);
       
   123 	void RemoveEmptySegments(CCacheClient* aClient, const TCacheLine& aCacheLine);
       
   124 
       
   125 	TInt LockCacheLine(CCacheClient* aClient, const TCacheLine& aCacheLine, TInt aLockedPageStart, TInt aLockedPageCount);
       
   126 	TBool UnlockCacheLine(CCacheClient* aClient, const TCacheLine& aCacheLine);
       
   127 
       
   128 	TBool FreeCacheLine(CCacheClient* aClient, const TCacheLine& aCacheLine);
       
   129 
       
   130 
       
   131 	TInt LockCount(CCacheClient* aClient, const TCacheLine& aCacheLine);
       
   132 	TInt FillCount(CCacheClient* aClient, const TCacheLine& aCacheLine);
       
   133 	TInt DirtyCount(CCacheClient* aClient, const TCacheLine& aCacheLine);
       
   134 	TBool TooManyLockedSegments();
       
   135 
       
   136 	void SetFilled(CCacheClient* aClient, const TCacheLine& aCacheLine,  TInt aSegmentCount);
       
   137 	void SetDirty(CCacheClient* aClient, const TCacheLine& aCacheLine,  TInt aSegmentCount);
       
   138 	void ClearDirty(CCacheClient* aClient, const TCacheLine& aCacheLine);
       
   139 	TBool InUse(const TCacheLine& aCacheLine);
       
   140 
       
   141 	// called from CKernEventNotifier::FreeMemoryChangeCallback()
       
   142 	void FreeMemoryChanged(TBool aMemoryLow);
       
   143 
       
   144 #if defined(_DEBUG) || defined(_DEBUG_RELEASE)
       
   145 	void DumpCache();
       
   146 	void DumpCacheLine(TCacheLine& aCacheLine);
       
   147 	void SimulateLockFailureMode(TBool aEnable);
       
   148 	void AllocateMaxSegments(TBool aEnable);
       
   149 	TBool AllocateMaxSegments();
       
   150 	// stats
       
   151 	TFileCacheStats& Stats();
       
   152 
       
   153 	void SimulateWriteFailure();
       
   154 	TBool SimulateWriteFailureEnabled();
       
   155 	TBool SimulatedFailure(TInt& aFailureCount);
       
   156 #endif
       
   157 
       
   158 private:
       
   159 	~CCacheManager();
       
   160 
       
   161 	CCacheManager(TUint aCacheSize);
       
   162 	void ConstructL();
       
   163 
       
   164 	inline TUint8* Base();
       
   165 
       
   166 	inline void CacheLock();
       
   167 	inline void CacheUnlock();
       
   168 
       
   169 	TBool StealCacheLine(CCacheClient* aClient);
       
   170 
       
   171 	TInt Lock(TUint8* aAddr, TInt aSegmentCount);
       
   172 	TInt Unlock(TUint8* aAddr, TInt aSegmentCount);
       
   173 	TInt Commit(TUint8* aAddr, TInt aSegmentCount);
       
   174 	TInt Decommit(TUint8* aAddr, TInt aSegmentCount);
       
   175 
       
   176 	static CCacheManager* NewCacheL(TInt aUncommitedCacheSize);
       
   177 
       
   178 	void FreeCacheLine(TCacheLine& aCacheLine);
       
   179 
       
   180 private:
       
   181 
       
   182 	RFastLock iLock;
       
   183 
       
   184 	TCacheLine* iCacheLines;
       
   185 	RPointerArray<TCacheLine> iFreeQueue;
       
   186 	RPointerArray<TCacheLine> iUsedQueue;
       
   187 
       
   188 	TUint8* iBase;
       
   189 	
       
   190 	TInt iNumOfCacheLines;
       
   191 	
       
   192 	TInt iCacheLineSize;
       
   193 	TInt iCacheLineSizeLog2;
       
   194 
       
   195 	TInt iCacheSize;
       
   196 	TInt iMaxLockedSegments;
       
   197 	
       
   198 	TInt iSegmentsPerCacheLine;
       
   199 
       
   200 	TInt iLockedSegmentCount;
       
   201 
       
   202 	RChunk iChunk;
       
   203 	TBool iSimulateLockFailureMode;
       
   204 
       
   205 #if defined(_DEBUG) || defined(_DEBUG_RELEASE)
       
   206 	TBool iManagerLocked;
       
   207 	TInt iLockFailureCount;
       
   208 	TInt iCommitFailureCount;
       
   209 	TInt iAllocFailureCount;
       
   210 	TBool iAllocateMaxSegments;
       
   211 
       
   212 	TFileCacheStats iStats;
       
   213 	TBool iSimulateWriteFailure;
       
   214 #endif
       
   215 
       
   216 	// low memory notification stuff
       
   217 	TBool iMemoryLow;			// ETrue if kernel has notified us that memory has fallen below a specified threshold
       
   218 	TInt iLowMemoryThreshold;
       
   219 
       
   220 	// index of the next cacheline in iUsedQueue to steal if 
       
   221 	// all cachelines are in use
       
   222 	TInt iNextCacheLineToSteal;
       
   223 
       
   224 friend class CCacheManagerFactory;
       
   225 friend class CCacheClient;
       
   226 	};
       
   227 
       
   228 
       
   229 NONSHARABLE_CLASS(TGlobalFileCacheSettings)
       
   230 	{
       
   231 public:
       
   232 	static void ReadPropertiesFile();
       
   233 
       
   234 	static TBool Enabled();
       
   235 	static TInt CacheSize();
       
   236 	static TInt MaxLockedSize();
       
   237 	static TInt LowMemoryThreshold();
       
   238 private:
       
   239 	static TBool iEnabled;
       
   240 	static TInt32 iCacheSize;
       
   241 	static TInt32 iMaxLockedSize;
       
   242 	static TInt32 iLowMemoryThreshold;
       
   243 	};
       
   244 
       
   245 #endif	// !defined(__SF_CACHE_MAN_H__)