51
|
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");
|
62
|
38 |
_LIT(KLegalNote, "PIProfiler Version 2.2.0.3 Copyright @ 2009 Nokia\n");
|
51
|
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 |
* @param aRunTimeInSeconds run time in seconds
|
|
86 |
* @param aBootTime run boot time sampling or not.
|
|
87 |
*/
|
|
88 |
// --------------------------------------------------------------------------------------------
|
|
89 |
static TInt StartProfilerProcess(const TDesC& configFile, TInt aRunTimeInSeconds, TBool aBootTime)
|
|
90 |
{
|
|
91 |
TInt err(KErrNone);
|
|
92 |
RProcess proc;
|
|
93 |
TRequestStatus status = KRequestPending;
|
|
94 |
TBuf<256> conf;
|
|
95 |
conf.Zero();
|
|
96 |
conf.Append(configFile);
|
|
97 |
if(aBootTime)
|
|
98 |
{
|
62
|
99 |
RDebug::Printf("PIProfiler: StartProfilerProcess: boot time measurement");
|
51
|
100 |
conf.Append(_L(" "));
|
|
101 |
conf.Append(_L("boot"));
|
|
102 |
}
|
|
103 |
RDebug::RawPrint(conf);
|
62
|
104 |
RDebug::Printf("PIProfiler: StartProfilerProcess run time is %d", aRunTimeInSeconds);
|
51
|
105 |
// check if process exists
|
|
106 |
err = FindProcess();
|
|
107 |
LOGSTRING2("PIProfiler: tried to find process, response %d", err);
|
|
108 |
|
|
109 |
// check if already exists and don't start a new eshell profiling
|
|
110 |
if( err == KErrNotFound )
|
|
111 |
{
|
|
112 |
// try create new process
|
|
113 |
err = proc.Create(KProfilerEngineExe, conf);
|
|
114 |
|
|
115 |
LOGSTRING2("PIProfiler: created process, response %d", err);
|
|
116 |
|
|
117 |
// check if RProcess::Create() succeeded
|
|
118 |
if( err == KErrNone )
|
|
119 |
{
|
62
|
120 |
LOGSTRING("PI Profiler start rendezvous ");
|
51
|
121 |
// Trigger rendezvous on the supplied TRequestStatus object
|
|
122 |
proc.Rendezvous(status);
|
62
|
123 |
LOGSTRING("PI Profiler start resume ");
|
51
|
124 |
// kick off the engine process
|
|
125 |
proc.Resume();
|
62
|
126 |
LOGSTRING("PI Profiler start waitforrequest ");
|
51
|
127 |
// wait for the constructor to complete
|
|
128 |
User::WaitForRequest(status);
|
62
|
129 |
LOGSTRING("PI Profiler start close ");
|
51
|
130 |
// just lose the handle
|
|
131 |
proc.Close();
|
62
|
132 |
LOGSTRING("PI Profiler start: closed");
|
51
|
133 |
// start sampling, using settings found in settings file or if not found the default settings
|
|
134 |
err = RProfiler::StartSampling();
|
62
|
135 |
LOGSTRING2("PI Profiler start: err %d", err);
|
51
|
136 |
// check if command succesful
|
|
137 |
if( err != KErrNone )
|
|
138 |
{
|
|
139 |
LOGSTRING2("PI Profiler start: err %d", err);
|
|
140 |
_LIT(KNoteProfilerCannotStart, "PI Profiler: cannot start PI Profiler, check settings!");
|
|
141 |
PrintUsageInfo(KNoteProfilerCannotStart);
|
|
142 |
|
|
143 |
// check if process still alive
|
|
144 |
if(err != KErrNotFound)
|
|
145 |
{
|
|
146 |
// exit the profiler process since process was started
|
|
147 |
RProfiler::ExitProfiler();
|
|
148 |
}
|
|
149 |
return err;
|
|
150 |
}
|
|
151 |
|
|
152 |
if(aRunTimeInSeconds > 0)
|
|
153 |
{
|
|
154 |
RDebug::Print(_L("Profiler running for %d s... "), aRunTimeInSeconds);
|
|
155 |
User::After(aRunTimeInSeconds*1000000);
|
|
156 |
RDebug::Print(_L("************* Profiler process closing *********"));
|
|
157 |
|
|
158 |
// stop sampling process
|
|
159 |
err = RProfiler::StopSampling();
|
|
160 |
// check if command succesfull
|
|
161 |
if( err != KErrNone )
|
|
162 |
{
|
|
163 |
LOGTEXT(_L("Profiler: could not connect engine, stop failed"));
|
|
164 |
return err;
|
|
165 |
}
|
|
166 |
|
|
167 |
// exit the profiler process
|
|
168 |
err = RProfiler::ExitProfiler();
|
|
169 |
// check if command succesfull
|
|
170 |
if( err != KErrNone )
|
|
171 |
{
|
|
172 |
LOGTEXT(_L("Profiler: could not connect engine, exit failed"));
|
|
173 |
return err;
|
|
174 |
}
|
|
175 |
}
|
|
176 |
}
|
|
177 |
else
|
|
178 |
{
|
|
179 |
_LIT(KNoteCannotFindProfiler, "PI Profiler: could not find PIProfilerEngine.exe");
|
|
180 |
PrintUsageInfo(KNoteCannotFindProfiler);
|
|
181 |
}
|
|
182 |
}
|
|
183 |
// check if old Profiler is already running
|
|
184 |
else if( err == KErrAlreadyExists )
|
|
185 |
{
|
|
186 |
_LIT(KNoteAlreadyRunning, "PI Profiler: old Profiler process already running, close it down before launching the new!");
|
|
187 |
PrintUsageInfo(KNoteAlreadyRunning);
|
|
188 |
}
|
|
189 |
// otherwise show error note
|
|
190 |
else
|
|
191 |
{
|
|
192 |
_LIT(KNoteAlreadyRunning, "PI Profiler: already running, not able to launch new one. NOTE: check if UI running!");
|
|
193 |
PrintUsageInfo(KNoteAlreadyRunning);
|
|
194 |
}
|
|
195 |
return KErrNone;
|
|
196 |
}
|
|
197 |
|
|
198 |
// --------------------------------------------------------------------------------------------
|
|
199 |
static TInt EndProfilerProcess()
|
|
200 |
{
|
|
201 |
LOGTEXT(_L("EndProfilerProcess() ..."));
|
|
202 |
|
|
203 |
// call profiler to stop sampling
|
|
204 |
TInt err = RProfiler::StopSampling();
|
|
205 |
|
|
206 |
// check if command succesfull
|
|
207 |
if( err != KErrNone )
|
|
208 |
{
|
|
209 |
LOGTEXT(_L("Profiler: could not connect engine, stop failed"));
|
|
210 |
return err;
|
|
211 |
}
|
|
212 |
|
|
213 |
// exit the profiler process
|
|
214 |
err = RProfiler::ExitProfiler();
|
|
215 |
// check if command succesfull
|
|
216 |
if( err != KErrNone )
|
|
217 |
{
|
|
218 |
LOGTEXT(_L("Profiler: could not connect engine, exit failed"));
|
|
219 |
return err;
|
|
220 |
}
|
|
221 |
|
|
222 |
return KErrNone;
|
|
223 |
}
|
|
224 |
|
|
225 |
// --------------------------------------------------------------------------------------------
|
|
226 |
static TInt TestSettingsFile(const TDesC& configFile)
|
|
227 |
{
|
|
228 |
RFs fs;
|
|
229 |
RFile file;
|
|
230 |
TInt err(KErrNone);
|
|
231 |
|
|
232 |
// check if file server can be connected
|
|
233 |
if (fs.Connect() != KErrNone)
|
|
234 |
{
|
|
235 |
// file server couldn't be connected, return false
|
|
236 |
return KErrNotFound;
|
|
237 |
}
|
|
238 |
|
|
239 |
// check if config file name length is > 0
|
|
240 |
if (configFile.Length() > 0)
|
|
241 |
{
|
|
242 |
// open the file with the given path and name
|
|
243 |
err = file.Open(fs,configFile,EFileRead);
|
|
244 |
// check if file open was succesfull
|
|
245 |
if(err != KErrNone)
|
|
246 |
{
|
|
247 |
// return false if failed
|
|
248 |
fs.Close();
|
|
249 |
return err;
|
|
250 |
}
|
|
251 |
|
|
252 |
}
|
|
253 |
else
|
|
254 |
{
|
|
255 |
// configFile length 0, return false
|
|
256 |
fs.Close();
|
|
257 |
return KErrNotFound;
|
|
258 |
}
|
|
259 |
// return true if tests passed
|
|
260 |
file.Close();
|
|
261 |
fs.Close();
|
|
262 |
return KErrNone;
|
|
263 |
}
|
|
264 |
|
|
265 |
// --------------------------------------------------------------------------------------------
|
|
266 |
static TInt ParseCommandAndExecute()
|
|
267 |
{
|
|
268 |
// commands literals for finding the right keyword
|
|
269 |
_LIT(KAutomatedTestStart,"start*");
|
|
270 |
_LIT(KAutomatedTestEnd,"end*");
|
|
271 |
_LIT(KAutomatedTestTimed,"timed*");
|
|
272 |
_LIT(KBootMeasurement,"boot*");
|
|
273 |
TBuf<256> c;
|
|
274 |
TInt match(KErrNotFound);
|
|
275 |
TInt bootmatch(KErrNotFound);
|
|
276 |
TBool myBoot=false;
|
|
277 |
// copy the full command line with arguments into a buffer
|
|
278 |
User::CommandLine(c);
|
62
|
279 |
LOGSTRING2("ParseCommandAndExecute command: %S", &c);
|
51
|
280 |
|
|
281 |
// try to match with each of the literals defined above
|
|
282 |
// (commands in atf format)
|
|
283 |
|
|
284 |
// check if command is "start"
|
|
285 |
match = c.Match(KAutomatedTestStart);
|
|
286 |
if (match != KErrNotFound)
|
|
287 |
{
|
62
|
288 |
LOGTEXT(_L("ParseCommandAndExecute Found keyword start"));
|
51
|
289 |
|
|
290 |
TBuf<256> fileName;
|
|
291 |
fileName.Append(c.Right(c.Length()-6));
|
62
|
292 |
LOGSTRING2("ParseCommandAndExecute Filename is %S", &fileName);
|
51
|
293 |
if(TestSettingsFile(fileName) != KErrNone)
|
|
294 |
{
|
|
295 |
_LIT(KSettingsFileFailed, "False settings file");
|
|
296 |
PrintUsageInfo(KSettingsFileFailed);
|
|
297 |
return -2;
|
|
298 |
}
|
|
299 |
// execute Profile process
|
|
300 |
if( StartProfilerProcess(fileName, 0, myBoot) == KErrNone )
|
|
301 |
{
|
|
302 |
return -10;
|
|
303 |
}
|
|
304 |
return -2;
|
|
305 |
}
|
|
306 |
|
|
307 |
// check if command is "end"
|
|
308 |
match = c.Match(KAutomatedTestEnd);
|
|
309 |
if (match != KErrNotFound)
|
|
310 |
{
|
|
311 |
LOGTEXT(_L("Found keyword end"));
|
|
312 |
|
|
313 |
// stop the profiling process
|
|
314 |
EndProfilerProcess();
|
|
315 |
return -10;
|
|
316 |
}
|
|
317 |
|
|
318 |
// check if command is "timed"
|
|
319 |
match = c.Match(KAutomatedTestTimed);
|
|
320 |
// check if command is "boot"
|
|
321 |
bootmatch = c.Match(KBootMeasurement);
|
|
322 |
if ((match!= KErrNotFound) || (bootmatch != KErrNotFound))
|
|
323 |
{
|
|
324 |
// command "timed" or " boot" found, need for finding settings file and run time next
|
|
325 |
if(bootmatch != KErrNotFound)
|
|
326 |
{
|
62
|
327 |
LOGTEXT(_L("ParseCommandAndExecute Found keyword boot"));
|
51
|
328 |
myBoot = TRUE;
|
|
329 |
}
|
|
330 |
if(match != KErrNotFound)
|
|
331 |
{
|
62
|
332 |
LOGTEXT(_L("ParseCommandAndExecute Found keyword timed"));
|
51
|
333 |
}
|
|
334 |
|
|
335 |
TBuf<256> temp;
|
|
336 |
temp.Append(c);
|
|
337 |
TLex lex(temp);
|
|
338 |
|
|
339 |
TBuf<256> fileName;
|
|
340 |
TInt seconds;
|
|
341 |
|
|
342 |
// parse the first command line argument, the command itself
|
|
343 |
lex.Mark();
|
|
344 |
lex.SkipCharacters();
|
|
345 |
if(lex.TokenLength() != 0)
|
|
346 |
{
|
|
347 |
#ifdef PIPROFILER_PRINTS
|
|
348 |
TPtrC token = lex.MarkedToken();
|
|
349 |
LOGSTRING2("Token 1 %S",&token);
|
|
350 |
#endif
|
|
351 |
}
|
|
352 |
else
|
|
353 |
{
|
|
354 |
LOGTEXT(_L("Problem 1 in parsing command line"));
|
|
355 |
_LIT(KSettingsFileFailed, "Failure: False argument");
|
|
356 |
PrintUsageInfo(KSettingsFileFailed);
|
|
357 |
return -2;
|
|
358 |
}
|
|
359 |
|
|
360 |
// parse the second command line argument, the settings file name
|
|
361 |
lex.SkipSpace();
|
|
362 |
lex.Mark();
|
|
363 |
lex.SkipCharacters();
|
|
364 |
if(lex.TokenLength() != 0)
|
|
365 |
{
|
|
366 |
TPtrC token2 = lex.MarkedToken();
|
|
367 |
LOGSTRING2("Token 2 %S",&token2);
|
|
368 |
fileName.Append(token2);
|
|
369 |
LOGSTRING2("Value of fileName is %S",&fileName);
|
|
370 |
// if(TestSettingsFile(fileName) != KErrNone)
|
|
371 |
// {
|
|
372 |
// _LIT(KSettingsFileFailed, "Failure: False settings file");
|
|
373 |
// PrintUsageInfo(KSettingsFileFailed);
|
|
374 |
// return -2;
|
|
375 |
// }
|
|
376 |
}
|
|
377 |
else
|
|
378 |
{
|
|
379 |
LOGTEXT(_L("Problem 2 in parsing command line"));
|
|
380 |
_LIT(KSettingsFileFailed, "Failure: No settings file specified");
|
|
381 |
PrintUsageInfo(KSettingsFileFailed);
|
|
382 |
return -2;
|
|
383 |
}
|
|
384 |
|
|
385 |
// parse the third command line argument, the run time in seconds
|
|
386 |
lex.SkipSpace();
|
|
387 |
lex.Mark();
|
|
388 |
lex.SkipCharacters();
|
|
389 |
if(lex.TokenLength() != 0)
|
|
390 |
{
|
|
391 |
// third token ok, try to convert into TInt value
|
|
392 |
TPtrC token3 = lex.MarkedToken();
|
|
393 |
LOGSTRING2("Token 3 %S",&token3);
|
|
394 |
TLex num(token3);
|
|
395 |
TInt err = num.Val(seconds);
|
|
396 |
// check if given time value acceptable
|
|
397 |
if (err != KErrNone)
|
|
398 |
{
|
|
399 |
// value parsing failed, show info note to user
|
|
400 |
_LIT(KSecondsFailed, "Failure: False time value");
|
|
401 |
PrintUsageInfo(KSecondsFailed);
|
|
402 |
return -2;
|
|
403 |
}
|
|
404 |
}
|
|
405 |
else
|
|
406 |
{
|
|
407 |
LOGTEXT(_L("Problem 3 in parsing command line"));
|
|
408 |
_LIT(KSecondsFailed, "Failure: False time value");
|
|
409 |
PrintUsageInfo(KSecondsFailed);
|
|
410 |
return -2;
|
|
411 |
}
|
|
412 |
|
|
413 |
LOGSTRING3("Filename is %S, seconds is %d", &fileName, seconds);
|
|
414 |
// execute Profile process
|
|
415 |
if( StartProfilerProcess(fileName, seconds, myBoot) == KErrNone )
|
|
416 |
{
|
|
417 |
return -10;
|
|
418 |
}
|
|
419 |
return -2;
|
|
420 |
}
|
|
421 |
|
|
422 |
// check if space character in the middle of command line string
|
|
423 |
if( c.LocateReverse(' ') != KErrNotFound)
|
|
424 |
{
|
|
425 |
_LIT(KWrongParameters, "Failure: Check command line parameters");
|
|
426 |
PrintUsageInfo(KWrongParameters);
|
|
427 |
return -2;
|
|
428 |
}
|
|
429 |
|
|
430 |
// return -1 if no command found
|
|
431 |
LOGTEXT(_L("No keyword found"));
|
|
432 |
return -1;
|
|
433 |
}
|
|
434 |
|
|
435 |
// --------------------------------------------------------------------------------------------
|
|
436 |
GLDEF_C TInt E32Main()
|
|
437 |
{
|
|
438 |
// parse command line arguments
|
|
439 |
ParseCommandAndExecute();
|
|
440 |
|
|
441 |
return KErrNone;
|
|
442 |
|
|
443 |
}
|