kernel/eka/memmodel/epoc/flexible/mmu/mpagecleaner.cpp
branchRCL_3
changeset 110 c734af59ce98
equal deleted inserted replaced
97:41f0cfe18c80 110:c734af59ce98
       
     1 // Copyright (c) 2007-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 //
       
    15 
       
    16 #include <kernel.h>
       
    17 #include "mpagecleaner.h"
       
    18 #include "mm.h"
       
    19 #include "mmu.h"
       
    20 #include "mpager.h"
       
    21 
       
    22 #ifdef __PAGING_PRE_CLEAN_DIRTY_PAGES
       
    23 
       
    24 inline void IgnorePrintf(...) { }
       
    25 
       
    26 #define PAGE_CLEANER_TRACE IgnorePrintf
       
    27 //#define PAGE_CLEANER_TRACE Kern::Printf
       
    28 
       
    29 _LIT(KThreadName, "PageCleaner");
       
    30 
       
    31 const TInt KThreadPriority = 25;
       
    32 
       
    33 /// The length of time the paging device is idle before we decide to use it for cleaning dirty
       
    34 /// pages, in milliseconds.
       
    35 const TInt KIdleDelayInMillis = 2;
       
    36 
       
    37 class DPageCleaner
       
    38 	{
       
    39 public:
       
    40 	DPageCleaner();
       
    41 	void Start();
       
    42 	void NotifyPagingDeviceIdle();
       
    43 	void NotifyPagingDeviceBusy();
       
    44 	void NotifyPagesToClean();
       
    45 
       
    46 private:
       
    47 	inline TBool IsRunning();
       
    48 	void UpdateBusyCount(TInt aChange);
       
    49 	void IdleTimerExpired(TUint aInitialNotificationCount);
       
    50 	void TryToClean();
       
    51 	
       
    52 private:
       
    53 	static void TimerDfcFn(TAny*);
       
    54 	static void CleanerDfcFn(TAny*);
       
    55 	
       
    56 private:
       
    57 	TInt iIdleDelayInTicks;
       
    58 	NTimer iDelayTimer;
       
    59 	TDfcQue iDfcQue;
       
    60 	TDfc iTimerDfc;
       
    61 	TDfc iCleanerDfc;
       
    62 	TBool iRunning;
       
    63 
       
    64 	// All state below is accessed with the MmuLock held.
       
    65 
       
    66 	/// Whether the paging device is currently idle.
       
    67 	TBool iPagingDeviceIdle;
       
    68 
       
    69 	/// Whether the paging device has been idle for longer than the wait period.
       
    70 	TBool iIdleForAWhile;
       
    71 	
       
    72 	/// Whether the page cleaner is currently running.
       
    73 	TBool iCleaningInProgress;	
       
    74 	};
       
    75 
       
    76 DPageCleaner ThePageCleaner;
       
    77 
       
    78 DPageCleaner::DPageCleaner() :
       
    79 	iTimerDfc(TimerDfcFn, NULL, 1),
       
    80 	iCleanerDfc(CleanerDfcFn, NULL, 1),
       
    81 	iRunning(EFalse),
       
    82 	iPagingDeviceIdle(ETrue),
       
    83 	iIdleForAWhile(ETrue),
       
    84 	iCleaningInProgress(EFalse)
       
    85 	{
       
    86 	}
       
    87 
       
    88 void DPageCleaner::Start()
       
    89 	{
       
    90 	TBool alreadyRunning = __e32_atomic_swp_ord32(&iRunning, ETrue);
       
    91 	if (alreadyRunning)
       
    92 		return;
       
    93 
       
    94 	iIdleDelayInTicks = NKern::TimerTicks(KIdleDelayInMillis);
       
    95 	
       
    96 	TInt r = Kern::DfcQInit(&iDfcQue, KThreadPriority, &KThreadName);
       
    97 	__NK_ASSERT_ALWAYS(r == KErrNone);
       
    98 	iTimerDfc.SetDfcQ(&iDfcQue);
       
    99 	iCleanerDfc.SetDfcQ(&iDfcQue);
       
   100 
       
   101 	PAGE_CLEANER_TRACE("PageCleaner started");
       
   102 	}
       
   103 
       
   104 FORCE_INLINE TBool DPageCleaner::IsRunning()
       
   105 	{
       
   106 	return __e32_atomic_load_acq32(&iRunning);
       
   107 	}
       
   108 
       
   109 void DPageCleaner::NotifyPagingDeviceIdle()
       
   110 	{
       
   111 	__NK_ASSERT_DEBUG(MmuLock::IsHeld());
       
   112 	if (IsRunning())
       
   113 		{
       
   114 		iPagingDeviceIdle = ETrue;
       
   115 		if (!iDelayTimer.IsPending())
       
   116 			iDelayTimer.OneShot(iIdleDelayInTicks, iTimerDfc);
       
   117 		}
       
   118 	}
       
   119 
       
   120 void DPageCleaner::NotifyPagingDeviceBusy()
       
   121 	{
       
   122 	__NK_ASSERT_DEBUG(MmuLock::IsHeld());
       
   123 	if (IsRunning())
       
   124 		{
       
   125 		iDelayTimer.Cancel();
       
   126 		iPagingDeviceIdle = EFalse;
       
   127 		iIdleForAWhile = EFalse;
       
   128 		}		
       
   129 	}
       
   130 
       
   131 void DPageCleaner::NotifyPagesToClean()
       
   132 	{
       
   133 	__NK_ASSERT_DEBUG(MmuLock::IsHeld());
       
   134 	if (IsRunning())
       
   135 		{
       
   136 		if (!iCleaningInProgress && iIdleForAWhile)
       
   137 			iCleanerDfc.Enque();
       
   138 		}
       
   139 	}
       
   140 
       
   141 void DPageCleaner::TimerDfcFn(TAny* aPtr)
       
   142 	{
       
   143 	ThePageCleaner.IdleTimerExpired((TUint)aPtr);	
       
   144 	}
       
   145 
       
   146 void DPageCleaner::IdleTimerExpired(TUint aInitialNotificationCount)
       
   147 	{
       
   148 	MmuLock::Lock();	
       
   149 	if (iPagingDeviceIdle)
       
   150 		{
       
   151 		iIdleForAWhile = ETrue;
       
   152 		if (!iCleaningInProgress && ThePager.HasPagesToClean())
       
   153 			iCleanerDfc.Enque();
       
   154 		}
       
   155 	MmuLock::Unlock();
       
   156 	}
       
   157 
       
   158 void DPageCleaner::CleanerDfcFn(TAny*)
       
   159 	{
       
   160 	ThePageCleaner.TryToClean();
       
   161 	}
       
   162 
       
   163 void DPageCleaner::TryToClean()
       
   164 	{
       
   165 	MmuLock::Lock();
       
   166 	TBool workToDo = iIdleForAWhile && ThePager.HasPagesToClean();
       
   167 	iCleaningInProgress = workToDo;
       
   168 	MmuLock::Unlock();
       
   169 	
       
   170 	if (!workToDo)
       
   171 		{
       
   172 		PAGE_CLEANER_TRACE("PageCleaner - started but no work to do");
       
   173 		return;
       
   174 		}
       
   175 
       
   176 	for (;;)
       
   177 		{
       
   178 		PageCleaningLock::Lock();
       
   179 		MmuLock::Lock();
       
   180 		if (!iIdleForAWhile)
       
   181 			break;
       
   182 		TInt attempted = ThePager.CleanSomePages(ETrue);
       
   183 		if (attempted == 0)
       
   184 			break;
       
   185 		PAGE_CLEANER_TRACE("PageCleaner - attempted to clean %d pages", attempted);
       
   186 		MmuLock::Unlock();
       
   187 		PageCleaningLock::Unlock();
       
   188 		}
       
   189 	
       
   190 	if (iIdleForAWhile)
       
   191 		PAGE_CLEANER_TRACE("PageCleaner - no more pages to clean");
       
   192 	else
       
   193 		PAGE_CLEANER_TRACE("PageCleaner - device now busy");
       
   194 	
       
   195 	iCleaningInProgress = EFalse;
       
   196 	MmuLock::Unlock();
       
   197 	PageCleaningLock::Unlock();
       
   198 	}
       
   199 
       
   200 void PageCleaner::Start()
       
   201 	{
       
   202 	ThePageCleaner.Start();
       
   203 	}
       
   204 
       
   205 void PageCleaner::NotifyPagesToClean()
       
   206 	{
       
   207 	ThePageCleaner.NotifyPagesToClean();
       
   208 	}
       
   209 
       
   210 EXPORT_C void DPagingDevice::NotifyIdle()
       
   211 	{
       
   212 	ThePageCleaner.NotifyPagingDeviceIdle();
       
   213 	}
       
   214 
       
   215 EXPORT_C void DPagingDevice::NotifyBusy()
       
   216 	{
       
   217 	ThePageCleaner.NotifyPagingDeviceBusy();
       
   218 	}
       
   219 
       
   220 #else  // __PAGING_PRE_CLEAN_DIRTY_PAGES not defined
       
   221 
       
   222 void PageCleaner::Start()
       
   223 	{
       
   224 	}
       
   225 
       
   226 void PageCleaner::NotifyPagesToClean()
       
   227 	{
       
   228 	}
       
   229 
       
   230 EXPORT_C void DPagingDevice::NotifyIdle()
       
   231 	{
       
   232 	}
       
   233 
       
   234 EXPORT_C void DPagingDevice::NotifyBusy()
       
   235 	{
       
   236 	}
       
   237 
       
   238 #endif
       
   239 
       
   240 EXPORT_C NFastMutex* DPagingDevice::NotificationLock()
       
   241 	{
       
   242 	// use the MmuLock
       
   243 	return &MmuLock::iLock;
       
   244 	}