sysanadatacapture/piprofiler/piprofiler/plugins/GeneralsPlugin/src/GeneralsDriver.cpp
changeset 2 6a82cd05fb1e
parent 1 3ff3fecb12fe
equal deleted inserted replaced
1:3ff3fecb12fe 2:6a82cd05fb1e
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 //
       
    20 // LDD for thread time profiling
       
    21 //
       
    22 
       
    23 #include <kern_priv.h>
       
    24 
       
    25 #include "GeneralsDriver.h"
       
    26 #include <piprofiler/PluginDriver.h>
       
    27 #include <piprofiler/PluginSampler.h>
       
    28 #include <piprofiler/ProfilerTraces.h>
       
    29 
       
    30 #include "GppSamplerImpl.h"
       
    31 #include "GfcSamplerImpl.h"
       
    32 #include "IttSamplerImpl.h"
       
    33 #include "MemSamplerImpl.h"
       
    34 #include "PriSamplerImpl.h"
       
    35 
       
    36 
       
    37 // just for testing
       
    38 extern TUint* IntStackPtr();
       
    39 extern void UsrModLr(TUint32*);
       
    40 
       
    41 // the synch property, for other sampler implementations
       
    42 //const TUid KGppPropertyCat={0x20201F70};
       
    43 //enum TGppPropertyKeys
       
    44 //	{
       
    45 //	EGppPropertySyncSampleNumber
       
    46 //	};
       
    47 
       
    48 static _LIT_SECURITY_POLICY_PASS(KAllowAllPolicy);
       
    49 static _LIT_SECURITY_POLICY_FAIL( KDenyAllPolicy );
       
    50 
       
    51 
       
    52 /*
       
    53  *
       
    54  *
       
    55  *	Class DGfcProfilerFactory definition
       
    56  *
       
    57  *
       
    58  */
       
    59 
       
    60 class DGeneralsProfilerFactory : public DLogicalDevice
       
    61 {
       
    62 	public:
       
    63 		DGeneralsProfilerFactory();
       
    64 
       
    65 
       
    66 	public:
       
    67 		virtual TInt Install();
       
    68 		virtual void GetCaps(TDes8& aDes) const;
       
    69 		virtual TInt Create(DLogicalChannelBase*& aChannel);
       
    70 };
       
    71 
       
    72 /*
       
    73  *
       
    74  *
       
    75  *	Class DGfcDriver definition
       
    76  *
       
    77  *
       
    78  */
       
    79 class DPluginDriver;
       
    80 
       
    81 class DGeneralsDriver : public DPluginDriver
       
    82 {
       
    83 
       
    84 public:
       
    85 	DGeneralsDriver();
       
    86 	~DGeneralsDriver();
       
    87 
       
    88 private:
       
    89 	TInt					NewStart(TInt aRate);
       
    90 	static void				NewDoProfilerProfile(TAny*);
       
    91 	static void				NewDoDfc(TAny*);
       
    92 	TInt					Sample();
       
    93 
       
    94 	TInt					GetSampleTime(TUint32* time);
       
    95 	//TInt					Test(TUint32 testCase); 
       
    96 
       
    97 	TInt					StartSampling();
       
    98 
       
    99 	void					InitialiseSamplerList(); 
       
   100 
       
   101 	DProfilerSamplerBase*		GetSamplerForId(TInt samplerId);
       
   102 	TInt					GetSamplerVersion(TDes* aDes);
       
   103 	
       
   104 	TInt					ProcessStreamReadRequest(TBapBuf* aBuf,TRequestStatus* aStatus);
       
   105 
       
   106 	TInt					MarkTraceActive(TInt samplerIdToActivate);
       
   107 	TInt					MarkTraceInactive(TInt samplerIdToDisable);
       
   108 	TInt					OutputSettingsForTrace(TInt samplerId,TInt settings);
       
   109 	TInt					AdditionalTraceSettings(TInt samplerId,TInt settings);
       
   110 	TInt					AdditionalTraceSettings2(TInt samplerId,TInt settings);
       
   111 	TInt					SetSamplingPeriod(TInt /*samplerId*/,TInt settings);
       
   112 private:
       
   113 	// create the driver in EKA-2 version
       
   114 	TInt					DoCreate(TInt aUnit, const TDesC8* anInfo, const TVersion& aVer);
       
   115 
       
   116 	// receive commands and control in EKA-2 version
       
   117 	void					HandleMsg(TMessageBase* aMsg);
       
   118 private:
       
   119 	// timer mechanism in EKA-2 version
       
   120 	NTimer						iTimer;
       
   121 	TDfc						iNewDfc;
       
   122 	TInt						iCount;
       
   123 	TInt						iLastPcVal;					
       
   124 	TInt						iPeriod;
       
   125 	
       
   126 	// sync sample number property for synchronizing other samplers
       
   127 	RPropertyRef 				iSampleStartTimeProp;
       
   128 	TInt						iSampleStartTime;
       
   129 	
       
   130 	DProfilerGppSampler<10000> 	gppSampler;
       
   131 	DProfilerGfcSampler<10000> 	gfcSampler;
       
   132 	DProfilerIttSampler<10000> 	ittSampler;
       
   133 	DProfilerMemSampler<20000> 	memSampler;
       
   134 	DProfilerPriSampler<10000> 	priSampler;
       
   135 
       
   136 	static const TInt			KSamplerAmount = 5;
       
   137 	DProfilerSamplerBase*		iSamplers[KSamplerAmount];
       
   138 };
       
   139 
       
   140 /*
       
   141  *
       
   142  *
       
   143  *	Class DGeneralsProfilerFactory implementation
       
   144  *
       
   145  *
       
   146  */
       
   147 
       
   148 DECLARE_STANDARD_LDD()
       
   149     {
       
   150 	return new DGeneralsProfilerFactory();
       
   151     }
       
   152 
       
   153 TInt DGeneralsProfilerFactory::Create(DLogicalChannelBase*& aChannel)
       
   154     {
       
   155 	aChannel = new DGeneralsDriver;
       
   156 	return aChannel?KErrNone:KErrNoMemory;
       
   157     }
       
   158 
       
   159 
       
   160 DGeneralsProfilerFactory::DGeneralsProfilerFactory()
       
   161     {
       
   162 	// major, minor, and build version number
       
   163     iVersion=TVersion(1,0,1);
       
   164     }
       
   165 
       
   166 
       
   167 TInt DGeneralsProfilerFactory::Install()
       
   168     {
       
   169     return(SetName(&KPluginSamplerName));
       
   170     }
       
   171 
       
   172 void DGeneralsProfilerFactory::GetCaps(TDes8& aDes) const
       
   173     {
       
   174     TCapsSamplerV01 b;
       
   175     
       
   176     b.iVersion=TVersion(1,0,1);
       
   177     
       
   178     aDes.FillZ(aDes.MaxLength());
       
   179     aDes.Copy((TUint8*)&b,Min(aDes.MaxLength(),sizeof(b)));
       
   180     }
       
   181 
       
   182 /*
       
   183  *
       
   184  *
       
   185  *	Class DGeneralsDriver implementation
       
   186  *
       
   187  *
       
   188  */
       
   189  
       
   190 DGeneralsDriver::DGeneralsDriver() :
       
   191 	iTimer(NewDoProfilerProfile,this),
       
   192 	iNewDfc(NewDoDfc,this,NULL,7),
       
   193 	gfcSampler(gppSampler.GetExportData()),
       
   194 	ittSampler(gppSampler.GetExportData()),
       
   195 	memSampler(gppSampler.GetExportData(), PROFILER_MEM_SAMPLER_ID),
       
   196 	priSampler(gppSampler.GetExportData(), PROFILER_PRI_SAMPLER_ID)
       
   197     {
       
   198 	LOGSTRING("DGeneralsDriver::DGeneralsDriver()");
       
   199 
       
   200 	iState = EStopped;
       
   201 	iEndRequestStatus = 0;
       
   202 	doingDfc = 0;
       
   203 	sampleRunning = 0;
       
   204 	iSyncOffset = 0;
       
   205 	InitialiseSamplerList();
       
   206     }
       
   207 
       
   208 /*
       
   209  *
       
   210  *	This method has to be changed for each new sampler
       
   211  *
       
   212  */ 
       
   213 void DGeneralsDriver::InitialiseSamplerList()
       
   214 	{
       
   215 	// initialize all samplers to zero
       
   216 	for(TInt i=0;i<KSamplerAmount;i++)
       
   217 		{
       
   218 		iSamplers[i] = 0;
       
   219 		}
       
   220 
       
   221 	TInt i(0);
       
   222 	iSamplers[i] = &gppSampler;i++;
       
   223 	iSamplers[i] = &gfcSampler;i++;
       
   224 	iSamplers[i] = &ittSampler;i++;
       
   225 	iSamplers[i] = &memSampler;i++;
       
   226 	iSamplers[i] = &priSampler;i++;
       
   227 	
       
   228 	// initialize synchronizing property
       
   229 	LOGSTRING("DGeneralsDriver::InitialiseSamplerList() - initializing property");
       
   230 	TInt r = iSampleStartTimeProp.Attach(KGppPropertyCat, EGppPropertySyncSampleNumber);
       
   231     if (r!=KErrNone)
       
   232         {
       
   233         LOGSTRING2("DGeneralsDriver::InitialiseSamplerList() - error in attaching counter property, error %d", r);
       
   234         }
       
   235     LOGSTRING("DGeneralsDriver::InitialiseSamplerList() - defining properties");
       
   236     r = iSampleStartTimeProp.Define(RProperty::EInt, KAllowAllPolicy, KDenyAllPolicy, 0, NULL);
       
   237     if (r!=KErrNone)
       
   238         {
       
   239         LOGSTRING2("DGeneralsDriver::InitialiseSamplerList() - error in defining counter property, error %d", r);
       
   240         }	
       
   241 	}
       
   242 
       
   243 
       
   244 DProfilerSamplerBase* DGeneralsDriver::GetSamplerForId(TInt samplerIdToGet)
       
   245     {
       
   246 	for(TInt i=0;i<KSamplerAmount;i++)
       
   247 	    {
       
   248 		if(iSamplers[i]->iSamplerId == samplerIdToGet)
       
   249 		    {
       
   250 			return iSamplers[i];
       
   251 		    }
       
   252 	    }
       
   253 	return (DProfilerSamplerBase*)0;
       
   254     }
       
   255 
       
   256 
       
   257 TInt DGeneralsDriver::DoCreate(TInt aUnit, const TDesC8* anInfo, const TVersion& aVer)
       
   258     {
       
   259     TUint8 err(KErrNone);
       
   260     if (!Kern::QueryVersionSupported(TVersion(1,0,1),aVer))
       
   261 	   	return KErrNotSupported;
       
   262 
       
   263 	// just for testing 
       
   264 	LOGTEXT("Initializing the stack pointer");
       
   265 	stackTop=(TUint32*)IntStackPtr();
       
   266 	LOGSTRING2("Got stack pointer 0x%x",(TUint32)stackTop);
       
   267 
       
   268 	iClient = &Kern::CurrentThread();
       
   269 	err = iClient->Open();
       
   270 	
       
   271 	iSampleStream.InsertCurrentClient(iClient);
       
   272 	
       
   273 	iTimer.Cancel();
       
   274 	iNewDfc.Cancel();
       
   275 
       
   276 	Kern::SetThreadPriority(24);
       
   277 	SetDfcQ(Kern::DfcQue0());
       
   278 	iNewDfc.SetDfcQ(iDfcQ);
       
   279 	iMsgQ.Receive();
       
   280 	return err;
       
   281     }
       
   282 
       
   283 DGeneralsDriver::~DGeneralsDriver()
       
   284     {
       
   285 	if (iState!=EStopped)
       
   286 	    iTimer.Cancel();
       
   287 	iNewDfc.Cancel();
       
   288 	iSampleStartTimeProp.Close();
       
   289 	Kern::SafeClose((DObject*&)iClient,NULL);
       
   290     }
       
   291 
       
   292 
       
   293 TInt DGeneralsDriver::GetSampleTime(TUint32* time)
       
   294     {
       
   295 	LOGSTRING("DGeneralsDriver::GetSampleTime - entry");
       
   296 
       
   297 	Kern::ThreadRawWrite(	iClient,(TAny*)time, 
       
   298 							(TAny*)&gppSampler.GetExportData()->sampleNumber, 
       
   299 							4, iClient);
       
   300 
       
   301 	LOGSTRING("DGeneralsDriver::GetSampleTime - exit");
       
   302 
       
   303 	return KErrNone;
       
   304     }
       
   305 
       
   306 
       
   307 TInt DGeneralsDriver::GetSamplerVersion(TDes* aDes)
       
   308     {
       
   309 	LOGSTRING2("DGeneralsDriver::GetSamplerVersion - 0x%x",aDes);
       
   310 	
       
   311 	TBuf8<16> aBuf;
       
   312 	aBuf.Append(PROFILER_SAMPLER_VERSION);
       
   313 	Kern::ThreadDesWrite(iClient,aDes,aBuf,0,KChunkShiftBy0,iClient);
       
   314 	LOGSTRING("DGeneralsDriver::GetSamplerVersion - written client descriptor");
       
   315 	return KErrNone;
       
   316     }
       
   317 
       
   318 TInt DGeneralsDriver::NewStart(TInt aDelay)
       
   319     {	
       
   320 	LOGSTRING("DGeneralsDriver::NewStart");
       
   321 	iEndRequestStatus = 0;
       
   322 	
       
   323 	aDelay = Min(KMaxDelay, Max(KMinDelay, aDelay));
       
   324 
       
   325 	// always use this rate
       
   326 	iPeriod = aDelay;
       
   327 	
       
   328 	iTimer.OneShot(aDelay);
       
   329 	
       
   330 	iState = ERunning;
       
   331 
       
   332 	return KErrNone;
       
   333     }
       
   334 
       
   335 /*
       
   336  *	This function is run in each interrupt
       
   337  */
       
   338 // EKA-2 implementation of the sampler method
       
   339 
       
   340 void DGeneralsDriver::NewDoProfilerProfile(TAny* pointer)
       
   341     {
       
   342     DGeneralsDriver& d = *((DGeneralsDriver*)pointer);
       
   343 
       
   344 	if (d.iState == ERunning && d.sampleRunning == 0)
       
   345 	    {
       
   346 		d.iTimer.Again(d.iPeriod);
       
   347 		d.sampleRunning++;
       
   348 
       
   349 		TInt8 postSampleNeeded = 0;
       
   350 
       
   351 		for(TInt i=0;i<KSamplerAmount;i++)
       
   352 		    {
       
   353 			if(d.iSamplers[i]->iEnabled)
       
   354 			    {
       
   355 				d.iSamplers[i]->Sample();
       
   356 				postSampleNeeded += d.iSamplers[i]->PostSampleNeeded();
       
   357                 }
       
   358             }
       
   359 			
       
   360 		if(postSampleNeeded > 0 && d.doingDfc == 0)
       
   361 		    {
       
   362 			d.doingDfc++;
       
   363 			d.iNewDfc.Add();
       
   364 
       
   365 			d.sampleRunning--;
       
   366 			return;
       
   367             }
       
   368 		d.sampleRunning--;
       
   369         }
       
   370 	else if (d.iState == EStopping && d.sampleRunning == 0)
       
   371 	    {
       
   372 		// add a dfc for this final time
       
   373 		d.iNewDfc.Add();
       
   374         }
       
   375 	else
       
   376 	    {
       
   377 		// the previous sample has not finished,
       
   378 		Kern::Printf("DGeneralsDriver::NewDoProfilerProfile - Profiler Sampler Error - interrupted before finished sampling!!");
       
   379         }
       
   380     }
       
   381 
       
   382 
       
   383 TInt DGeneralsDriver::Sample()
       
   384     {
       
   385 	return 0;
       
   386     }
       
   387 /*
       
   388  *	This function is run when any of the samplers
       
   389  *	requires post sampling
       
   390  */
       
   391 void DGeneralsDriver::NewDoDfc(TAny* pointer)
       
   392     {
       
   393 	DGeneralsDriver& d = *((DGeneralsDriver*)pointer);
       
   394 	
       
   395 	if(d.iState == ERunning)
       
   396 	    {
       
   397 		// for all enabled samplers, perform
       
   398 		// post sample if needed
       
   399 		for(TInt i=0;i<KSamplerAmount;i++)
       
   400 		    {
       
   401 			if(d.iSamplers[i]->iEnabled)
       
   402 			    {
       
   403 				if(d.iSamplers[i]->PostSampleNeeded())
       
   404 				    {
       
   405 					d.iSamplers[i]->PostSample();
       
   406                     }
       
   407                 }
       
   408             }
       
   409 		d.doingDfc--;
       
   410         }
       
   411 
       
   412 	else if(d.iState == EStopping)
       
   413 	    {
       
   414 		// for all enabled samplers,
       
   415 		// perform end sampling
       
   416 		TBool releaseBuffer = false;
       
   417 		for(TInt i=0;i<KSamplerAmount;i++)
       
   418 		    {
       
   419 			if(d.iSamplers[i]->iEnabled)
       
   420 			    {
       
   421 				LOGSTRING("DGeneralsDriver::NewDoDfc() - ending");
       
   422 				// perform end sampling for all samplers
       
   423 				// stream mode samplers may be pending, if they
       
   424 				// are still waiting for another client buffer
       
   425 				if(d.iSamplers[i]->EndSampling() == KErrNotReady) 
       
   426 				    {
       
   427 					LOGSTRING("DGeneralsDriver::NewDoDfc() - stream data pending");
       
   428 					releaseBuffer = true;
       
   429                     }
       
   430 				else 
       
   431 				    {
       
   432 					LOGSTRING("DGeneralsDriver::NewDoDfc() - no data pending");
       
   433 					releaseBuffer = true;
       
   434                     }		
       
   435                 }
       
   436             }
       
   437 
       
   438 		// At the end, once all the samplers are gone through, the buffer should be released
       
   439 		if (true == releaseBuffer) 
       
   440 		    {
       
   441 			LOGSTRING("DGeneralsDriver::NewDoDfc() - release the buffer");
       
   442 			d.iSampleStream.ReleaseIfPending();	
       
   443             }
       
   444 		
       
   445 		d.iState = EStopped;
       
   446 		if(d.iEndRequestStatus != 0 && d.iClient != 0)
       
   447 		    {
       
   448 			// sampling has ended
       
   449 			Kern::RequestComplete(d.iClient,d.iEndRequestStatus,KErrNone);
       
   450             }
       
   451         }
       
   452     }
       
   453 
       
   454 
       
   455 /*
       
   456  *	All controls are handled here
       
   457  */
       
   458  
       
   459 void DGeneralsDriver::HandleMsg(TMessageBase* aMsg)
       
   460     {
       
   461 	TInt r=KErrNone;
       
   462 	TThreadMessage& m=*(TThreadMessage*)aMsg;
       
   463 
       
   464 	LOGSTRING5("DGeneralsDriver::HandleMsg 0x%x 0x%x 0x%x 0x%x",m.Int0(),m.Int1(),m.Int2(),m.Int3());
       
   465 	
       
   466 	if(m.iValue == (TInt)ECloseMsg)
       
   467 	    {
       
   468 		LOGSTRING("DGeneralsDriver::HandleMsg - received close message");
       
   469 		iTimer.Cancel();
       
   470 		iNewDfc.Cancel();
       
   471 		m.Complete(KErrNone,EFalse);
       
   472 		iMsgQ.CompleteAll(KErrServerTerminated);
       
   473 		LOGSTRING("DGeneralsDriver::HandleMsg - cleaned up the driver!");
       
   474 		return;
       
   475         }
       
   476 
       
   477 	if (m.Client()!=iClient)
       
   478 	    {
       
   479 		LOGSTRING("DGeneralsDriver::HandleMsg - ERROR, wrong client");
       
   480 		m.PanicClient(_L("GENERALSSAMPLER"),EAccessDenied);
       
   481 		return;
       
   482         }
       
   483 
       
   484 	TInt id=m.iValue;
       
   485 	switch(id)
       
   486 	    {
       
   487 		 //Controls are handled here
       
   488 		case RPluginSampler::EMarkTraceActive:
       
   489 			LOGSTRING("DGeneralsDriver::HandleMsg - EMarkTraceActive");
       
   490 			r = MarkTraceActive((TInt)m.Int0());
       
   491 			break;
       
   492 
       
   493 		case RPluginSampler::EOutputSettingsForTrace:
       
   494 			LOGSTRING("DGeneralsDriver::HandleMsg - EOutputSettingsForTrace");
       
   495 			r = OutputSettingsForTrace((TInt)m.Int0(),(TInt)m.Int1());
       
   496 			break;
       
   497 
       
   498 		case RPluginSampler::EAdditionalTraceSettings:
       
   499 			LOGSTRING("DGeneralsDriver::HandleMsg - EAdditionalTraceSettings");
       
   500 			r = AdditionalTraceSettings((TInt)m.Int0(),(TInt)m.Int1());
       
   501 			break;
       
   502 
       
   503 		case RPluginSampler::EAdditionalTraceSettings2:
       
   504 			LOGSTRING("DGeneralsDriver::HandleMsg - EAdditionalTraceSettings2");
       
   505 			r = AdditionalTraceSettings2((TInt)m.Int0(),(TInt)m.Int1());
       
   506 			break;
       
   507 			
       
   508 		case RPluginSampler::ESetSamplingPeriod:
       
   509 		    LOGSTRING2("DGeneralsDriver::HandleMsg - ESetSamplingPeriod %d", (TInt)m.Int1());
       
   510 			r = SetSamplingPeriod((TInt)m.Int0(),(TInt)m.Int1());
       
   511 			break;
       
   512 			
       
   513 		case RPluginSampler::EMarkTraceInactive:
       
   514 			LOGSTRING("DGeneralsDriver::HandleMsg - EMarkTraceInactive");
       
   515 			r = MarkTraceInactive((TInt)m.Int0());
       
   516 			break;
       
   517 
       
   518 		case RPluginSampler::ESample:
       
   519 			LOGSTRING("DGeneralsDriver::HandleMsg - ESample");
       
   520 			r = Sample();
       
   521 			break;
       
   522 
       
   523 		case RPluginSampler::EStartSampling:
       
   524 			LOGSTRING("DGeneralsDriver::HandleMsg - EStartSampling");
       
   525 			r = StartSampling();
       
   526 			break;
       
   527 
       
   528 		case RPluginSampler::EGetSampleTime:
       
   529 			LOGSTRING("DGeneralsDriver::HandleMsg - EGetSampleTime");
       
   530 			r = GetSampleTime(reinterpret_cast<TUint32*>(m.Ptr0()));
       
   531 			break;
       
   532 
       
   533 		case RPluginSampler::EGetSamplerVersion:
       
   534 			LOGSTRING("DGeneralsDriver::HandleMsg - EGetSamplerVersion");
       
   535 			r = GetSamplerVersion(reinterpret_cast<TDes*>(m.Ptr0()));
       
   536 			break;
       
   537 		
       
   538 		case RPluginSampler::ECancelStreamRead:
       
   539 			LOGSTRING("DGeneralsDriver::HandleMsg - ECancelStreamRead");
       
   540 			iStreamReadCancelStatus = reinterpret_cast<TRequestStatus*>(m.Ptr0());
       
   541 			r = ProcessStreamReadCancel();
       
   542 			break;
       
   543 
       
   544 
       
   545 		 //	Requests are handled here
       
   546 
       
   547 		case ~RPluginSampler::EStopAndWaitForEnd:
       
   548 			LOGSTRING("DGeneralsDriver::HandleMsg - EStopAndWaitForEnd");
       
   549 			iEndRequestStatus = reinterpret_cast<TRequestStatus*>(m.Ptr0());
       
   550 			r = StopSampling();
       
   551 			break;
       
   552 
       
   553 		case ~RPluginSampler::ERequestFillThisStreamBuffer:
       
   554 			LOGSTRING("DGeneralsDriver::HandleMsg - ERequestFillThisStreamBuffer");			
       
   555 			r = ProcessStreamReadRequest(	reinterpret_cast<TBapBuf*>(m.Ptr1()),
       
   556 											reinterpret_cast<TRequestStatus*>(m.Ptr0()));
       
   557 			break;
       
   558 
       
   559 		default:
       
   560 			LOGSTRING2("DGeneralsDriver::HandleMsg - ERROR, unknown command %d",id);
       
   561 			r = KErrNotSupported;
       
   562 			break;
       
   563         }
       
   564 
       
   565 	LOGSTRING("DGeneralsDriver::HandleMsg - Completed");
       
   566 	m.Complete(r,ETrue);
       
   567     }
       
   568 
       
   569 
       
   570 inline TInt DGeneralsDriver::ProcessStreamReadRequest(TBapBuf* aBuf,TRequestStatus* aStatus)
       
   571 	{
       
   572 	LOGSTRING("DGeneralsDriver::ProcessStreamReadRequest - entry");
       
   573 	
       
   574 	// a new sample buffer has been received from the client
       
   575 	iSampleStream.AddSampleBuffer(aBuf,aStatus);
       
   576 	
       
   577 	// check if we are waiting for the last data to be written to the client
       
   578 	if(iState == EStopped)
       
   579 	    {
       
   580 		LOGSTRING("DGeneralsDriver::ProcessStreamReadRequest state = EStopped");
       
   581 	
       
   582 		// sampling has stopped and stream read cancel is pending
       
   583 		// try to perform the end sampling procedure again
       
   584 		TBool releaseBuffer = false;
       
   585 		for(TInt i=0;i<KSamplerAmount;i++)
       
   586 		    {
       
   587 			// only for all enabled samplers that have stream output mode
       
   588 			if(iSamplers[i]->iEnabled /*&& samplers[i]->outputMode == 2*/)
       
   589 			    {
       
   590 				//TInt pending = 0;
       
   591 				// stream mode samplers may be pending, if they
       
   592 				// are still waiting for another client buffer,
       
   593 				// in that case, the request should be completed already
       
   594 				if(iSamplers[i]->EndSampling() == KErrNotReady) 
       
   595 				    {
       
   596 					LOGSTRING("DGeneralsDriver::ProcessStreamReadRequest - still data pending");
       
   597 					releaseBuffer = true;
       
   598                     }
       
   599 				else 
       
   600 				    {
       
   601 					LOGSTRING("DGeneralsDriver::ProcessStreamReadRequest - no data pending");
       
   602 					releaseBuffer = true;
       
   603                     }
       
   604                 }
       
   605             }
       
   606 		// At the end, once all the samplers are gone through, the buffer should be released
       
   607 		if (true == releaseBuffer) 
       
   608 		    {
       
   609 			LOGSTRING("DGeneralsDriver::ProcessStreamReadRequest - all data copied, release the buffer");
       
   610 			iSampleStream.ReleaseIfPending();
       
   611 		    }
       
   612         }
       
   613 	LOGSTRING("DGeneralsDriver::ProcessStreamReadRequest - exit");
       
   614 	
       
   615 	return KErrNone;
       
   616 	}
       
   617 
       
   618 
       
   619 /*
       
   620  *	Mark traces active or inactive, this can be done
       
   621  *	only if sampling is not running
       
   622  */
       
   623 
       
   624 inline TInt DGeneralsDriver::MarkTraceActive(TInt samplerIdToActivate)
       
   625 	{
       
   626 	LOGSTRING2("DGeneralsDriver::MarkTraceActive %d",samplerIdToActivate);
       
   627 
       
   628 	for(TInt i=0;i<KSamplerAmount;i++)
       
   629 	    {
       
   630 		if(iSamplers[i]->iSamplerId == samplerIdToActivate)
       
   631 		    {
       
   632 			iSamplers[i]->SetEnabledFlag(true);
       
   633 			return KErrNone;
       
   634             }
       
   635         }
       
   636 
       
   637 	LOGSTRING2("DGeneralsDriver::MarkTraceActive - %d not supported",samplerIdToActivate);
       
   638 	return KErrNotSupported;
       
   639 	}
       
   640 
       
   641 inline TInt DGeneralsDriver::MarkTraceInactive(TInt samplerIdToDisable)
       
   642 	{
       
   643 	LOGSTRING2("DGeneralsDriver::MarkTraceInactive %d",samplerIdToDisable);
       
   644 
       
   645 	for(TInt i=0;i<KSamplerAmount;i++)
       
   646 	    {
       
   647 		if(iSamplers[i]->iSamplerId == samplerIdToDisable)
       
   648 		    {
       
   649 			iSamplers[i]->SetEnabledFlag(false);
       
   650 			return KErrNone;
       
   651             }
       
   652         }
       
   653 
       
   654 	LOGSTRING2("DGeneralsDriver::MarkTraceInactive - %d not supported",samplerIdToDisable);
       
   655 	return KErrNotSupported;
       
   656 	}
       
   657 
       
   658 /*
       
   659  *	Set output settings for a trace
       
   660  */
       
   661  
       
   662 inline TInt DGeneralsDriver::OutputSettingsForTrace(TInt samplerId,TInt settings)
       
   663 	{
       
   664 	LOGSTRING3("DGeneralsDriver::OutputSettingsForTrace id:%d set:%d",samplerId,settings);
       
   665 
       
   666 	for(TInt i=0;i<KSamplerAmount;i++)
       
   667 	    {
       
   668 		if(iSamplers[i]->iSamplerId == samplerId)
       
   669 		    {
       
   670 			iSamplers[i]->SetOutputCombination(settings);
       
   671 			return KErrNone;
       
   672 		    }
       
   673 	    }
       
   674 
       
   675 	return KErrNotSupported;	
       
   676 	}
       
   677 
       
   678 /*
       
   679  *	Set additional settings for a trace
       
   680  */
       
   681 
       
   682 inline TInt DGeneralsDriver::AdditionalTraceSettings(TInt samplerId,TInt settings)
       
   683 	{
       
   684 	LOGSTRING3("DGeneralsDriver::SetAdditionalTraceSettings id:%d set:%d",samplerId,settings);
       
   685 
       
   686 	for(TInt i=0;i<KSamplerAmount;i++)
       
   687 	    {
       
   688 		if(iSamplers[i]->iSamplerId == samplerId)
       
   689 		    {
       
   690 			iSamplers[i]->SetAdditionalSettings(settings);
       
   691 			return KErrNone;
       
   692             }
       
   693         }
       
   694 
       
   695 	return KErrNotSupported;	
       
   696 	}
       
   697 
       
   698 inline TInt DGeneralsDriver::AdditionalTraceSettings2(TInt samplerId,TInt settings)
       
   699 	{
       
   700 	LOGSTRING3("DGeneralsDriver::SetAdditionalTraceSettings id:%d set:%d",samplerId,settings);
       
   701 
       
   702 	for(TInt i=0;i<KSamplerAmount;i++)
       
   703 	    {
       
   704 		if(iSamplers[i]->iSamplerId == samplerId)
       
   705 		    {
       
   706 			iSamplers[i]->SetAdditionalSettings2(settings);
       
   707 			return KErrNone;
       
   708 		    }
       
   709         }
       
   710 
       
   711 	return KErrNotSupported;	
       
   712 	}
       
   713 
       
   714 inline TInt DGeneralsDriver::SetSamplingPeriod(TInt samplerId,TInt settings)
       
   715 	{
       
   716 	LOGSTRING2("DGeneralsDriver::SetSamplingPeriod - set:%d",settings);
       
   717 
       
   718 //	iPeriod = settings;
       
   719 
       
   720 	for(TInt i(0);i<KSamplerAmount;i++)
       
   721 	    {
       
   722 		if(iSamplers[i]->iSamplerId == samplerId)
       
   723 		    {
       
   724 			iSamplers[i]->SetSamplingPeriod(settings);
       
   725 			return KErrNone;
       
   726 		    }
       
   727 	    }
       
   728 
       
   729 	return KErrNotSupported;	
       
   730 	}
       
   731 
       
   732 /*
       
   733  *	Mark traces active or inactive, this can be done
       
   734  *	only if sampling is not running
       
   735  */
       
   736  
       
   737 TInt DGeneralsDriver::StartSampling()
       
   738 	{
       
   739 	LOGSTRING("DGeneralsDriver::StartSampling");
       
   740 
       
   741 	if(iState == EStopped)
       
   742 		{
       
   743 		// reset iSampleStartTimeProp property value
       
   744 		iSampleStartTime = NKern::TickCount();	// get the system tick value for sync purposes 
       
   745 		TInt r = iSampleStartTimeProp.Set(iSampleStartTime);
       
   746 		
       
   747 		// reset all enabled samplers
       
   748 		for(TInt i=0;i<KSamplerAmount;i++)
       
   749 			{
       
   750 			if(iSamplers[i]->iEnabled)
       
   751 				{
       
   752 
       
   753 				// reset with stream option
       
   754 				LOGSTRING2(("DGeneralsDriver::StartSampling - stream reset for generals driver, sync offset %d"), 0);
       
   755 				iSamplers[i]->Reset(&iSampleStream, 0);
       
   756 				}
       
   757 			}
       
   758 
       
   759 		NewStart(gppSampler.GetPeriod());
       
   760 		return KErrNone;
       
   761 		}
       
   762 	else
       
   763 		{
       
   764 		return KErrGeneral;
       
   765 		}
       
   766 	}
       
   767 
       
   768 
       
   769