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" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 32 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | afa3ec4 | 2003-08-13 21:32:37 +0000 | [diff] [blame] | 33 | #include <algorithm> |
Anton Korobeynikov | 579f071 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 34 | #include <cstring> |
Chris Lattner | dd978ce | 2003-12-14 21:27:33 +0000 | [diff] [blame] | 35 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 36 | |
Chris Lattner | 8c9969a | 2006-12-08 20:00:42 +0000 | [diff] [blame] | 37 | /// -stats - Command line option to cause transformations to emit stats about |
| 38 | /// what they did. |
| 39 | /// |
Matthias Braun | c603551 | 2016-09-26 18:38:07 +0000 | [diff] [blame] | 40 | static cl::opt<bool> Stats("stats", |
Jan Wen Voung | 7857a64 | 2013-03-08 22:56:31 +0000 | [diff] [blame] | 41 | cl::desc("Enable statistics output from program (available with Asserts)")); |
Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 42 | |
Chris Lattner | c758fe6 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 43 | |
Matthias Braun | 98ea88b | 2016-06-15 20:19:16 +0000 | [diff] [blame] | 44 | static cl::opt<bool> StatsAsJSON("stats-json", |
| 45 | cl::desc("Display statistics as json data")); |
| 46 | |
Matthias Braun | c603551 | 2016-09-26 18:38:07 +0000 | [diff] [blame] | 47 | static bool Enabled; |
Matthias Braun | 5391ffb | 2016-09-27 19:38:55 +0000 | [diff] [blame] | 48 | static bool PrintOnExit; |
Matthias Braun | c603551 | 2016-09-26 18:38:07 +0000 | [diff] [blame] | 49 | |
Chris Lattner | 8c9969a | 2006-12-08 20:00:42 +0000 | [diff] [blame] | 50 | namespace { |
| 51 | /// 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] | 52 | /// on demand (when the first statistic is bumped) and destroyed only when |
Chris Lattner | 8c9969a | 2006-12-08 20:00:42 +0000 | [diff] [blame] | 53 | /// llvm_shutdown is called. We print statistics from the destructor. |
| 54 | class StatisticInfo { |
Chris Lattner | 00bb216 | 2006-12-19 23:17:40 +0000 | [diff] [blame] | 55 | std::vector<const Statistic*> Stats; |
Douglas Gregor | 83fd015 | 2010-03-30 17:32:08 +0000 | [diff] [blame] | 56 | friend void llvm::PrintStatistics(); |
| 57 | friend void llvm::PrintStatistics(raw_ostream &OS); |
Matthias Braun | 98ea88b | 2016-06-15 20:19:16 +0000 | [diff] [blame] | 58 | friend void llvm::PrintStatisticsJSON(raw_ostream &OS); |
| 59 | |
| 60 | /// Sort statistics by debugtype,name,description. |
| 61 | void sort(); |
Chris Lattner | 8c9969a | 2006-12-08 20:00:42 +0000 | [diff] [blame] | 62 | public: |
| 63 | ~StatisticInfo(); |
Jim Grosbach | a927736 | 2010-08-17 17:37:22 +0000 | [diff] [blame] | 64 | |
Chris Lattner | 00bb216 | 2006-12-19 23:17:40 +0000 | [diff] [blame] | 65 | void addStatistic(const Statistic *S) { |
Chris Lattner | 8c9969a | 2006-12-08 20:00:42 +0000 | [diff] [blame] | 66 | Stats.push_back(S); |
Chris Lattner | c758fe6 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 67 | } |
Chris Lattner | 8c9969a | 2006-12-08 20:00:42 +0000 | [diff] [blame] | 68 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 69 | } |
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 | static ManagedStatic<StatisticInfo> StatInfo; |
Torok Edwin | 819d15c | 2009-09-27 11:08:03 +0000 | [diff] [blame] | 72 | static ManagedStatic<sys::SmartMutex<true> > StatLock; |
Chris Lattner | 8c9969a | 2006-12-08 20:00:42 +0000 | [diff] [blame] | 73 | |
| 74 | /// RegisterStatistic - The first time a statistic is bumped, this method is |
| 75 | /// called. |
Chris Lattner | 00bb216 | 2006-12-19 23:17:40 +0000 | [diff] [blame] | 76 | void Statistic::RegisterStatistic() { |
Chris Lattner | 8c9969a | 2006-12-08 20:00:42 +0000 | [diff] [blame] | 77 | // If stats are enabled, inform StatInfo that this statistic should be |
| 78 | // printed. |
Torok Edwin | 819d15c | 2009-09-27 11:08:03 +0000 | [diff] [blame] | 79 | sys::SmartScopedLock<true> Writer(*StatLock); |
Owen Anderson | ca8f986 | 2009-06-23 21:19:38 +0000 | [diff] [blame] | 80 | if (!Initialized) { |
Matthias Braun | c603551 | 2016-09-26 18:38:07 +0000 | [diff] [blame] | 81 | if (Stats || Enabled) |
Owen Anderson | ca8f986 | 2009-06-23 21:19:38 +0000 | [diff] [blame] | 82 | StatInfo->addStatistic(this); |
Jim Grosbach | a927736 | 2010-08-17 17:37:22 +0000 | [diff] [blame] | 83 | |
Nick Lewycky | 897a57e | 2011-12-05 23:07:05 +0000 | [diff] [blame] | 84 | TsanHappensBefore(this); |
Benjamin Kramer | 17388a6 | 2014-03-03 18:02:34 +0000 | [diff] [blame] | 85 | sys::MemoryFence(); |
Owen Anderson | ca8f986 | 2009-06-23 21:19:38 +0000 | [diff] [blame] | 86 | // Remember we have been registered. |
Nick Lewycky | 897a57e | 2011-12-05 23:07:05 +0000 | [diff] [blame] | 87 | TsanIgnoreWritesBegin(); |
Owen Anderson | ca8f986 | 2009-06-23 21:19:38 +0000 | [diff] [blame] | 88 | Initialized = true; |
Nick Lewycky | 897a57e | 2011-12-05 23:07:05 +0000 | [diff] [blame] | 89 | TsanIgnoreWritesEnd(); |
Owen Anderson | ca8f986 | 2009-06-23 21:19:38 +0000 | [diff] [blame] | 90 | } |
Chris Lattner | 8c9969a | 2006-12-08 20:00:42 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 93 | // Print information when destroyed, iff command line option is specified. |
Chris Lattner | 8c9969a | 2006-12-08 20:00:42 +0000 | [diff] [blame] | 94 | StatisticInfo::~StatisticInfo() { |
Matthias Braun | 5391ffb | 2016-09-27 19:38:55 +0000 | [diff] [blame] | 95 | if (::Stats || PrintOnExit) |
Matthias Braun | c603551 | 2016-09-26 18:38:07 +0000 | [diff] [blame] | 96 | llvm::PrintStatistics(); |
Douglas Gregor | 83fd015 | 2010-03-30 17:32:08 +0000 | [diff] [blame] | 97 | } |
Chris Lattner | c758fe6 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 98 | |
Matthias Braun | 5391ffb | 2016-09-27 19:38:55 +0000 | [diff] [blame] | 99 | void llvm::EnableStatistics(bool PrintOnExit) { |
Matthias Braun | c603551 | 2016-09-26 18:38:07 +0000 | [diff] [blame] | 100 | Enabled = true; |
Matthias Braun | 5391ffb | 2016-09-27 19:38:55 +0000 | [diff] [blame] | 101 | ::PrintOnExit = PrintOnExit; |
Douglas Gregor | 83fd015 | 2010-03-30 17:32:08 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Daniel Dunbar | 06dfe8e | 2011-02-26 23:17:12 +0000 | [diff] [blame] | 104 | bool llvm::AreStatisticsEnabled() { |
Matthias Braun | c603551 | 2016-09-26 18:38:07 +0000 | [diff] [blame] | 105 | return Enabled || Stats; |
Daniel Dunbar | 06dfe8e | 2011-02-26 23:17:12 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Matthias Braun | 98ea88b | 2016-06-15 20:19:16 +0000 | [diff] [blame] | 108 | void StatisticInfo::sort() { |
| 109 | std::stable_sort(Stats.begin(), Stats.end(), |
| 110 | [](const Statistic *LHS, const Statistic *RHS) { |
| 111 | if (int Cmp = std::strcmp(LHS->getDebugType(), RHS->getDebugType())) |
| 112 | return Cmp < 0; |
| 113 | |
| 114 | if (int Cmp = std::strcmp(LHS->getName(), RHS->getName())) |
| 115 | return Cmp < 0; |
| 116 | |
| 117 | return std::strcmp(LHS->getDesc(), RHS->getDesc()) < 0; |
| 118 | }); |
| 119 | } |
| 120 | |
Douglas Gregor | 83fd015 | 2010-03-30 17:32:08 +0000 | [diff] [blame] | 121 | void llvm::PrintStatistics(raw_ostream &OS) { |
| 122 | StatisticInfo &Stats = *StatInfo; |
Chris Lattner | c758fe6 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 123 | |
Chris Lattner | 8c9969a | 2006-12-08 20:00:42 +0000 | [diff] [blame] | 124 | // Figure out how long the biggest Value and Name fields are. |
Matthias Braun | 98ea88b | 2016-06-15 20:19:16 +0000 | [diff] [blame] | 125 | unsigned MaxDebugTypeLen = 0, MaxValLen = 0; |
Douglas Gregor | 83fd015 | 2010-03-30 17:32:08 +0000 | [diff] [blame] | 126 | for (size_t i = 0, e = Stats.Stats.size(); i != e; ++i) { |
Chris Lattner | 8c9969a | 2006-12-08 20:00:42 +0000 | [diff] [blame] | 127 | MaxValLen = std::max(MaxValLen, |
Douglas Gregor | 83fd015 | 2010-03-30 17:32:08 +0000 | [diff] [blame] | 128 | (unsigned)utostr(Stats.Stats[i]->getValue()).size()); |
Matthias Braun | 98ea88b | 2016-06-15 20:19:16 +0000 | [diff] [blame] | 129 | MaxDebugTypeLen = std::max(MaxDebugTypeLen, |
| 130 | (unsigned)std::strlen(Stats.Stats[i]->getDebugType())); |
Chris Lattner | c758fe6 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 131 | } |
Jim Grosbach | a927736 | 2010-08-17 17:37:22 +0000 | [diff] [blame] | 132 | |
Matthias Braun | 98ea88b | 2016-06-15 20:19:16 +0000 | [diff] [blame] | 133 | Stats.sort(); |
Chris Lattner | c758fe6 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 134 | |
Chris Lattner | 8c9969a | 2006-12-08 20:00:42 +0000 | [diff] [blame] | 135 | // Print out the statistics header... |
Douglas Gregor | 83fd015 | 2010-03-30 17:32:08 +0000 | [diff] [blame] | 136 | OS << "===" << std::string(73, '-') << "===\n" |
| 137 | << " ... Statistics Collected ...\n" |
| 138 | << "===" << std::string(73, '-') << "===\n\n"; |
Jim Grosbach | a927736 | 2010-08-17 17:37:22 +0000 | [diff] [blame] | 139 | |
Chris Lattner | 8c9969a | 2006-12-08 20:00:42 +0000 | [diff] [blame] | 140 | // Print all of the statistics. |
Benjamin Kramer | cc863b2 | 2011-10-16 16:30:34 +0000 | [diff] [blame] | 141 | for (size_t i = 0, e = Stats.Stats.size(); i != e; ++i) |
| 142 | OS << format("%*u %-*s - %s\n", |
| 143 | MaxValLen, Stats.Stats[i]->getValue(), |
Matthias Braun | 98ea88b | 2016-06-15 20:19:16 +0000 | [diff] [blame] | 144 | MaxDebugTypeLen, Stats.Stats[i]->getDebugType(), |
Benjamin Kramer | cc863b2 | 2011-10-16 16:30:34 +0000 | [diff] [blame] | 145 | Stats.Stats[i]->getDesc()); |
Jim Grosbach | a927736 | 2010-08-17 17:37:22 +0000 | [diff] [blame] | 146 | |
Douglas Gregor | 83fd015 | 2010-03-30 17:32:08 +0000 | [diff] [blame] | 147 | OS << '\n'; // Flush the output stream. |
| 148 | OS.flush(); |
Matthias Braun | 98ea88b | 2016-06-15 20:19:16 +0000 | [diff] [blame] | 149 | } |
Douglas Gregor | 83fd015 | 2010-03-30 17:32:08 +0000 | [diff] [blame] | 150 | |
Matthias Braun | 98ea88b | 2016-06-15 20:19:16 +0000 | [diff] [blame] | 151 | static void write_json_string_escaped(raw_ostream &OS, const char *string) { |
| 152 | // Out current usage should not need any escaping. Keep it simple and just |
| 153 | // check that the input is pure ASCII without special characers. |
| 154 | #ifndef NDEBUG |
| 155 | for (const unsigned char *c = (const unsigned char*)string; *c != '\0'; ++c) { |
| 156 | assert(*c != '\\' && *c != '\"' && *c >= 0x20 && *c < 0x80); |
| 157 | } |
| 158 | #endif |
| 159 | OS << string; |
| 160 | } |
| 161 | |
| 162 | void llvm::PrintStatisticsJSON(raw_ostream &OS) { |
| 163 | StatisticInfo &Stats = *StatInfo; |
| 164 | |
| 165 | Stats.sort(); |
| 166 | |
| 167 | // Print all of the statistics. |
| 168 | OS << "{\n"; |
| 169 | const char *delim = ""; |
| 170 | for (const Statistic *Stat : Stats.Stats) { |
| 171 | OS << delim; |
| 172 | OS << "\t\""; |
| 173 | write_json_string_escaped(OS, Stat->getDebugType()); |
| 174 | OS << '.'; |
| 175 | write_json_string_escaped(OS, Stat->getName()); |
| 176 | OS << "\": " << Stat->getValue(); |
| 177 | delim = ",\n"; |
| 178 | } |
| 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 | } |