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/Debug.h"
28 #include "llvm/Support/Format.h"
29 #include "llvm/Support/ManagedStatic.h"
30 #include "llvm/Support/Mutex.h"
31 #include "llvm/Support/raw_ostream.h"
36 // CreateInfoOutputFile - Return a file stream to print our output on.
37 namespace llvm { extern raw_ostream *CreateInfoOutputFile(); }
39 /// -stats - Command line option to cause transformations to emit stats about
45 cl::desc("Enable statistics output from program (available with Asserts)"));
49 /// StatisticInfo - This class is used in a ManagedStatic so that it is created
50 /// on demand (when the first statistic is bumped) and destroyed only when
51 /// llvm_shutdown is called. We print statistics from the destructor.
53 std::vector<const Statistic*> Stats;
54 friend void llvm::PrintStatistics();
55 friend void llvm::PrintStatistics(raw_ostream &OS);
59 void addStatistic(const Statistic *S) {
65 static ManagedStatic<StatisticInfo> StatInfo;
66 static ManagedStatic<sys::SmartMutex<true> > StatLock;
68 /// RegisterStatistic - The first time a statistic is bumped, this method is
70 void Statistic::RegisterStatistic() {
71 // If stats are enabled, inform StatInfo that this statistic should be
73 sys::SmartScopedLock<true> Writer(*StatLock);
76 StatInfo->addStatistic(this);
78 TsanHappensBefore(this);
80 // Remember we have been registered.
81 TsanIgnoreWritesBegin();
83 TsanIgnoreWritesEnd();
87 // Print information when destroyed, iff command line option is specified.
88 StatisticInfo::~StatisticInfo() {
89 llvm::PrintStatistics();
92 void llvm::EnableStatistics() {
93 Enabled.setValue(true);
96 bool llvm::AreStatisticsEnabled() {
100 void llvm::PrintStatistics(raw_ostream &OS) {
101 StatisticInfo &Stats = *StatInfo;
103 // Figure out how long the biggest Value and Name fields are.
104 unsigned MaxNameLen = 0, MaxValLen = 0;
105 for (size_t i = 0, e = Stats.Stats.size(); i != e; ++i) {
106 MaxValLen = std::max(MaxValLen,
107 (unsigned)utostr(Stats.Stats[i]->getValue()).size());
108 MaxNameLen = std::max(MaxNameLen,
109 (unsigned)std::strlen(Stats.Stats[i]->getName()));
112 // Sort the fields by name.
113 std::stable_sort(Stats.Stats.begin(), Stats.Stats.end(),
114 [](const Statistic *LHS, const Statistic *RHS) {
115 if (int Cmp = std::strcmp(LHS->getName(), RHS->getName()))
118 // Secondary key is the description.
119 return std::strcmp(LHS->getDesc(), RHS->getDesc()) < 0;
122 // Print out the statistics header...
123 OS << "===" << std::string(73, '-') << "===\n"
124 << " ... Statistics Collected ...\n"
125 << "===" << std::string(73, '-') << "===\n\n";
127 // Print all of the statistics.
128 for (size_t i = 0, e = Stats.Stats.size(); i != e; ++i)
129 OS << format("%*u %-*s - %s\n",
130 MaxValLen, Stats.Stats[i]->getValue(),
131 MaxNameLen, Stats.Stats[i]->getName(),
132 Stats.Stats[i]->getDesc());
134 OS << '\n'; // Flush the output stream.
139 void llvm::PrintStatistics() {
140 #if !defined(NDEBUG) || defined(LLVM_ENABLE_STATS)
141 StatisticInfo &Stats = *StatInfo;
143 // Statistics not enabled?
144 if (Stats.Stats.empty()) return;
146 // Get the stream to write to.
147 raw_ostream &OutStream = *CreateInfoOutputFile();
148 PrintStatistics(OutStream);
149 delete &OutStream; // Close the file.
151 // Check if the -stats option is set instead of checking
152 // !Stats.Stats.empty(). In release builds, Statistics operators
153 // do nothing, so stats are never Registered.
155 // Get the stream to write to.
156 raw_ostream &OutStream = *CreateInfoOutputFile();
157 OutStream << "Statistics are disabled. "
158 << "Build with asserts or with -DLLVM_ENABLE_STATS\n";
160 delete &OutStream; // Close the file.