51
|
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 |
#include <piprofiler/ProfilerVersion.h>
|
|
20 |
#include <piprofiler/ProfilerTraces.h>
|
|
21 |
|
|
22 |
#include <kern_priv.h>
|
|
23 |
#include <plat_priv.h>
|
|
24 |
|
|
25 |
#include "MemSamplerImpl.h"
|
|
26 |
|
|
27 |
// for testing precise stack utilization tracing...
|
|
28 |
// crashes at the moment
|
|
29 |
|
|
30 |
#include <nk_cpu.h>
|
|
31 |
|
|
32 |
#if !defined(__NKERN_H__)
|
|
33 |
#include <nkern.h>
|
|
34 |
#endif
|
|
35 |
|
|
36 |
#define TAG(obj) (*(TUint32*)&(obj->iAsyncDeleteNext))
|
|
37 |
#define PROFILER_CHUNK_MARK ((TUint32)0x00001000)
|
|
38 |
#define PROFILER_MEM_THREAD_MARK ((TUint32)0x00000001)
|
|
39 |
#define PROFILER_LIBRARY_MARK ((TUint32)0x10000000)
|
|
40 |
#define PROFILER_MEM_THREAD_UNMARK ~PROFILER_MEM_THREAD_MARK
|
|
41 |
|
|
42 |
#ifdef MEM_EVENT_HANDLER
|
|
43 |
#ifdef MEM_EVENT_HANDLER_LIBRARY_EVENTS
|
|
44 |
_LIT8(KMemVersion,"2.03");
|
|
45 |
#else
|
|
46 |
_LIT8(KMemVersion, "2.02");
|
|
47 |
#endif
|
|
48 |
#else
|
|
49 |
_LIT8(KMemVersion, "1.56");
|
|
50 |
#endif
|
|
51 |
|
|
52 |
DMemSamplerImpl::DMemSamplerImpl() :
|
|
53 |
sampleDescriptor(&(this->sample[1]),0,256)
|
|
54 |
{
|
|
55 |
LOGSTRING("MemSamplerImpl::MemSamplerImpl() - konstruktori");
|
|
56 |
|
|
57 |
iCount = 0;
|
|
58 |
|
|
59 |
iSampleType = ESampleThreads;
|
62
|
60 |
//iSampleType = ESampleChunks;
|
|
61 |
|
|
62 |
iTimeToSample = true;
|
51
|
63 |
iTotalMemoryOk = false;
|
|
64 |
iTotalMemoryNameOk = false;
|
|
65 |
|
|
66 |
iNewChunkCount = 0;
|
|
67 |
iChunkCount = 0;
|
|
68 |
iChunksProcessing = ENothingToProcess;
|
|
69 |
iThreadsProcessing = ENothingToProcess;
|
|
70 |
|
62
|
71 |
iChunksGathered = false;
|
|
72 |
iThreadsGathered = false;
|
|
73 |
|
51
|
74 |
iNewThreadCount = 0;
|
|
75 |
iThreadCount = 0;
|
|
76 |
|
|
77 |
// reset data structures
|
|
78 |
for(TInt i(0);i<KProfilerMaxChunksAmount;i++)
|
|
79 |
{
|
|
80 |
// heap chunks
|
|
81 |
this->heapChunksToSample[i] = 0;
|
|
82 |
this->heapChunkNamesToReport[i] = 0;
|
|
83 |
}
|
|
84 |
|
|
85 |
for(TInt i(0);i<KProfilerMaxThreadsAmount;i++)
|
|
86 |
{
|
|
87 |
// threads
|
|
88 |
this->threadsToSample[i] = 0;
|
|
89 |
this->threadNamesToReport[i] = 0;
|
|
90 |
}
|
|
91 |
|
|
92 |
#ifdef MEM_EVENT_HANDLER_LIBRARY_EVENTS
|
|
93 |
iLibrariesProcessing = ENothingToProcess;
|
|
94 |
iNewLibraryCount = 0;
|
|
95 |
iLibraryCount = 0;
|
|
96 |
|
|
97 |
for(TInt i(0); i<KProfilerMaxLibrariesAmount; i++)
|
|
98 |
{
|
|
99 |
// libraries
|
|
100 |
this->librariesToSample[i] = 0;
|
|
101 |
this->libraryNamesToReport[i] = 0;
|
|
102 |
}
|
|
103 |
#endif
|
|
104 |
|
|
105 |
}
|
|
106 |
|
|
107 |
DMemSamplerImpl::~DMemSamplerImpl()
|
|
108 |
{
|
|
109 |
|
|
110 |
}
|
|
111 |
|
|
112 |
TInt DMemSamplerImpl::CreateFirstSample()
|
|
113 |
{
|
|
114 |
LOGSTRING("MemSamplerImpl::CreateFirstSample - entry");
|
|
115 |
|
|
116 |
this->sampleDescriptor.Zero();
|
|
117 |
this->sampleDescriptor.Append(_L8("Bappea_V"));
|
|
118 |
this->sampleDescriptor.Append(KMemVersion);
|
|
119 |
this->sampleDescriptor.Append(_L8("_MEM"));
|
|
120 |
|
62
|
121 |
LOGSTRING2("timestamp : 0x%04x", iCount);
|
51
|
122 |
|
62
|
123 |
LOGSTRING("MemSamplerImpl::CreateFirstSample - exit");
|
|
124 |
sample[0] = this->sampleDescriptor.Size();
|
|
125 |
|
|
126 |
return (TInt)(sample[0]+1);
|
51
|
127 |
}
|
|
128 |
|
62
|
129 |
TBool DMemSamplerImpl::SampleNeeded(TUint32 sampleNum)
|
51
|
130 |
{
|
62
|
131 |
TBool ret(false);
|
|
132 |
iCount = sampleNum;
|
51
|
133 |
#ifdef MEM_EVENT_HANDLER
|
|
134 |
// make the collection of chunks/threads only once, rest will be collected with mem event handler
|
62
|
135 |
|
|
136 |
// #ifdef MEM_EVENT_HANDLER_LIBRARY_EVENTS
|
|
137 |
// if (iCount <= iMemSamplingPeriod && ((iCount % iMemSamplingPeriod) == 0 || (iCount % iMemSamplingPeriodDiv3) == 0))
|
|
138 |
// #else
|
|
139 |
if (iCount % iMemSamplingPeriod == 0)
|
|
140 |
// #endif
|
|
141 |
|
51
|
142 |
#else
|
|
143 |
if ((iCount % iMemSamplingPeriod) == 0 || (iCount % iMemSamplingPeriodDiv2) == 0)
|
|
144 |
#endif
|
|
145 |
{
|
|
146 |
LOGSTRING2("MemSamplerImpl::SampleNeeded - time: %d", iCount);
|
62
|
147 |
ret= true;
|
51
|
148 |
}
|
62
|
149 |
else
|
51
|
150 |
{
|
62
|
151 |
ret=false;
|
51
|
152 |
}
|
62
|
153 |
return ret;
|
|
154 |
}
|
51
|
155 |
|
62
|
156 |
/*
|
|
157 |
* When returns 0, means that all samples done.
|
|
158 |
* Should return length of sample available in iSample
|
|
159 |
*/
|
|
160 |
TInt DMemSamplerImpl::SampleImpl(TUint32 sampleNum)
|
|
161 |
{
|
|
162 |
if(iTimeToSample)
|
|
163 |
{
|
|
164 |
// Sample threads:
|
|
165 |
if( iSampleType == ESampleThreads && !iThreadsGathered )
|
|
166 |
{
|
|
167 |
if(this->iThreadsProcessing == ENothingToProcess )
|
51
|
168 |
{
|
62
|
169 |
// gather first all thread stacks
|
|
170 |
return GatherThreads();
|
51
|
171 |
}
|
|
172 |
else
|
|
173 |
{
|
62
|
174 |
// process now thread stack list
|
|
175 |
TInt length = this->ProcessThreads();
|
|
176 |
if(length == 0)
|
|
177 |
{
|
|
178 |
this->iThreadsProcessing = ENothingToProcess;
|
|
179 |
// switch to collect chunk data
|
|
180 |
iSampleType = ESampleChunks;
|
|
181 |
iThreadsGathered = ETrue;
|
|
182 |
}
|
|
183 |
return length;
|
51
|
184 |
}
|
|
185 |
}
|
62
|
186 |
// Sample chunks:
|
|
187 |
else if( iSampleType == ESampleChunks && !iChunksGathered)
|
51
|
188 |
{
|
62
|
189 |
if(this->iChunksProcessing == ENothingToProcess)
|
51
|
190 |
{
|
|
191 |
// gather first all chunks
|
|
192 |
return GatherChunks();
|
|
193 |
}
|
|
194 |
else
|
|
195 |
{
|
62
|
196 |
// still something to go through in lists
|
|
197 |
TInt length = this->ProcessChunks();
|
|
198 |
if(length == 0)
|
|
199 |
{
|
|
200 |
this->iChunksProcessing = ENothingToProcess;
|
|
201 |
// switch to collect library data
|
|
202 |
iSampleType = ESampleLibraries;
|
|
203 |
iChunksGathered = ETrue;
|
|
204 |
iThreadsGathered = ETrue;
|
|
205 |
//iSampleThreads = true;
|
|
206 |
}
|
|
207 |
return length;
|
|
208 |
}
|
|
209 |
}
|
|
210 |
|
|
211 |
#ifdef MEM_EVENT_HANDLER_LIBRARY_EVENTS
|
|
212 |
// Sample libraries:
|
|
213 |
else if( iSampleType == ESampleLibraries )
|
|
214 |
{
|
|
215 |
if(this->iLibrariesProcessing == ENothingToProcess )
|
|
216 |
{
|
51
|
217 |
// gather libraries
|
|
218 |
return GatherLibraries();
|
|
219 |
}
|
62
|
220 |
else
|
|
221 |
{
|
|
222 |
// process now thread stack list
|
|
223 |
TInt length = this->ProcessLibraries();
|
|
224 |
if(length == 0)
|
|
225 |
{
|
|
226 |
this->iLibrariesProcessing = ENothingToProcess;
|
|
227 |
// switch to collect chunk data
|
|
228 |
iSampleType = ESampleThreads;
|
|
229 |
iLibrariesGathered = ETrue;
|
|
230 |
}
|
|
231 |
return length;
|
|
232 |
}
|
51
|
233 |
}
|
62
|
234 |
#endif
|
51
|
235 |
else
|
|
236 |
{
|
62
|
237 |
// Will not be executed. Ever. PostSample handles the state change and starting of
|
|
238 |
// memory event handler.
|
|
239 |
iChunksGathered = ETrue;
|
|
240 |
iThreadsGathered = ETrue;
|
|
241 |
iTimeToSample = false;
|
|
242 |
Kern::Printf("MEM sampler - all initial samples generated but we should not be here...!");
|
51
|
243 |
}
|
|
244 |
}
|
62
|
245 |
else // not time to sample
|
|
246 |
{
|
|
247 |
LOGSTRING("MEM sampler - not time to sample!");
|
|
248 |
}
|
51
|
249 |
// should not reach this point...
|
|
250 |
return 0;
|
|
251 |
}
|
|
252 |
|
|
253 |
|
|
254 |
inline TInt DMemSamplerImpl::GatherChunks()
|
|
255 |
{
|
|
256 |
// encode a process binary
|
|
257 |
name.Zero();
|
|
258 |
NKern::ThreadEnterCS(); // Prevent us from dying or suspending whilst holding a DMutex
|
|
259 |
DObjectCon& chunks = *Kern::Containers()[EChunk];
|
|
260 |
chunks.Wait(); // Obtain the container mutex so the list does get changed under us
|
|
261 |
|
|
262 |
this->iChunkCount = 0;
|
|
263 |
this->iNewChunkCount = 0;
|
|
264 |
this->iTotalMemoryOk = false;
|
|
265 |
TInt totalChunkCount(chunks.Count());
|
|
266 |
DChunk* c;
|
62
|
267 |
LOGSTRING2("DMemSamplerImpl::GatherChunks() chunk count %d",totalChunkCount);
|
51
|
268 |
for(TInt i(0);i<totalChunkCount;i++)
|
|
269 |
{
|
|
270 |
c = (DChunk*)(chunks)[i];
|
|
271 |
|
62
|
272 |
//LOGSTRING3("Processing chunk %d, tag: 0x%x",i,TAG(c));
|
51
|
273 |
|
|
274 |
if( (TAG(c) & 0x0000ffff) != PROFILER_CHUNK_MARK)
|
|
275 |
{
|
62
|
276 |
//LOGSTRING4("Marking chunk %d/%d, old tag 0x%x",i,(totalChunkCount-1), TAG(c));
|
51
|
277 |
// this chunk has not been tagged yet
|
|
278 |
name.Zero();
|
|
279 |
c->TraceAppendName(name,false);
|
|
280 |
|
|
281 |
TAG(c) = (PROFILER_CHUNK_MARK);
|
|
282 |
this->heapChunkNamesToReport[iNewChunkCount] = c;
|
|
283 |
iNewChunkCount++;
|
|
284 |
}
|
|
285 |
|
|
286 |
// the chunk has been tagged, add heap chunks to the list
|
|
287 |
this->heapChunksToSample[this->iChunkCount] = c;
|
|
288 |
this->iChunkCount++;
|
|
289 |
}
|
62
|
290 |
LOGSTRING2("Added %d Chunks", totalChunkCount);
|
51
|
291 |
|
|
292 |
if(this->iChunkCount > 0 || this->iNewChunkCount > 0)
|
|
293 |
{
|
|
294 |
this->iChunksProcessing = EStartingToProcess;
|
|
295 |
|
|
296 |
// process the first sample
|
|
297 |
TInt length = this->ProcessChunks();
|
|
298 |
if(length == 0)
|
|
299 |
{
|
|
300 |
this->iChunksProcessing = ENothingToProcess;
|
|
301 |
}
|
|
302 |
|
|
303 |
chunks.Signal(); // Release the container mutex
|
|
304 |
NKern::ThreadLeaveCS(); // End of critical section
|
|
305 |
return length;
|
|
306 |
}
|
|
307 |
|
|
308 |
LOGTEXT("MemSamplerImpl::SampleImpl - Error, no threads");
|
|
309 |
chunks.Signal(); // Release the container mutex
|
|
310 |
NKern::ThreadLeaveCS(); // End of critical section
|
|
311 |
return 0;
|
|
312 |
}
|
|
313 |
|
|
314 |
inline TInt DMemSamplerImpl::GatherThreads()
|
|
315 |
{
|
|
316 |
// The thread memory consumption
|
|
317 |
NKern::ThreadEnterCS(); // Prevent us from dying or suspending whilst holding a DMutex
|
|
318 |
DObjectCon& threads = *Kern::Containers()[EThread];
|
|
319 |
threads.Wait(); // Obtain the container mutex so the list does get changed under us
|
|
320 |
|
|
321 |
this->iThreadCount = 0;
|
|
322 |
this->iNewThreadCount = 0;
|
|
323 |
this->iTotalMemoryOk = false;
|
|
324 |
|
|
325 |
TInt totalThreadCount = threads.Count();
|
62
|
326 |
LOGSTRING2("DMemSamplerImpl::GatherThreads() thread count %d",totalThreadCount);
|
51
|
327 |
for(TInt i(0);i<totalThreadCount;i++)
|
|
328 |
{
|
|
329 |
DThread* t = (DThread*)(threads)[i];
|
|
330 |
|
62
|
331 |
//LOGSTRING3("Processing thread %d, tag: 0x%x",i,TAG(t));
|
51
|
332 |
|
|
333 |
if( (TAG(t) & PROFILER_MEM_THREAD_MARK) == 0)
|
|
334 |
{
|
62
|
335 |
//LOGSTRING4("Marking thread %d/%d, old tag 0x%x",i,(totalThreadCount-1), TAG(t));
|
51
|
336 |
// this thread's chunk has not been reported yet
|
|
337 |
this->threadNamesToReport[iNewThreadCount] = t;
|
|
338 |
iNewThreadCount++;
|
|
339 |
// tag the thread
|
|
340 |
TAG(t) |= PROFILER_MEM_THREAD_MARK;
|
|
341 |
}
|
|
342 |
|
|
343 |
// the chunk has been tagged, add heap chunks to the list
|
|
344 |
this->threadsToSample[this->iThreadCount] = t;
|
|
345 |
this->iThreadCount++;
|
62
|
346 |
//LOGSTRING2("Added thread %d to threads to sample",i);
|
51
|
347 |
}
|
62
|
348 |
|
|
349 |
LOGSTRING3("Added %d threads. ithreadcount %d",totalThreadCount, iThreadCount);
|
|
350 |
|
51
|
351 |
if(this->iThreadCount > 0 || this->iNewThreadCount > 0)
|
|
352 |
{
|
|
353 |
this->iThreadsProcessing = EStartingToProcess;
|
62
|
354 |
// process the first sample
|
51
|
355 |
TInt length = this->ProcessThreads();
|
|
356 |
if(length == 0)
|
|
357 |
{
|
|
358 |
this->iThreadsProcessing = ENothingToProcess;
|
|
359 |
}
|
|
360 |
threads.Signal(); // Release the container mutex
|
|
361 |
NKern::ThreadLeaveCS(); // End of critical section
|
|
362 |
return length;
|
|
363 |
}
|
|
364 |
|
62
|
365 |
LOGTEXT("DMemSamplerImpl::GatherThreads() - Error, no threads");
|
51
|
366 |
threads.Signal(); // Release the container mutex
|
|
367 |
NKern::ThreadLeaveCS(); // End of critical section
|
|
368 |
return 0;
|
|
369 |
}
|
|
370 |
|
|
371 |
#ifdef MEM_EVENT_HANDLER_LIBRARY_EVENTS
|
|
372 |
|
|
373 |
inline TInt DMemSamplerImpl::GatherLibraries()
|
|
374 |
{
|
|
375 |
LOGTEXT("MemSamplerImpl::GatherLibraries() - entry");
|
|
376 |
// encode a process binary
|
|
377 |
name.Zero();
|
|
378 |
|
|
379 |
NKern::ThreadEnterCS(); // Prevent us from dying or suspending whilst holding a DMutex
|
|
380 |
DObjectCon& libs = *Kern::Containers()[ELibrary];
|
|
381 |
libs.Wait(); // Obtain the container mutex so the list does get changed under us
|
|
382 |
|
|
383 |
this->iLibraryCount = 0;
|
|
384 |
this->iNewLibraryCount = 0;
|
|
385 |
this->iTotalMemoryOk = false;
|
|
386 |
TInt totalLibCount(libs.Count());
|
|
387 |
DLibrary* l;
|
|
388 |
|
|
389 |
for(TInt i(0);i<totalLibCount;i++)
|
|
390 |
{
|
|
391 |
l = (DLibrary*)(libs)[i];
|
|
392 |
|
|
393 |
LOGSTRING3("Processing library %d, tag: 0x%x",i,TAG(l));
|
|
394 |
|
|
395 |
if( (TAG(l) & 0xffffffff) != PROFILER_LIBRARY_MARK)
|
|
396 |
{
|
|
397 |
LOGSTRING4("Marking library %d/%d, old tag 0x%x",i,(totalLibCount-1), TAG(l));
|
|
398 |
// this library has not been tagged yet
|
|
399 |
name.Zero();
|
|
400 |
l->TraceAppendName(name,false);
|
|
401 |
|
|
402 |
TAG(l) = (PROFILER_LIBRARY_MARK);
|
|
403 |
this->libraryNamesToReport[iNewLibraryCount] = l;
|
|
404 |
iNewLibraryCount++;
|
|
405 |
}
|
|
406 |
|
|
407 |
// the library has been tagged, add library to the list
|
|
408 |
this->librariesToSample[this->iLibraryCount] = l;
|
|
409 |
this->iLibraryCount++;
|
|
410 |
LOGSTRING2("Added library %d to Libraries",i);
|
|
411 |
}
|
|
412 |
|
|
413 |
if(this->iLibraryCount > 0 || this->iNewLibraryCount > 0)
|
|
414 |
{
|
|
415 |
this->iLibrariesProcessing = EStartingToProcess;
|
|
416 |
|
|
417 |
// process the first sample
|
|
418 |
TInt length = this->ProcessLibraries();
|
|
419 |
|
|
420 |
if(length == 0)
|
|
421 |
{
|
|
422 |
this->iLibrariesProcessing = ENothingToProcess;
|
|
423 |
}
|
|
424 |
|
|
425 |
libs.Signal(); // Release the container mutex
|
|
426 |
NKern::ThreadLeaveCS(); // End of critical section
|
|
427 |
return length;
|
|
428 |
}
|
|
429 |
|
|
430 |
LOGTEXT("MemSamplerImpl::SampleImpl - Error, no libraries");
|
|
431 |
libs.Signal(); // Release the container mutex
|
|
432 |
NKern::ThreadLeaveCS(); // End of critical section
|
|
433 |
return 0;
|
|
434 |
}
|
|
435 |
#endif
|
|
436 |
|
|
437 |
inline TInt DMemSamplerImpl::ProcessChunks()
|
|
438 |
{
|
62
|
439 |
if(iHandledChunks < 50)
|
|
440 |
{
|
|
441 |
if(iNewChunkCount > 0)
|
|
442 |
{
|
|
443 |
if(this->iChunksProcessing == EStartingToProcess)
|
|
444 |
{
|
|
445 |
// this is the first sample, encode a code for names
|
|
446 |
this->iChunksProcessing = EProcessingNames;
|
|
447 |
iHandledChunks++;
|
|
448 |
return EncodeNameCode();
|
|
449 |
}
|
|
450 |
|
|
451 |
if(iTotalMemoryNameOk == false)
|
|
452 |
{
|
|
453 |
iHandledChunks++;
|
|
454 |
return EncodeTotalMemoryName();
|
|
455 |
}
|
|
456 |
iNewChunkCount--;
|
|
457 |
DChunk* c = this->heapChunkNamesToReport[iNewChunkCount];
|
|
458 |
iHandledChunks++;
|
|
459 |
return EncodeChunkName(*c);
|
|
460 |
}
|
|
461 |
else if(iChunkCount > 0)
|
|
462 |
{
|
|
463 |
if(this->iChunksProcessing == EProcessingNames || this->iChunksProcessing == EStartingToProcess)
|
|
464 |
{
|
|
465 |
// this is the first data sample, encode a code for data
|
|
466 |
this->iChunksProcessing = EProcessingData;
|
|
467 |
iHandledChunks++;
|
|
468 |
return EncodeDataCode();
|
|
469 |
}
|
|
470 |
|
|
471 |
if(this->iTotalMemoryOk == false)
|
|
472 |
{
|
|
473 |
iHandledChunks++;
|
|
474 |
return EncodeTotalMemory();
|
|
475 |
}
|
|
476 |
|
|
477 |
// there are no new chunks to report
|
|
478 |
// thus generate the real report
|
|
479 |
iChunkCount--;
|
|
480 |
DChunk* c = this->heapChunksToSample[iChunkCount];
|
|
481 |
iHandledChunks++;
|
|
482 |
return EncodeChunkData(*c);
|
|
483 |
}
|
|
484 |
else
|
|
485 |
{
|
|
486 |
// everything is processed
|
|
487 |
LOGSTRING2("MemSamplerImpl::ProcessChunks() Chunks processed! Chunk count = %d", iChunkCount);
|
|
488 |
#ifdef MEM_EVENT_HANDLER
|
|
489 |
this->iChunksGathered = ETrue;
|
|
490 |
LOGSTRING2("MemSamplerImpl::ProcessChunks() - chunks gathered! Time: %d",iCount);
|
|
491 |
#endif
|
|
492 |
return 0;
|
|
493 |
}
|
|
494 |
}
|
|
495 |
else
|
|
496 |
{
|
|
497 |
LOGSTRING("MemSamplerImpl::ProcessChunks() 0");
|
|
498 |
iHandledChunks =0;
|
|
499 |
}
|
|
500 |
return -1;
|
|
501 |
}
|
|
502 |
|
|
503 |
inline TInt DMemSamplerImpl::ProcessThreads()
|
|
504 |
{
|
|
505 |
if(iHandledThreads < 50)
|
51
|
506 |
{
|
62
|
507 |
if(iNewThreadCount > 0)
|
|
508 |
{
|
|
509 |
if(this->iThreadsProcessing == EStartingToProcess)
|
|
510 |
{
|
|
511 |
// this is the first sample, encode a code for names
|
|
512 |
this->iThreadsProcessing = EProcessingNames;
|
|
513 |
iHandledThreads++;
|
|
514 |
return EncodeNameCode();
|
|
515 |
}
|
|
516 |
|
|
517 |
if(iTotalMemoryNameOk == false)
|
|
518 |
{
|
|
519 |
LOGSTRING("MemSamplerImpl::ProcessThreads() Encoding total memory name!");
|
|
520 |
iHandledThreads++;
|
|
521 |
return EncodeTotalMemoryName();
|
|
522 |
}
|
|
523 |
iNewThreadCount--;
|
|
524 |
DThread* t = this->threadNamesToReport[iNewThreadCount];
|
|
525 |
iHandledThreads++;
|
|
526 |
return EncodeChunkName(*t);
|
|
527 |
}
|
|
528 |
else if(iThreadCount > 0)
|
|
529 |
{
|
|
530 |
if(this->iThreadsProcessing == EProcessingNames || this->iThreadsProcessing == EStartingToProcess)
|
|
531 |
{
|
|
532 |
// this is the first data sample, encode a code for data
|
|
533 |
this->iThreadsProcessing = EProcessingData;
|
|
534 |
iHandledThreads++;
|
|
535 |
return EncodeDataCode();
|
|
536 |
}
|
|
537 |
|
|
538 |
if(this->iTotalMemoryOk == false)
|
|
539 |
{
|
|
540 |
iHandledThreads++;
|
|
541 |
return EncodeTotalMemory();
|
|
542 |
}
|
|
543 |
|
|
544 |
// there are no new threads to report
|
|
545 |
// thus generate the real report
|
|
546 |
iThreadCount--;
|
|
547 |
DThread* t = this->threadsToSample[iThreadCount];
|
|
548 |
iHandledThreads++;
|
|
549 |
return EncodeChunkData(*t);
|
|
550 |
}
|
|
551 |
else
|
|
552 |
{
|
|
553 |
// everything is processed
|
|
554 |
LOGSTRING2("MemSamplerImpl::ProcessThreads() Threads processed! Thread count = %d", iThreadCount);
|
|
555 |
#ifdef MEM_EVENT_HANDLER
|
|
556 |
this->iThreadsGathered = true;
|
|
557 |
LOGSTRING2("MemSamplerImpl::ProcessThreads() - threads gathered! Time: %d", iCount);
|
|
558 |
#endif
|
|
559 |
return 0;
|
|
560 |
}
|
|
561 |
}
|
|
562 |
else
|
|
563 |
{
|
|
564 |
LOGSTRING("MemSamplerImpl::ProcessThreads() 0");
|
|
565 |
iHandledThreads=0;
|
|
566 |
}
|
|
567 |
return -1;
|
|
568 |
}
|
|
569 |
|
|
570 |
#ifdef MEM_EVENT_HANDLER_LIBRARY_EVENTS
|
|
571 |
inline TInt DMemSamplerImpl::ProcessLibraries()
|
|
572 |
{
|
|
573 |
LOGTEXT("ProcessLibraries - entry");
|
|
574 |
|
|
575 |
if(iHandledLibs < 50)
|
|
576 |
{
|
|
577 |
if(iNewLibraryCount > 0)
|
|
578 |
{
|
|
579 |
if(this->iLibrariesProcessing == EStartingToProcess)
|
51
|
580 |
{
|
|
581 |
// this is the first sample, encode a code for names
|
62
|
582 |
this->iLibrariesProcessing = EProcessingNames;
|
|
583 |
iHandledLibs++;
|
51
|
584 |
return EncodeNameCode();
|
|
585 |
}
|
|
586 |
|
|
587 |
if(iTotalMemoryNameOk == false)
|
|
588 |
{
|
62
|
589 |
iHandledLibs++;
|
51
|
590 |
return EncodeTotalMemoryName();
|
|
591 |
}
|
|
592 |
|
|
593 |
// there are new library names to report
|
|
594 |
iNewLibraryCount--;
|
|
595 |
DLibrary* l = this->libraryNamesToReport[iNewLibraryCount];
|
62
|
596 |
iHandledLibs++;
|
51
|
597 |
return EncodeChunkName(*l);
|
|
598 |
|
|
599 |
}
|
|
600 |
else if(iLibraryCount > 0)
|
|
601 |
{
|
|
602 |
if(this->iLibrariesProcessing == EProcessingNames || this->iLibrariesProcessing == EStartingToProcess)
|
|
603 |
{
|
|
604 |
// this is the first data sample, encode a code for data
|
|
605 |
this->iLibrariesProcessing = EProcessingData;
|
62
|
606 |
iHandledLibs++;
|
51
|
607 |
return EncodeDataCode();
|
|
608 |
}
|
|
609 |
|
|
610 |
if(this->iTotalMemoryOk == false)
|
|
611 |
{
|
62
|
612 |
iHandledLibs++;
|
51
|
613 |
return EncodeTotalMemory();
|
|
614 |
}
|
|
615 |
|
|
616 |
// there are no new libraries to report
|
|
617 |
// thus generate the real report
|
|
618 |
iLibraryCount--;
|
|
619 |
DLibrary* l = this->librariesToSample[iLibraryCount];
|
62
|
620 |
iHandledLibs++;
|
51
|
621 |
return EncodeChunkData(*l);
|
|
622 |
}
|
|
623 |
else
|
|
624 |
{
|
|
625 |
// everything is processed
|
|
626 |
LOGSTRING2(" Libraries processed! Library count = %d", iLibraryCount);
|
|
627 |
|
|
628 |
this->iLibrariesGathered = true;
|
62
|
629 |
LOGSTRING2("MemSamplerImpl::ProcessLibraries() - libraries gathered! Time: %d",iCount);
|
51
|
630 |
|
|
631 |
return 0;
|
|
632 |
}
|
|
633 |
}
|
62
|
634 |
else
|
|
635 |
{
|
|
636 |
LOGSTRING("MemSamplerImpl::ProcessLibs() 0");
|
|
637 |
iHandledLibs =0;
|
|
638 |
}
|
|
639 |
return -1;
|
|
640 |
}
|
51
|
641 |
#endif
|
62
|
642 |
|
51
|
643 |
inline TInt DMemSamplerImpl::EncodeNameCode()
|
|
644 |
{
|
|
645 |
sample[0] = 1;
|
|
646 |
sample[1] = 0xaa;
|
|
647 |
return 2;
|
|
648 |
}
|
|
649 |
|
|
650 |
inline TInt DMemSamplerImpl::EncodeDataCode()
|
|
651 |
{
|
|
652 |
sample[0] = 1;
|
|
653 |
sample[1] = 0xdd;
|
|
654 |
return 2;
|
|
655 |
}
|
|
656 |
|
|
657 |
inline TInt DMemSamplerImpl::EncodeTotalMemoryName()
|
|
658 |
{
|
|
659 |
this->iTotalMemoryNameOk = true;
|
|
660 |
|
|
661 |
TUint8* size = &sample[0];
|
|
662 |
*size = 0;
|
|
663 |
|
|
664 |
// encode name
|
|
665 |
this->sampleDescriptor.Zero();
|
|
666 |
this->sampleDescriptor.Append(_L("TOTAL_MEMORY"));
|
|
667 |
*size += this->sampleDescriptor.Size();
|
|
668 |
|
|
669 |
// add id here
|
|
670 |
TUint32 id(0xbabbeaaa);
|
|
671 |
this->sampleDescriptor.Append((TUint8*)&(id),sizeof(TUint32));
|
|
672 |
*size += sizeof(TUint32);
|
|
673 |
|
|
674 |
// the size is the descriptor length + the size field
|
|
675 |
return ((TInt)(*size))+1;
|
|
676 |
}
|
|
677 |
|
|
678 |
inline TInt DMemSamplerImpl::EncodeTotalMemory()
|
|
679 |
{
|
|
680 |
TUint8* size = &sample[0];
|
|
681 |
*size = 0;
|
|
682 |
|
|
683 |
NKern::LockSystem();
|
|
684 |
TInt freeRam = Kern::FreeRamInBytes();
|
|
685 |
TInt totalRam = Kern::SuperPage().iTotalRamSize;
|
|
686 |
NKern::UnlockSystem();
|
|
687 |
|
|
688 |
this->sampleDescriptor.Zero();
|
|
689 |
|
|
690 |
TUint32 id(0xbabbeaaa);
|
|
691 |
TInt zero(0);
|
|
692 |
|
|
693 |
this->sampleDescriptor.Append((TUint8*)&(id),sizeof(TUint32));
|
|
694 |
*size += sizeof(TUint);
|
|
695 |
|
|
696 |
this->sampleDescriptor.Append((TUint8*)&(totalRam),sizeof(TInt));
|
|
697 |
*size += sizeof(TInt);
|
|
698 |
|
|
699 |
// append the cell amount allocated
|
|
700 |
this->sampleDescriptor.Append((TUint8*)&(zero),sizeof(TInt));
|
|
701 |
*size += sizeof(TInt);
|
|
702 |
|
|
703 |
// append the chunk size
|
|
704 |
this->sampleDescriptor.Append((TUint8*)&(freeRam),sizeof(TInt));
|
|
705 |
*size += sizeof(TInt);
|
|
706 |
|
|
707 |
// append the thread user stack size
|
|
708 |
this->sampleDescriptor.Append((TUint8*)&(zero),sizeof(TInt));
|
|
709 |
*size += sizeof(TInt);
|
|
710 |
|
|
711 |
this->iTotalMemoryOk = true;
|
|
712 |
|
|
713 |
return ((TInt)(*size))+1;
|
|
714 |
}
|
|
715 |
|
|
716 |
inline TInt DMemSamplerImpl::EncodeChunkName(DChunk& c)
|
|
717 |
{
|
|
718 |
// the size of the following name is in the first byte
|
|
719 |
TUint8* size = &sample[0];
|
|
720 |
*size = 0;
|
|
721 |
|
|
722 |
// encode chunk name
|
|
723 |
this->sampleDescriptor.Zero();
|
|
724 |
this->sampleDescriptor.Append(_L("C_"));
|
|
725 |
c.TraceAppendFullName(this->sampleDescriptor,false);
|
|
726 |
*size += this->sampleDescriptor.Size();
|
|
727 |
|
|
728 |
// add chunk object address here
|
|
729 |
TUint32 chunkAddr((TUint32)&c);
|
|
730 |
this->sampleDescriptor.Append((TUint8*)&(chunkAddr),sizeof(TUint32));
|
|
731 |
*size += sizeof(TUint32);
|
|
732 |
|
|
733 |
// the size is the descriptor length + the size field
|
|
734 |
LOGSTRING2("Non-Heap Chunk Name - %d",*size);
|
62
|
735 |
return ((TInt)(*size))+1;
|
51
|
736 |
}
|
|
737 |
|
|
738 |
inline TInt DMemSamplerImpl::EncodeChunkName(DThread& t)
|
|
739 |
{
|
|
740 |
// the size of the following name is in the first byte
|
|
741 |
TUint8* size = &sample[0];
|
|
742 |
*size = 0;
|
|
743 |
this->sampleDescriptor.Zero();
|
|
744 |
|
|
745 |
this->sampleDescriptor.Append(_L("T_"));
|
|
746 |
t.TraceAppendFullName(this->sampleDescriptor,false);
|
|
747 |
*size += this->sampleDescriptor.Size();
|
|
748 |
|
|
749 |
// copy the 4 bytes from the thread id field
|
|
750 |
this->sampleDescriptor.Append((TUint8*)&(t.iId),sizeof(TUint));
|
|
751 |
*size += sizeof(TUint);
|
|
752 |
|
|
753 |
// the size is the descriptor length + the size field
|
|
754 |
LOGSTRING2("Name - %d",*size);
|
|
755 |
return ((TInt)(*size))+1;
|
|
756 |
}
|
|
757 |
#ifdef MEM_EVENT_HANDLER_LIBRARY_EVENTS
|
|
758 |
inline TInt DMemSamplerImpl::EncodeChunkName(DLibrary& l)
|
|
759 |
{
|
|
760 |
// the size of the following name is in the first byte
|
|
761 |
TUint8* size = &sample[0];
|
|
762 |
*size = 0;
|
|
763 |
|
|
764 |
// encode library name
|
|
765 |
this->sampleDescriptor.Zero();
|
|
766 |
this->sampleDescriptor.Append(_L("L_"));
|
|
767 |
l.TraceAppendFullName(this->sampleDescriptor,false);
|
|
768 |
*size += this->sampleDescriptor.Size();
|
|
769 |
|
|
770 |
// add chunk object address here
|
|
771 |
TUint32 libAddr((TUint32)&l);
|
|
772 |
this->sampleDescriptor.Append((TUint8*)&(libAddr),sizeof(TUint32));
|
|
773 |
*size += sizeof(TUint32);
|
|
774 |
|
|
775 |
// the size is the descriptor length + the size field
|
|
776 |
LOGSTRING2("Name - %d",*size);
|
|
777 |
return ((TInt)(*size))+1;
|
|
778 |
}
|
|
779 |
#endif
|
|
780 |
inline TInt DMemSamplerImpl::EncodeChunkData(DChunk& c)
|
|
781 |
{
|
|
782 |
// the size of the following name is in the first byte
|
|
783 |
TUint8* size = &sample[0];
|
|
784 |
*size = 0;
|
62
|
785 |
|
|
786 |
TInt zero(0);
|
51
|
787 |
this->sampleDescriptor.Zero();
|
|
788 |
TUint32 address((TUint32)&c);
|
|
789 |
|
|
790 |
this->sampleDescriptor.Append((TUint8*)&address,sizeof(TUint32));
|
62
|
791 |
*size += sizeof(TUint32);
|
|
792 |
LOGSTRING2("address - 0x%04x",&address);
|
51
|
793 |
// copy the total amount of memory allocated
|
|
794 |
this->sampleDescriptor.Append((TUint8*)&(c.iSize),sizeof(TInt));
|
|
795 |
*size += sizeof(TInt);
|
|
796 |
|
|
797 |
// append the cell amount allocated
|
|
798 |
this->sampleDescriptor.Append((TUint8*)&(zero),sizeof(TInt));
|
|
799 |
*size += sizeof(TInt);
|
|
800 |
|
|
801 |
// append the chunk size
|
|
802 |
this->sampleDescriptor.Append((TUint8*)&(c.iSize),sizeof(TUint));
|
|
803 |
*size += sizeof(TUint);
|
|
804 |
|
|
805 |
// append the thread user stack size
|
|
806 |
this->sampleDescriptor.Append((TUint8*)&(zero),sizeof(TInt));
|
|
807 |
*size += sizeof(TInt);
|
|
808 |
|
62
|
809 |
LOGSTRING2("chunk Data - %d",*size);
|
51
|
810 |
return ((TInt)(*size))+1;
|
|
811 |
}
|
|
812 |
|
|
813 |
inline TInt DMemSamplerImpl::EncodeChunkData(DThread& t)
|
|
814 |
{
|
|
815 |
// the size of the following name is in the first byte
|
|
816 |
TUint8* size = &sample[0];
|
|
817 |
*size = 0;
|
62
|
818 |
TInt zero(0);
|
51
|
819 |
this->sampleDescriptor.Zero();
|
62
|
820 |
//LOGTEXT("MemSamplerImpl::EncodeChunkData - cleared");
|
51
|
821 |
|
|
822 |
this->sampleDescriptor.Append((TUint8*)&(t.iId),sizeof(TUint));
|
|
823 |
*size += sizeof(TUint);
|
|
824 |
|
|
825 |
// copy the total amount of memory allocated for user side stack
|
|
826 |
this->sampleDescriptor.Append((TUint8*)&(t.iUserStackSize),sizeof(TInt));
|
|
827 |
*size += sizeof(TInt);
|
|
828 |
|
62
|
829 |
|
51
|
830 |
// append the cell amount allocated (zero, not in use here)
|
|
831 |
this->sampleDescriptor.Append((TUint8*)&zero,sizeof(TInt));
|
|
832 |
*size += sizeof(TInt);
|
|
833 |
|
|
834 |
// append the chunk size (this is not a chunk)
|
|
835 |
this->sampleDescriptor.Append((TUint8*)&(zero),sizeof(TUint));
|
|
836 |
*size += sizeof(TUint);
|
|
837 |
|
|
838 |
// append user stack (max) size
|
|
839 |
this->sampleDescriptor.Append((TUint8*)&(t.iUserStackSize),sizeof(TInt));
|
|
840 |
*size += sizeof(TInt);
|
|
841 |
|
|
842 |
LOGSTRING2("Data -> %d",*size);
|
|
843 |
return ((TInt)(*size))+1;
|
|
844 |
}
|
|
845 |
#ifdef MEM_EVENT_HANDLER_LIBRARY_EVENTS
|
|
846 |
inline TInt DMemSamplerImpl::EncodeChunkData(DLibrary& l)
|
|
847 |
{
|
|
848 |
LOGTEXT("MemSamplerImpl::EncodeChunkData (Library) - entry");
|
|
849 |
// the size of the following name is in the first byte
|
|
850 |
TUint8* size = &sample[0];
|
|
851 |
*size = 0;
|
|
852 |
this->sampleDescriptor.Zero();
|
|
853 |
|
|
854 |
TUint32 address((TUint32)&l);
|
|
855 |
|
|
856 |
this->sampleDescriptor.Append((TUint8*)&address,sizeof(TUint32));
|
|
857 |
*size += sizeof(TUint);
|
|
858 |
|
|
859 |
this->sampleDescriptor.Append((TUint8*)&(l.iCodeSeg->iSize),sizeof(TUint32));
|
|
860 |
*size += sizeof(TInt);
|
|
861 |
|
|
862 |
this->sampleDescriptor.Append((TUint8*)&(l.iMapCount),sizeof(TInt));
|
|
863 |
*size += sizeof(TInt);
|
|
864 |
|
|
865 |
TInt zero(0);
|
|
866 |
this->sampleDescriptor.Append((TUint8*)&(zero),sizeof(TInt));
|
|
867 |
*size += sizeof(TInt);
|
|
868 |
|
|
869 |
this->sampleDescriptor.Append((TUint8*)&(zero),sizeof(TInt));
|
|
870 |
*size += sizeof(TInt);
|
|
871 |
|
|
872 |
LOGSTRING2("LData - %d",*size);
|
|
873 |
return ((TInt)(*size))+1;
|
|
874 |
|
|
875 |
}
|
|
876 |
#endif
|
|
877 |
void DMemSamplerImpl::Reset()
|
|
878 |
{
|
62
|
879 |
LOGSTRING("MemSamplerImpl::Reset - entry");
|
51
|
880 |
iCount = 0; // sample threads 1 cycle after actual MEM sample time...
|
62
|
881 |
this->iTimeToSample = true;
|
51
|
882 |
this->iChunkCount = 0;
|
|
883 |
this->iNewChunkCount = 0;
|
|
884 |
|
|
885 |
this->iTotalMemoryOk = false;
|
|
886 |
this->iTotalMemoryNameOk = false;
|
|
887 |
|
|
888 |
this->iChunksProcessing = ENothingToProcess;
|
|
889 |
this->iThreadsProcessing = ENothingToProcess;
|
|
890 |
#ifdef MEM_EVENT_HANDLER_LIBRARY_EVENTS
|
|
891 |
this->iLibrariesProcessing = ENothingToProcess;
|
62
|
892 |
#else
|
51
|
893 |
this->iSampleType = ESampleThreads;
|
62
|
894 |
//this->iSampleType = ESampleChunks;
|
|
895 |
//this->iSampleThreads = true;
|
51
|
896 |
#endif
|
62
|
897 |
this->sampleDescriptor.Zero();
|
|
898 |
// clear all chunk tags
|
51
|
899 |
NKern::ThreadEnterCS(); // Prevent us from dying or suspending whilst holding a DMutex
|
|
900 |
DObjectCon* chunks = Kern::Containers()[EChunk];
|
|
901 |
chunks->Wait(); // Obtain the container mutex so the list does get changed under us
|
|
902 |
|
|
903 |
TInt totalChunkCount = chunks->Count();
|
|
904 |
for(TInt i=0;i<totalChunkCount;i++)
|
|
905 |
{
|
|
906 |
DChunk* c = (DChunk*)(*chunks)[i];
|
|
907 |
TAG(c) = 0;
|
|
908 |
}
|
|
909 |
chunks->Signal(); // Release the container mutex
|
|
910 |
|
62
|
911 |
LOGSTRING("MemSamplerImpl::Reset");
|
51
|
912 |
this->iThreadCount = 0;
|
|
913 |
this->iNewThreadCount = 0;
|
|
914 |
this->sampleDescriptor.Zero();
|
|
915 |
|
62
|
916 |
// clear all thread tags
|
51
|
917 |
DObjectCon* threads = Kern::Containers()[EThread];
|
|
918 |
threads->Wait(); // Obtain the container mutex so the list does get changed under us
|
|
919 |
|
|
920 |
TInt totalThreadCount = threads->Count();
|
|
921 |
for(TInt i=0;i<totalThreadCount;i++)
|
|
922 |
{
|
|
923 |
DThread* t = (DThread*)(*threads)[i];
|
|
924 |
TAG(t) = (TAG(t) & 0xfffffffe);
|
|
925 |
}
|
|
926 |
threads->Signal(); // Release the container mutex
|
|
927 |
|
|
928 |
#ifdef MEM_EVENT_HANDLER_LIBRARY_EVENTS
|
|
929 |
this->iLibraryCount = 0;
|
|
930 |
this->iNewLibraryCount = 0;
|
|
931 |
this->sampleDescriptor.Zero();
|
|
932 |
|
|
933 |
// clear all library tags
|
|
934 |
DObjectCon* libs = Kern::Containers()[ELibrary];
|
|
935 |
libs->Wait(); // Obtain the container mutex so the list does get changed under us
|
|
936 |
|
|
937 |
TInt totalLibraryCount = libs->Count();
|
|
938 |
for(TInt i=0; i<totalLibraryCount; i++)
|
|
939 |
{
|
|
940 |
DLibrary* l = (DLibrary*)(*libs)[i];
|
|
941 |
TAG(l) = (TAG(l) & 0xefffffff);
|
|
942 |
}
|
|
943 |
libs->Signal(); // Release the container mutex
|
|
944 |
#endif
|
|
945 |
|
|
946 |
NKern::ThreadLeaveCS(); // End of critical section
|
|
947 |
}
|
|
948 |
|
62
|
949 |
|
|
950 |
// end of file
|