memspyui/ui/avkon/src/MemSpySettings.cpp
branchRCL_3
changeset 22 fad26422216a
parent 21 b3cee849fa46
child 23 f8280f3bfeb7
equal deleted inserted replaced
21:b3cee849fa46 22:fad26422216a
     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 #include "MemSpySettings.h"
       
    19 
       
    20 // System includes
       
    21 #include <e32svr.h>
       
    22 #include <s32file.h>
       
    23 
       
    24 // Engine includes
       
    25 #include <memspy/engine/memspyengine.h>
       
    26 #include <memspy/engine/memspyenginelogger.h>
       
    27 #include <memspy/engine/memspyenginehelperprocess.h>
       
    28 #include <memspy/engine/memspyenginehelpersysmemtracker.h>
       
    29 
       
    30 #include <memspysession.h>
       
    31 
       
    32 // Constants
       
    33 _LIT( KMemSpySettingsFileName, "settings.dat" ); //file or avkon client settings
       
    34 
       
    35 // Version 03 dumped some of the system wide memory tracker settings
       
    36 const TInt KMemSpySettingsFileFormatVersion = 6;
       
    37 
       
    38 CMemSpySettings::CMemSpySettings( RFs& aFsSession, RMemSpySession& aSession )
       
    39 :   iFsSession( aFsSession ), iMemSpySession( aSession )
       
    40     {	
       
    41     }
       
    42 
       
    43 
       
    44 CMemSpySettings::~CMemSpySettings()
       
    45     {	
       
    46     TRACE( RDebug::Printf( "CMemSpySettings::~CMemSpySettings() - START" ) );
       
    47     TRAP_IGNORE( StoreSettingsL() );
       
    48     TRACE( RDebug::Printf( "CMemSpySettings::~CMemSpySettings() - END" ) );    	
       
    49     }
       
    50 
       
    51 
       
    52 void CMemSpySettings::ConstructL()
       
    53     {	
       
    54     TRACE( RDebug::Print( _L("CMemSpySettings::ConstructL() - START") ) );
       
    55 
       
    56     TRAP_IGNORE( RestoreSettingsL() );
       
    57 
       
    58     TRACE( RDebug::Print( _L("CMemSpySettings::ConstructL() - END") ) );    
       
    59     }
       
    60 
       
    61 CMemSpySettings* CMemSpySettings::NewL( RFs& aFsSession, RMemSpySession& aSession )
       
    62     {
       
    63     CMemSpySettings* self = new(ELeave) CMemSpySettings( aFsSession, aSession );
       
    64     CleanupStack::PushL( self );
       
    65     self->ConstructL();
       
    66     CleanupStack::Pop( self );
       
    67     return self;
       
    68     }
       
    69 
       
    70 void CMemSpySettings::GetSettingsFileNameL( TDes& aFileName )
       
    71     {
       
    72     GetSettingsPathL( aFileName );
       
    73     aFileName.Append( KMemSpySettingsFileName );
       
    74     TRACE( RDebug::Print( _L("CMemSpySettings::GetSettingsFileNameL() - aFileName: %S"), &aFileName ) );
       
    75     }
       
    76 
       
    77 
       
    78 void CMemSpySettings::GetSettingsPathL( TDes& aPath )
       
    79     {
       
    80     TRACE( RDebug::Print( _L("CMemSpySettings::GetSettingsPathL() - START") ) );
       
    81     aPath.Zero();
       
    82 
       
    83     // Get private data cage path
       
    84     TInt err = iFsSession.PrivatePath( aPath );
       
    85     TRACE( RDebug::Print( _L("CMemSpySettings::GetSettingsPathL() - priv path err: %d"), err ) );
       
    86     User::LeaveIfError( err );
       
    87 
       
    88     // Combine with C: drive
       
    89     const TDriveUnit cDrive( EDriveC );
       
    90     const TDriveName cDriveName( cDrive.Name() );
       
    91     aPath.Insert( 0, cDriveName );
       
    92 
       
    93     iFsSession.MkDirAll( aPath );
       
    94     TRACE( RDebug::Print( _L("CMemSpySettings::GetSettingsPathL() - END - %S"), &aPath ) );
       
    95     }
       
    96 
       
    97 RFile CMemSpySettings::SettingsFileLC( TBool aReplace )
       
    98     {
       
    99     TRACE( RDebug::Print( _L("CMemSpySettings::SettingsFileLC() - START - aReplace: %d"), aReplace ) );
       
   100 
       
   101     TFileName* fileName = new(ELeave) TFileName();
       
   102     CleanupStack::PushL( fileName );
       
   103     GetSettingsFileNameL( *fileName );
       
   104     TRACE( RDebug::Print( _L("CMemSpySettings::SettingsFileLC() - fileName: %S"), fileName ) );
       
   105 
       
   106     RFile file;
       
   107     TInt error = KErrNone;
       
   108     //
       
   109     if  ( aReplace )
       
   110         {
       
   111         error = file.Replace( iFsSession, *fileName, EFileWrite );
       
   112         TRACE( RDebug::Print( _L("CMemSpySettings::SettingsFileLC() - replace err: %d"), error ) );
       
   113         }
       
   114     else
       
   115         {
       
   116         error = file.Open( iFsSession, *fileName, EFileWrite );
       
   117         TRACE( RDebug::Print( _L("CMemSpySettings::SettingsFileLC() - open err: %d"), error ) );
       
   118         //
       
   119         if  ( error == KErrNotFound )
       
   120             {
       
   121             error = file.Create( iFsSession, *fileName, EFileWrite );
       
   122             }
       
   123         }
       
   124     //
       
   125     User::LeaveIfError( error );
       
   126     CleanupStack::PopAndDestroy( fileName );
       
   127     CleanupClosePushL( file );
       
   128     //
       
   129     TRACE( RDebug::Print( _L("CMemSpySettings::SettingsFileLC() - END") ) );
       
   130     return file;
       
   131     }
       
   132 
       
   133 
       
   134 void CMemSpySettings::StoreSettingsL()
       
   135     {
       
   136     TRACE( RDebug::Printf( "CMemSpySettings::StoreSettingsL() - START" ) );
       
   137 
       
   138     RFile file = SettingsFileLC( ETrue );
       
   139     RFileWriteStream stream( file );
       
   140     CleanupStack::Pop(); // file
       
   141     CleanupClosePushL( stream );
       
   142     
       
   143     // Verion info
       
   144     stream.WriteInt32L( KMemSpySettingsFileFormatVersion );
       
   145     
       
   146     stream.WriteUint8L( iSinkType );
       
   147     
       
   148     
       
   149     // Get SWMT config
       
   150     //CMemSpyEngineHelperSysMemTracker& swmt = iEngine.HelperSysMemTracker();
       
   151     //TMemSpyEngineHelperSysMemTrackerConfig swmtConfig;
       
   152     //swmt.GetConfig( swmtConfig );
       
   153 
       
   154     // Write SWMT settings
       
   155     //stream.WriteInt32L( swmtConfig.TimerPeriod().Int() );
       
   156     //stream.WriteUint8L( swmtConfig.DumpData() );
       
   157     
       
   158     stream.WriteInt32L( iSwmtConfig.TimerPeriod().Int() );
       
   159     stream.WriteUint8L( iSwmtConfig.DumpData() );
       
   160     
       
   161     // Write memory tracking auto-start process list
       
   162     /*
       
   163     const RArray<TUid>& processUidList = iEngine.HelperProcess().MemoryTrackingAutoStartProcessList();
       
   164     stream.WriteInt32L( processUidList.Count() );
       
   165     for( TInt i=0; i<processUidList.Count(); i++ )
       
   166         {
       
   167         const TUid uid = processUidList[ i ];
       
   168         TRACE( RDebug::Printf( "CMemSpySettings::StoreSettingsL() - process tracker uid[%02d]: 0x%08x", i, uid.iUid ) );
       
   169         stream << uid;
       
   170         }
       
   171     */    
       
   172     stream.WriteInt32L( iUidList.Count() );
       
   173     for( TInt i = 0; i < iUidList.Count(); i++ )
       
   174     	{
       
   175 		const TUid uid = iUidList[ i ];
       
   176 		TRACE( RDebug::Printf( "CMemSpySettings::StoreSettingsL() - process tracker uid[%02d]: 0x%08x", i, uid.iUid ) );
       
   177 		stream << uid;
       
   178     	}
       
   179     
       
   180     // Write memory tracking categories
       
   181     //stream.WriteInt32L( swmtConfig.iEnabledCategories );
       
   182     stream.WriteInt32L( iSwmtConfig.iEnabledCategories );
       
   183     
       
   184     // Write heap tracking thread name filter
       
   185     //stream.WriteInt32L( swmtConfig.iThreadNameFilter.Length() );
       
   186     stream.WriteInt32L( iSwmtConfig.iThreadNameFilter.Length() );
       
   187     
       
   188     /*
       
   189     if ( swmtConfig.iThreadNameFilter.Length() > 0 )
       
   190         {
       
   191         stream.WriteL( swmtConfig.iThreadNameFilter, swmtConfig.iThreadNameFilter.Length() );
       
   192         }
       
   193     
       
   194     // Write mode
       
   195     stream.WriteInt32L( swmtConfig.iMode );
       
   196     */
       
   197     if ( iSwmtConfig.iThreadNameFilter.Length() > 0 )
       
   198     	{
       
   199 		stream.WriteL( iSwmtConfig.iThreadNameFilter, iSwmtConfig.iThreadNameFilter.Length() );
       
   200     	}
       
   201         
       
   202     // Write mode
       
   203     stream.WriteInt32L( iSwmtConfig.iMode );
       
   204     
       
   205     stream.CommitL();
       
   206     CleanupStack::PopAndDestroy( &stream ); // Closes file    
       
   207     }
       
   208 
       
   209 void CMemSpySettings::RestoreSettingsL()
       
   210     {  
       
   211     RFile file = SettingsFileLC();
       
   212     RFileReadStream stream( file );
       
   213     CleanupStack::Pop(); // file
       
   214     CleanupClosePushL( stream ); 
       
   215     
       
   216     // Version info
       
   217     const TInt version = stream.ReadInt32L(); // discarded for now
       
   218     TRACE( RDebug::Printf( "CMemSpySettings::RestoreSettingsL() - version: %d", version ) );
       
   219 
       
   220     // Engine settings
       
   221     TMemSpySinkType type = static_cast< TMemSpySinkType >( stream.ReadUint8L() );
       
   222     TRACE( RDebug::Printf( "CMemSpySettings::RestoreSettingsL() - read sinkType: %d", type ) );
       
   223        
       
   224     //iEngine.InstallSinkL( type );
       
   225     //iMemSpySession.SwitchOutputSink( type ); //TODO: to argue to set stuf in engine from here
       
   226     
       
   227     // Set SWMT config
       
   228     TMemSpyEngineHelperSysMemTrackerConfig swmtConfig;
       
   229     swmtConfig.iTimerPeriod = TTimeIntervalMicroSeconds32( stream.ReadInt32L() ); 
       
   230     swmtConfig.iDumpData = static_cast< TBool >( stream.ReadUint8L() );
       
   231 
       
   232     if  ( version < 3 )
       
   233         {
       
   234         // Restore but ignore old delta tracker settings which aren't used anymore
       
   235         //
       
   236         static_cast< TBool >( stream.ReadUint8L() );
       
   237         static_cast< TBool >( stream.ReadUint8L() );
       
   238         static_cast< TBool >( stream.ReadUint8L() );
       
   239         }
       
   240     
       
   241     // Restore memory tracking auto-start process uids if file format supports it...
       
   242     if ( version >= 2 )
       
   243         {
       
   244         RArray<TUid> list;		
       
   245         CleanupClosePushL( list );
       
   246         //
       
   247         const TInt count = stream.ReadInt32L();
       
   248         
       
   249         //CArrayFixFlat<TUid>* list = new(ELeave)CArrayFixFlat<TUid>(count);
       
   250         //CleanupStack::PushL(list );               
       
   251         
       
   252         for( TInt i=0; i<count; i++ )
       
   253             {
       
   254             TUid processUid;
       
   255             stream >> processUid;
       
   256             //           
       
   257             User::LeaveIfError( list.Append( processUid ) );            
       
   258             //list->AppendL( processUid );
       
   259             }
       
   260         //
       
   261         //CMemSpyEngineHelperProcess& processHelper = iEngine.HelperProcess();
       
   262         //processHelper.SetMemoryTrackingAutoStartProcessListL( list );        
       
   263         //iMemSpySession.SetSwmtAutoStartProcessList( list );
       
   264         iUidList = list; 	//TODO: to get it into the engine
       
   265         
       
   266         CleanupStack::PopAndDestroy( &list );
       
   267         }
       
   268     
       
   269     // Restore memory tracking categories 
       
   270     if ( version > 3 )
       
   271         {
       
   272         swmtConfig.iEnabledCategories = stream.ReadInt32L();
       
   273         }
       
   274     
       
   275     // Write heap tracking thread name filter 
       
   276     if ( version > 4 )
       
   277         {
       
   278         TInt len = stream.ReadInt32L();
       
   279         if ( len > 0 )
       
   280             {
       
   281             stream.ReadL( swmtConfig.iThreadNameFilter, len );
       
   282             }
       
   283         }
       
   284 
       
   285     // Write mode
       
   286     if ( version > 5 )
       
   287         {
       
   288         swmtConfig.iMode = (TMemSpyEngineHelperSysMemTrackerConfig::TMemSpyEngineSysMemTrackerMode)stream.ReadInt32L();
       
   289         }
       
   290     
       
   291     //CMemSpyEngineHelperSysMemTracker& swmt = iEngine.HelperSysMemTracker();
       
   292     //swmt.SetConfigL( swmtConfig );
       
   293     //iMemSpySession.SetSwmtConfig( swmtConfig );
       
   294     
       
   295     iSwmtConfig = swmtConfig; 	//TODO: to get it into the engine
       
   296     
       
   297     CleanupStack::PopAndDestroy( &stream ); // Closes file    
       
   298     }
       
   299 
       
   300