2 Performance library instance used in PEI phase.
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.
10 Copyright (c) 2006 - 2015, 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
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.
25 #include <Guid/Performance.h>
27 #include <Library/PerformanceLib.h>
28 #include <Library/DebugLib.h>
29 #include <Library/HobLib.h>
30 #include <Library/BaseLib.h>
31 #include <Library/TimerLib.h>
32 #include <Library/PcdLib.h>
33 #include <Library/BaseMemoryLib.h>
37 Gets the GUID HOB for PEI performance.
39 This internal function searches for the GUID HOB for PEI performance.
40 If that GUID HOB is not found, it will build a new one.
41 It outputs the data area of that GUID HOB to record performance log.
43 @param PeiPerformanceLog Pointer to Pointer to PEI performance log header.
44 @param PeiPerformanceIdArray Pointer to Pointer to PEI performance identifier array.
48 InternalGetPerformanceHobLog (
49 OUT PEI_PERFORMANCE_LOG_HEADER
**PeiPerformanceLog
,
50 OUT UINT32
**PeiPerformanceIdArray
53 EFI_HOB_GUID_TYPE
*GuidHob
;
54 UINTN PeiPerformanceSize
;
55 UINT16 PeiPerformanceLogEntries
;
57 ASSERT (PeiPerformanceLog
!= NULL
);
58 ASSERT (PeiPerformanceIdArray
!= NULL
);
60 PeiPerformanceLogEntries
= (UINT16
) (PcdGet16 (PcdMaxPeiPerformanceLogEntries16
) != 0 ?
61 PcdGet16 (PcdMaxPeiPerformanceLogEntries16
) :
62 PcdGet8 (PcdMaxPeiPerformanceLogEntries
));
63 GuidHob
= GetFirstGuidHob (&gPerformanceProtocolGuid
);
65 if (GuidHob
!= NULL
) {
67 // PEI Performance HOB was found, then return the existing one.
69 *PeiPerformanceLog
= GET_GUID_HOB_DATA (GuidHob
);
71 GuidHob
= GetFirstGuidHob (&gPerformanceExProtocolGuid
);
72 ASSERT (GuidHob
!= NULL
);
73 *PeiPerformanceIdArray
= GET_GUID_HOB_DATA (GuidHob
);
76 // PEI Performance HOB was not found, then build one.
78 PeiPerformanceSize
= sizeof (PEI_PERFORMANCE_LOG_HEADER
) +
79 sizeof (PEI_PERFORMANCE_LOG_ENTRY
) * PeiPerformanceLogEntries
;
80 *PeiPerformanceLog
= BuildGuidHob (&gPerformanceProtocolGuid
, PeiPerformanceSize
);
81 *PeiPerformanceLog
= ZeroMem (*PeiPerformanceLog
, PeiPerformanceSize
);
83 PeiPerformanceSize
= sizeof (UINT32
) * PeiPerformanceLogEntries
;
84 *PeiPerformanceIdArray
= BuildGuidHob (&gPerformanceExProtocolGuid
, PeiPerformanceSize
);
85 *PeiPerformanceIdArray
= ZeroMem (*PeiPerformanceIdArray
, PeiPerformanceSize
);
90 Searches in the log array with keyword Handle, Token, Module and Identifier.
92 This internal function searches for the log entry in the log array.
93 If there is an entry that exactly matches the given keywords
94 and its end time stamp is zero, then the index of that log entry is returned;
95 otherwise, the the number of log entries in the array is returned.
97 @param PeiPerformanceLog Pointer to the data structure containing PEI
99 @param PeiPerformanceIdArray Pointer to PEI performance identifier array.
100 @param Handle Pointer to environment specific context used
101 to identify the component being measured.
102 @param Token Pointer to a Null-terminated ASCII string
103 that identifies the component being measured.
104 @param Module Pointer to a Null-terminated ASCII string
105 that identifies the module being measured.
106 @param Identifier 32-bit identifier.
108 @retval The index of log entry in the array.
112 InternalSearchForLogEntry (
113 IN PEI_PERFORMANCE_LOG_HEADER
*PeiPerformanceLog
,
114 IN UINT32
*PeiPerformanceIdArray
,
115 IN CONST VOID
*Handle
, OPTIONAL
116 IN CONST CHAR8
*Token
, OPTIONAL
117 IN CONST CHAR8
*Module
, OPTIONAL
123 UINT32 NumberOfEntries
;
124 PEI_PERFORMANCE_LOG_ENTRY
*LogEntryArray
;
130 if (Module
== NULL
) {
133 NumberOfEntries
= PeiPerformanceLog
->NumberOfEntries
;
134 LogEntryArray
= (PEI_PERFORMANCE_LOG_ENTRY
*) (PeiPerformanceLog
+ 1);
138 for (Index
= 0; Index
< NumberOfEntries
; Index
++) {
139 Index2
= NumberOfEntries
- 1 - Index
;
140 if (LogEntryArray
[Index2
].EndTimeStamp
== 0 &&
141 (LogEntryArray
[Index2
].Handle
== (EFI_PHYSICAL_ADDRESS
) (UINTN
) Handle
) &&
142 AsciiStrnCmp (LogEntryArray
[Index2
].Token
, Token
, PEI_PERFORMANCE_STRING_LENGTH
) == 0 &&
143 AsciiStrnCmp (LogEntryArray
[Index2
].Module
, Module
, PEI_PERFORMANCE_STRING_LENGTH
) == 0 &&
144 (PeiPerformanceIdArray
[Index2
] == Identifier
)) {
153 Creates a record for the beginning of a performance measurement.
155 Creates a record that contains the Handle, Token, Module and Identifier.
156 If TimeStamp is not zero, then TimeStamp is added to the record as the start time.
157 If TimeStamp is zero, then this function reads the current time stamp
158 and adds that time stamp value to the record as the start time.
160 @param Handle Pointer to environment specific context used
161 to identify the component being measured.
162 @param Token Pointer to a Null-terminated ASCII string
163 that identifies the component being measured.
164 @param Module Pointer to a Null-terminated ASCII string
165 that identifies the module being measured.
166 @param TimeStamp 64-bit time stamp.
167 @param Identifier 32-bit identifier. If the value is 0, the created record
168 is same as the one created by StartPerformanceMeasurement.
170 @retval RETURN_SUCCESS The start of the measurement was recorded.
171 @retval RETURN_OUT_OF_RESOURCES There are not enough resources to record the measurement.
176 StartPerformanceMeasurementEx (
177 IN CONST VOID
*Handle
, OPTIONAL
178 IN CONST CHAR8
*Token
, OPTIONAL
179 IN CONST CHAR8
*Module
, OPTIONAL
184 PEI_PERFORMANCE_LOG_HEADER
*PeiPerformanceLog
;
185 UINT32
*PeiPerformanceIdArray
;
186 PEI_PERFORMANCE_LOG_ENTRY
*LogEntryArray
;
188 UINT16 PeiPerformanceLogEntries
;
190 PeiPerformanceLogEntries
= (UINT16
) (PcdGet16 (PcdMaxPeiPerformanceLogEntries16
) != 0 ?
191 PcdGet16 (PcdMaxPeiPerformanceLogEntries16
) :
192 PcdGet8 (PcdMaxPeiPerformanceLogEntries
));
194 InternalGetPerformanceHobLog (&PeiPerformanceLog
, &PeiPerformanceIdArray
);
196 if (PeiPerformanceLog
->NumberOfEntries
>= PeiPerformanceLogEntries
) {
197 DEBUG ((DEBUG_ERROR
, "PEI performance log array out of resources\n"));
198 return RETURN_OUT_OF_RESOURCES
;
200 Index
= PeiPerformanceLog
->NumberOfEntries
++;
201 LogEntryArray
= (PEI_PERFORMANCE_LOG_ENTRY
*) (PeiPerformanceLog
+ 1);
202 LogEntryArray
[Index
].Handle
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) Handle
;
205 AsciiStrnCpyS (LogEntryArray
[Index
].Token
, PEI_PERFORMANCE_STRING_SIZE
, Token
, PEI_PERFORMANCE_STRING_LENGTH
);
207 if (Module
!= NULL
) {
208 AsciiStrnCpyS (LogEntryArray
[Index
].Module
, PEI_PERFORMANCE_STRING_SIZE
, Module
, PEI_PERFORMANCE_STRING_LENGTH
);
211 LogEntryArray
[Index
].EndTimeStamp
= 0;
212 PeiPerformanceIdArray
[Index
] = Identifier
;
214 if (TimeStamp
== 0) {
215 TimeStamp
= GetPerformanceCounter ();
217 LogEntryArray
[Index
].StartTimeStamp
= TimeStamp
;
219 return RETURN_SUCCESS
;
223 Fills in the end time of a performance measurement.
225 Looks up the record that matches Handle, Token, Module and Identifier.
226 If the record can not be found then return RETURN_NOT_FOUND.
227 If the record is found and TimeStamp is not zero,
228 then TimeStamp is added to the record as the end time.
229 If the record is found and TimeStamp is zero, then this function reads
230 the current time stamp and adds that time stamp value to the record as the end time.
232 @param Handle Pointer to environment specific context used
233 to identify the component being measured.
234 @param Token Pointer to a Null-terminated ASCII string
235 that identifies the component being measured.
236 @param Module Pointer to a Null-terminated ASCII string
237 that identifies the module being measured.
238 @param TimeStamp 64-bit time stamp.
239 @param Identifier 32-bit identifier. If the value is 0, the found record
240 is same as the one found by EndPerformanceMeasurement.
242 @retval RETURN_SUCCESS The end of the measurement was recorded.
243 @retval RETURN_NOT_FOUND The specified measurement record could not be found.
248 EndPerformanceMeasurementEx (
249 IN CONST VOID
*Handle
, OPTIONAL
250 IN CONST CHAR8
*Token
, OPTIONAL
251 IN CONST CHAR8
*Module
, OPTIONAL
256 PEI_PERFORMANCE_LOG_HEADER
*PeiPerformanceLog
;
257 UINT32
*PeiPerformanceIdArray
;
258 PEI_PERFORMANCE_LOG_ENTRY
*LogEntryArray
;
261 if (TimeStamp
== 0) {
262 TimeStamp
= GetPerformanceCounter ();
265 InternalGetPerformanceHobLog (&PeiPerformanceLog
, &PeiPerformanceIdArray
);
266 Index
= InternalSearchForLogEntry (PeiPerformanceLog
, PeiPerformanceIdArray
, Handle
, Token
, Module
, Identifier
);
267 if (Index
>= PeiPerformanceLog
->NumberOfEntries
) {
268 return RETURN_NOT_FOUND
;
270 LogEntryArray
= (PEI_PERFORMANCE_LOG_ENTRY
*) (PeiPerformanceLog
+ 1);
271 LogEntryArray
[Index
].EndTimeStamp
= TimeStamp
;
273 return RETURN_SUCCESS
;
277 Attempts to retrieve a performance measurement log entry from the performance measurement log.
278 It can also retrieve the log created by StartPerformanceMeasurement and EndPerformanceMeasurement,
279 and then assign the Identifier with 0.
281 Attempts to retrieve the performance log entry specified by LogEntryKey. If LogEntryKey is
282 zero on entry, then an attempt is made to retrieve the first entry from the performance log,
283 and the key for the second entry in the log is returned. If the performance log is empty,
284 then no entry is retrieved and zero is returned. If LogEntryKey is not zero, then the performance
285 log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is
286 returned. If LogEntryKey is the key for the last entry in the log, then the last log entry is
287 retrieved and an implementation specific non-zero key value that specifies the end of the performance
288 log is returned. If LogEntryKey is equal this implementation specific non-zero key value, then no entry
289 is retrieved and zero is returned. In the cases where a performance log entry can be returned,
290 the log entry is returned in Handle, Token, Module, StartTimeStamp, EndTimeStamp and Identifier.
291 If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().
292 If Handle is NULL, then ASSERT().
293 If Token is NULL, then ASSERT().
294 If Module is NULL, then ASSERT().
295 If StartTimeStamp is NULL, then ASSERT().
296 If EndTimeStamp is NULL, then ASSERT().
297 If Identifier is NULL, then ASSERT().
299 @param LogEntryKey On entry, the key of the performance measurement log entry to retrieve.
300 0, then the first performance measurement log entry is retrieved.
301 On exit, the key of the next performance of entry entry.
302 @param Handle Pointer to environment specific context used to identify the component
304 @param Token Pointer to a Null-terminated ASCII string that identifies the component
306 @param Module Pointer to a Null-terminated ASCII string that identifies the module
308 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
310 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
312 @param Identifier Pointer to the 32-bit identifier that was recorded.
314 @return The key for the next performance log entry (in general case).
319 GetPerformanceMeasurementEx (
320 IN UINTN LogEntryKey
,
321 OUT CONST VOID
**Handle
,
322 OUT CONST CHAR8
**Token
,
323 OUT CONST CHAR8
**Module
,
324 OUT UINT64
*StartTimeStamp
,
325 OUT UINT64
*EndTimeStamp
,
326 OUT UINT32
*Identifier
329 PEI_PERFORMANCE_LOG_HEADER
*PeiPerformanceLog
;
330 UINT32
*PeiPerformanceIdArray
;
331 PEI_PERFORMANCE_LOG_ENTRY
*CurrentLogEntry
;
332 PEI_PERFORMANCE_LOG_ENTRY
*LogEntryArray
;
333 UINTN NumberOfEntries
;
335 ASSERT (Handle
!= NULL
);
336 ASSERT (Token
!= NULL
);
337 ASSERT (Module
!= NULL
);
338 ASSERT (StartTimeStamp
!= NULL
);
339 ASSERT (EndTimeStamp
!= NULL
);
340 ASSERT (Identifier
!= NULL
);
342 InternalGetPerformanceHobLog (&PeiPerformanceLog
, &PeiPerformanceIdArray
);
344 NumberOfEntries
= (UINTN
) (PeiPerformanceLog
->NumberOfEntries
);
345 LogEntryArray
= (PEI_PERFORMANCE_LOG_ENTRY
*) (PeiPerformanceLog
+ 1);
347 // Make sure that LogEntryKey is a valid log entry key.
349 ASSERT (LogEntryKey
<= NumberOfEntries
);
351 if (LogEntryKey
== NumberOfEntries
) {
355 CurrentLogEntry
= &(LogEntryArray
[LogEntryKey
]);
357 *Handle
= (VOID
*) (UINTN
) (CurrentLogEntry
->Handle
);
358 *Token
= CurrentLogEntry
->Token
;
359 *Module
= CurrentLogEntry
->Module
;
360 *StartTimeStamp
= CurrentLogEntry
->StartTimeStamp
;
361 *EndTimeStamp
= CurrentLogEntry
->EndTimeStamp
;
362 *Identifier
= PeiPerformanceIdArray
[LogEntryKey
++];
368 Creates a record for the beginning of a performance measurement.
370 Creates a record that contains the Handle, Token, and Module.
371 If TimeStamp is not zero, then TimeStamp is added to the record as the start time.
372 If TimeStamp is zero, then this function reads the current time stamp
373 and adds that time stamp value to the record as the start time.
375 @param Handle Pointer to environment specific context used
376 to identify the component being measured.
377 @param Token Pointer to a Null-terminated ASCII string
378 that identifies the component being measured.
379 @param Module Pointer to a Null-terminated ASCII string
380 that identifies the module being measured.
381 @param TimeStamp 64-bit time stamp.
383 @retval RETURN_SUCCESS The start of the measurement was recorded.
384 @retval RETURN_OUT_OF_RESOURCES There are not enough resources to record the measurement.
389 StartPerformanceMeasurement (
390 IN CONST VOID
*Handle
, OPTIONAL
391 IN CONST CHAR8
*Token
, OPTIONAL
392 IN CONST CHAR8
*Module
, OPTIONAL
396 return StartPerformanceMeasurementEx (Handle
, Token
, Module
, TimeStamp
, 0);
400 Fills in the end time of a performance measurement.
402 Looks up the record that matches Handle, Token, and Module.
403 If the record can not be found then return RETURN_NOT_FOUND.
404 If the record is found and TimeStamp is not zero,
405 then TimeStamp is added to the record as the end time.
406 If the record is found and TimeStamp is zero, then this function reads
407 the current time stamp and adds that time stamp value to the record as the end time.
409 @param Handle Pointer to environment specific context used
410 to identify the component being measured.
411 @param Token Pointer to a Null-terminated ASCII string
412 that identifies the component being measured.
413 @param Module Pointer to a Null-terminated ASCII string
414 that identifies the module being measured.
415 @param TimeStamp 64-bit time stamp.
417 @retval RETURN_SUCCESS The end of the measurement was recorded.
418 @retval RETURN_NOT_FOUND The specified measurement record could not be found.
423 EndPerformanceMeasurement (
424 IN CONST VOID
*Handle
, OPTIONAL
425 IN CONST CHAR8
*Token
, OPTIONAL
426 IN CONST CHAR8
*Module
, OPTIONAL
430 return EndPerformanceMeasurementEx (Handle
, Token
, Module
, TimeStamp
, 0);
434 Attempts to retrieve a performance measurement log entry from the performance measurement log.
435 It can also retrieve the log created by StartPerformanceMeasurementEx and EndPerformanceMeasurementEx,
436 and then eliminate the Identifier.
438 Attempts to retrieve the performance log entry specified by LogEntryKey. If LogEntryKey is
439 zero on entry, then an attempt is made to retrieve the first entry from the performance log,
440 and the key for the second entry in the log is returned. If the performance log is empty,
441 then no entry is retrieved and zero is returned. If LogEntryKey is not zero, then the performance
442 log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is
443 returned. If LogEntryKey is the key for the last entry in the log, then the last log entry is
444 retrieved and an implementation specific non-zero key value that specifies the end of the performance
445 log is returned. If LogEntryKey is equal this implementation specific non-zero key value, then no entry
446 is retrieved and zero is returned. In the cases where a performance log entry can be returned,
447 the log entry is returned in Handle, Token, Module, StartTimeStamp, and EndTimeStamp.
448 If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().
449 If Handle is NULL, then ASSERT().
450 If Token is NULL, then ASSERT().
451 If Module is NULL, then ASSERT().
452 If StartTimeStamp is NULL, then ASSERT().
453 If EndTimeStamp is NULL, then ASSERT().
455 @param LogEntryKey On entry, the key of the performance measurement log entry to retrieve.
456 0, then the first performance measurement log entry is retrieved.
457 On exit, the key of the next performance of entry entry.
458 @param Handle Pointer to environment specific context used to identify the component
460 @param Token Pointer to a Null-terminated ASCII string that identifies the component
462 @param Module Pointer to a Null-terminated ASCII string that identifies the module
464 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
466 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
469 @return The key for the next performance log entry (in general case).
474 GetPerformanceMeasurement (
475 IN UINTN LogEntryKey
,
476 OUT CONST VOID
**Handle
,
477 OUT CONST CHAR8
**Token
,
478 OUT CONST CHAR8
**Module
,
479 OUT UINT64
*StartTimeStamp
,
480 OUT UINT64
*EndTimeStamp
484 return GetPerformanceMeasurementEx (LogEntryKey
, Handle
, Token
, Module
, StartTimeStamp
, EndTimeStamp
, &Identifier
);
488 Returns TRUE if the performance measurement macros are enabled.
490 This function returns TRUE if the PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
491 PcdPerformanceLibraryPropertyMask is set. Otherwise FALSE is returned.
493 @retval TRUE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
494 PcdPerformanceLibraryPropertyMask is set.
495 @retval FALSE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
496 PcdPerformanceLibraryPropertyMask is clear.
501 PerformanceMeasurementEnabled (
505 return (BOOLEAN
) ((PcdGet8(PcdPerformanceLibraryPropertyMask
) & PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED
) != 0);