diff options
| author | Kadir Cetinkaya <kadircet@google.com> | 2018-10-30 12:13:27 +0000 |
|---|---|---|
| committer | Kadir Cetinkaya <kadircet@google.com> | 2018-10-30 12:13:27 +0000 |
| commit | 6675be87477395658f6d3859b028ab8dee81ba19 (patch) | |
| tree | 14064c7b081da8a5c91c985ce525440c82edfc47 | |
| parent | 85d3f1ee8f109c0f180050020ef9d7c233f782e5 (diff) | |
| download | bcm5719-llvm-6675be87477395658f6d3859b028ab8dee81ba19.tar.gz bcm5719-llvm-6675be87477395658f6d3859b028ab8dee81ba19.zip | |
[clangd] Use thread pool for background indexing.
Reviewers: sammccall, ioeric
Reviewed By: sammccall
Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, jfb, cfe-commits
Differential Revision: https://reviews.llvm.org/D53651
llvm-svn: 345590
| -rw-r--r-- | clang-tools-extra/clangd/Threading.cpp | 14 | ||||
| -rw-r--r-- | clang-tools-extra/clangd/Threading.h | 8 | ||||
| -rw-r--r-- | clang-tools-extra/clangd/index/Background.cpp | 21 | ||||
| -rw-r--r-- | clang-tools-extra/clangd/index/Background.h | 8 |
4 files changed, 44 insertions, 7 deletions
diff --git a/clang-tools-extra/clangd/Threading.cpp b/clang-tools-extra/clangd/Threading.cpp index acd45108418..9f1cbef95c0 100644 --- a/clang-tools-extra/clangd/Threading.cpp +++ b/clang-tools-extra/clangd/Threading.cpp @@ -1,9 +1,13 @@ #include "Threading.h" #include "Trace.h" #include "llvm/ADT/ScopeExit.h" +#include "llvm/Config/config.h" #include "llvm/Support/FormatVariadic.h" #include "llvm/Support/Threading.h" #include <thread> +#ifdef HAVE_PTHREAD_H +#include <pthread.h> +#endif using namespace llvm; namespace clang { @@ -97,5 +101,15 @@ void wait(std::unique_lock<std::mutex> &Lock, std::condition_variable &CV, CV.wait_until(Lock, D.time()); } +void setThreadPriority(std::thread &T, ThreadPriority Priority) { +#ifdef HAVE_PTHREAD_H + sched_param priority; + priority.sched_priority = 0; + pthread_setschedparam( + T.native_handle(), + Priority == ThreadPriority::Low ? SCHED_IDLE : SCHED_OTHER, &priority); +#endif +} + } // namespace clangd } // namespace clang diff --git a/clang-tools-extra/clangd/Threading.h b/clang-tools-extra/clangd/Threading.h index 6dad841da58..71becf68976 100644 --- a/clang-tools-extra/clangd/Threading.h +++ b/clang-tools-extra/clangd/Threading.h @@ -17,6 +17,7 @@ #include <condition_variable> #include <memory> #include <mutex> +#include <thread> #include <vector> namespace clang { @@ -115,6 +116,13 @@ private: mutable std::condition_variable TasksReachedZero; std::size_t InFlightTasks = 0; }; + +enum class ThreadPriority { + Low = 0, + Normal = 1, +}; +void setThreadPriority(std::thread &T, ThreadPriority Priority); + } // namespace clangd } // namespace clang #endif diff --git a/clang-tools-extra/clangd/index/Background.cpp b/clang-tools-extra/clangd/index/Background.cpp index 9137d1d84b8..7d9cd711fe7 100644 --- a/clang-tools-extra/clangd/index/Background.cpp +++ b/clang-tools-extra/clangd/index/Background.cpp @@ -11,6 +11,7 @@ #include "ClangdUnit.h" #include "Compiler.h" #include "Logger.h" +#include "Threading.h" #include "Trace.h" #include "index/IndexAction.h" #include "index/MemIndex.h" @@ -25,14 +26,26 @@ namespace clangd { BackgroundIndex::BackgroundIndex(Context BackgroundContext, StringRef ResourceDir, const FileSystemProvider &FSProvider, - ArrayRef<std::string> URISchemes) + ArrayRef<std::string> URISchemes, + size_t ThreadPoolSize) : SwapIndex(make_unique<MemIndex>()), ResourceDir(ResourceDir), FSProvider(FSProvider), BackgroundContext(std::move(BackgroundContext)), - URISchemes(URISchemes), Thread([this] { run(); }) {} + URISchemes(URISchemes) { + assert(ThreadPoolSize > 0 && "Thread pool size can't be zero."); + while (ThreadPoolSize--) { + ThreadPool.emplace_back([this] { run(); }); + // Set priority to low, since background indexing is a long running task we + // do not want to eat up cpu when there are any other high priority threads. + // FIXME: In the future we might want a more general way of handling this to + // support a tasks with various priorities. + setThreadPriority(ThreadPool.back(), ThreadPriority::Low); + } +} BackgroundIndex::~BackgroundIndex() { stop(); - Thread.join(); + for (auto &Thread : ThreadPool) + Thread.join(); } void BackgroundIndex::stop() { @@ -44,7 +57,7 @@ void BackgroundIndex::stop() { } void BackgroundIndex::run() { - WithContext Background(std::move(BackgroundContext)); + WithContext Background(BackgroundContext.clone()); while (true) { Optional<Task> Task; { diff --git a/clang-tools-extra/clangd/index/Background.h b/clang-tools-extra/clangd/index/Background.h index 57cee99d922..c4070455bf2 100644 --- a/clang-tools-extra/clangd/index/Background.h +++ b/clang-tools-extra/clangd/index/Background.h @@ -16,6 +16,7 @@ #include "index/Index.h" #include "clang/Tooling/CompilationDatabase.h" #include "llvm/Support/SHA1.h" +#include "llvm/Support/Threading.h" #include <condition_variable> #include <deque> #include <string> @@ -34,7 +35,8 @@ public: // FIXME: resource-dir injection should be hoisted somewhere common. BackgroundIndex(Context BackgroundContext, StringRef ResourceDir, const FileSystemProvider &, - ArrayRef<std::string> URISchemes = {}); + ArrayRef<std::string> URISchemes = {}, + size_t ThreadPoolSize = llvm::hardware_concurrency()); ~BackgroundIndex(); // Blocks while the current task finishes. // Enqueue a translation unit for indexing. @@ -66,7 +68,7 @@ private: llvm::StringMap<Hash> FileHash; // Digest of indexed file. // queue management - using Task = std::function<void()>; // FIXME: use multiple worker threads. + using Task = std::function<void()>; void run(); // Main loop executed by Thread. Runs tasks from Queue. void enqueueLocked(tooling::CompileCommand Cmd); std::mutex QueueMu; @@ -74,7 +76,7 @@ private: std::condition_variable QueueCV; bool ShouldStop = false; std::deque<Task> Queue; - std::thread Thread; // Must be last, spawned thread reads instance vars. + std::vector<std::thread> ThreadPool; // FIXME: Abstract this away. }; } // namespace clangd |

