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/Support/CommandLine.h" 00026 #include "llvm/Support/ManagedStatic.h" 00027 #include "llvm/Support/Streams.h" 00028 #include "llvm/ADT/StringExtras.h" 00029 #include <algorithm> 00030 #include <ostream> 00031 #include <cstring> 00032 using namespace llvm; 00033 00034 // GetLibSupportInfoOutputFile - Return a file stream to print our output on. 00035 namespace llvm { extern std::ostream *GetLibSupportInfoOutputFile(); } 00036 00037 /// -stats - Command line option to cause transformations to emit stats about 00038 /// what they did. 00039 /// 00040 static cl::opt<bool> 00041 Enabled("stats", cl::desc("Enable statistics output from program")); 00042 00043 00044 namespace { 00045 /// StatisticInfo - This class is used in a ManagedStatic so that it is created 00046 /// on demand (when the first statistic is bumped) and destroyed only when 00047 /// llvm_shutdown is called. We print statistics from the destructor. 00048 class StatisticInfo { 00049 std::vector<const Statistic*> Stats; 00050 public: 00051 ~StatisticInfo(); 00052 00053 void addStatistic(const Statistic *S) { 00054 Stats.push_back(S); 00055 } 00056 }; 00057 } 00058 00059 static ManagedStatic<StatisticInfo> StatInfo; 00060 00061 00062 /// RegisterStatistic - The first time a statistic is bumped, this method is 00063 /// called. 00064 void Statistic::RegisterStatistic() { 00065 // If stats are enabled, inform StatInfo that this statistic should be 00066 // printed. 00067 if (Enabled) 00068 StatInfo->addStatistic(this); 00069 // Remember we have been registered. 00070 Initialized = true; 00071 } 00072 00073 namespace { 00074 00075 struct NameCompare { 00076 bool operator()(const Statistic *LHS, const Statistic *RHS) const { 00077 int Cmp = std::strcmp(LHS->getName(), RHS->getName()); 00078 if (Cmp != 0) return Cmp < 0; 00079 00080 // Secondary key is the description. 00081 return std::strcmp(LHS->getDesc(), RHS->getDesc()) < 0; 00082 } 00083 }; 00084 00085 } 00086 00087 // Print information when destroyed, iff command line option is specified. 00088 StatisticInfo::~StatisticInfo() { 00089 // Statistics not enabled? 00090 if (Stats.empty()) return; 00091 00092 // Get the stream to write to. 00093 std::ostream &OutStream = *GetLibSupportInfoOutputFile(); 00094 00095 // Figure out how long the biggest Value and Name fields are. 00096 unsigned MaxNameLen = 0, MaxValLen = 0; 00097 for (size_t i = 0, e = Stats.size(); i != e; ++i) { 00098 MaxValLen = std::max(MaxValLen, 00099 (unsigned)utostr(Stats[i]->getValue()).size()); 00100 MaxNameLen = std::max(MaxNameLen, 00101 (unsigned)std::strlen(Stats[i]->getName())); 00102 } 00103 00104 // Sort the fields by name. 00105 std::stable_sort(Stats.begin(), Stats.end(), NameCompare()); 00106 00107 // Print out the statistics header... 00108 OutStream << "===" << std::string(73, '-') << "===\n" 00109 << " ... Statistics Collected ...\n" 00110 << "===" << std::string(73, '-') << "===\n\n"; 00111 00112 // Print all of the statistics. 00113 for (size_t i = 0, e = Stats.size(); i != e; ++i) { 00114 std::string CountStr = utostr(Stats[i]->getValue()); 00115 OutStream << std::string(MaxValLen-CountStr.size(), ' ') 00116 << CountStr << " " << Stats[i]->getName() 00117 << std::string(MaxNameLen-std::strlen(Stats[i]->getName()), ' ') 00118 << " - " << Stats[i]->getDesc() << "\n"; 00119 00120 } 00121 00122 OutStream << std::endl; // Flush the output stream... 00123 00124 if (&OutStream != cerr.stream() && &OutStream != cout.stream()) 00125 delete &OutStream; // Close the file. 00126 }