10b57cec5SDimitry Andric #include "llvm/Support/DebugCounter.h"
2fe6060f1SDimitry Andric 
3fe6060f1SDimitry Andric #include "DebugOptions.h"
4fe6060f1SDimitry Andric 
50b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h"
60b57cec5SDimitry Andric #include "llvm/Support/Format.h"
70b57cec5SDimitry Andric 
80b57cec5SDimitry Andric using namespace llvm;
90b57cec5SDimitry Andric 
100b57cec5SDimitry Andric namespace {
110b57cec5SDimitry Andric // This class overrides the default list implementation of printing so we
120b57cec5SDimitry Andric // can pretty print the list of debug counter options.  This type of
130b57cec5SDimitry Andric // dynamic option is pretty rare (basically this and pass lists).
140b57cec5SDimitry Andric class DebugCounterList : public cl::list<std::string, DebugCounter> {
150b57cec5SDimitry Andric private:
160b57cec5SDimitry Andric   using Base = cl::list<std::string, DebugCounter>;
170b57cec5SDimitry Andric 
180b57cec5SDimitry Andric public:
190b57cec5SDimitry Andric   template <class... Mods>
DebugCounterList(Mods &&...Ms)200b57cec5SDimitry Andric   explicit DebugCounterList(Mods &&... Ms) : Base(std::forward<Mods>(Ms)...) {}
210b57cec5SDimitry Andric 
220b57cec5SDimitry Andric private:
printOptionInfo(size_t GlobalWidth) const230b57cec5SDimitry Andric   void printOptionInfo(size_t GlobalWidth) const override {
240b57cec5SDimitry Andric     // This is a variant of from generic_parser_base::printOptionInfo.  Sadly,
250b57cec5SDimitry Andric     // it's not easy to make it more usable.  We could get it to print these as
260b57cec5SDimitry Andric     // options if we were a cl::opt and registered them, but lists don't have
270b57cec5SDimitry Andric     // options, nor does the parser for std::string.  The other mechanisms for
280b57cec5SDimitry Andric     // options are global and would pollute the global namespace with our
290b57cec5SDimitry Andric     // counters.  Rather than go that route, we have just overridden the
300b57cec5SDimitry Andric     // printing, which only a few things call anyway.
310b57cec5SDimitry Andric     outs() << "  -" << ArgStr;
320b57cec5SDimitry Andric     // All of the other options in CommandLine.cpp use ArgStr.size() + 6 for
330b57cec5SDimitry Andric     // width, so we do the same.
340b57cec5SDimitry Andric     Option::printHelpStr(HelpStr, GlobalWidth, ArgStr.size() + 6);
350b57cec5SDimitry Andric     const auto &CounterInstance = DebugCounter::instance();
365ffd83dbSDimitry Andric     for (const auto &Name : CounterInstance) {
370b57cec5SDimitry Andric       const auto Info =
380b57cec5SDimitry Andric           CounterInstance.getCounterInfo(CounterInstance.getCounterId(Name));
390b57cec5SDimitry Andric       size_t NumSpaces = GlobalWidth - Info.first.size() - 8;
400b57cec5SDimitry Andric       outs() << "    =" << Info.first;
410b57cec5SDimitry Andric       outs().indent(NumSpaces) << " -   " << Info.second << '\n';
420b57cec5SDimitry Andric     }
430b57cec5SDimitry Andric   }
440b57cec5SDimitry Andric };
450b57cec5SDimitry Andric 
46bdd1243dSDimitry Andric // All global objects associated to the DebugCounter, including the DebugCounter
47bdd1243dSDimitry Andric // itself, are owned by a single global instance of the DebugCounterOwner
48bdd1243dSDimitry Andric // struct. This makes it easier to control the order in which constructors and
49bdd1243dSDimitry Andric // destructors are run.
50bdd1243dSDimitry Andric struct DebugCounterOwner {
51bdd1243dSDimitry Andric   DebugCounter DC;
52bdd1243dSDimitry Andric   DebugCounterList DebugCounterOption{
530b57cec5SDimitry Andric       "debug-counter", cl::Hidden,
540b57cec5SDimitry Andric       cl::desc("Comma separated list of debug counter skip and count"),
55bdd1243dSDimitry Andric       cl::CommaSeparated, cl::location(DC)};
56bdd1243dSDimitry Andric   cl::opt<bool> PrintDebugCounter{
57bdd1243dSDimitry Andric       "print-debug-counter", cl::Hidden, cl::init(false), cl::Optional,
58bdd1243dSDimitry Andric       cl::desc("Print out debug counter info after all counters accumulated")};
59bdd1243dSDimitry Andric 
DebugCounterOwner__anon935b73b90111::DebugCounterOwner60bdd1243dSDimitry Andric   DebugCounterOwner() {
61bdd1243dSDimitry Andric     // Our destructor uses the debug stream. By referencing it here, we
62bdd1243dSDimitry Andric     // ensure that its destructor runs after our destructor.
63bdd1243dSDimitry Andric     (void)dbgs();
64fe6060f1SDimitry Andric   }
650b57cec5SDimitry Andric 
660b57cec5SDimitry Andric   // Print information when destroyed, iff command line option is specified.
~DebugCounterOwner__anon935b73b90111::DebugCounterOwner67bdd1243dSDimitry Andric   ~DebugCounterOwner() {
68bdd1243dSDimitry Andric     if (DC.isCountingEnabled() && PrintDebugCounter)
69bdd1243dSDimitry Andric       DC.print(dbgs());
700b57cec5SDimitry Andric   }
71bdd1243dSDimitry Andric };
720b57cec5SDimitry Andric 
73bdd1243dSDimitry Andric } // anonymous namespace
74bdd1243dSDimitry Andric 
initDebugCounterOptions()75bdd1243dSDimitry Andric void llvm::initDebugCounterOptions() { (void)DebugCounter::instance(); }
76bdd1243dSDimitry Andric 
instance()77bdd1243dSDimitry Andric DebugCounter &DebugCounter::instance() {
78bdd1243dSDimitry Andric   static DebugCounterOwner O;
79bdd1243dSDimitry Andric   return O.DC;
80bdd1243dSDimitry Andric }
810b57cec5SDimitry Andric 
820b57cec5SDimitry Andric // This is called by the command line parser when it sees a value for the
830b57cec5SDimitry Andric // debug-counter option defined above.
push_back(const std::string & Val)840b57cec5SDimitry Andric void DebugCounter::push_back(const std::string &Val) {
850b57cec5SDimitry Andric   if (Val.empty())
860b57cec5SDimitry Andric     return;
870b57cec5SDimitry Andric   // The strings should come in as counter=value
880b57cec5SDimitry Andric   auto CounterPair = StringRef(Val).split('=');
890b57cec5SDimitry Andric   if (CounterPair.second.empty()) {
900b57cec5SDimitry Andric     errs() << "DebugCounter Error: " << Val << " does not have an = in it\n";
910b57cec5SDimitry Andric     return;
920b57cec5SDimitry Andric   }
930b57cec5SDimitry Andric   // Now we have counter=value.
940b57cec5SDimitry Andric   // First, process value.
950b57cec5SDimitry Andric   int64_t CounterVal;
960b57cec5SDimitry Andric   if (CounterPair.second.getAsInteger(0, CounterVal)) {
970b57cec5SDimitry Andric     errs() << "DebugCounter Error: " << CounterPair.second
980b57cec5SDimitry Andric            << " is not a number\n";
990b57cec5SDimitry Andric     return;
1000b57cec5SDimitry Andric   }
1010b57cec5SDimitry Andric   // Now we need to see if this is the skip or the count, remove the suffix, and
1020b57cec5SDimitry Andric   // add it to the counter values.
103*c9157d92SDimitry Andric   if (CounterPair.first.ends_with("-skip")) {
1040b57cec5SDimitry Andric     auto CounterName = CounterPair.first.drop_back(5);
1055ffd83dbSDimitry Andric     unsigned CounterID = getCounterId(std::string(CounterName));
1060b57cec5SDimitry Andric     if (!CounterID) {
1070b57cec5SDimitry Andric       errs() << "DebugCounter Error: " << CounterName
1080b57cec5SDimitry Andric              << " is not a registered counter\n";
1090b57cec5SDimitry Andric       return;
1100b57cec5SDimitry Andric     }
1110b57cec5SDimitry Andric     enableAllCounters();
1120b57cec5SDimitry Andric 
1130b57cec5SDimitry Andric     CounterInfo &Counter = Counters[CounterID];
1140b57cec5SDimitry Andric     Counter.Skip = CounterVal;
1150b57cec5SDimitry Andric     Counter.IsSet = true;
116*c9157d92SDimitry Andric   } else if (CounterPair.first.ends_with("-count")) {
1170b57cec5SDimitry Andric     auto CounterName = CounterPair.first.drop_back(6);
1185ffd83dbSDimitry Andric     unsigned CounterID = getCounterId(std::string(CounterName));
1190b57cec5SDimitry Andric     if (!CounterID) {
1200b57cec5SDimitry Andric       errs() << "DebugCounter Error: " << CounterName
1210b57cec5SDimitry Andric              << " is not a registered counter\n";
1220b57cec5SDimitry Andric       return;
1230b57cec5SDimitry Andric     }
1240b57cec5SDimitry Andric     enableAllCounters();
1250b57cec5SDimitry Andric 
1260b57cec5SDimitry Andric     CounterInfo &Counter = Counters[CounterID];
1270b57cec5SDimitry Andric     Counter.StopAfter = CounterVal;
1280b57cec5SDimitry Andric     Counter.IsSet = true;
1290b57cec5SDimitry Andric   } else {
1300b57cec5SDimitry Andric     errs() << "DebugCounter Error: " << CounterPair.first
1310b57cec5SDimitry Andric            << " does not end with -skip or -count\n";
1320b57cec5SDimitry Andric   }
1330b57cec5SDimitry Andric }
1340b57cec5SDimitry Andric 
print(raw_ostream & OS) const1350b57cec5SDimitry Andric void DebugCounter::print(raw_ostream &OS) const {
1360b57cec5SDimitry Andric   SmallVector<StringRef, 16> CounterNames(RegisteredCounters.begin(),
1370b57cec5SDimitry Andric                                           RegisteredCounters.end());
138e8d8bef9SDimitry Andric   sort(CounterNames);
1390b57cec5SDimitry Andric 
1400b57cec5SDimitry Andric   auto &Us = instance();
1410b57cec5SDimitry Andric   OS << "Counters and values:\n";
1420b57cec5SDimitry Andric   for (auto &CounterName : CounterNames) {
1435ffd83dbSDimitry Andric     unsigned CounterID = getCounterId(std::string(CounterName));
1440b57cec5SDimitry Andric     OS << left_justify(RegisteredCounters[CounterID], 32) << ": {"
1450b57cec5SDimitry Andric        << Us.Counters[CounterID].Count << "," << Us.Counters[CounterID].Skip
1460b57cec5SDimitry Andric        << "," << Us.Counters[CounterID].StopAfter << "}\n";
1470b57cec5SDimitry Andric   }
1480b57cec5SDimitry Andric }
1490b57cec5SDimitry Andric 
dump() const1500b57cec5SDimitry Andric LLVM_DUMP_METHOD void DebugCounter::dump() const {
1510b57cec5SDimitry Andric   print(dbgs());
1520b57cec5SDimitry Andric }
153