|
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". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // Other possible inputs to thrashing detection: |
|
15 // - cache size and free ram |
|
16 // - pin failures |
|
17 // |
|
18 |
|
19 |
|
20 |
|
21 #include <kernel/kern_priv.h> |
|
22 #include "mthrash.h" |
|
23 |
|
24 const TInt KUpdatePeriod = 1000; // Update every second |
|
25 |
|
26 DThrashMonitor TheThrashMonitor; |
|
27 |
|
28 DThrashMonitor::DThrashMonitor() : |
|
29 iRunning(EFalse), |
|
30 iUpdateTimer(NULL, this), |
|
31 iUpdateDfc(UpdateDfcFunc, this, 0), |
|
32 iThrashLevel(0), |
|
33 iThresholdThrashing(200), |
|
34 iThresholdGood(150) |
|
35 { |
|
36 } |
|
37 |
|
38 void DThrashMonitor::Start() |
|
39 { |
|
40 TBool alreadyRunning = __e32_atomic_swp_ord32(&iRunning, ETrue); |
|
41 if (alreadyRunning) |
|
42 return; |
|
43 |
|
44 // reset |
|
45 memclr(&iCount[0], sizeof(iCount)); |
|
46 iLastUpdateTime = NKern::TickCount(); |
|
47 |
|
48 iUpdateDfc.SetDfcQ(Kern::DfcQue0()); |
|
49 TInt r = iUpdateTimer.OneShot(KUpdatePeriod, iUpdateDfc); |
|
50 __NK_ASSERT_ALWAYS(r == KErrNone); |
|
51 } |
|
52 |
|
53 TInt DThrashMonitor::ThrashLevel() |
|
54 { |
|
55 return iThrashLevel; |
|
56 } |
|
57 |
|
58 TInt DThrashMonitor::SetThresholds(TUint aThrashing, TUint aGood) |
|
59 { |
|
60 if (aThrashing < aGood || aThrashing > 255) |
|
61 return KErrArgument; |
|
62 iThresholdThrashing = aThrashing; |
|
63 iThresholdGood = aGood; |
|
64 return KErrNone; |
|
65 } |
|
66 |
|
67 void DThrashMonitor::UpdateCount(TCount aCount, TInt aDelta) |
|
68 { |
|
69 TCountData& c = iCount[aCount]; |
|
70 |
|
71 NKern::FMWait(&iMutex); |
|
72 |
|
73 TUint32 currentTime = NKern::TickCount(); |
|
74 c.iTotal += (TInt64)c.iCount * (currentTime - c.iLastUpdateTime); |
|
75 c.iCount += aDelta; |
|
76 c.iLastUpdateTime = currentTime; |
|
77 |
|
78 NKern::FMSignal(&iMutex); |
|
79 |
|
80 __NK_ASSERT_DEBUG(c.iCount >= 0); |
|
81 } |
|
82 |
|
83 void DThrashMonitor::NotifyStartPaging() |
|
84 { |
|
85 UpdateCount(ECountThreadsPaging, 1); |
|
86 } |
|
87 |
|
88 void DThrashMonitor::NotifyEndPaging() |
|
89 { |
|
90 UpdateCount(ECountThreadsPaging, -1); |
|
91 } |
|
92 |
|
93 void DThrashMonitor::UpdateDfcFunc(TAny* aPtr) |
|
94 { |
|
95 DThrashMonitor* self = (DThrashMonitor*)aPtr; |
|
96 self->RecalculateThrashLevel(); |
|
97 } |
|
98 |
|
99 void DThrashMonitor::RecalculateThrashLevel() |
|
100 { |
|
101 TInt currentTime = NKern::TickCount(); |
|
102 TInt elapsedTicks = currentTime - iLastUpdateTime; |
|
103 |
|
104 NKern::FMWait(&iMutex); |
|
105 for (TInt i = 0 ; i < EMaxCount ; ++i) |
|
106 { |
|
107 TCountData& c = iCount[i]; |
|
108 c.iTotal += (TInt64)c.iCount * (currentTime - c.iLastUpdateTime); |
|
109 c.iAverage = (TInt)((256 * c.iTotal) / elapsedTicks); |
|
110 c.iTotal = 0; |
|
111 c.iLastUpdateTime = currentTime; |
|
112 } |
|
113 NKern::FMSignal(&iMutex); |
|
114 |
|
115 TInt pagingActivity = Min(iCount[ECountThreadsPaging].iAverage, 255); |
|
116 |
|
117 // Base thrash level entirely on the average number of threads paging |
|
118 TInt newThrashLevel = pagingActivity; |
|
119 TInt oldThrashLevel = iThrashLevel; |
|
120 |
|
121 // Make thrash level increase slowly over time, but decrease quickly |
|
122 if (newThrashLevel > oldThrashLevel) |
|
123 newThrashLevel = (3 * oldThrashLevel + pagingActivity) >> 2; |
|
124 |
|
125 iThrashLevel = newThrashLevel; |
|
126 |
|
127 // Notify user-side if thrashing thresholds passed |
|
128 TBool notifyChange = EFalse; |
|
129 if (oldThrashLevel < iThresholdThrashing && newThrashLevel >= iThresholdThrashing) |
|
130 { |
|
131 iIsThrashing = ETrue; |
|
132 notifyChange = ETrue; |
|
133 } |
|
134 else if (iIsThrashing && oldThrashLevel >= iThresholdGood && newThrashLevel < iThresholdGood) |
|
135 { |
|
136 iIsThrashing = EFalse; |
|
137 notifyChange = ETrue; |
|
138 } |
|
139 |
|
140 if (notifyChange) |
|
141 { |
|
142 NKern::ThreadEnterCS(); |
|
143 Kern::AsyncNotifyChanges(EChangesThrashLevel); |
|
144 NKern::ThreadLeaveCS(); |
|
145 } |
|
146 |
|
147 iLastUpdateTime = currentTime; |
|
148 TInt r = iUpdateTimer.Again(KUpdatePeriod); |
|
149 if (r == KErrArgument) |
|
150 r = iUpdateTimer.OneShot(KUpdatePeriod, iUpdateDfc); |
|
151 __NK_ASSERT_ALWAYS(r == KErrNone); |
|
152 } |