LLVM  7.0.0svn
Statistic.cpp
Go to the documentation of this file.
1 //===-- Statistic.cpp - Easy way to expose stats information --------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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.
14 //
15 // This is useful for reporting information like the number of instructions
16 // simplified, optimized or removed by various transformations, like this:
17 //
18 // static Statistic NumInstEliminated("GCSE", "Number of instructions killed");
19 //
20 // Later, in the code: ++NumInstEliminated;
21 //
22 //===----------------------------------------------------------------------===//
23 
24 #include "llvm/ADT/Statistic.h"
25 #include "llvm/ADT/StringExtras.h"
27 #include "llvm/Support/Compiler.h"
28 #include "llvm/Support/Debug.h"
29 #include "llvm/Support/Format.h"
31 #include "llvm/Support/Mutex.h"
32 #include "llvm/Support/Timer.h"
35 #include <algorithm>
36 #include <cstring>
37 using namespace llvm;
38 
39 /// -stats - Command line option to cause transformations to emit stats about
40 /// what they did.
41 ///
42 static cl::opt<bool> Stats(
43  "stats",
44  cl::desc("Enable statistics output from program (available with Asserts)"),
45  cl::Hidden);
46 
47 static cl::opt<bool> StatsAsJSON("stats-json",
48  cl::desc("Display statistics as json data"),
49  cl::Hidden);
50 
51 static bool Enabled;
52 static bool PrintOnExit;
53 
54 namespace {
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.
58 class StatisticInfo {
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);
63 
64  /// Sort statistics by debugtype,name,description.
65  void sort();
66 public:
67  StatisticInfo();
68  ~StatisticInfo();
69 
70  void addStatistic(const Statistic *S) {
71  Stats.push_back(S);
72  }
73 };
74 }
75 
78 
79 /// RegisterStatistic - The first time a statistic is bumped, this method is
80 /// called.
82  // If stats are enabled, inform StatInfo that this statistic should be
83  // printed.
84  sys::SmartScopedLock<true> Writer(*StatLock);
85  if (!Initialized.load(std::memory_order_relaxed)) {
86  if (Stats || Enabled)
87  StatInfo->addStatistic(this);
88 
89  // Remember we have been registered.
90  Initialized.store(true, std::memory_order_release);
91  }
92 }
93 
94 StatisticInfo::StatisticInfo() {
95  // Ensure timergroup lists are created first so they are destructed after us.
97 }
98 
99 // Print information when destroyed, iff command line option is specified.
100 StatisticInfo::~StatisticInfo() {
101  if (::Stats || PrintOnExit)
103 }
104 
105 void llvm::EnableStatistics(bool PrintOnExit) {
106  Enabled = true;
108 }
109 
111  return Enabled || Stats;
112 }
113 
114 void StatisticInfo::sort() {
115  std::stable_sort(Stats.begin(), Stats.end(),
116  [](const Statistic *LHS, const Statistic *RHS) {
117  if (int Cmp = std::strcmp(LHS->getDebugType(), RHS->getDebugType()))
118  return Cmp < 0;
119 
120  if (int Cmp = std::strcmp(LHS->getName(), RHS->getName()))
121  return Cmp < 0;
122 
123  return std::strcmp(LHS->getDesc(), RHS->getDesc()) < 0;
124  });
125 }
126 
128  StatisticInfo &Stats = *StatInfo;
129 
130  // Figure out how long the biggest Value and Name fields are.
131  unsigned MaxDebugTypeLen = 0, MaxValLen = 0;
132  for (size_t i = 0, e = Stats.Stats.size(); i != e; ++i) {
133  MaxValLen = std::max(MaxValLen,
134  (unsigned)utostr(Stats.Stats[i]->getValue()).size());
135  MaxDebugTypeLen = std::max(MaxDebugTypeLen,
136  (unsigned)std::strlen(Stats.Stats[i]->getDebugType()));
137  }
138 
139  Stats.sort();
140 
141  // Print out the statistics header...
142  OS << "===" << std::string(73, '-') << "===\n"
143  << " ... Statistics Collected ...\n"
144  << "===" << std::string(73, '-') << "===\n\n";
145 
146  // Print all of the statistics.
147  for (size_t i = 0, e = Stats.Stats.size(); i != e; ++i)
148  OS << format("%*u %-*s - %s\n",
149  MaxValLen, Stats.Stats[i]->getValue(),
150  MaxDebugTypeLen, Stats.Stats[i]->getDebugType(),
151  Stats.Stats[i]->getDesc());
152 
153  OS << '\n'; // Flush the output stream.
154  OS.flush();
155 }
156 
158  StatisticInfo &Stats = *StatInfo;
159 
160  Stats.sort();
161 
162  // Print all of the statistics.
163  OS << "{\n";
164  const char *delim = "";
165  for (const Statistic *Stat : Stats.Stats) {
166  OS << delim;
167  assert(yaml::needsQuotes(Stat->getDebugType()) == yaml::QuotingType::None &&
168  "Statistic group/type name is simple.");
169  assert(yaml::needsQuotes(Stat->getName()) == yaml::QuotingType::None &&
170  "Statistic name is simple");
171  OS << "\t\"" << Stat->getDebugType() << '.' << Stat->getName() << "\": "
172  << Stat->getValue();
173  delim = ",\n";
174  }
175  // Print timers.
177 
178  OS << "\n}\n";
179  OS.flush();
180 }
181 
183 #if !defined(NDEBUG) || defined(LLVM_ENABLE_STATS)
184  StatisticInfo &Stats = *StatInfo;
185 
186  // Statistics not enabled?
187  if (Stats.Stats.empty()) return;
188 
189  // Get the stream to write to.
190  std::unique_ptr<raw_ostream> OutStream = CreateInfoOutputFile();
191  if (StatsAsJSON)
192  PrintStatisticsJSON(*OutStream);
193  else
194  PrintStatistics(*OutStream);
195 
196 #else
197  // Check if the -stats option is set instead of checking
198  // !Stats.Stats.empty(). In release builds, Statistics operators
199  // do nothing, so stats are never Registered.
200  if (Stats) {
201  // Get the stream to write to.
202  std::unique_ptr<raw_ostream> OutStream = CreateInfoOutputFile();
203  (*OutStream) << "Statistics are disabled. "
204  << "Build with asserts or with -DLLVM_ENABLE_STATS\n";
205  }
206 #endif
207 }
const NoneType None
Definition: None.h:24
static bool PrintOnExit
Definition: Statistic.cpp:52
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
void EnableStatistics(bool PrintOnExit=true)
Enable the collection and printing of statistics.
Definition: Statistic.cpp:105
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition: Format.h:124
void RegisterStatistic()
RegisterStatistic - The first time a statistic is bumped, this method is called.
Definition: Statistic.cpp:81
const char * getDebugType() const
Definition: Statistic.h:47
static ManagedStatic< StatisticInfo > StatInfo
Definition: Statistic.cpp:76
std::unique_ptr< raw_fd_ostream > CreateInfoOutputFile()
Return a file stream to print our output on.
Definition: Timer.cpp:53
void PrintStatisticsJSON(raw_ostream &OS)
Print statistics in JSON format.
Definition: Statistic.cpp:157
unsigned getValue() const
Definition: Statistic.h:46
const char * getDesc() const
Definition: Statistic.h:49
std::string utostr(uint64_t X, bool isNeg=false)
Definition: StringExtras.h:193
static bool Enabled
Definition: Statistic.cpp:51
static const char * printAllJSONValues(raw_ostream &OS, const char *delim)
Prints all timers as JSON key/value pairs, and clears them all out.
Definition: Timer.cpp:397
const char * getName() const
Definition: Statistic.h:48
void PrintStatistics()
Print statistics to the file returned by CreateInfoOutputFile().
Definition: Statistic.cpp:182
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static void ConstructTimerLists()
Ensure global timer group lists are initialized.
Definition: Timer.cpp:404
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
static cl::opt< bool > Stats("stats", cl::desc("Enable statistics output from program (available with Asserts)"), cl::Hidden)
-stats - Command line option to cause transformations to emit stats about what they did...
ManagedStatic - This transparently changes the behavior of global statics to be lazily constructed on...
Definition: ManagedStatic.h:61
void sort(Policy policy, RandomAccessIterator Start, RandomAccessIterator End, const Comparator &Comp=Comparator())
Definition: Parallel.h:199
static ManagedStatic< sys::SmartMutex< true > > StatLock
Definition: Statistic.cpp:77
bool AreStatisticsEnabled()
Check if statistics are enabled.
Definition: Statistic.cpp:110
static cl::opt< bool > StatsAsJSON("stats-json", cl::desc("Display statistics as json data"), cl::Hidden)