]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Library/DxeSmmPerformanceLib/DxeSmmPerformanceLib.c
Update function comments to remove confused description on PerformanceLib EndPerforma...
[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 - 2012, 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
120 @param Handle Pointer to environment specific context used
121 to identify the component being measured.
122 @param Token Pointer to a Null-terminated ASCII string
123 that identifies the component being measured.
124 @param Module Pointer to a Null-terminated ASCII string
125 that identifies the module being measured.
126 @param TimeStamp 64-bit time stamp.
127
128 @retval RETURN_SUCCESS The end of the measurement was recorded.
129 @retval RETURN_NOT_FOUND The specified measurement record could not be found.
130
131 **/
132 RETURN_STATUS
133 EFIAPI
134 EndPerformanceMeasurement (
135 IN CONST VOID *Handle, OPTIONAL
136 IN CONST CHAR8 *Token, OPTIONAL
137 IN CONST CHAR8 *Module, OPTIONAL
138 IN UINT64 TimeStamp
139 )
140 {
141 return EFI_UNSUPPORTED;
142 }
143
144 /**
145 Retrieves all previous logged performance measurement.
146 Function will use SMM communicate protocol to get all previous SMM performance measurement data.
147 If success, data buffer will be returned. If fail function will return NULL.
148
149 @retval !NULL Get all gauge data success.
150 @retval NULL Get all guage data failed.
151 **/
152 GAUGE_DATA_ENTRY*
153 EFIAPI
154 GetAllSmmGaugeData (VOID)
155 {
156 EFI_STATUS Status;
157 EFI_SMM_COMMUNICATE_HEADER *SmmCommBufferHeader;
158 SMM_PERF_COMMUNICATE *SmmPerfCommData;
159 UINTN CommSize;
160 UINTN DataSize;
161
162 if (mGaugeData != NULL) {
163 return mGaugeData;
164 }
165
166 Status = GetCommunicationProtocol ();
167 if (EFI_ERROR (Status)) {
168 return NULL;
169 }
170
171 //
172 // Initialize communicate buffer
173 //
174 SmmCommBufferHeader = (EFI_SMM_COMMUNICATE_HEADER*)mSmmPerformanceBuffer;
175 SmmPerfCommData = (SMM_PERF_COMMUNICATE*)SmmCommBufferHeader->Data;
176 ZeroMem((UINT8*)SmmPerfCommData, sizeof(SMM_PERF_COMMUNICATE));
177
178 CopyGuid (&SmmCommBufferHeader->HeaderGuid, &gSmmPerformanceProtocolGuid);
179 SmmCommBufferHeader->MessageLength = sizeof(SMM_PERF_COMMUNICATE);
180 CommSize = OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data) + sizeof(SMM_PERF_COMMUNICATE);
181
182 //
183 // Get totol number of SMM gauge entries
184 //
185 SmmPerfCommData->Function = SMM_PERF_FUNCTION_GET_GAUGE_ENTRY_NUMBER;
186 Status = mSmmCommunication->Communicate (mSmmCommunication, mSmmPerformanceBuffer, &CommSize);
187 ASSERT_EFI_ERROR (Status);
188
189 if (EFI_ERROR (SmmPerfCommData->ReturnStatus) || SmmPerfCommData->NumberOfEntries == 0) {
190 return NULL;
191 }
192
193 mGaugeNumberOfEntries = SmmPerfCommData->NumberOfEntries;
194
195 DataSize = mGaugeNumberOfEntries * sizeof(GAUGE_DATA_ENTRY);
196 mGaugeData = AllocateZeroPool(DataSize);
197 ASSERT_EFI_ERROR (Status);
198
199 //
200 // Get all SMM gauge data
201 //
202 SmmPerfCommData->Function = SMM_PERF_FUNCTION_GET_GAUGE_DATA;
203 SmmPerfCommData->LogEntryKey = 0;
204 SmmPerfCommData->NumberOfEntries = mGaugeNumberOfEntries;
205 SmmPerfCommData->GaugeData = mGaugeData;
206 Status = mSmmCommunication->Communicate (mSmmCommunication, mSmmPerformanceBuffer, &CommSize);
207 ASSERT_EFI_ERROR (Status);
208 ASSERT_EFI_ERROR(SmmPerfCommData->ReturnStatus);
209
210 return mGaugeData;
211 }
212
213 /**
214 Retrieves a previously logged performance measurement.
215
216 Retrieves the performance log entry from the performance log specified by LogEntryKey.
217 If it stands for a valid entry, then EFI_SUCCESS is returned and
218 GaugeDataEntry stores the pointer to that entry.
219
220 @param LogEntryKey The key for the previous performance measurement log entry.
221 If 0, then the first performance measurement log entry is retrieved.
222 @param GaugeDataEntry The indirect pointer to the gauge data entry specified by LogEntryKey
223 if the retrieval is successful.
224
225 @retval EFI_SUCCESS The GuageDataEntry is successfully found based on LogEntryKey.
226 @retval EFI_NOT_FOUND The LogEntryKey is the last entry (equals to the total entry number).
227 @retval EFI_INVALIDE_PARAMETER The LogEntryKey is not a valid entry (greater than the total entry number).
228 @retval EFI_INVALIDE_PARAMETER GaugeDataEntry is NULL.
229
230 **/
231 EFI_STATUS
232 EFIAPI
233 GetGauge (
234 IN UINTN LogEntryKey,
235 OUT GAUGE_DATA_ENTRY **GaugeDataEntry
236 )
237 {
238 if (LogEntryKey > mGaugeNumberOfEntries) {
239 return EFI_INVALID_PARAMETER;
240 }
241 if (LogEntryKey == mGaugeNumberOfEntries) {
242 return EFI_NOT_FOUND;
243 }
244
245 if (GaugeDataEntry == NULL) {
246 return EFI_INVALID_PARAMETER;
247 }
248 *GaugeDataEntry = &mGaugeData[LogEntryKey];
249
250 return EFI_SUCCESS;
251 }
252
253 /**
254 Attempts to retrieve a performance measurement log entry from the performance measurement log.
255
256 Attempts to retrieve the performance log entry specified by LogEntryKey. If LogEntryKey is
257 zero on entry, then an attempt is made to retrieve the first entry from the performance log,
258 and the key for the second entry in the log is returned. If the performance log is empty,
259 then no entry is retrieved and zero is returned. If LogEntryKey is not zero, then the performance
260 log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is
261 returned. If LogEntryKey is the key for the last entry in the log, then the last log entry is
262 retrieved and an implementation specific non-zero key value that specifies the end of the performance
263 log is returned. If LogEntryKey is equal this implementation specific non-zero key value, then no entry
264 is retrieved and zero is returned. In the cases where a performance log entry can be returned,
265 the log entry is returned in Handle, Token, Module, StartTimeStamp, and EndTimeStamp.
266 If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().
267 If Handle is NULL, then ASSERT().
268 If Token is NULL, then ASSERT().
269 If Module is NULL, then ASSERT().
270 If StartTimeStamp is NULL, then ASSERT().
271 If EndTimeStamp is NULL, then ASSERT().
272
273 @param LogEntryKey On entry, the key of the performance measurement log entry to retrieve.
274 0, then the first performance measurement log entry is retrieved.
275 On exit, the key of the next performance log entry.
276 @param Handle Pointer to environment specific context used to identify the component
277 being measured.
278 @param Token Pointer to a Null-terminated ASCII string that identifies the component
279 being measured.
280 @param Module Pointer to a Null-terminated ASCII string that identifies the module
281 being measured.
282 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
283 was started.
284 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
285 was ended.
286
287 @return The key for the next performance log entry (in general case).
288
289 **/
290 UINTN
291 EFIAPI
292 GetPerformanceMeasurement (
293 IN UINTN LogEntryKey,
294 OUT CONST VOID **Handle,
295 OUT CONST CHAR8 **Token,
296 OUT CONST CHAR8 **Module,
297 OUT UINT64 *StartTimeStamp,
298 OUT UINT64 *EndTimeStamp
299 )
300 {
301 EFI_STATUS Status;
302 GAUGE_DATA_ENTRY *GaugeData;
303
304 GaugeData = NULL;
305
306 ASSERT (Handle != NULL);
307 ASSERT (Token != NULL);
308 ASSERT (Module != NULL);
309 ASSERT (StartTimeStamp != NULL);
310 ASSERT (EndTimeStamp != NULL);
311
312 mGaugeData = GetAllSmmGaugeData();
313 if (mGaugeData == NULL) {
314 return 0;
315 }
316
317 Status = GetGauge (LogEntryKey++, &GaugeData);
318
319 //
320 // Make sure that LogEntryKey is a valid log entry key,
321 //
322 ASSERT (Status != EFI_INVALID_PARAMETER);
323
324 if (EFI_ERROR (Status)) {
325 //
326 // The LogEntryKey is the last entry (equals to the total entry number).
327 //
328 return 0;
329
330 }
331
332 ASSERT (GaugeData != NULL);
333
334 *Handle = (VOID *) (UINTN) GaugeData->Handle;
335 *Token = GaugeData->Token;
336 *Module = GaugeData->Module;
337 *StartTimeStamp = GaugeData->StartTimeStamp;
338 *EndTimeStamp = GaugeData->EndTimeStamp;
339
340 return LogEntryKey;
341 }
342
343 /**
344 Returns TRUE if the performance measurement macros are enabled.
345
346 This function returns TRUE if the PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
347 PcdPerformanceLibraryPropertyMask is set. Otherwise FALSE is returned.
348
349 @retval TRUE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
350 PcdPerformanceLibraryPropertyMask is set.
351 @retval FALSE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
352 PcdPerformanceLibraryPropertyMask is clear.
353
354 **/
355 BOOLEAN
356 EFIAPI
357 PerformanceMeasurementEnabled (
358 VOID
359 )
360 {
361 return (BOOLEAN) ((PcdGet8(PcdPerformanceLibraryPropertyMask) & PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED) != 0);
362 }