2004-09-02 00:55:40 +02:00
|
|
|
//===-- llvm/ADT/Statistic.h - Easy way to expose stats ---------*- C++ -*-===//
|
2005-04-21 22:19:05 +02:00
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2005-04-21 22:19:05 +02:00
|
|
|
//
|
2003-10-20 21:46:57 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-05-10 17:36:56 +02:00
|
|
|
//
|
|
|
|
// This file defines the 'Statistic' class, which is designed to be an easy way
|
2006-12-06 18:46:33 +01:00
|
|
|
// to expose various metrics from passes. These statistics are printed at the
|
|
|
|
// end of a run (from llvm_shutdown), when the -stats command line option is
|
|
|
|
// passed on the command line.
|
2002-05-10 17:36:56 +02:00
|
|
|
//
|
|
|
|
// This is useful for reporting information like the number of instructions
|
|
|
|
// simplified, optimized or removed by various transformations, like this:
|
|
|
|
//
|
2006-12-06 18:46:33 +01:00
|
|
|
// static Statistic NumInstsKilled("gcse", "Number of instructions killed");
|
2002-05-10 17:36:56 +02:00
|
|
|
//
|
2004-02-13 05:49:04 +01:00
|
|
|
// Later, in the code: ++NumInstsKilled;
|
2002-05-10 17:36:56 +02:00
|
|
|
//
|
2006-12-06 18:46:33 +01:00
|
|
|
// NOTE: Statistics *must* be declared as global variables.
|
|
|
|
//
|
2002-05-10 17:36:56 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2004-09-02 00:55:40 +02:00
|
|
|
#ifndef LLVM_ADT_STATISTIC_H
|
|
|
|
#define LLVM_ADT_STATISTIC_H
|
2002-05-10 17:36:56 +02:00
|
|
|
|
2018-03-05 20:38:16 +01:00
|
|
|
#include "llvm/Config/llvm-config.h"
|
2016-03-28 19:40:08 +02:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2016-06-02 10:44:05 +02:00
|
|
|
#include <atomic>
|
2015-12-16 23:28:34 +01:00
|
|
|
#include <memory>
|
2018-03-05 20:38:16 +01:00
|
|
|
#include <vector>
|
2009-06-23 23:19:38 +02:00
|
|
|
|
2018-03-07 20:32:36 +01:00
|
|
|
// Determine whether statistics should be enabled. We must do it here rather
|
|
|
|
// than in CMake because multi-config generators cannot determine this at
|
|
|
|
// configure time.
|
|
|
|
#if !defined(NDEBUG) || LLVM_FORCE_ENABLE_STATS
|
|
|
|
#define LLVM_ENABLE_STATS 1
|
|
|
|
#endif
|
|
|
|
|
2003-11-11 23:41:34 +01:00
|
|
|
namespace llvm {
|
2016-08-13 02:50:41 +02:00
|
|
|
|
2010-03-30 19:32:08 +02:00
|
|
|
class raw_ostream;
|
2015-12-16 23:28:34 +01:00
|
|
|
class raw_fd_ostream;
|
2018-03-05 20:38:16 +01:00
|
|
|
class StringRef;
|
2003-11-11 23:41:34 +01:00
|
|
|
|
2019-10-11 02:57:41 +02:00
|
|
|
class StatisticBase {
|
2006-12-19 22:27:47 +01:00
|
|
|
public:
|
2016-06-15 22:19:16 +02:00
|
|
|
const char *DebugType;
|
2002-05-10 17:36:56 +02:00
|
|
|
const char *Name;
|
2002-10-02 00:35:45 +02:00
|
|
|
const char *Desc;
|
2002-05-10 17:36:56 +02:00
|
|
|
|
2019-10-11 02:57:41 +02:00
|
|
|
StatisticBase(const char *DebugType, const char *Name, const char *Desc)
|
|
|
|
: DebugType(DebugType), Name(Name), Desc(Desc) {}
|
|
|
|
|
2016-06-15 22:19:16 +02:00
|
|
|
const char *getDebugType() const { return DebugType; }
|
2006-12-08 21:00:42 +01:00
|
|
|
const char *getName() const { return Name; }
|
|
|
|
const char *getDesc() const { return Desc; }
|
2019-10-11 02:57:41 +02:00
|
|
|
};
|
2006-12-20 00:17:40 +01:00
|
|
|
|
2019-10-11 02:57:41 +02:00
|
|
|
class TrackingStatistic : public StatisticBase {
|
|
|
|
public:
|
|
|
|
std::atomic<unsigned> Value;
|
|
|
|
std::atomic<bool> Initialized;
|
|
|
|
|
|
|
|
TrackingStatistic(const char *DebugType, const char *Name, const char *Desc)
|
|
|
|
: StatisticBase(DebugType, Name, Desc), Value(0), Initialized(false) {}
|
|
|
|
|
|
|
|
unsigned getValue() const { return Value.load(std::memory_order_relaxed); }
|
2006-12-20 00:17:40 +01:00
|
|
|
|
2006-12-08 21:00:42 +01:00
|
|
|
// Allow use of this class as the value itself.
|
2016-06-02 10:44:05 +02:00
|
|
|
operator unsigned() const { return getValue(); }
|
2013-03-08 23:56:31 +01:00
|
|
|
|
2019-10-11 02:57:41 +02:00
|
|
|
const TrackingStatistic &operator=(unsigned Val) {
|
2016-06-02 10:44:05 +02:00
|
|
|
Value.store(Val, std::memory_order_relaxed);
|
2009-06-23 23:19:38 +02:00
|
|
|
return init();
|
|
|
|
}
|
2011-10-10 21:35:46 +02:00
|
|
|
|
2019-10-11 02:57:41 +02:00
|
|
|
const TrackingStatistic &operator++() {
|
2016-06-02 10:44:05 +02:00
|
|
|
Value.fetch_add(1, std::memory_order_relaxed);
|
2009-06-23 23:19:38 +02:00
|
|
|
return init();
|
|
|
|
}
|
2011-10-10 21:35:46 +02:00
|
|
|
|
2009-06-23 23:19:38 +02:00
|
|
|
unsigned operator++(int) {
|
|
|
|
init();
|
2016-06-02 10:44:05 +02:00
|
|
|
return Value.fetch_add(1, std::memory_order_relaxed);
|
2009-06-23 23:19:38 +02:00
|
|
|
}
|
2011-10-10 21:35:46 +02:00
|
|
|
|
2019-10-11 02:57:41 +02:00
|
|
|
const TrackingStatistic &operator--() {
|
2016-06-02 10:44:05 +02:00
|
|
|
Value.fetch_sub(1, std::memory_order_relaxed);
|
2009-06-23 23:19:38 +02:00
|
|
|
return init();
|
|
|
|
}
|
2011-10-10 21:35:46 +02:00
|
|
|
|
2009-06-23 23:19:38 +02:00
|
|
|
unsigned operator--(int) {
|
|
|
|
init();
|
2016-06-02 10:44:05 +02:00
|
|
|
return Value.fetch_sub(1, std::memory_order_relaxed);
|
2009-06-23 23:19:38 +02:00
|
|
|
}
|
2011-10-10 21:35:46 +02:00
|
|
|
|
2019-10-11 02:57:41 +02:00
|
|
|
const TrackingStatistic &operator+=(unsigned V) {
|
2016-06-02 10:44:05 +02:00
|
|
|
if (V == 0)
|
|
|
|
return *this;
|
|
|
|
Value.fetch_add(V, std::memory_order_relaxed);
|
2009-06-23 23:19:38 +02:00
|
|
|
return init();
|
|
|
|
}
|
2011-10-10 21:35:46 +02:00
|
|
|
|
2019-10-11 02:57:41 +02:00
|
|
|
const TrackingStatistic &operator-=(unsigned V) {
|
2016-06-02 10:44:05 +02:00
|
|
|
if (V == 0)
|
|
|
|
return *this;
|
|
|
|
Value.fetch_sub(V, std::memory_order_relaxed);
|
2009-06-23 23:19:38 +02:00
|
|
|
return init();
|
|
|
|
}
|
2011-10-10 21:35:46 +02:00
|
|
|
|
2017-05-18 02:51:39 +02:00
|
|
|
void updateMax(unsigned V) {
|
|
|
|
unsigned PrevMax = Value.load(std::memory_order_relaxed);
|
|
|
|
// Keep trying to update max until we succeed or another thread produces
|
|
|
|
// a bigger max than us.
|
|
|
|
while (V > PrevMax && !Value.compare_exchange_weak(
|
|
|
|
PrevMax, V, std::memory_order_relaxed)) {
|
|
|
|
}
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
2019-10-11 02:57:41 +02:00
|
|
|
protected:
|
|
|
|
TrackingStatistic &init() {
|
|
|
|
if (!Initialized.load(std::memory_order_acquire))
|
|
|
|
RegisterStatistic();
|
2013-03-08 23:56:31 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-10-11 02:57:41 +02:00
|
|
|
void RegisterStatistic();
|
|
|
|
};
|
2013-03-08 23:56:31 +01:00
|
|
|
|
2019-10-11 02:57:41 +02:00
|
|
|
class NoopStatistic : public StatisticBase {
|
|
|
|
public:
|
|
|
|
using StatisticBase::StatisticBase;
|
2013-03-08 23:56:31 +01:00
|
|
|
|
2019-10-11 02:57:41 +02:00
|
|
|
unsigned getValue() const { return 0; }
|
2013-03-08 23:56:31 +01:00
|
|
|
|
2019-10-11 02:57:41 +02:00
|
|
|
// Allow use of this class as the value itself.
|
|
|
|
operator unsigned() const { return 0; }
|
2013-03-08 23:56:31 +01:00
|
|
|
|
2019-10-11 02:57:41 +02:00
|
|
|
const NoopStatistic &operator=(unsigned Val) { return *this; }
|
2013-03-08 23:56:31 +01:00
|
|
|
|
2019-10-11 02:57:41 +02:00
|
|
|
const NoopStatistic &operator++() { return *this; }
|
2013-03-08 23:56:31 +01:00
|
|
|
|
2019-10-11 02:57:41 +02:00
|
|
|
unsigned operator++(int) { return 0; }
|
2017-05-18 02:51:39 +02:00
|
|
|
|
2019-10-11 02:57:41 +02:00
|
|
|
const NoopStatistic &operator--() { return *this; }
|
2013-03-08 23:56:31 +01:00
|
|
|
|
2019-10-11 02:57:41 +02:00
|
|
|
unsigned operator--(int) { return 0; }
|
2016-08-13 02:50:41 +02:00
|
|
|
|
2019-10-11 02:57:41 +02:00
|
|
|
const NoopStatistic &operator+=(const unsigned &V) { return *this; }
|
|
|
|
|
|
|
|
const NoopStatistic &operator-=(const unsigned &V) { return *this; }
|
|
|
|
|
|
|
|
void updateMax(unsigned V) {}
|
2002-05-10 17:36:56 +02:00
|
|
|
};
|
2009-01-09 20:25:42 +01:00
|
|
|
|
2019-10-11 02:57:41 +02:00
|
|
|
#if LLVM_ENABLE_STATS
|
|
|
|
using Statistic = TrackingStatistic;
|
|
|
|
#else
|
|
|
|
using Statistic = NoopStatistic;
|
|
|
|
#endif
|
|
|
|
|
2006-12-19 22:27:47 +01:00
|
|
|
// STATISTIC - A macro to make definition of statistics really simple. This
|
|
|
|
// automatically passes the DEBUG_TYPE of the file into the statistic.
|
2016-06-02 10:44:05 +02:00
|
|
|
#define STATISTIC(VARNAME, DESC) \
|
2019-10-11 02:57:41 +02:00
|
|
|
static llvm::Statistic VARNAME = {DEBUG_TYPE, #VARNAME, DESC}
|
|
|
|
|
|
|
|
// ALWAYS_ENABLED_STATISTIC - A macro to define a statistic like STATISTIC but
|
|
|
|
// it is enabled even if LLVM_ENABLE_STATS is off.
|
|
|
|
#define ALWAYS_ENABLED_STATISTIC(VARNAME, DESC) \
|
|
|
|
static llvm::TrackingStatistic VARNAME = {DEBUG_TYPE, #VARNAME, DESC}
|
2002-05-10 17:36:56 +02:00
|
|
|
|
2018-05-01 17:54:18 +02:00
|
|
|
/// Enable the collection and printing of statistics.
|
2019-11-21 12:56:48 +01:00
|
|
|
void EnableStatistics(bool DoPrintOnExit = true);
|
2010-03-30 19:32:08 +02:00
|
|
|
|
2018-05-01 17:54:18 +02:00
|
|
|
/// Check if statistics are enabled.
|
2011-02-27 00:17:12 +01:00
|
|
|
bool AreStatisticsEnabled();
|
|
|
|
|
2018-05-01 17:54:18 +02:00
|
|
|
/// Return a file stream to print our output on.
|
2015-12-16 23:28:34 +01:00
|
|
|
std::unique_ptr<raw_fd_ostream> CreateInfoOutputFile();
|
|
|
|
|
2018-05-01 17:54:18 +02:00
|
|
|
/// Print statistics to the file returned by CreateInfoOutputFile().
|
2010-03-30 19:32:08 +02:00
|
|
|
void PrintStatistics();
|
|
|
|
|
2018-05-01 17:54:18 +02:00
|
|
|
/// Print statistics to the given output stream.
|
2010-03-30 19:32:08 +02:00
|
|
|
void PrintStatistics(raw_ostream &OS);
|
|
|
|
|
2016-11-18 20:43:24 +01:00
|
|
|
/// Print statistics in JSON format. This does include all global timers (\see
|
|
|
|
/// Timer, TimerGroup). Note that the timers are cleared after printing and will
|
|
|
|
/// not be printed in human readable form or in a second call of
|
|
|
|
/// PrintStatisticsJSON().
|
2016-06-15 22:19:16 +02:00
|
|
|
void PrintStatisticsJSON(raw_ostream &OS);
|
|
|
|
|
2018-05-01 17:54:18 +02:00
|
|
|
/// Get the statistics. This can be used to look up the value of
|
2018-03-05 20:38:16 +01:00
|
|
|
/// statistics without needing to parse JSON.
|
|
|
|
///
|
|
|
|
/// This function does not prevent statistics being updated by other threads
|
|
|
|
/// during it's execution. It will return the value at the point that it is
|
|
|
|
/// read. However, it will prevent new statistics from registering until it
|
|
|
|
/// completes.
|
|
|
|
const std::vector<std::pair<StringRef, unsigned>> GetStatistics();
|
|
|
|
|
2018-05-01 17:54:18 +02:00
|
|
|
/// Reset the statistics. This can be used to zero and de-register the
|
2018-03-08 03:36:25 +01:00
|
|
|
/// statistics in order to measure a compilation.
|
|
|
|
///
|
|
|
|
/// When this function begins to call destructors prior to returning, all
|
|
|
|
/// statistics will be zero and unregistered. However, that might not remain the
|
|
|
|
/// case by the time this function finishes returning. Whether update from other
|
|
|
|
/// threads are lost or merely deferred until during the function return is
|
|
|
|
/// timing sensitive.
|
|
|
|
///
|
|
|
|
/// Callers who intend to use this to measure statistics for a single
|
|
|
|
/// compilation should ensure that no compilations are in progress at the point
|
|
|
|
/// this function is called and that only one compilation executes until calling
|
|
|
|
/// GetStatistics().
|
|
|
|
void ResetStatistics();
|
|
|
|
|
2016-08-13 02:50:41 +02:00
|
|
|
} // end namespace llvm
|
2003-11-11 23:41:34 +01:00
|
|
|
|
2016-03-28 19:40:08 +02:00
|
|
|
#endif // LLVM_ADT_STATISTIC_H
|