diff --git a/clang-tools-extra/clangd/ClangdServer.h b/clang-tools-extra/clangd/ClangdServer.h --- a/clang-tools-extra/clangd/ClangdServer.h +++ b/clang-tools-extra/clangd/ClangdServer.h @@ -110,6 +110,7 @@ /// If true, ClangdServer automatically indexes files in the current project /// on background threads. The index is stored in the project root. bool BackgroundIndex = false; + llvm::ThreadPriority BackgroundIndexPriority = llvm::ThreadPriority::Low; /// If set, use this index to augment code completion results. SymbolIndex *StaticIndex = nullptr; diff --git a/clang-tools-extra/clangd/index/Background.h b/clang-tools-extra/clangd/index/Background.h --- a/clang-tools-extra/clangd/index/Background.h +++ b/clang-tools-extra/clangd/index/Background.h @@ -136,6 +136,8 @@ // Arbitrary value to ensure some concurrency in tests. // In production an explicit value is specified. size_t ThreadPoolSize = 4; + // Thread priority when indexing files. + llvm::ThreadPriority IndexingPriority = llvm::ThreadPriority::Low; // Callback that provides notifications as indexing makes progress. std::function OnProgress = nullptr; // Function called to obtain the Context to use while indexing the specified @@ -194,6 +196,7 @@ // configuration const ThreadsafeFS &TFS; const GlobalCompilationDatabase &CDB; + llvm::ThreadPriority IndexingPriority; std::function ContextProvider; llvm::Error index(tooling::CompileCommand); diff --git a/clang-tools-extra/clangd/index/Background.cpp b/clang-tools-extra/clangd/index/Background.cpp --- a/clang-tools-extra/clangd/index/Background.cpp +++ b/clang-tools-extra/clangd/index/Background.cpp @@ -92,6 +92,7 @@ const ThreadsafeFS &TFS, const GlobalCompilationDatabase &CDB, BackgroundIndexStorage::Factory IndexStorageFactory, Options Opts) : SwapIndex(std::make_unique()), TFS(TFS), CDB(CDB), + IndexingPriority(Opts.IndexingPriority), ContextProvider(std::move(Opts.ContextProvider)), IndexedSymbols(IndexContents::All), Rebuilder(this, &IndexedSymbols, Opts.ThreadPoolSize), @@ -165,6 +166,7 @@ elog("Indexing {0} failed: {1}", Path, std::move(Error)); }); T.QueuePri = IndexFile; + T.ThreadPri = IndexingPriority; T.Tag = std::move(Tag); T.Key = Key; return T; diff --git a/clang-tools-extra/clangd/tool/ClangdMain.cpp b/clang-tools-extra/clangd/tool/ClangdMain.cpp --- a/clang-tools-extra/clangd/tool/ClangdMain.cpp +++ b/clang-tools-extra/clangd/tool/ClangdMain.cpp @@ -169,6 +169,21 @@ init(true), }; +opt BackgroundIndexPriority{ + "background-index-priority", + cat(Features), + desc("Thread priority for building the background index. " + "The effect of this flag is OS-specific."), + values(clEnumValN(llvm::ThreadPriority::Background, "background", + "Minimum priority, runs on idle CPUs. " + "May leave 'performance' cores unused."), + clEnumValN(llvm::ThreadPriority::Low, "low", + "Reduced priority compared to interactive work."), + clEnumValN(llvm::ThreadPriority::Default, "normal", + "Same priority as other clangd work.")), + init(llvm::ThreadPriority::Low), +}; + opt EnableClangTidy{ "clang-tidy", cat(Features), @@ -876,6 +891,7 @@ } #endif Opts.BackgroundIndex = EnableBackgroundIndex; + Opts.BackgroundIndexPriority = BackgroundIndexPriority; Opts.ReferencesLimit = ReferencesLimit; auto PAI = createProjectAwareIndex(loadExternalIndex, Sync); if (StaticIdx) {