2 Performance Library used in SMM phase.
4 This library instance provides infrastructure for SMM drivers to log performance
5 data. It consumes SMM PerformanceEx or Performance Protocol published by SmmCorePerformanceLib
6 to log performance data. If both SMM PerformanceEx and Performance Protocol are not available, it does not log any
7 performance information.
9 Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved.<BR>
10 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.
21 #include <Guid/PerformanceMeasurement.h>
23 #include <Library/PerformanceLib.h>
24 #include <Library/DebugLib.h>
25 #include <Library/SmmServicesTableLib.h>
26 #include <Library/PcdLib.h>
27 #include <Library/BaseMemoryLib.h>
30 // The cached SMM Performance Protocol and SMM PerformanceEx Protocol interface.
31 EDKII_PERFORMANCE_MEASUREMENT_PROTOCOL
*mPerformanceMeasurement
= NULL
;
32 BOOLEAN mPerformanceMeasurementEnabled
;
35 The constructor function initializes the Performance Measurement Enable flag
37 @param ImageHandle The firmware allocated handle for the EFI image.
38 @param SystemTable A pointer to the EFI System Table.
40 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
45 SmmPerformanceLibConstructor (
46 IN EFI_HANDLE ImageHandle
,
47 IN EFI_SYSTEM_TABLE
*SystemTable
51 mPerformanceMeasurementEnabled
= (BOOLEAN
) ((PcdGet8(PcdPerformanceLibraryPropertyMask
) & PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED
) != 0);
57 The function caches the pointers to SMM PerformanceEx protocol and Performance Protocol.
59 The function locates SMM PerformanceEx protocol and Performance Protocol from protocol database.
61 @retval EFI_SUCCESS SMM PerformanceEx protocol or Performance Protocol is successfully located.
62 @retval EFI_NOT_FOUND Both SMM PerformanceEx protocol and Performance Protocol are not located to log performance.
66 GetPerformanceMeasurementProtocol (
71 EDKII_PERFORMANCE_MEASUREMENT_PROTOCOL
*PerformanceMeasurement
;
73 if (mPerformanceMeasurement
!= NULL
) {
77 Status
= gSmst
->SmmLocateProtocol (&gEdkiiSmmPerformanceMeasurementProtocolGuid
, NULL
, (VOID
**) &PerformanceMeasurement
);
78 if (!EFI_ERROR (Status
)) {
79 ASSERT (PerformanceMeasurement
!= NULL
);
81 // Cache PerformanceMeasurement Protocol.
83 mPerformanceMeasurement
= PerformanceMeasurement
;
90 Creates a record for the beginning of a performance measurement.
92 Creates a record that contains the Handle, Token, Module and Identifier.
93 If TimeStamp is not zero, then TimeStamp is added to the record as the start time.
94 If TimeStamp is zero, then this function reads the current time stamp
95 and adds that time stamp value to the record as the start time.
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.
103 @param TimeStamp 64-bit time stamp.
104 @param Identifier 32-bit identifier. If the value is 0, the created record
105 is same as the one created by StartPerformanceMeasurement.
107 @retval RETURN_SUCCESS The start of the measurement was recorded.
108 @retval RETURN_OUT_OF_RESOURCES There are not enough resources to record the measurement.
113 StartPerformanceMeasurementEx (
114 IN CONST VOID
*Handle
, OPTIONAL
115 IN CONST CHAR8
*Token
, OPTIONAL
116 IN CONST CHAR8
*Module
, OPTIONAL
124 Status
= GetPerformanceMeasurementProtocol ();
125 if (EFI_ERROR (Status
)) {
126 return RETURN_NOT_FOUND
;
131 } else if (Module
!= NULL
) {
137 if (mPerformanceMeasurement
!= NULL
) {
138 Status
= mPerformanceMeasurement
->CreatePerformanceMeasurement (Handle
, NULL
, String
, TimeStamp
, 0, Identifier
, PerfStartEntry
);
143 return (RETURN_STATUS
) Status
;
147 Fills in the end time of a performance measurement.
149 Looks up the record that matches Handle, Token and Module.
150 If the record can not be found then return RETURN_NOT_FOUND.
151 If the record is found and TimeStamp is not zero,
152 then TimeStamp is added to the record as the end time.
153 If the record is found and TimeStamp is zero, then this function reads
154 the current time stamp and adds that time stamp value to the record as the end time.
156 @param Handle Pointer to environment specific context used
157 to identify the component being measured.
158 @param Token Pointer to a Null-terminated ASCII string
159 that identifies the component being measured.
160 @param Module Pointer to a Null-terminated ASCII string
161 that identifies the module being measured.
162 @param TimeStamp 64-bit time stamp.
163 @param Identifier 32-bit identifier. If the value is 0, the found record
164 is same as the one found by EndPerformanceMeasurement.
166 @retval RETURN_SUCCESS The end of the measurement was recorded.
167 @retval RETURN_NOT_FOUND The specified measurement record could not be found.
172 EndPerformanceMeasurementEx (
173 IN CONST VOID
*Handle
, OPTIONAL
174 IN CONST CHAR8
*Token
, OPTIONAL
175 IN CONST CHAR8
*Module
, OPTIONAL
183 Status
= GetPerformanceMeasurementProtocol ();
184 if (EFI_ERROR (Status
)) {
185 return RETURN_NOT_FOUND
;
190 } else if (Module
!= NULL
) {
196 if (mPerformanceMeasurement
!= NULL
) {
197 Status
= mPerformanceMeasurement
->CreatePerformanceMeasurement (Handle
, NULL
, String
, TimeStamp
, 0, Identifier
, PerfEndEntry
);
202 return (RETURN_STATUS
) Status
;
206 Attempts to retrieve a performance measurement log entry from the performance measurement log.
207 It can also retrieve the log created by StartPerformanceMeasurement and EndPerformanceMeasurement,
208 and then assign the Identifier with 0.
210 Attempts to retrieve the performance log entry specified by LogEntryKey. If LogEntryKey is
211 zero on entry, then an attempt is made to retrieve the first entry from the performance log,
212 and the key for the second entry in the log is returned. If the performance log is empty,
213 then no entry is retrieved and zero is returned. If LogEntryKey is not zero, then the performance
214 log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is
215 returned. If LogEntryKey is the key for the last entry in the log, then the last log entry is
216 retrieved and an implementation specific non-zero key value that specifies the end of the performance
217 log is returned. If LogEntryKey is equal this implementation specific non-zero key value, then no entry
218 is retrieved and zero is returned. In the cases where a performance log entry can be returned,
219 the log entry is returned in Handle, Token, Module, StartTimeStamp, EndTimeStamp and Identifier.
220 If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().
221 If Handle is NULL, then ASSERT().
222 If Token is NULL, then ASSERT().
223 If Module is NULL, then ASSERT().
224 If StartTimeStamp is NULL, then ASSERT().
225 If EndTimeStamp is NULL, then ASSERT().
226 If Identifier is NULL, then ASSERT().
228 @param LogEntryKey On entry, the key of the performance measurement log entry to retrieve.
229 0, then the first performance measurement log entry is retrieved.
230 On exit, the key of the next performance log entry.
231 @param Handle Pointer to environment specific context used to identify the component
233 @param Token Pointer to a Null-terminated ASCII string that identifies the component
235 @param Module Pointer to a Null-terminated ASCII string that identifies the module
237 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
239 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
241 @param Identifier Pointer to the 32-bit identifier that was recorded.
243 @return The key for the next performance log entry (in general case).
248 GetPerformanceMeasurementEx (
249 IN UINTN LogEntryKey
,
250 OUT CONST VOID
**Handle
,
251 OUT CONST CHAR8
**Token
,
252 OUT CONST CHAR8
**Module
,
253 OUT UINT64
*StartTimeStamp
,
254 OUT UINT64
*EndTimeStamp
,
255 OUT UINT32
*Identifier
262 Creates a record for the beginning of a performance measurement.
264 Creates a record that contains the Handle, Token, and Module.
265 If TimeStamp is not zero, then TimeStamp is added to the record as the start time.
266 If TimeStamp is zero, then this function reads the current time stamp
267 and adds that time stamp value to the record as the start time.
269 @param Handle Pointer to environment specific context used
270 to identify the component being measured.
271 @param Token Pointer to a Null-terminated ASCII string
272 that identifies the component being measured.
273 @param Module Pointer to a Null-terminated ASCII string
274 that identifies the module being measured.
275 @param TimeStamp 64-bit time stamp.
277 @retval RETURN_SUCCESS The start of the measurement was recorded.
278 @retval RETURN_OUT_OF_RESOURCES There are not enough resources to record the measurement.
283 StartPerformanceMeasurement (
284 IN CONST VOID
*Handle
, OPTIONAL
285 IN CONST CHAR8
*Token
, OPTIONAL
286 IN CONST CHAR8
*Module
, OPTIONAL
290 return StartPerformanceMeasurementEx (Handle
, Token
, Module
, TimeStamp
, 0);
294 Fills in the end time of a performance measurement.
296 Looks up the record that matches Handle, Token, and Module.
297 If the record can not be found then return RETURN_NOT_FOUND.
298 If the record is found and TimeStamp is not zero,
299 then TimeStamp is added to the record as the end time.
300 If the record is found and TimeStamp is zero, then this function reads
301 the current time stamp and adds that time stamp value to the record as the end time.
303 @param Handle Pointer to environment specific context used
304 to identify the component being measured.
305 @param Token Pointer to a Null-terminated ASCII string
306 that identifies the component being measured.
307 @param Module Pointer to a Null-terminated ASCII string
308 that identifies the module being measured.
309 @param TimeStamp 64-bit time stamp.
311 @retval RETURN_SUCCESS The end of the measurement was recorded.
312 @retval RETURN_NOT_FOUND The specified measurement record could not be found.
317 EndPerformanceMeasurement (
318 IN CONST VOID
*Handle
, OPTIONAL
319 IN CONST CHAR8
*Token
, OPTIONAL
320 IN CONST CHAR8
*Module
, OPTIONAL
324 return EndPerformanceMeasurementEx (Handle
, Token
, Module
, TimeStamp
, 0);
328 Attempts to retrieve a performance measurement log entry from the performance measurement log.
329 It can also retrieve the log created by StartPerformanceMeasurementEx and EndPerformanceMeasurementEx,
330 and then eliminate the Identifier.
332 Attempts to retrieve the performance log entry specified by LogEntryKey. If LogEntryKey is
333 zero on entry, then an attempt is made to retrieve the first entry from the performance log,
334 and the key for the second entry in the log is returned. If the performance log is empty,
335 then no entry is retrieved and zero is returned. If LogEntryKey is not zero, then the performance
336 log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is
337 returned. If LogEntryKey is the key for the last entry in the log, then the last log entry is
338 retrieved and an implementation specific non-zero key value that specifies the end of the performance
339 log is returned. If LogEntryKey is equal this implementation specific non-zero key value, then no entry
340 is retrieved and zero is returned. In the cases where a performance log entry can be returned,
341 the log entry is returned in Handle, Token, Module, StartTimeStamp, and EndTimeStamp.
342 If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().
343 If Handle is NULL, then ASSERT().
344 If Token is NULL, then ASSERT().
345 If Module is NULL, then ASSERT().
346 If StartTimeStamp is NULL, then ASSERT().
347 If EndTimeStamp is NULL, then ASSERT().
349 @param LogEntryKey On entry, the key of the performance measurement log entry to retrieve.
350 0, then the first performance measurement log entry is retrieved.
351 On exit, the key of the next performance log entry.
352 @param Handle Pointer to environment specific context used to identify the component
354 @param Token Pointer to a Null-terminated ASCII string that identifies the component
356 @param Module Pointer to a Null-terminated ASCII string that identifies the module
358 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
360 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
363 @return The key for the next performance log entry (in general case).
368 GetPerformanceMeasurement (
369 IN UINTN LogEntryKey
,
370 OUT CONST VOID
**Handle
,
371 OUT CONST CHAR8
**Token
,
372 OUT CONST CHAR8
**Module
,
373 OUT UINT64
*StartTimeStamp
,
374 OUT UINT64
*EndTimeStamp
381 Returns TRUE if the performance measurement macros are enabled.
383 This function returns TRUE if the PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
384 PcdPerformanceLibraryPropertyMask is set. Otherwise FALSE is returned.
386 @retval TRUE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
387 PcdPerformanceLibraryPropertyMask is set.
388 @retval FALSE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
389 PcdPerformanceLibraryPropertyMask is clear.
394 PerformanceMeasurementEnabled (
398 return mPerformanceMeasurementEnabled
;
402 Create performance record with event description and a timestamp.
404 @param CallerIdentifier - Image handle or pointer to caller ID GUID
405 @param Guid - Pointer to a GUID
406 @param String - Pointer to a string describing the measurement
407 @param Address - Pointer to a location in memory relevant to the measurement
408 @param Identifier - Performance identifier describing the type of measurement
410 @retval RETURN_SUCCESS - Successfully created performance record
411 @retval RETURN_OUT_OF_RESOURCES - Ran out of space to store the records
412 @retval RETURN_INVALID_PARAMETER - Invalid parameter passed to function - NULL
413 pointer or invalid PerfId
418 LogPerformanceMeasurement (
419 IN CONST VOID
*CallerIdentifier
,
420 IN CONST VOID
*Guid
, OPTIONAL
421 IN CONST CHAR8
*String
, OPTIONAL
422 IN UINT64 Address
, OPTIONAL
428 Status
= GetPerformanceMeasurementProtocol ();
429 if (EFI_ERROR (Status
)) {
430 return RETURN_OUT_OF_RESOURCES
;
433 if (mPerformanceMeasurement
!= NULL
) {
434 Status
= mPerformanceMeasurement
->CreatePerformanceMeasurement (CallerIdentifier
, Guid
, String
, 0, Address
, Identifier
, PerfEntry
);
439 return (RETURN_STATUS
) Status
;
443 Check whether the specified performance measurement can be logged.
445 This function returns TRUE when the PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of PcdPerformanceLibraryPropertyMask is set
446 and the Type disable bit in PcdPerformanceLibraryPropertyMask is not set.
448 @param Type - Type of the performance measurement entry.
450 @retval TRUE The performance measurement can be logged.
451 @retval FALSE The performance measurement can NOT be logged.
456 LogPerformanceMeasurementEnabled (
461 // When Performance measurement is enabled and the type is not filtered, the performance can be logged.
463 if (PerformanceMeasurementEnabled () && (PcdGet8(PcdPerformanceLibraryPropertyMask
) & Type
) == 0) {