ArmPkg: only attempt buildin MmCommunicationDxe for AArch64
[mirror_edk2.git] / MdeModulePkg / Library / DxePerformanceLib / DxePerformanceLib.c
CommitLineData
8dbae30d 1/** @file\r
2 Performance Library\r
a0afd019 3\r
857dfc45 4 This library instance provides infrastructure for DXE phase drivers to log performance\r
f0da4d7d
SZ
5 data. It consumes PerformanceEx or Performance Protocol published by DxeCorePerformanceLib\r
6 to log performance data. If both PerformanceEx and Performance Protocol is not available, it does not log any\r
857dfc45 7 performance information.\r
8\r
d1102dba 9 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r
9d510e61 10SPDX-License-Identifier: BSD-2-Clause-Patent\r
a0afd019 11\r
8dbae30d 12**/\r
a0afd019 13\r
ed7748fe 14\r
a0afd019 15#include <PiDxe.h>\r
ed7748fe 16\r
137fb13d 17#include <Guid/PerformanceMeasurement.h>\r
ed7748fe 18\r
a0afd019 19#include <Library/PerformanceLib.h>\r
20#include <Library/DebugLib.h>\r
21#include <Library/UefiBootServicesTableLib.h>\r
22#include <Library/PcdLib.h>\r
23\r
857dfc45 24//\r
f0da4d7d 25// The cached Performance Protocol and PerformanceEx Protocol interface.\r
857dfc45 26//\r
137fb13d 27EDKII_PERFORMANCE_MEASUREMENT_PROTOCOL *mPerformanceMeasurement = NULL;\r
a0afd019 28\r
29/**\r
f0da4d7d 30 The function caches the pointers to PerformanceEx protocol and Performance Protocol.\r
a0afd019 31\r
f0da4d7d 32 The function locates PerformanceEx protocol and Performance Protocol from protocol database.\r
a0afd019 33\r
f0da4d7d
SZ
34 @retval EFI_SUCCESS PerformanceEx protocol or Performance Protocol is successfully located.\r
35 @retval EFI_NOT_FOUND Both PerformanceEx protocol and Performance Protocol are not located to log performance.\r
a0afd019 36\r
37**/\r
a0afd019 38EFI_STATUS\r
137fb13d 39GetPerformanceMeasurementProtocol (\r
a0afd019 40 VOID\r
41 )\r
42{\r
f0da4d7d 43 EFI_STATUS Status;\r
137fb13d 44 EDKII_PERFORMANCE_MEASUREMENT_PROTOCOL *PerformanceMeasurement;\r
a0afd019 45\r
137fb13d 46 if (mPerformanceMeasurement != NULL) {\r
f0da4d7d
SZ
47 return EFI_SUCCESS;\r
48 }\r
49\r
137fb13d 50 Status = gBS->LocateProtocol (&gEdkiiPerformanceMeasurementProtocolGuid, NULL, (VOID **) &PerformanceMeasurement);\r
f0da4d7d 51 if (!EFI_ERROR (Status)) {\r
137fb13d 52 ASSERT (PerformanceMeasurement != NULL);\r
f0da4d7d 53 //\r
137fb13d 54 // Cache PerformanceMeasurement Protocol.\r
f0da4d7d 55 //\r
137fb13d 56 mPerformanceMeasurement = PerformanceMeasurement;\r
f0da4d7d 57 return EFI_SUCCESS;\r
a0afd019 58 }\r
59\r
f0da4d7d 60 return EFI_NOT_FOUND;\r
a0afd019 61}\r
62\r
63/**\r
64 Creates a record for the beginning of a performance measurement.\r
65\r
f0da4d7d 66 Creates a record that contains the Handle, Token, Module and Identifier.\r
a0afd019 67 If TimeStamp is not zero, then TimeStamp is added to the record as the start time.\r
68 If TimeStamp is zero, then this function reads the current time stamp\r
69 and adds that time stamp value to the record as the start time.\r
70\r
71 @param Handle Pointer to environment specific context used\r
72 to identify the component being measured.\r
73 @param Token Pointer to a Null-terminated ASCII string\r
74 that identifies the component being measured.\r
75 @param Module Pointer to a Null-terminated ASCII string\r
76 that identifies the module being measured.\r
77 @param TimeStamp 64-bit time stamp.\r
f0da4d7d
SZ
78 @param Identifier 32-bit identifier. If the value is 0, the created record\r
79 is same as the one created by StartPerformanceMeasurement.\r
a0afd019 80\r
81 @retval RETURN_SUCCESS The start of the measurement was recorded.\r
82 @retval RETURN_OUT_OF_RESOURCES There are not enough resources to record the measurement.\r
83\r
84**/\r
85RETURN_STATUS\r
86EFIAPI\r
f0da4d7d 87StartPerformanceMeasurementEx (\r
a0afd019 88 IN CONST VOID *Handle, OPTIONAL\r
89 IN CONST CHAR8 *Token, OPTIONAL\r
90 IN CONST CHAR8 *Module, OPTIONAL\r
f0da4d7d
SZ
91 IN UINT64 TimeStamp,\r
92 IN UINT32 Identifier\r
a0afd019 93 )\r
94{\r
137fb13d
BD
95 EFI_STATUS Status;\r
96 CONST CHAR8* String;\r
a0afd019 97\r
137fb13d 98 Status = GetPerformanceMeasurementProtocol ();\r
a0afd019 99 if (EFI_ERROR (Status)) {\r
137fb13d
BD
100 return RETURN_NOT_FOUND;\r
101 }\r
102\r
103 if (Token != NULL) {\r
104 String = Token;\r
105 } else if (Module != NULL) {\r
106 String = Module;\r
107 } else {\r
108 String = NULL;\r
a0afd019 109 }\r
110\r
137fb13d
BD
111 if (mPerformanceMeasurement != NULL) {\r
112 Status = mPerformanceMeasurement->CreatePerformanceMeasurement (Handle, NULL, String, TimeStamp, 0, Identifier, PerfStartEntry);\r
f0da4d7d
SZ
113 } else {\r
114 ASSERT (FALSE);\r
115 }\r
a0afd019 116\r
117 return (RETURN_STATUS) Status;\r
118}\r
119\r
120/**\r
121 Fills in the end time of a performance measurement.\r
122\r
7a9395cd 123 Looks up the record that matches Handle, Token and Module.\r
a0afd019 124 If the record can not be found then return RETURN_NOT_FOUND.\r
125 If the record is found and TimeStamp is not zero,\r
126 then TimeStamp is added to the record as the end time.\r
127 If the record is found and TimeStamp is zero, then this function reads\r
128 the current time stamp and adds that time stamp value to the record as the end time.\r
a0afd019 129\r
130 @param Handle Pointer to environment specific context used\r
131 to identify the component being measured.\r
132 @param Token Pointer to a Null-terminated ASCII string\r
133 that identifies the component being measured.\r
134 @param Module Pointer to a Null-terminated ASCII string\r
135 that identifies the module being measured.\r
136 @param TimeStamp 64-bit time stamp.\r
f0da4d7d
SZ
137 @param Identifier 32-bit identifier. If the value is 0, the found record\r
138 is same as the one found by EndPerformanceMeasurement.\r
a0afd019 139\r
140 @retval RETURN_SUCCESS The end of the measurement was recorded.\r
141 @retval RETURN_NOT_FOUND The specified measurement record could not be found.\r
142\r
143**/\r
144RETURN_STATUS\r
145EFIAPI\r
f0da4d7d 146EndPerformanceMeasurementEx (\r
a0afd019 147 IN CONST VOID *Handle, OPTIONAL\r
148 IN CONST CHAR8 *Token, OPTIONAL\r
149 IN CONST CHAR8 *Module, OPTIONAL\r
f0da4d7d
SZ
150 IN UINT64 TimeStamp,\r
151 IN UINT32 Identifier\r
a0afd019 152 )\r
153{\r
137fb13d
BD
154 EFI_STATUS Status;\r
155 CONST CHAR8* String;\r
a0afd019 156\r
137fb13d 157 Status = GetPerformanceMeasurementProtocol ();\r
a0afd019 158 if (EFI_ERROR (Status)) {\r
159 return RETURN_NOT_FOUND;\r
160 }\r
161\r
137fb13d
BD
162 if (Token != NULL) {\r
163 String = Token;\r
164 } else if (Module != NULL) {\r
165 String = Module;\r
166 } else {\r
167 String = NULL;\r
168 }\r
169\r
170 if (mPerformanceMeasurement != NULL) {\r
171 Status = mPerformanceMeasurement->CreatePerformanceMeasurement (Handle, NULL, String, TimeStamp, 0, Identifier, PerfEndEntry);\r
f0da4d7d
SZ
172 } else {\r
173 ASSERT (FALSE);\r
174 }\r
a0afd019 175\r
176 return (RETURN_STATUS) Status;\r
177}\r
178\r
179/**\r
180 Attempts to retrieve a performance measurement log entry from the performance measurement log.\r
f0da4d7d
SZ
181 It can also retrieve the log created by StartPerformanceMeasurement and EndPerformanceMeasurement,\r
182 and then assign the Identifier with 0.\r
a0afd019 183\r
184 Attempts to retrieve the performance log entry specified by LogEntryKey. If LogEntryKey is\r
185 zero on entry, then an attempt is made to retrieve the first entry from the performance log,\r
186 and the key for the second entry in the log is returned. If the performance log is empty,\r
187 then no entry is retrieved and zero is returned. If LogEntryKey is not zero, then the performance\r
188 log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is\r
189 returned. If LogEntryKey is the key for the last entry in the log, then the last log entry is\r
190 retrieved and an implementation specific non-zero key value that specifies the end of the performance\r
191 log is returned. If LogEntryKey is equal this implementation specific non-zero key value, then no entry\r
192 is retrieved and zero is returned. In the cases where a performance log entry can be returned,\r
f0da4d7d 193 the log entry is returned in Handle, Token, Module, StartTimeStamp, EndTimeStamp and Identifier.\r
a0afd019 194 If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().\r
195 If Handle is NULL, then ASSERT().\r
196 If Token is NULL, then ASSERT().\r
197 If Module is NULL, then ASSERT().\r
198 If StartTimeStamp is NULL, then ASSERT().\r
199 If EndTimeStamp is NULL, then ASSERT().\r
f0da4d7d 200 If Identifier is NULL, then ASSERT().\r
a0afd019 201\r
202 @param LogEntryKey On entry, the key of the performance measurement log entry to retrieve.\r
203 0, then the first performance measurement log entry is retrieved.\r
857dfc45 204 On exit, the key of the next performance log entry.\r
a0afd019 205 @param Handle Pointer to environment specific context used to identify the component\r
206 being measured.\r
207 @param Token Pointer to a Null-terminated ASCII string that identifies the component\r
208 being measured.\r
209 @param Module Pointer to a Null-terminated ASCII string that identifies the module\r
210 being measured.\r
211 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement\r
212 was started.\r
213 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement\r
214 was ended.\r
f0da4d7d 215 @param Identifier Pointer to the 32-bit identifier that was recorded.\r
a0afd019 216\r
217 @return The key for the next performance log entry (in general case).\r
218\r
219**/\r
220UINTN\r
221EFIAPI\r
f0da4d7d 222GetPerformanceMeasurementEx (\r
d1102dba 223 IN UINTN LogEntryKey,\r
a0afd019 224 OUT CONST VOID **Handle,\r
225 OUT CONST CHAR8 **Token,\r
226 OUT CONST CHAR8 **Module,\r
227 OUT UINT64 *StartTimeStamp,\r
f0da4d7d
SZ
228 OUT UINT64 *EndTimeStamp,\r
229 OUT UINT32 *Identifier\r
a0afd019 230 )\r
231{\r
137fb13d 232 return 0;\r
a0afd019 233\r
a0afd019 234}\r
235\r
f0da4d7d
SZ
236/**\r
237 Creates a record for the beginning of a performance measurement.\r
238\r
239 Creates a record that contains the Handle, Token, and Module.\r
240 If TimeStamp is not zero, then TimeStamp is added to the record as the start time.\r
241 If TimeStamp is zero, then this function reads the current time stamp\r
242 and adds that time stamp value to the record as the start time.\r
243\r
244 @param Handle Pointer to environment specific context used\r
245 to identify the component being measured.\r
246 @param Token Pointer to a Null-terminated ASCII string\r
247 that identifies the component being measured.\r
248 @param Module Pointer to a Null-terminated ASCII string\r
249 that identifies the module being measured.\r
250 @param TimeStamp 64-bit time stamp.\r
251\r
252 @retval RETURN_SUCCESS The start of the measurement was recorded.\r
253 @retval RETURN_OUT_OF_RESOURCES There are not enough resources to record the measurement.\r
254\r
255**/\r
256RETURN_STATUS\r
257EFIAPI\r
258StartPerformanceMeasurement (\r
259 IN CONST VOID *Handle, OPTIONAL\r
260 IN CONST CHAR8 *Token, OPTIONAL\r
261 IN CONST CHAR8 *Module, OPTIONAL\r
262 IN UINT64 TimeStamp\r
263 )\r
264{\r
265 return StartPerformanceMeasurementEx (Handle, Token, Module, TimeStamp, 0);\r
266}\r
267\r
268/**\r
269 Fills in the end time of a performance measurement.\r
270\r
271 Looks up the record that matches Handle, Token, and Module.\r
272 If the record can not be found then return RETURN_NOT_FOUND.\r
273 If the record is found and TimeStamp is not zero,\r
274 then TimeStamp is added to the record as the end time.\r
275 If the record is found and TimeStamp is zero, then this function reads\r
276 the current time stamp and adds that time stamp value to the record as the end time.\r
277\r
278 @param Handle Pointer to environment specific context used\r
279 to identify the component being measured.\r
280 @param Token Pointer to a Null-terminated ASCII string\r
281 that identifies the component being measured.\r
282 @param Module Pointer to a Null-terminated ASCII string\r
283 that identifies the module being measured.\r
284 @param TimeStamp 64-bit time stamp.\r
285\r
286 @retval RETURN_SUCCESS The end of the measurement was recorded.\r
287 @retval RETURN_NOT_FOUND The specified measurement record could not be found.\r
288\r
289**/\r
290RETURN_STATUS\r
291EFIAPI\r
292EndPerformanceMeasurement (\r
293 IN CONST VOID *Handle, OPTIONAL\r
294 IN CONST CHAR8 *Token, OPTIONAL\r
295 IN CONST CHAR8 *Module, OPTIONAL\r
296 IN UINT64 TimeStamp\r
297 )\r
298{\r
299 return EndPerformanceMeasurementEx (Handle, Token, Module, TimeStamp, 0);\r
300}\r
301\r
302/**\r
303 Attempts to retrieve a performance measurement log entry from the performance measurement log.\r
304 It can also retrieve the log created by StartPerformanceMeasurementEx and EndPerformanceMeasurementEx,\r
305 and then eliminate the Identifier.\r
306\r
307 Attempts to retrieve the performance log entry specified by LogEntryKey. If LogEntryKey is\r
308 zero on entry, then an attempt is made to retrieve the first entry from the performance log,\r
309 and the key for the second entry in the log is returned. If the performance log is empty,\r
310 then no entry is retrieved and zero is returned. If LogEntryKey is not zero, then the performance\r
311 log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is\r
312 returned. If LogEntryKey is the key for the last entry in the log, then the last log entry is\r
313 retrieved and an implementation specific non-zero key value that specifies the end of the performance\r
314 log is returned. If LogEntryKey is equal this implementation specific non-zero key value, then no entry\r
315 is retrieved and zero is returned. In the cases where a performance log entry can be returned,\r
316 the log entry is returned in Handle, Token, Module, StartTimeStamp, and EndTimeStamp.\r
317 If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().\r
318 If Handle is NULL, then ASSERT().\r
319 If Token is NULL, then ASSERT().\r
320 If Module is NULL, then ASSERT().\r
321 If StartTimeStamp is NULL, then ASSERT().\r
322 If EndTimeStamp is NULL, then ASSERT().\r
323\r
324 @param LogEntryKey On entry, the key of the performance measurement log entry to retrieve.\r
325 0, then the first performance measurement log entry is retrieved.\r
326 On exit, the key of the next performance log entry.\r
327 @param Handle Pointer to environment specific context used to identify the component\r
328 being measured.\r
329 @param Token Pointer to a Null-terminated ASCII string that identifies the component\r
330 being measured.\r
331 @param Module Pointer to a Null-terminated ASCII string that identifies the module\r
332 being measured.\r
333 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement\r
334 was started.\r
335 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement\r
336 was ended.\r
337\r
338 @return The key for the next performance log entry (in general case).\r
339\r
340**/\r
341UINTN\r
342EFIAPI\r
343GetPerformanceMeasurement (\r
344 IN UINTN LogEntryKey,\r
345 OUT CONST VOID **Handle,\r
346 OUT CONST CHAR8 **Token,\r
347 OUT CONST CHAR8 **Module,\r
348 OUT UINT64 *StartTimeStamp,\r
349 OUT UINT64 *EndTimeStamp\r
350 )\r
351{\r
137fb13d 352 return 0;\r
f0da4d7d
SZ
353}\r
354\r
a0afd019 355/**\r
356 Returns TRUE if the performance measurement macros are enabled.\r
357\r
358 This function returns TRUE if the PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of\r
359 PcdPerformanceLibraryPropertyMask is set. Otherwise FALSE is returned.\r
360\r
361 @retval TRUE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of\r
362 PcdPerformanceLibraryPropertyMask is set.\r
363 @retval FALSE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of\r
364 PcdPerformanceLibraryPropertyMask is clear.\r
365\r
366**/\r
367BOOLEAN\r
368EFIAPI\r
369PerformanceMeasurementEnabled (\r
370 VOID\r
371 )\r
372{\r
373 return (BOOLEAN) ((PcdGet8(PcdPerformanceLibraryPropertyMask) & PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED) != 0);\r
374}\r
6b4d58a1
BD
375\r
376/**\r
377 Create performance record with event description and a timestamp.\r
378\r
379 @param CallerIdentifier - Image handle or pointer to caller ID GUID\r
380 @param Guid - Pointer to a GUID\r
381 @param String - Pointer to a string describing the measurement\r
382 @param Address - Pointer to a location in memory relevant to the measurement\r
383 @param Identifier - Performance identifier describing the type of measurement\r
384\r
385 @retval RETURN_SUCCESS - Successfully created performance record\r
386 @retval RETURN_OUT_OF_RESOURCES - Ran out of space to store the records\r
387 @retval RETURN_INVALID_PARAMETER - Invalid parameter passed to function - NULL\r
388 pointer or invalid PerfId\r
389\r
390**/\r
391RETURN_STATUS\r
392EFIAPI\r
393LogPerformanceMeasurement (\r
394 IN CONST VOID *CallerIdentifier,\r
395 IN CONST VOID *Guid, OPTIONAL\r
396 IN CONST CHAR8 *String, OPTIONAL\r
397 IN UINT64 Address, OPTIONAL\r
398 IN UINT32 Identifier\r
399 )\r
400{\r
401 EFI_STATUS Status;\r
402\r
403 Status = GetPerformanceMeasurementProtocol ();\r
404 if (EFI_ERROR (Status)) {\r
405 return RETURN_OUT_OF_RESOURCES;\r
406 }\r
407\r
408 if (mPerformanceMeasurement != NULL) {\r
409 Status = mPerformanceMeasurement->CreatePerformanceMeasurement (CallerIdentifier, Guid, String, 0, Address, Identifier, PerfEntry);\r
410 } else {\r
411 ASSERT (FALSE);\r
412 }\r
413\r
414 return (RETURN_STATUS) Status;\r
415}\r
416\r
417/**\r
418 Check whether the specified performance measurement can be logged.\r
419\r
420 This function returns TRUE when the PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of PcdPerformanceLibraryPropertyMask is set\r
421 and the Type disable bit in PcdPerformanceLibraryPropertyMask is not set.\r
422\r
423 @param Type - Type of the performance measurement entry.\r
424\r
425 @retval TRUE The performance measurement can be logged.\r
426 @retval FALSE The performance measurement can NOT be logged.\r
427\r
428**/\r
429BOOLEAN\r
430EFIAPI\r
431LogPerformanceMeasurementEnabled (\r
432 IN CONST UINTN Type\r
433 )\r
434{\r
435 //\r
436 // When Performance measurement is enabled and the type is not filtered, the performance can be logged.\r
437 //\r
438 if (PerformanceMeasurementEnabled () && (PcdGet8(PcdPerformanceLibraryPropertyMask) & Type) == 0) {\r
439 return TRUE;\r
440 }\r
441 return FALSE;\r
442}\r