2 Support for measurement of DXE performance
4 Copyright (c) 2006 - 2008, Intel Corporation. <BR>
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 #include <Protocol/Performance.h>
19 #include <Guid/PeiPerformanceHob.h>
21 #include <Library/PerformanceLib.h>
22 #include <Library/DebugLib.h>
23 #include <Library/HobLib.h>
24 #include <Library/BaseLib.h>
25 #include <Library/BaseMemoryLib.h>
26 #include <Library/TimerLib.h>
27 #include <Library/PcdLib.h>
28 #include <Library/UefiBootServicesTableLib.h>
29 #include <Library/MemoryAllocationLib.h>
32 // Interface declarations for Performance Protocol.
35 Adds a record at the end of the performance measurement log
36 that records the start time of a performance measurement.
38 Adds a record to the end of the performance measurement log
39 that contains the Handle, Token, and Module.
40 The end time of the new record must be set to zero.
41 If TimeStamp is not zero, then TimeStamp is used to fill in the start time in the record.
42 If TimeStamp is zero, the start time in the record is filled in with the value
43 read from the current time stamp.
45 @param Handle Pointer to environment specific context used
46 to identify the component being measured.
47 @param Token Pointer to a Null-terminated ASCII string
48 that identifies the component being measured.
49 @param Module Pointer to a Null-terminated ASCII string
50 that identifies the module being measured.
51 @param TimeStamp 64-bit time stamp.
53 @retval EFI_SUCCESS The data was read correctly from the device.
54 @retval EFI_OUT_OF_RESOURCES There are not enough resources to record the measurement.
60 IN CONST VOID
*Handle
, OPTIONAL
61 IN CONST CHAR8
*Token
, OPTIONAL
62 IN CONST CHAR8
*Module
, OPTIONAL
67 Searches the performance measurement log from the beginning of the log
68 for the first matching record that contains a zero end time and fills in a valid end time.
70 Searches the performance measurement log from the beginning of the log
71 for the first record that matches Handle, Token, and Module and has an end time value of zero.
72 If the record can not be found then return EFI_NOT_FOUND.
73 If the record is found and TimeStamp is not zero,
74 then the end time in the record is filled in with the value specified by TimeStamp.
75 If the record is found and TimeStamp is zero, then the end time in the matching record
76 is filled in with the current time stamp value.
78 @param Handle Pointer to environment specific context used
79 to identify the component being measured.
80 @param Token Pointer to a Null-terminated ASCII string
81 that identifies the component being measured.
82 @param Module Pointer to a Null-terminated ASCII string
83 that identifies the module being measured.
84 @param TimeStamp 64-bit time stamp.
86 @retval EFI_SUCCESS The end of the measurement was recorded.
87 @retval EFI_NOT_FOUND The specified measurement record could not be found.
93 IN CONST VOID
*Handle
, OPTIONAL
94 IN CONST CHAR8
*Token
, OPTIONAL
95 IN CONST CHAR8
*Module
, OPTIONAL
100 Retrieves a previously logged performance measurement.
102 Retrieves the performance log entry from the performance log specified by LogEntryKey.
103 If it stands for a valid entry, then EFI_SUCCESS is returned and
104 GaugeDataEntry stores the pointer to that entry.
106 @param LogEntryKey The key for the previous performance measurement log entry.
107 If 0, then the first performance measurement log entry is retrieved.
108 @param GaugeDataEntry The indirect pointer to the gauge data entry specified by LogEntryKey
109 if the retrieval is successful.
111 @retval EFI_SUCCESS The GuageDataEntry is successfuly found based on LogEntryKey.
112 @retval EFI_NOT_FOUND The LogEntryKey is the last entry (equals to the total entry number).
113 @retval EFI_INVALIDE_PARAMETER The LogEntryKey is not a valid entry (greater than the total entry number).
114 @retval EFI_INVALIDE_PARAMETER GaugeDataEntry is NULL.
120 IN UINTN LogEntryKey
,
121 OUT GAUGE_DATA_ENTRY
**GaugeDataEntry
125 // Definition for global variables.
127 GAUGE_DATA_HEADER
*mGaugeData
;
128 UINT32 mMaxGaugeRecords
;
130 EFI_HANDLE mHandle
= NULL
;
131 PERFORMANCE_PROTOCOL mPerformanceInterface
= {
139 Searches in the gauge array with keyword Handle, Token and Module.
141 This internal function searches for the gauge entry in the gauge array.
142 If there is an entry that exactly matches the given key word triple
143 and its end time stamp is zero, then the index of that gauge entry is returned;
144 otherwise, the the number of gauge entries in the array is returned.
146 @param Handle Pointer to environment specific context used
147 to identify the component being measured.
148 @param Token Pointer to a Null-terminated ASCII string
149 that identifies the component being measured.
150 @param Module Pointer to a Null-terminated ASCII string
151 that identifies the module being measured.
153 @retval The index of gauge entry in the array.
157 InternalSearchForGaugeEntry (
158 IN CONST VOID
*Handle
, OPTIONAL
159 IN CONST CHAR8
*Token
, OPTIONAL
160 IN CONST CHAR8
*Module OPTIONAL
164 UINT32 NumberOfEntries
;
165 GAUGE_DATA_ENTRY
*GaugeEntryArray
;
170 if (Module
== NULL
) {
174 NumberOfEntries
= mGaugeData
->NumberOfEntries
;
175 GaugeEntryArray
= (GAUGE_DATA_ENTRY
*) (mGaugeData
+ 1);
177 for (Index
= 0; Index
< NumberOfEntries
; Index
++) {
178 if ((GaugeEntryArray
[Index
].Handle
== (EFI_PHYSICAL_ADDRESS
) (UINTN
) Handle
) &&
179 AsciiStrnCmp (GaugeEntryArray
[Index
].Token
, Token
, PEI_PERFORMANCE_STRING_LENGTH
) == 0 &&
180 AsciiStrnCmp (GaugeEntryArray
[Index
].Module
, Module
, PEI_PERFORMANCE_STRING_LENGTH
) == 0 &&
181 GaugeEntryArray
[Index
].EndTimeStamp
== 0
191 Adds a record at the end of the performance measurement log
192 that records the start time of a performance measurement.
194 Adds a record to the end of the performance measurement log
195 that contains the Handle, Token, and Module.
196 The end time of the new record must be set to zero.
197 If TimeStamp is not zero, then TimeStamp is used to fill in the start time in the record.
198 If TimeStamp is zero, the start time in the record is filled in with the value
199 read from the current time stamp.
201 @param Handle Pointer to environment specific context used
202 to identify the component being measured.
203 @param Token Pointer to a Null-terminated ASCII string
204 that identifies the component being measured.
205 @param Module Pointer to a Null-terminated ASCII string
206 that identifies the module being measured.
207 @param TimeStamp 64-bit time stamp.
209 @retval EFI_SUCCESS The data was read correctly from the device.
210 @retval EFI_OUT_OF_RESOURCES There are not enough resources to record the measurement.
216 IN CONST VOID
*Handle
, OPTIONAL
217 IN CONST CHAR8
*Token
, OPTIONAL
218 IN CONST CHAR8
*Module
, OPTIONAL
222 GAUGE_DATA_ENTRY
*GaugeEntryArray
;
224 UINTN OldGaugeDataSize
;
225 GAUGE_DATA_HEADER
*OldGaugeData
;
228 Index
= mGaugeData
->NumberOfEntries
;
229 if (Index
>= mMaxGaugeRecords
) {
231 // Try to enlarge the scale of gauge arrary.
233 OldGaugeData
= mGaugeData
;
234 OldGaugeDataSize
= sizeof (GAUGE_DATA_HEADER
) + sizeof (GAUGE_DATA_ENTRY
) * mMaxGaugeRecords
;
236 mMaxGaugeRecords
*= 2;
237 GaugeDataSize
= sizeof (GAUGE_DATA_HEADER
) + sizeof (GAUGE_DATA_ENTRY
) * mMaxGaugeRecords
;
239 mGaugeData
= AllocateZeroPool (GaugeDataSize
);
240 if (mGaugeData
== NULL
) {
241 return EFI_OUT_OF_RESOURCES
;
244 // Initialize new data arry and migrate old data one.
246 mGaugeData
= CopyMem (mGaugeData
, OldGaugeData
, OldGaugeDataSize
);
248 FreePool (OldGaugeData
);
251 GaugeEntryArray
= (GAUGE_DATA_ENTRY
*) (mGaugeData
+ 1);
252 GaugeEntryArray
[Index
].Handle
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) Handle
;
255 AsciiStrnCpy (GaugeEntryArray
[Index
].Token
, Token
, DXE_PERFORMANCE_STRING_LENGTH
);
257 if (Module
!= NULL
) {
258 AsciiStrnCpy (GaugeEntryArray
[Index
].Module
, Module
, DXE_PERFORMANCE_STRING_LENGTH
);
261 if (TimeStamp
== 0) {
262 TimeStamp
= GetPerformanceCounter ();
264 GaugeEntryArray
[Index
].StartTimeStamp
= TimeStamp
;
266 mGaugeData
->NumberOfEntries
++;
272 Searches the performance measurement log from the beginning of the log
273 for the first matching record that contains a zero end time and fills in a valid end time.
275 Searches the performance measurement log from the beginning of the log
276 for the first record that matches Handle, Token, and Module and has an end time value of zero.
277 If the record can not be found then return EFI_NOT_FOUND.
278 If the record is found and TimeStamp is not zero,
279 then the end time in the record is filled in with the value specified by TimeStamp.
280 If the record is found and TimeStamp is zero, then the end time in the matching record
281 is filled in with the current time stamp value.
283 @param Handle Pointer to environment specific context used
284 to identify the component being measured.
285 @param Token Pointer to a Null-terminated ASCII string
286 that identifies the component being measured.
287 @param Module Pointer to a Null-terminated ASCII string
288 that identifies the module being measured.
289 @param TimeStamp 64-bit time stamp.
291 @retval EFI_SUCCESS The end of the measurement was recorded.
292 @retval EFI_NOT_FOUND The specified measurement record could not be found.
298 IN CONST VOID
*Handle
, OPTIONAL
299 IN CONST CHAR8
*Token
, OPTIONAL
300 IN CONST CHAR8
*Module
, OPTIONAL
304 GAUGE_DATA_ENTRY
*GaugeEntryArray
;
307 if (TimeStamp
== 0) {
308 TimeStamp
= GetPerformanceCounter ();
311 Index
= InternalSearchForGaugeEntry (Handle
, Token
, Module
);
312 if (Index
>= mGaugeData
->NumberOfEntries
) {
313 return EFI_NOT_FOUND
;
315 GaugeEntryArray
= (GAUGE_DATA_ENTRY
*) (mGaugeData
+ 1);
316 GaugeEntryArray
[Index
].EndTimeStamp
= TimeStamp
;
322 Retrieves a previously logged performance measurement.
324 Retrieves the performance log entry from the performance log specified by LogEntryKey.
325 If it stands for a valid entry, then EFI_SUCCESS is returned and
326 GaugeDataEntry stores the pointer to that entry.
328 @param LogEntryKey The key for the previous performance measurement log entry.
329 If 0, then the first performance measurement log entry is retrieved.
330 @param GaugeDataEntry The indirect pointer to the gauge data entry specified by LogEntryKey
331 if the retrieval is successful.
333 @retval EFI_SUCCESS The GuageDataEntry is successfuly found based on LogEntryKey.
334 @retval EFI_NOT_FOUND The LogEntryKey is the last entry (equals to the total entry number).
335 @retval EFI_INVALIDE_PARAMETER The LogEntryKey is not a valid entry (greater than the total entry number).
336 @retval EFI_INVALIDE_PARAMETER GaugeDataEntry is NULL.
342 IN UINTN LogEntryKey
,
343 OUT GAUGE_DATA_ENTRY
**GaugeDataEntry
346 UINTN NumberOfEntries
;
347 GAUGE_DATA_ENTRY
*LogEntryArray
;
349 NumberOfEntries
= (UINTN
) (mGaugeData
->NumberOfEntries
);
350 if (LogEntryKey
> NumberOfEntries
) {
351 return EFI_INVALID_PARAMETER
;
353 if (LogEntryKey
== NumberOfEntries
) {
354 return EFI_NOT_FOUND
;
357 LogEntryArray
= (GAUGE_DATA_ENTRY
*) (mGaugeData
+ 1);
359 if (GaugeDataEntry
== NULL
) {
360 return EFI_INVALID_PARAMETER
;
362 *GaugeDataEntry
= &LogEntryArray
[LogEntryKey
];
368 Dumps all the PEI performance log to DXE performance gauge array.
370 This internal function dumps all the PEI performance log to the DXE performance gauge array.
371 It retrieves the optional GUID HOB for PEI performance and then saves the performance data
372 to DXE performance data structures.
376 InternalGetPeiPerformance (
380 EFI_HOB_GUID_TYPE
*GuidHob
;
381 PEI_PERFORMANCE_LOG_HEADER
*LogHob
;
382 PEI_PERFORMANCE_LOG_ENTRY
*LogEntryArray
;
383 GAUGE_DATA_ENTRY
*GaugeEntryArray
;
385 UINT32 NumberOfEntries
;
388 GaugeEntryArray
= (GAUGE_DATA_ENTRY
*) (mGaugeData
+ 1);
391 // Dump PEI Log Entries to DXE Guage Data structure.
393 GuidHob
= GetFirstGuidHob (&gPeiPerformanceHobGuid
);
394 if (GuidHob
!= NULL
) {
395 LogHob
= GET_GUID_HOB_DATA (GuidHob
);
396 LogEntryArray
= (PEI_PERFORMANCE_LOG_ENTRY
*) (LogHob
+ 1);
397 GaugeEntryArray
= (GAUGE_DATA_ENTRY
*) (mGaugeData
+ 1);
399 NumberOfEntries
= LogHob
->NumberOfEntries
;
400 for (Index
= 0; Index
< NumberOfEntries
; Index
++) {
401 GaugeEntryArray
[Index
].Handle
= LogEntryArray
[Index
].Handle
;
402 AsciiStrnCpy (GaugeEntryArray
[Index
].Token
, LogEntryArray
[Index
].Token
, DXE_PERFORMANCE_STRING_LENGTH
);
403 AsciiStrnCpy (GaugeEntryArray
[Index
].Module
, LogEntryArray
[Index
].Module
, DXE_PERFORMANCE_STRING_LENGTH
);
404 GaugeEntryArray
[Index
].StartTimeStamp
= LogEntryArray
[Index
].StartTimeStamp
;
405 GaugeEntryArray
[Index
].EndTimeStamp
= LogEntryArray
[Index
].EndTimeStamp
;
408 mGaugeData
->NumberOfEntries
= NumberOfEntries
;
412 The constructor function initializes Performance infrastructure for DXE phase.
414 The constructor function publishes Performance protocol, allocates memory to log DXE performance
415 and merges PEI performance data to DXE performance log.
416 It will ASSERT() if one of these operations fails and it will always return EFI_SUCCESS.
418 @param ImageHandle The firmware allocated handle for the EFI image.
419 @param SystemTable A pointer to the EFI System Table.
421 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
426 DxeCorePerformanceLibConstructor (
427 IN EFI_HANDLE ImageHandle
,
428 IN EFI_SYSTEM_TABLE
*SystemTable
433 if (!PerformanceMeasurementEnabled ()) {
435 // Do not initialize performance infrastructure if not required.
440 // Install the protocol interfaces.
442 Status
= gBS
->InstallProtocolInterface (
444 &gPerformanceProtocolGuid
,
445 EFI_NATIVE_INTERFACE
,
446 &mPerformanceInterface
448 ASSERT_EFI_ERROR (Status
);
450 mMaxGaugeRecords
= INIT_DXE_GAUGE_DATA_ENTRIES
+ PcdGet8 (PcdMaxPeiPerformanceLogEntries
);
452 mGaugeData
= AllocateZeroPool (sizeof (GAUGE_DATA_HEADER
) + (sizeof (GAUGE_DATA_ENTRY
) * mMaxGaugeRecords
));
453 ASSERT (mGaugeData
!= NULL
);
455 InternalGetPeiPerformance ();
461 Adds a record at the end of the performance measurement log
462 that records the start time of a performance measurement.
464 Adds a record to the end of the performance measurement log
465 that contains the Handle, Token, and Module.
466 The end time of the new record must be set to zero.
467 If TimeStamp is not zero, then TimeStamp is used to fill in the start time in the record.
468 If TimeStamp is zero, the start time in the record is filled in with the value
469 read from the current time stamp.
471 @param Handle Pointer to environment specific context used
472 to identify the component being measured.
473 @param Token Pointer to a Null-terminated ASCII string
474 that identifies the component being measured.
475 @param Module Pointer to a Null-terminated ASCII string
476 that identifies the module being measured.
477 @param TimeStamp 64-bit time stamp.
479 @retval RETURN_SUCCESS The start of the measurement was recorded.
480 @retval RETURN_OUT_OF_RESOURCES There are not enough resources to record the measurement.
485 StartPerformanceMeasurement (
486 IN CONST VOID
*Handle
, OPTIONAL
487 IN CONST CHAR8
*Token
, OPTIONAL
488 IN CONST CHAR8
*Module
, OPTIONAL
494 Status
= StartGauge (Handle
, Token
, Module
, TimeStamp
);
495 return (RETURN_STATUS
) Status
;
499 Searches the performance measurement log from the beginning of the log
500 for the first matching record that contains a zero end time and fills in a valid end time.
502 Searches the performance measurement log from the beginning of the log
503 for the first record that matches Handle, Token, and Module and has an end time value of zero.
504 If the record can not be found then return RETURN_NOT_FOUND.
505 If the record is found and TimeStamp is not zero,
506 then the end time in the record is filled in with the value specified by TimeStamp.
507 If the record is found and TimeStamp is zero, then the end time in the matching record
508 is filled in with the current time stamp value.
510 @param Handle Pointer to environment specific context used
511 to identify the component being measured.
512 @param Token Pointer to a Null-terminated ASCII string
513 that identifies the component being measured.
514 @param Module Pointer to a Null-terminated ASCII string
515 that identifies the module being measured.
516 @param TimeStamp 64-bit time stamp.
518 @retval RETURN_SUCCESS The end of the measurement was recorded.
519 @retval RETURN_NOT_FOUND The specified measurement record could not be found.
524 EndPerformanceMeasurement (
525 IN CONST VOID
*Handle
, OPTIONAL
526 IN CONST CHAR8
*Token
, OPTIONAL
527 IN CONST CHAR8
*Module
, OPTIONAL
533 Status
= EndGauge (Handle
, Token
, Module
, TimeStamp
);
534 return (RETURN_STATUS
) Status
;
538 Attempts to retrieve a performance measurement log entry from the performance measurement log.
540 Attempts to retrieve the performance log entry specified by LogEntryKey. If LogEntryKey is
541 zero on entry, then an attempt is made to retrieve the first entry from the performance log,
542 and the key for the second entry in the log is returned. If the performance log is empty,
543 then no entry is retrieved and zero is returned. If LogEntryKey is not zero, then the performance
544 log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is
545 returned. If LogEntryKey is the key for the last entry in the log, then the last log entry is
546 retrieved and an implementation specific non-zero key value that specifies the end of the performance
547 log is returned. If LogEntryKey is equal this implementation specific non-zero key value, then no entry
548 is retrieved and zero is returned. In the cases where a performance log entry can be returned,
549 the log entry is returned in Handle, Token, Module, StartTimeStamp, and EndTimeStamp.
550 If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().
551 If Handle is NULL, then ASSERT().
552 If Token is NULL, then ASSERT().
553 If Module is NULL, then ASSERT().
554 If StartTimeStamp is NULL, then ASSERT().
555 If EndTimeStamp is NULL, then ASSERT().
557 @param LogEntryKey On entry, the key of the performance measurement log entry to retrieve.
558 0, then the first performance measurement log entry is retrieved.
559 On exit, the key of the next performance lof entry entry.
560 @param Handle Pointer to environment specific context used to identify the component
562 @param Token Pointer to a Null-terminated ASCII string that identifies the component
564 @param Module Pointer to a Null-terminated ASCII string that identifies the module
566 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
568 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
571 @return The key for the next performance log entry (in general case).
576 GetPerformanceMeasurement (
577 IN UINTN LogEntryKey
,
578 OUT CONST VOID
**Handle
,
579 OUT CONST CHAR8
**Token
,
580 OUT CONST CHAR8
**Module
,
581 OUT UINT64
*StartTimeStamp
,
582 OUT UINT64
*EndTimeStamp
586 GAUGE_DATA_ENTRY
*GaugeData
;
590 ASSERT (Handle
!= NULL
);
591 ASSERT (Token
!= NULL
);
592 ASSERT (Module
!= NULL
);
593 ASSERT (StartTimeStamp
!= NULL
);
594 ASSERT (EndTimeStamp
!= NULL
);
596 Status
= GetGauge (LogEntryKey
++, &GaugeData
);
599 // Make sure that LogEntryKey is a valid log entry key,
601 ASSERT (Status
!= EFI_INVALID_PARAMETER
);
603 if (EFI_ERROR (Status
)) {
605 // The LogEntryKey is the last entry (equals to the total entry number).
610 ASSERT (GaugeData
!= NULL
);
612 *Handle
= (VOID
*) (UINTN
) GaugeData
->Handle
;
613 *Token
= GaugeData
->Token
;
614 *Module
= GaugeData
->Module
;
615 *StartTimeStamp
= GaugeData
->StartTimeStamp
;
616 *EndTimeStamp
= GaugeData
->EndTimeStamp
;
622 Returns TRUE if the performance measurement macros are enabled.
624 This function returns TRUE if the PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
625 PcdPerformanceLibraryPropertyMask is set. Otherwise FALSE is returned.
627 @retval TRUE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
628 PcdPerformanceLibraryPropertyMask is set.
629 @retval FALSE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
630 PcdPerformanceLibraryPropertyMask is clear.
635 PerformanceMeasurementEnabled (
639 return (BOOLEAN
) ((PcdGet8(PcdPerformanceLibraryPropertyMask
) & PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED
) != 0);