Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 1 | //===--- Trace.cpp - Performance tracing facilities -----------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "Trace.h" |
Sam McCall | 1b475a1 | 2018-01-26 09:00:30 +0000 | [diff] [blame] | 11 | #include "Context.h" |
| 12 | #include "Function.h" |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/DenseSet.h" |
Sam McCall | 1b475a1 | 2018-01-26 09:00:30 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/ScopeExit.h" |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 15 | #include "llvm/Support/Chrono.h" |
| 16 | #include "llvm/Support/FormatProviders.h" |
| 17 | #include "llvm/Support/FormatVariadic.h" |
| 18 | #include "llvm/Support/Threading.h" |
Sam McCall | 7929d00 | 2018-02-15 08:40:54 +0000 | [diff] [blame] | 19 | #include <atomic> |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 20 | #include <mutex> |
| 21 | |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame^] | 22 | using namespace llvm; |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 23 | namespace clang { |
| 24 | namespace clangd { |
| 25 | namespace trace { |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 26 | |
| 27 | namespace { |
| 28 | // The current implementation is naive: each thread writes to Out guarded by Mu. |
| 29 | // Perhaps we should replace this by something that disturbs performance less. |
Ilya Biryukov | ee27d2e | 2017-12-14 15:04:59 +0000 | [diff] [blame] | 30 | class JSONTracer : public EventTracer { |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 31 | public: |
Ilya Biryukov | ee27d2e | 2017-12-14 15:04:59 +0000 | [diff] [blame] | 32 | JSONTracer(raw_ostream &Out, bool Pretty) |
Sam McCall | 9cfd9c9 | 2017-11-23 17:12:04 +0000 | [diff] [blame] | 33 | : Out(Out), Sep(""), Start(std::chrono::system_clock::now()), |
| 34 | JSONFormat(Pretty ? "{0:2}" : "{0}") { |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 35 | // The displayTimeUnit must be ns to avoid low-precision overlap |
| 36 | // calculations! |
| 37 | Out << R"({"displayTimeUnit":"ns","traceEvents":[)" |
| 38 | << "\n"; |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 39 | rawEvent("M", json::Object{ |
Sam McCall | 9cfd9c9 | 2017-11-23 17:12:04 +0000 | [diff] [blame] | 40 | {"name", "process_name"}, |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 41 | {"args", json::Object{{"name", "clangd"}}}, |
Sam McCall | 9cfd9c9 | 2017-11-23 17:12:04 +0000 | [diff] [blame] | 42 | }); |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Ilya Biryukov | ee27d2e | 2017-12-14 15:04:59 +0000 | [diff] [blame] | 45 | ~JSONTracer() { |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 46 | Out << "\n]}"; |
| 47 | Out.flush(); |
| 48 | } |
| 49 | |
Sam McCall | 7929d00 | 2018-02-15 08:40:54 +0000 | [diff] [blame] | 50 | // We stash a Span object in the context. It will record the start/end, |
| 51 | // and this also allows us to look up the parent Span's information. |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame^] | 52 | Context beginSpan(StringRef Name, json::Object *Args) override { |
Sam McCall | 892ea68 | 2018-02-15 14:16:17 +0000 | [diff] [blame] | 53 | return Context::current().derive( |
| 54 | SpanKey, llvm::make_unique<JSONSpan>(this, Name, Args)); |
Sam McCall | 7929d00 | 2018-02-15 08:40:54 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | // Trace viewer requires each thread to properly stack events. |
| 58 | // So we need to mark only duration that the span was active on the thread. |
| 59 | // (Hopefully any off-thread activity will be connected by a flow event). |
| 60 | // Record the end time here, but don't write the event: Args aren't ready yet. |
| 61 | void endSpan() override { |
| 62 | Context::current().getExisting(SpanKey)->markEnded(); |
Ilya Biryukov | 12cdb4f | 2017-12-14 15:33:38 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame^] | 65 | void instant(StringRef Name, json::Object &&Args) override { |
Sam McCall | 7929d00 | 2018-02-15 08:40:54 +0000 | [diff] [blame] | 66 | captureThreadMetadata(); |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 67 | jsonEvent("i", json::Object{{"name", Name}, {"args", std::move(Args)}}); |
Ilya Biryukov | 12cdb4f | 2017-12-14 15:33:38 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 70 | // Record an event on the current thread. ph, pid, tid, ts are set. |
| 71 | // Contents must be a list of the other JSON key/values. |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 72 | void jsonEvent(StringRef Phase, json::Object &&Contents, |
| 73 | uint64_t TID = get_threadid(), double Timestamp = 0) { |
Sam McCall | 7929d00 | 2018-02-15 08:40:54 +0000 | [diff] [blame] | 74 | Contents["ts"] = Timestamp ? Timestamp : timestamp(); |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 75 | Contents["tid"] = int64_t(TID); |
Sam McCall | 7929d00 | 2018-02-15 08:40:54 +0000 | [diff] [blame] | 76 | std::lock_guard<std::mutex> Lock(Mu); |
| 77 | rawEvent(Phase, std::move(Contents)); |
| 78 | } |
| 79 | |
| 80 | private: |
| 81 | class JSONSpan { |
| 82 | public: |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame^] | 83 | JSONSpan(JSONTracer *Tracer, StringRef Name, json::Object *Args) |
Sam McCall | 7929d00 | 2018-02-15 08:40:54 +0000 | [diff] [blame] | 84 | : StartTime(Tracer->timestamp()), EndTime(0), Name(Name), |
| 85 | TID(get_threadid()), Tracer(Tracer), Args(Args) { |
| 86 | // ~JSONSpan() may run in a different thread, so we need to capture now. |
| 87 | Tracer->captureThreadMetadata(); |
| 88 | |
| 89 | // We don't record begin events here (and end events in the destructor) |
| 90 | // because B/E pairs have to appear in the right order, which is awkward. |
| 91 | // Instead we send the complete (X) event in the destructor. |
| 92 | |
| 93 | // If our parent was on a different thread, add an arrow to this span. |
| 94 | auto *Parent = Context::current().get(SpanKey); |
| 95 | if (Parent && *Parent && (*Parent)->TID != TID) { |
| 96 | // If the parent span ended already, then show this as "following" it. |
| 97 | // Otherwise show us as "parallel". |
| 98 | double OriginTime = (*Parent)->EndTime; |
| 99 | if (!OriginTime) |
| 100 | OriginTime = (*Parent)->StartTime; |
| 101 | |
| 102 | auto FlowID = nextID(); |
| 103 | Tracer->jsonEvent("s", |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 104 | json::Object{{"id", FlowID}, |
| 105 | {"name", "Context crosses threads"}, |
| 106 | {"cat", "dummy"}}, |
Sam McCall | 7929d00 | 2018-02-15 08:40:54 +0000 | [diff] [blame] | 107 | (*Parent)->TID, (*Parent)->StartTime); |
| 108 | Tracer->jsonEvent("f", |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 109 | json::Object{{"id", FlowID}, |
| 110 | {"bp", "e"}, |
| 111 | {"name", "Context crosses threads"}, |
| 112 | {"cat", "dummy"}}, |
Sam McCall | 7929d00 | 2018-02-15 08:40:54 +0000 | [diff] [blame] | 113 | TID); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | ~JSONSpan() { |
| 118 | // Finally, record the event (ending at EndTime, not timestamp())! |
| 119 | Tracer->jsonEvent("X", |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 120 | json::Object{{"name", std::move(Name)}, |
| 121 | {"args", std::move(*Args)}, |
| 122 | {"dur", EndTime - StartTime}}, |
Sam McCall | 7929d00 | 2018-02-15 08:40:54 +0000 | [diff] [blame] | 123 | TID, StartTime); |
| 124 | } |
| 125 | |
| 126 | // May be called by any thread. |
| 127 | void markEnded() { |
| 128 | EndTime = Tracer->timestamp(); |
| 129 | } |
| 130 | |
| 131 | private: |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 132 | static int64_t nextID() { |
| 133 | static std::atomic<int64_t> Next = {0}; |
Sam McCall | 7929d00 | 2018-02-15 08:40:54 +0000 | [diff] [blame] | 134 | return Next++; |
| 135 | } |
| 136 | |
| 137 | double StartTime; |
| 138 | std::atomic<double> EndTime; // Filled in by markEnded(). |
| 139 | std::string Name; |
| 140 | uint64_t TID; |
| 141 | JSONTracer *Tracer; |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 142 | json::Object *Args; |
Sam McCall | 7929d00 | 2018-02-15 08:40:54 +0000 | [diff] [blame] | 143 | }; |
| 144 | static Key<std::unique_ptr<JSONSpan>> SpanKey; |
| 145 | |
| 146 | // Record an event. ph and pid are set. |
| 147 | // Contents must be a list of the other JSON key/values. |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 148 | void rawEvent(StringRef Phase, json::Object &&Event) /*REQUIRES(Mu)*/ { |
Sam McCall | 7929d00 | 2018-02-15 08:40:54 +0000 | [diff] [blame] | 149 | // PID 0 represents the clangd process. |
| 150 | Event["pid"] = 0; |
| 151 | Event["ph"] = Phase; |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 152 | Out << Sep << formatv(JSONFormat, json::Value(std::move(Event))); |
Sam McCall | 7929d00 | 2018-02-15 08:40:54 +0000 | [diff] [blame] | 153 | Sep = ",\n"; |
| 154 | } |
| 155 | |
| 156 | // If we haven't already, emit metadata describing this thread. |
| 157 | void captureThreadMetadata() { |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 158 | uint64_t TID = get_threadid(); |
| 159 | std::lock_guard<std::mutex> Lock(Mu); |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 160 | if (ThreadsWithMD.insert(TID).second) { |
| 161 | SmallString<32> Name; |
| 162 | get_thread_name(Name); |
| 163 | if (!Name.empty()) { |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 164 | rawEvent("M", json::Object{ |
| 165 | {"tid", int64_t(TID)}, |
Sam McCall | 9cfd9c9 | 2017-11-23 17:12:04 +0000 | [diff] [blame] | 166 | {"name", "thread_name"}, |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 167 | {"args", json::Object{{"name", Name}}}, |
Sam McCall | 9cfd9c9 | 2017-11-23 17:12:04 +0000 | [diff] [blame] | 168 | }); |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 169 | } |
| 170 | } |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | double timestamp() { |
| 174 | using namespace std::chrono; |
Sam McCall | 3d9e024 | 2017-11-15 17:53:46 +0000 | [diff] [blame] | 175 | return duration<double, std::micro>(system_clock::now() - Start).count(); |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | std::mutex Mu; |
| 179 | raw_ostream &Out /*GUARDED_BY(Mu)*/; |
| 180 | const char *Sep /*GUARDED_BY(Mu)*/; |
| 181 | DenseSet<uint64_t> ThreadsWithMD /*GUARDED_BY(Mu)*/; |
| 182 | const sys::TimePoint<> Start; |
Sam McCall | 9cfd9c9 | 2017-11-23 17:12:04 +0000 | [diff] [blame] | 183 | const char *JSONFormat; |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 184 | }; |
| 185 | |
Sam McCall | 7929d00 | 2018-02-15 08:40:54 +0000 | [diff] [blame] | 186 | Key<std::unique_ptr<JSONTracer::JSONSpan>> JSONTracer::SpanKey; |
| 187 | |
Ilya Biryukov | ee27d2e | 2017-12-14 15:04:59 +0000 | [diff] [blame] | 188 | EventTracer *T = nullptr; |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 189 | } // namespace |
| 190 | |
Ilya Biryukov | ee27d2e | 2017-12-14 15:04:59 +0000 | [diff] [blame] | 191 | Session::Session(EventTracer &Tracer) { |
| 192 | assert(!T && "Resetting global tracer is not allowed."); |
| 193 | T = &Tracer; |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Ilya Biryukov | ee27d2e | 2017-12-14 15:04:59 +0000 | [diff] [blame] | 196 | Session::~Session() { T = nullptr; } |
| 197 | |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame^] | 198 | std::unique_ptr<EventTracer> createJSONTracer(raw_ostream &OS, bool Pretty) { |
Ilya Biryukov | ee27d2e | 2017-12-14 15:04:59 +0000 | [diff] [blame] | 199 | return llvm::make_unique<JSONTracer>(OS, Pretty); |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 202 | void log(const Twine &Message) { |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 203 | if (!T) |
| 204 | return; |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 205 | T->instant("Log", json::Object{{"Message", Message.str()}}); |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | // Returned context owns Args. |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame^] | 209 | static Context makeSpanContext(Twine Name, json::Object *Args) { |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 210 | if (!T) |
| 211 | return Context::current().clone(); |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 212 | WithContextValue WithArgs{std::unique_ptr<json::Object>(Args)}; |
Sam McCall | c901c5d | 2018-02-19 09:56:28 +0000 | [diff] [blame] | 213 | return T->beginSpan(Name.isSingleStringRef() ? Name.getSingleStringRef() |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame^] | 214 | : StringRef(Name.str()), |
Sam McCall | c901c5d | 2018-02-19 09:56:28 +0000 | [diff] [blame] | 215 | Args); |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Sam McCall | 1b475a1 | 2018-01-26 09:00:30 +0000 | [diff] [blame] | 218 | // Span keeps a non-owning pointer to the args, which is how users access them. |
| 219 | // The args are owned by the context though. They stick around until the |
| 220 | // beginSpan() context is destroyed, when the tracing engine will consume them. |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame^] | 221 | Span::Span(Twine Name) |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 222 | : Args(T ? new json::Object() : nullptr), |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 223 | RestoreCtx(makeSpanContext(Name, Args)) {} |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 224 | |
Sam McCall | 7929d00 | 2018-02-15 08:40:54 +0000 | [diff] [blame] | 225 | Span::~Span() { |
| 226 | if (T) |
| 227 | T->endSpan(); |
| 228 | } |
| 229 | |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 230 | } // namespace trace |
| 231 | } // namespace clangd |
| 232 | } // namespace clang |