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.
9 Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.<BR>
10 This program and the accompanying materials
11 are licensed and made available under the terms and conditions of the BSD License
12 which accompanies this distribution. The full text of the license may be found at
13 http://opensource.org/licenses/bsd-license.php
15 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
16 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
23 #include <Guid/Performance.h>
25 #include <Library/PerformanceLib.h>
26 #include <Library/DebugLib.h>
27 #include <Library/HobLib.h>
28 #include <Library/BaseLib.h>
29 #include <Library/TimerLib.h>
30 #include <Library/PcdLib.h>
31 #include <Library/BaseMemoryLib.h>
35 Gets the GUID HOB for PEI performance.
37 This internal function searches for the GUID HOB for PEI performance.
38 If that GUID HOB is not found, it will build a new one.
39 It outputs the data area of that GUID HOB to record performance log.
41 @param PeiPerformanceLog Pointer to Pointer to PEI performance log header.
42 @param PeiPerformanceIdArray Pointer to Pointer to PEI performance identifier array.
46 InternalGetPerformanceHobLog (
47 OUT PEI_PERFORMANCE_LOG_HEADER
**PeiPerformanceLog
,
48 OUT UINT32
**PeiPerformanceIdArray
51 EFI_HOB_GUID_TYPE
*GuidHob
;
52 UINTN PeiPerformanceSize
;
54 ASSERT (PeiPerformanceLog
!= NULL
);
55 ASSERT (PeiPerformanceIdArray
!= NULL
);
57 GuidHob
= GetFirstGuidHob (&gPerformanceProtocolGuid
);
59 if (GuidHob
!= NULL
) {
61 // PEI Performance HOB was found, then return the existing one.
63 *PeiPerformanceLog
= GET_GUID_HOB_DATA (GuidHob
);
65 GuidHob
= GetFirstGuidHob (&gPerformanceExProtocolGuid
);
66 ASSERT (GuidHob
!= NULL
);
67 *PeiPerformanceIdArray
= GET_GUID_HOB_DATA (GuidHob
);
70 // PEI Performance HOB was not found, then build one.
72 PeiPerformanceSize
= sizeof (PEI_PERFORMANCE_LOG_HEADER
) +
73 sizeof (PEI_PERFORMANCE_LOG_ENTRY
) * PcdGet8 (PcdMaxPeiPerformanceLogEntries
);
74 *PeiPerformanceLog
= BuildGuidHob (&gPerformanceProtocolGuid
, PeiPerformanceSize
);
75 *PeiPerformanceLog
= ZeroMem (*PeiPerformanceLog
, PeiPerformanceSize
);
77 PeiPerformanceSize
= sizeof (UINT32
) * PcdGet8 (PcdMaxPeiPerformanceLogEntries
);
78 *PeiPerformanceIdArray
= BuildGuidHob (&gPerformanceExProtocolGuid
, PeiPerformanceSize
);
79 *PeiPerformanceIdArray
= ZeroMem (*PeiPerformanceIdArray
, PeiPerformanceSize
);
84 Searches in the log array with keyword Handle, Token, Module and Identifier.
86 This internal function searches for the log entry in the log array.
87 If there is an entry that exactly matches the given keywords
88 and its end time stamp is zero, then the index of that log entry is returned;
89 otherwise, the the number of log entries in the array is returned.
91 @param PeiPerformanceLog Pointer to the data structure containing PEI
93 @param PeiPerformanceIdArray Pointer to PEI performance identifier array.
94 @param Handle Pointer to environment specific context used
95 to identify the component being measured.
96 @param Token Pointer to a Null-terminated ASCII string
97 that identifies the component being measured.
98 @param Module Pointer to a Null-terminated ASCII string
99 that identifies the module being measured.
100 @param Identifier 32-bit identifier.
102 @retval The index of log entry in the array.
106 InternalSearchForLogEntry (
107 IN PEI_PERFORMANCE_LOG_HEADER
*PeiPerformanceLog
,
108 IN UINT32
*PeiPerformanceIdArray
,
109 IN CONST VOID
*Handle
, OPTIONAL
110 IN CONST CHAR8
*Token
, OPTIONAL
111 IN CONST CHAR8
*Module
, OPTIONAL
117 UINT32 NumberOfEntries
;
118 PEI_PERFORMANCE_LOG_ENTRY
*LogEntryArray
;
124 if (Module
== NULL
) {
127 NumberOfEntries
= PeiPerformanceLog
->NumberOfEntries
;
128 LogEntryArray
= (PEI_PERFORMANCE_LOG_ENTRY
*) (PeiPerformanceLog
+ 1);
132 for (Index
= 0; Index
< NumberOfEntries
; Index
++) {
133 Index2
= NumberOfEntries
- 1 - Index
;
134 if (LogEntryArray
[Index2
].EndTimeStamp
== 0 &&
135 (LogEntryArray
[Index2
].Handle
== (EFI_PHYSICAL_ADDRESS
) (UINTN
) Handle
) &&
136 AsciiStrnCmp (LogEntryArray
[Index2
].Token
, Token
, PEI_PERFORMANCE_STRING_LENGTH
) == 0 &&
137 AsciiStrnCmp (LogEntryArray
[Index2
].Module
, Module
, PEI_PERFORMANCE_STRING_LENGTH
) == 0 &&
138 (PeiPerformanceIdArray
[Index2
] == Identifier
)) {
147 Creates a record for the beginning of a performance measurement.
149 Creates a record that contains the Handle, Token, Module and Identifier.
150 If TimeStamp is not zero, then TimeStamp is added to the record as the start time.
151 If TimeStamp is zero, then this function reads the current time stamp
152 and adds that time stamp value to the record as the start time.
154 @param Handle Pointer to environment specific context used
155 to identify the component being measured.
156 @param Token Pointer to a Null-terminated ASCII string
157 that identifies the component being measured.
158 @param Module Pointer to a Null-terminated ASCII string
159 that identifies the module being measured.
160 @param TimeStamp 64-bit time stamp.
161 @param Identifier 32-bit identifier. If the value is 0, the created record
162 is same as the one created by StartPerformanceMeasurement.
164 @retval RETURN_SUCCESS The start of the measurement was recorded.
165 @retval RETURN_OUT_OF_RESOURCES There are not enough resources to record the measurement.
170 StartPerformanceMeasurementEx (
171 IN CONST VOID
*Handle
, OPTIONAL
172 IN CONST CHAR8
*Token
, OPTIONAL
173 IN CONST CHAR8
*Module
, OPTIONAL
178 PEI_PERFORMANCE_LOG_HEADER
*PeiPerformanceLog
;
179 UINT32
*PeiPerformanceIdArray
;
180 PEI_PERFORMANCE_LOG_ENTRY
*LogEntryArray
;
183 InternalGetPerformanceHobLog (&PeiPerformanceLog
, &PeiPerformanceIdArray
);
185 if (PeiPerformanceLog
->NumberOfEntries
>= PcdGet8 (PcdMaxPeiPerformanceLogEntries
)) {
186 return RETURN_OUT_OF_RESOURCES
;
188 Index
= PeiPerformanceLog
->NumberOfEntries
++;
189 LogEntryArray
= (PEI_PERFORMANCE_LOG_ENTRY
*) (PeiPerformanceLog
+ 1);
190 LogEntryArray
[Index
].Handle
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) Handle
;
193 AsciiStrCpyS (LogEntryArray
[Index
].Token
, PEI_PERFORMANCE_STRING_SIZE
, Token
);
195 if (Module
!= NULL
) {
196 AsciiStrCpyS (LogEntryArray
[Index
].Module
, PEI_PERFORMANCE_STRING_SIZE
, Module
);
199 LogEntryArray
[Index
].EndTimeStamp
= 0;
200 PeiPerformanceIdArray
[Index
] = Identifier
;
202 if (TimeStamp
== 0) {
203 TimeStamp
= GetPerformanceCounter ();
205 LogEntryArray
[Index
].StartTimeStamp
= TimeStamp
;
207 return RETURN_SUCCESS
;
211 Fills in the end time of a performance measurement.
213 Looks up the record that matches Handle, Token, Module and Identifier.
214 If the record can not be found then return RETURN_NOT_FOUND.
215 If the record is found and TimeStamp is not zero,
216 then TimeStamp is added to the record as the end time.
217 If the record is found and TimeStamp is zero, then this function reads
218 the current time stamp and adds that time stamp value to the record as the end time.
220 @param Handle Pointer to environment specific context used
221 to identify the component being measured.
222 @param Token Pointer to a Null-terminated ASCII string
223 that identifies the component being measured.
224 @param Module Pointer to a Null-terminated ASCII string
225 that identifies the module being measured.
226 @param TimeStamp 64-bit time stamp.
227 @param Identifier 32-bit identifier. If the value is 0, the found record
228 is same as the one found by EndPerformanceMeasurement.
230 @retval RETURN_SUCCESS The end of the measurement was recorded.
231 @retval RETURN_NOT_FOUND The specified measurement record could not be found.
236 EndPerformanceMeasurementEx (
237 IN CONST VOID
*Handle
, OPTIONAL
238 IN CONST CHAR8
*Token
, OPTIONAL
239 IN CONST CHAR8
*Module
, OPTIONAL
244 PEI_PERFORMANCE_LOG_HEADER
*PeiPerformanceLog
;
245 UINT32
*PeiPerformanceIdArray
;
246 PEI_PERFORMANCE_LOG_ENTRY
*LogEntryArray
;
249 if (TimeStamp
== 0) {
250 TimeStamp
= GetPerformanceCounter ();
253 InternalGetPerformanceHobLog (&PeiPerformanceLog
, &PeiPerformanceIdArray
);
254 Index
= InternalSearchForLogEntry (PeiPerformanceLog
, PeiPerformanceIdArray
, Handle
, Token
, Module
, Identifier
);
255 if (Index
>= PeiPerformanceLog
->NumberOfEntries
) {
256 return RETURN_NOT_FOUND
;
258 LogEntryArray
= (PEI_PERFORMANCE_LOG_ENTRY
*) (PeiPerformanceLog
+ 1);
259 LogEntryArray
[Index
].EndTimeStamp
= TimeStamp
;
261 return RETURN_SUCCESS
;
265 Attempts to retrieve a performance measurement log entry from the performance measurement log.
266 It can also retrieve the log created by StartPerformanceMeasurement and EndPerformanceMeasurement,
267 and then assign the Identifier with 0.
269 Attempts to retrieve the performance log entry specified by LogEntryKey. If LogEntryKey is
270 zero on entry, then an attempt is made to retrieve the first entry from the performance log,
271 and the key for the second entry in the log is returned. If the performance log is empty,
272 then no entry is retrieved and zero is returned. If LogEntryKey is not zero, then the performance
273 log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is
274 returned. If LogEntryKey is the key for the last entry in the log, then the last log entry is
275 retrieved and an implementation specific non-zero key value that specifies the end of the performance
276 log is returned. If LogEntryKey is equal this implementation specific non-zero key value, then no entry
277 is retrieved and zero is returned. In the cases where a performance log entry can be returned,
278 the log entry is returned in Handle, Token, Module, StartTimeStamp, EndTimeStamp and Identifier.
279 If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().
280 If Handle is NULL, then ASSERT().
281 If Token is NULL, then ASSERT().
282 If Module is NULL, then ASSERT().
283 If StartTimeStamp is NULL, then ASSERT().
284 If EndTimeStamp is NULL, then ASSERT().
285 If Identifier is NULL, then ASSERT().
287 @param LogEntryKey On entry, the key of the performance measurement log entry to retrieve.
288 0, then the first performance measurement log entry is retrieved.
289 On exit, the key of the next performance of entry entry.
290 @param Handle Pointer to environment specific context used to identify the component
292 @param Token Pointer to a Null-terminated ASCII string that identifies the component
294 @param Module Pointer to a Null-terminated ASCII string that identifies the module
296 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
298 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
300 @param Identifier Pointer to the 32-bit identifier that was recorded.
302 @return The key for the next performance log entry (in general case).
307 GetPerformanceMeasurementEx (
308 IN UINTN LogEntryKey
,
309 OUT CONST VOID
**Handle
,
310 OUT CONST CHAR8
**Token
,
311 OUT CONST CHAR8
**Module
,
312 OUT UINT64
*StartTimeStamp
,
313 OUT UINT64
*EndTimeStamp
,
314 OUT UINT32
*Identifier
317 PEI_PERFORMANCE_LOG_HEADER
*PeiPerformanceLog
;
318 UINT32
*PeiPerformanceIdArray
;
319 PEI_PERFORMANCE_LOG_ENTRY
*CurrentLogEntry
;
320 PEI_PERFORMANCE_LOG_ENTRY
*LogEntryArray
;
321 UINTN NumberOfEntries
;
323 ASSERT (Handle
!= NULL
);
324 ASSERT (Token
!= NULL
);
325 ASSERT (Module
!= NULL
);
326 ASSERT (StartTimeStamp
!= NULL
);
327 ASSERT (EndTimeStamp
!= NULL
);
328 ASSERT (Identifier
!= NULL
);
330 InternalGetPerformanceHobLog (&PeiPerformanceLog
, &PeiPerformanceIdArray
);
332 NumberOfEntries
= (UINTN
) (PeiPerformanceLog
->NumberOfEntries
);
333 LogEntryArray
= (PEI_PERFORMANCE_LOG_ENTRY
*) (PeiPerformanceLog
+ 1);
335 // Make sure that LogEntryKey is a valid log entry key.
337 ASSERT (LogEntryKey
<= NumberOfEntries
);
339 if (LogEntryKey
== NumberOfEntries
) {
343 CurrentLogEntry
= &(LogEntryArray
[LogEntryKey
]);
345 *Handle
= (VOID
*) (UINTN
) (CurrentLogEntry
->Handle
);
346 *Token
= CurrentLogEntry
->Token
;
347 *Module
= CurrentLogEntry
->Module
;
348 *StartTimeStamp
= CurrentLogEntry
->StartTimeStamp
;
349 *EndTimeStamp
= CurrentLogEntry
->EndTimeStamp
;
350 *Identifier
= PeiPerformanceIdArray
[LogEntryKey
++];
356 Creates a record for the beginning of a performance measurement.
358 Creates a record that contains the Handle, Token, and Module.
359 If TimeStamp is not zero, then TimeStamp is added to the record as the start time.
360 If TimeStamp is zero, then this function reads the current time stamp
361 and adds that time stamp value to the record as the start time.
363 @param Handle Pointer to environment specific context used
364 to identify the component being measured.
365 @param Token Pointer to a Null-terminated ASCII string
366 that identifies the component being measured.
367 @param Module Pointer to a Null-terminated ASCII string
368 that identifies the module being measured.
369 @param TimeStamp 64-bit time stamp.
371 @retval RETURN_SUCCESS The start of the measurement was recorded.
372 @retval RETURN_OUT_OF_RESOURCES There are not enough resources to record the measurement.
377 StartPerformanceMeasurement (
378 IN CONST VOID
*Handle
, OPTIONAL
379 IN CONST CHAR8
*Token
, OPTIONAL
380 IN CONST CHAR8
*Module
, OPTIONAL
384 return StartPerformanceMeasurementEx (Handle
, Token
, Module
, TimeStamp
, 0);
388 Fills in the end time of a performance measurement.
390 Looks up the record that matches Handle, Token, and Module.
391 If the record can not be found then return RETURN_NOT_FOUND.
392 If the record is found and TimeStamp is not zero,
393 then TimeStamp is added to the record as the end time.
394 If the record is found and TimeStamp is zero, then this function reads
395 the current time stamp and adds that time stamp value to the record as the end time.
397 @param Handle Pointer to environment specific context used
398 to identify the component being measured.
399 @param Token Pointer to a Null-terminated ASCII string
400 that identifies the component being measured.
401 @param Module Pointer to a Null-terminated ASCII string
402 that identifies the module being measured.
403 @param TimeStamp 64-bit time stamp.
405 @retval RETURN_SUCCESS The end of the measurement was recorded.
406 @retval RETURN_NOT_FOUND The specified measurement record could not be found.
411 EndPerformanceMeasurement (
412 IN CONST VOID
*Handle
, OPTIONAL
413 IN CONST CHAR8
*Token
, OPTIONAL
414 IN CONST CHAR8
*Module
, OPTIONAL
418 return EndPerformanceMeasurementEx (Handle
, Token
, Module
, TimeStamp
, 0);
422 Attempts to retrieve a performance measurement log entry from the performance measurement log.
423 It can also retrieve the log created by StartPerformanceMeasurementEx and EndPerformanceMeasurementEx,
424 and then eliminate the Identifier.
426 Attempts to retrieve the performance log entry specified by LogEntryKey. If LogEntryKey is
427 zero on entry, then an attempt is made to retrieve the first entry from the performance log,
428 and the key for the second entry in the log is returned. If the performance log is empty,
429 then no entry is retrieved and zero is returned. If LogEntryKey is not zero, then the performance
430 log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is
431 returned. If LogEntryKey is the key for the last entry in the log, then the last log entry is
432 retrieved and an implementation specific non-zero key value that specifies the end of the performance
433 log is returned. If LogEntryKey is equal this implementation specific non-zero key value, then no entry
434 is retrieved and zero is returned. In the cases where a performance log entry can be returned,
435 the log entry is returned in Handle, Token, Module, StartTimeStamp, and EndTimeStamp.
436 If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().
437 If Handle is NULL, then ASSERT().
438 If Token is NULL, then ASSERT().
439 If Module is NULL, then ASSERT().
440 If StartTimeStamp is NULL, then ASSERT().
441 If EndTimeStamp is NULL, then ASSERT().
443 @param LogEntryKey On entry, the key of the performance measurement log entry to retrieve.
444 0, then the first performance measurement log entry is retrieved.
445 On exit, the key of the next performance of entry entry.
446 @param Handle Pointer to environment specific context used to identify the component
448 @param Token Pointer to a Null-terminated ASCII string that identifies the component
450 @param Module Pointer to a Null-terminated ASCII string that identifies the module
452 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
454 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
457 @return The key for the next performance log entry (in general case).
462 GetPerformanceMeasurement (
463 IN UINTN LogEntryKey
,
464 OUT CONST VOID
**Handle
,
465 OUT CONST CHAR8
**Token
,
466 OUT CONST CHAR8
**Module
,
467 OUT UINT64
*StartTimeStamp
,
468 OUT UINT64
*EndTimeStamp
472 return GetPerformanceMeasurementEx (LogEntryKey
, Handle
, Token
, Module
, StartTimeStamp
, EndTimeStamp
, &Identifier
);
476 Returns TRUE if the performance measurement macros are enabled.
478 This function returns TRUE if the PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
479 PcdPerformanceLibraryPropertyMask is set. Otherwise FALSE is returned.
481 @retval TRUE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
482 PcdPerformanceLibraryPropertyMask is set.
483 @retval FALSE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
484 PcdPerformanceLibraryPropertyMask is clear.
489 PerformanceMeasurementEnabled (
493 return (BOOLEAN
) ((PcdGet8(PcdPerformanceLibraryPropertyMask
) & PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED
) != 0);