| 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" | 
| Matthias Braun | db39fd6 | 2016-11-18 19:43:24 +0000 | [diff] [blame] | 33 | #include "llvm/Support/YAMLTraits.h" | 
| Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 34 | #include "llvm/Support/raw_ostream.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 | /// | 
| Zachary Turner | 8065f0b | 2017-12-01 00:53:10 +0000 | [diff] [blame] | 42 | static cl::opt<bool> Stats( | 
|  | 43 | "stats", | 
|  | 44 | cl::desc("Enable statistics output from program (available with Asserts)"), | 
|  | 45 | cl::Hidden); | 
| Chris Lattner | c758fe6 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 46 |  | 
| Matthias Braun | 98ea88b | 2016-06-15 20:19:16 +0000 | [diff] [blame] | 47 | static cl::opt<bool> StatsAsJSON("stats-json", | 
| Zachary Turner | 8065f0b | 2017-12-01 00:53:10 +0000 | [diff] [blame] | 48 | cl::desc("Display statistics as json data"), | 
|  | 49 | cl::Hidden); | 
| Matthias Braun | 98ea88b | 2016-06-15 20:19:16 +0000 | [diff] [blame] | 50 |  | 
| Matthias Braun | c603551 | 2016-09-26 18:38:07 +0000 | [diff] [blame] | 51 | static bool Enabled; | 
| Matthias Braun | 5391ffb | 2016-09-27 19:38:55 +0000 | [diff] [blame] | 52 | static bool PrintOnExit; | 
| Matthias Braun | c603551 | 2016-09-26 18:38:07 +0000 | [diff] [blame] | 53 |  | 
| Chris Lattner | 8c9969a | 2006-12-08 20:00:42 +0000 | [diff] [blame] | 54 | namespace { | 
| Daniel Sanders | a09751e | 2018-03-05 19:38:16 +0000 | [diff] [blame] | 55 | /// 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 Lattner | 8c9969a | 2006-12-08 20:00:42 +0000 | [diff] [blame] | 60 | class StatisticInfo { | 
| Daniel Sanders | 1087a54 | 2018-03-08 02:36:25 +0000 | [diff] [blame] | 61 | std::vector<Statistic*> Stats; | 
| Daniel Sanders | a09751e | 2018-03-05 19:38:16 +0000 | [diff] [blame] | 62 |  | 
| Douglas Gregor | 83fd015 | 2010-03-30 17:32:08 +0000 | [diff] [blame] | 63 | friend void llvm::PrintStatistics(); | 
|  | 64 | friend void llvm::PrintStatistics(raw_ostream &OS); | 
| Matthias Braun | 98ea88b | 2016-06-15 20:19:16 +0000 | [diff] [blame] | 65 | friend void llvm::PrintStatisticsJSON(raw_ostream &OS); | 
|  | 66 |  | 
|  | 67 | /// Sort statistics by debugtype,name,description. | 
|  | 68 | void sort(); | 
| Chris Lattner | 8c9969a | 2006-12-08 20:00:42 +0000 | [diff] [blame] | 69 | public: | 
| Daniel Sanders | 1087a54 | 2018-03-08 02:36:25 +0000 | [diff] [blame] | 70 | using const_iterator = std::vector<Statistic *>::const_iterator; | 
| Daniel Sanders | a09751e | 2018-03-05 19:38:16 +0000 | [diff] [blame] | 71 |  | 
| Matthias Braun | db39fd6 | 2016-11-18 19:43:24 +0000 | [diff] [blame] | 72 | StatisticInfo(); | 
| Chris Lattner | 8c9969a | 2006-12-08 20:00:42 +0000 | [diff] [blame] | 73 | ~StatisticInfo(); | 
| Jim Grosbach | a927736 | 2010-08-17 17:37:22 +0000 | [diff] [blame] | 74 |  | 
| Daniel Sanders | 1087a54 | 2018-03-08 02:36:25 +0000 | [diff] [blame] | 75 | void addStatistic(Statistic *S) { | 
| Chris Lattner | 8c9969a | 2006-12-08 20:00:42 +0000 | [diff] [blame] | 76 | Stats.push_back(S); | 
| Chris Lattner | c758fe6 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 77 | } | 
| Daniel Sanders | a09751e | 2018-03-05 19:38:16 +0000 | [diff] [blame] | 78 |  | 
|  | 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 Sanders | 1087a54 | 2018-03-08 02:36:25 +0000 | [diff] [blame] | 84 |  | 
|  | 85 | void reset(); | 
| Chris Lattner | 8c9969a | 2006-12-08 20:00:42 +0000 | [diff] [blame] | 86 | }; | 
| Daniel Sanders | a09751e | 2018-03-05 19:38:16 +0000 | [diff] [blame] | 87 | } // end anonymous namespace | 
| Chris Lattner | c758fe6 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 88 |  | 
| Chris Lattner | 8c9969a | 2006-12-08 20:00:42 +0000 | [diff] [blame] | 89 | static ManagedStatic<StatisticInfo> StatInfo; | 
| Torok Edwin | 819d15c | 2009-09-27 11:08:03 +0000 | [diff] [blame] | 90 | static ManagedStatic<sys::SmartMutex<true> > StatLock; | 
| Chris Lattner | 8c9969a | 2006-12-08 20:00:42 +0000 | [diff] [blame] | 91 |  | 
|  | 92 | /// RegisterStatistic - The first time a statistic is bumped, this method is | 
|  | 93 | /// called. | 
| Chris Lattner | 00bb216 | 2006-12-19 23:17:40 +0000 | [diff] [blame] | 94 | void Statistic::RegisterStatistic() { | 
| Chris Lattner | 8c9969a | 2006-12-08 20:00:42 +0000 | [diff] [blame] | 95 | // If stats are enabled, inform StatInfo that this statistic should be | 
|  | 96 | // printed. | 
| Bob Haarman | 37a9269 | 2018-04-17 23:37:18 +0000 | [diff] [blame] | 97 | // 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 Kramer | 351e9f3 | 2018-02-01 20:28:33 +0000 | [diff] [blame] | 103 | if (!Initialized.load(std::memory_order_relaxed)) { | 
| Bob Haarman | 37a9269 | 2018-04-17 23:37:18 +0000 | [diff] [blame] | 104 | 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 Braun | c603551 | 2016-09-26 18:38:07 +0000 | [diff] [blame] | 110 | if (Stats || Enabled) | 
| Bob Haarman | 37a9269 | 2018-04-17 23:37:18 +0000 | [diff] [blame] | 111 | SI.addStatistic(this); | 
| Jim Grosbach | a927736 | 2010-08-17 17:37:22 +0000 | [diff] [blame] | 112 |  | 
| Owen Anderson | ca8f986 | 2009-06-23 21:19:38 +0000 | [diff] [blame] | 113 | // Remember we have been registered. | 
| Benjamin Kramer | 351e9f3 | 2018-02-01 20:28:33 +0000 | [diff] [blame] | 114 | Initialized.store(true, std::memory_order_release); | 
| Owen Anderson | ca8f986 | 2009-06-23 21:19:38 +0000 | [diff] [blame] | 115 | } | 
| Chris Lattner | 8c9969a | 2006-12-08 20:00:42 +0000 | [diff] [blame] | 116 | } | 
|  | 117 |  | 
| Matthias Braun | db39fd6 | 2016-11-18 19:43:24 +0000 | [diff] [blame] | 118 | StatisticInfo::StatisticInfo() { | 
|  | 119 | // Ensure timergroup lists are created first so they are destructed after us. | 
|  | 120 | TimerGroup::ConstructTimerLists(); | 
|  | 121 | } | 
|  | 122 |  | 
| Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 123 | // Print information when destroyed, iff command line option is specified. | 
| Chris Lattner | 8c9969a | 2006-12-08 20:00:42 +0000 | [diff] [blame] | 124 | StatisticInfo::~StatisticInfo() { | 
| Matthias Braun | 5391ffb | 2016-09-27 19:38:55 +0000 | [diff] [blame] | 125 | if (::Stats || PrintOnExit) | 
| Matthias Braun | c603551 | 2016-09-26 18:38:07 +0000 | [diff] [blame] | 126 | llvm::PrintStatistics(); | 
| Douglas Gregor | 83fd015 | 2010-03-30 17:32:08 +0000 | [diff] [blame] | 127 | } | 
| Chris Lattner | c758fe6 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 128 |  | 
| Matthias Braun | 5391ffb | 2016-09-27 19:38:55 +0000 | [diff] [blame] | 129 | void llvm::EnableStatistics(bool PrintOnExit) { | 
| Matthias Braun | c603551 | 2016-09-26 18:38:07 +0000 | [diff] [blame] | 130 | Enabled = true; | 
| Matthias Braun | 5391ffb | 2016-09-27 19:38:55 +0000 | [diff] [blame] | 131 | ::PrintOnExit = PrintOnExit; | 
| Douglas Gregor | 83fd015 | 2010-03-30 17:32:08 +0000 | [diff] [blame] | 132 | } | 
|  | 133 |  | 
| Daniel Dunbar | 06dfe8e | 2011-02-26 23:17:12 +0000 | [diff] [blame] | 134 | bool llvm::AreStatisticsEnabled() { | 
| Matthias Braun | c603551 | 2016-09-26 18:38:07 +0000 | [diff] [blame] | 135 | return Enabled || Stats; | 
| Daniel Dunbar | 06dfe8e | 2011-02-26 23:17:12 +0000 | [diff] [blame] | 136 | } | 
|  | 137 |  | 
| Matthias Braun | 98ea88b | 2016-06-15 20:19:16 +0000 | [diff] [blame] | 138 | void 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 Sanders | 1087a54 | 2018-03-08 02:36:25 +0000 | [diff] [blame] | 151 | void 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 Gregor | 83fd015 | 2010-03-30 17:32:08 +0000 | [diff] [blame] | 173 | void llvm::PrintStatistics(raw_ostream &OS) { | 
|  | 174 | StatisticInfo &Stats = *StatInfo; | 
| Chris Lattner | c758fe6 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 175 |  | 
| Chris Lattner | 8c9969a | 2006-12-08 20:00:42 +0000 | [diff] [blame] | 176 | // Figure out how long the biggest Value and Name fields are. | 
| Matthias Braun | 98ea88b | 2016-06-15 20:19:16 +0000 | [diff] [blame] | 177 | unsigned MaxDebugTypeLen = 0, MaxValLen = 0; | 
| Douglas Gregor | 83fd015 | 2010-03-30 17:32:08 +0000 | [diff] [blame] | 178 | for (size_t i = 0, e = Stats.Stats.size(); i != e; ++i) { | 
| Chris Lattner | 8c9969a | 2006-12-08 20:00:42 +0000 | [diff] [blame] | 179 | MaxValLen = std::max(MaxValLen, | 
| Douglas Gregor | 83fd015 | 2010-03-30 17:32:08 +0000 | [diff] [blame] | 180 | (unsigned)utostr(Stats.Stats[i]->getValue()).size()); | 
| Matthias Braun | 98ea88b | 2016-06-15 20:19:16 +0000 | [diff] [blame] | 181 | MaxDebugTypeLen = std::max(MaxDebugTypeLen, | 
|  | 182 | (unsigned)std::strlen(Stats.Stats[i]->getDebugType())); | 
| Chris Lattner | c758fe6 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 183 | } | 
| Jim Grosbach | a927736 | 2010-08-17 17:37:22 +0000 | [diff] [blame] | 184 |  | 
| Matthias Braun | 98ea88b | 2016-06-15 20:19:16 +0000 | [diff] [blame] | 185 | Stats.sort(); | 
| Chris Lattner | c758fe6 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 186 |  | 
| Chris Lattner | 8c9969a | 2006-12-08 20:00:42 +0000 | [diff] [blame] | 187 | // Print out the statistics header... | 
| Douglas Gregor | 83fd015 | 2010-03-30 17:32:08 +0000 | [diff] [blame] | 188 | OS << "===" << std::string(73, '-') << "===\n" | 
|  | 189 | << "                          ... Statistics Collected ...\n" | 
|  | 190 | << "===" << std::string(73, '-') << "===\n\n"; | 
| Jim Grosbach | a927736 | 2010-08-17 17:37:22 +0000 | [diff] [blame] | 191 |  | 
| Chris Lattner | 8c9969a | 2006-12-08 20:00:42 +0000 | [diff] [blame] | 192 | // Print all of the statistics. | 
| Benjamin Kramer | cc863b2 | 2011-10-16 16:30:34 +0000 | [diff] [blame] | 193 | 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 Braun | 98ea88b | 2016-06-15 20:19:16 +0000 | [diff] [blame] | 196 | MaxDebugTypeLen, Stats.Stats[i]->getDebugType(), | 
| Benjamin Kramer | cc863b2 | 2011-10-16 16:30:34 +0000 | [diff] [blame] | 197 | Stats.Stats[i]->getDesc()); | 
| Jim Grosbach | a927736 | 2010-08-17 17:37:22 +0000 | [diff] [blame] | 198 |  | 
| Douglas Gregor | 83fd015 | 2010-03-30 17:32:08 +0000 | [diff] [blame] | 199 | OS << '\n';  // Flush the output stream. | 
|  | 200 | OS.flush(); | 
| Matthias Braun | 98ea88b | 2016-06-15 20:19:16 +0000 | [diff] [blame] | 201 | } | 
| Douglas Gregor | 83fd015 | 2010-03-30 17:32:08 +0000 | [diff] [blame] | 202 |  | 
| Matthias Braun | 98ea88b | 2016-06-15 20:19:16 +0000 | [diff] [blame] | 203 | void llvm::PrintStatisticsJSON(raw_ostream &OS) { | 
| Daniel Sanders | 0f4b015 | 2018-03-06 21:16:42 +0000 | [diff] [blame] | 204 | sys::SmartScopedLock<true> Reader(*StatLock); | 
| Matthias Braun | 98ea88b | 2016-06-15 20:19:16 +0000 | [diff] [blame] | 205 | 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 Mistrih | b213b27 | 2017-12-18 17:38:03 +0000 | [diff] [blame] | 214 | assert(yaml::needsQuotes(Stat->getDebugType()) == yaml::QuotingType::None && | 
| Matthias Braun | db39fd6 | 2016-11-18 19:43:24 +0000 | [diff] [blame] | 215 | "Statistic group/type name is simple."); | 
| Francis Visoiu Mistrih | b213b27 | 2017-12-18 17:38:03 +0000 | [diff] [blame] | 216 | assert(yaml::needsQuotes(Stat->getName()) == yaml::QuotingType::None && | 
|  | 217 | "Statistic name is simple"); | 
| Matthias Braun | db39fd6 | 2016-11-18 19:43:24 +0000 | [diff] [blame] | 218 | OS << "\t\"" << Stat->getDebugType() << '.' << Stat->getName() << "\": " | 
|  | 219 | << Stat->getValue(); | 
| Matthias Braun | 98ea88b | 2016-06-15 20:19:16 +0000 | [diff] [blame] | 220 | delim = ",\n"; | 
|  | 221 | } | 
| Matthias Braun | db39fd6 | 2016-11-18 19:43:24 +0000 | [diff] [blame] | 222 | // Print timers. | 
|  | 223 | TimerGroup::printAllJSONValues(OS, delim); | 
|  | 224 |  | 
| Matthias Braun | 98ea88b | 2016-06-15 20:19:16 +0000 | [diff] [blame] | 225 | OS << "\n}\n"; | 
|  | 226 | OS.flush(); | 
| Douglas Gregor | 83fd015 | 2010-03-30 17:32:08 +0000 | [diff] [blame] | 227 | } | 
|  | 228 |  | 
|  | 229 | void llvm::PrintStatistics() { | 
| Daniel Sanders | a09751e | 2018-03-05 19:38:16 +0000 | [diff] [blame] | 230 | #if LLVM_ENABLE_STATS | 
| Daniel Sanders | 0f4b015 | 2018-03-06 21:16:42 +0000 | [diff] [blame] | 231 | sys::SmartScopedLock<true> Reader(*StatLock); | 
| Douglas Gregor | 83fd015 | 2010-03-30 17:32:08 +0000 | [diff] [blame] | 232 | StatisticInfo &Stats = *StatInfo; | 
|  | 233 |  | 
|  | 234 | // Statistics not enabled? | 
|  | 235 | if (Stats.Stats.empty()) return; | 
|  | 236 |  | 
|  | 237 | // Get the stream to write to. | 
| Rafael Espindola | b94ab5f | 2015-12-16 22:28:34 +0000 | [diff] [blame] | 238 | std::unique_ptr<raw_ostream> OutStream = CreateInfoOutputFile(); | 
| Matthias Braun | 98ea88b | 2016-06-15 20:19:16 +0000 | [diff] [blame] | 239 | if (StatsAsJSON) | 
|  | 240 | PrintStatisticsJSON(*OutStream); | 
|  | 241 | else | 
|  | 242 | PrintStatistics(*OutStream); | 
| Rafael Espindola | b94ab5f | 2015-12-16 22:28:34 +0000 | [diff] [blame] | 243 |  | 
| Jan Wen Voung | 7857a64 | 2013-03-08 22:56:31 +0000 | [diff] [blame] | 244 | #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 Braun | c603551 | 2016-09-26 18:38:07 +0000 | [diff] [blame] | 248 | if (Stats) { | 
| Jan Wen Voung | 7857a64 | 2013-03-08 22:56:31 +0000 | [diff] [blame] | 249 | // Get the stream to write to. | 
| Rafael Espindola | b94ab5f | 2015-12-16 22:28:34 +0000 | [diff] [blame] | 250 | std::unique_ptr<raw_ostream> OutStream = CreateInfoOutputFile(); | 
|  | 251 | (*OutStream) << "Statistics are disabled.  " | 
|  | 252 | << "Build with asserts or with -DLLVM_ENABLE_STATS\n"; | 
| Jan Wen Voung | 7857a64 | 2013-03-08 22:56:31 +0000 | [diff] [blame] | 253 | } | 
|  | 254 | #endif | 
| Chris Lattner | 9eb0052 | 2002-05-10 15:36:46 +0000 | [diff] [blame] | 255 | } | 
| Daniel Sanders | a09751e | 2018-03-05 19:38:16 +0000 | [diff] [blame] | 256 |  | 
|  | 257 | const 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 Sanders | 1087a54 | 2018-03-08 02:36:25 +0000 | [diff] [blame] | 265 |  | 
|  | 266 | void llvm::ResetStatistics() { | 
|  | 267 | StatInfo->reset(); | 
|  | 268 | } |