]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/DynamicCommand/DpDynamicCommand/Dp.c
ShellPkg/Dp: Make the help info align with code
[mirror_edk2.git] / ShellPkg / DynamicCommand / DpDynamicCommand / Dp.c
1 /** @file
2 Shell command for Displaying Performance Metrics.
3
4 The Dp command reads performance data and presents it in several
5 different formats depending upon the needs of the user. Both
6 Trace and Measured Profiling information is processed and presented.
7
8 Dp uses the "PerformanceLib" to read the measurement records.
9 The "TimerLib" provides information about the timer, such as frequency,
10 beginning, and ending counter values.
11 Measurement records contain identifying information (Handle, Token, Module)
12 and start and end time values.
13 Dp uses this information to group records in different ways. It also uses
14 timer information to calculate elapsed time for each measurement.
15
16 Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.
17 (C) Copyright 2015-2016 Hewlett Packard Enterprise Development LP<BR>
18 This program and the accompanying materials
19 are licensed and made available under the terms and conditions of the BSD License
20 which accompanies this distribution. The full text of the license may be found at
21 http://opensource.org/licenses/bsd-license.php
22
23 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
24 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
25 **/
26
27 #include "Dp.h"
28 #include "Literals.h"
29 #include "DpInternal.h"
30
31 #pragma pack(1)
32
33 typedef struct {
34 EFI_ACPI_DESCRIPTION_HEADER Header;
35 UINT32 Entry;
36 } RSDT_TABLE;
37
38 typedef struct {
39 EFI_ACPI_DESCRIPTION_HEADER Header;
40 UINT64 Entry;
41 } XSDT_TABLE;
42
43 #pragma pack()
44
45 EFI_HANDLE mDpHiiHandle;
46
47 typedef struct {
48 EFI_HANDLE Handle;
49 EFI_GUID ModuleGuid;
50 } HANDLE_GUID_MAP;
51
52 HANDLE_GUID_MAP *mCacheHandleGuidTable;
53 UINTN mCachePairCount = 0;
54
55 //
56 /// Module-Global Variables
57 ///@{
58 CHAR16 mGaugeString[DP_GAUGE_STRING_LENGTH + 1];
59 CHAR16 mUnicodeToken[DXE_PERFORMANCE_STRING_SIZE];
60 UINT64 mInterestThreshold;
61 BOOLEAN mShowId = FALSE;
62 UINT8 *mBootPerformanceTable;
63 UINTN mBootPerformanceTableSize;
64 BOOLEAN mPeiPhase = FALSE;
65 BOOLEAN mDxePhase = FALSE;
66
67 PERF_SUMMARY_DATA SummaryData = { 0 }; ///< Create the SummaryData structure and init. to ZERO.
68 MEASUREMENT_RECORD *mMeasurementList = NULL;
69 UINTN mMeasurementNum = 0;
70
71 /// Items for which to gather cumulative statistics.
72 PERF_CUM_DATA CumData[] = {
73 PERF_INIT_CUM_DATA (LOAD_IMAGE_TOK),
74 PERF_INIT_CUM_DATA (START_IMAGE_TOK),
75 PERF_INIT_CUM_DATA (DRIVERBINDING_START_TOK),
76 PERF_INIT_CUM_DATA (DRIVERBINDING_SUPPORT_TOK)
77 };
78
79 /// Number of items for which we are gathering cumulative statistics.
80 UINT32 const NumCum = sizeof(CumData) / sizeof(PERF_CUM_DATA);
81
82 STATIC CONST SHELL_PARAM_ITEM ParamList[] = {
83 {L"-v", TypeFlag}, // -v Verbose Mode
84 {L"-A", TypeFlag}, // -A All, Cooked
85 {L"-R", TypeFlag}, // -R RAW All
86 {L"-s", TypeFlag}, // -s Summary
87 {L"-x", TypeFlag}, // -x eXclude Cumulative Items
88 {L"-i", TypeFlag}, // -i Display Identifier
89 {L"-c", TypeValue}, // -c Display cumulative data.
90 {L"-n", TypeValue}, // -n # Number of records to display for A and R
91 {L"-t", TypeValue}, // -t # Threshold of interest
92 {NULL, TypeMax}
93 };
94
95 ///@}
96
97 /**
98 Display the trailing Verbose information.
99 **/
100 VOID
101 DumpStatistics( void )
102 {
103 EFI_STRING StringPtr;
104 EFI_STRING StringPtrUnknown;
105 StringPtr = HiiGetString (mDpHiiHandle, STRING_TOKEN (STR_DP_SECTION_STATISTICS), NULL);
106 StringPtrUnknown = HiiGetString (mDpHiiHandle, STRING_TOKEN (STR_ALIT_UNKNOWN), NULL);
107 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DP_SECTION_HEADER), mDpHiiHandle,
108 (StringPtr == NULL) ? StringPtrUnknown : StringPtr);
109 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DP_STATS_NUMTRACE), mDpHiiHandle, SummaryData.NumTrace);
110 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DP_STATS_NUMINCOMPLETE), mDpHiiHandle, SummaryData.NumIncomplete);
111 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DP_STATS_NUMPHASES), mDpHiiHandle, SummaryData.NumSummary);
112 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DP_STATS_NUMHANDLES), mDpHiiHandle, SummaryData.NumHandles, SummaryData.NumTrace - SummaryData.NumHandles);
113 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DP_STATS_NUMPEIMS), mDpHiiHandle, SummaryData.NumPEIMs);
114 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DP_STATS_NUMGLOBALS), mDpHiiHandle, SummaryData.NumGlobal);
115 SHELL_FREE_NON_NULL (StringPtr);
116 SHELL_FREE_NON_NULL (StringPtrUnknown);
117 }
118
119 /**
120 This function scan ACPI table in RSDT.
121
122 @param Rsdt ACPI RSDT
123 @param Signature ACPI table signature
124
125 @return ACPI table
126 **/
127 VOID *
128 ScanTableInRSDT (
129 IN RSDT_TABLE *Rsdt,
130 IN UINT32 Signature
131 )
132 {
133 UINTN Index;
134 UINT32 EntryCount;
135 UINT32 *EntryPtr;
136 EFI_ACPI_DESCRIPTION_HEADER *Table;
137
138 EntryCount = (Rsdt->Header.Length - sizeof (EFI_ACPI_DESCRIPTION_HEADER)) / sizeof(UINT32);
139
140 EntryPtr = &Rsdt->Entry;
141 for (Index = 0; Index < EntryCount; Index ++, EntryPtr ++) {
142 Table = (EFI_ACPI_DESCRIPTION_HEADER*)((UINTN)(*EntryPtr));
143 if (Table->Signature == Signature) {
144 return Table;
145 }
146 }
147
148 return NULL;
149 }
150
151 /**
152 This function scan ACPI table in XSDT.
153
154 @param Xsdt ACPI XSDT
155 @param Signature ACPI table signature
156
157 @return ACPI table
158 **/
159 VOID *
160 ScanTableInXSDT (
161 IN XSDT_TABLE *Xsdt,
162 IN UINT32 Signature
163 )
164 {
165 UINTN Index;
166 UINT32 EntryCount;
167 UINT64 EntryPtr;
168 UINTN BasePtr;
169 EFI_ACPI_DESCRIPTION_HEADER *Table;
170
171 EntryCount = (Xsdt->Header.Length - sizeof (EFI_ACPI_DESCRIPTION_HEADER)) / sizeof(UINT64);
172
173 BasePtr = (UINTN)(&(Xsdt->Entry));
174 for (Index = 0; Index < EntryCount; Index ++) {
175 CopyMem (&EntryPtr, (VOID *)(BasePtr + Index * sizeof(UINT64)), sizeof(UINT64));
176 Table = (EFI_ACPI_DESCRIPTION_HEADER*)((UINTN)(EntryPtr));
177 if (Table->Signature == Signature) {
178 return Table;
179 }
180 }
181
182 return NULL;
183 }
184
185 /**
186 This function scan ACPI table in RSDP.
187
188 @param Rsdp ACPI RSDP
189 @param Signature ACPI table signature
190
191 @return ACPI table
192 **/
193 VOID *
194 FindAcpiPtr (
195 IN EFI_ACPI_5_0_ROOT_SYSTEM_DESCRIPTION_POINTER *Rsdp,
196 IN UINT32 Signature
197 )
198 {
199 EFI_ACPI_DESCRIPTION_HEADER *AcpiTable;
200 RSDT_TABLE *Rsdt;
201 XSDT_TABLE *Xsdt;
202
203 AcpiTable = NULL;
204
205 //
206 // Check ACPI2.0 table
207 //
208 Rsdt = (RSDT_TABLE *)(UINTN)Rsdp->RsdtAddress;
209 Xsdt = NULL;
210 if ((Rsdp->Revision >= 2) && (Rsdp->XsdtAddress < (UINT64)(UINTN)-1)) {
211 Xsdt = (XSDT_TABLE *)(UINTN)Rsdp->XsdtAddress;
212 }
213 //
214 // Check Xsdt
215 //
216 if (Xsdt != NULL) {
217 AcpiTable = ScanTableInXSDT (Xsdt, Signature);
218 }
219 //
220 // Check Rsdt
221 //
222 if ((AcpiTable == NULL) && (Rsdt != NULL)) {
223 AcpiTable = ScanTableInRSDT (Rsdt, Signature);
224 }
225
226 return AcpiTable;
227 }
228
229 /**
230 Get Boot performance table form Acpi table.
231
232 **/
233 EFI_STATUS
234 GetBootPerformanceTable (
235 )
236 {
237 EFI_STATUS Status;
238 VOID *AcpiTable;
239 FIRMWARE_PERFORMANCE_TABLE *FirmwarePerformanceTable;
240
241 AcpiTable = NULL;
242
243 Status = EfiGetSystemConfigurationTable (
244 &gEfiAcpi20TableGuid,
245 &AcpiTable
246 );
247 if (EFI_ERROR (Status)) {
248 Status = EfiGetSystemConfigurationTable (
249 &gEfiAcpi10TableGuid,
250 &AcpiTable
251 );
252 }
253 if (EFI_ERROR(Status) || AcpiTable == NULL) {
254 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DP_GET_ACPI_TABLE_FAIL), mDpHiiHandle);
255 return Status;
256 }
257
258 FirmwarePerformanceTable = FindAcpiPtr (
259 (EFI_ACPI_5_0_ROOT_SYSTEM_DESCRIPTION_POINTER *)AcpiTable,
260 EFI_ACPI_5_0_FIRMWARE_PERFORMANCE_DATA_TABLE_SIGNATURE
261 );
262 if (FirmwarePerformanceTable == NULL) {
263 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DP_GET_ACPI_FPDT_FAIL), mDpHiiHandle);
264 return EFI_NOT_FOUND;
265 }
266
267 mBootPerformanceTable = (UINT8*) (UINTN)FirmwarePerformanceTable->BootPointerRecord.BootPerformanceTablePointer;
268 mBootPerformanceTableSize = ((BOOT_PERFORMANCE_TABLE *) mBootPerformanceTable)->Header.Length;
269
270 return EFI_SUCCESS;
271 }
272
273 /**
274 Get Handle form Module Guid.
275
276 @param ModuleGuid Module Guid.
277 @param Handle The handle to be returned.
278
279 **/
280 VOID
281 GetHandleFormModuleGuid (
282 IN EFI_GUID *ModuleGuid,
283 IN OUT EFI_HANDLE *Handle
284 )
285 {
286 UINTN Index;
287
288 if (IsZeroGuid (ModuleGuid)) {
289 *Handle = NULL;
290 }
291 //
292 // Try to get the Handle form the caached array.
293 //
294 for (Index = 0; Index < mCachePairCount; Index++) {
295 if (CompareGuid (ModuleGuid, &mCacheHandleGuidTable[Index].ModuleGuid)) {
296 *Handle = mCacheHandleGuidTable[Index].Handle;
297 break;
298 }
299 }
300 if (Index >= mCachePairCount) {
301 *Handle = NULL;
302 }
303 }
304
305 /**
306 Cache the GUID and handle mapping pairs. In order to save time for searching.
307
308 **/
309 EFI_STATUS
310 BuildCachedGuidHandleTable (
311 VOID
312 )
313 {
314 EFI_STATUS Status;
315 EFI_HANDLE *HandleBuffer;
316 UINTN HandleCount;
317 UINTN Index;
318 EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;
319 EFI_DRIVER_BINDING_PROTOCOL *DriverBinding;
320 EFI_GUID *TempGuid;
321 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *FvFilePath;
322
323 Status = gBS->LocateHandleBuffer (AllHandles, NULL, NULL, &HandleCount, &HandleBuffer);
324 if (EFI_ERROR (Status)) {
325 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DP_HANDLES_ERROR), mDpHiiHandle, Status);
326 return Status;
327 }
328
329 mCacheHandleGuidTable = AllocateZeroPool (HandleCount * sizeof (HANDLE_GUID_MAP));
330 if (mCacheHandleGuidTable == NULL) {
331 return EFI_OUT_OF_RESOURCES;
332 }
333
334 for (Index = 0; Index < HandleCount; Index++) {
335 //
336 // Try Handle as ImageHandle.
337 //
338 Status = gBS->HandleProtocol (
339 HandleBuffer[Index],
340 &gEfiLoadedImageProtocolGuid,
341 (VOID**) &LoadedImage
342 );
343 if (EFI_ERROR (Status)) {
344 //
345 // Try Handle as Controller Handle
346 //
347 Status = gBS->OpenProtocol (
348 HandleBuffer[Index],
349 &gEfiDriverBindingProtocolGuid,
350 (VOID **) &DriverBinding,
351 NULL,
352 NULL,
353 EFI_OPEN_PROTOCOL_GET_PROTOCOL
354 );
355 if (!EFI_ERROR (Status)) {
356 //
357 // Get Image protocol from ImageHandle
358 //
359 Status = gBS->HandleProtocol (
360 DriverBinding->ImageHandle,
361 &gEfiLoadedImageProtocolGuid,
362 (VOID**) &LoadedImage
363 );
364 }
365 }
366
367 if (!EFI_ERROR (Status) && LoadedImage != NULL) {
368 //
369 // Get Module Guid from DevicePath.
370 //
371 if (LoadedImage->FilePath != NULL &&
372 LoadedImage->FilePath->Type == MEDIA_DEVICE_PATH &&
373 LoadedImage->FilePath->SubType == MEDIA_PIWG_FW_FILE_DP
374 ) {
375 FvFilePath = (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) LoadedImage->FilePath;
376 TempGuid = &FvFilePath->FvFileName;
377
378 mCacheHandleGuidTable[mCachePairCount].Handle = HandleBuffer[Index];
379 CopyGuid (&mCacheHandleGuidTable[mCachePairCount].ModuleGuid, TempGuid);
380 mCachePairCount ++;
381 }
382 }
383 }
384 if (HandleBuffer != NULL) {
385 FreePool (HandleBuffer);
386 HandleBuffer = NULL;
387 }
388 return EFI_SUCCESS;
389 }
390
391 /**
392 Get Measurement form Fpdt records.
393
394 @param RecordHeader Pointer to the start record.
395 @param IsStart Is start record or End record.
396 @param Measurement Pointer to the measurement which need to be filled.
397
398 **/
399 VOID
400 GetMeasurementInfo (
401 IN EFI_ACPI_5_0_FPDT_PERFORMANCE_RECORD_HEADER *RecordHeader,
402 IN BOOLEAN IsStart,
403 IN OUT MEASUREMENT_RECORD *Measurement
404 )
405 {
406 VOID *ModuleGuid;
407 EFI_HANDLE StartHandle;
408
409 switch (RecordHeader->Type) {
410 case FPDT_GUID_EVENT_TYPE:
411 ModuleGuid = &(((FPDT_GUID_EVENT_RECORD *)RecordHeader)->Guid);
412 Measurement->Identifier = ((UINT32)((FPDT_GUID_EVENT_RECORD *)RecordHeader)->ProgressID);
413 if (IsStart) {
414 Measurement->StartTimeStamp = ((FPDT_GUID_EVENT_RECORD *)RecordHeader)->Timestamp;
415 } else {
416 Measurement->EndTimeStamp = ((FPDT_GUID_EVENT_RECORD *)RecordHeader)->Timestamp;
417 }
418 switch (Measurement->Identifier) {
419 case MODULE_START_ID:
420 case MODULE_END_ID:
421 if (mPeiPhase) {
422 Measurement->Token = ALit_PEIM;
423 Measurement->Module = ALit_PEIM;
424 } else if (mDxePhase) {
425 Measurement->Token = ALit_START_IMAGE;
426 Measurement->Module = ALit_START_IMAGE;
427 }
428 break;
429 default:
430 ASSERT(FALSE);
431 }
432
433 if (AsciiStrCmp (Measurement->Token, ALit_PEIM) == 0) {
434 Measurement->Handle = &(((FPDT_GUID_EVENT_RECORD *)RecordHeader)->Guid);
435 } else {
436 GetHandleFormModuleGuid(ModuleGuid, &StartHandle);
437 Measurement->Handle = StartHandle;
438 }
439 break;
440
441 case FPDT_DYNAMIC_STRING_EVENT_TYPE:
442 ModuleGuid = &(((FPDT_DYNAMIC_STRING_EVENT_RECORD *)RecordHeader)->Guid);
443 Measurement->Identifier = ((UINT32)((FPDT_DYNAMIC_STRING_EVENT_RECORD *)RecordHeader)->ProgressID);
444 if (IsStart) {
445 Measurement->StartTimeStamp = ((FPDT_DYNAMIC_STRING_EVENT_RECORD *)RecordHeader)->Timestamp;
446 } else {
447 Measurement->EndTimeStamp = ((FPDT_DYNAMIC_STRING_EVENT_RECORD *)RecordHeader)->Timestamp;
448 }
449 switch (Measurement->Identifier) {
450 case MODULE_START_ID:
451 case MODULE_END_ID:
452 if (mPeiPhase) {
453 Measurement->Token = ALit_PEIM;
454 } else if (mDxePhase) {
455 Measurement->Token = ALit_START_IMAGE;
456 }
457 break;
458
459 case MODULE_LOADIMAGE_START_ID:
460 case MODULE_LOADIMAGE_END_ID:
461 Measurement->Token = ALit_LOAD_IMAGE;
462 break;
463
464 case MODULE_DB_START_ID:
465 case MODULE_DB_END_ID:
466 Measurement->Token = ALit_DB_START;
467 break;
468
469 case MODULE_DB_SUPPORT_START_ID:
470 case MODULE_DB_SUPPORT_END_ID:
471 Measurement->Token = ALit_DB_SUPPORT;
472 break;
473
474 case MODULE_DB_STOP_START_ID:
475 case MODULE_DB_STOP_END_ID:
476 Measurement->Token = ALit_DB_STOP;
477 break;
478
479 default:
480 Measurement->Token = ((FPDT_DYNAMIC_STRING_EVENT_RECORD *)RecordHeader)->String;
481 break;
482 }
483
484 Measurement->Module = ((FPDT_DYNAMIC_STRING_EVENT_RECORD *)RecordHeader)->String;
485
486 if (AsciiStrCmp (Measurement->Token, ALit_PEIM) == 0) {
487 Measurement->Handle = &(((FPDT_DYNAMIC_STRING_EVENT_RECORD *)RecordHeader)->Guid);
488 } else {
489 GetHandleFormModuleGuid(ModuleGuid, &StartHandle);
490 Measurement->Handle = StartHandle;
491 }
492 break;
493
494 case FPDT_GUID_QWORD_EVENT_TYPE:
495 ModuleGuid = &(((FPDT_GUID_QWORD_EVENT_RECORD *)RecordHeader)->Guid);
496 Measurement->Identifier = ((UINT32)((FPDT_GUID_QWORD_EVENT_RECORD *)RecordHeader)->ProgressID);
497 if (IsStart) {
498 Measurement->StartTimeStamp = ((FPDT_GUID_QWORD_EVENT_RECORD *)RecordHeader)->Timestamp;
499 } else {
500 Measurement->EndTimeStamp = ((FPDT_GUID_QWORD_EVENT_RECORD *)RecordHeader)->Timestamp;
501 }
502 switch (Measurement->Identifier) {
503 case MODULE_DB_START_ID:
504 Measurement->Token = ALit_DB_START;
505 Measurement->Module = ALit_DB_START;
506 break;
507
508 case MODULE_DB_SUPPORT_START_ID:
509 case MODULE_DB_SUPPORT_END_ID:
510 Measurement->Token = ALit_DB_SUPPORT;
511 Measurement->Module = ALit_DB_SUPPORT;
512 break;
513
514 case MODULE_DB_STOP_START_ID:
515 case MODULE_DB_STOP_END_ID:
516 Measurement->Token = ALit_DB_STOP;
517 Measurement->Module = ALit_DB_STOP;
518 break;
519
520 case MODULE_LOADIMAGE_START_ID:
521 case MODULE_LOADIMAGE_END_ID:
522 Measurement->Token = ALit_LOAD_IMAGE;
523 Measurement->Module = ALit_LOAD_IMAGE;
524 break;
525
526 default:
527 ASSERT(FALSE);
528 }
529 GetHandleFormModuleGuid(ModuleGuid, &StartHandle);
530 Measurement->Handle = StartHandle;
531 break;
532
533 case FPDT_GUID_QWORD_STRING_EVENT_TYPE:
534 ModuleGuid = &(((FPDT_GUID_QWORD_STRING_EVENT_RECORD *)RecordHeader)->Guid);
535 Measurement->Identifier = ((UINT32)((FPDT_GUID_QWORD_STRING_EVENT_RECORD *)RecordHeader)->ProgressID);
536 if (IsStart) {
537 Measurement->StartTimeStamp = ((FPDT_GUID_QWORD_STRING_EVENT_RECORD*)RecordHeader)->Timestamp;
538 } else {
539 Measurement->EndTimeStamp = ((FPDT_GUID_QWORD_STRING_EVENT_RECORD *)RecordHeader)->Timestamp;
540 }
541 //
542 // Currently only "DB:Start:" end record with FPDT_GUID_QWORD_STRING_EVENT_TYPE.
543 //
544 switch (Measurement->Identifier) {
545 case MODULE_DB_END_ID:
546 Measurement->Token = ALit_DB_START;
547 Measurement->Module = ALit_DB_START;
548 break;
549 default:
550 ASSERT(FALSE);
551 }
552 GetHandleFormModuleGuid(ModuleGuid, &StartHandle);
553 Measurement->Handle = StartHandle;
554 break;
555
556 default:
557 break;
558 }
559 }
560
561 /**
562 Search the start measurement in the mMeasurementList for the end measurement.
563
564 @param EndMeasureMent Measurement for end record.
565
566 **/
567 VOID
568 SearchMeasurement (
569 IN MEASUREMENT_RECORD *EndMeasureMent
570 )
571 {
572 INTN Index;
573
574 for (Index = mMeasurementNum - 1; Index >= 0; Index--) {
575 if (AsciiStrCmp (EndMeasureMent->Token, ALit_PEIM) == 0) {
576 if (mMeasurementList[Index].EndTimeStamp == 0 && EndMeasureMent->Handle!= NULL && mMeasurementList[Index].Handle != NULL&&
577 CompareGuid(mMeasurementList[Index].Handle, EndMeasureMent->Handle) &&
578 (AsciiStrCmp (mMeasurementList[Index].Token, EndMeasureMent->Token) == 0) &&
579 (AsciiStrCmp (mMeasurementList[Index].Module, EndMeasureMent->Module) == 0)) {
580 mMeasurementList[Index].EndTimeStamp = EndMeasureMent->EndTimeStamp;
581 break;
582 }
583 } else {
584 if (mMeasurementList[Index].EndTimeStamp == 0 && mMeasurementList[Index].Handle == EndMeasureMent->Handle &&
585 (AsciiStrCmp (mMeasurementList[Index].Token, EndMeasureMent->Token) == 0) &&
586 (AsciiStrCmp (mMeasurementList[Index].Module, EndMeasureMent->Module) == 0)) {
587 mMeasurementList[Index].EndTimeStamp = EndMeasureMent->EndTimeStamp;
588 break;
589 }
590 }
591 }
592 }
593
594 /**
595 Generate the measure record array.
596
597 **/
598 EFI_STATUS
599 BuildMeasurementList (
600 )
601 {
602 EFI_ACPI_5_0_FPDT_PERFORMANCE_RECORD_HEADER *RecordHeader;
603 UINT8 *PerformanceTablePtr;
604 UINT16 StartProgressId;
605 UINTN TableLength;
606 UINT8 *StartRecordEvent;
607 MEASUREMENT_RECORD MeasureMent;
608
609 mMeasurementList = AllocateZeroPool (mBootPerformanceTableSize);
610 if (mMeasurementList == NULL) {
611 return EFI_OUT_OF_RESOURCES;
612 }
613
614 TableLength = sizeof (BOOT_PERFORMANCE_TABLE);
615 PerformanceTablePtr = (mBootPerformanceTable + TableLength);
616
617 while (TableLength < mBootPerformanceTableSize) {
618 RecordHeader = (EFI_ACPI_5_0_FPDT_PERFORMANCE_RECORD_HEADER*) PerformanceTablePtr;
619 StartRecordEvent = (UINT8 *)RecordHeader;
620 StartProgressId = ((FPDT_GUID_EVENT_RECORD *)StartRecordEvent)->ProgressID;
621
622 //
623 // If the record is the start record, fill the info to the measurement in the mMeasurementList.
624 // If the record is the end record, find the related start measurement in the mMeasurementList and fill the EndTimeStamp.
625 //
626 if (((StartProgressId >= PERF_EVENTSIGNAL_START_ID && ((StartProgressId & 0x000F) == 0)) ||
627 (StartProgressId < PERF_EVENTSIGNAL_START_ID && ((StartProgressId & 0x0001) != 0)))) {
628 //
629 // Since PEIM and StartImage has same Type and ID when PCD PcdEdkiiFpdtStringRecordEnableOnly = FALSE
630 // So we need to identify these two kinds of record through different phase.
631 //
632 if (AsciiStrCmp (((FPDT_DYNAMIC_STRING_EVENT_RECORD *)StartRecordEvent)->String, ALit_PEI) == 0) {
633 mPeiPhase = TRUE;
634 } else if (AsciiStrCmp (((FPDT_DYNAMIC_STRING_EVENT_RECORD *)StartRecordEvent)->String, ALit_DXE) == 0) {
635 mDxePhase = TRUE;
636 mPeiPhase = FALSE;
637 }
638 // Get measurement info form the start record to the mMeasurementList.
639 GetMeasurementInfo (RecordHeader, TRUE, &(mMeasurementList[mMeasurementNum]));
640 mMeasurementNum ++;
641 } else {
642 GetMeasurementInfo (RecordHeader, FALSE, &MeasureMent);
643 SearchMeasurement (&MeasureMent);
644 }
645 TableLength += RecordHeader->Length;
646 PerformanceTablePtr += RecordHeader->Length;
647 }
648 return EFI_SUCCESS;
649 }
650
651 /**
652 Initialize the cumulative data.
653
654 **/
655 VOID
656 InitCumulativeData (
657 VOID
658 )
659 {
660 UINTN Index;
661
662 for (Index = 0; Index < NumCum; ++Index) {
663 CumData[Index].Count = 0;
664 CumData[Index].MinDur = PERF_MAXDUR;
665 CumData[Index].MaxDur = 0;
666 CumData[Index].Duration = 0;
667 }
668 }
669
670 /**
671 Initialize the Summary data.
672
673 **/
674 VOID
675 InitSummaryData (
676 VOID
677 )
678 {
679 SummaryData.NumTrace = 0;
680 SummaryData.NumIncomplete = 0;
681 SummaryData.NumSummary = 0;
682 SummaryData.NumHandles = 0;
683 SummaryData.NumPEIMs = 0;
684 SummaryData.NumGlobal = 0;
685 }
686
687 /**
688 Dump performance data.
689
690 @param[in] ImageHandle The image handle.
691 @param[in] SystemTable The system table.
692
693 @retval SHELL_SUCCESS Command completed successfully.
694 @retval SHELL_INVALID_PARAMETER Command usage error.
695 @retval SHELL_ABORTED The user aborts the operation.
696 @retval value Unknown error.
697 **/
698 SHELL_STATUS
699 RunDp (
700 IN EFI_HANDLE ImageHandle,
701 IN EFI_SYSTEM_TABLE *SystemTable
702 )
703 {
704 LIST_ENTRY *ParamPackage;
705 CONST CHAR16 *CmdLineArg;
706 EFI_STATUS Status;
707
708 PERFORMANCE_PROPERTY *PerformanceProperty;
709 UINTN Number2Display;
710
711 EFI_STRING StringPtr;
712 BOOLEAN SummaryMode;
713 BOOLEAN VerboseMode;
714 BOOLEAN AllMode;
715 BOOLEAN RawMode;
716 BOOLEAN ExcludeMode;
717 BOOLEAN CumulativeMode;
718 CONST CHAR16 *CustomCumulativeToken;
719 PERF_CUM_DATA *CustomCumulativeData;
720 UINTN NameSize;
721 SHELL_STATUS ShellStatus;
722 TIMER_INFO TimerInfo;
723
724 StringPtr = NULL;
725 SummaryMode = FALSE;
726 VerboseMode = FALSE;
727 AllMode = FALSE;
728 RawMode = FALSE;
729 ExcludeMode = FALSE;
730 CumulativeMode = FALSE;
731 CustomCumulativeData = NULL;
732 ShellStatus = SHELL_SUCCESS;
733
734 //
735 // initialize the shell lib (we must be in non-auto-init...)
736 //
737 Status = ShellInitialize();
738 ASSERT_EFI_ERROR(Status);
739
740 //
741 // Process Command Line arguments
742 //
743 Status = ShellCommandLineParse (ParamList, &ParamPackage, NULL, TRUE);
744 if (EFI_ERROR(Status)) {
745 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DP_INVALID_ARG), mDpHiiHandle);
746 return SHELL_INVALID_PARAMETER;
747 }
748
749 //
750 // Boolean options
751 //
752 VerboseMode = ShellCommandLineGetFlag (ParamPackage, L"-v");
753 SummaryMode = (BOOLEAN) (ShellCommandLineGetFlag (ParamPackage, L"-S") || ShellCommandLineGetFlag (ParamPackage, L"-s"));
754 AllMode = ShellCommandLineGetFlag (ParamPackage, L"-A");
755 RawMode = ShellCommandLineGetFlag (ParamPackage, L"-R");
756 ExcludeMode = ShellCommandLineGetFlag (ParamPackage, L"-x");
757 mShowId = ShellCommandLineGetFlag (ParamPackage, L"-i");
758 CumulativeMode = ShellCommandLineGetFlag (ParamPackage, L"-c");
759
760 // Options with Values
761 CmdLineArg = ShellCommandLineGetValue (ParamPackage, L"-n");
762 if (CmdLineArg == NULL) {
763 Number2Display = DEFAULT_DISPLAYCOUNT;
764 } else {
765 Number2Display = StrDecimalToUintn(CmdLineArg);
766 if (Number2Display == 0) {
767 Number2Display = MAXIMUM_DISPLAYCOUNT;
768 }
769 }
770
771 CmdLineArg = ShellCommandLineGetValue (ParamPackage, L"-t");
772 if (CmdLineArg == NULL) {
773 mInterestThreshold = DEFAULT_THRESHOLD; // 1ms := 1,000 us
774 } else {
775 mInterestThreshold = StrDecimalToUint64(CmdLineArg);
776 }
777
778
779 //
780 // DP dump performance data by parsing FPDT table in ACPI table.
781 // Folloing 3 steps are to get the measurement form the FPDT table.
782 //
783
784 //
785 //1. Get FPDT from ACPI table.
786 //
787 Status = GetBootPerformanceTable ();
788 if (EFI_ERROR (Status)) {
789 ShellStatus = Status;
790 goto Done;
791 }
792
793 //
794 //2. Cache the ModuleGuid and hanlde mapping table.
795 //
796 Status = BuildCachedGuidHandleTable();
797 if (EFI_ERROR (Status)) {
798 ShellStatus = Status;
799 goto Done;
800 }
801
802 //
803 //3. Build the measurement array form the FPDT records.
804 //
805 Status = BuildMeasurementList ();
806 if (EFI_ERROR (Status)) {
807 ShellStatus = SHELL_OUT_OF_RESOURCES;
808 goto Done;
809 }
810
811 //
812 // Initialize the pre-defined cumulative data.
813 //
814 InitCumulativeData ();
815
816 //
817 // Initialize the Summary data.
818 //
819 InitSummaryData ();
820
821 //
822 // Init the custom cumulative data.
823 //
824 CustomCumulativeToken = ShellCommandLineGetValue (ParamPackage, L"-c");
825 if (CustomCumulativeToken != NULL) {
826 CustomCumulativeData = AllocateZeroPool (sizeof (PERF_CUM_DATA));
827 if (CustomCumulativeData == NULL) {
828 ShellStatus = SHELL_OUT_OF_RESOURCES;
829 goto Done;
830 }
831 CustomCumulativeData->MinDur = PERF_MAXDUR;
832 CustomCumulativeData->MaxDur = 0;
833 CustomCumulativeData->Count = 0;
834 CustomCumulativeData->Duration = 0;
835 NameSize = StrLen (CustomCumulativeToken) + 1;
836 CustomCumulativeData->Name = AllocateZeroPool (NameSize);
837 if (CustomCumulativeData->Name == NULL) {
838 ShellStatus = SHELL_OUT_OF_RESOURCES;
839 goto Done;
840 }
841 UnicodeStrToAsciiStrS (CustomCumulativeToken, CustomCumulativeData->Name, NameSize);
842 }
843
844 //
845 // Timer specific processing
846 //
847 // Get the Performance counter characteristics:
848 // Freq = Frequency in Hz
849 // StartCount = Value loaded into the counter when it starts counting
850 // EndCount = Value counter counts to before it needs to be reset
851 //
852 Status = EfiGetSystemConfigurationTable (&gPerformanceProtocolGuid, (VOID **) &PerformanceProperty);
853 if (EFI_ERROR (Status) || (PerformanceProperty == NULL)) {
854 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_PERF_PROPERTY_NOT_FOUND), mDpHiiHandle);
855 goto Done;
856 }
857
858 TimerInfo.Frequency = (UINT32)DivU64x32 (PerformanceProperty->Frequency, 1000);
859 TimerInfo.StartCount = 0;
860 TimerInfo.EndCount = 0xFFFF;
861 TimerInfo.CountUp = TRUE;
862
863 //
864 // Print header
865 //
866 // print DP's build version
867 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DP_BUILD_REVISION), mDpHiiHandle, DP_MAJOR_VERSION, DP_MINOR_VERSION);
868
869 // print performance timer characteristics
870 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DP_KHZ), mDpHiiHandle, TimerInfo.Frequency);
871
872 if (VerboseMode && !RawMode) {
873 StringPtr = HiiGetString (mDpHiiHandle,
874 (EFI_STRING_ID) (TimerInfo.CountUp ? STRING_TOKEN (STR_DP_UP) : STRING_TOKEN (STR_DP_DOWN)), NULL);
875 ASSERT (StringPtr != NULL);
876 // Print Timer count range and direction
877 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DP_TIMER_PROPERTIES), mDpHiiHandle,
878 StringPtr,
879 TimerInfo.StartCount,
880 TimerInfo.EndCount
881 );
882 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DP_VERBOSE_THRESHOLD), mDpHiiHandle, mInterestThreshold);
883 }
884
885 /****************************************************************************
886 **** Print Sections based on command line options
887 ****
888 **** Option modes have the following priority:
889 **** v Verbose -- Valid in combination with any other options
890 **** t Threshold -- Modifies All, Raw, and Cooked output
891 **** Default is 0 for All and Raw mode
892 **** Default is DEFAULT_THRESHOLD for "Cooked" mode
893 **** n Number2Display Used by All and Raw mode. Otherwise ignored.
894 **** A All -- R and S options are ignored
895 **** R Raw -- S option is ignored
896 **** s Summary -- Modifies "Cooked" output only
897 **** Cooked (Default)
898 ****************************************************************************/
899 GatherStatistics (CustomCumulativeData);
900 if (CumulativeMode) {
901 ProcessCumulative (CustomCumulativeData);
902 } else if (AllMode) {
903 Status = DumpAllTrace( Number2Display, ExcludeMode);
904 if (Status == EFI_ABORTED) {
905 ShellStatus = SHELL_ABORTED;
906 goto Done;
907 }
908 } else if (RawMode) {
909 Status = DumpRawTrace( Number2Display, ExcludeMode);
910 if (Status == EFI_ABORTED) {
911 ShellStatus = SHELL_ABORTED;
912 goto Done;
913 }
914 } else {
915 //------------- Begin Cooked Mode Processing
916 ProcessPhases ();
917 if ( ! SummaryMode) {
918 Status = ProcessHandles ( ExcludeMode);
919 if (Status == EFI_ABORTED) {
920 ShellStatus = SHELL_ABORTED;
921 goto Done;
922 }
923
924 Status = ProcessPeims ();
925 if (Status == EFI_ABORTED) {
926 ShellStatus = SHELL_ABORTED;
927 goto Done;
928 }
929
930 Status = ProcessGlobal ();
931 if (Status == EFI_ABORTED) {
932 ShellStatus = SHELL_ABORTED;
933 goto Done;
934 }
935
936 ProcessCumulative (NULL);
937 }
938 } //------------- End of Cooked Mode Processing
939 if ( VerboseMode || SummaryMode) {
940 DumpStatistics();
941 }
942
943 Done:
944 if (ParamPackage != NULL) {
945 ShellCommandLineFreeVarList (ParamPackage);
946 }
947 SHELL_FREE_NON_NULL (StringPtr);
948 if (CustomCumulativeData != NULL) {
949 SHELL_FREE_NON_NULL (CustomCumulativeData->Name);
950 }
951 SHELL_FREE_NON_NULL (CustomCumulativeData);
952
953 SHELL_FREE_NON_NULL (mMeasurementList);
954
955 SHELL_FREE_NON_NULL (mCacheHandleGuidTable);
956
957 mMeasurementNum = 0;
958 mCachePairCount = 0;
959 return ShellStatus;
960 }
961
962
963 /**
964 Retrive HII package list from ImageHandle and publish to HII database.
965
966 @param ImageHandle The image handle of the process.
967
968 @return HII handle.
969 **/
970 EFI_HANDLE
971 InitializeHiiPackage (
972 EFI_HANDLE ImageHandle
973 )
974 {
975 EFI_STATUS Status;
976 EFI_HII_PACKAGE_LIST_HEADER *PackageList;
977 EFI_HANDLE HiiHandle;
978
979 //
980 // Retrieve HII package list from ImageHandle
981 //
982 Status = gBS->OpenProtocol (
983 ImageHandle,
984 &gEfiHiiPackageListProtocolGuid,
985 (VOID **)&PackageList,
986 ImageHandle,
987 NULL,
988 EFI_OPEN_PROTOCOL_GET_PROTOCOL
989 );
990 ASSERT_EFI_ERROR (Status);
991 if (EFI_ERROR (Status)) {
992 return NULL;
993 }
994
995 //
996 // Publish HII package list to HII Database.
997 //
998 Status = gHiiDatabase->NewPackageList (
999 gHiiDatabase,
1000 PackageList,
1001 NULL,
1002 &HiiHandle
1003 );
1004 ASSERT_EFI_ERROR (Status);
1005 if (EFI_ERROR (Status)) {
1006 return NULL;
1007 }
1008 return HiiHandle;
1009 }