]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Library/DxeCorePerformanceLib/DxeCorePerformanceLib.c
IntelFsp2WrapperPkg: Apply uncrustify changes
[mirror_edk2.git] / MdeModulePkg / Library / DxeCorePerformanceLib / DxeCorePerformanceLib.c
1 /** @file
2 Performance library instance mainly used by DxeCore.
3
4 This library provides the performance measurement interfaces and initializes performance
5 logging for DXE phase. It first initializes its private global data structure for
6 performance logging and saves the performance GUIDed HOB passed from PEI phase.
7 It initializes DXE phase performance logging by publishing the Performance and PerformanceEx Protocol,
8 which are consumed by DxePerformanceLib to logging performance data in DXE phase.
9
10 This library is mainly used by DxeCore to start performance logging to ensure that
11 Performance Protocol is installed at the very beginning of DXE phase.
12
13 Copyright (c) 2006 - 2021, Intel Corporation. All rights reserved.<BR>
14 (C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>
15 SPDX-License-Identifier: BSD-2-Clause-Patent
16
17 **/
18
19
20 #include "DxeCorePerformanceLibInternal.h"
21
22 //
23 // Data for FPDT performance records.
24 //
25 #define SMM_BOOT_RECORD_COMM_SIZE (OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data) + sizeof(SMM_BOOT_RECORD_COMMUNICATE))
26 #define STRING_SIZE (FPDT_STRING_EVENT_RECORD_NAME_LENGTH * sizeof (CHAR8))
27 #define FIRMWARE_RECORD_BUFFER 0x10000
28 #define CACHE_HANDLE_GUID_COUNT 0x800
29
30 BOOT_PERFORMANCE_TABLE *mAcpiBootPerformanceTable = NULL;
31 BOOT_PERFORMANCE_TABLE mBootPerformanceTableTemplate = {
32 {
33 EFI_ACPI_5_0_FPDT_BOOT_PERFORMANCE_TABLE_SIGNATURE,
34 sizeof (BOOT_PERFORMANCE_TABLE)
35 },
36 {
37 {
38 EFI_ACPI_5_0_FPDT_RUNTIME_RECORD_TYPE_FIRMWARE_BASIC_BOOT, // Type
39 sizeof (EFI_ACPI_5_0_FPDT_FIRMWARE_BASIC_BOOT_RECORD), // Length
40 EFI_ACPI_5_0_FPDT_RUNTIME_RECORD_REVISION_FIRMWARE_BASIC_BOOT // Revision
41 },
42 0, // Reserved
43 //
44 // These values will be updated at runtime.
45 //
46 0, // ResetEnd
47 0, // OsLoaderLoadImageStart
48 0, // OsLoaderStartImageStart
49 0, // ExitBootServicesEntry
50 0 // ExitBootServicesExit
51 }
52 };
53
54 typedef struct {
55 EFI_HANDLE Handle;
56 CHAR8 NameString[FPDT_STRING_EVENT_RECORD_NAME_LENGTH];
57 EFI_GUID ModuleGuid;
58 } HANDLE_GUID_MAP;
59
60 HANDLE_GUID_MAP mCacheHandleGuidTable[CACHE_HANDLE_GUID_COUNT];
61 UINTN mCachePairCount = 0;
62
63 UINT32 mLoadImageCount = 0;
64 UINT32 mPerformanceLength = 0;
65 UINT32 mMaxPerformanceLength = 0;
66 UINT32 mBootRecordSize = 0;
67 UINTN mBootRecordMaxSize = 0;
68 UINT32 mCachedLength = 0;
69
70 BOOLEAN mFpdtBufferIsReported = FALSE;
71 BOOLEAN mLackSpaceIsReported = FALSE;
72 CHAR8 *mPlatformLanguage = NULL;
73 UINT8 *mPerformancePointer = NULL;
74 UINT8 *mBootRecordBuffer = NULL;
75 BOOLEAN mLockInsertRecord = FALSE;
76 CHAR8 *mDevicePathString = NULL;
77
78 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL *mDevicePathToText = NULL;
79
80 //
81 // Interfaces for PerformanceMeasurement Protocol.
82 //
83 EDKII_PERFORMANCE_MEASUREMENT_PROTOCOL mPerformanceMeasurementInterface = {
84 CreatePerformanceMeasurement,
85 };
86
87 PERFORMANCE_PROPERTY mPerformanceProperty;
88
89 /**
90 Return the pointer to the FPDT record in the allocated memory.
91
92 @param RecordSize The size of FPDT record.
93 @param FpdtRecordPtr Pointer the FPDT record in the allocated memory.
94
95 @retval EFI_SUCCESS Successfully get the pointer to the FPDT record.
96 @retval EFI_OUT_OF_RESOURCES Ran out of space to store the records.
97 **/
98 EFI_STATUS
99 GetFpdtRecordPtr (
100 IN UINT8 RecordSize,
101 IN OUT FPDT_RECORD_PTR *FpdtRecordPtr
102 )
103 {
104 if (mFpdtBufferIsReported) {
105 //
106 // Append Boot records to the boot performance table.
107 //
108 if (mBootRecordSize + RecordSize > mBootRecordMaxSize) {
109 if (!mLackSpaceIsReported) {
110 DEBUG ((DEBUG_INFO, "DxeCorePerformanceLib: No enough space to save boot records\n"));
111 mLackSpaceIsReported = TRUE;
112 }
113 return EFI_OUT_OF_RESOURCES;
114 } else {
115 //
116 // Save boot record into BootPerformance table
117 //
118 FpdtRecordPtr->RecordHeader = (EFI_ACPI_5_0_FPDT_PERFORMANCE_RECORD_HEADER *)(mBootRecordBuffer + mBootRecordSize);
119 }
120 } else {
121 //
122 // Check if pre-allocated buffer is full
123 //
124 if (mPerformanceLength + RecordSize > mMaxPerformanceLength) {
125 mPerformancePointer = ReallocatePool (
126 mPerformanceLength,
127 mPerformanceLength + RecordSize + FIRMWARE_RECORD_BUFFER,
128 mPerformancePointer
129 );
130 if (mPerformancePointer == NULL) {
131 return EFI_OUT_OF_RESOURCES;
132 }
133 mMaxPerformanceLength = mPerformanceLength + RecordSize + FIRMWARE_RECORD_BUFFER;
134 }
135 //
136 // Covert buffer to FPDT Ptr Union type.
137 //
138 FpdtRecordPtr->RecordHeader = (EFI_ACPI_5_0_FPDT_PERFORMANCE_RECORD_HEADER *)(mPerformancePointer + mPerformanceLength);
139 }
140 return EFI_SUCCESS;
141 }
142
143 /**
144 Check whether the Token is a known one which is uesed by core.
145
146 @param Token Pointer to a Null-terminated ASCII string
147
148 @retval TRUE Is a known one used by core.
149 @retval FALSE Not a known one.
150
151 **/
152 BOOLEAN
153 IsKnownTokens (
154 IN CONST CHAR8 *Token
155 )
156 {
157 if (Token == NULL) {
158 return FALSE;
159 }
160
161 if (AsciiStrCmp (Token, SEC_TOK) == 0 ||
162 AsciiStrCmp (Token, PEI_TOK) == 0 ||
163 AsciiStrCmp (Token, DXE_TOK) == 0 ||
164 AsciiStrCmp (Token, BDS_TOK) == 0 ||
165 AsciiStrCmp (Token, DRIVERBINDING_START_TOK) == 0 ||
166 AsciiStrCmp (Token, DRIVERBINDING_SUPPORT_TOK) == 0 ||
167 AsciiStrCmp (Token, DRIVERBINDING_STOP_TOK) == 0 ||
168 AsciiStrCmp (Token, LOAD_IMAGE_TOK) == 0 ||
169 AsciiStrCmp (Token, START_IMAGE_TOK) == 0 ||
170 AsciiStrCmp (Token, PEIM_TOK) == 0) {
171 return TRUE;
172 } else {
173 return FALSE;
174 }
175 }
176
177 /**
178 Check whether the ID is a known one which map to the known Token.
179
180 @param Identifier 32-bit identifier.
181
182 @retval TRUE Is a known one used by core.
183 @retval FALSE Not a known one.
184
185 **/
186 BOOLEAN
187 IsKnownID (
188 IN UINT32 Identifier
189 )
190 {
191 if (Identifier == MODULE_START_ID ||
192 Identifier == MODULE_END_ID ||
193 Identifier == MODULE_LOADIMAGE_START_ID ||
194 Identifier == MODULE_LOADIMAGE_END_ID ||
195 Identifier == MODULE_DB_START_ID ||
196 Identifier == MODULE_DB_END_ID ||
197 Identifier == MODULE_DB_SUPPORT_START_ID ||
198 Identifier == MODULE_DB_SUPPORT_END_ID ||
199 Identifier == MODULE_DB_STOP_START_ID ||
200 Identifier == MODULE_DB_STOP_END_ID) {
201 return TRUE;
202 } else {
203 return FALSE;
204 }
205 }
206
207 /**
208 This internal function dumps all the SMM performance data and size.
209
210 @param SmmPerfData Smm Performance data. The buffer contain the SMM perf data is allocated by this function and caller needs to free it.
211 @param SmmPerfDataSize Smm Performance data size.
212 @param SkipGetPerfData Skip to get performance data, just get the size.
213
214 **/
215 VOID
216 InternalGetSmmPerfData (
217 OUT VOID **SmmPerfData,
218 OUT UINTN *SmmPerfDataSize,
219 IN BOOLEAN SkipGetPerfData
220 )
221 {
222 EFI_STATUS Status;
223 UINT8 *SmmBootRecordCommBuffer;
224 EFI_SMM_COMMUNICATE_HEADER *SmmCommBufferHeader;
225 SMM_BOOT_RECORD_COMMUNICATE *SmmCommData;
226 UINTN CommSize;
227 EFI_SMM_COMMUNICATION_PROTOCOL *Communication;
228 EDKII_PI_SMM_COMMUNICATION_REGION_TABLE *SmmCommRegionTable;
229 EFI_MEMORY_DESCRIPTOR *SmmCommMemRegion;
230 UINTN Index;
231 VOID *SmmBootRecordData;
232 UINTN SmmBootRecordDataSize;
233 UINTN ReservedMemSize;
234
235 //
236 // Collect boot records from SMM drivers.
237 //
238 SmmBootRecordCommBuffer = NULL;
239 SmmCommData = NULL;
240 SmmBootRecordData = NULL;
241 ReservedMemSize = 0;
242 Status = gBS->LocateProtocol (&gEfiSmmCommunicationProtocolGuid, NULL, (VOID **) &Communication);
243 if (!EFI_ERROR (Status)) {
244 //
245 // Initialize communicate buffer
246 // Get the prepared Reserved Memory Range
247 //
248 Status = EfiGetSystemConfigurationTable (
249 &gEdkiiPiSmmCommunicationRegionTableGuid,
250 (VOID **) &SmmCommRegionTable
251 );
252 if (!EFI_ERROR (Status)) {
253 ASSERT (SmmCommRegionTable != NULL);
254 SmmCommMemRegion = (EFI_MEMORY_DESCRIPTOR *) (SmmCommRegionTable + 1);
255 for (Index = 0; Index < SmmCommRegionTable->NumberOfEntries; Index ++) {
256 if (SmmCommMemRegion->Type == EfiConventionalMemory) {
257 break;
258 }
259 SmmCommMemRegion = (EFI_MEMORY_DESCRIPTOR *) ((UINT8 *) SmmCommMemRegion + SmmCommRegionTable->DescriptorSize);
260 }
261 ASSERT (Index < SmmCommRegionTable->NumberOfEntries);
262 ASSERT (SmmCommMemRegion->PhysicalStart > 0);
263 ASSERT (SmmCommMemRegion->NumberOfPages > 0);
264 ReservedMemSize = (UINTN) SmmCommMemRegion->NumberOfPages * EFI_PAGE_SIZE;
265
266 //
267 // Check enough reserved memory space
268 //
269 if (ReservedMemSize > SMM_BOOT_RECORD_COMM_SIZE) {
270 SmmBootRecordCommBuffer = (VOID *) (UINTN) SmmCommMemRegion->PhysicalStart;
271 SmmCommBufferHeader = (EFI_SMM_COMMUNICATE_HEADER*)SmmBootRecordCommBuffer;
272 SmmCommData = (SMM_BOOT_RECORD_COMMUNICATE*)SmmCommBufferHeader->Data;
273 ZeroMem((UINT8*)SmmCommData, sizeof(SMM_BOOT_RECORD_COMMUNICATE));
274
275 CopyGuid (&SmmCommBufferHeader->HeaderGuid, &gEfiFirmwarePerformanceGuid);
276 SmmCommBufferHeader->MessageLength = sizeof(SMM_BOOT_RECORD_COMMUNICATE);
277 CommSize = SMM_BOOT_RECORD_COMM_SIZE;
278
279 //
280 // Get the size of boot records.
281 //
282 SmmCommData->Function = SMM_FPDT_FUNCTION_GET_BOOT_RECORD_SIZE;
283 SmmCommData->BootRecordData = NULL;
284 Status = Communication->Communicate (Communication, SmmBootRecordCommBuffer, &CommSize);
285
286 if (!EFI_ERROR (Status) && !EFI_ERROR (SmmCommData->ReturnStatus) && SmmCommData->BootRecordSize != 0) {
287 if (SkipGetPerfData) {
288 *SmmPerfDataSize = SmmCommData->BootRecordSize;
289 return;
290 }
291 //
292 // Get all boot records
293 //
294 SmmCommData->Function = SMM_FPDT_FUNCTION_GET_BOOT_RECORD_DATA_BY_OFFSET;
295 SmmBootRecordDataSize = SmmCommData->BootRecordSize;
296 SmmBootRecordData = AllocateZeroPool(SmmBootRecordDataSize);
297 ASSERT (SmmBootRecordData != NULL);
298 SmmCommData->BootRecordOffset = 0;
299 SmmCommData->BootRecordData = (VOID *) ((UINTN) SmmCommMemRegion->PhysicalStart + SMM_BOOT_RECORD_COMM_SIZE);
300 SmmCommData->BootRecordSize = ReservedMemSize - SMM_BOOT_RECORD_COMM_SIZE;
301 while (SmmCommData->BootRecordOffset < SmmBootRecordDataSize) {
302 Status = Communication->Communicate (Communication, SmmBootRecordCommBuffer, &CommSize);
303 ASSERT_EFI_ERROR (Status);
304 ASSERT_EFI_ERROR(SmmCommData->ReturnStatus);
305 if (SmmCommData->BootRecordOffset + SmmCommData->BootRecordSize > SmmBootRecordDataSize) {
306 CopyMem ((UINT8 *) SmmBootRecordData + SmmCommData->BootRecordOffset, SmmCommData->BootRecordData, SmmBootRecordDataSize - SmmCommData->BootRecordOffset);
307 } else {
308 CopyMem ((UINT8 *) SmmBootRecordData + SmmCommData->BootRecordOffset, SmmCommData->BootRecordData, SmmCommData->BootRecordSize);
309 }
310 SmmCommData->BootRecordOffset = SmmCommData->BootRecordOffset + SmmCommData->BootRecordSize;
311 }
312 *SmmPerfData = SmmBootRecordData;
313 *SmmPerfDataSize = SmmBootRecordDataSize;
314 }
315 }
316 }
317 }
318 }
319
320 /**
321 Allocate buffer for Boot Performance table.
322
323 @return Status code.
324
325 **/
326 EFI_STATUS
327 AllocateBootPerformanceTable (
328 VOID
329 )
330 {
331 EFI_STATUS Status;
332 UINTN Size;
333 UINTN BootPerformanceDataSize;
334 UINT8 *BootPerformanceData;
335 FIRMWARE_PERFORMANCE_VARIABLE PerformanceVariable;
336 UINTN SmmBootRecordDataSize;
337
338 SmmBootRecordDataSize = 0;
339
340 //
341 // Get SMM performance data size at the point of EndOfDxe in order to allocate the boot performance table.
342 // Will Get all the data at ReadyToBoot.
343 //
344 InternalGetSmmPerfData (NULL, &SmmBootRecordDataSize, TRUE);
345
346 //
347 // Prepare memory for Boot Performance table.
348 // Boot Performance table includes BasicBoot record, and one or more appended Boot Records.
349 //
350 BootPerformanceDataSize = sizeof (BOOT_PERFORMANCE_TABLE) + mPerformanceLength + SmmBootRecordDataSize + PcdGet32 (PcdExtFpdtBootRecordPadSize);
351
352 //
353 // Try to allocate the same runtime buffer as last time boot.
354 //
355 ZeroMem (&PerformanceVariable, sizeof (PerformanceVariable));
356 Size = sizeof (PerformanceVariable);
357 Status = gRT->GetVariable (
358 EFI_FIRMWARE_PERFORMANCE_VARIABLE_NAME,
359 &gEfiFirmwarePerformanceGuid,
360 NULL,
361 &Size,
362 &PerformanceVariable
363 );
364 if (!EFI_ERROR (Status)) {
365 Status = gBS->AllocatePages (
366 AllocateAddress,
367 EfiReservedMemoryType,
368 EFI_SIZE_TO_PAGES (BootPerformanceDataSize),
369 &PerformanceVariable.BootPerformanceTablePointer
370 );
371 if (!EFI_ERROR (Status)) {
372 mAcpiBootPerformanceTable = (BOOT_PERFORMANCE_TABLE *) (UINTN) PerformanceVariable.BootPerformanceTablePointer;
373 }
374 }
375
376 if (mAcpiBootPerformanceTable == NULL) {
377 //
378 // Fail to allocate at specified address, continue to allocate at any address.
379 //
380 mAcpiBootPerformanceTable = (BOOT_PERFORMANCE_TABLE *) AllocatePeiAccessiblePages (
381 EfiReservedMemoryType,
382 EFI_SIZE_TO_PAGES (BootPerformanceDataSize)
383 );
384 if (mAcpiBootPerformanceTable != NULL) {
385 ZeroMem (mAcpiBootPerformanceTable, BootPerformanceDataSize);
386 }
387 }
388 DEBUG ((DEBUG_INFO, "DxeCorePerformanceLib: ACPI Boot Performance Table address = 0x%x\n", mAcpiBootPerformanceTable));
389
390 if (mAcpiBootPerformanceTable == NULL) {
391 return EFI_OUT_OF_RESOURCES;
392 }
393
394 //
395 // Prepare Boot Performance Table.
396 //
397 BootPerformanceData = (UINT8 *) mAcpiBootPerformanceTable;
398 //
399 // Fill Basic Boot record to Boot Performance Table.
400 //
401 CopyMem (mAcpiBootPerformanceTable, &mBootPerformanceTableTemplate, sizeof (mBootPerformanceTableTemplate));
402 BootPerformanceData = BootPerformanceData + mAcpiBootPerformanceTable->Header.Length;
403 //
404 // Fill Boot records from boot drivers.
405 //
406 if (mPerformancePointer != NULL) {
407 CopyMem (BootPerformanceData, mPerformancePointer, mPerformanceLength);
408 mAcpiBootPerformanceTable->Header.Length += mPerformanceLength;
409 BootPerformanceData = BootPerformanceData + mPerformanceLength;
410 FreePool (mPerformancePointer);
411 mPerformancePointer = NULL;
412 mPerformanceLength = 0;
413 mMaxPerformanceLength = 0;
414 }
415
416 mBootRecordBuffer = (UINT8 *) mAcpiBootPerformanceTable;
417 mBootRecordSize = mAcpiBootPerformanceTable->Header.Length;
418 mBootRecordMaxSize = BootPerformanceDataSize;
419
420 return EFI_SUCCESS;
421 }
422
423 /**
424 Get a human readable module name and module guid for the given image handle.
425 If module name can't be found, "" string will return.
426 If module guid can't be found, Zero Guid will return.
427
428 @param Handle Image handle or Controller handle.
429 @param NameString The ascii string will be filled into it. If not found, null string will return.
430 @param BufferSize Size of the input NameString buffer.
431 @param ModuleGuid Point to the guid buffer to store the got module guid value.
432
433 @retval EFI_SUCCESS Successfully get module name and guid.
434 @retval EFI_INVALID_PARAMETER The input parameter NameString is NULL.
435 @retval other value Module Name can't be got.
436 **/
437 EFI_STATUS
438 GetModuleInfoFromHandle (
439 IN EFI_HANDLE Handle,
440 OUT CHAR8 *NameString,
441 IN UINTN BufferSize,
442 OUT EFI_GUID *ModuleGuid OPTIONAL
443 )
444 {
445 EFI_STATUS Status;
446 EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;
447 EFI_DRIVER_BINDING_PROTOCOL *DriverBinding;
448 CHAR8 *PdbFileName;
449 EFI_GUID *TempGuid;
450 UINTN StartIndex;
451 UINTN Index;
452 INTN Count;
453 BOOLEAN ModuleGuidIsGet;
454 UINTN StringSize;
455 CHAR16 *StringPtr;
456 EFI_COMPONENT_NAME2_PROTOCOL *ComponentName2;
457 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *FvFilePath;
458
459 if (NameString == NULL || BufferSize == 0) {
460 return EFI_INVALID_PARAMETER;
461 }
462 //
463 // Try to get the ModuleGuid and name string form the caached array.
464 //
465 if (mCachePairCount > 0) {
466 for (Count = mCachePairCount -1; Count >= 0; Count--) {
467 if (Handle == mCacheHandleGuidTable[Count].Handle) {
468 CopyGuid (ModuleGuid, &mCacheHandleGuidTable[Count].ModuleGuid);
469 AsciiStrCpyS (NameString, FPDT_STRING_EVENT_RECORD_NAME_LENGTH, mCacheHandleGuidTable[Count].NameString);
470 return EFI_SUCCESS;
471 }
472 }
473 }
474
475 Status = EFI_INVALID_PARAMETER;
476 LoadedImage = NULL;
477 ModuleGuidIsGet = FALSE;
478
479 //
480 // Initialize GUID as zero value.
481 //
482 TempGuid = &gZeroGuid;
483 //
484 // Initialize it as "" string.
485 //
486 NameString[0] = 0;
487
488 if (Handle != NULL) {
489 //
490 // Try Handle as ImageHandle.
491 //
492 Status = gBS->HandleProtocol (
493 Handle,
494 &gEfiLoadedImageProtocolGuid,
495 (VOID**) &LoadedImage
496 );
497
498 if (EFI_ERROR (Status)) {
499 //
500 // Try Handle as Controller Handle
501 //
502 Status = gBS->OpenProtocol (
503 Handle,
504 &gEfiDriverBindingProtocolGuid,
505 (VOID **) &DriverBinding,
506 NULL,
507 NULL,
508 EFI_OPEN_PROTOCOL_GET_PROTOCOL
509 );
510 if (!EFI_ERROR (Status)) {
511 //
512 // Get Image protocol from ImageHandle
513 //
514 Status = gBS->HandleProtocol (
515 DriverBinding->ImageHandle,
516 &gEfiLoadedImageProtocolGuid,
517 (VOID**) &LoadedImage
518 );
519 }
520 }
521 }
522
523 if (!EFI_ERROR (Status) && LoadedImage != NULL) {
524 //
525 // Get Module Guid from DevicePath.
526 //
527 if (LoadedImage->FilePath != NULL &&
528 LoadedImage->FilePath->Type == MEDIA_DEVICE_PATH &&
529 LoadedImage->FilePath->SubType == MEDIA_PIWG_FW_FILE_DP
530 ) {
531 //
532 // Determine GUID associated with module logging performance
533 //
534 ModuleGuidIsGet = TRUE;
535 FvFilePath = (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) LoadedImage->FilePath;
536 TempGuid = &FvFilePath->FvFileName;
537 }
538
539 //
540 // Method 1 Get Module Name from PDB string.
541 //
542 PdbFileName = PeCoffLoaderGetPdbPointer (LoadedImage->ImageBase);
543 if (PdbFileName != NULL && BufferSize > 0) {
544 StartIndex = 0;
545 for (Index = 0; PdbFileName[Index] != 0; Index++) {
546 if ((PdbFileName[Index] == '\\') || (PdbFileName[Index] == '/')) {
547 StartIndex = Index + 1;
548 }
549 }
550 //
551 // Copy the PDB file name to our temporary string.
552 // If the length is bigger than BufferSize, trim the redudant characters to avoid overflow in array boundary.
553 //
554 for (Index = 0; Index < BufferSize - 1; Index++) {
555 NameString[Index] = PdbFileName[Index + StartIndex];
556 if (NameString[Index] == 0 || NameString[Index] == '.') {
557 NameString[Index] = 0;
558 break;
559 }
560 }
561
562 if (Index == BufferSize - 1) {
563 NameString[Index] = 0;
564 }
565 //
566 // Module Name is got.
567 //
568 goto Done;
569 }
570 }
571
572 //
573 // Method 2: Get the name string from ComponentName2 protocol
574 //
575 Status = gBS->HandleProtocol (
576 Handle,
577 &gEfiComponentName2ProtocolGuid,
578 (VOID **) &ComponentName2
579 );
580 if (!EFI_ERROR (Status)) {
581 //
582 // Get the current platform language setting
583 //
584 if (mPlatformLanguage == NULL) {
585 GetEfiGlobalVariable2 (L"PlatformLang", (VOID **) &mPlatformLanguage, NULL);
586 }
587 if (mPlatformLanguage != NULL) {
588 Status = ComponentName2->GetDriverName (
589 ComponentName2,
590 mPlatformLanguage != NULL ? mPlatformLanguage : "en-US",
591 &StringPtr
592 );
593 if (!EFI_ERROR (Status)) {
594 for (Index = 0; Index < BufferSize - 1 && StringPtr[Index] != 0; Index++) {
595 NameString[Index] = (CHAR8) StringPtr[Index];
596 }
597 NameString[Index] = 0;
598 //
599 // Module Name is got.
600 //
601 goto Done;
602 }
603 }
604 }
605
606 if (ModuleGuidIsGet) {
607 //
608 // Method 3 Try to get the image's FFS UI section by image GUID
609 //
610 StringPtr = NULL;
611 StringSize = 0;
612 Status = GetSectionFromAnyFv (
613 TempGuid,
614 EFI_SECTION_USER_INTERFACE,
615 0,
616 (VOID **) &StringPtr,
617 &StringSize
618 );
619
620 if (!EFI_ERROR (Status)) {
621 //
622 // Method 3. Get the name string from FFS UI section
623 //
624 for (Index = 0; Index < BufferSize - 1 && StringPtr[Index] != 0; Index++) {
625 NameString[Index] = (CHAR8) StringPtr[Index];
626 }
627 NameString[Index] = 0;
628 FreePool (StringPtr);
629 }
630 }
631
632 Done:
633 //
634 // Copy Module Guid
635 //
636 if (ModuleGuid != NULL) {
637 CopyGuid (ModuleGuid, TempGuid);
638 if (IsZeroGuid(TempGuid) && (Handle != NULL) && !ModuleGuidIsGet) {
639 // Handle is GUID
640 CopyGuid (ModuleGuid, (EFI_GUID *) Handle);
641 }
642 }
643
644 //
645 // Cache the Handle and Guid pairs.
646 //
647 if (mCachePairCount < CACHE_HANDLE_GUID_COUNT) {
648 mCacheHandleGuidTable[mCachePairCount].Handle = Handle;
649 CopyGuid (&mCacheHandleGuidTable[mCachePairCount].ModuleGuid, ModuleGuid);
650 AsciiStrCpyS (mCacheHandleGuidTable[mCachePairCount].NameString, FPDT_STRING_EVENT_RECORD_NAME_LENGTH, NameString);
651 mCachePairCount ++;
652 }
653
654 return Status;
655 }
656
657 /**
658 Get the FPDT record identifier.
659
660 @param Attribute The attribute of the Record.
661 PerfStartEntry: Start Record.
662 PerfEndEntry: End Record.
663 @param Handle Pointer to environment specific context used to identify the component being measured.
664 @param String Pointer to a Null-terminated ASCII string that identifies the component being measured.
665 @param ProgressID On return, pointer to the ProgressID.
666
667 @retval EFI_SUCCESS Get record info successfully.
668 @retval EFI_INVALID_PARAMETER No matched FPDT record.
669
670 **/
671 EFI_STATUS
672 GetFpdtRecordId (
673 IN PERF_MEASUREMENT_ATTRIBUTE Attribute,
674 IN CONST VOID *Handle,
675 IN CONST CHAR8 *String,
676 OUT UINT16 *ProgressID
677 )
678 {
679 //
680 // Token to PerfId.
681 //
682 if (String != NULL) {
683 if (AsciiStrCmp (String, START_IMAGE_TOK) == 0) { // "StartImage:"
684 if (Attribute == PerfStartEntry) {
685 *ProgressID = MODULE_START_ID;
686 } else {
687 *ProgressID = MODULE_END_ID;
688 }
689 } else if (AsciiStrCmp (String, LOAD_IMAGE_TOK) == 0) { // "LoadImage:"
690 if (Attribute == PerfStartEntry) {
691 *ProgressID = MODULE_LOADIMAGE_START_ID;
692 } else {
693 *ProgressID = MODULE_LOADIMAGE_END_ID;
694 }
695 } else if (AsciiStrCmp (String, DRIVERBINDING_START_TOK) == 0) { // "DB:Start:"
696 if (Attribute == PerfStartEntry) {
697 *ProgressID = MODULE_DB_START_ID;
698 } else {
699 *ProgressID = MODULE_DB_END_ID;
700 }
701 } else if (AsciiStrCmp (String, DRIVERBINDING_SUPPORT_TOK) == 0) { // "DB:Support:"
702 if (PcdGetBool (PcdEdkiiFpdtStringRecordEnableOnly)) {
703 return RETURN_UNSUPPORTED;
704 }
705 if (Attribute == PerfStartEntry) {
706 *ProgressID = MODULE_DB_SUPPORT_START_ID;
707 } else {
708 *ProgressID = MODULE_DB_SUPPORT_END_ID;
709 }
710 } else if (AsciiStrCmp (String, DRIVERBINDING_STOP_TOK) == 0) { // "DB:Stop:"
711 if (PcdGetBool (PcdEdkiiFpdtStringRecordEnableOnly)) {
712 return RETURN_UNSUPPORTED;
713 }
714 if (Attribute == PerfStartEntry) {
715 *ProgressID = MODULE_DB_STOP_START_ID;
716 } else {
717 *ProgressID = MODULE_DB_STOP_END_ID;
718 }
719 } else if (AsciiStrCmp (String, PEI_TOK) == 0 || // "PEI"
720 AsciiStrCmp (String, DXE_TOK) == 0 || // "DXE"
721 AsciiStrCmp (String, BDS_TOK) == 0) { // "BDS"
722 if (Attribute == PerfStartEntry) {
723 *ProgressID = PERF_CROSSMODULE_START_ID;
724 } else {
725 *ProgressID = PERF_CROSSMODULE_END_ID;
726 }
727 } else { // Pref used in Modules.
728 if (Attribute == PerfStartEntry) {
729 *ProgressID = PERF_INMODULE_START_ID;
730 } else {
731 *ProgressID = PERF_INMODULE_END_ID;
732 }
733 }
734 } else if (Handle!= NULL) { // Pref used in Modules.
735 if (Attribute == PerfStartEntry) {
736 *ProgressID = PERF_INMODULE_START_ID;
737 } else {
738 *ProgressID = PERF_INMODULE_END_ID;
739 }
740 } else {
741 return EFI_INVALID_PARAMETER;
742 }
743 return EFI_SUCCESS;
744 }
745
746 /**
747 Copies the string from Source into Destination and updates Length with the
748 size of the string.
749
750 @param Destination - destination of the string copy
751 @param Source - pointer to the source string which will get copied
752 @param Length - pointer to a length variable to be updated
753
754 **/
755 VOID
756 CopyStringIntoPerfRecordAndUpdateLength (
757 IN OUT CHAR8 *Destination,
758 IN CONST CHAR8 *Source,
759 IN OUT UINT8 *Length
760 )
761 {
762 UINTN StringLen;
763 UINTN DestMax;
764
765 ASSERT (Source != NULL);
766
767 if (PcdGetBool (PcdEdkiiFpdtStringRecordEnableOnly)) {
768 DestMax = STRING_SIZE;
769 } else {
770 DestMax = AsciiStrSize (Source);
771 if (DestMax > STRING_SIZE) {
772 DestMax = STRING_SIZE;
773 }
774 }
775 StringLen = AsciiStrLen (Source);
776 if (StringLen >= DestMax) {
777 StringLen = DestMax -1;
778 }
779
780 AsciiStrnCpyS(Destination, DestMax, Source, StringLen);
781 *Length += (UINT8)DestMax;
782
783 return;
784 }
785
786 /**
787 Get a string description for device for the given controller handle and update record
788 length. If ComponentName2 GetControllerName is supported, the value is included in the string,
789 followed by device path, otherwise just device path.
790
791 @param Handle - Image handle
792 @param ControllerHandle - Controller handle.
793 @param ComponentNameString - Pointer to a location where the string will be saved
794 @param Length - Pointer to record length to be updated
795
796 @retval EFI_SUCCESS - Successfully got string description for device
797 @retval EFI_UNSUPPORTED - Neither ComponentName2 ControllerName nor DevicePath were found
798
799 **/
800 EFI_STATUS
801 GetDeviceInfoFromHandleAndUpdateLength (
802 IN CONST VOID *Handle,
803 IN EFI_HANDLE ControllerHandle,
804 OUT CHAR8 *ComponentNameString,
805 IN OUT UINT8 *Length
806 )
807 {
808 EFI_DEVICE_PATH_PROTOCOL *DevicePathProtocol;
809 EFI_COMPONENT_NAME2_PROTOCOL *ComponentName2;
810 EFI_STATUS Status;
811 CHAR16 *StringPtr;
812 CHAR8 *AsciiStringPtr;
813 UINTN ControllerNameStringSize;
814 UINTN DevicePathStringSize;
815
816 ControllerNameStringSize = 0;
817
818 Status = gBS->HandleProtocol (
819 (EFI_HANDLE) Handle,
820 &gEfiComponentName2ProtocolGuid,
821 (VOID **) &ComponentName2
822 );
823
824 if (!EFI_ERROR(Status)) {
825 //
826 // Get the current platform language setting
827 //
828 if (mPlatformLanguage == NULL) {
829 GetEfiGlobalVariable2 (L"PlatformLang", (VOID **)&mPlatformLanguage, NULL);
830 }
831
832 Status = ComponentName2->GetControllerName (
833 ComponentName2,
834 ControllerHandle,
835 NULL,
836 mPlatformLanguage != NULL ? mPlatformLanguage : "en-US",
837 &StringPtr
838 );
839 }
840
841 if (!EFI_ERROR (Status)) {
842 //
843 // This will produce the size of the unicode string, which is twice as large as the ASCII one
844 // This must be an even number, so ok to divide by 2
845 //
846 ControllerNameStringSize = StrSize(StringPtr) / 2;
847
848 //
849 // The + 1 is because we want to add a space between the ControllerName and the device path
850 //
851 if ((ControllerNameStringSize + (*Length) + 1) > FPDT_MAX_PERF_RECORD_SIZE) {
852 //
853 // Only copy enough to fill FPDT_MAX_PERF_RECORD_SIZE worth of the record
854 //
855 ControllerNameStringSize = FPDT_MAX_PERF_RECORD_SIZE - (*Length) - 1;
856 }
857
858 UnicodeStrnToAsciiStrS(StringPtr, ControllerNameStringSize - 1, ComponentNameString, ControllerNameStringSize, &ControllerNameStringSize);
859
860 //
861 // Add a space in the end of the ControllerName
862 //
863 AsciiStringPtr = ComponentNameString + ControllerNameStringSize - 1;
864 *AsciiStringPtr = 0x20;
865 AsciiStringPtr++;
866 *AsciiStringPtr = 0;
867 ControllerNameStringSize++;
868
869 *Length += (UINT8)ControllerNameStringSize;
870 }
871
872 //
873 // This function returns the device path protocol from the handle specified by Handle. If Handle is
874 // NULL or Handle does not contain a device path protocol, then NULL is returned.
875 //
876 DevicePathProtocol = DevicePathFromHandle(ControllerHandle);
877
878 if (DevicePathProtocol != NULL) {
879 StringPtr = ConvertDevicePathToText (DevicePathProtocol, TRUE, FALSE);
880 if (StringPtr != NULL) {
881 //
882 // This will produce the size of the unicode string, which is twice as large as the ASCII one
883 // This must be an even number, so ok to divide by 2
884 //
885 DevicePathStringSize = StrSize(StringPtr) / 2;
886
887 if ((DevicePathStringSize + (*Length)) > FPDT_MAX_PERF_RECORD_SIZE) {
888 //
889 // Only copy enough to fill FPDT_MAX_PERF_RECORD_SIZE worth of the record
890 //
891 DevicePathStringSize = FPDT_MAX_PERF_RECORD_SIZE - (*Length);
892 }
893
894 if (ControllerNameStringSize != 0) {
895 AsciiStringPtr = ComponentNameString + ControllerNameStringSize - 1;
896 } else {
897 AsciiStringPtr = ComponentNameString;
898 }
899
900 UnicodeStrnToAsciiStrS(StringPtr, DevicePathStringSize - 1, AsciiStringPtr, DevicePathStringSize, &DevicePathStringSize);
901 *Length += (UINT8)DevicePathStringSize;
902 return EFI_SUCCESS;
903 }
904 }
905
906 return EFI_UNSUPPORTED;
907 }
908
909 /**
910 Create performance record with event description and a timestamp.
911
912 @param CallerIdentifier - Image handle or pointer to caller ID GUID.
913 @param Guid - Pointer to a GUID.
914 @param String - Pointer to a string describing the measurement.
915 @param Ticker - 64-bit time stamp.
916 @param Address - Pointer to a location in memory relevant to the measurement.
917 @param PerfId - Performance identifier describing the type of measurement.
918 @param Attribute - The attribute of the measurement. According to attribute can create a start
919 record for PERF_START/PERF_START_EX, or a end record for PERF_END/PERF_END_EX,
920 or a general record for other Perf macros.
921
922 @retval EFI_SUCCESS - Successfully created performance record.
923 @retval EFI_OUT_OF_RESOURCES - Ran out of space to store the records.
924 @retval EFI_INVALID_PARAMETER - Invalid parameter passed to function - NULL
925 pointer or invalid PerfId.
926
927 @retval EFI_SUCCESS - Successfully created performance record
928 @retval EFI_OUT_OF_RESOURCES - Ran out of space to store the records
929 @retval EFI_INVALID_PARAMETER - Invalid parameter passed to function - NULL
930 pointer or invalid PerfId
931
932 **/
933 EFI_STATUS
934 InsertFpdtRecord (
935 IN CONST VOID *CallerIdentifier OPTIONAL,
936 IN CONST VOID *Guid OPTIONAL,
937 IN CONST CHAR8 *String OPTIONAL,
938 IN UINT64 Ticker,
939 IN UINT64 Address OPTIONAL,
940 IN UINT16 PerfId,
941 IN PERF_MEASUREMENT_ATTRIBUTE Attribute
942 )
943 {
944 EFI_GUID ModuleGuid;
945 CHAR8 ModuleName[FPDT_STRING_EVENT_RECORD_NAME_LENGTH];
946 FPDT_RECORD_PTR FpdtRecordPtr;
947 FPDT_RECORD_PTR CachedFpdtRecordPtr;
948 UINT64 TimeStamp;
949 CONST CHAR8 *StringPtr;
950 UINTN DestMax;
951 UINTN StringLen;
952 EFI_STATUS Status;
953 UINT16 ProgressId;
954
955 StringPtr = NULL;
956 ProgressId = 0;
957 ZeroMem (ModuleName, sizeof (ModuleName));
958
959 //
960 // 1. Get the Perf Id for records from PERF_START/PERF_END, PERF_START_EX/PERF_END_EX.
961 // notes: For other Perf macros (Attribute == PerfEntry), their Id is known.
962 //
963 if (Attribute != PerfEntry) {
964 //
965 // If PERF_START_EX()/PERF_END_EX() have specified the ProgressID,it has high priority.
966 // !!! Note: If the Perf is not the known Token used in the core but have same
967 // ID with the core Token, this case will not be supported.
968 // And in currtnt usage mode, for the unkown ID, there is a general rule:
969 // If it is start pref: the lower 4 bits of the ID should be 0.
970 // If it is end pref: the lower 4 bits of the ID should not be 0.
971 // If input ID doesn't follow the rule, we will adjust it.
972 //
973 if ((PerfId != 0) && (IsKnownID (PerfId)) && (!IsKnownTokens (String))) {
974 return EFI_INVALID_PARAMETER;
975 } else if ((PerfId != 0) && (!IsKnownID (PerfId)) && (!IsKnownTokens (String))) {
976 if ((Attribute == PerfStartEntry) && ((PerfId & 0x000F) != 0)) {
977 PerfId &= 0xFFF0;
978 } else if ((Attribute == PerfEndEntry) && ((PerfId & 0x000F) == 0)) {
979 PerfId += 1;
980 }
981 } else if (PerfId == 0) {
982 //
983 // Get ProgressID form the String Token.
984 //
985 Status = GetFpdtRecordId (Attribute, CallerIdentifier, String, &ProgressId);
986 if (EFI_ERROR (Status)) {
987 return Status;
988 }
989 PerfId = ProgressId;
990 }
991 }
992
993 //
994 // 2. Get the buffer to store the FPDT record.
995 //
996 Status = GetFpdtRecordPtr (FPDT_MAX_PERF_RECORD_SIZE, &FpdtRecordPtr);
997 if (EFI_ERROR (Status)) {
998 return Status;
999 }
1000
1001 //
1002 //3. Get the TimeStamp.
1003 //
1004 if (Ticker == 0) {
1005 Ticker = GetPerformanceCounter ();
1006 TimeStamp = GetTimeInNanoSecond (Ticker);
1007 } else if (Ticker == 1) {
1008 TimeStamp = 0;
1009 } else {
1010 TimeStamp = GetTimeInNanoSecond (Ticker);
1011 }
1012
1013 //
1014 // 4. Fill in the FPDT record according to different Performance Identifier.
1015 //
1016 switch (PerfId) {
1017 case MODULE_START_ID:
1018 case MODULE_END_ID:
1019 GetModuleInfoFromHandle ((EFI_HANDLE)CallerIdentifier, ModuleName, sizeof (ModuleName), &ModuleGuid);
1020 StringPtr = ModuleName;
1021 //
1022 // Cache the offset of start image start record and use to update the start image end record if needed.
1023 //
1024 if (Attribute == PerfEntry && PerfId == MODULE_START_ID) {
1025 if (mFpdtBufferIsReported) {
1026 mCachedLength = mBootRecordSize;
1027 } else {
1028 mCachedLength = mPerformanceLength;
1029 }
1030 }
1031 if (!PcdGetBool (PcdEdkiiFpdtStringRecordEnableOnly)) {
1032 FpdtRecordPtr.GuidEvent->Header.Type = FPDT_GUID_EVENT_TYPE;
1033 FpdtRecordPtr.GuidEvent->Header.Length = sizeof (FPDT_GUID_EVENT_RECORD);
1034 FpdtRecordPtr.GuidEvent->Header.Revision = FPDT_RECORD_REVISION_1;
1035 FpdtRecordPtr.GuidEvent->ProgressID = PerfId;
1036 FpdtRecordPtr.GuidEvent->Timestamp = TimeStamp;
1037 CopyMem (&FpdtRecordPtr.GuidEvent->Guid, &ModuleGuid, sizeof (FpdtRecordPtr.GuidEvent->Guid));
1038 if (CallerIdentifier == NULL && PerfId == MODULE_END_ID && mCachedLength != 0) {
1039 if (mFpdtBufferIsReported) {
1040 CachedFpdtRecordPtr.RecordHeader = (EFI_ACPI_5_0_FPDT_PERFORMANCE_RECORD_HEADER *)(mBootRecordBuffer + mCachedLength);
1041 } else {
1042 CachedFpdtRecordPtr.RecordHeader = (EFI_ACPI_5_0_FPDT_PERFORMANCE_RECORD_HEADER *)(mPerformancePointer + mCachedLength);
1043 }
1044 CopyMem (&FpdtRecordPtr.GuidEvent->Guid, &CachedFpdtRecordPtr.GuidEvent->Guid, sizeof (FpdtRecordPtr.GuidEvent->Guid));
1045 mCachedLength = 0;
1046 }
1047 }
1048 break;
1049
1050 case MODULE_LOADIMAGE_START_ID:
1051 case MODULE_LOADIMAGE_END_ID:
1052 GetModuleInfoFromHandle ((EFI_HANDLE)CallerIdentifier, ModuleName, sizeof (ModuleName), &ModuleGuid);
1053 StringPtr = ModuleName;
1054 if (PerfId == MODULE_LOADIMAGE_START_ID) {
1055 mLoadImageCount ++;
1056 //
1057 // Cache the offset of load image start record and use to be updated by the load image end record if needed.
1058 //
1059 if (CallerIdentifier == NULL && Attribute == PerfEntry) {
1060 if (mFpdtBufferIsReported) {
1061 mCachedLength = mBootRecordSize;
1062 } else {
1063 mCachedLength = mPerformanceLength;
1064 }
1065 }
1066 }
1067 if (!PcdGetBool (PcdEdkiiFpdtStringRecordEnableOnly)) {
1068 FpdtRecordPtr.GuidQwordEvent->Header.Type = FPDT_GUID_QWORD_EVENT_TYPE;
1069 FpdtRecordPtr.GuidQwordEvent->Header.Length = sizeof (FPDT_GUID_QWORD_EVENT_RECORD);
1070 FpdtRecordPtr.GuidQwordEvent->Header.Revision = FPDT_RECORD_REVISION_1;
1071 FpdtRecordPtr.GuidQwordEvent->ProgressID = PerfId;
1072 FpdtRecordPtr.GuidQwordEvent->Timestamp = TimeStamp;
1073 FpdtRecordPtr.GuidQwordEvent->Qword = mLoadImageCount;
1074 CopyMem (&FpdtRecordPtr.GuidQwordEvent->Guid, &ModuleGuid, sizeof (FpdtRecordPtr.GuidQwordEvent->Guid));
1075 if (PerfId == MODULE_LOADIMAGE_END_ID && mCachedLength != 0) {
1076 if (mFpdtBufferIsReported) {
1077 CachedFpdtRecordPtr.RecordHeader = (EFI_ACPI_5_0_FPDT_PERFORMANCE_RECORD_HEADER *)(mBootRecordBuffer + mCachedLength);
1078 } else {
1079 CachedFpdtRecordPtr.RecordHeader = (EFI_ACPI_5_0_FPDT_PERFORMANCE_RECORD_HEADER *)(mPerformancePointer + mCachedLength);
1080 }
1081 CopyMem (&CachedFpdtRecordPtr.GuidQwordEvent->Guid, &ModuleGuid, sizeof (CachedFpdtRecordPtr.GuidQwordEvent->Guid));
1082 mCachedLength = 0;
1083 }
1084 }
1085 break;
1086
1087 case MODULE_DB_START_ID:
1088 case MODULE_DB_SUPPORT_START_ID:
1089 case MODULE_DB_SUPPORT_END_ID:
1090 case MODULE_DB_STOP_START_ID:
1091 case MODULE_DB_STOP_END_ID:
1092 GetModuleInfoFromHandle ((EFI_HANDLE)CallerIdentifier, ModuleName, sizeof (ModuleName), &ModuleGuid);
1093 StringPtr = ModuleName;
1094 if (!PcdGetBool (PcdEdkiiFpdtStringRecordEnableOnly)) {
1095 FpdtRecordPtr.GuidQwordEvent->Header.Type = FPDT_GUID_QWORD_EVENT_TYPE;
1096 FpdtRecordPtr.GuidQwordEvent->Header.Length = sizeof (FPDT_GUID_QWORD_EVENT_RECORD);
1097 FpdtRecordPtr.GuidQwordEvent->Header.Revision = FPDT_RECORD_REVISION_1;
1098 FpdtRecordPtr.GuidQwordEvent->ProgressID = PerfId;
1099 FpdtRecordPtr.GuidQwordEvent->Timestamp = TimeStamp;
1100 FpdtRecordPtr.GuidQwordEvent->Qword = Address;
1101 CopyMem (&FpdtRecordPtr.GuidQwordEvent->Guid, &ModuleGuid, sizeof (FpdtRecordPtr.GuidQwordEvent->Guid));
1102 }
1103 break;
1104
1105 case MODULE_DB_END_ID:
1106 GetModuleInfoFromHandle ((EFI_HANDLE)CallerIdentifier, ModuleName, sizeof (ModuleName), &ModuleGuid);
1107 StringPtr = ModuleName;
1108 if (!PcdGetBool (PcdEdkiiFpdtStringRecordEnableOnly)) {
1109 FpdtRecordPtr.GuidQwordStringEvent->Header.Type = FPDT_GUID_QWORD_STRING_EVENT_TYPE;
1110 FpdtRecordPtr.GuidQwordStringEvent->Header.Length = sizeof (FPDT_GUID_QWORD_STRING_EVENT_RECORD);;
1111 FpdtRecordPtr.GuidQwordStringEvent->Header.Revision = FPDT_RECORD_REVISION_1;
1112 FpdtRecordPtr.GuidQwordStringEvent->ProgressID = PerfId;
1113 FpdtRecordPtr.GuidQwordStringEvent->Timestamp = TimeStamp;
1114 FpdtRecordPtr.GuidQwordStringEvent->Qword = Address;
1115 CopyMem (&FpdtRecordPtr.GuidQwordStringEvent->Guid, &ModuleGuid, sizeof (FpdtRecordPtr.GuidQwordStringEvent->Guid));
1116 if (Address != 0) {
1117 GetDeviceInfoFromHandleAndUpdateLength(CallerIdentifier, (EFI_HANDLE)(UINTN)Address, FpdtRecordPtr.GuidQwordStringEvent->String, &FpdtRecordPtr.GuidQwordStringEvent->Header.Length);
1118 }
1119 }
1120 break;
1121
1122 case PERF_EVENTSIGNAL_START_ID:
1123 case PERF_EVENTSIGNAL_END_ID:
1124 case PERF_CALLBACK_START_ID:
1125 case PERF_CALLBACK_END_ID:
1126 if (String == NULL || Guid == NULL) {
1127 return EFI_INVALID_PARAMETER;
1128 }
1129 StringPtr = String;
1130 if (AsciiStrLen (String) == 0) {
1131 StringPtr = "unknown name";
1132 }
1133 if (!PcdGetBool (PcdEdkiiFpdtStringRecordEnableOnly)) {
1134 FpdtRecordPtr.DualGuidStringEvent->Header.Type = FPDT_DUAL_GUID_STRING_EVENT_TYPE;
1135 FpdtRecordPtr.DualGuidStringEvent->Header.Length = sizeof (FPDT_DUAL_GUID_STRING_EVENT_RECORD);
1136 FpdtRecordPtr.DualGuidStringEvent->Header.Revision = FPDT_RECORD_REVISION_1;
1137 FpdtRecordPtr.DualGuidStringEvent->ProgressID = PerfId;
1138 FpdtRecordPtr.DualGuidStringEvent->Timestamp = TimeStamp;
1139 CopyMem (&FpdtRecordPtr.DualGuidStringEvent->Guid1, CallerIdentifier, sizeof (FpdtRecordPtr.DualGuidStringEvent->Guid1));
1140 CopyMem (&FpdtRecordPtr.DualGuidStringEvent->Guid2, Guid, sizeof (FpdtRecordPtr.DualGuidStringEvent->Guid2));
1141 CopyStringIntoPerfRecordAndUpdateLength (FpdtRecordPtr.DualGuidStringEvent->String, StringPtr, &FpdtRecordPtr.DualGuidStringEvent->Header.Length);
1142 }
1143 break;
1144
1145 case PERF_EVENT_ID:
1146 case PERF_FUNCTION_START_ID:
1147 case PERF_FUNCTION_END_ID:
1148 case PERF_INMODULE_START_ID:
1149 case PERF_INMODULE_END_ID:
1150 case PERF_CROSSMODULE_START_ID:
1151 case PERF_CROSSMODULE_END_ID:
1152 GetModuleInfoFromHandle ((EFI_HANDLE)CallerIdentifier, ModuleName, sizeof (ModuleName), &ModuleGuid);
1153 if (String != NULL) {
1154 StringPtr = String;
1155 } else {
1156 StringPtr = ModuleName;
1157 }
1158 if (AsciiStrLen (StringPtr) == 0) {
1159 StringPtr = "unknown name";
1160 }
1161 if (!PcdGetBool (PcdEdkiiFpdtStringRecordEnableOnly)) {
1162 FpdtRecordPtr.DynamicStringEvent->Header.Type = FPDT_DYNAMIC_STRING_EVENT_TYPE;
1163 FpdtRecordPtr.DynamicStringEvent->Header.Length = sizeof (FPDT_DYNAMIC_STRING_EVENT_RECORD);
1164 FpdtRecordPtr.DynamicStringEvent->Header.Revision = FPDT_RECORD_REVISION_1;
1165 FpdtRecordPtr.DynamicStringEvent->ProgressID = PerfId;
1166 FpdtRecordPtr.DynamicStringEvent->Timestamp = TimeStamp;
1167 CopyMem (&FpdtRecordPtr.DynamicStringEvent->Guid, &ModuleGuid, sizeof (FpdtRecordPtr.DynamicStringEvent->Guid));
1168 CopyStringIntoPerfRecordAndUpdateLength (FpdtRecordPtr.DynamicStringEvent->String, StringPtr, &FpdtRecordPtr.DynamicStringEvent->Header.Length);
1169 }
1170 break;
1171
1172 default:
1173 if (Attribute != PerfEntry) {
1174 GetModuleInfoFromHandle ((EFI_HANDLE)CallerIdentifier, ModuleName, sizeof (ModuleName), &ModuleGuid);
1175 if (String != NULL) {
1176 StringPtr = String;
1177 } else {
1178 StringPtr = ModuleName;
1179 }
1180 if (AsciiStrLen (StringPtr) == 0) {
1181 StringPtr = "unknown name";
1182 }
1183 if (!PcdGetBool (PcdEdkiiFpdtStringRecordEnableOnly)) {
1184 FpdtRecordPtr.DynamicStringEvent->Header.Type = FPDT_DYNAMIC_STRING_EVENT_TYPE;
1185 FpdtRecordPtr.DynamicStringEvent->Header.Length = sizeof (FPDT_DYNAMIC_STRING_EVENT_RECORD);
1186 FpdtRecordPtr.DynamicStringEvent->Header.Revision = FPDT_RECORD_REVISION_1;
1187 FpdtRecordPtr.DynamicStringEvent->ProgressID = PerfId;
1188 FpdtRecordPtr.DynamicStringEvent->Timestamp = TimeStamp;
1189 CopyMem (&FpdtRecordPtr.DynamicStringEvent->Guid, &ModuleGuid, sizeof (FpdtRecordPtr.DynamicStringEvent->Guid));
1190 CopyStringIntoPerfRecordAndUpdateLength (FpdtRecordPtr.DynamicStringEvent->String, StringPtr, &FpdtRecordPtr.DynamicStringEvent->Header.Length);
1191 }
1192 } else {
1193 return EFI_INVALID_PARAMETER;
1194 }
1195 break;
1196 }
1197
1198 //
1199 // 4.2 When PcdEdkiiFpdtStringRecordEnableOnly==TRUE, create string record for all Perf entries.
1200 //
1201 if (PcdGetBool (PcdEdkiiFpdtStringRecordEnableOnly)) {
1202 if (StringPtr == NULL ||PerfId == MODULE_DB_SUPPORT_START_ID || PerfId == MODULE_DB_SUPPORT_END_ID) {
1203 return EFI_INVALID_PARAMETER;
1204 }
1205 FpdtRecordPtr.DynamicStringEvent->Header.Type = FPDT_DYNAMIC_STRING_EVENT_TYPE;
1206 FpdtRecordPtr.DynamicStringEvent->Header.Length = sizeof (FPDT_DYNAMIC_STRING_EVENT_RECORD);
1207 FpdtRecordPtr.DynamicStringEvent->Header.Revision = FPDT_RECORD_REVISION_1;
1208 FpdtRecordPtr.DynamicStringEvent->ProgressID = PerfId;
1209 FpdtRecordPtr.DynamicStringEvent->Timestamp = TimeStamp;
1210 if (Guid != NULL) {
1211 //
1212 // Cache the event guid in string event record.
1213 //
1214 CopyMem (&FpdtRecordPtr.DynamicStringEvent->Guid, Guid, sizeof (FpdtRecordPtr.DynamicStringEvent->Guid));
1215 } else {
1216 CopyMem (&FpdtRecordPtr.DynamicStringEvent->Guid, &ModuleGuid, sizeof (FpdtRecordPtr.DynamicStringEvent->Guid));
1217 }
1218 if (AsciiStrLen (StringPtr) == 0) {
1219 StringPtr = "unknown name";
1220 }
1221 CopyStringIntoPerfRecordAndUpdateLength (FpdtRecordPtr.DynamicStringEvent->String, StringPtr, &FpdtRecordPtr.DynamicStringEvent->Header.Length);
1222
1223 if ((PerfId == MODULE_LOADIMAGE_START_ID) || (PerfId == MODULE_END_ID)) {
1224 FpdtRecordPtr.DynamicStringEvent->Header.Length = (UINT8)(sizeof (FPDT_DYNAMIC_STRING_EVENT_RECORD)+ STRING_SIZE);
1225 }
1226 if ((PerfId == MODULE_LOADIMAGE_END_ID || PerfId == MODULE_END_ID) && mCachedLength != 0) {
1227 if (mFpdtBufferIsReported) {
1228 CachedFpdtRecordPtr.RecordHeader = (EFI_ACPI_5_0_FPDT_PERFORMANCE_RECORD_HEADER *)(mBootRecordBuffer + mCachedLength);
1229 } else {
1230 CachedFpdtRecordPtr.RecordHeader = (EFI_ACPI_5_0_FPDT_PERFORMANCE_RECORD_HEADER *)(mPerformancePointer + mCachedLength);
1231 }
1232 if (PerfId == MODULE_LOADIMAGE_END_ID) {
1233 DestMax = CachedFpdtRecordPtr.DynamicStringEvent->Header.Length - sizeof (FPDT_DYNAMIC_STRING_EVENT_RECORD);
1234 StringLen = AsciiStrLen (StringPtr);
1235 if (StringLen >= DestMax) {
1236 StringLen = DestMax -1;
1237 }
1238 CopyMem (&CachedFpdtRecordPtr.DynamicStringEvent->Guid, &ModuleGuid, sizeof (CachedFpdtRecordPtr.DynamicStringEvent->Guid));
1239 AsciiStrnCpyS (CachedFpdtRecordPtr.DynamicStringEvent->String, DestMax, StringPtr, StringLen);
1240 } else if (PerfId == MODULE_END_ID) {
1241 DestMax = FpdtRecordPtr.DynamicStringEvent->Header.Length - sizeof (FPDT_DYNAMIC_STRING_EVENT_RECORD);
1242 StringLen = AsciiStrLen (CachedFpdtRecordPtr.DynamicStringEvent->String);
1243 if (StringLen >= DestMax) {
1244 StringLen = DestMax -1;
1245 }
1246 CopyMem (&FpdtRecordPtr.DynamicStringEvent->Guid, &CachedFpdtRecordPtr.DynamicStringEvent->Guid, sizeof (CachedFpdtRecordPtr.DynamicStringEvent->Guid));
1247 AsciiStrnCpyS (FpdtRecordPtr.DynamicStringEvent->String, DestMax, CachedFpdtRecordPtr.DynamicStringEvent->String, StringLen);
1248 }
1249 mCachedLength = 0;
1250 }
1251 }
1252
1253 //
1254 // 5. Update the length of the used buffer after fill in the record.
1255 //
1256 if (mFpdtBufferIsReported) {
1257 mBootRecordSize += FpdtRecordPtr.RecordHeader->Length;
1258 mAcpiBootPerformanceTable->Header.Length += FpdtRecordPtr.RecordHeader->Length;
1259 } else {
1260 mPerformanceLength += FpdtRecordPtr.RecordHeader->Length;
1261 }
1262 return EFI_SUCCESS;
1263 }
1264
1265 /**
1266 Dumps all the PEI performance.
1267
1268 @param HobStart A pointer to a Guid.
1269
1270 This internal function dumps all the PEI performance log to the DXE performance gauge array.
1271 It retrieves the optional GUID HOB for PEI performance and then saves the performance data
1272 to DXE performance data structures.
1273
1274 **/
1275 VOID
1276 InternalGetPeiPerformance (
1277 VOID *HobStart
1278 )
1279 {
1280 UINT8 *FirmwarePerformanceHob;
1281 FPDT_PEI_EXT_PERF_HEADER *PeiPerformanceLogHeader;
1282 UINT8 *EventRec;
1283 EFI_HOB_GUID_TYPE *GuidHob;
1284
1285 GuidHob = GetNextGuidHob (&gEdkiiFpdtExtendedFirmwarePerformanceGuid, HobStart);
1286 while (GuidHob != NULL) {
1287 FirmwarePerformanceHob = GET_GUID_HOB_DATA (GuidHob);
1288 PeiPerformanceLogHeader = (FPDT_PEI_EXT_PERF_HEADER *)FirmwarePerformanceHob;
1289
1290 if (mPerformanceLength + PeiPerformanceLogHeader->SizeOfAllEntries > mMaxPerformanceLength) {
1291 mPerformancePointer = ReallocatePool (
1292 mPerformanceLength,
1293 mPerformanceLength +
1294 (UINTN)PeiPerformanceLogHeader->SizeOfAllEntries +
1295 FIRMWARE_RECORD_BUFFER,
1296 mPerformancePointer
1297 );
1298 ASSERT (mPerformancePointer != NULL);
1299 mMaxPerformanceLength = mPerformanceLength +
1300 (UINTN)(PeiPerformanceLogHeader->SizeOfAllEntries) +
1301 FIRMWARE_RECORD_BUFFER;
1302 }
1303
1304 EventRec = mPerformancePointer + mPerformanceLength;
1305 CopyMem (EventRec, FirmwarePerformanceHob + sizeof (FPDT_PEI_EXT_PERF_HEADER), (UINTN)(PeiPerformanceLogHeader->SizeOfAllEntries));
1306 //
1307 // Update the used buffer size.
1308 //
1309 mPerformanceLength += (UINTN)(PeiPerformanceLogHeader->SizeOfAllEntries);
1310 mLoadImageCount += PeiPerformanceLogHeader->LoadImageCount;
1311
1312 //
1313 // Get next performance guid hob
1314 //
1315 GuidHob = GetNextGuidHob (&gEdkiiFpdtExtendedFirmwarePerformanceGuid, GET_NEXT_HOB (GuidHob));
1316 }
1317 }
1318
1319 /**
1320 Report Boot Perforamnce table address as report status code.
1321
1322 @param Event The event of notify protocol.
1323 @param Context Notify event context.
1324
1325 **/
1326 VOID
1327 EFIAPI
1328 ReportFpdtRecordBuffer (
1329 IN EFI_EVENT Event,
1330 IN VOID *Context
1331 )
1332 {
1333 EFI_STATUS Status;
1334 UINT64 BPDTAddr;
1335
1336 if (!mFpdtBufferIsReported) {
1337 Status = AllocateBootPerformanceTable ();
1338 if (!EFI_ERROR(Status)) {
1339 BPDTAddr = (UINT64)(UINTN)mAcpiBootPerformanceTable;
1340 REPORT_STATUS_CODE_EX (
1341 EFI_PROGRESS_CODE,
1342 EFI_SOFTWARE_DXE_BS_DRIVER,
1343 0,
1344 NULL,
1345 &gEdkiiFpdtExtendedFirmwarePerformanceGuid,
1346 &BPDTAddr,
1347 sizeof (UINT64)
1348 );
1349 }
1350 //
1351 // Set FPDT report state to TRUE.
1352 //
1353 mFpdtBufferIsReported = TRUE;
1354 }
1355 }
1356
1357 /**
1358 Update Boot Performance table.
1359
1360 @param Event The event of notify protocol.
1361 @param Context Notify event context.
1362
1363 **/
1364 VOID
1365 EFIAPI
1366 UpdateBootPerformanceTable (
1367 IN EFI_EVENT Event,
1368 IN VOID *Context
1369 )
1370 {
1371 VOID *SmmBootRecordData;
1372 UINTN SmmBootRecordDataSize;
1373 UINTN AppendSize;
1374 UINT8 *FirmwarePerformanceTablePtr;
1375
1376 SmmBootRecordDataSize = 0;
1377
1378 //
1379 // Get SMM performance data.
1380 //
1381 SmmBootRecordData = NULL;
1382 InternalGetSmmPerfData (&SmmBootRecordData, &SmmBootRecordDataSize, FALSE);
1383
1384 FirmwarePerformanceTablePtr = (UINT8 *) mAcpiBootPerformanceTable + mAcpiBootPerformanceTable->Header.Length;
1385
1386 if (mAcpiBootPerformanceTable->Header.Length + SmmBootRecordDataSize > mBootRecordMaxSize) {
1387 DEBUG ((DEBUG_INFO, "DxeCorePerformanceLib: No enough space to save all SMM boot performance data\n"));
1388 AppendSize = mBootRecordMaxSize - mAcpiBootPerformanceTable->Header.Length;
1389 } else {
1390 AppendSize = SmmBootRecordDataSize;
1391 }
1392 if (SmmBootRecordData != NULL) {
1393 CopyMem (FirmwarePerformanceTablePtr, SmmBootRecordData, AppendSize);
1394 mAcpiBootPerformanceTable->Header.Length += (UINT32) AppendSize;
1395 mBootRecordSize += (UINT32) AppendSize;
1396 FreePool (SmmBootRecordData);
1397 }
1398 }
1399
1400 /**
1401 The constructor function initializes Performance infrastructure for DXE phase.
1402
1403 The constructor function publishes Performance and PerformanceEx protocol, allocates memory to log DXE performance
1404 and merges PEI performance data to DXE performance log.
1405 It will ASSERT() if one of these operations fails and it will always return EFI_SUCCESS.
1406
1407 @param ImageHandle The firmware allocated handle for the EFI image.
1408 @param SystemTable A pointer to the EFI System Table.
1409
1410 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
1411
1412 **/
1413 EFI_STATUS
1414 EFIAPI
1415 DxeCorePerformanceLibConstructor (
1416 IN EFI_HANDLE ImageHandle,
1417 IN EFI_SYSTEM_TABLE *SystemTable
1418 )
1419 {
1420 EFI_STATUS Status;
1421 EFI_HANDLE Handle;
1422 EFI_EVENT EndOfDxeEvent;
1423 EFI_EVENT ReadyToBootEvent;
1424 PERFORMANCE_PROPERTY *PerformanceProperty;
1425
1426 if (!PerformanceMeasurementEnabled ()) {
1427 //
1428 // Do not initialize performance infrastructure if not required.
1429 //
1430 return EFI_SUCCESS;
1431 }
1432
1433 //
1434 // Dump normal PEI performance records
1435 //
1436 InternalGetPeiPerformance (GetHobList());
1437
1438 //
1439 // Install the protocol interfaces for DXE performance library instance.
1440 //
1441 Handle = NULL;
1442 Status = gBS->InstallMultipleProtocolInterfaces (
1443 &Handle,
1444 &gEdkiiPerformanceMeasurementProtocolGuid,
1445 &mPerformanceMeasurementInterface,
1446 NULL
1447 );
1448 ASSERT_EFI_ERROR (Status);
1449
1450 //
1451 // Register EndOfDxe event to allocate the boot performance table and report the table address through status code.
1452 //
1453 Status = gBS->CreateEventEx (
1454 EVT_NOTIFY_SIGNAL,
1455 TPL_NOTIFY,
1456 ReportFpdtRecordBuffer,
1457 NULL,
1458 &gEfiEndOfDxeEventGroupGuid,
1459 &EndOfDxeEvent
1460 );
1461
1462 //
1463 // Register ReadyToBoot event to update the boot performance table for SMM performance data.
1464 //
1465 Status = gBS->CreateEventEx (
1466 EVT_NOTIFY_SIGNAL,
1467 TPL_CALLBACK,
1468 UpdateBootPerformanceTable,
1469 NULL,
1470 &gEfiEventReadyToBootGuid,
1471 &ReadyToBootEvent
1472 );
1473
1474 ASSERT_EFI_ERROR (Status);
1475
1476 Status = EfiGetSystemConfigurationTable (&gPerformanceProtocolGuid, (VOID **) &PerformanceProperty);
1477 if (EFI_ERROR (Status)) {
1478 //
1479 // Install configuration table for performance property.
1480 //
1481 mPerformanceProperty.Revision = PERFORMANCE_PROPERTY_REVISION;
1482 mPerformanceProperty.Reserved = 0;
1483 mPerformanceProperty.Frequency = GetPerformanceCounterProperties (
1484 &mPerformanceProperty.TimerStartValue,
1485 &mPerformanceProperty.TimerEndValue
1486 );
1487 Status = gBS->InstallConfigurationTable (&gPerformanceProtocolGuid, &mPerformanceProperty);
1488 ASSERT_EFI_ERROR (Status);
1489 }
1490
1491 return EFI_SUCCESS;
1492 }
1493
1494 /**
1495 Create performance record with event description and a timestamp.
1496
1497 @param CallerIdentifier - Image handle or pointer to caller ID GUID.
1498 @param Guid - Pointer to a GUID.
1499 @param String - Pointer to a string describing the measurement.
1500 @param TimeStamp - 64-bit time stamp.
1501 @param Address - Pointer to a location in memory relevant to the measurement.
1502 @param Identifier - Performance identifier describing the type of measurement.
1503 @param Attribute - The attribute of the measurement. According to attribute can create a start
1504 record for PERF_START/PERF_START_EX, or a end record for PERF_END/PERF_END_EX,
1505 or a general record for other Perf macros.
1506
1507 @retval EFI_SUCCESS - Successfully created performance record.
1508 @retval EFI_OUT_OF_RESOURCES - Ran out of space to store the records.
1509 @retval EFI_INVALID_PARAMETER - Invalid parameter passed to function - NULL
1510 pointer or invalid PerfId.
1511 **/
1512 EFI_STATUS
1513 EFIAPI
1514 CreatePerformanceMeasurement (
1515 IN CONST VOID *CallerIdentifier,
1516 IN CONST VOID *Guid OPTIONAL,
1517 IN CONST CHAR8 *String OPTIONAL,
1518 IN UINT64 TimeStamp,
1519 IN UINT64 Address OPTIONAL,
1520 IN UINT32 Identifier,
1521 IN PERF_MEASUREMENT_ATTRIBUTE Attribute
1522 )
1523 {
1524 EFI_STATUS Status;
1525
1526 Status = EFI_SUCCESS;
1527
1528 if (mLockInsertRecord) {
1529 return EFI_INVALID_PARAMETER;
1530 }
1531 mLockInsertRecord = TRUE;
1532
1533 Status = InsertFpdtRecord (CallerIdentifier, Guid, String, TimeStamp, Address, (UINT16)Identifier, Attribute);
1534
1535 mLockInsertRecord = FALSE;
1536
1537 return Status;
1538 }
1539
1540 /**
1541 Adds a record at the end of the performance measurement log
1542 that records the start time of a performance measurement.
1543
1544 Adds a record to the end of the performance measurement log
1545 that contains the Handle, Token, Module and Identifier.
1546 The end time of the new record must be set to zero.
1547 If TimeStamp is not zero, then TimeStamp is used to fill in the start time in the record.
1548 If TimeStamp is zero, the start time in the record is filled in with the value
1549 read from the current time stamp.
1550
1551 @param Handle Pointer to environment specific context used
1552 to identify the component being measured.
1553 @param Token Pointer to a Null-terminated ASCII string
1554 that identifies the component being measured.
1555 @param Module Pointer to a Null-terminated ASCII string
1556 that identifies the module being measured.
1557 @param TimeStamp 64-bit time stamp.
1558 @param Identifier 32-bit identifier. If the value is 0, the created record
1559 is same as the one created by StartPerformanceMeasurement.
1560
1561 @retval RETURN_SUCCESS The start of the measurement was recorded.
1562 @retval RETURN_OUT_OF_RESOURCES There are not enough resources to record the measurement.
1563
1564 **/
1565 RETURN_STATUS
1566 EFIAPI
1567 StartPerformanceMeasurementEx (
1568 IN CONST VOID *Handle OPTIONAL,
1569 IN CONST CHAR8 *Token OPTIONAL,
1570 IN CONST CHAR8 *Module OPTIONAL,
1571 IN UINT64 TimeStamp,
1572 IN UINT32 Identifier
1573 )
1574 {
1575 CONST CHAR8 *String;
1576
1577 if (Token != NULL) {
1578 String = Token;
1579 } else if (Module != NULL) {
1580 String = Module;
1581 } else {
1582 String = NULL;
1583 }
1584
1585 return (RETURN_STATUS)CreatePerformanceMeasurement (Handle, NULL, String, TimeStamp, 0, Identifier, PerfStartEntry);
1586 }
1587
1588 /**
1589 Searches the performance measurement log from the beginning of the log
1590 for the first matching record that contains a zero end time and fills in a valid end time.
1591
1592 Searches the performance measurement log from the beginning of the log
1593 for the first record that matches Handle, Token, Module and Identifier and has an end time value of zero.
1594 If the record can not be found then return RETURN_NOT_FOUND.
1595 If the record is found and TimeStamp is not zero,
1596 then the end time in the record is filled in with the value specified by TimeStamp.
1597 If the record is found and TimeStamp is zero, then the end time in the matching record
1598 is filled in with the current time stamp value.
1599
1600 @param Handle Pointer to environment specific context used
1601 to identify the component being measured.
1602 @param Token Pointer to a Null-terminated ASCII string
1603 that identifies the component being measured.
1604 @param Module Pointer to a Null-terminated ASCII string
1605 that identifies the module being measured.
1606 @param TimeStamp 64-bit time stamp.
1607 @param Identifier 32-bit identifier. If the value is 0, the found record
1608 is same as the one found by EndPerformanceMeasurement.
1609
1610 @retval RETURN_SUCCESS The end of the measurement was recorded.
1611 @retval RETURN_NOT_FOUND The specified measurement record could not be found.
1612
1613 **/
1614 RETURN_STATUS
1615 EFIAPI
1616 EndPerformanceMeasurementEx (
1617 IN CONST VOID *Handle OPTIONAL,
1618 IN CONST CHAR8 *Token OPTIONAL,
1619 IN CONST CHAR8 *Module OPTIONAL,
1620 IN UINT64 TimeStamp,
1621 IN UINT32 Identifier
1622 )
1623 {
1624 CONST CHAR8 *String;
1625
1626 if (Token != NULL) {
1627 String = Token;
1628 } else if (Module != NULL) {
1629 String = Module;
1630 } else {
1631 String = NULL;
1632 }
1633
1634 return (RETURN_STATUS)CreatePerformanceMeasurement (Handle, NULL, String, TimeStamp, 0, Identifier, PerfEndEntry);
1635 }
1636
1637 /**
1638 Attempts to retrieve a performance measurement log entry from the performance measurement log.
1639 It can also retrieve the log created by StartPerformanceMeasurement and EndPerformanceMeasurement,
1640 and then assign the Identifier with 0.
1641
1642 !!! Not support!!!
1643
1644 Attempts to retrieve the performance log entry specified by LogEntryKey. If LogEntryKey is
1645 zero on entry, then an attempt is made to retrieve the first entry from the performance log,
1646 and the key for the second entry in the log is returned. If the performance log is empty,
1647 then no entry is retrieved and zero is returned. If LogEntryKey is not zero, then the performance
1648 log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is
1649 returned. If LogEntryKey is the key for the last entry in the log, then the last log entry is
1650 retrieved and an implementation specific non-zero key value that specifies the end of the performance
1651 log is returned. If LogEntryKey is equal this implementation specific non-zero key value, then no entry
1652 is retrieved and zero is returned. In the cases where a performance log entry can be returned,
1653 the log entry is returned in Handle, Token, Module, StartTimeStamp, EndTimeStamp and Identifier.
1654 If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().
1655 If Handle is NULL, then ASSERT().
1656 If Token is NULL, then ASSERT().
1657 If Module is NULL, then ASSERT().
1658 If StartTimeStamp is NULL, then ASSERT().
1659 If EndTimeStamp is NULL, then ASSERT().
1660 If Identifier is NULL, then ASSERT().
1661
1662 @param LogEntryKey On entry, the key of the performance measurement log entry to retrieve.
1663 0, then the first performance measurement log entry is retrieved.
1664 On exit, the key of the next performance log entry.
1665 @param Handle Pointer to environment specific context used to identify the component
1666 being measured.
1667 @param Token Pointer to a Null-terminated ASCII string that identifies the component
1668 being measured.
1669 @param Module Pointer to a Null-terminated ASCII string that identifies the module
1670 being measured.
1671 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
1672 was started.
1673 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
1674 was ended.
1675 @param Identifier Pointer to the 32-bit identifier that was recorded when the measurement
1676 was ended.
1677
1678 @return The key for the next performance log entry (in general case).
1679
1680 **/
1681 UINTN
1682 EFIAPI
1683 GetPerformanceMeasurementEx (
1684 IN UINTN LogEntryKey,
1685 OUT CONST VOID **Handle,
1686 OUT CONST CHAR8 **Token,
1687 OUT CONST CHAR8 **Module,
1688 OUT UINT64 *StartTimeStamp,
1689 OUT UINT64 *EndTimeStamp,
1690 OUT UINT32 *Identifier
1691 )
1692 {
1693 return 0;
1694 }
1695
1696 /**
1697 Adds a record at the end of the performance measurement log
1698 that records the start time of a performance measurement.
1699
1700 Adds a record to the end of the performance measurement log
1701 that contains the Handle, Token, and Module.
1702 The end time of the new record must be set to zero.
1703 If TimeStamp is not zero, then TimeStamp is used to fill in the start time in the record.
1704 If TimeStamp is zero, the start time in the record is filled in with the value
1705 read from the current time stamp.
1706
1707 @param Handle Pointer to environment specific context used
1708 to identify the component being measured.
1709 @param Token Pointer to a Null-terminated ASCII string
1710 that identifies the component being measured.
1711 @param Module Pointer to a Null-terminated ASCII string
1712 that identifies the module being measured.
1713 @param TimeStamp 64-bit time stamp.
1714
1715 @retval RETURN_SUCCESS The start of the measurement was recorded.
1716 @retval RETURN_OUT_OF_RESOURCES There are not enough resources to record the measurement.
1717
1718 **/
1719 RETURN_STATUS
1720 EFIAPI
1721 StartPerformanceMeasurement (
1722 IN CONST VOID *Handle OPTIONAL,
1723 IN CONST CHAR8 *Token OPTIONAL,
1724 IN CONST CHAR8 *Module OPTIONAL,
1725 IN UINT64 TimeStamp
1726 )
1727 {
1728 return StartPerformanceMeasurementEx (Handle, Token, Module, TimeStamp, 0);
1729 }
1730
1731 /**
1732 Searches the performance measurement log from the beginning of the log
1733 for the first matching record that contains a zero end time and fills in a valid end time.
1734
1735 Searches the performance measurement log from the beginning of the log
1736 for the first record that matches Handle, Token, and Module and has an end time value of zero.
1737 If the record can not be found then return RETURN_NOT_FOUND.
1738 If the record is found and TimeStamp is not zero,
1739 then the end time in the record is filled in with the value specified by TimeStamp.
1740 If the record is found and TimeStamp is zero, then the end time in the matching record
1741 is filled in with the current time stamp value.
1742
1743 @param Handle Pointer to environment specific context used
1744 to identify the component being measured.
1745 @param Token Pointer to a Null-terminated ASCII string
1746 that identifies the component being measured.
1747 @param Module Pointer to a Null-terminated ASCII string
1748 that identifies the module being measured.
1749 @param TimeStamp 64-bit time stamp.
1750
1751 @retval RETURN_SUCCESS The end of the measurement was recorded.
1752 @retval RETURN_NOT_FOUND The specified measurement record could not be found.
1753
1754 **/
1755 RETURN_STATUS
1756 EFIAPI
1757 EndPerformanceMeasurement (
1758 IN CONST VOID *Handle OPTIONAL,
1759 IN CONST CHAR8 *Token OPTIONAL,
1760 IN CONST CHAR8 *Module OPTIONAL,
1761 IN UINT64 TimeStamp
1762 )
1763 {
1764 return EndPerformanceMeasurementEx (Handle, Token, Module, TimeStamp, 0);
1765 }
1766
1767 /**
1768 Attempts to retrieve a performance measurement log entry from the performance measurement log.
1769 It can also retrieve the log created by StartPerformanceMeasurementEx and EndPerformanceMeasurementEx,
1770 and then eliminate the Identifier.
1771
1772 !!! Not support!!!
1773
1774 Attempts to retrieve the performance log entry specified by LogEntryKey. If LogEntryKey is
1775 zero on entry, then an attempt is made to retrieve the first entry from the performance log,
1776 and the key for the second entry in the log is returned. If the performance log is empty,
1777 then no entry is retrieved and zero is returned. If LogEntryKey is not zero, then the performance
1778 log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is
1779 returned. If LogEntryKey is the key for the last entry in the log, then the last log entry is
1780 retrieved and an implementation specific non-zero key value that specifies the end of the performance
1781 log is returned. If LogEntryKey is equal this implementation specific non-zero key value, then no entry
1782 is retrieved and zero is returned. In the cases where a performance log entry can be returned,
1783 the log entry is returned in Handle, Token, Module, StartTimeStamp, and EndTimeStamp.
1784 If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().
1785 If Handle is NULL, then ASSERT().
1786 If Token is NULL, then ASSERT().
1787 If Module is NULL, then ASSERT().
1788 If StartTimeStamp is NULL, then ASSERT().
1789 If EndTimeStamp is NULL, then ASSERT().
1790
1791 @param LogEntryKey On entry, the key of the performance measurement log entry to retrieve.
1792 0, then the first performance measurement log entry is retrieved.
1793 On exit, the key of the next performance log entry.
1794 @param Handle Pointer to environment specific context used to identify the component
1795 being measured.
1796 @param Token Pointer to a Null-terminated ASCII string that identifies the component
1797 being measured.
1798 @param Module Pointer to a Null-terminated ASCII string that identifies the module
1799 being measured.
1800 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
1801 was started.
1802 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
1803 was ended.
1804
1805 @return The key for the next performance log entry (in general case).
1806
1807 **/
1808 UINTN
1809 EFIAPI
1810 GetPerformanceMeasurement (
1811 IN UINTN LogEntryKey,
1812 OUT CONST VOID **Handle,
1813 OUT CONST CHAR8 **Token,
1814 OUT CONST CHAR8 **Module,
1815 OUT UINT64 *StartTimeStamp,
1816 OUT UINT64 *EndTimeStamp
1817 )
1818 {
1819 return 0;
1820 }
1821
1822 /**
1823 Returns TRUE if the performance measurement macros are enabled.
1824
1825 This function returns TRUE if the PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
1826 PcdPerformanceLibraryPropertyMask is set. Otherwise FALSE is returned.
1827
1828 @retval TRUE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
1829 PcdPerformanceLibraryPropertyMask is set.
1830 @retval FALSE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
1831 PcdPerformanceLibraryPropertyMask is clear.
1832
1833 **/
1834 BOOLEAN
1835 EFIAPI
1836 PerformanceMeasurementEnabled (
1837 VOID
1838 )
1839 {
1840 return (BOOLEAN) ((PcdGet8(PcdPerformanceLibraryPropertyMask) & PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED) != 0);
1841 }
1842
1843 /**
1844 Create performance record with event description and a timestamp.
1845
1846 @param CallerIdentifier - Image handle or pointer to caller ID GUID
1847 @param Guid - Pointer to a GUID
1848 @param String - Pointer to a string describing the measurement
1849 @param Address - Pointer to a location in memory relevant to the measurement
1850 @param Identifier - Performance identifier describing the type of measurement
1851
1852 @retval RETURN_SUCCESS - Successfully created performance record
1853 @retval RETURN_OUT_OF_RESOURCES - Ran out of space to store the records
1854 @retval RETURN_INVALID_PARAMETER - Invalid parameter passed to function - NULL
1855 pointer or invalid PerfId
1856
1857 **/
1858 RETURN_STATUS
1859 EFIAPI
1860 LogPerformanceMeasurement (
1861 IN CONST VOID *CallerIdentifier,
1862 IN CONST VOID *Guid OPTIONAL,
1863 IN CONST CHAR8 *String OPTIONAL,
1864 IN UINT64 Address OPTIONAL,
1865 IN UINT32 Identifier
1866 )
1867 {
1868 return (RETURN_STATUS)CreatePerformanceMeasurement (CallerIdentifier, Guid, String, 0, Address, Identifier, PerfEntry);
1869 }
1870
1871 /**
1872 Check whether the specified performance measurement can be logged.
1873
1874 This function returns TRUE when the PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of PcdPerformanceLibraryPropertyMask is set
1875 and the Type disable bit in PcdPerformanceLibraryPropertyMask is not set.
1876
1877 @param Type - Type of the performance measurement entry.
1878
1879 @retval TRUE The performance measurement can be logged.
1880 @retval FALSE The performance measurement can NOT be logged.
1881
1882 **/
1883 BOOLEAN
1884 EFIAPI
1885 LogPerformanceMeasurementEnabled (
1886 IN CONST UINTN Type
1887 )
1888 {
1889 //
1890 // When Performance measurement is enabled and the type is not filtered, the performance can be logged.
1891 //
1892 if (PerformanceMeasurementEnabled () && (PcdGet8(PcdPerformanceLibraryPropertyMask) & Type) == 0) {
1893 return TRUE;
1894 }
1895 return FALSE;
1896 }