]> git.proxmox.com Git - mirror_edk2.git/blob - PerformancePkg/Dp_App/Dp.c
New Shell binaries based on EDKII core r12898 and Shell r57.
[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[DP_GAUGE_STRING_LENGTH + 1];
50 CHAR16 mUnicodeToken[DXE_PERFORMANCE_STRING_SIZE];
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 Transfer the param list value and get the command line parse.
90
91 **/
92 VOID
93 InitialShellParamList( void )
94 {
95 UINT32 ListIndex;
96 UINT32 ListLength;
97
98 //
99 // Allocate one more for the end tag.
100 //
101 ListLength = sizeof (ParamList) / sizeof (ParamList[0]) + 1;
102 DpParamList = AllocatePool (sizeof (SHELL_PARAM_ITEM) * ListLength);
103 ASSERT (DpParamList != NULL);
104
105 for (ListIndex = 0; ListIndex < ListLength - 1; ListIndex ++)
106 {
107 DpParamList[ListIndex].Name = HiiGetString (gHiiHandle, ParamList[ListIndex].Token, NULL);
108 DpParamList[ListIndex].Type = ParamList[ListIndex].Type;
109 }
110 DpParamList[ListIndex].Name = NULL;
111 DpParamList[ListIndex].Type = TypeMax;
112 }
113
114 /**
115 Display Usage and Help information.
116 **/
117 VOID
118 ShowHelp( void )
119 {
120 PrintToken (STRING_TOKEN (STR_DP_HELP_HEAD));
121 #if PROFILING_IMPLEMENTED
122 PrintToken (STRING_TOKEN (STR_DP_HELP_FLAGS));
123 #else
124 PrintToken (STRING_TOKEN (STR_DP_HELP_FLAGS_2));
125 #endif // PROFILING_IMPLEMENTED
126 PrintToken (STRING_TOKEN (STR_DP_HELP_PAGINATE));
127 PrintToken (STRING_TOKEN (STR_DP_HELP_VERBOSE));
128 PrintToken (STRING_TOKEN (STR_DP_HELP_EXCLUDE));
129 PrintToken (STRING_TOKEN (STR_DP_HELP_STAT));
130 PrintToken (STRING_TOKEN (STR_DP_HELP_ALL));
131 PrintToken (STRING_TOKEN (STR_DP_HELP_RAW));
132 #if PROFILING_IMPLEMENTED
133 PrintToken (STRING_TOKEN (STR_DP_HELP_TRACE));
134 PrintToken (STRING_TOKEN (STR_DP_HELP_PROFILE));
135 #endif // PROFILING_IMPLEMENTED
136 PrintToken (STRING_TOKEN (STR_DP_HELP_THRESHOLD));
137 PrintToken (STRING_TOKEN (STR_DP_HELP_COUNT));
138 PrintToken (STRING_TOKEN (STR_DP_HELP_HELP));
139 Print(L"\n");
140 }
141
142 /**
143 Display the trailing Verbose information.
144 **/
145 VOID
146 DumpStatistics( void )
147 {
148 EFI_STRING StringPtr;
149 EFI_STRING StringPtrUnknown;
150 StringPtr = HiiGetString (gHiiHandle, STRING_TOKEN (STR_DP_SECTION_STATISTICS), NULL);
151 StringPtrUnknown = HiiGetString (gHiiHandle, STRING_TOKEN (STR_ALIT_UNKNOWN), NULL);
152 PrintToken( STRING_TOKEN (STR_DP_SECTION_HEADER),
153 (StringPtr == NULL) ? StringPtrUnknown : StringPtr);
154
155 PrintToken( STRING_TOKEN (STR_DP_STATS_NUMTRACE), SummaryData.NumTrace);
156 PrintToken( STRING_TOKEN (STR_DP_STATS_NUMINCOMPLETE), SummaryData.NumIncomplete);
157 PrintToken( STRING_TOKEN (STR_DP_STATS_NUMPHASES), SummaryData.NumSummary);
158 PrintToken( STRING_TOKEN (STR_DP_STATS_NUMHANDLES), SummaryData.NumHandles, SummaryData.NumTrace - SummaryData.NumHandles);
159 PrintToken( STRING_TOKEN (STR_DP_STATS_NUMPEIMS), SummaryData.NumPEIMs);
160 PrintToken( STRING_TOKEN (STR_DP_STATS_NUMGLOBALS), SummaryData.NumGlobal);
161 #if PROFILING_IMPLEMENTED
162 PrintToken( STRING_TOKEN (STR_DP_STATS_NUMPROFILE), SummaryData.NumProfile);
163 #endif // PROFILING_IMPLEMENTED
164 FreePool (StringPtr);
165 FreePool (StringPtrUnknown);
166 }
167
168 /**
169 Dump performance data.
170
171 @param[in] ImageHandle The image handle.
172 @param[in] SystemTable The system table.
173
174 @retval EFI_SUCCESS Command completed successfully.
175 @retval EFI_INVALID_PARAMETER Command usage error.
176 @retval value Unknown error.
177
178 **/
179 EFI_STATUS
180 EFIAPI
181 InitializeDp (
182 IN EFI_HANDLE ImageHandle,
183 IN EFI_SYSTEM_TABLE *SystemTable
184 )
185 {
186 UINT64 Freq;
187 UINT64 Ticker;
188 UINT32 ListIndex;
189
190 LIST_ENTRY *ParamPackage;
191 CONST CHAR16 *CmdLineArg;
192 EFI_STRING StringPtr;
193 UINTN Number2Display;
194
195 EFI_STATUS Status;
196 BOOLEAN SummaryMode;
197 BOOLEAN VerboseMode;
198 BOOLEAN AllMode;
199 BOOLEAN RawMode;
200 BOOLEAN TraceMode;
201 BOOLEAN ProfileMode;
202 BOOLEAN ExcludeMode;
203
204 EFI_STRING StringDpOptionQh;
205 EFI_STRING StringDpOptionLh;
206 EFI_STRING StringDpOptionUh;
207 EFI_STRING StringDpOptionLv;
208 EFI_STRING StringDpOptionUs;
209 EFI_STRING StringDpOptionLs;
210 EFI_STRING StringDpOptionUa;
211 EFI_STRING StringDpOptionUr;
212 EFI_STRING StringDpOptionUt;
213 EFI_STRING StringDpOptionUp;
214 EFI_STRING StringDpOptionLx;
215 EFI_STRING StringDpOptionLn;
216 EFI_STRING StringDpOptionLt;
217
218 SummaryMode = FALSE;
219 VerboseMode = FALSE;
220 AllMode = FALSE;
221 RawMode = FALSE;
222 TraceMode = FALSE;
223 ProfileMode = FALSE;
224 ExcludeMode = FALSE;
225
226 StringDpOptionQh = NULL;
227 StringDpOptionLh = NULL;
228 StringDpOptionUh = NULL;
229 StringDpOptionLv = NULL;
230 StringDpOptionUs = NULL;
231 StringDpOptionLs = NULL;
232 StringDpOptionUa = NULL;
233 StringDpOptionUr = NULL;
234 StringDpOptionUt = NULL;
235 StringDpOptionUp = NULL;
236 StringDpOptionLx = NULL;
237 StringDpOptionLn = NULL;
238 StringDpOptionLt = NULL;
239 StringPtr = NULL;
240
241 // Get DP's entry time as soon as possible.
242 // This is used as the Shell-Phase end time.
243 //
244 Ticker = GetPerformanceCounter ();
245
246 // Register our string package with HII and return the handle to it.
247 //
248 gHiiHandle = HiiAddPackages (&gEfiCallerIdGuid, ImageHandle, DPStrings, NULL);
249 ASSERT (gHiiHandle != NULL);
250
251 // Initial the command list
252 //
253 InitialShellParamList ();
254
255 /****************************************************************************
256 **** Process Command Line arguments ****
257 ****************************************************************************/
258 Status = ShellCommandLineParse (DpParamList, &ParamPackage, NULL, TRUE);
259
260 if (EFI_ERROR(Status)) {
261 PrintToken (STRING_TOKEN (STR_DP_INVALID_ARG));
262 ShowHelp();
263 }
264 else {
265 StringDpOptionQh = HiiGetString (gHiiHandle, STRING_TOKEN (STR_DP_OPTION_QH), NULL);
266 StringDpOptionLh = HiiGetString (gHiiHandle, STRING_TOKEN (STR_DP_OPTION_LH), NULL);
267 StringDpOptionUh = HiiGetString (gHiiHandle, STRING_TOKEN (STR_DP_OPTION_UH), NULL);
268
269 if (ShellCommandLineGetFlag (ParamPackage, StringDpOptionQh) ||
270 ShellCommandLineGetFlag (ParamPackage, StringDpOptionLh) ||
271 ShellCommandLineGetFlag (ParamPackage, StringDpOptionUh))
272 {
273 ShowHelp();
274 }
275 else {
276 StringDpOptionLv = HiiGetString (gHiiHandle, STRING_TOKEN (STR_DP_OPTION_LV), NULL);
277 StringDpOptionUs = HiiGetString (gHiiHandle, STRING_TOKEN (STR_DP_OPTION_US), NULL);
278 StringDpOptionLs = HiiGetString (gHiiHandle, STRING_TOKEN (STR_DP_OPTION_LS), NULL);
279 StringDpOptionUa = HiiGetString (gHiiHandle, STRING_TOKEN (STR_DP_OPTION_UA), NULL);
280 StringDpOptionUr = HiiGetString (gHiiHandle, STRING_TOKEN (STR_DP_OPTION_UR), NULL);
281 StringDpOptionUt = HiiGetString (gHiiHandle, STRING_TOKEN (STR_DP_OPTION_UT), NULL);
282 StringDpOptionUp = HiiGetString (gHiiHandle, STRING_TOKEN (STR_DP_OPTION_UP), NULL);
283 StringDpOptionLx = HiiGetString (gHiiHandle, STRING_TOKEN (STR_DP_OPTION_LX), NULL);
284 StringDpOptionLn = HiiGetString (gHiiHandle, STRING_TOKEN (STR_DP_OPTION_LN), NULL);
285 StringDpOptionLt = HiiGetString (gHiiHandle, STRING_TOKEN (STR_DP_OPTION_LT), NULL);
286
287 // Boolean Options
288 //
289 VerboseMode = ShellCommandLineGetFlag (ParamPackage, StringDpOptionLv);
290 SummaryMode = (BOOLEAN) (ShellCommandLineGetFlag (ParamPackage, StringDpOptionUs) ||
291 ShellCommandLineGetFlag (ParamPackage, StringDpOptionLs));
292 AllMode = ShellCommandLineGetFlag (ParamPackage, StringDpOptionUa);
293 RawMode = ShellCommandLineGetFlag (ParamPackage, StringDpOptionUr);
294 #if PROFILING_IMPLEMENTED
295 TraceMode = ShellCommandLineGetFlag (ParamPackage, StringDpOptionUt);
296 ProfileMode = ShellCommandLineGetFlag (ParamPackage, StringDpOptionUp);
297 #endif // PROFILING_IMPLEMENTED
298 ExcludeMode = ShellCommandLineGetFlag (ParamPackage, StringDpOptionLx);
299
300 // Options with Values
301 CmdLineArg = ShellCommandLineGetValue (ParamPackage, StringDpOptionLn);
302 if (CmdLineArg == NULL) {
303 Number2Display = DEFAULT_DISPLAYCOUNT;
304 }
305 else {
306 Number2Display = StrDecimalToUintn(CmdLineArg);
307 if (Number2Display == 0) {
308 Number2Display = MAXIMUM_DISPLAYCOUNT;
309 }
310 }
311 CmdLineArg = ShellCommandLineGetValue (ParamPackage, StringDpOptionLt);
312 if (CmdLineArg == NULL) {
313 mInterestThreshold = DEFAULT_THRESHOLD; // 1ms := 1,000 us
314 }
315 else {
316 mInterestThreshold = StrDecimalToUint64(CmdLineArg);
317 }
318 // Handle Flag combinations and default behaviors
319 // If both TraceMode and ProfileMode are FALSE, set them both to TRUE
320 if ((! TraceMode) && (! ProfileMode)) {
321 TraceMode = TRUE;
322 #if PROFILING_IMPLEMENTED
323 ProfileMode = TRUE;
324 #endif // PROFILING_IMPLEMENTED
325 }
326
327 /****************************************************************************
328 **** Timer specific processing ****
329 ****************************************************************************/
330 // Get the Performance counter characteristics:
331 // Freq = Frequency in Hz
332 // StartCount = Value loaded into the counter when it starts counting
333 // EndCount = Value counter counts to before it needs to be reset
334 //
335 Freq = GetPerformanceCounterProperties (&TimerInfo.StartCount, &TimerInfo.EndCount);
336
337 // Convert the Frequency from Hz to KHz
338 TimerInfo.Frequency = (UINT32)DivU64x32 (Freq, 1000);
339
340 // Determine in which direction the performance counter counts.
341 TimerInfo.CountUp = (BOOLEAN) (TimerInfo.EndCount >= TimerInfo.StartCount);
342
343 /****************************************************************************
344 **** Print heading ****
345 ****************************************************************************/
346 // print DP's build version
347 PrintToken (STRING_TOKEN (STR_DP_BUILD_REVISION), DP_MAJOR_VERSION, DP_MINOR_VERSION);
348
349 // print performance timer characteristics
350 PrintToken (STRING_TOKEN (STR_DP_KHZ), TimerInfo.Frequency); // Print Timer frequency in KHz
351
352 if ((VerboseMode) &&
353 (! RawMode)
354 ) {
355 StringPtr = HiiGetString (gHiiHandle,
356 (EFI_STRING_ID) (TimerInfo.CountUp ? STRING_TOKEN (STR_DP_UP) : STRING_TOKEN (STR_DP_DOWN)),
357 NULL);
358 ASSERT (StringPtr != NULL);
359 PrintToken (STRING_TOKEN (STR_DP_TIMER_PROPERTIES), // Print Timer count range and direction
360 StringPtr,
361 TimerInfo.StartCount,
362 TimerInfo.EndCount
363 );
364 PrintToken (STRING_TOKEN (STR_DP_VERBOSE_THRESHOLD), mInterestThreshold);
365 }
366
367 /* **************************************************************************
368 **** Print Sections based on command line options
369 ****
370 **** Option modes have the following priority:
371 **** v Verbose -- Valid in combination with any other options
372 **** t Threshold -- Modifies All, Raw, and Cooked output
373 **** Default is 0 for All and Raw mode
374 **** Default is DEFAULT_THRESHOLD for "Cooked" mode
375 **** n Number2Display Used by All and Raw mode. Otherwise ignored.
376 **** A All -- R and S options are ignored
377 **** R Raw -- S option is ignored
378 **** s Summary -- Modifies "Cooked" output only
379 **** Cooked (Default)
380 ****
381 **** The All, Raw, and Cooked modes are modified by the Trace and Profile
382 **** options.
383 **** !T && !P := (0) Default, Both are displayed
384 **** T && !P := (1) Only Trace records are displayed
385 **** !T && P := (2) Only Profile records are displayed
386 **** T && P := (3) Same as Default, both are displayed
387 ****************************************************************************/
388 GatherStatistics();
389 if (AllMode) {
390 if (TraceMode) {
391 DumpAllTrace( Number2Display, ExcludeMode);
392 }
393 if (ProfileMode) {
394 DumpAllProfile( Number2Display, ExcludeMode);
395 }
396 }
397 else if (RawMode) {
398 if (TraceMode) {
399 DumpRawTrace( Number2Display, ExcludeMode);
400 }
401 if (ProfileMode) {
402 DumpRawProfile( Number2Display, ExcludeMode);
403 }
404 }
405 else {
406 //------------- Begin Cooked Mode Processing
407 if (TraceMode) {
408 ProcessPhases ( Ticker );
409 if ( ! SummaryMode) {
410 Status = ProcessHandles ( ExcludeMode);
411 if ( ! EFI_ERROR( Status)) {
412 ProcessPeims ( );
413 ProcessGlobal ( );
414 ProcessCumulative ();
415 }
416 }
417 }
418 if (ProfileMode) {
419 DumpAllProfile( Number2Display, ExcludeMode);
420 }
421 } //------------- End of Cooked Mode Processing
422 if ( VerboseMode || SummaryMode) {
423 DumpStatistics();
424 }
425 }
426 }
427
428 // Free the memory allocate from HiiGetString
429 //
430 ListIndex = 0;
431 while (DpParamList[ListIndex].Name != NULL) {
432 FreePool (DpParamList[ListIndex].Name);
433 ListIndex ++;
434 }
435 FreePool (DpParamList);
436
437 SafeFreePool (StringDpOptionQh);
438 SafeFreePool (StringDpOptionLh);
439 SafeFreePool (StringDpOptionUh);
440 SafeFreePool (StringDpOptionLv);
441 SafeFreePool (StringDpOptionUs);
442 SafeFreePool (StringDpOptionLs);
443 SafeFreePool (StringDpOptionUa);
444 SafeFreePool (StringDpOptionUr);
445 SafeFreePool (StringDpOptionUt);
446 SafeFreePool (StringDpOptionUp);
447 SafeFreePool (StringDpOptionLx);
448 SafeFreePool (StringDpOptionLn);
449 SafeFreePool (StringDpOptionLt);
450 SafeFreePool (StringPtr);
451 SafeFreePool (mPrintTokenBuffer);
452
453 HiiRemovePackages (gHiiHandle);
454 return Status;
455 }