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 |
#include <piprofiler/ProfilerVersion.h>
|
|
20 |
#include <piprofiler/ProfilerTraces.h>
|
|
21 |
|
|
22 |
#include <kern_priv.h>
|
|
23 |
#include <plat_priv.h>
|
|
24 |
|
|
25 |
#include "PriSamplerImpl.h"
|
|
26 |
|
|
27 |
#if !defined(__NKERN_H__)
|
|
28 |
#include <nkern.h>
|
|
29 |
#endif
|
|
30 |
|
|
31 |
#define TAG(obj) (*(TUint32*)&(obj->iAsyncDeleteNext))
|
|
32 |
#define PROFILER_THREAD_MARK ((TUint32)0x00000002)
|
|
33 |
|
|
34 |
|
|
35 |
DPriSamplerImpl::DPriSamplerImpl() :
|
|
36 |
sampleDescriptor(&(this->sample[1]),0,256)
|
|
37 |
{
|
|
38 |
LOGTEXT("PriSamplerImpl::PriSamplerImpl() - konstruktori");
|
|
39 |
|
|
40 |
iCountti = 50; // sample threads 16 cycles before actual MEM and PRI sample time...
|
|
41 |
iNewThreadCount = 0;
|
|
42 |
iThreadCount = 0;
|
|
43 |
iProcessing = ENothingToProcess;
|
|
44 |
|
|
45 |
for(TInt i=0;i<KProfilerMaxThreadAmount;i++)
|
|
46 |
{
|
|
47 |
this->threadsToSample[i] = 0;
|
|
48 |
this->threadNamesToReport[i] = 0;
|
|
49 |
}
|
|
50 |
|
|
51 |
}
|
|
52 |
|
|
53 |
DPriSamplerImpl::~DPriSamplerImpl()
|
|
54 |
{
|
|
55 |
|
|
56 |
}
|
|
57 |
|
|
58 |
TInt DPriSamplerImpl::CreateFirstSample()
|
|
59 |
{
|
|
60 |
LOGTEXT("PriSamplerImpl::CreateFirstSample - entry");
|
|
61 |
|
|
62 |
this->sampleDescriptor.Zero();
|
|
63 |
this->sampleDescriptor.Append(_L8("Bappea_V"));
|
|
64 |
this->sampleDescriptor.Append(PROFILER_PRI_SAMPLER_VERSION);
|
|
65 |
this->sampleDescriptor.Append(_L8("_PRI"));
|
|
66 |
|
|
67 |
sample[0] = this->sampleDescriptor.Size();
|
|
68 |
|
|
69 |
LOGTEXT("PriSamplerImpl::CreateFirstSample - exit");
|
|
70 |
|
|
71 |
return (TInt)(sample[0]+1);
|
|
72 |
}
|
|
73 |
|
|
74 |
TBool DPriSamplerImpl::SampleNeeded()
|
|
75 |
{
|
|
76 |
iCountti++;
|
|
77 |
if(iCountti % (iPriSamplingPeriod) == 0)
|
|
78 |
{
|
|
79 |
LOGTEXT("PriSamplerImpl::SampleNeeded - true");
|
|
80 |
return true;
|
|
81 |
}
|
|
82 |
else
|
|
83 |
{
|
|
84 |
return false;
|
|
85 |
}
|
|
86 |
}
|
|
87 |
|
|
88 |
|
|
89 |
TInt DPriSamplerImpl::SampleImpl()
|
|
90 |
{
|
|
91 |
/*
|
|
92 |
*
|
|
93 |
* EKA-2 implementation of PRI trace
|
|
94 |
*
|
|
95 |
*/
|
|
96 |
if(this->iProcessing == ENothingToProcess)
|
|
97 |
{
|
|
98 |
if((iCountti % iPriSamplingPeriod) != 0 ) return 0;
|
|
99 |
|
|
100 |
LOGTEXT("Processing threads...");
|
|
101 |
|
62
|
102 |
DObjectCon* threads = Kern::Containers()[EThread];
|
|
103 |
NKern::ThreadEnterCS(); // Prevent us from dying or suspending whilst holding a DMutex
|
|
104 |
threads->Wait();
|
20
|
105 |
|
|
106 |
// PRI trace variables
|
|
107 |
this->iThreadCount = 0;
|
|
108 |
this->iNewThreadCount = 0;
|
62
|
109 |
TInt totalThreadCount = threads->Count();
|
20
|
110 |
|
|
111 |
for(TInt i=0;i<totalThreadCount;i++)
|
|
112 |
{
|
62
|
113 |
//DThread* t = (DThread*)(threads)[i];
|
|
114 |
DObject* pO=(*threads)[i];
|
|
115 |
DThread* t = (DThread*)pO;
|
|
116 |
|
20
|
117 |
LOGSTRING3("Processing thread %d, tag: 0x%x",i,TAG(t));
|
|
118 |
|
|
119 |
if( (TAG(t) & PROFILER_THREAD_MARK) == 0)
|
|
120 |
{
|
|
121 |
LOGSTRING2("Marking thread %d",i);
|
|
122 |
// this thread's chunk has not been reported yet
|
|
123 |
this->threadNamesToReport[iNewThreadCount] = t;
|
|
124 |
iNewThreadCount++;
|
|
125 |
// tag the thread
|
|
126 |
TAG(t) |= PROFILER_THREAD_MARK;
|
|
127 |
LOGSTRING2("New Thread %d",i);
|
|
128 |
}
|
|
129 |
else
|
|
130 |
{
|
|
131 |
LOGSTRING3("Thread %d marked already - 0x%x",i,TAG(t));
|
|
132 |
}
|
|
133 |
|
|
134 |
// the thread has been tagged, add heap chunks to the list
|
|
135 |
this->threadsToSample[this->iThreadCount] = t;
|
|
136 |
this->iThreadCount++;
|
|
137 |
LOGSTRING2("Added thread %d to threads to sample",i);
|
|
138 |
}
|
|
139 |
|
62
|
140 |
threads->Signal();
|
|
141 |
NKern::ThreadLeaveCS(); // End of critical section
|
|
142 |
|
20
|
143 |
if(this->iThreadCount > 0 || this->iNewThreadCount > 0)
|
|
144 |
{
|
|
145 |
this->iProcessing = EStartingToProcess;
|
|
146 |
|
|
147 |
// process the first sample
|
|
148 |
TInt length = this->ProcessChunks();
|
|
149 |
|
|
150 |
if(length == 0)
|
|
151 |
{
|
|
152 |
this->iProcessing = ENothingToProcess;
|
|
153 |
}
|
|
154 |
return length;
|
|
155 |
}
|
|
156 |
else
|
|
157 |
{
|
|
158 |
// there were no threads, should not take place
|
|
159 |
LOGTEXT("PriSamplerImpl::SampleImpl - Error, no threads");
|
|
160 |
return 0;
|
|
161 |
}
|
|
162 |
}
|
|
163 |
else
|
|
164 |
{
|
|
165 |
TInt length = this->ProcessChunks();
|
|
166 |
if(length == 0)
|
|
167 |
{
|
|
168 |
this->iProcessing = ENothingToProcess;
|
|
169 |
}
|
|
170 |
return length;
|
|
171 |
}
|
|
172 |
}
|
|
173 |
|
|
174 |
inline TInt DPriSamplerImpl::ProcessChunks()
|
|
175 |
{
|
|
176 |
/*
|
|
177 |
*
|
|
178 |
* EKA-2 implementation of PRI trace
|
|
179 |
*
|
|
180 |
*/
|
|
181 |
|
|
182 |
if(iNewThreadCount > 0)
|
|
183 |
{
|
|
184 |
if(this->iProcessing == EStartingToProcess)
|
|
185 |
{
|
|
186 |
// this is the first sample, encode a code for names
|
|
187 |
this->iProcessing = EProcessingNames;
|
|
188 |
return EncodeNameCode();
|
|
189 |
}
|
|
190 |
|
|
191 |
// there are new thread names to report
|
|
192 |
iNewThreadCount--;
|
|
193 |
DThread* t = this->threadNamesToReport[iNewThreadCount];
|
|
194 |
return EncodeChunkName(*t);
|
|
195 |
}
|
|
196 |
else if(iThreadCount > 0)
|
|
197 |
{
|
|
198 |
if(this->iProcessing == EProcessingNames || this->iProcessing == EStartingToProcess)
|
|
199 |
{
|
|
200 |
// this is the first data sample, encode a code for data
|
|
201 |
this->iProcessing = EProcessingData;
|
|
202 |
return EncodeDataCode();
|
|
203 |
}
|
|
204 |
|
|
205 |
// there are no new chunks to report
|
|
206 |
// thus generate the real report
|
|
207 |
iThreadCount--;
|
|
208 |
DThread* t = this->threadsToSample[iThreadCount];
|
|
209 |
LOGSTRING2("PriSamplerImpl::ProcessChunks - starting to process thread 0x%x",t);
|
|
210 |
return EncodeChunkData(*t);
|
|
211 |
}
|
|
212 |
else
|
|
213 |
{
|
|
214 |
// everything is processed
|
|
215 |
return 0;
|
|
216 |
}
|
|
217 |
}
|
|
218 |
|
|
219 |
inline TInt DPriSamplerImpl::EncodeNameCode()
|
|
220 |
{
|
|
221 |
sample[0] = 1;
|
|
222 |
sample[1] = 0xbb; // pri trace name code
|
|
223 |
return 2;
|
|
224 |
}
|
|
225 |
|
|
226 |
inline TInt DPriSamplerImpl::EncodeDataCode()
|
|
227 |
{
|
|
228 |
sample[0] = 1;
|
|
229 |
sample[1] = 0xee; // pri trace data code
|
|
230 |
return 2;
|
|
231 |
}
|
|
232 |
|
|
233 |
|
|
234 |
inline TInt DPriSamplerImpl::EncodeChunkName(DThread& t)
|
|
235 |
{
|
|
236 |
// the size of the following name is in the first byte
|
|
237 |
TUint8* size = &sample[0];
|
|
238 |
*size = 0;
|
|
239 |
this->sampleDescriptor.Zero();
|
|
240 |
|
|
241 |
t.TraceAppendFullName(this->sampleDescriptor,false);
|
|
242 |
*size += this->sampleDescriptor.Size();
|
|
243 |
|
|
244 |
// copy the 4 bytes from the thread id field
|
|
245 |
this->sampleDescriptor.Append((TUint8*)&(t.iId),sizeof(TUint));
|
|
246 |
*size += sizeof(TUint);
|
|
247 |
|
|
248 |
// the size is the descriptor length + the size field
|
|
249 |
LOGSTRING2("Name size - %d",*size);
|
|
250 |
return ((TInt)(*size))+1;
|
|
251 |
}
|
|
252 |
|
|
253 |
|
|
254 |
inline TInt DPriSamplerImpl::EncodeChunkData(DThread& t)
|
|
255 |
{
|
|
256 |
LOGTEXT("PriSamplerImpl::EncodeChunkData - entry");
|
|
257 |
LOGSTRING2("PriSamplerImpl::EncodeChunkData - processing thread 0x%x ",&t);
|
|
258 |
|
|
259 |
// the size of the following name is in the first byte
|
|
260 |
TUint8* size = &sample[0];
|
|
261 |
*size = 0;
|
|
262 |
this->sampleDescriptor.Zero();
|
|
263 |
|
|
264 |
LOGTEXT("PriSamplerImpl::EncodeChunkData - cleared");
|
|
265 |
|
|
266 |
// append the thread id
|
|
267 |
this->sampleDescriptor.Append((TUint8*)&(t.iId),sizeof(TUint));
|
|
268 |
*size += sizeof(TUint);
|
|
269 |
|
|
270 |
// NKern::LockSystem();
|
|
271 |
// TInt priority(-1);
|
|
272 |
// if(&t && t.Open()== KErrNone)
|
|
273 |
// {
|
|
274 |
// priority = t.iDefaultPriority;
|
|
275 |
// }
|
|
276 |
// NKern::UnlockSystem();
|
|
277 |
|
|
278 |
// append the priority of the nanokernel fast semaphore
|
|
279 |
// this->sampleDescriptor.Append((TUint8*)&priority,sizeof(TUint8));
|
|
280 |
// append the priority of the nanokernel fast semaphore
|
|
281 |
this->sampleDescriptor.Append((TUint8*)&(t.iNThread.iPriority),sizeof(TUint8));
|
|
282 |
// add space because EKA-1 implementation needs it
|
|
283 |
this->sampleDescriptor.Append((TUint8)0x0);
|
|
284 |
*size += 2*sizeof(TUint8);
|
|
285 |
|
|
286 |
LOGTEXT("PriSamplerImpl::EncodeChunkData - appended priority");
|
|
287 |
|
|
288 |
|
|
289 |
LOGSTRING2("Data size - %d",*size);
|
|
290 |
return ((TInt)(*size))+1;
|
|
291 |
}
|
|
292 |
|
|
293 |
|
|
294 |
void DPriSamplerImpl::Reset()
|
|
295 |
{
|
|
296 |
/*
|
|
297 |
*
|
|
298 |
* EKA-2 implementation of PRI trace
|
|
299 |
*
|
|
300 |
*/
|
|
301 |
|
|
302 |
LOGTEXT("PriSamplerImpl::Reset");
|
|
303 |
iCountti = 50; // sample threads 16 cycles before actual MEM and PRI sample time...
|
|
304 |
this->iThreadCount = 0;
|
|
305 |
this->iNewThreadCount = 0;
|
|
306 |
this->iProcessing = ENothingToProcess;
|
|
307 |
this->sampleDescriptor.Zero();
|
|
308 |
|
|
309 |
|
|
310 |
// clear all thread tags
|
62
|
311 |
NKern::ThreadEnterCS(); // Prevent us from dying or suspending whilst holding a DMutex
|
20
|
312 |
DObjectCon* threads = Kern::Containers()[EThread];
|
62
|
313 |
threads->Wait(); // Obtain the container mutex so the list does get changed under us
|
|
314 |
|
20
|
315 |
TInt totalThreadCount = threads->Count();
|
|
316 |
for(TInt i=0;i<totalThreadCount;i++)
|
|
317 |
{
|
|
318 |
DThread* t = (DThread*)(*threads)[i];
|
|
319 |
TAG(t) = (TAG(t) & 0xfffffffd);
|
|
320 |
}
|
62
|
321 |
threads->Signal();
|
|
322 |
NKern::ThreadLeaveCS(); // End of critical section
|
|
323 |
}
|
20
|
324 |
|