Skip to content

Commit 9291d51

Browse files
Converted nvmCacheState_ to std::optional to simplify NVM cache state handling when NVM cache state is not enabled
1 parent ee16a0a commit 9291d51

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
@@ -49,9 +49,7 @@ CacheAllocator<CacheTrait>::CacheAllocator(Config config)
4949
[this](Item* it) -> ItemHandle { return acquire(it); })),
5050
chainedItemLocks_(config_.chainedItemsLockPower,
5151
std::make_shared<MurmurHash2>()),
52-
cacheCreationTime_{util::getCurrentTimeSec()},
53-
nvmCacheState_{config_.cacheDir, config_.isNvmCacheEncryptionEnabled(),
54-
config_.isNvmCacheTruncateAllocSizeEnabled()} {
52+
cacheCreationTime_{util::getCurrentTimeSec()} {
5553
// TODO(MEMORY_TIER)
5654
if (std::holds_alternative<FileShmSegmentOpts>(
5755
memoryTierConfigs[0].getShmTypeOpts())) {
@@ -97,9 +95,7 @@ CacheAllocator<CacheTrait>::CacheAllocator(SharedMemNewT, Config config)
9795
[this](Item* it) -> ItemHandle { return acquire(it); })),
9896
chainedItemLocks_(config_.chainedItemsLockPower,
9997
std::make_shared<MurmurHash2>()),
100-
cacheCreationTime_{util::getCurrentTimeSec()},
101-
nvmCacheState_{config_.cacheDir, config_.isNvmCacheEncryptionEnabled(),
102-
config_.isNvmCacheTruncateAllocSizeEnabled()} {
98+
cacheCreationTime_{util::getCurrentTimeSec()} {
10399
initCommon(false);
104100
shmManager_->removeShm(detail::kShmInfoName,
105101
PosixSysVSegmentOpts(config_.isUsingPosixShm()));
@@ -134,9 +130,7 @@ CacheAllocator<CacheTrait>::CacheAllocator(SharedMemAttachT, Config config)
134130
[this](Item* it) -> ItemHandle { return acquire(it); })),
135131
chainedItemLocks_(config_.chainedItemsLockPower,
136132
std::make_shared<MurmurHash2>()),
137-
cacheCreationTime_{*metadata_.cacheCreationTime_ref()},
138-
nvmCacheState_{config_.cacheDir, config_.isNvmCacheEncryptionEnabled(),
139-
config_.isNvmCacheTruncateAllocSizeEnabled()} {
133+
cacheCreationTime_{*metadata_.cacheCreationTime_ref()} {
140134
for (auto pid : *metadata_.compactCachePools_ref()) {
141135
isCompactCachePool_[pid] = true;
142136
}
@@ -207,7 +201,7 @@ CacheAllocator<CacheTrait>::restoreCCacheManager() {
207201

208202
template <typename CacheTrait>
209203
void CacheAllocator<CacheTrait>::initCommon(bool dramCacheAttached) {
210-
if (config_.nvmConfig.has_value()) {
204+
if (config_.isNvmCacheEnabled()) {
211205
if (config_.nvmCacheAP) {
212206
nvmAdmissionPolicy_ = config_.nvmCacheAP;
213207
} else if (config_.rejectFirstAPNumEntries) {
@@ -230,24 +224,27 @@ void CacheAllocator<CacheTrait>::initCommon(bool dramCacheAttached) {
230224

231225
template <typename CacheTrait>
232226
void CacheAllocator<CacheTrait>::initNvmCache(bool dramCacheAttached) {
233-
if (!config_.nvmConfig.has_value()) {
227+
if (!config_.isNvmCacheEnabled()) {
234228
return;
235229
}
236230

231+
nvmCacheState_.emplace(NvmCacheState(config_.cacheDir, config_.isNvmCacheEncryptionEnabled(),
232+
config_.isNvmCacheTruncateAllocSizeEnabled()));
233+
237234
// for some usecases that create pools, restoring nvmcache when dram cache
238235
// is not persisted is not supported.
239236
const bool shouldDrop = config_.dropNvmCacheOnShmNew && !dramCacheAttached;
240237

241238
// if we are dealing with persistency, cache directory should be enabled
242239
const bool truncate = config_.cacheDir.empty() ||
243-
nvmCacheState_.shouldStartFresh() || shouldDrop;
240+
nvmCacheState_.value().shouldStartFresh() || shouldDrop;
244241
if (truncate) {
245-
nvmCacheState_.markTruncated();
242+
nvmCacheState_.value().markTruncated();
246243
}
247244

248245
nvmCache_ = std::make_unique<NvmCacheT>(*this, *config_.nvmConfig, truncate);
249246
if (!config_.cacheDir.empty()) {
250-
nvmCacheState_.clearPrevState();
247+
nvmCacheState_.value().clearPrevState();
251248
}
252249
}
253250

@@ -3057,7 +3054,7 @@ std::optional<bool> CacheAllocator<CacheTrait>::saveNvmCache() {
30573054
return false;
30583055
}
30593056

3060-
nvmCacheState_.markSafeShutDown();
3057+
nvmCacheState_.value().markSafeShutDown();
30613058
return true;
30623059
}
30633060

@@ -3252,8 +3249,8 @@ GlobalCacheStats CacheAllocator<CacheTrait>::getGlobalCacheStats() const {
32523249

32533250
const uint64_t currTime = util::getCurrentTimeSec();
32543251
ret.ramUpTime = currTime - cacheCreationTime_;
3255-
ret.nvmUpTime = currTime - nvmCacheState_.getCreationTime();
32563252
ret.nvmCacheEnabled = nvmCache_ ? nvmCache_->isEnabled() : false;
3253+
ret.nvmUpTime = currTime - getNVMCacheCreationTime();
32573254
ret.reaperStats = getReaperStats();
32583255
ret.numActiveHandles = getNumActiveHandles();
32593256

cachelib/allocator/CacheAllocator.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -968,8 +968,17 @@ class CacheAllocator : public CacheBase {
968968
//
969969
// @return time when the cache was created.
970970
time_t getCacheCreationTime() const noexcept { return cacheCreationTime_; }
971+
972+
// unix timestamp when the NVM cache was created. If NVM cahce isn't enaled,
973+
// the cache creation time is returned instead.
974+
//
975+
// @return time when the NVM cache was created.
971976
time_t getNVMCacheCreationTime() const {
972-
return nvmCacheState_.getCreationTime();
977+
auto result = getCacheCreationTime();
978+
if (nvmCacheState_.has_value()) {
979+
result = nvmCacheState_.value().getCreationTime();
980+
}
981+
return result;
973982
}
974983

975984
// Inspects the cache without changing its state.
@@ -1812,7 +1821,7 @@ class CacheAllocator : public CacheBase {
18121821
folly::ThreadLocal<TlsActiveItemRing, DummyTlsActiveItemRingTag> ring_;
18131822

18141823
// state for the nvmcache
1815-
NvmCacheState nvmCacheState_;
1824+
std::optional<NvmCacheState> nvmCacheState_{};
18161825

18171826
// admission policy for nvmcache
18181827
std::shared_ptr<NvmAdmissionPolicy<CacheT>> nvmAdmissionPolicy_;

cachelib/allocator/CacheAllocatorConfig.h

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

92+
bool isNvmCacheEnabled() const;
93+
9294
// enable the reject first admission policy through its parameters
9395
// @param numEntries the number of entries to track across all splits
9496
// @param numSplits the number of splits. we drop a whole split by
@@ -660,6 +662,11 @@ CacheAllocatorConfig<T>& CacheAllocatorConfig<T>::enableNvmCache(
660662
return *this;
661663
}
662664

665+
template <typename T>
666+
bool CacheAllocatorConfig<T>::isNvmCacheEnabled() const {
667+
return nvmConfig.has_value();
668+
}
669+
663670
template <typename T>
664671
CacheAllocatorConfig<T>& CacheAllocatorConfig<T>::setNvmCacheAdmissionPolicy(
665672
std::shared_ptr<NvmAdmissionPolicy<T>> policy) {

0 commit comments

Comments
 (0)