2 Shell application for Displaying Performance Metrics.
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.
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.
16 Copyright (c) 2009 - 2016, Intel Corporation. All rights reserved.<BR>
17 (C) Copyright 2015-2016 Hewlett Packard Enterprise Development LP<BR>
18 This program and the accompanying materials
19 are licensed and made available under the terms and conditions of the BSD License
20 which accompanies this distribution. The full text of the license may be found at
21 http://opensource.org/licenses/bsd-license.php
23 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
24 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
27 #include <Library/UefiApplicationEntryPoint.h>
28 #include <Library/UefiBootServicesTableLib.h>
29 #include <Library/ShellLib.h>
30 #include <Library/BaseLib.h>
31 #include <Library/MemoryAllocationLib.h>
32 #include <Library/DebugLib.h>
33 #include <Library/TimerLib.h>
34 #include <Library/UefiLib.h>
35 #include <Library/UefiHiiServicesLib.h>
36 #include <Library/HiiLib.h>
37 #include <Library/PcdLib.h>
39 #include <Guid/Performance.h>
41 #include <PerformanceTokens.h>
44 #include "DpInternal.h"
47 // String token ID of help message text.
48 // Shell supports to find help message in the resource section of an application image if
49 // .MAN file is not found. This global variable is added to make build tool recognizes
50 // that the help string is consumed by user and then build tool will add the string into
51 // the resource section. Thus the application can use '-?' option to show help message in
54 GLOBAL_REMOVE_IF_UNREFERENCED EFI_STRING_ID mDpStrEngHelpTokenId
= STRING_TOKEN (STR_DP_HELP_INFORMATION
);
57 /// Module-Global Variables
59 EFI_HII_HANDLE gHiiHandle
;
60 SHELL_PARAM_ITEM
*DpParamList
= NULL
;
61 CHAR16
*mPrintTokenBuffer
= NULL
;
62 CHAR16 mGaugeString
[DP_GAUGE_STRING_LENGTH
+ 1];
63 CHAR16 mUnicodeToken
[DXE_PERFORMANCE_STRING_SIZE
];
64 UINT64 mInterestThreshold
;
65 BOOLEAN mShowId
= FALSE
;
67 PERF_SUMMARY_DATA SummaryData
= { 0 }; ///< Create the SummaryData structure and init. to ZERO.
69 /// Timer Specific Information.
72 /// Items for which to gather cumulative statistics.
73 PERF_CUM_DATA CumData
[] = {
74 PERF_INIT_CUM_DATA (LOAD_IMAGE_TOK
),
75 PERF_INIT_CUM_DATA (START_IMAGE_TOK
),
76 PERF_INIT_CUM_DATA (DRIVERBINDING_START_TOK
),
77 PERF_INIT_CUM_DATA (DRIVERBINDING_SUPPORT_TOK
)
80 /// Number of items for which we are gathering cumulative statistics.
81 UINT32
const NumCum
= sizeof(CumData
) / sizeof(PERF_CUM_DATA
);
83 PARAM_ITEM_LIST ParamList
[] = {
84 {STRING_TOKEN (STR_DP_OPTION_QH
), TypeFlag
}, // -? Help
85 {STRING_TOKEN (STR_DP_OPTION_LH
), TypeFlag
}, // -h Help
86 {STRING_TOKEN (STR_DP_OPTION_UH
), TypeFlag
}, // -H Help
87 {STRING_TOKEN (STR_DP_OPTION_LV
), TypeFlag
}, // -v Verbose Mode
88 {STRING_TOKEN (STR_DP_OPTION_UA
), TypeFlag
}, // -A All, Cooked
89 {STRING_TOKEN (STR_DP_OPTION_UR
), TypeFlag
}, // -R RAW All
90 {STRING_TOKEN (STR_DP_OPTION_LS
), TypeFlag
}, // -s Summary
91 #if PROFILING_IMPLEMENTED
92 {STRING_TOKEN (STR_DP_OPTION_UP
), TypeFlag
}, // -P Dump Profile Data
93 {STRING_TOKEN (STR_DP_OPTION_UT
), TypeFlag
}, // -T Dump Trace Data
95 {STRING_TOKEN (STR_DP_OPTION_LX
), TypeFlag
}, // -x eXclude Cumulative Items
96 {STRING_TOKEN (STR_DP_OPTION_LI
), TypeFlag
}, // -i Display Identifier
97 {STRING_TOKEN (STR_DP_OPTION_LC
), TypeValue
}, // -c Display cumulative data.
98 {STRING_TOKEN (STR_DP_OPTION_LN
), TypeValue
}, // -n # Number of records to display for A and R
99 {STRING_TOKEN (STR_DP_OPTION_LT
), TypeValue
} // -t # Threshold of interest
105 Transfer the param list value and get the command line parse.
109 InitialShellParamList( void )
115 // Allocate one more for the end tag.
117 ListLength
= sizeof (ParamList
) / sizeof (ParamList
[0]) + 1;
118 DpParamList
= AllocatePool (sizeof (SHELL_PARAM_ITEM
) * ListLength
);
119 ASSERT (DpParamList
!= NULL
);
121 for (ListIndex
= 0; ListIndex
< ListLength
- 1; ListIndex
++)
123 DpParamList
[ListIndex
].Name
= HiiGetString (gHiiHandle
, ParamList
[ListIndex
].Token
, NULL
);
124 DpParamList
[ListIndex
].Type
= ParamList
[ListIndex
].Type
;
126 DpParamList
[ListIndex
].Name
= NULL
;
127 DpParamList
[ListIndex
].Type
= TypeMax
;
131 Display Usage and Help information.
136 PrintToken (STRING_TOKEN (STR_DP_HELP_HEAD
));
137 #if PROFILING_IMPLEMENTED
138 PrintToken (STRING_TOKEN (STR_DP_HELP_FLAGS
));
140 PrintToken (STRING_TOKEN (STR_DP_HELP_FLAGS_2
));
141 #endif // PROFILING_IMPLEMENTED
142 PrintToken (STRING_TOKEN (STR_DP_HELP_PAGINATE
));
143 PrintToken (STRING_TOKEN (STR_DP_HELP_VERBOSE
));
144 PrintToken (STRING_TOKEN (STR_DP_HELP_EXCLUDE
));
145 PrintToken (STRING_TOKEN (STR_DP_HELP_STAT
));
146 PrintToken (STRING_TOKEN (STR_DP_HELP_ALL
));
147 PrintToken (STRING_TOKEN (STR_DP_HELP_RAW
));
148 #if PROFILING_IMPLEMENTED
149 PrintToken (STRING_TOKEN (STR_DP_HELP_TRACE
));
150 PrintToken (STRING_TOKEN (STR_DP_HELP_PROFILE
));
151 #endif // PROFILING_IMPLEMENTED
152 PrintToken (STRING_TOKEN (STR_DP_HELP_THRESHOLD
));
153 PrintToken (STRING_TOKEN (STR_DP_HELP_COUNT
));
154 PrintToken (STRING_TOKEN (STR_DP_HELP_ID
));
155 PrintToken (STRING_TOKEN (STR_DP_HELP_CUM_DATA
));
156 PrintToken (STRING_TOKEN (STR_DP_HELP_HELP
));
161 Display the trailing Verbose information.
164 DumpStatistics( void )
166 EFI_STRING StringPtr
;
167 EFI_STRING StringPtrUnknown
;
168 StringPtr
= HiiGetString (gHiiHandle
, STRING_TOKEN (STR_DP_SECTION_STATISTICS
), NULL
);
169 StringPtrUnknown
= HiiGetString (gHiiHandle
, STRING_TOKEN (STR_ALIT_UNKNOWN
), NULL
);
170 PrintToken( STRING_TOKEN (STR_DP_SECTION_HEADER
),
171 (StringPtr
== NULL
) ? StringPtrUnknown
: StringPtr
);
173 PrintToken( STRING_TOKEN (STR_DP_STATS_NUMTRACE
), SummaryData
.NumTrace
);
174 PrintToken( STRING_TOKEN (STR_DP_STATS_NUMINCOMPLETE
), SummaryData
.NumIncomplete
);
175 PrintToken( STRING_TOKEN (STR_DP_STATS_NUMPHASES
), SummaryData
.NumSummary
);
176 PrintToken( STRING_TOKEN (STR_DP_STATS_NUMHANDLES
), SummaryData
.NumHandles
, SummaryData
.NumTrace
- SummaryData
.NumHandles
);
177 PrintToken( STRING_TOKEN (STR_DP_STATS_NUMPEIMS
), SummaryData
.NumPEIMs
);
178 PrintToken( STRING_TOKEN (STR_DP_STATS_NUMGLOBALS
), SummaryData
.NumGlobal
);
179 #if PROFILING_IMPLEMENTED
180 PrintToken( STRING_TOKEN (STR_DP_STATS_NUMPROFILE
), SummaryData
.NumProfile
);
181 #endif // PROFILING_IMPLEMENTED
182 FreePool (StringPtr
);
183 FreePool (StringPtrUnknown
);
187 Dump performance data.
189 @param[in] ImageHandle The image handle.
190 @param[in] SystemTable The system table.
192 @retval EFI_SUCCESS Command completed successfully.
193 @retval EFI_INVALID_PARAMETER Command usage error.
194 @retval EFI_ABORTED The user aborts the operation.
195 @retval value Unknown error.
200 IN EFI_HANDLE ImageHandle
,
201 IN EFI_SYSTEM_TABLE
*SystemTable
208 LIST_ENTRY
*ParamPackage
;
209 CONST CHAR16
*CmdLineArg
;
210 EFI_STRING StringPtr
;
211 UINTN Number2Display
;
221 BOOLEAN CumulativeMode
;
222 CONST CHAR16
*CustomCumulativeToken
;
223 PERF_CUM_DATA
*CustomCumulativeData
;
224 EFI_HII_PACKAGE_LIST_HEADER
*PackageList
;
226 EFI_STRING StringDpOptionQh
;
227 EFI_STRING StringDpOptionLh
;
228 EFI_STRING StringDpOptionUh
;
229 EFI_STRING StringDpOptionLv
;
230 EFI_STRING StringDpOptionUs
;
231 EFI_STRING StringDpOptionLs
;
232 EFI_STRING StringDpOptionUa
;
233 EFI_STRING StringDpOptionUr
;
234 EFI_STRING StringDpOptionUt
;
235 EFI_STRING StringDpOptionUp
;
236 EFI_STRING StringDpOptionLx
;
237 EFI_STRING StringDpOptionLn
;
238 EFI_STRING StringDpOptionLt
;
239 EFI_STRING StringDpOptionLi
;
240 EFI_STRING StringDpOptionLc
;
249 CumulativeMode
= FALSE
;
250 CustomCumulativeData
= NULL
;
252 StringDpOptionQh
= NULL
;
253 StringDpOptionLh
= NULL
;
254 StringDpOptionUh
= NULL
;
255 StringDpOptionLv
= NULL
;
256 StringDpOptionUs
= NULL
;
257 StringDpOptionLs
= NULL
;
258 StringDpOptionUa
= NULL
;
259 StringDpOptionUr
= NULL
;
260 StringDpOptionUt
= NULL
;
261 StringDpOptionUp
= NULL
;
262 StringDpOptionLx
= NULL
;
263 StringDpOptionLn
= NULL
;
264 StringDpOptionLt
= NULL
;
265 StringDpOptionLi
= NULL
;
266 StringDpOptionLc
= NULL
;
269 // Get DP's entry time as soon as possible.
270 // This is used as the Shell-Phase end time.
272 Ticker
= GetPerformanceCounter ();
275 // Retrieve HII package list from ImageHandle
277 Status
= gBS
->OpenProtocol (
279 &gEfiHiiPackageListProtocolGuid
,
280 (VOID
**) &PackageList
,
283 EFI_OPEN_PROTOCOL_GET_PROTOCOL
285 if (EFI_ERROR (Status
)) {
290 // Publish HII package list to HII Database.
292 Status
= gHiiDatabase
->NewPackageList (
298 if (EFI_ERROR (Status
)) {
301 ASSERT (gHiiHandle
!= NULL
);
304 // Initial the command list
306 InitialShellParamList ();
308 /****************************************************************************
309 **** Process Command Line arguments ****
310 ****************************************************************************/
311 Status
= ShellCommandLineParse (DpParamList
, &ParamPackage
, NULL
, TRUE
);
313 if (EFI_ERROR(Status
)) {
314 PrintToken (STRING_TOKEN (STR_DP_INVALID_ARG
));
318 StringDpOptionQh
= HiiGetString (gHiiHandle
, STRING_TOKEN (STR_DP_OPTION_QH
), NULL
);
319 StringDpOptionLh
= HiiGetString (gHiiHandle
, STRING_TOKEN (STR_DP_OPTION_LH
), NULL
);
320 StringDpOptionUh
= HiiGetString (gHiiHandle
, STRING_TOKEN (STR_DP_OPTION_UH
), NULL
);
322 if (ShellCommandLineGetFlag (ParamPackage
, StringDpOptionQh
) ||
323 ShellCommandLineGetFlag (ParamPackage
, StringDpOptionLh
) ||
324 ShellCommandLineGetFlag (ParamPackage
, StringDpOptionUh
))
329 StringDpOptionLv
= HiiGetString (gHiiHandle
, STRING_TOKEN (STR_DP_OPTION_LV
), NULL
);
330 StringDpOptionUs
= HiiGetString (gHiiHandle
, STRING_TOKEN (STR_DP_OPTION_US
), NULL
);
331 StringDpOptionLs
= HiiGetString (gHiiHandle
, STRING_TOKEN (STR_DP_OPTION_LS
), NULL
);
332 StringDpOptionUa
= HiiGetString (gHiiHandle
, STRING_TOKEN (STR_DP_OPTION_UA
), NULL
);
333 StringDpOptionUr
= HiiGetString (gHiiHandle
, STRING_TOKEN (STR_DP_OPTION_UR
), NULL
);
334 StringDpOptionUt
= HiiGetString (gHiiHandle
, STRING_TOKEN (STR_DP_OPTION_UT
), NULL
);
335 StringDpOptionUp
= HiiGetString (gHiiHandle
, STRING_TOKEN (STR_DP_OPTION_UP
), NULL
);
336 StringDpOptionLx
= HiiGetString (gHiiHandle
, STRING_TOKEN (STR_DP_OPTION_LX
), NULL
);
337 StringDpOptionLn
= HiiGetString (gHiiHandle
, STRING_TOKEN (STR_DP_OPTION_LN
), NULL
);
338 StringDpOptionLt
= HiiGetString (gHiiHandle
, STRING_TOKEN (STR_DP_OPTION_LT
), NULL
);
339 StringDpOptionLi
= HiiGetString (gHiiHandle
, STRING_TOKEN (STR_DP_OPTION_LI
), NULL
);
340 StringDpOptionLc
= HiiGetString (gHiiHandle
, STRING_TOKEN (STR_DP_OPTION_LC
), NULL
);
344 VerboseMode
= ShellCommandLineGetFlag (ParamPackage
, StringDpOptionLv
);
345 SummaryMode
= (BOOLEAN
) (ShellCommandLineGetFlag (ParamPackage
, StringDpOptionUs
) ||
346 ShellCommandLineGetFlag (ParamPackage
, StringDpOptionLs
));
347 AllMode
= ShellCommandLineGetFlag (ParamPackage
, StringDpOptionUa
);
348 RawMode
= ShellCommandLineGetFlag (ParamPackage
, StringDpOptionUr
);
349 #if PROFILING_IMPLEMENTED
350 TraceMode
= ShellCommandLineGetFlag (ParamPackage
, StringDpOptionUt
);
351 ProfileMode
= ShellCommandLineGetFlag (ParamPackage
, StringDpOptionUp
);
352 #endif // PROFILING_IMPLEMENTED
353 ExcludeMode
= ShellCommandLineGetFlag (ParamPackage
, StringDpOptionLx
);
354 mShowId
= ShellCommandLineGetFlag (ParamPackage
, StringDpOptionLi
);
355 CumulativeMode
= ShellCommandLineGetFlag (ParamPackage
, StringDpOptionLc
);
357 // Options with Values
358 CmdLineArg
= ShellCommandLineGetValue (ParamPackage
, StringDpOptionLn
);
359 if (CmdLineArg
== NULL
) {
360 Number2Display
= DEFAULT_DISPLAYCOUNT
;
363 Number2Display
= StrDecimalToUintn(CmdLineArg
);
364 if (Number2Display
== 0) {
365 Number2Display
= MAXIMUM_DISPLAYCOUNT
;
368 CmdLineArg
= ShellCommandLineGetValue (ParamPackage
, StringDpOptionLt
);
369 if (CmdLineArg
== NULL
) {
370 mInterestThreshold
= DEFAULT_THRESHOLD
; // 1ms := 1,000 us
373 mInterestThreshold
= StrDecimalToUint64(CmdLineArg
);
375 // Handle Flag combinations and default behaviors
376 // If both TraceMode and ProfileMode are FALSE, set them both to TRUE
377 if ((! TraceMode
) && (! ProfileMode
)) {
379 #if PROFILING_IMPLEMENTED
381 #endif // PROFILING_IMPLEMENTED
385 // Init the custom cumulative data.
387 CustomCumulativeToken
= ShellCommandLineGetValue (ParamPackage
, StringDpOptionLc
);
388 if (CustomCumulativeToken
!= NULL
) {
389 CustomCumulativeData
= AllocateZeroPool (sizeof (PERF_CUM_DATA
));
390 ASSERT (CustomCumulativeData
!= NULL
);
391 CustomCumulativeData
->MinDur
= 0;
392 CustomCumulativeData
->MaxDur
= 0;
393 CustomCumulativeData
->Count
= 0;
394 CustomCumulativeData
->Duration
= 0;
395 CustomCumulativeData
->Name
= AllocateZeroPool (StrLen (CustomCumulativeToken
) + 1);
396 UnicodeStrToAsciiStr (CustomCumulativeToken
, CustomCumulativeData
->Name
);
399 /****************************************************************************
400 **** Timer specific processing ****
401 ****************************************************************************/
402 // Get the Performance counter characteristics:
403 // Freq = Frequency in Hz
404 // StartCount = Value loaded into the counter when it starts counting
405 // EndCount = Value counter counts to before it needs to be reset
407 Freq
= GetPerformanceCounterProperties (&TimerInfo
.StartCount
, &TimerInfo
.EndCount
);
409 // Convert the Frequency from Hz to KHz
410 TimerInfo
.Frequency
= (UINT32
)DivU64x32 (Freq
, 1000);
412 // Determine in which direction the performance counter counts.
413 TimerInfo
.CountUp
= (BOOLEAN
) (TimerInfo
.EndCount
>= TimerInfo
.StartCount
);
415 /****************************************************************************
416 **** Print heading ****
417 ****************************************************************************/
418 // print DP's build version
419 PrintToken (STRING_TOKEN (STR_DP_BUILD_REVISION
), DP_MAJOR_VERSION
, DP_MINOR_VERSION
);
421 // print performance timer characteristics
422 PrintToken (STRING_TOKEN (STR_DP_KHZ
), TimerInfo
.Frequency
); // Print Timer frequency in KHz
427 StringPtr
= HiiGetString (gHiiHandle
,
428 (EFI_STRING_ID
) (TimerInfo
.CountUp
? STRING_TOKEN (STR_DP_UP
) : STRING_TOKEN (STR_DP_DOWN
)),
430 ASSERT (StringPtr
!= NULL
);
431 PrintToken (STRING_TOKEN (STR_DP_TIMER_PROPERTIES
), // Print Timer count range and direction
433 TimerInfo
.StartCount
,
436 PrintToken (STRING_TOKEN (STR_DP_VERBOSE_THRESHOLD
), mInterestThreshold
);
439 /* **************************************************************************
440 **** Print Sections based on command line options
442 **** Option modes have the following priority:
443 **** v Verbose -- Valid in combination with any other options
444 **** t Threshold -- Modifies All, Raw, and Cooked output
445 **** Default is 0 for All and Raw mode
446 **** Default is DEFAULT_THRESHOLD for "Cooked" mode
447 **** n Number2Display Used by All and Raw mode. Otherwise ignored.
448 **** A All -- R and S options are ignored
449 **** R Raw -- S option is ignored
450 **** s Summary -- Modifies "Cooked" output only
451 **** Cooked (Default)
453 **** The All, Raw, and Cooked modes are modified by the Trace and Profile
455 **** !T && !P := (0) Default, Both are displayed
456 **** T && !P := (1) Only Trace records are displayed
457 **** !T && P := (2) Only Profile records are displayed
458 **** T && P := (3) Same as Default, both are displayed
459 ****************************************************************************/
460 GatherStatistics (CustomCumulativeData
);
461 if (CumulativeMode
) {
462 ProcessCumulative (CustomCumulativeData
);
463 } else if (AllMode
) {
465 Status
= DumpAllTrace( Number2Display
, ExcludeMode
);
466 if (Status
== EFI_ABORTED
) {
471 DumpAllProfile( Number2Display
, ExcludeMode
);
476 Status
= DumpRawTrace( Number2Display
, ExcludeMode
);
477 if (Status
== EFI_ABORTED
) {
482 DumpRawProfile( Number2Display
, ExcludeMode
);
486 //------------- Begin Cooked Mode Processing
488 ProcessPhases ( Ticker
);
489 if ( ! SummaryMode
) {
490 Status
= ProcessHandles ( ExcludeMode
);
491 if (Status
== EFI_ABORTED
) {
495 Status
= ProcessPeims ();
496 if (Status
== EFI_ABORTED
) {
500 Status
= ProcessGlobal ();
501 if (Status
== EFI_ABORTED
) {
505 ProcessCumulative (NULL
);
509 DumpAllProfile( Number2Display
, ExcludeMode
);
511 } //------------- End of Cooked Mode Processing
512 if ( VerboseMode
|| SummaryMode
) {
521 // Free the memory allocate from HiiGetString
524 while (DpParamList
[ListIndex
].Name
!= NULL
) {
525 FreePool (DpParamList
[ListIndex
].Name
);
528 FreePool (DpParamList
);
530 SafeFreePool (StringDpOptionQh
);
531 SafeFreePool (StringDpOptionLh
);
532 SafeFreePool (StringDpOptionUh
);
533 SafeFreePool (StringDpOptionLv
);
534 SafeFreePool (StringDpOptionUs
);
535 SafeFreePool (StringDpOptionLs
);
536 SafeFreePool (StringDpOptionUa
);
537 SafeFreePool (StringDpOptionUr
);
538 SafeFreePool (StringDpOptionUt
);
539 SafeFreePool (StringDpOptionUp
);
540 SafeFreePool (StringDpOptionLx
);
541 SafeFreePool (StringDpOptionLn
);
542 SafeFreePool (StringDpOptionLt
);
543 SafeFreePool (StringDpOptionLi
);
544 SafeFreePool (StringDpOptionLc
);
545 SafeFreePool (StringPtr
);
546 SafeFreePool (mPrintTokenBuffer
);
548 if (ParamPackage
!= NULL
) {
549 ShellCommandLineFreeVarList (ParamPackage
);
551 if (CustomCumulativeData
!= NULL
) {
552 SafeFreePool (CustomCumulativeData
->Name
);
554 SafeFreePool (CustomCumulativeData
);
556 HiiRemovePackages (gHiiHandle
);