1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/kernelhwsrv/kerneltest/e32test/benchmark/ipc.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,338 @@
1.4 +// Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of the License "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +//
1.18 +
1.19 +#include <e32test.h>
1.20 +
1.21 +#include "bm_suite.h"
1.22 +
1.23 +class RIpcSession : public RSessionBase
1.24 + {
1.25 +public:
1.26 + TInt CreateSession(TDesC& aName, TVersion aVer, TInt aSlots)
1.27 + {
1.28 + return RSessionBase::CreateSession(aName, aVer, aSlots);
1.29 + }
1.30 + void SendReceive(TInt aService, TIpcArgs& args, TRequestStatus& aStatus)
1.31 + {
1.32 + RSessionBase::SendReceive(aService, args, aStatus);
1.33 + }
1.34 + TInt SendReceive(TInt aService, TIpcArgs& args)
1.35 + {
1.36 + return RSessionBase::SendReceive(aService, args);
1.37 + }
1.38 + };
1.39 +
1.40 +class CIpcScheduler : public CActiveScheduler
1.41 + {
1.42 +public:
1.43 + CIpcScheduler()
1.44 + {
1.45 + CActiveScheduler::Install(this);
1.46 + }
1.47 + };
1.48 +
1.49 +class CIpcServer : public CServer2
1.50 + {
1.51 +public:
1.52 +
1.53 + enum TService
1.54 + {
1.55 + ERunTest,
1.56 + EGetTimes,
1.57 + EStop
1.58 + };
1.59 +
1.60 + struct TServerTimes
1.61 + {
1.62 + TBMTicks iNewSessionEntryTime;
1.63 + TBMTicks iNewSessionLeaveTime;
1.64 + TBMTicks iServiceLTime;
1.65 + };
1.66 +
1.67 + CIpcServer() : CServer2(0)
1.68 + {
1.69 + }
1.70 + virtual CSession2* NewSessionL(const TVersion& aVer, const RMessage2&) const;
1.71 +
1.72 + TServerTimes iTimes;
1.73 +
1.74 + static TInt Entry(TAny* ptr);
1.75 + };
1.76 +
1.77 +class CIpcSession : public CSession2
1.78 + {
1.79 +public:
1.80 + CIpcSession(CIpcServer* aServer)
1.81 + {
1.82 + iServer = aServer;
1.83 + }
1.84 + virtual void ServiceL(const RMessage2& aMessage);
1.85 +
1.86 + CIpcServer* iServer;
1.87 + };
1.88 +
1.89 +class SpawnArgs : public TBMSpawnArgs
1.90 + {
1.91 +public:
1.92 +
1.93 + TBuf<16> iServerName;
1.94 + TVersion iVersion;
1.95 +
1.96 + RSemaphore iSem;
1.97 + TInt iIterationCount;
1.98 +
1.99 + SpawnArgs(const TPtrC& aServerName, TInt aPrio, TInt aRemote, TInt aIter);
1.100 +
1.101 + void Close();
1.102 + void ServerOpen();
1.103 + void ServerClose();
1.104 +
1.105 + };
1.106 +
1.107 +SpawnArgs::SpawnArgs(const TPtrC& aServerName, TInt aPrio, TInt aRemote, TInt aIter) :
1.108 + TBMSpawnArgs(CIpcServer::Entry, aPrio, aRemote, sizeof(*this)),
1.109 + iServerName(aServerName),
1.110 + iVersion(1,0,1),
1.111 + iIterationCount(aIter)
1.112 + {
1.113 + TInt r;
1.114 + if (aRemote)
1.115 + {
1.116 + r = iSem.CreateGlobal(_L("Semaphore"), 0);
1.117 + BM_ERROR(r, r == KErrNone);
1.118 + }
1.119 + else
1.120 + {
1.121 + r = iSem.CreateLocal(0);
1.122 + BM_ERROR(r, r == KErrNone);
1.123 + }
1.124 + }
1.125 +
1.126 +void SpawnArgs::ServerOpen()
1.127 + {
1.128 + if (iRemote)
1.129 + {
1.130 + iSem.Duplicate(iParent);
1.131 + }
1.132 + }
1.133 +
1.134 +void SpawnArgs::ServerClose()
1.135 + {
1.136 + if (iRemote)
1.137 + {
1.138 + iSem.Close();
1.139 + }
1.140 + }
1.141 +
1.142 +void SpawnArgs::Close()
1.143 + {
1.144 + iSem.Close();
1.145 + }
1.146 +
1.147 +void CIpcSession::ServiceL(const RMessage2& aMessage)
1.148 + {
1.149 + CIpcServer::TService f = (CIpcServer::TService) aMessage.Function();
1.150 + switch (f)
1.151 + {
1.152 + case CIpcServer::ERunTest:
1.153 + ::bmTimer.Stamp(&iServer->iTimes.iServiceLTime);
1.154 + break;
1.155 + case CIpcServer::EGetTimes:
1.156 + aMessage.WriteL(0, TPtrC8((TUint8*) &iServer->iTimes, sizeof(iServer->iTimes)));
1.157 + break;
1.158 + case CIpcServer::EStop:
1.159 + CActiveScheduler::Stop();
1.160 + break;
1.161 + default:
1.162 + BM_ASSERT(0);
1.163 + }
1.164 + aMessage.Complete(KErrNone);
1.165 + }
1.166 +
1.167 +CSession2* CIpcServer::NewSessionL(const TVersion&, const RMessage2&) const
1.168 + {
1.169 + CIpcServer* srv = (CIpcServer*) this;
1.170 + ::bmTimer.Stamp(&srv->iTimes.iNewSessionEntryTime);
1.171 + CSession2* s = new CIpcSession(srv);
1.172 + BM_ERROR(KErrNoMemory, s);
1.173 + ::bmTimer.Stamp(&srv->iTimes.iNewSessionLeaveTime);
1.174 + return s;
1.175 + }
1.176 +
1.177 +TInt CIpcServer::Entry(TAny* ptr)
1.178 + {
1.179 + SpawnArgs* sa = (SpawnArgs*) ptr;
1.180 +
1.181 + sa->ServerOpen();
1.182 +
1.183 + CIpcScheduler* sched = new CIpcScheduler();
1.184 + BM_ERROR(KErrNoMemory, sched);
1.185 +
1.186 + CIpcServer* srv = new CIpcServer();
1.187 + BM_ERROR(KErrNoMemory, srv);
1.188 + TInt r = srv->Start(sa->iServerName);
1.189 + BM_ERROR(r, r == KErrNone);
1.190 +
1.191 + // signal to the parent the end of the server intialization
1.192 + sa->iSem.Signal();
1.193 +
1.194 + sched->Start();
1.195 +
1.196 + delete srv;
1.197 + delete sched;
1.198 +
1.199 + sa->ServerClose();
1.200 +
1.201 + return KErrNone;
1.202 +}
1.203 +
1.204 +static const TInt KMaxIpcLatencyResults = 5;
1.205 +
1.206 +class IpcLatency : public BMProgram
1.207 + {
1.208 +public :
1.209 +
1.210 + TBool iRemote;
1.211 + TInt iPriority;
1.212 + TBMResult iResults[KMaxIpcLatencyResults];
1.213 +
1.214 + IpcLatency(TBool aRemote, TInt aPriority) :
1.215 + BMProgram(
1.216 + aRemote
1.217 + ? ((aPriority == KBMPriorityHigh)
1.218 + ? _L("Client-server Framework[Remote High Prioirty Server]")
1.219 + : _L("Client-server Framework[Remote Low Prioirty Server]"))
1.220 + : ((aPriority == KBMPriorityHigh)
1.221 + ? _L("Client-server Framework[Local High Prioirty Server]")
1.222 + : _L("Client-server Framework[Local Low Prioirty Server]")))
1.223 + {
1.224 + iPriority = aPriority;
1.225 + iRemote = aRemote;
1.226 + }
1.227 +
1.228 + virtual TBMResult* Run(TBMUInt64 aIter, TInt* aCount);
1.229 + };
1.230 +
1.231 +TBMResult* IpcLatency::Run(TBMUInt64 aIter, TInt* aCount)
1.232 + {
1.233 + SpawnArgs sa(_L("BMServer"), iPriority, iRemote, (TInt) aIter);
1.234 +
1.235 + // spawn the server
1.236 + MBMChild* child = SpawnChild(&sa);
1.237 +
1.238 + TInt n = 0;
1.239 + iResults[n++].Reset(_L("Connection Request Latency"));
1.240 + iResults[n++].Reset(_L("Connection Reply Latency"));
1.241 + iResults[n++].Reset(_L("Request Latency"));
1.242 + iResults[n++].Reset(_L("Request Response Time"));
1.243 + iResults[n++].Reset(_L("Reply Latency"));
1.244 + BM_ASSERT(KMaxIpcLatencyResults >= n);
1.245 +
1.246 + // wait for the srever intialization
1.247 + sa.iSem.Wait();
1.248 + // wait 2ms (ie more than one tick) to allow the server to complete its ActiveScheduler intialization ...
1.249 + User::After(2000);
1.250 +
1.251 + RIpcSession s;
1.252 +
1.253 + for (TBMUInt64 i = 0; i < aIter; ++i)
1.254 + {
1.255 + TBMTicks t1;
1.256 + ::bmTimer.Stamp(&t1);
1.257 + TInt r = s.CreateSession(sa.iServerName, sa.iVersion, 1);
1.258 + BM_ERROR(r, r == KErrNone);
1.259 + TBMTicks t2;
1.260 + ::bmTimer.Stamp(&t2);
1.261 +
1.262 + TRequestStatus st;
1.263 +
1.264 + TBMTicks t3;
1.265 + ::bmTimer.Stamp(&t3);
1.266 +
1.267 + {
1.268 + TIpcArgs args;
1.269 + s.SendReceive(CIpcServer::ERunTest, args, st);
1.270 + }
1.271 +
1.272 + TBMTicks t4;
1.273 + ::bmTimer.Stamp(&t4);
1.274 +
1.275 + User::WaitForRequest(st);
1.276 + BM_ERROR(r, st == KErrNone);
1.277 +
1.278 + TBMTicks t5;
1.279 + ::bmTimer.Stamp(&t5);
1.280 +
1.281 + CIpcServer::TServerTimes serverTimes;
1.282 + TPtr8 serverTimesDes((TUint8*) &serverTimes, sizeof(serverTimes), sizeof(serverTimes));
1.283 +
1.284 + {
1.285 + TIpcArgs args(&serverTimesDes);
1.286 + r = s.SendReceive(CIpcServer::EGetTimes, args);
1.287 + BM_ERROR(r, r == KErrNone);
1.288 + }
1.289 +
1.290 + s.Close();
1.291 +
1.292 + n = 0;
1.293 + iResults[n++].Cumulate(TBMTicksDelta(t1, serverTimes.iNewSessionEntryTime));
1.294 + iResults[n++].Cumulate(TBMTicksDelta(serverTimes.iNewSessionLeaveTime, t2));
1.295 + iResults[n++].Cumulate(TBMTicksDelta(t3, serverTimes.iServiceLTime));
1.296 + iResults[n++].Cumulate(TBMTicksDelta(t3, t4));
1.297 + iResults[n++].Cumulate(TBMTicksDelta(serverTimes.iServiceLTime, t5));
1.298 + BM_ASSERT(KMaxIpcLatencyResults >= n);
1.299 +
1.300 + // wait 2ms (ie more than one tick) to allow the server to complete.
1.301 + User::After(2000);
1.302 + }
1.303 +
1.304 + TInt r = s.CreateSession(sa.iServerName, sa.iVersion, 1);
1.305 + BM_ERROR(r, r == KErrNone);
1.306 + {
1.307 + TIpcArgs args;
1.308 + s.SendReceive(CIpcServer::EStop, args);
1.309 + }
1.310 + s.Close();
1.311 +
1.312 + child->WaitChildExit();
1.313 +
1.314 + sa.Close();
1.315 +
1.316 + for (TInt j = 0; j < KMaxIpcLatencyResults; ++j)
1.317 + {
1.318 + iResults[j].Update();
1.319 + }
1.320 +
1.321 + *aCount = KMaxIpcLatencyResults;
1.322 + return iResults;
1.323 + }
1.324 +
1.325 +IpcLatency test1(EFalse,KBMPriorityHigh);
1.326 +IpcLatency test2(EFalse,KBMPriorityLow );
1.327 +IpcLatency test3(ETrue, KBMPriorityHigh);
1.328 +IpcLatency test4(ETrue, KBMPriorityLow );
1.329 +
1.330 +void AddIpc()
1.331 + {
1.332 + BMProgram* next = bmSuite;
1.333 + bmSuite=(BMProgram*)&test4;
1.334 + bmSuite->Next()=next;
1.335 + bmSuite=(BMProgram*)&test3;
1.336 + bmSuite->Next()=&test4;
1.337 + bmSuite=(BMProgram*)&test2;
1.338 + bmSuite->Next()=&test3;
1.339 + bmSuite=(BMProgram*)&test1;
1.340 + bmSuite->Next()=&test2;
1.341 + }