inc/alf/alfperformance.h
changeset 3 4526337fb576
parent 2 b1a1f09f9110
child 7 c2c0f97004fc
child 8 b802b04b6cfa
child 17 3eca7e70b1b8
equal deleted inserted replaced
2:b1a1f09f9110 3:4526337fb576
     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: Perf log Header.
       
    15 *
       
    16 */
       
    17 
       
    18 #ifndef ALFPERFORMANCE_H
       
    19 #define ALFPERFORMANCE_H
       
    20 
       
    21 //  INCLUDES
       
    22 #include <e32debug.h>
       
    23 #include <e32base.h>
       
    24 #include <e32std.h>
       
    25 #include <hal.h>
       
    26 #include <e32cmn.h>
       
    27 #include <flogger.h>
       
    28 #include <f32file.h>
       
    29 
       
    30 _LIT(KLogFileName,"alfperf.log");
       
    31 _LIT(KLogFileDir,"alflogs");
       
    32 _LIT(KLogFileRootDir,"c:\\logs\\");
       
    33 
       
    34 class CAlfPerformance : public CBase
       
    35     {
       
    36     public:
       
    37         struct TPerfData
       
    38             {
       
    39             TTime iStartTime;
       
    40             TInt iStartMemory;
       
    41             };
       
    42 
       
    43     public:  // Constructors and destructor
       
    44         static CAlfPerformance* NewL();
       
    45         virtual ~CAlfPerformance();
       
    46 
       
    47     public: // New functions
       
    48         inline static void PrintUserMem(const TDesC& aMsg);
       
    49         inline static TPerfData* StartTestCase(const TDesC& aMsg = KNullDesC);
       
    50         inline static void StopTestCase(TPerfData* aData, const TDesC& aMsg = KNullDesC);
       
    51         //creates log file if it doesn't exist already
       
    52         inline static void CreateLogFile();
       
    53     private:
       
    54         CAlfPerformance();
       
    55         void ConstructL();
       
    56     };
       
    57 
       
    58 inline void CAlfPerformance::PrintUserMem(const TDesC& aMsg)
       
    59     {
       
    60     TBuf<512> buffer;
       
    61     TInt freeRAM;
       
    62     HAL::Get(HALData::EMemoryRAMFree, freeRAM);
       
    63     RHeap heap = User::Heap();
       
    64     heap.Open();
       
    65     TInt _size = heap.Size();
       
    66     TInt largest = 0;
       
    67     TInt available = heap.Available(largest);
       
    68     heap.Close();
       
    69     _LIT( KMemoryFormat, "FreeRAM: %d kB, User: - heap %d kB, available %d kB, largest block %d kB" );
       
    70     buffer.Format(KMemoryFormat, freeRAM / 1024, _size / 1024, available / 1024, largest / 1024);
       
    71     CreateLogFile();
       
    72     //Write logs into file
       
    73     RFileLogger::WriteFormat( KLogFileDir,KLogFileName,EFileLoggingModeAppend,_L("RAM - %S - %S"), &aMsg, &buffer);
       
    74     }
       
    75 
       
    76 inline CAlfPerformance::TPerfData* CAlfPerformance::StartTestCase(const TDesC& aMsg)
       
    77     {
       
    78     CreateLogFile();
       
    79     //Write logs into file
       
    80     RFileLogger::WriteFormat(KLogFileDir,KLogFileName,EFileLoggingModeAppend,_L("Test case %S starts"), &aMsg);
       
    81     CAlfPerformance::TPerfData* data = new (ELeave) TPerfData;
       
    82     TTime readyTime;
       
    83     data->iStartTime.HomeTime();
       
    84     User::AllocSize(data->iStartMemory);
       
    85     return data;
       
    86     }
       
    87 
       
    88 inline void CAlfPerformance::StopTestCase(CAlfPerformance::TPerfData* aData, const TDesC& aMsg)
       
    89     {
       
    90     if(!aData)
       
    91         {
       
    92         return;
       
    93         }
       
    94     TTime readyTime;
       
    95     readyTime.HomeTime();
       
    96     TTimeIntervalMicroSeconds delay = readyTime.MicroSecondsFrom(aData->iStartTime);
       
    97     TTime transferTime(delay.Int64());
       
    98     TBuf<64> timeString;
       
    99     transferTime.FormatL(timeString, _L("- Elapsed time: %S%C microseconds"));
       
   100     TBuf<256> tmp;
       
   101     tmp.Append(timeString);
       
   102 
       
   103     RFileLogger::Write(KLogFileDir,KLogFileName,EFileLoggingModeAppend,tmp);
       
   104     // Memory consumption
       
   105     TInt endMemory;
       
   106     User::AllocSize(endMemory); 
       
   107 
       
   108     CreateLogFile();
       
   109     //Write logs into file
       
   110     RFileLogger::WriteFormat(KLogFileDir,KLogFileName,EFileLoggingModeAppend,_L("- Allocated memory: %d kB"), (endMemory - aData->iStartMemory) / 1024);
       
   111     delete aData;
       
   112     aData = NULL;
       
   113     RFileLogger::WriteFormat(KLogFileDir,KLogFileName,EFileLoggingModeAppend,_L("Test case %S ends"), &aMsg);
       
   114     }
       
   115 inline void CAlfPerformance::CreateLogFile()
       
   116     {
       
   117         RFs fsSession;
       
   118         RFile file;
       
   119         User::LeaveIfError(fsSession.Connect());
       
   120         TFileName filename;
       
   121         filename.Append(KLogFileRootDir);
       
   122         filename.Append(KLogFileDir);
       
   123         filename.Append(_L("\\"));
       
   124         //create logs directory if it doesn't exist
       
   125         fsSession.MkDir(filename);
       
   126         filename.Append(KLogFileName);
       
   127         //check if log file already exists
       
   128         TInt retStatus = file.Open(fsSession,filename,EFileRead);
       
   129         if(retStatus == KErrNotFound)
       
   130             file.Create(fsSession,filename,EFileWrite);         //create a new log file
       
   131         file.Close();
       
   132         fsSession.Close();
       
   133     }
       
   134 #endif // ALFPERFORMANCE_H
       
   135             
       
   136 // End of File