1 /* |
|
2 * Copyright (c) 2007 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: Class for managing intercommunication between Camera UI* |
|
15 */ |
|
16 |
|
17 |
|
18 // INCLUDE FILES |
|
19 |
|
20 #include <e32std.h> |
|
21 #include <e32base.h> // TTime |
|
22 #include <eikenv.h> |
|
23 #include <s32file.h> // RFileWriteStream |
|
24 |
|
25 #include "CamPerformance.h" |
|
26 #include "CamAppUid.h" // KCameraappUID |
|
27 |
|
28 #ifdef CAMERAAPP_PERF_LOG_MEMORY |
|
29 // This file needs only be compiled if logging to memory is enabled |
|
30 |
|
31 // INTERNAL CONSTANTS, ENUMS AND STRUCTS |
|
32 |
|
33 // Constants related to logging in memory |
|
34 const TInt KPerfLogArrayGranularity = 20; |
|
35 const TInt KPerfMaxLogItemStringLength = 160; |
|
36 |
|
37 // Constants for converting 64-bit system time to milliseconds |
|
38 // and seconds |
|
39 const TInt KDividerSystemToMilliseconds = 1000; |
|
40 const TInt KDividerMillisecondsToSeconds = 1000; |
|
41 |
|
42 // Constants for formatting memory log to text |
|
43 _LIT( KPerfLogItemTab, "\t" ); |
|
44 _LIT8( KPerfLogItemCrLf8, "\n" ); |
|
45 _LIT( KPerfLogItemSecondsFormatSpace, "%6d.%03d" ); |
|
46 _LIT( KPerfLogItemSecondsFormat, "%d.%03d" ); |
|
47 |
|
48 // Constants for writing event analysis log |
|
49 _LIT( KAnalysisEventType, "Event: %d" ); |
|
50 _LIT( KAnalysisEventStartTime, ", start time: " ); |
|
51 _LIT( KAnalysisEventEndTime, ", end time: " ); |
|
52 _LIT( KAnalysisEventDuration, ", duration: " ); |
|
53 _LIT( KAnalysisEventAlreadyStarted, "Start for event %d, which has already been started, time: " ); |
|
54 _LIT( KAnalysisEndWithoutStart, "End for event %d without start, time: " ); |
|
55 _LIT( KAnalysisStartWithoutEnd, "Start for event %d without end, time: " ); |
|
56 |
|
57 _LIT( KCamPerformanceLogger, "CamPerformanceLogger" ); |
|
58 |
|
59 /** |
|
60 * Memory log item types |
|
61 */ |
|
62 enum TItemType |
|
63 { |
|
64 EItemEventStart, |
|
65 EItemEventEnd, |
|
66 EItemMessage, |
|
67 EItemEngineStateChange, |
|
68 EItemOperationStateChange |
|
69 }; |
|
70 |
|
71 /** |
|
72 * TLogitem struct definition. Used for storing log items in memory |
|
73 * in an array. |
|
74 */ |
|
75 struct TLogItem |
|
76 { |
|
77 public: |
|
78 TLogItem( TItemType aItemType, TInt aIntValue, TInt64 aTime ): |
|
79 iItemType( aItemType ), iIntValue( aIntValue), iTime( aTime ) {} |
|
80 |
|
81 TItemType iItemType; |
|
82 TInt iIntValue; |
|
83 TInt64 iTime; |
|
84 }; |
|
85 |
|
86 // ============================ MEMBER FUNCTIONS =============================== |
|
87 |
|
88 // ----------------------------------------------------------------------------- |
|
89 // CCamPerformanceLogger::CCamPerformanceLogger |
|
90 // C++ default constructor can NOT contain any code, that |
|
91 // might leave |
|
92 // ----------------------------------------------------------------------------- |
|
93 // |
|
94 CCamPerformanceLogger::CCamPerformanceLogger() : |
|
95 CCoeStatic( TUid::Uid( KCameraappUID ) ), iLogItems( KPerfLogArrayGranularity ) |
|
96 { |
|
97 iStartTime = Time64(); |
|
98 } |
|
99 |
|
100 // ----------------------------------------------------------------------------- |
|
101 // CCamPerformanceLogger::~CCamPerformanceLogger |
|
102 // Destructor |
|
103 // ----------------------------------------------------------------------------- |
|
104 // |
|
105 CCamPerformanceLogger::~CCamPerformanceLogger() |
|
106 { |
|
107 PRINT( _L("Camera => ~CCamPerformanceLogger") ); |
|
108 if( iLogItems.Count() > 0 ) |
|
109 { |
|
110 // Write files only if there are new log items |
|
111 // This is to avoid overwriting log already written using SaveAndReset() |
|
112 TRAP_IGNORE( SaveLogDataL() ); |
|
113 TRAP_IGNORE( SaveAnalysisL() ); |
|
114 } |
|
115 iLogItems.Close(); |
|
116 PRINT( _L("Camera <= ~CCamPerformanceLogger") ); |
|
117 } |
|
118 |
|
119 // --------------------------------------------------------------------------- |
|
120 // CCamPerformanceLogger::Logger |
|
121 // Static function, which returns a pointer to the currently active |
|
122 // CCamPerformanceLogger object or instantiates a new one |
|
123 // --------------------------------------------------------------------------- |
|
124 // |
|
125 CCamPerformanceLogger* CCamPerformanceLogger::Logger() |
|
126 { |
|
127 // Get pointer to the current CCamPerformanceLogger object using CCoeEnv.FindStatic() |
|
128 CCamPerformanceLogger* self = static_cast<CCamPerformanceLogger*>( CCoeEnv::Static()->FindStatic( TUid::Uid( KCameraappUID ) ) ); |
|
129 if( self ) |
|
130 { |
|
131 return self; |
|
132 } |
|
133 else |
|
134 { |
|
135 // FindStatic returned null, create a new instance |
|
136 self = new CCamPerformanceLogger(); |
|
137 if( !self ) |
|
138 { |
|
139 // Not enough memory to instantiate CCamPerfomranceLogger |
|
140 User::Panic( KCamPerformanceLogger, KErrNoMemory ); |
|
141 } |
|
142 return self; |
|
143 } |
|
144 } |
|
145 |
|
146 // --------------------------------------------------------------------------- |
|
147 // CCamPerformanceLogger::SaveAndReset |
|
148 // Static function, which saves the recoded log data and clears the log |
|
149 // --------------------------------------------------------------------------- |
|
150 // |
|
151 void CCamPerformanceLogger::SaveAndReset() |
|
152 { |
|
153 // Get pointer to the current CCamPerformanceLogger object using CCoeEnv.FindStatic() |
|
154 CCamPerformanceLogger* self = static_cast<CCamPerformanceLogger*>( CCoeEnv::Static()->FindStatic( TUid::Uid( KCameraappUID ) ) ); |
|
155 |
|
156 if( self && self->iLogItems.Count() > 0 ) |
|
157 { |
|
158 TRAP_IGNORE( self->SaveLogDataL() ); |
|
159 TRAP_IGNORE( self->SaveAnalysisL() ); |
|
160 |
|
161 // Clear the logitems array |
|
162 self->iLogItems.Reset(); |
|
163 } |
|
164 } |
|
165 |
|
166 // --------------------------------------------------------------------------- |
|
167 // CCamPerformanceLogger::EventStart |
|
168 // Appends an event start item to the memory log |
|
169 // --------------------------------------------------------------------------- |
|
170 // |
|
171 void CCamPerformanceLogger::EventStart( TCamEvent aEvent ) |
|
172 { |
|
173 TLogItem item( EItemEventStart, aEvent, Time64() - iStartTime ); |
|
174 iLogItems.Append( item ); // Ignore return value |
|
175 } |
|
176 |
|
177 // --------------------------------------------------------------------------- |
|
178 // CCamPerformanceLogger::EventEnd |
|
179 // Appends an event end item to the memory log |
|
180 // --------------------------------------------------------------------------- |
|
181 // |
|
182 void CCamPerformanceLogger::EventEnd( TCamEvent aEvent ) |
|
183 { |
|
184 TLogItem item( EItemEventEnd, aEvent, Time64() - iStartTime ); |
|
185 iLogItems.Append( item ); // Ignore return value |
|
186 } |
|
187 |
|
188 // --------------------------------------------------------------------------- |
|
189 // CCamPerformanceLogger::Message |
|
190 // Appends a message to the memory log |
|
191 // --------------------------------------------------------------------------- |
|
192 // |
|
193 void CCamPerformanceLogger::Message( TCamMessage aMessage ) |
|
194 { |
|
195 TLogItem item( EItemMessage, aMessage, Time64() - iStartTime ); |
|
196 iLogItems.Append( item ); // Ignore return value |
|
197 } |
|
198 |
|
199 // --------------------------------------------------------------------------- |
|
200 // CCamPerformanceLogger::EngineState |
|
201 // Appends an engine state change to the memory log |
|
202 // --------------------------------------------------------------------------- |
|
203 // |
|
204 void CCamPerformanceLogger::EngineState( TInt aState ) |
|
205 { |
|
206 TLogItem item( EItemEngineStateChange, aState, Time64() - iStartTime ); |
|
207 iLogItems.Append( item ); // Ignore return value |
|
208 } |
|
209 |
|
210 // --------------------------------------------------------------------------- |
|
211 // CCamPerformanceLogger::OperationState |
|
212 // Appends an operation state change to the memory log |
|
213 // --------------------------------------------------------------------------- |
|
214 // |
|
215 void CCamPerformanceLogger::OperationState( TInt aState ) |
|
216 { |
|
217 TLogItem item( EItemOperationStateChange, aState, Time64() - iStartTime ); |
|
218 iLogItems.Append( item ); // Ignore return value |
|
219 } |
|
220 |
|
221 // --------------------------------------------------------------------------- |
|
222 // CCamPerformanceLogger::LogItemToDes |
|
223 // Converts log item data into LogicAnalyzer compatible string, and stores |
|
224 // it in aDes |
|
225 // --------------------------------------------------------------------------- |
|
226 // |
|
227 void CCamPerformanceLogger::LogItemToDes( const TLogItem& aItem, TDes& aDes ) |
|
228 { |
|
229 // Clear the descriptor contents |
|
230 aDes.Zero(); |
|
231 |
|
232 // Append time of the log item and space |
|
233 TInt64 time = aItem.iTime; |
|
234 AppendTime( aDes, time, ETrue ); |
|
235 aDes.Append( KPerfLogItemTab ); |
|
236 |
|
237 // Append item type specific formatted string |
|
238 switch( aItem.iItemType ) |
|
239 { |
|
240 case EItemEventStart: |
|
241 { |
|
242 aDes.AppendFormat( KPerfEventStart, aItem.iIntValue ); |
|
243 } |
|
244 break; |
|
245 case EItemEventEnd: |
|
246 { |
|
247 aDes.AppendFormat( KPerfEventEnd, aItem.iIntValue ); |
|
248 } |
|
249 break; |
|
250 case EItemMessage: |
|
251 { |
|
252 aDes.AppendFormat( KPerfMessage, aItem.iIntValue ); |
|
253 } |
|
254 break; |
|
255 case EItemEngineStateChange: |
|
256 { |
|
257 aDes.AppendFormat( KPerfEngineStateChange, aItem.iIntValue ); |
|
258 } |
|
259 break; |
|
260 case EItemOperationStateChange: |
|
261 { |
|
262 aDes.AppendFormat( KPerfOperationStateChange, aItem.iIntValue ); |
|
263 } |
|
264 break; |
|
265 default: |
|
266 { |
|
267 aDes.AppendFormat( KPerfUnknown, aItem.iIntValue ); |
|
268 } |
|
269 break; |
|
270 } |
|
271 } |
|
272 |
|
273 |
|
274 // --------------------------------------------------------------------------- |
|
275 // CCamPerformanceLogger::AppendTime |
|
276 // Appends time represendted by aTime to aDes with format seconds.milliseconds |
|
277 // --------------------------------------------------------------------------- |
|
278 // |
|
279 void CCamPerformanceLogger::AppendTime( TDes& aDes, TInt64 aTime, TBool aSpace ) |
|
280 { |
|
281 // Convert system time to milliseconds |
|
282 TInt64 timeInMillis = aTime / KDividerSystemToMilliseconds; |
|
283 |
|
284 // Get seconds and remainder (milliseconds) |
|
285 TInt seconds = timeInMillis / KDividerMillisecondsToSeconds; |
|
286 TInt milliseconds = timeInMillis % KDividerMillisecondsToSeconds; |
|
287 |
|
288 // Append seconds to the log item, with or without trailing space |
|
289 if( aSpace ) |
|
290 { |
|
291 aDes.AppendFormat( KPerfLogItemSecondsFormatSpace, seconds, milliseconds ); |
|
292 } |
|
293 else |
|
294 { |
|
295 aDes.AppendFormat( KPerfLogItemSecondsFormat, seconds, milliseconds ); |
|
296 } |
|
297 } |
|
298 |
|
299 // --------------------------------------------------------------------------- |
|
300 // CCamPerformanceLogger::SaveLogDataL |
|
301 // Saves all data from memory log to file KPerfLogFilename |
|
302 // --------------------------------------------------------------------------- |
|
303 // |
|
304 void CCamPerformanceLogger::SaveLogDataL() const |
|
305 { |
|
306 TBuf<KPerfMaxLogItemStringLength> itemDes; |
|
307 |
|
308 // Connect to file server and create the output stream |
|
309 RFs fs; |
|
310 User::LeaveIfError( fs.Connect() ); |
|
311 CleanupClosePushL( fs ); |
|
312 |
|
313 RFileWriteStream writeStream; |
|
314 User::LeaveIfError( writeStream.Replace( fs, KPerfLogFilename, EFileWrite ) ); |
|
315 writeStream.PushL(); |
|
316 |
|
317 // Convert each item to Des8 and write to the stream |
|
318 TInt n = iLogItems.Count(); |
|
319 for( int i=0; i<n; i++ ) |
|
320 { |
|
321 const TLogItem& item = iLogItems[i]; |
|
322 LogItemToDes( item, itemDes ); |
|
323 WriteLineL( writeStream, itemDes ); |
|
324 } |
|
325 |
|
326 // Commit and release the stream |
|
327 writeStream.CommitL(); |
|
328 writeStream.Pop(); |
|
329 writeStream.Release(); |
|
330 |
|
331 CleanupStack::PopAndDestroy(); // fs |
|
332 } |
|
333 |
|
334 // --------------------------------------------------------------------------- |
|
335 // CCamPerformanceLogger::SaveAnalysisL |
|
336 // Performs simple analysis to event data from memory log and writes the |
|
337 // result to file KPerfAnalysisFilenam |
|
338 // --------------------------------------------------------------------------- |
|
339 // |
|
340 void CCamPerformanceLogger::SaveAnalysisL() const |
|
341 { |
|
342 #ifdef CAMERAAPP_PERF_LOG_ANALYZE_EVENTS |
|
343 |
|
344 TBuf<KPerfMaxLogItemStringLength> itemDes; |
|
345 |
|
346 TBool eventStatus[ EPerfEventLastEvent ]; |
|
347 TInt64 startTimes[ EPerfEventLastEvent ]; |
|
348 |
|
349 for( int i=0; i<EPerfEventLastEvent; i++) |
|
350 { |
|
351 eventStatus[i] = EFalse; |
|
352 } |
|
353 |
|
354 // Connect to file server and create the output stream |
|
355 RFs fs; |
|
356 User::LeaveIfError( fs.Connect() ); |
|
357 CleanupClosePushL( fs ); |
|
358 |
|
359 RFileWriteStream writeStream; |
|
360 User::LeaveIfError( writeStream.Replace( fs, KPerfAnalysisFileName, EFileWrite ) ); |
|
361 writeStream.PushL(); |
|
362 |
|
363 TInt n = iLogItems.Count(); |
|
364 |
|
365 // Go through each item in the memory log |
|
366 for( TInt i=0; i<n; i++ ) |
|
367 { |
|
368 const TLogItem& item = iLogItems[i]; |
|
369 |
|
370 TInt intValue = item.iIntValue; |
|
371 TInt64 time = item.iTime; |
|
372 TBool status = eventStatus[ intValue ]; |
|
373 |
|
374 if( EItemEventStart == item.iItemType ) |
|
375 { |
|
376 if( !status ) |
|
377 { |
|
378 // Start for an event that has not yet been start |
|
379 eventStatus[ intValue ] = ETrue; |
|
380 startTimes[ intValue ] = time; |
|
381 } |
|
382 else |
|
383 { |
|
384 // Start for an event that has already been started |
|
385 // Replace old start time with the new one |
|
386 startTimes[ intValue ] = time; |
|
387 #ifdef CAMERAAPP_PERF_ANALYSIS_WARN_MULTIPLE_START |
|
388 itemDes.Format( KAnalysisEventAlreadyStarted, intValue ); |
|
389 AppendTime( itemDes, time ); |
|
390 WriteLineL( writeStream, itemDes ); |
|
391 #endif |
|
392 } |
|
393 } |
|
394 else if( EItemEventEnd == item.iItemType ) |
|
395 { |
|
396 if( status ) |
|
397 { |
|
398 // End for an event that has been started |
|
399 itemDes.Format( KAnalysisEventType, intValue ); |
|
400 itemDes.Append( KAnalysisEventStartTime ); |
|
401 AppendTime( itemDes, startTimes[ intValue ] ); |
|
402 itemDes.Append( KAnalysisEventEndTime ); |
|
403 AppendTime( itemDes, time ); |
|
404 itemDes.Append( KAnalysisEventDuration ); |
|
405 AppendTime( itemDes, time - startTimes[ intValue ] ); |
|
406 WriteLineL( writeStream, itemDes ); |
|
407 |
|
408 eventStatus[ intValue ] = EFalse; |
|
409 } |
|
410 else |
|
411 { |
|
412 // End for an event that has not been started |
|
413 #ifdef CAMERAAPP_PERF_ANALYSIS_WARN_END_WITHOUT_START |
|
414 itemDes.Format( KAnalysisEndWithoutStart, intValue ); |
|
415 AppendTime( itemDes, time ); |
|
416 WriteLineL( writeStream, itemDes ); |
|
417 #endif |
|
418 } |
|
419 } |
|
420 else |
|
421 { |
|
422 // Ignore other event types |
|
423 } |
|
424 } |
|
425 |
|
426 #ifdef CAMERAAPP_PERF_ANALYSIS_WARN_START_WITHOUT_END |
|
427 for( int i=0; i<EPerfEventLastEvent; i++ ) |
|
428 { |
|
429 if( eventStatus[ i ] ) |
|
430 { |
|
431 itemDes.Format( KAnalysisStartWithoutEnd, i ); |
|
432 AppendTime( itemDes, startTimes[ i ] ); |
|
433 WriteLineL( writeStream, itemDes ); |
|
434 } |
|
435 } |
|
436 #endif |
|
437 |
|
438 // Commit and release the stream |
|
439 writeStream.CommitL(); |
|
440 writeStream.Pop(); |
|
441 writeStream.Release(); |
|
442 |
|
443 CleanupStack::PopAndDestroy(); // fs |
|
444 |
|
445 #endif // CAMERAAPP_PERF_LOG_ANALYZE_EVENTS |
|
446 } |
|
447 |
|
448 |
|
449 // --------------------------------------------------------------------------- |
|
450 // CCamPerformanceLogger::WriteLineL |
|
451 // Writes the contents of descriptor aDes followed by '\n' to aStream |
|
452 // --------------------------------------------------------------------------- |
|
453 // |
|
454 void CCamPerformanceLogger::WriteLineL( RFileWriteStream& aStream, TDes& aDes ) |
|
455 { |
|
456 TBuf8<KPerfMaxLogItemStringLength> des8; |
|
457 des8.Copy( aDes ); |
|
458 aStream.WriteL( des8 ); |
|
459 aStream.WriteL( KPerfLogItemCrLf8 ); |
|
460 } |
|
461 |
|
462 // --------------------------------------------------------------------------- |
|
463 // CCamPerformanceLogger::Time64 |
|
464 // Returns system 64-bit representation of the current time |
|
465 // --------------------------------------------------------------------------- |
|
466 // |
|
467 TInt64 CCamPerformanceLogger::Time64() |
|
468 { |
|
469 TTime time; |
|
470 time.HomeTime(); |
|
471 return time.Int64(); |
|
472 } |
|
473 |
|
474 #endif // CAMERAAPP_PERF_LOG_MEMORY |
|
475 |
|
476 // End of File |
|
477 |
|