Update contrib.
1 // Copyright (c) 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 // t_shbuf_perfserver.cpp
21 * Test server used for Performance Testing of shared buffers.
26 #include <e32msgqueue.h>
29 #include "t_shbuf_perfserver.h"
33 * Second phase constructor for sessions. Called by the CServer2 framework
34 * when a session is created (e.g. a connection is made to the server).
36 void CShBufTestServerSession::CreateL()
38 Server().AddSessionL(this);
39 } // CShBufTestServerSession::CreateL()
43 * Destructor for session classes. When this is called it indicates that
44 * a session is closing its connection with the server.
46 CShBufTestServerSession::~CShBufTestServerSession()
48 Server().DropSession(this);
49 } // CShBufTestServerSession::~CShBufTestServerSession()
53 * Handle message requests for this session. Leaving is handled by
54 * CShBufTestServer::RunError() which reports the error code to the client.
56 * @param aMessage RMessage2 reference which encapsulates a client request.
58 void CShBufTestServerSession::ServiceL(const RMessage2& aMessage)
61 switch (aMessage.Function())
63 case EShBufServerShutdownServer:
65 ShutdownServerL(aMessage);
69 case EShBufServerFromTPtr8ProcessAndReturn:
71 FromTPtr8ProcessAndReturnL(aMessage);
75 case EShBufServerFromTPtr8ProcessAndRelease:
77 FromTPtr8ProcessAndReleaseL(aMessage);
81 case EShBufServerOpenRShBufPool:
83 OpenRShBufPoolL(aMessage);
87 case EShBufServerCloseRShBufPool:
89 CloseRShBufPoolL(aMessage);
93 case EShBufServerFromRShBufProcessAndReturn:
95 FromRShBufProcessAndReturnL(aMessage);
99 case EShBufServerFromRShBufProcessAndRelease:
101 FromRShBufProcessAndReleaseL(aMessage);
105 case EShBufServerDbgMarkHeap:
107 DbgMarkHeapL(aMessage);
111 case EShBufServerDbgCheckHeap:
113 DbgCheckHeapL(aMessage);
117 case EShBufServerDbgMarkEnd:
119 DbgMarkEndL(aMessage);
123 case EShBufServerDbgFailNext:
125 DbgFailNextL(aMessage);
131 aMessage.Panic(KRShBufTestServerName, 999);
135 } // CShBufTestServerSession::ServiceL()
139 * Completes a client request. This function provides a single point of
140 * message completion which benefits debugging and maintenance.
142 * @param aMessage The RMessage2 client request.
143 * @param aResult Result of the request.
145 void CShBufTestServerSession::CompleteRequest(const RMessage2& aMessage,
148 if (aMessage.IsNull() == EFalse)
150 aMessage.Complete(aResult);
152 } // CShBufTestServerSession::CompleteRequest()
156 * Takes a buffer from the client, sends to the driver and back to the client.
158 * @param aMessage RMessage2 client request.
160 void CShBufTestServerSession::FromTPtr8ProcessAndReturnL(const RMessage2& aMessage)
163 // Read the client buffer...
165 TPtr8 bufPtr(iSessionTempBuffer, sizeof(iSessionTempBuffer));
167 aMessage.ReadL(0, bufPtr);
171 bufSize = aMessage.Int1();
174 // Pass to the server to pass to the driver and back...
178 result = Server().FromTPtr8ProcessAndReturn(bufPtr, bufSize);
181 // Write the client buffer back...
183 aMessage.WriteL(0, bufPtr);
186 // Complete the request...
188 CompleteRequest(aMessage, result);
189 } // CShBufTestServerSession::FromTPtr8ProcessAndReturnL
193 * Takes a buffer from the client and sends to the driver.
195 * @param aMessage RMessage2 client request.
197 void CShBufTestServerSession::FromTPtr8ProcessAndReleaseL(const RMessage2& aMessage)
200 // Read the client buffer...
202 TPtr8 bufPtr(iSessionTempBuffer, sizeof(iSessionTempBuffer));
204 aMessage.ReadL(0, bufPtr);
207 // Pass to the server to pass to the driver and back...
211 result = Server().FromTPtr8ProcessAndRelease(bufPtr);
214 // Complete the request...
216 CompleteRequest(aMessage, result);
217 } // CShBufTestServerSession::FromTPtr8ProcessAndReleaseL
221 * Allows the client to ask the test server to open a buffer pool.
223 * @param aMessage RMessage2 client request.
225 void CShBufTestServerSession::OpenRShBufPoolL(const RMessage2& aMessage)
228 // Read the handle...
230 TInt poolHandle = aMessage.Int0();
233 // Read the pool info...
235 TShPoolInfo shPoolInfo;
236 TPckg<TShPoolInfo> shPoolInfoPckg(shPoolInfo);
238 aMessage.ReadL(1, shPoolInfoPckg);
241 // Pass to the server to open the pool...
243 TInt result = Server().OpenRShBufPool(poolHandle, shPoolInfo);
246 // Complete the request...
248 CompleteRequest(aMessage, result);
249 } // CShBufTestServerSession::OpenRShBufPoolL
253 * Allows the client to ask the test server to close a buffer pool.
255 * @param aMessage RMessage2 client request.
257 void CShBufTestServerSession::CloseRShBufPoolL(const RMessage2& aMessage)
260 // Pass to the server to close the pool...
262 TInt result = Server().CloseRShBufPool();
265 // Complete the request...
267 CompleteRequest(aMessage, result);
268 } // CShBufTestServerSession::CloseRShBufPoolL
272 * Takes a buffer from the client, sends to the driver and back to the client.
274 * @param aMessage RMessage2 client request.
276 void CShBufTestServerSession::FromRShBufProcessAndReturnL(const RMessage2& aMessage)
279 // Read the client handle buffer...
284 bufSize = aMessage.Int1();
287 // Pass to the server to pass to the driver and back...
291 result = Server().FromRShBufProcessAndReturn(shBuf, bufSize);
294 // Write the client buffer handle back...
296 #ifdef CAN_TRANSFER_SHBUF_TO_ANOTHER_PROCESS
297 // TDBD aMessage.Complete(shbuf->Handle());
299 TPckg<TInt> handlePckg(shBuf.Handle());
300 aMessage.WriteL(0, handlePckg);
304 // Complete the request...
306 CompleteRequest(aMessage, result);
307 } // CShBufTestServerSession::FromRShBufProcessAndReturnL
311 * Takes a buffer from the client and sends to the driver.
313 * @param aMessage RMessage2 client request.
315 void CShBufTestServerSession::FromRShBufProcessAndReleaseL(const RMessage2& aMessage)
318 // Read the client buffer handle...
323 #ifdef CAN_TRANSFER_SHBUF_TO_ANOTHER_PROCESS
324 // TBD RShBuf.Open(aMessage, 0);
326 shBuf.SetReturnedHandle(aMessage.Int0());
330 // Pass to the server to pass to the driver and back...
334 result = Server().FromRShBufProcessAndRelease(shBuf);
337 // Complete the request...
339 CompleteRequest(aMessage, result);
340 } // CShBufTestServerSession::FromRShBufProcessAndReleaseL
344 * Requests the server to mark the start of checking the server's heap.
345 * This function only works in debug releases and is a synchronous request
346 * which will be completed when the procedure returns.
348 * @param aMessage RMessage2 client request.
350 void CShBufTestServerSession::DbgMarkHeapL(const RMessage2& aMessage)
354 result = Server().DbgMarkHeap();
357 // Complete the request...
359 CompleteRequest(aMessage, result);
360 } // CShBufTestServerSession::DbgMarkHeapL()
364 * Requests the server to check that the number of allocated cells at the
365 * current nested level on the server's heap is the same as the specified value.
366 * This function only works for debug builds and is a synchronous request
367 * which will be completed when the procedure returns.
369 * @param aMessage RMessage2 client request.
371 void CShBufTestServerSession::DbgCheckHeapL(const RMessage2& aMessage)
373 TInt count = aMessage.Int0();
376 result = Server().DbgCheckHeap(count);
379 // Complete the request...
381 CompleteRequest(aMessage, result);
382 } // CShBufTestServerSession::DbgCheckHeapL()
386 * Requests the server to mark the end of checking the server's heap. This
387 * function must match an earlier call to DbgMarkHeap() and only functions
388 * on debug releases. This is a synchronous request which will be completed
389 * when the procedure returns.
391 * @param aMessage RMessage2 client request.
393 void CShBufTestServerSession::DbgMarkEndL(const RMessage2& aMessage)
395 TInt count = aMessage.Int0();
398 result = Server().DbgMarkEnd(count);
401 // Complete the request...
403 CompleteRequest(aMessage, result);
404 } // CShBufTestServerSession::DbgMarkEndL()
408 * Simulates heap allocation failure for the sever. The failure occurs on
409 * the next call to new or any of the functions which allocate memory from
410 * the heap. This is defined only for debug builds and is a synchronous
411 * request which will be completed when the procedure returns.
413 * @param aMessage RMessage2 client request.
415 void CShBufTestServerSession::DbgFailNextL(const RMessage2& aMessage)
417 TInt count = aMessage.Int0();
420 result = Server().DbgFailNext(count);
423 // Complete the request...
425 CompleteRequest(aMessage, result);
426 } // CShBufTestServerSession::DbgFailNextL()
430 * Requests the server to shut down when it no longer has any connected
431 * sessions. This procedure is only premitted in debug builds for security
432 * reasons (e.g. to prevent a denial of service attack) and is provided
433 * for testing purposes. This is a synchronous request which will be
434 * completed when the procedure returns. The server will shutdown when the
435 * last session disconnects.
437 * @param aMessage RMessage2 client request.
439 void CShBufTestServerSession::ShutdownServerL(const RMessage2& aMessage)
441 TInt result = Server().ShutdownServer();
444 // Complete the request...
446 CompleteRequest(aMessage, result);
447 } // CShBufTestServerSession::ShutdownServerL()
451 * Static factory method used to create a CShBufTestServer object.
453 * @return Pointer to the created CShBufTestServer object, or NULL.
455 CShBufTestServer* CShBufTestServer::NewL()
457 CShBufTestServer* self = new (ELeave) CShBufTestServer();
458 CleanupStack::PushL(self);
460 CleanupStack::Pop(self);
463 } // CShBufTestServer::NewL
467 * Standard constructor.
469 CShBufTestServer::CShBufTestServer()
470 : CServer2(EPriorityNormal),
471 iShouldShutdownServer(EFalse)
473 __DECLARE_NAME(_L("CShBufTestServer"));
474 } // CShBufTestServer::CShBufTestServer
478 * Second phase constructor. Ensures the server is created and ready to run.
480 void CShBufTestServer::ConstructL()
483 // Open the driver...
487 ret = User::LoadLogicalDevice(_L("D_SHBUF_CLIENT.LDD"));
488 if (ret != KErrAlreadyExists)
490 User::LeaveIfError(ret);
493 ret = User::LoadLogicalDevice(_L("D_SHBUF_OWN.LDD"));
494 if (ret != KErrAlreadyExists)
496 User::LeaveIfError(ret);
499 User::LeaveIfError(iShBufLdd.Open(RShBufTestChannel::EClientThread));
501 StartL(KRShBufTestServerName);
502 } // CShBufTestServer::ConstructL
508 CShBufTestServer::~CShBufTestServer()
510 iSessionArray.Reset();
512 } // CShBufTestServer::~CShBufTestServer
516 * Create a new client session.
518 CSession2* CShBufTestServer::NewSessionL(const TVersion&, const RMessage2& /*aMessage*/) const
520 return new(ELeave) CShBufTestServerSession();
521 } // CShBufTestServer::NewSessionL
525 * Called by the session class when it is being created.
527 * @param aSession Server side session.
529 void CShBufTestServer::AddSessionL(CShBufTestServerSession* aSession)
532 // Store this session in the list of sessions...
534 iSessionArray.Append(aSession);
535 } // CShBufTestServer::AddSessionL
539 * Called by the session class when it is being destroyed.
541 * @param aSession Server side session.
543 void CShBufTestServer::DropSession(CShBufTestServerSession* aSession)
546 // Remove this session from the session array list...
550 position = iSessionArray.Find(aSession);
551 if (position != KErrNotFound)
553 iSessionArray.Remove(position);
557 // If we are shuting down then unconfigure and stop...
559 if (iSessionArray.Count() == 0 && iShouldShutdownServer)
561 CActiveScheduler::Stop();
563 } // CShBufTestServer::DropSession
566 TInt CShBufTestServer::FromTPtr8ProcessAndReturn(TDes8& aBuf, TUint aBufSize)
569 memset(iClearCache, 0xFF, sizeof(iClearCache));
571 return iShBufLdd.FromTPtr8ProcessAndReturn(aBuf, aBufSize);
572 } // CShBufTestServer::FromTPtr8ProcessAndReturn
575 TInt CShBufTestServer::FromTPtr8ProcessAndRelease(TDes8& aBuf)
577 return iShBufLdd.FromTPtr8ProcessAndRelease(aBuf);
578 } // CShBufTestServer::FromTPtr8ProcessAndRelease
581 TInt CShBufTestServer::OpenRShBufPool(TInt aHandle, const TShPoolInfo& aPoolInfo)
583 return iShBufLdd.OpenUserPool(aHandle, aPoolInfo);
584 } // CShBufTestServer::OpenRShBufPool
587 TInt CShBufTestServer::CloseRShBufPool()
589 return iShBufLdd.CloseUserPool();
590 } // CShBufTestServer::CloseRShBufPool
593 TInt CShBufTestServer::FromRShBufProcessAndReturn(RShBuf& aShBuf, TUint aBufSize)
598 memset(iClearCache, 0xFF, sizeof(iClearCache));
600 ret = iShBufLdd.FromRShBufProcessAndReturn(aBufSize);
604 aShBuf.SetReturnedHandle(ret);
609 } // CShBufTestServer::FromRShBufProcessAndReturn
612 TInt CShBufTestServer::FromRShBufProcessAndRelease(RShBuf& aShBuf)
614 return iShBufLdd.FromRShBufProcessAndRelease(aShBuf.Handle());
615 } // CShBufTestServer::FromRShBufProcessAndRelease
619 * Marks the start of checking the server's heap. This function only works
622 * Calls to this function can be nested but each call must be matched by
623 * corresponding DbgMarkEnd().
627 TInt CShBufTestServer::DbgMarkHeap() const
634 } // CShBufTestServer::DbgMarkHeap
638 * Checks that the number of allocated cells at the current nested level on
639 * the server's heap is the same as the specified value. This function only
640 * works for debug builds.
642 * @param aCount The number of heap cells expected to be allocated at
643 * the current nest level.
647 TInt CShBufTestServer::DbgCheckHeap(TInt aCount) const
650 __UHEAP_CHECK(aCount);
656 } // CShBufTestServer::DbgCheckHeap
660 * Marks the end of checking the current server's heap.
662 * The function expects aCount heap cells to remain allocated at the
663 * current nest level. This function must match an earlier call to
664 * DbgMarkHeap() and only functions on debug releases.
666 * @param aCount The number of heap cells expected to remain allocated
667 * at the current nest level.
671 TInt CShBufTestServer::DbgMarkEnd(TInt aCount) const
674 __UHEAP_MARKENDC(aCount);
680 } // CShBufTestServer::DbgMarkEnd
684 * Simulates heap allocation failure for the server.
686 * The failure occurs on the next call to new or any of the functions which
687 * allocate memory from the heap. This is defined only for debug builds.
689 * @param aCount Determines when the allocation will fail.
693 TInt CShBufTestServer::DbgFailNext(TInt aCount) const
702 __UHEAP_FAILNEXT(aCount);
709 } // CShBufTestServer::DbgFailNext
713 * Requests the server to shut down when it no longer has any connected
714 * sessions. This procedure is only premitted in debug builds and is provided
715 * for testing purposes.
717 * The server will shutdown when the last session disconnects.
719 * @return KErrNone if the shutdown request was accepted, otherwise returns
722 TInt CShBufTestServer::ShutdownServer()
724 iShouldShutdownServer = ETrue;
727 } // CShBufTestServer::ShutdownServer
732 * Standard Active Object RunError() method, called when the RunL() method
733 * leaves, which will be when the CShBufTestServerSession::ServiceL() leaves.
735 * Find the current message and complete it before restarting the server.
737 * @param aError Leave code from CShBufTestServerSession::ServiceL().
741 TInt CShBufTestServer::RunError(TInt aError)
744 // Complete the request with the available error code.
746 if (Message().IsNull() == EFalse)
748 Message().Complete(aError);
752 // The leave will result in an early return from CServer::RunL(), skipping
753 // the call to request another message. So do that now in order to keep the
759 } // CShBufTestServer::RunError
763 * Perform all server initialisation, in particular creation of the
764 * scheduler and server and then run the scheduler.
766 static void RunServerL()
769 // Naming the server thread after the server helps to debug panics.
771 User::LeaveIfError(User::RenameThread(KRShBufTestServerName));
774 // Increase priority so that requests are handled promptly...
776 RThread().SetPriority(EPriorityMuchMore);
779 // Create a new Active Scheduler...
781 CActiveScheduler* scheduler = new CActiveScheduler();
782 CleanupStack::PushL(scheduler);
783 CActiveScheduler::Install(scheduler);
786 // Create a new PhoneBookServer...
788 CShBufTestServer* server = CShBufTestServer::NewL();
789 CleanupStack::PushL(server);
792 // Initialisation complete, now signal the client thread...
794 #ifdef CAN_TRANSFER_SHBUF_TO_ANOTHER_PROCESS
795 RProcess::Rendezvous(KErrNone);
797 RThread::Rendezvous(KErrNone);
803 CActiveScheduler::Start();
805 CleanupStack::PopAndDestroy(2, scheduler);
810 * Server process entry-point.
812 * @return KErrNone or a standard Symbian error code.
814 #ifdef CAN_TRANSFER_SHBUF_TO_ANOTHER_PROCESS
817 TInt RShBufTestServerThread(TAny* /*aPtr*/)
822 CTrapCleanup* cleanup = CTrapCleanup::New();
823 TInt ret(KErrNoMemory);
827 TRAP(ret, RunServerL());