Statistic.cpp revision 206083
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/Support/CommandLine.h" 26#include "llvm/Support/Debug.h" 27#include "llvm/Support/ManagedStatic.h" 28#include "llvm/Support/raw_ostream.h" 29#include "llvm/System/Mutex.h" 30#include "llvm/ADT/StringExtras.h" 31#include <algorithm> 32#include <cstring> 33using namespace llvm; 34 35// CreateInfoOutputFile - Return a file stream to print our output on. 36namespace llvm { extern raw_ostream *CreateInfoOutputFile(); } 37 38/// -stats - Command line option to cause transformations to emit stats about 39/// what they did. 40/// 41static cl::opt<bool> 42Enabled("stats", cl::desc("Enable statistics output from program")); 43 44 45namespace { 46/// StatisticInfo - This class is used in a ManagedStatic so that it is created 47/// on demand (when the first statistic is bumped) and destroyed only when 48/// llvm_shutdown is called. We print statistics from the destructor. 49class StatisticInfo { 50 std::vector<const Statistic*> Stats; 51 friend void llvm::PrintStatistics(); 52 friend void llvm::PrintStatistics(raw_ostream &OS); 53public: 54 ~StatisticInfo(); 55 56 void addStatistic(const Statistic *S) { 57 Stats.push_back(S); 58 } 59}; 60} 61 62static ManagedStatic<StatisticInfo> StatInfo; 63static ManagedStatic<sys::SmartMutex<true> > StatLock; 64 65/// RegisterStatistic - The first time a statistic is bumped, this method is 66/// called. 67void Statistic::RegisterStatistic() { 68 // If stats are enabled, inform StatInfo that this statistic should be 69 // printed. 70 sys::SmartScopedLock<true> Writer(*StatLock); 71 if (!Initialized) { 72 if (Enabled) 73 StatInfo->addStatistic(this); 74 75 sys::MemoryFence(); 76 // Remember we have been registered. 77 Initialized = true; 78 } 79} 80 81namespace { 82 83struct NameCompare { 84 bool operator()(const Statistic *LHS, const Statistic *RHS) const { 85 int Cmp = std::strcmp(LHS->getName(), RHS->getName()); 86 if (Cmp != 0) return Cmp < 0; 87 88 // Secondary key is the description. 89 return std::strcmp(LHS->getDesc(), RHS->getDesc()) < 0; 90 } 91}; 92 93} 94 95// Print information when destroyed, iff command line option is specified. 96StatisticInfo::~StatisticInfo() { 97 llvm::PrintStatistics(); 98} 99 100void llvm::EnableStatistics() { 101 Enabled.setValue(true); 102} 103 104void llvm::PrintStatistics(raw_ostream &OS) { 105 StatisticInfo &Stats = *StatInfo; 106 107 // Figure out how long the biggest Value and Name fields are. 108 unsigned MaxNameLen = 0, MaxValLen = 0; 109 for (size_t i = 0, e = Stats.Stats.size(); i != e; ++i) { 110 MaxValLen = std::max(MaxValLen, 111 (unsigned)utostr(Stats.Stats[i]->getValue()).size()); 112 MaxNameLen = std::max(MaxNameLen, 113 (unsigned)std::strlen(Stats.Stats[i]->getName())); 114 } 115 116 // Sort the fields by name. 117 std::stable_sort(Stats.Stats.begin(), Stats.Stats.end(), NameCompare()); 118 119 // Print out the statistics header... 120 OS << "===" << std::string(73, '-') << "===\n" 121 << " ... Statistics Collected ...\n" 122 << "===" << std::string(73, '-') << "===\n\n"; 123 124 // Print all of the statistics. 125 for (size_t i = 0, e = Stats.Stats.size(); i != e; ++i) { 126 std::string CountStr = utostr(Stats.Stats[i]->getValue()); 127 OS << std::string(MaxValLen-CountStr.size(), ' ') 128 << CountStr << " " << Stats.Stats[i]->getName() 129 << std::string(MaxNameLen-std::strlen(Stats.Stats[i]->getName()), ' ') 130 << " - " << Stats.Stats[i]->getDesc() << "\n"; 131 } 132 133 OS << '\n'; // Flush the output stream. 134 OS.flush(); 135 136} 137 138void llvm::PrintStatistics() { 139 StatisticInfo &Stats = *StatInfo; 140 141 // Statistics not enabled? 142 if (Stats.Stats.empty()) return; 143 144 // Get the stream to write to. 145 raw_ostream &OutStream = *CreateInfoOutputFile(); 146 PrintStatistics(OutStream); 147 delete &OutStream; // Close the file. 148} 149