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