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 - 2008, Intel Corporation. <BR>
10 All rights reserved. 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/PeiPerformanceHob.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 (&gPeiPerformanceHobGuid
);
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 (&gPeiPerformanceHobGuid
, 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 Handle Pointer to environment specific context used
89 to identify the component being measured.
90 @param Token Pointer to a Null-terminated ASCII string
91 that identifies the component being measured.
92 @param Module Pointer to a Null-terminated ASCII string
93 that identifies the module being measured.
95 @retval The index of log entry in the array.
99 InternalSearchForLogEntry (
100 IN PEI_PERFORMANCE_LOG_HEADER
*PeiPerformanceLog
,
101 IN CONST VOID
*Handle
, OPTIONAL
102 IN CONST CHAR8
*Token
, OPTIONAL
103 IN CONST CHAR8
*Module OPTIONAL
107 UINT32 NumberOfEntries
;
108 PEI_PERFORMANCE_LOG_ENTRY
*LogEntryArray
;
114 if (Module
== NULL
) {
117 NumberOfEntries
= PeiPerformanceLog
->NumberOfEntries
;
118 LogEntryArray
= (PEI_PERFORMANCE_LOG_ENTRY
*) (PeiPerformanceLog
+ 1);
120 for (Index
= 0; Index
< NumberOfEntries
; Index
++) {
121 if ((LogEntryArray
[Index
].Handle
== (EFI_PHYSICAL_ADDRESS
) (UINTN
) Handle
) &&
122 AsciiStrnCmp (LogEntryArray
[Index
].Token
, Token
, PEI_PERFORMANCE_STRING_LENGTH
) == 0 &&
123 AsciiStrnCmp (LogEntryArray
[Index
].Module
, Module
, PEI_PERFORMANCE_STRING_LENGTH
) == 0 &&
124 LogEntryArray
[Index
].EndTimeStamp
== 0
133 Creates a record for the beginning of a performance measurement.
135 Creates a record that contains the Handle, Token, and Module.
136 If TimeStamp is not zero, then TimeStamp is added to the record as the start time.
137 If TimeStamp is zero, then this function reads the current time stamp
138 and adds that time stamp value to the record as the start time.
140 @param Handle Pointer to environment specific context used
141 to identify the component being measured.
142 @param Token Pointer to a Null-terminated ASCII string
143 that identifies the component being measured.
144 @param Module Pointer to a Null-terminated ASCII string
145 that identifies the module being measured.
146 @param TimeStamp 64-bit time stamp.
148 @retval RETURN_SUCCESS The start of the measurement was recorded.
149 @retval RETURN_OUT_OF_RESOURCES There are not enough resources to record the measurement.
154 StartPerformanceMeasurement (
155 IN CONST VOID
*Handle
, OPTIONAL
156 IN CONST CHAR8
*Token
, OPTIONAL
157 IN CONST CHAR8
*Module
, OPTIONAL
161 PEI_PERFORMANCE_LOG_HEADER
*PeiPerformanceLog
;
162 PEI_PERFORMANCE_LOG_ENTRY
*LogEntryArray
;
165 PeiPerformanceLog
= InternalGetPerformanceHobLog ();
167 if (PeiPerformanceLog
->NumberOfEntries
>= PcdGet8 (PcdMaxPeiPerformanceLogEntries
)) {
168 return RETURN_OUT_OF_RESOURCES
;
170 Index
= PeiPerformanceLog
->NumberOfEntries
++;
171 LogEntryArray
= (PEI_PERFORMANCE_LOG_ENTRY
*) (PeiPerformanceLog
+ 1);
172 LogEntryArray
[Index
].Handle
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) Handle
;
175 AsciiStrnCpy (LogEntryArray
[Index
].Token
, Token
, PEI_PERFORMANCE_STRING_LENGTH
);
177 if (Module
!= NULL
) {
178 AsciiStrnCpy (LogEntryArray
[Index
].Module
, Module
, PEI_PERFORMANCE_STRING_LENGTH
);
181 if (TimeStamp
== 0) {
182 TimeStamp
= GetPerformanceCounter ();
184 LogEntryArray
[Index
].StartTimeStamp
= TimeStamp
;
186 return RETURN_SUCCESS
;
190 Fills in the end time of a performance measurement.
192 Looks up the record that matches Handle, Token, and Module.
193 If the record can not be found then return RETURN_NOT_FOUND.
194 If the record is found and TimeStamp is not zero,
195 then TimeStamp is added to the record as the end time.
196 If the record is found and TimeStamp is zero, then this function reads
197 the current time stamp and adds that time stamp value to the record as the end time.
198 If this function is called multiple times for the same record, then the end time is overwritten.
200 @param Handle Pointer to environment specific context used
201 to identify the component being measured.
202 @param Token Pointer to a Null-terminated ASCII string
203 that identifies the component being measured.
204 @param Module Pointer to a Null-terminated ASCII string
205 that identifies the module being measured.
206 @param TimeStamp 64-bit time stamp.
208 @retval RETURN_SUCCESS The end of the measurement was recorded.
209 @retval RETURN_NOT_FOUND The specified measurement record could not be found.
214 EndPerformanceMeasurement (
215 IN CONST VOID
*Handle
, OPTIONAL
216 IN CONST CHAR8
*Token
, OPTIONAL
217 IN CONST CHAR8
*Module
, OPTIONAL
221 PEI_PERFORMANCE_LOG_HEADER
*PeiPerformanceLog
;
222 PEI_PERFORMANCE_LOG_ENTRY
*LogEntryArray
;
225 if (TimeStamp
== 0) {
226 TimeStamp
= GetPerformanceCounter ();
229 PeiPerformanceLog
= InternalGetPerformanceHobLog ();
230 Index
= InternalSearchForLogEntry (PeiPerformanceLog
, Handle
, Token
, Module
);
231 if (Index
>= PeiPerformanceLog
->NumberOfEntries
) {
232 return RETURN_NOT_FOUND
;
234 LogEntryArray
= (PEI_PERFORMANCE_LOG_ENTRY
*) (PeiPerformanceLog
+ 1);
235 LogEntryArray
[Index
].EndTimeStamp
= TimeStamp
;
237 return RETURN_SUCCESS
;
241 Attempts to retrieve a performance measurement log entry from the performance measurement log.
243 Attempts to retrieve the performance log entry specified by LogEntryKey. If LogEntryKey is
244 zero on entry, then an attempt is made to retrieve the first entry from the performance log,
245 and the key for the second entry in the log is returned. If the performance log is empty,
246 then no entry is retrieved and zero is returned. If LogEntryKey is not zero, then the performance
247 log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is
248 returned. If LogEntryKey is the key for the last entry in the log, then the last log entry is
249 retrieved and an implementation specific non-zero key value that specifies the end of the performance
250 log is returned. If LogEntryKey is equal this implementation specific non-zero key value, then no entry
251 is retrieved and zero is returned. In the cases where a performance log entry can be returned,
252 the log entry is returned in Handle, Token, Module, StartTimeStamp, and EndTimeStamp.
253 If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().
254 If Handle is NULL, then ASSERT().
255 If Token is NULL, then ASSERT().
256 If Module is NULL, then ASSERT().
257 If StartTimeStamp is NULL, then ASSERT().
258 If EndTimeStamp is NULL, then ASSERT().
260 @param LogEntryKey On entry, the key of the performance measurement log entry to retrieve.
261 0, then the first performance measurement log entry is retrieved.
262 On exit, the key of the next performance lof entry entry.
263 @param Handle Pointer to environment specific context used to identify the component
265 @param Token Pointer to a Null-terminated ASCII string that identifies the component
267 @param Module Pointer to a Null-terminated ASCII string that identifies the module
269 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
271 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
274 @return The key for the next performance log entry (in general case).
279 GetPerformanceMeasurement (
280 IN UINTN LogEntryKey
,
281 OUT CONST VOID
**Handle
,
282 OUT CONST CHAR8
**Token
,
283 OUT CONST CHAR8
**Module
,
284 OUT UINT64
*StartTimeStamp
,
285 OUT UINT64
*EndTimeStamp
288 PEI_PERFORMANCE_LOG_HEADER
*PeiPerformanceLog
;
289 PEI_PERFORMANCE_LOG_ENTRY
*CurrentLogEntry
;
290 PEI_PERFORMANCE_LOG_ENTRY
*LogEntryArray
;
291 UINTN NumberOfEntries
;
293 ASSERT (Handle
!= NULL
);
294 ASSERT (Token
!= NULL
);
295 ASSERT (Module
!= NULL
);
296 ASSERT (StartTimeStamp
!= NULL
);
297 ASSERT (EndTimeStamp
!= NULL
);
299 PeiPerformanceLog
= InternalGetPerformanceHobLog ();
301 NumberOfEntries
= (UINTN
) (PeiPerformanceLog
->NumberOfEntries
);
302 LogEntryArray
= (PEI_PERFORMANCE_LOG_ENTRY
*) (PeiPerformanceLog
+ 1);
304 // Make sure that LogEntryKey is a valid log entry key.
306 ASSERT (LogEntryKey
<= NumberOfEntries
);
308 if (LogEntryKey
== NumberOfEntries
) {
312 CurrentLogEntry
= &(LogEntryArray
[LogEntryKey
++]);
314 *Handle
= (VOID
*) (UINTN
) (CurrentLogEntry
->Handle
);
315 *Token
= CurrentLogEntry
->Token
;
316 *Module
= CurrentLogEntry
->Module
;
317 *StartTimeStamp
= CurrentLogEntry
->StartTimeStamp
;
318 *EndTimeStamp
= CurrentLogEntry
->EndTimeStamp
;
324 Returns TRUE if the performance measurement macros are enabled.
326 This function returns TRUE if the PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
327 PcdPerformanceLibraryPropertyMask is set. Otherwise FALSE is returned.
329 @retval TRUE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
330 PcdPerformanceLibraryPropertyMask is set.
331 @retval FALSE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
332 PcdPerformanceLibraryPropertyMask is clear.
337 PerformanceMeasurementEnabled (
341 return (BOOLEAN
) ((PcdGet8(PcdPerformanceLibraryPropertyMask
) & PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED
) != 0);