1 //===-- Statistic.cpp - Easy way to expose stats information --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the 'Statistic' class, which is designed to be an easy
11 // way to expose various success metrics from passes. These statistics are
12 // printed at the end of a run, when the -stats command line option is enabled
13 // on the command line.
15 // This is useful for reporting information like the number of instructions
16 // simplified, optimized or removed by various transformations, like this:
18 // static Statistic NumInstEliminated("GCSE", "Number of instructions killed");
20 // Later, in the code: ++NumInstEliminated;
22 //===----------------------------------------------------------------------===//
24 #include "llvm/ADT/Statistic.h"
25 #include "llvm/ADT/StringExtras.h"
26 #include "llvm/Support/CommandLine.h"
27 #include "llvm/Support/Compiler.h"
28 #include "llvm/Support/Debug.h"
29 #include "llvm/Support/Format.h"
30 #include "llvm/Support/ManagedStatic.h"
31 #include "llvm/Support/Mutex.h"
32 #include "llvm/Support/Timer.h"
33 #include "llvm/Support/YAMLTraits.h"
34 #include "llvm/Support/raw_ostream.h"
39 /// -stats - Command line option to cause transformations to emit stats about
42 static cl::opt<bool> Stats(
44 cl::desc("Enable statistics output from program (available with Asserts)"),
47 static cl::opt<bool> StatsAsJSON("stats-json",
48 cl::desc("Display statistics as json data"),
52 static bool PrintOnExit;
55 /// StatisticInfo - This class is used in a ManagedStatic so that it is created
56 /// on demand (when the first statistic is bumped) and destroyed only when
57 /// llvm_shutdown is called. We print statistics from the destructor.
59 std::vector<const Statistic*> Stats;
60 friend void llvm::PrintStatistics();
61 friend void llvm::PrintStatistics(raw_ostream &OS);
62 friend void llvm::PrintStatisticsJSON(raw_ostream &OS);
64 /// Sort statistics by debugtype,name,description.
70 void addStatistic(const Statistic *S) {
76 static ManagedStatic<StatisticInfo> StatInfo;
77 static ManagedStatic<sys::SmartMutex<true> > StatLock;
79 /// RegisterStatistic - The first time a statistic is bumped, this method is
81 void Statistic::RegisterStatistic() {
82 // If stats are enabled, inform StatInfo that this statistic should be
84 sys::SmartScopedLock<true> Writer(*StatLock);
87 StatInfo->addStatistic(this);
89 TsanHappensBefore(this);
91 // Remember we have been registered.
92 TsanIgnoreWritesBegin();
94 TsanIgnoreWritesEnd();
98 StatisticInfo::StatisticInfo() {
99 // Ensure timergroup lists are created first so they are destructed after us.
100 TimerGroup::ConstructTimerLists();
103 // Print information when destroyed, iff command line option is specified.
104 StatisticInfo::~StatisticInfo() {
105 if (::Stats || PrintOnExit)
106 llvm::PrintStatistics();
109 void llvm::EnableStatistics(bool PrintOnExit) {
111 ::PrintOnExit = PrintOnExit;
114 bool llvm::AreStatisticsEnabled() {
115 return Enabled || Stats;
118 void StatisticInfo::sort() {
119 std::stable_sort(Stats.begin(), Stats.end(),
120 [](const Statistic *LHS, const Statistic *RHS) {
121 if (int Cmp = std::strcmp(LHS->getDebugType(), RHS->getDebugType()))
124 if (int Cmp = std::strcmp(LHS->getName(), RHS->getName()))
127 return std::strcmp(LHS->getDesc(), RHS->getDesc()) < 0;
131 void llvm::PrintStatistics(raw_ostream &OS) {
132 StatisticInfo &Stats = *StatInfo;
134 // Figure out how long the biggest Value and Name fields are.
135 unsigned MaxDebugTypeLen = 0, MaxValLen = 0;
136 for (size_t i = 0, e = Stats.Stats.size(); i != e; ++i) {
137 MaxValLen = std::max(MaxValLen,
138 (unsigned)utostr(Stats.Stats[i]->getValue()).size());
139 MaxDebugTypeLen = std::max(MaxDebugTypeLen,
140 (unsigned)std::strlen(Stats.Stats[i]->getDebugType()));
145 // Print out the statistics header...
146 OS << "===" << std::string(73, '-') << "===\n"
147 << " ... Statistics Collected ...\n"
148 << "===" << std::string(73, '-') << "===\n\n";
150 // Print all of the statistics.
151 for (size_t i = 0, e = Stats.Stats.size(); i != e; ++i)
152 OS << format("%*u %-*s - %s\n",
153 MaxValLen, Stats.Stats[i]->getValue(),
154 MaxDebugTypeLen, Stats.Stats[i]->getDebugType(),
155 Stats.Stats[i]->getDesc());
157 OS << '\n'; // Flush the output stream.
161 void llvm::PrintStatisticsJSON(raw_ostream &OS) {
162 StatisticInfo &Stats = *StatInfo;
166 // Print all of the statistics.
168 const char *delim = "";
169 for (const Statistic *Stat : Stats.Stats) {
171 assert(yaml::needsQuotes(Stat->getDebugType()) == yaml::QuotingType::None &&
172 "Statistic group/type name is simple.");
173 assert(yaml::needsQuotes(Stat->getName()) == yaml::QuotingType::None &&
174 "Statistic name is simple");
175 OS << "\t\"" << Stat->getDebugType() << '.' << Stat->getName() << "\": "
180 TimerGroup::printAllJSONValues(OS, delim);
186 void llvm::PrintStatistics() {
187 #if !defined(NDEBUG) || defined(LLVM_ENABLE_STATS)
188 StatisticInfo &Stats = *StatInfo;
190 // Statistics not enabled?
191 if (Stats.Stats.empty()) return;
193 // Get the stream to write to.
194 std::unique_ptr<raw_ostream> OutStream = CreateInfoOutputFile();
196 PrintStatisticsJSON(*OutStream);
198 PrintStatistics(*OutStream);
201 // Check if the -stats option is set instead of checking
202 // !Stats.Stats.empty(). In release builds, Statistics operators
203 // do nothing, so stats are never Registered.
205 // Get the stream to write to.
206 std::unique_ptr<raw_ostream> OutStream = CreateInfoOutputFile();
207 (*OutStream) << "Statistics are disabled. "
208 << "Build with asserts or with -DLLVM_ENABLE_STATS\n";