commsfwutils/commsbufs/TE_mbufmgr/src/test16memoryfull.cpp
changeset 72 ae47d0499bee
equal deleted inserted replaced
68:5da8188e392b 72:ae47d0499bee
       
     1 // Copyright (c) 2002-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 "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 // Contains MBufMgr Test Step 16 - Memory Full
       
    15 // 
       
    16 //
       
    17 
       
    18 // EPOC includes
       
    19 #include <e32base.h>
       
    20 #include <ss_std.h>
       
    21 // Test system includes
       
    22 //#ifdef SYMBIAN_OLD_EXPORT_LOCATION
       
    23 //#include "networking/log.h"
       
    24 //#include "networking/teststep.h"
       
    25 //#else
       
    26 //#include <networking/log.h>
       
    27 //#include <networking/teststep.h>
       
    28 //#endif
       
    29 #include "TestStepCTMbufmgr.h"
       
    30 
       
    31 #include "test16memoryfull.h"
       
    32 
       
    33 #include <comms-infras/mbufallocator.h>
       
    34 #include <comms-infras/commsbufpond.h>
       
    35 // constructor
       
    36 CTest16MemoryFull::CTest16MemoryFull()
       
    37 	{
       
    38 	SetTestStepName( _L("MBufMgrTest16") );	// Store the name of this test case
       
    39 	}
       
    40 
       
    41 // destructor
       
    42 CTest16MemoryFull::~CTest16MemoryFull()
       
    43 	{
       
    44 	}
       
    45 
       
    46 //
       
    47 void CTest16MemoryFull::FillDes(TDes8 &aBuf, TInt aStartPos, TInt anEndPos, TUint aStartChar, TUint anEndChar)
       
    48 	{
       
    49 	StripeDes(aBuf, aStartPos, anEndPos, aStartChar, anEndChar);
       
    50 	}
       
    51 
       
    52 RCommsBufPond& CTest16MemoryFull::BufPond()
       
    53 	{
       
    54 	return iBufPond;
       
    55 	}
       
    56 
       
    57 enum TVerdict CTest16MemoryFull::doTestStepL(void)
       
    58 	{
       
    59 	//-------------- substep 1 --------------------
       
    60 	INFO_PRINTF1(_L("  01 Create CMBufManager and install active scheduler:"));
       
    61 	CleanupStack::PushL( iActSch = new(ELeave) CActiveScheduler );
       
    62 	CActiveScheduler::Install(iActSch);
       
    63 
       
    64 	// Create some initial pools
       
    65 	// AddL(KMBuf_MBufSize, KMBuf_InitialAllocation, KMBuf_MinGrowth, KMBuf_GrowthThreshold)
       
    66 	// Background memory allocation (CMBufPoolManager::RunL) is used more frequently if
       
    67 	// the KMBuf_MinGrowth and the KMBuf_GrowthThreshold are similar in size
       
    68 	// Synchronous allocation (AllocPool(*poolChain, growth, ETrue)) occurs when the
       
    69 	// requested size (iChain.CopyIn(size)) is larger than the KMBuf_MinGrowth
       
    70 	// Multiple threads attempt to grow different poolchains at the same time when there
       
    71 	// are a greater variety of pool sizes to grow.
       
    72 	// 16384
       
    73 
       
    74 	RArray<TCommsBufPoolCreateInfo> createInfoArray;
       
    75 	
       
    76 	TCommsBufPoolCreateInfo createInfo;
       
    77 	createInfo.iBufSize = 128;
       
    78 	createInfo.iInitialBufs = 128;
       
    79 	createInfo.iGrowByBufs = 64;
       
    80 	createInfo.iMinFreeBufs = 40;
       
    81 	createInfo.iCeiling = 410; 
       
    82 
       
    83 	TCommsBufPoolCreateInfo createInfo2;
       
    84 	createInfo2.iBufSize = 256;
       
    85 	createInfo2.iInitialBufs = 64;
       
    86 	createInfo2.iGrowByBufs = 7;
       
    87 	createInfo2.iMinFreeBufs = 6;
       
    88 	createInfo2.iCeiling = 204;
       
    89 
       
    90 	TCommsBufPoolCreateInfo createInfo3;
       
    91 	createInfo3.iBufSize = 512;
       
    92 	createInfo3.iInitialBufs = 32;
       
    93 	createInfo3.iGrowByBufs = 6;
       
    94 	createInfo3.iMinFreeBufs = 5;
       
    95 	createInfo3.iCeiling = 125;
       
    96 
       
    97 	TCommsBufPoolCreateInfo createInfo4;
       
    98 	createInfo4.iBufSize = 1024;
       
    99 	createInfo4.iInitialBufs = 16;
       
   100 	createInfo4.iGrowByBufs = 5;
       
   101 	createInfo4.iMinFreeBufs = 4;
       
   102 	createInfo4.iCeiling = 51; 
       
   103 
       
   104 	TCommsBufPoolCreateInfo createInfo5;
       
   105 	createInfo5.iBufSize = 2048;
       
   106 	createInfo5.iInitialBufs = 8;
       
   107 	createInfo5.iGrowByBufs = 4;
       
   108 	createInfo5.iMinFreeBufs = 3;
       
   109 	createInfo5.iCeiling = 15; 
       
   110 
       
   111 	createInfoArray.Append(createInfo);
       
   112 	createInfoArray.Append(createInfo2);
       
   113 	createInfoArray.Append(createInfo3);
       
   114 	createInfoArray.Append(createInfo4);
       
   115 	createInfoArray.Append(createInfo5);
       
   116 	// Create 262144
       
   117 	CreateInstanceMBufMgrL(createInfoArray);
       
   118 	CleanupClosePushL(iBufPond);
       
   119 	createInfoArray.Close ();
       
   120 
       
   121 	TBuf8<KFactor*KFactor*KFactor*KFactor> *aDes1, *aDes2;
       
   122 	CleanupStack::PushL( aDes1 = new(ELeave) TBuf8<KFactor*KFactor*KFactor*KFactor> );
       
   123 	CleanupStack::PushL( aDes2 = new(ELeave) TBuf8<KFactor*KFactor*KFactor*KFactor> );
       
   124 
       
   125 	//-------------------substep 02-----------------------------
       
   126 	INFO_PRINTF1(_L("  02 Create and install active object that will do the test:"));
       
   127 	RMBufAsyncRequest async;
       
   128 	CMBufAsyncMemFull* memoryfull;
       
   129 	CleanupStack::PushL(memoryfull = new(ELeave) CMBufAsyncMemFull(this, async, aDes1, aDes2));
       
   130 	CActiveScheduler::Add(memoryfull);
       
   131 
       
   132 	//-------------------substep 03-----------------------------
       
   133 	INFO_PRINTF1(_L("  03 Start the test:"));
       
   134 
       
   135 	TInt ret = memoryfull->DoStartTest();
       
   136 	if (ret != KErrNone)
       
   137 		{
       
   138 		INFO_PRINTF1(_L("Error: Async Alloc's failing in test run:"));
       
   139 		User::Leave(EFail);
       
   140 		}
       
   141 
       
   142 	// If allocation less than expected then there may be a problem.
       
   143 	// So get the allocated memory for each pool chain
       
   144 	RMBufAllocator allocator;
       
   145 
       
   146 	TInt size = allocator.NextMBufSize(0);
       
   147 	_LIT(aLogD,"     Info: Available MBufs in %d Pool Chain: %d"); 
       
   148 	while (size != KErrNotFound)
       
   149 		{
       
   150 		INFO_PRINTF3(aLogD, size, allocator.BytesAvailable(size) / size);
       
   151 		size = allocator.NextMBufSize(size);
       
   152 		}
       
   153 	
       
   154 //	TInt heapSize = iMBMngr->__DbgGetHeapSize();
       
   155 	
       
   156 	//Clean up stack
       
   157 	CleanupStack::PopAndDestroy(memoryfull);
       
   158 	CleanupStack::PopAndDestroy(aDes2);
       
   159 	CleanupStack::PopAndDestroy(aDes1);
       
   160 	CleanupStack::PopAndDestroy();
       
   161 	CActiveScheduler::Install(NULL);
       
   162 	CleanupStack::PopAndDestroy(iActSch);
       
   163 	
       
   164 	//-------------------substep 04-----------------------------
       
   165 	_LIT(aLog1,"     Info: Number of main thread checkings: %d with %d fails last failure on %d");
       
   166 	INFO_PRINTF4(aLog1,iMainThreadTries,iMainThreadFails,iMainThreadLastFailure);
       
   167 	_LIT(aLog2,"     Info: Number of main thread allocate size failures: %d");
       
   168 	INFO_PRINTF2(aLog2,iMainThreadSizeFails);
       
   169 	_LIT(aLog3,"     Info: Number of high priority thread checkings: %d with %d fails");
       
   170 	INFO_PRINTF3(aLog3,iThread3Tries,iThread3Fails);
       
   171 	_LIT(aLog4,"     Info: Number of high priority thread allocation failures: %d");
       
   172 	INFO_PRINTF2(aLog4,iThread3AllocFails);
       
   173 	_LIT(aLog5,"     Info: Number of memory gobbler allocate tries and failures: %d with %d memory full last memory full on %d");
       
   174 	INFO_PRINTF4(aLog5,iThread1AllocTries,iThread1MemoryFull,iThread1LastMemoryFull);
       
   175 	_LIT(aLog6,"     Info: Number of memory gobbler allocate size failures: %d");
       
   176 	INFO_PRINTF2(aLog6,iThread1SizeFails);
       
   177 	_LIT(aLog7,"     Info: Number of memory gobbler allocate tries and failures: %d with %d memory full last memory full on %d");
       
   178 	INFO_PRINTF4(aLog7,iThread2AllocTries,iThread2MemoryFull,iThread2LastMemoryFull);
       
   179 	_LIT(aLog8,"     Info: Number of memory gobbler allocate size failures: %d");
       
   180 	INFO_PRINTF2(aLog8,iThread2SizeFails);
       
   181 	_LIT(aLog9,"     Info: Max chain length for memory gobbler allocation: %d ; %d");
       
   182 	INFO_PRINTF3(aLog9,iThread1MaxLen,iThread2MaxLen);
       
   183 //	_LIT(aLogA,"     Info: Max allocated memory: %d"); Log(aLogA,heapSize);
       
   184 	_LIT(aLogB,"     Info: Basic API (uses TLS) - Average time to allocate mbufs: %d ; Average time to free mbufs %d");
       
   185 	INFO_PRINTF3(aLogB,iThread1AllocTime/iThread1AllocTries,iThread1FreeTime/(1000 - iThread1AllocTries));
       
   186 	_LIT(aLogC,"     Info: Higher Speed API (no TLS) - Average time to allocate mbufs: %d ; Average time to free mbufs %d");
       
   187 	INFO_PRINTF3(aLogC,iThread2AllocTime/iThread2AllocTries,iThread2FreeTime/(1000 - iThread2AllocTries));
       
   188 	
       
   189 	if (iThread3Tries <1000)
       
   190 		{
       
   191 		INFO_PRINTF1(_L("   Error: High priority thread ended before 1000 loops are finished"));
       
   192 
       
   193 		return EFail;
       
   194 		}
       
   195 
       
   196 	// test to see if the byte by byte comparisons failed
       
   197 	if ( iThread3Fails || iMainThreadFails || iThread1SizeFails || iThread2SizeFails)
       
   198 		{
       
   199 		INFO_PRINTF1(_L("   Error: MBuf allocation failure"));
       
   200 
       
   201         SetTestStepResult(EFail);
       
   202 		return TestStepResult();
       
   203 		}
       
   204 
       
   205 	// Test the maximum allocated memory
       
   206 	// Some of the possible problems include failing to background allocate new memory
       
   207 /*	if ( KMBufDefaultHeapSize > (heapSize + KMBufDefaultHeapSize/100) )
       
   208 		{
       
   209 		Log(_L("   Error: Background allocation may have failed"));
       
   210 
       
   211 		return EFail;
       
   212 		}
       
   213 */   async.Close();
       
   214 
       
   215     SetTestStepResult(EPass);
       
   216 	return TestStepResult();
       
   217   }
       
   218 
       
   219   // Async request
       
   220 CMBufAsyncMemFull::CMBufAsyncMemFull(CTest16MemoryFull* aTestObject,
       
   221 		RMBufAsyncRequest& aMBufAsyncReq,
       
   222 		TDes8 *aDes1,
       
   223 		TDes8 *aDes2)
       
   224 		:CActive(EPriorityStandard)
       
   225 		,iTestObject(aTestObject)
       
   226 		,iDes1(aDes1)
       
   227 		,iDes2(aDes2)
       
   228 		,iMBufAsyncReq(aMBufAsyncReq)
       
   229 	{
       
   230 	}
       
   231 
       
   232 TInt CMBufAsyncMemFull::DoStartTest()
       
   233 	{
       
   234 
       
   235 	// Create some random sizes for allocation later
       
   236 	for (TInt i=1; i<(KFactor+1); i++)
       
   237 		{
       
   238 		iRequested_size[i-1] = i * i * i * KFactor;
       
   239 		}
       
   240 
       
   241 	// Allocate two 5000-bytes long descriptors (Des1 & Des2):"));
       
   242 	iDes1->SetLength(iRequested_size[KFactor-1]);
       
   243 	iDes2->SetLength(iRequested_size[KFactor-1]);
       
   244 	//Fill in the Des1 with a pattern:"));
       
   245 	iTestObject->FillDes(*iDes1, 0, iRequested_size[KFactor-1], '@', 'Z');
       
   246 
       
   247 	//-------------------substep 01-----------------------------
       
   248 
       
   249 	TInt err=iThread1.Create(_L("testThread1Rec"),
       
   250 		                       fThread1,
       
   251  							   KDefaultStackSize,
       
   252 							   KDefaultHeapSize,
       
   253 							   KMaxHeapSize,
       
   254 							   (TAny*)iTestObject,
       
   255 							   EOwnerProcess);
       
   256 	if (err!=KErrNone)
       
   257 		{
       
   258 		User::Leave(EFail);
       
   259 		}
       
   260 	iThread1.SetPriority(EPriorityAbsoluteHigh);
       
   261 	iThread1.Resume();
       
   262 
       
   263 	//-------------------substep 02-----------------------------
       
   264 	RThread aThread2;
       
   265 
       
   266 	err=iThread2.Create(_L("testThread2Rec"),
       
   267 		                       fThread2,
       
   268  							   KDefaultStackSize,
       
   269 							   KDefaultHeapSize,
       
   270  							   KMaxHeapSize,
       
   271 							   (TAny*)iTestObject,
       
   272 							   EOwnerProcess);
       
   273 	if (err!=KErrNone)
       
   274 		{
       
   275 		User::Leave(EFail);
       
   276 		}
       
   277 	iThread2.SetPriority(EPriorityAbsoluteHigh);
       
   278 	iThread2.Resume();
       
   279 
       
   280 	//-------------------substep 03-----------------------------
       
   281 	RThread aThread3;
       
   282 
       
   283 	err=iThread3.Create(_L("testThread3Rec"),
       
   284 		                       fThread3,
       
   285  							   KDefaultStackSize,
       
   286 							   KDefaultHeapSize,
       
   287 							   KMaxHeapSize,
       
   288 							   (TAny*)iTestObject,
       
   289 							   EOwnerProcess);
       
   290 	if (err!=KErrNone)
       
   291 		{
       
   292 		User::Leave(EFail);
       
   293 		}
       
   294 	iThread3.SetPriority(EPriorityAbsoluteHigh);
       
   295 	iThread3.Resume();
       
   296 
       
   297 	//-------------------substep 04-----------------------------
       
   298 
       
   299 	iMBufAsyncReq.Alloc(iChain, iRequested_size[iRequestloop], iStatus);
       
   300 	SetActive();
       
   301 	CActiveScheduler::Start();
       
   302 
       
   303 	iThread1.Close();
       
   304 	iThread2.Close();
       
   305 	iThread3.Close();
       
   306 
       
   307 	return iStatus.Int();
       
   308 	}
       
   309 
       
   310 
       
   311 void CMBufAsyncMemFull::RunL()
       
   312 	{
       
   313 	if (iStatus.Int() == KErrNone)
       
   314 		{
       
   315 
       
   316 		//Copy in Des1 into Chain
       
   317 		iChain.CopyIn(iDes1->LeftTPtr(iRequested_size[iRequestloop]));
       
   318 		//Fill left most part of Des2 with zeros:"));
       
   319 		iDes2->SetLength(iRequested_size[KFactor-1]);
       
   320 		iTestObject->FillDes(*iDes2, 0, iRequested_size[iRequestloop], 0, 0);
       
   321 		//Copy out Chain into Des2:"));
       
   322 		iChain.CopyOut(*iDes2);
       
   323 		//Compare the contents of Des1 & Des2:
       
   324 		if((iDes1->LeftTPtr(iRequested_size[iRequestloop])).
       
   325 				Compare(iDes2->Left(iRequested_size[iRequestloop])))
       
   326 			{
       
   327 			iTestObject->iMainThreadFails++;
       
   328 			iTestObject->iMainThreadLastFailure = iTestObject->iMainThreadTries;
       
   329 			if (iChain.Length() != iRequested_size[iRequestloop])
       
   330 				{
       
   331 				iTestObject->iMainThreadSizeFails++;
       
   332 				}
       
   333 			}
       
   334 		iTestObject->iMainThreadTries++;
       
   335 		//Free chain
       
   336 		iChain.Free();
       
   337 
       
   338 		//Check whether the other task has finished
       
   339 		TBool isRunning1 = ETrue;
       
   340 		TBool isRunning2 = ETrue;
       
   341 		TBool isRunning3 = ETrue;
       
   342 		volatile TExitType aExit;
       
   343 		aExit = iThread1.ExitType();
       
   344 		if (aExit != EExitPending)
       
   345 			{
       
   346 			isRunning1 = EFalse;
       
   347 			}
       
   348 		aExit = iThread2.ExitType();
       
   349 		if (aExit != EExitPending)
       
   350 			{
       
   351 			isRunning2 = EFalse;
       
   352 			}
       
   353 		aExit = iThread3.ExitType();
       
   354 		if (aExit != EExitPending)
       
   355 			{
       
   356 			isRunning3 = EFalse;
       
   357 			}
       
   358 
       
   359 		iRequestloop++;
       
   360 		if (iRequestloop>9)
       
   361 			{
       
   362 			iRequestloop = 0;
       
   363 			}
       
   364 
       
   365 		if (isRunning1 || isRunning2 || isRunning3)
       
   366 			{
       
   367 
       
   368 			iMBufAsyncReq.Alloc(iChain, iRequested_size[iRequestloop], iStatus);
       
   369 
       
   370 			SetActive();
       
   371 
       
   372 			return;
       
   373 			}
       
   374 		}
       
   375 	
       
   376 	CActiveScheduler::Stop();
       
   377 
       
   378 	}
       
   379 
       
   380 // The memory gobbler thread
       
   381 TInt CMBufAsyncMemFull::fThread1(TAny* aInput)
       
   382 	{
       
   383 	CTest16MemoryFull* pTestObject = (CTest16MemoryFull*)aInput;
       
   384 	// We need to introduce this new client thread to the MBufMgr
       
   385 	TCommsBufPondTLSOp tls(pTestObject->BufPond());
       
   386 	tls.Set();
       
   387 	
       
   388 	CTrapCleanup* aCleanup = CTrapCleanup::New();
       
   389 
       
   390 	//Install active scheduler
       
   391     CActiveScheduler* aActSch = new CActiveScheduler;
       
   392 	if(aActSch==NULL)
       
   393 		{
       
   394 		return KErrNoMemory;
       
   395 		}
       
   396 	CActiveScheduler::Install(aActSch);
       
   397 
       
   398 	RTimer aTimer;
       
   399 	TRequestStatus aTimerStatus;	// Request status associated with timer
       
   400 	aTimer.CreateLocal();			// Create timer for this thread
       
   401 	//-------------- substep 1 --------------------
       
   402 
       
   403 	// Create some random sizes for allocation later
       
   404 	TInt requested_size[KFactor1];
       
   405 	for (TInt i=1; i<(KFactor1+1); i++)
       
   406 		{
       
   407 		requested_size[i-1] = i * i * KFactor1;
       
   408 		}
       
   409 
       
   410 	// Allocate two 5000-bytes long descriptors (Des1 & Des2):"));
       
   411 	TBuf8<KFactor1*KFactor1*KFactor1> *aDes1=NULL;
       
   412 	TRAPD(ret, aDes1 = new(ELeave) TBuf8<KFactor1*KFactor1*KFactor1> );
       
   413 	if(ret!=KErrNone)
       
   414 		{
       
   415 		return ret;
       
   416 		}
       
   417 
       
   418 	aDes1->SetLength(requested_size[KFactor1-1]);
       
   419 
       
   420 	//Fill in the Des1 with a pattern:"));
       
   421 	pTestObject->FillDes(*aDes1, 0, requested_size[KFactor1-1], 1, 1);
       
   422 	
       
   423 	TUint time;
       
   424 	RMBufChain aChain;
       
   425 	RMBufChain bChain;
       
   426 	TInt requestloop = 0;
       
   427 	TBool isFillMe = ETrue;
       
   428 	for (TInt i = 0 ; i<1000;i++)
       
   429 		{
       
   430 
       
   431 		if (isFillMe)
       
   432 			{
       
   433 			pTestObject->iThread1AllocTries++;
       
   434 			//Use up mbuf memory by appending to a chain
       
   435 			time = User::FastCounter();
       
   436 			TRAP(ret,aChain.AllocL(requested_size[requestloop]));
       
   437 			time = User::FastCounter() - time;
       
   438 			if (ret != KErrNone)
       
   439 				{
       
   440 				isFillMe = EFalse;
       
   441 				pTestObject->iThread1MemoryFull++;
       
   442 				pTestObject->iThread1LastMemoryFull = pTestObject->iThread1AllocTries - 1;
       
   443 				}
       
   444 			else
       
   445 				{
       
   446 				pTestObject->iThread1AllocTime += time;
       
   447 				//Copy in Des1 into Chain
       
   448 				aChain.CopyIn(aDes1->LeftTPtr(requested_size[requestloop]));
       
   449 				if (aChain.Length() != requested_size[requestloop])
       
   450 					{
       
   451 					pTestObject->iThread1SizeFails++;
       
   452 					}
       
   453 				// Now grow the chain
       
   454 				bChain.Append(aChain);
       
   455 				}
       
   456 			}
       
   457 		else
       
   458 			{
       
   459 			//Free some memory
       
   460 			TInt length = bChain.Length();
       
   461 			if (length > pTestObject->iThread1MaxLen)
       
   462 				{
       
   463 				pTestObject->iThread1MaxLen = length;
       
   464 				}
       
   465 			TInt trimOffset = length - requested_size[requestloop];
       
   466 			if (trimOffset > 0)
       
   467 				{
       
   468 				time = User::FastCounter();
       
   469 				bChain.TrimEnd(trimOffset);
       
   470 				time = User::FastCounter() - time;
       
   471 				pTestObject->iThread1FreeTime += time;
       
   472 				}
       
   473 			else
       
   474 				{
       
   475 				isFillMe = ETrue;
       
   476 				}
       
   477 			}
       
   478 
       
   479 		//Sleep for 5ms
       
   480 		aTimer.After(aTimerStatus,5000);
       
   481 		User::WaitForRequest(aTimerStatus);
       
   482 
       
   483 		requestloop++;
       
   484 		if (requestloop>(KFactor1-1))
       
   485 			{
       
   486 			requestloop = 0;
       
   487 			}
       
   488 		}
       
   489 	// Free the memory
       
   490 	bChain.Free();
       
   491 
       
   492 	delete aDes1;
       
   493 	CActiveScheduler::Install(NULL);
       
   494 	delete aActSch;
       
   495 	delete aCleanup;
       
   496 	return ret;
       
   497 }
       
   498 
       
   499 // The memory gobbler thread
       
   500 TInt CMBufAsyncMemFull::fThread2(TAny* aInput)
       
   501 	{
       
   502 	CTest16MemoryFull* pTestObject = (CTest16MemoryFull*)aInput;
       
   503 	// We need to introduce this new client thread to the MBufMgr
       
   504 	TCommsBufPondTLSOp tls(pTestObject->BufPond());
       
   505 	tls.Set();
       
   506 
       
   507 	CTrapCleanup* aCleanup = CTrapCleanup::New();
       
   508 
       
   509 	//Install active scheduler
       
   510 	CActiveScheduler* aActSch = new CActiveScheduler;
       
   511 	if(aActSch==NULL)
       
   512 		{
       
   513 		return KErrNoMemory;
       
   514 		}
       
   515 	CActiveScheduler::Install(aActSch);
       
   516 
       
   517 	RTimer aTimer;
       
   518 	TRequestStatus aTimerStatus;	// Request status associated with timer
       
   519 	aTimer.CreateLocal();			// Create timer for this thread
       
   520 	//-------------- substep 1 --------------------
       
   521 
       
   522 	// Create some random sizes for allocation later
       
   523 	TInt requested_size[KFactor2];
       
   524 	for (TInt i=1; i<(KFactor2+1); i++)
       
   525 		{
       
   526 		requested_size[i-1] = i * i * KFactor2;
       
   527 		}
       
   528 
       
   529 	// Allocate two 5000-bytes long descriptors (Des1 & Des2):"));
       
   530 	TBuf8<KFactor2*KFactor2*KFactor2> *aDes1=NULL;
       
   531 	TRAPD(ret, aDes1 = new(ELeave) TBuf8<KFactor2*KFactor2*KFactor2> );
       
   532 	if(ret!=KErrNone)
       
   533 		{
       
   534 		return ret;
       
   535 		}
       
   536 
       
   537 	aDes1->SetLength(requested_size[KFactor2-1]);
       
   538 
       
   539 	//Fill in the Des1 with a pattern:"));
       
   540 	pTestObject->FillDes(*aDes1, 0, requested_size[KFactor2-1], 2, 2);
       
   541 	
       
   542 	RMBufAllocator allocator;
       
   543 	TUint time;
       
   544 	
       
   545 	RMBufChain aChain;
       
   546 	RMBufChain bChain;
       
   547 	TInt requestloop = 0;
       
   548 	TBool isFillMe = ETrue;
       
   549 	for (TInt i = 0 ; i<1000;i++)
       
   550 		{
       
   551 
       
   552 		if (isFillMe)
       
   553 			{
       
   554 			pTestObject->iThread2AllocTries++;
       
   555 			//Use up mbuf memory by appending to a chain
       
   556 			time = User::FastCounter();
       
   557 			TRAP(ret,aChain.AllocL(requested_size[requestloop], allocator));
       
   558 			time = User::FastCounter() - time;
       
   559 			if (ret != KErrNone)
       
   560 				{
       
   561 				isFillMe = EFalse;
       
   562 				pTestObject->iThread2MemoryFull++;
       
   563 				pTestObject->iThread2LastMemoryFull = pTestObject->iThread2AllocTries - 1;
       
   564 				}
       
   565 			else
       
   566 				{
       
   567 				pTestObject->iThread2AllocTime += time;
       
   568 				//Copy in Des1 into Chain
       
   569 				aChain.CopyIn(aDes1->LeftTPtr(requested_size[requestloop]));
       
   570 				if (aChain.Length() != requested_size[requestloop])
       
   571 					{
       
   572 					pTestObject->iThread2SizeFails++;
       
   573 					}
       
   574 				// Now grow the chain
       
   575 				bChain.Append(aChain);
       
   576 				}
       
   577 			}
       
   578 		else
       
   579 			{
       
   580 			//Free some memory
       
   581 			TInt length = bChain.Length();
       
   582 			if (length > pTestObject->iThread2MaxLen)
       
   583 				{
       
   584 				pTestObject->iThread2MaxLen = length;
       
   585 				}
       
   586 			TInt trimOffset = length - requested_size[requestloop];
       
   587 			if (trimOffset > 0)
       
   588 				{
       
   589 				time = User::FastCounter();
       
   590 				bChain.TrimEnd(trimOffset);
       
   591 				time = User::FastCounter() - time;
       
   592 				pTestObject->iThread2FreeTime += time;
       
   593 				}
       
   594 			else
       
   595 				{
       
   596 				isFillMe = ETrue;
       
   597 				}
       
   598 			}
       
   599 
       
   600 		//Sleep for 5ms
       
   601 		aTimer.After(aTimerStatus,5000);
       
   602 		User::WaitForRequest(aTimerStatus);
       
   603 
       
   604 		requestloop++;
       
   605 		if (requestloop>(KFactor2-1))
       
   606 			{
       
   607 			requestloop = 0;
       
   608 			}
       
   609 		}
       
   610 	// Free the memory
       
   611 	bChain.Free();
       
   612 
       
   613 	delete aDes1;
       
   614 
       
   615 	CActiveScheduler::Install(NULL);
       
   616 	delete aActSch;
       
   617 	delete aCleanup;
       
   618 	return ret;
       
   619 }
       
   620 
       
   621 //
       
   622 TInt CMBufAsyncMemFull::fThread3(TAny* aInput)
       
   623 	{
       
   624 	CTest16MemoryFull* pTestObject = (CTest16MemoryFull*)aInput;
       
   625 	// We need to introduce this new client thread to the MBufMgr
       
   626 	TCommsBufPondTLSOp tls(pTestObject->BufPond());
       
   627 	tls.Set();
       
   628 
       
   629 	CTrapCleanup* aCleanup = CTrapCleanup::New();
       
   630 
       
   631 	//Install active scheduler
       
   632 	CActiveScheduler* aActSch = new CActiveScheduler;
       
   633 	if(aActSch==NULL)
       
   634 		{
       
   635 		return KErrNoMemory;
       
   636 		}
       
   637 	CActiveScheduler::Install(aActSch);
       
   638 
       
   639 	RTimer aTimer;
       
   640 	TRequestStatus aTimerStatus;	// Request status associated with timer
       
   641 	aTimer.CreateLocal();			// Create timer for this thread
       
   642 	//-------------- substep 1 --------------------
       
   643 
       
   644 	// Allocate two 500-bytes long descriptors (Des1 & Des2):"));
       
   645 	TBuf8<500> *aDes1 = NULL;
       
   646 	TBuf8<500> *aDes2 = NULL;
       
   647 
       
   648 	TRAPD(ret, aDes1 = new(ELeave) TBuf8<500>);
       
   649 	if(ret!=KErrNone)
       
   650 		{
       
   651 		return ret;
       
   652 		}
       
   653 
       
   654 	TRAP(ret, aDes2 = new(ELeave) TBuf8<500>);
       
   655 	if(ret!=KErrNone)
       
   656 		{
       
   657 		delete aDes1;
       
   658 		return ret;
       
   659 		}
       
   660 
       
   661 	aDes1->SetLength(500);
       
   662 	aDes2->SetLength(500);
       
   663 
       
   664 	//Fill in the Des1 with a pattern
       
   665 	pTestObject->FillDes(*aDes1, 0, 500, 'a', 'z');
       
   666 
       
   667 	for (TInt i = 0 ; i<1000;i++)
       
   668 		{
       
   669 		//Allocate 500-bytes long RMBufChain
       
   670 		RMBufChain aChain;
       
   671 		TRAP(ret,aChain.AllocL(500));
       
   672 		if (ret == KErrNone)
       
   673 			{
       
   674 			//Copy in Des1 into Chain
       
   675 			aChain.CopyIn(*aDes1);
       
   676 			//Fill in Des2 with zeros
       
   677 			pTestObject->FillDes(*aDes2, 0, 500, 0, 0);
       
   678 			//Copy out Chain into Des2;
       
   679 			aChain.CopyOut(*aDes2);
       
   680 			//Compare the contents of Des1 & Des2
       
   681 			if(aDes1->Compare(*aDes2))
       
   682 				pTestObject->iThread3Fails++;
       
   683 			//Free chain
       
   684 			aChain.Free();
       
   685 			}
       
   686 		else
       
   687 			{
       
   688 			pTestObject->iThread3AllocFails++;
       
   689 			}
       
   690 		pTestObject->iThread3Tries++;
       
   691 		//Sleep for 5ms
       
   692 		aTimer.After(aTimerStatus,5000);
       
   693 		User::WaitForRequest(aTimerStatus);
       
   694 		}
       
   695 	delete aDes1;
       
   696 	delete aDes2;
       
   697 	CActiveScheduler::Install(NULL);
       
   698 	delete aActSch;
       
   699 	delete aCleanup;
       
   700 	return ret;
       
   701 }
       
   702