perfsrv/piprofiler/plugins/GeneralsPlugin/inc/IttSamplerImpl.h
changeset 62 1c2bb2fc7c87
parent 51 98307c651589
equal deleted inserted replaced
56:aa2539c91954 62:1c2bb2fc7c87
    53 	TBool	SampleNeeded(TUint32 sampleNum);
    53 	TBool	SampleNeeded(TUint32 sampleNum);
    54 	TInt 	CreateFirstSample();
    54 	TInt 	CreateFirstSample();
    55 	void	Reset();
    55 	void	Reset();
    56 	TInt    ProcessEvent();
    56 	TInt    ProcessEvent();
    57 	
    57 	
       
    58 	TInt   HandleLibs(TUint32 sampleNum);
       
    59 	TInt   HandleSegs(TUint32 sampleNum);
       
    60 	
    58 	TUint8*         itt_sample;
    61 	TUint8*         itt_sample;
    59 	TInt            iIttSamplingPeriod;
    62 	TInt            iIttSamplingPeriod;
    60 	TInt            iIttSamplingPeriodDiv2;
    63 	TInt            iIttSamplingPeriodDiv2;
    61 	TBool           iTimeToSample;
    64 	TBool           iTimeToSample;
    62 #ifdef ITT_EVENT_HANDLER
    65 #ifdef ITT_EVENT_HANDLER
    63     TBool           iEventReceived;
    66     TBool           iEventReceived;
    64     TBool           iFirstSampleTaken;
    67     TBool           iInitialLibsTaken;
       
    68     TBool           iInitialSegsTaken;
    65 #endif
    69 #endif
    66 	
    70 	
    67 private:
    71 private:
       
    72     enum {
       
    73         KIttHandlingLibs=0,
       
    74         KIttHandlingCodeSegs    
       
    75     };
    68 #ifdef ITT_EVENT_HANDLER    
    76 #ifdef ITT_EVENT_HANDLER    
    69     TInt            iCount;
    77     TInt            iCount;
    70 #endif
    78 #endif
    71     TInt            currentLibCount;
    79     TInt            iCurrentLibCount;
       
    80     TInt            iLibsCount;
       
    81     TInt            iCodeSegsCount;
    72     TInt            currentProcCount;
    82     TInt            currentProcCount;
    73     
    83     
    74 	TUint8          sample[KITTSampleBufferSize ];
    84 	TUint8          sample[KITTSampleBufferSize ];
    75 	TPtr8           sampleDescriptor;
    85 	TPtr8           sampleDescriptor;
    76 		
    86 		
    77 	TBuf8<64>		iVersionData;
    87 	TBuf8<64>		iVersionData;
    78 	SDblQue* 		codeSegList;
    88 	SDblQue* 		codeSegList;
    79 
    89     TUint8          iInitState;
       
    90     SDblQueLink*    iLatestCodeseg;
    80 };
    91 };
    81 
    92 
    82 /*
    93 /*
    83  * ITT sampler kernel part
    94  * ITT sampler kernel part
    84  * 
    95  * 
    88 {
    99 {
    89 public:
   100 public:
    90 	DProfilerIttSampler(struct TProfilerGppSamplerData* gppSamplerDataIn);
   101 	DProfilerIttSampler(struct TProfilerGppSamplerData* gppSamplerDataIn);
    91 	~DProfilerIttSampler();
   102 	~DProfilerIttSampler();
    92 
   103 
    93 	void	Sample();
   104 	void	Sample(TAny* aPtr);
    94 	TInt	Reset(DProfilerSampleStream* aStream, TUint32 aSyncOffset);
   105 	TInt	Reset(DProfilerSampleStream* aStream, TUint32 aSyncOffset);
    95 	TInt	PostSample();
   106 	TInt	PostSample();
    96 	TBool	PostSampleNeeded();
   107 	TBool	PostSampleNeeded();
    97 
   108 
    98 private:
   109 private:
       
   110 
    99 #ifdef ITT_EVENT_HANDLER
   111 #ifdef ITT_EVENT_HANDLER
   100     DIttEventHandler*               iEventHandler;
   112     DIttEventHandler*               iEventHandler;
   101 #endif
   113 #endif
   102 	IttSamplerImpl					ittSamplerImpl;
   114 	IttSamplerImpl					ittSamplerImpl;
   103 	struct TProfilerGppSamplerData*     gppSamplerData;
   115 	struct TProfilerGppSamplerData*     gppSamplerData;
   104 	TBool							sampleInProgress;
   116 	TBool							sampleInProgress;
   105 	TBool							sampleNeeded;
   117 	TBool							sampleNeeded;
       
   118 	TUint32                         iSyncOffset;
   106 };
   119 };
   107 
   120 
   108 /*  
   121 /*  
   109  *	ITT sampler implementation
   122  *	ITT sampler implementation
   110  *	
   123  *	
   111  */ 
   124  */ 
   112 template <int BufferSize>
   125 template <int BufferSize>
   113 DProfilerIttSampler<BufferSize>::DProfilerIttSampler(struct TProfilerGppSamplerData* gppSamplerDataIn) :
   126 DProfilerIttSampler<BufferSize>::DProfilerIttSampler(struct TProfilerGppSamplerData* gppSamplerDataIn) :
   114 	DProfilerGenericSampler<BufferSize>(PROFILER_ITT_SAMPLER_ID)
   127 	DProfilerGenericSampler<BufferSize>(PROFILER_ITT_SAMPLER_ID)
   115 {
   128     {
   116 	this->gppSamplerData = (struct TProfilerGppSamplerData*)gppSamplerDataIn;
   129 	this->gppSamplerData = (struct TProfilerGppSamplerData*)gppSamplerDataIn;
   117 	this->sampleInProgress = false;
   130 	this->sampleInProgress = false;
   118 	LOGSTRING2("CProfilerIttSampler<%d>::CProfilerIttSampler",BufferSize);	
   131 	iSyncOffset = 0;
   119 }
   132 	LOGSTRING2("DProfilerIttSampler<%d>::DProfilerIttSampler",BufferSize);	
       
   133     }
   120 
   134 
   121 /*
   135 /*
   122  *  DProfilerIttSampler::Reset()
   136  *  DProfilerIttSampler::Reset()
   123  *  
   137  *  
   124  *  @param DProfilerSampleStream* sample stream
   138  *  @param DProfilerSampleStream* sample stream
   125  *  @param TUint32 Offset 
   139  *  @param TUint32 Offset 
   126  */
   140  */
   127 template <int BufferSize>
   141 template <int BufferSize>
   128 TInt DProfilerIttSampler<BufferSize>::Reset(DProfilerSampleStream* aStream, TUint32 aSyncOffset)
   142 TInt DProfilerIttSampler<BufferSize>::Reset(DProfilerSampleStream* aStream, TUint32 aSyncOffset)
   129 {
   143     {
   130     Kern::Printf("DProfilerIttSampler<%d>::Reset - calling superclass reset",BufferSize);
   144     iSyncOffset = aSyncOffset;
   131     DProfilerGenericSampler<BufferSize>::Reset(aStream);
       
   132 
       
   133     // check if reset called in stop (by driver)
   145     // check if reset called in stop (by driver)
   134     if(aSyncOffset != 999999)
   146     if(iSyncOffset != KStateSamplingEnding)
   135         {
   147         {
       
   148         DProfilerGenericSampler<BufferSize>::Reset(aStream);
   136 #ifdef ITT_EVENT_HANDLER
   149 #ifdef ITT_EVENT_HANDLER
   137         // Itt event handler
   150         // Itt event handler
   138         if(iEventHandler)
   151         if(iEventHandler)
   139             {
   152             {
   140             // stop previous sampling if still running
   153             // stop previous sampling if still running
   141             Kern::Printf("Stopping DIttEventHandler");
   154             Kern::Printf("Stopping DIttEventHandler");
   142             iEventHandler->Stop();
   155             iEventHandler->Stop();
   143             iEventHandler->Close();
   156             }
   144             iEventHandler = NULL;
   157     
   145             }
       
   146     
       
   147         Kern::Printf("Initiating DIttEventHandler");
       
   148         iEventHandler = new DIttEventHandler(this->iSampleBuffer, this->gppSamplerData);
   158         iEventHandler = new DIttEventHandler(this->iSampleBuffer, this->gppSamplerData);
   149         if(iEventHandler)
   159         if(iEventHandler)
   150             {
   160             {
   151             Kern::Printf("Creating DIttEventHandler");
       
   152             TInt err(iEventHandler->Create());
   161             TInt err(iEventHandler->Create());
   153             if(err != KErrNone)
   162             if(err != KErrNone)
   154                 {
   163                 {
   155                 Kern::Printf("Error in creation of DIttEventHandler, error %d", err);
   164                 Kern::Printf("Error in creation of DIttEventHandler, error %d", err);
   156                 return err;
   165                 return err;
   161             Kern::Printf("Could not initiate DIttEventHandler");
   170             Kern::Printf("Could not initiate DIttEventHandler");
   162             return KErrGeneral;
   171             return KErrGeneral;
   163             }
   172             }
   164     
   173     
   165         // set first sample at the 10 ms, should be enough
   174         // set first sample at the 10 ms, should be enough
   166         this->ittSamplerImpl.iIttSamplingPeriod = 10;
   175         this->ittSamplerImpl.iIttSamplingPeriod = 8;
   167 #else
   176 #else
   168         this->ittSamplerImpl.iIttSamplingPeriod = this->iSamplingPeriod;
   177         this->ittSamplerImpl.iIttSamplingPeriod = this->iSamplingPeriod;
   169 #endif
   178 #endif
   170         this->ittSamplerImpl.iIttSamplingPeriodDiv2 = (TInt)(this->ittSamplerImpl.iIttSamplingPeriod / 2);
   179         this->ittSamplerImpl.iIttSamplingPeriodDiv2 = (TInt)(this->ittSamplerImpl.iIttSamplingPeriod / 2);
   171         LOGSTRING3("CProfilerIttSampler<%d>::Reset - set ITT sampling period to %d",
   180         LOGSTRING3("DProfilerIttSampler<%d>::Reset - set ITT sampling period to %d",
   172                                 BufferSize,this->ittSamplerImpl.iIttSamplingPeriod);
   181                                 BufferSize,this->ittSamplerImpl.iIttSamplingPeriod);
   173         }
   182         }
   174     else
   183     else
   175         {
   184         {
   176         LOGSTRING2("DProfilerIttSampler<%d>::Reset - reset in stop", BufferSize);
   185         LOGSTRING2("DProfilerIttSampler<%d>::Reset - reset in stop", BufferSize);
   178         // destroy memory event handler
   187         // destroy memory event handler
   179         if(iEventHandler)
   188         if(iEventHandler)
   180             {
   189             {
   181             // stop previous sampling if still running
   190             // stop previous sampling if still running
   182             iEventHandler->Stop();
   191             iEventHandler->Stop();
   183             iEventHandler->Close();
       
   184             iEventHandler = NULL;
       
   185             }
   192             }
   186 #endif
   193 #endif
   187         return KErrNone;    // return if reset called in stop
   194         return KErrNone;    // return if reset called in stop
   188         }
   195         }
   189 
   196 
   194     this->sampleNeeded = false;
   201     this->sampleNeeded = false;
   195     //LOGSTRING("DProfilerIttSampler::Reset - exit");
   202     //LOGSTRING("DProfilerIttSampler::Reset - exit");
   196 	this->ittSamplerImpl.Reset();
   203 	this->ittSamplerImpl.Reset();
   197     return KErrNone;
   204     return KErrNone;
   198 
   205 
   199 }
   206     }
   200 
   207 
   201 /*
   208 /*
   202  * DProfilerIttSampler::PostSample
   209  * DProfilerIttSampler::PostSample
   203  * 
   210  * 
   204  * Function for finishing sample
   211  * Function for finishing sample
   205  */
   212  */
   206 template <int BufferSize> 
   213 template <int BufferSize> 
   207 TInt DProfilerIttSampler<BufferSize>::PostSample()
   214 TInt DProfilerIttSampler<BufferSize>::PostSample()
   208 {
   215     {
   209 #ifdef ITT_EVENT_HANDLER
   216 #ifdef ITT_EVENT_HANDLER
   210     if(!ittSamplerImpl.iFirstSampleTaken)   // if we haven't read the initial state
   217     if(!ittSamplerImpl.iInitialSegsTaken || !ittSamplerImpl.iInitialLibsTaken)   // if we haven't read the initial state
   211     {
   218         {
   212 #endif
   219 #endif
   213         if(sampleNeeded)
   220         if(sampleNeeded)
   214         {
   221             {
   215             this->sampleNeeded = false;
   222             this->sampleNeeded = false;
   216             //LOGSTRING3("CProfilerIttSampler<%d>::PostSample - state %d",BufferSize,this->iSampleBuffer->GetBufferStatus());
   223             LOGSTRING3("DProfilerIttSampler<%d>::PostSample - state %d",BufferSize,this->iSampleBuffer->GetBufferStatus());
   217             //Kern::Printf("DProfilerIttSampler<%d>::PostSample - state %d",BufferSize,this->iSampleBuffer->GetBufferStatus());
       
   218     
   224     
   219             TInt length = this->ittSamplerImpl.SampleImpl(this->gppSamplerData->lastPcValue,
   225             TInt length = this->ittSamplerImpl.SampleImpl(this->gppSamplerData->lastPcValue,
   220                                                           this->gppSamplerData->sampleNumber);
   226                                                           this->gppSamplerData->sampleNumber);
   221             if(length != 0)
   227             if(length != 0)
   222             {		
   228                 {		
   223                 LOGSTRING("ITT sampler PostSample - starting to sample");
   229                 LOGSTRING("ITT sampler PostSample - starting to sample");
   224     
       
   225                 while(length > 0)
   230                 while(length > 0)
   226                 {
   231                     {
   227                     this->iSampleBuffer->AddSample(ittSamplerImpl.itt_sample,length);
   232                     this->iSampleBuffer->AddSample(ittSamplerImpl.itt_sample,length);
   228                     length = this->ittSamplerImpl.SampleImpl( this->gppSamplerData->lastPcValue,
   233                     length = this->ittSamplerImpl.SampleImpl( this->gppSamplerData->lastPcValue,
   229                                                           this->gppSamplerData->sampleNumber );	
   234                                                           this->gppSamplerData->sampleNumber );	
   230                     if(length == 0) 
   235                     if(length == 0) 
   231                     {
   236                         {
   232                         LOGSTRING("MEM sampler PostSample - all samples generated!");
   237                         LOGSTRING("ITT sampler PostSample - done for this round!");
       
   238                         }
   233                     }
   239                     }
       
   240                 LOGSTRING("ITT sampler PostSample - finished sampling");
   234                 }
   241                 }
   235                 LOGSTRING("ITT sampler PostSample - finished sampling");
       
   236             }
       
   237             this->sampleInProgress = false;
   242             this->sampleInProgress = false;
   238         }
   243             }
   239 #ifdef ITT_EVENT_HANDLER
   244 #ifdef ITT_EVENT_HANDLER
   240     }   
   245         }
   241         if(!iEventHandler->Tracking())
   246 #endif
   242             {
       
   243             iEventHandler->Start();
       
   244             Kern::Printf("DProfilerITTSampler<%d>::PostSample - ITT handler started",BufferSize);
       
   245             }
       
   246 
       
   247 #endif    
       
   248 	
   247 	
   249     LOGSTRING2("ITT sampler PostSample - finished sampling, time: %d", gppSamplerData->sampleNumber);
   248     LOGSTRING2("ITT sampler PostSample - finished sampling, time: %d", gppSamplerData->sampleNumber);
   250     
   249     
   251 	// finally perform superclass postsample
   250 	// finally perform superclass postsample
   252 	TInt i(this->DProfilerGenericSampler<BufferSize>::PostSample());
   251 	TInt i(this->DProfilerGenericSampler<BufferSize>::PostSample());
       
   252 #ifdef ITT_EVENT_HANDLER	
       
   253 	// notify event handler
       
   254 	if(iEventHandler)
       
   255 	    {
       
   256 	    iEventHandler->SampleHandled();
       
   257 	    }
       
   258 #endif
   253 	return i;
   259 	return i;
   254 }
   260     }
   255 
   261 
   256 /*
   262 /*
   257  *  DProfilerIttSampler::PostSampleNeeded()
   263  *  DProfilerIttSampler::PostSampleNeeded()
   258  *  
   264  *  
   259  *  Function for deciding if sample handling is needed 
   265  *  Function for deciding if sample handling is needed 
   260  */
   266  */
   261 template <int BufferSize> 
   267 template <int BufferSize> 
   262 TBool DProfilerIttSampler<BufferSize>::PostSampleNeeded()
   268 TBool DProfilerIttSampler<BufferSize>::PostSampleNeeded()
   263 {
   269     {
   264 	LOGSTRING3("CProfilerIttSampler<%d>::PostSampleNeeded - state %d",BufferSize,this->iSampleBuffer->GetBufferStatus());
   270 	LOGSTRING3("DProfilerIttSampler<%d>::PostSampleNeeded - buffer status %d",BufferSize,this->iSampleBuffer->GetBufferStatus());
   265 
   271 
   266 	TUint32 status = this->iSampleBuffer->iBufferStatus;
   272 	TUint32 status(this->iSampleBuffer->GetBufferStatus());
   267 
   273 #ifdef ITT_EVENT_HANDLER
   268 	if(status == DProfilerSampleBuffer::BufferCopyAsap || status == DProfilerSampleBuffer::BufferFull || this->sampleNeeded == true)
   274     if(iEventHandler)
   269 	{
   275         {
       
   276         if(iEventHandler->Tracking())
       
   277             {
       
   278             this->sampleNeeded = iEventHandler->SampleNeeded();
       
   279             }
       
   280         }
       
   281 #endif    
       
   282 	if(status == DProfilerSampleBuffer::BufferCopyAsap || 
       
   283 	        status == DProfilerSampleBuffer::BufferFull || 
       
   284 	        this->sampleNeeded == true)
       
   285 	    {
       
   286 	    LOGSTRING2("DProfilerIttSampler<%d>::PostSampleNeeded - buffer needs emptying or sample is needed",BufferSize);
   270 		return true;
   287 		return true;
   271 	}
   288 	    }
   272 	
       
   273 	return false;
   289 	return false;
   274 }
   290     }
   275 
   291 
   276 /*
   292 /*
   277  * DProfilerIttSampler::Sample
   293  * DProfilerIttSampler::Sample
   278  * 
   294  * 
   279  * Function for initiating sampling
   295  * Function for initiating sampling
   280  */
   296  */
   281 template <int BufferSize>
   297 template <int BufferSize>
   282 void DProfilerIttSampler<BufferSize>::Sample()
   298 void DProfilerIttSampler<BufferSize>::Sample(TAny* aPtr)
   283 {
   299     {
   284 	LOGSTRING2("CProfilerIttSampler<%d>::Sample",BufferSize);	
   300 	LOGSTRING2("DProfilerIttSampler<%d>::Sample - entry",BufferSize);	
   285 	
   301     if(iEventHandler)
   286 	//#ifdef ITT_TEST
   302         {
   287 	LOGSTRING2("CProfilerIttSampler<%d>::Sample",BufferSize);	
   303         if(!iEventHandler->Tracking())
   288 	
   304             {
   289 	if(ittSamplerImpl.SampleNeeded(this->gppSamplerData->sampleNumber) && this->sampleInProgress == false) 
   305             if(ittSamplerImpl.SampleNeeded(this->gppSamplerData->sampleNumber) && this->sampleInProgress == false) 
   290 	{
   306                 {
   291 		this->sampleInProgress = true;
   307                 this->sampleInProgress = true;
   292 		this->sampleNeeded = true;
   308                 this->sampleNeeded = true;
   293 
   309         
   294 		LOGSTRING2("CProfilerIttSampler<%d>::Sample - sample needed",BufferSize);	
   310                 LOGSTRING2("DProfilerIttSampler<%d>::Sample - sample needed 1",BufferSize);	
   295 	}	
   311                 }
   296 #ifdef ITT_EVENT_HANDLER
   312             
   297     // call this to increase the time stamp
   313             if(ittSamplerImpl.iInitialSegsTaken && ittSamplerImpl.iInitialLibsTaken) 
   298     else if(iEventHandler->SampleNeeded())
   314                 {
   299         {
   315                 iEventHandler->Start();
   300         // set the flag for post sampling
   316                 }
   301         this->sampleNeeded = true;
   317             }
   302         }
   318         }
   303 #endif
   319     else
   304 
   320         {
   305 	LOGSTRING2("CProfilerIttSampler<%d>::Sample",BufferSize);
   321         if(ittSamplerImpl.SampleNeeded(this->gppSamplerData->sampleNumber) && this->sampleInProgress == false) 
       
   322             {
       
   323             this->sampleInProgress = true;
       
   324             this->sampleNeeded = true;
       
   325     
       
   326             LOGSTRING2("DProfilerIttSampler<%d>::Sample - sample needed 2",BufferSize);   
       
   327             }
       
   328         }
       
   329 
       
   330 	LOGSTRING2("DProfilerIttSampler<%d>::Sample - exit",BufferSize);
   306 	return;
   331 	return;
   307 }
   332     }
   308 
   333 
   309 /*
   334 /*
   310  * Destructor
   335  * Destructor
   311  */
   336  */
   312 template <int BufferSize>
   337 template <int BufferSize>
   313 DProfilerIttSampler<BufferSize>::~DProfilerIttSampler()
   338 DProfilerIttSampler<BufferSize>::~DProfilerIttSampler()
   314 {
   339     {
   315 	LOGSTRING2("CProfilerIttSampler<%d>::~CProfilerIttSampler",BufferSize);
   340 	LOGSTRING2("DProfilerIttSampler<%d>::~DProfilerIttSampler",BufferSize);
   316 #ifdef ITT_EVENT_HANDLER
   341 #ifdef ITT_EVENT_HANDLER
   317      if(iEventHandler)
   342      if(iEventHandler)
   318          {
   343          {
   319          // stop previous sampling if still running
   344          // stop previous sampling if still running
   320          iEventHandler->Stop();
   345          iEventHandler->Stop();
   321          iEventHandler->Close();
   346          iEventHandler->Close();
   322          iEventHandler = NULL;
   347          iEventHandler = NULL;
   323          }
   348          }
   324 #endif
   349 #endif
   325 }
   350     }
   326 #endif
   351 #endif
   327 // end of file
   352 // end of file