8
8
// ===----------------------------------------------------------------------===//
9
9
//
10
10
// Supports writing performance traces describing clangd's behavior.
11
- // Traces are written in the Trace Event format supported by chrome's trace
12
- // viewer (chrome://tracing).
11
+ // Traces are consumed by implementations of the EventTracer interface.
13
12
//
14
- // The format is documented here:
15
- // https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU/preview
16
13
//
17
14
// All APIs are no-ops unless a Session is active (created by ClangdMain).
18
15
//
21
18
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_TRACE_H_
22
19
#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_TRACE_H_
23
20
21
+ #include " Context.h"
24
22
#include " JSONExpr.h"
25
23
#include " llvm/ADT/Twine.h"
26
24
#include " llvm/Support/raw_ostream.h"
@@ -29,39 +27,55 @@ namespace clang {
29
27
namespace clangd {
30
28
namespace trace {
31
29
32
- // A session directs the output of trace events. Only one Session can exist.
33
- // It should be created before clangd threads are spawned, and destroyed after
34
- // they exit.
35
- // TODO: we may want to add pluggable support for other tracing backends.
30
+ // / A consumer of trace events. The events are produced by Spans and trace::log.
31
+ class EventTracer {
32
+ public:
33
+ virtual ~EventTracer () = default ;
34
+ // / Consume a trace event.
35
+ virtual void event (const Context &Ctx, llvm::StringRef Phase,
36
+ json::obj &&Contents) = 0;
37
+ };
38
+
39
+ // / Sets up a global EventTracer that consumes events produced by Span and
40
+ // / trace::log. Only one TracingSession can be active at a time and it should be
41
+ // / set up before calling any clangd-specific functions.
36
42
class Session {
37
43
public:
38
- // Starts a sessions capturing trace events and writing Trace Event JSON.
39
- static std::unique_ptr<Session> create (llvm::raw_ostream &OS,
40
- bool Pretty = false );
44
+ Session (EventTracer &Tracer);
41
45
~Session ();
42
-
43
- private:
44
- Session () = default ;
45
46
};
46
47
47
- // Records a single instant event, associated with the current thread.
48
- void log (const llvm::Twine &Name);
48
+ // / Create an instance of EventTracer that produces an output in the Trace Event
49
+ // / format supported by Chrome's trace viewer (chrome://tracing).
50
+ // /
51
+ // / The format is documented here:
52
+ // / https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU/preview
53
+ // /
54
+ // / The implementation supports concurrent calls and can be used as a global
55
+ // / tracer (i.e., can be put into a global Context).
56
+ std::unique_ptr<EventTracer> createJSONTracer (llvm::raw_ostream &OS,
57
+ bool Pretty = false );
49
58
50
- // Records an event whose duration is the lifetime of the Span object.
51
- //
52
- // Arbitrary JSON metadata can be attached while this span is active:
53
- // SPAN_ATTACH(MySpan, "Payload", SomeJSONExpr);
54
- // SomeJSONExpr is evaluated and copied only if actually needed.
59
+ // / Records a single instant event, associated with the current thread.
60
+ void log (const Context &Ctx, const llvm::Twine &Name);
61
+
62
+ // / Records an event whose duration is the lifetime of the Span object.
63
+ // / This is the main public interface for producing tracing events.
64
+ // /
65
+ // / Arbitrary JSON metadata can be attached while this span is active:
66
+ // / SPAN_ATTACH(MySpan, "Payload", SomeJSONExpr);
67
+ // / SomeJSONExpr is evaluated and copied only if actually needed.
55
68
class Span {
56
69
public:
57
- Span (std::string Name);
70
+ Span (const Context &Ctx, std::string Name);
58
71
~Span ();
59
72
60
- // Returns mutable span metadata if this span is interested.
61
- // Prefer to use SPAN_ATTACH rather than accessing this directly.
73
+ // / Returns mutable span metadata if this span is interested.
74
+ // / Prefer to use SPAN_ATTACH rather than accessing this directly.
62
75
json::obj *args () { return Args.get (); }
63
76
64
77
private:
78
+ llvm::Optional<Context> Ctx;
65
79
std::unique_ptr<json::obj> Args;
66
80
};
67
81
0 commit comments