LLVM API Documentation
00001 //===-- Statistic.cpp - Easy way to expose stats information --------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements the 'Statistic' class, which is designed to be an easy 00011 // way to expose various success metrics from passes. These statistics are 00012 // printed at the end of a run, when the -stats command line option is enabled 00013 // on the command line. 00014 // 00015 // This is useful for reporting information like the number of instructions 00016 // simplified, optimized or removed by various transformations, like this: 00017 // 00018 // static Statistic NumInstEliminated("GCSE", "Number of instructions killed"); 00019 // 00020 // Later, in the code: ++NumInstEliminated; 00021 // 00022 //===----------------------------------------------------------------------===// 00023 00024 #include "llvm/ADT/Statistic.h" 00025 #include "llvm/ADT/StringExtras.h" 00026 #include "llvm/Support/CommandLine.h" 00027 #include "llvm/Support/Debug.h" 00028 #include "llvm/Support/Format.h" 00029 #include "llvm/Support/ManagedStatic.h" 00030 #include "llvm/Support/Mutex.h" 00031 #include "llvm/Support/raw_ostream.h" 00032 #include <algorithm> 00033 #include <cstring> 00034 using namespace llvm; 00035 00036 // CreateInfoOutputFile - Return a file stream to print our output on. 00037 namespace llvm { extern raw_ostream *CreateInfoOutputFile(); } 00038 00039 /// -stats - Command line option to cause transformations to emit stats about 00040 /// what they did. 00041 /// 00042 static cl::opt<bool> 00043 Enabled( 00044 "stats", 00045 cl::desc("Enable statistics output from program (available with Asserts)")); 00046 00047 00048 namespace { 00049 /// StatisticInfo - This class is used in a ManagedStatic so that it is created 00050 /// on demand (when the first statistic is bumped) and destroyed only when 00051 /// llvm_shutdown is called. We print statistics from the destructor. 00052 class StatisticInfo { 00053 std::vector<const Statistic*> Stats; 00054 friend void llvm::PrintStatistics(); 00055 friend void llvm::PrintStatistics(raw_ostream &OS); 00056 public: 00057 ~StatisticInfo(); 00058 00059 void addStatistic(const Statistic *S) { 00060 Stats.push_back(S); 00061 } 00062 }; 00063 } 00064 00065 static ManagedStatic<StatisticInfo> StatInfo; 00066 static ManagedStatic<sys::SmartMutex<true> > StatLock; 00067 00068 /// RegisterStatistic - The first time a statistic is bumped, this method is 00069 /// called. 00070 void Statistic::RegisterStatistic() { 00071 // If stats are enabled, inform StatInfo that this statistic should be 00072 // printed. 00073 sys::SmartScopedLock<true> Writer(*StatLock); 00074 if (!Initialized) { 00075 if (Enabled) 00076 StatInfo->addStatistic(this); 00077 00078 TsanHappensBefore(this); 00079 sys::MemoryFence(); 00080 // Remember we have been registered. 00081 TsanIgnoreWritesBegin(); 00082 Initialized = true; 00083 TsanIgnoreWritesEnd(); 00084 } 00085 } 00086 00087 // Print information when destroyed, iff command line option is specified. 00088 StatisticInfo::~StatisticInfo() { 00089 llvm::PrintStatistics(); 00090 } 00091 00092 void llvm::EnableStatistics() { 00093 Enabled.setValue(true); 00094 } 00095 00096 bool llvm::AreStatisticsEnabled() { 00097 return Enabled; 00098 } 00099 00100 void llvm::PrintStatistics(raw_ostream &OS) { 00101 StatisticInfo &Stats = *StatInfo; 00102 00103 // Figure out how long the biggest Value and Name fields are. 00104 unsigned MaxNameLen = 0, MaxValLen = 0; 00105 for (size_t i = 0, e = Stats.Stats.size(); i != e; ++i) { 00106 MaxValLen = std::max(MaxValLen, 00107 (unsigned)utostr(Stats.Stats[i]->getValue()).size()); 00108 MaxNameLen = std::max(MaxNameLen, 00109 (unsigned)std::strlen(Stats.Stats[i]->getName())); 00110 } 00111 00112 // Sort the fields by name. 00113 std::stable_sort(Stats.Stats.begin(), Stats.Stats.end(), 00114 [](const Statistic *LHS, const Statistic *RHS) { 00115 if (int Cmp = std::strcmp(LHS->getName(), RHS->getName())) 00116 return Cmp < 0; 00117 00118 // Secondary key is the description. 00119 return std::strcmp(LHS->getDesc(), RHS->getDesc()) < 0; 00120 }); 00121 00122 // Print out the statistics header... 00123 OS << "===" << std::string(73, '-') << "===\n" 00124 << " ... Statistics Collected ...\n" 00125 << "===" << std::string(73, '-') << "===\n\n"; 00126 00127 // Print all of the statistics. 00128 for (size_t i = 0, e = Stats.Stats.size(); i != e; ++i) 00129 OS << format("%*u %-*s - %s\n", 00130 MaxValLen, Stats.Stats[i]->getValue(), 00131 MaxNameLen, Stats.Stats[i]->getName(), 00132 Stats.Stats[i]->getDesc()); 00133 00134 OS << '\n'; // Flush the output stream. 00135 OS.flush(); 00136 00137 } 00138 00139 void llvm::PrintStatistics() { 00140 #if !defined(NDEBUG) || defined(LLVM_ENABLE_STATS) 00141 StatisticInfo &Stats = *StatInfo; 00142 00143 // Statistics not enabled? 00144 if (Stats.Stats.empty()) return; 00145 00146 // Get the stream to write to. 00147 raw_ostream &OutStream = *CreateInfoOutputFile(); 00148 PrintStatistics(OutStream); 00149 delete &OutStream; // Close the file. 00150 #else 00151 // Check if the -stats option is set instead of checking 00152 // !Stats.Stats.empty(). In release builds, Statistics operators 00153 // do nothing, so stats are never Registered. 00154 if (Enabled) { 00155 // Get the stream to write to. 00156 raw_ostream &OutStream = *CreateInfoOutputFile(); 00157 OutStream << "Statistics are disabled. " 00158 << "Build with asserts or with -DLLVM_ENABLE_STATS\n"; 00159 OutStream.flush(); 00160 delete &OutStream; // Close the file. 00161 } 00162 #endif 00163 }