Update contrib.
1 // Copyright (c) 2005-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 // e32test\demandpaging\d_pagestrss.cpp
18 #include <kernel/kern_priv.h>
19 #include <kernel/cache.h>
20 #include "d_pagestress.h"
26 class DPageStressTestFactory : public DLogicalDevice
29 ~DPageStressTestFactory();
30 virtual TInt Install();
31 virtual void GetCaps(TDes8& aDes) const;
32 virtual TInt Create(DLogicalChannelBase*& aChannel);
35 class DPageStressTestChannel : public DLogicalChannelBase
38 DPageStressTestChannel();
39 ~DPageStressTestChannel();
40 virtual TInt DoCreate(TInt aUnit, const TDesC8* anInfo, const TVersion& aVer);
41 virtual TInt Request(TInt aFunction, TAny* a1, TAny* a2);
43 TInt DoConsumeRamSetup(TInt aNumPagesLeft, TInt aPagesInBlock);
44 TInt DoConsumeRamFinish(void);
45 TInt DoConsumeSomeRam(TInt aBlocks);
46 TInt DoReleaseSomeRam(TInt aBlocks);
48 TInt DoSetDebugFlag(TInt aState);
50 DPageStressTestFactory* iFactory;
57 TInt iLastBlockAllocd;
58 TPhysAddr* iAddrArray;
68 // DPageStressTestFactory
71 TInt DPageStressTestFactory::Install()
73 return SetName(&KPageStressTestLddName);
76 DPageStressTestFactory::~DPageStressTestFactory()
80 void DPageStressTestFactory::GetCaps(TDes8& /*aDes*/) const
82 // Not used but required as DLogicalDevice::GetCaps is pure virtual
85 TInt DPageStressTestFactory::Create(DLogicalChannelBase*& aChannel)
88 DPageStressTestChannel* channel=new DPageStressTestChannel;
91 channel->iFactory = this;
96 DECLARE_STANDARD_LDD()
98 return new DPageStressTestFactory;
102 // DPageStressTestChannel
105 TInt DPageStressTestChannel::DoCreate(TInt /*aUnit*/, const TDesC8* /*aInfo*/, const TVersion& /*aVer*/)
110 DPageStressTestChannel::DPageStressTestChannel()
111 : iRamAllocd(EFalse), iInitialFreeRam(0), iTotalBlocks(0), iBlockSize(0), iLastBlockAllocd(0),
112 iAddrArray(NULL), iDebug(0), iThreadCounter(1), iAddrMin(0), iAddrMax(0), iDemandPaging(ETrue)
114 _LIT(KMutexName,"TPageStressMutex");
115 NKern::ThreadEnterCS();
116 Kern::MutexCreate(iMutex, KMutexName,KMutexOrdNone);
117 NKern::ThreadLeaveCS();
118 SVMCacheInfo tempPages;
119 if (Kern::HalFunction(EHalGroupVM,EVMHalGetCacheSize,&tempPages,0) != KErrNone)
121 iDemandPaging = EFalse;
125 DPageStressTestChannel::~DPageStressTestChannel()
129 DoConsumeRamFinish();
138 TInt DPageStressTestChannel::Request(TInt aFunction, TAny* a1, TAny* a2)
140 TInt threadCount = __e32_atomic_tas_ord32(&iThreadCounter, 1, 1, 0);
141 if (threadCount >= 2)
143 Kern::Printf("DPageStressTestChannel::Request threadCount = %d\n", threadCount);
145 NKern::ThreadEnterCS();
146 Kern::MutexWait(*iMutex);
147 TInt retVal = KErrNotSupported;
150 case RPageStressTestLdd::EDoConsumeRamSetup:
152 retVal = DPageStressTestChannel::DoConsumeRamSetup((TInt)a1, (TInt)a2);
156 case RPageStressTestLdd::EDoConsumeRamFinish:
158 retVal = DPageStressTestChannel::DoConsumeRamFinish();
162 case RPageStressTestLdd::EDoConsumeSomeRam:
164 retVal = DPageStressTestChannel::DoConsumeSomeRam((TInt)a1);
168 case RPageStressTestLdd::EDoReleaseSomeRam:
170 retVal = DPageStressTestChannel::DoReleaseSomeRam((TInt)a1);
174 case RPageStressTestLdd::EDoSetDebugFlag:
176 retVal = DoSetDebugFlag((TInt) a1);
182 Kern::MutexSignal(*iMutex);
183 NKern::ThreadLeaveCS();
184 __e32_atomic_tas_ord32(&iThreadCounter, 1, -1, 0);
189 // DPageStressTestChannel::DoConsumeRamSetup
191 // This test attempts to consume most of the available Contiguous Ram until we need to ask the
192 // demand paging code to release memory for it.
196 #define CHECK(c) { if(!(c)) { Kern::Printf("Fail %d", __LINE__); ; retVal = __LINE__;} }
198 TInt DPageStressTestChannel::DoConsumeRamSetup(TInt aNumPagesLeft, TInt aPagesInBlock)
202 Kern::Printf("DPageStressTestChannel trying to start again when RAM alloc'd\n");
203 DoConsumeRamFinish();
206 TInt retVal = KErrNone;
209 CHECK(Kern::HalFunction(EHalGroupKernel,EKernelHalPageSizeInBytes,&pageSize,0)==KErrNone);
211 iBlockSize = aPagesInBlock * pageSize;
212 iInitialFreeRam = FreeRam();
213 iTotalBlocks = (iInitialFreeRam/iBlockSize) + 10;
215 iAddrArray = (TPhysAddr *)Kern::AllocZ(sizeof(TPhysAddr) * iTotalBlocks);
223 SVMCacheInfo tempPages;
227 // get the initial free ram again as the heap may have grabbed a page during the alloc
228 iInitialFreeRam = FreeRam();
233 CHECK(Kern::HalFunction(EHalGroupVM,EVMHalGetCacheSize,&tempPages,0) == KErrNone);
236 Kern::Printf("Start : min %d max %d current %d maxFree %d freeRam %d",
237 tempPages.iMinSize, tempPages.iMaxSize, tempPages.iCurrentSize ,tempPages.iMaxFreeSize, FreeRam());
240 // allocate blocks to use up RAM until we fail to allocate any further...
242 // only alloc upto the point where we have a number of pages left.
243 iLastBlockAllocd = iTotalBlocks - (aNumPagesLeft / aPagesInBlock);
244 for (index = 0; index < iLastBlockAllocd; index ++)
248 CHECK(Kern::HalFunction(EHalGroupVM,EVMHalGetCacheSize,&tempPages,0) == KErrNone);
251 if (KErrNone != Epoc::AllocPhysicalRam(iBlockSize, iAddrArray[index], 0))
253 iAddrArray[index] = NULL;
257 if ((iAddrMin == 0) || (iAddrMin > iAddrArray[index]))
259 iAddrMin = iAddrArray[index];
262 if (iAddrMax < iAddrArray[index])
264 iAddrMax = iAddrArray[index];
269 CHECK(Kern::HalFunction(EHalGroupVM,EVMHalGetCacheSize,&tempPages,0) == KErrNone);
272 iLastBlockAllocd = index - 1;
276 CHECK(Kern::HalFunction(EHalGroupVM,EVMHalGetCacheSize,&tempPages,0) == KErrNone);
279 Kern::Printf("Alloc'd : min %d max %d current %d maxFree %d freeRam %d lastIndex %d addrMin 0x%08x addrMax 0x%08x",
280 tempPages.iMinSize, tempPages.iMaxSize, tempPages.iCurrentSize ,tempPages.iMaxFreeSize, FreeRam(), iLastBlockAllocd, iAddrMin, iAddrMax);
286 TInt DPageStressTestChannel::DoConsumeRamFinish(void)
288 TInt retVal = KErrNone;
291 SVMCacheInfo tempPages;
295 CHECK(Kern::HalFunction(EHalGroupVM,EVMHalGetCacheSize,&tempPages,0) == KErrNone);
298 Kern::Printf("Cleanup cache info: iMinSize %d iMaxSize %d iCurrentSize %d iMaxFreeSize %d",
299 tempPages.iMinSize, tempPages.iMaxSize, tempPages.iCurrentSize ,tempPages.iMaxFreeSize);
302 TInt index = iTotalBlocks - 1;
304 TBool firstTime = ETrue;
306 // free the memory we allocated...
309 if (iAddrArray[index])
311 if ((iAddrArray[index] < iAddrMin) || (iAddrArray[index] > iAddrMax))
313 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);
314 TInt tempIndex = index - 8;
315 while (tempIndex < (index + 8))
317 Kern::Printf(" --> index %d addr 0x%08x", tempIndex, iAddrArray[tempIndex]);
321 iAddrArray[index] = NULL;
325 TInt r = Epoc::FreePhysicalRam(iAddrArray[index], iBlockSize);
326 iAddrArray[index] = NULL;
337 Kern::Printf("DoConsumeRamFinish : FreeRam Initial 0x%x now 0x%x",iInitialFreeRam, FreeRam());
339 //CHECK(FreeRam() == iInitialFreeRam)
340 if (FreeRam() != iInitialFreeRam)
342 Kern::Printf("DoConsumeRamFinish : FreeRam Initial 0x%x now 0x%x NOT EQUAL!",iInitialFreeRam, FreeRam());
345 Kern::Free(iAddrArray);
349 iLastBlockAllocd = -1;
355 CHECK(Kern::HalFunction(EHalGroupVM,EVMHalGetCacheSize,&tempPages,0) == KErrNone);
358 Kern::Printf("End cache info: iMinSize %d iMaxSize %d iCurrentSize %d iMaxFreeSize %d",
359 tempPages.iMinSize, tempPages.iMaxSize, tempPages.iCurrentSize ,tempPages.iMaxFreeSize);
366 TInt DPageStressTestChannel::DoConsumeSomeRam(TInt aBlocks)
368 TInt retVal = KErrNone;
373 TInt DPageStressTestChannel::DoReleaseSomeRam(TInt aBlocks)
375 if(iLastBlockAllocd<0)
376 return KErrUnderflow;
378 TInt retVal = KErrNone;
381 SVMCacheInfo tempPages;
385 CHECK(Kern::HalFunction(EHalGroupVM,EVMHalGetCacheSize,&tempPages,0) == KErrNone);
388 Kern::Printf("Release : min %d max %d current %d maxFree %d freeRam %d lastIndex %d",
389 tempPages.iMinSize, tempPages.iMaxSize, tempPages.iCurrentSize ,tempPages.iMaxFreeSize, FreeRam(), iLastBlockAllocd);
393 TInt index = iLastBlockAllocd;
394 iLastBlockAllocd -= aBlocks;
396 TBool firstTime = ETrue;
398 // free the memory we allocated...
399 while((index >= 0) && (index > iLastBlockAllocd))
401 if (iAddrArray[index])
403 //Kern::Printf("DoReleaseSomeRam : index %d addr 0x%08x size %d", index, iAddrArray[index], iBlockSize);
404 if ((iAddrArray[index] < iAddrMin) || (iAddrArray[index] > iAddrMax))
406 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);
407 TInt tempIndex = index - 8;
408 while (tempIndex < (index + 8))
410 Kern::Printf(" --> index %d addr 0x%08x", tempIndex, iAddrArray[tempIndex]);
414 iAddrArray[index] = NULL;
418 TInt r = Epoc::FreePhysicalRam(iAddrArray[index], iBlockSize);
419 iAddrArray[index] = NULL;
425 Kern::Printf("ERROR: DoReleaseSomeRam : trying to free NULL index %d", index, iAddrArray[index], iAddrMin, iAddrMax);
426 TInt tempIndex = index - 8;
427 while (tempIndex < (index + 8))
429 Kern::Printf(" --> index %d addr 0x%08x", tempIndex, iAddrArray[tempIndex]);
439 Kern::Printf("WARNING : DoReleaseSomeRam : index %d !!!!!", index);
444 CHECK(Kern::HalFunction(EHalGroupVM,EVMHalGetCacheSize,&tempPages,0) == KErrNone);
447 Kern::Printf("Released : min %d max %d current %d maxFree %d freeRam %d lastIndex %d",
448 tempPages.iMinSize, tempPages.iMaxSize, tempPages.iCurrentSize ,tempPages.iMaxFreeSize, FreeRam(), iLastBlockAllocd);
456 TInt DPageStressTestChannel::FreeRam()
458 return Kern::FreeRamInBytes();
461 TInt DPageStressTestChannel::DoSetDebugFlag(TInt aState)