================
@@ -73,9 +104,46 @@ class TelemetryManager : public llvm::telemetry::Manager {
 
 private:
   std::unique_ptr<llvm::telemetry::Config> m_config;
+  // Each instance of a TelemetryManager is assigned a unique ID.
+  const std::string m_id;
+
   static std::unique_ptr<TelemetryManager> g_instance;
 };
 
+/// Helper RAII class for collecting telemetry.
+class ScopeTelemetryCollector {
+public:
+  ScopeTelemetryCollector() {
+    if (TelemetryEnabled())
+      m_start = std::chrono::steady_clock::now();
+  }
+
+  ~ScopeTelemetryCollector() {
+    while (!m_exit_funcs.empty()) {
+      (m_exit_funcs.top())();
+      m_exit_funcs.pop();
+    }
+  }
+
+  bool TelemetryEnabled() {
+    TelemetryManager *instance = TelemetryManager::GetInstance();
+    return instance != nullptr && instance->GetConfig()->EnableTelemetry;
+  }
+
+  SteadyTimePoint GetStartTime() { return m_start; }
+  SteadyTimePoint GetCurrentTime() { return std::chrono::steady_clock::now(); }
+
+  template <typename Fp> void RunAtScopeExit(Fp &&F) {
+    assert(llvm::telemetry::Config::BuildTimeEnableTelemetry &&
+           "Telemetry should have been enabled");
+    m_exit_funcs.push(std::forward<Fp>(F));
+  }
+
+private:
+  SteadyTimePoint m_start;
+  std::stack<std::function<void()>> m_exit_funcs;
----------------
JDevlieghere wrote:

Does this actually need to be a `std::stack`? By default it wraps a 
`std::deque` and I don't think you need to be able to pop from both sides. You 
can have a std::stack that uses a std::vector underneath, but it's much more 
common to just push and pop to a `vector` directly.

https://github.com/llvm/llvm-project/pull/127696
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to