Update contrib.
1 // Copyright (c) 2008-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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // Other possible inputs to thrashing detection:
15 // - cache size and free ram
21 #include <kernel/kern_priv.h>
24 const TInt KUpdatePeriod = 1000; // Update every second
26 DThrashMonitor TheThrashMonitor;
28 DThrashMonitor::DThrashMonitor() :
30 iUpdateTimer(NULL, this),
31 iUpdateDfc(UpdateDfcFunc, this, 0),
33 iThresholdThrashing(200),
38 void DThrashMonitor::Start()
40 TBool alreadyRunning = __e32_atomic_swp_ord32(&iRunning, ETrue);
45 memclr(&iCount[0], sizeof(iCount));
46 iLastUpdateTime = NKern::TickCount();
48 iUpdateDfc.SetDfcQ(Kern::DfcQue0());
49 TInt r = iUpdateTimer.OneShot(KUpdatePeriod, iUpdateDfc);
50 __NK_ASSERT_ALWAYS(r == KErrNone);
53 TInt DThrashMonitor::ThrashLevel()
58 TInt DThrashMonitor::SetThresholds(TUint aThrashing, TUint aGood)
60 if (aThrashing < aGood || aThrashing > 255)
62 iThresholdThrashing = aThrashing;
63 iThresholdGood = aGood;
67 void DThrashMonitor::UpdateCount(TCount aCount, TInt aDelta)
69 TCountData& c = iCount[aCount];
71 NKern::FMWait(&iMutex);
73 TUint32 currentTime = NKern::TickCount();
74 c.iTotal += (TInt64)c.iCount * (currentTime - c.iLastUpdateTime);
76 c.iLastUpdateTime = currentTime;
78 if(!iUpdateTimer.IsPending())
80 TInt r = iUpdateTimer.OneShot(KUpdatePeriod, iUpdateDfc);
81 __NK_ASSERT_ALWAYS(r == KErrNone);
84 NKern::FMSignal(&iMutex);
86 __NK_ASSERT_DEBUG(c.iCount >= 0);
89 void DThrashMonitor::NotifyStartPaging()
91 UpdateCount(ECountThreadsPaging, 1);
94 void DThrashMonitor::NotifyEndPaging()
96 UpdateCount(ECountThreadsPaging, -1);
99 void DThrashMonitor::UpdateDfcFunc(TAny* aPtr)
101 DThrashMonitor* self = (DThrashMonitor*)aPtr;
102 self->RecalculateThrashLevel();
105 void DThrashMonitor::RecalculateThrashLevel()
107 TInt currentTime = NKern::TickCount();
108 TInt elapsedTicks = currentTime - iLastUpdateTime;
110 NKern::FMWait(&iMutex);
111 for (TInt i = 0 ; i < EMaxCount ; ++i)
113 TCountData& c = iCount[i];
114 c.iTotal += (TInt64)c.iCount * (currentTime - c.iLastUpdateTime);
115 c.iAverage = (TInt)((256 * c.iTotal) / elapsedTicks);
117 c.iLastUpdateTime = currentTime;
119 NKern::FMSignal(&iMutex);
121 TInt pagingActivity = Min(iCount[ECountThreadsPaging].iAverage, 255);
123 // Base thrash level entirely on the average number of threads paging
124 TInt newThrashLevel = pagingActivity;
125 TInt oldThrashLevel = iThrashLevel;
127 // Make thrash level increase slowly over time, but decrease quickly
128 if (newThrashLevel > oldThrashLevel)
129 newThrashLevel = (3 * oldThrashLevel + pagingActivity) >> 2;
131 iThrashLevel = newThrashLevel;
133 // Notify user-side if thrashing thresholds passed
134 TBool notifyChange = EFalse;
135 if (oldThrashLevel < iThresholdThrashing && newThrashLevel >= iThresholdThrashing)
137 iIsThrashing = ETrue;
138 notifyChange = ETrue;
140 else if (iIsThrashing && oldThrashLevel >= iThresholdGood && newThrashLevel < iThresholdGood)
142 iIsThrashing = EFalse;
143 notifyChange = ETrue;
148 NKern::ThreadEnterCS();
149 Kern::AsyncNotifyChanges(EChangesThrashLevel);
150 NKern::ThreadLeaveCS();
153 iLastUpdateTime = currentTime;
155 if(iThrashLevel != 0)
157 NKern::FMWait(&iMutex);
158 if(!iUpdateTimer.IsPending())
160 TInt r = iUpdateTimer.Again(KUpdatePeriod);
161 if (r == KErrArgument)
163 r = iUpdateTimer.OneShot(KUpdatePeriod, iUpdateDfc);
165 __NK_ASSERT_ALWAYS(r == KErrNone);
167 NKern::FMSignal(&iMutex);