aboutsummaryrefslogtreecommitdiff
path: root/src/zenserver/cache/cachedisklayer.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/zenserver/cache/cachedisklayer.cpp')
-rw-r--r--src/zenserver/cache/cachedisklayer.cpp63
1 files changed, 48 insertions, 15 deletions
diff --git a/src/zenserver/cache/cachedisklayer.cpp b/src/zenserver/cache/cachedisklayer.cpp
index 2c344dd1d..700529443 100644
--- a/src/zenserver/cache/cachedisklayer.cpp
+++ b/src/zenserver/cache/cachedisklayer.cpp
@@ -209,6 +209,9 @@ namespace {
zen::Sleep(100);
} while (true);
}
+
+ uint64_t EstimateMemCachePayloadMemory(uint64_t PayloadSize) { return 8u + 32u + RoundUp(PayloadSize, 8u); }
+
} // namespace
namespace fs = std::filesystem;
@@ -655,6 +658,9 @@ BucketManifestSerializer::WriteSidecarFile(const std::filesystem::path&
//////////////////////////////////////////////////////////////////////////
+static const float IndexMinLoadFactor = 0.2f;
+static const float IndexMaxLoadFactor = 0.7f;
+
ZenCacheDiskLayer::CacheBucket::CacheBucket(GcManager& Gc,
std::atomic_uint64_t& OuterCacheMemoryUsage,
std::string BucketName,
@@ -665,6 +671,9 @@ ZenCacheDiskLayer::CacheBucket::CacheBucket(GcManager& Gc,
, m_Configuration(Config)
, m_BucketId(Oid::Zero)
{
+ m_Index.min_load_factor(IndexMinLoadFactor);
+ m_Index.max_load_factor(IndexMaxLoadFactor);
+
if (m_BucketName.starts_with(std::string_view("legacy")) || m_BucketName.ends_with(std::string_view("shadermap")))
{
const uint64_t LegacyOverrideSize = 16 * 1024 * 1024;
@@ -1289,14 +1298,26 @@ ZenCacheDiskLayer::CacheBucket::MemCacheTrim(GcClock::TimePoint ExpireTime)
GcClock::Tick ExpireTicks = ExpireTime.time_since_epoch().count();
RwLock::ExclusiveLockScope _(m_IndexLock);
+ if (m_MemCachedPayloads.empty())
+ {
+ return;
+ }
for (const auto& Kv : m_Index)
{
- if (m_AccessTimes[Kv.second] < ExpireTicks)
+ size_t Index = Kv.second;
+ BucketPayload& Payload = m_Payloads[Index];
+ if (!Payload.MemCached)
+ {
+ continue;
+ }
+ if (m_AccessTimes[Index] < ExpireTicks)
{
- BucketPayload& Payload = m_Payloads[Kv.second];
RemoveMemCachedData(Payload);
}
}
+ m_MemCachedPayloads.shrink_to_fit();
+ m_FreeMemCachedPayloads.shrink_to_fit();
+ m_FreeMetaDatas.shrink_to_fit();
}
void
@@ -1305,6 +1326,10 @@ ZenCacheDiskLayer::CacheBucket::GetUsageByAccess(GcClock::TimePoint TickStart,
std::vector<uint64_t>& InOutUsageSlots)
{
RwLock::SharedLockScope _(m_IndexLock);
+ if (m_MemCachedPayloads.empty())
+ {
+ return;
+ }
for (const auto& It : m_Index)
{
size_t Index = It.second;
@@ -1929,10 +1954,9 @@ ZenCacheDiskLayer::CacheBucket::GatherReferences(GcContext& GcCtx)
for (const auto& Entry : StructuredItemsWithUnknownAttachments)
{
- const IoHash& Key = Entry.first;
- size_t PayloadIndex = Entry.second;
- BucketPayload& Payload = Payloads[PayloadIndex];
- const DiskLocation& Loc = Payload.Location;
+ const IoHash& Key = Entry.first;
+ BucketPayload& Payload = Payloads[Entry.second];
+ const DiskLocation& Loc = Payload.Location;
{
IoBuffer Buffer;
if (Loc.IsFlagSet(DiskLocation::kStandaloneFile))
@@ -1955,7 +1979,7 @@ ZenCacheDiskLayer::CacheBucket::GatherReferences(GcContext& GcCtx)
#endif // CALCULATE_BLOCKING_TIME
if (auto It = m_Index.find(Key); It != m_Index.end())
{
- const BucketPayload& CachedPayload = Payloads[PayloadIndex];
+ const BucketPayload& CachedPayload = Payloads[It->second];
if (CachedPayload.MemCached)
{
Buffer = m_MemCachedPayloads[CachedPayload.MemCached];
@@ -2630,7 +2654,7 @@ ZenCacheDiskLayer::CacheBucket::SetMemCachedData(BucketPayload& Payload, IoBuffe
{
Payload.MemCached = MemCachedIndex(gsl::narrow<uint32_t>(m_MemCachedPayloads.size()));
m_MemCachedPayloads.push_back(MemCachedData);
- AddMemCacheUsage(PayloadSize);
+ AddMemCacheUsage(EstimateMemCachePayloadMemory(PayloadSize));
m_MemoryWriteCount++;
}
}
@@ -2639,7 +2663,7 @@ ZenCacheDiskLayer::CacheBucket::SetMemCachedData(BucketPayload& Payload, IoBuffe
Payload.MemCached = m_FreeMemCachedPayloads.back();
m_FreeMemCachedPayloads.pop_back();
m_MemCachedPayloads[Payload.MemCached] = MemCachedData;
- AddMemCacheUsage(PayloadSize);
+ AddMemCacheUsage(EstimateMemCachePayloadMemory(PayloadSize));
m_MemoryWriteCount++;
}
}
@@ -2650,7 +2674,7 @@ ZenCacheDiskLayer::CacheBucket::RemoveMemCachedData(BucketPayload& Payload)
if (Payload.MemCached)
{
size_t PayloadSize = m_MemCachedPayloads[Payload.MemCached].GetSize();
- RemoveMemCacheUsage(PayloadSize);
+ RemoveMemCacheUsage(EstimateMemCachePayloadMemory(PayloadSize));
m_MemCachedPayloads[Payload.MemCached] = IoBuffer{};
m_FreeMemCachedPayloads.push_back(Payload.MemCached);
Payload.MemCached = {};
@@ -2844,7 +2868,8 @@ public:
m_Bucket.m_IndexLock.WithExclusiveLock([&]() { m_Bucket.m_UpdatedKeys = std::make_unique<HashSet>(); });
auto __ = MakeGuard([&]() { m_Bucket.m_IndexLock.WithExclusiveLock([&]() { m_Bucket.m_UpdatedKeys.reset(); }); });
- std::unordered_map<uint32_t, uint64_t> BlockUsage;
+ size_t InlineEntryCount = 0;
+ BlockStore::BlockUsageMap BlockUsage;
{
RwLock::SharedLockScope ___(m_Bucket.m_IndexLock);
for (const auto& Entry : m_Bucket.m_Index)
@@ -2857,15 +2882,17 @@ public:
{
continue;
}
+ InlineEntryCount++;
uint32_t BlockIndex = Loc.Location.BlockLocation.GetBlockIndex();
uint64_t ChunkSize = RoundUp(Loc.Size(), m_Bucket.m_Configuration.PayloadAlignment);
if (auto It = BlockUsage.find(BlockIndex); It != BlockUsage.end())
{
- It->second += ChunkSize;
+ It->second.EntryCount++;
+ It->second.DiskUsage += ChunkSize;
}
else
{
- BlockUsage.insert_or_assign(BlockIndex, ChunkSize);
+ BlockUsage.insert_or_assign(BlockIndex, BlockStore::BlockUsageInfo{.DiskUsage = ChunkSize, .EntryCount = 1});
}
}
}
@@ -2873,8 +2900,9 @@ public:
{
BlockStoreCompactState BlockCompactState;
std::vector<IoHash> BlockCompactStateKeys;
+ BlockCompactStateKeys.reserve(InlineEntryCount);
- std::vector<uint32_t> BlocksToCompact =
+ BlockStore::BlockEntryCountMap BlocksToCompact =
m_Bucket.m_BlockStore.GetBlocksToCompact(BlockUsage, Ctx.Settings.CompactBlockUsageThresholdPercent);
BlockCompactState.IncludeBlocks(BlocksToCompact);
@@ -3149,7 +3177,7 @@ public:
uint32_t Size;
};
std::vector<std::vector<InlineEntry>> EntriesPerBlock;
-
+ size_t UpdateCount = 0;
{
RwLock::SharedLockScope IndexLock(m_CacheBucket.m_IndexLock);
for (const auto& Entry : m_CacheBucket.m_Index)
@@ -3174,6 +3202,7 @@ public:
{
continue;
}
+ UpdateCount++;
const IoHash& Key = Entry.first;
if (Loc.IsFlagSet(DiskLocation::kStandaloneFile))
{
@@ -3200,6 +3229,8 @@ public:
}
}
+ UpdateKeys.reserve(UpdateCount);
+
for (auto It : BlockIndexToEntriesPerBlockIndex)
{
uint32_t BlockIndex = It.first;
@@ -3652,6 +3683,8 @@ ZenCacheDiskLayer::CacheBucket::CompactState(std::vector<BucketPayload>& Payloa
FirstReferenceIndex.reserve(EntryCount);
}
Index.reserve(EntryCount);
+ Index.min_load_factor(IndexMinLoadFactor);
+ Index.max_load_factor(IndexMaxLoadFactor);
for (auto It : m_Index)
{
PayloadIndex EntryIndex = PayloadIndex(Payloads.size());