File indexing completed on 2026-05-10 08:43:09
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026 #ifndef LLVM_ADT_STATISTIC_H
0027 #define LLVM_ADT_STATISTIC_H
0028
0029 #include "llvm/Config/llvm-config.h"
0030 #include <atomic>
0031 #include <memory>
0032 #include <vector>
0033
0034
0035
0036
0037 #if !defined(NDEBUG) || LLVM_FORCE_ENABLE_STATS
0038 #define LLVM_ENABLE_STATS 1
0039 #else
0040 #define LLVM_ENABLE_STATS 0
0041 #endif
0042
0043 namespace llvm {
0044
0045 class raw_ostream;
0046 class raw_fd_ostream;
0047 class StringRef;
0048
0049 class TrackingStatistic {
0050 public:
0051 const char *const DebugType;
0052 const char *const Name;
0053 const char *const Desc;
0054
0055 std::atomic<uint64_t> Value;
0056 std::atomic<bool> Initialized;
0057
0058 constexpr TrackingStatistic(const char *DebugType, const char *Name,
0059 const char *Desc)
0060 : DebugType(DebugType), Name(Name), Desc(Desc), Value(0),
0061 Initialized(false) {}
0062
0063 const char *getDebugType() const { return DebugType; }
0064 const char *getName() const { return Name; }
0065 const char *getDesc() const { return Desc; }
0066
0067 uint64_t getValue() const { return Value.load(std::memory_order_relaxed); }
0068
0069
0070 operator uint64_t() const { return getValue(); }
0071
0072 const TrackingStatistic &operator=(uint64_t Val) {
0073 Value.store(Val, std::memory_order_relaxed);
0074 return init();
0075 }
0076
0077 const TrackingStatistic &operator++() {
0078 Value.fetch_add(1, std::memory_order_relaxed);
0079 return init();
0080 }
0081
0082 uint64_t operator++(int) {
0083 init();
0084 return Value.fetch_add(1, std::memory_order_relaxed);
0085 }
0086
0087 const TrackingStatistic &operator--() {
0088 Value.fetch_sub(1, std::memory_order_relaxed);
0089 return init();
0090 }
0091
0092 uint64_t operator--(int) {
0093 init();
0094 return Value.fetch_sub(1, std::memory_order_relaxed);
0095 }
0096
0097 const TrackingStatistic &operator+=(uint64_t V) {
0098 if (V == 0)
0099 return *this;
0100 Value.fetch_add(V, std::memory_order_relaxed);
0101 return init();
0102 }
0103
0104 const TrackingStatistic &operator-=(uint64_t V) {
0105 if (V == 0)
0106 return *this;
0107 Value.fetch_sub(V, std::memory_order_relaxed);
0108 return init();
0109 }
0110
0111 void updateMax(uint64_t V) {
0112 uint64_t PrevMax = Value.load(std::memory_order_relaxed);
0113
0114
0115 while (V > PrevMax && !Value.compare_exchange_weak(
0116 PrevMax, V, std::memory_order_relaxed)) {
0117 }
0118 init();
0119 }
0120
0121 protected:
0122 TrackingStatistic &init() {
0123 if (!Initialized.load(std::memory_order_acquire))
0124 RegisterStatistic();
0125 return *this;
0126 }
0127
0128 void RegisterStatistic();
0129 };
0130
0131 class NoopStatistic {
0132 public:
0133 NoopStatistic(const char * , const char * ,
0134 const char * ) {}
0135
0136 uint64_t getValue() const { return 0; }
0137
0138
0139 operator uint64_t() const { return 0; }
0140
0141 const NoopStatistic &operator=(uint64_t Val) { return *this; }
0142
0143 const NoopStatistic &operator++() { return *this; }
0144
0145 uint64_t operator++(int) { return 0; }
0146
0147 const NoopStatistic &operator--() { return *this; }
0148
0149 uint64_t operator--(int) { return 0; }
0150
0151 const NoopStatistic &operator+=(const uint64_t &V) { return *this; }
0152
0153 const NoopStatistic &operator-=(const uint64_t &V) { return *this; }
0154
0155 void updateMax(uint64_t V) {}
0156 };
0157
0158 #if LLVM_ENABLE_STATS
0159 using Statistic = TrackingStatistic;
0160 #else
0161 using Statistic = NoopStatistic;
0162 #endif
0163
0164
0165
0166 #define STATISTIC(VARNAME, DESC) \
0167 static llvm::Statistic VARNAME = {DEBUG_TYPE, #VARNAME, DESC}
0168
0169
0170
0171 #define ALWAYS_ENABLED_STATISTIC(VARNAME, DESC) \
0172 static llvm::TrackingStatistic VARNAME = {DEBUG_TYPE, #VARNAME, DESC}
0173
0174
0175 void EnableStatistics(bool DoPrintOnExit = true);
0176
0177
0178 bool AreStatisticsEnabled();
0179
0180
0181 std::unique_ptr<raw_ostream> CreateInfoOutputFile();
0182
0183
0184 void PrintStatistics();
0185
0186
0187 void PrintStatistics(raw_ostream &OS);
0188
0189
0190
0191
0192
0193 void PrintStatisticsJSON(raw_ostream &OS);
0194
0195
0196
0197
0198
0199
0200
0201
0202 std::vector<std::pair<StringRef, uint64_t>> GetStatistics();
0203
0204
0205
0206
0207
0208
0209
0210
0211
0212
0213
0214
0215
0216
0217 void ResetStatistics();
0218
0219 }
0220
0221 #endif