]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Library/DxeSmmPerformanceLib/DxeSmmPerformanceLib.c
Add performance library instances for SMM performance measurement.
[mirror_edk2.git] / MdeModulePkg / Library / DxeSmmPerformanceLib / DxeSmmPerformanceLib.c
1 /** @file
2 Performance library instance used in DXE phase to dump SMM performance data.
3
4 This library instance allows a DXE driver or UEFI application to dump the SMM performance data.
5 StartPerformanceMeasurement() and EndPerformanceMeasurement() are not implemented.
6
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
12
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.
15
16 **/
17
18
19 #include <PiDxe.h>
20
21 #include <Guid/Performance.h>
22
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>
31
32 #include <Protocol/SmmCommunication.h>
33
34 #define SMM_PERFORMANCE_COMMUNICATION_BUFFER_SIZE (OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data) + sizeof (SMM_PERF_COMMUNICATE))
35 //
36 // The cached performance protocol interface.
37 //
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;
42
43
44 /**
45 The constructor function caches the pointer to SMM Communication protocol.
46
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.
49
50 @retval EFI_SUCCESS Performance protocol is successfully located.
51 @retval Other Performance protocol is not located to log performance.
52
53 **/
54 EFI_STATUS
55 GetCommunicationProtocol (
56 VOID
57 )
58 {
59 EFI_STATUS Status;
60 EFI_SMM_COMMUNICATION_PROTOCOL *Communication;
61
62 if (mSmmCommunication != NULL) {
63 return EFI_SUCCESS;
64 }
65
66 Status = gBS->LocateProtocol (&gEfiSmmCommunicationProtocolGuid, NULL, (VOID **) &Communication);
67 if (!EFI_ERROR (Status)) {
68 ASSERT (Communication != NULL);
69 //
70 // Cache SMM Communication protocol.
71 //
72 mSmmCommunication = Communication;
73 }
74
75 return Status;
76 }
77
78 /**
79 Creates a record for the beginning of a performance measurement.
80
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.
85
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.
93
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.
96
97 **/
98 RETURN_STATUS
99 EFIAPI
100 StartPerformanceMeasurement (
101 IN CONST VOID *Handle, OPTIONAL
102 IN CONST CHAR8 *Token, OPTIONAL
103 IN CONST CHAR8 *Module, OPTIONAL
104 IN UINT64 TimeStamp
105 )
106 {
107 return EFI_UNSUPPORTED;
108 }
109
110 /**
111 Fills in the end time of a performance measurement.
112
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.
120
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.
128
129 @retval RETURN_SUCCESS The end of the measurement was recorded.
130 @retval RETURN_NOT_FOUND The specified measurement record could not be found.
131
132 **/
133 RETURN_STATUS
134 EFIAPI
135 EndPerformanceMeasurement (
136 IN CONST VOID *Handle, OPTIONAL
137 IN CONST CHAR8 *Token, OPTIONAL
138 IN CONST CHAR8 *Module, OPTIONAL
139 IN UINT64 TimeStamp
140 )
141 {
142 return EFI_UNSUPPORTED;
143 }
144
145 /**
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.
149
150 @retval !NULL Get all gauge data success.
151 @retval NULL Get all guage data failed.
152 **/
153 GAUGE_DATA_ENTRY*
154 EFIAPI
155 GetAllSmmGaugeData (VOID)
156 {
157 EFI_STATUS Status;
158 EFI_SMM_COMMUNICATE_HEADER *SmmCommBufferHeader;
159 SMM_PERF_COMMUNICATE *SmmPerfCommData;
160 UINTN CommSize;
161 UINTN DataSize;
162
163 if (mGaugeData != NULL) {
164 return mGaugeData;
165 }
166
167 Status = GetCommunicationProtocol ();
168 if (EFI_ERROR (Status)) {
169 return NULL;
170 }
171
172 //
173 // Initialize communicate buffer
174 //
175 SmmCommBufferHeader = (EFI_SMM_COMMUNICATE_HEADER*)mSmmPerformanceBuffer;
176 SmmPerfCommData = (SMM_PERF_COMMUNICATE*)SmmCommBufferHeader->Data;
177 ZeroMem((UINT8*)SmmPerfCommData, sizeof(SMM_PERF_COMMUNICATE));
178
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);
182
183 //
184 // Get totol number of SMM gauge entries
185 //
186 SmmPerfCommData->Function = SMM_PERF_FUNCTION_GET_GAUGE_ENTRY_NUMBER;
187 Status = mSmmCommunication->Communicate (mSmmCommunication, mSmmPerformanceBuffer, &CommSize);
188 ASSERT_EFI_ERROR (Status);
189
190 if (EFI_ERROR (SmmPerfCommData->ReturnStatus) || SmmPerfCommData->NumberOfEntries == 0) {
191 return NULL;
192 }
193
194 mGaugeNumberOfEntries = SmmPerfCommData->NumberOfEntries;
195
196 DataSize = mGaugeNumberOfEntries * sizeof(GAUGE_DATA_ENTRY);
197 mGaugeData = AllocateZeroPool(DataSize);
198 ASSERT_EFI_ERROR (Status);
199
200 //
201 // Get all SMM gauge data
202 //
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);
210
211 return mGaugeData;
212 }
213
214 /**
215 Retrieves a previously logged performance measurement.
216
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.
220
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.
225
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.
230
231 **/
232 EFI_STATUS
233 EFIAPI
234 GetGauge (
235 IN UINTN LogEntryKey,
236 OUT GAUGE_DATA_ENTRY **GaugeDataEntry
237 )
238 {
239 if (LogEntryKey > mGaugeNumberOfEntries) {
240 return EFI_INVALID_PARAMETER;
241 }
242 if (LogEntryKey == mGaugeNumberOfEntries) {
243 return EFI_NOT_FOUND;
244 }
245
246 if (GaugeDataEntry == NULL) {
247 return EFI_INVALID_PARAMETER;
248 }
249 *GaugeDataEntry = &mGaugeData[LogEntryKey];
250
251 return EFI_SUCCESS;
252 }
253
254 /**
255 Attempts to retrieve a performance measurement log entry from the performance measurement log.
256
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().
273
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
278 being measured.
279 @param Token Pointer to a Null-terminated ASCII string that identifies the component
280 being measured.
281 @param Module Pointer to a Null-terminated ASCII string that identifies the module
282 being measured.
283 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
284 was started.
285 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
286 was ended.
287
288 @return The key for the next performance log entry (in general case).
289
290 **/
291 UINTN
292 EFIAPI
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
300 )
301 {
302 EFI_STATUS Status;
303 GAUGE_DATA_ENTRY *GaugeData;
304
305 GaugeData = NULL;
306
307 ASSERT (Handle != NULL);
308 ASSERT (Token != NULL);
309 ASSERT (Module != NULL);
310 ASSERT (StartTimeStamp != NULL);
311 ASSERT (EndTimeStamp != NULL);
312
313 mGaugeData = GetAllSmmGaugeData();
314 if (mGaugeData == NULL) {
315 return 0;
316 }
317
318 Status = GetGauge (LogEntryKey++, &GaugeData);
319
320 //
321 // Make sure that LogEntryKey is a valid log entry key,
322 //
323 ASSERT (Status != EFI_INVALID_PARAMETER);
324
325 if (EFI_ERROR (Status)) {
326 //
327 // The LogEntryKey is the last entry (equals to the total entry number).
328 //
329 return 0;
330
331 }
332
333 ASSERT (GaugeData != NULL);
334
335 *Handle = (VOID *) (UINTN) GaugeData->Handle;
336 *Token = GaugeData->Token;
337 *Module = GaugeData->Module;
338 *StartTimeStamp = GaugeData->StartTimeStamp;
339 *EndTimeStamp = GaugeData->EndTimeStamp;
340
341 return LogEntryKey;
342 }
343
344 /**
345 Returns TRUE if the performance measurement macros are enabled.
346
347 This function returns TRUE if the PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
348 PcdPerformanceLibraryPropertyMask is set. Otherwise FALSE is returned.
349
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.
354
355 **/
356 BOOLEAN
357 EFIAPI
358 PerformanceMeasurementEnabled (
359 VOID
360 )
361 {
362 return (BOOLEAN) ((PcdGet8(PcdPerformanceLibraryPropertyMask) & PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED) != 0);
363 }