]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - MdeModulePkg/Library/PeiPerformanceLib/PeiPerformanceLib.c
MdeModulePkg: Add match2 opcode support in SetupBrowserDxe and sample code in DriverS...
[mirror_edk2.git] / MdeModulePkg / Library / PeiPerformanceLib / PeiPerformanceLib.c
... / ...
CommitLineData
1/** @file\r
2 Performance library instance used in PEI phase.\r
3\r
4 This file implements all APIs in Performance Library class in MdePkg. It creates\r
5 performance logging GUIDed HOB on the first performance logging and then logs the\r
6 performance data to the GUIDed HOB. Due to the limitation of temporary RAM, the maximum\r
7 number of performance logging entry is specified by PcdMaxPeiPerformanceLogEntries. \r
8\r
9Copyright (c) 2006 - 2012, Intel Corporation. All rights reserved.<BR>\r
10This program and the accompanying materials\r
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
16WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
17\r
18**/\r
19\r
20\r
21#include <PiPei.h>\r
22\r
23#include <Guid/Performance.h>\r
24\r
25#include <Library/PerformanceLib.h>\r
26#include <Library/DebugLib.h>\r
27#include <Library/HobLib.h>\r
28#include <Library/BaseLib.h>\r
29#include <Library/TimerLib.h>\r
30#include <Library/PcdLib.h>\r
31#include <Library/BaseMemoryLib.h>\r
32\r
33\r
34/**\r
35 Gets the GUID HOB for PEI performance.\r
36\r
37 This internal function searches for the GUID HOB for PEI performance.\r
38 If that GUID HOB is not found, it will build a new one.\r
39 It outputs the data area of that GUID HOB to record performance log.\r
40\r
41 @param PeiPerformanceLog Pointer to Pointer to PEI performance log header.\r
42 @param PeiPerformanceIdArray Pointer to Pointer to PEI performance identifier array.\r
43\r
44**/\r
45VOID\r
46InternalGetPerformanceHobLog (\r
47 OUT PEI_PERFORMANCE_LOG_HEADER **PeiPerformanceLog,\r
48 OUT UINT32 **PeiPerformanceIdArray\r
49 )\r
50{\r
51 EFI_HOB_GUID_TYPE *GuidHob;\r
52 UINTN PeiPerformanceSize;\r
53\r
54 ASSERT (PeiPerformanceLog != NULL);\r
55 ASSERT (PeiPerformanceIdArray != NULL);\r
56\r
57 GuidHob = GetFirstGuidHob (&gPerformanceProtocolGuid);\r
58\r
59 if (GuidHob != NULL) {\r
60 //\r
61 // PEI Performance HOB was found, then return the existing one.\r
62 //\r
63 *PeiPerformanceLog = GET_GUID_HOB_DATA (GuidHob);\r
64\r
65 GuidHob = GetFirstGuidHob (&gPerformanceExProtocolGuid);\r
66 ASSERT (GuidHob != NULL);\r
67 *PeiPerformanceIdArray = GET_GUID_HOB_DATA (GuidHob);\r
68 } else {\r
69 //\r
70 // PEI Performance HOB was not found, then build one.\r
71 //\r
72 PeiPerformanceSize = sizeof (PEI_PERFORMANCE_LOG_HEADER) +\r
73 sizeof (PEI_PERFORMANCE_LOG_ENTRY) * PcdGet8 (PcdMaxPeiPerformanceLogEntries);\r
74 *PeiPerformanceLog = BuildGuidHob (&gPerformanceProtocolGuid, PeiPerformanceSize);\r
75 *PeiPerformanceLog = ZeroMem (*PeiPerformanceLog, PeiPerformanceSize);\r
76\r
77 PeiPerformanceSize = sizeof (UINT32) * PcdGet8 (PcdMaxPeiPerformanceLogEntries);\r
78 *PeiPerformanceIdArray = BuildGuidHob (&gPerformanceExProtocolGuid, PeiPerformanceSize);\r
79 *PeiPerformanceIdArray = ZeroMem (*PeiPerformanceIdArray, PeiPerformanceSize);\r
80 }\r
81}\r
82\r
83/**\r
84 Searches in the log array with keyword Handle, Token, Module and Identifier.\r
85\r
86 This internal function searches for the log entry in the log array.\r
87 If there is an entry that exactly matches the given keywords\r
88 and its end time stamp is zero, then the index of that log entry is returned;\r
89 otherwise, the the number of log entries in the array is returned.\r
90\r
91 @param PeiPerformanceLog Pointer to the data structure containing PEI \r
92 performance data.\r
93 @param PeiPerformanceIdArray Pointer to PEI performance identifier array.\r
94 @param Handle Pointer to environment specific context used\r
95 to identify the component being measured.\r
96 @param Token Pointer to a Null-terminated ASCII string\r
97 that identifies the component being measured.\r
98 @param Module Pointer to a Null-terminated ASCII string\r
99 that identifies the module being measured.\r
100 @param Identifier 32-bit identifier.\r
101\r
102 @retval The index of log entry in the array.\r
103\r
104**/\r
105UINT32\r
106InternalSearchForLogEntry (\r
107 IN PEI_PERFORMANCE_LOG_HEADER *PeiPerformanceLog,\r
108 IN UINT32 *PeiPerformanceIdArray,\r
109 IN CONST VOID *Handle, OPTIONAL\r
110 IN CONST CHAR8 *Token, OPTIONAL\r
111 IN CONST CHAR8 *Module, OPTIONAL\r
112 IN UINT32 Identifier\r
113 )\r
114{\r
115 UINT32 Index;\r
116 UINT32 Index2;\r
117 UINT32 NumberOfEntries;\r
118 PEI_PERFORMANCE_LOG_ENTRY *LogEntryArray;\r
119\r
120\r
121 if (Token == NULL) {\r
122 Token = "";\r
123 }\r
124 if (Module == NULL) {\r
125 Module = "";\r
126 }\r
127 NumberOfEntries = PeiPerformanceLog->NumberOfEntries;\r
128 LogEntryArray = (PEI_PERFORMANCE_LOG_ENTRY *) (PeiPerformanceLog + 1);\r
129\r
130 Index2 = 0;\r
131\r
132 for (Index = 0; Index < NumberOfEntries; Index++) {\r
133 Index2 = NumberOfEntries - 1 - Index;\r
134 if (LogEntryArray[Index2].EndTimeStamp == 0 &&\r
135 (LogEntryArray[Index2].Handle == (EFI_PHYSICAL_ADDRESS) (UINTN) Handle) &&\r
136 AsciiStrnCmp (LogEntryArray[Index2].Token, Token, PEI_PERFORMANCE_STRING_LENGTH) == 0 &&\r
137 AsciiStrnCmp (LogEntryArray[Index2].Module, Module, PEI_PERFORMANCE_STRING_LENGTH) == 0 &&\r
138 (PeiPerformanceIdArray[Index2] == Identifier)) {\r
139 Index = Index2;\r
140 break;\r
141 }\r
142 }\r
143 return Index;\r
144}\r
145\r
146/**\r
147 Creates a record for the beginning of a performance measurement.\r
148\r
149 Creates a record that contains the Handle, Token, Module and Identifier.\r
150 If TimeStamp is not zero, then TimeStamp is added to the record as the start time.\r
151 If TimeStamp is zero, then this function reads the current time stamp\r
152 and adds that time stamp value to the record as the start time.\r
153\r
154 @param Handle Pointer to environment specific context used\r
155 to identify the component being measured.\r
156 @param Token Pointer to a Null-terminated ASCII string\r
157 that identifies the component being measured.\r
158 @param Module Pointer to a Null-terminated ASCII string\r
159 that identifies the module being measured.\r
160 @param TimeStamp 64-bit time stamp.\r
161 @param Identifier 32-bit identifier. If the value is 0, the created record\r
162 is same as the one created by StartPerformanceMeasurement.\r
163\r
164 @retval RETURN_SUCCESS The start of the measurement was recorded.\r
165 @retval RETURN_OUT_OF_RESOURCES There are not enough resources to record the measurement.\r
166\r
167**/\r
168RETURN_STATUS\r
169EFIAPI\r
170StartPerformanceMeasurementEx (\r
171 IN CONST VOID *Handle, OPTIONAL\r
172 IN CONST CHAR8 *Token, OPTIONAL\r
173 IN CONST CHAR8 *Module, OPTIONAL\r
174 IN UINT64 TimeStamp,\r
175 IN UINT32 Identifier\r
176 )\r
177{\r
178 PEI_PERFORMANCE_LOG_HEADER *PeiPerformanceLog;\r
179 UINT32 *PeiPerformanceIdArray;\r
180 PEI_PERFORMANCE_LOG_ENTRY *LogEntryArray;\r
181 UINT32 Index;\r
182\r
183 InternalGetPerformanceHobLog (&PeiPerformanceLog, &PeiPerformanceIdArray);\r
184\r
185 if (PeiPerformanceLog->NumberOfEntries >= PcdGet8 (PcdMaxPeiPerformanceLogEntries)) {\r
186 return RETURN_OUT_OF_RESOURCES;\r
187 }\r
188 Index = PeiPerformanceLog->NumberOfEntries++;\r
189 LogEntryArray = (PEI_PERFORMANCE_LOG_ENTRY *) (PeiPerformanceLog + 1);\r
190 LogEntryArray[Index].Handle = (EFI_PHYSICAL_ADDRESS) (UINTN) Handle;\r
191\r
192 if (Token != NULL) {\r
193 AsciiStrnCpy (LogEntryArray[Index].Token, Token, PEI_PERFORMANCE_STRING_LENGTH);\r
194 }\r
195 if (Module != NULL) {\r
196 AsciiStrnCpy (LogEntryArray[Index].Module, Module, PEI_PERFORMANCE_STRING_LENGTH);\r
197 }\r
198\r
199 LogEntryArray[Index].EndTimeStamp = 0;\r
200 PeiPerformanceIdArray[Index] = Identifier;\r
201\r
202 if (TimeStamp == 0) {\r
203 TimeStamp = GetPerformanceCounter ();\r
204 }\r
205 LogEntryArray[Index].StartTimeStamp = TimeStamp;\r
206\r
207 return RETURN_SUCCESS;\r
208}\r
209\r
210/**\r
211 Fills in the end time of a performance measurement.\r
212\r
213 Looks up the record that matches Handle, Token, Module and Identifier.\r
214 If the record can not be found then return RETURN_NOT_FOUND.\r
215 If the record is found and TimeStamp is not zero,\r
216 then TimeStamp is added to the record as the end time.\r
217 If the record is found and TimeStamp is zero, then this function reads\r
218 the current time stamp and adds that time stamp value to the record as the end time.\r
219\r
220 @param Handle Pointer to environment specific context used\r
221 to identify the component being measured.\r
222 @param Token Pointer to a Null-terminated ASCII string\r
223 that identifies the component being measured.\r
224 @param Module Pointer to a Null-terminated ASCII string\r
225 that identifies the module being measured.\r
226 @param TimeStamp 64-bit time stamp.\r
227 @param Identifier 32-bit identifier. If the value is 0, the found record\r
228 is same as the one found by EndPerformanceMeasurement.\r
229\r
230 @retval RETURN_SUCCESS The end of the measurement was recorded.\r
231 @retval RETURN_NOT_FOUND The specified measurement record could not be found.\r
232\r
233**/\r
234RETURN_STATUS\r
235EFIAPI\r
236EndPerformanceMeasurementEx (\r
237 IN CONST VOID *Handle, OPTIONAL\r
238 IN CONST CHAR8 *Token, OPTIONAL\r
239 IN CONST CHAR8 *Module, OPTIONAL\r
240 IN UINT64 TimeStamp,\r
241 IN UINT32 Identifier\r
242 )\r
243{\r
244 PEI_PERFORMANCE_LOG_HEADER *PeiPerformanceLog;\r
245 UINT32 *PeiPerformanceIdArray;\r
246 PEI_PERFORMANCE_LOG_ENTRY *LogEntryArray;\r
247 UINT32 Index;\r
248\r
249 if (TimeStamp == 0) {\r
250 TimeStamp = GetPerformanceCounter ();\r
251 }\r
252\r
253 InternalGetPerformanceHobLog (&PeiPerformanceLog, &PeiPerformanceIdArray);\r
254 Index = InternalSearchForLogEntry (PeiPerformanceLog, PeiPerformanceIdArray, Handle, Token, Module, Identifier);\r
255 if (Index >= PeiPerformanceLog->NumberOfEntries) {\r
256 return RETURN_NOT_FOUND;\r
257 }\r
258 LogEntryArray = (PEI_PERFORMANCE_LOG_ENTRY *) (PeiPerformanceLog + 1);\r
259 LogEntryArray[Index].EndTimeStamp = TimeStamp;\r
260\r
261 return RETURN_SUCCESS;\r
262}\r
263\r
264/**\r
265 Attempts to retrieve a performance measurement log entry from the performance measurement log.\r
266 It can also retrieve the log created by StartPerformanceMeasurement and EndPerformanceMeasurement,\r
267 and then assign the Identifier with 0.\r
268\r
269 Attempts to retrieve the performance log entry specified by LogEntryKey. If LogEntryKey is\r
270 zero on entry, then an attempt is made to retrieve the first entry from the performance log,\r
271 and the key for the second entry in the log is returned. If the performance log is empty,\r
272 then no entry is retrieved and zero is returned. If LogEntryKey is not zero, then the performance\r
273 log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is\r
274 returned. If LogEntryKey is the key for the last entry in the log, then the last log entry is\r
275 retrieved and an implementation specific non-zero key value that specifies the end of the performance\r
276 log is returned. If LogEntryKey is equal this implementation specific non-zero key value, then no entry\r
277 is retrieved and zero is returned. In the cases where a performance log entry can be returned,\r
278 the log entry is returned in Handle, Token, Module, StartTimeStamp, EndTimeStamp and Identifier.\r
279 If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().\r
280 If Handle is NULL, then ASSERT().\r
281 If Token is NULL, then ASSERT().\r
282 If Module is NULL, then ASSERT().\r
283 If StartTimeStamp is NULL, then ASSERT().\r
284 If EndTimeStamp is NULL, then ASSERT().\r
285 If Identifier is NULL, then ASSERT().\r
286\r
287 @param LogEntryKey On entry, the key of the performance measurement log entry to retrieve.\r
288 0, then the first performance measurement log entry is retrieved.\r
289 On exit, the key of the next performance of entry entry.\r
290 @param Handle Pointer to environment specific context used to identify the component\r
291 being measured.\r
292 @param Token Pointer to a Null-terminated ASCII string that identifies the component\r
293 being measured.\r
294 @param Module Pointer to a Null-terminated ASCII string that identifies the module\r
295 being measured.\r
296 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement\r
297 was started.\r
298 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement\r
299 was ended.\r
300 @param Identifier Pointer to the 32-bit identifier that was recorded.\r
301\r
302 @return The key for the next performance log entry (in general case).\r
303\r
304**/\r
305UINTN\r
306EFIAPI\r
307GetPerformanceMeasurementEx (\r
308 IN UINTN LogEntryKey,\r
309 OUT CONST VOID **Handle,\r
310 OUT CONST CHAR8 **Token,\r
311 OUT CONST CHAR8 **Module,\r
312 OUT UINT64 *StartTimeStamp,\r
313 OUT UINT64 *EndTimeStamp,\r
314 OUT UINT32 *Identifier\r
315 )\r
316{\r
317 PEI_PERFORMANCE_LOG_HEADER *PeiPerformanceLog;\r
318 UINT32 *PeiPerformanceIdArray;\r
319 PEI_PERFORMANCE_LOG_ENTRY *CurrentLogEntry;\r
320 PEI_PERFORMANCE_LOG_ENTRY *LogEntryArray;\r
321 UINTN NumberOfEntries;\r
322\r
323 ASSERT (Handle != NULL);\r
324 ASSERT (Token != NULL);\r
325 ASSERT (Module != NULL);\r
326 ASSERT (StartTimeStamp != NULL);\r
327 ASSERT (EndTimeStamp != NULL);\r
328 ASSERT (Identifier != NULL);\r
329\r
330 InternalGetPerformanceHobLog (&PeiPerformanceLog, &PeiPerformanceIdArray);\r
331\r
332 NumberOfEntries = (UINTN) (PeiPerformanceLog->NumberOfEntries);\r
333 LogEntryArray = (PEI_PERFORMANCE_LOG_ENTRY *) (PeiPerformanceLog + 1);\r
334 //\r
335 // Make sure that LogEntryKey is a valid log entry key.\r
336 //\r
337 ASSERT (LogEntryKey <= NumberOfEntries);\r
338\r
339 if (LogEntryKey == NumberOfEntries) {\r
340 return 0;\r
341 }\r
342\r
343 CurrentLogEntry = &(LogEntryArray[LogEntryKey]);\r
344\r
345 *Handle = (VOID *) (UINTN) (CurrentLogEntry->Handle);\r
346 *Token = CurrentLogEntry->Token;\r
347 *Module = CurrentLogEntry->Module;\r
348 *StartTimeStamp = CurrentLogEntry->StartTimeStamp;\r
349 *EndTimeStamp = CurrentLogEntry->EndTimeStamp;\r
350 *Identifier = PeiPerformanceIdArray[LogEntryKey++];\r
351\r
352 return LogEntryKey;\r
353}\r
354\r
355/**\r
356 Creates a record for the beginning of a performance measurement.\r
357\r
358 Creates a record that contains the Handle, Token, and Module.\r
359 If TimeStamp is not zero, then TimeStamp is added to the record as the start time.\r
360 If TimeStamp is zero, then this function reads the current time stamp\r
361 and adds that time stamp value to the record as the start time.\r
362\r
363 @param Handle Pointer to environment specific context used\r
364 to identify the component being measured.\r
365 @param Token Pointer to a Null-terminated ASCII string\r
366 that identifies the component being measured.\r
367 @param Module Pointer to a Null-terminated ASCII string\r
368 that identifies the module being measured.\r
369 @param TimeStamp 64-bit time stamp.\r
370\r
371 @retval RETURN_SUCCESS The start of the measurement was recorded.\r
372 @retval RETURN_OUT_OF_RESOURCES There are not enough resources to record the measurement.\r
373\r
374**/\r
375RETURN_STATUS\r
376EFIAPI\r
377StartPerformanceMeasurement (\r
378 IN CONST VOID *Handle, OPTIONAL\r
379 IN CONST CHAR8 *Token, OPTIONAL\r
380 IN CONST CHAR8 *Module, OPTIONAL\r
381 IN UINT64 TimeStamp\r
382 )\r
383{\r
384 return StartPerformanceMeasurementEx (Handle, Token, Module, TimeStamp, 0);\r
385}\r
386\r
387/**\r
388 Fills in the end time of a performance measurement.\r
389\r
390 Looks up the record that matches Handle, Token, and Module.\r
391 If the record can not be found then return RETURN_NOT_FOUND.\r
392 If the record is found and TimeStamp is not zero,\r
393 then TimeStamp is added to the record as the end time.\r
394 If the record is found and TimeStamp is zero, then this function reads\r
395 the current time stamp and adds that time stamp value to the record as the end time.\r
396\r
397 @param Handle Pointer to environment specific context used\r
398 to identify the component being measured.\r
399 @param Token Pointer to a Null-terminated ASCII string\r
400 that identifies the component being measured.\r
401 @param Module Pointer to a Null-terminated ASCII string\r
402 that identifies the module being measured.\r
403 @param TimeStamp 64-bit time stamp.\r
404\r
405 @retval RETURN_SUCCESS The end of the measurement was recorded.\r
406 @retval RETURN_NOT_FOUND The specified measurement record could not be found.\r
407\r
408**/\r
409RETURN_STATUS\r
410EFIAPI\r
411EndPerformanceMeasurement (\r
412 IN CONST VOID *Handle, OPTIONAL\r
413 IN CONST CHAR8 *Token, OPTIONAL\r
414 IN CONST CHAR8 *Module, OPTIONAL\r
415 IN UINT64 TimeStamp\r
416 )\r
417{\r
418 return EndPerformanceMeasurementEx (Handle, Token, Module, TimeStamp, 0);\r
419}\r
420\r
421/**\r
422 Attempts to retrieve a performance measurement log entry from the performance measurement log.\r
423 It can also retrieve the log created by StartPerformanceMeasurementEx and EndPerformanceMeasurementEx,\r
424 and then eliminate the Identifier.\r
425\r
426 Attempts to retrieve the performance log entry specified by LogEntryKey. If LogEntryKey is\r
427 zero on entry, then an attempt is made to retrieve the first entry from the performance log,\r
428 and the key for the second entry in the log is returned. If the performance log is empty,\r
429 then no entry is retrieved and zero is returned. If LogEntryKey is not zero, then the performance\r
430 log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is\r
431 returned. If LogEntryKey is the key for the last entry in the log, then the last log entry is\r
432 retrieved and an implementation specific non-zero key value that specifies the end of the performance\r
433 log is returned. If LogEntryKey is equal this implementation specific non-zero key value, then no entry\r
434 is retrieved and zero is returned. In the cases where a performance log entry can be returned,\r
435 the log entry is returned in Handle, Token, Module, StartTimeStamp, and EndTimeStamp.\r
436 If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().\r
437 If Handle is NULL, then ASSERT().\r
438 If Token is NULL, then ASSERT().\r
439 If Module is NULL, then ASSERT().\r
440 If StartTimeStamp is NULL, then ASSERT().\r
441 If EndTimeStamp is NULL, then ASSERT().\r
442\r
443 @param LogEntryKey On entry, the key of the performance measurement log entry to retrieve.\r
444 0, then the first performance measurement log entry is retrieved.\r
445 On exit, the key of the next performance of entry entry.\r
446 @param Handle Pointer to environment specific context used to identify the component\r
447 being measured.\r
448 @param Token Pointer to a Null-terminated ASCII string that identifies the component\r
449 being measured.\r
450 @param Module Pointer to a Null-terminated ASCII string that identifies the module\r
451 being measured.\r
452 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement\r
453 was started.\r
454 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement\r
455 was ended.\r
456\r
457 @return The key for the next performance log entry (in general case).\r
458\r
459**/\r
460UINTN\r
461EFIAPI\r
462GetPerformanceMeasurement (\r
463 IN UINTN LogEntryKey,\r
464 OUT CONST VOID **Handle,\r
465 OUT CONST CHAR8 **Token,\r
466 OUT CONST CHAR8 **Module,\r
467 OUT UINT64 *StartTimeStamp,\r
468 OUT UINT64 *EndTimeStamp\r
469 )\r
470{\r
471 UINT32 Identifier;\r
472 return GetPerformanceMeasurementEx (LogEntryKey, Handle, Token, Module, StartTimeStamp, EndTimeStamp, &Identifier);\r
473}\r
474\r
475/**\r
476 Returns TRUE if the performance measurement macros are enabled.\r
477\r
478 This function returns TRUE if the PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of\r
479 PcdPerformanceLibraryPropertyMask is set. Otherwise FALSE is returned.\r
480\r
481 @retval TRUE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of\r
482 PcdPerformanceLibraryPropertyMask is set.\r
483 @retval FALSE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of\r
484 PcdPerformanceLibraryPropertyMask is clear.\r
485\r
486**/\r
487BOOLEAN\r
488EFIAPI\r
489PerformanceMeasurementEnabled (\r
490 VOID\r
491 )\r
492{\r
493 return (BOOLEAN) ((PcdGet8(PcdPerformanceLibraryPropertyMask) & PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED) != 0);\r
494}\r