sysanadatacapture/piprofiler/piprofiler_api/inc/ProfilerSession.h
changeset 2 6a82cd05fb1e
parent 1 3ff3fecb12fe
equal deleted inserted replaced
1:3ff3fecb12fe 2:6a82cd05fb1e
     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_SESSION_H__
       
    20 #define __PROFILER_SESSION_H__
       
    21 
       
    22 #include <e32base.h>
       
    23 #include <e32svr.h>
       
    24 #include <piprofiler/ProfilerConfig.h>
       
    25 #include <piprofiler/ProfilerVersion.h>
       
    26 #include <piprofiler/ProfilerTraces.h>
       
    27 #include <piprofiler/ProfilerAttributes.h>
       
    28 
       
    29 _LIT(KProfilerName,"PIProfilerEngine");
       
    30 
       
    31 /**
       
    32  * The RProfiler class provides a functional interface to the sampling profiler.
       
    33  *
       
    34  * The engine must already be running for this interface to work, this can be
       
    35  * achieved by executing PIProfilerEngine.exe. The control methods are all static, and 
       
    36  * require no other context.
       
    37  */
       
    38 NONSHARABLE_CLASS( RProfiler ) : public RSessionBase
       
    39 	{
       
    40 public:
       
    41 	enum TCommand {	
       
    42 		// profiler controls
       
    43 		EStartSampling = 1,
       
    44 		EStopSampling,
       
    45 //		ELoadSettings,
       
    46 		EExitProfiler,
       
    47 		EFinalise,
       
    48 
       
    49 		// general attributes
       
    50 		EGetGeneralAttributes,
       
    51 		EGetSamplerAttributes,
       
    52 		EGetSamplerAttributeCount,
       
    53 		ESetGeneralAttributes,
       
    54 		ESetSamplerAttributes,
       
    55 		
       
    56 		// generic settings
       
    57 		ESetTraceFilePrefix, 
       
    58 		ESetTraceFileDrive,
       
    59 		EGetSamplerVersion,
       
    60 		EGetFileName,
       
    61 		EGetActiveWriter,
       
    62 		ERefreshProfilerStatus
       
    63 		};
       
    64 	
       
    65 	enum TSamplerState {
       
    66 		EIdle = 0, 
       
    67 		EInitializing, 
       
    68 		ERunning, 
       
    69 		EStopping, 
       
    70 		EStopped
       
    71 		};
       
    72 public:
       
    73 	
       
    74 	
       
    75 	/*
       
    76 	 * 
       
    77 	 *  Methods for commanding Profiler Engine
       
    78 	 *   
       
    79 	 */
       
    80 	
       
    81 	// get general settings
       
    82 	static inline TInt GetGeneralAttributes(TGeneralAttributes& aAttributes);
       
    83 	
       
    84 	// get samplers; names, settings, states, descriptions etc. in one array containing all sampler attributes
       
    85 	static inline TInt GetSamplerAttributes(CArrayFixFlat<TSamplerAttributes>& aAttributeArray);
       
    86 	
       
    87 	// save settings back to engine and plugins
       
    88 	static inline TInt SetGeneralAttributes(TGeneralAttributes aAttributes);
       
    89 
       
    90 	// save settings back to engine and plugins
       
    91     static inline TInt SetSamplerAttributes(TSamplerAttributes aAttributes);
       
    92     
       
    93     // refresh profiler engine status
       
    94     static inline TInt RefreshProfilerStatus();	
       
    95 	
       
    96     /** Start sampling */
       
    97 	static inline TInt StartSampling();
       
    98 	/** Stop sampling */
       
    99 	static inline TInt StopSampling();
       
   100     /** Load settings */
       
   101 //    static inline TInt LoadSettings(TDesC& aSettingsFile);
       
   102 	/** Exit profiler */
       
   103 	static inline TInt ExitProfiler();
       
   104 
       
   105 	/** Get file name */
       
   106 	static inline TInt GetFileName(TDes&);
       
   107     /** Get active writer */
       
   108     static inline TInt GetActiveWriter(TDes&);
       
   109 	/** Perform a test case */
       
   110 	static inline TInt Test(TUint32 testCase);
       
   111 	/** Issue a control request to the engine without data*/
       
   112 	static inline TInt ControlState(TCommand aRequest);
       
   113 	/** Issue a control request to the engine with descriptor data to write there*/
       
   114 	static inline TInt ControlWrite(TCommand aRequest,TDesC& data);
       
   115 	/** Issue a control request to the engine with numeric and descriptor data to write there*/
       
   116 	static inline TInt ControlWrite(TCommand aRequest,TInt numData, TDesC& data);
       
   117 	/** Issue a control request to read descriptor data from the engine*/
       
   118 	static inline TInt ControlRead(TCommand aRequest,TDes& data);
       
   119 	/** Actually sends the message to profiler engine*/	
       
   120 	static inline TInt PerformControl(	TCommand aRequest,
       
   121 										TDesC* fromDescriptor = NULL,
       
   122 										TUint32 numData1 = 0,
       
   123 										TDes* toDescriptor = NULL,
       
   124 										TUint32 numData2 = 0xffffffff);
       
   125 
       
   126 private:
       
   127 	inline RProfiler();
       
   128 	};
       
   129 
       
   130 inline RProfiler::RProfiler()
       
   131 	{}
       
   132 
       
   133 //
       
   134 // Connect to the profiler engine, and issue the control request if successful
       
   135 //
       
   136 inline TInt RProfiler::ControlState(TCommand aRequest)
       
   137     { return PerformControl(aRequest); }
       
   138 
       
   139 //
       
   140 // Connect to the profiler engine, and issue the control request if successful
       
   141 //
       
   142 inline TInt RProfiler::ControlWrite(TCommand aRequest,TDesC& data)
       
   143     { return PerformControl(aRequest,&data,0); }
       
   144 
       
   145 //
       
   146 // Connect to the profiler engine, and issue the control request if successful
       
   147 //
       
   148 inline TInt RProfiler::ControlWrite(TCommand aRequest,TInt numData, TDesC& data)
       
   149     { return PerformControl(aRequest,&data,numData); }
       
   150 
       
   151 //
       
   152 // Connect to the profiler engine, and issue the control request if successful
       
   153 //
       
   154 inline TInt RProfiler::ControlRead(TCommand aRequest,TDes& data)
       
   155     { return PerformControl(aRequest,0,0,&data); }
       
   156 
       
   157 inline TInt RProfiler::PerformControl(TCommand aRequest,TDesC* fromDescriptor,TUint32 numData1,TDes* toDescriptor,TUint32 numData2)
       
   158 	{
       
   159 	LOGTEXT(_L("Creating a session to profiler"));
       
   160 	RProfiler p;
       
   161 	TUint count(0);
       
   162 	TInt r(KErrNone);
       
   163 
       
   164 	// in boot measurement mode, count until 30s
       
   165 	#ifdef PROFILER_BOOT_MEASUREMENT
       
   166 	while(count < 60)
       
   167 	#else
       
   168 	while(count < 6)
       
   169 	#endif
       
   170 		{
       
   171 		r = p.CreateSession(KProfilerName, TVersion(), 0);
       
   172 		if (r == KErrNone)
       
   173 			{
       
   174 			LOGSTRING2("Succeeded, sending a message %d", aRequest);
       
   175 			LOGSTRING5(" - parameters 0x%x 0x%x 0x%x 0x%x",fromDescriptor,numData1,toDescriptor,numData2);
       
   176 			TInt err = KErrNone;
       
   177 
       
   178 			TIpcArgs a;
       
   179 			a.Set(0,fromDescriptor);
       
   180 			a.Set(1,numData1);
       
   181 			a.Set(2,toDescriptor);
       
   182 			a.Set(3,numData2);
       
   183 			err = p.SendReceive(aRequest,a);
       
   184 
       
   185 			p.RSessionBase::Close();
       
   186 			
       
   187 			if(err != KErrNone)
       
   188 				{
       
   189 				LOGSTRING2("RProfiler responded with an error - code %d !!",err);		
       
   190 				return err;
       
   191 				}
       
   192 			else
       
   193 				{ 
       
   194 				LOGTEXT(_L("OK, message sent, closing"));
       
   195 				return KErrNone;
       
   196 				}
       
   197 			}
       
   198 
       
   199 		LOGSTRING2("Error in opening session to profiler - code %d !!",r);
       
   200 		//#ifdef PROFILER_BOOT_MEASUREMENT
       
   201 		// there was an error contacting the Profiler
       
   202 		// indicates that the server is most probably not up
       
   203 		// however, since it should be(unless some resource is not yet ready)
       
   204 		// we can just wait
       
   205 		User::After(500000); // wait 1/2 s
       
   206 		count++;
       
   207 		//#else
       
   208 		// exit immediately on error
       
   209 		//return r;
       
   210 		//#endif
       
   211 		}
       
   212 	return r;
       
   213 	}
       
   214 
       
   215 // the new UI access methods
       
   216 inline TInt RProfiler::GetGeneralAttributes(TGeneralAttributes& aAttributes)
       
   217     {
       
   218 #ifdef _TEST_
       
   219     _LIT(KDefaultTraceOutput,"debug_output");
       
   220     _LIT(KDefaultTraceFilePrefix,"PIProfiler_#");
       
   221     _LIT(KDefaultTraceFileSaveDrive,"E:\\");
       
   222     aAttributes.iTraceOutput.Copy(KDefaultTraceOutput);
       
   223     aAttributes.iTraceFilePrefix.Copy(KDefaultTraceFilePrefix);
       
   224     aAttributes.iSaveFileDrive.Copy(KDefaultTraceFileSaveDrive);
       
   225 #else
       
   226     LOGTEXT(_L("Creating a session to profiler"));
       
   227     RProfiler p;
       
   228     TInt r(KErrNone);
       
   229     r = p.CreateSession(KProfilerName, TVersion(), 0);
       
   230     if (r == KErrNone)
       
   231         {
       
   232         LOGSTRING2("Succeeded, sending a message %d", EGetGeneralAttributes);
       
   233 
       
   234         TPckg<TGeneralAttributes> attrPckg(aAttributes);
       
   235         TIpcArgs a(&attrPckg);
       
   236         r = p.SendReceive(RProfiler::EGetGeneralAttributes,a);
       
   237         
       
   238         p.RSessionBase::Close();
       
   239         
       
   240         if(r != KErrNone)
       
   241             {
       
   242             LOGSTRING2("Profiler responded with an error - code %d !!",r);        
       
   243             return r;
       
   244             }
       
   245         else
       
   246             { 
       
   247             LOGTEXT(_L("OK, message sent, closing"));
       
   248             return KErrNone;
       
   249             }
       
   250         }
       
   251 #endif
       
   252     return r;   // return error code  
       
   253     }
       
   254 inline TInt RProfiler::GetSamplerAttributes(CArrayFixFlat<TSamplerAttributes>& aAttributes)
       
   255     {
       
   256 #ifdef _TEST_
       
   257     _LIT(KDefaultTraceOutput,"debug_output");
       
   258     _LIT(KDefaultTraceFilePrefix,"PIProfiler_#");
       
   259     _LIT(KDefaultTraceFileSaveDrive,"E:\\");
       
   260     aAttributes.iTraceOutput.Copy(KDefaultTraceOutput);
       
   261     aAttributes.iTraceFilePrefix.Copy(KDefaultTraceFilePrefix);
       
   262     aAttributes.iSaveFileDrive.Copy(KDefaultTraceFileSaveDrive);
       
   263 #else
       
   264     // do receive stream of TSamplerAttributes
       
   265     LOGTEXT(_L("Creating a session to profiler"));
       
   266     RProfiler p;
       
   267     TInt r(KErrNone);
       
   268     r = p.CreateSession(KProfilerName, TVersion(), 0);
       
   269     if (r == KErrNone)
       
   270         {
       
   271         TInt attrCount(0);
       
   272         TPckg<TInt> pckg(attrCount);
       
   273         TIpcArgs args(&pckg);
       
   274         
       
   275         r = p.SendReceive(RProfiler::EGetSamplerAttributeCount, args);
       
   276         
       
   277         HBufC8* buffer = HBufC8::NewL(attrCount*sizeof(TSamplerAttributes));
       
   278         TPtr8 ptr(buffer->Des());
       
   279         TIpcArgs args2(&ptr);
       
   280         r = p.SendReceive(RProfiler::EGetSamplerAttributes, args2);
       
   281         
       
   282         TInt len(ptr.Length());
       
   283         TInt pos(0);
       
   284         while (pos != len)
       
   285            {
       
   286            TPckgBuf<TSamplerAttributes> attrPckg;
       
   287            attrPckg.Copy(ptr.Mid(pos, attrPckg.Length()));
       
   288            pos += attrPckg.Length();
       
   289     
       
   290            aAttributes.AppendL(attrPckg());
       
   291            }
       
   292         
       
   293         p.RSessionBase::Close();
       
   294         
       
   295         if(r != KErrNone)
       
   296             {
       
   297             LOGSTRING2("Profiler responded with an error - code %d !!",r);        
       
   298             return r;
       
   299             }
       
   300         else
       
   301             { 
       
   302             LOGTEXT(_L("OK, message sent, closing"));
       
   303             return KErrNone;
       
   304             }
       
   305         }
       
   306 #endif
       
   307     return KErrNone; 
       
   308     }
       
   309 
       
   310 inline TInt RProfiler::SetGeneralAttributes(TGeneralAttributes aAttributes)
       
   311     {
       
   312     // do receive stream of TSamplerAttributes
       
   313     LOGTEXT(_L("Creating a session to profiler"));
       
   314     RProfiler p;
       
   315     TInt r(KErrNone);
       
   316     r = p.CreateSession(KProfilerName, TVersion(), 0);
       
   317     if (r == KErrNone)
       
   318         {
       
   319 
       
   320         TPckg<TGeneralAttributes> attrPckg(aAttributes);
       
   321         TIpcArgs a(&attrPckg);
       
   322         r = p.SendReceive(RProfiler::ESetGeneralAttributes,a);
       
   323         
       
   324         p.RSessionBase::Close();
       
   325         
       
   326         if(r != KErrNone)
       
   327             {
       
   328             LOGSTRING2("Profiler responded with an error - code %d !!",r);        
       
   329             return r;
       
   330             }
       
   331         else
       
   332             { 
       
   333             LOGTEXT(_L("OK, message sent, closing"));
       
   334             return KErrNone;
       
   335             }
       
   336         }
       
   337     return r; 
       
   338     }
       
   339 
       
   340 // save settings back to engine and plugins
       
   341 inline TInt RProfiler::SetSamplerAttributes(TSamplerAttributes aAttributes)
       
   342     {
       
   343     // do receive stream of TSamplerAttributes
       
   344     LOGTEXT(_L("Creating a session to profiler"));
       
   345     RProfiler p;
       
   346     TInt r(KErrNone);
       
   347     r = p.CreateSession(KProfilerName, TVersion(), 0);
       
   348     if (r == KErrNone)
       
   349         {
       
   350 
       
   351         TPckg<TSamplerAttributes> attrPckg(aAttributes);
       
   352         
       
   353         TIpcArgs a;
       
   354         a.Set(0,&attrPckg);
       
   355 
       
   356         r = p.SendReceive(RProfiler::ESetSamplerAttributes,a);
       
   357         
       
   358         p.RSessionBase::Close();
       
   359         
       
   360         if(r != KErrNone)
       
   361             {
       
   362             LOGSTRING2("Profiler responded with an error - code %d !!",r);        
       
   363             return r;
       
   364             }
       
   365         else
       
   366             { 
       
   367             LOGTEXT(_L("OK, message sent, closing"));
       
   368             return KErrNone;
       
   369             }
       
   370         }
       
   371     return r; 
       
   372     }
       
   373 
       
   374 inline TInt RProfiler::RefreshProfilerStatus()
       
   375     {return ControlState(RProfiler::ERefreshProfilerStatus); }
       
   376 
       
   377 inline TInt RProfiler::StartSampling()
       
   378 	{return ControlState(RProfiler::EStartSampling);}
       
   379 
       
   380 inline TInt RProfiler::StopSampling()
       
   381 	{return ControlState(RProfiler::EStopSampling);}
       
   382 
       
   383 inline TInt RProfiler::ExitProfiler()
       
   384 	{return ControlState(RProfiler::EExitProfiler);}
       
   385 
       
   386 inline TInt RProfiler::GetFileName(TDes& fileName)
       
   387 	{return ControlRead(EGetFileName,fileName);}
       
   388 
       
   389 inline TInt RProfiler::GetActiveWriter(TDes& writerDes)
       
   390     {return ControlRead(EGetActiveWriter,writerDes);}
       
   391 
       
   392 #endif // __PROFILER_SESSION_H__