DebugCounter.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #include "llvm/Support/DebugCounter.h"
  2. #include "DebugOptions.h"
  3. #include "llvm/Support/CommandLine.h"
  4. #include "llvm/Support/Format.h"
  5. using namespace llvm;
  6. namespace {
  7. // This class overrides the default list implementation of printing so we
  8. // can pretty print the list of debug counter options. This type of
  9. // dynamic option is pretty rare (basically this and pass lists).
  10. class DebugCounterList : public cl::list<std::string, DebugCounter> {
  11. private:
  12. using Base = cl::list<std::string, DebugCounter>;
  13. public:
  14. template <class... Mods>
  15. explicit DebugCounterList(Mods &&... Ms) : Base(std::forward<Mods>(Ms)...) {}
  16. private:
  17. void printOptionInfo(size_t GlobalWidth) const override {
  18. // This is a variant of from generic_parser_base::printOptionInfo. Sadly,
  19. // it's not easy to make it more usable. We could get it to print these as
  20. // options if we were a cl::opt and registered them, but lists don't have
  21. // options, nor does the parser for std::string. The other mechanisms for
  22. // options are global and would pollute the global namespace with our
  23. // counters. Rather than go that route, we have just overridden the
  24. // printing, which only a few things call anyway.
  25. outs() << " -" << ArgStr;
  26. // All of the other options in CommandLine.cpp use ArgStr.size() + 6 for
  27. // width, so we do the same.
  28. Option::printHelpStr(HelpStr, GlobalWidth, ArgStr.size() + 6);
  29. const auto &CounterInstance = DebugCounter::instance();
  30. for (const auto &Name : CounterInstance) {
  31. const auto Info =
  32. CounterInstance.getCounterInfo(CounterInstance.getCounterId(Name));
  33. size_t NumSpaces = GlobalWidth - Info.first.size() - 8;
  34. outs() << " =" << Info.first;
  35. outs().indent(NumSpaces) << " - " << Info.second << '\n';
  36. }
  37. }
  38. };
  39. // All global objects associated to the DebugCounter, including the DebugCounter
  40. // itself, are owned by a single global instance of the DebugCounterOwner
  41. // struct. This makes it easier to control the order in which constructors and
  42. // destructors are run.
  43. struct DebugCounterOwner {
  44. DebugCounter DC;
  45. DebugCounterList DebugCounterOption{
  46. "debug-counter", cl::Hidden,
  47. cl::desc("Comma separated list of debug counter skip and count"),
  48. cl::CommaSeparated, cl::location(DC)};
  49. cl::opt<bool> PrintDebugCounter{
  50. "print-debug-counter", cl::Hidden, cl::init(false), cl::Optional,
  51. cl::desc("Print out debug counter info after all counters accumulated")};
  52. DebugCounterOwner() {
  53. // Our destructor uses the debug stream. By referencing it here, we
  54. // ensure that its destructor runs after our destructor.
  55. (void)dbgs();
  56. }
  57. // Print information when destroyed, iff command line option is specified.
  58. ~DebugCounterOwner() {
  59. if (DC.isCountingEnabled() && PrintDebugCounter)
  60. DC.print(dbgs());
  61. }
  62. };
  63. } // anonymous namespace
  64. void llvm::initDebugCounterOptions() { (void)DebugCounter::instance(); }
  65. DebugCounter &DebugCounter::instance() {
  66. static DebugCounterOwner O;
  67. return O.DC;
  68. }
  69. // This is called by the command line parser when it sees a value for the
  70. // debug-counter option defined above.
  71. void DebugCounter::push_back(const std::string &Val) {
  72. if (Val.empty())
  73. return;
  74. // The strings should come in as counter=value
  75. auto CounterPair = StringRef(Val).split('=');
  76. if (CounterPair.second.empty()) {
  77. errs() << "DebugCounter Error: " << Val << " does not have an = in it\n";
  78. return;
  79. }
  80. // Now we have counter=value.
  81. // First, process value.
  82. int64_t CounterVal;
  83. if (CounterPair.second.getAsInteger(0, CounterVal)) {
  84. errs() << "DebugCounter Error: " << CounterPair.second
  85. << " is not a number\n";
  86. return;
  87. }
  88. // Now we need to see if this is the skip or the count, remove the suffix, and
  89. // add it to the counter values.
  90. if (CounterPair.first.endswith("-skip")) {
  91. auto CounterName = CounterPair.first.drop_back(5);
  92. unsigned CounterID = getCounterId(std::string(CounterName));
  93. if (!CounterID) {
  94. errs() << "DebugCounter Error: " << CounterName
  95. << " is not a registered counter\n";
  96. return;
  97. }
  98. enableAllCounters();
  99. CounterInfo &Counter = Counters[CounterID];
  100. Counter.Skip = CounterVal;
  101. Counter.IsSet = true;
  102. } else if (CounterPair.first.endswith("-count")) {
  103. auto CounterName = CounterPair.first.drop_back(6);
  104. unsigned CounterID = getCounterId(std::string(CounterName));
  105. if (!CounterID) {
  106. errs() << "DebugCounter Error: " << CounterName
  107. << " is not a registered counter\n";
  108. return;
  109. }
  110. enableAllCounters();
  111. CounterInfo &Counter = Counters[CounterID];
  112. Counter.StopAfter = CounterVal;
  113. Counter.IsSet = true;
  114. } else {
  115. errs() << "DebugCounter Error: " << CounterPair.first
  116. << " does not end with -skip or -count\n";
  117. }
  118. }
  119. void DebugCounter::print(raw_ostream &OS) const {
  120. SmallVector<StringRef, 16> CounterNames(RegisteredCounters.begin(),
  121. RegisteredCounters.end());
  122. sort(CounterNames);
  123. auto &Us = instance();
  124. OS << "Counters and values:\n";
  125. for (auto &CounterName : CounterNames) {
  126. unsigned CounterID = getCounterId(std::string(CounterName));
  127. OS << left_justify(RegisteredCounters[CounterID], 32) << ": {"
  128. << Us.Counters[CounterID].Count << "," << Us.Counters[CounterID].Skip
  129. << "," << Us.Counters[CounterID].StopAfter << "}\n";
  130. }
  131. }
  132. LLVM_DUMP_METHOD void DebugCounter::dump() const {
  133. print(dbgs());
  134. }