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