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 @@ -19,6 +19,7 @@ #include "support/Path.h" #include "support/Threading.h" #include "support/ThreadsafeFS.h" +#include "support/Trace.h" #include "clang/Tooling/CompilationDatabase.h" #include "llvm/ADT/StringMap.h" #include "llvm/Support/Threading.h" @@ -172,6 +173,8 @@ return Queue.blockUntilIdleForTest(TimeoutSeconds); } + MemoryTree getMemoryUsage() const; + private: /// Represents the state of a single file when indexing was performed. struct ShardVersion { 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 @@ -414,5 +414,11 @@ return {TUsToIndex.begin(), TUsToIndex.end()}; } +MemoryTree BackgroundIndex::getMemoryUsage() const { + MemoryTree MT; + MT.addChild("symbols", IndexedSymbols.getMemoryUsage(), true); + MT.addChild("index", estimateMemoryUsage()); + return MT; +} } // namespace clangd } // namespace clang diff --git a/clang-tools-extra/clangd/index/FileIndex.h b/clang-tools-extra/clangd/index/FileIndex.h --- a/clang-tools-extra/clangd/index/FileIndex.h +++ b/clang-tools-extra/clangd/index/FileIndex.h @@ -24,6 +24,7 @@ #include "index/Relation.h" #include "index/Serialization.h" #include "index/Symbol.h" +#include "support/MemoryTree.h" #include "support/Path.h" #include "clang/Lex/Preprocessor.h" #include "clang/Tooling/CompilationDatabase.h" @@ -87,6 +88,8 @@ DuplicateHandling DuplicateHandle = DuplicateHandling::PickOne, size_t *Version = nullptr); + MemoryTree getMemoryUsage() const; + private: struct RefSlabAndCountReferences { std::shared_ptr Slab; @@ -116,6 +119,8 @@ /// `indexMainDecls`. void updateMain(PathRef Path, ParsedAST &AST); + MemoryTree getMemoryUsage() const; + private: bool UseDex; // FIXME: this should be always on. bool CollectMainFileRefs; diff --git a/clang-tools-extra/clangd/index/FileIndex.cpp b/clang-tools-extra/clangd/index/FileIndex.cpp --- a/clang-tools-extra/clangd/index/FileIndex.cpp +++ b/clang-tools-extra/clangd/index/FileIndex.cpp @@ -22,6 +22,7 @@ #include "index/SymbolOrigin.h" #include "index/dex/Dex.h" #include "support/Logger.h" +#include "support/MemoryTree.h" #include "support/Path.h" #include "clang/AST/ASTContext.h" #include "clang/Index/IndexingAction.h" @@ -373,6 +374,18 @@ llvm_unreachable("Unknown clangd::IndexType"); } +MemoryTree FileSymbols::getMemoryUsage() const { + MemoryTree MT; + std::lock_guard Lock(Mutex); + for (const auto &SymSlab : SymbolsSnapshot) + MT.addChild(SymSlab.first(), SymSlab.second->bytes()); + for (const auto &RefSlab : RefsSnapshot) + MT.addChild(RefSlab.first(), RefSlab.second.Slab->bytes()); + for (const auto &RelSlab : SymbolsSnapshot) + MT.addChild(RelSlab.first(), RelSlab.second->bytes()); + return MT; +} + FileIndex::FileIndex(bool UseDex, bool CollectMainFileRefs) : MergedIndex(&MainFileIndex, &PreambleIndex), UseDex(UseDex), CollectMainFileRefs(CollectMainFileRefs), @@ -442,5 +455,13 @@ } } +MemoryTree FileIndex::getMemoryUsage() const { + MemoryTree MT; + MT.addChild("preamble_symbols", PreambleSymbols.getMemoryUsage(), true); + MT.addChild("preamble_index", PreambleIndex.estimateMemoryUsage()); + MT.addChild("main_file_symbols", MainFileIndex.estimateMemoryUsage()); + MT.addChild("main_file_index", MainFileSymbols.getMemoryUsage(), true); + return MT; +} } // namespace clangd } // namespace clang