20
|
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 |
#include "MemoryEventHandler.h"
|
|
30 |
#include <e32btrace.h>
|
|
31 |
|
|
32 |
|
|
33 |
// constants
|
|
34 |
// defines the maximum thread amount that is assumed to
|
|
35 |
// be possible with MEM trace
|
|
36 |
const TInt KSampleBufferSize = 257;
|
|
37 |
const TInt KProfilerMaxThreadsAmount = 512;
|
|
38 |
const TInt KProfilerMaxChunksAmount = 1024;
|
|
39 |
const TInt KProfilerMaxLibrariesAmount = 1024;
|
|
40 |
const TInt KProfilerTotalMemorySamplePeriod = 100;
|
|
41 |
|
|
42 |
// flags
|
62
|
43 |
//#ifndef __SMP__
|
20
|
44 |
#define MEM_EVENT_HANDLER
|
|
45 |
//#define MEM_EVENT_HANDLER_LIBRARY_EVENTS
|
62
|
46 |
//#endif
|
20
|
47 |
|
|
48 |
/*
|
|
49 |
*
|
|
50 |
* MEM sampler definition
|
|
51 |
*
|
|
52 |
*/
|
|
53 |
|
|
54 |
class DMemoryEventHandler;
|
|
55 |
|
|
56 |
class DMemSamplerImpl //: public DBase
|
|
57 |
{
|
|
58 |
public:
|
|
59 |
enum EProcessingState
|
|
60 |
{
|
|
61 |
EStartingToProcess,
|
|
62 |
EProcessingNames,
|
|
63 |
EProcessingData,
|
|
64 |
ENothingToProcess
|
|
65 |
};
|
62
|
66 |
|
20
|
67 |
enum ESampleType
|
|
68 |
{
|
|
69 |
ESampleChunks,
|
|
70 |
ESampleThreads,
|
|
71 |
ESampleLibraries
|
|
72 |
};
|
|
73 |
|
|
74 |
DMemSamplerImpl();
|
|
75 |
~DMemSamplerImpl();
|
|
76 |
|
|
77 |
TInt CreateFirstSample();
|
62
|
78 |
TInt SampleImpl(TUint32 sampleNum);
|
|
79 |
TBool SampleNeeded(TUint32 sampleNum);
|
20
|
80 |
void Reset();
|
|
81 |
TInt ProcessChunks();
|
|
82 |
TInt ProcessThreads();
|
|
83 |
TInt GatherChunks();
|
|
84 |
TInt GatherThreads();
|
|
85 |
|
|
86 |
#ifdef MEM_EVENT_HANDLER_LIBRARY_EVENTS
|
|
87 |
TInt GatherLibraries();
|
|
88 |
TInt ProcessLibraries();
|
|
89 |
#endif
|
|
90 |
|
|
91 |
TInt EncodeChunkData(DChunk& chunk);
|
|
92 |
TInt EncodeChunkName(DChunk& chunk);
|
|
93 |
TInt EncodeChunkData(DThread& thread);
|
|
94 |
TInt EncodeChunkName(DThread& thread);
|
|
95 |
#ifdef MEM_EVENT_HANDLER_LIBRARY_EVENTS
|
|
96 |
TInt EncodeChunkData(DLibrary& library);
|
|
97 |
TInt EncodeChunkName(DLibrary& library);
|
|
98 |
#endif
|
|
99 |
|
|
100 |
TInt EncodeTotalMemoryName();
|
|
101 |
TInt EncodeTotalMemory();
|
|
102 |
|
|
103 |
TInt EncodeNameCode();
|
|
104 |
TInt EncodeDataCode();
|
|
105 |
|
|
106 |
DChunk* heapChunksToSample[KProfilerMaxChunksAmount];
|
|
107 |
DChunk* heapChunkNamesToReport[KProfilerMaxChunksAmount];
|
62
|
108 |
TUint32 iCount;
|
20
|
109 |
TInt iChunkCount;
|
|
110 |
TInt iNewChunkCount;
|
|
111 |
TBuf8<0x50> name;
|
|
112 |
DThread* threadsToSample[KProfilerMaxThreadsAmount];
|
|
113 |
DThread* threadNamesToReport[KProfilerMaxThreadsAmount];
|
|
114 |
TInt iThreadCount;
|
|
115 |
TInt iNewThreadCount;
|
62
|
116 |
|
|
117 |
TInt iHandledThreads;
|
|
118 |
TInt iHandledChunks;
|
|
119 |
TInt iHandledLibs;
|
|
120 |
|
20
|
121 |
#ifdef MEM_EVENT_HANDLER_LIBRARY_EVENTS
|
|
122 |
DLibrary* librariesToSample[KProfilerMaxLibrariesAmount];
|
|
123 |
DLibrary* libraryNamesToReport[KProfilerMaxLibrariesAmount];
|
|
124 |
TInt iLibraryCount;
|
|
125 |
TInt iNewLibraryCount;
|
|
126 |
TInt iLibrariesProcessing;
|
|
127 |
#endif
|
|
128 |
|
|
129 |
TInt iChunksProcessing;
|
|
130 |
TInt iThreadsProcessing;
|
|
131 |
TInt iMemSamplingPeriod;
|
62
|
132 |
//TInt iMemSamplingPeriodDiv2;
|
|
133 |
//TInt iMemSamplingPeriodDiv3;
|
20
|
134 |
|
62
|
135 |
|
20
|
136 |
ESampleType iSampleType;
|
62
|
137 |
|
20
|
138 |
TBool iTimeToSample;
|
|
139 |
TBool iTotalMemoryOk;
|
|
140 |
TBool iTotalMemoryNameOk;
|
|
141 |
|
|
142 |
TUint8 sample[KSampleBufferSize];
|
|
143 |
TPtr8 sampleDescriptor;
|
62
|
144 |
TInt iThreadsHandled;
|
|
145 |
TInt iChunksHandled;
|
20
|
146 |
#ifdef MEM_EVENT_HANDLER
|
|
147 |
TBool iChunksGathered;
|
|
148 |
TBool iThreadsGathered;
|
|
149 |
#ifdef MEM_EVENT_HANDLER_LIBRARY_EVENTS
|
|
150 |
TBool iLibrariesGathered;
|
|
151 |
#endif
|
|
152 |
#endif
|
|
153 |
};
|
|
154 |
|
|
155 |
|
|
156 |
template <int BufferSize>
|
|
157 |
class DProfilerMemSampler : public DProfilerGenericSampler<BufferSize>
|
|
158 |
{
|
|
159 |
public:
|
|
160 |
DProfilerMemSampler(struct TProfilerGppSamplerData*, TInt id);
|
|
161 |
~DProfilerMemSampler();
|
|
162 |
|
62
|
163 |
void Sample(TAny* aPtr);
|
20
|
164 |
TInt Reset(DProfilerSampleStream* aStream, TUint32 aSyncOffset);
|
|
165 |
TInt PostSample();
|
|
166 |
TBool PostSampleNeeded();
|
|
167 |
|
|
168 |
private:
|
|
169 |
#ifdef MEM_EVENT_HANDLER
|
|
170 |
DMemoryEventHandler* iEventHandler;
|
|
171 |
#endif
|
|
172 |
DMemSamplerImpl memSamplerImpl;
|
|
173 |
struct TProfilerGppSamplerData* gppSamplerData;
|
|
174 |
TBool sampleNeeded;
|
62
|
175 |
TUint32 iSyncOffset;
|
20
|
176 |
};
|
|
177 |
|
|
178 |
/*
|
|
179 |
*
|
|
180 |
* MEM sampler implementation
|
|
181 |
*
|
|
182 |
*/
|
|
183 |
|
|
184 |
template <int BufferSize>
|
|
185 |
DProfilerMemSampler<BufferSize>::DProfilerMemSampler(struct TProfilerGppSamplerData* gppSamplerDataIn, TInt id) :
|
|
186 |
DProfilerGenericSampler<BufferSize>(PROFILER_MEM_SAMPLER_ID)
|
|
187 |
{
|
|
188 |
LOGSTRING2("DProfilerMemSampler<%d>::CProfilerMemSampler",BufferSize);
|
|
189 |
this->gppSamplerData = gppSamplerDataIn;
|
62
|
190 |
#ifndef MEM_EVENT_HANDLER
|
20
|
191 |
this->iSamplingPeriod = 3000; // set default setting
|
62
|
192 |
#endif
|
|
193 |
iSyncOffset = 0;
|
20
|
194 |
}
|
|
195 |
|
|
196 |
template <int BufferSize>
|
|
197 |
TInt DProfilerMemSampler<BufferSize>::Reset(DProfilerSampleStream* aStream, TUint32 aSyncOffset)
|
|
198 |
{
|
62
|
199 |
iSyncOffset = aSyncOffset;
|
20
|
200 |
// check if reset called in stop (by driver)
|
62
|
201 |
if(iSyncOffset != KStateSamplingEnding)
|
20
|
202 |
{
|
|
203 |
DProfilerGenericSampler<BufferSize>::Reset(aStream);
|
|
204 |
memSamplerImpl.Reset();
|
|
205 |
|
|
206 |
#ifdef MEM_EVENT_HANDLER
|
|
207 |
// reset member variables
|
|
208 |
this->memSamplerImpl.iThreadsGathered = false;
|
|
209 |
this->memSamplerImpl.iChunksGathered = false;
|
|
210 |
#ifdef MEM_EVENT_HANDLER_LIBRARY_EVENTS
|
|
211 |
this->memSamplerImpl.iLibrariesGathered = false;
|
|
212 |
#endif
|
|
213 |
|
|
214 |
// memory event handler
|
|
215 |
if(iEventHandler)
|
|
216 |
{
|
|
217 |
// stop previous sampling if still running
|
|
218 |
iEventHandler->Stop();
|
|
219 |
}
|
62
|
220 |
else
|
|
221 |
{
|
|
222 |
iEventHandler = new DMemoryEventHandler(this->iSampleBuffer, this->gppSamplerData);
|
|
223 |
}
|
|
224 |
|
20
|
225 |
if(iEventHandler)
|
|
226 |
{
|
|
227 |
TInt err(iEventHandler->Create());
|
|
228 |
if(err != KErrNone)
|
|
229 |
{
|
|
230 |
Kern::Printf("Error in creation of DMemoryEventHandler, error %d", err);
|
|
231 |
return err;
|
|
232 |
}
|
|
233 |
}
|
|
234 |
else
|
|
235 |
{
|
|
236 |
Kern::Printf("Could not initiate DMemoryEventHandler");
|
|
237 |
return KErrGeneral;
|
|
238 |
}
|
|
239 |
|
|
240 |
// set first chunk&thread memory lookup at the 5 ms, should be enough
|
|
241 |
#ifdef MEM_EVENT_HANDLER_LIBRARY_EVENTS
|
62
|
242 |
this->memSamplerImpl.iMemSamplingPeriod = 10;
|
20
|
243 |
#else
|
|
244 |
this->memSamplerImpl.iMemSamplingPeriod = 10;
|
|
245 |
#endif
|
62
|
246 |
|
|
247 |
#else // ifdef mem event handler
|
20
|
248 |
this->memSamplerImpl.iMemSamplingPeriod = this->iSamplingPeriod;
|
|
249 |
#endif
|
62
|
250 |
// this->memSamplerImpl.iMemSamplingPeriodDiv2 = (TInt)(this->memSamplerImpl.iMemSamplingPeriod / 2);
|
|
251 |
//#ifdef MEM_EVENT_HANDLER_LIBRARY_EVENTS
|
|
252 |
// this->memSamplerImpl.iMemSamplingPeriodDiv3 = (TInt)(this->memSamplerImpl.iMemSamplingPeriod / 3);
|
|
253 |
//#endif
|
20
|
254 |
|
|
255 |
LOGSTRING3("CProfilerMemSampler<%d>::Reset - set mem sampling period to %d",
|
|
256 |
BufferSize,this->memSamplerImpl.iMemSamplingPeriod);
|
|
257 |
}
|
62
|
258 |
else // iSyncOffset == KStateSamplingEnding
|
20
|
259 |
{
|
|
260 |
LOGSTRING2("DProfilerMemSampler<%d>::Reset - reset in stop", BufferSize);
|
|
261 |
#ifdef MEM_EVENT_HANDLER
|
|
262 |
// destroy memory event handler
|
|
263 |
if(iEventHandler)
|
|
264 |
{
|
|
265 |
// stop previous sampling if still running
|
62
|
266 |
LOGSTRING("Stopping DMemoryEventHandler");
|
20
|
267 |
iEventHandler->Stop();
|
|
268 |
}
|
|
269 |
#endif
|
|
270 |
return KErrNone; // return if reset called in stop
|
|
271 |
}
|
|
272 |
|
|
273 |
// add MEM sample header
|
|
274 |
TInt length(memSamplerImpl.CreateFirstSample());
|
|
275 |
this->iSampleBuffer->AddSample(memSamplerImpl.sample,length);
|
|
276 |
|
62
|
277 |
TUint8 memSamplerId(4); // mem sampler id
|
|
278 |
this->iSampleBuffer->AddSample(&memSamplerId,1);
|
|
279 |
this->iSampleBuffer->AddSample((TUint8*)&(gppSamplerData->sampleNumber),4);
|
20
|
280 |
this->sampleNeeded = false;
|
|
281 |
LOGSTRING("DProfilerMemSampler::Reset - exit");
|
|
282 |
return KErrNone;
|
|
283 |
}
|
|
284 |
|
|
285 |
template <int BufferSize>
|
|
286 |
TInt DProfilerMemSampler<BufferSize>::PostSample()
|
|
287 |
{
|
|
288 |
this->sampleNeeded = false;
|
|
289 |
|
|
290 |
LOGSTRING3("DProfilerMemSampler<%d>::PostSample - state %d",BufferSize,this->iSampleBuffer->GetBufferStatus());
|
|
291 |
|
|
292 |
#ifdef MEM_EVENT_HANDLER
|
|
293 |
// check if all threads and chunks (and libraries) are gathered
|
|
294 |
#ifdef MEM_EVENT_HANDLER_LIBRARY_EVENTS
|
|
295 |
if(!memSamplerImpl.iThreadsGathered || !memSamplerImpl.iChunksGathered || !memSamplerImpl.iLibrariesGathered)
|
|
296 |
#else
|
|
297 |
if(!memSamplerImpl.iThreadsGathered || !memSamplerImpl.iChunksGathered)
|
|
298 |
#endif
|
|
299 |
{
|
|
300 |
#endif
|
|
301 |
// disable interrupts for checking the kernel containers (EChunk, EThread)
|
|
302 |
// TInt interruptLevel(NKern::DisableInterrupts(0));
|
|
303 |
// first collect chunk data
|
62
|
304 |
TInt length(this->memSamplerImpl.SampleImpl(this->gppSamplerData->sampleNumber));
|
|
305 |
LOGSTRING2("DProfilerMemSampler<>::PostSample - in post sample, clock %d", this->memSamplerImpl.iCount );
|
20
|
306 |
if(length != 0)
|
|
307 |
{
|
|
308 |
// then, encode the sample to the buffer until no further data available
|
|
309 |
while(length > 0)
|
|
310 |
{
|
62
|
311 |
TInt ret =this->iSampleBuffer->AddSample(memSamplerImpl.sample,length);
|
|
312 |
if (ret != 0)
|
|
313 |
{
|
|
314 |
Kern::Printf(("DProfilerMemSampler<>::PostSample() - add to sample buffer failed, loosing data, error = %d"),ret); }
|
|
315 |
length = this->memSamplerImpl.SampleImpl(this->gppSamplerData->sampleNumber);
|
|
316 |
LOGSTRING("DProfilerMemSampler<>::PostSample - in post sample again");
|
20
|
317 |
|
|
318 |
// indicate that the whole MEM sample ends by having a 0x00 in the end
|
|
319 |
if(length == 0)
|
|
320 |
{
|
62
|
321 |
TUint8 endMark(0);
|
20
|
322 |
LOGSTRING("MEM sampler PostSample - all samples generated!");
|
|
323 |
|
62
|
324 |
this->iSampleBuffer->AddSample(&endMark,1);
|
20
|
325 |
LOGSTRING2("MEM sampler PostSample - end mark added, time: %d", gppSamplerData->sampleNumber);
|
62
|
326 |
|
|
327 |
if (memSamplerImpl.iThreadsGathered && !memSamplerImpl.iChunksGathered)
|
|
328 |
{
|
|
329 |
LOGSTRING("MEM sampler PostSample - creating timestamp for chunks!");
|
|
330 |
// add start marker for chunks
|
|
331 |
TUint8 memSamplerId(4); // mem sampler id
|
|
332 |
this->iSampleBuffer->AddSample(&memSamplerId,1);
|
|
333 |
this->iSampleBuffer->AddSample((TUint8*)&(gppSamplerData->sampleNumber),4);
|
|
334 |
}
|
|
335 |
#ifdef MEM_EVENT_HANDLER_LIBRARY_EVENTS
|
|
336 |
if (memSamplerImpl.iThreadsGathered && memSamplerImpl.iChunksGathered)
|
|
337 |
{
|
|
338 |
// add start marker for chunks
|
|
339 |
TUint8 memSamplerId(4); // mem sampler id
|
|
340 |
this->iSampleBuffer->AddSample(&memSamplerId,1);
|
|
341 |
this->iSampleBuffer->AddSample((TUint8*)&(gppSamplerData->sampleNumber),4);
|
|
342 |
}
|
|
343 |
#endif
|
20
|
344 |
}
|
|
345 |
}
|
|
346 |
}
|
|
347 |
// restore interrupts and continue normal execution
|
|
348 |
// NKern::RestoreInterrupts(interruptLevel);
|
|
349 |
#ifdef MEM_EVENT_HANDLER
|
|
350 |
}
|
|
351 |
// check if all threads and chunks are gathered
|
|
352 |
#ifdef MEM_EVENT_HANDLER_LIBRARY_EVENTS
|
|
353 |
if(memSamplerImpl.iThreadsGathered && memSamplerImpl.iChunksGathered && memSamplerImpl.iLibrariesGathered)
|
|
354 |
#else
|
|
355 |
if(memSamplerImpl.iThreadsGathered && memSamplerImpl.iChunksGathered)
|
|
356 |
#endif
|
|
357 |
{
|
62
|
358 |
if(iSyncOffset != KStateSamplingEnding)
|
20
|
359 |
{
|
62
|
360 |
// start memory event tracking after checking the current memory status
|
|
361 |
if(!iEventHandler->Tracking())
|
|
362 |
{
|
|
363 |
LOGSTRING2("MEM sampler PostSample - Starting event handler timestamp : %x", gppSamplerData->sampleNumber);
|
|
364 |
memSamplerImpl.iTimeToSample = false;
|
|
365 |
iEventHandler->Start();
|
|
366 |
LOGSTRING2("DProfilerMemSampler<%d>::PostSample - memory event handler started",BufferSize);
|
|
367 |
}
|
20
|
368 |
}
|
|
369 |
}
|
|
370 |
#endif
|
|
371 |
|
|
372 |
LOGSTRING2("MEM sampler PostSample - finished sampling, time: %d", gppSamplerData->sampleNumber);
|
|
373 |
|
|
374 |
// finally perform superclass postsample
|
|
375 |
TInt i(this->DProfilerGenericSampler<BufferSize>::PostSample());
|
62
|
376 |
// notify event handler
|
|
377 |
//iEventHandler->SampleHandled();
|
20
|
378 |
return i;
|
|
379 |
}
|
|
380 |
|
|
381 |
template <int BufferSize>
|
|
382 |
TBool DProfilerMemSampler<BufferSize>::PostSampleNeeded()
|
|
383 |
{
|
|
384 |
LOGSTRING3("DProfilerMemSampler<%d>::PostSampleNeeded - state %d",BufferSize,this->iSampleBuffer->GetBufferStatus());
|
|
385 |
|
62
|
386 |
TUint32 status(this->iSampleBuffer->GetBufferStatus());
|
|
387 |
if(iEventHandler)
|
|
388 |
{
|
|
389 |
if(iEventHandler->Tracking())
|
|
390 |
{
|
|
391 |
this->sampleNeeded = iEventHandler->SampleNeeded();
|
|
392 |
}
|
|
393 |
}
|
|
394 |
if(status == DProfilerSampleBuffer::BufferCopyAsap || status == DProfilerSampleBuffer::BufferFull || this->sampleNeeded == true)
|
|
395 |
{
|
|
396 |
return true;
|
|
397 |
}
|
|
398 |
|
|
399 |
return false;
|
20
|
400 |
}
|
|
401 |
|
|
402 |
template <int BufferSize>
|
62
|
403 |
void DProfilerMemSampler<BufferSize>::Sample(TAny* aPtr)
|
20
|
404 |
{
|
62
|
405 |
LOGSTRING3("DProfilerMemSampler<%d>::Sample, time %d",BufferSize, this->memSamplerImpl.iCount);
|
|
406 |
if(iEventHandler)
|
|
407 |
{
|
|
408 |
if(!iEventHandler->Tracking())
|
|
409 |
{
|
|
410 |
// check if sample is needed, i.e. the sampling interval is met
|
|
411 |
if(memSamplerImpl.SampleNeeded(gppSamplerData->sampleNumber))
|
|
412 |
{
|
|
413 |
// set the flag for post sampling
|
|
414 |
this->sampleNeeded = true;
|
|
415 |
/*
|
|
416 |
LOGSTRING2("timestamp : 0x%04x", gppSamplerData->sampleNumber);
|
|
417 |
// start the MEM sample with the sample time
|
|
418 |
TUint8 memSamplerId(4); // mem sampler id
|
|
419 |
this->iSampleBuffer->AddSample(&memSamplerId,1);
|
|
420 |
this->iSampleBuffer->AddSample((TUint8*)&(gppSamplerData->sampleNumber),4);
|
|
421 |
*/
|
|
422 |
// leave the rest of the processing for PostSample()
|
|
423 |
}
|
|
424 |
}
|
|
425 |
//#ifdef MEM_EVENT_HANDLER
|
|
426 |
// // call this to increase the time stamp
|
|
427 |
// if(iEventHandler->SampleNeeded())
|
20
|
428 |
// {
|
62
|
429 |
// // set the flag for post sampling
|
|
430 |
// this->sampleNeeded = true;
|
20
|
431 |
// }
|
62
|
432 |
//#endif
|
|
433 |
}
|
20
|
434 |
return;
|
|
435 |
}
|
|
436 |
|
|
437 |
template <int BufferSize>
|
|
438 |
DProfilerMemSampler<BufferSize>::~DProfilerMemSampler()
|
|
439 |
{
|
|
440 |
LOGSTRING2("CProfilerMemSampler<%d>::~CProfilerMemSampler",BufferSize);
|
|
441 |
#ifdef MEM_EVENT_HANDLER
|
|
442 |
// memory event handler
|
|
443 |
if(iEventHandler)
|
|
444 |
{
|
|
445 |
// stop previous sampling if still running
|
62
|
446 |
LOGSTRING("Stopping DMemoryEventHandler");
|
20
|
447 |
iEventHandler->Stop();
|
|
448 |
iEventHandler->Close();
|
|
449 |
iEventHandler = NULL;
|
|
450 |
}
|
|
451 |
#endif
|
|
452 |
}
|
|
453 |
#endif
|