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.
16 #include "DxeCorePerformanceLibInternal.h"
20 // Definition for global variables.
22 GAUGE_DATA_HEADER
*mGaugeData
;
23 UINT32 mMaxGaugeRecords
;
25 EFI_HANDLE mHandle
= NULL
;
26 PERFORMANCE_PROTOCOL mPerformanceInterface
= {
33 Searches in the gauge array with keyword Handle, Token and Module.
35 This internal function searches for the gauge entry in the gauge array.
36 If there is an entry that exactly matches the given key word triple
37 and its end time stamp is zero, then the index of that gauge entry is returned;
38 otherwise, the the number of gauge entries in the array is returned.
40 @param Handle Pointer to environment specific context used
41 to identify the component being measured.
42 @param Token Pointer to a Null-terminated ASCII string
43 that identifies the component being measured.
44 @param Module Pointer to a Null-terminated ASCII string
45 that identifies the module being measured.
47 @retval The index of gauge entry in the array.
51 InternalSearchForGaugeEntry (
52 IN CONST VOID
*Handle
, OPTIONAL
53 IN CONST CHAR8
*Token
, OPTIONAL
54 IN CONST CHAR8
*Module OPTIONAL
58 UINT32 NumberOfEntries
;
59 GAUGE_DATA_ENTRY
*GaugeEntryArray
;
68 NumberOfEntries
= mGaugeData
->NumberOfEntries
;
69 GaugeEntryArray
= (GAUGE_DATA_ENTRY
*) (mGaugeData
+ 1);
71 for (Index
= 0; Index
< NumberOfEntries
; Index
++) {
72 if ((GaugeEntryArray
[Index
].Handle
== (EFI_PHYSICAL_ADDRESS
) (UINTN
) Handle
) &&
73 AsciiStrnCmp (GaugeEntryArray
[Index
].Token
, Token
, PEI_PERFORMANCE_STRING_LENGTH
) == 0 &&
74 AsciiStrnCmp (GaugeEntryArray
[Index
].Module
, Module
, PEI_PERFORMANCE_STRING_LENGTH
) == 0 &&
75 GaugeEntryArray
[Index
].EndTimeStamp
== 0
85 Adds a record at the end of the performance measurement log
86 that records the start time of a performance measurement.
88 Adds a record to the end of the performance measurement log
89 that contains the Handle, Token, and Module.
90 The end time of the new record must be set to zero.
91 If TimeStamp is not zero, then TimeStamp is used to fill in the start time in the record.
92 If TimeStamp is zero, the start time in the record is filled in with the value
93 read from the current time stamp.
95 @param Handle Pointer to environment specific context used
96 to identify the component being measured.
97 @param Token Pointer to a Null-terminated ASCII string
98 that identifies the component being measured.
99 @param Module Pointer to a Null-terminated ASCII string
100 that identifies the module being measured.
101 @param TimeStamp 64-bit time stamp.
103 @retval EFI_SUCCESS The data was read correctly from the device.
104 @retval EFI_OUT_OF_RESOURCES There are not enough resources to record the measurement.
110 IN CONST VOID
*Handle
, OPTIONAL
111 IN CONST CHAR8
*Token
, OPTIONAL
112 IN CONST CHAR8
*Module
, OPTIONAL
116 GAUGE_DATA_ENTRY
*GaugeEntryArray
;
118 UINTN OldGaugeDataSize
;
119 GAUGE_DATA_HEADER
*OldGaugeData
;
122 Index
= mGaugeData
->NumberOfEntries
;
123 if (Index
>= mMaxGaugeRecords
) {
125 // Try to enlarge the scale of gauge arrary.
127 OldGaugeData
= mGaugeData
;
128 OldGaugeDataSize
= sizeof (GAUGE_DATA_HEADER
) + sizeof (GAUGE_DATA_ENTRY
) * mMaxGaugeRecords
;
130 mMaxGaugeRecords
*= 2;
131 GaugeDataSize
= sizeof (GAUGE_DATA_HEADER
) + sizeof (GAUGE_DATA_ENTRY
) * mMaxGaugeRecords
;
133 mGaugeData
= AllocateZeroPool (GaugeDataSize
);
134 if (mGaugeData
== NULL
) {
135 return EFI_OUT_OF_RESOURCES
;
138 // Initialize new data arry and migrate old data one.
140 mGaugeData
= CopyMem (mGaugeData
, OldGaugeData
, OldGaugeDataSize
);
142 FreePool (OldGaugeData
);
145 GaugeEntryArray
= (GAUGE_DATA_ENTRY
*) (mGaugeData
+ 1);
146 GaugeEntryArray
[Index
].Handle
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) Handle
;
149 AsciiStrnCpy (GaugeEntryArray
[Index
].Token
, Token
, DXE_PERFORMANCE_STRING_LENGTH
);
151 if (Module
!= NULL
) {
152 AsciiStrnCpy (GaugeEntryArray
[Index
].Module
, Module
, DXE_PERFORMANCE_STRING_LENGTH
);
155 if (TimeStamp
== 0) {
156 TimeStamp
= GetPerformanceCounter ();
158 GaugeEntryArray
[Index
].StartTimeStamp
= TimeStamp
;
160 mGaugeData
->NumberOfEntries
++;
166 Searches the performance measurement log from the beginning of the log
167 for the first matching record that contains a zero end time and fills in a valid end time.
169 Searches the performance measurement log from the beginning of the log
170 for the first record that matches Handle, Token, and Module and has an end time value of zero.
171 If the record can not be found then return EFI_NOT_FOUND.
172 If the record is found and TimeStamp is not zero,
173 then the end time in the record is filled in with the value specified by TimeStamp.
174 If the record is found and TimeStamp is zero, then the end time in the matching record
175 is filled in with the current time stamp value.
177 @param Handle Pointer to environment specific context used
178 to identify the component being measured.
179 @param Token Pointer to a Null-terminated ASCII string
180 that identifies the component being measured.
181 @param Module Pointer to a Null-terminated ASCII string
182 that identifies the module being measured.
183 @param TimeStamp 64-bit time stamp.
185 @retval EFI_SUCCESS The end of the measurement was recorded.
186 @retval EFI_NOT_FOUND The specified measurement record could not be found.
192 IN CONST VOID
*Handle
, OPTIONAL
193 IN CONST CHAR8
*Token
, OPTIONAL
194 IN CONST CHAR8
*Module
, OPTIONAL
198 GAUGE_DATA_ENTRY
*GaugeEntryArray
;
201 if (TimeStamp
== 0) {
202 TimeStamp
= GetPerformanceCounter ();
205 Index
= InternalSearchForGaugeEntry (Handle
, Token
, Module
);
206 if (Index
>= mGaugeData
->NumberOfEntries
) {
207 return EFI_NOT_FOUND
;
209 GaugeEntryArray
= (GAUGE_DATA_ENTRY
*) (mGaugeData
+ 1);
210 GaugeEntryArray
[Index
].EndTimeStamp
= TimeStamp
;
216 Retrieves a previously logged performance measurement.
218 Retrieves the performance log entry from the performance log specified by LogEntryKey.
219 If it stands for a valid entry, then EFI_SUCCESS is returned and
220 GaugeDataEntry stores the pointer to that entry.
222 @param LogEntryKey The key for the previous performance measurement log entry.
223 If 0, then the first performance measurement log entry is retrieved.
224 @param GaugeDataEntry The indirect pointer to the gauge data entry specified by LogEntryKey
225 if the retrieval is successful.
227 @retval EFI_SUCCESS The GuageDataEntry is successfuly found based on LogEntryKey.
228 @retval EFI_NOT_FOUND The LogEntryKey is the last entry (equals to the total entry number).
229 @retval EFI_INVALIDE_PARAMETER The LogEntryKey is not a valid entry (greater than the total entry number).
230 @retval EFI_INVALIDE_PARAMETER GaugeDataEntry is NULL.
236 IN UINTN LogEntryKey
,
237 OUT GAUGE_DATA_ENTRY
**GaugeDataEntry
240 UINTN NumberOfEntries
;
241 GAUGE_DATA_ENTRY
*LogEntryArray
;
243 NumberOfEntries
= (UINTN
) (mGaugeData
->NumberOfEntries
);
244 if (LogEntryKey
> NumberOfEntries
) {
245 return EFI_INVALID_PARAMETER
;
247 if (LogEntryKey
== NumberOfEntries
) {
248 return EFI_NOT_FOUND
;
251 LogEntryArray
= (GAUGE_DATA_ENTRY
*) (mGaugeData
+ 1);
253 if (GaugeDataEntry
== NULL
) {
254 return EFI_INVALID_PARAMETER
;
256 *GaugeDataEntry
= &LogEntryArray
[LogEntryKey
];
262 Dumps all the PEI performance log to DXE performance gauge array.
264 This internal function dumps all the PEI performance log to the DXE performance gauge array.
265 It retrieves the optional GUID HOB for PEI performance and then saves the performance data
266 to DXE performance data structures.
270 InternalGetPeiPerformance (
274 EFI_HOB_GUID_TYPE
*GuidHob
;
275 PEI_PERFORMANCE_LOG_HEADER
*LogHob
;
276 PEI_PERFORMANCE_LOG_ENTRY
*LogEntryArray
;
277 GAUGE_DATA_ENTRY
*GaugeEntryArray
;
279 UINT32 NumberOfEntries
;
282 GaugeEntryArray
= (GAUGE_DATA_ENTRY
*) (mGaugeData
+ 1);
285 // Dump PEI Log Entries to DXE Guage Data structure.
287 GuidHob
= GetFirstGuidHob (&gPeiPerformanceHobGuid
);
288 if (GuidHob
!= NULL
) {
289 LogHob
= GET_GUID_HOB_DATA (GuidHob
);
290 LogEntryArray
= (PEI_PERFORMANCE_LOG_ENTRY
*) (LogHob
+ 1);
291 GaugeEntryArray
= (GAUGE_DATA_ENTRY
*) (mGaugeData
+ 1);
293 NumberOfEntries
= LogHob
->NumberOfEntries
;
294 for (Index
= 0; Index
< NumberOfEntries
; Index
++) {
295 GaugeEntryArray
[Index
].Handle
= LogEntryArray
[Index
].Handle
;
296 AsciiStrnCpy (GaugeEntryArray
[Index
].Token
, LogEntryArray
[Index
].Token
, DXE_PERFORMANCE_STRING_LENGTH
);
297 AsciiStrnCpy (GaugeEntryArray
[Index
].Module
, LogEntryArray
[Index
].Module
, DXE_PERFORMANCE_STRING_LENGTH
);
298 GaugeEntryArray
[Index
].StartTimeStamp
= LogEntryArray
[Index
].StartTimeStamp
;
299 GaugeEntryArray
[Index
].EndTimeStamp
= LogEntryArray
[Index
].EndTimeStamp
;
302 mGaugeData
->NumberOfEntries
= NumberOfEntries
;
306 The constructor function initializes Performance infrastructure for DXE phase.
308 The constructor function publishes Performance protocol, allocates memory to log DXE performance
309 and merges PEI performance data to DXE performance log.
310 It will ASSERT() if one of these operations fails and it will always return EFI_SUCCESS.
312 @param ImageHandle The firmware allocated handle for the EFI image.
313 @param SystemTable A pointer to the EFI System Table.
315 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
320 DxeCorePerformanceLibConstructor (
321 IN EFI_HANDLE ImageHandle
,
322 IN EFI_SYSTEM_TABLE
*SystemTable
327 if (!PerformanceMeasurementEnabled ()) {
329 // Do not initialize performance infrastructure if not required.
334 // Install the protocol interfaces.
336 Status
= gBS
->InstallProtocolInterface (
338 &gPerformanceProtocolGuid
,
339 EFI_NATIVE_INTERFACE
,
340 &mPerformanceInterface
342 ASSERT_EFI_ERROR (Status
);
344 mMaxGaugeRecords
= INIT_DXE_GAUGE_DATA_ENTRIES
+ PcdGet8 (PcdMaxPeiPerformanceLogEntries
);
346 mGaugeData
= AllocateZeroPool (sizeof (GAUGE_DATA_HEADER
) + (sizeof (GAUGE_DATA_ENTRY
) * mMaxGaugeRecords
));
347 ASSERT (mGaugeData
!= NULL
);
349 InternalGetPeiPerformance ();
355 Adds a record at the end of the performance measurement log
356 that records the start time of a performance measurement.
358 Adds a record to the end of the performance measurement log
359 that contains the Handle, Token, and Module.
360 The end time of the new record must be set to zero.
361 If TimeStamp is not zero, then TimeStamp is used to fill in the start time in the record.
362 If TimeStamp is zero, the start time in the record is filled in with the value
363 read from the current time stamp.
365 @param Handle Pointer to environment specific context used
366 to identify the component being measured.
367 @param Token Pointer to a Null-terminated ASCII string
368 that identifies the component being measured.
369 @param Module Pointer to a Null-terminated ASCII string
370 that identifies the module being measured.
371 @param TimeStamp 64-bit time stamp.
373 @retval RETURN_SUCCESS The start of the measurement was recorded.
374 @retval RETURN_OUT_OF_RESOURCES There are not enough resources to record the measurement.
379 StartPerformanceMeasurement (
380 IN CONST VOID
*Handle
, OPTIONAL
381 IN CONST CHAR8
*Token
, OPTIONAL
382 IN CONST CHAR8
*Module
, OPTIONAL
388 Status
= StartGauge (Handle
, Token
, Module
, TimeStamp
);
389 return (RETURN_STATUS
) Status
;
393 Searches the performance measurement log from the beginning of the log
394 for the first matching record that contains a zero end time and fills in a valid end time.
396 Searches the performance measurement log from the beginning of the log
397 for the first record that matches Handle, Token, and Module and has an end time value of zero.
398 If the record can not be found then return RETURN_NOT_FOUND.
399 If the record is found and TimeStamp is not zero,
400 then the end time in the record is filled in with the value specified by TimeStamp.
401 If the record is found and TimeStamp is zero, then the end time in the matching record
402 is filled in with the current time stamp value.
404 @param Handle Pointer to environment specific context used
405 to identify the component being measured.
406 @param Token Pointer to a Null-terminated ASCII string
407 that identifies the component being measured.
408 @param Module Pointer to a Null-terminated ASCII string
409 that identifies the module being measured.
410 @param TimeStamp 64-bit time stamp.
412 @retval RETURN_SUCCESS The end of the measurement was recorded.
413 @retval RETURN_NOT_FOUND The specified measurement record could not be found.
418 EndPerformanceMeasurement (
419 IN CONST VOID
*Handle
, OPTIONAL
420 IN CONST CHAR8
*Token
, OPTIONAL
421 IN CONST CHAR8
*Module
, OPTIONAL
427 Status
= EndGauge (Handle
, Token
, Module
, TimeStamp
);
428 return (RETURN_STATUS
) Status
;
432 Attempts to retrieve a performance measurement log entry from the performance measurement log.
434 Attempts to retrieve the performance log entry specified by LogEntryKey. If LogEntryKey is
435 zero on entry, then an attempt is made to retrieve the first entry from the performance log,
436 and the key for the second entry in the log is returned. If the performance log is empty,
437 then no entry is retrieved and zero is returned. If LogEntryKey is not zero, then the performance
438 log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is
439 returned. If LogEntryKey is the key for the last entry in the log, then the last log entry is
440 retrieved and an implementation specific non-zero key value that specifies the end of the performance
441 log is returned. If LogEntryKey is equal this implementation specific non-zero key value, then no entry
442 is retrieved and zero is returned. In the cases where a performance log entry can be returned,
443 the log entry is returned in Handle, Token, Module, StartTimeStamp, and EndTimeStamp.
444 If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().
445 If Handle is NULL, then ASSERT().
446 If Token is NULL, then ASSERT().
447 If Module is NULL, then ASSERT().
448 If StartTimeStamp is NULL, then ASSERT().
449 If EndTimeStamp is NULL, then ASSERT().
451 @param LogEntryKey On entry, the key of the performance measurement log entry to retrieve.
452 0, then the first performance measurement log entry is retrieved.
453 On exit, the key of the next performance lof entry entry.
454 @param Handle Pointer to environment specific context used to identify the component
456 @param Token Pointer to a Null-terminated ASCII string that identifies the component
458 @param Module Pointer to a Null-terminated ASCII string that identifies the module
460 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
462 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
465 @return The key for the next performance log entry (in general case).
470 GetPerformanceMeasurement (
471 IN UINTN LogEntryKey
,
472 OUT CONST VOID
**Handle
,
473 OUT CONST CHAR8
**Token
,
474 OUT CONST CHAR8
**Module
,
475 OUT UINT64
*StartTimeStamp
,
476 OUT UINT64
*EndTimeStamp
480 GAUGE_DATA_ENTRY
*GaugeData
;
484 ASSERT (Handle
!= NULL
);
485 ASSERT (Token
!= NULL
);
486 ASSERT (Module
!= NULL
);
487 ASSERT (StartTimeStamp
!= NULL
);
488 ASSERT (EndTimeStamp
!= NULL
);
490 Status
= GetGauge (LogEntryKey
++, &GaugeData
);
493 // Make sure that LogEntryKey is a valid log entry key,
495 ASSERT (Status
!= EFI_INVALID_PARAMETER
);
497 if (EFI_ERROR (Status
)) {
499 // The LogEntryKey is the last entry (equals to the total entry number).
504 ASSERT (GaugeData
!= NULL
);
506 *Handle
= (VOID
*) (UINTN
) GaugeData
->Handle
;
507 *Token
= GaugeData
->Token
;
508 *Module
= GaugeData
->Module
;
509 *StartTimeStamp
= GaugeData
->StartTimeStamp
;
510 *EndTimeStamp
= GaugeData
->EndTimeStamp
;
516 Returns TRUE if the performance measurement macros are enabled.
518 This function returns TRUE if the PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
519 PcdPerformanceLibraryPropertyMask is set. Otherwise FALSE is returned.
521 @retval TRUE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
522 PcdPerformanceLibraryPropertyMask is set.
523 @retval FALSE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
524 PcdPerformanceLibraryPropertyMask is clear.
529 PerformanceMeasurementEnabled (
533 return (BOOLEAN
) ((PcdGet8(PcdPerformanceLibraryPropertyMask
) & PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED
) != 0);