1 //===-- Statistic.cpp - Easy way to expose stats information --------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 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 // 18 // static Statistic NumInstEliminated("GCSE", "Number of instructions killed"); 19 // 20 // Later, in the code: ++NumInstEliminated; 21 // 22 //===----------------------------------------------------------------------===// 23 24 #include "llvm/ADT/Statistic.h" 25 #include "llvm/ADT/StringExtras.h" 26 #include "llvm/Support/CommandLine.h" 27 #include "llvm/Support/Compiler.h" 28 #include "llvm/Support/Debug.h" 29 #include "llvm/Support/Format.h" 30 #include "llvm/Support/ManagedStatic.h" 31 #include "llvm/Support/Mutex.h" 32 #include "llvm/Support/raw_ostream.h" 33 #include <algorithm> 34 #include <cstring> 35 using namespace llvm; 36 37 /// -stats - Command line option to cause transformations to emit stats about 38 /// what they did. 39 /// 40 static cl::opt<bool> Stats("stats", 41 cl::desc("Enable statistics output from program (available with Asserts)")); 42 43 44 static cl::opt<bool> StatsAsJSON("stats-json", 45 cl::desc("Display statistics as json data")); 46 47 static bool Enabled; 48 static bool PrintOnExit; 49 50 namespace { 51 /// StatisticInfo - This class is used in a ManagedStatic so that it is created 52 /// on demand (when the first statistic is bumped) and destroyed only when 53 /// llvm_shutdown is called. We print statistics from the destructor. 54 class StatisticInfo { 55 std::vector<const Statistic*> Stats; 56 friend void llvm::PrintStatistics(); 57 friend void llvm::PrintStatistics(raw_ostream &OS); 58 friend void llvm::PrintStatisticsJSON(raw_ostream &OS); 59 60 /// Sort statistics by debugtype,name,description. 61 void sort(); 62 public: 63 ~StatisticInfo(); 64 65 void addStatistic(const Statistic *S) { 66 Stats.push_back(S); 67 } 68 }; 69 } 70 71 static ManagedStatic<StatisticInfo> StatInfo; 72 static ManagedStatic<sys::SmartMutex<true> > StatLock; 73 74 /// RegisterStatistic - The first time a statistic is bumped, this method is 75 /// called. 76 void Statistic::RegisterStatistic() { 77 // If stats are enabled, inform StatInfo that this statistic should be 78 // printed. 79 sys::SmartScopedLock<true> Writer(*StatLock); 80 if (!Initialized) { 81 if (Stats || Enabled) 82 StatInfo->addStatistic(this); 83 84 TsanHappensBefore(this); 85 sys::MemoryFence(); 86 // Remember we have been registered. 87 TsanIgnoreWritesBegin(); 88 Initialized = true; 89 TsanIgnoreWritesEnd(); 90 } 91 } 92 93 // Print information when destroyed, iff command line option is specified. 94 StatisticInfo::~StatisticInfo() { 95 if (::Stats || PrintOnExit) 96 llvm::PrintStatistics(); 97 } 98 99 void llvm::EnableStatistics(bool PrintOnExit) { 100 Enabled = true; 101 ::PrintOnExit = PrintOnExit; 102 } 103 104 bool llvm::AreStatisticsEnabled() { 105 return Enabled || Stats; 106 } 107 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 121 void llvm::PrintStatistics(raw_ostream &OS) { 122 StatisticInfo &Stats = *StatInfo; 123 124 // Figure out how long the biggest Value and Name fields are. 125 unsigned MaxDebugTypeLen = 0, MaxValLen = 0; 126 for (size_t i = 0, e = Stats.Stats.size(); i != e; ++i) { 127 MaxValLen = std::max(MaxValLen, 128 (unsigned)utostr(Stats.Stats[i]->getValue()).size()); 129 MaxDebugTypeLen = std::max(MaxDebugTypeLen, 130 (unsigned)std::strlen(Stats.Stats[i]->getDebugType())); 131 } 132 133 Stats.sort(); 134 135 // Print out the statistics header... 136 OS << "===" << std::string(73, '-') << "===\n" 137 << " ... Statistics Collected ...\n" 138 << "===" << std::string(73, '-') << "===\n\n"; 139 140 // Print all of the statistics. 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(), 144 MaxDebugTypeLen, Stats.Stats[i]->getDebugType(), 145 Stats.Stats[i]->getDesc()); 146 147 OS << '\n'; // Flush the output stream. 148 OS.flush(); 149 } 150 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(); 181 } 182 183 void llvm::PrintStatistics() { 184 #if !defined(NDEBUG) || defined(LLVM_ENABLE_STATS) 185 StatisticInfo &Stats = *StatInfo; 186 187 // Statistics not enabled? 188 if (Stats.Stats.empty()) return; 189 190 // Get the stream to write to. 191 std::unique_ptr<raw_ostream> OutStream = CreateInfoOutputFile(); 192 if (StatsAsJSON) 193 PrintStatisticsJSON(*OutStream); 194 else 195 PrintStatistics(*OutStream); 196 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. 201 if (Stats) { 202 // Get the stream to write to. 203 std::unique_ptr<raw_ostream> OutStream = CreateInfoOutputFile(); 204 (*OutStream) << "Statistics are disabled. " 205 << "Build with asserts or with -DLLVM_ENABLE_STATS\n"; 206 } 207 #endif 208 } 209