Index: clang-tools-extra/trunk/clangd/index/FileIndex.h =================================================================== --- clang-tools-extra/trunk/clangd/index/FileIndex.h +++ clang-tools-extra/trunk/clangd/index/FileIndex.h @@ -81,6 +81,9 @@ void findOccurrences(const OccurrencesRequest &Req, llvm::function_ref Callback) const override; + + size_t estimateMemoryUsage() const override; + private: FileSymbols FSymbols; MemIndex Index; Index: clang-tools-extra/trunk/clangd/index/FileIndex.cpp =================================================================== --- clang-tools-extra/trunk/clangd/index/FileIndex.cpp +++ clang-tools-extra/trunk/clangd/index/FileIndex.cpp @@ -118,5 +118,9 @@ log("findOccurrences is not implemented."); } +size_t FileIndex::estimateMemoryUsage() const { + return Index.estimateMemoryUsage(); +} + } // namespace clangd } // namespace clang Index: clang-tools-extra/trunk/clangd/index/Index.h =================================================================== --- clang-tools-extra/trunk/clangd/index/Index.h +++ clang-tools-extra/trunk/clangd/index/Index.h @@ -385,6 +385,12 @@ virtual void findOccurrences( const OccurrencesRequest &Req, llvm::function_ref Callback) const = 0; + + /// Returns estimated size of index (in bytes). + // FIXME(kbobyrev): Currently, this only returns the size of index itself + // excluding the size of actual symbol slab index refers to. We should include + // both. + virtual size_t estimateMemoryUsage() const = 0; }; } // namespace clangd Index: clang-tools-extra/trunk/clangd/index/MemIndex.h =================================================================== --- clang-tools-extra/trunk/clangd/index/MemIndex.h +++ clang-tools-extra/trunk/clangd/index/MemIndex.h @@ -39,7 +39,10 @@ llvm::function_ref Callback) const override; + size_t estimateMemoryUsage() const override; + private: + std::shared_ptr> Symbols; // Index is a set of symbols that are deduplicated by symbol IDs. // FIXME: build smarter index structure. Index: clang-tools-extra/trunk/clangd/index/MemIndex.cpp =================================================================== --- clang-tools-extra/trunk/clangd/index/MemIndex.cpp +++ clang-tools-extra/trunk/clangd/index/MemIndex.cpp @@ -26,6 +26,9 @@ Index = std::move(TempIndex); Symbols = std::move(Syms); // Relase old symbols. } + + vlog("Built MemIndex with estimated memory usage {0} bytes.", + estimateMemoryUsage()); } std::unique_ptr MemIndex::build(SymbolSlab Slab) { @@ -98,5 +101,10 @@ &Snap->Pointers); } +size_t MemIndex::estimateMemoryUsage() const { + std::lock_guard Lock(Mutex); + return Index.getMemorySize(); +} + } // namespace clangd } // namespace clang Index: clang-tools-extra/trunk/clangd/index/Merge.cpp =================================================================== --- clang-tools-extra/trunk/clangd/index/Merge.cpp +++ clang-tools-extra/trunk/clangd/index/Merge.cpp @@ -84,6 +84,10 @@ log("findOccurrences is not implemented."); } + size_t estimateMemoryUsage() const override { + return Dynamic->estimateMemoryUsage() + Static->estimateMemoryUsage(); + } + private: const SymbolIndex *Dynamic, *Static; }; Index: clang-tools-extra/trunk/clangd/index/dex/DexIndex.h =================================================================== --- clang-tools-extra/trunk/clangd/index/dex/DexIndex.h +++ clang-tools-extra/trunk/clangd/index/dex/DexIndex.h @@ -57,7 +57,10 @@ llvm::function_ref Callback) const override; + size_t estimateMemoryUsage() const override; + private: + mutable std::mutex Mutex; std::shared_ptr> Symbols /*GUARDED_BY(Mutex)*/; Index: clang-tools-extra/trunk/clangd/index/dex/DexIndex.cpp =================================================================== --- clang-tools-extra/trunk/clangd/index/dex/DexIndex.cpp +++ clang-tools-extra/trunk/clangd/index/dex/DexIndex.cpp @@ -67,6 +67,9 @@ InvertedIndex = std::move(TempInvertedIndex); SymbolQuality = std::move(TempSymbolQuality); } + + vlog("Built DexIndex with estimated memory usage {0} bytes.", + estimateMemoryUsage()); } std::unique_ptr DexIndex::build(SymbolSlab Slab) { @@ -171,6 +174,20 @@ log("findOccurrences is not implemented."); } +size_t DexIndex::estimateMemoryUsage() const { + std::lock_guard Lock(Mutex); + + size_t Bytes = + LookupTable.size() * sizeof(std::pair); + Bytes += SymbolQuality.size() * sizeof(std::pair); + Bytes += InvertedIndex.size() * sizeof(Token); + + for (const auto &P : InvertedIndex) { + Bytes += P.second.size() * sizeof(DocID); + } + return Bytes; +} + } // namespace dex } // namespace clangd } // namespace clang Index: clang-tools-extra/trunk/unittests/clangd/CodeCompleteTests.cpp =================================================================== --- clang-tools-extra/trunk/unittests/clangd/CodeCompleteTests.cpp +++ clang-tools-extra/trunk/unittests/clangd/CodeCompleteTests.cpp @@ -923,6 +923,10 @@ llvm::function_ref Callback) const override {} + // This is incorrect, but IndexRequestCollector is not an actual index and it + // isn't used in production code. + size_t estimateMemoryUsage() const override { return 0; } + const std::vector allRequests() const { return Requests; } private: