DpTrace.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  1. /** @file
  2. Trace reporting for the Dp utility.
  3. Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.
  4. (C) Copyright 2015-2016 Hewlett Packard Enterprise Development LP<BR>
  5. SPDX-License-Identifier: BSD-2-Clause-Patent
  6. **/
  7. #include <Library/BaseLib.h>
  8. #include <Library/BaseMemoryLib.h>
  9. #include <Library/MemoryAllocationLib.h>
  10. #include <Library/DebugLib.h>
  11. #include <Library/UefiBootServicesTableLib.h>
  12. #include <Library/PeCoffGetEntryPointLib.h>
  13. #include <Library/PerformanceLib.h>
  14. #include <Library/PrintLib.h>
  15. #include <Library/HiiLib.h>
  16. #include <Library/PcdLib.h>
  17. #include "Dp.h"
  18. #include "Literals.h"
  19. #include "DpInternal.h"
  20. /**
  21. Attempts to retrieve a performance measurement log entry from the performance measurement log.
  22. @param LogEntryKey On entry, the key of the performance measurement log entry to retrieve.
  23. 0, then the first performance measurement log entry is retrieved.
  24. On exit, the key of the next performance log entry.
  25. @param Handle Pointer to environment specific context used to identify the component
  26. being measured.
  27. @param Token Pointer to a Null-terminated ASCII string that identifies the component
  28. being measured.
  29. @param Module Pointer to a Null-terminated ASCII string that identifies the module
  30. being measured.
  31. @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
  32. was started.
  33. @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
  34. was ended.
  35. @param Identifier Pointer to the 32-bit identifier that was recorded when the measurement
  36. was ended.
  37. @return The key for the next performance log entry (in general case).
  38. **/
  39. UINTN
  40. GetPerformanceMeasurementRecord (
  41. IN UINTN LogEntryKey,
  42. OUT CONST VOID **Handle,
  43. OUT CONST CHAR8 **Token,
  44. OUT CONST CHAR8 **Module,
  45. OUT UINT64 *StartTimeStamp,
  46. OUT UINT64 *EndTimeStamp,
  47. OUT UINT32 *Identifier
  48. )
  49. {
  50. if (LogEntryKey == mMeasurementNum) {
  51. return 0;
  52. }
  53. *Handle = (VOID *)(UINTN)mMeasurementList[LogEntryKey].Handle;
  54. *Token = mMeasurementList[LogEntryKey].Token;
  55. *Module = mMeasurementList[LogEntryKey].Module;
  56. *StartTimeStamp = mMeasurementList[LogEntryKey].StartTimeStamp;
  57. *EndTimeStamp = mMeasurementList[LogEntryKey].EndTimeStamp;
  58. *Identifier = mMeasurementList[LogEntryKey].Identifier;
  59. LogEntryKey++;
  60. return LogEntryKey;
  61. }
  62. /**
  63. Collect verbose statistics about the logged performance measurements.
  64. General Summary information for all Trace measurements is gathered and
  65. stored within the SummaryData structure. This information is both
  66. used internally by subsequent reporting functions, and displayed
  67. at the end of verbose reports.
  68. @pre The SummaryData and CumData structures must be initialized
  69. prior to calling this function.
  70. @post The SummaryData and CumData structures contain statistics for the
  71. current performance logs.
  72. @param[in, out] CustomCumulativeData A pointer to the custom cumulative data.
  73. **/
  74. VOID
  75. GatherStatistics (
  76. IN OUT PERF_CUM_DATA *CustomCumulativeData OPTIONAL
  77. )
  78. {
  79. MEASUREMENT_RECORD Measurement;
  80. UINT64 Duration;
  81. UINTN LogEntryKey;
  82. INTN TIndex;
  83. LogEntryKey = 0;
  84. while ((LogEntryKey = GetPerformanceMeasurementRecord (
  85. LogEntryKey,
  86. &Measurement.Handle,
  87. &Measurement.Token,
  88. &Measurement.Module,
  89. &Measurement.StartTimeStamp,
  90. &Measurement.EndTimeStamp,
  91. &Measurement.Identifier
  92. )) != 0)
  93. {
  94. ++SummaryData.NumTrace; // Count the number of TRACE Measurement records
  95. if (Measurement.EndTimeStamp == 0) {
  96. ++SummaryData.NumIncomplete; // Count the incomplete records
  97. continue;
  98. }
  99. if (Measurement.Handle != NULL) {
  100. ++SummaryData.NumHandles; // Count the number of measurements with non-NULL handles
  101. }
  102. if (IsPhase (&Measurement)) {
  103. ++SummaryData.NumSummary; // Count the number of major phases
  104. } else {
  105. // !IsPhase
  106. if (Measurement.Handle == NULL) {
  107. ++SummaryData.NumGlobal;
  108. }
  109. }
  110. if (AsciiStrCmp (Measurement.Token, ALit_PEIM) == 0) {
  111. ++SummaryData.NumPEIMs; // Count PEIM measurements
  112. }
  113. Duration = GetDuration (&Measurement);
  114. TIndex = GetCumulativeItem (&Measurement);
  115. if (TIndex >= 0) {
  116. CumData[TIndex].Duration += Duration;
  117. CumData[TIndex].Count++;
  118. if ( Duration < CumData[TIndex].MinDur ) {
  119. CumData[TIndex].MinDur = Duration;
  120. }
  121. if ( Duration > CumData[TIndex].MaxDur ) {
  122. CumData[TIndex].MaxDur = Duration;
  123. }
  124. }
  125. //
  126. // Collect the data for custom cumulative data.
  127. //
  128. if ((CustomCumulativeData != NULL) && (AsciiStrCmp (Measurement.Token, CustomCumulativeData->Name) == 0)) {
  129. CustomCumulativeData->Duration += Duration;
  130. CustomCumulativeData->Count++;
  131. if (Duration < CustomCumulativeData->MinDur) {
  132. CustomCumulativeData->MinDur = Duration;
  133. }
  134. if (Duration > CustomCumulativeData->MaxDur) {
  135. CustomCumulativeData->MaxDur = Duration;
  136. }
  137. }
  138. }
  139. }
  140. /**
  141. Gather and print ALL Trace Records.
  142. Displays all "interesting" Trace measurements in order.<BR>
  143. The number of records displayed is controlled by:
  144. - records with a duration less than mInterestThreshold microseconds are not displayed.
  145. - No more than Limit records are displayed. A Limit of zero will not limit the output.
  146. - If the ExcludeFlag is TRUE, records matching entries in the CumData array are not
  147. displayed.
  148. @pre The mInterestThreshold global variable is set to the shortest duration to be printed.
  149. The mGaugeString and mUnicodeToken global arrays are used for temporary string storage.
  150. They must not be in use by a calling function.
  151. @param[in] Limit The number of records to print. Zero is ALL.
  152. @param[in] ExcludeFlag TRUE to exclude individual Cumulative items from display.
  153. @retval EFI_SUCCESS The operation was successful.
  154. @retval EFI_ABORTED The user aborts the operation.
  155. @return Others from a call to gBS->LocateHandleBuffer().
  156. **/
  157. EFI_STATUS
  158. DumpAllTrace (
  159. IN UINTN Limit,
  160. IN BOOLEAN ExcludeFlag
  161. )
  162. {
  163. MEASUREMENT_RECORD Measurement;
  164. UINT64 ElapsedTime;
  165. UINT64 Duration;
  166. CHAR16 *IncFlag;
  167. UINTN LogEntryKey;
  168. UINTN Count;
  169. UINTN Index;
  170. UINTN TIndex;
  171. EFI_HANDLE *HandleBuffer;
  172. UINTN HandleCount;
  173. EFI_STATUS Status;
  174. EFI_STRING StringPtrUnknown;
  175. StringPtrUnknown = HiiGetString (mDpHiiHandle, STRING_TOKEN (STR_ALIT_UNKNOWN), NULL);
  176. IncFlag = HiiGetString (mDpHiiHandle, STRING_TOKEN (STR_DP_SECTION_ALL), NULL);
  177. ShellPrintHiiEx (
  178. -1,
  179. -1,
  180. NULL,
  181. STRING_TOKEN (STR_DP_SECTION_HEADER),
  182. mDpHiiHandle,
  183. (IncFlag == NULL) ? StringPtrUnknown : IncFlag
  184. );
  185. FreePool (StringPtrUnknown);
  186. // Get Handle information
  187. //
  188. Status = gBS->LocateHandleBuffer (AllHandles, NULL, NULL, &HandleCount, &HandleBuffer);
  189. if (EFI_ERROR (Status)) {
  190. ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DP_HANDLES_ERROR), mDpHiiHandle, Status);
  191. } else {
  192. // We have successfully populated the HandleBuffer
  193. // Display ALL Measurement Records
  194. // Up to Limit lines displayed
  195. // Display only records with Elapsed times >= mInterestThreshold
  196. // Display driver names in Module field for records with Handles.
  197. //
  198. if (mShowId) {
  199. ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DP_ALL_HEADR2), mDpHiiHandle);
  200. ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DP_ALL_DASHES2), mDpHiiHandle);
  201. } else {
  202. ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DP_ALL_HEADR), mDpHiiHandle);
  203. ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DP_DASHES), mDpHiiHandle);
  204. }
  205. LogEntryKey = 0;
  206. Count = 0;
  207. Index = 0;
  208. while ( WITHIN_LIMIT (Count, Limit) &&
  209. ((LogEntryKey = GetPerformanceMeasurementRecord (
  210. LogEntryKey,
  211. &Measurement.Handle,
  212. &Measurement.Token,
  213. &Measurement.Module,
  214. &Measurement.StartTimeStamp,
  215. &Measurement.EndTimeStamp,
  216. &Measurement.Identifier
  217. )) != 0)
  218. )
  219. {
  220. ++Index; // Count every record. First record is 1.
  221. ElapsedTime = 0;
  222. SHELL_FREE_NON_NULL (IncFlag);
  223. if (Measurement.EndTimeStamp != 0) {
  224. Duration = GetDuration (&Measurement);
  225. ElapsedTime = DurationInMicroSeconds (Duration);
  226. IncFlag = HiiGetString (mDpHiiHandle, STRING_TOKEN (STR_DP_COMPLETE), NULL);
  227. } else {
  228. IncFlag = HiiGetString (mDpHiiHandle, STRING_TOKEN (STR_DP_INCOMPLETE), NULL); // Mark incomplete records
  229. }
  230. if (((Measurement.EndTimeStamp != 0) && (ElapsedTime < mInterestThreshold)) ||
  231. ((ExcludeFlag) && (GetCumulativeItem (&Measurement) >= 0))
  232. ) // Ignore "uninteresting" or excluded records
  233. {
  234. continue;
  235. }
  236. ++Count; // Count the number of records printed
  237. // If Handle is non-zero, see if we can determine a name for the driver
  238. AsciiStrToUnicodeStrS (Measurement.Module, mGaugeString, ARRAY_SIZE (mGaugeString)); // Use Module by default
  239. AsciiStrToUnicodeStrS (Measurement.Token, mUnicodeToken, ARRAY_SIZE (mUnicodeToken));
  240. if (Measurement.Handle != NULL) {
  241. // See if the Handle is in the HandleBuffer
  242. for (TIndex = 0; TIndex < HandleCount; TIndex++) {
  243. if (Measurement.Handle == HandleBuffer[TIndex]) {
  244. DpGetNameFromHandle (HandleBuffer[TIndex]);
  245. break;
  246. }
  247. }
  248. }
  249. if (AsciiStrCmp (Measurement.Token, ALit_PEIM) == 0) {
  250. UnicodeSPrint (mGaugeString, sizeof (mGaugeString), L"%g", Measurement.Handle);
  251. }
  252. // Ensure that the argument strings are not too long.
  253. mGaugeString[DP_GAUGE_STRING_LENGTH] = 0;
  254. mUnicodeToken[13] = 0;
  255. if (mShowId) {
  256. ShellPrintHiiEx (
  257. -1,
  258. -1,
  259. NULL,
  260. STRING_TOKEN (STR_DP_ALL_VARS2),
  261. mDpHiiHandle,
  262. Index, // 1 based, Which measurement record is being printed
  263. IncFlag,
  264. Measurement.Handle,
  265. mGaugeString,
  266. mUnicodeToken,
  267. ElapsedTime,
  268. Measurement.Identifier
  269. );
  270. } else {
  271. ShellPrintHiiEx (
  272. -1,
  273. -1,
  274. NULL,
  275. STRING_TOKEN (STR_DP_ALL_VARS),
  276. mDpHiiHandle,
  277. Index, // 1 based, Which measurement record is being printed
  278. IncFlag,
  279. Measurement.Handle,
  280. mGaugeString,
  281. mUnicodeToken,
  282. ElapsedTime
  283. );
  284. }
  285. if (ShellGetExecutionBreakFlag ()) {
  286. Status = EFI_ABORTED;
  287. break;
  288. }
  289. }
  290. }
  291. if (HandleBuffer != NULL) {
  292. FreePool (HandleBuffer);
  293. }
  294. SHELL_FREE_NON_NULL (IncFlag);
  295. return Status;
  296. }
  297. /**
  298. Gather and print Raw Trace Records.
  299. All Trace measurements with a duration greater than or equal to
  300. mInterestThreshold are printed without interpretation.
  301. The number of records displayed is controlled by:
  302. - records with a duration less than mInterestThreshold microseconds are not displayed.
  303. - No more than Limit records are displayed. A Limit of zero will not limit the output.
  304. - If the ExcludeFlag is TRUE, records matching entries in the CumData array are not
  305. displayed.
  306. @pre The mInterestThreshold global variable is set to the shortest duration to be printed.
  307. @param[in] Limit The number of records to print. Zero is ALL.
  308. @param[in] ExcludeFlag TRUE to exclude individual Cumulative items from display.
  309. @retval EFI_SUCCESS The operation was successful.
  310. @retval EFI_ABORTED The user aborts the operation.
  311. **/
  312. EFI_STATUS
  313. DumpRawTrace (
  314. IN UINTN Limit,
  315. IN BOOLEAN ExcludeFlag
  316. )
  317. {
  318. MEASUREMENT_RECORD Measurement;
  319. UINT64 ElapsedTime;
  320. UINT64 Duration;
  321. UINTN LogEntryKey;
  322. UINTN Count;
  323. UINTN Index;
  324. EFI_STRING StringPtr;
  325. EFI_STRING StringPtrUnknown;
  326. EFI_STATUS Status;
  327. Status = EFI_SUCCESS;
  328. StringPtrUnknown = HiiGetString (mDpHiiHandle, STRING_TOKEN (STR_ALIT_UNKNOWN), NULL);
  329. StringPtr = HiiGetString (mDpHiiHandle, STRING_TOKEN (STR_DP_SECTION_RAWTRACE), NULL);
  330. ShellPrintHiiEx (
  331. -1,
  332. -1,
  333. NULL,
  334. STRING_TOKEN (STR_DP_SECTION_HEADER),
  335. mDpHiiHandle,
  336. (StringPtr == NULL) ? StringPtrUnknown : StringPtr
  337. );
  338. FreePool (StringPtr);
  339. FreePool (StringPtrUnknown);
  340. if (mShowId) {
  341. ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DP_RAW_HEADR2), mDpHiiHandle);
  342. ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DP_RAW_DASHES2), mDpHiiHandle);
  343. } else {
  344. ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DP_RAW_HEADR), mDpHiiHandle);
  345. ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DP_RAW_DASHES), mDpHiiHandle);
  346. }
  347. LogEntryKey = 0;
  348. Count = 0;
  349. Index = 0;
  350. while ( WITHIN_LIMIT (Count, Limit) &&
  351. ((LogEntryKey = GetPerformanceMeasurementRecord (
  352. LogEntryKey,
  353. &Measurement.Handle,
  354. &Measurement.Token,
  355. &Measurement.Module,
  356. &Measurement.StartTimeStamp,
  357. &Measurement.EndTimeStamp,
  358. &Measurement.Identifier
  359. )) != 0)
  360. )
  361. {
  362. ++Index; // Count every record. First record is 1.
  363. ElapsedTime = 0;
  364. if (Measurement.EndTimeStamp != 0) {
  365. Duration = GetDuration (&Measurement);
  366. ElapsedTime = DurationInMicroSeconds (Duration);
  367. }
  368. if ((ElapsedTime < mInterestThreshold) ||
  369. ((ExcludeFlag) && (GetCumulativeItem (&Measurement) >= 0))
  370. ) // Ignore "uninteresting" or Excluded records
  371. {
  372. continue;
  373. }
  374. ++Count; // Count the number of records printed
  375. if (mShowId) {
  376. ShellPrintHiiEx (
  377. -1,
  378. -1,
  379. NULL,
  380. STRING_TOKEN (STR_DP_RAW_VARS2),
  381. mDpHiiHandle,
  382. Index, // 1 based, Which measurement record is being printed
  383. Measurement.Handle,
  384. Measurement.StartTimeStamp,
  385. Measurement.EndTimeStamp,
  386. Measurement.Token,
  387. Measurement.Module,
  388. Measurement.Identifier
  389. );
  390. } else {
  391. ShellPrintHiiEx (
  392. -1,
  393. -1,
  394. NULL,
  395. STRING_TOKEN (STR_DP_RAW_VARS),
  396. mDpHiiHandle,
  397. Index, // 1 based, Which measurement record is being printed
  398. Measurement.Handle,
  399. Measurement.StartTimeStamp,
  400. Measurement.EndTimeStamp,
  401. Measurement.Token,
  402. Measurement.Module
  403. );
  404. }
  405. if (ShellGetExecutionBreakFlag ()) {
  406. Status = EFI_ABORTED;
  407. break;
  408. }
  409. }
  410. return Status;
  411. }
  412. /**
  413. Gather and print Major Phase metrics.
  414. **/
  415. VOID
  416. ProcessPhases (
  417. VOID
  418. )
  419. {
  420. MEASUREMENT_RECORD Measurement;
  421. UINT64 BdsTimeoutValue;
  422. UINT64 SecTime;
  423. UINT64 PeiTime;
  424. UINT64 DxeTime;
  425. UINT64 BdsTime;
  426. UINT64 ElapsedTime;
  427. UINT64 Duration;
  428. UINT64 Total;
  429. EFI_STRING StringPtr;
  430. UINTN LogEntryKey;
  431. EFI_STRING StringPtrUnknown;
  432. BdsTimeoutValue = 0;
  433. SecTime = 0;
  434. PeiTime = 0;
  435. DxeTime = 0;
  436. BdsTime = 0;
  437. //
  438. // Get Execution Phase Statistics
  439. //
  440. StringPtrUnknown = HiiGetString (mDpHiiHandle, STRING_TOKEN (STR_ALIT_UNKNOWN), NULL);
  441. StringPtr = HiiGetString (mDpHiiHandle, STRING_TOKEN (STR_DP_SECTION_PHASES), NULL);
  442. ShellPrintHiiEx (
  443. -1,
  444. -1,
  445. NULL,
  446. STRING_TOKEN (STR_DP_SECTION_HEADER),
  447. mDpHiiHandle,
  448. (StringPtr == NULL) ? StringPtrUnknown : StringPtr
  449. );
  450. FreePool (StringPtr);
  451. FreePool (StringPtrUnknown);
  452. LogEntryKey = 0;
  453. while ((LogEntryKey = GetPerformanceMeasurementRecord (
  454. LogEntryKey,
  455. &Measurement.Handle,
  456. &Measurement.Token,
  457. &Measurement.Module,
  458. &Measurement.StartTimeStamp,
  459. &Measurement.EndTimeStamp,
  460. &Measurement.Identifier
  461. )) != 0)
  462. {
  463. if (Measurement.EndTimeStamp == 0) {
  464. // Skip "incomplete" records
  465. continue;
  466. }
  467. Duration = GetDuration (&Measurement);
  468. if ( (Measurement.Handle != NULL)
  469. && (AsciiStrCmp (Measurement.Token, ALit_BdsTO) == 0)
  470. )
  471. {
  472. BdsTimeoutValue = Duration;
  473. } else if (AsciiStrCmp (Measurement.Token, ALit_SEC) == 0) {
  474. SecTime = Duration;
  475. } else if (AsciiStrCmp (Measurement.Token, ALit_PEI) == 0) {
  476. PeiTime = Duration;
  477. } else if (AsciiStrCmp (Measurement.Token, ALit_DXE) == 0) {
  478. DxeTime = Duration;
  479. } else if (AsciiStrCmp (Measurement.Token, ALit_BDS) == 0) {
  480. BdsTime = Duration;
  481. }
  482. }
  483. Total = 0;
  484. // print Reset End if it's valid
  485. //
  486. if (SecTime > mResetEnd) {
  487. SecTime = SecTime - mResetEnd; // Calculate sec time duration start from the beginning of firmware image execution
  488. ElapsedTime = DurationInMicroSeconds (mResetEnd); // Calculate elapsed time in microseconds
  489. Total += DivU64x32 (ElapsedTime, 1000); // Accumulate time in milliseconds
  490. ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DP_RESET_END), mDpHiiHandle, ElapsedTime);
  491. }
  492. // print SEC phase duration time
  493. //
  494. if (SecTime > 0) {
  495. ElapsedTime = DurationInMicroSeconds (SecTime); // Calculate elapsed time in microseconds
  496. Total += DivU64x32 (ElapsedTime, 1000); // Accumulate time in milliseconds
  497. ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DP_SEC_PHASE), mDpHiiHandle, ElapsedTime);
  498. }
  499. // print PEI phase duration time
  500. //
  501. if (PeiTime > 0) {
  502. ElapsedTime = DivU64x32 (PeiTime, 1000000);
  503. Total += ElapsedTime;
  504. ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DP_PHASE_DURATION), mDpHiiHandle, ALit_PEI, ElapsedTime);
  505. }
  506. // print DXE phase duration time
  507. //
  508. if (DxeTime > 0) {
  509. ElapsedTime = DivU64x32 (DxeTime, 1000000);
  510. Total += ElapsedTime;
  511. ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DP_PHASE_DURATION), mDpHiiHandle, ALit_DXE, ElapsedTime);
  512. }
  513. // print BDS phase duration time
  514. //
  515. if (BdsTime > 0) {
  516. ElapsedTime = DivU64x32 (BdsTime, 1000000);
  517. Total += ElapsedTime;
  518. ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DP_PHASE_DURATION), mDpHiiHandle, ALit_BDS, ElapsedTime);
  519. }
  520. if (BdsTimeoutValue > 0) {
  521. ElapsedTime = DivU64x32 (BdsTimeoutValue, 1000000);
  522. ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DP_PHASE_BDSTO), mDpHiiHandle, ALit_BdsTO, ElapsedTime);
  523. }
  524. ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DP_TOTAL_DURATION), mDpHiiHandle, Total);
  525. }
  526. /**
  527. Gather and print Handle data.
  528. @param[in] ExcludeFlag TRUE to exclude individual Cumulative items from display.
  529. @retval EFI_SUCCESS The operation was successful.
  530. @retval EFI_ABORTED The user aborts the operation.
  531. @return Others from a call to gBS->LocateHandleBuffer().
  532. **/
  533. EFI_STATUS
  534. ProcessHandles (
  535. IN BOOLEAN ExcludeFlag
  536. )
  537. {
  538. MEASUREMENT_RECORD Measurement;
  539. UINT64 ElapsedTime;
  540. UINT64 Duration;
  541. EFI_HANDLE *HandleBuffer;
  542. EFI_STRING StringPtr;
  543. UINTN Index;
  544. UINTN LogEntryKey;
  545. UINTN Count;
  546. UINTN HandleCount;
  547. EFI_STATUS Status;
  548. EFI_STRING StringPtrUnknown;
  549. StringPtrUnknown = HiiGetString (mDpHiiHandle, STRING_TOKEN (STR_ALIT_UNKNOWN), NULL);
  550. StringPtr = HiiGetString (mDpHiiHandle, STRING_TOKEN (STR_DP_SECTION_DRIVERS), NULL);
  551. ShellPrintHiiEx (
  552. -1,
  553. -1,
  554. NULL,
  555. STRING_TOKEN (STR_DP_SECTION_HEADER),
  556. mDpHiiHandle,
  557. (StringPtr == NULL) ? StringPtrUnknown : StringPtr
  558. );
  559. FreePool (StringPtr);
  560. FreePool (StringPtrUnknown);
  561. Status = gBS->LocateHandleBuffer (AllHandles, NULL, NULL, &HandleCount, &HandleBuffer);
  562. if (EFI_ERROR (Status)) {
  563. ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DP_HANDLES_ERROR), mDpHiiHandle, Status);
  564. } else {
  565. #if DP_DEBUG == 2
  566. Print (L"There are %,d Handles defined.\n", (Size / sizeof (HandleBuffer[0])));
  567. #endif
  568. if (mShowId) {
  569. ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DP_HANDLE_SECTION2), mDpHiiHandle);
  570. } else {
  571. ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DP_HANDLE_SECTION), mDpHiiHandle);
  572. }
  573. ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DP_DASHES), mDpHiiHandle);
  574. LogEntryKey = 0;
  575. Count = 0;
  576. while ((LogEntryKey = GetPerformanceMeasurementRecord (
  577. LogEntryKey,
  578. &Measurement.Handle,
  579. &Measurement.Token,
  580. &Measurement.Module,
  581. &Measurement.StartTimeStamp,
  582. &Measurement.EndTimeStamp,
  583. &Measurement.Identifier
  584. )) != 0)
  585. {
  586. Count++;
  587. Duration = GetDuration (&Measurement);
  588. ElapsedTime = DurationInMicroSeconds (Duration);
  589. if ((ElapsedTime < mInterestThreshold) ||
  590. (Measurement.EndTimeStamp == 0) ||
  591. (!IsCorePerf (&Measurement)) ||
  592. ((ExcludeFlag) && (GetCumulativeItem (&Measurement) >= 0))
  593. ) // Ignore "uninteresting" or excluded records
  594. {
  595. continue;
  596. }
  597. mGaugeString[0] = 0; // Empty driver name by default
  598. AsciiStrToUnicodeStrS (Measurement.Token, mUnicodeToken, ARRAY_SIZE (mUnicodeToken));
  599. // See if the Handle is in the HandleBuffer
  600. for (Index = 0; Index < HandleCount; Index++) {
  601. if (Measurement.Handle == HandleBuffer[Index]) {
  602. DpGetNameFromHandle (HandleBuffer[Index]); // Name is put into mGaugeString
  603. break;
  604. }
  605. }
  606. // Ensure that the argument strings are not too long.
  607. mGaugeString[DP_GAUGE_STRING_LENGTH] = 0;
  608. mUnicodeToken[11] = 0;
  609. if (mGaugeString[0] != 0) {
  610. // Display the record if it has a valid handle.
  611. if (mShowId) {
  612. ShellPrintHiiEx (
  613. -1,
  614. -1,
  615. NULL,
  616. STRING_TOKEN (STR_DP_HANDLE_VARS2),
  617. mDpHiiHandle,
  618. Count, // 1 based, Which measurement record is being printed
  619. Index + 1, // 1 based, Which handle is being printed
  620. mGaugeString,
  621. mUnicodeToken,
  622. ElapsedTime,
  623. Measurement.Identifier
  624. );
  625. } else {
  626. ShellPrintHiiEx (
  627. -1,
  628. -1,
  629. NULL,
  630. STRING_TOKEN (STR_DP_HANDLE_VARS),
  631. mDpHiiHandle,
  632. Count, // 1 based, Which measurement record is being printed
  633. Index + 1, // 1 based, Which handle is being printed
  634. mGaugeString,
  635. mUnicodeToken,
  636. ElapsedTime
  637. );
  638. }
  639. }
  640. if (ShellGetExecutionBreakFlag ()) {
  641. Status = EFI_ABORTED;
  642. break;
  643. }
  644. }
  645. }
  646. if (HandleBuffer != NULL) {
  647. FreePool (HandleBuffer);
  648. }
  649. return Status;
  650. }
  651. /**
  652. Gather and print PEIM data.
  653. Only prints complete PEIM records
  654. @retval EFI_SUCCESS The operation was successful.
  655. @retval EFI_ABORTED The user aborts the operation.
  656. **/
  657. EFI_STATUS
  658. ProcessPeims (
  659. VOID
  660. )
  661. {
  662. MEASUREMENT_RECORD Measurement;
  663. UINT64 Duration;
  664. UINT64 ElapsedTime;
  665. EFI_STRING StringPtr;
  666. UINTN LogEntryKey;
  667. UINTN TIndex;
  668. EFI_STRING StringPtrUnknown;
  669. EFI_STATUS Status;
  670. Status = EFI_SUCCESS;
  671. StringPtrUnknown = HiiGetString (mDpHiiHandle, STRING_TOKEN (STR_ALIT_UNKNOWN), NULL);
  672. StringPtr = HiiGetString (mDpHiiHandle, STRING_TOKEN (STR_DP_SECTION_PEIMS), NULL);
  673. ShellPrintHiiEx (
  674. -1,
  675. -1,
  676. NULL,
  677. STRING_TOKEN (STR_DP_SECTION_HEADER),
  678. mDpHiiHandle,
  679. (StringPtr == NULL) ? StringPtrUnknown : StringPtr
  680. );
  681. FreePool (StringPtr);
  682. FreePool (StringPtrUnknown);
  683. if (mShowId) {
  684. ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DP_PEIM_SECTION2), mDpHiiHandle);
  685. } else {
  686. ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DP_PEIM_SECTION), mDpHiiHandle);
  687. }
  688. ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DP_DASHES), mDpHiiHandle);
  689. TIndex = 0;
  690. LogEntryKey = 0;
  691. while ((LogEntryKey = GetPerformanceMeasurementRecord (
  692. LogEntryKey,
  693. &Measurement.Handle,
  694. &Measurement.Token,
  695. &Measurement.Module,
  696. &Measurement.StartTimeStamp,
  697. &Measurement.EndTimeStamp,
  698. &Measurement.Identifier
  699. )) != 0)
  700. {
  701. TIndex++;
  702. if ((Measurement.EndTimeStamp == 0) ||
  703. (AsciiStrCmp (Measurement.Token, ALit_PEIM) != 0)
  704. )
  705. {
  706. continue;
  707. }
  708. Duration = GetDuration (&Measurement);
  709. ElapsedTime = DurationInMicroSeconds (Duration); // Calculate elapsed time in microseconds
  710. if (ElapsedTime >= mInterestThreshold) {
  711. // PEIM FILE Handle is the start address of its FFS file that contains its file guid.
  712. if (mShowId) {
  713. ShellPrintHiiEx (
  714. -1,
  715. -1,
  716. NULL,
  717. STRING_TOKEN (STR_DP_PEIM_VARS2),
  718. mDpHiiHandle,
  719. TIndex, // 1 based, Which measurement record is being printed
  720. Measurement.Handle, // file guid
  721. ElapsedTime,
  722. Measurement.Identifier
  723. );
  724. } else {
  725. ShellPrintHiiEx (
  726. -1,
  727. -1,
  728. NULL,
  729. STRING_TOKEN (STR_DP_PEIM_VARS),
  730. mDpHiiHandle,
  731. TIndex, // 1 based, Which measurement record is being printed
  732. Measurement.Handle, // file guid
  733. ElapsedTime
  734. );
  735. }
  736. }
  737. if (ShellGetExecutionBreakFlag ()) {
  738. Status = EFI_ABORTED;
  739. break;
  740. }
  741. }
  742. return Status;
  743. }
  744. /**
  745. Gather and print global data.
  746. Strips out incomplete or "Execution Phase" records
  747. Only prints records where Handle is NULL
  748. Increment TIndex for every record, even skipped ones, so that we have an
  749. indication of every measurement record taken.
  750. @retval EFI_SUCCESS The operation was successful.
  751. @retval EFI_ABORTED The user aborts the operation.
  752. **/
  753. EFI_STATUS
  754. ProcessGlobal (
  755. VOID
  756. )
  757. {
  758. MEASUREMENT_RECORD Measurement;
  759. UINT64 Duration;
  760. UINT64 ElapsedTime;
  761. EFI_STRING StringPtr;
  762. UINTN LogEntryKey;
  763. UINTN Index; // Index, or number, of the measurement record being processed
  764. EFI_STRING StringPtrUnknown;
  765. EFI_STATUS Status;
  766. Status = EFI_SUCCESS;
  767. StringPtrUnknown = HiiGetString (mDpHiiHandle, STRING_TOKEN (STR_ALIT_UNKNOWN), NULL);
  768. StringPtr = HiiGetString (mDpHiiHandle, STRING_TOKEN (STR_DP_SECTION_GENERAL), NULL);
  769. ShellPrintHiiEx (
  770. -1,
  771. -1,
  772. NULL,
  773. STRING_TOKEN (STR_DP_SECTION_HEADER),
  774. mDpHiiHandle,
  775. (StringPtr == NULL) ? StringPtrUnknown : StringPtr
  776. );
  777. FreePool (StringPtr);
  778. FreePool (StringPtrUnknown);
  779. if (mShowId) {
  780. ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DP_GLOBAL_SECTION2), mDpHiiHandle);
  781. } else {
  782. ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DP_GLOBAL_SECTION), mDpHiiHandle);
  783. }
  784. ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DP_DASHES), mDpHiiHandle);
  785. Index = 1;
  786. LogEntryKey = 0;
  787. while ((LogEntryKey = GetPerformanceMeasurementRecord (
  788. LogEntryKey,
  789. &Measurement.Handle,
  790. &Measurement.Token,
  791. &Measurement.Module,
  792. &Measurement.StartTimeStamp,
  793. &Measurement.EndTimeStamp,
  794. &Measurement.Identifier
  795. )) != 0)
  796. {
  797. AsciiStrToUnicodeStrS (Measurement.Module, mGaugeString, ARRAY_SIZE (mGaugeString));
  798. AsciiStrToUnicodeStrS (Measurement.Token, mUnicodeToken, ARRAY_SIZE (mUnicodeToken));
  799. mGaugeString[25] = 0;
  800. mUnicodeToken[31] = 0;
  801. if ( !(IsPhase (&Measurement) ||
  802. IsCorePerf (&Measurement) ||
  803. (Measurement.EndTimeStamp == 0)
  804. ))
  805. {
  806. Duration = GetDuration (&Measurement);
  807. ElapsedTime = DurationInMicroSeconds (Duration);
  808. if (ElapsedTime >= mInterestThreshold) {
  809. if (mShowId) {
  810. ShellPrintHiiEx (
  811. -1,
  812. -1,
  813. NULL,
  814. STRING_TOKEN (STR_DP_GLOBAL_VARS2),
  815. mDpHiiHandle,
  816. Index,
  817. mGaugeString,
  818. mUnicodeToken,
  819. ElapsedTime,
  820. Measurement.Identifier
  821. );
  822. } else {
  823. ShellPrintHiiEx (
  824. -1,
  825. -1,
  826. NULL,
  827. STRING_TOKEN (STR_DP_GLOBAL_VARS),
  828. mDpHiiHandle,
  829. Index,
  830. mGaugeString,
  831. mUnicodeToken,
  832. ElapsedTime
  833. );
  834. }
  835. }
  836. }
  837. if (ShellGetExecutionBreakFlag ()) {
  838. Status = EFI_ABORTED;
  839. break;
  840. }
  841. Index++;
  842. }
  843. return Status;
  844. }
  845. /**
  846. Gather and print cumulative data.
  847. Traverse the measurement records and:<BR>
  848. For each record with a Token listed in the CumData array:<BR>
  849. - Update the instance count and the total, minimum, and maximum durations.
  850. Finally, print the gathered cumulative statistics.
  851. @param[in] CustomCumulativeData A pointer to the custom cumulative data.
  852. **/
  853. VOID
  854. ProcessCumulative (
  855. IN PERF_CUM_DATA *CustomCumulativeData OPTIONAL
  856. )
  857. {
  858. UINT64 AvgDur; // the computed average duration
  859. UINT64 Dur;
  860. UINT64 MinDur;
  861. UINT64 MaxDur;
  862. EFI_STRING StringPtr;
  863. UINTN TIndex;
  864. EFI_STRING StringPtrUnknown;
  865. StringPtrUnknown = HiiGetString (mDpHiiHandle, STRING_TOKEN (STR_ALIT_UNKNOWN), NULL);
  866. StringPtr = HiiGetString (mDpHiiHandle, STRING_TOKEN (STR_DP_SECTION_CUMULATIVE), NULL);
  867. ShellPrintHiiEx (
  868. -1,
  869. -1,
  870. NULL,
  871. STRING_TOKEN (STR_DP_SECTION_HEADER),
  872. mDpHiiHandle,
  873. (StringPtr == NULL) ? StringPtrUnknown : StringPtr
  874. );
  875. FreePool (StringPtr);
  876. FreePool (StringPtrUnknown);
  877. ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DP_CUMULATIVE_SECT_1), mDpHiiHandle);
  878. ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DP_CUMULATIVE_SECT_2), mDpHiiHandle);
  879. ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DP_DASHES), mDpHiiHandle);
  880. for ( TIndex = 0; TIndex < NumCum; ++TIndex) {
  881. if (CumData[TIndex].Count != 0) {
  882. AvgDur = DivU64x32 (CumData[TIndex].Duration, CumData[TIndex].Count);
  883. AvgDur = DurationInMicroSeconds (AvgDur);
  884. Dur = DurationInMicroSeconds (CumData[TIndex].Duration);
  885. MaxDur = DurationInMicroSeconds (CumData[TIndex].MaxDur);
  886. MinDur = DurationInMicroSeconds (CumData[TIndex].MinDur);
  887. ShellPrintHiiEx (
  888. -1,
  889. -1,
  890. NULL,
  891. STRING_TOKEN (STR_DP_CUMULATIVE_STATS),
  892. mDpHiiHandle,
  893. CumData[TIndex].Name,
  894. CumData[TIndex].Count,
  895. Dur,
  896. AvgDur,
  897. MinDur,
  898. MaxDur
  899. );
  900. }
  901. }
  902. //
  903. // Print the custom cumulative data.
  904. //
  905. if (CustomCumulativeData != NULL) {
  906. if (CustomCumulativeData->Count != 0) {
  907. AvgDur = DivU64x32 (CustomCumulativeData->Duration, CustomCumulativeData->Count);
  908. AvgDur = DurationInMicroSeconds (AvgDur);
  909. Dur = DurationInMicroSeconds (CustomCumulativeData->Duration);
  910. MaxDur = DurationInMicroSeconds (CustomCumulativeData->MaxDur);
  911. MinDur = DurationInMicroSeconds (CustomCumulativeData->MinDur);
  912. } else {
  913. AvgDur = 0;
  914. Dur = 0;
  915. MaxDur = 0;
  916. MinDur = 0;
  917. }
  918. ShellPrintHiiEx (
  919. -1,
  920. -1,
  921. NULL,
  922. STRING_TOKEN (STR_DP_CUMULATIVE_STATS),
  923. mDpHiiHandle,
  924. CustomCumulativeData->Name,
  925. CustomCumulativeData->Count,
  926. Dur,
  927. AvgDur,
  928. MinDur,
  929. MaxDur
  930. );
  931. }
  932. }