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 - 2012, 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 PEI 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 returns the data area of that GUID HOB to record performance log.
41 @param Handle Pointer to environment specific context used
42 to identify the component being measured.
43 @param Token Pointer to a Null-terminated ASCII string
44 that identifies the component being measured.
45 @param Module Pointer to a Null-terminated ASCII string
46 that identifies the module being measured.
48 @retval The index of log entry in the array.
51 PEI_PERFORMANCE_LOG_HEADER
*
52 InternalGetPerformanceHobLog (
56 EFI_HOB_GUID_TYPE
*GuidHob
;
57 PEI_PERFORMANCE_LOG_HEADER
*PeiPerformanceLog
;
58 UINTN PeiPerformanceLogSize
;
60 GuidHob
= GetFirstGuidHob (&gPerformanceProtocolGuid
);
62 if (GuidHob
!= NULL
) {
64 // PEI Performance HOB was found, then return the existing one.
66 PeiPerformanceLog
= GET_GUID_HOB_DATA (GuidHob
);
69 // PEI Performance HOB was not found, then build one.
71 PeiPerformanceLogSize
= sizeof (PEI_PERFORMANCE_LOG_HEADER
) +
72 sizeof (PEI_PERFORMANCE_LOG_ENTRY
) * PcdGet8 (PcdMaxPeiPerformanceLogEntries
);
73 PeiPerformanceLog
= BuildGuidHob (&gPerformanceProtocolGuid
, PeiPerformanceLogSize
);
74 PeiPerformanceLog
= ZeroMem (PeiPerformanceLog
, PeiPerformanceLogSize
);
77 return PeiPerformanceLog
;
81 Searches in the log array with keyword Handle, Token and Module.
83 This internal function searches for the log entry in the log array.
84 If there is an entry that exactly matches the given key word triple
85 and its end time stamp is zero, then the index of that log entry is returned;
86 otherwise, the the number of log entries in the array is returned.
88 @param PeiPerformanceLog Pointer to the data structure containing PEI
90 @param Handle Pointer to environment specific context used
91 to identify the component being measured.
92 @param Token Pointer to a Null-terminated ASCII string
93 that identifies the component being measured.
94 @param Module Pointer to a Null-terminated ASCII string
95 that identifies the module being measured.
97 @retval The index of log entry in the array.
101 InternalSearchForLogEntry (
102 IN PEI_PERFORMANCE_LOG_HEADER
*PeiPerformanceLog
,
103 IN CONST VOID
*Handle
, OPTIONAL
104 IN CONST CHAR8
*Token
, OPTIONAL
105 IN CONST CHAR8
*Module OPTIONAL
109 UINT32 NumberOfEntries
;
110 PEI_PERFORMANCE_LOG_ENTRY
*LogEntryArray
;
116 if (Module
== NULL
) {
119 NumberOfEntries
= PeiPerformanceLog
->NumberOfEntries
;
120 LogEntryArray
= (PEI_PERFORMANCE_LOG_ENTRY
*) (PeiPerformanceLog
+ 1);
122 for (Index
= 0; Index
< NumberOfEntries
; Index
++) {
123 if ((LogEntryArray
[Index
].Handle
== (EFI_PHYSICAL_ADDRESS
) (UINTN
) Handle
) &&
124 AsciiStrnCmp (LogEntryArray
[Index
].Token
, Token
, PEI_PERFORMANCE_STRING_LENGTH
) == 0 &&
125 AsciiStrnCmp (LogEntryArray
[Index
].Module
, Module
, PEI_PERFORMANCE_STRING_LENGTH
) == 0 &&
126 LogEntryArray
[Index
].EndTimeStamp
== 0
135 Creates a record for the beginning of a performance measurement.
137 Creates a record that contains the Handle, Token, and Module.
138 If TimeStamp is not zero, then TimeStamp is added to the record as the start time.
139 If TimeStamp is zero, then this function reads the current time stamp
140 and adds that time stamp value to the record as the start time.
142 @param Handle Pointer to environment specific context used
143 to identify the component being measured.
144 @param Token Pointer to a Null-terminated ASCII string
145 that identifies the component being measured.
146 @param Module Pointer to a Null-terminated ASCII string
147 that identifies the module being measured.
148 @param TimeStamp 64-bit time stamp.
150 @retval RETURN_SUCCESS The start of the measurement was recorded.
151 @retval RETURN_OUT_OF_RESOURCES There are not enough resources to record the measurement.
156 StartPerformanceMeasurement (
157 IN CONST VOID
*Handle
, OPTIONAL
158 IN CONST CHAR8
*Token
, OPTIONAL
159 IN CONST CHAR8
*Module
, OPTIONAL
163 PEI_PERFORMANCE_LOG_HEADER
*PeiPerformanceLog
;
164 PEI_PERFORMANCE_LOG_ENTRY
*LogEntryArray
;
167 PeiPerformanceLog
= InternalGetPerformanceHobLog ();
169 if (PeiPerformanceLog
->NumberOfEntries
>= PcdGet8 (PcdMaxPeiPerformanceLogEntries
)) {
170 return RETURN_OUT_OF_RESOURCES
;
172 Index
= PeiPerformanceLog
->NumberOfEntries
++;
173 LogEntryArray
= (PEI_PERFORMANCE_LOG_ENTRY
*) (PeiPerformanceLog
+ 1);
174 LogEntryArray
[Index
].Handle
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) Handle
;
177 AsciiStrnCpy (LogEntryArray
[Index
].Token
, Token
, PEI_PERFORMANCE_STRING_LENGTH
);
179 if (Module
!= NULL
) {
180 AsciiStrnCpy (LogEntryArray
[Index
].Module
, Module
, PEI_PERFORMANCE_STRING_LENGTH
);
183 if (TimeStamp
== 0) {
184 TimeStamp
= GetPerformanceCounter ();
186 LogEntryArray
[Index
].StartTimeStamp
= TimeStamp
;
188 return RETURN_SUCCESS
;
192 Fills in the end time of a performance measurement.
194 Looks up the record that matches Handle, Token, and Module.
195 If the record can not be found then return RETURN_NOT_FOUND.
196 If the record is found and TimeStamp is not zero,
197 then TimeStamp is added to the record as the end time.
198 If the record is found and TimeStamp is zero, then this function reads
199 the current time stamp and adds that time stamp value to the record as the end time.
201 @param Handle Pointer to environment specific context used
202 to identify the component being measured.
203 @param Token Pointer to a Null-terminated ASCII string
204 that identifies the component being measured.
205 @param Module Pointer to a Null-terminated ASCII string
206 that identifies the module being measured.
207 @param TimeStamp 64-bit time stamp.
209 @retval RETURN_SUCCESS The end of the measurement was recorded.
210 @retval RETURN_NOT_FOUND The specified measurement record could not be found.
215 EndPerformanceMeasurement (
216 IN CONST VOID
*Handle
, OPTIONAL
217 IN CONST CHAR8
*Token
, OPTIONAL
218 IN CONST CHAR8
*Module
, OPTIONAL
222 PEI_PERFORMANCE_LOG_HEADER
*PeiPerformanceLog
;
223 PEI_PERFORMANCE_LOG_ENTRY
*LogEntryArray
;
226 if (TimeStamp
== 0) {
227 TimeStamp
= GetPerformanceCounter ();
230 PeiPerformanceLog
= InternalGetPerformanceHobLog ();
231 Index
= InternalSearchForLogEntry (PeiPerformanceLog
, Handle
, Token
, Module
);
232 if (Index
>= PeiPerformanceLog
->NumberOfEntries
) {
233 return RETURN_NOT_FOUND
;
235 LogEntryArray
= (PEI_PERFORMANCE_LOG_ENTRY
*) (PeiPerformanceLog
+ 1);
236 LogEntryArray
[Index
].EndTimeStamp
= TimeStamp
;
238 return RETURN_SUCCESS
;
242 Attempts to retrieve a performance measurement log entry from the performance measurement log.
244 Attempts to retrieve the performance log entry specified by LogEntryKey. If LogEntryKey is
245 zero on entry, then an attempt is made to retrieve the first entry from the performance log,
246 and the key for the second entry in the log is returned. If the performance log is empty,
247 then no entry is retrieved and zero is returned. If LogEntryKey is not zero, then the performance
248 log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is
249 returned. If LogEntryKey is the key for the last entry in the log, then the last log entry is
250 retrieved and an implementation specific non-zero key value that specifies the end of the performance
251 log is returned. If LogEntryKey is equal this implementation specific non-zero key value, then no entry
252 is retrieved and zero is returned. In the cases where a performance log entry can be returned,
253 the log entry is returned in Handle, Token, Module, StartTimeStamp, and EndTimeStamp.
254 If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().
255 If Handle is NULL, then ASSERT().
256 If Token is NULL, then ASSERT().
257 If Module is NULL, then ASSERT().
258 If StartTimeStamp is NULL, then ASSERT().
259 If EndTimeStamp is NULL, then ASSERT().
261 @param LogEntryKey On entry, the key of the performance measurement log entry to retrieve.
262 0, then the first performance measurement log entry is retrieved.
263 On exit, the key of the next performance lof entry entry.
264 @param Handle Pointer to environment specific context used to identify the component
266 @param Token Pointer to a Null-terminated ASCII string that identifies the component
268 @param Module Pointer to a Null-terminated ASCII string that identifies the module
270 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
272 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
275 @return The key for the next performance log entry (in general case).
280 GetPerformanceMeasurement (
281 IN UINTN LogEntryKey
,
282 OUT CONST VOID
**Handle
,
283 OUT CONST CHAR8
**Token
,
284 OUT CONST CHAR8
**Module
,
285 OUT UINT64
*StartTimeStamp
,
286 OUT UINT64
*EndTimeStamp
289 PEI_PERFORMANCE_LOG_HEADER
*PeiPerformanceLog
;
290 PEI_PERFORMANCE_LOG_ENTRY
*CurrentLogEntry
;
291 PEI_PERFORMANCE_LOG_ENTRY
*LogEntryArray
;
292 UINTN NumberOfEntries
;
294 ASSERT (Handle
!= NULL
);
295 ASSERT (Token
!= NULL
);
296 ASSERT (Module
!= NULL
);
297 ASSERT (StartTimeStamp
!= NULL
);
298 ASSERT (EndTimeStamp
!= NULL
);
300 PeiPerformanceLog
= InternalGetPerformanceHobLog ();
302 NumberOfEntries
= (UINTN
) (PeiPerformanceLog
->NumberOfEntries
);
303 LogEntryArray
= (PEI_PERFORMANCE_LOG_ENTRY
*) (PeiPerformanceLog
+ 1);
305 // Make sure that LogEntryKey is a valid log entry key.
307 ASSERT (LogEntryKey
<= NumberOfEntries
);
309 if (LogEntryKey
== NumberOfEntries
) {
313 CurrentLogEntry
= &(LogEntryArray
[LogEntryKey
++]);
315 *Handle
= (VOID
*) (UINTN
) (CurrentLogEntry
->Handle
);
316 *Token
= CurrentLogEntry
->Token
;
317 *Module
= CurrentLogEntry
->Module
;
318 *StartTimeStamp
= CurrentLogEntry
->StartTimeStamp
;
319 *EndTimeStamp
= CurrentLogEntry
->EndTimeStamp
;
325 Returns TRUE if the performance measurement macros are enabled.
327 This function returns TRUE if the PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
328 PcdPerformanceLibraryPropertyMask is set. Otherwise FALSE is returned.
330 @retval TRUE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
331 PcdPerformanceLibraryPropertyMask is set.
332 @retval FALSE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
333 PcdPerformanceLibraryPropertyMask is clear.
338 PerformanceMeasurementEnabled (
342 return (BOOLEAN
) ((PcdGet8(PcdPerformanceLibraryPropertyMask
) & PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED
) != 0);