Statistic.cpp revision 221345
10Sstevel@tonic-gate//===-- Statistic.cpp - Easy way to expose stats information --------------===//
20Sstevel@tonic-gate//
30Sstevel@tonic-gate//                     The LLVM Compiler Infrastructure
40Sstevel@tonic-gate//
50Sstevel@tonic-gate// This file is distributed under the University of Illinois Open Source
60Sstevel@tonic-gate// License. See LICENSE.TXT for details.
70Sstevel@tonic-gate//
80Sstevel@tonic-gate//===----------------------------------------------------------------------===//
90Sstevel@tonic-gate//
100Sstevel@tonic-gate// This file implements the 'Statistic' class, which is designed to be an easy
110Sstevel@tonic-gate// way to expose various success metrics from passes.  These statistics are
120Sstevel@tonic-gate// printed at the end of a run, when the -stats command line option is enabled
130Sstevel@tonic-gate// on the command line.
140Sstevel@tonic-gate//
150Sstevel@tonic-gate// This is useful for reporting information like the number of instructions
160Sstevel@tonic-gate// simplified, optimized or removed by various transformations, like this:
170Sstevel@tonic-gate//
180Sstevel@tonic-gate// static Statistic NumInstEliminated("GCSE", "Number of instructions killed");
190Sstevel@tonic-gate//
200Sstevel@tonic-gate// Later, in the code: ++NumInstEliminated;
210Sstevel@tonic-gate//
220Sstevel@tonic-gate//===----------------------------------------------------------------------===//
230Sstevel@tonic-gate
240Sstevel@tonic-gate#include "llvm/ADT/Statistic.h"
250Sstevel@tonic-gate#include "llvm/Support/CommandLine.h"
260Sstevel@tonic-gate#include "llvm/Support/Debug.h"
270Sstevel@tonic-gate#include "llvm/Support/ManagedStatic.h"
280Sstevel@tonic-gate#include "llvm/Support/raw_ostream.h"
290Sstevel@tonic-gate#include "llvm/Support/Mutex.h"
300Sstevel@tonic-gate#include "llvm/ADT/StringExtras.h"
310Sstevel@tonic-gate#include <algorithm>
320Sstevel@tonic-gate#include <cstring>
330Sstevel@tonic-gateusing namespace llvm;
340Sstevel@tonic-gate
350Sstevel@tonic-gate// CreateInfoOutputFile - Return a file stream to print our output on.
360Sstevel@tonic-gatenamespace llvm { extern raw_ostream *CreateInfoOutputFile(); }
370Sstevel@tonic-gate
380Sstevel@tonic-gate/// -stats - Command line option to cause transformations to emit stats about
390Sstevel@tonic-gate/// what they did.
400Sstevel@tonic-gate///
410Sstevel@tonic-gatestatic cl::opt<bool>
420Sstevel@tonic-gateEnabled("stats", cl::desc("Enable statistics output from program"));
430Sstevel@tonic-gate
440Sstevel@tonic-gate
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
104bool llvm::AreStatisticsEnabled() {
105  return Enabled;
106}
107
108void llvm::PrintStatistics(raw_ostream &OS) {
109  StatisticInfo &Stats = *StatInfo;
110
111  // Figure out how long the biggest Value and Name fields are.
112  unsigned MaxNameLen = 0, MaxValLen = 0;
113  for (size_t i = 0, e = Stats.Stats.size(); i != e; ++i) {
114    MaxValLen = std::max(MaxValLen,
115                         (unsigned)utostr(Stats.Stats[i]->getValue()).size());
116    MaxNameLen = std::max(MaxNameLen,
117                          (unsigned)std::strlen(Stats.Stats[i]->getName()));
118  }
119
120  // Sort the fields by name.
121  std::stable_sort(Stats.Stats.begin(), Stats.Stats.end(), NameCompare());
122
123  // Print out the statistics header...
124  OS << "===" << std::string(73, '-') << "===\n"
125     << "                          ... Statistics Collected ...\n"
126     << "===" << std::string(73, '-') << "===\n\n";
127
128  // Print all of the statistics.
129  for (size_t i = 0, e = Stats.Stats.size(); i != e; ++i) {
130    std::string CountStr = utostr(Stats.Stats[i]->getValue());
131    OS << std::string(MaxValLen-CountStr.size(), ' ')
132       << CountStr << " " << Stats.Stats[i]->getName()
133       << std::string(MaxNameLen-std::strlen(Stats.Stats[i]->getName()), ' ')
134       << " - " << Stats.Stats[i]->getDesc() << "\n";
135  }
136
137  OS << '\n';  // Flush the output stream.
138  OS.flush();
139
140}
141
142void llvm::PrintStatistics() {
143  StatisticInfo &Stats = *StatInfo;
144
145  // Statistics not enabled?
146  if (Stats.Stats.empty()) return;
147
148  // Get the stream to write to.
149  raw_ostream &OutStream = *CreateInfoOutputFile();
150  PrintStatistics(OutStream);
151  delete &OutStream;   // Close the file.
152}
153