os/kernelhwsrv/kerneltest/e32test/misc/t_svrstress.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 2008-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_svrstress.cpp
    15 // This is a stress test for client server session connect and disconnect
    16 //
    17 
    18 #include <e32base.h>
    19 #include <e32base_private.h>
    20 #define __E32TEST_EXTENSION__
    21 #include <e32test.h>
    22 #include <e32svr.h>
    23 #include "u32std.h"
    24 #include <e32atomics.h>
    25 #include <e32panic.h>
    26 #include <e32def.h>
    27 #include <e32def_private.h>
    28 
    29 RTest test(_L("T_SVRSTRESS"));
    30 
    31 RSemaphore SyncSemaphore;
    32 TUint32 WaitABit;
    33 
    34 TInt NumMessageSlots;
    35 TInt UseGlobalMessagePool;
    36 
    37 const TInt BigDesLength = 256 * 1024;
    38 
    39 #if 1
    40 #define TRACE(t) RDebug::RawPrint(_L8(t))
    41 #else
    42 #define TRACE(t)
    43 #endif
    44 
    45 //
    46 // utility functions...
    47 //
    48 
    49 void WaitForRequest()
    50 	{
    51 	User::WaitForAnyRequest();
    52 	RThread().RequestSignal(); // put request semaphore count back
    53 	}
    54 
    55 
    56 TInt WaitForRequest(TRequestStatus& aStatus,TTimeIntervalMicroSeconds32 aTimeout=2*1000000)
    57 	{
    58 	RTimer timer;
    59 	test_Equal(KErrNone,timer.CreateLocal());
    60 
    61 	TRequestStatus timeoutStatus;
    62 	timer.After(timeoutStatus,aTimeout);
    63 
    64 	User::WaitForRequest(aStatus,timeoutStatus);
    65 
    66 	TInt r;
    67 	if(aStatus.Int()==KRequestPending)
    68 		{
    69 		r = KErrTimedOut;
    70 		}
    71 	else
    72 		{
    73 		r = KErrNone;
    74 		timer.Cancel();
    75 		User::WaitForRequest(timeoutStatus);
    76 		}
    77 
    78 	CLOSE_AND_WAIT(timer);
    79 
    80 	return r;
    81 	}
    82 
    83 
    84 //
    85 // CMyServer
    86 //
    87 
    88 _LIT(KMyServerName,"StressSvr");
    89 
    90 class CMyServer : public CServer2
    91 	{
    92 public:
    93 	CMyServer(TInt aPriority);
    94 	static CMyServer* New(TInt aPriority);
    95 	virtual CSession2* NewSessionL(const TVersion&, const RMessage2&) const;
    96 	};
    97 
    98 
    99 class CMySession : public CSession2
   100 	{
   101 public:
   102 	virtual void ServiceL(const RMessage2& aMessage);
   103 	};
   104 
   105 
   106 CMyServer* CMyServer::New(TInt aPriority)
   107 	{
   108 	return new CMyServer(aPriority);
   109 	}
   110 
   111 
   112 CMyServer::CMyServer(TInt aPriority)
   113 	: CServer2(aPriority, ESharableSessions)
   114 	{}
   115 
   116 
   117 CSession2* CMyServer::NewSessionL(const TVersion&, const RMessage2&) const
   118 	{
   119 	TRACE("O");
   120 	return new(ELeave) CMySession;
   121 	}
   122 
   123 
   124 TBool RestartServer;
   125 
   126 TInt MyServerThread(TAny*)
   127 	{
   128 	CActiveScheduler* pR=new CActiveScheduler;
   129 	if(!pR)
   130 		return KErrNoMemory;
   131 	CActiveScheduler::Install(pR);
   132 	RestartServer = ETrue;
   133 
   134 	while(RestartServer)
   135 		{
   136 		__UHEAP_MARK;
   137 		CMyServer* pS=CMyServer::New(0);
   138 		if(!pS)
   139 			return KErrNoMemory;
   140 		TInt r = pS->Start(KMyServerName);
   141 		if(r!=KErrNone)
   142 			return r;
   143 
   144 		TRACE("S");
   145 		RThread::Rendezvous(KErrNone);
   146 
   147 		CActiveScheduler::Start();
   148 
   149 		delete pS;
   150 		__UHEAP_MARKEND;
   151 		}
   152 
   153 	delete pR;
   154 	return KErrNone;
   155 	}
   156 
   157 
   158 //
   159 // RMyServer
   160 //
   161 
   162 class RMyServer : public RSessionBase
   163 	{
   164 public:
   165 	enum TFunction
   166 		{
   167 		EStop,
   168 		ESync,
   169 		EPing,
   170 		EShutdown,
   171 		ECompleteWhileCopying
   172 		};
   173 public:
   174 	TInt Connect();
   175 
   176 	inline TInt Send(TFunction aFunction) const
   177 		{ return SendReceive(aFunction); }
   178 
   179 	inline TInt Send(TFunction aFunction, const TIpcArgs& aArgs) const
   180 		{ return SendReceive(aFunction, aArgs); }
   181 
   182 	inline void Send(TFunction aFunction, TRequestStatus& aStatus) const
   183 		{ SendReceive(aFunction, aStatus); }
   184 
   185 	inline void Send(TFunction aFunction, const TIpcArgs& aArgs, TRequestStatus& aStatus) const
   186 		{ SendReceive(aFunction, aArgs, aStatus); }
   187 	};
   188 
   189 
   190 TInt RMyServer::Connect()
   191 	{
   192 	RMyServer temp;
   193 	TInt r = temp.CreateSession(KMyServerName, TVersion(), UseGlobalMessagePool ? -1 : NumMessageSlots);
   194 	if(r!=KErrNone)
   195 		return r;
   196 
   197 	// turn handle into process owned...
   198 	RMyServer temp2(temp);
   199 	r = temp2.Duplicate(RThread());
   200 	temp.Close();
   201 
   202 	*this = temp2;
   203 	return r;
   204 	}
   205 
   206 
   207 
   208 //
   209 // CMySession
   210 //
   211 
   212 TInt CopierThread(TAny* aPtr)
   213 	{
   214 	RMessage2& msg = *(RMessage2*)aPtr;
   215 	HBufC* bigdes = HBufC::NewMax(BigDesLength);
   216 	if (bigdes == NULL)
   217 		return KErrNoMemory;
   218 	TPtr ptr = bigdes->Des();
   219 	RThread().Rendezvous(KErrNone);
   220 	RDebug::Print(_L("START\n"));
   221 	TInt r = msg.Read(2, ptr);
   222 	RDebug::Print(_L("DONE\n"));
   223 	delete bigdes;
   224 	return r;
   225 	}
   226 
   227 void CMySession::ServiceL(const RMessage2& aMessage)
   228 	{
   229 	RThread client;
   230 	RThread copier;
   231 	aMessage.Client(client);
   232 	TRequestStatus* s;
   233 	TRequestStatus* s2;
   234 	TRequestStatus logon, rendez;
   235 	TInt r;
   236 	s = (TRequestStatus*)aMessage.Ptr0();
   237 
   238 	switch(aMessage.Function())
   239 		{
   240 	case RMyServer::EStop:
   241 		TRACE("E");
   242 		CActiveScheduler::Stop();
   243 		break;
   244 
   245 	case RMyServer::ESync:
   246 		TRACE("Y");
   247 		client.RequestComplete(s,KErrNone);		// let client know we've received the message
   248 		SyncSemaphore.Wait();					// wait for signal from client
   249 		s = (TRequestStatus*)aMessage.Ptr1();	// use second status for later end signal
   250 		aMessage.Complete(KErrNone);			// complete the message
   251 		break;
   252 
   253 	case RMyServer::EPing:
   254 		TRACE("P");
   255 		aMessage.Complete(KErrNone);
   256 		break;
   257 
   258 	case RMyServer::EShutdown:
   259 		TRACE("D");
   260 		RestartServer = EFalse;
   261 		CActiveScheduler::Stop();
   262 		break;
   263 
   264 	case RMyServer::ECompleteWhileCopying:
   265 		s2 = (TRequestStatus*)aMessage.Ptr1();
   266 		r = copier.Create(_L("Copier"),CopierThread,KDefaultStackSize,&User::Allocator(),(TAny*)&aMessage);
   267 		if (r == KErrNone)
   268 			{
   269 			copier.Logon(logon);
   270 			copier.Rendezvous(rendez);
   271 			copier.SetPriority(EPriorityLess);
   272 			copier.Resume();
   273 			User::WaitForRequest(rendez);
   274 			User::AfterHighRes(5000); // 5ms delay to let copy actually start
   275 			RDebug::Print(_L("COMPLETING\n"));
   276 			aMessage.Complete(KErrNone);
   277 			User::WaitForRequest(logon);
   278 			copier.Close();
   279 			}
   280 		client.RequestComplete(s,r);
   281 		s = s2;
   282 		break;
   283 
   284 	default:
   285 		TRACE("?");
   286 		aMessage.Complete(KErrNotSupported);
   287 		break;
   288 		}
   289 
   290 	// let client know we've completed the message...
   291 	TRACE("X");
   292 	client.RequestComplete(s,KErrNone);
   293 
   294 	client.Close();
   295 	}
   296 
   297 
   298 
   299 //
   300 // RStressThread
   301 //
   302 
   303 class RStressThread
   304 	{
   305 public:
   306 	RStressThread(TThreadFunction aThreadFunction, const char* aName, TInt aDelay=-1);
   307 	~RStressThread();
   308 	void Start();
   309 	void Restart();
   310 	void Stop();
   311 	// for use by thread...
   312 	static RStressThread& Begin(TAny* aInfo);
   313 	TBool Loop();
   314 private:
   315 	TThreadFunction iThreadFunction;
   316 	const char* iName;
   317 	RThread iThread;
   318 	TRequestStatus iLogon;
   319 	TUint iCount;
   320 	TBool iStop;
   321 	TInt iDelay;
   322 
   323 private:
   324 	static TInt iInstanceCounter;
   325 	};
   326 
   327 
   328 TInt RStressThread::iInstanceCounter = 0;
   329 
   330 
   331 RStressThread::RStressThread(TThreadFunction aThreadFunction, const char* aName, TInt aDelay)
   332 	: iThreadFunction(aThreadFunction), iName(aName), iLogon(KErrNone), iDelay(aDelay)
   333 	{
   334 	iThread.SetHandle(0);
   335 	}
   336 
   337 
   338 RStressThread::~RStressThread()
   339 	{
   340 	Stop();
   341 	}
   342 
   343 
   344 void RStressThread::Start()
   345 	{
   346 	iStop = false;
   347 	iCount = 0;
   348 
   349 	TBuf<KMaxKernelName> name;
   350 	name.Copy(TPtrC8((const TUint8*)iName));
   351 	name.Append((TText)'-');
   352 	name.AppendNum(iInstanceCounter++);
   353 	test_Equal(KErrNone,iThread.Create(name,iThreadFunction,KDefaultStackSize,&User::Allocator(),this));
   354 
   355 	iThread.Logon(iLogon);
   356 	test_Equal(KRequestPending,iLogon.Int());
   357 
   358 	TRequestStatus rendezvous;
   359 	iThread.Rendezvous(rendezvous);
   360 
   361 	iThread.Resume();
   362 
   363 	User::WaitForRequest(rendezvous);
   364 	test_Equal(KErrNone,rendezvous.Int());
   365 	}
   366 
   367 
   368 void RStressThread::Stop()
   369 	{
   370 	if(!iThread.Handle())
   371 		return; // thread not running
   372 
   373 	iStop = true;
   374 	RDebug::Printf("RStressThread::Stop %s (count=%d)",iName,iCount);
   375 	if(WaitForRequest(iLogon,10*1000000)!=KErrNone)
   376 		test(0);
   377 	CLOSE_AND_WAIT(iThread);
   378 	}
   379 
   380 
   381 void RStressThread::Restart()
   382 	{
   383 	if(iThread.Handle())
   384 		{
   385 		if(iLogon==KRequestPending)
   386 			return; // thread still running
   387 
   388 		User::WaitForRequest(iLogon);
   389 		CLOSE_AND_WAIT(iThread);
   390 		}
   391 
   392 	Start();
   393 	}
   394 
   395 
   396 TBool RStressThread::Loop()
   397 	{
   398 	if(iDelay>=0)
   399 		User::AfterHighRes(iDelay);
   400 	++iCount;
   401 	return !iStop;
   402 	}
   403 
   404 
   405 RStressThread& RStressThread::Begin(TAny* aInfo)
   406 	{
   407 	RStressThread& t = *(RStressThread*)aInfo;
   408 	if(t.iDelay>=0)
   409 		RThread().SetPriority(EPriorityMore); // so this preempts threads after delay
   410 	RThread::Rendezvous(KErrNone);
   411 	return t;
   412 	}
   413 
   414 //
   415 //
   416 //
   417 
   418 
   419 RMyServer Session;
   420 RThread ServerThread;
   421 
   422 
   423 void NewSession()
   424 	{
   425 	RMyServer newSession;
   426 	TRACE("o");
   427 	test_Equal(KErrNone,newSession.Connect());
   428 
   429 	RMyServer oldSession(Session);
   430 	Session = newSession;
   431 
   432 	TRACE("c");
   433 	if(oldSession.Handle())
   434 		CLOSE_AND_WAIT(oldSession);
   435 	}
   436 
   437 
   438 TInt SessionCloserThread(TAny* aInfo)
   439 	{
   440 	RStressThread& t = RStressThread::Begin(aInfo);
   441 	do
   442 		{
   443 		NewSession();
   444 		}
   445 	while(t.Loop());
   446 	return KErrNone;
   447 	}
   448 
   449 
   450 TInt ServerStopperThread(TAny* aInfo)
   451 	{
   452 	RStressThread& t = RStressThread::Begin(aInfo);
   453 	do
   454 		{
   455 		TRACE("s");
   456 		TRequestStatus rendezvous;
   457 		ServerThread.Rendezvous(rendezvous);
   458 
   459 		TRequestStatus s1 = KRequestPending;
   460 		TRequestStatus s2;
   461 		Session.Send(RMyServer::EStop,TIpcArgs(&s1),s2);
   462 		User::WaitForRequest(s1,s2);
   463 		if(s2!=KRequestPending)
   464 			{
   465 			test_Equal(KErrServerTerminated,s2.Int());
   466 			User::WaitForRequest(s1);
   467 			}
   468 
   469 		User::WaitForRequest(rendezvous);
   470 		NewSession();
   471 		}
   472 	while(t.Loop());
   473 	return KErrNone;
   474 	}
   475 
   476 
   477 TInt SessionPingerThread(TAny* aInfo)
   478 	{
   479 	RStressThread& t = RStressThread::Begin(aInfo);
   480 	do
   481 		{
   482 		TRACE("p");
   483 		TRequestStatus s1 = KRequestPending;
   484 		TRequestStatus s2;
   485 		Session.Send(RMyServer::EPing,TIpcArgs(&s1),s2);
   486 		User::WaitForRequest(s1,s2);
   487 		if(s2.Int()==KErrNone)
   488 			{
   489 			// message completed OK, wait for servers extra signal
   490 			User::WaitForRequest(s1);
   491 			}
   492 		else if(s2.Int()==KErrServerTerminated)
   493 			{
   494 			// server died before message processed, there shouldn't be an extra signal
   495 			test_Equal(KRequestPending,s1.Int());
   496 			}
   497 		else
   498 			{
   499 			// assume message was completed by server, but we didn't get signalled because session was closed
   500 			test_Equal(KRequestPending,s2.Int());
   501 			test_Equal(KErrNone,s1.Int());
   502 			}
   503 		}
   504 	while(t.Loop());
   505 	return KErrNone;
   506 	}
   507 
   508 
   509 void TestInit()
   510 	{
   511 	RThread().SetPriority(EPriorityMuchMore); // so this main thread is higher priority than workers
   512 
   513 	test_Equal(KErrNone,SyncSemaphore.CreateLocal(0,EOwnerProcess));
   514 
   515 	// calculate async cleanup timeout value...
   516 	TInt factor = UserSvr::HalFunction(EHalGroupVariant, EVariantHalTimeoutExpansion, 0, 0);
   517 	if (factor<=0)
   518 		factor = 1;
   519 	if (factor>1024)
   520 		factor = 1024;
   521 	WaitABit = 200000 * (TUint32)factor;
   522 	}
   523 
   524 
   525 void StartServer()
   526 	{
   527 	// start test server...
   528 	test_Equal(KErrNone,ServerThread.Create(_L("Server"),MyServerThread,KDefaultStackSize,1<<12,1<<20,0));
   529 	TRequestStatus rendezvous;
   530 	ServerThread.Rendezvous(rendezvous);
   531 	ServerThread.Resume();
   532 	User::WaitForRequest(rendezvous);
   533 	test_Equal(KErrNone,rendezvous.Int());
   534 	test_Equal(EExitPending,ServerThread.ExitType());
   535 	}
   536 
   537 
   538 void StopServer()
   539 	{
   540 	TRequestStatus logon;
   541 	NewSession();
   542 	TRequestStatus s1 = KRequestPending;
   543 	TRequestStatus s2;
   544 	ServerThread.Logon(logon);
   545 	Session.Send(RMyServer::EShutdown,TIpcArgs(&s1),s2);
   546 	User::WaitForRequest(s1,s2);
   547 	if(s2!=KRequestPending)
   548 		{
   549 		test_Equal(KErrServerTerminated,s2.Int());
   550 		User::WaitForRequest(s1);
   551 		}
   552 	CLOSE_AND_WAIT(Session);
   553 	User::WaitForRequest(logon);
   554 	test_KErrNone(logon.Int());
   555 	test_Equal(EExitKill, ServerThread.ExitType());
   556 	CLOSE_AND_WAIT(ServerThread);
   557 	}
   558 
   559 
   560 void TestMessageCompleteOnClosedSession()
   561 	{
   562 	__KHEAP_MARK;
   563 
   564 	test.Start(_L("Start server"));
   565 	StartServer();
   566 
   567 	test.Next(_L("Connect"));
   568 	test_Equal(KErrNone,Session.Connect());
   569 
   570 	test.Next(_L("Send message"));
   571 	TRequestStatus s1 = KRequestPending;
   572 	TRequestStatus s2 = KRequestPending;
   573 	TRequestStatus s3;
   574 	Session.Send(RMyServer::ESync,TIpcArgs(&s1,&s2),s3);
   575 	test_Equal(KRequestPending,s3.Int());
   576 
   577 	test.Next(_L("Wait for s1"));
   578 	test_Equal(KErrNone,WaitForRequest(s1));
   579 	test_Equal(KErrNone,s1.Int());
   580 	test_Equal(KRequestPending,s2.Int());
   581 	test_Equal(KRequestPending,s3.Int());
   582 
   583 	test.Next(_L("Close session"));
   584 	Session.Close();
   585 	test_Equal(KRequestPending,s2.Int());
   586 	test_Equal(KRequestPending,s3.Int());
   587 
   588 	test.Next(_L("Trigger message completion"));
   589 	SyncSemaphore.Signal();
   590 
   591 	test.Next(_L("Wait for s2"));
   592 	test_Equal(KErrNone,WaitForRequest(s2));
   593 	test_Equal(KErrNone,s2.Int());
   594 	test_Equal(KRequestPending,s3.Int());
   595 
   596 	test.Next(_L("Stop server"));
   597 	StopServer();
   598 
   599 	test.End();
   600 
   601 	User::After(WaitABit);	// allow asynchronous cleanup to happen
   602 
   603 	__KHEAP_MARKEND;
   604 	}
   605 
   606 
   607 void TestMessageCompleteWhileCopying()
   608 	{
   609 	__KHEAP_MARK;
   610 
   611 	test.Start(_L("Start server"));
   612 	StartServer();
   613 
   614 	test.Next(_L("Connect"));
   615 	test_Equal(KErrNone,Session.Connect());
   616 
   617 	test.Next(_L("Create large descriptor"));
   618 	HBufC* bigdes = HBufC::NewMax(BigDesLength);
   619 	test_NotNull(bigdes);
   620 	TPtr ptr = bigdes->Des();
   621 
   622 	test.Next(_L("Send message"));
   623 	TRequestStatus s1 = KRequestPending;
   624 	TRequestStatus s2 = KRequestPending;
   625 	TRequestStatus s3;
   626 	Session.Send(RMyServer::ECompleteWhileCopying,TIpcArgs(&s1,&s2,&ptr),s3);
   627 
   628 	test.Next(_L("Wait for s3"));
   629 	test_Equal(KErrNone,WaitForRequest(s3,10*1000000));
   630 	test_Equal(KErrNone,s3.Int());
   631 
   632 	test.Next(_L("Wait for s2"));
   633 	test_Equal(KErrNone,WaitForRequest(s2,10*1000000));
   634 	test_Equal(KErrNone,s2.Int());
   635 
   636 	test.Next(_L("Wait for s1"));
   637 	test_Equal(KErrNone,WaitForRequest(s1,10*1000000));
   638 	test_Equal(KErrNone,s1.Int());
   639 
   640 	test.Next(_L("Close session"));
   641 	Session.Close();
   642 
   643 	test.Next(_L("Stop server"));
   644 	StopServer();
   645 
   646 	test.End();
   647 
   648 	User::After(WaitABit);	// allow asynchronous cleanup to happen
   649 
   650 	__KHEAP_MARKEND;
   651 	}
   652 
   653 
   654 void RunStressThreads(RStressThread& aThread1, RStressThread& aThread2, TInt aTimeout=1000000)
   655 	{
   656 	__KHEAP_MARK;
   657 
   658 	StartServer();
   659 
   660 	NewSession();
   661 
   662 	aThread1.Start();
   663 	aThread2.Start();
   664 
   665 	RTimer timer;
   666 	test_Equal(KErrNone,timer.CreateLocal());
   667 	TRequestStatus timeoutStatus;
   668 	timer.After(timeoutStatus,aTimeout);
   669 	do
   670 		{
   671 		aThread1.Restart();
   672 		aThread2.Restart();
   673 		WaitForRequest();
   674 		}
   675 	while(timeoutStatus==KRequestPending);
   676 	User::WaitForRequest(timeoutStatus);
   677 	CLOSE_AND_WAIT(timer);
   678 
   679 	aThread2.Stop();
   680 	aThread1.Stop();
   681 
   682 	CLOSE_AND_WAIT(Session);
   683 	StopServer();
   684 
   685 	User::After(WaitABit);	// allow asynchronous cleanup to happen
   686 	__KHEAP_MARKEND;
   687 	}
   688 
   689 
   690 GLDEF_C TInt E32Main()
   691 	{
   692 	TInt i;
   693 
   694 	test.Title();
   695 
   696 	test.Start(_L("Initialise"));
   697 	TestInit();
   698 
   699 	for(UseGlobalMessagePool=0; UseGlobalMessagePool<2; ++UseGlobalMessagePool)
   700 		{
   701 		if(UseGlobalMessagePool)
   702 			test.Next(_L("Tests using global message pool"));
   703 		else
   704 			test.Next(_L("Tests using local message pool"));
   705 
   706 		NumMessageSlots = 1;
   707 
   708 		test.Start(_L("Check completing messages on dead session"));
   709 		TestMessageCompleteOnClosedSession();
   710 
   711 		for (i=0; i<10; i++)
   712 			{
   713 			test.Next(_L("Check completing message while IPC copying"));
   714 			TestMessageCompleteWhileCopying();
   715 			}
   716 
   717 		test.Next(_L("Stress closing session whilst in use"));
   718 		RStressThread closer(SessionCloserThread,"SessionCloser",0);
   719 		RStressThread pinger1(SessionPingerThread,"Pinger");
   720 		RunStressThreads(closer, pinger1);
   721 
   722 		NumMessageSlots = 2;
   723 
   724 		test.Next(_L("Stress stopping server whilst in use"));
   725 		RStressThread stopper(ServerStopperThread,"ServerStopper",0);
   726 		RStressThread pinger2(SessionPingerThread,"Pinger");
   727 		RunStressThreads(stopper, pinger2);
   728 
   729 		test.End();
   730 		}
   731 
   732 	test.End();
   733 	return(0);
   734 	}
   735