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