os/kernelhwsrv/kernel/eka/memmodel/epoc/flexible/mmu/mthrash.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/kernelhwsrv/kernel/eka/memmodel/epoc/flexible/mmu/mthrash.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,171 @@
     1.4 +// Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of the License "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +// Other possible inputs to thrashing detection:
    1.18 +// - cache size and free ram
    1.19 +// - pin failures
    1.20 +//
    1.21 +
    1.22 +
    1.23 +
    1.24 +#include <kernel/kern_priv.h>
    1.25 +#include "mthrash.h"
    1.26 +
    1.27 +const TInt KUpdatePeriod = 1000;   // Update every second
    1.28 +
    1.29 +DThrashMonitor TheThrashMonitor;
    1.30 +
    1.31 +DThrashMonitor::DThrashMonitor() :
    1.32 +	iRunning(EFalse),
    1.33 +	iUpdateTimer(NULL, this),
    1.34 +	iUpdateDfc(UpdateDfcFunc, this, 0),
    1.35 +	iThrashLevel(0),
    1.36 +	iThresholdThrashing(200),
    1.37 +	iThresholdGood(150)
    1.38 +	{
    1.39 +	}
    1.40 +
    1.41 +void DThrashMonitor::Start()
    1.42 +	{
    1.43 +	TBool alreadyRunning = __e32_atomic_swp_ord32(&iRunning, ETrue);
    1.44 +	if (alreadyRunning)
    1.45 +		return;
    1.46 +	
    1.47 +	// reset
    1.48 +	memclr(&iCount[0], sizeof(iCount));
    1.49 +	iLastUpdateTime = NKern::TickCount();
    1.50 +	
    1.51 +	iUpdateDfc.SetDfcQ(Kern::DfcQue0());
    1.52 +	TInt r = iUpdateTimer.OneShot(KUpdatePeriod, iUpdateDfc);
    1.53 +	__NK_ASSERT_ALWAYS(r == KErrNone);
    1.54 +	}
    1.55 +
    1.56 +TInt DThrashMonitor::ThrashLevel()
    1.57 +	{
    1.58 +	return iThrashLevel;
    1.59 +	}
    1.60 +
    1.61 +TInt DThrashMonitor::SetThresholds(TUint aThrashing, TUint aGood)
    1.62 +	{
    1.63 +	if (aThrashing < aGood || aThrashing > 255)
    1.64 +		return KErrArgument;
    1.65 +	iThresholdThrashing = aThrashing;
    1.66 +	iThresholdGood = aGood;
    1.67 +	return KErrNone;
    1.68 +	}
    1.69 +
    1.70 +void DThrashMonitor::UpdateCount(TCount aCount, TInt aDelta)
    1.71 +	{
    1.72 +	TCountData& c = iCount[aCount];
    1.73 +
    1.74 +	NKern::FMWait(&iMutex);
    1.75 +	
    1.76 +	TUint32 currentTime = NKern::TickCount();
    1.77 +	c.iTotal += (TInt64)c.iCount * (currentTime - c.iLastUpdateTime);
    1.78 +	c.iCount += aDelta;
    1.79 +	c.iLastUpdateTime = currentTime;
    1.80 +	
    1.81 +	if(!iUpdateTimer.IsPending())	
    1.82 +		{
    1.83 +		TInt r = iUpdateTimer.OneShot(KUpdatePeriod, iUpdateDfc);
    1.84 +		__NK_ASSERT_ALWAYS(r == KErrNone);
    1.85 +		}
    1.86 +	
    1.87 +	NKern::FMSignal(&iMutex);
    1.88 +	
    1.89 +	__NK_ASSERT_DEBUG(c.iCount >= 0);
    1.90 +	}
    1.91 +
    1.92 +void DThrashMonitor::NotifyStartPaging()
    1.93 +	{
    1.94 +	UpdateCount(ECountThreadsPaging, 1);
    1.95 +	}
    1.96 +
    1.97 +void DThrashMonitor::NotifyEndPaging()
    1.98 +	{
    1.99 +	UpdateCount(ECountThreadsPaging, -1);
   1.100 +	}
   1.101 +
   1.102 +void DThrashMonitor::UpdateDfcFunc(TAny* aPtr)
   1.103 +	{
   1.104 +	DThrashMonitor* self = (DThrashMonitor*)aPtr;
   1.105 +	self->RecalculateThrashLevel();
   1.106 +	}
   1.107 +
   1.108 +void DThrashMonitor::RecalculateThrashLevel()
   1.109 +	{
   1.110 +	TInt currentTime = NKern::TickCount();
   1.111 +	TInt elapsedTicks = currentTime - iLastUpdateTime;
   1.112 +
   1.113 +	NKern::FMWait(&iMutex);
   1.114 +	for (TInt i = 0 ; i < EMaxCount ; ++i)
   1.115 +		{
   1.116 +		TCountData& c = iCount[i];
   1.117 +		c.iTotal += (TInt64)c.iCount * (currentTime - c.iLastUpdateTime);
   1.118 +		c.iAverage = (TInt)((256 * c.iTotal) / elapsedTicks);
   1.119 +		c.iTotal = 0;
   1.120 +		c.iLastUpdateTime = currentTime;
   1.121 +		}
   1.122 +	NKern::FMSignal(&iMutex);
   1.123 +
   1.124 +	TInt pagingActivity = Min(iCount[ECountThreadsPaging].iAverage, 255);
   1.125 +
   1.126 +	// Base thrash level entirely on the average number of threads paging
   1.127 +	TInt newThrashLevel = pagingActivity;
   1.128 +	TInt oldThrashLevel = iThrashLevel;
   1.129 +
   1.130 +	// Make thrash level increase slowly over time, but decrease quickly
   1.131 +	if (newThrashLevel > oldThrashLevel)
   1.132 +		newThrashLevel = (3 * oldThrashLevel + pagingActivity) >> 2;
   1.133 +
   1.134 +	iThrashLevel = newThrashLevel;
   1.135 +	
   1.136 +	// Notify user-side if thrashing thresholds passed
   1.137 +	TBool notifyChange = EFalse;
   1.138 +	if (oldThrashLevel < iThresholdThrashing && newThrashLevel >= iThresholdThrashing)
   1.139 +		{
   1.140 +		iIsThrashing = ETrue;
   1.141 +		notifyChange = ETrue;
   1.142 +		}
   1.143 +	else if (iIsThrashing && oldThrashLevel >= iThresholdGood && newThrashLevel < iThresholdGood)
   1.144 +		{
   1.145 +		iIsThrashing = EFalse;
   1.146 +		notifyChange = ETrue;
   1.147 +		}
   1.148 +	
   1.149 +	if (notifyChange)
   1.150 +		{
   1.151 +		NKern::ThreadEnterCS();
   1.152 +		Kern::AsyncNotifyChanges(EChangesThrashLevel);
   1.153 +		NKern::ThreadLeaveCS();
   1.154 +		}
   1.155 +	
   1.156 +	iLastUpdateTime = currentTime;
   1.157 +	
   1.158 +	if(iThrashLevel != 0)
   1.159 +	    {
   1.160 +	    NKern::FMWait(&iMutex);
   1.161 +	    if(!iUpdateTimer.IsPending())
   1.162 +	        {
   1.163 +            TInt r = iUpdateTimer.Again(KUpdatePeriod);
   1.164 +            if (r == KErrArgument)
   1.165 +                {
   1.166 +                r = iUpdateTimer.OneShot(KUpdatePeriod, iUpdateDfc);  
   1.167 +                }
   1.168 +            __NK_ASSERT_ALWAYS(r == KErrNone);
   1.169 +	        }
   1.170 +        NKern::FMSignal(&iMutex);    
   1.171 +        
   1.172 +	    }
   1.173 +	
   1.174 +	}