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