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.
47 PEI_PERFORMANCE_LOG_HEADER
*
48 InternalGetPerformanceHobLog (
52 EFI_HOB_GUID_TYPE
*GuidHob
;
53 PEI_PERFORMANCE_LOG_HEADER
*PeiPerformanceLog
;
54 UINTN PeiPerformanceLogSize
;
56 GuidHob
= GetFirstGuidHob (&gPeiPerformanceHobGuid
);
58 if (GuidHob
!= NULL
) {
60 // PEI Performance HOB was found, then return the existing one.
62 PeiPerformanceLog
= GET_GUID_HOB_DATA (GuidHob
);
65 // PEI Performance HOB was not found, then build one.
67 PeiPerformanceLogSize
= sizeof (PEI_PERFORMANCE_LOG_HEADER
) +
68 sizeof (PEI_PERFORMANCE_LOG_ENTRY
) * PcdGet8 (PcdMaxPeiPerformanceLogEntries
);
69 PeiPerformanceLog
= BuildGuidHob (&gPeiPerformanceHobGuid
, PeiPerformanceLogSize
);
70 PeiPerformanceLog
= ZeroMem (PeiPerformanceLog
, PeiPerformanceLogSize
);
73 return PeiPerformanceLog
;
77 Searches in the log array with keyword Handle, Token and Module.
79 This internal function searches for the log entry in the log array.
80 If there is an entry that exactly matches the given key word triple
81 and its end time stamp is zero, then the index of that log entry is returned;
82 otherwise, the the number of log entries in the array is returned.
84 @param Handle Pointer to environment specific context used
85 to identify the component being measured.
86 @param Token Pointer to a Null-terminated ASCII string
87 that identifies the component being measured.
88 @param Module Pointer to a Null-terminated ASCII string
89 that identifies the module being measured.
91 @retval The index of log entry in the array.
96 InternalSearchForLogEntry (
97 IN PEI_PERFORMANCE_LOG_HEADER
*PeiPerformanceLog
,
98 IN CONST VOID
*Handle
, OPTIONAL
99 IN CONST CHAR8
*Token
, OPTIONAL
100 IN CONST CHAR8
*Module OPTIONAL
104 UINT32 NumberOfEntries
;
105 PEI_PERFORMANCE_LOG_ENTRY
*LogEntryArray
;
111 if (Module
== NULL
) {
114 NumberOfEntries
= PeiPerformanceLog
->NumberOfEntries
;
115 LogEntryArray
= (PEI_PERFORMANCE_LOG_ENTRY
*) (PeiPerformanceLog
+ 1);
117 for (Index
= 0; Index
< NumberOfEntries
; Index
++) {
118 if ((LogEntryArray
[Index
].Handle
== (EFI_PHYSICAL_ADDRESS
) (UINTN
) Handle
) &&
119 AsciiStrnCmp (LogEntryArray
[Index
].Token
, Token
, PEI_PERFORMANCE_STRING_LENGTH
) == 0 &&
120 AsciiStrnCmp (LogEntryArray
[Index
].Module
, Module
, PEI_PERFORMANCE_STRING_LENGTH
) == 0 &&
121 LogEntryArray
[Index
].EndTimeStamp
== 0
130 Creates a record for the beginning of a performance measurement.
132 Creates a record that contains the Handle, Token, and Module.
133 If TimeStamp is not zero, then TimeStamp is added to the record as the start time.
134 If TimeStamp is zero, then this function reads the current time stamp
135 and adds that time stamp value to the record as the start time.
137 @param Handle Pointer to environment specific context used
138 to identify the component being measured.
139 @param Token Pointer to a Null-terminated ASCII string
140 that identifies the component being measured.
141 @param Module Pointer to a Null-terminated ASCII string
142 that identifies the module being measured.
143 @param TimeStamp 64-bit time stamp.
145 @retval RETURN_SUCCESS The start of the measurement was recorded.
146 @retval RETURN_OUT_OF_RESOURCES There are not enough resources to record the measurement.
151 StartPerformanceMeasurement (
152 IN CONST VOID
*Handle
, OPTIONAL
153 IN CONST CHAR8
*Token
, OPTIONAL
154 IN CONST CHAR8
*Module
, OPTIONAL
158 PEI_PERFORMANCE_LOG_HEADER
*PeiPerformanceLog
;
159 PEI_PERFORMANCE_LOG_ENTRY
*LogEntryArray
;
162 PeiPerformanceLog
= InternalGetPerformanceHobLog ();
164 if (PeiPerformanceLog
->NumberOfEntries
>= PcdGet8 (PcdMaxPeiPerformanceLogEntries
)) {
165 return RETURN_OUT_OF_RESOURCES
;
167 Index
= PeiPerformanceLog
->NumberOfEntries
++;
168 LogEntryArray
= (PEI_PERFORMANCE_LOG_ENTRY
*) (PeiPerformanceLog
+ 1);
169 LogEntryArray
[Index
].Handle
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) Handle
;
172 AsciiStrnCpy (LogEntryArray
[Index
].Token
, Token
, PEI_PERFORMANCE_STRING_LENGTH
);
174 if (Module
!= NULL
) {
175 AsciiStrnCpy (LogEntryArray
[Index
].Module
, Module
, PEI_PERFORMANCE_STRING_LENGTH
);
178 if (TimeStamp
== 0) {
179 TimeStamp
= GetPerformanceCounter ();
181 LogEntryArray
[Index
].StartTimeStamp
= TimeStamp
;
183 return RETURN_SUCCESS
;
187 Fills in the end time of a performance measurement.
189 Looks up the record that matches Handle, Token, and Module.
190 If the record can not be found then return RETURN_NOT_FOUND.
191 If the record is found and TimeStamp is not zero,
192 then TimeStamp is added to the record as the end time.
193 If the record is found and TimeStamp is zero, then this function reads
194 the current time stamp and adds that time stamp value to the record as the end time.
195 If this function is called multiple times for the same record, then the end time is overwritten.
197 @param Handle Pointer to environment specific context used
198 to identify the component being measured.
199 @param Token Pointer to a Null-terminated ASCII string
200 that identifies the component being measured.
201 @param Module Pointer to a Null-terminated ASCII string
202 that identifies the module being measured.
203 @param TimeStamp 64-bit time stamp.
205 @retval RETURN_SUCCESS The end of the measurement was recorded.
206 @retval RETURN_NOT_FOUND The specified measurement record could not be found.
211 EndPerformanceMeasurement (
212 IN CONST VOID
*Handle
, OPTIONAL
213 IN CONST CHAR8
*Token
, OPTIONAL
214 IN CONST CHAR8
*Module
, OPTIONAL
218 PEI_PERFORMANCE_LOG_HEADER
*PeiPerformanceLog
;
219 PEI_PERFORMANCE_LOG_ENTRY
*LogEntryArray
;
222 if (TimeStamp
== 0) {
223 TimeStamp
= GetPerformanceCounter ();
226 PeiPerformanceLog
= InternalGetPerformanceHobLog ();
227 Index
= InternalSearchForLogEntry (PeiPerformanceLog
, Handle
, Token
, Module
);
228 if (Index
>= PeiPerformanceLog
->NumberOfEntries
) {
229 return RETURN_NOT_FOUND
;
231 LogEntryArray
= (PEI_PERFORMANCE_LOG_ENTRY
*) (PeiPerformanceLog
+ 1);
232 LogEntryArray
[Index
].EndTimeStamp
= TimeStamp
;
234 return RETURN_SUCCESS
;
238 Attempts to retrieve a performance measurement log entry from the performance measurement log.
240 Attempts to retrieve the performance log entry specified by LogEntryKey. If LogEntryKey is
241 zero on entry, then an attempt is made to retrieve the first entry from the performance log,
242 and the key for the second entry in the log is returned. If the performance log is empty,
243 then no entry is retrieved and zero is returned. If LogEntryKey is not zero, then the performance
244 log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is
245 returned. If LogEntryKey is the key for the last entry in the log, then the last log entry is
246 retrieved and an implementation specific non-zero key value that specifies the end of the performance
247 log is returned. If LogEntryKey is equal this implementation specific non-zero key value, then no entry
248 is retrieved and zero is returned. In the cases where a performance log entry can be returned,
249 the log entry is returned in Handle, Token, Module, StartTimeStamp, and EndTimeStamp.
250 If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().
251 If Handle is NULL, then ASSERT().
252 If Token is NULL, then ASSERT().
253 If Module is NULL, then ASSERT().
254 If StartTimeStamp is NULL, then ASSERT().
255 If EndTimeStamp is NULL, then ASSERT().
257 @param LogEntryKey On entry, the key of the performance measurement log entry to retrieve.
258 0, then the first performance measurement log entry is retrieved.
259 On exit, the key of the next performance lof entry entry.
260 @param Handle Pointer to environment specific context used to identify the component
262 @param Token Pointer to a Null-terminated ASCII string that identifies the component
264 @param Module Pointer to a Null-terminated ASCII string that identifies the module
266 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
268 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
271 @return The key for the next performance log entry (in general case).
276 GetPerformanceMeasurement (
277 IN UINTN LogEntryKey
,
278 OUT CONST VOID
**Handle
,
279 OUT CONST CHAR8
**Token
,
280 OUT CONST CHAR8
**Module
,
281 OUT UINT64
*StartTimeStamp
,
282 OUT UINT64
*EndTimeStamp
285 PEI_PERFORMANCE_LOG_HEADER
*PeiPerformanceLog
;
286 PEI_PERFORMANCE_LOG_ENTRY
*CurrentLogEntry
;
287 PEI_PERFORMANCE_LOG_ENTRY
*LogEntryArray
;
288 UINTN NumberOfEntries
;
290 ASSERT (Handle
!= NULL
);
291 ASSERT (Token
!= NULL
);
292 ASSERT (Module
!= NULL
);
293 ASSERT (StartTimeStamp
!= NULL
);
294 ASSERT (EndTimeStamp
!= NULL
);
296 PeiPerformanceLog
= InternalGetPerformanceHobLog ();
298 NumberOfEntries
= (UINTN
) (PeiPerformanceLog
->NumberOfEntries
);
299 LogEntryArray
= (PEI_PERFORMANCE_LOG_ENTRY
*) (PeiPerformanceLog
+ 1);
301 // Make sure that LogEntryKey is a valid log entry key.
303 ASSERT (LogEntryKey
<= NumberOfEntries
);
305 if (LogEntryKey
== NumberOfEntries
) {
309 CurrentLogEntry
= &(LogEntryArray
[LogEntryKey
++]);
311 *Handle
= (VOID
*) (UINTN
) (CurrentLogEntry
->Handle
);
312 *Token
= CurrentLogEntry
->Token
;
313 *Module
= CurrentLogEntry
->Module
;
314 *StartTimeStamp
= CurrentLogEntry
->StartTimeStamp
;
315 *EndTimeStamp
= CurrentLogEntry
->EndTimeStamp
;
321 Returns TRUE if the performance measurement macros are enabled.
323 This function returns TRUE if the PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
324 PcdPerformanceLibraryPropertyMask is set. Otherwise FALSE is returned.
326 @retval TRUE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
327 PcdPerformanceLibraryPropertyMask is set.
328 @retval FALSE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
329 PcdPerformanceLibraryPropertyMask is clear.
334 PerformanceMeasurementEnabled (
338 return (BOOLEAN
) ((PcdGet8(PcdPerformanceLibraryPropertyMask
) & PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED
) != 0);