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