1 //===-- llvm/ADT/Statistic.h - Easy way to expose stats ---------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the 'Statistic' class, which is designed to be an easy way
11 // to expose various metrics from passes. These statistics are printed at the
12 // end of a run (from llvm_shutdown), when the -stats command line option is
13 // passed on the command line.
15 // This is useful for reporting information like the number of instructions
16 // simplified, optimized or removed by various transformations, like this:
18 // static Statistic NumInstsKilled("gcse", "Number of instructions killed");
20 // Later, in the code: ++NumInstsKilled;
22 // NOTE: Statistics *must* be declared as global variables.
24 //===----------------------------------------------------------------------===//
26 #ifndef LLVM_ADT_STATISTIC_H
27 #define LLVM_ADT_STATISTIC_H
29 #include "llvm/Support/Atomic.h"
30 #include "llvm/Support/Compiler.h"
41 const char *DebugType;
44 std::atomic<unsigned> Value;
47 unsigned getValue() const { return Value.load(std::memory_order_relaxed); }
48 const char *getDebugType() const { return DebugType; }
49 const char *getName() const { return Name; }
50 const char *getDesc() const { return Desc; }
52 /// construct - This should only be called for non-global statistics.
53 void construct(const char *debugtype, const char *name, const char *desc) {
54 DebugType = debugtype;
61 // Allow use of this class as the value itself.
62 operator unsigned() const { return getValue(); }
64 #if !defined(NDEBUG) || defined(LLVM_ENABLE_STATS)
65 const Statistic &operator=(unsigned Val) {
66 Value.store(Val, std::memory_order_relaxed);
70 const Statistic &operator++() {
71 Value.fetch_add(1, std::memory_order_relaxed);
75 unsigned operator++(int) {
77 return Value.fetch_add(1, std::memory_order_relaxed);
80 const Statistic &operator--() {
81 Value.fetch_sub(1, std::memory_order_relaxed);
85 unsigned operator--(int) {
87 return Value.fetch_sub(1, std::memory_order_relaxed);
90 const Statistic &operator+=(unsigned V) {
93 Value.fetch_add(V, std::memory_order_relaxed);
97 const Statistic &operator-=(unsigned V) {
100 Value.fetch_sub(V, std::memory_order_relaxed);
104 void updateMax(unsigned V) {
105 unsigned PrevMax = Value.load(std::memory_order_relaxed);
106 // Keep trying to update max until we succeed or another thread produces
107 // a bigger max than us.
108 while (V > PrevMax && !Value.compare_exchange_weak(
109 PrevMax, V, std::memory_order_relaxed)) {
114 #else // Statistics are disabled in release builds.
116 const Statistic &operator=(unsigned Val) {
120 const Statistic &operator++() {
124 unsigned operator++(int) {
128 const Statistic &operator--() {
132 unsigned operator--(int) {
136 const Statistic &operator+=(const unsigned &V) {
140 const Statistic &operator-=(const unsigned &V) {
144 void updateMax(unsigned V) {}
146 #endif // !defined(NDEBUG) || defined(LLVM_ENABLE_STATS)
150 bool tmp = Initialized;
152 if (!tmp) RegisterStatistic();
153 TsanHappensAfter(this);
157 void RegisterStatistic();
160 // STATISTIC - A macro to make definition of statistics really simple. This
161 // automatically passes the DEBUG_TYPE of the file into the statistic.
162 #define STATISTIC(VARNAME, DESC) \
163 static llvm::Statistic VARNAME = {DEBUG_TYPE, #VARNAME, DESC, {0}, false}
165 /// \brief Enable the collection and printing of statistics.
166 void EnableStatistics(bool PrintOnExit = true);
168 /// \brief Check if statistics are enabled.
169 bool AreStatisticsEnabled();
171 /// \brief Return a file stream to print our output on.
172 std::unique_ptr<raw_fd_ostream> CreateInfoOutputFile();
174 /// \brief Print statistics to the file returned by CreateInfoOutputFile().
175 void PrintStatistics();
177 /// \brief Print statistics to the given output stream.
178 void PrintStatistics(raw_ostream &OS);
180 /// Print statistics in JSON format. This does include all global timers (\see
181 /// Timer, TimerGroup). Note that the timers are cleared after printing and will
182 /// not be printed in human readable form or in a second call of
183 /// PrintStatisticsJSON().
184 void PrintStatisticsJSON(raw_ostream &OS);
186 } // end namespace llvm
188 #endif // LLVM_ADT_STATISTIC_H