Chris Lattner | c758fe6 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 1 | //===-- Statistic.cpp - Easy way to expose stats information --------------===// |
Misha Brukman | 10468d8 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 2 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | 10468d8 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 7 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 9eb0052 | 2002-05-10 15:36:46 +0000 | [diff] [blame] | 9 | // |
| 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 Lattner | 8c9969a | 2006-12-08 20:00:42 +0000 | [diff] [blame] | 18 | // static Statistic NumInstEliminated("GCSE", "Number of instructions killed"); |
Chris Lattner | 9eb0052 | 2002-05-10 15:36:46 +0000 | [diff] [blame] | 19 | // |
| 20 | // Later, in the code: ++NumInstEliminated; |
| 21 | // |
| 22 | //===----------------------------------------------------------------------===// |
| 23 | |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/Statistic.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/StringExtras.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 26 | #include "llvm/Support/CommandLine.h" |
Peter Collingbourne | 4cfa086 | 2015-08-18 22:31:24 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Compiler.h" |
David Greene | b28b1ed | 2010-01-05 01:28:47 +0000 | [diff] [blame] | 28 | #include "llvm/Support/Debug.h" |
Benjamin Kramer | cc863b2 | 2011-10-16 16:30:34 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Format.h" |
Chris Lattner | 8c9969a | 2006-12-08 20:00:42 +0000 | [diff] [blame] | 30 | #include "llvm/Support/ManagedStatic.h" |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 31 | #include "llvm/Support/Mutex.h" |
Matthias Braun | db39fd6 | 2016-11-18 19:43:24 +0000 | [diff] [blame] | 32 | #include "llvm/Support/Timer.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 33 | #include "llvm/Support/raw_ostream.h" |
Matthias Braun | db39fd6 | 2016-11-18 19:43:24 +0000 | [diff] [blame] | 34 | #include "llvm/Support/YAMLTraits.h" |
Chris Lattner | afa3ec4 | 2003-08-13 21:32:37 +0000 | [diff] [blame] | 35 | #include <algorithm> |
Anton Korobeynikov | 579f071 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 36 | #include <cstring> |
Chris Lattner | dd978ce | 2003-12-14 21:27:33 +0000 | [diff] [blame] | 37 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 38 | |
Chris Lattner | 8c9969a | 2006-12-08 20:00:42 +0000 | [diff] [blame] | 39 | /// -stats - Command line option to cause transformations to emit stats about |
| 40 | /// what they did. |
| 41 | /// |
Matthias Braun | c603551 | 2016-09-26 18:38:07 +0000 | [diff] [blame] | 42 | static cl::opt<bool> Stats("stats", |
Jan Wen Voung | 7857a64 | 2013-03-08 22:56:31 +0000 | [diff] [blame] | 43 | cl::desc("Enable statistics output from program (available with Asserts)")); |
Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 44 | |
Chris Lattner | c758fe6 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 45 | |
Matthias Braun | 98ea88b | 2016-06-15 20:19:16 +0000 | [diff] [blame] | 46 | static cl::opt<bool> StatsAsJSON("stats-json", |
| 47 | cl::desc("Display statistics as json data")); |
| 48 | |
Matthias Braun | c603551 | 2016-09-26 18:38:07 +0000 | [diff] [blame] | 49 | static bool Enabled; |
Matthias Braun | 5391ffb | 2016-09-27 19:38:55 +0000 | [diff] [blame] | 50 | static bool PrintOnExit; |
Matthias Braun | c603551 | 2016-09-26 18:38:07 +0000 | [diff] [blame] | 51 | |
Chris Lattner | 8c9969a | 2006-12-08 20:00:42 +0000 | [diff] [blame] | 52 | namespace { |
| 53 | /// StatisticInfo - This class is used in a ManagedStatic so that it is created |
Jim Grosbach | a927736 | 2010-08-17 17:37:22 +0000 | [diff] [blame] | 54 | /// on demand (when the first statistic is bumped) and destroyed only when |
Chris Lattner | 8c9969a | 2006-12-08 20:00:42 +0000 | [diff] [blame] | 55 | /// llvm_shutdown is called. We print statistics from the destructor. |
| 56 | class StatisticInfo { |
Chris Lattner | 00bb216 | 2006-12-19 23:17:40 +0000 | [diff] [blame] | 57 | std::vector<const Statistic*> Stats; |
Douglas Gregor | 83fd015 | 2010-03-30 17:32:08 +0000 | [diff] [blame] | 58 | friend void llvm::PrintStatistics(); |
| 59 | friend void llvm::PrintStatistics(raw_ostream &OS); |
Matthias Braun | 98ea88b | 2016-06-15 20:19:16 +0000 | [diff] [blame] | 60 | friend void llvm::PrintStatisticsJSON(raw_ostream &OS); |
| 61 | |
| 62 | /// Sort statistics by debugtype,name,description. |
| 63 | void sort(); |
Chris Lattner | 8c9969a | 2006-12-08 20:00:42 +0000 | [diff] [blame] | 64 | public: |
Matthias Braun | db39fd6 | 2016-11-18 19:43:24 +0000 | [diff] [blame] | 65 | StatisticInfo(); |
Chris Lattner | 8c9969a | 2006-12-08 20:00:42 +0000 | [diff] [blame] | 66 | ~StatisticInfo(); |
Jim Grosbach | a927736 | 2010-08-17 17:37:22 +0000 | [diff] [blame] | 67 | |
Chris Lattner | 00bb216 | 2006-12-19 23:17:40 +0000 | [diff] [blame] | 68 | void addStatistic(const Statistic *S) { |
Chris Lattner | 8c9969a | 2006-12-08 20:00:42 +0000 | [diff] [blame] | 69 | Stats.push_back(S); |
Chris Lattner | c758fe6 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 70 | } |
Chris Lattner | 8c9969a | 2006-12-08 20:00:42 +0000 | [diff] [blame] | 71 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 72 | } |
Chris Lattner | c758fe6 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 73 | |
Chris Lattner | 8c9969a | 2006-12-08 20:00:42 +0000 | [diff] [blame] | 74 | static ManagedStatic<StatisticInfo> StatInfo; |
Torok Edwin | 819d15c | 2009-09-27 11:08:03 +0000 | [diff] [blame] | 75 | static ManagedStatic<sys::SmartMutex<true> > StatLock; |
Chris Lattner | 8c9969a | 2006-12-08 20:00:42 +0000 | [diff] [blame] | 76 | |
| 77 | /// RegisterStatistic - The first time a statistic is bumped, this method is |
| 78 | /// called. |
Chris Lattner | 00bb216 | 2006-12-19 23:17:40 +0000 | [diff] [blame] | 79 | void Statistic::RegisterStatistic() { |
Chris Lattner | 8c9969a | 2006-12-08 20:00:42 +0000 | [diff] [blame] | 80 | // If stats are enabled, inform StatInfo that this statistic should be |
| 81 | // printed. |
Torok Edwin | 819d15c | 2009-09-27 11:08:03 +0000 | [diff] [blame] | 82 | sys::SmartScopedLock<true> Writer(*StatLock); |
Owen Anderson | ca8f986 | 2009-06-23 21:19:38 +0000 | [diff] [blame] | 83 | if (!Initialized) { |
Matthias Braun | c603551 | 2016-09-26 18:38:07 +0000 | [diff] [blame] | 84 | if (Stats || Enabled) |
Owen Anderson | ca8f986 | 2009-06-23 21:19:38 +0000 | [diff] [blame] | 85 | StatInfo->addStatistic(this); |
Jim Grosbach | a927736 | 2010-08-17 17:37:22 +0000 | [diff] [blame] | 86 | |
Nick Lewycky | 897a57e | 2011-12-05 23:07:05 +0000 | [diff] [blame] | 87 | TsanHappensBefore(this); |
Benjamin Kramer | 17388a6 | 2014-03-03 18:02:34 +0000 | [diff] [blame] | 88 | sys::MemoryFence(); |
Owen Anderson | ca8f986 | 2009-06-23 21:19:38 +0000 | [diff] [blame] | 89 | // Remember we have been registered. |
Nick Lewycky | 897a57e | 2011-12-05 23:07:05 +0000 | [diff] [blame] | 90 | TsanIgnoreWritesBegin(); |
Owen Anderson | ca8f986 | 2009-06-23 21:19:38 +0000 | [diff] [blame] | 91 | Initialized = true; |
Nick Lewycky | 897a57e | 2011-12-05 23:07:05 +0000 | [diff] [blame] | 92 | TsanIgnoreWritesEnd(); |
Owen Anderson | ca8f986 | 2009-06-23 21:19:38 +0000 | [diff] [blame] | 93 | } |
Chris Lattner | 8c9969a | 2006-12-08 20:00:42 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Matthias Braun | db39fd6 | 2016-11-18 19:43:24 +0000 | [diff] [blame] | 96 | StatisticInfo::StatisticInfo() { |
| 97 | // Ensure timergroup lists are created first so they are destructed after us. |
| 98 | TimerGroup::ConstructTimerLists(); |
| 99 | } |
| 100 | |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 101 | // Print information when destroyed, iff command line option is specified. |
Chris Lattner | 8c9969a | 2006-12-08 20:00:42 +0000 | [diff] [blame] | 102 | StatisticInfo::~StatisticInfo() { |
Matthias Braun | 5391ffb | 2016-09-27 19:38:55 +0000 | [diff] [blame] | 103 | if (::Stats || PrintOnExit) |
Matthias Braun | c603551 | 2016-09-26 18:38:07 +0000 | [diff] [blame] | 104 | llvm::PrintStatistics(); |
Douglas Gregor | 83fd015 | 2010-03-30 17:32:08 +0000 | [diff] [blame] | 105 | } |
Chris Lattner | c758fe6 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 106 | |
Matthias Braun | 5391ffb | 2016-09-27 19:38:55 +0000 | [diff] [blame] | 107 | void llvm::EnableStatistics(bool PrintOnExit) { |
Matthias Braun | c603551 | 2016-09-26 18:38:07 +0000 | [diff] [blame] | 108 | Enabled = true; |
Matthias Braun | 5391ffb | 2016-09-27 19:38:55 +0000 | [diff] [blame] | 109 | ::PrintOnExit = PrintOnExit; |
Douglas Gregor | 83fd015 | 2010-03-30 17:32:08 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Daniel Dunbar | 06dfe8e | 2011-02-26 23:17:12 +0000 | [diff] [blame] | 112 | bool llvm::AreStatisticsEnabled() { |
Matthias Braun | c603551 | 2016-09-26 18:38:07 +0000 | [diff] [blame] | 113 | return Enabled || Stats; |
Daniel Dunbar | 06dfe8e | 2011-02-26 23:17:12 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Matthias Braun | 98ea88b | 2016-06-15 20:19:16 +0000 | [diff] [blame] | 116 | void 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 Gregor | 83fd015 | 2010-03-30 17:32:08 +0000 | [diff] [blame] | 129 | void llvm::PrintStatistics(raw_ostream &OS) { |
| 130 | StatisticInfo &Stats = *StatInfo; |
Chris Lattner | c758fe6 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 131 | |
Chris Lattner | 8c9969a | 2006-12-08 20:00:42 +0000 | [diff] [blame] | 132 | // Figure out how long the biggest Value and Name fields are. |
Matthias Braun | 98ea88b | 2016-06-15 20:19:16 +0000 | [diff] [blame] | 133 | unsigned MaxDebugTypeLen = 0, MaxValLen = 0; |
Douglas Gregor | 83fd015 | 2010-03-30 17:32:08 +0000 | [diff] [blame] | 134 | for (size_t i = 0, e = Stats.Stats.size(); i != e; ++i) { |
Chris Lattner | 8c9969a | 2006-12-08 20:00:42 +0000 | [diff] [blame] | 135 | MaxValLen = std::max(MaxValLen, |
Douglas Gregor | 83fd015 | 2010-03-30 17:32:08 +0000 | [diff] [blame] | 136 | (unsigned)utostr(Stats.Stats[i]->getValue()).size()); |
Matthias Braun | 98ea88b | 2016-06-15 20:19:16 +0000 | [diff] [blame] | 137 | MaxDebugTypeLen = std::max(MaxDebugTypeLen, |
| 138 | (unsigned)std::strlen(Stats.Stats[i]->getDebugType())); |
Chris Lattner | c758fe6 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 139 | } |
Jim Grosbach | a927736 | 2010-08-17 17:37:22 +0000 | [diff] [blame] | 140 | |
Matthias Braun | 98ea88b | 2016-06-15 20:19:16 +0000 | [diff] [blame] | 141 | Stats.sort(); |
Chris Lattner | c758fe6 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 142 | |
Chris Lattner | 8c9969a | 2006-12-08 20:00:42 +0000 | [diff] [blame] | 143 | // Print out the statistics header... |
Douglas Gregor | 83fd015 | 2010-03-30 17:32:08 +0000 | [diff] [blame] | 144 | OS << "===" << std::string(73, '-') << "===\n" |
| 145 | << " ... Statistics Collected ...\n" |
| 146 | << "===" << std::string(73, '-') << "===\n\n"; |
Jim Grosbach | a927736 | 2010-08-17 17:37:22 +0000 | [diff] [blame] | 147 | |
Chris Lattner | 8c9969a | 2006-12-08 20:00:42 +0000 | [diff] [blame] | 148 | // Print all of the statistics. |
Benjamin Kramer | cc863b2 | 2011-10-16 16:30:34 +0000 | [diff] [blame] | 149 | 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 Braun | 98ea88b | 2016-06-15 20:19:16 +0000 | [diff] [blame] | 152 | MaxDebugTypeLen, Stats.Stats[i]->getDebugType(), |
Benjamin Kramer | cc863b2 | 2011-10-16 16:30:34 +0000 | [diff] [blame] | 153 | Stats.Stats[i]->getDesc()); |
Jim Grosbach | a927736 | 2010-08-17 17:37:22 +0000 | [diff] [blame] | 154 | |
Douglas Gregor | 83fd015 | 2010-03-30 17:32:08 +0000 | [diff] [blame] | 155 | OS << '\n'; // Flush the output stream. |
| 156 | OS.flush(); |
Matthias Braun | 98ea88b | 2016-06-15 20:19:16 +0000 | [diff] [blame] | 157 | } |
Douglas Gregor | 83fd015 | 2010-03-30 17:32:08 +0000 | [diff] [blame] | 158 | |
Matthias Braun | 98ea88b | 2016-06-15 20:19:16 +0000 | [diff] [blame] | 159 | void 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 Braun | db39fd6 | 2016-11-18 19:43:24 +0000 | [diff] [blame] | 169 | 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 Braun | 98ea88b | 2016-06-15 20:19:16 +0000 | [diff] [blame] | 174 | delim = ",\n"; |
| 175 | } |
Matthias Braun | db39fd6 | 2016-11-18 19:43:24 +0000 | [diff] [blame] | 176 | // Print timers. |
| 177 | TimerGroup::printAllJSONValues(OS, delim); |
| 178 | |
Matthias Braun | 98ea88b | 2016-06-15 20:19:16 +0000 | [diff] [blame] | 179 | OS << "\n}\n"; |
| 180 | OS.flush(); |
Douglas Gregor | 83fd015 | 2010-03-30 17:32:08 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | void llvm::PrintStatistics() { |
Jan Wen Voung | 7857a64 | 2013-03-08 22:56:31 +0000 | [diff] [blame] | 184 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_STATS) |
Douglas Gregor | 83fd015 | 2010-03-30 17:32:08 +0000 | [diff] [blame] | 185 | StatisticInfo &Stats = *StatInfo; |
| 186 | |
| 187 | // Statistics not enabled? |
| 188 | if (Stats.Stats.empty()) return; |
| 189 | |
| 190 | // Get the stream to write to. |
Rafael Espindola | b94ab5f | 2015-12-16 22:28:34 +0000 | [diff] [blame] | 191 | std::unique_ptr<raw_ostream> OutStream = CreateInfoOutputFile(); |
Matthias Braun | 98ea88b | 2016-06-15 20:19:16 +0000 | [diff] [blame] | 192 | if (StatsAsJSON) |
| 193 | PrintStatisticsJSON(*OutStream); |
| 194 | else |
| 195 | PrintStatistics(*OutStream); |
Rafael Espindola | b94ab5f | 2015-12-16 22:28:34 +0000 | [diff] [blame] | 196 | |
Jan Wen Voung | 7857a64 | 2013-03-08 22:56:31 +0000 | [diff] [blame] | 197 | #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 Braun | c603551 | 2016-09-26 18:38:07 +0000 | [diff] [blame] | 201 | if (Stats) { |
Jan Wen Voung | 7857a64 | 2013-03-08 22:56:31 +0000 | [diff] [blame] | 202 | // Get the stream to write to. |
Rafael Espindola | b94ab5f | 2015-12-16 22:28:34 +0000 | [diff] [blame] | 203 | std::unique_ptr<raw_ostream> OutStream = CreateInfoOutputFile(); |
| 204 | (*OutStream) << "Statistics are disabled. " |
| 205 | << "Build with asserts or with -DLLVM_ENABLE_STATS\n"; |
Jan Wen Voung | 7857a64 | 2013-03-08 22:56:31 +0000 | [diff] [blame] | 206 | } |
| 207 | #endif |
Chris Lattner | 9eb0052 | 2002-05-10 15:36:46 +0000 | [diff] [blame] | 208 | } |