LLVM  10.0.0svn
Statistic.h
Go to the documentation of this file.
1 //===-- llvm/ADT/Statistic.h - Easy way to expose stats ---------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the 'Statistic' class, which is designed to be an easy way
10 // to expose various metrics from passes. These statistics are printed at the
11 // end of a run (from llvm_shutdown), when the -stats command line option is
12 // passed on the command line.
13 //
14 // This is useful for reporting information like the number of instructions
15 // simplified, optimized or removed by various transformations, like this:
16 //
17 // static Statistic NumInstsKilled("gcse", "Number of instructions killed");
18 //
19 // Later, in the code: ++NumInstsKilled;
20 //
21 // NOTE: Statistics *must* be declared as global variables.
22 //
23 //===----------------------------------------------------------------------===//
24 
25 #ifndef LLVM_ADT_STATISTIC_H
26 #define LLVM_ADT_STATISTIC_H
27 
28 #include "llvm/Config/llvm-config.h"
29 #include "llvm/Support/Compiler.h"
30 #include <atomic>
31 #include <memory>
32 #include <vector>
33 
34 // Determine whether statistics should be enabled. We must do it here rather
35 // than in CMake because multi-config generators cannot determine this at
36 // configure time.
37 #if !defined(NDEBUG) || LLVM_FORCE_ENABLE_STATS
38 #define LLVM_ENABLE_STATS 1
39 #endif
40 
41 namespace llvm {
42 
43 class raw_ostream;
44 class raw_fd_ostream;
45 class StringRef;
46 
48 public:
49  const char *DebugType;
50  const char *Name;
51  const char *Desc;
52 
53  StatisticBase(const char *DebugType, const char *Name, const char *Desc)
54  : DebugType(DebugType), Name(Name), Desc(Desc) {}
55 
56  const char *getDebugType() const { return DebugType; }
57  const char *getName() const { return Name; }
58  const char *getDesc() const { return Desc; }
59 };
60 
62 public:
63  std::atomic<unsigned> Value;
64  std::atomic<bool> Initialized;
65 
66  TrackingStatistic(const char *DebugType, const char *Name, const char *Desc)
67  : StatisticBase(DebugType, Name, Desc), Value(0), Initialized(false) {}
68 
69  unsigned getValue() const { return Value.load(std::memory_order_relaxed); }
70 
71  // Allow use of this class as the value itself.
72  operator unsigned() const { return getValue(); }
73 
74  const TrackingStatistic &operator=(unsigned Val) {
75  Value.store(Val, std::memory_order_relaxed);
76  return init();
77  }
78 
80  Value.fetch_add(1, std::memory_order_relaxed);
81  return init();
82  }
83 
84  unsigned operator++(int) {
85  init();
86  return Value.fetch_add(1, std::memory_order_relaxed);
87  }
88 
90  Value.fetch_sub(1, std::memory_order_relaxed);
91  return init();
92  }
93 
94  unsigned operator--(int) {
95  init();
96  return Value.fetch_sub(1, std::memory_order_relaxed);
97  }
98 
99  const TrackingStatistic &operator+=(unsigned V) {
100  if (V == 0)
101  return *this;
102  Value.fetch_add(V, std::memory_order_relaxed);
103  return init();
104  }
105 
106  const TrackingStatistic &operator-=(unsigned V) {
107  if (V == 0)
108  return *this;
109  Value.fetch_sub(V, std::memory_order_relaxed);
110  return init();
111  }
112 
113  void updateMax(unsigned V) {
114  unsigned PrevMax = Value.load(std::memory_order_relaxed);
115  // Keep trying to update max until we succeed or another thread produces
116  // a bigger max than us.
117  while (V > PrevMax && !Value.compare_exchange_weak(
118  PrevMax, V, std::memory_order_relaxed)) {
119  }
120  init();
121  }
122 
123 protected:
125  if (!Initialized.load(std::memory_order_acquire))
126  RegisterStatistic();
127  return *this;
128  }
129 
130  void RegisterStatistic();
131 };
132 
133 class NoopStatistic : public StatisticBase {
134 public:
136 
137  unsigned getValue() const { return 0; }
138 
139  // Allow use of this class as the value itself.
140  operator unsigned() const { return 0; }
141 
142  const NoopStatistic &operator=(unsigned Val) { return *this; }
143 
144  const NoopStatistic &operator++() { return *this; }
145 
146  unsigned operator++(int) { return 0; }
147 
148  const NoopStatistic &operator--() { return *this; }
149 
150  unsigned operator--(int) { return 0; }
151 
152  const NoopStatistic &operator+=(const unsigned &V) { return *this; }
153 
154  const NoopStatistic &operator-=(const unsigned &V) { return *this; }
155 
156  void updateMax(unsigned V) {}
157 };
158 
159 #if LLVM_ENABLE_STATS
161 #else
162 using Statistic = NoopStatistic;
163 #endif
164 
165 // STATISTIC - A macro to make definition of statistics really simple. This
166 // automatically passes the DEBUG_TYPE of the file into the statistic.
167 #define STATISTIC(VARNAME, DESC) \
168  static llvm::Statistic VARNAME = {DEBUG_TYPE, #VARNAME, DESC}
169 
170 // ALWAYS_ENABLED_STATISTIC - A macro to define a statistic like STATISTIC but
171 // it is enabled even if LLVM_ENABLE_STATS is off.
172 #define ALWAYS_ENABLED_STATISTIC(VARNAME, DESC) \
173  static llvm::TrackingStatistic VARNAME = {DEBUG_TYPE, #VARNAME, DESC}
174 
175 /// Enable the collection and printing of statistics.
176 void EnableStatistics(bool PrintOnExit = true);
177 
178 /// Check if statistics are enabled.
179 bool AreStatisticsEnabled();
180 
181 /// Return a file stream to print our output on.
182 std::unique_ptr<raw_fd_ostream> CreateInfoOutputFile();
183 
184 /// Print statistics to the file returned by CreateInfoOutputFile().
185 void PrintStatistics();
186 
187 /// Print statistics to the given output stream.
188 void PrintStatistics(raw_ostream &OS);
189 
190 /// Print statistics in JSON format. This does include all global timers (\see
191 /// Timer, TimerGroup). Note that the timers are cleared after printing and will
192 /// not be printed in human readable form or in a second call of
193 /// PrintStatisticsJSON().
194 void PrintStatisticsJSON(raw_ostream &OS);
195 
196 /// Get the statistics. This can be used to look up the value of
197 /// statistics without needing to parse JSON.
198 ///
199 /// This function does not prevent statistics being updated by other threads
200 /// during it's execution. It will return the value at the point that it is
201 /// read. However, it will prevent new statistics from registering until it
202 /// completes.
203 const std::vector<std::pair<StringRef, unsigned>> GetStatistics();
204 
205 /// Reset the statistics. This can be used to zero and de-register the
206 /// statistics in order to measure a compilation.
207 ///
208 /// When this function begins to call destructors prior to returning, all
209 /// statistics will be zero and unregistered. However, that might not remain the
210 /// case by the time this function finishes returning. Whether update from other
211 /// threads are lost or merely deferred until during the function return is
212 /// timing sensitive.
213 ///
214 /// Callers who intend to use this to measure statistics for a single
215 /// compilation should ensure that no compilations are in progress at the point
216 /// this function is called and that only one compilation executes until calling
217 /// GetStatistics().
218 void ResetStatistics();
219 
220 } // end namespace llvm
221 
222 #endif // LLVM_ADT_STATISTIC_H
const NoopStatistic & operator-=(const unsigned &V)
Definition: Statistic.h:154
static bool PrintOnExit
Definition: Statistic.cpp:51
unsigned operator++(int)
Definition: Statistic.h:84
void EnableStatistics(bool PrintOnExit=true)
Enable the collection and printing of statistics.
Definition: Statistic.cpp:126
This class represents lattice values for constants.
Definition: AllocatorList.h:23
const TrackingStatistic & operator++()
Definition: Statistic.h:79
void updateMax(unsigned V)
Definition: Statistic.h:156
void updateMax(unsigned V)
Definition: Statistic.h:113
const TrackingStatistic & operator=(unsigned Val)
Definition: Statistic.h:74
void ResetStatistics()
Reset the statistics.
Definition: Statistic.cpp:263
const char * DebugType
Definition: Statistic.h:49
unsigned getValue() const
Definition: Statistic.h:137
const NoopStatistic & operator=(unsigned Val)
Definition: Statistic.h:142
const char * getName() const
Definition: Statistic.h:57
const char * getDesc() const
Definition: Statistic.h:58
TrackingStatistic(const char *DebugType, const char *Name, const char *Desc)
Definition: Statistic.h:66
std::atomic< unsigned > Value
Definition: Statistic.h:63
std::unique_ptr< raw_fd_ostream > CreateInfoOutputFile()
Return a file stream to print our output on.
Definition: Timer.cpp:58
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:432
const NoopStatistic & operator+=(const unsigned &V)
Definition: Statistic.h:152
const NoopStatistic & operator--()
Definition: Statistic.h:148
std::atomic< bool > Initialized
Definition: Statistic.h:64
TrackingStatistic & init()
Definition: Statistic.h:124
const std::vector< std::pair< StringRef, unsigned > > GetStatistics()
Get the statistics.
Definition: Statistic.cpp:254
const TrackingStatistic & operator--()
Definition: Statistic.h:89
unsigned operator--(int)
Definition: Statistic.h:150
void PrintStatisticsJSON(raw_ostream &OS)
Print statistics in JSON format.
Definition: Statistic.cpp:200
unsigned getValue() const
Definition: Statistic.h:69
unsigned operator++(int)
Definition: Statistic.h:146
StatisticBase(const char *DebugType, const char *Name, const char *Desc)
Definition: Statistic.h:53
unsigned operator--(int)
Definition: Statistic.h:94
const char * getDebugType() const
Definition: Statistic.h:56
const TrackingStatistic & operator+=(unsigned V)
Definition: Statistic.h:99
const NoopStatistic & operator++()
Definition: Statistic.h:144
void PrintStatistics()
Print statistics to the file returned by CreateInfoOutputFile().
Definition: Statistic.cpp:226
const TrackingStatistic & operator-=(unsigned V)
Definition: Statistic.h:106
const char * Name
Definition: Statistic.h:50
const char * Desc
Definition: Statistic.h:51
bool AreStatisticsEnabled()
Check if statistics are enabled.
Definition: Statistic.cpp:131