]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Library/DxeCorePerformanceLib/DxeCorePerformanceLib.c
Global variables have been moved backward ahead of functions.
[mirror_edk2.git] / MdeModulePkg / Library / DxeCorePerformanceLib / DxeCorePerformanceLib.c
1 /** @file
2 Support for measurement of DXE performance
3
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
9
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.
12
13 **/
14
15
16 #include "DxeCorePerformanceLibInternal.h"
17
18
19 //
20 // Definition for global variables.
21 //
22 GAUGE_DATA_HEADER *mGaugeData;
23 UINT32 mMaxGaugeRecords;
24
25 EFI_HANDLE mHandle = NULL;
26 PERFORMANCE_PROTOCOL mPerformanceInterface = {
27 StartGauge,
28 EndGauge,
29 GetGauge
30 };
31
32 /**
33 Searches in the gauge array with keyword Handle, Token and Module.
34
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.
39
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.
46
47 @retval The index of gauge entry in the array.
48
49 **/
50 UINT32
51 InternalSearchForGaugeEntry (
52 IN CONST VOID *Handle, OPTIONAL
53 IN CONST CHAR8 *Token, OPTIONAL
54 IN CONST CHAR8 *Module OPTIONAL
55 )
56 {
57 UINT32 Index;
58 UINT32 NumberOfEntries;
59 GAUGE_DATA_ENTRY *GaugeEntryArray;
60
61 if (Token == NULL) {
62 Token = "";
63 }
64 if (Module == NULL) {
65 Module = "";
66 }
67
68 NumberOfEntries = mGaugeData->NumberOfEntries;
69 GaugeEntryArray = (GAUGE_DATA_ENTRY *) (mGaugeData + 1);
70
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
76 ) {
77 break;
78 }
79 }
80
81 return Index;
82 }
83
84 /**
85 Adds a record at the end of the performance measurement log
86 that records the start time of a performance measurement.
87
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.
94
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.
102
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.
105
106 **/
107 EFI_STATUS
108 EFIAPI
109 StartGauge (
110 IN CONST VOID *Handle, OPTIONAL
111 IN CONST CHAR8 *Token, OPTIONAL
112 IN CONST CHAR8 *Module, OPTIONAL
113 IN UINT64 TimeStamp
114 )
115 {
116 GAUGE_DATA_ENTRY *GaugeEntryArray;
117 UINTN GaugeDataSize;
118 UINTN OldGaugeDataSize;
119 GAUGE_DATA_HEADER *OldGaugeData;
120 UINT32 Index;
121
122 Index = mGaugeData->NumberOfEntries;
123 if (Index >= mMaxGaugeRecords) {
124 //
125 // Try to enlarge the scale of gauge arrary.
126 //
127 OldGaugeData = mGaugeData;
128 OldGaugeDataSize = sizeof (GAUGE_DATA_HEADER) + sizeof (GAUGE_DATA_ENTRY) * mMaxGaugeRecords;
129
130 mMaxGaugeRecords *= 2;
131 GaugeDataSize = sizeof (GAUGE_DATA_HEADER) + sizeof (GAUGE_DATA_ENTRY) * mMaxGaugeRecords;
132
133 mGaugeData = AllocateZeroPool (GaugeDataSize);
134 if (mGaugeData == NULL) {
135 return EFI_OUT_OF_RESOURCES;
136 }
137 //
138 // Initialize new data arry and migrate old data one.
139 //
140 mGaugeData = CopyMem (mGaugeData, OldGaugeData, OldGaugeDataSize);
141
142 FreePool (OldGaugeData);
143 }
144
145 GaugeEntryArray = (GAUGE_DATA_ENTRY *) (mGaugeData + 1);
146 GaugeEntryArray[Index].Handle = (EFI_PHYSICAL_ADDRESS) (UINTN) Handle;
147
148 if (Token != NULL) {
149 AsciiStrnCpy (GaugeEntryArray[Index].Token, Token, DXE_PERFORMANCE_STRING_LENGTH);
150 }
151 if (Module != NULL) {
152 AsciiStrnCpy (GaugeEntryArray[Index].Module, Module, DXE_PERFORMANCE_STRING_LENGTH);
153 }
154
155 if (TimeStamp == 0) {
156 TimeStamp = GetPerformanceCounter ();
157 }
158 GaugeEntryArray[Index].StartTimeStamp = TimeStamp;
159
160 mGaugeData->NumberOfEntries++;
161
162 return EFI_SUCCESS;
163 }
164
165 /**
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.
168
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.
176
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.
184
185 @retval EFI_SUCCESS The end of the measurement was recorded.
186 @retval EFI_NOT_FOUND The specified measurement record could not be found.
187
188 **/
189 EFI_STATUS
190 EFIAPI
191 EndGauge (
192 IN CONST VOID *Handle, OPTIONAL
193 IN CONST CHAR8 *Token, OPTIONAL
194 IN CONST CHAR8 *Module, OPTIONAL
195 IN UINT64 TimeStamp
196 )
197 {
198 GAUGE_DATA_ENTRY *GaugeEntryArray;
199 UINT32 Index;
200
201 if (TimeStamp == 0) {
202 TimeStamp = GetPerformanceCounter ();
203 }
204
205 Index = InternalSearchForGaugeEntry (Handle, Token, Module);
206 if (Index >= mGaugeData->NumberOfEntries) {
207 return EFI_NOT_FOUND;
208 }
209 GaugeEntryArray = (GAUGE_DATA_ENTRY *) (mGaugeData + 1);
210 GaugeEntryArray[Index].EndTimeStamp = TimeStamp;
211
212 return EFI_SUCCESS;
213 }
214
215 /**
216 Retrieves a previously logged performance measurement.
217
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.
221
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.
226
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.
231
232 **/
233 EFI_STATUS
234 EFIAPI
235 GetGauge (
236 IN UINTN LogEntryKey,
237 OUT GAUGE_DATA_ENTRY **GaugeDataEntry
238 )
239 {
240 UINTN NumberOfEntries;
241 GAUGE_DATA_ENTRY *LogEntryArray;
242
243 NumberOfEntries = (UINTN) (mGaugeData->NumberOfEntries);
244 if (LogEntryKey > NumberOfEntries) {
245 return EFI_INVALID_PARAMETER;
246 }
247 if (LogEntryKey == NumberOfEntries) {
248 return EFI_NOT_FOUND;
249 }
250
251 LogEntryArray = (GAUGE_DATA_ENTRY *) (mGaugeData + 1);
252
253 if (GaugeDataEntry == NULL) {
254 return EFI_INVALID_PARAMETER;
255 }
256 *GaugeDataEntry = &LogEntryArray[LogEntryKey];
257
258 return EFI_SUCCESS;
259 }
260
261 /**
262 Dumps all the PEI performance log to DXE performance gauge array.
263
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.
267
268 **/
269 VOID
270 InternalGetPeiPerformance (
271 VOID
272 )
273 {
274 EFI_HOB_GUID_TYPE *GuidHob;
275 PEI_PERFORMANCE_LOG_HEADER *LogHob;
276 PEI_PERFORMANCE_LOG_ENTRY *LogEntryArray;
277 GAUGE_DATA_ENTRY *GaugeEntryArray;
278 UINT32 Index;
279 UINT32 NumberOfEntries;
280
281 NumberOfEntries = 0;
282 GaugeEntryArray = (GAUGE_DATA_ENTRY *) (mGaugeData + 1);
283
284 //
285 // Dump PEI Log Entries to DXE Guage Data structure.
286 //
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);
292
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;
300 }
301 }
302 mGaugeData->NumberOfEntries = NumberOfEntries;
303 }
304
305 /**
306 The constructor function initializes Performance infrastructure for DXE phase.
307
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.
311
312 @param ImageHandle The firmware allocated handle for the EFI image.
313 @param SystemTable A pointer to the EFI System Table.
314
315 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
316
317 **/
318 EFI_STATUS
319 EFIAPI
320 DxeCorePerformanceLibConstructor (
321 IN EFI_HANDLE ImageHandle,
322 IN EFI_SYSTEM_TABLE *SystemTable
323 )
324 {
325 EFI_STATUS Status;
326
327 if (!PerformanceMeasurementEnabled ()) {
328 //
329 // Do not initialize performance infrastructure if not required.
330 //
331 return EFI_SUCCESS;
332 }
333 //
334 // Install the protocol interfaces.
335 //
336 Status = gBS->InstallProtocolInterface (
337 &mHandle,
338 &gPerformanceProtocolGuid,
339 EFI_NATIVE_INTERFACE,
340 &mPerformanceInterface
341 );
342 ASSERT_EFI_ERROR (Status);
343
344 mMaxGaugeRecords = INIT_DXE_GAUGE_DATA_ENTRIES + PcdGet8 (PcdMaxPeiPerformanceLogEntries);
345
346 mGaugeData = AllocateZeroPool (sizeof (GAUGE_DATA_HEADER) + (sizeof (GAUGE_DATA_ENTRY) * mMaxGaugeRecords));
347 ASSERT (mGaugeData != NULL);
348
349 InternalGetPeiPerformance ();
350
351 return Status;
352 }
353
354 /**
355 Adds a record at the end of the performance measurement log
356 that records the start time of a performance measurement.
357
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.
364
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.
372
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.
375
376 **/
377 RETURN_STATUS
378 EFIAPI
379 StartPerformanceMeasurement (
380 IN CONST VOID *Handle, OPTIONAL
381 IN CONST CHAR8 *Token, OPTIONAL
382 IN CONST CHAR8 *Module, OPTIONAL
383 IN UINT64 TimeStamp
384 )
385 {
386 EFI_STATUS Status;
387
388 Status = StartGauge (Handle, Token, Module, TimeStamp);
389 return (RETURN_STATUS) Status;
390 }
391
392 /**
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.
395
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.
403
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.
411
412 @retval RETURN_SUCCESS The end of the measurement was recorded.
413 @retval RETURN_NOT_FOUND The specified measurement record could not be found.
414
415 **/
416 RETURN_STATUS
417 EFIAPI
418 EndPerformanceMeasurement (
419 IN CONST VOID *Handle, OPTIONAL
420 IN CONST CHAR8 *Token, OPTIONAL
421 IN CONST CHAR8 *Module, OPTIONAL
422 IN UINT64 TimeStamp
423 )
424 {
425 EFI_STATUS Status;
426
427 Status = EndGauge (Handle, Token, Module, TimeStamp);
428 return (RETURN_STATUS) Status;
429 }
430
431 /**
432 Attempts to retrieve a performance measurement log entry from the performance measurement log.
433
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().
450
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
455 being measured.
456 @param Token Pointer to a Null-terminated ASCII string that identifies the component
457 being measured.
458 @param Module Pointer to a Null-terminated ASCII string that identifies the module
459 being measured.
460 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
461 was started.
462 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
463 was ended.
464
465 @return The key for the next performance log entry (in general case).
466
467 **/
468 UINTN
469 EFIAPI
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
477 )
478 {
479 EFI_STATUS Status;
480 GAUGE_DATA_ENTRY *GaugeData;
481
482 GaugeData = NULL;
483
484 ASSERT (Handle != NULL);
485 ASSERT (Token != NULL);
486 ASSERT (Module != NULL);
487 ASSERT (StartTimeStamp != NULL);
488 ASSERT (EndTimeStamp != NULL);
489
490 Status = GetGauge (LogEntryKey++, &GaugeData);
491
492 //
493 // Make sure that LogEntryKey is a valid log entry key,
494 //
495 ASSERT (Status != EFI_INVALID_PARAMETER);
496
497 if (EFI_ERROR (Status)) {
498 //
499 // The LogEntryKey is the last entry (equals to the total entry number).
500 //
501 return 0;
502 }
503
504 ASSERT (GaugeData != NULL);
505
506 *Handle = (VOID *) (UINTN) GaugeData->Handle;
507 *Token = GaugeData->Token;
508 *Module = GaugeData->Module;
509 *StartTimeStamp = GaugeData->StartTimeStamp;
510 *EndTimeStamp = GaugeData->EndTimeStamp;
511
512 return LogEntryKey;
513 }
514
515 /**
516 Returns TRUE if the performance measurement macros are enabled.
517
518 This function returns TRUE if the PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
519 PcdPerformanceLibraryPropertyMask is set. Otherwise FALSE is returned.
520
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.
525
526 **/
527 BOOLEAN
528 EFIAPI
529 PerformanceMeasurementEnabled (
530 VOID
531 )
532 {
533 return (BOOLEAN) ((PcdGet8(PcdPerformanceLibraryPropertyMask) & PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED) != 0);
534 }