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 // Interface declarations for Performance Protocol.
26 Adds a record at the end of the performance measurement log
27 that records the start time of a performance measurement.
29 Adds a record to the end of the performance measurement log
30 that contains the Handle, Token, and Module.
31 The end time of the new record must be set to zero.
32 If TimeStamp is not zero, then TimeStamp is used to fill in the start time in the record.
33 If TimeStamp is zero, the start time in the record is filled in with the value
34 read from the current time stamp.
36 @param Handle Pointer to environment specific context used
37 to identify the component being measured.
38 @param Token Pointer to a Null-terminated ASCII string
39 that identifies the component being measured.
40 @param Module Pointer to a Null-terminated ASCII string
41 that identifies the module being measured.
42 @param TimeStamp 64-bit time stamp.
44 @retval EFI_SUCCESS The data was read correctly from the device.
45 @retval EFI_OUT_OF_RESOURCES There are not enough resources to record the measurement.
52 IN CONST VOID
*Handle
, OPTIONAL
53 IN CONST CHAR8
*Token
, OPTIONAL
54 IN CONST CHAR8
*Module
, OPTIONAL
59 Searches the performance measurement log from the beginning of the log
60 for the first matching record that contains a zero end time and fills in a valid end time.
62 Searches the performance measurement log from the beginning of the log
63 for the first record that matches Handle, Token, and Module and has an end time value of zero.
64 If the record can not be found then return EFI_NOT_FOUND.
65 If the record is found and TimeStamp is not zero,
66 then the end time in the record is filled in with the value specified by TimeStamp.
67 If the record is found and TimeStamp is zero, then the end time in the matching record
68 is filled in with the current time stamp value.
70 @param Handle Pointer to environment specific context used
71 to identify the component being measured.
72 @param Token Pointer to a Null-terminated ASCII string
73 that identifies the component being measured.
74 @param Module Pointer to a Null-terminated ASCII string
75 that identifies the module being measured.
76 @param TimeStamp 64-bit time stamp.
78 @retval EFI_SUCCESS The end of the measurement was recorded.
79 @retval EFI_NOT_FOUND The specified measurement record could not be found.
86 IN CONST VOID
*Handle
, OPTIONAL
87 IN CONST CHAR8
*Token
, OPTIONAL
88 IN CONST CHAR8
*Module
, OPTIONAL
93 Retrieves a previously logged performance measurement.
95 Retrieves the performance log entry from the performance log specified by LogEntryKey.
96 If it stands for a valid entry, then EFI_SUCCESS is returned and
97 GaugeDataEntry stores the pointer to that entry.
99 @param LogEntryKey The key for the previous performance measurement log entry.
100 If 0, then the first performance measurement log entry is retrieved.
101 @param GaugeDataEntry The indirect pointer to the gauge data entry specified by LogEntryKey
102 if the retrieval is successful.
104 @retval EFI_SUCCESS The GuageDataEntry is successfuly found based on LogEntryKey.
105 @retval EFI_NOT_FOUND The LogEntryKey is the last entry (equals to the total entry number).
106 @retval EFI_INVALIDE_PARAMETER The LogEntryKey is not a valid entry (greater than the total entry number).
107 @retval EFI_INVALIDE_PARAMETER GaugeDataEntry is NULL.
114 IN UINTN LogEntryKey
,
115 OUT GAUGE_DATA_ENTRY
**GaugeDataEntry
119 // Definition for global variables.
121 STATIC GAUGE_DATA_HEADER
*mGaugeData
;
122 STATIC UINT32 mMaxGaugeRecords
;
124 EFI_HANDLE mHandle
= NULL
;
125 PERFORMANCE_PROTOCOL mPerformanceInterface
= {
133 Searches in the gauge array with keyword Handle, Token and Module.
135 This internal function searches for the gauge entry in the gauge array.
136 If there is an entry that exactly matches the given key word triple
137 and its end time stamp is zero, then the index of that gauge entry is returned;
138 otherwise, the the number of gauge entries in the array is returned.
140 @param Handle Pointer to environment specific context used
141 to identify the component being measured.
142 @param Token Pointer to a Null-terminated ASCII string
143 that identifies the component being measured.
144 @param Module Pointer to a Null-terminated ASCII string
145 that identifies the module being measured.
147 @retval The index of gauge entry in the array.
152 InternalSearchForGaugeEntry (
153 IN CONST VOID
*Handle
, OPTIONAL
154 IN CONST CHAR8
*Token
, OPTIONAL
155 IN CONST CHAR8
*Module OPTIONAL
159 UINT32 NumberOfEntries
;
160 GAUGE_DATA_ENTRY
*GaugeEntryArray
;
165 if (Module
== NULL
) {
169 NumberOfEntries
= mGaugeData
->NumberOfEntries
;
170 GaugeEntryArray
= (GAUGE_DATA_ENTRY
*) (mGaugeData
+ 1);
172 for (Index
= 0; Index
< NumberOfEntries
; Index
++) {
173 if ((GaugeEntryArray
[Index
].Handle
== (EFI_PHYSICAL_ADDRESS
) (UINTN
) Handle
) &&
174 AsciiStrnCmp (GaugeEntryArray
[Index
].Token
, Token
, PEI_PERFORMANCE_STRING_LENGTH
) == 0 &&
175 AsciiStrnCmp (GaugeEntryArray
[Index
].Module
, Module
, PEI_PERFORMANCE_STRING_LENGTH
) == 0 &&
176 GaugeEntryArray
[Index
].EndTimeStamp
== 0
186 Adds a record at the end of the performance measurement log
187 that records the start time of a performance measurement.
189 Adds a record to the end of the performance measurement log
190 that contains the Handle, Token, and Module.
191 The end time of the new record must be set to zero.
192 If TimeStamp is not zero, then TimeStamp is used to fill in the start time in the record.
193 If TimeStamp is zero, the start time in the record is filled in with the value
194 read from the current time stamp.
196 @param Handle Pointer to environment specific context used
197 to identify the component being measured.
198 @param Token Pointer to a Null-terminated ASCII string
199 that identifies the component being measured.
200 @param Module Pointer to a Null-terminated ASCII string
201 that identifies the module being measured.
202 @param TimeStamp 64-bit time stamp.
204 @retval EFI_SUCCESS The data was read correctly from the device.
205 @retval EFI_OUT_OF_RESOURCES There are not enough resources to record the measurement.
212 IN CONST VOID
*Handle
, OPTIONAL
213 IN CONST CHAR8
*Token
, OPTIONAL
214 IN CONST CHAR8
*Module
, OPTIONAL
218 GAUGE_DATA_ENTRY
*GaugeEntryArray
;
220 UINTN OldGaugeDataSize
;
221 GAUGE_DATA_HEADER
*OldGaugeData
;
224 Index
= mGaugeData
->NumberOfEntries
;
225 if (Index
>= mMaxGaugeRecords
) {
227 // Try to enlarge the scale of gauge arrary.
229 OldGaugeData
= mGaugeData
;
230 OldGaugeDataSize
= sizeof (GAUGE_DATA_HEADER
) + sizeof (GAUGE_DATA_ENTRY
) * mMaxGaugeRecords
;
232 mMaxGaugeRecords
*= 2;
233 GaugeDataSize
= sizeof (GAUGE_DATA_HEADER
) + sizeof (GAUGE_DATA_ENTRY
) * mMaxGaugeRecords
;
235 mGaugeData
= AllocateZeroPool (GaugeDataSize
);
236 if (mGaugeData
== NULL
) {
237 return EFI_OUT_OF_RESOURCES
;
240 // Initialize new data arry and migrate old data one.
242 mGaugeData
= CopyMem (mGaugeData
, OldGaugeData
, OldGaugeDataSize
);
244 FreePool (OldGaugeData
);
247 GaugeEntryArray
= (GAUGE_DATA_ENTRY
*) (mGaugeData
+ 1);
248 GaugeEntryArray
[Index
].Handle
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) Handle
;
251 AsciiStrnCpy (GaugeEntryArray
[Index
].Token
, Token
, DXE_PERFORMANCE_STRING_LENGTH
);
253 if (Module
!= NULL
) {
254 AsciiStrnCpy (GaugeEntryArray
[Index
].Module
, Module
, DXE_PERFORMANCE_STRING_LENGTH
);
257 if (TimeStamp
== 0) {
258 TimeStamp
= GetPerformanceCounter ();
260 GaugeEntryArray
[Index
].StartTimeStamp
= TimeStamp
;
262 mGaugeData
->NumberOfEntries
++;
268 Searches the performance measurement log from the beginning of the log
269 for the first matching record that contains a zero end time and fills in a valid end time.
271 Searches the performance measurement log from the beginning of the log
272 for the first record that matches Handle, Token, and Module and has an end time value of zero.
273 If the record can not be found then return EFI_NOT_FOUND.
274 If the record is found and TimeStamp is not zero,
275 then the end time in the record is filled in with the value specified by TimeStamp.
276 If the record is found and TimeStamp is zero, then the end time in the matching record
277 is filled in with the current time stamp value.
279 @param Handle Pointer to environment specific context used
280 to identify the component being measured.
281 @param Token Pointer to a Null-terminated ASCII string
282 that identifies the component being measured.
283 @param Module Pointer to a Null-terminated ASCII string
284 that identifies the module being measured.
285 @param TimeStamp 64-bit time stamp.
287 @retval EFI_SUCCESS The end of the measurement was recorded.
288 @retval EFI_NOT_FOUND The specified measurement record could not be found.
295 IN CONST VOID
*Handle
, OPTIONAL
296 IN CONST CHAR8
*Token
, OPTIONAL
297 IN CONST CHAR8
*Module
, OPTIONAL
301 GAUGE_DATA_ENTRY
*GaugeEntryArray
;
304 if (TimeStamp
== 0) {
305 TimeStamp
= GetPerformanceCounter ();
308 Index
= InternalSearchForGaugeEntry (Handle
, Token
, Module
);
309 if (Index
>= mGaugeData
->NumberOfEntries
) {
310 return EFI_NOT_FOUND
;
312 GaugeEntryArray
= (GAUGE_DATA_ENTRY
*) (mGaugeData
+ 1);
313 GaugeEntryArray
[Index
].EndTimeStamp
= TimeStamp
;
319 Retrieves a previously logged performance measurement.
321 Retrieves the performance log entry from the performance log specified by LogEntryKey.
322 If it stands for a valid entry, then EFI_SUCCESS is returned and
323 GaugeDataEntry stores the pointer to that entry.
325 @param LogEntryKey The key for the previous performance measurement log entry.
326 If 0, then the first performance measurement log entry is retrieved.
327 @param GaugeDataEntry The indirect pointer to the gauge data entry specified by LogEntryKey
328 if the retrieval is successful.
330 @retval EFI_SUCCESS The GuageDataEntry is successfuly found based on LogEntryKey.
331 @retval EFI_NOT_FOUND The LogEntryKey is the last entry (equals to the total entry number).
332 @retval EFI_INVALIDE_PARAMETER The LogEntryKey is not a valid entry (greater than the total entry number).
333 @retval EFI_INVALIDE_PARAMETER GaugeDataEntry is NULL.
340 IN UINTN LogEntryKey
,
341 OUT GAUGE_DATA_ENTRY
**GaugeDataEntry
344 UINTN NumberOfEntries
;
345 GAUGE_DATA_ENTRY
*LogEntryArray
;
347 NumberOfEntries
= (UINTN
) (mGaugeData
->NumberOfEntries
);
348 if (LogEntryKey
> NumberOfEntries
) {
349 return EFI_INVALID_PARAMETER
;
351 if (LogEntryKey
== NumberOfEntries
) {
352 return EFI_NOT_FOUND
;
355 LogEntryArray
= (GAUGE_DATA_ENTRY
*) (mGaugeData
+ 1);
357 if (GaugeDataEntry
== NULL
) {
358 return EFI_INVALID_PARAMETER
;
360 *GaugeDataEntry
= &LogEntryArray
[LogEntryKey
];
366 Dumps all the PEI performance log to DXE performance gauge array.
368 This internal function dumps all the PEI performance log to the DXE performance gauge array.
369 It retrieves the optional GUID HOB for PEI performance and then saves the performance data
370 to DXE performance data structures.
375 InternalGetPeiPerformance (
379 EFI_HOB_GUID_TYPE
*GuidHob
;
380 PEI_PERFORMANCE_LOG_HEADER
*LogHob
;
381 PEI_PERFORMANCE_LOG_ENTRY
*LogEntryArray
;
382 GAUGE_DATA_ENTRY
*GaugeEntryArray
;
384 UINT32 NumberOfEntries
;
387 GaugeEntryArray
= (GAUGE_DATA_ENTRY
*) (mGaugeData
+ 1);
390 // Dump PEI Log Entries to DXE Guage Data structure.
392 GuidHob
= GetFirstGuidHob (&gPeiPerformanceHobGuid
);
393 if (GuidHob
!= NULL
) {
394 LogHob
= GET_GUID_HOB_DATA (GuidHob
);
395 LogEntryArray
= (PEI_PERFORMANCE_LOG_ENTRY
*) (LogHob
+ 1);
396 GaugeEntryArray
= (GAUGE_DATA_ENTRY
*) (mGaugeData
+ 1);
398 NumberOfEntries
= LogHob
->NumberOfEntries
;
399 for (Index
= 0; Index
< NumberOfEntries
; Index
++) {
400 GaugeEntryArray
[Index
].Handle
= LogEntryArray
[Index
].Handle
;
401 AsciiStrnCpy (GaugeEntryArray
[Index
].Token
, LogEntryArray
[Index
].Token
, DXE_PERFORMANCE_STRING_LENGTH
);
402 AsciiStrnCpy (GaugeEntryArray
[Index
].Module
, LogEntryArray
[Index
].Module
, DXE_PERFORMANCE_STRING_LENGTH
);
403 GaugeEntryArray
[Index
].StartTimeStamp
= LogEntryArray
[Index
].StartTimeStamp
;
404 GaugeEntryArray
[Index
].EndTimeStamp
= LogEntryArray
[Index
].EndTimeStamp
;
407 mGaugeData
->NumberOfEntries
= NumberOfEntries
;
411 The constructor function initializes Performance infrastructure for DXE phase.
413 The constructor function publishes Performance protocol, allocates memory to log DXE performance
414 and merges PEI performance data to DXE performance log.
415 It will ASSERT() if one of these operations fails and it will always return EFI_SUCCESS.
417 @param ImageHandle The firmware allocated handle for the EFI image.
418 @param SystemTable A pointer to the EFI System Table.
420 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
425 DxeCorePerformanceLibConstructor (
426 IN EFI_HANDLE ImageHandle
,
427 IN EFI_SYSTEM_TABLE
*SystemTable
432 if (!PerformanceMeasurementEnabled ()) {
434 // Do not initialize performance infrastructure if not required.
439 // Install the protocol interfaces.
441 Status
= gBS
->InstallProtocolInterface (
443 &gPerformanceProtocolGuid
,
444 EFI_NATIVE_INTERFACE
,
445 &mPerformanceInterface
447 ASSERT_EFI_ERROR (Status
);
449 mMaxGaugeRecords
= INIT_DXE_GAUGE_DATA_ENTRIES
+ PcdGet8 (PcdMaxPeiPerformanceLogEntries
);
451 mGaugeData
= AllocateZeroPool (sizeof (GAUGE_DATA_HEADER
) + (sizeof (GAUGE_DATA_ENTRY
) * mMaxGaugeRecords
));
452 ASSERT (mGaugeData
!= NULL
);
454 InternalGetPeiPerformance ();
460 Adds a record at the end of the performance measurement log
461 that records the start time of a performance measurement.
463 Adds a record to the end of the performance measurement log
464 that contains the Handle, Token, and Module.
465 The end time of the new record must be set to zero.
466 If TimeStamp is not zero, then TimeStamp is used to fill in the start time in the record.
467 If TimeStamp is zero, the start time in the record is filled in with the value
468 read from the current time stamp.
470 @param Handle Pointer to environment specific context used
471 to identify the component being measured.
472 @param Token Pointer to a Null-terminated ASCII string
473 that identifies the component being measured.
474 @param Module Pointer to a Null-terminated ASCII string
475 that identifies the module being measured.
476 @param TimeStamp 64-bit time stamp.
478 @retval RETURN_SUCCESS The start of the measurement was recorded.
479 @retval RETURN_OUT_OF_RESOURCES There are not enough resources to record the measurement.
484 StartPerformanceMeasurement (
485 IN CONST VOID
*Handle
, OPTIONAL
486 IN CONST CHAR8
*Token
, OPTIONAL
487 IN CONST CHAR8
*Module
, OPTIONAL
493 Status
= StartGauge (Handle
, Token
, Module
, TimeStamp
);
494 return (RETURN_STATUS
) Status
;
498 Searches the performance measurement log from the beginning of the log
499 for the first matching record that contains a zero end time and fills in a valid end time.
501 Searches the performance measurement log from the beginning of the log
502 for the first record that matches Handle, Token, and Module and has an end time value of zero.
503 If the record can not be found then return RETURN_NOT_FOUND.
504 If the record is found and TimeStamp is not zero,
505 then the end time in the record is filled in with the value specified by TimeStamp.
506 If the record is found and TimeStamp is zero, then the end time in the matching record
507 is filled in with the current time stamp value.
509 @param Handle Pointer to environment specific context used
510 to identify the component being measured.
511 @param Token Pointer to a Null-terminated ASCII string
512 that identifies the component being measured.
513 @param Module Pointer to a Null-terminated ASCII string
514 that identifies the module being measured.
515 @param TimeStamp 64-bit time stamp.
517 @retval RETURN_SUCCESS The end of the measurement was recorded.
518 @retval RETURN_NOT_FOUND The specified measurement record could not be found.
523 EndPerformanceMeasurement (
524 IN CONST VOID
*Handle
, OPTIONAL
525 IN CONST CHAR8
*Token
, OPTIONAL
526 IN CONST CHAR8
*Module
, OPTIONAL
532 Status
= EndGauge (Handle
, Token
, Module
, TimeStamp
);
533 return (RETURN_STATUS
) Status
;
537 Attempts to retrieve a performance measurement log entry from the performance measurement log.
539 Attempts to retrieve the performance log entry specified by LogEntryKey. If LogEntryKey is
540 zero on entry, then an attempt is made to retrieve the first entry from the performance log,
541 and the key for the second entry in the log is returned. If the performance log is empty,
542 then no entry is retrieved and zero is returned. If LogEntryKey is not zero, then the performance
543 log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is
544 returned. If LogEntryKey is the key for the last entry in the log, then the last log entry is
545 retrieved and an implementation specific non-zero key value that specifies the end of the performance
546 log is returned. If LogEntryKey is equal this implementation specific non-zero key value, then no entry
547 is retrieved and zero is returned. In the cases where a performance log entry can be returned,
548 the log entry is returned in Handle, Token, Module, StartTimeStamp, and EndTimeStamp.
549 If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().
550 If Handle is NULL, then ASSERT().
551 If Token is NULL, then ASSERT().
552 If Module is NULL, then ASSERT().
553 If StartTimeStamp is NULL, then ASSERT().
554 If EndTimeStamp is NULL, then ASSERT().
556 @param LogEntryKey On entry, the key of the performance measurement log entry to retrieve.
557 0, then the first performance measurement log entry is retrieved.
558 On exit, the key of the next performance lof entry entry.
559 @param Handle Pointer to environment specific context used to identify the component
561 @param Token Pointer to a Null-terminated ASCII string that identifies the component
563 @param Module Pointer to a Null-terminated ASCII string that identifies the module
565 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
567 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
570 @return The key for the next performance log entry (in general case).
575 GetPerformanceMeasurement (
576 IN UINTN LogEntryKey
,
577 OUT CONST VOID
**Handle
,
578 OUT CONST CHAR8
**Token
,
579 OUT CONST CHAR8
**Module
,
580 OUT UINT64
*StartTimeStamp
,
581 OUT UINT64
*EndTimeStamp
585 GAUGE_DATA_ENTRY
*GaugeData
;
587 ASSERT (Handle
!= NULL
);
588 ASSERT (Token
!= NULL
);
589 ASSERT (Module
!= NULL
);
590 ASSERT (StartTimeStamp
!= NULL
);
591 ASSERT (EndTimeStamp
!= NULL
);
593 Status
= GetGauge (LogEntryKey
++, &GaugeData
);
596 // Make sure that LogEntryKey is a valid log entry key,
598 ASSERT (Status
!= EFI_INVALID_PARAMETER
);
600 if (EFI_ERROR (Status
)) {
602 // The LogEntryKey is the last entry (equals to the total entry number).
607 ASSERT (GaugeData
!= NULL
);
609 *Handle
= (VOID
*) (UINTN
) GaugeData
->Handle
;
610 *Token
= GaugeData
->Token
;
611 *Module
= GaugeData
->Module
;
612 *StartTimeStamp
= GaugeData
->StartTimeStamp
;
613 *EndTimeStamp
= GaugeData
->EndTimeStamp
;
619 Returns TRUE if the performance measurement macros are enabled.
621 This function returns TRUE if the PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
622 PcdPerformanceLibraryPropertyMask is set. Otherwise FALSE is returned.
624 @retval TRUE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
625 PcdPerformanceLibraryPropertyMask is set.
626 @retval FALSE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
627 PcdPerformanceLibraryPropertyMask is clear.
632 PerformanceMeasurementEnabled (
636 return (BOOLEAN
) ((PcdGet8(PcdPerformanceLibraryPropertyMask
) & PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED
) != 0);