3 Copyright (c) 2006, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
23 // The package level header files this module uses
27 // The protocols, PPI and GUID defintions for this module
29 #include <Guid/PeiPerformanceHob.h>
31 // The Library classes this module consumes
33 #include <Library/PerformanceLib.h>
34 #include <Library/DebugLib.h>
35 #include <Library/HobLib.h>
36 #include <Library/BaseLib.h>
37 #include <Library/TimerLib.h>
38 #include <Library/PcdLib.h>
39 #include <Library/BaseMemoryLib.h>
43 Gets PEI the GUID HOB for PEI performance.
45 This internal function searches for the GUID HOB for PEI performance.
46 If that GUID HOB is not found, it will build a new one.
47 It returns the data area of that GUID HOB to record performance log.
49 @param Handle Pointer to environment specific context used
50 to identify the component being measured.
51 @param Token Pointer to a Null-terminated ASCII string
52 that identifies the component being measured.
53 @param Module Pointer to a Null-terminated ASCII string
54 that identifies the module being measured.
56 @retval The index of log entry in the array.
60 PEI_PERFORMANCE_LOG_HEADER
*
61 InternalGetPerformanceHobLog (
65 EFI_HOB_GUID_TYPE
*GuidHob
;
66 PEI_PERFORMANCE_LOG_HEADER
*PeiPerformanceLog
;
67 UINTN PeiPerformanceLogSize
;
69 GuidHob
= GetFirstGuidHob (&gPeiPerformanceHobGuid
);
71 if (GuidHob
!= NULL
) {
73 // PEI Performance HOB was found, then return the existing one.
75 PeiPerformanceLog
= GET_GUID_HOB_DATA (GuidHob
);
78 // PEI Performance HOB was not found, then build one.
80 PeiPerformanceLogSize
= sizeof (PEI_PERFORMANCE_LOG_HEADER
) +
81 sizeof (PEI_PERFORMANCE_LOG_ENTRY
) * PcdGet8 (PcdMaxPeiPerformanceLogEntries
);
82 PeiPerformanceLog
= BuildGuidHob (&gPeiPerformanceHobGuid
, PeiPerformanceLogSize
);
83 PeiPerformanceLog
= ZeroMem (PeiPerformanceLog
, PeiPerformanceLogSize
);
86 return PeiPerformanceLog
;
90 Searches in the log array with keyword Handle, Token and Module.
92 This internal function searches for the log entry in the log array.
93 If there is an entry that exactly matches the given key word triple
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 Handle Pointer to environment specific context used
98 to identify the component being measured.
99 @param Token Pointer to a Null-terminated ASCII string
100 that identifies the component being measured.
101 @param Module Pointer to a Null-terminated ASCII string
102 that identifies the module being measured.
104 @retval The index of log entry in the array.
109 InternalSearchForLogEntry (
110 IN PEI_PERFORMANCE_LOG_HEADER
*PeiPerformanceLog
,
111 IN CONST VOID
*Handle
, OPTIONAL
112 IN CONST CHAR8
*Token
, OPTIONAL
113 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);
130 for (Index
= 0; Index
< NumberOfEntries
; Index
++) {
131 if ((LogEntryArray
[Index
].Handle
== (EFI_PHYSICAL_ADDRESS
) (UINTN
) Handle
) &&
132 AsciiStrnCmp (LogEntryArray
[Index
].Token
, Token
, PEI_PERFORMANCE_STRING_LENGTH
) == 0 &&
133 AsciiStrnCmp (LogEntryArray
[Index
].Module
, Module
, PEI_PERFORMANCE_STRING_LENGTH
) == 0 &&
134 LogEntryArray
[Index
].EndTimeStamp
== 0
143 Creates a record for the beginning of a performance measurement.
145 Creates a record that contains the Handle, Token, and Module.
146 If TimeStamp is not zero, then TimeStamp is added to the record as the start time.
147 If TimeStamp is zero, then this function reads the current time stamp
148 and adds that time stamp value to the record as the start time.
150 @param Handle Pointer to environment specific context used
151 to identify the component being measured.
152 @param Token Pointer to a Null-terminated ASCII string
153 that identifies the component being measured.
154 @param Module Pointer to a Null-terminated ASCII string
155 that identifies the module being measured.
156 @param TimeStamp 64-bit time stamp.
158 @retval RETURN_SUCCESS The start of the measurement was recorded.
159 @retval RETURN_OUT_OF_RESOURCES There are not enough resources to record the measurement.
164 StartPerformanceMeasurement (
165 IN CONST VOID
*Handle
, OPTIONAL
166 IN CONST CHAR8
*Token
, OPTIONAL
167 IN CONST CHAR8
*Module
, OPTIONAL
171 PEI_PERFORMANCE_LOG_HEADER
*PeiPerformanceLog
;
172 PEI_PERFORMANCE_LOG_ENTRY
*LogEntryArray
;
175 PeiPerformanceLog
= InternalGetPerformanceHobLog ();
177 if (PeiPerformanceLog
->NumberOfEntries
>= PcdGet8 (PcdMaxPeiPerformanceLogEntries
)) {
178 return RETURN_OUT_OF_RESOURCES
;
180 Index
= PeiPerformanceLog
->NumberOfEntries
++;
181 LogEntryArray
= (PEI_PERFORMANCE_LOG_ENTRY
*) (PeiPerformanceLog
+ 1);
182 LogEntryArray
[Index
].Handle
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) Handle
;
185 AsciiStrnCpy (LogEntryArray
[Index
].Token
, Token
, PEI_PERFORMANCE_STRING_LENGTH
);
187 if (Module
!= NULL
) {
188 AsciiStrnCpy (LogEntryArray
[Index
].Module
, Module
, PEI_PERFORMANCE_STRING_LENGTH
);
191 if (TimeStamp
== 0) {
192 TimeStamp
= GetPerformanceCounter ();
194 LogEntryArray
[Index
].StartTimeStamp
= TimeStamp
;
196 return RETURN_SUCCESS
;
200 Fills in the end time of a performance measurement.
202 Looks up the record that matches Handle, Token, and Module.
203 If the record can not be found then return RETURN_NOT_FOUND.
204 If the record is found and TimeStamp is not zero,
205 then TimeStamp is added to the record as the end time.
206 If the record is found and TimeStamp is zero, then this function reads
207 the current time stamp and adds that time stamp value to the record as the end time.
208 If this function is called multiple times for the same record, then the end time is overwritten.
210 @param Handle Pointer to environment specific context used
211 to identify the component being measured.
212 @param Token Pointer to a Null-terminated ASCII string
213 that identifies the component being measured.
214 @param Module Pointer to a Null-terminated ASCII string
215 that identifies the module being measured.
216 @param TimeStamp 64-bit time stamp.
218 @retval RETURN_SUCCESS The end of the measurement was recorded.
219 @retval RETURN_NOT_FOUND The specified measurement record could not be found.
224 EndPerformanceMeasurement (
225 IN CONST VOID
*Handle
, OPTIONAL
226 IN CONST CHAR8
*Token
, OPTIONAL
227 IN CONST CHAR8
*Module
, OPTIONAL
231 PEI_PERFORMANCE_LOG_HEADER
*PeiPerformanceLog
;
232 PEI_PERFORMANCE_LOG_ENTRY
*LogEntryArray
;
235 if (TimeStamp
== 0) {
236 TimeStamp
= GetPerformanceCounter ();
239 PeiPerformanceLog
= InternalGetPerformanceHobLog ();
240 Index
= InternalSearchForLogEntry (PeiPerformanceLog
, Handle
, Token
, Module
);
241 if (Index
>= PeiPerformanceLog
->NumberOfEntries
) {
242 return RETURN_NOT_FOUND
;
244 LogEntryArray
= (PEI_PERFORMANCE_LOG_ENTRY
*) (PeiPerformanceLog
+ 1);
245 LogEntryArray
[Index
].EndTimeStamp
= TimeStamp
;
247 return RETURN_SUCCESS
;
251 Attempts to retrieve a performance measurement log entry from the performance measurement log.
253 Attempts to retrieve the performance log entry specified by LogEntryKey. If LogEntryKey is
254 zero on entry, then an attempt is made to retrieve the first entry from the performance log,
255 and the key for the second entry in the log is returned. If the performance log is empty,
256 then no entry is retrieved and zero is returned. If LogEntryKey is not zero, then the performance
257 log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is
258 returned. If LogEntryKey is the key for the last entry in the log, then the last log entry is
259 retrieved and an implementation specific non-zero key value that specifies the end of the performance
260 log is returned. If LogEntryKey is equal this implementation specific non-zero key value, then no entry
261 is retrieved and zero is returned. In the cases where a performance log entry can be returned,
262 the log entry is returned in Handle, Token, Module, StartTimeStamp, and EndTimeStamp.
263 If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().
264 If Handle is NULL, then ASSERT().
265 If Token is NULL, then ASSERT().
266 If Module is NULL, then ASSERT().
267 If StartTimeStamp is NULL, then ASSERT().
268 If EndTimeStamp is NULL, then ASSERT().
270 @param LogEntryKey On entry, the key of the performance measurement log entry to retrieve.
271 0, then the first performance measurement log entry is retrieved.
272 On exit, the key of the next performance lof entry entry.
273 @param Handle Pointer to environment specific context used to identify the component
275 @param Token Pointer to a Null-terminated ASCII string that identifies the component
277 @param Module Pointer to a Null-terminated ASCII string that identifies the module
279 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
281 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
284 @return The key for the next performance log entry (in general case).
289 GetPerformanceMeasurement (
290 IN UINTN LogEntryKey
,
291 OUT CONST VOID
**Handle
,
292 OUT CONST CHAR8
**Token
,
293 OUT CONST CHAR8
**Module
,
294 OUT UINT64
*StartTimeStamp
,
295 OUT UINT64
*EndTimeStamp
298 PEI_PERFORMANCE_LOG_HEADER
*PeiPerformanceLog
;
299 PEI_PERFORMANCE_LOG_ENTRY
*CurrentLogEntry
;
300 PEI_PERFORMANCE_LOG_ENTRY
*LogEntryArray
;
301 UINTN NumberOfEntries
;
303 ASSERT (Handle
!= NULL
);
304 ASSERT (Token
!= NULL
);
305 ASSERT (Module
!= NULL
);
306 ASSERT (StartTimeStamp
!= NULL
);
307 ASSERT (EndTimeStamp
!= NULL
);
309 PeiPerformanceLog
= InternalGetPerformanceHobLog ();
311 NumberOfEntries
= (UINTN
) (PeiPerformanceLog
->NumberOfEntries
);
312 LogEntryArray
= (PEI_PERFORMANCE_LOG_ENTRY
*) (PeiPerformanceLog
+ 1);
314 // Make sure that LogEntryKey is a valid log entry key.
316 ASSERT (LogEntryKey
<= NumberOfEntries
);
318 if (LogEntryKey
== NumberOfEntries
) {
322 CurrentLogEntry
= &(LogEntryArray
[LogEntryKey
++]);
324 *Handle
= (VOID
*) (UINTN
) (CurrentLogEntry
->Handle
);
325 *Token
= CurrentLogEntry
->Token
;
326 *Module
= CurrentLogEntry
->Module
;
327 *StartTimeStamp
= CurrentLogEntry
->StartTimeStamp
;
328 *EndTimeStamp
= CurrentLogEntry
->EndTimeStamp
;
334 Returns TRUE if the performance measurement macros are enabled.
336 This function returns TRUE if the PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
337 PcdPerformanceLibraryPropertyMask is set. Otherwise FALSE is returned.
339 @retval TRUE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
340 PcdPerformanceLibraryPropertyMask is set.
341 @retval FALSE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
342 PcdPerformanceLibraryPropertyMask is clear.
347 PerformanceMeasurementEnabled (
351 return (BOOLEAN
) ((PcdGet8(PcdPerformanceLibraryPropertyMask
) & PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED
) != 0);