LLVM  6.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("stats",
43  cl::desc("Enable statistics output from program (available with Asserts)"));
44 
45 
46 static cl::opt<bool> StatsAsJSON("stats-json",
47  cl::desc("Display statistics as json data"));
48 
49 static bool Enabled;
50 static bool PrintOnExit;
51 
52 namespace {
53 /// StatisticInfo - This class is used in a ManagedStatic so that it is created
54 /// on demand (when the first statistic is bumped) and destroyed only when
55 /// llvm_shutdown is called. We print statistics from the destructor.
56 class StatisticInfo {
57  std::vector<const Statistic*> Stats;
58  friend void llvm::PrintStatistics();
59  friend void llvm::PrintStatistics(raw_ostream &OS);
60  friend void llvm::PrintStatisticsJSON(raw_ostream &OS);
61 
62  /// Sort statistics by debugtype,name,description.
63  void sort();
64 public:
65  StatisticInfo();
66  ~StatisticInfo();
67 
68  void addStatistic(const Statistic *S) {
69  Stats.push_back(S);
70  }
71 };
72 }
73 
76 
77 /// RegisterStatistic - The first time a statistic is bumped, this method is
78 /// called.
80  // If stats are enabled, inform StatInfo that this statistic should be
81  // printed.
82  sys::SmartScopedLock<true> Writer(*StatLock);
83  if (!Initialized) {
84  if (Stats || Enabled)
85  StatInfo->addStatistic(this);
86 
87  TsanHappensBefore(this);
89  // Remember we have been registered.
91  Initialized = true;
93  }
94 }
95 
96 StatisticInfo::StatisticInfo() {
97  // Ensure timergroup lists are created first so they are destructed after us.
99 }
100 
101 // Print information when destroyed, iff command line option is specified.
102 StatisticInfo::~StatisticInfo() {
103  if (::Stats || PrintOnExit)
105 }
106 
107 void llvm::EnableStatistics(bool PrintOnExit) {
108  Enabled = true;
110 }
111 
113  return Enabled || Stats;
114 }
115 
116 void StatisticInfo::sort() {
117  std::stable_sort(Stats.begin(), Stats.end(),
118  [](const Statistic *LHS, const Statistic *RHS) {
119  if (int Cmp = std::strcmp(LHS->getDebugType(), RHS->getDebugType()))
120  return Cmp < 0;
121 
122  if (int Cmp = std::strcmp(LHS->getName(), RHS->getName()))
123  return Cmp < 0;
124 
125  return std::strcmp(LHS->getDesc(), RHS->getDesc()) < 0;
126  });
127 }
128 
130  StatisticInfo &Stats = *StatInfo;
131 
132  // Figure out how long the biggest Value and Name fields are.
133  unsigned MaxDebugTypeLen = 0, MaxValLen = 0;
134  for (size_t i = 0, e = Stats.Stats.size(); i != e; ++i) {
135  MaxValLen = std::max(MaxValLen,
136  (unsigned)utostr(Stats.Stats[i]->getValue()).size());
137  MaxDebugTypeLen = std::max(MaxDebugTypeLen,
138  (unsigned)std::strlen(Stats.Stats[i]->getDebugType()));
139  }
140 
141  Stats.sort();
142 
143  // Print out the statistics header...
144  OS << "===" << std::string(73, '-') << "===\n"
145  << " ... Statistics Collected ...\n"
146  << "===" << std::string(73, '-') << "===\n\n";
147 
148  // Print all of the statistics.
149  for (size_t i = 0, e = Stats.Stats.size(); i != e; ++i)
150  OS << format("%*u %-*s - %s\n",
151  MaxValLen, Stats.Stats[i]->getValue(),
152  MaxDebugTypeLen, Stats.Stats[i]->getDebugType(),
153  Stats.Stats[i]->getDesc());
154 
155  OS << '\n'; // Flush the output stream.
156  OS.flush();
157 }
158 
160  StatisticInfo &Stats = *StatInfo;
161 
162  Stats.sort();
163 
164  // Print all of the statistics.
165  OS << "{\n";
166  const char *delim = "";
167  for (const Statistic *Stat : Stats.Stats) {
168  OS << delim;
170  "Statistic group/type name is simple.");
171  assert(!yaml::needsQuotes(Stat->getName()) && "Statistic name is simple");
172  OS << "\t\"" << Stat->getDebugType() << '.' << Stat->getName() << "\": "
173  << Stat->getValue();
174  delim = ",\n";
175  }
176  // Print timers.
178 
179  OS << "\n}\n";
180  OS.flush();
181 }
182 
184 #if !defined(NDEBUG) || defined(LLVM_ENABLE_STATS)
185  StatisticInfo &Stats = *StatInfo;
186 
187  // Statistics not enabled?
188  if (Stats.Stats.empty()) return;
189 
190  // Get the stream to write to.
191  std::unique_ptr<raw_ostream> OutStream = CreateInfoOutputFile();
192  if (StatsAsJSON)
193  PrintStatisticsJSON(*OutStream);
194  else
195  PrintStatistics(*OutStream);
196 
197 #else
198  // Check if the -stats option is set instead of checking
199  // !Stats.Stats.empty(). In release builds, Statistics operators
200  // do nothing, so stats are never Registered.
201  if (Stats) {
202  // Get the stream to write to.
203  std::unique_ptr<raw_ostream> OutStream = CreateInfoOutputFile();
204  (*OutStream) << "Statistics are disabled. "
205  << "Build with asserts or with -DLLVM_ENABLE_STATS\n";
206  }
207 #endif
208 }
static bool PrintOnExit
Definition: Statistic.cpp:50
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
void EnableStatistics(bool PrintOnExit=true)
Enable the collection and printing of statistics.
Definition: Statistic.cpp:107
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
static cl::opt< bool > StatsAsJSON("stats-json", cl::desc("Display statistics as json data"))
void RegisterStatistic()
RegisterStatistic - The first time a statistic is bumped, this method is called.
Definition: Statistic.cpp:79
bool needsQuotes(StringRef S)
Definition: YAMLTraits.h:498
static cl::opt< bool > Stats("stats", cl::desc("Enable statistics output from program (available with Asserts)"))
-stats - Command line option to cause transformations to emit stats about what they did...
const char * getDebugType() const
Definition: Statistic.h:48
static ManagedStatic< StatisticInfo > StatInfo
Definition: Statistic.cpp:74
std::unique_ptr< raw_fd_ostream > CreateInfoOutputFile()
Return a file stream to print our output on.
Definition: Timer.cpp:53
#define TsanHappensBefore(cv)
Definition: Compiler.h:428
void MemoryFence()
Definition: Atomic.cpp:31
void PrintStatisticsJSON(raw_ostream &OS)
Print statistics in JSON format.
Definition: Statistic.cpp:159
unsigned getValue() const
Definition: Statistic.h:47
#define TsanIgnoreWritesBegin()
Definition: Compiler.h:430
const char * getDesc() const
Definition: Statistic.h:50
std::string utostr(uint64_t X, bool isNeg=false)
Definition: StringExtras.h:174
static bool Enabled
Definition: Statistic.cpp:49
#define TsanIgnoreWritesEnd()
Definition: Compiler.h:431
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:387
const char * getName() const
Definition: Statistic.h:49
void PrintStatistics()
Print statistics to the file returned by CreateInfoOutputFile().
Definition: Statistic.cpp:183
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static void ConstructTimerLists()
Ensure global timer group lists are initialized.
Definition: Timer.cpp:394
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
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:75
bool AreStatisticsEnabled()
Check if statistics are enabled.
Definition: Statistic.cpp:112