blob: 9c28176b730e954dd10a1a4422423359d2dc2b0f [file] [log] [blame]
Chris Lattner96ef1b92002-10-01 22:35:45 +00001//===-- Statistic.cpp - Easy way to expose stats information --------------===//
Misha Brukmanf976c852005-04-21 22:55:34 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanf976c852005-04-21 22:55:34 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnerf3f4fd52002-05-10 15:36:46 +00009//
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//
Chris Lattner975f0582006-12-08 20:00:42 +000018// static Statistic NumInstEliminated("GCSE", "Number of instructions killed");
Chris Lattnerf3f4fd52002-05-10 15:36:46 +000019//
20// Later, in the code: ++NumInstEliminated;
21//
22//===----------------------------------------------------------------------===//
23
Reid Spencer551ccae2004-09-01 22:55:40 +000024#include "llvm/ADT/Statistic.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000025#include "llvm/ADT/StringExtras.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000026#include "llvm/Support/CommandLine.h"
David Greene8061a442010-01-05 01:28:47 +000027#include "llvm/Support/Debug.h"
Benjamin Kramer962bad72011-10-16 16:30:34 +000028#include "llvm/Support/Format.h"
Chris Lattner975f0582006-12-08 20:00:42 +000029#include "llvm/Support/ManagedStatic.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000030#include "llvm/Support/Mutex.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000031#include "llvm/Support/raw_ostream.h"
Chris Lattnera4ca41e2003-08-13 21:32:37 +000032#include <algorithm>
Anton Korobeynikovae9f3a32008-02-20 11:08:44 +000033#include <cstring>
Chris Lattnerb6d465f2003-12-14 21:27:33 +000034using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000035
Chris Lattner49a2bb22010-03-30 05:01:08 +000036// CreateInfoOutputFile - Return a file stream to print our output on.
37namespace llvm { extern raw_ostream *CreateInfoOutputFile(); }
Chris Lattnerf205fec2003-05-09 20:05:44 +000038
Chris Lattner975f0582006-12-08 20:00:42 +000039/// -stats - Command line option to cause transformations to emit stats about
40/// what they did.
41///
Chris Lattner5ff62e92002-07-22 02:10:13 +000042static cl::opt<bool>
Jan Wen Voungfa785cb2013-03-08 22:56:31 +000043Enabled(
44 "stats",
45 cl::desc("Enable statistics output from program (available with Asserts)"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000046
Chris Lattner96ef1b92002-10-01 22:35:45 +000047
Chris Lattner975f0582006-12-08 20:00:42 +000048namespace {
49/// StatisticInfo - This class is used in a ManagedStatic so that it is created
Jim Grosbach37e59c02010-08-17 17:37:22 +000050/// on demand (when the first statistic is bumped) and destroyed only when
Chris Lattner975f0582006-12-08 20:00:42 +000051/// llvm_shutdown is called. We print statistics from the destructor.
52class StatisticInfo {
Chris Lattner0a361522006-12-19 23:17:40 +000053 std::vector<const Statistic*> Stats;
Douglas Gregor6ce57922010-03-30 17:32:08 +000054 friend void llvm::PrintStatistics();
55 friend void llvm::PrintStatistics(raw_ostream &OS);
Chris Lattner975f0582006-12-08 20:00:42 +000056public:
57 ~StatisticInfo();
Jim Grosbach37e59c02010-08-17 17:37:22 +000058
Chris Lattner0a361522006-12-19 23:17:40 +000059 void addStatistic(const Statistic *S) {
Chris Lattner975f0582006-12-08 20:00:42 +000060 Stats.push_back(S);
Chris Lattner96ef1b92002-10-01 22:35:45 +000061 }
Chris Lattner975f0582006-12-08 20:00:42 +000062};
63}
Chris Lattner96ef1b92002-10-01 22:35:45 +000064
Chris Lattner975f0582006-12-08 20:00:42 +000065static ManagedStatic<StatisticInfo> StatInfo;
Torok Edwind7830d52009-09-27 11:08:03 +000066static ManagedStatic<sys::SmartMutex<true> > StatLock;
Chris Lattner975f0582006-12-08 20:00:42 +000067
68/// RegisterStatistic - The first time a statistic is bumped, this method is
69/// called.
Chris Lattner0a361522006-12-19 23:17:40 +000070void Statistic::RegisterStatistic() {
Chris Lattner975f0582006-12-08 20:00:42 +000071 // If stats are enabled, inform StatInfo that this statistic should be
72 // printed.
Torok Edwind7830d52009-09-27 11:08:03 +000073 sys::SmartScopedLock<true> Writer(*StatLock);
Owen Anderson92915e32009-06-23 21:19:38 +000074 if (!Initialized) {
75 if (Enabled)
76 StatInfo->addStatistic(this);
Jim Grosbach37e59c02010-08-17 17:37:22 +000077
Nick Lewyckyaa21e412011-12-05 23:07:05 +000078 TsanHappensBefore(this);
Owen Anderson92915e32009-06-23 21:19:38 +000079 sys::MemoryFence();
80 // Remember we have been registered.
Nick Lewyckyaa21e412011-12-05 23:07:05 +000081 TsanIgnoreWritesBegin();
Owen Anderson92915e32009-06-23 21:19:38 +000082 Initialized = true;
Nick Lewyckyaa21e412011-12-05 23:07:05 +000083 TsanIgnoreWritesEnd();
Owen Anderson92915e32009-06-23 21:19:38 +000084 }
Chris Lattner975f0582006-12-08 20:00:42 +000085}
86
Dan Gohman844731a2008-05-13 00:00:25 +000087namespace {
88
Chris Lattner975f0582006-12-08 20:00:42 +000089struct NameCompare {
Chris Lattner0a361522006-12-19 23:17:40 +000090 bool operator()(const Statistic *LHS, const Statistic *RHS) const {
Chris Lattner975f0582006-12-08 20:00:42 +000091 int Cmp = std::strcmp(LHS->getName(), RHS->getName());
92 if (Cmp != 0) return Cmp < 0;
Jim Grosbach37e59c02010-08-17 17:37:22 +000093
Chris Lattner975f0582006-12-08 20:00:42 +000094 // Secondary key is the description.
95 return std::strcmp(LHS->getDesc(), RHS->getDesc()) < 0;
Chris Lattner96ef1b92002-10-01 22:35:45 +000096 }
97};
98
Dan Gohman844731a2008-05-13 00:00:25 +000099}
100
Sylvestre Ledru94c22712012-09-27 10:14:43 +0000101// Print information when destroyed, iff command line option is specified.
Chris Lattner975f0582006-12-08 20:00:42 +0000102StatisticInfo::~StatisticInfo() {
Douglas Gregor6ce57922010-03-30 17:32:08 +0000103 llvm::PrintStatistics();
104}
Chris Lattner96ef1b92002-10-01 22:35:45 +0000105
Douglas Gregor6ce57922010-03-30 17:32:08 +0000106void llvm::EnableStatistics() {
107 Enabled.setValue(true);
108}
109
Daniel Dunbar0aa00f92011-02-26 23:17:12 +0000110bool llvm::AreStatisticsEnabled() {
111 return Enabled;
112}
113
Douglas Gregor6ce57922010-03-30 17:32:08 +0000114void llvm::PrintStatistics(raw_ostream &OS) {
115 StatisticInfo &Stats = *StatInfo;
Chris Lattner96ef1b92002-10-01 22:35:45 +0000116
Chris Lattner975f0582006-12-08 20:00:42 +0000117 // Figure out how long the biggest Value and Name fields are.
118 unsigned MaxNameLen = 0, MaxValLen = 0;
Douglas Gregor6ce57922010-03-30 17:32:08 +0000119 for (size_t i = 0, e = Stats.Stats.size(); i != e; ++i) {
Chris Lattner975f0582006-12-08 20:00:42 +0000120 MaxValLen = std::max(MaxValLen,
Douglas Gregor6ce57922010-03-30 17:32:08 +0000121 (unsigned)utostr(Stats.Stats[i]->getValue()).size());
Chris Lattner975f0582006-12-08 20:00:42 +0000122 MaxNameLen = std::max(MaxNameLen,
Douglas Gregor6ce57922010-03-30 17:32:08 +0000123 (unsigned)std::strlen(Stats.Stats[i]->getName()));
Chris Lattner96ef1b92002-10-01 22:35:45 +0000124 }
Jim Grosbach37e59c02010-08-17 17:37:22 +0000125
Chris Lattner975f0582006-12-08 20:00:42 +0000126 // Sort the fields by name.
Douglas Gregor6ce57922010-03-30 17:32:08 +0000127 std::stable_sort(Stats.Stats.begin(), Stats.Stats.end(), NameCompare());
Chris Lattner96ef1b92002-10-01 22:35:45 +0000128
Chris Lattner975f0582006-12-08 20:00:42 +0000129 // Print out the statistics header...
Douglas Gregor6ce57922010-03-30 17:32:08 +0000130 OS << "===" << std::string(73, '-') << "===\n"
131 << " ... Statistics Collected ...\n"
132 << "===" << std::string(73, '-') << "===\n\n";
Jim Grosbach37e59c02010-08-17 17:37:22 +0000133
Chris Lattner975f0582006-12-08 20:00:42 +0000134 // Print all of the statistics.
Benjamin Kramer962bad72011-10-16 16:30:34 +0000135 for (size_t i = 0, e = Stats.Stats.size(); i != e; ++i)
136 OS << format("%*u %-*s - %s\n",
137 MaxValLen, Stats.Stats[i]->getValue(),
138 MaxNameLen, Stats.Stats[i]->getName(),
139 Stats.Stats[i]->getDesc());
Jim Grosbach37e59c02010-08-17 17:37:22 +0000140
Douglas Gregor6ce57922010-03-30 17:32:08 +0000141 OS << '\n'; // Flush the output stream.
142 OS.flush();
143
144}
145
146void llvm::PrintStatistics() {
Jan Wen Voungfa785cb2013-03-08 22:56:31 +0000147#if !defined(NDEBUG) || defined(LLVM_ENABLE_STATS)
Douglas Gregor6ce57922010-03-30 17:32:08 +0000148 StatisticInfo &Stats = *StatInfo;
149
150 // Statistics not enabled?
151 if (Stats.Stats.empty()) return;
152
153 // Get the stream to write to.
154 raw_ostream &OutStream = *CreateInfoOutputFile();
155 PrintStatistics(OutStream);
Chris Lattner49a2bb22010-03-30 05:01:08 +0000156 delete &OutStream; // Close the file.
Jan Wen Voungfa785cb2013-03-08 22:56:31 +0000157#else
158 // Check if the -stats option is set instead of checking
159 // !Stats.Stats.empty(). In release builds, Statistics operators
160 // do nothing, so stats are never Registered.
161 if (Enabled) {
162 // Get the stream to write to.
163 raw_ostream &OutStream = *CreateInfoOutputFile();
164 OutStream << "Statistics are disabled. "
165 << "Build with asserts or with -DLLVM_ENABLE_STATS\n";
166 OutStream.flush();
167 delete &OutStream; // Close the file.
168 }
169#endif
Chris Lattnerf3f4fd52002-05-10 15:36:46 +0000170}