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 (C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>
11 This program and the accompanying materials
12 are licensed and made available under the terms and conditions of the BSD License
13 which accompanies this distribution. The full text of the license may be found at
14 http://opensource.org/licenses/bsd-license.php
16 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
17 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
24 #include <Guid/Performance.h>
26 #include <Library/PerformanceLib.h>
27 #include <Library/DebugLib.h>
28 #include <Library/HobLib.h>
29 #include <Library/BaseLib.h>
30 #include <Library/TimerLib.h>
31 #include <Library/PcdLib.h>
32 #include <Library/BaseMemoryLib.h>
36 Gets the GUID HOB for PEI performance.
38 This internal function searches for the GUID HOB for PEI performance.
39 If that GUID HOB is not found, it will build a new one.
40 It outputs the data area of that GUID HOB to record performance log.
42 @param PeiPerformanceLog Pointer to Pointer to PEI performance log header.
43 @param PeiPerformanceIdArray Pointer to Pointer to PEI performance identifier array.
47 InternalGetPerformanceHobLog (
48 OUT PEI_PERFORMANCE_LOG_HEADER
**PeiPerformanceLog
,
49 OUT UINT32
**PeiPerformanceIdArray
52 EFI_HOB_GUID_TYPE
*GuidHob
;
53 UINTN PeiPerformanceSize
;
55 ASSERT (PeiPerformanceLog
!= NULL
);
56 ASSERT (PeiPerformanceIdArray
!= NULL
);
58 GuidHob
= GetFirstGuidHob (&gPerformanceProtocolGuid
);
60 if (GuidHob
!= NULL
) {
62 // PEI Performance HOB was found, then return the existing one.
64 *PeiPerformanceLog
= GET_GUID_HOB_DATA (GuidHob
);
66 GuidHob
= GetFirstGuidHob (&gPerformanceExProtocolGuid
);
67 ASSERT (GuidHob
!= NULL
);
68 *PeiPerformanceIdArray
= GET_GUID_HOB_DATA (GuidHob
);
71 // PEI Performance HOB was not found, then build one.
73 PeiPerformanceSize
= sizeof (PEI_PERFORMANCE_LOG_HEADER
) +
74 sizeof (PEI_PERFORMANCE_LOG_ENTRY
) * PcdGet8 (PcdMaxPeiPerformanceLogEntries
);
75 *PeiPerformanceLog
= BuildGuidHob (&gPerformanceProtocolGuid
, PeiPerformanceSize
);
76 *PeiPerformanceLog
= ZeroMem (*PeiPerformanceLog
, PeiPerformanceSize
);
78 PeiPerformanceSize
= sizeof (UINT32
) * PcdGet8 (PcdMaxPeiPerformanceLogEntries
);
79 *PeiPerformanceIdArray
= BuildGuidHob (&gPerformanceExProtocolGuid
, PeiPerformanceSize
);
80 *PeiPerformanceIdArray
= ZeroMem (*PeiPerformanceIdArray
, PeiPerformanceSize
);
85 Searches in the log array with keyword Handle, Token, Module and Identifier.
87 This internal function searches for the log entry in the log array.
88 If there is an entry that exactly matches the given keywords
89 and its end time stamp is zero, then the index of that log entry is returned;
90 otherwise, the the number of log entries in the array is returned.
92 @param PeiPerformanceLog Pointer to the data structure containing PEI
94 @param PeiPerformanceIdArray Pointer to PEI performance identifier array.
95 @param Handle Pointer to environment specific context used
96 to identify the component being measured.
97 @param Token Pointer to a Null-terminated ASCII string
98 that identifies the component being measured.
99 @param Module Pointer to a Null-terminated ASCII string
100 that identifies the module being measured.
101 @param Identifier 32-bit identifier.
103 @retval The index of log entry in the array.
107 InternalSearchForLogEntry (
108 IN PEI_PERFORMANCE_LOG_HEADER
*PeiPerformanceLog
,
109 IN UINT32
*PeiPerformanceIdArray
,
110 IN CONST VOID
*Handle
, OPTIONAL
111 IN CONST CHAR8
*Token
, OPTIONAL
112 IN CONST CHAR8
*Module
, OPTIONAL
118 UINT32 NumberOfEntries
;
119 PEI_PERFORMANCE_LOG_ENTRY
*LogEntryArray
;
125 if (Module
== NULL
) {
128 NumberOfEntries
= PeiPerformanceLog
->NumberOfEntries
;
129 LogEntryArray
= (PEI_PERFORMANCE_LOG_ENTRY
*) (PeiPerformanceLog
+ 1);
133 for (Index
= 0; Index
< NumberOfEntries
; Index
++) {
134 Index2
= NumberOfEntries
- 1 - Index
;
135 if (LogEntryArray
[Index2
].EndTimeStamp
== 0 &&
136 (LogEntryArray
[Index2
].Handle
== (EFI_PHYSICAL_ADDRESS
) (UINTN
) Handle
) &&
137 AsciiStrnCmp (LogEntryArray
[Index2
].Token
, Token
, PEI_PERFORMANCE_STRING_LENGTH
) == 0 &&
138 AsciiStrnCmp (LogEntryArray
[Index2
].Module
, Module
, PEI_PERFORMANCE_STRING_LENGTH
) == 0 &&
139 (PeiPerformanceIdArray
[Index2
] == Identifier
)) {
148 Creates a record for the beginning of a performance measurement.
150 Creates a record that contains the Handle, Token, Module and Identifier.
151 If TimeStamp is not zero, then TimeStamp is added to the record as the start time.
152 If TimeStamp is zero, then this function reads the current time stamp
153 and adds that time stamp value to the record as the start time.
155 @param Handle Pointer to environment specific context used
156 to identify the component being measured.
157 @param Token Pointer to a Null-terminated ASCII string
158 that identifies the component being measured.
159 @param Module Pointer to a Null-terminated ASCII string
160 that identifies the module being measured.
161 @param TimeStamp 64-bit time stamp.
162 @param Identifier 32-bit identifier. If the value is 0, the created record
163 is same as the one created by StartPerformanceMeasurement.
165 @retval RETURN_SUCCESS The start of the measurement was recorded.
166 @retval RETURN_OUT_OF_RESOURCES There are not enough resources to record the measurement.
171 StartPerformanceMeasurementEx (
172 IN CONST VOID
*Handle
, OPTIONAL
173 IN CONST CHAR8
*Token
, OPTIONAL
174 IN CONST CHAR8
*Module
, OPTIONAL
179 PEI_PERFORMANCE_LOG_HEADER
*PeiPerformanceLog
;
180 UINT32
*PeiPerformanceIdArray
;
181 PEI_PERFORMANCE_LOG_ENTRY
*LogEntryArray
;
184 InternalGetPerformanceHobLog (&PeiPerformanceLog
, &PeiPerformanceIdArray
);
186 if (PeiPerformanceLog
->NumberOfEntries
>= PcdGet8 (PcdMaxPeiPerformanceLogEntries
)) {
187 DEBUG ((DEBUG_ERROR
, "PEI performance log array out of resources\n"));
188 return RETURN_OUT_OF_RESOURCES
;
190 Index
= PeiPerformanceLog
->NumberOfEntries
++;
191 LogEntryArray
= (PEI_PERFORMANCE_LOG_ENTRY
*) (PeiPerformanceLog
+ 1);
192 LogEntryArray
[Index
].Handle
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) Handle
;
195 AsciiStrnCpyS (LogEntryArray
[Index
].Token
, PEI_PERFORMANCE_STRING_SIZE
, Token
, PEI_PERFORMANCE_STRING_LENGTH
);
197 if (Module
!= NULL
) {
198 AsciiStrnCpyS (LogEntryArray
[Index
].Module
, PEI_PERFORMANCE_STRING_SIZE
, Module
, PEI_PERFORMANCE_STRING_LENGTH
);
201 LogEntryArray
[Index
].EndTimeStamp
= 0;
202 PeiPerformanceIdArray
[Index
] = Identifier
;
204 if (TimeStamp
== 0) {
205 TimeStamp
= GetPerformanceCounter ();
207 LogEntryArray
[Index
].StartTimeStamp
= TimeStamp
;
209 return RETURN_SUCCESS
;
213 Fills in the end time of a performance measurement.
215 Looks up the record that matches Handle, Token, Module and Identifier.
216 If the record can not be found then return RETURN_NOT_FOUND.
217 If the record is found and TimeStamp is not zero,
218 then TimeStamp is added to the record as the end time.
219 If the record is found and TimeStamp is zero, then this function reads
220 the current time stamp and adds that time stamp value to the record as the end time.
222 @param Handle Pointer to environment specific context used
223 to identify the component being measured.
224 @param Token Pointer to a Null-terminated ASCII string
225 that identifies the component being measured.
226 @param Module Pointer to a Null-terminated ASCII string
227 that identifies the module being measured.
228 @param TimeStamp 64-bit time stamp.
229 @param Identifier 32-bit identifier. If the value is 0, the found record
230 is same as the one found by EndPerformanceMeasurement.
232 @retval RETURN_SUCCESS The end of the measurement was recorded.
233 @retval RETURN_NOT_FOUND The specified measurement record could not be found.
238 EndPerformanceMeasurementEx (
239 IN CONST VOID
*Handle
, OPTIONAL
240 IN CONST CHAR8
*Token
, OPTIONAL
241 IN CONST CHAR8
*Module
, OPTIONAL
246 PEI_PERFORMANCE_LOG_HEADER
*PeiPerformanceLog
;
247 UINT32
*PeiPerformanceIdArray
;
248 PEI_PERFORMANCE_LOG_ENTRY
*LogEntryArray
;
251 if (TimeStamp
== 0) {
252 TimeStamp
= GetPerformanceCounter ();
255 InternalGetPerformanceHobLog (&PeiPerformanceLog
, &PeiPerformanceIdArray
);
256 Index
= InternalSearchForLogEntry (PeiPerformanceLog
, PeiPerformanceIdArray
, Handle
, Token
, Module
, Identifier
);
257 if (Index
>= PeiPerformanceLog
->NumberOfEntries
) {
258 return RETURN_NOT_FOUND
;
260 LogEntryArray
= (PEI_PERFORMANCE_LOG_ENTRY
*) (PeiPerformanceLog
+ 1);
261 LogEntryArray
[Index
].EndTimeStamp
= TimeStamp
;
263 return RETURN_SUCCESS
;
267 Attempts to retrieve a performance measurement log entry from the performance measurement log.
268 It can also retrieve the log created by StartPerformanceMeasurement and EndPerformanceMeasurement,
269 and then assign the Identifier with 0.
271 Attempts to retrieve the performance log entry specified by LogEntryKey. If LogEntryKey is
272 zero on entry, then an attempt is made to retrieve the first entry from the performance log,
273 and the key for the second entry in the log is returned. If the performance log is empty,
274 then no entry is retrieved and zero is returned. If LogEntryKey is not zero, then the performance
275 log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is
276 returned. If LogEntryKey is the key for the last entry in the log, then the last log entry is
277 retrieved and an implementation specific non-zero key value that specifies the end of the performance
278 log is returned. If LogEntryKey is equal this implementation specific non-zero key value, then no entry
279 is retrieved and zero is returned. In the cases where a performance log entry can be returned,
280 the log entry is returned in Handle, Token, Module, StartTimeStamp, EndTimeStamp and Identifier.
281 If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().
282 If Handle is NULL, then ASSERT().
283 If Token is NULL, then ASSERT().
284 If Module is NULL, then ASSERT().
285 If StartTimeStamp is NULL, then ASSERT().
286 If EndTimeStamp is NULL, then ASSERT().
287 If Identifier is NULL, then ASSERT().
289 @param LogEntryKey On entry, the key of the performance measurement log entry to retrieve.
290 0, then the first performance measurement log entry is retrieved.
291 On exit, the key of the next performance of entry entry.
292 @param Handle Pointer to environment specific context used to identify the component
294 @param Token Pointer to a Null-terminated ASCII string that identifies the component
296 @param Module Pointer to a Null-terminated ASCII string that identifies the module
298 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
300 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
302 @param Identifier Pointer to the 32-bit identifier that was recorded.
304 @return The key for the next performance log entry (in general case).
309 GetPerformanceMeasurementEx (
310 IN UINTN LogEntryKey
,
311 OUT CONST VOID
**Handle
,
312 OUT CONST CHAR8
**Token
,
313 OUT CONST CHAR8
**Module
,
314 OUT UINT64
*StartTimeStamp
,
315 OUT UINT64
*EndTimeStamp
,
316 OUT UINT32
*Identifier
319 PEI_PERFORMANCE_LOG_HEADER
*PeiPerformanceLog
;
320 UINT32
*PeiPerformanceIdArray
;
321 PEI_PERFORMANCE_LOG_ENTRY
*CurrentLogEntry
;
322 PEI_PERFORMANCE_LOG_ENTRY
*LogEntryArray
;
323 UINTN NumberOfEntries
;
325 ASSERT (Handle
!= NULL
);
326 ASSERT (Token
!= NULL
);
327 ASSERT (Module
!= NULL
);
328 ASSERT (StartTimeStamp
!= NULL
);
329 ASSERT (EndTimeStamp
!= NULL
);
330 ASSERT (Identifier
!= NULL
);
332 InternalGetPerformanceHobLog (&PeiPerformanceLog
, &PeiPerformanceIdArray
);
334 NumberOfEntries
= (UINTN
) (PeiPerformanceLog
->NumberOfEntries
);
335 LogEntryArray
= (PEI_PERFORMANCE_LOG_ENTRY
*) (PeiPerformanceLog
+ 1);
337 // Make sure that LogEntryKey is a valid log entry key.
339 ASSERT (LogEntryKey
<= NumberOfEntries
);
341 if (LogEntryKey
== NumberOfEntries
) {
345 CurrentLogEntry
= &(LogEntryArray
[LogEntryKey
]);
347 *Handle
= (VOID
*) (UINTN
) (CurrentLogEntry
->Handle
);
348 *Token
= CurrentLogEntry
->Token
;
349 *Module
= CurrentLogEntry
->Module
;
350 *StartTimeStamp
= CurrentLogEntry
->StartTimeStamp
;
351 *EndTimeStamp
= CurrentLogEntry
->EndTimeStamp
;
352 *Identifier
= PeiPerformanceIdArray
[LogEntryKey
++];
358 Creates a record for the beginning of a performance measurement.
360 Creates a record that contains the Handle, Token, and Module.
361 If TimeStamp is not zero, then TimeStamp is added to the record as the start time.
362 If TimeStamp is zero, then this function reads the current time stamp
363 and adds that time stamp value to the record as the start time.
365 @param Handle Pointer to environment specific context used
366 to identify the component being measured.
367 @param Token Pointer to a Null-terminated ASCII string
368 that identifies the component being measured.
369 @param Module Pointer to a Null-terminated ASCII string
370 that identifies the module being measured.
371 @param TimeStamp 64-bit time stamp.
373 @retval RETURN_SUCCESS The start of the measurement was recorded.
374 @retval RETURN_OUT_OF_RESOURCES There are not enough resources to record the measurement.
379 StartPerformanceMeasurement (
380 IN CONST VOID
*Handle
, OPTIONAL
381 IN CONST CHAR8
*Token
, OPTIONAL
382 IN CONST CHAR8
*Module
, OPTIONAL
386 return StartPerformanceMeasurementEx (Handle
, Token
, Module
, TimeStamp
, 0);
390 Fills in the end time of a performance measurement.
392 Looks up the record that matches Handle, Token, and Module.
393 If the record can not be found then return RETURN_NOT_FOUND.
394 If the record is found and TimeStamp is not zero,
395 then TimeStamp is added to the record as the end time.
396 If the record is found and TimeStamp is zero, then this function reads
397 the current time stamp and adds that time stamp value to the record as the end time.
399 @param Handle Pointer to environment specific context used
400 to identify the component being measured.
401 @param Token Pointer to a Null-terminated ASCII string
402 that identifies the component being measured.
403 @param Module Pointer to a Null-terminated ASCII string
404 that identifies the module being measured.
405 @param TimeStamp 64-bit time stamp.
407 @retval RETURN_SUCCESS The end of the measurement was recorded.
408 @retval RETURN_NOT_FOUND The specified measurement record could not be found.
413 EndPerformanceMeasurement (
414 IN CONST VOID
*Handle
, OPTIONAL
415 IN CONST CHAR8
*Token
, OPTIONAL
416 IN CONST CHAR8
*Module
, OPTIONAL
420 return EndPerformanceMeasurementEx (Handle
, Token
, Module
, TimeStamp
, 0);
424 Attempts to retrieve a performance measurement log entry from the performance measurement log.
425 It can also retrieve the log created by StartPerformanceMeasurementEx and EndPerformanceMeasurementEx,
426 and then eliminate the Identifier.
428 Attempts to retrieve the performance log entry specified by LogEntryKey. If LogEntryKey is
429 zero on entry, then an attempt is made to retrieve the first entry from the performance log,
430 and the key for the second entry in the log is returned. If the performance log is empty,
431 then no entry is retrieved and zero is returned. If LogEntryKey is not zero, then the performance
432 log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is
433 returned. If LogEntryKey is the key for the last entry in the log, then the last log entry is
434 retrieved and an implementation specific non-zero key value that specifies the end of the performance
435 log is returned. If LogEntryKey is equal this implementation specific non-zero key value, then no entry
436 is retrieved and zero is returned. In the cases where a performance log entry can be returned,
437 the log entry is returned in Handle, Token, Module, StartTimeStamp, and EndTimeStamp.
438 If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().
439 If Handle is NULL, then ASSERT().
440 If Token is NULL, then ASSERT().
441 If Module is NULL, then ASSERT().
442 If StartTimeStamp is NULL, then ASSERT().
443 If EndTimeStamp is NULL, then ASSERT().
445 @param LogEntryKey On entry, the key of the performance measurement log entry to retrieve.
446 0, then the first performance measurement log entry is retrieved.
447 On exit, the key of the next performance of entry entry.
448 @param Handle Pointer to environment specific context used to identify the component
450 @param Token Pointer to a Null-terminated ASCII string that identifies the component
452 @param Module Pointer to a Null-terminated ASCII string that identifies the module
454 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
456 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
459 @return The key for the next performance log entry (in general case).
464 GetPerformanceMeasurement (
465 IN UINTN LogEntryKey
,
466 OUT CONST VOID
**Handle
,
467 OUT CONST CHAR8
**Token
,
468 OUT CONST CHAR8
**Module
,
469 OUT UINT64
*StartTimeStamp
,
470 OUT UINT64
*EndTimeStamp
474 return GetPerformanceMeasurementEx (LogEntryKey
, Handle
, Token
, Module
, StartTimeStamp
, EndTimeStamp
, &Identifier
);
478 Returns TRUE if the performance measurement macros are enabled.
480 This function returns TRUE if the PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
481 PcdPerformanceLibraryPropertyMask is set. Otherwise FALSE is returned.
483 @retval TRUE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
484 PcdPerformanceLibraryPropertyMask is set.
485 @retval FALSE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
486 PcdPerformanceLibraryPropertyMask is clear.
491 PerformanceMeasurementEnabled (
495 return (BOOLEAN
) ((PcdGet8(PcdPerformanceLibraryPropertyMask
) & PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED
) != 0);