]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Library/DxeSmmPerformanceLib/DxeSmmPerformanceLib.c
Add performance library instances for SMM performance measurement.
[mirror_edk2.git] / MdeModulePkg / Library / DxeSmmPerformanceLib / DxeSmmPerformanceLib.c
diff --git a/MdeModulePkg/Library/DxeSmmPerformanceLib/DxeSmmPerformanceLib.c b/MdeModulePkg/Library/DxeSmmPerformanceLib/DxeSmmPerformanceLib.c
new file mode 100644 (file)
index 0000000..3b0a9f6
--- /dev/null
@@ -0,0 +1,363 @@
+/** @file\r
+  Performance library instance used in DXE phase to dump SMM performance data.\r
+\r
+  This library instance allows a DXE driver or UEFI application to dump the SMM performance data.\r
+  StartPerformanceMeasurement() and EndPerformanceMeasurement() are not implemented.\r
+\r
+  Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>\r
+This program and the accompanying materials\r
+are licensed and made available under the terms and conditions of the BSD License\r
+which accompanies this distribution.  The full text of the license may be found at\r
+http://opensource.org/licenses/bsd-license.php\r
+\r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+\r
+**/\r
+\r
+\r
+#include <PiDxe.h>\r
+\r
+#include <Guid/Performance.h>\r
+\r
+#include <Library/PerformanceLib.h>\r
+#include <Library/DebugLib.h>\r
+#include <Library/UefiBootServicesTableLib.h>\r
+#include <Library/UefiRuntimeServicesTableLib.h>\r
+#include <Library/PcdLib.h>\r
+#include <Library/BaseMemoryLib.h>\r
+#include <Library/BaseLib.h>\r
+#include <Library/MemoryAllocationLib.h>\r
+\r
+#include <Protocol/SmmCommunication.h>\r
+\r
+#define SMM_PERFORMANCE_COMMUNICATION_BUFFER_SIZE (OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data)  + sizeof (SMM_PERF_COMMUNICATE))\r
+//\r
+// The cached performance protocol interface.\r
+//\r
+EFI_SMM_COMMUNICATION_PROTOCOL  *mSmmCommunication = NULL;\r
+UINT8                           mSmmPerformanceBuffer[SMM_PERFORMANCE_COMMUNICATION_BUFFER_SIZE];\r
+GAUGE_DATA_ENTRY                *mGaugeData = NULL;\r
+UINTN                           mGaugeNumberOfEntries = 0;\r
+\r
+\r
+/**\r
+  The constructor function caches the pointer to SMM Communication protocol.\r
+\r
+  The constructor function locates Performance protocol from protocol database.\r
+  It will ASSERT() if that operation fails and it will always return EFI_SUCCESS.\r
+\r
+  @retval EFI_SUCCESS     Performance protocol is successfully located.\r
+  @retval Other           Performance protocol is not located to log performance.\r
+\r
+**/\r
+EFI_STATUS\r
+GetCommunicationProtocol (\r
+  VOID\r
+  )\r
+{\r
+  EFI_STATUS                      Status;\r
+  EFI_SMM_COMMUNICATION_PROTOCOL  *Communication;\r
+\r
+  if (mSmmCommunication != NULL) {\r
+    return EFI_SUCCESS;\r
+  }\r
+\r
+  Status = gBS->LocateProtocol (&gEfiSmmCommunicationProtocolGuid, NULL, (VOID **) &Communication);\r
+  if (!EFI_ERROR (Status)) {\r
+    ASSERT (Communication != NULL);\r
+    //\r
+    // Cache SMM Communication protocol.\r
+    //\r
+    mSmmCommunication = Communication;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  Creates a record for the beginning of a performance measurement.\r
+\r
+  Creates a record that contains the Handle, Token, and Module.\r
+  If TimeStamp is not zero, then TimeStamp is added to the record as the start time.\r
+  If TimeStamp is zero, then this function reads the current time stamp\r
+  and adds that time stamp value to the record as the start time.\r
+\r
+  @param  Handle                  Pointer to environment specific context used\r
+                                  to identify the component being measured.\r
+  @param  Token                   Pointer to a Null-terminated ASCII string\r
+                                  that identifies the component being measured.\r
+  @param  Module                  Pointer to a Null-terminated ASCII string\r
+                                  that identifies the module being measured.\r
+  @param  TimeStamp               64-bit time stamp.\r
+\r
+  @retval RETURN_SUCCESS          The start of the measurement was recorded.\r
+  @retval RETURN_OUT_OF_RESOURCES There are not enough resources to record the measurement.\r
+\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+StartPerformanceMeasurement (\r
+  IN CONST VOID   *Handle,  OPTIONAL\r
+  IN CONST CHAR8  *Token,   OPTIONAL\r
+  IN CONST CHAR8  *Module,  OPTIONAL\r
+  IN UINT64       TimeStamp\r
+  )\r
+{\r
+  return EFI_UNSUPPORTED;\r
+}\r
+\r
+/**\r
+  Fills in the end time of a performance measurement.\r
+\r
+  Looks up the record that matches Handle, Token, and Module.\r
+  If the record can not be found then return RETURN_NOT_FOUND.\r
+  If the record is found and TimeStamp is not zero,\r
+  then TimeStamp is added to the record as the end time.\r
+  If the record is found and TimeStamp is zero, then this function reads\r
+  the current time stamp and adds that time stamp value to the record as the end time.\r
+  If this function is called multiple times for the same record, then the end time is overwritten.\r
+\r
+  @param  Handle                  Pointer to environment specific context used\r
+                                  to identify the component being measured.\r
+  @param  Token                   Pointer to a Null-terminated ASCII string\r
+                                  that identifies the component being measured.\r
+  @param  Module                  Pointer to a Null-terminated ASCII string\r
+                                  that identifies the module being measured.\r
+  @param  TimeStamp               64-bit time stamp.\r
+\r
+  @retval RETURN_SUCCESS          The end of  the measurement was recorded.\r
+  @retval RETURN_NOT_FOUND        The specified measurement record could not be found.\r
+\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+EndPerformanceMeasurement (\r
+  IN CONST VOID   *Handle,  OPTIONAL\r
+  IN CONST CHAR8  *Token,   OPTIONAL\r
+  IN CONST CHAR8  *Module,  OPTIONAL\r
+  IN UINT64       TimeStamp\r
+  )\r
+{\r
+  return EFI_UNSUPPORTED;\r
+}\r
+\r
+/**\r
+  Retrieves all previous logged performance measurement.\r
+  Function will use SMM communicate protocol to get all previous SMM performance measurement data.\r
+  If success, data buffer will be returned. If fail function will return NULL.\r
+\r
+  @retval !NULL           Get all gauge data success.\r
+  @retval NULL            Get all guage data failed.\r
+**/\r
+GAUGE_DATA_ENTRY*\r
+EFIAPI\r
+GetAllSmmGaugeData (VOID)\r
+{\r
+  EFI_STATUS                  Status;\r
+  EFI_SMM_COMMUNICATE_HEADER  *SmmCommBufferHeader;\r
+  SMM_PERF_COMMUNICATE        *SmmPerfCommData;\r
+  UINTN                       CommSize;\r
+  UINTN                       DataSize;\r
+\r
+  if (mGaugeData != NULL) {\r
+    return mGaugeData;\r
+  }\r
+\r
+  Status = GetCommunicationProtocol ();\r
+  if (EFI_ERROR (Status)) {\r
+    return NULL;\r
+  }\r
+\r
+  //\r
+  // Initialize communicate buffer \r
+  //\r
+  SmmCommBufferHeader = (EFI_SMM_COMMUNICATE_HEADER*)mSmmPerformanceBuffer;\r
+  SmmPerfCommData = (SMM_PERF_COMMUNICATE*)SmmCommBufferHeader->Data;\r
+  ZeroMem((UINT8*)SmmPerfCommData, sizeof(SMM_PERF_COMMUNICATE));\r
+    \r
+  CopyGuid (&SmmCommBufferHeader->HeaderGuid, &gSmmPerformanceProtocolGuid);\r
+  SmmCommBufferHeader->MessageLength = sizeof(SMM_PERF_COMMUNICATE);\r
+  CommSize = OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data)  + sizeof(SMM_PERF_COMMUNICATE);\r
+\r
+  //\r
+  // Get totol number of SMM gauge entries\r
+  //\r
+  SmmPerfCommData->Function = SMM_PERF_FUNCTION_GET_GAUGE_ENTRY_NUMBER;\r
+  Status = mSmmCommunication->Communicate (mSmmCommunication, mSmmPerformanceBuffer, &CommSize);\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  if (EFI_ERROR (SmmPerfCommData->ReturnStatus) || SmmPerfCommData->NumberOfEntries == 0) {\r
+    return NULL;\r
+  }\r
+\r
+  mGaugeNumberOfEntries = SmmPerfCommData->NumberOfEntries;\r
+  \r
+  DataSize = mGaugeNumberOfEntries * sizeof(GAUGE_DATA_ENTRY);\r
+  mGaugeData = AllocateZeroPool(DataSize);\r
+  ASSERT_EFI_ERROR (Status);\r
+  \r
+  //\r
+  // Get all SMM gauge data\r
+  //  \r
+  SmmPerfCommData->Function = SMM_PERF_FUNCTION_GET_GAUGE_DATA;\r
+  SmmPerfCommData->LogEntryKey = 0;\r
+  SmmPerfCommData->NumberOfEntries = mGaugeNumberOfEntries;\r
+  SmmPerfCommData->GaugeData = mGaugeData;\r
+  Status = mSmmCommunication->Communicate (mSmmCommunication, mSmmPerformanceBuffer, &CommSize);\r
+  ASSERT_EFI_ERROR (Status);\r
+  ASSERT_EFI_ERROR(SmmPerfCommData->ReturnStatus);\r
+\r
+  return mGaugeData; \r
+}\r
+\r
+/**\r
+  Retrieves a previously logged performance measurement.\r
+\r
+  Retrieves the performance log entry from the performance log specified by LogEntryKey.\r
+  If it stands for a valid entry, then EFI_SUCCESS is returned and\r
+  GaugeDataEntry stores the pointer to that entry.\r
+\r
+  @param  LogEntryKey             The key for the previous performance measurement log entry.\r
+                                  If 0, then the first performance measurement log entry is retrieved.\r
+  @param  GaugeDataEntry          The indirect pointer to the gauge data entry specified by LogEntryKey\r
+                                  if the retrieval is successful.\r
+\r
+  @retval EFI_SUCCESS             The GuageDataEntry is successfully found based on LogEntryKey.\r
+  @retval EFI_NOT_FOUND           The LogEntryKey is the last entry (equals to the total entry number).\r
+  @retval EFI_INVALIDE_PARAMETER  The LogEntryKey is not a valid entry (greater than the total entry number).\r
+  @retval EFI_INVALIDE_PARAMETER  GaugeDataEntry is NULL.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+GetGauge (\r
+  IN  UINTN               LogEntryKey,\r
+  OUT GAUGE_DATA_ENTRY    **GaugeDataEntry\r
+  )\r
+{\r
+  if (LogEntryKey > mGaugeNumberOfEntries) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+  if (LogEntryKey == mGaugeNumberOfEntries) {\r
+    return EFI_NOT_FOUND;\r
+  }\r
+\r
+  if (GaugeDataEntry == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+  *GaugeDataEntry = &mGaugeData[LogEntryKey];\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+  Attempts to retrieve a performance measurement log entry from the performance measurement log.\r
+\r
+  Attempts to retrieve the performance log entry specified by LogEntryKey.  If LogEntryKey is\r
+  zero on entry, then an attempt is made to retrieve the first entry from the performance log,\r
+  and the key for the second entry in the log is returned.  If the performance log is empty,\r
+  then no entry is retrieved and zero is returned.  If LogEntryKey is not zero, then the performance\r
+  log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is\r
+  returned.  If LogEntryKey is the key for the last entry in the log, then the last log entry is\r
+  retrieved and an implementation specific non-zero key value that specifies the end of the performance\r
+  log is returned.  If LogEntryKey is equal this implementation specific non-zero key value, then no entry\r
+  is retrieved and zero is returned.  In the cases where a performance log entry can be returned,\r
+  the log entry is returned in Handle, Token, Module, StartTimeStamp, and EndTimeStamp.\r
+  If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().\r
+  If Handle is NULL, then ASSERT().\r
+  If Token is NULL, then ASSERT().\r
+  If Module is NULL, then ASSERT().\r
+  If StartTimeStamp is NULL, then ASSERT().\r
+  If EndTimeStamp is NULL, then ASSERT().\r
+\r
+  @param  LogEntryKey             On entry, the key of the performance measurement log entry to retrieve.\r
+                                  0, then the first performance measurement log entry is retrieved.\r
+                                  On exit, the key of the next performance log entry.\r
+  @param  Handle                  Pointer to environment specific context used to identify the component\r
+                                  being measured.\r
+  @param  Token                   Pointer to a Null-terminated ASCII string that identifies the component\r
+                                  being measured.\r
+  @param  Module                  Pointer to a Null-terminated ASCII string that identifies the module\r
+                                  being measured.\r
+  @param  StartTimeStamp          Pointer to the 64-bit time stamp that was recorded when the measurement\r
+                                  was started.\r
+  @param  EndTimeStamp            Pointer to the 64-bit time stamp that was recorded when the measurement\r
+                                  was ended.\r
+\r
+  @return The key for the next performance log entry (in general case).\r
+\r
+**/\r
+UINTN\r
+EFIAPI\r
+GetPerformanceMeasurement (\r
+  IN  UINTN       LogEntryKey,\r
+  OUT CONST VOID  **Handle,\r
+  OUT CONST CHAR8 **Token,\r
+  OUT CONST CHAR8 **Module,\r
+  OUT UINT64      *StartTimeStamp,\r
+  OUT UINT64      *EndTimeStamp\r
+  )\r
+{\r
+  EFI_STATUS                    Status;\r
+  GAUGE_DATA_ENTRY      *GaugeData;\r
+\r
+  GaugeData = NULL;\r
+\r
+  ASSERT (Handle != NULL);\r
+  ASSERT (Token != NULL);\r
+  ASSERT (Module != NULL);\r
+  ASSERT (StartTimeStamp != NULL);\r
+  ASSERT (EndTimeStamp != NULL);\r
+\r
+  mGaugeData = GetAllSmmGaugeData();\r
+  if (mGaugeData == NULL) {\r
+    return 0;\r
+  }\r
+\r
+  Status = GetGauge (LogEntryKey++, &GaugeData);\r
+\r
+  //\r
+  // Make sure that LogEntryKey is a valid log entry key,\r
+  //\r
+  ASSERT (Status != EFI_INVALID_PARAMETER);\r
+\r
+  if (EFI_ERROR (Status)) {\r
+    //\r
+    // The LogEntryKey is the last entry (equals to the total entry number).\r
+    //\r
+    return 0;\r
+\r
+  }\r
+\r
+  ASSERT (GaugeData != NULL);\r
+\r
+  *Handle         = (VOID *) (UINTN) GaugeData->Handle;\r
+  *Token          = GaugeData->Token;\r
+  *Module         = GaugeData->Module;\r
+  *StartTimeStamp = GaugeData->StartTimeStamp;\r
+  *EndTimeStamp   = GaugeData->EndTimeStamp;\r
+\r
+  return LogEntryKey;\r
+}\r
+\r
+/**\r
+  Returns TRUE if the performance measurement macros are enabled.\r
+\r
+  This function returns TRUE if the PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of\r
+  PcdPerformanceLibraryPropertyMask is set.  Otherwise FALSE is returned.\r
+\r
+  @retval TRUE                    The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of\r
+                                  PcdPerformanceLibraryPropertyMask is set.\r
+  @retval FALSE                   The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of\r
+                                  PcdPerformanceLibraryPropertyMask is clear.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+PerformanceMeasurementEnabled (\r
+  VOID\r
+  )\r
+{\r
+  return (BOOLEAN) ((PcdGet8(PcdPerformanceLibraryPropertyMask) & PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED) != 0);\r
+}\r