10b57cec5SDimitry Andric #include "llvm/Support/DebugCounter.h"
2*5f7ddb14SDimitry Andric
3*5f7ddb14SDimitry Andric #include "DebugOptions.h"
4*5f7ddb14SDimitry Andric
50b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h"
60b57cec5SDimitry Andric #include "llvm/Support/Format.h"
70b57cec5SDimitry Andric #include "llvm/Support/ManagedStatic.h"
80b57cec5SDimitry Andric
90b57cec5SDimitry Andric using namespace llvm;
100b57cec5SDimitry Andric
110b57cec5SDimitry Andric namespace {
120b57cec5SDimitry Andric // This class overrides the default list implementation of printing so we
130b57cec5SDimitry Andric // can pretty print the list of debug counter options. This type of
140b57cec5SDimitry Andric // dynamic option is pretty rare (basically this and pass lists).
150b57cec5SDimitry Andric class DebugCounterList : public cl::list<std::string, DebugCounter> {
160b57cec5SDimitry Andric private:
170b57cec5SDimitry Andric using Base = cl::list<std::string, DebugCounter>;
180b57cec5SDimitry Andric
190b57cec5SDimitry Andric public:
200b57cec5SDimitry Andric template <class... Mods>
DebugCounterList(Mods &&...Ms)210b57cec5SDimitry Andric explicit DebugCounterList(Mods &&... Ms) : Base(std::forward<Mods>(Ms)...) {}
220b57cec5SDimitry Andric
230b57cec5SDimitry Andric private:
printOptionInfo(size_t GlobalWidth) const240b57cec5SDimitry Andric void printOptionInfo(size_t GlobalWidth) const override {
250b57cec5SDimitry Andric // This is a variant of from generic_parser_base::printOptionInfo. Sadly,
260b57cec5SDimitry Andric // it's not easy to make it more usable. We could get it to print these as
270b57cec5SDimitry Andric // options if we were a cl::opt and registered them, but lists don't have
280b57cec5SDimitry Andric // options, nor does the parser for std::string. The other mechanisms for
290b57cec5SDimitry Andric // options are global and would pollute the global namespace with our
300b57cec5SDimitry Andric // counters. Rather than go that route, we have just overridden the
310b57cec5SDimitry Andric // printing, which only a few things call anyway.
320b57cec5SDimitry Andric outs() << " -" << ArgStr;
330b57cec5SDimitry Andric // All of the other options in CommandLine.cpp use ArgStr.size() + 6 for
340b57cec5SDimitry Andric // width, so we do the same.
350b57cec5SDimitry Andric Option::printHelpStr(HelpStr, GlobalWidth, ArgStr.size() + 6);
360b57cec5SDimitry Andric const auto &CounterInstance = DebugCounter::instance();
375ffd83dbSDimitry Andric for (const auto &Name : CounterInstance) {
380b57cec5SDimitry Andric const auto Info =
390b57cec5SDimitry Andric CounterInstance.getCounterInfo(CounterInstance.getCounterId(Name));
400b57cec5SDimitry Andric size_t NumSpaces = GlobalWidth - Info.first.size() - 8;
410b57cec5SDimitry Andric outs() << " =" << Info.first;
420b57cec5SDimitry Andric outs().indent(NumSpaces) << " - " << Info.second << '\n';
430b57cec5SDimitry Andric }
440b57cec5SDimitry Andric }
450b57cec5SDimitry Andric };
460b57cec5SDimitry Andric
47*5f7ddb14SDimitry Andric struct CreateDebugCounterOption {
call__anon1cb7c1b70111::CreateDebugCounterOption48*5f7ddb14SDimitry Andric static void *call() {
49*5f7ddb14SDimitry Andric return new DebugCounterList(
500b57cec5SDimitry Andric "debug-counter", cl::Hidden,
510b57cec5SDimitry Andric cl::desc("Comma separated list of debug counter skip and count"),
52*5f7ddb14SDimitry Andric cl::CommaSeparated, cl::ZeroOrMore,
53*5f7ddb14SDimitry Andric cl::location(DebugCounter::instance()));
54*5f7ddb14SDimitry Andric }
55*5f7ddb14SDimitry Andric };
56*5f7ddb14SDimitry Andric } // namespace
570b57cec5SDimitry Andric
58*5f7ddb14SDimitry Andric static ManagedStatic<DebugCounterList, CreateDebugCounterOption>
59*5f7ddb14SDimitry Andric DebugCounterOption;
60*5f7ddb14SDimitry Andric static bool PrintDebugCounter;
61*5f7ddb14SDimitry Andric
initDebugCounterOptions()62*5f7ddb14SDimitry Andric void llvm::initDebugCounterOptions() {
63*5f7ddb14SDimitry Andric *DebugCounterOption;
64*5f7ddb14SDimitry Andric static cl::opt<bool, true> RegisterPrintDebugCounter(
65*5f7ddb14SDimitry Andric "print-debug-counter", cl::Hidden, cl::location(PrintDebugCounter),
66*5f7ddb14SDimitry Andric cl::init(false), cl::Optional,
670b57cec5SDimitry Andric cl::desc("Print out debug counter info after all counters accumulated"));
68*5f7ddb14SDimitry Andric }
690b57cec5SDimitry Andric
700b57cec5SDimitry Andric static ManagedStatic<DebugCounter> DC;
710b57cec5SDimitry Andric
720b57cec5SDimitry Andric // Print information when destroyed, iff command line option is specified.
~DebugCounter()730b57cec5SDimitry Andric DebugCounter::~DebugCounter() {
740b57cec5SDimitry Andric if (isCountingEnabled() && PrintDebugCounter)
750b57cec5SDimitry Andric print(dbgs());
760b57cec5SDimitry Andric }
770b57cec5SDimitry Andric
instance()780b57cec5SDimitry Andric DebugCounter &DebugCounter::instance() { return *DC; }
790b57cec5SDimitry Andric
800b57cec5SDimitry Andric // This is called by the command line parser when it sees a value for the
810b57cec5SDimitry Andric // debug-counter option defined above.
push_back(const std::string & Val)820b57cec5SDimitry Andric void DebugCounter::push_back(const std::string &Val) {
830b57cec5SDimitry Andric if (Val.empty())
840b57cec5SDimitry Andric return;
850b57cec5SDimitry Andric // The strings should come in as counter=value
860b57cec5SDimitry Andric auto CounterPair = StringRef(Val).split('=');
870b57cec5SDimitry Andric if (CounterPair.second.empty()) {
880b57cec5SDimitry Andric errs() << "DebugCounter Error: " << Val << " does not have an = in it\n";
890b57cec5SDimitry Andric return;
900b57cec5SDimitry Andric }
910b57cec5SDimitry Andric // Now we have counter=value.
920b57cec5SDimitry Andric // First, process value.
930b57cec5SDimitry Andric int64_t CounterVal;
940b57cec5SDimitry Andric if (CounterPair.second.getAsInteger(0, CounterVal)) {
950b57cec5SDimitry Andric errs() << "DebugCounter Error: " << CounterPair.second
960b57cec5SDimitry Andric << " is not a number\n";
970b57cec5SDimitry Andric return;
980b57cec5SDimitry Andric }
990b57cec5SDimitry Andric // Now we need to see if this is the skip or the count, remove the suffix, and
1000b57cec5SDimitry Andric // add it to the counter values.
1010b57cec5SDimitry Andric if (CounterPair.first.endswith("-skip")) {
1020b57cec5SDimitry Andric auto CounterName = CounterPair.first.drop_back(5);
1035ffd83dbSDimitry Andric unsigned CounterID = getCounterId(std::string(CounterName));
1040b57cec5SDimitry Andric if (!CounterID) {
1050b57cec5SDimitry Andric errs() << "DebugCounter Error: " << CounterName
1060b57cec5SDimitry Andric << " is not a registered counter\n";
1070b57cec5SDimitry Andric return;
1080b57cec5SDimitry Andric }
1090b57cec5SDimitry Andric enableAllCounters();
1100b57cec5SDimitry Andric
1110b57cec5SDimitry Andric CounterInfo &Counter = Counters[CounterID];
1120b57cec5SDimitry Andric Counter.Skip = CounterVal;
1130b57cec5SDimitry Andric Counter.IsSet = true;
1140b57cec5SDimitry Andric } else if (CounterPair.first.endswith("-count")) {
1150b57cec5SDimitry Andric auto CounterName = CounterPair.first.drop_back(6);
1165ffd83dbSDimitry Andric unsigned CounterID = getCounterId(std::string(CounterName));
1170b57cec5SDimitry Andric if (!CounterID) {
1180b57cec5SDimitry Andric errs() << "DebugCounter Error: " << CounterName
1190b57cec5SDimitry Andric << " is not a registered counter\n";
1200b57cec5SDimitry Andric return;
1210b57cec5SDimitry Andric }
1220b57cec5SDimitry Andric enableAllCounters();
1230b57cec5SDimitry Andric
1240b57cec5SDimitry Andric CounterInfo &Counter = Counters[CounterID];
1250b57cec5SDimitry Andric Counter.StopAfter = CounterVal;
1260b57cec5SDimitry Andric Counter.IsSet = true;
1270b57cec5SDimitry Andric } else {
1280b57cec5SDimitry Andric errs() << "DebugCounter Error: " << CounterPair.first
1290b57cec5SDimitry Andric << " does not end with -skip or -count\n";
1300b57cec5SDimitry Andric }
1310b57cec5SDimitry Andric }
1320b57cec5SDimitry Andric
print(raw_ostream & OS) const1330b57cec5SDimitry Andric void DebugCounter::print(raw_ostream &OS) const {
1340b57cec5SDimitry Andric SmallVector<StringRef, 16> CounterNames(RegisteredCounters.begin(),
1350b57cec5SDimitry Andric RegisteredCounters.end());
136af732203SDimitry Andric sort(CounterNames);
1370b57cec5SDimitry Andric
1380b57cec5SDimitry Andric auto &Us = instance();
1390b57cec5SDimitry Andric OS << "Counters and values:\n";
1400b57cec5SDimitry Andric for (auto &CounterName : CounterNames) {
1415ffd83dbSDimitry Andric unsigned CounterID = getCounterId(std::string(CounterName));
1420b57cec5SDimitry Andric OS << left_justify(RegisteredCounters[CounterID], 32) << ": {"
1430b57cec5SDimitry Andric << Us.Counters[CounterID].Count << "," << Us.Counters[CounterID].Skip
1440b57cec5SDimitry Andric << "," << Us.Counters[CounterID].StopAfter << "}\n";
1450b57cec5SDimitry Andric }
1460b57cec5SDimitry Andric }
1470b57cec5SDimitry Andric
dump() const1480b57cec5SDimitry Andric LLVM_DUMP_METHOD void DebugCounter::dump() const {
1490b57cec5SDimitry Andric print(dbgs());
1500b57cec5SDimitry Andric }
151