os/kernelhwsrv/kerneltest/e32test/benchmark/ipc.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
// Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of the License "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
//
sl@0
    15
sl@0
    16
#include <e32test.h>
sl@0
    17
sl@0
    18
#include "bm_suite.h"
sl@0
    19
sl@0
    20
class RIpcSession : public RSessionBase
sl@0
    21
	{
sl@0
    22
public:
sl@0
    23
	TInt CreateSession(TDesC& aName, TVersion aVer, TInt aSlots)
sl@0
    24
		{
sl@0
    25
		return RSessionBase::CreateSession(aName, aVer, aSlots);
sl@0
    26
		}
sl@0
    27
	void SendReceive(TInt aService, TIpcArgs& args, TRequestStatus& aStatus)
sl@0
    28
		{
sl@0
    29
		RSessionBase::SendReceive(aService, args, aStatus);
sl@0
    30
		}
sl@0
    31
	TInt SendReceive(TInt aService, TIpcArgs& args)
sl@0
    32
		{
sl@0
    33
		return RSessionBase::SendReceive(aService, args);
sl@0
    34
		}
sl@0
    35
	};
sl@0
    36
sl@0
    37
class CIpcScheduler : public CActiveScheduler
sl@0
    38
	{
sl@0
    39
public:
sl@0
    40
	CIpcScheduler()
sl@0
    41
		{
sl@0
    42
		CActiveScheduler::Install(this);
sl@0
    43
		}
sl@0
    44
	};
sl@0
    45
sl@0
    46
class CIpcServer : public CServer2
sl@0
    47
	{
sl@0
    48
public:
sl@0
    49
sl@0
    50
	enum TService 
sl@0
    51
		{
sl@0
    52
		ERunTest,
sl@0
    53
		EGetTimes,
sl@0
    54
		EStop
sl@0
    55
		};
sl@0
    56
sl@0
    57
	struct TServerTimes
sl@0
    58
		{
sl@0
    59
		TBMTicks iNewSessionEntryTime;
sl@0
    60
		TBMTicks iNewSessionLeaveTime;
sl@0
    61
		TBMTicks iServiceLTime;
sl@0
    62
		};
sl@0
    63
sl@0
    64
	CIpcServer() : CServer2(0)
sl@0
    65
		{
sl@0
    66
		}
sl@0
    67
	virtual CSession2* NewSessionL(const TVersion& aVer, const RMessage2&) const;
sl@0
    68
sl@0
    69
	TServerTimes	iTimes;
sl@0
    70
sl@0
    71
	static TInt Entry(TAny* ptr);
sl@0
    72
	};
sl@0
    73
sl@0
    74
class CIpcSession : public CSession2
sl@0
    75
	{
sl@0
    76
public:
sl@0
    77
	CIpcSession(CIpcServer* aServer)
sl@0
    78
		{
sl@0
    79
		iServer = aServer;
sl@0
    80
		}
sl@0
    81
	virtual void ServiceL(const RMessage2& aMessage);
sl@0
    82
sl@0
    83
	CIpcServer*	iServer;
sl@0
    84
	};
sl@0
    85
sl@0
    86
class SpawnArgs : public TBMSpawnArgs
sl@0
    87
	{
sl@0
    88
public:
sl@0
    89
sl@0
    90
	TBuf<16>	iServerName;
sl@0
    91
	TVersion	iVersion;
sl@0
    92
sl@0
    93
	RSemaphore	iSem;
sl@0
    94
	TInt		iIterationCount;
sl@0
    95
sl@0
    96
	SpawnArgs(const TPtrC& aServerName, TInt aPrio, TInt aRemote, TInt aIter);
sl@0
    97
sl@0
    98
	void Close();
sl@0
    99
	void ServerOpen();
sl@0
   100
	void ServerClose();
sl@0
   101
sl@0
   102
	};
sl@0
   103
sl@0
   104
SpawnArgs::SpawnArgs(const TPtrC& aServerName, TInt aPrio, TInt aRemote, TInt aIter) :
sl@0
   105
	TBMSpawnArgs(CIpcServer::Entry, aPrio, aRemote, sizeof(*this)),
sl@0
   106
		iServerName(aServerName),
sl@0
   107
		iVersion(1,0,1), 
sl@0
   108
		iIterationCount(aIter)
sl@0
   109
	{
sl@0
   110
	TInt r;
sl@0
   111
	if (aRemote)
sl@0
   112
		{
sl@0
   113
		r = iSem.CreateGlobal(_L("Semaphore"), 0);
sl@0
   114
		BM_ERROR(r, r == KErrNone);
sl@0
   115
		}
sl@0
   116
	else
sl@0
   117
		{
sl@0
   118
		r = iSem.CreateLocal(0);
sl@0
   119
		BM_ERROR(r, r == KErrNone);
sl@0
   120
		}
sl@0
   121
	}
sl@0
   122
sl@0
   123
void SpawnArgs::ServerOpen()
sl@0
   124
	{
sl@0
   125
	if (iRemote)
sl@0
   126
		{
sl@0
   127
		iSem.Duplicate(iParent);
sl@0
   128
		}
sl@0
   129
	}
sl@0
   130
sl@0
   131
void SpawnArgs::ServerClose()
sl@0
   132
	{
sl@0
   133
	if (iRemote)
sl@0
   134
		{
sl@0
   135
		iSem.Close();
sl@0
   136
		}
sl@0
   137
	}
sl@0
   138
sl@0
   139
void SpawnArgs::Close()
sl@0
   140
	{
sl@0
   141
	iSem.Close();
sl@0
   142
	}
sl@0
   143
sl@0
   144
void CIpcSession::ServiceL(const RMessage2& aMessage)
sl@0
   145
	{
sl@0
   146
	CIpcServer::TService f = (CIpcServer::TService) aMessage.Function();
sl@0
   147
	switch (f) 
sl@0
   148
		{
sl@0
   149
	case CIpcServer::ERunTest:
sl@0
   150
		::bmTimer.Stamp(&iServer->iTimes.iServiceLTime);
sl@0
   151
		break;
sl@0
   152
	case CIpcServer::EGetTimes:
sl@0
   153
		aMessage.WriteL(0, TPtrC8((TUint8*) &iServer->iTimes, sizeof(iServer->iTimes)));
sl@0
   154
		break;
sl@0
   155
	case CIpcServer::EStop:
sl@0
   156
		CActiveScheduler::Stop();
sl@0
   157
		break;
sl@0
   158
	default:
sl@0
   159
		BM_ASSERT(0);
sl@0
   160
		}
sl@0
   161
	aMessage.Complete(KErrNone);
sl@0
   162
	}
sl@0
   163
	
sl@0
   164
CSession2* CIpcServer::NewSessionL(const TVersion&, const RMessage2&) const
sl@0
   165
	{
sl@0
   166
	CIpcServer* srv = (CIpcServer*) this;
sl@0
   167
	::bmTimer.Stamp(&srv->iTimes.iNewSessionEntryTime);
sl@0
   168
	CSession2* s  = new CIpcSession(srv);
sl@0
   169
	BM_ERROR(KErrNoMemory, s);
sl@0
   170
	::bmTimer.Stamp(&srv->iTimes.iNewSessionLeaveTime);
sl@0
   171
	return s;
sl@0
   172
	}
sl@0
   173
sl@0
   174
TInt CIpcServer::Entry(TAny* ptr)
sl@0
   175
	{
sl@0
   176
	SpawnArgs* sa = (SpawnArgs*) ptr;
sl@0
   177
sl@0
   178
	sa->ServerOpen();
sl@0
   179
sl@0
   180
	CIpcScheduler* sched = new CIpcScheduler();
sl@0
   181
	BM_ERROR(KErrNoMemory, sched);
sl@0
   182
sl@0
   183
	CIpcServer* srv = new CIpcServer();
sl@0
   184
	BM_ERROR(KErrNoMemory, srv);
sl@0
   185
	TInt r = srv->Start(sa->iServerName);
sl@0
   186
	BM_ERROR(r, r == KErrNone);
sl@0
   187
	
sl@0
   188
		// signal to the parent the end of the server intialization
sl@0
   189
	sa->iSem.Signal();
sl@0
   190
		
sl@0
   191
	sched->Start();
sl@0
   192
sl@0
   193
	delete srv;
sl@0
   194
	delete sched;
sl@0
   195
sl@0
   196
	sa->ServerClose();
sl@0
   197
sl@0
   198
	return KErrNone;
sl@0
   199
}
sl@0
   200
sl@0
   201
static const TInt KMaxIpcLatencyResults = 5;
sl@0
   202
sl@0
   203
class IpcLatency : public BMProgram
sl@0
   204
	{
sl@0
   205
public :
sl@0
   206
sl@0
   207
	TBool		iRemote;
sl@0
   208
	TInt		iPriority;
sl@0
   209
	TBMResult	iResults[KMaxIpcLatencyResults];
sl@0
   210
sl@0
   211
	IpcLatency(TBool aRemote, TInt aPriority) : 
sl@0
   212
		BMProgram(
sl@0
   213
			aRemote 
sl@0
   214
				? ((aPriority == KBMPriorityHigh) 
sl@0
   215
					? _L("Client-server Framework[Remote High Prioirty Server]")
sl@0
   216
					: _L("Client-server Framework[Remote Low Prioirty Server]"))
sl@0
   217
				: ((aPriority == KBMPriorityHigh) 
sl@0
   218
					? _L("Client-server Framework[Local High Prioirty Server]")
sl@0
   219
					: _L("Client-server Framework[Local Low Prioirty Server]")))
sl@0
   220
		{
sl@0
   221
		iPriority = aPriority;
sl@0
   222
		iRemote = aRemote;
sl@0
   223
		}
sl@0
   224
sl@0
   225
	virtual TBMResult* Run(TBMUInt64 aIter, TInt* aCount);
sl@0
   226
	};
sl@0
   227
sl@0
   228
TBMResult* IpcLatency::Run(TBMUInt64 aIter, TInt* aCount)
sl@0
   229
	{
sl@0
   230
	SpawnArgs sa(_L("BMServer"), iPriority, iRemote, (TInt) aIter);
sl@0
   231
sl@0
   232
		// spawn the server
sl@0
   233
	MBMChild* child = SpawnChild(&sa);
sl@0
   234
sl@0
   235
	TInt n = 0;
sl@0
   236
	iResults[n++].Reset(_L("Connection Request Latency"));
sl@0
   237
	iResults[n++].Reset(_L("Connection Reply Latency"));
sl@0
   238
	iResults[n++].Reset(_L("Request Latency"));
sl@0
   239
	iResults[n++].Reset(_L("Request Response Time"));
sl@0
   240
	iResults[n++].Reset(_L("Reply Latency"));
sl@0
   241
	BM_ASSERT(KMaxIpcLatencyResults >= n);
sl@0
   242
sl@0
   243
		// wait for the srever intialization
sl@0
   244
	sa.iSem.Wait();
sl@0
   245
		// wait 2ms (ie more than one tick) to allow the server to complete its ActiveScheduler intialization ...
sl@0
   246
	User::After(2000);
sl@0
   247
sl@0
   248
	RIpcSession s;
sl@0
   249
sl@0
   250
	for (TBMUInt64 i = 0; i < aIter; ++i)
sl@0
   251
		{	
sl@0
   252
		TBMTicks t1;
sl@0
   253
		::bmTimer.Stamp(&t1);
sl@0
   254
		TInt r = s.CreateSession(sa.iServerName, sa.iVersion, 1);
sl@0
   255
		BM_ERROR(r, r == KErrNone);
sl@0
   256
		TBMTicks t2;
sl@0
   257
		::bmTimer.Stamp(&t2);
sl@0
   258
sl@0
   259
		TRequestStatus st;
sl@0
   260
sl@0
   261
		TBMTicks t3;
sl@0
   262
		::bmTimer.Stamp(&t3);
sl@0
   263
sl@0
   264
			{
sl@0
   265
			TIpcArgs args;
sl@0
   266
			s.SendReceive(CIpcServer::ERunTest, args, st);
sl@0
   267
			}
sl@0
   268
sl@0
   269
		TBMTicks t4;
sl@0
   270
		::bmTimer.Stamp(&t4);
sl@0
   271
sl@0
   272
		User::WaitForRequest(st);
sl@0
   273
		BM_ERROR(r, st == KErrNone);
sl@0
   274
sl@0
   275
		TBMTicks t5;
sl@0
   276
		::bmTimer.Stamp(&t5);
sl@0
   277
sl@0
   278
		CIpcServer::TServerTimes serverTimes;
sl@0
   279
		TPtr8 serverTimesDes((TUint8*) &serverTimes, sizeof(serverTimes), sizeof(serverTimes));
sl@0
   280
sl@0
   281
			{
sl@0
   282
			TIpcArgs args(&serverTimesDes);
sl@0
   283
			r = s.SendReceive(CIpcServer::EGetTimes, args);
sl@0
   284
			BM_ERROR(r, r == KErrNone);
sl@0
   285
			}
sl@0
   286
sl@0
   287
		s.Close();	
sl@0
   288
		
sl@0
   289
		n = 0;
sl@0
   290
		iResults[n++].Cumulate(TBMTicksDelta(t1, serverTimes.iNewSessionEntryTime));
sl@0
   291
		iResults[n++].Cumulate(TBMTicksDelta(serverTimes.iNewSessionLeaveTime, t2));
sl@0
   292
		iResults[n++].Cumulate(TBMTicksDelta(t3, serverTimes.iServiceLTime));
sl@0
   293
		iResults[n++].Cumulate(TBMTicksDelta(t3, t4));
sl@0
   294
		iResults[n++].Cumulate(TBMTicksDelta(serverTimes.iServiceLTime, t5));
sl@0
   295
		BM_ASSERT(KMaxIpcLatencyResults >= n);	
sl@0
   296
sl@0
   297
			// wait 2ms (ie more than one tick) to allow the server to complete.
sl@0
   298
		User::After(2000);
sl@0
   299
		}
sl@0
   300
sl@0
   301
	TInt r = s.CreateSession(sa.iServerName, sa.iVersion, 1);
sl@0
   302
	BM_ERROR(r, r == KErrNone);
sl@0
   303
		{
sl@0
   304
		TIpcArgs args;
sl@0
   305
		s.SendReceive(CIpcServer::EStop, args);
sl@0
   306
		}
sl@0
   307
	s.Close();
sl@0
   308
		
sl@0
   309
	child->WaitChildExit();
sl@0
   310
sl@0
   311
	sa.Close();
sl@0
   312
sl@0
   313
	for (TInt j = 0; j < KMaxIpcLatencyResults; ++j)
sl@0
   314
		{
sl@0
   315
		iResults[j].Update();
sl@0
   316
		}
sl@0
   317
sl@0
   318
	*aCount = KMaxIpcLatencyResults;
sl@0
   319
	return iResults;
sl@0
   320
	}
sl@0
   321
sl@0
   322
IpcLatency test1(EFalse,KBMPriorityHigh);
sl@0
   323
IpcLatency test2(EFalse,KBMPriorityLow );
sl@0
   324
IpcLatency test3(ETrue, KBMPriorityHigh);
sl@0
   325
IpcLatency test4(ETrue, KBMPriorityLow );
sl@0
   326
sl@0
   327
void AddIpc()
sl@0
   328
	{
sl@0
   329
	BMProgram* next = bmSuite;
sl@0
   330
	bmSuite=(BMProgram*)&test4;
sl@0
   331
	bmSuite->Next()=next;
sl@0
   332
	bmSuite=(BMProgram*)&test3;
sl@0
   333
	bmSuite->Next()=&test4;
sl@0
   334
	bmSuite=(BMProgram*)&test2;
sl@0
   335
	bmSuite->Next()=&test3;
sl@0
   336
	bmSuite=(BMProgram*)&test1;
sl@0
   337
	bmSuite->Next()=&test2;
sl@0
   338
	}