kerneltest/e32test/misc/t_svr3.cpp
changeset 0 a41df078684a
child 109 b3a1d9898418
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     1 // Copyright (c) 1995-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 // e32test\misc\t_svr3.cpp
       
    15 // 
       
    16 //
       
    17 
       
    18 #define __E32TEST_EXTENSION__
       
    19 
       
    20 #include <e32base.h>
       
    21 #include <e32base_private.h>
       
    22 #include <e32test.h>
       
    23 #include <e32svr.h>
       
    24 #include "u32std.h"
       
    25 #include "../misc/prbs.h"
       
    26 #include "../mmu/freeram.h"
       
    27 
       
    28 const TInt KStackSize=0x1000;
       
    29 const TInt KHeapMaxSize=0x100000;
       
    30 const TInt KMajorVersionNumber=1;
       
    31 const TInt KMinorVersionNumber=0;
       
    32 const TInt KBuildVersionNumber=1;
       
    33 const TInt KNumMessageSlots=20;
       
    34 
       
    35 _LIT(KServerName,"StressSvr");
       
    36 
       
    37 LOCAL_D RTest test(_L("T_SVR3"));
       
    38 
       
    39 class CMySession : public CSession2
       
    40 	{
       
    41 public:
       
    42 	CMySession();
       
    43 	virtual void ServiceL(const RMessage2& aMessage);			 //pure virtual fns.
       
    44 	void Process(const RMessage2& aMessage);
       
    45 public:
       
    46 	TInt iOutstanding;
       
    47 	TUint iSeed[2];
       
    48 	};
       
    49 
       
    50 class CMyServer : public CServer2
       
    51 	{
       
    52 public:
       
    53 	enum {ETest};
       
    54 public:
       
    55 	CMyServer(TInt aPriority);
       
    56 	static CMyServer* New(TInt aPriority);
       
    57 	virtual CSession2* NewSessionL(const TVersion& aVersion, const RMessage2& aMessage) const;//Overloading
       
    58 	};
       
    59 
       
    60 class CMyActiveScheduler : public CActiveScheduler
       
    61 	{
       
    62 public:
       
    63 	virtual void Error(TInt anError) const;  //Overloading pure virtual function
       
    64 	};
       
    65 
       
    66 class RStressSvr : public RSessionBase
       
    67 	{
       
    68 public:
       
    69 	TInt Connect();
       
    70 	TInt Test();
       
    71 	void Test(TRequestStatus& aStatus);
       
    72 	TVersion Version();
       
    73 	};
       
    74 
       
    75 class CThread : public CActive
       
    76 	{
       
    77 public:
       
    78 	CThread(TInt aPriority);
       
    79 	~CThread();
       
    80 	virtual void RunL();
       
    81 	virtual void DoCancel();
       
    82 	virtual void DisplayStats()=0;
       
    83 	TInt Start();
       
    84 public:
       
    85 	virtual TInt StartThread()=0;
       
    86 public:
       
    87 	RThread iThread;
       
    88 	TInt iExitCount;
       
    89 	TInt iServerTerminatedCount;
       
    90 	TInt iTerminateCount;
       
    91 	};
       
    92 
       
    93 class CServerThread : public CThread
       
    94 	{
       
    95 public:
       
    96 	static void NewL();
       
    97 	CServerThread();
       
    98 	virtual TInt StartThread();
       
    99 	virtual void DisplayStats();
       
   100 public:
       
   101 	TInt iMessagesReceived;
       
   102 	};
       
   103 
       
   104 class CClientThread : public CThread
       
   105 	{
       
   106 public:
       
   107 	static void NewL(TInt anId);
       
   108 	CClientThread(TInt anId);
       
   109 	virtual TInt StartThread();
       
   110 	virtual void DisplayStats();
       
   111 public:
       
   112 	TInt iId;
       
   113 	TInt iCloses;
       
   114 	};
       
   115 
       
   116 class CRandomTimer : public CActive
       
   117 	{
       
   118 public:
       
   119 	static void NewL();
       
   120 	CRandomTimer(TInt aPriority);
       
   121 	~CRandomTimer();
       
   122 	virtual void RunL();
       
   123 	virtual void DoCancel();
       
   124 	void Start();
       
   125 public:
       
   126 	RTimer iTimer;
       
   127 	TUint iSeed[2];
       
   128 	TInt iCount;
       
   129 	};
       
   130 
       
   131 class CStatsTimer : public CActive
       
   132 	{
       
   133 public:
       
   134 	static void NewL();
       
   135 	CStatsTimer(TInt aPriority);
       
   136 	~CStatsTimer();
       
   137 	virtual void RunL();
       
   138 	virtual void DoCancel();
       
   139 	void Start();
       
   140 public:
       
   141 	RTimer iTimer;
       
   142 	TInt iInitFreeRam;
       
   143 	TInt iMaxDelta;
       
   144 	TInt iCount;
       
   145 	};
       
   146 
       
   147 const TInt KNumClients=3;
       
   148 LOCAL_D CServerThread* TheServer;
       
   149 LOCAL_D CClientThread* TheClients[KNumClients];
       
   150 LOCAL_D CRandomTimer* TheRandomTimer;
       
   151 
       
   152 CMySession::CMySession()
       
   153 //
       
   154 // Constructor
       
   155 //
       
   156 	{
       
   157 	iSeed[0]=User::TickCount();
       
   158 	}
       
   159 
       
   160 CMyServer* CMyServer::New(TInt aPriority)
       
   161 //
       
   162 // Create a new CMyServer.
       
   163 //
       
   164 	{
       
   165 
       
   166 	return new CMyServer(aPriority);
       
   167 	}
       
   168 
       
   169 CMyServer::CMyServer(TInt aPriority)
       
   170 //
       
   171 // Constructor.
       
   172 //
       
   173 	: CServer2(aPriority)
       
   174 	{}
       
   175 
       
   176 CSession2* CMyServer::NewSessionL(const TVersion& aVersion, const RMessage2&) const
       
   177 //
       
   178 // Create a new client for this server.
       
   179 //
       
   180 	{
       
   181 
       
   182 	TVersion v(KMajorVersionNumber,KMinorVersionNumber,KBuildVersionNumber);
       
   183 	if (!User::QueryVersionSupported(v,aVersion))
       
   184 		User::Leave(KErrNotSupported);
       
   185 	return new(ELeave) CMySession;
       
   186 	}
       
   187 
       
   188 void CMySession::ServiceL(const RMessage2& aMessage)
       
   189 //
       
   190 // Handle messages for this server.
       
   191 //
       
   192 	{
       
   193 
       
   194 	++TheServer->iMessagesReceived;
       
   195 	switch (aMessage.Function())
       
   196 		{
       
   197 		case CMyServer::ETest:
       
   198 			if (iOutstanding==KNumMessageSlots-1)
       
   199 				Process(aMessage);
       
   200 			else
       
   201 				++iOutstanding;
       
   202 			break;
       
   203 		default:
       
   204 			aMessage.Complete(KErrNotSupported);
       
   205 			break;
       
   206 		}
       
   207 	}
       
   208 
       
   209 void CMySession::Process(const RMessage2& aMessage)
       
   210 	{
       
   211 	TUint x=Random(iSeed)&16383;
       
   212 	if (x==0)
       
   213 		User::Exit(0);			// exit the server
       
   214 	else if (x<8)
       
   215 		aMessage.Terminate(0);	// terminate the client
       
   216 	else
       
   217 		aMessage.Complete(KErrNone);
       
   218 	}
       
   219 
       
   220 void CMyActiveScheduler::Error(TInt anError) const
       
   221 //
       
   222 // Called if any Run() method leaves.
       
   223 //
       
   224 	{
       
   225 
       
   226 	User::Panic(_L("Server Error"),anError);
       
   227 	}
       
   228 
       
   229 TInt RStressSvr::Connect()
       
   230 //
       
   231 // Connect to the server
       
   232 //
       
   233 	{
       
   234 
       
   235 	return CreateSession(KServerName,Version(),KNumMessageSlots);
       
   236 	}
       
   237 
       
   238 TInt RStressSvr::Test()
       
   239 //
       
   240 // Send a message and wait for completion.
       
   241 //
       
   242 	{
       
   243 
       
   244 	return SendReceive(CMyServer::ETest);
       
   245 	}
       
   246 
       
   247 void RStressSvr::Test(TRequestStatus& aStatus)
       
   248 //
       
   249 // Send a message asynchronously
       
   250 //
       
   251 	{
       
   252 
       
   253 	SendReceive(CMyServer::ETest,aStatus);
       
   254 	}
       
   255 
       
   256 TVersion RStressSvr::Version()
       
   257 //
       
   258 // Return the current version.
       
   259 //
       
   260 	{
       
   261 
       
   262 	TVersion v(KMajorVersionNumber,KMinorVersionNumber,KBuildVersionNumber);
       
   263 	return(v);
       
   264 	}
       
   265 
       
   266 LOCAL_C TInt ServerThread(TAny*)
       
   267 	{
       
   268 
       
   269 	CMyActiveScheduler* pR=new CMyActiveScheduler;
       
   270 	if (!pR)
       
   271 		return KErrNoMemory;
       
   272 	CActiveScheduler::Install(pR);
       
   273 	CMyServer* pS=CMyServer::New(0);
       
   274 	if (!pS)
       
   275 		return KErrNoMemory;
       
   276 	TInt r=pS->Start(KServerName);
       
   277 	if (r!=KErrNone)
       
   278 		return r;
       
   279 
       
   280 	CActiveScheduler::Start();
       
   281 
       
   282 	delete pS;
       
   283 	delete pR;
       
   284 	return KErrNone;
       
   285 	}
       
   286 
       
   287 LOCAL_C TInt ClientThread(TAny* aPtr)
       
   288 	{
       
   289 	CClientThread* pT=(CClientThread*)aPtr;
       
   290 	RStressSvr d;
       
   291 	TUint seed[2];
       
   292 	seed[0]=User::TickCount();
       
   293 	seed[1]=0;
       
   294 	FOREVER
       
   295 		{
       
   296 		TInt r;
       
   297 		FOREVER
       
   298 			{
       
   299 			r=d.Connect();
       
   300 			if (r!=KErrNotFound)
       
   301 				break;
       
   302 			User::After(50000);
       
   303 			}
       
   304 		if (r!=KErrNone)
       
   305 			return r;
       
   306 		TRequestStatus s[KNumMessageSlots];
       
   307 		TInt i;
       
   308 		for (i=0; i<KNumMessageSlots-1; i++)
       
   309 			d.Test(s[i]);
       
   310 		TInt n=Random(seed)&16383;
       
   311 		for (i=0; i<n; i++)
       
   312 			d.Test();
       
   313 		d.Close();
       
   314 		++pT->iCloses;
       
   315 		}
       
   316 	}
       
   317 
       
   318 CThread::CThread(TInt aPriority)
       
   319 	: CActive(aPriority)
       
   320 	{
       
   321 	}
       
   322 
       
   323 CThread::~CThread()
       
   324 	{
       
   325 	Cancel();
       
   326 	iThread.Kill(0);
       
   327 	iThread.Close();
       
   328 	}
       
   329 
       
   330 void CThread::RunL()
       
   331 	{
       
   332 	TExitType exitType=iThread.ExitType();
       
   333 	TInt exitReason=iThread.ExitReason();
       
   334 	TBuf<32> exitCat=iThread.ExitCategory();
       
   335 	TBool bad=EFalse;
       
   336 	if (exitType==EExitKill)
       
   337 		{
       
   338 		if (exitReason!=KErrNone && exitReason!=KErrServerTerminated)
       
   339 			bad=ETrue;
       
   340 		}
       
   341 	else if (exitType==EExitPanic)
       
   342 		bad=ETrue;
       
   343 	if (bad)
       
   344 		{
       
   345 		TFullName n(iThread.FullName());
       
   346 		test.Printf(_L("Thread %S exited %d,%d,%S\n"),&n,exitType,exitReason,&exitCat);
       
   347 		CActiveScheduler::Stop();
       
   348 		return;
       
   349 		}
       
   350 	iThread.Close();
       
   351 	if (exitType==EExitTerminate)
       
   352 		++iTerminateCount;
       
   353 	else if (exitReason==KErrNone)
       
   354 		++iExitCount;
       
   355 	else if (exitReason==KErrServerTerminated)
       
   356 		++iServerTerminatedCount;
       
   357 	TInt r=Start();
       
   358 	if (r!=KErrNone)
       
   359 		{
       
   360 		test.Printf(_L("Start thread error %d\n"),r);
       
   361 		CActiveScheduler::Stop();
       
   362 		}
       
   363 	}
       
   364 
       
   365 void CThread::DoCancel()
       
   366 	{
       
   367 	iThread.LogonCancel(iStatus);
       
   368 	}
       
   369 
       
   370 TInt CThread::Start()
       
   371 	{
       
   372 	TInt r;
       
   373 	FOREVER
       
   374 		{
       
   375 		r=StartThread();
       
   376 		if (r==KErrNone)
       
   377 			break;
       
   378 		if (r!=KErrAlreadyExists)
       
   379 			break;
       
   380 		User::After(100000);
       
   381 		}
       
   382 	if (r==KErrNone)
       
   383 		{
       
   384 		iThread.Logon(iStatus);
       
   385 		SetActive();
       
   386 		}
       
   387 	return r;
       
   388 	}
       
   389 
       
   390 CServerThread::CServerThread()
       
   391 	: CThread(0)
       
   392 	{
       
   393 	}
       
   394 
       
   395 TInt CServerThread::StartThread()
       
   396 	{
       
   397 	TUint seed[2];
       
   398 	seed[1]=0;
       
   399 	seed[0]=User::TickCount();
       
   400 	TInt heapMin=TInt(Random(seed)&0x0f)+1;
       
   401 	heapMin<<=12;
       
   402 	TInt r=iThread.Create(KNullDesC(),ServerThread,KStackSize,heapMin,KHeapMaxSize,this);	// use unnamed thread
       
   403 	if (r!=KErrNone)
       
   404 		return r;
       
   405 	iThread.Resume();
       
   406 	return KErrNone;
       
   407 	}
       
   408 
       
   409 void CServerThread::NewL()
       
   410 	{
       
   411 	CServerThread* pT=new (ELeave) CServerThread;
       
   412 	TheServer=pT;
       
   413 	CActiveScheduler::Add(pT);
       
   414 	User::LeaveIfError(pT->Start());
       
   415 	}
       
   416 
       
   417 void CServerThread::DisplayStats()
       
   418 	{
       
   419 	test.Printf(_L("Svr  : X:%9d ST:%9d T:%9d RX:%9d\n"),iExitCount,iServerTerminatedCount,iTerminateCount,iMessagesReceived);
       
   420 	}
       
   421 
       
   422 CClientThread::CClientThread(TInt anId)
       
   423 	: CThread(0), iId(anId)
       
   424 	{
       
   425 	}
       
   426 
       
   427 TInt CClientThread::StartThread()
       
   428 	{
       
   429 	TInt r=iThread.Create(KNullDesC(),ClientThread,KStackSize,NULL,this);	// use unnamed thread
       
   430 	if (r!=KErrNone)
       
   431 		return r;
       
   432 	iThread.Resume();
       
   433 	return KErrNone;
       
   434 	}
       
   435 
       
   436 void CClientThread::NewL(TInt anId)
       
   437 	{
       
   438 	CClientThread* pT=new (ELeave) CClientThread(anId);
       
   439 	TheClients[anId]=pT;
       
   440 	CActiveScheduler::Add(pT);
       
   441 	User::LeaveIfError(pT->Start());
       
   442 	}
       
   443 
       
   444 void CClientThread::DisplayStats()
       
   445 	{
       
   446 	test.Printf(_L("Cli %1d: X:%9d ST:%9d T:%9d CL:%9d\n"),iId,iExitCount,iServerTerminatedCount,iTerminateCount,iCloses);
       
   447 	}
       
   448 
       
   449 void CRandomTimer::NewL()
       
   450 	{
       
   451 	CRandomTimer* pR=new (ELeave) CRandomTimer(20);
       
   452 	User::LeaveIfError(pR->iTimer.CreateLocal());
       
   453 	CActiveScheduler::Add(pR);
       
   454 	TheRandomTimer=pR;
       
   455 	pR->Start();
       
   456 	}
       
   457 
       
   458 CRandomTimer::CRandomTimer(TInt aPriority)
       
   459 	: CActive(aPriority)
       
   460 	{
       
   461 	iSeed[0]=User::TickCount();
       
   462 	}
       
   463 
       
   464 CRandomTimer::~CRandomTimer()
       
   465 	{
       
   466 	Cancel();
       
   467 	iTimer.Close();
       
   468 	}
       
   469 
       
   470 void CRandomTimer::RunL()
       
   471 	{
       
   472 	++iCount;
       
   473 	TUint x=Random(iSeed)&3;
       
   474 	CThread* pT;
       
   475 	if (x==0)
       
   476 		pT=TheServer;
       
   477 	else
       
   478 		pT=TheClients[x-1];
       
   479 	pT->iThread.Kill(0);
       
   480 	Start();
       
   481 	}
       
   482 
       
   483 void CRandomTimer::Start()
       
   484 	{
       
   485 	TUint x=Random(iSeed)&63;
       
   486 	x+=32;
       
   487 	iTimer.HighRes(iStatus, x*1000);
       
   488 	SetActive();
       
   489 	}
       
   490 
       
   491 void CRandomTimer::DoCancel()
       
   492 	{
       
   493 	iTimer.Cancel();
       
   494 	}
       
   495 
       
   496 void CStatsTimer::NewL()
       
   497 	{
       
   498 	CStatsTimer* pT=new (ELeave) CStatsTimer(-10);
       
   499 	User::LeaveIfError(pT->iTimer.CreateLocal());
       
   500 	CActiveScheduler::Add(pT);
       
   501 	pT->Start();
       
   502 	}
       
   503 
       
   504 CStatsTimer::CStatsTimer(TInt aPriority)
       
   505 	: CActive(aPriority)
       
   506 	{
       
   507 	iInitFreeRam = FreeRam();
       
   508 	}
       
   509 
       
   510 CStatsTimer::~CStatsTimer()
       
   511 	{
       
   512 	Cancel();
       
   513 	iTimer.Close();
       
   514 	}
       
   515 
       
   516 void CStatsTimer::RunL()
       
   517 	{
       
   518 	TheServer->DisplayStats();
       
   519 	TInt i;
       
   520 	for (i=0; i<KNumClients; i++)
       
   521 		TheClients[i]->DisplayStats();
       
   522 	test.Printf(_L("RndTm: %9d\n"),TheRandomTimer->iCount);
       
   523 	TInt free_ram = FreeRam();
       
   524 	TInt delta_ram = iInitFreeRam - free_ram;
       
   525 	if (delta_ram > iMaxDelta)
       
   526 		iMaxDelta = delta_ram;
       
   527 	if (++iCount==10)
       
   528 		{
       
   529 		test.Printf(_L("Max RAM delta %dK Free RAM %08x\n"), iMaxDelta/1024, free_ram);
       
   530 		iCount=0;
       
   531 		}
       
   532 	Start();
       
   533 	}
       
   534 
       
   535 void CStatsTimer::Start()
       
   536 	{
       
   537 	iTimer.After(iStatus, 1000000);
       
   538 	SetActive();
       
   539 	}
       
   540 
       
   541 void CStatsTimer::DoCancel()
       
   542 	{
       
   543 	iTimer.Cancel();
       
   544 	}
       
   545 
       
   546 void InitialiseL()
       
   547 	{
       
   548 	CActiveScheduler* pA=new (ELeave) CActiveScheduler;
       
   549 	CActiveScheduler::Install(pA);
       
   550 	CServerThread::NewL();
       
   551 	TInt id;
       
   552 	for (id=0; id<KNumClients; id++)
       
   553 		CClientThread::NewL(id);
       
   554 	CRandomTimer::NewL();
       
   555 	CStatsTimer::NewL();
       
   556 	}
       
   557 
       
   558 GLDEF_C TInt E32Main()
       
   559 //
       
   560 // Test timers.
       
   561 //
       
   562 	{
       
   563 
       
   564 	test.Title();
       
   565 
       
   566 	RThread().SetPriority(EPriorityMore);
       
   567 
       
   568 	TRAPD(r,InitialiseL());
       
   569 	test(r==KErrNone);
       
   570 
       
   571 	CActiveScheduler::Start();
       
   572 
       
   573 	test(0);
       
   574 
       
   575 	return(0);
       
   576 	}
       
   577 
       
   578 // Override heap creation for this process
       
   579 // This function runs at the beginning of every thread
       
   580 // Initial heap is shared but subsequent heaps are single threaded
       
   581 TInt UserHeap::SetupThreadHeap(TBool aNotFirst, SStdEpocThreadCreateInfo& aInfo)
       
   582 	{
       
   583 	TInt r = KErrNone;
       
   584 	if (!aInfo.iAllocator && aInfo.iHeapInitialSize>0)
       
   585 		{
       
   586 		// new heap required
       
   587 		RHeap* pH = NULL;
       
   588 		r = CreateThreadHeap(aInfo, pH, 0, aNotFirst);
       
   589 		}
       
   590 	else if (aInfo.iAllocator)
       
   591 		{
       
   592 		// sharing a heap
       
   593 		RAllocator* pA = aInfo.iAllocator;
       
   594 		pA->Open();
       
   595 		User::SwitchAllocator(pA);
       
   596 		}
       
   597 	return r;
       
   598 	}
       
   599