TianoCore EDK2 master
Loading...
Searching...
No Matches
DpUtilities.c
Go to the documentation of this file.
1
9#include <Library/BaseLib.h>
12#include <Library/DebugLib.h>
15#include <Library/PrintLib.h>
16#include <Library/HiiLib.h>
17#include <Library/PcdLib.h>
18#include <Library/UefiLib.h>
21
22#include <Pi/PiFirmwareFile.h>
24
28#include <Protocol/DevicePath.h>
29
30#include <Guid/Performance.h>
31
32#include "Dp.h"
33#include "Literals.h"
34#include "DpInternal.h"
35
56UINT64
58 IN OUT MEASUREMENT_RECORD *Measurement
59 )
60{
61 UINT64 Duration;
62 BOOLEAN Error;
63
64 if (Measurement->EndTimeStamp == 0) {
65 return 0;
66 }
67
68 Duration = Measurement->EndTimeStamp - Measurement->StartTimeStamp;
69 Error = (BOOLEAN)(Duration > Measurement->EndTimeStamp);
70
71 if (Error) {
72 DEBUG ((DEBUG_ERROR, ALit_TimerLibError));
73 Duration = 0;
74 }
75
76 return Duration;
77}
78
90BOOLEAN
92 IN MEASUREMENT_RECORD *Measurement
93 )
94{
95 BOOLEAN RetVal;
96
97 RetVal = (BOOLEAN)(
98 ((AsciiStrCmp (Measurement->Token, ALit_SEC) == 0) ||
99 (AsciiStrCmp (Measurement->Token, ALit_PEI) == 0) ||
100 (AsciiStrCmp (Measurement->Token, ALit_DXE) == 0) ||
101 (AsciiStrCmp (Measurement->Token, ALit_BDS) == 0))
102 );
103 return RetVal;
104}
105
115BOOLEAN
117 IN MEASUREMENT_RECORD *Measurement
118 )
119{
120 BOOLEAN RetVal;
121
122 RetVal = (BOOLEAN)(
123 ((Measurement->Identifier == MODULE_START_ID) ||
124 (Measurement->Identifier == MODULE_END_ID) ||
125 (Measurement->Identifier == MODULE_LOADIMAGE_START_ID) ||
126 (Measurement->Identifier == MODULE_LOADIMAGE_END_ID) ||
127 (Measurement->Identifier == MODULE_DB_START_ID) ||
128 (Measurement->Identifier == MODULE_DB_END_ID) ||
129 (Measurement->Identifier == MODULE_DB_SUPPORT_START_ID) ||
130 (Measurement->Identifier == MODULE_DB_SUPPORT_END_ID) ||
131 (Measurement->Identifier == MODULE_DB_STOP_START_ID) ||
132 (Measurement->Identifier == MODULE_DB_STOP_START_ID))
133 );
134 return RetVal;
135}
136
149VOID
151 IN CHAR8 *PdbFileName,
152 OUT CHAR16 *UnicodeBuffer
153 )
154{
155 UINTN IndexA; // Current work location within an ASCII string.
156 UINTN IndexU; // Current work location within a Unicode string.
157 UINTN StartIndex;
158 UINTN EndIndex;
159
160 ZeroMem (UnicodeBuffer, (DP_GAUGE_STRING_LENGTH + 1) * sizeof (CHAR16));
161
162 if (PdbFileName == NULL) {
163 StrnCpyS (UnicodeBuffer, DP_GAUGE_STRING_LENGTH + 1, L" ", 1);
164 } else {
165 StartIndex = 0;
166 for (EndIndex = 0; PdbFileName[EndIndex] != 0; EndIndex++) {
167 }
168
169 for (IndexA = 0; PdbFileName[IndexA] != 0; IndexA++) {
170 if ((PdbFileName[IndexA] == '\\') || (PdbFileName[IndexA] == '/')) {
171 StartIndex = IndexA + 1;
172 }
173
174 if (PdbFileName[IndexA] == '.') {
175 EndIndex = IndexA;
176 }
177 }
178
179 IndexU = 0;
180 for (IndexA = StartIndex; IndexA < EndIndex; IndexA++) {
181 UnicodeBuffer[IndexU] = (CHAR16)PdbFileName[IndexA];
182 IndexU++;
183 if (IndexU >= DP_GAUGE_STRING_LENGTH) {
184 UnicodeBuffer[DP_GAUGE_STRING_LENGTH] = 0;
185 break;
186 }
187 }
188 }
189}
190
207VOID
209 IN EFI_HANDLE Handle
210 )
211{
212 EFI_STATUS Status;
214 CHAR8 *PdbFileName;
215 EFI_DRIVER_BINDING_PROTOCOL *DriverBinding;
216 EFI_STRING StringPtr;
217 EFI_DEVICE_PATH_PROTOCOL *LoadedImageDevicePath;
218 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
219 EFI_GUID *NameGuid;
220 CHAR16 *NameString;
221 UINTN StringSize;
222 CHAR8 *PlatformLanguage;
223 CHAR8 *BestLanguage;
224 EFI_COMPONENT_NAME2_PROTOCOL *ComponentName2;
225
226 Image = NULL;
227 LoadedImageDevicePath = NULL;
228 DevicePath = NULL;
229
230 //
231 // Method 1: Get the name string from image PDB
232 //
233 Status = gBS->HandleProtocol (
234 Handle,
235 &gEfiLoadedImageProtocolGuid,
236 (VOID **)&Image
237 );
238
239 if (EFI_ERROR (Status)) {
240 Status = gBS->OpenProtocol (
241 Handle,
242 &gEfiDriverBindingProtocolGuid,
243 (VOID **)&DriverBinding,
244 NULL,
245 NULL,
246 EFI_OPEN_PROTOCOL_GET_PROTOCOL
247 );
248 if (!EFI_ERROR (Status)) {
249 Status = gBS->HandleProtocol (
250 DriverBinding->ImageHandle,
251 &gEfiLoadedImageProtocolGuid,
252 (VOID **)&Image
253 );
254 }
255 }
256
257 if (!EFI_ERROR (Status)) {
258 PdbFileName = PeCoffLoaderGetPdbPointer (Image->ImageBase);
259
260 if (PdbFileName != NULL) {
261 DpGetShortPdbFileName (PdbFileName, mGaugeString);
262 return;
263 }
264 }
265
266 //
267 // Method 2: Get the name string from ComponentName2 protocol
268 //
269 Status = gBS->HandleProtocol (
270 Handle,
271 &gEfiComponentName2ProtocolGuid,
272 (VOID **)&ComponentName2
273 );
274 if (!EFI_ERROR (Status)) {
275 //
276 // Firstly use platform language setting, secondly use driver's first supported language.
277 //
278 GetVariable2 (L"PlatformLang", &gEfiGlobalVariableGuid, (VOID **)&PlatformLanguage, NULL);
279 BestLanguage = GetBestLanguage (
280 ComponentName2->SupportedLanguages,
281 FALSE,
282 (PlatformLanguage != NULL) ? PlatformLanguage : "",
283 ComponentName2->SupportedLanguages,
284 NULL
285 );
286 SHELL_FREE_NON_NULL (PlatformLanguage);
287
288 Status = ComponentName2->GetDriverName (
289 ComponentName2,
290 BestLanguage != NULL ? BestLanguage : "en-US",
291 &StringPtr
292 );
293 if (!EFI_ERROR (Status)) {
294 SHELL_FREE_NON_NULL (BestLanguage);
295 StrnCpyS (mGaugeString, DP_GAUGE_STRING_LENGTH + 1, StringPtr, DP_GAUGE_STRING_LENGTH);
296 mGaugeString[DP_GAUGE_STRING_LENGTH] = 0;
297 return;
298 }
299 }
300
301 Status = gBS->HandleProtocol (
302 Handle,
303 &gEfiLoadedImageDevicePathProtocolGuid,
304 (VOID **)&LoadedImageDevicePath
305 );
306 if (!EFI_ERROR (Status) && (LoadedImageDevicePath != NULL)) {
307 DevicePath = LoadedImageDevicePath;
308 } else if (Image != NULL) {
309 DevicePath = Image->FilePath;
310 }
311
312 if (DevicePath != NULL) {
313 //
314 // Try to get image GUID from image DevicePath
315 //
316 NameGuid = NULL;
317 while (!IsDevicePathEndType (DevicePath)) {
319 if (NameGuid != NULL) {
320 break;
321 }
322
323 DevicePath = NextDevicePathNode (DevicePath);
324 }
325
326 if (NameGuid != NULL) {
327 //
328 // Try to get the image's FFS UI section by image GUID
329 //
330 NameString = NULL;
331 StringSize = 0;
332 Status = GetSectionFromAnyFv (
333 NameGuid,
334 EFI_SECTION_USER_INTERFACE,
335 0,
336 (VOID **)&NameString,
337 &StringSize
338 );
339
340 if (!EFI_ERROR (Status)) {
341 //
342 // Method 3. Get the name string from FFS UI section
343 //
344 StrnCpyS (mGaugeString, DP_GAUGE_STRING_LENGTH + 1, NameString, DP_GAUGE_STRING_LENGTH);
345 mGaugeString[DP_GAUGE_STRING_LENGTH] = 0;
346 FreePool (NameString);
347 } else {
348 //
349 // Method 4: Get the name string from image GUID
350 //
351 UnicodeSPrint (mGaugeString, sizeof (mGaugeString), L"%g", NameGuid);
352 }
353
354 return;
355 } else {
356 //
357 // Method 5: Get the name string from image DevicePath
358 //
359 NameString = ConvertDevicePathToText (DevicePath, TRUE, FALSE);
360 if (NameString != NULL) {
361 StrnCpyS (mGaugeString, DP_GAUGE_STRING_LENGTH + 1, NameString, DP_GAUGE_STRING_LENGTH);
362 mGaugeString[DP_GAUGE_STRING_LENGTH] = 0;
363 FreePool (NameString);
364 return;
365 }
366 }
367 }
368
369 //
370 // Method 6: Unknown Driver Name
371 //
372 StringPtr = HiiGetString (mDpHiiHandle, STRING_TOKEN (STR_DP_ERROR_NAME), NULL);
373 ASSERT (StringPtr != NULL);
374 StrnCpyS (mGaugeString, DP_GAUGE_STRING_LENGTH + 1, StringPtr, DP_GAUGE_STRING_LENGTH);
375 FreePool (StringPtr);
376}
377
391UINT64
393 IN UINT64 Duration
394 )
395{
396 return DivU64x32 (Duration, 1000);
397}
398
412INTN
414 IN MEASUREMENT_RECORD *Measurement
415 )
416{
417 INTN Index;
418
419 for ( Index = 0; Index < (INTN)NumCum; ++Index) {
420 if (AsciiStrCmp (Measurement->Token, CumData[Index].Name) == 0) {
421 return Index; // Exit, we found a match
422 }
423 }
424
425 // If the for loop exits, Token was not found.
426 return -1; // Indicate failure
427}
UINT64 UINTN
INT64 INTN
UINT64 EFIAPI DivU64x32(IN UINT64 Dividend, IN UINT32 Divisor)
Definition: DivU64x32.c:29
INTN EFIAPI AsciiStrCmp(IN CONST CHAR8 *FirstString, IN CONST CHAR8 *SecondString)
Definition: String.c:716
RETURN_STATUS EFIAPI StrnCpyS(OUT CHAR16 *Destination, IN UINTN DestMax, IN CONST CHAR16 *Source, IN UINTN Length)
Definition: SafeString.c:310
VOID *EFIAPI ZeroMem(OUT VOID *Buffer, IN UINTN Length)
EFI_DEVICE_PATH_PROTOCOL *EFIAPI NextDevicePathNode(IN CONST VOID *Node)
CHAR16 *EFIAPI ConvertDevicePathToText(IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath, IN BOOLEAN DisplayOnly, IN BOOLEAN AllowShortcuts)
BOOLEAN EFIAPI IsDevicePathEndType(IN CONST VOID *Node)
PERF_CUM_DATA CumData[]
Items for which to gather cumulative statistics.
Definition: Dp.c:67
CHAR16 mGaugeString[DP_GAUGE_STRING_LENGTH+1]
Definition: Dp.c:52
UINT32 const NumCum
Number of items for which we are gathering cumulative statistics.
Definition: Dp.c:76
EFI_HII_HANDLE mDpHiiHandle
Definition: Dp.c:39
INTN GetCumulativeItem(IN MEASUREMENT_RECORD *Measurement)
Definition: DpUtilities.c:413
VOID DpGetShortPdbFileName(IN CHAR8 *PdbFileName, OUT CHAR16 *UnicodeBuffer)
Definition: DpUtilities.c:150
BOOLEAN IsPhase(IN MEASUREMENT_RECORD *Measurement)
Definition: DpUtilities.c:91
VOID DpGetNameFromHandle(IN EFI_HANDLE Handle)
Definition: DpUtilities.c:208
UINT64 GetDuration(IN OUT MEASUREMENT_RECORD *Measurement)
Definition: DpUtilities.c:57
BOOLEAN IsCorePerf(IN MEASUREMENT_RECORD *Measurement)
Definition: DpUtilities.c:116
UINT64 DurationInMicroSeconds(IN UINT64 Duration)
Definition: DpUtilities.c:392
EFI_STATUS EFIAPI GetSectionFromAnyFv(IN CONST EFI_GUID *NameGuid, IN EFI_SECTION_TYPE SectionType, IN UINTN SectionInstance, OUT VOID **Buffer, OUT UINTN *Size)
VOID EFIAPI FreePool(IN VOID *Buffer)
EFI_STRING EFIAPI HiiGetString(IN EFI_HII_HANDLE HiiHandle, IN EFI_STRING_ID StringId, IN CONST CHAR8 *Language OPTIONAL)
Definition: HiiString.c:211
UINTN EFIAPI UnicodeSPrint(OUT CHAR16 *StartOfBuffer, IN UINTN BufferSize, IN CONST CHAR16 *FormatString,...)
Definition: PrintLib.c:408
#define NULL
Definition: Base.h:319
#define TRUE
Definition: Base.h:301
#define FALSE
Definition: Base.h:307
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
#define DEBUG(Expression)
Definition: DebugLib.h:434
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
VOID * EFI_HANDLE
Definition: UefiBaseType.h:33
EFI_BOOT_SERVICES * gBS
#define STRING_TOKEN(t)
EFI_STATUS EFIAPI GetVariable2(IN CONST CHAR16 *Name, IN CONST EFI_GUID *Guid, OUT VOID **Value, OUT UINTN *Size OPTIONAL)
Definition: UefiLib.c:1317
CHAR8 *EFIAPI GetBestLanguage(IN CONST CHAR8 *SupportedLanguages, IN UINTN Iso639Language,...)
Definition: UefiLib.c:1522
EFI_GUID *EFIAPI EfiGetNameGuidFromFwVolDevicePathNode(IN CONST MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *FvDevicePathNode)
Definition: UefiNotTiano.c:292
EFI_DEVICE_PATH_PROTOCOL * FilePath
Definition: LoadedImage.h:54
VOID * ImageBase
The base address at which the image was loaded.
Definition: LoadedImage.h:67
Definition: Base.h:213
CHAR8 * Name
ASCII name of this item.
Definition: Dp.h:81