os/kernelhwsrv/kernel/eka/memmodel/epoc/flexible/mmu/mthrash.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
// Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of the License "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
// Other possible inputs to thrashing detection:
sl@0
    15
// - cache size and free ram
sl@0
    16
// - pin failures
sl@0
    17
//
sl@0
    18
sl@0
    19
sl@0
    20
sl@0
    21
#include <kernel/kern_priv.h>
sl@0
    22
#include "mthrash.h"
sl@0
    23
sl@0
    24
const TInt KUpdatePeriod = 1000;   // Update every second
sl@0
    25
sl@0
    26
DThrashMonitor TheThrashMonitor;
sl@0
    27
sl@0
    28
DThrashMonitor::DThrashMonitor() :
sl@0
    29
	iRunning(EFalse),
sl@0
    30
	iUpdateTimer(NULL, this),
sl@0
    31
	iUpdateDfc(UpdateDfcFunc, this, 0),
sl@0
    32
	iThrashLevel(0),
sl@0
    33
	iThresholdThrashing(200),
sl@0
    34
	iThresholdGood(150)
sl@0
    35
	{
sl@0
    36
	}
sl@0
    37
sl@0
    38
void DThrashMonitor::Start()
sl@0
    39
	{
sl@0
    40
	TBool alreadyRunning = __e32_atomic_swp_ord32(&iRunning, ETrue);
sl@0
    41
	if (alreadyRunning)
sl@0
    42
		return;
sl@0
    43
	
sl@0
    44
	// reset
sl@0
    45
	memclr(&iCount[0], sizeof(iCount));
sl@0
    46
	iLastUpdateTime = NKern::TickCount();
sl@0
    47
	
sl@0
    48
	iUpdateDfc.SetDfcQ(Kern::DfcQue0());
sl@0
    49
	TInt r = iUpdateTimer.OneShot(KUpdatePeriod, iUpdateDfc);
sl@0
    50
	__NK_ASSERT_ALWAYS(r == KErrNone);
sl@0
    51
	}
sl@0
    52
sl@0
    53
TInt DThrashMonitor::ThrashLevel()
sl@0
    54
	{
sl@0
    55
	return iThrashLevel;
sl@0
    56
	}
sl@0
    57
sl@0
    58
TInt DThrashMonitor::SetThresholds(TUint aThrashing, TUint aGood)
sl@0
    59
	{
sl@0
    60
	if (aThrashing < aGood || aThrashing > 255)
sl@0
    61
		return KErrArgument;
sl@0
    62
	iThresholdThrashing = aThrashing;
sl@0
    63
	iThresholdGood = aGood;
sl@0
    64
	return KErrNone;
sl@0
    65
	}
sl@0
    66
sl@0
    67
void DThrashMonitor::UpdateCount(TCount aCount, TInt aDelta)
sl@0
    68
	{
sl@0
    69
	TCountData& c = iCount[aCount];
sl@0
    70
sl@0
    71
	NKern::FMWait(&iMutex);
sl@0
    72
	
sl@0
    73
	TUint32 currentTime = NKern::TickCount();
sl@0
    74
	c.iTotal += (TInt64)c.iCount * (currentTime - c.iLastUpdateTime);
sl@0
    75
	c.iCount += aDelta;
sl@0
    76
	c.iLastUpdateTime = currentTime;
sl@0
    77
	
sl@0
    78
	if(!iUpdateTimer.IsPending())	
sl@0
    79
		{
sl@0
    80
		TInt r = iUpdateTimer.OneShot(KUpdatePeriod, iUpdateDfc);
sl@0
    81
		__NK_ASSERT_ALWAYS(r == KErrNone);
sl@0
    82
		}
sl@0
    83
	
sl@0
    84
	NKern::FMSignal(&iMutex);
sl@0
    85
	
sl@0
    86
	__NK_ASSERT_DEBUG(c.iCount >= 0);
sl@0
    87
	}
sl@0
    88
sl@0
    89
void DThrashMonitor::NotifyStartPaging()
sl@0
    90
	{
sl@0
    91
	UpdateCount(ECountThreadsPaging, 1);
sl@0
    92
	}
sl@0
    93
sl@0
    94
void DThrashMonitor::NotifyEndPaging()
sl@0
    95
	{
sl@0
    96
	UpdateCount(ECountThreadsPaging, -1);
sl@0
    97
	}
sl@0
    98
sl@0
    99
void DThrashMonitor::UpdateDfcFunc(TAny* aPtr)
sl@0
   100
	{
sl@0
   101
	DThrashMonitor* self = (DThrashMonitor*)aPtr;
sl@0
   102
	self->RecalculateThrashLevel();
sl@0
   103
	}
sl@0
   104
sl@0
   105
void DThrashMonitor::RecalculateThrashLevel()
sl@0
   106
	{
sl@0
   107
	TInt currentTime = NKern::TickCount();
sl@0
   108
	TInt elapsedTicks = currentTime - iLastUpdateTime;
sl@0
   109
sl@0
   110
	NKern::FMWait(&iMutex);
sl@0
   111
	for (TInt i = 0 ; i < EMaxCount ; ++i)
sl@0
   112
		{
sl@0
   113
		TCountData& c = iCount[i];
sl@0
   114
		c.iTotal += (TInt64)c.iCount * (currentTime - c.iLastUpdateTime);
sl@0
   115
		c.iAverage = (TInt)((256 * c.iTotal) / elapsedTicks);
sl@0
   116
		c.iTotal = 0;
sl@0
   117
		c.iLastUpdateTime = currentTime;
sl@0
   118
		}
sl@0
   119
	NKern::FMSignal(&iMutex);
sl@0
   120
sl@0
   121
	TInt pagingActivity = Min(iCount[ECountThreadsPaging].iAverage, 255);
sl@0
   122
sl@0
   123
	// Base thrash level entirely on the average number of threads paging
sl@0
   124
	TInt newThrashLevel = pagingActivity;
sl@0
   125
	TInt oldThrashLevel = iThrashLevel;
sl@0
   126
sl@0
   127
	// Make thrash level increase slowly over time, but decrease quickly
sl@0
   128
	if (newThrashLevel > oldThrashLevel)
sl@0
   129
		newThrashLevel = (3 * oldThrashLevel + pagingActivity) >> 2;
sl@0
   130
sl@0
   131
	iThrashLevel = newThrashLevel;
sl@0
   132
	
sl@0
   133
	// Notify user-side if thrashing thresholds passed
sl@0
   134
	TBool notifyChange = EFalse;
sl@0
   135
	if (oldThrashLevel < iThresholdThrashing && newThrashLevel >= iThresholdThrashing)
sl@0
   136
		{
sl@0
   137
		iIsThrashing = ETrue;
sl@0
   138
		notifyChange = ETrue;
sl@0
   139
		}
sl@0
   140
	else if (iIsThrashing && oldThrashLevel >= iThresholdGood && newThrashLevel < iThresholdGood)
sl@0
   141
		{
sl@0
   142
		iIsThrashing = EFalse;
sl@0
   143
		notifyChange = ETrue;
sl@0
   144
		}
sl@0
   145
	
sl@0
   146
	if (notifyChange)
sl@0
   147
		{
sl@0
   148
		NKern::ThreadEnterCS();
sl@0
   149
		Kern::AsyncNotifyChanges(EChangesThrashLevel);
sl@0
   150
		NKern::ThreadLeaveCS();
sl@0
   151
		}
sl@0
   152
	
sl@0
   153
	iLastUpdateTime = currentTime;
sl@0
   154
	
sl@0
   155
	if(iThrashLevel != 0)
sl@0
   156
	    {
sl@0
   157
	    NKern::FMWait(&iMutex);
sl@0
   158
	    if(!iUpdateTimer.IsPending())
sl@0
   159
	        {
sl@0
   160
            TInt r = iUpdateTimer.Again(KUpdatePeriod);
sl@0
   161
            if (r == KErrArgument)
sl@0
   162
                {
sl@0
   163
                r = iUpdateTimer.OneShot(KUpdatePeriod, iUpdateDfc);  
sl@0
   164
                }
sl@0
   165
            __NK_ASSERT_ALWAYS(r == KErrNone);
sl@0
   166
	        }
sl@0
   167
        NKern::FMSignal(&iMutex);    
sl@0
   168
        
sl@0
   169
	    }
sl@0
   170
	
sl@0
   171
	}