4 Copyright (c) 2006 - 2008, Intel Corporation. <BR>
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 #include <Guid/PeiPerformanceHob.h>
20 #include <Library/PerformanceLib.h>
21 #include <Library/DebugLib.h>
22 #include <Library/HobLib.h>
23 #include <Library/BaseLib.h>
24 #include <Library/TimerLib.h>
25 #include <Library/PcdLib.h>
26 #include <Library/BaseMemoryLib.h>
30 Gets PEI the GUID HOB for PEI performance.
32 This internal function searches for the GUID HOB for PEI performance.
33 If that GUID HOB is not found, it will build a new one.
34 It returns the data area of that GUID HOB to record performance log.
36 @param Handle Pointer to environment specific context used
37 to identify the component being measured.
38 @param Token Pointer to a Null-terminated ASCII string
39 that identifies the component being measured.
40 @param Module Pointer to a Null-terminated ASCII string
41 that identifies the module being measured.
43 @retval The index of log entry in the array.
46 PEI_PERFORMANCE_LOG_HEADER
*
47 InternalGetPerformanceHobLog (
51 EFI_HOB_GUID_TYPE
*GuidHob
;
52 PEI_PERFORMANCE_LOG_HEADER
*PeiPerformanceLog
;
53 UINTN PeiPerformanceLogSize
;
55 GuidHob
= GetFirstGuidHob (&gPeiPerformanceHobGuid
);
57 if (GuidHob
!= NULL
) {
59 // PEI Performance HOB was found, then return the existing one.
61 PeiPerformanceLog
= GET_GUID_HOB_DATA (GuidHob
);
64 // PEI Performance HOB was not found, then build one.
66 PeiPerformanceLogSize
= sizeof (PEI_PERFORMANCE_LOG_HEADER
) +
67 sizeof (PEI_PERFORMANCE_LOG_ENTRY
) * PcdGet8 (PcdMaxPeiPerformanceLogEntries
);
68 PeiPerformanceLog
= BuildGuidHob (&gPeiPerformanceHobGuid
, PeiPerformanceLogSize
);
69 PeiPerformanceLog
= ZeroMem (PeiPerformanceLog
, PeiPerformanceLogSize
);
72 return PeiPerformanceLog
;
76 Searches in the log array with keyword Handle, Token and Module.
78 This internal function searches for the log entry in the log array.
79 If there is an entry that exactly matches the given key word triple
80 and its end time stamp is zero, then the index of that log entry is returned;
81 otherwise, the the number of log entries in the array is returned.
83 @param Handle Pointer to environment specific context used
84 to identify the component being measured.
85 @param Token Pointer to a Null-terminated ASCII string
86 that identifies the component being measured.
87 @param Module Pointer to a Null-terminated ASCII string
88 that identifies the module being measured.
90 @retval The index of log entry in the array.
94 InternalSearchForLogEntry (
95 IN PEI_PERFORMANCE_LOG_HEADER
*PeiPerformanceLog
,
96 IN CONST VOID
*Handle
, OPTIONAL
97 IN CONST CHAR8
*Token
, OPTIONAL
98 IN CONST CHAR8
*Module OPTIONAL
102 UINT32 NumberOfEntries
;
103 PEI_PERFORMANCE_LOG_ENTRY
*LogEntryArray
;
109 if (Module
== NULL
) {
112 NumberOfEntries
= PeiPerformanceLog
->NumberOfEntries
;
113 LogEntryArray
= (PEI_PERFORMANCE_LOG_ENTRY
*) (PeiPerformanceLog
+ 1);
115 for (Index
= 0; Index
< NumberOfEntries
; Index
++) {
116 if ((LogEntryArray
[Index
].Handle
== (EFI_PHYSICAL_ADDRESS
) (UINTN
) Handle
) &&
117 AsciiStrnCmp (LogEntryArray
[Index
].Token
, Token
, PEI_PERFORMANCE_STRING_LENGTH
) == 0 &&
118 AsciiStrnCmp (LogEntryArray
[Index
].Module
, Module
, PEI_PERFORMANCE_STRING_LENGTH
) == 0 &&
119 LogEntryArray
[Index
].EndTimeStamp
== 0
128 Creates a record for the beginning of a performance measurement.
130 Creates a record that contains the Handle, Token, and Module.
131 If TimeStamp is not zero, then TimeStamp is added to the record as the start time.
132 If TimeStamp is zero, then this function reads the current time stamp
133 and adds that time stamp value to the record as the start time.
135 @param Handle Pointer to environment specific context used
136 to identify the component being measured.
137 @param Token Pointer to a Null-terminated ASCII string
138 that identifies the component being measured.
139 @param Module Pointer to a Null-terminated ASCII string
140 that identifies the module being measured.
141 @param TimeStamp 64-bit time stamp.
143 @retval RETURN_SUCCESS The start of the measurement was recorded.
144 @retval RETURN_OUT_OF_RESOURCES There are not enough resources to record the measurement.
149 StartPerformanceMeasurement (
150 IN CONST VOID
*Handle
, OPTIONAL
151 IN CONST CHAR8
*Token
, OPTIONAL
152 IN CONST CHAR8
*Module
, OPTIONAL
156 PEI_PERFORMANCE_LOG_HEADER
*PeiPerformanceLog
;
157 PEI_PERFORMANCE_LOG_ENTRY
*LogEntryArray
;
160 PeiPerformanceLog
= InternalGetPerformanceHobLog ();
162 if (PeiPerformanceLog
->NumberOfEntries
>= PcdGet8 (PcdMaxPeiPerformanceLogEntries
)) {
163 return RETURN_OUT_OF_RESOURCES
;
165 Index
= PeiPerformanceLog
->NumberOfEntries
++;
166 LogEntryArray
= (PEI_PERFORMANCE_LOG_ENTRY
*) (PeiPerformanceLog
+ 1);
167 LogEntryArray
[Index
].Handle
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) Handle
;
170 AsciiStrnCpy (LogEntryArray
[Index
].Token
, Token
, PEI_PERFORMANCE_STRING_LENGTH
);
172 if (Module
!= NULL
) {
173 AsciiStrnCpy (LogEntryArray
[Index
].Module
, Module
, PEI_PERFORMANCE_STRING_LENGTH
);
176 if (TimeStamp
== 0) {
177 TimeStamp
= GetPerformanceCounter ();
179 LogEntryArray
[Index
].StartTimeStamp
= TimeStamp
;
181 return RETURN_SUCCESS
;
185 Fills in the end time of a performance measurement.
187 Looks up the record that matches Handle, Token, and Module.
188 If the record can not be found then return RETURN_NOT_FOUND.
189 If the record is found and TimeStamp is not zero,
190 then TimeStamp is added to the record as the end time.
191 If the record is found and TimeStamp is zero, then this function reads
192 the current time stamp and adds that time stamp value to the record as the end time.
193 If this function is called multiple times for the same record, then the end time is overwritten.
195 @param Handle Pointer to environment specific context used
196 to identify the component being measured.
197 @param Token Pointer to a Null-terminated ASCII string
198 that identifies the component being measured.
199 @param Module Pointer to a Null-terminated ASCII string
200 that identifies the module being measured.
201 @param TimeStamp 64-bit time stamp.
203 @retval RETURN_SUCCESS The end of the measurement was recorded.
204 @retval RETURN_NOT_FOUND The specified measurement record could not be found.
209 EndPerformanceMeasurement (
210 IN CONST VOID
*Handle
, OPTIONAL
211 IN CONST CHAR8
*Token
, OPTIONAL
212 IN CONST CHAR8
*Module
, OPTIONAL
216 PEI_PERFORMANCE_LOG_HEADER
*PeiPerformanceLog
;
217 PEI_PERFORMANCE_LOG_ENTRY
*LogEntryArray
;
220 if (TimeStamp
== 0) {
221 TimeStamp
= GetPerformanceCounter ();
224 PeiPerformanceLog
= InternalGetPerformanceHobLog ();
225 Index
= InternalSearchForLogEntry (PeiPerformanceLog
, Handle
, Token
, Module
);
226 if (Index
>= PeiPerformanceLog
->NumberOfEntries
) {
227 return RETURN_NOT_FOUND
;
229 LogEntryArray
= (PEI_PERFORMANCE_LOG_ENTRY
*) (PeiPerformanceLog
+ 1);
230 LogEntryArray
[Index
].EndTimeStamp
= TimeStamp
;
232 return RETURN_SUCCESS
;
236 Attempts to retrieve a performance measurement log entry from the performance measurement log.
238 Attempts to retrieve the performance log entry specified by LogEntryKey. If LogEntryKey is
239 zero on entry, then an attempt is made to retrieve the first entry from the performance log,
240 and the key for the second entry in the log is returned. If the performance log is empty,
241 then no entry is retrieved and zero is returned. If LogEntryKey is not zero, then the performance
242 log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is
243 returned. If LogEntryKey is the key for the last entry in the log, then the last log entry is
244 retrieved and an implementation specific non-zero key value that specifies the end of the performance
245 log is returned. If LogEntryKey is equal this implementation specific non-zero key value, then no entry
246 is retrieved and zero is returned. In the cases where a performance log entry can be returned,
247 the log entry is returned in Handle, Token, Module, StartTimeStamp, and EndTimeStamp.
248 If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().
249 If Handle is NULL, then ASSERT().
250 If Token is NULL, then ASSERT().
251 If Module is NULL, then ASSERT().
252 If StartTimeStamp is NULL, then ASSERT().
253 If EndTimeStamp is NULL, then ASSERT().
255 @param LogEntryKey On entry, the key of the performance measurement log entry to retrieve.
256 0, then the first performance measurement log entry is retrieved.
257 On exit, the key of the next performance lof entry entry.
258 @param Handle Pointer to environment specific context used to identify the component
260 @param Token Pointer to a Null-terminated ASCII string that identifies the component
262 @param Module Pointer to a Null-terminated ASCII string that identifies the module
264 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
266 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
269 @return The key for the next performance log entry (in general case).
274 GetPerformanceMeasurement (
275 IN UINTN LogEntryKey
,
276 OUT CONST VOID
**Handle
,
277 OUT CONST CHAR8
**Token
,
278 OUT CONST CHAR8
**Module
,
279 OUT UINT64
*StartTimeStamp
,
280 OUT UINT64
*EndTimeStamp
283 PEI_PERFORMANCE_LOG_HEADER
*PeiPerformanceLog
;
284 PEI_PERFORMANCE_LOG_ENTRY
*CurrentLogEntry
;
285 PEI_PERFORMANCE_LOG_ENTRY
*LogEntryArray
;
286 UINTN NumberOfEntries
;
288 ASSERT (Handle
!= NULL
);
289 ASSERT (Token
!= NULL
);
290 ASSERT (Module
!= NULL
);
291 ASSERT (StartTimeStamp
!= NULL
);
292 ASSERT (EndTimeStamp
!= NULL
);
294 PeiPerformanceLog
= InternalGetPerformanceHobLog ();
296 NumberOfEntries
= (UINTN
) (PeiPerformanceLog
->NumberOfEntries
);
297 LogEntryArray
= (PEI_PERFORMANCE_LOG_ENTRY
*) (PeiPerformanceLog
+ 1);
299 // Make sure that LogEntryKey is a valid log entry key.
301 ASSERT (LogEntryKey
<= NumberOfEntries
);
303 if (LogEntryKey
== NumberOfEntries
) {
307 CurrentLogEntry
= &(LogEntryArray
[LogEntryKey
++]);
309 *Handle
= (VOID
*) (UINTN
) (CurrentLogEntry
->Handle
);
310 *Token
= CurrentLogEntry
->Token
;
311 *Module
= CurrentLogEntry
->Module
;
312 *StartTimeStamp
= CurrentLogEntry
->StartTimeStamp
;
313 *EndTimeStamp
= CurrentLogEntry
->EndTimeStamp
;
319 Returns TRUE if the performance measurement macros are enabled.
321 This function returns TRUE if the PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
322 PcdPerformanceLibraryPropertyMask is set. Otherwise FALSE is returned.
324 @retval TRUE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
325 PcdPerformanceLibraryPropertyMask is set.
326 @retval FALSE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
327 PcdPerformanceLibraryPropertyMask is clear.
332 PerformanceMeasurementEnabled (
336 return (BOOLEAN
) ((PcdGet8(PcdPerformanceLibraryPropertyMask
) & PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED
) != 0);