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 49 namespace { 50 /// StatisticInfo - This class is used in a ManagedStatic so that it is created 51 /// on demand (when the first statistic is bumped) and destroyed only when 52 /// llvm_shutdown is called. We print statistics from the destructor. 53 class StatisticInfo { 54 std::vector<const Statistic*> Stats; 55 friend void llvm::PrintStatistics(); 56 friend void llvm::PrintStatistics(raw_ostream &OS); 57 friend void llvm::PrintStatisticsJSON(raw_ostream &OS); 58 59 /// Sort statistics by debugtype,name,description. 60 void sort(); 61 public: 62 ~StatisticInfo(); 63 64 void addStatistic(const Statistic *S) { 65 Stats.push_back(S); 66 } 67 }; 68 } 69 70 static ManagedStatic<StatisticInfo> StatInfo; 71 static ManagedStatic<sys::SmartMutex<true> > StatLock; 72 73 /// RegisterStatistic - The first time a statistic is bumped, this method is 74 /// called. 75 void Statistic::RegisterStatistic() { 76 // If stats are enabled, inform StatInfo that this statistic should be 77 // printed. 78 sys::SmartScopedLock<true> Writer(*StatLock); 79 if (!Initialized) { 80 if (Stats || Enabled) 81 StatInfo->addStatistic(this); 82 83 TsanHappensBefore(this); 84 sys::MemoryFence(); 85 // Remember we have been registered. 86 TsanIgnoreWritesBegin(); 87 Initialized = true; 88 TsanIgnoreWritesEnd(); 89 } 90 } 91 92 // Print information when destroyed, iff command line option is specified. 93 StatisticInfo::~StatisticInfo() { 94 if (::Stats) 95 llvm::PrintStatistics(); 96 } 97 98 void llvm::EnableStatistics() { 99 Enabled = true; 100 } 101 102 bool llvm::AreStatisticsEnabled() { 103 return Enabled || Stats; 104 } 105 106 void StatisticInfo::sort() { 107 std::stable_sort(Stats.begin(), Stats.end(), 108 [](const Statistic *LHS, const Statistic *RHS) { 109 if (int Cmp = std::strcmp(LHS->getDebugType(), RHS->getDebugType())) 110 return Cmp < 0; 111 112 if (int Cmp = std::strcmp(LHS->getName(), RHS->getName())) 113 return Cmp < 0; 114 115 return std::strcmp(LHS->getDesc(), RHS->getDesc()) < 0; 116 }); 117 } 118 119 void llvm::PrintStatistics(raw_ostream &OS) { 120 StatisticInfo &Stats = *StatInfo; 121 122 // Figure out how long the biggest Value and Name fields are. 123 unsigned MaxDebugTypeLen = 0, MaxValLen = 0; 124 for (size_t i = 0, e = Stats.Stats.size(); i != e; ++i) { 125 MaxValLen = std::max(MaxValLen, 126 (unsigned)utostr(Stats.Stats[i]->getValue()).size()); 127 MaxDebugTypeLen = std::max(MaxDebugTypeLen, 128 (unsigned)std::strlen(Stats.Stats[i]->getDebugType())); 129 } 130 131 Stats.sort(); 132 133 // Print out the statistics header... 134 OS << "===" << std::string(73, '-') << "===\n" 135 << " ... Statistics Collected ...\n" 136 << "===" << std::string(73, '-') << "===\n\n"; 137 138 // Print all of the statistics. 139 for (size_t i = 0, e = Stats.Stats.size(); i != e; ++i) 140 OS << format("%*u %-*s - %s\n", 141 MaxValLen, Stats.Stats[i]->getValue(), 142 MaxDebugTypeLen, Stats.Stats[i]->getDebugType(), 143 Stats.Stats[i]->getDesc()); 144 145 OS << '\n'; // Flush the output stream. 146 OS.flush(); 147 } 148 149 static void write_json_string_escaped(raw_ostream &OS, const char *string) { 150 // Out current usage should not need any escaping. Keep it simple and just 151 // check that the input is pure ASCII without special characers. 152 #ifndef NDEBUG 153 for (const unsigned char *c = (const unsigned char*)string; *c != '\0'; ++c) { 154 assert(*c != '\\' && *c != '\"' && *c >= 0x20 && *c < 0x80); 155 } 156 #endif 157 OS << string; 158 } 159 160 void llvm::PrintStatisticsJSON(raw_ostream &OS) { 161 StatisticInfo &Stats = *StatInfo; 162 163 Stats.sort(); 164 165 // Print all of the statistics. 166 OS << "{\n"; 167 const char *delim = ""; 168 for (const Statistic *Stat : Stats.Stats) { 169 OS << delim; 170 OS << "\t\""; 171 write_json_string_escaped(OS, Stat->getDebugType()); 172 OS << '.'; 173 write_json_string_escaped(OS, Stat->getName()); 174 OS << "\": " << Stat->getValue(); 175 delim = ",\n"; 176 } 177 OS << "\n}\n"; 178 OS.flush(); 179 } 180 181 void llvm::PrintStatistics() { 182 #if !defined(NDEBUG) || defined(LLVM_ENABLE_STATS) 183 StatisticInfo &Stats = *StatInfo; 184 185 // Statistics not enabled? 186 if (Stats.Stats.empty()) return; 187 188 // Get the stream to write to. 189 std::unique_ptr<raw_ostream> OutStream = CreateInfoOutputFile(); 190 if (StatsAsJSON) 191 PrintStatisticsJSON(*OutStream); 192 else 193 PrintStatistics(*OutStream); 194 195 #else 196 // Check if the -stats option is set instead of checking 197 // !Stats.Stats.empty(). In release builds, Statistics operators 198 // do nothing, so stats are never Registered. 199 if (Stats) { 200 // Get the stream to write to. 201 std::unique_ptr<raw_ostream> OutStream = CreateInfoOutputFile(); 202 (*OutStream) << "Statistics are disabled. " 203 << "Build with asserts or with -DLLVM_ENABLE_STATS\n"; 204 } 205 #endif 206 } 207