1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/kernelhwsrv/kerneltest/e32test/demandpaging/d_pagestress.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,465 @@
1.4 +// Copyright (c) 2005-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 +// e32test\demandpaging\d_pagestrss.cpp
1.18 +//
1.19 +//
1.20 +
1.21 +#include <kernel/kern_priv.h>
1.22 +#include <kernel/cache.h>
1.23 +#include "d_pagestress.h"
1.24 +
1.25 +//
1.26 +// Class definitions
1.27 +//
1.28 +
1.29 +class DPageStressTestFactory : public DLogicalDevice
1.30 + {
1.31 +public:
1.32 + ~DPageStressTestFactory();
1.33 + virtual TInt Install();
1.34 + virtual void GetCaps(TDes8& aDes) const;
1.35 + virtual TInt Create(DLogicalChannelBase*& aChannel);
1.36 + };
1.37 +
1.38 +class DPageStressTestChannel : public DLogicalChannelBase
1.39 + {
1.40 +public:
1.41 + DPageStressTestChannel();
1.42 + ~DPageStressTestChannel();
1.43 + virtual TInt DoCreate(TInt aUnit, const TDesC8* anInfo, const TVersion& aVer);
1.44 + virtual TInt Request(TInt aFunction, TAny* a1, TAny* a2);
1.45 +
1.46 + TInt DoConsumeRamSetup(TInt aNumPagesLeft, TInt aPagesInBlock);
1.47 + TInt DoConsumeRamFinish(void);
1.48 + TInt DoConsumeSomeRam(TInt aBlocks);
1.49 + TInt DoReleaseSomeRam(TInt aBlocks);
1.50 + TInt FreeRam();
1.51 + TInt DoSetDebugFlag(TInt aState);
1.52 +public:
1.53 + DPageStressTestFactory* iFactory;
1.54 +
1.55 +private:
1.56 + TBool iRamAllocd;
1.57 + TInt iInitialFreeRam;
1.58 + TInt iTotalBlocks;
1.59 + TInt iBlockSize;
1.60 + TInt iLastBlockAllocd;
1.61 + TPhysAddr* iAddrArray;
1.62 + TInt iDebug;
1.63 + DMutex* iMutex;
1.64 + TInt iThreadCounter;
1.65 + TPhysAddr iAddrMin;
1.66 + TPhysAddr iAddrMax;
1.67 + TBool iDemandPaging;
1.68 + };
1.69 +
1.70 +//
1.71 +// DPageStressTestFactory
1.72 +//
1.73 +
1.74 +TInt DPageStressTestFactory::Install()
1.75 + {
1.76 + return SetName(&KPageStressTestLddName);
1.77 + }
1.78 +
1.79 +DPageStressTestFactory::~DPageStressTestFactory()
1.80 + {
1.81 + }
1.82 +
1.83 +void DPageStressTestFactory::GetCaps(TDes8& /*aDes*/) const
1.84 + {
1.85 + // Not used but required as DLogicalDevice::GetCaps is pure virtual
1.86 + }
1.87 +
1.88 +TInt DPageStressTestFactory::Create(DLogicalChannelBase*& aChannel)
1.89 + {
1.90 + aChannel = NULL;
1.91 + DPageStressTestChannel* channel=new DPageStressTestChannel;
1.92 + if(!channel)
1.93 + return KErrNoMemory;
1.94 + channel->iFactory = this;
1.95 + aChannel = channel;
1.96 + return KErrNone;
1.97 + }
1.98 +
1.99 +DECLARE_STANDARD_LDD()
1.100 + {
1.101 + return new DPageStressTestFactory;
1.102 + }
1.103 +
1.104 +//
1.105 +// DPageStressTestChannel
1.106 +//
1.107 +
1.108 +TInt DPageStressTestChannel::DoCreate(TInt /*aUnit*/, const TDesC8* /*aInfo*/, const TVersion& /*aVer*/)
1.109 + {
1.110 + return KErrNone;
1.111 + }
1.112 +
1.113 +DPageStressTestChannel::DPageStressTestChannel()
1.114 + : iRamAllocd(EFalse), iInitialFreeRam(0), iTotalBlocks(0), iBlockSize(0), iLastBlockAllocd(0),
1.115 + iAddrArray(NULL), iDebug(0), iThreadCounter(1), iAddrMin(0), iAddrMax(0), iDemandPaging(ETrue)
1.116 + {
1.117 + _LIT(KMutexName,"TPageStressMutex");
1.118 + NKern::ThreadEnterCS();
1.119 + Kern::MutexCreate(iMutex, KMutexName,KMutexOrdNone);
1.120 + NKern::ThreadLeaveCS();
1.121 + SVMCacheInfo tempPages;
1.122 + if (Kern::HalFunction(EHalGroupVM,EVMHalGetCacheSize,&tempPages,0) != KErrNone)
1.123 + {
1.124 + iDemandPaging = EFalse;
1.125 + }
1.126 + }
1.127 +
1.128 +DPageStressTestChannel::~DPageStressTestChannel()
1.129 + {
1.130 + if (iRamAllocd)
1.131 + {
1.132 + DoConsumeRamFinish();
1.133 + }
1.134 + if (iMutex)
1.135 + {
1.136 + iMutex->Close(NULL);
1.137 + iMutex = NULL;
1.138 + }
1.139 + }
1.140 +
1.141 +TInt DPageStressTestChannel::Request(TInt aFunction, TAny* a1, TAny* a2)
1.142 + {
1.143 + TInt threadCount = __e32_atomic_tas_ord32(&iThreadCounter, 1, 1, 0);
1.144 + if (threadCount >= 2)
1.145 + {
1.146 + Kern::Printf("DPageStressTestChannel::Request threadCount = %d\n", threadCount);
1.147 + }
1.148 + NKern::ThreadEnterCS();
1.149 + Kern::MutexWait(*iMutex);
1.150 + TInt retVal = KErrNotSupported;
1.151 + switch(aFunction)
1.152 + {
1.153 + case RPageStressTestLdd::EDoConsumeRamSetup:
1.154 + {
1.155 + retVal = DPageStressTestChannel::DoConsumeRamSetup((TInt)a1, (TInt)a2);
1.156 + }
1.157 + break;
1.158 +
1.159 + case RPageStressTestLdd::EDoConsumeRamFinish:
1.160 + {
1.161 + retVal = DPageStressTestChannel::DoConsumeRamFinish();
1.162 + }
1.163 + break;
1.164 +
1.165 + case RPageStressTestLdd::EDoConsumeSomeRam:
1.166 + {
1.167 + retVal = DPageStressTestChannel::DoConsumeSomeRam((TInt)a1);
1.168 + }
1.169 + break;
1.170 +
1.171 + case RPageStressTestLdd::EDoReleaseSomeRam:
1.172 + {
1.173 + retVal = DPageStressTestChannel::DoReleaseSomeRam((TInt)a1);
1.174 + }
1.175 + break;
1.176 +
1.177 + case RPageStressTestLdd::EDoSetDebugFlag:
1.178 + {
1.179 + retVal = DoSetDebugFlag((TInt) a1);
1.180 + }
1.181 + break;
1.182 +
1.183 + default: break;
1.184 + }
1.185 + Kern::MutexSignal(*iMutex);
1.186 + NKern::ThreadLeaveCS();
1.187 + __e32_atomic_tas_ord32(&iThreadCounter, 1, -1, 0);
1.188 + return retVal;
1.189 + }
1.190 +
1.191 +//
1.192 +// DPageStressTestChannel::DoConsumeRamSetup
1.193 +//
1.194 +// This test attempts to consume most of the available Contiguous Ram until we need to ask the
1.195 +// demand paging code to release memory for it.
1.196 +//
1.197 +//
1.198 +//
1.199 +#define CHECK(c) { if(!(c)) { Kern::Printf("Fail %d", __LINE__); ; retVal = __LINE__;} }
1.200 +
1.201 +TInt DPageStressTestChannel::DoConsumeRamSetup(TInt aNumPagesLeft, TInt aPagesInBlock)
1.202 + {
1.203 + if (iRamAllocd)
1.204 + {
1.205 + Kern::Printf("DPageStressTestChannel trying to start again when RAM alloc'd\n");
1.206 + DoConsumeRamFinish();
1.207 + }
1.208 +
1.209 + TInt retVal = KErrNone;
1.210 +
1.211 + TInt pageSize = 0;
1.212 + CHECK(Kern::HalFunction(EHalGroupKernel,EKernelHalPageSizeInBytes,&pageSize,0)==KErrNone);
1.213 +
1.214 + iBlockSize = aPagesInBlock * pageSize;
1.215 + iInitialFreeRam = FreeRam();
1.216 + iTotalBlocks = (iInitialFreeRam/iBlockSize) + 10;
1.217 +
1.218 + iAddrArray = (TPhysAddr *)Kern::AllocZ(sizeof(TPhysAddr) * iTotalBlocks);
1.219 +
1.220 + CHECK(iAddrArray);
1.221 + if(!iAddrArray)
1.222 + {
1.223 + return KErrNoMemory;
1.224 + }
1.225 +
1.226 + SVMCacheInfo tempPages;
1.227 +
1.228 + iRamAllocd = ETrue;
1.229 +
1.230 + // get the initial free ram again as the heap may have grabbed a page during the alloc
1.231 + iInitialFreeRam = FreeRam();
1.232 +
1.233 +
1.234 + if (iDemandPaging)
1.235 + {
1.236 + CHECK(Kern::HalFunction(EHalGroupVM,EVMHalGetCacheSize,&tempPages,0) == KErrNone);
1.237 + if (iDebug)
1.238 + {
1.239 + Kern::Printf("Start : min %d max %d current %d maxFree %d freeRam %d",
1.240 + tempPages.iMinSize, tempPages.iMaxSize, tempPages.iCurrentSize ,tempPages.iMaxFreeSize, FreeRam());
1.241 + }
1.242 + }
1.243 + // allocate blocks to use up RAM until we fail to allocate any further...
1.244 + TInt index;
1.245 + // only alloc upto the point where we have a number of pages left.
1.246 + iLastBlockAllocd = iTotalBlocks - (aNumPagesLeft / aPagesInBlock);
1.247 + for (index = 0; index < iLastBlockAllocd; index ++)
1.248 + {
1.249 + if (iDemandPaging)
1.250 + {
1.251 + CHECK(Kern::HalFunction(EHalGroupVM,EVMHalGetCacheSize,&tempPages,0) == KErrNone);
1.252 + }
1.253 +
1.254 + if (KErrNone != Epoc::AllocPhysicalRam(iBlockSize, iAddrArray[index], 0))
1.255 + {
1.256 + iAddrArray[index] = NULL;
1.257 + break;
1.258 + }
1.259 +
1.260 + if ((iAddrMin == 0) || (iAddrMin > iAddrArray[index]))
1.261 + {
1.262 + iAddrMin = iAddrArray[index];
1.263 + }
1.264 +
1.265 + if (iAddrMax < iAddrArray[index])
1.266 + {
1.267 + iAddrMax = iAddrArray[index];
1.268 + }
1.269 +
1.270 + if (iDemandPaging)
1.271 + {
1.272 + CHECK(Kern::HalFunction(EHalGroupVM,EVMHalGetCacheSize,&tempPages,0) == KErrNone);
1.273 + }
1.274 + }
1.275 + iLastBlockAllocd = index - 1;
1.276 +
1.277 + if (iDemandPaging)
1.278 + {
1.279 + CHECK(Kern::HalFunction(EHalGroupVM,EVMHalGetCacheSize,&tempPages,0) == KErrNone);
1.280 + if (iDebug)
1.281 + {
1.282 + Kern::Printf("Alloc'd : min %d max %d current %d maxFree %d freeRam %d lastIndex %d addrMin 0x%08x addrMax 0x%08x",
1.283 + tempPages.iMinSize, tempPages.iMaxSize, tempPages.iCurrentSize ,tempPages.iMaxFreeSize, FreeRam(), iLastBlockAllocd, iAddrMin, iAddrMax);
1.284 + }
1.285 + }
1.286 + return retVal;
1.287 + }
1.288 +
1.289 +TInt DPageStressTestChannel::DoConsumeRamFinish(void)
1.290 + {
1.291 + TInt retVal = KErrNone;
1.292 + if (iRamAllocd)
1.293 + {
1.294 + SVMCacheInfo tempPages;
1.295 +
1.296 + if (iDemandPaging)
1.297 + {
1.298 + CHECK(Kern::HalFunction(EHalGroupVM,EVMHalGetCacheSize,&tempPages,0) == KErrNone);
1.299 + if (iDebug)
1.300 + {
1.301 + Kern::Printf("Cleanup cache info: iMinSize %d iMaxSize %d iCurrentSize %d iMaxFreeSize %d",
1.302 + tempPages.iMinSize, tempPages.iMaxSize, tempPages.iCurrentSize ,tempPages.iMaxFreeSize);
1.303 + }
1.304 + }
1.305 + TInt index = iTotalBlocks - 1;
1.306 +
1.307 + TBool firstTime = ETrue;
1.308 +
1.309 + // free the memory we allocated...
1.310 + while(index >= 0)
1.311 + {
1.312 + if (iAddrArray[index])
1.313 + {
1.314 + if ((iAddrArray[index] < iAddrMin) || (iAddrArray[index] > iAddrMax))
1.315 + {
1.316 + Kern::Printf("ERROR: DoConsumeRamFinish : index %d addr 0x%08x min 0x%08x max 0x%08x\n : firstTime %d iLastBlockAllocd %d iTotalBlock %d", index, iAddrArray[index], iAddrMin, iAddrMax, firstTime, iLastBlockAllocd, iTotalBlocks);
1.317 + TInt tempIndex = index - 8;
1.318 + while (tempIndex < (index + 8))
1.319 + {
1.320 + Kern::Printf(" --> index %d addr 0x%08x", tempIndex, iAddrArray[tempIndex]);
1.321 + tempIndex ++;
1.322 + }
1.323 +
1.324 + iAddrArray[index] = NULL;
1.325 + }
1.326 + else
1.327 + {
1.328 + TInt r = Epoc::FreePhysicalRam(iAddrArray[index], iBlockSize);
1.329 + iAddrArray[index] = NULL;
1.330 + CHECK(r==KErrNone);
1.331 + }
1.332 + }
1.333 +
1.334 + firstTime = EFalse;
1.335 + --index;
1.336 + }
1.337 +
1.338 + if (iDebug)
1.339 + {
1.340 + Kern::Printf("DoConsumeRamFinish : FreeRam Initial 0x%x now 0x%x",iInitialFreeRam, FreeRam());
1.341 + }
1.342 + //CHECK(FreeRam() == iInitialFreeRam)
1.343 + if (FreeRam() != iInitialFreeRam)
1.344 + {
1.345 + Kern::Printf("DoConsumeRamFinish : FreeRam Initial 0x%x now 0x%x NOT EQUAL!",iInitialFreeRam, FreeRam());
1.346 + }
1.347 +
1.348 + Kern::Free(iAddrArray);
1.349 + iAddrArray = NULL;
1.350 + iAddrMin = 0;
1.351 + iAddrMax = 0;
1.352 + iLastBlockAllocd = -1;
1.353 + iTotalBlocks = 0;
1.354 + iRamAllocd = EFalse;
1.355 +
1.356 + if (iDemandPaging)
1.357 + {
1.358 + CHECK(Kern::HalFunction(EHalGroupVM,EVMHalGetCacheSize,&tempPages,0) == KErrNone);
1.359 + if (iDebug)
1.360 + {
1.361 + Kern::Printf("End cache info: iMinSize %d iMaxSize %d iCurrentSize %d iMaxFreeSize %d",
1.362 + tempPages.iMinSize, tempPages.iMaxSize, tempPages.iCurrentSize ,tempPages.iMaxFreeSize);
1.363 + }
1.364 + }
1.365 + }
1.366 + return retVal;
1.367 + }
1.368 +
1.369 +TInt DPageStressTestChannel::DoConsumeSomeRam(TInt aBlocks)
1.370 + {
1.371 + TInt retVal = KErrNone;
1.372 +
1.373 + return retVal;
1.374 + }
1.375 +
1.376 +TInt DPageStressTestChannel::DoReleaseSomeRam(TInt aBlocks)
1.377 + {
1.378 + if(iLastBlockAllocd<0)
1.379 + return KErrUnderflow;
1.380 +
1.381 + TInt retVal = KErrNone;
1.382 + if (iRamAllocd)
1.383 + {
1.384 + SVMCacheInfo tempPages;
1.385 +
1.386 + if (iDemandPaging)
1.387 + {
1.388 + CHECK(Kern::HalFunction(EHalGroupVM,EVMHalGetCacheSize,&tempPages,0) == KErrNone);
1.389 + if (iDebug)
1.390 + {
1.391 + Kern::Printf("Release : min %d max %d current %d maxFree %d freeRam %d lastIndex %d",
1.392 + tempPages.iMinSize, tempPages.iMaxSize, tempPages.iCurrentSize ,tempPages.iMaxFreeSize, FreeRam(), iLastBlockAllocd);
1.393 + }
1.394 + }
1.395 +
1.396 + TInt index = iLastBlockAllocd;
1.397 + iLastBlockAllocd -= aBlocks;
1.398 +
1.399 + TBool firstTime = ETrue;
1.400 +
1.401 + // free the memory we allocated...
1.402 + while((index >= 0) && (index > iLastBlockAllocd))
1.403 + {
1.404 + if (iAddrArray[index])
1.405 + {
1.406 + //Kern::Printf("DoReleaseSomeRam : index %d addr 0x%08x size %d", index, iAddrArray[index], iBlockSize);
1.407 + if ((iAddrArray[index] < iAddrMin) || (iAddrArray[index] > iAddrMax))
1.408 + {
1.409 + Kern::Printf("ERROR: DoReleaseSomeRam : index %d addr 0x%08x min 0x%08x max 0x%08x\n : firstTime %d iLastBlockAllocd %d iTotalBlock %d", index, iAddrArray[index], iAddrMin, iAddrMax, firstTime, iLastBlockAllocd + aBlocks, iTotalBlocks);
1.410 + TInt tempIndex = index - 8;
1.411 + while (tempIndex < (index + 8))
1.412 + {
1.413 + Kern::Printf(" --> index %d addr 0x%08x", tempIndex, iAddrArray[tempIndex]);
1.414 + tempIndex ++;
1.415 + }
1.416 +
1.417 + iAddrArray[index] = NULL;
1.418 + }
1.419 + else
1.420 + {
1.421 + TInt r = Epoc::FreePhysicalRam(iAddrArray[index], iBlockSize);
1.422 + iAddrArray[index] = NULL;
1.423 + CHECK(r==KErrNone);
1.424 + }
1.425 + }
1.426 + else
1.427 + {
1.428 + Kern::Printf("ERROR: DoReleaseSomeRam : trying to free NULL index %d", index, iAddrArray[index], iAddrMin, iAddrMax);
1.429 + TInt tempIndex = index - 8;
1.430 + while (tempIndex < (index + 8))
1.431 + {
1.432 + Kern::Printf(" --> index %d addr 0x%08x", tempIndex, iAddrArray[tempIndex]);
1.433 + tempIndex ++;
1.434 + }
1.435 +
1.436 + }
1.437 + firstTime = EFalse;
1.438 + --index;
1.439 + }
1.440 + if (index <= 0)
1.441 + {
1.442 + Kern::Printf("WARNING : DoReleaseSomeRam : index %d !!!!!", index);
1.443 + }
1.444 +
1.445 + if (iDemandPaging)
1.446 + {
1.447 + CHECK(Kern::HalFunction(EHalGroupVM,EVMHalGetCacheSize,&tempPages,0) == KErrNone);
1.448 + if (iDebug)
1.449 + {
1.450 + Kern::Printf("Released : min %d max %d current %d maxFree %d freeRam %d lastIndex %d",
1.451 + tempPages.iMinSize, tempPages.iMaxSize, tempPages.iCurrentSize ,tempPages.iMaxFreeSize, FreeRam(), iLastBlockAllocd);
1.452 + }
1.453 + }
1.454 + }
1.455 + return retVal;
1.456 + }
1.457 +
1.458 +
1.459 +TInt DPageStressTestChannel::FreeRam()
1.460 + {
1.461 + return Kern::FreeRamInBytes();
1.462 + }
1.463 +
1.464 +TInt DPageStressTestChannel::DoSetDebugFlag(TInt aState)
1.465 + {
1.466 + iDebug = aState;
1.467 + return KErrNone;
1.468 + }