xref: /llvm-project/clang-tools-extra/clangd/index/BackgroundRebuild.h (revision 4d006520b8c0cc3a52913b4665bf741c737e5592)
12f760c44SSam McCall //===--- BackgroundIndexRebuild.h - when to rebuild the bg index--*- C++-*-===//
22f760c44SSam McCall //
32f760c44SSam McCall // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42f760c44SSam McCall // See https://llvm.org/LICENSE.txt for license information.
52f760c44SSam McCall // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
62f760c44SSam McCall //
72f760c44SSam McCall //===----------------------------------------------------------------------===//
82f760c44SSam McCall //
92f760c44SSam McCall // This file contains an implementation detail of the background indexer
102f760c44SSam McCall // (Background.h), which is exposed for testing.
112f760c44SSam McCall //
122f760c44SSam McCall //===----------------------------------------------------------------------===//
132f760c44SSam McCall 
14*5bd643d3SChristian Kühnel #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_BACKGROUNDREBUILD_H
15*5bd643d3SChristian Kühnel #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_BACKGROUNDREBUILD_H
162f760c44SSam McCall 
172f760c44SSam McCall #include "index/FileIndex.h"
182f760c44SSam McCall #include "index/Index.h"
1991e5f4b4SKadir Cetinkaya #include <cstddef>
202f760c44SSam McCall 
212f760c44SSam McCall namespace clang {
222f760c44SSam McCall namespace clangd {
232f760c44SSam McCall 
242f760c44SSam McCall // The BackgroundIndexRebuilder builds the serving data structures periodically
252f760c44SSam McCall // in response to events in the background indexer. The goal is to ensure the
262f760c44SSam McCall // served data stays fairly fresh, without wasting lots of CPU rebuilding it
272f760c44SSam McCall // often.
282f760c44SSam McCall //
292f760c44SSam McCall // The index is always built after a set of shards are loaded from disk.
302f760c44SSam McCall // This happens when clangd discovers a compilation database that we've
312f760c44SSam McCall // previously built an index for. It's a fairly fast process that yields lots
322f760c44SSam McCall // of data, so we wait to get all of it.
332f760c44SSam McCall //
342f760c44SSam McCall // The index is built after indexing a few translation units, if it wasn't built
352f760c44SSam McCall // already. This ensures quick startup if there's no existing index.
362f760c44SSam McCall // Waiting for a few random TUs yields coverage of the most common headers.
372f760c44SSam McCall //
382f760c44SSam McCall // The index is rebuilt every N TUs, to keep if fresh as files are indexed.
392f760c44SSam McCall //
402f760c44SSam McCall // The index is rebuilt every time the queue goes idle, if it's stale.
412f760c44SSam McCall //
422f760c44SSam McCall // All methods are threadsafe. They're called after FileSymbols is updated
432f760c44SSam McCall // etc. Without external locking, the rebuilt index may include more updates
442f760c44SSam McCall // than intended, which is fine.
452f760c44SSam McCall //
462f760c44SSam McCall // This class is exposed in the header so it can be tested.
472f760c44SSam McCall class BackgroundIndexRebuilder {
482f760c44SSam McCall public:
BackgroundIndexRebuilder(SwapIndex * Target,FileSymbols * Source,unsigned Threads)4906377ae2SSam McCall   BackgroundIndexRebuilder(SwapIndex *Target, FileSymbols *Source,
5006377ae2SSam McCall                            unsigned Threads)
513c5745cbSSam McCall       : TUsBeforeFirstBuild(Threads), Target(Target), Source(Source) {}
522f760c44SSam McCall 
532f760c44SSam McCall   // Called to indicate a TU has been indexed.
542f760c44SSam McCall   // May rebuild, if enough TUs have been indexed.
552f760c44SSam McCall   void indexedTU();
562f760c44SSam McCall   // Called to indicate that all worker threads are idle.
572f760c44SSam McCall   // May reindex, if the index is not up to date.
582f760c44SSam McCall   void idle();
592f760c44SSam McCall   // Called to indicate we're going to load a batch of shards from disk.
602f760c44SSam McCall   // startLoading() and doneLoading() must be paired, but multiple loading
612f760c44SSam McCall   // sessions may happen concurrently.
622f760c44SSam McCall   void startLoading();
632f760c44SSam McCall   // Called to indicate some shards were actually loaded from disk.
6491e5f4b4SKadir Cetinkaya   void loadedShard(size_t ShardCount);
652f760c44SSam McCall   // Called to indicate we're finished loading shards from disk.
662f760c44SSam McCall   // May rebuild (if any were loaded).
672f760c44SSam McCall   void doneLoading();
682f760c44SSam McCall 
692f760c44SSam McCall   // Ensures we won't start any more rebuilds.
702f760c44SSam McCall   void shutdown();
712f760c44SSam McCall 
723c5745cbSSam McCall   // Thresholds for rebuilding as TUs get indexed. Exposed for testing.
7306377ae2SSam McCall   const unsigned TUsBeforeFirstBuild; // Typically one per worker thread.
7406377ae2SSam McCall   const unsigned TUsBeforeRebuild = 100;
7506377ae2SSam McCall 
762f760c44SSam McCall private:
772f760c44SSam McCall   // Run Check under the lock, and rebuild if it returns true.
782f760c44SSam McCall   void maybeRebuild(const char *Reason, std::function<bool()> Check);
792f760c44SSam McCall   bool enoughTUsToRebuild() const;
802f760c44SSam McCall 
812f760c44SSam McCall   // All transient state is guarded by the mutex.
822f760c44SSam McCall   std::mutex Mu;
832f760c44SSam McCall   bool ShouldStop = false;
842f760c44SSam McCall   // Index builds are versioned. ActiveVersion chases StartedVersion.
852f760c44SSam McCall   unsigned StartedVersion = 0;
862f760c44SSam McCall   unsigned ActiveVersion = 0;
872f760c44SSam McCall   // How many TUs have we indexed so far since startup?
882f760c44SSam McCall   unsigned IndexedTUs = 0;
892f760c44SSam McCall   unsigned IndexedTUsAtLastRebuild = 0;
902f760c44SSam McCall   // Are we loading shards? May be multiple concurrent sessions.
912f760c44SSam McCall   unsigned Loading = 0;
9291e5f4b4SKadir Cetinkaya   unsigned LoadedShards; // In the current loading session.
932f760c44SSam McCall 
942f760c44SSam McCall   SwapIndex *Target;
952f760c44SSam McCall   FileSymbols *Source;
962f760c44SSam McCall };
972f760c44SSam McCall 
982f760c44SSam McCall } // namespace clangd
992f760c44SSam McCall } // namespace clang
1002f760c44SSam McCall 
1012f760c44SSam McCall #endif
102