blob: d57300a75d1d24b3ac647288a392b03f2314558a [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"
Matthias Braundb39fd62016-11-18 19:43:24 +000033#include "llvm/Support/YAMLTraits.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000034#include "llvm/Support/raw_ostream.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///
Zachary Turner8065f0b2017-12-01 00:53:10 +000042static cl::opt<bool> Stats(
43 "stats",
44 cl::desc("Enable statistics output from program (available with Asserts)"),
45 cl::Hidden);
Chris Lattnerc758fe62002-10-01 22:35:45 +000046
Matthias Braun98ea88b2016-06-15 20:19:16 +000047static cl::opt<bool> StatsAsJSON("stats-json",
Zachary Turner8065f0b2017-12-01 00:53:10 +000048 cl::desc("Display statistics as json data"),
49 cl::Hidden);
Matthias Braun98ea88b2016-06-15 20:19:16 +000050
Matthias Braunc6035512016-09-26 18:38:07 +000051static bool Enabled;
Matthias Braun5391ffb2016-09-27 19:38:55 +000052static bool PrintOnExit;
Matthias Braunc6035512016-09-26 18:38:07 +000053
Chris Lattner8c9969a2006-12-08 20:00:42 +000054namespace {
Daniel Sandersa09751e2018-03-05 19:38:16 +000055/// This class is used in a ManagedStatic so that it is created on demand (when
56/// the first statistic is bumped) and destroyed only when llvm_shutdown is
57/// called. We print statistics from the destructor.
58/// This class is also used to look up statistic values from applications that
59/// use LLVM.
Chris Lattner8c9969a2006-12-08 20:00:42 +000060class StatisticInfo {
Daniel Sanders1087a542018-03-08 02:36:25 +000061 std::vector<Statistic*> Stats;
Daniel Sandersa09751e2018-03-05 19:38:16 +000062
Douglas Gregor83fd0152010-03-30 17:32:08 +000063 friend void llvm::PrintStatistics();
64 friend void llvm::PrintStatistics(raw_ostream &OS);
Matthias Braun98ea88b2016-06-15 20:19:16 +000065 friend void llvm::PrintStatisticsJSON(raw_ostream &OS);
66
67 /// Sort statistics by debugtype,name,description.
68 void sort();
Chris Lattner8c9969a2006-12-08 20:00:42 +000069public:
Daniel Sanders1087a542018-03-08 02:36:25 +000070 using const_iterator = std::vector<Statistic *>::const_iterator;
Daniel Sandersa09751e2018-03-05 19:38:16 +000071
Matthias Braundb39fd62016-11-18 19:43:24 +000072 StatisticInfo();
Chris Lattner8c9969a2006-12-08 20:00:42 +000073 ~StatisticInfo();
Jim Grosbacha9277362010-08-17 17:37:22 +000074
Daniel Sanders1087a542018-03-08 02:36:25 +000075 void addStatistic(Statistic *S) {
Chris Lattner8c9969a2006-12-08 20:00:42 +000076 Stats.push_back(S);
Chris Lattnerc758fe62002-10-01 22:35:45 +000077 }
Daniel Sandersa09751e2018-03-05 19:38:16 +000078
79 const_iterator begin() const { return Stats.begin(); }
80 const_iterator end() const { return Stats.end(); }
81 iterator_range<const_iterator> statistics() const {
82 return {begin(), end()};
83 }
Daniel Sanders1087a542018-03-08 02:36:25 +000084
85 void reset();
Chris Lattner8c9969a2006-12-08 20:00:42 +000086};
Daniel Sandersa09751e2018-03-05 19:38:16 +000087} // end anonymous namespace
Chris Lattnerc758fe62002-10-01 22:35:45 +000088
Chris Lattner8c9969a2006-12-08 20:00:42 +000089static ManagedStatic<StatisticInfo> StatInfo;
Torok Edwin819d15c2009-09-27 11:08:03 +000090static ManagedStatic<sys::SmartMutex<true> > StatLock;
Chris Lattner8c9969a2006-12-08 20:00:42 +000091
92/// RegisterStatistic - The first time a statistic is bumped, this method is
93/// called.
Chris Lattner00bb2162006-12-19 23:17:40 +000094void Statistic::RegisterStatistic() {
Chris Lattner8c9969a2006-12-08 20:00:42 +000095 // If stats are enabled, inform StatInfo that this statistic should be
96 // printed.
Bob Haarman37a92692018-04-17 23:37:18 +000097 // llvm_shutdown calls destructors while holding the ManagedStatic mutex.
98 // These destructors end up calling PrintStatistics, which takes StatLock.
99 // Since dereferencing StatInfo and StatLock can require taking the
100 // ManagedStatic mutex, doing so with StatLock held would lead to a lock
101 // order inversion. To avoid that, we dereference the ManagedStatics first,
102 // and only take StatLock afterwards.
Benjamin Kramer351e9f32018-02-01 20:28:33 +0000103 if (!Initialized.load(std::memory_order_relaxed)) {
Bob Haarman37a92692018-04-17 23:37:18 +0000104 sys::SmartMutex<true> &Lock = *StatLock;
105 StatisticInfo &SI = *StatInfo;
106 sys::SmartScopedLock<true> Writer(Lock);
107 // Check Initialized again after acquiring the lock.
108 if (Initialized.load(std::memory_order_relaxed))
109 return;
Matthias Braunc6035512016-09-26 18:38:07 +0000110 if (Stats || Enabled)
Bob Haarman37a92692018-04-17 23:37:18 +0000111 SI.addStatistic(this);
Jim Grosbacha9277362010-08-17 17:37:22 +0000112
Owen Andersonca8f9862009-06-23 21:19:38 +0000113 // Remember we have been registered.
Benjamin Kramer351e9f32018-02-01 20:28:33 +0000114 Initialized.store(true, std::memory_order_release);
Owen Andersonca8f9862009-06-23 21:19:38 +0000115 }
Chris Lattner8c9969a2006-12-08 20:00:42 +0000116}
117
Matthias Braundb39fd62016-11-18 19:43:24 +0000118StatisticInfo::StatisticInfo() {
119 // Ensure timergroup lists are created first so they are destructed after us.
120 TimerGroup::ConstructTimerLists();
121}
122
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000123// Print information when destroyed, iff command line option is specified.
Chris Lattner8c9969a2006-12-08 20:00:42 +0000124StatisticInfo::~StatisticInfo() {
Matthias Braun5391ffb2016-09-27 19:38:55 +0000125 if (::Stats || PrintOnExit)
Matthias Braunc6035512016-09-26 18:38:07 +0000126 llvm::PrintStatistics();
Douglas Gregor83fd0152010-03-30 17:32:08 +0000127}
Chris Lattnerc758fe62002-10-01 22:35:45 +0000128
Matthias Braun5391ffb2016-09-27 19:38:55 +0000129void llvm::EnableStatistics(bool PrintOnExit) {
Matthias Braunc6035512016-09-26 18:38:07 +0000130 Enabled = true;
Matthias Braun5391ffb2016-09-27 19:38:55 +0000131 ::PrintOnExit = PrintOnExit;
Douglas Gregor83fd0152010-03-30 17:32:08 +0000132}
133
Daniel Dunbar06dfe8e2011-02-26 23:17:12 +0000134bool llvm::AreStatisticsEnabled() {
Matthias Braunc6035512016-09-26 18:38:07 +0000135 return Enabled || Stats;
Daniel Dunbar06dfe8e2011-02-26 23:17:12 +0000136}
137
Matthias Braun98ea88b2016-06-15 20:19:16 +0000138void StatisticInfo::sort() {
139 std::stable_sort(Stats.begin(), Stats.end(),
140 [](const Statistic *LHS, const Statistic *RHS) {
141 if (int Cmp = std::strcmp(LHS->getDebugType(), RHS->getDebugType()))
142 return Cmp < 0;
143
144 if (int Cmp = std::strcmp(LHS->getName(), RHS->getName()))
145 return Cmp < 0;
146
147 return std::strcmp(LHS->getDesc(), RHS->getDesc()) < 0;
148 });
149}
150
Daniel Sanders1087a542018-03-08 02:36:25 +0000151void StatisticInfo::reset() {
152 sys::SmartScopedLock<true> Writer(*StatLock);
153
154 // Tell each statistic that it isn't registered so it has to register
155 // again. We're holding the lock so it won't be able to do so until we're
156 // finished. Once we've forced it to re-register (after we return), then zero
157 // the value.
158 for (auto *Stat : Stats) {
159 // Value updates to a statistic that complete before this statement in the
160 // iteration for that statistic will be lost as intended.
161 Stat->Initialized = false;
162 Stat->Value = 0;
163 }
164
165 // Clear the registration list and release the lock once we're done. Any
166 // pending updates from other threads will safely take effect after we return.
167 // That might not be what the user wants if they're measuring a compilation
168 // but it's their responsibility to prevent concurrent compilations to make
169 // a single compilation measurable.
170 Stats.clear();
171}
172
Douglas Gregor83fd0152010-03-30 17:32:08 +0000173void llvm::PrintStatistics(raw_ostream &OS) {
174 StatisticInfo &Stats = *StatInfo;
Chris Lattnerc758fe62002-10-01 22:35:45 +0000175
Chris Lattner8c9969a2006-12-08 20:00:42 +0000176 // Figure out how long the biggest Value and Name fields are.
Matthias Braun98ea88b2016-06-15 20:19:16 +0000177 unsigned MaxDebugTypeLen = 0, MaxValLen = 0;
Douglas Gregor83fd0152010-03-30 17:32:08 +0000178 for (size_t i = 0, e = Stats.Stats.size(); i != e; ++i) {
Chris Lattner8c9969a2006-12-08 20:00:42 +0000179 MaxValLen = std::max(MaxValLen,
Douglas Gregor83fd0152010-03-30 17:32:08 +0000180 (unsigned)utostr(Stats.Stats[i]->getValue()).size());
Matthias Braun98ea88b2016-06-15 20:19:16 +0000181 MaxDebugTypeLen = std::max(MaxDebugTypeLen,
182 (unsigned)std::strlen(Stats.Stats[i]->getDebugType()));
Chris Lattnerc758fe62002-10-01 22:35:45 +0000183 }
Jim Grosbacha9277362010-08-17 17:37:22 +0000184
Matthias Braun98ea88b2016-06-15 20:19:16 +0000185 Stats.sort();
Chris Lattnerc758fe62002-10-01 22:35:45 +0000186
Chris Lattner8c9969a2006-12-08 20:00:42 +0000187 // Print out the statistics header...
Douglas Gregor83fd0152010-03-30 17:32:08 +0000188 OS << "===" << std::string(73, '-') << "===\n"
189 << " ... Statistics Collected ...\n"
190 << "===" << std::string(73, '-') << "===\n\n";
Jim Grosbacha9277362010-08-17 17:37:22 +0000191
Chris Lattner8c9969a2006-12-08 20:00:42 +0000192 // Print all of the statistics.
Benjamin Kramercc863b22011-10-16 16:30:34 +0000193 for (size_t i = 0, e = Stats.Stats.size(); i != e; ++i)
194 OS << format("%*u %-*s - %s\n",
195 MaxValLen, Stats.Stats[i]->getValue(),
Matthias Braun98ea88b2016-06-15 20:19:16 +0000196 MaxDebugTypeLen, Stats.Stats[i]->getDebugType(),
Benjamin Kramercc863b22011-10-16 16:30:34 +0000197 Stats.Stats[i]->getDesc());
Jim Grosbacha9277362010-08-17 17:37:22 +0000198
Douglas Gregor83fd0152010-03-30 17:32:08 +0000199 OS << '\n'; // Flush the output stream.
200 OS.flush();
Matthias Braun98ea88b2016-06-15 20:19:16 +0000201}
Douglas Gregor83fd0152010-03-30 17:32:08 +0000202
Matthias Braun98ea88b2016-06-15 20:19:16 +0000203void llvm::PrintStatisticsJSON(raw_ostream &OS) {
Daniel Sanders0f4b0152018-03-06 21:16:42 +0000204 sys::SmartScopedLock<true> Reader(*StatLock);
Matthias Braun98ea88b2016-06-15 20:19:16 +0000205 StatisticInfo &Stats = *StatInfo;
206
207 Stats.sort();
208
209 // Print all of the statistics.
210 OS << "{\n";
211 const char *delim = "";
212 for (const Statistic *Stat : Stats.Stats) {
213 OS << delim;
Francis Visoiu Mistrihb213b272017-12-18 17:38:03 +0000214 assert(yaml::needsQuotes(Stat->getDebugType()) == yaml::QuotingType::None &&
Matthias Braundb39fd62016-11-18 19:43:24 +0000215 "Statistic group/type name is simple.");
Francis Visoiu Mistrihb213b272017-12-18 17:38:03 +0000216 assert(yaml::needsQuotes(Stat->getName()) == yaml::QuotingType::None &&
217 "Statistic name is simple");
Matthias Braundb39fd62016-11-18 19:43:24 +0000218 OS << "\t\"" << Stat->getDebugType() << '.' << Stat->getName() << "\": "
219 << Stat->getValue();
Matthias Braun98ea88b2016-06-15 20:19:16 +0000220 delim = ",\n";
221 }
Matthias Braundb39fd62016-11-18 19:43:24 +0000222 // Print timers.
223 TimerGroup::printAllJSONValues(OS, delim);
224
Matthias Braun98ea88b2016-06-15 20:19:16 +0000225 OS << "\n}\n";
226 OS.flush();
Douglas Gregor83fd0152010-03-30 17:32:08 +0000227}
228
229void llvm::PrintStatistics() {
Daniel Sandersa09751e2018-03-05 19:38:16 +0000230#if LLVM_ENABLE_STATS
Daniel Sanders0f4b0152018-03-06 21:16:42 +0000231 sys::SmartScopedLock<true> Reader(*StatLock);
Douglas Gregor83fd0152010-03-30 17:32:08 +0000232 StatisticInfo &Stats = *StatInfo;
233
234 // Statistics not enabled?
235 if (Stats.Stats.empty()) return;
236
237 // Get the stream to write to.
Rafael Espindolab94ab5f2015-12-16 22:28:34 +0000238 std::unique_ptr<raw_ostream> OutStream = CreateInfoOutputFile();
Matthias Braun98ea88b2016-06-15 20:19:16 +0000239 if (StatsAsJSON)
240 PrintStatisticsJSON(*OutStream);
241 else
242 PrintStatistics(*OutStream);
Rafael Espindolab94ab5f2015-12-16 22:28:34 +0000243
Jan Wen Voung7857a642013-03-08 22:56:31 +0000244#else
245 // Check if the -stats option is set instead of checking
246 // !Stats.Stats.empty(). In release builds, Statistics operators
247 // do nothing, so stats are never Registered.
Matthias Braunc6035512016-09-26 18:38:07 +0000248 if (Stats) {
Jan Wen Voung7857a642013-03-08 22:56:31 +0000249 // Get the stream to write to.
Rafael Espindolab94ab5f2015-12-16 22:28:34 +0000250 std::unique_ptr<raw_ostream> OutStream = CreateInfoOutputFile();
251 (*OutStream) << "Statistics are disabled. "
252 << "Build with asserts or with -DLLVM_ENABLE_STATS\n";
Jan Wen Voung7857a642013-03-08 22:56:31 +0000253 }
254#endif
Chris Lattner9eb00522002-05-10 15:36:46 +0000255}
Daniel Sandersa09751e2018-03-05 19:38:16 +0000256
257const std::vector<std::pair<StringRef, unsigned>> llvm::GetStatistics() {
258 sys::SmartScopedLock<true> Reader(*StatLock);
259 std::vector<std::pair<StringRef, unsigned>> ReturnStats;
260
261 for (const auto &Stat : StatInfo->statistics())
262 ReturnStats.emplace_back(Stat->getName(), Stat->getValue());
263 return ReturnStats;
264}
Daniel Sanders1087a542018-03-08 02:36:25 +0000265
266void llvm::ResetStatistics() {
267 StatInfo->reset();
268}