2002-10-02 06:35:45 +08:00
|
|
|
//===-- Statistic.cpp - Easy way to expose stats information --------------===//
|
2005-04-22 06:55:34 +08:00
|
|
|
//
|
2003-10-21 03:43:21 +08:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 06:55:34 +08:00
|
|
|
//
|
2003-10-21 03:43:21 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-05-10 23:36:46 +08:00
|
|
|
//
|
|
|
|
// This file implements the 'Statistic' class, which is designed to be an easy
|
|
|
|
// way to expose various success metrics from passes. These statistics are
|
|
|
|
// printed at the end of a run, when the -stats command line option is enabled
|
|
|
|
// on the command line.
|
|
|
|
//
|
|
|
|
// This is useful for reporting information like the number of instructions
|
|
|
|
// simplified, optimized or removed by various transformations, like this:
|
|
|
|
//
|
2006-12-09 04:00:42 +08:00
|
|
|
// static Statistic NumInstEliminated("GCSE", "Number of instructions killed");
|
2002-05-10 23:36:46 +08:00
|
|
|
//
|
|
|
|
// Later, in the code: ++NumInstEliminated;
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2004-09-02 06:55:40 +08:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2004-09-02 06:55:40 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2015-08-19 06:31:24 +08:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2010-01-05 09:28:47 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2011-10-17 00:30:34 +08:00
|
|
|
#include "llvm/Support/Format.h"
|
2006-12-09 04:00:42 +08:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2010-11-30 02:16:10 +08:00
|
|
|
#include "llvm/Support/Mutex.h"
|
2016-11-19 03:43:24 +08:00
|
|
|
#include "llvm/Support/Timer.h"
|
|
|
|
#include "llvm/Support/YAMLTraits.h"
|
2017-06-06 19:49:48 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2003-08-14 05:32:37 +08:00
|
|
|
#include <algorithm>
|
2008-02-20 19:08:44 +08:00
|
|
|
#include <cstring>
|
2003-12-15 05:27:33 +08:00
|
|
|
using namespace llvm;
|
2003-11-12 06:41:34 +08:00
|
|
|
|
2006-12-09 04:00:42 +08:00
|
|
|
/// -stats - Command line option to cause transformations to emit stats about
|
|
|
|
/// what they did.
|
|
|
|
///
|
2017-12-01 08:53:10 +08:00
|
|
|
static cl::opt<bool> Stats(
|
|
|
|
"stats",
|
|
|
|
cl::desc("Enable statistics output from program (available with Asserts)"),
|
|
|
|
cl::Hidden);
|
2002-10-02 06:35:45 +08:00
|
|
|
|
2016-06-16 04:19:16 +08:00
|
|
|
static cl::opt<bool> StatsAsJSON("stats-json",
|
2017-12-01 08:53:10 +08:00
|
|
|
cl::desc("Display statistics as json data"),
|
|
|
|
cl::Hidden);
|
2016-06-16 04:19:16 +08:00
|
|
|
|
2016-09-27 02:38:07 +08:00
|
|
|
static bool Enabled;
|
2016-09-28 03:38:55 +08:00
|
|
|
static bool PrintOnExit;
|
2016-09-27 02:38:07 +08:00
|
|
|
|
2006-12-09 04:00:42 +08:00
|
|
|
namespace {
|
2018-03-06 03:38:16 +08:00
|
|
|
/// This class is used in a ManagedStatic so that it is created on demand (when
|
|
|
|
/// the first statistic is bumped) and destroyed only when llvm_shutdown is
|
|
|
|
/// called. We print statistics from the destructor.
|
|
|
|
/// This class is also used to look up statistic values from applications that
|
|
|
|
/// use LLVM.
|
2006-12-09 04:00:42 +08:00
|
|
|
class StatisticInfo {
|
2018-03-08 10:36:25 +08:00
|
|
|
std::vector<Statistic*> Stats;
|
2018-03-06 03:38:16 +08:00
|
|
|
|
2010-03-31 01:32:08 +08:00
|
|
|
friend void llvm::PrintStatistics();
|
|
|
|
friend void llvm::PrintStatistics(raw_ostream &OS);
|
2016-06-16 04:19:16 +08:00
|
|
|
friend void llvm::PrintStatisticsJSON(raw_ostream &OS);
|
|
|
|
|
|
|
|
/// Sort statistics by debugtype,name,description.
|
|
|
|
void sort();
|
2006-12-09 04:00:42 +08:00
|
|
|
public:
|
2018-03-08 10:36:25 +08:00
|
|
|
using const_iterator = std::vector<Statistic *>::const_iterator;
|
2018-03-06 03:38:16 +08:00
|
|
|
|
2016-11-19 03:43:24 +08:00
|
|
|
StatisticInfo();
|
2006-12-09 04:00:42 +08:00
|
|
|
~StatisticInfo();
|
2010-08-18 01:37:22 +08:00
|
|
|
|
2018-03-08 10:36:25 +08:00
|
|
|
void addStatistic(Statistic *S) {
|
2006-12-09 04:00:42 +08:00
|
|
|
Stats.push_back(S);
|
2002-10-02 06:35:45 +08:00
|
|
|
}
|
2018-03-06 03:38:16 +08:00
|
|
|
|
|
|
|
const_iterator begin() const { return Stats.begin(); }
|
|
|
|
const_iterator end() const { return Stats.end(); }
|
|
|
|
iterator_range<const_iterator> statistics() const {
|
|
|
|
return {begin(), end()};
|
|
|
|
}
|
2018-03-08 10:36:25 +08:00
|
|
|
|
|
|
|
void reset();
|
2002-10-02 06:35:45 +08:00
|
|
|
};
|
2018-03-06 03:38:16 +08:00
|
|
|
} // end anonymous namespace
|
2002-10-02 06:35:45 +08:00
|
|
|
|
2006-12-09 04:00:42 +08:00
|
|
|
static ManagedStatic<StatisticInfo> StatInfo;
|
2009-09-27 19:08:03 +08:00
|
|
|
static ManagedStatic<sys::SmartMutex<true> > StatLock;
|
2002-10-02 06:35:45 +08:00
|
|
|
|
2006-12-09 04:00:42 +08:00
|
|
|
/// RegisterStatistic - The first time a statistic is bumped, this method is
|
|
|
|
/// called.
|
2006-12-20 07:17:40 +08:00
|
|
|
void Statistic::RegisterStatistic() {
|
2006-12-09 04:00:42 +08:00
|
|
|
// If stats are enabled, inform StatInfo that this statistic should be
|
|
|
|
// printed.
|
2018-04-18 07:37:18 +08:00
|
|
|
// llvm_shutdown calls destructors while holding the ManagedStatic mutex.
|
|
|
|
// These destructors end up calling PrintStatistics, which takes StatLock.
|
|
|
|
// Since dereferencing StatInfo and StatLock can require taking the
|
|
|
|
// ManagedStatic mutex, doing so with StatLock held would lead to a lock
|
|
|
|
// order inversion. To avoid that, we dereference the ManagedStatics first,
|
|
|
|
// and only take StatLock afterwards.
|
2018-02-02 04:28:33 +08:00
|
|
|
if (!Initialized.load(std::memory_order_relaxed)) {
|
2018-04-18 07:37:18 +08:00
|
|
|
sys::SmartMutex<true> &Lock = *StatLock;
|
|
|
|
StatisticInfo &SI = *StatInfo;
|
|
|
|
sys::SmartScopedLock<true> Writer(Lock);
|
|
|
|
// Check Initialized again after acquiring the lock.
|
|
|
|
if (Initialized.load(std::memory_order_relaxed))
|
|
|
|
return;
|
2016-09-27 02:38:07 +08:00
|
|
|
if (Stats || Enabled)
|
2018-04-18 07:37:18 +08:00
|
|
|
SI.addStatistic(this);
|
2010-08-18 01:37:22 +08:00
|
|
|
|
2009-06-24 05:19:38 +08:00
|
|
|
// Remember we have been registered.
|
2018-02-02 04:28:33 +08:00
|
|
|
Initialized.store(true, std::memory_order_release);
|
2009-06-24 05:19:38 +08:00
|
|
|
}
|
2006-12-09 04:00:42 +08:00
|
|
|
}
|
2002-10-02 06:35:45 +08:00
|
|
|
|
2016-11-19 03:43:24 +08:00
|
|
|
StatisticInfo::StatisticInfo() {
|
|
|
|
// Ensure timergroup lists are created first so they are destructed after us.
|
|
|
|
TimerGroup::ConstructTimerLists();
|
|
|
|
}
|
|
|
|
|
2012-09-27 18:14:43 +08:00
|
|
|
// Print information when destroyed, iff command line option is specified.
|
2006-12-09 04:00:42 +08:00
|
|
|
StatisticInfo::~StatisticInfo() {
|
2016-09-28 03:38:55 +08:00
|
|
|
if (::Stats || PrintOnExit)
|
2016-09-27 02:38:07 +08:00
|
|
|
llvm::PrintStatistics();
|
2010-03-31 01:32:08 +08:00
|
|
|
}
|
2006-12-09 04:00:42 +08:00
|
|
|
|
2016-09-28 03:38:55 +08:00
|
|
|
void llvm::EnableStatistics(bool PrintOnExit) {
|
2016-09-27 02:38:07 +08:00
|
|
|
Enabled = true;
|
2016-09-28 03:38:55 +08:00
|
|
|
::PrintOnExit = PrintOnExit;
|
2010-03-31 01:32:08 +08:00
|
|
|
}
|
|
|
|
|
2011-02-27 07:17:12 +08:00
|
|
|
bool llvm::AreStatisticsEnabled() {
|
2016-09-27 02:38:07 +08:00
|
|
|
return Enabled || Stats;
|
2011-02-27 07:17:12 +08:00
|
|
|
}
|
|
|
|
|
2016-06-16 04:19:16 +08:00
|
|
|
void StatisticInfo::sort() {
|
|
|
|
std::stable_sort(Stats.begin(), Stats.end(),
|
|
|
|
[](const Statistic *LHS, const Statistic *RHS) {
|
|
|
|
if (int Cmp = std::strcmp(LHS->getDebugType(), RHS->getDebugType()))
|
|
|
|
return Cmp < 0;
|
|
|
|
|
|
|
|
if (int Cmp = std::strcmp(LHS->getName(), RHS->getName()))
|
|
|
|
return Cmp < 0;
|
|
|
|
|
|
|
|
return std::strcmp(LHS->getDesc(), RHS->getDesc()) < 0;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-03-08 10:36:25 +08:00
|
|
|
void StatisticInfo::reset() {
|
|
|
|
sys::SmartScopedLock<true> Writer(*StatLock);
|
|
|
|
|
|
|
|
// Tell each statistic that it isn't registered so it has to register
|
|
|
|
// again. We're holding the lock so it won't be able to do so until we're
|
|
|
|
// finished. Once we've forced it to re-register (after we return), then zero
|
|
|
|
// the value.
|
|
|
|
for (auto *Stat : Stats) {
|
|
|
|
// Value updates to a statistic that complete before this statement in the
|
|
|
|
// iteration for that statistic will be lost as intended.
|
|
|
|
Stat->Initialized = false;
|
|
|
|
Stat->Value = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear the registration list and release the lock once we're done. Any
|
|
|
|
// pending updates from other threads will safely take effect after we return.
|
|
|
|
// That might not be what the user wants if they're measuring a compilation
|
|
|
|
// but it's their responsibility to prevent concurrent compilations to make
|
|
|
|
// a single compilation measurable.
|
|
|
|
Stats.clear();
|
|
|
|
}
|
|
|
|
|
2010-03-31 01:32:08 +08:00
|
|
|
void llvm::PrintStatistics(raw_ostream &OS) {
|
|
|
|
StatisticInfo &Stats = *StatInfo;
|
2006-12-09 04:00:42 +08:00
|
|
|
|
|
|
|
// Figure out how long the biggest Value and Name fields are.
|
2016-06-16 04:19:16 +08:00
|
|
|
unsigned MaxDebugTypeLen = 0, MaxValLen = 0;
|
2010-03-31 01:32:08 +08:00
|
|
|
for (size_t i = 0, e = Stats.Stats.size(); i != e; ++i) {
|
2006-12-09 04:00:42 +08:00
|
|
|
MaxValLen = std::max(MaxValLen,
|
2010-03-31 01:32:08 +08:00
|
|
|
(unsigned)utostr(Stats.Stats[i]->getValue()).size());
|
2016-06-16 04:19:16 +08:00
|
|
|
MaxDebugTypeLen = std::max(MaxDebugTypeLen,
|
|
|
|
(unsigned)std::strlen(Stats.Stats[i]->getDebugType()));
|
2006-12-09 04:00:42 +08:00
|
|
|
}
|
2010-08-18 01:37:22 +08:00
|
|
|
|
2016-06-16 04:19:16 +08:00
|
|
|
Stats.sort();
|
2006-12-09 04:00:42 +08:00
|
|
|
|
|
|
|
// Print out the statistics header...
|
2010-03-31 01:32:08 +08:00
|
|
|
OS << "===" << std::string(73, '-') << "===\n"
|
|
|
|
<< " ... Statistics Collected ...\n"
|
|
|
|
<< "===" << std::string(73, '-') << "===\n\n";
|
2010-08-18 01:37:22 +08:00
|
|
|
|
2006-12-09 04:00:42 +08:00
|
|
|
// Print all of the statistics.
|
2011-10-17 00:30:34 +08:00
|
|
|
for (size_t i = 0, e = Stats.Stats.size(); i != e; ++i)
|
|
|
|
OS << format("%*u %-*s - %s\n",
|
|
|
|
MaxValLen, Stats.Stats[i]->getValue(),
|
2016-06-16 04:19:16 +08:00
|
|
|
MaxDebugTypeLen, Stats.Stats[i]->getDebugType(),
|
2011-10-17 00:30:34 +08:00
|
|
|
Stats.Stats[i]->getDesc());
|
2010-08-18 01:37:22 +08:00
|
|
|
|
2010-03-31 01:32:08 +08:00
|
|
|
OS << '\n'; // Flush the output stream.
|
|
|
|
OS.flush();
|
2016-06-16 04:19:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void llvm::PrintStatisticsJSON(raw_ostream &OS) {
|
2018-03-07 05:16:42 +08:00
|
|
|
sys::SmartScopedLock<true> Reader(*StatLock);
|
2016-06-16 04:19:16 +08:00
|
|
|
StatisticInfo &Stats = *StatInfo;
|
|
|
|
|
|
|
|
Stats.sort();
|
2010-03-31 01:32:08 +08:00
|
|
|
|
2016-06-16 04:19:16 +08:00
|
|
|
// Print all of the statistics.
|
|
|
|
OS << "{\n";
|
|
|
|
const char *delim = "";
|
|
|
|
for (const Statistic *Stat : Stats.Stats) {
|
|
|
|
OS << delim;
|
2017-12-19 01:38:03 +08:00
|
|
|
assert(yaml::needsQuotes(Stat->getDebugType()) == yaml::QuotingType::None &&
|
2016-11-19 03:43:24 +08:00
|
|
|
"Statistic group/type name is simple.");
|
2017-12-19 01:38:03 +08:00
|
|
|
assert(yaml::needsQuotes(Stat->getName()) == yaml::QuotingType::None &&
|
|
|
|
"Statistic name is simple");
|
2016-11-19 03:43:24 +08:00
|
|
|
OS << "\t\"" << Stat->getDebugType() << '.' << Stat->getName() << "\": "
|
|
|
|
<< Stat->getValue();
|
2016-06-16 04:19:16 +08:00
|
|
|
delim = ",\n";
|
|
|
|
}
|
2016-11-19 03:43:24 +08:00
|
|
|
// Print timers.
|
|
|
|
TimerGroup::printAllJSONValues(OS, delim);
|
|
|
|
|
2016-06-16 04:19:16 +08:00
|
|
|
OS << "\n}\n";
|
|
|
|
OS.flush();
|
2010-03-31 01:32:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void llvm::PrintStatistics() {
|
2018-03-06 03:38:16 +08:00
|
|
|
#if LLVM_ENABLE_STATS
|
2018-03-07 05:16:42 +08:00
|
|
|
sys::SmartScopedLock<true> Reader(*StatLock);
|
2010-03-31 01:32:08 +08:00
|
|
|
StatisticInfo &Stats = *StatInfo;
|
|
|
|
|
|
|
|
// Statistics not enabled?
|
|
|
|
if (Stats.Stats.empty()) return;
|
|
|
|
|
|
|
|
// Get the stream to write to.
|
2015-12-17 06:28:34 +08:00
|
|
|
std::unique_ptr<raw_ostream> OutStream = CreateInfoOutputFile();
|
2016-06-16 04:19:16 +08:00
|
|
|
if (StatsAsJSON)
|
|
|
|
PrintStatisticsJSON(*OutStream);
|
|
|
|
else
|
|
|
|
PrintStatistics(*OutStream);
|
2015-12-17 06:28:34 +08:00
|
|
|
|
2013-03-09 06:56:31 +08:00
|
|
|
#else
|
|
|
|
// Check if the -stats option is set instead of checking
|
|
|
|
// !Stats.Stats.empty(). In release builds, Statistics operators
|
|
|
|
// do nothing, so stats are never Registered.
|
2016-09-27 02:38:07 +08:00
|
|
|
if (Stats) {
|
2013-03-09 06:56:31 +08:00
|
|
|
// Get the stream to write to.
|
2015-12-17 06:28:34 +08:00
|
|
|
std::unique_ptr<raw_ostream> OutStream = CreateInfoOutputFile();
|
|
|
|
(*OutStream) << "Statistics are disabled. "
|
|
|
|
<< "Build with asserts or with -DLLVM_ENABLE_STATS\n";
|
2013-03-09 06:56:31 +08:00
|
|
|
}
|
|
|
|
#endif
|
2002-05-10 23:36:46 +08:00
|
|
|
}
|
2018-03-06 03:38:16 +08:00
|
|
|
|
|
|
|
const std::vector<std::pair<StringRef, unsigned>> llvm::GetStatistics() {
|
|
|
|
sys::SmartScopedLock<true> Reader(*StatLock);
|
|
|
|
std::vector<std::pair<StringRef, unsigned>> ReturnStats;
|
|
|
|
|
|
|
|
for (const auto &Stat : StatInfo->statistics())
|
|
|
|
ReturnStats.emplace_back(Stat->getName(), Stat->getValue());
|
|
|
|
return ReturnStats;
|
|
|
|
}
|
2018-03-08 10:36:25 +08:00
|
|
|
|
|
|
|
void llvm::ResetStatistics() {
|
|
|
|
StatInfo->reset();
|
|
|
|
}
|