Update contrib.
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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // e32test\misc\t_svr3.cpp
18 #define __E32TEST_EXTENSION__
21 #include <e32base_private.h>
25 #include "../misc/prbs.h"
26 #include "../mmu/freeram.h"
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;
35 _LIT(KServerName,"StressSvr");
37 LOCAL_D RTest test(_L("T_SVR3"));
39 class CMySession : public CSession2
43 virtual void ServiceL(const RMessage2& aMessage); //pure virtual fns.
44 void Process(const RMessage2& aMessage);
50 class CMyServer : public CServer2
55 CMyServer(TInt aPriority);
56 static CMyServer* New(TInt aPriority);
57 virtual CSession2* NewSessionL(const TVersion& aVersion, const RMessage2& aMessage) const;//Overloading
60 class CMyActiveScheduler : public CActiveScheduler
63 virtual void Error(TInt anError) const; //Overloading pure virtual function
66 class RStressSvr : public RSessionBase
71 void Test(TRequestStatus& aStatus);
75 class CThread : public CActive
78 CThread(TInt aPriority);
81 virtual void DoCancel();
82 virtual void DisplayStats()=0;
85 virtual TInt StartThread()=0;
89 TInt iServerTerminatedCount;
93 class CServerThread : public CThread
98 virtual TInt StartThread();
99 virtual void DisplayStats();
101 TInt iMessagesReceived;
104 class CClientThread : public CThread
107 static void NewL(TInt anId);
108 CClientThread(TInt anId);
109 virtual TInt StartThread();
110 virtual void DisplayStats();
116 class CRandomTimer : public CActive
120 CRandomTimer(TInt aPriority);
123 virtual void DoCancel();
131 class CStatsTimer : public CActive
135 CStatsTimer(TInt aPriority);
138 virtual void DoCancel();
147 const TInt KNumClients=3;
148 LOCAL_D CServerThread* TheServer;
149 LOCAL_D CClientThread* TheClients[KNumClients];
150 LOCAL_D CRandomTimer* TheRandomTimer;
152 CMySession::CMySession()
157 iSeed[0]=User::TickCount();
160 CMyServer* CMyServer::New(TInt aPriority)
162 // Create a new CMyServer.
166 return new CMyServer(aPriority);
169 CMyServer::CMyServer(TInt aPriority)
173 : CServer2(aPriority)
176 CSession2* CMyServer::NewSessionL(const TVersion& aVersion, const RMessage2&) const
178 // Create a new client for this server.
182 TVersion v(KMajorVersionNumber,KMinorVersionNumber,KBuildVersionNumber);
183 if (!User::QueryVersionSupported(v,aVersion))
184 User::Leave(KErrNotSupported);
185 return new(ELeave) CMySession;
188 void CMySession::ServiceL(const RMessage2& aMessage)
190 // Handle messages for this server.
194 ++TheServer->iMessagesReceived;
195 switch (aMessage.Function())
197 case CMyServer::ETest:
198 if (iOutstanding==KNumMessageSlots-1)
204 aMessage.Complete(KErrNotSupported);
209 void CMySession::Process(const RMessage2& aMessage)
211 TUint x=Random(iSeed)&16383;
213 User::Exit(0); // exit the server
215 aMessage.Terminate(0); // terminate the client
217 aMessage.Complete(KErrNone);
220 void CMyActiveScheduler::Error(TInt anError) const
222 // Called if any Run() method leaves.
226 User::Panic(_L("Server Error"),anError);
229 TInt RStressSvr::Connect()
231 // Connect to the server
235 return CreateSession(KServerName,Version(),KNumMessageSlots);
238 TInt RStressSvr::Test()
240 // Send a message and wait for completion.
244 return SendReceive(CMyServer::ETest);
247 void RStressSvr::Test(TRequestStatus& aStatus)
249 // Send a message asynchronously
253 SendReceive(CMyServer::ETest,aStatus);
256 TVersion RStressSvr::Version()
258 // Return the current version.
262 TVersion v(KMajorVersionNumber,KMinorVersionNumber,KBuildVersionNumber);
266 LOCAL_C TInt ServerThread(TAny*)
269 CMyActiveScheduler* pR=new CMyActiveScheduler;
272 CActiveScheduler::Install(pR);
273 CMyServer* pS=CMyServer::New(0);
276 TInt r=pS->Start(KServerName);
280 CActiveScheduler::Start();
287 LOCAL_C TInt ClientThread(TAny* aPtr)
289 CClientThread* pT=(CClientThread*)aPtr;
292 seed[0]=User::TickCount();
306 TRequestStatus s[KNumMessageSlots];
308 for (i=0; i<KNumMessageSlots-1; i++)
310 TInt n=Random(seed)&16383;
318 CThread::CThread(TInt aPriority)
332 TExitType exitType=iThread.ExitType();
333 TInt exitReason=iThread.ExitReason();
334 TBuf<32> exitCat=iThread.ExitCategory();
336 if (exitType==EExitKill)
338 if (exitReason!=KErrNone && exitReason!=KErrServerTerminated)
341 else if (exitType==EExitPanic)
345 TFullName n(iThread.FullName());
346 test.Printf(_L("Thread %S exited %d,%d,%S\n"),&n,exitType,exitReason,&exitCat);
347 CActiveScheduler::Stop();
351 if (exitType==EExitTerminate)
353 else if (exitReason==KErrNone)
355 else if (exitReason==KErrServerTerminated)
356 ++iServerTerminatedCount;
360 test.Printf(_L("Start thread error %d\n"),r);
361 CActiveScheduler::Stop();
365 void CThread::DoCancel()
367 iThread.LogonCancel(iStatus);
370 TInt CThread::Start()
378 if (r!=KErrAlreadyExists)
384 iThread.Logon(iStatus);
390 CServerThread::CServerThread()
395 TInt CServerThread::StartThread()
399 seed[0]=User::TickCount();
400 TInt heapMin=TInt(Random(seed)&0x0f)+1;
402 TInt r=iThread.Create(KNullDesC(),ServerThread,KStackSize,heapMin,KHeapMaxSize,this); // use unnamed thread
409 void CServerThread::NewL()
411 CServerThread* pT=new (ELeave) CServerThread;
413 CActiveScheduler::Add(pT);
414 User::LeaveIfError(pT->Start());
417 void CServerThread::DisplayStats()
419 test.Printf(_L("Svr : X:%9d ST:%9d T:%9d RX:%9d\n"),iExitCount,iServerTerminatedCount,iTerminateCount,iMessagesReceived);
422 CClientThread::CClientThread(TInt anId)
423 : CThread(0), iId(anId)
427 TInt CClientThread::StartThread()
429 TInt r=iThread.Create(KNullDesC(),ClientThread,KStackSize,NULL,this); // use unnamed thread
436 void CClientThread::NewL(TInt anId)
438 CClientThread* pT=new (ELeave) CClientThread(anId);
440 CActiveScheduler::Add(pT);
441 User::LeaveIfError(pT->Start());
444 void CClientThread::DisplayStats()
446 test.Printf(_L("Cli %1d: X:%9d ST:%9d T:%9d CL:%9d\n"),iId,iExitCount,iServerTerminatedCount,iTerminateCount,iCloses);
449 void CRandomTimer::NewL()
451 CRandomTimer* pR=new (ELeave) CRandomTimer(20);
452 User::LeaveIfError(pR->iTimer.CreateLocal());
453 CActiveScheduler::Add(pR);
458 CRandomTimer::CRandomTimer(TInt aPriority)
461 iSeed[0]=User::TickCount();
464 CRandomTimer::~CRandomTimer()
470 void CRandomTimer::RunL()
473 TUint x=Random(iSeed)&3;
483 void CRandomTimer::Start()
485 TUint x=Random(iSeed)&63;
487 iTimer.HighRes(iStatus, x*1000);
491 void CRandomTimer::DoCancel()
496 void CStatsTimer::NewL()
498 CStatsTimer* pT=new (ELeave) CStatsTimer(-10);
499 User::LeaveIfError(pT->iTimer.CreateLocal());
500 CActiveScheduler::Add(pT);
504 CStatsTimer::CStatsTimer(TInt aPriority)
507 iInitFreeRam = FreeRam();
510 CStatsTimer::~CStatsTimer()
516 void CStatsTimer::RunL()
518 TheServer->DisplayStats();
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;
529 test.Printf(_L("Max RAM delta %dK Free RAM %08x\n"), iMaxDelta/1024, free_ram);
535 void CStatsTimer::Start()
537 iTimer.After(iStatus, 1000000);
541 void CStatsTimer::DoCancel()
548 CActiveScheduler* pA=new (ELeave) CActiveScheduler;
549 CActiveScheduler::Install(pA);
550 CServerThread::NewL();
552 for (id=0; id<KNumClients; id++)
553 CClientThread::NewL(id);
554 CRandomTimer::NewL();
558 GLDEF_C TInt E32Main()
566 RThread().SetPriority(EPriorityMore);
568 TRAPD(r,InitialiseL());
571 CActiveScheduler::Start();
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)
584 if (!aInfo.iAllocator && aInfo.iHeapInitialSize>0)
588 r = CreateThreadHeap(aInfo, pH, 0, aNotFirst);
590 else if (aInfo.iAllocator)
593 RAllocator* pA = aInfo.iAllocator;
595 User::SwitchAllocator(pA);