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