aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHenning Baldersheim <balder@yahoo-inc.com>2023-03-14 15:57:32 +0100
committerGitHub <noreply@github.com>2023-03-14 15:57:32 +0100
commit4f53639e4c262a9806d05b24114aee40b1f9336b (patch)
tree5e16dbfc400849f83e3b3189a868cc771cd30cbb
parent12a906cd6d29c637cbbada5a881a3636a27885e0 (diff)
parent880d37d8adbd904c2701a8ce35fd4fb9e60da946 (diff)
Merge pull request #26433 from vespa-engine/balder/also-consider-static-cost-of-caches
Balder/also consider static cost of caches
-rw-r--r--searchlib/src/tests/docstore/logdatastore/logdatastore_test.cpp33
-rw-r--r--searchlib/src/vespa/searchlib/docstore/documentstore.cpp5
-rw-r--r--searchlib/src/vespa/searchlib/docstore/visitcache.cpp18
-rw-r--r--searchlib/src/vespa/searchlib/docstore/visitcache.h4
-rw-r--r--vespalib/src/vespa/vespalib/stllike/cache.h19
-rw-r--r--vespalib/src/vespa/vespalib/stllike/cache.hpp14
-rw-r--r--vespalib/src/vespa/vespalib/stllike/lrucache_map.h2
7 files changed, 71 insertions, 24 deletions
diff --git a/searchlib/src/tests/docstore/logdatastore/logdatastore_test.cpp b/searchlib/src/tests/docstore/logdatastore/logdatastore_test.cpp
index 258b4d6518a..eec49043282 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<uint32_t> _expected;
- vespalib::hash_set<uint32_t> _actual;
- bool _allowVisitCaching;
+ VisitCacheStore &_vcs;
+ vespalib::hash_set<uint32_t> _expected;
+ vespalib::hash_set<uint32_t> _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<LogDocumentStore> _datastore;
- std::map<uint32_t, Document::UP> _inserted;
- SerialNum _serial;
+ std::map<uint32_t, Document::UP> _inserted;
+ SerialNum _serial;
};
VisitCacheStore::VerifyVisitor::VerifyVisitor(VisitCacheStore & vcs, std::vector<uint32_t> lids, bool allowCaching)
@@ -560,6 +560,15 @@ 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();
+ constexpr size_t mutex_size = sizeof(std::mutex) * 2 * (113 + 1); // sizeof(std::mutex) is platform dependent
+ EXPECT_EQUAL(74580 + mutex_size, usage.allocatedBytes());
+ EXPECT_EQUAL(952u + mutex_size, 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<DataStoreFileChunkStats>
diff --git a/searchlib/src/vespa/searchlib/docstore/visitcache.cpp b/searchlib/src/vespa/searchlib/docstore/visitcache.cpp
index 60c08c281df..c99bb50d4f8 100644
--- a/searchlib/src/vespa/searchlib/docstore/visitcache.cpp
+++ b/searchlib/src/vespa/searchlib/docstore/visitcache.cpp
@@ -259,4 +259,22 @@ VisitCache::Cache::onRemove(const K & key) {
_id2KeySet.erase(key.getKeys().front());
}
+vespalib::MemoryUsage
+VisitCache::Cache::getStaticMemoryUsage() const {
+ vespalib::MemoryUsage usage = Parent::getStaticMemoryUsage();
+ auto cacheGuard = getGuard();
+ size_t baseSelf = sizeof(_lid2Id) + sizeof(_id2KeySet);
+ usage.incAllocatedBytes(baseSelf);
+ usage.incAllocatedBytes(_lid2Id.capacity() * sizeof(LidUniqueKeySetId::value_type));
+ usage.incAllocatedBytes(_id2KeySet.capacity() * sizeof(IdKeySetMap::value_type));
+ usage.incUsedBytes(baseSelf);
+ usage.incUsedBytes(_lid2Id.size() * sizeof(LidUniqueKeySetId::value_type));
+ usage.incUsedBytes(_id2KeySet.size() * sizeof(IdKeySetMap::value_type));
+ for (const auto & entry: _id2KeySet) {
+ usage.incAllocatedBytes(entry.second.getKeys().capacity() * sizeof(uint32_t));
+ usage.incUsedBytes(entry.second.getKeys().size() * sizeof(uint32_t));
+ }
+ 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<CacheParams> {
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<uint64_t>;
diff --git a/vespalib/src/vespa/vespalib/stllike/cache.h b/vespalib/src/vespa/vespalib/stllike/cache.h
index de4841f2284..907fbdd54c9 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 <vespa/vespalib/util/memoryusage.h>
#include <atomic>
#include <mutex>
@@ -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.
@@ -124,7 +127,7 @@ public:
protected:
using UniqueLock = std::unique_lock<std::mutex>;
- UniqueLock getGuard();
+ UniqueLock getGuard() const;
void invalidate(const UniqueLock & guard, const K & key);
bool hasKey(const UniqueLock & guard, const K & key) const;
private:
@@ -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<size_t> _maxBytes;
std::atomic<size_t> _sizeBytes;
mutable std::atomic<size_t> _hit;
@@ -165,10 +168,10 @@ private:
mutable std::atomic<size_t> _update;
mutable std::atomic<size_t> _invalidate;
mutable std::atomic<size_t> _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..8e449fcfca4 100644
--- a/vespalib/src/vespa/vespalib/stllike/cache.hpp
+++ b/vespalib/src/vespa/vespalib/stllike/cache.hpp
@@ -63,6 +63,18 @@ cache<P>::cache(BackingStore & b, size_t maxBytes) :
{ }
template< typename P >
+MemoryUsage
+cache<P>::getStaticMemoryUsage() const {
+ MemoryUsage usage;
+ auto cacheGuard = getGuard();
+ usage.incAllocatedBytes(sizeof(*this));
+ usage.incAllocatedBytes(Lru::capacity()*sizeof(typename Lru::value_type));
+ usage.incUsedBytes(sizeof(*this));
+ usage.incUsedBytes(Lru::size()*sizeof(typename Lru::value_type));
+ return usage;
+}
+
+template< typename P >
bool
cache<P>::removeOldest(const value_type & v) {
bool remove(Lru::removeOldest(v) || (sizeBytes() >= capacityBytes()));
@@ -74,7 +86,7 @@ cache<P>::removeOldest(const value_type & v) {
template< typename P >
std::unique_lock<std::mutex>
-cache<P>::getGuard() {
+cache<P>::getGuard() const {
return UniqueLock(_hashLock);
}
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<size_t>::max();
public:
using insert_result = typename HashTable::insert_result;
+ using value_type = typename P::value_type;
class iterator {
public: