mmappcomponents/mmmtpdataprovider/inc/cmmmtpdpperflog.inl
changeset 0 a2952bb97e68
child 9 bee149131e4b
child 25 d881023c13eb
equal deleted inserted replaced
-1:000000000000 0:a2952bb97e68
       
     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 #if defined(_DEBUG) || defined(MMMTPDP_PERFLOG)
       
    20 #include <e32svr.h>
       
    21 #include <hal.h>
       
    22 
       
    23 const TInt KMtpLogBufferSize = 1000;
       
    24 
       
    25 CMmMtpDpPerfLog* CMmMtpDpPerfLog::NewL( const TDesC& aTitle )
       
    26     {
       
    27     CMmMtpDpPerfLog* self = new( ELeave ) CMmMtpDpPerfLog();
       
    28     CleanupStack::PushL( self );
       
    29     self->ConstructL( aTitle );
       
    30     CleanupStack::Pop( self );
       
    31     
       
    32     return self;
       
    33     }
       
    34 
       
    35 void CMmMtpDpPerfLog::ConstructL( const TDesC& aTitle )
       
    36     {
       
    37     iOverflowHandler.SetOwner( this );
       
    38     HAL::Get( HAL::ENanoTickPeriod, iNTickPeriod );
       
    39     
       
    40     iTitle = HBufC16::NewL( aTitle.Length() );
       
    41     
       
    42     TPtr title = iTitle->Des();
       
    43     
       
    44     title.Copy( aTitle );
       
    45     }
       
    46 
       
    47 CMmMtpDpPerfLog::CMmMtpDpPerfLog() : iDescription( 2 ),
       
    48                                     iStartTick( 2 ),
       
    49                                     iTotalTime( 2 ),
       
    50                                     iTotalUsage( 2 ),
       
    51                                     iLastIndex( 0 )
       
    52     {
       
    53     }
       
    54 
       
    55 CMmMtpDpPerfLog::~CMmMtpDpPerfLog()
       
    56     {
       
    57     iDescription.Reset();
       
    58     iStartTick.Reset();
       
    59     iTotalTime.Reset();
       
    60     iTotalUsage.Reset();
       
    61     delete iTitle;
       
    62     }
       
    63 
       
    64 void CMmMtpDpPerfLog::Write( const TDesC& aText )
       
    65     {
       
    66     RDebug::Print( aText );
       
    67     }
       
    68 
       
    69 void CMmMtpDpPerfLog::WriteFormat( TRefByValue<const TDesC> aFmt, ... )
       
    70     {
       
    71     VA_LIST list;
       
    72     VA_START( list, aFmt );
       
    73 
       
    74     TBuf<KMtpLogBufferSize> buf;
       
    75     
       
    76     buf.AppendFormatList( aFmt, list, &iOverflowHandler );
       
    77     Write( buf );
       
    78     }
       
    79 
       
    80 void CMmMtpDpPerfLog::Start( const TDesC& aDescription )
       
    81     {
       
    82     TInt index = 0;
       
    83     TInt err = KErrNone;
       
    84     
       
    85     // check last remember index first, to minimize looping string search
       
    86     if ( ( iLastIndex >= 0 ) && ( iLastIndex < iDescription.Count() ) )
       
    87         {
       
    88         if ( iDescription[iLastIndex].Compare( aDescription ) == 0 )
       
    89             index = iLastIndex;
       
    90         else
       
    91             {    
       
    92             for ( index = 0; index < iDescription.Count(); index++ )
       
    93                 {
       
    94                 if ( index == iLastIndex )
       
    95                     continue;
       
    96                 else if ( iDescription[index].Compare( aDescription ) == 0 )
       
    97                     break;
       
    98                 }
       
    99             }
       
   100         }
       
   101     
       
   102     if ( index == iDescription.Count() )
       
   103         {
       
   104         TRAP( err, iDescription.AppendL( aDescription ) );
       
   105         
       
   106         if ( err == KErrNone )
       
   107             TRAP( err, iStartTick.AppendL( 0 ) );
       
   108         
       
   109         if ( err == KErrNone )
       
   110             TRAP( err, iTotalTime.AppendL( 0 ));
       
   111         
       
   112         if ( err == KErrNone )
       
   113             TRAP( err, iTotalUsage.AppendL( 0 ) );
       
   114         
       
   115         if ( err == KErrNone )
       
   116             WriteFormat( _L( "CMmMtpDpPerfLog::Start, '%S' is created..." ), &aDescription );
       
   117         else
       
   118             {
       
   119             WriteFormat( _L( "CMmMtpDpPerfLog::Start, '%S' cannot be created!!!" ), &aDescription );
       
   120             return;
       
   121             }
       
   122         }
       
   123     
       
   124     iStartTick[index] = User::NTickCount();
       
   125     // remember for next time, in case being ::Stop is call immediately afterward
       
   126     iLastIndex = index;
       
   127     }
       
   128 
       
   129 void CMmMtpDpPerfLog::Stop( const TDesC& aDescription )
       
   130     {
       
   131     TUint32 endtick = User::NTickCount();
       
   132     TInt index = 0;
       
   133 
       
   134     // check last remember index first, to minimize looping string search
       
   135     if ( ( iLastIndex >= 0 ) && ( iLastIndex < iDescription.Count() ) )
       
   136         {
       
   137         if ( iDescription[iLastIndex].Compare( aDescription ) == 0 )
       
   138             index = iLastIndex;
       
   139         else
       
   140             {    
       
   141             for ( index = 0; index < iDescription.Count(); index++ )
       
   142                 {
       
   143                 if ( index == iLastIndex )
       
   144                     continue;
       
   145                 else if ( iDescription[index].Compare( aDescription ) == 0 )
       
   146                     break;
       
   147                 }
       
   148             }
       
   149         }
       
   150     
       
   151     if ( index == iDescription.Count() )
       
   152         {
       
   153         WriteFormat( _L( "CMmMtpDpPerfLog::Stop, '%S' cannot be found!!!" ), &aDescription );
       
   154         return;
       
   155         }
       
   156     
       
   157     TUint microsecTaken = ( endtick-iStartTick[index] ) * iNTickPeriod;
       
   158     
       
   159     iTotalTime[index] += microsecTaken;
       
   160     iTotalUsage[index]++;
       
   161     
       
   162     TBuf<3> lastTimeDecimal;
       
   163     TBuf<3> totalTimeDecimal;
       
   164     TBuf<3> averageTimeDecimal;
       
   165     
       
   166     TUint32 lastTimeValue = microsecTaken / 1000;
       
   167     TUint32 lastTimeDecimalValue = microsecTaken % 1000;
       
   168     TUint64 totalTimeValue = iTotalTime[index] / 1000;
       
   169     TUint32 totalTimeDecimalValue = iTotalTime[index] % 1000;
       
   170     TUint32 averageTimeValue = iTotalTime[index] / iTotalUsage[index] / 1000;
       
   171     TUint32 averageTimeDecimalValue = iTotalTime[index] / iTotalUsage[index] % 1000;
       
   172     
       
   173     if ( ( lastTimeDecimalValue < 100 ) && ( lastTimeDecimalValue > 0 ) )
       
   174         lastTimeDecimal.AppendNum( 0 );
       
   175     
       
   176     lastTimeDecimal.AppendNum( lastTimeDecimalValue );
       
   177     
       
   178     if ( ( totalTimeDecimalValue < 100 ) && ( totalTimeDecimalValue > 0 ) )
       
   179         totalTimeDecimal.AppendNum( 0 );
       
   180     
       
   181     totalTimeDecimal.AppendNum( totalTimeDecimalValue );
       
   182     
       
   183     if ( ( averageTimeDecimalValue < 100 ) && ( averageTimeDecimalValue > 0 ) )
       
   184         averageTimeDecimal.AppendNum( 0 );    
       
   185     
       
   186     averageTimeDecimal.AppendNum( averageTimeDecimalValue );
       
   187     
       
   188     if (totalTimeValue <= 0xFFFFFFFF)
       
   189         {
       
   190         WriteFormat( _L( "<PERFLOG>%S-%S, usage = %u, last time = %u.%S ms, total time = %u.%S ms, average time = %u.%S ms</PERFLOG>" ), 
       
   191             iTitle, 
       
   192             &iDescription[index], 
       
   193             iTotalUsage[index], 
       
   194             lastTimeValue, 
       
   195             &lastTimeDecimal, 
       
   196             I64LOW(totalTimeValue), 
       
   197             &totalTimeDecimal, 
       
   198             averageTimeValue, 
       
   199             &averageTimeDecimal );
       
   200         }
       
   201     else
       
   202         {
       
   203         WriteFormat( _L( "<PERFLOG>%S-%S, usage = %u, last time = %u.%S ms, total time = %u%u.%S ms, average time = %u.%S ms</PERFLOG>" ), 
       
   204             iTitle, 
       
   205             &iDescription[index], 
       
   206             iTotalUsage[index], 
       
   207             lastTimeValue, 
       
   208             &lastTimeDecimal, 
       
   209             I64HIGH(totalTimeValue), 
       
   210             I64LOW(totalTimeValue),
       
   211             &totalTimeDecimal, 
       
   212             averageTimeValue, 
       
   213             &averageTimeDecimal );
       
   214         }
       
   215     }
       
   216      
       
   217 // Maintain a references for writing...
       
   218 void TMmMtpDpOverflowHandler::SetOwner( CMmMtpDpPerfLog* aOwner )
       
   219     {
       
   220     iOwner = aOwner;
       
   221     }
       
   222     
       
   223 void TMmMtpDpOverflowHandler::Overflow( TDes& aDes )
       
   224     {
       
   225     iOwner->Write( _L( "Logging Overflow!" ) );
       
   226     iOwner->Write( aDes );
       
   227     }
       
   228 
       
   229 #endif // defined(_DEBUG) || defined(MMMTPDP_PERFLOG)
       
   230