From 28ab766a23032cf35c3a124007775712a4fd033e Mon Sep 17 00:00:00 2001 From: Henning Baldersheim Date: Tue, 14 Mar 2023 12:04:30 +0000 Subject: Wire in and test static memory usage for caches. --- .../docstore/logdatastore/logdatastore_test.cpp | 32 ++++++++++++++-------- .../src/vespa/searchlib/docstore/documentstore.cpp | 5 +++- .../src/vespa/searchlib/docstore/visitcache.cpp | 6 ++++ .../src/vespa/searchlib/docstore/visitcache.h | 4 ++- vespalib/src/vespa/vespalib/stllike/cache.h | 17 +++++++----- vespalib/src/vespa/vespalib/stllike/cache.hpp | 7 +++++ vespalib/src/vespa/vespalib/stllike/lrucache_map.h | 2 +- 7 files changed, 51 insertions(+), 22 deletions(-) diff --git a/searchlib/src/tests/docstore/logdatastore/logdatastore_test.cpp b/searchlib/src/tests/docstore/logdatastore/logdatastore_test.cpp index 687f3624b44..c496472bdfa 100644 --- a/searchlib/src/tests/docstore/logdatastore/logdatastore_test.cpp +++ b/searchlib/src/tests/docstore/logdatastore/logdatastore_test.cpp @@ -497,20 +497,20 @@ private: } bool allowVisitCaching() const override { return _allowVisitCaching; } private: - VisitCacheStore &_vcs; - vespalib::hash_set _expected; - vespalib::hash_set _actual; - bool _allowVisitCaching; + VisitCacheStore &_vcs; + vespalib::hash_set _expected; + vespalib::hash_set _actual; + bool _allowVisitCaching; }; - DirectoryHandler _myDir; - document::DocumentTypeRepo _repo; - LogDocumentStore::Config _config; - DummyFileHeaderContext _fileHeaderContext; - vespalib::ThreadStackExecutor _executor; - MyTlSyncer _tlSyncer; + DirectoryHandler _myDir; + document::DocumentTypeRepo _repo; + LogDocumentStore::Config _config; + DummyFileHeaderContext _fileHeaderContext; + vespalib::ThreadStackExecutor _executor; + MyTlSyncer _tlSyncer; std::unique_ptr _datastore; - std::map _inserted; - SerialNum _serial; + std::map _inserted; + SerialNum _serial; }; VisitCacheStore::VerifyVisitor::VerifyVisitor(VisitCacheStore & vcs, std::vector lids, bool allowCaching) @@ -560,6 +560,14 @@ verifyCacheStats(CacheStats cs, size_t hits, size_t misses, size_t elements, siz EXPECT_GREATER_EQUAL(memory_used+20, cs.memory_used); } +TEST("Control static memory usage") { + VisitCacheStore vcs(DocumentStore::Config::UpdateStrategy::UPDATE); + IDocumentStore &ds = vcs.getStore(); + vespalib::MemoryUsage usage = ds.getMemoryUsage(); + EXPECT_EQUAL(74116u, usage.allocatedBytes()); + EXPECT_EQUAL(392u, usage.usedBytes()); +} + TEST("test the update cache strategy") { VisitCacheStore vcs(DocumentStore::Config::UpdateStrategy::UPDATE); IDocumentStore & ds = vcs.getStore(); diff --git a/searchlib/src/vespa/searchlib/docstore/documentstore.cpp b/searchlib/src/vespa/searchlib/docstore/documentstore.cpp index 1e04b7c61db..368dd31678d 100644 --- a/searchlib/src/vespa/searchlib/docstore/documentstore.cpp +++ b/searchlib/src/vespa/searchlib/docstore/documentstore.cpp @@ -428,7 +428,10 @@ DocumentStore::getStorageStats() const vespalib::MemoryUsage DocumentStore::getMemoryUsage() const { - return _backingStore.getMemoryUsage(); + vespalib::MemoryUsage usage = _backingStore.getMemoryUsage(); + usage.merge(_cache->getStaticMemoryUsage()); + usage.merge(_visitCache->getStaticMemoryUsage()); + return usage; } std::vector diff --git a/searchlib/src/vespa/searchlib/docstore/visitcache.cpp b/searchlib/src/vespa/searchlib/docstore/visitcache.cpp index 60c08c281df..d7c1a400b1e 100644 --- a/searchlib/src/vespa/searchlib/docstore/visitcache.cpp +++ b/searchlib/src/vespa/searchlib/docstore/visitcache.cpp @@ -259,4 +259,10 @@ VisitCache::Cache::onRemove(const K & key) { _id2KeySet.erase(key.getKeys().front()); } +vespalib::MemoryUsage +VisitCache::Cache::getStaticMemoryUsage() const { + vespalib::MemoryUsage usage = Parent::getStaticMemoryUsage(); + return usage; +} + } diff --git a/searchlib/src/vespa/searchlib/docstore/visitcache.h b/searchlib/src/vespa/searchlib/docstore/visitcache.h index 9a07c0ceab2..baa594b8d28 100644 --- a/searchlib/src/vespa/searchlib/docstore/visitcache.h +++ b/searchlib/src/vespa/searchlib/docstore/visitcache.h @@ -104,6 +104,7 @@ public: void invalidate(uint32_t key) { remove(key); } vespalib::CacheStats getCacheStats() const; + vespalib::MemoryUsage getStaticMemoryUsage() const { return _cache->getStaticMemoryUsage(); } void reconfigure(size_t cacheSize, CompressionConfig compression); private: /** @@ -144,9 +145,10 @@ private: class Cache : public vespalib::cache { public: Cache(BackingStore & b, size_t maxBytes); - ~Cache(); + ~Cache() override; CompressedBlobSet readSet(const KeySet & keys); void removeKey(uint32_t key); + vespalib::MemoryUsage getStaticMemoryUsage() const override; private: void locateAndInvalidateOtherSubsets(const UniqueLock & cacheGuard, const KeySet & keys); using IdSet = vespalib::hash_set; diff --git a/vespalib/src/vespa/vespalib/stllike/cache.h b/vespalib/src/vespa/vespalib/stllike/cache.h index de4841f2284..b823d0001ea 100644 --- a/vespalib/src/vespa/vespalib/stllike/cache.h +++ b/vespalib/src/vespa/vespalib/stllike/cache.h @@ -2,6 +2,7 @@ #pragma once #include "lrucache_map.h" +#include #include #include @@ -63,7 +64,7 @@ public: * @maxBytes is the maximum limit of bytes the store can hold, before eviction starts. */ cache(BackingStore & b, size_t maxBytes); - ~cache(); + ~cache() override; /** * Can be used for controlling max number of elements. */ @@ -81,6 +82,8 @@ public: size_t sizeBytes() const { return _sizeBytes.load(std::memory_order_relaxed); } bool empty() const { return Lru::empty(); } + virtual MemoryUsage getStaticMemoryUsage() const; + /** * This simply erases the object. * This will also erase from backing store. @@ -151,9 +154,9 @@ private: v.store(v.load(std::memory_order_relaxed) + 1, std::memory_order_relaxed); } - Hash _hasher; - SizeK _sizeK; - SizeV _sizeV; + Hash _hasher; + SizeK _sizeK; + SizeV _sizeV; std::atomic _maxBytes; std::atomic _sizeBytes; mutable std::atomic _hit; @@ -165,10 +168,10 @@ private: mutable std::atomic _update; mutable std::atomic _invalidate; mutable std::atomic _lookup; - BackingStore & _store; - mutable std::mutex _hashLock; + BackingStore & _store; + mutable std::mutex _hashLock; /// Striped locks that can be used for having a locked access to the backing store. - std::mutex _addLocks[113]; + std::mutex _addLocks[113]; }; } diff --git a/vespalib/src/vespa/vespalib/stllike/cache.hpp b/vespalib/src/vespa/vespalib/stllike/cache.hpp index bb05d564f1f..9abd4a5e91f 100644 --- a/vespalib/src/vespa/vespalib/stllike/cache.hpp +++ b/vespalib/src/vespa/vespalib/stllike/cache.hpp @@ -62,6 +62,13 @@ cache

::cache(BackingStore & b, size_t maxBytes) : _store(b) { } +template< typename P > +MemoryUsage +cache

::getStaticMemoryUsage() const { + MemoryUsage usage; + return usage; +} + template< typename P > bool cache

::removeOldest(const value_type & v) { diff --git a/vespalib/src/vespa/vespalib/stllike/lrucache_map.h b/vespalib/src/vespa/vespalib/stllike/lrucache_map.h index 6758b62d2c2..92667171e31 100644 --- a/vespalib/src/vespa/vespalib/stllike/lrucache_map.h +++ b/vespalib/src/vespa/vespalib/stllike/lrucache_map.h @@ -46,7 +46,6 @@ private: using HashTable = typename P::HashTable; using V = typename P::Value; using K = typename P::Key; - using value_type = typename P::value_type; using LV = typename P::LV; using internal_iterator = typename HashTable::iterator; using next_t = typename HashTable::next_t; @@ -55,6 +54,7 @@ protected: static constexpr size_t UNLIMITED = std::numeric_limits::max(); public: using insert_result = typename HashTable::insert_result; + using value_type = typename P::value_type; class iterator { public: -- cgit v1.2.3