|
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 "GeneralsPlugin.h" |
|
20 //#include <piprofiler/EngineUIDs.h> |
|
21 #include <piprofiler/ProfilerTraces.h> |
|
22 |
|
23 const TInt KMaxSamplerAmount = 20; |
|
24 |
|
25 // LITERALS |
|
26 _LIT8(KSamplingPeriod, "sampling_period_ms"); |
|
27 |
|
28 // CONSTANTS |
|
29 // GeneralsPlugin UID: |
|
30 const TUid KSamplerGeneralsPluginUid = { 0x2001E5B2 }; |
|
31 |
|
32 // Gpp sub-sampler UID: |
|
33 const TUid KSamplerGppPluginUid = { 0x2001E570 }; |
|
34 |
|
35 // Gfc sub-sampler UID: |
|
36 const TUid KSamplerGfcPluginUid = { 0x2001E571 }; |
|
37 |
|
38 // Itt sub-sampler UID: |
|
39 const TUid KSamplerIttPluginUid = { 0x2001E572 }; |
|
40 |
|
41 // Mem sub-sampler UID: |
|
42 const TUid KSamplerMemPluginUid = { 0x2001E573 }; |
|
43 |
|
44 // Pri sub-sampler UID: |
|
45 const TUid KSamplerPriPluginUid = { 0x2001E574 }; |
|
46 |
|
47 /* |
|
48 * |
|
49 * class CGeneralsPlugin implementation |
|
50 * |
|
51 */ |
|
52 |
|
53 CGeneralsPlugin* CGeneralsPlugin::NewL(const TUid aImplementationUid, TAny* /*aInitParams*/) |
|
54 { |
|
55 LOGTEXT(_L("CGeneralsPlugin::NewL() - entry")); |
|
56 CGeneralsPlugin* self = new (ELeave) CGeneralsPlugin(); |
|
57 CleanupStack::PushL( self ); |
|
58 self->ConstructL(); |
|
59 CleanupStack::Pop(); |
|
60 LOGTEXT(_L("CGeneralsPlugin::NewL() - exit")); |
|
61 return self; |
|
62 } |
|
63 |
|
64 CGeneralsPlugin::CGeneralsPlugin() : |
|
65 iVersionDescriptor(&(this->iVersion[1]),0,19) |
|
66 { |
|
67 iSamplerType = PROFILER_KERNEL_MODE_SAMPLER; |
|
68 iSamplerId = PROFILER_GENERALS_SAMPLER_ID; |
|
69 iBufferHandler = NULL; |
|
70 iEnabled = EFalse; |
|
71 LOGTEXT(_L("CGeneralsPlugin::CGeneralsPlugin() - constructor")); |
|
72 } |
|
73 |
|
74 void CGeneralsPlugin::ConstructL() |
|
75 { |
|
76 LOGTEXT(_L("CGeneralsPlugin::ConstructL() - entry")); |
|
77 |
|
78 // create attribute array |
|
79 iSamplerAttributes = new(ELeave) CArrayFixFlat<TSamplerAttributes>(KSubSamplerCount); |
|
80 |
|
81 TInt err = InitiateSamplerL(); |
|
82 if( err != KErrNone ) |
|
83 { |
|
84 LOGTEXT(_L("CGeneralsPlugin::ConstructL - LEAVING, failed to load open sampler device")); |
|
85 User::Leave(err); |
|
86 } |
|
87 |
|
88 // initiate sampler attributes, i.e. settings for modification |
|
89 InitiateSamplerAttributesL(); |
|
90 |
|
91 LOGTEXT(_L("CGeneralsPlugin::ConstructL() - exit")); |
|
92 } |
|
93 |
|
94 |
|
95 CGeneralsPlugin::~CGeneralsPlugin() |
|
96 { |
|
97 // clean all the members |
|
98 CleanSampler(); // clean the created sampler |
|
99 } |
|
100 |
|
101 TInt CGeneralsPlugin::InitiateSamplerL() |
|
102 { |
|
103 RThread me; |
|
104 |
|
105 LOGTEXT(_L("CGeneralsPlugin::InitiateSamplerL - #1")); |
|
106 |
|
107 me.SetPriority(EPriorityRealTime); |
|
108 |
|
109 LOGTEXT(_L("CGeneralsPlugin::InitiateSamplerL - #2")); |
|
110 |
|
111 // create |
|
112 User::FreeLogicalDevice(KPluginSamplerName); |
|
113 TInt err(KErrGeneral); |
|
114 |
|
115 LOGTEXT(_L("CGeneralsPlugin::InitiateSamplerL - #3")); |
|
116 |
|
117 err = User::LoadLogicalDevice(KPluginSamplerName); |
|
118 if(err != KErrNone) |
|
119 { |
|
120 User::Leave(err); |
|
121 } |
|
122 |
|
123 LOGTEXT(_L("CGeneralsPlugin::InitiateSamplerL - #4")); |
|
124 |
|
125 err = KErrGeneral; |
|
126 |
|
127 err = iGeneralsSampler.Open(); |
|
128 if(err != KErrNone) |
|
129 { |
|
130 LOGSTRING2("CGeneralsPlugin::InitiateSamplerL - Could not open sampler device - waiting and trying again: %d", err); |
|
131 User::Leave(err); |
|
132 } |
|
133 |
|
134 LOGTEXT(_L("CGeneralsPlugin::InitiateSamplerL - #5")); |
|
135 |
|
136 return err; |
|
137 } |
|
138 |
|
139 /* |
|
140 * |
|
141 * Default sampler attributes |
|
142 * |
|
143 */ |
|
144 void CGeneralsPlugin::InitiateSamplerAttributesL() |
|
145 { |
|
146 |
|
147 // |
|
148 for(TInt i(0);i<KMaxSamplerAmount;i++) |
|
149 { |
|
150 switch(i) |
|
151 { |
|
152 // Usage: |
|
153 // TSamplerAttributes(TUint32 aUid, |
|
154 // const TDesC8& aShortName, |
|
155 // const TDesC& aName, |
|
156 // const TDesC& aDescription, |
|
157 // TInt aSampleRate, |
|
158 // TBool aEnabled, |
|
159 // TBool aHidden, |
|
160 // TUint32 aItemCount); |
|
161 case PROFILER_GPP_SAMPLER_ID: |
|
162 { |
|
163 TSamplerAttributes attr(KSamplerGppPluginUid.iUid, |
|
164 KGPPShortName(), |
|
165 KGPPLongName(), |
|
166 KGPPDescription(), |
|
167 1, |
|
168 ETrue, |
|
169 ETrue, |
|
170 0); |
|
171 iSamplerAttributes->AppendL(attr); |
|
172 break; |
|
173 } |
|
174 case PROFILER_GFC_SAMPLER_ID: |
|
175 { |
|
176 TSamplerAttributes attr2(KSamplerGfcPluginUid.iUid, |
|
177 KGFCShortName(), |
|
178 KGFCLongName(), |
|
179 KGFCDescription(), |
|
180 -1, |
|
181 EFalse, |
|
182 EFalse, |
|
183 0); |
|
184 this->iSamplerAttributes->AppendL(attr2); |
|
185 break; |
|
186 } |
|
187 case PROFILER_ITT_SAMPLER_ID: |
|
188 { |
|
189 TSamplerAttributes attr3(KSamplerIttPluginUid.iUid, |
|
190 KITTShortName(), |
|
191 KITTLongName(), |
|
192 KITTDescription(), |
|
193 -1, |
|
194 ETrue, |
|
195 EFalse, |
|
196 0); |
|
197 this->iSamplerAttributes->AppendL(attr3); |
|
198 break; |
|
199 } |
|
200 case PROFILER_MEM_SAMPLER_ID: |
|
201 { |
|
202 TSamplerAttributes attr4(KSamplerMemPluginUid.iUid, |
|
203 KMEMShortName(), |
|
204 KMEMLongName(), |
|
205 KMEMDescription(), |
|
206 -1, |
|
207 EFalse, |
|
208 EFalse, |
|
209 0); |
|
210 // // select event or sampling based |
|
211 // attr4.iSettingItem1.iSettingDescription.Copy(KMEM1Desc); |
|
212 // attr4.iSettingItem1.iType = TSettingItem::ESettingItemTypeBool; |
|
213 // attr4.iSettingItem1.iValue.AppendNum(0, EDecimal); |
|
214 // attr4.iSettingItem1.iUIText.Copy(KMEM1UIText); |
|
215 // attr4.iSettingItem1.iSettingText.Copy(KMemCounter1); |
|
216 |
|
217 this->iSamplerAttributes->AppendL(attr4); |
|
218 break; |
|
219 } |
|
220 case PROFILER_PRI_SAMPLER_ID: |
|
221 { |
|
222 TSamplerAttributes attr5(KSamplerPriPluginUid.iUid, |
|
223 KPRIShortName(), |
|
224 KPRILongName(), |
|
225 KPRIDescription(), |
|
226 3000, |
|
227 EFalse, |
|
228 EFalse, |
|
229 0); |
|
230 this->iSamplerAttributes->AppendL(attr5); |
|
231 break; |
|
232 } |
|
233 } |
|
234 } |
|
235 } |
|
236 |
|
237 TInt CGeneralsPlugin::CleanSampler() |
|
238 { |
|
239 LOGTEXT(_L("CGeneralsPlugin::CleanSampler() - deleting buffer handler")); |
|
240 // release the buffer handler |
|
241 if(iBufferHandler) |
|
242 { |
|
243 iBufferHandler->Cancel(); |
|
244 delete iBufferHandler; |
|
245 iBufferHandler = NULL; |
|
246 } |
|
247 |
|
248 LOGTEXT(_L("CGeneralsPlugin::CleanSampler() - closing sampler")); |
|
249 iGeneralsSampler.Close(); |
|
250 |
|
251 LOGTEXT(_L("CGeneralsPlugin::CleanSampler() - Freeing sampler device")); |
|
252 User::FreeLogicalDevice(KPluginSamplerName); |
|
253 |
|
254 // release attribute array |
|
255 if(iSamplerAttributes) |
|
256 { |
|
257 iSamplerAttributes->Reset(); |
|
258 } |
|
259 delete iSamplerAttributes; |
|
260 iSamplerAttributes = NULL; |
|
261 |
|
262 LOGTEXT(_L("CGeneralsPlugin::CleanSampler() - exit")); |
|
263 |
|
264 return KErrNone; |
|
265 } |
|
266 |
|
267 // returns setting array |
|
268 void CGeneralsPlugin::GetAttributesL(CArrayFixFlat<TSamplerAttributes>* aAttributes) |
|
269 { |
|
270 TInt count(iSamplerAttributes->Count()); |
|
271 // append all sampler attributes to aAttributes array |
|
272 for(TInt i(0);i<count;i++) |
|
273 { |
|
274 aAttributes->AppendL(iSamplerAttributes->At(i)); |
|
275 } |
|
276 } |
|
277 |
|
278 TInt CGeneralsPlugin::SetAttributesL(TSamplerAttributes aAttributes) |
|
279 { |
|
280 TSamplerAttributes attr; |
|
281 |
|
282 TInt count(iSamplerAttributes->Count()); |
|
283 // loop the sub sampler attributes (by UID) |
|
284 for(TInt i(0);i<count;i++) |
|
285 { |
|
286 attr = iSamplerAttributes->At(i); |
|
287 // if UIDs match replace the old |
|
288 if(attr.iUid == aAttributes.iUid) |
|
289 { |
|
290 // replace the old attribute container |
|
291 iSamplerAttributes->Delete(i); |
|
292 iSamplerAttributes->InsertL(i, aAttributes); |
|
293 return KErrNone; |
|
294 } |
|
295 } |
|
296 return KErrNotFound; |
|
297 } |
|
298 |
|
299 /* |
|
300 * Method for parsing and transforming text array settings into TSamplerAttributes (per each sub sampler), |
|
301 * called by CSamplerController class |
|
302 * |
|
303 * @param array of raw text setting lines, e.g. [gpp]\nenabled=true\nsampling_period_ms=1\n |
|
304 */ |
|
305 TInt CGeneralsPlugin::ConvertRawSettingsToAttributes(CDesC8ArrayFlat* aAllSettingsArray) |
|
306 { |
|
307 // local literals |
|
308 _LIT8(KGPPShort, "gpp"); |
|
309 _LIT8(KGFCShort, "gfc"); |
|
310 _LIT8(KITTShort, "itt"); |
|
311 _LIT8(KMEMShort, "mem"); |
|
312 _LIT8(KPRIShort, "pri"); |
|
313 |
|
314 TInt err(KErrNone); |
|
315 |
|
316 TBuf8<16> samplerSearchName; |
|
317 |
|
318 // loop previous settings, update value if changed |
|
319 for(TInt i(0);i<KSubSamplerCount;i++) |
|
320 { |
|
321 // go through all the sub samplers |
|
322 switch (i) |
|
323 { |
|
324 case 0: |
|
325 samplerSearchName.Copy(KGPPShort); |
|
326 break; |
|
327 case 1: |
|
328 samplerSearchName.Copy(KGFCShort); |
|
329 break; |
|
330 case 2: |
|
331 samplerSearchName.Copy(KITTShort); |
|
332 break; |
|
333 case 3: |
|
334 samplerSearchName.Copy(KMEMShort); |
|
335 break; |
|
336 case 4: |
|
337 samplerSearchName.Copy(KPRIShort); |
|
338 break; |
|
339 } |
|
340 |
|
341 // get sampler specific settings |
|
342 err = DoSetSamplerSettings(aAllSettingsArray, samplerSearchName, i); |
|
343 } |
|
344 |
|
345 // returns KErrNone if settings found, otherwise KErrNotFound |
|
346 return err; |
|
347 } |
|
348 |
|
349 /** |
|
350 * Method for searching sampler specific settings among all settings (raw setting lines read from settings file) |
|
351 * |
|
352 * @param aAllSettings array of all settings from settings file |
|
353 * @param aSamplerName short name of sampler to be searched among the settings |
|
354 * @param aIndex index number of sampler specific sampler attributes (TSamplerAttributes) |
|
355 * @return KErrNone if settings found ok else KErrNotFound |
|
356 */ |
|
357 TInt CGeneralsPlugin::DoSetSamplerSettings(CDesC8ArrayFlat* aAllSettings, TDesC8& aSamplerName, TInt aIndex) |
|
358 { |
|
359 // sampler name to be searched among the all settings |
|
360 TBuf8<16> samplerSearch; |
|
361 samplerSearch.Copy(KBracketOpen); |
|
362 samplerSearch.Append(aSamplerName); |
|
363 samplerSearch.Append(KBracketClose); |
|
364 |
|
365 // read a line from ALL settings array |
|
366 for (TInt i(0); i<aAllSettings->MdcaCount(); i++) |
|
367 { |
|
368 // check if this line has a setting block start, i.e. contains [xxx] in it |
|
369 if (aAllSettings->MdcaPoint(i).CompareF(samplerSearch) == 0) |
|
370 { |
|
371 // right settings block found, now loop until the next block is found |
|
372 for(TInt j(i+1);j<aAllSettings->MdcaCount();j++) |
|
373 { |
|
374 // check if the next settings block was found |
|
375 if(aAllSettings->MdcaPoint(j).Left(1).CompareF(KBracketOpen) != 0) |
|
376 { |
|
377 // save found setting value directly to its owners attributes |
|
378 SaveSettingToAttributes(aAllSettings->MdcaPoint(j), aIndex); |
|
379 } |
|
380 else |
|
381 { |
|
382 // next block found, return KErrNone |
|
383 return KErrNone; |
|
384 } |
|
385 } |
|
386 } |
|
387 } |
|
388 // no settings found for specific sampler |
|
389 return KErrNotFound; |
|
390 } |
|
391 |
|
392 /** |
|
393 * Method for setting a specific descriptor (from settings file) to attribute structure |
|
394 * |
|
395 * @param aSetting |
|
396 * @param aName |
|
397 */ |
|
398 void CGeneralsPlugin::SaveSettingToAttributes(const TDesC8& aSetting, TInt aIndex) |
|
399 { |
|
400 // local literals |
|
401 _LIT8(KSettingItemSeparator, "="); |
|
402 |
|
403 // find the equal mark from the setting line |
|
404 TInt sepPos = aSetting.Find(KSettingItemSeparator); |
|
405 // check that '=' is found |
|
406 if (sepPos > 0) |
|
407 { |
|
408 // check that the element matches |
|
409 if (aSetting.Left(sepPos).CompareF(KEnabled) == 0) |
|
410 { |
|
411 TBool en; |
|
412 CSamplerPluginInterface::Str2Bool(aSetting.Right(aSetting.Length()-sepPos-1), en); |
|
413 if(en != iSamplerAttributes->At(aIndex).iEnabled) |
|
414 { |
|
415 iSamplerAttributes->At(aIndex).iEnabled = en; |
|
416 } |
|
417 } |
|
418 else if (aSetting.Left(sepPos).CompareF(KSamplingPeriod) == 0) |
|
419 { |
|
420 TInt sr; |
|
421 CSamplerPluginInterface::Str2Int(aSetting.Right(aSetting.Length()-sepPos-1), sr); |
|
422 if(sr != iSamplerAttributes->At(aIndex).iSampleRate) |
|
423 { |
|
424 iSamplerAttributes->At(aIndex).iSampleRate = sr; |
|
425 } |
|
426 } |
|
427 } |
|
428 } |
|
429 |
|
430 |
|
431 TUid CGeneralsPlugin::Id(TInt aSubId) const |
|
432 { |
|
433 if(aSubId == PROFILER_GPP_SAMPLER_ID) |
|
434 return KSamplerGppPluginUid; |
|
435 else if (aSubId == PROFILER_GFC_SAMPLER_ID) |
|
436 return KSamplerGfcPluginUid; |
|
437 else if (aSubId == PROFILER_ITT_SAMPLER_ID) |
|
438 return KSamplerIttPluginUid; |
|
439 else if (aSubId == PROFILER_MEM_SAMPLER_ID) |
|
440 return KSamplerMemPluginUid; |
|
441 else if (aSubId == PROFILER_PRI_SAMPLER_ID) |
|
442 return KSamplerPriPluginUid; |
|
443 else |
|
444 return KSamplerGeneralsPluginUid; |
|
445 } |
|
446 |
|
447 TInt CGeneralsPlugin::SubId(TUid aId) const |
|
448 { |
|
449 /* definitions from ProfilerConfig.h: |
|
450 * #define PROFILER_GPP_SAMPLER_ID 1 |
|
451 #define PROFILER_GFC_SAMPLER_ID 2 |
|
452 #define PROFILER_ITT_SAMPLER_ID 3 |
|
453 #define PROFILER_MEM_SAMPLER_ID 4 |
|
454 #define PROFILER_PRI_SAMPLER_ID 5 |
|
455 */ |
|
456 |
|
457 if(aId == KSamplerGppPluginUid) |
|
458 return PROFILER_GPP_SAMPLER_ID; |
|
459 else if (aId == KSamplerGfcPluginUid) |
|
460 return PROFILER_GFC_SAMPLER_ID; |
|
461 else if (aId == KSamplerIttPluginUid) |
|
462 return PROFILER_ITT_SAMPLER_ID; |
|
463 else if (aId == KSamplerMemPluginUid) |
|
464 return PROFILER_MEM_SAMPLER_ID; |
|
465 else if (aId == KSamplerPriPluginUid) |
|
466 return PROFILER_PRI_SAMPLER_ID; |
|
467 else |
|
468 return KErrNotFound; |
|
469 } |
|
470 |
|
471 TInt CGeneralsPlugin::GetSamplerType() |
|
472 { |
|
473 return iSamplerType; |
|
474 } |
|
475 |
|
476 |
|
477 void CGeneralsPlugin::InstallStreamForActiveTraces(RGeneralsSampler& sampler, CProfilerSampleStream& aStream) |
|
478 { |
|
479 // output mode for this trace is stream |
|
480 if(!iBufferHandler) |
|
481 { |
|
482 // stream object has not been created yet |
|
483 LOGTEXT(_L("CGeneralsPlugin::InstallStreamForActiveTraces - creating stream for trace")); |
|
484 |
|
485 // use a 32KB buffer to transfer data from sampler to client |
|
486 // commonStream = new RProfilerSampleStream(sampler,totalPrefix,32768); |
|
487 TRAPD(err, iBufferHandler = CProfilerBufferHandler::NewL(aStream, sampler)); |
|
488 if(err != KErrNone) |
|
489 { |
|
490 LOGTEXT(_L("CGeneralsPlugin::InstallStreamForActiveTraces() - No memory")); |
|
491 return; |
|
492 } |
|
493 } |
|
494 |
|
495 // initiate receiving of data from the sampler device driver |
|
496 if(iBufferHandler) |
|
497 { |
|
498 iBufferHandler->StartReceivingData(); |
|
499 } |
|
500 } |
|
501 |
|
502 void CGeneralsPlugin::SetSettingsToSamplers() |
|
503 { |
|
504 TSamplerAttributes attr; |
|
505 |
|
506 // loop through the setting attributes |
|
507 for(TInt i(0);i<iSamplerAttributes->Count();i++) |
|
508 { |
|
509 // get the attribute container |
|
510 attr = iSamplerAttributes->At(i); |
|
511 |
|
512 // make changes according to right sampler, NOTE! The old IDs of sub samplers (i+1)! |
|
513 if(attr.iEnabled) |
|
514 { |
|
515 iGeneralsSampler.MarkTraceActive(i+1); |
|
516 |
|
517 // set enabled |
|
518 iEnabled = ETrue; |
|
519 } |
|
520 else |
|
521 { |
|
522 iGeneralsSampler.MarkTraceInactive(i+1); |
|
523 } |
|
524 // set sampling period if available |
|
525 if(attr.iSampleRate != KErrNotFound) |
|
526 { |
|
527 iGeneralsSampler.SetSamplingPeriod(i+1, attr.iSampleRate); |
|
528 } |
|
529 } |
|
530 } |
|
531 |
|
532 TInt CGeneralsPlugin::ResetAndActivateL(CProfilerSampleStream& aStream) |
|
533 { |
|
534 // the sampler starting functionality |
|
535 LOGTEXT(_L("CGeneralsPlugin::ResetAndActivate() - entry")); |
|
536 |
|
537 // now before starting the latest settings must be set to samplers itself |
|
538 SetSettingsToSamplers(); |
|
539 |
|
540 if(Enabled()) |
|
541 { |
|
542 LOGTEXT(_L("CGeneralsPlugin::ResetAndActivate() - starting sampling...")); |
|
543 // start sampling process of enabled sub samplers |
|
544 iGeneralsSampler.StartSampling(); |
|
545 LOGTEXT(_L("CGeneralsPlugin::ResetAndActivate() - installing stream for an active trace...")); |
|
546 |
|
547 // install the trace for enabled samplers |
|
548 InstallStreamForActiveTraces(iGeneralsSampler, aStream); |
|
549 LOGSTRING2("CGeneralsPlugin::ResetAndActivate() - stream installed: 0x%x", aStream); |
|
550 } |
|
551 |
|
552 LOGTEXT(_L("CGeneralsPlugin::ResetAndActivate() - exit")); |
|
553 return KErrNone; |
|
554 } |
|
555 |
|
556 TInt CGeneralsPlugin::StopSampling() |
|
557 { |
|
558 // RDebug::Print(_L("CGeneralsPlugin::StopSampling() - Stopping sampler LDD")); |
|
559 iGeneralsSampler.StopSampling(); |
|
560 // RDebug::Print(_L("CGeneralsPlugin::StopSampling() - Sampler LDD stopped")); |
|
561 |
|
562 // check if bufferhandler has died |
|
563 if(iBufferHandler) |
|
564 { |
|
565 // RDebug::Print(_L("CGeneralsPlugin::StopSampling() - Canceling the buffer handler")); |
|
566 iBufferHandler->Cancel(); |
|
567 delete iBufferHandler; |
|
568 iBufferHandler = NULL; |
|
569 } |
|
570 // set enabled |
|
571 iEnabled = EFalse; |
|
572 // RDebug::Print(_L("CGeneralsPlugin::StopSampling() - exit")); |
|
573 return KErrNone; |
|
574 } |
|
575 |
|
576 |
|
577 |