Statistic.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. #ifndef LLVM_ADT_STATISTIC_H
  25. #define LLVM_ADT_STATISTIC_H
  26. #include "llvm/Config/llvm-config.h"
  27. #include "llvm/Support/Compiler.h"
  28. #include <atomic>
  29. #include <memory>
  30. #include <vector>
  31. // Determine whether statistics should be enabled. We must do it here rather
  32. // than in CMake because multi-config generators cannot determine this at
  33. // configure time.
  34. #if !defined(NDEBUG) || LLVM_FORCE_ENABLE_STATS
  35. #define LLVM_ENABLE_STATS 1
  36. #else
  37. #define LLVM_ENABLE_STATS 0
  38. #endif
  39. namespace llvm {
  40. class raw_ostream;
  41. class raw_fd_ostream;
  42. class StringRef;
  43. class TrackingStatistic {
  44. public:
  45. const char *const DebugType;
  46. const char *const Name;
  47. const char *const Desc;
  48. std::atomic<unsigned> Value;
  49. std::atomic<bool> Initialized;
  50. constexpr TrackingStatistic(const char *DebugType, const char *Name,
  51. const char *Desc)
  52. : DebugType(DebugType), Name(Name), Desc(Desc), Value(0),
  53. Initialized(false) {}
  54. const char *getDebugType() const { return DebugType; }
  55. const char *getName() const { return Name; }
  56. const char *getDesc() const { return Desc; }
  57. unsigned getValue() const { return Value.load(std::memory_order_relaxed); }
  58. // Allow use of this class as the value itself.
  59. operator unsigned() const { return getValue(); }
  60. const TrackingStatistic &operator=(unsigned Val) {
  61. Value.store(Val, std::memory_order_relaxed);
  62. return init();
  63. }
  64. const TrackingStatistic &operator++() {
  65. Value.fetch_add(1, std::memory_order_relaxed);
  66. return init();
  67. }
  68. unsigned operator++(int) {
  69. init();
  70. return Value.fetch_add(1, std::memory_order_relaxed);
  71. }
  72. const TrackingStatistic &operator--() {
  73. Value.fetch_sub(1, std::memory_order_relaxed);
  74. return init();
  75. }
  76. unsigned operator--(int) {
  77. init();
  78. return Value.fetch_sub(1, std::memory_order_relaxed);
  79. }
  80. const TrackingStatistic &operator+=(unsigned V) {
  81. if (V == 0)
  82. return *this;
  83. Value.fetch_add(V, std::memory_order_relaxed);
  84. return init();
  85. }
  86. const TrackingStatistic &operator-=(unsigned V) {
  87. if (V == 0)
  88. return *this;
  89. Value.fetch_sub(V, std::memory_order_relaxed);
  90. return init();
  91. }
  92. void updateMax(unsigned V) {
  93. unsigned PrevMax = Value.load(std::memory_order_relaxed);
  94. // Keep trying to update max until we succeed or another thread produces
  95. // a bigger max than us.
  96. while (V > PrevMax && !Value.compare_exchange_weak(
  97. PrevMax, V, std::memory_order_relaxed)) {
  98. }
  99. init();
  100. }
  101. protected:
  102. TrackingStatistic &init() {
  103. if (!Initialized.load(std::memory_order_acquire))
  104. RegisterStatistic();
  105. return *this;
  106. }
  107. void RegisterStatistic();
  108. };
  109. class NoopStatistic {
  110. public:
  111. NoopStatistic(const char * /*DebugType*/, const char * /*Name*/,
  112. const char * /*Desc*/) {}
  113. unsigned getValue() const { return 0; }
  114. // Allow use of this class as the value itself.
  115. operator unsigned() const { return 0; }
  116. const NoopStatistic &operator=(unsigned Val) { return *this; }
  117. const NoopStatistic &operator++() { return *this; }
  118. unsigned operator++(int) { return 0; }
  119. const NoopStatistic &operator--() { return *this; }
  120. unsigned operator--(int) { return 0; }
  121. const NoopStatistic &operator+=(const unsigned &V) { return *this; }
  122. const NoopStatistic &operator-=(const unsigned &V) { return *this; }
  123. void updateMax(unsigned V) {}
  124. };
  125. #if LLVM_ENABLE_STATS
  126. using Statistic = TrackingStatistic;
  127. #else
  128. using Statistic = NoopStatistic;
  129. #endif
  130. // STATISTIC - A macro to make definition of statistics really simple. This
  131. // automatically passes the DEBUG_TYPE of the file into the statistic.
  132. #define STATISTIC(VARNAME, DESC) \
  133. static llvm::Statistic VARNAME = {DEBUG_TYPE, #VARNAME, DESC}
  134. // ALWAYS_ENABLED_STATISTIC - A macro to define a statistic like STATISTIC but
  135. // it is enabled even if LLVM_ENABLE_STATS is off.
  136. #define ALWAYS_ENABLED_STATISTIC(VARNAME, DESC) \
  137. static llvm::TrackingStatistic VARNAME = {DEBUG_TYPE, #VARNAME, DESC}
  138. /// Enable the collection and printing of statistics.
  139. void EnableStatistics(bool DoPrintOnExit = true);
  140. /// Check if statistics are enabled.
  141. bool AreStatisticsEnabled();
  142. /// Return a file stream to print our output on.
  143. std::unique_ptr<raw_fd_ostream> CreateInfoOutputFile();
  144. /// Print statistics to the file returned by CreateInfoOutputFile().
  145. void PrintStatistics();
  146. /// Print statistics to the given output stream.
  147. void PrintStatistics(raw_ostream &OS);
  148. /// Print statistics in JSON format. This does include all global timers (\see
  149. /// Timer, TimerGroup). Note that the timers are cleared after printing and will
  150. /// not be printed in human readable form or in a second call of
  151. /// PrintStatisticsJSON().
  152. void PrintStatisticsJSON(raw_ostream &OS);
  153. /// Get the statistics. This can be used to look up the value of
  154. /// statistics without needing to parse JSON.
  155. ///
  156. /// This function does not prevent statistics being updated by other threads
  157. /// during it's execution. It will return the value at the point that it is
  158. /// read. However, it will prevent new statistics from registering until it
  159. /// completes.
  160. const std::vector<std::pair<StringRef, unsigned>> GetStatistics();
  161. /// Reset the statistics. This can be used to zero and de-register the
  162. /// statistics in order to measure a compilation.
  163. ///
  164. /// When this function begins to call destructors prior to returning, all
  165. /// statistics will be zero and unregistered. However, that might not remain the
  166. /// case by the time this function finishes returning. Whether update from other
  167. /// threads are lost or merely deferred until during the function return is
  168. /// timing sensitive.
  169. ///
  170. /// Callers who intend to use this to measure statistics for a single
  171. /// compilation should ensure that no compilations are in progress at the point
  172. /// this function is called and that only one compilation executes until calling
  173. /// GetStatistics().
  174. void ResetStatistics();
  175. } // end namespace llvm
  176. #endif // LLVM_ADT_STATISTIC_H