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
25 #include <Protocol/Performance.h>
26 #include <Guid/PeiPerformanceHob.h>
28 #include <Library/PerformanceLib.h>
29 #include <Library/DebugLib.h>
30 #include <Library/HobLib.h>
31 #include <Library/BaseLib.h>
32 #include <Library/BaseMemoryLib.h>
33 #include <Library/TimerLib.h>
34 #include <Library/PcdLib.h>
35 #include <Library/UefiBootServicesTableLib.h>
36 #include <Library/MemoryAllocationLib.h>
39 // Interface declarations for Performance Protocol.
42 Adds a record at the end of the performance measurement log
43 that records the start time of a performance measurement.
45 Adds a record to the end of the performance measurement log
46 that contains the Handle, Token, and Module.
47 The end time of the new record must be set to zero.
48 If TimeStamp is not zero, then TimeStamp is used to fill in the start time in the record.
49 If TimeStamp is zero, the start time in the record is filled in with the value
50 read from the current time stamp.
52 @param Handle Pointer to environment specific context used
53 to identify the component being measured.
54 @param Token Pointer to a Null-terminated ASCII string
55 that identifies the component being measured.
56 @param Module Pointer to a Null-terminated ASCII string
57 that identifies the module being measured.
58 @param TimeStamp 64-bit time stamp.
60 @retval EFI_SUCCESS The data was read correctly from the device.
61 @retval EFI_OUT_OF_RESOURCES There are not enough resources to record the measurement.
68 IN CONST VOID
*Handle
, OPTIONAL
69 IN CONST CHAR8
*Token
, OPTIONAL
70 IN CONST CHAR8
*Module
, OPTIONAL
75 Searches the performance measurement log from the beginning of the log
76 for the first matching record that contains a zero end time and fills in a valid end time.
78 Searches the performance measurement log from the beginning of the log
79 for the first record that matches Handle, Token, and Module and has an end time value of zero.
80 If the record can not be found then return EFI_NOT_FOUND.
81 If the record is found and TimeStamp is not zero,
82 then the end time in the record is filled in with the value specified by TimeStamp.
83 If the record is found and TimeStamp is zero, then the end time in the matching record
84 is filled in with the current time stamp value.
86 @param Handle Pointer to environment specific context used
87 to identify the component being measured.
88 @param Token Pointer to a Null-terminated ASCII string
89 that identifies the component being measured.
90 @param Module Pointer to a Null-terminated ASCII string
91 that identifies the module being measured.
92 @param TimeStamp 64-bit time stamp.
94 @retval EFI_SUCCESS The end of the measurement was recorded.
95 @retval EFI_NOT_FOUND The specified measurement record could not be found.
102 IN CONST VOID
*Handle
, OPTIONAL
103 IN CONST CHAR8
*Token
, OPTIONAL
104 IN CONST CHAR8
*Module
, OPTIONAL
109 Retrieves a previously logged performance measurement.
111 Retrieves the performance log entry from the performance log specified by LogEntryKey.
112 If it stands for a valid entry, then EFI_SUCCESS is returned and
113 GaugeDataEntry stores the pointer to that entry.
115 @param LogEntryKey The key for the previous performance measurement log entry.
116 If 0, then the first performance measurement log entry is retrieved.
117 @param GaugeDataEntry The indirect pointer to the gauge data entry specified by LogEntryKey
118 if the retrieval is successful.
120 @retval EFI_SUCCESS The GuageDataEntry is successfuly found based on LogEntryKey.
121 @retval EFI_NOT_FOUND The LogEntryKey is the last entry (equals to the total entry number).
122 @retval EFI_INVALIDE_PARAMETER The LogEntryKey is not a valid entry (greater than the total entry number).
123 @retval EFI_INVALIDE_PARAMETER GaugeDataEntry is NULL.
130 IN UINTN LogEntryKey
,
131 OUT GAUGE_DATA_ENTRY
**GaugeDataEntry
135 // Definition for global variables.
137 STATIC GAUGE_DATA_HEADER
*mGaugeData
;
138 STATIC UINT32 mMaxGaugeRecords
;
140 EFI_HANDLE mHandle
= NULL
;
141 PERFORMANCE_PROTOCOL mPerformanceInterface
= {
149 Searches in the gauge array with keyword Handle, Token and Module.
151 This internal function searches for the gauge entry in the gauge array.
152 If there is an entry that exactly matches the given key word triple
153 and its end time stamp is zero, then the index of that gauge entry is returned;
154 otherwise, the the number of gauge entries in the array is returned.
156 @param Handle Pointer to environment specific context used
157 to identify the component being measured.
158 @param Token Pointer to a Null-terminated ASCII string
159 that identifies the component being measured.
160 @param Module Pointer to a Null-terminated ASCII string
161 that identifies the module being measured.
163 @retval The index of gauge entry in the array.
168 InternalSearchForGaugeEntry (
169 IN CONST VOID
*Handle
, OPTIONAL
170 IN CONST CHAR8
*Token
, OPTIONAL
171 IN CONST CHAR8
*Module OPTIONAL
175 UINT32 NumberOfEntries
;
176 GAUGE_DATA_ENTRY
*GaugeEntryArray
;
181 if (Module
== NULL
) {
185 NumberOfEntries
= mGaugeData
->NumberOfEntries
;
186 GaugeEntryArray
= (GAUGE_DATA_ENTRY
*) (mGaugeData
+ 1);
188 for (Index
= 0; Index
< NumberOfEntries
; Index
++) {
189 if ((GaugeEntryArray
[Index
].Handle
== (EFI_PHYSICAL_ADDRESS
) (UINTN
) Handle
) &&
190 AsciiStrnCmp (GaugeEntryArray
[Index
].Token
, Token
, PEI_PERFORMANCE_STRING_LENGTH
) == 0 &&
191 AsciiStrnCmp (GaugeEntryArray
[Index
].Module
, Module
, PEI_PERFORMANCE_STRING_LENGTH
) == 0 &&
192 GaugeEntryArray
[Index
].EndTimeStamp
== 0
202 Adds a record at the end of the performance measurement log
203 that records the start time of a performance measurement.
205 Adds a record to the end of the performance measurement log
206 that contains the Handle, Token, and Module.
207 The end time of the new record must be set to zero.
208 If TimeStamp is not zero, then TimeStamp is used to fill in the start time in the record.
209 If TimeStamp is zero, the start time in the record is filled in with the value
210 read from the current time stamp.
212 @param Handle Pointer to environment specific context used
213 to identify the component being measured.
214 @param Token Pointer to a Null-terminated ASCII string
215 that identifies the component being measured.
216 @param Module Pointer to a Null-terminated ASCII string
217 that identifies the module being measured.
218 @param TimeStamp 64-bit time stamp.
220 @retval EFI_SUCCESS The data was read correctly from the device.
221 @retval EFI_OUT_OF_RESOURCES There are not enough resources to record the measurement.
228 IN CONST VOID
*Handle
, OPTIONAL
229 IN CONST CHAR8
*Token
, OPTIONAL
230 IN CONST CHAR8
*Module
, OPTIONAL
234 GAUGE_DATA_ENTRY
*GaugeEntryArray
;
236 UINTN OldGaugeDataSize
;
237 GAUGE_DATA_HEADER
*OldGaugeData
;
240 Index
= mGaugeData
->NumberOfEntries
;
241 if (Index
>= mMaxGaugeRecords
) {
243 // Try to enlarge the scale of gauge arrary.
245 OldGaugeData
= mGaugeData
;
246 OldGaugeDataSize
= sizeof (GAUGE_DATA_HEADER
) + sizeof (GAUGE_DATA_ENTRY
) * mMaxGaugeRecords
;
248 mMaxGaugeRecords
*= 2;
249 GaugeDataSize
= sizeof (GAUGE_DATA_HEADER
) + sizeof (GAUGE_DATA_ENTRY
) * mMaxGaugeRecords
;
251 mGaugeData
= AllocateZeroPool (GaugeDataSize
);
252 if (mGaugeData
== NULL
) {
253 return EFI_OUT_OF_RESOURCES
;
256 // Initialize new data arry and migrate old data one.
258 mGaugeData
= CopyMem (mGaugeData
, OldGaugeData
, OldGaugeDataSize
);
260 FreePool (OldGaugeData
);
263 GaugeEntryArray
= (GAUGE_DATA_ENTRY
*) (mGaugeData
+ 1);
264 GaugeEntryArray
[Index
].Handle
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) Handle
;
267 AsciiStrnCpy (GaugeEntryArray
[Index
].Token
, Token
, DXE_PERFORMANCE_STRING_LENGTH
);
269 if (Module
!= NULL
) {
270 AsciiStrnCpy (GaugeEntryArray
[Index
].Module
, Module
, DXE_PERFORMANCE_STRING_LENGTH
);
273 if (TimeStamp
== 0) {
274 TimeStamp
= GetPerformanceCounter ();
276 GaugeEntryArray
[Index
].StartTimeStamp
= TimeStamp
;
278 mGaugeData
->NumberOfEntries
++;
284 Searches the performance measurement log from the beginning of the log
285 for the first matching record that contains a zero end time and fills in a valid end time.
287 Searches the performance measurement log from the beginning of the log
288 for the first record that matches Handle, Token, and Module and has an end time value of zero.
289 If the record can not be found then return EFI_NOT_FOUND.
290 If the record is found and TimeStamp is not zero,
291 then the end time in the record is filled in with the value specified by TimeStamp.
292 If the record is found and TimeStamp is zero, then the end time in the matching record
293 is filled in with the current time stamp value.
295 @param Handle Pointer to environment specific context used
296 to identify the component being measured.
297 @param Token Pointer to a Null-terminated ASCII string
298 that identifies the component being measured.
299 @param Module Pointer to a Null-terminated ASCII string
300 that identifies the module being measured.
301 @param TimeStamp 64-bit time stamp.
303 @retval EFI_SUCCESS The end of the measurement was recorded.
304 @retval EFI_NOT_FOUND The specified measurement record could not be found.
311 IN CONST VOID
*Handle
, OPTIONAL
312 IN CONST CHAR8
*Token
, OPTIONAL
313 IN CONST CHAR8
*Module
, OPTIONAL
317 GAUGE_DATA_ENTRY
*GaugeEntryArray
;
320 if (TimeStamp
== 0) {
321 TimeStamp
= GetPerformanceCounter ();
324 Index
= InternalSearchForGaugeEntry (Handle
, Token
, Module
);
325 if (Index
>= mGaugeData
->NumberOfEntries
) {
326 return EFI_NOT_FOUND
;
328 GaugeEntryArray
= (GAUGE_DATA_ENTRY
*) (mGaugeData
+ 1);
329 GaugeEntryArray
[Index
].EndTimeStamp
= TimeStamp
;
335 Retrieves a previously logged performance measurement.
337 Retrieves the performance log entry from the performance log specified by LogEntryKey.
338 If it stands for a valid entry, then EFI_SUCCESS is returned and
339 GaugeDataEntry stores the pointer to that entry.
341 @param LogEntryKey The key for the previous performance measurement log entry.
342 If 0, then the first performance measurement log entry is retrieved.
343 @param GaugeDataEntry The indirect pointer to the gauge data entry specified by LogEntryKey
344 if the retrieval is successful.
346 @retval EFI_SUCCESS The GuageDataEntry is successfuly found based on LogEntryKey.
347 @retval EFI_NOT_FOUND The LogEntryKey is the last entry (equals to the total entry number).
348 @retval EFI_INVALIDE_PARAMETER The LogEntryKey is not a valid entry (greater than the total entry number).
349 @retval EFI_INVALIDE_PARAMETER GaugeDataEntry is NULL.
356 IN UINTN LogEntryKey
,
357 OUT GAUGE_DATA_ENTRY
**GaugeDataEntry
360 UINTN NumberOfEntries
;
361 GAUGE_DATA_ENTRY
*LogEntryArray
;
363 NumberOfEntries
= (UINTN
) (mGaugeData
->NumberOfEntries
);
364 if (LogEntryKey
> NumberOfEntries
) {
365 return EFI_INVALID_PARAMETER
;
367 if (LogEntryKey
== NumberOfEntries
) {
368 return EFI_NOT_FOUND
;
371 LogEntryArray
= (GAUGE_DATA_ENTRY
*) (mGaugeData
+ 1);
373 if (GaugeDataEntry
== NULL
) {
374 return EFI_INVALID_PARAMETER
;
376 *GaugeDataEntry
= &LogEntryArray
[LogEntryKey
];
382 Dumps all the PEI performance log to DXE performance gauge array.
384 This internal function dumps all the PEI performance log to the DXE performance gauge array.
385 It retrieves the optional GUID HOB for PEI performance and then saves the performance data
386 to DXE performance data structures.
391 InternalGetPeiPerformance (
395 EFI_HOB_GUID_TYPE
*GuidHob
;
396 PEI_PERFORMANCE_LOG_HEADER
*LogHob
;
397 PEI_PERFORMANCE_LOG_ENTRY
*LogEntryArray
;
398 GAUGE_DATA_ENTRY
*GaugeEntryArray
;
400 UINT32 NumberOfEntries
;
403 GaugeEntryArray
= (GAUGE_DATA_ENTRY
*) (mGaugeData
+ 1);
406 // Dump PEI Log Entries to DXE Guage Data structure.
408 GuidHob
= GetFirstGuidHob (&gPeiPerformanceHobGuid
);
409 if (GuidHob
!= NULL
) {
410 LogHob
= GET_GUID_HOB_DATA (GuidHob
);
411 LogEntryArray
= (PEI_PERFORMANCE_LOG_ENTRY
*) (LogHob
+ 1);
412 GaugeEntryArray
= (GAUGE_DATA_ENTRY
*) (mGaugeData
+ 1);
414 NumberOfEntries
= LogHob
->NumberOfEntries
;
415 for (Index
= 0; Index
< NumberOfEntries
; Index
++) {
416 GaugeEntryArray
[Index
].Handle
= LogEntryArray
[Index
].Handle
;
417 AsciiStrnCpy (GaugeEntryArray
[Index
].Token
, LogEntryArray
[Index
].Token
, DXE_PERFORMANCE_STRING_LENGTH
);
418 AsciiStrnCpy (GaugeEntryArray
[Index
].Module
, LogEntryArray
[Index
].Module
, DXE_PERFORMANCE_STRING_LENGTH
);
419 GaugeEntryArray
[Index
].StartTimeStamp
= LogEntryArray
[Index
].StartTimeStamp
;
420 GaugeEntryArray
[Index
].EndTimeStamp
= LogEntryArray
[Index
].EndTimeStamp
;
423 mGaugeData
->NumberOfEntries
= NumberOfEntries
;
427 The constructor function initializes Performance infrastructure for DXE phase.
429 The constructor function publishes Performance protocol, allocates memory to log DXE performance
430 and merges PEI performance data to DXE performance log.
431 It will ASSERT() if one of these operations fails and it will always return EFI_SUCCESS.
433 @param ImageHandle The firmware allocated handle for the EFI image.
434 @param SystemTable A pointer to the EFI System Table.
436 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
441 DxeCorePerformanceLibConstructor (
442 IN EFI_HANDLE ImageHandle
,
443 IN EFI_SYSTEM_TABLE
*SystemTable
448 if (!PerformanceMeasurementEnabled ()) {
450 // Do not initialize performance infrastructure if not required.
455 // Install the protocol interfaces.
457 Status
= gBS
->InstallProtocolInterface (
459 &gPerformanceProtocolGuid
,
460 EFI_NATIVE_INTERFACE
,
461 &mPerformanceInterface
463 ASSERT_EFI_ERROR (Status
);
465 mMaxGaugeRecords
= INIT_DXE_GAUGE_DATA_ENTRIES
+ PcdGet8 (PcdMaxPeiPerformanceLogEntries
);
467 mGaugeData
= AllocateZeroPool (sizeof (GAUGE_DATA_HEADER
) + (sizeof (GAUGE_DATA_ENTRY
) * mMaxGaugeRecords
));
468 ASSERT (mGaugeData
!= NULL
);
470 InternalGetPeiPerformance ();
476 Adds a record at the end of the performance measurement log
477 that records the start time of a performance measurement.
479 Adds a record to the end of the performance measurement log
480 that contains the Handle, Token, and Module.
481 The end time of the new record must be set to zero.
482 If TimeStamp is not zero, then TimeStamp is used to fill in the start time in the record.
483 If TimeStamp is zero, the start time in the record is filled in with the value
484 read from the current time stamp.
486 @param Handle Pointer to environment specific context used
487 to identify the component being measured.
488 @param Token Pointer to a Null-terminated ASCII string
489 that identifies the component being measured.
490 @param Module Pointer to a Null-terminated ASCII string
491 that identifies the module being measured.
492 @param TimeStamp 64-bit time stamp.
494 @retval RETURN_SUCCESS The start of the measurement was recorded.
495 @retval RETURN_OUT_OF_RESOURCES There are not enough resources to record the measurement.
500 StartPerformanceMeasurement (
501 IN CONST VOID
*Handle
, OPTIONAL
502 IN CONST CHAR8
*Token
, OPTIONAL
503 IN CONST CHAR8
*Module
, OPTIONAL
509 Status
= StartGauge (Handle
, Token
, Module
, TimeStamp
);
510 return (RETURN_STATUS
) Status
;
514 Searches the performance measurement log from the beginning of the log
515 for the first matching record that contains a zero end time and fills in a valid end time.
517 Searches the performance measurement log from the beginning of the log
518 for the first record that matches Handle, Token, and Module and has an end time value of zero.
519 If the record can not be found then return RETURN_NOT_FOUND.
520 If the record is found and TimeStamp is not zero,
521 then the end time in the record is filled in with the value specified by TimeStamp.
522 If the record is found and TimeStamp is zero, then the end time in the matching record
523 is filled in with the current time stamp value.
525 @param Handle Pointer to environment specific context used
526 to identify the component being measured.
527 @param Token Pointer to a Null-terminated ASCII string
528 that identifies the component being measured.
529 @param Module Pointer to a Null-terminated ASCII string
530 that identifies the module being measured.
531 @param TimeStamp 64-bit time stamp.
533 @retval RETURN_SUCCESS The end of the measurement was recorded.
534 @retval RETURN_NOT_FOUND The specified measurement record could not be found.
539 EndPerformanceMeasurement (
540 IN CONST VOID
*Handle
, OPTIONAL
541 IN CONST CHAR8
*Token
, OPTIONAL
542 IN CONST CHAR8
*Module
, OPTIONAL
548 Status
= EndGauge (Handle
, Token
, Module
, TimeStamp
);
549 return (RETURN_STATUS
) Status
;
553 Attempts to retrieve a performance measurement log entry from the performance measurement log.
555 Attempts to retrieve the performance log entry specified by LogEntryKey. If LogEntryKey is
556 zero on entry, then an attempt is made to retrieve the first entry from the performance log,
557 and the key for the second entry in the log is returned. If the performance log is empty,
558 then no entry is retrieved and zero is returned. If LogEntryKey is not zero, then the performance
559 log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is
560 returned. If LogEntryKey is the key for the last entry in the log, then the last log entry is
561 retrieved and an implementation specific non-zero key value that specifies the end of the performance
562 log is returned. If LogEntryKey is equal this implementation specific non-zero key value, then no entry
563 is retrieved and zero is returned. In the cases where a performance log entry can be returned,
564 the log entry is returned in Handle, Token, Module, StartTimeStamp, and EndTimeStamp.
565 If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().
566 If Handle is NULL, then ASSERT().
567 If Token is NULL, then ASSERT().
568 If Module is NULL, then ASSERT().
569 If StartTimeStamp is NULL, then ASSERT().
570 If EndTimeStamp is NULL, then ASSERT().
572 @param LogEntryKey On entry, the key of the performance measurement log entry to retrieve.
573 0, then the first performance measurement log entry is retrieved.
574 On exit, the key of the next performance lof entry entry.
575 @param Handle Pointer to environment specific context used to identify the component
577 @param Token Pointer to a Null-terminated ASCII string that identifies the component
579 @param Module Pointer to a Null-terminated ASCII string that identifies the module
581 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
583 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
586 @return The key for the next performance log entry (in general case).
591 GetPerformanceMeasurement (
592 IN UINTN LogEntryKey
,
593 OUT CONST VOID
**Handle
,
594 OUT CONST CHAR8
**Token
,
595 OUT CONST CHAR8
**Module
,
596 OUT UINT64
*StartTimeStamp
,
597 OUT UINT64
*EndTimeStamp
601 GAUGE_DATA_ENTRY
*GaugeData
;
603 ASSERT (Handle
!= NULL
);
604 ASSERT (Token
!= NULL
);
605 ASSERT (Module
!= NULL
);
606 ASSERT (StartTimeStamp
!= NULL
);
607 ASSERT (EndTimeStamp
!= NULL
);
609 Status
= GetGauge (LogEntryKey
++, &GaugeData
);
612 // Make sure that LogEntryKey is a valid log entry key,
614 ASSERT (Status
!= EFI_INVALID_PARAMETER
);
616 if (EFI_ERROR (Status
)) {
618 // The LogEntryKey is the last entry (equals to the total entry number).
623 ASSERT (GaugeData
!= NULL
);
625 *Handle
= (VOID
*) (UINTN
) GaugeData
->Handle
;
626 *Token
= GaugeData
->Token
;
627 *Module
= GaugeData
->Module
;
628 *StartTimeStamp
= GaugeData
->StartTimeStamp
;
629 *EndTimeStamp
= GaugeData
->EndTimeStamp
;
635 Returns TRUE if the performance measurement macros are enabled.
637 This function returns TRUE if the PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
638 PcdPerformanceLibraryPropertyMask is set. Otherwise FALSE is returned.
640 @retval TRUE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
641 PcdPerformanceLibraryPropertyMask is set.
642 @retval FALSE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
643 PcdPerformanceLibraryPropertyMask is clear.
648 PerformanceMeasurementEnabled (
652 return (BOOLEAN
) ((PcdGet8(PcdPerformanceLibraryPropertyMask
) & PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED
) != 0);