RetireControlUnitStatistics.cpp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. //===--------------------- RetireControlUnitStatistics.cpp ------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. /// \file
  9. ///
  10. /// This file implements the RetireControlUnitStatistics interface.
  11. ///
  12. //===----------------------------------------------------------------------===//
  13. #include "Views/RetireControlUnitStatistics.h"
  14. #include "llvm/Support/Format.h"
  15. namespace llvm {
  16. namespace mca {
  17. RetireControlUnitStatistics::RetireControlUnitStatistics(const MCSchedModel &SM)
  18. : NumRetired(0), NumCycles(0), EntriesInUse(0), MaxUsedEntries(0),
  19. SumOfUsedEntries(0) {
  20. TotalROBEntries = SM.MicroOpBufferSize;
  21. if (SM.hasExtraProcessorInfo()) {
  22. const MCExtraProcessorInfo &EPI = SM.getExtraProcessorInfo();
  23. if (EPI.ReorderBufferSize)
  24. TotalROBEntries = EPI.ReorderBufferSize;
  25. }
  26. }
  27. void RetireControlUnitStatistics::onEvent(const HWInstructionEvent &Event) {
  28. if (Event.Type == HWInstructionEvent::Dispatched) {
  29. unsigned NumEntries =
  30. static_cast<const HWInstructionDispatchedEvent &>(Event).MicroOpcodes;
  31. EntriesInUse += NumEntries;
  32. }
  33. if (Event.Type == HWInstructionEvent::Retired) {
  34. unsigned ReleasedEntries = Event.IR.getInstruction()->getDesc().NumMicroOps;
  35. assert(EntriesInUse >= ReleasedEntries && "Invalid internal state!");
  36. EntriesInUse -= ReleasedEntries;
  37. ++NumRetired;
  38. }
  39. }
  40. void RetireControlUnitStatistics::onCycleEnd() {
  41. // Update histogram
  42. RetiredPerCycle[NumRetired]++;
  43. NumRetired = 0;
  44. ++NumCycles;
  45. MaxUsedEntries = std::max(MaxUsedEntries, EntriesInUse);
  46. SumOfUsedEntries += EntriesInUse;
  47. }
  48. void RetireControlUnitStatistics::printView(raw_ostream &OS) const {
  49. std::string Buffer;
  50. raw_string_ostream TempStream(Buffer);
  51. TempStream << "\n\nRetire Control Unit - "
  52. << "number of cycles where we saw N instructions retired:\n";
  53. TempStream << "[# retired], [# cycles]\n";
  54. for (const std::pair<const unsigned, unsigned> &Entry : RetiredPerCycle) {
  55. TempStream << " " << Entry.first;
  56. if (Entry.first < 10)
  57. TempStream << ", ";
  58. else
  59. TempStream << ", ";
  60. TempStream << Entry.second << " ("
  61. << format("%.1f", ((double)Entry.second / NumCycles) * 100.0)
  62. << "%)\n";
  63. }
  64. unsigned AvgUsage = (double)SumOfUsedEntries / NumCycles;
  65. double MaxUsagePercentage =
  66. ((double)MaxUsedEntries / TotalROBEntries) * 100.0;
  67. double NormalizedMaxPercentage = floor((MaxUsagePercentage * 10) + 0.5) / 10;
  68. double AvgUsagePercentage = ((double)AvgUsage / TotalROBEntries) * 100.0;
  69. double NormalizedAvgPercentage = floor((AvgUsagePercentage * 10) + 0.5) / 10;
  70. TempStream << "\nTotal ROB Entries: " << TotalROBEntries
  71. << "\nMax Used ROB Entries: " << MaxUsedEntries
  72. << format(" ( %.1f%% )", NormalizedMaxPercentage)
  73. << "\nAverage Used ROB Entries per cy: " << AvgUsage
  74. << format(" ( %.1f%% )\n", NormalizedAvgPercentage);
  75. TempStream.flush();
  76. OS << Buffer;
  77. }
  78. } // namespace mca
  79. } // namespace llvm