]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Library/DxePerformanceLib/DxePerformanceLib.c
Add new extension PerformanceLib APIs to store ID info.
[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
3dffec12 9 Copyright (c) 2006 - 2012, 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
ee0961f7 23#include <Guid/Performance.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
f0da4d7d
SZ
33PERFORMANCE_PROTOCOL *mPerformance = NULL;\r
34PERFORMANCE_EX_PROTOCOL *mPerformanceEx = NULL;\r
a0afd019 35\r
36/**\r
f0da4d7d 37 The function caches the pointers to PerformanceEx protocol and Performance Protocol.\r
a0afd019 38\r
f0da4d7d 39 The function locates PerformanceEx protocol and Performance Protocol from protocol database.\r
a0afd019 40\r
f0da4d7d
SZ
41 @retval EFI_SUCCESS PerformanceEx protocol or Performance Protocol is successfully located.\r
42 @retval EFI_NOT_FOUND Both PerformanceEx protocol and Performance Protocol are not located to log performance.\r
a0afd019 43\r
44**/\r
a0afd019 45EFI_STATUS\r
46GetPerformanceProtocol (\r
47 VOID\r
48 )\r
49{\r
f0da4d7d
SZ
50 EFI_STATUS Status;\r
51 PERFORMANCE_PROTOCOL *Performance;\r
52 PERFORMANCE_EX_PROTOCOL *PerformanceEx;\r
a0afd019 53\r
f0da4d7d
SZ
54 if (mPerformanceEx != NULL || mPerformance != NULL) {\r
55 return EFI_SUCCESS;\r
56 }\r
57\r
58 Status = gBS->LocateProtocol (&gPerformanceExProtocolGuid, NULL, (VOID **) &PerformanceEx);\r
59 if (!EFI_ERROR (Status)) {\r
60 ASSERT (PerformanceEx != NULL);\r
61 //\r
62 // Cache PerformanceEx Protocol.\r
63 //\r
64 mPerformanceEx = PerformanceEx;\r
a0afd019 65 return EFI_SUCCESS;\r
66 }\r
67\r
68 Status = gBS->LocateProtocol (&gPerformanceProtocolGuid, NULL, (VOID **) &Performance);\r
69 if (!EFI_ERROR (Status)) {\r
70 ASSERT (Performance != NULL);\r
71 //\r
72 // Cache performance protocol.\r
73 //\r
74 mPerformance = Performance;\r
f0da4d7d 75 return EFI_SUCCESS;\r
a0afd019 76 }\r
77\r
f0da4d7d 78 return EFI_NOT_FOUND;\r
a0afd019 79}\r
80\r
81/**\r
82 Creates a record for the beginning of a performance measurement.\r
83\r
f0da4d7d 84 Creates a record that contains the Handle, Token, Module and Identifier.\r
a0afd019 85 If TimeStamp is not zero, then TimeStamp is added to the record as the start time.\r
86 If TimeStamp is zero, then this function reads the current time stamp\r
87 and adds that time stamp value to the record as the start time.\r
88\r
89 @param Handle Pointer to environment specific context used\r
90 to identify the component being measured.\r
91 @param Token Pointer to a Null-terminated ASCII string\r
92 that identifies the component being measured.\r
93 @param Module Pointer to a Null-terminated ASCII string\r
94 that identifies the module being measured.\r
95 @param TimeStamp 64-bit time stamp.\r
f0da4d7d
SZ
96 @param Identifier 32-bit identifier. If the value is 0, the created record\r
97 is same as the one created by StartPerformanceMeasurement.\r
a0afd019 98\r
99 @retval RETURN_SUCCESS The start of the measurement was recorded.\r
100 @retval RETURN_OUT_OF_RESOURCES There are not enough resources to record the measurement.\r
101\r
102**/\r
103RETURN_STATUS\r
104EFIAPI\r
f0da4d7d 105StartPerformanceMeasurementEx (\r
a0afd019 106 IN CONST VOID *Handle, OPTIONAL\r
107 IN CONST CHAR8 *Token, OPTIONAL\r
108 IN CONST CHAR8 *Module, OPTIONAL\r
f0da4d7d
SZ
109 IN UINT64 TimeStamp,\r
110 IN UINT32 Identifier\r
a0afd019 111 )\r
112{\r
113 EFI_STATUS Status;\r
114\r
115 Status = GetPerformanceProtocol ();\r
116 if (EFI_ERROR (Status)) {\r
117 return RETURN_OUT_OF_RESOURCES;\r
118 }\r
119\r
f0da4d7d
SZ
120 if (mPerformanceEx != NULL) {\r
121 Status = mPerformanceEx->StartGaugeEx (Handle, Token, Module, TimeStamp, Identifier);\r
122 } else if (mPerformance != NULL) {\r
123 Status = mPerformance->StartGauge (Handle, Token, Module, TimeStamp);\r
124 } else {\r
125 ASSERT (FALSE);\r
126 }\r
a0afd019 127\r
128 return (RETURN_STATUS) Status;\r
129}\r
130\r
131/**\r
132 Fills in the end time of a performance measurement.\r
133\r
f0da4d7d 134 Looks up the record that matches Handle, Token, Module and Identifier.\r
a0afd019 135 If the record can not be found then return RETURN_NOT_FOUND.\r
136 If the record is found and TimeStamp is not zero,\r
137 then TimeStamp is added to the record as the end time.\r
138 If the record is found and TimeStamp is zero, then this function reads\r
139 the current time stamp and adds that time stamp value to the record as the end time.\r
a0afd019 140\r
141 @param Handle Pointer to environment specific context used\r
142 to identify the component being measured.\r
143 @param Token Pointer to a Null-terminated ASCII string\r
144 that identifies the component being measured.\r
145 @param Module Pointer to a Null-terminated ASCII string\r
146 that identifies the module being measured.\r
147 @param TimeStamp 64-bit time stamp.\r
f0da4d7d
SZ
148 @param Identifier 32-bit identifier. If the value is 0, the found record\r
149 is same as the one found by EndPerformanceMeasurement.\r
a0afd019 150\r
151 @retval RETURN_SUCCESS The end of the measurement was recorded.\r
152 @retval RETURN_NOT_FOUND The specified measurement record could not be found.\r
153\r
154**/\r
155RETURN_STATUS\r
156EFIAPI\r
f0da4d7d 157EndPerformanceMeasurementEx (\r
a0afd019 158 IN CONST VOID *Handle, OPTIONAL\r
159 IN CONST CHAR8 *Token, OPTIONAL\r
160 IN CONST CHAR8 *Module, OPTIONAL\r
f0da4d7d
SZ
161 IN UINT64 TimeStamp,\r
162 IN UINT32 Identifier\r
a0afd019 163 )\r
164{\r
165 EFI_STATUS Status;\r
166\r
167 Status = GetPerformanceProtocol ();\r
168 if (EFI_ERROR (Status)) {\r
169 return RETURN_NOT_FOUND;\r
170 }\r
171\r
f0da4d7d
SZ
172 if (mPerformanceEx != NULL) {\r
173 Status = mPerformanceEx->EndGaugeEx (Handle, Token, Module, TimeStamp, Identifier);\r
174 } else if (mPerformance != NULL) {\r
175 Status = mPerformance->EndGauge (Handle, Token, Module, TimeStamp);\r
176 } else {\r
177 ASSERT (FALSE);\r
178 }\r
a0afd019 179\r
180 return (RETURN_STATUS) Status;\r
181}\r
182\r
183/**\r
184 Attempts to retrieve a performance measurement log entry from the performance measurement log.\r
f0da4d7d
SZ
185 It can also retrieve the log created by StartPerformanceMeasurement and EndPerformanceMeasurement,\r
186 and then assign the Identifier with 0.\r
a0afd019 187\r
188 Attempts to retrieve the performance log entry specified by LogEntryKey. If LogEntryKey is\r
189 zero on entry, then an attempt is made to retrieve the first entry from the performance log,\r
190 and the key for the second entry in the log is returned. If the performance log is empty,\r
191 then no entry is retrieved and zero is returned. If LogEntryKey is not zero, then the performance\r
192 log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is\r
193 returned. If LogEntryKey is the key for the last entry in the log, then the last log entry is\r
194 retrieved and an implementation specific non-zero key value that specifies the end of the performance\r
195 log is returned. If LogEntryKey is equal this implementation specific non-zero key value, then no entry\r
196 is retrieved and zero is returned. In the cases where a performance log entry can be returned,\r
f0da4d7d 197 the log entry is returned in Handle, Token, Module, StartTimeStamp, EndTimeStamp and Identifier.\r
a0afd019 198 If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().\r
199 If Handle is NULL, then ASSERT().\r
200 If Token is NULL, then ASSERT().\r
201 If Module is NULL, then ASSERT().\r
202 If StartTimeStamp is NULL, then ASSERT().\r
203 If EndTimeStamp is NULL, then ASSERT().\r
f0da4d7d 204 If Identifier is NULL, then ASSERT().\r
a0afd019 205\r
206 @param LogEntryKey On entry, the key of the performance measurement log entry to retrieve.\r
207 0, then the first performance measurement log entry is retrieved.\r
857dfc45 208 On exit, the key of the next performance log entry.\r
a0afd019 209 @param Handle Pointer to environment specific context used to identify the component\r
210 being measured.\r
211 @param Token Pointer to a Null-terminated ASCII string that identifies the component\r
212 being measured.\r
213 @param Module Pointer to a Null-terminated ASCII string that identifies the module\r
214 being measured.\r
215 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement\r
216 was started.\r
217 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement\r
218 was ended.\r
f0da4d7d 219 @param Identifier Pointer to the 32-bit identifier that was recorded.\r
a0afd019 220\r
221 @return The key for the next performance log entry (in general case).\r
222\r
223**/\r
224UINTN\r
225EFIAPI\r
f0da4d7d
SZ
226GetPerformanceMeasurementEx (\r
227 IN UINTN LogEntryKey, \r
a0afd019 228 OUT CONST VOID **Handle,\r
229 OUT CONST CHAR8 **Token,\r
230 OUT CONST CHAR8 **Module,\r
231 OUT UINT64 *StartTimeStamp,\r
f0da4d7d
SZ
232 OUT UINT64 *EndTimeStamp,\r
233 OUT UINT32 *Identifier\r
a0afd019 234 )\r
235{\r
f0da4d7d
SZ
236 EFI_STATUS Status;\r
237 GAUGE_DATA_ENTRY_EX *GaugeData;\r
238\r
239 GaugeData = NULL;\r
a0afd019 240\r
241 ASSERT (Handle != NULL);\r
242 ASSERT (Token != NULL);\r
243 ASSERT (Module != NULL);\r
244 ASSERT (StartTimeStamp != NULL);\r
245 ASSERT (EndTimeStamp != NULL);\r
f0da4d7d 246 ASSERT (Identifier != NULL);\r
a0afd019 247\r
248 Status = GetPerformanceProtocol ();\r
249 if (EFI_ERROR (Status)) {\r
250 return 0;\r
251 }\r
252\r
f0da4d7d
SZ
253 if (mPerformanceEx != NULL) {\r
254 Status = mPerformanceEx->GetGaugeEx (LogEntryKey++, &GaugeData);\r
255 } else if (mPerformance != NULL) {\r
256 Status = mPerformance->GetGauge (LogEntryKey++, (GAUGE_DATA_ENTRY **) &GaugeData);\r
257 } else {\r
258 ASSERT (FALSE);\r
259 return 0;\r
260 }\r
a0afd019 261\r
262 //\r
263 // Make sure that LogEntryKey is a valid log entry key,\r
264 //\r
265 ASSERT (Status != EFI_INVALID_PARAMETER);\r
266\r
267 if (EFI_ERROR (Status)) {\r
268 //\r
269 // The LogEntryKey is the last entry (equals to the total entry number).\r
270 //\r
271 return 0;\r
272 }\r
273\r
274 ASSERT (GaugeData != NULL);\r
275\r
276 *Handle = (VOID *) (UINTN) GaugeData->Handle;\r
277 *Token = GaugeData->Token;\r
278 *Module = GaugeData->Module;\r
279 *StartTimeStamp = GaugeData->StartTimeStamp;\r
280 *EndTimeStamp = GaugeData->EndTimeStamp;\r
f0da4d7d
SZ
281 if (mPerformanceEx != NULL) {\r
282 *Identifier = GaugeData->Identifier;\r
283 } else {\r
284 *Identifier = 0;\r
285 }\r
a0afd019 286\r
287 return LogEntryKey;\r
288}\r
289\r
f0da4d7d
SZ
290/**\r
291 Creates a record for the beginning of a performance measurement.\r
292\r
293 Creates a record that contains the Handle, Token, and Module.\r
294 If TimeStamp is not zero, then TimeStamp is added to the record as the start time.\r
295 If TimeStamp is zero, then this function reads the current time stamp\r
296 and adds that time stamp value to the record as the start time.\r
297\r
298 @param Handle Pointer to environment specific context used\r
299 to identify the component being measured.\r
300 @param Token Pointer to a Null-terminated ASCII string\r
301 that identifies the component being measured.\r
302 @param Module Pointer to a Null-terminated ASCII string\r
303 that identifies the module being measured.\r
304 @param TimeStamp 64-bit time stamp.\r
305\r
306 @retval RETURN_SUCCESS The start of the measurement was recorded.\r
307 @retval RETURN_OUT_OF_RESOURCES There are not enough resources to record the measurement.\r
308\r
309**/\r
310RETURN_STATUS\r
311EFIAPI\r
312StartPerformanceMeasurement (\r
313 IN CONST VOID *Handle, OPTIONAL\r
314 IN CONST CHAR8 *Token, OPTIONAL\r
315 IN CONST CHAR8 *Module, OPTIONAL\r
316 IN UINT64 TimeStamp\r
317 )\r
318{\r
319 return StartPerformanceMeasurementEx (Handle, Token, Module, TimeStamp, 0);\r
320}\r
321\r
322/**\r
323 Fills in the end time of a performance measurement.\r
324\r
325 Looks up the record that matches Handle, Token, and Module.\r
326 If the record can not be found then return RETURN_NOT_FOUND.\r
327 If the record is found and TimeStamp is not zero,\r
328 then TimeStamp is added to the record as the end time.\r
329 If the record is found and TimeStamp is zero, then this function reads\r
330 the current time stamp and adds that time stamp value to the record as the end time.\r
331\r
332 @param Handle Pointer to environment specific context used\r
333 to identify the component being measured.\r
334 @param Token Pointer to a Null-terminated ASCII string\r
335 that identifies the component being measured.\r
336 @param Module Pointer to a Null-terminated ASCII string\r
337 that identifies the module being measured.\r
338 @param TimeStamp 64-bit time stamp.\r
339\r
340 @retval RETURN_SUCCESS The end of the measurement was recorded.\r
341 @retval RETURN_NOT_FOUND The specified measurement record could not be found.\r
342\r
343**/\r
344RETURN_STATUS\r
345EFIAPI\r
346EndPerformanceMeasurement (\r
347 IN CONST VOID *Handle, OPTIONAL\r
348 IN CONST CHAR8 *Token, OPTIONAL\r
349 IN CONST CHAR8 *Module, OPTIONAL\r
350 IN UINT64 TimeStamp\r
351 )\r
352{\r
353 return EndPerformanceMeasurementEx (Handle, Token, Module, TimeStamp, 0);\r
354}\r
355\r
356/**\r
357 Attempts to retrieve a performance measurement log entry from the performance measurement log.\r
358 It can also retrieve the log created by StartPerformanceMeasurementEx and EndPerformanceMeasurementEx,\r
359 and then eliminate the Identifier.\r
360\r
361 Attempts to retrieve the performance log entry specified by LogEntryKey. If LogEntryKey is\r
362 zero on entry, then an attempt is made to retrieve the first entry from the performance log,\r
363 and the key for the second entry in the log is returned. If the performance log is empty,\r
364 then no entry is retrieved and zero is returned. If LogEntryKey is not zero, then the performance\r
365 log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is\r
366 returned. If LogEntryKey is the key for the last entry in the log, then the last log entry is\r
367 retrieved and an implementation specific non-zero key value that specifies the end of the performance\r
368 log is returned. If LogEntryKey is equal this implementation specific non-zero key value, then no entry\r
369 is retrieved and zero is returned. In the cases where a performance log entry can be returned,\r
370 the log entry is returned in Handle, Token, Module, StartTimeStamp, and EndTimeStamp.\r
371 If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().\r
372 If Handle is NULL, then ASSERT().\r
373 If Token is NULL, then ASSERT().\r
374 If Module is NULL, then ASSERT().\r
375 If StartTimeStamp is NULL, then ASSERT().\r
376 If EndTimeStamp is NULL, then ASSERT().\r
377\r
378 @param LogEntryKey On entry, the key of the performance measurement log entry to retrieve.\r
379 0, then the first performance measurement log entry is retrieved.\r
380 On exit, the key of the next performance log entry.\r
381 @param Handle Pointer to environment specific context used to identify the component\r
382 being measured.\r
383 @param Token Pointer to a Null-terminated ASCII string that identifies the component\r
384 being measured.\r
385 @param Module Pointer to a Null-terminated ASCII string that identifies the module\r
386 being measured.\r
387 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement\r
388 was started.\r
389 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement\r
390 was ended.\r
391\r
392 @return The key for the next performance log entry (in general case).\r
393\r
394**/\r
395UINTN\r
396EFIAPI\r
397GetPerformanceMeasurement (\r
398 IN UINTN LogEntryKey,\r
399 OUT CONST VOID **Handle,\r
400 OUT CONST CHAR8 **Token,\r
401 OUT CONST CHAR8 **Module,\r
402 OUT UINT64 *StartTimeStamp,\r
403 OUT UINT64 *EndTimeStamp\r
404 )\r
405{\r
406 UINT32 Identifier;\r
407 return GetPerformanceMeasurementEx (LogEntryKey, Handle, Token, Module, StartTimeStamp, EndTimeStamp, &Identifier);\r
408}\r
409\r
a0afd019 410/**\r
411 Returns TRUE if the performance measurement macros are enabled.\r
412\r
413 This function returns TRUE if the PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of\r
414 PcdPerformanceLibraryPropertyMask is set. Otherwise FALSE is returned.\r
415\r
416 @retval TRUE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of\r
417 PcdPerformanceLibraryPropertyMask is set.\r
418 @retval FALSE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of\r
419 PcdPerformanceLibraryPropertyMask is clear.\r
420\r
421**/\r
422BOOLEAN\r
423EFIAPI\r
424PerformanceMeasurementEnabled (\r
425 VOID\r
426 )\r
427{\r
428 return (BOOLEAN) ((PcdGet8(PcdPerformanceLibraryPropertyMask) & PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED) != 0);\r
429}\r