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.
61 IN CONST VOID
*Handle
, OPTIONAL
62 IN CONST CHAR8
*Token
, OPTIONAL
63 IN CONST CHAR8
*Module
, OPTIONAL
68 Searches the performance measurement log from the beginning of the log
69 for the first matching record that contains a zero end time and fills in a valid end time.
71 Searches the performance measurement log from the beginning of the log
72 for the first record that matches Handle, Token, and Module and has an end time value of zero.
73 If the record can not be found then return EFI_NOT_FOUND.
74 If the record is found and TimeStamp is not zero,
75 then the end time in the record is filled in with the value specified by TimeStamp.
76 If the record is found and TimeStamp is zero, then the end time in the matching record
77 is filled in with the current time stamp value.
79 @param Handle Pointer to environment specific context used
80 to identify the component being measured.
81 @param Token Pointer to a Null-terminated ASCII string
82 that identifies the component being measured.
83 @param Module Pointer to a Null-terminated ASCII string
84 that identifies the module being measured.
85 @param TimeStamp 64-bit time stamp.
87 @retval EFI_SUCCESS The end of the measurement was recorded.
88 @retval EFI_NOT_FOUND The specified measurement record could not be found.
95 IN CONST VOID
*Handle
, OPTIONAL
96 IN CONST CHAR8
*Token
, OPTIONAL
97 IN CONST CHAR8
*Module
, OPTIONAL
102 Retrieves a previously logged performance measurement.
104 Retrieves the performance log entry from the performance log specified by LogEntryKey.
105 If it stands for a valid entry, then EFI_SUCCESS is returned and
106 GaugeDataEntry stores the pointer to that entry.
108 @param LogEntryKey The key for the previous performance measurement log entry.
109 If 0, then the first performance measurement log entry is retrieved.
110 @param GaugeDataEntry The indirect pointer to the gauge data entry specified by LogEntryKey
111 if the retrieval is successful.
113 @retval EFI_SUCCESS The GuageDataEntry is successfuly found based on LogEntryKey.
114 @retval EFI_NOT_FOUND The LogEntryKey is the last entry (equals to the total entry number).
115 @retval EFI_INVALIDE_PARAMETER The LogEntryKey is not a valid entry (greater than the total entry number).
116 @retval EFI_INVALIDE_PARAMETER GaugeDataEntry is NULL.
123 IN UINTN LogEntryKey
,
124 OUT GAUGE_DATA_ENTRY
**GaugeDataEntry
128 // Definition for global variables.
130 STATIC GAUGE_DATA_HEADER
*mGaugeData
;
131 STATIC UINT32 mMaxGaugeRecords
;
133 EFI_HANDLE mHandle
= NULL
;
134 PERFORMANCE_PROTOCOL mPerformanceInterface
= {
142 Searches in the gauge array with keyword Handle, Token and Module.
144 This internal function searches for the gauge entry in the gauge array.
145 If there is an entry that exactly matches the given key word triple
146 and its end time stamp is zero, then the index of that gauge entry is returned;
147 otherwise, the the number of gauge entries in the array is returned.
149 @param Handle Pointer to environment specific context used
150 to identify the component being measured.
151 @param Token Pointer to a Null-terminated ASCII string
152 that identifies the component being measured.
153 @param Module Pointer to a Null-terminated ASCII string
154 that identifies the module being measured.
156 @retval The index of gauge entry in the array.
161 InternalSearchForGaugeEntry (
162 IN CONST VOID
*Handle
, OPTIONAL
163 IN CONST CHAR8
*Token
, OPTIONAL
164 IN CONST CHAR8
*Module OPTIONAL
168 UINT32 NumberOfEntries
;
169 GAUGE_DATA_ENTRY
*GaugeEntryArray
;
174 if (Module
== NULL
) {
178 NumberOfEntries
= mGaugeData
->NumberOfEntries
;
179 GaugeEntryArray
= (GAUGE_DATA_ENTRY
*) (mGaugeData
+ 1);
181 for (Index
= 0; Index
< NumberOfEntries
; Index
++) {
182 if ((GaugeEntryArray
[Index
].Handle
== (EFI_PHYSICAL_ADDRESS
) (UINTN
) Handle
) &&
183 AsciiStrnCmp (GaugeEntryArray
[Index
].Token
, Token
, PEI_PERFORMANCE_STRING_LENGTH
) == 0 &&
184 AsciiStrnCmp (GaugeEntryArray
[Index
].Module
, Module
, PEI_PERFORMANCE_STRING_LENGTH
) == 0 &&
185 GaugeEntryArray
[Index
].EndTimeStamp
== 0
195 Adds a record at the end of the performance measurement log
196 that records the start time of a performance measurement.
198 Adds a record to the end of the performance measurement log
199 that contains the Handle, Token, and Module.
200 The end time of the new record must be set to zero.
201 If TimeStamp is not zero, then TimeStamp is used to fill in the start time in the record.
202 If TimeStamp is zero, the start time in the record is filled in with the value
203 read from the current time stamp.
205 @param Handle Pointer to environment specific context used
206 to identify the component being measured.
207 @param Token Pointer to a Null-terminated ASCII string
208 that identifies the component being measured.
209 @param Module Pointer to a Null-terminated ASCII string
210 that identifies the module being measured.
211 @param TimeStamp 64-bit time stamp.
213 @retval EFI_SUCCESS The data was read correctly from the device.
214 @retval EFI_OUT_OF_RESOURCES There are not enough resources to record the measurement.
221 IN CONST VOID
*Handle
, OPTIONAL
222 IN CONST CHAR8
*Token
, OPTIONAL
223 IN CONST CHAR8
*Module
, OPTIONAL
227 GAUGE_DATA_ENTRY
*GaugeEntryArray
;
229 UINTN OldGaugeDataSize
;
230 GAUGE_DATA_HEADER
*OldGaugeData
;
233 Index
= mGaugeData
->NumberOfEntries
;
234 if (Index
>= mMaxGaugeRecords
) {
236 // Try to enlarge the scale of gauge arrary.
238 OldGaugeData
= mGaugeData
;
239 OldGaugeDataSize
= sizeof (GAUGE_DATA_HEADER
) + sizeof (GAUGE_DATA_ENTRY
) * mMaxGaugeRecords
;
241 mMaxGaugeRecords
*= 2;
242 GaugeDataSize
= sizeof (GAUGE_DATA_HEADER
) + sizeof (GAUGE_DATA_ENTRY
) * mMaxGaugeRecords
;
244 mGaugeData
= AllocateZeroPool (GaugeDataSize
);
245 if (mGaugeData
== NULL
) {
246 return EFI_OUT_OF_RESOURCES
;
249 // Initialize new data arry and migrate old data one.
251 mGaugeData
= CopyMem (mGaugeData
, OldGaugeData
, OldGaugeDataSize
);
253 FreePool (OldGaugeData
);
256 GaugeEntryArray
= (GAUGE_DATA_ENTRY
*) (mGaugeData
+ 1);
257 GaugeEntryArray
[Index
].Handle
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) Handle
;
260 AsciiStrnCpy (GaugeEntryArray
[Index
].Token
, Token
, DXE_PERFORMANCE_STRING_LENGTH
);
262 if (Module
!= NULL
) {
263 AsciiStrnCpy (GaugeEntryArray
[Index
].Module
, Module
, DXE_PERFORMANCE_STRING_LENGTH
);
266 if (TimeStamp
== 0) {
267 TimeStamp
= GetPerformanceCounter ();
269 GaugeEntryArray
[Index
].StartTimeStamp
= TimeStamp
;
271 mGaugeData
->NumberOfEntries
++;
277 Searches the performance measurement log from the beginning of the log
278 for the first matching record that contains a zero end time and fills in a valid end time.
280 Searches the performance measurement log from the beginning of the log
281 for the first record that matches Handle, Token, and Module and has an end time value of zero.
282 If the record can not be found then return EFI_NOT_FOUND.
283 If the record is found and TimeStamp is not zero,
284 then the end time in the record is filled in with the value specified by TimeStamp.
285 If the record is found and TimeStamp is zero, then the end time in the matching record
286 is filled in with the current time stamp value.
288 @param Handle Pointer to environment specific context used
289 to identify the component being measured.
290 @param Token Pointer to a Null-terminated ASCII string
291 that identifies the component being measured.
292 @param Module Pointer to a Null-terminated ASCII string
293 that identifies the module being measured.
294 @param TimeStamp 64-bit time stamp.
296 @retval EFI_SUCCESS The end of the measurement was recorded.
297 @retval EFI_NOT_FOUND The specified measurement record could not be found.
304 IN CONST VOID
*Handle
, OPTIONAL
305 IN CONST CHAR8
*Token
, OPTIONAL
306 IN CONST CHAR8
*Module
, OPTIONAL
310 GAUGE_DATA_ENTRY
*GaugeEntryArray
;
313 if (TimeStamp
== 0) {
314 TimeStamp
= GetPerformanceCounter ();
317 Index
= InternalSearchForGaugeEntry (Handle
, Token
, Module
);
318 if (Index
>= mGaugeData
->NumberOfEntries
) {
319 return EFI_NOT_FOUND
;
321 GaugeEntryArray
= (GAUGE_DATA_ENTRY
*) (mGaugeData
+ 1);
322 GaugeEntryArray
[Index
].EndTimeStamp
= TimeStamp
;
328 Retrieves a previously logged performance measurement.
330 Retrieves the performance log entry from the performance log specified by LogEntryKey.
331 If it stands for a valid entry, then EFI_SUCCESS is returned and
332 GaugeDataEntry stores the pointer to that entry.
334 @param LogEntryKey The key for the previous performance measurement log entry.
335 If 0, then the first performance measurement log entry is retrieved.
336 @param GaugeDataEntry The indirect pointer to the gauge data entry specified by LogEntryKey
337 if the retrieval is successful.
339 @retval EFI_SUCCESS The GuageDataEntry is successfuly found based on LogEntryKey.
340 @retval EFI_NOT_FOUND The LogEntryKey is the last entry (equals to the total entry number).
341 @retval EFI_INVALIDE_PARAMETER The LogEntryKey is not a valid entry (greater than the total entry number).
342 @retval EFI_INVALIDE_PARAMETER GaugeDataEntry is NULL.
349 IN UINTN LogEntryKey
,
350 OUT GAUGE_DATA_ENTRY
**GaugeDataEntry
353 UINTN NumberOfEntries
;
354 GAUGE_DATA_ENTRY
*LogEntryArray
;
356 NumberOfEntries
= (UINTN
) (mGaugeData
->NumberOfEntries
);
357 if (LogEntryKey
> NumberOfEntries
) {
358 return EFI_INVALID_PARAMETER
;
360 if (LogEntryKey
== NumberOfEntries
) {
361 return EFI_NOT_FOUND
;
364 LogEntryArray
= (GAUGE_DATA_ENTRY
*) (mGaugeData
+ 1);
366 if (GaugeDataEntry
== NULL
) {
367 return EFI_INVALID_PARAMETER
;
369 *GaugeDataEntry
= &LogEntryArray
[LogEntryKey
];
375 Dumps all the PEI performance log to DXE performance gauge array.
377 This internal function dumps all the PEI performance log to the DXE performance gauge array.
378 It retrieves the optional GUID HOB for PEI performance and then saves the performance data
379 to DXE performance data structures.
384 InternalGetPeiPerformance (
388 EFI_HOB_GUID_TYPE
*GuidHob
;
389 PEI_PERFORMANCE_LOG_HEADER
*LogHob
;
390 PEI_PERFORMANCE_LOG_ENTRY
*LogEntryArray
;
391 GAUGE_DATA_ENTRY
*GaugeEntryArray
;
393 UINT32 NumberOfEntries
;
396 GaugeEntryArray
= (GAUGE_DATA_ENTRY
*) (mGaugeData
+ 1);
399 // Dump PEI Log Entries to DXE Guage Data structure.
401 GuidHob
= GetFirstGuidHob (&gPeiPerformanceHobGuid
);
402 if (GuidHob
!= NULL
) {
403 LogHob
= GET_GUID_HOB_DATA (GuidHob
);
404 LogEntryArray
= (PEI_PERFORMANCE_LOG_ENTRY
*) (LogHob
+ 1);
405 GaugeEntryArray
= (GAUGE_DATA_ENTRY
*) (mGaugeData
+ 1);
407 NumberOfEntries
= LogHob
->NumberOfEntries
;
408 for (Index
= 0; Index
< NumberOfEntries
; Index
++) {
409 GaugeEntryArray
[Index
].Handle
= LogEntryArray
[Index
].Handle
;
410 AsciiStrnCpy (GaugeEntryArray
[Index
].Token
, LogEntryArray
[Index
].Token
, DXE_PERFORMANCE_STRING_LENGTH
);
411 AsciiStrnCpy (GaugeEntryArray
[Index
].Module
, LogEntryArray
[Index
].Module
, DXE_PERFORMANCE_STRING_LENGTH
);
412 GaugeEntryArray
[Index
].StartTimeStamp
= LogEntryArray
[Index
].StartTimeStamp
;
413 GaugeEntryArray
[Index
].EndTimeStamp
= LogEntryArray
[Index
].EndTimeStamp
;
416 mGaugeData
->NumberOfEntries
= NumberOfEntries
;
420 The constructor function initializes Performance infrastructure for DXE phase.
422 The constructor function publishes Performance protocol, allocates memory to log DXE performance
423 and merges PEI performance data to DXE performance log.
424 It will ASSERT() if one of these operations fails and it will always return EFI_SUCCESS.
426 @param ImageHandle The firmware allocated handle for the EFI image.
427 @param SystemTable A pointer to the EFI System Table.
429 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
434 DxeCorePerformanceLibConstructor (
435 IN EFI_HANDLE ImageHandle
,
436 IN EFI_SYSTEM_TABLE
*SystemTable
441 if (!PerformanceMeasurementEnabled ()) {
443 // Do not initialize performance infrastructure if not required.
448 // Install the protocol interfaces.
450 Status
= gBS
->InstallProtocolInterface (
452 &gPerformanceProtocolGuid
,
453 EFI_NATIVE_INTERFACE
,
454 &mPerformanceInterface
456 ASSERT_EFI_ERROR (Status
);
458 mMaxGaugeRecords
= INIT_DXE_GAUGE_DATA_ENTRIES
+ PcdGet8 (PcdMaxPeiPerformanceLogEntries
);
460 mGaugeData
= AllocateZeroPool (sizeof (GAUGE_DATA_HEADER
) + (sizeof (GAUGE_DATA_ENTRY
) * mMaxGaugeRecords
));
461 ASSERT (mGaugeData
!= NULL
);
463 InternalGetPeiPerformance ();
469 Adds a record at the end of the performance measurement log
470 that records the start time of a performance measurement.
472 Adds a record to the end of the performance measurement log
473 that contains the Handle, Token, and Module.
474 The end time of the new record must be set to zero.
475 If TimeStamp is not zero, then TimeStamp is used to fill in the start time in the record.
476 If TimeStamp is zero, the start time in the record is filled in with the value
477 read from the current time stamp.
479 @param Handle Pointer to environment specific context used
480 to identify the component being measured.
481 @param Token Pointer to a Null-terminated ASCII string
482 that identifies the component being measured.
483 @param Module Pointer to a Null-terminated ASCII string
484 that identifies the module being measured.
485 @param TimeStamp 64-bit time stamp.
487 @retval RETURN_SUCCESS The start of the measurement was recorded.
488 @retval RETURN_OUT_OF_RESOURCES There are not enough resources to record the measurement.
493 StartPerformanceMeasurement (
494 IN CONST VOID
*Handle
, OPTIONAL
495 IN CONST CHAR8
*Token
, OPTIONAL
496 IN CONST CHAR8
*Module
, OPTIONAL
502 Status
= StartGauge (Handle
, Token
, Module
, TimeStamp
);
503 return (RETURN_STATUS
) Status
;
507 Searches the performance measurement log from the beginning of the log
508 for the first matching record that contains a zero end time and fills in a valid end time.
510 Searches the performance measurement log from the beginning of the log
511 for the first record that matches Handle, Token, and Module and has an end time value of zero.
512 If the record can not be found then return RETURN_NOT_FOUND.
513 If the record is found and TimeStamp is not zero,
514 then the end time in the record is filled in with the value specified by TimeStamp.
515 If the record is found and TimeStamp is zero, then the end time in the matching record
516 is filled in with the current time stamp value.
518 @param Handle Pointer to environment specific context used
519 to identify the component being measured.
520 @param Token Pointer to a Null-terminated ASCII string
521 that identifies the component being measured.
522 @param Module Pointer to a Null-terminated ASCII string
523 that identifies the module being measured.
524 @param TimeStamp 64-bit time stamp.
526 @retval RETURN_SUCCESS The end of the measurement was recorded.
527 @retval RETURN_NOT_FOUND The specified measurement record could not be found.
532 EndPerformanceMeasurement (
533 IN CONST VOID
*Handle
, OPTIONAL
534 IN CONST CHAR8
*Token
, OPTIONAL
535 IN CONST CHAR8
*Module
, OPTIONAL
541 Status
= EndGauge (Handle
, Token
, Module
, TimeStamp
);
542 return (RETURN_STATUS
) Status
;
546 Attempts to retrieve a performance measurement log entry from the performance measurement log.
548 Attempts to retrieve the performance log entry specified by LogEntryKey. If LogEntryKey is
549 zero on entry, then an attempt is made to retrieve the first entry from the performance log,
550 and the key for the second entry in the log is returned. If the performance log is empty,
551 then no entry is retrieved and zero is returned. If LogEntryKey is not zero, then the performance
552 log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is
553 returned. If LogEntryKey is the key for the last entry in the log, then the last log entry is
554 retrieved and an implementation specific non-zero key value that specifies the end of the performance
555 log is returned. If LogEntryKey is equal this implementation specific non-zero key value, then no entry
556 is retrieved and zero is returned. In the cases where a performance log entry can be returned,
557 the log entry is returned in Handle, Token, Module, StartTimeStamp, and EndTimeStamp.
558 If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().
559 If Handle is NULL, then ASSERT().
560 If Token is NULL, then ASSERT().
561 If Module is NULL, then ASSERT().
562 If StartTimeStamp is NULL, then ASSERT().
563 If EndTimeStamp is NULL, then ASSERT().
565 @param LogEntryKey On entry, the key of the performance measurement log entry to retrieve.
566 0, then the first performance measurement log entry is retrieved.
567 On exit, the key of the next performance lof entry entry.
568 @param Handle Pointer to environment specific context used to identify the component
570 @param Token Pointer to a Null-terminated ASCII string that identifies the component
572 @param Module Pointer to a Null-terminated ASCII string that identifies the module
574 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
576 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
579 @return The key for the next performance log entry (in general case).
584 GetPerformanceMeasurement (
585 IN UINTN LogEntryKey
,
586 OUT CONST VOID
**Handle
,
587 OUT CONST CHAR8
**Token
,
588 OUT CONST CHAR8
**Module
,
589 OUT UINT64
*StartTimeStamp
,
590 OUT UINT64
*EndTimeStamp
594 GAUGE_DATA_ENTRY
*GaugeData
;
598 ASSERT (Handle
!= NULL
);
599 ASSERT (Token
!= NULL
);
600 ASSERT (Module
!= NULL
);
601 ASSERT (StartTimeStamp
!= NULL
);
602 ASSERT (EndTimeStamp
!= NULL
);
604 Status
= GetGauge (LogEntryKey
++, &GaugeData
);
607 // Make sure that LogEntryKey is a valid log entry key,
609 ASSERT (Status
!= EFI_INVALID_PARAMETER
);
611 if (EFI_ERROR (Status
)) {
613 // The LogEntryKey is the last entry (equals to the total entry number).
618 ASSERT (GaugeData
!= NULL
);
620 *Handle
= (VOID
*) (UINTN
) GaugeData
->Handle
;
621 *Token
= GaugeData
->Token
;
622 *Module
= GaugeData
->Module
;
623 *StartTimeStamp
= GaugeData
->StartTimeStamp
;
624 *EndTimeStamp
= GaugeData
->EndTimeStamp
;
630 Returns TRUE if the performance measurement macros are enabled.
632 This function returns TRUE if the PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
633 PcdPerformanceLibraryPropertyMask is set. Otherwise FALSE is returned.
635 @retval TRUE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
636 PcdPerformanceLibraryPropertyMask is set.
637 @retval FALSE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
638 PcdPerformanceLibraryPropertyMask is clear.
643 PerformanceMeasurementEnabled (
647 return (BOOLEAN
) ((PcdGet8(PcdPerformanceLibraryPropertyMask
) & PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED
) != 0);