2017-11-02 17:21:51 +08:00
|
|
|
//===--- Trace.cpp - Performance tracing facilities -----------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2017-11-02 17:21:51 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
[clangd] Move non-clang base pieces into separate support/ lib. NFCI
Summary:
This enforces layering, reduces a sprawling clangd/ directory, and makes life
easier for embedders.
Reviewers: kbobyrev
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D79014
2020-04-28 23:49:17 +08:00
|
|
|
#include "support/Trace.h"
|
|
|
|
#include "support/Context.h"
|
2017-11-02 17:21:51 +08:00
|
|
|
#include "llvm/ADT/DenseSet.h"
|
2020-04-17 05:12:09 +08:00
|
|
|
#include "llvm/ADT/Optional.h"
|
2018-01-26 17:00:30 +08:00
|
|
|
#include "llvm/ADT/ScopeExit.h"
|
2020-04-17 05:12:09 +08:00
|
|
|
#include "llvm/ADT/StringRef.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"
|
2018-02-15 16:40:54 +08:00
|
|
|
#include <atomic>
|
2020-04-17 05:12:09 +08:00
|
|
|
#include <chrono>
|
|
|
|
#include <memory>
|
2017-11-02 17:21:51 +08:00
|
|
|
#include <mutex>
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace clangd {
|
|
|
|
namespace trace {
|
|
|
|
|
|
|
|
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:
|
2019-04-26 00:37:07 +08:00
|
|
|
JSONTracer(llvm::raw_ostream &OS, bool Pretty)
|
|
|
|
: Out(OS, Pretty ? 2 : 0), Start(std::chrono::system_clock::now()) {
|
2017-11-02 17:21:51 +08:00
|
|
|
// The displayTimeUnit must be ns to avoid low-precision overlap
|
|
|
|
// calculations!
|
2019-04-26 00:37:07 +08:00
|
|
|
Out.objectBegin();
|
|
|
|
Out.attribute("displayTimeUnit", "ns");
|
|
|
|
Out.attributeBegin("traceEvents");
|
|
|
|
Out.arrayBegin();
|
2019-01-07 23:45:19 +08:00
|
|
|
rawEvent("M", llvm::json::Object{
|
2017-11-24 01:12:04 +08:00
|
|
|
{"name", "process_name"},
|
2019-01-07 23:45:19 +08:00
|
|
|
{"args", llvm::json::Object{{"name", "clangd"}}},
|
2017-11-24 01:12:04 +08:00
|
|
|
});
|
2017-11-02 17:21:51 +08:00
|
|
|
}
|
|
|
|
|
2017-12-14 23:04:59 +08:00
|
|
|
~JSONTracer() {
|
2019-04-26 00:37:07 +08:00
|
|
|
Out.arrayEnd();
|
|
|
|
Out.attributeEnd();
|
|
|
|
Out.objectEnd();
|
2017-11-02 17:21:51 +08:00
|
|
|
Out.flush();
|
|
|
|
}
|
|
|
|
|
2018-02-15 16:40:54 +08:00
|
|
|
// We stash a Span object in the context. It will record the start/end,
|
|
|
|
// and this also allows us to look up the parent Span's information.
|
2019-01-07 23:45:19 +08:00
|
|
|
Context beginSpan(llvm::StringRef Name, llvm::json::Object *Args) override {
|
2018-02-15 22:16:17 +08:00
|
|
|
return Context::current().derive(
|
2019-08-15 07:52:23 +08:00
|
|
|
SpanKey, std::make_unique<JSONSpan>(this, Name, Args));
|
2018-02-15 16:40:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Trace viewer requires each thread to properly stack events.
|
|
|
|
// So we need to mark only duration that the span was active on the thread.
|
|
|
|
// (Hopefully any off-thread activity will be connected by a flow event).
|
|
|
|
// Record the end time here, but don't write the event: Args aren't ready yet.
|
|
|
|
void endSpan() override {
|
|
|
|
Context::current().getExisting(SpanKey)->markEnded();
|
2017-12-14 23:33:38 +08:00
|
|
|
}
|
|
|
|
|
2019-01-07 23:45:19 +08:00
|
|
|
void instant(llvm::StringRef Name, llvm::json::Object &&Args) override {
|
2018-02-15 16:40:54 +08:00
|
|
|
captureThreadMetadata();
|
2019-01-07 23:45:19 +08:00
|
|
|
jsonEvent("i",
|
|
|
|
llvm::json::Object{{"name", Name}, {"args", std::move(Args)}});
|
2017-12-14 23:33:38 +08:00
|
|
|
}
|
|
|
|
|
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.
|
2019-01-07 23:45:19 +08:00
|
|
|
void jsonEvent(llvm::StringRef Phase, llvm::json::Object &&Contents,
|
|
|
|
uint64_t TID = llvm::get_threadid(), double Timestamp = 0) {
|
2018-02-15 16:40:54 +08:00
|
|
|
Contents["ts"] = Timestamp ? Timestamp : timestamp();
|
2018-07-09 22:25:59 +08:00
|
|
|
Contents["tid"] = int64_t(TID);
|
2018-02-15 16:40:54 +08:00
|
|
|
std::lock_guard<std::mutex> Lock(Mu);
|
2019-04-26 00:37:07 +08:00
|
|
|
rawEvent(Phase, Contents);
|
2017-11-02 17:21:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-02-15 16:40:54 +08:00
|
|
|
class JSONSpan {
|
|
|
|
public:
|
2019-01-07 23:45:19 +08:00
|
|
|
JSONSpan(JSONTracer *Tracer, llvm::StringRef Name, llvm::json::Object *Args)
|
2018-02-15 16:40:54 +08:00
|
|
|
: StartTime(Tracer->timestamp()), EndTime(0), Name(Name),
|
2019-01-07 23:45:19 +08:00
|
|
|
TID(llvm::get_threadid()), Tracer(Tracer), Args(Args) {
|
2018-02-15 16:40:54 +08:00
|
|
|
// ~JSONSpan() may run in a different thread, so we need to capture now.
|
|
|
|
Tracer->captureThreadMetadata();
|
|
|
|
|
|
|
|
// We don't record begin events here (and end events in the destructor)
|
|
|
|
// because B/E pairs have to appear in the right order, which is awkward.
|
|
|
|
// Instead we send the complete (X) event in the destructor.
|
|
|
|
|
|
|
|
// If our parent was on a different thread, add an arrow to this span.
|
|
|
|
auto *Parent = Context::current().get(SpanKey);
|
|
|
|
if (Parent && *Parent && (*Parent)->TID != TID) {
|
|
|
|
// If the parent span ended already, then show this as "following" it.
|
|
|
|
// Otherwise show us as "parallel".
|
|
|
|
double OriginTime = (*Parent)->EndTime;
|
|
|
|
if (!OriginTime)
|
|
|
|
OriginTime = (*Parent)->StartTime;
|
|
|
|
|
|
|
|
auto FlowID = nextID();
|
2019-01-07 23:45:19 +08:00
|
|
|
Tracer->jsonEvent(
|
|
|
|
"s",
|
|
|
|
llvm::json::Object{{"id", FlowID},
|
|
|
|
{"name", "Context crosses threads"},
|
|
|
|
{"cat", "dummy"}},
|
|
|
|
(*Parent)->TID, (*Parent)->StartTime);
|
|
|
|
Tracer->jsonEvent(
|
|
|
|
"f",
|
|
|
|
llvm::json::Object{{"id", FlowID},
|
|
|
|
{"bp", "e"},
|
|
|
|
{"name", "Context crosses threads"},
|
|
|
|
{"cat", "dummy"}},
|
|
|
|
TID);
|
2018-02-15 16:40:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~JSONSpan() {
|
|
|
|
// Finally, record the event (ending at EndTime, not timestamp())!
|
|
|
|
Tracer->jsonEvent("X",
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::json::Object{{"name", std::move(Name)},
|
|
|
|
{"args", std::move(*Args)},
|
|
|
|
{"dur", EndTime - StartTime}},
|
2018-02-15 16:40:54 +08:00
|
|
|
TID, StartTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
// May be called by any thread.
|
2019-01-03 21:28:05 +08:00
|
|
|
void markEnded() { EndTime = Tracer->timestamp(); }
|
2018-02-15 16:40:54 +08:00
|
|
|
|
|
|
|
private:
|
2018-07-09 22:25:59 +08:00
|
|
|
static int64_t nextID() {
|
|
|
|
static std::atomic<int64_t> Next = {0};
|
2018-02-15 16:40:54 +08:00
|
|
|
return Next++;
|
|
|
|
}
|
|
|
|
|
|
|
|
double StartTime;
|
|
|
|
std::atomic<double> EndTime; // Filled in by markEnded().
|
|
|
|
std::string Name;
|
|
|
|
uint64_t TID;
|
|
|
|
JSONTracer *Tracer;
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::json::Object *Args;
|
2018-02-15 16:40:54 +08:00
|
|
|
};
|
|
|
|
static Key<std::unique_ptr<JSONSpan>> SpanKey;
|
|
|
|
|
2017-11-02 17:21:51 +08:00
|
|
|
// Record an event. ph and pid are set.
|
|
|
|
// Contents must be a list of the other JSON key/values.
|
2019-01-07 23:45:19 +08:00
|
|
|
void rawEvent(llvm::StringRef Phase,
|
2019-04-26 00:37:07 +08:00
|
|
|
const llvm::json::Object &Event) /*REQUIRES(Mu)*/ {
|
2017-11-02 17:21:51 +08:00
|
|
|
// PID 0 represents the clangd process.
|
[clangd] Move non-clang base pieces into separate support/ lib. NFCI
Summary:
This enforces layering, reduces a sprawling clangd/ directory, and makes life
easier for embedders.
Reviewers: kbobyrev
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D79014
2020-04-28 23:49:17 +08:00
|
|
|
Out.object([&] {
|
2019-04-26 00:37:07 +08:00
|
|
|
Out.attribute("pid", 0);
|
|
|
|
Out.attribute("ph", Phase);
|
[clangd] Move non-clang base pieces into separate support/ lib. NFCI
Summary:
This enforces layering, reduces a sprawling clangd/ directory, and makes life
easier for embedders.
Reviewers: kbobyrev
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D79014
2020-04-28 23:49:17 +08:00
|
|
|
for (const auto &KV : Event)
|
2019-04-26 00:37:07 +08:00
|
|
|
Out.attribute(KV.first, KV.second);
|
|
|
|
});
|
2017-11-02 17:21:51 +08:00
|
|
|
}
|
|
|
|
|
2018-02-15 16:40:54 +08:00
|
|
|
// If we haven't already, emit metadata describing this thread.
|
|
|
|
void captureThreadMetadata() {
|
2019-01-07 23:45:19 +08:00
|
|
|
uint64_t TID = llvm::get_threadid();
|
2018-02-15 16:40:54 +08:00
|
|
|
std::lock_guard<std::mutex> Lock(Mu);
|
|
|
|
if (ThreadsWithMD.insert(TID).second) {
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::SmallString<32> Name;
|
|
|
|
llvm::get_thread_name(Name);
|
2018-02-15 16:40:54 +08:00
|
|
|
if (!Name.empty()) {
|
2019-01-07 23:45:19 +08:00
|
|
|
rawEvent("M", llvm::json::Object{
|
2018-07-09 22:25:59 +08:00
|
|
|
{"tid", int64_t(TID)},
|
2018-02-15 16:40:54 +08:00
|
|
|
{"name", "thread_name"},
|
2019-01-07 23:45:19 +08:00
|
|
|
{"args", llvm::json::Object{{"name", Name}}},
|
2018-02-15 16:40:54 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-02 17:21:51 +08:00
|
|
|
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;
|
2019-04-26 00:37:07 +08:00
|
|
|
llvm::json::OStream Out /*GUARDED_BY(Mu)*/;
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::DenseSet<uint64_t> ThreadsWithMD /*GUARDED_BY(Mu)*/;
|
|
|
|
const llvm::sys::TimePoint<> Start;
|
2017-11-02 17:21:51 +08:00
|
|
|
};
|
|
|
|
|
2018-02-15 16:40:54 +08:00
|
|
|
Key<std::unique_ptr<JSONTracer::JSONSpan>> JSONTracer::SpanKey;
|
|
|
|
|
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; }
|
|
|
|
|
2019-01-07 23:45:19 +08:00
|
|
|
std::unique_ptr<EventTracer> createJSONTracer(llvm::raw_ostream &OS,
|
|
|
|
bool Pretty) {
|
2019-08-15 07:52:23 +08:00
|
|
|
return std::make_unique<JSONTracer>(OS, Pretty);
|
2017-11-02 17:21:51 +08:00
|
|
|
}
|
|
|
|
|
2019-01-07 23:45:19 +08:00
|
|
|
void log(const llvm::Twine &Message) {
|
2017-11-02 17:21:51 +08:00
|
|
|
if (!T)
|
|
|
|
return;
|
2019-01-07 23:45:19 +08:00
|
|
|
T->instant("Log", llvm::json::Object{{"Message", Message.str()}});
|
[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
|
|
|
}
|
|
|
|
|
|
|
|
// Returned context owns Args.
|
2020-04-17 05:12:09 +08:00
|
|
|
static Context makeSpanContext(llvm::Twine Name, llvm::json::Object *Args,
|
|
|
|
const Metric &LatencyMetric) {
|
[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
|
|
|
if (!T)
|
|
|
|
return Context::current().clone();
|
2019-01-07 23:45:19 +08:00
|
|
|
WithContextValue WithArgs{std::unique_ptr<llvm::json::Object>(Args)};
|
2020-04-17 05:12:09 +08:00
|
|
|
llvm::Optional<WithContextValue> WithLatency;
|
|
|
|
using Clock = std::chrono::high_resolution_clock;
|
|
|
|
WithLatency.emplace(llvm::make_scope_exit(
|
|
|
|
[StartTime = Clock::now(), Name = Name.str(), &LatencyMetric] {
|
|
|
|
LatencyMetric.record(
|
|
|
|
std::chrono::duration_cast<std::chrono::milliseconds>(Clock::now() -
|
|
|
|
StartTime)
|
|
|
|
.count(),
|
|
|
|
Name);
|
|
|
|
}));
|
2018-02-19 17:56:28 +08:00
|
|
|
return T->beginSpan(Name.isSingleStringRef() ? Name.getSingleStringRef()
|
2019-01-07 23:45:19 +08:00
|
|
|
: llvm::StringRef(Name.str()),
|
2018-02-19 17:56:28 +08:00
|
|
|
Args);
|
2017-11-02 17:21:51 +08:00
|
|
|
}
|
|
|
|
|
2020-04-17 05:12:09 +08:00
|
|
|
// Fallback metric that measures latencies for spans without an explicit latency
|
|
|
|
// metric. Labels are span names.
|
|
|
|
constexpr Metric SpanLatency("span_latency", Metric::Distribution, "span_name");
|
|
|
|
|
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.
|
2020-04-17 05:12:09 +08:00
|
|
|
Span::Span(llvm::Twine Name) : Span(Name, SpanLatency) {}
|
|
|
|
Span::Span(llvm::Twine Name, const Metric &LatencyMetric)
|
2019-01-07 23:45:19 +08:00
|
|
|
: Args(T ? new llvm::json::Object() : nullptr),
|
2020-04-17 05:12:09 +08:00
|
|
|
RestoreCtx(makeSpanContext(Name, Args, LatencyMetric)) {}
|
2017-11-02 17:21:51 +08:00
|
|
|
|
2018-02-15 16:40:54 +08:00
|
|
|
Span::~Span() {
|
|
|
|
if (T)
|
|
|
|
T->endSpan();
|
|
|
|
}
|
|
|
|
|
2020-04-17 05:12:09 +08:00
|
|
|
void Metric::record(double Value, llvm::StringRef Label) const {
|
|
|
|
if (!T)
|
|
|
|
return;
|
|
|
|
assert((LabelName.empty() == Label.empty()) &&
|
|
|
|
"recording a measurement with inconsistent labeling");
|
|
|
|
T->record(*this, Value, Label);
|
|
|
|
}
|
|
|
|
|
|
|
|
Context EventTracer::beginSpan(llvm::StringRef Name, llvm::json::Object *Args) {
|
|
|
|
return Context::current().clone();
|
|
|
|
}
|
2017-11-02 17:21:51 +08:00
|
|
|
} // namespace trace
|
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|