First public contribution.
1 // Copyright (c) 2005-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 // A helper test driver for testing Kernel Performance Logger implementation.
23 #include "d_perflogger_ldd.h"
24 #include <kernperflogger.h>
26 _LIT(KDFCThreadName,"D_PL_DFC_THREAD");
27 const TInt KDFCThreadPriority=27;
29 //-----------------------------------------------------------------------------------
32 DKPLoggerTestHelperLDD* DKPLoggerTestHelperLDD::pSelf = NULL; //-- static pointer to the single instance of the logical channel class
34 //-----------------------------------------------------------------------------------
36 DLogTicker::DLogTicker() : iTimer(LogTimerCallback,this) //-- initialize log events generator
40 iUserThreadContext = NULL;
43 DLogTicker::~DLogTicker()
45 Cancel(); //-- cancel user request, DFC, timers etc.
46 Kern::DestroyClientRequest(iRequest);
50 //-----------------------------------------------------------------------------------
53 Construct ticker object. Creates appropriate TDfc object for dealing with IDFC or DFC
55 @param apUserThreadContext pointer to the user thread context where the request will be completed in
56 @param apDfcQ pointer to the DFC queue this object will be using.
57 @param aLogContext specfies the context (ISR, DFC or IDFC) the logging will be made from. Can be NKern::EIDFC, NKern::EThread, NKern::EInterrupt
59 void DLogTicker::Construct(DThread* aUserThreadContext, TDfcQue* aDfcQ, NKern::TContext aLogContext)
61 __NK_ASSERT_DEBUG(aUserThreadContext && aDfcQ);
63 iLogContext = aLogContext;
64 if(aLogContext == NKern::EIDFC)
65 {//-- we will deal with IDFC, create appropriate DFC object
66 iLogDFC = new TDfc(LogDFC, this);
70 if(aLogContext == NKern::EThread || aLogContext == NKern::EInterrupt)
71 {//-- we will deal with DFC or ISR
72 iLogDFC = new TDfc(LogDFC, this, aDfcQ, 0);
76 __PRINT("#KPLogTest:DLogTicker::Construct() wrong context request !");
81 __NK_ASSERT_ALWAYS(iLogDFC);
83 TInt r = Kern::CreateClientRequest(iRequest);
84 __NK_ASSERT_ALWAYS(r == KErrNone);
86 iUserThreadContext = aUserThreadContext; //-- store user thread context co complete requests correctly
87 // iLogDFC->SetDfcQ(aDfcQ); //-- attach to the given DFC queue. !!!!DON'T DO THIS FOR IDFC!!!!!
90 //-----------------------------------------------------------------------------------
93 Start the state machine by scheduling a DFC (or IDFC)
95 @param apLogControl log parameters structure
96 @param apRqStat pointer to the user request staus object that will be completed when all loggings done.
98 void DLogTicker::Start(const TTestLogCtrl* aLogControl, TRequestStatus* aRqStat)
100 __NK_ASSERT_DEBUG(aLogControl && aRqStat && iLogDFC);
101 kumemget32(&iLogControl, aLogControl, sizeof(TTestLogCtrl)); //-- copy control structure from the user side
103 __NK_ASSERT_DEBUG(iLogControl.iLogsNum>=0);
104 __PRINT1("#KPLogTest:DLogTicker::Start() for %d loggings",iLogControl.iLogsNum);
106 if (iRequest->SetStatus(aRqStat) != KErrNone)
107 {//-- current request is pending, panic client thread
108 __PRINT("#KPLogTest:DLogTicker::Start() request is already pending !");
109 Kern::PanicCurrentThread(KPLoggerHelperTestDrv, EReqAlreadyPending);
112 if(iLogContext == NKern::EIDFC)
113 {//-- DLogTicker::LogDFC() will be called as IDFC
115 iLogDFC->Add(); //-- start
119 {//-- DLogTicker::LogDFC() will be called as DFC
120 iLogDFC->Enque(); //-- start
125 //-----------------------------------------------------------------------------------
128 Complete the user request when all logging done.
129 This can be called from 2 concurrent places - DFC & NTimer callback, either of them can complete the request
131 @param aCompletionCode request completion code
133 void DLogTicker::CompleteRequest(TInt aCompletionCode/*=KErrNone*/)
135 Kern::QueueRequestComplete(iUserThreadContext, iRequest, aCompletionCode);
138 //-----------------------------------------------------------------------------------
143 void DLogTicker::Cancel(void)
145 CompleteRequest(KErrCancel); //-- cancel user request
146 iLogControl.iLogsNum = 0; // Prevent DFC from restarting the timer
147 iTimer.Cancel(); //-- cancel Timer
148 iLogDFC->Cancel(); //-- cancel DFC
151 //-----------------------------------------------------------------------------------
154 Ticker timer callback. Can be called in ISR or DFC context.
155 If called in ISR context, makes logging, and schedules a DFC to complete the request if needed.
156 If called in DFC context, makes logging for DFC (not for IDFC) mode and completes the request if needed.
158 @param apSelf pointer to the DLogTicker object
160 void DLogTicker::LogTimerCallback(TAny* aSelf)
162 DLogTicker *pSelf = (DLogTicker*)aSelf;
163 __NK_ASSERT_DEBUG(pSelf);
165 TTestLogCtrl& logClontrol = pSelf->iLogControl;
166 __NK_ASSERT_DEBUG(logClontrol.iLogsNum >=0);
168 TInt context = NKern::CurrentContext();
169 if(context == NKern::EInterrupt)
170 {//-- This callback is from ISR
172 //-- make logging from ISR context, category field is ignored, it will be FastTrace::EKernPerfLog
173 PERF_LOG(logClontrol.iSubCategory, logClontrol.iUserData, logClontrol.iUserData2);
175 //-- kick DFC, it will probaly complete the request.
176 pSelf->iLogDFC->Add();
179 {//-- this is a DFC callback, but the DFC object could also have been ceated as IDFC
180 //-- complete user request here if necessarily.
181 if(pSelf->iLogDFC->IsIDFC())
182 {//-- the logging will be made in IDFC function.
183 if(pSelf->iLogControl.iLogsNum == 0)
184 {//-- all done, complete the request here, because we can't do in in IDFC
185 pSelf->CompleteRequest();
188 {//-- this callback came from IDFC object, kick IDFC in a special way.
190 pSelf->iLogDFC->Add();
195 {//-- this callback came from IDFC object, make logging from DFC context
196 //-- category field is ignored, it will be FastTrace::EKernPerfLog
197 PERF_LOG(logClontrol.iSubCategory, logClontrol.iUserData, logClontrol.iUserData2);
199 pSelf->iLogDFC->Enque(); //-- kick DFC
206 //-----------------------------------------------------------------------------------
209 Ticker DFC or IDFC function. Kicks the timer;
210 If is called as IDFC, makes logging in this context and schedules a timer callback in DFC context to complete the request.
211 If is called as DFC, schedules timer callback in DFC or ISR context.
213 @param apSelf pointer to the DLogTicker object
215 void DLogTicker::LogDFC(TAny* aSelf)
217 DLogTicker *pSelf = (DLogTicker*)aSelf;
218 __NK_ASSERT_DEBUG(pSelf);
220 TInt context = NKern::CurrentContext();
221 (void)context; //-- avoid warning in release mode
223 if(pSelf->iLogControl.iLogsNum <= 0)
224 {//-- complete user request, all done. The request can also be completed in LogTimerCallback()
225 //-- in case if this is a IDFC and callback is a DFC
226 pSelf->CompleteRequest();
230 TTestLogCtrl& logClontrol = pSelf->iLogControl;
231 logClontrol.iLogsNum--; //-- decrease remaining number of loggings
233 if(pSelf->iLogDFC->IsIDFC())
234 {//-- we are in IDFC context, make logging from here, timer callback won't be IDFC
235 __NK_ASSERT_DEBUG(context == NKern::EIDFC);
237 //-- category field is ignored, it will be FastTrace::EKernPerfLog
238 PERF_LOG(logClontrol.iSubCategory, logClontrol.iUserData, logClontrol.iUserData2);
240 //-- kick the timer to have a callback in a specified time in DFC context
241 //-- timer's DFC will complete the request if necessarily.
242 pSelf->iTimer.OneShot(logClontrol.iLogPeriodTick, ETrue);
245 {//-- we are in DFC context, kick the timer to have a callback in a specified time in ISR or DFC context
246 pSelf->iTimer.OneShot(logClontrol.iLogPeriodTick, !(pSelf->iLogContext == NKern::EInterrupt));
252 //-----------------------------------------------------------------------------------
255 //###################################################################################
256 //# DKPLoggerTestHelperLDD class implementation
257 //###################################################################################
259 DKPLoggerTestHelperLDD::DKPLoggerTestHelperLDD()
262 //-- store the pointer to the current thread for request completion from ISR->DFC
263 iClientThread = &Kern::CurrentThread();
265 __NK_ASSERT_DEBUG(iClientThread);
267 //-- Open client's user thread, incrementing ref. counter
268 TInt nRes = iClientThread->Open();
269 __NK_ASSERT_DEBUG(nRes == KErrNone);
270 (void)nRes;//-- avoid warning in release mode
273 //-- initialize DFC machinery
274 //iDfcQ = Kern::DfcQue0(); //-- attach to the low priority DFC queue
277 TInt r = Kern::DynamicDfcQCreate(iDfcQ, KDFCThreadPriority, KDFCThreadName);
283 #ifdef CPU_AFFINITY_ANY
284 NKern::ThreadSetCpuAffinity((NThread*)(iDfcQ->iThread), KCpuAffinityAny);
288 iIsrLogTicker.Construct (iClientThread, iDfcQ, NKern::EInterrupt);//-- construct ISR log ticker
289 iDfcLogTicker.Construct (iClientThread, iDfcQ, NKern::EThread); //-- construct DFC log ticker
290 iIDfcLogTicker.Construct(iClientThread, iDfcQ, NKern::EIDFC); //-- construct IDFC log ticker
293 //-----------------------------------------------------------------------------------
295 DKPLoggerTestHelperLDD::~DKPLoggerTestHelperLDD()
297 __PRINT("#KPLogTest:~DKPLoggerTestHelperLDD()");
299 iClientThread->Close(NULL);
304 pSelf = NULL; //-- clear the pointer to this class instance
307 //-----------------------------------------------------------------------------------
310 static factory function for the LDD.
312 @return pointer to the created (or existing) instance of the class
314 DKPLoggerTestHelperLDD* DKPLoggerTestHelperLDD::CreateInstance()
316 __PRINT("#KPLogTest:DKPLoggerTestHelperLDD::CreateInstance()");
318 //-- create LDD channel instance
320 {//-- this is a singleton, can't have more than one instance
321 __PRINT("#DKPLoggerTestHelperLDD::CreateInstance(): Attempt to create a second instance of a singleton!");
325 pSelf = new DKPLoggerTestHelperLDD;
329 __PRINT("#KPLogTest:DKPLoggerTestHelperLDD::CreateInstance(): Unable to create class instance !");
335 //-----------------------------------------------------------------------------------
338 LDD second stage constructor
340 TInt DKPLoggerTestHelperLDD::DoCreate(TInt /*aUnit*/, const TDesC8* /*anInfo*/, const TVersion& aVer)
342 //-- check if the version aVer is supported
343 if (!Kern::QueryVersionSupported(TVersion(KMajorVersionNumber,KMinorVersionNumber,KBuildVersionNumber),aVer))
344 return KErrNotSupported;
349 //-----------------------------------------------------------------------------------
352 Requests processing function.
353 @return request processing error code.
355 TInt DKPLoggerTestHelperLDD::Request(TInt aFunction, TAny* a1, TAny* a2)
357 TInt nRes = KErrNone;
359 if (aFunction == KMaxTInt)
360 {//-- this is DoCancel()
362 TUint reqMask = (TUint)a1;
368 {//-- This is DoRequest()
370 //-- extract request parameters
371 TRequestStatus* pRqStat=(TRequestStatus*)a1;
374 kumemget32(params, a2, sizeof(params));
376 nRes = DoRequest(~aFunction, pRqStat, params[0], params[1]);
379 {//-- This is DoControl()
380 nRes = DoControl(aFunction, a1, a2);
387 //-----------------------------------------------------------------------------------
390 Cancel outstanding request(s)
391 @param aReqNumber request number to cancel
393 void DKPLoggerTestHelperLDD::DoCancel(TUint aReqNumber)
398 //-- cancel logging from ISR.
399 case RKPLoggerTestHelper::EDoLogFromISR:
400 iIsrLogTicker.Cancel();
403 //-- cancel logging from IDFC.
404 case RKPLoggerTestHelper::EDoLogFromIDFC:
405 iIDfcLogTicker.Cancel();
408 //-- cancel logging from DFC.
409 case RKPLoggerTestHelper::EDoLogFromDFC:
410 iDfcLogTicker.Cancel();
414 __PRINT1("#KPLogTest:DKPLoggerTestHelperLDD::DoCancel Cancelling a wrong request number:%d!", aReqMask);
415 Kern::PanicCurrentThread(KPLoggerHelperTestDrv, EWrongRequest);
421 //-----------------------------------------------------------------------------------
424 Asynchronous request processing.
426 @param aFunction request function number, see RKPLoggerTestHelper::TControl
428 @param apRqStat pointer to the user's request status object.
429 @param apArg1 pointer to the 1st parameter in RKPLoggerTestHelper::DoRequest
430 @param apArg2 pointer to the 2nd parameter in RKPLoggerTestHelper::DoRequest
432 @return request scheduling result, system-wide error code.
435 TInt DKPLoggerTestHelperLDD::DoRequest(TInt aReqNumber, TRequestStatus* aRqStat, TAny* aArg1, TAny* /*apArg2*/)
439 //-- make logging from ISR.
440 case RKPLoggerTestHelper::EDoLogFromISR:
442 __PRINT("#KPLogTest: making loggings from ISR context");
443 iIsrLogTicker.Start((const TTestLogCtrl*)aArg1, aRqStat);
447 //-- make logging from IDFC.
448 case RKPLoggerTestHelper::EDoLogFromIDFC:
450 __PRINT("#KPLogTest: making loggings from IDFC context");
451 iIDfcLogTicker.Start((const TTestLogCtrl*)aArg1, aRqStat);
455 //-- make logging from DFC.
456 case RKPLoggerTestHelper::EDoLogFromDFC:
458 __PRINT("#KPLogTest: making loggings from DFC context");
459 iDfcLogTicker.Start((const TTestLogCtrl*)aArg1, aRqStat);
465 __PRINT1("#KPLogTest:DKPLoggerTestHelperLDD::DoRequest() Wrong request number:%d!", aReqNumber);
466 Kern::PanicCurrentThread(KPLoggerHelperTestDrv, EWrongRequest);
473 //-----------------------------------------------------------------------------------
476 Synchronous requests processing.
478 @param aFunction request function number, see RKernPerfLogger::TControl
479 @param apArg1 pointer to the 1st parameter in RKernPerfLogger::DoControl
480 @param apArg2 pointer to the 2n parameter in RKernPerfLogger::DoControl
482 @return request processing result
484 TInt DKPLoggerTestHelperLDD::DoControl(TInt aFunction, TAny* aArg1, TAny* /*apArg2*/)
489 //-- make test logging from the user thread
490 case RKPLoggerTestHelper::EDoLogFromUserThread:
492 kumemget32(&iLogControlUserThread, aArg1, sizeof(iLogControlUserThread)); //-- copy control structure from the user side
493 __PRINT1("#KPLogTest: making %d loggings from a user-thread context", iLogControlUserThread.iLogsNum);
494 __NK_ASSERT_DEBUG(iLogControlUserThread.iLogsNum >=0 );
496 //-- This context is actually, a user thread. Make logging from here
497 for(TInt i=0; i<iLogControlUserThread.iLogsNum; ++i)
499 //-- category field is ignored, it will be FastTrace::EKernPerfLog
500 PERF_LOG(iLogControlUserThread.iSubCategory, iLogControlUserThread.iUserData, iLogControlUserThread.iUserData2);
502 NKern::Sleep(iLogControlUserThread.iLogPeriodTick);
507 //-- unit test for different PERF_LOG macros
508 case RKPLoggerTestHelper::EDoTestMacros:
510 kumemget32(&iLogControlUserThread, aArg1, sizeof(iLogControlUserThread)); //-- copy control structure from the user side
511 __PRINT1("#KPLogTest: making %d loggings from a user-thread context, testing different macros", iLogControlUserThread.iLogsNum);
512 __NK_ASSERT_DEBUG(iLogControlUserThread.iLogsNum >=0 );
514 for(TInt i=0; i<iLogControlUserThread.iLogsNum; ++i)
516 PERF_LOG0(iLogControlUserThread.iSubCategory);
517 PERF_LOG1(iLogControlUserThread.iSubCategory, iLogControlUserThread.iUserData);
518 PERF_LOG (iLogControlUserThread.iSubCategory, iLogControlUserThread.iUserData, iLogControlUserThread.iUserData2);
520 NKern::Sleep(iLogControlUserThread.iLogPeriodTick);
528 __PRINT1("#KPLogTest:DKPLoggerTestHelperLDD::DoControl() Wrong function number:%d!", aFunction);
529 Kern::PanicCurrentThread(KPLoggerHelperTestDrv, EWrongRequest);
540 //###################################################################################
541 //# LDD factory, DKPLoggerTestHelperLDDFactory class implementation
542 //###################################################################################
544 DKPLoggerTestHelperLDDFactory::DKPLoggerTestHelperLDDFactory()
546 iUnitsMask = 0x00; //-- don't support units
547 iVersion=TVersion(KMajorVersionNumber,KMinorVersionNumber,KBuildVersionNumber);
550 DKPLoggerTestHelperLDDFactory::~DKPLoggerTestHelperLDDFactory()
552 __PRINT("#KPLogTest:~DKPLoggerTestHelperLDDFactory()");
555 //-----------------------------------------------------------------------------------
558 static factory function for the LDD factory.
560 @return pointer to the created instance of the class
562 DKPLoggerTestHelperLDDFactory* DKPLoggerTestHelperLDDFactory::CreateInstance()
564 __PRINT("#KPLogTest:DKPLoggerTestHelperLDDFactory::CreateInstance()");
566 //-- create LDD factory
567 DKPLoggerTestHelperLDDFactory* pSelf = new DKPLoggerTestHelperLDDFactory;
571 __PRINT("#KPLogTest:DKPLoggerTestHelperLDDFactory::CreateInstance(): Unable to create class instance !");
577 //-----------------------------------------------------------------------------------
581 TInt DKPLoggerTestHelperLDDFactory::Install()
583 return SetName(&KPLoggerHelperTestDrv); // Set our name and return error code
586 //-----------------------------------------------------------------------------------
590 void DKPLoggerTestHelperLDDFactory::GetCaps(TDes8& /*aDes*/) const
594 //-----------------------------------------------------------------------------------
597 LDD factory function. Creates LDD object.
598 @param aChannel A pointer to an LDD channel object which will be initialised on return.
599 @return KErrNone if object successfully allocated, KErrNoMemory if not.
600 @return KErrAlreadyExists if the client tries to creae more than 1 instance of the channel
603 TInt DKPLoggerTestHelperLDDFactory::Create(DLogicalChannelBase*& aChannel)
606 if(DKPLoggerTestHelperLDD::pSelf)
607 {//-- channel is a singleton, can't have more than one instance
608 __PRINT("#DKPLoggerTestHelperLDDFactory::Create: Attmpt to create another instance of the LDD!");
609 return KErrAlreadyExists;
612 aChannel = DKPLoggerTestHelperLDD::CreateInstance();
620 //-----------------------------------------------------------------------------------
623 "Standard LDD" entrypoint.
624 Is called on CreateLogicalDevice() if the user calls LoadLogicalDevice(). Creates LDD factory.
626 @return pointer to the LDD factory object.
628 DECLARE_STANDARD_LDD()
630 DKPLoggerTestHelperLDDFactory* pLDDFactory = DKPLoggerTestHelperLDDFactory::CreateInstance();