sysanadatacapture/piprofiler/piprofiler/engine/src/ProfilerEshell.cpp
changeset 1 3ff3fecb12fe
equal deleted inserted replaced
0:f0f2b8682603 1:3ff3fecb12fe
       
     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 <f32file.h>
       
    19 #include <e32cons.h>
       
    20 
       
    21 #include "ProfilerEshell.h"
       
    22 #include <piprofiler/ProfilerSession.h>
       
    23 
       
    24 
       
    25 
       
    26 _LIT(KProfilerEngineExe, "PIProfilerEngine.exe");
       
    27 
       
    28 /*
       
    29  *
       
    30  * Static methods for controlling the profiler
       
    31  * through command line
       
    32  *
       
    33  */
       
    34 // --------------------------------------------------------------------------------------------
       
    35 static void PrintUsageInfo(const TDesC& aAdditionalInfo)
       
    36     {
       
    37     _LIT(KConsoleName, "Console");
       
    38     _LIT(KLegalNote, "PIProfiler Version 2.00.0 Copyright @ 2009 Nokia\n");
       
    39     _LIT(KUsageNote, "Usage: PIProfiler [start/end/timed] settingsfile [time to run]\n");
       
    40     _LIT(KExample, "Example: PIProfiler timed C:\\data\\piprofilersettings.txt 60\n");
       
    41 
       
    42     TRAP_IGNORE(CConsoleBase* console = Console::NewL(KConsoleName,TSize(KConsFullScreen,KConsFullScreen));
       
    43         console->Printf(KLegalNote);
       
    44         console->Printf(KUsageNote);
       
    45         console->Printf(KExample);
       
    46         console->Write(aAdditionalInfo);
       
    47     
       
    48         console->Printf(_L("\n[Press any key]"));
       
    49         console->Getch();
       
    50         delete console;);
       
    51     }
       
    52 
       
    53 // --------------------------------------------------------------------------------------------
       
    54 static TInt FindProcess()
       
    55     {
       
    56     TFindProcess procName;
       
    57     procName.Find(_L("PIProfilerEngine.exe*"));
       
    58     TFullName aResult;
       
    59     
       
    60     // find the first appearance, i.e. "myself"...
       
    61     TInt err(procName.Next(aResult));  // the first appearance
       
    62     if(err == KErrNotFound)
       
    63         {
       
    64         // now check if old Profiler is still running on
       
    65         procName.Find(_L("BappeaProf.exe*"));
       
    66         err = procName.Next(aResult);
       
    67         // check if old profiler process found 
       
    68         if(err == KErrNone)
       
    69             {
       
    70             // return error for error handling
       
    71             return KErrAlreadyExists;
       
    72             }
       
    73         // return KErrNotFound and a new profiler engine process can be started
       
    74         return KErrNotFound;
       
    75         }
       
    76     return err;
       
    77     }
       
    78 
       
    79 /**
       
    80  * Function for starting profiler engine in the background,
       
    81  * call profiler to load settings
       
    82  * and start sampling process
       
    83  * 
       
    84  * @param configFile name and location of settings file
       
    85  */
       
    86 // --------------------------------------------------------------------------------------------
       
    87 static TInt StartProfilerProcess(const TDesC& configFile, TInt aRunTimeInSeconds)
       
    88     {
       
    89     TInt err(KErrNone);
       
    90     RProcess proc;
       
    91     TRequestStatus status = KRequestPending;
       
    92     
       
    93     // check if process exists
       
    94     err = FindProcess();
       
    95     LOGSTRING2(_L("PIProfiler: tried to find process, response %d"), err); 
       
    96 
       
    97     // check if already exists and don't start a new eshell profiling
       
    98     if( err == KErrNotFound )
       
    99         {
       
   100         // try create new process
       
   101         err = proc.Create(KProfilerEngineExe, configFile);
       
   102 
       
   103         LOGSTRING2(_L("PIProfiler: created process, response %d"), err); 
       
   104 
       
   105         // check if RProcess::Create() succeeded
       
   106         if( err == KErrNone )
       
   107             {
       
   108             // kick off the engine process
       
   109             proc.Resume();
       
   110             
       
   111             // wait for the constructor to complete 
       
   112             proc.Rendezvous(status); // Trigger rendezvous on the supplied TRequestStatus object
       
   113             User::WaitForRequest(status); 
       
   114             
       
   115             // just lose the handle
       
   116             proc.Close();
       
   117     
       
   118             // check if command succesfull
       
   119             if( err != KErrNone )
       
   120                 {
       
   121                 LOGTEXT(_L("Profiler: could not connect engine"));
       
   122                 return err;
       
   123                 }
       
   124             
       
   125             // start sampling, using settings found in settings file or if not found the default settings
       
   126             err = RProfiler::StartSampling();
       
   127             // check if command succesful
       
   128             if( err != KErrNone )
       
   129                 {
       
   130                 _LIT(KNoteProfilerCannotStart, "PI Profiler: cannot start PI Profiler, check settings!");
       
   131                 PrintUsageInfo(KNoteProfilerCannotStart);
       
   132                 // check if process still alive
       
   133                 if(err != KErrNotFound)
       
   134                     {
       
   135                     // exit the profiler process since process was started
       
   136                     RProfiler::ExitProfiler();
       
   137                     }
       
   138                 return err;
       
   139                 }
       
   140             
       
   141             if(aRunTimeInSeconds > 0)
       
   142                 {
       
   143                 RDebug::Print(_L("Profiler running for %d s... "), aRunTimeInSeconds);
       
   144                 User::After(aRunTimeInSeconds*1000000);
       
   145                 RDebug::Print(_L("************* Profiler process closing *********"));
       
   146         
       
   147                 // stop sampling process
       
   148                 err = RProfiler::StopSampling();
       
   149                 // check if command succesfull
       
   150                 if( err != KErrNone )
       
   151                     {
       
   152                     LOGTEXT(_L("Profiler: could not connect engine, stop failed"));
       
   153                     return err;
       
   154                     }
       
   155                 
       
   156                 // exit the profiler process
       
   157                 err = RProfiler::ExitProfiler();
       
   158                 // check if command succesfull
       
   159                 if( err != KErrNone )
       
   160                     {
       
   161                     LOGTEXT(_L("Profiler: could not connect engine, exit failed"));
       
   162                     return err;
       
   163                     }
       
   164                 }
       
   165             } 
       
   166         else
       
   167             {
       
   168             _LIT(KNoteCannotFindProfiler, "PI Profiler: could not find PIProfilerEngine.exe");
       
   169             PrintUsageInfo(KNoteCannotFindProfiler);
       
   170             }
       
   171         }
       
   172     // check if old Profiler is already running
       
   173     else if( err == KErrAlreadyExists )
       
   174         {
       
   175         _LIT(KNoteAlreadyRunning, "PI Profiler: old Profiler process already running, close it down before launching the new!");
       
   176         PrintUsageInfo(KNoteAlreadyRunning);
       
   177         }
       
   178     // otherwise show error note
       
   179     else
       
   180         {
       
   181         _LIT(KNoteAlreadyRunning, "PI Profiler: already running, not able to launch new one. NOTE: check if UI running!");
       
   182         PrintUsageInfo(KNoteAlreadyRunning);
       
   183         }
       
   184     return KErrNone;
       
   185     }
       
   186 
       
   187 // --------------------------------------------------------------------------------------------
       
   188 static TInt EndProfilerProcess()
       
   189     {
       
   190     LOGTEXT(_L("EndProfilerProcess() ..."));
       
   191 
       
   192     // call profiler to stop sampling
       
   193     TInt err = RProfiler::StopSampling();
       
   194     
       
   195     // check if command succesfull
       
   196     if( err != KErrNone )
       
   197         {
       
   198         LOGTEXT(_L("Profiler: could not connect engine, stop failed"));
       
   199         return err;
       
   200         }
       
   201     
       
   202     // exit the profiler process
       
   203     err = RProfiler::ExitProfiler();
       
   204     // check if command succesfull
       
   205     if( err != KErrNone )
       
   206         {
       
   207         LOGTEXT(_L("Profiler: could not connect engine, exit failed"));
       
   208         return err;
       
   209         }
       
   210     
       
   211     return KErrNone;
       
   212     }
       
   213 
       
   214 // --------------------------------------------------------------------------------------------
       
   215 static TInt TestSettingsFile(const TDesC& configFile)
       
   216     {
       
   217     RFs fs;
       
   218     RFile file;
       
   219     TInt err(KErrNone);
       
   220 
       
   221     // check if file server can be connected
       
   222     if (fs.Connect() != KErrNone)
       
   223         {
       
   224         // file server couldn't be connected, return false
       
   225         return KErrNotFound;
       
   226         }
       
   227 
       
   228     // check if config file name length is > 0
       
   229     if (configFile.Length() > 0)
       
   230         {
       
   231         // open the file with the given path and name
       
   232         err = file.Open(fs,configFile,EFileRead);
       
   233         // check if file open was succesfull 
       
   234         if(err != KErrNone)
       
   235             {
       
   236             // return false if failed
       
   237             fs.Close();
       
   238             return err;
       
   239             }
       
   240         
       
   241         }
       
   242     else
       
   243         {
       
   244         // configFile length 0, return false
       
   245         fs.Close();
       
   246         return KErrNotFound;
       
   247         }
       
   248     // return true if tests passed
       
   249     file.Close();
       
   250     fs.Close();
       
   251     return KErrNone;
       
   252     }
       
   253 
       
   254 // --------------------------------------------------------------------------------------------
       
   255 static TInt ParseCommandAndExecute()
       
   256     {
       
   257     // commands literals for finding the right keyword 
       
   258     _LIT(KAutomatedTestStart,"start*");
       
   259     _LIT(KAutomatedTestEnd,"end*");
       
   260     _LIT(KAutomatedTestTimed,"timed*");
       
   261     
       
   262     TBuf<256> c;
       
   263     TInt match(KErrNotFound);
       
   264     
       
   265     // copy the full command line with arguments into a buffer
       
   266     User::CommandLine(c);
       
   267 
       
   268     // try to match with each of the literals defined above
       
   269     // (commands in atf format)
       
   270 
       
   271     // check if command is "start"
       
   272     match = c.Match(KAutomatedTestStart);
       
   273     if (match != KErrNotFound)
       
   274         {
       
   275         LOGTEXT(_L("Found keyword start"));
       
   276 
       
   277         TBuf<256> fileName;
       
   278         fileName.Append(c.Right(c.Length()-6));
       
   279         LOGSTRING2(_L("Filename is %S"), &fileName);
       
   280         if(TestSettingsFile(fileName) != KErrNone)
       
   281             {
       
   282             _LIT(KSettingsFileFailed, "False settings file");
       
   283             PrintUsageInfo(KSettingsFileFailed);
       
   284             return -2;
       
   285             }
       
   286         // execute Profile process 
       
   287         if( StartProfilerProcess(fileName, 0) == KErrNone )
       
   288             {
       
   289             return -10;
       
   290             }
       
   291         return -2;
       
   292         }
       
   293 
       
   294     // check if command is "end"
       
   295     match = c.Match(KAutomatedTestEnd);
       
   296     if (match != KErrNotFound)
       
   297         {
       
   298         LOGTEXT(_L("Found keyword end"));
       
   299 
       
   300         // stop the profiling process
       
   301         EndProfilerProcess();
       
   302         return -10;
       
   303         }
       
   304 
       
   305     // check if command is "timed"
       
   306     match = c.Match(KAutomatedTestTimed);
       
   307     if (match != KErrNotFound)
       
   308         {
       
   309         // command "timed" found, need for finding settings file and run time next
       
   310         LOGTEXT(_L("Found keyword timed"));
       
   311         TBuf<256> temp;
       
   312         temp.Append(c);
       
   313         TLex lex(temp);
       
   314         
       
   315         TBuf<256> fileName;
       
   316         TInt seconds;
       
   317 
       
   318         // parse the first command line argument, the command itself
       
   319         lex.Mark();
       
   320         lex.SkipCharacters();
       
   321         if(lex.TokenLength() != 0)
       
   322             {
       
   323             #ifdef PIPROFILER_PRINTS
       
   324             TPtrC token = lex.MarkedToken();
       
   325             LOGSTRING2("Token 1 %S",&token);
       
   326             #endif
       
   327             }
       
   328         else
       
   329             {
       
   330             LOGTEXT(_L("Problem 1 in parsing command line"));
       
   331             _LIT(KSettingsFileFailed, "Failure: False argument");
       
   332             PrintUsageInfo(KSettingsFileFailed);
       
   333             return -2;
       
   334             }
       
   335 
       
   336         // parse the second command line argument, the settings file name
       
   337         lex.SkipSpace();
       
   338         lex.Mark();
       
   339         lex.SkipCharacters();
       
   340         if(lex.TokenLength() != 0)
       
   341             {
       
   342             TPtrC token2 = lex.MarkedToken();
       
   343             LOGSTRING2(_L("Token 2 %S"),&token2);
       
   344             fileName.Append(token2);
       
   345             LOGSTRING2(_L("Value of fileName is %S"),&fileName);
       
   346 //            if(TestSettingsFile(fileName) != KErrNone)
       
   347 //                {
       
   348 //                _LIT(KSettingsFileFailed, "Failure: False settings file");
       
   349 //                PrintUsageInfo(KSettingsFileFailed);
       
   350 //                return -2;
       
   351 //                }
       
   352             }
       
   353         else
       
   354             {
       
   355             LOGTEXT(_L("Problem 2 in parsing command line"));
       
   356             _LIT(KSettingsFileFailed, "Failure: No settings file specified");
       
   357             PrintUsageInfo(KSettingsFileFailed);
       
   358             return -2;
       
   359             }
       
   360 
       
   361         // parse the third command line argument, the run time in seconds
       
   362         lex.SkipSpace();
       
   363         lex.Mark();
       
   364         lex.SkipCharacters();
       
   365         if(lex.TokenLength() != 0)
       
   366             {
       
   367             // third token ok, try to convert into TInt value
       
   368             TPtrC token3 = lex.MarkedToken();
       
   369             LOGSTRING2(_L("Token 3 %S"),&token3);
       
   370             TLex num(token3);
       
   371             TInt err = num.Val(seconds);
       
   372             // check if given time value acceptable 
       
   373             if (err != KErrNone)
       
   374                 {
       
   375                 // value parsing failed, show info note to user
       
   376                 _LIT(KSecondsFailed, "Failure: False time value");
       
   377                 PrintUsageInfo(KSecondsFailed);
       
   378                 return -2;
       
   379                 }
       
   380             }
       
   381         else
       
   382             {
       
   383             LOGTEXT(_L("Problem 3 in parsing command line"));
       
   384             _LIT(KSecondsFailed, "Failure: False time value");
       
   385             PrintUsageInfo(KSecondsFailed);
       
   386             return -2;
       
   387             }           
       
   388         
       
   389         LOGSTRING3(_L("Filename is %S, seconds is %d"), &fileName, seconds);
       
   390         // execute Profile process 
       
   391         if( StartProfilerProcess(fileName, seconds) == KErrNone )
       
   392             {
       
   393             return -10;
       
   394             }
       
   395         return -2;
       
   396         }
       
   397     
       
   398     // check if space character in the middle of command line string 
       
   399     if( c.LocateReverse(' ') != KErrNotFound)
       
   400         {
       
   401         _LIT(KWrongParameters, "Failure: Check command line parameters");
       
   402         PrintUsageInfo(KWrongParameters);
       
   403         return -2;
       
   404         }
       
   405     
       
   406     // return -1 if no command found
       
   407     LOGTEXT(_L("No keyword found"));
       
   408     return -1;
       
   409     }
       
   410 
       
   411 // --------------------------------------------------------------------------------------------
       
   412 GLDEF_C TInt E32Main()
       
   413     {
       
   414     // parse command line arguments
       
   415     ParseCommandAndExecute();
       
   416 
       
   417     return KErrNone;
       
   418 
       
   419     }