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 <f32file.h> |
|
20 #include <e32svr.h> |
|
21 |
|
22 #include <piprofiler/EngineUIDs.h> |
|
23 #include <piprofiler/ProfilerTraces.h> |
|
24 |
|
25 // constants |
|
26 const TInt KInitialFreeBufferAmount = 4; |
|
27 |
|
28 /* |
|
29 * |
|
30 * Class CProfilerBufferHandler implementation |
|
31 * |
|
32 */ |
|
33 inline CProfilerBufferHandler* CProfilerBufferHandler::NewL(CProfilerSampleStream& aStream, RPluginSampler& aSampler) |
|
34 { |
|
35 LOGTEXT(_L("CProfilerBufferHandler::NewL - entry")); |
|
36 CProfilerBufferHandler* self = new(ELeave) CProfilerBufferHandler(aStream, aSampler); |
|
37 CleanupStack::PushL(self); |
|
38 self->ConstructL(); |
|
39 CleanupStack::Pop(self); |
|
40 return self; |
|
41 } |
|
42 |
|
43 inline CProfilerBufferHandler::CProfilerBufferHandler(CProfilerSampleStream& aStream, RPluginSampler& aSampler) |
|
44 : CActive(EPriorityStandard), |
|
45 iSampler(aSampler), |
|
46 iObserver(aStream) |
|
47 { |
|
48 } |
|
49 |
|
50 inline CProfilerBufferHandler::~CProfilerBufferHandler() |
|
51 { |
|
52 LOGTEXT(_L("CProfilerBufferHandler::~CProfilerBufferHandler() - entry")); |
|
53 } |
|
54 |
|
55 inline void CProfilerBufferHandler::ConstructL() |
|
56 { |
|
57 LOGTEXT(_L("CProfilerBufferHandler::ConstructL - entry")); |
|
58 iBufferInProcess = 0; |
|
59 iEndOfStreamDetected = false; |
|
60 |
|
61 iFinished = 0; |
|
62 // add the buffer handler to the active scheduler |
|
63 CActiveScheduler::Add(this); |
|
64 } |
|
65 |
|
66 inline void CProfilerBufferHandler::StartReceivingData() |
|
67 { |
|
68 LOGTEXT(_L("CProfilerBufferHandler::StartReceivingData - entry")); |
|
69 |
|
70 iEndOfStreamDetected = false; |
|
71 // this method initiates receiving data from the sampler |
|
72 iBufferInProcess = iObserver.GetNextFreeBuffer(); |
|
73 |
|
74 LOGSTRING5("CProfilerBufferHandler::StartReceivingData - 0x%x -> b:0x%x s:%d d:%d", |
|
75 iBufferInProcess, |
|
76 iBufferInProcess->iBuffer, |
|
77 iBufferInProcess->iBufferSize, |
|
78 iBufferInProcess->iDataSize); |
|
79 |
|
80 iSampler.FillThisStreamBuffer(iBufferInProcess,iStatus); |
|
81 |
|
82 LOGTEXT(_L("CProfilerBufferHandler::StartReceivingData - SetActive")); |
|
83 SetActive(); |
|
84 |
|
85 LOGTEXT(_L("CProfilerBufferHandler::StartReceivingData - exit")); |
|
86 } |
|
87 |
|
88 inline TInt CProfilerBufferHandler::RunError(TInt aError) |
|
89 { |
|
90 // handle the error case by stopping the trace |
|
91 HandleEndOfStream(); |
|
92 return aError; |
|
93 } |
|
94 |
|
95 inline void CProfilerBufferHandler::HandleEndOfStream() |
|
96 { |
|
97 LOGTEXT(_L("CProfilerBufferHandler::RunError - entry")); |
|
98 // Cancel has been called, the stream should be about to end now, |
|
99 // we will wait for the rest of the buffers to be filled synchronously |
|
100 // the end of the stream will be indicated through an empty buffer |
|
101 // at first, complete the ongoing request |
|
102 if(iStatus == KRequestPending && iBufferInProcess != 0) |
|
103 { |
|
104 LOGTEXT(_L("CProfilerBufferHandler::DoCancel - case 1")); |
|
105 |
|
106 // wait for the buffer to be filled synchronously |
|
107 User::WaitForRequest(iStatus); |
|
108 |
|
109 // add the received buffer to the list of filled buffers |
|
110 iObserver.AddToFilledBuffers(iBufferInProcess); |
|
111 // continue writing to output |
|
112 iObserver.NotifyWriter(); |
|
113 |
|
114 if(iBufferInProcess->iDataSize == 0) |
|
115 { |
|
116 // a buffer with size 0 was received |
|
117 LOGTEXT(_L("CProfilerBufferHandler::DoCancel - case 1.1")); |
|
118 iEndOfStreamDetected = true; |
|
119 } |
|
120 |
|
121 // there will be no more asynchronous requests |
|
122 iBufferInProcess = 0; |
|
123 } |
|
124 else if (iBufferInProcess != 0) |
|
125 { |
|
126 LOGTEXT(_L("CProfilerBufferHandler::DoCancel - case 2")); |
|
127 |
|
128 // add the buffer into filled, i.e. ready-to-write buffers |
|
129 iObserver.AddToFilledBuffers(iBufferInProcess); |
|
130 iObserver.NotifyWriter(); |
|
131 |
|
132 if(iBufferInProcess->iDataSize == 0) |
|
133 { |
|
134 // a buffer with size 0 was received |
|
135 LOGTEXT(_L("CProfilerBufferHandler::DoCancel - case 2.1")); |
|
136 iEndOfStreamDetected = true; |
|
137 } |
|
138 // there will be no more asynchronous requests |
|
139 iBufferInProcess = 0; |
|
140 } |
|
141 |
|
142 // then, continue until end of stream has been reached |
|
143 while(iEndOfStreamDetected == false) |
|
144 { |
|
145 // the end of stream has not yet been detected, so get more |
|
146 // buffers from the sampler, until we get an empty one |
|
147 |
|
148 if(iStatus == KRequestPending) |
|
149 { |
|
150 LOGTEXT(_L("CProfilerBufferHandler::DoCancel - ERROR 1")); |
|
151 } |
|
152 |
|
153 LOGTEXT(_L("CProfilerBufferHandler::DoCancel - case 3")); |
|
154 |
|
155 TBapBuf* nextFree = iObserver.GetNextFreeBuffer(); |
|
156 iSampler.FillThisStreamBuffer(nextFree,iStatus); |
|
157 // wait for the buffer to be filled synchronously |
|
158 User::WaitForRequest(iStatus); |
|
159 |
|
160 // call the writer plugin to write data to output |
|
161 iObserver.AddToFilledBuffers(nextFree); |
|
162 iObserver.NotifyWriter(); |
|
163 |
|
164 // check if end-of-data message (i.e. data size is 0 sized) received |
|
165 if(nextFree->iDataSize == 0) |
|
166 { |
|
167 LOGTEXT(_L("CProfilerBufferHandler::DoCancel - case 3.1")); |
|
168 // a buffer with size 0 was received |
|
169 iEndOfStreamDetected = true; |
|
170 nextFree = 0; |
|
171 } |
|
172 } |
|
173 } |
|
174 |
|
175 inline void CProfilerBufferHandler::RunL() |
|
176 { |
|
177 LOGTEXT(_L("CProfilerBufferHandler::RunL - entry")); |
|
178 |
|
179 // is called by the active scheduler |
|
180 // when a buffer has been received |
|
181 |
|
182 // buffer with dataSize 0 is returned when the sampling ends |
|
183 if(iBufferInProcess->iDataSize != 0) |
|
184 { |
|
185 LOGTEXT(_L("CProfilerBufferHandler::RunL - buffer received")); |
|
186 |
|
187 TBapBuf* nextFree = iObserver.GetNextFreeBuffer(); |
|
188 |
|
189 LOGSTRING5("CProfilerBufferHandler::RunL - 0x%x -> b:0x%x s:%d d:%d", |
|
190 nextFree, |
|
191 nextFree->iBuffer, |
|
192 nextFree->iBufferSize, |
|
193 nextFree->iDataSize); |
|
194 |
|
195 iSampler.FillThisStreamBuffer(nextFree,iStatus); |
|
196 |
|
197 LOGTEXT(_L("CProfilerBufferHandler::RunL - issued new sample command")); |
|
198 |
|
199 // add the received buffer to the list of filled buffers |
|
200 iObserver.AddToFilledBuffers(iBufferInProcess); |
|
201 iObserver.NotifyWriter(); |
|
202 |
|
203 // the empty buffer is now the one being processed |
|
204 iBufferInProcess = nextFree; |
|
205 |
|
206 LOGTEXT(_L("CProfilerBufferHandler::RunL - SetActive")); |
|
207 SetActive(); |
|
208 } |
|
209 else |
|
210 { |
|
211 LOGTEXT(_L("CProfilerBufferHandler::RunL - end of stream detected")); |
|
212 iEndOfStreamDetected = true; |
|
213 |
|
214 // add the empty buffer to the writer so that it will also get the information |
|
215 // about the finished stream |
|
216 iObserver.AddToFilledBuffers(iBufferInProcess); |
|
217 iObserver.NotifyWriter(); |
|
218 |
|
219 iBufferInProcess = 0; |
|
220 Cancel(); |
|
221 |
|
222 } |
|
223 LOGTEXT(_L("CProfilerBufferHandler::RunL - exit")); |
|
224 } |
|
225 |
|
226 inline void CProfilerBufferHandler::DoCancel() |
|
227 { |
|
228 LOGTEXT(_L("CProfilerBufferHandler::DoCancel - entry")); |
|
229 HandleEndOfStream(); |
|
230 LOGTEXT(_L("CProfilerBufferHandler::DoCancel - exit")); |
|
231 } |
|
232 |
|
233 |
|
234 /* |
|
235 * |
|
236 * Class CProfilerSampleStream implementation |
|
237 * |
|
238 * - used by Plugin |
|
239 **/ |
|
240 |
|
241 inline CProfilerSampleStream* CProfilerSampleStream::NewL(TInt aBufSize) |
|
242 { |
|
243 LOGTEXT(_L("CProfilerSampleStream::NewL - entry")); |
|
244 CProfilerSampleStream* self = new(ELeave) CProfilerSampleStream(aBufSize); |
|
245 CleanupStack::PushL(self); |
|
246 self->ConstructL(); |
|
247 CleanupStack::Pop(self); |
|
248 return self; |
|
249 } |
|
250 |
|
251 inline CProfilerSampleStream::CProfilerSampleStream(TInt aBufSize) : |
|
252 iBufferSize(aBufSize) |
|
253 { |
|
254 LOGTEXT(_L("CProfilerSampleStream::CProfilerSampleStream - entry")); |
|
255 |
|
256 iFilledBuffers = 0; |
|
257 iFreeBuffers = 0; |
|
258 iFinished = 0; |
|
259 |
|
260 LOGTEXT(_L("CProfilerSampleStream::CProfilerSampleStream - exit")); |
|
261 } |
|
262 |
|
263 inline CProfilerSampleStream::~CProfilerSampleStream() |
|
264 { |
|
265 LOGTEXT(_L("CProfilerSampleStream::~CProfilerSampleStream - entry")); |
|
266 |
|
267 // empty all buffers |
|
268 EmptyBuffers(); |
|
269 |
|
270 LOGTEXT(_L("CProfilerSampleStream::~CProfilerSampleStream - exit")); |
|
271 } |
|
272 |
|
273 inline void CProfilerSampleStream::ConstructL() |
|
274 { |
|
275 |
|
276 } |
|
277 |
|
278 inline void CProfilerSampleStream::SetWriter(CWriterPluginInterface& aWriter) |
|
279 { |
|
280 // set writer plugin |
|
281 iWriter = &aWriter; |
|
282 } |
|
283 |
|
284 inline void CProfilerSampleStream::Finalise() |
|
285 { |
|
286 LOGTEXT(_L("CProfilerSampleStream::Finalise - entry")); |
|
287 } |
|
288 |
|
289 inline void CProfilerSampleStream::ResetBuffers() |
|
290 { |
|
291 |
|
292 // empty buffers |
|
293 EmptyBuffers(); |
|
294 |
|
295 // re-initialise buffers |
|
296 InitialiseBuffers(); |
|
297 } |
|
298 |
|
299 inline void CProfilerSampleStream::InitialiseBuffers() |
|
300 { |
|
301 // re-initialize member variables |
|
302 iFilledBuffers = 0; |
|
303 iFreeBuffers = 0; |
|
304 iFinished = 0; |
|
305 |
|
306 // create three(orig. two) new TBapBuf objects and add them to the |
|
307 // list of free buffers |
|
308 for(TInt i(0);i<KInitialFreeBufferAmount;i++) |
|
309 { |
|
310 // alloc new buffer |
|
311 TBapBuf* newBuf = (TBapBuf*)User::Alloc(sizeof(TBapBuf)); |
|
312 newBuf->iBuffer = (TUint8*)User::Alloc(iBufferSize); |
|
313 |
|
314 // initialize the new buffer |
|
315 newBuf->iBufferSize = iBufferSize; |
|
316 newBuf->iDataSize = 0; |
|
317 newBuf->iNext = 0; |
|
318 newBuf->iDes = new TPtr8((TUint8*)newBuf,sizeof(TBapBuf)); |
|
319 newBuf->iDes->SetLength(sizeof(TBapBuf)); |
|
320 newBuf->iBufDes = new TPtr8((TUint8*)newBuf->iBuffer,iBufferSize); |
|
321 newBuf->iBufDes->SetLength(iBufferSize); |
|
322 AddToFreeBuffers(newBuf); |
|
323 } |
|
324 } |
|
325 |
|
326 inline void CProfilerSampleStream::EmptyBuffers() |
|
327 { |
|
328 LOGTEXT(_L("CProfilerSampleStream::EmptyBuffers - entry")); |
|
329 |
|
330 // delete all free buffers |
|
331 while(iFreeBuffers != 0) |
|
332 { |
|
333 LOGSTRING2("CProfilerSampleStream::EmptyBuffers - deleting 0x%x",iFreeBuffers); |
|
334 |
|
335 // store the next buffer in the list |
|
336 TBapBuf* nextFree = iFreeBuffers->iNext; |
|
337 // delete the first one in the list |
|
338 delete iFreeBuffers->iBufDes; |
|
339 delete iFreeBuffers->iDes; |
|
340 delete iFreeBuffers->iBuffer; |
|
341 delete iFreeBuffers; |
|
342 // set the list start to the next buffer |
|
343 iFreeBuffers = nextFree; |
|
344 } |
|
345 iFreeBuffers = 0; |
|
346 LOGTEXT(_L("CProfilerSampleStream::EmptyBuffers - exit")); |
|
347 } |
|
348 |
|
349 inline TBapBuf* CProfilerSampleStream::GetNextFreeBuffer() |
|
350 { |
|
351 LOGTEXT(_L("CProfilerSampleStream::GetNextFreeBuffer - entry")); |
|
352 |
|
353 // get a new buffer from the free buffers list |
|
354 TBapBuf* nextFree = iFreeBuffers; |
|
355 |
|
356 // check if we got a buffer or not |
|
357 if(nextFree == 0) |
|
358 { |
|
359 // if there are no free buffers, |
|
360 // create a new one |
|
361 LOGTEXT(_L("CProfilerSampleStream::GetNextFreeBuffer - creating new buffer")); |
|
362 TBapBuf* newBuf = (TBapBuf*)User::Alloc(sizeof(TBapBuf)); |
|
363 if(newBuf != 0) |
|
364 { |
|
365 newBuf->iBuffer = (TUint8*)User::Alloc(iBufferSize); |
|
366 if(newBuf->iBuffer != 0) |
|
367 { |
|
368 newBuf->iBufferSize = iBufferSize; |
|
369 newBuf->iDataSize = 0; |
|
370 newBuf->iNext = 0; |
|
371 newBuf->iDes = new TPtr8((TUint8*)newBuf,sizeof(TBapBuf)); |
|
372 newBuf->iDes->SetLength(sizeof(TBapBuf)); |
|
373 newBuf->iBufDes = new TPtr8((TUint8*)newBuf->iBuffer,iBufferSize); |
|
374 newBuf->iBufDes->SetLength(iBufferSize); |
|
375 nextFree = newBuf; |
|
376 } |
|
377 else |
|
378 { |
|
379 LOGTEXT(_L("CProfilerSampleStream::GetNextFreeBuffer - Out of memory (1)!!")); |
|
380 return 0; |
|
381 } |
|
382 } |
|
383 else |
|
384 { |
|
385 LOGTEXT(_L("CProfilerSampleStream::GetNextFreeBuffer - Out of memory (2)!!")); |
|
386 delete newBuf; |
|
387 return 0; |
|
388 } |
|
389 } |
|
390 else |
|
391 { |
|
392 // set the list to point to next free buffer |
|
393 iFreeBuffers = nextFree->iNext; |
|
394 } |
|
395 |
|
396 LOGTEXT(_L("CProfilerSampleStream::GetNextFreeBuffer - exit")); |
|
397 return nextFree; |
|
398 } |
|
399 |
|
400 inline void CProfilerSampleStream::AddToFilledBuffers(TBapBuf* aFilledBuffer) |
|
401 { |
|
402 LOGSTRING2(_L("CProfilerSampleStream::AddToFilledBuffers - entry, size %d"), aFilledBuffer->iDataSize); |
|
403 |
|
404 // add this buffer to the list of filled buffers |
|
405 if(iFilledBuffers == 0) |
|
406 { |
|
407 // the list is empty, so add the the beginning of the list |
|
408 // there is no next buffer in the list at the moment |
|
409 aFilledBuffer->iNext = 0; |
|
410 iFilledBuffers = aFilledBuffer; |
|
411 } |
|
412 else |
|
413 { |
|
414 // there are buffers in the list, add this buffer to the beginning of the list |
|
415 aFilledBuffer->iNext = iFilledBuffers; |
|
416 iFilledBuffers = aFilledBuffer; |
|
417 } |
|
418 LOGTEXT(_L("CProfilerSampleStream::AddToFilledBuffers - exit")); |
|
419 } |
|
420 |
|
421 TBapBuf* CProfilerSampleStream::GetNextFilledBuffer() |
|
422 { |
|
423 LOGTEXT(_L("CProfilerSampleStream::GetNextFilledBuffer - entry")); |
|
424 |
|
425 if(iFilledBuffers == 0) |
|
426 { |
|
427 // there are no filled buffers in the list |
|
428 LOGTEXT(_L("CProfilerSampleStream::GetNextFilledBuffer - no filled bufs")); |
|
429 return 0; |
|
430 } |
|
431 else |
|
432 { |
|
433 // get a buffer from the end of the list |
|
434 TBapBuf* buf = iFilledBuffers; |
|
435 TBapBuf* prev = 0; |
|
436 |
|
437 if(buf->iNext == 0) |
|
438 { |
|
439 // this was the last (and only) buffer |
|
440 iFilledBuffers = 0; |
|
441 LOGTEXT(_L("CProfilerSampleStream::GetNextFilledBuffer - last filled")); |
|
442 return buf; |
|
443 } |
|
444 else |
|
445 { |
|
446 LOGTEXT(_L("CProfilerSampleStream::GetNextFilledBuffer - searching last filled")); |
|
447 while(buf->iNext != 0) |
|
448 { |
|
449 // there are two or more buffers in the list |
|
450 // proceed until the end of the list is found |
|
451 prev = buf; |
|
452 buf = buf->iNext; |
|
453 } |
|
454 // now buf->next is 0, return buf and set the next |
|
455 // element of prev to NULL |
|
456 prev->iNext = 0; |
|
457 LOGTEXT(_L("CProfilerSampleStream::GetNextFilledBuffer - found last")); |
|
458 return buf; |
|
459 } |
|
460 } |
|
461 } |
|
462 |
|
463 inline void CProfilerSampleStream::AddToFreeBuffers(TBapBuf* aFreeBuffer) |
|
464 { |
|
465 LOGTEXT(_L("CProfilerSampleStream::AddToFreeBuffers - entry")); |
|
466 |
|
467 // set data size of the buffer to 0 |
|
468 aFreeBuffer->iDataSize = 0; |
|
469 |
|
470 // add this buffer to the list of free buffers |
|
471 if(iFreeBuffers == 0) |
|
472 { |
|
473 // this is the first buffer, so set the next to point to NULL |
|
474 aFreeBuffer->iNext = 0; |
|
475 } |
|
476 else |
|
477 { |
|
478 // otherwise set to point to the beginning of the list |
|
479 aFreeBuffer->iNext = iFreeBuffers; |
|
480 } |
|
481 |
|
482 // set this buffer to be the first one in the list |
|
483 iFreeBuffers = aFreeBuffer; |
|
484 |
|
485 LOGTEXT(_L("CProfilerSampleStream::AddToFreeBuffers - exit")); |
|
486 } |
|
487 |
|
488 void CProfilerSampleStream::NotifyWriter() |
|
489 { |
|
490 // notify writer plugin to write data from filled buffer list |
|
491 LOGTEXT(_L("CProfilerSampleStream::NotifyWriter() - entry")); |
|
492 iWriter->WriteData(); |
|
493 LOGTEXT(_L("CProfilerSampleStream::NotifyWriter() - exit")); |
|
494 } |
|
495 |
|
496 // end of file |
|
497 |
|
498 |
|
499 |
|