clang-tools 10.0.0

Trace.h
Go to the documentation of this file.
1 //===--- Trace.h - Performance tracing facilities ---------------*- 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 // Supports writing performance traces describing clangd's behavior.
10 // Traces are consumed by implementations of the EventTracer interface.
11 //
12 //
13 // All APIs are no-ops unless a Session is active (created by ClangdMain).
14 //
15 //===----------------------------------------------------------------------===//
16
17 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_TRACE_H_
18 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_TRACE_H_
19
20 #include "Context.h"
21 #include "llvm/ADT/Twine.h"
22 #include "llvm/Support/JSON.h"
23 #include "llvm/Support/raw_ostream.h"
24
25 namespace clang {
26 namespace clangd {
27 namespace trace {
28
29 /// A consumer of trace events. The events are produced by Spans and trace::log.
30 /// Implementations of this interface must be thread-safe.
31 class EventTracer {
32 public:
33 virtual ~EventTracer() = default;
34
35 /// Called when event that has a duration starts. \p Name describes the event.
36 /// Returns a derived context that will be destroyed when the event ends.
37 /// Usually implementations will store an object in the returned context
38 /// whose destructor records the end of the event.
39 /// The args are *Args, only complete when the event ends.
40 virtual Context beginSpan(llvm::StringRef Name, llvm::json::Object *Args) = 0;
41 // Called when a Span is destroyed (it may still be active on other threads).
42 // beginSpan() and endSpan() will always form a proper stack on each thread.
43 // The Context returned by beginSpan is active, but Args is not ready.
44 // Tracers should not override this unless they need to observe strict
45 // per-thread nesting. Instead they should observe context destruction.
46 virtual void endSpan(){};
47
48 /// Called for instant events.
49 virtual void instant(llvm::StringRef Name, llvm::json::Object &&Args) = 0;
50 };
51
52 /// Sets up a global EventTracer that consumes events produced by Span and
53 /// trace::log. Only one TracingSession can be active at a time and it should be
54 /// set up before calling any clangd-specific functions.
55 class Session {
56 public:
57 Session(EventTracer &Tracer);
58 ~Session();
59 };
60
61 /// Create an instance of EventTracer that produces an output in the Trace Event
62 /// format supported by Chrome's trace viewer (chrome://tracing).
63 ///
64 /// The format is documented here:
65 /// https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU/preview
66 std::unique_ptr<EventTracer> createJSONTracer(llvm::raw_ostream &OS,
67 bool Pretty = false);
68
69 /// Records a single instant event, associated with the current thread.
70 void log(const llvm::Twine &Name);
71
72 /// Records an event whose duration is the lifetime of the Span object.
73 /// This lifetime is extended when the span's context is reused.
74 ///
75 /// This is the main public interface for producing tracing events.
76 ///
77 /// Arbitrary JSON metadata can be attached while this span is active:
78 /// SPAN_ATTACH(MySpan, "Payload", SomeJSONExpr);
79 ///
80 /// SomeJSONExpr is evaluated and copied only if actually needed.
81 class Span {
82 public:
83 Span(llvm::Twine Name);
84 ~Span();
85
86 /// Mutable metadata, if this span is interested.
87 /// Prefer to use SPAN_ATTACH rather than accessing this directly.
88 /// The lifetime of Args is the whole event, even if the Span dies.
89 llvm::json::Object *const Args;
90
91 private:
92 WithContext RestoreCtx;
93 };
94
95 /// Attach a key-value pair to a Span event.
96 /// This is not threadsafe when used with the same Span.
97 #define SPAN_ATTACH(S, Name, Expr) \
98 do { \
99 if (auto *Args = (S).Args) \
100 (*Args)[Name] = Expr; \
101 } while (0)
102
103 } // namespace trace
104 } // namespace clangd
105 } // namespace clang
106
107 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_TRACE_H_
clang::clangd::trace::Session
Sets up a global EventTracer that consumes events produced by Span and trace::log.
Definition: Trace.h:55
Context.h
clang::clangd::trace::EventTracer::~EventTracer
virtual ~EventTracer()=default
clang::clangd::trace::EventTracer::instant
virtual void instant(llvm::StringRef Name, llvm::json::Object &&Args)=0
Called for instant events.
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:27
clang::clangd::Context
A context is an immutable container for per-request data that must be propagated through layers that ...
Definition: Context.h:69
clang::clangd::WithContext
WithContext replaces Context::current() with a provided scope.
Definition: Context.h:189
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::clangd::trace::log
void log(const llvm::Twine &Message)
Records a single instant event, associated with the current thread.
Definition: Trace.cpp:205
clang::clangd::trace::createJSONTracer
std::unique_ptr< EventTracer > createJSONTracer(llvm::raw_ostream &OS, bool Pretty)
Create an instance of EventTracer that produces an output in the Trace Event format supported by Chro...
Definition: Trace.cpp:200
clang::clangd::trace::Span::Args
llvm::json::Object *const Args
Mutable metadata, if this span is interested.
Definition: Trace.h:89
clang::clangd::trace::Span
Records an event whose duration is the lifetime of the Span object.
Definition: Trace.h:81
clang::clangd::trace::EventTracer::beginSpan
virtual Context beginSpan(llvm::StringRef Name, llvm::json::Object *Args)=0
Called when event that has a duration starts.
clang::clangd::trace::EventTracer
A consumer of trace events.
Definition: Trace.h:31
clang::clangd::trace::EventTracer::endSpan
virtual void endSpan()
Definition: Trace.h:46