Skip to content

Commit 2cacc99

Browse files
victoria-mcgrathvinser52
authored andcommitted
Converted nvmCacheState_ to std::optional to simplify NVM cache state handling when NVM cache state is not enabled
1 parent ab752e8 commit 2cacc99

File tree

3 files changed

+31
-18
lines changed

3 files changed

+31
-18
lines changed

cachelib/allocator/CacheAllocator-inl.h

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,7 @@ CacheAllocator<CacheTrait>::CacheAllocator(Config config)
4646
[this](Item* it) -> ItemHandle { return acquire(it); })),
4747
chainedItemLocks_(config_.chainedItemsLockPower,
4848
std::make_shared<MurmurHash2>()),
49-
cacheCreationTime_{util::getCurrentTimeSec()},
50-
nvmCacheState_{config_.cacheDir, config_.isNvmCacheEncryptionEnabled(),
51-
config_.isNvmCacheTruncateAllocSizeEnabled()} {
49+
cacheCreationTime_{util::getCurrentTimeSec()} {
5250
// TODO(MEMORY_TIER)
5351
if (std::holds_alternative<FileShmSegmentOpts>(
5452
memoryTierConfigs[0].getShmTypeOpts())) {
@@ -94,9 +92,7 @@ CacheAllocator<CacheTrait>::CacheAllocator(SharedMemNewT, Config config)
9492
[this](Item* it) -> ItemHandle { return acquire(it); })),
9593
chainedItemLocks_(config_.chainedItemsLockPower,
9694
std::make_shared<MurmurHash2>()),
97-
cacheCreationTime_{util::getCurrentTimeSec()},
98-
nvmCacheState_{config_.cacheDir, config_.isNvmCacheEncryptionEnabled(),
99-
config_.isNvmCacheTruncateAllocSizeEnabled()} {
95+
cacheCreationTime_{util::getCurrentTimeSec()} {
10096
initCommon(false);
10197
shmManager_->removeShm(detail::kShmInfoName,
10298
PosixSysVSegmentOpts(config_.isUsingPosixShm()));
@@ -131,9 +127,7 @@ CacheAllocator<CacheTrait>::CacheAllocator(SharedMemAttachT, Config config)
131127
[this](Item* it) -> ItemHandle { return acquire(it); })),
132128
chainedItemLocks_(config_.chainedItemsLockPower,
133129
std::make_shared<MurmurHash2>()),
134-
cacheCreationTime_{*metadata_.cacheCreationTime_ref()},
135-
nvmCacheState_{config_.cacheDir, config_.isNvmCacheEncryptionEnabled(),
136-
config_.isNvmCacheTruncateAllocSizeEnabled()} {
130+
cacheCreationTime_{*metadata_.cacheCreationTime_ref()} {
137131
for (auto pid : *metadata_.compactCachePools_ref()) {
138132
isCompactCachePool_[pid] = true;
139133
}
@@ -204,7 +198,7 @@ CacheAllocator<CacheTrait>::restoreCCacheManager() {
204198

205199
template <typename CacheTrait>
206200
void CacheAllocator<CacheTrait>::initCommon(bool dramCacheAttached) {
207-
if (config_.nvmConfig.has_value()) {
201+
if (config_.isNvmCacheEnabled()) {
208202
if (config_.nvmCacheAP) {
209203
nvmAdmissionPolicy_ = config_.nvmCacheAP;
210204
} else if (config_.rejectFirstAPNumEntries) {
@@ -227,25 +221,28 @@ void CacheAllocator<CacheTrait>::initCommon(bool dramCacheAttached) {
227221

228222
template <typename CacheTrait>
229223
void CacheAllocator<CacheTrait>::initNvmCache(bool dramCacheAttached) {
230-
if (!config_.nvmConfig.has_value()) {
224+
if (!config_.isNvmCacheEnabled()) {
231225
return;
232226
}
233227

228+
nvmCacheState_.emplace(NvmCacheState(config_.cacheDir, config_.isNvmCacheEncryptionEnabled(),
229+
config_.isNvmCacheTruncateAllocSizeEnabled()));
230+
234231
// for some usecases that create pools, restoring nvmcache when dram cache
235232
// is not persisted is not supported.
236233
const bool shouldDrop = config_.dropNvmCacheOnShmNew && !dramCacheAttached;
237234

238235
// if we are dealing with persistency, cache directory should be enabled
239236
const bool truncate = config_.cacheDir.empty() ||
240-
nvmCacheState_.shouldStartFresh() || shouldDrop;
237+
nvmCacheState_.value().shouldStartFresh() || shouldDrop;
241238
if (truncate) {
242-
nvmCacheState_.markTruncated();
239+
nvmCacheState_.value().markTruncated();
243240
}
244241

245242
nvmCache_ = std::make_unique<NvmCacheT>(*this, *config_.nvmConfig, truncate,
246243
config_.itemDestructor);
247244
if (!config_.cacheDir.empty()) {
248-
nvmCacheState_.clearPrevState();
245+
nvmCacheState_.value().clearPrevState();
249246
}
250247
}
251248

@@ -3113,7 +3110,7 @@ std::optional<bool> CacheAllocator<CacheTrait>::saveNvmCache() {
31133110
return false;
31143111
}
31153112

3116-
nvmCacheState_.markSafeShutDown();
3113+
nvmCacheState_.value().markSafeShutDown();
31173114
return true;
31183115
}
31193116

@@ -3310,8 +3307,8 @@ GlobalCacheStats CacheAllocator<CacheTrait>::getGlobalCacheStats() const {
33103307

33113308
const uint64_t currTime = util::getCurrentTimeSec();
33123309
ret.ramUpTime = currTime - cacheCreationTime_;
3313-
ret.nvmUpTime = currTime - nvmCacheState_.getCreationTime();
33143310
ret.nvmCacheEnabled = nvmCache_ ? nvmCache_->isEnabled() : false;
3311+
ret.nvmUpTime = currTime - getNVMCacheCreationTime();
33153312
ret.reaperStats = getReaperStats();
33163313
ret.numActiveHandles = getNumActiveHandles();
33173314

cachelib/allocator/CacheAllocator.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,8 +1082,17 @@ class CacheAllocator : public CacheBase {
10821082
//
10831083
// @return time when the cache was created.
10841084
time_t getCacheCreationTime() const noexcept { return cacheCreationTime_; }
1085+
1086+
// unix timestamp when the NVM cache was created. If NVM cahce isn't enaled,
1087+
// the cache creation time is returned instead.
1088+
//
1089+
// @return time when the NVM cache was created.
10851090
time_t getNVMCacheCreationTime() const {
1086-
return nvmCacheState_.getCreationTime();
1091+
auto result = getCacheCreationTime();
1092+
if (nvmCacheState_.has_value()) {
1093+
result = nvmCacheState_.value().getCreationTime();
1094+
}
1095+
return result;
10871096
}
10881097

10891098
// Inspects the cache without changing its state.
@@ -1939,7 +1948,7 @@ class CacheAllocator : public CacheBase {
19391948
folly::ThreadLocal<TlsActiveItemRing, DummyTlsActiveItemRingTag> ring_;
19401949

19411950
// state for the nvmcache
1942-
NvmCacheState nvmCacheState_;
1951+
std::optional<NvmCacheState> nvmCacheState_{};
19431952

19441953
// admission policy for nvmcache
19451954
std::shared_ptr<NvmAdmissionPolicy<CacheT>> nvmAdmissionPolicy_;

cachelib/allocator/CacheAllocatorConfig.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ class CacheAllocatorConfig {
9494
// Config for NvmCache. If enabled, cachelib will also make use of flash.
9595
CacheAllocatorConfig& enableNvmCache(NvmCacheConfig config);
9696

97+
bool isNvmCacheEnabled() const;
98+
9799
// enable the reject first admission policy through its parameters
98100
// @param numEntries the number of entries to track across all splits
99101
// @param numSplits the number of splits. we drop a whole split by
@@ -688,6 +690,11 @@ CacheAllocatorConfig<T>& CacheAllocatorConfig<T>::enableNvmCache(
688690
return *this;
689691
}
690692

693+
template <typename T>
694+
bool CacheAllocatorConfig<T>::isNvmCacheEnabled() const {
695+
return nvmConfig.has_value();
696+
}
697+
691698
template <typename T>
692699
CacheAllocatorConfig<T>& CacheAllocatorConfig<T>::setNvmCacheAdmissionPolicy(
693700
std::shared_ptr<NvmAdmissionPolicy<T>> policy) {

0 commit comments

Comments
 (0)