| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_BACKGROUND_H |
| 10 |
#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_BACKGROUND_H |
| 11 |
|
| 12 |
#include "GlobalCompilationDatabase.h" |
| 13 |
#include "SourceCode.h" |
| 14 |
#include "index/BackgroundRebuild.h" |
| 15 |
#include "index/FileIndex.h" |
| 16 |
#include "index/Index.h" |
| 17 |
#include "index/Serialization.h" |
| 18 |
#include "support/Context.h" |
| 19 |
#include "support/MemoryTree.h" |
| 20 |
#include "support/Path.h" |
| 21 |
#include "support/Threading.h" |
| 22 |
#include "support/ThreadsafeFS.h" |
| 23 |
#include "clang/Tooling/CompilationDatabase.h" |
| 24 |
#include "llvm/ADT/StringMap.h" |
| 25 |
#include "llvm/Support/Threading.h" |
| 26 |
#include <atomic> |
| 27 |
#include <condition_variable> |
| 28 |
#include <deque> |
| 29 |
#include <mutex> |
| 30 |
#include <optional> |
| 31 |
#include <queue> |
| 32 |
#include <string> |
| 33 |
#include <thread> |
| 34 |
#include <vector> |
| 35 |
|
| 36 |
namespace clang { |
| 37 |
namespace clangd { |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
class BackgroundIndexStorage { |
| 42 |
public: |
| 43 |
virtual ~BackgroundIndexStorage() = default; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
virtual llvm::Error storeShard(llvm::StringRef ShardIdentifier, |
| 48 |
IndexFileOut Shard) const = 0; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
virtual std::unique_ptr<IndexFileIn> |
| 53 |
loadShard(llvm::StringRef ShardIdentifier) const = 0; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
using Factory = llvm::unique_function<BackgroundIndexStorage *(PathRef)>; |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
static Factory createDiskBackedStorageFactory( |
| 65 |
std::function<std::optional<ProjectInfo>(PathRef)> GetProjectInfo); |
| 66 |
}; |
| 67 |
|
| 68 |
|
| 69 |
class BackgroundQueue { |
| 70 |
public: |
| 71 |
|
| 72 |
struct Task { |
| 73 |
explicit Task(std::function<void()> Run) : Run(std::move(Run)) {} |
| 74 |
|
| 75 |
std::function<void()> Run; |
| 76 |
llvm::ThreadPriority ThreadPri = llvm::ThreadPriority::Low; |
| 77 |
unsigned QueuePri = 0; |
| 78 |
std::string Tag; |
| 79 |
uint64_t Key = 0; |
| 80 |
|
| 81 |
|
| 82 |
bool operator<(const Task &O) const { return QueuePri < O.QueuePri; } |
| 83 |
}; |
| 84 |
|
| 85 |
|
| 86 |
struct Stats { |
| 87 |
unsigned Enqueued = 0; |
| 88 |
unsigned Active = 0; |
| 89 |
unsigned Completed = 0; |
| 90 |
unsigned LastIdle = 0; |
| 91 |
}; |
| 92 |
|
| 93 |
BackgroundQueue(std::function<void(Stats)> OnProgress = nullptr) |
| 94 |
: OnProgress(OnProgress) {} |
| 95 |
|
| 96 |
|
| 97 |
void push(Task); |
| 98 |
void append(std::vector<Task>); |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
void boost(llvm::StringRef Tag, unsigned NewPriority); |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
void work(std::function<void()> OnIdle = nullptr); |
| 107 |
|
| 108 |
|
| 109 |
void stop(); |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
static void preventThreadStarvationInTests(); |
| 114 |
[[nodiscard]] bool |
| 115 |
blockUntilIdleForTest(std::optional<double> TimeoutSeconds); |
| 116 |
|
| 117 |
private: |
| 118 |
void notifyProgress() const; |
| 119 |
bool adjust(Task &T); |
| 120 |
|
| 121 |
std::mutex Mu; |
| 122 |
Stats Stat; |
| 123 |
std::condition_variable CV; |
| 124 |
bool ShouldStop = false; |
| 125 |
std::vector<Task> Queue; |
| 126 |
llvm::StringMap<unsigned> Boosts; |
| 127 |
std::function<void(Stats)> OnProgress; |
| 128 |
llvm::DenseSet<uint64_t> SeenKeys; |
| 129 |
}; |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
class BackgroundIndex : public SwapIndex { |
| 135 |
public: |
| 136 |
struct Options { |
| 137 |
|
| 138 |
|
| 139 |
size_t ThreadPoolSize = 4; |
| 140 |
|
| 141 |
llvm::ThreadPriority IndexingPriority = llvm::ThreadPriority::Low; |
| 142 |
|
| 143 |
std::function<void(BackgroundQueue::Stats)> OnProgress = nullptr; |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
std::function<Context(PathRef)> ContextProvider = nullptr; |
| 148 |
}; |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
BackgroundIndex(const ThreadsafeFS &, const GlobalCompilationDatabase &CDB, |
| 153 |
BackgroundIndexStorage::Factory IndexStorageFactory, |
| 154 |
Options Opts); |
| 155 |
~BackgroundIndex(); |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
void enqueue(const std::vector<std::string> &ChangedFiles) { |
| 161 |
Queue.push(changedFilesTask(ChangedFiles)); |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
void boostRelated(llvm::StringRef Path); |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
void stop() { |
| 171 |
Rebuilder.shutdown(); |
| 172 |
Queue.stop(); |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
[[nodiscard]] bool |
| 177 |
blockUntilIdleForTest(std::optional<double> TimeoutSeconds = 10) { |
| 178 |
return Queue.blockUntilIdleForTest(TimeoutSeconds); |
| 179 |
} |
| 180 |
|
| 181 |
void profile(MemoryTree &MT) const; |
| 182 |
|
| 183 |
private: |
| 184 |
|
| 185 |
struct ShardVersion { |
| 186 |
FileDigest Digest{{0}}; |
| 187 |
bool HadErrors = false; |
| 188 |
}; |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
void update(llvm::StringRef MainFile, IndexFileIn Index, |
| 194 |
const llvm::StringMap<ShardVersion> &ShardVersionsSnapshot, |
| 195 |
bool HadErrors); |
| 196 |
|
| 197 |
|
| 198 |
const ThreadsafeFS &TFS; |
| 199 |
const GlobalCompilationDatabase &CDB; |
| 200 |
llvm::ThreadPriority IndexingPriority; |
| 201 |
std::function<Context(PathRef)> ContextProvider; |
| 202 |
|
| 203 |
llvm::Error index(tooling::CompileCommand); |
| 204 |
|
| 205 |
FileSymbols IndexedSymbols; |
| 206 |
BackgroundIndexRebuilder Rebuilder; |
| 207 |
llvm::StringMap<ShardVersion> ShardVersions; |
| 208 |
std::mutex ShardVersionsMu; |
| 209 |
|
| 210 |
BackgroundIndexStorage::Factory IndexStorageFactory; |
| 211 |
|
| 212 |
std::vector<std::string> loadProject(std::vector<std::string> MainFiles); |
| 213 |
|
| 214 |
BackgroundQueue::Task |
| 215 |
changedFilesTask(const std::vector<std::string> &ChangedFiles); |
| 216 |
BackgroundQueue::Task indexFileTask(std::string Path); |
| 217 |
|
| 218 |
|
| 219 |
enum QueuePriority { |
| 220 |
IndexFile, |
| 221 |
IndexBoostedFile, |
| 222 |
LoadShards, |
| 223 |
}; |
| 224 |
BackgroundQueue Queue; |
| 225 |
AsyncTaskRunner ThreadPool; |
| 226 |
GlobalCompilationDatabase::CommandChanged::Subscription CommandsChanged; |
| 227 |
}; |
| 228 |
|
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
#endif |