perfsrv/memspy/Engine/Source/SysMemTracker/MemSpyEngineHelperSysMemTrackerCycle.cpp
changeset 48 516af714ebb4
equal deleted inserted replaced
45:185201be11b0 48:516af714ebb4
       
     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 <memspy/engine/memspyenginehelpersysmemtrackercycle.h>
       
    19 
       
    20 // System includes
       
    21 #include <e32debug.h>
       
    22 #include <hal.h>
       
    23 #include <hal_data.h>
       
    24 
       
    25 // User includes
       
    26 #include <memspy/engine/memspyengine.h>
       
    27 #include <memspy/engine/memspyengineutils.h>
       
    28 #include <memspy/engine/memspyengineobjectthread.h>
       
    29 #include <memspy/engine/memspyengineobjectprocess.h>
       
    30 #include <memspy/engine/memspyengineobjectcontainer.h>
       
    31 #include <memspy/engine/memspyenginehelperheap.h>
       
    32 #include <memspy/engine/memspyenginehelperchunk.h>
       
    33 #include <memspy/engine/memspyengineoutputsink.h>
       
    34 #include <memspy/engine/memspyengineoutputlist.h>
       
    35 #include <memspy/engine/memspyengineoutputsink.h>
       
    36 #include <memspy/engine/memspyenginehelpersysmemtrackercyclechange.h>
       
    37     
       
    38 // Literal constants
       
    39 _LIT( KMemSpySWMTDataFolderSpecification, "Data" );
       
    40 _LIT( KMemSpySWMTFolderSpecification, "System Wide MT\\Cycle %04d" );
       
    41 
       
    42 
       
    43 
       
    44 CMemSpyEngineHelperSysMemTrackerCycle::CMemSpyEngineHelperSysMemTrackerCycle( const TMemSpyEngineHelperSysMemTrackerConfig& aConfig, const CMemSpyEngineHelperSysMemTrackerCycle* aPreviousCycle )
       
    45 :   iConfig( aConfig ), iPreviousCycle( aPreviousCycle ), iCycleNumber( 1 )
       
    46     {
       
    47     iTime.HomeTime();
       
    48     if  ( aPreviousCycle )
       
    49         {
       
    50         iCycleNumber = aPreviousCycle->CycleNumber() + 1;
       
    51         }
       
    52     }
       
    53 
       
    54 
       
    55 CMemSpyEngineHelperSysMemTrackerCycle::~CMemSpyEngineHelperSysMemTrackerCycle()
       
    56     {
       
    57     delete iFixedItems;
       
    58     delete iCaption;
       
    59     delete iTimeFormatted;
       
    60     //
       
    61     DiscardChanges();
       
    62     iChangeDescriptors.Close();
       
    63     }
       
    64 
       
    65 
       
    66 void CMemSpyEngineHelperSysMemTrackerCycle::ConstructL()
       
    67     {
       
    68     iFixedItems = CMemSpyEngineOutputList::NewL();
       
    69 
       
    70     // Get time stamp
       
    71     TBuf<128> timeBuf;
       
    72     MemSpyEngineUtils::FormatTimeSimple( timeBuf, iTime );
       
    73     iTimeFormatted = timeBuf.AllocL();
       
    74     
       
    75     // Get free ram
       
    76     TInt freeRam = KErrNotFound;
       
    77     User::LeaveIfError( HAL::Get( HALData::EMemoryRAMFree, freeRam ) );
       
    78     iMemoryFree = freeRam;
       
    79     }
       
    80 
       
    81 
       
    82 CMemSpyEngineHelperSysMemTrackerCycle* CMemSpyEngineHelperSysMemTrackerCycle::NewLC( const TMemSpyEngineHelperSysMemTrackerConfig& aConfig, const CMemSpyEngineHelperSysMemTrackerCycle* aPreviousCycle )
       
    83     {
       
    84     CMemSpyEngineHelperSysMemTrackerCycle* self = new(ELeave) CMemSpyEngineHelperSysMemTrackerCycle( aConfig, aPreviousCycle );
       
    85     CleanupStack::PushL( self );
       
    86     self->ConstructL();
       
    87     return self;
       
    88     }
       
    89 
       
    90 
       
    91 EXPORT_C TInt CMemSpyEngineHelperSysMemTrackerCycle::CycleNumber() const
       
    92     {
       
    93     return iCycleNumber;
       
    94     }
       
    95 
       
    96 
       
    97 EXPORT_C const TTime& CMemSpyEngineHelperSysMemTrackerCycle::Time() const
       
    98     {
       
    99     return iTime;
       
   100     }
       
   101 
       
   102 
       
   103 EXPORT_C const TDesC& CMemSpyEngineHelperSysMemTrackerCycle::TimeFormatted() const
       
   104     {
       
   105     return *iTimeFormatted;
       
   106     }
       
   107 
       
   108 
       
   109 EXPORT_C const TDesC& CMemSpyEngineHelperSysMemTrackerCycle::Caption() const
       
   110     {
       
   111     return *iCaption;
       
   112     }
       
   113 
       
   114 
       
   115 EXPORT_C const TInt64& CMemSpyEngineHelperSysMemTrackerCycle::MemoryFree() const
       
   116     {
       
   117     return iMemoryFree;
       
   118     }
       
   119 
       
   120 
       
   121 EXPORT_C TInt64 CMemSpyEngineHelperSysMemTrackerCycle::MemoryFreePreviousCycle() const
       
   122     {
       
   123     TInt64 ret = MemoryFree();
       
   124     //
       
   125     if  ( iPreviousCycle )
       
   126         {
       
   127         ret = iPreviousCycle->MemoryFree();
       
   128         }
       
   129     //
       
   130     return ret;
       
   131     }
       
   132 
       
   133 
       
   134 EXPORT_C TInt64 CMemSpyEngineHelperSysMemTrackerCycle::MemoryDelta() const
       
   135     {
       
   136     const TInt64 ret = ( iMemoryFree - iMemoryUsed );
       
   137     return ret;
       
   138     }
       
   139 
       
   140 
       
   141 EXPORT_C TInt CMemSpyEngineHelperSysMemTrackerCycle::ChangeCount() const
       
   142     {
       
   143     return iChangeCount;
       
   144     }
       
   145 
       
   146 
       
   147 EXPORT_C TInt CMemSpyEngineHelperSysMemTrackerCycle::MdcaCount() const
       
   148     {
       
   149     return iFixedItems->Count();
       
   150     }
       
   151 
       
   152 
       
   153 EXPORT_C TPtrC CMemSpyEngineHelperSysMemTrackerCycle::MdcaPoint( TInt aIndex ) const
       
   154     {
       
   155     return iFixedItems->MdcaPoint( aIndex );
       
   156     }
       
   157 
       
   158 
       
   159 void CMemSpyEngineHelperSysMemTrackerCycle::FinalizeL()
       
   160     {
       
   161     _LIT( KChange, "change" );
       
   162     _LIT( KChanges, "changes" );
       
   163 
       
   164     // Save change count
       
   165     iChangeCount = iChangeDescriptors.Count();
       
   166 
       
   167     // First make the main caption
       
   168     TMemSpySizeText sizeText;
       
   169     TMemSpySizeText deltaText;
       
   170     TPtrC pChangeText( KChange );
       
   171    
       
   172     // Get free RAM formatted as a descriptor
       
   173     sizeText = MemSpyEngineUtils::FormatSizeTextPrecise( MemoryFree() );
       
   174 
       
   175     // Get delta formatted as a descriptor
       
   176     const TInt64 delta( MemoryDelta() );
       
   177     deltaText = MemSpyEngineUtils::FormatSizeTextPrecise( delta );
       
   178 
       
   179     // Get number of changed threads as a descriptor
       
   180     if  ( ChangeCount() == 0 || ChangeCount() > 1 )
       
   181         {
       
   182         pChangeText.Set( KChanges );
       
   183         }
       
   184 
       
   185     // Now make the caption
       
   186     if  ( MemoryDelta() != 0 )
       
   187         {
       
   188         TBuf<300> buf;
       
   189         _LIT( KMainCaption1, "\t[%03d] F: %S\t\tD: %S, %d %S" );
       
   190         buf.Format( KMainCaption1, CycleNumber(), &sizeText, &deltaText, ChangeCount(), &pChangeText );
       
   191         HBufC* caption = buf.AllocL();
       
   192         delete iCaption;
       
   193         iCaption = caption;
       
   194         }
       
   195     else
       
   196         {
       
   197         TBuf<300> buf;
       
   198         _LIT( KMainCaption2, "\t[%03d] F: %S\t\tNo Delta, %d %S" );
       
   199         buf.Format( KMainCaption2, CycleNumber(), &sizeText, ChangeCount(), &pChangeText );
       
   200         HBufC* caption = buf.AllocL();
       
   201         delete iCaption;
       
   202         iCaption = caption;
       
   203         }
       
   204 
       
   205     // Now make the child entries
       
   206     if  ( CycleNumber() > 0 )
       
   207         {
       
   208         _LIT(KItem1a, "Cycle");
       
   209         iFixedItems->AddItemL( KItem1a, CycleNumber() );
       
   210         }
       
   211     else
       
   212         {
       
   213         _LIT(KItem1a, "Baseline");
       
   214         iFixedItems->AddItemL( KItem1a );
       
   215         }
       
   216     //
       
   217     _LIT(KItem1c, "Time");
       
   218     iFixedItems->AddItemL( KItem1c, iTimeFormatted );
       
   219     //
       
   220     _LIT(KItem2, "Memory Free");
       
   221     iFixedItems->AddItemL( KItem2, sizeText );
       
   222     //
       
   223     if  ( CycleNumber() > 0 )
       
   224         {
       
   225         _LIT(KItem2b, "Memory Change (vs. Last Cycle)");
       
   226         sizeText = MemSpyEngineUtils::FormatSizeTextPrecise( MemoryFree() - MemoryFreePreviousCycle() );
       
   227         iFixedItems->AddItemL( KItem2b, sizeText );
       
   228         //
       
   229         _LIT(KItem3, "Number of Changes");
       
   230         iFixedItems->AddItemL( KItem3, ChangeCount() );
       
   231         }
       
   232     }
       
   233 
       
   234 
       
   235 void CMemSpyEngineHelperSysMemTrackerCycle::DiscardChanges()
       
   236     {
       
   237     iChangeDescriptors.ResetAndDestroy();
       
   238     }
       
   239 
       
   240 
       
   241 CMemSpyEngineHelperSysMemTrackerCycleChange& CMemSpyEngineHelperSysMemTrackerCycle::ChangeAt( TInt aIndex )
       
   242     {
       
   243     return *iChangeDescriptors[ aIndex ];
       
   244     }
       
   245 
       
   246 
       
   247 void CMemSpyEngineHelperSysMemTrackerCycle::GetDataFolderL( RFs& aFsSession, TDes& aFolder )
       
   248     {
       
   249     TFileName fileName;
       
   250     fileName.Format( KMemSpySWMTFolderSpecification, CycleNumber() );
       
   251 
       
   252     CMemSpyEngineSinkMetaData* metaData = CMemSpyEngineSinkMetaData::NewL( KNullDesC, fileName, KNullDesC, ETrue, EFalse, Time() );
       
   253     CleanupStack::PushL( metaData );
       
   254     MemSpyEngineUtils::GetFolderL( aFsSession, aFolder, *metaData );
       
   255     CleanupStack::PopAndDestroy( metaData );
       
   256 
       
   257     // This get's us something like:
       
   258     //
       
   259     //    E:\MemSpy\System Wide MT\Data - Cycle 001\Log.txt
       
   260     //
       
   261     // Now we must strip off the filename and return just the folder specification
       
   262     const TParsePtrC parser( aFolder );
       
   263     if  ( parser.NamePresent() && parser.ExtPresent() )
       
   264         {
       
   265         const TPtrC nameAndExt( parser.NameAndExt() );
       
   266         aFolder.SetLength( aFolder.Length() - nameAndExt.Length() );
       
   267         }
       
   268 
       
   269     // Add "Data" on to the end so that we put all data in a clearly marked subdir)
       
   270     aFolder.Append( KMemSpySWMTDataFolderSpecification );
       
   271     TRACE( RDebug::Print(_L("CMemSpyEngineHelperSysMemTrackerCycle::GetDataFolderL() - folder1: %S"), &aFolder ) );
       
   272 
       
   273     // Now we should return something like:
       
   274     //
       
   275     //    E:\MemSpy\System Wide MT\Data - Cycle 001
       
   276     TRACE( RDebug::Print(_L("CMemSpyEngineHelperSysMemTrackerCycle::GetDataFolderL() - folder2: %S"), &aFolder ) );
       
   277 
       
   278     const TInt err = aFsSession.MkDirAll( aFolder );
       
   279     if  ( !(err == KErrNone || err == KErrAlreadyExists ) )
       
   280         {
       
   281         User::LeaveIfError( err );
       
   282         }
       
   283     }
       
   284 
       
   285 
       
   286 void CMemSpyEngineHelperSysMemTrackerCycle::DataStreamBeginL( CMemSpyEngineOutputSink& aSink, const TDesC& aContext )
       
   287     {
       
   288     // First make the folder...
       
   289     HBufC* folder = HBufC::NewLC( KMemSpySWMTFolderSpecification().Length() + 10 );
       
   290     TPtr pFolder( folder->Des() );
       
   291     pFolder.Format( KMemSpySWMTFolderSpecification, CycleNumber() );
       
   292 
       
   293     // Now start the data stream
       
   294     aSink.DataStreamBeginL( aContext, pFolder, KMemSpyLogDefaultExtension, ETrue, EFalse );
       
   295     CleanupStack::PopAndDestroy( folder );
       
   296     }
       
   297 
       
   298 
       
   299 void CMemSpyEngineHelperSysMemTrackerCycle::DataStreamEndL( CMemSpyEngineOutputSink& aSink )
       
   300     {
       
   301 	aSink.DataStreamEndL();
       
   302     }
       
   303 
       
   304 
       
   305 void CMemSpyEngineHelperSysMemTrackerCycle::AddAndPopL( CMemSpyEngineHelperSysMemTrackerCycleChange* aInfo )
       
   306     {
       
   307     iChangeDescriptors.AppendL( aInfo );
       
   308     CleanupStack::Pop( aInfo );
       
   309     }
       
   310 
       
   311 
       
   312 const TMemSpyEngineHelperSysMemTrackerConfig& CMemSpyEngineHelperSysMemTrackerCycle::Config() const
       
   313     {
       
   314     return iConfig; 
       
   315     }
       
   316 
       
   317 
       
   318 void CMemSpyEngineHelperSysMemTrackerCycle::AddToMemoryUsed( TInt aValue )
       
   319     {
       
   320     iMemoryUsed += aValue;
       
   321     }
       
   322 
       
   323 
       
   324 void CMemSpyEngineHelperSysMemTrackerCycle::AddToMemoryHeapAllocs( TInt aValue )
       
   325     {
       
   326     iMemoryHeapAllocs += aValue;
       
   327     }
       
   328 
       
   329 
       
   330 void CMemSpyEngineHelperSysMemTrackerCycle::AddToMemoryHeapFrees( TInt aValue )
       
   331     {
       
   332     iMemoryHeapFrees += aValue;
       
   333     }
       
   334 
       
   335 
       
   336 void CMemSpyEngineHelperSysMemTrackerCycle::AddToCellCountFree( TInt aValue )
       
   337     {
       
   338     iCellCountsFrees += aValue;
       
   339     }
       
   340 
       
   341 
       
   342 void CMemSpyEngineHelperSysMemTrackerCycle::AddToCellCountAlloc( TInt aValue )
       
   343     {
       
   344     iCellCountsAllocs += aValue;
       
   345     }
       
   346 
       
   347 
       
   348 const TInt64& CMemSpyEngineHelperSysMemTrackerCycle::MemoryUsed() const
       
   349     {
       
   350     return iMemoryUsed;
       
   351     }
       
   352 
       
   353 
       
   354 const TInt64& CMemSpyEngineHelperSysMemTrackerCycle::MemoryHeapAllocs() const
       
   355     {
       
   356     return iMemoryHeapAllocs;
       
   357     }
       
   358 
       
   359 
       
   360 const TInt64& CMemSpyEngineHelperSysMemTrackerCycle::MemoryHeapFrees() const
       
   361     {
       
   362     return iMemoryHeapFrees;
       
   363     }
       
   364 
       
   365 
       
   366 const TInt64& CMemSpyEngineHelperSysMemTrackerCycle::MemoryHeapCellCountAlloc() const
       
   367     {
       
   368     return iCellCountsAllocs;
       
   369     }
       
   370 
       
   371 
       
   372 const TInt64& CMemSpyEngineHelperSysMemTrackerCycle::MemoryHeapCellCountFree() const
       
   373     {
       
   374     return iCellCountsFrees;
       
   375     }
       
   376 
       
   377 
       
   378 
       
   379 
       
   380 
       
   381 
       
   382