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