blob: 0c50dfd27d61528f1793dfb262fa29673e2e707c [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"
Peter Collingbourne4cfa0862015-08-18 22:31:24 +000027#include "llvm/Support/Compiler.h"
David Greeneb28b1ed2010-01-05 01:28:47 +000028#include "llvm/Support/Debug.h"
Benjamin Kramercc863b22011-10-16 16:30:34 +000029#include "llvm/Support/Format.h"
Chris Lattner8c9969a2006-12-08 20:00:42 +000030#include "llvm/Support/ManagedStatic.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000031#include "llvm/Support/Mutex.h"
Matthias Braundb39fd62016-11-18 19:43:24 +000032#include "llvm/Support/Timer.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000033#include "llvm/Support/raw_ostream.h"
Matthias Braundb39fd62016-11-18 19:43:24 +000034#include "llvm/Support/YAMLTraits.h"
Chris Lattnerafa3ec42003-08-13 21:32:37 +000035#include <algorithm>
Anton Korobeynikov579f0712008-02-20 11:08:44 +000036#include <cstring>
Chris Lattnerdd978ce2003-12-14 21:27:33 +000037using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +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///
Matthias Braunc6035512016-09-26 18:38:07 +000042static cl::opt<bool> Stats("stats",
Jan Wen Voung7857a642013-03-08 22:56:31 +000043 cl::desc("Enable statistics output from program (available with Asserts)"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000044
Chris Lattnerc758fe62002-10-01 22:35:45 +000045
Matthias Braun98ea88b2016-06-15 20:19:16 +000046static cl::opt<bool> StatsAsJSON("stats-json",
47 cl::desc("Display statistics as json data"));
48
Matthias Braunc6035512016-09-26 18:38:07 +000049static bool Enabled;
Matthias Braun5391ffb2016-09-27 19:38:55 +000050static bool PrintOnExit;
Matthias Braunc6035512016-09-26 18:38:07 +000051
Chris Lattner8c9969a2006-12-08 20:00:42 +000052namespace {
53/// StatisticInfo - This class is used in a ManagedStatic so that it is created
Jim Grosbacha9277362010-08-17 17:37:22 +000054/// on demand (when the first statistic is bumped) and destroyed only when
Chris Lattner8c9969a2006-12-08 20:00:42 +000055/// llvm_shutdown is called. We print statistics from the destructor.
56class StatisticInfo {
Chris Lattner00bb2162006-12-19 23:17:40 +000057 std::vector<const Statistic*> Stats;
Douglas Gregor83fd0152010-03-30 17:32:08 +000058 friend void llvm::PrintStatistics();
59 friend void llvm::PrintStatistics(raw_ostream &OS);
Matthias Braun98ea88b2016-06-15 20:19:16 +000060 friend void llvm::PrintStatisticsJSON(raw_ostream &OS);
61
62 /// Sort statistics by debugtype,name,description.
63 void sort();
Chris Lattner8c9969a2006-12-08 20:00:42 +000064public:
Matthias Braundb39fd62016-11-18 19:43:24 +000065 StatisticInfo();
Chris Lattner8c9969a2006-12-08 20:00:42 +000066 ~StatisticInfo();
Jim Grosbacha9277362010-08-17 17:37:22 +000067
Chris Lattner00bb2162006-12-19 23:17:40 +000068 void addStatistic(const Statistic *S) {
Chris Lattner8c9969a2006-12-08 20:00:42 +000069 Stats.push_back(S);
Chris Lattnerc758fe62002-10-01 22:35:45 +000070 }
Chris Lattner8c9969a2006-12-08 20:00:42 +000071};
Alexander Kornienkof00654e2015-06-23 09:49:53 +000072}
Chris Lattnerc758fe62002-10-01 22:35:45 +000073
Chris Lattner8c9969a2006-12-08 20:00:42 +000074static ManagedStatic<StatisticInfo> StatInfo;
Torok Edwin819d15c2009-09-27 11:08:03 +000075static ManagedStatic<sys::SmartMutex<true> > StatLock;
Chris Lattner8c9969a2006-12-08 20:00:42 +000076
77/// RegisterStatistic - The first time a statistic is bumped, this method is
78/// called.
Chris Lattner00bb2162006-12-19 23:17:40 +000079void Statistic::RegisterStatistic() {
Chris Lattner8c9969a2006-12-08 20:00:42 +000080 // If stats are enabled, inform StatInfo that this statistic should be
81 // printed.
Torok Edwin819d15c2009-09-27 11:08:03 +000082 sys::SmartScopedLock<true> Writer(*StatLock);
Owen Andersonca8f9862009-06-23 21:19:38 +000083 if (!Initialized) {
Matthias Braunc6035512016-09-26 18:38:07 +000084 if (Stats || Enabled)
Owen Andersonca8f9862009-06-23 21:19:38 +000085 StatInfo->addStatistic(this);
Jim Grosbacha9277362010-08-17 17:37:22 +000086
Nick Lewycky897a57e2011-12-05 23:07:05 +000087 TsanHappensBefore(this);
Benjamin Kramer17388a62014-03-03 18:02:34 +000088 sys::MemoryFence();
Owen Andersonca8f9862009-06-23 21:19:38 +000089 // Remember we have been registered.
Nick Lewycky897a57e2011-12-05 23:07:05 +000090 TsanIgnoreWritesBegin();
Owen Andersonca8f9862009-06-23 21:19:38 +000091 Initialized = true;
Nick Lewycky897a57e2011-12-05 23:07:05 +000092 TsanIgnoreWritesEnd();
Owen Andersonca8f9862009-06-23 21:19:38 +000093 }
Chris Lattner8c9969a2006-12-08 20:00:42 +000094}
95
Matthias Braundb39fd62016-11-18 19:43:24 +000096StatisticInfo::StatisticInfo() {
97 // Ensure timergroup lists are created first so they are destructed after us.
98 TimerGroup::ConstructTimerLists();
99}
100
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000101// Print information when destroyed, iff command line option is specified.
Chris Lattner8c9969a2006-12-08 20:00:42 +0000102StatisticInfo::~StatisticInfo() {
Matthias Braun5391ffb2016-09-27 19:38:55 +0000103 if (::Stats || PrintOnExit)
Matthias Braunc6035512016-09-26 18:38:07 +0000104 llvm::PrintStatistics();
Douglas Gregor83fd0152010-03-30 17:32:08 +0000105}
Chris Lattnerc758fe62002-10-01 22:35:45 +0000106
Matthias Braun5391ffb2016-09-27 19:38:55 +0000107void llvm::EnableStatistics(bool PrintOnExit) {
Matthias Braunc6035512016-09-26 18:38:07 +0000108 Enabled = true;
Matthias Braun5391ffb2016-09-27 19:38:55 +0000109 ::PrintOnExit = PrintOnExit;
Douglas Gregor83fd0152010-03-30 17:32:08 +0000110}
111
Daniel Dunbar06dfe8e2011-02-26 23:17:12 +0000112bool llvm::AreStatisticsEnabled() {
Matthias Braunc6035512016-09-26 18:38:07 +0000113 return Enabled || Stats;
Daniel Dunbar06dfe8e2011-02-26 23:17:12 +0000114}
115
Matthias Braun98ea88b2016-06-15 20:19:16 +0000116void StatisticInfo::sort() {
117 std::stable_sort(Stats.begin(), Stats.end(),
118 [](const Statistic *LHS, const Statistic *RHS) {
119 if (int Cmp = std::strcmp(LHS->getDebugType(), RHS->getDebugType()))
120 return Cmp < 0;
121
122 if (int Cmp = std::strcmp(LHS->getName(), RHS->getName()))
123 return Cmp < 0;
124
125 return std::strcmp(LHS->getDesc(), RHS->getDesc()) < 0;
126 });
127}
128
Douglas Gregor83fd0152010-03-30 17:32:08 +0000129void llvm::PrintStatistics(raw_ostream &OS) {
130 StatisticInfo &Stats = *StatInfo;
Chris Lattnerc758fe62002-10-01 22:35:45 +0000131
Chris Lattner8c9969a2006-12-08 20:00:42 +0000132 // Figure out how long the biggest Value and Name fields are.
Matthias Braun98ea88b2016-06-15 20:19:16 +0000133 unsigned MaxDebugTypeLen = 0, MaxValLen = 0;
Douglas Gregor83fd0152010-03-30 17:32:08 +0000134 for (size_t i = 0, e = Stats.Stats.size(); i != e; ++i) {
Chris Lattner8c9969a2006-12-08 20:00:42 +0000135 MaxValLen = std::max(MaxValLen,
Douglas Gregor83fd0152010-03-30 17:32:08 +0000136 (unsigned)utostr(Stats.Stats[i]->getValue()).size());
Matthias Braun98ea88b2016-06-15 20:19:16 +0000137 MaxDebugTypeLen = std::max(MaxDebugTypeLen,
138 (unsigned)std::strlen(Stats.Stats[i]->getDebugType()));
Chris Lattnerc758fe62002-10-01 22:35:45 +0000139 }
Jim Grosbacha9277362010-08-17 17:37:22 +0000140
Matthias Braun98ea88b2016-06-15 20:19:16 +0000141 Stats.sort();
Chris Lattnerc758fe62002-10-01 22:35:45 +0000142
Chris Lattner8c9969a2006-12-08 20:00:42 +0000143 // Print out the statistics header...
Douglas Gregor83fd0152010-03-30 17:32:08 +0000144 OS << "===" << std::string(73, '-') << "===\n"
145 << " ... Statistics Collected ...\n"
146 << "===" << std::string(73, '-') << "===\n\n";
Jim Grosbacha9277362010-08-17 17:37:22 +0000147
Chris Lattner8c9969a2006-12-08 20:00:42 +0000148 // Print all of the statistics.
Benjamin Kramercc863b22011-10-16 16:30:34 +0000149 for (size_t i = 0, e = Stats.Stats.size(); i != e; ++i)
150 OS << format("%*u %-*s - %s\n",
151 MaxValLen, Stats.Stats[i]->getValue(),
Matthias Braun98ea88b2016-06-15 20:19:16 +0000152 MaxDebugTypeLen, Stats.Stats[i]->getDebugType(),
Benjamin Kramercc863b22011-10-16 16:30:34 +0000153 Stats.Stats[i]->getDesc());
Jim Grosbacha9277362010-08-17 17:37:22 +0000154
Douglas Gregor83fd0152010-03-30 17:32:08 +0000155 OS << '\n'; // Flush the output stream.
156 OS.flush();
Matthias Braun98ea88b2016-06-15 20:19:16 +0000157}
Douglas Gregor83fd0152010-03-30 17:32:08 +0000158
Matthias Braun98ea88b2016-06-15 20:19:16 +0000159void llvm::PrintStatisticsJSON(raw_ostream &OS) {
160 StatisticInfo &Stats = *StatInfo;
161
162 Stats.sort();
163
164 // Print all of the statistics.
165 OS << "{\n";
166 const char *delim = "";
167 for (const Statistic *Stat : Stats.Stats) {
168 OS << delim;
Matthias Braundb39fd62016-11-18 19:43:24 +0000169 assert(!yaml::needsQuotes(Stat->getDebugType()) &&
170 "Statistic group/type name is simple.");
171 assert(!yaml::needsQuotes(Stat->getName()) && "Statistic name is simple");
172 OS << "\t\"" << Stat->getDebugType() << '.' << Stat->getName() << "\": "
173 << Stat->getValue();
Matthias Braun98ea88b2016-06-15 20:19:16 +0000174 delim = ",\n";
175 }
Matthias Braundb39fd62016-11-18 19:43:24 +0000176 // Print timers.
177 TimerGroup::printAllJSONValues(OS, delim);
178
Matthias Braun98ea88b2016-06-15 20:19:16 +0000179 OS << "\n}\n";
180 OS.flush();
Douglas Gregor83fd0152010-03-30 17:32:08 +0000181}
182
183void llvm::PrintStatistics() {
Jan Wen Voung7857a642013-03-08 22:56:31 +0000184#if !defined(NDEBUG) || defined(LLVM_ENABLE_STATS)
Douglas Gregor83fd0152010-03-30 17:32:08 +0000185 StatisticInfo &Stats = *StatInfo;
186
187 // Statistics not enabled?
188 if (Stats.Stats.empty()) return;
189
190 // Get the stream to write to.
Rafael Espindolab94ab5f2015-12-16 22:28:34 +0000191 std::unique_ptr<raw_ostream> OutStream = CreateInfoOutputFile();
Matthias Braun98ea88b2016-06-15 20:19:16 +0000192 if (StatsAsJSON)
193 PrintStatisticsJSON(*OutStream);
194 else
195 PrintStatistics(*OutStream);
Rafael Espindolab94ab5f2015-12-16 22:28:34 +0000196
Jan Wen Voung7857a642013-03-08 22:56:31 +0000197#else
198 // Check if the -stats option is set instead of checking
199 // !Stats.Stats.empty(). In release builds, Statistics operators
200 // do nothing, so stats are never Registered.
Matthias Braunc6035512016-09-26 18:38:07 +0000201 if (Stats) {
Jan Wen Voung7857a642013-03-08 22:56:31 +0000202 // Get the stream to write to.
Rafael Espindolab94ab5f2015-12-16 22:28:34 +0000203 std::unique_ptr<raw_ostream> OutStream = CreateInfoOutputFile();
204 (*OutStream) << "Statistics are disabled. "
205 << "Build with asserts or with -DLLVM_ENABLE_STATS\n";
Jan Wen Voung7857a642013-03-08 22:56:31 +0000206 }
207#endif
Chris Lattner9eb00522002-05-10 15:36:46 +0000208}