]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/DynamicCommand/DpDynamicCommand/DpUtilities.c
ShellPkg/dp: Update dp tool to parse new Perf record
[mirror_edk2.git] / ShellPkg / DynamicCommand / DpDynamicCommand / DpUtilities.c
1 /** @file
2 Utility functions used by the Dp application.
3
4 Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.
5 (C) Copyright 2015-2016 Hewlett Packard Enterprise Development LP<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13 **/
14
15 #include <Library/BaseLib.h>
16 #include <Library/BaseMemoryLib.h>
17 #include <Library/MemoryAllocationLib.h>
18 #include <Library/DebugLib.h>
19 #include <Library/UefiBootServicesTableLib.h>
20 #include <Library/PeCoffGetEntryPointLib.h>
21 #include <Library/PrintLib.h>
22 #include <Library/HiiLib.h>
23 #include <Library/PcdLib.h>
24 #include <Library/UefiLib.h>
25 #include <Library/DevicePathLib.h>
26 #include <Library/HandleParsingLib.h>
27
28 #include <Pi/PiFirmwareFile.h>
29 #include <Library/DxeServicesLib.h>
30
31 #include <Protocol/LoadedImage.h>
32 #include <Protocol/DriverBinding.h>
33 #include <Protocol/ComponentName2.h>
34 #include <Protocol/DevicePath.h>
35
36 #include <Guid/Performance.h>
37
38 #include "Dp.h"
39 #include "Literals.h"
40 #include "DpInternal.h"
41
42 /**
43 Calculate an event's duration in timer ticks.
44
45 Given the count direction and the event's start and end timer values,
46 calculate the duration of the event in timer ticks. Information for
47 the current measurement is pointed to by the parameter.
48
49 If the measurement's start time is 1, it indicates that the developer
50 is indicating that the measurement began at the release of reset.
51 The start time is adjusted to the timer's starting count before performing
52 the elapsed time calculation.
53
54 The calculated duration, in ticks, is the absolute difference between
55 the measurement's ending and starting counts.
56
57 @param Measurement Pointer to a MEASUREMENT_RECORD structure containing
58 data for the current measurement.
59
60 @return The 64-bit duration of the event.
61 **/
62 UINT64
63 GetDuration (
64 IN OUT MEASUREMENT_RECORD *Measurement
65 )
66 {
67 UINT64 Duration;
68 BOOLEAN Error;
69
70 if (Measurement->EndTimeStamp == 0) {
71 return 0;
72 }
73
74 Duration = Measurement->EndTimeStamp - Measurement->StartTimeStamp;
75 Error = (BOOLEAN)(Duration > Measurement->EndTimeStamp);
76
77 if (Error) {
78 DEBUG ((EFI_D_ERROR, ALit_TimerLibError));
79 Duration = 0;
80 }
81 return Duration;
82 }
83
84 /**
85 Determine whether the Measurement record is for an EFI Phase.
86
87 The Token and Module members of the measurement record are checked.
88 Module must be empty and Token must be one of SEC, PEI, DXE, BDS, or SHELL.
89
90 @param[in] Measurement A pointer to the Measurement record to test.
91
92 @retval TRUE The measurement record is for an EFI Phase.
93 @retval FALSE The measurement record is NOT for an EFI Phase.
94 **/
95 BOOLEAN
96 IsPhase(
97 IN MEASUREMENT_RECORD *Measurement
98 )
99 {
100 BOOLEAN RetVal;
101
102 RetVal = (BOOLEAN)(
103 ((AsciiStrCmp (Measurement->Token, ALit_SEC) == 0) ||
104 (AsciiStrCmp (Measurement->Token, ALit_PEI) == 0) ||
105 (AsciiStrCmp (Measurement->Token, ALit_DXE) == 0) ||
106 (AsciiStrCmp (Measurement->Token, ALit_BDS) == 0))
107 );
108 return RetVal;
109 }
110
111 /**
112 Determine whether the Measurement record is for core code.
113
114 @param[in] Measurement A pointer to the Measurement record to test.
115
116 @retval TRUE The measurement record is used for core.
117 @retval FALSE The measurement record is NOT used for core.
118
119 **/
120 BOOLEAN
121 IsCorePerf(
122 IN MEASUREMENT_RECORD *Measurement
123 )
124 {
125 BOOLEAN RetVal;
126
127 RetVal = (BOOLEAN)(
128 ((Measurement->Identifier == MODULE_START_ID) ||
129 (Measurement->Identifier == MODULE_END_ID) ||
130 (Measurement->Identifier == MODULE_LOADIMAGE_START_ID) ||
131 (Measurement->Identifier == MODULE_LOADIMAGE_END_ID) ||
132 (Measurement->Identifier == MODULE_DB_START_ID) ||
133 (Measurement->Identifier == MODULE_DB_END_ID) ||
134 (Measurement->Identifier == MODULE_DB_SUPPORT_START_ID) ||
135 (Measurement->Identifier == MODULE_DB_SUPPORT_END_ID) ||
136 (Measurement->Identifier == MODULE_DB_STOP_START_ID) ||
137 (Measurement->Identifier == MODULE_DB_STOP_START_ID))
138 );
139 return RetVal;
140 }
141
142 /**
143 Get the file name portion of the Pdb File Name.
144
145 The portion of the Pdb File Name between the last backslash and
146 either a following period or the end of the string is converted
147 to Unicode and copied into UnicodeBuffer. The name is truncated,
148 if necessary, to ensure that UnicodeBuffer is not overrun.
149
150 @param[in] PdbFileName Pdb file name.
151 @param[out] UnicodeBuffer The resultant Unicode File Name.
152
153 **/
154 VOID
155 DpGetShortPdbFileName (
156 IN CHAR8 *PdbFileName,
157 OUT CHAR16 *UnicodeBuffer
158 )
159 {
160 UINTN IndexA; // Current work location within an ASCII string.
161 UINTN IndexU; // Current work location within a Unicode string.
162 UINTN StartIndex;
163 UINTN EndIndex;
164
165 ZeroMem (UnicodeBuffer, (DP_GAUGE_STRING_LENGTH + 1) * sizeof (CHAR16));
166
167 if (PdbFileName == NULL) {
168 StrnCpyS (UnicodeBuffer, DP_GAUGE_STRING_LENGTH + 1, L" ", 1);
169 } else {
170 StartIndex = 0;
171 for (EndIndex = 0; PdbFileName[EndIndex] != 0; EndIndex++)
172 ;
173 for (IndexA = 0; PdbFileName[IndexA] != 0; IndexA++) {
174 if ((PdbFileName[IndexA] == '\\') || (PdbFileName[IndexA] == '/')) {
175 StartIndex = IndexA + 1;
176 }
177
178 if (PdbFileName[IndexA] == '.') {
179 EndIndex = IndexA;
180 }
181 }
182
183 IndexU = 0;
184 for (IndexA = StartIndex; IndexA < EndIndex; IndexA++) {
185 UnicodeBuffer[IndexU] = (CHAR16) PdbFileName[IndexA];
186 IndexU++;
187 if (IndexU >= DP_GAUGE_STRING_LENGTH) {
188 UnicodeBuffer[DP_GAUGE_STRING_LENGTH] = 0;
189 break;
190 }
191 }
192 }
193 }
194
195 /**
196 Get a human readable name for an image handle.
197 The following methods will be tried orderly:
198 1. Image PDB
199 2. ComponentName2 protocol
200 3. FFS UI section
201 4. Image GUID
202 5. Image DevicePath
203 6. Unknown Driver Name
204
205 @param[in] Handle
206
207 @post The resulting Unicode name string is stored in the
208 mGaugeString global array.
209
210 **/
211 VOID
212 DpGetNameFromHandle (
213 IN EFI_HANDLE Handle
214 )
215 {
216 EFI_STATUS Status;
217 EFI_LOADED_IMAGE_PROTOCOL *Image;
218 CHAR8 *PdbFileName;
219 EFI_DRIVER_BINDING_PROTOCOL *DriverBinding;
220 EFI_STRING StringPtr;
221 EFI_DEVICE_PATH_PROTOCOL *LoadedImageDevicePath;
222 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
223 EFI_GUID *NameGuid;
224 CHAR16 *NameString;
225 UINTN StringSize;
226 CHAR8 *PlatformLanguage;
227 CHAR8 *BestLanguage;
228 EFI_COMPONENT_NAME2_PROTOCOL *ComponentName2;
229
230 Image = NULL;
231 LoadedImageDevicePath = NULL;
232 DevicePath = NULL;
233
234 //
235 // Method 1: Get the name string from image PDB
236 //
237 Status = gBS->HandleProtocol (
238 Handle,
239 &gEfiLoadedImageProtocolGuid,
240 (VOID **) &Image
241 );
242
243 if (EFI_ERROR (Status)) {
244 Status = gBS->OpenProtocol (
245 Handle,
246 &gEfiDriverBindingProtocolGuid,
247 (VOID **) &DriverBinding,
248 NULL,
249 NULL,
250 EFI_OPEN_PROTOCOL_GET_PROTOCOL
251 );
252 if (!EFI_ERROR (Status)) {
253 Status = gBS->HandleProtocol (
254 DriverBinding->ImageHandle,
255 &gEfiLoadedImageProtocolGuid,
256 (VOID **) &Image
257 );
258 }
259 }
260
261 if (!EFI_ERROR (Status)) {
262 PdbFileName = PeCoffLoaderGetPdbPointer (Image->ImageBase);
263
264 if (PdbFileName != NULL) {
265 DpGetShortPdbFileName (PdbFileName, mGaugeString);
266 return;
267 }
268 }
269
270 //
271 // Method 2: Get the name string from ComponentName2 protocol
272 //
273 Status = gBS->HandleProtocol (
274 Handle,
275 &gEfiComponentName2ProtocolGuid,
276 (VOID **) &ComponentName2
277 );
278 if (!EFI_ERROR (Status)) {
279 //
280 // Firstly use platform language setting, secondly use driver's first supported language.
281 //
282 GetVariable2 (L"PlatformLang", &gEfiGlobalVariableGuid, (VOID**)&PlatformLanguage, NULL);
283 BestLanguage = GetBestLanguage(
284 ComponentName2->SupportedLanguages,
285 FALSE,
286 (PlatformLanguage != NULL) ? PlatformLanguage : "",
287 ComponentName2->SupportedLanguages,
288 NULL
289 );
290 SHELL_FREE_NON_NULL (PlatformLanguage);
291
292 Status = ComponentName2->GetDriverName (
293 ComponentName2,
294 BestLanguage != NULL ? BestLanguage : "en-US",
295 &StringPtr
296 );
297 if (!EFI_ERROR (Status)) {
298 SHELL_FREE_NON_NULL (BestLanguage);
299 StrnCpyS (mGaugeString, DP_GAUGE_STRING_LENGTH + 1, StringPtr, DP_GAUGE_STRING_LENGTH);
300 mGaugeString[DP_GAUGE_STRING_LENGTH] = 0;
301 return;
302 }
303 }
304
305 Status = gBS->HandleProtocol (
306 Handle,
307 &gEfiLoadedImageDevicePathProtocolGuid,
308 (VOID **) &LoadedImageDevicePath
309 );
310 if (!EFI_ERROR (Status) && (LoadedImageDevicePath != NULL)) {
311 DevicePath = LoadedImageDevicePath;
312 } else if (Image != NULL) {
313 DevicePath = Image->FilePath;
314 }
315
316 if (DevicePath != NULL) {
317 //
318 // Try to get image GUID from image DevicePath
319 //
320 NameGuid = NULL;
321 while (!IsDevicePathEndType (DevicePath)) {
322 NameGuid = EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) DevicePath);
323 if (NameGuid != NULL) {
324 break;
325 }
326 DevicePath = NextDevicePathNode (DevicePath);
327 }
328
329 if (NameGuid != NULL) {
330 //
331 // Try to get the image's FFS UI section by image GUID
332 //
333 NameString = NULL;
334 StringSize = 0;
335 Status = GetSectionFromAnyFv (
336 NameGuid,
337 EFI_SECTION_USER_INTERFACE,
338 0,
339 (VOID **) &NameString,
340 &StringSize
341 );
342
343 if (!EFI_ERROR (Status)) {
344 //
345 // Method 3. Get the name string from FFS UI section
346 //
347 StrnCpyS (mGaugeString, DP_GAUGE_STRING_LENGTH + 1, NameString, DP_GAUGE_STRING_LENGTH);
348 mGaugeString[DP_GAUGE_STRING_LENGTH] = 0;
349 FreePool (NameString);
350 } else {
351 //
352 // Method 4: Get the name string from image GUID
353 //
354 UnicodeSPrint (mGaugeString, sizeof (mGaugeString), L"%g", NameGuid);
355 }
356 return;
357 } else {
358 //
359 // Method 5: Get the name string from image DevicePath
360 //
361 NameString = ConvertDevicePathToText (DevicePath, TRUE, FALSE);
362 if (NameString != NULL) {
363 StrnCpyS (mGaugeString, DP_GAUGE_STRING_LENGTH + 1, NameString, DP_GAUGE_STRING_LENGTH);
364 mGaugeString[DP_GAUGE_STRING_LENGTH] = 0;
365 FreePool (NameString);
366 return;
367 }
368 }
369 }
370
371 //
372 // Method 6: Unknown Driver Name
373 //
374 StringPtr = HiiGetString (mDpHiiHandle, STRING_TOKEN (STR_DP_ERROR_NAME), NULL);
375 ASSERT (StringPtr != NULL);
376 StrnCpyS (mGaugeString, DP_GAUGE_STRING_LENGTH + 1, StringPtr, DP_GAUGE_STRING_LENGTH);
377 FreePool (StringPtr);
378 }
379
380 /**
381 Calculate the Duration in microseconds.
382
383 Duration is multiplied by 1000, instead of Frequency being divided by 1000 or
384 multiplying the result by 1000, in order to maintain precision. Since Duration is
385 a 64-bit value, multiplying it by 1000 is unlikely to produce an overflow.
386
387 The time is calculated as (Duration * 1000) / Timer_Frequency.
388
389 @param[in] Duration The event duration in timer ticks.
390
391 @return A 64-bit value which is the Elapsed time in microseconds.
392 **/
393 UINT64
394 DurationInMicroSeconds (
395 IN UINT64 Duration
396 )
397 {
398 return DivU64x32 (Duration, 1000);
399 }
400
401 /**
402 Get index of Measurement Record's match in the CumData array.
403
404 If the Measurement's Token value matches a Token in one of the CumData
405 records, the index of the matching record is returned. The returned
406 index is a signed value so that negative values can indicate that
407 the Measurement didn't match any entry in the CumData array.
408
409 @param[in] Measurement A pointer to a Measurement Record to match against the CumData array.
410
411 @retval <0 Token is not in the CumData array.
412 @retval >=0 Return value is the index into CumData where Token is found.
413 **/
414 INTN
415 GetCumulativeItem(
416 IN MEASUREMENT_RECORD *Measurement
417 )
418 {
419 INTN Index;
420
421 for( Index = 0; Index < (INTN)NumCum; ++Index) {
422 if (AsciiStrCmp (Measurement->Token, CumData[Index].Name) == 0) {
423 return Index; // Exit, we found a match
424 }
425 }
426 // If the for loop exits, Token was not found.
427 return -1; // Indicate failure
428 }