1 //===-- BackgroundRebuild.cpp - when to rebuild thei background index -----===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "index/BackgroundRebuild.h"
10 #include "Compiler.h"
11 #include "Headers.h"
12 #include "ParsedAST.h"
13 #include "SourceCode.h"
14 #include "Symbol.h"
15 #include "URI.h"
16 #include "index/FileIndex.h"
17 #include "index/IndexAction.h"
18 #include "index/MemIndex.h"
19 #include "index/Ref.h"
20 #include "index/Relation.h"
21 #include "index/Serialization.h"
22 #include "index/SymbolCollector.h"
23 #include "support/Logger.h"
24 #include "support/Path.h"
25 #include "support/Threading.h"
26 #include "support/Trace.h"
27 #include "clang/Basic/SourceLocation.h"
28 #include "clang/Basic/SourceManager.h"
29 #include "llvm/ADT/Hashing.h"
30 #include "llvm/ADT/STLExtras.h"
31 #include "llvm/ADT/ScopeExit.h"
32 #include "llvm/ADT/StringMap.h"
33 #include "llvm/ADT/StringRef.h"
34 #include "llvm/ADT/StringSet.h"
35 #include "llvm/Support/Error.h"
36 #include "llvm/Support/Threading.h"
37 
38 #include <atomic>
39 #include <chrono>
40 #include <condition_variable>
41 #include <memory>
42 #include <mutex>
43 #include <numeric>
44 #include <queue>
45 #include <random>
46 #include <string>
47 #include <thread>
48 
49 namespace clang {
50 namespace clangd {
51 
enoughTUsToRebuild() const52 bool BackgroundIndexRebuilder::enoughTUsToRebuild() const {
53   if (!ActiveVersion)                         // never built
54     return IndexedTUs == TUsBeforeFirstBuild; // use low threshold
55   // rebuild if we've reached the (higher) threshold
56   return IndexedTUs >= IndexedTUsAtLastRebuild + TUsBeforeRebuild;
57 }
58 
indexedTU()59 void BackgroundIndexRebuilder::indexedTU() {
60   maybeRebuild("after indexing enough files", [this] {
61     ++IndexedTUs;
62     if (Loading)
63       return false;                      // rebuild once loading finishes
64     if (ActiveVersion != StartedVersion) // currently building
65       return false;                      // no urgency, avoid overlapping builds
66     return enoughTUsToRebuild();
67   });
68 }
69 
idle()70 void BackgroundIndexRebuilder::idle() {
71   maybeRebuild("when background indexer is idle", [this] {
72     // rebuild if there's anything new in the index.
73     // (even if currently rebuilding! this ensures eventual completeness)
74     return IndexedTUs > IndexedTUsAtLastRebuild;
75   });
76 }
77 
startLoading()78 void BackgroundIndexRebuilder::startLoading() {
79   std::lock_guard<std::mutex> Lock(Mu);
80   if (!Loading)
81     LoadedShards = 0;
82   ++Loading;
83 }
loadedShard(size_t ShardCount)84 void BackgroundIndexRebuilder::loadedShard(size_t ShardCount) {
85   std::lock_guard<std::mutex> Lock(Mu);
86   assert(Loading);
87   LoadedShards += ShardCount;
88 }
doneLoading()89 void BackgroundIndexRebuilder::doneLoading() {
90   maybeRebuild("after loading index from disk", [this] {
91     assert(Loading);
92     --Loading;
93     if (Loading)    // was loading multiple batches concurrently
94       return false; // rebuild once the last batch is done.
95     // Rebuild if we loaded any shards, or if we stopped an indexedTU rebuild.
96     return LoadedShards > 0 || enoughTUsToRebuild();
97   });
98 }
99 
shutdown()100 void BackgroundIndexRebuilder::shutdown() {
101   std::lock_guard<std::mutex> Lock(Mu);
102   ShouldStop = true;
103 }
104 
maybeRebuild(const char * Reason,std::function<bool ()> Check)105 void BackgroundIndexRebuilder::maybeRebuild(const char *Reason,
106                                             std::function<bool()> Check) {
107   unsigned BuildVersion = 0;
108   {
109     std::lock_guard<std::mutex> Lock(Mu);
110     if (!ShouldStop && Check()) {
111       BuildVersion = ++StartedVersion;
112       IndexedTUsAtLastRebuild = IndexedTUs;
113     }
114   }
115   if (BuildVersion) {
116     std::unique_ptr<SymbolIndex> NewIndex;
117     {
118       vlog("BackgroundIndex: building version {0} {1}", BuildVersion, Reason);
119       trace::Span Tracer("RebuildBackgroundIndex");
120       SPAN_ATTACH(Tracer, "reason", Reason);
121       NewIndex = Source->buildIndex(IndexType::Heavy, DuplicateHandling::Merge);
122     }
123     {
124       std::lock_guard<std::mutex> Lock(Mu);
125       // Guard against rebuild finishing in the wrong order.
126       if (BuildVersion > ActiveVersion) {
127         ActiveVersion = BuildVersion;
128         vlog("BackgroundIndex: serving version {0} ({1} bytes)", BuildVersion,
129              NewIndex->estimateMemoryUsage());
130         Target->reset(std::move(NewIndex));
131       }
132     }
133   }
134 }
135 
136 } // namespace clangd
137 } // namespace clang
138