blob: 56c3b0f5659fa716e7c43cc567b9ac4d5f4bdc7d [file] [log] [blame]
Chris Lattnerc758fe62002-10-01 22:35:45 +00001//===-- Statistic.cpp - Easy way to expose stats information --------------===//
Misha Brukman10468d82005-04-21 22:55:34 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Brukman10468d82005-04-21 22:55:34 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner9eb00522002-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 Lattner8c9969a2006-12-08 20:00:42 +000018// static Statistic NumInstEliminated("GCSE", "Number of instructions killed");
Chris Lattner9eb00522002-05-10 15:36:46 +000019//
20// Later, in the code: ++NumInstEliminated;
21//
22//===----------------------------------------------------------------------===//
23
Reid Spencer7c16caa2004-09-01 22:55:40 +000024#include "llvm/ADT/Statistic.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000025#include "llvm/ADT/StringExtras.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000026#include "llvm/Support/CommandLine.h"
David Greeneb28b1ed2010-01-05 01:28:47 +000027#include "llvm/Support/Debug.h"
Benjamin Kramercc863b22011-10-16 16:30:34 +000028#include "llvm/Support/Format.h"
Chris Lattner8c9969a2006-12-08 20:00:42 +000029#include "llvm/Support/ManagedStatic.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000030#include "llvm/Support/Mutex.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000031#include "llvm/Support/raw_ostream.h"
Chris Lattnerafa3ec42003-08-13 21:32:37 +000032#include <algorithm>
Anton Korobeynikov579f0712008-02-20 11:08:44 +000033#include <cstring>
Chris Lattnerdd978ce2003-12-14 21:27:33 +000034using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000035
Chris Lattner6cbd8d12010-03-30 05:01:08 +000036// CreateInfoOutputFile - Return a file stream to print our output on.
37namespace llvm { extern raw_ostream *CreateInfoOutputFile(); }
Chris Lattnerb0e59582003-05-09 20:05:44 +000038
Chris Lattner8c9969a2006-12-08 20:00:42 +000039/// -stats - Command line option to cause transformations to emit stats about
40/// what they did.
41///
Chris Lattnerf5cad152002-07-22 02:10:13 +000042static cl::opt<bool>
Jan Wen Voung7857a642013-03-08 22:56:31 +000043Enabled(
44 "stats",
45 cl::desc("Enable statistics output from program (available with Asserts)"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000046
Chris Lattnerc758fe62002-10-01 22:35:45 +000047
Chris Lattner8c9969a2006-12-08 20:00:42 +000048namespace {
49/// StatisticInfo - This class is used in a ManagedStatic so that it is created
Jim Grosbacha9277362010-08-17 17:37:22 +000050/// on demand (when the first statistic is bumped) and destroyed only when
Chris Lattner8c9969a2006-12-08 20:00:42 +000051/// llvm_shutdown is called. We print statistics from the destructor.
52class StatisticInfo {
Chris Lattner00bb2162006-12-19 23:17:40 +000053 std::vector<const Statistic*> Stats;
Douglas Gregor83fd0152010-03-30 17:32:08 +000054 friend void llvm::PrintStatistics();
55 friend void llvm::PrintStatistics(raw_ostream &OS);
Chris Lattner8c9969a2006-12-08 20:00:42 +000056public:
57 ~StatisticInfo();
Jim Grosbacha9277362010-08-17 17:37:22 +000058
Chris Lattner00bb2162006-12-19 23:17:40 +000059 void addStatistic(const Statistic *S) {
Chris Lattner8c9969a2006-12-08 20:00:42 +000060 Stats.push_back(S);
Chris Lattnerc758fe62002-10-01 22:35:45 +000061 }
Chris Lattner8c9969a2006-12-08 20:00:42 +000062};
63}
Chris Lattnerc758fe62002-10-01 22:35:45 +000064
Chris Lattner8c9969a2006-12-08 20:00:42 +000065static ManagedStatic<StatisticInfo> StatInfo;
Torok Edwin819d15c2009-09-27 11:08:03 +000066static ManagedStatic<sys::SmartMutex<true> > StatLock;
Chris Lattner8c9969a2006-12-08 20:00:42 +000067
68/// RegisterStatistic - The first time a statistic is bumped, this method is
69/// called.
Chris Lattner00bb2162006-12-19 23:17:40 +000070void Statistic::RegisterStatistic() {
Chris Lattner8c9969a2006-12-08 20:00:42 +000071 // If stats are enabled, inform StatInfo that this statistic should be
72 // printed.
Torok Edwin819d15c2009-09-27 11:08:03 +000073 sys::SmartScopedLock<true> Writer(*StatLock);
Owen Andersonca8f9862009-06-23 21:19:38 +000074 if (!Initialized) {
75 if (Enabled)
76 StatInfo->addStatistic(this);
Jim Grosbacha9277362010-08-17 17:37:22 +000077
Nick Lewycky897a57e2011-12-05 23:07:05 +000078 TsanHappensBefore(this);
Benjamin Kramer17388a62014-03-03 18:02:34 +000079 sys::MemoryFence();
Owen Andersonca8f9862009-06-23 21:19:38 +000080 // Remember we have been registered.
Nick Lewycky897a57e2011-12-05 23:07:05 +000081 TsanIgnoreWritesBegin();
Owen Andersonca8f9862009-06-23 21:19:38 +000082 Initialized = true;
Nick Lewycky897a57e2011-12-05 23:07:05 +000083 TsanIgnoreWritesEnd();
Owen Andersonca8f9862009-06-23 21:19:38 +000084 }
Chris Lattner8c9969a2006-12-08 20:00:42 +000085}
86
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +000087// Print information when destroyed, iff command line option is specified.
Chris Lattner8c9969a2006-12-08 20:00:42 +000088StatisticInfo::~StatisticInfo() {
Douglas Gregor83fd0152010-03-30 17:32:08 +000089 llvm::PrintStatistics();
90}
Chris Lattnerc758fe62002-10-01 22:35:45 +000091
Douglas Gregor83fd0152010-03-30 17:32:08 +000092void llvm::EnableStatistics() {
93 Enabled.setValue(true);
94}
95
Daniel Dunbar06dfe8e2011-02-26 23:17:12 +000096bool llvm::AreStatisticsEnabled() {
97 return Enabled;
98}
99
Douglas Gregor83fd0152010-03-30 17:32:08 +0000100void llvm::PrintStatistics(raw_ostream &OS) {
101 StatisticInfo &Stats = *StatInfo;
Chris Lattnerc758fe62002-10-01 22:35:45 +0000102
Chris Lattner8c9969a2006-12-08 20:00:42 +0000103 // Figure out how long the biggest Value and Name fields are.
104 unsigned MaxNameLen = 0, MaxValLen = 0;
Douglas Gregor83fd0152010-03-30 17:32:08 +0000105 for (size_t i = 0, e = Stats.Stats.size(); i != e; ++i) {
Chris Lattner8c9969a2006-12-08 20:00:42 +0000106 MaxValLen = std::max(MaxValLen,
Douglas Gregor83fd0152010-03-30 17:32:08 +0000107 (unsigned)utostr(Stats.Stats[i]->getValue()).size());
Chris Lattner8c9969a2006-12-08 20:00:42 +0000108 MaxNameLen = std::max(MaxNameLen,
Douglas Gregor83fd0152010-03-30 17:32:08 +0000109 (unsigned)std::strlen(Stats.Stats[i]->getName()));
Chris Lattnerc758fe62002-10-01 22:35:45 +0000110 }
Jim Grosbacha9277362010-08-17 17:37:22 +0000111
Chris Lattner8c9969a2006-12-08 20:00:42 +0000112 // Sort the fields by name.
Benjamin Kramer3a377bc2014-03-01 11:47:00 +0000113 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()))
116 return Cmp < 0;
117
118 // Secondary key is the description.
119 return std::strcmp(LHS->getDesc(), RHS->getDesc()) < 0;
120 });
Chris Lattnerc758fe62002-10-01 22:35:45 +0000121
Chris Lattner8c9969a2006-12-08 20:00:42 +0000122 // Print out the statistics header...
Douglas Gregor83fd0152010-03-30 17:32:08 +0000123 OS << "===" << std::string(73, '-') << "===\n"
124 << " ... Statistics Collected ...\n"
125 << "===" << std::string(73, '-') << "===\n\n";
Jim Grosbacha9277362010-08-17 17:37:22 +0000126
Chris Lattner8c9969a2006-12-08 20:00:42 +0000127 // Print all of the statistics.
Benjamin Kramercc863b22011-10-16 16:30:34 +0000128 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());
Jim Grosbacha9277362010-08-17 17:37:22 +0000133
Douglas Gregor83fd0152010-03-30 17:32:08 +0000134 OS << '\n'; // Flush the output stream.
135 OS.flush();
136
137}
138
139void llvm::PrintStatistics() {
Jan Wen Voung7857a642013-03-08 22:56:31 +0000140#if !defined(NDEBUG) || defined(LLVM_ENABLE_STATS)
Douglas Gregor83fd0152010-03-30 17:32:08 +0000141 StatisticInfo &Stats = *StatInfo;
142
143 // Statistics not enabled?
144 if (Stats.Stats.empty()) return;
145
146 // Get the stream to write to.
147 raw_ostream &OutStream = *CreateInfoOutputFile();
148 PrintStatistics(OutStream);
Chris Lattner6cbd8d12010-03-30 05:01:08 +0000149 delete &OutStream; // Close the file.
Jan Wen Voung7857a642013-03-08 22:56:31 +0000150#else
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.
154 if (Enabled) {
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";
159 OutStream.flush();
160 delete &OutStream; // Close the file.
161 }
162#endif
Chris Lattner9eb00522002-05-10 15:36:46 +0000163}