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. |
| 27 | class Tracer { |
| 28 | public: |
Sam McCall | 9cfd9c9 | 2017-11-23 17:12:04 +0000 | [diff] [blame] | 29 | Tracer(raw_ostream &Out, bool Pretty) |
| 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 | |
| 42 | ~Tracer() { |
| 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. |
Sam McCall | 9cfd9c9 | 2017-11-23 17:12:04 +0000 | [diff] [blame] | 49 | void event(StringRef Phase, json::obj &&Contents) { |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 50 | uint64_t TID = get_threadid(); |
| 51 | std::lock_guard<std::mutex> Lock(Mu); |
| 52 | // If we haven't already, emit metadata describing this thread. |
| 53 | if (ThreadsWithMD.insert(TID).second) { |
| 54 | SmallString<32> Name; |
| 55 | get_thread_name(Name); |
| 56 | if (!Name.empty()) { |
Sam McCall | 9cfd9c9 | 2017-11-23 17:12:04 +0000 | [diff] [blame] | 57 | rawEvent("M", json::obj{ |
| 58 | {"tid", TID}, |
| 59 | {"name", "thread_name"}, |
| 60 | {"args", json::obj{{"name", Name}}}, |
| 61 | }); |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 62 | } |
| 63 | } |
Sam McCall | 9cfd9c9 | 2017-11-23 17:12:04 +0000 | [diff] [blame] | 64 | Contents["ts"] = timestamp(); |
| 65 | Contents["tid"] = TID; |
| 66 | rawEvent(Phase, std::move(Contents)); |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | private: |
| 70 | // Record an event. ph and pid are set. |
| 71 | // Contents must be a list of the other JSON key/values. |
Sam McCall | 9cfd9c9 | 2017-11-23 17:12:04 +0000 | [diff] [blame] | 72 | void rawEvent(StringRef Phase, json::obj &&Event) /*REQUIRES(Mu)*/ { |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 73 | // PID 0 represents the clangd process. |
Sam McCall | 9cfd9c9 | 2017-11-23 17:12:04 +0000 | [diff] [blame] | 74 | Event["pid"] = 0; |
| 75 | Event["ph"] = Phase; |
| 76 | Out << Sep << formatv(JSONFormat, json::Expr(std::move(Event))); |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 77 | Sep = ",\n"; |
| 78 | } |
| 79 | |
| 80 | double timestamp() { |
| 81 | using namespace std::chrono; |
Sam McCall | 3d9e024 | 2017-11-15 17:53:46 +0000 | [diff] [blame] | 82 | return duration<double, std::micro>(system_clock::now() - Start).count(); |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | std::mutex Mu; |
| 86 | raw_ostream &Out /*GUARDED_BY(Mu)*/; |
| 87 | const char *Sep /*GUARDED_BY(Mu)*/; |
| 88 | DenseSet<uint64_t> ThreadsWithMD /*GUARDED_BY(Mu)*/; |
| 89 | const sys::TimePoint<> Start; |
Sam McCall | 9cfd9c9 | 2017-11-23 17:12:04 +0000 | [diff] [blame] | 90 | const char *JSONFormat; |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | static Tracer *T = nullptr; |
| 94 | } // namespace |
| 95 | |
Sam McCall | 9cfd9c9 | 2017-11-23 17:12:04 +0000 | [diff] [blame] | 96 | std::unique_ptr<Session> Session::create(raw_ostream &OS, bool Pretty) { |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 97 | assert(!T && "A session is already active"); |
Sam McCall | 9cfd9c9 | 2017-11-23 17:12:04 +0000 | [diff] [blame] | 98 | T = new Tracer(OS, Pretty); |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 99 | return std::unique_ptr<Session>(new Session()); |
| 100 | } |
| 101 | |
| 102 | Session::~Session() { |
| 103 | delete T; |
| 104 | T = nullptr; |
| 105 | } |
| 106 | |
| 107 | void log(const Twine &Message) { |
| 108 | if (!T) |
| 109 | return; |
Sam McCall | 9cfd9c9 | 2017-11-23 17:12:04 +0000 | [diff] [blame] | 110 | T->event("i", json::obj{ |
| 111 | {"name", "Log"}, |
| 112 | {"args", json::obj{{"Message", Message.str()}}}, |
| 113 | }); |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Sam McCall | 9cfd9c9 | 2017-11-23 17:12:04 +0000 | [diff] [blame] | 116 | Span::Span(std::string Name) { |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 117 | if (!T) |
| 118 | return; |
Sam McCall | 9cfd9c9 | 2017-11-23 17:12:04 +0000 | [diff] [blame] | 119 | T->event("B", json::obj{{"name", std::move(Name)}}); |
| 120 | Args = llvm::make_unique<json::obj>(); |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | Span::~Span() { |
| 124 | if (!T) |
| 125 | return; |
Sam McCall | 9cfd9c9 | 2017-11-23 17:12:04 +0000 | [diff] [blame] | 126 | if (!Args) |
| 127 | Args = llvm::make_unique<json::obj>(); |
| 128 | T->event("E", Args ? json::obj{{"args", std::move(*Args)}} : json::obj{}); |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | } // namespace trace |
| 132 | } // namespace clangd |
| 133 | } // namespace clang |