]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Library/DxeCorePerformanceLib/DxeCorePerformanceLib.c
MdeModulePkg: Remove NetLib and Ip4Config Protocol dependency.
[mirror_edk2.git] / MdeModulePkg / Library / DxeCorePerformanceLib / DxeCorePerformanceLib.c
CommitLineData
8dbae30d 1/** @file\r
857dfc45 2 Performance library instance mainly used by DxeCore.\r
3\r
4 This library provides the performance measurement interfaces and initializes performance\r
5 logging for DXE phase. It first initializes its private global data structure for\r
6 performance logging and saves the performance GUIDed HOB passed from PEI phase. \r
f0da4d7d
SZ
7 It initializes DXE phase performance logging by publishing the Performance and PerformanceEx Protocol,\r
8 which are consumed by DxePerformanceLib to logging performance data in DXE phase.\r
857dfc45 9\r
10 This library is mainly used by DxeCore to start performance logging to ensure that\r
11 Performance Protocol is installed at the very beginning of DXE phase.\r
a0afd019 12\r
96f983fa 13Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.<BR>\r
cd5ebaa0 14This program and the accompanying materials\r
a0afd019 15are licensed and made available under the terms and conditions of the BSD License\r
16which accompanies this distribution. The full text of the license may be found at\r
17http://opensource.org/licenses/bsd-license.php\r
18\r
19THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
20WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
21\r
8dbae30d 22**/\r
a0afd019 23\r
ed7748fe 24\r
aa79b0b3 25#include "DxeCorePerformanceLibInternal.h"\r
ed7748fe 26\r
a0afd019 27\r
28//\r
857dfc45 29// The data structure to hold global performance data.\r
a0afd019 30//\r
fe1e36e5 31GAUGE_DATA_HEADER *mGaugeData;\r
857dfc45 32\r
33//\r
34// The current maximum number of logging entries. If current number of \r
35// entries exceeds this value, it will re-allocate a larger array and\r
36// migration the old data to the larger array.\r
37//\r
fe1e36e5 38UINT32 mMaxGaugeRecords;\r
a0afd019 39\r
857dfc45 40//\r
41// The handle to install Performance Protocol instance.\r
42//\r
a0afd019 43EFI_HANDLE mHandle = NULL;\r
857dfc45 44\r
45//\r
f0da4d7d 46// Interfaces for Performance Protocol.\r
857dfc45 47//\r
a0afd019 48PERFORMANCE_PROTOCOL mPerformanceInterface = {\r
49 StartGauge,\r
50 EndGauge,\r
51 GetGauge\r
52 };\r
53\r
f0da4d7d
SZ
54//\r
55// Interfaces for PerformanceEx Protocol.\r
56//\r
57PERFORMANCE_EX_PROTOCOL mPerformanceExInterface = {\r
58 StartGaugeEx,\r
59 EndGaugeEx,\r
60 GetGaugeEx\r
61 };\r
62\r
a0afd019 63/**\r
f0da4d7d 64 Searches in the gauge array with keyword Handle, Token, Module and Identifier.\r
a0afd019 65\r
66 This internal function searches for the gauge entry in the gauge array.\r
f0da4d7d 67 If there is an entry that exactly matches the given keywords\r
a0afd019 68 and its end time stamp is zero, then the index of that gauge entry is returned;\r
69 otherwise, the the number of gauge entries in the array is returned.\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
f0da4d7d 77 @param Identifier 32-bit identifier.\r
a0afd019 78\r
79 @retval The index of gauge entry in the array.\r
80\r
81**/\r
a0afd019 82UINT32\r
83InternalSearchForGaugeEntry (\r
84 IN CONST VOID *Handle, OPTIONAL\r
85 IN CONST CHAR8 *Token, OPTIONAL\r
f0da4d7d
SZ
86 IN CONST CHAR8 *Module, OPTIONAL\r
87 IN UINT32 Identifier\r
a0afd019 88 )\r
89{\r
90 UINT32 Index;\r
b504f519 91 UINT32 Index2;\r
a0afd019 92 UINT32 NumberOfEntries;\r
f0da4d7d 93 GAUGE_DATA_ENTRY_EX *GaugeEntryExArray;\r
a0afd019 94\r
95 if (Token == NULL) {\r
96 Token = "";\r
97 }\r
98 if (Module == NULL) {\r
99 Module = "";\r
100 }\r
101\r
102 NumberOfEntries = mGaugeData->NumberOfEntries;\r
f0da4d7d 103 GaugeEntryExArray = (GAUGE_DATA_ENTRY_EX *) (mGaugeData + 1);\r
a0afd019 104\r
b504f519
SZ
105 Index2 = 0;\r
106\r
a0afd019 107 for (Index = 0; Index < NumberOfEntries; Index++) {\r
b504f519
SZ
108 Index2 = NumberOfEntries - 1 - Index;\r
109 if (GaugeEntryExArray[Index2].EndTimeStamp == 0 &&\r
110 (GaugeEntryExArray[Index2].Handle == (EFI_PHYSICAL_ADDRESS) (UINTN) Handle) &&\r
111 AsciiStrnCmp (GaugeEntryExArray[Index2].Token, Token, DXE_PERFORMANCE_STRING_LENGTH) == 0 &&\r
112 AsciiStrnCmp (GaugeEntryExArray[Index2].Module, Module, DXE_PERFORMANCE_STRING_LENGTH) == 0 &&\r
113 (GaugeEntryExArray[Index2].Identifier == Identifier)) {\r
114 Index = Index2;\r
a0afd019 115 break;\r
116 }\r
117 }\r
118\r
119 return Index;\r
120}\r
121\r
122/**\r
123 Adds a record at the end of the performance measurement log\r
124 that records the start time of a performance measurement.\r
125\r
126 Adds a record to the end of the performance measurement log\r
f0da4d7d 127 that contains the Handle, Token, Module and Identifier.\r
a0afd019 128 The end time of the new record must be set to zero.\r
129 If TimeStamp is not zero, then TimeStamp is used to fill in the start time in the record.\r
130 If TimeStamp is zero, the start time in the record is filled in with the value\r
131 read from the current time stamp.\r
132\r
133 @param Handle Pointer to environment specific context used\r
134 to identify the component being measured.\r
135 @param Token Pointer to a Null-terminated ASCII string\r
136 that identifies the component being measured.\r
137 @param Module Pointer to a Null-terminated ASCII string\r
138 that identifies the module being measured.\r
139 @param TimeStamp 64-bit time stamp.\r
f0da4d7d
SZ
140 @param Identifier 32-bit identifier. If the value is 0, the created record\r
141 is same as the one created by StartGauge of PERFORMANCE_PROTOCOL.\r
a0afd019 142\r
143 @retval EFI_SUCCESS The data was read correctly from the device.\r
144 @retval EFI_OUT_OF_RESOURCES There are not enough resources to record the measurement.\r
145\r
146**/\r
a0afd019 147EFI_STATUS\r
148EFIAPI\r
f0da4d7d 149StartGaugeEx (\r
a0afd019 150 IN CONST VOID *Handle, OPTIONAL\r
151 IN CONST CHAR8 *Token, OPTIONAL\r
152 IN CONST CHAR8 *Module, OPTIONAL\r
f0da4d7d
SZ
153 IN UINT64 TimeStamp,\r
154 IN UINT32 Identifier\r
a0afd019 155 )\r
156{\r
f0da4d7d 157 GAUGE_DATA_ENTRY_EX *GaugeEntryExArray;\r
a0afd019 158 UINTN GaugeDataSize;\r
f0da4d7d 159 GAUGE_DATA_HEADER *NewGaugeData;\r
a0afd019 160 UINTN OldGaugeDataSize;\r
161 GAUGE_DATA_HEADER *OldGaugeData;\r
162 UINT32 Index;\r
163\r
164 Index = mGaugeData->NumberOfEntries;\r
165 if (Index >= mMaxGaugeRecords) {\r
166 //\r
857dfc45 167 // Try to enlarge the scale of gauge array.\r
a0afd019 168 //\r
169 OldGaugeData = mGaugeData;\r
f0da4d7d 170 OldGaugeDataSize = sizeof (GAUGE_DATA_HEADER) + sizeof (GAUGE_DATA_ENTRY_EX) * mMaxGaugeRecords;\r
a0afd019 171\r
f0da4d7d 172 GaugeDataSize = sizeof (GAUGE_DATA_HEADER) + sizeof (GAUGE_DATA_ENTRY_EX) * mMaxGaugeRecords * 2;\r
a0afd019 173\r
f0da4d7d
SZ
174 NewGaugeData = AllocateZeroPool (GaugeDataSize);\r
175 if (NewGaugeData == NULL) {\r
a0afd019 176 return EFI_OUT_OF_RESOURCES;\r
177 }\r
f0da4d7d
SZ
178\r
179 mGaugeData = NewGaugeData;\r
180 mMaxGaugeRecords *= 2;\r
181\r
a0afd019 182 //\r
857dfc45 183 // Initialize new data array and migrate old data one.\r
a0afd019 184 //\r
185 mGaugeData = CopyMem (mGaugeData, OldGaugeData, OldGaugeDataSize);\r
186\r
187 FreePool (OldGaugeData);\r
188 }\r
189\r
f0da4d7d
SZ
190 GaugeEntryExArray = (GAUGE_DATA_ENTRY_EX *) (mGaugeData + 1);\r
191 GaugeEntryExArray[Index].Handle = (EFI_PHYSICAL_ADDRESS) (UINTN) Handle;\r
a0afd019 192\r
193 if (Token != NULL) {\r
0ec2bfc8 194 AsciiStrnCpyS (GaugeEntryExArray[Index].Token, DXE_PERFORMANCE_STRING_SIZE, Token, DXE_PERFORMANCE_STRING_LENGTH);\r
a0afd019 195 }\r
196 if (Module != NULL) {\r
0ec2bfc8 197 AsciiStrnCpyS (GaugeEntryExArray[Index].Module, DXE_PERFORMANCE_STRING_SIZE, Module, DXE_PERFORMANCE_STRING_LENGTH);\r
a0afd019 198 }\r
199\r
f0da4d7d
SZ
200 GaugeEntryExArray[Index].EndTimeStamp = 0;\r
201 GaugeEntryExArray[Index].Identifier = Identifier;\r
202\r
a0afd019 203 if (TimeStamp == 0) {\r
204 TimeStamp = GetPerformanceCounter ();\r
205 }\r
f0da4d7d 206 GaugeEntryExArray[Index].StartTimeStamp = TimeStamp;\r
a0afd019 207\r
208 mGaugeData->NumberOfEntries++;\r
209\r
210 return EFI_SUCCESS;\r
211}\r
212\r
213/**\r
214 Searches the performance measurement log from the beginning of the log\r
215 for the first matching record that contains a zero end time and fills in a valid end time.\r
216\r
217 Searches the performance measurement log from the beginning of the log\r
f0da4d7d 218 for the first record that matches Handle, Token, Module and Identifier and has an end time value of zero.\r
a0afd019 219 If the record can not be found then return EFI_NOT_FOUND.\r
220 If the record is found and TimeStamp is not zero,\r
221 then the end time in the record is filled in with the value specified by TimeStamp.\r
222 If the record is found and TimeStamp is zero, then the end time in the matching record\r
223 is filled in with the current time stamp value.\r
224\r
225 @param Handle Pointer to environment specific context used\r
226 to identify the component being measured.\r
227 @param Token Pointer to a Null-terminated ASCII string\r
228 that identifies the component being measured.\r
229 @param Module Pointer to a Null-terminated ASCII string\r
230 that identifies the module being measured.\r
231 @param TimeStamp 64-bit time stamp.\r
f0da4d7d
SZ
232 @param Identifier 32-bit identifier. If the value is 0, the found record\r
233 is same as the one found by EndGauge of PERFORMANCE_PROTOCOL.\r
a0afd019 234\r
235 @retval EFI_SUCCESS The end of the measurement was recorded.\r
236 @retval EFI_NOT_FOUND The specified measurement record could not be found.\r
237\r
238**/\r
a0afd019 239EFI_STATUS\r
240EFIAPI\r
f0da4d7d 241EndGaugeEx (\r
a0afd019 242 IN CONST VOID *Handle, OPTIONAL\r
243 IN CONST CHAR8 *Token, OPTIONAL\r
244 IN CONST CHAR8 *Module, OPTIONAL\r
f0da4d7d
SZ
245 IN UINT64 TimeStamp,\r
246 IN UINT32 Identifier\r
a0afd019 247 )\r
248{\r
f0da4d7d
SZ
249 GAUGE_DATA_ENTRY_EX *GaugeEntryExArray;\r
250 UINT32 Index;\r
a0afd019 251\r
252 if (TimeStamp == 0) {\r
253 TimeStamp = GetPerformanceCounter ();\r
254 }\r
255\r
f0da4d7d 256 Index = InternalSearchForGaugeEntry (Handle, Token, Module, Identifier);\r
a0afd019 257 if (Index >= mGaugeData->NumberOfEntries) {\r
258 return EFI_NOT_FOUND;\r
259 }\r
f0da4d7d
SZ
260 GaugeEntryExArray = (GAUGE_DATA_ENTRY_EX *) (mGaugeData + 1);\r
261 GaugeEntryExArray[Index].EndTimeStamp = TimeStamp;\r
a0afd019 262\r
263 return EFI_SUCCESS;\r
264}\r
265\r
266/**\r
267 Retrieves a previously logged performance measurement.\r
f0da4d7d
SZ
268 It can also retrieve the log created by StartGauge and EndGauge of PERFORMANCE_PROTOCOL,\r
269 and then assign the Identifier with 0.\r
a0afd019 270\r
271 Retrieves the performance log entry from the performance log specified by LogEntryKey.\r
272 If it stands for a valid entry, then EFI_SUCCESS is returned and\r
f0da4d7d 273 GaugeDataEntryEx stores the pointer to that entry.\r
a0afd019 274\r
275 @param LogEntryKey The key for the previous performance measurement log entry.\r
276 If 0, then the first performance measurement log entry is retrieved.\r
f0da4d7d 277 @param GaugeDataEntryEx The indirect pointer to the extended gauge data entry specified by LogEntryKey\r
a0afd019 278 if the retrieval is successful.\r
279\r
f0da4d7d 280 @retval EFI_SUCCESS The GuageDataEntryEx is successfully found based on LogEntryKey.\r
a0afd019 281 @retval EFI_NOT_FOUND The LogEntryKey is the last entry (equals to the total entry number).\r
282 @retval EFI_INVALIDE_PARAMETER The LogEntryKey is not a valid entry (greater than the total entry number).\r
f0da4d7d 283 @retval EFI_INVALIDE_PARAMETER GaugeDataEntryEx is NULL.\r
a0afd019 284\r
285**/\r
a0afd019 286EFI_STATUS\r
287EFIAPI\r
f0da4d7d
SZ
288GetGaugeEx (\r
289 IN UINTN LogEntryKey,\r
290 OUT GAUGE_DATA_ENTRY_EX **GaugeDataEntryEx\r
a0afd019 291 )\r
292{\r
293 UINTN NumberOfEntries;\r
f0da4d7d 294 GAUGE_DATA_ENTRY_EX *GaugeEntryExArray;\r
a0afd019 295\r
296 NumberOfEntries = (UINTN) (mGaugeData->NumberOfEntries);\r
297 if (LogEntryKey > NumberOfEntries) {\r
298 return EFI_INVALID_PARAMETER;\r
299 }\r
300 if (LogEntryKey == NumberOfEntries) {\r
301 return EFI_NOT_FOUND;\r
302 }\r
303\r
f0da4d7d
SZ
304 GaugeEntryExArray = (GAUGE_DATA_ENTRY_EX *) (mGaugeData + 1);\r
305\r
306 if (GaugeDataEntryEx == NULL) {\r
307 return EFI_INVALID_PARAMETER;\r
308 }\r
309 *GaugeDataEntryEx = &GaugeEntryExArray[LogEntryKey];\r
310\r
311 return EFI_SUCCESS;\r
312}\r
313\r
314/**\r
315 Adds a record at the end of the performance measurement log\r
316 that records the start time of a performance measurement.\r
317\r
318 Adds a record to the end of the performance measurement log\r
319 that contains the Handle, Token, and Module.\r
320 The end time of the new record must be set to zero.\r
321 If TimeStamp is not zero, then TimeStamp is used to fill in the start time in the record.\r
322 If TimeStamp is zero, the start time in the record is filled in with the value\r
323 read from the current time stamp.\r
324\r
325 @param Handle Pointer to environment specific context used\r
326 to identify the component being measured.\r
327 @param Token Pointer to a Null-terminated ASCII string\r
328 that identifies the component being measured.\r
329 @param Module Pointer to a Null-terminated ASCII string\r
330 that identifies the module being measured.\r
331 @param TimeStamp 64-bit time stamp.\r
332\r
333 @retval EFI_SUCCESS The data was read correctly from the device.\r
334 @retval EFI_OUT_OF_RESOURCES There are not enough resources to record the measurement.\r
335\r
336**/\r
337EFI_STATUS\r
338EFIAPI\r
339StartGauge (\r
340 IN CONST VOID *Handle, OPTIONAL\r
341 IN CONST CHAR8 *Token, OPTIONAL\r
342 IN CONST CHAR8 *Module, OPTIONAL\r
343 IN UINT64 TimeStamp\r
344 )\r
345{\r
346 return StartGaugeEx (Handle, Token, Module, TimeStamp, 0);\r
347}\r
348\r
349/**\r
350 Searches the performance measurement log from the beginning of the log\r
351 for the first matching record that contains a zero end time and fills in a valid end time.\r
352\r
353 Searches the performance measurement log from the beginning of the log\r
354 for the first record that matches Handle, Token, and Module and has an end time value of zero.\r
355 If the record can not be found then return EFI_NOT_FOUND.\r
356 If the record is found and TimeStamp is not zero,\r
357 then the end time in the record is filled in with the value specified by TimeStamp.\r
358 If the record is found and TimeStamp is zero, then the end time in the matching record\r
359 is filled in with the current time stamp value.\r
360\r
361 @param Handle Pointer to environment specific context used\r
362 to identify the component being measured.\r
363 @param Token Pointer to a Null-terminated ASCII string\r
364 that identifies the component being measured.\r
365 @param Module Pointer to a Null-terminated ASCII string\r
366 that identifies the module being measured.\r
367 @param TimeStamp 64-bit time stamp.\r
368\r
369 @retval EFI_SUCCESS The end of the measurement was recorded.\r
370 @retval EFI_NOT_FOUND The specified measurement record could not be found.\r
371\r
372**/\r
373EFI_STATUS\r
374EFIAPI\r
375EndGauge (\r
376 IN CONST VOID *Handle, OPTIONAL\r
377 IN CONST CHAR8 *Token, OPTIONAL\r
378 IN CONST CHAR8 *Module, OPTIONAL\r
379 IN UINT64 TimeStamp\r
380 )\r
381{\r
382 return EndGaugeEx (Handle, Token, Module, TimeStamp, 0);\r
383}\r
384\r
385/**\r
386 Retrieves a previously logged performance measurement.\r
387 It can also retrieve the log created by StartGaugeEx and EndGaugeEx of PERFORMANCE_EX_PROTOCOL,\r
388 and then eliminate the Identifier.\r
389\r
390 Retrieves the performance log entry from the performance log specified by LogEntryKey.\r
391 If it stands for a valid entry, then EFI_SUCCESS is returned and\r
392 GaugeDataEntry stores the pointer to that entry.\r
393\r
394 @param LogEntryKey The key for the previous performance measurement log entry.\r
395 If 0, then the first performance measurement log entry is retrieved.\r
396 @param GaugeDataEntry The indirect pointer to the gauge data entry specified by LogEntryKey\r
397 if the retrieval is successful.\r
398\r
399 @retval EFI_SUCCESS The GuageDataEntry is successfully found based on LogEntryKey.\r
400 @retval EFI_NOT_FOUND The LogEntryKey is the last entry (equals to the total entry number).\r
401 @retval EFI_INVALIDE_PARAMETER The LogEntryKey is not a valid entry (greater than the total entry number).\r
402 @retval EFI_INVALIDE_PARAMETER GaugeDataEntry is NULL.\r
403\r
404**/\r
405EFI_STATUS\r
406EFIAPI\r
407GetGauge (\r
408 IN UINTN LogEntryKey,\r
409 OUT GAUGE_DATA_ENTRY **GaugeDataEntry\r
410 )\r
411{\r
412 EFI_STATUS Status;\r
413 GAUGE_DATA_ENTRY_EX *GaugeEntryEx;\r
414\r
1658440e
SZ
415 GaugeEntryEx = NULL;\r
416\r
f0da4d7d
SZ
417 Status = GetGaugeEx (LogEntryKey, &GaugeEntryEx);\r
418 if (EFI_ERROR (Status)) {\r
419 return Status;\r
420 }\r
a0afd019 421\r
422 if (GaugeDataEntry == NULL) {\r
423 return EFI_INVALID_PARAMETER;\r
424 }\r
f0da4d7d
SZ
425\r
426 *GaugeDataEntry = (GAUGE_DATA_ENTRY *) GaugeEntryEx;\r
a0afd019 427\r
428 return EFI_SUCCESS;\r
429}\r
430\r
431/**\r
432 Dumps all the PEI performance log to DXE performance gauge array.\r
433\r
434 This internal function dumps all the PEI performance log to the DXE performance gauge array.\r
435 It retrieves the optional GUID HOB for PEI performance and then saves the performance data\r
436 to DXE performance data structures.\r
437\r
438**/\r
a0afd019 439VOID\r
440InternalGetPeiPerformance (\r
441 VOID\r
442 )\r
443{\r
444 EFI_HOB_GUID_TYPE *GuidHob;\r
445 PEI_PERFORMANCE_LOG_HEADER *LogHob;\r
446 PEI_PERFORMANCE_LOG_ENTRY *LogEntryArray;\r
f0da4d7d
SZ
447 UINT32 *LogIdArray;\r
448 GAUGE_DATA_ENTRY_EX *GaugeEntryExArray;\r
a0afd019 449 UINT32 Index;\r
450 UINT32 NumberOfEntries;\r
451\r
452 NumberOfEntries = 0;\r
f0da4d7d 453 GaugeEntryExArray = (GAUGE_DATA_ENTRY_EX *) (mGaugeData + 1);\r
a0afd019 454\r
455 //\r
456 // Dump PEI Log Entries to DXE Guage Data structure.\r
457 //\r
ee0961f7 458 GuidHob = GetFirstGuidHob (&gPerformanceProtocolGuid);\r
a0afd019 459 if (GuidHob != NULL) {\r
460 LogHob = GET_GUID_HOB_DATA (GuidHob);\r
461 LogEntryArray = (PEI_PERFORMANCE_LOG_ENTRY *) (LogHob + 1);\r
a0afd019 462\r
463 NumberOfEntries = LogHob->NumberOfEntries;\r
464 for (Index = 0; Index < NumberOfEntries; Index++) {\r
f0da4d7d 465 GaugeEntryExArray[Index].Handle = LogEntryArray[Index].Handle;\r
96f983fa
QS
466 AsciiStrCpyS (GaugeEntryExArray[Index].Token, DXE_PERFORMANCE_STRING_SIZE, LogEntryArray[Index].Token);\r
467 AsciiStrCpyS (GaugeEntryExArray[Index].Module, DXE_PERFORMANCE_STRING_SIZE, LogEntryArray[Index].Module);\r
f0da4d7d
SZ
468 GaugeEntryExArray[Index].StartTimeStamp = LogEntryArray[Index].StartTimeStamp;\r
469 GaugeEntryExArray[Index].EndTimeStamp = LogEntryArray[Index].EndTimeStamp;\r
470 GaugeEntryExArray[Index].Identifier = 0;\r
471 }\r
472\r
473 GuidHob = GetFirstGuidHob (&gPerformanceExProtocolGuid);\r
474 if (GuidHob != NULL) {\r
475 LogIdArray = GET_GUID_HOB_DATA (GuidHob);\r
476 for (Index = 0; Index < NumberOfEntries; Index++) {\r
477 GaugeEntryExArray[Index].Identifier = LogIdArray[Index];\r
478 }\r
a0afd019 479 }\r
480 }\r
481 mGaugeData->NumberOfEntries = NumberOfEntries;\r
482}\r
483\r
484/**\r
485 The constructor function initializes Performance infrastructure for DXE phase.\r
486\r
f0da4d7d 487 The constructor function publishes Performance and PerformanceEx protocol, allocates memory to log DXE performance\r
a0afd019 488 and merges PEI performance data to DXE performance log.\r
489 It will ASSERT() if one of these operations fails and it will always return EFI_SUCCESS.\r
490\r
491 @param ImageHandle The firmware allocated handle for the EFI image.\r
492 @param SystemTable A pointer to the EFI System Table.\r
493\r
494 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.\r
495\r
496**/\r
497EFI_STATUS\r
498EFIAPI\r
499DxeCorePerformanceLibConstructor (\r
500 IN EFI_HANDLE ImageHandle,\r
501 IN EFI_SYSTEM_TABLE *SystemTable\r
502 )\r
503{\r
504 EFI_STATUS Status;\r
505\r
506 if (!PerformanceMeasurementEnabled ()) {\r
507 //\r
508 // Do not initialize performance infrastructure if not required.\r
509 //\r
510 return EFI_SUCCESS;\r
511 }\r
512 //\r
513 // Install the protocol interfaces.\r
514 //\r
f0da4d7d 515 Status = gBS->InstallMultipleProtocolInterfaces (\r
a0afd019 516 &mHandle,\r
517 &gPerformanceProtocolGuid,\r
f0da4d7d
SZ
518 &mPerformanceInterface,\r
519 &gPerformanceExProtocolGuid,\r
520 &mPerformanceExInterface,\r
521 NULL\r
a0afd019 522 );\r
523 ASSERT_EFI_ERROR (Status);\r
524\r
525 mMaxGaugeRecords = INIT_DXE_GAUGE_DATA_ENTRIES + PcdGet8 (PcdMaxPeiPerformanceLogEntries);\r
526\r
f0da4d7d 527 mGaugeData = AllocateZeroPool (sizeof (GAUGE_DATA_HEADER) + (sizeof (GAUGE_DATA_ENTRY_EX) * mMaxGaugeRecords));\r
a0afd019 528 ASSERT (mGaugeData != NULL);\r
529\r
530 InternalGetPeiPerformance ();\r
531\r
532 return Status;\r
533}\r
534\r
535/**\r
536 Adds a record at the end of the performance measurement log\r
537 that records the start time of a performance measurement.\r
538\r
539 Adds a record to the end of the performance measurement log\r
f0da4d7d 540 that contains the Handle, Token, Module and Identifier.\r
a0afd019 541 The end time of the new record must be set to zero.\r
542 If TimeStamp is not zero, then TimeStamp is used to fill in the start time in the record.\r
543 If TimeStamp is zero, the start time in the record is filled in with the value\r
544 read from the current time stamp.\r
545\r
546 @param Handle Pointer to environment specific context used\r
547 to identify the component being measured.\r
548 @param Token Pointer to a Null-terminated ASCII string\r
549 that identifies the component being measured.\r
550 @param Module Pointer to a Null-terminated ASCII string\r
551 that identifies the module being measured.\r
552 @param TimeStamp 64-bit time stamp.\r
f0da4d7d
SZ
553 @param Identifier 32-bit identifier. If the value is 0, the created record\r
554 is same as the one created by StartPerformanceMeasurement.\r
a0afd019 555\r
556 @retval RETURN_SUCCESS The start of the measurement was recorded.\r
557 @retval RETURN_OUT_OF_RESOURCES There are not enough resources to record the measurement.\r
558\r
559**/\r
560RETURN_STATUS\r
561EFIAPI\r
f0da4d7d 562StartPerformanceMeasurementEx (\r
a0afd019 563 IN CONST VOID *Handle, OPTIONAL\r
564 IN CONST CHAR8 *Token, OPTIONAL\r
565 IN CONST CHAR8 *Module, OPTIONAL\r
f0da4d7d
SZ
566 IN UINT64 TimeStamp,\r
567 IN UINT32 Identifier\r
a0afd019 568 )\r
569{\r
f0da4d7d 570 return (RETURN_STATUS) StartGaugeEx (Handle, Token, Module, TimeStamp, Identifier);\r
a0afd019 571}\r
572\r
573/**\r
574 Searches the performance measurement log from the beginning of the log\r
575 for the first matching record that contains a zero end time and fills in a valid end time.\r
576\r
577 Searches the performance measurement log from the beginning of the log\r
f0da4d7d 578 for the first record that matches Handle, Token, Module and Identifier and has an end time value of zero.\r
a0afd019 579 If the record can not be found then return RETURN_NOT_FOUND.\r
580 If the record is found and TimeStamp is not zero,\r
581 then the end time in the record is filled in with the value specified by TimeStamp.\r
582 If the record is found and TimeStamp is zero, then the end time in the matching record\r
583 is filled in with the current time stamp value.\r
584\r
585 @param Handle Pointer to environment specific context used\r
586 to identify the component being measured.\r
587 @param Token Pointer to a Null-terminated ASCII string\r
588 that identifies the component being measured.\r
589 @param Module Pointer to a Null-terminated ASCII string\r
590 that identifies the module being measured.\r
591 @param TimeStamp 64-bit time stamp.\r
f0da4d7d
SZ
592 @param Identifier 32-bit identifier. If the value is 0, the found record\r
593 is same as the one found by EndPerformanceMeasurement.\r
a0afd019 594\r
595 @retval RETURN_SUCCESS The end of the measurement was recorded.\r
596 @retval RETURN_NOT_FOUND The specified measurement record could not be found.\r
597\r
598**/\r
599RETURN_STATUS\r
600EFIAPI\r
f0da4d7d 601EndPerformanceMeasurementEx (\r
a0afd019 602 IN CONST VOID *Handle, OPTIONAL\r
603 IN CONST CHAR8 *Token, OPTIONAL\r
604 IN CONST CHAR8 *Module, OPTIONAL\r
f0da4d7d
SZ
605 IN UINT64 TimeStamp,\r
606 IN UINT32 Identifier\r
a0afd019 607 )\r
608{\r
f0da4d7d 609 return (RETURN_STATUS) EndGaugeEx (Handle, Token, Module, TimeStamp, Identifier);\r
a0afd019 610}\r
611\r
612/**\r
613 Attempts to retrieve a performance measurement log entry from the performance measurement log.\r
f0da4d7d
SZ
614 It can also retrieve the log created by StartPerformanceMeasurement and EndPerformanceMeasurement,\r
615 and then assign the Identifier with 0.\r
a0afd019 616\r
617 Attempts to retrieve the performance log entry specified by LogEntryKey. If LogEntryKey is\r
618 zero on entry, then an attempt is made to retrieve the first entry from the performance log,\r
619 and the key for the second entry in the log is returned. If the performance log is empty,\r
620 then no entry is retrieved and zero is returned. If LogEntryKey is not zero, then the performance\r
621 log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is\r
622 returned. If LogEntryKey is the key for the last entry in the log, then the last log entry is\r
623 retrieved and an implementation specific non-zero key value that specifies the end of the performance\r
624 log is returned. If LogEntryKey is equal this implementation specific non-zero key value, then no entry\r
625 is retrieved and zero is returned. In the cases where a performance log entry can be returned,\r
f0da4d7d 626 the log entry is returned in Handle, Token, Module, StartTimeStamp, EndTimeStamp and Identifier.\r
a0afd019 627 If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().\r
628 If Handle is NULL, then ASSERT().\r
629 If Token is NULL, then ASSERT().\r
630 If Module is NULL, then ASSERT().\r
631 If StartTimeStamp is NULL, then ASSERT().\r
632 If EndTimeStamp is NULL, then ASSERT().\r
f0da4d7d 633 If Identifier is NULL, then ASSERT().\r
a0afd019 634\r
635 @param LogEntryKey On entry, the key of the performance measurement log entry to retrieve.\r
636 0, then the first performance measurement log entry is retrieved.\r
857dfc45 637 On exit, the key of the next performance log entry.\r
a0afd019 638 @param Handle Pointer to environment specific context used to identify the component\r
639 being measured.\r
640 @param Token Pointer to a Null-terminated ASCII string that identifies the component\r
641 being measured.\r
642 @param Module Pointer to a Null-terminated ASCII string that identifies the module\r
643 being measured.\r
644 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement\r
645 was started.\r
646 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement\r
647 was ended.\r
f0da4d7d 648 @param Identifier Pointer to the 32-bit identifier that was recorded.\r
a0afd019 649\r
650 @return The key for the next performance log entry (in general case).\r
651\r
652**/\r
653UINTN\r
654EFIAPI\r
f0da4d7d
SZ
655GetPerformanceMeasurementEx (\r
656 IN UINTN LogEntryKey, \r
a0afd019 657 OUT CONST VOID **Handle,\r
658 OUT CONST CHAR8 **Token,\r
659 OUT CONST CHAR8 **Module,\r
660 OUT UINT64 *StartTimeStamp,\r
f0da4d7d
SZ
661 OUT UINT64 *EndTimeStamp,\r
662 OUT UINT32 *Identifier\r
a0afd019 663 )\r
664{\r
f0da4d7d
SZ
665 EFI_STATUS Status;\r
666 GAUGE_DATA_ENTRY_EX *GaugeData;\r
a0afd019 667\r
952671b3 668 GaugeData = NULL;\r
669 \r
a0afd019 670 ASSERT (Handle != NULL);\r
671 ASSERT (Token != NULL);\r
672 ASSERT (Module != NULL);\r
673 ASSERT (StartTimeStamp != NULL);\r
674 ASSERT (EndTimeStamp != NULL);\r
f0da4d7d 675 ASSERT (Identifier != NULL);\r
a0afd019 676\r
f0da4d7d 677 Status = GetGaugeEx (LogEntryKey++, &GaugeData);\r
a0afd019 678\r
679 //\r
680 // Make sure that LogEntryKey is a valid log entry key,\r
681 //\r
682 ASSERT (Status != EFI_INVALID_PARAMETER);\r
683\r
684 if (EFI_ERROR (Status)) {\r
685 //\r
686 // The LogEntryKey is the last entry (equals to the total entry number).\r
687 //\r
688 return 0;\r
689 }\r
690\r
691 ASSERT (GaugeData != NULL);\r
692\r
693 *Handle = (VOID *) (UINTN) GaugeData->Handle;\r
694 *Token = GaugeData->Token;\r
695 *Module = GaugeData->Module;\r
696 *StartTimeStamp = GaugeData->StartTimeStamp;\r
697 *EndTimeStamp = GaugeData->EndTimeStamp;\r
f0da4d7d 698 *Identifier = GaugeData->Identifier;\r
a0afd019 699\r
700 return LogEntryKey;\r
701}\r
702\r
f0da4d7d
SZ
703/**\r
704 Adds a record at the end of the performance measurement log\r
705 that records the start time of a performance measurement.\r
706\r
707 Adds a record to the end of the performance measurement log\r
708 that contains the Handle, Token, and Module.\r
709 The end time of the new record must be set to zero.\r
710 If TimeStamp is not zero, then TimeStamp is used to fill in the start time in the record.\r
711 If TimeStamp is zero, the start time in the record is filled in with the value\r
712 read from the current time stamp.\r
713\r
714 @param Handle Pointer to environment specific context used\r
715 to identify the component being measured.\r
716 @param Token Pointer to a Null-terminated ASCII string\r
717 that identifies the component being measured.\r
718 @param Module Pointer to a Null-terminated ASCII string\r
719 that identifies the module being measured.\r
720 @param TimeStamp 64-bit time stamp.\r
721\r
722 @retval RETURN_SUCCESS The start of the measurement was recorded.\r
723 @retval RETURN_OUT_OF_RESOURCES There are not enough resources to record the measurement.\r
724\r
725**/\r
726RETURN_STATUS\r
727EFIAPI\r
728StartPerformanceMeasurement (\r
729 IN CONST VOID *Handle, OPTIONAL\r
730 IN CONST CHAR8 *Token, OPTIONAL\r
731 IN CONST CHAR8 *Module, OPTIONAL\r
732 IN UINT64 TimeStamp\r
733 )\r
734{\r
735 return StartPerformanceMeasurementEx (Handle, Token, Module, TimeStamp, 0);\r
736}\r
737\r
738/**\r
739 Searches the performance measurement log from the beginning of the log\r
740 for the first matching record that contains a zero end time and fills in a valid end time.\r
741\r
742 Searches the performance measurement log from the beginning of the log\r
743 for the first record that matches Handle, Token, and Module and has an end time value of zero.\r
744 If the record can not be found then return RETURN_NOT_FOUND.\r
745 If the record is found and TimeStamp is not zero,\r
746 then the end time in the record is filled in with the value specified by TimeStamp.\r
747 If the record is found and TimeStamp is zero, then the end time in the matching record\r
748 is filled in with the current time stamp value.\r
749\r
750 @param Handle Pointer to environment specific context used\r
751 to identify the component being measured.\r
752 @param Token Pointer to a Null-terminated ASCII string\r
753 that identifies the component being measured.\r
754 @param Module Pointer to a Null-terminated ASCII string\r
755 that identifies the module being measured.\r
756 @param TimeStamp 64-bit time stamp.\r
757\r
758 @retval RETURN_SUCCESS The end of the measurement was recorded.\r
759 @retval RETURN_NOT_FOUND The specified measurement record could not be found.\r
760\r
761**/\r
762RETURN_STATUS\r
763EFIAPI\r
764EndPerformanceMeasurement (\r
765 IN CONST VOID *Handle, OPTIONAL\r
766 IN CONST CHAR8 *Token, OPTIONAL\r
767 IN CONST CHAR8 *Module, OPTIONAL\r
768 IN UINT64 TimeStamp\r
769 )\r
770{\r
771 return EndPerformanceMeasurementEx (Handle, Token, Module, TimeStamp, 0);\r
772}\r
773\r
774/**\r
775 Attempts to retrieve a performance measurement log entry from the performance measurement log.\r
776 It can also retrieve the log created by StartPerformanceMeasurementEx and EndPerformanceMeasurementEx,\r
777 and then eliminate the Identifier.\r
778\r
779 Attempts to retrieve the performance log entry specified by LogEntryKey. If LogEntryKey is\r
780 zero on entry, then an attempt is made to retrieve the first entry from the performance log,\r
781 and the key for the second entry in the log is returned. If the performance log is empty,\r
782 then no entry is retrieved and zero is returned. If LogEntryKey is not zero, then the performance\r
783 log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is\r
784 returned. If LogEntryKey is the key for the last entry in the log, then the last log entry is\r
785 retrieved and an implementation specific non-zero key value that specifies the end of the performance\r
786 log is returned. If LogEntryKey is equal this implementation specific non-zero key value, then no entry\r
787 is retrieved and zero is returned. In the cases where a performance log entry can be returned,\r
788 the log entry is returned in Handle, Token, Module, StartTimeStamp, and EndTimeStamp.\r
789 If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().\r
790 If Handle is NULL, then ASSERT().\r
791 If Token is NULL, then ASSERT().\r
792 If Module is NULL, then ASSERT().\r
793 If StartTimeStamp is NULL, then ASSERT().\r
794 If EndTimeStamp is NULL, then ASSERT().\r
795\r
796 @param LogEntryKey On entry, the key of the performance measurement log entry to retrieve.\r
797 0, then the first performance measurement log entry is retrieved.\r
798 On exit, the key of the next performance log entry.\r
799 @param Handle Pointer to environment specific context used to identify the component\r
800 being measured.\r
801 @param Token Pointer to a Null-terminated ASCII string that identifies the component\r
802 being measured.\r
803 @param Module Pointer to a Null-terminated ASCII string that identifies the module\r
804 being measured.\r
805 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement\r
806 was started.\r
807 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement\r
808 was ended.\r
809\r
810 @return The key for the next performance log entry (in general case).\r
811\r
812**/\r
813UINTN\r
814EFIAPI\r
815GetPerformanceMeasurement (\r
816 IN UINTN LogEntryKey,\r
817 OUT CONST VOID **Handle,\r
818 OUT CONST CHAR8 **Token,\r
819 OUT CONST CHAR8 **Module,\r
820 OUT UINT64 *StartTimeStamp,\r
821 OUT UINT64 *EndTimeStamp\r
822 )\r
823{\r
824 UINT32 Identifier;\r
825 return GetPerformanceMeasurementEx (LogEntryKey, Handle, Token, Module, StartTimeStamp, EndTimeStamp, &Identifier);\r
826}\r
827\r
a0afd019 828/**\r
829 Returns TRUE if the performance measurement macros are enabled.\r
830\r
831 This function returns TRUE if the PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of\r
832 PcdPerformanceLibraryPropertyMask is set. Otherwise FALSE is returned.\r
833\r
834 @retval TRUE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of\r
835 PcdPerformanceLibraryPropertyMask is set.\r
836 @retval FALSE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of\r
837 PcdPerformanceLibraryPropertyMask is clear.\r
838\r
839**/\r
840BOOLEAN\r
841EFIAPI\r
842PerformanceMeasurementEnabled (\r
843 VOID\r
844 )\r
845{\r
846 return (BOOLEAN) ((PcdGet8(PcdPerformanceLibraryPropertyMask) & PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED) != 0);\r
847}\r