3 Copyright (c) 2006, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 Support for measurement of DXE performance
23 // The package level header files this module uses
27 // The protocols, PPI and GUID defintions for this module
29 #include <Protocol/Performance.h>
30 #include <Guid/PeiPerformanceHob.h>
32 // The Library classes this module consumes
34 #include <Library/PerformanceLib.h>
35 #include <Library/DebugLib.h>
36 #include <Library/HobLib.h>
37 #include <Library/BaseLib.h>
38 #include <Library/BaseMemoryLib.h>
39 #include <Library/TimerLib.h>
40 #include <Library/PcdLib.h>
41 #include <Library/UefiBootServicesTableLib.h>
42 #include <Library/MemoryAllocationLib.h>
45 // Interface declarations for Performance Protocol.
48 Adds a record at the end of the performance measurement log
49 that records the start time of a performance measurement.
51 Adds a record to the end of the performance measurement log
52 that contains the Handle, Token, and Module.
53 The end time of the new record must be set to zero.
54 If TimeStamp is not zero, then TimeStamp is used to fill in the start time in the record.
55 If TimeStamp is zero, the start time in the record is filled in with the value
56 read from the current time stamp.
58 @param Handle Pointer to environment specific context used
59 to identify the component being measured.
60 @param Token Pointer to a Null-terminated ASCII string
61 that identifies the component being measured.
62 @param Module Pointer to a Null-terminated ASCII string
63 that identifies the module being measured.
64 @param TimeStamp 64-bit time stamp.
66 @retval EFI_SUCCESS The data was read correctly from the device.
67 @retval EFI_OUT_OF_RESOURCES There are not enough resources to record the measurement.
74 IN CONST VOID
*Handle
, OPTIONAL
75 IN CONST CHAR8
*Token
, OPTIONAL
76 IN CONST CHAR8
*Module
, OPTIONAL
81 Searches the performance measurement log from the beginning of the log
82 for the first matching record that contains a zero end time and fills in a valid end time.
84 Searches the performance measurement log from the beginning of the log
85 for the first record that matches Handle, Token, and Module and has an end time value of zero.
86 If the record can not be found then return EFI_NOT_FOUND.
87 If the record is found and TimeStamp is not zero,
88 then the end time in the record is filled in with the value specified by TimeStamp.
89 If the record is found and TimeStamp is zero, then the end time in the matching record
90 is filled in with the current time stamp value.
92 @param Handle Pointer to environment specific context used
93 to identify the component being measured.
94 @param Token Pointer to a Null-terminated ASCII string
95 that identifies the component being measured.
96 @param Module Pointer to a Null-terminated ASCII string
97 that identifies the module being measured.
98 @param TimeStamp 64-bit time stamp.
100 @retval EFI_SUCCESS The end of the measurement was recorded.
101 @retval EFI_NOT_FOUND The specified measurement record could not be found.
108 IN CONST VOID
*Handle
, OPTIONAL
109 IN CONST CHAR8
*Token
, OPTIONAL
110 IN CONST CHAR8
*Module
, OPTIONAL
115 Retrieves a previously logged performance measurement.
117 Retrieves the performance log entry from the performance log specified by LogEntryKey.
118 If it stands for a valid entry, then EFI_SUCCESS is returned and
119 GaugeDataEntry stores the pointer to that entry.
121 @param LogEntryKey The key for the previous performance measurement log entry.
122 If 0, then the first performance measurement log entry is retrieved.
123 @param GaugeDataEntry The indirect pointer to the gauge data entry specified by LogEntryKey
124 if the retrieval is successful.
126 @retval EFI_SUCCESS The GuageDataEntry is successfuly found based on LogEntryKey.
127 @retval EFI_NOT_FOUND The LogEntryKey is the last entry (equals to the total entry number).
128 @retval EFI_INVALIDE_PARAMETER The LogEntryKey is not a valid entry (greater than the total entry number).
129 @retval EFI_INVALIDE_PARAMETER GaugeDataEntry is NULL.
136 IN UINTN LogEntryKey
,
137 OUT GAUGE_DATA_ENTRY
**GaugeDataEntry
141 // Definition for global variables.
143 STATIC GAUGE_DATA_HEADER
*mGaugeData
;
144 STATIC UINT32 mMaxGaugeRecords
;
146 EFI_HANDLE mHandle
= NULL
;
147 PERFORMANCE_PROTOCOL mPerformanceInterface
= {
155 Searches in the gauge array with keyword Handle, Token and Module.
157 This internal function searches for the gauge entry in the gauge array.
158 If there is an entry that exactly matches the given key word triple
159 and its end time stamp is zero, then the index of that gauge entry is returned;
160 otherwise, the the number of gauge entries in the array is returned.
162 @param Handle Pointer to environment specific context used
163 to identify the component being measured.
164 @param Token Pointer to a Null-terminated ASCII string
165 that identifies the component being measured.
166 @param Module Pointer to a Null-terminated ASCII string
167 that identifies the module being measured.
169 @retval The index of gauge entry in the array.
174 InternalSearchForGaugeEntry (
175 IN CONST VOID
*Handle
, OPTIONAL
176 IN CONST CHAR8
*Token
, OPTIONAL
177 IN CONST CHAR8
*Module OPTIONAL
181 UINT32 NumberOfEntries
;
182 GAUGE_DATA_ENTRY
*GaugeEntryArray
;
187 if (Module
== NULL
) {
191 NumberOfEntries
= mGaugeData
->NumberOfEntries
;
192 GaugeEntryArray
= (GAUGE_DATA_ENTRY
*) (mGaugeData
+ 1);
194 for (Index
= 0; Index
< NumberOfEntries
; Index
++) {
195 if ((GaugeEntryArray
[Index
].Handle
== (EFI_PHYSICAL_ADDRESS
) (UINTN
) Handle
) &&
196 AsciiStrnCmp (GaugeEntryArray
[Index
].Token
, Token
, PEI_PERFORMANCE_STRING_LENGTH
) == 0 &&
197 AsciiStrnCmp (GaugeEntryArray
[Index
].Module
, Module
, PEI_PERFORMANCE_STRING_LENGTH
) == 0 &&
198 GaugeEntryArray
[Index
].EndTimeStamp
== 0
208 Adds a record at the end of the performance measurement log
209 that records the start time of a performance measurement.
211 Adds a record to the end of the performance measurement log
212 that contains the Handle, Token, and Module.
213 The end time of the new record must be set to zero.
214 If TimeStamp is not zero, then TimeStamp is used to fill in the start time in the record.
215 If TimeStamp is zero, the start time in the record is filled in with the value
216 read from the current time stamp.
218 @param Handle Pointer to environment specific context used
219 to identify the component being measured.
220 @param Token Pointer to a Null-terminated ASCII string
221 that identifies the component being measured.
222 @param Module Pointer to a Null-terminated ASCII string
223 that identifies the module being measured.
224 @param TimeStamp 64-bit time stamp.
226 @retval EFI_SUCCESS The data was read correctly from the device.
227 @retval EFI_OUT_OF_RESOURCES There are not enough resources to record the measurement.
234 IN CONST VOID
*Handle
, OPTIONAL
235 IN CONST CHAR8
*Token
, OPTIONAL
236 IN CONST CHAR8
*Module
, OPTIONAL
240 GAUGE_DATA_ENTRY
*GaugeEntryArray
;
242 UINTN OldGaugeDataSize
;
243 GAUGE_DATA_HEADER
*OldGaugeData
;
246 Index
= mGaugeData
->NumberOfEntries
;
247 if (Index
>= mMaxGaugeRecords
) {
249 // Try to enlarge the scale of gauge arrary.
251 OldGaugeData
= mGaugeData
;
252 OldGaugeDataSize
= sizeof (GAUGE_DATA_HEADER
) + sizeof (GAUGE_DATA_ENTRY
) * mMaxGaugeRecords
;
254 mMaxGaugeRecords
*= 2;
255 GaugeDataSize
= sizeof (GAUGE_DATA_HEADER
) + sizeof (GAUGE_DATA_ENTRY
) * mMaxGaugeRecords
;
257 mGaugeData
= AllocateZeroPool (GaugeDataSize
);
258 if (mGaugeData
== NULL
) {
259 return EFI_OUT_OF_RESOURCES
;
262 // Initialize new data arry and migrate old data one.
264 mGaugeData
= CopyMem (mGaugeData
, OldGaugeData
, OldGaugeDataSize
);
266 FreePool (OldGaugeData
);
269 GaugeEntryArray
= (GAUGE_DATA_ENTRY
*) (mGaugeData
+ 1);
270 GaugeEntryArray
[Index
].Handle
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) Handle
;
273 AsciiStrnCpy (GaugeEntryArray
[Index
].Token
, Token
, DXE_PERFORMANCE_STRING_LENGTH
);
275 if (Module
!= NULL
) {
276 AsciiStrnCpy (GaugeEntryArray
[Index
].Module
, Module
, DXE_PERFORMANCE_STRING_LENGTH
);
279 if (TimeStamp
== 0) {
280 TimeStamp
= GetPerformanceCounter ();
282 GaugeEntryArray
[Index
].StartTimeStamp
= TimeStamp
;
284 mGaugeData
->NumberOfEntries
++;
290 Searches the performance measurement log from the beginning of the log
291 for the first matching record that contains a zero end time and fills in a valid end time.
293 Searches the performance measurement log from the beginning of the log
294 for the first record that matches Handle, Token, and Module and has an end time value of zero.
295 If the record can not be found then return EFI_NOT_FOUND.
296 If the record is found and TimeStamp is not zero,
297 then the end time in the record is filled in with the value specified by TimeStamp.
298 If the record is found and TimeStamp is zero, then the end time in the matching record
299 is filled in with the current time stamp value.
301 @param Handle Pointer to environment specific context used
302 to identify the component being measured.
303 @param Token Pointer to a Null-terminated ASCII string
304 that identifies the component being measured.
305 @param Module Pointer to a Null-terminated ASCII string
306 that identifies the module being measured.
307 @param TimeStamp 64-bit time stamp.
309 @retval EFI_SUCCESS The end of the measurement was recorded.
310 @retval EFI_NOT_FOUND The specified measurement record could not be found.
317 IN CONST VOID
*Handle
, OPTIONAL
318 IN CONST CHAR8
*Token
, OPTIONAL
319 IN CONST CHAR8
*Module
, OPTIONAL
323 GAUGE_DATA_ENTRY
*GaugeEntryArray
;
326 if (TimeStamp
== 0) {
327 TimeStamp
= GetPerformanceCounter ();
330 Index
= InternalSearchForGaugeEntry (Handle
, Token
, Module
);
331 if (Index
>= mGaugeData
->NumberOfEntries
) {
332 return EFI_NOT_FOUND
;
334 GaugeEntryArray
= (GAUGE_DATA_ENTRY
*) (mGaugeData
+ 1);
335 GaugeEntryArray
[Index
].EndTimeStamp
= TimeStamp
;
341 Retrieves a previously logged performance measurement.
343 Retrieves the performance log entry from the performance log specified by LogEntryKey.
344 If it stands for a valid entry, then EFI_SUCCESS is returned and
345 GaugeDataEntry stores the pointer to that entry.
347 @param LogEntryKey The key for the previous performance measurement log entry.
348 If 0, then the first performance measurement log entry is retrieved.
349 @param GaugeDataEntry The indirect pointer to the gauge data entry specified by LogEntryKey
350 if the retrieval is successful.
352 @retval EFI_SUCCESS The GuageDataEntry is successfuly found based on LogEntryKey.
353 @retval EFI_NOT_FOUND The LogEntryKey is the last entry (equals to the total entry number).
354 @retval EFI_INVALIDE_PARAMETER The LogEntryKey is not a valid entry (greater than the total entry number).
355 @retval EFI_INVALIDE_PARAMETER GaugeDataEntry is NULL.
362 IN UINTN LogEntryKey
,
363 OUT GAUGE_DATA_ENTRY
**GaugeDataEntry
366 UINTN NumberOfEntries
;
367 GAUGE_DATA_ENTRY
*LogEntryArray
;
369 NumberOfEntries
= (UINTN
) (mGaugeData
->NumberOfEntries
);
370 if (LogEntryKey
> NumberOfEntries
) {
371 return EFI_INVALID_PARAMETER
;
373 if (LogEntryKey
== NumberOfEntries
) {
374 return EFI_NOT_FOUND
;
377 LogEntryArray
= (GAUGE_DATA_ENTRY
*) (mGaugeData
+ 1);
379 if (GaugeDataEntry
== NULL
) {
380 return EFI_INVALID_PARAMETER
;
382 *GaugeDataEntry
= &LogEntryArray
[LogEntryKey
];
388 Dumps all the PEI performance log to DXE performance gauge array.
390 This internal function dumps all the PEI performance log to the DXE performance gauge array.
391 It retrieves the optional GUID HOB for PEI performance and then saves the performance data
392 to DXE performance data structures.
397 InternalGetPeiPerformance (
401 EFI_HOB_GUID_TYPE
*GuidHob
;
402 PEI_PERFORMANCE_LOG_HEADER
*LogHob
;
403 PEI_PERFORMANCE_LOG_ENTRY
*LogEntryArray
;
404 GAUGE_DATA_ENTRY
*GaugeEntryArray
;
406 UINT32 NumberOfEntries
;
409 GaugeEntryArray
= (GAUGE_DATA_ENTRY
*) (mGaugeData
+ 1);
412 // Dump PEI Log Entries to DXE Guage Data structure.
414 GuidHob
= GetFirstGuidHob (&gPeiPerformanceHobGuid
);
415 if (GuidHob
!= NULL
) {
416 LogHob
= GET_GUID_HOB_DATA (GuidHob
);
417 LogEntryArray
= (PEI_PERFORMANCE_LOG_ENTRY
*) (LogHob
+ 1);
418 GaugeEntryArray
= (GAUGE_DATA_ENTRY
*) (mGaugeData
+ 1);
420 NumberOfEntries
= LogHob
->NumberOfEntries
;
421 for (Index
= 0; Index
< NumberOfEntries
; Index
++) {
422 GaugeEntryArray
[Index
].Handle
= LogEntryArray
[Index
].Handle
;
423 AsciiStrnCpy (GaugeEntryArray
[Index
].Token
, LogEntryArray
[Index
].Token
, DXE_PERFORMANCE_STRING_LENGTH
);
424 AsciiStrnCpy (GaugeEntryArray
[Index
].Module
, LogEntryArray
[Index
].Module
, DXE_PERFORMANCE_STRING_LENGTH
);
425 GaugeEntryArray
[Index
].StartTimeStamp
= LogEntryArray
[Index
].StartTimeStamp
;
426 GaugeEntryArray
[Index
].EndTimeStamp
= LogEntryArray
[Index
].EndTimeStamp
;
429 mGaugeData
->NumberOfEntries
= NumberOfEntries
;
433 The constructor function initializes Performance infrastructure for DXE phase.
435 The constructor function publishes Performance protocol, allocates memory to log DXE performance
436 and merges PEI performance data to DXE performance log.
437 It will ASSERT() if one of these operations fails and it will always return EFI_SUCCESS.
439 @param ImageHandle The firmware allocated handle for the EFI image.
440 @param SystemTable A pointer to the EFI System Table.
442 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
447 DxeCorePerformanceLibConstructor (
448 IN EFI_HANDLE ImageHandle
,
449 IN EFI_SYSTEM_TABLE
*SystemTable
454 if (!PerformanceMeasurementEnabled ()) {
456 // Do not initialize performance infrastructure if not required.
461 // Install the protocol interfaces.
463 Status
= gBS
->InstallProtocolInterface (
465 &gPerformanceProtocolGuid
,
466 EFI_NATIVE_INTERFACE
,
467 &mPerformanceInterface
469 ASSERT_EFI_ERROR (Status
);
471 mMaxGaugeRecords
= INIT_DXE_GAUGE_DATA_ENTRIES
+ PcdGet8 (PcdMaxPeiPerformanceLogEntries
);
473 mGaugeData
= AllocateZeroPool (sizeof (GAUGE_DATA_HEADER
) + (sizeof (GAUGE_DATA_ENTRY
) * mMaxGaugeRecords
));
474 ASSERT (mGaugeData
!= NULL
);
476 InternalGetPeiPerformance ();
482 Adds a record at the end of the performance measurement log
483 that records the start time of a performance measurement.
485 Adds a record to the end of the performance measurement log
486 that contains the Handle, Token, and Module.
487 The end time of the new record must be set to zero.
488 If TimeStamp is not zero, then TimeStamp is used to fill in the start time in the record.
489 If TimeStamp is zero, the start time in the record is filled in with the value
490 read from the current time stamp.
492 @param Handle Pointer to environment specific context used
493 to identify the component being measured.
494 @param Token Pointer to a Null-terminated ASCII string
495 that identifies the component being measured.
496 @param Module Pointer to a Null-terminated ASCII string
497 that identifies the module being measured.
498 @param TimeStamp 64-bit time stamp.
500 @retval RETURN_SUCCESS The start of the measurement was recorded.
501 @retval RETURN_OUT_OF_RESOURCES There are not enough resources to record the measurement.
506 StartPerformanceMeasurement (
507 IN CONST VOID
*Handle
, OPTIONAL
508 IN CONST CHAR8
*Token
, OPTIONAL
509 IN CONST CHAR8
*Module
, OPTIONAL
515 Status
= StartGauge (Handle
, Token
, Module
, TimeStamp
);
516 return (RETURN_STATUS
) Status
;
520 Searches the performance measurement log from the beginning of the log
521 for the first matching record that contains a zero end time and fills in a valid end time.
523 Searches the performance measurement log from the beginning of the log
524 for the first record that matches Handle, Token, and Module and has an end time value of zero.
525 If the record can not be found then return RETURN_NOT_FOUND.
526 If the record is found and TimeStamp is not zero,
527 then the end time in the record is filled in with the value specified by TimeStamp.
528 If the record is found and TimeStamp is zero, then the end time in the matching record
529 is filled in with the current time stamp value.
531 @param Handle Pointer to environment specific context used
532 to identify the component being measured.
533 @param Token Pointer to a Null-terminated ASCII string
534 that identifies the component being measured.
535 @param Module Pointer to a Null-terminated ASCII string
536 that identifies the module being measured.
537 @param TimeStamp 64-bit time stamp.
539 @retval RETURN_SUCCESS The end of the measurement was recorded.
540 @retval RETURN_NOT_FOUND The specified measurement record could not be found.
545 EndPerformanceMeasurement (
546 IN CONST VOID
*Handle
, OPTIONAL
547 IN CONST CHAR8
*Token
, OPTIONAL
548 IN CONST CHAR8
*Module
, OPTIONAL
554 Status
= EndGauge (Handle
, Token
, Module
, TimeStamp
);
555 return (RETURN_STATUS
) Status
;
559 Attempts to retrieve a performance measurement log entry from the performance measurement log.
561 Attempts to retrieve the performance log entry specified by LogEntryKey. If LogEntryKey is
562 zero on entry, then an attempt is made to retrieve the first entry from the performance log,
563 and the key for the second entry in the log is returned. If the performance log is empty,
564 then no entry is retrieved and zero is returned. If LogEntryKey is not zero, then the performance
565 log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is
566 returned. If LogEntryKey is the key for the last entry in the log, then the last log entry is
567 retrieved and an implementation specific non-zero key value that specifies the end of the performance
568 log is returned. If LogEntryKey is equal this implementation specific non-zero key value, then no entry
569 is retrieved and zero is returned. In the cases where a performance log entry can be returned,
570 the log entry is returned in Handle, Token, Module, StartTimeStamp, and EndTimeStamp.
571 If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().
572 If Handle is NULL, then ASSERT().
573 If Token is NULL, then ASSERT().
574 If Module is NULL, then ASSERT().
575 If StartTimeStamp is NULL, then ASSERT().
576 If EndTimeStamp is NULL, then ASSERT().
578 @param LogEntryKey On entry, the key of the performance measurement log entry to retrieve.
579 0, then the first performance measurement log entry is retrieved.
580 On exit, the key of the next performance lof entry entry.
581 @param Handle Pointer to environment specific context used to identify the component
583 @param Token Pointer to a Null-terminated ASCII string that identifies the component
585 @param Module Pointer to a Null-terminated ASCII string that identifies the module
587 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
589 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
592 @return The key for the next performance log entry (in general case).
597 GetPerformanceMeasurement (
598 IN UINTN LogEntryKey
,
599 OUT CONST VOID
**Handle
,
600 OUT CONST CHAR8
**Token
,
601 OUT CONST CHAR8
**Module
,
602 OUT UINT64
*StartTimeStamp
,
603 OUT UINT64
*EndTimeStamp
607 GAUGE_DATA_ENTRY
*GaugeData
;
609 ASSERT (Handle
!= NULL
);
610 ASSERT (Token
!= NULL
);
611 ASSERT (Module
!= NULL
);
612 ASSERT (StartTimeStamp
!= NULL
);
613 ASSERT (EndTimeStamp
!= NULL
);
615 Status
= GetGauge (LogEntryKey
++, &GaugeData
);
618 // Make sure that LogEntryKey is a valid log entry key,
620 ASSERT (Status
!= EFI_INVALID_PARAMETER
);
622 if (EFI_ERROR (Status
)) {
624 // The LogEntryKey is the last entry (equals to the total entry number).
629 ASSERT (GaugeData
!= NULL
);
631 *Handle
= (VOID
*) (UINTN
) GaugeData
->Handle
;
632 *Token
= GaugeData
->Token
;
633 *Module
= GaugeData
->Module
;
634 *StartTimeStamp
= GaugeData
->StartTimeStamp
;
635 *EndTimeStamp
= GaugeData
->EndTimeStamp
;
641 Returns TRUE if the performance measurement macros are enabled.
643 This function returns TRUE if the PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
644 PcdPerformanceLibraryPropertyMask is set. Otherwise FALSE is returned.
646 @retval TRUE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
647 PcdPerformanceLibraryPropertyMask is set.
648 @retval FALSE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
649 PcdPerformanceLibraryPropertyMask is clear.
654 PerformanceMeasurementEnabled (
658 return (BOOLEAN
) ((PcdGet8(PcdPerformanceLibraryPropertyMask
) & PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED
) != 0);