]> git.proxmox.com Git - mirror_edk2.git/blame - ShellPkg/Library/UefiDpLib/Dp.c
PerformancePkg/Dp_App: Fix a memory leak issue in Dp.
[mirror_edk2.git] / ShellPkg / Library / UefiDpLib / Dp.c
CommitLineData
d41bc92c 1/** @file\r
2 Shell command for Displaying Performance Metrics.\r
3\r
4 The Dp command reads performance data and presents it in several\r
5 different formats depending upon the needs of the user. Both\r
6 Trace and Measured Profiling information is processed and presented.\r
7\r
8 Dp uses the "PerformanceLib" to read the measurement records.\r
9 The "TimerLib" provides information about the timer, such as frequency,\r
10 beginning, and ending counter values.\r
11 Measurement records contain identifying information (Handle, Token, Module)\r
12 and start and end time values.\r
13 Dp uses this information to group records in different ways. It also uses\r
14 timer information to calculate elapsed time for each measurement.\r
15 \r
9d41c48f 16 Copyright (c) 2009 - 2015, Intel Corporation. All rights reserved.\r
196ccda0 17 (C) Copyright 2015-2016 Hewlett Packard Enterprise Development LP<BR>\r
d41bc92c 18 This program and the accompanying materials\r
19 are licensed and made available under the terms and conditions of the BSD License\r
20 which accompanies this distribution. The full text of the license may be found at\r
21 http://opensource.org/licenses/bsd-license.php\r
22 \r
23 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
24 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
25**/\r
26\r
27#include "UefiDpLib.h"\r
28#include <Guid/GlobalVariable.h>\r
29#include <Library/PrintLib.h>\r
30#include <Library/HandleParsingLib.h>\r
31#include <Library/DevicePathLib.h>\r
32\r
33#include <Library/ShellLib.h>\r
34#include <Library/BaseLib.h>\r
35#include <Library/MemoryAllocationLib.h>\r
36#include <Library/DebugLib.h>\r
37#include <Library/TimerLib.h>\r
38#include <Library/UefiLib.h>\r
39\r
40#include <Guid/Performance.h>\r
41\r
dd42277f 42#include "PerformanceTokens.h"\r
d41bc92c 43#include "Dp.h"\r
44#include "Literals.h"\r
45#include "DpInternal.h"\r
46\r
47//\r
48/// Module-Global Variables\r
49///@{\r
50CHAR16 mGaugeString[DP_GAUGE_STRING_LENGTH + 1];\r
51CHAR16 mUnicodeToken[DXE_PERFORMANCE_STRING_SIZE];\r
52UINT64 mInterestThreshold;\r
53BOOLEAN mShowId = FALSE;\r
54\r
55PERF_SUMMARY_DATA SummaryData = { 0 }; ///< Create the SummaryData structure and init. to ZERO.\r
56\r
57/// Timer Specific Information.\r
58TIMER_INFO TimerInfo;\r
59\r
60/// Items for which to gather cumulative statistics.\r
61PERF_CUM_DATA CumData[] = {\r
62 PERF_INIT_CUM_DATA (LOAD_IMAGE_TOK),\r
63 PERF_INIT_CUM_DATA (START_IMAGE_TOK),\r
64 PERF_INIT_CUM_DATA (DRIVERBINDING_START_TOK),\r
65 PERF_INIT_CUM_DATA (DRIVERBINDING_SUPPORT_TOK)\r
66};\r
67\r
68/// Number of items for which we are gathering cumulative statistics.\r
69UINT32 const NumCum = sizeof(CumData) / sizeof(PERF_CUM_DATA);\r
70\r
71STATIC CONST SHELL_PARAM_ITEM ParamList[] = {\r
72 {L"-v", TypeFlag}, // -v Verbose Mode\r
73 {L"-A", TypeFlag}, // -A All, Cooked\r
74 {L"-R", TypeFlag}, // -R RAW All\r
75 {L"-s", TypeFlag}, // -s Summary\r
76#if PROFILING_IMPLEMENTED\r
77 {L"-P", TypeFlag}, // -P Dump Profile Data\r
78 {L"-T", TypeFlag}, // -T Dump Trace Data\r
79#endif // PROFILING_IMPLEMENTED\r
80 {L"-x", TypeFlag}, // -x eXclude Cumulative Items\r
81 {L"-i", TypeFlag}, // -i Display Identifier\r
a06795c6 82 {L"-c", TypeValue}, // -c Display cumulative data.\r
d41bc92c 83 {L"-n", TypeValue}, // -n # Number of records to display for A and R\r
84 {L"-t", TypeValue}, // -t # Threshold of interest\r
85 {NULL, TypeMax}\r
86 };\r
87\r
88///@}\r
89\r
90/**\r
91 Display the trailing Verbose information.\r
92**/\r
93VOID\r
94DumpStatistics( void )\r
95{\r
96 EFI_STRING StringPtr;\r
97 EFI_STRING StringPtrUnknown;\r
98 StringPtr = HiiGetString (gDpHiiHandle, STRING_TOKEN (STR_DP_SECTION_STATISTICS), NULL);\r
99 StringPtrUnknown = HiiGetString (gDpHiiHandle, STRING_TOKEN (STR_ALIT_UNKNOWN), NULL);\r
100 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DP_SECTION_HEADER), gDpHiiHandle,\r
101 (StringPtr == NULL) ? StringPtrUnknown : StringPtr);\r
102 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DP_STATS_NUMTRACE), gDpHiiHandle, SummaryData.NumTrace);\r
103 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DP_STATS_NUMINCOMPLETE), gDpHiiHandle, SummaryData.NumIncomplete);\r
104 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DP_STATS_NUMPHASES), gDpHiiHandle, SummaryData.NumSummary);\r
105 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DP_STATS_NUMHANDLES), gDpHiiHandle, SummaryData.NumHandles, SummaryData.NumTrace - SummaryData.NumHandles);\r
106 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DP_STATS_NUMPEIMS), gDpHiiHandle, SummaryData.NumPEIMs);\r
107 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DP_STATS_NUMGLOBALS), gDpHiiHandle, SummaryData.NumGlobal);\r
108#if PROFILING_IMPLEMENTED\r
109 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DP_STATS_NUMPROFILE), gDpHiiHandle, SummaryData.NumProfile);\r
110#endif // PROFILING_IMPLEMENTED\r
111 SHELL_FREE_NON_NULL (StringPtr);\r
112 SHELL_FREE_NON_NULL (StringPtrUnknown);\r
113}\r
114\r
303ec9bc
CS
115/**\r
116 Initialize the cumulative data.\r
117\r
118**/\r
119VOID\r
120InitCumulativeData (\r
121 VOID\r
122 )\r
123{\r
124 UINTN Index;\r
125\r
126 for (Index = 0; Index < NumCum; ++Index) {\r
127 CumData[Index].Count = 0;\r
128 CumData[Index].MinDur = PERF_MAXDUR;\r
129 CumData[Index].MaxDur = 0;\r
130 CumData[Index].Duration = 0;\r
131 }\r
132}\r
133\r
134/**\r
d41bc92c 135 Dump performance data.\r
136 \r
137 @param[in] ImageHandle The image handle.\r
138 @param[in] SystemTable The system table.\r
139 \r
196ccda0
CS
140 @retval SHELL_SUCCESS Command completed successfully.\r
141 @retval SHELL_INVALID_PARAMETER Command usage error.\r
142 @retval SHELL_ABORTED The user aborts the operation.\r
143 @retval value Unknown error.\r
d41bc92c 144**/\r
145SHELL_STATUS\r
146EFIAPI\r
147ShellCommandRunDp (\r
148 IN EFI_HANDLE ImageHandle,\r
149 IN EFI_SYSTEM_TABLE *SystemTable\r
150 )\r
151{\r
152 LIST_ENTRY *ParamPackage;\r
153 CONST CHAR16 *CmdLineArg;\r
154 EFI_STATUS Status;\r
155\r
156 UINT64 Freq;\r
157 UINT64 Ticker;\r
158 UINTN Number2Display;\r
159\r
a9b4ff8d
ED
160 EFI_STRING StringPtr;\r
161 BOOLEAN SummaryMode;\r
162 BOOLEAN VerboseMode;\r
163 BOOLEAN AllMode;\r
164 BOOLEAN RawMode;\r
165 BOOLEAN TraceMode;\r
166 BOOLEAN ProfileMode;\r
167 BOOLEAN ExcludeMode;\r
a06795c6
CS
168 BOOLEAN CumulativeMode;\r
169 CONST CHAR16 *CustomCumulativeToken;\r
170 PERF_CUM_DATA *CustomCumulativeData;\r
196ccda0 171 SHELL_STATUS ShellStatus;\r
a9b4ff8d
ED
172\r
173 StringPtr = NULL;\r
174 SummaryMode = FALSE;\r
175 VerboseMode = FALSE;\r
176 AllMode = FALSE;\r
177 RawMode = FALSE;\r
178 TraceMode = FALSE;\r
179 ProfileMode = FALSE;\r
180 ExcludeMode = FALSE;\r
a06795c6
CS
181 CumulativeMode = FALSE;\r
182 CustomCumulativeData = NULL;\r
196ccda0 183 ShellStatus = SHELL_SUCCESS;\r
d41bc92c 184\r
185 // Get DP's entry time as soon as possible.\r
186 // This is used as the Shell-Phase end time.\r
187 //\r
188 Ticker = GetPerformanceCounter ();\r
189\r
190 //\r
191 // initialize the shell lib (we must be in non-auto-init...)\r
192 //\r
193 Status = ShellInitialize();\r
194 ASSERT_EFI_ERROR(Status);\r
195\r
196 Status = CommandInit();\r
197 ASSERT_EFI_ERROR(Status);\r
198\r
199 //\r
200 // Process Command Line arguments\r
201 //\r
202 Status = ShellCommandLineParse (ParamList, &ParamPackage, NULL, TRUE);\r
203 if (EFI_ERROR(Status)) {\r
204 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DP_INVALID_ARG), gDpHiiHandle);\r
205 return SHELL_INVALID_PARAMETER;\r
206 }\r
207\r
208 //\r
209 // Boolean options\r
210 //\r
211 VerboseMode = ShellCommandLineGetFlag (ParamPackage, L"-v");\r
212 SummaryMode = (BOOLEAN) (ShellCommandLineGetFlag (ParamPackage, L"-S") || ShellCommandLineGetFlag (ParamPackage, L"-s"));\r
213 AllMode = ShellCommandLineGetFlag (ParamPackage, L"-A");\r
214 RawMode = ShellCommandLineGetFlag (ParamPackage, L"-R");\r
215#if PROFILING_IMPLEMENTED\r
216 TraceMode = ShellCommandLineGetFlag (ParamPackage, L"-T");\r
217 ProfileMode = ShellCommandLineGetFlag (ParamPackage, L"-P");\r
218#endif // PROFILING_IMPLEMENTED\r
219 ExcludeMode = ShellCommandLineGetFlag (ParamPackage, L"-x");\r
220 mShowId = ShellCommandLineGetFlag (ParamPackage, L"-i");\r
a06795c6 221 CumulativeMode = ShellCommandLineGetFlag (ParamPackage, L"-c");\r
d41bc92c 222\r
223 // Options with Values\r
224 CmdLineArg = ShellCommandLineGetValue (ParamPackage, L"-n");\r
225 if (CmdLineArg == NULL) {\r
226 Number2Display = DEFAULT_DISPLAYCOUNT;\r
227 } else {\r
228 Number2Display = StrDecimalToUintn(CmdLineArg);\r
229 if (Number2Display == 0) {\r
230 Number2Display = MAXIMUM_DISPLAYCOUNT;\r
231 }\r
232 }\r
233\r
234 CmdLineArg = ShellCommandLineGetValue (ParamPackage, L"-t");\r
235 if (CmdLineArg == NULL) {\r
236 mInterestThreshold = DEFAULT_THRESHOLD; // 1ms := 1,000 us\r
237 } else {\r
238 mInterestThreshold = StrDecimalToUint64(CmdLineArg);\r
239 }\r
240\r
241 // Handle Flag combinations and default behaviors\r
242 // If both TraceMode and ProfileMode are FALSE, set them both to TRUE\r
243 if ((! TraceMode) && (! ProfileMode)) {\r
244 TraceMode = TRUE;\r
245#if PROFILING_IMPLEMENTED\r
246 ProfileMode = TRUE;\r
247#endif // PROFILING_IMPLEMENTED\r
248 }\r
249\r
303ec9bc
CS
250 //\r
251 // Initialize the pre-defined cumulative data.\r
252 //\r
253 InitCumulativeData ();\r
254\r
a06795c6
CS
255 //\r
256 // Init the custom cumulative data.\r
257 //\r
258 CustomCumulativeToken = ShellCommandLineGetValue (ParamPackage, L"-c");\r
259 if (CustomCumulativeToken != NULL) {\r
260 CustomCumulativeData = AllocateZeroPool (sizeof (PERF_CUM_DATA));\r
9d41c48f 261 ASSERT (CustomCumulativeData != NULL);\r
a06795c6
CS
262 CustomCumulativeData->MinDur = 0;\r
263 CustomCumulativeData->MaxDur = 0;\r
264 CustomCumulativeData->Count = 0;\r
265 CustomCumulativeData->Duration = 0;\r
266 CustomCumulativeData->Name = AllocateZeroPool (StrLen (CustomCumulativeToken) + 1);\r
267 UnicodeStrToAsciiStr (CustomCumulativeToken, CustomCumulativeData->Name);\r
268 }\r
269\r
d41bc92c 270 //\r
271 // Timer specific processing\r
272 //\r
273 // Get the Performance counter characteristics:\r
274 // Freq = Frequency in Hz\r
275 // StartCount = Value loaded into the counter when it starts counting\r
276 // EndCount = Value counter counts to before it needs to be reset\r
277 //\r
278 Freq = GetPerformanceCounterProperties (&TimerInfo.StartCount, &TimerInfo.EndCount);\r
279\r
280 // Convert the Frequency from Hz to KHz\r
281 TimerInfo.Frequency = (UINT32)DivU64x32 (Freq, 1000);\r
282\r
283 // Determine in which direction the performance counter counts.\r
284 TimerInfo.CountUp = (BOOLEAN) (TimerInfo.EndCount >= TimerInfo.StartCount);\r
285\r
286 //\r
287 // Print header\r
288 //\r
289 // print DP's build version\r
290 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DP_BUILD_REVISION), gDpHiiHandle, DP_MAJOR_VERSION, DP_MINOR_VERSION);\r
291\r
292 // print performance timer characteristics\r
293 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DP_KHZ), gDpHiiHandle, TimerInfo.Frequency);\r
294\r
295 if (VerboseMode && !RawMode) {\r
296 StringPtr = HiiGetString (gDpHiiHandle,\r
297 (EFI_STRING_ID) (TimerInfo.CountUp ? STRING_TOKEN (STR_DP_UP) : STRING_TOKEN (STR_DP_DOWN)), NULL);\r
298 ASSERT (StringPtr != NULL);\r
299 // Print Timer count range and direction\r
300 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DP_TIMER_PROPERTIES), gDpHiiHandle,\r
301 StringPtr,\r
302 TimerInfo.StartCount,\r
303 TimerInfo.EndCount\r
304 );\r
305 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DP_VERBOSE_THRESHOLD), gDpHiiHandle, mInterestThreshold);\r
306 }\r
307\r
308/****************************************************************************\r
309**** Print Sections based on command line options\r
310****\r
311**** Option modes have the following priority:\r
312**** v Verbose -- Valid in combination with any other options\r
313**** t Threshold -- Modifies All, Raw, and Cooked output\r
314**** Default is 0 for All and Raw mode\r
315**** Default is DEFAULT_THRESHOLD for "Cooked" mode\r
316**** n Number2Display Used by All and Raw mode. Otherwise ignored.\r
317**** A All -- R and S options are ignored\r
318**** R Raw -- S option is ignored\r
319**** s Summary -- Modifies "Cooked" output only\r
320**** Cooked (Default)\r
321****\r
322**** The All, Raw, and Cooked modes are modified by the Trace and Profile\r
323**** options.\r
324**** !T && !P := (0) Default, Both are displayed\r
325**** T && !P := (1) Only Trace records are displayed\r
326**** !T && P := (2) Only Profile records are displayed\r
327**** T && P := (3) Same as Default, both are displayed\r
328****************************************************************************/\r
a06795c6
CS
329 GatherStatistics (CustomCumulativeData);\r
330 if (CumulativeMode) { \r
331 ProcessCumulative (CustomCumulativeData);\r
332 } else if (AllMode) {\r
d41bc92c 333 if (TraceMode) {\r
196ccda0
CS
334 Status = DumpAllTrace( Number2Display, ExcludeMode);\r
335 if (Status == EFI_ABORTED) {\r
336 ShellStatus = SHELL_ABORTED;\r
337 goto Done;\r
338 }\r
d41bc92c 339 }\r
340 if (ProfileMode) {\r
341 DumpAllProfile( Number2Display, ExcludeMode);\r
342 }\r
343 } else if (RawMode) {\r
344 if (TraceMode) {\r
196ccda0
CS
345 Status = DumpRawTrace( Number2Display, ExcludeMode);\r
346 if (Status == EFI_ABORTED) {\r
347 ShellStatus = SHELL_ABORTED;\r
348 goto Done;\r
349 }\r
d41bc92c 350 }\r
351 if (ProfileMode) {\r
352 DumpRawProfile( Number2Display, ExcludeMode);\r
353 }\r
354 } else {\r
355 //------------- Begin Cooked Mode Processing\r
356 if (TraceMode) {\r
357 ProcessPhases ( Ticker );\r
358 if ( ! SummaryMode) {\r
359 Status = ProcessHandles ( ExcludeMode);\r
196ccda0
CS
360 if (Status == EFI_ABORTED) {\r
361 ShellStatus = SHELL_ABORTED;\r
362 goto Done;\r
363 }\r
364\r
365 Status = ProcessPeims ();\r
366 if (Status == EFI_ABORTED) {\r
367 ShellStatus = SHELL_ABORTED;\r
368 goto Done;\r
369 }\r
370\r
371 Status = ProcessGlobal ();\r
372 if (Status == EFI_ABORTED) {\r
373 ShellStatus = SHELL_ABORTED;\r
374 goto Done;\r
d41bc92c 375 }\r
196ccda0
CS
376\r
377 ProcessCumulative (NULL);\r
d41bc92c 378 }\r
379 }\r
380 if (ProfileMode) {\r
381 DumpAllProfile( Number2Display, ExcludeMode);\r
382 }\r
383 } //------------- End of Cooked Mode Processing\r
384 if ( VerboseMode || SummaryMode) {\r
385 DumpStatistics();\r
386 }\r
387\r
196ccda0 388Done:\r
d41bc92c 389 SHELL_FREE_NON_NULL (StringPtr);\r
a06795c6
CS
390 if (CustomCumulativeData != NULL) {\r
391 SHELL_FREE_NON_NULL (CustomCumulativeData->Name);\r
392 }\r
393 SHELL_FREE_NON_NULL (CustomCumulativeData);\r
d41bc92c 394\r
196ccda0 395 return ShellStatus;\r
d41bc92c 396}\r