sysanadatacapture/piprofiler/piprofiler/plugins/GeneralsPlugin/inc/MemSamplerImpl.h
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 #ifndef PROFILER_MEM_SAMPLER_H
       
    20 #define PROFILER_MEM_SAMPLER_H
       
    21 
       
    22 #include "GeneralsConfig.h"
       
    23 
       
    24 #include <kern_priv.h>
       
    25 
       
    26 #include <piprofiler/ProfilerGenericClassesKrn.h>
       
    27 #include <piprofiler/ProfilerTraces.h>
       
    28 #include "GppSamplerImpl.h"
       
    29 
       
    30 // defines the maximum thread amount that is assumed to
       
    31 // be possible with MEM trace
       
    32 #define PROFILER_MAX_THREAD_AMOUNT	512
       
    33 
       
    34 /*
       
    35  *	
       
    36  *	MEM sampler definition
       
    37  *	
       
    38  */
       
    39 
       
    40 class MemSamplerImpl
       
    41 {
       
    42 public:
       
    43 	enum EProcessingState
       
    44 	{
       
    45 		EStartingToProcess,
       
    46 		EProcessingNames,
       
    47 		EProcessingData,
       
    48 		ENothingToProcess
       
    49 	};
       
    50 
       
    51 	MemSamplerImpl();
       
    52 	~MemSamplerImpl();
       
    53 
       
    54 	TInt	CreateFirstSample();
       
    55 	TInt	SampleImpl();
       
    56 	TBool	SampleNeeded();
       
    57 	void	Reset();
       
    58 	TInt	ProcessChunks();
       
    59 	TInt    ProcessThreads();
       
    60     TInt    GatherChunks();
       
    61     TInt    GatherThreads();
       
    62 	
       
    63 	TInt	EncodeChunkData(DChunk& chunk);
       
    64 	TInt	EncodeChunkName(DChunk& chunk);
       
    65 	TInt	EncodeChunkData(DThread& thread);
       
    66 	TInt	EncodeChunkName(DThread& thread);	
       
    67 
       
    68 	TInt 	EncodeTotalMemoryName();
       
    69 	TInt 	EncodeTotalMemory();
       
    70 	
       
    71 	TInt	EncodeNameCode();
       
    72 	TInt	EncodeDataCode();
       
    73 
       
    74 	DChunk*		heapChunksToSample[PROFILER_MAX_THREAD_AMOUNT];
       
    75 	DChunk*		heapChunkNamesToReport[PROFILER_MAX_THREAD_AMOUNT];
       
    76 	TInt		iCount;
       
    77 	TInt		iChunkCount;
       
    78 	TInt		iNewChunkCount;
       
    79 	TBuf8<0x50> name;
       
    80 	DThread*	threadsToSample[PROFILER_MAX_THREAD_AMOUNT];
       
    81 	DThread*	threadNamesToReport[PROFILER_MAX_THREAD_AMOUNT];
       
    82 	TInt		iThreadCount;
       
    83 	TInt		iNewThreadCount;
       
    84 
       
    85 	TInt		iChunksProcessing;
       
    86     TInt        iThreadsProcessing;
       
    87 	TInt		iMemSamplingPeriod;
       
    88 	TInt		iMemSamplingPeriodDiv2;
       
    89 	TBool		iSampleThreads;
       
    90 	TBool       iTimeToSample;
       
    91 	
       
    92 	TBool 		iTotalMemoryOk;
       
    93 	TBool		iTotalMemoryNameOk;
       
    94 
       
    95 	TUint8		sample[257];
       
    96 	TPtr8		sampleDescriptor;
       
    97 };
       
    98 
       
    99 template <int BufferSize>
       
   100 class DProfilerMemSampler : public DProfilerGenericSampler<BufferSize>
       
   101 {
       
   102 public:
       
   103 	DProfilerMemSampler(struct TProfilerGppSamplerData*, TInt id);
       
   104 	~DProfilerMemSampler();
       
   105 
       
   106 	void	Sample();
       
   107 	TInt	Reset(DProfilerSampleStream* aStream, TUint32 aSyncOffset);
       
   108 	TInt	PostSample();
       
   109 	TBool	PostSampleNeeded();
       
   110 	
       
   111 private:
       
   112 	MemSamplerImpl			           memSamplerImpl;
       
   113 	struct TProfilerGppSamplerData*    gppSamplerData;
       
   114 	TBool                              sampleNeeded;
       
   115 };
       
   116 
       
   117 /*
       
   118  *	
       
   119  *	MEM sampler implementation
       
   120  *	
       
   121  */
       
   122 
       
   123 template <int BufferSize>
       
   124 DProfilerMemSampler<BufferSize>::DProfilerMemSampler(struct TProfilerGppSamplerData* gppSamplerDataIn, TInt id) :
       
   125 	DProfilerGenericSampler<BufferSize>(PROFILER_MEM_SAMPLER_ID)
       
   126     {
       
   127 	LOGSTRING2("CProfilerMemSampler<%d>::CProfilerMemSampler",BufferSize);
       
   128 	this->gppSamplerData = gppSamplerDataIn;
       
   129 	this->iSamplingPeriod = 3000;	// set default setting
       
   130     }
       
   131 
       
   132 template <int BufferSize>
       
   133 TInt DProfilerMemSampler<BufferSize>::Reset(DProfilerSampleStream* aStream, TUint32 aSyncOffset)
       
   134     {
       
   135 	LOGSTRING2("CProfilerMemSampler<%d>::Reset - calling superclass reset",BufferSize);
       
   136 	DProfilerGenericSampler<BufferSize>::Reset(aStream);
       
   137 	LOGSTRING2("CProfilerMemSampler<%d>::Reset - called superclass reset",BufferSize);
       
   138 	memSamplerImpl.Reset();
       
   139 
       
   140 	this->memSamplerImpl.iMemSamplingPeriod = this->iSamplingPeriod;
       
   141 	this->memSamplerImpl.iMemSamplingPeriodDiv2 = (TInt)(this->memSamplerImpl.iMemSamplingPeriod / 2);
       
   142 	
       
   143 	LOGSTRING3("CProfilerMemSampler<%d>::Reset - set mem sampling period to",
       
   144 							BufferSize,this->memSamplerImpl.iMemSamplingPeriod);
       
   145 
       
   146 	TInt length = memSamplerImpl.CreateFirstSample();
       
   147 	this->iSampleBuffer->AddSample(memSamplerImpl.sample,length);
       
   148 	
       
   149 	this->sampleNeeded = false;
       
   150 
       
   151 	return KErrNone;
       
   152     }
       
   153 
       
   154 template <int BufferSize> 
       
   155 TInt DProfilerMemSampler<BufferSize>::PostSample()
       
   156     {
       
   157     this->sampleNeeded = false;
       
   158 
       
   159     LOGSTRING3("CProfilerMemSampler<%d>::PostSample - state %d",BufferSize,this->sampleBuffer->GetBufferStatus());
       
   160     
       
   161     TInt interruptLevel = NKern::DisableInterrupts(0);
       
   162     
       
   163     // first collect chunk data
       
   164     TInt length = this->memSamplerImpl.SampleImpl();
       
   165     if(length != 0)
       
   166         {
       
   167         LOGSTRING("MEM sampler PostSample - starting to sample");
       
   168 
       
   169         // then, encode the sample to the buffer
       
   170         while(length > 0)
       
   171             {
       
   172             this->iSampleBuffer->AddSample(memSamplerImpl.sample,length);
       
   173             length = this->memSamplerImpl.SampleImpl();
       
   174             // indicate that the whole MEM sample ends by having a 0x00 in the end
       
   175             if(length == 0)
       
   176                 {
       
   177                 TUint8 number = 0;
       
   178                 LOGSTRING("MEM sampler PostSample - all samples generated!");
       
   179                 
       
   180                 this->iSampleBuffer->AddSample(&number,1);
       
   181                 LOGSTRING("MEM sampler PostSample - end mark added");
       
   182                 }
       
   183             }
       
   184         }
       
   185     
       
   186     LOGSTRING("MEM sampler PostSample - finished sampling");
       
   187     
       
   188     NKern::RestoreInterrupts(interruptLevel);
       
   189    
       
   190     // finally perform superclass postsample
       
   191 	TInt i = this->DProfilerGenericSampler<BufferSize>::PostSample();
       
   192 	return i;
       
   193 
       
   194     }
       
   195 
       
   196 template <int BufferSize> 
       
   197 TBool DProfilerMemSampler<BufferSize>::PostSampleNeeded()
       
   198     {
       
   199 	LOGSTRING3("CProfilerMemSampler<%d>::PostSampleNeeded - state %d",BufferSize,this->sampleBuffer->GetBufferStatus());
       
   200 
       
   201 	TUint32 status = this->iSampleBuffer->iBufferStatus;
       
   202 
       
   203 	if(status == DProfilerSampleBuffer::BufferCopyAsap || status == DProfilerSampleBuffer::BufferFull || this->sampleNeeded == true)
       
   204 	    {
       
   205 		return true;
       
   206 	    }
       
   207 	
       
   208 	return false;
       
   209     }
       
   210 
       
   211 template <int BufferSize>
       
   212 void DProfilerMemSampler<BufferSize>::Sample()
       
   213     {
       
   214 
       
   215 	LOGSTRING2("CProfilerMemSampler<%d>::Sample",BufferSize);	
       
   216 	
       
   217 	if(memSamplerImpl.SampleNeeded()) 
       
   218 	    {
       
   219 		this->sampleNeeded = true;
       
   220 
       
   221 		LOGSTRING2("CProfilerMemSampler<%d>::Sample - sample needed",BufferSize);	
       
   222 		
       
   223 		// start the MEM sample with the sample time
       
   224 		TUint8 number = 4;    // mem sampler id
       
   225 		this->iSampleBuffer->AddSample(&number,1);
       
   226 		this->iSampleBuffer->AddSample((TUint8*)&(gppSamplerData->sampleNumber),4);
       
   227 
       
   228 		// leave the rest of the processing for PostSample()
       
   229 	    }	
       
   230 
       
   231 	LOGSTRING2("CProfilerMemSampler<%d>::Sample",BufferSize);
       
   232 	return;
       
   233     }
       
   234 
       
   235 template <int BufferSize>
       
   236 DProfilerMemSampler<BufferSize>::~DProfilerMemSampler()
       
   237     {
       
   238 	LOGSTRING2("CProfilerMemSampler<%d>::~CProfilerMemSampler",BufferSize);		
       
   239     }
       
   240 #endif