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.
25 #include <Guid/PeiPerformanceHob.h>
27 #include <Library/PerformanceLib.h>
28 #include <Library/DebugLib.h>
29 #include <Library/HobLib.h>
30 #include <Library/BaseLib.h>
31 #include <Library/TimerLib.h>
32 #include <Library/PcdLib.h>
33 #include <Library/BaseMemoryLib.h>
37 Gets PEI the GUID HOB for PEI performance.
39 This internal function searches for the GUID HOB for PEI performance.
40 If that GUID HOB is not found, it will build a new one.
41 It returns the data area of that GUID HOB to record performance log.
43 @param Handle Pointer to environment specific context used
44 to identify the component being measured.
45 @param Token Pointer to a Null-terminated ASCII string
46 that identifies the component being measured.
47 @param Module Pointer to a Null-terminated ASCII string
48 that identifies the module being measured.
50 @retval The index of log entry in the array.
54 PEI_PERFORMANCE_LOG_HEADER
*
55 InternalGetPerformanceHobLog (
59 EFI_HOB_GUID_TYPE
*GuidHob
;
60 PEI_PERFORMANCE_LOG_HEADER
*PeiPerformanceLog
;
61 UINTN PeiPerformanceLogSize
;
63 GuidHob
= GetFirstGuidHob (&gPeiPerformanceHobGuid
);
65 if (GuidHob
!= NULL
) {
67 // PEI Performance HOB was found, then return the existing one.
69 PeiPerformanceLog
= GET_GUID_HOB_DATA (GuidHob
);
72 // PEI Performance HOB was not found, then build one.
74 PeiPerformanceLogSize
= sizeof (PEI_PERFORMANCE_LOG_HEADER
) +
75 sizeof (PEI_PERFORMANCE_LOG_ENTRY
) * PcdGet8 (PcdMaxPeiPerformanceLogEntries
);
76 PeiPerformanceLog
= BuildGuidHob (&gPeiPerformanceHobGuid
, PeiPerformanceLogSize
);
77 PeiPerformanceLog
= ZeroMem (PeiPerformanceLog
, PeiPerformanceLogSize
);
80 return PeiPerformanceLog
;
84 Searches in the log array with keyword Handle, Token and Module.
86 This internal function searches for the log entry in the log array.
87 If there is an entry that exactly matches the given key word triple
88 and its end time stamp is zero, then the index of that log entry is returned;
89 otherwise, the the number of log entries in the array is returned.
91 @param Handle Pointer to environment specific context used
92 to identify the component being measured.
93 @param Token Pointer to a Null-terminated ASCII string
94 that identifies the component being measured.
95 @param Module Pointer to a Null-terminated ASCII string
96 that identifies the module being measured.
98 @retval The index of log entry in the array.
103 InternalSearchForLogEntry (
104 IN PEI_PERFORMANCE_LOG_HEADER
*PeiPerformanceLog
,
105 IN CONST VOID
*Handle
, OPTIONAL
106 IN CONST CHAR8
*Token
, OPTIONAL
107 IN CONST CHAR8
*Module OPTIONAL
111 UINT32 NumberOfEntries
;
112 PEI_PERFORMANCE_LOG_ENTRY
*LogEntryArray
;
118 if (Module
== NULL
) {
121 NumberOfEntries
= PeiPerformanceLog
->NumberOfEntries
;
122 LogEntryArray
= (PEI_PERFORMANCE_LOG_ENTRY
*) (PeiPerformanceLog
+ 1);
124 for (Index
= 0; Index
< NumberOfEntries
; Index
++) {
125 if ((LogEntryArray
[Index
].Handle
== (EFI_PHYSICAL_ADDRESS
) (UINTN
) Handle
) &&
126 AsciiStrnCmp (LogEntryArray
[Index
].Token
, Token
, PEI_PERFORMANCE_STRING_LENGTH
) == 0 &&
127 AsciiStrnCmp (LogEntryArray
[Index
].Module
, Module
, PEI_PERFORMANCE_STRING_LENGTH
) == 0 &&
128 LogEntryArray
[Index
].EndTimeStamp
== 0
137 Creates a record for the beginning of a performance measurement.
139 Creates a record that contains the Handle, Token, and Module.
140 If TimeStamp is not zero, then TimeStamp is added to the record as the start time.
141 If TimeStamp is zero, then this function reads the current time stamp
142 and adds that time stamp value to the record as the start time.
144 @param Handle Pointer to environment specific context used
145 to identify the component being measured.
146 @param Token Pointer to a Null-terminated ASCII string
147 that identifies the component being measured.
148 @param Module Pointer to a Null-terminated ASCII string
149 that identifies the module being measured.
150 @param TimeStamp 64-bit time stamp.
152 @retval RETURN_SUCCESS The start of the measurement was recorded.
153 @retval RETURN_OUT_OF_RESOURCES There are not enough resources to record the measurement.
158 StartPerformanceMeasurement (
159 IN CONST VOID
*Handle
, OPTIONAL
160 IN CONST CHAR8
*Token
, OPTIONAL
161 IN CONST CHAR8
*Module
, OPTIONAL
165 PEI_PERFORMANCE_LOG_HEADER
*PeiPerformanceLog
;
166 PEI_PERFORMANCE_LOG_ENTRY
*LogEntryArray
;
169 PeiPerformanceLog
= InternalGetPerformanceHobLog ();
171 if (PeiPerformanceLog
->NumberOfEntries
>= PcdGet8 (PcdMaxPeiPerformanceLogEntries
)) {
172 return RETURN_OUT_OF_RESOURCES
;
174 Index
= PeiPerformanceLog
->NumberOfEntries
++;
175 LogEntryArray
= (PEI_PERFORMANCE_LOG_ENTRY
*) (PeiPerformanceLog
+ 1);
176 LogEntryArray
[Index
].Handle
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) Handle
;
179 AsciiStrnCpy (LogEntryArray
[Index
].Token
, Token
, PEI_PERFORMANCE_STRING_LENGTH
);
181 if (Module
!= NULL
) {
182 AsciiStrnCpy (LogEntryArray
[Index
].Module
, Module
, PEI_PERFORMANCE_STRING_LENGTH
);
185 if (TimeStamp
== 0) {
186 TimeStamp
= GetPerformanceCounter ();
188 LogEntryArray
[Index
].StartTimeStamp
= TimeStamp
;
190 return RETURN_SUCCESS
;
194 Fills in the end time of a performance measurement.
196 Looks up the record that matches Handle, Token, and Module.
197 If the record can not be found then return RETURN_NOT_FOUND.
198 If the record is found and TimeStamp is not zero,
199 then TimeStamp is added to the record as the end time.
200 If the record is found and TimeStamp is zero, then this function reads
201 the current time stamp and adds that time stamp value to the record as the end time.
202 If this function is called multiple times for the same record, then the end time is overwritten.
204 @param Handle Pointer to environment specific context used
205 to identify the component being measured.
206 @param Token Pointer to a Null-terminated ASCII string
207 that identifies the component being measured.
208 @param Module Pointer to a Null-terminated ASCII string
209 that identifies the module being measured.
210 @param TimeStamp 64-bit time stamp.
212 @retval RETURN_SUCCESS The end of the measurement was recorded.
213 @retval RETURN_NOT_FOUND The specified measurement record could not be found.
218 EndPerformanceMeasurement (
219 IN CONST VOID
*Handle
, OPTIONAL
220 IN CONST CHAR8
*Token
, OPTIONAL
221 IN CONST CHAR8
*Module
, OPTIONAL
225 PEI_PERFORMANCE_LOG_HEADER
*PeiPerformanceLog
;
226 PEI_PERFORMANCE_LOG_ENTRY
*LogEntryArray
;
229 if (TimeStamp
== 0) {
230 TimeStamp
= GetPerformanceCounter ();
233 PeiPerformanceLog
= InternalGetPerformanceHobLog ();
234 Index
= InternalSearchForLogEntry (PeiPerformanceLog
, Handle
, Token
, Module
);
235 if (Index
>= PeiPerformanceLog
->NumberOfEntries
) {
236 return RETURN_NOT_FOUND
;
238 LogEntryArray
= (PEI_PERFORMANCE_LOG_ENTRY
*) (PeiPerformanceLog
+ 1);
239 LogEntryArray
[Index
].EndTimeStamp
= TimeStamp
;
241 return RETURN_SUCCESS
;
245 Attempts to retrieve a performance measurement log entry from the performance measurement log.
247 Attempts to retrieve the performance log entry specified by LogEntryKey. If LogEntryKey is
248 zero on entry, then an attempt is made to retrieve the first entry from the performance log,
249 and the key for the second entry in the log is returned. If the performance log is empty,
250 then no entry is retrieved and zero is returned. If LogEntryKey is not zero, then the performance
251 log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is
252 returned. If LogEntryKey is the key for the last entry in the log, then the last log entry is
253 retrieved and an implementation specific non-zero key value that specifies the end of the performance
254 log is returned. If LogEntryKey is equal this implementation specific non-zero key value, then no entry
255 is retrieved and zero is returned. In the cases where a performance log entry can be returned,
256 the log entry is returned in Handle, Token, Module, StartTimeStamp, and EndTimeStamp.
257 If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().
258 If Handle is NULL, then ASSERT().
259 If Token is NULL, then ASSERT().
260 If Module is NULL, then ASSERT().
261 If StartTimeStamp is NULL, then ASSERT().
262 If EndTimeStamp is NULL, then ASSERT().
264 @param LogEntryKey On entry, the key of the performance measurement log entry to retrieve.
265 0, then the first performance measurement log entry is retrieved.
266 On exit, the key of the next performance lof entry entry.
267 @param Handle Pointer to environment specific context used to identify the component
269 @param Token Pointer to a Null-terminated ASCII string that identifies the component
271 @param Module Pointer to a Null-terminated ASCII string that identifies the module
273 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
275 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
278 @return The key for the next performance log entry (in general case).
283 GetPerformanceMeasurement (
284 IN UINTN LogEntryKey
,
285 OUT CONST VOID
**Handle
,
286 OUT CONST CHAR8
**Token
,
287 OUT CONST CHAR8
**Module
,
288 OUT UINT64
*StartTimeStamp
,
289 OUT UINT64
*EndTimeStamp
292 PEI_PERFORMANCE_LOG_HEADER
*PeiPerformanceLog
;
293 PEI_PERFORMANCE_LOG_ENTRY
*CurrentLogEntry
;
294 PEI_PERFORMANCE_LOG_ENTRY
*LogEntryArray
;
295 UINTN NumberOfEntries
;
297 ASSERT (Handle
!= NULL
);
298 ASSERT (Token
!= NULL
);
299 ASSERT (Module
!= NULL
);
300 ASSERT (StartTimeStamp
!= NULL
);
301 ASSERT (EndTimeStamp
!= NULL
);
303 PeiPerformanceLog
= InternalGetPerformanceHobLog ();
305 NumberOfEntries
= (UINTN
) (PeiPerformanceLog
->NumberOfEntries
);
306 LogEntryArray
= (PEI_PERFORMANCE_LOG_ENTRY
*) (PeiPerformanceLog
+ 1);
308 // Make sure that LogEntryKey is a valid log entry key.
310 ASSERT (LogEntryKey
<= NumberOfEntries
);
312 if (LogEntryKey
== NumberOfEntries
) {
316 CurrentLogEntry
= &(LogEntryArray
[LogEntryKey
++]);
318 *Handle
= (VOID
*) (UINTN
) (CurrentLogEntry
->Handle
);
319 *Token
= CurrentLogEntry
->Token
;
320 *Module
= CurrentLogEntry
->Module
;
321 *StartTimeStamp
= CurrentLogEntry
->StartTimeStamp
;
322 *EndTimeStamp
= CurrentLogEntry
->EndTimeStamp
;
328 Returns TRUE if the performance measurement macros are enabled.
330 This function returns TRUE if the PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
331 PcdPerformanceLibraryPropertyMask is set. Otherwise FALSE is returned.
333 @retval TRUE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
334 PcdPerformanceLibraryPropertyMask is set.
335 @retval FALSE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
336 PcdPerformanceLibraryPropertyMask is clear.
341 PerformanceMeasurementEnabled (
345 return (BOOLEAN
) ((PcdGet8(PcdPerformanceLibraryPropertyMask
) & PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED
) != 0);