]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Library/SmmPerformanceLib/SmmPerformanceLib.c
MdeModulePkg: Change OPTIONAL keyword usage style
[mirror_edk2.git] / MdeModulePkg / Library / SmmPerformanceLib / SmmPerformanceLib.c
index 106155984a3f0b6390a096d88e0b261185602a60..c3fa79b2de926b2589f2aaff2e072a54fa2924bb 100644 (file)
@@ -2,23 +2,17 @@
   Performance Library used in SMM phase.\r
 \r
   This library instance provides infrastructure for SMM drivers to log performance\r
-  data. It consumes SMM Performance Protocol published by SmmCorePerformanceLib\r
-  to log performance data. If Performance Protocol is not available, it does not log any\r
+  data. It consumes SMM PerformanceEx or Performance Protocol published by SmmCorePerformanceLib\r
+  to log performance data. If both SMM PerformanceEx and Performance Protocol are not available, it does not log any\r
   performance information.\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
+  Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved.<BR>\r
+SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
 **/\r
 \r
 \r
-#include <Guid/Performance.h>\r
+#include <Guid/PerformanceMeasurement.h>\r
 \r
 #include <Library/PerformanceLib.h>\r
 #include <Library/DebugLib.h>\r
@@ -26,21 +20,13 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 #include <Library/PcdLib.h>\r
 #include <Library/BaseMemoryLib.h>\r
 \r
-#include <Protocol/SmmCommunication.h>\r
-\r
-//\r
-// The cached performance protocol interface.\r
 //\r
-PERFORMANCE_PROTOCOL  *mPerformance = NULL;\r
-BOOLEAN               mPerformanceMeasurementEnabled;\r
-\r
+// The cached SMM Performance Protocol and SMM PerformanceEx Protocol interface.\r
+EDKII_PERFORMANCE_MEASUREMENT_PROTOCOL  *mPerformanceMeasurement = NULL;\r
+BOOLEAN                                 mPerformanceMeasurementEnabled;\r
 \r
 /**\r
-  The constructor function initializes Performance infrastructure for DXE phase.\r
-\r
-  The constructor function publishes Performance protocol, allocates memory to log DXE performance\r
-  and merges PEI performance data to DXE performance log.\r
-  It will ASSERT() if one of these operations fails and it will always return EFI_SUCCESS.\r
+  The constructor function initializes the Performance Measurement Enable flag\r
 \r
   @param  ImageHandle   The firmware allocated handle for the EFI image.\r
   @param  SystemTable   A pointer to the EFI System Table.\r
@@ -55,50 +41,49 @@ SmmPerformanceLibConstructor (
   IN EFI_SYSTEM_TABLE  *SystemTable\r
   )\r
 {\r
-  \r
+\r
   mPerformanceMeasurementEnabled =  (BOOLEAN) ((PcdGet8(PcdPerformanceLibraryPropertyMask) & PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED) != 0);\r
 \r
   return EFI_SUCCESS;\r
 }\r
 \r
 /**\r
-  The constructor function caches the pointer to Performance protocol.\r
+  The function caches the pointers to SMM PerformanceEx protocol and Performance Protocol.\r
 \r
-  The constructor function locates SMM erformance protocol from the SMM protocol database.\r
-  It will ASSERT() if that operation fails and it will always return EFI_SUCCESS.\r
+  The function locates SMM PerformanceEx protocol and Performance Protocol from protocol database.\r
 \r
-  @retval EFI_SUCCESS     Performance protocol is successfully located.\r
-  @retval Other           Performance protocol is not located to log performance.\r
+  @retval EFI_SUCCESS     SMM PerformanceEx protocol or Performance Protocol is successfully located.\r
+  @retval EFI_NOT_FOUND   Both SMM PerformanceEx protocol and Performance Protocol are not located to log performance.\r
 \r
 **/\r
 EFI_STATUS\r
-GetPerformanceProtocol (\r
+GetPerformanceMeasurementProtocol (\r
   VOID\r
   )\r
 {\r
-  EFI_STATUS            Status;\r
-  PERFORMANCE_PROTOCOL  *Performance;\r
+  EFI_STATUS                Status;\r
+  EDKII_PERFORMANCE_MEASUREMENT_PROTOCOL   *PerformanceMeasurement;\r
 \r
-  if (mPerformance != NULL) {\r
+  if (mPerformanceMeasurement != NULL) {\r
     return EFI_SUCCESS;\r
   }\r
 \r
-  Status = gSmst->SmmLocateProtocol (&gSmmPerformanceProtocolGuid, NULL, (VOID **) &Performance);\r
+  Status = gSmst->SmmLocateProtocol (&gEdkiiSmmPerformanceMeasurementProtocolGuid, NULL, (VOID **) &PerformanceMeasurement);\r
   if (!EFI_ERROR (Status)) {\r
-    ASSERT (Performance != NULL);\r
+    ASSERT (PerformanceMeasurement != NULL);\r
     //\r
-    // Cache performance protocol.\r
+    // Cache PerformanceMeasurement Protocol.\r
     //\r
-    mPerformance = Performance;\r
+    mPerformanceMeasurement = PerformanceMeasurement;\r
+    return EFI_SUCCESS;\r
   }\r
-\r
-  return Status;\r
+  return EFI_NOT_FOUND;\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
+  Creates a record that contains the Handle, Token, Module and Identifier.\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
@@ -110,6 +95,8 @@ GetPerformanceProtocol (
   @param  Module                  Pointer to a Null-terminated ASCII string\r
                                   that identifies the module being measured.\r
   @param  TimeStamp               64-bit time stamp.\r
+  @param  Identifier              32-bit identifier. If the value is 0, the created record\r
+                                  is same as the one created by StartPerformanceMeasurement.\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
@@ -117,21 +104,35 @@ GetPerformanceProtocol (
 **/\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
+StartPerformanceMeasurementEx (\r
+  IN CONST VOID   *Handle   OPTIONAL,\r
+  IN CONST CHAR8  *Token    OPTIONAL,\r
+  IN CONST CHAR8  *Module   OPTIONAL,\r
+  IN UINT64       TimeStamp,\r
+  IN UINT32       Identifier\r
   )\r
 {\r
-  EFI_STATUS  Status;\r
+  EFI_STATUS    Status;\r
+  CONST CHAR8*  String;\r
 \r
-  Status = GetPerformanceProtocol ();\r
+  Status = GetPerformanceMeasurementProtocol ();\r
   if (EFI_ERROR (Status)) {\r
-    return RETURN_OUT_OF_RESOURCES;\r
+    return RETURN_NOT_FOUND;\r
   }\r
 \r
-  Status = mPerformance->StartGauge (Handle, Token, Module, TimeStamp);\r
+  if (Token != NULL) {\r
+    String = Token;\r
+  } else if (Module != NULL) {\r
+    String = Module;\r
+  } else {\r
+    String = NULL;\r
+  }\r
+\r
+  if (mPerformanceMeasurement != NULL) {\r
+    Status = mPerformanceMeasurement->CreatePerformanceMeasurement (Handle, NULL, String, TimeStamp, 0, Identifier, PerfStartEntry);\r
+  } else {\r
+    ASSERT (FALSE);\r
+  }\r
 \r
   return (RETURN_STATUS) Status;\r
 }\r
@@ -139,13 +140,12 @@ StartPerformanceMeasurement (
 /**\r
   Fills in the end time of a performance measurement.\r
 \r
-  Looks up the record that matches Handle, Token, and Module.\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
@@ -154,6 +154,8 @@ StartPerformanceMeasurement (
   @param  Module                  Pointer to a Null-terminated ASCII string\r
                                   that identifies the module being measured.\r
   @param  TimeStamp               64-bit time stamp.\r
+  @param  Identifier              32-bit identifier. If the value is 0, the found record\r
+                                  is same as the one found by EndPerformanceMeasurement.\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
@@ -161,27 +163,43 @@ StartPerformanceMeasurement (
 **/\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
+EndPerformanceMeasurementEx (\r
+  IN CONST VOID   *Handle   OPTIONAL,\r
+  IN CONST CHAR8  *Token    OPTIONAL,\r
+  IN CONST CHAR8  *Module   OPTIONAL,\r
+  IN UINT64       TimeStamp,\r
+  IN UINT32       Identifier\r
   )\r
 {\r
-  EFI_STATUS  Status;\r
+  EFI_STATUS    Status;\r
+  CONST CHAR8*  String;\r
 \r
-  Status = GetPerformanceProtocol ();\r
+  Status = GetPerformanceMeasurementProtocol ();\r
   if (EFI_ERROR (Status)) {\r
     return RETURN_NOT_FOUND;\r
   }\r
 \r
-  Status = mPerformance->EndGauge (Handle, Token, Module, TimeStamp);\r
+  if (Token != NULL) {\r
+    String = Token;\r
+  } else if (Module != NULL) {\r
+    String = Module;\r
+  } else {\r
+    String = NULL;\r
+  }\r
+\r
+  if (mPerformanceMeasurement != NULL) {\r
+    Status = mPerformanceMeasurement->CreatePerformanceMeasurement (Handle, NULL, String, TimeStamp, 0, Identifier, PerfEndEntry);\r
+  } else {\r
+    ASSERT (FALSE);\r
+  }\r
 \r
   return (RETURN_STATUS) Status;\r
 }\r
 \r
 /**\r
   Attempts to retrieve a performance measurement log entry from the performance measurement log.\r
+  It can also retrieve the log created by StartPerformanceMeasurement and EndPerformanceMeasurement,\r
+  and then assign the Identifier with 0.\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
@@ -192,13 +210,14 @@ EndPerformanceMeasurement (
   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
+  the log entry is returned in Handle, Token, Module, StartTimeStamp, EndTimeStamp and Identifier.\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
+  If Identifier 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
@@ -213,60 +232,143 @@ EndPerformanceMeasurement (
                                   was started.\r
   @param  EndTimeStamp            Pointer to the 64-bit time stamp that was recorded when the measurement\r
                                   was ended.\r
+  @param  Identifier              Pointer to the 32-bit identifier that was recorded.\r
 \r
   @return The key for the next performance log entry (in general case).\r
 \r
 **/\r
 UINTN\r
 EFIAPI\r
-GetPerformanceMeasurement (\r
+GetPerformanceMeasurementEx (\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
+  OUT UINT64      *EndTimeStamp,\r
+  OUT UINT32      *Identifier\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
-  Status = GetPerformanceProtocol ();\r
-  if (EFI_ERROR (Status)) {\r
-    return 0;\r
-  }\r
+  return 0;\r
+}\r
 \r
-  Status = mPerformance->GetGauge (LogEntryKey++, &GaugeData);\r
+/**\r
+  Creates a record for the beginning of a performance measurement.\r
 \r
-  //\r
-  // Make sure that LogEntryKey is a valid log entry key,\r
-  //\r
-  ASSERT (Status != EFI_INVALID_PARAMETER);\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
-  if (EFI_ERROR (Status)) {\r
-    //\r
-    // The LogEntryKey is the last entry (equals to the total entry number).\r
-    //\r
-    return 0;\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 StartPerformanceMeasurementEx (Handle, Token, Module, TimeStamp, 0);\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
+\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 EndPerformanceMeasurementEx (Handle, Token, Module, TimeStamp, 0);\r
+}\r
 \r
-  ASSERT (GaugeData != NULL);\r
+/**\r
+  Attempts to retrieve a performance measurement log entry from the performance measurement log.\r
+  It can also retrieve the log created by StartPerformanceMeasurementEx and EndPerformanceMeasurementEx,\r
+  and then eliminate the Identifier.\r
 \r
-  *Handle         = (VOID *) (UINTN) GaugeData->Handle;\r
-  *Token          = GaugeData->Token;\r
-  *Module         = GaugeData->Module;\r
-  *StartTimeStamp = GaugeData->StartTimeStamp;\r
-  *EndTimeStamp   = GaugeData->EndTimeStamp;\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
-  return LogEntryKey;\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
+  return 0;\r
 }\r
 \r
 /**\r
@@ -289,3 +391,71 @@ PerformanceMeasurementEnabled (
 {\r
   return mPerformanceMeasurementEnabled;\r
 }\r
+\r
+/**\r
+  Create performance record with event description and a timestamp.\r
+\r
+  @param CallerIdentifier  - Image handle or pointer to caller ID GUID\r
+  @param Guid              - Pointer to a GUID\r
+  @param String            - Pointer to a string describing the measurement\r
+  @param Address           - Pointer to a location in memory relevant to the measurement\r
+  @param Identifier        - Performance identifier describing the type of measurement\r
+\r
+  @retval RETURN_SUCCESS           - Successfully created performance record\r
+  @retval RETURN_OUT_OF_RESOURCES  - Ran out of space to store the records\r
+  @retval RETURN_INVALID_PARAMETER - Invalid parameter passed to function - NULL\r
+                                     pointer or invalid PerfId\r
+\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+LogPerformanceMeasurement (\r
+  IN CONST VOID   *CallerIdentifier,\r
+  IN CONST VOID   *Guid     OPTIONAL,\r
+  IN CONST CHAR8  *String   OPTIONAL,\r
+  IN UINT64       Address  OPTIONAL,\r
+  IN UINT32       Identifier\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+\r
+  Status = GetPerformanceMeasurementProtocol ();\r
+  if (EFI_ERROR (Status)) {\r
+    return RETURN_OUT_OF_RESOURCES;\r
+  }\r
+\r
+  if (mPerformanceMeasurement != NULL) {\r
+    Status = mPerformanceMeasurement->CreatePerformanceMeasurement (CallerIdentifier, Guid, String, 0, Address, Identifier, PerfEntry);\r
+  } else {\r
+    ASSERT (FALSE);\r
+  }\r
+\r
+  return (RETURN_STATUS) Status;\r
+}\r
+\r
+/**\r
+  Check whether the specified performance measurement can be logged.\r
+\r
+  This function returns TRUE when the PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of PcdPerformanceLibraryPropertyMask is set\r
+  and the Type disable bit in PcdPerformanceLibraryPropertyMask is not set.\r
+\r
+  @param Type        - Type of the performance measurement entry.\r
+\r
+  @retval TRUE         The performance measurement can be logged.\r
+  @retval FALSE        The performance measurement can NOT be logged.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+LogPerformanceMeasurementEnabled (\r
+  IN  CONST UINTN        Type\r
+  )\r
+{\r
+  //\r
+  // When Performance measurement is enabled and the type is not filtered, the performance can be logged.\r
+  //\r
+  if (PerformanceMeasurementEnabled () && (PcdGet8(PcdPerformanceLibraryPropertyMask) & Type) == 0) {\r
+    return TRUE;\r
+  }\r
+  return FALSE;\r
+}\r