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