memspyui/source/MemSpySettings.cpp
changeset 0 d6fe6244b863
equal deleted inserted replaced
-1:000000000000 0:d6fe6244b863
       
     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 #include <memspy/engine/memspyenginehelpersysmemtrackerconfig.h>
       
    30 
       
    31 // Constants
       
    32 _LIT( KMemSpySettingsFileName, "settings.dat" );
       
    33 
       
    34 // Version 03 dumped some of the system wide memory tracker settings
       
    35 const TInt KMemSpySettingsFileFormatVersion = 6;
       
    36 
       
    37 
       
    38 CMemSpySettings::CMemSpySettings( RFs& aFsSession, CMemSpyEngine& aEngine )
       
    39 :   iFsSession( aFsSession ), iEngine( aEngine )
       
    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 
       
    62 CMemSpySettings* CMemSpySettings::NewL( RFs& aFsSession, CMemSpyEngine& aEngine )
       
    63     {
       
    64     CMemSpySettings* self = new(ELeave) CMemSpySettings( aFsSession, aEngine );
       
    65     CleanupStack::PushL( self );
       
    66     self->ConstructL();
       
    67     CleanupStack::Pop( self );
       
    68     return self;
       
    69     }
       
    70 
       
    71 
       
    72 void CMemSpySettings::GetSettingsFileNameL( TDes& aFileName )
       
    73     {
       
    74     GetSettingsPathL( aFileName );
       
    75     aFileName.Append( KMemSpySettingsFileName );
       
    76     TRACE( RDebug::Print( _L("CMemSpySettings::GetSettingsFileNameL() - aFileName: %S"), &aFileName ) );
       
    77     }
       
    78 
       
    79 
       
    80 void CMemSpySettings::GetSettingsPathL( TDes& aPath )
       
    81     {
       
    82     TRACE( RDebug::Print( _L("CMemSpySettings::GetSettingsPathL() - START") ) );
       
    83     aPath.Zero();
       
    84 
       
    85     // Get private data cage path
       
    86     TInt err = iFsSession.PrivatePath( aPath );
       
    87     TRACE( RDebug::Print( _L("CMemSpySettings::GetSettingsPathL() - priv path err: %d"), err ) );
       
    88     User::LeaveIfError( err );
       
    89 
       
    90     // Combine with C: drive
       
    91     const TDriveUnit cDrive( EDriveC );
       
    92     const TDriveName cDriveName( cDrive.Name() );
       
    93     aPath.Insert( 0, cDriveName );
       
    94 
       
    95     iFsSession.MkDirAll( aPath );
       
    96     TRACE( RDebug::Print( _L("CMemSpySettings::GetSettingsPathL() - END - %S"), &aPath ) );
       
    97     }
       
    98 
       
    99 
       
   100 RFile CMemSpySettings::SettingsFileLC( TBool aReplace )
       
   101     {
       
   102     TRACE( RDebug::Print( _L("CMemSpySettings::SettingsFileLC() - START - aReplace: %d"), aReplace ) );
       
   103 
       
   104     TFileName* fileName = new(ELeave) TFileName();
       
   105     CleanupStack::PushL( fileName );
       
   106     GetSettingsFileNameL( *fileName );
       
   107     TRACE( RDebug::Print( _L("CMemSpySettings::SettingsFileLC() - fileName: %S"), fileName ) );
       
   108 
       
   109     RFile file;
       
   110     TInt error = KErrNone;
       
   111     //
       
   112     if  ( aReplace )
       
   113         {
       
   114         error = file.Replace( iFsSession, *fileName, EFileWrite );
       
   115         TRACE( RDebug::Print( _L("CMemSpySettings::SettingsFileLC() - replace err: %d"), error ) );
       
   116         }
       
   117     else
       
   118         {
       
   119         error = file.Open( iFsSession, *fileName, EFileWrite );
       
   120         TRACE( RDebug::Print( _L("CMemSpySettings::SettingsFileLC() - open err: %d"), error ) );
       
   121         //
       
   122         if  ( error == KErrNotFound )
       
   123             {
       
   124             error = file.Create( iFsSession, *fileName, EFileWrite );
       
   125             }
       
   126         }
       
   127     //
       
   128     User::LeaveIfError( error );
       
   129     CleanupStack::PopAndDestroy( fileName );
       
   130     CleanupClosePushL( file );
       
   131     //
       
   132     TRACE( RDebug::Print( _L("CMemSpySettings::SettingsFileLC() - END") ) );
       
   133     return file;
       
   134     }
       
   135 
       
   136 
       
   137 void CMemSpySettings::StoreSettingsL()
       
   138     {
       
   139     TRACE( RDebug::Printf( "CMemSpySettings::StoreSettingsL() - START" ) );
       
   140 
       
   141     RFile file = SettingsFileLC( ETrue );
       
   142     RFileWriteStream stream( file );
       
   143     CleanupStack::Pop(); // file
       
   144     CleanupClosePushL( stream );
       
   145     
       
   146     // Verion info
       
   147     stream.WriteInt32L( KMemSpySettingsFileFormatVersion );
       
   148     
       
   149     // Engine settings
       
   150     TRACE( RDebug::Printf( "CMemSpySettings::StoreSettingsL() - sinkType: %d", iEngine.SinkType() ) );
       
   151     stream.WriteUint8L( iEngine.SinkType() );
       
   152 
       
   153     // Get SWMT config
       
   154     CMemSpyEngineHelperSysMemTracker& swmt = iEngine.HelperSysMemTracker();
       
   155     TMemSpyEngineHelperSysMemTrackerConfig swmtConfig;
       
   156     swmt.GetConfig( swmtConfig );
       
   157 
       
   158     // Write SWMT settings
       
   159     stream.WriteInt32L( swmtConfig.TimerPeriod().Int() );
       
   160     stream.WriteUint8L( swmtConfig.DumpData() );
       
   161     
       
   162     // Write memory tracking auto-start process list
       
   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     // Write memory tracking categories
       
   173     stream.WriteInt32L( swmtConfig.iEnabledCategories );
       
   174     
       
   175     // Write heap tracking thread name filter
       
   176     stream.WriteInt32L( swmtConfig.iThreadNameFilter.Length() );
       
   177     if ( swmtConfig.iThreadNameFilter.Length() > 0 )
       
   178         {
       
   179         stream.WriteL( swmtConfig.iThreadNameFilter, swmtConfig.iThreadNameFilter.Length() );
       
   180         }
       
   181     
       
   182     // Write mode
       
   183     stream.WriteInt32L( swmtConfig.iMode );
       
   184     
       
   185     stream.CommitL();
       
   186     CleanupStack::PopAndDestroy( &stream ); // Closes file
       
   187     TRACE( RDebug::Printf( "CMemSpySettings::StoreSettingsL() - END - sinkType: %d", iEngine.SinkType() ) );
       
   188     }
       
   189 
       
   190 
       
   191 void CMemSpySettings::RestoreSettingsL()
       
   192     {
       
   193     TRACE( RDebug::Printf( "CMemSpySettings::RestoreSettingsL() - START - current engine sinkType: %d", iEngine.SinkType() ) );
       
   194 
       
   195     RFile file = SettingsFileLC();
       
   196     RFileReadStream stream( file );
       
   197     CleanupStack::Pop(); // file
       
   198     CleanupClosePushL( stream );
       
   199     
       
   200     // Version info
       
   201     const TInt version = stream.ReadInt32L(); // discarded for now
       
   202     TRACE( RDebug::Printf( "CMemSpySettings::RestoreSettingsL() - version: %d", version ) );
       
   203 
       
   204     // Engine settings
       
   205     TMemSpySinkType type = static_cast< TMemSpySinkType >( stream.ReadUint8L() );
       
   206     TRACE( RDebug::Printf( "CMemSpySettings::RestoreSettingsL() - read sinkType: %d", type ) );
       
   207     iEngine.InstallSinkL( type );
       
   208     
       
   209     // Set SWMT config
       
   210     TMemSpyEngineHelperSysMemTrackerConfig swmtConfig;
       
   211     swmtConfig.iTimerPeriod = TTimeIntervalMicroSeconds32( stream.ReadInt32L() );
       
   212     swmtConfig.iDumpData = static_cast< TBool >( stream.ReadUint8L() );
       
   213 
       
   214     if  ( version < 3 )
       
   215         {
       
   216         // Restore but ignore old delta tracker settings which aren't used anymore
       
   217         //
       
   218         /* iHeapDeltaTrackerIncludeKernel =*/ static_cast< TBool >( stream.ReadUint8L() );
       
   219         /* iHeapDeltaTrackerCheckAllocCellCounts =*/ static_cast< TBool >( stream.ReadUint8L() );
       
   220         /* iHeapDeltaTrackerCheckFreeCellCounts =*/ static_cast< TBool >( stream.ReadUint8L() );
       
   221         }
       
   222     
       
   223     // Restore memory tracking auto-start process uids if file format supports it...
       
   224     if ( version >= 2 )
       
   225         {
       
   226         RArray<TUid> list;
       
   227         CleanupClosePushL( list );
       
   228         //
       
   229         const TInt count = stream.ReadInt32L();
       
   230         for( TInt i=0; i<count; i++ )
       
   231             {
       
   232             TUid processUid;
       
   233             stream >> processUid;
       
   234             //
       
   235             TRACE( RDebug::Printf( "CMemSpySettings::RestoreSettingsL() - process tracker uid[%02d]: 0x%08x", i, processUid.iUid ) );
       
   236             User::LeaveIfError( list.Append( processUid ) );
       
   237             }
       
   238         //
       
   239         CMemSpyEngineHelperProcess& processHelper = iEngine.HelperProcess();
       
   240         processHelper.SetMemoryTrackingAutoStartProcessListL( list );
       
   241         CleanupStack::PopAndDestroy( &list );
       
   242         }
       
   243     
       
   244     // Restore memory tracking categories 
       
   245     if ( version > 3 )
       
   246         {
       
   247         swmtConfig.iEnabledCategories = stream.ReadInt32L();
       
   248         }
       
   249     
       
   250     // Write heap tracking thread name filter 
       
   251     if ( version > 4 )
       
   252         {
       
   253         TInt len = stream.ReadInt32L();
       
   254         if ( len > 0 )
       
   255             {
       
   256             stream.ReadL( swmtConfig.iThreadNameFilter, len );
       
   257             }
       
   258         }
       
   259 
       
   260     // Write mode
       
   261     if ( version > 5 )
       
   262         {
       
   263         swmtConfig.iMode = (TMemSpyEngineHelperSysMemTrackerConfig::TMemSpyEngineSysMemTrackerMode)stream.ReadInt32L();
       
   264         }
       
   265     
       
   266     CMemSpyEngineHelperSysMemTracker& swmt = iEngine.HelperSysMemTracker();
       
   267     swmt.SetConfigL( swmtConfig );
       
   268 
       
   269     CleanupStack::PopAndDestroy( &stream ); // Closes file
       
   270     TRACE( RDebug::Printf( "CMemSpySettings::RestoreSettingsL() - END - engine sink type: %d", iEngine.SinkType() ) );
       
   271     }
       
   272 
       
   273