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/ManagedStatic.h"
28 #include "llvm/Support/raw_ostream.h"
29 #include "llvm/Support/Mutex.h"
30 #include "llvm/ADT/StringExtras.h"
35 // CreateInfoOutputFile - Return a file stream to print our output on.
36 namespace llvm
{ extern raw_ostream
*CreateInfoOutputFile(); }
38 /// -stats - Command line option to cause transformations to emit stats about
42 Enabled("stats", cl::desc("Enable statistics output from program"));
46 /// StatisticInfo - This class is used in a ManagedStatic so that it is created
47 /// on demand (when the first statistic is bumped) and destroyed only when
48 /// llvm_shutdown is called. We print statistics from the destructor.
50 std::vector
<const Statistic
*> Stats
;
51 friend void llvm::PrintStatistics();
52 friend void llvm::PrintStatistics(raw_ostream
&OS
);
56 void addStatistic(const Statistic
*S
) {
62 static ManagedStatic
<StatisticInfo
> StatInfo
;
63 static ManagedStatic
<sys::SmartMutex
<true> > StatLock
;
65 /// RegisterStatistic - The first time a statistic is bumped, this method is
67 void Statistic::RegisterStatistic() {
68 // If stats are enabled, inform StatInfo that this statistic should be
70 sys::SmartScopedLock
<true> Writer(*StatLock
);
73 StatInfo
->addStatistic(this);
76 // Remember we have been registered.
84 bool operator()(const Statistic
*LHS
, const Statistic
*RHS
) const {
85 int Cmp
= std::strcmp(LHS
->getName(), RHS
->getName());
86 if (Cmp
!= 0) return Cmp
< 0;
88 // Secondary key is the description.
89 return std::strcmp(LHS
->getDesc(), RHS
->getDesc()) < 0;
95 // Print information when destroyed, iff command line option is specified.
96 StatisticInfo::~StatisticInfo() {
97 llvm::PrintStatistics();
100 void llvm::EnableStatistics() {
101 Enabled
.setValue(true);
104 bool llvm::AreStatisticsEnabled() {
108 void llvm::PrintStatistics(raw_ostream
&OS
) {
109 StatisticInfo
&Stats
= *StatInfo
;
111 // Figure out how long the biggest Value and Name fields are.
112 unsigned MaxNameLen
= 0, MaxValLen
= 0;
113 for (size_t i
= 0, e
= Stats
.Stats
.size(); i
!= e
; ++i
) {
114 MaxValLen
= std::max(MaxValLen
,
115 (unsigned)utostr(Stats
.Stats
[i
]->getValue()).size());
116 MaxNameLen
= std::max(MaxNameLen
,
117 (unsigned)std::strlen(Stats
.Stats
[i
]->getName()));
120 // Sort the fields by name.
121 std::stable_sort(Stats
.Stats
.begin(), Stats
.Stats
.end(), NameCompare());
123 // Print out the statistics header...
124 OS
<< "===" << std::string(73, '-') << "===\n"
125 << " ... Statistics Collected ...\n"
126 << "===" << std::string(73, '-') << "===\n\n";
128 // Print all of the statistics.
129 for (size_t i
= 0, e
= Stats
.Stats
.size(); i
!= e
; ++i
) {
130 std::string CountStr
= utostr(Stats
.Stats
[i
]->getValue());
131 OS
<< std::string(MaxValLen
-CountStr
.size(), ' ')
132 << CountStr
<< " " << Stats
.Stats
[i
]->getName()
133 << std::string(MaxNameLen
-std::strlen(Stats
.Stats
[i
]->getName()), ' ')
134 << " - " << Stats
.Stats
[i
]->getDesc() << "\n";
137 OS
<< '\n'; // Flush the output stream.
142 void llvm::PrintStatistics() {
143 StatisticInfo
&Stats
= *StatInfo
;
145 // Statistics not enabled?
146 if (Stats
.Stats
.empty()) return;
148 // Get the stream to write to.
149 raw_ostream
&OutStream
= *CreateInfoOutputFile();
150 PrintStatistics(OutStream
);
151 delete &OutStream
; // Close the file.