ee6c1305cdbe526473b0946c23168b6a6152b67a
[oota-llvm.git] / lib / Support / Statistic.cpp
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"
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/raw_ostream.h"
33 #include <algorithm>
34 #include <cstring>
35 using namespace llvm;
36
37 // CreateInfoOutputFile - Return a file stream to print our output on.
38 namespace llvm { extern raw_ostream *CreateInfoOutputFile(); }
39
40 /// -stats - Command line option to cause transformations to emit stats about
41 /// what they did.
42 ///
43 static cl::opt<bool>
44 Enabled(
45     "stats",
46     cl::desc("Enable statistics output from program (available with Asserts)"));
47
48
49 namespace {
50 /// StatisticInfo - This class is used in a ManagedStatic so that it is created
51 /// on demand (when the first statistic is bumped) and destroyed only when
52 /// llvm_shutdown is called.  We print statistics from the destructor.
53 class StatisticInfo {
54   std::vector<const Statistic*> Stats;
55   friend void llvm::PrintStatistics();
56   friend void llvm::PrintStatistics(raw_ostream &OS);
57 public:
58   ~StatisticInfo();
59
60   void addStatistic(const Statistic *S) {
61     Stats.push_back(S);
62   }
63 };
64 }
65
66 static ManagedStatic<StatisticInfo> StatInfo;
67 static ManagedStatic<sys::SmartMutex<true> > StatLock;
68
69 /// RegisterStatistic - The first time a statistic is bumped, this method is
70 /// called.
71 void Statistic::RegisterStatistic() {
72   // If stats are enabled, inform StatInfo that this statistic should be
73   // printed.
74   sys::SmartScopedLock<true> Writer(*StatLock);
75   if (!Initialized) {
76     if (Enabled)
77       StatInfo->addStatistic(this);
78
79     TsanHappensBefore(this);
80     sys::MemoryFence();
81     // Remember we have been registered.
82     TsanIgnoreWritesBegin();
83     Initialized = true;
84     TsanIgnoreWritesEnd();
85   }
86 }
87
88 // Print information when destroyed, iff command line option is specified.
89 StatisticInfo::~StatisticInfo() {
90   llvm::PrintStatistics();
91 }
92
93 void llvm::EnableStatistics() {
94   Enabled.setValue(true);
95 }
96
97 bool llvm::AreStatisticsEnabled() {
98   return Enabled;
99 }
100
101 void llvm::PrintStatistics(raw_ostream &OS) {
102   StatisticInfo &Stats = *StatInfo;
103
104   // Figure out how long the biggest Value and Name fields are.
105   unsigned MaxNameLen = 0, MaxValLen = 0;
106   for (size_t i = 0, e = Stats.Stats.size(); i != e; ++i) {
107     MaxValLen = std::max(MaxValLen,
108                          (unsigned)utostr(Stats.Stats[i]->getValue()).size());
109     MaxNameLen = std::max(MaxNameLen,
110                           (unsigned)std::strlen(Stats.Stats[i]->getName()));
111   }
112
113   // Sort the fields by name.
114   std::stable_sort(Stats.Stats.begin(), Stats.Stats.end(),
115                    [](const Statistic *LHS, const Statistic *RHS) {
116     if (int Cmp = std::strcmp(LHS->getName(), RHS->getName()))
117       return Cmp < 0;
118
119     // Secondary key is the description.
120     return std::strcmp(LHS->getDesc(), RHS->getDesc()) < 0;
121   });
122
123   // Print out the statistics header...
124   OS << "===" << std::string(73, '-') << "===\n"
125      << "                          ... Statistics Collected ...\n"
126      << "===" << std::string(73, '-') << "===\n\n";
127
128   // Print all of the statistics.
129   for (size_t i = 0, e = Stats.Stats.size(); i != e; ++i)
130     OS << format("%*u %-*s - %s\n",
131                  MaxValLen, Stats.Stats[i]->getValue(),
132                  MaxNameLen, Stats.Stats[i]->getName(),
133                  Stats.Stats[i]->getDesc());
134
135   OS << '\n';  // Flush the output stream.
136   OS.flush();
137
138 }
139
140 void llvm::PrintStatistics() {
141 #if !defined(NDEBUG) || defined(LLVM_ENABLE_STATS)
142   StatisticInfo &Stats = *StatInfo;
143
144   // Statistics not enabled?
145   if (Stats.Stats.empty()) return;
146
147   // Get the stream to write to.
148   raw_ostream &OutStream = *CreateInfoOutputFile();
149   PrintStatistics(OutStream);
150   delete &OutStream;   // Close the file.
151 #else
152   // Check if the -stats option is set instead of checking
153   // !Stats.Stats.empty().  In release builds, Statistics operators
154   // do nothing, so stats are never Registered.
155   if (Enabled) {
156     // Get the stream to write to.
157     raw_ostream &OutStream = *CreateInfoOutputFile();
158     OutStream << "Statistics are disabled.  "
159             << "Build with asserts or with -DLLVM_ENABLE_STATS\n";
160     OutStream.flush();
161     delete &OutStream;   // Close the file.
162   }
163 #endif
164 }