summaryrefslogtreecommitdiff
path: root/include/llvm/Support/DebugCounter.h
diff options
context:
space:
mode:
authorDimitry Andric <dim@FreeBSD.org>2019-01-19 10:01:25 +0000
committerDimitry Andric <dim@FreeBSD.org>2019-01-19 10:01:25 +0000
commitd8e91e46262bc44006913e6796843909f1ac7bcd (patch)
tree7d0c143d9b38190e0fa0180805389da22cd834c5 /include/llvm/Support/DebugCounter.h
parentb7eb8e35e481a74962664b63dfb09483b200209a (diff)
Notes
Diffstat (limited to 'include/llvm/Support/DebugCounter.h')
-rw-r--r--include/llvm/Support/DebugCounter.h30
1 files changed, 25 insertions, 5 deletions
diff --git a/include/llvm/Support/DebugCounter.h b/include/llvm/Support/DebugCounter.h
index 250fc6bb1f5c..6eadd5c6aeff 100644
--- a/include/llvm/Support/DebugCounter.h
+++ b/include/llvm/Support/DebugCounter.h
@@ -55,6 +55,8 @@ namespace llvm {
class DebugCounter {
public:
+ ~DebugCounter();
+
/// Returns a reference to the singleton instance.
static DebugCounter &instance();
@@ -70,10 +72,9 @@ public:
return instance().addCounter(Name, Desc);
}
inline static bool shouldExecute(unsigned CounterName) {
-// Compile to nothing when debugging is off
-#ifdef NDEBUG
- return true;
-#else
+ if (!isCountingEnabled())
+ return true;
+
auto &Us = instance();
auto Result = Us.Counters.find(CounterName);
if (Result != Us.Counters.end()) {
@@ -93,7 +94,6 @@ public:
}
// Didn't find the counter, should we warn?
return true;
-#endif // NDEBUG
}
// Return true if a given counter had values set (either programatically or on
@@ -142,7 +142,23 @@ public:
}
CounterVector::const_iterator end() const { return RegisteredCounters.end(); }
+ // Force-enables counting all DebugCounters.
+ //
+ // Since DebugCounters are incompatible with threading (not only do they not
+ // make sense, but we'll also see data races), this should only be used in
+ // contexts where we're certain we won't spawn threads.
+ static void enableAllCounters() { instance().Enabled = true; }
+
private:
+ static bool isCountingEnabled() {
+// Compile to nothing when debugging is off
+#ifdef NDEBUG
+ return false;
+#else
+ return instance().Enabled;
+#endif
+ }
+
unsigned addCounter(const std::string &Name, const std::string &Desc) {
unsigned Result = RegisteredCounters.insert(Name);
Counters[Result] = {};
@@ -159,6 +175,10 @@ private:
};
DenseMap<unsigned, CounterInfo> Counters;
CounterVector RegisteredCounters;
+
+ // Whether we should do DebugCounting at all. DebugCounters aren't
+ // thread-safe, so this should always be false in multithreaded scenarios.
+ bool Enabled = false;
};
#define DEBUG_COUNTER(VARNAME, COUNTERNAME, DESC) \