blob: e7876704599821bce5cf08088022c6b96b9368cf [file] [log] [blame]
Chris Lattner96ef1b92002-10-01 22:35:45 +00001//===-- Statistic.cpp - Easy way to expose stats information --------------===//
Misha Brukmanf976c852005-04-21 22:55:34 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanf976c852005-04-21 22:55:34 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnerf3f4fd52002-05-10 15:36:46 +00009//
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//
Chris Lattner975f0582006-12-08 20:00:42 +000018// static Statistic NumInstEliminated("GCSE", "Number of instructions killed");
Chris Lattnerf3f4fd52002-05-10 15:36:46 +000019//
20// Later, in the code: ++NumInstEliminated;
21//
22//===----------------------------------------------------------------------===//
23
Reid Spencer551ccae2004-09-01 22:55:40 +000024#include "llvm/ADT/Statistic.h"
25#include "llvm/Support/CommandLine.h"
David Greene8061a442010-01-05 01:28:47 +000026#include "llvm/Support/Debug.h"
Chris Lattner975f0582006-12-08 20:00:42 +000027#include "llvm/Support/ManagedStatic.h"
Chris Lattnerd9ea85a2009-08-23 08:43:55 +000028#include "llvm/Support/raw_ostream.h"
Owen Anderson5de83af2009-06-22 23:08:27 +000029#include "llvm/System/Mutex.h"
Chris Lattnercf845042006-12-06 18:20:44 +000030#include "llvm/ADT/StringExtras.h"
Chris Lattnera4ca41e2003-08-13 21:32:37 +000031#include <algorithm>
Anton Korobeynikovae9f3a32008-02-20 11:08:44 +000032#include <cstring>
Chris Lattnerb6d465f2003-12-14 21:27:33 +000033using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000034
Chris Lattner975f0582006-12-08 20:00:42 +000035// GetLibSupportInfoOutputFile - Return a file stream to print our output on.
Chris Lattnerd9ea85a2009-08-23 08:43:55 +000036namespace llvm { extern raw_ostream *GetLibSupportInfoOutputFile(); }
Chris Lattnerf205fec2003-05-09 20:05:44 +000037
Chris Lattner975f0582006-12-08 20:00:42 +000038/// -stats - Command line option to cause transformations to emit stats about
39/// what they did.
40///
Chris Lattner5ff62e92002-07-22 02:10:13 +000041static cl::opt<bool>
42Enabled("stats", cl::desc("Enable statistics output from program"));
43
Chris Lattner96ef1b92002-10-01 22:35:45 +000044
Chris Lattner975f0582006-12-08 20:00:42 +000045namespace {
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 {
Chris Lattner0a361522006-12-19 23:17:40 +000050 std::vector<const Statistic*> Stats;
Chris Lattner975f0582006-12-08 20:00:42 +000051public:
52 ~StatisticInfo();
53
Chris Lattner0a361522006-12-19 23:17:40 +000054 void addStatistic(const Statistic *S) {
Chris Lattner975f0582006-12-08 20:00:42 +000055 Stats.push_back(S);
Chris Lattner96ef1b92002-10-01 22:35:45 +000056 }
Chris Lattner975f0582006-12-08 20:00:42 +000057};
58}
Chris Lattner96ef1b92002-10-01 22:35:45 +000059
Chris Lattner975f0582006-12-08 20:00:42 +000060static ManagedStatic<StatisticInfo> StatInfo;
Torok Edwind7830d52009-09-27 11:08:03 +000061static ManagedStatic<sys::SmartMutex<true> > StatLock;
Chris Lattner975f0582006-12-08 20:00:42 +000062
63/// RegisterStatistic - The first time a statistic is bumped, this method is
64/// called.
Chris Lattner0a361522006-12-19 23:17:40 +000065void Statistic::RegisterStatistic() {
Chris Lattner975f0582006-12-08 20:00:42 +000066 // If stats are enabled, inform StatInfo that this statistic should be
67 // printed.
Torok Edwind7830d52009-09-27 11:08:03 +000068 sys::SmartScopedLock<true> Writer(*StatLock);
Owen Anderson92915e32009-06-23 21:19:38 +000069 if (!Initialized) {
70 if (Enabled)
71 StatInfo->addStatistic(this);
72
73 sys::MemoryFence();
74 // Remember we have been registered.
75 Initialized = true;
76 }
Chris Lattner975f0582006-12-08 20:00:42 +000077}
78
Dan Gohman844731a2008-05-13 00:00:25 +000079namespace {
80
Chris Lattner975f0582006-12-08 20:00:42 +000081struct NameCompare {
Chris Lattner0a361522006-12-19 23:17:40 +000082 bool operator()(const Statistic *LHS, const Statistic *RHS) const {
Chris Lattner975f0582006-12-08 20:00:42 +000083 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;
Chris Lattner96ef1b92002-10-01 22:35:45 +000088 }
89};
90
Dan Gohman844731a2008-05-13 00:00:25 +000091}
92
Chris Lattner975f0582006-12-08 20:00:42 +000093// Print information when destroyed, iff command line option is specified.
94StatisticInfo::~StatisticInfo() {
95 // Statistics not enabled?
96 if (Stats.empty()) return;
Chris Lattner96ef1b92002-10-01 22:35:45 +000097
Chris Lattner975f0582006-12-08 20:00:42 +000098 // Get the stream to write to.
Chris Lattnerd9ea85a2009-08-23 08:43:55 +000099 raw_ostream &OutStream = *GetLibSupportInfoOutputFile();
Chris Lattner96ef1b92002-10-01 22:35:45 +0000100
Chris Lattner975f0582006-12-08 20:00:42 +0000101 // Figure out how long the biggest Value and Name fields are.
102 unsigned MaxNameLen = 0, MaxValLen = 0;
Evan Cheng34cd4a42008-05-05 18:30:58 +0000103 for (size_t i = 0, e = Stats.size(); i != e; ++i) {
Chris Lattner975f0582006-12-08 20:00:42 +0000104 MaxValLen = std::max(MaxValLen,
105 (unsigned)utostr(Stats[i]->getValue()).size());
106 MaxNameLen = std::max(MaxNameLen,
107 (unsigned)std::strlen(Stats[i]->getName()));
Chris Lattner96ef1b92002-10-01 22:35:45 +0000108 }
Chris Lattner975f0582006-12-08 20:00:42 +0000109
110 // Sort the fields by name.
111 std::stable_sort(Stats.begin(), Stats.end(), NameCompare());
Chris Lattner96ef1b92002-10-01 22:35:45 +0000112
Chris Lattner975f0582006-12-08 20:00:42 +0000113 // 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.
Evan Cheng34cd4a42008-05-05 18:30:58 +0000119 for (size_t i = 0, e = Stats.size(); i != e; ++i) {
Chris Lattner975f0582006-12-08 20:00:42 +0000120 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
Chris Lattnerf3f4fd52002-05-10 15:36:46 +0000126 }
Chris Lattner975f0582006-12-08 20:00:42 +0000127
Chris Lattnerd9ea85a2009-08-23 08:43:55 +0000128 OutStream << '\n'; // Flush the output stream...
129 OutStream.flush();
Chris Lattner975f0582006-12-08 20:00:42 +0000130
David Greene8061a442010-01-05 01:28:47 +0000131 if (&OutStream != &outs() && &OutStream != &errs() && &OutStream != &dbgs())
Chris Lattner975f0582006-12-08 20:00:42 +0000132 delete &OutStream; // Close the file.
Chris Lattnerf3f4fd52002-05-10 15:36:46 +0000133}