DebugCounter.cpp 5.2 KB

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