1ce3d348aSDaniel Berlin #include "llvm/Support/DebugCounter.h"
276374573SMehdi Amini
376374573SMehdi Amini #include "DebugOptions.h"
476374573SMehdi Amini
5ce3d348aSDaniel Berlin #include "llvm/Support/CommandLine.h"
6ce3d348aSDaniel Berlin #include "llvm/Support/Format.h"
7ce3d348aSDaniel Berlin #include "llvm/Support/ManagedStatic.h"
8ce3d348aSDaniel Berlin
9ce3d348aSDaniel Berlin using namespace llvm;
10ce3d348aSDaniel Berlin
11debb3c35SBenjamin Kramer namespace {
12ce3d348aSDaniel Berlin // This class overrides the default list implementation of printing so we
13ce3d348aSDaniel Berlin // can pretty print the list of debug counter options. This type of
14ce3d348aSDaniel Berlin // dynamic option is pretty rare (basically this and pass lists).
15ce3d348aSDaniel Berlin class DebugCounterList : public cl::list<std::string, DebugCounter> {
16ce3d348aSDaniel Berlin private:
17ce3d348aSDaniel Berlin using Base = cl::list<std::string, DebugCounter>;
18ce3d348aSDaniel Berlin
19ce3d348aSDaniel Berlin public:
20ce3d348aSDaniel Berlin template <class... Mods>
DebugCounterList(Mods &&...Ms)21ce3d348aSDaniel Berlin explicit DebugCounterList(Mods &&... Ms) : Base(std::forward<Mods>(Ms)...) {}
22ce3d348aSDaniel Berlin
23ce3d348aSDaniel Berlin private:
printOptionInfo(size_t GlobalWidth) const24ce3d348aSDaniel Berlin void printOptionInfo(size_t GlobalWidth) const override {
25ce3d348aSDaniel Berlin // This is a variant of from generic_parser_base::printOptionInfo. Sadly,
26ce3d348aSDaniel Berlin // it's not easy to make it more usable. We could get it to print these as
27ce3d348aSDaniel Berlin // options if we were a cl::opt and registered them, but lists don't have
28ce3d348aSDaniel Berlin // options, nor does the parser for std::string. The other mechanisms for
29ce3d348aSDaniel Berlin // options are global and would pollute the global namespace with our
30ce3d348aSDaniel Berlin // counters. Rather than go that route, we have just overridden the
31ce3d348aSDaniel Berlin // printing, which only a few things call anyway.
32ce3d348aSDaniel Berlin outs() << " -" << ArgStr;
33ce3d348aSDaniel Berlin // All of the other options in CommandLine.cpp use ArgStr.size() + 6 for
34ce3d348aSDaniel Berlin // width, so we do the same.
35ce3d348aSDaniel Berlin Option::printHelpStr(HelpStr, GlobalWidth, ArgStr.size() + 6);
36ce3d348aSDaniel Berlin const auto &CounterInstance = DebugCounter::instance();
3758a85717SSimon Pilgrim for (const auto &Name : CounterInstance) {
38ce3d348aSDaniel Berlin const auto Info =
39ce3d348aSDaniel Berlin CounterInstance.getCounterInfo(CounterInstance.getCounterId(Name));
40ce3d348aSDaniel Berlin size_t NumSpaces = GlobalWidth - Info.first.size() - 8;
41ce3d348aSDaniel Berlin outs() << " =" << Info.first;
42ce3d348aSDaniel Berlin outs().indent(NumSpaces) << " - " << Info.second << '\n';
43ce3d348aSDaniel Berlin }
44ce3d348aSDaniel Berlin }
45ce3d348aSDaniel Berlin };
4642f588f3SMehdi Amini
4776374573SMehdi Amini struct CreateDebugCounterOption {
call__anon7e130d330111::CreateDebugCounterOption4876374573SMehdi Amini static void *call() {
4976374573SMehdi Amini return new DebugCounterList(
508d051d85SMehdi Amini "debug-counter", cl::Hidden,
518d051d85SMehdi Amini cl::desc("Comma separated list of debug counter skip and count"),
52*95a13425SFangrui Song cl::CommaSeparated, cl::location(DebugCounter::instance()));
5376374573SMehdi Amini }
5476374573SMehdi Amini };
5576374573SMehdi Amini } // namespace
56af932173SMehdi Amini
5776374573SMehdi Amini static ManagedStatic<DebugCounterList, CreateDebugCounterOption>
5876374573SMehdi Amini DebugCounterOption;
5976374573SMehdi Amini static bool PrintDebugCounter;
6076374573SMehdi Amini
initDebugCounterOptions()6176374573SMehdi Amini void llvm::initDebugCounterOptions() {
6276374573SMehdi Amini *DebugCounterOption;
6376374573SMehdi Amini static cl::opt<bool, true> RegisterPrintDebugCounter(
6476374573SMehdi Amini "print-debug-counter", cl::Hidden, cl::location(PrintDebugCounter),
6576374573SMehdi Amini cl::init(false), cl::Optional,
6613f76f84SZhizhou Yang cl::desc("Print out debug counter info after all counters accumulated"));
6776374573SMehdi Amini }
6813f76f84SZhizhou Yang
69ce3d348aSDaniel Berlin static ManagedStatic<DebugCounter> DC;
70ce3d348aSDaniel Berlin
7113f76f84SZhizhou Yang // Print information when destroyed, iff command line option is specified.
~DebugCounter()7213f76f84SZhizhou Yang DebugCounter::~DebugCounter() {
7313f76f84SZhizhou Yang if (isCountingEnabled() && PrintDebugCounter)
7413f76f84SZhizhou Yang print(dbgs());
7513f76f84SZhizhou Yang }
7613f76f84SZhizhou Yang
instance()77ce3d348aSDaniel Berlin DebugCounter &DebugCounter::instance() { return *DC; }
78ce3d348aSDaniel Berlin
79ce3d348aSDaniel Berlin // This is called by the command line parser when it sees a value for the
80ce3d348aSDaniel Berlin // debug-counter option defined above.
push_back(const std::string & Val)81ce3d348aSDaniel Berlin void DebugCounter::push_back(const std::string &Val) {
82ce3d348aSDaniel Berlin if (Val.empty())
83ce3d348aSDaniel Berlin return;
84ce3d348aSDaniel Berlin // The strings should come in as counter=value
85ce3d348aSDaniel Berlin auto CounterPair = StringRef(Val).split('=');
86ce3d348aSDaniel Berlin if (CounterPair.second.empty()) {
87ce3d348aSDaniel Berlin errs() << "DebugCounter Error: " << Val << " does not have an = in it\n";
88ce3d348aSDaniel Berlin return;
89ce3d348aSDaniel Berlin }
90ce3d348aSDaniel Berlin // Now we have counter=value.
91ce3d348aSDaniel Berlin // First, process value.
92b00fb464SGeorge Burgess IV int64_t CounterVal;
93ce3d348aSDaniel Berlin if (CounterPair.second.getAsInteger(0, CounterVal)) {
94ce3d348aSDaniel Berlin errs() << "DebugCounter Error: " << CounterPair.second
95ce3d348aSDaniel Berlin << " is not a number\n";
96ce3d348aSDaniel Berlin return;
97ce3d348aSDaniel Berlin }
98ce3d348aSDaniel Berlin // Now we need to see if this is the skip or the count, remove the suffix, and
99ce3d348aSDaniel Berlin // add it to the counter values.
100ce3d348aSDaniel Berlin if (CounterPair.first.endswith("-skip")) {
101ce3d348aSDaniel Berlin auto CounterName = CounterPair.first.drop_back(5);
102adcd0268SBenjamin Kramer unsigned CounterID = getCounterId(std::string(CounterName));
103ce3d348aSDaniel Berlin if (!CounterID) {
104ce3d348aSDaniel Berlin errs() << "DebugCounter Error: " << CounterName
105ce3d348aSDaniel Berlin << " is not a registered counter\n";
106ce3d348aSDaniel Berlin return;
107ce3d348aSDaniel Berlin }
10831da130eSGeorge Burgess IV enableAllCounters();
10904df67e2SGeorge Burgess IV
11004df67e2SGeorge Burgess IV CounterInfo &Counter = Counters[CounterID];
11104df67e2SGeorge Burgess IV Counter.Skip = CounterVal;
11204df67e2SGeorge Burgess IV Counter.IsSet = true;
113ce3d348aSDaniel Berlin } else if (CounterPair.first.endswith("-count")) {
114ce3d348aSDaniel Berlin auto CounterName = CounterPair.first.drop_back(6);
115adcd0268SBenjamin Kramer unsigned CounterID = getCounterId(std::string(CounterName));
116ce3d348aSDaniel Berlin if (!CounterID) {
117ce3d348aSDaniel Berlin errs() << "DebugCounter Error: " << CounterName
118ce3d348aSDaniel Berlin << " is not a registered counter\n";
119ce3d348aSDaniel Berlin return;
120ce3d348aSDaniel Berlin }
12131da130eSGeorge Burgess IV enableAllCounters();
12204df67e2SGeorge Burgess IV
12304df67e2SGeorge Burgess IV CounterInfo &Counter = Counters[CounterID];
12404df67e2SGeorge Burgess IV Counter.StopAfter = CounterVal;
12504df67e2SGeorge Burgess IV Counter.IsSet = true;
126ce3d348aSDaniel Berlin } else {
127ce3d348aSDaniel Berlin errs() << "DebugCounter Error: " << CounterPair.first
128ce3d348aSDaniel Berlin << " does not end with -skip or -count\n";
129ce3d348aSDaniel Berlin }
130ce3d348aSDaniel Berlin }
131ce3d348aSDaniel Berlin
print(raw_ostream & OS) const132dceb612eSFrederich Munch void DebugCounter::print(raw_ostream &OS) const {
13313f76f84SZhizhou Yang SmallVector<StringRef, 16> CounterNames(RegisteredCounters.begin(),
13413f76f84SZhizhou Yang RegisteredCounters.end());
135352fcfc6SKazu Hirata sort(CounterNames);
13613f76f84SZhizhou Yang
13713f76f84SZhizhou Yang auto &Us = instance();
138ce3d348aSDaniel Berlin OS << "Counters and values:\n";
13913f76f84SZhizhou Yang for (auto &CounterName : CounterNames) {
140adcd0268SBenjamin Kramer unsigned CounterID = getCounterId(std::string(CounterName));
14113f76f84SZhizhou Yang OS << left_justify(RegisteredCounters[CounterID], 32) << ": {"
14213f76f84SZhizhou Yang << Us.Counters[CounterID].Count << "," << Us.Counters[CounterID].Skip
14313f76f84SZhizhou Yang << "," << Us.Counters[CounterID].StopAfter << "}\n";
14413f76f84SZhizhou Yang }
145ce3d348aSDaniel Berlin }
146dceb612eSFrederich Munch
dump() const147dceb612eSFrederich Munch LLVM_DUMP_METHOD void DebugCounter::dump() const {
148dceb612eSFrederich Munch print(dbgs());
149dceb612eSFrederich Munch }
150