ilya-biryukov updated this revision to Diff 126329.
ilya-biryukov added a comment.

Updated the patch after changes to Context


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D40489

Files:
  clangd/Trace.cpp
  clangd/Trace.h

Index: clangd/Trace.h
===================================================================
--- clangd/Trace.h
+++ clangd/Trace.h
@@ -28,12 +28,21 @@
 namespace trace {
 
 /// A consumer of trace events. The events are produced by Spans and trace::log.
+/// Calls to begin_event and end_event with the same name are always properly
+/// nested by using a RAII object (Span).
+/// Implmentations of this interface must be thread-safe.
 class EventTracer {
 public:
   virtual ~EventTracer() = default;
-  /// Consume a trace event.
-  virtual void event(const Context &Ctx, llvm::StringRef Phase,
-                     json::obj &&Contents) = 0;
+
+  /// Called when event with \p Name starts.
+  virtual void begin_event(const Context &Ctx, llvm::StringRef Name) = 0;
+  /// Called when event with \p Name ends.
+  virtual void end_event(const Context &Ctx, llvm::StringRef Name,
+                         json::obj &&Args) = 0;
+  /// Called for instant events.
+  virtual void instant_event(const Context &Ctx, llvm::StringRef Name,
+                             json::obj &&Args) = 0;
 };
 
 /// Sets up a global EventTracer that consumes events produced by Span and
@@ -67,15 +76,16 @@
 /// SomeJSONExpr is evaluated and copied only if actually needed.
 class Span {
 public:
-  Span(const Context &Ctx, std::string Name);
+  Span(const Context &Ctx, llvm::StringRef Name);
   ~Span();
 
   /// Returns mutable span metadata if this span is interested.
   /// Prefer to use SPAN_ATTACH rather than accessing this directly.
   json::obj *args() { return Args.get(); }
 
 private:
   llvm::Optional<Context> Ctx;
+  std::string Name;
   std::unique_ptr<json::obj> Args;
 };
 
Index: clangd/Trace.cpp
===================================================================
--- clangd/Trace.cpp
+++ clangd/Trace.cpp
@@ -44,10 +44,23 @@
     Out.flush();
   }
 
+  void begin_event(const Context &Ctx, llvm::StringRef Name) override {
+    jsonEvent("B", json::obj{{"name", Name}});
+  }
+
+  void end_event(const Context &Ctx, llvm::StringRef Name,
+                 json::obj &&Args) override {
+    jsonEvent("E", json::obj{{"args", std::move(Args)}});
+  }
+
+  void instant_event(const Context &Ctx, llvm::StringRef Name,
+                     json::obj &&Args) override {
+    jsonEvent("i", json::obj{{"name", Name}, {"args", std::move(Args)}});
+  }
+
   // Record an event on the current thread. ph, pid, tid, ts are set.
   // Contents must be a list of the other JSON key/values.
-  void event(const Context &Ctx, StringRef Phase,
-             json::obj &&Contents) override {
+  void jsonEvent(StringRef Phase, json::obj &&Contents) {
     uint64_t TID = get_threadid();
     std::lock_guard<std::mutex> Lock(Mu);
     // If we haven't already, emit metadata describing this thread.
@@ -109,30 +122,24 @@
 void log(const Context &Ctx, const Twine &Message) {
   if (!T)
     return;
-  T->event(Ctx, "i",
-           json::obj{
-               {"name", "Log"},
-               {"args", json::obj{{"Message", Message.str()}}},
-           });
+  T->instant_event(Ctx, "Log", json::obj{{"Message", Message.str()}});
 }
 
-Span::Span(const Context &Ctx, std::string Name) {
+Span::Span(const Context &Ctx, llvm::StringRef Name) : Name(Name) {
   if (!T)
     return;
   // Clone the context, so that the original Context can be moved.
   this->Ctx.emplace(Ctx.clone());
 
-  T->event(*this->Ctx, "B", json::obj{{"name", std::move(Name)}});
+  T->begin_event(*this->Ctx, this->Name);
   Args = llvm::make_unique<json::obj>();
 }
 
 Span::~Span() {
   if (!T)
     return;
-  if (!Args)
-    Args = llvm::make_unique<json::obj>();
-  T->event(*Ctx, "E",
-           Args ? json::obj{{"args", std::move(*Args)}} : json::obj{});
+  assert(Args && "Args can't be null at this point");
+  T->end_event(*this->Ctx, Name, std::move(*Args));
 }
 
 } // namespace trace
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to