LLVM API Documentation

Statistic.cpp

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