2 Performance library instance used in DXE phase to dump SMM performance data.
4 This library instance allows a DXE driver or UEFI application to dump the SMM performance data.
5 StartPerformanceMeasurement() and EndPerformanceMeasurement() are not implemented.
7 Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>
8 This program and the accompanying materials
9 are licensed and made available under the terms and conditions of the BSD License
10 which accompanies this distribution. The full text of the license may be found at
11 http://opensource.org/licenses/bsd-license.php
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
21 #include <Guid/Performance.h>
23 #include <Library/PerformanceLib.h>
24 #include <Library/DebugLib.h>
25 #include <Library/UefiBootServicesTableLib.h>
26 #include <Library/UefiRuntimeServicesTableLib.h>
27 #include <Library/PcdLib.h>
28 #include <Library/BaseMemoryLib.h>
29 #include <Library/BaseLib.h>
30 #include <Library/MemoryAllocationLib.h>
32 #include <Protocol/SmmCommunication.h>
34 #define SMM_PERFORMANCE_COMMUNICATION_BUFFER_SIZE (OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data) + sizeof (SMM_PERF_COMMUNICATE))
36 // The cached performance protocol interface.
38 EFI_SMM_COMMUNICATION_PROTOCOL
*mSmmCommunication
= NULL
;
39 UINT8 mSmmPerformanceBuffer
[SMM_PERFORMANCE_COMMUNICATION_BUFFER_SIZE
];
40 GAUGE_DATA_ENTRY
*mGaugeData
= NULL
;
41 UINTN mGaugeNumberOfEntries
= 0;
45 The constructor function caches the pointer to SMM Communication protocol.
47 The constructor function locates Performance protocol from protocol database.
48 It will ASSERT() if that operation fails and it will always return EFI_SUCCESS.
50 @retval EFI_SUCCESS Performance protocol is successfully located.
51 @retval Other Performance protocol is not located to log performance.
55 GetCommunicationProtocol (
60 EFI_SMM_COMMUNICATION_PROTOCOL
*Communication
;
62 if (mSmmCommunication
!= NULL
) {
66 Status
= gBS
->LocateProtocol (&gEfiSmmCommunicationProtocolGuid
, NULL
, (VOID
**) &Communication
);
67 if (!EFI_ERROR (Status
)) {
68 ASSERT (Communication
!= NULL
);
70 // Cache SMM Communication protocol.
72 mSmmCommunication
= Communication
;
79 Creates a record for the beginning of a performance measurement.
81 Creates a record that contains the Handle, Token, and Module.
82 If TimeStamp is not zero, then TimeStamp is added to the record as the start time.
83 If TimeStamp is zero, then this function reads the current time stamp
84 and adds that time stamp value to the record as the start time.
86 @param Handle Pointer to environment specific context used
87 to identify the component being measured.
88 @param Token Pointer to a Null-terminated ASCII string
89 that identifies the component being measured.
90 @param Module Pointer to a Null-terminated ASCII string
91 that identifies the module being measured.
92 @param TimeStamp 64-bit time stamp.
94 @retval RETURN_SUCCESS The start of the measurement was recorded.
95 @retval RETURN_OUT_OF_RESOURCES There are not enough resources to record the measurement.
100 StartPerformanceMeasurement (
101 IN CONST VOID
*Handle
, OPTIONAL
102 IN CONST CHAR8
*Token
, OPTIONAL
103 IN CONST CHAR8
*Module
, OPTIONAL
107 return EFI_UNSUPPORTED
;
111 Fills in the end time of a performance measurement.
113 Looks up the record that matches Handle, Token, and Module.
114 If the record can not be found then return RETURN_NOT_FOUND.
115 If the record is found and TimeStamp is not zero,
116 then TimeStamp is added to the record as the end time.
117 If the record is found and TimeStamp is zero, then this function reads
118 the current time stamp and adds that time stamp value to the record as the end time.
119 If this function is called multiple times for the same record, then the end time is overwritten.
121 @param Handle Pointer to environment specific context used
122 to identify the component being measured.
123 @param Token Pointer to a Null-terminated ASCII string
124 that identifies the component being measured.
125 @param Module Pointer to a Null-terminated ASCII string
126 that identifies the module being measured.
127 @param TimeStamp 64-bit time stamp.
129 @retval RETURN_SUCCESS The end of the measurement was recorded.
130 @retval RETURN_NOT_FOUND The specified measurement record could not be found.
135 EndPerformanceMeasurement (
136 IN CONST VOID
*Handle
, OPTIONAL
137 IN CONST CHAR8
*Token
, OPTIONAL
138 IN CONST CHAR8
*Module
, OPTIONAL
142 return EFI_UNSUPPORTED
;
146 Retrieves all previous logged performance measurement.
147 Function will use SMM communicate protocol to get all previous SMM performance measurement data.
148 If success, data buffer will be returned. If fail function will return NULL.
150 @retval !NULL Get all gauge data success.
151 @retval NULL Get all guage data failed.
155 GetAllSmmGaugeData (VOID
)
158 EFI_SMM_COMMUNICATE_HEADER
*SmmCommBufferHeader
;
159 SMM_PERF_COMMUNICATE
*SmmPerfCommData
;
163 if (mGaugeData
!= NULL
) {
167 Status
= GetCommunicationProtocol ();
168 if (EFI_ERROR (Status
)) {
173 // Initialize communicate buffer
175 SmmCommBufferHeader
= (EFI_SMM_COMMUNICATE_HEADER
*)mSmmPerformanceBuffer
;
176 SmmPerfCommData
= (SMM_PERF_COMMUNICATE
*)SmmCommBufferHeader
->Data
;
177 ZeroMem((UINT8
*)SmmPerfCommData
, sizeof(SMM_PERF_COMMUNICATE
));
179 CopyGuid (&SmmCommBufferHeader
->HeaderGuid
, &gSmmPerformanceProtocolGuid
);
180 SmmCommBufferHeader
->MessageLength
= sizeof(SMM_PERF_COMMUNICATE
);
181 CommSize
= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER
, Data
) + sizeof(SMM_PERF_COMMUNICATE
);
184 // Get totol number of SMM gauge entries
186 SmmPerfCommData
->Function
= SMM_PERF_FUNCTION_GET_GAUGE_ENTRY_NUMBER
;
187 Status
= mSmmCommunication
->Communicate (mSmmCommunication
, mSmmPerformanceBuffer
, &CommSize
);
188 ASSERT_EFI_ERROR (Status
);
190 if (EFI_ERROR (SmmPerfCommData
->ReturnStatus
) || SmmPerfCommData
->NumberOfEntries
== 0) {
194 mGaugeNumberOfEntries
= SmmPerfCommData
->NumberOfEntries
;
196 DataSize
= mGaugeNumberOfEntries
* sizeof(GAUGE_DATA_ENTRY
);
197 mGaugeData
= AllocateZeroPool(DataSize
);
198 ASSERT_EFI_ERROR (Status
);
201 // Get all SMM gauge data
203 SmmPerfCommData
->Function
= SMM_PERF_FUNCTION_GET_GAUGE_DATA
;
204 SmmPerfCommData
->LogEntryKey
= 0;
205 SmmPerfCommData
->NumberOfEntries
= mGaugeNumberOfEntries
;
206 SmmPerfCommData
->GaugeData
= mGaugeData
;
207 Status
= mSmmCommunication
->Communicate (mSmmCommunication
, mSmmPerformanceBuffer
, &CommSize
);
208 ASSERT_EFI_ERROR (Status
);
209 ASSERT_EFI_ERROR(SmmPerfCommData
->ReturnStatus
);
215 Retrieves a previously logged performance measurement.
217 Retrieves the performance log entry from the performance log specified by LogEntryKey.
218 If it stands for a valid entry, then EFI_SUCCESS is returned and
219 GaugeDataEntry stores the pointer to that entry.
221 @param LogEntryKey The key for the previous performance measurement log entry.
222 If 0, then the first performance measurement log entry is retrieved.
223 @param GaugeDataEntry The indirect pointer to the gauge data entry specified by LogEntryKey
224 if the retrieval is successful.
226 @retval EFI_SUCCESS The GuageDataEntry is successfully found based on LogEntryKey.
227 @retval EFI_NOT_FOUND The LogEntryKey is the last entry (equals to the total entry number).
228 @retval EFI_INVALIDE_PARAMETER The LogEntryKey is not a valid entry (greater than the total entry number).
229 @retval EFI_INVALIDE_PARAMETER GaugeDataEntry is NULL.
235 IN UINTN LogEntryKey
,
236 OUT GAUGE_DATA_ENTRY
**GaugeDataEntry
239 if (LogEntryKey
> mGaugeNumberOfEntries
) {
240 return EFI_INVALID_PARAMETER
;
242 if (LogEntryKey
== mGaugeNumberOfEntries
) {
243 return EFI_NOT_FOUND
;
246 if (GaugeDataEntry
== NULL
) {
247 return EFI_INVALID_PARAMETER
;
249 *GaugeDataEntry
= &mGaugeData
[LogEntryKey
];
255 Attempts to retrieve a performance measurement log entry from the performance measurement log.
257 Attempts to retrieve the performance log entry specified by LogEntryKey. If LogEntryKey is
258 zero on entry, then an attempt is made to retrieve the first entry from the performance log,
259 and the key for the second entry in the log is returned. If the performance log is empty,
260 then no entry is retrieved and zero is returned. If LogEntryKey is not zero, then the performance
261 log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is
262 returned. If LogEntryKey is the key for the last entry in the log, then the last log entry is
263 retrieved and an implementation specific non-zero key value that specifies the end of the performance
264 log is returned. If LogEntryKey is equal this implementation specific non-zero key value, then no entry
265 is retrieved and zero is returned. In the cases where a performance log entry can be returned,
266 the log entry is returned in Handle, Token, Module, StartTimeStamp, and EndTimeStamp.
267 If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().
268 If Handle is NULL, then ASSERT().
269 If Token is NULL, then ASSERT().
270 If Module is NULL, then ASSERT().
271 If StartTimeStamp is NULL, then ASSERT().
272 If EndTimeStamp is NULL, then ASSERT().
274 @param LogEntryKey On entry, the key of the performance measurement log entry to retrieve.
275 0, then the first performance measurement log entry is retrieved.
276 On exit, the key of the next performance log entry.
277 @param Handle Pointer to environment specific context used to identify the component
279 @param Token Pointer to a Null-terminated ASCII string that identifies the component
281 @param Module Pointer to a Null-terminated ASCII string that identifies the module
283 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
285 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
288 @return The key for the next performance log entry (in general case).
293 GetPerformanceMeasurement (
294 IN UINTN LogEntryKey
,
295 OUT CONST VOID
**Handle
,
296 OUT CONST CHAR8
**Token
,
297 OUT CONST CHAR8
**Module
,
298 OUT UINT64
*StartTimeStamp
,
299 OUT UINT64
*EndTimeStamp
303 GAUGE_DATA_ENTRY
*GaugeData
;
307 ASSERT (Handle
!= NULL
);
308 ASSERT (Token
!= NULL
);
309 ASSERT (Module
!= NULL
);
310 ASSERT (StartTimeStamp
!= NULL
);
311 ASSERT (EndTimeStamp
!= NULL
);
313 mGaugeData
= GetAllSmmGaugeData();
314 if (mGaugeData
== NULL
) {
318 Status
= GetGauge (LogEntryKey
++, &GaugeData
);
321 // Make sure that LogEntryKey is a valid log entry key,
323 ASSERT (Status
!= EFI_INVALID_PARAMETER
);
325 if (EFI_ERROR (Status
)) {
327 // The LogEntryKey is the last entry (equals to the total entry number).
333 ASSERT (GaugeData
!= NULL
);
335 *Handle
= (VOID
*) (UINTN
) GaugeData
->Handle
;
336 *Token
= GaugeData
->Token
;
337 *Module
= GaugeData
->Module
;
338 *StartTimeStamp
= GaugeData
->StartTimeStamp
;
339 *EndTimeStamp
= GaugeData
->EndTimeStamp
;
345 Returns TRUE if the performance measurement macros are enabled.
347 This function returns TRUE if the PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
348 PcdPerformanceLibraryPropertyMask is set. Otherwise FALSE is returned.
350 @retval TRUE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
351 PcdPerformanceLibraryPropertyMask is set.
352 @retval FALSE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
353 PcdPerformanceLibraryPropertyMask is clear.
358 PerformanceMeasurementEnabled (
362 return (BOOLEAN
) ((PcdGet8(PcdPerformanceLibraryPropertyMask
) & PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED
) != 0);