2018-02-08 15:37:35 +08:00
|
|
|
//===--- TUScheduler.cpp -----------------------------------------*-C++-*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// For each file, managed by TUScheduler, we create a single ASTWorker that
|
|
|
|
// manages an AST for that file. All operations that modify or read the AST are
|
|
|
|
// run on a separate dedicated thread asynchronously in FIFO order.
|
|
|
|
//
|
|
|
|
// We start processing each update immediately after we receive it. If two or
|
|
|
|
// more updates come subsequently without reads in-between, we attempt to drop
|
|
|
|
// an older one to not waste time building the ASTs we don't need.
|
|
|
|
//
|
|
|
|
// The processing thread of the ASTWorker is also responsible for building the
|
|
|
|
// preamble. However, unlike AST, the same preamble can be read concurrently, so
|
|
|
|
// we run each of async preamble reads on its own thread.
|
|
|
|
//
|
|
|
|
// To limit the concurrent load that clangd produces we mantain a semaphore that
|
|
|
|
// keeps more than a fixed number of threads from running concurrently.
|
|
|
|
//
|
|
|
|
// Rationale for cancelling updates.
|
|
|
|
// LSP clients can send updates to clangd on each keystroke. Some files take
|
|
|
|
// significant time to parse (e.g. a few seconds) and clangd can get starved by
|
|
|
|
// the updates to those files. Therefore we try to process only the last update,
|
|
|
|
// if possible.
|
|
|
|
// Our current strategy to do that is the following:
|
|
|
|
// - For each update we immediately schedule rebuild of the AST.
|
|
|
|
// - Rebuild of the AST checks if it was cancelled before doing any actual work.
|
|
|
|
// If it was, it does not do an actual rebuild, only reports llvm::None to the
|
|
|
|
// callback
|
|
|
|
// - When adding an update, we cancel the last update in the queue if it didn't
|
|
|
|
// have any reads.
|
|
|
|
// There is probably a optimal ways to do that. One approach we might take is
|
|
|
|
// the following:
|
|
|
|
// - For each update we remember the pending inputs, but delay rebuild of the
|
|
|
|
// AST for some timeout.
|
|
|
|
// - If subsequent updates come before rebuild was started, we replace the
|
|
|
|
// pending inputs and reset the timer.
|
|
|
|
// - If any reads of the AST are scheduled, we start building the AST
|
|
|
|
// immediately.
|
|
|
|
|
2018-01-31 16:51:16 +08:00
|
|
|
#include "TUScheduler.h"
|
2018-02-08 15:37:35 +08:00
|
|
|
#include "Logger.h"
|
2018-02-19 17:56:28 +08:00
|
|
|
#include "Trace.h"
|
2018-01-31 16:51:16 +08:00
|
|
|
#include "clang/Frontend/PCHContainerOperations.h"
|
|
|
|
#include "llvm/Support/Errc.h"
|
2018-02-19 17:56:28 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2018-02-08 15:37:35 +08:00
|
|
|
#include <memory>
|
|
|
|
#include <queue>
|
2018-02-09 18:17:23 +08:00
|
|
|
#include <thread>
|
2018-01-31 16:51:16 +08:00
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace clangd {
|
2018-03-02 16:56:37 +08:00
|
|
|
using std::chrono::steady_clock;
|
2018-02-08 15:37:35 +08:00
|
|
|
namespace {
|
|
|
|
class ASTWorkerHandle;
|
|
|
|
|
|
|
|
/// Owns one instance of the AST, schedules updates and reads of it.
|
|
|
|
/// Also responsible for building and providing access to the preamble.
|
|
|
|
/// Each ASTWorker processes the async requests sent to it on a separate
|
|
|
|
/// dedicated thread.
|
|
|
|
/// The ASTWorker that manages the AST is shared by both the processing thread
|
|
|
|
/// and the TUScheduler. The TUScheduler should discard an ASTWorker when
|
|
|
|
/// remove() is called, but its thread may be busy and we don't want to block.
|
|
|
|
/// So the workers are accessed via an ASTWorkerHandle. Destroying the handle
|
|
|
|
/// signals the worker to exit its run loop and gives up shared ownership of the
|
|
|
|
/// worker.
|
|
|
|
class ASTWorker {
|
|
|
|
friend class ASTWorkerHandle;
|
2018-03-02 16:56:37 +08:00
|
|
|
ASTWorker(llvm::StringRef File, Semaphore &Barrier, CppFile AST, bool RunSync,
|
|
|
|
steady_clock::duration UpdateDebounce);
|
2018-02-08 15:37:35 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
/// Create a new ASTWorker and return a handle to it.
|
|
|
|
/// The processing thread is spawned using \p Tasks. However, when \p Tasks
|
|
|
|
/// is null, all requests will be processed on the calling thread
|
|
|
|
/// synchronously instead. \p Barrier is acquired when processing each
|
|
|
|
/// request, it is be used to limit the number of actively running threads.
|
2018-02-19 17:56:28 +08:00
|
|
|
static ASTWorkerHandle Create(llvm::StringRef File, AsyncTaskRunner *Tasks,
|
2018-03-02 16:56:37 +08:00
|
|
|
Semaphore &Barrier, CppFile AST,
|
|
|
|
steady_clock::duration UpdateDebounce);
|
2018-02-08 15:37:35 +08:00
|
|
|
~ASTWorker();
|
|
|
|
|
2018-02-22 21:11:12 +08:00
|
|
|
void update(ParseInputs Inputs, WantDiagnostics,
|
|
|
|
UniqueFunction<void(std::vector<DiagWithFixIts>)> OnUpdated);
|
2018-02-19 17:56:28 +08:00
|
|
|
void runWithAST(llvm::StringRef Name,
|
|
|
|
UniqueFunction<void(llvm::Expected<InputsAndAST>)> Action);
|
2018-02-13 16:59:23 +08:00
|
|
|
bool blockUntilIdle(Deadline Timeout) const;
|
2018-02-08 15:37:35 +08:00
|
|
|
|
|
|
|
std::shared_ptr<const PreambleData> getPossiblyStalePreamble() const;
|
|
|
|
std::size_t getUsedBytes() const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Must be called exactly once on processing thread. Will return after
|
|
|
|
// stop() is called on a separate thread and all pending requests are
|
|
|
|
// processed.
|
|
|
|
void run();
|
|
|
|
/// Signal that run() should finish processing pending requests and exit.
|
|
|
|
void stop();
|
|
|
|
/// Adds a new task to the end of the request queue.
|
2018-02-19 17:56:28 +08:00
|
|
|
void startTask(llvm::StringRef Name, UniqueFunction<void()> Task,
|
2018-02-22 21:11:12 +08:00
|
|
|
llvm::Optional<WantDiagnostics> UpdateType);
|
2018-03-02 16:56:37 +08:00
|
|
|
/// Determines the next action to perform.
|
|
|
|
/// All actions that should never run are disarded.
|
|
|
|
/// Returns a deadline for the next action. If it's expired, run now.
|
|
|
|
/// scheduleLocked() is called again at the deadline, or if requests arrive.
|
|
|
|
Deadline scheduleLocked();
|
2018-02-22 21:11:12 +08:00
|
|
|
/// Should the first task in the queue be skipped instead of run?
|
|
|
|
bool shouldSkipHeadLocked() const;
|
2018-02-08 15:37:35 +08:00
|
|
|
|
2018-02-19 17:56:28 +08:00
|
|
|
struct Request {
|
|
|
|
UniqueFunction<void()> Action;
|
|
|
|
std::string Name;
|
2018-03-02 16:56:37 +08:00
|
|
|
steady_clock::time_point AddTime;
|
2018-02-19 17:56:28 +08:00
|
|
|
Context Ctx;
|
2018-02-22 21:11:12 +08:00
|
|
|
llvm::Optional<WantDiagnostics> UpdateType;
|
2018-02-19 17:56:28 +08:00
|
|
|
};
|
2018-02-08 15:37:35 +08:00
|
|
|
|
2018-03-02 16:56:37 +08:00
|
|
|
const std::string File;
|
2018-02-08 15:37:35 +08:00
|
|
|
const bool RunSync;
|
2018-03-02 16:56:37 +08:00
|
|
|
// Time to wait after an update to see whether another update obsoletes it.
|
|
|
|
const steady_clock::duration UpdateDebounce;
|
|
|
|
|
2018-02-08 15:37:35 +08:00
|
|
|
Semaphore &Barrier;
|
|
|
|
// AST and FileInputs are only accessed on the processing thread from run().
|
2018-02-09 18:17:23 +08:00
|
|
|
CppFile AST;
|
2018-02-08 15:37:35 +08:00
|
|
|
// Inputs, corresponding to the current state of AST.
|
|
|
|
ParseInputs FileInputs;
|
|
|
|
// Guards members used by both TUScheduler and the worker thread.
|
|
|
|
mutable std::mutex Mutex;
|
2018-02-09 18:17:23 +08:00
|
|
|
std::shared_ptr<const PreambleData> LastBuiltPreamble; /* GUARDED_BY(Mutex) */
|
|
|
|
// Result of getUsedBytes() after the last rebuild or read of AST.
|
|
|
|
std::size_t LastASTSize; /* GUARDED_BY(Mutex) */
|
2018-02-08 15:37:35 +08:00
|
|
|
// Set to true to signal run() to finish processing.
|
|
|
|
bool Done; /* GUARDED_BY(Mutex) */
|
2018-02-22 21:11:12 +08:00
|
|
|
std::deque<Request> Requests; /* GUARDED_BY(Mutex) */
|
2018-02-13 16:59:23 +08:00
|
|
|
mutable std::condition_variable RequestsCV;
|
2018-02-08 15:37:35 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/// A smart-pointer-like class that points to an active ASTWorker.
|
|
|
|
/// In destructor, signals to the underlying ASTWorker that no new requests will
|
|
|
|
/// be sent and the processing loop may exit (after running all pending
|
|
|
|
/// requests).
|
|
|
|
class ASTWorkerHandle {
|
|
|
|
friend class ASTWorker;
|
|
|
|
ASTWorkerHandle(std::shared_ptr<ASTWorker> Worker)
|
|
|
|
: Worker(std::move(Worker)) {
|
|
|
|
assert(this->Worker);
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
ASTWorkerHandle(const ASTWorkerHandle &) = delete;
|
|
|
|
ASTWorkerHandle &operator=(const ASTWorkerHandle &) = delete;
|
|
|
|
ASTWorkerHandle(ASTWorkerHandle &&) = default;
|
|
|
|
ASTWorkerHandle &operator=(ASTWorkerHandle &&) = default;
|
|
|
|
|
|
|
|
~ASTWorkerHandle() {
|
|
|
|
if (Worker)
|
|
|
|
Worker->stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
ASTWorker &operator*() {
|
|
|
|
assert(Worker && "Handle was moved from");
|
|
|
|
return *Worker;
|
|
|
|
}
|
|
|
|
|
|
|
|
ASTWorker *operator->() {
|
|
|
|
assert(Worker && "Handle was moved from");
|
|
|
|
return Worker.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns an owning reference to the underlying ASTWorker that can outlive
|
|
|
|
/// the ASTWorkerHandle. However, no new requests to an active ASTWorker can
|
|
|
|
/// be schedule via the returned reference, i.e. only reads of the preamble
|
|
|
|
/// are possible.
|
|
|
|
std::shared_ptr<const ASTWorker> lock() { return Worker; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::shared_ptr<ASTWorker> Worker;
|
|
|
|
};
|
|
|
|
|
2018-02-19 17:56:28 +08:00
|
|
|
ASTWorkerHandle ASTWorker::Create(llvm::StringRef File, AsyncTaskRunner *Tasks,
|
2018-03-02 16:56:37 +08:00
|
|
|
Semaphore &Barrier, CppFile AST,
|
|
|
|
steady_clock::duration UpdateDebounce) {
|
|
|
|
std::shared_ptr<ASTWorker> Worker(new ASTWorker(
|
|
|
|
File, Barrier, std::move(AST), /*RunSync=*/!Tasks, UpdateDebounce));
|
2018-02-08 15:37:35 +08:00
|
|
|
if (Tasks)
|
2018-02-19 17:56:28 +08:00
|
|
|
Tasks->runAsync("worker:" + llvm::sys::path::filename(File),
|
|
|
|
[Worker]() { Worker->run(); });
|
2018-02-08 15:37:35 +08:00
|
|
|
|
|
|
|
return ASTWorkerHandle(std::move(Worker));
|
|
|
|
}
|
|
|
|
|
2018-02-19 17:56:28 +08:00
|
|
|
ASTWorker::ASTWorker(llvm::StringRef File, Semaphore &Barrier, CppFile AST,
|
2018-03-02 16:56:37 +08:00
|
|
|
bool RunSync, steady_clock::duration UpdateDebounce)
|
|
|
|
: File(File), RunSync(RunSync), UpdateDebounce(UpdateDebounce),
|
|
|
|
Barrier(Barrier), AST(std::move(AST)), Done(false) {
|
2018-02-08 15:37:35 +08:00
|
|
|
if (RunSync)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ASTWorker::~ASTWorker() {
|
|
|
|
#ifndef NDEBUG
|
|
|
|
std::lock_guard<std::mutex> Lock(Mutex);
|
|
|
|
assert(Done && "handle was not destroyed");
|
|
|
|
assert(Requests.empty() && "unprocessed requests when destroying ASTWorker");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void ASTWorker::update(
|
2018-02-22 21:11:12 +08:00
|
|
|
ParseInputs Inputs, WantDiagnostics WantDiags,
|
|
|
|
UniqueFunction<void(std::vector<DiagWithFixIts>)> OnUpdated) {
|
|
|
|
auto Task = [=](decltype(OnUpdated) OnUpdated) mutable {
|
2018-02-08 15:37:35 +08:00
|
|
|
FileInputs = Inputs;
|
2018-02-09 18:17:23 +08:00
|
|
|
auto Diags = AST.rebuild(std::move(Inputs));
|
|
|
|
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> Lock(Mutex);
|
|
|
|
if (AST.getPreamble())
|
|
|
|
LastBuiltPreamble = AST.getPreamble();
|
|
|
|
LastASTSize = AST.getUsedBytes();
|
|
|
|
}
|
2018-02-08 15:37:35 +08:00
|
|
|
// We want to report the diagnostics even if this update was cancelled.
|
|
|
|
// It seems more useful than making the clients wait indefinitely if they
|
|
|
|
// spam us with updates.
|
2018-02-22 21:11:12 +08:00
|
|
|
if (Diags && WantDiags != WantDiagnostics::No)
|
|
|
|
OnUpdated(std::move(*Diags));
|
2018-02-08 15:37:35 +08:00
|
|
|
};
|
|
|
|
|
2018-02-23 15:54:17 +08:00
|
|
|
startTask("Update", Bind(Task, std::move(OnUpdated)), WantDiags);
|
2018-02-08 15:37:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ASTWorker::runWithAST(
|
2018-02-19 17:56:28 +08:00
|
|
|
llvm::StringRef Name,
|
2018-02-08 15:37:35 +08:00
|
|
|
UniqueFunction<void(llvm::Expected<InputsAndAST>)> Action) {
|
|
|
|
auto Task = [=](decltype(Action) Action) {
|
2018-02-09 18:17:23 +08:00
|
|
|
ParsedAST *ActualAST = AST.getAST();
|
|
|
|
if (!ActualAST) {
|
|
|
|
Action(llvm::make_error<llvm::StringError>("invalid AST",
|
|
|
|
llvm::errc::invalid_argument));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Action(InputsAndAST{FileInputs, *ActualAST});
|
|
|
|
|
|
|
|
// Size of the AST might have changed after reads too, e.g. if some decls
|
|
|
|
// were deserialized from preamble.
|
|
|
|
std::lock_guard<std::mutex> Lock(Mutex);
|
|
|
|
LastASTSize = ActualAST->getUsedBytes();
|
2018-02-08 15:37:35 +08:00
|
|
|
};
|
|
|
|
|
2018-02-23 15:54:17 +08:00
|
|
|
startTask(Name, Bind(Task, std::move(Action)),
|
2018-02-22 21:11:12 +08:00
|
|
|
/*UpdateType=*/llvm::None);
|
2018-02-08 15:37:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<const PreambleData>
|
|
|
|
ASTWorker::getPossiblyStalePreamble() const {
|
2018-02-09 18:17:23 +08:00
|
|
|
std::lock_guard<std::mutex> Lock(Mutex);
|
|
|
|
return LastBuiltPreamble;
|
2018-02-08 15:37:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::size_t ASTWorker::getUsedBytes() const {
|
2018-02-09 18:17:23 +08:00
|
|
|
std::lock_guard<std::mutex> Lock(Mutex);
|
|
|
|
return LastASTSize;
|
2018-02-08 15:37:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ASTWorker::stop() {
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> Lock(Mutex);
|
|
|
|
assert(!Done && "stop() called twice");
|
|
|
|
Done = true;
|
|
|
|
}
|
2018-02-13 16:59:23 +08:00
|
|
|
RequestsCV.notify_all();
|
2018-02-08 15:37:35 +08:00
|
|
|
}
|
|
|
|
|
2018-02-19 17:56:28 +08:00
|
|
|
void ASTWorker::startTask(llvm::StringRef Name, UniqueFunction<void()> Task,
|
2018-02-22 21:11:12 +08:00
|
|
|
llvm::Optional<WantDiagnostics> UpdateType) {
|
2018-02-08 15:37:35 +08:00
|
|
|
if (RunSync) {
|
|
|
|
assert(!Done && "running a task after stop()");
|
2018-02-19 17:56:28 +08:00
|
|
|
trace::Span Tracer(Name + ":" + llvm::sys::path::filename(File));
|
2018-02-08 15:37:35 +08:00
|
|
|
Task();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> Lock(Mutex);
|
|
|
|
assert(!Done && "running a task after stop()");
|
2018-03-02 16:56:37 +08:00
|
|
|
Requests.push_back({std::move(Task), Name, steady_clock::now(),
|
|
|
|
Context::current().clone(), UpdateType});
|
2018-02-22 21:11:12 +08:00
|
|
|
}
|
2018-02-13 16:59:23 +08:00
|
|
|
RequestsCV.notify_all();
|
2018-02-08 15:37:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ASTWorker::run() {
|
|
|
|
while (true) {
|
2018-02-19 17:56:28 +08:00
|
|
|
Request Req;
|
2018-02-08 15:37:35 +08:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> Lock(Mutex);
|
2018-03-02 16:56:37 +08:00
|
|
|
for (auto Wait = scheduleLocked(); !Wait.expired();
|
|
|
|
Wait = scheduleLocked()) {
|
|
|
|
if (Done) {
|
|
|
|
if (Requests.empty())
|
|
|
|
return;
|
|
|
|
else // Even though Done is set, finish pending requests.
|
|
|
|
break; // However, skip delays to shutdown fast.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tracing: we have a next request, attribute this sleep to it.
|
|
|
|
Optional<WithContext> Ctx;
|
|
|
|
Optional<trace::Span> Tracer;
|
|
|
|
if (!Requests.empty()) {
|
|
|
|
Ctx.emplace(Requests.front().Ctx.clone());
|
|
|
|
Tracer.emplace("Debounce");
|
|
|
|
SPAN_ATTACH(*Tracer, "next_request", Requests.front().Name);
|
|
|
|
if (!(Wait == Deadline::infinity()))
|
|
|
|
SPAN_ATTACH(*Tracer, "sleep_ms",
|
|
|
|
std::chrono::duration_cast<std::chrono::milliseconds>(
|
|
|
|
Wait.time() - steady_clock::now())
|
|
|
|
.count());
|
|
|
|
}
|
|
|
|
|
|
|
|
wait(Lock, RequestsCV, Wait);
|
2018-02-08 15:37:35 +08:00
|
|
|
}
|
|
|
|
Req = std::move(Requests.front());
|
2018-02-13 16:59:23 +08:00
|
|
|
// Leave it on the queue for now, so waiters don't see an empty queue.
|
2018-02-08 15:37:35 +08:00
|
|
|
} // unlock Mutex
|
|
|
|
|
2018-02-19 17:56:28 +08:00
|
|
|
{
|
|
|
|
std::lock_guard<Semaphore> BarrierLock(Barrier);
|
|
|
|
WithContext Guard(std::move(Req.Ctx));
|
|
|
|
trace::Span Tracer(Req.Name);
|
|
|
|
Req.Action();
|
|
|
|
}
|
2018-02-13 16:59:23 +08:00
|
|
|
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> Lock(Mutex);
|
2018-02-22 21:11:12 +08:00
|
|
|
Requests.pop_front();
|
2018-02-13 16:59:23 +08:00
|
|
|
}
|
|
|
|
RequestsCV.notify_all();
|
2018-02-08 15:37:35 +08:00
|
|
|
}
|
|
|
|
}
|
2018-02-13 16:59:23 +08:00
|
|
|
|
2018-03-02 16:56:37 +08:00
|
|
|
Deadline ASTWorker::scheduleLocked() {
|
|
|
|
if (Requests.empty())
|
|
|
|
return Deadline::infinity(); // Wait for new requests.
|
|
|
|
while (shouldSkipHeadLocked())
|
|
|
|
Requests.pop_front();
|
|
|
|
assert(!Requests.empty() && "skipped the whole queue");
|
|
|
|
// Some updates aren't dead yet, but never end up being used.
|
|
|
|
// e.g. the first keystroke is live until obsoleted by the second.
|
|
|
|
// We debounce "maybe-unused" writes, sleeping 500ms in case they become dead.
|
|
|
|
// But don't delay reads (including updates where diagnostics are needed).
|
|
|
|
for (const auto &R : Requests)
|
|
|
|
if (R.UpdateType == None || R.UpdateType == WantDiagnostics::Yes)
|
|
|
|
return Deadline::zero();
|
|
|
|
// Front request needs to be debounced, so determine when we're ready.
|
|
|
|
Deadline D(Requests.front().AddTime + UpdateDebounce);
|
|
|
|
return D;
|
|
|
|
}
|
|
|
|
|
2018-02-22 21:11:12 +08:00
|
|
|
// Returns true if Requests.front() is a dead update that can be skipped.
|
|
|
|
bool ASTWorker::shouldSkipHeadLocked() const {
|
|
|
|
assert(!Requests.empty());
|
|
|
|
auto Next = Requests.begin();
|
|
|
|
auto UpdateType = Next->UpdateType;
|
|
|
|
if (!UpdateType) // Only skip updates.
|
|
|
|
return false;
|
|
|
|
++Next;
|
|
|
|
// An update is live if its AST might still be read.
|
|
|
|
// That is, if it's not immediately followed by another update.
|
|
|
|
if (Next == Requests.end() || !Next->UpdateType)
|
|
|
|
return false;
|
|
|
|
// The other way an update can be live is if its diagnostics might be used.
|
|
|
|
switch (*UpdateType) {
|
|
|
|
case WantDiagnostics::Yes:
|
|
|
|
return false; // Always used.
|
|
|
|
case WantDiagnostics::No:
|
|
|
|
return true; // Always dead.
|
|
|
|
case WantDiagnostics::Auto:
|
|
|
|
// Used unless followed by an update that generates diagnostics.
|
|
|
|
for (; Next != Requests.end(); ++Next)
|
|
|
|
if (Next->UpdateType == WantDiagnostics::Yes ||
|
|
|
|
Next->UpdateType == WantDiagnostics::Auto)
|
|
|
|
return true; // Prefer later diagnostics.
|
|
|
|
return false;
|
|
|
|
}
|
2018-02-23 00:12:27 +08:00
|
|
|
llvm_unreachable("Unknown WantDiagnostics");
|
2018-02-22 21:11:12 +08:00
|
|
|
}
|
|
|
|
|
2018-02-13 16:59:23 +08:00
|
|
|
bool ASTWorker::blockUntilIdle(Deadline Timeout) const {
|
|
|
|
std::unique_lock<std::mutex> Lock(Mutex);
|
|
|
|
return wait(Lock, RequestsCV, Timeout, [&] { return Requests.empty(); });
|
|
|
|
}
|
|
|
|
|
2018-02-08 15:37:35 +08:00
|
|
|
} // namespace
|
|
|
|
|
2018-01-31 16:51:16 +08:00
|
|
|
unsigned getDefaultAsyncThreadsCount() {
|
|
|
|
unsigned HardwareConcurrency = std::thread::hardware_concurrency();
|
|
|
|
// C++ standard says that hardware_concurrency()
|
|
|
|
// may return 0, fallback to 1 worker thread in
|
|
|
|
// that case.
|
|
|
|
if (HardwareConcurrency == 0)
|
|
|
|
return 1;
|
|
|
|
return HardwareConcurrency;
|
|
|
|
}
|
|
|
|
|
2018-02-08 15:37:35 +08:00
|
|
|
struct TUScheduler::FileData {
|
|
|
|
/// Latest inputs, passed to TUScheduler::update().
|
|
|
|
ParseInputs Inputs;
|
|
|
|
ASTWorkerHandle Worker;
|
|
|
|
};
|
|
|
|
|
2018-01-31 16:51:16 +08:00
|
|
|
TUScheduler::TUScheduler(unsigned AsyncThreadsCount,
|
|
|
|
bool StorePreamblesInMemory,
|
2018-03-02 16:56:37 +08:00
|
|
|
ASTParsedCallback ASTCallback,
|
|
|
|
steady_clock::duration UpdateDebounce)
|
2018-02-08 15:37:35 +08:00
|
|
|
: StorePreamblesInMemory(StorePreamblesInMemory),
|
|
|
|
PCHOps(std::make_shared<PCHContainerOperations>()),
|
2018-03-02 16:56:37 +08:00
|
|
|
ASTCallback(std::move(ASTCallback)), Barrier(AsyncThreadsCount),
|
|
|
|
UpdateDebounce(UpdateDebounce) {
|
2018-02-13 16:59:23 +08:00
|
|
|
if (0 < AsyncThreadsCount) {
|
|
|
|
PreambleTasks.emplace();
|
|
|
|
WorkerThreads.emplace();
|
|
|
|
}
|
2018-02-08 15:37:35 +08:00
|
|
|
}
|
2018-01-31 16:51:16 +08:00
|
|
|
|
2018-02-08 15:37:35 +08:00
|
|
|
TUScheduler::~TUScheduler() {
|
|
|
|
// Notify all workers that they need to stop.
|
|
|
|
Files.clear();
|
|
|
|
|
|
|
|
// Wait for all in-flight tasks to finish.
|
2018-02-13 16:59:23 +08:00
|
|
|
if (PreambleTasks)
|
|
|
|
PreambleTasks->wait();
|
|
|
|
if (WorkerThreads)
|
|
|
|
WorkerThreads->wait();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TUScheduler::blockUntilIdle(Deadline D) const {
|
|
|
|
for (auto &File : Files)
|
|
|
|
if (!File.getValue()->Worker->blockUntilIdle(D))
|
|
|
|
return false;
|
|
|
|
if (PreambleTasks)
|
|
|
|
if (!PreambleTasks->wait(D))
|
|
|
|
return false;
|
|
|
|
return true;
|
2018-02-08 15:37:35 +08:00
|
|
|
}
|
2018-01-31 16:51:16 +08:00
|
|
|
|
|
|
|
void TUScheduler::update(
|
2018-02-22 21:11:12 +08:00
|
|
|
PathRef File, ParseInputs Inputs, WantDiagnostics WantDiags,
|
|
|
|
UniqueFunction<void(std::vector<DiagWithFixIts>)> OnUpdated) {
|
2018-02-08 15:37:35 +08:00
|
|
|
std::unique_ptr<FileData> &FD = Files[File];
|
|
|
|
if (!FD) {
|
|
|
|
// Create a new worker to process the AST-related tasks.
|
|
|
|
ASTWorkerHandle Worker = ASTWorker::Create(
|
2018-02-19 17:56:28 +08:00
|
|
|
File, WorkerThreads ? WorkerThreads.getPointer() : nullptr, Barrier,
|
2018-03-02 16:56:37 +08:00
|
|
|
CppFile(File, StorePreamblesInMemory, PCHOps, ASTCallback),
|
|
|
|
UpdateDebounce);
|
2018-02-08 15:37:35 +08:00
|
|
|
FD = std::unique_ptr<FileData>(new FileData{Inputs, std::move(Worker)});
|
|
|
|
} else {
|
|
|
|
FD->Inputs = Inputs;
|
2018-01-31 16:51:16 +08:00
|
|
|
}
|
2018-02-22 21:11:12 +08:00
|
|
|
FD->Worker->update(std::move(Inputs), WantDiags, std::move(OnUpdated));
|
2018-02-08 15:37:35 +08:00
|
|
|
}
|
2018-02-07 03:22:40 +08:00
|
|
|
|
2018-02-08 15:37:35 +08:00
|
|
|
void TUScheduler::remove(PathRef File) {
|
|
|
|
bool Removed = Files.erase(File);
|
|
|
|
if (!Removed)
|
|
|
|
log("Trying to remove file from TUScheduler that is not tracked. File:" +
|
|
|
|
File);
|
2018-01-31 16:51:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void TUScheduler::runWithAST(
|
2018-02-19 17:56:28 +08:00
|
|
|
llvm::StringRef Name, PathRef File,
|
|
|
|
UniqueFunction<void(llvm::Expected<InputsAndAST>)> Action) {
|
2018-02-08 15:37:35 +08:00
|
|
|
auto It = Files.find(File);
|
|
|
|
if (It == Files.end()) {
|
2018-01-31 16:51:16 +08:00
|
|
|
Action(llvm::make_error<llvm::StringError>(
|
|
|
|
"trying to get AST for non-added document",
|
|
|
|
llvm::errc::invalid_argument));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-19 17:56:28 +08:00
|
|
|
It->second->Worker->runWithAST(Name, std::move(Action));
|
2018-01-31 16:51:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void TUScheduler::runWithPreamble(
|
2018-02-19 17:56:28 +08:00
|
|
|
llvm::StringRef Name, PathRef File,
|
2018-01-31 16:51:16 +08:00
|
|
|
UniqueFunction<void(llvm::Expected<InputsAndPreamble>)> Action) {
|
2018-02-08 15:37:35 +08:00
|
|
|
auto It = Files.find(File);
|
|
|
|
if (It == Files.end()) {
|
2018-01-31 16:51:16 +08:00
|
|
|
Action(llvm::make_error<llvm::StringError>(
|
|
|
|
"trying to get preamble for non-added document",
|
|
|
|
llvm::errc::invalid_argument));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-13 16:59:23 +08:00
|
|
|
if (!PreambleTasks) {
|
2018-02-19 17:56:28 +08:00
|
|
|
trace::Span Tracer(Name);
|
|
|
|
SPAN_ATTACH(Tracer, "file", File);
|
2018-02-08 15:37:35 +08:00
|
|
|
std::shared_ptr<const PreambleData> Preamble =
|
|
|
|
It->second->Worker->getPossiblyStalePreamble();
|
|
|
|
Action(InputsAndPreamble{It->second->Inputs, Preamble.get()});
|
|
|
|
return;
|
|
|
|
}
|
2018-01-31 16:51:16 +08:00
|
|
|
|
2018-02-08 15:37:35 +08:00
|
|
|
ParseInputs InputsCopy = It->second->Inputs;
|
|
|
|
std::shared_ptr<const ASTWorker> Worker = It->second->Worker.lock();
|
2018-02-19 17:56:28 +08:00
|
|
|
auto Task = [InputsCopy, Worker, this](std::string Name, std::string File,
|
|
|
|
Context Ctx,
|
2018-02-08 15:37:35 +08:00
|
|
|
decltype(Action) Action) mutable {
|
|
|
|
std::lock_guard<Semaphore> BarrierLock(Barrier);
|
|
|
|
WithContext Guard(std::move(Ctx));
|
2018-02-19 17:56:28 +08:00
|
|
|
trace::Span Tracer(Name);
|
|
|
|
SPAN_ATTACH(Tracer, "file", File);
|
2018-02-08 15:37:35 +08:00
|
|
|
std::shared_ptr<const PreambleData> Preamble =
|
|
|
|
Worker->getPossiblyStalePreamble();
|
|
|
|
Action(InputsAndPreamble{InputsCopy, Preamble.get()});
|
|
|
|
};
|
2018-01-31 16:51:16 +08:00
|
|
|
|
2018-02-23 15:54:17 +08:00
|
|
|
PreambleTasks->runAsync("task:" + llvm::sys::path::filename(File),
|
|
|
|
Bind(Task, std::string(Name), std::string(File),
|
|
|
|
Context::current().clone(), std::move(Action)));
|
2018-01-31 16:51:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::pair<Path, std::size_t>>
|
|
|
|
TUScheduler::getUsedBytesPerFile() const {
|
2018-02-08 15:37:35 +08:00
|
|
|
std::vector<std::pair<Path, std::size_t>> Result;
|
|
|
|
Result.reserve(Files.size());
|
|
|
|
for (auto &&PathAndFile : Files)
|
|
|
|
Result.push_back(
|
|
|
|
{PathAndFile.first(), PathAndFile.second->Worker->getUsedBytes()});
|
|
|
|
return Result;
|
2018-01-31 16:51:16 +08:00
|
|
|
}
|
2018-02-08 15:37:35 +08:00
|
|
|
|
2018-01-31 16:51:16 +08:00
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|