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