]> git.proxmox.com Git - mirror_edk2.git/blame - PerformancePkg/Dp_App/DpUtilities.c
Refine the select language logic.
[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
863986b3 4 Copyright (c) 2009 - 2013, 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
159 ZeroMem (UnicodeBuffer, DXE_PERFORMANCE_STRING_LENGTH * sizeof (CHAR16));\r
160\r
161 if (PdbFileName == NULL) {\r
162 StrCpy (UnicodeBuffer, L" ");\r
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
181 if (IndexU >= DXE_PERFORMANCE_STRING_LENGTH) {\r
182 UnicodeBuffer[DXE_PERFORMANCE_STRING_LENGTH] = 0;\r
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
10516d29
ED
224 BestLanguage = NULL;\r
225 PlatformLanguage = NULL;\r
226\r
a2daf8db
SZ
227 //\r
228 // Method 1: Get the name string from image PDB\r
c06ad33e 229 //\r
230 Status = gBS->HandleProtocol (\r
a2daf8db
SZ
231 Handle,\r
232 &gEfiLoadedImageProtocolGuid,\r
233 (VOID **) &Image\r
234 );\r
c06ad33e 235\r
236 if (EFI_ERROR (Status)) {\r
237 Status = gBS->OpenProtocol (\r
a2daf8db
SZ
238 Handle,\r
239 &gEfiDriverBindingProtocolGuid,\r
240 (VOID **) &DriverBinding,\r
241 NULL,\r
242 NULL,\r
243 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
244 );\r
245 if (!EFI_ERROR (Status)) {\r
246 Status = gBS->HandleProtocol (\r
247 DriverBinding->ImageHandle,\r
248 &gEfiLoadedImageProtocolGuid,\r
249 (VOID **) &Image\r
250 );\r
251 }\r
252 }\r
253\r
254 if (!EFI_ERROR (Status)) {\r
255 PdbFileName = PeCoffLoaderGetPdbPointer (Image->ImageBase);\r
256\r
257 if (PdbFileName != NULL) {\r
258 GetShortPdbFileName (PdbFileName, mGaugeString);\r
259 return;\r
260 }\r
261 }\r
262\r
263 //\r
264 // Method 2: Get the name string from ComponentName2 protocol\r
265 //\r
266 Status = gBS->HandleProtocol (\r
c06ad33e 267 Handle,\r
a2daf8db
SZ
268 &gEfiComponentName2ProtocolGuid,\r
269 (VOID **) &ComponentName2\r
c06ad33e 270 );\r
a2daf8db
SZ
271 if (!EFI_ERROR (Status)) {\r
272 //\r
273 // Get the current platform language setting\r
274 //\r
f01b91ae 275 GetEfiGlobalVariable2 (L"PlatformLang", (VOID**)&PlatformLanguage, NULL);\r
10516d29
ED
276\r
277 BestLanguage = GetBestLanguage(\r
278 ComponentName2->SupportedLanguages,\r
279 FALSE,\r
280 PlatformLanguage,\r
281 ComponentName2->SupportedLanguages,\r
282 NULL\r
283 );\r
284\r
285 SafeFreePool (PlatformLanguage);\r
a2daf8db
SZ
286 Status = ComponentName2->GetDriverName (\r
287 ComponentName2,\r
10516d29 288 BestLanguage,\r
a2daf8db
SZ
289 &StringPtr\r
290 );\r
10516d29 291 SafeFreePool (BestLanguage);\r
a2daf8db 292 if (!EFI_ERROR (Status)) {\r
a2daf8db
SZ
293 StrnCpy (mGaugeString, StringPtr, DP_GAUGE_STRING_LENGTH);\r
294 mGaugeString[DP_GAUGE_STRING_LENGTH] = 0;\r
295 return;\r
c06ad33e 296 }\r
a2daf8db 297 }\r
c06ad33e 298\r
a2daf8db
SZ
299 Status = gBS->HandleProtocol (\r
300 Handle,\r
301 &gEfiLoadedImageDevicePathProtocolGuid,\r
302 (VOID **) &LoadedImageDevicePath\r
c06ad33e 303 );\r
7ea7eee3 304 if (!EFI_ERROR (Status) && (LoadedImageDevicePath != NULL)) {\r
a2daf8db 305 DevicePath = LoadedImageDevicePath;\r
c06ad33e 306\r
a2daf8db
SZ
307 //\r
308 // Try to get image GUID from LoadedImageDevicePath protocol\r
309 //\r
310 NameGuid = NULL;\r
311 while (!IsDevicePathEndType (DevicePath)) {\r
312 NameGuid = EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) DevicePath);\r
313 if (NameGuid != NULL) {\r
314 break;\r
315 }\r
316 DevicePath = NextDevicePathNode (DevicePath);\r
317 }\r
c06ad33e 318\r
a2daf8db
SZ
319 if (NameGuid != NULL) {\r
320 //\r
321 // Try to get the image's FFS UI section by image GUID\r
322 //\r
323 NameString = NULL;\r
324 StringSize = 0;\r
325 Status = GetSectionFromAnyFv (\r
326 NameGuid,\r
327 EFI_SECTION_USER_INTERFACE,\r
328 0,\r
f7fe94ab 329 (VOID **) &NameString,\r
a2daf8db
SZ
330 &StringSize\r
331 );\r
332\r
333 if (!EFI_ERROR (Status)) {\r
334 //\r
335 // Method 3. Get the name string from FFS UI section\r
336 //\r
337 StrnCpy (mGaugeString, NameString, DP_GAUGE_STRING_LENGTH);\r
338 mGaugeString[DP_GAUGE_STRING_LENGTH] = 0;\r
339 FreePool (NameString);\r
340 } else {\r
341 //\r
342 // Method 4: Get the name string from image GUID\r
343 //\r
344 UnicodeSPrint (mGaugeString, sizeof (mGaugeString), L"%g", NameGuid);\r
345 }\r
346 return;\r
347 } else {\r
348 //\r
349 // Method 5: Get the name string from image DevicePath\r
350 //\r
863986b3
RN
351 NameString = ConvertDevicePathToText (LoadedImageDevicePath, TRUE, FALSE);\r
352 if (NameString != NULL) {\r
353 StrnCpy (mGaugeString, NameString, DP_GAUGE_STRING_LENGTH);\r
354 mGaugeString[DP_GAUGE_STRING_LENGTH] = 0;\r
355 FreePool (NameString);\r
356 return;\r
a2daf8db
SZ
357 }\r
358 }\r
c06ad33e 359 }\r
a2daf8db
SZ
360\r
361 //\r
362 // Method 6: Unknown Driver Name\r
363 //\r
364 StringPtr = HiiGetString (gHiiHandle, STRING_TOKEN (STR_DP_ERROR_NAME), NULL);\r
365 ASSERT (StringPtr != NULL);\r
366 StrCpy (mGaugeString, StringPtr);\r
88359546 367 FreePool (StringPtr);\r
a2daf8db 368 return;\r
c06ad33e 369}\r
370\r
9dd74618
ED
371/** \r
372 Calculate the Duration in microseconds.\r
373 \r
374 Duration is multiplied by 1000, instead of Frequency being divided by 1000 or\r
375 multiplying the result by 1000, in order to maintain precision. Since Duration is\r
376 a 64-bit value, multiplying it by 1000 is unlikely to produce an overflow.\r
377 \r
378 The time is calculated as (Duration * 1000) / Timer_Frequency.\r
379 \r
380 @param[in] Duration The event duration in timer ticks.\r
381 \r
382 @return A 64-bit value which is the Elapsed time in microseconds.\r
c06ad33e 383**/\r
384UINT64\r
385DurationInMicroSeconds (\r
386 IN UINT64 Duration\r
387 )\r
388{\r
389 UINT64 Temp;\r
390\r
391 Temp = MultU64x32 (Duration, 1000);\r
392 return DivU64x32 (Temp, TimerInfo.Frequency);\r
393}\r
394\r
9dd74618
ED
395/** \r
396 Formatted Print using a Hii Token to reference the localized format string.\r
397 \r
398 @param[in] Token A HII token associated with a localized Unicode string.\r
399 @param[in] ... The variable argument list.\r
400 \r
401 @return The number of characters converted by UnicodeVSPrint().\r
402 \r
c06ad33e 403**/\r
404UINTN\r
405PrintToken (\r
406 IN UINT16 Token,\r
407 ...\r
408 )\r
409{\r
410 VA_LIST Marker;\r
411 EFI_STRING StringPtr;\r
412 UINTN Return;\r
413 UINTN BufferSize;\r
414\r
415 StringPtr = HiiGetString (gHiiHandle, Token, NULL);\r
416 ASSERT (StringPtr != NULL);\r
417\r
418 VA_START (Marker, Token);\r
419\r
420 BufferSize = (PcdGet32 (PcdUefiLibMaxPrintBufferSize) + 1) * sizeof (CHAR16);\r
421\r
422 if (mPrintTokenBuffer == NULL) {\r
423 mPrintTokenBuffer = AllocatePool (BufferSize);\r
424 ASSERT (mPrintTokenBuffer != NULL);\r
425 }\r
426 SetMem( mPrintTokenBuffer, BufferSize, 0);\r
427\r
428 Return = UnicodeVSPrint (mPrintTokenBuffer, BufferSize, StringPtr, Marker);\r
3bbe68a3 429 VA_END (Marker);\r
430 \r
c06ad33e 431 if (Return > 0 && gST->ConOut != NULL) {\r
432 gST->ConOut->OutputString (gST->ConOut, mPrintTokenBuffer);\r
433 }\r
88359546 434 FreePool (StringPtr);\r
c06ad33e 435 return Return;\r
436}\r
437\r
9dd74618
ED
438/** \r
439 Get index of Measurement Record's match in the CumData array.\r
440 \r
441 If the Measurement's Token value matches a Token in one of the CumData\r
442 records, the index of the matching record is returned. The returned\r
443 index is a signed value so that negative values can indicate that\r
444 the Measurement didn't match any entry in the CumData array.\r
445 \r
446 @param[in] Measurement A pointer to a Measurement Record to match against the CumData array.\r
447 \r
448 @retval <0 Token is not in the CumData array.\r
449 @retval >=0 Return value is the index into CumData where Token is found.\r
c06ad33e 450**/\r
451INTN\r
452GetCumulativeItem(\r
453 IN MEASUREMENT_RECORD *Measurement\r
454 )\r
455{\r
456 INTN Index;\r
457\r
458 for( Index = 0; Index < (INTN)NumCum; ++Index) {\r
459 if (AsciiStrnCmp (Measurement->Token, CumData[Index].Name, PERF_TOKEN_LENGTH) == 0) {\r
460 return Index; // Exit, we found a match\r
461 }\r
462 }\r
463 // If the for loop exits, Token was not found.\r
464 return -1; // Indicate failure\r
465}\r