blob: 0d8b5b2e0abcc075d0ad26912de13f7234944ab9 [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//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Misha Brukman10468d82005-04-21 22:55:34 +00006//
John Criswell482202a2003-10-20 19:43:21 +00007//===----------------------------------------------------------------------===//
Chris Lattner9eb00522002-05-10 15:36:46 +00008//
9// This file implements the 'Statistic' class, which is designed to be an easy
10// way to expose various success metrics from passes. These statistics are
11// printed at the end of a run, when the -stats command line option is enabled
12// on the command line.
13//
14// This is useful for reporting information like the number of instructions
15// simplified, optimized or removed by various transformations, like this:
16//
Chris Lattner8c9969a2006-12-08 20:00:42 +000017// static Statistic NumInstEliminated("GCSE", "Number of instructions killed");
Chris Lattner9eb00522002-05-10 15:36:46 +000018//
19// Later, in the code: ++NumInstEliminated;
20//
21//===----------------------------------------------------------------------===//
22
Reid Spencer7c16caa2004-09-01 22:55:40 +000023#include "llvm/ADT/Statistic.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include "llvm/ADT/StringExtras.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000025#include "llvm/Support/CommandLine.h"
Peter Collingbourne4cfa0862015-08-18 22:31:24 +000026#include "llvm/Support/Compiler.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"
Matthias Braundb39fd62016-11-18 19:43:24 +000031#include "llvm/Support/Timer.h"
Matthias Braundb39fd62016-11-18 19:43:24 +000032#include "llvm/Support/YAMLTraits.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000033#include "llvm/Support/raw_ostream.h"
Chris Lattnerafa3ec42003-08-13 21:32:37 +000034#include <algorithm>
Anton Korobeynikov579f0712008-02-20 11:08:44 +000035#include <cstring>
Chris Lattnerdd978ce2003-12-14 21:27:33 +000036using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000037
Chris Lattner8c9969a2006-12-08 20:00:42 +000038/// -stats - Command line option to cause transformations to emit stats about
39/// what they did.
40///
Zachary Turner8065f0b2017-12-01 00:53:10 +000041static cl::opt<bool> Stats(
42 "stats",
43 cl::desc("Enable statistics output from program (available with Asserts)"),
44 cl::Hidden);
Chris Lattnerc758fe62002-10-01 22:35:45 +000045
Matthias Braun98ea88b2016-06-15 20:19:16 +000046static cl::opt<bool> StatsAsJSON("stats-json",
Zachary Turner8065f0b2017-12-01 00:53:10 +000047 cl::desc("Display statistics as json data"),
48 cl::Hidden);
Matthias Braun98ea88b2016-06-15 20:19:16 +000049
Matthias Braunc6035512016-09-26 18:38:07 +000050static bool Enabled;
Matthias Braun5391ffb2016-09-27 19:38:55 +000051static bool PrintOnExit;
Matthias Braunc6035512016-09-26 18:38:07 +000052
Chris Lattner8c9969a2006-12-08 20:00:42 +000053namespace {
Daniel Sandersa09751e2018-03-05 19:38:16 +000054/// This class is used in a ManagedStatic so that it is created on demand (when
55/// the first statistic is bumped) and destroyed only when llvm_shutdown is
56/// called. We print statistics from the destructor.
57/// This class is also used to look up statistic values from applications that
58/// use LLVM.
Chris Lattner8c9969a2006-12-08 20:00:42 +000059class StatisticInfo {
Daniel Sanders1087a542018-03-08 02:36:25 +000060 std::vector<Statistic*> Stats;
Daniel Sandersa09751e2018-03-05 19:38:16 +000061
Douglas Gregor83fd0152010-03-30 17:32:08 +000062 friend void llvm::PrintStatistics();
63 friend void llvm::PrintStatistics(raw_ostream &OS);
Matthias Braun98ea88b2016-06-15 20:19:16 +000064 friend void llvm::PrintStatisticsJSON(raw_ostream &OS);
65
66 /// Sort statistics by debugtype,name,description.
67 void sort();
Chris Lattner8c9969a2006-12-08 20:00:42 +000068public:
Daniel Sanders1087a542018-03-08 02:36:25 +000069 using const_iterator = std::vector<Statistic *>::const_iterator;
Daniel Sandersa09751e2018-03-05 19:38:16 +000070
Matthias Braundb39fd62016-11-18 19:43:24 +000071 StatisticInfo();
Chris Lattner8c9969a2006-12-08 20:00:42 +000072 ~StatisticInfo();
Jim Grosbacha9277362010-08-17 17:37:22 +000073
Daniel Sanders1087a542018-03-08 02:36:25 +000074 void addStatistic(Statistic *S) {
Chris Lattner8c9969a2006-12-08 20:00:42 +000075 Stats.push_back(S);
Chris Lattnerc758fe62002-10-01 22:35:45 +000076 }
Daniel Sandersa09751e2018-03-05 19:38:16 +000077
78 const_iterator begin() const { return Stats.begin(); }
79 const_iterator end() const { return Stats.end(); }
80 iterator_range<const_iterator> statistics() const {
81 return {begin(), end()};
82 }
Daniel Sanders1087a542018-03-08 02:36:25 +000083
84 void reset();
Chris Lattner8c9969a2006-12-08 20:00:42 +000085};
Daniel Sandersa09751e2018-03-05 19:38:16 +000086} // end anonymous namespace
Chris Lattnerc758fe62002-10-01 22:35:45 +000087
Chris Lattner8c9969a2006-12-08 20:00:42 +000088static ManagedStatic<StatisticInfo> StatInfo;
Torok Edwin819d15c2009-09-27 11:08:03 +000089static ManagedStatic<sys::SmartMutex<true> > StatLock;
Chris Lattner8c9969a2006-12-08 20:00:42 +000090
91/// RegisterStatistic - The first time a statistic is bumped, this method is
92/// called.
Chris Lattner00bb2162006-12-19 23:17:40 +000093void Statistic::RegisterStatistic() {
Chris Lattner8c9969a2006-12-08 20:00:42 +000094 // If stats are enabled, inform StatInfo that this statistic should be
95 // printed.
Bob Haarman37a92692018-04-17 23:37:18 +000096 // llvm_shutdown calls destructors while holding the ManagedStatic mutex.
97 // These destructors end up calling PrintStatistics, which takes StatLock.
98 // Since dereferencing StatInfo and StatLock can require taking the
99 // ManagedStatic mutex, doing so with StatLock held would lead to a lock
100 // order inversion. To avoid that, we dereference the ManagedStatics first,
101 // and only take StatLock afterwards.
Benjamin Kramer351e9f32018-02-01 20:28:33 +0000102 if (!Initialized.load(std::memory_order_relaxed)) {
Bob Haarman37a92692018-04-17 23:37:18 +0000103 sys::SmartMutex<true> &Lock = *StatLock;
104 StatisticInfo &SI = *StatInfo;
105 sys::SmartScopedLock<true> Writer(Lock);
106 // Check Initialized again after acquiring the lock.
107 if (Initialized.load(std::memory_order_relaxed))
108 return;
Matthias Braunc6035512016-09-26 18:38:07 +0000109 if (Stats || Enabled)
Bob Haarman37a92692018-04-17 23:37:18 +0000110 SI.addStatistic(this);
Jim Grosbacha9277362010-08-17 17:37:22 +0000111
Owen Andersonca8f9862009-06-23 21:19:38 +0000112 // Remember we have been registered.
Benjamin Kramer351e9f32018-02-01 20:28:33 +0000113 Initialized.store(true, std::memory_order_release);
Owen Andersonca8f9862009-06-23 21:19:38 +0000114 }
Chris Lattner8c9969a2006-12-08 20:00:42 +0000115}
116
Matthias Braundb39fd62016-11-18 19:43:24 +0000117StatisticInfo::StatisticInfo() {
118 // Ensure timergroup lists are created first so they are destructed after us.
119 TimerGroup::ConstructTimerLists();
120}
121
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000122// Print information when destroyed, iff command line option is specified.
Chris Lattner8c9969a2006-12-08 20:00:42 +0000123StatisticInfo::~StatisticInfo() {
Matthias Braun5391ffb2016-09-27 19:38:55 +0000124 if (::Stats || PrintOnExit)
Matthias Braunc6035512016-09-26 18:38:07 +0000125 llvm::PrintStatistics();
Douglas Gregor83fd0152010-03-30 17:32:08 +0000126}
Chris Lattnerc758fe62002-10-01 22:35:45 +0000127
Matthias Braun5391ffb2016-09-27 19:38:55 +0000128void llvm::EnableStatistics(bool PrintOnExit) {
Matthias Braunc6035512016-09-26 18:38:07 +0000129 Enabled = true;
Matthias Braun5391ffb2016-09-27 19:38:55 +0000130 ::PrintOnExit = PrintOnExit;
Douglas Gregor83fd0152010-03-30 17:32:08 +0000131}
132
Daniel Dunbar06dfe8e2011-02-26 23:17:12 +0000133bool llvm::AreStatisticsEnabled() {
Matthias Braunc6035512016-09-26 18:38:07 +0000134 return Enabled || Stats;
Daniel Dunbar06dfe8e2011-02-26 23:17:12 +0000135}
136
Matthias Braun98ea88b2016-06-15 20:19:16 +0000137void StatisticInfo::sort() {
138 std::stable_sort(Stats.begin(), Stats.end(),
139 [](const Statistic *LHS, const Statistic *RHS) {
140 if (int Cmp = std::strcmp(LHS->getDebugType(), RHS->getDebugType()))
141 return Cmp < 0;
142
143 if (int Cmp = std::strcmp(LHS->getName(), RHS->getName()))
144 return Cmp < 0;
145
146 return std::strcmp(LHS->getDesc(), RHS->getDesc()) < 0;
147 });
148}
149
Daniel Sanders1087a542018-03-08 02:36:25 +0000150void StatisticInfo::reset() {
151 sys::SmartScopedLock<true> Writer(*StatLock);
152
153 // Tell each statistic that it isn't registered so it has to register
154 // again. We're holding the lock so it won't be able to do so until we're
155 // finished. Once we've forced it to re-register (after we return), then zero
156 // the value.
157 for (auto *Stat : Stats) {
158 // Value updates to a statistic that complete before this statement in the
159 // iteration for that statistic will be lost as intended.
160 Stat->Initialized = false;
161 Stat->Value = 0;
162 }
163
164 // Clear the registration list and release the lock once we're done. Any
165 // pending updates from other threads will safely take effect after we return.
166 // That might not be what the user wants if they're measuring a compilation
167 // but it's their responsibility to prevent concurrent compilations to make
168 // a single compilation measurable.
169 Stats.clear();
170}
171
Douglas Gregor83fd0152010-03-30 17:32:08 +0000172void llvm::PrintStatistics(raw_ostream &OS) {
173 StatisticInfo &Stats = *StatInfo;
Chris Lattnerc758fe62002-10-01 22:35:45 +0000174
Chris Lattner8c9969a2006-12-08 20:00:42 +0000175 // Figure out how long the biggest Value and Name fields are.
Matthias Braun98ea88b2016-06-15 20:19:16 +0000176 unsigned MaxDebugTypeLen = 0, MaxValLen = 0;
Douglas Gregor83fd0152010-03-30 17:32:08 +0000177 for (size_t i = 0, e = Stats.Stats.size(); i != e; ++i) {
Chris Lattner8c9969a2006-12-08 20:00:42 +0000178 MaxValLen = std::max(MaxValLen,
Douglas Gregor83fd0152010-03-30 17:32:08 +0000179 (unsigned)utostr(Stats.Stats[i]->getValue()).size());
Matthias Braun98ea88b2016-06-15 20:19:16 +0000180 MaxDebugTypeLen = std::max(MaxDebugTypeLen,
181 (unsigned)std::strlen(Stats.Stats[i]->getDebugType()));
Chris Lattnerc758fe62002-10-01 22:35:45 +0000182 }
Jim Grosbacha9277362010-08-17 17:37:22 +0000183
Matthias Braun98ea88b2016-06-15 20:19:16 +0000184 Stats.sort();
Chris Lattnerc758fe62002-10-01 22:35:45 +0000185
Chris Lattner8c9969a2006-12-08 20:00:42 +0000186 // Print out the statistics header...
Douglas Gregor83fd0152010-03-30 17:32:08 +0000187 OS << "===" << std::string(73, '-') << "===\n"
188 << " ... Statistics Collected ...\n"
189 << "===" << std::string(73, '-') << "===\n\n";
Jim Grosbacha9277362010-08-17 17:37:22 +0000190
Chris Lattner8c9969a2006-12-08 20:00:42 +0000191 // Print all of the statistics.
Benjamin Kramercc863b22011-10-16 16:30:34 +0000192 for (size_t i = 0, e = Stats.Stats.size(); i != e; ++i)
193 OS << format("%*u %-*s - %s\n",
194 MaxValLen, Stats.Stats[i]->getValue(),
Matthias Braun98ea88b2016-06-15 20:19:16 +0000195 MaxDebugTypeLen, Stats.Stats[i]->getDebugType(),
Benjamin Kramercc863b22011-10-16 16:30:34 +0000196 Stats.Stats[i]->getDesc());
Jim Grosbacha9277362010-08-17 17:37:22 +0000197
Douglas Gregor83fd0152010-03-30 17:32:08 +0000198 OS << '\n'; // Flush the output stream.
199 OS.flush();
Matthias Braun98ea88b2016-06-15 20:19:16 +0000200}
Douglas Gregor83fd0152010-03-30 17:32:08 +0000201
Matthias Braun98ea88b2016-06-15 20:19:16 +0000202void llvm::PrintStatisticsJSON(raw_ostream &OS) {
Daniel Sanders0f4b0152018-03-06 21:16:42 +0000203 sys::SmartScopedLock<true> Reader(*StatLock);
Matthias Braun98ea88b2016-06-15 20:19:16 +0000204 StatisticInfo &Stats = *StatInfo;
205
206 Stats.sort();
207
208 // Print all of the statistics.
209 OS << "{\n";
210 const char *delim = "";
211 for (const Statistic *Stat : Stats.Stats) {
212 OS << delim;
Francis Visoiu Mistrihb213b272017-12-18 17:38:03 +0000213 assert(yaml::needsQuotes(Stat->getDebugType()) == yaml::QuotingType::None &&
Matthias Braundb39fd62016-11-18 19:43:24 +0000214 "Statistic group/type name is simple.");
Francis Visoiu Mistrihb213b272017-12-18 17:38:03 +0000215 assert(yaml::needsQuotes(Stat->getName()) == yaml::QuotingType::None &&
216 "Statistic name is simple");
Matthias Braundb39fd62016-11-18 19:43:24 +0000217 OS << "\t\"" << Stat->getDebugType() << '.' << Stat->getName() << "\": "
218 << Stat->getValue();
Matthias Braun98ea88b2016-06-15 20:19:16 +0000219 delim = ",\n";
220 }
Matthias Braundb39fd62016-11-18 19:43:24 +0000221 // Print timers.
222 TimerGroup::printAllJSONValues(OS, delim);
223
Matthias Braun98ea88b2016-06-15 20:19:16 +0000224 OS << "\n}\n";
225 OS.flush();
Douglas Gregor83fd0152010-03-30 17:32:08 +0000226}
227
228void llvm::PrintStatistics() {
Daniel Sandersa09751e2018-03-05 19:38:16 +0000229#if LLVM_ENABLE_STATS
Daniel Sanders0f4b0152018-03-06 21:16:42 +0000230 sys::SmartScopedLock<true> Reader(*StatLock);
Douglas Gregor83fd0152010-03-30 17:32:08 +0000231 StatisticInfo &Stats = *StatInfo;
232
233 // Statistics not enabled?
234 if (Stats.Stats.empty()) return;
235
236 // Get the stream to write to.
Rafael Espindolab94ab5f2015-12-16 22:28:34 +0000237 std::unique_ptr<raw_ostream> OutStream = CreateInfoOutputFile();
Matthias Braun98ea88b2016-06-15 20:19:16 +0000238 if (StatsAsJSON)
239 PrintStatisticsJSON(*OutStream);
240 else
241 PrintStatistics(*OutStream);
Rafael Espindolab94ab5f2015-12-16 22:28:34 +0000242
Jan Wen Voung7857a642013-03-08 22:56:31 +0000243#else
244 // Check if the -stats option is set instead of checking
245 // !Stats.Stats.empty(). In release builds, Statistics operators
246 // do nothing, so stats are never Registered.
Matthias Braunc6035512016-09-26 18:38:07 +0000247 if (Stats) {
Jan Wen Voung7857a642013-03-08 22:56:31 +0000248 // Get the stream to write to.
Rafael Espindolab94ab5f2015-12-16 22:28:34 +0000249 std::unique_ptr<raw_ostream> OutStream = CreateInfoOutputFile();
250 (*OutStream) << "Statistics are disabled. "
251 << "Build with asserts or with -DLLVM_ENABLE_STATS\n";
Jan Wen Voung7857a642013-03-08 22:56:31 +0000252 }
253#endif
Chris Lattner9eb00522002-05-10 15:36:46 +0000254}
Daniel Sandersa09751e2018-03-05 19:38:16 +0000255
256const std::vector<std::pair<StringRef, unsigned>> llvm::GetStatistics() {
257 sys::SmartScopedLock<true> Reader(*StatLock);
258 std::vector<std::pair<StringRef, unsigned>> ReturnStats;
259
260 for (const auto &Stat : StatInfo->statistics())
261 ReturnStats.emplace_back(Stat->getName(), Stat->getValue());
262 return ReturnStats;
263}
Daniel Sanders1087a542018-03-08 02:36:25 +0000264
265void llvm::ResetStatistics() {
266 StatInfo->reset();
267}