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 | |
| 22 | namespace clang { |
| 23 | namespace clangd { |
| 24 | namespace trace { |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 25 | |
| 26 | namespace { |
| 27 | // The current implementation is naive: each thread writes to Out guarded by Mu. |
| 28 | // Perhaps we should replace this by something that disturbs performance less. |
Ilya Biryukov | ee27d2e | 2017-12-14 15:04:59 +0000 | [diff] [blame] | 29 | class JSONTracer : public EventTracer { |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 30 | public: |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 31 | JSONTracer(llvm::raw_ostream &Out, bool Pretty) |
Sam McCall | 9cfd9c9 | 2017-11-23 17:12:04 +0000 | [diff] [blame] | 32 | : Out(Out), Sep(""), Start(std::chrono::system_clock::now()), |
| 33 | JSONFormat(Pretty ? "{0:2}" : "{0}") { |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 34 | // The displayTimeUnit must be ns to avoid low-precision overlap |
| 35 | // calculations! |
| 36 | Out << R"({"displayTimeUnit":"ns","traceEvents":[)" |
| 37 | << "\n"; |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 38 | rawEvent("M", llvm::json::Object{ |
Sam McCall | 9cfd9c9 | 2017-11-23 17:12:04 +0000 | [diff] [blame] | 39 | {"name", "process_name"}, |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 40 | {"args", llvm::json::Object{{"name", "clangd"}}}, |
Sam McCall | 9cfd9c9 | 2017-11-23 17:12:04 +0000 | [diff] [blame] | 41 | }); |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Ilya Biryukov | ee27d2e | 2017-12-14 15:04:59 +0000 | [diff] [blame] | 44 | ~JSONTracer() { |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 45 | Out << "\n]}"; |
| 46 | Out.flush(); |
| 47 | } |
| 48 | |
Sam McCall | 7929d00 | 2018-02-15 08:40:54 +0000 | [diff] [blame] | 49 | // We stash a Span object in the context. It will record the start/end, |
| 50 | // and this also allows us to look up the parent Span's information. |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 51 | Context beginSpan(llvm::StringRef Name, llvm::json::Object *Args) override { |
Sam McCall | 892ea68 | 2018-02-15 14:16:17 +0000 | [diff] [blame] | 52 | return Context::current().derive( |
| 53 | SpanKey, llvm::make_unique<JSONSpan>(this, Name, Args)); |
Sam McCall | 7929d00 | 2018-02-15 08:40:54 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | // Trace viewer requires each thread to properly stack events. |
| 57 | // So we need to mark only duration that the span was active on the thread. |
| 58 | // (Hopefully any off-thread activity will be connected by a flow event). |
| 59 | // Record the end time here, but don't write the event: Args aren't ready yet. |
| 60 | void endSpan() override { |
| 61 | Context::current().getExisting(SpanKey)->markEnded(); |
Ilya Biryukov | 12cdb4f | 2017-12-14 15:33:38 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 64 | void instant(llvm::StringRef Name, llvm::json::Object &&Args) override { |
Sam McCall | 7929d00 | 2018-02-15 08:40:54 +0000 | [diff] [blame] | 65 | captureThreadMetadata(); |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 66 | jsonEvent("i", |
| 67 | llvm::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. |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 72 | void jsonEvent(llvm::StringRef Phase, llvm::json::Object &&Contents, |
| 73 | uint64_t TID = llvm::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: |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 83 | JSONSpan(JSONTracer *Tracer, llvm::StringRef Name, llvm::json::Object *Args) |
Sam McCall | 7929d00 | 2018-02-15 08:40:54 +0000 | [diff] [blame] | 84 | : StartTime(Tracer->timestamp()), EndTime(0), Name(Name), |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 85 | TID(llvm::get_threadid()), Tracer(Tracer), Args(Args) { |
Sam McCall | 7929d00 | 2018-02-15 08:40:54 +0000 | [diff] [blame] | 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(); |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 103 | Tracer->jsonEvent( |
| 104 | "s", |
| 105 | llvm::json::Object{{"id", FlowID}, |
| 106 | {"name", "Context crosses threads"}, |
| 107 | {"cat", "dummy"}}, |
| 108 | (*Parent)->TID, (*Parent)->StartTime); |
| 109 | Tracer->jsonEvent( |
| 110 | "f", |
| 111 | llvm::json::Object{{"id", FlowID}, |
| 112 | {"bp", "e"}, |
| 113 | {"name", "Context crosses threads"}, |
| 114 | {"cat", "dummy"}}, |
| 115 | TID); |
Sam McCall | 7929d00 | 2018-02-15 08:40:54 +0000 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | |
| 119 | ~JSONSpan() { |
| 120 | // Finally, record the event (ending at EndTime, not timestamp())! |
| 121 | Tracer->jsonEvent("X", |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 122 | llvm::json::Object{{"name", std::move(Name)}, |
| 123 | {"args", std::move(*Args)}, |
| 124 | {"dur", EndTime - StartTime}}, |
Sam McCall | 7929d00 | 2018-02-15 08:40:54 +0000 | [diff] [blame] | 125 | TID, StartTime); |
| 126 | } |
| 127 | |
| 128 | // May be called by any thread. |
Ilya Biryukov | 22fa465 | 2019-01-03 13:28:05 +0000 | [diff] [blame] | 129 | void markEnded() { EndTime = Tracer->timestamp(); } |
Sam McCall | 7929d00 | 2018-02-15 08:40:54 +0000 | [diff] [blame] | 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; |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 142 | llvm::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. |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 148 | void rawEvent(llvm::StringRef Phase, |
| 149 | llvm::json::Object &&Event) /*REQUIRES(Mu)*/ { |
Sam McCall | 7929d00 | 2018-02-15 08:40:54 +0000 | [diff] [blame] | 150 | // PID 0 represents the clangd process. |
| 151 | Event["pid"] = 0; |
| 152 | Event["ph"] = Phase; |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 153 | Out << Sep |
| 154 | << llvm::formatv(JSONFormat, llvm::json::Value(std::move(Event))); |
Sam McCall | 7929d00 | 2018-02-15 08:40:54 +0000 | [diff] [blame] | 155 | Sep = ",\n"; |
| 156 | } |
| 157 | |
| 158 | // If we haven't already, emit metadata describing this thread. |
| 159 | void captureThreadMetadata() { |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 160 | uint64_t TID = llvm::get_threadid(); |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 161 | std::lock_guard<std::mutex> Lock(Mu); |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 162 | if (ThreadsWithMD.insert(TID).second) { |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 163 | llvm::SmallString<32> Name; |
| 164 | llvm::get_thread_name(Name); |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 165 | if (!Name.empty()) { |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 166 | rawEvent("M", llvm::json::Object{ |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 167 | {"tid", int64_t(TID)}, |
Sam McCall | 9cfd9c9 | 2017-11-23 17:12:04 +0000 | [diff] [blame] | 168 | {"name", "thread_name"}, |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 169 | {"args", llvm::json::Object{{"name", Name}}}, |
Sam McCall | 9cfd9c9 | 2017-11-23 17:12:04 +0000 | [diff] [blame] | 170 | }); |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 171 | } |
| 172 | } |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | double timestamp() { |
| 176 | using namespace std::chrono; |
Sam McCall | 3d9e024 | 2017-11-15 17:53:46 +0000 | [diff] [blame] | 177 | return duration<double, std::micro>(system_clock::now() - Start).count(); |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | std::mutex Mu; |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 181 | llvm::raw_ostream &Out /*GUARDED_BY(Mu)*/; |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 182 | const char *Sep /*GUARDED_BY(Mu)*/; |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 183 | llvm::DenseSet<uint64_t> ThreadsWithMD /*GUARDED_BY(Mu)*/; |
| 184 | const llvm::sys::TimePoint<> Start; |
Sam McCall | 9cfd9c9 | 2017-11-23 17:12:04 +0000 | [diff] [blame] | 185 | const char *JSONFormat; |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 186 | }; |
| 187 | |
Sam McCall | 7929d00 | 2018-02-15 08:40:54 +0000 | [diff] [blame] | 188 | Key<std::unique_ptr<JSONTracer::JSONSpan>> JSONTracer::SpanKey; |
| 189 | |
Ilya Biryukov | ee27d2e | 2017-12-14 15:04:59 +0000 | [diff] [blame] | 190 | EventTracer *T = nullptr; |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 191 | } // namespace |
| 192 | |
Ilya Biryukov | ee27d2e | 2017-12-14 15:04:59 +0000 | [diff] [blame] | 193 | Session::Session(EventTracer &Tracer) { |
| 194 | assert(!T && "Resetting global tracer is not allowed."); |
| 195 | T = &Tracer; |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Ilya Biryukov | ee27d2e | 2017-12-14 15:04:59 +0000 | [diff] [blame] | 198 | Session::~Session() { T = nullptr; } |
| 199 | |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 200 | std::unique_ptr<EventTracer> createJSONTracer(llvm::raw_ostream &OS, |
| 201 | bool Pretty) { |
Ilya Biryukov | ee27d2e | 2017-12-14 15:04:59 +0000 | [diff] [blame] | 202 | return llvm::make_unique<JSONTracer>(OS, Pretty); |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 205 | void log(const llvm::Twine &Message) { |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 206 | if (!T) |
| 207 | return; |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 208 | T->instant("Log", llvm::json::Object{{"Message", Message.str()}}); |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | // Returned context owns Args. |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 212 | static Context makeSpanContext(llvm::Twine Name, llvm::json::Object *Args) { |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 213 | if (!T) |
| 214 | return Context::current().clone(); |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 215 | WithContextValue WithArgs{std::unique_ptr<llvm::json::Object>(Args)}; |
Sam McCall | c901c5d | 2018-02-19 09:56:28 +0000 | [diff] [blame] | 216 | return T->beginSpan(Name.isSingleStringRef() ? Name.getSingleStringRef() |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 217 | : llvm::StringRef(Name.str()), |
Sam McCall | c901c5d | 2018-02-19 09:56:28 +0000 | [diff] [blame] | 218 | Args); |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Sam McCall | 1b475a1 | 2018-01-26 09:00:30 +0000 | [diff] [blame] | 221 | // Span keeps a non-owning pointer to the args, which is how users access them. |
| 222 | // The args are owned by the context though. They stick around until the |
| 223 | // beginSpan() context is destroyed, when the tracing engine will consume them. |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 224 | Span::Span(llvm::Twine Name) |
| 225 | : Args(T ? new llvm::json::Object() : nullptr), |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 226 | RestoreCtx(makeSpanContext(Name, Args)) {} |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 227 | |
Sam McCall | 7929d00 | 2018-02-15 08:40:54 +0000 | [diff] [blame] | 228 | Span::~Span() { |
| 229 | if (T) |
| 230 | T->endSpan(); |
| 231 | } |
| 232 | |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 233 | } // namespace trace |
| 234 | } // namespace clangd |
| 235 | } // namespace clang |