[llvm] r374490 - [Stats] Add ALWAYS_ENABLED_STATISTIC enabled regardless of LLVM_ENABLE_STATS.

Volodymyr Sapsai via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 10 17:57:41 PDT 2019


Author: vsapsai
Date: Thu Oct 10 17:57:41 2019
New Revision: 374490

URL: http://llvm.org/viewvc/llvm-project?rev=374490&view=rev
Log:
[Stats] Add ALWAYS_ENABLED_STATISTIC enabled regardless of LLVM_ENABLE_STATS.

The intended usage is to measure relatively expensive operations. So the
cost of the statistic is negligible compared to the cost of a measured
operation and can be enabled all the time without impairing the
compilation time.

rdar://problem/55715134

Reviewers: dsanders, bogner, rtereshin

Reviewed By: dsanders

Subscribers: hiraditya, jkorous, dexonsmith, ributzka, cfe-commits, llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D68252

Modified:
    llvm/trunk/include/llvm/ADT/Statistic.h
    llvm/trunk/lib/Support/Statistic.cpp
    llvm/trunk/unittests/ADT/StatisticTest.cpp

Modified: llvm/trunk/include/llvm/ADT/Statistic.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/Statistic.h?rev=374490&r1=374489&r2=374490&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/Statistic.h (original)
+++ llvm/trunk/include/llvm/ADT/Statistic.h Thu Oct 10 17:57:41 2019
@@ -44,38 +44,39 @@ class raw_ostream;
 class raw_fd_ostream;
 class StringRef;
 
-class Statistic {
+class StatisticBase {
 public:
   const char *DebugType;
   const char *Name;
   const char *Desc;
-  std::atomic<unsigned> Value;
-  std::atomic<bool> Initialized;
 
-  unsigned getValue() const { return Value.load(std::memory_order_relaxed); }
+  StatisticBase(const char *DebugType, const char *Name, const char *Desc)
+      : DebugType(DebugType), Name(Name), Desc(Desc) {}
+
   const char *getDebugType() const { return DebugType; }
   const char *getName() const { return Name; }
   const char *getDesc() const { return Desc; }
+};
 
-  /// construct - This should only be called for non-global statistics.
-  void construct(const char *debugtype, const char *name, const char *desc) {
-    DebugType = debugtype;
-    Name = name;
-    Desc = desc;
-    Value = 0;
-    Initialized = false;
-  }
+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); }
 
   // Allow use of this class as the value itself.
   operator unsigned() const { return getValue(); }
 
-#if LLVM_ENABLE_STATS
-   const Statistic &operator=(unsigned Val) {
+  const TrackingStatistic &operator=(unsigned Val) {
     Value.store(Val, std::memory_order_relaxed);
     return init();
   }
 
-  const Statistic &operator++() {
+  const TrackingStatistic &operator++() {
     Value.fetch_add(1, std::memory_order_relaxed);
     return init();
   }
@@ -85,7 +86,7 @@ public:
     return Value.fetch_add(1, std::memory_order_relaxed);
   }
 
-  const Statistic &operator--() {
+  const TrackingStatistic &operator--() {
     Value.fetch_sub(1, std::memory_order_relaxed);
     return init();
   }
@@ -95,14 +96,14 @@ public:
     return Value.fetch_sub(1, std::memory_order_relaxed);
   }
 
-  const Statistic &operator+=(unsigned V) {
+  const TrackingStatistic &operator+=(unsigned V) {
     if (V == 0)
       return *this;
     Value.fetch_add(V, std::memory_order_relaxed);
     return init();
   }
 
-  const Statistic &operator-=(unsigned V) {
+  const TrackingStatistic &operator-=(unsigned V) {
     if (V == 0)
       return *this;
     Value.fetch_sub(V, std::memory_order_relaxed);
@@ -119,54 +120,57 @@ public:
     init();
   }
 
-#else  // Statistics are disabled in release builds.
-
-  const Statistic &operator=(unsigned Val) {
+protected:
+  TrackingStatistic &init() {
+    if (!Initialized.load(std::memory_order_acquire))
+      RegisterStatistic();
     return *this;
   }
 
-  const Statistic &operator++() {
-    return *this;
-  }
+  void RegisterStatistic();
+};
 
-  unsigned operator++(int) {
-    return 0;
-  }
+class NoopStatistic : public StatisticBase {
+public:
+  using StatisticBase::StatisticBase;
 
-  const Statistic &operator--() {
-    return *this;
-  }
+  unsigned getValue() const { return 0; }
 
-  unsigned operator--(int) {
-    return 0;
-  }
+  // Allow use of this class as the value itself.
+  operator unsigned() const { return 0; }
 
-  const Statistic &operator+=(const unsigned &V) {
-    return *this;
-  }
+  const NoopStatistic &operator=(unsigned Val) { return *this; }
 
-  const Statistic &operator-=(const unsigned &V) {
-    return *this;
-  }
+  const NoopStatistic &operator++() { return *this; }
 
-  void updateMax(unsigned V) {}
+  unsigned operator++(int) { return 0; }
 
-#endif  // LLVM_ENABLE_STATS
+  const NoopStatistic &operator--() { return *this; }
 
-protected:
-  Statistic &init() {
-    if (!Initialized.load(std::memory_order_acquire))
-      RegisterStatistic();
-    return *this;
-  }
+  unsigned operator--(int) { return 0; }
 
-  void RegisterStatistic();
+  const NoopStatistic &operator+=(const unsigned &V) { return *this; }
+
+  const NoopStatistic &operator-=(const unsigned &V) { return *this; }
+
+  void updateMax(unsigned V) {}
 };
 
+#if LLVM_ENABLE_STATS
+using Statistic = TrackingStatistic;
+#else
+using Statistic = NoopStatistic;
+#endif
+
 // STATISTIC - A macro to make definition of statistics really simple.  This
 // automatically passes the DEBUG_TYPE of the file into the statistic.
 #define STATISTIC(VARNAME, DESC)                                               \
-  static llvm::Statistic VARNAME = {DEBUG_TYPE, #VARNAME, DESC, {0}, {false}}
+  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}
 
 /// Enable the collection and printing of statistics.
 void EnableStatistics(bool PrintOnExit = true);

Modified: llvm/trunk/lib/Support/Statistic.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Statistic.cpp?rev=374490&r1=374489&r2=374490&view=diff
==============================================================================
--- llvm/trunk/lib/Support/Statistic.cpp (original)
+++ llvm/trunk/lib/Support/Statistic.cpp Thu Oct 10 17:57:41 2019
@@ -57,7 +57,7 @@ namespace {
 /// This class is also used to look up statistic values from applications that
 /// use LLVM.
 class StatisticInfo {
-  std::vector<Statistic*> Stats;
+  std::vector<TrackingStatistic *> Stats;
 
   friend void llvm::PrintStatistics();
   friend void llvm::PrintStatistics(raw_ostream &OS);
@@ -66,14 +66,12 @@ class StatisticInfo {
   /// Sort statistics by debugtype,name,description.
   void sort();
 public:
-  using const_iterator = std::vector<Statistic *>::const_iterator;
+  using const_iterator = std::vector<TrackingStatistic *>::const_iterator;
 
   StatisticInfo();
   ~StatisticInfo();
 
-  void addStatistic(Statistic *S) {
-    Stats.push_back(S);
-  }
+  void addStatistic(TrackingStatistic *S) { Stats.push_back(S); }
 
   const_iterator begin() const { return Stats.begin(); }
   const_iterator end() const { return Stats.end(); }
@@ -90,7 +88,7 @@ static ManagedStatic<sys::SmartMutex<tru
 
 /// RegisterStatistic - The first time a statistic is bumped, this method is
 /// called.
-void Statistic::RegisterStatistic() {
+void TrackingStatistic::RegisterStatistic() {
   // If stats are enabled, inform StatInfo that this statistic should be
   // printed.
   // llvm_shutdown calls destructors while holding the ManagedStatic mutex.
@@ -135,15 +133,16 @@ bool llvm::AreStatisticsEnabled() {
 }
 
 void StatisticInfo::sort() {
-  llvm::stable_sort(Stats, [](const Statistic *LHS, const Statistic *RHS) {
-    if (int Cmp = std::strcmp(LHS->getDebugType(), RHS->getDebugType()))
-      return Cmp < 0;
+  llvm::stable_sort(
+      Stats, [](const TrackingStatistic *LHS, const TrackingStatistic *RHS) {
+        if (int Cmp = std::strcmp(LHS->getDebugType(), RHS->getDebugType()))
+          return Cmp < 0;
 
-    if (int Cmp = std::strcmp(LHS->getName(), RHS->getName()))
-      return Cmp < 0;
+        if (int Cmp = std::strcmp(LHS->getName(), RHS->getName()))
+          return Cmp < 0;
 
-    return std::strcmp(LHS->getDesc(), RHS->getDesc()) < 0;
-  });
+        return std::strcmp(LHS->getDesc(), RHS->getDesc()) < 0;
+      });
 }
 
 void StatisticInfo::reset() {
@@ -207,7 +206,7 @@ void llvm::PrintStatisticsJSON(raw_ostre
   // Print all of the statistics.
   OS << "{\n";
   const char *delim = "";
-  for (const Statistic *Stat : Stats.Stats) {
+  for (const TrackingStatistic *Stat : Stats.Stats) {
     OS << delim;
     assert(yaml::needsQuotes(Stat->getDebugType()) == yaml::QuotingType::None &&
            "Statistic group/type name is simple.");

Modified: llvm/trunk/unittests/ADT/StatisticTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/StatisticTest.cpp?rev=374490&r1=374489&r2=374490&view=diff
==============================================================================
--- llvm/trunk/unittests/ADT/StatisticTest.cpp (original)
+++ llvm/trunk/unittests/ADT/StatisticTest.cpp Thu Oct 10 17:57:41 2019
@@ -17,6 +17,7 @@ namespace {
 #define DEBUG_TYPE "unittest"
 STATISTIC(Counter, "Counts things");
 STATISTIC(Counter2, "Counts other things");
+ALWAYS_ENABLED_STATISTIC(AlwaysCounter, "Counts things always");
 
 #if LLVM_ENABLE_STATS
 static void
@@ -43,6 +44,12 @@ TEST(StatisticTest, Count) {
 #else
   EXPECT_EQ(Counter, 0u);
 #endif
+
+  AlwaysCounter = 0;
+  EXPECT_EQ(AlwaysCounter, 0u);
+  AlwaysCounter++;
+  ++AlwaysCounter;
+  EXPECT_EQ(AlwaysCounter, 2u);
 }
 
 TEST(StatisticTest, Assign) {
@@ -54,6 +61,9 @@ TEST(StatisticTest, Assign) {
 #else
   EXPECT_EQ(Counter, 0u);
 #endif
+
+  AlwaysCounter = 2;
+  EXPECT_EQ(AlwaysCounter, 2u);
 }
 
 TEST(StatisticTest, API) {




More information about the llvm-commits mailing list