2017-11-02 17:21:51 +08:00
|
|
|
//===--- Trace.cpp - Performance tracing facilities -----------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Trace.h"
|
2018-01-26 17:00:30 +08:00
|
|
|
#include "Context.h"
|
|
|
|
#include "Function.h"
|
2017-11-02 17:21:51 +08:00
|
|
|
#include "llvm/ADT/DenseSet.h"
|
2018-01-26 17:00:30 +08:00
|
|
|
#include "llvm/ADT/ScopeExit.h"
|
2017-11-02 17:21:51 +08:00
|
|
|
#include "llvm/Support/Chrono.h"
|
|
|
|
#include "llvm/Support/FormatProviders.h"
|
|
|
|
#include "llvm/Support/FormatVariadic.h"
|
|
|
|
#include "llvm/Support/Threading.h"
|
|
|
|
#include <mutex>
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace clangd {
|
|
|
|
namespace trace {
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
// The current implementation is naive: each thread writes to Out guarded by Mu.
|
|
|
|
// Perhaps we should replace this by something that disturbs performance less.
|
2017-12-14 23:04:59 +08:00
|
|
|
class JSONTracer : public EventTracer {
|
2017-11-02 17:21:51 +08:00
|
|
|
public:
|
2017-12-14 23:04:59 +08:00
|
|
|
JSONTracer(raw_ostream &Out, bool Pretty)
|
2017-11-24 01:12:04 +08:00
|
|
|
: Out(Out), Sep(""), Start(std::chrono::system_clock::now()),
|
|
|
|
JSONFormat(Pretty ? "{0:2}" : "{0}") {
|
2017-11-02 17:21:51 +08:00
|
|
|
// The displayTimeUnit must be ns to avoid low-precision overlap
|
|
|
|
// calculations!
|
|
|
|
Out << R"({"displayTimeUnit":"ns","traceEvents":[)"
|
|
|
|
<< "\n";
|
2017-11-24 01:12:04 +08:00
|
|
|
rawEvent("M", json::obj{
|
|
|
|
{"name", "process_name"},
|
|
|
|
{"args", json::obj{{"name", "clangd"}}},
|
|
|
|
});
|
2017-11-02 17:21:51 +08:00
|
|
|
}
|
|
|
|
|
2017-12-14 23:04:59 +08:00
|
|
|
~JSONTracer() {
|
2017-11-02 17:21:51 +08:00
|
|
|
Out << "\n]}";
|
|
|
|
Out.flush();
|
|
|
|
}
|
|
|
|
|
[clangd] Pass Context implicitly using TLS.
Summary:
Instead of passing Context explicitly around, we now have a thread-local
Context object `Context::current()` which is an implicit argument to
every function.
Most manipulation of this should use the WithContextValue helper, which
augments the current Context to add a single KV pair, and restores the
old context on destruction.
Advantages are:
- less boilerplate in functions that just propagate contexts
- reading most code doesn't require understanding context at all, and
using context as values in fewer places still
- fewer options to pass the "wrong" context when it changes within a
scope (e.g. when using Span)
- contexts pass through interfaces we can't modify, such as VFS
- propagating contexts across threads was slightly tricky (e.g.
copy vs move, no move-init in lambdas), and is now encapsulated in
the threadpool
Disadvantages are all the usual TLS stuff - hidden magic, and
potential for higher memory usage on threads that don't use the
context. (In practice, it's just one pointer)
Reviewers: ilya-biryukov
Subscribers: klimek, jkorous-apple, ioeric, cfe-commits
Differential Revision: https://reviews.llvm.org/D42517
llvm-svn: 323872
2018-01-31 21:40:48 +08:00
|
|
|
Context beginSpan(llvm::StringRef Name, json::obj *Args) override {
|
2017-12-14 23:33:38 +08:00
|
|
|
jsonEvent("B", json::obj{{"name", Name}});
|
[clangd] Pass Context implicitly using TLS.
Summary:
Instead of passing Context explicitly around, we now have a thread-local
Context object `Context::current()` which is an implicit argument to
every function.
Most manipulation of this should use the WithContextValue helper, which
augments the current Context to add a single KV pair, and restores the
old context on destruction.
Advantages are:
- less boilerplate in functions that just propagate contexts
- reading most code doesn't require understanding context at all, and
using context as values in fewer places still
- fewer options to pass the "wrong" context when it changes within a
scope (e.g. when using Span)
- contexts pass through interfaces we can't modify, such as VFS
- propagating contexts across threads was slightly tricky (e.g.
copy vs move, no move-init in lambdas), and is now encapsulated in
the threadpool
Disadvantages are all the usual TLS stuff - hidden magic, and
potential for higher memory usage on threads that don't use the
context. (In practice, it's just one pointer)
Reviewers: ilya-biryukov
Subscribers: klimek, jkorous-apple, ioeric, cfe-commits
Differential Revision: https://reviews.llvm.org/D42517
llvm-svn: 323872
2018-01-31 21:40:48 +08:00
|
|
|
return Context::current().derive(make_scope_exit([this, Args] {
|
2018-01-26 17:00:30 +08:00
|
|
|
jsonEvent("E", json::obj{{"args", std::move(*Args)}});
|
|
|
|
}));
|
2017-12-14 23:33:38 +08:00
|
|
|
}
|
|
|
|
|
[clangd] Pass Context implicitly using TLS.
Summary:
Instead of passing Context explicitly around, we now have a thread-local
Context object `Context::current()` which is an implicit argument to
every function.
Most manipulation of this should use the WithContextValue helper, which
augments the current Context to add a single KV pair, and restores the
old context on destruction.
Advantages are:
- less boilerplate in functions that just propagate contexts
- reading most code doesn't require understanding context at all, and
using context as values in fewer places still
- fewer options to pass the "wrong" context when it changes within a
scope (e.g. when using Span)
- contexts pass through interfaces we can't modify, such as VFS
- propagating contexts across threads was slightly tricky (e.g.
copy vs move, no move-init in lambdas), and is now encapsulated in
the threadpool
Disadvantages are all the usual TLS stuff - hidden magic, and
potential for higher memory usage on threads that don't use the
context. (In practice, it's just one pointer)
Reviewers: ilya-biryukov
Subscribers: klimek, jkorous-apple, ioeric, cfe-commits
Differential Revision: https://reviews.llvm.org/D42517
llvm-svn: 323872
2018-01-31 21:40:48 +08:00
|
|
|
void instant(llvm::StringRef Name, json::obj &&Args) override {
|
2017-12-14 23:33:38 +08:00
|
|
|
jsonEvent("i", json::obj{{"name", Name}, {"args", std::move(Args)}});
|
|
|
|
}
|
|
|
|
|
2017-11-02 17:21:51 +08:00
|
|
|
// Record an event on the current thread. ph, pid, tid, ts are set.
|
|
|
|
// Contents must be a list of the other JSON key/values.
|
2017-12-14 23:33:38 +08:00
|
|
|
void jsonEvent(StringRef Phase, json::obj &&Contents) {
|
2017-11-02 17:21:51 +08:00
|
|
|
uint64_t TID = get_threadid();
|
|
|
|
std::lock_guard<std::mutex> Lock(Mu);
|
|
|
|
// If we haven't already, emit metadata describing this thread.
|
|
|
|
if (ThreadsWithMD.insert(TID).second) {
|
|
|
|
SmallString<32> Name;
|
|
|
|
get_thread_name(Name);
|
|
|
|
if (!Name.empty()) {
|
2017-11-24 01:12:04 +08:00
|
|
|
rawEvent("M", json::obj{
|
|
|
|
{"tid", TID},
|
|
|
|
{"name", "thread_name"},
|
|
|
|
{"args", json::obj{{"name", Name}}},
|
|
|
|
});
|
2017-11-02 17:21:51 +08:00
|
|
|
}
|
|
|
|
}
|
2017-11-24 01:12:04 +08:00
|
|
|
Contents["ts"] = timestamp();
|
|
|
|
Contents["tid"] = TID;
|
|
|
|
rawEvent(Phase, std::move(Contents));
|
2017-11-02 17:21:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Record an event. ph and pid are set.
|
|
|
|
// Contents must be a list of the other JSON key/values.
|
2017-11-24 01:12:04 +08:00
|
|
|
void rawEvent(StringRef Phase, json::obj &&Event) /*REQUIRES(Mu)*/ {
|
2017-11-02 17:21:51 +08:00
|
|
|
// PID 0 represents the clangd process.
|
2017-11-24 01:12:04 +08:00
|
|
|
Event["pid"] = 0;
|
|
|
|
Event["ph"] = Phase;
|
|
|
|
Out << Sep << formatv(JSONFormat, json::Expr(std::move(Event)));
|
2017-11-02 17:21:51 +08:00
|
|
|
Sep = ",\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
double timestamp() {
|
|
|
|
using namespace std::chrono;
|
2017-11-16 01:53:46 +08:00
|
|
|
return duration<double, std::micro>(system_clock::now() - Start).count();
|
2017-11-02 17:21:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::mutex Mu;
|
|
|
|
raw_ostream &Out /*GUARDED_BY(Mu)*/;
|
|
|
|
const char *Sep /*GUARDED_BY(Mu)*/;
|
|
|
|
DenseSet<uint64_t> ThreadsWithMD /*GUARDED_BY(Mu)*/;
|
|
|
|
const sys::TimePoint<> Start;
|
2017-11-24 01:12:04 +08:00
|
|
|
const char *JSONFormat;
|
2017-11-02 17:21:51 +08:00
|
|
|
};
|
|
|
|
|
2017-12-14 23:04:59 +08:00
|
|
|
EventTracer *T = nullptr;
|
2017-11-02 17:21:51 +08:00
|
|
|
} // namespace
|
|
|
|
|
2017-12-14 23:04:59 +08:00
|
|
|
Session::Session(EventTracer &Tracer) {
|
|
|
|
assert(!T && "Resetting global tracer is not allowed.");
|
|
|
|
T = &Tracer;
|
2017-11-02 17:21:51 +08:00
|
|
|
}
|
|
|
|
|
2017-12-14 23:04:59 +08:00
|
|
|
Session::~Session() { T = nullptr; }
|
|
|
|
|
|
|
|
std::unique_ptr<EventTracer> createJSONTracer(llvm::raw_ostream &OS,
|
|
|
|
bool Pretty) {
|
|
|
|
return llvm::make_unique<JSONTracer>(OS, Pretty);
|
2017-11-02 17:21:51 +08:00
|
|
|
}
|
|
|
|
|
[clangd] Pass Context implicitly using TLS.
Summary:
Instead of passing Context explicitly around, we now have a thread-local
Context object `Context::current()` which is an implicit argument to
every function.
Most manipulation of this should use the WithContextValue helper, which
augments the current Context to add a single KV pair, and restores the
old context on destruction.
Advantages are:
- less boilerplate in functions that just propagate contexts
- reading most code doesn't require understanding context at all, and
using context as values in fewer places still
- fewer options to pass the "wrong" context when it changes within a
scope (e.g. when using Span)
- contexts pass through interfaces we can't modify, such as VFS
- propagating contexts across threads was slightly tricky (e.g.
copy vs move, no move-init in lambdas), and is now encapsulated in
the threadpool
Disadvantages are all the usual TLS stuff - hidden magic, and
potential for higher memory usage on threads that don't use the
context. (In practice, it's just one pointer)
Reviewers: ilya-biryukov
Subscribers: klimek, jkorous-apple, ioeric, cfe-commits
Differential Revision: https://reviews.llvm.org/D42517
llvm-svn: 323872
2018-01-31 21:40:48 +08:00
|
|
|
void log(const Twine &Message) {
|
2017-11-02 17:21:51 +08:00
|
|
|
if (!T)
|
|
|
|
return;
|
[clangd] Pass Context implicitly using TLS.
Summary:
Instead of passing Context explicitly around, we now have a thread-local
Context object `Context::current()` which is an implicit argument to
every function.
Most manipulation of this should use the WithContextValue helper, which
augments the current Context to add a single KV pair, and restores the
old context on destruction.
Advantages are:
- less boilerplate in functions that just propagate contexts
- reading most code doesn't require understanding context at all, and
using context as values in fewer places still
- fewer options to pass the "wrong" context when it changes within a
scope (e.g. when using Span)
- contexts pass through interfaces we can't modify, such as VFS
- propagating contexts across threads was slightly tricky (e.g.
copy vs move, no move-init in lambdas), and is now encapsulated in
the threadpool
Disadvantages are all the usual TLS stuff - hidden magic, and
potential for higher memory usage on threads that don't use the
context. (In practice, it's just one pointer)
Reviewers: ilya-biryukov
Subscribers: klimek, jkorous-apple, ioeric, cfe-commits
Differential Revision: https://reviews.llvm.org/D42517
llvm-svn: 323872
2018-01-31 21:40:48 +08:00
|
|
|
T->instant("Log", json::obj{{"Message", Message.str()}});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returned context owns Args.
|
|
|
|
static Context makeSpanContext(llvm::StringRef Name, json::obj *Args) {
|
|
|
|
if (!T)
|
|
|
|
return Context::current().clone();
|
|
|
|
WithContextValue WithArgs{std::unique_ptr<json::obj>(Args)};
|
|
|
|
return T->beginSpan(Name, Args);
|
2017-11-02 17:21:51 +08:00
|
|
|
}
|
|
|
|
|
2018-01-26 17:00:30 +08:00
|
|
|
// Span keeps a non-owning pointer to the args, which is how users access them.
|
|
|
|
// The args are owned by the context though. They stick around until the
|
|
|
|
// beginSpan() context is destroyed, when the tracing engine will consume them.
|
[clangd] Pass Context implicitly using TLS.
Summary:
Instead of passing Context explicitly around, we now have a thread-local
Context object `Context::current()` which is an implicit argument to
every function.
Most manipulation of this should use the WithContextValue helper, which
augments the current Context to add a single KV pair, and restores the
old context on destruction.
Advantages are:
- less boilerplate in functions that just propagate contexts
- reading most code doesn't require understanding context at all, and
using context as values in fewer places still
- fewer options to pass the "wrong" context when it changes within a
scope (e.g. when using Span)
- contexts pass through interfaces we can't modify, such as VFS
- propagating contexts across threads was slightly tricky (e.g.
copy vs move, no move-init in lambdas), and is now encapsulated in
the threadpool
Disadvantages are all the usual TLS stuff - hidden magic, and
potential for higher memory usage on threads that don't use the
context. (In practice, it's just one pointer)
Reviewers: ilya-biryukov
Subscribers: klimek, jkorous-apple, ioeric, cfe-commits
Differential Revision: https://reviews.llvm.org/D42517
llvm-svn: 323872
2018-01-31 21:40:48 +08:00
|
|
|
Span::Span(llvm::StringRef Name)
|
2018-01-26 17:00:30 +08:00
|
|
|
: Args(T ? new json::obj() : nullptr),
|
[clangd] Pass Context implicitly using TLS.
Summary:
Instead of passing Context explicitly around, we now have a thread-local
Context object `Context::current()` which is an implicit argument to
every function.
Most manipulation of this should use the WithContextValue helper, which
augments the current Context to add a single KV pair, and restores the
old context on destruction.
Advantages are:
- less boilerplate in functions that just propagate contexts
- reading most code doesn't require understanding context at all, and
using context as values in fewer places still
- fewer options to pass the "wrong" context when it changes within a
scope (e.g. when using Span)
- contexts pass through interfaces we can't modify, such as VFS
- propagating contexts across threads was slightly tricky (e.g.
copy vs move, no move-init in lambdas), and is now encapsulated in
the threadpool
Disadvantages are all the usual TLS stuff - hidden magic, and
potential for higher memory usage on threads that don't use the
context. (In practice, it's just one pointer)
Reviewers: ilya-biryukov
Subscribers: klimek, jkorous-apple, ioeric, cfe-commits
Differential Revision: https://reviews.llvm.org/D42517
llvm-svn: 323872
2018-01-31 21:40:48 +08:00
|
|
|
RestoreCtx(makeSpanContext(Name, Args)) {}
|
2017-11-02 17:21:51 +08:00
|
|
|
|
|
|
|
} // namespace trace
|
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|