]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Library/PeiPerformanceLib/PeiPerformanceLib.c
MdeModulePkg/PerformanceLib: Add NULL pointer check
[mirror_edk2.git] / MdeModulePkg / Library / PeiPerformanceLib / PeiPerformanceLib.c
CommitLineData
8dbae30d 1/** @file\r
857dfc45 2 Performance library instance used in PEI phase.\r
3\r
4 This file implements all APIs in Performance Library class in MdePkg. It creates\r
5 performance logging GUIDed HOB on the first performance logging and then logs the\r
6 performance data to the GUIDed HOB. Due to the limitation of temporary RAM, the maximum\r
d1102dba 7 number of performance logging entry is specified by PcdMaxPeiPerformanceLogEntries or\r
7c50b343 8 PcdMaxPeiPerformanceLogEntries16.\r
a0afd019 9\r
9169f676 10Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r
7c50b343 11(C) Copyright 2015-2016 Hewlett Packard Enterprise Development LP<BR>\r
cd5ebaa0 12This program and the accompanying materials\r
a0afd019 13are licensed and made available under the terms and conditions of the BSD License\r
14which accompanies this distribution. The full text of the license may be found at\r
15http://opensource.org/licenses/bsd-license.php\r
16\r
17THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
18WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
19\r
8dbae30d 20**/\r
a0afd019 21\r
ed7748fe 22\r
a0afd019 23#include <PiPei.h>\r
ed7748fe 24\r
9169f676 25#include <Guid/ExtendedFirmwarePerformance.h>\r
6b4d58a1 26#include <Guid/PerformanceMeasurement.h>\r
ed7748fe 27\r
a0afd019 28#include <Library/PerformanceLib.h>\r
29#include <Library/DebugLib.h>\r
30#include <Library/HobLib.h>\r
31#include <Library/BaseLib.h>\r
32#include <Library/TimerLib.h>\r
33#include <Library/PcdLib.h>\r
34#include <Library/BaseMemoryLib.h>\r
35\r
9169f676 36#define STRING_SIZE (FPDT_STRING_EVENT_RECORD_NAME_LENGTH * sizeof (CHAR8))\r
6b4d58a1
BD
37#define PEI_MAX_RECORD_SIZE (sizeof (FPDT_DUAL_GUID_STRING_EVENT_RECORD) + STRING_SIZE)\r
38\r
39\r
40/**\r
41 Return the pointer to the FPDT record in the allocated memory.\r
42\r
43 @param RecordSize The size of FPDT record.\r
44 @param FpdtRecordPtr Pointer the FPDT record in the allocated memory.\r
45 @param PeiPerformanceLogHeader Pointer to the header of the PEI Performance records in the GUID Hob.\r
46\r
47 @retval EFI_SUCCESS Successfully get the pointer to the FPDT record.\r
48 @retval EFI_OUT_OF_RESOURCES Ran out of space to store the records.\r
49**/\r
50EFI_STATUS\r
51GetFpdtRecordPtr (\r
52 IN UINT8 RecordSize,\r
53 IN OUT FPDT_RECORD_PTR *FpdtRecordPtr,\r
54 IN OUT FPDT_PEI_EXT_PERF_HEADER **PeiPerformanceLogHeader\r
55)\r
56{\r
57 UINT16 PeiPerformanceLogEntries;\r
58 UINTN PeiPerformanceSize;\r
59 UINT8 *PeiFirmwarePerformance;\r
60 EFI_HOB_GUID_TYPE *GuidHob;\r
61\r
62 //\r
63 // Get the number of PeiPerformanceLogEntries form PCD.\r
64 //\r
65 PeiPerformanceLogEntries = (UINT16) (PcdGet16 (PcdMaxPeiPerformanceLogEntries16) != 0 ?\r
66 PcdGet16 (PcdMaxPeiPerformanceLogEntries16) :\r
67 PcdGet8 (PcdMaxPeiPerformanceLogEntries));\r
68\r
69 //\r
70 // Create GUID HOB Data.\r
71 //\r
72 GuidHob = GetFirstGuidHob (&gEdkiiFpdtExtendedFirmwarePerformanceGuid);\r
73 PeiFirmwarePerformance = NULL;\r
74 while (GuidHob != NULL) {\r
75 //\r
76 // PEI Performance HOB was found, then return the existing one.\r
77 //\r
78 PeiFirmwarePerformance = (UINT8*)GET_GUID_HOB_DATA (GuidHob);\r
79 *PeiPerformanceLogHeader = (FPDT_PEI_EXT_PERF_HEADER *)PeiFirmwarePerformance;\r
981b7edc 80 if (!(*PeiPerformanceLogHeader)->HobIsFull && (*PeiPerformanceLogHeader)->SizeOfAllEntries + RecordSize > (PeiPerformanceLogEntries * PEI_MAX_RECORD_SIZE)) {\r
6b4d58a1
BD
81 (*PeiPerformanceLogHeader)->HobIsFull = TRUE;\r
82 }\r
981b7edc 83 if (!(*PeiPerformanceLogHeader)->HobIsFull && (*PeiPerformanceLogHeader)->SizeOfAllEntries + RecordSize <= (PeiPerformanceLogEntries * PEI_MAX_RECORD_SIZE)) {\r
6b4d58a1
BD
84 FpdtRecordPtr->RecordHeader = (EFI_ACPI_5_0_FPDT_PERFORMANCE_RECORD_HEADER *)(PeiFirmwarePerformance + sizeof (FPDT_PEI_EXT_PERF_HEADER) + (*PeiPerformanceLogHeader)->SizeOfAllEntries);\r
85 break;\r
86 }\r
87 //\r
88 // Previous HOB is used, then find next one.\r
89 //\r
90 GuidHob = GetNextGuidHob (&gEdkiiFpdtExtendedFirmwarePerformanceGuid, GET_NEXT_HOB (GuidHob));\r
91 }\r
92\r
93 if (GuidHob == NULL) {\r
94 //\r
95 // PEI Performance HOB was not found, then build one.\r
96 //\r
97 PeiPerformanceSize = sizeof (FPDT_PEI_EXT_PERF_HEADER) +\r
98 PEI_MAX_RECORD_SIZE * PeiPerformanceLogEntries;\r
99 PeiFirmwarePerformance = (UINT8*)BuildGuidHob (&gEdkiiFpdtExtendedFirmwarePerformanceGuid, PeiPerformanceSize);\r
100 if (PeiFirmwarePerformance != NULL) {\r
101 ZeroMem (PeiFirmwarePerformance, PeiPerformanceSize);\r
102 (*PeiPerformanceLogHeader) = (FPDT_PEI_EXT_PERF_HEADER *)PeiFirmwarePerformance;\r
103 FpdtRecordPtr->RecordHeader = (EFI_ACPI_5_0_FPDT_PERFORMANCE_RECORD_HEADER *)(PeiFirmwarePerformance + sizeof (FPDT_PEI_EXT_PERF_HEADER));\r
104 }\r
105 }\r
106\r
107 if (PeiFirmwarePerformance == NULL) {\r
108 //\r
109 // there is no enough resource to store performance data\r
110 //\r
111 return EFI_OUT_OF_RESOURCES;\r
112 }\r
113\r
114 return EFI_SUCCESS;\r
115}\r
a0afd019 116\r
117/**\r
9169f676 118Check whether the Token is a known one which is uesed by core.\r
a0afd019 119\r
9169f676 120@param Token Pointer to a Null-terminated ASCII string\r
a0afd019 121\r
9169f676
DB
122@retval TRUE Is a known one used by core.\r
123@retval FALSE Not a known one.\r
a0afd019 124\r
125**/\r
9169f676
DB
126BOOLEAN\r
127IsKnownTokens (\r
128 IN CONST CHAR8 *Token\r
a0afd019 129 )\r
130{\r
c9faac27
DB
131 if (Token == NULL) {\r
132 return FALSE;\r
133 }\r
134\r
9169f676
DB
135 if (AsciiStrCmp (Token, SEC_TOK) == 0 ||\r
136 AsciiStrCmp (Token, PEI_TOK) == 0 ||\r
137 AsciiStrCmp (Token, DXE_TOK) == 0 ||\r
138 AsciiStrCmp (Token, BDS_TOK) == 0 ||\r
139 AsciiStrCmp (Token, DRIVERBINDING_START_TOK) == 0 ||\r
140 AsciiStrCmp (Token, DRIVERBINDING_SUPPORT_TOK) == 0 ||\r
141 AsciiStrCmp (Token, DRIVERBINDING_STOP_TOK) == 0 ||\r
142 AsciiStrCmp (Token, LOAD_IMAGE_TOK) == 0 ||\r
143 AsciiStrCmp (Token, START_IMAGE_TOK) == 0 ||\r
144 AsciiStrCmp (Token, PEIM_TOK) == 0) {\r
145 return TRUE;\r
146 } else {\r
147 return FALSE;\r
148 }\r
149}\r
f0da4d7d 150\r
9169f676
DB
151/**\r
152Check whether the ID is a known one which map to the known Token.\r
a0afd019 153\r
9169f676 154@param Identifier 32-bit identifier.\r
a0afd019 155\r
9169f676
DB
156@retval TRUE Is a known one used by core.\r
157@retval FALSE Not a known one.\r
f0da4d7d 158\r
9169f676
DB
159**/\r
160BOOLEAN\r
161IsKnownID (\r
162 IN UINT32 Identifier\r
163 )\r
164{\r
165 if (Identifier == MODULE_START_ID ||\r
166 Identifier == MODULE_END_ID ||\r
167 Identifier == MODULE_LOADIMAGE_START_ID ||\r
168 Identifier == MODULE_LOADIMAGE_END_ID ||\r
169 Identifier == MODULE_DB_START_ID ||\r
170 Identifier == MODULE_DB_END_ID ||\r
171 Identifier == MODULE_DB_SUPPORT_START_ID ||\r
172 Identifier == MODULE_DB_SUPPORT_END_ID ||\r
173 Identifier == MODULE_DB_STOP_START_ID ||\r
174 Identifier == MODULE_DB_STOP_END_ID) {\r
175 return TRUE;\r
a0afd019 176 } else {\r
9169f676 177 return FALSE;\r
a0afd019 178 }\r
a0afd019 179}\r
180\r
181/**\r
6b4d58a1 182 Get the FPDT record identifier.\r
a0afd019 183\r
6b4d58a1
BD
184 @param Attribute The attribute of the Record.\r
185 PerfStartEntry: Start Record.\r
186 PerfEndEntry: End Record.\r
187 @param Handle Pointer to environment specific context used to identify the component being measured.\r
188 @param String Pointer to a Null-terminated ASCII string that identifies the component being measured.\r
189 @param ProgressID On return, pointer to the ProgressID.\r
a0afd019 190\r
6b4d58a1
BD
191 @retval EFI_SUCCESS Get record info successfully.\r
192 @retval EFI_INVALID_PARAMETER No matched FPDT record.\r
a0afd019 193\r
194**/\r
9169f676 195EFI_STATUS\r
6b4d58a1
BD
196GetFpdtRecordId (\r
197 IN BOOLEAN Attribute,\r
9169f676 198 IN CONST VOID *Handle,\r
6b4d58a1
BD
199 IN CONST CHAR8 *String,\r
200 OUT UINT16 *ProgressID\r
a0afd019 201 )\r
202{\r
9169f676
DB
203 //\r
204 // Get the ProgressID based on the Token.\r
205 // When PcdEdkiiFpdtStringRecordEnableOnly is TRUE, all records are with type of FPDT_DYNAMIC_STRING_EVENT_TYPE.\r
206 //\r
6b4d58a1
BD
207 if (String != NULL) {\r
208 if (AsciiStrCmp (String, LOAD_IMAGE_TOK) == 0) { // "LoadImage:"\r
209 if (Attribute == PerfStartEntry) {\r
210 *ProgressID = MODULE_LOADIMAGE_START_ID;\r
9169f676 211 } else {\r
6b4d58a1 212 *ProgressID = MODULE_LOADIMAGE_END_ID;\r
9169f676 213 }\r
6b4d58a1
BD
214 } else if (AsciiStrCmp (String, SEC_TOK) == 0 || // "SEC"\r
215 AsciiStrCmp (String, PEI_TOK) == 0) { // "PEI"\r
216 if (Attribute == PerfStartEntry) {\r
217 *ProgressID = PERF_CROSSMODULE_START_ID;\r
9169f676 218 } else {\r
6b4d58a1 219 *ProgressID = PERF_CROSSMODULE_END_ID;\r
9169f676 220 }\r
6b4d58a1
BD
221 } else if (AsciiStrCmp (String, PEIM_TOK) == 0) { // "PEIM"\r
222 if (Attribute == PerfStartEntry) {\r
223 *ProgressID = MODULE_START_ID;\r
9169f676 224 } else {\r
6b4d58a1 225 *ProgressID = MODULE_END_ID;\r
9169f676
DB
226 }\r
227 } else { //Pref used in Modules.\r
6b4d58a1
BD
228 if (Attribute == PerfStartEntry) {\r
229 *ProgressID = PERF_INMODULE_START_ID;\r
9169f676 230 } else {\r
6b4d58a1 231 *ProgressID = PERF_INMODULE_END_ID;\r
9169f676
DB
232 }\r
233 }\r
6b4d58a1
BD
234 } else if (Handle != NULL) { //Pref used in Modules.\r
235 if (Attribute == PerfStartEntry) {\r
236 *ProgressID = PERF_INMODULE_START_ID;\r
9169f676 237 } else {\r
6b4d58a1 238 *ProgressID = PERF_INMODULE_END_ID;\r
9169f676
DB
239 }\r
240 } else {\r
6b4d58a1 241 return EFI_INVALID_PARAMETER;\r
a0afd019 242 }\r
9169f676 243\r
6b4d58a1
BD
244 return EFI_SUCCESS;\r
245}\r
9169f676 246\r
6b4d58a1
BD
247/**\r
248 Copies the string from Source into Destination and updates Length with the\r
249 size of the string.\r
9169f676 250\r
6b4d58a1
BD
251 @param Destination - destination of the string copy\r
252 @param Source - pointer to the source string which will get copied\r
253 @param Length - pointer to a length variable to be updated\r
9169f676 254\r
6b4d58a1
BD
255**/\r
256VOID\r
257CopyStringIntoPerfRecordAndUpdateLength (\r
258 IN OUT CHAR8 *Destination,\r
259 IN CONST CHAR8 *Source,\r
260 IN OUT UINT8 *Length\r
261 )\r
262{\r
263 UINTN StringLen;\r
264 UINTN DestMax;\r
265\r
266 ASSERT (Source != NULL);\r
267\r
268 if (PcdGetBool (PcdEdkiiFpdtStringRecordEnableOnly)) {\r
269 DestMax = STRING_SIZE;\r
270 } else {\r
271 DestMax = AsciiStrSize (Source);\r
272 if (DestMax > STRING_SIZE) {\r
273 DestMax = STRING_SIZE;\r
a0afd019 274 }\r
275 }\r
6b4d58a1
BD
276 StringLen = AsciiStrLen (Source);\r
277 if (StringLen >= DestMax) {\r
278 StringLen = DestMax -1;\r
279 }\r
280\r
281 AsciiStrnCpyS(Destination, DestMax, Source, StringLen);\r
282 *Length += (UINT8)DestMax;\r
283\r
284 return;\r
a0afd019 285}\r
286\r
6b4d58a1 287\r
a0afd019 288/**\r
9169f676 289 Convert PEI performance log to FPDT String boot record.\r
a0afd019 290\r
6b4d58a1
BD
291 @param CallerIdentifier - Image handle or pointer to caller ID GUID.\r
292 @param Guid - Pointer to a GUID.\r
293 @param String - Pointer to a string describing the measurement.\r
294 @param Ticker - 64-bit time stamp.\r
295 @param Address - Pointer to a location in memory relevant to the measurement.\r
296 @param PerfId - Performance identifier describing the type of measurement.\r
297 @param Attribute - The attribute of the measurement. According to attribute can create a start\r
298 record for PERF_START/PERF_START_EX, or a end record for PERF_END/PERF_END_EX,\r
299 or a general record for other Perf macros.\r
300\r
301 @retval EFI_SUCCESS - Successfully created performance record.\r
302 @retval EFI_OUT_OF_RESOURCES - Ran out of space to store the records.\r
303 @retval EFI_INVALID_PARAMETER - Invalid parameter passed to function - NULL\r
304 pointer or invalid PerfId.\r
a0afd019 305\r
306**/\r
9169f676 307EFI_STATUS\r
6b4d58a1
BD
308InsertFpdtRecord (\r
309 IN CONST VOID *CallerIdentifier, OPTIONAL\r
310 IN CONST VOID *Guid, OPTIONAL\r
311 IN CONST CHAR8 *String, OPTIONAL\r
312 IN UINT64 Ticker,\r
313 IN UINT64 Address, OPTIONAL\r
314 IN UINT16 PerfId,\r
315 IN PERF_MEASUREMENT_ATTRIBUTE Attribute\r
a0afd019 316 )\r
317{\r
9169f676 318 FPDT_RECORD_PTR FpdtRecordPtr;\r
9169f676 319 CONST VOID *ModuleGuid;\r
9169f676
DB
320 CONST CHAR8 *StringPtr;\r
321 EFI_STATUS Status;\r
9169f676 322 UINT64 TimeStamp;\r
6b4d58a1 323 FPDT_PEI_EXT_PERF_HEADER *PeiPerformanceLogHeader;\r
9169f676
DB
324\r
325 StringPtr = NULL;\r
326 FpdtRecordPtr.RecordHeader = NULL;\r
327 PeiPerformanceLogHeader = NULL;\r
328\r
329 //\r
6b4d58a1
BD
330 // 1. Get the Perf Id for records from PERF_START/PERF_END, PERF_START_EX/PERF_END_EX.\r
331 // notes: For other Perf macros (Attribute == PerfEntry), their Id is known.\r
9169f676 332 //\r
6b4d58a1 333 if (Attribute != PerfEntry) {\r
9169f676 334 //\r
6b4d58a1
BD
335 // If PERF_START_EX()/PERF_END_EX() have specified the ProgressID,it has high priority.\r
336 // !!! Note: If the Perf is not the known Token used in the core but have same\r
337 // ID with the core Token, this case will not be supported.\r
338 // And in currtnt usage mode, for the unkown ID, there is a general rule:\r
339 // If it is start pref: the lower 4 bits of the ID should be 0.\r
340 // If it is end pref: the lower 4 bits of the ID should not be 0.\r
341 // If input ID doesn't follow the rule, we will adjust it.\r
9169f676 342 //\r
6b4d58a1
BD
343 if ((PerfId != 0) && (IsKnownID (PerfId)) && (!IsKnownTokens (String))) {\r
344 return EFI_UNSUPPORTED;\r
345 } else if ((PerfId != 0) && (!IsKnownID (PerfId)) && (!IsKnownTokens (String))) {\r
346 if (Attribute == PerfStartEntry && ((PerfId & 0x000F) != 0)) {\r
347 PerfId &= 0xFFF0;\r
348 } else if ((Attribute == PerfEndEntry) && ((PerfId & 0x000F) == 0)) {\r
349 PerfId += 1;\r
350 }\r
351 } else if (PerfId == 0) {\r
352 Status = GetFpdtRecordId (Attribute, CallerIdentifier, String, &PerfId);\r
353 if (EFI_ERROR (Status)) {\r
354 return Status;\r
355 }\r
9169f676 356 }\r
a0afd019 357 }\r
a0afd019 358\r
6b4d58a1
BD
359 //\r
360 // 2. Get the buffer to store the FPDT record.\r
361 //\r
362 Status = GetFpdtRecordPtr (PEI_MAX_RECORD_SIZE, &FpdtRecordPtr, &PeiPerformanceLogHeader);\r
363 if (EFI_ERROR (Status)) {\r
364 return Status;\r
9169f676
DB
365 }\r
366\r
367 //\r
6b4d58a1 368 // 3 Get the TimeStamp.\r
9169f676
DB
369 //\r
370 if (Ticker == 0) {\r
371 Ticker = GetPerformanceCounter ();\r
372 TimeStamp = GetTimeInNanoSecond (Ticker);\r
373 } else if (Ticker == 1) {\r
374 TimeStamp = 0;\r
375 } else {\r
376 TimeStamp = GetTimeInNanoSecond (Ticker);\r
a0afd019 377 }\r
9169f676
DB
378\r
379 //\r
6b4d58a1 380 // 4.Get the ModuleGuid.\r
9169f676 381 //\r
6b4d58a1
BD
382 if (CallerIdentifier != NULL) {\r
383 ModuleGuid = CallerIdentifier;\r
9169f676
DB
384 } else {\r
385 ModuleGuid = &gEfiCallerIdGuid;\r
a0afd019 386 }\r
387\r
6b4d58a1
BD
388 //\r
389 // 5. Fill in the FPDT record according to different Performance Identifier.\r
390 //\r
391 switch (PerfId) {\r
392 case MODULE_START_ID:\r
393 case MODULE_END_ID:\r
394 StringPtr = PEIM_TOK;\r
395 if (!PcdGetBool (PcdEdkiiFpdtStringRecordEnableOnly)) {\r
396 FpdtRecordPtr.GuidEvent->Header.Type = FPDT_GUID_EVENT_TYPE;\r
397 FpdtRecordPtr.GuidEvent->Header.Length = sizeof (FPDT_GUID_EVENT_RECORD);\r
398 FpdtRecordPtr.GuidEvent->Header.Revision = FPDT_RECORD_REVISION_1;\r
399 FpdtRecordPtr.GuidEvent->ProgressID = PerfId;\r
400 FpdtRecordPtr.GuidEvent->Timestamp = TimeStamp;\r
401 CopyMem (&FpdtRecordPtr.GuidEvent->Guid, ModuleGuid, sizeof (EFI_GUID));\r
402 }\r
9169f676
DB
403 break;\r
404\r
6b4d58a1
BD
405 case MODULE_LOADIMAGE_START_ID:\r
406 case MODULE_LOADIMAGE_END_ID:\r
407 StringPtr = LOAD_IMAGE_TOK;\r
408 if (!PcdGetBool (PcdEdkiiFpdtStringRecordEnableOnly)) {\r
409 FpdtRecordPtr.GuidQwordEvent->Header.Type = FPDT_GUID_QWORD_EVENT_TYPE;\r
410 FpdtRecordPtr.GuidQwordEvent->Header.Length = sizeof (FPDT_GUID_QWORD_EVENT_RECORD);\r
411 FpdtRecordPtr.GuidQwordEvent->Header.Revision = FPDT_RECORD_REVISION_1;\r
412 FpdtRecordPtr.GuidQwordEvent->ProgressID = PerfId;\r
413 FpdtRecordPtr.GuidQwordEvent->Timestamp = TimeStamp;\r
414 if (PerfId == MODULE_LOADIMAGE_START_ID) {\r
415 PeiPerformanceLogHeader->LoadImageCount++;\r
416 }\r
417 FpdtRecordPtr.GuidQwordEvent->Qword = PeiPerformanceLogHeader->LoadImageCount;\r
418 CopyMem (&FpdtRecordPtr.GuidQwordEvent->Guid, ModuleGuid, sizeof (EFI_GUID));\r
419 }\r
9169f676
DB
420 break;\r
421\r
6b4d58a1
BD
422 case PERF_EVENTSIGNAL_START_ID:\r
423 case PERF_EVENTSIGNAL_END_ID:\r
424 case PERF_CALLBACK_START_ID:\r
425 case PERF_CALLBACK_END_ID:\r
981b7edc
DB
426 if (String == NULL || Guid == NULL) {\r
427 return EFI_INVALID_PARAMETER;\r
428 }\r
429 StringPtr = String;\r
430 if (AsciiStrLen (String) == 0) {\r
6b4d58a1 431 StringPtr = "unknown name";\r
9169f676 432 }\r
6b4d58a1
BD
433 if (!PcdGetBool (PcdEdkiiFpdtStringRecordEnableOnly)) {\r
434 FpdtRecordPtr.DualGuidStringEvent->Header.Type = FPDT_DUAL_GUID_STRING_EVENT_TYPE;\r
435 FpdtRecordPtr.DualGuidStringEvent->Header.Length = sizeof (FPDT_DUAL_GUID_STRING_EVENT_RECORD);\r
436 FpdtRecordPtr.DualGuidStringEvent->Header.Revision = FPDT_RECORD_REVISION_1;\r
437 FpdtRecordPtr.DualGuidStringEvent->ProgressID = PerfId;\r
438 FpdtRecordPtr.DualGuidStringEvent->Timestamp = TimeStamp;\r
439 CopyMem (&FpdtRecordPtr.DualGuidStringEvent->Guid1, ModuleGuid, sizeof (FpdtRecordPtr.DualGuidStringEvent->Guid1));\r
440 CopyMem (&FpdtRecordPtr.DualGuidStringEvent->Guid2, Guid, sizeof (FpdtRecordPtr.DualGuidStringEvent->Guid2));\r
981b7edc 441 CopyStringIntoPerfRecordAndUpdateLength (FpdtRecordPtr.DualGuidStringEvent->String, StringPtr, &FpdtRecordPtr.DualGuidStringEvent->Header.Length);\r
6b4d58a1
BD
442 }\r
443 break;\r
444\r
445 case PERF_EVENT_ID:\r
446 case PERF_FUNCTION_START_ID:\r
447 case PERF_FUNCTION_END_ID:\r
448 case PERF_INMODULE_START_ID:\r
449 case PERF_INMODULE_END_ID:\r
450 case PERF_CROSSMODULE_START_ID:\r
451 case PERF_CROSSMODULE_END_ID:\r
452 if (String != NULL && AsciiStrLen (String) != 0) {\r
453 StringPtr = String;\r
9169f676 454 } else {\r
6b4d58a1
BD
455 StringPtr = "unknown name";\r
456 }\r
457 if (!PcdGetBool (PcdEdkiiFpdtStringRecordEnableOnly)) {\r
458 FpdtRecordPtr.DynamicStringEvent->Header.Type = FPDT_DYNAMIC_STRING_EVENT_TYPE;\r
459 FpdtRecordPtr.DynamicStringEvent->Header.Length = sizeof (FPDT_DYNAMIC_STRING_EVENT_RECORD);\r
460 FpdtRecordPtr.DynamicStringEvent->Header.Revision = FPDT_RECORD_REVISION_1;\r
461 FpdtRecordPtr.DynamicStringEvent->ProgressID = PerfId;\r
462 FpdtRecordPtr.DynamicStringEvent->Timestamp = TimeStamp;\r
463 CopyMem (&FpdtRecordPtr.DynamicStringEvent->Guid, ModuleGuid, sizeof (EFI_GUID));\r
464 CopyStringIntoPerfRecordAndUpdateLength (FpdtRecordPtr.DynamicStringEvent->String, StringPtr, &FpdtRecordPtr.DynamicStringEvent->Header.Length);\r
9169f676
DB
465 }\r
466 break;\r
f0da4d7d 467\r
9169f676 468 default:\r
6b4d58a1
BD
469 if (Attribute != PerfEntry) {\r
470 if (String != NULL && AsciiStrLen (String) != 0) {\r
471 StringPtr = String;\r
472 } else {\r
473 StringPtr = "unknown name";\r
474 }\r
475 if (!PcdGetBool (PcdEdkiiFpdtStringRecordEnableOnly)) {\r
476 FpdtRecordPtr.DynamicStringEvent->Header.Type = FPDT_DYNAMIC_STRING_EVENT_TYPE;\r
477 FpdtRecordPtr.DynamicStringEvent->Header.Length = sizeof (FPDT_DYNAMIC_STRING_EVENT_RECORD);\r
478 FpdtRecordPtr.DynamicStringEvent->Header.Revision = FPDT_RECORD_REVISION_1;\r
479 FpdtRecordPtr.DynamicStringEvent->ProgressID = PerfId;\r
480 FpdtRecordPtr.DynamicStringEvent->Timestamp = TimeStamp;\r
481 CopyMem (&FpdtRecordPtr.DynamicStringEvent->Guid, ModuleGuid, sizeof (FpdtRecordPtr.DynamicStringEvent->Guid));\r
482 CopyStringIntoPerfRecordAndUpdateLength (FpdtRecordPtr.DynamicStringEvent->String, StringPtr, &FpdtRecordPtr.DynamicStringEvent->Header.Length);\r
483 }\r
484 } else {\r
485 return EFI_INVALID_PARAMETER;\r
486 }\r
487 break;\r
488 }\r
489\r
490 //\r
491 // 5.2 When PcdEdkiiFpdtStringRecordEnableOnly==TRUE, create string record for all Perf entries.\r
492 //\r
493 if (PcdGetBool (PcdEdkiiFpdtStringRecordEnableOnly)) {\r
494 FpdtRecordPtr.DynamicStringEvent->Header.Type = FPDT_DYNAMIC_STRING_EVENT_TYPE;\r
495 FpdtRecordPtr.DynamicStringEvent->Header.Length = sizeof (FPDT_DYNAMIC_STRING_EVENT_RECORD);\r
496 FpdtRecordPtr.DynamicStringEvent->Header.Revision = FPDT_RECORD_REVISION_1;\r
497 FpdtRecordPtr.DynamicStringEvent->ProgressID = PerfId;\r
498 FpdtRecordPtr.DynamicStringEvent->Timestamp = TimeStamp;\r
499 if (Guid != NULL) {\r
500 //\r
501 // Cache the event guid in string event record.\r
502 //\r
503 CopyMem (&FpdtRecordPtr.DynamicStringEvent->Guid, Guid, sizeof (EFI_GUID));\r
504 } else {\r
505 CopyMem (&FpdtRecordPtr.DynamicStringEvent->Guid, ModuleGuid, sizeof (EFI_GUID));\r
506 }\r
507 CopyStringIntoPerfRecordAndUpdateLength (FpdtRecordPtr.DynamicStringEvent->String, StringPtr, &FpdtRecordPtr.DynamicStringEvent->Header.Length);\r
a0afd019 508 }\r
a0afd019 509\r
6b4d58a1
BD
510 //\r
511 // 6. Update the length of the used buffer after fill in the record.\r
512 //\r
513 PeiPerformanceLogHeader->SizeOfAllEntries += FpdtRecordPtr.RecordHeader->Length;\r
514\r
9169f676 515 return EFI_SUCCESS;\r
a0afd019 516}\r
517\r
518/**\r
9169f676
DB
519 Creates a record for the beginning of a performance measurement.\r
520\r
521 If TimeStamp is zero, then this function reads the current time stamp\r
522 and adds that time stamp value to the record as the start time.\r
523\r
524 If TimeStamp is one, then this function reads 0 as the start time.\r
a0afd019 525\r
9169f676
DB
526 If TimeStamp is other value, then TimeStamp is added to the record as the start time.\r
527\r
528 @param Handle Pointer to environment specific context used\r
529 to identify the component being measured.\r
530 @param Token Pointer to a Null-terminated ASCII string\r
531 that identifies the component being measured.\r
532 @param Module Pointer to a Null-terminated ASCII string\r
533 that identifies the module being measured.\r
534 @param TimeStamp 64-bit time stamp.\r
535 @param Identifier 32-bit identifier. If the value is 0, the created record\r
536 is same as the one created by StartPerformanceMeasurement.\r
537\r
538 @retval RETURN_SUCCESS The start of the measurement was recorded.\r
539 @retval RETURN_OUT_OF_RESOURCES There are not enough resources to record the measurement.\r
540\r
541**/\r
542RETURN_STATUS\r
543EFIAPI\r
544StartPerformanceMeasurementEx (\r
545 IN CONST VOID *Handle, OPTIONAL\r
546 IN CONST CHAR8 *Token, OPTIONAL\r
547 IN CONST CHAR8 *Module, OPTIONAL\r
548 IN UINT64 TimeStamp,\r
549 IN UINT32 Identifier\r
550 )\r
551{\r
6b4d58a1
BD
552 CONST CHAR8 *String;\r
553\r
554 if (Token != NULL) {\r
555 String = Token;\r
556 } else if (Module != NULL) {\r
557 String = Module;\r
558 } else {\r
559 String = NULL;\r
560 }\r
561\r
562 return (RETURN_STATUS)InsertFpdtRecord (Handle, NULL, String, TimeStamp, 0, (UINT16)Identifier, PerfStartEntry);\r
563\r
9169f676
DB
564}\r
565\r
566/**\r
567\r
568 Creates a record for the end of a performance measurement.\r
569\r
570 If the TimeStamp is not zero or one, then TimeStamp is added to the record as the end time.\r
571 If the TimeStamp is zero, then this function reads the current time stamp and adds that time stamp value to the record as the end time.\r
572 If the TimeStamp is one, then this function reads 0 as the end time.\r
a0afd019 573\r
574 @param Handle Pointer to environment specific context used\r
575 to identify the component being measured.\r
576 @param Token Pointer to a Null-terminated ASCII string\r
577 that identifies the component being measured.\r
578 @param Module Pointer to a Null-terminated ASCII string\r
579 that identifies the module being measured.\r
580 @param TimeStamp 64-bit time stamp.\r
f0da4d7d
SZ
581 @param Identifier 32-bit identifier. If the value is 0, the found record\r
582 is same as the one found by EndPerformanceMeasurement.\r
a0afd019 583\r
584 @retval RETURN_SUCCESS The end of the measurement was recorded.\r
585 @retval RETURN_NOT_FOUND The specified measurement record could not be found.\r
586\r
587**/\r
588RETURN_STATUS\r
589EFIAPI\r
f0da4d7d 590EndPerformanceMeasurementEx (\r
a0afd019 591 IN CONST VOID *Handle, OPTIONAL\r
592 IN CONST CHAR8 *Token, OPTIONAL\r
593 IN CONST CHAR8 *Module, OPTIONAL\r
f0da4d7d
SZ
594 IN UINT64 TimeStamp,\r
595 IN UINT32 Identifier\r
a0afd019 596 )\r
597{\r
6b4d58a1
BD
598 CONST CHAR8 *String;\r
599\r
600 if (Token != NULL) {\r
601 String = Token;\r
602 } else if (Module != NULL) {\r
603 String = Module;\r
604 } else {\r
605 String = NULL;\r
606 }\r
607\r
608 return (RETURN_STATUS)InsertFpdtRecord (Handle, NULL, String, TimeStamp, 0, (UINT16)Identifier, PerfEndEntry);\r
a0afd019 609}\r
610\r
611/**\r
612 Attempts to retrieve a performance measurement log entry from the performance measurement log.\r
f0da4d7d
SZ
613 It can also retrieve the log created by StartPerformanceMeasurement and EndPerformanceMeasurement,\r
614 and then assign the Identifier with 0.\r
a0afd019 615\r
616 Attempts to retrieve the performance log entry specified by LogEntryKey. If LogEntryKey is\r
617 zero on entry, then an attempt is made to retrieve the first entry from the performance log,\r
618 and the key for the second entry in the log is returned. If the performance log is empty,\r
619 then no entry is retrieved and zero is returned. If LogEntryKey is not zero, then the performance\r
620 log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is\r
621 returned. If LogEntryKey is the key for the last entry in the log, then the last log entry is\r
622 retrieved and an implementation specific non-zero key value that specifies the end of the performance\r
623 log is returned. If LogEntryKey is equal this implementation specific non-zero key value, then no entry\r
624 is retrieved and zero is returned. In the cases where a performance log entry can be returned,\r
f0da4d7d 625 the log entry is returned in Handle, Token, Module, StartTimeStamp, EndTimeStamp and Identifier.\r
a0afd019 626 If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().\r
627 If Handle is NULL, then ASSERT().\r
628 If Token is NULL, then ASSERT().\r
629 If Module is NULL, then ASSERT().\r
630 If StartTimeStamp is NULL, then ASSERT().\r
631 If EndTimeStamp is NULL, then ASSERT().\r
f0da4d7d 632 If Identifier is NULL, then ASSERT().\r
a0afd019 633\r
9169f676
DB
634 !!!NOT Support yet!!!\r
635\r
a0afd019 636 @param LogEntryKey On entry, the key of the performance measurement log entry to retrieve.\r
637 0, then the first performance measurement log entry is retrieved.\r
f0da4d7d 638 On exit, the key of the next performance of entry entry.\r
a0afd019 639 @param Handle Pointer to environment specific context used to identify the component\r
640 being measured.\r
641 @param Token Pointer to a Null-terminated ASCII string that identifies the component\r
642 being measured.\r
643 @param Module Pointer to a Null-terminated ASCII string that identifies the module\r
644 being measured.\r
645 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement\r
646 was started.\r
647 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement\r
648 was ended.\r
f0da4d7d 649 @param Identifier Pointer to the 32-bit identifier that was recorded.\r
a0afd019 650\r
651 @return The key for the next performance log entry (in general case).\r
652\r
653**/\r
654UINTN\r
655EFIAPI\r
f0da4d7d 656GetPerformanceMeasurementEx (\r
a0afd019 657 IN UINTN LogEntryKey,\r
658 OUT CONST VOID **Handle,\r
659 OUT CONST CHAR8 **Token,\r
660 OUT CONST CHAR8 **Module,\r
661 OUT UINT64 *StartTimeStamp,\r
f0da4d7d
SZ
662 OUT UINT64 *EndTimeStamp,\r
663 OUT UINT32 *Identifier\r
a0afd019 664 )\r
665{\r
9169f676 666 return 0;\r
a0afd019 667}\r
668\r
f0da4d7d
SZ
669/**\r
670 Creates a record for the beginning of a performance measurement.\r
671\r
f0da4d7d
SZ
672 If TimeStamp is zero, then this function reads the current time stamp\r
673 and adds that time stamp value to the record as the start time.\r
674\r
9169f676
DB
675 If TimeStamp is one, then this function reads 0 as the start time.\r
676\r
677 If TimeStamp is other value, then TimeStamp is added to the record as the start time.\r
678\r
679\r
f0da4d7d
SZ
680 @param Handle Pointer to environment specific context used\r
681 to identify the component being measured.\r
682 @param Token Pointer to a Null-terminated ASCII string\r
683 that identifies the component being measured.\r
684 @param Module Pointer to a Null-terminated ASCII string\r
685 that identifies the module being measured.\r
686 @param TimeStamp 64-bit time stamp.\r
687\r
688 @retval RETURN_SUCCESS The start of the measurement was recorded.\r
689 @retval RETURN_OUT_OF_RESOURCES There are not enough resources to record the measurement.\r
690\r
691**/\r
692RETURN_STATUS\r
693EFIAPI\r
694StartPerformanceMeasurement (\r
695 IN CONST VOID *Handle, OPTIONAL\r
696 IN CONST CHAR8 *Token, OPTIONAL\r
697 IN CONST CHAR8 *Module, OPTIONAL\r
698 IN UINT64 TimeStamp\r
699 )\r
700{\r
6b4d58a1 701 return StartPerformanceMeasurementEx (Handle, Token, Module, TimeStamp, 0);\r
f0da4d7d
SZ
702}\r
703\r
704/**\r
f0da4d7d 705\r
9169f676
DB
706 Creates a record for the end of a performance measurement.\r
707\r
708 If the TimeStamp is not zero or one, then TimeStamp is added to the record as the end time.\r
709 If the TimeStamp is zero, then this function reads the current time stamp and adds that time stamp value to the record as the end time.\r
710 If the TimeStamp is one, then this function reads 0 as the end time.\r
f0da4d7d
SZ
711\r
712 @param Handle Pointer to environment specific context used\r
713 to identify the component being measured.\r
714 @param Token Pointer to a Null-terminated ASCII string\r
715 that identifies the component being measured.\r
716 @param Module Pointer to a Null-terminated ASCII string\r
717 that identifies the module being measured.\r
718 @param TimeStamp 64-bit time stamp.\r
719\r
720 @retval RETURN_SUCCESS The end of the measurement was recorded.\r
721 @retval RETURN_NOT_FOUND The specified measurement record could not be found.\r
722\r
723**/\r
724RETURN_STATUS\r
725EFIAPI\r
726EndPerformanceMeasurement (\r
727 IN CONST VOID *Handle, OPTIONAL\r
728 IN CONST CHAR8 *Token, OPTIONAL\r
729 IN CONST CHAR8 *Module, OPTIONAL\r
730 IN UINT64 TimeStamp\r
731 )\r
732{\r
6b4d58a1 733 return EndPerformanceMeasurementEx (Handle, Token, Module, TimeStamp, 0);\r
f0da4d7d
SZ
734}\r
735\r
736/**\r
737 Attempts to retrieve a performance measurement log entry from the performance measurement log.\r
738 It can also retrieve the log created by StartPerformanceMeasurementEx and EndPerformanceMeasurementEx,\r
739 and then eliminate the Identifier.\r
740\r
741 Attempts to retrieve the performance log entry specified by LogEntryKey. If LogEntryKey is\r
742 zero on entry, then an attempt is made to retrieve the first entry from the performance log,\r
743 and the key for the second entry in the log is returned. If the performance log is empty,\r
744 then no entry is retrieved and zero is returned. If LogEntryKey is not zero, then the performance\r
745 log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is\r
746 returned. If LogEntryKey is the key for the last entry in the log, then the last log entry is\r
747 retrieved and an implementation specific non-zero key value that specifies the end of the performance\r
748 log is returned. If LogEntryKey is equal this implementation specific non-zero key value, then no entry\r
749 is retrieved and zero is returned. In the cases where a performance log entry can be returned,\r
750 the log entry is returned in Handle, Token, Module, StartTimeStamp, and EndTimeStamp.\r
751 If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().\r
752 If Handle is NULL, then ASSERT().\r
753 If Token is NULL, then ASSERT().\r
754 If Module is NULL, then ASSERT().\r
755 If StartTimeStamp is NULL, then ASSERT().\r
756 If EndTimeStamp is NULL, then ASSERT().\r
757\r
9169f676
DB
758 NOT Support yet.\r
759\r
f0da4d7d
SZ
760 @param LogEntryKey On entry, the key of the performance measurement log entry to retrieve.\r
761 0, then the first performance measurement log entry is retrieved.\r
762 On exit, the key of the next performance of entry entry.\r
763 @param Handle Pointer to environment specific context used to identify the component\r
764 being measured.\r
765 @param Token Pointer to a Null-terminated ASCII string that identifies the component\r
766 being measured.\r
767 @param Module Pointer to a Null-terminated ASCII string that identifies the module\r
768 being measured.\r
769 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement\r
770 was started.\r
771 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement\r
772 was ended.\r
773\r
774 @return The key for the next performance log entry (in general case).\r
775\r
776**/\r
777UINTN\r
778EFIAPI\r
779GetPerformanceMeasurement (\r
780 IN UINTN LogEntryKey,\r
781 OUT CONST VOID **Handle,\r
782 OUT CONST CHAR8 **Token,\r
783 OUT CONST CHAR8 **Module,\r
784 OUT UINT64 *StartTimeStamp,\r
785 OUT UINT64 *EndTimeStamp\r
786 )\r
787{\r
9169f676 788 return 0;\r
f0da4d7d
SZ
789}\r
790\r
a0afd019 791/**\r
792 Returns TRUE if the performance measurement macros are enabled.\r
793\r
794 This function returns TRUE if the PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of\r
795 PcdPerformanceLibraryPropertyMask is set. Otherwise FALSE is returned.\r
796\r
797 @retval TRUE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of\r
798 PcdPerformanceLibraryPropertyMask is set.\r
799 @retval FALSE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of\r
800 PcdPerformanceLibraryPropertyMask is clear.\r
801\r
802**/\r
803BOOLEAN\r
804EFIAPI\r
805PerformanceMeasurementEnabled (\r
806 VOID\r
807 )\r
808{\r
809 return (BOOLEAN) ((PcdGet8(PcdPerformanceLibraryPropertyMask) & PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED) != 0);\r
810}\r
6b4d58a1
BD
811\r
812/**\r
813 Create performance record with event description and a timestamp.\r
814\r
815 @param CallerIdentifier - Image handle or pointer to caller ID GUID\r
816 @param Guid - Pointer to a GUID\r
817 @param String - Pointer to a string describing the measurement\r
818 @param Address - Pointer to a location in memory relevant to the measurement\r
819 @param Identifier - Performance identifier describing the type of measurement\r
820\r
821 @retval RETURN_SUCCESS - Successfully created performance record\r
822 @retval RETURN_OUT_OF_RESOURCES - Ran out of space to store the records\r
823 @retval RETURN_INVALID_PARAMETER - Invalid parameter passed to function - NULL\r
824 pointer or invalid PerfId\r
825\r
826**/\r
827RETURN_STATUS\r
828EFIAPI\r
829LogPerformanceMeasurement (\r
830 IN CONST VOID *CallerIdentifier,\r
831 IN CONST VOID *Guid, OPTIONAL\r
832 IN CONST CHAR8 *String, OPTIONAL\r
833 IN UINT64 Address, OPTIONAL\r
834 IN UINT32 Identifier\r
835 )\r
836{\r
837 return (RETURN_STATUS)InsertFpdtRecord (CallerIdentifier, Guid, String, 0, Address, (UINT16)Identifier, PerfEntry);\r
838}\r
839\r
840/**\r
841 Check whether the specified performance measurement can be logged.\r
842\r
843 This function returns TRUE when the PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of PcdPerformanceLibraryPropertyMask is set\r
844 and the Type disable bit in PcdPerformanceLibraryPropertyMask is not set.\r
845\r
846 @param Type - Type of the performance measurement entry.\r
847\r
848 @retval TRUE The performance measurement can be logged.\r
849 @retval FALSE The performance measurement can NOT be logged.\r
850\r
851**/\r
852BOOLEAN\r
853EFIAPI\r
854LogPerformanceMeasurementEnabled (\r
855 IN CONST UINTN Type\r
856 )\r
857{\r
858 //\r
859 // When Performance measurement is enabled and the type is not filtered, the performance can be logged.\r
860 //\r
861 if (PerformanceMeasurementEnabled () && (PcdGet8(PcdPerformanceLibraryPropertyMask) & Type) == 0) {\r
862 return TRUE;\r
863 }\r
864 return FALSE;\r
865}\r