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" |
| 11 | |
| 12 | #include "llvm/ADT/DenseSet.h" |
| 13 | #include "llvm/Support/Chrono.h" |
| 14 | #include "llvm/Support/FormatProviders.h" |
| 15 | #include "llvm/Support/FormatVariadic.h" |
| 16 | #include "llvm/Support/Threading.h" |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 17 | #include <mutex> |
| 18 | |
| 19 | namespace clang { |
| 20 | namespace clangd { |
| 21 | namespace trace { |
| 22 | using namespace llvm; |
| 23 | |
| 24 | namespace { |
| 25 | // The current implementation is naive: each thread writes to Out guarded by Mu. |
| 26 | // Perhaps we should replace this by something that disturbs performance less. |
Ilya Biryukov | ee27d2e | 2017-12-14 15:04:59 +0000 | [diff] [blame^] | 27 | class JSONTracer : public EventTracer { |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 28 | public: |
Ilya Biryukov | ee27d2e | 2017-12-14 15:04:59 +0000 | [diff] [blame^] | 29 | JSONTracer(raw_ostream &Out, bool Pretty) |
Sam McCall | 9cfd9c9 | 2017-11-23 17:12:04 +0000 | [diff] [blame] | 30 | : Out(Out), Sep(""), Start(std::chrono::system_clock::now()), |
| 31 | JSONFormat(Pretty ? "{0:2}" : "{0}") { |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 32 | // The displayTimeUnit must be ns to avoid low-precision overlap |
| 33 | // calculations! |
| 34 | Out << R"({"displayTimeUnit":"ns","traceEvents":[)" |
| 35 | << "\n"; |
Sam McCall | 9cfd9c9 | 2017-11-23 17:12:04 +0000 | [diff] [blame] | 36 | rawEvent("M", json::obj{ |
| 37 | {"name", "process_name"}, |
| 38 | {"args", json::obj{{"name", "clangd"}}}, |
| 39 | }); |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Ilya Biryukov | ee27d2e | 2017-12-14 15:04:59 +0000 | [diff] [blame^] | 42 | ~JSONTracer() { |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 43 | Out << "\n]}"; |
| 44 | Out.flush(); |
| 45 | } |
| 46 | |
| 47 | // Record an event on the current thread. ph, pid, tid, ts are set. |
| 48 | // Contents must be a list of the other JSON key/values. |
Ilya Biryukov | ee27d2e | 2017-12-14 15:04:59 +0000 | [diff] [blame^] | 49 | void event(const Context &Ctx, StringRef Phase, |
| 50 | json::obj &&Contents) override { |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 51 | uint64_t TID = get_threadid(); |
| 52 | std::lock_guard<std::mutex> Lock(Mu); |
| 53 | // If we haven't already, emit metadata describing this thread. |
| 54 | if (ThreadsWithMD.insert(TID).second) { |
| 55 | SmallString<32> Name; |
| 56 | get_thread_name(Name); |
| 57 | if (!Name.empty()) { |
Sam McCall | 9cfd9c9 | 2017-11-23 17:12:04 +0000 | [diff] [blame] | 58 | rawEvent("M", json::obj{ |
| 59 | {"tid", TID}, |
| 60 | {"name", "thread_name"}, |
| 61 | {"args", json::obj{{"name", Name}}}, |
| 62 | }); |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 63 | } |
| 64 | } |
Sam McCall | 9cfd9c9 | 2017-11-23 17:12:04 +0000 | [diff] [blame] | 65 | Contents["ts"] = timestamp(); |
| 66 | Contents["tid"] = TID; |
| 67 | rawEvent(Phase, std::move(Contents)); |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | private: |
| 71 | // Record an event. ph and pid are set. |
| 72 | // Contents must be a list of the other JSON key/values. |
Sam McCall | 9cfd9c9 | 2017-11-23 17:12:04 +0000 | [diff] [blame] | 73 | void rawEvent(StringRef Phase, json::obj &&Event) /*REQUIRES(Mu)*/ { |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 74 | // PID 0 represents the clangd process. |
Sam McCall | 9cfd9c9 | 2017-11-23 17:12:04 +0000 | [diff] [blame] | 75 | Event["pid"] = 0; |
| 76 | Event["ph"] = Phase; |
| 77 | Out << Sep << formatv(JSONFormat, json::Expr(std::move(Event))); |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 78 | Sep = ",\n"; |
| 79 | } |
| 80 | |
| 81 | double timestamp() { |
| 82 | using namespace std::chrono; |
Sam McCall | 3d9e024 | 2017-11-15 17:53:46 +0000 | [diff] [blame] | 83 | return duration<double, std::micro>(system_clock::now() - Start).count(); |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | std::mutex Mu; |
| 87 | raw_ostream &Out /*GUARDED_BY(Mu)*/; |
| 88 | const char *Sep /*GUARDED_BY(Mu)*/; |
| 89 | DenseSet<uint64_t> ThreadsWithMD /*GUARDED_BY(Mu)*/; |
| 90 | const sys::TimePoint<> Start; |
Sam McCall | 9cfd9c9 | 2017-11-23 17:12:04 +0000 | [diff] [blame] | 91 | const char *JSONFormat; |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 92 | }; |
| 93 | |
Ilya Biryukov | ee27d2e | 2017-12-14 15:04:59 +0000 | [diff] [blame^] | 94 | EventTracer *T = nullptr; |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 95 | } // namespace |
| 96 | |
Ilya Biryukov | ee27d2e | 2017-12-14 15:04:59 +0000 | [diff] [blame^] | 97 | Session::Session(EventTracer &Tracer) { |
| 98 | assert(!T && "Resetting global tracer is not allowed."); |
| 99 | T = &Tracer; |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Ilya Biryukov | ee27d2e | 2017-12-14 15:04:59 +0000 | [diff] [blame^] | 102 | Session::~Session() { T = nullptr; } |
| 103 | |
| 104 | std::unique_ptr<EventTracer> createJSONTracer(llvm::raw_ostream &OS, |
| 105 | bool Pretty) { |
| 106 | return llvm::make_unique<JSONTracer>(OS, Pretty); |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Ilya Biryukov | ee27d2e | 2017-12-14 15:04:59 +0000 | [diff] [blame^] | 109 | void log(const Context &Ctx, const Twine &Message) { |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 110 | if (!T) |
| 111 | return; |
Ilya Biryukov | ee27d2e | 2017-12-14 15:04:59 +0000 | [diff] [blame^] | 112 | T->event(Ctx, "i", |
| 113 | json::obj{ |
| 114 | {"name", "Log"}, |
| 115 | {"args", json::obj{{"Message", Message.str()}}}, |
| 116 | }); |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Ilya Biryukov | ee27d2e | 2017-12-14 15:04:59 +0000 | [diff] [blame^] | 119 | Span::Span(const Context &Ctx, std::string Name) { |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 120 | if (!T) |
| 121 | return; |
Ilya Biryukov | ee27d2e | 2017-12-14 15:04:59 +0000 | [diff] [blame^] | 122 | // Clone the context, so that the original Context can be moved. |
| 123 | this->Ctx.emplace(Ctx.clone()); |
| 124 | |
| 125 | T->event(*this->Ctx, "B", json::obj{{"name", std::move(Name)}}); |
Sam McCall | 9cfd9c9 | 2017-11-23 17:12:04 +0000 | [diff] [blame] | 126 | Args = llvm::make_unique<json::obj>(); |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | Span::~Span() { |
| 130 | if (!T) |
| 131 | return; |
Sam McCall | 9cfd9c9 | 2017-11-23 17:12:04 +0000 | [diff] [blame] | 132 | if (!Args) |
| 133 | Args = llvm::make_unique<json::obj>(); |
Ilya Biryukov | ee27d2e | 2017-12-14 15:04:59 +0000 | [diff] [blame^] | 134 | T->event(*Ctx, "E", |
| 135 | Args ? json::obj{{"args", std::move(*Args)}} : json::obj{}); |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | } // namespace trace |
| 139 | } // namespace clangd |
| 140 | } // namespace clang |