]> git.proxmox.com Git - mirror_edk2.git/blob - PerformancePkg/Dp_App/Dp.c
Refine code to make code follow the coding style.
[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 - 2010, 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 CHAR16 *mPrintTokenBuffer = NULL;
48 CHAR16 mGaugeString[DXE_PERFORMANCE_STRING_SIZE];
49 CHAR16 mUnicodeToken[PERF_TOKEN_LENGTH + 1];
50 UINT64 mInterestThreshold;
51
52 PERF_SUMMARY_DATA SummaryData = { 0 }; ///< Create the SummaryData structure and init. to ZERO.
53
54 /// Timer Specific Information.
55 TIMER_INFO TimerInfo;
56
57 /// Items for which to gather cumulative statistics.
58 PERF_CUM_DATA CumData[] = {
59 PERF_INIT_CUM_DATA (LOAD_IMAGE_TOK),
60 PERF_INIT_CUM_DATA (START_IMAGE_TOK),
61 PERF_INIT_CUM_DATA (DRIVERBINDING_START_TOK),
62 PERF_INIT_CUM_DATA (DRIVERBINDING_SUPPORT_TOK)
63 };
64
65 /// Number of items for which we are gathering cumulative statistics.
66 UINT32 const NumCum = sizeof(CumData) / sizeof(PERF_CUM_DATA);
67
68 SHELL_PARAM_ITEM DpParamList[] = {
69 {STR_DP_OPTION_QH, TypeFlag}, // -? Help
70 {STR_DP_OPTION_LH, TypeFlag}, // -h Help
71 {STR_DP_OPTION_UH, TypeFlag}, // -H Help
72 {STR_DP_OPTION_LV, TypeFlag}, // -v Verbose Mode
73 {STR_DP_OPTION_UA, TypeFlag}, // -A All, Cooked
74 {STR_DP_OPTION_UR, TypeFlag}, // -R RAW All
75 {STR_DP_OPTION_LS, TypeFlag}, // -s Summary
76 #if PROFILING_IMPLEMENTED
77 {STR_DP_OPTION_UP, TypeFlag}, // -P Dump Profile Data
78 {STR_DP_OPTION_UT, TypeFlag}, // -T Dump Trace Data
79 #endif
80 {STR_DP_OPTION_LX, TypeFlag}, // -x eXclude Cumulative Items
81 {STR_DP_OPTION_LN, TypeValue}, // -n # Number of records to display for A and R
82 {STR_DP_OPTION_LT, TypeValue}, // -t # Threshold of interest
83 {NULL, TypeMax}
84 };
85
86 ///@}
87
88 /**
89 Display Usage and Help information.
90 **/
91 VOID
92 ShowHelp( void )
93 {
94 PrintToken (STRING_TOKEN (STR_DP_HELP_HEAD));
95 #if PROFILING_IMPLEMENTED
96 PrintToken (STRING_TOKEN (STR_DP_HELP_FLAGS));
97 #else
98 PrintToken (STRING_TOKEN (STR_DP_HELP_FLAGS_2));
99 #endif // PROFILING_IMPLEMENTED
100 PrintToken (STRING_TOKEN (STR_DP_HELP_PAGINATE));
101 PrintToken (STRING_TOKEN (STR_DP_HELP_VERBOSE));
102 PrintToken (STRING_TOKEN (STR_DP_HELP_EXCLUDE));
103 PrintToken (STRING_TOKEN (STR_DP_HELP_STAT));
104 PrintToken (STRING_TOKEN (STR_DP_HELP_ALL));
105 PrintToken (STRING_TOKEN (STR_DP_HELP_RAW));
106 #if PROFILING_IMPLEMENTED
107 PrintToken (STRING_TOKEN (STR_DP_HELP_TRACE));
108 PrintToken (STRING_TOKEN (STR_DP_HELP_PROFILE));
109 #endif // PROFILING_IMPLEMENTED
110 PrintToken (STRING_TOKEN (STR_DP_HELP_THRESHOLD));
111 PrintToken (STRING_TOKEN (STR_DP_HELP_COUNT));
112 PrintToken (STRING_TOKEN (STR_DP_HELP_HELP));
113 Print(L"\n");
114 }
115
116 /**
117 Display the trailing Verbose information.
118 **/
119 VOID
120 DumpStatistics( void )
121 {
122 EFI_STRING StringPtr;
123
124 StringPtr = HiiGetString (gHiiHandle, STRING_TOKEN (STR_DP_SECTION_STATISTICS), NULL);
125 PrintToken( STRING_TOKEN (STR_DP_SECTION_HEADER),
126 (StringPtr == NULL) ? ALit_UNKNOWN: StringPtr);
127
128 PrintToken( STRING_TOKEN (STR_DP_STATS_NUMTRACE), SummaryData.NumTrace);
129 PrintToken( STRING_TOKEN (STR_DP_STATS_NUMINCOMPLETE), SummaryData.NumIncomplete);
130 PrintToken( STRING_TOKEN (STR_DP_STATS_NUMPHASES), SummaryData.NumSummary);
131 PrintToken( STRING_TOKEN (STR_DP_STATS_NUMHANDLES), SummaryData.NumHandles, SummaryData.NumTrace - SummaryData.NumHandles);
132 PrintToken( STRING_TOKEN (STR_DP_STATS_NUMPEIMS), SummaryData.NumPEIMs);
133 PrintToken( STRING_TOKEN (STR_DP_STATS_NUMGLOBALS), SummaryData.NumGlobal);
134 #if PROFILING_IMPLEMENTED
135 PrintToken( STRING_TOKEN (STR_DP_STATS_NUMPROFILE), SummaryData.NumProfile);
136 #endif // PROFILING_IMPLEMENTED
137 }
138
139 /**
140 Dump performance data.
141
142 @param[in] ImageHandle The image handle.
143 @param[in] SystemTable The system table.
144
145 @retval EFI_SUCCESS Command completed successfully.
146 @retval EFI_INVALID_PARAMETER Command usage error.
147 @retval value Unknown error.
148
149 **/
150 EFI_STATUS
151 EFIAPI
152 InitializeDp (
153 IN EFI_HANDLE ImageHandle,
154 IN EFI_SYSTEM_TABLE *SystemTable
155 )
156 {
157 UINT64 Freq;
158 UINT64 Ticker;
159
160 LIST_ENTRY *ParamPackage;
161 CONST CHAR16 *CmdLineArg;
162 EFI_STRING StringPtr;
163 UINTN Number2Display;
164
165 EFI_STATUS Status;
166 BOOLEAN SummaryMode;
167 BOOLEAN VerboseMode;
168 BOOLEAN AllMode;
169 BOOLEAN RawMode;
170 BOOLEAN TraceMode;
171 BOOLEAN ProfileMode;
172 BOOLEAN ExcludeMode;
173
174 SummaryMode = FALSE;
175 VerboseMode = FALSE;
176 AllMode = FALSE;
177 RawMode = FALSE;
178 TraceMode = FALSE;
179 ProfileMode = FALSE;
180 ExcludeMode = FALSE;
181 // Get DP's entry time as soon as possible.
182 // This is used as the Shell-Phase end time.
183 //
184 Ticker = GetPerformanceCounter ();
185
186 // Register our string package with HII and return the handle to it.
187 //
188 gHiiHandle = HiiAddPackages (&gEfiCallerIdGuid, ImageHandle, DPStrings, NULL);
189 ASSERT (gHiiHandle != NULL);
190
191 /****************************************************************************
192 **** Process Command Line arguments ****
193 ****************************************************************************/
194 Status = ShellCommandLineParse (DpParamList, &ParamPackage, NULL, TRUE);
195
196 if (EFI_ERROR(Status)) {
197 PrintToken (STRING_TOKEN (STR_DP_INVALID_ARG));
198 ShowHelp();
199 }
200 else {
201 if (ShellCommandLineGetFlag (ParamPackage, STR_DP_OPTION_QH) ||
202 ShellCommandLineGetFlag (ParamPackage, STR_DP_OPTION_LH) ||
203 ShellCommandLineGetFlag (ParamPackage, STR_DP_OPTION_UH))
204 {
205 ShowHelp();
206 }
207 else {
208 // Boolean Options
209 VerboseMode = (ShellCommandLineGetFlag (ParamPackage, STR_DP_OPTION_LV));
210 SummaryMode = (ShellCommandLineGetFlag (ParamPackage, STR_DP_OPTION_US) ||
211 ShellCommandLineGetFlag (ParamPackage, STR_DP_OPTION_LS));
212 AllMode = (ShellCommandLineGetFlag (ParamPackage, STR_DP_OPTION_UA));
213 RawMode = (ShellCommandLineGetFlag (ParamPackage, STR_DP_OPTION_UR));
214 #if PROFILING_IMPLEMENTED
215 TraceMode = (ShellCommandLineGetFlag (ParamPackage, STR_DP_OPTION_UT));
216 ProfileMode = (ShellCommandLineGetFlag (ParamPackage, STR_DP_OPTION_UP));
217 #endif // PROFILING_IMPLEMENTED
218 ExcludeMode = (ShellCommandLineGetFlag (ParamPackage, STR_DP_OPTION_LX));
219
220 // Options with Values
221 CmdLineArg = ( ShellCommandLineGetValue (ParamPackage, STR_DP_OPTION_LN));
222 if (CmdLineArg == NULL) {
223 Number2Display = DEFAULT_DISPLAYCOUNT;
224 }
225 else {
226 Number2Display = StrDecimalToUintn(CmdLineArg);
227 if (Number2Display == 0) {
228 Number2Display = MAXIMUM_DISPLAYCOUNT;
229 }
230 }
231 CmdLineArg = (ShellCommandLineGetValue (ParamPackage, STR_DP_OPTION_LT));
232 if (CmdLineArg == NULL) {
233 mInterestThreshold = DEFAULT_THRESHOLD; // 1ms := 1,000 us
234 }
235 else {
236 mInterestThreshold = StrDecimalToUint64(CmdLineArg);
237 }
238 // Handle Flag combinations and default behaviors
239 // If both TraceMode and ProfileMode are FALSE, set them both to TRUE
240 if ((! TraceMode) && (! ProfileMode)) {
241 TraceMode = TRUE;
242 #if PROFILING_IMPLEMENTED
243 ProfileMode = TRUE;
244 #endif // PROFILING_IMPLEMENTED
245 }
246
247 /****************************************************************************
248 **** Timer specific processing ****
249 ****************************************************************************/
250 // Get the Performance counter characteristics:
251 // Freq = Frequency in Hz
252 // StartCount = Value loaded into the counter when it starts counting
253 // EndCount = Value counter counts to before it needs to be reset
254 //
255 Freq = GetPerformanceCounterProperties (&TimerInfo.StartCount, &TimerInfo.EndCount);
256
257 // Convert the Frequency from Hz to KHz
258 TimerInfo.Frequency = (UINT32)DivU64x32 (Freq, 1000);
259
260 // Determine in which direction the performance counter counts.
261 TimerInfo.CountUp = (BOOLEAN) (TimerInfo.EndCount >= TimerInfo.StartCount);
262
263 /****************************************************************************
264 **** Print heading ****
265 ****************************************************************************/
266 // print DP's build version
267 PrintToken (STRING_TOKEN (STR_DP_BUILD_REVISION), DP_MAJOR_VERSION, DP_MINOR_VERSION);
268
269 // print performance timer characteristics
270 PrintToken (STRING_TOKEN (STR_DP_KHZ), TimerInfo.Frequency); // Print Timer frequency in KHz
271
272 if ((VerboseMode) &&
273 (! RawMode)
274 ) {
275 StringPtr = HiiGetString (gHiiHandle,
276 TimerInfo.CountUp ? STRING_TOKEN (STR_DP_UP) : STRING_TOKEN (STR_DP_DOWN),
277 NULL);
278 ASSERT (StringPtr != NULL);
279 PrintToken (STRING_TOKEN (STR_DP_TIMER_PROPERTIES), // Print Timer count range and direction
280 StringPtr,
281 TimerInfo.StartCount,
282 TimerInfo.EndCount
283 );
284 PrintToken (STRING_TOKEN (STR_DP_VERBOSE_THRESHOLD), mInterestThreshold);
285 }
286
287 /* **************************************************************************
288 **** Print Sections based on command line options
289 ****
290 **** Option modes have the following priority:
291 **** v Verbose -- Valid in combination with any other options
292 **** t Threshold -- Modifies All, Raw, and Cooked output
293 **** Default is 0 for All and Raw mode
294 **** Default is DEFAULT_THRESHOLD for "Cooked" mode
295 **** n Number2Display Used by All and Raw mode. Otherwise ignored.
296 **** A All -- R and S options are ignored
297 **** R Raw -- S option is ignored
298 **** s Summary -- Modifies "Cooked" output only
299 **** Cooked (Default)
300 ****
301 **** The All, Raw, and Cooked modes are modified by the Trace and Profile
302 **** options.
303 **** !T && !P := (0) Default, Both are displayed
304 **** T && !P := (1) Only Trace records are displayed
305 **** !T && P := (2) Only Profile records are displayed
306 **** T && P := (3) Same as Default, both are displayed
307 ****************************************************************************/
308 GatherStatistics();
309 if (AllMode) {
310 if (TraceMode) {
311 DumpAllTrace( Number2Display, ExcludeMode);
312 }
313 if (ProfileMode) {
314 DumpAllProfile( Number2Display, ExcludeMode);
315 }
316 }
317 else if (RawMode) {
318 if (TraceMode) {
319 DumpRawTrace( Number2Display, ExcludeMode);
320 }
321 if (ProfileMode) {
322 DumpRawProfile( Number2Display, ExcludeMode);
323 }
324 }
325 else {
326 //------------- Begin Cooked Mode Processing
327 if (TraceMode) {
328 ProcessPhases ( Ticker );
329 if ( ! SummaryMode) {
330 Status = ProcessHandles ( ExcludeMode);
331 if ( ! EFI_ERROR( Status)) {
332 ProcessPeims ( );
333 ProcessGlobal ( );
334 ProcessCumulative ();
335 }
336 }
337 }
338 if (ProfileMode) {
339 DumpAllProfile( Number2Display, ExcludeMode);
340 }
341 } //------------- End of Cooked Mode Processing
342 if ( VerboseMode || SummaryMode) {
343 DumpStatistics();
344 }
345 }
346 }
347 (void) FreePool (mPrintTokenBuffer);
348 HiiRemovePackages (gHiiHandle);
349 return Status;
350 }