]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Library/DxeSmmPerformanceLib/DxeSmmPerformanceLib.c
Add performance library instances for SMM performance measurement.
[mirror_edk2.git] / MdeModulePkg / Library / DxeSmmPerformanceLib / DxeSmmPerformanceLib.c
CommitLineData
d042c6e8 1/** @file\r
2 Performance library instance used in DXE phase to dump SMM performance data.\r
3\r
4 This library instance allows a DXE driver or UEFI application to dump the SMM performance data.\r
5 StartPerformanceMeasurement() and EndPerformanceMeasurement() are not implemented.\r
6\r
7 Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>\r
8This program and the accompanying materials\r
9are licensed and made available under the terms and conditions of the BSD License\r
10which accompanies this distribution. The full text of the license may be found at\r
11http://opensource.org/licenses/bsd-license.php\r
12\r
13THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
14WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
15\r
16**/\r
17\r
18\r
19#include <PiDxe.h>\r
20\r
21#include <Guid/Performance.h>\r
22\r
23#include <Library/PerformanceLib.h>\r
24#include <Library/DebugLib.h>\r
25#include <Library/UefiBootServicesTableLib.h>\r
26#include <Library/UefiRuntimeServicesTableLib.h>\r
27#include <Library/PcdLib.h>\r
28#include <Library/BaseMemoryLib.h>\r
29#include <Library/BaseLib.h>\r
30#include <Library/MemoryAllocationLib.h>\r
31\r
32#include <Protocol/SmmCommunication.h>\r
33\r
34#define SMM_PERFORMANCE_COMMUNICATION_BUFFER_SIZE (OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data) + sizeof (SMM_PERF_COMMUNICATE))\r
35//\r
36// The cached performance protocol interface.\r
37//\r
38EFI_SMM_COMMUNICATION_PROTOCOL *mSmmCommunication = NULL;\r
39UINT8 mSmmPerformanceBuffer[SMM_PERFORMANCE_COMMUNICATION_BUFFER_SIZE];\r
40GAUGE_DATA_ENTRY *mGaugeData = NULL;\r
41UINTN mGaugeNumberOfEntries = 0;\r
42\r
43\r
44/**\r
45 The constructor function caches the pointer to SMM Communication protocol.\r
46\r
47 The constructor function locates Performance protocol from protocol database.\r
48 It will ASSERT() if that operation fails and it will always return EFI_SUCCESS.\r
49\r
50 @retval EFI_SUCCESS Performance protocol is successfully located.\r
51 @retval Other Performance protocol is not located to log performance.\r
52\r
53**/\r
54EFI_STATUS\r
55GetCommunicationProtocol (\r
56 VOID\r
57 )\r
58{\r
59 EFI_STATUS Status;\r
60 EFI_SMM_COMMUNICATION_PROTOCOL *Communication;\r
61\r
62 if (mSmmCommunication != NULL) {\r
63 return EFI_SUCCESS;\r
64 }\r
65\r
66 Status = gBS->LocateProtocol (&gEfiSmmCommunicationProtocolGuid, NULL, (VOID **) &Communication);\r
67 if (!EFI_ERROR (Status)) {\r
68 ASSERT (Communication != NULL);\r
69 //\r
70 // Cache SMM Communication protocol.\r
71 //\r
72 mSmmCommunication = Communication;\r
73 }\r
74\r
75 return Status;\r
76}\r
77\r
78/**\r
79 Creates a record for the beginning of a performance measurement.\r
80\r
81 Creates a record that contains the Handle, Token, and Module.\r
82 If TimeStamp is not zero, then TimeStamp is added to the record as the start time.\r
83 If TimeStamp is zero, then this function reads the current time stamp\r
84 and adds that time stamp value to the record as the start time.\r
85\r
86 @param Handle Pointer to environment specific context used\r
87 to identify the component being measured.\r
88 @param Token Pointer to a Null-terminated ASCII string\r
89 that identifies the component being measured.\r
90 @param Module Pointer to a Null-terminated ASCII string\r
91 that identifies the module being measured.\r
92 @param TimeStamp 64-bit time stamp.\r
93\r
94 @retval RETURN_SUCCESS The start of the measurement was recorded.\r
95 @retval RETURN_OUT_OF_RESOURCES There are not enough resources to record the measurement.\r
96\r
97**/\r
98RETURN_STATUS\r
99EFIAPI\r
100StartPerformanceMeasurement (\r
101 IN CONST VOID *Handle, OPTIONAL\r
102 IN CONST CHAR8 *Token, OPTIONAL\r
103 IN CONST CHAR8 *Module, OPTIONAL\r
104 IN UINT64 TimeStamp\r
105 )\r
106{\r
107 return EFI_UNSUPPORTED;\r
108}\r
109\r
110/**\r
111 Fills in the end time of a performance measurement.\r
112\r
113 Looks up the record that matches Handle, Token, and Module.\r
114 If the record can not be found then return RETURN_NOT_FOUND.\r
115 If the record is found and TimeStamp is not zero,\r
116 then TimeStamp is added to the record as the end time.\r
117 If the record is found and TimeStamp is zero, then this function reads\r
118 the current time stamp and adds that time stamp value to the record as the end time.\r
119 If this function is called multiple times for the same record, then the end time is overwritten.\r
120\r
121 @param Handle Pointer to environment specific context used\r
122 to identify the component being measured.\r
123 @param Token Pointer to a Null-terminated ASCII string\r
124 that identifies the component being measured.\r
125 @param Module Pointer to a Null-terminated ASCII string\r
126 that identifies the module being measured.\r
127 @param TimeStamp 64-bit time stamp.\r
128\r
129 @retval RETURN_SUCCESS The end of the measurement was recorded.\r
130 @retval RETURN_NOT_FOUND The specified measurement record could not be found.\r
131\r
132**/\r
133RETURN_STATUS\r
134EFIAPI\r
135EndPerformanceMeasurement (\r
136 IN CONST VOID *Handle, OPTIONAL\r
137 IN CONST CHAR8 *Token, OPTIONAL\r
138 IN CONST CHAR8 *Module, OPTIONAL\r
139 IN UINT64 TimeStamp\r
140 )\r
141{\r
142 return EFI_UNSUPPORTED;\r
143}\r
144\r
145/**\r
146 Retrieves all previous logged performance measurement.\r
147 Function will use SMM communicate protocol to get all previous SMM performance measurement data.\r
148 If success, data buffer will be returned. If fail function will return NULL.\r
149\r
150 @retval !NULL Get all gauge data success.\r
151 @retval NULL Get all guage data failed.\r
152**/\r
153GAUGE_DATA_ENTRY*\r
154EFIAPI\r
155GetAllSmmGaugeData (VOID)\r
156{\r
157 EFI_STATUS Status;\r
158 EFI_SMM_COMMUNICATE_HEADER *SmmCommBufferHeader;\r
159 SMM_PERF_COMMUNICATE *SmmPerfCommData;\r
160 UINTN CommSize;\r
161 UINTN DataSize;\r
162\r
163 if (mGaugeData != NULL) {\r
164 return mGaugeData;\r
165 }\r
166\r
167 Status = GetCommunicationProtocol ();\r
168 if (EFI_ERROR (Status)) {\r
169 return NULL;\r
170 }\r
171\r
172 //\r
173 // Initialize communicate buffer \r
174 //\r
175 SmmCommBufferHeader = (EFI_SMM_COMMUNICATE_HEADER*)mSmmPerformanceBuffer;\r
176 SmmPerfCommData = (SMM_PERF_COMMUNICATE*)SmmCommBufferHeader->Data;\r
177 ZeroMem((UINT8*)SmmPerfCommData, sizeof(SMM_PERF_COMMUNICATE));\r
178 \r
179 CopyGuid (&SmmCommBufferHeader->HeaderGuid, &gSmmPerformanceProtocolGuid);\r
180 SmmCommBufferHeader->MessageLength = sizeof(SMM_PERF_COMMUNICATE);\r
181 CommSize = OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data) + sizeof(SMM_PERF_COMMUNICATE);\r
182\r
183 //\r
184 // Get totol number of SMM gauge entries\r
185 //\r
186 SmmPerfCommData->Function = SMM_PERF_FUNCTION_GET_GAUGE_ENTRY_NUMBER;\r
187 Status = mSmmCommunication->Communicate (mSmmCommunication, mSmmPerformanceBuffer, &CommSize);\r
188 ASSERT_EFI_ERROR (Status);\r
189\r
190 if (EFI_ERROR (SmmPerfCommData->ReturnStatus) || SmmPerfCommData->NumberOfEntries == 0) {\r
191 return NULL;\r
192 }\r
193\r
194 mGaugeNumberOfEntries = SmmPerfCommData->NumberOfEntries;\r
195 \r
196 DataSize = mGaugeNumberOfEntries * sizeof(GAUGE_DATA_ENTRY);\r
197 mGaugeData = AllocateZeroPool(DataSize);\r
198 ASSERT_EFI_ERROR (Status);\r
199 \r
200 //\r
201 // Get all SMM gauge data\r
202 // \r
203 SmmPerfCommData->Function = SMM_PERF_FUNCTION_GET_GAUGE_DATA;\r
204 SmmPerfCommData->LogEntryKey = 0;\r
205 SmmPerfCommData->NumberOfEntries = mGaugeNumberOfEntries;\r
206 SmmPerfCommData->GaugeData = mGaugeData;\r
207 Status = mSmmCommunication->Communicate (mSmmCommunication, mSmmPerformanceBuffer, &CommSize);\r
208 ASSERT_EFI_ERROR (Status);\r
209 ASSERT_EFI_ERROR(SmmPerfCommData->ReturnStatus);\r
210\r
211 return mGaugeData; \r
212}\r
213\r
214/**\r
215 Retrieves a previously logged performance measurement.\r
216\r
217 Retrieves the performance log entry from the performance log specified by LogEntryKey.\r
218 If it stands for a valid entry, then EFI_SUCCESS is returned and\r
219 GaugeDataEntry stores the pointer to that entry.\r
220\r
221 @param LogEntryKey The key for the previous performance measurement log entry.\r
222 If 0, then the first performance measurement log entry is retrieved.\r
223 @param GaugeDataEntry The indirect pointer to the gauge data entry specified by LogEntryKey\r
224 if the retrieval is successful.\r
225\r
226 @retval EFI_SUCCESS The GuageDataEntry is successfully found based on LogEntryKey.\r
227 @retval EFI_NOT_FOUND The LogEntryKey is the last entry (equals to the total entry number).\r
228 @retval EFI_INVALIDE_PARAMETER The LogEntryKey is not a valid entry (greater than the total entry number).\r
229 @retval EFI_INVALIDE_PARAMETER GaugeDataEntry is NULL.\r
230\r
231**/\r
232EFI_STATUS\r
233EFIAPI\r
234GetGauge (\r
235 IN UINTN LogEntryKey,\r
236 OUT GAUGE_DATA_ENTRY **GaugeDataEntry\r
237 )\r
238{\r
239 if (LogEntryKey > mGaugeNumberOfEntries) {\r
240 return EFI_INVALID_PARAMETER;\r
241 }\r
242 if (LogEntryKey == mGaugeNumberOfEntries) {\r
243 return EFI_NOT_FOUND;\r
244 }\r
245\r
246 if (GaugeDataEntry == NULL) {\r
247 return EFI_INVALID_PARAMETER;\r
248 }\r
249 *GaugeDataEntry = &mGaugeData[LogEntryKey];\r
250\r
251 return EFI_SUCCESS;\r
252}\r
253\r
254/**\r
255 Attempts to retrieve a performance measurement log entry from the performance measurement log.\r
256\r
257 Attempts to retrieve the performance log entry specified by LogEntryKey. If LogEntryKey is\r
258 zero on entry, then an attempt is made to retrieve the first entry from the performance log,\r
259 and the key for the second entry in the log is returned. If the performance log is empty,\r
260 then no entry is retrieved and zero is returned. If LogEntryKey is not zero, then the performance\r
261 log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is\r
262 returned. If LogEntryKey is the key for the last entry in the log, then the last log entry is\r
263 retrieved and an implementation specific non-zero key value that specifies the end of the performance\r
264 log is returned. If LogEntryKey is equal this implementation specific non-zero key value, then no entry\r
265 is retrieved and zero is returned. In the cases where a performance log entry can be returned,\r
266 the log entry is returned in Handle, Token, Module, StartTimeStamp, and EndTimeStamp.\r
267 If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().\r
268 If Handle is NULL, then ASSERT().\r
269 If Token is NULL, then ASSERT().\r
270 If Module is NULL, then ASSERT().\r
271 If StartTimeStamp is NULL, then ASSERT().\r
272 If EndTimeStamp is NULL, then ASSERT().\r
273\r
274 @param LogEntryKey On entry, the key of the performance measurement log entry to retrieve.\r
275 0, then the first performance measurement log entry is retrieved.\r
276 On exit, the key of the next performance log entry.\r
277 @param Handle Pointer to environment specific context used to identify the component\r
278 being measured.\r
279 @param Token Pointer to a Null-terminated ASCII string that identifies the component\r
280 being measured.\r
281 @param Module Pointer to a Null-terminated ASCII string that identifies the module\r
282 being measured.\r
283 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement\r
284 was started.\r
285 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement\r
286 was ended.\r
287\r
288 @return The key for the next performance log entry (in general case).\r
289\r
290**/\r
291UINTN\r
292EFIAPI\r
293GetPerformanceMeasurement (\r
294 IN UINTN LogEntryKey,\r
295 OUT CONST VOID **Handle,\r
296 OUT CONST CHAR8 **Token,\r
297 OUT CONST CHAR8 **Module,\r
298 OUT UINT64 *StartTimeStamp,\r
299 OUT UINT64 *EndTimeStamp\r
300 )\r
301{\r
302 EFI_STATUS Status;\r
303 GAUGE_DATA_ENTRY *GaugeData;\r
304\r
305 GaugeData = NULL;\r
306\r
307 ASSERT (Handle != NULL);\r
308 ASSERT (Token != NULL);\r
309 ASSERT (Module != NULL);\r
310 ASSERT (StartTimeStamp != NULL);\r
311 ASSERT (EndTimeStamp != NULL);\r
312\r
313 mGaugeData = GetAllSmmGaugeData();\r
314 if (mGaugeData == NULL) {\r
315 return 0;\r
316 }\r
317\r
318 Status = GetGauge (LogEntryKey++, &GaugeData);\r
319\r
320 //\r
321 // Make sure that LogEntryKey is a valid log entry key,\r
322 //\r
323 ASSERT (Status != EFI_INVALID_PARAMETER);\r
324\r
325 if (EFI_ERROR (Status)) {\r
326 //\r
327 // The LogEntryKey is the last entry (equals to the total entry number).\r
328 //\r
329 return 0;\r
330\r
331 }\r
332\r
333 ASSERT (GaugeData != NULL);\r
334\r
335 *Handle = (VOID *) (UINTN) GaugeData->Handle;\r
336 *Token = GaugeData->Token;\r
337 *Module = GaugeData->Module;\r
338 *StartTimeStamp = GaugeData->StartTimeStamp;\r
339 *EndTimeStamp = GaugeData->EndTimeStamp;\r
340\r
341 return LogEntryKey;\r
342}\r
343\r
344/**\r
345 Returns TRUE if the performance measurement macros are enabled.\r
346\r
347 This function returns TRUE if the PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of\r
348 PcdPerformanceLibraryPropertyMask is set. Otherwise FALSE is returned.\r
349\r
350 @retval TRUE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of\r
351 PcdPerformanceLibraryPropertyMask is set.\r
352 @retval FALSE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of\r
353 PcdPerformanceLibraryPropertyMask is clear.\r
354\r
355**/\r
356BOOLEAN\r
357EFIAPI\r
358PerformanceMeasurementEnabled (\r
359 VOID\r
360 )\r
361{\r
362 return (BOOLEAN) ((PcdGet8(PcdPerformanceLibraryPropertyMask) & PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED) != 0);\r
363}\r