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/Support/CommandLine.h"
26 #include "llvm/Support/Debug.h"
27 #include "llvm/Support/Format.h"
28 #include "llvm/Support/ManagedStatic.h"
29 #include "llvm/Support/raw_ostream.h"
30 #include "llvm/Support/Mutex.h"
31 #include "llvm/ADT/StringExtras.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
43 Enabled("stats", cl::desc("Enable statistics output from program"));
47 /// StatisticInfo - This class is used in a ManagedStatic so that it is created
48 /// on demand (when the first statistic is bumped) and destroyed only when
49 /// llvm_shutdown is called. We print statistics from the destructor.
51 std::vector<const Statistic*> Stats;
52 friend void llvm::PrintStatistics();
53 friend void llvm::PrintStatistics(raw_ostream &OS);
57 void addStatistic(const Statistic *S) {
63 static ManagedStatic<StatisticInfo> StatInfo;
64 static ManagedStatic<sys::SmartMutex<true> > StatLock;
66 /// RegisterStatistic - The first time a statistic is bumped, this method is
68 void Statistic::RegisterStatistic() {
69 // If stats are enabled, inform StatInfo that this statistic should be
71 sys::SmartScopedLock<true> Writer(*StatLock);
74 StatInfo->addStatistic(this);
76 TsanHappensBefore(this);
78 // Remember we have been registered.
79 TsanIgnoreWritesBegin();
81 TsanIgnoreWritesEnd();
88 bool operator()(const Statistic *LHS, const Statistic *RHS) const {
89 int Cmp = std::strcmp(LHS->getName(), RHS->getName());
90 if (Cmp != 0) return Cmp < 0;
92 // Secondary key is the description.
93 return std::strcmp(LHS->getDesc(), RHS->getDesc()) < 0;
99 // Print information when destroyed, if command line option is specified.
100 StatisticInfo::~StatisticInfo() {
101 llvm::PrintStatistics();
104 void llvm::EnableStatistics() {
105 Enabled.setValue(true);
108 bool llvm::AreStatisticsEnabled() {
112 void llvm::PrintStatistics(raw_ostream &OS) {
113 StatisticInfo &Stats = *StatInfo;
115 // Figure out how long the biggest Value and Name fields are.
116 unsigned MaxNameLen = 0, MaxValLen = 0;
117 for (size_t i = 0, e = Stats.Stats.size(); i != e; ++i) {
118 MaxValLen = std::max(MaxValLen,
119 (unsigned)utostr(Stats.Stats[i]->getValue()).size());
120 MaxNameLen = std::max(MaxNameLen,
121 (unsigned)std::strlen(Stats.Stats[i]->getName()));
124 // Sort the fields by name.
125 std::stable_sort(Stats.Stats.begin(), Stats.Stats.end(), NameCompare());
127 // Print out the statistics header...
128 OS << "===" << std::string(73, '-') << "===\n"
129 << " ... Statistics Collected ...\n"
130 << "===" << std::string(73, '-') << "===\n\n";
132 // Print all of the statistics.
133 for (size_t i = 0, e = Stats.Stats.size(); i != e; ++i)
134 OS << format("%*u %-*s - %s\n",
135 MaxValLen, Stats.Stats[i]->getValue(),
136 MaxNameLen, Stats.Stats[i]->getName(),
137 Stats.Stats[i]->getDesc());
139 OS << '\n'; // Flush the output stream.
144 void llvm::PrintStatistics() {
145 StatisticInfo &Stats = *StatInfo;
147 // Statistics not enabled?
148 if (Stats.Stats.empty()) return;
150 // Get the stream to write to.
151 raw_ostream &OutStream = *CreateInfoOutputFile();
152 PrintStatistics(OutStream);
153 delete &OutStream; // Close the file.