]> git.proxmox.com Git - mirror_edk2.git/blob - PerformancePkg/Dp_App/Dp.c
Add NULL pointer check before free pool.
[mirror_edk2.git] / PerformancePkg / Dp_App / Dp.c
1 /** @file
2 * Shell application for Displaying Performance Metrics.
3 *
4 * The Dp application reads performance data and presents it in several
5 * different formats depending upon the needs of the user. Both
6 * Trace and Measured Profiling information is processed and presented.
7 *
8 * Dp uses the "PerformanceLib" to read the measurement records.
9 * The "TimerLib" provides information about the timer, such as frequency,
10 * beginning, and ending counter values.
11 * Measurement records contain identifying information (Handle, Token, Module)
12 * and start and end time values.
13 * Dp uses this information to group records in different ways. It also uses
14 * timer information to calculate elapsed time for each measurement.
15 *
16 * Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.<BR>
17 * This program and the accompanying materials
18 * are licensed and made available under the terms and conditions of the BSD License
19 * which accompanies this distribution. The full text of the license may be found at
20 * http://opensource.org/licenses/bsd-license.php
21 *
22 * THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
23 * WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
24 **/
25
26 #include <Library/UefiApplicationEntryPoint.h>
27 #include <Library/ShellLib.h>
28 #include <Library/BaseLib.h>
29 #include <Library/MemoryAllocationLib.h>
30 #include <Library/DebugLib.h>
31 #include <Library/TimerLib.h>
32 #include <Library/UefiLib.h>
33 #include <Library/HiiLib.h>
34 #include <Library/PcdLib.h>
35
36 #include <Guid/Performance.h>
37
38 #include <PerformanceTokens.h>
39 #include "Dp.h"
40 #include "Literals.h"
41 #include "DpInternal.h"
42
43 //
44 /// Module-Global Variables
45 ///@{
46 EFI_HII_HANDLE gHiiHandle;
47 SHELL_PARAM_ITEM *DpParamList = NULL;
48 CHAR16 *mPrintTokenBuffer = NULL;
49 CHAR16 mGaugeString[DXE_PERFORMANCE_STRING_SIZE];
50 CHAR16 mUnicodeToken[PERF_TOKEN_LENGTH + 1];
51 UINT64 mInterestThreshold;
52
53 PERF_SUMMARY_DATA SummaryData = { 0 }; ///< Create the SummaryData structure and init. to ZERO.
54
55 /// Timer Specific Information.
56 TIMER_INFO TimerInfo;
57
58 /// Items for which to gather cumulative statistics.
59 PERF_CUM_DATA CumData[] = {
60 PERF_INIT_CUM_DATA (LOAD_IMAGE_TOK),
61 PERF_INIT_CUM_DATA (START_IMAGE_TOK),
62 PERF_INIT_CUM_DATA (DRIVERBINDING_START_TOK),
63 PERF_INIT_CUM_DATA (DRIVERBINDING_SUPPORT_TOK)
64 };
65
66 /// Number of items for which we are gathering cumulative statistics.
67 UINT32 const NumCum = sizeof(CumData) / sizeof(PERF_CUM_DATA);
68
69 PARAM_ITEM_LIST ParamList[] = {
70 {STRING_TOKEN (STR_DP_OPTION_QH), TypeFlag}, // -? Help
71 {STRING_TOKEN (STR_DP_OPTION_LH), TypeFlag}, // -h Help
72 {STRING_TOKEN (STR_DP_OPTION_UH), TypeFlag}, // -H Help
73 {STRING_TOKEN (STR_DP_OPTION_LV), TypeFlag}, // -v Verbose Mode
74 {STRING_TOKEN (STR_DP_OPTION_UA), TypeFlag}, // -A All, Cooked
75 {STRING_TOKEN (STR_DP_OPTION_UR), TypeFlag}, // -R RAW All
76 {STRING_TOKEN (STR_DP_OPTION_LS), TypeFlag}, // -s Summary
77 #if PROFILING_IMPLEMENTED
78 {STRING_TOKEN (STR_DP_OPTION_UP), TypeFlag}, // -P Dump Profile Data
79 {STRING_TOKEN (STR_DP_OPTION_UT), TypeFlag}, // -T Dump Trace Data
80 #endif
81 {STRING_TOKEN (STR_DP_OPTION_LX), TypeFlag}, // -x eXclude Cumulative Items
82 {STRING_TOKEN (STR_DP_OPTION_LN), TypeValue}, // -n # Number of records to display for A and R
83 {STRING_TOKEN (STR_DP_OPTION_LT), TypeValue} // -t # Threshold of interest
84 };
85
86 ///@}
87
88 /**
89 Wrap original FreePool to check NULL pointer first.
90 **/
91 VOID
92 SafeFreePool (
93 IN VOID *Buffer
94 )
95 {
96 if (Buffer != NULL) {
97 FreePool (Buffer);
98 }
99 }
100
101 /**
102 Transfer the param list value and get the command line parse.
103
104 **/
105 VOID
106 InitialShellParamList( void )
107 {
108 UINT32 ListIndex;
109 UINT32 ListLength;
110
111 //
112 // Allocate one more for the end tag.
113 //
114 ListLength = sizeof (ParamList) / sizeof (ParamList[0]) + 1;
115 DpParamList = AllocatePool (sizeof (SHELL_PARAM_ITEM) * ListLength);
116 ASSERT (DpParamList != NULL);
117
118 for (ListIndex = 0; ListIndex < ListLength - 1; ListIndex ++)
119 {
120 DpParamList[ListIndex].Name = HiiGetString (gHiiHandle, ParamList[ListIndex].Token, NULL);
121 DpParamList[ListIndex].Type = ParamList[ListIndex].Type;
122 }
123 DpParamList[ListIndex].Name = NULL;
124 DpParamList[ListIndex].Type = TypeMax;
125 }
126
127 /**
128 Display Usage and Help information.
129 **/
130 VOID
131 ShowHelp( void )
132 {
133 PrintToken (STRING_TOKEN (STR_DP_HELP_HEAD));
134 #if PROFILING_IMPLEMENTED
135 PrintToken (STRING_TOKEN (STR_DP_HELP_FLAGS));
136 #else
137 PrintToken (STRING_TOKEN (STR_DP_HELP_FLAGS_2));
138 #endif // PROFILING_IMPLEMENTED
139 PrintToken (STRING_TOKEN (STR_DP_HELP_PAGINATE));
140 PrintToken (STRING_TOKEN (STR_DP_HELP_VERBOSE));
141 PrintToken (STRING_TOKEN (STR_DP_HELP_EXCLUDE));
142 PrintToken (STRING_TOKEN (STR_DP_HELP_STAT));
143 PrintToken (STRING_TOKEN (STR_DP_HELP_ALL));
144 PrintToken (STRING_TOKEN (STR_DP_HELP_RAW));
145 #if PROFILING_IMPLEMENTED
146 PrintToken (STRING_TOKEN (STR_DP_HELP_TRACE));
147 PrintToken (STRING_TOKEN (STR_DP_HELP_PROFILE));
148 #endif // PROFILING_IMPLEMENTED
149 PrintToken (STRING_TOKEN (STR_DP_HELP_THRESHOLD));
150 PrintToken (STRING_TOKEN (STR_DP_HELP_COUNT));
151 PrintToken (STRING_TOKEN (STR_DP_HELP_HELP));
152 Print(L"\n");
153 }
154
155 /**
156 Display the trailing Verbose information.
157 **/
158 VOID
159 DumpStatistics( void )
160 {
161 EFI_STRING StringPtr;
162 EFI_STRING StringPtrUnknown;
163 StringPtr = HiiGetString (gHiiHandle, STRING_TOKEN (STR_DP_SECTION_STATISTICS), NULL);
164 StringPtrUnknown = HiiGetString (gHiiHandle, STRING_TOKEN (STR_ALIT_UNKNOWN), NULL);
165 PrintToken( STRING_TOKEN (STR_DP_SECTION_HEADER),
166 (StringPtr == NULL) ? StringPtrUnknown : StringPtr);
167
168 PrintToken( STRING_TOKEN (STR_DP_STATS_NUMTRACE), SummaryData.NumTrace);
169 PrintToken( STRING_TOKEN (STR_DP_STATS_NUMINCOMPLETE), SummaryData.NumIncomplete);
170 PrintToken( STRING_TOKEN (STR_DP_STATS_NUMPHASES), SummaryData.NumSummary);
171 PrintToken( STRING_TOKEN (STR_DP_STATS_NUMHANDLES), SummaryData.NumHandles, SummaryData.NumTrace - SummaryData.NumHandles);
172 PrintToken( STRING_TOKEN (STR_DP_STATS_NUMPEIMS), SummaryData.NumPEIMs);
173 PrintToken( STRING_TOKEN (STR_DP_STATS_NUMGLOBALS), SummaryData.NumGlobal);
174 #if PROFILING_IMPLEMENTED
175 PrintToken( STRING_TOKEN (STR_DP_STATS_NUMPROFILE), SummaryData.NumProfile);
176 #endif // PROFILING_IMPLEMENTED
177 FreePool (StringPtr);
178 FreePool (StringPtrUnknown);
179 }
180
181 /**
182 Dump performance data.
183
184 @param[in] ImageHandle The image handle.
185 @param[in] SystemTable The system table.
186
187 @retval EFI_SUCCESS Command completed successfully.
188 @retval EFI_INVALID_PARAMETER Command usage error.
189 @retval value Unknown error.
190
191 **/
192 EFI_STATUS
193 EFIAPI
194 InitializeDp (
195 IN EFI_HANDLE ImageHandle,
196 IN EFI_SYSTEM_TABLE *SystemTable
197 )
198 {
199 UINT64 Freq;
200 UINT64 Ticker;
201 UINT32 ListIndex;
202
203 LIST_ENTRY *ParamPackage;
204 CONST CHAR16 *CmdLineArg;
205 EFI_STRING StringPtr;
206 UINTN Number2Display;
207
208 EFI_STATUS Status;
209 BOOLEAN SummaryMode;
210 BOOLEAN VerboseMode;
211 BOOLEAN AllMode;
212 BOOLEAN RawMode;
213 BOOLEAN TraceMode;
214 BOOLEAN ProfileMode;
215 BOOLEAN ExcludeMode;
216
217 EFI_STRING StringDpOptionQh;
218 EFI_STRING StringDpOptionLh;
219 EFI_STRING StringDpOptionUh;
220 EFI_STRING StringDpOptionLv;
221 EFI_STRING StringDpOptionUs;
222 EFI_STRING StringDpOptionLs;
223 EFI_STRING StringDpOptionUa;
224 EFI_STRING StringDpOptionUr;
225 EFI_STRING StringDpOptionUt;
226 EFI_STRING StringDpOptionUp;
227 EFI_STRING StringDpOptionLx;
228 EFI_STRING StringDpOptionLn;
229 EFI_STRING StringDpOptionLt;
230
231 SummaryMode = FALSE;
232 VerboseMode = FALSE;
233 AllMode = FALSE;
234 RawMode = FALSE;
235 TraceMode = FALSE;
236 ProfileMode = FALSE;
237 ExcludeMode = FALSE;
238
239 StringDpOptionQh = NULL;
240 StringDpOptionLh = NULL;
241 StringDpOptionUh = NULL;
242 StringDpOptionLv = NULL;
243 StringDpOptionUs = NULL;
244 StringDpOptionLs = NULL;
245 StringDpOptionUa = NULL;
246 StringDpOptionUr = NULL;
247 StringDpOptionUt = NULL;
248 StringDpOptionUp = NULL;
249 StringDpOptionLx = NULL;
250 StringDpOptionLn = NULL;
251 StringDpOptionLt = NULL;
252 StringPtr = NULL;
253
254 // Get DP's entry time as soon as possible.
255 // This is used as the Shell-Phase end time.
256 //
257 Ticker = GetPerformanceCounter ();
258
259 // Register our string package with HII and return the handle to it.
260 //
261 gHiiHandle = HiiAddPackages (&gEfiCallerIdGuid, ImageHandle, DPStrings, NULL);
262 ASSERT (gHiiHandle != NULL);
263
264 // Initial the command list
265 //
266 InitialShellParamList ();
267
268 /****************************************************************************
269 **** Process Command Line arguments ****
270 ****************************************************************************/
271 Status = ShellCommandLineParse (DpParamList, &ParamPackage, NULL, TRUE);
272
273 if (EFI_ERROR(Status)) {
274 PrintToken (STRING_TOKEN (STR_DP_INVALID_ARG));
275 ShowHelp();
276 }
277 else {
278 StringDpOptionQh = HiiGetString (gHiiHandle, STRING_TOKEN (STR_DP_OPTION_QH), NULL);
279 StringDpOptionLh = HiiGetString (gHiiHandle, STRING_TOKEN (STR_DP_OPTION_LH), NULL);
280 StringDpOptionUh = HiiGetString (gHiiHandle, STRING_TOKEN (STR_DP_OPTION_UH), NULL);
281
282 if (ShellCommandLineGetFlag (ParamPackage, StringDpOptionQh) ||
283 ShellCommandLineGetFlag (ParamPackage, StringDpOptionLh) ||
284 ShellCommandLineGetFlag (ParamPackage, StringDpOptionUh))
285 {
286 ShowHelp();
287 }
288 else {
289 StringDpOptionLv = HiiGetString (gHiiHandle, STRING_TOKEN (STR_DP_OPTION_LV), NULL);
290 StringDpOptionUs = HiiGetString (gHiiHandle, STRING_TOKEN (STR_DP_OPTION_US), NULL);
291 StringDpOptionLs = HiiGetString (gHiiHandle, STRING_TOKEN (STR_DP_OPTION_LS), NULL);
292 StringDpOptionUa = HiiGetString (gHiiHandle, STRING_TOKEN (STR_DP_OPTION_UA), NULL);
293 StringDpOptionUr = HiiGetString (gHiiHandle, STRING_TOKEN (STR_DP_OPTION_UR), NULL);
294 StringDpOptionUt = HiiGetString (gHiiHandle, STRING_TOKEN (STR_DP_OPTION_UT), NULL);
295 StringDpOptionUp = HiiGetString (gHiiHandle, STRING_TOKEN (STR_DP_OPTION_UP), NULL);
296 StringDpOptionLx = HiiGetString (gHiiHandle, STRING_TOKEN (STR_DP_OPTION_LX), NULL);
297 StringDpOptionLn = HiiGetString (gHiiHandle, STRING_TOKEN (STR_DP_OPTION_LN), NULL);
298 StringDpOptionLt = HiiGetString (gHiiHandle, STRING_TOKEN (STR_DP_OPTION_LT), NULL);
299
300 // Boolean Options
301 //
302 VerboseMode = ShellCommandLineGetFlag (ParamPackage, StringDpOptionLv);
303 SummaryMode = (BOOLEAN) (ShellCommandLineGetFlag (ParamPackage, StringDpOptionUs) ||
304 ShellCommandLineGetFlag (ParamPackage, StringDpOptionLs));
305 AllMode = ShellCommandLineGetFlag (ParamPackage, StringDpOptionUa);
306 RawMode = ShellCommandLineGetFlag (ParamPackage, StringDpOptionUr);
307 #if PROFILING_IMPLEMENTED
308 TraceMode = ShellCommandLineGetFlag (ParamPackage, StringDpOptionUt);
309 ProfileMode = ShellCommandLineGetFlag (ParamPackage, StringDpOptionUp);
310 #endif // PROFILING_IMPLEMENTED
311 ExcludeMode = ShellCommandLineGetFlag (ParamPackage, StringDpOptionLx);
312
313 // Options with Values
314 CmdLineArg = ShellCommandLineGetValue (ParamPackage, StringDpOptionLn);
315 if (CmdLineArg == NULL) {
316 Number2Display = DEFAULT_DISPLAYCOUNT;
317 }
318 else {
319 Number2Display = StrDecimalToUintn(CmdLineArg);
320 if (Number2Display == 0) {
321 Number2Display = MAXIMUM_DISPLAYCOUNT;
322 }
323 }
324 CmdLineArg = ShellCommandLineGetValue (ParamPackage, StringDpOptionLt);
325 if (CmdLineArg == NULL) {
326 mInterestThreshold = DEFAULT_THRESHOLD; // 1ms := 1,000 us
327 }
328 else {
329 mInterestThreshold = StrDecimalToUint64(CmdLineArg);
330 }
331 // Handle Flag combinations and default behaviors
332 // If both TraceMode and ProfileMode are FALSE, set them both to TRUE
333 if ((! TraceMode) && (! ProfileMode)) {
334 TraceMode = TRUE;
335 #if PROFILING_IMPLEMENTED
336 ProfileMode = TRUE;
337 #endif // PROFILING_IMPLEMENTED
338 }
339
340 /****************************************************************************
341 **** Timer specific processing ****
342 ****************************************************************************/
343 // Get the Performance counter characteristics:
344 // Freq = Frequency in Hz
345 // StartCount = Value loaded into the counter when it starts counting
346 // EndCount = Value counter counts to before it needs to be reset
347 //
348 Freq = GetPerformanceCounterProperties (&TimerInfo.StartCount, &TimerInfo.EndCount);
349
350 // Convert the Frequency from Hz to KHz
351 TimerInfo.Frequency = (UINT32)DivU64x32 (Freq, 1000);
352
353 // Determine in which direction the performance counter counts.
354 TimerInfo.CountUp = (BOOLEAN) (TimerInfo.EndCount >= TimerInfo.StartCount);
355
356 /****************************************************************************
357 **** Print heading ****
358 ****************************************************************************/
359 // print DP's build version
360 PrintToken (STRING_TOKEN (STR_DP_BUILD_REVISION), DP_MAJOR_VERSION, DP_MINOR_VERSION);
361
362 // print performance timer characteristics
363 PrintToken (STRING_TOKEN (STR_DP_KHZ), TimerInfo.Frequency); // Print Timer frequency in KHz
364
365 if ((VerboseMode) &&
366 (! RawMode)
367 ) {
368 StringPtr = HiiGetString (gHiiHandle,
369 (EFI_STRING_ID) (TimerInfo.CountUp ? STRING_TOKEN (STR_DP_UP) : STRING_TOKEN (STR_DP_DOWN)),
370 NULL);
371 ASSERT (StringPtr != NULL);
372 PrintToken (STRING_TOKEN (STR_DP_TIMER_PROPERTIES), // Print Timer count range and direction
373 StringPtr,
374 TimerInfo.StartCount,
375 TimerInfo.EndCount
376 );
377 PrintToken (STRING_TOKEN (STR_DP_VERBOSE_THRESHOLD), mInterestThreshold);
378 }
379
380 /* **************************************************************************
381 **** Print Sections based on command line options
382 ****
383 **** Option modes have the following priority:
384 **** v Verbose -- Valid in combination with any other options
385 **** t Threshold -- Modifies All, Raw, and Cooked output
386 **** Default is 0 for All and Raw mode
387 **** Default is DEFAULT_THRESHOLD for "Cooked" mode
388 **** n Number2Display Used by All and Raw mode. Otherwise ignored.
389 **** A All -- R and S options are ignored
390 **** R Raw -- S option is ignored
391 **** s Summary -- Modifies "Cooked" output only
392 **** Cooked (Default)
393 ****
394 **** The All, Raw, and Cooked modes are modified by the Trace and Profile
395 **** options.
396 **** !T && !P := (0) Default, Both are displayed
397 **** T && !P := (1) Only Trace records are displayed
398 **** !T && P := (2) Only Profile records are displayed
399 **** T && P := (3) Same as Default, both are displayed
400 ****************************************************************************/
401 GatherStatistics();
402 if (AllMode) {
403 if (TraceMode) {
404 DumpAllTrace( Number2Display, ExcludeMode);
405 }
406 if (ProfileMode) {
407 DumpAllProfile( Number2Display, ExcludeMode);
408 }
409 }
410 else if (RawMode) {
411 if (TraceMode) {
412 DumpRawTrace( Number2Display, ExcludeMode);
413 }
414 if (ProfileMode) {
415 DumpRawProfile( Number2Display, ExcludeMode);
416 }
417 }
418 else {
419 //------------- Begin Cooked Mode Processing
420 if (TraceMode) {
421 ProcessPhases ( Ticker );
422 if ( ! SummaryMode) {
423 Status = ProcessHandles ( ExcludeMode);
424 if ( ! EFI_ERROR( Status)) {
425 ProcessPeims ( );
426 ProcessGlobal ( );
427 ProcessCumulative ();
428 }
429 }
430 }
431 if (ProfileMode) {
432 DumpAllProfile( Number2Display, ExcludeMode);
433 }
434 } //------------- End of Cooked Mode Processing
435 if ( VerboseMode || SummaryMode) {
436 DumpStatistics();
437 }
438 }
439 }
440
441 // Free the memory allocate from HiiGetString
442 //
443 ListIndex = 0;
444 while (DpParamList[ListIndex].Name != NULL) {
445 FreePool (DpParamList[ListIndex].Name);
446 ListIndex ++;
447 }
448 FreePool (DpParamList);
449
450 SafeFreePool (StringDpOptionQh);
451 SafeFreePool (StringDpOptionLh);
452 SafeFreePool (StringDpOptionUh);
453 SafeFreePool (StringDpOptionLv);
454 SafeFreePool (StringDpOptionUs);
455 SafeFreePool (StringDpOptionLs);
456 SafeFreePool (StringDpOptionUa);
457 SafeFreePool (StringDpOptionUr);
458 SafeFreePool (StringDpOptionUt);
459 SafeFreePool (StringDpOptionUp);
460 SafeFreePool (StringDpOptionLx);
461 SafeFreePool (StringDpOptionLn);
462 SafeFreePool (StringDpOptionLt);
463 SafeFreePool (StringPtr);
464 SafeFreePool (mPrintTokenBuffer);
465
466 HiiRemovePackages (gHiiHandle);
467 return Status;
468 }