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(), EndPerformanceMeasurement(), StartPerformanceMeasurementEx()
6 and EndPerformanceMeasurementEx() are not implemented.
8 Copyright (c) 2011 - 2012, Intel Corporation. All rights reserved.<BR>
9 This program and the accompanying materials
10 are licensed and made available under the terms and conditions of the BSD License
11 which accompanies this distribution. The full text of the license may be found at
12 http://opensource.org/licenses/bsd-license.php
14 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
15 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
22 #include <Guid/Performance.h>
24 #include <Library/PerformanceLib.h>
25 #include <Library/DebugLib.h>
26 #include <Library/UefiBootServicesTableLib.h>
27 #include <Library/UefiRuntimeServicesTableLib.h>
28 #include <Library/PcdLib.h>
29 #include <Library/BaseMemoryLib.h>
30 #include <Library/BaseLib.h>
31 #include <Library/MemoryAllocationLib.h>
33 #include <Protocol/SmmCommunication.h>
35 #define SMM_PERFORMANCE_COMMUNICATION_BUFFER_SIZE (OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data) + sizeof (SMM_PERF_COMMUNICATE))
37 // The cached performance protocol interface.
39 EFI_SMM_COMMUNICATION_PROTOCOL
*mSmmCommunication
= NULL
;
40 UINT8 mSmmPerformanceBuffer
[SMM_PERFORMANCE_COMMUNICATION_BUFFER_SIZE
];
41 GAUGE_DATA_ENTRY
*mGaugeData
= NULL
;
42 UINTN mGaugeNumberOfEntries
= 0;
43 GAUGE_DATA_ENTRY_EX
*mGaugeDataEx
= NULL
;
44 UINTN mGaugeNumberOfEntriesEx
= 0;
47 The function caches the pointer to SMM Communication protocol.
49 The function locates SMM Communication protocol from protocol database.
51 @retval EFI_SUCCESS SMM Communication protocol is successfully located.
52 @retval Other SMM Communication protocol is not located to log performance.
56 GetCommunicationProtocol (
61 EFI_SMM_COMMUNICATION_PROTOCOL
*Communication
;
63 if (mSmmCommunication
!= NULL
) {
67 Status
= gBS
->LocateProtocol (&gEfiSmmCommunicationProtocolGuid
, NULL
, (VOID
**) &Communication
);
68 if (!EFI_ERROR (Status
)) {
69 ASSERT (Communication
!= NULL
);
71 // Cache SMM Communication protocol.
73 mSmmCommunication
= Communication
;
80 Creates a record for the beginning of a performance measurement.
82 Creates a record that contains the Handle, Token, Module and Identifier.
83 If TimeStamp is not zero, then TimeStamp is added to the record as the start time.
84 If TimeStamp is zero, then this function reads the current time stamp
85 and adds that time stamp value to the record as the start time.
87 @param Handle Pointer to environment specific context used
88 to identify the component being measured.
89 @param Token Pointer to a Null-terminated ASCII string
90 that identifies the component being measured.
91 @param Module Pointer to a Null-terminated ASCII string
92 that identifies the module being measured.
93 @param TimeStamp 64-bit time stamp.
94 @param Identifier 32-bit identifier. If the value is 0, the created record
95 is same as the one created by StartPerformanceMeasurement.
97 @retval RETURN_SUCCESS The start of the measurement was recorded.
98 @retval RETURN_OUT_OF_RESOURCES There are not enough resources to record the measurement.
103 StartPerformanceMeasurementEx (
104 IN CONST VOID
*Handle
, OPTIONAL
105 IN CONST CHAR8
*Token
, OPTIONAL
106 IN CONST CHAR8
*Module
, OPTIONAL
111 return RETURN_SUCCESS
;
115 Fills in the end time of a performance measurement.
117 Looks up the record that matches Handle, Token, Module and Identifier.
118 If the record can not be found then return RETURN_NOT_FOUND.
119 If the record is found and TimeStamp is not zero,
120 then TimeStamp is added to the record as the end time.
121 If the record is found and TimeStamp is zero, then this function reads
122 the current time stamp and adds that time stamp value to the record as the end time.
124 @param Handle Pointer to environment specific context used
125 to identify the component being measured.
126 @param Token Pointer to a Null-terminated ASCII string
127 that identifies the component being measured.
128 @param Module Pointer to a Null-terminated ASCII string
129 that identifies the module being measured.
130 @param TimeStamp 64-bit time stamp.
131 @param Identifier 32-bit identifier. If the value is 0, the found record
132 is same as the one found by EndPerformanceMeasurement.
134 @retval RETURN_SUCCESS The end of the measurement was recorded.
135 @retval RETURN_NOT_FOUND The specified measurement record could not be found.
140 EndPerformanceMeasurementEx (
141 IN CONST VOID
*Handle
, OPTIONAL
142 IN CONST CHAR8
*Token
, OPTIONAL
143 IN CONST CHAR8
*Module
, OPTIONAL
148 return RETURN_SUCCESS
;
152 Creates a record for the beginning of a performance measurement.
154 Creates a record that contains the Handle, Token, and Module.
155 If TimeStamp is not zero, then TimeStamp is added to the record as the start time.
156 If TimeStamp is zero, then this function reads the current time stamp
157 and adds that time stamp value to the record as the start time.
159 @param Handle Pointer to environment specific context used
160 to identify the component being measured.
161 @param Token Pointer to a Null-terminated ASCII string
162 that identifies the component being measured.
163 @param Module Pointer to a Null-terminated ASCII string
164 that identifies the module being measured.
165 @param TimeStamp 64-bit time stamp.
167 @retval RETURN_SUCCESS The start of the measurement was recorded.
168 @retval RETURN_OUT_OF_RESOURCES There are not enough resources to record the measurement.
173 StartPerformanceMeasurement (
174 IN CONST VOID
*Handle
, OPTIONAL
175 IN CONST CHAR8
*Token
, OPTIONAL
176 IN CONST CHAR8
*Module
, OPTIONAL
180 return RETURN_SUCCESS
;
184 Fills in the end time of a performance measurement.
186 Looks up the record that matches Handle, Token, and Module.
187 If the record can not be found then return RETURN_NOT_FOUND.
188 If the record is found and TimeStamp is not zero,
189 then TimeStamp is added to the record as the end time.
190 If the record is found and TimeStamp is zero, then this function reads
191 the current time stamp and adds that time stamp value to the record as the end time.
193 @param Handle Pointer to environment specific context used
194 to identify the component being measured.
195 @param Token Pointer to a Null-terminated ASCII string
196 that identifies the component being measured.
197 @param Module Pointer to a Null-terminated ASCII string
198 that identifies the module being measured.
199 @param TimeStamp 64-bit time stamp.
201 @retval RETURN_SUCCESS The end of the measurement was recorded.
202 @retval RETURN_NOT_FOUND The specified measurement record could not be found.
207 EndPerformanceMeasurement (
208 IN CONST VOID
*Handle
, OPTIONAL
209 IN CONST CHAR8
*Token
, OPTIONAL
210 IN CONST CHAR8
*Module
, OPTIONAL
214 return RETURN_SUCCESS
;
218 Retrieves all previous logged performance measurement.
219 Function will use SMM communicate protocol to get all previous SMM performance measurement data.
220 If success, data buffer will be returned. If fail function will return NULL.
222 @retval !NULL Get all gauge data success.
223 @retval NULL Get all guage data failed.
227 GetAllSmmGaugeData (VOID
)
230 EFI_SMM_COMMUNICATE_HEADER
*SmmCommBufferHeader
;
231 SMM_PERF_COMMUNICATE
*SmmPerfCommData
;
235 if (mGaugeData
!= NULL
) {
239 Status
= GetCommunicationProtocol ();
240 if (EFI_ERROR (Status
)) {
245 // Initialize communicate buffer
247 SmmCommBufferHeader
= (EFI_SMM_COMMUNICATE_HEADER
*)mSmmPerformanceBuffer
;
248 SmmPerfCommData
= (SMM_PERF_COMMUNICATE
*)SmmCommBufferHeader
->Data
;
249 ZeroMem((UINT8
*)SmmPerfCommData
, sizeof(SMM_PERF_COMMUNICATE
));
251 CopyGuid (&SmmCommBufferHeader
->HeaderGuid
, &gSmmPerformanceProtocolGuid
);
252 SmmCommBufferHeader
->MessageLength
= sizeof(SMM_PERF_COMMUNICATE
);
253 CommSize
= SMM_PERFORMANCE_COMMUNICATION_BUFFER_SIZE
;
256 // Get totol number of SMM gauge entries
258 SmmPerfCommData
->Function
= SMM_PERF_FUNCTION_GET_GAUGE_ENTRY_NUMBER
;
259 Status
= mSmmCommunication
->Communicate (mSmmCommunication
, mSmmPerformanceBuffer
, &CommSize
);
260 if (EFI_ERROR (Status
) || EFI_ERROR (SmmPerfCommData
->ReturnStatus
) || SmmPerfCommData
->NumberOfEntries
== 0) {
264 mGaugeNumberOfEntries
= SmmPerfCommData
->NumberOfEntries
;
266 DataSize
= mGaugeNumberOfEntries
* sizeof(GAUGE_DATA_ENTRY
);
267 mGaugeData
= AllocateZeroPool(DataSize
);
268 ASSERT (mGaugeData
!= NULL
);
271 // Get all SMM gauge data
273 SmmPerfCommData
->Function
= SMM_PERF_FUNCTION_GET_GAUGE_DATA
;
274 SmmPerfCommData
->LogEntryKey
= 0;
275 SmmPerfCommData
->NumberOfEntries
= mGaugeNumberOfEntries
;
276 SmmPerfCommData
->GaugeData
= mGaugeData
;
277 Status
= mSmmCommunication
->Communicate (mSmmCommunication
, mSmmPerformanceBuffer
, &CommSize
);
278 if (EFI_ERROR (Status
) || EFI_ERROR (SmmPerfCommData
->ReturnStatus
)) {
279 FreePool (mGaugeData
);
281 mGaugeNumberOfEntries
= 0;
288 Retrieves all previous logged performance measurement.
289 Function will use SMM communicate protocol to get all previous SMM performance measurement data.
290 If success, data buffer will be returned. If fail function will return NULL.
292 @retval !NULL Get all gauge data success.
293 @retval NULL Get all guage data failed.
295 GAUGE_DATA_ENTRY_EX
*
297 GetAllSmmGaugeDataEx (VOID
)
300 EFI_SMM_COMMUNICATE_HEADER
*SmmCommBufferHeader
;
301 SMM_PERF_COMMUNICATE_EX
*SmmPerfCommData
;
305 if (mGaugeDataEx
!= NULL
) {
309 Status
= GetCommunicationProtocol ();
310 if (EFI_ERROR (Status
)) {
315 // Initialize communicate buffer
317 SmmCommBufferHeader
= (EFI_SMM_COMMUNICATE_HEADER
*)mSmmPerformanceBuffer
;
318 SmmPerfCommData
= (SMM_PERF_COMMUNICATE_EX
*)SmmCommBufferHeader
->Data
;
319 ZeroMem((UINT8
*)SmmPerfCommData
, sizeof(SMM_PERF_COMMUNICATE_EX
));
321 CopyGuid (&SmmCommBufferHeader
->HeaderGuid
, &gSmmPerformanceExProtocolGuid
);
322 SmmCommBufferHeader
->MessageLength
= sizeof(SMM_PERF_COMMUNICATE_EX
);
323 CommSize
= SMM_PERFORMANCE_COMMUNICATION_BUFFER_SIZE
;
326 // Get totol number of SMM gauge entries
328 SmmPerfCommData
->Function
= SMM_PERF_FUNCTION_GET_GAUGE_ENTRY_NUMBER
;
329 Status
= mSmmCommunication
->Communicate (mSmmCommunication
, mSmmPerformanceBuffer
, &CommSize
);
330 if (EFI_ERROR (Status
) || EFI_ERROR (SmmPerfCommData
->ReturnStatus
) || SmmPerfCommData
->NumberOfEntries
== 0) {
334 mGaugeNumberOfEntriesEx
= SmmPerfCommData
->NumberOfEntries
;
336 DataSize
= mGaugeNumberOfEntriesEx
* sizeof(GAUGE_DATA_ENTRY_EX
);
337 mGaugeDataEx
= AllocateZeroPool(DataSize
);
338 ASSERT (mGaugeDataEx
!= NULL
);
341 // Get all SMM gauge data
343 SmmPerfCommData
->Function
= SMM_PERF_FUNCTION_GET_GAUGE_DATA
;
344 SmmPerfCommData
->LogEntryKey
= 0;
345 SmmPerfCommData
->NumberOfEntries
= mGaugeNumberOfEntriesEx
;
346 SmmPerfCommData
->GaugeDataEx
= mGaugeDataEx
;
347 Status
= mSmmCommunication
->Communicate (mSmmCommunication
, mSmmPerformanceBuffer
, &CommSize
);
348 if (EFI_ERROR (Status
) || EFI_ERROR (SmmPerfCommData
->ReturnStatus
)) {
349 FreePool (mGaugeDataEx
);
351 mGaugeNumberOfEntriesEx
= 0;
358 Attempts to retrieve a performance measurement log entry from the performance measurement log.
359 It can also retrieve the log created by StartPerformanceMeasurement and EndPerformanceMeasurement,
360 and then assign the Identifier with 0.
362 Attempts to retrieve the performance log entry specified by LogEntryKey. If LogEntryKey is
363 zero on entry, then an attempt is made to retrieve the first entry from the performance log,
364 and the key for the second entry in the log is returned. If the performance log is empty,
365 then no entry is retrieved and zero is returned. If LogEntryKey is not zero, then the performance
366 log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is
367 returned. If LogEntryKey is the key for the last entry in the log, then the last log entry is
368 retrieved and an implementation specific non-zero key value that specifies the end of the performance
369 log is returned. If LogEntryKey is equal this implementation specific non-zero key value, then no entry
370 is retrieved and zero is returned. In the cases where a performance log entry can be returned,
371 the log entry is returned in Handle, Token, Module, StartTimeStamp, EndTimeStamp and Identifier.
372 If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().
373 If Handle is NULL, then ASSERT().
374 If Token is NULL, then ASSERT().
375 If Module is NULL, then ASSERT().
376 If StartTimeStamp is NULL, then ASSERT().
377 If EndTimeStamp is NULL, then ASSERT().
378 If Identifier is NULL, then ASSERT().
380 @param LogEntryKey On entry, the key of the performance measurement log entry to retrieve.
381 0, then the first performance measurement log entry is retrieved.
382 On exit, the key of the next performance log entry.
383 @param Handle Pointer to environment specific context used to identify the component
385 @param Token Pointer to a Null-terminated ASCII string that identifies the component
387 @param Module Pointer to a Null-terminated ASCII string that identifies the module
389 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
391 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
393 @param Identifier Pointer to the 32-bit identifier that was recorded.
395 @return The key for the next performance log entry (in general case).
400 GetPerformanceMeasurementEx (
401 IN UINTN LogEntryKey
,
402 OUT CONST VOID
**Handle
,
403 OUT CONST CHAR8
**Token
,
404 OUT CONST CHAR8
**Module
,
405 OUT UINT64
*StartTimeStamp
,
406 OUT UINT64
*EndTimeStamp
,
407 OUT UINT32
*Identifier
410 GAUGE_DATA_ENTRY_EX
*GaugeData
;
414 ASSERT (Handle
!= NULL
);
415 ASSERT (Token
!= NULL
);
416 ASSERT (Module
!= NULL
);
417 ASSERT (StartTimeStamp
!= NULL
);
418 ASSERT (EndTimeStamp
!= NULL
);
419 ASSERT (Identifier
!= NULL
);
421 mGaugeDataEx
= GetAllSmmGaugeDataEx();
422 if (mGaugeDataEx
!= NULL
) {
424 // Make sure that LogEntryKey is a valid log entry key.
426 ASSERT (LogEntryKey
<= mGaugeNumberOfEntriesEx
);
428 if (LogEntryKey
== mGaugeNumberOfEntriesEx
) {
432 GaugeData
= &mGaugeDataEx
[LogEntryKey
++];
433 *Identifier
= GaugeData
->Identifier
;
435 mGaugeData
= GetAllSmmGaugeData();
436 if (mGaugeData
!= NULL
) {
438 // Make sure that LogEntryKey is a valid log entry key.
440 ASSERT (LogEntryKey
<= mGaugeNumberOfEntries
);
442 if (LogEntryKey
== mGaugeNumberOfEntries
) {
446 GaugeData
= (GAUGE_DATA_ENTRY_EX
*) &mGaugeData
[LogEntryKey
++];
453 *Handle
= (VOID
*) (UINTN
) GaugeData
->Handle
;
454 *Token
= GaugeData
->Token
;
455 *Module
= GaugeData
->Module
;
456 *StartTimeStamp
= GaugeData
->StartTimeStamp
;
457 *EndTimeStamp
= GaugeData
->EndTimeStamp
;
463 Attempts to retrieve a performance measurement log entry from the performance measurement log.
464 It can also retrieve the log created by StartPerformanceMeasurementEx and EndPerformanceMeasurementEx,
465 and then eliminate the Identifier.
467 Attempts to retrieve the performance log entry specified by LogEntryKey. If LogEntryKey is
468 zero on entry, then an attempt is made to retrieve the first entry from the performance log,
469 and the key for the second entry in the log is returned. If the performance log is empty,
470 then no entry is retrieved and zero is returned. If LogEntryKey is not zero, then the performance
471 log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is
472 returned. If LogEntryKey is the key for the last entry in the log, then the last log entry is
473 retrieved and an implementation specific non-zero key value that specifies the end of the performance
474 log is returned. If LogEntryKey is equal this implementation specific non-zero key value, then no entry
475 is retrieved and zero is returned. In the cases where a performance log entry can be returned,
476 the log entry is returned in Handle, Token, Module, StartTimeStamp, and EndTimeStamp.
477 If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().
478 If Handle is NULL, then ASSERT().
479 If Token is NULL, then ASSERT().
480 If Module is NULL, then ASSERT().
481 If StartTimeStamp is NULL, then ASSERT().
482 If EndTimeStamp is NULL, then ASSERT().
484 @param LogEntryKey On entry, the key of the performance measurement log entry to retrieve.
485 0, then the first performance measurement log entry is retrieved.
486 On exit, the key of the next performance log entry.
487 @param Handle Pointer to environment specific context used to identify the component
489 @param Token Pointer to a Null-terminated ASCII string that identifies the component
491 @param Module Pointer to a Null-terminated ASCII string that identifies the module
493 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
495 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
498 @return The key for the next performance log entry (in general case).
503 GetPerformanceMeasurement (
504 IN UINTN LogEntryKey
,
505 OUT CONST VOID
**Handle
,
506 OUT CONST CHAR8
**Token
,
507 OUT CONST CHAR8
**Module
,
508 OUT UINT64
*StartTimeStamp
,
509 OUT UINT64
*EndTimeStamp
513 return GetPerformanceMeasurementEx (LogEntryKey
, Handle
, Token
, Module
, StartTimeStamp
, EndTimeStamp
, &Identifier
);
517 Returns TRUE if the performance measurement macros are enabled.
519 This function returns TRUE if the PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
520 PcdPerformanceLibraryPropertyMask is set. Otherwise FALSE is returned.
522 @retval TRUE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
523 PcdPerformanceLibraryPropertyMask is set.
524 @retval FALSE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
525 PcdPerformanceLibraryPropertyMask is clear.
530 PerformanceMeasurementEnabled (
534 return (BOOLEAN
) ((PcdGet8(PcdPerformanceLibraryPropertyMask
) & PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED
) != 0);